xref: /openbsd-src/sys/dev/isa/isapnp.c (revision 91f110e064cd7c194e59e019b83bb7496c1c84d4)
1 /*	$OpenBSD: isapnp.c,v 1.40 2011/06/29 12:17:40 tedu Exp $	*/
2 /*	$NetBSD: isapnp.c,v 1.9.4.3 1997/10/29 00:40:43 thorpej Exp $	*/
3 
4 /*
5  * Copyright (c) 1996 Christos Zoulas.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed by Christos Zoulas.
18  * 4. The name of the author may not be used to endorse or promote products
19  *    derived from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 /*
34  * ISA PnP bus autoconfiguration.
35  */
36 
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/device.h>
40 #include <sys/malloc.h>
41 
42 #include <machine/bus.h>
43 
44 #include <dev/isa/isapnpreg.h>
45 
46 #include <dev/isa/isavar.h>
47 #include <dev/isa/isadmavar.h>
48 
49 #include <dev/isa/pnpdevs.h>
50 
51 #include "isadma.h"
52 
53 void isapnp_init(struct isapnp_softc *);
54 static __inline u_char isapnp_shift_bit(struct isapnp_softc *);
55 int isapnp_findcard(struct isapnp_softc *);
56 void isapnp_free_region(bus_space_tag_t, struct isapnp_region *);
57 int isapnp_alloc_region(bus_space_tag_t, struct isapnp_region *);
58 int isapnp_alloc_irq(isa_chipset_tag_t, struct isapnp_pin *);
59 int isapnp_alloc_drq(struct device *, struct isapnp_pin *);
60 int isapnp_testconfig(bus_space_tag_t, bus_space_tag_t,
61     struct isa_attach_args *, int);
62 struct isa_attach_args *isapnp_bestconfig(struct device *,
63     struct isapnp_softc *, struct isa_attach_args **);
64 void isapnp_print_region(const char *, struct isapnp_region *,
65     size_t);
66 void isapnp_configure(struct isapnp_softc *,
67     const struct isa_attach_args *);
68 void isapnp_print_pin(const char *, struct isapnp_pin *, size_t);
69 int isapnp_print(void *, const char *);
70 int isapnp_submatch(struct device *, void *, void *);
71 int isapnp_com_submatch(struct device *, void *, void *);
72 int isapnp_find(struct isapnp_softc *, int);
73 int isapnp_match(struct device *, void *, void *);
74 void isapnp_attach(struct device *, struct device *, void *);
75 
76 #ifdef DEBUG_ISAPNP
77 # define DPRINTF(a) printf a
78 #else
79 # define DPRINTF(a)
80 #endif
81 
82 struct cfattach isapnp_ca = {
83 	sizeof(struct isapnp_softc), isapnp_match, isapnp_attach
84 };
85 
86 struct cfdriver isapnp_cd = {
87 	NULL, "isapnp", DV_DULL
88 };
89 
90 
91 /* isapnp_init():
92  *	Write the PNP initiation key to wake up the cards...
93  */
94 void
95 isapnp_init(sc)
96 	struct isapnp_softc *sc;
97 {
98 	int i;
99 	u_char v = ISAPNP_LFSR_INIT;
100 
101 	/* First write 0's twice to enter the Wait for Key state */
102 	ISAPNP_WRITE_ADDR(sc, 0);
103 	ISAPNP_WRITE_ADDR(sc, 0);
104 
105 	/* Send the 32 byte sequence to awake the logic */
106 	for (i = 0; i < ISAPNP_LFSR_LENGTH; i++) {
107 		ISAPNP_WRITE_ADDR(sc, v);
108 		v = ISAPNP_LFSR_NEXT(v);
109 	}
110 }
111 
112 
113 /* isapnp_shift_bit():
114  *	Read a bit at a time from the config card.
115  */
116 static __inline u_char
117 isapnp_shift_bit(sc)
118 	struct isapnp_softc *sc;
119 {
120 	u_char c1, c2;
121 
122 	DELAY(250);
123 	c1 = ISAPNP_READ_DATA(sc);
124 	DELAY(250);
125 	c2 = ISAPNP_READ_DATA(sc);
126 
127 	if (c1 == 0x55 && c2 == 0xAA)
128 		return 0x80;
129 	else
130 		return 0;
131 }
132 
133 
134 /* isapnp_findcard():
135  *	Attempt to read the vendor/serial/checksum for a card
136  *	If a card is found [the checksum matches], assign the
137  *	next card number to it and return 1
138  */
139 int
140 isapnp_findcard(sc)
141 	struct isapnp_softc *sc;
142 {
143 	u_char v = ISAPNP_LFSR_INIT, csum, w;
144 	int i, b;
145 
146 	if (sc->sc_ncards == ISAPNP_MAX_CARDS) {
147 		printf("%s: Too many pnp cards\n", sc->sc_dev.dv_xname);
148 		return 0;
149 	}
150 
151 	/* Set the read port */
152 	isapnp_write_reg(sc, ISAPNP_WAKE, 0);
153 	isapnp_write_reg(sc, ISAPNP_SET_RD_PORT, sc->sc_read_port >> 2);
154 	sc->sc_read_port |= 3;
155 	DELAY(1000);
156 
157 	ISAPNP_WRITE_ADDR(sc, ISAPNP_SERIAL_ISOLATION);
158 	DELAY(1000);
159 
160 	/* Read the 8 bytes of the Vendor ID and Serial Number */
161 	for(i = 0; i < 8; i++) {
162 		/* Read each bit separately */
163 		for (w = 0, b = 0; b < 8; b++) {
164 			u_char neg = isapnp_shift_bit(sc);
165 
166 			w >>= 1;
167 			w |= neg;
168 			v = ISAPNP_LFSR_NEXT(v) ^ neg;
169 		}
170 		sc->sc_id[sc->sc_ncards][i] = w;
171 	}
172 
173 	/* Read the remaining checksum byte */
174 	for (csum = 0, b = 0; b < 8; b++) {
175 		u_char neg = isapnp_shift_bit(sc);
176 
177 		csum >>= 1;
178 		csum |= neg;
179 	}
180 	sc->sc_id[sc->sc_ncards][8] = csum;
181 
182 	if (csum == v) {
183 		sc->sc_ncards++;
184 		isapnp_write_reg(sc, ISAPNP_CARD_SELECT_NUM, sc->sc_ncards);
185 		return 1;
186 	}
187 	return 0;
188 }
189 
190 
191 /* isapnp_free_region():
192  *	Free a region
193  */
194 void
195 isapnp_free_region(t, r)
196 	bus_space_tag_t t;
197 	struct isapnp_region *r;
198 {
199 	if (r->length == 0)
200 		return;
201 
202 	bus_space_unmap(t, r->h, r->length);
203 	r->h = 0;
204 }
205 
206 
207 /* isapnp_alloc_region():
208  *	Allocate a single region if possible
209  */
210 int
211 isapnp_alloc_region(t, r)
212 	bus_space_tag_t t;
213 	struct isapnp_region *r;
214 {
215 	int error = 0;
216 
217 	if (r->length == 0)
218 		return 0;
219 
220 	r->h = 0;
221 	for (r->base = r->minbase; r->base <= r->maxbase;
222 	     r->base += r->align) {
223 		error = bus_space_map(t, r->base, r->length, 0, &r->h);
224 		if (error == 0)
225 			return 0;
226 	}
227 	return error;
228 }
229 
230 
231 /* isapnp_alloc_irq():
232  *	Allocate an irq
233  */
234 int
235 isapnp_alloc_irq(ic, i)
236 	isa_chipset_tag_t ic;
237 	struct isapnp_pin *i;
238 {
239 	int irq;
240 #define LEVEL_IRQ (ISAPNP_IRQTYPE_LEVEL_PLUS|ISAPNP_IRQTYPE_LEVEL_MINUS)
241 	i->type = (i->flags & LEVEL_IRQ) ? IST_LEVEL : IST_EDGE;
242 
243 	if (i->bits == 0) {
244 		i->num = 0;
245 		return 0;
246 	}
247 
248 	if (isa_intr_alloc(ic, i->bits, i->type, &irq) == 0) {
249 		i->num = irq;
250 		return 0;
251 	}
252 
253 	return EINVAL;
254 }
255 
256 /* isapnp_alloc_drq():
257  *	Allocate a drq
258  */
259 int
260 isapnp_alloc_drq(isa, i)
261 	struct device *isa;
262 	struct isapnp_pin *i;
263 {
264 #if NISADMA > 0
265 	int b;
266 
267 	if (i->bits == 0) {
268 		i->num = 0;
269 		return 0;
270 	}
271 
272 	for (b = 0; b < 16; b++)
273 		if ((i->bits & (1 << b)) && isa_drq_isfree(isa, b)) {
274 			i->num = b;
275 			return 0;
276 		}
277 #endif
278 
279 	return EINVAL;
280 }
281 
282 /* isapnp_testconfig():
283  *	Test/Allocate the regions used
284  */
285 int
286 isapnp_testconfig(iot, memt, ipa, alloc)
287 	bus_space_tag_t iot, memt;
288 	struct isa_attach_args *ipa;
289 	int alloc;
290 {
291 	int nio = 0, nmem = 0, nmem32 = 0, nirq = 0, ndrq = 0;
292 	int error = 0;
293 
294 #ifdef DEBUG_ISAPNP
295 	isapnp_print_attach(ipa);
296 #endif
297 
298 	for (; nio < ipa->ipa_nio; nio++) {
299 		error = isapnp_alloc_region(iot, &ipa->ipa_io[nio]);
300 		if (error)
301 			goto bad;
302 	}
303 
304 	for (; nmem < ipa->ipa_nmem; nmem++) {
305 		error = isapnp_alloc_region(memt, &ipa->ipa_mem[nmem]);
306 		if (error)
307 			goto bad;
308 	}
309 
310 	for (; nmem32 < ipa->ipa_nmem32; nmem32++) {
311 		error = isapnp_alloc_region(memt, &ipa->ipa_mem32[nmem32]);
312 		if (error)
313 			goto bad;
314 	}
315 
316 	for (; nirq < ipa->ipa_nirq; nirq++) {
317 		error = isapnp_alloc_irq(ipa->ia_ic, &ipa->ipa_irq[nirq]);
318 		if (error)
319 			goto bad;
320 	}
321 
322 	for (; ndrq < ipa->ipa_ndrq; ndrq++) {
323 		error = isapnp_alloc_drq(ipa->ia_isa, &ipa->ipa_drq[ndrq]);
324 		if (error)
325 			goto bad;
326 	}
327 
328 	if (alloc)
329 		return error;
330 
331 bad:
332 	for (nmem32--; nmem32 >= 0; nmem32--)
333 		isapnp_free_region(memt, &ipa->ipa_mem32[nmem32]);
334 
335 	for (nmem--; nmem >= 0; nmem--)
336 		isapnp_free_region(memt, &ipa->ipa_mem[nmem]);
337 
338 	for (nio--; nio >= 0; nio--)
339 		isapnp_free_region(iot, &ipa->ipa_io[nio]);
340 
341 	return error;
342 }
343 
344 
345 /* isapnp_config():
346  *	Test/Allocate the regions used
347  */
348 int
349 isapnp_config(iot, memt, ipa)
350 	bus_space_tag_t iot, memt;
351 	struct isa_attach_args *ipa;
352 {
353 	return isapnp_testconfig(iot, memt, ipa, 1);
354 }
355 
356 
357 /* isapnp_unconfig():
358  *	Free the regions used
359  */
360 void
361 isapnp_unconfig(iot, memt, ipa)
362 	bus_space_tag_t iot, memt;
363 	struct isa_attach_args *ipa;
364 {
365 	int i;
366 
367 	for (i = 0; i < ipa->ipa_nmem32; i++)
368 		isapnp_free_region(memt, &ipa->ipa_mem32[i]);
369 
370 	for (i = 0; i < ipa->ipa_nmem; i++)
371 		isapnp_free_region(memt, &ipa->ipa_mem[i]);
372 
373 	for (i = 0; i < ipa->ipa_nio; i++)
374 		isapnp_free_region(iot, &ipa->ipa_io[i]);
375 }
376 
377 
378 /* isapnp_bestconfig():
379  *	Return the best configuration for each logical device, remove and
380  *	free all other configurations.
381  */
382 struct isa_attach_args *
383 isapnp_bestconfig(isa, sc, ipa)
384 	struct device *isa;
385 	struct isapnp_softc *sc;
386 	struct isa_attach_args **ipa;
387 {
388 	struct isa_attach_args *c, *best, *f = *ipa;
389 	int error;
390 
391 	for (;;) {
392 		if (f == NULL)
393 			return NULL;
394 
395 #define SAMEDEV(a, b) (strcmp((a)->ipa_devlogic, (b)->ipa_devlogic) == 0)
396 
397 		/* Find the best config */
398 		for (best = c = f; c != NULL; c = c->ipa_sibling) {
399 			if (!SAMEDEV(c, f))
400 				continue;
401 			if (c->ipa_pref < best->ipa_pref)
402 				best = c;
403 		}
404 
405 		best->ia_isa = isa;
406 		/* Test the best config */
407 		error = isapnp_testconfig(sc->sc_iot, sc->sc_memt, best, 0);
408 
409 		/* Remove this config from the list */
410 		if (best == f)
411 			f = f->ipa_sibling;
412 		else {
413 			for (c = f; c->ipa_sibling != best; c = c->ipa_sibling)
414 				continue;
415 			c->ipa_sibling = best->ipa_sibling;
416 		}
417 
418 		if (error) {
419 			best->ipa_pref = ISAPNP_DEP_CONFLICTING;
420 
421 			for (c = f; c != NULL; c = c->ipa_sibling)
422 				if (c != best && SAMEDEV(c, best))
423 					break;
424 			/* Last config for this logical device is conflicting */
425 			if (c == NULL) {
426 				*ipa = f;
427 				return best;
428 			}
429 
430 			free(best, M_DEVBUF);
431 			continue;
432 		}
433 		else {
434 			/* Remove all other configs for this device */
435 			struct isa_attach_args *l = NULL, *n = NULL, *d;
436 
437 			for (c = f; c; ) {
438 				if (c == best)
439 					continue;
440 				d = c->ipa_sibling;
441 				if (SAMEDEV(c, best))
442 					free(c, M_DEVBUF);
443 				else {
444 					if (n)
445 						n->ipa_sibling = c;
446 
447 					else
448 						l = c;
449 					n = c;
450 					c->ipa_sibling = NULL;
451 				}
452 				c = d;
453 			}
454 			f = l;
455 		}
456 		*ipa = f;
457 		return best;
458 	}
459 }
460 
461 
462 /* isapnp_id_to_vendor():
463  *	Convert a pnp ``compressed ascii'' vendor id to a string
464  */
465 char *
466 isapnp_id_to_vendor(v, id)
467 	char   *v;
468 	const u_char *id;
469 {
470 	static const char hex[] = "0123456789ABCDEF";
471 	char *p = v;
472 
473 	*p++ = 'A' + (id[0] >> 2) - 1;
474 	*p++ = 'A' + ((id[0] & 3) << 3) + (id[1] >> 5) - 1;
475 	*p++ = 'A' + (id[1] & 0x1f) - 1;
476 	*p++ = hex[id[2] >> 4];
477 	*p++ = hex[id[2] & 0x0f];
478 	*p++ = hex[id[3] >> 4];
479 	*p++ = hex[id[3] & 0x0f];
480 	*p = '\0';
481 
482 	return v;
483 }
484 
485 
486 /* isapnp_print_region():
487  *	Print a region allocation
488  */
489 void
490 isapnp_print_region(str, r, n)
491 	const char *str;
492 	struct isapnp_region *r;
493 	size_t n;
494 {
495 	size_t i;
496 
497 	if (n == 0)
498 		return;
499 
500 	printf(" %s ", str);
501 	for (i = 0; i < n; i++, r++) {
502 		printf("0x%x", r->base);
503 		if (r->length)
504 			printf("/%d", r->length);
505 		if (i != n - 1)
506 			printf(",");
507 	}
508 }
509 
510 
511 /* isapnp_print_pin():
512  *	Print an irq/drq assignment
513  */
514 void
515 isapnp_print_pin(str, p, n)
516 	const char *str;
517 	struct isapnp_pin *p;
518 	size_t n;
519 {
520 	size_t i;
521 
522 	if (n == 0)
523 		return;
524 
525 	printf(" %s ", str);
526 	for (i = 0; i < n; i++, p++) {
527 		printf("%d", p->num);
528 		if (i != n - 1)
529 			printf(",");
530 	}
531 }
532 
533 /* isapnp_print():
534  *	Print the configuration line for an ISA PnP card.
535  */
536 int
537 isapnp_print(aux, str)
538 	void *aux;
539 	const char *str;
540 {
541 	struct isa_attach_args *ipa = aux;
542 
543 	if (!str)
544 		printf(" ");
545 	printf("\"%s, %s, %s, %s\"", ipa->ipa_devident,
546 	    ipa->ipa_devlogic, ipa->ipa_devcompat, ipa->ipa_devclass);
547 
548 	if (str)
549 		printf(" at %s", str);
550 
551 	isapnp_print_region("port", ipa->ipa_io, ipa->ipa_nio);
552 	isapnp_print_region("mem", ipa->ipa_mem, ipa->ipa_nmem);
553 	isapnp_print_region("mem32", ipa->ipa_mem32, ipa->ipa_nmem32);
554 	isapnp_print_pin("irq", ipa->ipa_irq, ipa->ipa_nirq);
555 	isapnp_print_pin("drq", ipa->ipa_drq, ipa->ipa_ndrq);
556 	return UNCONF;
557 }
558 
559 
560 /* isapnp_submatch():
561  * Special case.
562  * A lot of com devices do not have the PNPxxx identifiers
563  * they should have.  If it looks like a modem..... let's try it.
564  */
565 int
566 isapnp_com_submatch(parent, match, aux)
567 	struct device *parent;
568 	void *match, *aux;
569 {
570 	struct cfdata *cf = match;
571 	struct isa_attach_args *ipa = aux;
572 
573 	if (strcmp("com", cf->cf_driver->cd_name) == 0 &&
574 	    ipa->ipa_nio == 1 && ipa->ipa_nirq == 1 &&
575 	    ipa->ipa_ndrq == 0 && ipa->ipa_nmem == 0 &&
576 	    ipa->ipa_io[0].length == 8) {
577 		if (isapnp_config(ipa->ia_iot, ipa->ia_memt, ipa)) {
578 			printf("%s: error in region allocation\n",
579 			    cf->cf_driver->cd_name);
580 			return (0);
581 		}
582 		return ((*cf->cf_attach->ca_match)(parent, match, ipa));
583 	}
584 	return (0);
585 }
586 
587 /* isapnp_submatch():
588  *	Probe the logical device...
589  */
590 int
591 isapnp_submatch(parent, match, aux)
592 	struct device *parent;
593 	void *match, *aux;
594 {
595 	struct cfdata *cf = match;
596 	struct isa_attach_args *ipa = aux;
597 	const char *dname;
598 	int i;
599 
600 	for (i = 0; i < nitems(isapnp_knowndevs); i++) {
601 		dname = NULL;
602 
603 		if (strcmp(isapnp_knowndevs[i].pnpid, ipa->ipa_devlogic) == 0)
604 			dname = isapnp_knowndevs[i].driver;
605 		else if (strcmp(isapnp_knowndevs[i].pnpid, ipa->ipa_devcompat) == 0)
606 			dname = isapnp_knowndevs[i].driver;
607 
608 		if (dname && strcmp(dname, cf->cf_driver->cd_name) == 0) {
609 			/*
610 			 * We found a match.  Configure the card and call the
611 			 * ISA probe...
612 			 */
613 			if (isapnp_config(ipa->ia_iot, ipa->ia_memt, ipa)) {
614 				printf("%s: error in region allocation\n",
615 				    cf->cf_driver->cd_name);
616 				return (0);
617 			}
618 
619 			return ((*cf->cf_attach->ca_match)(parent, match, ipa));
620 		}
621 	}
622 
623 	return (0);
624 }
625 
626 /* isapnp_find():
627  *	Probe and add cards
628  */
629 int
630 isapnp_find(sc, all)
631 	struct isapnp_softc *sc;
632 	int all;
633 {
634 	int p;
635 
636 	isapnp_init(sc);
637 
638 	isapnp_write_reg(sc, ISAPNP_CONFIG_CONTROL, ISAPNP_CC_RESET_DRV);
639 	DELAY(2000);
640 
641 	isapnp_init(sc);
642 	DELAY(2000);
643 
644 	for (p = ISAPNP_RDDATA_MIN; p <= ISAPNP_RDDATA_MAX; p += 4) {
645 		sc->sc_read_port = p;
646 		if (isapnp_map_readport(sc))
647 			continue;
648 		DPRINTF(("%s: Trying port %x\n", sc->sc_dev.dv_xname, p));
649 		if (isapnp_findcard(sc))
650 			break;
651 		isapnp_unmap_readport(sc);
652 	}
653 
654 	if (p > ISAPNP_RDDATA_MAX) {
655 		sc->sc_read_port = 0;
656 		return 0;
657 	}
658 
659 	if (all)
660 		while (isapnp_findcard(sc))
661 			continue;
662 
663 	return 1;
664 }
665 
666 
667 /* isapnp_configure():
668  *	Configure a PnP card
669  *	XXX: The memory configuration code is wrong. We need to check the
670  *	     range/length bit an do appropriate sets.
671  */
672 void
673 isapnp_configure(sc, ipa)
674 	struct isapnp_softc *sc;
675 	const struct isa_attach_args *ipa;
676 {
677 	int i;
678 	static u_char isapnp_mem_range[] = ISAPNP_MEM_DESC;
679 	static u_char isapnp_io_range[] = ISAPNP_IO_DESC;
680 	static u_char isapnp_irq_range[] = ISAPNP_IRQ_DESC;
681 	static u_char isapnp_drq_range[] = ISAPNP_DRQ_DESC;
682 	static u_char isapnp_mem32_range[] = ISAPNP_MEM32_DESC;
683 	const struct isapnp_region *r;
684 	const struct isapnp_pin *p;
685 	struct isapnp_region rz;
686 	struct isapnp_pin pz;
687 
688 	bzero(&pz, sizeof(pz));
689 	bzero(&rz, sizeof(rz));
690 
691 #define B0(a) ((a) & 0xff)
692 #define B1(a) (((a) >> 8) & 0xff)
693 #define B2(a) (((a) >> 16) & 0xff)
694 #define B3(a) (((a) >> 24) & 0xff)
695 
696 	for (i = 0; i < sizeof(isapnp_io_range); i++) {
697 		if (i < ipa->ipa_nio)
698 			r = &ipa->ipa_io[i];
699 		else
700 			r = &rz;
701 
702 		isapnp_write_reg(sc,
703 		    isapnp_io_range[i] + ISAPNP_IO_BASE_15_8, B1(r->base));
704 		isapnp_write_reg(sc,
705 		    isapnp_io_range[i] + ISAPNP_IO_BASE_7_0, B0(r->base));
706 	}
707 
708 	for (i = 0; i < sizeof(isapnp_mem_range); i++) {
709 		if (i < ipa->ipa_nmem)
710 			r = &ipa->ipa_mem[i];
711 		else
712 			r = &rz;
713 
714 		isapnp_write_reg(sc,
715 		    isapnp_mem_range[i] + ISAPNP_MEM_BASE_23_16, B2(r->base));
716 		isapnp_write_reg(sc,
717 		    isapnp_mem_range[i] + ISAPNP_MEM_BASE_15_8, B1(r->base));
718 
719 		isapnp_write_reg(sc,
720 		    isapnp_mem_range[i] + ISAPNP_MEM_LRANGE_23_16,
721 		    B2(r->length));
722 		isapnp_write_reg(sc,
723 		    isapnp_mem_range[i] + ISAPNP_MEM_LRANGE_15_8,
724 		    B1(r->length));
725 	}
726 
727 	for (i = 0; i < sizeof(isapnp_irq_range); i++) {
728 		u_char v;
729 
730 		if (i < ipa->ipa_nirq)
731 			p = &ipa->ipa_irq[i];
732 		else
733 			p = &pz;
734 
735 		isapnp_write_reg(sc,
736 		    isapnp_irq_range[i] + ISAPNP_IRQ_NUMBER, p->num);
737 
738 		switch (p->flags) {
739 		case ISAPNP_IRQTYPE_LEVEL_PLUS:
740 			v = ISAPNP_IRQ_LEVEL|ISAPNP_IRQ_HIGH;
741 			break;
742 
743 		case ISAPNP_IRQTYPE_EDGE_PLUS:
744 			v = ISAPNP_IRQ_HIGH;
745 			break;
746 
747 		case ISAPNP_IRQTYPE_LEVEL_MINUS:
748 			v = ISAPNP_IRQ_LEVEL;
749 			break;
750 
751 		default:
752 		case ISAPNP_IRQTYPE_EDGE_MINUS:
753 			v = 0;
754 			break;
755 		}
756 		isapnp_write_reg(sc,
757 		    isapnp_irq_range[i] + ISAPNP_IRQ_CONTROL, v);
758 	}
759 
760 	for (i = 0; i < sizeof(isapnp_drq_range); i++) {
761 		u_char v;
762 
763 		if (i < ipa->ipa_ndrq)
764 			v = ipa->ipa_drq[i].num;
765 		else
766 			v = 4;
767 
768 		isapnp_write_reg(sc, isapnp_drq_range[i], v);
769 	}
770 
771 	for (i = 0; i < sizeof(isapnp_mem32_range); i++) {
772 		if (i < ipa->ipa_nmem32)
773 			r = &ipa->ipa_mem32[i];
774 		else
775 			r = &rz;
776 
777 		isapnp_write_reg(sc,
778 		    isapnp_mem32_range[i] + ISAPNP_MEM32_BASE_31_24,
779 		    B3(r->base));
780 		isapnp_write_reg(sc,
781 		    isapnp_mem32_range[i] + ISAPNP_MEM32_BASE_23_16,
782 		    B2(r->base));
783 		isapnp_write_reg(sc,
784 		    isapnp_mem32_range[i] + ISAPNP_MEM32_BASE_15_8,
785 		    B1(r->base));
786 		isapnp_write_reg(sc,
787 		    isapnp_mem32_range[i] + ISAPNP_MEM32_BASE_7_0,
788 		    B0(r->base));
789 
790 		isapnp_write_reg(sc,
791 		    isapnp_mem32_range[i] + ISAPNP_MEM32_LRANGE_31_24,
792 		    B3(r->length));
793 		isapnp_write_reg(sc,
794 		    isapnp_mem32_range[i] + ISAPNP_MEM32_LRANGE_23_16,
795 		    B2(r->length));
796 		isapnp_write_reg(sc,
797 		    isapnp_mem32_range[i] + ISAPNP_MEM32_LRANGE_15_8,
798 		    B1(r->length));
799 		isapnp_write_reg(sc,
800 		    isapnp_mem32_range[i] + ISAPNP_MEM32_LRANGE_7_0,
801 		    B0(r->length));
802 	}
803 }
804 
805 
806 /*
807  * Some BIOSes take the liberty of configuring our ISA cards for us.
808  * This code undoes the PNP card configuration.
809  */
810 
811 void
812 isapnp_isa_attach_hook(isa_sc)
813 	struct isa_softc *isa_sc;
814 
815 {
816 	struct isapnp_softc sc;
817 
818 	bzero(&sc, sizeof sc);
819 	sc.sc_iot = isa_sc->sc_iot;
820 	sc.sc_ncards = 0;
821 
822 	if (isapnp_map(&sc))
823 		return;
824 
825 	isapnp_init(&sc);
826 
827 	isapnp_write_reg(&sc, ISAPNP_CONFIG_CONTROL, ISAPNP_CC_RESET_DRV);
828 	DELAY(2000);
829 
830 	isapnp_unmap(&sc);
831 }
832 
833 /* isapnp_match():
834  *	Probe routine
835  */
836 int
837 isapnp_match(parent, match, aux)
838 	struct device *parent;
839 	void *match;
840 	void *aux;
841 {
842 	int rv;
843 	struct isapnp_softc sc;
844 	struct isa_attach_args *ia = aux;
845 
846 	sc.sc_iot = ia->ia_iot;
847 	sc.sc_ncards = 0;
848 	(void) strlcpy(sc.sc_dev.dv_xname, "(isapnp probe)",
849 	     sizeof sc.sc_dev.dv_xname);
850 
851 	if (isapnp_map(&sc))
852 		return 0;
853 
854 	rv = isapnp_find(&sc, 0);
855 	ia->ia_iobase = ISAPNP_ADDR;
856 	ia->ia_iosize = 1;
857 
858 	isapnp_unmap(&sc);
859 	if (rv)
860 		isapnp_unmap_readport(&sc);
861 
862 	return (rv);
863 }
864 
865 
866 /* isapnp_attach
867  *	Find and attach PnP cards.
868  */
869 void
870 isapnp_attach(parent, self, aux)
871 	struct device *parent, *self;
872 	void *aux;
873 {
874 	struct isapnp_softc *sc = (struct isapnp_softc *) self;
875 	struct isa_attach_args *ia = aux;
876 	void *match;
877 	int c, d;
878 
879 	sc->sc_iot = ia->ia_iot;
880 	sc->sc_memt = ia->ia_memt;
881 #if NISADMA > 0
882 	sc->sc_dmat = ia->ia_dmat;
883 #endif
884 	sc->sc_ncards = 0;
885 
886 	if (isapnp_map(sc))
887 		panic("%s: bus map failed", sc->sc_dev.dv_xname);
888 
889 	if (!isapnp_find(sc, 1)) {
890 		printf(": no cards found\n");
891 		return;
892 	}
893 
894 	printf(": read port 0x%x\n", sc->sc_read_port);
895 
896 	for (c = 0; c < sc->sc_ncards; c++) {
897 		struct isa_attach_args *ipa, *lpa;
898 
899 		/* Good morning card c */
900 		isapnp_write_reg(sc, ISAPNP_WAKE, c + 1);
901 
902 		if ((ipa = isapnp_get_resource(sc, c, ia)) == NULL)
903 			continue;
904 
905 		DPRINTF(("Selecting attachments\n"));
906 		for (d = 0;
907 		    (lpa = isapnp_bestconfig(parent, sc, &ipa)) != NULL; d++) {
908 			isapnp_write_reg(sc, ISAPNP_LOGICAL_DEV_NUM, d);
909 			isapnp_configure(sc, lpa);
910 #ifdef DEBUG_ISAPNP
911 			{
912 				struct isa_attach_args pa;
913 
914 				isapnp_get_config(sc, &pa);
915 				isapnp_print_config(&pa);
916 			}
917 #endif
918 
919 			DPRINTF(("%s: configuring <%s, %s, %s, %s>\n",
920 			    sc->sc_dev.dv_xname,
921 			    lpa->ipa_devident, lpa->ipa_devlogic,
922 			    lpa->ipa_devcompat, lpa->ipa_devclass));
923 			if (lpa->ipa_pref == ISAPNP_DEP_CONFLICTING) {
924 				isapnp_print(lpa, self->dv_xname);
925 				printf(" resource conflict\n");
926 				free(lpa, M_DEVBUF);
927 				continue;
928 			}
929 
930 			lpa->ia_ic = ia->ia_ic;
931 			lpa->ia_iot = ia->ia_iot;
932 			lpa->ia_memt = ia->ia_memt;
933 #if NISADMA > 0
934 			lpa->ia_dmat = ia->ia_dmat;
935 #endif
936 			lpa->ia_delaybah = ia->ia_delaybah;
937 
938 			isapnp_write_reg(sc, ISAPNP_ACTIVATE, 1);
939 
940 			if ((match = config_search(isapnp_submatch,
941 			    self, lpa)))
942 				config_attach(self, match, lpa, isapnp_print);
943 			else if ((match = config_search(isapnp_com_submatch,
944 			    self, lpa)))
945 				config_attach(self, match, lpa, isapnp_print);
946 			else {
947 				isapnp_print(lpa, self->dv_xname);
948 				printf(" not configured\n");
949 				isapnp_write_reg(sc, ISAPNP_ACTIVATE, 0);
950 			}
951 			free(lpa, M_DEVBUF);
952 		}
953 		isapnp_write_reg(sc, ISAPNP_WAKE, 0);    /* Good night cards */
954 	}
955 }
956