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