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
67static int ofw_fdt_init(ofw_t, void *);
68static phandle_t ofw_fdt_peer(ofw_t, phandle_t);
69static phandle_t ofw_fdt_child(ofw_t, phandle_t);
70static phandle_t ofw_fdt_parent(ofw_t, phandle_t);
71static phandle_t ofw_fdt_instance_to_package(ofw_t, ihandle_t);
72static ssize_t ofw_fdt_getproplen(ofw_t, phandle_t, const char *);
73static ssize_t ofw_fdt_getprop(ofw_t, phandle_t, const char *, void *, size_t);
74static int ofw_fdt_nextprop(ofw_t, phandle_t, const char *, char *, size_t);
75static int ofw_fdt_setprop(ofw_t, phandle_t, const char *, const void *,
76    size_t);
77static ssize_t ofw_fdt_canon(ofw_t, const char *, char *, size_t);
78static phandle_t ofw_fdt_finddevice(ofw_t, const char *);
79static ssize_t ofw_fdt_instance_to_path(ofw_t, ihandle_t, char *, size_t);
80static ssize_t ofw_fdt_package_to_path(ofw_t, phandle_t, char *, size_t);
81static int ofw_fdt_interpret(ofw_t, const char *, int, cell_t *);
82
83static 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
101static ofw_def_t ofw_fdt = {
102	OFW_FDT,
103	ofw_fdt_methods,
104	0
105};
106OFW_DEF(ofw_fdt);
107
108static void *fdtp = NULL;
109
110static int
111sysctl_handle_dtb(SYSCTL_HANDLER_ARGS)
112{
113
114        return (sysctl_handle_opaque(oidp, fdtp, fdt_totalsize(fdtp), req));
115}
116
117static void
118sysctl_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}
129SYSINIT(dtb_oid, SI_SUB_KMEM, SI_ORDER_ANY, sysctl_register_fdt_oid, NULL);
130
131static int
132ofw_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
154static phandle_t
155fdt_offset_phandle(int offset)
156{
157	if (offset < 0)
158		return (0);
159	return ((phandle_t)offset + fdt_off_dt_struct(fdtp));
160}
161
162static int
163fdt_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. */
174static phandle_t
175ofw_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. */
194static phandle_t
195ofw_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. */
207static phandle_t
208ofw_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. */
221static phandle_t
222ofw_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. */
230static ssize_t
231ofw_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. */
263static ssize_t
264ofw_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		return (len + 1);
283	}
284
285	if (prop == NULL && offset == fdt_path_offset(fdtp, "/chosen")) {
286		if (strcmp(propname, "fdtbootcpu") == 0) {
287			cpuid = cpu_to_fdt32(fdt_boot_cpuid_phys(fdtp));
288			len = sizeof(cpuid);
289			prop = &cpuid;
290		}
291		if (strcmp(propname, "fdtmemreserv") == 0) {
292			prop = (char *)fdtp + fdt_off_mem_rsvmap(fdtp);
293			len = sizeof(uint64_t)*2*fdt_num_mem_rsv(fdtp);
294		}
295	}
296
297	if (prop == NULL)
298		return (-1);
299
300	bcopy(prop, buf, min(len, buflen));
301
302	return (len);
303}
304
305/*
306 * Get the next property of a package. Return values:
307 *  -1: package or previous property does not exist
308 *   0: no more properties
309 *   1: success
310 */
311static int
312ofw_fdt_nextprop(ofw_t ofw, phandle_t package, const char *previous, char *buf,
313    size_t size)
314{
315	const void *prop;
316	const char *name;
317	int offset;
318
319	offset = fdt_phandle_offset(package);
320	if (offset < 0)
321		return (-1);
322
323	if (previous == NULL)
324		/* Find the first prop in the node */
325		offset = fdt_first_property_offset(fdtp, offset);
326	else {
327		fdt_for_each_property_offset(offset, fdtp, offset) {
328			prop = fdt_getprop_by_offset(fdtp, offset, &name, NULL);
329			if (prop == NULL)
330				return (-1); /* Internal error */
331			/* Skip until we find 'previous', then bail out */
332			if (strcmp(name, previous) != 0)
333				continue;
334			offset = fdt_next_property_offset(fdtp, offset);
335			break;
336		}
337	}
338
339	if (offset < 0)
340		return (0); /* No properties */
341
342	prop = fdt_getprop_by_offset(fdtp, offset, &name, &offset);
343	if (prop == NULL)
344		return (-1); /* Internal error */
345
346	strncpy(buf, name, size);
347
348	return (1);
349}
350
351/* Set the value of a property of a package. */
352static int
353ofw_fdt_setprop(ofw_t ofw, phandle_t package, const char *propname,
354    const void *buf, size_t len)
355{
356	int offset;
357
358	offset = fdt_phandle_offset(package);
359	if (offset < 0)
360		return (-1);
361
362	if (fdt_setprop_inplace(fdtp, offset, propname, buf, len) != 0)
363		/* Try to add property, when setting value inplace failed */
364		return (fdt_setprop(fdtp, offset, propname, buf, len));
365
366	return (0);
367}
368
369/* Convert a device specifier to a fully qualified pathname. */
370static ssize_t
371ofw_fdt_canon(ofw_t ofw, const char *device, char *buf, size_t len)
372{
373
374	return (-1);
375}
376
377/* Return a package handle for the specified device. */
378static phandle_t
379ofw_fdt_finddevice(ofw_t ofw, const char *device)
380{
381	int offset;
382
383	offset = fdt_path_offset(fdtp, device);
384	if (offset < 0)
385		return (-1);
386	return (fdt_offset_phandle(offset));
387}
388
389/* Return the fully qualified pathname corresponding to an instance. */
390static ssize_t
391ofw_fdt_instance_to_path(ofw_t ofw, ihandle_t instance, char *buf, size_t len)
392{
393	phandle_t phandle;
394
395	phandle = OF_instance_to_package(instance);
396	if (phandle == -1)
397		return (-1);
398
399	return (OF_package_to_path(phandle, buf, len));
400}
401
402/* Return the fully qualified pathname corresponding to a package. */
403static ssize_t
404ofw_fdt_package_to_path(ofw_t ofw, phandle_t package, char *buf, size_t len)
405{
406
407	return (-1);
408}
409
410#if defined(FDT_MARVELL)
411static int
412ofw_fdt_fixup(ofw_t ofw)
413{
414#define FDT_MODEL_LEN	80
415	char model[FDT_MODEL_LEN];
416	phandle_t root;
417	ssize_t len;
418	int i;
419
420	if ((root = ofw_fdt_finddevice(ofw, "/")) == -1)
421		return (ENODEV);
422
423	if ((len = ofw_fdt_getproplen(ofw, root, "model")) <= 0)
424		return (0);
425
426	bzero(model, FDT_MODEL_LEN);
427	if (ofw_fdt_getprop(ofw, root, "model", model, FDT_MODEL_LEN) <= 0)
428		return (0);
429
430	/*
431	 * Search fixup table and call handler if appropriate.
432	 */
433	for (i = 0; fdt_fixup_table[i].model != NULL; i++) {
434		if (strncmp(model, fdt_fixup_table[i].model,
435		    FDT_MODEL_LEN) != 0)
436			/*
437			 * Sometimes it's convenient to provide one
438			 * fixup entry that refers to many boards.
439			 * To handle this case, simply check if model
440			 * is compatible parameter
441			 */
442			if(!ofw_bus_node_is_compatible(root,
443			    fdt_fixup_table[i].model))
444				continue;
445
446		if (fdt_fixup_table[i].handler != NULL)
447			(*fdt_fixup_table[i].handler)(root);
448	}
449
450	return (0);
451}
452#endif
453
454static int
455ofw_fdt_interpret(ofw_t ofw, const char *cmd, int nret, cell_t *retvals)
456{
457#if defined(FDT_MARVELL)
458	int rv;
459
460	/*
461	 * Note: FDT does not have the possibility to 'interpret' commands,
462	 * but we abuse the interface a bit to use it for doing non-standard
463	 * operations on the device tree blob.
464	 *
465	 * Currently the only supported 'command' is to trigger performing
466	 * fixups.
467	 */
468	if (strncmp("perform-fixup", cmd, 13) != 0)
469		return (0);
470
471	rv = ofw_fdt_fixup(ofw);
472	if (nret > 0)
473		retvals[0] = rv;
474
475	return (rv);
476#else
477	return (0);
478#endif
479}
480