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