xref: /openbsd-src/sys/dev/isa/isapnp.c (revision a28daedfc357b214be5c701aa8ba8adb29a7f1c2)
1 /*	$OpenBSD: isapnp.c,v 1.36 2008/05/21 19:20:08 miod 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 = NULL;
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 = NULL;
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 #ifdef notyet
333 	for (ndrq--; ndrq >= 0; ndrq--)
334 		isapnp_free_pin(&ipa->ipa_drq[ndrq]);
335 
336 	for (nirq--; nirq >= 0; nirq--)
337 		isapnp_free_pin(&ipa->ipa_irq[nirq]);
338 #endif
339 
340 	for (nmem32--; nmem32 >= 0; nmem32--)
341 		isapnp_free_region(memt, &ipa->ipa_mem32[nmem32]);
342 
343 	for (nmem--; nmem >= 0; nmem--)
344 		isapnp_free_region(memt, &ipa->ipa_mem[nmem]);
345 
346 	for (nio--; nio >= 0; nio--)
347 		isapnp_free_region(iot, &ipa->ipa_io[nio]);
348 
349 	return error;
350 }
351 
352 
353 /* isapnp_config():
354  *	Test/Allocate the regions used
355  */
356 int
357 isapnp_config(iot, memt, ipa)
358 	bus_space_tag_t iot, memt;
359 	struct isa_attach_args *ipa;
360 {
361 	return isapnp_testconfig(iot, memt, ipa, 1);
362 }
363 
364 
365 /* isapnp_unconfig():
366  *	Free the regions used
367  */
368 void
369 isapnp_unconfig(iot, memt, ipa)
370 	bus_space_tag_t iot, memt;
371 	struct isa_attach_args *ipa;
372 {
373 	int i;
374 
375 #ifdef notyet
376 	for (i = 0; i < ipa->ipa_ndrq; i++)
377 		isapnp_free_pin(&ipa->ipa_drq[i]);
378 
379 	for (i = 0; i < ipa->ipa_nirq; i++)
380 		isapnp_free_pin(&ipa->ipa_irq[i]);
381 #endif
382 
383 	for (i = 0; i < ipa->ipa_nmem32; i++)
384 		isapnp_free_region(memt, &ipa->ipa_mem32[i]);
385 
386 	for (i = 0; i < ipa->ipa_nmem; i++)
387 		isapnp_free_region(memt, &ipa->ipa_mem[i]);
388 
389 	for (i = 0; i < ipa->ipa_nio; i++)
390 		isapnp_free_region(iot, &ipa->ipa_io[i]);
391 }
392 
393 
394 /* isapnp_bestconfig():
395  *	Return the best configuration for each logical device, remove and
396  *	free all other configurations.
397  */
398 struct isa_attach_args *
399 isapnp_bestconfig(isa, sc, ipa)
400 	struct device *isa;
401 	struct isapnp_softc *sc;
402 	struct isa_attach_args **ipa;
403 {
404 	struct isa_attach_args *c, *best, *f = *ipa;
405 	int error;
406 
407 	for (;;) {
408 		if (f == NULL)
409 			return NULL;
410 
411 #define SAMEDEV(a, b) (strcmp((a)->ipa_devlogic, (b)->ipa_devlogic) == 0)
412 
413 		/* Find the best config */
414 		for (best = c = f; c != NULL; c = c->ipa_sibling) {
415 			if (!SAMEDEV(c, f))
416 				continue;
417 			if (c->ipa_pref < best->ipa_pref)
418 				best = c;
419 		}
420 
421 		best->ia_isa = isa;
422 		/* Test the best config */
423 		error = isapnp_testconfig(sc->sc_iot, sc->sc_memt, best, 0);
424 
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;
432 		}
433 
434 		if (error) {
435 			best->ipa_pref = ISAPNP_DEP_CONFLICTING;
436 
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;
444 			}
445 
446 			ISAPNP_FREE(best);
447 			continue;
448 		}
449 		else {
450 			/* Remove all other configs for this device */
451 			struct isa_attach_args *l = NULL, *n = NULL, *d;
452 
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;
462 
463 					else
464 						l = c;
465 					n = c;
466 					c->ipa_sibling = NULL;
467 				}
468 				c = d;
469 			}
470 			f = l;
471 		}
472 		*ipa = f;
473 		return best;
474 	}
475 }
476 
477 
478 /* isapnp_id_to_vendor():
479  *	Convert a pnp ``compressed ascii'' vendor id to a string
480  */
481 char *
482 isapnp_id_to_vendor(v, id)
483 	char   *v;
484 	const u_char *id;
485 {
486 	static const char hex[] = "0123456789ABCDEF";
487 	char *p = v;
488 
489 	*p++ = 'A' + (id[0] >> 2) - 1;
490 	*p++ = 'A' + ((id[0] & 3) << 3) + (id[1] >> 5) - 1;
491 	*p++ = 'A' + (id[1] & 0x1f) - 1;
492 	*p++ = hex[id[2] >> 4];
493 	*p++ = hex[id[2] & 0x0f];
494 	*p++ = hex[id[3] >> 4];
495 	*p++ = hex[id[3] & 0x0f];
496 	*p = '\0';
497 
498 	return v;
499 }
500 
501 
502 /* isapnp_print_region():
503  *	Print a region allocation
504  */
505 void
506 isapnp_print_region(str, r, n)
507 	const char *str;
508 	struct isapnp_region *r;
509 	size_t n;
510 {
511 	size_t i;
512 
513 	if (n == 0)
514 		return;
515 
516 	printf(" %s ", str);
517 	for (i = 0; i < n; i++, r++) {
518 		printf("0x%x", r->base);
519 		if (r->length)
520 			printf("/%d", r->length);
521 		if (i != n - 1)
522 			printf(",");
523 	}
524 }
525 
526 
527 /* isapnp_print_pin():
528  *	Print an irq/drq assignment
529  */
530 void
531 isapnp_print_pin(str, p, n)
532 	const char *str;
533 	struct isapnp_pin *p;
534 	size_t n;
535 {
536 	size_t i;
537 
538 	if (n == 0)
539 		return;
540 
541 	printf(" %s ", str);
542 	for (i = 0; i < n; i++, p++) {
543 		printf("%d", p->num);
544 		if (i != n - 1)
545 			printf(",");
546 	}
547 }
548 
549 /* isapnp_print():
550  *	Print the configuration line for an ISA PnP card.
551  */
552 int
553 isapnp_print(aux, str)
554 	void *aux;
555 	const char *str;
556 {
557 	struct isa_attach_args *ipa = aux;
558 
559 	if (!str)
560 		printf(" ");
561 	printf("\"%s, %s, %s, %s\"", ipa->ipa_devident,
562 	    ipa->ipa_devlogic, ipa->ipa_devcompat, ipa->ipa_devclass);
563 
564 	if (str)
565 		printf(" at %s", str);
566 
567 	isapnp_print_region("port", ipa->ipa_io, ipa->ipa_nio);
568 	isapnp_print_region("mem", ipa->ipa_mem, ipa->ipa_nmem);
569 	isapnp_print_region("mem32", ipa->ipa_mem32, ipa->ipa_nmem32);
570 	isapnp_print_pin("irq", ipa->ipa_irq, ipa->ipa_nirq);
571 	isapnp_print_pin("drq", ipa->ipa_drq, ipa->ipa_ndrq);
572 	return UNCONF;
573 }
574 
575 
576 /* isapnp_submatch():
577  * Special case.
578  * A lot of com devices do not have the PNPxxx identifiers
579  * they should have.  If it looks like a modem..... let's try it.
580  */
581 int
582 isapnp_com_submatch(parent, match, aux)
583 	struct device *parent;
584 	void *match, *aux;
585 {
586 	struct cfdata *cf = match;
587 	struct isa_attach_args *ipa = aux;
588 
589 	if (strcmp("com", cf->cf_driver->cd_name) == 0 &&
590 	    ipa->ipa_nio == 1 && ipa->ipa_nirq == 1 &&
591 	    ipa->ipa_ndrq == 0 && ipa->ipa_nmem == 0 &&
592 	    ipa->ipa_io[0].length == 8) {
593 		if (isapnp_config(ipa->ia_iot, ipa->ia_memt, ipa)) {
594 			printf("%s: error in region allocation\n",
595 			    cf->cf_driver->cd_name);
596 			return (0);
597 		}
598 		return ((*cf->cf_attach->ca_match)(parent, match, ipa));
599 	}
600 	return (0);
601 }
602 
603 /* isapnp_submatch():
604  *	Probe the logical device...
605  */
606 int
607 isapnp_submatch(parent, match, aux)
608 	struct device *parent;
609 	void *match, *aux;
610 {
611 	struct cfdata *cf = match;
612 	struct isa_attach_args *ipa = aux;
613 	const char *dname;
614 	int i;
615 
616 	for (i = 0; i < sizeof(isapnp_knowndevs)/sizeof(isapnp_knowndevs[0]); i++) {
617 		dname = NULL;
618 
619 		if (strcmp(isapnp_knowndevs[i].pnpid, ipa->ipa_devlogic) == 0)
620 			dname = isapnp_knowndevs[i].driver;
621 		else if (strcmp(isapnp_knowndevs[i].pnpid, ipa->ipa_devcompat) == 0)
622 			dname = isapnp_knowndevs[i].driver;
623 
624 		if (dname && strcmp(dname, cf->cf_driver->cd_name) == 0) {
625 			/*
626 			 * We found a match.  Configure the card and call the
627 			 * ISA probe...
628 			 */
629 			if (isapnp_config(ipa->ia_iot, ipa->ia_memt, ipa)) {
630 				printf("%s: error in region allocation\n",
631 				    cf->cf_driver->cd_name);
632 				return (0);
633 			}
634 
635 			return ((*cf->cf_attach->ca_match)(parent, match, ipa));
636 		}
637 	}
638 
639 	return (0);
640 }
641 
642 /* isapnp_find():
643  *	Probe and add cards
644  */
645 int
646 isapnp_find(sc, all)
647 	struct isapnp_softc *sc;
648 	int all;
649 {
650 	int p;
651 
652 	isapnp_init(sc);
653 
654 	isapnp_write_reg(sc, ISAPNP_CONFIG_CONTROL, ISAPNP_CC_RESET_DRV);
655 	DELAY(2000);
656 
657 	isapnp_init(sc);
658 	DELAY(2000);
659 
660 	for (p = ISAPNP_RDDATA_MIN; p <= ISAPNP_RDDATA_MAX; p += 4) {
661 		sc->sc_read_port = p;
662 		if (isapnp_map_readport(sc))
663 			continue;
664 		DPRINTF(("%s: Trying port %x\n", sc->sc_dev.dv_xname, p));
665 		if (isapnp_findcard(sc))
666 			break;
667 		isapnp_unmap_readport(sc);
668 	}
669 
670 	if (p > ISAPNP_RDDATA_MAX) {
671 		sc->sc_read_port = 0;
672 		return 0;
673 	}
674 
675 	if (all)
676 		while (isapnp_findcard(sc))
677 			continue;
678 
679 	return 1;
680 }
681 
682 
683 /* isapnp_configure():
684  *	Configure a PnP card
685  *	XXX: The memory configuration code is wrong. We need to check the
686  *	     range/length bit an do appropriate sets.
687  */
688 void
689 isapnp_configure(sc, ipa)
690 	struct isapnp_softc *sc;
691 	const struct isa_attach_args *ipa;
692 {
693 	int i;
694 	static u_char isapnp_mem_range[] = ISAPNP_MEM_DESC;
695 	static u_char isapnp_io_range[] = ISAPNP_IO_DESC;
696 	static u_char isapnp_irq_range[] = ISAPNP_IRQ_DESC;
697 	static u_char isapnp_drq_range[] = ISAPNP_DRQ_DESC;
698 	static u_char isapnp_mem32_range[] = ISAPNP_MEM32_DESC;
699 	const struct isapnp_region *r;
700 	const struct isapnp_pin *p;
701 	struct isapnp_region rz;
702 	struct isapnp_pin pz;
703 
704 	bzero(&pz, sizeof(pz));
705 	bzero(&rz, sizeof(rz));
706 
707 #define B0(a) ((a) & 0xff)
708 #define B1(a) (((a) >> 8) & 0xff)
709 #define B2(a) (((a) >> 16) & 0xff)
710 #define B3(a) (((a) >> 24) & 0xff)
711 
712 	for (i = 0; i < sizeof(isapnp_io_range); i++) {
713 		if (i < ipa->ipa_nio)
714 			r = &ipa->ipa_io[i];
715 		else
716 			r = &rz;
717 
718 		isapnp_write_reg(sc,
719 		    isapnp_io_range[i] + ISAPNP_IO_BASE_15_8, B1(r->base));
720 		isapnp_write_reg(sc,
721 		    isapnp_io_range[i] + ISAPNP_IO_BASE_7_0, B0(r->base));
722 	}
723 
724 	for (i = 0; i < sizeof(isapnp_mem_range); i++) {
725 		if (i < ipa->ipa_nmem)
726 			r = &ipa->ipa_mem[i];
727 		else
728 			r = &rz;
729 
730 		isapnp_write_reg(sc,
731 		    isapnp_mem_range[i] + ISAPNP_MEM_BASE_23_16, B2(r->base));
732 		isapnp_write_reg(sc,
733 		    isapnp_mem_range[i] + ISAPNP_MEM_BASE_15_8, B1(r->base));
734 
735 		isapnp_write_reg(sc,
736 		    isapnp_mem_range[i] + ISAPNP_MEM_LRANGE_23_16,
737 		    B2(r->length));
738 		isapnp_write_reg(sc,
739 		    isapnp_mem_range[i] + ISAPNP_MEM_LRANGE_15_8,
740 		    B1(r->length));
741 	}
742 
743 	for (i = 0; i < sizeof(isapnp_irq_range); i++) {
744 		u_char v;
745 
746 		if (i < ipa->ipa_nirq)
747 			p = &ipa->ipa_irq[i];
748 		else
749 			p = &pz;
750 
751 		isapnp_write_reg(sc,
752 		    isapnp_irq_range[i] + ISAPNP_IRQ_NUMBER, p->num);
753 
754 		switch (p->flags) {
755 		case ISAPNP_IRQTYPE_LEVEL_PLUS:
756 			v = ISAPNP_IRQ_LEVEL|ISAPNP_IRQ_HIGH;
757 			break;
758 
759 		case ISAPNP_IRQTYPE_EDGE_PLUS:
760 			v = ISAPNP_IRQ_HIGH;
761 			break;
762 
763 		case ISAPNP_IRQTYPE_LEVEL_MINUS:
764 			v = ISAPNP_IRQ_LEVEL;
765 			break;
766 
767 		default:
768 		case ISAPNP_IRQTYPE_EDGE_MINUS:
769 			v = 0;
770 			break;
771 		}
772 		isapnp_write_reg(sc,
773 		    isapnp_irq_range[i] + ISAPNP_IRQ_CONTROL, v);
774 	}
775 
776 	for (i = 0; i < sizeof(isapnp_drq_range); i++) {
777 		u_char v;
778 
779 		if (i < ipa->ipa_ndrq)
780 			v = ipa->ipa_drq[i].num;
781 		else
782 			v = 4;
783 
784 		isapnp_write_reg(sc, isapnp_drq_range[i], v);
785 	}
786 
787 	for (i = 0; i < sizeof(isapnp_mem32_range); i++) {
788 		if (i < ipa->ipa_nmem32)
789 			r = &ipa->ipa_mem32[i];
790 		else
791 			r = &rz;
792 
793 		isapnp_write_reg(sc,
794 		    isapnp_mem32_range[i] + ISAPNP_MEM32_BASE_31_24,
795 		    B3(r->base));
796 		isapnp_write_reg(sc,
797 		    isapnp_mem32_range[i] + ISAPNP_MEM32_BASE_23_16,
798 		    B2(r->base));
799 		isapnp_write_reg(sc,
800 		    isapnp_mem32_range[i] + ISAPNP_MEM32_BASE_15_8,
801 		    B1(r->base));
802 		isapnp_write_reg(sc,
803 		    isapnp_mem32_range[i] + ISAPNP_MEM32_BASE_7_0,
804 		    B0(r->base));
805 
806 		isapnp_write_reg(sc,
807 		    isapnp_mem32_range[i] + ISAPNP_MEM32_LRANGE_31_24,
808 		    B3(r->length));
809 		isapnp_write_reg(sc,
810 		    isapnp_mem32_range[i] + ISAPNP_MEM32_LRANGE_23_16,
811 		    B2(r->length));
812 		isapnp_write_reg(sc,
813 		    isapnp_mem32_range[i] + ISAPNP_MEM32_LRANGE_15_8,
814 		    B1(r->length));
815 		isapnp_write_reg(sc,
816 		    isapnp_mem32_range[i] + ISAPNP_MEM32_LRANGE_7_0,
817 		    B0(r->length));
818 	}
819 }
820 
821 
822 /*
823  * Some BIOSes take the liberty of configuring our ISA cards for us.
824  * This code undoes the PNP card configuration.
825  */
826 
827 void
828 isapnp_isa_attach_hook(isa_sc)
829 	struct isa_softc *isa_sc;
830 
831 {
832 	struct isapnp_softc sc;
833 
834 	bzero(&sc, sizeof sc);
835 	sc.sc_iot = isa_sc->sc_iot;
836 	sc.sc_ncards = 0;
837 
838 	if (isapnp_map(&sc))
839 		return;
840 
841 	isapnp_init(&sc);
842 
843 	isapnp_write_reg(&sc, ISAPNP_CONFIG_CONTROL, ISAPNP_CC_RESET_DRV);
844 	DELAY(2000);
845 
846 	isapnp_unmap(&sc);
847 }
848 
849 /* isapnp_match():
850  *	Probe routine
851  */
852 int
853 isapnp_match(parent, match, aux)
854 	struct device *parent;
855 	void *match;
856 	void *aux;
857 {
858 	int rv;
859 	struct isapnp_softc sc;
860 	struct isa_attach_args *ia = aux;
861 
862 	sc.sc_iot = ia->ia_iot;
863 	sc.sc_ncards = 0;
864 	(void) strlcpy(sc.sc_dev.dv_xname, "(isapnp probe)",
865 	     sizeof sc.sc_dev.dv_xname);
866 
867 	if (isapnp_map(&sc))
868 		return 0;
869 
870 	rv = isapnp_find(&sc, 0);
871 	ia->ia_iobase = ISAPNP_ADDR;
872 	ia->ia_iosize = 1;
873 
874 	isapnp_unmap(&sc);
875 	if (rv)
876 		isapnp_unmap_readport(&sc);
877 
878 	return (rv);
879 }
880 
881 
882 /* isapnp_attach
883  *	Find and attach PnP cards.
884  */
885 void
886 isapnp_attach(parent, self, aux)
887 	struct device *parent, *self;
888 	void *aux;
889 {
890 	struct isapnp_softc *sc = (struct isapnp_softc *) self;
891 	struct isa_attach_args *ia = aux;
892 	void *match;
893 	int c, d;
894 
895 	sc->sc_iot = ia->ia_iot;
896 	sc->sc_memt = ia->ia_memt;
897 #if NISADMA > 0
898 	sc->sc_dmat = ia->ia_dmat;
899 #endif
900 	sc->sc_ncards = 0;
901 
902 	if (isapnp_map(sc))
903 		panic("%s: bus map failed", sc->sc_dev.dv_xname);
904 
905 	if (!isapnp_find(sc, 1)) {
906 		printf(": no cards found\n");
907 		return;
908 	}
909 
910 	printf(": read port 0x%x\n", sc->sc_read_port);
911 
912 	for (c = 0; c < sc->sc_ncards; c++) {
913 		struct isa_attach_args *ipa, *lpa;
914 
915 		/* Good morning card c */
916 		isapnp_write_reg(sc, ISAPNP_WAKE, c + 1);
917 
918 		if ((ipa = isapnp_get_resource(sc, c, ia)) == NULL)
919 			continue;
920 
921 		DPRINTF(("Selecting attachments\n"));
922 		for (d = 0;
923 		    (lpa = isapnp_bestconfig(parent, sc, &ipa)) != NULL; d++) {
924 			isapnp_write_reg(sc, ISAPNP_LOGICAL_DEV_NUM, d);
925 			isapnp_configure(sc, lpa);
926 #ifdef DEBUG_ISAPNP
927 			{
928 				struct isa_attach_args pa;
929 
930 				isapnp_get_config(sc, &pa);
931 				isapnp_print_config(&pa);
932 			}
933 #endif
934 
935 			DPRINTF(("%s: configuring <%s, %s, %s, %s>\n",
936 			    sc->sc_dev.dv_xname,
937 			    lpa->ipa_devident, lpa->ipa_devlogic,
938 			    lpa->ipa_devcompat, lpa->ipa_devclass));
939 			if (lpa->ipa_pref == ISAPNP_DEP_CONFLICTING) {
940 				isapnp_print(lpa, self->dv_xname);
941 				printf(" resource conflict\n");
942 				ISAPNP_FREE(lpa);
943 				continue;
944 			}
945 
946 			lpa->ia_ic = ia->ia_ic;
947 			lpa->ia_iot = ia->ia_iot;
948 			lpa->ia_memt = ia->ia_memt;
949 #if NISADMA > 0
950 			lpa->ia_dmat = ia->ia_dmat;
951 #endif
952 			lpa->ia_delaybah = ia->ia_delaybah;
953 
954 			isapnp_write_reg(sc, ISAPNP_ACTIVATE, 1);
955 
956 			if ((match = config_search(isapnp_submatch,
957 			    self, lpa)))
958 				config_attach(self, match, lpa, isapnp_print);
959 			else if ((match = config_search(isapnp_com_submatch,
960 			    self, lpa)))
961 				config_attach(self, match, lpa, isapnp_print);
962 			else {
963 				isapnp_print(lpa, self->dv_xname);
964 				printf(" not configured\n");
965 				isapnp_write_reg(sc, ISAPNP_ACTIVATE, 0);
966 			}
967 			ISAPNP_FREE(lpa);
968 		}
969 		isapnp_write_reg(sc, ISAPNP_WAKE, 0);    /* Good night cards */
970 	}
971 }
972