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