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