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