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