xref: /openbsd/sys/dev/isa/isapnp.c (revision f2dfb0a4)
1 /*	$OpenBSD: isapnp.c,v 1.18 1998/06/02 18:23:33 deraadt 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 void isapnp_init __P((struct isapnp_softc *));
52 static __inline u_char isapnp_shift_bit __P((struct isapnp_softc *));
53 int isapnp_findcard __P((struct isapnp_softc *));
54 void isapnp_free_region __P((bus_space_tag_t, struct isapnp_region *));
55 int isapnp_alloc_region __P((bus_space_tag_t, struct isapnp_region *));
56 int isapnp_alloc_irq __P((isa_chipset_tag_t, struct isapnp_pin *));
57 int isapnp_alloc_drq __P((struct device *, struct isapnp_pin *));
58 int isapnp_testconfig __P((bus_space_tag_t, bus_space_tag_t,
59     struct isa_attach_args *, int));
60 struct isa_attach_args *isapnp_bestconfig __P((struct device *,
61     struct isapnp_softc *, struct isa_attach_args **));
62 void isapnp_print_region __P((const char *, struct isapnp_region *,
63     size_t));
64 void isapnp_configure __P((struct isapnp_softc *,
65     const struct isa_attach_args *));
66 void isapnp_print_pin __P((const char *, struct isapnp_pin *, size_t));
67 int isapnp_print __P((void *, const char *));
68 #ifdef _KERNEL
69 int isapnp_submatch __P((struct device *, void *, void *));
70 #endif
71 int isapnp_find __P((struct isapnp_softc *, int));
72 int isapnp_match __P((struct device *, void *, void *));
73 void isapnp_attach __P((struct device *, struct device *, void *));
74 
75 #ifdef DEBUG_ISAPNP
76 # define DPRINTF(a) printf a
77 #else
78 # define DPRINTF(a)
79 #endif
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 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 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 void
194 isapnp_free_region(t, r)
195 	bus_space_tag_t t;
196 	struct isapnp_region *r;
197 {
198 	if (r->length == 0)
199 		return;
200 
201 #ifdef _KERNEL
202 	bus_space_unmap(t, r->h, r->length);
203 #endif
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 	for (r->base = r->minbase; r->base <= r->maxbase;
221 	     r->base += r->align) {
222 #ifdef _KERNEL
223 		error = bus_space_map(t, r->base, r->length, 0, &r->h);
224 #endif
225 		if (error == 0)
226 			return 0;
227 	}
228 	return error;
229 }
230 
231 
232 /* isapnp_alloc_irq():
233  *	Allocate an irq
234  */
235 int
236 isapnp_alloc_irq(ic, i)
237 	isa_chipset_tag_t ic;
238 	struct isapnp_pin *i;
239 {
240 	int irq;
241 #define LEVEL_IRQ (ISAPNP_IRQTYPE_LEVEL_PLUS|ISAPNP_IRQTYPE_LEVEL_MINUS)
242 	i->type = (i->flags & LEVEL_IRQ) ? IST_LEVEL : IST_EDGE;
243 
244 	if (i->bits == 0) {
245 		i->num = 0;
246 		return 0;
247 	}
248 
249 	if (isa_intr_alloc(ic, i->bits, i->type, &irq) == 0) {
250 		i->num = irq;
251 		return 0;
252 	}
253 
254 	return EINVAL;
255 }
256 
257 /* isapnp_alloc_drq():
258  *	Allocate a drq
259  */
260 int
261 isapnp_alloc_drq(isa, i)
262 	struct device *isa;
263 	struct isapnp_pin *i;
264 {
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 
278 	return EINVAL;
279 }
280 
281 /* isapnp_testconfig():
282  *	Test/Allocate the regions used
283  */
284 int
285 isapnp_testconfig(iot, memt, ipa, alloc)
286 	bus_space_tag_t iot, memt;
287 	struct isa_attach_args *ipa;
288 	int alloc;
289 {
290 	int nio = 0, nmem = 0, nmem32 = 0, nirq = 0, ndrq = 0;
291 	int error = 0;
292 
293 #ifdef DEBUG_ISAPNP
294 	isapnp_print_attach(ipa);
295 #endif
296 
297 	for (; nio < ipa->ipa_nio; nio++) {
298 		error = isapnp_alloc_region(iot, &ipa->ipa_io[nio]);
299 		if (error)
300 			goto bad;
301 	}
302 
303 	for (; nmem < ipa->ipa_nmem; nmem++) {
304 		error = isapnp_alloc_region(memt, &ipa->ipa_mem[nmem]);
305 		if (error)
306 			goto bad;
307 	}
308 
309 	for (; nmem32 < ipa->ipa_nmem32; nmem32++) {
310 		error = isapnp_alloc_region(memt, &ipa->ipa_mem32[nmem32]);
311 		if (error)
312 			goto bad;
313 	}
314 
315 	for (; nirq < ipa->ipa_nirq; nirq++) {
316 		error = isapnp_alloc_irq(ipa->ia_ic, &ipa->ipa_irq[nirq]);
317 		if (error)
318 			goto bad;
319 	}
320 
321 	for (; ndrq < ipa->ipa_ndrq; ndrq++) {
322 		error = isapnp_alloc_drq(ipa->ia_isa, &ipa->ipa_drq[ndrq]);
323 		if (error)
324 			goto bad;
325 	}
326 
327 	if (alloc)
328 		return error;
329 
330 bad:
331 #ifdef notyet
332 	for (ndrq--; ndrq >= 0; ndrq--)
333 		isapnp_free_pin(&ipa->ipa_drq[ndrq]);
334 
335 	for (nirq--; nirq >= 0; nirq--)
336 		isapnp_free_pin(&ipa->ipa_irq[nirq]);
337 #endif
338 
339 	for (nmem32--; nmem32 >= 0; nmem32--)
340 		isapnp_free_region(memt, &ipa->ipa_mem32[nmem32]);
341 
342 	for (nmem--; nmem >= 0; nmem--)
343 		isapnp_free_region(memt, &ipa->ipa_mem[nmem]);
344 
345 	for (nio--; nio >= 0; nio--)
346 		isapnp_free_region(iot, &ipa->ipa_io[nio]);
347 
348 	return error;
349 }
350 
351 
352 /* isapnp_config():
353  *	Test/Allocate the regions used
354  */
355 int
356 isapnp_config(iot, memt, ipa)
357 	bus_space_tag_t iot, memt;
358 	struct isa_attach_args *ipa;
359 {
360 	return isapnp_testconfig(iot, memt, ipa, 1);
361 }
362 
363 
364 /* isapnp_unconfig():
365  *	Free the regions used
366  */
367 void
368 isapnp_unconfig(iot, memt, ipa)
369 	bus_space_tag_t iot, memt;
370 	struct isa_attach_args *ipa;
371 {
372 	int i;
373 
374 #ifdef notyet
375 	for (i = 0; i < ipa->ipa_ndrq; i++)
376 		isapnp_free_pin(&ipa->ipa_drq[i]);
377 
378 	for (i = 0; i < ipa->ipa_nirq; i++)
379 		isapnp_free_pin(&ipa->ipa_irq[i]);
380 #endif
381 
382 	for (i = 0; i < ipa->ipa_nmem32; i++)
383 		isapnp_free_region(memt, &ipa->ipa_mem32[i]);
384 
385 	for (i = 0; i < ipa->ipa_nmem; i++)
386 		isapnp_free_region(memt, &ipa->ipa_mem[i]);
387 
388 	for (i = 0; i < ipa->ipa_nio; i++)
389 		isapnp_free_region(iot, &ipa->ipa_io[i]);
390 }
391 
392 
393 /* isapnp_bestconfig():
394  *	Return the best configuration for each logical device, remove and
395  *	free all other configurations.
396  */
397 struct isa_attach_args *
398 isapnp_bestconfig(isa, sc, ipa)
399 	struct device *isa;
400 	struct isapnp_softc *sc;
401 	struct isa_attach_args **ipa;
402 {
403 	struct isa_attach_args *c, *best, *f = *ipa;
404 	int error;
405 
406 	for (;;) {
407 		if (f == NULL)
408 			return NULL;
409 
410 #define SAMEDEV(a, b) (strcmp((a)->ipa_devlogic, (b)->ipa_devlogic) == 0)
411 
412 		/* Find the best config */
413 		for (best = c = f; c != NULL; c = c->ipa_sibling) {
414 			if (!SAMEDEV(c, f))
415 				continue;
416 			if (c->ipa_pref < best->ipa_pref)
417 				best = c;
418 		}
419 
420 		best->ia_isa = isa;
421 		/* Test the best config */
422 		error = isapnp_testconfig(sc->sc_iot, sc->sc_memt, best, 0);
423 
424 		/* Remove this config from the list */
425 		if (best == f)
426 			f = f->ipa_sibling;
427 		else {
428 			for (c = f; c->ipa_sibling != best; c = c->ipa_sibling)
429 				continue;
430 			c->ipa_sibling = best->ipa_sibling;
431 		}
432 
433 		if (error) {
434 			best->ipa_pref = ISAPNP_DEP_CONFLICTING;
435 
436 			for (c = f; c != NULL; c = c->ipa_sibling)
437 				if (c != best && SAMEDEV(c, best))
438 					break;
439 			/* Last config for this logical device is conflicting */
440 			if (c == NULL) {
441 				*ipa = f;
442 				return best;
443 			}
444 
445 			ISAPNP_FREE(best);
446 			continue;
447 		}
448 		else {
449 			/* Remove all other configs for this device */
450 			struct isa_attach_args *l = NULL, *n = NULL, *d;
451 
452 			for (c = f; c; ) {
453 				if (c == best)
454 					continue;
455 				d = c->ipa_sibling;
456 				if (SAMEDEV(c, best))
457 					ISAPNP_FREE(c);
458 				else {
459 					if (n)
460 						n->ipa_sibling = c;
461 
462 					else
463 						l = c;
464 					n = c;
465 					c->ipa_sibling = NULL;
466 				}
467 				c = d;
468 			}
469 			f = l;
470 		}
471 		*ipa = f;
472 		return best;
473 	}
474 }
475 
476 
477 /* isapnp_id_to_vendor():
478  *	Convert a pnp ``compressed ascii'' vendor id to a string
479  */
480 char *
481 isapnp_id_to_vendor(v, id)
482 	char   *v;
483 	const u_char *id;
484 {
485 	static const char hex[] = "0123456789ABCDEF";
486 	char *p = v;
487 
488 	*p++ = 'A' + (id[0] >> 2) - 1;
489 	*p++ = 'A' + ((id[0] & 3) << 3) + (id[1] >> 5) - 1;
490 	*p++ = 'A' + (id[1] & 0x1f) - 1;
491 	*p++ = hex[id[2] >> 4];
492 	*p++ = hex[id[2] & 0x0f];
493 	*p++ = hex[id[3] >> 4];
494 	*p++ = hex[id[3] & 0x0f];
495 	*p = '\0';
496 
497 	return v;
498 }
499 
500 
501 /* isapnp_print_region():
502  *	Print a region allocation
503  */
504 void
505 isapnp_print_region(str, r, n)
506 	const char *str;
507 	struct isapnp_region *r;
508 	size_t n;
509 {
510 	size_t i;
511 
512 	if (n == 0)
513 		return;
514 
515 	printf(" %s ", str);
516 	for (i = 0; i < n; i++, r++) {
517 		printf("0x%x", r->base);
518 		if (r->length)
519 			printf("/%d", r->length);
520 		if (i != n - 1)
521 			printf(",");
522 	}
523 }
524 
525 
526 /* isapnp_print_pin():
527  *	Print an irq/drq assignment
528  */
529 void
530 isapnp_print_pin(str, p, n)
531 	const char *str;
532 	struct isapnp_pin *p;
533 	size_t n;
534 {
535 	size_t i;
536 
537 	if (n == 0)
538 		return;
539 
540 	printf(" %s ", str);
541 	for (i = 0; i < n; i++, p++) {
542 		printf("%d", p->num);
543 		if (i != n - 1)
544 			printf(",");
545 	}
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 != NULL)
560 		printf("%s:", str);
561 	printf(" <%s, %s, %s, %s>", ipa->ipa_devident,
562 	    ipa->ipa_devlogic, ipa->ipa_devcompat, ipa->ipa_devclass);
563 
564 	isapnp_print_region("port", ipa->ipa_io, ipa->ipa_nio);
565 	isapnp_print_region("mem", ipa->ipa_mem, ipa->ipa_nmem);
566 	isapnp_print_region("mem32", ipa->ipa_mem32, ipa->ipa_nmem32);
567 	isapnp_print_pin("irq", ipa->ipa_irq, ipa->ipa_nirq);
568 	isapnp_print_pin("drq", ipa->ipa_drq, ipa->ipa_ndrq);
569 	return UNCONF;
570 }
571 
572 
573 #ifdef _KERNEL
574 /* isapnp_submatch():
575  *	Probe the logical device...
576  */
577 int
578 isapnp_submatch(parent, match, aux)
579 	struct device *parent;
580 	void *match, *aux;
581 {
582 	struct cfdata *cf = match;
583 	struct isa_attach_args *ipa = aux;
584 	const char *dname;
585 	int i;
586 
587 	for (i = 0; isapnp_knowndevs[i].pnpid; i++) {
588 		dname = NULL;
589 
590 		if (strcmp(isapnp_knowndevs[i].pnpid, ipa->ipa_devlogic) == 0)
591 			dname = isapnp_knowndevs[i].driver;
592 		else if (strcmp(isapnp_knowndevs[i].pnpid, ipa->ipa_devcompat) == 0)
593 			dname = isapnp_knowndevs[i].driver;
594 
595 		if (dname && strcmp(dname, cf->cf_driver->cd_name) == 0) {
596 			/*
597 			 * We found a match.  Configure the card and call the
598 			 * ISA probe...
599 			 */
600 			if (isapnp_config(ipa->ia_iot, ipa->ia_memt, ipa)) {
601 				printf("%s: error in region allocation\n",
602 				    cf->cf_driver->cd_name);
603 				return (0);
604 			}
605 
606 			return ((*cf->cf_attach->ca_match)(parent, match, ipa));
607 		}
608 	}
609 
610 	return (0);
611 }
612 #endif
613 
614 
615 /* isapnp_find():
616  *	Probe and add cards
617  */
618 int
619 isapnp_find(sc, all)
620 	struct isapnp_softc *sc;
621 	int all;
622 {
623 	int p;
624 
625 	isapnp_init(sc);
626 
627 	isapnp_write_reg(sc, ISAPNP_CONFIG_CONTROL, ISAPNP_CC_RESET_DRV);
628 	DELAY(2000);
629 
630 	isapnp_init(sc);
631 	DELAY(2000);
632 
633 	for (p = ISAPNP_RDDATA_MIN; p <= ISAPNP_RDDATA_MAX; p += 4) {
634 		sc->sc_read_port = p;
635 		if (isapnp_map_readport(sc))
636 			continue;
637 		DPRINTF(("%s: Trying port %x\n", sc->sc_dev.dv_xname, p));
638 		if (isapnp_findcard(sc))
639 			break;
640 		isapnp_unmap_readport(sc);
641 	}
642 
643 	if (p > ISAPNP_RDDATA_MAX) {
644 		sc->sc_read_port = 0;
645 		return 0;
646 	}
647 
648 	if (all)
649 		while (isapnp_findcard(sc))
650 			continue;
651 
652 	return 1;
653 }
654 
655 
656 /* isapnp_configure():
657  *	Configure a PnP card
658  *	XXX: The memory configuration code is wrong. We need to check the
659  *	     range/length bit an do appropriate sets.
660  */
661 void
662 isapnp_configure(sc, ipa)
663 	struct isapnp_softc *sc;
664 	const struct isa_attach_args *ipa;
665 {
666 	int i;
667 	static u_char isapnp_mem_range[] = ISAPNP_MEM_DESC;
668 	static u_char isapnp_io_range[] = ISAPNP_IO_DESC;
669 	static u_char isapnp_irq_range[] = ISAPNP_IRQ_DESC;
670 	static u_char isapnp_drq_range[] = ISAPNP_DRQ_DESC;
671 	static u_char isapnp_mem32_range[] = ISAPNP_MEM32_DESC;
672 	const struct isapnp_region *r;
673 	const struct isapnp_pin *p;
674 	struct isapnp_region rz;
675 	struct isapnp_pin pz;
676 
677 	bzero(&pz, sizeof(pz));
678 	bzero(&rz, sizeof(rz));
679 
680 #define B0(a) ((a) & 0xff)
681 #define B1(a) (((a) >> 8) & 0xff)
682 #define B2(a) (((a) >> 16) & 0xff)
683 #define B3(a) (((a) >> 24) & 0xff)
684 
685 	for (i = 0; i < sizeof(isapnp_io_range); i++) {
686 		if (i < ipa->ipa_nio)
687 			r = &ipa->ipa_io[i];
688 		else
689 			r = &rz;
690 
691 		isapnp_write_reg(sc,
692 		    isapnp_io_range[i] + ISAPNP_IO_BASE_15_8, B1(r->base));
693 		isapnp_write_reg(sc,
694 		    isapnp_io_range[i] + ISAPNP_IO_BASE_7_0, B0(r->base));
695 	}
696 
697 	for (i = 0; i < sizeof(isapnp_mem_range); i++) {
698 		if (i < ipa->ipa_nmem)
699 			r = &ipa->ipa_mem[i];
700 		else
701 			r = &rz;
702 
703 		isapnp_write_reg(sc,
704 		    isapnp_mem_range[i] + ISAPNP_MEM_BASE_23_16, B2(r->base));
705 		isapnp_write_reg(sc,
706 		    isapnp_mem_range[i] + ISAPNP_MEM_BASE_15_8, B1(r->base));
707 
708 		isapnp_write_reg(sc,
709 		    isapnp_mem_range[i] + ISAPNP_MEM_LRANGE_23_16,
710 		    B2(r->length));
711 		isapnp_write_reg(sc,
712 		    isapnp_mem_range[i] + ISAPNP_MEM_LRANGE_15_8,
713 		    B1(r->length));
714 	}
715 
716 	for (i = 0; i < sizeof(isapnp_irq_range); i++) {
717 		u_char v;
718 
719 		if (i < ipa->ipa_nirq)
720 			p = &ipa->ipa_irq[i];
721 		else
722 			p = &pz;
723 
724 		isapnp_write_reg(sc,
725 		    isapnp_irq_range[i] + ISAPNP_IRQ_NUMBER, p->num);
726 
727 		switch (p->flags) {
728 		case ISAPNP_IRQTYPE_LEVEL_PLUS:
729 			v = ISAPNP_IRQ_LEVEL|ISAPNP_IRQ_HIGH;
730 			break;
731 
732 		case ISAPNP_IRQTYPE_EDGE_PLUS:
733 			v = ISAPNP_IRQ_HIGH;
734 			break;
735 
736 		case ISAPNP_IRQTYPE_LEVEL_MINUS:
737 			v = ISAPNP_IRQ_LEVEL;
738 			break;
739 
740 		default:
741 		case ISAPNP_IRQTYPE_EDGE_MINUS:
742 			v = 0;
743 			break;
744 		}
745 		isapnp_write_reg(sc,
746 		    isapnp_irq_range[i] + ISAPNP_IRQ_CONTROL, v);
747 	}
748 
749 	for (i = 0; i < sizeof(isapnp_drq_range); i++) {
750 		u_char v;
751 
752 		if (i < ipa->ipa_ndrq)
753 			v = ipa->ipa_drq[i].num;
754 		else
755 			v = 4;
756 
757 		isapnp_write_reg(sc, isapnp_drq_range[i], v);
758 	}
759 
760 	for (i = 0; i < sizeof(isapnp_mem32_range); i++) {
761 		if (i < ipa->ipa_nmem32)
762 			r = &ipa->ipa_mem32[i];
763 		else
764 			r = &rz;
765 
766 		isapnp_write_reg(sc,
767 		    isapnp_mem32_range[i] + ISAPNP_MEM32_BASE_31_24,
768 		    B3(r->base));
769 		isapnp_write_reg(sc,
770 		    isapnp_mem32_range[i] + ISAPNP_MEM32_BASE_23_16,
771 		    B2(r->base));
772 		isapnp_write_reg(sc,
773 		    isapnp_mem32_range[i] + ISAPNP_MEM32_BASE_15_8,
774 		    B1(r->base));
775 		isapnp_write_reg(sc,
776 		    isapnp_mem32_range[i] + ISAPNP_MEM32_BASE_7_0,
777 		    B0(r->base));
778 
779 		isapnp_write_reg(sc,
780 		    isapnp_mem32_range[i] + ISAPNP_MEM32_LRANGE_31_24,
781 		    B3(r->length));
782 		isapnp_write_reg(sc,
783 		    isapnp_mem32_range[i] + ISAPNP_MEM32_LRANGE_23_16,
784 		    B2(r->length));
785 		isapnp_write_reg(sc,
786 		    isapnp_mem32_range[i] + ISAPNP_MEM32_LRANGE_15_8,
787 		    B1(r->length));
788 		isapnp_write_reg(sc,
789 		    isapnp_mem32_range[i] + ISAPNP_MEM32_LRANGE_7_0,
790 		    B0(r->length));
791 	}
792 }
793 
794 
795 /* isapnp_match():
796  *	Probe routine
797  */
798 int
799 isapnp_match(parent, match, aux)
800 	struct device *parent;
801 	void *match;
802 	void *aux;
803 {
804 	int rv;
805 	struct isapnp_softc sc;
806 	struct isa_attach_args *ia = aux;
807 
808 	sc.sc_iot = ia->ia_iot;
809 	sc.sc_ncards = 0;
810 	(void) strcpy(sc.sc_dev.dv_xname, "(isapnp probe)");
811 
812 	if (isapnp_map(&sc))
813 		return 0;
814 
815 	rv = isapnp_find(&sc, 0);
816 	ia->ia_iobase = ISAPNP_ADDR;
817 	ia->ia_iosize = 1;
818 
819 	isapnp_unmap(&sc);
820 	if (rv)
821 		isapnp_unmap_readport(&sc);
822 
823 	return (rv);
824 }
825 
826 
827 /* isapnp_attach
828  *	Find and attach PnP cards.
829  */
830 void
831 isapnp_attach(parent, self, aux)
832 	struct device *parent, *self;
833 	void *aux;
834 {
835 	struct isapnp_softc *sc = (struct isapnp_softc *) self;
836 	struct isa_attach_args *ia = aux;
837 	int c, d;
838 
839 	sc->sc_iot = ia->ia_iot;
840 	sc->sc_memt = ia->ia_memt;
841 	sc->sc_dmat = ia->ia_dmat;
842 	sc->sc_ncards = 0;
843 
844 	if (isapnp_map(sc))
845 		panic("%s: bus map failed\n", sc->sc_dev.dv_xname);
846 
847 	if (!isapnp_find(sc, 1))
848 		panic("%s: no cards found\n", sc->sc_dev.dv_xname);
849 
850 	printf(": read port 0x%x\n", sc->sc_read_port);
851 
852 	for (c = 0; c < sc->sc_ncards; c++) {
853 		struct isa_attach_args *ipa, *lpa;
854 
855 		/* Good morning card c */
856 		isapnp_write_reg(sc, ISAPNP_WAKE, c + 1);
857 
858 		if ((ipa = isapnp_get_resource(sc, c)) == NULL)
859 			continue;
860 
861 		DPRINTF(("Selecting attachments\n"));
862 		for (d = 0;
863 		    (lpa = isapnp_bestconfig(parent, sc, &ipa)) != NULL; d++) {
864 			isapnp_write_reg(sc, ISAPNP_LOGICAL_DEV_NUM, d);
865 			isapnp_configure(sc, lpa);
866 #ifdef DEBUG_ISAPNP
867 			{
868 				struct isa_attach_args pa;
869 
870 				isapnp_get_config(sc, &pa);
871 				isapnp_print_config(&pa);
872 			}
873 #endif
874 
875 			DPRINTF(("%s: configuring <%s, %s, %s, %s>\n",
876 			    sc->sc_dev.dv_xname,
877 			    lpa->ipa_devident, lpa->ipa_devlogic,
878 			    lpa->ipa_devcompat, lpa->ipa_devclass));
879 			if (lpa->ipa_pref == ISAPNP_DEP_CONFLICTING) {
880 				printf("%s: <%s, %s, %s, %s> ignored; %s\n",
881 				    sc->sc_dev.dv_xname,
882 				    lpa->ipa_devident, lpa->ipa_devlogic,
883 				    lpa->ipa_devcompat, lpa->ipa_devclass,
884 				    "resource conflict");
885 				ISAPNP_FREE(lpa);
886 				continue;
887 			}
888 
889 			lpa->ia_ic = ia->ia_ic;
890 			lpa->ia_iot = ia->ia_iot;
891 			lpa->ia_memt = ia->ia_memt;
892 			lpa->ia_dmat = ia->ia_dmat;
893 			lpa->ia_delaybah = ia->ia_delaybah;
894 
895 			isapnp_write_reg(sc, ISAPNP_ACTIVATE, 1);
896 #ifdef _KERNEL
897 			if (config_found_sm(self, lpa, isapnp_print,
898 			    isapnp_submatch) == NULL)
899 				isapnp_write_reg(sc, ISAPNP_ACTIVATE, 0);
900 #else
901 			isapnp_print(lpa, NULL);
902 			printf("\n");
903 #endif
904 			ISAPNP_FREE(lpa);
905 		}
906 		isapnp_write_reg(sc, ISAPNP_WAKE, 0);    /* Good night cards */
907 	}
908 }
909