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