Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
/*	$NetBSD: pioc.c,v 1.18 2012/10/27 17:17:23 chs Exp $	*/     

/*
 * Copyright (c) 1997 Mark Brinicombe.
 * Copyright (c) 1997 Causality Limited.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed by Mark Brinicombe.
 * 4. The name of the company nor the name of the author may be used to
 *    endorse or promote products derived from this software without specific
 *    prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * Peripheral I/O controller - wd, fd, com, lpt Combo chip
 *
 * Parent device for combo chip I/O drivers
 * Currently supports the SMC FDC37GT66[56] controllers.
 */

/*#define PIOC_DEBUG*/

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: pioc.c,v 1.18 2012/10/27 17:17:23 chs Exp $");

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/device.h>
#include <sys/bus.h>

#include <arm/mainbus/mainbus.h>
#include <acorn32/mainbus/piocreg.h>
#include <acorn32/mainbus/piocvar.h>

#include "locators.h"

/*
 * PIOC device.
 *
 * This probes and attaches the top level pioc device.
 * It then configures any children of the pioc device.
 */

/*
 * pioc softc structure.
 *
 * Contains the device node, bus space tag, handle and address along with
 * other global information such as id and config registers.
 */

struct pioc_softc {
	bus_space_tag_t		sc_iot;			/* bus tag */
	bus_space_handle_t	sc_ioh;			/* bus handle */
	bus_addr_t		sc_iobase;		/* IO base address */
 	int			sc_id;			/* chip ID */
	int			sc_config[PIOC_CM_REGS];/* config regs */
};

/*
 * The pioc device is a parent to the com device.
 * This means that it needs to provide a bus space tag for
 * a serial console.
 *
 * XXX - This is not fully tested yet.
 */

extern struct bus_space mainbus_bs_tag;
bus_space_tag_t comconstag = &mainbus_bs_tag;

/* Prototypes for functions */

static int  piocmatch(device_t, cfdata_t, void *);
static void piocattach(device_t, device_t, void *);
static int  piocprint(void *aux, const char *name);
#if 0
static int  piocsearch(device_t, cfdata_t, void *);
#endif
static int  piocsubmatch(device_t, cfdata_t,
			      const int *, void *);
static void piocgetid(bus_space_tag_t iot, bus_space_handle_t ioh,
			      int config_entry, int *id, int *revision);

/* device attach and driver structure */

CFATTACH_DECL_NEW(pioc, sizeof(struct pioc_softc),
    piocmatch, piocattach, NULL, NULL);

/*
 * void piocgetid(bus_space_tag_t iot, bus_space_handle_t ioh,
 *                int config_entry, int *id, int *revision)
 *
 * Enter config mode and return the id and revision
 */

static void
piocgetid(bus_space_tag_t iot, bus_space_handle_t ioh, int config_entry, int *id, int *revision)
{
	/*
	 * Put the chip info configuration mode and read the ID and revision
	 */
	bus_space_write_1(iot, ioh, PIOC_CM_SELECT_REG, config_entry);
	bus_space_write_1(iot, ioh, PIOC_CM_SELECT_REG, config_entry);
	
	bus_space_write_1(iot, ioh, PIOC_CM_SELECT_REG, PIOC_CM_CRD);
	*id = bus_space_read_1(iot, ioh, PIOC_CM_DATA_REG);
	bus_space_write_1(iot, ioh, PIOC_CM_SELECT_REG, PIOC_CM_CRE);
	*revision = bus_space_read_1(iot, ioh, PIOC_CM_DATA_REG);

	bus_space_write_1(iot, ioh, PIOC_CM_SELECT_REG, PIOC_CM_EXIT);
}

/*
 * int piocmatch(device_t parent, cfdata_t cf, void *aux)
 *
 * Put the controller into config mode and probe the ID to see if
 * we recognise it.
 *
 * XXX - INTRUSIVE PROBE
 */ 
 
