pnp.c revision 139267
1/*
2 * Copyright (c) 1996, Sujal M. Patel
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 *
26 *      from: pnp.c,v 1.11 1999/05/06 22:11:19 peter Exp
27 */
28
29#include <sys/cdefs.h>
30__FBSDID("$FreeBSD: head/sys/isa/pnp.c 139267 2004-12-24 21:53:21Z imp $");
31
32#include <sys/param.h>
33#include <sys/systm.h>
34#include <sys/kernel.h>
35#include <sys/module.h>
36#include <sys/bus.h>
37#include <sys/malloc.h>
38#include <isa/isavar.h>
39#include <isa/pnpreg.h>
40#include <isa/pnpvar.h>
41#include <machine/bus.h>
42
43typedef struct _pnp_id {
44	u_int32_t vendor_id;
45	u_int32_t serial;
46	u_char checksum;
47} pnp_id;
48
49struct pnp_set_config_arg {
50	int	csn;		/* Card number to configure */
51	int	ldn;		/* Logical device on card */
52};
53
54struct pnp_quirk {
55	u_int32_t vendor_id;	/* Vendor of the card */
56	u_int32_t logical_id;	/* ID of the device with quirk */
57	int	type;
58#define PNP_QUIRK_WRITE_REG	1 /* Need to write a pnp register  */
59#define PNP_QUIRK_EXTRA_IO	2 /* Has extra io ports  */
60	int	arg1;
61	int	arg2;
62};
63
64struct pnp_quirk pnp_quirks[] = {
65	/*
66	 * The Gravis UltraSound needs register 0xf2 to be set to 0xff
67	 * to enable power.
68	 * XXX need to know the logical device id.
69	 */
70	{ 0x0100561e /* GRV0001 */,	0,
71	  PNP_QUIRK_WRITE_REG,	0xf2,	 0xff },
72	/*
73	 * An emu8000 does not give us other than the first
74	 * port.
75	 */
76	{ 0x26008c0e /* SB16 */,	0x21008c0e,
77	  PNP_QUIRK_EXTRA_IO,	0x400,	 0x800 },
78	{ 0x42008c0e /* SB32(CTL0042) */,	0x21008c0e,
79	  PNP_QUIRK_EXTRA_IO,	0x400,	 0x800 },
80	{ 0x44008c0e /* SB32(CTL0044) */,	0x21008c0e,
81	  PNP_QUIRK_EXTRA_IO,	0x400,	 0x800 },
82	{ 0x49008c0e /* SB32(CTL0049) */,	0x21008c0e,
83	  PNP_QUIRK_EXTRA_IO,	0x400,	 0x800 },
84	{ 0xf1008c0e /* SB32(CTL00f1) */,	0x21008c0e,
85	  PNP_QUIRK_EXTRA_IO,	0x400,	 0x800 },
86	{ 0xc1008c0e /* SB64(CTL00c1) */,	0x22008c0e,
87	  PNP_QUIRK_EXTRA_IO,	0x400,	 0x800 },
88	{ 0xc5008c0e /* SB64(CTL00c5) */,	0x22008c0e,
89	  PNP_QUIRK_EXTRA_IO,	0x400,	 0x800 },
90	{ 0xe4008c0e /* SB64(CTL00e4) */,	0x22008c0e,
91	  PNP_QUIRK_EXTRA_IO,	0x400,	 0x800 },
92
93	{ 0 }
94};
95
96#ifdef PC98
97/* Some NEC PnP cards have 9 bytes serial code. */
98static pnp_id necids[] = {
99	{0x4180a3b8, 0xffffffff, 0x00},	/* PC-9801CB-B04 (NEC8041) */
100	{0x5181a3b8, 0xffffffff, 0x46},	/* PC-9821CB2-B04(NEC8151) */
101	{0x5182a3b8, 0xffffffff, 0xb8},	/* PC-9801-XX    (NEC8251) */
102	{0x9181a3b8, 0xffffffff, 0x00},	/* PC-9801-120   (NEC8191) */
103	{0, 0, 0}
104};
105#endif
106
107/* The READ_DATA port that we are using currently */
108static int pnp_rd_port;
109
110static void   pnp_send_initiation_key(void);
111static int    pnp_get_serial(pnp_id *p);
112static int    pnp_isolation_protocol(device_t parent);
113
114char *
115pnp_eisaformat(u_int32_t id)
116{
117	u_int8_t *data = (u_int8_t *) &id;
118	static char idbuf[8];
119	const char  hextoascii[] = "0123456789abcdef";
120
121	idbuf[0] = '@' + ((data[0] & 0x7c) >> 2);
122	idbuf[1] = '@' + (((data[0] & 0x3) << 3) + ((data[1] & 0xe0) >> 5));
123	idbuf[2] = '@' + (data[1] & 0x1f);
124	idbuf[3] = hextoascii[(data[2] >> 4)];
125	idbuf[4] = hextoascii[(data[2] & 0xf)];
126	idbuf[5] = hextoascii[(data[3] >> 4)];
127	idbuf[6] = hextoascii[(data[3] & 0xf)];
128	idbuf[7] = 0;
129	return(idbuf);
130}
131
132static void
133pnp_write(int d, u_char r)
134{
135	outb (_PNP_ADDRESS, d);
136	outb (_PNP_WRITE_DATA, r);
137}
138
139/*
140 * Send Initiation LFSR as described in "Plug and Play ISA Specification",
141 * Intel May 94.
142 */
143static void
144pnp_send_initiation_key()
145{
146	int cur, i;
147
148	/* Reset the LSFR */
149	outb(_PNP_ADDRESS, 0);
150	outb(_PNP_ADDRESS, 0); /* yes, we do need it twice! */
151
152	cur = 0x6a;
153	outb(_PNP_ADDRESS, cur);
154
155	for (i = 1; i < 32; i++) {
156		cur = (cur >> 1) | (((cur ^ (cur >> 1)) << 7) & 0xff);
157		outb(_PNP_ADDRESS, cur);
158	}
159}
160
161
162/*
163 * Get the device's serial number.  Returns 1 if the serial is valid.
164 */
165static int
166pnp_get_serial(pnp_id *p)
167{
168	int i, bit, valid = 0, sum = 0x6a;
169	u_char *data = (u_char *)p;
170
171	bzero(data, sizeof(char) * 9);
172	outb(_PNP_ADDRESS, PNP_SERIAL_ISOLATION);
173	for (i = 0; i < 72; i++) {
174		bit = inb((pnp_rd_port << 2) | 0x3) == 0x55;
175		DELAY(250);	/* Delay 250 usec */
176
177		/* Can't Short Circuit the next evaluation, so 'and' is last */
178		bit = (inb((pnp_rd_port << 2) | 0x3) == 0xaa) && bit;
179		DELAY(250);	/* Delay 250 usec */
180
181		valid = valid || bit;
182
183		if (i < 64)
184			sum = (sum >> 1) |
185				(((sum ^ (sum >> 1) ^ bit) << 7) & 0xff);
186
187		data[i / 8] = (data[i / 8] >> 1) | (bit ? 0x80 : 0);
188	}
189
190	valid = valid && (data[8] == sum);
191
192	return valid;
193}
194
195/*
196 * Fill's the buffer with resource info from the device.
197 * Returns the number of characters read.
198 */
199static int
200pnp_get_resource_info(u_char *buffer, int len)
201{
202	int i, j, count;
203	u_char temp;
204
205	count = 0;
206	for (i = 0; i < len; i++) {
207		outb(_PNP_ADDRESS, PNP_STATUS);
208		for (j = 0; j < 100; j++) {
209			if ((inb((pnp_rd_port << 2) | 0x3)) & 0x1)
210				break;
211			DELAY(1);
212		}
213		if (j == 100) {
214			printf("PnP device failed to report resource data\n");
215			return count;
216		}
217		outb(_PNP_ADDRESS, PNP_RESOURCE_DATA);
218		temp = inb((pnp_rd_port << 2) | 0x3);
219		if (buffer != NULL)
220			buffer[i] = temp;
221		count++;
222	}
223	return count;
224}
225
226/*
227 * This function is called after the bus has assigned resource
228 * locations for a logical device.
229 */
230static void
231pnp_set_config(void *arg, struct isa_config *config, int enable)
232{
233	int csn = ((struct pnp_set_config_arg *) arg)->csn;
234	int ldn = ((struct pnp_set_config_arg *) arg)->ldn;
235	int i;
236
237	/*
238	 * First put all cards into Sleep state with the initiation
239	 * key, then put our card into Config state.
240	 */
241	pnp_send_initiation_key();
242	pnp_write(PNP_WAKE, csn);
243
244	/*
245	 * Select our logical device so that we can program it.
246	 */
247	pnp_write(PNP_SET_LDN, ldn);
248
249	/*
250	 * Constrain the number of resources we will try to program
251	 */
252	if (config->ic_nmem > ISA_PNP_NMEM) {
253	    printf("too many ISA memory ranges (%d > %d)\n", config->ic_nmem, ISA_PNP_NMEM);
254	    config->ic_nmem = ISA_PNP_NMEM;
255	}
256	if (config->ic_nport > ISA_PNP_NPORT) {
257	    printf("too many ISA I/O ranges (%d > %d)\n", config->ic_nport, ISA_PNP_NPORT);
258	    config->ic_nport = ISA_PNP_NPORT;
259	}
260	if (config->ic_nirq > ISA_PNP_NIRQ) {
261	    printf("too many ISA IRQs (%d > %d)\n", config->ic_nirq, ISA_PNP_NIRQ);
262	    config->ic_nirq = ISA_PNP_NIRQ;
263	}
264	if (config->ic_ndrq > ISA_PNP_NDRQ) {
265	    printf("too many ISA DRQs (%d > %d)\n", config->ic_ndrq, ISA_PNP_NDRQ);
266	    config->ic_ndrq = ISA_PNP_NDRQ;
267	}
268
269	/*
270	 * Now program the resources.
271	 */
272	for (i = 0; i < config->ic_nmem; i++) {
273		u_int32_t start;
274		u_int32_t size;
275
276		/* XXX: should handle memory control register, 32 bit memory */
277		if (config->ic_mem[i].ir_size == 0) {
278			pnp_write(PNP_MEM_BASE_HIGH(i), 0);
279			pnp_write(PNP_MEM_BASE_LOW(i), 0);
280			pnp_write(PNP_MEM_RANGE_HIGH(i), 0);
281			pnp_write(PNP_MEM_RANGE_LOW(i), 0);
282		} else {
283			start = config->ic_mem[i].ir_start;
284			size =  config->ic_mem[i].ir_size;
285			if (start & 0xff)
286				panic("pnp_set_config: bogus memory assignment");
287			pnp_write(PNP_MEM_BASE_HIGH(i), (start >> 16) & 0xff);
288			pnp_write(PNP_MEM_BASE_LOW(i), (start >> 8) & 0xff);
289			pnp_write(PNP_MEM_RANGE_HIGH(i), (size >> 16) & 0xff);
290			pnp_write(PNP_MEM_RANGE_LOW(i), (size >> 8) & 0xff);
291		}
292	}
293	for (; i < ISA_PNP_NMEM; i++) {
294		pnp_write(PNP_MEM_BASE_HIGH(i), 0);
295		pnp_write(PNP_MEM_BASE_LOW(i), 0);
296		pnp_write(PNP_MEM_RANGE_HIGH(i), 0);
297		pnp_write(PNP_MEM_RANGE_LOW(i), 0);
298	}
299
300	for (i = 0; i < config->ic_nport; i++) {
301		u_int32_t start;
302
303		if (config->ic_port[i].ir_size == 0) {
304			pnp_write(PNP_IO_BASE_HIGH(i), 0);
305			pnp_write(PNP_IO_BASE_LOW(i), 0);
306		} else {
307			start = config->ic_port[i].ir_start;
308			pnp_write(PNP_IO_BASE_HIGH(i), (start >> 8) & 0xff);
309			pnp_write(PNP_IO_BASE_LOW(i), (start >> 0) & 0xff);
310		}
311	}
312	for (; i < ISA_PNP_NPORT; i++) {
313		pnp_write(PNP_IO_BASE_HIGH(i), 0);
314		pnp_write(PNP_IO_BASE_LOW(i), 0);
315	}
316
317	for (i = 0; i < config->ic_nirq; i++) {
318		int irq;
319
320		/* XXX: interrupt type */
321		if (config->ic_irqmask[i] == 0) {
322			pnp_write(PNP_IRQ_LEVEL(i), 0);
323			pnp_write(PNP_IRQ_TYPE(i), 2);
324		} else {
325			irq = ffs(config->ic_irqmask[i]) - 1;
326			pnp_write(PNP_IRQ_LEVEL(i), irq);
327			pnp_write(PNP_IRQ_TYPE(i), 2); /* XXX */
328		}
329	}
330	for (; i < ISA_PNP_NIRQ; i++) {
331		/*
332		 * IRQ 0 is not a valid interrupt selection and
333		 * represents no interrupt selection.
334		 */
335		pnp_write(PNP_IRQ_LEVEL(i), 0);
336		pnp_write(PNP_IRQ_TYPE(i), 2);
337	}
338
339	for (i = 0; i < config->ic_ndrq; i++) {
340		int drq;
341
342		if (config->ic_drqmask[i] == 0) {
343			pnp_write(PNP_DMA_CHANNEL(i), 4);
344		} else {
345			drq = ffs(config->ic_drqmask[i]) - 1;
346			pnp_write(PNP_DMA_CHANNEL(i), drq);
347		}
348	}
349	for (; i < ISA_PNP_NDRQ; i++) {
350		/*
351		 * DMA channel 4, the cascade channel is used to
352		 * indicate no DMA channel is active.
353		 */
354		pnp_write(PNP_DMA_CHANNEL(i), 4);
355	}
356
357	pnp_write(PNP_ACTIVATE, enable ? 1 : 0);
358
359	/*
360	 * Wake everyone up again, we are finished.
361	 */
362	pnp_write(PNP_CONFIG_CONTROL, PNP_CONFIG_CONTROL_WAIT_FOR_KEY);
363}
364
365/*
366 * Process quirks for a logical device.. The card must be in Config state.
367 */
368void
369pnp_check_quirks(u_int32_t vendor_id, u_int32_t logical_id, int ldn, struct isa_config *config)
370{
371	struct pnp_quirk *qp;
372
373	for (qp = &pnp_quirks[0]; qp->vendor_id; qp++) {
374		if (qp->vendor_id == vendor_id
375		    && (qp->logical_id == 0
376			|| qp->logical_id == logical_id)) {
377			switch (qp->type) {
378			case PNP_QUIRK_WRITE_REG:
379				pnp_write(PNP_SET_LDN, ldn);
380				pnp_write(qp->arg1, qp->arg2);
381				break;
382			case PNP_QUIRK_EXTRA_IO:
383				if (config == NULL)
384					break;
385				if (qp->arg1 != 0) {
386					config->ic_nport++;
387					config->ic_port[config->ic_nport - 1] = config->ic_port[0];
388					config->ic_port[config->ic_nport - 1].ir_start += qp->arg1;
389					config->ic_port[config->ic_nport - 1].ir_end += qp->arg1;
390				}
391				if (qp->arg2 != 0) {
392					config->ic_nport++;
393					config->ic_port[config->ic_nport - 1] = config->ic_port[0];
394					config->ic_port[config->ic_nport - 1].ir_start += qp->arg2;
395					config->ic_port[config->ic_nport - 1].ir_end += qp->arg2;
396				}
397				break;
398			}
399		}
400	}
401}
402
403/*
404 * Scan Resource Data for Logical Devices.
405 *
406 * This function exits as soon as it gets an error reading *ANY*
407 * Resource Data or it reaches the end of Resource Data.  In the first
408 * case the return value will be TRUE, FALSE otherwise.
409 */
410static int
411pnp_create_devices(device_t parent, pnp_id *p, int csn,
412		   u_char *resources, int len)
413{
414	u_char tag, *resp, *resinfo, *startres = 0;
415	int large_len, scanning = len, retval = FALSE;
416	u_int32_t logical_id;
417	device_t dev = 0;
418	int ldn = 0;
419	struct pnp_set_config_arg *csnldn;
420	char buf[100];
421	char *desc = 0;
422
423	resp = resources;
424	while (scanning > 0) {
425		tag = *resp++;
426		scanning--;
427		if (PNP_RES_TYPE(tag) != 0) {
428			/* Large resource */
429			if (scanning < 2) {
430				scanning = 0;
431				continue;
432			}
433			large_len = resp[0] + (resp[1] << 8);
434			resp += 2;
435
436			if (scanning < large_len) {
437				scanning = 0;
438				continue;
439			}
440			resinfo = resp;
441			resp += large_len;
442			scanning -= large_len;
443
444			if (PNP_LRES_NUM(tag) == PNP_TAG_ID_ANSI) {
445				if (dev) {
446					/*
447					 * This is an optional device
448					 * indentifier string. Skipt it
449					 * for now.
450					 */
451					continue;
452				}
453				/* else mandately card identifier string */
454				if (large_len > sizeof(buf) - 1)
455					large_len = sizeof(buf) - 1;
456				bcopy(resinfo, buf, large_len);
457
458				/*
459				 * Trim trailing spaces.
460				 */
461				while (buf[large_len-1] == ' ')
462					large_len--;
463				buf[large_len] = '\0';
464				desc = buf;
465				continue;
466			}
467
468			continue;
469		}
470
471		/* Small resource */
472		if (scanning < PNP_SRES_LEN(tag)) {
473			scanning = 0;
474			continue;
475		}
476		resinfo = resp;
477		resp += PNP_SRES_LEN(tag);
478		scanning -= PNP_SRES_LEN(tag);;
479
480		switch (PNP_SRES_NUM(tag)) {
481		case PNP_TAG_LOGICAL_DEVICE:
482			/*
483			 * Parse the resources for the previous
484			 * logical device (if any).
485			 */
486			if (startres) {
487				pnp_parse_resources(dev, startres,
488						    resinfo - startres - 1,
489						    ldn);
490				dev = 0;
491				startres = 0;
492			}
493
494			/*
495			 * A new logical device. Scan for end of
496			 * resources.
497			 */
498			bcopy(resinfo, &logical_id, 4);
499			pnp_check_quirks(p->vendor_id, logical_id, ldn, NULL);
500			dev = BUS_ADD_CHILD(parent, ISA_ORDER_PNP, NULL, -1);
501			if (desc)
502				device_set_desc_copy(dev, desc);
503			else
504				device_set_desc_copy(dev,
505						     pnp_eisaformat(logical_id));
506			isa_set_vendorid(dev, p->vendor_id);
507			isa_set_serial(dev, p->serial);
508			isa_set_logicalid(dev, logical_id);
509			isa_set_configattr(dev,
510					   ISACFGATTR_CANDISABLE |
511					   ISACFGATTR_DYNAMIC);
512			csnldn = malloc(sizeof *csnldn, M_DEVBUF, M_NOWAIT);
513			if (!csnldn) {
514				device_printf(parent,
515					      "out of memory\n");
516				scanning = 0;
517				break;
518			}
519			csnldn->csn = csn;
520			csnldn->ldn = ldn;
521			ISA_SET_CONFIG_CALLBACK(parent, dev,
522						pnp_set_config, csnldn);
523			ldn++;
524			startres = resp;
525			break;
526
527		case PNP_TAG_END:
528			if (!startres) {
529				device_printf(parent,
530					      "malformed resources\n");
531				scanning = 0;
532				break;
533			}
534			pnp_parse_resources(dev, startres,
535					    resinfo - startres - 1, ldn);
536			dev = 0;
537			startres = 0;
538			scanning = 0;
539			break;
540
541		default:
542			/* Skip this resource */
543			break;
544		}
545	}
546
547	return retval;
548}
549
550/*
551 * Read 'amount' bytes of resources from the card, allocating memory
552 * as needed. If a buffer is already available, it should be passed in
553 * '*resourcesp' and its length in '*spacep'. The number of resource
554 * bytes already in the buffer should be passed in '*lenp'. The memory
555 * allocated will be returned in '*resourcesp' with its size and the
556 * number of bytes of resources in '*spacep' and '*lenp' respectively.
557 *
558 * XXX: Multiple problems here, we forget to free() stuff in one
559 * XXX: error return, and in another case we free (*resourcesp) but
560 * XXX: don't tell the caller.
561 */
562static int
563pnp_read_bytes(int amount, u_char **resourcesp, int *spacep, int *lenp)
564{
565	u_char *resources = *resourcesp;
566	u_char *newres;
567	int space = *spacep;
568	int len = *lenp;
569
570	if (space == 0) {
571		space = 1024;
572		resources = malloc(space, M_TEMP, M_NOWAIT);
573		if (!resources)
574			return ENOMEM;
575	}
576
577	if (len + amount > space) {
578		int extra = 1024;
579		while (len + amount > space + extra)
580			extra += 1024;
581		newres = malloc(space + extra, M_TEMP, M_NOWAIT);
582		if (!newres) {
583			/* XXX: free resources */
584			return ENOMEM;
585		}
586		bcopy(resources, newres, len);
587		free(resources, M_TEMP);
588		resources = newres;
589		space += extra;
590	}
591
592	if (pnp_get_resource_info(resources + len, amount) != amount)
593		return EINVAL;
594	len += amount;
595
596	*resourcesp = resources;
597	*spacep = space;
598	*lenp = len;
599
600	return 0;
601}
602
603/*
604 * Read all resources from the card, allocating memory as needed. If a
605 * buffer is already available, it should be passed in '*resourcesp'
606 * and its length in '*spacep'. The memory allocated will be returned
607 * in '*resourcesp' with its size and the number of bytes of resources
608 * in '*spacep' and '*lenp' respectively.
609 */
610static int
611pnp_read_resources(u_char **resourcesp, int *spacep, int *lenp)
612{
613	u_char *resources = *resourcesp;
614	int space = *spacep;
615	int len = 0;
616	int error, done;
617	u_char tag;
618
619	error = 0;
620	done = 0;
621	while (!done) {
622		error = pnp_read_bytes(1, &resources, &space, &len);
623		if (error)
624			goto out;
625		tag = resources[len-1];
626		if (PNP_RES_TYPE(tag) == 0) {
627			/*
628			 * Small resource, read contents.
629			 */
630			error = pnp_read_bytes(PNP_SRES_LEN(tag),
631					       &resources, &space, &len);
632			if (error)
633				goto out;
634			if (PNP_SRES_NUM(tag) == PNP_TAG_END)
635				done = 1;
636		} else {
637			/*
638			 * Large resource, read length and contents.
639			 */
640			error = pnp_read_bytes(2, &resources, &space, &len);
641			if (error)
642				goto out;
643			error = pnp_read_bytes(resources[len-2]
644					       + (resources[len-1] << 8),
645					       &resources, &space, &len);
646			if (error)
647				goto out;
648		}
649	}
650
651 out:
652	*resourcesp = resources;
653	*spacep = space;
654	*lenp = len;
655	return error;
656}
657
658/*
659 * Run the isolation protocol. Use pnp_rd_port as the READ_DATA port
660 * value (caller should try multiple READ_DATA locations before giving
661 * up). Upon exiting, all cards are aware that they should use
662 * pnp_rd_port as the READ_DATA port.
663 *
664 * In the first pass, a csn is assigned to each board and pnp_id's
665 * are saved to an array, pnp_devices. In the second pass, each
666 * card is woken up and the device configuration is called.
667 */
668static int
669pnp_isolation_protocol(device_t parent)
670{
671	int csn;
672	pnp_id id;
673	int found = 0, len;
674	u_char *resources = 0;
675	int space = 0;
676	int error;
677#ifdef PC98
678	int n, necpnp;
679	u_char buffer[10];
680#endif
681
682	/*
683	 * Put all cards into the Sleep state so that we can clear
684	 * their CSNs.
685	 */
686	pnp_send_initiation_key();
687
688	/*
689	 * Clear the CSN for all cards.
690	 */
691	pnp_write(PNP_CONFIG_CONTROL, PNP_CONFIG_CONTROL_RESET_CSN);
692
693	/*
694	 * Move all cards to the Isolation state.
695	 */
696	pnp_write(PNP_WAKE, 0);
697
698	/*
699	 * Tell them where the read point is going to be this time.
700	 */
701	pnp_write(PNP_SET_RD_DATA, pnp_rd_port);
702
703	for (csn = 1; csn < PNP_MAX_CARDS; csn++) {
704		/*
705		 * Start the serial isolation protocol.
706		 */
707		outb(_PNP_ADDRESS, PNP_SERIAL_ISOLATION);
708		DELAY(1000);	/* Delay 1 msec */
709
710		if (pnp_get_serial(&id)) {
711			/*
712			 * We have read the id from a card
713			 * successfully. The card which won the
714			 * isolation protocol will be in Isolation
715			 * mode and all others will be in Sleep.
716			 * Program the CSN of the isolated card
717			 * (taking it to Config state) and read its
718			 * resources, creating devices as we find
719			 * logical devices on the card.
720			 */
721			pnp_write(PNP_SET_CSN, csn);
722#ifdef PC98
723			if (bootverbose)
724				printf("PnP Vendor ID = %x\n", id.vendor_id);
725			/* Check for NEC PnP (9 bytes serial). */
726			for (n = necpnp = 0; necids[n].vendor_id; n++) {
727				if (id.vendor_id == necids[n].vendor_id) {
728					necpnp = 1;
729					break;
730				}
731			}
732			if (necpnp) {
733				if (bootverbose)
734					printf("It seems to NEC-PnP card (%s).\n",
735					       pnp_eisaformat(id.vendor_id));
736				/*  Read dummy 9 bytes serial area. */
737				pnp_get_resource_info(buffer, 9);
738			} else {
739				if (bootverbose)
740					printf("It seems to Normal-ISA-PnP card (%s).\n",
741					       pnp_eisaformat(id.vendor_id));
742			}
743			if (bootverbose)
744				printf("Reading PnP configuration for %s.\n",
745				       pnp_eisaformat(id.vendor_id));
746#endif
747			error = pnp_read_resources(&resources,
748						   &space,
749						   &len);
750			if (error)
751				break;
752			pnp_create_devices(parent, &id, csn,
753					   resources, len);
754			found++;
755		} else
756			break;
757
758		/*
759		 * Put this card back to the Sleep state and
760		 * simultaneously move all cards which don't have a
761		 * CSN yet to Isolation state.
762		 */
763		pnp_write(PNP_WAKE, 0);
764	}
765
766	/*
767	 * Unless we have chosen the wrong read port, all cards will
768	 * be in Sleep state. Put them back into WaitForKey for
769	 * now. Their resources will be programmed later.
770	 */
771	pnp_write(PNP_CONFIG_CONTROL, PNP_CONFIG_CONTROL_WAIT_FOR_KEY);
772
773	/*
774	 * Cleanup.
775	 */
776	if (resources)
777		free(resources, M_TEMP);
778
779	return found;
780}
781
782
783/*
784 * pnp_identify()
785 *
786 * autoconfiguration of pnp devices. This routine just runs the
787 * isolation protocol over several ports, until one is successful.
788 *
789 * may be called more than once ?
790 *
791 */
792
793static void
794pnp_identify(driver_t *driver, device_t parent)
795{
796	int num_pnp_devs;
797
798	/* Try various READ_DATA ports from 0x203-0x3ff */
799	for (pnp_rd_port = 0x80; (pnp_rd_port < 0xff); pnp_rd_port += 0x10) {
800		if (bootverbose)
801			printf("Trying Read_Port at %x\n", (pnp_rd_port << 2) | 0x3);
802
803		num_pnp_devs = pnp_isolation_protocol(parent);
804		if (num_pnp_devs)
805			break;
806	}
807}
808
809static device_method_t pnp_methods[] = {
810	/* Device interface */
811	DEVMETHOD(device_identify,	pnp_identify),
812
813	{ 0, 0 }
814};
815
816static driver_t pnp_driver = {
817	"pnp",
818	pnp_methods,
819	1,			/* no softc */
820};
821
822static devclass_t pnp_devclass;
823
824DRIVER_MODULE(pnp, isa, pnp_driver, pnp_devclass, 0, 0);
825