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