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