Home | History | Annotate | Line # | Download | only in pnpbus
pnpbus.c revision 1.8
      1  1.8  garbled /*	$NetBSD: pnpbus.c,v 1.8 2008/04/28 19:01:45 garbled 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.8  garbled __KERNEL_RCSID(0, "$NetBSD: pnpbus.c,v 1.8 2008/04/28 19:01:45 garbled 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.5  garbled #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.6  garbled #include "isadma.h"
     62  1.6  garbled 
     63  1.1  garbled static int	pnpbus_match(struct device *, struct cfdata *, void *);
     64  1.1  garbled static void	pnpbus_attach(struct device *, struct device *, void *);
     65  1.1  garbled static int	pnpbus_print(void *, const char *);
     66  1.1  garbled static int	pnpbus_search(struct device *, struct cfdata *,
     67  1.1  garbled 			      const int *, void *);
     68  1.1  garbled 
     69  1.1  garbled CFATTACH_DECL(pnpbus, sizeof(struct pnpbus_softc),
     70  1.1  garbled     pnpbus_match, pnpbus_attach, NULL, NULL);
     71  1.1  garbled 
     72  1.1  garbled struct pnpbus_softc *pnpbus_softc;
     73  1.1  garbled extern struct cfdriver pnpbus_cd;
     74  1.1  garbled 
     75  1.1  garbled static int
     76  1.1  garbled pnpbus_match(struct device *parent, struct cfdata *cf, void *aux)
     77  1.1  garbled {
     78  1.8  garbled 	struct pnpbus_attach_args *paa = aux;
     79  1.8  garbled 
     80  1.8  garbled 	if (paa->paa_name != NULL && strcmp(paa->paa_name, "pnpbus") == 0)
     81  1.8  garbled 		return 1;
     82  1.8  garbled 	return 0;
     83  1.1  garbled }
     84  1.1  garbled 
     85  1.1  garbled static void
     86  1.1  garbled pnpbus_attach(struct device *parent, struct device *self, void *aux)
     87  1.1  garbled {
     88  1.1  garbled 	struct pnpbus_softc *sc = (struct pnpbus_softc *)self;
     89  1.1  garbled 	struct pnpbus_attach_args *paa = aux;
     90  1.1  garbled 
     91  1.7  garbled 	aprint_normal("\n");
     92  1.1  garbled 
     93  1.1  garbled 	pnpbus_softc = sc;
     94  1.1  garbled 	sc->sc_ic = paa->paa_ic;
     95  1.1  garbled 	sc->sc_iot = paa->paa_iot;
     96  1.1  garbled 	sc->sc_memt = paa->paa_memt;
     97  1.6  garbled 	sc->sc_dmat = paa->paa_dmat;
     98  1.6  garbled 
     99  1.6  garbled #if NISADMA > 0
    100  1.6  garbled 	isa_dmainit(sc->sc_ic, sc->sc_iot, sc->sc_dmat, self);
    101  1.6  garbled #endif
    102  1.1  garbled 
    103  1.1  garbled 	(void)config_search_ia(pnpbus_search, self, "pnpbus", aux);
    104  1.1  garbled }
    105  1.1  garbled 
    106  1.1  garbled static int
    107  1.1  garbled pnp_newirq(void *v, struct pnpresources *r, int size)
    108  1.1  garbled {
    109  1.1  garbled 	struct _S4_Pack *p = v;
    110  1.1  garbled 	struct pnpbus_irq *irq;
    111  1.1  garbled 
    112  1.1  garbled 	irq = malloc(sizeof(struct pnpbus_irq), M_DEVBUF, M_NOWAIT);
    113  1.1  garbled 
    114  1.1  garbled 	irq->mask = le16dec(&p->IRQMask[0]);
    115  1.2  garbled 
    116  1.1  garbled 	if (size > 2)
    117  1.1  garbled 		irq->flags = p->IRQInfo;
    118  1.1  garbled 	else
    119  1.3  garbled 		irq->flags = 0x1;
    120  1.1  garbled 
    121  1.1  garbled 	SIMPLEQ_INSERT_TAIL(&r->irq, irq, next);
    122  1.1  garbled 	r->numirq++;
    123  1.1  garbled 
    124  1.1  garbled 	return 0;
    125  1.1  garbled }
    126  1.1  garbled 
    127  1.1  garbled static int
    128  1.1  garbled pnp_newdma(void *v, struct pnpresources *r, int size)
    129  1.1  garbled {
    130  1.1  garbled 	struct _S5_Pack *p = v;
    131  1.1  garbled 	struct pnpbus_dma *dma;
    132  1.1  garbled 
    133  1.1  garbled 	dma = malloc(sizeof(struct pnpbus_dma), M_DEVBUF, M_NOWAIT);
    134  1.1  garbled 
    135  1.1  garbled 	dma->mask = le16dec(&p->DMAMask);
    136  1.1  garbled 	if (size > 2)
    137  1.1  garbled 		dma->flags = p->DMAInfo;
    138  1.1  garbled 	else
    139  1.1  garbled 		dma->flags = 0x01;
    140  1.1  garbled 
    141  1.1  garbled 	SIMPLEQ_INSERT_TAIL(&r->dma, dma, next);
    142  1.1  garbled 	r->numdma++;
    143  1.1  garbled 
    144  1.1  garbled 	return 0;
    145  1.1  garbled }
    146  1.1  garbled 
    147  1.1  garbled static int
    148  1.1  garbled pnp_newioport(void *v, struct pnpresources *r, int size)
    149  1.1  garbled {
    150  1.1  garbled 	struct _S8_Pack *p = v;
    151  1.1  garbled 	struct pnpbus_io *io;
    152  1.1  garbled 	uint16_t mask;
    153  1.1  garbled 
    154  1.1  garbled 	io = malloc(sizeof(struct pnpbus_io), M_DEVBUF, M_NOWAIT);
    155  1.1  garbled 	mask = p->IOInfo & ISAAddr16bit ? 0xffff : 0x03ff;
    156  1.1  garbled 	io->minbase = (p->RangeMin[0] | (p->RangeMin[1] << 8)) & mask;
    157  1.1  garbled 	io->maxbase = (p->RangeMax[0] | (p->RangeMax[1] << 8)) & mask;
    158  1.1  garbled 	io->align = p->IOAlign;
    159  1.1  garbled 	io->len = p->IONum;
    160  1.1  garbled 	io->flags = p->IOInfo;
    161  1.1  garbled 
    162  1.1  garbled 	SIMPLEQ_INSERT_TAIL(&r->io, io, next);
    163  1.1  garbled 	r->numio++;
    164  1.1  garbled 
    165  1.1  garbled 	return 0;
    166  1.1  garbled }
    167  1.1  garbled 
    168  1.1  garbled static int
    169  1.2  garbled pnp_newfixedioport(void *v, struct pnpresources *r, int size)
    170  1.2  garbled {
    171  1.2  garbled 	struct _S9_Pack *p = v;
    172  1.2  garbled 	struct pnpbus_io *io;
    173  1.2  garbled 
    174  1.2  garbled 	io = malloc(sizeof(struct pnpbus_io), M_DEVBUF, M_NOWAIT);
    175  1.2  garbled 	io->minbase = (p->Range[0] | (p->Range[1] << 8)) & 0x3ff;
    176  1.2  garbled 	io->len = p->IONum;
    177  1.2  garbled 	io->maxbase = -1;
    178  1.2  garbled 	io->flags = 0;
    179  1.2  garbled 	io->align = 1;
    180  1.2  garbled 
    181  1.2  garbled 	SIMPLEQ_INSERT_TAIL(&r->io, io, next);
    182  1.2  garbled 	r->numio++;
    183  1.2  garbled 
    184  1.2  garbled 	return 0;
    185  1.2  garbled }
    186  1.2  garbled 
    187  1.2  garbled static int
    188  1.5  garbled pnp_newiomem(void *v, struct pnpresources *r, int size)
    189  1.5  garbled {
    190  1.5  garbled 	struct pnpbus_mem *mem;
    191  1.5  garbled 	struct _L1_Pack *pack = v;
    192  1.5  garbled 
    193  1.5  garbled 	if (pack->Count0 >= 0x9) {
    194  1.5  garbled 		mem = malloc(sizeof(struct pnpbus_mem), M_DEVBUF, M_NOWAIT);
    195  1.5  garbled 		mem->minbase = (pack->Data[2] << 16) | (pack->Data[1] << 8);
    196  1.5  garbled 		mem->maxbase = (pack->Data[4] << 16) | (pack->Data[3] << 8);
    197  1.5  garbled 		mem->align = (pack->Data[6] << 8) | pack->Data[5];
    198  1.5  garbled 		mem->len = (pack->Data[8] << 16) | (pack->Data[7] << 8);
    199  1.5  garbled 		mem->flags = pack->Data[0];
    200  1.5  garbled 		SIMPLEQ_INSERT_TAIL(&r->iomem, mem, next);
    201  1.5  garbled 		r->numiomem++;
    202  1.5  garbled 		return 0;
    203  1.5  garbled 	}
    204  1.5  garbled 	return -1;
    205  1.5  garbled }
    206  1.5  garbled 
    207  1.5  garbled static int
    208  1.1  garbled pnp_newaddr(void *v, struct pnpresources *r, int size)
    209  1.1  garbled {
    210  1.1  garbled 	struct pnpbus_io *io;
    211  1.1  garbled 	struct pnpbus_mem *mem;
    212  1.1  garbled 	struct _L4_Pack *pack = v;
    213  1.1  garbled 	struct _L4_PPCPack *p =  &pack->L4_Data.L4_PPCPack;
    214  1.1  garbled 
    215  1.1  garbled 	if (p->PPCData[0] == 1) {/* type IO */
    216  1.1  garbled 		io = malloc(sizeof(struct pnpbus_io), M_DEVBUF, M_NOWAIT);
    217  1.1  garbled 		io->minbase = (uint16_t)le64dec(&p->PPCData[4]);
    218  1.1  garbled 		io->maxbase = -1;
    219  1.1  garbled 		io->align = p->PPCData[1];
    220  1.1  garbled 		io->len = (uint16_t)le64dec(&p->PPCData[12]);
    221  1.1  garbled 		io->flags = 0;
    222  1.1  garbled 		SIMPLEQ_INSERT_TAIL(&r->io, io, next);
    223  1.1  garbled 		r->numio++;
    224  1.1  garbled 
    225  1.1  garbled 		return 0;
    226  1.1  garbled 	} else if (p->PPCData[0] == 2) {
    227  1.1  garbled 		mem = malloc(sizeof(struct pnpbus_mem), M_DEVBUF, M_NOWAIT);
    228  1.1  garbled 		mem->minbase = (uint32_t)le64dec(&p->PPCData[4]);
    229  1.1  garbled 		mem->maxbase = -1;
    230  1.1  garbled 		mem->align = p->PPCData[1];
    231  1.1  garbled 		mem->len = (uint32_t)le64dec(&p->PPCData[12]);
    232  1.1  garbled 		mem->flags = 0;
    233  1.1  garbled 		SIMPLEQ_INSERT_TAIL(&r->mem, mem, next);
    234  1.1  garbled 		r->nummem++;
    235  1.1  garbled 
    236  1.1  garbled 		return 0;
    237  1.1  garbled 	} else
    238  1.1  garbled 		return -1;
    239  1.1  garbled }
    240  1.1  garbled 
    241  1.1  garbled static int
    242  1.1  garbled pnp_newcompatid(void *v, struct pnpresources *r, int size)
    243  1.1  garbled {
    244  1.1  garbled 	struct _S3_Pack *p = v;
    245  1.1  garbled 	struct pnpbus_compatid *id;
    246  1.1  garbled 	uint32_t cid;
    247  1.1  garbled 
    248  1.1  garbled 	id = malloc(sizeof(*id), M_DEVBUF, M_NOWAIT);
    249  1.1  garbled 	cid = le32dec(p->CompatId);
    250  1.1  garbled 	pnp_devid_to_string(cid, id->idstr);
    251  1.1  garbled 	id->next = r->compatids;
    252  1.1  garbled 	r->compatids = id;
    253  1.1  garbled 
    254  1.1  garbled 	return 0;
    255  1.1  garbled }
    256  1.1  garbled 
    257  1.1  garbled /*
    258  1.1  garbled  * Call if match succeeds.  This way we don't allocate lots of ram
    259  1.1  garbled  * for structures we never use if the device isn't attached.
    260  1.1  garbled  */
    261  1.1  garbled 
    262  1.1  garbled int
    263  1.1  garbled pnpbus_scan(struct pnpbus_dev_attach_args *pna, PPC_DEVICE *dev)
    264  1.1  garbled {
    265  1.1  garbled 	struct pnpresources *r = &pna->pna_res;
    266  1.1  garbled 	uint32_t l;
    267  1.1  garbled 	uint8_t *p, *q;
    268  1.1  garbled 	void *v;
    269  1.1  garbled 	int tag, size, item;
    270  1.1  garbled 
    271  1.1  garbled 	l = be32toh(dev->AllocatedOffset);
    272  1.1  garbled 	p = res->DevicePnPHeap + l;
    273  1.1  garbled 
    274  1.1  garbled 	if (p == NULL)
    275  1.1  garbled 		return -1;
    276  1.1  garbled 
    277  1.1  garbled 	for (; p[0] != END_TAG; p += size) {
    278  1.1  garbled 		tag = *p;
    279  1.1  garbled 		v = p;
    280  1.1  garbled 		if (tag_type(p[0]) == PNP_SMALL) {
    281  1.1  garbled 			size = tag_small_count(tag) + 1;
    282  1.1  garbled 			item = tag_small_item_name(tag);
    283  1.1  garbled 			switch (item) {
    284  1.1  garbled 			case IRQFormat:
    285  1.1  garbled 				pnp_newirq(v, r, size);
    286  1.1  garbled 				break;
    287  1.1  garbled 			case DMAFormat:
    288  1.1  garbled 				pnp_newdma(v, r, size);
    289  1.1  garbled 				break;
    290  1.1  garbled 			case IOPort:
    291  1.1  garbled 				pnp_newioport(v, r, size);
    292  1.1  garbled 				break;
    293  1.2  garbled 			case FixedIOPort:
    294  1.2  garbled 				pnp_newfixedioport(v, r, size);
    295  1.2  garbled 				break;
    296  1.1  garbled 			}
    297  1.1  garbled 		} else {
    298  1.1  garbled 			struct _L4_Pack *pack = v;
    299  1.1  garbled 			struct _L4_PPCPack *pa = &pack->L4_Data.L4_PPCPack;
    300  1.1  garbled 
    301  1.1  garbled 			q = p;
    302  1.1  garbled 			size = (q[1] | (q[2] << 8)) + 3 /* tag + length */;
    303  1.1  garbled 			item = tag_large_item_name(tag);
    304  1.1  garbled 			if (item == LargeVendorItem &&
    305  1.1  garbled 			    pa->Type == LV_GenericAddress)
    306  1.1  garbled 				pnp_newaddr(v, r, size);
    307  1.5  garbled 			else if (item == MemoryRange)
    308  1.5  garbled 				pnp_newiomem(v, r, size);
    309  1.1  garbled 		}
    310  1.1  garbled 	}
    311  1.1  garbled 
    312  1.1  garbled 	/* scan for compatid's */
    313  1.1  garbled 
    314  1.1  garbled 	l = be32toh(dev->CompatibleOffset);
    315  1.1  garbled 	p = res->DevicePnPHeap + l;
    316  1.1  garbled 
    317  1.1  garbled 	if (p == NULL)
    318  1.1  garbled 		return -1;
    319  1.1  garbled 
    320  1.1  garbled 	for (; p[0] != END_TAG; p += size) {
    321  1.1  garbled 		tag = *p;
    322  1.1  garbled 		v = p;
    323  1.1  garbled 		if (tag_type(p[0]) == PNP_SMALL) {
    324  1.1  garbled 			size = tag_small_count(tag) + 1;
    325  1.1  garbled 			item = tag_small_item_name(tag);
    326  1.1  garbled 			switch (item) {
    327  1.1  garbled 			case CompatibleDevice:
    328  1.1  garbled 				pnp_newcompatid(v, r, size);
    329  1.1  garbled 				break;
    330  1.1  garbled 			}
    331  1.1  garbled 		} else {
    332  1.1  garbled 			q = p;
    333  1.1  garbled 			size = (q[1] | (q[2] << 8)) + 3 /* tag + length */;
    334  1.1  garbled 		}
    335  1.1  garbled 	}
    336  1.1  garbled 	return 0;
    337  1.1  garbled }
    338  1.1  garbled 
    339  1.1  garbled /*
    340  1.1  garbled  * Setup the basic pna structure.
    341  1.1  garbled  */
    342  1.1  garbled 
    343  1.1  garbled static void
    344  1.1  garbled pnp_getpna(struct pnpbus_dev_attach_args *pna, struct pnpbus_attach_args *paa,
    345  1.1  garbled 	PPC_DEVICE *dev)
    346  1.1  garbled {
    347  1.1  garbled 	DEVICE_ID *id = &dev->DeviceId;
    348  1.1  garbled 	struct pnpresources *r = &pna->pna_res;
    349  1.5  garbled 	ChipIDPack *pack;
    350  1.5  garbled 	uint32_t l;
    351  1.5  garbled 	uint8_t *p;
    352  1.5  garbled 	void *v;
    353  1.5  garbled 	int tag, size, item;
    354  1.1  garbled 
    355  1.1  garbled 	l = be32toh(dev->AllocatedOffset);
    356  1.5  garbled 	p = res->DevicePnPHeap + l;
    357  1.1  garbled 
    358  1.1  garbled 	pna->pna_iot = paa->paa_iot;
    359  1.1  garbled 	pna->pna_memt = paa->paa_memt;
    360  1.1  garbled 	pna->pna_ic = paa->paa_ic;
    361  1.6  garbled 	pna->pna_dmat = paa->paa_dmat;
    362  1.1  garbled 	pnp_devid_to_string(id->DevId, pna->pna_devid);
    363  1.4  garbled 	pna->basetype = id->BaseType;
    364  1.4  garbled 	pna->subtype = id->SubType;
    365  1.4  garbled 	pna->interface = id->Interface;
    366  1.1  garbled 	pna->pna_ppc_dev = dev;
    367  1.1  garbled 	memset(r, 0, sizeof(*r));
    368  1.1  garbled 	SIMPLEQ_INIT(&r->mem);
    369  1.1  garbled 	SIMPLEQ_INIT(&r->io);
    370  1.1  garbled 	SIMPLEQ_INIT(&r->irq);
    371  1.1  garbled 	SIMPLEQ_INIT(&r->dma);
    372  1.5  garbled 	SIMPLEQ_INIT(&r->iomem);
    373  1.5  garbled 	if (p == NULL)
    374  1.5  garbled 		return;
    375  1.5  garbled 	/* otherwise, we start looking for chipid's */
    376  1.5  garbled 	for (; p[0] != END_TAG; p += size) {
    377  1.5  garbled 		tag = *p;
    378  1.5  garbled 		v = p;
    379  1.5  garbled 		if (tag_type(p[0]) == PNP_SMALL) {
    380  1.5  garbled 			size = tag_small_count(tag) + 1;
    381  1.5  garbled 			item = tag_small_item_name(tag);
    382  1.5  garbled 			if (item != SmallVendorItem || p[1] != 1)
    383  1.5  garbled 				continue;
    384  1.5  garbled 			pack = v;
    385  1.5  garbled 			pna->chipid = le16dec(&pack->Name[0]);
    386  1.5  garbled 			pna->chipmfg0 = pack->VendorID0;
    387  1.5  garbled 			pna->chipmfg1 = pack->VendorID1;
    388  1.5  garbled 			break;
    389  1.5  garbled 		} else {
    390  1.5  garbled 			/* Large */
    391  1.5  garbled 			size = (p[1] | (p[2] << 8)) + 3 /* tag + length */;
    392  1.5  garbled 		}
    393  1.5  garbled 	}
    394  1.1  garbled }
    395  1.1  garbled 
    396  1.1  garbled static int
    397  1.1  garbled pnpbus_search(struct device *parent, struct cfdata *cf,
    398  1.1  garbled 	    const int *ldesc, void *aux)
    399  1.1  garbled {
    400  1.1  garbled 	struct pnpbus_dev_attach_args pna;
    401  1.1  garbled 	struct pnpbus_attach_args *paa = aux;
    402  1.1  garbled 	PPC_DEVICE *ppc_dev;
    403  1.1  garbled 	int i;
    404  1.1  garbled 	uint32_t ndev;
    405  1.1  garbled 
    406  1.1  garbled 	ndev = be32toh(res->ActualNumDevices);
    407  1.1  garbled 	ppc_dev = res->Devices;
    408  1.1  garbled 
    409  1.1  garbled 	for (i = 0; i < ((ndev > MAX_DEVICES) ? MAX_DEVICES : ndev); i++) {
    410  1.1  garbled 		pnp_getpna(&pna, paa, &ppc_dev[i]);
    411  1.1  garbled 		if (config_match(parent, cf, &pna) > 0)
    412  1.1  garbled 			config_attach(parent, cf, &pna, pnpbus_print);
    413  1.1  garbled 	}
    414  1.1  garbled 
    415  1.1  garbled 	return 0;
    416  1.1  garbled }
    417  1.1  garbled 
    418  1.1  garbled static void
    419  1.1  garbled pnpbus_printres(struct pnpresources *r)
    420  1.1  garbled {
    421  1.1  garbled 	struct pnpbus_io *io;
    422  1.1  garbled 	struct pnpbus_mem *mem;
    423  1.1  garbled 	struct pnpbus_irq *irq;
    424  1.1  garbled 	struct pnpbus_dma *dma;
    425  1.1  garbled 	int p = 0;
    426  1.1  garbled 
    427  1.1  garbled 	if (!SIMPLEQ_EMPTY(&r->mem)) {
    428  1.1  garbled 		aprint_normal("mem");
    429  1.1  garbled 		SIMPLEQ_FOREACH(mem, &r->mem, next) {
    430  1.1  garbled 			aprint_normal(" 0x%x", mem->minbase);
    431  1.1  garbled 			if (mem->len > 1)
    432  1.1  garbled 				aprint_normal("-0x%x",
    433  1.1  garbled 				    mem->minbase + mem->len - 1);
    434  1.1  garbled 		}
    435  1.1  garbled 		p++;
    436  1.1  garbled 	}
    437  1.1  garbled 	if (!SIMPLEQ_EMPTY(&r->io)) {
    438  1.1  garbled 		if (p++)
    439  1.1  garbled 			aprint_normal(", ");
    440  1.1  garbled 		aprint_normal("port");
    441  1.1  garbled 		SIMPLEQ_FOREACH(io, &r->io, next) {
    442  1.1  garbled 			aprint_normal(" 0x%x", io->minbase);
    443  1.1  garbled 			if (io->len > 1)
    444  1.1  garbled 				aprint_normal("-0x%x",
    445  1.1  garbled 				    io->minbase + io->len - 1);
    446  1.1  garbled 		}
    447  1.1  garbled 	}
    448  1.5  garbled 	if (!SIMPLEQ_EMPTY(&r->iomem)) {
    449  1.5  garbled 		if (p++)
    450  1.5  garbled 			aprint_normal(", ");
    451  1.5  garbled 		aprint_normal("iomem");
    452  1.5  garbled 		SIMPLEQ_FOREACH(mem, &r->iomem, next) {
    453  1.5  garbled 			aprint_normal(" 0x%x", mem->minbase);
    454  1.5  garbled 			if (mem->len > 1)
    455  1.5  garbled 				aprint_normal("-0x%x",
    456  1.5  garbled 				    mem->minbase + mem->len - 1);
    457  1.5  garbled 		}
    458  1.5  garbled 		p++;
    459  1.5  garbled 	}
    460  1.1  garbled 	if (!SIMPLEQ_EMPTY(&r->irq)) {
    461  1.1  garbled 		if (p++)
    462  1.1  garbled 			aprint_normal(", ");
    463  1.1  garbled 		aprint_normal("irq");
    464  1.1  garbled 		SIMPLEQ_FOREACH(irq, &r->irq, next) {
    465  1.1  garbled 			aprint_normal(" %d", ffs(irq->mask) - 1);
    466  1.1  garbled 		}
    467  1.1  garbled 	}
    468  1.1  garbled 	if (!SIMPLEQ_EMPTY(&r->dma)) {
    469  1.1  garbled 		if (p++)
    470  1.1  garbled 			aprint_normal(", ");
    471  1.1  garbled 		aprint_normal("DMA");
    472  1.1  garbled 		SIMPLEQ_FOREACH(dma, &r->dma, next) {
    473  1.1  garbled 			aprint_normal(" %d", ffs(dma->mask) - 1);
    474  1.1  garbled 		}
    475  1.1  garbled 	}
    476  1.1  garbled }
    477  1.1  garbled 
    478  1.2  garbled void
    479  1.2  garbled pnpbus_print_devres(struct pnpbus_dev_attach_args *pna)
    480  1.2  garbled {
    481  1.2  garbled 	aprint_normal(": ");
    482  1.2  garbled 	pnpbus_printres(&pna->pna_res);
    483  1.2  garbled }
    484  1.2  garbled 
    485  1.1  garbled static int
    486  1.1  garbled pnpbus_print(void *args, const char *name)
    487  1.1  garbled {
    488  1.1  garbled 	struct pnpbus_dev_attach_args *pna = args;
    489  1.1  garbled 
    490  1.2  garbled 	pnpbus_print_devres(pna);
    491  1.1  garbled 	return (UNCONF);
    492  1.1  garbled }
    493  1.1  garbled 
    494  1.1  garbled /*
    495  1.1  garbled  * Set up an interrupt handler to start being called.
    496  1.1  garbled  */
    497  1.1  garbled void *
    498  1.8  garbled pnpbus_intr_establish(int idx, int level, int tover, int (*ih_fun)(void *),
    499  1.1  garbled     void *ih_arg, struct pnpresources *r)
    500  1.1  garbled {
    501  1.1  garbled 	struct pnpbus_irq *irq;
    502  1.1  garbled 	int irqnum, type;
    503  1.1  garbled 
    504  1.1  garbled 	if (idx >= r->numirq)
    505  1.1  garbled 		return 0;
    506  1.1  garbled 
    507  1.1  garbled 	irq = SIMPLEQ_FIRST(&r->irq);
    508  1.1  garbled 	while (idx--)
    509  1.1  garbled 		irq = SIMPLEQ_NEXT(irq, next);
    510  1.1  garbled 
    511  1.1  garbled 	irqnum = ffs(irq->mask) - 1;
    512  1.1  garbled 	type = (irq->flags & 0x0c) ? IST_LEVEL : IST_EDGE;
    513  1.8  garbled 	if (tover != IST_PNP)
    514  1.8  garbled 		type = tover;
    515  1.1  garbled 
    516  1.1  garbled 	return (void *)intr_establish(irqnum, type, level, ih_fun, ih_arg);
    517  1.1  garbled }
    518  1.1  garbled 
    519  1.1  garbled /*
    520  1.1  garbled  * Deregister an interrupt handler.
    521  1.1  garbled  */
    522  1.1  garbled void
    523  1.1  garbled pnpbus_intr_disestablish(void *arg)
    524  1.1  garbled {
    525  1.1  garbled 
    526  1.1  garbled 	intr_disestablish(arg);
    527  1.1  garbled }
    528  1.1  garbled 
    529  1.1  garbled int
    530  1.1  garbled pnpbus_getirqnum(struct pnpresources *r, int idx, int *irqp, int *istp)
    531  1.1  garbled {
    532  1.1  garbled 	struct pnpbus_irq *irq;
    533  1.1  garbled 
    534  1.1  garbled 	if (idx >= r->numirq)
    535  1.1  garbled 		return EINVAL;
    536  1.1  garbled 
    537  1.1  garbled 	irq = SIMPLEQ_FIRST(&r->irq);
    538  1.1  garbled 	while (idx--)
    539  1.1  garbled 		irq = SIMPLEQ_NEXT(irq, next);
    540  1.1  garbled 
    541  1.1  garbled 	if (irqp != NULL)
    542  1.1  garbled 		*irqp = ffs(irq->mask) - 1;
    543  1.1  garbled 	if (istp != NULL)
    544  1.1  garbled 		*istp = (irq->flags &0x0c) ? IST_LEVEL : IST_EDGE;
    545  1.1  garbled 	return 0;
    546  1.1  garbled }
    547  1.1  garbled 
    548  1.1  garbled int
    549  1.1  garbled pnpbus_getdmachan(struct pnpresources *r, int idx, int *chanp)
    550  1.1  garbled {
    551  1.1  garbled 	struct pnpbus_dma *dma;
    552  1.1  garbled 
    553  1.1  garbled 	if (idx >= r->numdma)
    554  1.1  garbled 		return EINVAL;
    555  1.1  garbled 
    556  1.1  garbled 	dma = SIMPLEQ_FIRST(&r->dma);
    557  1.1  garbled 	while (idx--)
    558  1.1  garbled 		dma = SIMPLEQ_NEXT(dma, next);
    559  1.1  garbled 
    560  1.1  garbled 	if (chanp != NULL)
    561  1.1  garbled 		*chanp = ffs(dma->mask) - 1;
    562  1.1  garbled 	return 0;
    563  1.1  garbled }
    564  1.1  garbled 
    565  1.1  garbled int
    566  1.1  garbled pnpbus_getioport(struct pnpresources *r, int idx, int *basep, int *sizep)
    567  1.1  garbled {
    568  1.1  garbled 	struct pnpbus_io *io;
    569  1.1  garbled 
    570  1.1  garbled 	if (idx >= r->numio)
    571  1.1  garbled 		return EINVAL;
    572  1.1  garbled 
    573  1.1  garbled 	io = SIMPLEQ_FIRST(&r->io);
    574  1.1  garbled 	while (idx--)
    575  1.1  garbled 		io = SIMPLEQ_NEXT(io, next);
    576  1.1  garbled 
    577  1.1  garbled 	if (basep)
    578  1.1  garbled 		*basep = io->minbase;
    579  1.1  garbled 	if (sizep)
    580  1.1  garbled 		*sizep = io->len;
    581  1.1  garbled 	return 0;
    582  1.1  garbled }
    583  1.1  garbled 
    584  1.1  garbled int
    585  1.1  garbled pnpbus_io_map(struct pnpresources *r, int idx, bus_space_tag_t *tagp,
    586  1.1  garbled     bus_space_handle_t *hdlp)
    587  1.1  garbled {
    588  1.1  garbled 	struct pnpbus_io *io;
    589  1.1  garbled 
    590  1.1  garbled 	if (idx >= r->numio)
    591  1.1  garbled 		return EINVAL;
    592  1.1  garbled 
    593  1.1  garbled 	io = SIMPLEQ_FIRST(&r->io);
    594  1.1  garbled 	while (idx--)
    595  1.1  garbled 		io = SIMPLEQ_NEXT(io, next);
    596  1.1  garbled 
    597  1.7  garbled 	*tagp = &genppc_isa_io_space_tag;
    598  1.7  garbled 	return (bus_space_map(&genppc_isa_io_space_tag, io->minbase, io->len,
    599  1.1  garbled 	    0, hdlp));
    600  1.1  garbled }
    601  1.1  garbled 
    602  1.1  garbled void
    603  1.1  garbled pnpbus_io_unmap(struct pnpresources *r, int idx, bus_space_tag_t tag,
    604  1.1  garbled     bus_space_handle_t hdl)
    605  1.1  garbled {
    606  1.1  garbled 	struct pnpbus_io *io;
    607  1.1  garbled 
    608  1.1  garbled 	if (idx >= r->numio)
    609  1.1  garbled 		return;
    610  1.1  garbled 
    611  1.1  garbled 	io = SIMPLEQ_FIRST(&r->io);
    612  1.1  garbled 	while (idx--)
    613  1.1  garbled 		io = SIMPLEQ_NEXT(io, next);
    614  1.1  garbled 
    615  1.1  garbled 	bus_space_unmap(tag, hdl, io->len);
    616  1.1  garbled }
    617  1.5  garbled 
    618  1.5  garbled int
    619  1.5  garbled pnpbus_getiomem(struct pnpresources *r, int idx, int *basep, int *sizep)
    620  1.5  garbled {
    621  1.5  garbled 	struct pnpbus_mem *mem;
    622  1.5  garbled 
    623  1.5  garbled 	if (idx >= r->numiomem)
    624  1.5  garbled 		return EINVAL;
    625  1.5  garbled 
    626  1.5  garbled 	mem = SIMPLEQ_FIRST(&r->iomem);
    627  1.5  garbled 	while (idx--)
    628  1.5  garbled 		mem = SIMPLEQ_NEXT(mem, next);
    629  1.5  garbled 
    630  1.5  garbled 	if (basep)
    631  1.5  garbled 		*basep = mem->minbase;
    632  1.5  garbled 	if (sizep)
    633  1.5  garbled 		*sizep = mem->len;
    634  1.5  garbled 	return 0;
    635  1.5  garbled }
    636  1.5  garbled 
    637  1.5  garbled int
    638  1.5  garbled pnpbus_iomem_map(struct pnpresources *r, int idx, bus_space_tag_t *tagp,
    639  1.5  garbled     bus_space_handle_t *hdlp)
    640  1.5  garbled {
    641  1.5  garbled 	struct pnpbus_mem *mem;
    642  1.5  garbled 
    643  1.5  garbled 	if (idx >= r->numiomem)
    644  1.5  garbled 		return EINVAL;
    645  1.5  garbled 
    646  1.5  garbled 	mem = SIMPLEQ_FIRST(&r->iomem);
    647  1.5  garbled 	while (idx--)
    648  1.5  garbled 		mem = SIMPLEQ_NEXT(mem, next);
    649  1.5  garbled 
    650  1.7  garbled 	*tagp = &genppc_isa_mem_space_tag;
    651  1.7  garbled 	return (bus_space_map(&genppc_isa_mem_space_tag, mem->minbase, mem->len,
    652  1.5  garbled 	    0, hdlp));
    653  1.5  garbled }
    654  1.5  garbled 
    655  1.5  garbled void
    656  1.5  garbled pnpbus_iomem_unmap(struct pnpresources *r, int idx, bus_space_tag_t tag,
    657  1.5  garbled     bus_space_handle_t hdl)
    658  1.5  garbled {
    659  1.5  garbled 	struct pnpbus_mem *mem;
    660  1.5  garbled 
    661  1.5  garbled 	if (idx >= r->numiomem)
    662  1.5  garbled 		return;
    663  1.5  garbled 
    664  1.5  garbled 	mem = SIMPLEQ_FIRST(&r->mem);
    665  1.5  garbled 	while (idx--)
    666  1.5  garbled 		mem = SIMPLEQ_NEXT(mem, next);
    667  1.5  garbled 
    668  1.5  garbled 	bus_space_unmap(tag, hdl, mem->len);
    669  1.5  garbled }
    670