xref: /netbsd-src/sys/dev/isapnp/isapnp.c (revision 8b0f9554ff8762542c4defc4f70e1eb76fb508fa)
1 /*	$NetBSD: isapnp.c,v 1.55 2007/12/09 20:28:02 jmcneill Exp $	*/
2 
3 /*-
4  * Copyright (c) 1996 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Christos Zoulas.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *        This product includes software developed by the NetBSD
21  *        Foundation, Inc. and its contributors.
22  * 4. Neither the name of The NetBSD Foundation nor the names of its
23  *    contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  */
38 
39 /*
40  * ISA PnP bus autoconfiguration.
41  */
42 
43 #include <sys/cdefs.h>
44 __KERNEL_RCSID(0, "$NetBSD: isapnp.c,v 1.55 2007/12/09 20:28:02 jmcneill Exp $");
45 
46 #include "isadma.h"
47 
48 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/device.h>
51 #include <sys/malloc.h>
52 
53 #include <sys/bus.h>
54 
55 #include <dev/isa/isavar.h>
56 
57 #include <dev/isapnp/isapnpreg.h>
58 #include <dev/isapnp/isapnpvar.h>
59 #include <dev/isapnp/isapnpdevs.h>
60 
61 #include "wss_isapnp.h"		/* XXX part of disgusting CS chip hack */
62 
63 #ifndef ISAPNP_ALLOC_INTR_MASK
64 #define ISAPNP_ALLOC_INTR_MASK (~0)
65 #endif
66 
67 static void isapnp_init(struct isapnp_softc *);
68 static inline u_char isapnp_shift_bit(struct isapnp_softc *);
69 static int isapnp_findcard(struct isapnp_softc *);
70 static void isapnp_free_region(bus_space_tag_t, struct isapnp_region *);
71 static int isapnp_alloc_region(bus_space_tag_t, struct isapnp_region *);
72 static int isapnp_alloc_irq(isa_chipset_tag_t, struct isapnp_pin *);
73 static int isapnp_alloc_drq(isa_chipset_tag_t, struct isapnp_pin *);
74 static int isapnp_testconfig(bus_space_tag_t, bus_space_tag_t,
75     struct isapnp_attach_args *, int);
76 static struct isapnp_attach_args *isapnp_bestconfig(struct isapnp_softc *,
77     struct isapnp_attach_args **);
78 static void isapnp_print_region(const char *, struct isapnp_region *, size_t);
79 static void isapnp_configure(struct isapnp_softc *,
80     const struct isapnp_attach_args *);
81 static void isapnp_print_pin(const char *, struct isapnp_pin *, size_t);
82 static int isapnp_print(void *, const char *);
83 #ifdef _KERNEL
84 static int isapnp_submatch(struct device *, struct cfdata *,
85 				const int *, void *);
86 #endif
87 static int isapnp_find(struct isapnp_softc *, int);
88 static int isapnp_match(struct device *, struct cfdata *, void *);
89 static void isapnp_attach(struct device *, struct device *, void *);
90 static void isapnp_callback(struct device *);
91 
92 CFATTACH_DECL(isapnp, sizeof(struct isapnp_softc),
93     isapnp_match, isapnp_attach, NULL, NULL);
94 
95 /*
96  * This keeps track if which ISA's we have been probed on.
97  */
98 struct isapnp_probe_cookie {
99 	LIST_ENTRY(isapnp_probe_cookie)	ipc_link;
100 	struct device *ipc_parent;
101 };
102 LIST_HEAD(, isapnp_probe_cookie) isapnp_probes =
103     LIST_HEAD_INITIALIZER(isapnp_probes);
104 
105 /* isapnp_init():
106  *	Write the PNP initiation key to wake up the cards...
107  */
108 static void
109 isapnp_init(sc)
110 	struct isapnp_softc *sc;
111 {
112 	int i;
113 	u_char v = ISAPNP_LFSR_INIT;
114 
115 	/* First write 0's twice to enter the Wait for Key state */
116 	ISAPNP_WRITE_ADDR(sc, 0);
117 	ISAPNP_WRITE_ADDR(sc, 0);
118 
119 	/* Send the 32 byte sequence to awake the logic */
120 	for (i = 0; i < ISAPNP_LFSR_LENGTH; i++) {
121 		ISAPNP_WRITE_ADDR(sc, v);
122 		v = ISAPNP_LFSR_NEXT(v);
123 	}
124 }
125 
126 
127 /* isapnp_shift_bit():
128  *	Read a bit at a time from the config card.
129  */
130 static inline u_char
131 isapnp_shift_bit(sc)
132 	struct isapnp_softc *sc;
133 {
134 	u_char c1, c2;
135 
136 	DELAY(250);
137 	c1 = ISAPNP_READ_DATA(sc);
138 	DELAY(250);
139 	c2 = ISAPNP_READ_DATA(sc);
140 
141 	if (c1 == 0x55 && c2 == 0xAA)
142 		return 0x80;
143 	else
144 		return 0;
145 }
146 
147 
148 /* isapnp_findcard():
149  *	Attempt to read the vendor/serial/checksum for a card
150  *	If a card is found [the checksum matches], assign the
151  *	next card number to it and return 1
152  */
153 static int
154 isapnp_findcard(sc)
155 	struct isapnp_softc *sc;
156 {
157 	u_char v = ISAPNP_LFSR_INIT, csum, w;
158 	int i, b;
159 
160 	if (sc->sc_ncards == ISAPNP_MAX_CARDS) {
161 		aprint_error("%s: Too many pnp cards\n", sc->sc_dev.dv_xname);
162 		return 0;
163 	}
164 
165 	/* Set the read port */
166 	isapnp_write_reg(sc, ISAPNP_WAKE, 0);
167 	isapnp_write_reg(sc, ISAPNP_SET_RD_PORT, sc->sc_read_port >> 2);
168 	sc->sc_read_port |= 3;
169 	DELAY(1000);
170 
171 	ISAPNP_WRITE_ADDR(sc, ISAPNP_SERIAL_ISOLATION);
172 	DELAY(1000);
173 
174 	/* Read the 8 bytes of the Vendor ID and Serial Number */
175 	for(i = 0; i < 8; i++) {
176 		/* Read each bit separately */
177 		for (w = 0, b = 0; b < 8; b++) {
178 			u_char neg = isapnp_shift_bit(sc);
179 
180 			w >>= 1;
181 			w |= neg;
182 			v = ISAPNP_LFSR_NEXT(v) ^ neg;
183 		}
184 		sc->sc_id[sc->sc_ncards][i] = w;
185 	}
186 
187 	/* Read the remaining checksum byte */
188 	for (csum = 0, b = 0; b < 8; b++) {
189 		u_char neg = isapnp_shift_bit(sc);
190 
191 		csum >>= 1;
192 		csum |= neg;
193 	}
194 	sc->sc_id[sc->sc_ncards][8] = csum;
195 
196 	if (csum == v) {
197 		sc->sc_ncards++;
198 		isapnp_write_reg(sc, ISAPNP_CARD_SELECT_NUM, sc->sc_ncards);
199 		return 1;
200 	}
201 	return 0;
202 }
203 
204 
205 /* isapnp_free_region():
206  *	Free a region
207  */
208 static void
209 isapnp_free_region(t, r)
210 	bus_space_tag_t t;
211 	struct isapnp_region *r;
212 {
213 	if (r->length == 0)
214 		return;
215 
216 #ifdef _KERNEL
217 	bus_space_unmap(t, r->h, r->length);
218 #endif
219 }
220 
221 
222 /* isapnp_alloc_region():
223  *	Allocate a single region if possible
224  */
225 static int
226 isapnp_alloc_region(t, r)
227 	bus_space_tag_t t;
228 	struct isapnp_region *r;
229 {
230 	int error = 0;
231 
232 	if (r->length == 0) {
233 		r->base = 0;
234 		return 0;
235 	}
236 
237 	for (r->base = r->minbase; r->base <= r->maxbase;
238 	     r->base += r->align) {
239 #ifdef _KERNEL
240 		error = bus_space_map(t, r->base, r->length, 0, &r->h);
241 #endif
242 		if (error == 0)
243 			return 0;
244 		if (r->align == 0)
245 			break;
246 	}
247 	return error;
248 }
249 
250 
251 /* isapnp_alloc_irq():
252  *	Allocate an irq
253  */
254 static int
255 isapnp_alloc_irq(ic, i)
256 	isa_chipset_tag_t ic;
257 	struct isapnp_pin *i;
258 {
259 	int irq;
260 #define LEVEL_IRQ (ISAPNP_IRQTYPE_LEVEL_PLUS|ISAPNP_IRQTYPE_LEVEL_MINUS)
261 	i->type = (i->flags & LEVEL_IRQ) ? IST_LEVEL : IST_EDGE;
262 
263 	if (i->bits == 0) {
264 		i->num = 0;
265 		return 0;
266 	}
267 
268 	if (isa_intr_alloc(ic, ISAPNP_ALLOC_INTR_MASK & i->bits,
269 			   i->type, &irq) == 0) {
270 		i->num = irq;
271 		return 0;
272 	}
273 
274 	return EINVAL;
275 }
276 
277 /* isapnp_alloc_drq():
278  *	Allocate a drq
279  */
280 static int
281 isapnp_alloc_drq(ic, i)
282 	isa_chipset_tag_t ic;
283 	struct isapnp_pin *i;
284 {
285 #if NISADMA > 0
286 	int b;
287 
288 	if (i->bits == 0) {
289 		i->num = 0;
290 		return 0;
291 	}
292 
293 	for (b = 0; b < 8; b++)
294 		if ((i->bits & (1 << b)) && isa_drq_isfree(ic, b)) {
295 			i->num = b;
296 			return 0;
297 		}
298 #endif /* NISADMA > 0 */
299 
300 	return EINVAL;
301 }
302 
303 /* isapnp_testconfig():
304  *	Test/Allocate the regions used
305  */
306 static int
307 isapnp_testconfig(iot, memt, ipa, alloc)
308 	bus_space_tag_t iot, memt;
309 	struct isapnp_attach_args *ipa;
310 	int alloc;
311 {
312 	int nio = 0, nmem = 0, nmem32 = 0, nirq = 0, ndrq = 0;
313 	int error = 0;
314 
315 #ifdef DEBUG_ISAPNP
316 	isapnp_print_attach(ipa);
317 #endif
318 
319 	for (; nio < ipa->ipa_nio; nio++) {
320 		error = isapnp_alloc_region(iot, &ipa->ipa_io[nio]);
321 		if (error)
322 			goto bad;
323 	}
324 
325 	for (; nmem < ipa->ipa_nmem; nmem++) {
326 		error = isapnp_alloc_region(memt, &ipa->ipa_mem[nmem]);
327 		if (error)
328 			goto bad;
329 	}
330 
331 	for (; nmem32 < ipa->ipa_nmem32; nmem32++) {
332 		error = isapnp_alloc_region(memt, &ipa->ipa_mem32[nmem32]);
333 		if (error)
334 			goto bad;
335 	}
336 
337 	for (; nirq < ipa->ipa_nirq; nirq++) {
338 		error = isapnp_alloc_irq(ipa->ipa_ic, &ipa->ipa_irq[nirq]);
339 		if (error)
340 			goto bad;
341 	}
342 
343 	for (; ndrq < ipa->ipa_ndrq; ndrq++) {
344 		error = isapnp_alloc_drq(ipa->ipa_ic, &ipa->ipa_drq[ndrq]);
345 		if (error)
346 			goto bad;
347 	}
348 
349 	if (alloc)
350 		return error;
351 
352 bad:
353 #ifdef notyet
354 	for (ndrq--; ndrq >= 0; ndrq--)
355 		isapnp_free_pin(&ipa->ipa_drq[ndrq]);
356 
357 	for (nirq--; nirq >= 0; nirq--)
358 		isapnp_free_pin(&ipa->ipa_irq[nirq]);
359 #endif
360 
361 	for (nmem32--; nmem32 >= 0; nmem32--)
362 		isapnp_free_region(memt, &ipa->ipa_mem32[nmem32]);
363 
364 	for (nmem--; nmem >= 0; nmem--)
365 		isapnp_free_region(memt, &ipa->ipa_mem[nmem]);
366 
367 	for (nio--; nio >= 0; nio--)
368 		isapnp_free_region(iot, &ipa->ipa_io[nio]);
369 
370 	return error;
371 }
372 
373 
374 /* isapnp_config():
375  *	Test/Allocate the regions used
376  */
377 int
378 isapnp_config(iot, memt, ipa)
379 	bus_space_tag_t iot, memt;
380 	struct isapnp_attach_args *ipa;
381 {
382 	return isapnp_testconfig(iot, memt, ipa, 1);
383 }
384 
385 
386 /* isapnp_unconfig():
387  *	Free the regions used
388  */
389 void
390 isapnp_unconfig(iot, memt, ipa)
391 	bus_space_tag_t iot, memt;
392 	struct isapnp_attach_args *ipa;
393 {
394 	int i;
395 
396 #ifdef notyet
397 	for (i = 0; i < ipa->ipa_ndrq; i++)
398 		isapnp_free_pin(&ipa->ipa_drq[i]);
399 
400 	for (i = 0; i < ipa->ipa_nirq; i++)
401 		isapnp_free_pin(&ipa->ipa_irq[i]);
402 #endif
403 
404 	for (i = 0; i < ipa->ipa_nmem32; i++)
405 		isapnp_free_region(memt, &ipa->ipa_mem32[i]);
406 
407 	for (i = 0; i < ipa->ipa_nmem; i++)
408 		isapnp_free_region(memt, &ipa->ipa_mem[i]);
409 
410 	for (i = 0; i < ipa->ipa_nio; i++)
411 		isapnp_free_region(iot, &ipa->ipa_io[i]);
412 }
413 
414 
415 /* isapnp_bestconfig():
416  *	Return the best configuration for each logical device, remove and
417  *	free all other configurations.
418  */
419 static struct isapnp_attach_args *
420 isapnp_bestconfig(sc, ipa)
421 	struct isapnp_softc *sc;
422 	struct isapnp_attach_args **ipa;
423 {
424 	struct isapnp_attach_args *c, *best, *f = *ipa;
425 	int error;
426 
427 	for (;;) {
428 		if (f == NULL)
429 			return NULL;
430 
431 #define SAMEDEV(a, b) (strcmp((a)->ipa_devlogic, (b)->ipa_devlogic) == 0)
432 
433 		/* Find the best config */
434 		for (best = c = f; c != NULL; c = c->ipa_sibling) {
435 			if (!SAMEDEV(c, f))
436 				continue;
437 			if (c->ipa_pref < best->ipa_pref)
438 				best = c;
439 		}
440 
441 		/*
442 		 * Make sure the ISA chipset is initialized!  We need
443 		 * it to test the best config!
444 		 */
445 		best->ipa_ic = sc->sc_ic;
446 
447 		/* Test the best config */
448 		error = isapnp_testconfig(sc->sc_iot, sc->sc_memt, best, 0);
449 
450 		/* Remove this config from the list */
451 		if (best == f)
452 			f = f->ipa_sibling;
453 		else {
454 			for (c = f; c->ipa_sibling != best; c = c->ipa_sibling)
455 				continue;
456 			c->ipa_sibling = best->ipa_sibling;
457 		}
458 
459 		if (error) {
460 			best->ipa_pref = ISAPNP_DEP_CONFLICTING;
461 
462 			for (c = f; c != NULL; c = c->ipa_sibling)
463 				if (c != best && SAMEDEV(c, best))
464 					break;
465 			/* Last config for this logical device is conflicting */
466 			if (c == NULL) {
467 				*ipa = f;
468 				return best;
469 			}
470 
471 			ISAPNP_FREE(best);
472 			continue;
473 		}
474 		else {
475 			/* Remove all other configs for this device */
476 			struct isapnp_attach_args *l = NULL, *n = NULL, *d;
477 
478 			for (c = f; c; ) {
479 				if (c == best)
480 					continue;
481 				d = c->ipa_sibling;
482 				if (SAMEDEV(c, best))
483 					ISAPNP_FREE(c);
484 				else {
485 					if (n)
486 						n->ipa_sibling = c;
487 
488 					else
489 						l = c;
490 					n = c;
491 					c->ipa_sibling = NULL;
492 				}
493 				c = d;
494 			}
495 			f = l;
496 		}
497 		*ipa = f;
498 		return best;
499 	}
500 }
501 
502 
503 /* isapnp_id_to_vendor():
504  *	Convert a pnp ``compressed ascii'' vendor id to a string
505  */
506 char *
507 isapnp_id_to_vendor(v, id)
508 	char   *v;
509 	const u_char *id;
510 {
511 	char *p = v;
512 
513 	*p++ = 'A' + (id[0] >> 2) - 1;
514 	*p++ = 'A' + ((id[0] & 3) << 3) + (id[1] >> 5) - 1;
515 	*p++ = 'A' + (id[1] & 0x1f) - 1;
516 	*p++ = HEXDIGITS[id[2] >> 4];
517 	*p++ = HEXDIGITS[id[2] & 0x0f];
518 	*p++ = HEXDIGITS[id[3] >> 4];
519 	*p++ = HEXDIGITS[id[3] & 0x0f];
520 	*p = '\0';
521 
522 	return v;
523 }
524 
525 
526 /* isapnp_print_region():
527  *	Print a region allocation
528  */
529 static void
530 isapnp_print_region(str, r, n)
531 	const char *str;
532 	struct isapnp_region *r;
533 	size_t n;
534 {
535 	size_t i;
536 
537 	if (n == 0)
538 		return;
539 
540 	aprint_normal(" %s ", str);
541 	for (i = 0; i < n; i++, r++) {
542 		aprint_normal("0x%x", r->base);
543 		if (r->length)
544 			aprint_normal("/%d", r->length);
545 		if (i != n - 1)
546 			aprint_normal(",");
547 	}
548 }
549 
550 
551 /* isapnp_print_pin():
552  *	Print an irq/drq assignment
553  */
554 static void
555 isapnp_print_pin(str, p, n)
556 	const char *str;
557 	struct isapnp_pin *p;
558 	size_t n;
559 {
560 	size_t i;
561 
562 	if (n == 0)
563 		return;
564 
565 	printf(" %s ", str);
566 	for (i = 0; i < n; i++, p++) {
567 		printf("%d", p->num);
568 		if (i != n - 1)
569 			printf(",");
570 	}
571 }
572 
573 
574 /* isapnp_print():
575  *	Print the configuration line for an ISA PnP card.
576  */
577 static int
578 isapnp_print(aux, str)
579 	void *aux;
580 	const char *str;
581 {
582 	struct isapnp_attach_args *ipa = aux;
583 
584 	if (str != NULL)
585 		aprint_normal("%s: <%s, %s, %s, %s>",
586 		    str, ipa->ipa_devident, ipa->ipa_devlogic,
587 		    ipa->ipa_devcompat, ipa->ipa_devclass);
588 
589 	isapnp_print_region("port", ipa->ipa_io, ipa->ipa_nio);
590 	isapnp_print_region("mem", ipa->ipa_mem, ipa->ipa_nmem);
591 	isapnp_print_region("mem32", ipa->ipa_mem32, ipa->ipa_nmem32);
592 	isapnp_print_pin("irq", ipa->ipa_irq, ipa->ipa_nirq);
593 	isapnp_print_pin("drq", ipa->ipa_drq, ipa->ipa_ndrq);
594 
595 	return UNCONF;
596 }
597 
598 
599 #ifdef _KERNEL
600 /* isapnp_submatch():
601  *	Probe the logical device...
602  */
603 static int
604 isapnp_submatch(struct device *parent, struct cfdata *match,
605     const int *ldesc, void *aux)
606 {
607 
608 	return (config_match(parent, match, aux));
609 }
610 
611 
612 /* isapnp_devmatch():
613  *	Match a probed device with the information from the driver
614  */
615 int
616 isapnp_devmatch(ipa, dinfo, variant)
617 	const struct isapnp_attach_args *ipa;
618 	const struct isapnp_devinfo *dinfo;
619 	int *variant;
620 {
621 	const struct isapnp_matchinfo *match;
622 	int n;
623 
624 	for (match = dinfo->devlogic, n = dinfo->nlogic; n--; match++)
625 		if (strcmp(match->name, ipa->ipa_devlogic) == 0) {
626 			*variant = match->variant;
627 			return (1);
628 		}
629 
630 	for (match = dinfo->devcompat, n = dinfo->ncompat; n--; match++)
631 		if (strcmp(match->name, ipa->ipa_devcompat) == 0) {
632 			*variant = match->variant;
633 			return (1);
634 		}
635 
636 	return (0);
637 }
638 
639 
640 /* isapnp_isa_attach_hook():
641  *	This routine is called from the isa attach code and
642  *	is a kludge; we are resetting all the cards here in order
643  *	to undo any card configuration that the bios did for us, in order
644  *	to avoid having the PnP devices match an isa probe. The correct
645  *	way of doing this is to read the PnP BIOS and find the card settings
646  *	from there. Unfortunately it is not as easy as it sounds.
647  */
648 void
649 isapnp_isa_attach_hook(isa_sc)
650 	struct isa_softc *isa_sc;
651 {
652 	struct isapnp_softc sc;
653 
654 	sc.sc_iot = isa_sc->sc_iot;
655 	sc.sc_ncards = 0;
656 
657 	if (isapnp_map(&sc))
658 		return;
659 
660 #if NWSS_ISAPNP > 0
661 	/*
662 	 * XXX XXX
663 	 * This a totally disgusting hack, but I can't figure out another way.
664 	 * It seems that many CS audio chips have a bug (as far as I can
665 	 * understand).  The reset below does not really reset the chip, it
666 	 * remains in a catatonic state and will not respond when probed.
667 	 * The chip can be used both as a WSS and as a SB device, and a
668 	 * single read at the WSS address (0x534) takes it out of this
669 	 * non-responsive state.
670 	 * The read has to happen at this point in time (or earlier) so
671 	 * it cannot be moved to the wss_isapnp.c driver.
672 	 * (BTW, We're not alone in having problems with these chips:
673 	 * Windoze 98 couldn't detect the sound chip on a Dell when I tried.)
674 	 *
675 	 *     Lennart Augustsson <augustss@NetBSD.org>
676 	 *
677 	 * (Implementation from John Kohl <jtk@kolvir.arlington.ma.us>)
678 	 */
679 	{
680 		bus_space_handle_t ioh;
681 		int rv;
682 		if ((rv = bus_space_map(sc.sc_iot, 0x534, 1, 0, &ioh)) == 0) {
683 			DPRINTF(("wss probe kludge\n"));
684 			(void)bus_space_read_1(sc.sc_iot, ioh, 0);
685 			bus_space_unmap(sc.sc_iot, ioh, 1);
686 		} else {
687 			DPRINTF(("wss probe kludge failed to map: %d\n", rv));
688 		}
689 	}
690 #endif
691 
692 	isapnp_init(&sc);
693 
694 	isapnp_write_reg(&sc, ISAPNP_CONFIG_CONTROL, ISAPNP_CC_RESET_DRV);
695 	DELAY(2000);
696 
697 	isapnp_unmap(&sc);
698 }
699 #endif
700 
701 
702 /* isapnp_find():
703  *	Probe and add cards
704  */
705 static int
706 isapnp_find(sc, all)
707 	struct isapnp_softc *sc;
708 	int all;
709 {
710 	int p;
711 
712 	isapnp_init(sc);
713 
714 	isapnp_write_reg(sc, ISAPNP_CONFIG_CONTROL, ISAPNP_CC_RESET_DRV);
715 	DELAY(2000);
716 
717 	isapnp_init(sc);
718 	DELAY(2000);
719 
720 	for (p = ISAPNP_RDDATA_MIN; p <= ISAPNP_RDDATA_MAX; p += 4) {
721 		sc->sc_read_port = p;
722 		if (isapnp_map_readport(sc))
723 			continue;
724 		DPRINTF(("%s: Trying port %x\r", sc->sc_dev.dv_xname, p));
725 		if (isapnp_findcard(sc))
726 			break;
727 		isapnp_unmap_readport(sc);
728 	}
729 
730 	if (p > ISAPNP_RDDATA_MAX) {
731 		sc->sc_read_port = 0;
732 		return 0;
733 	}
734 
735 	if (all)
736 		while (isapnp_findcard(sc))
737 			continue;
738 
739 	return 1;
740 }
741 
742 
743 /* isapnp_configure():
744  *	Configure a PnP card
745  *	XXX: The memory configuration code is wrong. We need to check the
746  *	     range/length bit an do appropriate sets.
747  */
748 static void
749 isapnp_configure(sc, ipa)
750 	struct isapnp_softc *sc;
751 	const struct isapnp_attach_args *ipa;
752 {
753 	int i;
754 	static u_char isapnp_mem_range[] = ISAPNP_MEM_DESC;
755 	static u_char isapnp_io_range[] = ISAPNP_IO_DESC;
756 	static u_char isapnp_irq_range[] = ISAPNP_IRQ_DESC;
757 	static u_char isapnp_drq_range[] = ISAPNP_DRQ_DESC;
758 	static u_char isapnp_mem32_range[] = ISAPNP_MEM32_DESC;
759 	const struct isapnp_region *r;
760 	const struct isapnp_pin *p;
761 	struct isapnp_region rz;
762 	struct isapnp_pin pz;
763 
764 	memset(&pz, 0, sizeof(pz));
765 	memset(&rz, 0, sizeof(rz));
766 
767 #define B0(a) ((a) & 0xff)
768 #define B1(a) (((a) >> 8) & 0xff)
769 #define B2(a) (((a) >> 16) & 0xff)
770 #define B3(a) (((a) >> 24) & 0xff)
771 
772 	for (i = 0; i < sizeof(isapnp_io_range); i++) {
773 		if (i < ipa->ipa_nio)
774 			r = &ipa->ipa_io[i];
775 		else
776 			r = &rz;
777 
778 		isapnp_write_reg(sc,
779 		    isapnp_io_range[i] + ISAPNP_IO_BASE_15_8, B1(r->base));
780 		isapnp_write_reg(sc,
781 		    isapnp_io_range[i] + ISAPNP_IO_BASE_7_0, B0(r->base));
782 	}
783 
784 	for (i = 0; i < sizeof(isapnp_mem_range); i++) {
785 		if (i < ipa->ipa_nmem)
786 			r = &ipa->ipa_mem[i];
787 		else
788 			r = &rz;
789 
790 		isapnp_write_reg(sc,
791 		    isapnp_mem_range[i] + ISAPNP_MEM_BASE_23_16, B2(r->base));
792 		isapnp_write_reg(sc,
793 		    isapnp_mem_range[i] + ISAPNP_MEM_BASE_15_8, B1(r->base));
794 
795 		isapnp_write_reg(sc,
796 		    isapnp_mem_range[i] + ISAPNP_MEM_LRANGE_23_16,
797 		    B2(r->length));
798 		isapnp_write_reg(sc,
799 		    isapnp_mem_range[i] + ISAPNP_MEM_LRANGE_15_8,
800 		    B1(r->length));
801 	}
802 
803 	for (i = 0; i < sizeof(isapnp_irq_range); i++) {
804 		u_char v;
805 
806 		if (i < ipa->ipa_nirq)
807 			p = &ipa->ipa_irq[i];
808 		else
809 			p = &pz;
810 
811 		isapnp_write_reg(sc,
812 		    isapnp_irq_range[i] + ISAPNP_IRQ_NUMBER, p->num);
813 
814 		switch (p->flags) {
815 		case ISAPNP_IRQTYPE_LEVEL_PLUS:
816 			v = ISAPNP_IRQ_LEVEL|ISAPNP_IRQ_HIGH;
817 			break;
818 
819 		case ISAPNP_IRQTYPE_EDGE_PLUS:
820 			v = ISAPNP_IRQ_HIGH;
821 			break;
822 
823 		case ISAPNP_IRQTYPE_LEVEL_MINUS:
824 			v = ISAPNP_IRQ_LEVEL;
825 			break;
826 
827 		default:
828 		case ISAPNP_IRQTYPE_EDGE_MINUS:
829 			v = 0;
830 			break;
831 		}
832 		isapnp_write_reg(sc,
833 		    isapnp_irq_range[i] + ISAPNP_IRQ_CONTROL, v);
834 	}
835 
836 	for (i = 0; i < sizeof(isapnp_drq_range); i++) {
837 		u_char v;
838 
839 		if (i < ipa->ipa_ndrq)
840 			v = ipa->ipa_drq[i].num;
841 		else
842 			v = 4;
843 
844 		isapnp_write_reg(sc, isapnp_drq_range[i], v);
845 	}
846 
847 	for (i = 0; i < sizeof(isapnp_mem32_range); i++) {
848 		if (i < ipa->ipa_nmem32)
849 			r = &ipa->ipa_mem32[i];
850 		else
851 			r = &rz;
852 
853 		isapnp_write_reg(sc,
854 		    isapnp_mem32_range[i] + ISAPNP_MEM32_BASE_31_24,
855 		    B3(r->base));
856 		isapnp_write_reg(sc,
857 		    isapnp_mem32_range[i] + ISAPNP_MEM32_BASE_23_16,
858 		    B2(r->base));
859 		isapnp_write_reg(sc,
860 		    isapnp_mem32_range[i] + ISAPNP_MEM32_BASE_15_8,
861 		    B1(r->base));
862 		isapnp_write_reg(sc,
863 		    isapnp_mem32_range[i] + ISAPNP_MEM32_BASE_7_0,
864 		    B0(r->base));
865 
866 		isapnp_write_reg(sc,
867 		    isapnp_mem32_range[i] + ISAPNP_MEM32_LRANGE_31_24,
868 		    B3(r->length));
869 		isapnp_write_reg(sc,
870 		    isapnp_mem32_range[i] + ISAPNP_MEM32_LRANGE_23_16,
871 		    B2(r->length));
872 		isapnp_write_reg(sc,
873 		    isapnp_mem32_range[i] + ISAPNP_MEM32_LRANGE_15_8,
874 		    B1(r->length));
875 		isapnp_write_reg(sc,
876 		    isapnp_mem32_range[i] + ISAPNP_MEM32_LRANGE_7_0,
877 		    B0(r->length));
878 	}
879 }
880 
881 
882 /* isapnp_match():
883  *	Probe routine
884  */
885 static int
886 isapnp_match(struct device *parent, struct cfdata *match, void *aux)
887 {
888 	struct isapnp_softc sc;
889 	struct isa_attach_args *ia = aux;
890 	struct isapnp_probe_cookie *ipc;
891 
892 	/*
893 	 * Ensure we only probe ISA PnP once; we don't actually consume
894 	 * bus resources, so we have to prevent being cloned forever.
895 	 */
896 	for (ipc = LIST_FIRST(&isapnp_probes); ipc != NULL;
897 	     ipc = LIST_NEXT(ipc, ipc_link))
898 		if (ipc->ipc_parent == parent)
899 			return (0);
900 
901 	ipc = malloc(sizeof(*ipc), M_DEVBUF, M_NOWAIT);
902 	if (ipc == NULL)
903 		panic("isapnp_match: can't allocate probe cookie");
904 
905 	ipc->ipc_parent = parent;
906 	LIST_INSERT_HEAD(&isapnp_probes, ipc, ipc_link);
907 
908 	sc.sc_iot = ia->ia_iot;
909 	(void) strcpy(sc.sc_dev.dv_xname, "(isapnp probe)");
910 
911 	if (isapnp_map(&sc))
912 		return 0;
913 
914 	isapnp_unmap(&sc);
915 
916 	/*
917 	 * We always match.  We must let all legacy ISA devices map
918 	 * their address spaces before we look for a read port.
919 	 */
920 	ia->ia_io[0].ir_addr = ISAPNP_ADDR;
921 	ia->ia_io[0].ir_size = 1;
922 
923 	ia->ia_niomem = 0;
924 	ia->ia_nirq = 0;
925 	ia->ia_ndrq = 0;
926 
927 	return (1);
928 }
929 
930 
931 /* isapnp_attach
932  *	Attach the PnP `bus'.
933  */
934 static void
935 isapnp_attach(struct device *parent, struct device *self, void *aux)
936 {
937 	struct isapnp_softc *sc = device_private(self);
938 	struct isa_attach_args *ia = aux;
939 
940 	sc->sc_iot = ia->ia_iot;
941 	sc->sc_memt = ia->ia_memt;
942 	sc->sc_ic = ia->ia_ic;
943 	sc->sc_dmat = ia->ia_dmat;
944 	sc->sc_ncards = 0;
945 
946 	aprint_normal(": ISA Plug 'n Play device support\n");
947 
948 	if (isapnp_map(sc)) {
949 		aprint_error("%s: unable to map PnP register\n",
950 		    sc->sc_dev.dv_xname);
951 		return;
952 	}
953 
954 #ifdef _KERNEL
955 	/*
956 	 * Defer configuration until the rest of the ISA devices have
957 	 * attached themselves.
958 	 */
959 	config_defer(self, isapnp_callback);
960 #else
961 	isapnp_callback(self);
962 #endif
963 
964 	if (!pmf_device_register(self, NULL, NULL))
965 		aprint_error_dev(self, "couldn't establish power handler\n");
966 }
967 
968 /* isapnp_callback
969  *	Find and attach PnP cards.
970  */
971 void
972 isapnp_callback(self)
973 	struct device *self;
974 {
975 	struct isapnp_softc *sc = device_private(self);
976 	struct isapnp_attach_args *ipa, *lpa;
977 	int c, d;
978 
979 	/*
980 	 * Look for cards.  If none are found, we say so and just return.
981 	 */
982 	if (isapnp_find(sc, 1) == 0) {
983 		aprint_verbose("%s: no ISA Plug 'n Play devices found\n",
984 		    sc->sc_dev.dv_xname);
985 		return;
986 	}
987 
988 	aprint_verbose("%s: read port 0x%x\n", sc->sc_dev.dv_xname, sc->sc_read_port);
989 
990 	/*
991 	 * Now configure all of the cards.
992 	 */
993 	for (c = 0; c < sc->sc_ncards; c++) {
994 		/* Good morning card c */
995 		isapnp_write_reg(sc, ISAPNP_WAKE, c + 1);
996 
997 		if ((ipa = isapnp_get_resource(sc, c)) == NULL)
998 			continue;
999 
1000 		DPRINTF(("Selecting attachments\n"));
1001 		for (d = 0;
1002 		    (lpa = isapnp_bestconfig(sc, &ipa)) != NULL; d++) {
1003 			isapnp_write_reg(sc, ISAPNP_LOGICAL_DEV_NUM, d);
1004 			isapnp_configure(sc, lpa);
1005 #ifdef DEBUG_ISAPNP
1006 			{
1007 				struct isapnp_attach_args pa;
1008 
1009 				isapnp_get_config(sc, &pa);
1010 				isapnp_print_config(&pa);
1011 			}
1012 #endif
1013 
1014 			DPRINTF(("%s: configuring <%s, %s, %s, %s>\n",
1015 			    sc->sc_dev.dv_xname,
1016 			    lpa->ipa_devident, lpa->ipa_devlogic,
1017 			    lpa->ipa_devcompat, lpa->ipa_devclass));
1018 			if (lpa->ipa_pref == ISAPNP_DEP_CONFLICTING) {
1019 				aprint_verbose("%s: <%s, %s, %s, %s> ignored; %s\n",
1020 				    sc->sc_dev.dv_xname,
1021 				    lpa->ipa_devident, lpa->ipa_devlogic,
1022 				    lpa->ipa_devcompat, lpa->ipa_devclass,
1023 				    "resource conflict");
1024 				ISAPNP_FREE(lpa);
1025 				continue;
1026 			}
1027 
1028 			lpa->ipa_ic = sc->sc_ic;
1029 			lpa->ipa_iot = sc->sc_iot;
1030 			lpa->ipa_memt = sc->sc_memt;
1031 			lpa->ipa_dmat = sc->sc_dmat;
1032 
1033 			isapnp_write_reg(sc, ISAPNP_ACTIVATE, 1);
1034 #ifdef _KERNEL
1035 			if (config_found_sm_loc(self, "isapnp", NULL, lpa,
1036 			    isapnp_print, isapnp_submatch) == NULL)
1037 				isapnp_write_reg(sc, ISAPNP_ACTIVATE, 0);
1038 #else
1039 			isapnp_print(lpa, NULL);
1040 			aprint_verbose("\n");
1041 #endif
1042 			ISAPNP_FREE(lpa);
1043 		}
1044 		isapnp_write_reg(sc, ISAPNP_WAKE, 0);    /* Good night cards */
1045 	}
1046 }
1047