]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/of/fdt.c
56fbd6e3122add28bdcabc6184f395c3c8de95bc
[karo-tx-linux.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/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 = *((u32 *)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 = *((u32 *)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 (*((u32 *)p) == OF_DT_NOP)
107                 p += 4;
108         BUG_ON(*((u32 *)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 = *((u32 *)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 = *((u32 *)p);
136                 noff = *((u32 *)(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 = *((u32 *)(*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 = *((u32 *)(*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 = *((u32 *)(*p));
297                 noff = *((u32 *)((*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 = *((u32 *)(*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         u32 *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         u32 *prop;
418
419         if (depth != 0)
420                 return 0;
421
422         prop = of_get_flat_dt_prop(node, "#size-cells", NULL);
423         dt_root_size_cells = prop ? *prop : OF_ROOT_NODE_SIZE_CELLS_DEFAULT;
424         pr_debug("dt_root_size_cells = %x\n", dt_root_size_cells);
425
426         prop = of_get_flat_dt_prop(node, "#address-cells", NULL);
427         dt_root_addr_cells = prop ? *prop : OF_ROOT_NODE_ADDR_CELLS_DEFAULT;
428         pr_debug("dt_root_addr_cells = %x\n", dt_root_addr_cells);
429
430         /* break now */
431         return 1;
432 }
433
434 u64 __init dt_mem_next_cell(int s, u32 **cellp)
435 {
436         u32 *p = *cellp;
437
438         *cellp = p + s;
439         return of_read_number(p, s);
440 }
441
442 /**
443  * early_init_dt_scan_memory - Look for an parse memory nodes
444  */
445 int __init early_init_dt_scan_memory(unsigned long node, const char *uname,
446                                      int depth, void *data)
447 {
448         char *type = of_get_flat_dt_prop(node, "device_type", NULL);
449         __be32 *reg, *endp;
450         unsigned long l;
451
452         /* We are scanning "memory" nodes only */
453         if (type == NULL) {
454                 /*
455                  * The longtrail doesn't have a device_type on the
456                  * /memory node, so look for the node called /memory@0.
457                  */
458                 if (depth != 1 || strcmp(uname, "memory@0") != 0)
459                         return 0;
460         } else if (strcmp(type, "memory") != 0)
461                 return 0;
462
463         reg = of_get_flat_dt_prop(node, "linux,usable-memory", &l);
464         if (reg == NULL)
465                 reg = of_get_flat_dt_prop(node, "reg", &l);
466         if (reg == NULL)
467                 return 0;
468
469         endp = reg + (l / sizeof(__be32));
470
471         pr_debug("memory scan node %s, reg size %ld, data: %x %x %x %x,\n",
472             uname, l, reg[0], reg[1], reg[2], reg[3]);
473
474         while ((endp - reg) >= (dt_root_addr_cells + dt_root_size_cells)) {
475                 u64 base, size;
476
477                 base = dt_mem_next_cell(dt_root_addr_cells, &reg);
478                 size = dt_mem_next_cell(dt_root_size_cells, &reg);
479
480                 if (size == 0)
481                         continue;
482                 pr_debug(" - %llx ,  %llx\n", (unsigned long long)base,
483                     (unsigned long long)size);
484
485                 early_init_dt_add_memory_arch(base, size);
486         }
487
488         return 0;
489 }
490
491 int __init early_init_dt_scan_chosen(unsigned long node, const char *uname,
492                                      int depth, void *data)
493 {
494         unsigned long l;
495         char *p;
496
497         pr_debug("search \"chosen\", depth: %d, uname: %s\n", depth, uname);
498
499         if (depth != 1 ||
500             (strcmp(uname, "chosen") != 0 && strcmp(uname, "chosen@0") != 0))
501                 return 0;
502
503         early_init_dt_check_for_initrd(node);
504
505         /* Retreive command line */
506         p = of_get_flat_dt_prop(node, "bootargs", &l);
507         if (p != NULL && l > 0)
508                 strlcpy(cmd_line, p, min((int)l, COMMAND_LINE_SIZE));
509
510 #ifdef CONFIG_CMDLINE
511 #ifndef CONFIG_CMDLINE_FORCE
512         if (p == NULL || l == 0 || (l == 1 && (*p) == 0))
513 #endif
514                 strlcpy(cmd_line, CONFIG_CMDLINE, COMMAND_LINE_SIZE);
515 #endif /* CONFIG_CMDLINE */
516
517         early_init_dt_scan_chosen_arch(node);
518
519         pr_debug("Command line is: %s\n", cmd_line);
520
521         /* break now */
522         return 1;
523 }
524
525 /**
526  * unflatten_device_tree - create tree of device_nodes from flat blob
527  *
528  * unflattens the device-tree passed by the firmware, creating the
529  * tree of struct device_node. It also fills the "name" and "type"
530  * pointers of the nodes so the normal device-tree walking functions
531  * can be used.
532  */
533 void __init unflatten_device_tree(void)
534 {
535         unsigned long start, mem, size;
536         struct device_node **allnextp = &allnodes;
537
538         pr_debug(" -> unflatten_device_tree()\n");
539
540         /* First pass, scan for size */
541         start = ((unsigned long)initial_boot_params) +
542                 initial_boot_params->off_dt_struct;
543         size = unflatten_dt_node(0, &start, NULL, NULL, 0);
544         size = (size | 3) + 1;
545
546         pr_debug("  size is %lx, allocating...\n", size);
547
548         /* Allocate memory for the expanded device tree */
549         mem = lmb_alloc(size + 4, __alignof__(struct device_node));
550         mem = (unsigned long) __va(mem);
551
552         ((u32 *)mem)[size / 4] = 0xdeadbeef;
553
554         pr_debug("  unflattening %lx...\n", mem);
555
556         /* Second pass, do actual unflattening */
557         start = ((unsigned long)initial_boot_params) +
558                 initial_boot_params->off_dt_struct;
559         unflatten_dt_node(mem, &start, NULL, &allnextp, 0);
560         if (*((u32 *)start) != OF_DT_END)
561                 pr_warning("Weird tag at end of tree: %08x\n", *((u32 *)start));
562         if (((u32 *)mem)[size / 4] != 0xdeadbeef)
563                 pr_warning("End of tree marker overwritten: %08x\n",
564                            ((u32 *)mem)[size / 4]);
565         *allnextp = NULL;
566
567         /* Get pointer to OF "/chosen" node for use everywhere */
568         of_chosen = of_find_node_by_path("/chosen");
569         if (of_chosen == NULL)
570                 of_chosen = of_find_node_by_path("/chosen@0");
571
572         pr_debug(" <- unflatten_device_tree()\n");
573 }