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