]> Pileus Git - ~andy/linux/blob - drivers/of/fdt.c
of: assume big-endian properties, adding conversions where necessary
[~andy/linux] / drivers / of / fdt.c
1 /*
2  * Functions for working with the Flattened Device Tree data format
3  *
4  * Copyright 2009 Benjamin Herrenschmidt, IBM Corp
5  * benh@kernel.crashing.org
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * version 2 as published by the Free Software Foundation.
10  */
11
12 #include <linux/kernel.h>
13 #include <linux/lmb.h>
14 #include <linux/initrd.h>
15 #include <linux/of.h>
16 #include <linux/of_fdt.h>
17
18
19 #ifdef CONFIG_PPC
20 #include <asm/machdep.h>
21 #endif /* CONFIG_PPC */
22
23 int __initdata dt_root_addr_cells;
24 int __initdata dt_root_size_cells;
25
26 struct boot_param_header *initial_boot_params;
27
28 char *find_flat_dt_string(u32 offset)
29 {
30         return ((char *)initial_boot_params) +
31                 initial_boot_params->off_dt_strings + offset;
32 }
33
34 /**
35  * of_scan_flat_dt - scan flattened tree blob and call callback on each.
36  * @it: callback function
37  * @data: context data pointer
38  *
39  * This function is used to scan the flattened device-tree, it is
40  * used to extract the memory information at boot before we can
41  * unflatten the tree
42  */
43 int __init of_scan_flat_dt(int (*it)(unsigned long node,
44                                      const char *uname, int depth,
45                                      void *data),
46                            void *data)
47 {
48         unsigned long p = ((unsigned long)initial_boot_params) +
49                 initial_boot_params->off_dt_struct;
50         int rc = 0;
51         int depth = -1;
52
53         do {
54                 u32 tag = be32_to_cpup((__be32 *)p);
55                 char *pathp;
56
57                 p += 4;
58                 if (tag == OF_DT_END_NODE) {
59                         depth--;
60                         continue;
61                 }
62                 if (tag == OF_DT_NOP)
63                         continue;
64                 if (tag == OF_DT_END)
65                         break;
66                 if (tag == OF_DT_PROP) {
67                         u32 sz = be32_to_cpup((__be32 *)p);
68                         p += 8;
69                         if (initial_boot_params->version < 0x10)
70                                 p = _ALIGN(p, sz >= 8 ? 8 : 4);
71                         p += sz;
72                         p = _ALIGN(p, 4);
73                         continue;
74                 }
75                 if (tag != OF_DT_BEGIN_NODE) {
76                         pr_err("Invalid tag %x in flat device tree!\n", tag);
77                         return -EINVAL;
78                 }
79                 depth++;
80                 pathp = (char *)p;
81                 p = _ALIGN(p + strlen(pathp) + 1, 4);
82                 if ((*pathp) == '/') {
83                         char *lp, *np;
84                         for (lp = NULL, np = pathp; *np; np++)
85                                 if ((*np) == '/')
86                                         lp = np+1;
87                         if (lp != NULL)
88                                 pathp = lp;
89                 }
90                 rc = it(p, pathp, depth, data);
91                 if (rc != 0)
92                         break;
93         } while (1);
94
95         return rc;
96 }
97
98 /**
99  * of_get_flat_dt_root - find the root node in the flat blob
100  */
101 unsigned long __init of_get_flat_dt_root(void)
102 {
103         unsigned long p = ((unsigned long)initial_boot_params) +
104                 initial_boot_params->off_dt_struct;
105
106         while (be32_to_cpup((__be32 *)p) == OF_DT_NOP)
107                 p += 4;
108         BUG_ON(be32_to_cpup((__be32 *)p) != OF_DT_BEGIN_NODE);
109         p += 4;
110         return _ALIGN(p + strlen((char *)p) + 1, 4);
111 }
112
113 /**
114  * of_get_flat_dt_prop - Given a node in the flat blob, return the property ptr
115  *
116  * This function can be used within scan_flattened_dt callback to get
117  * access to properties
118  */
119 void *__init of_get_flat_dt_prop(unsigned long node, const char *name,
120                                  unsigned long *size)
121 {
122         unsigned long p = node;
123
124         do {
125                 u32 tag = be32_to_cpup((__be32 *)p);
126                 u32 sz, noff;
127                 const char *nstr;
128
129                 p += 4;
130                 if (tag == OF_DT_NOP)
131                         continue;
132                 if (tag != OF_DT_PROP)
133                         return NULL;
134
135                 sz = be32_to_cpup((__be32 *)p);
136                 noff = be32_to_cpup((__be32 *)(p + 4));
137                 p += 8;
138                 if (initial_boot_params->version < 0x10)
139                         p = _ALIGN(p, sz >= 8 ? 8 : 4);
140
141                 nstr = find_flat_dt_string(noff);
142                 if (nstr == NULL) {
143                         pr_warning("Can't find property index name !\n");
144                         return NULL;
145                 }
146                 if (strcmp(name, nstr) == 0) {
147                         if (size)
148                                 *size = sz;
149                         return (void *)p;
150                 }
151                 p += sz;
152                 p = _ALIGN(p, 4);
153         } while (1);
154 }
155
156 /**
157  * of_flat_dt_is_compatible - Return true if given node has compat in compatible list
158  * @node: node to test
159  * @compat: compatible string to compare with compatible list.
160  */
161 int __init of_flat_dt_is_compatible(unsigned long node, const char *compat)
162 {
163         const char *cp;
164         unsigned long cplen, l;
165
166         cp = of_get_flat_dt_prop(node, "compatible", &cplen);
167         if (cp == NULL)
168                 return 0;
169         while (cplen > 0) {
170                 if (strncasecmp(cp, compat, strlen(compat)) == 0)
171                         return 1;
172                 l = strlen(cp) + 1;
173                 cp += l;
174                 cplen -= l;
175         }
176
177         return 0;
178 }
179
180 static void *__init unflatten_dt_alloc(unsigned long *mem, unsigned long size,
181                                        unsigned long align)
182 {
183         void *res;
184
185         *mem = _ALIGN(*mem, align);
186         res = (void *)*mem;
187         *mem += size;
188
189         return res;
190 }
191
192 /**
193  * unflatten_dt_node - Alloc and populate a device_node from the flat tree
194  * @p: pointer to node in flat tree
195  * @dad: Parent struct device_node
196  * @allnextpp: pointer to ->allnext from last allocated device_node
197  * @fpsize: Size of the node path up at the current depth.
198  */
199 unsigned long __init unflatten_dt_node(unsigned long mem,
200                                         unsigned long *p,
201                                         struct device_node *dad,
202                                         struct device_node ***allnextpp,
203                                         unsigned long fpsize)
204 {
205         struct device_node *np;
206         struct property *pp, **prev_pp = NULL;
207         char *pathp;
208         u32 tag;
209         unsigned int l, allocl;
210         int has_name = 0;
211         int new_format = 0;
212
213         tag = be32_to_cpup((__be32 *)(*p));
214         if (tag != OF_DT_BEGIN_NODE) {
215                 pr_err("Weird tag at start of node: %x\n", tag);
216                 return mem;
217         }
218         *p += 4;
219         pathp = (char *)*p;
220         l = allocl = strlen(pathp) + 1;
221         *p = _ALIGN(*p + l, 4);
222
223         /* version 0x10 has a more compact unit name here instead of the full
224          * path. we accumulate the full path size using "fpsize", we'll rebuild
225          * it later. We detect this because the first character of the name is
226          * not '/'.
227          */
228         if ((*pathp) != '/') {
229                 new_format = 1;
230                 if (fpsize == 0) {
231                         /* root node: special case. fpsize accounts for path
232                          * plus terminating zero. root node only has '/', so
233                          * fpsize should be 2, but we want to avoid the first
234                          * level nodes to have two '/' so we use fpsize 1 here
235                          */
236                         fpsize = 1;
237                         allocl = 2;
238                 } else {
239                         /* account for '/' and path size minus terminal 0
240                          * already in 'l'
241                          */
242                         fpsize += l;
243                         allocl = fpsize;
244                 }
245         }
246
247         np = unflatten_dt_alloc(&mem, sizeof(struct device_node) + allocl,
248                                 __alignof__(struct device_node));
249         if (allnextpp) {
250                 memset(np, 0, sizeof(*np));
251                 np->full_name = ((char *)np) + sizeof(struct device_node);
252                 if (new_format) {
253                         char *fn = np->full_name;
254                         /* rebuild full path for new format */
255                         if (dad && dad->parent) {
256                                 strcpy(fn, dad->full_name);
257 #ifdef DEBUG
258                                 if ((strlen(fn) + l + 1) != allocl) {
259                                         pr_debug("%s: p: %d, l: %d, a: %d\n",
260                                                 pathp, (int)strlen(fn),
261                                                 l, allocl);
262                                 }
263 #endif
264                                 fn += strlen(fn);
265                         }
266                         *(fn++) = '/';
267                         memcpy(fn, pathp, l);
268                 } else
269                         memcpy(np->full_name, pathp, l);
270                 prev_pp = &np->properties;
271                 **allnextpp = np;
272                 *allnextpp = &np->allnext;
273                 if (dad != NULL) {
274                         np->parent = dad;
275                         /* we temporarily use the next field as `last_child'*/
276                         if (dad->next == NULL)
277                                 dad->child = np;
278                         else
279                                 dad->next->sibling = np;
280                         dad->next = np;
281                 }
282                 kref_init(&np->kref);
283         }
284         while (1) {
285                 u32 sz, noff;
286                 char *pname;
287
288                 tag = be32_to_cpup((__be32 *)(*p));
289                 if (tag == OF_DT_NOP) {
290                         *p += 4;
291                         continue;
292                 }
293                 if (tag != OF_DT_PROP)
294                         break;
295                 *p += 4;
296                 sz = be32_to_cpup((__be32 *)(*p));
297                 noff = be32_to_cpup((__be32 *)((*p) + 4));
298                 *p += 8;
299                 if (initial_boot_params->version < 0x10)
300                         *p = _ALIGN(*p, sz >= 8 ? 8 : 4);
301
302                 pname = find_flat_dt_string(noff);
303                 if (pname == NULL) {
304                         pr_info("Can't find property name in list !\n");
305                         break;
306                 }
307                 if (strcmp(pname, "name") == 0)
308                         has_name = 1;
309                 l = strlen(pname) + 1;
310                 pp = unflatten_dt_alloc(&mem, sizeof(struct property),
311                                         __alignof__(struct property));
312                 if (allnextpp) {
313                         if (strcmp(pname, "linux,phandle") == 0) {
314                                 if (np->phandle == 0)
315                                         np->phandle = *((u32 *)*p);
316                         }
317                         if (strcmp(pname, "ibm,phandle") == 0)
318                                 np->phandle = *((u32 *)*p);
319                         pp->name = pname;
320                         pp->length = sz;
321                         pp->value = (void *)*p;
322                         *prev_pp = pp;
323                         prev_pp = &pp->next;
324                 }
325                 *p = _ALIGN((*p) + sz, 4);
326         }
327         /* with version 0x10 we may not have the name property, recreate
328          * it here from the unit name if absent
329          */
330         if (!has_name) {
331                 char *p1 = pathp, *ps = pathp, *pa = NULL;
332                 int sz;
333
334                 while (*p1) {
335                         if ((*p1) == '@')
336                                 pa = p1;
337                         if ((*p1) == '/')
338                                 ps = p1 + 1;
339                         p1++;
340                 }
341                 if (pa < ps)
342                         pa = p1;
343                 sz = (pa - ps) + 1;
344                 pp = unflatten_dt_alloc(&mem, sizeof(struct property) + sz,
345                                         __alignof__(struct property));
346                 if (allnextpp) {
347                         pp->name = "name";
348                         pp->length = sz;
349                         pp->value = pp + 1;
350                         *prev_pp = pp;
351                         prev_pp = &pp->next;
352                         memcpy(pp->value, ps, sz - 1);
353                         ((char *)pp->value)[sz - 1] = 0;
354                         pr_debug("fixed up name for %s -> %s\n", pathp,
355                                 (char *)pp->value);
356                 }
357         }
358         if (allnextpp) {
359                 *prev_pp = NULL;
360                 np->name = of_get_property(np, "name", NULL);
361                 np->type = of_get_property(np, "device_type", NULL);
362
363                 if (!np->name)
364                         np->name = "<NULL>";
365                 if (!np->type)
366                         np->type = "<NULL>";
367         }
368         while (tag == OF_DT_BEGIN_NODE) {
369                 mem = unflatten_dt_node(mem, p, np, allnextpp, fpsize);
370                 tag = be32_to_cpup((__be32 *)(*p));
371         }
372         if (tag != OF_DT_END_NODE) {
373                 pr_err("Weird tag at end of node: %x\n", tag);
374                 return mem;
375         }
376         *p += 4;
377         return mem;
378 }
379
380 #ifdef CONFIG_BLK_DEV_INITRD
381 /**
382  * early_init_dt_check_for_initrd - Decode initrd location from flat tree
383  * @node: reference to node containing initrd location ('chosen')
384  */
385 void __init early_init_dt_check_for_initrd(unsigned long node)
386 {
387         unsigned long start, end, len;
388         __be32 *prop;
389
390         pr_debug("Looking for initrd properties... ");
391
392         prop = of_get_flat_dt_prop(node, "linux,initrd-start", &len);
393         if (!prop)
394                 return;
395         start = of_read_ulong(prop, len/4);
396
397         prop = of_get_flat_dt_prop(node, "linux,initrd-end", &len);
398         if (!prop)
399                 return;
400         end = of_read_ulong(prop, len/4);
401
402         early_init_dt_setup_initrd_arch(start, end);
403         pr_debug("initrd_start=0x%lx  initrd_end=0x%lx\n", start, end);
404 }
405 #else
406 inline void early_init_dt_check_for_initrd(unsigned long node)
407 {
408 }
409 #endif /* CONFIG_BLK_DEV_INITRD */
410
411 /**
412  * early_init_dt_scan_root - fetch the top level address and size cells
413  */
414 int __init early_init_dt_scan_root(unsigned long node, const char *uname,
415                                    int depth, void *data)
416 {
417         __be32 *prop;
418
419         if (depth != 0)
420                 return 0;
421
422         dt_root_size_cells = OF_ROOT_NODE_SIZE_CELLS_DEFAULT;
423         dt_root_addr_cells = OF_ROOT_NODE_ADDR_CELLS_DEFAULT;
424
425         prop = of_get_flat_dt_prop(node, "#size-cells", NULL);
426         if (prop)
427                 dt_root_size_cells = be32_to_cpup(prop);
428         pr_debug("dt_root_size_cells = %x\n", dt_root_size_cells);
429
430         prop = of_get_flat_dt_prop(node, "#address-cells", NULL);
431         if (prop)
432                 dt_root_addr_cells = be32_to_cpup(prop);
433         pr_debug("dt_root_addr_cells = %x\n", dt_root_addr_cells);
434
435         /* break now */
436         return 1;
437 }
438
439 u64 __init dt_mem_next_cell(int s, __be32 **cellp)
440 {
441         __be32 *p = *cellp;
442
443         *cellp = p + s;
444         return of_read_number(p, s);
445 }
446
447 /**
448  * early_init_dt_scan_memory - Look for an parse memory nodes
449  */
450 int __init early_init_dt_scan_memory(unsigned long node, const char *uname,
451                                      int depth, void *data)
452 {
453         char *type = of_get_flat_dt_prop(node, "device_type", NULL);
454         __be32 *reg, *endp;
455         unsigned long l;
456
457         /* We are scanning "memory" nodes only */
458         if (type == NULL) {
459                 /*
460                  * The longtrail doesn't have a device_type on the
461                  * /memory node, so look for the node called /memory@0.
462                  */
463                 if (depth != 1 || strcmp(uname, "memory@0") != 0)
464                         return 0;
465         } else if (strcmp(type, "memory") != 0)
466                 return 0;
467
468         reg = of_get_flat_dt_prop(node, "linux,usable-memory", &l);
469         if (reg == NULL)
470                 reg = of_get_flat_dt_prop(node, "reg", &l);
471         if (reg == NULL)
472                 return 0;
473
474         endp = reg + (l / sizeof(__be32));
475
476         pr_debug("memory scan node %s, reg size %ld, data: %x %x %x %x,\n",
477             uname, l, reg[0], reg[1], reg[2], reg[3]);
478
479         while ((endp - reg) >= (dt_root_addr_cells + dt_root_size_cells)) {
480                 u64 base, size;
481
482                 base = dt_mem_next_cell(dt_root_addr_cells, &reg);
483                 size = dt_mem_next_cell(dt_root_size_cells, &reg);
484
485                 if (size == 0)
486                         continue;
487                 pr_debug(" - %llx ,  %llx\n", (unsigned long long)base,
488                     (unsigned long long)size);
489
490                 early_init_dt_add_memory_arch(base, size);
491         }
492
493         return 0;
494 }
495
496 int __init early_init_dt_scan_chosen(unsigned long node, const char *uname,
497                                      int depth, void *data)
498 {
499         unsigned long l;
500         char *p;
501
502         pr_debug("search \"chosen\", depth: %d, uname: %s\n", depth, uname);
503
504         if (depth != 1 ||
505             (strcmp(uname, "chosen") != 0 && strcmp(uname, "chosen@0") != 0))
506                 return 0;
507
508         early_init_dt_check_for_initrd(node);
509
510         /* Retreive command line */
511         p = of_get_flat_dt_prop(node, "bootargs", &l);
512         if (p != NULL && l > 0)
513                 strlcpy(cmd_line, p, min((int)l, COMMAND_LINE_SIZE));
514
515 #ifdef CONFIG_CMDLINE
516 #ifndef CONFIG_CMDLINE_FORCE
517         if (p == NULL || l == 0 || (l == 1 && (*p) == 0))
518 #endif
519                 strlcpy(cmd_line, CONFIG_CMDLINE, COMMAND_LINE_SIZE);
520 #endif /* CONFIG_CMDLINE */
521
522         early_init_dt_scan_chosen_arch(node);
523
524         pr_debug("Command line is: %s\n", cmd_line);
525
526         /* break now */
527         return 1;
528 }
529
530 /**
531  * unflatten_device_tree - create tree of device_nodes from flat blob
532  *
533  * unflattens the device-tree passed by the firmware, creating the
534  * tree of struct device_node. It also fills the "name" and "type"
535  * pointers of the nodes so the normal device-tree walking functions
536  * can be used.
537  */
538 void __init unflatten_device_tree(void)
539 {
540         unsigned long start, mem, size;
541         struct device_node **allnextp = &allnodes;
542
543         pr_debug(" -> unflatten_device_tree()\n");
544
545         /* First pass, scan for size */
546         start = ((unsigned long)initial_boot_params) +
547                 initial_boot_params->off_dt_struct;
548         size = unflatten_dt_node(0, &start, NULL, NULL, 0);
549         size = (size | 3) + 1;
550
551         pr_debug("  size is %lx, allocating...\n", size);
552
553         /* Allocate memory for the expanded device tree */
554         mem = lmb_alloc(size + 4, __alignof__(struct device_node));
555         mem = (unsigned long) __va(mem);
556
557         ((__be32 *)mem)[size / 4] = cpu_to_be32(0xdeadbeef);
558
559         pr_debug("  unflattening %lx...\n", mem);
560
561         /* Second pass, do actual unflattening */
562         start = ((unsigned long)initial_boot_params) +
563                 initial_boot_params->off_dt_struct;
564         unflatten_dt_node(mem, &start, NULL, &allnextp, 0);
565         if (be32_to_cpup((__be32 *)start) != OF_DT_END)
566                 pr_warning("Weird tag at end of tree: %08x\n", *((u32 *)start));
567         if (be32_to_cpu(((__be32 *)mem)[size / 4]) != 0xdeadbeef)
568                 pr_warning("End of tree marker overwritten: %08x\n",
569                            be32_to_cpu(((__be32 *)mem)[size / 4]));
570         *allnextp = NULL;
571
572         /* Get pointer to OF "/chosen" node for use everywhere */
573         of_chosen = of_find_node_by_path("/chosen");
574         if (of_chosen == NULL)
575                 of_chosen = of_find_node_by_path("/chosen@0");
576
577         pr_debug(" <- unflatten_device_tree()\n");
578 }