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