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