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