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