xref: /freebsd/sys/dev/ofw/ofw_fdt.c (revision 74ca7bf1d4c7173d5575ba168bc4b5f6d181ff5a)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2009-2010 The FreeBSD Foundation
5  * All rights reserved.
6  *
7  * This software was developed by Semihalf under sponsorship from
8  * the FreeBSD Foundation.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
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.
18  *
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
29  * SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34 
35 #include <sys/param.h>
36 #include <sys/kernel.h>
37 #include <sys/malloc.h>
38 #include <sys/systm.h>
39 
40 #include <contrib/libfdt/libfdt.h>
41 
42 #include <machine/stdarg.h>
43 
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>
48 
49 #include "ofw_if.h"
50 
51 #ifdef DEBUG
52 #define debugf(fmt, args...) do { printf("%s(): ", __func__);	\
53     printf(fmt,##args); } while (0)
54 #else
55 #define debugf(fmt, args...)
56 #endif
57 
58 #if defined(__arm__)
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)
63 #define FDT_MARVELL
64 #endif
65 #endif
66 
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 *,
76     size_t);
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 *);
82 
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),
98 	{ 0, 0 }
99 };
100 
101 static ofw_def_t ofw_fdt = {
102 	OFW_FDT,
103 	ofw_fdt_methods,
104 	0
105 };
106 OFW_DEF(ofw_fdt);
107 
108 static void *fdtp = NULL;
109 
110 static int
111 sysctl_handle_dtb(SYSCTL_HANDLER_ARGS)
112 {
113 
114         return (sysctl_handle_opaque(oidp, fdtp, fdt_totalsize(fdtp), req));
115 }
116 
117 static void
118 sysctl_register_fdt_oid(void *arg)
119 {
120 
121 	/* If there is no FDT registered, skip adding the sysctl */
122 	if (fdtp == NULL)
123 		return;
124 
125 	SYSCTL_ADD_PROC(NULL, SYSCTL_STATIC_CHILDREN(_hw_fdt), OID_AUTO, "dtb",
126 	    CTLTYPE_OPAQUE | CTLFLAG_RD, NULL, 0, sysctl_handle_dtb, "",
127 	    "Device Tree Blob");
128 }
129 SYSINIT(dtb_oid, SI_SUB_KMEM, SI_ORDER_ANY, sysctl_register_fdt_oid, 0);
130 
131 static int
132 ofw_fdt_init(ofw_t ofw, void *data)
133 {
134 	int err;
135 
136 	/* Check FDT blob integrity */
137 	if ((err = fdt_check_header(data)) != 0)
138 		return (err);
139 
140 	fdtp = data;
141 	return (0);
142 }
143 
144 /*
145  * Device tree functions.
146  *
147  * We use the offset from fdtp to the node as the 'phandle' in OF interface.
148  *
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.
152  */
153 
154 static phandle_t
155 fdt_offset_phandle(int offset)
156 {
157 	if (offset < 0)
158 		return (0);
159 	return ((phandle_t)offset + fdt_off_dt_struct(fdtp));
160 }
161 
162 static int
163 fdt_phandle_offset(phandle_t p)
164 {
165 	int pint = (int)p;
166 	int dtoff = fdt_off_dt_struct(fdtp);
167 
168 	if (pint < dtoff)
169 		return (-1);
170 	return (pint - dtoff);
171 }
172 
173 /* Return the next sibling of this node or 0. */
174 static phandle_t
175 ofw_fdt_peer(ofw_t ofw, phandle_t node)
176 {
177 	int offset;
178 
179 	if (node == 0) {
180 		/* Find root node */
181 		offset = fdt_path_offset(fdtp, "/");
182 
183 		return (fdt_offset_phandle(offset));
184 	}
185 
186 	offset = fdt_phandle_offset(node);
187 	if (offset < 0)
188 		return (0);
189 	offset = fdt_next_subnode(fdtp, offset);
190 	return (fdt_offset_phandle(offset));
191 }
192 
193 /* Return the first child of this node or 0. */
194 static phandle_t
195 ofw_fdt_child(ofw_t ofw, phandle_t node)
196 {
197 	int offset;
198 
199 	offset = fdt_phandle_offset(node);
200 	if (offset < 0)
201 		return (0);
202 	offset = fdt_first_subnode(fdtp, offset);
203 	return (fdt_offset_phandle(offset));
204 }
205 
206 /* Return the parent of this node or 0. */
207 static phandle_t
208 ofw_fdt_parent(ofw_t ofw, phandle_t node)
209 {
210 	int offset, paroffset;
211 
212 	offset = fdt_phandle_offset(node);
213 	if (offset < 0)
214 		return (0);
215 
216 	paroffset = fdt_parent_offset(fdtp, offset);
217 	return (fdt_offset_phandle(paroffset));
218 }
219 
220 /* Return the package handle that corresponds to an instance handle. */
221 static phandle_t
222 ofw_fdt_instance_to_package(ofw_t ofw, ihandle_t instance)
223 {
224 
225 	/* Where real OF uses ihandles in the tree, FDT uses xref phandles */
226 	return (OF_node_from_xref(instance));
227 }
228 
229 /* Get the length of a property of a package. */
230 static ssize_t
231 ofw_fdt_getproplen(ofw_t ofw, phandle_t package, const char *propname)
232 {
233 	const void *prop;
234 	int offset, len;
235 
236 	offset = fdt_phandle_offset(package);
237 	if (offset < 0)
238 		return (-1);
239 
240 	len = -1;
241 	prop = fdt_getprop(fdtp, offset, propname, &len);
242 
243 	if (prop == NULL && strcmp(propname, "name") == 0) {
244 		/* Emulate the 'name' property */
245 		fdt_get_name(fdtp, offset, &len);
246 		return (len + 1);
247 	}
248 
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));
254 	}
255 
256 	if (prop == NULL)
257 		return (-1);
258 
259 	return (len);
260 }
261 
262 /* Get the value of a property of a package. */
263 static ssize_t
264 ofw_fdt_getprop(ofw_t ofw, phandle_t package, const char *propname, void *buf,
265     size_t buflen)
266 {
267 	const void *prop;
268 	const char *name;
269 	int len, offset;
270 	uint32_t cpuid;
271 
272 	offset = fdt_phandle_offset(package);
273 	if (offset < 0)
274 		return (-1);
275 
276 	prop = fdt_getprop(fdtp, offset, propname, &len);
277 
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);
282 		if (len + 1 > buflen)
283 			len = buflen;
284 		return (len + 1);
285 	}
286 
287 	if (prop == NULL && offset == fdt_path_offset(fdtp, "/chosen")) {
288 		if (strcmp(propname, "fdtbootcpu") == 0) {
289 			cpuid = cpu_to_fdt32(fdt_boot_cpuid_phys(fdtp));
290 			len = sizeof(cpuid);
291 			prop = &cpuid;
292 		}
293 		if (strcmp(propname, "fdtmemreserv") == 0) {
294 			prop = (char *)fdtp + fdt_off_mem_rsvmap(fdtp);
295 			len = sizeof(uint64_t)*2*fdt_num_mem_rsv(fdtp);
296 		}
297 	}
298 
299 	if (prop == NULL)
300 		return (-1);
301 
302 	if (len > buflen)
303 		len = buflen;
304 	bcopy(prop, buf, len);
305 	return (len);
306 }
307 
308 /*
309  * Get the next property of a package. Return values:
310  *  -1: package or previous property does not exist
311  *   0: no more properties
312  *   1: success
313  */
314 static int
315 ofw_fdt_nextprop(ofw_t ofw, phandle_t package, const char *previous, char *buf,
316     size_t size)
317 {
318 	const void *prop;
319 	const char *name;
320 	int offset;
321 
322 	offset = fdt_phandle_offset(package);
323 	if (offset < 0)
324 		return (-1);
325 
326 	if (previous == NULL)
327 		/* Find the first prop in the node */
328 		offset = fdt_first_property_offset(fdtp, offset);
329 	else {
330 		fdt_for_each_property_offset(offset, fdtp, offset) {
331 			prop = fdt_getprop_by_offset(fdtp, offset, &name, NULL);
332 			if (prop == NULL)
333 				return (-1); /* Internal error */
334 			/* Skip until we find 'previous', then bail out */
335 			if (strcmp(name, previous) != 0)
336 				continue;
337 			offset = fdt_next_property_offset(fdtp, offset);
338 			break;
339 		}
340 	}
341 
342 	if (offset < 0)
343 		return (0); /* No properties */
344 
345 	prop = fdt_getprop_by_offset(fdtp, offset, &name, &offset);
346 	if (prop == NULL)
347 		return (-1); /* Internal error */
348 
349 	strncpy(buf, name, size);
350 
351 	return (1);
352 }
353 
354 /* Set the value of a property of a package. */
355 static int
356 ofw_fdt_setprop(ofw_t ofw, phandle_t package, const char *propname,
357     const void *buf, size_t len)
358 {
359 	int offset;
360 
361 	offset = fdt_phandle_offset(package);
362 	if (offset < 0)
363 		return (-1);
364 
365 	if (fdt_setprop_inplace(fdtp, offset, propname, buf, len) != 0)
366 		/* Try to add property, when setting value inplace failed */
367 		return (fdt_setprop(fdtp, offset, propname, buf, len));
368 
369 	return (0);
370 }
371 
372 /* Convert a device specifier to a fully qualified pathname. */
373 static ssize_t
374 ofw_fdt_canon(ofw_t ofw, const char *device, char *buf, size_t len)
375 {
376 
377 	return (-1);
378 }
379 
380 /* Return a package handle for the specified device. */
381 static phandle_t
382 ofw_fdt_finddevice(ofw_t ofw, const char *device)
383 {
384 	int offset;
385 
386 	offset = fdt_path_offset(fdtp, device);
387 	if (offset < 0)
388 		return (-1);
389 	return (fdt_offset_phandle(offset));
390 }
391 
392 /* Return the fully qualified pathname corresponding to an instance. */
393 static ssize_t
394 ofw_fdt_instance_to_path(ofw_t ofw, ihandle_t instance, char *buf, size_t len)
395 {
396 	phandle_t phandle;
397 
398 	phandle = OF_instance_to_package(instance);
399 	if (phandle == -1)
400 		return (-1);
401 
402 	return (OF_package_to_path(phandle, buf, len));
403 }
404 
405 /* Return the fully qualified pathname corresponding to a package. */
406 static ssize_t
407 ofw_fdt_package_to_path(ofw_t ofw, phandle_t package, char *buf, size_t len)
408 {
409 
410 	return (-1);
411 }
412 
413 #if defined(FDT_MARVELL)
414 static int
415 ofw_fdt_fixup(ofw_t ofw)
416 {
417 #define FDT_MODEL_LEN	80
418 	char model[FDT_MODEL_LEN];
419 	phandle_t root;
420 	ssize_t len;
421 	int i;
422 
423 	if ((root = ofw_fdt_finddevice(ofw, "/")) == -1)
424 		return (ENODEV);
425 
426 	if ((len = ofw_fdt_getproplen(ofw, root, "model")) <= 0)
427 		return (0);
428 
429 	bzero(model, FDT_MODEL_LEN);
430 	if (ofw_fdt_getprop(ofw, root, "model", model, FDT_MODEL_LEN) <= 0)
431 		return (0);
432 
433 	/*
434 	 * Search fixup table and call handler if appropriate.
435 	 */
436 	for (i = 0; fdt_fixup_table[i].model != NULL; i++) {
437 		if (strncmp(model, fdt_fixup_table[i].model,
438 		    FDT_MODEL_LEN) != 0)
439 			/*
440 			 * Sometimes it's convenient to provide one
441 			 * fixup entry that refers to many boards.
442 			 * To handle this case, simply check if model
443 			 * is compatible parameter
444 			 */
445 			if(!ofw_bus_node_is_compatible(root,
446 			    fdt_fixup_table[i].model))
447 				continue;
448 
449 		if (fdt_fixup_table[i].handler != NULL)
450 			(*fdt_fixup_table[i].handler)(root);
451 	}
452 
453 	return (0);
454 }
455 #endif
456 
457 static int
458 ofw_fdt_interpret(ofw_t ofw, const char *cmd, int nret, cell_t *retvals)
459 {
460 #if defined(FDT_MARVELL)
461 	int rv;
462 
463 	/*
464 	 * Note: FDT does not have the possibility to 'interpret' commands,
465 	 * but we abuse the interface a bit to use it for doing non-standard
466 	 * operations on the device tree blob.
467 	 *
468 	 * Currently the only supported 'command' is to trigger performing
469 	 * fixups.
470 	 */
471 	if (strncmp("perform-fixup", cmd, 13) != 0)
472 		return (0);
473 
474 	rv = ofw_fdt_fixup(ofw);
475 	if (nret > 0)
476 		retvals[0] = rv;
477 
478 	return (rv);
479 #else
480 	return (0);
481 #endif
482 }
483