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