of/flattree: Merge earlyinit_dt_scan_root()
[deliverable/linux.git] / drivers / of / fdt.c
CommitLineData
e169cfbe
GL
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
41f88009
GL
12#include <linux/kernel.h>
13#include <linux/lmb.h>
f7b3a835 14#include <linux/initrd.h>
e169cfbe
GL
15#include <linux/of.h>
16#include <linux/of_fdt.h>
17
f00abd94
GL
18int __initdata dt_root_addr_cells;
19int __initdata dt_root_size_cells;
20
e169cfbe
GL
21struct boot_param_header *initial_boot_params;
22
23char *find_flat_dt_string(u32 offset)
24{
25 return ((char *)initial_boot_params) +
26 initial_boot_params->off_dt_strings + offset;
27}
c8cb7a59
GL
28
29/**
30 * of_scan_flat_dt - scan flattened tree blob and call callback on each.
31 * @it: callback function
32 * @data: context data pointer
33 *
34 * This function is used to scan the flattened device-tree, it is
35 * used to extract the memory information at boot before we can
36 * unflatten the tree
37 */
38int __init of_scan_flat_dt(int (*it)(unsigned long node,
39 const char *uname, int depth,
40 void *data),
41 void *data)
42{
43 unsigned long p = ((unsigned long)initial_boot_params) +
44 initial_boot_params->off_dt_struct;
45 int rc = 0;
46 int depth = -1;
47
48 do {
49 u32 tag = *((u32 *)p);
50 char *pathp;
51
52 p += 4;
53 if (tag == OF_DT_END_NODE) {
54 depth--;
55 continue;
56 }
57 if (tag == OF_DT_NOP)
58 continue;
59 if (tag == OF_DT_END)
60 break;
61 if (tag == OF_DT_PROP) {
62 u32 sz = *((u32 *)p);
63 p += 8;
64 if (initial_boot_params->version < 0x10)
65 p = _ALIGN(p, sz >= 8 ? 8 : 4);
66 p += sz;
67 p = _ALIGN(p, 4);
68 continue;
69 }
70 if (tag != OF_DT_BEGIN_NODE) {
71 pr_err("Invalid tag %x in flat device tree!\n", tag);
72 return -EINVAL;
73 }
74 depth++;
75 pathp = (char *)p;
76 p = _ALIGN(p + strlen(pathp) + 1, 4);
77 if ((*pathp) == '/') {
78 char *lp, *np;
79 for (lp = NULL, np = pathp; *np; np++)
80 if ((*np) == '/')
81 lp = np+1;
82 if (lp != NULL)
83 pathp = lp;
84 }
85 rc = it(p, pathp, depth, data);
86 if (rc != 0)
87 break;
88 } while (1);
89
90 return rc;
91}
819d2819
GL
92
93/**
94 * of_get_flat_dt_root - find the root node in the flat blob
95 */
96unsigned long __init of_get_flat_dt_root(void)
97{
98 unsigned long p = ((unsigned long)initial_boot_params) +
99 initial_boot_params->off_dt_struct;
100
101 while (*((u32 *)p) == OF_DT_NOP)
102 p += 4;
103 BUG_ON(*((u32 *)p) != OF_DT_BEGIN_NODE);
104 p += 4;
105 return _ALIGN(p + strlen((char *)p) + 1, 4);
106}
107
ca900cfa
GL
108/**
109 * of_get_flat_dt_prop - Given a node in the flat blob, return the property ptr
110 *
111 * This function can be used within scan_flattened_dt callback to get
112 * access to properties
113 */
114void *__init of_get_flat_dt_prop(unsigned long node, const char *name,
115 unsigned long *size)
116{
117 unsigned long p = node;
118
119 do {
120 u32 tag = *((u32 *)p);
121 u32 sz, noff;
122 const char *nstr;
123
124 p += 4;
125 if (tag == OF_DT_NOP)
126 continue;
127 if (tag != OF_DT_PROP)
128 return NULL;
129
130 sz = *((u32 *)p);
131 noff = *((u32 *)(p + 4));
132 p += 8;
133 if (initial_boot_params->version < 0x10)
134 p = _ALIGN(p, sz >= 8 ? 8 : 4);
135
136 nstr = find_flat_dt_string(noff);
137 if (nstr == NULL) {
138 pr_warning("Can't find property index name !\n");
139 return NULL;
140 }
141 if (strcmp(name, nstr) == 0) {
142 if (size)
143 *size = sz;
144 return (void *)p;
145 }
146 p += sz;
147 p = _ALIGN(p, 4);
148 } while (1);
149}
150
00e38efd
GL
151/**
152 * of_flat_dt_is_compatible - Return true if given node has compat in compatible list
153 * @node: node to test
154 * @compat: compatible string to compare with compatible list.
155 */
156int __init of_flat_dt_is_compatible(unsigned long node, const char *compat)
157{
158 const char *cp;
159 unsigned long cplen, l;
160
161 cp = of_get_flat_dt_prop(node, "compatible", &cplen);
162 if (cp == NULL)
163 return 0;
164 while (cplen > 0) {
165 if (strncasecmp(cp, compat, strlen(compat)) == 0)
166 return 1;
167 l = strlen(cp) + 1;
168 cp += l;
169 cplen -= l;
170 }
171
172 return 0;
173}
174
bbd33931
GL
175static void *__init unflatten_dt_alloc(unsigned long *mem, unsigned long size,
176 unsigned long align)
177{
178 void *res;
179
180 *mem = _ALIGN(*mem, align);
181 res = (void *)*mem;
182 *mem += size;
183
184 return res;
185}
186
187/**
188 * unflatten_dt_node - Alloc and populate a device_node from the flat tree
189 * @p: pointer to node in flat tree
190 * @dad: Parent struct device_node
191 * @allnextpp: pointer to ->allnext from last allocated device_node
192 * @fpsize: Size of the node path up at the current depth.
193 */
194unsigned long __init unflatten_dt_node(unsigned long mem,
195 unsigned long *p,
196 struct device_node *dad,
197 struct device_node ***allnextpp,
198 unsigned long fpsize)
199{
200 struct device_node *np;
201 struct property *pp, **prev_pp = NULL;
202 char *pathp;
203 u32 tag;
204 unsigned int l, allocl;
205 int has_name = 0;
206 int new_format = 0;
207
208 tag = *((u32 *)(*p));
209 if (tag != OF_DT_BEGIN_NODE) {
210 pr_err("Weird tag at start of node: %x\n", tag);
211 return mem;
212 }
213 *p += 4;
214 pathp = (char *)*p;
215 l = allocl = strlen(pathp) + 1;
216 *p = _ALIGN(*p + l, 4);
217
218 /* version 0x10 has a more compact unit name here instead of the full
219 * path. we accumulate the full path size using "fpsize", we'll rebuild
220 * it later. We detect this because the first character of the name is
221 * not '/'.
222 */
223 if ((*pathp) != '/') {
224 new_format = 1;
225 if (fpsize == 0) {
226 /* root node: special case. fpsize accounts for path
227 * plus terminating zero. root node only has '/', so
228 * fpsize should be 2, but we want to avoid the first
229 * level nodes to have two '/' so we use fpsize 1 here
230 */
231 fpsize = 1;
232 allocl = 2;
233 } else {
234 /* account for '/' and path size minus terminal 0
235 * already in 'l'
236 */
237 fpsize += l;
238 allocl = fpsize;
239 }
240 }
241
242 np = unflatten_dt_alloc(&mem, sizeof(struct device_node) + allocl,
243 __alignof__(struct device_node));
244 if (allnextpp) {
245 memset(np, 0, sizeof(*np));
246 np->full_name = ((char *)np) + sizeof(struct device_node);
247 if (new_format) {
248 char *fn = np->full_name;
249 /* rebuild full path for new format */
250 if (dad && dad->parent) {
251 strcpy(fn, dad->full_name);
252#ifdef DEBUG
253 if ((strlen(fn) + l + 1) != allocl) {
254 pr_debug("%s: p: %d, l: %d, a: %d\n",
255 pathp, (int)strlen(fn),
256 l, allocl);
257 }
258#endif
259 fn += strlen(fn);
260 }
261 *(fn++) = '/';
262 memcpy(fn, pathp, l);
263 } else
264 memcpy(np->full_name, pathp, l);
265 prev_pp = &np->properties;
266 **allnextpp = np;
267 *allnextpp = &np->allnext;
268 if (dad != NULL) {
269 np->parent = dad;
270 /* we temporarily use the next field as `last_child'*/
271 if (dad->next == NULL)
272 dad->child = np;
273 else
274 dad->next->sibling = np;
275 dad->next = np;
276 }
277 kref_init(&np->kref);
278 }
279 while (1) {
280 u32 sz, noff;
281 char *pname;
282
283 tag = *((u32 *)(*p));
284 if (tag == OF_DT_NOP) {
285 *p += 4;
286 continue;
287 }
288 if (tag != OF_DT_PROP)
289 break;
290 *p += 4;
291 sz = *((u32 *)(*p));
292 noff = *((u32 *)((*p) + 4));
293 *p += 8;
294 if (initial_boot_params->version < 0x10)
295 *p = _ALIGN(*p, sz >= 8 ? 8 : 4);
296
297 pname = find_flat_dt_string(noff);
298 if (pname == NULL) {
299 pr_info("Can't find property name in list !\n");
300 break;
301 }
302 if (strcmp(pname, "name") == 0)
303 has_name = 1;
304 l = strlen(pname) + 1;
305 pp = unflatten_dt_alloc(&mem, sizeof(struct property),
306 __alignof__(struct property));
307 if (allnextpp) {
308 if (strcmp(pname, "linux,phandle") == 0) {
309 np->node = *((u32 *)*p);
310 if (np->linux_phandle == 0)
311 np->linux_phandle = np->node;
312 }
313 if (strcmp(pname, "ibm,phandle") == 0)
314 np->linux_phandle = *((u32 *)*p);
315 pp->name = pname;
316 pp->length = sz;
317 pp->value = (void *)*p;
318 *prev_pp = pp;
319 prev_pp = &pp->next;
320 }
321 *p = _ALIGN((*p) + sz, 4);
322 }
323 /* with version 0x10 we may not have the name property, recreate
324 * it here from the unit name if absent
325 */
326 if (!has_name) {
327 char *p1 = pathp, *ps = pathp, *pa = NULL;
328 int sz;
329
330 while (*p1) {
331 if ((*p1) == '@')
332 pa = p1;
333 if ((*p1) == '/')
334 ps = p1 + 1;
335 p1++;
336 }
337 if (pa < ps)
338 pa = p1;
339 sz = (pa - ps) + 1;
340 pp = unflatten_dt_alloc(&mem, sizeof(struct property) + sz,
341 __alignof__(struct property));
342 if (allnextpp) {
343 pp->name = "name";
344 pp->length = sz;
345 pp->value = pp + 1;
346 *prev_pp = pp;
347 prev_pp = &pp->next;
348 memcpy(pp->value, ps, sz - 1);
349 ((char *)pp->value)[sz - 1] = 0;
350 pr_debug("fixed up name for %s -> %s\n", pathp,
351 (char *)pp->value);
352 }
353 }
354 if (allnextpp) {
355 *prev_pp = NULL;
356 np->name = of_get_property(np, "name", NULL);
357 np->type = of_get_property(np, "device_type", NULL);
358
359 if (!np->name)
360 np->name = "<NULL>";
361 if (!np->type)
362 np->type = "<NULL>";
363 }
364 while (tag == OF_DT_BEGIN_NODE) {
365 mem = unflatten_dt_node(mem, p, np, allnextpp, fpsize);
366 tag = *((u32 *)(*p));
367 }
368 if (tag != OF_DT_END_NODE) {
369 pr_err("Weird tag at end of node: %x\n", tag);
370 return mem;
371 }
372 *p += 4;
373 return mem;
374}
41f88009 375
f7b3a835
GL
376#ifdef CONFIG_BLK_DEV_INITRD
377/**
378 * early_init_dt_check_for_initrd - Decode initrd location from flat tree
379 * @node: reference to node containing initrd location ('chosen')
380 */
381void __init early_init_dt_check_for_initrd(unsigned long node)
382{
383 unsigned long len;
384 u32 *prop;
385
386 pr_debug("Looking for initrd properties... ");
387
388 prop = of_get_flat_dt_prop(node, "linux,initrd-start", &len);
389 if (prop) {
390 initrd_start = (unsigned long)
391 __va(of_read_ulong(prop, len/4));
392
393 prop = of_get_flat_dt_prop(node, "linux,initrd-end", &len);
394 if (prop) {
395 initrd_end = (unsigned long)
396 __va(of_read_ulong(prop, len/4));
397 initrd_below_start_ok = 1;
398 } else {
399 initrd_start = 0;
400 }
401 }
402
403 pr_debug("initrd_start=0x%lx initrd_end=0x%lx\n",
404 initrd_start, initrd_end);
405}
406#else
407inline void early_init_dt_check_for_initrd(unsigned long node)
408{
409}
410#endif /* CONFIG_BLK_DEV_INITRD */
411
f00abd94
GL
412/**
413 * early_init_dt_scan_root - fetch the top level address and size cells
414 */
415int __init early_init_dt_scan_root(unsigned long node, const char *uname,
416 int depth, void *data)
417{
418 u32 *prop;
419
420 if (depth != 0)
421 return 0;
422
423 prop = of_get_flat_dt_prop(node, "#size-cells", NULL);
424 dt_root_size_cells = (prop == NULL) ? 1 : *prop;
425 pr_debug("dt_root_size_cells = %x\n", dt_root_size_cells);
426
427 prop = of_get_flat_dt_prop(node, "#address-cells", NULL);
428 dt_root_addr_cells = (prop == NULL) ? 2 : *prop;
429 pr_debug("dt_root_addr_cells = %x\n", dt_root_addr_cells);
430
431 /* break now */
432 return 1;
433}
434
41f88009
GL
435/**
436 * unflatten_device_tree - create tree of device_nodes from flat blob
437 *
438 * unflattens the device-tree passed by the firmware, creating the
439 * tree of struct device_node. It also fills the "name" and "type"
440 * pointers of the nodes so the normal device-tree walking functions
441 * can be used.
442 */
443void __init unflatten_device_tree(void)
444{
445 unsigned long start, mem, size;
446 struct device_node **allnextp = &allnodes;
447
448 pr_debug(" -> unflatten_device_tree()\n");
449
450 /* First pass, scan for size */
451 start = ((unsigned long)initial_boot_params) +
452 initial_boot_params->off_dt_struct;
453 size = unflatten_dt_node(0, &start, NULL, NULL, 0);
454 size = (size | 3) + 1;
455
456 pr_debug(" size is %lx, allocating...\n", size);
457
458 /* Allocate memory for the expanded device tree */
459 mem = lmb_alloc(size + 4, __alignof__(struct device_node));
460 mem = (unsigned long) __va(mem);
461
462 ((u32 *)mem)[size / 4] = 0xdeadbeef;
463
464 pr_debug(" unflattening %lx...\n", mem);
465
466 /* Second pass, do actual unflattening */
467 start = ((unsigned long)initial_boot_params) +
468 initial_boot_params->off_dt_struct;
469 unflatten_dt_node(mem, &start, NULL, &allnextp, 0);
470 if (*((u32 *)start) != OF_DT_END)
471 pr_warning("Weird tag at end of tree: %08x\n", *((u32 *)start));
472 if (((u32 *)mem)[size / 4] != 0xdeadbeef)
473 pr_warning("End of tree marker overwritten: %08x\n",
474 ((u32 *)mem)[size / 4]);
475 *allnextp = NULL;
476
477 /* Get pointer to OF "/chosen" node for use everywhere */
478 of_chosen = of_find_node_by_path("/chosen");
479 if (of_chosen == NULL)
480 of_chosen = of_find_node_by_path("/chosen@0");
481
482 pr_debug(" <- unflatten_device_tree()\n");
483}
This page took 0.042883 seconds and 5 git commands to generate.