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