]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/ofw/ofw_fdt.c
MFV r328323,328324:
[FreeBSD/FreeBSD.git] / sys / dev / ofw / ofw_fdt.c
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 depth, 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
190         for (depth = 1, offset = fdt_next_node(fdtp, offset, &depth);
191             offset >= 0;
192             offset = fdt_next_node(fdtp, offset, &depth)) {
193                 if (depth < 0)
194                         return (0);
195                 if (depth == 1)
196                         return (fdt_offset_phandle(offset));
197         }
198
199         return (0);
200 }
201
202 /* Return the first child of this node or 0. */
203 static phandle_t
204 ofw_fdt_child(ofw_t ofw, phandle_t node)
205 {
206         int depth, offset;
207
208         offset = fdt_phandle_offset(node);
209         if (offset < 0)
210                 return (0);
211
212         for (depth = 0, offset = fdt_next_node(fdtp, offset, &depth);
213             (offset >= 0) && (depth > 0);
214             offset = fdt_next_node(fdtp, offset, &depth)) {
215                 if (depth < 0)
216                         return (0);
217                 if (depth == 1)
218                         return (fdt_offset_phandle(offset));
219         }
220
221         return (0);
222 }
223
224 /* Return the parent of this node or 0. */
225 static phandle_t
226 ofw_fdt_parent(ofw_t ofw, phandle_t node)
227 {
228         int offset, paroffset;
229
230         offset = fdt_phandle_offset(node);
231         if (offset < 0)
232                 return (0);
233
234         paroffset = fdt_parent_offset(fdtp, offset);
235         return (fdt_offset_phandle(paroffset));
236 }
237
238 /* Return the package handle that corresponds to an instance handle. */
239 static phandle_t
240 ofw_fdt_instance_to_package(ofw_t ofw, ihandle_t instance)
241 {
242
243         /* Where real OF uses ihandles in the tree, FDT uses xref phandles */
244         return (OF_node_from_xref(instance));
245 }
246
247 /* Get the length of a property of a package. */
248 static ssize_t
249 ofw_fdt_getproplen(ofw_t ofw, phandle_t package, const char *propname)
250 {
251         const void *prop;
252         int offset, len;
253
254         offset = fdt_phandle_offset(package);
255         if (offset < 0)
256                 return (-1);
257
258         len = -1;
259         prop = fdt_getprop(fdtp, offset, propname, &len);
260
261         if (prop == NULL && strcmp(propname, "name") == 0) {
262                 /* Emulate the 'name' property */
263                 fdt_get_name(fdtp, offset, &len);
264                 return (len + 1);
265         }
266
267         if (prop == NULL && offset == fdt_path_offset(fdtp, "/chosen")) {
268                 if (strcmp(propname, "fdtbootcpu") == 0)
269                         return (sizeof(cell_t));
270                 if (strcmp(propname, "fdtmemreserv") == 0)
271                         return (sizeof(uint64_t)*2*fdt_num_mem_rsv(fdtp));
272         }
273
274         if (prop == NULL)
275                 return (-1);
276
277         return (len);
278 }
279
280 /* Get the value of a property of a package. */
281 static ssize_t
282 ofw_fdt_getprop(ofw_t ofw, phandle_t package, const char *propname, void *buf,
283     size_t buflen)
284 {
285         const void *prop;
286         const char *name;
287         int len, offset;
288         uint32_t cpuid;
289
290         offset = fdt_phandle_offset(package);
291         if (offset < 0)
292                 return (-1);
293
294         prop = fdt_getprop(fdtp, offset, propname, &len);
295
296         if (prop == NULL && strcmp(propname, "name") == 0) {
297                 /* Emulate the 'name' property */
298                 name = fdt_get_name(fdtp, offset, &len);
299                 strncpy(buf, name, buflen);
300                 if (len + 1 > buflen)
301                         len = buflen;
302                 return (len + 1);
303         }
304
305         if (prop == NULL && offset == fdt_path_offset(fdtp, "/chosen")) {
306                 if (strcmp(propname, "fdtbootcpu") == 0) {
307                         cpuid = cpu_to_fdt32(fdt_boot_cpuid_phys(fdtp));
308                         len = sizeof(cpuid);
309                         prop = &cpuid;
310                 }
311                 if (strcmp(propname, "fdtmemreserv") == 0) {
312                         prop = (char *)fdtp + fdt_off_mem_rsvmap(fdtp);
313                         len = sizeof(uint64_t)*2*fdt_num_mem_rsv(fdtp);
314                 }
315         }
316
317         if (prop == NULL)
318                 return (-1);
319
320         if (len > buflen)
321                 len = buflen;
322         bcopy(prop, buf, len);
323         return (len);
324 }
325
326 /*
327  * Get the next property of a package. Return values:
328  *  -1: package or previous property does not exist
329  *   0: no more properties
330  *   1: success
331  */
332 static int
333 ofw_fdt_nextprop(ofw_t ofw, phandle_t package, const char *previous, char *buf,
334     size_t size)
335 {
336         const void *prop;
337         const char *name;
338         int offset;
339
340         offset = fdt_phandle_offset(package);
341         if (offset < 0)
342                 return (-1);
343
344         /* Find the first prop in the node */
345         offset = fdt_first_property_offset(fdtp, offset);
346         if (offset < 0)
347                 return (0); /* No properties */
348
349         if (previous != NULL) {
350                 while (offset >= 0) {
351                         prop = fdt_getprop_by_offset(fdtp, offset, &name, NULL);
352                         if (prop == NULL)
353                                 return (-1); /* Internal error */
354
355                         offset = fdt_next_property_offset(fdtp, offset);
356                         if (offset < 0)
357                                 return (0); /* No more properties */
358
359                         /* Check if the last one was the one we wanted */
360                         if (strcmp(name, previous) == 0)
361                                 break;
362                 }
363         }
364
365         prop = fdt_getprop_by_offset(fdtp, offset, &name, &offset);
366         if (prop == NULL)
367                 return (-1); /* Internal error */
368
369         strncpy(buf, name, size);
370
371         return (1);
372 }
373
374 /* Set the value of a property of a package. */
375 static int
376 ofw_fdt_setprop(ofw_t ofw, phandle_t package, const char *propname,
377     const void *buf, size_t len)
378 {
379         int offset;
380
381         offset = fdt_phandle_offset(package);
382         if (offset < 0)
383                 return (-1);
384
385         if (fdt_setprop_inplace(fdtp, offset, propname, buf, len) != 0)
386                 /* Try to add property, when setting value inplace failed */
387                 return (fdt_setprop(fdtp, offset, propname, buf, len));
388
389         return (0);
390 }
391
392 /* Convert a device specifier to a fully qualified pathname. */
393 static ssize_t
394 ofw_fdt_canon(ofw_t ofw, const char *device, char *buf, size_t len)
395 {
396
397         return (-1);
398 }
399
400 /* Return a package handle for the specified device. */
401 static phandle_t
402 ofw_fdt_finddevice(ofw_t ofw, const char *device)
403 {
404         int offset;
405
406         offset = fdt_path_offset(fdtp, device);
407         if (offset < 0)
408                 return (-1);
409         return (fdt_offset_phandle(offset));
410 }
411
412 /* Return the fully qualified pathname corresponding to an instance. */
413 static ssize_t
414 ofw_fdt_instance_to_path(ofw_t ofw, ihandle_t instance, char *buf, size_t len)
415 {
416         phandle_t phandle;
417
418         phandle = OF_instance_to_package(instance);
419         if (phandle == -1)
420                 return (-1);
421
422         return (OF_package_to_path(phandle, buf, len));
423 }
424
425 /* Return the fully qualified pathname corresponding to a package. */
426 static ssize_t
427 ofw_fdt_package_to_path(ofw_t ofw, phandle_t package, char *buf, size_t len)
428 {
429
430         return (-1);
431 }
432
433 #if defined(FDT_MARVELL)
434 static int
435 ofw_fdt_fixup(ofw_t ofw)
436 {
437 #define FDT_MODEL_LEN   80
438         char model[FDT_MODEL_LEN];
439         phandle_t root;
440         ssize_t len;
441         int i;
442
443         if ((root = ofw_fdt_finddevice(ofw, "/")) == -1)
444                 return (ENODEV);
445
446         if ((len = ofw_fdt_getproplen(ofw, root, "model")) <= 0)
447                 return (0);
448
449         bzero(model, FDT_MODEL_LEN);
450         if (ofw_fdt_getprop(ofw, root, "model", model, FDT_MODEL_LEN) <= 0)
451                 return (0);
452
453         /*
454          * Search fixup table and call handler if appropriate.
455          */
456         for (i = 0; fdt_fixup_table[i].model != NULL; i++) {
457                 if (strncmp(model, fdt_fixup_table[i].model,
458                     FDT_MODEL_LEN) != 0)
459                         /*
460                          * Sometimes it's convenient to provide one
461                          * fixup entry that refers to many boards.
462                          * To handle this case, simply check if model
463                          * is compatible parameter
464                          */
465                         if(!ofw_bus_node_is_compatible(root,
466                             fdt_fixup_table[i].model))
467                                 continue;
468
469                 if (fdt_fixup_table[i].handler != NULL)
470                         (*fdt_fixup_table[i].handler)(root);
471         }
472
473         return (0);
474 }
475 #endif
476
477 static int
478 ofw_fdt_interpret(ofw_t ofw, const char *cmd, int nret, cell_t *retvals)
479 {
480 #if defined(FDT_MARVELL)
481         int rv;
482
483         /*
484          * Note: FDT does not have the possibility to 'interpret' commands,
485          * but we abuse the interface a bit to use it for doing non-standard
486          * operations on the device tree blob.
487          *
488          * Currently the only supported 'command' is to trigger performing
489          * fixups.
490          */
491         if (strncmp("perform-fixup", cmd, 13) != 0)
492                 return (0);
493
494         rv = ofw_fdt_fixup(ofw);
495         if (nret > 0)
496                 retvals[0] = rv;
497
498         return (rv);
499 #else
500         return (0);
501 #endif
502 }