static int
piocmatch(device_t parent, cfdata_t cf, void *aux)
{
	struct mainbus_attach_args *mb = aux;
	bus_space_tag_t iot;
	bus_space_handle_t ioh;
	int id, rev;
	int rv = 1;

	/* We need a base address */
	if (mb->mb_iobase == MAINBUSCF_BASE_DEFAULT)
		return(0);

	iot = mb->mb_iot;
	if (bus_space_map(iot, mb->mb_iobase, PIOC_SIZE, 0, &ioh))
		return(0);

	mb->mb_iosize = PIOC_SIZE;

	piocgetid(iot, ioh, PIOC_CM_ENTER_665, &id, &rev);
	if (id == PIOC_CM_ID_665)
		goto out;

	piocgetid(iot, ioh, PIOC_CM_ENTER_666, &id, &rev);
	if (id == PIOC_CM_ID_666)
		goto out;

	rv = 0;

out:
	bus_space_unmap(iot, ioh, PIOC_SIZE);
	return(rv);
}

/*
 * int piocprint(void *aux, const char *name)
 *
 * print routine used during child configuration
 */

static int
piocprint(void *aux, const char *name)
{
	struct pioc_attach_args *pa = aux;

	if (!name) {
		if (pa->pa_offset)
			aprint_normal(" offset 0x%x", pa->pa_offset >> 2);
		if (pa->pa_iosize > 1)
			aprint_normal("-0x%x",
			    ((pa->pa_offset >> 2) + pa->pa_iosize) - 1);
		if (pa->pa_irq != -1)
			aprint_normal(" irq %d", pa->pa_irq);
		if (pa->pa_drq != -1)
			aprint_normal(" drq 0x%08x", pa->pa_drq);
	}

/* XXX print flags */
	return (QUIET);
}

#if 0
/*
 * int piocsearch(device_t parent, cfdata_t cf, void *aux)
 *
 * search function used to probe and attach the child devices.
 *
 * Note: since the offsets of the devices need to be specified in the
 * config file we ignore the FSTAT_STAR.
 */

static int
piocsearch(device_t parent, cfdata_t cf, const int *ldesc, void *aux)
{
	struct pioc_softc *sc = device_private(parent);
	struct pioc_attach_args pa;
	int tryagain;

	do {
		pa.pa_name = NULL;
		pa.pa_iobase = sc->sc_iobase;
		pa.pa_iosize = 0;
		pa.pa_iot = sc->sc_iot;
		if (cf->cf_loc[PIOCCF_OFFSET] == PIOCCF_OFFSET_DEFAULT) {
			pa.pa_offset = PIOCCF_OFFSET_DEFAULT;
			pa.pa_drq = PIOCCF_DACK_DEFAULT;
			pa.pa_irq = PIOCCF_IRQ_DEFAULT;
		} else {    
			pa.pa_offset = (cf->cf_loc[PIOCCF_OFFSET] << 2);
			pa.pa_drq = cf->cf_loc[PIOCCF_DACK];
			pa.pa_irq = cf->cf_loc[PIOCCF_IRQ];
		}

		tryagain = 0;
		if (config_match(parent, cf, &pa) > 0) {
			config_attach(parent, cf, &pa, piocprint);
/*			tryagain = (cf->cf_fstate == FSTATE_STAR);*/
		}
	} while (tryagain);

	return (0);
}
#endif

/*
 * int piocsubmatch(device_t parent, cfdata_t cf, void *aux)
 *
 * search function used to probe and attach the child devices.
 *
 * Note: since the offsets of the devices need to be specified in the
 * config file we ignore the FSTAT_STAR.
 */

static int
piocsubmatch(device_t parent, cfdata_t cf, const int *ldesc, void *aux)
{
	struct pioc_attach_args *pa = aux;
	int tryagain;

	if ((pa->pa_offset >> 2) != cf->cf_loc[PIOCCF_OFFSET])
		return(0);
	do {
		if (pa->pa_drq == -1)
			pa->pa_drq = cf->cf_loc[PIOCCF_DACK];
		if (pa->pa_irq == -1)
			pa->pa_irq = cf->cf_loc[PIOCCF_IRQ];
		tryagain = 0;
		if (config_match(parent, cf, pa) > 0) {
			config_attach(parent, cf, pa, piocprint);
/*			tryagain = (cf->cf_fstate == FSTATE_STAR);*/
		}
	} while (tryagain);
	
	return (0);
}

/*
 * void piocattach(device_t parent, device_t dev, void *aux)
 *
 * Identify the PIOC and read the config registers into the softc.
 * Search and configure all children
 */
  
