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