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