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