2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2009-2014 The FreeBSD Foundation
7 * This software was developed by Andrew Turner under sponsorship from
8 * the FreeBSD Foundation.
9 * This software was developed by Semihalf under sponsorship from
10 * the FreeBSD Foundation.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
40 #include <sys/module.h>
42 #include <sys/limits.h>
43 #include <sys/sysctl.h>
45 #include <machine/resource.h>
47 #include <dev/fdt/fdt_common.h>
48 #include <dev/ofw/ofw_bus.h>
49 #include <dev/ofw/ofw_bus_subr.h>
50 #include <dev/ofw/openfirm.h>
52 #include "ofw_bus_if.h"
55 #define debugf(fmt, args...) do { printf("%s(): ", __func__); \
56 printf(fmt,##args); } while (0)
58 #define debugf(fmt, args...)
61 #define FDT_COMPAT_LEN 255
63 #define FDT_REG_CELLS 4
64 #define FDT_RANGES_SIZE 48
66 SYSCTL_NODE(_hw, OID_AUTO, fdt, CTLFLAG_RD, 0, "Flattened Device Tree");
68 vm_paddr_t fdt_immr_pa;
69 vm_offset_t fdt_immr_va;
70 vm_offset_t fdt_immr_size;
72 struct fdt_ic_list fdt_ic_list_head = SLIST_HEAD_INITIALIZER(fdt_ic_list_head);
74 static int fdt_is_compatible(phandle_t, const char *);
77 fdt_get_range_by_busaddr(phandle_t node, u_long addr, u_long *base,
80 pcell_t ranges[32], *rangesptr;
81 pcell_t addr_cells, size_cells, par_addr_cells;
82 u_long bus_addr, par_bus_addr, pbase, psize;
83 int err, i, len, tuple_size, tuples;
91 if ((fdt_addrsize_cells(node, &addr_cells, &size_cells)) != 0)
94 * Process 'ranges' property.
96 par_addr_cells = fdt_parent_addr_cells(node);
97 if (par_addr_cells > 2) {
101 len = OF_getproplen(node, "ranges");
104 if (len > sizeof(ranges))
107 return (fdt_get_range_by_busaddr(OF_parent(node), addr,
111 if (OF_getprop(node, "ranges", ranges, sizeof(ranges)) <= 0)
114 tuple_size = addr_cells + par_addr_cells + size_cells;
115 tuples = len / (tuple_size * sizeof(cell_t));
117 if (par_addr_cells > 2 || addr_cells > 2 || size_cells > 2)
123 for (i = 0; i < tuples; i++) {
124 rangesptr = &ranges[i * tuple_size];
126 bus_addr = fdt_data_get((void *)rangesptr, addr_cells);
127 if (bus_addr != addr)
129 rangesptr += addr_cells;
131 par_bus_addr = fdt_data_get((void *)rangesptr, par_addr_cells);
132 rangesptr += par_addr_cells;
134 err = fdt_get_range_by_busaddr(OF_parent(node), par_bus_addr,
141 *base = par_bus_addr;
143 *size = fdt_data_get((void *)rangesptr, size_cells);
152 fdt_get_range(phandle_t node, int range_id, u_long *base, u_long *size)
154 pcell_t ranges[FDT_RANGES_SIZE], *rangesptr;
155 pcell_t addr_cells, size_cells, par_addr_cells;
156 u_long par_bus_addr, pbase, psize;
159 if ((fdt_addrsize_cells(node, &addr_cells, &size_cells)) != 0)
162 * Process 'ranges' property.
164 par_addr_cells = fdt_parent_addr_cells(node);
165 if (par_addr_cells > 2)
168 len = OF_getproplen(node, "ranges");
169 if (len > sizeof(ranges))
177 if (!(range_id < len))
180 if (OF_getprop(node, "ranges", ranges, sizeof(ranges)) <= 0)
183 if (par_addr_cells > 2 || addr_cells > 2 || size_cells > 2)
188 rangesptr = &ranges[range_id];
190 *base = fdt_data_get((void *)rangesptr, addr_cells);
191 rangesptr += addr_cells;
193 par_bus_addr = fdt_data_get((void *)rangesptr, par_addr_cells);
194 rangesptr += par_addr_cells;
196 err = fdt_get_range_by_busaddr(OF_parent(node), par_bus_addr,
201 *base += par_bus_addr;
203 *size = fdt_data_get((void *)rangesptr, size_cells);
208 fdt_immr_addr(vm_offset_t immr_va)
215 * Try to access the SOC node directly i.e. through /aliases/.
217 if ((node = OF_finddevice("soc")) != -1)
218 if (fdt_is_compatible(node, "simple-bus"))
221 * Find the node the long way.
223 if ((node = OF_finddevice("/")) == -1)
226 if ((node = fdt_find_compatible(node, "simple-bus", 0)) == 0)
230 if ((r = fdt_get_range(node, 0, &base, &size)) == 0) {
232 fdt_immr_va = immr_va;
233 fdt_immr_size = size;
240 * This routine is an early-usage version of the ofw_bus_is_compatible() when
241 * the ofw_bus I/F is not available (like early console routines and similar).
242 * Note the buffer has to be on the stack since malloc() is usually not
243 * available in such cases either.
246 fdt_is_compatible(phandle_t node, const char *compatstr)
248 char buf[FDT_COMPAT_LEN];
250 int len, onelen, l, rv;
252 if ((len = OF_getproplen(node, "compatible")) <= 0)
255 compat = (char *)&buf;
256 bzero(compat, FDT_COMPAT_LEN);
258 if (OF_getprop(node, "compatible", compat, FDT_COMPAT_LEN) < 0)
261 onelen = strlen(compatstr);
264 if (strncasecmp(compat, compatstr, onelen) == 0) {
269 /* Slide to the next sub-string. */
270 l = strlen(compat) + 1;
279 fdt_is_compatible_strict(phandle_t node, const char *compatible)
281 char compat[FDT_COMPAT_LEN];
283 if (OF_getproplen(node, "compatible") <= 0)
286 if (OF_getprop(node, "compatible", compat, FDT_COMPAT_LEN) < 0)
289 if (strncasecmp(compat, compatible, FDT_COMPAT_LEN) == 0)
297 fdt_find_compatible(phandle_t start, const char *compat, int strict)
302 * Traverse all children of 'start' node, and find first with
303 * matching 'compatible' property.
305 for (child = OF_child(start); child != 0; child = OF_peer(child))
306 if (fdt_is_compatible(child, compat)) {
308 if (!fdt_is_compatible_strict(child, compat))
316 fdt_depth_search_compatible(phandle_t start, const char *compat, int strict)
318 phandle_t child, node;
321 * Depth-search all descendants of 'start' node, and find first with
322 * matching 'compatible' property.
324 for (node = OF_child(start); node != 0; node = OF_peer(node)) {
325 if (fdt_is_compatible(node, compat) &&
326 (strict == 0 || fdt_is_compatible_strict(node, compat))) {
329 child = fdt_depth_search_compatible(node, compat, strict);
337 fdt_parent_addr_cells(phandle_t node)
341 /* Find out #address-cells of the superior bus. */
342 if (OF_searchprop(OF_parent(node), "#address-cells", &addr_cells,
343 sizeof(addr_cells)) <= 0)
346 return ((int)fdt32_to_cpu(addr_cells));
350 fdt_data_get(void *data, int cells)
354 return (fdt32_to_cpu(*((uint32_t *)data)));
356 return (fdt64_to_cpu(*((uint64_t *)data)));
360 fdt_addrsize_cells(phandle_t node, int *addr_cells, int *size_cells)
366 * Retrieve #{address,size}-cells.
368 cell_size = sizeof(cell);
369 if (OF_getencprop(node, "#address-cells", &cell, cell_size) < cell_size)
371 *addr_cells = (int)cell;
373 if (OF_getencprop(node, "#size-cells", &cell, cell_size) < cell_size)
375 *size_cells = (int)cell;
377 if (*addr_cells > 3 || *size_cells > 2)
383 fdt_data_to_res(pcell_t *data, int addr_cells, int size_cells, u_long *start,
387 /* Address portion. */
391 *start = fdt_data_get((void *)data, addr_cells);
398 *count = fdt_data_get((void *)data, size_cells);
403 fdt_regsize(phandle_t node, u_long *base, u_long *size)
406 int addr_cells, len, size_cells;
408 if (fdt_addrsize_cells(OF_parent(node), &addr_cells, &size_cells))
411 if ((sizeof(pcell_t) * (addr_cells + size_cells)) > sizeof(reg))
414 len = OF_getprop(node, "reg", ®, sizeof(reg));
418 *base = fdt_data_get(®[0], addr_cells);
419 *size = fdt_data_get(®[addr_cells], size_cells);
424 fdt_get_phyaddr(phandle_t node, device_t dev, int *phy_addr, void **phy_sc)
427 pcell_t phy_handle, phy_reg;
429 device_t parent, child;
431 if (OF_getencprop(node, "phy-handle", (void *)&phy_handle,
432 sizeof(phy_handle)) <= 0)
435 phy_node = OF_node_from_xref(phy_handle);
437 if (OF_getencprop(phy_node, "reg", (void *)&phy_reg,
438 sizeof(phy_reg)) <= 0)
444 * Search for softc used to communicate with phy.
448 * Step 1: Search for ancestor of the phy-node with a "phy-handle"
451 phy_node = OF_parent(phy_node);
452 while (phy_node != 0) {
453 if (OF_getprop(phy_node, "phy-handle", (void *)&phy_handle,
454 sizeof(phy_handle)) > 0)
456 phy_node = OF_parent(phy_node);
462 * Step 2: For each device with the same parent and name as ours
463 * compare its node with the one found in step 1, ancestor of phy
464 * node (stored in phy_node).
466 parent = device_get_parent(dev);
468 child = device_find_child(parent, device_get_name(dev), i);
469 while (child != NULL) {
470 if (ofw_bus_get_node(child) == phy_node)
473 child = device_find_child(parent, device_get_name(dev), i);
479 * Use softc of the device found.
481 *phy_sc = (void *)device_get_softc(child);
487 fdt_get_reserved_regions(struct mem_region *mr, int *mrcnt)
489 pcell_t reserve[FDT_REG_CELLS * FDT_MEM_REGIONS];
491 phandle_t memory, root;
492 int addr_cells, size_cells;
493 int i, res_len, rv, tuple_size, tuples;
495 root = OF_finddevice("/");
496 memory = OF_finddevice("/memory");
502 if ((rv = fdt_addrsize_cells(OF_parent(memory), &addr_cells,
506 if (addr_cells > 2) {
511 tuple_size = sizeof(pcell_t) * (addr_cells + size_cells);
513 res_len = OF_getproplen(root, "memreserve");
514 if (res_len <= 0 || res_len > sizeof(reserve)) {
519 if (OF_getprop(root, "memreserve", reserve, res_len) <= 0) {
524 tuples = res_len / tuple_size;
525 reservep = (pcell_t *)&reserve;
526 for (i = 0; i < tuples; i++) {
528 rv = fdt_data_to_res(reservep, addr_cells, size_cells,
529 (u_long *)&mr[i].mr_start, (u_long *)&mr[i].mr_size);
534 reservep += addr_cells + size_cells;
544 fdt_get_mem_regions(struct mem_region *mr, int *mrcnt, uint64_t *memsize)
546 pcell_t reg[FDT_REG_CELLS * FDT_MEM_REGIONS];
549 uint64_t memory_size;
550 int addr_cells, size_cells;
551 int i, reg_len, rv, tuple_size, tuples;
553 memory = OF_finddevice("/memory");
559 if ((rv = fdt_addrsize_cells(OF_parent(memory), &addr_cells,
563 if (addr_cells > 2) {
568 tuple_size = sizeof(pcell_t) * (addr_cells + size_cells);
569 reg_len = OF_getproplen(memory, "reg");
570 if (reg_len <= 0 || reg_len > sizeof(reg)) {
575 if (OF_getprop(memory, "reg", reg, reg_len) <= 0) {
581 tuples = reg_len / tuple_size;
582 regp = (pcell_t *)®
583 for (i = 0; i < tuples; i++) {
585 rv = fdt_data_to_res(regp, addr_cells, size_cells,
586 (u_long *)&mr[i].mr_start, (u_long *)&mr[i].mr_size);
591 regp += addr_cells + size_cells;
592 memory_size += mr[i].mr_size;
595 if (memory_size == 0) {
602 *memsize = memory_size;
609 fdt_get_chosen_bootargs(char *bootargs, size_t max_size)
613 chosen = OF_finddevice("/chosen");
616 if (OF_getprop(chosen, "bootargs", bootargs, max_size) == -1)