2 * Copyright (c) 2009-2010 The FreeBSD Foundation
5 * This software was developed by Semihalf under sponsorship from
6 * the FreeBSD Foundation.
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
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
34 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/module.h>
40 #include <sys/malloc.h>
42 #include <machine/fdt.h>
44 #include <dev/ofw/openfirm.h>
46 #include "fdt_common.h"
47 #include "ofw_bus_if.h"
53 #define debugf(fmt, args...) do { printf("%s(): ", __func__); \
54 printf(fmt,##args); } while (0)
56 #define debugf(fmt, args...)
59 static MALLOC_DEFINE(M_FDTBUS, "fdtbus", "FDTbus devices information");
61 struct fdtbus_devinfo {
66 struct resource_list di_res;
68 /* Interrupts sense-level info for this device */
69 struct fdt_sense_level di_intr_sl[DI_MAX_INTR_NUM];
80 static void fdtbus_identify(driver_t *, device_t);
81 static int fdtbus_probe(device_t);
82 static int fdtbus_attach(device_t);
84 static int fdtbus_print_child(device_t, device_t);
85 static struct resource *fdtbus_alloc_resource(device_t, device_t, int,
86 int *, u_long, u_long, u_long, u_int);
87 static int fdtbus_release_resource(device_t, device_t, int, int,
89 static int fdtbus_activate_resource(device_t, device_t, int, int,
91 static int fdtbus_deactivate_resource(device_t, device_t, int, int,
93 static int fdtbus_setup_intr(device_t, device_t, struct resource *, int,
94 driver_filter_t *, driver_intr_t *, void *, void **);
95 static int fdtbus_teardown_intr(device_t, device_t, struct resource *,
98 static const char *fdtbus_ofw_get_name(device_t, device_t);
99 static phandle_t fdtbus_ofw_get_node(device_t, device_t);
100 static const char *fdtbus_ofw_get_type(device_t, device_t);
101 static const char *fdtbus_ofw_get_compat(device_t, device_t);
106 static void newbus_device_from_fdt_node(device_t, phandle_t);
109 * Bus interface definition.
111 static device_method_t fdtbus_methods[] = {
112 /* Device interface */
113 DEVMETHOD(device_identify, fdtbus_identify),
114 DEVMETHOD(device_probe, fdtbus_probe),
115 DEVMETHOD(device_attach, fdtbus_attach),
116 DEVMETHOD(device_detach, bus_generic_detach),
117 DEVMETHOD(device_shutdown, bus_generic_shutdown),
118 DEVMETHOD(device_suspend, bus_generic_suspend),
119 DEVMETHOD(device_resume, bus_generic_resume),
122 DEVMETHOD(bus_print_child, fdtbus_print_child),
123 DEVMETHOD(bus_alloc_resource, fdtbus_alloc_resource),
124 DEVMETHOD(bus_release_resource, fdtbus_release_resource),
125 DEVMETHOD(bus_activate_resource, fdtbus_activate_resource),
126 DEVMETHOD(bus_deactivate_resource, fdtbus_deactivate_resource),
127 DEVMETHOD(bus_setup_intr, fdtbus_setup_intr),
128 DEVMETHOD(bus_teardown_intr, fdtbus_teardown_intr),
130 /* OFW bus interface */
131 DEVMETHOD(ofw_bus_get_node, fdtbus_ofw_get_node),
132 DEVMETHOD(ofw_bus_get_name, fdtbus_ofw_get_name),
133 DEVMETHOD(ofw_bus_get_type, fdtbus_ofw_get_type),
134 DEVMETHOD(ofw_bus_get_compat, fdtbus_ofw_get_compat),
139 static driver_t fdtbus_driver = {
142 sizeof(struct fdtbus_softc)
145 devclass_t fdtbus_devclass;
147 DRIVER_MODULE(fdtbus, nexus, fdtbus_driver, fdtbus_devclass, 0, 0);
150 fdtbus_identify(driver_t *driver, device_t parent)
153 debugf("%s(driver=%p, parent=%p)\n", __func__, driver, parent);
155 if (device_find_child(parent, "fdtbus", -1) == NULL)
156 BUS_ADD_CHILD(parent, 0, "fdtbus", -1);
160 fdtbus_probe(device_t dev)
163 debugf("%s(dev=%p); pass=%u\n", __func__, dev, bus_current_pass);
165 device_set_desc(dev, "FDT main bus");
168 return (BUS_PROBE_DEFAULT);
172 fdtbus_attach(device_t dev)
176 struct fdtbus_softc *sc;
180 if ((root = OF_peer(0)) == 0)
181 panic("fdtbus_attach: no root node.");
183 sc = device_get_softc(dev);
189 end = FDT_INTR_MAX - 1;
190 sc->sc_irq.rm_start = start;
191 sc->sc_irq.rm_end = end;
192 sc->sc_irq.rm_type = RMAN_ARRAY;
193 sc->sc_irq.rm_descr = "Interrupt request lines";
194 if ((error = rman_init(&sc->sc_irq)) != 0) {
195 device_printf(dev, "could not init IRQ rman, error = %d\n",
199 if ((error = rman_manage_region(&sc->sc_irq, start, end)) != 0) {
200 device_printf(dev, "could not manage IRQ region, error = %d\n",
206 * Mem-mapped I/O space rman.
210 sc->sc_mem.rm_start = start;
211 sc->sc_mem.rm_end = end;
212 sc->sc_mem.rm_type = RMAN_ARRAY;
213 sc->sc_mem.rm_descr = "I/O memory";
214 if ((error = rman_init(&sc->sc_mem)) != 0) {
215 device_printf(dev, "could not init I/O mem rman, error = %d\n",
219 if ((error = rman_manage_region(&sc->sc_mem, start, end)) != 0) {
220 device_printf(dev, "could not manage I/O mem region, "
221 "error = %d\n", error);
226 * Walk the FDT root node and add top-level devices as our children.
228 for (child = OF_child(root); child != 0; child = OF_peer(child)) {
229 /* Check and process 'status' property. */
230 if (!(fdt_is_enabled(child)))
233 newbus_device_from_fdt_node(dev, child);
236 return (bus_generic_attach(dev));
240 fdtbus_print_child(device_t dev, device_t child)
242 struct fdtbus_devinfo *di;
243 struct resource_list *rl;
246 di = device_get_ivars(child);
250 rv += bus_print_child_header(dev, child);
251 rv += resource_list_print_type(rl, "mem", SYS_RES_MEMORY, "%#lx");
252 rv += resource_list_print_type(rl, "irq", SYS_RES_IRQ, "%ld");
253 rv += bus_print_child_footer(dev, child);
259 newbus_device_destroy(device_t dev)
261 struct fdtbus_devinfo *di;
263 di = device_get_ivars(dev);
265 free(di->di_name, M_OFWPROP);
266 free(di->di_type, M_OFWPROP);
267 free(di->di_compat, M_OFWPROP);
269 resource_list_free(&di->di_res);
274 newbus_device_create(device_t dev_par, phandle_t node, char *name, char *type,
278 struct fdtbus_devinfo *di;
280 child = device_add_child(dev_par, NULL, -1);
282 free(name, M_OFWPROP);
283 free(type, M_OFWPROP);
284 free(compat, M_OFWPROP);
288 di = malloc(sizeof(*di), M_FDTBUS, M_WAITOK);
292 di->di_compat = compat;
294 resource_list_init(&di->di_res);
296 if (fdt_reg_to_rl(node, &di->di_res, fdt_immr_va)) {
297 device_printf(child, "could not process 'reg' property\n");
298 newbus_device_destroy(child);
303 if (fdt_intr_to_rl(node, &di->di_res, di->di_intr_sl)) {
304 device_printf(child, "could not process 'interrupts' "
306 newbus_device_destroy(child);
311 device_set_ivars(child, di);
312 debugf("added child name='%s', node=%p\n", name, (void *)node);
319 newbus_pci_create(device_t dev_par, phandle_t dt_node, u_long par_base,
324 u_long start, end, count;
325 struct fdtbus_devinfo *di;
326 char *name, *type, *compat;
329 OF_getprop_alloc(dt_node, "device_type", 1, (void **)&type);
330 if (!(type != NULL && strcmp(type, "pci") == 0)) {
331 /* Only process 'pci' subnodes. */
332 free(type, M_OFWPROP);
336 OF_getprop_alloc(dt_node, "name", 1, (void **)&name);
337 OF_getprop_alloc(OF_parent(dt_node), "compatible", 1,
340 dev_child = device_add_child(dev_par, NULL, -1);
341 if (dev_child == NULL) {
342 free(name, M_OFWPROP);
343 free(type, M_OFWPROP);
344 free(compat, M_OFWPROP);
348 di = malloc(sizeof(*di), M_FDTBUS, M_WAITOK);
349 di->di_node = dt_node;
352 di->di_compat = compat;
354 resource_list_init(&di->di_res);
357 * Produce and set SYS_RES_MEMORY resources.
362 len = OF_getprop(dt_node, "reg", ®, sizeof(reg));
364 if (fdt_data_verify((void *)®[1], 2) != 0) {
365 device_printf(dev_child, "'reg' address value out of "
367 newbus_device_destroy(dev_child);
371 start = fdt_data_get((void *)®[1], 2);
373 if (fdt_data_verify((void *)®[3], 2) != 0) {
374 device_printf(dev_child, "'reg' size value out of "
376 newbus_device_destroy(dev_child);
380 count = fdt_data_get((void *)®[3], 2);
383 /* Calculate address range relative to base. */
384 par_base &= 0x000ffffful;
385 start &= 0x000ffffful;
386 start += par_base + fdt_immr_va;
389 end = start + count - 1;
391 debugf("start = 0x%08lx, end = 0x%08lx, count = 0x%08lx\n",
394 if (count > par_size) {
395 device_printf(dev_child, "'reg' size value out of range\n");
396 newbus_device_destroy(dev_child);
401 resource_list_add(&di->di_res, SYS_RES_MEMORY, 0, start, end, count);
404 * Set SYS_RES_IRQ resources.
406 if (fdt_intr_to_rl(OF_parent(dt_node), &di->di_res, di->di_intr_sl)) {
407 device_printf(dev_child, "could not process 'interrupts' "
409 newbus_device_destroy(dev_child);
414 device_set_ivars(dev_child, di);
415 debugf("added child name='%s', node=%p\n", name,
423 pci_from_fdt_node(device_t dev_par, phandle_t dt_node, char *name,
424 char *type, char *compat)
426 u_long reg_base, reg_size;
430 * Retrieve 'reg' property.
432 if (fdt_regsize(dt_node, ®_base, ®_size) != 0) {
433 device_printf(dev_par, "could not retrieve 'reg' prop\n");
438 * Walk the PCI node and instantiate newbus devices representing
439 * logical resources (bridges / ports).
441 for (dt_child = OF_child(dt_node); dt_child != 0;
442 dt_child = OF_peer(dt_child)) {
444 if (!(fdt_is_enabled(dt_child)))
447 newbus_pci_create(dev_par, dt_child, reg_base, reg_size);
452 * These FDT nodes do not need a corresponding newbus device object.
454 static char *fdt_devices_skip[] = {
462 newbus_device_from_fdt_node(device_t dev_par, phandle_t node)
464 char *name, *type, *compat;
468 OF_getprop_alloc(node, "name", 1, (void **)&name);
469 OF_getprop_alloc(node, "device_type", 1, (void **)&type);
470 OF_getprop_alloc(node, "compatible", 1, (void **)&compat);
472 for (i = 0; fdt_devices_skip[i] != NULL; i++)
473 if (name != NULL && strcmp(name, fdt_devices_skip[i]) == 0) {
474 debugf("skipping instantiating FDT device='%s'\n",
479 child = newbus_device_create(dev_par, node, name, type, compat);
480 if (type != NULL && strcmp(type, "pci") == 0)
481 pci_from_fdt_node(child, node, name, type, compat);
484 static struct resource *
485 fdtbus_alloc_resource(device_t bus, device_t child, int type, int *rid,
486 u_long start, u_long end, u_long count, u_int flags)
488 struct fdtbus_softc *sc;
489 struct resource *res;
491 struct fdtbus_devinfo *di;
492 struct resource_list_entry *rle;
496 * Request for the default allocation with a given rid: use resource
497 * list stored in the local device info.
499 if ((start == 0UL) && (end == ~0UL)) {
500 if ((di = device_get_ivars(child)) == NULL)
503 if (type == SYS_RES_IOPORT)
504 type = SYS_RES_MEMORY;
506 rle = resource_list_find(&di->di_res, type, *rid);
508 device_printf(bus, "no default resources for "
509 "rid = %d, type = %d\n", *rid, type);
517 sc = device_get_softc(bus);
519 needactivate = flags & RF_ACTIVE;
536 res = rman_reserve_resource(rm, start, end, count, flags, child);
538 device_printf(bus, "failed to reserve resource %#lx - %#lx "
539 "(%#lx)\n", start, end, count);
543 rman_set_rid(res, *rid);
545 if (type == SYS_RES_IOPORT || type == SYS_RES_MEMORY) {
546 /* XXX endianess should be set based on SOC node */
547 rman_set_bustag(res, fdtbus_bs_tag);
548 rman_set_bushandle(res, rman_get_start(res));
552 if (bus_activate_resource(child, type, *rid, res)) {
553 device_printf(child, "resource activation failed\n");
554 rman_release_resource(res);
562 fdtbus_release_resource(device_t bus, device_t child, int type, int rid,
563 struct resource *res)
567 if (rman_get_flags(res) & RF_ACTIVE) {
568 err = bus_deactivate_resource(child, type, rid, res);
573 return (rman_release_resource(res));
577 fdtbus_setup_intr(device_t bus, device_t child, struct resource *res,
578 int flags, driver_filter_t *filter, driver_intr_t *ihand, void *arg,
584 if ((rman_get_flags(res) & RF_SHAREABLE) == 0)
587 err = rman_activate_resource(res);
591 #if defined(__powerpc__)
592 err = powerpc_setup_intr(device_get_nameunit(child),
593 rman_get_start(res), filter, ihand, arg, flags, cookiep);
594 #elif defined(__arm__)
595 arm_setup_irqhandler(device_get_nameunit(child),
596 filter, ihand, arg, rman_get_start(res), flags, cookiep);
597 arm_unmask_irq(rman_get_start(res));
605 fdtbus_activate_resource(device_t bus, device_t child, int type, int rid,
606 struct resource *res)
609 return (rman_activate_resource(res));
613 fdtbus_deactivate_resource(device_t bus, device_t child, int type, int rid,
614 struct resource *res)
617 return (rman_deactivate_resource(res));
622 fdtbus_teardown_intr(device_t bus, device_t child, struct resource *res,
626 #if defined(__powerpc__)
627 return (powerpc_teardown_intr(cookie));
628 #elif defined(__arm__)
629 return (arm_remove_irqhandler(rman_get_start(res), cookie));
634 fdtbus_ofw_get_name(device_t bus, device_t dev)
636 struct fdtbus_devinfo *di;
638 return ((di = device_get_ivars(dev)) == NULL ? NULL : di->di_name);
642 fdtbus_ofw_get_node(device_t bus, device_t dev)
644 struct fdtbus_devinfo *di;
646 return ((di = device_get_ivars(dev)) == NULL ? 0 : di->di_node);
650 fdtbus_ofw_get_type(device_t bus, device_t dev)
652 struct fdtbus_devinfo *di;
654 return ((di = device_get_ivars(dev)) == NULL ? NULL : di->di_type);
658 fdtbus_ofw_get_compat(device_t bus, device_t dev)
660 struct fdtbus_devinfo *di;
662 return ((di = device_get_ivars(dev)) == NULL ? NULL : di->di_compat);