Gerald Van Baren | 64dbbd4 | 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> |
| 25 | #include <linux/ctype.h> |
| 26 | #include <linux/types.h> |
Gerald Van Baren | 64dbbd4 | 2007-04-06 14:19:43 -0400 | [diff] [blame] | 27 | #include <asm/global_data.h> |
| 28 | #include <fdt.h> |
| 29 | #include <libfdt.h> |
| 30 | #include <fdt_support.h> |
Kumar Gala | 151c8b0 | 2007-11-26 17:06:15 -0600 | [diff] [blame] | 31 | #include <exports.h> |
Gerald Van Baren | 64dbbd4 | 2007-04-06 14:19:43 -0400 | [diff] [blame] | 32 | |
| 33 | /* |
| 34 | * Global data (for the gd->bd) |
| 35 | */ |
| 36 | DECLARE_GLOBAL_DATA_PTR; |
| 37 | |
Kumar Gala | 3bed2aa | 2008-10-23 00:05:47 -0500 | [diff] [blame] | 38 | /** |
| 39 | * fdt_getprop_u32_default - Find a node and return it's property or a default |
| 40 | * |
| 41 | * @fdt: ptr to device tree |
| 42 | * @path: path of node |
| 43 | * @prop: property name |
| 44 | * @dflt: default value if the property isn't found |
| 45 | * |
| 46 | * Convenience function to find a node and return it's property or a |
| 47 | * default value if it doesn't exist. |
| 48 | */ |
| 49 | u32 fdt_getprop_u32_default(void *fdt, const char *path, const char *prop, |
| 50 | const u32 dflt) |
| 51 | { |
| 52 | const u32 *val; |
| 53 | int off; |
| 54 | |
| 55 | off = fdt_path_offset(fdt, path); |
| 56 | if (off < 0) |
| 57 | return dflt; |
| 58 | |
| 59 | val = fdt_getprop(fdt, off, prop, NULL); |
| 60 | if (val) |
| 61 | return *val; |
| 62 | else |
| 63 | return dflt; |
| 64 | } |
Gerald Van Baren | 64dbbd4 | 2007-04-06 14:19:43 -0400 | [diff] [blame] | 65 | |
Kumar Gala | a3c2933 | 2007-10-24 10:21:57 -0500 | [diff] [blame] | 66 | /** |
| 67 | * fdt_find_and_setprop: Find a node and set it's property |
| 68 | * |
| 69 | * @fdt: ptr to device tree |
| 70 | * @node: path of node |
| 71 | * @prop: property name |
| 72 | * @val: ptr to new value |
| 73 | * @len: length of new property value |
| 74 | * @create: flag to create the property if it doesn't exist |
| 75 | * |
| 76 | * Convenience function to directly set a property given the path to the node. |
| 77 | */ |
| 78 | int fdt_find_and_setprop(void *fdt, const char *node, const char *prop, |
| 79 | const void *val, int len, int create) |
| 80 | { |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 81 | int nodeoff = fdt_path_offset(fdt, node); |
Kumar Gala | a3c2933 | 2007-10-24 10:21:57 -0500 | [diff] [blame] | 82 | |
| 83 | if (nodeoff < 0) |
| 84 | return nodeoff; |
| 85 | |
| 86 | if ((!create) && (fdt_get_property(fdt, nodeoff, prop, 0) == NULL)) |
| 87 | return 0; /* create flag not set; so exit quietly */ |
| 88 | |
| 89 | return fdt_setprop(fdt, nodeoff, prop, val, len); |
| 90 | } |
| 91 | |
Kumar Gala | 151c8b0 | 2007-11-26 17:06:15 -0600 | [diff] [blame] | 92 | #ifdef CONFIG_OF_STDOUT_VIA_ALIAS |
Detlev Zundel | 4077781 | 2008-06-20 22:24:05 +0200 | [diff] [blame] | 93 | static int fdt_fixup_stdout(void *fdt, int chosenoff) |
Kumar Gala | 151c8b0 | 2007-11-26 17:06:15 -0600 | [diff] [blame] | 94 | { |
| 95 | int err = 0; |
| 96 | #ifdef CONFIG_CONS_INDEX |
| 97 | int node; |
| 98 | char sername[9] = { 0 }; |
| 99 | const char *path; |
| 100 | |
| 101 | sprintf(sername, "serial%d", CONFIG_CONS_INDEX - 1); |
| 102 | |
| 103 | err = node = fdt_path_offset(fdt, "/aliases"); |
| 104 | if (node >= 0) { |
| 105 | int len; |
| 106 | path = fdt_getprop(fdt, node, sername, &len); |
| 107 | if (path) { |
| 108 | char *p = malloc(len); |
| 109 | err = -FDT_ERR_NOSPACE; |
| 110 | if (p) { |
| 111 | memcpy(p, path, len); |
Detlev Zundel | 4077781 | 2008-06-20 22:24:05 +0200 | [diff] [blame] | 112 | err = fdt_setprop(fdt, chosenoff, |
Kumar Gala | 151c8b0 | 2007-11-26 17:06:15 -0600 | [diff] [blame] | 113 | "linux,stdout-path", p, len); |
| 114 | free(p); |
| 115 | } |
| 116 | } else { |
| 117 | err = len; |
| 118 | } |
| 119 | } |
| 120 | #endif |
| 121 | if (err < 0) |
| 122 | printf("WARNING: could not set linux,stdout-path %s.\n", |
| 123 | fdt_strerror(err)); |
| 124 | |
| 125 | return err; |
| 126 | } |
| 127 | #endif |
| 128 | |
Kumar Gala | 2a1a2cb | 2008-08-15 08:24:43 -0500 | [diff] [blame] | 129 | int fdt_initrd(void *fdt, ulong initrd_start, ulong initrd_end, int force) |
| 130 | { |
| 131 | int nodeoffset; |
| 132 | int err, j, total; |
| 133 | u32 tmp; |
| 134 | const char *path; |
| 135 | uint64_t addr, size; |
| 136 | |
| 137 | /* Find the "chosen" node. */ |
| 138 | nodeoffset = fdt_path_offset (fdt, "/chosen"); |
| 139 | |
| 140 | /* If there is no "chosen" node in the blob return */ |
| 141 | if (nodeoffset < 0) { |
| 142 | printf("fdt_initrd: %s\n", fdt_strerror(nodeoffset)); |
| 143 | return nodeoffset; |
| 144 | } |
| 145 | |
| 146 | /* just return if initrd_start/end aren't valid */ |
| 147 | if ((initrd_start == 0) || (initrd_end == 0)) |
| 148 | return 0; |
| 149 | |
| 150 | total = fdt_num_mem_rsv(fdt); |
| 151 | |
| 152 | /* |
| 153 | * Look for an existing entry and update it. If we don't find |
| 154 | * the entry, we will j be the next available slot. |
| 155 | */ |
| 156 | for (j = 0; j < total; j++) { |
| 157 | err = fdt_get_mem_rsv(fdt, j, &addr, &size); |
| 158 | if (addr == initrd_start) { |
| 159 | fdt_del_mem_rsv(fdt, j); |
| 160 | break; |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | err = fdt_add_mem_rsv(fdt, initrd_start, initrd_end - initrd_start + 1); |
| 165 | if (err < 0) { |
| 166 | printf("fdt_initrd: %s\n", fdt_strerror(err)); |
| 167 | return err; |
| 168 | } |
| 169 | |
| 170 | path = fdt_getprop(fdt, nodeoffset, "linux,initrd-start", NULL); |
| 171 | if ((path == NULL) || force) { |
| 172 | tmp = __cpu_to_be32(initrd_start); |
| 173 | err = fdt_setprop(fdt, nodeoffset, |
| 174 | "linux,initrd-start", &tmp, sizeof(tmp)); |
| 175 | if (err < 0) { |
| 176 | printf("WARNING: " |
| 177 | "could not set linux,initrd-start %s.\n", |
| 178 | fdt_strerror(err)); |
| 179 | return err; |
| 180 | } |
| 181 | tmp = __cpu_to_be32(initrd_end); |
| 182 | err = fdt_setprop(fdt, nodeoffset, |
| 183 | "linux,initrd-end", &tmp, sizeof(tmp)); |
| 184 | if (err < 0) { |
| 185 | printf("WARNING: could not set linux,initrd-end %s.\n", |
| 186 | fdt_strerror(err)); |
| 187 | |
| 188 | return err; |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | return 0; |
| 193 | } |
| 194 | |
Heiko Schocher | 56844a2 | 2008-09-11 08:11:23 +0200 | [diff] [blame] | 195 | int fdt_chosen(void *fdt, int force) |
Gerald Van Baren | 64dbbd4 | 2007-04-06 14:19:43 -0400 | [diff] [blame] | 196 | { |
Gerald Van Baren | 64dbbd4 | 2007-04-06 14:19:43 -0400 | [diff] [blame] | 197 | int nodeoffset; |
| 198 | int err; |
Gerald Van Baren | 35ec398 | 2007-05-25 22:08:57 -0400 | [diff] [blame] | 199 | char *str; /* used to set string properties */ |
Gerald Van Baren | b60af3d | 2007-12-29 22:45:27 -0500 | [diff] [blame] | 200 | const char *path; |
Gerald Van Baren | 64dbbd4 | 2007-04-06 14:19:43 -0400 | [diff] [blame] | 201 | |
| 202 | err = fdt_check_header(fdt); |
| 203 | if (err < 0) { |
Gerald Van Baren | 5fe6be6 | 2007-08-07 21:14:22 -0400 | [diff] [blame] | 204 | printf("fdt_chosen: %s\n", fdt_strerror(err)); |
Gerald Van Baren | 64dbbd4 | 2007-04-06 14:19:43 -0400 | [diff] [blame] | 205 | return err; |
| 206 | } |
| 207 | |
Gerald Van Baren | 64dbbd4 | 2007-04-06 14:19:43 -0400 | [diff] [blame] | 208 | /* |
| 209 | * Find the "chosen" node. |
| 210 | */ |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 211 | nodeoffset = fdt_path_offset (fdt, "/chosen"); |
Gerald Van Baren | 64dbbd4 | 2007-04-06 14:19:43 -0400 | [diff] [blame] | 212 | |
| 213 | /* |
Gerald Van Baren | b60af3d | 2007-12-29 22:45:27 -0500 | [diff] [blame] | 214 | * If there is no "chosen" node in the blob, create it. |
Gerald Van Baren | 64dbbd4 | 2007-04-06 14:19:43 -0400 | [diff] [blame] | 215 | */ |
| 216 | if (nodeoffset < 0) { |
| 217 | /* |
| 218 | * Create a new node "/chosen" (offset 0 is root level) |
| 219 | */ |
| 220 | nodeoffset = fdt_add_subnode(fdt, 0, "chosen"); |
| 221 | if (nodeoffset < 0) { |
Gerald Van Baren | 5fe6be6 | 2007-08-07 21:14:22 -0400 | [diff] [blame] | 222 | printf("WARNING: could not create /chosen %s.\n", |
Gerald Van Baren | 35ec398 | 2007-05-25 22:08:57 -0400 | [diff] [blame] | 223 | fdt_strerror(nodeoffset)); |
Gerald Van Baren | 64dbbd4 | 2007-04-06 14:19:43 -0400 | [diff] [blame] | 224 | return nodeoffset; |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | /* |
Gerald Van Baren | b60af3d | 2007-12-29 22:45:27 -0500 | [diff] [blame] | 229 | * Create /chosen properites that don't exist in the fdt. |
| 230 | * If the property exists, update it only if the "force" parameter |
| 231 | * is true. |
Gerald Van Baren | 64dbbd4 | 2007-04-06 14:19:43 -0400 | [diff] [blame] | 232 | */ |
| 233 | str = getenv("bootargs"); |
| 234 | if (str != NULL) { |
Gerald Van Baren | b60af3d | 2007-12-29 22:45:27 -0500 | [diff] [blame] | 235 | path = fdt_getprop(fdt, nodeoffset, "bootargs", NULL); |
| 236 | if ((path == NULL) || force) { |
| 237 | err = fdt_setprop(fdt, nodeoffset, |
| 238 | "bootargs", str, strlen(str)+1); |
| 239 | if (err < 0) |
| 240 | printf("WARNING: could not set bootargs %s.\n", |
| 241 | fdt_strerror(err)); |
| 242 | } |
Gerald Van Baren | 64dbbd4 | 2007-04-06 14:19:43 -0400 | [diff] [blame] | 243 | } |
Kumar Gala | 2a1a2cb | 2008-08-15 08:24:43 -0500 | [diff] [blame] | 244 | |
Kumar Gala | 151c8b0 | 2007-11-26 17:06:15 -0600 | [diff] [blame] | 245 | #ifdef CONFIG_OF_STDOUT_VIA_ALIAS |
Gerald Van Baren | b60af3d | 2007-12-29 22:45:27 -0500 | [diff] [blame] | 246 | path = fdt_getprop(fdt, nodeoffset, "linux,stdout-path", NULL); |
| 247 | if ((path == NULL) || force) |
| 248 | err = fdt_fixup_stdout(fdt, nodeoffset); |
Kumar Gala | 151c8b0 | 2007-11-26 17:06:15 -0600 | [diff] [blame] | 249 | #endif |
| 250 | |
Gerald Van Baren | 64dbbd4 | 2007-04-06 14:19:43 -0400 | [diff] [blame] | 251 | #ifdef OF_STDOUT_PATH |
Gerald Van Baren | b60af3d | 2007-12-29 22:45:27 -0500 | [diff] [blame] | 252 | path = fdt_getprop(fdt, nodeoffset, "linux,stdout-path", NULL); |
| 253 | if ((path == NULL) || force) { |
| 254 | err = fdt_setprop(fdt, nodeoffset, |
| 255 | "linux,stdout-path", OF_STDOUT_PATH, strlen(OF_STDOUT_PATH)+1); |
| 256 | if (err < 0) |
| 257 | printf("WARNING: could not set linux,stdout-path %s.\n", |
| 258 | fdt_strerror(err)); |
| 259 | } |
Gerald Van Baren | 64dbbd4 | 2007-04-06 14:19:43 -0400 | [diff] [blame] | 260 | #endif |
| 261 | |
Gerald Van Baren | 64dbbd4 | 2007-04-06 14:19:43 -0400 | [diff] [blame] | 262 | return err; |
| 263 | } |
| 264 | |
Kumar Gala | e93becf | 2007-11-03 19:46:28 -0500 | [diff] [blame] | 265 | void do_fixup_by_path(void *fdt, const char *path, const char *prop, |
| 266 | const void *val, int len, int create) |
| 267 | { |
| 268 | #if defined(DEBUG) |
| 269 | int i; |
Kumar Gala | d9ad115 | 2008-02-13 15:09:58 -0600 | [diff] [blame] | 270 | debug("Updating property '%s/%s' = ", path, prop); |
Kumar Gala | e93becf | 2007-11-03 19:46:28 -0500 | [diff] [blame] | 271 | for (i = 0; i < len; i++) |
| 272 | debug(" %.2x", *(u8*)(val+i)); |
| 273 | debug("\n"); |
| 274 | #endif |
| 275 | int rc = fdt_find_and_setprop(fdt, path, prop, val, len, create); |
| 276 | if (rc) |
| 277 | printf("Unable to update property %s:%s, err=%s\n", |
| 278 | path, prop, fdt_strerror(rc)); |
| 279 | } |
| 280 | |
| 281 | void do_fixup_by_path_u32(void *fdt, const char *path, const char *prop, |
| 282 | u32 val, int create) |
| 283 | { |
| 284 | val = cpu_to_fdt32(val); |
| 285 | do_fixup_by_path(fdt, path, prop, &val, sizeof(val), create); |
| 286 | } |
| 287 | |
Kumar Gala | 9eb77ce | 2007-11-21 13:30:15 -0600 | [diff] [blame] | 288 | void do_fixup_by_prop(void *fdt, |
| 289 | const char *pname, const void *pval, int plen, |
| 290 | const char *prop, const void *val, int len, |
| 291 | int create) |
| 292 | { |
| 293 | int off; |
| 294 | #if defined(DEBUG) |
| 295 | int i; |
Kumar Gala | d9ad115 | 2008-02-13 15:09:58 -0600 | [diff] [blame] | 296 | debug("Updating property '%s' = ", prop); |
Kumar Gala | 9eb77ce | 2007-11-21 13:30:15 -0600 | [diff] [blame] | 297 | for (i = 0; i < len; i++) |
| 298 | debug(" %.2x", *(u8*)(val+i)); |
| 299 | debug("\n"); |
| 300 | #endif |
| 301 | off = fdt_node_offset_by_prop_value(fdt, -1, pname, pval, plen); |
| 302 | while (off != -FDT_ERR_NOTFOUND) { |
| 303 | if (create || (fdt_get_property(fdt, off, prop, 0) != NULL)) |
| 304 | fdt_setprop(fdt, off, prop, val, len); |
| 305 | off = fdt_node_offset_by_prop_value(fdt, off, pname, pval, plen); |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | void do_fixup_by_prop_u32(void *fdt, |
| 310 | const char *pname, const void *pval, int plen, |
| 311 | const char *prop, u32 val, int create) |
| 312 | { |
| 313 | val = cpu_to_fdt32(val); |
| 314 | do_fixup_by_prop(fdt, pname, pval, plen, prop, &val, 4, create); |
| 315 | } |
| 316 | |
| 317 | void do_fixup_by_compat(void *fdt, const char *compat, |
| 318 | const char *prop, const void *val, int len, int create) |
| 319 | { |
| 320 | int off = -1; |
| 321 | #if defined(DEBUG) |
| 322 | int i; |
Kumar Gala | d9ad115 | 2008-02-13 15:09:58 -0600 | [diff] [blame] | 323 | debug("Updating property '%s' = ", prop); |
Kumar Gala | 9eb77ce | 2007-11-21 13:30:15 -0600 | [diff] [blame] | 324 | for (i = 0; i < len; i++) |
| 325 | debug(" %.2x", *(u8*)(val+i)); |
| 326 | debug("\n"); |
| 327 | #endif |
| 328 | off = fdt_node_offset_by_compatible(fdt, -1, compat); |
| 329 | while (off != -FDT_ERR_NOTFOUND) { |
| 330 | if (create || (fdt_get_property(fdt, off, prop, 0) != NULL)) |
| 331 | fdt_setprop(fdt, off, prop, val, len); |
| 332 | off = fdt_node_offset_by_compatible(fdt, off, compat); |
| 333 | } |
| 334 | } |
| 335 | |
| 336 | void do_fixup_by_compat_u32(void *fdt, const char *compat, |
| 337 | const char *prop, u32 val, int create) |
| 338 | { |
| 339 | val = cpu_to_fdt32(val); |
| 340 | do_fixup_by_compat(fdt, compat, prop, &val, 4, create); |
| 341 | } |
| 342 | |
Kumar Gala | 3c92728 | 2007-11-26 14:57:45 -0600 | [diff] [blame] | 343 | int fdt_fixup_memory(void *blob, u64 start, u64 size) |
| 344 | { |
| 345 | int err, nodeoffset, len = 0; |
| 346 | u8 tmp[16]; |
| 347 | const u32 *addrcell, *sizecell; |
| 348 | |
| 349 | err = fdt_check_header(blob); |
| 350 | if (err < 0) { |
| 351 | printf("%s: %s\n", __FUNCTION__, fdt_strerror(err)); |
| 352 | return err; |
| 353 | } |
| 354 | |
| 355 | /* update, or add and update /memory node */ |
| 356 | nodeoffset = fdt_path_offset(blob, "/memory"); |
| 357 | if (nodeoffset < 0) { |
| 358 | nodeoffset = fdt_add_subnode(blob, 0, "memory"); |
| 359 | if (nodeoffset < 0) |
| 360 | printf("WARNING: could not create /memory: %s.\n", |
| 361 | fdt_strerror(nodeoffset)); |
| 362 | return nodeoffset; |
| 363 | } |
| 364 | err = fdt_setprop(blob, nodeoffset, "device_type", "memory", |
| 365 | sizeof("memory")); |
| 366 | if (err < 0) { |
| 367 | printf("WARNING: could not set %s %s.\n", "device_type", |
| 368 | fdt_strerror(err)); |
| 369 | return err; |
| 370 | } |
| 371 | |
| 372 | addrcell = fdt_getprop(blob, 0, "#address-cells", NULL); |
| 373 | /* use shifts and mask to ensure endianness */ |
| 374 | if ((addrcell) && (*addrcell == 2)) { |
| 375 | tmp[0] = (start >> 56) & 0xff; |
| 376 | tmp[1] = (start >> 48) & 0xff; |
| 377 | tmp[2] = (start >> 40) & 0xff; |
| 378 | tmp[3] = (start >> 32) & 0xff; |
| 379 | tmp[4] = (start >> 24) & 0xff; |
| 380 | tmp[5] = (start >> 16) & 0xff; |
| 381 | tmp[6] = (start >> 8) & 0xff; |
| 382 | tmp[7] = (start ) & 0xff; |
| 383 | len = 8; |
| 384 | } else { |
| 385 | tmp[0] = (start >> 24) & 0xff; |
| 386 | tmp[1] = (start >> 16) & 0xff; |
| 387 | tmp[2] = (start >> 8) & 0xff; |
| 388 | tmp[3] = (start ) & 0xff; |
| 389 | len = 4; |
| 390 | } |
| 391 | |
| 392 | sizecell = fdt_getprop(blob, 0, "#size-cells", NULL); |
| 393 | /* use shifts and mask to ensure endianness */ |
| 394 | if ((sizecell) && (*sizecell == 2)) { |
| 395 | tmp[0+len] = (size >> 56) & 0xff; |
| 396 | tmp[1+len] = (size >> 48) & 0xff; |
| 397 | tmp[2+len] = (size >> 40) & 0xff; |
| 398 | tmp[3+len] = (size >> 32) & 0xff; |
| 399 | tmp[4+len] = (size >> 24) & 0xff; |
| 400 | tmp[5+len] = (size >> 16) & 0xff; |
| 401 | tmp[6+len] = (size >> 8) & 0xff; |
| 402 | tmp[7+len] = (size ) & 0xff; |
| 403 | len += 8; |
| 404 | } else { |
| 405 | tmp[0+len] = (size >> 24) & 0xff; |
| 406 | tmp[1+len] = (size >> 16) & 0xff; |
| 407 | tmp[2+len] = (size >> 8) & 0xff; |
| 408 | tmp[3+len] = (size ) & 0xff; |
| 409 | len += 4; |
| 410 | } |
| 411 | |
| 412 | err = fdt_setprop(blob, nodeoffset, "reg", tmp, len); |
| 413 | if (err < 0) { |
| 414 | printf("WARNING: could not set %s %s.\n", |
| 415 | "reg", fdt_strerror(err)); |
| 416 | return err; |
| 417 | } |
| 418 | return 0; |
| 419 | } |
| 420 | |
Kumar Gala | ba37aa0 | 2008-08-19 15:41:18 -0500 | [diff] [blame] | 421 | void fdt_fixup_ethernet(void *fdt) |
Kumar Gala | ab54463 | 2007-11-21 11:11:03 -0600 | [diff] [blame] | 422 | { |
Kumar Gala | ba37aa0 | 2008-08-19 15:41:18 -0500 | [diff] [blame] | 423 | int node, i, j; |
| 424 | char enet[16], *tmp, *end; |
| 425 | char mac[16] = "ethaddr"; |
Kumar Gala | ab54463 | 2007-11-21 11:11:03 -0600 | [diff] [blame] | 426 | const char *path; |
Kumar Gala | ba37aa0 | 2008-08-19 15:41:18 -0500 | [diff] [blame] | 427 | unsigned char mac_addr[6]; |
Kumar Gala | ab54463 | 2007-11-21 11:11:03 -0600 | [diff] [blame] | 428 | |
| 429 | node = fdt_path_offset(fdt, "/aliases"); |
Kumar Gala | ba37aa0 | 2008-08-19 15:41:18 -0500 | [diff] [blame] | 430 | if (node < 0) |
| 431 | return; |
| 432 | |
| 433 | i = 0; |
| 434 | while ((tmp = getenv(mac)) != NULL) { |
| 435 | sprintf(enet, "ethernet%d", i); |
| 436 | path = fdt_getprop(fdt, node, enet, NULL); |
| 437 | if (!path) { |
| 438 | debug("No alias for %s\n", enet); |
| 439 | sprintf(mac, "eth%daddr", ++i); |
| 440 | continue; |
Kumar Gala | ab54463 | 2007-11-21 11:11:03 -0600 | [diff] [blame] | 441 | } |
Kumar Gala | ba37aa0 | 2008-08-19 15:41:18 -0500 | [diff] [blame] | 442 | |
| 443 | for (j = 0; j < 6; j++) { |
| 444 | mac_addr[j] = tmp ? simple_strtoul(tmp, &end, 16) : 0; |
| 445 | if (tmp) |
| 446 | tmp = (*end) ? end+1 : end; |
Kumar Gala | ab54463 | 2007-11-21 11:11:03 -0600 | [diff] [blame] | 447 | } |
Kumar Gala | ba37aa0 | 2008-08-19 15:41:18 -0500 | [diff] [blame] | 448 | |
| 449 | do_fixup_by_path(fdt, path, "mac-address", &mac_addr, 6, 0); |
| 450 | do_fixup_by_path(fdt, path, "local-mac-address", |
| 451 | &mac_addr, 6, 1); |
| 452 | |
| 453 | sprintf(mac, "eth%daddr", ++i); |
Kumar Gala | ab54463 | 2007-11-21 11:11:03 -0600 | [diff] [blame] | 454 | } |
| 455 | } |
Anton Vorontsov | 18e69a3 | 2008-03-14 23:20:18 +0300 | [diff] [blame] | 456 | |
| 457 | #ifdef CONFIG_HAS_FSL_DR_USB |
| 458 | void fdt_fixup_dr_usb(void *blob, bd_t *bd) |
| 459 | { |
| 460 | char *mode; |
Anton Vorontsov | 015b27b | 2008-07-08 20:59:43 +0400 | [diff] [blame] | 461 | char *type; |
Anton Vorontsov | 18e69a3 | 2008-03-14 23:20:18 +0300 | [diff] [blame] | 462 | const char *compat = "fsl-usb2-dr"; |
Anton Vorontsov | 015b27b | 2008-07-08 20:59:43 +0400 | [diff] [blame] | 463 | const char *prop_mode = "dr_mode"; |
| 464 | const char *prop_type = "phy_type"; |
Anton Vorontsov | 18e69a3 | 2008-03-14 23:20:18 +0300 | [diff] [blame] | 465 | int node_offset; |
| 466 | int err; |
| 467 | |
| 468 | mode = getenv("usb_dr_mode"); |
Anton Vorontsov | 015b27b | 2008-07-08 20:59:43 +0400 | [diff] [blame] | 469 | type = getenv("usb_phy_type"); |
| 470 | if (!mode && !type) |
Anton Vorontsov | 18e69a3 | 2008-03-14 23:20:18 +0300 | [diff] [blame] | 471 | return; |
| 472 | |
| 473 | node_offset = fdt_node_offset_by_compatible(blob, 0, compat); |
Anton Vorontsov | 015b27b | 2008-07-08 20:59:43 +0400 | [diff] [blame] | 474 | if (node_offset < 0) { |
Anton Vorontsov | 18e69a3 | 2008-03-14 23:20:18 +0300 | [diff] [blame] | 475 | printf("WARNING: could not find compatible node %s: %s.\n", |
| 476 | compat, fdt_strerror(node_offset)); |
Anton Vorontsov | 015b27b | 2008-07-08 20:59:43 +0400 | [diff] [blame] | 477 | return; |
| 478 | } |
Anton Vorontsov | 18e69a3 | 2008-03-14 23:20:18 +0300 | [diff] [blame] | 479 | |
Anton Vorontsov | 015b27b | 2008-07-08 20:59:43 +0400 | [diff] [blame] | 480 | if (mode) { |
| 481 | err = fdt_setprop(blob, node_offset, prop_mode, mode, |
| 482 | strlen(mode) + 1); |
| 483 | if (err < 0) |
| 484 | printf("WARNING: could not set %s for %s: %s.\n", |
| 485 | prop_mode, compat, fdt_strerror(err)); |
| 486 | } |
| 487 | |
| 488 | if (type) { |
| 489 | err = fdt_setprop(blob, node_offset, prop_type, type, |
| 490 | strlen(type) + 1); |
| 491 | if (err < 0) |
| 492 | printf("WARNING: could not set %s for %s: %s.\n", |
| 493 | prop_type, compat, fdt_strerror(err)); |
| 494 | } |
Anton Vorontsov | 18e69a3 | 2008-03-14 23:20:18 +0300 | [diff] [blame] | 495 | } |
| 496 | #endif /* CONFIG_HAS_FSL_DR_USB */ |
Kim Phillips | 6b70ffb | 2008-06-16 15:55:53 -0500 | [diff] [blame] | 497 | |
Peter Tyser | 0f89860 | 2009-05-22 17:23:24 -0500 | [diff] [blame^] | 498 | #if defined(CONFIG_MPC83xx) || defined(CONFIG_MPC85xx) |
Kim Phillips | 6b70ffb | 2008-06-16 15:55:53 -0500 | [diff] [blame] | 499 | /* |
| 500 | * update crypto node properties to a specified revision of the SEC |
| 501 | * called with sec_rev == 0 if not on an mpc8xxxE processor |
| 502 | */ |
| 503 | void fdt_fixup_crypto_node(void *blob, int sec_rev) |
| 504 | { |
| 505 | const struct sec_rev_prop { |
| 506 | u32 sec_rev; |
| 507 | u32 num_channels; |
| 508 | u32 channel_fifo_len; |
| 509 | u32 exec_units_mask; |
| 510 | u32 descriptor_types_mask; |
| 511 | } sec_rev_prop_list [] = { |
| 512 | { 0x0200, 4, 24, 0x07e, 0x01010ebf }, /* SEC 2.0 */ |
| 513 | { 0x0201, 4, 24, 0x0fe, 0x012b0ebf }, /* SEC 2.1 */ |
| 514 | { 0x0202, 1, 24, 0x04c, 0x0122003f }, /* SEC 2.2 */ |
| 515 | { 0x0204, 4, 24, 0x07e, 0x012b0ebf }, /* SEC 2.4 */ |
| 516 | { 0x0300, 4, 24, 0x9fe, 0x03ab0ebf }, /* SEC 3.0 */ |
| 517 | { 0x0303, 4, 24, 0x97c, 0x03ab0abf }, /* SEC 3.3 */ |
| 518 | }; |
| 519 | char compat_strlist[ARRAY_SIZE(sec_rev_prop_list) * |
| 520 | sizeof("fsl,secX.Y")]; |
| 521 | int crypto_node, sec_idx, err; |
| 522 | char *p; |
| 523 | u32 val; |
| 524 | |
| 525 | /* locate crypto node based on lowest common compatible */ |
| 526 | crypto_node = fdt_node_offset_by_compatible(blob, -1, "fsl,sec2.0"); |
| 527 | if (crypto_node == -FDT_ERR_NOTFOUND) |
| 528 | return; |
| 529 | |
| 530 | /* delete it if not on an E-processor */ |
| 531 | if (crypto_node > 0 && !sec_rev) { |
| 532 | fdt_del_node(blob, crypto_node); |
| 533 | return; |
| 534 | } |
| 535 | |
| 536 | /* else we got called for possible uprev */ |
| 537 | for (sec_idx = 0; sec_idx < ARRAY_SIZE(sec_rev_prop_list); sec_idx++) |
| 538 | if (sec_rev_prop_list[sec_idx].sec_rev == sec_rev) |
| 539 | break; |
| 540 | |
| 541 | if (sec_idx == ARRAY_SIZE(sec_rev_prop_list)) { |
| 542 | puts("warning: unknown SEC revision number\n"); |
| 543 | return; |
| 544 | } |
| 545 | |
| 546 | val = cpu_to_fdt32(sec_rev_prop_list[sec_idx].num_channels); |
| 547 | err = fdt_setprop(blob, crypto_node, "fsl,num-channels", &val, 4); |
| 548 | if (err < 0) |
| 549 | printf("WARNING: could not set crypto property: %s\n", |
| 550 | fdt_strerror(err)); |
| 551 | |
| 552 | val = cpu_to_fdt32(sec_rev_prop_list[sec_idx].descriptor_types_mask); |
| 553 | err = fdt_setprop(blob, crypto_node, "fsl,descriptor-types-mask", &val, 4); |
| 554 | if (err < 0) |
| 555 | printf("WARNING: could not set crypto property: %s\n", |
| 556 | fdt_strerror(err)); |
| 557 | |
| 558 | val = cpu_to_fdt32(sec_rev_prop_list[sec_idx].exec_units_mask); |
| 559 | err = fdt_setprop(blob, crypto_node, "fsl,exec-units-mask", &val, 4); |
| 560 | if (err < 0) |
| 561 | printf("WARNING: could not set crypto property: %s\n", |
| 562 | fdt_strerror(err)); |
| 563 | |
| 564 | val = cpu_to_fdt32(sec_rev_prop_list[sec_idx].channel_fifo_len); |
| 565 | err = fdt_setprop(blob, crypto_node, "fsl,channel-fifo-len", &val, 4); |
| 566 | if (err < 0) |
| 567 | printf("WARNING: could not set crypto property: %s\n", |
| 568 | fdt_strerror(err)); |
| 569 | |
| 570 | val = 0; |
| 571 | while (sec_idx >= 0) { |
| 572 | p = compat_strlist + val; |
| 573 | val += sprintf(p, "fsl,sec%d.%d", |
| 574 | (sec_rev_prop_list[sec_idx].sec_rev & 0xff00) >> 8, |
| 575 | sec_rev_prop_list[sec_idx].sec_rev & 0x00ff) + 1; |
| 576 | sec_idx--; |
| 577 | } |
| 578 | err = fdt_setprop(blob, crypto_node, "compatible", &compat_strlist, val); |
| 579 | if (err < 0) |
| 580 | printf("WARNING: could not set crypto property: %s\n", |
| 581 | fdt_strerror(err)); |
| 582 | } |
Peter Tyser | 0f89860 | 2009-05-22 17:23:24 -0500 | [diff] [blame^] | 583 | #endif /* defined(CONFIG_MPC83xx) || defined(CONFIG_MPC85xx) */ |
Kumar Gala | 3082d23 | 2008-08-15 08:24:42 -0500 | [diff] [blame] | 584 | |
| 585 | /* Resize the fdt to its actual size + a bit of padding */ |
| 586 | int fdt_resize(void *blob) |
| 587 | { |
| 588 | int i; |
| 589 | uint64_t addr, size; |
| 590 | int total, ret; |
| 591 | uint actualsize; |
| 592 | |
| 593 | if (!blob) |
| 594 | return 0; |
| 595 | |
| 596 | total = fdt_num_mem_rsv(blob); |
| 597 | for (i = 0; i < total; i++) { |
| 598 | fdt_get_mem_rsv(blob, i, &addr, &size); |
| 599 | if (addr == (uint64_t)(u32)blob) { |
| 600 | fdt_del_mem_rsv(blob, i); |
| 601 | break; |
| 602 | } |
| 603 | } |
| 604 | |
Peter Korsgaard | f242a08 | 2008-10-28 08:26:52 +0100 | [diff] [blame] | 605 | /* |
| 606 | * Calculate the actual size of the fdt |
| 607 | * plus the size needed for fdt_add_mem_rsv |
| 608 | */ |
Kumar Gala | 3082d23 | 2008-08-15 08:24:42 -0500 | [diff] [blame] | 609 | actualsize = fdt_off_dt_strings(blob) + |
Peter Korsgaard | f242a08 | 2008-10-28 08:26:52 +0100 | [diff] [blame] | 610 | fdt_size_dt_strings(blob) + sizeof(struct fdt_reserve_entry); |
Kumar Gala | 3082d23 | 2008-08-15 08:24:42 -0500 | [diff] [blame] | 611 | |
| 612 | /* Make it so the fdt ends on a page boundary */ |
Peter Korsgaard | c088a10 | 2009-01-14 13:52:24 +0100 | [diff] [blame] | 613 | actualsize = ALIGN(actualsize + ((uint)blob & 0xfff), 0x1000); |
Kumar Gala | 3082d23 | 2008-08-15 08:24:42 -0500 | [diff] [blame] | 614 | actualsize = actualsize - ((uint)blob & 0xfff); |
| 615 | |
| 616 | /* Change the fdt header to reflect the correct size */ |
| 617 | fdt_set_totalsize(blob, actualsize); |
| 618 | |
| 619 | /* Add the new reservation */ |
| 620 | ret = fdt_add_mem_rsv(blob, (uint)blob, actualsize); |
| 621 | if (ret < 0) |
| 622 | return ret; |
| 623 | |
| 624 | return actualsize; |
| 625 | } |
Kumar Gala | 8ab451c | 2008-10-22 23:33:56 -0500 | [diff] [blame] | 626 | |
| 627 | #ifdef CONFIG_PCI |
| 628 | #define CONFIG_SYS_PCI_NR_INBOUND_WIN 3 |
| 629 | |
| 630 | #define FDT_PCI_PREFETCH (0x40000000) |
| 631 | #define FDT_PCI_MEM32 (0x02000000) |
| 632 | #define FDT_PCI_IO (0x01000000) |
| 633 | #define FDT_PCI_MEM64 (0x03000000) |
| 634 | |
| 635 | int fdt_pci_dma_ranges(void *blob, int phb_off, struct pci_controller *hose) { |
| 636 | |
| 637 | int addrcell, sizecell, len, r; |
| 638 | u32 *dma_range; |
| 639 | /* sized based on pci addr cells, size-cells, & address-cells */ |
| 640 | u32 dma_ranges[(3 + 2 + 2) * CONFIG_SYS_PCI_NR_INBOUND_WIN]; |
| 641 | |
| 642 | addrcell = fdt_getprop_u32_default(blob, "/", "#address-cells", 1); |
| 643 | sizecell = fdt_getprop_u32_default(blob, "/", "#size-cells", 1); |
| 644 | |
| 645 | dma_range = &dma_ranges[0]; |
| 646 | for (r = 0; r < hose->region_count; r++) { |
| 647 | u64 bus_start, phys_start, size; |
| 648 | |
Kumar Gala | ff4e66e | 2009-02-06 09:49:31 -0600 | [diff] [blame] | 649 | /* skip if !PCI_REGION_SYS_MEMORY */ |
| 650 | if (!(hose->regions[r].flags & PCI_REGION_SYS_MEMORY)) |
Kumar Gala | 8ab451c | 2008-10-22 23:33:56 -0500 | [diff] [blame] | 651 | continue; |
| 652 | |
| 653 | bus_start = (u64)hose->regions[r].bus_start; |
| 654 | phys_start = (u64)hose->regions[r].phys_start; |
| 655 | size = (u64)hose->regions[r].size; |
| 656 | |
| 657 | dma_range[0] = 0; |
| 658 | if (size > 0x100000000ull) |
| 659 | dma_range[0] |= FDT_PCI_MEM64; |
| 660 | else |
| 661 | dma_range[0] |= FDT_PCI_MEM32; |
| 662 | if (hose->regions[r].flags & PCI_REGION_PREFETCH) |
| 663 | dma_range[0] |= FDT_PCI_PREFETCH; |
| 664 | #ifdef CONFIG_SYS_PCI_64BIT |
| 665 | dma_range[1] = bus_start >> 32; |
| 666 | #else |
| 667 | dma_range[1] = 0; |
| 668 | #endif |
| 669 | dma_range[2] = bus_start & 0xffffffff; |
| 670 | |
| 671 | if (addrcell == 2) { |
| 672 | dma_range[3] = phys_start >> 32; |
| 673 | dma_range[4] = phys_start & 0xffffffff; |
| 674 | } else { |
| 675 | dma_range[3] = phys_start & 0xffffffff; |
| 676 | } |
| 677 | |
| 678 | if (sizecell == 2) { |
| 679 | dma_range[3 + addrcell + 0] = size >> 32; |
| 680 | dma_range[3 + addrcell + 1] = size & 0xffffffff; |
| 681 | } else { |
| 682 | dma_range[3 + addrcell + 0] = size & 0xffffffff; |
| 683 | } |
| 684 | |
| 685 | dma_range += (3 + addrcell + sizecell); |
| 686 | } |
| 687 | |
| 688 | len = dma_range - &dma_ranges[0]; |
| 689 | if (len) |
| 690 | fdt_setprop(blob, phb_off, "dma-ranges", &dma_ranges[0], len*4); |
| 691 | |
| 692 | return 0; |
| 693 | } |
| 694 | #endif |