2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2009-2010 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/kernel.h>
37 #include <sys/malloc.h>
38 #include <sys/systm.h>
40 #include <contrib/libfdt/libfdt.h>
42 #include <machine/stdarg.h>
44 #include <dev/fdt/fdt_common.h>
45 #include <dev/ofw/ofwvar.h>
46 #include <dev/ofw/openfirm.h>
47 #include <dev/ofw/ofw_bus_subr.h>
52 #define debugf(fmt, args...) do { printf("%s(): ", __func__); \
53 printf(fmt,##args); } while (0)
55 #define debugf(fmt, args...)
59 #if defined(SOC_MV_ARMADAXP) || defined(SOC_MV_ARMADA38X) || \
60 defined(SOC_MV_DISCOVERY) || defined(SOC_MV_DOVE) || \
61 defined(SOC_MV_FREY) || defined(SOC_MV_KIRKWOOD) || \
62 defined(SOC_MV_LOKIPLUS) || defined(SOC_MV_ORION)
67 static int ofw_fdt_init(ofw_t, void *);
68 static phandle_t ofw_fdt_peer(ofw_t, phandle_t);
69 static phandle_t ofw_fdt_child(ofw_t, phandle_t);
70 static phandle_t ofw_fdt_parent(ofw_t, phandle_t);
71 static phandle_t ofw_fdt_instance_to_package(ofw_t, ihandle_t);
72 static ssize_t ofw_fdt_getproplen(ofw_t, phandle_t, const char *);
73 static ssize_t ofw_fdt_getprop(ofw_t, phandle_t, const char *, void *, size_t);
74 static int ofw_fdt_nextprop(ofw_t, phandle_t, const char *, char *, size_t);
75 static int ofw_fdt_setprop(ofw_t, phandle_t, const char *, const void *,
77 static ssize_t ofw_fdt_canon(ofw_t, const char *, char *, size_t);
78 static phandle_t ofw_fdt_finddevice(ofw_t, const char *);
79 static ssize_t ofw_fdt_instance_to_path(ofw_t, ihandle_t, char *, size_t);
80 static ssize_t ofw_fdt_package_to_path(ofw_t, phandle_t, char *, size_t);
81 static int ofw_fdt_interpret(ofw_t, const char *, int, cell_t *);
83 static ofw_method_t ofw_fdt_methods[] = {
84 OFWMETHOD(ofw_init, ofw_fdt_init),
85 OFWMETHOD(ofw_peer, ofw_fdt_peer),
86 OFWMETHOD(ofw_child, ofw_fdt_child),
87 OFWMETHOD(ofw_parent, ofw_fdt_parent),
88 OFWMETHOD(ofw_instance_to_package, ofw_fdt_instance_to_package),
89 OFWMETHOD(ofw_getproplen, ofw_fdt_getproplen),
90 OFWMETHOD(ofw_getprop, ofw_fdt_getprop),
91 OFWMETHOD(ofw_nextprop, ofw_fdt_nextprop),
92 OFWMETHOD(ofw_setprop, ofw_fdt_setprop),
93 OFWMETHOD(ofw_canon, ofw_fdt_canon),
94 OFWMETHOD(ofw_finddevice, ofw_fdt_finddevice),
95 OFWMETHOD(ofw_instance_to_path, ofw_fdt_instance_to_path),
96 OFWMETHOD(ofw_package_to_path, ofw_fdt_package_to_path),
97 OFWMETHOD(ofw_interpret, ofw_fdt_interpret),
101 static ofw_def_t ofw_fdt = {
108 static void *fdtp = NULL;
111 sysctl_handle_dtb(SYSCTL_HANDLER_ARGS)
114 return (sysctl_handle_opaque(oidp, fdtp, fdt_totalsize(fdtp), req));
118 sysctl_register_fdt_oid(void *arg)
121 /* If there is no FDT registered, skip adding the sysctl */
125 SYSCTL_ADD_PROC(NULL, SYSCTL_STATIC_CHILDREN(_hw_fdt), OID_AUTO, "dtb",
126 CTLTYPE_OPAQUE | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
127 sysctl_handle_dtb, "", "Device Tree Blob");
129 SYSINIT(dtb_oid, SI_SUB_KMEM, SI_ORDER_ANY, sysctl_register_fdt_oid, NULL);
132 ofw_fdt_init(ofw_t ofw, void *data)
136 /* Check FDT blob integrity */
137 if ((err = fdt_check_header(data)) != 0)
145 * Device tree functions.
147 * We use the offset from fdtp to the node as the 'phandle' in OF interface.
149 * phandle is a u32 value, therefore we cannot use the pointer to node as
150 * phandle in 64 bit. We also do not use the usual fdt offset as phandle,
151 * as it can be 0, and the OF interface has special meaning for phandle 0.
155 fdt_offset_phandle(int offset)
159 return ((phandle_t)offset + fdt_off_dt_struct(fdtp));
163 fdt_phandle_offset(phandle_t p)
166 int dtoff = fdt_off_dt_struct(fdtp);
170 return (pint - dtoff);
173 /* Return the next sibling of this node or 0. */
175 ofw_fdt_peer(ofw_t ofw, phandle_t node)
181 offset = fdt_path_offset(fdtp, "/");
183 return (fdt_offset_phandle(offset));
186 offset = fdt_phandle_offset(node);
189 offset = fdt_next_subnode(fdtp, offset);
190 return (fdt_offset_phandle(offset));
193 /* Return the first child of this node or 0. */
195 ofw_fdt_child(ofw_t ofw, phandle_t node)
199 offset = fdt_phandle_offset(node);
202 offset = fdt_first_subnode(fdtp, offset);
203 return (fdt_offset_phandle(offset));
206 /* Return the parent of this node or 0. */
208 ofw_fdt_parent(ofw_t ofw, phandle_t node)
210 int offset, paroffset;
212 offset = fdt_phandle_offset(node);
216 paroffset = fdt_parent_offset(fdtp, offset);
217 return (fdt_offset_phandle(paroffset));
220 /* Return the package handle that corresponds to an instance handle. */
222 ofw_fdt_instance_to_package(ofw_t ofw, ihandle_t instance)
225 /* Where real OF uses ihandles in the tree, FDT uses xref phandles */
226 return (OF_node_from_xref(instance));
229 /* Get the length of a property of a package. */
231 ofw_fdt_getproplen(ofw_t ofw, phandle_t package, const char *propname)
236 offset = fdt_phandle_offset(package);
241 prop = fdt_getprop(fdtp, offset, propname, &len);
243 if (prop == NULL && strcmp(propname, "name") == 0) {
244 /* Emulate the 'name' property */
245 fdt_get_name(fdtp, offset, &len);
249 if (prop == NULL && offset == fdt_path_offset(fdtp, "/chosen")) {
250 if (strcmp(propname, "fdtbootcpu") == 0)
251 return (sizeof(cell_t));
252 if (strcmp(propname, "fdtmemreserv") == 0)
253 return (sizeof(uint64_t)*2*fdt_num_mem_rsv(fdtp));
262 /* Get the value of a property of a package. */
264 ofw_fdt_getprop(ofw_t ofw, phandle_t package, const char *propname, void *buf,
272 offset = fdt_phandle_offset(package);
276 prop = fdt_getprop(fdtp, offset, propname, &len);
278 if (prop == NULL && strcmp(propname, "name") == 0) {
279 /* Emulate the 'name' property */
280 name = fdt_get_name(fdtp, offset, &len);
281 strncpy(buf, name, buflen);
285 if (prop == NULL && offset == fdt_path_offset(fdtp, "/chosen")) {
286 if (strcmp(propname, "fdtbootcpu") == 0) {
287 cpuid = cpu_to_fdt32(fdt_boot_cpuid_phys(fdtp));
291 if (strcmp(propname, "fdtmemreserv") == 0) {
292 prop = (char *)fdtp + fdt_off_mem_rsvmap(fdtp);
293 len = sizeof(uint64_t)*2*fdt_num_mem_rsv(fdtp);
300 bcopy(prop, buf, min(len, buflen));
306 * Get the next property of a package. Return values:
307 * -1: package or previous property does not exist
308 * 0: no more properties
312 ofw_fdt_nextprop(ofw_t ofw, phandle_t package, const char *previous, char *buf,
319 offset = fdt_phandle_offset(package);
323 if (previous == NULL)
324 /* Find the first prop in the node */
325 offset = fdt_first_property_offset(fdtp, offset);
327 fdt_for_each_property_offset(offset, fdtp, offset) {
328 prop = fdt_getprop_by_offset(fdtp, offset, &name, NULL);
330 return (-1); /* Internal error */
331 /* Skip until we find 'previous', then bail out */
332 if (strcmp(name, previous) != 0)
334 offset = fdt_next_property_offset(fdtp, offset);
340 return (0); /* No properties */
342 prop = fdt_getprop_by_offset(fdtp, offset, &name, &offset);
344 return (-1); /* Internal error */
346 strncpy(buf, name, size);
351 /* Set the value of a property of a package. */
353 ofw_fdt_setprop(ofw_t ofw, phandle_t package, const char *propname,
354 const void *buf, size_t len)
358 offset = fdt_phandle_offset(package);
362 if (fdt_setprop_inplace(fdtp, offset, propname, buf, len) != 0)
363 /* Try to add property, when setting value inplace failed */
364 return (fdt_setprop(fdtp, offset, propname, buf, len));
369 /* Convert a device specifier to a fully qualified pathname. */
371 ofw_fdt_canon(ofw_t ofw, const char *device, char *buf, size_t len)
377 /* Return a package handle for the specified device. */
379 ofw_fdt_finddevice(ofw_t ofw, const char *device)
383 offset = fdt_path_offset(fdtp, device);
386 return (fdt_offset_phandle(offset));
389 /* Return the fully qualified pathname corresponding to an instance. */
391 ofw_fdt_instance_to_path(ofw_t ofw, ihandle_t instance, char *buf, size_t len)
395 phandle = OF_instance_to_package(instance);
399 return (OF_package_to_path(phandle, buf, len));
402 /* Return the fully qualified pathname corresponding to a package. */
404 ofw_fdt_package_to_path(ofw_t ofw, phandle_t package, char *buf, size_t len)
410 #if defined(FDT_MARVELL)
412 ofw_fdt_fixup(ofw_t ofw)
414 #define FDT_MODEL_LEN 80
415 char model[FDT_MODEL_LEN];
420 if ((root = ofw_fdt_finddevice(ofw, "/")) == -1)
423 if ((len = ofw_fdt_getproplen(ofw, root, "model")) <= 0)
426 bzero(model, FDT_MODEL_LEN);
427 if (ofw_fdt_getprop(ofw, root, "model", model, FDT_MODEL_LEN) <= 0)
431 * Search fixup table and call handler if appropriate.
433 for (i = 0; fdt_fixup_table[i].model != NULL; i++) {
434 if (strncmp(model, fdt_fixup_table[i].model,
437 * Sometimes it's convenient to provide one
438 * fixup entry that refers to many boards.
439 * To handle this case, simply check if model
440 * is compatible parameter
442 if(!ofw_bus_node_is_compatible(root,
443 fdt_fixup_table[i].model))
446 if (fdt_fixup_table[i].handler != NULL)
447 (*fdt_fixup_table[i].handler)(root);
455 ofw_fdt_interpret(ofw_t ofw, const char *cmd, int nret, cell_t *retvals)
457 #if defined(FDT_MARVELL)
461 * Note: FDT does not have the possibility to 'interpret' commands,
462 * but we abuse the interface a bit to use it for doing non-standard
463 * operations on the device tree blob.
465 * Currently the only supported 'command' is to trigger performing
468 if (strncmp("perform-fixup", cmd, 13) != 0)
471 rv = ofw_fdt_fixup(ofw);