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