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