static void
piocattach(device_t parent, device_t self, void *aux)
{
	struct mainbus_attach_args *mb = aux;
	struct pioc_softc *sc = device_private(self);
	bus_space_tag_t iot;
	bus_space_handle_t ioh;
	int id, rev;
	int loop;
	struct pioc_attach_args pa;

	sc->sc_iobase = mb->mb_iobase;
	iot = sc->sc_iot = mb->mb_iot;

	if (bus_space_map(iot, sc->sc_iobase, PIOC_SIZE, 0, &ioh))
		panic("%s: couldn't map I/O space", device_xname(self));
	sc->sc_ioh = ioh;

	piocgetid(iot, ioh, PIOC_CM_ENTER_665, &id, &rev);
	if (id != PIOC_CM_ID_665)
		piocgetid(iot, ioh, PIOC_CM_ENTER_666, &id, &rev);
	
	printf("\n%s: ", device_xname(self));

	/* Do we recognise it ? */
	switch (id) {
	case PIOC_CM_ID_665:
	case PIOC_CM_ID_666:
		printf("SMC FDC37C6%xGT peripheral controller rev %d\n", id, rev);
		break;
	default:
		printf("Unrecognised peripheral controller id=%2x rev=%2x\n", id, rev);
		return;
	}

	sc->sc_id = id;

	/*
	 * Put the chip info configuration mode and save all the registers
	 */

	switch (id) {
	case PIOC_CM_ID_665:
		bus_space_write_1(iot, ioh, PIOC_CM_SELECT_REG, PIOC_CM_ENTER_665);
		bus_space_write_1(iot, ioh, PIOC_CM_SELECT_REG, PIOC_CM_ENTER_665);
		break;
		
	case PIOC_CM_ID_666:
		bus_space_write_1(iot, ioh, PIOC_CM_SELECT_REG, PIOC_CM_ENTER_666);
		bus_space_write_1(iot, ioh, PIOC_CM_SELECT_REG, PIOC_CM_ENTER_666);
		break;
	}

	for (loop = 0; loop < PIOC_CM_REGS; ++loop) {
		bus_space_write_1(iot, ioh, PIOC_CM_SELECT_REG, loop);
		sc->sc_config[loop] = bus_space_read_1(iot, ioh, PIOC_CM_DATA_REG);
	}

	bus_space_write_1(iot, ioh, PIOC_CM_SELECT_REG, PIOC_CM_EXIT);

#ifdef PIOC_DEBUG
	printf("%s: ", device_xname(self));

	for (loop = 0; loop < PIOC_CM_REGS; ++loop)
		printf("%02x ", sc->sc_config[loop]);
	printf("\n");
#endif

	/*
	 * Ok as yet we cannot do specific config_found() calls
	 * for the children yet. This is because the pioc device does
	 * not know the interrupt numbers to use.
	 * Eventually this information will have to be provided by the
	 * riscpc specific code.
	 * Until then just do a config_search_ia() and pick the info up
	 * from the cfdata.
	 * Note the child devices require some modifications as well.
	 */

	/*
	 * Ok Now configure the child devices of the pioc device
	 * Use the pioc config registers to determine the addressing
	 * of the children
	 */

	/*
	 * Start by configuring the IDE controller
	 */	

	if (sc->sc_config[PIOC_CM_CR0] & PIOC_WDC_ENABLE) {
		pa.pa_name = "wdc";
		pa.pa_iobase = sc->sc_iobase;
		pa.pa_iosize = 0;
		pa.pa_iot = iot;
		if (sc->sc_config[PIOC_CM_CR5] & PIOC_WDC_SECONDARY)
			pa.pa_offset = (PIOC_WDC_SECONDARY_OFFSET << 2);
		else
			pa.pa_offset = (PIOC_WDC_PRIMARY_OFFSET << 2);
		pa.pa_drq = -1;
		pa.pa_irq = -1;
		config_found_sm_loc(self, "pioc", NULL, &pa, piocprint,
				    piocsubmatch);
	}

	/*
	 * Next configure the floppy controller
	 */	

	if (sc->sc_config[PIOC_CM_CR0] & PIOC_FDC_ENABLE) {
		pa.pa_name = "fdc";
		pa.pa_iobase = sc->sc_iobase;
		pa.pa_iosize = 0;
		pa.pa_iot = iot;
		if (sc->sc_config[PIOC_CM_CR5] & PIOC_FDC_SECONDARY)
			pa.pa_offset = (PIOC_FDC_SECONDARY_OFFSET << 2);
		else
			pa.pa_offset = (PIOC_FDC_PRIMARY_OFFSET << 2);
		pa.pa_drq = -1;
		pa.pa_irq = -1;
		config_found_sm_loc(self, "pioc", NULL, &pa, piocprint,
				    piocsubmatch);
	}

	/*
	 * Next configure the serial ports
	 */

	/*
	 * XXX - There is a deficiency in the serial configuration
	 * If the PIOC has the serial ports configured for COM3 and COM4
	 * the standard COM3 and COM4 addresses are assumed rather than
	 * examining CR1 to determine the COM3 and COM4 addresses.
	 */

	if (sc->sc_config[PIOC_CM_CR2] & PIOC_UART1_ENABLE) {
		pa.pa_name = "com";
		pa.pa_iobase = sc->sc_iobase;
		pa.pa_iosize = 0;
		pa.pa_iot = iot;
		switch (sc->sc_config[PIOC_CM_CR2] & PIOC_UART1_ADDR_MASK) {
		case PIOC_UART1_ADDR_COM1:
			pa.pa_offset = (PIOC_COM1_OFFSET << 2);
			break;
		case PIOC_UART1_ADDR_COM2:
			pa.pa_offset = (PIOC_COM2_OFFSET << 2);
			break;
		case PIOC_UART1_ADDR_COM3:
			pa.pa_offset = (PIOC_COM3_OFFSET << 2);
			break;
		case PIOC_UART1_ADDR_COM4:
			pa.pa_offset = (PIOC_COM4_OFFSET << 2);
			break;
		}
		pa.pa_drq = -1;
		pa.pa_irq = -1;
		config_found_sm_loc(self, "pioc", NULL, &pa, piocprint,
				    piocsubmatch);
	}

	if (sc->sc_config[PIOC_CM_CR2] & PIOC_UART2_ENABLE) {
		pa.pa_name = "com";
		pa.pa_iobase = sc->sc_iobase;
		pa.pa_iosize = 0;
		pa.pa_iot = iot;
		switch (sc->sc_config[PIOC_CM_CR2] & PIOC_UART2_ADDR_MASK) {
		case PIOC_UART2_ADDR_COM1:
			pa.pa_offset = (PIOC_COM1_OFFSET << 2);
			break;
		case PIOC_UART2_ADDR_COM2:
			pa.pa_offset = (PIOC_COM2_OFFSET << 2);
			break;
		case PIOC_UART2_ADDR_COM3:
			pa.pa_offset = (PIOC_COM3_OFFSET << 2);
			break;
		case PIOC_UART2_ADDR_COM4:
			pa.pa_offset = (PIOC_COM4_OFFSET << 2);
			break;
		}
		pa.pa_drq = -1;
		pa.pa_irq = -1;
		config_found_sm_loc(self, "pioc", NULL, &pa, piocprint,
				    piocsubmatch);
	}

	/*
	 * Next configure the printer port
	 */
	
	if ((sc->sc_config[PIOC_CM_CR1] & PIOC_LPT_ADDR_MASK) != PIOC_LPT_ADDR_DISABLE) {
		pa.pa_name = "lpt";
		pa.pa_iobase = sc->sc_iobase;
		pa.pa_iosize = 0;
		pa.pa_iot = iot;
		switch (sc->sc_config[PIOC_CM_CR1] & PIOC_LPT_ADDR_MASK) {
		case PIOC_LPT_ADDR_1:
			pa.pa_offset = (PIOC_LPT1_OFFSET << 2);
			break;
		case PIOC_LPT_ADDR_2:
			pa.pa_offset = (PIOC_LPT2_OFFSET << 2);
			break;
		case PIOC_LPT_ADDR_3:
			pa.pa_offset = (PIOC_LPT3_OFFSET << 2);
			break;
		}
		pa.pa_drq = -1;
		pa.pa_irq = -1;
		config_found_sm_loc(self, "pioc", NULL, &pa, piocprint,
				    piocsubmatch);
	}

#if 0
	config_search_ia(piocsearch, self, "pioc", NULL);
#endif
}

/* End of pioc.c */