2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 1998 Doug Rabson
5 * Copyright (c) 2001 Thomas Moestl <tmm@FreeBSD.org>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * from: FreeBSD: src/sys/alpha/isa/isa.c,v 1.26 2001/07/11
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include <sys/param.h>
36 #include <sys/systm.h>
39 #include <machine/bus.h>
43 #include <isa/isareg.h>
44 #include <isa/isavar.h>
45 #include <isa/isa_common.h>
47 #include <dev/ofw/ofw_bus.h>
48 #include <dev/ofw/openfirm.h>
50 #include <machine/resource.h>
52 #include <dev/pci/pcireg.h>
53 #include <dev/pci/pcivar.h>
55 #include <sparc64/pci/ofw_pci.h>
56 #include <sparc64/isa/ofw_isa.h>
58 /* There can be only one ISA bus, so it is safe to use globals. */
59 static u_int64_t isa_io_base;
60 static u_int64_t isa_io_limit;
61 static u_int64_t isa_mem_base;
62 static u_int64_t isa_mem_limit;
64 device_t isa_bus_device;
66 static phandle_t isab_node;
67 static struct isa_ranges *isab_ranges;
68 static int isab_nrange;
69 static struct ofw_bus_iinfo isa_iinfo;
72 * XXX: This is really partly PCI-specific, but unfortunately is
73 * differently enough to have to duplicate it here...
75 #define ISAB_RANGE_PHYS(r) \
76 (((u_int64_t)(r)->phys_mid << 32) | (u_int64_t)(r)->phys_lo)
77 #define ISAB_RANGE_SPACE(r) (((r)->phys_hi >> 24) & 0x03)
79 #define ISAR_SPACE_IO 0x01
80 #define ISAR_SPACE_MEM 0x02
82 #define INRANGE(x, start, end) ((x) >= (start) && (x) <= (end))
84 static void isa_setup_children(device_t, phandle_t);
87 isa_init(device_t dev)
92 /* The parent of the bus must be a PCI-ISA bridge. */
93 bridge = device_get_parent(dev);
94 isab_node = ofw_bus_get_node(bridge);
95 isab_nrange = OF_getprop_alloc(isab_node, "ranges",
96 sizeof(*isab_ranges), (void **)&isab_ranges);
98 panic("isa_init: cannot get bridge range property");
100 ofw_bus_setup_iinfo(isab_node, &isa_iinfo, sizeof(ofw_isa_intr_t));
102 isa_setup_children(dev, isab_node);
104 for (i = isab_nrange - 1; i >= 0; i--) {
105 switch(ISAB_RANGE_SPACE(&isab_ranges[i])) {
107 /* This is probably always 0. */
108 isa_io_base = ISAB_RANGE_PHYS(&isab_ranges[i]);
109 isa_io_limit = isab_ranges[i].size;
112 /* This is probably always 0. */
113 isa_mem_base = ISAB_RANGE_PHYS(&isab_ranges[i]);
114 isa_mem_limit = isab_ranges[i].size;
120 static const struct {
121 const char *const name;
123 } ofw_isa_pnp_map[] = {
124 { "SUNW,lomh", 0x0000ae4e }, /* SUN0000 */
125 { "dma", 0x0002d041 }, /* PNP0200 */
126 { "floppy", 0x0007d041 }, /* PNP0700 */
127 { "rtc", 0x000bd041 }, /* PNP0B00 */
128 { "flashprom", 0x0100ae4e }, /* SUN0001 */
129 { "parallel", 0x0104d041 }, /* PNP0401 */
130 { "serial", 0x0105d041 }, /* PNP0501 */
131 { "su", 0x0105d041 }, /* PNP0501 */
132 { "i2c", 0x0200ae4e }, /* SUN0002 */
133 { "rmc-comm", 0x0300ae4e }, /* SUN0003 */
134 { "kb_ps2", 0x0303d041 }, /* PNP0303 */
135 { "kdmouse", 0x030fd041 }, /* PNP0F03 */
136 { "bscbus", 0x0400ae4e }, /* SUN0004 */
137 { "power", 0x0c0cd041 }, /* PNP0C0C */
142 isa_setup_children(device_t dev, phandle_t parent)
144 struct isa_regs *regs;
145 struct resource_list *rl;
147 u_int64_t end, start;
148 ofw_isa_intr_t *intrs, rintr;
150 uint32_t *drqs, *regidx;
151 int i, ndrq, nintr, nreg, nregidx, rid, rtype;
155 * Loop through children and fake up PnP devices for them.
156 * Their resources are added as fully mapped and specified because
157 * adjusting the resources and the resource list entries respectively
158 * in isa_alloc_resource() causes trouble with drivers which use
159 * rman_get_start(), pass-through or allocate and release resources
160 * multiple times, etc. Adjusting the resources might be better off
161 * in a bus_activate_resource method but the common ISA code doesn't
162 * allow for an isa_activate_resource().
164 for (node = OF_child(parent); node != 0; node = OF_peer(node)) {
165 if ((OF_getprop_alloc(node, "name", 1, (void **)&name)) == -1)
169 * Keyboard and mouse controllers hang off of the `8042'
170 * node but we have no real use for the `8042' itself.
172 if (strcmp(name, "8042") == 0) {
173 isa_setup_children(dev, node);
178 for (i = 0; ofw_isa_pnp_map[i].name != NULL; i++)
179 if (strcmp(ofw_isa_pnp_map[i].name, name) == 0)
181 if (ofw_isa_pnp_map[i].name == NULL) {
182 device_printf(dev, "no PnP map entry for node "
183 "0x%lx: %s\n", (unsigned long)node, name);
188 if ((cdev = BUS_ADD_CHILD(dev, ISA_ORDER_PNPBIOS, NULL, -1)) ==
190 panic("isa_setup_children: BUS_ADD_CHILD failed");
191 isa_set_logicalid(cdev, ofw_isa_pnp_map[i].id);
192 isa_set_vendorid(cdev, ofw_isa_pnp_map[i].id);
194 rl = BUS_GET_RESOURCE_LIST(dev, cdev);
195 nreg = OF_getprop_alloc(node, "reg", sizeof(*regs),
197 for (i = 0; i < nreg; i++) {
198 start = ISA_REG_PHYS(®s[i]);
199 end = start + regs[i].size - 1;
200 rtype = ofw_isa_range_map(isab_ranges, isab_nrange,
203 while (resource_list_find(rl, rtype, rid) != NULL)
205 bus_set_resource(cdev, rtype, rid, start,
208 if (nreg == -1 && parent != isab_node) {
210 * The "reg" property still might be an index into
211 * the set of registers of the parent device like
212 * with the nodes hanging off of the `8042' node.
214 nregidx = OF_getprop_alloc(node, "reg", sizeof(*regidx),
217 panic("isa_setup_children: impossible number "
218 "of register indices");
219 if (nregidx != -1 && (nreg = OF_getprop_alloc(parent,
220 "reg", sizeof(*regs), (void **)®s)) >= nregidx) {
221 for (i = 0; i < nregidx; i++) {
222 start = ISA_REG_PHYS(®s[regidx[i]]);
223 end = start + regs[regidx[i]].size - 1;
224 rtype = ofw_isa_range_map(isab_ranges,
225 isab_nrange, &start, &end, NULL);
227 while (resource_list_find(rl, rtype,
230 bus_set_resource(cdev, rtype, rid,
231 start, end - start + 1);
235 OF_prop_free(regidx);
240 nintr = OF_getprop_alloc(node, "interrupts", sizeof(*intrs),
242 for (i = 0; i < nintr; i++) {
244 panic("isa_setup_children: intr too large");
245 rintr = ofw_isa_route_intr(device_get_parent(dev), node,
246 &isa_iinfo, intrs[i]);
247 if (rintr == PCI_INVALID_IRQ) {
248 device_printf(dev, "could not map ISA "
249 "interrupt %d for node 0x%lx: %s\n",
250 intrs[i], (unsigned long)node, name);
253 bus_set_resource(cdev, SYS_RES_IRQ, i, rintr, 1);
258 ndrq = OF_getprop_alloc(node, "dma-channel", sizeof(*drqs),
260 for (i = 0; i < ndrq; i++)
261 bus_set_resource(cdev, SYS_RES_DRQ, i, drqs[i], 1);
266 * Devices using DMA hang off of the `dma' node instead of
267 * directly from the ISA bridge node.
269 if (strcmp(name, "dma") == 0)
270 isa_setup_children(dev, node);
277 isa_alloc_resource(device_t bus, device_t child, int type, int *rid,
278 rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
281 * Consider adding a resource definition.
283 int passthrough = (device_get_parent(child) != bus);
284 int isdefault = RMAN_IS_DEFAULT_RANGE(start, end);
285 struct resource_list *rl;
286 struct resource_list_entry *rle;
289 rl = BUS_GET_RESOURCE_LIST(bus, child);
290 if (!passthrough && !isdefault) {
291 rle = resource_list_find(rl, type, *rid);
297 if (*rid >= ISA_NIRQ)
301 if (*rid >= ISA_NDRQ)
305 if (*rid >= ISA_NMEM)
309 if (*rid >= ISA_NPORT)
315 resource_list_add(rl, type, *rid, start, end, count);
320 * Sanity check if the resource in the respective entry is fully
321 * mapped and specified and its type allocable. A driver could
322 * have added an out of range resource on its own.
325 if ((rle = resource_list_find(rl, type, *rid)) == NULL)
331 limit = base + isa_mem_limit;
335 limit = base + isa_io_limit;
338 if (rle->start != rle->end || rle->start <= 7)
346 if (type == SYS_RES_MEMORY || type == SYS_RES_IOPORT) {
347 if (!INRANGE(rle->start, base, limit) ||
348 !INRANGE(rle->end, base, limit))
353 return (resource_list_alloc(rl, bus, child, type, rid, start, end,
358 isa_release_resource(device_t bus, device_t child, int type, int rid,
359 struct resource *res)
362 return (bus_generic_rl_release_resource(bus, child, type, rid, res));