]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/ofw/ofw_fdt.c
Update releng/11.3 to RC3 as part of the 11.3-RELEASE cycle.
[FreeBSD/FreeBSD.git] / sys / dev / ofw / ofw_fdt.c
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 ofw_fdt_init(ofw_t ofw, void *data)
110 {
111         int err;
112
113         /* Check FDT blob integrity */
114         if ((err = fdt_check_header(data)) != 0)
115                 return (err);
116
117         fdtp = data;
118         return (0);
119 }
120
121 /*
122  * Device tree functions.
123  *
124  * We use the offset from fdtp to the node as the 'phandle' in OF interface.
125  *
126  * phandle is a u32 value, therefore we cannot use the pointer to node as
127  * phandle in 64 bit. We also do not use the usual fdt offset as phandle,
128  * as it can be 0, and the OF interface has special meaning for phandle 0.
129  */
130
131 static phandle_t
132 fdt_offset_phandle(int offset)
133 {
134         if (offset < 0)
135                 return (0);
136         return ((phandle_t)offset + fdt_off_dt_struct(fdtp));
137 }
138
139 static int
140 fdt_phandle_offset(phandle_t p)
141 {
142         int pint = (int)p;
143         int dtoff = fdt_off_dt_struct(fdtp);
144
145         if (pint < dtoff)
146                 return (-1);
147         return (pint - dtoff);
148 }
149
150 /* Return the next sibling of this node or 0. */
151 static phandle_t
152 ofw_fdt_peer(ofw_t ofw, phandle_t node)
153 {
154         int offset;
155
156         if (node == 0) {
157                 /* Find root node */
158                 offset = fdt_path_offset(fdtp, "/");
159
160                 return (fdt_offset_phandle(offset));
161         }
162
163         offset = fdt_phandle_offset(node);
164         if (offset < 0)
165                 return (0);
166         offset = fdt_next_subnode(fdtp, offset);
167         return (fdt_offset_phandle(offset));
168 }
169
170 /* Return the first child of this node or 0. */
171 static phandle_t
172 ofw_fdt_child(ofw_t ofw, phandle_t node)
173 {
174         int offset;
175
176         offset = fdt_phandle_offset(node);
177         if (offset < 0)
178                 return (0);
179         offset = fdt_first_subnode(fdtp, offset);
180         return (fdt_offset_phandle(offset));
181 }
182
183 /* Return the parent of this node or 0. */
184 static phandle_t
185 ofw_fdt_parent(ofw_t ofw, phandle_t node)
186 {
187         int offset, paroffset;
188
189         offset = fdt_phandle_offset(node);
190         if (offset < 0)
191                 return (0);
192
193         paroffset = fdt_parent_offset(fdtp, offset);
194         return (fdt_offset_phandle(paroffset));
195 }
196
197 /* Return the package handle that corresponds to an instance handle. */
198 static phandle_t
199 ofw_fdt_instance_to_package(ofw_t ofw, ihandle_t instance)
200 {
201
202         /* Where real OF uses ihandles in the tree, FDT uses xref phandles */
203         return (OF_node_from_xref(instance));
204 }
205
206 /* Get the length of a property of a package. */
207 static ssize_t
208 ofw_fdt_getproplen(ofw_t ofw, phandle_t package, const char *propname)
209 {
210         const void *prop;
211         int offset, len;
212
213         offset = fdt_phandle_offset(package);
214         if (offset < 0)
215                 return (-1);
216
217         len = -1;
218         prop = fdt_getprop(fdtp, offset, propname, &len);
219
220         if (prop == NULL && strcmp(propname, "name") == 0) {
221                 /* Emulate the 'name' property */
222                 fdt_get_name(fdtp, offset, &len);
223                 return (len + 1);
224         }
225
226         if (prop == NULL && offset == fdt_path_offset(fdtp, "/chosen")) {
227                 if (strcmp(propname, "fdtbootcpu") == 0)
228                         return (sizeof(cell_t));
229                 if (strcmp(propname, "fdtmemreserv") == 0)
230                         return (sizeof(uint64_t)*2*fdt_num_mem_rsv(fdtp));
231         }
232
233         if (prop == NULL)
234                 return (-1);
235
236         return (len);
237 }
238
239 /* Get the value of a property of a package. */
240 static ssize_t
241 ofw_fdt_getprop(ofw_t ofw, phandle_t package, const char *propname, void *buf,
242     size_t buflen)
243 {
244         const void *prop;
245         const char *name;
246         int len, offset;
247         uint32_t cpuid;
248
249         offset = fdt_phandle_offset(package);
250         if (offset < 0)
251                 return (-1);
252
253         prop = fdt_getprop(fdtp, offset, propname, &len);
254
255         if (prop == NULL && strcmp(propname, "name") == 0) {
256                 /* Emulate the 'name' property */
257                 name = fdt_get_name(fdtp, offset, &len);
258                 strncpy(buf, name, buflen);
259                 if (len + 1 > buflen)
260                         len = buflen;
261                 return (len + 1);
262         }
263
264         if (prop == NULL && offset == fdt_path_offset(fdtp, "/chosen")) {
265                 if (strcmp(propname, "fdtbootcpu") == 0) {
266                         cpuid = cpu_to_fdt32(fdt_boot_cpuid_phys(fdtp));
267                         len = sizeof(cpuid);
268                         prop = &cpuid;
269                 }
270                 if (strcmp(propname, "fdtmemreserv") == 0) {
271                         prop = (char *)fdtp + fdt_off_mem_rsvmap(fdtp);
272                         len = sizeof(uint64_t)*2*fdt_num_mem_rsv(fdtp);
273                 }
274         }
275
276         if (prop == NULL)
277                 return (-1);
278
279         if (len > buflen)
280                 len = buflen;
281         bcopy(prop, buf, len);
282         return (len);
283 }
284
285 /*
286  * Get the next property of a package. Return values:
287  *  -1: package or previous property does not exist
288  *   0: no more properties
289  *   1: success
290  */
291 static int
292 ofw_fdt_nextprop(ofw_t ofw, phandle_t package, const char *previous, char *buf,
293     size_t size)
294 {
295         const void *prop;
296         const char *name;
297         int offset;
298
299         offset = fdt_phandle_offset(package);
300         if (offset < 0)
301                 return (-1);
302
303         if (previous == NULL)
304                 /* Find the first prop in the node */
305                 offset = fdt_first_property_offset(fdtp, offset);
306         else {
307                 fdt_for_each_property_offset(offset, fdtp, offset) {
308                         prop = fdt_getprop_by_offset(fdtp, offset, &name, NULL);
309                         if (prop == NULL)
310                                 return (-1); /* Internal error */
311                         /* Skip until we find 'previous', then bail out */
312                         if (strcmp(name, previous) != 0)
313                                 continue;
314                         offset = fdt_next_property_offset(fdtp, offset);
315                         break;
316                 }
317         }
318
319         if (offset < 0)
320                 return (0); /* No properties */
321
322         prop = fdt_getprop_by_offset(fdtp, offset, &name, &offset);
323         if (prop == NULL)
324                 return (-1); /* Internal error */
325
326         strncpy(buf, name, size);
327
328         return (1);
329 }
330
331 /* Set the value of a property of a package. */
332 static int
333 ofw_fdt_setprop(ofw_t ofw, phandle_t package, const char *propname,
334     const void *buf, size_t len)
335 {
336         int offset;
337
338         offset = fdt_phandle_offset(package);
339         if (offset < 0)
340                 return (-1);
341
342         if (fdt_setprop_inplace(fdtp, offset, propname, buf, len) != 0)
343                 /* Try to add property, when setting value inplace failed */
344                 return (fdt_setprop(fdtp, offset, propname, buf, len));
345
346         return (0);
347 }
348
349 /* Convert a device specifier to a fully qualified pathname. */
350 static ssize_t
351 ofw_fdt_canon(ofw_t ofw, const char *device, char *buf, size_t len)
352 {
353
354         return (-1);
355 }
356
357 /* Return a package handle for the specified device. */
358 static phandle_t
359 ofw_fdt_finddevice(ofw_t ofw, const char *device)
360 {
361         int offset;
362
363         offset = fdt_path_offset(fdtp, device);
364         if (offset < 0)
365                 return (-1);
366         return (fdt_offset_phandle(offset));
367 }
368
369 /* Return the fully qualified pathname corresponding to an instance. */
370 static ssize_t
371 ofw_fdt_instance_to_path(ofw_t ofw, ihandle_t instance, char *buf, size_t len)
372 {
373         phandle_t phandle;
374
375         phandle = OF_instance_to_package(instance);
376         if (phandle == -1)
377                 return (-1);
378
379         return (OF_package_to_path(phandle, buf, len));
380 }
381
382 /* Return the fully qualified pathname corresponding to a package. */
383 static ssize_t
384 ofw_fdt_package_to_path(ofw_t ofw, phandle_t package, char *buf, size_t len)
385 {
386
387         return (-1);
388 }
389
390 #if defined(FDT_MARVELL) || defined(__powerpc__)
391 static int
392 ofw_fdt_fixup(ofw_t ofw)
393 {
394 #define FDT_MODEL_LEN   80
395         char model[FDT_MODEL_LEN];
396         phandle_t root;
397         ssize_t len;
398         int i;
399
400         if ((root = ofw_fdt_finddevice(ofw, "/")) == -1)
401                 return (ENODEV);
402
403         if ((len = ofw_fdt_getproplen(ofw, root, "model")) <= 0)
404                 return (0);
405
406         bzero(model, FDT_MODEL_LEN);
407         if (ofw_fdt_getprop(ofw, root, "model", model, FDT_MODEL_LEN) <= 0)
408                 return (0);
409
410         /*
411          * Search fixup table and call handler if appropriate.
412          */
413         for (i = 0; fdt_fixup_table[i].model != NULL; i++) {
414                 if (strncmp(model, fdt_fixup_table[i].model,
415                     FDT_MODEL_LEN) != 0)
416                         /*
417                          * Sometimes it's convenient to provide one
418                          * fixup entry that refers to many boards.
419                          * To handle this case, simply check if model
420                          * is compatible parameter
421                          */
422                         if(!ofw_bus_node_is_compatible(root,
423                             fdt_fixup_table[i].model))
424                                 continue;
425
426                 if (fdt_fixup_table[i].handler != NULL)
427                         (*fdt_fixup_table[i].handler)(root);
428         }
429
430         return (0);
431 }
432 #endif
433
434 static int
435 ofw_fdt_interpret(ofw_t ofw, const char *cmd, int nret, cell_t *retvals)
436 {
437 #if defined(FDT_MARVELL) || defined(__powerpc__)
438         int rv;
439
440         /*
441          * Note: FDT does not have the possibility to 'interpret' commands,
442          * but we abuse the interface a bit to use it for doing non-standard
443          * operations on the device tree blob.
444          *
445          * Currently the only supported 'command' is to trigger performing
446          * fixups.
447          */
448         if (strncmp("perform-fixup", cmd, 13) != 0)
449                 return (0);
450
451         rv = ofw_fdt_fixup(ofw);
452         if (nret > 0)
453                 retvals[0] = rv;
454
455         return (rv);
456 #else
457         return (0);
458 #endif
459 }