Expand PMF_FN_* macros.
[netbsd-mini2440.git] / sys / dev / isapnp / isapnp.c
blobb0265ad9f1a2ea5bf3a28ceac4721723c85416c3
1 /* $NetBSD: isapnp.c,v 1.58 2008/04/28 20:23:53 martin Exp $ */
3 /*-
4 * Copyright (c) 1996, 2008 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Christos Zoulas.
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.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
33 * ISA PnP bus autoconfiguration.
36 #include <sys/cdefs.h>
37 __KERNEL_RCSID(0, "$NetBSD: isapnp.c,v 1.58 2008/04/28 20:23:53 martin Exp $");
39 #include "isadma.h"
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/device.h>
44 #include <sys/malloc.h>
46 #include <sys/bus.h>
48 #include <dev/isa/isavar.h>
50 #include <dev/isapnp/isapnpreg.h>
51 #include <dev/isapnp/isapnpvar.h>
52 #include <dev/isapnp/isapnpdevs.h>
54 #include "wss_isapnp.h" /* XXX part of disgusting CS chip hack */
56 #ifndef ISAPNP_ALLOC_INTR_MASK
57 #define ISAPNP_ALLOC_INTR_MASK (~0)
58 #endif
60 static void isapnp_init(struct isapnp_softc *);
61 static inline u_char isapnp_shift_bit(struct isapnp_softc *);
62 static int isapnp_findcard(struct isapnp_softc *);
63 static void isapnp_free_region(bus_space_tag_t, struct isapnp_region *);
64 static int isapnp_alloc_region(bus_space_tag_t, struct isapnp_region *);
65 static int isapnp_alloc_irq(isa_chipset_tag_t, struct isapnp_pin *);
66 static int isapnp_alloc_drq(isa_chipset_tag_t, struct isapnp_pin *);
67 static int isapnp_testconfig(bus_space_tag_t, bus_space_tag_t,
68 struct isapnp_attach_args *, int);
69 static struct isapnp_attach_args *isapnp_bestconfig(struct isapnp_softc *,
70 struct isapnp_attach_args **);
71 static void isapnp_print_region(const char *, struct isapnp_region *, size_t);
72 static void isapnp_configure(struct isapnp_softc *,
73 const struct isapnp_attach_args *);
74 static void isapnp_print_pin(const char *, struct isapnp_pin *, size_t);
75 static int isapnp_print(void *, const char *);
76 #ifdef _KERNEL
77 static int isapnp_submatch(device_t, cfdata_t, const int *, void *);
78 #endif
79 static int isapnp_find(struct isapnp_softc *, int);
80 static int isapnp_match(device_t, cfdata_t, void *);
81 static void isapnp_attach(device_t, device_t, void *);
82 static void isapnp_callback(device_t);
84 CFATTACH_DECL_NEW(isapnp, sizeof(struct isapnp_softc),
85 isapnp_match, isapnp_attach, NULL, NULL);
88 * This keeps track if which ISA's we have been probed on.
90 struct isapnp_probe_cookie {
91 LIST_ENTRY(isapnp_probe_cookie) ipc_link;
92 device_t ipc_parent;
94 LIST_HEAD(, isapnp_probe_cookie) isapnp_probes =
95 LIST_HEAD_INITIALIZER(isapnp_probes);
97 /* isapnp_init():
98 * Write the PNP initiation key to wake up the cards...
100 static void
101 isapnp_init(struct isapnp_softc *sc)
103 int i;
104 u_char v = ISAPNP_LFSR_INIT;
106 /* First write 0's twice to enter the Wait for Key state */
107 ISAPNP_WRITE_ADDR(sc, 0);
108 ISAPNP_WRITE_ADDR(sc, 0);
110 /* Send the 32 byte sequence to awake the logic */
111 for (i = 0; i < ISAPNP_LFSR_LENGTH; i++) {
112 ISAPNP_WRITE_ADDR(sc, v);
113 v = ISAPNP_LFSR_NEXT(v);
118 /* isapnp_shift_bit():
119 * Read a bit at a time from the config card.
121 static inline u_char
122 isapnp_shift_bit(struct isapnp_softc *sc)
124 u_char c1, c2;
126 DELAY(250);
127 c1 = ISAPNP_READ_DATA(sc);
128 DELAY(250);
129 c2 = ISAPNP_READ_DATA(sc);
131 if (c1 == 0x55 && c2 == 0xAA)
132 return 0x80;
133 else
134 return 0;
138 /* isapnp_findcard():
139 * Attempt to read the vendor/serial/checksum for a card
140 * If a card is found [the checksum matches], assign the
141 * next card number to it and return 1
143 static int
144 isapnp_findcard(struct isapnp_softc *sc)
146 u_char v = ISAPNP_LFSR_INIT, csum, w;
147 int i, b;
149 if (sc->sc_ncards == ISAPNP_MAX_CARDS) {
150 aprint_error_dev(sc->sc_dev, "Too many pnp cards\n");
151 return 0;
154 /* Set the read port */
155 isapnp_write_reg(sc, ISAPNP_WAKE, 0);
156 isapnp_write_reg(sc, ISAPNP_SET_RD_PORT, sc->sc_read_port >> 2);
157 sc->sc_read_port |= 3;
158 DELAY(1000);
160 ISAPNP_WRITE_ADDR(sc, ISAPNP_SERIAL_ISOLATION);
161 DELAY(1000);
163 /* Read the 8 bytes of the Vendor ID and Serial Number */
164 for(i = 0; i < 8; i++) {
165 /* Read each bit separately */
166 for (w = 0, b = 0; b < 8; b++) {
167 u_char neg = isapnp_shift_bit(sc);
169 w >>= 1;
170 w |= neg;
171 v = ISAPNP_LFSR_NEXT(v) ^ neg;
173 sc->sc_id[sc->sc_ncards][i] = w;
176 /* Read the remaining checksum byte */
177 for (csum = 0, b = 0; b < 8; b++) {
178 u_char neg = isapnp_shift_bit(sc);
180 csum >>= 1;
181 csum |= neg;
183 sc->sc_id[sc->sc_ncards][8] = csum;
185 if (csum == v) {
186 sc->sc_ncards++;
187 isapnp_write_reg(sc, ISAPNP_CARD_SELECT_NUM, sc->sc_ncards);
188 return 1;
190 return 0;
194 /* isapnp_free_region():
195 * Free a region
197 static void
198 isapnp_free_region(bus_space_tag_t t, struct isapnp_region *r)
200 if (r->length == 0)
201 return;
203 #ifdef _KERNEL
204 bus_space_unmap(t, r->h, r->length);
205 #endif
209 /* isapnp_alloc_region():
210 * Allocate a single region if possible
212 static int
213 isapnp_alloc_region(bus_space_tag_t t, struct isapnp_region *r)
215 int error = 0;
217 if (r->length == 0) {
218 r->base = 0;
219 return 0;
222 for (r->base = r->minbase; r->base <= r->maxbase;
223 r->base += r->align) {
224 #ifdef _KERNEL
225 error = bus_space_map(t, r->base, r->length, 0, &r->h);
226 #endif
227 if (error == 0)
228 return 0;
229 if (r->align == 0)
230 break;
232 return error;
236 /* isapnp_alloc_irq():
237 * Allocate an irq
239 static int
240 isapnp_alloc_irq(isa_chipset_tag_t ic, struct isapnp_pin *i)
242 int irq;
243 #define LEVEL_IRQ (ISAPNP_IRQTYPE_LEVEL_PLUS|ISAPNP_IRQTYPE_LEVEL_MINUS)
244 i->type = (i->flags & LEVEL_IRQ) ? IST_LEVEL : IST_EDGE;
246 if (i->bits == 0) {
247 i->num = 0;
248 return 0;
251 if (isa_intr_alloc(ic, ISAPNP_ALLOC_INTR_MASK & i->bits,
252 i->type, &irq) == 0) {
253 i->num = irq;
254 return 0;
257 return EINVAL;
260 /* isapnp_alloc_drq():
261 * Allocate a drq
263 static int
264 isapnp_alloc_drq(isa_chipset_tag_t ic, struct isapnp_pin *i)
266 #if NISADMA > 0
267 int b;
269 if (i->bits == 0) {
270 i->num = 0;
271 return 0;
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;
279 #endif /* NISADMA > 0 */
281 return EINVAL;
284 /* isapnp_testconfig():
285 * Test/Allocate the regions used
287 static int
288 isapnp_testconfig(bus_space_tag_t iot, bus_space_tag_t memt,
289 struct isapnp_attach_args *ipa, int alloc)
291 int nio = 0, nmem = 0, nmem32 = 0, nirq = 0, ndrq = 0;
292 int error = 0;
294 #ifdef DEBUG_ISAPNP
295 isapnp_print_attach(ipa);
296 #endif
298 for (; nio < ipa->ipa_nio; nio++) {
299 error = isapnp_alloc_region(iot, &ipa->ipa_io[nio]);
300 if (error)
301 goto bad;
304 for (; nmem < ipa->ipa_nmem; nmem++) {
305 error = isapnp_alloc_region(memt, &ipa->ipa_mem[nmem]);
306 if (error)
307 goto bad;
310 for (; nmem32 < ipa->ipa_nmem32; nmem32++) {
311 error = isapnp_alloc_region(memt, &ipa->ipa_mem32[nmem32]);
312 if (error)
313 goto bad;
316 for (; nirq < ipa->ipa_nirq; nirq++) {
317 error = isapnp_alloc_irq(ipa->ipa_ic, &ipa->ipa_irq[nirq]);
318 if (error)
319 goto bad;
322 for (; ndrq < ipa->ipa_ndrq; ndrq++) {
323 error = isapnp_alloc_drq(ipa->ipa_ic, &ipa->ipa_drq[ndrq]);
324 if (error)
325 goto bad;
328 if (alloc)
329 return error;
331 bad:
332 #ifdef notyet
333 for (ndrq--; ndrq >= 0; ndrq--)
334 isapnp_free_pin(&ipa->ipa_drq[ndrq]);
336 for (nirq--; nirq >= 0; nirq--)
337 isapnp_free_pin(&ipa->ipa_irq[nirq]);
338 #endif
340 for (nmem32--; nmem32 >= 0; nmem32--)
341 isapnp_free_region(memt, &ipa->ipa_mem32[nmem32]);
343 for (nmem--; nmem >= 0; nmem--)
344 isapnp_free_region(memt, &ipa->ipa_mem[nmem]);
346 for (nio--; nio >= 0; nio--)
347 isapnp_free_region(iot, &ipa->ipa_io[nio]);
349 return error;
353 /* isapnp_config():
354 * Test/Allocate the regions used
357 isapnp_config(bus_space_tag_t iot, bus_space_tag_t memt,
358 struct isapnp_attach_args *ipa)
360 return isapnp_testconfig(iot, memt, ipa, 1);
364 /* isapnp_unconfig():
365 * Free the regions used
367 void
368 isapnp_unconfig(bus_space_tag_t iot, bus_space_tag_t memt,
369 struct isapnp_attach_args *ipa)
371 int i;
373 #ifdef notyet
374 for (i = 0; i < ipa->ipa_ndrq; i++)
375 isapnp_free_pin(&ipa->ipa_drq[i]);
377 for (i = 0; i < ipa->ipa_nirq; i++)
378 isapnp_free_pin(&ipa->ipa_irq[i]);
379 #endif
381 for (i = 0; i < ipa->ipa_nmem32; i++)
382 isapnp_free_region(memt, &ipa->ipa_mem32[i]);
384 for (i = 0; i < ipa->ipa_nmem; i++)
385 isapnp_free_region(memt, &ipa->ipa_mem[i]);
387 for (i = 0; i < ipa->ipa_nio; i++)
388 isapnp_free_region(iot, &ipa->ipa_io[i]);
392 /* isapnp_bestconfig():
393 * Return the best configuration for each logical device, remove and
394 * free all other configurations.
396 static struct isapnp_attach_args *
397 isapnp_bestconfig(struct isapnp_softc *sc, struct isapnp_attach_args **ipa)
399 struct isapnp_attach_args *c, *best, *f = *ipa;
400 int error;
402 for (;;) {
403 if (f == NULL)
404 return NULL;
406 #define SAMEDEV(a, b) (strcmp((a)->ipa_devlogic, (b)->ipa_devlogic) == 0)
408 /* Find the best config */
409 for (best = c = f; c != NULL; c = c->ipa_sibling) {
410 if (!SAMEDEV(c, f))
411 continue;
412 if (c->ipa_pref < best->ipa_pref)
413 best = c;
417 * Make sure the ISA chipset is initialized! We need
418 * it to test the best config!
420 best->ipa_ic = sc->sc_ic;
422 /* Test the best config */
423 error = isapnp_testconfig(sc->sc_iot, sc->sc_memt, best, 0);
425 /* Remove this config from the list */
426 if (best == f)
427 f = f->ipa_sibling;
428 else {
429 for (c = f; c->ipa_sibling != best; c = c->ipa_sibling)
430 continue;
431 c->ipa_sibling = best->ipa_sibling;
434 if (error) {
435 best->ipa_pref = ISAPNP_DEP_CONFLICTING;
437 for (c = f; c != NULL; c = c->ipa_sibling)
438 if (c != best && SAMEDEV(c, best))
439 break;
440 /* Last config for this logical device is conflicting */
441 if (c == NULL) {
442 *ipa = f;
443 return best;
446 ISAPNP_FREE(best);
447 continue;
449 else {
450 /* Remove all other configs for this device */
451 struct isapnp_attach_args *l = NULL, *n = NULL, *d;
453 for (c = f; c; ) {
454 if (c == best)
455 continue;
456 d = c->ipa_sibling;
457 if (SAMEDEV(c, best))
458 ISAPNP_FREE(c);
459 else {
460 if (n)
461 n->ipa_sibling = c;
463 else
464 l = c;
465 n = c;
466 c->ipa_sibling = NULL;
468 c = d;
470 f = l;
472 *ipa = f;
473 return best;
478 /* isapnp_id_to_vendor():
479 * Convert a pnp ``compressed ascii'' vendor id to a string
481 char *
482 isapnp_id_to_vendor(char *v, const u_char *id)
484 char *p = v;
486 *p++ = 'A' + (id[0] >> 2) - 1;
487 *p++ = 'A' + ((id[0] & 3) << 3) + (id[1] >> 5) - 1;
488 *p++ = 'A' + (id[1] & 0x1f) - 1;
489 *p++ = HEXDIGITS[id[2] >> 4];
490 *p++ = HEXDIGITS[id[2] & 0x0f];
491 *p++ = HEXDIGITS[id[3] >> 4];
492 *p++ = HEXDIGITS[id[3] & 0x0f];
493 *p = '\0';
495 return v;
499 /* isapnp_print_region():
500 * Print a region allocation
502 static void
503 isapnp_print_region(const char *str, struct isapnp_region *r, size_t n)
505 size_t i;
507 if (n == 0)
508 return;
510 aprint_normal(" %s ", str);
511 for (i = 0; i < n; i++, r++) {
512 aprint_normal("0x%x", r->base);
513 if (r->length)
514 aprint_normal("/%d", r->length);
515 if (i != n - 1)
516 aprint_normal(",");
521 /* isapnp_print_pin():
522 * Print an irq/drq assignment
524 static void
525 isapnp_print_pin(const char *str, struct isapnp_pin *p, size_t n)
527 size_t i;
529 if (n == 0)
530 return;
532 printf(" %s ", str);
533 for (i = 0; i < n; i++, p++) {
534 printf("%d", p->num);
535 if (i != n - 1)
536 printf(",");
541 /* isapnp_print():
542 * Print the configuration line for an ISA PnP card.
544 static int
545 isapnp_print(void *aux, const char *str)
547 struct isapnp_attach_args *ipa = aux;
549 if (str != NULL)
550 aprint_normal("%s: <%s, %s, %s, %s>",
551 str, ipa->ipa_devident, ipa->ipa_devlogic,
552 ipa->ipa_devcompat, ipa->ipa_devclass);
554 isapnp_print_region("port", ipa->ipa_io, ipa->ipa_nio);
555 isapnp_print_region("mem", ipa->ipa_mem, ipa->ipa_nmem);
556 isapnp_print_region("mem32", ipa->ipa_mem32, ipa->ipa_nmem32);
557 isapnp_print_pin("irq", ipa->ipa_irq, ipa->ipa_nirq);
558 isapnp_print_pin("drq", ipa->ipa_drq, ipa->ipa_ndrq);
560 return UNCONF;
564 #ifdef _KERNEL
565 /* isapnp_submatch():
566 * Probe the logical device...
568 static int
569 isapnp_submatch(device_t parent, cfdata_t match, const int *ldesc, void *aux)
572 return (config_match(parent, match, aux));
576 /* isapnp_devmatch():
577 * Match a probed device with the information from the driver
580 isapnp_devmatch(const struct isapnp_attach_args *ipa,
581 const struct isapnp_devinfo *dinfo, int *variant)
583 const struct isapnp_matchinfo *match;
584 int n;
586 for (match = dinfo->devlogic, n = dinfo->nlogic; n--; match++)
587 if (strcmp(match->name, ipa->ipa_devlogic) == 0) {
588 *variant = match->variant;
589 return (1);
592 for (match = dinfo->devcompat, n = dinfo->ncompat; n--; match++)
593 if (strcmp(match->name, ipa->ipa_devcompat) == 0) {
594 *variant = match->variant;
595 return (1);
598 return (0);
602 /* isapnp_isa_attach_hook():
603 * This routine is called from the isa attach code and
604 * is a kludge; we are resetting all the cards here in order
605 * to undo any card configuration that the bios did for us, in order
606 * to avoid having the PnP devices match an isa probe. The correct
607 * way of doing this is to read the PnP BIOS and find the card settings
608 * from there. Unfortunately it is not as easy as it sounds.
610 void
611 isapnp_isa_attach_hook(struct isa_softc *isa_sc)
613 struct isapnp_softc sc;
615 sc.sc_iot = isa_sc->sc_iot;
616 sc.sc_ncards = 0;
618 if (isapnp_map(&sc))
619 return;
621 #if NWSS_ISAPNP > 0
623 * XXX XXX
624 * This a totally disgusting hack, but I can't figure out another way.
625 * It seems that many CS audio chips have a bug (as far as I can
626 * understand). The reset below does not really reset the chip, it
627 * remains in a catatonic state and will not respond when probed.
628 * The chip can be used both as a WSS and as a SB device, and a
629 * single read at the WSS address (0x534) takes it out of this
630 * non-responsive state.
631 * The read has to happen at this point in time (or earlier) so
632 * it cannot be moved to the wss_isapnp.c driver.
633 * (BTW, We're not alone in having problems with these chips:
634 * Windoze 98 couldn't detect the sound chip on a Dell when I tried.)
636 * Lennart Augustsson <augustss@NetBSD.org>
638 * (Implementation from John Kohl <jtk@kolvir.arlington.ma.us>)
641 bus_space_handle_t ioh;
642 int rv;
643 if ((rv = bus_space_map(sc.sc_iot, 0x534, 1, 0, &ioh)) == 0) {
644 DPRINTF(("wss probe kludge\n"));
645 (void)bus_space_read_1(sc.sc_iot, ioh, 0);
646 bus_space_unmap(sc.sc_iot, ioh, 1);
647 } else {
648 DPRINTF(("wss probe kludge failed to map: %d\n", rv));
651 #endif
653 isapnp_init(&sc);
655 isapnp_write_reg(&sc, ISAPNP_CONFIG_CONTROL, ISAPNP_CC_RESET_DRV);
656 DELAY(2000);
658 isapnp_unmap(&sc);
660 #endif
663 /* isapnp_find():
664 * Probe and add cards
666 static int
667 isapnp_find(struct isapnp_softc *sc, int all)
669 int p;
671 isapnp_init(sc);
673 isapnp_write_reg(sc, ISAPNP_CONFIG_CONTROL, ISAPNP_CC_RESET_DRV);
674 DELAY(2000);
676 isapnp_init(sc);
677 DELAY(2000);
679 for (p = ISAPNP_RDDATA_MIN; p <= ISAPNP_RDDATA_MAX; p += 4) {
680 sc->sc_read_port = p;
681 if (isapnp_map_readport(sc))
682 continue;
683 DPRINTF(("%s: Trying port %x\r", device_xname(sc->sc_dev), p));
684 if (isapnp_findcard(sc))
685 break;
686 isapnp_unmap_readport(sc);
689 if (p > ISAPNP_RDDATA_MAX) {
690 sc->sc_read_port = 0;
691 return 0;
694 if (all)
695 while (isapnp_findcard(sc))
696 continue;
698 return 1;
702 /* isapnp_configure():
703 * Configure a PnP card
704 * XXX: The memory configuration code is wrong. We need to check the
705 * range/length bit an do appropriate sets.
707 static void
708 isapnp_configure(struct isapnp_softc *sc, const struct isapnp_attach_args *ipa)
710 int i;
711 static u_char isapnp_mem_range[] = ISAPNP_MEM_DESC;
712 static u_char isapnp_io_range[] = ISAPNP_IO_DESC;
713 static u_char isapnp_irq_range[] = ISAPNP_IRQ_DESC;
714 static u_char isapnp_drq_range[] = ISAPNP_DRQ_DESC;
715 static u_char isapnp_mem32_range[] = ISAPNP_MEM32_DESC;
716 const struct isapnp_region *r;
717 const struct isapnp_pin *p;
718 struct isapnp_region rz;
719 struct isapnp_pin pz;
721 memset(&pz, 0, sizeof(pz));
722 memset(&rz, 0, sizeof(rz));
724 #define B0(a) ((a) & 0xff)
725 #define B1(a) (((a) >> 8) & 0xff)
726 #define B2(a) (((a) >> 16) & 0xff)
727 #define B3(a) (((a) >> 24) & 0xff)
729 for (i = 0; i < sizeof(isapnp_io_range); i++) {
730 if (i < ipa->ipa_nio)
731 r = &ipa->ipa_io[i];
732 else
733 r = &rz;
735 isapnp_write_reg(sc,
736 isapnp_io_range[i] + ISAPNP_IO_BASE_15_8, B1(r->base));
737 isapnp_write_reg(sc,
738 isapnp_io_range[i] + ISAPNP_IO_BASE_7_0, B0(r->base));
741 for (i = 0; i < sizeof(isapnp_mem_range); i++) {
742 if (i < ipa->ipa_nmem)
743 r = &ipa->ipa_mem[i];
744 else
745 r = &rz;
747 isapnp_write_reg(sc,
748 isapnp_mem_range[i] + ISAPNP_MEM_BASE_23_16, B2(r->base));
749 isapnp_write_reg(sc,
750 isapnp_mem_range[i] + ISAPNP_MEM_BASE_15_8, B1(r->base));
752 isapnp_write_reg(sc,
753 isapnp_mem_range[i] + ISAPNP_MEM_LRANGE_23_16,
754 B2(r->length));
755 isapnp_write_reg(sc,
756 isapnp_mem_range[i] + ISAPNP_MEM_LRANGE_15_8,
757 B1(r->length));
760 for (i = 0; i < sizeof(isapnp_irq_range); i++) {
761 u_char v;
763 if (i < ipa->ipa_nirq)
764 p = &ipa->ipa_irq[i];
765 else
766 p = &pz;
768 isapnp_write_reg(sc,
769 isapnp_irq_range[i] + ISAPNP_IRQ_NUMBER, p->num);
771 switch (p->flags) {
772 case ISAPNP_IRQTYPE_LEVEL_PLUS:
773 v = ISAPNP_IRQ_LEVEL|ISAPNP_IRQ_HIGH;
774 break;
776 case ISAPNP_IRQTYPE_EDGE_PLUS:
777 v = ISAPNP_IRQ_HIGH;
778 break;
780 case ISAPNP_IRQTYPE_LEVEL_MINUS:
781 v = ISAPNP_IRQ_LEVEL;
782 break;
784 default:
785 case ISAPNP_IRQTYPE_EDGE_MINUS:
786 v = 0;
787 break;
789 isapnp_write_reg(sc,
790 isapnp_irq_range[i] + ISAPNP_IRQ_CONTROL, v);
793 for (i = 0; i < sizeof(isapnp_drq_range); i++) {
794 u_char v;
796 if (i < ipa->ipa_ndrq)
797 v = ipa->ipa_drq[i].num;
798 else
799 v = 4;
801 isapnp_write_reg(sc, isapnp_drq_range[i], v);
804 for (i = 0; i < sizeof(isapnp_mem32_range); i++) {
805 if (i < ipa->ipa_nmem32)
806 r = &ipa->ipa_mem32[i];
807 else
808 r = &rz;
810 isapnp_write_reg(sc,
811 isapnp_mem32_range[i] + ISAPNP_MEM32_BASE_31_24,
812 B3(r->base));
813 isapnp_write_reg(sc,
814 isapnp_mem32_range[i] + ISAPNP_MEM32_BASE_23_16,
815 B2(r->base));
816 isapnp_write_reg(sc,
817 isapnp_mem32_range[i] + ISAPNP_MEM32_BASE_15_8,
818 B1(r->base));
819 isapnp_write_reg(sc,
820 isapnp_mem32_range[i] + ISAPNP_MEM32_BASE_7_0,
821 B0(r->base));
823 isapnp_write_reg(sc,
824 isapnp_mem32_range[i] + ISAPNP_MEM32_LRANGE_31_24,
825 B3(r->length));
826 isapnp_write_reg(sc,
827 isapnp_mem32_range[i] + ISAPNP_MEM32_LRANGE_23_16,
828 B2(r->length));
829 isapnp_write_reg(sc,
830 isapnp_mem32_range[i] + ISAPNP_MEM32_LRANGE_15_8,
831 B1(r->length));
832 isapnp_write_reg(sc,
833 isapnp_mem32_range[i] + ISAPNP_MEM32_LRANGE_7_0,
834 B0(r->length));
839 /* isapnp_match():
840 * Probe routine
842 static int
843 isapnp_match(device_t parent, cfdata_t match, void *aux)
845 struct isapnp_softc sc;
846 struct isa_attach_args *ia = aux;
847 struct isapnp_probe_cookie *ipc;
850 * If the system has no ISA expansion slots, skip the probe
851 * because it's very slow.
853 if (isa_get_slotcount() == 0)
854 return (0);
857 * Ensure we only probe ISA PnP once; we don't actually consume
858 * bus resources, so we have to prevent being cloned forever.
860 for (ipc = LIST_FIRST(&isapnp_probes); ipc != NULL;
861 ipc = LIST_NEXT(ipc, ipc_link))
862 if (ipc->ipc_parent == parent)
863 return (0);
865 ipc = malloc(sizeof(*ipc), M_DEVBUF, M_NOWAIT);
866 if (ipc == NULL)
867 panic("isapnp_match: can't allocate probe cookie");
869 ipc->ipc_parent = parent;
870 LIST_INSERT_HEAD(&isapnp_probes, ipc, ipc_link);
872 sc.sc_iot = ia->ia_iot;
874 if (isapnp_map(&sc))
875 return 0;
877 isapnp_unmap(&sc);
880 * We always match. We must let all legacy ISA devices map
881 * their address spaces before we look for a read port.
883 ia->ia_io[0].ir_addr = ISAPNP_ADDR;
884 ia->ia_io[0].ir_size = 1;
886 ia->ia_niomem = 0;
887 ia->ia_nirq = 0;
888 ia->ia_ndrq = 0;
890 return (1);
894 /* isapnp_attach
895 * Attach the PnP `bus'.
897 static void
898 isapnp_attach(device_t parent, device_t self, void *aux)
900 struct isapnp_softc *sc = device_private(self);
901 struct isa_attach_args *ia = aux;
903 sc->sc_dev = self;
904 sc->sc_iot = ia->ia_iot;
905 sc->sc_memt = ia->ia_memt;
906 sc->sc_ic = ia->ia_ic;
907 sc->sc_dmat = ia->ia_dmat;
908 sc->sc_ncards = 0;
910 aprint_naive("\n");
911 aprint_normal("\n");
913 if (isapnp_map(sc)) {
914 aprint_error_dev(self, "unable to map PnP register\n");
915 return;
918 #ifdef _KERNEL
920 * Defer configuration until the rest of the ISA devices have
921 * attached themselves.
923 config_defer(self, isapnp_callback);
924 #else
925 isapnp_callback(self);
926 #endif
928 if (!pmf_device_register(self, NULL, NULL))
929 aprint_error_dev(self, "couldn't establish power handler\n");
932 /* isapnp_callback
933 * Find and attach PnP cards.
935 void
936 isapnp_callback(device_t self)
938 struct isapnp_softc *sc = device_private(self);
939 struct isapnp_attach_args *ipa, *lpa;
940 int c, d;
943 * Look for cards. If none are found, we say so and just return.
945 if (isapnp_find(sc, 1) == 0) {
946 aprint_verbose_dev(sc->sc_dev,
947 "no ISA Plug 'n Play devices found\n");
948 return;
951 aprint_verbose_dev(sc->sc_dev, "read port 0x%x\n", sc->sc_read_port);
954 * Now configure all of the cards.
956 for (c = 0; c < sc->sc_ncards; c++) {
957 /* Good morning card c */
958 isapnp_write_reg(sc, ISAPNP_WAKE, c + 1);
960 if ((ipa = isapnp_get_resource(sc, c)) == NULL)
961 continue;
963 DPRINTF(("Selecting attachments\n"));
964 for (d = 0;
965 (lpa = isapnp_bestconfig(sc, &ipa)) != NULL; d++) {
966 isapnp_write_reg(sc, ISAPNP_LOGICAL_DEV_NUM, d);
967 isapnp_configure(sc, lpa);
968 #ifdef DEBUG_ISAPNP
970 struct isapnp_attach_args pa;
972 isapnp_get_config(sc, &pa);
973 isapnp_print_config(&pa);
975 #endif
977 DPRINTF(("%s: configuring <%s, %s, %s, %s>\n",
978 device_xname(sc->sc_dev),
979 lpa->ipa_devident, lpa->ipa_devlogic,
980 lpa->ipa_devcompat, lpa->ipa_devclass));
981 if (lpa->ipa_pref == ISAPNP_DEP_CONFLICTING) {
982 aprint_verbose_dev(sc->sc_dev,
983 "<%s, %s, %s, %s> ignored; %s\n",
984 lpa->ipa_devident, lpa->ipa_devlogic,
985 lpa->ipa_devcompat, lpa->ipa_devclass,
986 "resource conflict");
987 ISAPNP_FREE(lpa);
988 continue;
991 lpa->ipa_ic = sc->sc_ic;
992 lpa->ipa_iot = sc->sc_iot;
993 lpa->ipa_memt = sc->sc_memt;
994 lpa->ipa_dmat = sc->sc_dmat;
996 isapnp_write_reg(sc, ISAPNP_ACTIVATE, 1);
997 #ifdef _KERNEL
998 if (config_found_sm_loc(self, "isapnp", NULL, lpa,
999 isapnp_print, isapnp_submatch) == NULL)
1000 isapnp_write_reg(sc, ISAPNP_ACTIVATE, 0);
1001 #else
1002 isapnp_print(lpa, NULL);
1003 aprint_verbose("\n");
1004 #endif
1005 ISAPNP_FREE(lpa);
1007 isapnp_write_reg(sc, ISAPNP_WAKE, 0); /* Good night cards */