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/kernel.h>
35 #include <sys/malloc.h>
36 #include <sys/systm.h>
38 #include <contrib/libfdt/libfdt.h>
40 #include <machine/stdarg.h>
42 #include <dev/fdt/fdt_common.h>
43 #include <dev/ofw/ofwvar.h>
44 #include <dev/ofw/openfirm.h>
49 #define debugf(fmt, args...) do { printf("%s(): ", __func__); \
50 printf(fmt,##args); } while (0)
52 #define debugf(fmt, args...)
55 static int ofw_fdt_init(ofw_t, void *);
56 static phandle_t ofw_fdt_peer(ofw_t, phandle_t);
57 static phandle_t ofw_fdt_child(ofw_t, phandle_t);
58 static phandle_t ofw_fdt_parent(ofw_t, phandle_t);
59 static phandle_t ofw_fdt_instance_to_package(ofw_t, ihandle_t);
60 static ssize_t ofw_fdt_getproplen(ofw_t, phandle_t, const char *);
61 static ssize_t ofw_fdt_getprop(ofw_t, phandle_t, const char *, void *, size_t);
62 static int ofw_fdt_nextprop(ofw_t, phandle_t, const char *, char *, size_t);
63 static int ofw_fdt_setprop(ofw_t, phandle_t, const char *, const void *,
65 static ssize_t ofw_fdt_canon(ofw_t, const char *, char *, size_t);
66 static phandle_t ofw_fdt_finddevice(ofw_t, const char *);
67 static ssize_t ofw_fdt_instance_to_path(ofw_t, ihandle_t, char *, size_t);
68 static ssize_t ofw_fdt_package_to_path(ofw_t, phandle_t, char *, size_t);
69 static int ofw_fdt_interpret(ofw_t, const char *, int, cell_t *);
71 static ofw_method_t ofw_fdt_methods[] = {
72 OFWMETHOD(ofw_init, ofw_fdt_init),
73 OFWMETHOD(ofw_peer, ofw_fdt_peer),
74 OFWMETHOD(ofw_child, ofw_fdt_child),
75 OFWMETHOD(ofw_parent, ofw_fdt_parent),
76 OFWMETHOD(ofw_instance_to_package, ofw_fdt_instance_to_package),
77 OFWMETHOD(ofw_getproplen, ofw_fdt_getproplen),
78 OFWMETHOD(ofw_getprop, ofw_fdt_getprop),
79 OFWMETHOD(ofw_nextprop, ofw_fdt_nextprop),
80 OFWMETHOD(ofw_setprop, ofw_fdt_setprop),
81 OFWMETHOD(ofw_canon, ofw_fdt_canon),
82 OFWMETHOD(ofw_finddevice, ofw_fdt_finddevice),
83 OFWMETHOD(ofw_instance_to_path, ofw_fdt_instance_to_path),
84 OFWMETHOD(ofw_package_to_path, ofw_fdt_package_to_path),
85 OFWMETHOD(ofw_interpret, ofw_fdt_interpret),
89 static ofw_def_t ofw_fdt = {
96 static void *fdtp = NULL;
99 ofw_fdt_init(ofw_t ofw, void *data)
103 /* Check FDT blob integrity */
104 if ((err = fdt_check_header(data)) != 0)
112 * Device tree functions.
114 * We use the offset from fdtp to the node as the 'phandle' in OF interface.
116 * phandle is a u32 value, therefore we cannot use the pointer to node as
117 * phandle in 64 bit. We also do not use the usual fdt offset as phandle,
118 * as it can be 0, and the OF interface has special meaning for phandle 0.
122 fdt_offset_phandle(int offset)
126 return ((phandle_t)offset + fdt_off_dt_struct(fdtp));
130 fdt_phandle_offset(phandle_t p)
133 int dtoff = fdt_off_dt_struct(fdtp);
137 return (pint - dtoff);
141 fdt_pointer_offset(const void *ptr)
143 uintptr_t dt_struct, p;
147 dt_struct = (uintptr_t)fdtp + fdt_off_dt_struct(fdtp);
149 if ((p < dt_struct) ||
150 p > (dt_struct + fdt_size_dt_struct(fdtp)))
153 offset = p - dt_struct;
160 /* Return the next sibling of this node or 0. */
162 ofw_fdt_peer(ofw_t ofw, phandle_t node)
168 offset = fdt_path_offset(fdtp, "/");
170 return (fdt_offset_phandle(offset));
173 offset = fdt_phandle_offset(node);
177 for (depth = 1, offset = fdt_next_node(fdtp, offset, &depth);
179 offset = fdt_next_node(fdtp, offset, &depth)) {
183 return (fdt_offset_phandle(offset));
189 /* Return the first child of this node or 0. */
191 ofw_fdt_child(ofw_t ofw, phandle_t node)
195 offset = fdt_phandle_offset(node);
199 for (depth = 0, offset = fdt_next_node(fdtp, offset, &depth);
200 (offset >= 0) && (depth > 0);
201 offset = fdt_next_node(fdtp, offset, &depth)) {
205 return (fdt_offset_phandle(offset));
211 /* Return the parent of this node or 0. */
213 ofw_fdt_parent(ofw_t ofw, phandle_t node)
215 int offset, paroffset;
217 offset = fdt_phandle_offset(node);
221 paroffset = fdt_parent_offset(fdtp, offset);
222 return (fdt_offset_phandle(paroffset));
225 /* Return the package handle that corresponds to an instance handle. */
227 ofw_fdt_instance_to_package(ofw_t ofw, ihandle_t instance)
232 * Note: FDT does not have the notion of instances, but we somewhat
233 * abuse the semantics and let treat as 'instance' the internal
234 * 'phandle' prop, so that ofw I/F consumers have a uniform way of
235 * translation between internal representation (which appear in some
236 * contexts as property values) and effective phandles.
238 offset = fdt_node_offset_by_phandle(fdtp, instance);
242 return (fdt_offset_phandle(offset));
245 /* Get the length of a property of a package. */
247 ofw_fdt_getproplen(ofw_t ofw, phandle_t package, const char *propname)
249 const struct fdt_property *prop;
252 offset = fdt_phandle_offset(package);
256 if (strcmp(propname, "name") == 0) {
257 /* Emulate the 'name' property */
258 fdt_get_name(fdtp, offset, &len);
263 prop = fdt_get_property(fdtp, offset, propname, &len);
268 /* Get the value of a property of a package. */
270 ofw_fdt_getprop(ofw_t ofw, phandle_t package, const char *propname, void *buf,
277 offset = fdt_phandle_offset(package);
281 if (strcmp(propname, "name") == 0) {
282 /* Emulate the 'name' property */
283 name = fdt_get_name(fdtp, offset, &len);
284 strncpy(buf, name, buflen);
285 if (len + 1 > buflen)
290 prop = fdt_getprop(fdtp, offset, propname, &len);
296 bcopy(prop, buf, len);
301 fdt_nextprop(int offset, char *buf, size_t size)
303 const struct fdt_property *prop;
306 int nextoffset, depth;
309 tag = fdt_next_tag(fdtp, offset, &nextoffset);
311 /* Find the next prop */
314 tag = fdt_next_tag(fdtp, offset, &nextoffset);
316 if (tag == FDT_BEGIN_NODE)
318 else if (tag == FDT_END_NODE)
320 else if ((tag == FDT_PROP) && (depth == 0)) {
322 (const struct fdt_property *)fdt_offset_ptr(fdtp,
323 offset, sizeof(*prop));
324 name = fdt_string(fdtp,
325 fdt32_to_cpu(prop->nameoff));
326 strncpy(buf, name, size);
327 return (strlen(name));
330 } while (depth >= 0);
336 * Get the next property of a package. Return the actual len of retrieved
340 ofw_fdt_nextprop(ofw_t ofw, phandle_t package, const char *previous, char *buf,
343 const struct fdt_property *prop;
346 offset = fdt_phandle_offset(package);
350 if (previous == NULL)
351 /* Find the first prop in the node */
352 return (fdt_nextprop(offset, buf, size));
355 * Advance to the previous prop
357 prop = fdt_get_property(fdtp, offset, previous, NULL);
361 offset = fdt_pointer_offset(prop);
362 rv = fdt_nextprop(offset, buf, size);
366 /* Set the value of a property of a package. */
368 ofw_fdt_setprop(ofw_t ofw, phandle_t package, const char *propname,
369 const void *buf, size_t len)
373 offset = fdt_phandle_offset(package);
377 return (fdt_setprop_inplace(fdtp, offset, propname, buf, len));
380 /* Convert a device specifier to a fully qualified pathname. */
382 ofw_fdt_canon(ofw_t ofw, const char *device, char *buf, size_t len)
388 /* Return a package handle for the specified device. */
390 ofw_fdt_finddevice(ofw_t ofw, const char *device)
394 offset = fdt_path_offset(fdtp, device);
397 return (fdt_offset_phandle(offset));
400 /* Return the fully qualified pathname corresponding to an instance. */
402 ofw_fdt_instance_to_path(ofw_t ofw, ihandle_t instance, char *buf, size_t len)
408 /* Return the fully qualified pathname corresponding to a package. */
410 ofw_fdt_package_to_path(ofw_t ofw, phandle_t package, char *buf, size_t len)
417 ofw_fdt_fixup(ofw_t ofw)
419 #define FDT_MODEL_LEN 80
420 char model[FDT_MODEL_LEN];
425 if ((root = ofw_fdt_finddevice(ofw, "/")) == -1)
428 if ((len = ofw_fdt_getproplen(ofw, root, "model")) <= 0)
431 bzero(model, FDT_MODEL_LEN);
432 if (ofw_fdt_getprop(ofw, root, "model", model, FDT_MODEL_LEN) <= 0)
436 * Search fixup table and call handler if appropriate.
438 for (i = 0; fdt_fixup_table[i].model != NULL; i++) {
439 if (strncmp(model, fdt_fixup_table[i].model,
443 if (fdt_fixup_table[i].handler != NULL)
444 (*fdt_fixup_table[i].handler)(root);
451 ofw_fdt_interpret(ofw_t ofw, const char *cmd, int nret, cell_t *retvals)
456 * Note: FDT does not have the possibility to 'interpret' commands,
457 * but we abuse the interface a bit to use it for doing non-standard
458 * operations on the device tree blob.
460 * Currently the only supported 'command' is to trigger performing
463 if (strncmp("perform-fixup", cmd, 13) != 0)
466 rv = ofw_fdt_fixup(ofw);