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