xref: /freebsd/sys/isa/pnp.c (revision 30f5ad0faf9ef227fd664841d0811aae433f692d)
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 
947698537bSYoshihiro Takahashi #ifdef PC98
957698537bSYoshihiro Takahashi /* Some NEC PnP cards have 9 bytes serial code. */
967698537bSYoshihiro Takahashi static pnp_id necids[] = {
977698537bSYoshihiro Takahashi 	{0x4180a3b8, 0xffffffff, 0x00},	/* PC-9801CB-B04 (NEC8041) */
987698537bSYoshihiro Takahashi 	{0x5181a3b8, 0xffffffff, 0x46},	/* PC-9821CB2-B04(NEC8151) */
997698537bSYoshihiro Takahashi 	{0x5182a3b8, 0xffffffff, 0xb8},	/* PC-9801-XX    (NEC8251) */
1007698537bSYoshihiro Takahashi 	{0x9181a3b8, 0xffffffff, 0x00},	/* PC-9801-120   (NEC8191) */
1017698537bSYoshihiro Takahashi 	{0, 0, 0}
1027698537bSYoshihiro Takahashi };
1037698537bSYoshihiro Takahashi #endif
1047698537bSYoshihiro Takahashi 
1054249382dSDoug Rabson #if 0
1064249382dSDoug Rabson /*
1074249382dSDoug Rabson  * these entries are initialized using the autoconfig menu
1084249382dSDoug Rabson  * The struct is invalid (and must be initialized) if the first
1094249382dSDoug Rabson  * CSN is zero. The init code fills invalid entries with CSN 255
1104249382dSDoug Rabson  * which is not a supported value.
1114249382dSDoug Rabson  */
1124249382dSDoug Rabson 
1134249382dSDoug Rabson struct pnp_cinfo pnp_ldn_overrides[MAX_PNP_LDN] = {
1144249382dSDoug Rabson     { 0 }
1154249382dSDoug Rabson };
1164249382dSDoug Rabson #endif
1174249382dSDoug Rabson 
1184249382dSDoug Rabson /* The READ_DATA port that we are using currently */
1194249382dSDoug Rabson static int pnp_rd_port;
1204249382dSDoug Rabson 
1214249382dSDoug Rabson static void   pnp_send_initiation_key(void);
1224249382dSDoug Rabson static int    pnp_get_serial(pnp_id *p);
1234249382dSDoug Rabson static int    pnp_isolation_protocol(device_t parent);
1244249382dSDoug Rabson 
125bb2b9030SDoug Rabson char *
126bb2b9030SDoug Rabson pnp_eisaformat(u_int32_t id)
127bb2b9030SDoug Rabson {
128bb2b9030SDoug Rabson 	u_int8_t *data = (u_int8_t *) &id;
129bb2b9030SDoug Rabson 	static char idbuf[8];
130bb2b9030SDoug Rabson 	const char  hextoascii[] = "0123456789abcdef";
131bb2b9030SDoug Rabson 
132bb2b9030SDoug Rabson 	idbuf[0] = '@' + ((data[0] & 0x7c) >> 2);
133bb2b9030SDoug Rabson 	idbuf[1] = '@' + (((data[0] & 0x3) << 3) + ((data[1] & 0xe0) >> 5));
134bb2b9030SDoug Rabson 	idbuf[2] = '@' + (data[1] & 0x1f);
135bb2b9030SDoug Rabson 	idbuf[3] = hextoascii[(data[2] >> 4)];
136bb2b9030SDoug Rabson 	idbuf[4] = hextoascii[(data[2] & 0xf)];
137bb2b9030SDoug Rabson 	idbuf[5] = hextoascii[(data[3] >> 4)];
138bb2b9030SDoug Rabson 	idbuf[6] = hextoascii[(data[3] & 0xf)];
139bb2b9030SDoug Rabson 	idbuf[7] = 0;
140bb2b9030SDoug Rabson 	return(idbuf);
141bb2b9030SDoug Rabson }
142bb2b9030SDoug Rabson 
1434249382dSDoug Rabson static void
1444249382dSDoug Rabson pnp_write(int d, u_char r)
1454249382dSDoug Rabson {
1464249382dSDoug Rabson 	outb (_PNP_ADDRESS, d);
1474249382dSDoug Rabson 	outb (_PNP_WRITE_DATA, r);
1484249382dSDoug Rabson }
1494249382dSDoug Rabson 
1504249382dSDoug Rabson #if 0
1514249382dSDoug Rabson 
1524249382dSDoug Rabson static u_char
1534249382dSDoug Rabson pnp_read(int d)
1544249382dSDoug Rabson {
1554249382dSDoug Rabson 	outb (_PNP_ADDRESS, d);
1564249382dSDoug Rabson 	return (inb(3 | (pnp_rd_port <<2)));
1574249382dSDoug Rabson }
1584249382dSDoug Rabson 
1594249382dSDoug Rabson #endif
1604249382dSDoug Rabson 
1614249382dSDoug Rabson /*
1624249382dSDoug Rabson  * Send Initiation LFSR as described in "Plug and Play ISA Specification",
1634249382dSDoug Rabson  * Intel May 94.
1644249382dSDoug Rabson  */
1654249382dSDoug Rabson static void
1664249382dSDoug Rabson pnp_send_initiation_key()
1674249382dSDoug Rabson {
1684249382dSDoug Rabson 	int cur, i;
1694249382dSDoug Rabson 
1704249382dSDoug Rabson 	/* Reset the LSFR */
1714249382dSDoug Rabson 	outb(_PNP_ADDRESS, 0);
1724249382dSDoug Rabson 	outb(_PNP_ADDRESS, 0); /* yes, we do need it twice! */
1734249382dSDoug Rabson 
1744249382dSDoug Rabson 	cur = 0x6a;
1754249382dSDoug Rabson 	outb(_PNP_ADDRESS, cur);
1764249382dSDoug Rabson 
1774249382dSDoug Rabson 	for (i = 1; i < 32; i++) {
1784249382dSDoug Rabson 		cur = (cur >> 1) | (((cur ^ (cur >> 1)) << 7) & 0xff);
1794249382dSDoug Rabson 		outb(_PNP_ADDRESS, cur);
1804249382dSDoug Rabson 	}
1814249382dSDoug Rabson }
1824249382dSDoug Rabson 
1834249382dSDoug Rabson 
1844249382dSDoug Rabson /*
1854249382dSDoug Rabson  * Get the device's serial number.  Returns 1 if the serial is valid.
1864249382dSDoug Rabson  */
1874249382dSDoug Rabson static int
1884249382dSDoug Rabson pnp_get_serial(pnp_id *p)
1894249382dSDoug Rabson {
1904249382dSDoug Rabson 	int i, bit, valid = 0, sum = 0x6a;
1914249382dSDoug Rabson 	u_char *data = (u_char *)p;
1924249382dSDoug Rabson 
1934249382dSDoug Rabson 	bzero(data, sizeof(char) * 9);
1944249382dSDoug Rabson 	outb(_PNP_ADDRESS, PNP_SERIAL_ISOLATION);
1954249382dSDoug Rabson 	for (i = 0; i < 72; i++) {
1964249382dSDoug Rabson 		bit = inb((pnp_rd_port << 2) | 0x3) == 0x55;
1974249382dSDoug Rabson 		DELAY(250);	/* Delay 250 usec */
1984249382dSDoug Rabson 
1994249382dSDoug Rabson 		/* Can't Short Circuit the next evaluation, so 'and' is last */
2004249382dSDoug Rabson 		bit = (inb((pnp_rd_port << 2) | 0x3) == 0xaa) && bit;
2014249382dSDoug Rabson 		DELAY(250);	/* Delay 250 usec */
2024249382dSDoug Rabson 
2034249382dSDoug Rabson 		valid = valid || bit;
2044249382dSDoug Rabson 
2054249382dSDoug Rabson 		if (i < 64)
2064249382dSDoug Rabson 			sum = (sum >> 1) |
2074249382dSDoug Rabson 				(((sum ^ (sum >> 1) ^ bit) << 7) & 0xff);
2084249382dSDoug Rabson 
2094249382dSDoug Rabson 		data[i / 8] = (data[i / 8] >> 1) | (bit ? 0x80 : 0);
2104249382dSDoug Rabson 	}
2114249382dSDoug Rabson 
2124249382dSDoug Rabson 	valid = valid && (data[8] == sum);
2134249382dSDoug Rabson 
2144249382dSDoug Rabson 	return valid;
2154249382dSDoug Rabson }
2164249382dSDoug Rabson 
2174249382dSDoug Rabson /*
2184249382dSDoug Rabson  * Fill's the buffer with resource info from the device.
2195b45337dSDoug Rabson  * Returns the number of characters read.
2204249382dSDoug Rabson  */
2214249382dSDoug Rabson static int
2224249382dSDoug Rabson pnp_get_resource_info(u_char *buffer, int len)
2234249382dSDoug Rabson {
2245b45337dSDoug Rabson 	int i, j, count;
2254249382dSDoug Rabson 	u_char temp;
2264249382dSDoug Rabson 
2275b45337dSDoug Rabson 	count = 0;
2284249382dSDoug Rabson 	for (i = 0; i < len; i++) {
2294249382dSDoug Rabson 		outb(_PNP_ADDRESS, PNP_STATUS);
2304249382dSDoug Rabson 		for (j = 0; j < 100; j++) {
2314249382dSDoug Rabson 			if ((inb((pnp_rd_port << 2) | 0x3)) & 0x1)
2324249382dSDoug Rabson 				break;
2334249382dSDoug Rabson 			DELAY(1);
2344249382dSDoug Rabson 		}
2354249382dSDoug Rabson 		if (j == 100) {
2364249382dSDoug Rabson 			printf("PnP device failed to report resource data\n");
2375b45337dSDoug Rabson 			return count;
2384249382dSDoug Rabson 		}
2394249382dSDoug Rabson 		outb(_PNP_ADDRESS, PNP_RESOURCE_DATA);
2404249382dSDoug Rabson 		temp = inb((pnp_rd_port << 2) | 0x3);
2414249382dSDoug Rabson 		if (buffer != NULL)
2424249382dSDoug Rabson 			buffer[i] = temp;
2435b45337dSDoug Rabson 		count++;
2444249382dSDoug Rabson 	}
2455b45337dSDoug Rabson 	return count;
2464249382dSDoug Rabson }
2474249382dSDoug Rabson 
2484249382dSDoug Rabson #if 0
2494249382dSDoug Rabson /*
2504249382dSDoug Rabson  * write_pnp_parms initializes a logical device with the parms
2514249382dSDoug Rabson  * in d, and then activates the board if the last parameter is 1.
2524249382dSDoug Rabson  */
2534249382dSDoug Rabson 
2544249382dSDoug Rabson static int
2554249382dSDoug Rabson write_pnp_parms(struct pnp_cinfo *d, pnp_id *p, int ldn)
2564249382dSDoug Rabson {
2574249382dSDoug Rabson     int i, empty = -1 ;
2584249382dSDoug Rabson 
2594249382dSDoug Rabson     pnp_write (SET_LDN, ldn );
2604249382dSDoug Rabson     i = pnp_read(SET_LDN) ;
2614249382dSDoug Rabson     if (i != ldn) {
2624249382dSDoug Rabson 	printf("Warning: LDN %d does not exist\n", ldn);
2634249382dSDoug Rabson     }
2644249382dSDoug Rabson     for (i = 0; i < 8; i++) {
2654249382dSDoug Rabson 	pnp_write(IO_CONFIG_BASE + i * 2, d->ic_port[i] >> 8 );
2664249382dSDoug Rabson 	pnp_write(IO_CONFIG_BASE + i * 2 + 1, d->ic_port[i] & 0xff );
2674249382dSDoug Rabson     }
2684249382dSDoug Rabson     for (i = 0; i < 4; i++) {
2694249382dSDoug Rabson 	pnp_write(MEM_CONFIG + i*8, (d->ic_mem[i].base >> 16) & 0xff );
2704249382dSDoug Rabson 	pnp_write(MEM_CONFIG + i*8+1, (d->ic_mem[i].base >> 8) & 0xff );
2714249382dSDoug Rabson 	pnp_write(MEM_CONFIG + i*8+2, d->ic_mem[i].control & 0xff );
2724249382dSDoug Rabson 	pnp_write(MEM_CONFIG + i*8+3, (d->ic_mem[i].range >> 16) & 0xff );
2734249382dSDoug Rabson 	pnp_write(MEM_CONFIG + i*8+4, (d->ic_mem[i].range >> 8) & 0xff );
2744249382dSDoug Rabson     }
2754249382dSDoug Rabson     for (i = 0; i < 2; i++) {
2764249382dSDoug Rabson 	pnp_write(IRQ_CONFIG + i*2    , d->irq[i] );
2774249382dSDoug Rabson 	pnp_write(IRQ_CONFIG + i*2 + 1, d->irq_type[i] );
2784249382dSDoug Rabson 	pnp_write(DRQ_CONFIG + i, d->drq[i] );
2794249382dSDoug Rabson     }
2804249382dSDoug Rabson     /*
2814249382dSDoug Rabson      * store parameters read into the current kernel
2824249382dSDoug Rabson      * so manual editing next time is easier
2834249382dSDoug Rabson      */
2844249382dSDoug Rabson     for (i = 0 ; i < MAX_PNP_LDN; i++) {
2854249382dSDoug Rabson 	if (pnp_ldn_overrides[i].csn == d->csn &&
2864249382dSDoug Rabson 		pnp_ldn_overrides[i].ldn == ldn) {
2874249382dSDoug Rabson 	    d->flags = pnp_ldn_overrides[i].flags ;
2884249382dSDoug Rabson 	    pnp_ldn_overrides[i] = *d ;
2894249382dSDoug Rabson 	    break ;
2904249382dSDoug Rabson 	} else if (pnp_ldn_overrides[i].csn < 1 ||
2914249382dSDoug Rabson 		pnp_ldn_overrides[i].csn == 255)
2924249382dSDoug Rabson 	    empty = i ;
2934249382dSDoug Rabson     }
2944249382dSDoug Rabson     if (i== MAX_PNP_LDN && empty != -1)
2954249382dSDoug Rabson 	pnp_ldn_overrides[empty] = *d;
2964249382dSDoug Rabson 
2974249382dSDoug Rabson     /*
2984249382dSDoug Rabson      * Here should really perform the range check, and
2994249382dSDoug Rabson      * return a failure if not successful.
3004249382dSDoug Rabson      */
3014249382dSDoug Rabson     pnp_write (IO_RANGE_CHECK, 0);
3024249382dSDoug Rabson     DELAY(1000); /* XXX is it really necessary ? */
3034249382dSDoug Rabson     pnp_write (ACTIVATE, d->enable ? 1 : 0);
3044249382dSDoug Rabson     DELAY(1000); /* XXX is it really necessary ? */
3054249382dSDoug Rabson     return 1 ;
3064249382dSDoug Rabson }
3074249382dSDoug Rabson #endif
3084249382dSDoug Rabson 
3094249382dSDoug Rabson /*
3104249382dSDoug Rabson  * This function is called after the bus has assigned resource
3114249382dSDoug Rabson  * locations for a logical device.
3124249382dSDoug Rabson  */
3134249382dSDoug Rabson static void
3144249382dSDoug Rabson pnp_set_config(void *arg, struct isa_config *config, int enable)
3154249382dSDoug Rabson {
3164249382dSDoug Rabson 	int csn = ((struct pnp_set_config_arg *) arg)->csn;
3174249382dSDoug Rabson 	int ldn = ((struct pnp_set_config_arg *) arg)->ldn;
3184249382dSDoug Rabson 	int i;
3194249382dSDoug Rabson 
3204249382dSDoug Rabson 	/*
3214249382dSDoug Rabson 	 * First put all cards into Sleep state with the initiation
3224249382dSDoug Rabson 	 * key, then put our card into Config state.
3234249382dSDoug Rabson 	 */
3244249382dSDoug Rabson 	pnp_send_initiation_key();
3254249382dSDoug Rabson 	pnp_write(PNP_WAKE, csn);
3264249382dSDoug Rabson 
3274249382dSDoug Rabson 	/*
3284249382dSDoug Rabson 	 * Select our logical device so that we can program it.
3294249382dSDoug Rabson 	 */
3304249382dSDoug Rabson 	pnp_write(PNP_SET_LDN, ldn);
3314249382dSDoug Rabson 
3324249382dSDoug Rabson 	/*
33308764b84SMike Smith 	 * Constrain the number of resources we will try to program
33408764b84SMike Smith 	 */
33508764b84SMike Smith 	if (config->ic_nmem > ISA_PNP_NMEM) {
33608764b84SMike Smith 	    printf("too many ISA memory ranges (%d > %d)\n", config->ic_nmem, ISA_PNP_NMEM);
33708764b84SMike Smith 	    config->ic_nmem = ISA_PNP_NMEM;
33808764b84SMike Smith 	}
33908764b84SMike Smith 	if (config->ic_nport > ISA_PNP_NPORT) {
34008764b84SMike Smith 	    printf("too many ISA I/O ranges (%d > %d)\n", config->ic_nport, ISA_PNP_NPORT);
34108764b84SMike Smith 	    config->ic_nport = ISA_PNP_NPORT;
34208764b84SMike Smith 	}
34308764b84SMike Smith 	if (config->ic_nirq > ISA_PNP_NIRQ) {
34408764b84SMike Smith 	    printf("too many ISA IRQs (%d > %d)\n", config->ic_nirq, ISA_PNP_NIRQ);
34508764b84SMike Smith 	    config->ic_nirq = ISA_PNP_NIRQ;
34608764b84SMike Smith 	}
34708764b84SMike Smith 	if (config->ic_ndrq > ISA_PNP_NDRQ) {
34808764b84SMike Smith 	    printf("too many ISA DRQs (%d > %d)\n", config->ic_ndrq, ISA_PNP_NDRQ);
34908764b84SMike Smith 	    config->ic_ndrq = ISA_PNP_NDRQ;
35008764b84SMike Smith 	}
35108764b84SMike Smith 
35208764b84SMike Smith 	/*
3534249382dSDoug Rabson 	 * Now program the resources.
3544249382dSDoug Rabson 	 */
3554249382dSDoug Rabson 	for (i = 0; i < config->ic_nmem; i++) {
356c3959391SKazutaka YOKOTA 		u_int32_t start;
357c3959391SKazutaka YOKOTA 		u_int32_t size;
358c3959391SKazutaka YOKOTA 
359c3959391SKazutaka YOKOTA 		/* XXX: should handle memory control register, 32 bit memory */
360c3959391SKazutaka YOKOTA 		if (config->ic_mem[i].ir_size == 0) {
361c3959391SKazutaka YOKOTA 			pnp_write(PNP_MEM_BASE_HIGH(i), 0);
362c3959391SKazutaka YOKOTA 			pnp_write(PNP_MEM_BASE_LOW(i), 0);
363c3959391SKazutaka YOKOTA 			pnp_write(PNP_MEM_RANGE_HIGH(i), 0);
364c3959391SKazutaka YOKOTA 			pnp_write(PNP_MEM_RANGE_LOW(i), 0);
365c3959391SKazutaka YOKOTA 		} else {
366c3959391SKazutaka YOKOTA 			start = config->ic_mem[i].ir_start;
367c3959391SKazutaka YOKOTA 			size =  config->ic_mem[i].ir_size;
3684249382dSDoug Rabson 			if (start & 0xff)
3694249382dSDoug Rabson 				panic("pnp_set_config: bogus memory assignment");
3704249382dSDoug Rabson 			pnp_write(PNP_MEM_BASE_HIGH(i), (start >> 16) & 0xff);
3714249382dSDoug Rabson 			pnp_write(PNP_MEM_BASE_LOW(i), (start >> 8) & 0xff);
3724249382dSDoug Rabson 			pnp_write(PNP_MEM_RANGE_HIGH(i), (size >> 16) & 0xff);
3734249382dSDoug Rabson 			pnp_write(PNP_MEM_RANGE_LOW(i), (size >> 8) & 0xff);
3744249382dSDoug Rabson 		}
375c3959391SKazutaka YOKOTA 	}
37608764b84SMike Smith 	for (; i < ISA_PNP_NMEM; i++) {
3774249382dSDoug Rabson 		pnp_write(PNP_MEM_BASE_HIGH(i), 0);
3784249382dSDoug Rabson 		pnp_write(PNP_MEM_BASE_LOW(i), 0);
3794249382dSDoug Rabson 		pnp_write(PNP_MEM_RANGE_HIGH(i), 0);
3804249382dSDoug Rabson 		pnp_write(PNP_MEM_RANGE_LOW(i), 0);
3814249382dSDoug Rabson 	}
3824249382dSDoug Rabson 
3834249382dSDoug Rabson 	for (i = 0; i < config->ic_nport; i++) {
384c3959391SKazutaka YOKOTA 		u_int32_t start;
385c3959391SKazutaka YOKOTA 
386c3959391SKazutaka YOKOTA 		if (config->ic_port[i].ir_size == 0) {
387c3959391SKazutaka YOKOTA 			pnp_write(PNP_IO_BASE_HIGH(i), 0);
388c3959391SKazutaka YOKOTA 			pnp_write(PNP_IO_BASE_LOW(i), 0);
389c3959391SKazutaka YOKOTA 		} else {
390c3959391SKazutaka YOKOTA 			start = config->ic_port[i].ir_start;
3914249382dSDoug Rabson 			pnp_write(PNP_IO_BASE_HIGH(i), (start >> 8) & 0xff);
3924249382dSDoug Rabson 			pnp_write(PNP_IO_BASE_LOW(i), (start >> 0) & 0xff);
3934249382dSDoug Rabson 		}
394c3959391SKazutaka YOKOTA 	}
39508764b84SMike Smith 	for (; i < ISA_PNP_NPORT; i++) {
3964249382dSDoug Rabson 		pnp_write(PNP_IO_BASE_HIGH(i), 0);
3974249382dSDoug Rabson 		pnp_write(PNP_IO_BASE_LOW(i), 0);
3984249382dSDoug Rabson 	}
3994249382dSDoug Rabson 
4004249382dSDoug Rabson 	for (i = 0; i < config->ic_nirq; i++) {
401c3959391SKazutaka YOKOTA 		int irq;
402c3959391SKazutaka YOKOTA 
403c3959391SKazutaka YOKOTA 		/* XXX: interrupt type */
404c3959391SKazutaka YOKOTA 		if (config->ic_irqmask[i] == 0) {
405c3959391SKazutaka YOKOTA 			pnp_write(PNP_IRQ_LEVEL(i), 0);
406c3959391SKazutaka YOKOTA 			pnp_write(PNP_IRQ_TYPE(i), 2);
407c3959391SKazutaka YOKOTA 		} else {
408c3959391SKazutaka YOKOTA 			irq = ffs(config->ic_irqmask[i]) - 1;
4094249382dSDoug Rabson 			pnp_write(PNP_IRQ_LEVEL(i), irq);
4104249382dSDoug Rabson 			pnp_write(PNP_IRQ_TYPE(i), 2); /* XXX */
4114249382dSDoug Rabson 		}
412c3959391SKazutaka YOKOTA 	}
41308764b84SMike Smith 	for (; i < ISA_PNP_NIRQ; i++) {
4144249382dSDoug Rabson 		/*
4154249382dSDoug Rabson 		 * IRQ 0 is not a valid interrupt selection and
4164249382dSDoug Rabson 		 * represents no interrupt selection.
4174249382dSDoug Rabson 		 */
4184249382dSDoug Rabson 		pnp_write(PNP_IRQ_LEVEL(i), 0);
419c3959391SKazutaka YOKOTA 		pnp_write(PNP_IRQ_TYPE(i), 2);
4204249382dSDoug Rabson 	}
4214249382dSDoug Rabson 
4224249382dSDoug Rabson 	for (i = 0; i < config->ic_ndrq; i++) {
423c3959391SKazutaka YOKOTA 		int drq;
424c3959391SKazutaka YOKOTA 
425c3959391SKazutaka YOKOTA 		if (config->ic_drqmask[i] == 0) {
426c3959391SKazutaka YOKOTA 			pnp_write(PNP_DMA_CHANNEL(i), 4);
427c3959391SKazutaka YOKOTA 		} else {
428c3959391SKazutaka YOKOTA 			drq = ffs(config->ic_drqmask[i]) - 1;
4294249382dSDoug Rabson 			pnp_write(PNP_DMA_CHANNEL(i), drq);
4304249382dSDoug Rabson 		}
431c3959391SKazutaka YOKOTA 	}
43208764b84SMike Smith 	for (; i < ISA_PNP_NDRQ; i++) {
4334249382dSDoug Rabson 		/*
4344249382dSDoug Rabson 		 * DMA channel 4, the cascade channel is used to
4354249382dSDoug Rabson 		 * indicate no DMA channel is active.
4364249382dSDoug Rabson 		 */
4374249382dSDoug Rabson 		pnp_write(PNP_DMA_CHANNEL(i), 4);
4384249382dSDoug Rabson 	}
4394249382dSDoug Rabson 
4404249382dSDoug Rabson 	pnp_write(PNP_ACTIVATE, enable ? 1 : 0);
4414249382dSDoug Rabson 
4424249382dSDoug Rabson 	/*
4434249382dSDoug Rabson 	 * Wake everyone up again, we are finished.
4444249382dSDoug Rabson 	 */
4454249382dSDoug Rabson 	pnp_write(PNP_CONFIG_CONTROL, PNP_CONFIG_CONTROL_WAIT_FOR_KEY);
4464249382dSDoug Rabson }
4474249382dSDoug Rabson 
4484249382dSDoug Rabson /*
4494249382dSDoug Rabson  * Process quirks for a logical device.. The card must be in Config state.
4504249382dSDoug Rabson  */
451fb0ef528SSeigo Tanimura void
452fb0ef528SSeigo Tanimura pnp_check_quirks(u_int32_t vendor_id, u_int32_t logical_id, int ldn, struct isa_config *config)
4534249382dSDoug Rabson {
4544249382dSDoug Rabson 	struct pnp_quirk *qp;
4554249382dSDoug Rabson 
4564249382dSDoug Rabson 	for (qp = &pnp_quirks[0]; qp->vendor_id; qp++) {
4574249382dSDoug Rabson 		if (qp->vendor_id == vendor_id
4584249382dSDoug Rabson 		    && (qp->logical_id == 0
4594249382dSDoug Rabson 			|| qp->logical_id == logical_id)) {
4604249382dSDoug Rabson 			switch (qp->type) {
4614249382dSDoug Rabson 			case PNP_QUIRK_WRITE_REG:
4624249382dSDoug Rabson 				pnp_write(PNP_SET_LDN, ldn);
4634249382dSDoug Rabson 				pnp_write(qp->arg1, qp->arg2);
4644249382dSDoug Rabson 				break;
465fb0ef528SSeigo Tanimura 			case PNP_QUIRK_EXTRA_IO:
466fb0ef528SSeigo Tanimura 				if (config == NULL)
467fb0ef528SSeigo Tanimura 					break;
468fb0ef528SSeigo Tanimura 				if (qp->arg1 != 0) {
469fb0ef528SSeigo Tanimura 					config->ic_nport++;
470fb0ef528SSeigo Tanimura 					config->ic_port[config->ic_nport - 1] = config->ic_port[0];
471fb0ef528SSeigo Tanimura 					config->ic_port[config->ic_nport - 1].ir_start += qp->arg1;
472fb0ef528SSeigo Tanimura 					config->ic_port[config->ic_nport - 1].ir_end += qp->arg1;
473fb0ef528SSeigo Tanimura 				}
474fb0ef528SSeigo Tanimura 				if (qp->arg2 != 0) {
475fb0ef528SSeigo Tanimura 					config->ic_nport++;
476fb0ef528SSeigo Tanimura 					config->ic_port[config->ic_nport - 1] = config->ic_port[0];
477fb0ef528SSeigo Tanimura 					config->ic_port[config->ic_nport - 1].ir_start += qp->arg2;
478fb0ef528SSeigo Tanimura 					config->ic_port[config->ic_nport - 1].ir_end += qp->arg2;
479fb0ef528SSeigo Tanimura 				}
480fb0ef528SSeigo Tanimura 				break;
4814249382dSDoug Rabson 			}
4824249382dSDoug Rabson 		}
4834249382dSDoug Rabson 	}
4844249382dSDoug Rabson }
4854249382dSDoug Rabson 
4864249382dSDoug Rabson /*
4874249382dSDoug Rabson  * Scan Resource Data for Logical Devices.
4884249382dSDoug Rabson  *
4894249382dSDoug Rabson  * This function exits as soon as it gets an error reading *ANY*
4905b45337dSDoug Rabson  * Resource Data or it reaches the end of Resource Data.  In the first
4914249382dSDoug Rabson  * case the return value will be TRUE, FALSE otherwise.
4924249382dSDoug Rabson  */
4934249382dSDoug Rabson static int
4945b45337dSDoug Rabson pnp_create_devices(device_t parent, pnp_id *p, int csn,
4955b45337dSDoug Rabson 		   u_char *resources, int len)
4964249382dSDoug Rabson {
4975b45337dSDoug Rabson 	u_char tag, *resp, *resinfo, *startres = 0;
4985b45337dSDoug Rabson 	int large_len, scanning = len, retval = FALSE;
4994249382dSDoug Rabson 	u_int32_t logical_id;
5004249382dSDoug Rabson 	device_t dev = 0;
5014249382dSDoug Rabson 	int ldn = 0;
5024249382dSDoug Rabson 	struct pnp_set_config_arg *csnldn;
5035b45337dSDoug Rabson 	char buf[100];
5044249382dSDoug Rabson 	char *desc = 0;
5054249382dSDoug Rabson 
5065b45337dSDoug Rabson 	resp = resources;
5075b45337dSDoug Rabson 	while (scanning > 0) {
5085b45337dSDoug Rabson 		tag = *resp++;
5095b45337dSDoug Rabson 		scanning--;
5105b45337dSDoug Rabson 		if (PNP_RES_TYPE(tag) != 0) {
5115b45337dSDoug Rabson 			/* Large resource */
5125b45337dSDoug Rabson 			if (scanning < 2) {
5134249382dSDoug Rabson 				scanning = 0;
5144249382dSDoug Rabson 				continue;
5154249382dSDoug Rabson 			}
5165b45337dSDoug Rabson 			large_len = resp[0] + (resp[1] << 8);
5175b45337dSDoug Rabson 			resp += 2;
5185b45337dSDoug Rabson 
5195b45337dSDoug Rabson 			if (scanning < large_len) {
5205b45337dSDoug Rabson 				scanning = 0;
5215b45337dSDoug Rabson 				continue;
5225b45337dSDoug Rabson 			}
5235b45337dSDoug Rabson 			resinfo = resp;
5245b45337dSDoug Rabson 			resp += large_len;
5255b45337dSDoug Rabson 			scanning -= large_len;
5265b45337dSDoug Rabson 
5275b45337dSDoug Rabson 			if (PNP_LRES_NUM(tag) == PNP_TAG_ID_ANSI) {
528d117cb4eSKazutaka YOKOTA 				if (dev) {
529d117cb4eSKazutaka YOKOTA 					/*
530d117cb4eSKazutaka YOKOTA 					 * This is an optional device
531d117cb4eSKazutaka YOKOTA 					 * indentifier string. Skipt it
532d117cb4eSKazutaka YOKOTA 					 * for now.
533d117cb4eSKazutaka YOKOTA 					 */
534d117cb4eSKazutaka YOKOTA 					continue;
535d117cb4eSKazutaka YOKOTA 				}
536d117cb4eSKazutaka YOKOTA 				/* else mandately card identifier string */
5375b45337dSDoug Rabson 				if (large_len > sizeof(buf) - 1)
5385b45337dSDoug Rabson 					large_len = sizeof(buf) - 1;
5395b45337dSDoug Rabson 				bcopy(resinfo, buf, large_len);
5405b45337dSDoug Rabson 
5415b45337dSDoug Rabson 				/*
5425b45337dSDoug Rabson 				 * Trim trailing spaces.
5435b45337dSDoug Rabson 				 */
5445b45337dSDoug Rabson 				while (buf[large_len-1] == ' ')
5455b45337dSDoug Rabson 					large_len--;
5465b45337dSDoug Rabson 				buf[large_len] = '\0';
5475b45337dSDoug Rabson 				desc = buf;
5485b45337dSDoug Rabson 				continue;
5495b45337dSDoug Rabson 			}
5505b45337dSDoug Rabson 
5515b45337dSDoug Rabson 			continue;
5525b45337dSDoug Rabson 		}
5535b45337dSDoug Rabson 
5545b45337dSDoug Rabson 		/* Small resource */
5555b45337dSDoug Rabson 		if (scanning < PNP_SRES_LEN(tag)) {
5565b45337dSDoug Rabson 			scanning = 0;
5575b45337dSDoug Rabson 			continue;
5585b45337dSDoug Rabson 		}
5595b45337dSDoug Rabson 		resinfo = resp;
5605b45337dSDoug Rabson 		resp += PNP_SRES_LEN(tag);
5615b45337dSDoug Rabson 		scanning -= PNP_SRES_LEN(tag);;
5624249382dSDoug Rabson 
5634249382dSDoug Rabson 		switch (PNP_SRES_NUM(tag)) {
56448ab255eSPeter Wemm 		case PNP_TAG_LOGICAL_DEVICE:
5654249382dSDoug Rabson 			/*
5665b45337dSDoug Rabson 			 * Parse the resources for the previous
5675b45337dSDoug Rabson 			 * logical device (if any).
5685b45337dSDoug Rabson 			 */
5695b45337dSDoug Rabson 			if (startres) {
5705b45337dSDoug Rabson 				pnp_parse_resources(dev, startres,
571fb0ef528SSeigo Tanimura 						    resinfo - startres - 1,
572c3959391SKazutaka YOKOTA 						    ldn);
5735b45337dSDoug Rabson 				dev = 0;
5745b45337dSDoug Rabson 				startres = 0;
5755b45337dSDoug Rabson 			}
5765b45337dSDoug Rabson 
5775b45337dSDoug Rabson 			/*
5785b45337dSDoug Rabson 			 * A new logical device. Scan for end of
5795b45337dSDoug Rabson 			 * resources.
5804249382dSDoug Rabson 			 */
5814249382dSDoug Rabson 			bcopy(resinfo, &logical_id, 4);
582fb0ef528SSeigo Tanimura 			pnp_check_quirks(p->vendor_id, logical_id, ldn, NULL);
5835b45337dSDoug Rabson 			dev = BUS_ADD_CHILD(parent, ISA_ORDER_PNP, NULL, -1);
5844249382dSDoug Rabson 			if (desc)
5854249382dSDoug Rabson 				device_set_desc_copy(dev, desc);
586d117cb4eSKazutaka YOKOTA 			else
587d117cb4eSKazutaka YOKOTA 				device_set_desc_copy(dev,
588d117cb4eSKazutaka YOKOTA 						     pnp_eisaformat(logical_id));
5894249382dSDoug Rabson 			isa_set_vendorid(dev, p->vendor_id);
5904249382dSDoug Rabson 			isa_set_serial(dev, p->serial);
5914249382dSDoug Rabson 			isa_set_logicalid(dev, logical_id);
592c3959391SKazutaka YOKOTA 			isa_set_configattr(dev,
593c3959391SKazutaka YOKOTA 					   ISACFGATTR_CANDISABLE |
594c3959391SKazutaka YOKOTA 					   ISACFGATTR_DYNAMIC);
5955b45337dSDoug Rabson 			csnldn = malloc(sizeof *csnldn, M_DEVBUF, M_NOWAIT);
5964249382dSDoug Rabson 			if (!csnldn) {
5974249382dSDoug Rabson 				device_printf(parent,
5984249382dSDoug Rabson 					      "out of memory\n");
5994249382dSDoug Rabson 				scanning = 0;
6004249382dSDoug Rabson 				break;
6014249382dSDoug Rabson 			}
6024249382dSDoug Rabson 			csnldn->csn = csn;
6034249382dSDoug Rabson 			csnldn->ldn = ldn;
6044249382dSDoug Rabson 			ISA_SET_CONFIG_CALLBACK(parent, dev,
6055b45337dSDoug Rabson 						pnp_set_config, csnldn);
6064249382dSDoug Rabson 			ldn++;
6075b45337dSDoug Rabson 			startres = resp;
6084249382dSDoug Rabson 			break;
6094249382dSDoug Rabson 
6104249382dSDoug Rabson 		case PNP_TAG_END:
6115b45337dSDoug Rabson 			if (!startres) {
6125b45337dSDoug Rabson 				device_printf(parent,
6135b45337dSDoug Rabson 					      "malformed resources\n");
6145b45337dSDoug Rabson 				scanning = 0;
6155b45337dSDoug Rabson 				break;
6165b45337dSDoug Rabson 			}
6175b45337dSDoug Rabson 			pnp_parse_resources(dev, startres,
618c3959391SKazutaka YOKOTA 					    resinfo - startres - 1, ldn);
6195b45337dSDoug Rabson 			dev = 0;
6205b45337dSDoug Rabson 			startres = 0;
6214249382dSDoug Rabson 			scanning = 0;
6224249382dSDoug Rabson 			break;
6234249382dSDoug Rabson 
6244249382dSDoug Rabson 		default:
6254249382dSDoug Rabson 			/* Skip this resource */
6264249382dSDoug Rabson 			break;
6274249382dSDoug Rabson 		}
6284249382dSDoug Rabson 	}
6294249382dSDoug Rabson 
6304249382dSDoug Rabson 	return retval;
6314249382dSDoug Rabson }
6324249382dSDoug Rabson 
6334249382dSDoug Rabson /*
6345b45337dSDoug Rabson  * Read 'amount' bytes of resources from the card, allocating memory
6355b45337dSDoug Rabson  * as needed. If a buffer is already available, it should be passed in
6365b45337dSDoug Rabson  * '*resourcesp' and its length in '*spacep'. The number of resource
6375b45337dSDoug Rabson  * bytes already in the buffer should be passed in '*lenp'. The memory
6385b45337dSDoug Rabson  * allocated will be returned in '*resourcesp' with its size and the
6395b45337dSDoug Rabson  * number of bytes of resources in '*spacep' and '*lenp' respectively.
6408b7f9bdcSPoul-Henning Kamp  *
6418b7f9bdcSPoul-Henning Kamp  * XXX: Multiple problems here, we forget to free() stuff in one
6428b7f9bdcSPoul-Henning Kamp  * XXX: error return, and in another case we free (*resourcesp) but
6438b7f9bdcSPoul-Henning Kamp  * XXX: don't tell the caller.
6445b45337dSDoug Rabson  */
6455b45337dSDoug Rabson static int
6465b45337dSDoug Rabson pnp_read_bytes(int amount, u_char **resourcesp, int *spacep, int *lenp)
6475b45337dSDoug Rabson {
6485b45337dSDoug Rabson 	u_char *resources = *resourcesp;
6495b45337dSDoug Rabson 	u_char *newres;
6505b45337dSDoug Rabson 	int space = *spacep;
6515b45337dSDoug Rabson 	int len = *lenp;
6525b45337dSDoug Rabson 
6535b45337dSDoug Rabson 	if (space == 0) {
6545b45337dSDoug Rabson 		space = 1024;
6555b45337dSDoug Rabson 		resources = malloc(space, M_TEMP, M_NOWAIT);
6565b45337dSDoug Rabson 		if (!resources)
6575b45337dSDoug Rabson 			return ENOMEM;
6585b45337dSDoug Rabson 	}
6595b45337dSDoug Rabson 
6605b45337dSDoug Rabson 	if (len + amount > space) {
6615b45337dSDoug Rabson 		int extra = 1024;
6625b45337dSDoug Rabson 		while (len + amount > space + extra)
6635b45337dSDoug Rabson 			extra += 1024;
6645b45337dSDoug Rabson 		newres = malloc(space + extra, M_TEMP, M_NOWAIT);
66530f5ad0fSPoul-Henning Kamp 		if (!newres) {
66630f5ad0fSPoul-Henning Kamp 			/* XXX: free resources */
6675b45337dSDoug Rabson 			return ENOMEM;
66830f5ad0fSPoul-Henning Kamp 		}
6695b45337dSDoug Rabson 		bcopy(resources, newres, len);
6705b45337dSDoug Rabson 		free(resources, M_TEMP);
6715b45337dSDoug Rabson 		resources = newres;
6725b45337dSDoug Rabson 		space += extra;
6735b45337dSDoug Rabson 	}
6745b45337dSDoug Rabson 
6755b45337dSDoug Rabson 	if (pnp_get_resource_info(resources + len, amount) != amount)
6765b45337dSDoug Rabson 		return EINVAL;
6775b45337dSDoug Rabson 	len += amount;
6785b45337dSDoug Rabson 
6795b45337dSDoug Rabson 	*resourcesp = resources;
6805b45337dSDoug Rabson 	*spacep = space;
6815b45337dSDoug Rabson 	*lenp = len;
6825b45337dSDoug Rabson 
6835b45337dSDoug Rabson 	return 0;
6845b45337dSDoug Rabson }
6855b45337dSDoug Rabson 
6865b45337dSDoug Rabson /*
6875b45337dSDoug Rabson  * Read all resources from the card, allocating memory as needed. If a
6885b45337dSDoug Rabson  * buffer is already available, it should be passed in '*resourcesp'
6895b45337dSDoug Rabson  * and its length in '*spacep'. The memory allocated will be returned
6905b45337dSDoug Rabson  * in '*resourcesp' with its size and the number of bytes of resources
6915b45337dSDoug Rabson  * in '*spacep' and '*lenp' respectively.
6925b45337dSDoug Rabson  */
6935b45337dSDoug Rabson static int
6945b45337dSDoug Rabson pnp_read_resources(u_char **resourcesp, int *spacep, int *lenp)
6955b45337dSDoug Rabson {
6965b45337dSDoug Rabson 	u_char *resources = *resourcesp;
6975b45337dSDoug Rabson 	int space = *spacep;
6985b45337dSDoug Rabson 	int len = 0;
6995b45337dSDoug Rabson 	int error, done;
7005b45337dSDoug Rabson 	u_char tag;
7015b45337dSDoug Rabson 
7025b45337dSDoug Rabson 	error = 0;
7035b45337dSDoug Rabson 	done = 0;
7045b45337dSDoug Rabson 	while (!done) {
7055b45337dSDoug Rabson 		error = pnp_read_bytes(1, &resources, &space, &len);
7065b45337dSDoug Rabson 		if (error)
7075b45337dSDoug Rabson 			goto out;
7085b45337dSDoug Rabson 		tag = resources[len-1];
7095b45337dSDoug Rabson 		if (PNP_RES_TYPE(tag) == 0) {
7105b45337dSDoug Rabson 			/*
7115b45337dSDoug Rabson 			 * Small resource, read contents.
7125b45337dSDoug Rabson 			 */
7135b45337dSDoug Rabson 			error = pnp_read_bytes(PNP_SRES_LEN(tag),
7145b45337dSDoug Rabson 					       &resources, &space, &len);
7155b45337dSDoug Rabson 			if (error)
7165b45337dSDoug Rabson 				goto out;
7175b45337dSDoug Rabson 			if (PNP_SRES_NUM(tag) == PNP_TAG_END)
7185b45337dSDoug Rabson 				done = 1;
7195b45337dSDoug Rabson 		} else {
7205b45337dSDoug Rabson 			/*
7215b45337dSDoug Rabson 			 * Large resource, read length and contents.
7225b45337dSDoug Rabson 			 */
7235b45337dSDoug Rabson 			error = pnp_read_bytes(2, &resources, &space, &len);
7245b45337dSDoug Rabson 			if (error)
7255b45337dSDoug Rabson 				goto out;
7265b45337dSDoug Rabson 			error = pnp_read_bytes(resources[len-2]
7275b45337dSDoug Rabson 					       + (resources[len-1] << 8),
7285b45337dSDoug Rabson 					       &resources, &space, &len);
7295b45337dSDoug Rabson 			if (error)
7305b45337dSDoug Rabson 				goto out;
7315b45337dSDoug Rabson 		}
7325b45337dSDoug Rabson 	}
7335b45337dSDoug Rabson 
7345b45337dSDoug Rabson  out:
7355b45337dSDoug Rabson 	*resourcesp = resources;
7365b45337dSDoug Rabson 	*spacep = space;
7375b45337dSDoug Rabson 	*lenp = len;
7385b45337dSDoug Rabson 	return error;
7395b45337dSDoug Rabson }
7405b45337dSDoug Rabson 
7415b45337dSDoug Rabson /*
7424249382dSDoug Rabson  * Run the isolation protocol. Use pnp_rd_port as the READ_DATA port
7434249382dSDoug Rabson  * value (caller should try multiple READ_DATA locations before giving
7444249382dSDoug Rabson  * up). Upon exiting, all cards are aware that they should use
7454249382dSDoug Rabson  * pnp_rd_port as the READ_DATA port.
7464249382dSDoug Rabson  *
7474249382dSDoug Rabson  * In the first pass, a csn is assigned to each board and pnp_id's
7484249382dSDoug Rabson  * are saved to an array, pnp_devices. In the second pass, each
7494249382dSDoug Rabson  * card is woken up and the device configuration is called.
7504249382dSDoug Rabson  */
7514249382dSDoug Rabson static int
7524249382dSDoug Rabson pnp_isolation_protocol(device_t parent)
7534249382dSDoug Rabson {
7544249382dSDoug Rabson 	int csn;
7554249382dSDoug Rabson 	pnp_id id;
7565b45337dSDoug Rabson 	int found = 0, len;
7575b45337dSDoug Rabson 	u_char *resources = 0;
7585b45337dSDoug Rabson 	int space = 0;
7595b45337dSDoug Rabson 	int error;
7607698537bSYoshihiro Takahashi #ifdef PC98
7617698537bSYoshihiro Takahashi 	int n, necpnp;
7627698537bSYoshihiro Takahashi 	u_char buffer[10];
7637698537bSYoshihiro Takahashi #endif
7644249382dSDoug Rabson 
7654249382dSDoug Rabson 	/*
7664249382dSDoug Rabson 	 * Put all cards into the Sleep state so that we can clear
7674249382dSDoug Rabson 	 * their CSNs.
7684249382dSDoug Rabson 	 */
7694249382dSDoug Rabson 	pnp_send_initiation_key();
7704249382dSDoug Rabson 
7714249382dSDoug Rabson 	/*
7724249382dSDoug Rabson 	 * Clear the CSN for all cards.
7734249382dSDoug Rabson 	 */
7744249382dSDoug Rabson 	pnp_write(PNP_CONFIG_CONTROL, PNP_CONFIG_CONTROL_RESET_CSN);
7754249382dSDoug Rabson 
7764249382dSDoug Rabson 	/*
7774249382dSDoug Rabson 	 * Move all cards to the Isolation state.
7784249382dSDoug Rabson 	 */
7794249382dSDoug Rabson 	pnp_write(PNP_WAKE, 0);
7804249382dSDoug Rabson 
7814249382dSDoug Rabson 	/*
7824249382dSDoug Rabson 	 * Tell them where the read point is going to be this time.
7834249382dSDoug Rabson 	 */
7844249382dSDoug Rabson 	pnp_write(PNP_SET_RD_DATA, pnp_rd_port);
7854249382dSDoug Rabson 
7864249382dSDoug Rabson 	for (csn = 1; csn < PNP_MAX_CARDS; csn++) {
7874249382dSDoug Rabson 		/*
7884249382dSDoug Rabson 		 * Start the serial isolation protocol.
7894249382dSDoug Rabson 		 */
7904249382dSDoug Rabson 		outb(_PNP_ADDRESS, PNP_SERIAL_ISOLATION);
7914249382dSDoug Rabson 		DELAY(1000);	/* Delay 1 msec */
7924249382dSDoug Rabson 
7934249382dSDoug Rabson 		if (pnp_get_serial(&id)) {
7944249382dSDoug Rabson 			/*
7954249382dSDoug Rabson 			 * We have read the id from a card
7964249382dSDoug Rabson 			 * successfully. The card which won the
7974249382dSDoug Rabson 			 * isolation protocol will be in Isolation
7985b45337dSDoug Rabson 			 * mode and all others will be in Sleep.
7994249382dSDoug Rabson 			 * Program the CSN of the isolated card
8004249382dSDoug Rabson 			 * (taking it to Config state) and read its
8014249382dSDoug Rabson 			 * resources, creating devices as we find
8024249382dSDoug Rabson 			 * logical devices on the card.
8034249382dSDoug Rabson 			 */
8044249382dSDoug Rabson 			pnp_write(PNP_SET_CSN, csn);
8057698537bSYoshihiro Takahashi #ifdef PC98
8067698537bSYoshihiro Takahashi 			if (bootverbose)
8077698537bSYoshihiro Takahashi 				printf("PnP Vendor ID = %x\n", id.vendor_id);
8087698537bSYoshihiro Takahashi 			/* Check for NEC PnP (9 bytes serial). */
8097698537bSYoshihiro Takahashi 			for (n = necpnp = 0; necids[n].vendor_id; n++) {
8107698537bSYoshihiro Takahashi 				if (id.vendor_id == necids[n].vendor_id) {
8117698537bSYoshihiro Takahashi 					necpnp = 1;
8127698537bSYoshihiro Takahashi 					break;
8137698537bSYoshihiro Takahashi 				}
8147698537bSYoshihiro Takahashi 			}
8157698537bSYoshihiro Takahashi 			if (necpnp) {
8167698537bSYoshihiro Takahashi 				if (bootverbose)
8177698537bSYoshihiro Takahashi 					printf("It seems to NEC-PnP card (%s).\n",
8187698537bSYoshihiro Takahashi 					       pnp_eisaformat(id.vendor_id));
8197698537bSYoshihiro Takahashi 				/*  Read dummy 9 bytes serial area. */
8207698537bSYoshihiro Takahashi 				pnp_get_resource_info(buffer, 9);
8217698537bSYoshihiro Takahashi 			} else {
8227698537bSYoshihiro Takahashi 				if (bootverbose)
8237698537bSYoshihiro Takahashi 					printf("It seems to Normal-ISA-PnP card (%s).\n",
8247698537bSYoshihiro Takahashi 					       pnp_eisaformat(id.vendor_id));
8257698537bSYoshihiro Takahashi 			}
8267698537bSYoshihiro Takahashi 			if (bootverbose)
8277698537bSYoshihiro Takahashi 				printf("Reading PnP configuration for %s.\n",
8287698537bSYoshihiro Takahashi 				       pnp_eisaformat(id.vendor_id));
8297698537bSYoshihiro Takahashi #endif
8305b45337dSDoug Rabson 			error = pnp_read_resources(&resources,
8315b45337dSDoug Rabson 						   &space,
8325b45337dSDoug Rabson 						   &len);
8335b45337dSDoug Rabson 			if (error)
8345b45337dSDoug Rabson 				break;
8355b45337dSDoug Rabson 			pnp_create_devices(parent, &id, csn,
8365b45337dSDoug Rabson 					   resources, len);
8374249382dSDoug Rabson 			found++;
8384249382dSDoug Rabson 		} else
8394249382dSDoug Rabson 			break;
8404249382dSDoug Rabson 
8414249382dSDoug Rabson 		/*
8424249382dSDoug Rabson 		 * Put this card back to the Sleep state and
8434249382dSDoug Rabson 		 * simultaneously move all cards which don't have a
8444249382dSDoug Rabson 		 * CSN yet to Isolation state.
8454249382dSDoug Rabson 		 */
8464249382dSDoug Rabson 		pnp_write(PNP_WAKE, 0);
8474249382dSDoug Rabson 	}
8484249382dSDoug Rabson 
8494249382dSDoug Rabson 	/*
8504249382dSDoug Rabson 	 * Unless we have chosen the wrong read port, all cards will
8514249382dSDoug Rabson 	 * be in Sleep state. Put them back into WaitForKey for
8524249382dSDoug Rabson 	 * now. Their resources will be programmed later.
8534249382dSDoug Rabson 	 */
8544249382dSDoug Rabson 	pnp_write(PNP_CONFIG_CONTROL, PNP_CONFIG_CONTROL_WAIT_FOR_KEY);
8554249382dSDoug Rabson 
8565b45337dSDoug Rabson 	/*
8575b45337dSDoug Rabson 	 * Cleanup.
8585b45337dSDoug Rabson 	 */
8595b45337dSDoug Rabson 	if (resources)
8605b45337dSDoug Rabson 		free(resources, M_TEMP);
8615b45337dSDoug Rabson 
8624249382dSDoug Rabson 	return found;
8634249382dSDoug Rabson }
8644249382dSDoug Rabson 
8654249382dSDoug Rabson 
8664249382dSDoug Rabson /*
8674249382dSDoug Rabson  * pnp_identify()
8684249382dSDoug Rabson  *
8694249382dSDoug Rabson  * autoconfiguration of pnp devices. This routine just runs the
8704249382dSDoug Rabson  * isolation protocol over several ports, until one is successful.
8714249382dSDoug Rabson  *
8724249382dSDoug Rabson  * may be called more than once ?
8734249382dSDoug Rabson  *
8744249382dSDoug Rabson  */
8754249382dSDoug Rabson 
8764249382dSDoug Rabson static void
8774249382dSDoug Rabson pnp_identify(driver_t *driver, device_t parent)
8784249382dSDoug Rabson {
8794249382dSDoug Rabson 	int num_pnp_devs;
8804249382dSDoug Rabson 
8814249382dSDoug Rabson #if 0
8824249382dSDoug Rabson 	if (pnp_ldn_overrides[0].csn == 0) {
8834249382dSDoug Rabson 		if (bootverbose)
8844249382dSDoug Rabson 			printf("Initializing PnP override table\n");
8854249382dSDoug Rabson 		bzero (pnp_ldn_overrides, sizeof(pnp_ldn_overrides));
8864249382dSDoug Rabson 		pnp_ldn_overrides[0].csn = 255 ;
8874249382dSDoug Rabson 	}
8884249382dSDoug Rabson #endif
8894249382dSDoug Rabson 
8904249382dSDoug Rabson 	/* Try various READ_DATA ports from 0x203-0x3ff */
8914249382dSDoug Rabson 	for (pnp_rd_port = 0x80; (pnp_rd_port < 0xff); pnp_rd_port += 0x10) {
8924249382dSDoug Rabson 		if (bootverbose)
8934249382dSDoug Rabson 			printf("Trying Read_Port at %x\n", (pnp_rd_port << 2) | 0x3);
8944249382dSDoug Rabson 
8954249382dSDoug Rabson 		num_pnp_devs = pnp_isolation_protocol(parent);
8964249382dSDoug Rabson 		if (num_pnp_devs)
8974249382dSDoug Rabson 			break;
8984249382dSDoug Rabson 	}
8994249382dSDoug Rabson }
9004249382dSDoug Rabson 
9014249382dSDoug Rabson static device_method_t pnp_methods[] = {
9024249382dSDoug Rabson 	/* Device interface */
9034249382dSDoug Rabson 	DEVMETHOD(device_identify,	pnp_identify),
9044249382dSDoug Rabson 
9054249382dSDoug Rabson 	{ 0, 0 }
9064249382dSDoug Rabson };
9074249382dSDoug Rabson 
9084249382dSDoug Rabson static driver_t pnp_driver = {
9094249382dSDoug Rabson 	"pnp",
9104249382dSDoug Rabson 	pnp_methods,
9114249382dSDoug Rabson 	1,			/* no softc */
9124249382dSDoug Rabson };
9134249382dSDoug Rabson 
9144249382dSDoug Rabson static devclass_t pnp_devclass;
9154249382dSDoug Rabson 
9164249382dSDoug Rabson DRIVER_MODULE(pnp, isa, pnp_driver, pnp_devclass, 0, 0);
917