xref: /freebsd/sys/isa/isa_common.c (revision 3c6e15bceeab4470243c60c9a4b5b9cafca9abaa)
1 /*-
2  * Copyright (c) 1999 Doug Rabson
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 /*
27  * Modifications for Intel architecture by Garrett A. Wollman.
28  * Copyright 1998 Massachusetts Institute of Technology
29  *
30  * Permission to use, copy, modify, and distribute this software and
31  * its documentation for any purpose and without fee is hereby
32  * granted, provided that both the above copyright notice and this
33  * permission notice appear in all copies, that both the above
34  * copyright notice and this permission notice appear in all
35  * supporting documentation, and that the name of M.I.T. not be used
36  * in advertising or publicity pertaining to distribution of the
37  * software without specific, written prior permission.  M.I.T. makes
38  * no representations about the suitability of this software for any
39  * purpose.  It is provided "as is" without express or implied
40  * warranty.
41  *
42  * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''.  M.I.T. DISCLAIMS
43  * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE,
44  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
45  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
46  * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
47  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
48  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
49  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
50  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
51  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
52  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
53  * SUCH DAMAGE.
54  */
55 
56 /*
57  * Parts of the ISA bus implementation common to all architectures.
58  */
59 
60 #include <sys/cdefs.h>
61 __FBSDID("$FreeBSD$");
62 
63 #include "opt_isa.h"
64 
65 #include <sys/param.h>
66 #include <sys/systm.h>
67 #include <sys/kernel.h>
68 #include <sys/bus.h>
69 #include <sys/malloc.h>
70 #include <sys/module.h>
71 #include <machine/bus.h>
72 #include <sys/rman.h>
73 
74 #include <machine/resource.h>
75 
76 #include <isa/isavar.h>
77 #include <isa/isa_common.h>
78 
79 static int	isa_print_child(device_t bus, device_t dev);
80 
81 static MALLOC_DEFINE(M_ISADEV, "isadev", "ISA device");
82 
83 static int isa_running;
84 
85 /*
86  * At 'probe' time, we add all the devices which we know about to the
87  * bus.  The generic attach routine will probe and attach them if they
88  * are alive.
89  */
90 static int
91 isa_probe(device_t dev)
92 {
93 	device_set_desc(dev, "ISA bus");
94 	isa_init(dev);		/* Allow machdep code to initialise */
95 	return (0);
96 }
97 
98 extern device_t isa_bus_device;
99 
100 static int
101 isa_attach(device_t dev)
102 {
103 	/*
104 	 * Arrange for isa_probe_children(dev) to be called later. XXX
105 	 */
106 	isa_bus_device = dev;
107 	return (0);
108 }
109 
110 /*
111  * Find a working set of memory regions for a child using the ranges
112  * in *config  and return the regions in *result. Returns non-zero if
113  * a set of ranges was found.
114  */
115 static int
116 isa_find_memory(device_t child, struct isa_config *config,
117     struct isa_config *result)
118 {
119 	int success, i;
120 	struct resource *res[ISA_NMEM];
121 
122 	/*
123 	 * First clear out any existing resource definitions.
124 	 */
125 	for (i = 0; i < ISA_NMEM; i++) {
126 		bus_delete_resource(child, SYS_RES_MEMORY, i);
127 		res[i] = NULL;
128 	}
129 
130 	success = 1;
131 	result->ic_nmem = config->ic_nmem;
132 	for (i = 0; i < config->ic_nmem; i++) {
133 		uint32_t start, end, size, align;
134 
135 		size = config->ic_mem[i].ir_size;
136 
137 		/* the PnP device may have a null resource as filler */
138 		if (size == 0) {
139 			result->ic_mem[i].ir_start = 0;
140 			result->ic_mem[i].ir_end = 0;
141 			result->ic_mem[i].ir_size = 0;
142 			result->ic_mem[i].ir_align = 0;
143 			continue;
144 		}
145 
146 		for (start = config->ic_mem[i].ir_start,
147 			     end = config->ic_mem[i].ir_end,
148 			     align = config->ic_mem[i].ir_align;
149 		     start + size - 1 <= end && start + size > start;
150 		     start += MAX(align, 1)) {
151 			bus_set_resource(child, SYS_RES_MEMORY, i,
152 					 start, size);
153 			res[i] = bus_alloc_resource(child,
154 			    SYS_RES_MEMORY, &i, 0, ~0, 1,
155 			    rman_make_alignment_flags(align) /* !RF_ACTIVE */);
156 			if (res[i]) {
157 				result->ic_mem[i].ir_start = start;
158 				result->ic_mem[i].ir_end = start + size - 1;
159 				result->ic_mem[i].ir_size = size;
160 				result->ic_mem[i].ir_align = align;
161 				break;
162 			}
163 		}
164 
165 		/*
166 		 * If we didn't find a place for memory range i, then
167 		 * give up now.
168 		 */
169 		if (!res[i]) {
170 			success = 0;
171 			break;
172 		}
173 	}
174 
175 	for (i = 0; i < ISA_NMEM; i++) {
176 		if (res[i])
177 			bus_release_resource(child, SYS_RES_MEMORY,
178 					     i, res[i]);
179 	}
180 
181 	return (success);
182 }
183 
184 /*
185  * Find a working set of port regions for a child using the ranges
186  * in *config  and return the regions in *result. Returns non-zero if
187  * a set of ranges was found.
188  */
189 static int
190 isa_find_port(device_t child, struct isa_config *config,
191     struct isa_config *result)
192 {
193 	int success, i;
194 	struct resource *res[ISA_NPORT];
195 
196 	/*
197 	 * First clear out any existing resource definitions.
198 	 */
199 	for (i = 0; i < ISA_NPORT; i++) {
200 		bus_delete_resource(child, SYS_RES_IOPORT, i);
201 		res[i] = NULL;
202 	}
203 
204 	success = 1;
205 	result->ic_nport = config->ic_nport;
206 	for (i = 0; i < config->ic_nport; i++) {
207 		uint32_t start, end, size, align;
208 
209 		size = config->ic_port[i].ir_size;
210 
211 		/* the PnP device may have a null resource as filler */
212 		if (size == 0) {
213 			result->ic_port[i].ir_start = 0;
214 			result->ic_port[i].ir_end = 0;
215 			result->ic_port[i].ir_size = 0;
216 			result->ic_port[i].ir_align = 0;
217 			continue;
218 		}
219 
220 		for (start = config->ic_port[i].ir_start,
221 			     end = config->ic_port[i].ir_end,
222 			     align = config->ic_port[i].ir_align;
223 		     start + size - 1 <= end;
224 		     start += align) {
225 			bus_set_resource(child, SYS_RES_IOPORT, i,
226 					 start, size);
227 			res[i] = bus_alloc_resource(child,
228 			    SYS_RES_IOPORT, &i, 0, ~0, 1,
229 			    rman_make_alignment_flags(align) /* !RF_ACTIVE */);
230 			if (res[i]) {
231 				result->ic_port[i].ir_start = start;
232 				result->ic_port[i].ir_end = start + size - 1;
233 				result->ic_port[i].ir_size = size;
234 				result->ic_port[i].ir_align = align;
235 				break;
236 			}
237 		}
238 
239 		/*
240 		 * If we didn't find a place for port range i, then
241 		 * give up now.
242 		 */
243 		if (!res[i]) {
244 			success = 0;
245 			break;
246 		}
247 	}
248 
249 	for (i = 0; i < ISA_NPORT; i++) {
250 		if (res[i])
251 			bus_release_resource(child, SYS_RES_IOPORT,
252 					     i, res[i]);
253 	}
254 
255 	return success;
256 }
257 
258 /*
259  * Return the index of the first bit in the mask (or -1 if mask is empty.
260  */
261 static int
262 find_first_bit(uint32_t mask)
263 {
264 	return (ffs(mask) - 1);
265 }
266 
267 /*
268  * Return the index of the next bit in the mask, or -1 if there are no more.
269  */
270 static int
271 find_next_bit(uint32_t mask, int bit)
272 {
273 	bit++;
274 	while (bit < 32 && !(mask & (1 << bit)))
275 		bit++;
276 	if (bit != 32)
277 		return (bit);
278 	return (-1);
279 }
280 
281 /*
282  * Find a working set of irqs for a child using the masks in *config
283  * and return the regions in *result. Returns non-zero if a set of
284  * irqs was found.
285  */
286 static int
287 isa_find_irq(device_t child, struct isa_config *config,
288     struct isa_config *result)
289 {
290 	int success, i;
291 	struct resource *res[ISA_NIRQ];
292 
293 	/*
294 	 * First clear out any existing resource definitions.
295 	 */
296 	for (i = 0; i < ISA_NIRQ; i++) {
297 		bus_delete_resource(child, SYS_RES_IRQ, i);
298 		res[i] = NULL;
299 	}
300 
301 	success = 1;
302 	result->ic_nirq = config->ic_nirq;
303 	for (i = 0; i < config->ic_nirq; i++) {
304 		uint32_t mask = config->ic_irqmask[i];
305 		int irq;
306 
307 		/* the PnP device may have a null resource as filler */
308 		if (mask == 0) {
309 			result->ic_irqmask[i] = 0;
310 			continue;
311 		}
312 
313 		for (irq = find_first_bit(mask);
314 		     irq != -1;
315 		     irq = find_next_bit(mask, irq)) {
316 			bus_set_resource(child, SYS_RES_IRQ, i,
317 					 irq, 1);
318 			res[i] = bus_alloc_resource_any(child,
319 							SYS_RES_IRQ, &i,
320 							0 /* !RF_ACTIVE */ );
321 			if (res[i]) {
322 				result->ic_irqmask[i] = (1 << irq);
323 				break;
324 			}
325 		}
326 
327 		/*
328 		 * If we didn't find a place for irq range i, then
329 		 * give up now.
330 		 */
331 		if (!res[i]) {
332 			success = 0;
333 			break;
334 		}
335 	}
336 
337 	for (i = 0; i < ISA_NIRQ; i++) {
338 		if (res[i])
339 			bus_release_resource(child, SYS_RES_IRQ,
340 					     i, res[i]);
341 	}
342 
343 	return (success);
344 }
345 
346 /*
347  * Find a working set of drqs for a child using the masks in *config
348  * and return the regions in *result. Returns non-zero if a set of
349  * drqs was found.
350  */
351 static int
352 isa_find_drq(device_t child, struct isa_config *config,
353     struct isa_config *result)
354 {
355 	int success, i;
356 	struct resource *res[ISA_NDRQ];
357 
358 	/*
359 	 * First clear out any existing resource definitions.
360 	 */
361 	for (i = 0; i < ISA_NDRQ; i++) {
362 		bus_delete_resource(child, SYS_RES_DRQ, i);
363 		res[i] = NULL;
364 	}
365 
366 	success = 1;
367 	result->ic_ndrq = config->ic_ndrq;
368 	for (i = 0; i < config->ic_ndrq; i++) {
369 		uint32_t mask = config->ic_drqmask[i];
370 		int drq;
371 
372 		/* the PnP device may have a null resource as filler */
373 		if (mask == 0) {
374 			result->ic_drqmask[i] = 0;
375 			continue;
376 		}
377 
378 		for (drq = find_first_bit(mask);
379 		     drq != -1;
380 		     drq = find_next_bit(mask, drq)) {
381 			bus_set_resource(child, SYS_RES_DRQ, i,
382 					 drq, 1);
383 			res[i] = bus_alloc_resource_any(child,
384 							SYS_RES_DRQ, &i,
385 							0 /* !RF_ACTIVE */);
386 			if (res[i]) {
387 				result->ic_drqmask[i] = (1 << drq);
388 				break;
389 			}
390 		}
391 
392 		/*
393 		 * If we didn't find a place for drq range i, then
394 		 * give up now.
395 		 */
396 		if (!res[i]) {
397 			success = 0;
398 			break;
399 		}
400 	}
401 
402 	for (i = 0; i < ISA_NDRQ; i++) {
403 		if (res[i])
404 			bus_release_resource(child, SYS_RES_DRQ,
405 					     i, res[i]);
406 	}
407 
408 	return (success);
409 }
410 
411 /*
412  * Attempt to find a working set of resources for a device. Return
413  * non-zero if a working configuration is found.
414  */
415 static int
416 isa_assign_resources(device_t child)
417 {
418 	struct isa_device *idev = DEVTOISA(child);
419 	struct isa_config_entry *ice;
420 	struct isa_config *cfg;
421 	const char *reason;
422 
423 	reason = "Empty ISA id_configs";
424 	cfg = malloc(sizeof(struct isa_config), M_TEMP, M_NOWAIT|M_ZERO);
425 	if (cfg == NULL)
426 		return(0);
427 	TAILQ_FOREACH(ice, &idev->id_configs, ice_link) {
428 		reason = "memory";
429 		if (!isa_find_memory(child, &ice->ice_config, cfg))
430 			continue;
431 		reason = "port";
432 		if (!isa_find_port(child, &ice->ice_config, cfg))
433 			continue;
434 		reason = "irq";
435 		if (!isa_find_irq(child, &ice->ice_config, cfg))
436 			continue;
437 		reason = "drq";
438 		if (!isa_find_drq(child, &ice->ice_config, cfg))
439 			continue;
440 
441 		/*
442 		 * A working configuration was found enable the device
443 		 * with this configuration.
444 		 */
445 		reason = "no callback";
446 		if (idev->id_config_cb) {
447 			idev->id_config_cb(idev->id_config_arg,
448 					   cfg, 1);
449 			free(cfg, M_TEMP);
450 			return (1);
451 		}
452 	}
453 
454 	/*
455 	 * Disable the device.
456 	 */
457 	bus_print_child_header(device_get_parent(child), child);
458 	printf(" can't assign resources (%s)\n", reason);
459 	if (bootverbose)
460 		isa_print_child(device_get_parent(child), child);
461 	bzero(cfg, sizeof (*cfg));
462 	if (idev->id_config_cb)
463 		idev->id_config_cb(idev->id_config_arg, cfg, 0);
464 	device_disable(child);
465 
466 	free(cfg, M_TEMP);
467 	return (0);
468 }
469 
470 void
471 isa_probe_children(device_t dev)
472 {
473 	device_t *children;
474 	struct isa_config *cfg;
475 	int nchildren, i;
476 
477 	/*
478 	 * Create all the children by calling driver's identify methods.
479 	 */
480 	bus_generic_probe(dev);
481 
482 	if (device_get_children(dev, &children, &nchildren))
483 		return;
484 
485 	/*
486 	 * First disable all pnp devices so that they don't get
487 	 * matched by legacy probes.
488 	 */
489 	if (bootverbose)
490 		printf("isa_probe_children: disabling PnP devices\n");
491 
492 	cfg = malloc(sizeof(*cfg), M_TEMP, M_NOWAIT|M_ZERO);
493 	if (cfg == NULL) {
494 		free(children, M_TEMP);
495 		return;
496 	}
497 
498 	for (i = 0; i < nchildren; i++) {
499 		device_t child = children[i];
500 		struct isa_device *idev = DEVTOISA(child);
501 
502 		bzero(cfg, sizeof(*cfg));
503 		if (idev->id_config_cb)
504 			idev->id_config_cb(idev->id_config_arg, cfg, 0);
505 	}
506 
507 	free(cfg, M_TEMP);
508 
509 	/*
510 	 * Next probe all non-pnp devices so that they claim their
511 	 * resources first.
512 	 */
513 	if (bootverbose)
514 		printf("isa_probe_children: probing non-PnP devices\n");
515 	for (i = 0; i < nchildren; i++) {
516 		device_t child = children[i];
517 		struct isa_device *idev = DEVTOISA(child);
518 
519 		if (TAILQ_FIRST(&idev->id_configs))
520 			continue;
521 
522 		device_probe_and_attach(child);
523 	}
524 
525 	/*
526 	 * Finally assign resource to pnp devices and probe them.
527 	 */
528 	if (bootverbose)
529 		printf("isa_probe_children: probing PnP devices\n");
530 	for (i = 0; i < nchildren; i++) {
531 		device_t child = children[i];
532 		struct isa_device* idev = DEVTOISA(child);
533 
534 		if (!TAILQ_FIRST(&idev->id_configs))
535 			continue;
536 
537 		if (isa_assign_resources(child)) {
538 			struct resource_list *rl = &idev->id_resources;
539 			struct resource_list_entry *rle;
540 
541 			device_probe_and_attach(child);
542 
543 			/*
544 			 * Claim any unallocated resources to keep other
545 			 * devices from using them.
546 			 */
547 			STAILQ_FOREACH(rle, rl, link) {
548 				if (!rle->res) {
549 					int rid = rle->rid;
550 					resource_list_alloc(rl, dev, child,
551 							    rle->type,
552 							    &rid,
553 							    0, ~0, 1, 0);
554 				}
555 			}
556 		}
557 	}
558 
559 	free(children, M_TEMP);
560 
561 	isa_running = 1;
562 }
563 
564 /*
565  * Add a new child with default ivars.
566  */
567 static device_t
568 isa_add_child(device_t dev, int order, const char *name, int unit)
569 {
570 	device_t child;
571 	struct	isa_device *idev;
572 
573 	child = device_add_child_ordered(dev, order, name, unit);
574 	if (child == NULL)
575 		return (child);
576 
577 	idev = malloc(sizeof(struct isa_device), M_ISADEV, M_NOWAIT | M_ZERO);
578 	if (!idev)
579 		return (0);
580 
581 	resource_list_init(&idev->id_resources);
582 	TAILQ_INIT(&idev->id_configs);
583 
584 	device_set_ivars(child, idev);
585 
586 	return (child);
587 }
588 
589 static int
590 isa_print_all_resources(device_t dev)
591 {
592 	struct	isa_device *idev = DEVTOISA(dev);
593 	struct resource_list *rl = &idev->id_resources;
594 	int retval = 0;
595 
596 	if (STAILQ_FIRST(rl) || device_get_flags(dev))
597 		retval += printf(" at");
598 
599 	retval += resource_list_print_type(rl, "port", SYS_RES_IOPORT, "%#lx");
600 	retval += resource_list_print_type(rl, "iomem", SYS_RES_MEMORY, "%#lx");
601 	retval += resource_list_print_type(rl, "irq", SYS_RES_IRQ, "%ld");
602 	retval += resource_list_print_type(rl, "drq", SYS_RES_DRQ, "%ld");
603 	if (device_get_flags(dev))
604 		retval += printf(" flags %#x", device_get_flags(dev));
605 #ifdef ISAPNP
606 	if (idev->id_vendorid)
607 		retval += printf(" pnpid %s", pnp_eisaformat(idev->id_vendorid));
608 #endif
609 
610 	return (retval);
611 }
612 
613 static int
614 isa_print_child(device_t bus, device_t dev)
615 {
616 	int retval = 0;
617 
618 	retval += bus_print_child_header(bus, dev);
619 	retval += isa_print_all_resources(dev);
620 	retval += bus_print_child_footer(bus, dev);
621 
622 	return (retval);
623 }
624 
625 static void
626 isa_probe_nomatch(device_t dev, device_t child)
627 {
628 	if (bootverbose) {
629 		bus_print_child_header(dev, child);
630 		printf(" failed to probe");
631 		isa_print_all_resources(child);
632 		bus_print_child_footer(dev, child);
633 	}
634 
635 	return;
636 }
637 
638 static int
639 isa_read_ivar(device_t bus, device_t dev, int index, uintptr_t * result)
640 {
641 	struct isa_device* idev = DEVTOISA(dev);
642 	struct resource_list *rl = &idev->id_resources;
643 	struct resource_list_entry *rle;
644 
645 	switch (index) {
646 	case ISA_IVAR_PORT_0:
647 		rle = resource_list_find(rl, SYS_RES_IOPORT, 0);
648 		if (rle)
649 			*result = rle->start;
650 		else
651 			*result = -1;
652 		break;
653 
654 	case ISA_IVAR_PORT_1:
655 		rle = resource_list_find(rl, SYS_RES_IOPORT, 1);
656 		if (rle)
657 			*result = rle->start;
658 		else
659 			*result = -1;
660 		break;
661 
662 	case ISA_IVAR_PORTSIZE_0:
663 		rle = resource_list_find(rl, SYS_RES_IOPORT, 0);
664 		if (rle)
665 			*result = rle->count;
666 		else
667 			*result = 0;
668 		break;
669 
670 	case ISA_IVAR_PORTSIZE_1:
671 		rle = resource_list_find(rl, SYS_RES_IOPORT, 1);
672 		if (rle)
673 			*result = rle->count;
674 		else
675 			*result = 0;
676 		break;
677 
678 	case ISA_IVAR_MADDR_0:
679 		rle = resource_list_find(rl, SYS_RES_MEMORY, 0);
680 		if (rle)
681 			*result = rle->start;
682 		else
683 			*result = -1;
684 		break;
685 
686 	case ISA_IVAR_MADDR_1:
687 		rle = resource_list_find(rl, SYS_RES_MEMORY, 1);
688 		if (rle)
689 			*result = rle->start;
690 		else
691 			*result = -1;
692 		break;
693 
694 	case ISA_IVAR_MEMSIZE_0:
695 		rle = resource_list_find(rl, SYS_RES_MEMORY, 0);
696 		if (rle)
697 			*result = rle->count;
698 		else
699 			*result = 0;
700 		break;
701 
702 	case ISA_IVAR_MEMSIZE_1:
703 		rle = resource_list_find(rl, SYS_RES_MEMORY, 1);
704 		if (rle)
705 			*result = rle->count;
706 		else
707 			*result = 0;
708 		break;
709 
710 	case ISA_IVAR_IRQ_0:
711 		rle = resource_list_find(rl, SYS_RES_IRQ, 0);
712 		if (rle)
713 			*result = rle->start;
714 		else
715 			*result = -1;
716 		break;
717 
718 	case ISA_IVAR_IRQ_1:
719 		rle = resource_list_find(rl, SYS_RES_IRQ, 1);
720 		if (rle)
721 			*result = rle->start;
722 		else
723 			*result = -1;
724 		break;
725 
726 	case ISA_IVAR_DRQ_0:
727 		rle = resource_list_find(rl, SYS_RES_DRQ, 0);
728 		if (rle)
729 			*result = rle->start;
730 		else
731 			*result = -1;
732 		break;
733 
734 	case ISA_IVAR_DRQ_1:
735 		rle = resource_list_find(rl, SYS_RES_DRQ, 1);
736 		if (rle)
737 			*result = rle->start;
738 		else
739 			*result = -1;
740 		break;
741 
742 	case ISA_IVAR_VENDORID:
743 		*result = idev->id_vendorid;
744 		break;
745 
746 	case ISA_IVAR_SERIAL:
747 		*result = idev->id_serial;
748 		break;
749 
750 	case ISA_IVAR_LOGICALID:
751 		*result = idev->id_logicalid;
752 		break;
753 
754 	case ISA_IVAR_COMPATID:
755 		*result = idev->id_compatid;
756 		break;
757 
758 	case ISA_IVAR_CONFIGATTR:
759 		*result = idev->id_config_attr;
760 		break;
761 
762 	case ISA_IVAR_PNP_CSN:
763 		*result = idev->id_pnp_csn;
764 		break;
765 
766 	case ISA_IVAR_PNP_LDN:
767 		*result = idev->id_pnp_ldn;
768 		break;
769 
770 	case ISA_IVAR_PNPBIOS_HANDLE:
771 		*result = idev->id_pnpbios_handle;
772 		break;
773 
774 	default:
775 		return (ENOENT);
776 	}
777 
778 	return (0);
779 }
780 
781 static int
782 isa_write_ivar(device_t bus, device_t dev, int index, uintptr_t value)
783 {
784 	struct isa_device* idev = DEVTOISA(dev);
785 
786 	switch (index) {
787 	case ISA_IVAR_PORT_0:
788 	case ISA_IVAR_PORT_1:
789 	case ISA_IVAR_PORTSIZE_0:
790 	case ISA_IVAR_PORTSIZE_1:
791 	case ISA_IVAR_MADDR_0:
792 	case ISA_IVAR_MADDR_1:
793 	case ISA_IVAR_MEMSIZE_0:
794 	case ISA_IVAR_MEMSIZE_1:
795 	case ISA_IVAR_IRQ_0:
796 	case ISA_IVAR_IRQ_1:
797 	case ISA_IVAR_DRQ_0:
798 	case ISA_IVAR_DRQ_1:
799 		return (EINVAL);
800 
801 	case ISA_IVAR_VENDORID:
802 		idev->id_vendorid = value;
803 		break;
804 
805 	case ISA_IVAR_SERIAL:
806 		idev->id_serial = value;
807 		break;
808 
809 	case ISA_IVAR_LOGICALID:
810 		idev->id_logicalid = value;
811 		break;
812 
813 	case ISA_IVAR_COMPATID:
814 		idev->id_compatid = value;
815 		break;
816 
817 	case ISA_IVAR_CONFIGATTR:
818 		idev->id_config_attr = value;
819 		break;
820 
821 	default:
822 		return (ENOENT);
823 	}
824 
825 	return (0);
826 }
827 
828 /*
829  * Free any resources which the driver missed or which we were holding for
830  * it (see isa_probe_children).
831  */
832 static void
833 isa_child_detached(device_t dev, device_t child)
834 {
835 	struct isa_device* idev = DEVTOISA(child);
836 	struct resource_list *rl = &idev->id_resources;
837 	struct resource_list_entry *rle;
838 
839 	if (TAILQ_FIRST(&idev->id_configs)) {
840 		/*
841 		 * Claim any unallocated resources to keep other
842 		 * devices from using them.
843 		 */
844 		STAILQ_FOREACH(rle, rl, link) {
845 			if (!rle->res) {
846 				int rid = rle->rid;
847 				resource_list_alloc(rl, dev, child,
848 						    rle->type,
849 						    &rid, 0, ~0, 1, 0);
850 			}
851 		}
852 	}
853 }
854 
855 static void
856 isa_driver_added(device_t dev, driver_t *driver)
857 {
858 	device_t *children;
859 	int nchildren, i;
860 
861 	/*
862 	 * Don't do anything if drivers are dynamically
863 	 * added during autoconfiguration (cf. ymf724).
864 	 * since that would end up calling identify
865 	 * twice.
866 	 */
867 	if (!isa_running)
868 		return;
869 
870 	DEVICE_IDENTIFY(driver, dev);
871 	if (device_get_children(dev, &children, &nchildren))
872 		return;
873 
874 	for (i = 0; i < nchildren; i++) {
875 		device_t child = children[i];
876 		struct isa_device *idev = DEVTOISA(child);
877 		struct resource_list *rl = &idev->id_resources;
878 		struct resource_list_entry *rle;
879 
880 		if (device_get_state(child) != DS_NOTPRESENT)
881 			continue;
882 		if (!device_is_enabled(child))
883 			continue;
884 
885 		/*
886 		 * Free resources which we were holding on behalf of
887 		 * the device.
888 		 */
889 		STAILQ_FOREACH(rle, &idev->id_resources, link) {
890 			if (rle->res)
891 				resource_list_release(rl, dev, child,
892 						      rle->type,
893 						      rle->rid,
894 						      rle->res);
895 		}
896 
897 		if (TAILQ_FIRST(&idev->id_configs))
898 			if (!isa_assign_resources(child))
899 				continue;
900 
901 		device_probe_and_attach(child);
902 
903 		if (TAILQ_FIRST(&idev->id_configs)) {
904 			/*
905 			 * Claim any unallocated resources to keep other
906 			 * devices from using them.
907 			 */
908 			STAILQ_FOREACH(rle, rl, link) {
909 				if (!rle->res) {
910 					int rid = rle->rid;
911 					resource_list_alloc(rl, dev, child,
912 							    rle->type,
913 							    &rid, 0, ~0, 1, 0);
914 				}
915 			}
916 		}
917 	}
918 
919 	free(children, M_TEMP);
920 }
921 
922 static int
923 isa_set_resource(device_t dev, device_t child, int type, int rid,
924     u_long start, u_long count)
925 {
926 	struct isa_device* idev = DEVTOISA(child);
927 	struct resource_list *rl = &idev->id_resources;
928 
929 	if (type != SYS_RES_IOPORT && type != SYS_RES_MEMORY
930 	    && type != SYS_RES_IRQ && type != SYS_RES_DRQ)
931 		return (EINVAL);
932 	if (rid < 0)
933 		return (EINVAL);
934 	if (type == SYS_RES_IOPORT && rid >= ISA_NPORT)
935 		return (EINVAL);
936 	if (type == SYS_RES_MEMORY && rid >= ISA_NMEM)
937 		return (EINVAL);
938 	if (type == SYS_RES_IRQ && rid >= ISA_NIRQ)
939 		return (EINVAL);
940 	if (type == SYS_RES_DRQ && rid >= ISA_NDRQ)
941 		return (EINVAL);
942 
943 	resource_list_add(rl, type, rid, start, start + count - 1, count);
944 
945 	return (0);
946 }
947 
948 static struct resource_list *
949 isa_get_resource_list (device_t dev, device_t child)
950 {
951 	struct isa_device* idev = DEVTOISA(child);
952 	struct resource_list *rl = &idev->id_resources;
953 
954 	if (!rl)
955 		return (NULL);
956 
957 	return (rl);
958 }
959 
960 static int
961 isa_add_config(device_t dev, device_t child, int priority,
962     struct isa_config *config)
963 {
964 	struct isa_device* idev = DEVTOISA(child);
965 	struct isa_config_entry *newice, *ice;
966 
967 	newice = malloc(sizeof *ice, M_DEVBUF, M_NOWAIT);
968 	if (!newice)
969 		return (ENOMEM);
970 
971 	newice->ice_priority = priority;
972 	newice->ice_config = *config;
973 
974 	TAILQ_FOREACH(ice, &idev->id_configs, ice_link) {
975 		if (ice->ice_priority > priority)
976 			break;
977 	}
978 	if (ice)
979 		TAILQ_INSERT_BEFORE(ice, newice, ice_link);
980 	else
981 		TAILQ_INSERT_TAIL(&idev->id_configs, newice, ice_link);
982 
983 	return (0);
984 }
985 
986 static void
987 isa_set_config_callback(device_t dev, device_t child, isa_config_cb *fn,
988     void *arg)
989 {
990 	struct isa_device* idev = DEVTOISA(child);
991 
992 	idev->id_config_cb = fn;
993 	idev->id_config_arg = arg;
994 }
995 
996 static int
997 isa_pnp_probe(device_t dev, device_t child, struct isa_pnp_id *ids)
998 {
999 	struct isa_device* idev = DEVTOISA(child);
1000 
1001 	if (!idev->id_vendorid)
1002 		return (ENOENT);
1003 
1004 	while (ids && ids->ip_id) {
1005 		/*
1006 		 * Really ought to support >1 compat id per device.
1007 		 */
1008 		if (idev->id_logicalid == ids->ip_id
1009 		    || idev->id_compatid == ids->ip_id) {
1010 			if (ids->ip_desc)
1011 				device_set_desc(child, ids->ip_desc);
1012 			return (0);
1013 		}
1014 		ids++;
1015 	}
1016 
1017 	return (ENXIO);
1018 }
1019 
1020 static int
1021 isa_child_pnpinfo_str(device_t bus, device_t child, char *buf,
1022     size_t buflen)
1023 {
1024 #ifdef ISAPNP
1025 	struct isa_device *idev = DEVTOISA(child);
1026 
1027 	if (idev->id_vendorid)
1028 		snprintf(buf, buflen, "pnpid=%s",
1029 		    pnp_eisaformat(idev->id_vendorid));
1030 #endif
1031 	return (0);
1032 }
1033 
1034 static int
1035 isa_child_location_str(device_t bus, device_t child, char *buf,
1036     size_t buflen)
1037 {
1038 #if 0
1039 	/* id_pnphandle isn't there yet */
1040 	struct isa_device *idev = DEVTOISA(child);
1041 
1042 	if (idev->id_vendorid)
1043 		snprintf(buf, buflen, "pnphandle=%d", idev->id_pnphandle);
1044 #endif
1045 	/* Nothing here yet */
1046 	*buf = '\0';
1047 	return (0);
1048 }
1049 
1050 static device_method_t isa_methods[] = {
1051 	/* Device interface */
1052 	DEVMETHOD(device_probe,		isa_probe),
1053 	DEVMETHOD(device_attach,	isa_attach),
1054 	DEVMETHOD(device_detach,	bus_generic_detach),
1055 	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
1056 	DEVMETHOD(device_suspend,	bus_generic_suspend),
1057 	DEVMETHOD(device_resume,	bus_generic_resume),
1058 
1059 	/* Bus interface */
1060 	DEVMETHOD(bus_add_child,	isa_add_child),
1061 	DEVMETHOD(bus_print_child,	isa_print_child),
1062 	DEVMETHOD(bus_probe_nomatch,	isa_probe_nomatch),
1063 	DEVMETHOD(bus_read_ivar,	isa_read_ivar),
1064 	DEVMETHOD(bus_write_ivar,	isa_write_ivar),
1065 	DEVMETHOD(bus_child_detached,	isa_child_detached),
1066 	DEVMETHOD(bus_driver_added,	isa_driver_added),
1067 	DEVMETHOD(bus_setup_intr,	isa_setup_intr),
1068 	DEVMETHOD(bus_teardown_intr,	isa_teardown_intr),
1069 
1070 	DEVMETHOD(bus_get_resource_list,isa_get_resource_list),
1071 	DEVMETHOD(bus_alloc_resource,	isa_alloc_resource),
1072 	DEVMETHOD(bus_release_resource,	isa_release_resource),
1073 	DEVMETHOD(bus_set_resource,	isa_set_resource),
1074 	DEVMETHOD(bus_get_resource,	bus_generic_rl_get_resource),
1075 	DEVMETHOD(bus_delete_resource,	bus_generic_rl_delete_resource),
1076 	DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
1077 	DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
1078 	DEVMETHOD(bus_child_pnpinfo_str, isa_child_pnpinfo_str),
1079 	DEVMETHOD(bus_child_location_str, isa_child_location_str),
1080 
1081 	/* ISA interface */
1082 	DEVMETHOD(isa_add_config,	isa_add_config),
1083 	DEVMETHOD(isa_set_config_callback, isa_set_config_callback),
1084 	DEVMETHOD(isa_pnp_probe,	isa_pnp_probe),
1085 
1086 	{ 0, 0 }
1087 };
1088 
1089 driver_t isa_driver = {
1090 	"isa",
1091 	isa_methods,
1092 	1,			/* no softc */
1093 };
1094 
1095 devclass_t isa_devclass;
1096 
1097 /*
1098  * ISA can be attached to a PCI-ISA bridge, or other locations on some
1099  * platforms.
1100  */
1101 DRIVER_MODULE(isa, isab, isa_driver, isa_devclass, 0, 0);
1102 DRIVER_MODULE(isa, eisab, isa_driver, isa_devclass, 0, 0);
1103 MODULE_VERSION(isa, 1);
1104 
1105 /*
1106  * Code common to ISA bridges.
1107  */
1108 
1109 devclass_t isab_devclass;
1110 
1111 int
1112 isab_attach(device_t dev)
1113 {
1114 	device_t child;
1115 
1116 	child = device_add_child(dev, "isa", 0);
1117 	if (child != NULL)
1118 		return (bus_generic_attach(dev));
1119 	return (ENXIO);
1120 }
1121