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 | * |
Kumar Gala | b87e793 | 2010-06-16 14:27:38 -0500 | [diff] [blame] | 5 | * Copyright 2010 Freescale Semiconductor, Inc. |
| 6 | * |
Gerald Van Baren | c4a57ea | 2007-04-06 14:19:43 -0400 | [diff] [blame] | 7 | * See file CREDITS for list of people who contributed to this |
| 8 | * project. |
| 9 | * |
| 10 | * This program is free software; you can redistribute it and/or |
| 11 | * modify it under the terms of the GNU General Public License as |
| 12 | * published by the Free Software Foundation; either version 2 of |
| 13 | * the License, or (at your option) any later version. |
| 14 | * |
| 15 | * This program is distributed in the hope that it will be useful, |
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | * GNU General Public License for more details. |
| 19 | * |
| 20 | * You should have received a copy of the GNU General Public License |
| 21 | * along with this program; if not, write to the Free Software |
| 22 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 23 | * MA 02111-1307 USA |
| 24 | */ |
| 25 | |
| 26 | #include <common.h> |
Anton Vorontsov | 9e9cf60 | 2009-10-15 17:47:04 +0400 | [diff] [blame] | 27 | #include <stdio_dev.h> |
Gerald Van Baren | c4a57ea | 2007-04-06 14:19:43 -0400 | [diff] [blame] | 28 | #include <linux/ctype.h> |
| 29 | #include <linux/types.h> |
Gerald Van Baren | c4a57ea | 2007-04-06 14:19:43 -0400 | [diff] [blame] | 30 | #include <asm/global_data.h> |
| 31 | #include <fdt.h> |
| 32 | #include <libfdt.h> |
| 33 | #include <fdt_support.h> |
Kumar Gala | 2dc9b4a | 2007-11-26 17:06:15 -0600 | [diff] [blame] | 34 | #include <exports.h> |
Gerald Van Baren | c4a57ea | 2007-04-06 14:19:43 -0400 | [diff] [blame] | 35 | |
| 36 | /* |
| 37 | * Global data (for the gd->bd) |
| 38 | */ |
| 39 | DECLARE_GLOBAL_DATA_PTR; |
| 40 | |
Kumar Gala | 1ba0040 | 2008-10-23 00:05:47 -0500 | [diff] [blame] | 41 | /** |
| 42 | * fdt_getprop_u32_default - Find a node and return it's property or a default |
| 43 | * |
| 44 | * @fdt: ptr to device tree |
| 45 | * @path: path of node |
| 46 | * @prop: property name |
| 47 | * @dflt: default value if the property isn't found |
| 48 | * |
| 49 | * Convenience function to find a node and return it's property or a |
| 50 | * default value if it doesn't exist. |
| 51 | */ |
| 52 | u32 fdt_getprop_u32_default(void *fdt, const char *path, const char *prop, |
| 53 | const u32 dflt) |
| 54 | { |
| 55 | const u32 *val; |
| 56 | int off; |
| 57 | |
| 58 | off = fdt_path_offset(fdt, path); |
| 59 | if (off < 0) |
| 60 | return dflt; |
| 61 | |
| 62 | val = fdt_getprop(fdt, off, prop, NULL); |
| 63 | if (val) |
| 64 | return *val; |
| 65 | else |
| 66 | return dflt; |
| 67 | } |
Gerald Van Baren | c4a57ea | 2007-04-06 14:19:43 -0400 | [diff] [blame] | 68 | |
Kumar Gala | bf771dc | 2007-10-24 10:21:57 -0500 | [diff] [blame] | 69 | /** |
| 70 | * fdt_find_and_setprop: Find a node and set it's property |
| 71 | * |
| 72 | * @fdt: ptr to device tree |
| 73 | * @node: path of node |
| 74 | * @prop: property name |
| 75 | * @val: ptr to new value |
| 76 | * @len: length of new property value |
| 77 | * @create: flag to create the property if it doesn't exist |
| 78 | * |
| 79 | * Convenience function to directly set a property given the path to the node. |
| 80 | */ |
| 81 | int fdt_find_and_setprop(void *fdt, const char *node, const char *prop, |
| 82 | const void *val, int len, int create) |
| 83 | { |
Kumar Gala | c8ab705 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 84 | int nodeoff = fdt_path_offset(fdt, node); |
Kumar Gala | bf771dc | 2007-10-24 10:21:57 -0500 | [diff] [blame] | 85 | |
| 86 | if (nodeoff < 0) |
| 87 | return nodeoff; |
| 88 | |
| 89 | if ((!create) && (fdt_get_property(fdt, nodeoff, prop, 0) == NULL)) |
| 90 | return 0; /* create flag not set; so exit quietly */ |
| 91 | |
| 92 | return fdt_setprop(fdt, nodeoff, prop, val, len); |
| 93 | } |
| 94 | |
Kumar Gala | 2dc9b4a | 2007-11-26 17:06:15 -0600 | [diff] [blame] | 95 | #ifdef CONFIG_OF_STDOUT_VIA_ALIAS |
Anton Vorontsov | 9e9cf60 | 2009-10-15 17:47:04 +0400 | [diff] [blame] | 96 | |
| 97 | #ifdef CONFIG_SERIAL_MULTI |
| 98 | static void fdt_fill_multisername(char *sername, size_t maxlen) |
| 99 | { |
| 100 | const char *outname = stdio_devices[stdout]->name; |
| 101 | |
| 102 | if (strcmp(outname, "serial") > 0) |
| 103 | strncpy(sername, outname, maxlen); |
| 104 | |
| 105 | /* eserial? */ |
| 106 | if (strcmp(outname + 1, "serial") > 0) |
| 107 | strncpy(sername, outname + 1, maxlen); |
| 108 | } |
| 109 | #else |
| 110 | static inline void fdt_fill_multisername(char *sername, size_t maxlen) {} |
| 111 | #endif /* CONFIG_SERIAL_MULTI */ |
| 112 | |
Detlev Zundel | 46e192a | 2008-06-20 22:24:05 +0200 | [diff] [blame] | 113 | static int fdt_fixup_stdout(void *fdt, int chosenoff) |
Kumar Gala | 2dc9b4a | 2007-11-26 17:06:15 -0600 | [diff] [blame] | 114 | { |
| 115 | int err = 0; |
| 116 | #ifdef CONFIG_CONS_INDEX |
| 117 | int node; |
| 118 | char sername[9] = { 0 }; |
| 119 | const char *path; |
| 120 | |
Anton Vorontsov | 9e9cf60 | 2009-10-15 17:47:04 +0400 | [diff] [blame] | 121 | fdt_fill_multisername(sername, sizeof(sername) - 1); |
| 122 | if (!sername[0]) |
| 123 | sprintf(sername, "serial%d", CONFIG_CONS_INDEX - 1); |
Kumar Gala | 2dc9b4a | 2007-11-26 17:06:15 -0600 | [diff] [blame] | 124 | |
| 125 | err = node = fdt_path_offset(fdt, "/aliases"); |
| 126 | if (node >= 0) { |
| 127 | int len; |
| 128 | path = fdt_getprop(fdt, node, sername, &len); |
| 129 | if (path) { |
| 130 | char *p = malloc(len); |
| 131 | err = -FDT_ERR_NOSPACE; |
| 132 | if (p) { |
| 133 | memcpy(p, path, len); |
Detlev Zundel | 46e192a | 2008-06-20 22:24:05 +0200 | [diff] [blame] | 134 | err = fdt_setprop(fdt, chosenoff, |
Kumar Gala | 2dc9b4a | 2007-11-26 17:06:15 -0600 | [diff] [blame] | 135 | "linux,stdout-path", p, len); |
| 136 | free(p); |
| 137 | } |
| 138 | } else { |
| 139 | err = len; |
| 140 | } |
| 141 | } |
| 142 | #endif |
| 143 | if (err < 0) |
| 144 | printf("WARNING: could not set linux,stdout-path %s.\n", |
| 145 | fdt_strerror(err)); |
| 146 | |
| 147 | return err; |
| 148 | } |
| 149 | #endif |
| 150 | |
Kumar Gala | 99ebc05 | 2008-08-15 08:24:43 -0500 | [diff] [blame] | 151 | 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] | 152 | { |
Gerald Van Baren | c4a57ea | 2007-04-06 14:19:43 -0400 | [diff] [blame] | 153 | int nodeoffset; |
Kumar Gala | 99ebc05 | 2008-08-15 08:24:43 -0500 | [diff] [blame] | 154 | int err, j, total; |
| 155 | u32 tmp; |
Gerald Van Baren | 569c035 | 2007-12-29 22:45:27 -0500 | [diff] [blame] | 156 | const char *path; |
Kumar Gala | 99ebc05 | 2008-08-15 08:24:43 -0500 | [diff] [blame] | 157 | uint64_t addr, size; |
Gerald Van Baren | c4a57ea | 2007-04-06 14:19:43 -0400 | [diff] [blame] | 158 | |
Kumar Gala | 99ebc05 | 2008-08-15 08:24:43 -0500 | [diff] [blame] | 159 | /* Find the "chosen" node. */ |
| 160 | nodeoffset = fdt_path_offset (fdt, "/chosen"); |
| 161 | |
| 162 | /* If there is no "chosen" node in the blob return */ |
| 163 | if (nodeoffset < 0) { |
| 164 | printf("fdt_initrd: %s\n", fdt_strerror(nodeoffset)); |
| 165 | return nodeoffset; |
Gerald Van Baren | c4a57ea | 2007-04-06 14:19:43 -0400 | [diff] [blame] | 166 | } |
| 167 | |
Kumar Gala | 99ebc05 | 2008-08-15 08:24:43 -0500 | [diff] [blame] | 168 | /* just return if initrd_start/end aren't valid */ |
| 169 | if ((initrd_start == 0) || (initrd_end == 0)) |
| 170 | return 0; |
Gerald Van Baren | c9502b9 | 2007-04-14 22:51:24 -0400 | [diff] [blame] | 171 | |
Kumar Gala | 99ebc05 | 2008-08-15 08:24:43 -0500 | [diff] [blame] | 172 | total = fdt_num_mem_rsv(fdt); |
| 173 | |
| 174 | /* |
| 175 | * Look for an existing entry and update it. If we don't find |
| 176 | * the entry, we will j be the next available slot. |
| 177 | */ |
| 178 | for (j = 0; j < total; j++) { |
| 179 | err = fdt_get_mem_rsv(fdt, j, &addr, &size); |
| 180 | if (addr == initrd_start) { |
| 181 | fdt_del_mem_rsv(fdt, j); |
| 182 | break; |
Gerald Van Baren | c9502b9 | 2007-04-14 22:51:24 -0400 | [diff] [blame] | 183 | } |
Kumar Gala | 99ebc05 | 2008-08-15 08:24:43 -0500 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | err = fdt_add_mem_rsv(fdt, initrd_start, initrd_end - initrd_start + 1); |
| 187 | if (err < 0) { |
| 188 | printf("fdt_initrd: %s\n", fdt_strerror(err)); |
| 189 | return err; |
| 190 | } |
Kumar Gala | c8ab705 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 191 | |
Kumar Gala | 99ebc05 | 2008-08-15 08:24:43 -0500 | [diff] [blame] | 192 | path = fdt_getprop(fdt, nodeoffset, "linux,initrd-start", NULL); |
| 193 | if ((path == NULL) || force) { |
| 194 | tmp = __cpu_to_be32(initrd_start); |
| 195 | err = fdt_setprop(fdt, nodeoffset, |
| 196 | "linux,initrd-start", &tmp, sizeof(tmp)); |
| 197 | if (err < 0) { |
| 198 | printf("WARNING: " |
| 199 | "could not set linux,initrd-start %s.\n", |
| 200 | fdt_strerror(err)); |
| 201 | return err; |
| 202 | } |
| 203 | tmp = __cpu_to_be32(initrd_end); |
| 204 | err = fdt_setprop(fdt, nodeoffset, |
| 205 | "linux,initrd-end", &tmp, sizeof(tmp)); |
Gerald Van Baren | c4a57ea | 2007-04-06 14:19:43 -0400 | [diff] [blame] | 206 | if (err < 0) { |
Kumar Gala | 99ebc05 | 2008-08-15 08:24:43 -0500 | [diff] [blame] | 207 | printf("WARNING: could not set linux,initrd-end %s.\n", |
| 208 | fdt_strerror(err)); |
| 209 | |
Gerald Van Baren | c4a57ea | 2007-04-06 14:19:43 -0400 | [diff] [blame] | 210 | return err; |
| 211 | } |
| 212 | } |
| 213 | |
Kumar Gala | 99ebc05 | 2008-08-15 08:24:43 -0500 | [diff] [blame] | 214 | return 0; |
| 215 | } |
| 216 | |
Heiko Schocher | d29abae | 2008-09-11 08:11:23 +0200 | [diff] [blame] | 217 | int fdt_chosen(void *fdt, int force) |
Kumar Gala | 99ebc05 | 2008-08-15 08:24:43 -0500 | [diff] [blame] | 218 | { |
| 219 | int nodeoffset; |
| 220 | int err; |
| 221 | char *str; /* used to set string properties */ |
| 222 | const char *path; |
| 223 | |
| 224 | err = fdt_check_header(fdt); |
| 225 | if (err < 0) { |
| 226 | printf("fdt_chosen: %s\n", fdt_strerror(err)); |
| 227 | return err; |
| 228 | } |
| 229 | |
Gerald Van Baren | c4a57ea | 2007-04-06 14:19:43 -0400 | [diff] [blame] | 230 | /* |
| 231 | * Find the "chosen" node. |
| 232 | */ |
Kumar Gala | c8ab705 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 233 | nodeoffset = fdt_path_offset (fdt, "/chosen"); |
Gerald Van Baren | c4a57ea | 2007-04-06 14:19:43 -0400 | [diff] [blame] | 234 | |
| 235 | /* |
Gerald Van Baren | 569c035 | 2007-12-29 22:45:27 -0500 | [diff] [blame] | 236 | * If there is no "chosen" node in the blob, create it. |
Gerald Van Baren | c4a57ea | 2007-04-06 14:19:43 -0400 | [diff] [blame] | 237 | */ |
| 238 | if (nodeoffset < 0) { |
| 239 | /* |
| 240 | * Create a new node "/chosen" (offset 0 is root level) |
| 241 | */ |
| 242 | nodeoffset = fdt_add_subnode(fdt, 0, "chosen"); |
| 243 | if (nodeoffset < 0) { |
Gerald Van Baren | 0dc2512 | 2007-08-07 21:14:22 -0400 | [diff] [blame] | 244 | printf("WARNING: could not create /chosen %s.\n", |
Gerald Van Baren | 43555f4 | 2007-05-25 22:08:57 -0400 | [diff] [blame] | 245 | fdt_strerror(nodeoffset)); |
Gerald Van Baren | c4a57ea | 2007-04-06 14:19:43 -0400 | [diff] [blame] | 246 | return nodeoffset; |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | /* |
Gerald Van Baren | 569c035 | 2007-12-29 22:45:27 -0500 | [diff] [blame] | 251 | * Create /chosen properites that don't exist in the fdt. |
| 252 | * If the property exists, update it only if the "force" parameter |
| 253 | * is true. |
Gerald Van Baren | c4a57ea | 2007-04-06 14:19:43 -0400 | [diff] [blame] | 254 | */ |
| 255 | str = getenv("bootargs"); |
| 256 | if (str != NULL) { |
Gerald Van Baren | 569c035 | 2007-12-29 22:45:27 -0500 | [diff] [blame] | 257 | path = fdt_getprop(fdt, nodeoffset, "bootargs", NULL); |
| 258 | if ((path == NULL) || force) { |
| 259 | err = fdt_setprop(fdt, nodeoffset, |
| 260 | "bootargs", str, strlen(str)+1); |
| 261 | if (err < 0) |
| 262 | printf("WARNING: could not set bootargs %s.\n", |
| 263 | fdt_strerror(err)); |
| 264 | } |
Gerald Van Baren | c4a57ea | 2007-04-06 14:19:43 -0400 | [diff] [blame] | 265 | } |
Kumar Gala | 2dc9b4a | 2007-11-26 17:06:15 -0600 | [diff] [blame] | 266 | |
| 267 | #ifdef CONFIG_OF_STDOUT_VIA_ALIAS |
Gerald Van Baren | 569c035 | 2007-12-29 22:45:27 -0500 | [diff] [blame] | 268 | path = fdt_getprop(fdt, nodeoffset, "linux,stdout-path", NULL); |
| 269 | if ((path == NULL) || force) |
| 270 | err = fdt_fixup_stdout(fdt, nodeoffset); |
Kumar Gala | 2dc9b4a | 2007-11-26 17:06:15 -0600 | [diff] [blame] | 271 | #endif |
| 272 | |
Gerald Van Baren | c4a57ea | 2007-04-06 14:19:43 -0400 | [diff] [blame] | 273 | #ifdef OF_STDOUT_PATH |
Gerald Van Baren | 569c035 | 2007-12-29 22:45:27 -0500 | [diff] [blame] | 274 | path = fdt_getprop(fdt, nodeoffset, "linux,stdout-path", NULL); |
| 275 | if ((path == NULL) || force) { |
| 276 | err = fdt_setprop(fdt, nodeoffset, |
| 277 | "linux,stdout-path", OF_STDOUT_PATH, strlen(OF_STDOUT_PATH)+1); |
| 278 | if (err < 0) |
| 279 | printf("WARNING: could not set linux,stdout-path %s.\n", |
| 280 | fdt_strerror(err)); |
| 281 | } |
Gerald Van Baren | c4a57ea | 2007-04-06 14:19:43 -0400 | [diff] [blame] | 282 | #endif |
| 283 | |
Gerald Van Baren | c4a57ea | 2007-04-06 14:19:43 -0400 | [diff] [blame] | 284 | return err; |
| 285 | } |
| 286 | |
Kumar Gala | 7e64cf8 | 2007-11-03 19:46:28 -0500 | [diff] [blame] | 287 | void do_fixup_by_path(void *fdt, const char *path, const char *prop, |
| 288 | const void *val, int len, int create) |
| 289 | { |
| 290 | #if defined(DEBUG) |
| 291 | int i; |
Kumar Gala | 88f0ca2 | 2008-02-13 15:09:58 -0600 | [diff] [blame] | 292 | debug("Updating property '%s/%s' = ", path, prop); |
Kumar Gala | 7e64cf8 | 2007-11-03 19:46:28 -0500 | [diff] [blame] | 293 | for (i = 0; i < len; i++) |
| 294 | debug(" %.2x", *(u8*)(val+i)); |
| 295 | debug("\n"); |
| 296 | #endif |
| 297 | int rc = fdt_find_and_setprop(fdt, path, prop, val, len, create); |
| 298 | if (rc) |
| 299 | printf("Unable to update property %s:%s, err=%s\n", |
| 300 | path, prop, fdt_strerror(rc)); |
| 301 | } |
| 302 | |
| 303 | void do_fixup_by_path_u32(void *fdt, const char *path, const char *prop, |
| 304 | u32 val, int create) |
| 305 | { |
| 306 | val = cpu_to_fdt32(val); |
| 307 | do_fixup_by_path(fdt, path, prop, &val, sizeof(val), create); |
| 308 | } |
| 309 | |
Kumar Gala | 7590a19 | 2007-11-21 13:30:15 -0600 | [diff] [blame] | 310 | void do_fixup_by_prop(void *fdt, |
| 311 | const char *pname, const void *pval, int plen, |
| 312 | const char *prop, const void *val, int len, |
| 313 | int create) |
| 314 | { |
| 315 | int off; |
| 316 | #if defined(DEBUG) |
| 317 | int i; |
Kumar Gala | 88f0ca2 | 2008-02-13 15:09:58 -0600 | [diff] [blame] | 318 | debug("Updating property '%s' = ", prop); |
Kumar Gala | 7590a19 | 2007-11-21 13:30:15 -0600 | [diff] [blame] | 319 | for (i = 0; i < len; i++) |
| 320 | debug(" %.2x", *(u8*)(val+i)); |
| 321 | debug("\n"); |
| 322 | #endif |
| 323 | off = fdt_node_offset_by_prop_value(fdt, -1, pname, pval, plen); |
| 324 | while (off != -FDT_ERR_NOTFOUND) { |
| 325 | if (create || (fdt_get_property(fdt, off, prop, 0) != NULL)) |
| 326 | fdt_setprop(fdt, off, prop, val, len); |
| 327 | off = fdt_node_offset_by_prop_value(fdt, off, pname, pval, plen); |
| 328 | } |
| 329 | } |
| 330 | |
| 331 | void do_fixup_by_prop_u32(void *fdt, |
| 332 | const char *pname, const void *pval, int plen, |
| 333 | const char *prop, u32 val, int create) |
| 334 | { |
| 335 | val = cpu_to_fdt32(val); |
| 336 | do_fixup_by_prop(fdt, pname, pval, plen, prop, &val, 4, create); |
| 337 | } |
| 338 | |
| 339 | void do_fixup_by_compat(void *fdt, const char *compat, |
| 340 | const char *prop, const void *val, int len, int create) |
| 341 | { |
| 342 | int off = -1; |
| 343 | #if defined(DEBUG) |
| 344 | int i; |
Kumar Gala | 88f0ca2 | 2008-02-13 15:09:58 -0600 | [diff] [blame] | 345 | debug("Updating property '%s' = ", prop); |
Kumar Gala | 7590a19 | 2007-11-21 13:30:15 -0600 | [diff] [blame] | 346 | for (i = 0; i < len; i++) |
| 347 | debug(" %.2x", *(u8*)(val+i)); |
| 348 | debug("\n"); |
| 349 | #endif |
| 350 | off = fdt_node_offset_by_compatible(fdt, -1, compat); |
| 351 | while (off != -FDT_ERR_NOTFOUND) { |
| 352 | if (create || (fdt_get_property(fdt, off, prop, 0) != NULL)) |
| 353 | fdt_setprop(fdt, off, prop, val, len); |
| 354 | off = fdt_node_offset_by_compatible(fdt, off, compat); |
| 355 | } |
| 356 | } |
| 357 | |
| 358 | void do_fixup_by_compat_u32(void *fdt, const char *compat, |
| 359 | const char *prop, u32 val, int create) |
| 360 | { |
| 361 | val = cpu_to_fdt32(val); |
| 362 | do_fixup_by_compat(fdt, compat, prop, &val, 4, create); |
| 363 | } |
| 364 | |
John Rigby | b4ae942 | 2010-10-13 13:57:33 -0600 | [diff] [blame] | 365 | /* |
| 366 | * Get cells len in bytes |
| 367 | * if #NNNN-cells property is 2 then len is 8 |
| 368 | * otherwise len is 4 |
| 369 | */ |
| 370 | static int get_cells_len(void *blob, char *nr_cells_name) |
Kumar Gala | 0db72ed | 2007-11-26 14:57:45 -0600 | [diff] [blame] | 371 | { |
John Rigby | b4ae942 | 2010-10-13 13:57:33 -0600 | [diff] [blame] | 372 | const u32 *cell; |
| 373 | |
| 374 | cell = fdt_getprop(blob, 0, nr_cells_name, NULL); |
| 375 | if (cell && *cell == 2) |
| 376 | return 8; |
| 377 | |
| 378 | return 4; |
| 379 | } |
| 380 | |
| 381 | /* |
| 382 | * Write a 4 or 8 byte big endian cell |
| 383 | */ |
| 384 | static void write_cell(u8 *addr, u64 val, int size) |
| 385 | { |
| 386 | int shift = (size - 1) * 8; |
| 387 | while (size-- > 0) { |
| 388 | *addr++ = (val >> shift) & 0xff; |
| 389 | shift -= 8; |
| 390 | } |
| 391 | } |
| 392 | |
| 393 | int fdt_fixup_memory_banks(void *blob, u64 start[], u64 size[], int banks) |
| 394 | { |
| 395 | int err, nodeoffset; |
| 396 | int addr_cell_len, size_cell_len, len; |
| 397 | u8 tmp[banks * 8]; |
| 398 | int bank; |
Kumar Gala | 0db72ed | 2007-11-26 14:57:45 -0600 | [diff] [blame] | 399 | |
| 400 | err = fdt_check_header(blob); |
| 401 | if (err < 0) { |
| 402 | printf("%s: %s\n", __FUNCTION__, fdt_strerror(err)); |
| 403 | return err; |
| 404 | } |
| 405 | |
| 406 | /* update, or add and update /memory node */ |
| 407 | nodeoffset = fdt_path_offset(blob, "/memory"); |
| 408 | if (nodeoffset < 0) { |
| 409 | nodeoffset = fdt_add_subnode(blob, 0, "memory"); |
| 410 | if (nodeoffset < 0) |
| 411 | printf("WARNING: could not create /memory: %s.\n", |
| 412 | fdt_strerror(nodeoffset)); |
| 413 | return nodeoffset; |
| 414 | } |
| 415 | err = fdt_setprop(blob, nodeoffset, "device_type", "memory", |
| 416 | sizeof("memory")); |
| 417 | if (err < 0) { |
| 418 | printf("WARNING: could not set %s %s.\n", "device_type", |
| 419 | fdt_strerror(err)); |
| 420 | return err; |
| 421 | } |
| 422 | |
John Rigby | b4ae942 | 2010-10-13 13:57:33 -0600 | [diff] [blame] | 423 | addr_cell_len = get_cells_len(blob, "#address-cells"); |
| 424 | size_cell_len = get_cells_len(blob, "#size-cells"); |
| 425 | |
| 426 | for (bank = 0, len = 0; bank < banks; bank++) { |
| 427 | write_cell(tmp + len, start[bank], addr_cell_len); |
| 428 | len += addr_cell_len; |
Kumar Gala | 0db72ed | 2007-11-26 14:57:45 -0600 | [diff] [blame] | 429 | |
John Rigby | b4ae942 | 2010-10-13 13:57:33 -0600 | [diff] [blame] | 430 | write_cell(tmp + len, size[bank], size_cell_len); |
| 431 | len += size_cell_len; |
Kumar Gala | 0db72ed | 2007-11-26 14:57:45 -0600 | [diff] [blame] | 432 | } |
| 433 | |
| 434 | err = fdt_setprop(blob, nodeoffset, "reg", tmp, len); |
| 435 | if (err < 0) { |
| 436 | printf("WARNING: could not set %s %s.\n", |
| 437 | "reg", fdt_strerror(err)); |
| 438 | return err; |
| 439 | } |
| 440 | return 0; |
| 441 | } |
| 442 | |
John Rigby | b4ae942 | 2010-10-13 13:57:33 -0600 | [diff] [blame] | 443 | int fdt_fixup_memory(void *blob, u64 start, u64 size) |
| 444 | { |
| 445 | return fdt_fixup_memory_banks(blob, &start, &size, 1); |
| 446 | } |
| 447 | |
Kumar Gala | fabda92 | 2008-08-19 15:41:18 -0500 | [diff] [blame] | 448 | void fdt_fixup_ethernet(void *fdt) |
Kumar Gala | 2269910 | 2007-11-21 11:11:03 -0600 | [diff] [blame] | 449 | { |
Kumar Gala | fabda92 | 2008-08-19 15:41:18 -0500 | [diff] [blame] | 450 | int node, i, j; |
| 451 | char enet[16], *tmp, *end; |
| 452 | char mac[16] = "ethaddr"; |
Kumar Gala | 2269910 | 2007-11-21 11:11:03 -0600 | [diff] [blame] | 453 | const char *path; |
Kumar Gala | fabda92 | 2008-08-19 15:41:18 -0500 | [diff] [blame] | 454 | unsigned char mac_addr[6]; |
Kumar Gala | 2269910 | 2007-11-21 11:11:03 -0600 | [diff] [blame] | 455 | |
| 456 | node = fdt_path_offset(fdt, "/aliases"); |
Kumar Gala | fabda92 | 2008-08-19 15:41:18 -0500 | [diff] [blame] | 457 | if (node < 0) |
| 458 | return; |
| 459 | |
| 460 | i = 0; |
| 461 | while ((tmp = getenv(mac)) != NULL) { |
| 462 | sprintf(enet, "ethernet%d", i); |
| 463 | path = fdt_getprop(fdt, node, enet, NULL); |
| 464 | if (!path) { |
| 465 | debug("No alias for %s\n", enet); |
| 466 | sprintf(mac, "eth%daddr", ++i); |
| 467 | continue; |
Kumar Gala | 2269910 | 2007-11-21 11:11:03 -0600 | [diff] [blame] | 468 | } |
Kumar Gala | fabda92 | 2008-08-19 15:41:18 -0500 | [diff] [blame] | 469 | |
| 470 | for (j = 0; j < 6; j++) { |
| 471 | mac_addr[j] = tmp ? simple_strtoul(tmp, &end, 16) : 0; |
| 472 | if (tmp) |
| 473 | tmp = (*end) ? end+1 : end; |
Kumar Gala | 2269910 | 2007-11-21 11:11:03 -0600 | [diff] [blame] | 474 | } |
Kumar Gala | fabda92 | 2008-08-19 15:41:18 -0500 | [diff] [blame] | 475 | |
| 476 | do_fixup_by_path(fdt, path, "mac-address", &mac_addr, 6, 0); |
| 477 | do_fixup_by_path(fdt, path, "local-mac-address", |
| 478 | &mac_addr, 6, 1); |
| 479 | |
| 480 | sprintf(mac, "eth%daddr", ++i); |
Kumar Gala | 2269910 | 2007-11-21 11:11:03 -0600 | [diff] [blame] | 481 | } |
| 482 | } |
Anton Vorontsov | 07e6091 | 2008-03-14 23:20:18 +0300 | [diff] [blame] | 483 | |
Kumar Gala | f2f58c5 | 2008-08-15 08:24:42 -0500 | [diff] [blame] | 484 | /* Resize the fdt to its actual size + a bit of padding */ |
| 485 | int fdt_resize(void *blob) |
| 486 | { |
| 487 | int i; |
| 488 | uint64_t addr, size; |
| 489 | int total, ret; |
| 490 | uint actualsize; |
| 491 | |
| 492 | if (!blob) |
| 493 | return 0; |
| 494 | |
| 495 | total = fdt_num_mem_rsv(blob); |
| 496 | for (i = 0; i < total; i++) { |
| 497 | fdt_get_mem_rsv(blob, i, &addr, &size); |
| 498 | if (addr == (uint64_t)(u32)blob) { |
| 499 | fdt_del_mem_rsv(blob, i); |
| 500 | break; |
| 501 | } |
| 502 | } |
| 503 | |
Peter Korsgaard | 9c7b705 | 2008-10-28 08:26:52 +0100 | [diff] [blame] | 504 | /* |
| 505 | * Calculate the actual size of the fdt |
Feng Wang | ba31fb6 | 2010-08-03 16:22:43 +0200 | [diff] [blame] | 506 | * plus the size needed for 5 fdt_add_mem_rsv, one |
| 507 | * for the fdt itself and 4 for a possible initrd |
| 508 | * ((initrd-start + initrd-end) * 2 (name & value)) |
Peter Korsgaard | 9c7b705 | 2008-10-28 08:26:52 +0100 | [diff] [blame] | 509 | */ |
Kumar Gala | f2f58c5 | 2008-08-15 08:24:42 -0500 | [diff] [blame] | 510 | actualsize = fdt_off_dt_strings(blob) + |
Feng Wang | ba31fb6 | 2010-08-03 16:22:43 +0200 | [diff] [blame] | 511 | fdt_size_dt_strings(blob) + 5 * sizeof(struct fdt_reserve_entry); |
Kumar Gala | f2f58c5 | 2008-08-15 08:24:42 -0500 | [diff] [blame] | 512 | |
| 513 | /* Make it so the fdt ends on a page boundary */ |
Peter Korsgaard | 9e0a99d | 2009-01-14 13:52:24 +0100 | [diff] [blame] | 514 | actualsize = ALIGN(actualsize + ((uint)blob & 0xfff), 0x1000); |
Kumar Gala | f2f58c5 | 2008-08-15 08:24:42 -0500 | [diff] [blame] | 515 | actualsize = actualsize - ((uint)blob & 0xfff); |
| 516 | |
| 517 | /* Change the fdt header to reflect the correct size */ |
| 518 | fdt_set_totalsize(blob, actualsize); |
| 519 | |
| 520 | /* Add the new reservation */ |
| 521 | ret = fdt_add_mem_rsv(blob, (uint)blob, actualsize); |
| 522 | if (ret < 0) |
| 523 | return ret; |
| 524 | |
| 525 | return actualsize; |
| 526 | } |
Kumar Gala | e0475cd | 2008-10-22 23:33:56 -0500 | [diff] [blame] | 527 | |
| 528 | #ifdef CONFIG_PCI |
Kumar Gala | baf4189 | 2009-08-05 09:03:54 -0500 | [diff] [blame] | 529 | #define CONFIG_SYS_PCI_NR_INBOUND_WIN 4 |
Kumar Gala | e0475cd | 2008-10-22 23:33:56 -0500 | [diff] [blame] | 530 | |
| 531 | #define FDT_PCI_PREFETCH (0x40000000) |
| 532 | #define FDT_PCI_MEM32 (0x02000000) |
| 533 | #define FDT_PCI_IO (0x01000000) |
| 534 | #define FDT_PCI_MEM64 (0x03000000) |
| 535 | |
| 536 | int fdt_pci_dma_ranges(void *blob, int phb_off, struct pci_controller *hose) { |
| 537 | |
| 538 | int addrcell, sizecell, len, r; |
| 539 | u32 *dma_range; |
| 540 | /* sized based on pci addr cells, size-cells, & address-cells */ |
| 541 | u32 dma_ranges[(3 + 2 + 2) * CONFIG_SYS_PCI_NR_INBOUND_WIN]; |
| 542 | |
| 543 | addrcell = fdt_getprop_u32_default(blob, "/", "#address-cells", 1); |
| 544 | sizecell = fdt_getprop_u32_default(blob, "/", "#size-cells", 1); |
| 545 | |
| 546 | dma_range = &dma_ranges[0]; |
| 547 | for (r = 0; r < hose->region_count; r++) { |
| 548 | u64 bus_start, phys_start, size; |
| 549 | |
Kumar Gala | efa1f1d | 2009-02-06 09:49:31 -0600 | [diff] [blame] | 550 | /* skip if !PCI_REGION_SYS_MEMORY */ |
| 551 | if (!(hose->regions[r].flags & PCI_REGION_SYS_MEMORY)) |
Kumar Gala | e0475cd | 2008-10-22 23:33:56 -0500 | [diff] [blame] | 552 | continue; |
| 553 | |
| 554 | bus_start = (u64)hose->regions[r].bus_start; |
| 555 | phys_start = (u64)hose->regions[r].phys_start; |
| 556 | size = (u64)hose->regions[r].size; |
| 557 | |
| 558 | dma_range[0] = 0; |
Kumar Gala | baf4189 | 2009-08-05 09:03:54 -0500 | [diff] [blame] | 559 | if (size >= 0x100000000ull) |
Kumar Gala | e0475cd | 2008-10-22 23:33:56 -0500 | [diff] [blame] | 560 | dma_range[0] |= FDT_PCI_MEM64; |
| 561 | else |
| 562 | dma_range[0] |= FDT_PCI_MEM32; |
| 563 | if (hose->regions[r].flags & PCI_REGION_PREFETCH) |
| 564 | dma_range[0] |= FDT_PCI_PREFETCH; |
| 565 | #ifdef CONFIG_SYS_PCI_64BIT |
| 566 | dma_range[1] = bus_start >> 32; |
| 567 | #else |
| 568 | dma_range[1] = 0; |
| 569 | #endif |
| 570 | dma_range[2] = bus_start & 0xffffffff; |
| 571 | |
| 572 | if (addrcell == 2) { |
| 573 | dma_range[3] = phys_start >> 32; |
| 574 | dma_range[4] = phys_start & 0xffffffff; |
| 575 | } else { |
| 576 | dma_range[3] = phys_start & 0xffffffff; |
| 577 | } |
| 578 | |
| 579 | if (sizecell == 2) { |
| 580 | dma_range[3 + addrcell + 0] = size >> 32; |
| 581 | dma_range[3 + addrcell + 1] = size & 0xffffffff; |
| 582 | } else { |
| 583 | dma_range[3 + addrcell + 0] = size & 0xffffffff; |
| 584 | } |
| 585 | |
| 586 | dma_range += (3 + addrcell + sizecell); |
| 587 | } |
| 588 | |
| 589 | len = dma_range - &dma_ranges[0]; |
| 590 | if (len) |
| 591 | fdt_setprop(blob, phb_off, "dma-ranges", &dma_ranges[0], len*4); |
| 592 | |
| 593 | return 0; |
| 594 | } |
| 595 | #endif |
Stefan Roese | a0f10c9 | 2009-10-21 11:59:52 +0200 | [diff] [blame] | 596 | |
| 597 | #ifdef CONFIG_FDT_FIXUP_NOR_FLASH_SIZE |
| 598 | /* |
Stefan Roese | 412a71a | 2010-09-16 14:01:53 +0200 | [diff] [blame] | 599 | * Provide a weak default function to return the flash bank size. |
| 600 | * There might be multiple non-identical flash chips connected to one |
| 601 | * chip-select, so we need to pass an index as well. |
| 602 | */ |
| 603 | u32 __flash_get_bank_size(int cs, int idx) |
| 604 | { |
| 605 | extern flash_info_t flash_info[]; |
| 606 | |
| 607 | /* |
| 608 | * As default, a simple 1:1 mapping is provided. Boards with |
| 609 | * a different mapping need to supply a board specific mapping |
| 610 | * routine. |
| 611 | */ |
| 612 | return flash_info[cs].size; |
| 613 | } |
| 614 | u32 flash_get_bank_size(int cs, int idx) |
| 615 | __attribute__((weak, alias("__flash_get_bank_size"))); |
| 616 | |
| 617 | /* |
Stefan Roese | a0f10c9 | 2009-10-21 11:59:52 +0200 | [diff] [blame] | 618 | * This function can be used to update the size in the "reg" property |
Stefan Roese | 412a71a | 2010-09-16 14:01:53 +0200 | [diff] [blame] | 619 | * of all NOR FLASH device nodes. This is necessary for boards with |
Stefan Roese | a0f10c9 | 2009-10-21 11:59:52 +0200 | [diff] [blame] | 620 | * non-fixed NOR FLASH sizes. |
| 621 | */ |
Stefan Roese | 412a71a | 2010-09-16 14:01:53 +0200 | [diff] [blame] | 622 | int fdt_fixup_nor_flash_size(void *blob) |
Stefan Roese | a0f10c9 | 2009-10-21 11:59:52 +0200 | [diff] [blame] | 623 | { |
| 624 | char compat[][16] = { "cfi-flash", "jedec-flash" }; |
| 625 | int off; |
| 626 | int len; |
| 627 | struct fdt_property *prop; |
Stefan Roese | 442cd1a | 2010-09-24 13:51:50 +0200 | [diff] [blame] | 628 | u32 *reg, *reg2; |
Stefan Roese | a0f10c9 | 2009-10-21 11:59:52 +0200 | [diff] [blame] | 629 | int i; |
| 630 | |
| 631 | for (i = 0; i < 2; i++) { |
| 632 | off = fdt_node_offset_by_compatible(blob, -1, compat[i]); |
| 633 | while (off != -FDT_ERR_NOTFOUND) { |
Stefan Roese | 412a71a | 2010-09-16 14:01:53 +0200 | [diff] [blame] | 634 | int idx; |
| 635 | |
Stefan Roese | a0f10c9 | 2009-10-21 11:59:52 +0200 | [diff] [blame] | 636 | /* |
Stefan Roese | 412a71a | 2010-09-16 14:01:53 +0200 | [diff] [blame] | 637 | * Found one compatible node, so fixup the size |
| 638 | * int its reg properties |
Stefan Roese | a0f10c9 | 2009-10-21 11:59:52 +0200 | [diff] [blame] | 639 | */ |
| 640 | prop = fdt_get_property_w(blob, off, "reg", &len); |
| 641 | if (prop) { |
Stefan Roese | 412a71a | 2010-09-16 14:01:53 +0200 | [diff] [blame] | 642 | int tuple_size = 3 * sizeof(reg); |
| 643 | |
| 644 | /* |
| 645 | * There might be multiple reg-tuples, |
| 646 | * so loop through them all |
| 647 | */ |
Stefan Roese | 442cd1a | 2010-09-24 13:51:50 +0200 | [diff] [blame] | 648 | reg = reg2 = (u32 *)&prop->data[0]; |
| 649 | for (idx = 0; idx < (len / tuple_size); idx++) { |
Stefan Roese | 412a71a | 2010-09-16 14:01:53 +0200 | [diff] [blame] | 650 | /* |
| 651 | * Update size in reg property |
| 652 | */ |
| 653 | reg[2] = flash_get_bank_size(reg[0], |
| 654 | idx); |
Stefan Roese | 442cd1a | 2010-09-24 13:51:50 +0200 | [diff] [blame] | 655 | |
| 656 | /* |
| 657 | * Point to next reg tuple |
| 658 | */ |
| 659 | reg += 3; |
Stefan Roese | a0f10c9 | 2009-10-21 11:59:52 +0200 | [diff] [blame] | 660 | } |
Stefan Roese | 442cd1a | 2010-09-24 13:51:50 +0200 | [diff] [blame] | 661 | |
| 662 | fdt_setprop(blob, off, "reg", reg2, len); |
Stefan Roese | a0f10c9 | 2009-10-21 11:59:52 +0200 | [diff] [blame] | 663 | } |
| 664 | |
| 665 | /* Move to next compatible node */ |
| 666 | off = fdt_node_offset_by_compatible(blob, off, |
| 667 | compat[i]); |
| 668 | } |
| 669 | } |
| 670 | |
Stefan Roese | 412a71a | 2010-09-16 14:01:53 +0200 | [diff] [blame] | 671 | return 0; |
Stefan Roese | a0f10c9 | 2009-10-21 11:59:52 +0200 | [diff] [blame] | 672 | } |
| 673 | #endif |
Anatolij Gustschin | 6c44c38 | 2010-03-16 17:10:05 +0100 | [diff] [blame] | 674 | |
Matthew McClintock | acda3a4 | 2010-10-13 13:39:26 +0200 | [diff] [blame] | 675 | int fdt_increase_size(void *fdt, int add_len) |
| 676 | { |
| 677 | int newlen; |
| 678 | |
| 679 | newlen = fdt_totalsize(fdt) + add_len; |
| 680 | |
| 681 | /* Open in place with a new len */ |
| 682 | return fdt_open_into(fdt, fdt, newlen); |
| 683 | } |
| 684 | |
Anatolij Gustschin | 6c44c38 | 2010-03-16 17:10:05 +0100 | [diff] [blame] | 685 | #ifdef CONFIG_FDT_FIXUP_PARTITIONS |
| 686 | #include <jffs2/load_kernel.h> |
| 687 | #include <mtd_node.h> |
| 688 | |
| 689 | struct reg_cell { |
| 690 | unsigned int r0; |
| 691 | unsigned int r1; |
| 692 | }; |
| 693 | |
| 694 | int fdt_del_subnodes(const void *blob, int parent_offset) |
| 695 | { |
| 696 | int off, ndepth; |
| 697 | int ret; |
| 698 | |
| 699 | for (ndepth = 0, off = fdt_next_node(blob, parent_offset, &ndepth); |
| 700 | (off >= 0) && (ndepth > 0); |
| 701 | off = fdt_next_node(blob, off, &ndepth)) { |
| 702 | if (ndepth == 1) { |
| 703 | debug("delete %s: offset: %x\n", |
| 704 | fdt_get_name(blob, off, 0), off); |
| 705 | ret = fdt_del_node((void *)blob, off); |
| 706 | if (ret < 0) { |
| 707 | printf("Can't delete node: %s\n", |
| 708 | fdt_strerror(ret)); |
| 709 | return ret; |
| 710 | } else { |
| 711 | ndepth = 0; |
| 712 | off = parent_offset; |
| 713 | } |
| 714 | } |
| 715 | } |
| 716 | return 0; |
| 717 | } |
| 718 | |
Anatolij Gustschin | 6c44c38 | 2010-03-16 17:10:05 +0100 | [diff] [blame] | 719 | int fdt_del_partitions(void *blob, int parent_offset) |
| 720 | { |
| 721 | const void *prop; |
| 722 | int ndepth = 0; |
| 723 | int off; |
| 724 | int ret; |
| 725 | |
| 726 | off = fdt_next_node(blob, parent_offset, &ndepth); |
| 727 | if (off > 0 && ndepth == 1) { |
| 728 | prop = fdt_getprop(blob, off, "label", NULL); |
| 729 | if (prop == NULL) { |
| 730 | /* |
| 731 | * Could not find label property, nand {}; node? |
| 732 | * Check subnode, delete partitions there if any. |
| 733 | */ |
| 734 | return fdt_del_partitions(blob, off); |
| 735 | } else { |
| 736 | ret = fdt_del_subnodes(blob, parent_offset); |
| 737 | if (ret < 0) { |
| 738 | printf("Can't remove subnodes: %s\n", |
| 739 | fdt_strerror(ret)); |
| 740 | return ret; |
| 741 | } |
| 742 | } |
| 743 | } |
| 744 | return 0; |
| 745 | } |
| 746 | |
| 747 | int fdt_node_set_part_info(void *blob, int parent_offset, |
| 748 | struct mtd_device *dev) |
| 749 | { |
| 750 | struct list_head *pentry; |
| 751 | struct part_info *part; |
| 752 | struct reg_cell cell; |
| 753 | int off, ndepth = 0; |
| 754 | int part_num, ret; |
| 755 | char buf[64]; |
| 756 | |
| 757 | ret = fdt_del_partitions(blob, parent_offset); |
| 758 | if (ret < 0) |
| 759 | return ret; |
| 760 | |
| 761 | /* |
| 762 | * Check if it is nand {}; subnode, adjust |
| 763 | * the offset in this case |
| 764 | */ |
| 765 | off = fdt_next_node(blob, parent_offset, &ndepth); |
| 766 | if (off > 0 && ndepth == 1) |
| 767 | parent_offset = off; |
| 768 | |
| 769 | part_num = 0; |
| 770 | list_for_each_prev(pentry, &dev->parts) { |
| 771 | int newoff; |
| 772 | |
| 773 | part = list_entry(pentry, struct part_info, link); |
| 774 | |
| 775 | debug("%2d: %-20s0x%08x\t0x%08x\t%d\n", |
| 776 | part_num, part->name, part->size, |
| 777 | part->offset, part->mask_flags); |
| 778 | |
| 779 | sprintf(buf, "partition@%x", part->offset); |
| 780 | add_sub: |
| 781 | ret = fdt_add_subnode(blob, parent_offset, buf); |
| 782 | if (ret == -FDT_ERR_NOSPACE) { |
| 783 | ret = fdt_increase_size(blob, 512); |
| 784 | if (!ret) |
| 785 | goto add_sub; |
| 786 | else |
| 787 | goto err_size; |
| 788 | } else if (ret < 0) { |
| 789 | printf("Can't add partition node: %s\n", |
| 790 | fdt_strerror(ret)); |
| 791 | return ret; |
| 792 | } |
| 793 | newoff = ret; |
| 794 | |
| 795 | /* Check MTD_WRITEABLE_CMD flag */ |
| 796 | if (part->mask_flags & 1) { |
| 797 | add_ro: |
| 798 | ret = fdt_setprop(blob, newoff, "read_only", NULL, 0); |
| 799 | if (ret == -FDT_ERR_NOSPACE) { |
| 800 | ret = fdt_increase_size(blob, 512); |
| 801 | if (!ret) |
| 802 | goto add_ro; |
| 803 | else |
| 804 | goto err_size; |
| 805 | } else if (ret < 0) |
| 806 | goto err_prop; |
| 807 | } |
| 808 | |
| 809 | cell.r0 = cpu_to_fdt32(part->offset); |
| 810 | cell.r1 = cpu_to_fdt32(part->size); |
| 811 | add_reg: |
| 812 | ret = fdt_setprop(blob, newoff, "reg", &cell, sizeof(cell)); |
| 813 | if (ret == -FDT_ERR_NOSPACE) { |
| 814 | ret = fdt_increase_size(blob, 512); |
| 815 | if (!ret) |
| 816 | goto add_reg; |
| 817 | else |
| 818 | goto err_size; |
| 819 | } else if (ret < 0) |
| 820 | goto err_prop; |
| 821 | |
| 822 | add_label: |
| 823 | ret = fdt_setprop_string(blob, newoff, "label", part->name); |
| 824 | if (ret == -FDT_ERR_NOSPACE) { |
| 825 | ret = fdt_increase_size(blob, 512); |
| 826 | if (!ret) |
| 827 | goto add_label; |
| 828 | else |
| 829 | goto err_size; |
| 830 | } else if (ret < 0) |
| 831 | goto err_prop; |
| 832 | |
| 833 | part_num++; |
| 834 | } |
| 835 | return 0; |
| 836 | err_size: |
| 837 | printf("Can't increase blob size: %s\n", fdt_strerror(ret)); |
| 838 | return ret; |
| 839 | err_prop: |
| 840 | printf("Can't add property: %s\n", fdt_strerror(ret)); |
| 841 | return ret; |
| 842 | } |
| 843 | |
| 844 | /* |
| 845 | * Update partitions in nor/nand nodes using info from |
| 846 | * mtdparts environment variable. The nodes to update are |
| 847 | * specified by node_info structure which contains mtd device |
| 848 | * type and compatible string: E. g. the board code in |
| 849 | * ft_board_setup() could use: |
| 850 | * |
| 851 | * struct node_info nodes[] = { |
| 852 | * { "fsl,mpc5121-nfc", MTD_DEV_TYPE_NAND, }, |
| 853 | * { "cfi-flash", MTD_DEV_TYPE_NOR, }, |
| 854 | * }; |
| 855 | * |
| 856 | * fdt_fixup_mtdparts(blob, nodes, ARRAY_SIZE(nodes)); |
| 857 | */ |
| 858 | void fdt_fixup_mtdparts(void *blob, void *node_info, int node_info_size) |
| 859 | { |
| 860 | struct node_info *ni = node_info; |
| 861 | struct mtd_device *dev; |
| 862 | char *parts; |
| 863 | int i, idx; |
| 864 | int noff; |
| 865 | |
| 866 | parts = getenv("mtdparts"); |
| 867 | if (!parts) |
| 868 | return; |
| 869 | |
| 870 | if (mtdparts_init() != 0) |
| 871 | return; |
| 872 | |
| 873 | for (i = 0; i < node_info_size; i++) { |
| 874 | idx = 0; |
| 875 | noff = fdt_node_offset_by_compatible(blob, -1, ni[i].compat); |
| 876 | while (noff != -FDT_ERR_NOTFOUND) { |
| 877 | debug("%s: %s, mtd dev type %d\n", |
| 878 | fdt_get_name(blob, noff, 0), |
| 879 | ni[i].compat, ni[i].type); |
| 880 | dev = device_find(ni[i].type, idx++); |
| 881 | if (dev) { |
| 882 | if (fdt_node_set_part_info(blob, noff, dev)) |
| 883 | return; /* return on error */ |
| 884 | } |
| 885 | |
| 886 | /* Jump to next flash node */ |
| 887 | noff = fdt_node_offset_by_compatible(blob, noff, |
| 888 | ni[i].compat); |
| 889 | } |
| 890 | } |
| 891 | } |
| 892 | #endif |
Kumar Gala | f4ad4fd | 2010-03-30 10:19:26 -0500 | [diff] [blame] | 893 | |
| 894 | void fdt_del_node_and_alias(void *blob, const char *alias) |
| 895 | { |
| 896 | int off = fdt_path_offset(blob, alias); |
| 897 | |
| 898 | if (off < 0) |
| 899 | return; |
| 900 | |
| 901 | fdt_del_node(blob, off); |
| 902 | |
| 903 | off = fdt_path_offset(blob, "/aliases"); |
| 904 | fdt_delprop(blob, off, alias); |
| 905 | } |
Kumar Gala | b87e793 | 2010-06-16 14:27:38 -0500 | [diff] [blame] | 906 | |
| 907 | /* Helper to read a big number; size is in cells (not bytes) */ |
| 908 | static inline u64 of_read_number(const __be32 *cell, int size) |
| 909 | { |
| 910 | u64 r = 0; |
| 911 | while (size--) |
| 912 | r = (r << 32) | be32_to_cpu(*(cell++)); |
| 913 | return r; |
| 914 | } |
| 915 | |
Kumar Gala | b87e793 | 2010-06-16 14:27:38 -0500 | [diff] [blame] | 916 | #define PRu64 "%llx" |
| 917 | |
| 918 | /* Max address size we deal with */ |
| 919 | #define OF_MAX_ADDR_CELLS 4 |
| 920 | #define OF_BAD_ADDR ((u64)-1) |
| 921 | #define OF_CHECK_COUNTS(na, ns) ((na) > 0 && (na) <= OF_MAX_ADDR_CELLS && \ |
| 922 | (ns) > 0) |
| 923 | |
| 924 | /* Debug utility */ |
| 925 | #ifdef DEBUG |
| 926 | static void of_dump_addr(const char *s, const u32 *addr, int na) |
| 927 | { |
| 928 | printf("%s", s); |
| 929 | while(na--) |
| 930 | printf(" %08x", *(addr++)); |
| 931 | printf("\n"); |
| 932 | } |
| 933 | #else |
| 934 | static void of_dump_addr(const char *s, const u32 *addr, int na) { } |
| 935 | #endif |
| 936 | |
| 937 | /* Callbacks for bus specific translators */ |
| 938 | struct of_bus { |
| 939 | const char *name; |
| 940 | const char *addresses; |
Scott Wood | 9989971 | 2010-08-12 18:37:39 -0500 | [diff] [blame] | 941 | void (*count_cells)(void *blob, int parentoffset, |
Kumar Gala | b87e793 | 2010-06-16 14:27:38 -0500 | [diff] [blame] | 942 | int *addrc, int *sizec); |
| 943 | u64 (*map)(u32 *addr, const u32 *range, |
| 944 | int na, int ns, int pna); |
| 945 | int (*translate)(u32 *addr, u64 offset, int na); |
| 946 | }; |
| 947 | |
| 948 | /* Default translator (generic bus) */ |
Scott Wood | 9989971 | 2010-08-12 18:37:39 -0500 | [diff] [blame] | 949 | static void of_bus_default_count_cells(void *blob, int parentoffset, |
Kumar Gala | b87e793 | 2010-06-16 14:27:38 -0500 | [diff] [blame] | 950 | int *addrc, int *sizec) |
| 951 | { |
Scott Wood | 9989971 | 2010-08-12 18:37:39 -0500 | [diff] [blame] | 952 | const u32 *prop; |
| 953 | |
| 954 | if (addrc) { |
| 955 | prop = fdt_getprop(blob, parentoffset, "#address-cells", NULL); |
| 956 | if (prop) |
Dirk Behme | 4169ea0 | 2010-11-21 20:19:34 +0100 | [diff] [blame^] | 957 | *addrc = be32_to_cpup((u32 *)prop); |
Scott Wood | 9989971 | 2010-08-12 18:37:39 -0500 | [diff] [blame] | 958 | else |
| 959 | *addrc = 2; |
| 960 | } |
| 961 | |
| 962 | if (sizec) { |
| 963 | prop = fdt_getprop(blob, parentoffset, "#size-cells", NULL); |
| 964 | if (prop) |
Dirk Behme | 4169ea0 | 2010-11-21 20:19:34 +0100 | [diff] [blame^] | 965 | *sizec = be32_to_cpup((u32 *)prop); |
Scott Wood | 9989971 | 2010-08-12 18:37:39 -0500 | [diff] [blame] | 966 | else |
| 967 | *sizec = 1; |
| 968 | } |
Kumar Gala | b87e793 | 2010-06-16 14:27:38 -0500 | [diff] [blame] | 969 | } |
| 970 | |
| 971 | static u64 of_bus_default_map(u32 *addr, const u32 *range, |
| 972 | int na, int ns, int pna) |
| 973 | { |
| 974 | u64 cp, s, da; |
| 975 | |
| 976 | cp = of_read_number(range, na); |
| 977 | s = of_read_number(range + na + pna, ns); |
| 978 | da = of_read_number(addr, na); |
| 979 | |
| 980 | debug("OF: default map, cp="PRu64", s="PRu64", da="PRu64"\n", |
| 981 | cp, s, da); |
| 982 | |
| 983 | if (da < cp || da >= (cp + s)) |
| 984 | return OF_BAD_ADDR; |
| 985 | return da - cp; |
| 986 | } |
| 987 | |
| 988 | static int of_bus_default_translate(u32 *addr, u64 offset, int na) |
| 989 | { |
| 990 | u64 a = of_read_number(addr, na); |
| 991 | memset(addr, 0, na * 4); |
| 992 | a += offset; |
| 993 | if (na > 1) |
| 994 | addr[na - 2] = a >> 32; |
| 995 | addr[na - 1] = a & 0xffffffffu; |
| 996 | |
| 997 | return 0; |
| 998 | } |
| 999 | |
| 1000 | /* Array of bus specific translators */ |
| 1001 | static struct of_bus of_busses[] = { |
| 1002 | /* Default */ |
| 1003 | { |
| 1004 | .name = "default", |
| 1005 | .addresses = "reg", |
| 1006 | .count_cells = of_bus_default_count_cells, |
| 1007 | .map = of_bus_default_map, |
| 1008 | .translate = of_bus_default_translate, |
| 1009 | }, |
| 1010 | }; |
| 1011 | |
| 1012 | static int of_translate_one(void * blob, int parent, struct of_bus *bus, |
| 1013 | struct of_bus *pbus, u32 *addr, |
| 1014 | int na, int ns, int pna, const char *rprop) |
| 1015 | { |
| 1016 | const u32 *ranges; |
| 1017 | int rlen; |
| 1018 | int rone; |
| 1019 | u64 offset = OF_BAD_ADDR; |
| 1020 | |
| 1021 | /* Normally, an absence of a "ranges" property means we are |
| 1022 | * crossing a non-translatable boundary, and thus the addresses |
| 1023 | * below the current not cannot be converted to CPU physical ones. |
| 1024 | * Unfortunately, while this is very clear in the spec, it's not |
| 1025 | * what Apple understood, and they do have things like /uni-n or |
| 1026 | * /ht nodes with no "ranges" property and a lot of perfectly |
| 1027 | * useable mapped devices below them. Thus we treat the absence of |
| 1028 | * "ranges" as equivalent to an empty "ranges" property which means |
| 1029 | * a 1:1 translation at that level. It's up to the caller not to try |
| 1030 | * to translate addresses that aren't supposed to be translated in |
| 1031 | * the first place. --BenH. |
| 1032 | */ |
| 1033 | ranges = (u32 *)fdt_getprop(blob, parent, rprop, &rlen); |
| 1034 | if (ranges == NULL || rlen == 0) { |
| 1035 | offset = of_read_number(addr, na); |
| 1036 | memset(addr, 0, pna * 4); |
| 1037 | debug("OF: no ranges, 1:1 translation\n"); |
| 1038 | goto finish; |
| 1039 | } |
| 1040 | |
| 1041 | debug("OF: walking ranges...\n"); |
| 1042 | |
| 1043 | /* Now walk through the ranges */ |
| 1044 | rlen /= 4; |
| 1045 | rone = na + pna + ns; |
| 1046 | for (; rlen >= rone; rlen -= rone, ranges += rone) { |
| 1047 | offset = bus->map(addr, ranges, na, ns, pna); |
| 1048 | if (offset != OF_BAD_ADDR) |
| 1049 | break; |
| 1050 | } |
| 1051 | if (offset == OF_BAD_ADDR) { |
| 1052 | debug("OF: not found !\n"); |
| 1053 | return 1; |
| 1054 | } |
| 1055 | memcpy(addr, ranges + na, 4 * pna); |
| 1056 | |
| 1057 | finish: |
| 1058 | of_dump_addr("OF: parent translation for:", addr, pna); |
| 1059 | debug("OF: with offset: "PRu64"\n", offset); |
| 1060 | |
| 1061 | /* Translate it into parent bus space */ |
| 1062 | return pbus->translate(addr, offset, pna); |
| 1063 | } |
| 1064 | |
| 1065 | /* |
| 1066 | * Translate an address from the device-tree into a CPU physical address, |
| 1067 | * this walks up the tree and applies the various bus mappings on the |
| 1068 | * way. |
| 1069 | * |
| 1070 | * Note: We consider that crossing any level with #size-cells == 0 to mean |
| 1071 | * that translation is impossible (that is we are not dealing with a value |
| 1072 | * that can be mapped to a cpu physical address). This is not really specified |
| 1073 | * that way, but this is traditionally the way IBM at least do things |
| 1074 | */ |
| 1075 | u64 __of_translate_address(void *blob, int node_offset, const u32 *in_addr, |
| 1076 | const char *rprop) |
| 1077 | { |
| 1078 | int parent; |
| 1079 | struct of_bus *bus, *pbus; |
| 1080 | u32 addr[OF_MAX_ADDR_CELLS]; |
| 1081 | int na, ns, pna, pns; |
| 1082 | u64 result = OF_BAD_ADDR; |
| 1083 | |
| 1084 | debug("OF: ** translation for device %s **\n", |
| 1085 | fdt_get_name(blob, node_offset, NULL)); |
| 1086 | |
| 1087 | /* Get parent & match bus type */ |
| 1088 | parent = fdt_parent_offset(blob, node_offset); |
| 1089 | if (parent < 0) |
| 1090 | goto bail; |
| 1091 | bus = &of_busses[0]; |
| 1092 | |
| 1093 | /* Cound address cells & copy address locally */ |
Scott Wood | 9989971 | 2010-08-12 18:37:39 -0500 | [diff] [blame] | 1094 | bus->count_cells(blob, parent, &na, &ns); |
Kumar Gala | b87e793 | 2010-06-16 14:27:38 -0500 | [diff] [blame] | 1095 | if (!OF_CHECK_COUNTS(na, ns)) { |
| 1096 | printf("%s: Bad cell count for %s\n", __FUNCTION__, |
| 1097 | fdt_get_name(blob, node_offset, NULL)); |
| 1098 | goto bail; |
| 1099 | } |
| 1100 | memcpy(addr, in_addr, na * 4); |
| 1101 | |
| 1102 | debug("OF: bus is %s (na=%d, ns=%d) on %s\n", |
| 1103 | bus->name, na, ns, fdt_get_name(blob, parent, NULL)); |
| 1104 | of_dump_addr("OF: translating address:", addr, na); |
| 1105 | |
| 1106 | /* Translate */ |
| 1107 | for (;;) { |
| 1108 | /* Switch to parent bus */ |
| 1109 | node_offset = parent; |
| 1110 | parent = fdt_parent_offset(blob, node_offset); |
| 1111 | |
| 1112 | /* If root, we have finished */ |
| 1113 | if (parent < 0) { |
| 1114 | debug("OF: reached root node\n"); |
| 1115 | result = of_read_number(addr, na); |
| 1116 | break; |
| 1117 | } |
| 1118 | |
| 1119 | /* Get new parent bus and counts */ |
| 1120 | pbus = &of_busses[0]; |
Scott Wood | 9989971 | 2010-08-12 18:37:39 -0500 | [diff] [blame] | 1121 | pbus->count_cells(blob, parent, &pna, &pns); |
Kumar Gala | b87e793 | 2010-06-16 14:27:38 -0500 | [diff] [blame] | 1122 | if (!OF_CHECK_COUNTS(pna, pns)) { |
| 1123 | printf("%s: Bad cell count for %s\n", __FUNCTION__, |
| 1124 | fdt_get_name(blob, node_offset, NULL)); |
| 1125 | break; |
| 1126 | } |
| 1127 | |
| 1128 | debug("OF: parent bus is %s (na=%d, ns=%d) on %s\n", |
| 1129 | pbus->name, pna, pns, fdt_get_name(blob, parent, NULL)); |
| 1130 | |
| 1131 | /* Apply bus translation */ |
| 1132 | if (of_translate_one(blob, node_offset, bus, pbus, |
| 1133 | addr, na, ns, pna, rprop)) |
| 1134 | break; |
| 1135 | |
| 1136 | /* Complete the move up one level */ |
| 1137 | na = pna; |
| 1138 | ns = pns; |
| 1139 | bus = pbus; |
| 1140 | |
| 1141 | of_dump_addr("OF: one level translation:", addr, na); |
| 1142 | } |
| 1143 | bail: |
| 1144 | |
| 1145 | return result; |
| 1146 | } |
| 1147 | |
| 1148 | u64 fdt_translate_address(void *blob, int node_offset, const u32 *in_addr) |
| 1149 | { |
| 1150 | return __of_translate_address(blob, node_offset, in_addr, "ranges"); |
| 1151 | } |
Kumar Gala | 800d1d1 | 2010-07-04 12:48:21 -0500 | [diff] [blame] | 1152 | |
| 1153 | /** |
| 1154 | * fdt_node_offset_by_compat_reg: Find a node that matches compatiable and |
| 1155 | * who's reg property matches a physical cpu address |
| 1156 | * |
| 1157 | * @blob: ptr to device tree |
| 1158 | * @compat: compatiable string to match |
| 1159 | * @compat_off: property name |
| 1160 | * |
| 1161 | */ |
| 1162 | int fdt_node_offset_by_compat_reg(void *blob, const char *compat, |
| 1163 | phys_addr_t compat_off) |
| 1164 | { |
| 1165 | int len, off = fdt_node_offset_by_compatible(blob, -1, compat); |
| 1166 | while (off != -FDT_ERR_NOTFOUND) { |
| 1167 | u32 *reg = (u32 *)fdt_getprop(blob, off, "reg", &len); |
| 1168 | if (reg) { |
| 1169 | if (compat_off == fdt_translate_address(blob, off, reg)) |
| 1170 | return off; |
| 1171 | } |
| 1172 | off = fdt_node_offset_by_compatible(blob, off, compat); |
| 1173 | } |
| 1174 | |
| 1175 | return -FDT_ERR_NOTFOUND; |
| 1176 | } |
| 1177 | |
Kumar Gala | b7afb11 | 2010-07-09 16:18:58 -0500 | [diff] [blame] | 1178 | /** |
| 1179 | * fdt_alloc_phandle: Return next free phandle value |
| 1180 | * |
| 1181 | * @blob: ptr to device tree |
| 1182 | */ |
| 1183 | int fdt_alloc_phandle(void *blob) |
| 1184 | { |
| 1185 | int offset, len, phandle = 0; |
| 1186 | const u32 *val; |
| 1187 | |
| 1188 | for (offset = fdt_next_node(blob, -1, NULL); offset >= 0; |
| 1189 | offset = fdt_next_node(blob, offset, NULL)) { |
| 1190 | val = fdt_getprop(blob, offset, "linux,phandle", &len); |
| 1191 | if (val) |
| 1192 | phandle = max(*val, phandle); |
| 1193 | } |
Kumar Gala | 800d1d1 | 2010-07-04 12:48:21 -0500 | [diff] [blame] | 1194 | |
Kumar Gala | b7afb11 | 2010-07-09 16:18:58 -0500 | [diff] [blame] | 1195 | return phandle + 1; |
| 1196 | } |
Anatolij Gustschin | 3b857f1 | 2010-08-18 11:25:20 +0200 | [diff] [blame] | 1197 | |
| 1198 | #if defined(CONFIG_VIDEO) |
| 1199 | int fdt_add_edid(void *blob, const char *compat, unsigned char *edid_buf) |
| 1200 | { |
| 1201 | int noff; |
| 1202 | int ret; |
| 1203 | |
| 1204 | noff = fdt_node_offset_by_compatible(blob, -1, compat); |
| 1205 | if (noff != -FDT_ERR_NOTFOUND) { |
| 1206 | debug("%s: %s\n", fdt_get_name(blob, noff, 0), compat); |
| 1207 | add_edid: |
| 1208 | ret = fdt_setprop(blob, noff, "edid", edid_buf, 128); |
| 1209 | if (ret == -FDT_ERR_NOSPACE) { |
| 1210 | ret = fdt_increase_size(blob, 512); |
| 1211 | if (!ret) |
| 1212 | goto add_edid; |
| 1213 | else |
| 1214 | goto err_size; |
| 1215 | } else if (ret < 0) { |
| 1216 | printf("Can't add property: %s\n", fdt_strerror(ret)); |
| 1217 | return ret; |
| 1218 | } |
| 1219 | } |
| 1220 | return 0; |
| 1221 | err_size: |
| 1222 | printf("Can't increase blob size: %s\n", fdt_strerror(ret)); |
| 1223 | return ret; |
| 1224 | } |
| 1225 | #endif |