isapnp.c revision 1.32 1 /* $NetBSD: isapnp.c,v 1.32 1999/03/22 07:40:57 mycroft Exp $ */
2
3 /*-
4 * Copyright (c) 1996 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Christos Zoulas.
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 /*
40 * ISA PnP bus autoconfiguration.
41 */
42
43 #include "isadma.h"
44
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/device.h>
48 #include <sys/malloc.h>
49
50 #include <machine/bus.h>
51
52 #include <dev/isa/isavar.h>
53
54 #include <dev/isapnp/isapnpreg.h>
55 #include <dev/isapnp/isapnpvar.h>
56 #include <dev/isapnp/isapnpdevs.h>
57
58 #include "wss_isapnp.h" /* XXX part of disgusting CS chip hack */
59
60 static void isapnp_init __P((struct isapnp_softc *));
61 static __inline u_char isapnp_shift_bit __P((struct isapnp_softc *));
62 static int isapnp_findcard __P((struct isapnp_softc *));
63 static void isapnp_free_region __P((bus_space_tag_t, struct isapnp_region *));
64 static int isapnp_alloc_region __P((bus_space_tag_t, struct isapnp_region *));
65 static int isapnp_alloc_irq __P((isa_chipset_tag_t, struct isapnp_pin *));
66 static int isapnp_alloc_drq __P((isa_chipset_tag_t, struct isapnp_pin *));
67 static int isapnp_testconfig __P((bus_space_tag_t, bus_space_tag_t,
68 struct isapnp_attach_args *, int));
69 static struct isapnp_attach_args *isapnp_bestconfig __P((struct isapnp_softc *,
70 struct isapnp_attach_args **));
71 static void isapnp_print_region __P((const char *, struct isapnp_region *,
72 size_t));
73 static void isapnp_configure __P((struct isapnp_softc *,
74 const struct isapnp_attach_args *));
75 static void isapnp_print_pin __P((const char *, struct isapnp_pin *, size_t));
76 static int isapnp_print __P((void *, const char *));
77 #ifdef _KERNEL
78 static int isapnp_submatch __P((struct device *, struct cfdata *, void *));
79 #endif
80 static int isapnp_find __P((struct isapnp_softc *, int));
81 static int isapnp_match __P((struct device *, struct cfdata *, void *));
82 static void isapnp_attach __P((struct device *, struct device *, void *));
83 static void isapnp_callback __P((struct device *));
84
85 struct cfattach isapnp_ca = {
86 sizeof(struct isapnp_softc), isapnp_match, isapnp_attach
87 };
88
89 /*
90 * This keeps track if which ISA's we have been probed on.
91 */
92 struct isapnp_probe_cookie {
93 LIST_ENTRY(isapnp_probe_cookie) ipc_link;
94 struct device *ipc_parent;
95 };
96 LIST_HEAD(, isapnp_probe_cookie) isapnp_probes =
97 LIST_HEAD_INITIALIZER(isapnp_probes);
98
99 /* isapnp_init():
100 * Write the PNP initiation key to wake up the cards...
101 */
102 static void
103 isapnp_init(sc)
104 struct isapnp_softc *sc;
105 {
106 int i;
107 u_char v = ISAPNP_LFSR_INIT;
108
109 /* First write 0's twice to enter the Wait for Key state */
110 ISAPNP_WRITE_ADDR(sc, 0);
111 ISAPNP_WRITE_ADDR(sc, 0);
112
113 /* Send the 32 byte sequence to awake the logic */
114 for (i = 0; i < ISAPNP_LFSR_LENGTH; i++) {
115 ISAPNP_WRITE_ADDR(sc, v);
116 v = ISAPNP_LFSR_NEXT(v);
117 }
118 }
119
120
121 /* isapnp_shift_bit():
122 * Read a bit at a time from the config card.
123 */
124 static __inline u_char
125 isapnp_shift_bit(sc)
126 struct isapnp_softc *sc;
127 {
128 u_char c1, c2;
129
130 DELAY(250);
131 c1 = ISAPNP_READ_DATA(sc);
132 DELAY(250);
133 c2 = ISAPNP_READ_DATA(sc);
134
135 if (c1 == 0x55 && c2 == 0xAA)
136 return 0x80;
137 else
138 return 0;
139 }
140
141
142 /* isapnp_findcard():
143 * Attempt to read the vendor/serial/checksum for a card
144 * If a card is found [the checksum matches], assign the
145 * next card number to it and return 1
146 */
147 static int
148 isapnp_findcard(sc)
149 struct isapnp_softc *sc;
150 {
151 u_char v = ISAPNP_LFSR_INIT, csum, w;
152 int i, b;
153
154 if (sc->sc_ncards == ISAPNP_MAX_CARDS) {
155 printf("%s: Too many pnp cards\n", sc->sc_dev.dv_xname);
156 return 0;
157 }
158
159 /* Set the read port */
160 isapnp_write_reg(sc, ISAPNP_WAKE, 0);
161 isapnp_write_reg(sc, ISAPNP_SET_RD_PORT, sc->sc_read_port >> 2);
162 sc->sc_read_port |= 3;
163 DELAY(1000);
164
165 ISAPNP_WRITE_ADDR(sc, ISAPNP_SERIAL_ISOLATION);
166 DELAY(1000);
167
168 /* Read the 8 bytes of the Vendor ID and Serial Number */
169 for(i = 0; i < 8; i++) {
170 /* Read each bit separately */
171 for (w = 0, b = 0; b < 8; b++) {
172 u_char neg = isapnp_shift_bit(sc);
173
174 w >>= 1;
175 w |= neg;
176 v = ISAPNP_LFSR_NEXT(v) ^ neg;
177 }
178 sc->sc_id[sc->sc_ncards][i] = w;
179 }
180
181 /* Read the remaining checksum byte */
182 for (csum = 0, b = 0; b < 8; b++) {
183 u_char neg = isapnp_shift_bit(sc);
184
185 csum >>= 1;
186 csum |= neg;
187 }
188 sc->sc_id[sc->sc_ncards][8] = csum;
189
190 if (csum == v) {
191 sc->sc_ncards++;
192 isapnp_write_reg(sc, ISAPNP_CARD_SELECT_NUM, sc->sc_ncards);
193 return 1;
194 }
195 return 0;
196 }
197
198
199 /* isapnp_free_region():
200 * Free a region
201 */
202 static void
203 isapnp_free_region(t, r)
204 bus_space_tag_t t;
205 struct isapnp_region *r;
206 {
207 if (r->length == 0)
208 return;
209
210 #ifdef _KERNEL
211 bus_space_unmap(t, r->h, r->length);
212 #endif
213 }
214
215
216 /* isapnp_alloc_region():
217 * Allocate a single region if possible
218 */
219 static int
220 isapnp_alloc_region(t, r)
221 bus_space_tag_t t;
222 struct isapnp_region *r;
223 {
224 int error = 0;
225
226 if (r->length == 0) {
227 r->base = 0;
228 return 0;
229 }
230
231 for (r->base = r->minbase; r->base <= r->maxbase;
232 r->base += r->align) {
233 #ifdef _KERNEL
234 error = bus_space_map(t, r->base, r->length, 0, &r->h);
235 #endif
236 if (error == 0)
237 return 0;
238 if (r->align == 0)
239 break;
240 }
241 return error;
242 }
243
244
245 /* isapnp_alloc_irq():
246 * Allocate an irq
247 */
248 static int
249 isapnp_alloc_irq(ic, i)
250 isa_chipset_tag_t ic;
251 struct isapnp_pin *i;
252 {
253 int irq;
254 #define LEVEL_IRQ (ISAPNP_IRQTYPE_LEVEL_PLUS|ISAPNP_IRQTYPE_LEVEL_MINUS)
255 i->type = (i->flags & LEVEL_IRQ) ? IST_LEVEL : IST_EDGE;
256
257 if (i->bits == 0) {
258 i->num = 0;
259 return 0;
260 }
261
262 if (isa_intr_alloc(ic, i->bits, i->type, &irq) == 0) {
263 i->num = irq;
264 return 0;
265 }
266
267 return EINVAL;
268 }
269
270 /* isapnp_alloc_drq():
271 * Allocate a drq
272 */
273 static int
274 isapnp_alloc_drq(ic, i)
275 isa_chipset_tag_t ic;
276 struct isapnp_pin *i;
277 {
278 #if NISADMA > 0
279 int b;
280
281 if (i->bits == 0) {
282 i->num = 0;
283 return 0;
284 }
285
286 for (b = 0; b < 8; b++)
287 if ((i->bits & (1 << b)) && isa_drq_isfree(ic, b)) {
288 i->num = b;
289 return 0;
290 }
291 #endif /* NISADMA > 0 */
292
293 return EINVAL;
294 }
295
296 /* isapnp_testconfig():
297 * Test/Allocate the regions used
298 */
299 static int
300 isapnp_testconfig(iot, memt, ipa, alloc)
301 bus_space_tag_t iot, memt;
302 struct isapnp_attach_args *ipa;
303 int alloc;
304 {
305 int nio = 0, nmem = 0, nmem32 = 0, nirq = 0, ndrq = 0;
306 int error = 0;
307
308 #ifdef DEBUG_ISAPNP
309 isapnp_print_attach(ipa);
310 #endif
311
312 for (; nio < ipa->ipa_nio; nio++) {
313 error = isapnp_alloc_region(iot, &ipa->ipa_io[nio]);
314 if (error)
315 goto bad;
316 }
317
318 for (; nmem < ipa->ipa_nmem; nmem++) {
319 error = isapnp_alloc_region(memt, &ipa->ipa_mem[nmem]);
320 if (error)
321 goto bad;
322 }
323
324 for (; nmem32 < ipa->ipa_nmem32; nmem32++) {
325 error = isapnp_alloc_region(memt, &ipa->ipa_mem32[nmem32]);
326 if (error)
327 goto bad;
328 }
329
330 for (; nirq < ipa->ipa_nirq; nirq++) {
331 error = isapnp_alloc_irq(ipa->ipa_ic, &ipa->ipa_irq[nirq]);
332 if (error)
333 goto bad;
334 }
335
336 for (; ndrq < ipa->ipa_ndrq; ndrq++) {
337 error = isapnp_alloc_drq(ipa->ipa_ic, &ipa->ipa_drq[ndrq]);
338 if (error)
339 goto bad;
340 }
341
342 if (alloc)
343 return error;
344
345 bad:
346 #ifdef notyet
347 for (ndrq--; ndrq >= 0; ndrq--)
348 isapnp_free_pin(&ipa->ipa_drq[ndrq]);
349
350 for (nirq--; nirq >= 0; nirq--)
351 isapnp_free_pin(&ipa->ipa_irq[nirq]);
352 #endif
353
354 for (nmem32--; nmem32 >= 0; nmem32--)
355 isapnp_free_region(memt, &ipa->ipa_mem32[nmem32]);
356
357 for (nmem--; nmem >= 0; nmem--)
358 isapnp_free_region(memt, &ipa->ipa_mem[nmem]);
359
360 for (nio--; nio >= 0; nio--)
361 isapnp_free_region(iot, &ipa->ipa_io[nio]);
362
363 return error;
364 }
365
366
367 /* isapnp_config():
368 * Test/Allocate the regions used
369 */
370 int
371 isapnp_config(iot, memt, ipa)
372 bus_space_tag_t iot, memt;
373 struct isapnp_attach_args *ipa;
374 {
375 return isapnp_testconfig(iot, memt, ipa, 1);
376 }
377
378
379 /* isapnp_unconfig():
380 * Free the regions used
381 */
382 void
383 isapnp_unconfig(iot, memt, ipa)
384 bus_space_tag_t iot, memt;
385 struct isapnp_attach_args *ipa;
386 {
387 int i;
388
389 #ifdef notyet
390 for (i = 0; i < ipa->ipa_ndrq; i++)
391 isapnp_free_pin(&ipa->ipa_drq[i]);
392
393 for (i = 0; i < ipa->ipa_nirq; i++)
394 isapnp_free_pin(&ipa->ipa_irq[i]);
395 #endif
396
397 for (i = 0; i < ipa->ipa_nmem32; i++)
398 isapnp_free_region(memt, &ipa->ipa_mem32[i]);
399
400 for (i = 0; i < ipa->ipa_nmem; i++)
401 isapnp_free_region(memt, &ipa->ipa_mem[i]);
402
403 for (i = 0; i < ipa->ipa_nio; i++)
404 isapnp_free_region(iot, &ipa->ipa_io[i]);
405 }
406
407
408 /* isapnp_bestconfig():
409 * Return the best configuration for each logical device, remove and
410 * free all other configurations.
411 */
412 static struct isapnp_attach_args *
413 isapnp_bestconfig(sc, ipa)
414 struct isapnp_softc *sc;
415 struct isapnp_attach_args **ipa;
416 {
417 struct isapnp_attach_args *c, *best, *f = *ipa;
418 int error;
419
420 for (;;) {
421 if (f == NULL)
422 return NULL;
423
424 #define SAMEDEV(a, b) (strcmp((a)->ipa_devlogic, (b)->ipa_devlogic) == 0)
425
426 /* Find the best config */
427 for (best = c = f; c != NULL; c = c->ipa_sibling) {
428 if (!SAMEDEV(c, f))
429 continue;
430 if (c->ipa_pref < best->ipa_pref)
431 best = c;
432 }
433
434 /*
435 * Make sure the ISA chipset is initialized! We need
436 * it to test the best config!
437 */
438 best->ipa_ic = sc->sc_ic;
439
440 /* Test the best config */
441 error = isapnp_testconfig(sc->sc_iot, sc->sc_memt, best, 0);
442
443 /* Remove this config from the list */
444 if (best == f)
445 f = f->ipa_sibling;
446 else {
447 for (c = f; c->ipa_sibling != best; c = c->ipa_sibling)
448 continue;
449 c->ipa_sibling = best->ipa_sibling;
450 }
451
452 if (error) {
453 best->ipa_pref = ISAPNP_DEP_CONFLICTING;
454
455 for (c = f; c != NULL; c = c->ipa_sibling)
456 if (c != best && SAMEDEV(c, best))
457 break;
458 /* Last config for this logical device is conflicting */
459 if (c == NULL) {
460 *ipa = f;
461 return best;
462 }
463
464 ISAPNP_FREE(best);
465 continue;
466 }
467 else {
468 /* Remove all other configs for this device */
469 struct isapnp_attach_args *l = NULL, *n = NULL, *d;
470
471 for (c = f; c; ) {
472 if (c == best)
473 continue;
474 d = c->ipa_sibling;
475 if (SAMEDEV(c, best))
476 ISAPNP_FREE(c);
477 else {
478 if (n)
479 n->ipa_sibling = c;
480
481 else
482 l = c;
483 n = c;
484 c->ipa_sibling = NULL;
485 }
486 c = d;
487 }
488 f = l;
489 }
490 *ipa = f;
491 return best;
492 }
493 }
494
495
496 /* isapnp_id_to_vendor():
497 * Convert a pnp ``compressed ascii'' vendor id to a string
498 */
499 char *
500 isapnp_id_to_vendor(v, id)
501 char *v;
502 const u_char *id;
503 {
504 static const char hex[] = "0123456789ABCDEF";
505 char *p = v;
506
507 *p++ = 'A' + (id[0] >> 2) - 1;
508 *p++ = 'A' + ((id[0] & 3) << 3) + (id[1] >> 5) - 1;
509 *p++ = 'A' + (id[1] & 0x1f) - 1;
510 *p++ = hex[id[2] >> 4];
511 *p++ = hex[id[2] & 0x0f];
512 *p++ = hex[id[3] >> 4];
513 *p++ = hex[id[3] & 0x0f];
514 *p = '\0';
515
516 return v;
517 }
518
519
520 /* isapnp_print_region():
521 * Print a region allocation
522 */
523 static void
524 isapnp_print_region(str, r, n)
525 const char *str;
526 struct isapnp_region *r;
527 size_t n;
528 {
529 size_t i;
530
531 if (n == 0)
532 return;
533
534 printf(" %s ", str);
535 for (i = 0; i < n; i++, r++) {
536 printf("0x%x", r->base);
537 if (r->length)
538 printf("/%d", r->length);
539 if (i != n - 1)
540 printf(",");
541 }
542 }
543
544
545 /* isapnp_print_pin():
546 * Print an irq/drq assignment
547 */
548 static void
549 isapnp_print_pin(str, p, n)
550 const char *str;
551 struct isapnp_pin *p;
552 size_t n;
553 {
554 size_t i;
555
556 if (n == 0)
557 return;
558
559 printf(" %s ", str);
560 for (i = 0; i < n; i++, p++) {
561 printf("%d", p->num);
562 if (i != n - 1)
563 printf(",");
564 }
565 }
566
567
568 /* isapnp_print():
569 * Print the configuration line for an ISA PnP card.
570 */
571 static int
572 isapnp_print(aux, str)
573 void *aux;
574 const char *str;
575 {
576 struct isapnp_attach_args *ipa = aux;
577
578 if (str != NULL)
579 printf("%s: <%s, %s, %s, %s>",
580 str, ipa->ipa_devident, ipa->ipa_devlogic,
581 ipa->ipa_devcompat, ipa->ipa_devclass);
582
583 isapnp_print_region("port", ipa->ipa_io, ipa->ipa_nio);
584 isapnp_print_region("mem", ipa->ipa_mem, ipa->ipa_nmem);
585 isapnp_print_region("mem32", ipa->ipa_mem32, ipa->ipa_nmem32);
586 isapnp_print_pin("irq", ipa->ipa_irq, ipa->ipa_nirq);
587 isapnp_print_pin("drq", ipa->ipa_drq, ipa->ipa_ndrq);
588
589 return UNCONF;
590 }
591
592
593 #ifdef _KERNEL
594 /* isapnp_submatch():
595 * Probe the logical device...
596 */
597 static int
598 isapnp_submatch(parent, match, aux)
599 struct device *parent;
600 struct cfdata *match;
601 void *aux;
602 {
603 struct cfdata *cf = (struct cfdata *) match;
604 return ((*cf->cf_attach->ca_match)(parent, match, aux));
605 }
606
607
608 /* isapnp_devmatch():
609 * Match a probed device with the information from the driver
610 */
611 int
612 isapnp_devmatch(ipa, dinfo)
613 const struct isapnp_attach_args *ipa;
614 const struct isapnp_devinfo *dinfo;
615 {
616 const char *const *name;
617
618 for (name = dinfo->devlogic; *name; name++)
619 if (strcmp(*name, ipa->ipa_devlogic) == 0)
620 return 1;
621
622 for (name = dinfo->devcompat; *name; name++)
623 if (strcmp(*name, ipa->ipa_devcompat) == 0)
624 return 1;
625
626 return 0;
627 }
628
629
630 /* isapnp_isa_attach_hook():
631 * This routine is called from the isa attach code and
632 * is a kludge; we are resetting all the cards here in order
633 * to undo any card configuration that the bios did for us, in order
634 * to avoid having the PnP devices match an isa probe. The correct
635 * way of doing this is to read the PnP BIOS and find the card settings
636 * from there. Unfortunately it is not as easy as it sounds.
637 */
638 void
639 isapnp_isa_attach_hook(isa_sc)
640 struct isa_softc *isa_sc;
641 {
642 struct isapnp_softc sc;
643
644 sc.sc_iot = isa_sc->sc_iot;
645 sc.sc_ncards = 0;
646
647 if (isapnp_map(&sc))
648 return;
649
650 #if NWSS_ISAPNP > 0
651 /*
652 * XXX XXX
653 * This a totally disgusting hack, but I can't figure out another way.
654 * It seems that many CS audio chips have a bug (as far as I can
655 * understand). The reset below does not really reset the chip, it
656 * remains in a catatonic state and will not respond when probed.
657 * The chip can be used both as a WSS and as a SB device, and a
658 * single read at the WSS address (0x534) takes it out of this
659 * non-responsive state.
660 * The read has to happen at this point in time (or earlier) so
661 * it cannot be moved to the wss_isapnp.c driver.
662 * (BTW, We're not alone in having problems with these chips:
663 * Windoze 98 couldn't detect the sound chip on a Dell when I tried.)
664 *
665 * Lennart Augustsson <augustss (at) netbsd.org>
666 *
667 * (Implementation from John Kohl <jtk (at) kolvir.arlington.ma.us>)
668 */
669 {
670 bus_space_handle_t ioh;
671 int rv;
672 if ((rv = bus_space_map(sc.sc_iot, 0x534, 1, 0, &ioh)) == 0) {
673 DPRINTF(("wss probe kludge\n"));
674 (void)bus_space_read_1(sc.sc_iot, ioh, 0);
675 bus_space_unmap(sc.sc_iot, ioh, 1);
676 } else {
677 DPRINTF(("wss probe kludge failed to map: %d\n", rv));
678 }
679 }
680 #endif
681
682 isapnp_init(&sc);
683
684 isapnp_write_reg(&sc, ISAPNP_CONFIG_CONTROL, ISAPNP_CC_RESET_DRV);
685 DELAY(2000);
686
687 isapnp_unmap(&sc);
688 }
689 #endif
690
691
692 /* isapnp_find():
693 * Probe and add cards
694 */
695 static int
696 isapnp_find(sc, all)
697 struct isapnp_softc *sc;
698 int all;
699 {
700 int p;
701
702 isapnp_init(sc);
703
704 isapnp_write_reg(sc, ISAPNP_CONFIG_CONTROL, ISAPNP_CC_RESET_DRV);
705 DELAY(2000);
706
707 isapnp_init(sc);
708 DELAY(2000);
709
710 for (p = ISAPNP_RDDATA_MIN; p <= ISAPNP_RDDATA_MAX; p += 4) {
711 sc->sc_read_port = p;
712 if (isapnp_map_readport(sc))
713 continue;
714 DPRINTF(("%s: Trying port %x\r", sc->sc_dev.dv_xname, p));
715 if (isapnp_findcard(sc))
716 break;
717 isapnp_unmap_readport(sc);
718 }
719
720 if (p > ISAPNP_RDDATA_MAX) {
721 sc->sc_read_port = 0;
722 return 0;
723 }
724
725 if (all)
726 while (isapnp_findcard(sc))
727 continue;
728
729 return 1;
730 }
731
732
733 /* isapnp_configure():
734 * Configure a PnP card
735 * XXX: The memory configuration code is wrong. We need to check the
736 * range/length bit an do appropriate sets.
737 */
738 static void
739 isapnp_configure(sc, ipa)
740 struct isapnp_softc *sc;
741 const struct isapnp_attach_args *ipa;
742 {
743 int i;
744 static u_char isapnp_mem_range[] = ISAPNP_MEM_DESC;
745 static u_char isapnp_io_range[] = ISAPNP_IO_DESC;
746 static u_char isapnp_irq_range[] = ISAPNP_IRQ_DESC;
747 static u_char isapnp_drq_range[] = ISAPNP_DRQ_DESC;
748 static u_char isapnp_mem32_range[] = ISAPNP_MEM32_DESC;
749 const struct isapnp_region *r;
750 const struct isapnp_pin *p;
751 struct isapnp_region rz;
752 struct isapnp_pin pz;
753
754 memset(&pz, 0, sizeof(pz));
755 memset(&rz, 0, sizeof(rz));
756
757 #define B0(a) ((a) & 0xff)
758 #define B1(a) (((a) >> 8) & 0xff)
759 #define B2(a) (((a) >> 16) & 0xff)
760 #define B3(a) (((a) >> 24) & 0xff)
761
762 for (i = 0; i < sizeof(isapnp_io_range); i++) {
763 if (i < ipa->ipa_nio)
764 r = &ipa->ipa_io[i];
765 else
766 r = &rz;
767
768 isapnp_write_reg(sc,
769 isapnp_io_range[i] + ISAPNP_IO_BASE_15_8, B1(r->base));
770 isapnp_write_reg(sc,
771 isapnp_io_range[i] + ISAPNP_IO_BASE_7_0, B0(r->base));
772 }
773
774 for (i = 0; i < sizeof(isapnp_mem_range); i++) {
775 if (i < ipa->ipa_nmem)
776 r = &ipa->ipa_mem[i];
777 else
778 r = &rz;
779
780 isapnp_write_reg(sc,
781 isapnp_mem_range[i] + ISAPNP_MEM_BASE_23_16, B2(r->base));
782 isapnp_write_reg(sc,
783 isapnp_mem_range[i] + ISAPNP_MEM_BASE_15_8, B1(r->base));
784
785 isapnp_write_reg(sc,
786 isapnp_mem_range[i] + ISAPNP_MEM_LRANGE_23_16,
787 B2(r->length));
788 isapnp_write_reg(sc,
789 isapnp_mem_range[i] + ISAPNP_MEM_LRANGE_15_8,
790 B1(r->length));
791 }
792
793 for (i = 0; i < sizeof(isapnp_irq_range); i++) {
794 u_char v;
795
796 if (i < ipa->ipa_nirq)
797 p = &ipa->ipa_irq[i];
798 else
799 p = &pz;
800
801 isapnp_write_reg(sc,
802 isapnp_irq_range[i] + ISAPNP_IRQ_NUMBER, p->num);
803
804 switch (p->flags) {
805 case ISAPNP_IRQTYPE_LEVEL_PLUS:
806 v = ISAPNP_IRQ_LEVEL|ISAPNP_IRQ_HIGH;
807 break;
808
809 case ISAPNP_IRQTYPE_EDGE_PLUS:
810 v = ISAPNP_IRQ_HIGH;
811 break;
812
813 case ISAPNP_IRQTYPE_LEVEL_MINUS:
814 v = ISAPNP_IRQ_LEVEL;
815 break;
816
817 default:
818 case ISAPNP_IRQTYPE_EDGE_MINUS:
819 v = 0;
820 break;
821 }
822 isapnp_write_reg(sc,
823 isapnp_irq_range[i] + ISAPNP_IRQ_CONTROL, v);
824 }
825
826 for (i = 0; i < sizeof(isapnp_drq_range); i++) {
827 u_char v;
828
829 if (i < ipa->ipa_ndrq)
830 v = ipa->ipa_drq[i].num;
831 else
832 v = 4;
833
834 isapnp_write_reg(sc, isapnp_drq_range[i], v);
835 }
836
837 for (i = 0; i < sizeof(isapnp_mem32_range); i++) {
838 if (i < ipa->ipa_nmem32)
839 r = &ipa->ipa_mem32[i];
840 else
841 r = &rz;
842
843 isapnp_write_reg(sc,
844 isapnp_mem32_range[i] + ISAPNP_MEM32_BASE_31_24,
845 B3(r->base));
846 isapnp_write_reg(sc,
847 isapnp_mem32_range[i] + ISAPNP_MEM32_BASE_23_16,
848 B2(r->base));
849 isapnp_write_reg(sc,
850 isapnp_mem32_range[i] + ISAPNP_MEM32_BASE_15_8,
851 B1(r->base));
852 isapnp_write_reg(sc,
853 isapnp_mem32_range[i] + ISAPNP_MEM32_BASE_7_0,
854 B0(r->base));
855
856 isapnp_write_reg(sc,
857 isapnp_mem32_range[i] + ISAPNP_MEM32_LRANGE_31_24,
858 B3(r->length));
859 isapnp_write_reg(sc,
860 isapnp_mem32_range[i] + ISAPNP_MEM32_LRANGE_23_16,
861 B2(r->length));
862 isapnp_write_reg(sc,
863 isapnp_mem32_range[i] + ISAPNP_MEM32_LRANGE_15_8,
864 B1(r->length));
865 isapnp_write_reg(sc,
866 isapnp_mem32_range[i] + ISAPNP_MEM32_LRANGE_7_0,
867 B0(r->length));
868 }
869 }
870
871
872 /* isapnp_match():
873 * Probe routine
874 */
875 static int
876 isapnp_match(parent, match, aux)
877 struct device *parent;
878 struct cfdata *match;
879 void *aux;
880 {
881 struct isapnp_softc sc;
882 struct isa_attach_args *ia = aux;
883 struct isapnp_probe_cookie *ipc;
884
885 /*
886 * Ensure we only probe ISA PnP once; we don't actually consume
887 * bus resources, so we have to prevent being cloned forever.
888 */
889 for (ipc = LIST_FIRST(&isapnp_probes); ipc != NULL;
890 ipc = LIST_NEXT(ipc, ipc_link))
891 if (ipc->ipc_parent == parent)
892 return (0);
893
894 ipc = malloc(sizeof(*ipc), M_DEVBUF, M_NOWAIT);
895 if (ipc == NULL)
896 panic("isapnp_match: can't allocate probe cookie");
897
898 ipc->ipc_parent = parent;
899 LIST_INSERT_HEAD(&isapnp_probes, ipc, ipc_link);
900
901 sc.sc_iot = ia->ia_iot;
902 (void) strcpy(sc.sc_dev.dv_xname, "(isapnp probe)");
903
904 if (isapnp_map(&sc))
905 return 0;
906
907 isapnp_unmap(&sc);
908
909 /*
910 * We always match. We must let all legacy ISA devices map
911 * their address spaces before we look for a read port.
912 */
913 ia->ia_iobase = ISAPNP_ADDR;
914 ia->ia_iosize = 1;
915
916 return (1);
917 }
918
919
920 /* isapnp_attach
921 * Attach the PnP `bus'.
922 */
923 static void
924 isapnp_attach(parent, self, aux)
925 struct device *parent, *self;
926 void *aux;
927 {
928 struct isapnp_softc *sc = (struct isapnp_softc *) self;
929 struct isa_attach_args *ia = aux;
930
931 sc->sc_iot = ia->ia_iot;
932 sc->sc_memt = ia->ia_memt;
933 sc->sc_ic = ia->ia_ic;
934 sc->sc_dmat = ia->ia_dmat;
935 sc->sc_ncards = 0;
936
937 printf(": ISA Plug 'n Play device support\n");
938
939 if (isapnp_map(sc)) {
940 printf("%s: unable to map PnP register\n",
941 sc->sc_dev.dv_xname);
942 return;
943 }
944
945 #ifdef _KERNEL
946 /*
947 * Defer configuration until the rest of the ISA devices have
948 * attached themselves.
949 */
950 config_defer(self, isapnp_callback);
951 #else
952 isapnp_callback(self);
953 #endif
954 }
955
956 /* isapnp_callback
957 * Find and attach PnP cards.
958 */
959 void
960 isapnp_callback(self)
961 struct device *self;
962 {
963 struct isapnp_softc *sc = (struct isapnp_softc *)self;
964 struct isapnp_attach_args *ipa, *lpa;
965 int c, d;
966
967 /*
968 * Look for cards. If none are found, we say so and just return.
969 */
970 if (isapnp_find(sc, 1) == 0) {
971 printf("%s: no ISA Plug 'n Play devices found\n",
972 sc->sc_dev.dv_xname);
973 return;
974 }
975
976 printf("%s: read port 0x%x\n", sc->sc_dev.dv_xname, sc->sc_read_port);
977
978 /*
979 * Now configure all of the cards.
980 */
981 for (c = 0; c < sc->sc_ncards; c++) {
982 /* Good morning card c */
983 isapnp_write_reg(sc, ISAPNP_WAKE, c + 1);
984
985 if ((ipa = isapnp_get_resource(sc, c)) == NULL)
986 continue;
987
988 DPRINTF(("Selecting attachments\n"));
989 for (d = 0;
990 (lpa = isapnp_bestconfig(sc, &ipa)) != NULL; d++) {
991 isapnp_write_reg(sc, ISAPNP_LOGICAL_DEV_NUM, d);
992 isapnp_configure(sc, lpa);
993 #ifdef DEBUG_ISAPNP
994 {
995 struct isapnp_attach_args pa;
996
997 isapnp_get_config(sc, &pa);
998 isapnp_print_config(&pa);
999 }
1000 #endif
1001
1002 DPRINTF(("%s: configuring <%s, %s, %s, %s>\n",
1003 sc->sc_dev.dv_xname,
1004 lpa->ipa_devident, lpa->ipa_devlogic,
1005 lpa->ipa_devcompat, lpa->ipa_devclass));
1006 if (lpa->ipa_pref == ISAPNP_DEP_CONFLICTING) {
1007 printf("%s: <%s, %s, %s, %s> ignored; %s\n",
1008 sc->sc_dev.dv_xname,
1009 lpa->ipa_devident, lpa->ipa_devlogic,
1010 lpa->ipa_devcompat, lpa->ipa_devclass,
1011 "resource conflict");
1012 ISAPNP_FREE(lpa);
1013 continue;
1014 }
1015
1016 lpa->ipa_ic = sc->sc_ic;
1017 lpa->ipa_iot = sc->sc_iot;
1018 lpa->ipa_memt = sc->sc_memt;
1019 lpa->ipa_dmat = sc->sc_dmat;
1020
1021 isapnp_write_reg(sc, ISAPNP_ACTIVATE, 1);
1022 #ifdef _KERNEL
1023 if (config_found_sm(self, lpa, isapnp_print,
1024 isapnp_submatch) == NULL)
1025 isapnp_write_reg(sc, ISAPNP_ACTIVATE, 0);
1026 #else
1027 isapnp_print(lpa, NULL);
1028 printf("\n");
1029 #endif
1030 ISAPNP_FREE(lpa);
1031 }
1032 isapnp_write_reg(sc, ISAPNP_WAKE, 0); /* Good night cards */
1033 }
1034 }
1035