xref: /freebsd/sys/isa/pnp.c (revision c2ede4b379d8fa04de02c36201538fd610763299)
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  *      from: pnp.c,v 1.11 1999/05/06 22:11:19 peter Exp
274249382dSDoug Rabson  */
284249382dSDoug Rabson 
298c9bbf48SDavid E. O'Brien #include <sys/cdefs.h>
308c9bbf48SDavid E. O'Brien __FBSDID("$FreeBSD$");
318c9bbf48SDavid E. O'Brien 
324249382dSDoug Rabson #include <sys/param.h>
334249382dSDoug Rabson #include <sys/systm.h>
344249382dSDoug Rabson #include <sys/kernel.h>
354249382dSDoug Rabson #include <sys/module.h>
364249382dSDoug Rabson #include <sys/bus.h>
37c7974ff1SMarius Strobl #include <sys/endian.h>
384249382dSDoug Rabson #include <sys/malloc.h>
394249382dSDoug Rabson #include <isa/isavar.h>
404249382dSDoug Rabson #include <isa/pnpreg.h>
414249382dSDoug Rabson #include <isa/pnpvar.h>
4221c3015aSDoug Rabson #include <machine/bus.h>
434249382dSDoug Rabson 
444249382dSDoug Rabson typedef struct _pnp_id {
457a83aa63SWarner Losh 	uint32_t vendor_id;
467a83aa63SWarner Losh 	uint32_t serial;
474249382dSDoug Rabson 	u_char checksum;
484249382dSDoug Rabson } pnp_id;
494249382dSDoug Rabson 
504249382dSDoug Rabson struct pnp_set_config_arg {
514249382dSDoug Rabson 	int	csn;		/* Card number to configure */
524249382dSDoug Rabson 	int	ldn;		/* Logical device on card */
534249382dSDoug Rabson };
544249382dSDoug Rabson 
554249382dSDoug Rabson struct pnp_quirk {
567a83aa63SWarner Losh 	uint32_t vendor_id;	/* Vendor of the card */
577a83aa63SWarner Losh 	uint32_t logical_id;	/* ID of the device with quirk */
584249382dSDoug Rabson 	int	type;
594249382dSDoug Rabson #define PNP_QUIRK_WRITE_REG	1 /* Need to write a pnp register  */
60fb0ef528SSeigo Tanimura #define PNP_QUIRK_EXTRA_IO	2 /* Has extra io ports  */
614249382dSDoug Rabson 	int	arg1;
624249382dSDoug Rabson 	int	arg2;
634249382dSDoug Rabson };
644249382dSDoug Rabson 
654249382dSDoug Rabson struct pnp_quirk pnp_quirks[] = {
664249382dSDoug Rabson 	/*
674249382dSDoug Rabson 	 * The Gravis UltraSound needs register 0xf2 to be set to 0xff
684249382dSDoug Rabson 	 * to enable power.
694249382dSDoug Rabson 	 * XXX need to know the logical device id.
704249382dSDoug Rabson 	 */
714249382dSDoug Rabson 	{ 0x0100561e /* GRV0001 */,	0,
724249382dSDoug Rabson 	  PNP_QUIRK_WRITE_REG,	0xf2,	 0xff },
73fb0ef528SSeigo Tanimura 	/*
74fb0ef528SSeigo Tanimura 	 * An emu8000 does not give us other than the first
75fb0ef528SSeigo Tanimura 	 * port.
76fb0ef528SSeigo Tanimura 	 */
77fb0ef528SSeigo Tanimura 	{ 0x26008c0e /* SB16 */,	0x21008c0e,
78fb0ef528SSeigo Tanimura 	  PNP_QUIRK_EXTRA_IO,	0x400,	 0x800 },
79fb0ef528SSeigo Tanimura 	{ 0x42008c0e /* SB32(CTL0042) */,	0x21008c0e,
80fb0ef528SSeigo Tanimura 	  PNP_QUIRK_EXTRA_IO,	0x400,	 0x800 },
81fb0ef528SSeigo Tanimura 	{ 0x44008c0e /* SB32(CTL0044) */,	0x21008c0e,
82fb0ef528SSeigo Tanimura 	  PNP_QUIRK_EXTRA_IO,	0x400,	 0x800 },
83fb0ef528SSeigo Tanimura 	{ 0x49008c0e /* SB32(CTL0049) */,	0x21008c0e,
84fb0ef528SSeigo Tanimura 	  PNP_QUIRK_EXTRA_IO,	0x400,	 0x800 },
85fb0ef528SSeigo Tanimura 	{ 0xf1008c0e /* SB32(CTL00f1) */,	0x21008c0e,
86fb0ef528SSeigo Tanimura 	  PNP_QUIRK_EXTRA_IO,	0x400,	 0x800 },
87fb0ef528SSeigo Tanimura 	{ 0xc1008c0e /* SB64(CTL00c1) */,	0x22008c0e,
88fb0ef528SSeigo Tanimura 	  PNP_QUIRK_EXTRA_IO,	0x400,	 0x800 },
8938b968c3SSeigo Tanimura 	{ 0xc5008c0e /* SB64(CTL00c5) */,	0x22008c0e,
9038b968c3SSeigo Tanimura 	  PNP_QUIRK_EXTRA_IO,	0x400,	 0x800 },
91fb0ef528SSeigo Tanimura 	{ 0xe4008c0e /* SB64(CTL00e4) */,	0x22008c0e,
92fb0ef528SSeigo Tanimura 	  PNP_QUIRK_EXTRA_IO,	0x400,	 0x800 },
934249382dSDoug Rabson 
944249382dSDoug Rabson 	{ 0 }
954249382dSDoug Rabson };
964249382dSDoug Rabson 
977698537bSYoshihiro Takahashi #ifdef PC98
987698537bSYoshihiro Takahashi /* Some NEC PnP cards have 9 bytes serial code. */
997698537bSYoshihiro Takahashi static pnp_id necids[] = {
1007698537bSYoshihiro Takahashi 	{0x4180a3b8, 0xffffffff, 0x00},	/* PC-9801CB-B04 (NEC8041) */
1017698537bSYoshihiro Takahashi 	{0x5181a3b8, 0xffffffff, 0x46},	/* PC-9821CB2-B04(NEC8151) */
1027698537bSYoshihiro Takahashi 	{0x5182a3b8, 0xffffffff, 0xb8},	/* PC-9801-XX    (NEC8251) */
1037698537bSYoshihiro Takahashi 	{0x9181a3b8, 0xffffffff, 0x00},	/* PC-9801-120   (NEC8191) */
1047698537bSYoshihiro Takahashi 	{0, 0, 0}
1057698537bSYoshihiro Takahashi };
1067698537bSYoshihiro Takahashi #endif
1077698537bSYoshihiro Takahashi 
1084249382dSDoug Rabson /* The READ_DATA port that we are using currently */
1094249382dSDoug Rabson static int pnp_rd_port;
1104249382dSDoug Rabson 
1114249382dSDoug Rabson static void   pnp_send_initiation_key(void);
1124249382dSDoug Rabson static int    pnp_get_serial(pnp_id *p);
1134249382dSDoug Rabson static int    pnp_isolation_protocol(device_t parent);
1144249382dSDoug Rabson 
115bb2b9030SDoug Rabson char *
1167a83aa63SWarner Losh pnp_eisaformat(uint32_t id)
117bb2b9030SDoug Rabson {
118c7974ff1SMarius Strobl 	uint8_t *data;
119bb2b9030SDoug Rabson 	static char idbuf[8];
120bb2b9030SDoug Rabson 	const char  hextoascii[] = "0123456789abcdef";
121bb2b9030SDoug Rabson 
122c7974ff1SMarius Strobl 	id = htole32(id);
123c7974ff1SMarius Strobl 	data = (uint8_t *)&id;
124bb2b9030SDoug Rabson 	idbuf[0] = '@' + ((data[0] & 0x7c) >> 2);
125bb2b9030SDoug Rabson 	idbuf[1] = '@' + (((data[0] & 0x3) << 3) + ((data[1] & 0xe0) >> 5));
126bb2b9030SDoug Rabson 	idbuf[2] = '@' + (data[1] & 0x1f);
127bb2b9030SDoug Rabson 	idbuf[3] = hextoascii[(data[2] >> 4)];
128bb2b9030SDoug Rabson 	idbuf[4] = hextoascii[(data[2] & 0xf)];
129bb2b9030SDoug Rabson 	idbuf[5] = hextoascii[(data[3] >> 4)];
130bb2b9030SDoug Rabson 	idbuf[6] = hextoascii[(data[3] & 0xf)];
131bb2b9030SDoug Rabson 	idbuf[7] = 0;
132bb2b9030SDoug Rabson 	return(idbuf);
133bb2b9030SDoug Rabson }
134bb2b9030SDoug Rabson 
1354249382dSDoug Rabson static void
1364249382dSDoug Rabson pnp_write(int d, u_char r)
1374249382dSDoug Rabson {
1384249382dSDoug Rabson 	outb (_PNP_ADDRESS, d);
1394249382dSDoug Rabson 	outb (_PNP_WRITE_DATA, r);
1404249382dSDoug Rabson }
1414249382dSDoug Rabson 
1424249382dSDoug Rabson /*
1434249382dSDoug Rabson  * Send Initiation LFSR as described in "Plug and Play ISA Specification",
1444249382dSDoug Rabson  * Intel May 94.
1454249382dSDoug Rabson  */
1464249382dSDoug Rabson static void
1474249382dSDoug Rabson pnp_send_initiation_key()
1484249382dSDoug Rabson {
1494249382dSDoug Rabson 	int cur, i;
1504249382dSDoug Rabson 
1514249382dSDoug Rabson 	/* Reset the LSFR */
1524249382dSDoug Rabson 	outb(_PNP_ADDRESS, 0);
1534249382dSDoug Rabson 	outb(_PNP_ADDRESS, 0); /* yes, we do need it twice! */
1544249382dSDoug Rabson 
1554249382dSDoug Rabson 	cur = 0x6a;
1564249382dSDoug Rabson 	outb(_PNP_ADDRESS, cur);
1574249382dSDoug Rabson 
1584249382dSDoug Rabson 	for (i = 1; i < 32; i++) {
1594249382dSDoug Rabson 		cur = (cur >> 1) | (((cur ^ (cur >> 1)) << 7) & 0xff);
1604249382dSDoug Rabson 		outb(_PNP_ADDRESS, cur);
1614249382dSDoug Rabson 	}
1624249382dSDoug Rabson }
1634249382dSDoug Rabson 
1644249382dSDoug Rabson 
1654249382dSDoug Rabson /*
1664249382dSDoug Rabson  * Get the device's serial number.  Returns 1 if the serial is valid.
1674249382dSDoug Rabson  */
1684249382dSDoug Rabson static int
1694249382dSDoug Rabson pnp_get_serial(pnp_id *p)
1704249382dSDoug Rabson {
1714249382dSDoug Rabson 	int i, bit, valid = 0, sum = 0x6a;
1724249382dSDoug Rabson 	u_char *data = (u_char *)p;
1734249382dSDoug Rabson 
1744249382dSDoug Rabson 	bzero(data, sizeof(char) * 9);
1754249382dSDoug Rabson 	outb(_PNP_ADDRESS, PNP_SERIAL_ISOLATION);
1764249382dSDoug Rabson 	for (i = 0; i < 72; i++) {
1774249382dSDoug Rabson 		bit = inb((pnp_rd_port << 2) | 0x3) == 0x55;
1784249382dSDoug Rabson 		DELAY(250);	/* Delay 250 usec */
1794249382dSDoug Rabson 
1804249382dSDoug Rabson 		/* Can't Short Circuit the next evaluation, so 'and' is last */
1814249382dSDoug Rabson 		bit = (inb((pnp_rd_port << 2) | 0x3) == 0xaa) && bit;
1824249382dSDoug Rabson 		DELAY(250);	/* Delay 250 usec */
1834249382dSDoug Rabson 
1844249382dSDoug Rabson 		valid = valid || bit;
1854249382dSDoug Rabson 		if (i < 64)
1864249382dSDoug Rabson 			sum = (sum >> 1) |
1874249382dSDoug Rabson 			  (((sum ^ (sum >> 1) ^ bit) << 7) & 0xff);
1884249382dSDoug Rabson 		data[i / 8] = (data[i / 8] >> 1) | (bit ? 0x80 : 0);
1894249382dSDoug Rabson 	}
1904249382dSDoug Rabson 
1914249382dSDoug Rabson 	valid = valid && (data[8] == sum);
1924249382dSDoug Rabson 
1937a83aa63SWarner Losh 	return (valid);
1944249382dSDoug Rabson }
1954249382dSDoug Rabson 
1964249382dSDoug Rabson /*
1974249382dSDoug Rabson  * Fill's the buffer with resource info from the device.
1985b45337dSDoug Rabson  * Returns the number of characters read.
1994249382dSDoug Rabson  */
2004249382dSDoug Rabson static int
2014249382dSDoug Rabson pnp_get_resource_info(u_char *buffer, int len)
2024249382dSDoug Rabson {
2035b45337dSDoug Rabson 	int i, j, count;
2044249382dSDoug Rabson 	u_char temp;
2054249382dSDoug Rabson 
2065b45337dSDoug Rabson 	count = 0;
2074249382dSDoug Rabson 	for (i = 0; i < len; i++) {
2084249382dSDoug Rabson 		outb(_PNP_ADDRESS, PNP_STATUS);
2094249382dSDoug Rabson 		for (j = 0; j < 100; j++) {
2104249382dSDoug Rabson 			if ((inb((pnp_rd_port << 2) | 0x3)) & 0x1)
2114249382dSDoug Rabson 				break;
2124249382dSDoug Rabson 			DELAY(1);
2134249382dSDoug Rabson 		}
2144249382dSDoug Rabson 		if (j == 100) {
2154249382dSDoug Rabson 			printf("PnP device failed to report resource data\n");
2167a83aa63SWarner Losh 			return (count);
2174249382dSDoug Rabson 		}
2184249382dSDoug Rabson 		outb(_PNP_ADDRESS, PNP_RESOURCE_DATA);
2194249382dSDoug Rabson 		temp = inb((pnp_rd_port << 2) | 0x3);
2204249382dSDoug Rabson 		if (buffer != NULL)
2214249382dSDoug Rabson 			buffer[i] = temp;
2225b45337dSDoug Rabson 		count++;
2234249382dSDoug Rabson 	}
2247a83aa63SWarner Losh 	return (count);
2254249382dSDoug Rabson }
2264249382dSDoug Rabson 
2274249382dSDoug Rabson /*
2284249382dSDoug Rabson  * This function is called after the bus has assigned resource
2294249382dSDoug Rabson  * locations for a logical device.
2304249382dSDoug Rabson  */
2314249382dSDoug Rabson static void
2324249382dSDoug Rabson pnp_set_config(void *arg, struct isa_config *config, int enable)
2334249382dSDoug Rabson {
2344249382dSDoug Rabson 	int csn = ((struct pnp_set_config_arg *) arg)->csn;
2354249382dSDoug Rabson 	int ldn = ((struct pnp_set_config_arg *) arg)->ldn;
2364249382dSDoug Rabson 	int i;
2374249382dSDoug Rabson 
2384249382dSDoug Rabson 	/*
2394249382dSDoug Rabson 	 * First put all cards into Sleep state with the initiation
2404249382dSDoug Rabson 	 * key, then put our card into Config state.
2414249382dSDoug Rabson 	 */
2424249382dSDoug Rabson 	pnp_send_initiation_key();
2434249382dSDoug Rabson 	pnp_write(PNP_WAKE, csn);
2444249382dSDoug Rabson 
2454249382dSDoug Rabson 	/*
2464249382dSDoug Rabson 	 * Select our logical device so that we can program it.
2474249382dSDoug Rabson 	 */
2484249382dSDoug Rabson 	pnp_write(PNP_SET_LDN, ldn);
2494249382dSDoug Rabson 
2504249382dSDoug Rabson 	/*
25108764b84SMike Smith 	 * Constrain the number of resources we will try to program
25208764b84SMike Smith 	 */
25308764b84SMike Smith 	if (config->ic_nmem > ISA_PNP_NMEM) {
2547a83aa63SWarner Losh 		printf("too many ISA memory ranges (%d > %d)\n",
2557a83aa63SWarner Losh 		    config->ic_nmem, ISA_PNP_NMEM);
25608764b84SMike Smith 		config->ic_nmem = ISA_PNP_NMEM;
25708764b84SMike Smith 	}
25808764b84SMike Smith 	if (config->ic_nport > ISA_PNP_NPORT) {
2597a83aa63SWarner Losh 		printf("too many ISA I/O ranges (%d > %d)\n", config->ic_nport,
2607a83aa63SWarner Losh 		    ISA_PNP_NPORT);
26108764b84SMike Smith 		config->ic_nport = ISA_PNP_NPORT;
26208764b84SMike Smith 	}
26308764b84SMike Smith 	if (config->ic_nirq > ISA_PNP_NIRQ) {
2647a83aa63SWarner Losh 		printf("too many ISA IRQs (%d > %d)\n", config->ic_nirq,
2657a83aa63SWarner Losh 		    ISA_PNP_NIRQ);
26608764b84SMike Smith 		config->ic_nirq = ISA_PNP_NIRQ;
26708764b84SMike Smith 	}
26808764b84SMike Smith 	if (config->ic_ndrq > ISA_PNP_NDRQ) {
2697a83aa63SWarner Losh 		printf("too many ISA DRQs (%d > %d)\n", config->ic_ndrq,
2707a83aa63SWarner Losh 		    ISA_PNP_NDRQ);
27108764b84SMike Smith 		config->ic_ndrq = ISA_PNP_NDRQ;
27208764b84SMike Smith 	}
27308764b84SMike Smith 
27408764b84SMike Smith 	/*
2754249382dSDoug Rabson 	 * Now program the resources.
2764249382dSDoug Rabson 	 */
2774249382dSDoug Rabson 	for (i = 0; i < config->ic_nmem; i++) {
2787a83aa63SWarner Losh 		uint32_t start;
2797a83aa63SWarner Losh 		uint32_t size;
280c3959391SKazutaka YOKOTA 
281c3959391SKazutaka YOKOTA 		/* XXX: should handle memory control register, 32 bit memory */
282c3959391SKazutaka YOKOTA 		if (config->ic_mem[i].ir_size == 0) {
283c3959391SKazutaka YOKOTA 			pnp_write(PNP_MEM_BASE_HIGH(i), 0);
284c3959391SKazutaka YOKOTA 			pnp_write(PNP_MEM_BASE_LOW(i), 0);
285c3959391SKazutaka YOKOTA 			pnp_write(PNP_MEM_RANGE_HIGH(i), 0);
286c3959391SKazutaka YOKOTA 			pnp_write(PNP_MEM_RANGE_LOW(i), 0);
287c3959391SKazutaka YOKOTA 		} else {
288c3959391SKazutaka YOKOTA 			start = config->ic_mem[i].ir_start;
289c3959391SKazutaka YOKOTA 			size =  config->ic_mem[i].ir_size;
2904249382dSDoug Rabson 			if (start & 0xff)
2914249382dSDoug Rabson 				panic("pnp_set_config: bogus memory assignment");
2924249382dSDoug Rabson 			pnp_write(PNP_MEM_BASE_HIGH(i), (start >> 16) & 0xff);
2934249382dSDoug Rabson 			pnp_write(PNP_MEM_BASE_LOW(i), (start >> 8) & 0xff);
2944249382dSDoug Rabson 			pnp_write(PNP_MEM_RANGE_HIGH(i), (size >> 16) & 0xff);
2954249382dSDoug Rabson 			pnp_write(PNP_MEM_RANGE_LOW(i), (size >> 8) & 0xff);
2964249382dSDoug Rabson 		}
297c3959391SKazutaka YOKOTA 	}
29808764b84SMike Smith 	for (; i < ISA_PNP_NMEM; i++) {
2994249382dSDoug Rabson 		pnp_write(PNP_MEM_BASE_HIGH(i), 0);
3004249382dSDoug Rabson 		pnp_write(PNP_MEM_BASE_LOW(i), 0);
3014249382dSDoug Rabson 		pnp_write(PNP_MEM_RANGE_HIGH(i), 0);
3024249382dSDoug Rabson 		pnp_write(PNP_MEM_RANGE_LOW(i), 0);
3034249382dSDoug Rabson 	}
3044249382dSDoug Rabson 
3054249382dSDoug Rabson 	for (i = 0; i < config->ic_nport; i++) {
3067a83aa63SWarner Losh 		uint32_t start;
307c3959391SKazutaka YOKOTA 
308c3959391SKazutaka YOKOTA 		if (config->ic_port[i].ir_size == 0) {
309c3959391SKazutaka YOKOTA 			pnp_write(PNP_IO_BASE_HIGH(i), 0);
310c3959391SKazutaka YOKOTA 			pnp_write(PNP_IO_BASE_LOW(i), 0);
311c3959391SKazutaka YOKOTA 		} else {
312c3959391SKazutaka YOKOTA 			start = config->ic_port[i].ir_start;
3134249382dSDoug Rabson 			pnp_write(PNP_IO_BASE_HIGH(i), (start >> 8) & 0xff);
3144249382dSDoug Rabson 			pnp_write(PNP_IO_BASE_LOW(i), (start >> 0) & 0xff);
3154249382dSDoug Rabson 		}
316c3959391SKazutaka YOKOTA 	}
31708764b84SMike Smith 	for (; i < ISA_PNP_NPORT; i++) {
3184249382dSDoug Rabson 		pnp_write(PNP_IO_BASE_HIGH(i), 0);
3194249382dSDoug Rabson 		pnp_write(PNP_IO_BASE_LOW(i), 0);
3204249382dSDoug Rabson 	}
3214249382dSDoug Rabson 
3224249382dSDoug Rabson 	for (i = 0; i < config->ic_nirq; i++) {
323c3959391SKazutaka YOKOTA 		int irq;
324c3959391SKazutaka YOKOTA 
325c3959391SKazutaka YOKOTA 		/* XXX: interrupt type */
326c3959391SKazutaka YOKOTA 		if (config->ic_irqmask[i] == 0) {
327c3959391SKazutaka YOKOTA 			pnp_write(PNP_IRQ_LEVEL(i), 0);
328c3959391SKazutaka YOKOTA 			pnp_write(PNP_IRQ_TYPE(i), 2);
329c3959391SKazutaka YOKOTA 		} else {
330c3959391SKazutaka YOKOTA 			irq = ffs(config->ic_irqmask[i]) - 1;
3314249382dSDoug Rabson 			pnp_write(PNP_IRQ_LEVEL(i), irq);
3324249382dSDoug Rabson 			pnp_write(PNP_IRQ_TYPE(i), 2); /* XXX */
3334249382dSDoug Rabson 		}
334c3959391SKazutaka YOKOTA 	}
33508764b84SMike Smith 	for (; i < ISA_PNP_NIRQ; i++) {
3364249382dSDoug Rabson 		/*
3374249382dSDoug Rabson 		 * IRQ 0 is not a valid interrupt selection and
3384249382dSDoug Rabson 		 * represents no interrupt selection.
3394249382dSDoug Rabson 		 */
3404249382dSDoug Rabson 		pnp_write(PNP_IRQ_LEVEL(i), 0);
341c3959391SKazutaka YOKOTA 		pnp_write(PNP_IRQ_TYPE(i), 2);
3424249382dSDoug Rabson 	}
3434249382dSDoug Rabson 
3444249382dSDoug Rabson 	for (i = 0; i < config->ic_ndrq; i++) {
345c3959391SKazutaka YOKOTA 		int drq;
346c3959391SKazutaka YOKOTA 
347c3959391SKazutaka YOKOTA 		if (config->ic_drqmask[i] == 0) {
348c3959391SKazutaka YOKOTA 			pnp_write(PNP_DMA_CHANNEL(i), 4);
349c3959391SKazutaka YOKOTA 		} else {
350c3959391SKazutaka YOKOTA 			drq = ffs(config->ic_drqmask[i]) - 1;
3514249382dSDoug Rabson 			pnp_write(PNP_DMA_CHANNEL(i), drq);
3524249382dSDoug Rabson 		}
353c3959391SKazutaka YOKOTA 	}
35408764b84SMike Smith 	for (; i < ISA_PNP_NDRQ; i++) {
3554249382dSDoug Rabson 		/*
3564249382dSDoug Rabson 		 * DMA channel 4, the cascade channel is used to
3574249382dSDoug Rabson 		 * indicate no DMA channel is active.
3584249382dSDoug Rabson 		 */
3594249382dSDoug Rabson 		pnp_write(PNP_DMA_CHANNEL(i), 4);
3604249382dSDoug Rabson 	}
3614249382dSDoug Rabson 
3624249382dSDoug Rabson 	pnp_write(PNP_ACTIVATE, enable ? 1 : 0);
3634249382dSDoug Rabson 
3644249382dSDoug Rabson 	/*
3654249382dSDoug Rabson 	 * Wake everyone up again, we are finished.
3664249382dSDoug Rabson 	 */
3674249382dSDoug Rabson 	pnp_write(PNP_CONFIG_CONTROL, PNP_CONFIG_CONTROL_WAIT_FOR_KEY);
3684249382dSDoug Rabson }
3694249382dSDoug Rabson 
3704249382dSDoug Rabson /*
3714249382dSDoug Rabson  * Process quirks for a logical device.. The card must be in Config state.
3724249382dSDoug Rabson  */
373fb0ef528SSeigo Tanimura void
3747a83aa63SWarner Losh pnp_check_quirks(uint32_t vendor_id, uint32_t logical_id, int ldn,
3757a83aa63SWarner Losh     struct isa_config *config)
3764249382dSDoug Rabson {
3774249382dSDoug Rabson 	struct pnp_quirk *qp;
3784249382dSDoug Rabson 
3794249382dSDoug Rabson 	for (qp = &pnp_quirks[0]; qp->vendor_id; qp++) {
3804249382dSDoug Rabson 		if (qp->vendor_id == vendor_id
3817a83aa63SWarner Losh 		    && (qp->logical_id == 0 || qp->logical_id == logical_id)) {
3824249382dSDoug Rabson 			switch (qp->type) {
3834249382dSDoug Rabson 			case PNP_QUIRK_WRITE_REG:
3844249382dSDoug Rabson 				pnp_write(PNP_SET_LDN, ldn);
3854249382dSDoug Rabson 				pnp_write(qp->arg1, qp->arg2);
3864249382dSDoug Rabson 				break;
387fb0ef528SSeigo Tanimura 			case PNP_QUIRK_EXTRA_IO:
388fb0ef528SSeigo Tanimura 				if (config == NULL)
389fb0ef528SSeigo Tanimura 					break;
390fb0ef528SSeigo Tanimura 				if (qp->arg1 != 0) {
391fb0ef528SSeigo Tanimura 					config->ic_nport++;
392fb0ef528SSeigo Tanimura 					config->ic_port[config->ic_nport - 1] = config->ic_port[0];
393fb0ef528SSeigo Tanimura 					config->ic_port[config->ic_nport - 1].ir_start += qp->arg1;
394fb0ef528SSeigo Tanimura 					config->ic_port[config->ic_nport - 1].ir_end += qp->arg1;
395fb0ef528SSeigo Tanimura 				}
396fb0ef528SSeigo Tanimura 				if (qp->arg2 != 0) {
397fb0ef528SSeigo Tanimura 					config->ic_nport++;
398fb0ef528SSeigo Tanimura 					config->ic_port[config->ic_nport - 1] = config->ic_port[0];
399fb0ef528SSeigo Tanimura 					config->ic_port[config->ic_nport - 1].ir_start += qp->arg2;
400fb0ef528SSeigo Tanimura 					config->ic_port[config->ic_nport - 1].ir_end += qp->arg2;
401fb0ef528SSeigo Tanimura 				}
402fb0ef528SSeigo Tanimura 				break;
4034249382dSDoug Rabson 			}
4044249382dSDoug Rabson 		}
4054249382dSDoug Rabson 	}
4064249382dSDoug Rabson }
4074249382dSDoug Rabson 
4084249382dSDoug Rabson /*
4094249382dSDoug Rabson  * Scan Resource Data for Logical Devices.
4104249382dSDoug Rabson  *
4114249382dSDoug Rabson  * This function exits as soon as it gets an error reading *ANY*
4125b45337dSDoug Rabson  * Resource Data or it reaches the end of Resource Data.  In the first
4134249382dSDoug Rabson  * case the return value will be TRUE, FALSE otherwise.
4144249382dSDoug Rabson  */
4154249382dSDoug Rabson static int
4165b45337dSDoug Rabson pnp_create_devices(device_t parent, pnp_id *p, int csn,
4175b45337dSDoug Rabson     u_char *resources, int len)
4184249382dSDoug Rabson {
4195b45337dSDoug Rabson 	u_char tag, *resp, *resinfo, *startres = 0;
4205b45337dSDoug Rabson 	int large_len, scanning = len, retval = FALSE;
4217a83aa63SWarner Losh 	uint32_t logical_id;
4224249382dSDoug Rabson 	device_t dev = 0;
4234249382dSDoug Rabson 	int ldn = 0;
4244249382dSDoug Rabson 	struct pnp_set_config_arg *csnldn;
4255b45337dSDoug Rabson 	char buf[100];
4264249382dSDoug Rabson 	char *desc = 0;
4274249382dSDoug Rabson 
4285b45337dSDoug Rabson 	resp = resources;
4295b45337dSDoug Rabson 	while (scanning > 0) {
4305b45337dSDoug Rabson 		tag = *resp++;
4315b45337dSDoug Rabson 		scanning--;
4325b45337dSDoug Rabson 		if (PNP_RES_TYPE(tag) != 0) {
4335b45337dSDoug Rabson 			/* Large resource */
4345b45337dSDoug Rabson 			if (scanning < 2) {
4354249382dSDoug Rabson 				scanning = 0;
4364249382dSDoug Rabson 				continue;
4374249382dSDoug Rabson 			}
4385b45337dSDoug Rabson 			large_len = resp[0] + (resp[1] << 8);
4395b45337dSDoug Rabson 			resp += 2;
4405b45337dSDoug Rabson 
4415b45337dSDoug Rabson 			if (scanning < large_len) {
4425b45337dSDoug Rabson 				scanning = 0;
4435b45337dSDoug Rabson 				continue;
4445b45337dSDoug Rabson 			}
4455b45337dSDoug Rabson 			resinfo = resp;
4465b45337dSDoug Rabson 			resp += large_len;
4475b45337dSDoug Rabson 			scanning -= large_len;
4485b45337dSDoug Rabson 
4495b45337dSDoug Rabson 			if (PNP_LRES_NUM(tag) == PNP_TAG_ID_ANSI) {
450d117cb4eSKazutaka YOKOTA 				if (dev) {
451d117cb4eSKazutaka YOKOTA 					/*
452d117cb4eSKazutaka YOKOTA 					 * This is an optional device
453d117cb4eSKazutaka YOKOTA 					 * indentifier string. Skipt it
454d117cb4eSKazutaka YOKOTA 					 * for now.
455d117cb4eSKazutaka YOKOTA 					 */
456d117cb4eSKazutaka YOKOTA 					continue;
457d117cb4eSKazutaka YOKOTA 				}
458d117cb4eSKazutaka YOKOTA 				/* else mandately card identifier string */
4595b45337dSDoug Rabson 				if (large_len > sizeof(buf) - 1)
4605b45337dSDoug Rabson 					large_len = sizeof(buf) - 1;
4615b45337dSDoug Rabson 				bcopy(resinfo, buf, large_len);
4625b45337dSDoug Rabson 
4635b45337dSDoug Rabson 				/*
4645b45337dSDoug Rabson 				 * Trim trailing spaces.
4655b45337dSDoug Rabson 				 */
4665b45337dSDoug Rabson 				while (buf[large_len-1] == ' ')
4675b45337dSDoug Rabson 					large_len--;
4685b45337dSDoug Rabson 				buf[large_len] = '\0';
4695b45337dSDoug Rabson 				desc = buf;
4705b45337dSDoug Rabson 				continue;
4715b45337dSDoug Rabson 			}
4725b45337dSDoug Rabson 
4735b45337dSDoug Rabson 			continue;
4745b45337dSDoug Rabson 		}
4755b45337dSDoug Rabson 
4765b45337dSDoug Rabson 		/* Small resource */
4775b45337dSDoug Rabson 		if (scanning < PNP_SRES_LEN(tag)) {
4785b45337dSDoug Rabson 			scanning = 0;
4795b45337dSDoug Rabson 			continue;
4805b45337dSDoug Rabson 		}
4815b45337dSDoug Rabson 		resinfo = resp;
4825b45337dSDoug Rabson 		resp += PNP_SRES_LEN(tag);
483c2ede4b3SMartin Blapp 		scanning -= PNP_SRES_LEN(tag);
4844249382dSDoug Rabson 
4854249382dSDoug Rabson 		switch (PNP_SRES_NUM(tag)) {
48648ab255eSPeter Wemm 		case PNP_TAG_LOGICAL_DEVICE:
4874249382dSDoug Rabson 			/*
4885b45337dSDoug Rabson 			 * Parse the resources for the previous
4895b45337dSDoug Rabson 			 * logical device (if any).
4905b45337dSDoug Rabson 			 */
4915b45337dSDoug Rabson 			if (startres) {
4925b45337dSDoug Rabson 				pnp_parse_resources(dev, startres,
4937a83aa63SWarner Losh 				    resinfo - startres - 1, ldn);
4945b45337dSDoug Rabson 				dev = 0;
4955b45337dSDoug Rabson 				startres = 0;
4965b45337dSDoug Rabson 			}
4975b45337dSDoug Rabson 
4985b45337dSDoug Rabson 			/*
4995b45337dSDoug Rabson 			 * A new logical device. Scan for end of
5005b45337dSDoug Rabson 			 * resources.
5014249382dSDoug Rabson 			 */
5024249382dSDoug Rabson 			bcopy(resinfo, &logical_id, 4);
503fb0ef528SSeigo Tanimura 			pnp_check_quirks(p->vendor_id, logical_id, ldn, NULL);
5045b45337dSDoug Rabson 			dev = BUS_ADD_CHILD(parent, ISA_ORDER_PNP, NULL, -1);
5054249382dSDoug Rabson 			if (desc)
5064249382dSDoug Rabson 				device_set_desc_copy(dev, desc);
507d117cb4eSKazutaka YOKOTA 			else
508d117cb4eSKazutaka YOKOTA 				device_set_desc_copy(dev,
509d117cb4eSKazutaka YOKOTA 				    pnp_eisaformat(logical_id));
5104249382dSDoug Rabson 			isa_set_vendorid(dev, p->vendor_id);
5114249382dSDoug Rabson 			isa_set_serial(dev, p->serial);
5124249382dSDoug Rabson 			isa_set_logicalid(dev, logical_id);
513c3959391SKazutaka YOKOTA 			isa_set_configattr(dev,
5147a83aa63SWarner Losh 			    ISACFGATTR_CANDISABLE | ISACFGATTR_DYNAMIC);
5155b45337dSDoug Rabson 			csnldn = malloc(sizeof *csnldn, M_DEVBUF, M_NOWAIT);
5164249382dSDoug Rabson 			if (!csnldn) {
5177a83aa63SWarner Losh 				device_printf(parent, "out of memory\n");
5184249382dSDoug Rabson 				scanning = 0;
5194249382dSDoug Rabson 				break;
5204249382dSDoug Rabson 			}
5214249382dSDoug Rabson 			csnldn->csn = csn;
5224249382dSDoug Rabson 			csnldn->ldn = ldn;
5237a83aa63SWarner Losh 			ISA_SET_CONFIG_CALLBACK(parent, dev, pnp_set_config,
5247a83aa63SWarner Losh 			    csnldn);
525132580b5SWarner Losh 			isa_set_pnp_csn(dev, csn);
526132580b5SWarner Losh 			isa_set_pnp_ldn(dev, ldn);
5274249382dSDoug Rabson 			ldn++;
5285b45337dSDoug Rabson 			startres = resp;
5294249382dSDoug Rabson 			break;
5304249382dSDoug Rabson 
5314249382dSDoug Rabson 		case PNP_TAG_END:
5325b45337dSDoug Rabson 			if (!startres) {
5337a83aa63SWarner Losh 				device_printf(parent, "malformed resources\n");
5345b45337dSDoug Rabson 				scanning = 0;
5355b45337dSDoug Rabson 				break;
5365b45337dSDoug Rabson 			}
5375b45337dSDoug Rabson 			pnp_parse_resources(dev, startres,
538c3959391SKazutaka YOKOTA 			    resinfo - startres - 1, ldn);
5395b45337dSDoug Rabson 			dev = 0;
5405b45337dSDoug Rabson 			startres = 0;
5414249382dSDoug Rabson 			scanning = 0;
5424249382dSDoug Rabson 			break;
5434249382dSDoug Rabson 
5444249382dSDoug Rabson 		default:
5454249382dSDoug Rabson 			/* Skip this resource */
5464249382dSDoug Rabson 			break;
5474249382dSDoug Rabson 		}
5484249382dSDoug Rabson 	}
5494249382dSDoug Rabson 
5507a83aa63SWarner Losh 	return (retval);
5514249382dSDoug Rabson }
5524249382dSDoug Rabson 
5534249382dSDoug Rabson /*
5545b45337dSDoug Rabson  * Read 'amount' bytes of resources from the card, allocating memory
5555b45337dSDoug Rabson  * as needed. If a buffer is already available, it should be passed in
5565b45337dSDoug Rabson  * '*resourcesp' and its length in '*spacep'. The number of resource
5575b45337dSDoug Rabson  * bytes already in the buffer should be passed in '*lenp'. The memory
5585b45337dSDoug Rabson  * allocated will be returned in '*resourcesp' with its size and the
5595b45337dSDoug Rabson  * number of bytes of resources in '*spacep' and '*lenp' respectively.
5608b7f9bdcSPoul-Henning Kamp  *
5618b7f9bdcSPoul-Henning Kamp  * XXX: Multiple problems here, we forget to free() stuff in one
5628b7f9bdcSPoul-Henning Kamp  * XXX: error return, and in another case we free (*resourcesp) but
5638b7f9bdcSPoul-Henning Kamp  * XXX: don't tell the caller.
5645b45337dSDoug Rabson  */
5655b45337dSDoug Rabson static int
5665b45337dSDoug Rabson pnp_read_bytes(int amount, u_char **resourcesp, int *spacep, int *lenp)
5675b45337dSDoug Rabson {
5685b45337dSDoug Rabson 	u_char *resources = *resourcesp;
5695b45337dSDoug Rabson 	u_char *newres;
5705b45337dSDoug Rabson 	int space = *spacep;
5715b45337dSDoug Rabson 	int len = *lenp;
5725b45337dSDoug Rabson 
5735b45337dSDoug Rabson 	if (space == 0) {
5745b45337dSDoug Rabson 		space = 1024;
5755b45337dSDoug Rabson 		resources = malloc(space, M_TEMP, M_NOWAIT);
5765b45337dSDoug Rabson 		if (!resources)
5777a83aa63SWarner Losh 			return (ENOMEM);
5785b45337dSDoug Rabson 	}
5795b45337dSDoug Rabson 
5805b45337dSDoug Rabson 	if (len + amount > space) {
5815b45337dSDoug Rabson 		int extra = 1024;
5825b45337dSDoug Rabson 		while (len + amount > space + extra)
5835b45337dSDoug Rabson 			extra += 1024;
5845b45337dSDoug Rabson 		newres = malloc(space + extra, M_TEMP, M_NOWAIT);
58530f5ad0fSPoul-Henning Kamp 		if (!newres) {
58630f5ad0fSPoul-Henning Kamp 			/* XXX: free resources */
5877a83aa63SWarner Losh 			return (ENOMEM);
58830f5ad0fSPoul-Henning Kamp 		}
5895b45337dSDoug Rabson 		bcopy(resources, newres, len);
5905b45337dSDoug Rabson 		free(resources, M_TEMP);
5915b45337dSDoug Rabson 		resources = newres;
5925b45337dSDoug Rabson 		space += extra;
5935b45337dSDoug Rabson 	}
5945b45337dSDoug Rabson 
5955b45337dSDoug Rabson 	if (pnp_get_resource_info(resources + len, amount) != amount)
5967a83aa63SWarner Losh 		return (EINVAL);
5975b45337dSDoug Rabson 	len += amount;
5985b45337dSDoug Rabson 
5995b45337dSDoug Rabson 	*resourcesp = resources;
6005b45337dSDoug Rabson 	*spacep = space;
6015b45337dSDoug Rabson 	*lenp = len;
6025b45337dSDoug Rabson 
6037a83aa63SWarner Losh 	return (0);
6045b45337dSDoug Rabson }
6055b45337dSDoug Rabson 
6065b45337dSDoug Rabson /*
6075b45337dSDoug Rabson  * Read all resources from the card, allocating memory as needed. If a
6085b45337dSDoug Rabson  * buffer is already available, it should be passed in '*resourcesp'
6095b45337dSDoug Rabson  * and its length in '*spacep'. The memory allocated will be returned
6105b45337dSDoug Rabson  * in '*resourcesp' with its size and the number of bytes of resources
6115b45337dSDoug Rabson  * in '*spacep' and '*lenp' respectively.
6125b45337dSDoug Rabson  */
6135b45337dSDoug Rabson static int
6145b45337dSDoug Rabson pnp_read_resources(u_char **resourcesp, int *spacep, int *lenp)
6155b45337dSDoug Rabson {
6165b45337dSDoug Rabson 	u_char *resources = *resourcesp;
6175b45337dSDoug Rabson 	int space = *spacep;
6185b45337dSDoug Rabson 	int len = 0;
6195b45337dSDoug Rabson 	int error, done;
6205b45337dSDoug Rabson 	u_char tag;
6215b45337dSDoug Rabson 
6225b45337dSDoug Rabson 	error = 0;
6235b45337dSDoug Rabson 	done = 0;
6245b45337dSDoug Rabson 	while (!done) {
6255b45337dSDoug Rabson 		error = pnp_read_bytes(1, &resources, &space, &len);
6265b45337dSDoug Rabson 		if (error)
6275b45337dSDoug Rabson 			goto out;
6285b45337dSDoug Rabson 		tag = resources[len-1];
6295b45337dSDoug Rabson 		if (PNP_RES_TYPE(tag) == 0) {
6305b45337dSDoug Rabson 			/*
6315b45337dSDoug Rabson 			 * Small resource, read contents.
6325b45337dSDoug Rabson 			 */
6335b45337dSDoug Rabson 			error = pnp_read_bytes(PNP_SRES_LEN(tag),
6345b45337dSDoug Rabson 			    &resources, &space, &len);
6355b45337dSDoug Rabson 			if (error)
6365b45337dSDoug Rabson 				goto out;
6375b45337dSDoug Rabson 			if (PNP_SRES_NUM(tag) == PNP_TAG_END)
6385b45337dSDoug Rabson 				done = 1;
6395b45337dSDoug Rabson 		} else {
6405b45337dSDoug Rabson 			/*
6415b45337dSDoug Rabson 			 * Large resource, read length and contents.
6425b45337dSDoug Rabson 			 */
6435b45337dSDoug Rabson 			error = pnp_read_bytes(2, &resources, &space, &len);
6445b45337dSDoug Rabson 			if (error)
6455b45337dSDoug Rabson 				goto out;
6465b45337dSDoug Rabson 			error = pnp_read_bytes(resources[len-2]
6477a83aa63SWarner Losh 			    + (resources[len-1] << 8), &resources, &space,
6487a83aa63SWarner Losh 			    &len);
6495b45337dSDoug Rabson 			if (error)
6505b45337dSDoug Rabson 				goto out;
6515b45337dSDoug Rabson 		}
6525b45337dSDoug Rabson 	}
6535b45337dSDoug Rabson 
6545b45337dSDoug Rabson  out:
6555b45337dSDoug Rabson 	*resourcesp = resources;
6565b45337dSDoug Rabson 	*spacep = space;
6575b45337dSDoug Rabson 	*lenp = len;
6587a83aa63SWarner Losh 	return (error);
6595b45337dSDoug Rabson }
6605b45337dSDoug Rabson 
6615b45337dSDoug Rabson /*
6624249382dSDoug Rabson  * Run the isolation protocol. Use pnp_rd_port as the READ_DATA port
6634249382dSDoug Rabson  * value (caller should try multiple READ_DATA locations before giving
6644249382dSDoug Rabson  * up). Upon exiting, all cards are aware that they should use
6654249382dSDoug Rabson  * pnp_rd_port as the READ_DATA port.
6664249382dSDoug Rabson  *
6674249382dSDoug Rabson  * In the first pass, a csn is assigned to each board and pnp_id's
6684249382dSDoug Rabson  * are saved to an array, pnp_devices. In the second pass, each
6694249382dSDoug Rabson  * card is woken up and the device configuration is called.
6704249382dSDoug Rabson  */
6714249382dSDoug Rabson static int
6724249382dSDoug Rabson pnp_isolation_protocol(device_t parent)
6734249382dSDoug Rabson {
6744249382dSDoug Rabson 	int csn;
6754249382dSDoug Rabson 	pnp_id id;
6765b45337dSDoug Rabson 	int found = 0, len;
6775b45337dSDoug Rabson 	u_char *resources = 0;
6785b45337dSDoug Rabson 	int space = 0;
6795b45337dSDoug Rabson 	int error;
6807698537bSYoshihiro Takahashi #ifdef PC98
6817698537bSYoshihiro Takahashi 	int n, necpnp;
6827698537bSYoshihiro Takahashi 	u_char buffer[10];
6837698537bSYoshihiro Takahashi #endif
6844249382dSDoug Rabson 
6854249382dSDoug Rabson 	/*
6864249382dSDoug Rabson 	 * Put all cards into the Sleep state so that we can clear
6874249382dSDoug Rabson 	 * their CSNs.
6884249382dSDoug Rabson 	 */
6894249382dSDoug Rabson 	pnp_send_initiation_key();
6904249382dSDoug Rabson 
6914249382dSDoug Rabson 	/*
6924249382dSDoug Rabson 	 * Clear the CSN for all cards.
6934249382dSDoug Rabson 	 */
6944249382dSDoug Rabson 	pnp_write(PNP_CONFIG_CONTROL, PNP_CONFIG_CONTROL_RESET_CSN);
6954249382dSDoug Rabson 
6964249382dSDoug Rabson 	/*
6974249382dSDoug Rabson 	 * Move all cards to the Isolation state.
6984249382dSDoug Rabson 	 */
6994249382dSDoug Rabson 	pnp_write(PNP_WAKE, 0);
7004249382dSDoug Rabson 
7014249382dSDoug Rabson 	/*
7024249382dSDoug Rabson 	 * Tell them where the read point is going to be this time.
7034249382dSDoug Rabson 	 */
7044249382dSDoug Rabson 	pnp_write(PNP_SET_RD_DATA, pnp_rd_port);
7054249382dSDoug Rabson 
7064249382dSDoug Rabson 	for (csn = 1; csn < PNP_MAX_CARDS; csn++) {
7074249382dSDoug Rabson 		/*
7084249382dSDoug Rabson 		 * Start the serial isolation protocol.
7094249382dSDoug Rabson 		 */
7104249382dSDoug Rabson 		outb(_PNP_ADDRESS, PNP_SERIAL_ISOLATION);
7114249382dSDoug Rabson 		DELAY(1000);	/* Delay 1 msec */
7124249382dSDoug Rabson 
7134249382dSDoug Rabson 		if (pnp_get_serial(&id)) {
7144249382dSDoug Rabson 			/*
7154249382dSDoug Rabson 			 * We have read the id from a card
7164249382dSDoug Rabson 			 * successfully. The card which won the
7174249382dSDoug Rabson 			 * isolation protocol will be in Isolation
7185b45337dSDoug Rabson 			 * mode and all others will be in Sleep.
7194249382dSDoug Rabson 			 * Program the CSN of the isolated card
7204249382dSDoug Rabson 			 * (taking it to Config state) and read its
7214249382dSDoug Rabson 			 * resources, creating devices as we find
7224249382dSDoug Rabson 			 * logical devices on the card.
7234249382dSDoug Rabson 			 */
7244249382dSDoug Rabson 			pnp_write(PNP_SET_CSN, csn);
7257698537bSYoshihiro Takahashi #ifdef PC98
7267698537bSYoshihiro Takahashi 			if (bootverbose)
7277698537bSYoshihiro Takahashi 				printf("PnP Vendor ID = %x\n", id.vendor_id);
7287698537bSYoshihiro Takahashi 			/* Check for NEC PnP (9 bytes serial). */
7297698537bSYoshihiro Takahashi 			for (n = necpnp = 0; necids[n].vendor_id; n++) {
7307698537bSYoshihiro Takahashi 				if (id.vendor_id == necids[n].vendor_id) {
7317698537bSYoshihiro Takahashi 					necpnp = 1;
7327698537bSYoshihiro Takahashi 					break;
7337698537bSYoshihiro Takahashi 				}
7347698537bSYoshihiro Takahashi 			}
7357698537bSYoshihiro Takahashi 			if (necpnp) {
7367698537bSYoshihiro Takahashi 				if (bootverbose)
7377a83aa63SWarner Losh 					printf("An NEC-PnP card (%s).\n",
7387698537bSYoshihiro Takahashi 					    pnp_eisaformat(id.vendor_id));
7397698537bSYoshihiro Takahashi 				/*  Read dummy 9 bytes serial area. */
7407698537bSYoshihiro Takahashi 				pnp_get_resource_info(buffer, 9);
7417698537bSYoshihiro Takahashi 			} else {
7427698537bSYoshihiro Takahashi 				if (bootverbose)
7437a83aa63SWarner Losh 					printf("A Normal-ISA-PnP card (%s).\n",
7447698537bSYoshihiro Takahashi 					    pnp_eisaformat(id.vendor_id));
7457698537bSYoshihiro Takahashi 			}
7467698537bSYoshihiro Takahashi 			if (bootverbose)
7477698537bSYoshihiro Takahashi 				printf("Reading PnP configuration for %s.\n",
7487698537bSYoshihiro Takahashi 				    pnp_eisaformat(id.vendor_id));
7497698537bSYoshihiro Takahashi #endif
7507a83aa63SWarner Losh 			error = pnp_read_resources(&resources, &space, &len);
7515b45337dSDoug Rabson 			if (error)
7525b45337dSDoug Rabson 				break;
7537a83aa63SWarner Losh 			pnp_create_devices(parent, &id, csn, resources, len);
7544249382dSDoug Rabson 			found++;
7554249382dSDoug Rabson 		} else
7564249382dSDoug Rabson 			break;
7574249382dSDoug Rabson 
7584249382dSDoug Rabson 		/*
7594249382dSDoug Rabson 		 * Put this card back to the Sleep state and
7604249382dSDoug Rabson 		 * simultaneously move all cards which don't have a
7614249382dSDoug Rabson 		 * CSN yet to Isolation state.
7624249382dSDoug Rabson 		 */
7634249382dSDoug Rabson 		pnp_write(PNP_WAKE, 0);
7644249382dSDoug Rabson 	}
7654249382dSDoug Rabson 
7664249382dSDoug Rabson 	/*
7674249382dSDoug Rabson 	 * Unless we have chosen the wrong read port, all cards will
7684249382dSDoug Rabson 	 * be in Sleep state. Put them back into WaitForKey for
7694249382dSDoug Rabson 	 * now. Their resources will be programmed later.
7704249382dSDoug Rabson 	 */
7714249382dSDoug Rabson 	pnp_write(PNP_CONFIG_CONTROL, PNP_CONFIG_CONTROL_WAIT_FOR_KEY);
7724249382dSDoug Rabson 
7735b45337dSDoug Rabson 	/*
7745b45337dSDoug Rabson 	 * Cleanup.
7755b45337dSDoug Rabson 	 */
7765b45337dSDoug Rabson 	if (resources)
7775b45337dSDoug Rabson 		free(resources, M_TEMP);
7785b45337dSDoug Rabson 
7797a83aa63SWarner Losh 	return (found);
7804249382dSDoug Rabson }
7814249382dSDoug Rabson 
7824249382dSDoug Rabson 
7834249382dSDoug Rabson /*
7844249382dSDoug Rabson  * pnp_identify()
7854249382dSDoug Rabson  *
7864249382dSDoug Rabson  * autoconfiguration of pnp devices. This routine just runs the
7874249382dSDoug Rabson  * isolation protocol over several ports, until one is successful.
7884249382dSDoug Rabson  *
7894249382dSDoug Rabson  * may be called more than once ?
7904249382dSDoug Rabson  *
7914249382dSDoug Rabson  */
7924249382dSDoug Rabson 
7934249382dSDoug Rabson static void
7944249382dSDoug Rabson pnp_identify(driver_t *driver, device_t parent)
7954249382dSDoug Rabson {
7964249382dSDoug Rabson 	int num_pnp_devs;
7974249382dSDoug Rabson 
7984249382dSDoug Rabson 	/* Try various READ_DATA ports from 0x203-0x3ff */
7994249382dSDoug Rabson 	for (pnp_rd_port = 0x80; (pnp_rd_port < 0xff); pnp_rd_port += 0x10) {
8004249382dSDoug Rabson 		if (bootverbose)
801934bd5edSWarner Losh 			printf("pnp_identify: Trying Read_Port at %x\n",
8027a83aa63SWarner Losh 			    (pnp_rd_port << 2) | 0x3);
8034249382dSDoug Rabson 
8044249382dSDoug Rabson 		num_pnp_devs = pnp_isolation_protocol(parent);
8054249382dSDoug Rabson 		if (num_pnp_devs)
8064249382dSDoug Rabson 			break;
8074249382dSDoug Rabson 	}
8082e63992fSWarner Losh 	if (bootverbose)
8092e63992fSWarner Losh 		printf("PNP Identify complete\n");
8104249382dSDoug Rabson }
8114249382dSDoug Rabson 
8124249382dSDoug Rabson static device_method_t pnp_methods[] = {
8134249382dSDoug Rabson 	/* Device interface */
8144249382dSDoug Rabson 	DEVMETHOD(device_identify,	pnp_identify),
8154249382dSDoug Rabson 
8164249382dSDoug Rabson 	{ 0, 0 }
8174249382dSDoug Rabson };
8184249382dSDoug Rabson 
8194249382dSDoug Rabson static driver_t pnp_driver = {
8204249382dSDoug Rabson 	"pnp",
8214249382dSDoug Rabson 	pnp_methods,
8224249382dSDoug Rabson 	1,			/* no softc */
8234249382dSDoug Rabson };
8244249382dSDoug Rabson 
8254249382dSDoug Rabson static devclass_t pnp_devclass;
8264249382dSDoug Rabson 
8274249382dSDoug Rabson DRIVER_MODULE(pnp, isa, pnp_driver, pnp_devclass, 0, 0);
828