xref: /freebsd/sys/dev/ofw/ofw_fdt.c (revision fdafd315ad0d0f28a11b9fb4476a9ab059c62b92)
16365985dSRafal Jaworowski /*-
2*4d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
3718cf2ccSPedro F. Giffuni  *
46365985dSRafal Jaworowski  * Copyright (c) 2009-2010 The FreeBSD Foundation
56365985dSRafal Jaworowski  *
66365985dSRafal Jaworowski  * This software was developed by Semihalf under sponsorship from
76365985dSRafal Jaworowski  * the FreeBSD Foundation.
86365985dSRafal Jaworowski  *
96365985dSRafal Jaworowski  * Redistribution and use in source and binary forms, with or without
106365985dSRafal Jaworowski  * modification, are permitted provided that the following conditions
116365985dSRafal Jaworowski  * are met:
126365985dSRafal Jaworowski  * 1. Redistributions of source code must retain the above copyright
136365985dSRafal Jaworowski  *    notice, this list of conditions and the following disclaimer.
146365985dSRafal Jaworowski  * 2. Redistributions in binary form must reproduce the above copyright
156365985dSRafal Jaworowski  *    notice, this list of conditions and the following disclaimer in the
166365985dSRafal Jaworowski  *    documentation and/or other materials provided with the distribution.
176365985dSRafal Jaworowski  *
186365985dSRafal Jaworowski  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
196365985dSRafal Jaworowski  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
206365985dSRafal Jaworowski  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
216365985dSRafal Jaworowski  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
226365985dSRafal Jaworowski  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
236365985dSRafal Jaworowski  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
246365985dSRafal Jaworowski  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
256365985dSRafal Jaworowski  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
266365985dSRafal Jaworowski  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
276365985dSRafal Jaworowski  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
286365985dSRafal Jaworowski  * SUCH DAMAGE.
296365985dSRafal Jaworowski  */
306365985dSRafal Jaworowski 
316365985dSRafal Jaworowski #include <sys/param.h>
32b8b0c65eSMike Karels #include <sys/ctype.h>
336365985dSRafal Jaworowski #include <sys/kernel.h>
346365985dSRafal Jaworowski #include <sys/malloc.h>
356365985dSRafal Jaworowski #include <sys/systm.h>
366365985dSRafal Jaworowski 
376365985dSRafal Jaworowski #include <contrib/libfdt/libfdt.h>
386365985dSRafal Jaworowski 
396365985dSRafal Jaworowski #include <machine/stdarg.h>
406365985dSRafal Jaworowski 
416365985dSRafal Jaworowski #include <dev/fdt/fdt_common.h>
426365985dSRafal Jaworowski #include <dev/ofw/ofwvar.h>
436365985dSRafal Jaworowski #include <dev/ofw/openfirm.h>
44a355bb88SMarcin Wojtas #include <dev/ofw/ofw_bus_subr.h>
456365985dSRafal Jaworowski 
466365985dSRafal Jaworowski #include "ofw_if.h"
476365985dSRafal Jaworowski 
486365985dSRafal Jaworowski #ifdef DEBUG
496365985dSRafal Jaworowski #define debugf(fmt, args...) do { printf("%s(): ", __func__);	\
506365985dSRafal Jaworowski     printf(fmt,##args); } while (0)
516365985dSRafal Jaworowski #else
526365985dSRafal Jaworowski #define debugf(fmt, args...)
536365985dSRafal Jaworowski #endif
546365985dSRafal Jaworowski 
550a4c0732SAndrew Turner #if defined(__arm__)
560a4c0732SAndrew Turner #if defined(SOC_MV_ARMADAXP) || defined(SOC_MV_ARMADA38X) || \
570a4c0732SAndrew Turner     defined(SOC_MV_DISCOVERY) || defined(SOC_MV_DOVE) || \
580a4c0732SAndrew Turner     defined(SOC_MV_FREY) || defined(SOC_MV_KIRKWOOD) || \
590a4c0732SAndrew Turner     defined(SOC_MV_LOKIPLUS) || defined(SOC_MV_ORION)
600a4c0732SAndrew Turner #define FDT_MARVELL
610a4c0732SAndrew Turner #endif
620a4c0732SAndrew Turner #endif
630a4c0732SAndrew Turner 
646365985dSRafal Jaworowski static int ofw_fdt_init(ofw_t, void *);
656365985dSRafal Jaworowski static phandle_t ofw_fdt_peer(ofw_t, phandle_t);
666365985dSRafal Jaworowski static phandle_t ofw_fdt_child(ofw_t, phandle_t);
676365985dSRafal Jaworowski static phandle_t ofw_fdt_parent(ofw_t, phandle_t);
686365985dSRafal Jaworowski static phandle_t ofw_fdt_instance_to_package(ofw_t, ihandle_t);
696365985dSRafal Jaworowski static ssize_t ofw_fdt_getproplen(ofw_t, phandle_t, const char *);
706365985dSRafal Jaworowski static ssize_t ofw_fdt_getprop(ofw_t, phandle_t, const char *, void *, size_t);
716365985dSRafal Jaworowski static int ofw_fdt_nextprop(ofw_t, phandle_t, const char *, char *, size_t);
726365985dSRafal Jaworowski static int ofw_fdt_setprop(ofw_t, phandle_t, const char *, const void *,
736365985dSRafal Jaworowski     size_t);
746365985dSRafal Jaworowski static ssize_t ofw_fdt_canon(ofw_t, const char *, char *, size_t);
756365985dSRafal Jaworowski static phandle_t ofw_fdt_finddevice(ofw_t, const char *);
766365985dSRafal Jaworowski static ssize_t ofw_fdt_instance_to_path(ofw_t, ihandle_t, char *, size_t);
776365985dSRafal Jaworowski static ssize_t ofw_fdt_package_to_path(ofw_t, phandle_t, char *, size_t);
78cdb25d82SMarius Strobl static int ofw_fdt_interpret(ofw_t, const char *, int, cell_t *);
796365985dSRafal Jaworowski 
806365985dSRafal Jaworowski static ofw_method_t ofw_fdt_methods[] = {
816365985dSRafal Jaworowski 	OFWMETHOD(ofw_init,			ofw_fdt_init),
826365985dSRafal Jaworowski 	OFWMETHOD(ofw_peer,			ofw_fdt_peer),
836365985dSRafal Jaworowski 	OFWMETHOD(ofw_child,			ofw_fdt_child),
846365985dSRafal Jaworowski 	OFWMETHOD(ofw_parent,			ofw_fdt_parent),
856365985dSRafal Jaworowski 	OFWMETHOD(ofw_instance_to_package,	ofw_fdt_instance_to_package),
866365985dSRafal Jaworowski 	OFWMETHOD(ofw_getproplen,		ofw_fdt_getproplen),
876365985dSRafal Jaworowski 	OFWMETHOD(ofw_getprop,			ofw_fdt_getprop),
886365985dSRafal Jaworowski 	OFWMETHOD(ofw_nextprop,			ofw_fdt_nextprop),
896365985dSRafal Jaworowski 	OFWMETHOD(ofw_setprop,			ofw_fdt_setprop),
906365985dSRafal Jaworowski 	OFWMETHOD(ofw_canon,			ofw_fdt_canon),
916365985dSRafal Jaworowski 	OFWMETHOD(ofw_finddevice,		ofw_fdt_finddevice),
926365985dSRafal Jaworowski 	OFWMETHOD(ofw_instance_to_path,		ofw_fdt_instance_to_path),
936365985dSRafal Jaworowski 	OFWMETHOD(ofw_package_to_path,		ofw_fdt_package_to_path),
946365985dSRafal Jaworowski 	OFWMETHOD(ofw_interpret,		ofw_fdt_interpret),
956365985dSRafal Jaworowski 	{ 0, 0 }
966365985dSRafal Jaworowski };
976365985dSRafal Jaworowski 
986365985dSRafal Jaworowski static ofw_def_t ofw_fdt = {
996365985dSRafal Jaworowski 	OFW_FDT,
1006365985dSRafal Jaworowski 	ofw_fdt_methods,
1016365985dSRafal Jaworowski 	0
1026365985dSRafal Jaworowski };
1036365985dSRafal Jaworowski OFW_DEF(ofw_fdt);
1046365985dSRafal Jaworowski 
10550e0dc0cSEmmanuel Vadot #define	FDT_FBSDVER_LEN	16
10650e0dc0cSEmmanuel Vadot #define	FDT_MODEL_LEN	80
10750e0dc0cSEmmanuel Vadot #define	FDT_COMPAT_LEN	255
108b8b0c65eSMike Karels #define	FDT_SERIAL_LEN	32
10950e0dc0cSEmmanuel Vadot 
1106365985dSRafal Jaworowski static void *fdtp = NULL;
11150e0dc0cSEmmanuel Vadot static char fdt_model[FDT_MODEL_LEN];
11250e0dc0cSEmmanuel Vadot static char fdt_compatible[FDT_COMPAT_LEN];
11350e0dc0cSEmmanuel Vadot static char fdt_fbsd_version[FDT_FBSDVER_LEN];
114b8b0c65eSMike Karels static char fdt_serial[FDT_SERIAL_LEN];
1156365985dSRafal Jaworowski 
1166365985dSRafal Jaworowski static int
sysctl_handle_dtb(SYSCTL_HANDLER_ARGS)117a183d81dSStephen J. Kiernan sysctl_handle_dtb(SYSCTL_HANDLER_ARGS)
118a183d81dSStephen J. Kiernan {
119a183d81dSStephen J. Kiernan 
120a183d81dSStephen J. Kiernan         return (sysctl_handle_opaque(oidp, fdtp, fdt_totalsize(fdtp), req));
121a183d81dSStephen J. Kiernan }
122a183d81dSStephen J. Kiernan 
123a183d81dSStephen J. Kiernan static void
sysctl_register_fdt_oid(void * arg)124a183d81dSStephen J. Kiernan sysctl_register_fdt_oid(void *arg)
125a183d81dSStephen J. Kiernan {
126a183d81dSStephen J. Kiernan 
127a183d81dSStephen J. Kiernan 	/* If there is no FDT registered, skip adding the sysctl */
128a183d81dSStephen J. Kiernan 	if (fdtp == NULL)
129a183d81dSStephen J. Kiernan 		return;
130a183d81dSStephen J. Kiernan 
131a183d81dSStephen J. Kiernan 	SYSCTL_ADD_PROC(NULL, SYSCTL_STATIC_CHILDREN(_hw_fdt), OID_AUTO, "dtb",
1325c2c6e01SPawel Biernacki 	    CTLTYPE_OPAQUE | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
1335c2c6e01SPawel Biernacki 	    sysctl_handle_dtb, "", "Device Tree Blob");
13450e0dc0cSEmmanuel Vadot 	if (fdt_model[0] != '\0')
13550e0dc0cSEmmanuel Vadot 		SYSCTL_ADD_STRING(NULL, SYSCTL_STATIC_CHILDREN(_hw_fdt),
13650e0dc0cSEmmanuel Vadot 		    OID_AUTO, "model", CTLFLAG_RD, fdt_model,
13750e0dc0cSEmmanuel Vadot 		    FDT_MODEL_LEN, "System board model");
13850e0dc0cSEmmanuel Vadot 	if (fdt_compatible[0] != '\0')
13950e0dc0cSEmmanuel Vadot 		SYSCTL_ADD_STRING(NULL, SYSCTL_STATIC_CHILDREN(_hw_fdt),
14050e0dc0cSEmmanuel Vadot 		    OID_AUTO, "compatible", CTLFLAG_RD, fdt_compatible,
14150e0dc0cSEmmanuel Vadot 		    FDT_COMPAT_LEN, "Compatible platforms");
14250e0dc0cSEmmanuel Vadot 	if (fdt_fbsd_version[0] != '\0')
14350e0dc0cSEmmanuel Vadot 		SYSCTL_ADD_STRING(NULL, SYSCTL_STATIC_CHILDREN(_hw_fdt),
14450e0dc0cSEmmanuel Vadot 		    OID_AUTO, "freebsd-version", CTLFLAG_RD, fdt_fbsd_version,
14550e0dc0cSEmmanuel Vadot 		    FDT_FBSDVER_LEN, "FreeBSD DTS branding version");
146b8b0c65eSMike Karels 	if (fdt_serial[0] != '\0')
147b8b0c65eSMike Karels 		SYSCTL_ADD_STRING(NULL, SYSCTL_STATIC_CHILDREN(_hw_fdt),
148b8b0c65eSMike Karels 		    OID_AUTO, "serial-number", CTLFLAG_RD, fdt_serial,
149b8b0c65eSMike Karels 		    FDT_SERIAL_LEN, "Serial number");
150a183d81dSStephen J. Kiernan }
151891cf3edSEd Maste SYSINIT(dtb_oid, SI_SUB_KMEM, SI_ORDER_ANY, sysctl_register_fdt_oid, NULL);
152a183d81dSStephen J. Kiernan 
153a183d81dSStephen J. Kiernan static int
ofw_fdt_init(ofw_t ofw,void * data)1546365985dSRafal Jaworowski ofw_fdt_init(ofw_t ofw, void *data)
1556365985dSRafal Jaworowski {
15650e0dc0cSEmmanuel Vadot 	phandle_t root;
15750e0dc0cSEmmanuel Vadot 	ssize_t len;
1586365985dSRafal Jaworowski 	int err;
15950e0dc0cSEmmanuel Vadot 	int i;
1606365985dSRafal Jaworowski 
1616365985dSRafal Jaworowski 	/* Check FDT blob integrity */
1626365985dSRafal Jaworowski 	if ((err = fdt_check_header(data)) != 0)
1636365985dSRafal Jaworowski 		return (err);
1646365985dSRafal Jaworowski 
1656365985dSRafal Jaworowski 	fdtp = data;
16650e0dc0cSEmmanuel Vadot 	root = ofw_fdt_finddevice(NULL, "/");
16750e0dc0cSEmmanuel Vadot 	len = ofw_fdt_getproplen(NULL, root, "model");
168b8b0c65eSMike Karels 	if (len > 0 && len <= FDT_MODEL_LEN) {
16950e0dc0cSEmmanuel Vadot 		bzero(fdt_model, FDT_MODEL_LEN);
17050e0dc0cSEmmanuel Vadot 		ofw_fdt_getprop(NULL, root, "model", fdt_model, FDT_MODEL_LEN);
171b8b0c65eSMike Karels 	}
17250e0dc0cSEmmanuel Vadot 	len = ofw_fdt_getproplen(NULL, root, "compatible");
173b8b0c65eSMike Karels 	if (len > 0 && len <= FDT_COMPAT_LEN) {
17450e0dc0cSEmmanuel Vadot 		bzero(fdt_compatible, FDT_COMPAT_LEN);
175b8b0c65eSMike Karels 		ofw_fdt_getprop(NULL, root, "compatible", fdt_compatible,
176b8b0c65eSMike Karels 		    FDT_COMPAT_LEN);
17750e0dc0cSEmmanuel Vadot 		/* Replace the middle '\0' with ' ' */
17850e0dc0cSEmmanuel Vadot 		for (i = 0; i < len - 1; i++)
17950e0dc0cSEmmanuel Vadot 			if (fdt_compatible[i] == '\0')
18050e0dc0cSEmmanuel Vadot 				fdt_compatible[i] = ' ';
181b8b0c65eSMike Karels 	}
182b8b0c65eSMike Karels 	len = ofw_fdt_getproplen(NULL, root, "freebsd,dts-version");
183b8b0c65eSMike Karels 	if (len > 0 && len <= FDT_FBSDVER_LEN) {
18450e0dc0cSEmmanuel Vadot 		bzero(fdt_fbsd_version, FDT_FBSDVER_LEN);
18550e0dc0cSEmmanuel Vadot 		ofw_fdt_getprop(NULL, root, "freebsd,dts-version",
18650e0dc0cSEmmanuel Vadot 		  fdt_fbsd_version, FDT_FBSDVER_LEN);
18750e0dc0cSEmmanuel Vadot 	}
188b8b0c65eSMike Karels 	len = ofw_fdt_getproplen(NULL, root, "serial-number");
189b8b0c65eSMike Karels 	if (len > 0 && len <= FDT_SERIAL_LEN) {
190b8b0c65eSMike Karels 		bzero(fdt_serial, FDT_SERIAL_LEN);
191b8b0c65eSMike Karels 		ofw_fdt_getprop(NULL, root, "serial-number",
192b8b0c65eSMike Karels 		    fdt_serial, FDT_SERIAL_LEN);
193b8b0c65eSMike Karels 		/*
194b8b0c65eSMike Karels 		 * Non-standard property; check for NUL-terminated
195b8b0c65eSMike Karels 		 * printable string.
196b8b0c65eSMike Karels 		 */
197b8b0c65eSMike Karels 		for (i = 0; i < len - 1; i++) {
198b8b0c65eSMike Karels 			if (!isprint(fdt_serial[i])) {
199b8b0c65eSMike Karels 				fdt_serial[0] = '\0';
200b8b0c65eSMike Karels 				break;
201b8b0c65eSMike Karels 			}
202b8b0c65eSMike Karels 		}
203b8b0c65eSMike Karels 		if (fdt_serial[len - 1] != '\0')
204b8b0c65eSMike Karels 			fdt_serial[0] = '\0';
205b8b0c65eSMike Karels 	}
2066365985dSRafal Jaworowski 	return (0);
2076365985dSRafal Jaworowski }
2086365985dSRafal Jaworowski 
2096365985dSRafal Jaworowski /*
210af6edc28SJayachandran C.  * Device tree functions.
211af6edc28SJayachandran C.  *
212af6edc28SJayachandran C.  * We use the offset from fdtp to the node as the 'phandle' in OF interface.
213af6edc28SJayachandran C.  *
214af6edc28SJayachandran C.  * phandle is a u32 value, therefore we cannot use the pointer to node as
215af6edc28SJayachandran C.  * phandle in 64 bit. We also do not use the usual fdt offset as phandle,
216af6edc28SJayachandran C.  * as it can be 0, and the OF interface has special meaning for phandle 0.
2176365985dSRafal Jaworowski  */
2186365985dSRafal Jaworowski 
219af6edc28SJayachandran C. static phandle_t
fdt_offset_phandle(int offset)220af6edc28SJayachandran C. fdt_offset_phandle(int offset)
221af6edc28SJayachandran C. {
222af6edc28SJayachandran C. 	if (offset < 0)
223af6edc28SJayachandran C. 		return (0);
224af6edc28SJayachandran C. 	return ((phandle_t)offset + fdt_off_dt_struct(fdtp));
225af6edc28SJayachandran C. }
226af6edc28SJayachandran C. 
2276365985dSRafal Jaworowski static int
fdt_phandle_offset(phandle_t p)2286365985dSRafal Jaworowski fdt_phandle_offset(phandle_t p)
2296365985dSRafal Jaworowski {
230af6edc28SJayachandran C. 	int pint = (int)p;
231af6edc28SJayachandran C. 	int dtoff = fdt_off_dt_struct(fdtp);
232af6edc28SJayachandran C. 
233af6edc28SJayachandran C. 	if (pint < dtoff)
234af6edc28SJayachandran C. 		return (-1);
235af6edc28SJayachandran C. 	return (pint - dtoff);
236af6edc28SJayachandran C. }
237af6edc28SJayachandran C. 
2386365985dSRafal Jaworowski /* Return the next sibling of this node or 0. */
2396365985dSRafal Jaworowski static phandle_t
ofw_fdt_peer(ofw_t ofw,phandle_t node)2406365985dSRafal Jaworowski ofw_fdt_peer(ofw_t ofw, phandle_t node)
2416365985dSRafal Jaworowski {
242cd78f5ffSKyle Evans 	int offset;
2436365985dSRafal Jaworowski 
2446365985dSRafal Jaworowski 	if (node == 0) {
2456365985dSRafal Jaworowski 		/* Find root node */
2466365985dSRafal Jaworowski 		offset = fdt_path_offset(fdtp, "/");
2476365985dSRafal Jaworowski 
248af6edc28SJayachandran C. 		return (fdt_offset_phandle(offset));
2496365985dSRafal Jaworowski 	}
2506365985dSRafal Jaworowski 
2516365985dSRafal Jaworowski 	offset = fdt_phandle_offset(node);
2526365985dSRafal Jaworowski 	if (offset < 0)
2536365985dSRafal Jaworowski 		return (0);
254cd78f5ffSKyle Evans 	offset = fdt_next_subnode(fdtp, offset);
255af6edc28SJayachandran C. 	return (fdt_offset_phandle(offset));
2566365985dSRafal Jaworowski }
2576365985dSRafal Jaworowski 
2586365985dSRafal Jaworowski /* Return the first child of this node or 0. */
2596365985dSRafal Jaworowski static phandle_t
ofw_fdt_child(ofw_t ofw,phandle_t node)2606365985dSRafal Jaworowski ofw_fdt_child(ofw_t ofw, phandle_t node)
2616365985dSRafal Jaworowski {
262cd78f5ffSKyle Evans 	int offset;
2636365985dSRafal Jaworowski 
2646365985dSRafal Jaworowski 	offset = fdt_phandle_offset(node);
2656365985dSRafal Jaworowski 	if (offset < 0)
2666365985dSRafal Jaworowski 		return (0);
267cd78f5ffSKyle Evans 	offset = fdt_first_subnode(fdtp, offset);
268af6edc28SJayachandran C. 	return (fdt_offset_phandle(offset));
2696365985dSRafal Jaworowski }
2706365985dSRafal Jaworowski 
2716365985dSRafal Jaworowski /* Return the parent of this node or 0. */
2726365985dSRafal Jaworowski static phandle_t
ofw_fdt_parent(ofw_t ofw,phandle_t node)2736365985dSRafal Jaworowski ofw_fdt_parent(ofw_t ofw, phandle_t node)
2746365985dSRafal Jaworowski {
2756365985dSRafal Jaworowski 	int offset, paroffset;
2766365985dSRafal Jaworowski 
2776365985dSRafal Jaworowski 	offset = fdt_phandle_offset(node);
2786365985dSRafal Jaworowski 	if (offset < 0)
2796365985dSRafal Jaworowski 		return (0);
2806365985dSRafal Jaworowski 
2816365985dSRafal Jaworowski 	paroffset = fdt_parent_offset(fdtp, offset);
282af6edc28SJayachandran C. 	return (fdt_offset_phandle(paroffset));
2836365985dSRafal Jaworowski }
2846365985dSRafal Jaworowski 
2856365985dSRafal Jaworowski /* Return the package handle that corresponds to an instance handle. */
2866365985dSRafal Jaworowski static phandle_t
ofw_fdt_instance_to_package(ofw_t ofw,ihandle_t instance)2876365985dSRafal Jaworowski ofw_fdt_instance_to_package(ofw_t ofw, ihandle_t instance)
2886365985dSRafal Jaworowski {
2896365985dSRafal Jaworowski 
290fb5b9994SNathan Whitehorn 	/* Where real OF uses ihandles in the tree, FDT uses xref phandles */
291752ba930SIan Lepore 	return (OF_node_from_xref(instance));
2926365985dSRafal Jaworowski }
2936365985dSRafal Jaworowski 
2946365985dSRafal Jaworowski /* Get the length of a property of a package. */
2956365985dSRafal Jaworowski static ssize_t
ofw_fdt_getproplen(ofw_t ofw,phandle_t package,const char * propname)2966365985dSRafal Jaworowski ofw_fdt_getproplen(ofw_t ofw, phandle_t package, const char *propname)
2976365985dSRafal Jaworowski {
29801d1c9d8SNathan Whitehorn 	const void *prop;
2996365985dSRafal Jaworowski 	int offset, len;
3006365985dSRafal Jaworowski 
3016365985dSRafal Jaworowski 	offset = fdt_phandle_offset(package);
3026365985dSRafal Jaworowski 	if (offset < 0)
30396fe0b35SRafal Jaworowski 		return (-1);
3046365985dSRafal Jaworowski 
3055499037cSNathan Whitehorn 	len = -1;
30601d1c9d8SNathan Whitehorn 	prop = fdt_getprop(fdtp, offset, propname, &len);
3075499037cSNathan Whitehorn 
3085499037cSNathan Whitehorn 	if (prop == NULL && strcmp(propname, "name") == 0) {
3096365985dSRafal Jaworowski 		/* Emulate the 'name' property */
3106365985dSRafal Jaworowski 		fdt_get_name(fdtp, offset, &len);
3116365985dSRafal Jaworowski 		return (len + 1);
3126365985dSRafal Jaworowski 	}
3136365985dSRafal Jaworowski 
314c884d311SNathan Whitehorn 	if (prop == NULL && offset == fdt_path_offset(fdtp, "/chosen")) {
315c884d311SNathan Whitehorn 		if (strcmp(propname, "fdtbootcpu") == 0)
316c884d311SNathan Whitehorn 			return (sizeof(cell_t));
317c884d311SNathan Whitehorn 		if (strcmp(propname, "fdtmemreserv") == 0)
318c884d311SNathan Whitehorn 			return (sizeof(uint64_t)*2*fdt_num_mem_rsv(fdtp));
319c884d311SNathan Whitehorn 	}
320c884d311SNathan Whitehorn 
321d2d3e9b8SNathan Whitehorn 	if (prop == NULL)
322d2d3e9b8SNathan Whitehorn 		return (-1);
323d2d3e9b8SNathan Whitehorn 
3246365985dSRafal Jaworowski 	return (len);
3256365985dSRafal Jaworowski }
3266365985dSRafal Jaworowski 
3276365985dSRafal Jaworowski /* Get the value of a property of a package. */
3286365985dSRafal Jaworowski static ssize_t
ofw_fdt_getprop(ofw_t ofw,phandle_t package,const char * propname,void * buf,size_t buflen)3296365985dSRafal Jaworowski ofw_fdt_getprop(ofw_t ofw, phandle_t package, const char *propname, void *buf,
3306365985dSRafal Jaworowski     size_t buflen)
3316365985dSRafal Jaworowski {
3326365985dSRafal Jaworowski 	const void *prop;
3336365985dSRafal Jaworowski 	const char *name;
3346365985dSRafal Jaworowski 	int len, offset;
335c884d311SNathan Whitehorn 	uint32_t cpuid;
3366365985dSRafal Jaworowski 
3376365985dSRafal Jaworowski 	offset = fdt_phandle_offset(package);
3386365985dSRafal Jaworowski 	if (offset < 0)
33996fe0b35SRafal Jaworowski 		return (-1);
3406365985dSRafal Jaworowski 
3415499037cSNathan Whitehorn 	prop = fdt_getprop(fdtp, offset, propname, &len);
3425499037cSNathan Whitehorn 
3435499037cSNathan Whitehorn 	if (prop == NULL && strcmp(propname, "name") == 0) {
3446365985dSRafal Jaworowski 		/* Emulate the 'name' property */
3456365985dSRafal Jaworowski 		name = fdt_get_name(fdtp, offset, &len);
3466365985dSRafal Jaworowski 		strncpy(buf, name, buflen);
3476365985dSRafal Jaworowski 		return (len + 1);
3486365985dSRafal Jaworowski 	}
3496365985dSRafal Jaworowski 
350c884d311SNathan Whitehorn 	if (prop == NULL && offset == fdt_path_offset(fdtp, "/chosen")) {
351c884d311SNathan Whitehorn 		if (strcmp(propname, "fdtbootcpu") == 0) {
352c884d311SNathan Whitehorn 			cpuid = cpu_to_fdt32(fdt_boot_cpuid_phys(fdtp));
353c884d311SNathan Whitehorn 			len = sizeof(cpuid);
354c884d311SNathan Whitehorn 			prop = &cpuid;
355c884d311SNathan Whitehorn 		}
356c884d311SNathan Whitehorn 		if (strcmp(propname, "fdtmemreserv") == 0) {
357c884d311SNathan Whitehorn 			prop = (char *)fdtp + fdt_off_mem_rsvmap(fdtp);
358c884d311SNathan Whitehorn 			len = sizeof(uint64_t)*2*fdt_num_mem_rsv(fdtp);
359c884d311SNathan Whitehorn 		}
360c884d311SNathan Whitehorn 	}
361c884d311SNathan Whitehorn 
3626365985dSRafal Jaworowski 	if (prop == NULL)
36396fe0b35SRafal Jaworowski 		return (-1);
3646365985dSRafal Jaworowski 
3654a4b4c98SBreno Leitao 	bcopy(prop, buf, min(len, buflen));
3664a4b4c98SBreno Leitao 
3676365985dSRafal Jaworowski 	return (len);
3686365985dSRafal Jaworowski }
3696365985dSRafal Jaworowski 
3706365985dSRafal Jaworowski /*
371641ed462SNathan Whitehorn  * Get the next property of a package. Return values:
372641ed462SNathan Whitehorn  *  -1: package or previous property does not exist
373641ed462SNathan Whitehorn  *   0: no more properties
374641ed462SNathan Whitehorn  *   1: success
3756365985dSRafal Jaworowski  */
3766365985dSRafal Jaworowski static int
ofw_fdt_nextprop(ofw_t ofw,phandle_t package,const char * previous,char * buf,size_t size)3776365985dSRafal Jaworowski ofw_fdt_nextprop(ofw_t ofw, phandle_t package, const char *previous, char *buf,
3786365985dSRafal Jaworowski     size_t size)
3796365985dSRafal Jaworowski {
38001d1c9d8SNathan Whitehorn 	const void *prop;
3811b12f87dSNathan Whitehorn 	const char *name;
3821b12f87dSNathan Whitehorn 	int offset;
3836365985dSRafal Jaworowski 
3846365985dSRafal Jaworowski 	offset = fdt_phandle_offset(package);
3856365985dSRafal Jaworowski 	if (offset < 0)
38696fe0b35SRafal Jaworowski 		return (-1);
3876365985dSRafal Jaworowski 
388cd78f5ffSKyle Evans 	if (previous == NULL)
3896365985dSRafal Jaworowski 		/* Find the first prop in the node */
3901b12f87dSNathan Whitehorn 		offset = fdt_first_property_offset(fdtp, offset);
391cd78f5ffSKyle Evans 	else {
392cd78f5ffSKyle Evans 		fdt_for_each_property_offset(offset, fdtp, offset) {
39301d1c9d8SNathan Whitehorn 			prop = fdt_getprop_by_offset(fdtp, offset, &name, NULL);
3946365985dSRafal Jaworowski 			if (prop == NULL)
3951b12f87dSNathan Whitehorn 				return (-1); /* Internal error */
396cd78f5ffSKyle Evans 			/* Skip until we find 'previous', then bail out */
397cd78f5ffSKyle Evans 			if (strcmp(name, previous) != 0)
398cd78f5ffSKyle Evans 				continue;
3991b12f87dSNathan Whitehorn 			offset = fdt_next_property_offset(fdtp, offset);
4001b12f87dSNathan Whitehorn 			break;
4011b12f87dSNathan Whitehorn 		}
4021b12f87dSNathan Whitehorn 	}
4031b12f87dSNathan Whitehorn 
404cd78f5ffSKyle Evans 	if (offset < 0)
405cd78f5ffSKyle Evans 		return (0); /* No properties */
406cd78f5ffSKyle Evans 
40701d1c9d8SNathan Whitehorn 	prop = fdt_getprop_by_offset(fdtp, offset, &name, &offset);
4081b12f87dSNathan Whitehorn 	if (prop == NULL)
4091b12f87dSNathan Whitehorn 		return (-1); /* Internal error */
4101b12f87dSNathan Whitehorn 
41101d1c9d8SNathan Whitehorn 	strncpy(buf, name, size);
4121b12f87dSNathan Whitehorn 
413641ed462SNathan Whitehorn 	return (1);
4146365985dSRafal Jaworowski }
4156365985dSRafal Jaworowski 
4166365985dSRafal Jaworowski /* Set the value of a property of a package. */
4176365985dSRafal Jaworowski static int
ofw_fdt_setprop(ofw_t ofw,phandle_t package,const char * propname,const void * buf,size_t len)4186365985dSRafal Jaworowski ofw_fdt_setprop(ofw_t ofw, phandle_t package, const char *propname,
4196365985dSRafal Jaworowski     const void *buf, size_t len)
4206365985dSRafal Jaworowski {
4216365985dSRafal Jaworowski 	int offset;
4226365985dSRafal Jaworowski 
4236365985dSRafal Jaworowski 	offset = fdt_phandle_offset(package);
4246365985dSRafal Jaworowski 	if (offset < 0)
4256365985dSRafal Jaworowski 		return (-1);
4266365985dSRafal Jaworowski 
427e2b9d202SMarcin Wojtas 	if (fdt_setprop_inplace(fdtp, offset, propname, buf, len) != 0)
428e2b9d202SMarcin Wojtas 		/* Try to add property, when setting value inplace failed */
429e2b9d202SMarcin Wojtas 		return (fdt_setprop(fdtp, offset, propname, buf, len));
430e2b9d202SMarcin Wojtas 
431e2b9d202SMarcin Wojtas 	return (0);
4326365985dSRafal Jaworowski }
4336365985dSRafal Jaworowski 
4346365985dSRafal Jaworowski /* Convert a device specifier to a fully qualified pathname. */
4356365985dSRafal Jaworowski static ssize_t
ofw_fdt_canon(ofw_t ofw,const char * device,char * buf,size_t len)4366365985dSRafal Jaworowski ofw_fdt_canon(ofw_t ofw, const char *device, char *buf, size_t len)
4376365985dSRafal Jaworowski {
4386365985dSRafal Jaworowski 
4396365985dSRafal Jaworowski 	return (-1);
4406365985dSRafal Jaworowski }
4416365985dSRafal Jaworowski 
4426365985dSRafal Jaworowski /* Return a package handle for the specified device. */
4436365985dSRafal Jaworowski static phandle_t
ofw_fdt_finddevice(ofw_t ofw,const char * device)4446365985dSRafal Jaworowski ofw_fdt_finddevice(ofw_t ofw, const char *device)
4456365985dSRafal Jaworowski {
4466365985dSRafal Jaworowski 	int offset;
4476365985dSRafal Jaworowski 
4486365985dSRafal Jaworowski 	offset = fdt_path_offset(fdtp, device);
44907042befSJayachandran C. 	if (offset < 0)
45007042befSJayachandran C. 		return (-1);
451af6edc28SJayachandran C. 	return (fdt_offset_phandle(offset));
4526365985dSRafal Jaworowski }
4536365985dSRafal Jaworowski 
4546365985dSRafal Jaworowski /* Return the fully qualified pathname corresponding to an instance. */
4556365985dSRafal Jaworowski static ssize_t
ofw_fdt_instance_to_path(ofw_t ofw,ihandle_t instance,char * buf,size_t len)4566365985dSRafal Jaworowski ofw_fdt_instance_to_path(ofw_t ofw, ihandle_t instance, char *buf, size_t len)
4576365985dSRafal Jaworowski {
458fb5b9994SNathan Whitehorn 	phandle_t phandle;
4596365985dSRafal Jaworowski 
460fb5b9994SNathan Whitehorn 	phandle = OF_instance_to_package(instance);
461fb5b9994SNathan Whitehorn 	if (phandle == -1)
4626365985dSRafal Jaworowski 		return (-1);
463fb5b9994SNathan Whitehorn 
464fb5b9994SNathan Whitehorn 	return (OF_package_to_path(phandle, buf, len));
4656365985dSRafal Jaworowski }
4666365985dSRafal Jaworowski 
4676365985dSRafal Jaworowski /* Return the fully qualified pathname corresponding to a package. */
4686365985dSRafal Jaworowski static ssize_t
ofw_fdt_package_to_path(ofw_t ofw,phandle_t package,char * buf,size_t len)4696365985dSRafal Jaworowski ofw_fdt_package_to_path(ofw_t ofw, phandle_t package, char *buf, size_t len)
4706365985dSRafal Jaworowski {
4716365985dSRafal Jaworowski 
4726365985dSRafal Jaworowski 	return (-1);
4736365985dSRafal Jaworowski }
4746365985dSRafal Jaworowski 
475ed03d62aSJustin Hibbits #if defined(FDT_MARVELL)
4766365985dSRafal Jaworowski static int
ofw_fdt_fixup(ofw_t ofw)4776365985dSRafal Jaworowski ofw_fdt_fixup(ofw_t ofw)
4786365985dSRafal Jaworowski {
4796365985dSRafal Jaworowski 	char model[FDT_MODEL_LEN];
4806365985dSRafal Jaworowski 	phandle_t root;
4816365985dSRafal Jaworowski 	ssize_t len;
4826365985dSRafal Jaworowski 	int i;
4836365985dSRafal Jaworowski 
48407042befSJayachandran C. 	if ((root = ofw_fdt_finddevice(ofw, "/")) == -1)
4856365985dSRafal Jaworowski 		return (ENODEV);
4866365985dSRafal Jaworowski 
4876365985dSRafal Jaworowski 	if ((len = ofw_fdt_getproplen(ofw, root, "model")) <= 0)
4886365985dSRafal Jaworowski 		return (0);
4896365985dSRafal Jaworowski 
4906365985dSRafal Jaworowski 	bzero(model, FDT_MODEL_LEN);
4916365985dSRafal Jaworowski 	if (ofw_fdt_getprop(ofw, root, "model", model, FDT_MODEL_LEN) <= 0)
4926365985dSRafal Jaworowski 		return (0);
4936365985dSRafal Jaworowski 
4946365985dSRafal Jaworowski 	/*
4956365985dSRafal Jaworowski 	 * Search fixup table and call handler if appropriate.
4966365985dSRafal Jaworowski 	 */
4976365985dSRafal Jaworowski 	for (i = 0; fdt_fixup_table[i].model != NULL; i++) {
4986365985dSRafal Jaworowski 		if (strncmp(model, fdt_fixup_table[i].model,
4996365985dSRafal Jaworowski 		    FDT_MODEL_LEN) != 0)
500a355bb88SMarcin Wojtas 			/*
501a355bb88SMarcin Wojtas 			 * Sometimes it's convenient to provide one
502a355bb88SMarcin Wojtas 			 * fixup entry that refers to many boards.
503a355bb88SMarcin Wojtas 			 * To handle this case, simply check if model
504a355bb88SMarcin Wojtas 			 * is compatible parameter
505a355bb88SMarcin Wojtas 			 */
506a355bb88SMarcin Wojtas 			if(!ofw_bus_node_is_compatible(root,
507a355bb88SMarcin Wojtas 			    fdt_fixup_table[i].model))
5086365985dSRafal Jaworowski 				continue;
5096365985dSRafal Jaworowski 
5106365985dSRafal Jaworowski 		if (fdt_fixup_table[i].handler != NULL)
5116365985dSRafal Jaworowski 			(*fdt_fixup_table[i].handler)(root);
5126365985dSRafal Jaworowski 	}
5136365985dSRafal Jaworowski 
5146365985dSRafal Jaworowski 	return (0);
5156365985dSRafal Jaworowski }
516252a329bSAndrew Turner #endif
5176365985dSRafal Jaworowski 
5186365985dSRafal Jaworowski static int
ofw_fdt_interpret(ofw_t ofw,const char * cmd,int nret,cell_t * retvals)519cdb25d82SMarius Strobl ofw_fdt_interpret(ofw_t ofw, const char *cmd, int nret, cell_t *retvals)
5206365985dSRafal Jaworowski {
521ed03d62aSJustin Hibbits #if defined(FDT_MARVELL)
5226365985dSRafal Jaworowski 	int rv;
5236365985dSRafal Jaworowski 
5246365985dSRafal Jaworowski 	/*
5256365985dSRafal Jaworowski 	 * Note: FDT does not have the possibility to 'interpret' commands,
5266365985dSRafal Jaworowski 	 * but we abuse the interface a bit to use it for doing non-standard
5276365985dSRafal Jaworowski 	 * operations on the device tree blob.
5286365985dSRafal Jaworowski 	 *
5296365985dSRafal Jaworowski 	 * Currently the only supported 'command' is to trigger performing
5306365985dSRafal Jaworowski 	 * fixups.
5316365985dSRafal Jaworowski 	 */
5326365985dSRafal Jaworowski 	if (strncmp("perform-fixup", cmd, 13) != 0)
5336365985dSRafal Jaworowski 		return (0);
5346365985dSRafal Jaworowski 
5356365985dSRafal Jaworowski 	rv = ofw_fdt_fixup(ofw);
5366365985dSRafal Jaworowski 	if (nret > 0)
5376365985dSRafal Jaworowski 		retvals[0] = rv;
5386365985dSRafal Jaworowski 
5396365985dSRafal Jaworowski 	return (rv);
540252a329bSAndrew Turner #else
541252a329bSAndrew Turner 	return (0);
542252a329bSAndrew Turner #endif
5436365985dSRafal Jaworowski }
544