Gerald Van Baren | c4a57ea | 2007-04-06 14:19:43 -0400 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2007 |
| 3 | * Gerald Van Baren, Custom IDEAS, vanbaren@cideas.com |
| 4 | * |
| 5 | * See file CREDITS for list of people who contributed to this |
| 6 | * project. |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or |
| 9 | * modify it under the terms of the GNU General Public License as |
| 10 | * published by the Free Software Foundation; either version 2 of |
| 11 | * the License, or (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 21 | * MA 02111-1307 USA |
| 22 | */ |
| 23 | |
| 24 | #include <common.h> |
Anton Vorontsov | 9e9cf60 | 2009-10-15 17:47:04 +0400 | [diff] [blame] | 25 | #include <stdio_dev.h> |
Gerald Van Baren | c4a57ea | 2007-04-06 14:19:43 -0400 | [diff] [blame] | 26 | #include <linux/ctype.h> |
| 27 | #include <linux/types.h> |
Gerald Van Baren | c4a57ea | 2007-04-06 14:19:43 -0400 | [diff] [blame] | 28 | #include <asm/global_data.h> |
| 29 | #include <fdt.h> |
| 30 | #include <libfdt.h> |
| 31 | #include <fdt_support.h> |
Kumar Gala | 2dc9b4a | 2007-11-26 17:06:15 -0600 | [diff] [blame] | 32 | #include <exports.h> |
Gerald Van Baren | c4a57ea | 2007-04-06 14:19:43 -0400 | [diff] [blame] | 33 | |
| 34 | /* |
| 35 | * Global data (for the gd->bd) |
| 36 | */ |
| 37 | DECLARE_GLOBAL_DATA_PTR; |
| 38 | |
Kumar Gala | 1ba0040 | 2008-10-23 00:05:47 -0500 | [diff] [blame] | 39 | /** |
| 40 | * fdt_getprop_u32_default - Find a node and return it's property or a default |
| 41 | * |
| 42 | * @fdt: ptr to device tree |
| 43 | * @path: path of node |
| 44 | * @prop: property name |
| 45 | * @dflt: default value if the property isn't found |
| 46 | * |
| 47 | * Convenience function to find a node and return it's property or a |
| 48 | * default value if it doesn't exist. |
| 49 | */ |
| 50 | u32 fdt_getprop_u32_default(void *fdt, const char *path, const char *prop, |
| 51 | const u32 dflt) |
| 52 | { |
| 53 | const u32 *val; |
| 54 | int off; |
| 55 | |
| 56 | off = fdt_path_offset(fdt, path); |
| 57 | if (off < 0) |
| 58 | return dflt; |
| 59 | |
| 60 | val = fdt_getprop(fdt, off, prop, NULL); |
| 61 | if (val) |
| 62 | return *val; |
| 63 | else |
| 64 | return dflt; |
| 65 | } |
Gerald Van Baren | c4a57ea | 2007-04-06 14:19:43 -0400 | [diff] [blame] | 66 | |
Kumar Gala | bf771dc | 2007-10-24 10:21:57 -0500 | [diff] [blame] | 67 | /** |
| 68 | * fdt_find_and_setprop: Find a node and set it's property |
| 69 | * |
| 70 | * @fdt: ptr to device tree |
| 71 | * @node: path of node |
| 72 | * @prop: property name |
| 73 | * @val: ptr to new value |
| 74 | * @len: length of new property value |
| 75 | * @create: flag to create the property if it doesn't exist |
| 76 | * |
| 77 | * Convenience function to directly set a property given the path to the node. |
| 78 | */ |
| 79 | int fdt_find_and_setprop(void *fdt, const char *node, const char *prop, |
| 80 | const void *val, int len, int create) |
| 81 | { |
Kumar Gala | c8ab705 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 82 | int nodeoff = fdt_path_offset(fdt, node); |
Kumar Gala | bf771dc | 2007-10-24 10:21:57 -0500 | [diff] [blame] | 83 | |
| 84 | if (nodeoff < 0) |
| 85 | return nodeoff; |
| 86 | |
| 87 | if ((!create) && (fdt_get_property(fdt, nodeoff, prop, 0) == NULL)) |
| 88 | return 0; /* create flag not set; so exit quietly */ |
| 89 | |
| 90 | return fdt_setprop(fdt, nodeoff, prop, val, len); |
| 91 | } |
| 92 | |
Kumar Gala | 2dc9b4a | 2007-11-26 17:06:15 -0600 | [diff] [blame] | 93 | #ifdef CONFIG_OF_STDOUT_VIA_ALIAS |
Anton Vorontsov | 9e9cf60 | 2009-10-15 17:47:04 +0400 | [diff] [blame] | 94 | |
| 95 | #ifdef CONFIG_SERIAL_MULTI |
| 96 | static void fdt_fill_multisername(char *sername, size_t maxlen) |
| 97 | { |
| 98 | const char *outname = stdio_devices[stdout]->name; |
| 99 | |
| 100 | if (strcmp(outname, "serial") > 0) |
| 101 | strncpy(sername, outname, maxlen); |
| 102 | |
| 103 | /* eserial? */ |
| 104 | if (strcmp(outname + 1, "serial") > 0) |
| 105 | strncpy(sername, outname + 1, maxlen); |
| 106 | } |
| 107 | #else |
| 108 | static inline void fdt_fill_multisername(char *sername, size_t maxlen) {} |
| 109 | #endif /* CONFIG_SERIAL_MULTI */ |
| 110 | |
Detlev Zundel | 46e192a | 2008-06-20 22:24:05 +0200 | [diff] [blame] | 111 | static int fdt_fixup_stdout(void *fdt, int chosenoff) |
Kumar Gala | 2dc9b4a | 2007-11-26 17:06:15 -0600 | [diff] [blame] | 112 | { |
| 113 | int err = 0; |
| 114 | #ifdef CONFIG_CONS_INDEX |
| 115 | int node; |
| 116 | char sername[9] = { 0 }; |
| 117 | const char *path; |
| 118 | |
Anton Vorontsov | 9e9cf60 | 2009-10-15 17:47:04 +0400 | [diff] [blame] | 119 | fdt_fill_multisername(sername, sizeof(sername) - 1); |
| 120 | if (!sername[0]) |
| 121 | sprintf(sername, "serial%d", CONFIG_CONS_INDEX - 1); |
Kumar Gala | 2dc9b4a | 2007-11-26 17:06:15 -0600 | [diff] [blame] | 122 | |
| 123 | err = node = fdt_path_offset(fdt, "/aliases"); |
| 124 | if (node >= 0) { |
| 125 | int len; |
| 126 | path = fdt_getprop(fdt, node, sername, &len); |
| 127 | if (path) { |
| 128 | char *p = malloc(len); |
| 129 | err = -FDT_ERR_NOSPACE; |
| 130 | if (p) { |
| 131 | memcpy(p, path, len); |
Detlev Zundel | 46e192a | 2008-06-20 22:24:05 +0200 | [diff] [blame] | 132 | err = fdt_setprop(fdt, chosenoff, |
Kumar Gala | 2dc9b4a | 2007-11-26 17:06:15 -0600 | [diff] [blame] | 133 | "linux,stdout-path", p, len); |
| 134 | free(p); |
| 135 | } |
| 136 | } else { |
| 137 | err = len; |
| 138 | } |
| 139 | } |
| 140 | #endif |
| 141 | if (err < 0) |
| 142 | printf("WARNING: could not set linux,stdout-path %s.\n", |
| 143 | fdt_strerror(err)); |
| 144 | |
| 145 | return err; |
| 146 | } |
| 147 | #endif |
| 148 | |
Kumar Gala | 99ebc05 | 2008-08-15 08:24:43 -0500 | [diff] [blame] | 149 | int fdt_initrd(void *fdt, ulong initrd_start, ulong initrd_end, int force) |
Gerald Van Baren | c4a57ea | 2007-04-06 14:19:43 -0400 | [diff] [blame] | 150 | { |
Gerald Van Baren | c4a57ea | 2007-04-06 14:19:43 -0400 | [diff] [blame] | 151 | int nodeoffset; |
Kumar Gala | 99ebc05 | 2008-08-15 08:24:43 -0500 | [diff] [blame] | 152 | int err, j, total; |
| 153 | u32 tmp; |
Gerald Van Baren | 569c035 | 2007-12-29 22:45:27 -0500 | [diff] [blame] | 154 | const char *path; |
Kumar Gala | 99ebc05 | 2008-08-15 08:24:43 -0500 | [diff] [blame] | 155 | uint64_t addr, size; |
Gerald Van Baren | c4a57ea | 2007-04-06 14:19:43 -0400 | [diff] [blame] | 156 | |
Kumar Gala | 99ebc05 | 2008-08-15 08:24:43 -0500 | [diff] [blame] | 157 | /* Find the "chosen" node. */ |
| 158 | nodeoffset = fdt_path_offset (fdt, "/chosen"); |
| 159 | |
| 160 | /* If there is no "chosen" node in the blob return */ |
| 161 | if (nodeoffset < 0) { |
| 162 | printf("fdt_initrd: %s\n", fdt_strerror(nodeoffset)); |
| 163 | return nodeoffset; |
Gerald Van Baren | c4a57ea | 2007-04-06 14:19:43 -0400 | [diff] [blame] | 164 | } |
| 165 | |
Kumar Gala | 99ebc05 | 2008-08-15 08:24:43 -0500 | [diff] [blame] | 166 | /* just return if initrd_start/end aren't valid */ |
| 167 | if ((initrd_start == 0) || (initrd_end == 0)) |
| 168 | return 0; |
Gerald Van Baren | c9502b9 | 2007-04-14 22:51:24 -0400 | [diff] [blame] | 169 | |
Kumar Gala | 99ebc05 | 2008-08-15 08:24:43 -0500 | [diff] [blame] | 170 | total = fdt_num_mem_rsv(fdt); |
| 171 | |
| 172 | /* |
| 173 | * Look for an existing entry and update it. If we don't find |
| 174 | * the entry, we will j be the next available slot. |
| 175 | */ |
| 176 | for (j = 0; j < total; j++) { |
| 177 | err = fdt_get_mem_rsv(fdt, j, &addr, &size); |
| 178 | if (addr == initrd_start) { |
| 179 | fdt_del_mem_rsv(fdt, j); |
| 180 | break; |
Gerald Van Baren | c9502b9 | 2007-04-14 22:51:24 -0400 | [diff] [blame] | 181 | } |
Kumar Gala | 99ebc05 | 2008-08-15 08:24:43 -0500 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | err = fdt_add_mem_rsv(fdt, initrd_start, initrd_end - initrd_start + 1); |
| 185 | if (err < 0) { |
| 186 | printf("fdt_initrd: %s\n", fdt_strerror(err)); |
| 187 | return err; |
| 188 | } |
Kumar Gala | c8ab705 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 189 | |
Kumar Gala | 99ebc05 | 2008-08-15 08:24:43 -0500 | [diff] [blame] | 190 | path = fdt_getprop(fdt, nodeoffset, "linux,initrd-start", NULL); |
| 191 | if ((path == NULL) || force) { |
| 192 | tmp = __cpu_to_be32(initrd_start); |
| 193 | err = fdt_setprop(fdt, nodeoffset, |
| 194 | "linux,initrd-start", &tmp, sizeof(tmp)); |
| 195 | if (err < 0) { |
| 196 | printf("WARNING: " |
| 197 | "could not set linux,initrd-start %s.\n", |
| 198 | fdt_strerror(err)); |
| 199 | return err; |
| 200 | } |
| 201 | tmp = __cpu_to_be32(initrd_end); |
| 202 | err = fdt_setprop(fdt, nodeoffset, |
| 203 | "linux,initrd-end", &tmp, sizeof(tmp)); |
Gerald Van Baren | c4a57ea | 2007-04-06 14:19:43 -0400 | [diff] [blame] | 204 | if (err < 0) { |
Kumar Gala | 99ebc05 | 2008-08-15 08:24:43 -0500 | [diff] [blame] | 205 | printf("WARNING: could not set linux,initrd-end %s.\n", |
| 206 | fdt_strerror(err)); |
| 207 | |
Gerald Van Baren | c4a57ea | 2007-04-06 14:19:43 -0400 | [diff] [blame] | 208 | return err; |
| 209 | } |
| 210 | } |
| 211 | |
Kumar Gala | 99ebc05 | 2008-08-15 08:24:43 -0500 | [diff] [blame] | 212 | return 0; |
| 213 | } |
| 214 | |
Heiko Schocher | d29abae | 2008-09-11 08:11:23 +0200 | [diff] [blame] | 215 | int fdt_chosen(void *fdt, int force) |
Kumar Gala | 99ebc05 | 2008-08-15 08:24:43 -0500 | [diff] [blame] | 216 | { |
| 217 | int nodeoffset; |
| 218 | int err; |
| 219 | char *str; /* used to set string properties */ |
| 220 | const char *path; |
| 221 | |
| 222 | err = fdt_check_header(fdt); |
| 223 | if (err < 0) { |
| 224 | printf("fdt_chosen: %s\n", fdt_strerror(err)); |
| 225 | return err; |
| 226 | } |
| 227 | |
Gerald Van Baren | c4a57ea | 2007-04-06 14:19:43 -0400 | [diff] [blame] | 228 | /* |
| 229 | * Find the "chosen" node. |
| 230 | */ |
Kumar Gala | c8ab705 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 231 | nodeoffset = fdt_path_offset (fdt, "/chosen"); |
Gerald Van Baren | c4a57ea | 2007-04-06 14:19:43 -0400 | [diff] [blame] | 232 | |
| 233 | /* |
Gerald Van Baren | 569c035 | 2007-12-29 22:45:27 -0500 | [diff] [blame] | 234 | * If there is no "chosen" node in the blob, create it. |
Gerald Van Baren | c4a57ea | 2007-04-06 14:19:43 -0400 | [diff] [blame] | 235 | */ |
| 236 | if (nodeoffset < 0) { |
| 237 | /* |
| 238 | * Create a new node "/chosen" (offset 0 is root level) |
| 239 | */ |
| 240 | nodeoffset = fdt_add_subnode(fdt, 0, "chosen"); |
| 241 | if (nodeoffset < 0) { |
Gerald Van Baren | 0dc2512 | 2007-08-07 21:14:22 -0400 | [diff] [blame] | 242 | printf("WARNING: could not create /chosen %s.\n", |
Gerald Van Baren | 43555f4 | 2007-05-25 22:08:57 -0400 | [diff] [blame] | 243 | fdt_strerror(nodeoffset)); |
Gerald Van Baren | c4a57ea | 2007-04-06 14:19:43 -0400 | [diff] [blame] | 244 | return nodeoffset; |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | /* |
Gerald Van Baren | 569c035 | 2007-12-29 22:45:27 -0500 | [diff] [blame] | 249 | * Create /chosen properites that don't exist in the fdt. |
| 250 | * If the property exists, update it only if the "force" parameter |
| 251 | * is true. |
Gerald Van Baren | c4a57ea | 2007-04-06 14:19:43 -0400 | [diff] [blame] | 252 | */ |
| 253 | str = getenv("bootargs"); |
| 254 | if (str != NULL) { |
Gerald Van Baren | 569c035 | 2007-12-29 22:45:27 -0500 | [diff] [blame] | 255 | path = fdt_getprop(fdt, nodeoffset, "bootargs", NULL); |
| 256 | if ((path == NULL) || force) { |
| 257 | err = fdt_setprop(fdt, nodeoffset, |
| 258 | "bootargs", str, strlen(str)+1); |
| 259 | if (err < 0) |
| 260 | printf("WARNING: could not set bootargs %s.\n", |
| 261 | fdt_strerror(err)); |
| 262 | } |
Gerald Van Baren | c4a57ea | 2007-04-06 14:19:43 -0400 | [diff] [blame] | 263 | } |
Kumar Gala | 2dc9b4a | 2007-11-26 17:06:15 -0600 | [diff] [blame] | 264 | |
| 265 | #ifdef CONFIG_OF_STDOUT_VIA_ALIAS |
Gerald Van Baren | 569c035 | 2007-12-29 22:45:27 -0500 | [diff] [blame] | 266 | path = fdt_getprop(fdt, nodeoffset, "linux,stdout-path", NULL); |
| 267 | if ((path == NULL) || force) |
| 268 | err = fdt_fixup_stdout(fdt, nodeoffset); |
Kumar Gala | 2dc9b4a | 2007-11-26 17:06:15 -0600 | [diff] [blame] | 269 | #endif |
| 270 | |
Gerald Van Baren | c4a57ea | 2007-04-06 14:19:43 -0400 | [diff] [blame] | 271 | #ifdef OF_STDOUT_PATH |
Gerald Van Baren | 569c035 | 2007-12-29 22:45:27 -0500 | [diff] [blame] | 272 | path = fdt_getprop(fdt, nodeoffset, "linux,stdout-path", NULL); |
| 273 | if ((path == NULL) || force) { |
| 274 | err = fdt_setprop(fdt, nodeoffset, |
| 275 | "linux,stdout-path", OF_STDOUT_PATH, strlen(OF_STDOUT_PATH)+1); |
| 276 | if (err < 0) |
| 277 | printf("WARNING: could not set linux,stdout-path %s.\n", |
| 278 | fdt_strerror(err)); |
| 279 | } |
Gerald Van Baren | c4a57ea | 2007-04-06 14:19:43 -0400 | [diff] [blame] | 280 | #endif |
| 281 | |
Gerald Van Baren | c4a57ea | 2007-04-06 14:19:43 -0400 | [diff] [blame] | 282 | return err; |
| 283 | } |
| 284 | |
Kumar Gala | 7e64cf8 | 2007-11-03 19:46:28 -0500 | [diff] [blame] | 285 | void do_fixup_by_path(void *fdt, const char *path, const char *prop, |
| 286 | const void *val, int len, int create) |
| 287 | { |
| 288 | #if defined(DEBUG) |
| 289 | int i; |
Kumar Gala | 88f0ca2 | 2008-02-13 15:09:58 -0600 | [diff] [blame] | 290 | debug("Updating property '%s/%s' = ", path, prop); |
Kumar Gala | 7e64cf8 | 2007-11-03 19:46:28 -0500 | [diff] [blame] | 291 | for (i = 0; i < len; i++) |
| 292 | debug(" %.2x", *(u8*)(val+i)); |
| 293 | debug("\n"); |
| 294 | #endif |
| 295 | int rc = fdt_find_and_setprop(fdt, path, prop, val, len, create); |
| 296 | if (rc) |
| 297 | printf("Unable to update property %s:%s, err=%s\n", |
| 298 | path, prop, fdt_strerror(rc)); |
| 299 | } |
| 300 | |
| 301 | void do_fixup_by_path_u32(void *fdt, const char *path, const char *prop, |
| 302 | u32 val, int create) |
| 303 | { |
| 304 | val = cpu_to_fdt32(val); |
| 305 | do_fixup_by_path(fdt, path, prop, &val, sizeof(val), create); |
| 306 | } |
| 307 | |
Kumar Gala | 7590a19 | 2007-11-21 13:30:15 -0600 | [diff] [blame] | 308 | void do_fixup_by_prop(void *fdt, |
| 309 | const char *pname, const void *pval, int plen, |
| 310 | const char *prop, const void *val, int len, |
| 311 | int create) |
| 312 | { |
| 313 | int off; |
| 314 | #if defined(DEBUG) |
| 315 | int i; |
Kumar Gala | 88f0ca2 | 2008-02-13 15:09:58 -0600 | [diff] [blame] | 316 | debug("Updating property '%s' = ", prop); |
Kumar Gala | 7590a19 | 2007-11-21 13:30:15 -0600 | [diff] [blame] | 317 | for (i = 0; i < len; i++) |
| 318 | debug(" %.2x", *(u8*)(val+i)); |
| 319 | debug("\n"); |
| 320 | #endif |
| 321 | off = fdt_node_offset_by_prop_value(fdt, -1, pname, pval, plen); |
| 322 | while (off != -FDT_ERR_NOTFOUND) { |
| 323 | if (create || (fdt_get_property(fdt, off, prop, 0) != NULL)) |
| 324 | fdt_setprop(fdt, off, prop, val, len); |
| 325 | off = fdt_node_offset_by_prop_value(fdt, off, pname, pval, plen); |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | void do_fixup_by_prop_u32(void *fdt, |
| 330 | const char *pname, const void *pval, int plen, |
| 331 | const char *prop, u32 val, int create) |
| 332 | { |
| 333 | val = cpu_to_fdt32(val); |
| 334 | do_fixup_by_prop(fdt, pname, pval, plen, prop, &val, 4, create); |
| 335 | } |
| 336 | |
| 337 | void do_fixup_by_compat(void *fdt, const char *compat, |
| 338 | const char *prop, const void *val, int len, int create) |
| 339 | { |
| 340 | int off = -1; |
| 341 | #if defined(DEBUG) |
| 342 | int i; |
Kumar Gala | 88f0ca2 | 2008-02-13 15:09:58 -0600 | [diff] [blame] | 343 | debug("Updating property '%s' = ", prop); |
Kumar Gala | 7590a19 | 2007-11-21 13:30:15 -0600 | [diff] [blame] | 344 | for (i = 0; i < len; i++) |
| 345 | debug(" %.2x", *(u8*)(val+i)); |
| 346 | debug("\n"); |
| 347 | #endif |
| 348 | off = fdt_node_offset_by_compatible(fdt, -1, compat); |
| 349 | while (off != -FDT_ERR_NOTFOUND) { |
| 350 | if (create || (fdt_get_property(fdt, off, prop, 0) != NULL)) |
| 351 | fdt_setprop(fdt, off, prop, val, len); |
| 352 | off = fdt_node_offset_by_compatible(fdt, off, compat); |
| 353 | } |
| 354 | } |
| 355 | |
| 356 | void do_fixup_by_compat_u32(void *fdt, const char *compat, |
| 357 | const char *prop, u32 val, int create) |
| 358 | { |
| 359 | val = cpu_to_fdt32(val); |
| 360 | do_fixup_by_compat(fdt, compat, prop, &val, 4, create); |
| 361 | } |
| 362 | |
Kumar Gala | 0db72ed | 2007-11-26 14:57:45 -0600 | [diff] [blame] | 363 | int fdt_fixup_memory(void *blob, u64 start, u64 size) |
| 364 | { |
| 365 | int err, nodeoffset, len = 0; |
| 366 | u8 tmp[16]; |
| 367 | const u32 *addrcell, *sizecell; |
| 368 | |
| 369 | err = fdt_check_header(blob); |
| 370 | if (err < 0) { |
| 371 | printf("%s: %s\n", __FUNCTION__, fdt_strerror(err)); |
| 372 | return err; |
| 373 | } |
| 374 | |
| 375 | /* update, or add and update /memory node */ |
| 376 | nodeoffset = fdt_path_offset(blob, "/memory"); |
| 377 | if (nodeoffset < 0) { |
| 378 | nodeoffset = fdt_add_subnode(blob, 0, "memory"); |
| 379 | if (nodeoffset < 0) |
| 380 | printf("WARNING: could not create /memory: %s.\n", |
| 381 | fdt_strerror(nodeoffset)); |
| 382 | return nodeoffset; |
| 383 | } |
| 384 | err = fdt_setprop(blob, nodeoffset, "device_type", "memory", |
| 385 | sizeof("memory")); |
| 386 | if (err < 0) { |
| 387 | printf("WARNING: could not set %s %s.\n", "device_type", |
| 388 | fdt_strerror(err)); |
| 389 | return err; |
| 390 | } |
| 391 | |
| 392 | addrcell = fdt_getprop(blob, 0, "#address-cells", NULL); |
| 393 | /* use shifts and mask to ensure endianness */ |
| 394 | if ((addrcell) && (*addrcell == 2)) { |
| 395 | tmp[0] = (start >> 56) & 0xff; |
| 396 | tmp[1] = (start >> 48) & 0xff; |
| 397 | tmp[2] = (start >> 40) & 0xff; |
| 398 | tmp[3] = (start >> 32) & 0xff; |
| 399 | tmp[4] = (start >> 24) & 0xff; |
| 400 | tmp[5] = (start >> 16) & 0xff; |
| 401 | tmp[6] = (start >> 8) & 0xff; |
| 402 | tmp[7] = (start ) & 0xff; |
| 403 | len = 8; |
| 404 | } else { |
| 405 | tmp[0] = (start >> 24) & 0xff; |
| 406 | tmp[1] = (start >> 16) & 0xff; |
| 407 | tmp[2] = (start >> 8) & 0xff; |
| 408 | tmp[3] = (start ) & 0xff; |
| 409 | len = 4; |
| 410 | } |
| 411 | |
| 412 | sizecell = fdt_getprop(blob, 0, "#size-cells", NULL); |
| 413 | /* use shifts and mask to ensure endianness */ |
| 414 | if ((sizecell) && (*sizecell == 2)) { |
| 415 | tmp[0+len] = (size >> 56) & 0xff; |
| 416 | tmp[1+len] = (size >> 48) & 0xff; |
| 417 | tmp[2+len] = (size >> 40) & 0xff; |
| 418 | tmp[3+len] = (size >> 32) & 0xff; |
| 419 | tmp[4+len] = (size >> 24) & 0xff; |
| 420 | tmp[5+len] = (size >> 16) & 0xff; |
| 421 | tmp[6+len] = (size >> 8) & 0xff; |
| 422 | tmp[7+len] = (size ) & 0xff; |
| 423 | len += 8; |
| 424 | } else { |
| 425 | tmp[0+len] = (size >> 24) & 0xff; |
| 426 | tmp[1+len] = (size >> 16) & 0xff; |
| 427 | tmp[2+len] = (size >> 8) & 0xff; |
| 428 | tmp[3+len] = (size ) & 0xff; |
| 429 | len += 4; |
| 430 | } |
| 431 | |
| 432 | err = fdt_setprop(blob, nodeoffset, "reg", tmp, len); |
| 433 | if (err < 0) { |
| 434 | printf("WARNING: could not set %s %s.\n", |
| 435 | "reg", fdt_strerror(err)); |
| 436 | return err; |
| 437 | } |
| 438 | return 0; |
| 439 | } |
| 440 | |
Kumar Gala | fabda92 | 2008-08-19 15:41:18 -0500 | [diff] [blame] | 441 | void fdt_fixup_ethernet(void *fdt) |
Kumar Gala | 2269910 | 2007-11-21 11:11:03 -0600 | [diff] [blame] | 442 | { |
Kumar Gala | fabda92 | 2008-08-19 15:41:18 -0500 | [diff] [blame] | 443 | int node, i, j; |
| 444 | char enet[16], *tmp, *end; |
| 445 | char mac[16] = "ethaddr"; |
Kumar Gala | 2269910 | 2007-11-21 11:11:03 -0600 | [diff] [blame] | 446 | const char *path; |
Kumar Gala | fabda92 | 2008-08-19 15:41:18 -0500 | [diff] [blame] | 447 | unsigned char mac_addr[6]; |
Kumar Gala | 2269910 | 2007-11-21 11:11:03 -0600 | [diff] [blame] | 448 | |
| 449 | node = fdt_path_offset(fdt, "/aliases"); |
Kumar Gala | fabda92 | 2008-08-19 15:41:18 -0500 | [diff] [blame] | 450 | if (node < 0) |
| 451 | return; |
| 452 | |
| 453 | i = 0; |
| 454 | while ((tmp = getenv(mac)) != NULL) { |
| 455 | sprintf(enet, "ethernet%d", i); |
| 456 | path = fdt_getprop(fdt, node, enet, NULL); |
| 457 | if (!path) { |
| 458 | debug("No alias for %s\n", enet); |
| 459 | sprintf(mac, "eth%daddr", ++i); |
| 460 | continue; |
Kumar Gala | 2269910 | 2007-11-21 11:11:03 -0600 | [diff] [blame] | 461 | } |
Kumar Gala | fabda92 | 2008-08-19 15:41:18 -0500 | [diff] [blame] | 462 | |
| 463 | for (j = 0; j < 6; j++) { |
| 464 | mac_addr[j] = tmp ? simple_strtoul(tmp, &end, 16) : 0; |
| 465 | if (tmp) |
| 466 | tmp = (*end) ? end+1 : end; |
Kumar Gala | 2269910 | 2007-11-21 11:11:03 -0600 | [diff] [blame] | 467 | } |
Kumar Gala | fabda92 | 2008-08-19 15:41:18 -0500 | [diff] [blame] | 468 | |
| 469 | do_fixup_by_path(fdt, path, "mac-address", &mac_addr, 6, 0); |
| 470 | do_fixup_by_path(fdt, path, "local-mac-address", |
| 471 | &mac_addr, 6, 1); |
| 472 | |
| 473 | sprintf(mac, "eth%daddr", ++i); |
Kumar Gala | 2269910 | 2007-11-21 11:11:03 -0600 | [diff] [blame] | 474 | } |
| 475 | } |
Anton Vorontsov | 07e6091 | 2008-03-14 23:20:18 +0300 | [diff] [blame] | 476 | |
Kumar Gala | f2f58c5 | 2008-08-15 08:24:42 -0500 | [diff] [blame] | 477 | /* Resize the fdt to its actual size + a bit of padding */ |
| 478 | int fdt_resize(void *blob) |
| 479 | { |
| 480 | int i; |
| 481 | uint64_t addr, size; |
| 482 | int total, ret; |
| 483 | uint actualsize; |
| 484 | |
| 485 | if (!blob) |
| 486 | return 0; |
| 487 | |
| 488 | total = fdt_num_mem_rsv(blob); |
| 489 | for (i = 0; i < total; i++) { |
| 490 | fdt_get_mem_rsv(blob, i, &addr, &size); |
| 491 | if (addr == (uint64_t)(u32)blob) { |
| 492 | fdt_del_mem_rsv(blob, i); |
| 493 | break; |
| 494 | } |
| 495 | } |
| 496 | |
Peter Korsgaard | 9c7b705 | 2008-10-28 08:26:52 +0100 | [diff] [blame] | 497 | /* |
| 498 | * Calculate the actual size of the fdt |
Kumar Gala | 59735d3 | 2009-10-21 23:29:51 -0500 | [diff] [blame] | 499 | * plus the size needed for two fdt_add_mem_rsv, one |
| 500 | * for the fdt itself and one for a possible initrd |
Peter Korsgaard | 9c7b705 | 2008-10-28 08:26:52 +0100 | [diff] [blame] | 501 | */ |
Kumar Gala | f2f58c5 | 2008-08-15 08:24:42 -0500 | [diff] [blame] | 502 | actualsize = fdt_off_dt_strings(blob) + |
Kumar Gala | 59735d3 | 2009-10-21 23:29:51 -0500 | [diff] [blame] | 503 | fdt_size_dt_strings(blob) + 2*sizeof(struct fdt_reserve_entry); |
Kumar Gala | f2f58c5 | 2008-08-15 08:24:42 -0500 | [diff] [blame] | 504 | |
| 505 | /* Make it so the fdt ends on a page boundary */ |
Peter Korsgaard | 9e0a99d | 2009-01-14 13:52:24 +0100 | [diff] [blame] | 506 | actualsize = ALIGN(actualsize + ((uint)blob & 0xfff), 0x1000); |
Kumar Gala | f2f58c5 | 2008-08-15 08:24:42 -0500 | [diff] [blame] | 507 | actualsize = actualsize - ((uint)blob & 0xfff); |
| 508 | |
| 509 | /* Change the fdt header to reflect the correct size */ |
| 510 | fdt_set_totalsize(blob, actualsize); |
| 511 | |
| 512 | /* Add the new reservation */ |
| 513 | ret = fdt_add_mem_rsv(blob, (uint)blob, actualsize); |
| 514 | if (ret < 0) |
| 515 | return ret; |
| 516 | |
| 517 | return actualsize; |
| 518 | } |
Kumar Gala | e0475cd | 2008-10-22 23:33:56 -0500 | [diff] [blame] | 519 | |
| 520 | #ifdef CONFIG_PCI |
Kumar Gala | baf4189 | 2009-08-05 09:03:54 -0500 | [diff] [blame] | 521 | #define CONFIG_SYS_PCI_NR_INBOUND_WIN 4 |
Kumar Gala | e0475cd | 2008-10-22 23:33:56 -0500 | [diff] [blame] | 522 | |
| 523 | #define FDT_PCI_PREFETCH (0x40000000) |
| 524 | #define FDT_PCI_MEM32 (0x02000000) |
| 525 | #define FDT_PCI_IO (0x01000000) |
| 526 | #define FDT_PCI_MEM64 (0x03000000) |
| 527 | |
| 528 | int fdt_pci_dma_ranges(void *blob, int phb_off, struct pci_controller *hose) { |
| 529 | |
| 530 | int addrcell, sizecell, len, r; |
| 531 | u32 *dma_range; |
| 532 | /* sized based on pci addr cells, size-cells, & address-cells */ |
| 533 | u32 dma_ranges[(3 + 2 + 2) * CONFIG_SYS_PCI_NR_INBOUND_WIN]; |
| 534 | |
| 535 | addrcell = fdt_getprop_u32_default(blob, "/", "#address-cells", 1); |
| 536 | sizecell = fdt_getprop_u32_default(blob, "/", "#size-cells", 1); |
| 537 | |
| 538 | dma_range = &dma_ranges[0]; |
| 539 | for (r = 0; r < hose->region_count; r++) { |
| 540 | u64 bus_start, phys_start, size; |
| 541 | |
Kumar Gala | efa1f1d | 2009-02-06 09:49:31 -0600 | [diff] [blame] | 542 | /* skip if !PCI_REGION_SYS_MEMORY */ |
| 543 | if (!(hose->regions[r].flags & PCI_REGION_SYS_MEMORY)) |
Kumar Gala | e0475cd | 2008-10-22 23:33:56 -0500 | [diff] [blame] | 544 | continue; |
| 545 | |
| 546 | bus_start = (u64)hose->regions[r].bus_start; |
| 547 | phys_start = (u64)hose->regions[r].phys_start; |
| 548 | size = (u64)hose->regions[r].size; |
| 549 | |
| 550 | dma_range[0] = 0; |
Kumar Gala | baf4189 | 2009-08-05 09:03:54 -0500 | [diff] [blame] | 551 | if (size >= 0x100000000ull) |
Kumar Gala | e0475cd | 2008-10-22 23:33:56 -0500 | [diff] [blame] | 552 | dma_range[0] |= FDT_PCI_MEM64; |
| 553 | else |
| 554 | dma_range[0] |= FDT_PCI_MEM32; |
| 555 | if (hose->regions[r].flags & PCI_REGION_PREFETCH) |
| 556 | dma_range[0] |= FDT_PCI_PREFETCH; |
| 557 | #ifdef CONFIG_SYS_PCI_64BIT |
| 558 | dma_range[1] = bus_start >> 32; |
| 559 | #else |
| 560 | dma_range[1] = 0; |
| 561 | #endif |
| 562 | dma_range[2] = bus_start & 0xffffffff; |
| 563 | |
| 564 | if (addrcell == 2) { |
| 565 | dma_range[3] = phys_start >> 32; |
| 566 | dma_range[4] = phys_start & 0xffffffff; |
| 567 | } else { |
| 568 | dma_range[3] = phys_start & 0xffffffff; |
| 569 | } |
| 570 | |
| 571 | if (sizecell == 2) { |
| 572 | dma_range[3 + addrcell + 0] = size >> 32; |
| 573 | dma_range[3 + addrcell + 1] = size & 0xffffffff; |
| 574 | } else { |
| 575 | dma_range[3 + addrcell + 0] = size & 0xffffffff; |
| 576 | } |
| 577 | |
| 578 | dma_range += (3 + addrcell + sizecell); |
| 579 | } |
| 580 | |
| 581 | len = dma_range - &dma_ranges[0]; |
| 582 | if (len) |
| 583 | fdt_setprop(blob, phb_off, "dma-ranges", &dma_ranges[0], len*4); |
| 584 | |
| 585 | return 0; |
| 586 | } |
| 587 | #endif |
Stefan Roese | a0f10c9 | 2009-10-21 11:59:52 +0200 | [diff] [blame] | 588 | |
| 589 | #ifdef CONFIG_FDT_FIXUP_NOR_FLASH_SIZE |
| 590 | /* |
| 591 | * This function can be used to update the size in the "reg" property |
| 592 | * of the NOR FLASH device nodes. This is necessary for boards with |
| 593 | * non-fixed NOR FLASH sizes. |
| 594 | */ |
| 595 | int fdt_fixup_nor_flash_size(void *blob, int cs, u32 size) |
| 596 | { |
| 597 | char compat[][16] = { "cfi-flash", "jedec-flash" }; |
| 598 | int off; |
| 599 | int len; |
| 600 | struct fdt_property *prop; |
| 601 | u32 *reg; |
| 602 | int i; |
| 603 | |
| 604 | for (i = 0; i < 2; i++) { |
| 605 | off = fdt_node_offset_by_compatible(blob, -1, compat[i]); |
| 606 | while (off != -FDT_ERR_NOTFOUND) { |
| 607 | /* |
| 608 | * Found one compatible node, now check if this one |
| 609 | * has the correct CS |
| 610 | */ |
| 611 | prop = fdt_get_property_w(blob, off, "reg", &len); |
| 612 | if (prop) { |
| 613 | reg = (u32 *)&prop->data[0]; |
| 614 | if (reg[0] == cs) { |
| 615 | reg[2] = size; |
| 616 | fdt_setprop(blob, off, "reg", reg, |
| 617 | 3 * sizeof(u32)); |
| 618 | |
| 619 | return 0; |
| 620 | } |
| 621 | } |
| 622 | |
| 623 | /* Move to next compatible node */ |
| 624 | off = fdt_node_offset_by_compatible(blob, off, |
| 625 | compat[i]); |
| 626 | } |
| 627 | } |
| 628 | |
| 629 | return -1; |
| 630 | } |
| 631 | #endif |
Anatolij Gustschin | 6c44c38 | 2010-03-16 17:10:05 +0100 | [diff] [blame] | 632 | |
| 633 | #ifdef CONFIG_FDT_FIXUP_PARTITIONS |
| 634 | #include <jffs2/load_kernel.h> |
| 635 | #include <mtd_node.h> |
| 636 | |
| 637 | struct reg_cell { |
| 638 | unsigned int r0; |
| 639 | unsigned int r1; |
| 640 | }; |
| 641 | |
| 642 | int fdt_del_subnodes(const void *blob, int parent_offset) |
| 643 | { |
| 644 | int off, ndepth; |
| 645 | int ret; |
| 646 | |
| 647 | for (ndepth = 0, off = fdt_next_node(blob, parent_offset, &ndepth); |
| 648 | (off >= 0) && (ndepth > 0); |
| 649 | off = fdt_next_node(blob, off, &ndepth)) { |
| 650 | if (ndepth == 1) { |
| 651 | debug("delete %s: offset: %x\n", |
| 652 | fdt_get_name(blob, off, 0), off); |
| 653 | ret = fdt_del_node((void *)blob, off); |
| 654 | if (ret < 0) { |
| 655 | printf("Can't delete node: %s\n", |
| 656 | fdt_strerror(ret)); |
| 657 | return ret; |
| 658 | } else { |
| 659 | ndepth = 0; |
| 660 | off = parent_offset; |
| 661 | } |
| 662 | } |
| 663 | } |
| 664 | return 0; |
| 665 | } |
| 666 | |
| 667 | int fdt_increase_size(void *fdt, int add_len) |
| 668 | { |
| 669 | int newlen; |
| 670 | |
| 671 | newlen = fdt_totalsize(fdt) + add_len; |
| 672 | |
| 673 | /* Open in place with a new len */ |
| 674 | return fdt_open_into(fdt, fdt, newlen); |
| 675 | } |
| 676 | |
| 677 | int fdt_del_partitions(void *blob, int parent_offset) |
| 678 | { |
| 679 | const void *prop; |
| 680 | int ndepth = 0; |
| 681 | int off; |
| 682 | int ret; |
| 683 | |
| 684 | off = fdt_next_node(blob, parent_offset, &ndepth); |
| 685 | if (off > 0 && ndepth == 1) { |
| 686 | prop = fdt_getprop(blob, off, "label", NULL); |
| 687 | if (prop == NULL) { |
| 688 | /* |
| 689 | * Could not find label property, nand {}; node? |
| 690 | * Check subnode, delete partitions there if any. |
| 691 | */ |
| 692 | return fdt_del_partitions(blob, off); |
| 693 | } else { |
| 694 | ret = fdt_del_subnodes(blob, parent_offset); |
| 695 | if (ret < 0) { |
| 696 | printf("Can't remove subnodes: %s\n", |
| 697 | fdt_strerror(ret)); |
| 698 | return ret; |
| 699 | } |
| 700 | } |
| 701 | } |
| 702 | return 0; |
| 703 | } |
| 704 | |
| 705 | int fdt_node_set_part_info(void *blob, int parent_offset, |
| 706 | struct mtd_device *dev) |
| 707 | { |
| 708 | struct list_head *pentry; |
| 709 | struct part_info *part; |
| 710 | struct reg_cell cell; |
| 711 | int off, ndepth = 0; |
| 712 | int part_num, ret; |
| 713 | char buf[64]; |
| 714 | |
| 715 | ret = fdt_del_partitions(blob, parent_offset); |
| 716 | if (ret < 0) |
| 717 | return ret; |
| 718 | |
| 719 | /* |
| 720 | * Check if it is nand {}; subnode, adjust |
| 721 | * the offset in this case |
| 722 | */ |
| 723 | off = fdt_next_node(blob, parent_offset, &ndepth); |
| 724 | if (off > 0 && ndepth == 1) |
| 725 | parent_offset = off; |
| 726 | |
| 727 | part_num = 0; |
| 728 | list_for_each_prev(pentry, &dev->parts) { |
| 729 | int newoff; |
| 730 | |
| 731 | part = list_entry(pentry, struct part_info, link); |
| 732 | |
| 733 | debug("%2d: %-20s0x%08x\t0x%08x\t%d\n", |
| 734 | part_num, part->name, part->size, |
| 735 | part->offset, part->mask_flags); |
| 736 | |
| 737 | sprintf(buf, "partition@%x", part->offset); |
| 738 | add_sub: |
| 739 | ret = fdt_add_subnode(blob, parent_offset, buf); |
| 740 | if (ret == -FDT_ERR_NOSPACE) { |
| 741 | ret = fdt_increase_size(blob, 512); |
| 742 | if (!ret) |
| 743 | goto add_sub; |
| 744 | else |
| 745 | goto err_size; |
| 746 | } else if (ret < 0) { |
| 747 | printf("Can't add partition node: %s\n", |
| 748 | fdt_strerror(ret)); |
| 749 | return ret; |
| 750 | } |
| 751 | newoff = ret; |
| 752 | |
| 753 | /* Check MTD_WRITEABLE_CMD flag */ |
| 754 | if (part->mask_flags & 1) { |
| 755 | add_ro: |
| 756 | ret = fdt_setprop(blob, newoff, "read_only", NULL, 0); |
| 757 | if (ret == -FDT_ERR_NOSPACE) { |
| 758 | ret = fdt_increase_size(blob, 512); |
| 759 | if (!ret) |
| 760 | goto add_ro; |
| 761 | else |
| 762 | goto err_size; |
| 763 | } else if (ret < 0) |
| 764 | goto err_prop; |
| 765 | } |
| 766 | |
| 767 | cell.r0 = cpu_to_fdt32(part->offset); |
| 768 | cell.r1 = cpu_to_fdt32(part->size); |
| 769 | add_reg: |
| 770 | ret = fdt_setprop(blob, newoff, "reg", &cell, sizeof(cell)); |
| 771 | if (ret == -FDT_ERR_NOSPACE) { |
| 772 | ret = fdt_increase_size(blob, 512); |
| 773 | if (!ret) |
| 774 | goto add_reg; |
| 775 | else |
| 776 | goto err_size; |
| 777 | } else if (ret < 0) |
| 778 | goto err_prop; |
| 779 | |
| 780 | add_label: |
| 781 | ret = fdt_setprop_string(blob, newoff, "label", part->name); |
| 782 | if (ret == -FDT_ERR_NOSPACE) { |
| 783 | ret = fdt_increase_size(blob, 512); |
| 784 | if (!ret) |
| 785 | goto add_label; |
| 786 | else |
| 787 | goto err_size; |
| 788 | } else if (ret < 0) |
| 789 | goto err_prop; |
| 790 | |
| 791 | part_num++; |
| 792 | } |
| 793 | return 0; |
| 794 | err_size: |
| 795 | printf("Can't increase blob size: %s\n", fdt_strerror(ret)); |
| 796 | return ret; |
| 797 | err_prop: |
| 798 | printf("Can't add property: %s\n", fdt_strerror(ret)); |
| 799 | return ret; |
| 800 | } |
| 801 | |
| 802 | /* |
| 803 | * Update partitions in nor/nand nodes using info from |
| 804 | * mtdparts environment variable. The nodes to update are |
| 805 | * specified by node_info structure which contains mtd device |
| 806 | * type and compatible string: E. g. the board code in |
| 807 | * ft_board_setup() could use: |
| 808 | * |
| 809 | * struct node_info nodes[] = { |
| 810 | * { "fsl,mpc5121-nfc", MTD_DEV_TYPE_NAND, }, |
| 811 | * { "cfi-flash", MTD_DEV_TYPE_NOR, }, |
| 812 | * }; |
| 813 | * |
| 814 | * fdt_fixup_mtdparts(blob, nodes, ARRAY_SIZE(nodes)); |
| 815 | */ |
| 816 | void fdt_fixup_mtdparts(void *blob, void *node_info, int node_info_size) |
| 817 | { |
| 818 | struct node_info *ni = node_info; |
| 819 | struct mtd_device *dev; |
| 820 | char *parts; |
| 821 | int i, idx; |
| 822 | int noff; |
| 823 | |
| 824 | parts = getenv("mtdparts"); |
| 825 | if (!parts) |
| 826 | return; |
| 827 | |
| 828 | if (mtdparts_init() != 0) |
| 829 | return; |
| 830 | |
| 831 | for (i = 0; i < node_info_size; i++) { |
| 832 | idx = 0; |
| 833 | noff = fdt_node_offset_by_compatible(blob, -1, ni[i].compat); |
| 834 | while (noff != -FDT_ERR_NOTFOUND) { |
| 835 | debug("%s: %s, mtd dev type %d\n", |
| 836 | fdt_get_name(blob, noff, 0), |
| 837 | ni[i].compat, ni[i].type); |
| 838 | dev = device_find(ni[i].type, idx++); |
| 839 | if (dev) { |
| 840 | if (fdt_node_set_part_info(blob, noff, dev)) |
| 841 | return; /* return on error */ |
| 842 | } |
| 843 | |
| 844 | /* Jump to next flash node */ |
| 845 | noff = fdt_node_offset_by_compatible(blob, noff, |
| 846 | ni[i].compat); |
| 847 | } |
| 848 | } |
| 849 | } |
| 850 | #endif |
Kumar Gala | f4ad4fd | 2010-03-30 10:19:26 -0500 | [diff] [blame] | 851 | |
| 852 | void fdt_del_node_and_alias(void *blob, const char *alias) |
| 853 | { |
| 854 | int off = fdt_path_offset(blob, alias); |
| 855 | |
| 856 | if (off < 0) |
| 857 | return; |
| 858 | |
| 859 | fdt_del_node(blob, off); |
| 860 | |
| 861 | off = fdt_path_offset(blob, "/aliases"); |
| 862 | fdt_delprop(blob, off, alias); |
| 863 | } |