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