Home | History | Annotate | Line # | Download | only in pnpbus
pnpbus.c revision 1.4
      1 /*	$NetBSD: pnpbus.c,v 1.4 2006/06/15 18:15:32 garbled Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1998 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Tim Rightnour
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *        This product includes software developed by the NetBSD
     21  *        Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 #include <sys/cdefs.h>
     40 __KERNEL_RCSID(0, "$NetBSD: pnpbus.c,v 1.4 2006/06/15 18:15:32 garbled Exp $");
     41 
     42 #include <sys/param.h>
     43 #include <sys/systm.h>
     44 #include <sys/device.h>
     45 #include <sys/extent.h>
     46 #include <sys/malloc.h>
     47 
     48 #include <machine/bus.h>
     49 #include <machine/pio.h>
     50 #include <machine/intr.h>
     51 #include <machine/platform.h>
     52 #include <machine/residual.h>
     53 #include <machine/pnp.h>
     54 #include <machine/isa_machdep.h>
     55 
     56 #include <dev/isa/isareg.h>
     57 
     58 #include <prep/pnpbus/pnpbusvar.h>
     59 
     60 static int	pnpbus_match(struct device *, struct cfdata *, void *);
     61 static void	pnpbus_attach(struct device *, struct device *, void *);
     62 static int	pnpbus_print(void *, const char *);
     63 static int	pnpbus_search(struct device *, struct cfdata *,
     64 			      const int *, void *);
     65 
     66 CFATTACH_DECL(pnpbus, sizeof(struct pnpbus_softc),
     67     pnpbus_match, pnpbus_attach, NULL, NULL);
     68 
     69 struct pnpbus_softc *pnpbus_softc;
     70 extern struct cfdriver pnpbus_cd;
     71 
     72 static int
     73 pnpbus_match(struct device *parent, struct cfdata *cf, void *aux)
     74 {
     75 	return 1;
     76 }
     77 
     78 static void
     79 pnpbus_attach(struct device *parent, struct device *self, void *aux)
     80 {
     81 	struct pnpbus_softc *sc = (struct pnpbus_softc *)self;
     82 	struct pnpbus_attach_args *paa = aux;
     83 
     84 	printf("\n");
     85 
     86 	pnpbus_softc = sc;
     87 	sc->sc_ic = paa->paa_ic;
     88 	sc->sc_iot = paa->paa_iot;
     89 	sc->sc_memt = paa->paa_memt;
     90 
     91 	(void)config_search_ia(pnpbus_search, self, "pnpbus", aux);
     92 }
     93 
     94 static int
     95 pnp_newirq(void *v, struct pnpresources *r, int size)
     96 {
     97 	struct _S4_Pack *p = v;
     98 	struct pnpbus_irq *irq;
     99 
    100 	irq = malloc(sizeof(struct pnpbus_irq), M_DEVBUF, M_NOWAIT);
    101 
    102 	irq->mask = le16dec(&p->IRQMask[0]);
    103 
    104 	if (size > 2)
    105 		irq->flags = p->IRQInfo;
    106 	else
    107 		irq->flags = 0x1;
    108 
    109 	SIMPLEQ_INSERT_TAIL(&r->irq, irq, next);
    110 	r->numirq++;
    111 
    112 	return 0;
    113 }
    114 
    115 static int
    116 pnp_newdma(void *v, struct pnpresources *r, int size)
    117 {
    118 	struct _S5_Pack *p = v;
    119 	struct pnpbus_dma *dma;
    120 
    121 	dma = malloc(sizeof(struct pnpbus_dma), M_DEVBUF, M_NOWAIT);
    122 
    123 	dma->mask = le16dec(&p->DMAMask);
    124 	if (size > 2)
    125 		dma->flags = p->DMAInfo;
    126 	else
    127 		dma->flags = 0x01;
    128 
    129 	SIMPLEQ_INSERT_TAIL(&r->dma, dma, next);
    130 	r->numdma++;
    131 
    132 	return 0;
    133 }
    134 
    135 static int
    136 pnp_newioport(void *v, struct pnpresources *r, int size)
    137 {
    138 	struct _S8_Pack *p = v;
    139 	struct pnpbus_io *io;
    140 	uint16_t mask;
    141 
    142 	io = malloc(sizeof(struct pnpbus_io), M_DEVBUF, M_NOWAIT);
    143 	mask = p->IOInfo & ISAAddr16bit ? 0xffff : 0x03ff;
    144 	io->minbase = (p->RangeMin[0] | (p->RangeMin[1] << 8)) & mask;
    145 	io->maxbase = (p->RangeMax[0] | (p->RangeMax[1] << 8)) & mask;
    146 	io->align = p->IOAlign;
    147 	io->len = p->IONum;
    148 	io->flags = p->IOInfo;
    149 
    150 	SIMPLEQ_INSERT_TAIL(&r->io, io, next);
    151 	r->numio++;
    152 
    153 	return 0;
    154 }
    155 
    156 static int
    157 pnp_newfixedioport(void *v, struct pnpresources *r, int size)
    158 {
    159 	struct _S9_Pack *p = v;
    160 	struct pnpbus_io *io;
    161 
    162 	io = malloc(sizeof(struct pnpbus_io), M_DEVBUF, M_NOWAIT);
    163 	io->minbase = (p->Range[0] | (p->Range[1] << 8)) & 0x3ff;
    164 	io->len = p->IONum;
    165 	io->maxbase = -1;
    166 	io->flags = 0;
    167 	io->align = 1;
    168 
    169 	SIMPLEQ_INSERT_TAIL(&r->io, io, next);
    170 	r->numio++;
    171 
    172 	return 0;
    173 }
    174 
    175 static int
    176 pnp_newaddr(void *v, struct pnpresources *r, int size)
    177 {
    178 	struct pnpbus_io *io;
    179 	struct pnpbus_mem *mem;
    180 	struct _L4_Pack *pack = v;
    181 	struct _L4_PPCPack *p =  &pack->L4_Data.L4_PPCPack;
    182 
    183 	if (p->PPCData[0] == 1) {/* type IO */
    184 		io = malloc(sizeof(struct pnpbus_io), M_DEVBUF, M_NOWAIT);
    185 		io->minbase = (uint16_t)le64dec(&p->PPCData[4]);
    186 		io->maxbase = -1;
    187 		io->align = p->PPCData[1];
    188 		io->len = (uint16_t)le64dec(&p->PPCData[12]);
    189 		io->flags = 0;
    190 		SIMPLEQ_INSERT_TAIL(&r->io, io, next);
    191 		r->numio++;
    192 
    193 		return 0;
    194 	} else if (p->PPCData[0] == 2) {
    195 		mem = malloc(sizeof(struct pnpbus_mem), M_DEVBUF, M_NOWAIT);
    196 		mem->minbase = (uint32_t)le64dec(&p->PPCData[4]);
    197 		mem->maxbase = -1;
    198 		mem->align = p->PPCData[1];
    199 		mem->len = (uint32_t)le64dec(&p->PPCData[12]);
    200 		mem->flags = 0;
    201 		SIMPLEQ_INSERT_TAIL(&r->mem, mem, next);
    202 		r->nummem++;
    203 
    204 		return 0;
    205 	} else
    206 		return -1;
    207 }
    208 
    209 static int
    210 pnp_newcompatid(void *v, struct pnpresources *r, int size)
    211 {
    212 	struct _S3_Pack *p = v;
    213 	struct pnpbus_compatid *id;
    214 	uint32_t cid;
    215 
    216 	id = malloc(sizeof(*id), M_DEVBUF, M_NOWAIT);
    217 	cid = le32dec(p->CompatId);
    218 	pnp_devid_to_string(cid, id->idstr);
    219 	id->next = r->compatids;
    220 	r->compatids = id;
    221 
    222 	return 0;
    223 }
    224 
    225 /*
    226  * Call if match succeeds.  This way we don't allocate lots of ram
    227  * for structures we never use if the device isn't attached.
    228  */
    229 
    230 int
    231 pnpbus_scan(struct pnpbus_dev_attach_args *pna, PPC_DEVICE *dev)
    232 {
    233 	struct pnpresources *r = &pna->pna_res;
    234 	uint32_t l;
    235 	uint8_t *p, *q;
    236 	void *v;
    237 	int tag, size, item;
    238 
    239 	l = be32toh(dev->AllocatedOffset);
    240 	p = res->DevicePnPHeap + l;
    241 
    242 	if (p == NULL)
    243 		return -1;
    244 
    245 	for (; p[0] != END_TAG; p += size) {
    246 		tag = *p;
    247 		v = p;
    248 		if (tag_type(p[0]) == PNP_SMALL) {
    249 			size = tag_small_count(tag) + 1;
    250 			item = tag_small_item_name(tag);
    251 			switch (item) {
    252 			case IRQFormat:
    253 				pnp_newirq(v, r, size);
    254 				break;
    255 			case DMAFormat:
    256 				pnp_newdma(v, r, size);
    257 				break;
    258 			case IOPort:
    259 				pnp_newioport(v, r, size);
    260 				break;
    261 			case FixedIOPort:
    262 				pnp_newfixedioport(v, r, size);
    263 				break;
    264 			}
    265 		} else {
    266 			struct _L4_Pack *pack = v;
    267 			struct _L4_PPCPack *pa = &pack->L4_Data.L4_PPCPack;
    268 
    269 			q = p;
    270 			size = (q[1] | (q[2] << 8)) + 3 /* tag + length */;
    271 			item = tag_large_item_name(tag);
    272 			if (item == LargeVendorItem &&
    273 			    pa->Type == LV_GenericAddress)
    274 				pnp_newaddr(v, r, size);
    275 		}
    276 	}
    277 
    278 	/* scan for compatid's */
    279 
    280 	l = be32toh(dev->CompatibleOffset);
    281 	p = res->DevicePnPHeap + l;
    282 
    283 	if (p == NULL)
    284 		return -1;
    285 
    286 	for (; p[0] != END_TAG; p += size) {
    287 		tag = *p;
    288 		v = p;
    289 		if (tag_type(p[0]) == PNP_SMALL) {
    290 			size = tag_small_count(tag) + 1;
    291 			item = tag_small_item_name(tag);
    292 			switch (item) {
    293 			case CompatibleDevice:
    294 				pnp_newcompatid(v, r, size);
    295 				break;
    296 			}
    297 		} else {
    298 			q = p;
    299 			size = (q[1] | (q[2] << 8)) + 3 /* tag + length */;
    300 		}
    301 	}
    302 	return 0;
    303 }
    304 
    305 /*
    306  * Setup the basic pna structure.
    307  */
    308 
    309 static void
    310 pnp_getpna(struct pnpbus_dev_attach_args *pna, struct pnpbus_attach_args *paa,
    311 	PPC_DEVICE *dev)
    312 {
    313 	uint32_t l;
    314 	DEVICE_ID *id = &dev->DeviceId;
    315 	struct pnpresources *r = &pna->pna_res;
    316 
    317 	l = be32toh(dev->AllocatedOffset);
    318 
    319 	pna->pna_iot = paa->paa_iot;
    320 	pna->pna_memt = paa->paa_memt;
    321 	pna->pna_ic = paa->paa_ic;
    322 	pnp_devid_to_string(id->DevId, pna->pna_devid);
    323 	pna->basetype = id->BaseType;
    324 	pna->subtype = id->SubType;
    325 	pna->interface = id->Interface;
    326 	pna->pna_ppc_dev = dev;
    327 	memset(r, 0, sizeof(*r));
    328 	SIMPLEQ_INIT(&r->mem);
    329 	SIMPLEQ_INIT(&r->io);
    330 	SIMPLEQ_INIT(&r->irq);
    331 	SIMPLEQ_INIT(&r->dma);
    332 }
    333 
    334 static int
    335 pnpbus_search(struct device *parent, struct cfdata *cf,
    336 	    const int *ldesc, void *aux)
    337 {
    338 	struct pnpbus_dev_attach_args pna;
    339 	struct pnpbus_attach_args *paa = aux;
    340 	PPC_DEVICE *ppc_dev;
    341 	int i;
    342 	uint32_t ndev;
    343 
    344 	ndev = be32toh(res->ActualNumDevices);
    345 	ppc_dev = res->Devices;
    346 
    347 	for (i = 0; i < ((ndev > MAX_DEVICES) ? MAX_DEVICES : ndev); i++) {
    348 		pnp_getpna(&pna, paa, &ppc_dev[i]);
    349 		if (config_match(parent, cf, &pna) > 0)
    350 			config_attach(parent, cf, &pna, pnpbus_print);
    351 	}
    352 
    353 	return 0;
    354 }
    355 
    356 static void
    357 pnpbus_printres(struct pnpresources *r)
    358 {
    359 	struct pnpbus_io *io;
    360 	struct pnpbus_mem *mem;
    361 	struct pnpbus_irq *irq;
    362 	struct pnpbus_dma *dma;
    363 	int p = 0;
    364 
    365 	if (!SIMPLEQ_EMPTY(&r->mem)) {
    366 		aprint_normal("mem");
    367 		SIMPLEQ_FOREACH(mem, &r->mem, next) {
    368 			aprint_normal(" 0x%x", mem->minbase);
    369 			if (mem->len > 1)
    370 				aprint_normal("-0x%x",
    371 				    mem->minbase + mem->len - 1);
    372 		}
    373 		p++;
    374 	}
    375 	if (!SIMPLEQ_EMPTY(&r->io)) {
    376 		if (p++)
    377 			aprint_normal(", ");
    378 		aprint_normal("port");
    379 		SIMPLEQ_FOREACH(io, &r->io, next) {
    380 			aprint_normal(" 0x%x", io->minbase);
    381 			if (io->len > 1)
    382 				aprint_normal("-0x%x",
    383 				    io->minbase + io->len - 1);
    384 		}
    385 	}
    386 	if (!SIMPLEQ_EMPTY(&r->irq)) {
    387 		if (p++)
    388 			aprint_normal(", ");
    389 		aprint_normal("irq");
    390 		SIMPLEQ_FOREACH(irq, &r->irq, next) {
    391 			aprint_normal(" %d", ffs(irq->mask) - 1);
    392 		}
    393 	}
    394 	if (!SIMPLEQ_EMPTY(&r->dma)) {
    395 		if (p++)
    396 			aprint_normal(", ");
    397 		aprint_normal("DMA");
    398 		SIMPLEQ_FOREACH(dma, &r->dma, next) {
    399 			aprint_normal(" %d", ffs(dma->mask) - 1);
    400 		}
    401 	}
    402 }
    403 
    404 void
    405 pnpbus_print_devres(struct pnpbus_dev_attach_args *pna)
    406 {
    407 	aprint_normal(": ");
    408 	pnpbus_printres(&pna->pna_res);
    409 }
    410 
    411 static int
    412 pnpbus_print(void *args, const char *name)
    413 {
    414 	struct pnpbus_dev_attach_args *pna = args;
    415 
    416 	pnpbus_print_devres(pna);
    417 	return (UNCONF);
    418 }
    419 
    420 /*
    421  * Set up an interrupt handler to start being called.
    422  */
    423 void *
    424 pnpbus_intr_establish(int idx, int level, int (*ih_fun)(void *),
    425     void *ih_arg, struct pnpresources *r)
    426 {
    427 	struct pnpbus_irq *irq;
    428 	int irqnum, type;
    429 
    430 	if (idx >= r->numirq)
    431 		return 0;
    432 
    433 	irq = SIMPLEQ_FIRST(&r->irq);
    434 	while (idx--)
    435 		irq = SIMPLEQ_NEXT(irq, next);
    436 
    437 	irqnum = ffs(irq->mask) - 1;
    438 	type = (irq->flags & 0x0c) ? IST_LEVEL : IST_EDGE;
    439 
    440 	return (void *)intr_establish(irqnum, type, level, ih_fun, ih_arg);
    441 }
    442 
    443 /*
    444  * Deregister an interrupt handler.
    445  */
    446 void
    447 pnpbus_intr_disestablish(void *arg)
    448 {
    449 
    450 	intr_disestablish(arg);
    451 }
    452 
    453 int
    454 pnpbus_getirqnum(struct pnpresources *r, int idx, int *irqp, int *istp)
    455 {
    456 	struct pnpbus_irq *irq;
    457 
    458 	if (idx >= r->numirq)
    459 		return EINVAL;
    460 
    461 	irq = SIMPLEQ_FIRST(&r->irq);
    462 	while (idx--)
    463 		irq = SIMPLEQ_NEXT(irq, next);
    464 
    465 	if (irqp != NULL)
    466 		*irqp = ffs(irq->mask) - 1;
    467 	if (istp != NULL)
    468 		*istp = (irq->flags &0x0c) ? IST_LEVEL : IST_EDGE;
    469 	return 0;
    470 }
    471 
    472 int
    473 pnpbus_getdmachan(struct pnpresources *r, int idx, int *chanp)
    474 {
    475 	struct pnpbus_dma *dma;
    476 
    477 	if (idx >= r->numdma)
    478 		return EINVAL;
    479 
    480 	dma = SIMPLEQ_FIRST(&r->dma);
    481 	while (idx--)
    482 		dma = SIMPLEQ_NEXT(dma, next);
    483 
    484 	if (chanp != NULL)
    485 		*chanp = ffs(dma->mask) - 1;
    486 	return 0;
    487 }
    488 
    489 int
    490 pnpbus_getioport(struct pnpresources *r, int idx, int *basep, int *sizep)
    491 {
    492 	struct pnpbus_io *io;
    493 
    494 	if (idx >= r->numio)
    495 		return EINVAL;
    496 
    497 	io = SIMPLEQ_FIRST(&r->io);
    498 	while (idx--)
    499 		io = SIMPLEQ_NEXT(io, next);
    500 
    501 	if (basep)
    502 		*basep = io->minbase;
    503 	if (sizep)
    504 		*sizep = io->len;
    505 	return 0;
    506 }
    507 
    508 int
    509 pnpbus_io_map(struct pnpresources *r, int idx, bus_space_tag_t *tagp,
    510     bus_space_handle_t *hdlp)
    511 {
    512 	struct pnpbus_io *io;
    513 
    514 	if (idx >= r->numio)
    515 		return EINVAL;
    516 
    517 	io = SIMPLEQ_FIRST(&r->io);
    518 	while (idx--)
    519 		io = SIMPLEQ_NEXT(io, next);
    520 
    521 	*tagp = &prep_isa_io_space_tag;
    522 	return (bus_space_map(&prep_isa_io_space_tag, io->minbase, io->len,
    523 	    0, hdlp));
    524 }
    525 
    526 void
    527 pnpbus_io_unmap(struct pnpresources *r, int idx, bus_space_tag_t tag,
    528     bus_space_handle_t hdl)
    529 {
    530 	struct pnpbus_io *io;
    531 
    532 	if (idx >= r->numio)
    533 		return;
    534 
    535 	io = SIMPLEQ_FIRST(&r->io);
    536 	while (idx--)
    537 		io = SIMPLEQ_NEXT(io, next);
    538 
    539 	bus_space_unmap(tag, hdl, io->len);
    540 }
    541