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