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