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