2 * Copyright 2002 by Peter Grehan. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * 3. The name of the author may not be used to endorse or promote products
13 * derived from this software without specific prior written permission.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
20 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
22 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * PSIM 'iobus' local bus. Should be set up in the device tree like:
33 * /iobus@0x80000000/name psim-iobus
35 * Code borrowed from various nexus.c and uninorth.c :-)
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/kernel.h>
41 #include <sys/malloc.h>
42 #include <sys/module.h>
44 #include <machine/bus.h>
47 #include <dev/ofw/ofw_bus.h>
48 #include <dev/ofw/openfirm.h>
50 #include <machine/vmparam.h>
54 #include <machine/resource.h>
56 #include <powerpc/psim/iobusvar.h>
62 struct rman sc_mem_rman;
65 static MALLOC_DEFINE(M_IOBUS, "iobus", "iobus device information");
67 static int iobus_probe(device_t);
68 static int iobus_attach(device_t);
69 static int iobus_print_child(device_t dev, device_t child);
70 static void iobus_probe_nomatch(device_t, device_t);
71 static int iobus_read_ivar(device_t, device_t, int, uintptr_t *);
72 static int iobus_write_ivar(device_t, device_t, int, uintptr_t);
73 static struct resource *iobus_alloc_resource(device_t, device_t, int, int *,
74 rman_res_t, rman_res_t, rman_res_t,
76 static int iobus_activate_resource(device_t, device_t, int, int,
78 static int iobus_deactivate_resource(device_t, device_t, int, int,
80 static int iobus_release_resource(device_t, device_t, int, int,
84 * Bus interface definition
86 static device_method_t iobus_methods[] = {
87 /* Device interface */
88 DEVMETHOD(device_probe, iobus_probe),
89 DEVMETHOD(device_attach, iobus_attach),
90 DEVMETHOD(device_detach, bus_generic_detach),
91 DEVMETHOD(device_shutdown, bus_generic_shutdown),
92 DEVMETHOD(device_suspend, bus_generic_suspend),
93 DEVMETHOD(device_resume, bus_generic_resume),
96 DEVMETHOD(bus_print_child, iobus_print_child),
97 DEVMETHOD(bus_probe_nomatch, iobus_probe_nomatch),
98 DEVMETHOD(bus_read_ivar, iobus_read_ivar),
99 DEVMETHOD(bus_write_ivar, iobus_write_ivar),
100 DEVMETHOD(bus_setup_intr, bus_generic_setup_intr),
101 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr),
103 DEVMETHOD(bus_alloc_resource, iobus_alloc_resource),
104 DEVMETHOD(bus_release_resource, iobus_release_resource),
105 DEVMETHOD(bus_activate_resource, iobus_activate_resource),
106 DEVMETHOD(bus_deactivate_resource, iobus_deactivate_resource),
111 static driver_t iobus_driver = {
114 sizeof(struct iobus_softc)
117 devclass_t iobus_devclass;
119 DRIVER_MODULE(iobus, ofwbus, iobus_driver, iobus_devclass, 0, 0);
122 iobus_probe(device_t dev)
124 const char *type = ofw_bus_get_name(dev);
126 if (strcmp(type, "psim-iobus") != 0)
129 device_set_desc(dev, "PSIM local bus");
134 * Add interrupt/addr range to the dev's resource list if present
137 iobus_add_intr(phandle_t devnode, struct iobus_devinfo *dinfo)
141 if (OF_getprop(devnode, "interrupt", &intr, sizeof(intr)) != -1) {
142 resource_list_add(&dinfo->id_resources,
143 SYS_RES_IRQ, 0, intr, intr, 1);
145 dinfo->id_interrupt = intr;
150 iobus_add_reg(phandle_t devnode, struct iobus_devinfo *dinfo,
151 vm_offset_t iobus_off)
156 size = OF_getprop(devnode, "reg", dinfo->id_reg,sizeof(dinfo->id_reg));
159 dinfo->id_nregs = size / (sizeof(dinfo->id_reg[0]));
161 for (i = 0; i < dinfo->id_nregs; i+= 3) {
163 * Scale the absolute addresses back to iobus
164 * relative offsets. This is to better simulate
167 dinfo->id_reg[i+1] -= iobus_off;
169 resource_list_add(&dinfo->id_resources,
181 iobus_attach(device_t dev)
183 struct iobus_softc *sc;
184 struct iobus_devinfo *dinfo;
192 sc = device_get_softc(dev);
193 sc->sc_node = ofw_bus_get_node(dev);
196 * Find the base addr/size of the iobus, and initialize the
199 size = OF_getprop(sc->sc_node, "reg", reg, sizeof(reg));
200 if (size == sizeof(reg)) {
201 sc->sc_addr = reg[0];
202 sc->sc_size = reg[1];
207 sc->sc_mem_rman.rm_type = RMAN_ARRAY;
208 sc->sc_mem_rman.rm_descr = "IOBus Device Memory";
209 if (rman_init(&sc->sc_mem_rman) != 0) {
211 "failed to init mem range resources\n");
214 rman_manage_region(&sc->sc_mem_rman, 0, sc->sc_size);
217 * Iterate through the sub-devices
221 for (child = OF_child(root); child != 0; child = OF_peer(child)) {
222 OF_getprop_alloc(child, "name", 1, (void **)&name);
224 cdev = device_add_child(dev, NULL, -1);
226 dinfo = malloc(sizeof(*dinfo), M_IOBUS, M_WAITOK);
227 memset(dinfo, 0, sizeof(*dinfo));
228 resource_list_init(&dinfo->id_resources);
229 dinfo->id_node = child;
230 dinfo->id_name = name;
231 iobus_add_intr(child, dinfo);
232 iobus_add_reg(child, dinfo, sc->sc_addr);
233 device_set_ivars(cdev, dinfo);
239 return (bus_generic_attach(dev));
244 iobus_print_child(device_t dev, device_t child)
246 struct iobus_devinfo *dinfo;
247 struct resource_list *rl;
250 dinfo = device_get_ivars(child);
251 rl = &dinfo->id_resources;
253 retval += bus_print_child_header(dev, child);
255 retval += printf(" offset 0x%x", dinfo->id_reg[1]);
256 retval += resource_list_print_type(rl, "irq", SYS_RES_IRQ, "%jd");
258 retval += bus_print_child_footer(dev, child);
265 iobus_probe_nomatch(device_t dev, device_t child)
271 iobus_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
273 struct iobus_devinfo *dinfo;
275 if ((dinfo = device_get_ivars(child)) == NULL)
279 case IOBUS_IVAR_NODE:
280 *result = dinfo->id_node;
282 case IOBUS_IVAR_NAME:
283 *result = (uintptr_t)dinfo->id_name;
285 case IOBUS_IVAR_NREGS:
286 *result = dinfo->id_nregs;
288 case IOBUS_IVAR_REGS:
289 *result = (uintptr_t)dinfo->id_reg;
300 iobus_write_ivar(device_t dev, device_t child, int which, uintptr_t value)
306 static struct resource *
307 iobus_alloc_resource(device_t bus, device_t child, int type, int *rid,
308 rman_res_t start, rman_res_t end, rman_res_t count,
311 struct iobus_softc *sc;
316 sc = device_get_softc(bus);
318 needactivate = flags & RF_ACTIVE;
324 rm = &sc->sc_mem_rman;
327 return (bus_alloc_resource(bus, type, rid, start, end, count,
330 device_printf(bus, "unknown resource request from %s\n",
331 device_get_nameunit(child));
335 rv = rman_reserve_resource(rm, start, end, count, flags, child);
337 device_printf(bus, "failed to reserve resource for %s\n",
338 device_get_nameunit(child));
342 rman_set_rid(rv, *rid);
345 if (bus_activate_resource(child, type, *rid, rv) != 0) {
347 "failed to activate resource for %s\n",
348 device_get_nameunit(child));
349 rman_release_resource(rv);
359 iobus_release_resource(device_t bus, device_t child, int type, int rid,
360 struct resource *res)
362 if (rman_get_flags(res) & RF_ACTIVE) {
363 int error = bus_deactivate_resource(child, type, rid, res);
368 return (rman_release_resource(res));
373 iobus_activate_resource(device_t bus, device_t child, int type, int rid,
374 struct resource *res)
376 struct iobus_softc *sc;
379 sc = device_get_softc(bus);
381 if (type == SYS_RES_IRQ)
382 return (bus_activate_resource(bus, type, rid, res));
384 if ((type == SYS_RES_MEMORY) || (type == SYS_RES_IOPORT)) {
385 p = pmap_mapdev((vm_offset_t)rman_get_start(res) + sc->sc_addr,
386 (vm_size_t)rman_get_size(res));
389 rman_set_virtual(res, p);
390 rman_set_bustag(res, &bs_le_tag);
391 rman_set_bushandle(res, (u_long)p);
394 return (rman_activate_resource(res));
399 iobus_deactivate_resource(device_t bus, device_t child, int type, int rid,
400 struct resource *res)
403 * If this is a memory resource, unmap it.
405 if ((type == SYS_RES_MEMORY) || (type == SYS_RES_IOPORT)) {
408 psize = rman_get_size(res);
409 pmap_unmapdev((vm_offset_t)rman_get_virtual(res), psize);
412 return (rman_deactivate_resource(res));