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