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