Home | History | Annotate | Line # | Download | only in isapnp
isapnp.c revision 1.11
      1 /*	$NetBSD: isapnp.c,v 1.11 1997/10/27 23:42:30 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((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 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  */
    225 static int
    226 isapnp_alloc_irq(ic, i)
    227 	isa_chipset_tag_t ic;
    228 	struct isapnp_pin *i;
    229 {
    230 	int irq;
    231 
    232 	if (i->bits == 0) {
    233 		i->num = 0;
    234 		return 0;
    235 	}
    236 
    237 	/*
    238 	 * XXX Are there any PnP cards with level triggered interrupts?
    239 	 * XXX If so, we'll have to change the interface that configures
    240 	 * XXX the cards to convey that information.
    241 	 */
    242 	if (isa_intr_alloc(ic, i->bits, IST_EDGE, &irq) == 0) {
    243 		i->num = irq;
    244 		return 0;
    245 	}
    246 
    247 	return EINVAL;
    248 }
    249 
    250 /* isapnp_alloc_drq():
    251  *	Allocate a drq
    252  */
    253 static int
    254 isapnp_alloc_drq(isa, i)
    255 	struct device *isa;
    256 	struct isapnp_pin *i;
    257 {
    258 	int b;
    259 
    260 	if (i->bits == 0) {
    261 		i->num = 0;
    262 		return 0;
    263 	}
    264 
    265 	for (b = 0; b < 16; b++)
    266 		if ((i->bits & (1 << b)) && isa_drq_isfree(isa, b)) {
    267 			i->num = b;
    268 			return 0;
    269 		}
    270 
    271 	return EINVAL;
    272 }
    273 
    274 /* isapnp_testconfig():
    275  *	Test/Allocate the regions used
    276  */
    277 static int
    278 isapnp_testconfig(iot, memt, ipa, alloc)
    279 	bus_space_tag_t iot, memt;
    280 	struct isapnp_attach_args *ipa;
    281 	int alloc;
    282 {
    283 	int nio = 0, nmem = 0, nmem32 = 0, nirq = 0, ndrq = 0;
    284 	int error = 0;
    285 
    286 #ifdef DEBUG_ISAPNP
    287 	isapnp_print_attach(ipa);
    288 #endif
    289 
    290 	for (; nio < ipa->ipa_nio; nio++) {
    291 		error = isapnp_alloc_region(iot, &ipa->ipa_io[nio]);
    292 		if (error)
    293 			goto bad;
    294 	}
    295 
    296 	for (; nmem < ipa->ipa_nmem; nmem++) {
    297 		error = isapnp_alloc_region(memt, &ipa->ipa_mem[nmem]);
    298 		if (error)
    299 			goto bad;
    300 	}
    301 
    302 	for (; nmem32 < ipa->ipa_nmem32; nmem32++) {
    303 		error = isapnp_alloc_region(memt, &ipa->ipa_mem32[nmem32]);
    304 		if (error)
    305 			goto bad;
    306 	}
    307 
    308 	for (; nirq < ipa->ipa_nirq; nirq++) {
    309 		error = isapnp_alloc_irq(ipa->ipa_ic, &ipa->ipa_irq[nirq]);
    310 		if (error)
    311 			goto bad;
    312 	}
    313 
    314 	for (; ndrq < ipa->ipa_ndrq; ndrq++) {
    315 		error = isapnp_alloc_drq(ipa->ipa_isa, &ipa->ipa_drq[ndrq]);
    316 		if (error)
    317 			goto bad;
    318 	}
    319 
    320 	if (alloc)
    321 		return error;
    322 
    323 bad:
    324 #ifdef notyet
    325 	for (ndrq--; ndrq >= 0; ndrq--)
    326 		isapnp_free_pin(&ipa->ipa_drq[ndrq]);
    327 
    328 	for (nirq--; nirq >= 0; nirq--)
    329 		isapnp_free_pin(&ipa->ipa_irq[nirq]);
    330 #endif
    331 
    332 	for (nmem32--; nmem32 >= 0; nmem32--)
    333 		isapnp_free_region(memt, &ipa->ipa_mem32[nmem32]);
    334 
    335 	for (nmem--; nmem >= 0; nmem--)
    336 		isapnp_free_region(memt, &ipa->ipa_mem[nmem]);
    337 
    338 	for (nio--; nio >= 0; nio--)
    339 		isapnp_free_region(iot, &ipa->ipa_io[nio]);
    340 
    341 	return error;
    342 }
    343 
    344 
    345 /* isapnp_config():
    346  *	Test/Allocate the regions used
    347  */
    348 int
    349 isapnp_config(iot, memt, ipa)
    350 	bus_space_tag_t iot, memt;
    351 	struct isapnp_attach_args *ipa;
    352 {
    353 	return isapnp_testconfig(iot, memt, ipa, 1);
    354 }
    355 
    356 
    357 /* isapnp_unconfig():
    358  *	Free the regions used
    359  */
    360 void
    361 isapnp_unconfig(iot, memt, ipa)
    362 	bus_space_tag_t iot, memt;
    363 	struct isapnp_attach_args *ipa;
    364 {
    365 	int i;
    366 
    367 #ifdef notyet
    368 	for (i = 0; i < ipa->ipa_ndrq; i++)
    369 		isapnp_free_pin(&ipa->ipa_drq[i]);
    370 
    371 	for (i = 0; i < ipa->ipa_nirq; i++)
    372 		isapnp_free_pin(&ipa->ipa_irq[i]);
    373 #endif
    374 
    375 	for (i = 0; i < ipa->ipa_nmem32; i++)
    376 		isapnp_free_region(memt, &ipa->ipa_mem32[i]);
    377 
    378 	for (i = 0; i < ipa->ipa_nmem; i++)
    379 		isapnp_free_region(memt, &ipa->ipa_mem[i]);
    380 
    381 	for (i = 0; i < ipa->ipa_nio; i++)
    382 		isapnp_free_region(iot, &ipa->ipa_io[i]);
    383 }
    384 
    385 
    386 /* isapnp_bestconfig():
    387  *	Return the best configuration for each logical device, remove and
    388  *	free all other configurations.
    389  */
    390 static struct isapnp_attach_args *
    391 isapnp_bestconfig(isa, sc, ipa)
    392 	struct device *isa;
    393 	struct isapnp_softc *sc;
    394 	struct isapnp_attach_args **ipa;
    395 {
    396 	struct isapnp_attach_args *c, *best, *f = *ipa;
    397 	int error;
    398 
    399 	for (;;) {
    400 		if (f == NULL)
    401 			return NULL;
    402 
    403 #define SAMEDEV(a, b) (strcmp((a)->ipa_devlogic, (b)->ipa_devlogic) == 0)
    404 
    405 		/* Find the best config */
    406 		for (best = c = f; c != NULL; c = c->ipa_sibling) {
    407 			if (!SAMEDEV(c, f))
    408 				continue;
    409 			if (c->ipa_pref < best->ipa_pref)
    410 				best = c;
    411 		}
    412 
    413 		best->ipa_isa = isa;
    414 		/* Test the best config */
    415 		error = isapnp_testconfig(sc->sc_iot, sc->sc_memt, best, 0);
    416 
    417 		/* Remove this config from the list */
    418 		if (best == f)
    419 			f = f->ipa_sibling;
    420 		else {
    421 			for (c = f; c->ipa_sibling != best; c = c->ipa_sibling)
    422 				continue;
    423 			c->ipa_sibling = best->ipa_sibling;
    424 		}
    425 
    426 		if (error) {
    427 			best->ipa_pref = ISAPNP_DEP_CONFLICTING;
    428 
    429 			for (c = f; c != NULL; c = c->ipa_sibling)
    430 				if (c != best && SAMEDEV(c, best))
    431 					break;
    432 			/* Last config for this logical device is conflicting */
    433 			if (c == NULL) {
    434 				*ipa = f;
    435 				return best;
    436 			}
    437 
    438 			ISAPNP_FREE(best);
    439 			continue;
    440 		}
    441 		else {
    442 			/* Remove all other configs for this device */
    443 			struct isapnp_attach_args *l = NULL, *n = NULL, *d;
    444 
    445 			for (c = f; c; ) {
    446 				if (c == best)
    447 					continue;
    448 				d = c->ipa_sibling;
    449 				if (SAMEDEV(c, best))
    450 					ISAPNP_FREE(c);
    451 				else {
    452 					if (n)
    453 						n->ipa_sibling = c;
    454 
    455 					else
    456 						l = c;
    457 					n = c;
    458 					c->ipa_sibling = NULL;
    459 				}
    460 				c = d;
    461 			}
    462 			f = l;
    463 		}
    464 		*ipa = f;
    465 		return best;
    466 	}
    467 }
    468 
    469 
    470 /* isapnp_id_to_vendor():
    471  *	Convert a pnp ``compressed ascii'' vendor id to a string
    472  */
    473 char *
    474 isapnp_id_to_vendor(v, id)
    475 	char   *v;
    476 	const u_char *id;
    477 {
    478 	static const char hex[] = "0123456789ABCDEF";
    479 	char *p = v;
    480 
    481 	*p++ = 'A' + (id[0] >> 2) - 1;
    482 	*p++ = 'A' + ((id[0] & 3) << 3) + (id[1] >> 5) - 1;
    483 	*p++ = 'A' + (id[1] & 0x1f) - 1;
    484 	*p++ = hex[id[2] >> 4];
    485 	*p++ = hex[id[2] & 0x0f];
    486 	*p++ = hex[id[3] >> 4];
    487 	*p++ = hex[id[3] & 0x0f];
    488 	*p = '\0';
    489 
    490 	return v;
    491 }
    492 
    493 
    494 /* isapnp_print_region():
    495  *	Print a region allocation
    496  */
    497 static void
    498 isapnp_print_region(str, r, n)
    499 	const char *str;
    500 	struct isapnp_region *r;
    501 	size_t n;
    502 {
    503 	size_t i;
    504 
    505 	if (n == 0)
    506 		return;
    507 
    508 	printf(" %s ", str);
    509 	for (i = 0; i < n; i++, r++) {
    510 		printf("0x%x", r->base);
    511 		if (r->length)
    512 			printf("/%d", r->length);
    513 		if (i != n - 1)
    514 			printf(",");
    515 	}
    516 }
    517 
    518 
    519 /* isapnp_print_pin():
    520  *	Print an irq/drq assignment
    521  */
    522 static void
    523 isapnp_print_pin(str, p, n)
    524 	const char *str;
    525 	struct isapnp_pin *p;
    526 	size_t n;
    527 {
    528 	size_t i;
    529 
    530 	if (n == 0)
    531 		return;
    532 
    533 	printf(" %s ", str);
    534 	for (i = 0; i < n; i++, p++) {
    535 		printf("%d", p->num);
    536 		if (i != n - 1)
    537 			printf(",");
    538 	}
    539 }
    540 
    541 
    542 /* isapnp_print():
    543  *	Print the configuration line for an ISA PnP card.
    544  */
    545 static int
    546 isapnp_print(aux, str)
    547 	void *aux;
    548 	const char *str;
    549 {
    550 	struct isapnp_attach_args *ipa = aux;
    551 
    552 	if (str != NULL)
    553 		printf("%s: <%s, %s, %s, %s>",
    554 		    str, ipa->ipa_devident, ipa->ipa_devlogic,
    555 		    ipa->ipa_devcompat, ipa->ipa_devclass);
    556 
    557 	isapnp_print_region("port", ipa->ipa_io, ipa->ipa_nio);
    558 	isapnp_print_region("mem", ipa->ipa_mem, ipa->ipa_nmem);
    559 	isapnp_print_region("mem32", ipa->ipa_mem32, ipa->ipa_nmem32);
    560 	isapnp_print_pin("irq", ipa->ipa_irq, ipa->ipa_nirq);
    561 	isapnp_print_pin("drq", ipa->ipa_drq, ipa->ipa_ndrq);
    562 
    563 	return UNCONF;
    564 }
    565 
    566 
    567 #ifdef _KERNEL
    568 /* isapnp_submatch():
    569  *	Probe the logical device...
    570  */
    571 static int
    572 isapnp_submatch(parent, match, aux)
    573 	struct device *parent;
    574 	void *match, *aux;
    575 {
    576 	struct cfdata *cf = match;
    577 	return ((*cf->cf_attach->ca_match)(parent, match, aux));
    578 }
    579 #endif
    580 
    581 
    582 /* isapnp_find():
    583  *	Probe and add cards
    584  */
    585 static int
    586 isapnp_find(sc, all)
    587 	struct isapnp_softc *sc;
    588 	int all;
    589 {
    590 	int p;
    591 
    592 	isapnp_init(sc);
    593 
    594 	isapnp_write_reg(sc, ISAPNP_CONFIG_CONTROL, ISAPNP_CC_RESET_DRV);
    595 	DELAY(2000);
    596 
    597 	isapnp_init(sc);
    598 	DELAY(2000);
    599 
    600 	for (p = ISAPNP_RDDATA_MIN; p <= ISAPNP_RDDATA_MAX; p += 4) {
    601 		sc->sc_read_port = p;
    602 		if (isapnp_map_readport(sc))
    603 			continue;
    604 		DPRINTF(("%s: Trying port %x\n", sc->sc_dev.dv_xname, p));
    605 		if (isapnp_findcard(sc))
    606 			break;
    607 		isapnp_unmap_readport(sc);
    608 	}
    609 
    610 	if (p > ISAPNP_RDDATA_MAX) {
    611 		sc->sc_read_port = 0;
    612 		return 0;
    613 	}
    614 
    615 	if (all)
    616 		while (isapnp_findcard(sc))
    617 			continue;
    618 
    619 	return 1;
    620 }
    621 
    622 
    623 /* isapnp_configure():
    624  *	Configure a PnP card
    625  *	XXX: The memory configuration code is wrong. We need to check the
    626  *	     range/length bit an do appropriate sets.
    627  */
    628 static void
    629 isapnp_configure(sc, ipa)
    630 	struct isapnp_softc *sc;
    631 	const struct isapnp_attach_args *ipa;
    632 {
    633 	int i;
    634 	static u_char isapnp_mem_range[] = ISAPNP_MEM_DESC;
    635 	static u_char isapnp_io_range[] = ISAPNP_IO_DESC;
    636 	static u_char isapnp_irq_range[] = ISAPNP_IRQ_DESC;
    637 	static u_char isapnp_drq_range[] = ISAPNP_DRQ_DESC;
    638 	static u_char isapnp_mem32_range[] = ISAPNP_MEM32_DESC;
    639 	const struct isapnp_region *r;
    640 	const struct isapnp_pin *p;
    641 	struct isapnp_region rz;
    642 	struct isapnp_pin pz;
    643 
    644 	memset(&pz, 0, sizeof(pz));
    645 	memset(&rz, 0, sizeof(rz));
    646 
    647 #define B0(a) ((a) & 0xff)
    648 #define B1(a) (((a) >> 8) & 0xff)
    649 #define B2(a) (((a) >> 16) & 0xff)
    650 #define B3(a) (((a) >> 24) & 0xff)
    651 
    652 	for (i = 0; i < sizeof(isapnp_io_range); i++) {
    653 		if (i < ipa->ipa_nio)
    654 			r = &ipa->ipa_io[i];
    655 		else
    656 			r = &rz;
    657 
    658 		isapnp_write_reg(sc,
    659 		    isapnp_io_range[i] + ISAPNP_IO_BASE_15_8, B1(r->base));
    660 		isapnp_write_reg(sc,
    661 		    isapnp_io_range[i] + ISAPNP_IO_BASE_7_0, B0(r->base));
    662 	}
    663 
    664 	for (i = 0; i < sizeof(isapnp_mem_range); i++) {
    665 		if (i < ipa->ipa_nmem)
    666 			r = &ipa->ipa_mem[i];
    667 		else
    668 			r = &rz;
    669 
    670 		isapnp_write_reg(sc,
    671 		    isapnp_mem_range[i] + ISAPNP_MEM_BASE_23_16, B2(r->base));
    672 		isapnp_write_reg(sc,
    673 		    isapnp_mem_range[i] + ISAPNP_MEM_BASE_15_8, B1(r->base));
    674 
    675 		isapnp_write_reg(sc,
    676 		    isapnp_mem_range[i] + ISAPNP_MEM_LRANGE_23_16,
    677 		    B2(r->length));
    678 		isapnp_write_reg(sc,
    679 		    isapnp_mem_range[i] + ISAPNP_MEM_LRANGE_15_8,
    680 		    B1(r->length));
    681 	}
    682 
    683 	for (i = 0; i < sizeof(isapnp_irq_range); i++) {
    684 		u_char v;
    685 
    686 		if (i < ipa->ipa_nirq)
    687 			p = &ipa->ipa_irq[i];
    688 		else
    689 			p = &pz;
    690 
    691 		isapnp_write_reg(sc,
    692 		    isapnp_irq_range[i] + ISAPNP_IRQ_NUMBER, p->num);
    693 
    694 		switch (p->flags) {
    695 		case ISAPNP_IRQTYPE_LEVEL_PLUS:
    696 			v = ISAPNP_IRQ_LEVEL|ISAPNP_IRQ_HIGH;
    697 			break;
    698 
    699 		case ISAPNP_IRQTYPE_EDGE_PLUS:
    700 			v = ISAPNP_IRQ_HIGH;
    701 			break;
    702 
    703 		case ISAPNP_IRQTYPE_LEVEL_MINUS:
    704 			v = ISAPNP_IRQ_LEVEL;
    705 			break;
    706 
    707 		default:
    708 		case ISAPNP_IRQTYPE_EDGE_MINUS:
    709 			v = 0;
    710 			break;
    711 		}
    712 		isapnp_write_reg(sc,
    713 		    isapnp_irq_range[i] + ISAPNP_IRQ_CONTROL, v);
    714 	}
    715 
    716 	for (i = 0; i < sizeof(isapnp_drq_range); i++) {
    717 		u_char v;
    718 
    719 		if (i < ipa->ipa_ndrq)
    720 			v = ipa->ipa_drq[i].num;
    721 		else
    722 			v = 4;
    723 
    724 		isapnp_write_reg(sc, isapnp_drq_range[i], v);
    725 	}
    726 
    727 	for (i = 0; i < sizeof(isapnp_mem32_range); i++) {
    728 		if (i < ipa->ipa_nmem32)
    729 			r = &ipa->ipa_mem32[i];
    730 		else
    731 			r = &rz;
    732 
    733 		isapnp_write_reg(sc,
    734 		    isapnp_mem32_range[i] + ISAPNP_MEM32_BASE_31_24,
    735 		    B3(r->base));
    736 		isapnp_write_reg(sc,
    737 		    isapnp_mem32_range[i] + ISAPNP_MEM32_BASE_23_16,
    738 		    B2(r->base));
    739 		isapnp_write_reg(sc,
    740 		    isapnp_mem32_range[i] + ISAPNP_MEM32_BASE_15_8,
    741 		    B1(r->base));
    742 		isapnp_write_reg(sc,
    743 		    isapnp_mem32_range[i] + ISAPNP_MEM32_BASE_7_0,
    744 		    B0(r->base));
    745 
    746 		isapnp_write_reg(sc,
    747 		    isapnp_mem32_range[i] + ISAPNP_MEM32_LRANGE_31_24,
    748 		    B3(r->length));
    749 		isapnp_write_reg(sc,
    750 		    isapnp_mem32_range[i] + ISAPNP_MEM32_LRANGE_23_16,
    751 		    B2(r->length));
    752 		isapnp_write_reg(sc,
    753 		    isapnp_mem32_range[i] + ISAPNP_MEM32_LRANGE_15_8,
    754 		    B1(r->length));
    755 		isapnp_write_reg(sc,
    756 		    isapnp_mem32_range[i] + ISAPNP_MEM32_LRANGE_7_0,
    757 		    B0(r->length));
    758 	}
    759 }
    760 
    761 
    762 /* isapnp_match():
    763  *	Probe routine
    764  */
    765 static int
    766 isapnp_match(parent, match, aux)
    767 	struct device *parent;
    768 #ifdef __BROKEN_INDIRECT_CONFIG
    769 	void *match;
    770 #else
    771 	struct cfdata *match;
    772 #endif
    773 	void *aux;
    774 {
    775 	int rv;
    776 	struct isapnp_softc sc;
    777 	struct isa_attach_args *ia = aux;
    778 
    779 	sc.sc_iot = ia->ia_iot;
    780 	sc.sc_ncards = 0;
    781 	(void) strcpy(sc.sc_dev.dv_xname, "(isapnp probe)");
    782 
    783 	if (isapnp_map(&sc))
    784 		return 0;
    785 
    786 	rv = isapnp_find(&sc, 0);
    787 	ia->ia_iobase = ISAPNP_ADDR;
    788 	ia->ia_iosize = 1;
    789 
    790 	isapnp_unmap(&sc);
    791 	if (rv)
    792 		isapnp_unmap_readport(&sc);
    793 
    794 	return (rv);
    795 }
    796 
    797 
    798 /* isapnp_attach
    799  *	Find and attach PnP cards.
    800  */
    801 static void
    802 isapnp_attach(parent, self, aux)
    803 	struct device *parent, *self;
    804 	void *aux;
    805 {
    806 	struct isapnp_softc *sc = (struct isapnp_softc *) self;
    807 	struct isa_attach_args *ia = aux;
    808 	int c, d;
    809 
    810 	sc->sc_iot = ia->ia_iot;
    811 	sc->sc_memt = ia->ia_memt;
    812 	sc->sc_ncards = 0;
    813 
    814 	if (isapnp_map(sc))
    815 		panic("%s: bus map failed\n", sc->sc_dev.dv_xname);
    816 
    817 	if (!isapnp_find(sc, 1))
    818 		panic("%s: no cards found\n", sc->sc_dev.dv_xname);
    819 
    820 	printf(": read port 0x%x\n", sc->sc_read_port);
    821 
    822 	for (c = 0; c < sc->sc_ncards; c++) {
    823 		struct isapnp_attach_args *ipa, *lpa;
    824 
    825 		/* Good morning card c */
    826 		isapnp_write_reg(sc, ISAPNP_WAKE, c + 1);
    827 
    828 		if ((ipa = isapnp_get_resource(sc, c)) == NULL)
    829 			continue;
    830 
    831 		DPRINTF(("Selecting attachments\n"));
    832 		for (d = 0;
    833 		    (lpa = isapnp_bestconfig(parent, sc, &ipa)) != NULL; d++) {
    834 			isapnp_write_reg(sc, ISAPNP_LOGICAL_DEV_NUM, d);
    835 			isapnp_configure(sc, lpa);
    836 #ifdef DEBUG_ISAPNP
    837 			{
    838 				struct isapnp_attach_args pa;
    839 
    840 				isapnp_get_config(sc, &pa);
    841 				isapnp_print_config(&pa);
    842 			}
    843 #endif
    844 
    845 #ifdef DEBUG
    846 			/* XXX do we even really need this?  --thorpej */
    847 			printf("%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 #endif
    852 			if (lpa->ipa_pref == ISAPNP_DEP_CONFLICTING) {
    853 				printf("%s: <%s, %s, %s, %s> ignored; %s\n",
    854 				    sc->sc_dev.dv_xname,
    855 				    lpa->ipa_devident, lpa->ipa_devlogic,
    856 				    lpa->ipa_devcompat, lpa->ipa_devclass,
    857 				    "resource conflict");
    858 				ISAPNP_FREE(lpa);
    859 				continue;
    860 			}
    861 
    862 			lpa->ipa_ic = ia->ia_ic;
    863 			lpa->ipa_iot = ia->ia_iot;
    864 			lpa->ipa_memt = ia->ia_memt;
    865 			lpa->ipa_dmat = ia->ia_dmat;
    866 
    867 			isapnp_write_reg(sc, ISAPNP_ACTIVATE, 1);
    868 #ifdef _KERNEL
    869 			if (config_found_sm(self, lpa, isapnp_print,
    870 			    isapnp_submatch) == NULL)
    871 				isapnp_write_reg(sc, ISAPNP_ACTIVATE, 0);
    872 #else
    873 			isapnp_print(lpa, NULL);
    874 			printf("\n");
    875 #endif
    876 			ISAPNP_FREE(lpa);
    877 		}
    878 		isapnp_write_reg(sc, ISAPNP_WAKE, 0);    /* Good night cards */
    879 	}
    880 }
    881