Home | History | Annotate | Line # | Download | only in pnpbus
pnpbus.c revision 1.1
      1 /*	$NetBSD: pnpbus.c,v 1.1 2006/03/09 20:17:27 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.1 2006/03/09 20:17:27 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 	if (size > 2)
    104 		irq->flags = p->IRQInfo;
    105 	else
    106 		irq->flags = 0x01;
    107 
    108 	SIMPLEQ_INSERT_TAIL(&r->irq, irq, next);
    109 	r->numirq++;
    110 
    111 	return 0;
    112 }
    113 
    114 static int
    115 pnp_newdma(void *v, struct pnpresources *r, int size)
    116 {
    117 	struct _S5_Pack *p = v;
    118 	struct pnpbus_dma *dma;
    119 
    120 	dma = malloc(sizeof(struct pnpbus_dma), M_DEVBUF, M_NOWAIT);
    121 
    122 	dma->mask = le16dec(&p->DMAMask);
    123 	if (size > 2)
    124 		dma->flags = p->DMAInfo;
    125 	else
    126 		dma->flags = 0x01;
    127 
    128 	SIMPLEQ_INSERT_TAIL(&r->dma, dma, next);
    129 	r->numdma++;
    130 
    131 	return 0;
    132 }
    133 
    134 static int
    135 pnp_newioport(void *v, struct pnpresources *r, int size)
    136 {
    137 	struct _S8_Pack *p = v;
    138 	struct pnpbus_io *io;
    139 	uint16_t mask;
    140 
    141 	io = malloc(sizeof(struct pnpbus_io), M_DEVBUF, M_NOWAIT);
    142 	mask = p->IOInfo & ISAAddr16bit ? 0xffff : 0x03ff;
    143 	io->minbase = (p->RangeMin[0] | (p->RangeMin[1] << 8)) & mask;
    144 	io->maxbase = (p->RangeMax[0] | (p->RangeMax[1] << 8)) & mask;
    145 	io->align = p->IOAlign;
    146 	io->len = p->IONum;
    147 	io->flags = p->IOInfo;
    148 
    149 	SIMPLEQ_INSERT_TAIL(&r->io, io, next);
    150 	r->numio++;
    151 
    152 	return 0;
    153 }
    154 
    155 static int
    156 pnp_newaddr(void *v, struct pnpresources *r, int size)
    157 {
    158 	struct pnpbus_io *io;
    159 	struct pnpbus_mem *mem;
    160 	struct _L4_Pack *pack = v;
    161 	struct _L4_PPCPack *p =  &pack->L4_Data.L4_PPCPack;
    162 
    163 	if (p->PPCData[0] == 1) {/* type IO */
    164 		io = malloc(sizeof(struct pnpbus_io), M_DEVBUF, M_NOWAIT);
    165 		io->minbase = (uint16_t)le64dec(&p->PPCData[4]);
    166 		io->maxbase = -1;
    167 		io->align = p->PPCData[1];
    168 		io->len = (uint16_t)le64dec(&p->PPCData[12]);
    169 		io->flags = 0;
    170 		SIMPLEQ_INSERT_TAIL(&r->io, io, next);
    171 		r->numio++;
    172 
    173 		return 0;
    174 	} else if (p->PPCData[0] == 2) {
    175 		mem = malloc(sizeof(struct pnpbus_mem), M_DEVBUF, M_NOWAIT);
    176 		mem->minbase = (uint32_t)le64dec(&p->PPCData[4]);
    177 		mem->maxbase = -1;
    178 		mem->align = p->PPCData[1];
    179 		mem->len = (uint32_t)le64dec(&p->PPCData[12]);
    180 		mem->flags = 0;
    181 		SIMPLEQ_INSERT_TAIL(&r->mem, mem, next);
    182 		r->nummem++;
    183 
    184 		return 0;
    185 	} else
    186 		return -1;
    187 }
    188 
    189 static int
    190 pnp_newcompatid(void *v, struct pnpresources *r, int size)
    191 {
    192 	struct _S3_Pack *p = v;
    193 	struct pnpbus_compatid *id;
    194 	uint32_t cid;
    195 
    196 	id = malloc(sizeof(*id), M_DEVBUF, M_NOWAIT);
    197 	cid = le32dec(p->CompatId);
    198 	pnp_devid_to_string(cid, id->idstr);
    199 	id->next = r->compatids;
    200 	r->compatids = id;
    201 
    202 	return 0;
    203 }
    204 
    205 /*
    206  * Call if match succeeds.  This way we don't allocate lots of ram
    207  * for structures we never use if the device isn't attached.
    208  */
    209 
    210 int
    211 pnpbus_scan(struct pnpbus_dev_attach_args *pna, PPC_DEVICE *dev)
    212 {
    213 	struct pnpresources *r = &pna->pna_res;
    214 	uint32_t l;
    215 	uint8_t *p, *q;
    216 	void *v;
    217 	int tag, size, item;
    218 
    219 	l = be32toh(dev->AllocatedOffset);
    220 	p = res->DevicePnPHeap + l;
    221 
    222 	if (p == NULL)
    223 		return -1;
    224 
    225 	for (; p[0] != END_TAG; p += size) {
    226 		tag = *p;
    227 		v = p;
    228 		if (tag_type(p[0]) == PNP_SMALL) {
    229 			size = tag_small_count(tag) + 1;
    230 			item = tag_small_item_name(tag);
    231 			switch (item) {
    232 			case IRQFormat:
    233 				pnp_newirq(v, r, size);
    234 				break;
    235 			case DMAFormat:
    236 				pnp_newdma(v, r, size);
    237 				break;
    238 			case IOPort:
    239 				pnp_newioport(v, r, size);
    240 				break;
    241 			}
    242 		} else {
    243 			struct _L4_Pack *pack = v;
    244 			struct _L4_PPCPack *pa = &pack->L4_Data.L4_PPCPack;
    245 
    246 			q = p;
    247 			size = (q[1] | (q[2] << 8)) + 3 /* tag + length */;
    248 			item = tag_large_item_name(tag);
    249 			printf("large vi item %d, %d\n", item, pa->Type);
    250 			if (item == LargeVendorItem &&
    251 			    pa->Type == LV_GenericAddress)
    252 				pnp_newaddr(v, r, size);
    253 		}
    254 	}
    255 
    256 	/* scan for compatid's */
    257 
    258 	l = be32toh(dev->CompatibleOffset);
    259 	p = res->DevicePnPHeap + l;
    260 
    261 	if (p == NULL)
    262 		return -1;
    263 
    264 	for (; p[0] != END_TAG; p += size) {
    265 		tag = *p;
    266 		v = p;
    267 		if (tag_type(p[0]) == PNP_SMALL) {
    268 			size = tag_small_count(tag) + 1;
    269 			item = tag_small_item_name(tag);
    270 			switch (item) {
    271 			case CompatibleDevice:
    272 				pnp_newcompatid(v, r, size);
    273 				break;
    274 			}
    275 		} else {
    276 			q = p;
    277 			size = (q[1] | (q[2] << 8)) + 3 /* tag + length */;
    278 		}
    279 	}
    280 	return 0;
    281 }
    282 
    283 /*
    284  * Setup the basic pna structure.
    285  */
    286 
    287 static void
    288 pnp_getpna(struct pnpbus_dev_attach_args *pna, struct pnpbus_attach_args *paa,
    289 	PPC_DEVICE *dev)
    290 {
    291 	uint32_t l;
    292 	DEVICE_ID *id = &dev->DeviceId;
    293 	struct pnpresources *r = &pna->pna_res;
    294 
    295 	l = be32toh(dev->AllocatedOffset);
    296 
    297 	pna->pna_iot = paa->paa_iot;
    298 	pna->pna_memt = paa->paa_memt;
    299 	pna->pna_ic = paa->paa_ic;
    300 	pnp_devid_to_string(id->DevId, pna->pna_devid);
    301 	pna->pna_ppc_dev = dev;
    302 	memset(r, 0, sizeof(*r));
    303 	SIMPLEQ_INIT(&r->mem);
    304 	SIMPLEQ_INIT(&r->io);
    305 	SIMPLEQ_INIT(&r->irq);
    306 	SIMPLEQ_INIT(&r->dma);
    307 }
    308 
    309 static int
    310 pnpbus_search(struct device *parent, struct cfdata *cf,
    311 	    const int *ldesc, void *aux)
    312 {
    313 	struct pnpbus_dev_attach_args pna;
    314 	struct pnpbus_attach_args *paa = aux;
    315 	PPC_DEVICE *ppc_dev;
    316 	int i;
    317 	uint32_t ndev;
    318 
    319 	ndev = be32toh(res->ActualNumDevices);
    320 	ppc_dev = res->Devices;
    321 
    322 	for (i = 0; i < ((ndev > MAX_DEVICES) ? MAX_DEVICES : ndev); i++) {
    323 		pnp_getpna(&pna, paa, &ppc_dev[i]);
    324 		if (config_match(parent, cf, &pna) > 0)
    325 			config_attach(parent, cf, &pna, pnpbus_print);
    326 	}
    327 
    328 	return 0;
    329 }
    330 
    331 static void
    332 pnpbus_printres(struct pnpresources *r)
    333 {
    334 	struct pnpbus_io *io;
    335 	struct pnpbus_mem *mem;
    336 	struct pnpbus_irq *irq;
    337 	struct pnpbus_dma *dma;
    338 	int p = 0;
    339 
    340 	if (!SIMPLEQ_EMPTY(&r->mem)) {
    341 		aprint_normal("mem");
    342 		SIMPLEQ_FOREACH(mem, &r->mem, next) {
    343 			aprint_normal(" 0x%x", mem->minbase);
    344 			if (mem->len > 1)
    345 				aprint_normal("-0x%x",
    346 				    mem->minbase + mem->len - 1);
    347 		}
    348 		p++;
    349 	}
    350 	if (!SIMPLEQ_EMPTY(&r->io)) {
    351 		if (p++)
    352 			aprint_normal(", ");
    353 		aprint_normal("port");
    354 		SIMPLEQ_FOREACH(io, &r->io, next) {
    355 			aprint_normal(" 0x%x", io->minbase);
    356 			if (io->len > 1)
    357 				aprint_normal("-0x%x",
    358 				    io->minbase + io->len - 1);
    359 		}
    360 	}
    361 	if (!SIMPLEQ_EMPTY(&r->irq)) {
    362 		if (p++)
    363 			aprint_normal(", ");
    364 		aprint_normal("irq");
    365 		SIMPLEQ_FOREACH(irq, &r->irq, next) {
    366 			aprint_normal(" %d", ffs(irq->mask) - 1);
    367 		}
    368 	}
    369 	if (!SIMPLEQ_EMPTY(&r->dma)) {
    370 		if (p++)
    371 			aprint_normal(", ");
    372 		aprint_normal("DMA");
    373 		SIMPLEQ_FOREACH(dma, &r->dma, next) {
    374 			aprint_normal(" %d", ffs(dma->mask) - 1);
    375 		}
    376 	}
    377 }
    378 
    379 static int
    380 pnpbus_print(void *args, const char *name)
    381 {
    382 	struct pnpbus_dev_attach_args *pna = args;
    383 
    384 	pnpbus_printres(&pna->pna_res);
    385 
    386 	return (UNCONF);
    387 }
    388 
    389 void
    390 pnpbus_print_devres(struct pnpbus_dev_attach_args *pna)
    391 {
    392 	aprint_normal(" ");
    393 	pnpbus_printres(&pna->pna_res);
    394 	aprint_normal("\n");
    395 }
    396 
    397 /*
    398  * Set up an interrupt handler to start being called.
    399  */
    400 void *
    401 pnpbus_intr_establish(int idx, int level, int (*ih_fun)(void *),
    402     void *ih_arg, struct pnpresources *r)
    403 {
    404 	struct pnpbus_irq *irq;
    405 	int irqnum, type;
    406 
    407 	if (idx >= r->numirq)
    408 		return 0;
    409 
    410 	irq = SIMPLEQ_FIRST(&r->irq);
    411 	while (idx--)
    412 		irq = SIMPLEQ_NEXT(irq, next);
    413 
    414 	irqnum = ffs(irq->mask) - 1;
    415 	type = (irq->flags & 0x0c) ? IST_LEVEL : IST_EDGE;
    416 
    417 	return (void *)intr_establish(irqnum, type, level, ih_fun, ih_arg);
    418 }
    419 
    420 /*
    421  * Deregister an interrupt handler.
    422  */
    423 void
    424 pnpbus_intr_disestablish(void *arg)
    425 {
    426 
    427 	intr_disestablish(arg);
    428 }
    429 
    430 int
    431 pnpbus_getirqnum(struct pnpresources *r, int idx, int *irqp, int *istp)
    432 {
    433 	struct pnpbus_irq *irq;
    434 
    435 	if (idx >= r->numirq)
    436 		return EINVAL;
    437 
    438 	irq = SIMPLEQ_FIRST(&r->irq);
    439 	while (idx--)
    440 		irq = SIMPLEQ_NEXT(irq, next);
    441 
    442 	if (irqp != NULL)
    443 		*irqp = ffs(irq->mask) - 1;
    444 	if (istp != NULL)
    445 		*istp = (irq->flags &0x0c) ? IST_LEVEL : IST_EDGE;
    446 	return 0;
    447 }
    448 
    449 int
    450 pnpbus_getdmachan(struct pnpresources *r, int idx, int *chanp)
    451 {
    452 	struct pnpbus_dma *dma;
    453 
    454 	if (idx >= r->numdma)
    455 		return EINVAL;
    456 
    457 	dma = SIMPLEQ_FIRST(&r->dma);
    458 	while (idx--)
    459 		dma = SIMPLEQ_NEXT(dma, next);
    460 
    461 	if (chanp != NULL)
    462 		*chanp = ffs(dma->mask) - 1;
    463 	return 0;
    464 }
    465 
    466 int
    467 pnpbus_getioport(struct pnpresources *r, int idx, int *basep, int *sizep)
    468 {
    469 	struct pnpbus_io *io;
    470 
    471 	if (idx >= r->numio)
    472 		return EINVAL;
    473 
    474 	io = SIMPLEQ_FIRST(&r->io);
    475 	while (idx--)
    476 		io = SIMPLEQ_NEXT(io, next);
    477 
    478 	if (basep)
    479 		*basep = io->minbase;
    480 	if (sizep)
    481 		*sizep = io->len;
    482 	return 0;
    483 }
    484 
    485 int
    486 pnpbus_io_map(struct pnpresources *r, int idx, bus_space_tag_t *tagp,
    487     bus_space_handle_t *hdlp)
    488 {
    489 	struct pnpbus_io *io;
    490 
    491 	if (idx >= r->numio)
    492 		return EINVAL;
    493 
    494 	io = SIMPLEQ_FIRST(&r->io);
    495 	while (idx--)
    496 		io = SIMPLEQ_NEXT(io, next);
    497 
    498 	*tagp = &prep_isa_io_space_tag;
    499 	return (bus_space_map(&prep_isa_io_space_tag, io->minbase, io->len,
    500 	    0, hdlp));
    501 }
    502 
    503 void
    504 pnpbus_io_unmap(struct pnpresources *r, int idx, bus_space_tag_t tag,
    505     bus_space_handle_t hdl)
    506 {
    507 	struct pnpbus_io *io;
    508 
    509 	if (idx >= r->numio)
    510 		return;
    511 
    512 	io = SIMPLEQ_FIRST(&r->io);
    513 	while (idx--)
    514 		io = SIMPLEQ_NEXT(io, next);
    515 
    516 	bus_space_unmap(tag, hdl, io->len);
    517 }
    518