2 * Copyright (c) 2006 Benno Rice. 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.
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 * from: FreeBSD: //depot/projects/arm/src/sys/arm/xscale/pxa2x0/pxa2x0_obio.c, rev 1
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
31 #include <sys/systm.h>
33 #include <sys/kernel.h>
34 #include <sys/module.h>
35 #include <sys/malloc.h>
37 #include <machine/bus.h>
38 #include <machine/intr.h>
40 #include <arm/mv/mvvar.h>
41 #include <arm/mv/mvreg.h>
43 static void mbus_identify(driver_t *, device_t);
44 static int mbus_probe(device_t);
45 static int mbus_attach(device_t);
48 mbus_identify(driver_t *driver, device_t parent)
51 BUS_ADD_CHILD(parent, 0, "mbus", 0);
55 mbus_probe(device_t dev)
58 device_set_desc(dev, "Marvell Internal Bus (Mbus)");
63 mbus_attach(device_t dev)
65 struct obio_softc *sc;
66 struct obio_device *od;
70 sc = device_get_softc(dev);
72 sc->obio_bst = obio_tag;
74 sc->obio_mem.rm_type = RMAN_ARRAY;
75 sc->obio_mem.rm_descr = "Marvell OBIO Memory";
76 if (rman_init(&sc->obio_mem) != 0)
77 panic("mbus_attach: failed to init obio mem rman");
78 if (rman_manage_region(&sc->obio_mem, 0, ~0) != 0)
79 panic("mbus_attach: failed to set up obio mem rman");
81 sc->obio_irq.rm_type = RMAN_ARRAY;
82 sc->obio_irq.rm_descr = "Marvell OBIO IRQ";
83 if (rman_init(&sc->obio_irq) != 0)
84 panic("mbus_attach: failed to init obio irq rman");
85 if (rman_manage_region(&sc->obio_irq, 0, NIRQ - 1) != 0)
86 panic("mbus_attach: failed to set up obio irq rman");
88 sc->obio_gpio.rm_type = RMAN_ARRAY;
89 sc->obio_gpio.rm_descr = "Marvell GPIO";
90 if (rman_init(&sc->obio_gpio) != 0)
91 panic("mbus_attach: failed to init gpio rman");
92 if (rman_manage_region(&sc->obio_gpio, 0, MV_GPIO_MAX_NPINS - 1) != 0)
93 panic("mbus_attach: failed to set up gpio rman");
95 for (od = obio_devices; od->od_name != NULL; od++) {
96 if (soc_power_ctrl_get(od->od_pwr_mask) != od->od_pwr_mask)
99 resource_list_init(&od->od_resources);
101 resource_list_add(&od->od_resources, SYS_RES_MEMORY, 0,
102 od->od_base, od->od_base + od->od_size - 1, od->od_size);
104 for (i = 0; od->od_irqs[i] != -1; i++) {
105 resource_list_add(&od->od_resources, SYS_RES_IRQ, i,
106 od->od_irqs[i], od->od_irqs[i], 1);
109 for (i = 0; od->od_gpio[i] != -1; i++) {
110 resource_list_add(&od->od_resources, SYS_RES_GPIO, i,
111 od->od_gpio[i], od->od_gpio[i], 1);
114 child = device_add_child(dev, od->od_name, -1);
115 device_set_ivars(child, od);
118 bus_generic_probe(dev);
119 bus_generic_attach(dev);
125 mbus_print_child(device_t dev, device_t child)
127 struct obio_device *od;
130 od = (struct obio_device *)device_get_ivars(child);
132 panic("Unknown device on %s", device_get_nameunit(dev));
134 rv = bus_print_child_header(dev, child);
136 rv += resource_list_print_type(&od->od_resources, "at mem",
137 SYS_RES_MEMORY, "0x%08lx");
138 rv += resource_list_print_type(&od->od_resources, "irq",
140 rv += resource_list_print_type(&od->od_resources, "gpio",
141 SYS_RES_GPIO, "%ld");
143 if (device_get_flags(child))
144 rv += printf(" flags %#x", device_get_flags(child));
145 rv += bus_print_child_footer(dev, child);
151 mbus_setup_intr(device_t dev, device_t child, struct resource *ires, int flags,
152 driver_filter_t *filt, driver_intr_t *intr, void *arg, void **cookiep)
154 struct obio_softc *sc;
157 sc = (struct obio_softc *)device_get_softc(dev);
159 if (rman_is_region_manager(ires, &sc->obio_gpio)) {
160 error = mv_gpio_setup_intrhandler(
161 device_get_nameunit(child), filt, intr, arg,
162 rman_get_start(ires), flags, cookiep);
167 mv_gpio_intr_unmask(rman_get_start(ires));
171 BUS_SETUP_INTR(device_get_parent(dev), child, ires, flags, filt, intr, arg,
173 arm_unmask_irq(rman_get_start(ires));
178 mbus_teardown_intr(device_t dev, device_t child, struct resource *ires, void *cookie)
181 return (BUS_TEARDOWN_INTR(device_get_parent(dev), child, ires, cookie));
185 mbus_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
187 struct obio_device *od;
189 od = (struct obio_device *)device_get_ivars(child);
193 *((u_long *)result) = od->od_base;
203 static struct resource_list *
204 mbus_get_resource_list(device_t dev, device_t child)
206 struct obio_device *od;
208 od = (struct obio_device *)device_get_ivars(child);
213 return (&od->od_resources);
216 static struct resource *
217 mbus_alloc_resource(device_t dev, device_t child, int type, int *rid,
218 u_long start, u_long end, u_long count, u_int flags)
220 struct obio_softc *sc;
221 struct obio_device *od;
223 struct resource_list *rl;
224 struct resource_list_entry *rle = NULL;
228 sc = (struct obio_softc *)device_get_softc(dev);
230 if (type == SYS_RES_IRQ && IS_GPIO_IRQ(start)) {
232 start = IRQ2GPIO(start);
249 if (device_get_parent(child) == dev) {
250 od = (struct obio_device *)device_get_ivars(child);
251 rl = &od->od_resources;
252 rle = resource_list_find(rl, type, *rid);
254 if (rle->res != NULL)
255 panic("mbus_alloc_resource: resource is busy");
264 needactivate = flags & RF_ACTIVE;
266 rv = rman_reserve_resource(rm, start, end, count, flags, child);
274 rman_set_rid(rv, *rid);
276 if (type == SYS_RES_MEMORY) {
277 rman_set_bustag(rv, sc->obio_bst);
278 rman_set_bushandle(rv, start);
282 if (bus_activate_resource(child, type, *rid, rv)) {
283 rman_release_resource(rv);
291 mbus_release_resource(device_t dev, device_t child, int type, int rid,
294 struct obio_device *od;
295 struct resource_list *rl;
296 struct resource_list_entry *rle;
298 if (device_get_parent(child) == dev) {
299 od = (struct obio_device *)device_get_ivars(child);
300 rl = &od->od_resources;
301 rle = resource_list_find(rl, type, rid);
304 panic("mbus_release_resource: can't find resource");
307 panic("mbus_release_resource: resource entry is not busy");
313 rman_release_resource(r);
319 mbus_activate_resource(device_t dev, device_t child, int type, int rid,
323 return (rman_activate_resource(r));
327 mbus_add_child(device_t bus, int order, const char *name, int unit)
329 struct obio_device *od;
332 od = malloc(sizeof(struct obio_device), M_DEVBUF, M_NOWAIT | M_ZERO);
336 resource_list_init(&od->od_resources);
339 child = device_add_child_ordered(bus, order, name, unit);
340 device_set_ivars(child, od);
345 static device_method_t mbus_methods[] = {
346 DEVMETHOD(device_identify, mbus_identify),
347 DEVMETHOD(device_probe, mbus_probe),
348 DEVMETHOD(device_attach, mbus_attach),
350 DEVMETHOD(bus_add_child, mbus_add_child),
351 DEVMETHOD(bus_print_child, mbus_print_child),
353 DEVMETHOD(bus_read_ivar, mbus_read_ivar),
354 DEVMETHOD(bus_setup_intr, mbus_setup_intr),
355 DEVMETHOD(bus_teardown_intr, mbus_teardown_intr),
357 DEVMETHOD(bus_set_resource, bus_generic_rl_set_resource),
358 DEVMETHOD(bus_get_resource_list, mbus_get_resource_list),
359 DEVMETHOD(bus_alloc_resource, mbus_alloc_resource),
360 DEVMETHOD(bus_release_resource, mbus_release_resource),
361 DEVMETHOD(bus_activate_resource, mbus_activate_resource),
366 static driver_t mbus_driver = {
369 sizeof(struct obio_softc),
372 static devclass_t mbus_devclass;
374 DRIVER_MODULE(mbus, nexus, mbus_driver, mbus_devclass, 0, 0);