ofw_fdt.c revision 208615
1208615Sraj/*-
2208615Sraj * Copyright (c) 2009-2010 The FreeBSD Foundation
3208615Sraj * All rights reserved.
4208615Sraj *
5208615Sraj * This software was developed by Semihalf under sponsorship from
6208615Sraj * the FreeBSD Foundation.
7208615Sraj *
8208615Sraj * Redistribution and use in source and binary forms, with or without
9208615Sraj * modification, are permitted provided that the following conditions
10208615Sraj * are met:
11208615Sraj * 1. Redistributions of source code must retain the above copyright
12208615Sraj *    notice, this list of conditions and the following disclaimer.
13208615Sraj * 2. Redistributions in binary form must reproduce the above copyright
14208615Sraj *    notice, this list of conditions and the following disclaimer in the
15208615Sraj *    documentation and/or other materials provided with the distribution.
16208615Sraj *
17208615Sraj * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18208615Sraj * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19208615Sraj * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20208615Sraj * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21208615Sraj * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22208615Sraj * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23208615Sraj * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24208615Sraj * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25208615Sraj * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26208615Sraj * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27208615Sraj * SUCH DAMAGE.
28208615Sraj */
29208615Sraj
30208615Sraj#include <sys/cdefs.h>
31208615Sraj__FBSDID("$FreeBSD: head/sys/dev/ofw/ofw_fdt.c 208615 2010-05-28 10:51:44Z raj $");
32208615Sraj
33208615Sraj#include <sys/param.h>
34208615Sraj#include <sys/kernel.h>
35208615Sraj#include <sys/malloc.h>
36208615Sraj#include <sys/systm.h>
37208615Sraj
38208615Sraj#include <contrib/libfdt/libfdt.h>
39208615Sraj
40208615Sraj#include <machine/stdarg.h>
41208615Sraj
42208615Sraj#include <dev/fdt/fdt_common.h>
43208615Sraj#include <dev/ofw/ofwvar.h>
44208615Sraj#include <dev/ofw/openfirm.h>
45208615Sraj
46208615Sraj#include "ofw_if.h"
47208615Sraj
48208615Sraj#ifdef DEBUG
49208615Sraj#define debugf(fmt, args...) do { printf("%s(): ", __func__);	\
50208615Sraj    printf(fmt,##args); } while (0)
51208615Sraj#else
52208615Sraj#define debugf(fmt, args...)
53208615Sraj#endif
54208615Sraj
55208615Srajstatic int ofw_fdt_init(ofw_t, void *);
56208615Srajstatic phandle_t ofw_fdt_peer(ofw_t, phandle_t);
57208615Srajstatic phandle_t ofw_fdt_child(ofw_t, phandle_t);
58208615Srajstatic phandle_t ofw_fdt_parent(ofw_t, phandle_t);
59208615Srajstatic phandle_t ofw_fdt_instance_to_package(ofw_t, ihandle_t);
60208615Srajstatic ssize_t ofw_fdt_getproplen(ofw_t, phandle_t, const char *);
61208615Srajstatic ssize_t ofw_fdt_getprop(ofw_t, phandle_t, const char *, void *, size_t);
62208615Srajstatic int ofw_fdt_nextprop(ofw_t, phandle_t, const char *, char *, size_t);
63208615Srajstatic int ofw_fdt_setprop(ofw_t, phandle_t, const char *, const void *,
64208615Sraj    size_t);
65208615Srajstatic ssize_t ofw_fdt_canon(ofw_t, const char *, char *, size_t);
66208615Srajstatic phandle_t ofw_fdt_finddevice(ofw_t, const char *);
67208615Srajstatic ssize_t ofw_fdt_instance_to_path(ofw_t, ihandle_t, char *, size_t);
68208615Srajstatic ssize_t ofw_fdt_package_to_path(ofw_t, phandle_t, char *, size_t);
69208615Srajstatic int ofw_fdt_interpret(ofw_t, const char *, int, unsigned long *);
70208615Sraj
71208615Srajstatic ofw_method_t ofw_fdt_methods[] = {
72208615Sraj	OFWMETHOD(ofw_init,			ofw_fdt_init),
73208615Sraj	OFWMETHOD(ofw_peer,			ofw_fdt_peer),
74208615Sraj	OFWMETHOD(ofw_child,			ofw_fdt_child),
75208615Sraj	OFWMETHOD(ofw_parent,			ofw_fdt_parent),
76208615Sraj	OFWMETHOD(ofw_instance_to_package,	ofw_fdt_instance_to_package),
77208615Sraj	OFWMETHOD(ofw_getproplen,		ofw_fdt_getproplen),
78208615Sraj	OFWMETHOD(ofw_getprop,			ofw_fdt_getprop),
79208615Sraj	OFWMETHOD(ofw_nextprop,			ofw_fdt_nextprop),
80208615Sraj	OFWMETHOD(ofw_setprop,			ofw_fdt_setprop),
81208615Sraj	OFWMETHOD(ofw_canon,			ofw_fdt_canon),
82208615Sraj	OFWMETHOD(ofw_finddevice,		ofw_fdt_finddevice),
83208615Sraj	OFWMETHOD(ofw_instance_to_path,		ofw_fdt_instance_to_path),
84208615Sraj	OFWMETHOD(ofw_package_to_path,		ofw_fdt_package_to_path),
85208615Sraj	OFWMETHOD(ofw_interpret,		ofw_fdt_interpret),
86208615Sraj	{ 0, 0 }
87208615Sraj};
88208615Sraj
89208615Srajstatic ofw_def_t ofw_fdt = {
90208615Sraj	OFW_FDT,
91208615Sraj	ofw_fdt_methods,
92208615Sraj	0
93208615Sraj};
94208615SrajOFW_DEF(ofw_fdt);
95208615Sraj
96208615Srajstatic void *fdtp = NULL;
97208615Sraj
98208615Srajstatic int
99208615Srajofw_fdt_init(ofw_t ofw, void *data)
100208615Sraj{
101208615Sraj	int err;
102208615Sraj
103208615Sraj	/* Check FDT blob integrity */
104208615Sraj	if ((err = fdt_check_header(data)) != 0)
105208615Sraj		return (err);
106208615Sraj
107208615Sraj	fdtp = data;
108208615Sraj	return (0);
109208615Sraj}
110208615Sraj
111208615Sraj/*
112208615Sraj * Device tree functions
113208615Sraj */
114208615Sraj
115208615Srajstatic int
116208615Srajfdt_phandle_offset(phandle_t p)
117208615Sraj{
118208615Sraj	const char *dt_struct;
119208615Sraj	int offset;
120208615Sraj
121208615Sraj	dt_struct = (const char *)fdtp + fdt_off_dt_struct(fdtp);
122208615Sraj
123208615Sraj	if (((const char *)p < dt_struct) ||
124208615Sraj	    (const char *)p > (dt_struct + fdt_size_dt_struct(fdtp)))
125208615Sraj		return (-1);
126208615Sraj
127208615Sraj	offset = (const char *)p - dt_struct;
128208615Sraj	if (offset < 0)
129208615Sraj		return (-1);
130208615Sraj
131208615Sraj	return (offset);
132208615Sraj}
133208615Sraj
134208615Sraj/* Return the next sibling of this node or 0. */
135208615Srajstatic phandle_t
136208615Srajofw_fdt_peer(ofw_t ofw, phandle_t node)
137208615Sraj{
138208615Sraj	phandle_t p;
139208615Sraj	int depth, offset;
140208615Sraj
141208615Sraj	if (node == 0) {
142208615Sraj		/* Find root node */
143208615Sraj		offset = fdt_path_offset(fdtp, "/");
144208615Sraj		p = (phandle_t)fdt_offset_ptr(fdtp, offset, sizeof(p));
145208615Sraj
146208615Sraj		return (p);
147208615Sraj	}
148208615Sraj
149208615Sraj	offset = fdt_phandle_offset(node);
150208615Sraj	if (offset < 0)
151208615Sraj		return (0);
152208615Sraj
153208615Sraj	for (depth = 1, offset = fdt_next_node(fdtp, offset, &depth);
154208615Sraj	    offset >= 0;
155208615Sraj	    offset = fdt_next_node(fdtp, offset, &depth)) {
156208615Sraj		if (depth < 0)
157208615Sraj			return (0);
158208615Sraj		if (depth == 1) {
159208615Sraj			p = (phandle_t)fdt_offset_ptr(fdtp, offset, sizeof(p));
160208615Sraj			return (p);
161208615Sraj		}
162208615Sraj	}
163208615Sraj
164208615Sraj	return (0);
165208615Sraj}
166208615Sraj
167208615Sraj/* Return the first child of this node or 0. */
168208615Srajstatic phandle_t
169208615Srajofw_fdt_child(ofw_t ofw, phandle_t node)
170208615Sraj{
171208615Sraj	phandle_t p;
172208615Sraj	int depth, offset;
173208615Sraj
174208615Sraj	offset = fdt_phandle_offset(node);
175208615Sraj	if (offset < 0)
176208615Sraj		return (0);
177208615Sraj
178208615Sraj	for (depth = 0, offset = fdt_next_node(fdtp, offset, &depth);
179208615Sraj	    (offset >= 0) && (depth > 0);
180208615Sraj	    offset = fdt_next_node(fdtp, offset, &depth)) {
181208615Sraj		if (depth < 0)
182208615Sraj			return (0);
183208615Sraj		if (depth == 1) {
184208615Sraj			p = (phandle_t)fdt_offset_ptr(fdtp, offset, sizeof(p));
185208615Sraj			return (p);
186208615Sraj		}
187208615Sraj	}
188208615Sraj
189208615Sraj	return (0);
190208615Sraj}
191208615Sraj
192208615Sraj/* Return the parent of this node or 0. */
193208615Srajstatic phandle_t
194208615Srajofw_fdt_parent(ofw_t ofw, phandle_t node)
195208615Sraj{
196208615Sraj	phandle_t p;
197208615Sraj	int offset, paroffset;
198208615Sraj
199208615Sraj	offset = fdt_phandle_offset(node);
200208615Sraj	if (offset < 0)
201208615Sraj		return (0);
202208615Sraj
203208615Sraj	paroffset = fdt_parent_offset(fdtp, offset);
204208615Sraj	p = (phandle_t)fdt_offset_ptr(fdtp, paroffset, sizeof(phandle_t));
205208615Sraj	return (p);
206208615Sraj}
207208615Sraj
208208615Sraj/* Return the package handle that corresponds to an instance handle. */
209208615Srajstatic phandle_t
210208615Srajofw_fdt_instance_to_package(ofw_t ofw, ihandle_t instance)
211208615Sraj{
212208615Sraj	phandle_t p;
213208615Sraj	int offset;
214208615Sraj
215208615Sraj	/*
216208615Sraj	 * Note: FDT does not have the notion of instances, but we somewhat
217208615Sraj	 * abuse the semantics and let treat as 'instance' the internal
218208615Sraj	 * 'phandle' prop, so that ofw I/F consumers have a uniform way of
219208615Sraj	 * translation between internal representation (which appear in some
220208615Sraj	 * contexts as property values) and effective phandles.
221208615Sraj	 */
222208615Sraj	offset = fdt_node_offset_by_phandle(fdtp, instance);
223208615Sraj	if (offset < 0)
224208615Sraj		return (0);
225208615Sraj
226208615Sraj	p = (phandle_t)fdt_offset_ptr(fdtp, offset, sizeof(phandle_t));
227208615Sraj	return (p);
228208615Sraj}
229208615Sraj
230208615Sraj/* Get the length of a property of a package. */
231208615Srajstatic ssize_t
232208615Srajofw_fdt_getproplen(ofw_t ofw, phandle_t package, const char *propname)
233208615Sraj{
234208615Sraj	const struct fdt_property *prop;
235208615Sraj	int offset, len;
236208615Sraj
237208615Sraj	offset = fdt_phandle_offset(package);
238208615Sraj	if (offset < 0)
239208615Sraj		return (0);
240208615Sraj
241208615Sraj	if (strcmp(propname, "name") == 0) {
242208615Sraj		/* Emulate the 'name' property */
243208615Sraj		fdt_get_name(fdtp, offset, &len);
244208615Sraj		return (len + 1);
245208615Sraj	}
246208615Sraj
247208615Sraj	len = 0;
248208615Sraj	prop = fdt_get_property(fdtp, offset, propname, &len);
249208615Sraj
250208615Sraj	return (len);
251208615Sraj}
252208615Sraj
253208615Sraj/* Get the value of a property of a package. */
254208615Srajstatic ssize_t
255208615Srajofw_fdt_getprop(ofw_t ofw, phandle_t package, const char *propname, void *buf,
256208615Sraj    size_t buflen)
257208615Sraj{
258208615Sraj	const void *prop;
259208615Sraj	const char *name;
260208615Sraj	int len, offset;
261208615Sraj
262208615Sraj	offset = fdt_phandle_offset(package);
263208615Sraj	if (offset < 0)
264208615Sraj		return (0);
265208615Sraj
266208615Sraj	if (strcmp(propname, "name") == 0) {
267208615Sraj		/* Emulate the 'name' property */
268208615Sraj		name = fdt_get_name(fdtp, offset, &len);
269208615Sraj		strncpy(buf, name, buflen);
270208615Sraj		if (len + 1 > buflen)
271208615Sraj			len = buflen;
272208615Sraj		return (len + 1);
273208615Sraj	}
274208615Sraj
275208615Sraj	prop = fdt_getprop(fdtp, offset, propname, &len);
276208615Sraj	if (prop == NULL)
277208615Sraj		return (0);
278208615Sraj
279208615Sraj	if (len > buflen)
280208615Sraj		len = buflen;
281208615Sraj	bcopy(prop, buf, len);
282208615Sraj	return (len);
283208615Sraj}
284208615Sraj
285208615Srajstatic int
286208615Srajfdt_nextprop(int offset, char *buf, size_t size)
287208615Sraj{
288208615Sraj	const struct fdt_property *prop;
289208615Sraj	const char *name;
290208615Sraj	uint32_t tag;
291208615Sraj	int nextoffset, depth;
292208615Sraj
293208615Sraj	depth = 0;
294208615Sraj	tag = fdt_next_tag(fdtp, offset, &nextoffset);
295208615Sraj
296208615Sraj	/* Find the next prop */
297208615Sraj	do {
298208615Sraj		offset = nextoffset;
299208615Sraj		tag = fdt_next_tag(fdtp, offset, &nextoffset);
300208615Sraj
301208615Sraj		if (tag == FDT_BEGIN_NODE)
302208615Sraj			depth++;
303208615Sraj		else if (tag == FDT_END_NODE)
304208615Sraj			depth--;
305208615Sraj		else if ((tag == FDT_PROP) && (depth == 0)) {
306208615Sraj			prop =
307208615Sraj			    (const struct fdt_property *)fdt_offset_ptr(fdtp,
308208615Sraj			    offset, sizeof(*prop));
309208615Sraj			name = fdt_string(fdtp,
310208615Sraj			    fdt32_to_cpu(prop->nameoff));
311208615Sraj			strncpy(buf, name, size);
312208615Sraj			return (strlen(name));
313208615Sraj		} else
314208615Sraj			depth = -1;
315208615Sraj	} while (depth >= 0);
316208615Sraj
317208615Sraj	return (0);
318208615Sraj}
319208615Sraj
320208615Sraj/*
321208615Sraj * Get the next property of a package. Return the actual len of retrieved
322208615Sraj * prop name.
323208615Sraj */
324208615Srajstatic int
325208615Srajofw_fdt_nextprop(ofw_t ofw, phandle_t package, const char *previous, char *buf,
326208615Sraj    size_t size)
327208615Sraj{
328208615Sraj	const struct fdt_property *prop;
329208615Sraj	int offset, rv;
330208615Sraj
331208615Sraj	offset = fdt_phandle_offset(package);
332208615Sraj	if (offset < 0)
333208615Sraj		return (0);
334208615Sraj
335208615Sraj	if (previous == NULL)
336208615Sraj		/* Find the first prop in the node */
337208615Sraj		return (fdt_nextprop(offset, buf, size));
338208615Sraj
339208615Sraj	/*
340208615Sraj	 * Advance to the previous prop
341208615Sraj	 */
342208615Sraj	prop = fdt_get_property(fdtp, offset, previous, NULL);
343208615Sraj	if (prop == NULL)
344208615Sraj		return (0);
345208615Sraj
346208615Sraj	offset = fdt_phandle_offset((phandle_t)prop);
347208615Sraj	rv = fdt_nextprop(offset, buf, size);
348208615Sraj	return (rv);
349208615Sraj}
350208615Sraj
351208615Sraj/* Set the value of a property of a package. */
352208615Srajstatic int
353208615Srajofw_fdt_setprop(ofw_t ofw, phandle_t package, const char *propname,
354208615Sraj    const void *buf, size_t len)
355208615Sraj{
356208615Sraj	int offset;
357208615Sraj
358208615Sraj	offset = fdt_phandle_offset(package);
359208615Sraj	if (offset < 0)
360208615Sraj		return (-1);
361208615Sraj
362208615Sraj	return (fdt_setprop_inplace(fdtp, offset, propname, buf, len));
363208615Sraj}
364208615Sraj
365208615Sraj/* Convert a device specifier to a fully qualified pathname. */
366208615Srajstatic ssize_t
367208615Srajofw_fdt_canon(ofw_t ofw, const char *device, char *buf, size_t len)
368208615Sraj{
369208615Sraj
370208615Sraj	return (-1);
371208615Sraj}
372208615Sraj
373208615Sraj/* Return a package handle for the specified device. */
374208615Srajstatic phandle_t
375208615Srajofw_fdt_finddevice(ofw_t ofw, const char *device)
376208615Sraj{
377208615Sraj	phandle_t p;
378208615Sraj	int offset;
379208615Sraj
380208615Sraj	offset = fdt_path_offset(fdtp, device);
381208615Sraj
382208615Sraj	p = (phandle_t)fdt_offset_ptr(fdtp, offset, sizeof(p));
383208615Sraj
384208615Sraj	return (p);
385208615Sraj}
386208615Sraj
387208615Sraj/* Return the fully qualified pathname corresponding to an instance. */
388208615Srajstatic ssize_t
389208615Srajofw_fdt_instance_to_path(ofw_t ofw, ihandle_t instance, char *buf, size_t len)
390208615Sraj{
391208615Sraj
392208615Sraj	return (-1);
393208615Sraj}
394208615Sraj
395208615Sraj/* Return the fully qualified pathname corresponding to a package. */
396208615Srajstatic ssize_t
397208615Srajofw_fdt_package_to_path(ofw_t ofw, phandle_t package, char *buf, size_t len)
398208615Sraj{
399208615Sraj
400208615Sraj	return (-1);
401208615Sraj}
402208615Sraj
403208615Srajstatic int
404208615Srajofw_fdt_fixup(ofw_t ofw)
405208615Sraj{
406208615Sraj#define FDT_MODEL_LEN	80
407208615Sraj	char model[FDT_MODEL_LEN];
408208615Sraj	phandle_t root;
409208615Sraj	ssize_t len;
410208615Sraj	int i;
411208615Sraj
412208615Sraj	if ((root = ofw_fdt_finddevice(ofw, "/")) == 0)
413208615Sraj		return (ENODEV);
414208615Sraj
415208615Sraj	if ((len = ofw_fdt_getproplen(ofw, root, "model")) <= 0)
416208615Sraj		return (0);
417208615Sraj
418208615Sraj	bzero(model, FDT_MODEL_LEN);
419208615Sraj	if (ofw_fdt_getprop(ofw, root, "model", model, FDT_MODEL_LEN) <= 0)
420208615Sraj		return (0);
421208615Sraj
422208615Sraj	/*
423208615Sraj	 * Search fixup table and call handler if appropriate.
424208615Sraj	 */
425208615Sraj	for (i = 0; fdt_fixup_table[i].model != NULL; i++) {
426208615Sraj		if (strncmp(model, fdt_fixup_table[i].model,
427208615Sraj		    FDT_MODEL_LEN) != 0)
428208615Sraj			continue;
429208615Sraj
430208615Sraj		if (fdt_fixup_table[i].handler != NULL)
431208615Sraj			(*fdt_fixup_table[i].handler)(root);
432208615Sraj	}
433208615Sraj
434208615Sraj	return (0);
435208615Sraj}
436208615Sraj
437208615Srajstatic int
438208615Srajofw_fdt_interpret(ofw_t ofw, const char *cmd, int nret, unsigned long *retvals)
439208615Sraj{
440208615Sraj	int rv;
441208615Sraj
442208615Sraj	/*
443208615Sraj	 * Note: FDT does not have the possibility to 'interpret' commands,
444208615Sraj	 * but we abuse the interface a bit to use it for doing non-standard
445208615Sraj	 * operations on the device tree blob.
446208615Sraj	 *
447208615Sraj	 * Currently the only supported 'command' is to trigger performing
448208615Sraj	 * fixups.
449208615Sraj	 */
450208615Sraj	if (strncmp("perform-fixup", cmd, 13) != 0)
451208615Sraj		return (0);
452208615Sraj
453208615Sraj	rv = ofw_fdt_fixup(ofw);
454208615Sraj	if (nret > 0)
455208615Sraj		retvals[0] = rv;
456208615Sraj
457208615Sraj	return (rv);
458208615Sraj}
459