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