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