2 * Copyright (c) 2012 Semihalf.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include "opt_platform.h"
31 #include <sys/param.h>
32 #include <sys/systm.h>
34 #include <sys/kernel.h>
35 #include <sys/module.h>
38 #include <sys/malloc.h>
39 #include <sys/devmap.h>
43 #include <machine/fdt.h>
45 #include <dev/ofw/ofw_bus.h>
46 #include <dev/ofw/ofw_bus_subr.h>
47 #include <dev/ofw/openfirm.h>
49 #include "dev/fdt/fdt_common.h"
50 #include "ofw_bus_if.h"
52 #include <arm/mv/mvvar.h>
53 #include <arm/mv/mvwin.h>
56 #define debugf(fmt, args...) do { printf("%s(): ", __func__); \
57 printf(fmt,##args); } while (0)
59 #define debugf(fmt, args...)
62 #define MV_LOCALBUS_MAX_BANKS 8
63 #define MV_LOCALBUS_MAX_BANK_CELLS 4
65 static MALLOC_DEFINE(M_LOCALBUS, "localbus", "localbus devices information");
67 struct localbus_bank {
68 vm_offset_t va; /* VA of the bank */
69 vm_paddr_t pa; /* physical address of the bank */
70 vm_size_t size; /* bank size */
71 uint8_t mapped; /* device memory has mapping */
74 struct localbus_softc {
76 bus_space_handle_t sc_bsh;
77 bus_space_tag_t sc_bst;
80 struct localbus_bank *sc_banks;
83 struct localbus_devinfo {
84 struct ofw_bus_devinfo di_ofw;
85 struct resource_list di_res;
89 struct localbus_va_entry {
98 static int localbus_probe(device_t);
99 static int localbus_attach(device_t);
100 static int localbus_print_child(device_t, device_t);
102 static struct resource *localbus_alloc_resource(device_t, device_t, int,
103 int *, rman_res_t, rman_res_t, rman_res_t, u_int);
104 static struct resource_list *localbus_get_resource_list(device_t, device_t);
106 static ofw_bus_get_devinfo_t localbus_get_devinfo;
109 * Bus interface definition.
111 static device_method_t localbus_methods[] = {
112 /* Device interface */
113 DEVMETHOD(device_probe, localbus_probe),
114 DEVMETHOD(device_attach, localbus_attach),
115 DEVMETHOD(device_detach, bus_generic_detach),
116 DEVMETHOD(device_shutdown, bus_generic_shutdown),
117 DEVMETHOD(device_suspend, bus_generic_suspend),
118 DEVMETHOD(device_resume, bus_generic_resume),
121 DEVMETHOD(bus_print_child, localbus_print_child),
122 DEVMETHOD(bus_alloc_resource, localbus_alloc_resource),
123 DEVMETHOD(bus_release_resource, bus_generic_release_resource),
124 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
125 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
126 DEVMETHOD(bus_setup_intr, bus_generic_setup_intr),
127 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr),
128 DEVMETHOD(bus_get_resource_list, localbus_get_resource_list),
130 /* OFW bus interface */
131 DEVMETHOD(ofw_bus_get_devinfo, localbus_get_devinfo),
132 DEVMETHOD(ofw_bus_get_compat, ofw_bus_gen_get_compat),
133 DEVMETHOD(ofw_bus_get_model, ofw_bus_gen_get_model),
134 DEVMETHOD(ofw_bus_get_name, ofw_bus_gen_get_name),
135 DEVMETHOD(ofw_bus_get_node, ofw_bus_gen_get_node),
136 DEVMETHOD(ofw_bus_get_type, ofw_bus_gen_get_type),
141 static driver_t localbus_driver = {
144 sizeof(struct localbus_softc)
147 const struct localbus_va_entry localbus_virtmap[] = {
148 { 0, MV_DEV_BOOT_BASE, MV_DEV_BOOT_SIZE },
149 { 1, MV_DEV_CS0_BASE, MV_DEV_CS0_SIZE },
150 { 2, MV_DEV_CS1_BASE, MV_DEV_CS1_SIZE },
151 { 3, MV_DEV_CS2_BASE, MV_DEV_CS2_SIZE },
156 static struct localbus_bank localbus_banks[MV_LOCALBUS_MAX_BANKS];
158 devclass_t localbus_devclass;
160 DRIVER_MODULE(localbus, ofwbus, localbus_driver, localbus_devclass, 0, 0);
163 fdt_localbus_reg_decode(phandle_t node, struct localbus_softc *sc,
164 struct localbus_devinfo *di)
166 u_long start, end, count;
167 pcell_t *reg, *regptr;
168 pcell_t addr_cells, size_cells;
169 int tuple_size, tuples;
172 if (fdt_addrsize_cells(OF_parent(node), &addr_cells, &size_cells) != 0)
175 tuple_size = sizeof(pcell_t) * (addr_cells + size_cells);
176 tuples = OF_getprop_alloc(node, "reg", tuple_size, (void **)®);
177 debugf("addr_cells = %d, size_cells = %d\n", addr_cells, size_cells);
178 debugf("tuples = %d, tuple size = %d\n", tuples, tuple_size);
180 /* No 'reg' property in this node. */
184 for (i = 0; i < tuples; i++) {
186 bank = fdt_data_get((void *)regptr, 1);
188 if (bank >= MV_LOCALBUS_MAX_BANKS) {
189 device_printf(sc->sc_dev, "bank number [%d] out of "
195 * If device doesn't have virtual to physical mapping don't add
198 if (!(sc->sc_banks[bank].mapped)) {
199 device_printf(sc->sc_dev, "device [%d]: missing memory "
207 /* Get address/size. */
208 rv = fdt_data_to_res(regptr, addr_cells - 1, size_cells, &start,
211 resource_list_free(&di->di_res);
215 /* Check if enough amount of memory is mapped */
216 if (sc->sc_banks[bank].size < count) {
217 device_printf(sc->sc_dev, "device [%d]: not enough "
218 "memory reserved\n", bank);
222 regptr += addr_cells - 1 + size_cells;
224 /* Calculate address range relative to VA base. */
225 start = sc->sc_banks[bank].va + start;
226 end = start + count - 1;
228 debugf("reg addr bank = %d, start = %lx, end = %lx, "
229 "count = %lx\n", bank, start, end, count);
231 /* Use bank (CS) cell as rid. */
232 resource_list_add(&di->di_res, SYS_RES_MEMORY, di->di_bank,
242 localbus_probe(device_t dev)
245 if (!ofw_bus_is_compatible_strict(dev, "mrvl,lbc"))
248 device_set_desc(dev, "Marvell device bus");
250 return (BUS_PROBE_DEFAULT);
254 localbus_attach(device_t dev)
257 struct localbus_softc *sc;
258 struct localbus_devinfo *di;
259 phandle_t dt_node, dt_child;
261 sc = device_get_softc(dev);
263 sc->sc_banks = localbus_banks;
266 * Walk localbus and add direct subordinates as our children.
268 dt_node = ofw_bus_get_node(dev);
269 for (dt_child = OF_child(dt_node); dt_child != 0;
270 dt_child = OF_peer(dt_child)) {
272 /* Check and process 'status' property. */
273 if (!(fdt_is_enabled(dt_child)))
276 if (!(fdt_pm_is_enabled(dt_child)))
279 di = malloc(sizeof(*di), M_LOCALBUS, M_WAITOK | M_ZERO);
280 if (ofw_bus_gen_setup_devinfo(&di->di_ofw, dt_child) != 0) {
281 free(di, M_LOCALBUS);
282 device_printf(dev, "could not set up devinfo\n");
286 resource_list_init(&di->di_res);
287 if (fdt_localbus_reg_decode(dt_child, sc, di)) {
288 device_printf(dev, "could not process 'reg' "
290 ofw_bus_gen_destroy_devinfo(&di->di_ofw);
291 free(di, M_LOCALBUS);
295 /* Add newbus device for this FDT node */
296 dev_child = device_add_child(dev, NULL, -1);
297 if (dev_child == NULL) {
298 device_printf(dev, "could not add child: %s\n",
299 di->di_ofw.obd_name);
300 resource_list_free(&di->di_res);
301 ofw_bus_gen_destroy_devinfo(&di->di_ofw);
302 free(di, M_LOCALBUS);
306 device_printf(dev, "added child: %s\n\n", di->di_ofw.obd_name);
308 device_set_ivars(dev_child, di);
311 return (bus_generic_attach(dev));
315 localbus_print_child(device_t dev, device_t child)
317 struct localbus_devinfo *di;
318 struct resource_list *rl;
321 di = device_get_ivars(child);
325 rv += bus_print_child_header(dev, child);
326 rv += resource_list_print_type(rl, "mem", SYS_RES_MEMORY, "%#jx");
327 rv += resource_list_print_type(rl, "irq", SYS_RES_IRQ, "%jd");
328 rv += bus_print_child_footer(dev, child);
333 static struct resource *
334 localbus_alloc_resource(device_t bus, device_t child, int type, int *rid,
335 rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
337 struct localbus_devinfo *di;
338 struct resource_list_entry *rle;
341 * Request for the default allocation with a given rid: use resource
342 * list stored in the local device info.
344 if (RMAN_IS_DEFAULT_RANGE(start, end)) {
345 if ((di = device_get_ivars(child)) == NULL)
348 if (type == SYS_RES_IOPORT)
349 type = SYS_RES_MEMORY;
352 rle = resource_list_find(&di->di_res, type, *rid);
354 device_printf(bus, "no default resources for "
355 "rid = %d, type = %d\n", *rid, type);
363 return (bus_generic_alloc_resource(bus, child, type, rid, start, end,
368 static struct resource_list *
369 localbus_get_resource_list(device_t bus, device_t child)
371 struct localbus_devinfo *di;
373 di = device_get_ivars(child);
374 return (&di->di_res);
377 static const struct ofw_bus_devinfo *
378 localbus_get_devinfo(device_t bus, device_t child)
380 struct localbus_devinfo *di;
382 di = device_get_ivars(child);
383 return (&di->di_ofw);
387 fdt_localbus_devmap(phandle_t dt_node, struct devmap_entry *fdt_devmap,
388 int banks_max_num, int *banks_added)
390 pcell_t ranges[MV_LOCALBUS_MAX_BANKS * MV_LOCALBUS_MAX_BANK_CELLS];
392 uint32_t tuple_size, bank;
395 int dev_num, addr_cells, size_cells, par_addr_cells, va_index, i, j, k;
397 if ((fdt_addrsize_cells(dt_node, &addr_cells, &size_cells)) != 0)
400 par_addr_cells = fdt_parent_addr_cells(dt_node);
401 if (par_addr_cells > 2) {
403 * Localbus devmap initialization error: unsupported parent
409 tuple_size = (addr_cells + par_addr_cells + size_cells);
410 if (tuple_size > MV_LOCALBUS_MAX_BANK_CELLS)
413 tuple_size *= sizeof(pcell_t);
415 dev_num = OF_getprop(dt_node, "ranges", ranges, sizeof(ranges));
419 /* Calculate number of devices attached to bus */
420 dev_num = dev_num / tuple_size;
423 * If number of ranges > max number of localbus devices,
424 * additional entries will not be processed
426 dev_num = MIN(dev_num, banks_max_num);
428 rangesptr = &ranges[0];
431 /* Process data from FDT */
432 for (i = 0; i < dev_num; i++) {
434 /* First field is bank number */
435 bank = fdt_data_get((void *)rangesptr, 1);
438 if (bank > MV_LOCALBUS_MAX_BANKS) {
439 /* Bank out of range */
440 rangesptr += ((addr_cells - 1) + par_addr_cells +
445 /* Find virtmap entry for this bank */
447 for (k = 0; localbus_virtmap[k].bank >= 0; k++) {
448 if (localbus_virtmap[k].bank == bank) {
454 /* Check if virtmap entry was found */
455 if (va_index == -1) {
456 rangesptr += ((addr_cells - 1) + par_addr_cells +
461 /* Remaining child's address fields are unused */
462 rangesptr += (addr_cells - 1);
464 /* Parent address offset */
465 offset = fdt_data_get((void *)rangesptr, par_addr_cells);
466 rangesptr += par_addr_cells;
468 /* Last field is size */
469 size = fdt_data_get((void *)rangesptr, size_cells);
470 rangesptr += size_cells;
472 if (size > localbus_virtmap[va_index].size) {
473 /* Not enough space reserved in virtual memory map */
477 fdt_devmap[j].pd_va = localbus_virtmap[va_index].va;
478 fdt_devmap[j].pd_pa = offset;
479 fdt_devmap[j].pd_size = size;
481 /* Copy data to structure used by localbus driver */
482 localbus_banks[bank].va = fdt_devmap[j].pd_va;
483 localbus_banks[bank].pa = fdt_devmap[j].pd_pa;
484 localbus_banks[bank].size = fdt_devmap[j].pd_size;
485 localbus_banks[bank].mapped = 1;