xref: /freebsd/sys/isa/pnp.c (revision 38b968c3a5fbc52770ed79b0579b7894e6a10c27)
14249382dSDoug Rabson /*
24249382dSDoug Rabson  * Copyright (c) 1996, Sujal M. Patel
34249382dSDoug Rabson  * All rights reserved.
44249382dSDoug Rabson  *
54249382dSDoug Rabson  * Redistribution and use in source and binary forms, with or without
64249382dSDoug Rabson  * modification, are permitted provided that the following conditions
74249382dSDoug Rabson  * are met:
84249382dSDoug Rabson  * 1. Redistributions of source code must retain the above copyright
94249382dSDoug Rabson  *    notice, this list of conditions and the following disclaimer.
104249382dSDoug Rabson  * 2. Redistributions in binary form must reproduce the above copyright
114249382dSDoug Rabson  *    notice, this list of conditions and the following disclaimer in the
124249382dSDoug Rabson  *    documentation and/or other materials provided with the distribution.
134249382dSDoug Rabson  *
144249382dSDoug Rabson  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
154249382dSDoug Rabson  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
164249382dSDoug Rabson  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
174249382dSDoug Rabson  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
184249382dSDoug Rabson  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
194249382dSDoug Rabson  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
204249382dSDoug Rabson  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
214249382dSDoug Rabson  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
224249382dSDoug Rabson  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
234249382dSDoug Rabson  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
244249382dSDoug Rabson  * SUCH DAMAGE.
254249382dSDoug Rabson  *
264249382dSDoug Rabson  *	$FreeBSD$
274249382dSDoug Rabson  *      from: pnp.c,v 1.11 1999/05/06 22:11:19 peter Exp
284249382dSDoug Rabson  */
294249382dSDoug Rabson 
304249382dSDoug Rabson #include <sys/param.h>
314249382dSDoug Rabson #include <sys/systm.h>
324249382dSDoug Rabson #include <sys/kernel.h>
334249382dSDoug Rabson #include <sys/module.h>
344249382dSDoug Rabson #include <sys/bus.h>
354249382dSDoug Rabson #include <sys/malloc.h>
364249382dSDoug Rabson #include <isa/isavar.h>
374249382dSDoug Rabson #include <isa/pnpreg.h>
384249382dSDoug Rabson #include <isa/pnpvar.h>
3921c3015aSDoug Rabson #include <machine/bus.h>
404249382dSDoug Rabson 
414249382dSDoug Rabson typedef struct _pnp_id {
424249382dSDoug Rabson 	u_int32_t vendor_id;
434249382dSDoug Rabson 	u_int32_t serial;
444249382dSDoug Rabson 	u_char checksum;
454249382dSDoug Rabson } pnp_id;
464249382dSDoug Rabson 
474249382dSDoug Rabson struct pnp_set_config_arg {
484249382dSDoug Rabson 	int	csn;		/* Card number to configure */
494249382dSDoug Rabson 	int	ldn;		/* Logical device on card */
504249382dSDoug Rabson };
514249382dSDoug Rabson 
524249382dSDoug Rabson struct pnp_quirk {
534249382dSDoug Rabson 	u_int32_t vendor_id;	/* Vendor of the card */
544249382dSDoug Rabson 	u_int32_t logical_id;	/* ID of the device with quirk */
554249382dSDoug Rabson 	int	type;
564249382dSDoug Rabson #define PNP_QUIRK_WRITE_REG	1 /* Need to write a pnp register  */
57fb0ef528SSeigo Tanimura #define PNP_QUIRK_EXTRA_IO	2 /* Has extra io ports  */
584249382dSDoug Rabson 	int	arg1;
594249382dSDoug Rabson 	int	arg2;
604249382dSDoug Rabson };
614249382dSDoug Rabson 
624249382dSDoug Rabson struct pnp_quirk pnp_quirks[] = {
634249382dSDoug Rabson 	/*
644249382dSDoug Rabson 	 * The Gravis UltraSound needs register 0xf2 to be set to 0xff
654249382dSDoug Rabson 	 * to enable power.
664249382dSDoug Rabson 	 * XXX need to know the logical device id.
674249382dSDoug Rabson 	 */
684249382dSDoug Rabson 	{ 0x0100561e /* GRV0001 */,	0,
694249382dSDoug Rabson 	  PNP_QUIRK_WRITE_REG,	0xf2,	 0xff },
70fb0ef528SSeigo Tanimura 	/*
71fb0ef528SSeigo Tanimura 	 * An emu8000 does not give us other than the first
72fb0ef528SSeigo Tanimura 	 * port.
73fb0ef528SSeigo Tanimura 	 */
74fb0ef528SSeigo Tanimura 	{ 0x26008c0e /* SB16 */,	0x21008c0e,
75fb0ef528SSeigo Tanimura 	  PNP_QUIRK_EXTRA_IO,	0x400,	 0x800 },
76fb0ef528SSeigo Tanimura 	{ 0x42008c0e /* SB32(CTL0042) */,	0x21008c0e,
77fb0ef528SSeigo Tanimura 	  PNP_QUIRK_EXTRA_IO,	0x400,	 0x800 },
78fb0ef528SSeigo Tanimura 	{ 0x44008c0e /* SB32(CTL0044) */,	0x21008c0e,
79fb0ef528SSeigo Tanimura 	  PNP_QUIRK_EXTRA_IO,	0x400,	 0x800 },
80fb0ef528SSeigo Tanimura 	{ 0x49008c0e /* SB32(CTL0049) */,	0x21008c0e,
81fb0ef528SSeigo Tanimura 	  PNP_QUIRK_EXTRA_IO,	0x400,	 0x800 },
82fb0ef528SSeigo Tanimura 	{ 0xf1008c0e /* SB32(CTL00f1) */,	0x21008c0e,
83fb0ef528SSeigo Tanimura 	  PNP_QUIRK_EXTRA_IO,	0x400,	 0x800 },
84fb0ef528SSeigo Tanimura 	{ 0xc1008c0e /* SB64(CTL00c1) */,	0x22008c0e,
85fb0ef528SSeigo Tanimura 	  PNP_QUIRK_EXTRA_IO,	0x400,	 0x800 },
8638b968c3SSeigo Tanimura 	{ 0xc5008c0e /* SB64(CTL00c5) */,	0x22008c0e,
8738b968c3SSeigo Tanimura 	  PNP_QUIRK_EXTRA_IO,	0x400,	 0x800 },
88fb0ef528SSeigo Tanimura 	{ 0xe4008c0e /* SB64(CTL00e4) */,	0x22008c0e,
89fb0ef528SSeigo Tanimura 	  PNP_QUIRK_EXTRA_IO,	0x400,	 0x800 },
904249382dSDoug Rabson 
914249382dSDoug Rabson 	{ 0 }
924249382dSDoug Rabson };
934249382dSDoug Rabson 
944249382dSDoug Rabson #if 0
954249382dSDoug Rabson /*
964249382dSDoug Rabson  * these entries are initialized using the autoconfig menu
974249382dSDoug Rabson  * The struct is invalid (and must be initialized) if the first
984249382dSDoug Rabson  * CSN is zero. The init code fills invalid entries with CSN 255
994249382dSDoug Rabson  * which is not a supported value.
1004249382dSDoug Rabson  */
1014249382dSDoug Rabson 
1024249382dSDoug Rabson struct pnp_cinfo pnp_ldn_overrides[MAX_PNP_LDN] = {
1034249382dSDoug Rabson     { 0 }
1044249382dSDoug Rabson };
1054249382dSDoug Rabson #endif
1064249382dSDoug Rabson 
1074249382dSDoug Rabson /* The READ_DATA port that we are using currently */
1084249382dSDoug Rabson static int pnp_rd_port;
1094249382dSDoug Rabson 
1104249382dSDoug Rabson static void   pnp_send_initiation_key(void);
1114249382dSDoug Rabson static int    pnp_get_serial(pnp_id *p);
1124249382dSDoug Rabson static int    pnp_isolation_protocol(device_t parent);
1134249382dSDoug Rabson 
114bb2b9030SDoug Rabson char *
115bb2b9030SDoug Rabson pnp_eisaformat(u_int32_t id)
116bb2b9030SDoug Rabson {
117bb2b9030SDoug Rabson 	u_int8_t *data = (u_int8_t *) &id;
118bb2b9030SDoug Rabson 	static char idbuf[8];
119bb2b9030SDoug Rabson 	const char  hextoascii[] = "0123456789abcdef";
120bb2b9030SDoug Rabson 
121bb2b9030SDoug Rabson 	idbuf[0] = '@' + ((data[0] & 0x7c) >> 2);
122bb2b9030SDoug Rabson 	idbuf[1] = '@' + (((data[0] & 0x3) << 3) + ((data[1] & 0xe0) >> 5));
123bb2b9030SDoug Rabson 	idbuf[2] = '@' + (data[1] & 0x1f);
124bb2b9030SDoug Rabson 	idbuf[3] = hextoascii[(data[2] >> 4)];
125bb2b9030SDoug Rabson 	idbuf[4] = hextoascii[(data[2] & 0xf)];
126bb2b9030SDoug Rabson 	idbuf[5] = hextoascii[(data[3] >> 4)];
127bb2b9030SDoug Rabson 	idbuf[6] = hextoascii[(data[3] & 0xf)];
128bb2b9030SDoug Rabson 	idbuf[7] = 0;
129bb2b9030SDoug Rabson 	return(idbuf);
130bb2b9030SDoug Rabson }
131bb2b9030SDoug Rabson 
1324249382dSDoug Rabson static void
1334249382dSDoug Rabson pnp_write(int d, u_char r)
1344249382dSDoug Rabson {
1354249382dSDoug Rabson 	outb (_PNP_ADDRESS, d);
1364249382dSDoug Rabson 	outb (_PNP_WRITE_DATA, r);
1374249382dSDoug Rabson }
1384249382dSDoug Rabson 
1394249382dSDoug Rabson #if 0
1404249382dSDoug Rabson 
1414249382dSDoug Rabson static u_char
1424249382dSDoug Rabson pnp_read(int d)
1434249382dSDoug Rabson {
1444249382dSDoug Rabson 	outb (_PNP_ADDRESS, d);
1454249382dSDoug Rabson 	return (inb(3 | (pnp_rd_port <<2)));
1464249382dSDoug Rabson }
1474249382dSDoug Rabson 
1484249382dSDoug Rabson #endif
1494249382dSDoug Rabson 
1504249382dSDoug Rabson /*
1514249382dSDoug Rabson  * Send Initiation LFSR as described in "Plug and Play ISA Specification",
1524249382dSDoug Rabson  * Intel May 94.
1534249382dSDoug Rabson  */
1544249382dSDoug Rabson static void
1554249382dSDoug Rabson pnp_send_initiation_key()
1564249382dSDoug Rabson {
1574249382dSDoug Rabson 	int cur, i;
1584249382dSDoug Rabson 
1594249382dSDoug Rabson 	/* Reset the LSFR */
1604249382dSDoug Rabson 	outb(_PNP_ADDRESS, 0);
1614249382dSDoug Rabson 	outb(_PNP_ADDRESS, 0); /* yes, we do need it twice! */
1624249382dSDoug Rabson 
1634249382dSDoug Rabson 	cur = 0x6a;
1644249382dSDoug Rabson 	outb(_PNP_ADDRESS, cur);
1654249382dSDoug Rabson 
1664249382dSDoug Rabson 	for (i = 1; i < 32; i++) {
1674249382dSDoug Rabson 		cur = (cur >> 1) | (((cur ^ (cur >> 1)) << 7) & 0xff);
1684249382dSDoug Rabson 		outb(_PNP_ADDRESS, cur);
1694249382dSDoug Rabson 	}
1704249382dSDoug Rabson }
1714249382dSDoug Rabson 
1724249382dSDoug Rabson 
1734249382dSDoug Rabson /*
1744249382dSDoug Rabson  * Get the device's serial number.  Returns 1 if the serial is valid.
1754249382dSDoug Rabson  */
1764249382dSDoug Rabson static int
1774249382dSDoug Rabson pnp_get_serial(pnp_id *p)
1784249382dSDoug Rabson {
1794249382dSDoug Rabson 	int i, bit, valid = 0, sum = 0x6a;
1804249382dSDoug Rabson 	u_char *data = (u_char *)p;
1814249382dSDoug Rabson 
1824249382dSDoug Rabson 	bzero(data, sizeof(char) * 9);
1834249382dSDoug Rabson 	outb(_PNP_ADDRESS, PNP_SERIAL_ISOLATION);
1844249382dSDoug Rabson 	for (i = 0; i < 72; i++) {
1854249382dSDoug Rabson 		bit = inb((pnp_rd_port << 2) | 0x3) == 0x55;
1864249382dSDoug Rabson 		DELAY(250);	/* Delay 250 usec */
1874249382dSDoug Rabson 
1884249382dSDoug Rabson 		/* Can't Short Circuit the next evaluation, so 'and' is last */
1894249382dSDoug Rabson 		bit = (inb((pnp_rd_port << 2) | 0x3) == 0xaa) && bit;
1904249382dSDoug Rabson 		DELAY(250);	/* Delay 250 usec */
1914249382dSDoug Rabson 
1924249382dSDoug Rabson 		valid = valid || bit;
1934249382dSDoug Rabson 
1944249382dSDoug Rabson 		if (i < 64)
1954249382dSDoug Rabson 			sum = (sum >> 1) |
1964249382dSDoug Rabson 				(((sum ^ (sum >> 1) ^ bit) << 7) & 0xff);
1974249382dSDoug Rabson 
1984249382dSDoug Rabson 		data[i / 8] = (data[i / 8] >> 1) | (bit ? 0x80 : 0);
1994249382dSDoug Rabson 	}
2004249382dSDoug Rabson 
2014249382dSDoug Rabson 	valid = valid && (data[8] == sum);
2024249382dSDoug Rabson 
2034249382dSDoug Rabson 	return valid;
2044249382dSDoug Rabson }
2054249382dSDoug Rabson 
2064249382dSDoug Rabson /*
2074249382dSDoug Rabson  * Fill's the buffer with resource info from the device.
2085b45337dSDoug Rabson  * Returns the number of characters read.
2094249382dSDoug Rabson  */
2104249382dSDoug Rabson static int
2114249382dSDoug Rabson pnp_get_resource_info(u_char *buffer, int len)
2124249382dSDoug Rabson {
2135b45337dSDoug Rabson 	int i, j, count;
2144249382dSDoug Rabson 	u_char temp;
2154249382dSDoug Rabson 
2165b45337dSDoug Rabson 	count = 0;
2174249382dSDoug Rabson 	for (i = 0; i < len; i++) {
2184249382dSDoug Rabson 		outb(_PNP_ADDRESS, PNP_STATUS);
2194249382dSDoug Rabson 		for (j = 0; j < 100; j++) {
2204249382dSDoug Rabson 			if ((inb((pnp_rd_port << 2) | 0x3)) & 0x1)
2214249382dSDoug Rabson 				break;
2224249382dSDoug Rabson 			DELAY(1);
2234249382dSDoug Rabson 		}
2244249382dSDoug Rabson 		if (j == 100) {
2254249382dSDoug Rabson 			printf("PnP device failed to report resource data\n");
2265b45337dSDoug Rabson 			return count;
2274249382dSDoug Rabson 		}
2284249382dSDoug Rabson 		outb(_PNP_ADDRESS, PNP_RESOURCE_DATA);
2294249382dSDoug Rabson 		temp = inb((pnp_rd_port << 2) | 0x3);
2304249382dSDoug Rabson 		if (buffer != NULL)
2314249382dSDoug Rabson 			buffer[i] = temp;
2325b45337dSDoug Rabson 		count++;
2334249382dSDoug Rabson 	}
2345b45337dSDoug Rabson 	return count;
2354249382dSDoug Rabson }
2364249382dSDoug Rabson 
2374249382dSDoug Rabson #if 0
2384249382dSDoug Rabson /*
2394249382dSDoug Rabson  * write_pnp_parms initializes a logical device with the parms
2404249382dSDoug Rabson  * in d, and then activates the board if the last parameter is 1.
2414249382dSDoug Rabson  */
2424249382dSDoug Rabson 
2434249382dSDoug Rabson static int
2444249382dSDoug Rabson write_pnp_parms(struct pnp_cinfo *d, pnp_id *p, int ldn)
2454249382dSDoug Rabson {
2464249382dSDoug Rabson     int i, empty = -1 ;
2474249382dSDoug Rabson 
2484249382dSDoug Rabson     pnp_write (SET_LDN, ldn );
2494249382dSDoug Rabson     i = pnp_read(SET_LDN) ;
2504249382dSDoug Rabson     if (i != ldn) {
2514249382dSDoug Rabson 	printf("Warning: LDN %d does not exist\n", ldn);
2524249382dSDoug Rabson     }
2534249382dSDoug Rabson     for (i = 0; i < 8; i++) {
2544249382dSDoug Rabson 	pnp_write(IO_CONFIG_BASE + i * 2, d->ic_port[i] >> 8 );
2554249382dSDoug Rabson 	pnp_write(IO_CONFIG_BASE + i * 2 + 1, d->ic_port[i] & 0xff );
2564249382dSDoug Rabson     }
2574249382dSDoug Rabson     for (i = 0; i < 4; i++) {
2584249382dSDoug Rabson 	pnp_write(MEM_CONFIG + i*8, (d->ic_mem[i].base >> 16) & 0xff );
2594249382dSDoug Rabson 	pnp_write(MEM_CONFIG + i*8+1, (d->ic_mem[i].base >> 8) & 0xff );
2604249382dSDoug Rabson 	pnp_write(MEM_CONFIG + i*8+2, d->ic_mem[i].control & 0xff );
2614249382dSDoug Rabson 	pnp_write(MEM_CONFIG + i*8+3, (d->ic_mem[i].range >> 16) & 0xff );
2624249382dSDoug Rabson 	pnp_write(MEM_CONFIG + i*8+4, (d->ic_mem[i].range >> 8) & 0xff );
2634249382dSDoug Rabson     }
2644249382dSDoug Rabson     for (i = 0; i < 2; i++) {
2654249382dSDoug Rabson 	pnp_write(IRQ_CONFIG + i*2    , d->irq[i] );
2664249382dSDoug Rabson 	pnp_write(IRQ_CONFIG + i*2 + 1, d->irq_type[i] );
2674249382dSDoug Rabson 	pnp_write(DRQ_CONFIG + i, d->drq[i] );
2684249382dSDoug Rabson     }
2694249382dSDoug Rabson     /*
2704249382dSDoug Rabson      * store parameters read into the current kernel
2714249382dSDoug Rabson      * so manual editing next time is easier
2724249382dSDoug Rabson      */
2734249382dSDoug Rabson     for (i = 0 ; i < MAX_PNP_LDN; i++) {
2744249382dSDoug Rabson 	if (pnp_ldn_overrides[i].csn == d->csn &&
2754249382dSDoug Rabson 		pnp_ldn_overrides[i].ldn == ldn) {
2764249382dSDoug Rabson 	    d->flags = pnp_ldn_overrides[i].flags ;
2774249382dSDoug Rabson 	    pnp_ldn_overrides[i] = *d ;
2784249382dSDoug Rabson 	    break ;
2794249382dSDoug Rabson 	} else if (pnp_ldn_overrides[i].csn < 1 ||
2804249382dSDoug Rabson 		pnp_ldn_overrides[i].csn == 255)
2814249382dSDoug Rabson 	    empty = i ;
2824249382dSDoug Rabson     }
2834249382dSDoug Rabson     if (i== MAX_PNP_LDN && empty != -1)
2844249382dSDoug Rabson 	pnp_ldn_overrides[empty] = *d;
2854249382dSDoug Rabson 
2864249382dSDoug Rabson     /*
2874249382dSDoug Rabson      * Here should really perform the range check, and
2884249382dSDoug Rabson      * return a failure if not successful.
2894249382dSDoug Rabson      */
2904249382dSDoug Rabson     pnp_write (IO_RANGE_CHECK, 0);
2914249382dSDoug Rabson     DELAY(1000); /* XXX is it really necessary ? */
2924249382dSDoug Rabson     pnp_write (ACTIVATE, d->enable ? 1 : 0);
2934249382dSDoug Rabson     DELAY(1000); /* XXX is it really necessary ? */
2944249382dSDoug Rabson     return 1 ;
2954249382dSDoug Rabson }
2964249382dSDoug Rabson #endif
2974249382dSDoug Rabson 
2984249382dSDoug Rabson /*
2994249382dSDoug Rabson  * This function is called after the bus has assigned resource
3004249382dSDoug Rabson  * locations for a logical device.
3014249382dSDoug Rabson  */
3024249382dSDoug Rabson static void
3034249382dSDoug Rabson pnp_set_config(void *arg, struct isa_config *config, int enable)
3044249382dSDoug Rabson {
3054249382dSDoug Rabson 	int csn = ((struct pnp_set_config_arg *) arg)->csn;
3064249382dSDoug Rabson 	int ldn = ((struct pnp_set_config_arg *) arg)->ldn;
3074249382dSDoug Rabson 	int i;
3084249382dSDoug Rabson 
3094249382dSDoug Rabson 	/*
3104249382dSDoug Rabson 	 * First put all cards into Sleep state with the initiation
3114249382dSDoug Rabson 	 * key, then put our card into Config state.
3124249382dSDoug Rabson 	 */
3134249382dSDoug Rabson 	pnp_send_initiation_key();
3144249382dSDoug Rabson 	pnp_write(PNP_WAKE, csn);
3154249382dSDoug Rabson 
3164249382dSDoug Rabson 	/*
3174249382dSDoug Rabson 	 * Select our logical device so that we can program it.
3184249382dSDoug Rabson 	 */
3194249382dSDoug Rabson 	pnp_write(PNP_SET_LDN, ldn);
3204249382dSDoug Rabson 
3214249382dSDoug Rabson 	/*
32208764b84SMike Smith 	 * Constrain the number of resources we will try to program
32308764b84SMike Smith 	 */
32408764b84SMike Smith 	if (config->ic_nmem > ISA_PNP_NMEM) {
32508764b84SMike Smith 	    printf("too many ISA memory ranges (%d > %d)\n", config->ic_nmem, ISA_PNP_NMEM);
32608764b84SMike Smith 	    config->ic_nmem = ISA_PNP_NMEM;
32708764b84SMike Smith 	}
32808764b84SMike Smith 	if (config->ic_nport > ISA_PNP_NPORT) {
32908764b84SMike Smith 	    printf("too many ISA I/O ranges (%d > %d)\n", config->ic_nport, ISA_PNP_NPORT);
33008764b84SMike Smith 	    config->ic_nport = ISA_PNP_NPORT;
33108764b84SMike Smith 	}
33208764b84SMike Smith 	if (config->ic_nirq > ISA_PNP_NIRQ) {
33308764b84SMike Smith 	    printf("too many ISA IRQs (%d > %d)\n", config->ic_nirq, ISA_PNP_NIRQ);
33408764b84SMike Smith 	    config->ic_nirq = ISA_PNP_NIRQ;
33508764b84SMike Smith 	}
33608764b84SMike Smith 	if (config->ic_ndrq > ISA_PNP_NDRQ) {
33708764b84SMike Smith 	    printf("too many ISA DRQs (%d > %d)\n", config->ic_ndrq, ISA_PNP_NDRQ);
33808764b84SMike Smith 	    config->ic_ndrq = ISA_PNP_NDRQ;
33908764b84SMike Smith 	}
34008764b84SMike Smith 
34108764b84SMike Smith 	/*
3424249382dSDoug Rabson 	 * Now program the resources.
3434249382dSDoug Rabson 	 */
3444249382dSDoug Rabson 	for (i = 0; i < config->ic_nmem; i++) {
345c3959391SKazutaka YOKOTA 		u_int32_t start;
346c3959391SKazutaka YOKOTA 		u_int32_t size;
347c3959391SKazutaka YOKOTA 
348c3959391SKazutaka YOKOTA 		/* XXX: should handle memory control register, 32 bit memory */
349c3959391SKazutaka YOKOTA 		if (config->ic_mem[i].ir_size == 0) {
350c3959391SKazutaka YOKOTA 			pnp_write(PNP_MEM_BASE_HIGH(i), 0);
351c3959391SKazutaka YOKOTA 			pnp_write(PNP_MEM_BASE_LOW(i), 0);
352c3959391SKazutaka YOKOTA 			pnp_write(PNP_MEM_RANGE_HIGH(i), 0);
353c3959391SKazutaka YOKOTA 			pnp_write(PNP_MEM_RANGE_LOW(i), 0);
354c3959391SKazutaka YOKOTA 		} else {
355c3959391SKazutaka YOKOTA 			start = config->ic_mem[i].ir_start;
356c3959391SKazutaka YOKOTA 			size =  config->ic_mem[i].ir_size;
3574249382dSDoug Rabson 			if (start & 0xff)
3584249382dSDoug Rabson 				panic("pnp_set_config: bogus memory assignment");
3594249382dSDoug Rabson 			pnp_write(PNP_MEM_BASE_HIGH(i), (start >> 16) & 0xff);
3604249382dSDoug Rabson 			pnp_write(PNP_MEM_BASE_LOW(i), (start >> 8) & 0xff);
3614249382dSDoug Rabson 			pnp_write(PNP_MEM_RANGE_HIGH(i), (size >> 16) & 0xff);
3624249382dSDoug Rabson 			pnp_write(PNP_MEM_RANGE_LOW(i), (size >> 8) & 0xff);
3634249382dSDoug Rabson 		}
364c3959391SKazutaka YOKOTA 	}
36508764b84SMike Smith 	for (; i < ISA_PNP_NMEM; i++) {
3664249382dSDoug Rabson 		pnp_write(PNP_MEM_BASE_HIGH(i), 0);
3674249382dSDoug Rabson 		pnp_write(PNP_MEM_BASE_LOW(i), 0);
3684249382dSDoug Rabson 		pnp_write(PNP_MEM_RANGE_HIGH(i), 0);
3694249382dSDoug Rabson 		pnp_write(PNP_MEM_RANGE_LOW(i), 0);
3704249382dSDoug Rabson 	}
3714249382dSDoug Rabson 
3724249382dSDoug Rabson 	for (i = 0; i < config->ic_nport; i++) {
373c3959391SKazutaka YOKOTA 		u_int32_t start;
374c3959391SKazutaka YOKOTA 
375c3959391SKazutaka YOKOTA 		if (config->ic_port[i].ir_size == 0) {
376c3959391SKazutaka YOKOTA 			pnp_write(PNP_IO_BASE_HIGH(i), 0);
377c3959391SKazutaka YOKOTA 			pnp_write(PNP_IO_BASE_LOW(i), 0);
378c3959391SKazutaka YOKOTA 		} else {
379c3959391SKazutaka YOKOTA 			start = config->ic_port[i].ir_start;
3804249382dSDoug Rabson 			pnp_write(PNP_IO_BASE_HIGH(i), (start >> 8) & 0xff);
3814249382dSDoug Rabson 			pnp_write(PNP_IO_BASE_LOW(i), (start >> 0) & 0xff);
3824249382dSDoug Rabson 		}
383c3959391SKazutaka YOKOTA 	}
38408764b84SMike Smith 	for (; i < ISA_PNP_NPORT; i++) {
3854249382dSDoug Rabson 		pnp_write(PNP_IO_BASE_HIGH(i), 0);
3864249382dSDoug Rabson 		pnp_write(PNP_IO_BASE_LOW(i), 0);
3874249382dSDoug Rabson 	}
3884249382dSDoug Rabson 
3894249382dSDoug Rabson 	for (i = 0; i < config->ic_nirq; i++) {
390c3959391SKazutaka YOKOTA 		int irq;
391c3959391SKazutaka YOKOTA 
392c3959391SKazutaka YOKOTA 		/* XXX: interrupt type */
393c3959391SKazutaka YOKOTA 		if (config->ic_irqmask[i] == 0) {
394c3959391SKazutaka YOKOTA 			pnp_write(PNP_IRQ_LEVEL(i), 0);
395c3959391SKazutaka YOKOTA 			pnp_write(PNP_IRQ_TYPE(i), 2);
396c3959391SKazutaka YOKOTA 		} else {
397c3959391SKazutaka YOKOTA 			irq = ffs(config->ic_irqmask[i]) - 1;
3984249382dSDoug Rabson 			pnp_write(PNP_IRQ_LEVEL(i), irq);
3994249382dSDoug Rabson 			pnp_write(PNP_IRQ_TYPE(i), 2); /* XXX */
4004249382dSDoug Rabson 		}
401c3959391SKazutaka YOKOTA 	}
40208764b84SMike Smith 	for (; i < ISA_PNP_NIRQ; i++) {
4034249382dSDoug Rabson 		/*
4044249382dSDoug Rabson 		 * IRQ 0 is not a valid interrupt selection and
4054249382dSDoug Rabson 		 * represents no interrupt selection.
4064249382dSDoug Rabson 		 */
4074249382dSDoug Rabson 		pnp_write(PNP_IRQ_LEVEL(i), 0);
408c3959391SKazutaka YOKOTA 		pnp_write(PNP_IRQ_TYPE(i), 2);
4094249382dSDoug Rabson 	}
4104249382dSDoug Rabson 
4114249382dSDoug Rabson 	for (i = 0; i < config->ic_ndrq; i++) {
412c3959391SKazutaka YOKOTA 		int drq;
413c3959391SKazutaka YOKOTA 
414c3959391SKazutaka YOKOTA 		if (config->ic_drqmask[i] == 0) {
415c3959391SKazutaka YOKOTA 			pnp_write(PNP_DMA_CHANNEL(i), 4);
416c3959391SKazutaka YOKOTA 		} else {
417c3959391SKazutaka YOKOTA 			drq = ffs(config->ic_drqmask[i]) - 1;
4184249382dSDoug Rabson 			pnp_write(PNP_DMA_CHANNEL(i), drq);
4194249382dSDoug Rabson 		}
420c3959391SKazutaka YOKOTA 	}
42108764b84SMike Smith 	for (; i < ISA_PNP_NDRQ; i++) {
4224249382dSDoug Rabson 		/*
4234249382dSDoug Rabson 		 * DMA channel 4, the cascade channel is used to
4244249382dSDoug Rabson 		 * indicate no DMA channel is active.
4254249382dSDoug Rabson 		 */
4264249382dSDoug Rabson 		pnp_write(PNP_DMA_CHANNEL(i), 4);
4274249382dSDoug Rabson 	}
4284249382dSDoug Rabson 
4294249382dSDoug Rabson 	pnp_write(PNP_ACTIVATE, enable ? 1 : 0);
4304249382dSDoug Rabson 
4314249382dSDoug Rabson 	/*
4324249382dSDoug Rabson 	 * Wake everyone up again, we are finished.
4334249382dSDoug Rabson 	 */
4344249382dSDoug Rabson 	pnp_write(PNP_CONFIG_CONTROL, PNP_CONFIG_CONTROL_WAIT_FOR_KEY);
4354249382dSDoug Rabson }
4364249382dSDoug Rabson 
4374249382dSDoug Rabson /*
4384249382dSDoug Rabson  * Process quirks for a logical device.. The card must be in Config state.
4394249382dSDoug Rabson  */
440fb0ef528SSeigo Tanimura void
441fb0ef528SSeigo Tanimura pnp_check_quirks(u_int32_t vendor_id, u_int32_t logical_id, int ldn, struct isa_config *config)
4424249382dSDoug Rabson {
4434249382dSDoug Rabson 	struct pnp_quirk *qp;
4444249382dSDoug Rabson 
4454249382dSDoug Rabson 	for (qp = &pnp_quirks[0]; qp->vendor_id; qp++) {
4464249382dSDoug Rabson 		if (qp->vendor_id == vendor_id
4474249382dSDoug Rabson 		    && (qp->logical_id == 0
4484249382dSDoug Rabson 			|| qp->logical_id == logical_id)) {
4494249382dSDoug Rabson 			switch (qp->type) {
4504249382dSDoug Rabson 			case PNP_QUIRK_WRITE_REG:
4514249382dSDoug Rabson 				pnp_write(PNP_SET_LDN, ldn);
4524249382dSDoug Rabson 				pnp_write(qp->arg1, qp->arg2);
4534249382dSDoug Rabson 				break;
454fb0ef528SSeigo Tanimura 			case PNP_QUIRK_EXTRA_IO:
455fb0ef528SSeigo Tanimura 				if (config == NULL)
456fb0ef528SSeigo Tanimura 					break;
457fb0ef528SSeigo Tanimura 				if (qp->arg1 != 0) {
458fb0ef528SSeigo Tanimura 					config->ic_nport++;
459fb0ef528SSeigo Tanimura 					config->ic_port[config->ic_nport - 1] = config->ic_port[0];
460fb0ef528SSeigo Tanimura 					config->ic_port[config->ic_nport - 1].ir_start += qp->arg1;
461fb0ef528SSeigo Tanimura 					config->ic_port[config->ic_nport - 1].ir_end += qp->arg1;
462fb0ef528SSeigo Tanimura 				}
463fb0ef528SSeigo Tanimura 				if (qp->arg2 != 0) {
464fb0ef528SSeigo Tanimura 					config->ic_nport++;
465fb0ef528SSeigo Tanimura 					config->ic_port[config->ic_nport - 1] = config->ic_port[0];
466fb0ef528SSeigo Tanimura 					config->ic_port[config->ic_nport - 1].ir_start += qp->arg2;
467fb0ef528SSeigo Tanimura 					config->ic_port[config->ic_nport - 1].ir_end += qp->arg2;
468fb0ef528SSeigo Tanimura 				}
469fb0ef528SSeigo Tanimura 				break;
4704249382dSDoug Rabson 			}
4714249382dSDoug Rabson 		}
4724249382dSDoug Rabson 	}
4734249382dSDoug Rabson }
4744249382dSDoug Rabson 
4754249382dSDoug Rabson /*
4764249382dSDoug Rabson  * Scan Resource Data for Logical Devices.
4774249382dSDoug Rabson  *
4784249382dSDoug Rabson  * This function exits as soon as it gets an error reading *ANY*
4795b45337dSDoug Rabson  * Resource Data or it reaches the end of Resource Data.  In the first
4804249382dSDoug Rabson  * case the return value will be TRUE, FALSE otherwise.
4814249382dSDoug Rabson  */
4824249382dSDoug Rabson static int
4835b45337dSDoug Rabson pnp_create_devices(device_t parent, pnp_id *p, int csn,
4845b45337dSDoug Rabson 		   u_char *resources, int len)
4854249382dSDoug Rabson {
4865b45337dSDoug Rabson 	u_char tag, *resp, *resinfo, *startres = 0;
4875b45337dSDoug Rabson 	int large_len, scanning = len, retval = FALSE;
4884249382dSDoug Rabson 	u_int32_t logical_id;
4894249382dSDoug Rabson 	u_int32_t compat_id;
4904249382dSDoug Rabson 	device_t dev = 0;
4914249382dSDoug Rabson 	int ldn = 0;
4924249382dSDoug Rabson 	struct pnp_set_config_arg *csnldn;
4935b45337dSDoug Rabson 	char buf[100];
4944249382dSDoug Rabson 	char *desc = 0;
4954249382dSDoug Rabson 
4965b45337dSDoug Rabson 	resp = resources;
4975b45337dSDoug Rabson 	while (scanning > 0) {
4985b45337dSDoug Rabson 		tag = *resp++;
4995b45337dSDoug Rabson 		scanning--;
5005b45337dSDoug Rabson 		if (PNP_RES_TYPE(tag) != 0) {
5015b45337dSDoug Rabson 			/* Large resource */
5025b45337dSDoug Rabson 			if (scanning < 2) {
5034249382dSDoug Rabson 				scanning = 0;
5044249382dSDoug Rabson 				continue;
5054249382dSDoug Rabson 			}
5065b45337dSDoug Rabson 			large_len = resp[0] + (resp[1] << 8);
5075b45337dSDoug Rabson 			resp += 2;
5085b45337dSDoug Rabson 
5095b45337dSDoug Rabson 			if (scanning < large_len) {
5105b45337dSDoug Rabson 				scanning = 0;
5115b45337dSDoug Rabson 				continue;
5125b45337dSDoug Rabson 			}
5135b45337dSDoug Rabson 			resinfo = resp;
5145b45337dSDoug Rabson 			resp += large_len;
5155b45337dSDoug Rabson 			scanning -= large_len;
5165b45337dSDoug Rabson 
5175b45337dSDoug Rabson 			if (PNP_LRES_NUM(tag) == PNP_TAG_ID_ANSI) {
518d117cb4eSKazutaka YOKOTA 				if (dev) {
519d117cb4eSKazutaka YOKOTA 					/*
520d117cb4eSKazutaka YOKOTA 					 * This is an optional device
521d117cb4eSKazutaka YOKOTA 					 * indentifier string. Skipt it
522d117cb4eSKazutaka YOKOTA 					 * for now.
523d117cb4eSKazutaka YOKOTA 					 */
524d117cb4eSKazutaka YOKOTA 					continue;
525d117cb4eSKazutaka YOKOTA 				}
526d117cb4eSKazutaka YOKOTA 				/* else mandately card identifier string */
5275b45337dSDoug Rabson 				if (large_len > sizeof(buf) - 1)
5285b45337dSDoug Rabson 					large_len = sizeof(buf) - 1;
5295b45337dSDoug Rabson 				bcopy(resinfo, buf, large_len);
5305b45337dSDoug Rabson 
5315b45337dSDoug Rabson 				/*
5325b45337dSDoug Rabson 				 * Trim trailing spaces.
5335b45337dSDoug Rabson 				 */
5345b45337dSDoug Rabson 				while (buf[large_len-1] == ' ')
5355b45337dSDoug Rabson 					large_len--;
5365b45337dSDoug Rabson 				buf[large_len] = '\0';
5375b45337dSDoug Rabson 				desc = buf;
5385b45337dSDoug Rabson 				continue;
5395b45337dSDoug Rabson 			}
5405b45337dSDoug Rabson 
5415b45337dSDoug Rabson 			continue;
5425b45337dSDoug Rabson 		}
5435b45337dSDoug Rabson 
5445b45337dSDoug Rabson 		/* Small resource */
5455b45337dSDoug Rabson 		if (scanning < PNP_SRES_LEN(tag)) {
5465b45337dSDoug Rabson 			scanning = 0;
5475b45337dSDoug Rabson 			continue;
5485b45337dSDoug Rabson 		}
5495b45337dSDoug Rabson 		resinfo = resp;
5505b45337dSDoug Rabson 		resp += PNP_SRES_LEN(tag);
5515b45337dSDoug Rabson 		scanning -= PNP_SRES_LEN(tag);;
5524249382dSDoug Rabson 
5534249382dSDoug Rabson 		switch (PNP_SRES_NUM(tag)) {
55448ab255eSPeter Wemm 		case PNP_TAG_LOGICAL_DEVICE:
5554249382dSDoug Rabson 			/*
5565b45337dSDoug Rabson 			 * Parse the resources for the previous
5575b45337dSDoug Rabson 			 * logical device (if any).
5585b45337dSDoug Rabson 			 */
5595b45337dSDoug Rabson 			if (startres) {
5605b45337dSDoug Rabson 				pnp_parse_resources(dev, startres,
561fb0ef528SSeigo Tanimura 						    resinfo - startres - 1,
562c3959391SKazutaka YOKOTA 						    ldn);
5635b45337dSDoug Rabson 				dev = 0;
5645b45337dSDoug Rabson 				startres = 0;
5655b45337dSDoug Rabson 			}
5665b45337dSDoug Rabson 
5675b45337dSDoug Rabson 			/*
5685b45337dSDoug Rabson 			 * A new logical device. Scan for end of
5695b45337dSDoug Rabson 			 * resources.
5704249382dSDoug Rabson 			 */
5714249382dSDoug Rabson 			bcopy(resinfo, &logical_id, 4);
572fb0ef528SSeigo Tanimura 			pnp_check_quirks(p->vendor_id, logical_id, ldn, NULL);
5734249382dSDoug Rabson 			compat_id = 0;
5745b45337dSDoug Rabson 			dev = BUS_ADD_CHILD(parent, ISA_ORDER_PNP, NULL, -1);
5754249382dSDoug Rabson 			if (desc)
5764249382dSDoug Rabson 				device_set_desc_copy(dev, desc);
577d117cb4eSKazutaka YOKOTA 			else
578d117cb4eSKazutaka YOKOTA 				device_set_desc_copy(dev,
579d117cb4eSKazutaka YOKOTA 						     pnp_eisaformat(logical_id));
5804249382dSDoug Rabson 			isa_set_vendorid(dev, p->vendor_id);
5814249382dSDoug Rabson 			isa_set_serial(dev, p->serial);
5824249382dSDoug Rabson 			isa_set_logicalid(dev, logical_id);
583c3959391SKazutaka YOKOTA 			isa_set_configattr(dev,
584c3959391SKazutaka YOKOTA 					   ISACFGATTR_CANDISABLE |
585c3959391SKazutaka YOKOTA 					   ISACFGATTR_DYNAMIC);
5865b45337dSDoug Rabson 			csnldn = malloc(sizeof *csnldn, M_DEVBUF, M_NOWAIT);
5874249382dSDoug Rabson 			if (!csnldn) {
5884249382dSDoug Rabson 				device_printf(parent,
5894249382dSDoug Rabson 					      "out of memory\n");
5904249382dSDoug Rabson 				scanning = 0;
5914249382dSDoug Rabson 				break;
5924249382dSDoug Rabson 			}
5934249382dSDoug Rabson 			csnldn->csn = csn;
5944249382dSDoug Rabson 			csnldn->ldn = ldn;
5954249382dSDoug Rabson 			ISA_SET_CONFIG_CALLBACK(parent, dev,
5965b45337dSDoug Rabson 						pnp_set_config, csnldn);
5974249382dSDoug Rabson 			ldn++;
5985b45337dSDoug Rabson 			startres = resp;
5994249382dSDoug Rabson 			break;
6004249382dSDoug Rabson 
6014249382dSDoug Rabson 		case PNP_TAG_END:
6025b45337dSDoug Rabson 			if (!startres) {
6035b45337dSDoug Rabson 				device_printf(parent,
6045b45337dSDoug Rabson 					      "malformed resources\n");
6055b45337dSDoug Rabson 				scanning = 0;
6065b45337dSDoug Rabson 				break;
6075b45337dSDoug Rabson 			}
6085b45337dSDoug Rabson 			pnp_parse_resources(dev, startres,
609c3959391SKazutaka YOKOTA 					    resinfo - startres - 1, ldn);
6105b45337dSDoug Rabson 			dev = 0;
6115b45337dSDoug Rabson 			startres = 0;
6124249382dSDoug Rabson 			scanning = 0;
6134249382dSDoug Rabson 			break;
6144249382dSDoug Rabson 
6154249382dSDoug Rabson 		default:
6164249382dSDoug Rabson 			/* Skip this resource */
6174249382dSDoug Rabson 			break;
6184249382dSDoug Rabson 		}
6194249382dSDoug Rabson 	}
6204249382dSDoug Rabson 
6214249382dSDoug Rabson 	return retval;
6224249382dSDoug Rabson }
6234249382dSDoug Rabson 
6244249382dSDoug Rabson /*
6255b45337dSDoug Rabson  * Read 'amount' bytes of resources from the card, allocating memory
6265b45337dSDoug Rabson  * as needed. If a buffer is already available, it should be passed in
6275b45337dSDoug Rabson  * '*resourcesp' and its length in '*spacep'. The number of resource
6285b45337dSDoug Rabson  * bytes already in the buffer should be passed in '*lenp'. The memory
6295b45337dSDoug Rabson  * allocated will be returned in '*resourcesp' with its size and the
6305b45337dSDoug Rabson  * number of bytes of resources in '*spacep' and '*lenp' respectively.
6315b45337dSDoug Rabson  */
6325b45337dSDoug Rabson static int
6335b45337dSDoug Rabson pnp_read_bytes(int amount, u_char **resourcesp, int *spacep, int *lenp)
6345b45337dSDoug Rabson {
6355b45337dSDoug Rabson 	u_char *resources = *resourcesp;
6365b45337dSDoug Rabson 	u_char *newres;
6375b45337dSDoug Rabson 	int space = *spacep;
6385b45337dSDoug Rabson 	int len = *lenp;
6395b45337dSDoug Rabson 
6405b45337dSDoug Rabson 	if (space == 0) {
6415b45337dSDoug Rabson 		space = 1024;
6425b45337dSDoug Rabson 		resources = malloc(space, M_TEMP, M_NOWAIT);
6435b45337dSDoug Rabson 		if (!resources)
6445b45337dSDoug Rabson 			return ENOMEM;
6455b45337dSDoug Rabson 	}
6465b45337dSDoug Rabson 
6475b45337dSDoug Rabson 	if (len + amount > space) {
6485b45337dSDoug Rabson 		int extra = 1024;
6495b45337dSDoug Rabson 		while (len + amount > space + extra)
6505b45337dSDoug Rabson 			extra += 1024;
6515b45337dSDoug Rabson 		newres = malloc(space + extra, M_TEMP, M_NOWAIT);
6525b45337dSDoug Rabson 		if (!newres)
6535b45337dSDoug Rabson 			return ENOMEM;
6545b45337dSDoug Rabson 		bcopy(resources, newres, len);
6555b45337dSDoug Rabson 		free(resources, M_TEMP);
6565b45337dSDoug Rabson 		resources = newres;
6575b45337dSDoug Rabson 		space += extra;
6585b45337dSDoug Rabson 	}
6595b45337dSDoug Rabson 
6605b45337dSDoug Rabson 	if (pnp_get_resource_info(resources + len, amount) != amount)
6615b45337dSDoug Rabson 		return EINVAL;
6625b45337dSDoug Rabson 	len += amount;
6635b45337dSDoug Rabson 
6645b45337dSDoug Rabson 	*resourcesp = resources;
6655b45337dSDoug Rabson 	*spacep = space;
6665b45337dSDoug Rabson 	*lenp = len;
6675b45337dSDoug Rabson 
6685b45337dSDoug Rabson 	return 0;
6695b45337dSDoug Rabson }
6705b45337dSDoug Rabson 
6715b45337dSDoug Rabson /*
6725b45337dSDoug Rabson  * Read all resources from the card, allocating memory as needed. If a
6735b45337dSDoug Rabson  * buffer is already available, it should be passed in '*resourcesp'
6745b45337dSDoug Rabson  * and its length in '*spacep'. The memory allocated will be returned
6755b45337dSDoug Rabson  * in '*resourcesp' with its size and the number of bytes of resources
6765b45337dSDoug Rabson  * in '*spacep' and '*lenp' respectively.
6775b45337dSDoug Rabson  */
6785b45337dSDoug Rabson static int
6795b45337dSDoug Rabson pnp_read_resources(u_char **resourcesp, int *spacep, int *lenp)
6805b45337dSDoug Rabson {
6815b45337dSDoug Rabson 	u_char *resources = *resourcesp;
6825b45337dSDoug Rabson 	int space = *spacep;
6835b45337dSDoug Rabson 	int len = 0;
6845b45337dSDoug Rabson 	int error, done;
6855b45337dSDoug Rabson 	u_char tag;
6865b45337dSDoug Rabson 
6875b45337dSDoug Rabson 	error = 0;
6885b45337dSDoug Rabson 	done = 0;
6895b45337dSDoug Rabson 	while (!done) {
6905b45337dSDoug Rabson 		error = pnp_read_bytes(1, &resources, &space, &len);
6915b45337dSDoug Rabson 		if (error)
6925b45337dSDoug Rabson 			goto out;
6935b45337dSDoug Rabson 		tag = resources[len-1];
6945b45337dSDoug Rabson 		if (PNP_RES_TYPE(tag) == 0) {
6955b45337dSDoug Rabson 			/*
6965b45337dSDoug Rabson 			 * Small resource, read contents.
6975b45337dSDoug Rabson 			 */
6985b45337dSDoug Rabson 			error = pnp_read_bytes(PNP_SRES_LEN(tag),
6995b45337dSDoug Rabson 					       &resources, &space, &len);
7005b45337dSDoug Rabson 			if (error)
7015b45337dSDoug Rabson 				goto out;
7025b45337dSDoug Rabson 			if (PNP_SRES_NUM(tag) == PNP_TAG_END)
7035b45337dSDoug Rabson 				done = 1;
7045b45337dSDoug Rabson 		} else {
7055b45337dSDoug Rabson 			/*
7065b45337dSDoug Rabson 			 * Large resource, read length and contents.
7075b45337dSDoug Rabson 			 */
7085b45337dSDoug Rabson 			error = pnp_read_bytes(2, &resources, &space, &len);
7095b45337dSDoug Rabson 			if (error)
7105b45337dSDoug Rabson 				goto out;
7115b45337dSDoug Rabson 			error = pnp_read_bytes(resources[len-2]
7125b45337dSDoug Rabson 					       + (resources[len-1] << 8),
7135b45337dSDoug Rabson 					       &resources, &space, &len);
7145b45337dSDoug Rabson 			if (error)
7155b45337dSDoug Rabson 				goto out;
7165b45337dSDoug Rabson 		}
7175b45337dSDoug Rabson 	}
7185b45337dSDoug Rabson 
7195b45337dSDoug Rabson  out:
7205b45337dSDoug Rabson 	*resourcesp = resources;
7215b45337dSDoug Rabson 	*spacep = space;
7225b45337dSDoug Rabson 	*lenp = len;
7235b45337dSDoug Rabson 	return error;
7245b45337dSDoug Rabson }
7255b45337dSDoug Rabson 
7265b45337dSDoug Rabson /*
7274249382dSDoug Rabson  * Run the isolation protocol. Use pnp_rd_port as the READ_DATA port
7284249382dSDoug Rabson  * value (caller should try multiple READ_DATA locations before giving
7294249382dSDoug Rabson  * up). Upon exiting, all cards are aware that they should use
7304249382dSDoug Rabson  * pnp_rd_port as the READ_DATA port.
7314249382dSDoug Rabson  *
7324249382dSDoug Rabson  * In the first pass, a csn is assigned to each board and pnp_id's
7334249382dSDoug Rabson  * are saved to an array, pnp_devices. In the second pass, each
7344249382dSDoug Rabson  * card is woken up and the device configuration is called.
7354249382dSDoug Rabson  */
7364249382dSDoug Rabson static int
7374249382dSDoug Rabson pnp_isolation_protocol(device_t parent)
7384249382dSDoug Rabson {
7394249382dSDoug Rabson 	int csn;
7404249382dSDoug Rabson 	pnp_id id;
7415b45337dSDoug Rabson 	int found = 0, len;
7425b45337dSDoug Rabson 	u_char *resources = 0;
7435b45337dSDoug Rabson 	int space = 0;
7445b45337dSDoug Rabson 	int error;
7454249382dSDoug Rabson 
7464249382dSDoug Rabson 	/*
7474249382dSDoug Rabson 	 * Put all cards into the Sleep state so that we can clear
7484249382dSDoug Rabson 	 * their CSNs.
7494249382dSDoug Rabson 	 */
7504249382dSDoug Rabson 	pnp_send_initiation_key();
7514249382dSDoug Rabson 
7524249382dSDoug Rabson 	/*
7534249382dSDoug Rabson 	 * Clear the CSN for all cards.
7544249382dSDoug Rabson 	 */
7554249382dSDoug Rabson 	pnp_write(PNP_CONFIG_CONTROL, PNP_CONFIG_CONTROL_RESET_CSN);
7564249382dSDoug Rabson 
7574249382dSDoug Rabson 	/*
7584249382dSDoug Rabson 	 * Move all cards to the Isolation state.
7594249382dSDoug Rabson 	 */
7604249382dSDoug Rabson 	pnp_write(PNP_WAKE, 0);
7614249382dSDoug Rabson 
7624249382dSDoug Rabson 	/*
7634249382dSDoug Rabson 	 * Tell them where the read point is going to be this time.
7644249382dSDoug Rabson 	 */
7654249382dSDoug Rabson 	pnp_write(PNP_SET_RD_DATA, pnp_rd_port);
7664249382dSDoug Rabson 
7674249382dSDoug Rabson 	for (csn = 1; csn < PNP_MAX_CARDS; csn++) {
7684249382dSDoug Rabson 		/*
7694249382dSDoug Rabson 		 * Start the serial isolation protocol.
7704249382dSDoug Rabson 		 */
7714249382dSDoug Rabson 		outb(_PNP_ADDRESS, PNP_SERIAL_ISOLATION);
7724249382dSDoug Rabson 		DELAY(1000);	/* Delay 1 msec */
7734249382dSDoug Rabson 
7744249382dSDoug Rabson 		if (pnp_get_serial(&id)) {
7754249382dSDoug Rabson 			/*
7764249382dSDoug Rabson 			 * We have read the id from a card
7774249382dSDoug Rabson 			 * successfully. The card which won the
7784249382dSDoug Rabson 			 * isolation protocol will be in Isolation
7795b45337dSDoug Rabson 			 * mode and all others will be in Sleep.
7804249382dSDoug Rabson 			 * Program the CSN of the isolated card
7814249382dSDoug Rabson 			 * (taking it to Config state) and read its
7824249382dSDoug Rabson 			 * resources, creating devices as we find
7834249382dSDoug Rabson 			 * logical devices on the card.
7844249382dSDoug Rabson 			 */
7854249382dSDoug Rabson 			pnp_write(PNP_SET_CSN, csn);
7865b45337dSDoug Rabson 			error = pnp_read_resources(&resources,
7875b45337dSDoug Rabson 						   &space,
7885b45337dSDoug Rabson 						   &len);
7895b45337dSDoug Rabson 			if (error)
7905b45337dSDoug Rabson 				break;
7915b45337dSDoug Rabson 			pnp_create_devices(parent, &id, csn,
7925b45337dSDoug Rabson 					   resources, len);
7934249382dSDoug Rabson 			found++;
7944249382dSDoug Rabson 		} else
7954249382dSDoug Rabson 			break;
7964249382dSDoug Rabson 
7974249382dSDoug Rabson 		/*
7984249382dSDoug Rabson 		 * Put this card back to the Sleep state and
7994249382dSDoug Rabson 		 * simultaneously move all cards which don't have a
8004249382dSDoug Rabson 		 * CSN yet to Isolation state.
8014249382dSDoug Rabson 		 */
8024249382dSDoug Rabson 		pnp_write(PNP_WAKE, 0);
8034249382dSDoug Rabson 	}
8044249382dSDoug Rabson 
8054249382dSDoug Rabson 	/*
8064249382dSDoug Rabson 	 * Unless we have chosen the wrong read port, all cards will
8074249382dSDoug Rabson 	 * be in Sleep state. Put them back into WaitForKey for
8084249382dSDoug Rabson 	 * now. Their resources will be programmed later.
8094249382dSDoug Rabson 	 */
8104249382dSDoug Rabson 	pnp_write(PNP_CONFIG_CONTROL, PNP_CONFIG_CONTROL_WAIT_FOR_KEY);
8114249382dSDoug Rabson 
8125b45337dSDoug Rabson 	/*
8135b45337dSDoug Rabson 	 * Cleanup.
8145b45337dSDoug Rabson 	 */
8155b45337dSDoug Rabson 	if (resources)
8165b45337dSDoug Rabson 		free(resources, M_TEMP);
8175b45337dSDoug Rabson 
8184249382dSDoug Rabson 	return found;
8194249382dSDoug Rabson }
8204249382dSDoug Rabson 
8214249382dSDoug Rabson 
8224249382dSDoug Rabson /*
8234249382dSDoug Rabson  * pnp_identify()
8244249382dSDoug Rabson  *
8254249382dSDoug Rabson  * autoconfiguration of pnp devices. This routine just runs the
8264249382dSDoug Rabson  * isolation protocol over several ports, until one is successful.
8274249382dSDoug Rabson  *
8284249382dSDoug Rabson  * may be called more than once ?
8294249382dSDoug Rabson  *
8304249382dSDoug Rabson  */
8314249382dSDoug Rabson 
8324249382dSDoug Rabson static void
8334249382dSDoug Rabson pnp_identify(driver_t *driver, device_t parent)
8344249382dSDoug Rabson {
8354249382dSDoug Rabson 	int num_pnp_devs;
8364249382dSDoug Rabson 
8374249382dSDoug Rabson #if 0
8384249382dSDoug Rabson 	if (pnp_ldn_overrides[0].csn == 0) {
8394249382dSDoug Rabson 		if (bootverbose)
8404249382dSDoug Rabson 			printf("Initializing PnP override table\n");
8414249382dSDoug Rabson 		bzero (pnp_ldn_overrides, sizeof(pnp_ldn_overrides));
8424249382dSDoug Rabson 		pnp_ldn_overrides[0].csn = 255 ;
8434249382dSDoug Rabson 	}
8444249382dSDoug Rabson #endif
8454249382dSDoug Rabson 
8464249382dSDoug Rabson 	/* Try various READ_DATA ports from 0x203-0x3ff */
8474249382dSDoug Rabson 	for (pnp_rd_port = 0x80; (pnp_rd_port < 0xff); pnp_rd_port += 0x10) {
8484249382dSDoug Rabson 		if (bootverbose)
8494249382dSDoug Rabson 			printf("Trying Read_Port at %x\n", (pnp_rd_port << 2) | 0x3);
8504249382dSDoug Rabson 
8514249382dSDoug Rabson 		num_pnp_devs = pnp_isolation_protocol(parent);
8524249382dSDoug Rabson 		if (num_pnp_devs)
8534249382dSDoug Rabson 			break;
8544249382dSDoug Rabson 	}
8554249382dSDoug Rabson }
8564249382dSDoug Rabson 
8574249382dSDoug Rabson static device_method_t pnp_methods[] = {
8584249382dSDoug Rabson 	/* Device interface */
8594249382dSDoug Rabson 	DEVMETHOD(device_identify,	pnp_identify),
8604249382dSDoug Rabson 
8614249382dSDoug Rabson 	{ 0, 0 }
8624249382dSDoug Rabson };
8634249382dSDoug Rabson 
8644249382dSDoug Rabson static driver_t pnp_driver = {
8654249382dSDoug Rabson 	"pnp",
8664249382dSDoug Rabson 	pnp_methods,
8674249382dSDoug Rabson 	1,			/* no softc */
8684249382dSDoug Rabson };
8694249382dSDoug Rabson 
8704249382dSDoug Rabson static devclass_t pnp_devclass;
8714249382dSDoug Rabson 
8724249382dSDoug Rabson DRIVER_MODULE(pnp, isa, pnp_driver, pnp_devclass, 0, 0);
873