xref: /freebsd/sys/isa/isahint.c (revision 783d3ff6d7fae619db8a7990b8a6387de0c677b5)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
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 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/kernel.h>
32 #include <sys/bus.h>
33 #include <sys/module.h>
34 #include <isa/isavar.h>
35 #include <isa/isa_common.h>
36 #include <machine/resource.h>
37 
38 void
39 isa_hinted_child(device_t parent, const char *name, int unit)
40 {
41 	device_t	child;
42 	int		sensitive, start, count;
43 	int		order;
44 
45 	/* device-specific flag overrides any wildcard */
46 	sensitive = 0;
47 	if (resource_int_value(name, unit, "sensitive", &sensitive) != 0)
48 		resource_int_value(name, -1, "sensitive", &sensitive);
49 
50 	if (sensitive)
51 		order = ISA_ORDER_SENSITIVE;
52 	else
53 		order = ISA_ORDER_SPECULATIVE;
54 
55 	child = BUS_ADD_CHILD(parent, order, name, unit);
56 	if (child == 0)
57 		return;
58 
59 	start = 0;
60 	count = 0;
61 	resource_int_value(name, unit, "port", &start);
62 	resource_int_value(name, unit, "portsize", &count);
63 	if (start > 0 || count > 0)
64 		bus_set_resource(child, SYS_RES_IOPORT, 0, start, count);
65 
66 	start = 0;
67 	count = 0;
68 	resource_int_value(name, unit, "maddr", &start);
69 	resource_int_value(name, unit, "msize", &count);
70 	if (start > 0 || count > 0)
71 		bus_set_resource(child, SYS_RES_MEMORY, 0, start, count);
72 
73 	if (resource_int_value(name, unit, "irq", &start) == 0 && start > 0)
74 		bus_set_resource(child, SYS_RES_IRQ, 0, start, 1);
75 
76 	if (resource_int_value(name, unit, "drq", &start) == 0 && start >= 0)
77 		bus_set_resource(child, SYS_RES_DRQ, 0, start, 1);
78 
79 	if (resource_disabled(name, unit))
80 		device_disable(child);
81 
82 	isa_set_configattr(child, (isa_get_configattr(child)|ISACFGATTR_HINTS));
83 }
84 
85 static int
86 isa_match_resource_hint(device_t dev, int type, long value)
87 {
88 	struct isa_device* idev = DEVTOISA(dev);
89 	struct resource_list *rl = &idev->id_resources;
90 	struct resource_list_entry *rle;
91 
92 	STAILQ_FOREACH(rle, rl, link) {
93 		if (rle->type != type)
94 			continue;
95 		if (rle->start <= value && rle->end >= value)
96 			return (1);
97 	}
98 	return (0);
99 }
100 
101 void
102 isa_hint_device_unit(device_t bus, device_t child, const char *name, int *unitp)
103 {
104 	const char *s;
105 	long value;
106 	int line, matches, unit;
107 
108 	line = 0;
109 	for (;;) {
110 		if (resource_find_dev(&line, name, &unit, "at", NULL) != 0)
111 			break;
112 
113 		/* Must have an "at" for isa. */
114 		resource_string_value(name, unit, "at", &s);
115 		if (!(strcmp(s, device_get_nameunit(bus)) == 0 ||
116 		    strcmp(s, device_get_name(bus)) == 0))
117 			continue;
118 
119 		/*
120 		 * Check for matching resources.  We must have at
121 		 * least one match.  Since I/O and memory resources
122 		 * cannot be shared, if we get a match on either of
123 		 * those, ignore any mismatches in IRQs or DRQs.
124 		 *
125 		 * XXX: We may want to revisit this to be more lenient
126 		 * and wire as long as it gets one match.
127 		 */
128 		matches = 0;
129 		if (resource_long_value(name, unit, "port", &value) == 0) {
130 			/*
131 			 * Floppy drive controllers are notorious for
132 			 * having a wide variety of resources not all
133 			 * of which include the first port that is
134 			 * specified by the hint (typically 0x3f0)
135 			 * (see the comment above
136 			 * fdc_isa_alloc_resources() in fdc_isa.c).
137 			 * However, they do all seem to include port +
138 			 * 2 (e.g. 0x3f2) so for a floppy device, look
139 			 * for 'value + 2' in the port resources
140 			 * instead of the hint value.
141 			 */
142 			if (strcmp(name, "fdc") == 0)
143 				value += 2;
144 			if (isa_match_resource_hint(child, SYS_RES_IOPORT,
145 			    value))
146 				matches++;
147 			else
148 				continue;
149 		}
150 		if (resource_long_value(name, unit, "maddr", &value) == 0) {
151 			if (isa_match_resource_hint(child, SYS_RES_MEMORY,
152 			    value))
153 				matches++;
154 			else
155 				continue;
156 		}
157 		if (matches > 0)
158 			goto matched;
159 		if (resource_long_value(name, unit, "irq", &value) == 0) {
160 			if (isa_match_resource_hint(child, SYS_RES_IRQ, value))
161 				matches++;
162 			else
163 				continue;
164 		}
165 		if (resource_long_value(name, unit, "drq", &value) == 0) {
166 			if (isa_match_resource_hint(child, SYS_RES_DRQ, value))
167 				matches++;
168 			else
169 				continue;
170 		}
171 
172 	matched:
173 		if (matches > 0) {
174 			/* We have a winner! */
175 			*unitp = unit;
176 			break;
177 		}
178 	}
179 }
180