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