2 * Copyright (c) 2009-2014 The FreeBSD Foundation
5 * This software was developed by Andrew Turner under sponsorship from
6 * the FreeBSD Foundation.
7 * This software was developed by Semihalf under sponsorship from
8 * the FreeBSD Foundation.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/module.h>
40 #include <sys/limits.h>
42 #include <machine/resource.h>
44 #include <dev/fdt/fdt_common.h>
45 #include <dev/ofw/ofw_bus.h>
46 #include <dev/ofw/ofw_bus_subr.h>
47 #include <dev/ofw/openfirm.h>
49 #include "ofw_bus_if.h"
52 #define debugf(fmt, args...) do { printf("%s(): ", __func__); \
53 printf(fmt,##args); } while (0)
55 #define debugf(fmt, args...)
58 #define FDT_COMPAT_LEN 255
59 #define FDT_TYPE_LEN 64
61 #define FDT_REG_CELLS 4
63 vm_paddr_t fdt_immr_pa;
64 vm_offset_t fdt_immr_va;
65 vm_offset_t fdt_immr_size;
67 struct fdt_ic_list fdt_ic_list_head = SLIST_HEAD_INITIALIZER(fdt_ic_list_head);
70 fdt_get_range_by_busaddr(phandle_t node, u_long addr, u_long *base,
73 pcell_t ranges[32], *rangesptr;
74 pcell_t addr_cells, size_cells, par_addr_cells;
75 u_long bus_addr, par_bus_addr, pbase, psize;
76 int err, i, len, tuple_size, tuples;
84 if ((fdt_addrsize_cells(node, &addr_cells, &size_cells)) != 0)
87 * Process 'ranges' property.
89 par_addr_cells = fdt_parent_addr_cells(node);
90 if (par_addr_cells > 2) {
94 len = OF_getproplen(node, "ranges");
97 if (len > sizeof(ranges))
100 return (fdt_get_range_by_busaddr(OF_parent(node), addr,
104 if (OF_getprop(node, "ranges", ranges, sizeof(ranges)) <= 0)
107 tuple_size = addr_cells + par_addr_cells + size_cells;
108 tuples = len / (tuple_size * sizeof(cell_t));
110 if (par_addr_cells > 2 || addr_cells > 2 || size_cells > 2)
116 for (i = 0; i < tuples; i++) {
117 rangesptr = &ranges[i * tuple_size];
119 bus_addr = fdt_data_get((void *)rangesptr, addr_cells);
120 if (bus_addr != addr)
122 rangesptr += addr_cells;
124 par_bus_addr = fdt_data_get((void *)rangesptr, par_addr_cells);
125 rangesptr += par_addr_cells;
127 err = fdt_get_range_by_busaddr(OF_parent(node), par_bus_addr,
134 *base = par_bus_addr;
136 *size = fdt_data_get((void *)rangesptr, size_cells);
145 fdt_get_range(phandle_t node, int range_id, u_long *base, u_long *size)
147 pcell_t ranges[6], *rangesptr;
148 pcell_t addr_cells, size_cells, par_addr_cells;
149 u_long par_bus_addr, pbase, psize;
150 int err, len, tuple_size, tuples;
152 if ((fdt_addrsize_cells(node, &addr_cells, &size_cells)) != 0)
155 * Process 'ranges' property.
157 par_addr_cells = fdt_parent_addr_cells(node);
158 if (par_addr_cells > 2)
161 len = OF_getproplen(node, "ranges");
162 if (len > sizeof(ranges))
170 if (!(range_id < len))
173 if (OF_getprop(node, "ranges", ranges, sizeof(ranges)) <= 0)
176 tuple_size = sizeof(pcell_t) * (addr_cells + par_addr_cells +
178 tuples = len / tuple_size;
180 if (par_addr_cells > 2 || addr_cells > 2 || size_cells > 2)
185 rangesptr = &ranges[range_id];
187 *base = fdt_data_get((void *)rangesptr, addr_cells);
188 rangesptr += addr_cells;
190 par_bus_addr = fdt_data_get((void *)rangesptr, par_addr_cells);
191 rangesptr += par_addr_cells;
193 err = fdt_get_range_by_busaddr(OF_parent(node), par_bus_addr,
198 *base += par_bus_addr;
200 *size = fdt_data_get((void *)rangesptr, size_cells);
205 fdt_immr_addr(vm_offset_t immr_va)
212 * Try to access the SOC node directly i.e. through /aliases/.
214 if ((node = OF_finddevice("soc")) != 0)
215 if (fdt_is_compatible_strict(node, "simple-bus"))
218 * Find the node the long way.
220 if ((node = OF_finddevice("/")) == 0)
223 if ((node = fdt_find_compatible(node, "simple-bus", 1)) == 0)
227 if ((r = fdt_get_range(node, 0, &base, &size)) == 0) {
229 fdt_immr_va = immr_va;
230 fdt_immr_size = size;
237 * This routine is an early-usage version of the ofw_bus_is_compatible() when
238 * the ofw_bus I/F is not available (like early console routines and similar).
239 * Note the buffer has to be on the stack since malloc() is usually not
240 * available in such cases either.
243 fdt_is_compatible(phandle_t node, const char *compatstr)
245 char buf[FDT_COMPAT_LEN];
247 int len, onelen, l, rv;
249 if ((len = OF_getproplen(node, "compatible")) <= 0)
252 compat = (char *)&buf;
253 bzero(compat, FDT_COMPAT_LEN);
255 if (OF_getprop(node, "compatible", compat, FDT_COMPAT_LEN) < 0)
258 onelen = strlen(compatstr);
261 if (strncasecmp(compat, compatstr, onelen) == 0) {
266 /* Slide to the next sub-string. */
267 l = strlen(compat) + 1;
276 fdt_is_compatible_strict(phandle_t node, const char *compatible)
278 char compat[FDT_COMPAT_LEN];
280 if (OF_getproplen(node, "compatible") <= 0)
283 if (OF_getprop(node, "compatible", compat, FDT_COMPAT_LEN) < 0)
286 if (strncasecmp(compat, compatible, FDT_COMPAT_LEN) == 0)
294 fdt_find_compatible(phandle_t start, const char *compat, int strict)
299 * Traverse all children of 'start' node, and find first with
300 * matching 'compatible' property.
302 for (child = OF_child(start); child != 0; child = OF_peer(child))
303 if (fdt_is_compatible(child, compat)) {
305 if (!fdt_is_compatible_strict(child, compat))
313 fdt_depth_search_compatible(phandle_t start, const char *compat, int strict)
315 phandle_t child, node;
318 * Depth-search all descendants of 'start' node, and find first with
319 * matching 'compatible' property.
321 for (node = OF_child(start); node != 0; node = OF_peer(node)) {
322 if (fdt_is_compatible(node, compat) &&
323 (strict == 0 || fdt_is_compatible_strict(node, compat))) {
326 child = fdt_depth_search_compatible(node, compat, strict);
334 fdt_is_enabled(phandle_t node)
339 len = OF_getprop_alloc(node, "status", sizeof(char),
343 /* It is OK if no 'status' property. */
346 /* Anything other than 'okay' means disabled. */
348 if (strncmp((char *)stat, "okay", len) == 0)
351 free(stat, M_OFWPROP);
356 fdt_is_type(phandle_t node, const char *typestr)
358 char type[FDT_TYPE_LEN];
360 if (OF_getproplen(node, "device_type") <= 0)
363 if (OF_getprop(node, "device_type", type, FDT_TYPE_LEN) < 0)
366 if (strncasecmp(type, typestr, FDT_TYPE_LEN) == 0)
374 fdt_parent_addr_cells(phandle_t node)
378 /* Find out #address-cells of the superior bus. */
379 if (OF_searchprop(OF_parent(node), "#address-cells", &addr_cells,
380 sizeof(addr_cells)) <= 0)
383 return ((int)fdt32_to_cpu(addr_cells));
387 fdt_pm_is_enabled(phandle_t node)
393 #if defined(SOC_MV_KIRKWOOD) || defined(SOC_MV_DISCOVERY)
400 fdt_data_get(void *data, int cells)
404 return (fdt32_to_cpu(*((uint32_t *)data)));
406 return (fdt64_to_cpu(*((uint64_t *)data)));
410 fdt_addrsize_cells(phandle_t node, int *addr_cells, int *size_cells)
416 * Retrieve #{address,size}-cells.
418 cell_size = sizeof(cell);
419 if (OF_getprop(node, "#address-cells", &cell, cell_size) < cell_size)
421 *addr_cells = fdt32_to_cpu((int)cell);
423 if (OF_getprop(node, "#size-cells", &cell, cell_size) < cell_size)
425 *size_cells = fdt32_to_cpu((int)cell);
427 if (*addr_cells > 3 || *size_cells > 2)
433 fdt_data_to_res(pcell_t *data, int addr_cells, int size_cells, u_long *start,
437 /* Address portion. */
441 *start = fdt_data_get((void *)data, addr_cells);
448 *count = fdt_data_get((void *)data, size_cells);
453 fdt_regsize(phandle_t node, u_long *base, u_long *size)
456 int addr_cells, len, size_cells;
458 if (fdt_addrsize_cells(OF_parent(node), &addr_cells, &size_cells))
461 if ((sizeof(pcell_t) * (addr_cells + size_cells)) > sizeof(reg))
464 len = OF_getprop(node, "reg", ®, sizeof(reg));
468 *base = fdt_data_get(®[0], addr_cells);
469 *size = fdt_data_get(®[addr_cells], size_cells);
474 fdt_reg_to_rl(phandle_t node, struct resource_list *rl)
476 u_long end, count, start;
477 pcell_t *reg, *regptr;
478 pcell_t addr_cells, size_cells;
479 int tuple_size, tuples;
481 long busaddr, bussize;
483 if (fdt_addrsize_cells(OF_parent(node), &addr_cells, &size_cells) != 0)
485 if (fdt_get_range(OF_parent(node), 0, &busaddr, &bussize)) {
490 tuple_size = sizeof(pcell_t) * (addr_cells + size_cells);
491 tuples = OF_getprop_alloc(node, "reg", tuple_size, (void **)®);
492 debugf("addr_cells = %d, size_cells = %d\n", addr_cells, size_cells);
493 debugf("tuples = %d, tuple size = %d\n", tuples, tuple_size);
495 /* No 'reg' property in this node. */
499 for (i = 0; i < tuples; i++) {
501 rv = fdt_data_to_res(reg, addr_cells, size_cells, &start,
504 resource_list_free(rl);
507 reg += addr_cells + size_cells;
509 /* Calculate address range relative to base. */
511 end = start + count - 1;
513 debugf("reg addr start = %lx, end = %lx, count = %lx\n", start,
516 resource_list_add(rl, SYS_RES_MEMORY, i, start, end,
522 free(regptr, M_OFWPROP);
527 fdt_get_phyaddr(phandle_t node, device_t dev, int *phy_addr, void **phy_sc)
530 pcell_t phy_handle, phy_reg;
532 device_t parent, child;
534 if (OF_getencprop(node, "phy-handle", (void *)&phy_handle,
535 sizeof(phy_handle)) <= 0)
538 phy_node = OF_node_from_xref(phy_handle);
540 if (OF_getprop(phy_node, "reg", (void *)&phy_reg,
541 sizeof(phy_reg)) <= 0)
544 *phy_addr = fdt32_to_cpu(phy_reg);
547 * Search for softc used to communicate with phy.
551 * Step 1: Search for ancestor of the phy-node with a "phy-handle"
554 phy_node = OF_parent(phy_node);
555 while (phy_node != 0) {
556 if (OF_getprop(phy_node, "phy-handle", (void *)&phy_handle,
557 sizeof(phy_handle)) > 0)
559 phy_node = OF_parent(phy_node);
565 * Step 2: For each device with the same parent and name as ours
566 * compare its node with the one found in step 1, ancestor of phy
567 * node (stored in phy_node).
569 parent = device_get_parent(dev);
571 child = device_find_child(parent, device_get_name(dev), i);
572 while (child != NULL) {
573 if (ofw_bus_get_node(child) == phy_node)
576 child = device_find_child(parent, device_get_name(dev), i);
582 * Use softc of the device found.
584 *phy_sc = (void *)device_get_softc(child);
590 fdt_get_reserved_regions(struct mem_region *mr, int *mrcnt)
592 pcell_t reserve[FDT_REG_CELLS * FDT_MEM_REGIONS];
594 phandle_t memory, root;
595 uint32_t memory_size;
596 int addr_cells, size_cells;
597 int i, max_size, res_len, rv, tuple_size, tuples;
599 max_size = sizeof(reserve);
600 root = OF_finddevice("/");
601 memory = OF_finddevice("/memory");
607 if ((rv = fdt_addrsize_cells(OF_parent(memory), &addr_cells,
611 if (addr_cells > 2) {
616 tuple_size = sizeof(pcell_t) * (addr_cells + size_cells);
618 res_len = OF_getproplen(root, "memreserve");
619 if (res_len <= 0 || res_len > sizeof(reserve)) {
624 if (OF_getprop(root, "memreserve", reserve, res_len) <= 0) {
630 tuples = res_len / tuple_size;
631 reservep = (pcell_t *)&reserve;
632 for (i = 0; i < tuples; i++) {
634 rv = fdt_data_to_res(reservep, addr_cells, size_cells,
635 (u_long *)&mr[i].mr_start, (u_long *)&mr[i].mr_size);
640 reservep += addr_cells + size_cells;
650 fdt_get_mem_regions(struct mem_region *mr, int *mrcnt, uint32_t *memsize)
652 pcell_t reg[FDT_REG_CELLS * FDT_MEM_REGIONS];
655 uint32_t memory_size;
656 int addr_cells, size_cells;
657 int i, max_size, reg_len, rv, tuple_size, tuples;
659 max_size = sizeof(reg);
660 memory = OF_finddevice("/memory");
666 if ((rv = fdt_addrsize_cells(OF_parent(memory), &addr_cells,
670 if (addr_cells > 2) {
675 tuple_size = sizeof(pcell_t) * (addr_cells + size_cells);
676 reg_len = OF_getproplen(memory, "reg");
677 if (reg_len <= 0 || reg_len > sizeof(reg)) {
682 if (OF_getprop(memory, "reg", reg, reg_len) <= 0) {
688 tuples = reg_len / tuple_size;
689 regp = (pcell_t *)®
690 for (i = 0; i < tuples; i++) {
692 rv = fdt_data_to_res(regp, addr_cells, size_cells,
693 (u_long *)&mr[i].mr_start, (u_long *)&mr[i].mr_size);
698 regp += addr_cells + size_cells;
699 memory_size += mr[i].mr_size;
702 if (memory_size == 0) {
708 *memsize = memory_size;
715 fdt_get_unit(device_t dev)
719 name = ofw_bus_get_name(dev);
720 name = strchr(name, '@') + 1;
722 return (strtol(name,NULL,0));