Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Gerald Van Baren | c4a57ea | 2007-04-06 14:19:43 -0400 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2007 |
| 4 | * Gerald Van Baren, Custom IDEAS, vanbaren@cideas.com |
| 5 | * |
Shengzhou Liu | a7be802 | 2011-10-14 16:26:05 +0800 | [diff] [blame] | 6 | * Copyright 2010-2011 Freescale Semiconductor, Inc. |
Gerald Van Baren | c4a57ea | 2007-04-06 14:19:43 -0400 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include <common.h> |
Simon Glass | 0af6e2d | 2019-08-01 09:46:52 -0600 | [diff] [blame] | 10 | #include <env.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 11 | #include <log.h> |
Heinrich Schuchardt | 668cabc | 2018-11-18 17:58:51 +0100 | [diff] [blame] | 12 | #include <mapmem.h> |
Simon Glass | 274e0b0 | 2020-05-10 11:39:56 -0600 | [diff] [blame] | 13 | #include <net.h> |
Anton Vorontsov | 9e9cf60 | 2009-10-15 17:47:04 +0400 | [diff] [blame] | 14 | #include <stdio_dev.h> |
Gerald Van Baren | c4a57ea | 2007-04-06 14:19:43 -0400 | [diff] [blame] | 15 | #include <linux/ctype.h> |
| 16 | #include <linux/types.h> |
Gerald Van Baren | c4a57ea | 2007-04-06 14:19:43 -0400 | [diff] [blame] | 17 | #include <asm/global_data.h> |
Masahiro Yamada | 75f82d0 | 2018-03-05 01:20:11 +0900 | [diff] [blame] | 18 | #include <linux/libfdt.h> |
Gerald Van Baren | c4a57ea | 2007-04-06 14:19:43 -0400 | [diff] [blame] | 19 | #include <fdt_support.h> |
Kumar Gala | 2dc9b4a | 2007-11-26 17:06:15 -0600 | [diff] [blame] | 20 | #include <exports.h> |
Bin Meng | d9a0093 | 2015-12-07 01:39:47 -0800 | [diff] [blame] | 21 | #include <fdtdec.h> |
Francesco Dolcini | 05cc827 | 2022-05-19 16:22:26 +0200 | [diff] [blame] | 22 | #include <version.h> |
Gerald Van Baren | c4a57ea | 2007-04-06 14:19:43 -0400 | [diff] [blame] | 23 | |
Kumar Gala | 1ba0040 | 2008-10-23 00:05:47 -0500 | [diff] [blame] | 24 | /** |
Alexander Graf | 7c8a6cf | 2014-04-11 17:09:40 +0200 | [diff] [blame] | 25 | * fdt_getprop_u32_default_node - Return a node's property or a default |
| 26 | * |
| 27 | * @fdt: ptr to device tree |
| 28 | * @off: offset of node |
| 29 | * @cell: cell offset in property |
| 30 | * @prop: property name |
| 31 | * @dflt: default value if the property isn't found |
| 32 | * |
| 33 | * Convenience function to return a node's property or a default value if |
| 34 | * the property doesn't exist. |
| 35 | */ |
| 36 | u32 fdt_getprop_u32_default_node(const void *fdt, int off, int cell, |
| 37 | const char *prop, const u32 dflt) |
| 38 | { |
| 39 | const fdt32_t *val; |
| 40 | int len; |
| 41 | |
| 42 | val = fdt_getprop(fdt, off, prop, &len); |
| 43 | |
| 44 | /* Check if property exists */ |
| 45 | if (!val) |
| 46 | return dflt; |
| 47 | |
| 48 | /* Check if property is long enough */ |
| 49 | if (len < ((cell + 1) * sizeof(uint32_t))) |
| 50 | return dflt; |
| 51 | |
| 52 | return fdt32_to_cpu(*val); |
| 53 | } |
| 54 | |
| 55 | /** |
Kumar Gala | 1ba0040 | 2008-10-23 00:05:47 -0500 | [diff] [blame] | 56 | * fdt_getprop_u32_default - Find a node and return it's property or a default |
| 57 | * |
| 58 | * @fdt: ptr to device tree |
| 59 | * @path: path of node |
| 60 | * @prop: property name |
| 61 | * @dflt: default value if the property isn't found |
| 62 | * |
| 63 | * Convenience function to find a node and return it's property or a |
| 64 | * default value if it doesn't exist. |
| 65 | */ |
Gabe Black | cd37de7 | 2011-11-08 01:09:44 -0800 | [diff] [blame] | 66 | u32 fdt_getprop_u32_default(const void *fdt, const char *path, |
| 67 | const char *prop, const u32 dflt) |
Kumar Gala | 1ba0040 | 2008-10-23 00:05:47 -0500 | [diff] [blame] | 68 | { |
Kumar Gala | 1ba0040 | 2008-10-23 00:05:47 -0500 | [diff] [blame] | 69 | int off; |
| 70 | |
| 71 | off = fdt_path_offset(fdt, path); |
| 72 | if (off < 0) |
| 73 | return dflt; |
| 74 | |
Alexander Graf | 7c8a6cf | 2014-04-11 17:09:40 +0200 | [diff] [blame] | 75 | return fdt_getprop_u32_default_node(fdt, off, 0, prop, dflt); |
Kumar Gala | 1ba0040 | 2008-10-23 00:05:47 -0500 | [diff] [blame] | 76 | } |
Gerald Van Baren | c4a57ea | 2007-04-06 14:19:43 -0400 | [diff] [blame] | 77 | |
Kumar Gala | bf771dc | 2007-10-24 10:21:57 -0500 | [diff] [blame] | 78 | /** |
| 79 | * fdt_find_and_setprop: Find a node and set it's property |
| 80 | * |
| 81 | * @fdt: ptr to device tree |
| 82 | * @node: path of node |
| 83 | * @prop: property name |
| 84 | * @val: ptr to new value |
| 85 | * @len: length of new property value |
| 86 | * @create: flag to create the property if it doesn't exist |
| 87 | * |
| 88 | * Convenience function to directly set a property given the path to the node. |
| 89 | */ |
| 90 | int fdt_find_and_setprop(void *fdt, const char *node, const char *prop, |
| 91 | const void *val, int len, int create) |
| 92 | { |
Kumar Gala | c8ab705 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 93 | int nodeoff = fdt_path_offset(fdt, node); |
Kumar Gala | bf771dc | 2007-10-24 10:21:57 -0500 | [diff] [blame] | 94 | |
| 95 | if (nodeoff < 0) |
| 96 | return nodeoff; |
| 97 | |
Kim Phillips | 6542c07 | 2013-01-16 14:00:11 +0000 | [diff] [blame] | 98 | if ((!create) && (fdt_get_property(fdt, nodeoff, prop, NULL) == NULL)) |
Kumar Gala | bf771dc | 2007-10-24 10:21:57 -0500 | [diff] [blame] | 99 | return 0; /* create flag not set; so exit quietly */ |
| 100 | |
| 101 | return fdt_setprop(fdt, nodeoff, prop, val, len); |
| 102 | } |
| 103 | |
Masahiro Yamada | c020728 | 2014-04-18 17:40:58 +0900 | [diff] [blame] | 104 | /** |
Simon Glass | 014aa05 | 2014-10-23 18:58:49 -0600 | [diff] [blame] | 105 | * fdt_find_or_add_subnode() - find or possibly add a subnode of a given node |
| 106 | * |
Masahiro Yamada | c020728 | 2014-04-18 17:40:58 +0900 | [diff] [blame] | 107 | * @fdt: pointer to the device tree blob |
| 108 | * @parentoffset: structure block offset of a node |
| 109 | * @name: name of the subnode to locate |
| 110 | * |
| 111 | * fdt_subnode_offset() finds a subnode of the node with a given name. |
| 112 | * If the subnode does not exist, it will be created. |
| 113 | */ |
Simon Glass | 014aa05 | 2014-10-23 18:58:49 -0600 | [diff] [blame] | 114 | int fdt_find_or_add_subnode(void *fdt, int parentoffset, const char *name) |
Masahiro Yamada | c020728 | 2014-04-18 17:40:58 +0900 | [diff] [blame] | 115 | { |
| 116 | int offset; |
| 117 | |
| 118 | offset = fdt_subnode_offset(fdt, parentoffset, name); |
| 119 | |
| 120 | if (offset == -FDT_ERR_NOTFOUND) |
| 121 | offset = fdt_add_subnode(fdt, parentoffset, name); |
| 122 | |
| 123 | if (offset < 0) |
| 124 | printf("%s: %s: %s\n", __func__, name, fdt_strerror(offset)); |
| 125 | |
| 126 | return offset; |
| 127 | } |
| 128 | |
Andre Przywara | 0d1fcf8 | 2021-02-14 10:35:18 +0000 | [diff] [blame] | 129 | #if defined(CONFIG_OF_STDOUT_VIA_ALIAS) && defined(CONFIG_CONS_INDEX) |
Detlev Zundel | 46e192a | 2008-06-20 22:24:05 +0200 | [diff] [blame] | 130 | static int fdt_fixup_stdout(void *fdt, int chosenoff) |
Kumar Gala | 2dc9b4a | 2007-11-26 17:06:15 -0600 | [diff] [blame] | 131 | { |
Masahiro Yamada | a467792c6 | 2014-04-18 17:41:01 +0900 | [diff] [blame] | 132 | int err; |
| 133 | int aliasoff; |
Kumar Gala | 2dc9b4a | 2007-11-26 17:06:15 -0600 | [diff] [blame] | 134 | char sername[9] = { 0 }; |
Masahiro Yamada | a467792c6 | 2014-04-18 17:41:01 +0900 | [diff] [blame] | 135 | const void *path; |
| 136 | int len; |
| 137 | char tmp[256]; /* long enough */ |
Kumar Gala | 2dc9b4a | 2007-11-26 17:06:15 -0600 | [diff] [blame] | 138 | |
Bin Meng | b819911 | 2016-01-13 19:38:58 -0800 | [diff] [blame] | 139 | sprintf(sername, "serial%d", CONFIG_CONS_INDEX - 1); |
Kumar Gala | 2dc9b4a | 2007-11-26 17:06:15 -0600 | [diff] [blame] | 140 | |
Masahiro Yamada | a467792c6 | 2014-04-18 17:41:01 +0900 | [diff] [blame] | 141 | aliasoff = fdt_path_offset(fdt, "/aliases"); |
| 142 | if (aliasoff < 0) { |
| 143 | err = aliasoff; |
Scott Wood | edd551f | 2015-09-01 22:48:08 -0500 | [diff] [blame] | 144 | goto noalias; |
Kumar Gala | 2dc9b4a | 2007-11-26 17:06:15 -0600 | [diff] [blame] | 145 | } |
Masahiro Yamada | a467792c6 | 2014-04-18 17:41:01 +0900 | [diff] [blame] | 146 | |
| 147 | path = fdt_getprop(fdt, aliasoff, sername, &len); |
| 148 | if (!path) { |
| 149 | err = len; |
Scott Wood | edd551f | 2015-09-01 22:48:08 -0500 | [diff] [blame] | 150 | goto noalias; |
Masahiro Yamada | a467792c6 | 2014-04-18 17:41:01 +0900 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | /* fdt_setprop may break "path" so we copy it to tmp buffer */ |
| 154 | memcpy(tmp, path, len); |
| 155 | |
| 156 | err = fdt_setprop(fdt, chosenoff, "linux,stdout-path", tmp, len); |
Kumar Gala | 2dc9b4a | 2007-11-26 17:06:15 -0600 | [diff] [blame] | 157 | if (err < 0) |
| 158 | printf("WARNING: could not set linux,stdout-path %s.\n", |
Masahiro Yamada | a467792c6 | 2014-04-18 17:41:01 +0900 | [diff] [blame] | 159 | fdt_strerror(err)); |
Kumar Gala | 2dc9b4a | 2007-11-26 17:06:15 -0600 | [diff] [blame] | 160 | |
| 161 | return err; |
Scott Wood | edd551f | 2015-09-01 22:48:08 -0500 | [diff] [blame] | 162 | |
| 163 | noalias: |
| 164 | printf("WARNING: %s: could not read %s alias: %s\n", |
| 165 | __func__, sername, fdt_strerror(err)); |
| 166 | |
| 167 | return 0; |
Kumar Gala | 2dc9b4a | 2007-11-26 17:06:15 -0600 | [diff] [blame] | 168 | } |
Masahiro Yamada | a467792c6 | 2014-04-18 17:41:01 +0900 | [diff] [blame] | 169 | #else |
| 170 | static int fdt_fixup_stdout(void *fdt, int chosenoff) |
| 171 | { |
| 172 | return 0; |
| 173 | } |
Kumar Gala | 2dc9b4a | 2007-11-26 17:06:15 -0600 | [diff] [blame] | 174 | #endif |
| 175 | |
Masahiro Yamada | b891a34 | 2014-04-18 17:41:04 +0900 | [diff] [blame] | 176 | static inline int fdt_setprop_uxx(void *fdt, int nodeoffset, const char *name, |
| 177 | uint64_t val, int is_u64) |
| 178 | { |
| 179 | if (is_u64) |
| 180 | return fdt_setprop_u64(fdt, nodeoffset, name, val); |
| 181 | else |
| 182 | return fdt_setprop_u32(fdt, nodeoffset, name, (uint32_t)val); |
| 183 | } |
| 184 | |
Paul Kocialkowski | 2c0fbc7 | 2015-05-21 11:27:03 +0200 | [diff] [blame] | 185 | int fdt_root(void *fdt) |
| 186 | { |
| 187 | char *serial; |
| 188 | int err; |
| 189 | |
| 190 | err = fdt_check_header(fdt); |
| 191 | if (err < 0) { |
| 192 | printf("fdt_root: %s\n", fdt_strerror(err)); |
| 193 | return err; |
| 194 | } |
| 195 | |
Simon Glass | 64b723f | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 196 | serial = env_get("serial#"); |
Paul Kocialkowski | 2c0fbc7 | 2015-05-21 11:27:03 +0200 | [diff] [blame] | 197 | if (serial) { |
| 198 | err = fdt_setprop(fdt, 0, "serial-number", serial, |
| 199 | strlen(serial) + 1); |
| 200 | |
| 201 | if (err < 0) { |
| 202 | printf("WARNING: could not set serial-number %s.\n", |
| 203 | fdt_strerror(err)); |
| 204 | return err; |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | return 0; |
| 209 | } |
Masahiro Yamada | b891a34 | 2014-04-18 17:41:04 +0900 | [diff] [blame] | 210 | |
Masahiro Yamada | 5b11489 | 2014-04-18 17:40:59 +0900 | [diff] [blame] | 211 | int fdt_initrd(void *fdt, ulong initrd_start, ulong initrd_end) |
Gerald Van Baren | c4a57ea | 2007-04-06 14:19:43 -0400 | [diff] [blame] | 212 | { |
Masahiro Yamada | b891a34 | 2014-04-18 17:41:04 +0900 | [diff] [blame] | 213 | int nodeoffset; |
Kumar Gala | 99ebc05 | 2008-08-15 08:24:43 -0500 | [diff] [blame] | 214 | int err, j, total; |
Masahiro Yamada | b891a34 | 2014-04-18 17:41:04 +0900 | [diff] [blame] | 215 | int is_u64; |
Kumar Gala | 99ebc05 | 2008-08-15 08:24:43 -0500 | [diff] [blame] | 216 | uint64_t addr, size; |
Gerald Van Baren | c4a57ea | 2007-04-06 14:19:43 -0400 | [diff] [blame] | 217 | |
Masahiro Yamada | 9a3b4de | 2014-04-18 17:41:05 +0900 | [diff] [blame] | 218 | /* just return if the size of initrd is zero */ |
| 219 | if (initrd_start == initrd_end) |
| 220 | return 0; |
| 221 | |
Masahiro Yamada | c020728 | 2014-04-18 17:40:58 +0900 | [diff] [blame] | 222 | /* find or create "/chosen" node. */ |
| 223 | nodeoffset = fdt_find_or_add_subnode(fdt, 0, "chosen"); |
| 224 | if (nodeoffset < 0) |
Kumar Gala | 99ebc05 | 2008-08-15 08:24:43 -0500 | [diff] [blame] | 225 | return nodeoffset; |
Gerald Van Baren | c4a57ea | 2007-04-06 14:19:43 -0400 | [diff] [blame] | 226 | |
Kumar Gala | 99ebc05 | 2008-08-15 08:24:43 -0500 | [diff] [blame] | 227 | total = fdt_num_mem_rsv(fdt); |
| 228 | |
| 229 | /* |
| 230 | * Look for an existing entry and update it. If we don't find |
| 231 | * the entry, we will j be the next available slot. |
| 232 | */ |
| 233 | for (j = 0; j < total; j++) { |
| 234 | err = fdt_get_mem_rsv(fdt, j, &addr, &size); |
| 235 | if (addr == initrd_start) { |
| 236 | fdt_del_mem_rsv(fdt, j); |
| 237 | break; |
Gerald Van Baren | c9502b9 | 2007-04-14 22:51:24 -0400 | [diff] [blame] | 238 | } |
Kumar Gala | 99ebc05 | 2008-08-15 08:24:43 -0500 | [diff] [blame] | 239 | } |
| 240 | |
Grant Likely | c379a56 | 2011-03-28 09:58:55 +0000 | [diff] [blame] | 241 | err = fdt_add_mem_rsv(fdt, initrd_start, initrd_end - initrd_start); |
Kumar Gala | 99ebc05 | 2008-08-15 08:24:43 -0500 | [diff] [blame] | 242 | if (err < 0) { |
| 243 | printf("fdt_initrd: %s\n", fdt_strerror(err)); |
| 244 | return err; |
| 245 | } |
Kumar Gala | c8ab705 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 246 | |
Simon Glass | 9fbc632 | 2014-10-23 18:58:57 -0600 | [diff] [blame] | 247 | is_u64 = (fdt_address_cells(fdt, 0) == 2); |
David Feng | d985013 | 2013-12-14 11:47:29 +0800 | [diff] [blame] | 248 | |
Masahiro Yamada | b891a34 | 2014-04-18 17:41:04 +0900 | [diff] [blame] | 249 | err = fdt_setprop_uxx(fdt, nodeoffset, "linux,initrd-start", |
| 250 | (uint64_t)initrd_start, is_u64); |
| 251 | |
Masahiro Yamada | 5b11489 | 2014-04-18 17:40:59 +0900 | [diff] [blame] | 252 | if (err < 0) { |
| 253 | printf("WARNING: could not set linux,initrd-start %s.\n", |
| 254 | fdt_strerror(err)); |
| 255 | return err; |
| 256 | } |
Masahiro Yamada | b891a34 | 2014-04-18 17:41:04 +0900 | [diff] [blame] | 257 | |
| 258 | err = fdt_setprop_uxx(fdt, nodeoffset, "linux,initrd-end", |
| 259 | (uint64_t)initrd_end, is_u64); |
| 260 | |
Masahiro Yamada | 5b11489 | 2014-04-18 17:40:59 +0900 | [diff] [blame] | 261 | if (err < 0) { |
| 262 | printf("WARNING: could not set linux,initrd-end %s.\n", |
| 263 | fdt_strerror(err)); |
Kumar Gala | 99ebc05 | 2008-08-15 08:24:43 -0500 | [diff] [blame] | 264 | |
Masahiro Yamada | 5b11489 | 2014-04-18 17:40:59 +0900 | [diff] [blame] | 265 | return err; |
Gerald Van Baren | c4a57ea | 2007-04-06 14:19:43 -0400 | [diff] [blame] | 266 | } |
| 267 | |
Kumar Gala | 99ebc05 | 2008-08-15 08:24:43 -0500 | [diff] [blame] | 268 | return 0; |
| 269 | } |
| 270 | |
Niko Mauno | 673db43 | 2021-02-22 19:18:51 +0000 | [diff] [blame] | 271 | /** |
| 272 | * board_fdt_chosen_bootargs - boards may override this function to use |
| 273 | * alternative kernel command line arguments |
| 274 | */ |
| 275 | __weak char *board_fdt_chosen_bootargs(void) |
| 276 | { |
| 277 | return env_get("bootargs"); |
| 278 | } |
| 279 | |
Masahiro Yamada | 451c204 | 2014-04-18 17:41:00 +0900 | [diff] [blame] | 280 | int fdt_chosen(void *fdt) |
Kumar Gala | 99ebc05 | 2008-08-15 08:24:43 -0500 | [diff] [blame] | 281 | { |
| 282 | int nodeoffset; |
| 283 | int err; |
| 284 | char *str; /* used to set string properties */ |
Kumar Gala | 99ebc05 | 2008-08-15 08:24:43 -0500 | [diff] [blame] | 285 | |
| 286 | err = fdt_check_header(fdt); |
| 287 | if (err < 0) { |
| 288 | printf("fdt_chosen: %s\n", fdt_strerror(err)); |
| 289 | return err; |
| 290 | } |
| 291 | |
Masahiro Yamada | c020728 | 2014-04-18 17:40:58 +0900 | [diff] [blame] | 292 | /* find or create "/chosen" node. */ |
| 293 | nodeoffset = fdt_find_or_add_subnode(fdt, 0, "chosen"); |
| 294 | if (nodeoffset < 0) |
| 295 | return nodeoffset; |
Gerald Van Baren | c4a57ea | 2007-04-06 14:19:43 -0400 | [diff] [blame] | 296 | |
Niko Mauno | 673db43 | 2021-02-22 19:18:51 +0000 | [diff] [blame] | 297 | str = board_fdt_chosen_bootargs(); |
| 298 | |
Masahiro Yamada | a467792c6 | 2014-04-18 17:41:01 +0900 | [diff] [blame] | 299 | if (str) { |
| 300 | err = fdt_setprop(fdt, nodeoffset, "bootargs", str, |
| 301 | strlen(str) + 1); |
| 302 | if (err < 0) { |
Masahiro Yamada | 451c204 | 2014-04-18 17:41:00 +0900 | [diff] [blame] | 303 | printf("WARNING: could not set bootargs %s.\n", |
| 304 | fdt_strerror(err)); |
Masahiro Yamada | a467792c6 | 2014-04-18 17:41:01 +0900 | [diff] [blame] | 305 | return err; |
| 306 | } |
Gerald Van Baren | c4a57ea | 2007-04-06 14:19:43 -0400 | [diff] [blame] | 307 | } |
Kumar Gala | 2dc9b4a | 2007-11-26 17:06:15 -0600 | [diff] [blame] | 308 | |
Francesco Dolcini | 05cc827 | 2022-05-19 16:22:26 +0200 | [diff] [blame] | 309 | /* add u-boot version */ |
| 310 | err = fdt_setprop(fdt, nodeoffset, "u-boot,version", PLAIN_VERSION, |
| 311 | strlen(PLAIN_VERSION) + 1); |
| 312 | if (err < 0) { |
| 313 | printf("WARNING: could not set u-boot,version %s.\n", |
| 314 | fdt_strerror(err)); |
| 315 | return err; |
| 316 | } |
| 317 | |
Masahiro Yamada | a467792c6 | 2014-04-18 17:41:01 +0900 | [diff] [blame] | 318 | return fdt_fixup_stdout(fdt, nodeoffset); |
Gerald Van Baren | c4a57ea | 2007-04-06 14:19:43 -0400 | [diff] [blame] | 319 | } |
| 320 | |
Kumar Gala | 7e64cf8 | 2007-11-03 19:46:28 -0500 | [diff] [blame] | 321 | void do_fixup_by_path(void *fdt, const char *path, const char *prop, |
| 322 | const void *val, int len, int create) |
| 323 | { |
| 324 | #if defined(DEBUG) |
| 325 | int i; |
Kumar Gala | 88f0ca2 | 2008-02-13 15:09:58 -0600 | [diff] [blame] | 326 | debug("Updating property '%s/%s' = ", path, prop); |
Kumar Gala | 7e64cf8 | 2007-11-03 19:46:28 -0500 | [diff] [blame] | 327 | for (i = 0; i < len; i++) |
| 328 | debug(" %.2x", *(u8*)(val+i)); |
| 329 | debug("\n"); |
| 330 | #endif |
| 331 | int rc = fdt_find_and_setprop(fdt, path, prop, val, len, create); |
| 332 | if (rc) |
| 333 | printf("Unable to update property %s:%s, err=%s\n", |
| 334 | path, prop, fdt_strerror(rc)); |
| 335 | } |
| 336 | |
| 337 | void do_fixup_by_path_u32(void *fdt, const char *path, const char *prop, |
| 338 | u32 val, int create) |
| 339 | { |
Kim Phillips | 6542c07 | 2013-01-16 14:00:11 +0000 | [diff] [blame] | 340 | fdt32_t tmp = cpu_to_fdt32(val); |
| 341 | do_fixup_by_path(fdt, path, prop, &tmp, sizeof(tmp), create); |
Kumar Gala | 7e64cf8 | 2007-11-03 19:46:28 -0500 | [diff] [blame] | 342 | } |
| 343 | |
Kumar Gala | 7590a19 | 2007-11-21 13:30:15 -0600 | [diff] [blame] | 344 | void do_fixup_by_prop(void *fdt, |
| 345 | const char *pname, const void *pval, int plen, |
| 346 | const char *prop, const void *val, int len, |
| 347 | int create) |
| 348 | { |
| 349 | int off; |
| 350 | #if defined(DEBUG) |
| 351 | int i; |
Kumar Gala | 88f0ca2 | 2008-02-13 15:09:58 -0600 | [diff] [blame] | 352 | debug("Updating property '%s' = ", prop); |
Kumar Gala | 7590a19 | 2007-11-21 13:30:15 -0600 | [diff] [blame] | 353 | for (i = 0; i < len; i++) |
| 354 | debug(" %.2x", *(u8*)(val+i)); |
| 355 | debug("\n"); |
| 356 | #endif |
| 357 | off = fdt_node_offset_by_prop_value(fdt, -1, pname, pval, plen); |
| 358 | while (off != -FDT_ERR_NOTFOUND) { |
Kim Phillips | 6542c07 | 2013-01-16 14:00:11 +0000 | [diff] [blame] | 359 | if (create || (fdt_get_property(fdt, off, prop, NULL) != NULL)) |
Kumar Gala | 7590a19 | 2007-11-21 13:30:15 -0600 | [diff] [blame] | 360 | fdt_setprop(fdt, off, prop, val, len); |
| 361 | off = fdt_node_offset_by_prop_value(fdt, off, pname, pval, plen); |
| 362 | } |
| 363 | } |
| 364 | |
| 365 | void do_fixup_by_prop_u32(void *fdt, |
| 366 | const char *pname, const void *pval, int plen, |
| 367 | const char *prop, u32 val, int create) |
| 368 | { |
Kim Phillips | 6542c07 | 2013-01-16 14:00:11 +0000 | [diff] [blame] | 369 | fdt32_t tmp = cpu_to_fdt32(val); |
| 370 | do_fixup_by_prop(fdt, pname, pval, plen, prop, &tmp, 4, create); |
Kumar Gala | 7590a19 | 2007-11-21 13:30:15 -0600 | [diff] [blame] | 371 | } |
| 372 | |
| 373 | void do_fixup_by_compat(void *fdt, const char *compat, |
| 374 | const char *prop, const void *val, int len, int create) |
| 375 | { |
| 376 | int off = -1; |
| 377 | #if defined(DEBUG) |
| 378 | int i; |
Kumar Gala | 88f0ca2 | 2008-02-13 15:09:58 -0600 | [diff] [blame] | 379 | debug("Updating property '%s' = ", prop); |
Kumar Gala | 7590a19 | 2007-11-21 13:30:15 -0600 | [diff] [blame] | 380 | for (i = 0; i < len; i++) |
| 381 | debug(" %.2x", *(u8*)(val+i)); |
| 382 | debug("\n"); |
| 383 | #endif |
Marek Behún | 5d6b448 | 2022-01-20 01:04:42 +0100 | [diff] [blame] | 384 | fdt_for_each_node_by_compatible(off, fdt, -1, compat) |
Kim Phillips | 6542c07 | 2013-01-16 14:00:11 +0000 | [diff] [blame] | 385 | if (create || (fdt_get_property(fdt, off, prop, NULL) != NULL)) |
Kumar Gala | 7590a19 | 2007-11-21 13:30:15 -0600 | [diff] [blame] | 386 | fdt_setprop(fdt, off, prop, val, len); |
Kumar Gala | 7590a19 | 2007-11-21 13:30:15 -0600 | [diff] [blame] | 387 | } |
| 388 | |
| 389 | void do_fixup_by_compat_u32(void *fdt, const char *compat, |
| 390 | const char *prop, u32 val, int create) |
| 391 | { |
Kim Phillips | 6542c07 | 2013-01-16 14:00:11 +0000 | [diff] [blame] | 392 | fdt32_t tmp = cpu_to_fdt32(val); |
| 393 | do_fixup_by_compat(fdt, compat, prop, &tmp, 4, create); |
Kumar Gala | 7590a19 | 2007-11-21 13:30:15 -0600 | [diff] [blame] | 394 | } |
| 395 | |
Masahiro Yamada | f6aa39e | 2016-11-26 11:02:10 +0900 | [diff] [blame] | 396 | #ifdef CONFIG_ARCH_FIXUP_FDT_MEMORY |
Masahiro Yamada | a6ae8d3 | 2014-04-18 17:41:03 +0900 | [diff] [blame] | 397 | /* |
| 398 | * fdt_pack_reg - pack address and size array into the "reg"-suitable stream |
| 399 | */ |
Simon Glass | 0058e11 | 2014-10-23 18:58:55 -0600 | [diff] [blame] | 400 | static int fdt_pack_reg(const void *fdt, void *buf, u64 *address, u64 *size, |
| 401 | int n) |
Masahiro Yamada | a6ae8d3 | 2014-04-18 17:41:03 +0900 | [diff] [blame] | 402 | { |
| 403 | int i; |
Hans de Goede | 4c80fb0 | 2014-11-28 14:23:51 +0100 | [diff] [blame] | 404 | int address_cells = fdt_address_cells(fdt, 0); |
| 405 | int size_cells = fdt_size_cells(fdt, 0); |
Masahiro Yamada | a6ae8d3 | 2014-04-18 17:41:03 +0900 | [diff] [blame] | 406 | char *p = buf; |
| 407 | |
| 408 | for (i = 0; i < n; i++) { |
Hans de Goede | 4c80fb0 | 2014-11-28 14:23:51 +0100 | [diff] [blame] | 409 | if (address_cells == 2) |
Masahiro Yamada | a6ae8d3 | 2014-04-18 17:41:03 +0900 | [diff] [blame] | 410 | *(fdt64_t *)p = cpu_to_fdt64(address[i]); |
| 411 | else |
| 412 | *(fdt32_t *)p = cpu_to_fdt32(address[i]); |
Hans de Goede | 4c80fb0 | 2014-11-28 14:23:51 +0100 | [diff] [blame] | 413 | p += 4 * address_cells; |
Masahiro Yamada | a6ae8d3 | 2014-04-18 17:41:03 +0900 | [diff] [blame] | 414 | |
Hans de Goede | 4c80fb0 | 2014-11-28 14:23:51 +0100 | [diff] [blame] | 415 | if (size_cells == 2) |
Masahiro Yamada | a6ae8d3 | 2014-04-18 17:41:03 +0900 | [diff] [blame] | 416 | *(fdt64_t *)p = cpu_to_fdt64(size[i]); |
| 417 | else |
| 418 | *(fdt32_t *)p = cpu_to_fdt32(size[i]); |
Hans de Goede | 4c80fb0 | 2014-11-28 14:23:51 +0100 | [diff] [blame] | 419 | p += 4 * size_cells; |
Masahiro Yamada | a6ae8d3 | 2014-04-18 17:41:03 +0900 | [diff] [blame] | 420 | } |
| 421 | |
| 422 | return p - (char *)buf; |
| 423 | } |
| 424 | |
Ramon Fried | 9cab183 | 2018-08-14 00:35:42 +0300 | [diff] [blame] | 425 | #if CONFIG_NR_DRAM_BANKS > 4 |
| 426 | #define MEMORY_BANKS_MAX CONFIG_NR_DRAM_BANKS |
| 427 | #else |
Kim Phillips | 6542c07 | 2013-01-16 14:00:11 +0000 | [diff] [blame] | 428 | #define MEMORY_BANKS_MAX 4 |
Ramon Fried | 9cab183 | 2018-08-14 00:35:42 +0300 | [diff] [blame] | 429 | #endif |
Michal Simek | 9628373 | 2021-08-10 09:21:54 +0200 | [diff] [blame] | 430 | |
| 431 | /** |
| 432 | * fdt_fixup_memory_banks - Update DT memory node |
| 433 | * @blob: Pointer to DT blob |
| 434 | * @start: Pointer to memory start addresses array |
| 435 | * @size: Pointer to memory sizes array |
| 436 | * @banks: Number of memory banks |
| 437 | * |
| 438 | * Return: 0 on success, negative value on failure |
| 439 | * |
| 440 | * Based on the passed number of banks and arrays, the function is able to |
| 441 | * update existing DT memory nodes to match run time detected/changed memory |
| 442 | * configuration. Implementation is handling one specific case with only one |
| 443 | * memory node where multiple tuples could be added/updated. |
| 444 | * The case where multiple memory nodes with a single tuple (base, size) are |
| 445 | * used, this function is only updating the first memory node without removing |
| 446 | * others. |
| 447 | */ |
John Rigby | b4ae942 | 2010-10-13 13:57:33 -0600 | [diff] [blame] | 448 | int fdt_fixup_memory_banks(void *blob, u64 start[], u64 size[], int banks) |
| 449 | { |
| 450 | int err, nodeoffset; |
Thierry Reding | 74af8a3 | 2018-01-30 11:34:17 +0100 | [diff] [blame] | 451 | int len, i; |
Kim Phillips | 6542c07 | 2013-01-16 14:00:11 +0000 | [diff] [blame] | 452 | u8 tmp[MEMORY_BANKS_MAX * 16]; /* Up to 64-bit address + 64-bit size */ |
Kumar Gala | 0db72ed | 2007-11-26 14:57:45 -0600 | [diff] [blame] | 453 | |
Kim Phillips | 6542c07 | 2013-01-16 14:00:11 +0000 | [diff] [blame] | 454 | if (banks > MEMORY_BANKS_MAX) { |
| 455 | printf("%s: num banks %d exceeds hardcoded limit %d." |
| 456 | " Recompile with higher MEMORY_BANKS_MAX?\n", |
| 457 | __FUNCTION__, banks, MEMORY_BANKS_MAX); |
| 458 | return -1; |
| 459 | } |
| 460 | |
Kumar Gala | 0db72ed | 2007-11-26 14:57:45 -0600 | [diff] [blame] | 461 | err = fdt_check_header(blob); |
| 462 | if (err < 0) { |
| 463 | printf("%s: %s\n", __FUNCTION__, fdt_strerror(err)); |
| 464 | return err; |
| 465 | } |
| 466 | |
Masahiro Yamada | c020728 | 2014-04-18 17:40:58 +0900 | [diff] [blame] | 467 | /* find or create "/memory" node. */ |
| 468 | nodeoffset = fdt_find_or_add_subnode(blob, 0, "memory"); |
| 469 | if (nodeoffset < 0) |
Miao Yan | 0904eb2 | 2013-11-28 17:51:39 +0800 | [diff] [blame] | 470 | return nodeoffset; |
Masahiro Yamada | c020728 | 2014-04-18 17:40:58 +0900 | [diff] [blame] | 471 | |
Kumar Gala | 0db72ed | 2007-11-26 14:57:45 -0600 | [diff] [blame] | 472 | err = fdt_setprop(blob, nodeoffset, "device_type", "memory", |
| 473 | sizeof("memory")); |
| 474 | if (err < 0) { |
| 475 | printf("WARNING: could not set %s %s.\n", "device_type", |
| 476 | fdt_strerror(err)); |
| 477 | return err; |
| 478 | } |
| 479 | |
Thierry Reding | 9eb0e7e | 2018-02-15 19:05:59 +0100 | [diff] [blame] | 480 | for (i = 0; i < banks; i++) { |
| 481 | if (start[i] == 0 && size[i] == 0) |
| 482 | break; |
| 483 | } |
| 484 | |
| 485 | banks = i; |
| 486 | |
Andre Przywara | 3d2d441 | 2015-06-21 00:29:54 +0100 | [diff] [blame] | 487 | if (!banks) |
| 488 | return 0; |
| 489 | |
Masahiro Yamada | a6ae8d3 | 2014-04-18 17:41:03 +0900 | [diff] [blame] | 490 | len = fdt_pack_reg(blob, tmp, start, size, banks); |
Kumar Gala | 0db72ed | 2007-11-26 14:57:45 -0600 | [diff] [blame] | 491 | |
| 492 | err = fdt_setprop(blob, nodeoffset, "reg", tmp, len); |
| 493 | if (err < 0) { |
| 494 | printf("WARNING: could not set %s %s.\n", |
| 495 | "reg", fdt_strerror(err)); |
| 496 | return err; |
| 497 | } |
| 498 | return 0; |
| 499 | } |
Igor Opaniuk | 80b9578 | 2019-12-03 14:04:46 +0200 | [diff] [blame] | 500 | |
| 501 | int fdt_set_usable_memory(void *blob, u64 start[], u64 size[], int areas) |
| 502 | { |
| 503 | int err, nodeoffset; |
| 504 | int len; |
| 505 | u8 tmp[8 * 16]; /* Up to 64-bit address + 64-bit size */ |
| 506 | |
| 507 | if (areas > 8) { |
| 508 | printf("%s: num areas %d exceeds hardcoded limit %d\n", |
| 509 | __func__, areas, 8); |
| 510 | return -1; |
| 511 | } |
| 512 | |
| 513 | err = fdt_check_header(blob); |
| 514 | if (err < 0) { |
| 515 | printf("%s: %s\n", __func__, fdt_strerror(err)); |
| 516 | return err; |
| 517 | } |
| 518 | |
| 519 | /* find or create "/memory" node. */ |
| 520 | nodeoffset = fdt_find_or_add_subnode(blob, 0, "memory"); |
| 521 | if (nodeoffset < 0) |
| 522 | return nodeoffset; |
| 523 | |
| 524 | len = fdt_pack_reg(blob, tmp, start, size, areas); |
| 525 | |
| 526 | err = fdt_setprop(blob, nodeoffset, "linux,usable-memory", tmp, len); |
| 527 | if (err < 0) { |
| 528 | printf("WARNING: could not set %s %s.\n", |
| 529 | "reg", fdt_strerror(err)); |
| 530 | return err; |
| 531 | } |
| 532 | |
| 533 | return 0; |
| 534 | } |
Masahiro Yamada | f6aa39e | 2016-11-26 11:02:10 +0900 | [diff] [blame] | 535 | #endif |
Kumar Gala | 0db72ed | 2007-11-26 14:57:45 -0600 | [diff] [blame] | 536 | |
John Rigby | b4ae942 | 2010-10-13 13:57:33 -0600 | [diff] [blame] | 537 | int fdt_fixup_memory(void *blob, u64 start, u64 size) |
| 538 | { |
| 539 | return fdt_fixup_memory_banks(blob, &start, &size, 1); |
| 540 | } |
| 541 | |
Kumar Gala | fabda92 | 2008-08-19 15:41:18 -0500 | [diff] [blame] | 542 | void fdt_fixup_ethernet(void *fdt) |
Kumar Gala | 2269910 | 2007-11-21 11:11:03 -0600 | [diff] [blame] | 543 | { |
Prabhakar Kushwaha | 2dec06f | 2017-11-23 16:51:32 +0530 | [diff] [blame] | 544 | int i = 0, j, prop; |
Bin Meng | bfb93cc | 2015-11-03 04:24:41 -0800 | [diff] [blame] | 545 | char *tmp, *end; |
Stephen Warren | 7c15c77 | 2013-05-27 18:01:19 +0000 | [diff] [blame] | 546 | char mac[16]; |
Kumar Gala | 2269910 | 2007-11-21 11:11:03 -0600 | [diff] [blame] | 547 | const char *path; |
oliver@schinagl.nl | 496904b | 2016-11-25 16:30:19 +0100 | [diff] [blame] | 548 | unsigned char mac_addr[ARP_HLEN]; |
Bin Meng | bfb93cc | 2015-11-03 04:24:41 -0800 | [diff] [blame] | 549 | int offset; |
Prabhakar Kushwaha | 2dec06f | 2017-11-23 16:51:32 +0530 | [diff] [blame] | 550 | #ifdef FDT_SEQ_MACADDR_FROM_ENV |
| 551 | int nodeoff; |
| 552 | const struct fdt_property *fdt_prop; |
| 553 | #endif |
Kumar Gala | 2269910 | 2007-11-21 11:11:03 -0600 | [diff] [blame] | 554 | |
Lev Iserovich | cde7761 | 2016-01-07 18:04:16 -0500 | [diff] [blame] | 555 | if (fdt_path_offset(fdt, "/aliases") < 0) |
Kumar Gala | fabda92 | 2008-08-19 15:41:18 -0500 | [diff] [blame] | 556 | return; |
| 557 | |
Lev Iserovich | cde7761 | 2016-01-07 18:04:16 -0500 | [diff] [blame] | 558 | /* Cycle through all aliases */ |
| 559 | for (prop = 0; ; prop++) { |
Bin Meng | bfb93cc | 2015-11-03 04:24:41 -0800 | [diff] [blame] | 560 | const char *name; |
Dan Murphy | 0b9bf27 | 2013-10-02 14:00:15 -0500 | [diff] [blame] | 561 | |
Lev Iserovich | cde7761 | 2016-01-07 18:04:16 -0500 | [diff] [blame] | 562 | /* FDT might have been edited, recompute the offset */ |
| 563 | offset = fdt_first_property_offset(fdt, |
| 564 | fdt_path_offset(fdt, "/aliases")); |
| 565 | /* Select property number 'prop' */ |
Prabhakar Kushwaha | 2dec06f | 2017-11-23 16:51:32 +0530 | [diff] [blame] | 566 | for (j = 0; j < prop; j++) |
Lev Iserovich | cde7761 | 2016-01-07 18:04:16 -0500 | [diff] [blame] | 567 | offset = fdt_next_property_offset(fdt, offset); |
| 568 | |
| 569 | if (offset < 0) |
| 570 | break; |
| 571 | |
Bin Meng | bfb93cc | 2015-11-03 04:24:41 -0800 | [diff] [blame] | 572 | path = fdt_getprop_by_offset(fdt, offset, &name, NULL); |
Tuomas Tynkkynen | a7042f5 | 2017-03-20 10:04:55 +0200 | [diff] [blame] | 573 | if (!strncmp(name, "ethernet", 8)) { |
| 574 | /* Treat plain "ethernet" same as "ethernet0". */ |
Prabhakar Kushwaha | 2dec06f | 2017-11-23 16:51:32 +0530 | [diff] [blame] | 575 | if (!strcmp(name, "ethernet") |
| 576 | #ifdef FDT_SEQ_MACADDR_FROM_ENV |
| 577 | || !strcmp(name, "ethernet0") |
| 578 | #endif |
| 579 | ) |
Tuomas Tynkkynen | a7042f5 | 2017-03-20 10:04:55 +0200 | [diff] [blame] | 580 | i = 0; |
Prabhakar Kushwaha | 2dec06f | 2017-11-23 16:51:32 +0530 | [diff] [blame] | 581 | #ifndef FDT_SEQ_MACADDR_FROM_ENV |
Tuomas Tynkkynen | a7042f5 | 2017-03-20 10:04:55 +0200 | [diff] [blame] | 582 | else |
| 583 | i = trailing_strtol(name); |
Prabhakar Kushwaha | 2dec06f | 2017-11-23 16:51:32 +0530 | [diff] [blame] | 584 | #endif |
Bin Meng | bfb93cc | 2015-11-03 04:24:41 -0800 | [diff] [blame] | 585 | if (i != -1) { |
| 586 | if (i == 0) |
| 587 | strcpy(mac, "ethaddr"); |
| 588 | else |
| 589 | sprintf(mac, "eth%daddr", i); |
| 590 | } else { |
| 591 | continue; |
| 592 | } |
Prabhakar Kushwaha | 2dec06f | 2017-11-23 16:51:32 +0530 | [diff] [blame] | 593 | #ifdef FDT_SEQ_MACADDR_FROM_ENV |
| 594 | nodeoff = fdt_path_offset(fdt, path); |
| 595 | fdt_prop = fdt_get_property(fdt, nodeoff, "status", |
| 596 | NULL); |
| 597 | if (fdt_prop && !strcmp(fdt_prop->data, "disabled")) |
| 598 | continue; |
| 599 | i++; |
| 600 | #endif |
Simon Glass | 64b723f | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 601 | tmp = env_get(mac); |
Bin Meng | bfb93cc | 2015-11-03 04:24:41 -0800 | [diff] [blame] | 602 | if (!tmp) |
| 603 | continue; |
Kumar Gala | fabda92 | 2008-08-19 15:41:18 -0500 | [diff] [blame] | 604 | |
Bin Meng | bfb93cc | 2015-11-03 04:24:41 -0800 | [diff] [blame] | 605 | for (j = 0; j < 6; j++) { |
| 606 | mac_addr[j] = tmp ? |
Simon Glass | 3ff49ec | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 607 | hextoul(tmp, &end) : 0; |
Bin Meng | bfb93cc | 2015-11-03 04:24:41 -0800 | [diff] [blame] | 608 | if (tmp) |
| 609 | tmp = (*end) ? end + 1 : end; |
| 610 | } |
Kumar Gala | fabda92 | 2008-08-19 15:41:18 -0500 | [diff] [blame] | 611 | |
Bin Meng | bfb93cc | 2015-11-03 04:24:41 -0800 | [diff] [blame] | 612 | do_fixup_by_path(fdt, path, "mac-address", |
| 613 | &mac_addr, 6, 0); |
| 614 | do_fixup_by_path(fdt, path, "local-mac-address", |
| 615 | &mac_addr, 6, 1); |
| 616 | } |
Kumar Gala | 2269910 | 2007-11-21 11:11:03 -0600 | [diff] [blame] | 617 | } |
| 618 | } |
Anton Vorontsov | 07e6091 | 2008-03-14 23:20:18 +0300 | [diff] [blame] | 619 | |
Philipp Tomsich | 1b1b4e4 | 2018-02-02 12:01:44 +0100 | [diff] [blame] | 620 | int fdt_record_loadable(void *blob, u32 index, const char *name, |
| 621 | uintptr_t load_addr, u32 size, uintptr_t entry_point, |
Michal Simek | 0e4f43d | 2021-05-27 11:40:09 +0200 | [diff] [blame] | 622 | const char *type, const char *os, const char *arch) |
Philipp Tomsich | 1b1b4e4 | 2018-02-02 12:01:44 +0100 | [diff] [blame] | 623 | { |
| 624 | int err, node; |
| 625 | |
| 626 | err = fdt_check_header(blob); |
| 627 | if (err < 0) { |
| 628 | printf("%s: %s\n", __func__, fdt_strerror(err)); |
| 629 | return err; |
| 630 | } |
| 631 | |
| 632 | /* find or create "/fit-images" node */ |
| 633 | node = fdt_find_or_add_subnode(blob, 0, "fit-images"); |
| 634 | if (node < 0) |
| 635 | return node; |
| 636 | |
| 637 | /* find or create "/fit-images/<name>" node */ |
| 638 | node = fdt_find_or_add_subnode(blob, node, name); |
| 639 | if (node < 0) |
| 640 | return node; |
| 641 | |
Michal Simek | d31b40f | 2020-09-03 12:44:51 +0200 | [diff] [blame] | 642 | fdt_setprop_u64(blob, node, "load", load_addr); |
Philipp Tomsich | 1b1b4e4 | 2018-02-02 12:01:44 +0100 | [diff] [blame] | 643 | if (entry_point != -1) |
Michal Simek | d31b40f | 2020-09-03 12:44:51 +0200 | [diff] [blame] | 644 | fdt_setprop_u64(blob, node, "entry", entry_point); |
Philipp Tomsich | 1b1b4e4 | 2018-02-02 12:01:44 +0100 | [diff] [blame] | 645 | fdt_setprop_u32(blob, node, "size", size); |
| 646 | if (type) |
| 647 | fdt_setprop_string(blob, node, "type", type); |
| 648 | if (os) |
| 649 | fdt_setprop_string(blob, node, "os", os); |
Michal Simek | 0e4f43d | 2021-05-27 11:40:09 +0200 | [diff] [blame] | 650 | if (arch) |
| 651 | fdt_setprop_string(blob, node, "arch", arch); |
Philipp Tomsich | 1b1b4e4 | 2018-02-02 12:01:44 +0100 | [diff] [blame] | 652 | |
| 653 | return node; |
| 654 | } |
| 655 | |
Kumar Gala | f2f58c5 | 2008-08-15 08:24:42 -0500 | [diff] [blame] | 656 | /* Resize the fdt to its actual size + a bit of padding */ |
Hannes Schmelzer | d3dbac8 | 2016-09-20 18:10:43 +0200 | [diff] [blame] | 657 | int fdt_shrink_to_minimum(void *blob, uint extrasize) |
Kumar Gala | f2f58c5 | 2008-08-15 08:24:42 -0500 | [diff] [blame] | 658 | { |
| 659 | int i; |
| 660 | uint64_t addr, size; |
| 661 | int total, ret; |
| 662 | uint actualsize; |
Simon Goldschmidt | 5d5a0c0 | 2019-05-03 21:19:03 +0200 | [diff] [blame] | 663 | int fdt_memrsv = 0; |
Kumar Gala | f2f58c5 | 2008-08-15 08:24:42 -0500 | [diff] [blame] | 664 | |
| 665 | if (!blob) |
| 666 | return 0; |
| 667 | |
| 668 | total = fdt_num_mem_rsv(blob); |
| 669 | for (i = 0; i < total; i++) { |
| 670 | fdt_get_mem_rsv(blob, i, &addr, &size); |
Simon Glass | fc95324 | 2011-09-17 06:48:58 +0000 | [diff] [blame] | 671 | if (addr == (uintptr_t)blob) { |
Kumar Gala | f2f58c5 | 2008-08-15 08:24:42 -0500 | [diff] [blame] | 672 | fdt_del_mem_rsv(blob, i); |
Simon Goldschmidt | 5d5a0c0 | 2019-05-03 21:19:03 +0200 | [diff] [blame] | 673 | fdt_memrsv = 1; |
Kumar Gala | f2f58c5 | 2008-08-15 08:24:42 -0500 | [diff] [blame] | 674 | break; |
| 675 | } |
| 676 | } |
| 677 | |
Peter Korsgaard | 9c7b705 | 2008-10-28 08:26:52 +0100 | [diff] [blame] | 678 | /* |
| 679 | * Calculate the actual size of the fdt |
Feng Wang | ba31fb6 | 2010-08-03 16:22:43 +0200 | [diff] [blame] | 680 | * plus the size needed for 5 fdt_add_mem_rsv, one |
| 681 | * for the fdt itself and 4 for a possible initrd |
| 682 | * ((initrd-start + initrd-end) * 2 (name & value)) |
Peter Korsgaard | 9c7b705 | 2008-10-28 08:26:52 +0100 | [diff] [blame] | 683 | */ |
Kumar Gala | f2f58c5 | 2008-08-15 08:24:42 -0500 | [diff] [blame] | 684 | actualsize = fdt_off_dt_strings(blob) + |
Feng Wang | ba31fb6 | 2010-08-03 16:22:43 +0200 | [diff] [blame] | 685 | fdt_size_dt_strings(blob) + 5 * sizeof(struct fdt_reserve_entry); |
Kumar Gala | f2f58c5 | 2008-08-15 08:24:42 -0500 | [diff] [blame] | 686 | |
Hannes Schmelzer | d3dbac8 | 2016-09-20 18:10:43 +0200 | [diff] [blame] | 687 | actualsize += extrasize; |
Kumar Gala | f2f58c5 | 2008-08-15 08:24:42 -0500 | [diff] [blame] | 688 | /* Make it so the fdt ends on a page boundary */ |
Simon Glass | fc95324 | 2011-09-17 06:48:58 +0000 | [diff] [blame] | 689 | actualsize = ALIGN(actualsize + ((uintptr_t)blob & 0xfff), 0x1000); |
| 690 | actualsize = actualsize - ((uintptr_t)blob & 0xfff); |
Kumar Gala | f2f58c5 | 2008-08-15 08:24:42 -0500 | [diff] [blame] | 691 | |
| 692 | /* Change the fdt header to reflect the correct size */ |
| 693 | fdt_set_totalsize(blob, actualsize); |
| 694 | |
Simon Goldschmidt | 5d5a0c0 | 2019-05-03 21:19:03 +0200 | [diff] [blame] | 695 | if (fdt_memrsv) { |
| 696 | /* Add the new reservation */ |
| 697 | ret = fdt_add_mem_rsv(blob, map_to_sysmem(blob), actualsize); |
| 698 | if (ret < 0) |
| 699 | return ret; |
| 700 | } |
Kumar Gala | f2f58c5 | 2008-08-15 08:24:42 -0500 | [diff] [blame] | 701 | |
| 702 | return actualsize; |
| 703 | } |
Kumar Gala | e0475cd | 2008-10-22 23:33:56 -0500 | [diff] [blame] | 704 | |
Marek Behún | ac4d9ff | 2021-11-26 14:57:15 +0100 | [diff] [blame] | 705 | /** |
| 706 | * fdt_delete_disabled_nodes: Delete all nodes with status == "disabled" |
| 707 | * |
| 708 | * @blob: ptr to device tree |
| 709 | */ |
| 710 | int fdt_delete_disabled_nodes(void *blob) |
| 711 | { |
| 712 | while (1) { |
| 713 | int ret, offset; |
| 714 | |
| 715 | offset = fdt_node_offset_by_prop_value(blob, -1, "status", |
| 716 | "disabled", 9); |
| 717 | if (offset < 0) |
| 718 | break; |
| 719 | |
| 720 | ret = fdt_del_node(blob, offset); |
| 721 | if (ret < 0) |
| 722 | return ret; |
| 723 | } |
| 724 | |
| 725 | return 0; |
| 726 | } |
| 727 | |
Kumar Gala | e0475cd | 2008-10-22 23:33:56 -0500 | [diff] [blame] | 728 | #ifdef CONFIG_PCI |
Kumar Gala | baf4189 | 2009-08-05 09:03:54 -0500 | [diff] [blame] | 729 | #define CONFIG_SYS_PCI_NR_INBOUND_WIN 4 |
Kumar Gala | e0475cd | 2008-10-22 23:33:56 -0500 | [diff] [blame] | 730 | |
| 731 | #define FDT_PCI_PREFETCH (0x40000000) |
| 732 | #define FDT_PCI_MEM32 (0x02000000) |
| 733 | #define FDT_PCI_IO (0x01000000) |
| 734 | #define FDT_PCI_MEM64 (0x03000000) |
| 735 | |
| 736 | int fdt_pci_dma_ranges(void *blob, int phb_off, struct pci_controller *hose) { |
| 737 | |
| 738 | int addrcell, sizecell, len, r; |
| 739 | u32 *dma_range; |
| 740 | /* sized based on pci addr cells, size-cells, & address-cells */ |
| 741 | u32 dma_ranges[(3 + 2 + 2) * CONFIG_SYS_PCI_NR_INBOUND_WIN]; |
| 742 | |
| 743 | addrcell = fdt_getprop_u32_default(blob, "/", "#address-cells", 1); |
| 744 | sizecell = fdt_getprop_u32_default(blob, "/", "#size-cells", 1); |
| 745 | |
| 746 | dma_range = &dma_ranges[0]; |
| 747 | for (r = 0; r < hose->region_count; r++) { |
| 748 | u64 bus_start, phys_start, size; |
| 749 | |
Kumar Gala | efa1f1d | 2009-02-06 09:49:31 -0600 | [diff] [blame] | 750 | /* skip if !PCI_REGION_SYS_MEMORY */ |
| 751 | if (!(hose->regions[r].flags & PCI_REGION_SYS_MEMORY)) |
Kumar Gala | e0475cd | 2008-10-22 23:33:56 -0500 | [diff] [blame] | 752 | continue; |
| 753 | |
| 754 | bus_start = (u64)hose->regions[r].bus_start; |
| 755 | phys_start = (u64)hose->regions[r].phys_start; |
| 756 | size = (u64)hose->regions[r].size; |
| 757 | |
| 758 | dma_range[0] = 0; |
Kumar Gala | baf4189 | 2009-08-05 09:03:54 -0500 | [diff] [blame] | 759 | if (size >= 0x100000000ull) |
Marek Vasut | 22203f5 | 2019-07-09 02:49:29 +0200 | [diff] [blame] | 760 | dma_range[0] |= cpu_to_fdt32(FDT_PCI_MEM64); |
Kumar Gala | e0475cd | 2008-10-22 23:33:56 -0500 | [diff] [blame] | 761 | else |
Marek Vasut | 22203f5 | 2019-07-09 02:49:29 +0200 | [diff] [blame] | 762 | dma_range[0] |= cpu_to_fdt32(FDT_PCI_MEM32); |
Kumar Gala | e0475cd | 2008-10-22 23:33:56 -0500 | [diff] [blame] | 763 | if (hose->regions[r].flags & PCI_REGION_PREFETCH) |
Marek Vasut | 22203f5 | 2019-07-09 02:49:29 +0200 | [diff] [blame] | 764 | dma_range[0] |= cpu_to_fdt32(FDT_PCI_PREFETCH); |
Kumar Gala | e0475cd | 2008-10-22 23:33:56 -0500 | [diff] [blame] | 765 | #ifdef CONFIG_SYS_PCI_64BIT |
Marek Vasut | 22203f5 | 2019-07-09 02:49:29 +0200 | [diff] [blame] | 766 | dma_range[1] = cpu_to_fdt32(bus_start >> 32); |
Kumar Gala | e0475cd | 2008-10-22 23:33:56 -0500 | [diff] [blame] | 767 | #else |
| 768 | dma_range[1] = 0; |
| 769 | #endif |
Marek Vasut | 22203f5 | 2019-07-09 02:49:29 +0200 | [diff] [blame] | 770 | dma_range[2] = cpu_to_fdt32(bus_start & 0xffffffff); |
Kumar Gala | e0475cd | 2008-10-22 23:33:56 -0500 | [diff] [blame] | 771 | |
| 772 | if (addrcell == 2) { |
Marek Vasut | 22203f5 | 2019-07-09 02:49:29 +0200 | [diff] [blame] | 773 | dma_range[3] = cpu_to_fdt32(phys_start >> 32); |
| 774 | dma_range[4] = cpu_to_fdt32(phys_start & 0xffffffff); |
Kumar Gala | e0475cd | 2008-10-22 23:33:56 -0500 | [diff] [blame] | 775 | } else { |
Marek Vasut | 22203f5 | 2019-07-09 02:49:29 +0200 | [diff] [blame] | 776 | dma_range[3] = cpu_to_fdt32(phys_start & 0xffffffff); |
Kumar Gala | e0475cd | 2008-10-22 23:33:56 -0500 | [diff] [blame] | 777 | } |
| 778 | |
| 779 | if (sizecell == 2) { |
Marek Vasut | 22203f5 | 2019-07-09 02:49:29 +0200 | [diff] [blame] | 780 | dma_range[3 + addrcell + 0] = |
| 781 | cpu_to_fdt32(size >> 32); |
| 782 | dma_range[3 + addrcell + 1] = |
| 783 | cpu_to_fdt32(size & 0xffffffff); |
Kumar Gala | e0475cd | 2008-10-22 23:33:56 -0500 | [diff] [blame] | 784 | } else { |
Marek Vasut | 22203f5 | 2019-07-09 02:49:29 +0200 | [diff] [blame] | 785 | dma_range[3 + addrcell + 0] = |
| 786 | cpu_to_fdt32(size & 0xffffffff); |
Kumar Gala | e0475cd | 2008-10-22 23:33:56 -0500 | [diff] [blame] | 787 | } |
| 788 | |
| 789 | dma_range += (3 + addrcell + sizecell); |
| 790 | } |
| 791 | |
| 792 | len = dma_range - &dma_ranges[0]; |
| 793 | if (len) |
| 794 | fdt_setprop(blob, phb_off, "dma-ranges", &dma_ranges[0], len*4); |
| 795 | |
| 796 | return 0; |
| 797 | } |
| 798 | #endif |
Stefan Roese | a0f10c9 | 2009-10-21 11:59:52 +0200 | [diff] [blame] | 799 | |
Matthew McClintock | acda3a4 | 2010-10-13 13:39:26 +0200 | [diff] [blame] | 800 | int fdt_increase_size(void *fdt, int add_len) |
| 801 | { |
| 802 | int newlen; |
| 803 | |
| 804 | newlen = fdt_totalsize(fdt) + add_len; |
| 805 | |
| 806 | /* Open in place with a new len */ |
| 807 | return fdt_open_into(fdt, fdt, newlen); |
| 808 | } |
| 809 | |
Anatolij Gustschin | 6c44c38 | 2010-03-16 17:10:05 +0100 | [diff] [blame] | 810 | #ifdef CONFIG_FDT_FIXUP_PARTITIONS |
| 811 | #include <jffs2/load_kernel.h> |
| 812 | #include <mtd_node.h> |
| 813 | |
Michal Simek | c2ed007 | 2018-07-31 14:31:04 +0200 | [diff] [blame] | 814 | static int fdt_del_subnodes(const void *blob, int parent_offset) |
Anatolij Gustschin | 6c44c38 | 2010-03-16 17:10:05 +0100 | [diff] [blame] | 815 | { |
| 816 | int off, ndepth; |
| 817 | int ret; |
| 818 | |
| 819 | for (ndepth = 0, off = fdt_next_node(blob, parent_offset, &ndepth); |
| 820 | (off >= 0) && (ndepth > 0); |
| 821 | off = fdt_next_node(blob, off, &ndepth)) { |
| 822 | if (ndepth == 1) { |
| 823 | debug("delete %s: offset: %x\n", |
| 824 | fdt_get_name(blob, off, 0), off); |
| 825 | ret = fdt_del_node((void *)blob, off); |
| 826 | if (ret < 0) { |
| 827 | printf("Can't delete node: %s\n", |
| 828 | fdt_strerror(ret)); |
| 829 | return ret; |
| 830 | } else { |
| 831 | ndepth = 0; |
| 832 | off = parent_offset; |
| 833 | } |
| 834 | } |
| 835 | } |
| 836 | return 0; |
| 837 | } |
| 838 | |
Michal Simek | c2ed007 | 2018-07-31 14:31:04 +0200 | [diff] [blame] | 839 | static int fdt_del_partitions(void *blob, int parent_offset) |
Anatolij Gustschin | 6c44c38 | 2010-03-16 17:10:05 +0100 | [diff] [blame] | 840 | { |
| 841 | const void *prop; |
| 842 | int ndepth = 0; |
| 843 | int off; |
| 844 | int ret; |
| 845 | |
| 846 | off = fdt_next_node(blob, parent_offset, &ndepth); |
| 847 | if (off > 0 && ndepth == 1) { |
| 848 | prop = fdt_getprop(blob, off, "label", NULL); |
| 849 | if (prop == NULL) { |
| 850 | /* |
| 851 | * Could not find label property, nand {}; node? |
| 852 | * Check subnode, delete partitions there if any. |
| 853 | */ |
| 854 | return fdt_del_partitions(blob, off); |
| 855 | } else { |
| 856 | ret = fdt_del_subnodes(blob, parent_offset); |
| 857 | if (ret < 0) { |
| 858 | printf("Can't remove subnodes: %s\n", |
| 859 | fdt_strerror(ret)); |
| 860 | return ret; |
| 861 | } |
| 862 | } |
| 863 | } |
| 864 | return 0; |
| 865 | } |
| 866 | |
Masahiro Yamada | 6566960 | 2020-07-15 19:35:47 +0900 | [diff] [blame] | 867 | static int fdt_node_set_part_info(void *blob, int parent_offset, |
| 868 | struct mtd_device *dev) |
Anatolij Gustschin | 6c44c38 | 2010-03-16 17:10:05 +0100 | [diff] [blame] | 869 | { |
| 870 | struct list_head *pentry; |
| 871 | struct part_info *part; |
Anatolij Gustschin | 6c44c38 | 2010-03-16 17:10:05 +0100 | [diff] [blame] | 872 | int off, ndepth = 0; |
| 873 | int part_num, ret; |
Stefan Mavrodiev | 51b0c03 | 2019-04-24 08:31:54 +0300 | [diff] [blame] | 874 | int sizecell; |
Anatolij Gustschin | 6c44c38 | 2010-03-16 17:10:05 +0100 | [diff] [blame] | 875 | char buf[64]; |
| 876 | |
| 877 | ret = fdt_del_partitions(blob, parent_offset); |
| 878 | if (ret < 0) |
| 879 | return ret; |
| 880 | |
| 881 | /* |
Stefan Mavrodiev | 51b0c03 | 2019-04-24 08:31:54 +0300 | [diff] [blame] | 882 | * Check if size/address is 1 or 2 cells. |
| 883 | * We assume #address-cells and #size-cells have same value. |
| 884 | */ |
| 885 | sizecell = fdt_getprop_u32_default_node(blob, parent_offset, |
| 886 | 0, "#size-cells", 1); |
| 887 | |
| 888 | /* |
Anatolij Gustschin | 6c44c38 | 2010-03-16 17:10:05 +0100 | [diff] [blame] | 889 | * Check if it is nand {}; subnode, adjust |
| 890 | * the offset in this case |
| 891 | */ |
| 892 | off = fdt_next_node(blob, parent_offset, &ndepth); |
| 893 | if (off > 0 && ndepth == 1) |
| 894 | parent_offset = off; |
| 895 | |
| 896 | part_num = 0; |
| 897 | list_for_each_prev(pentry, &dev->parts) { |
| 898 | int newoff; |
| 899 | |
| 900 | part = list_entry(pentry, struct part_info, link); |
| 901 | |
Scott Wood | efe7f30 | 2013-10-15 17:41:27 -0500 | [diff] [blame] | 902 | debug("%2d: %-20s0x%08llx\t0x%08llx\t%d\n", |
Anatolij Gustschin | 6c44c38 | 2010-03-16 17:10:05 +0100 | [diff] [blame] | 903 | part_num, part->name, part->size, |
| 904 | part->offset, part->mask_flags); |
| 905 | |
Scott Wood | efe7f30 | 2013-10-15 17:41:27 -0500 | [diff] [blame] | 906 | sprintf(buf, "partition@%llx", part->offset); |
Anatolij Gustschin | 6c44c38 | 2010-03-16 17:10:05 +0100 | [diff] [blame] | 907 | add_sub: |
| 908 | ret = fdt_add_subnode(blob, parent_offset, buf); |
| 909 | if (ret == -FDT_ERR_NOSPACE) { |
| 910 | ret = fdt_increase_size(blob, 512); |
| 911 | if (!ret) |
| 912 | goto add_sub; |
| 913 | else |
| 914 | goto err_size; |
| 915 | } else if (ret < 0) { |
| 916 | printf("Can't add partition node: %s\n", |
| 917 | fdt_strerror(ret)); |
| 918 | return ret; |
| 919 | } |
| 920 | newoff = ret; |
| 921 | |
| 922 | /* Check MTD_WRITEABLE_CMD flag */ |
| 923 | if (part->mask_flags & 1) { |
| 924 | add_ro: |
| 925 | ret = fdt_setprop(blob, newoff, "read_only", NULL, 0); |
| 926 | if (ret == -FDT_ERR_NOSPACE) { |
| 927 | ret = fdt_increase_size(blob, 512); |
| 928 | if (!ret) |
| 929 | goto add_ro; |
| 930 | else |
| 931 | goto err_size; |
| 932 | } else if (ret < 0) |
| 933 | goto err_prop; |
| 934 | } |
| 935 | |
Anatolij Gustschin | 6c44c38 | 2010-03-16 17:10:05 +0100 | [diff] [blame] | 936 | add_reg: |
Stefan Mavrodiev | 51b0c03 | 2019-04-24 08:31:54 +0300 | [diff] [blame] | 937 | if (sizecell == 2) { |
| 938 | ret = fdt_setprop_u64(blob, newoff, |
| 939 | "reg", part->offset); |
| 940 | if (!ret) |
| 941 | ret = fdt_appendprop_u64(blob, newoff, |
| 942 | "reg", part->size); |
| 943 | } else { |
| 944 | ret = fdt_setprop_u32(blob, newoff, |
| 945 | "reg", part->offset); |
| 946 | if (!ret) |
| 947 | ret = fdt_appendprop_u32(blob, newoff, |
| 948 | "reg", part->size); |
| 949 | } |
| 950 | |
Anatolij Gustschin | 6c44c38 | 2010-03-16 17:10:05 +0100 | [diff] [blame] | 951 | if (ret == -FDT_ERR_NOSPACE) { |
| 952 | ret = fdt_increase_size(blob, 512); |
| 953 | if (!ret) |
| 954 | goto add_reg; |
| 955 | else |
| 956 | goto err_size; |
| 957 | } else if (ret < 0) |
| 958 | goto err_prop; |
| 959 | |
| 960 | add_label: |
| 961 | ret = fdt_setprop_string(blob, newoff, "label", part->name); |
| 962 | if (ret == -FDT_ERR_NOSPACE) { |
| 963 | ret = fdt_increase_size(blob, 512); |
| 964 | if (!ret) |
| 965 | goto add_label; |
| 966 | else |
| 967 | goto err_size; |
| 968 | } else if (ret < 0) |
| 969 | goto err_prop; |
| 970 | |
| 971 | part_num++; |
| 972 | } |
| 973 | return 0; |
| 974 | err_size: |
| 975 | printf("Can't increase blob size: %s\n", fdt_strerror(ret)); |
| 976 | return ret; |
| 977 | err_prop: |
| 978 | printf("Can't add property: %s\n", fdt_strerror(ret)); |
| 979 | return ret; |
| 980 | } |
| 981 | |
| 982 | /* |
| 983 | * Update partitions in nor/nand nodes using info from |
| 984 | * mtdparts environment variable. The nodes to update are |
| 985 | * specified by node_info structure which contains mtd device |
| 986 | * type and compatible string: E. g. the board code in |
| 987 | * ft_board_setup() could use: |
| 988 | * |
| 989 | * struct node_info nodes[] = { |
| 990 | * { "fsl,mpc5121-nfc", MTD_DEV_TYPE_NAND, }, |
| 991 | * { "cfi-flash", MTD_DEV_TYPE_NOR, }, |
| 992 | * }; |
| 993 | * |
| 994 | * fdt_fixup_mtdparts(blob, nodes, ARRAY_SIZE(nodes)); |
| 995 | */ |
Masahiro Yamada | f3d4014 | 2018-07-19 16:28:22 +0900 | [diff] [blame] | 996 | void fdt_fixup_mtdparts(void *blob, const struct node_info *node_info, |
| 997 | int node_info_size) |
Anatolij Gustschin | 6c44c38 | 2010-03-16 17:10:05 +0100 | [diff] [blame] | 998 | { |
Anatolij Gustschin | 6c44c38 | 2010-03-16 17:10:05 +0100 | [diff] [blame] | 999 | struct mtd_device *dev; |
Anatolij Gustschin | 6c44c38 | 2010-03-16 17:10:05 +0100 | [diff] [blame] | 1000 | int i, idx; |
Matthias Schiffer | 01f078f | 2022-02-03 15:14:47 +0100 | [diff] [blame] | 1001 | int noff, parts; |
Masahiro Yamada | 030348f | 2020-07-17 10:46:18 +0900 | [diff] [blame] | 1002 | bool inited = false; |
Anatolij Gustschin | 6c44c38 | 2010-03-16 17:10:05 +0100 | [diff] [blame] | 1003 | |
| 1004 | for (i = 0; i < node_info_size; i++) { |
| 1005 | idx = 0; |
Masahiro Yamada | 2164429 | 2020-07-17 10:46:19 +0900 | [diff] [blame] | 1006 | |
Marek Behún | 5d6b448 | 2022-01-20 01:04:42 +0100 | [diff] [blame] | 1007 | fdt_for_each_node_by_compatible(noff, blob, -1, |
| 1008 | node_info[i].compat) { |
Masahiro Yamada | 2164429 | 2020-07-17 10:46:19 +0900 | [diff] [blame] | 1009 | const char *prop; |
| 1010 | |
| 1011 | prop = fdt_getprop(blob, noff, "status", NULL); |
| 1012 | if (prop && !strcmp(prop, "disabled")) |
| 1013 | continue; |
| 1014 | |
Anatolij Gustschin | 6c44c38 | 2010-03-16 17:10:05 +0100 | [diff] [blame] | 1015 | debug("%s: %s, mtd dev type %d\n", |
| 1016 | fdt_get_name(blob, noff, 0), |
Masahiro Yamada | f3d4014 | 2018-07-19 16:28:22 +0900 | [diff] [blame] | 1017 | node_info[i].compat, node_info[i].type); |
Masahiro Yamada | 030348f | 2020-07-17 10:46:18 +0900 | [diff] [blame] | 1018 | |
| 1019 | if (!inited) { |
| 1020 | if (mtdparts_init() != 0) |
| 1021 | return; |
| 1022 | inited = true; |
| 1023 | } |
| 1024 | |
Masahiro Yamada | f3d4014 | 2018-07-19 16:28:22 +0900 | [diff] [blame] | 1025 | dev = device_find(node_info[i].type, idx++); |
Anatolij Gustschin | 6c44c38 | 2010-03-16 17:10:05 +0100 | [diff] [blame] | 1026 | if (dev) { |
Matthias Schiffer | 01f078f | 2022-02-03 15:14:47 +0100 | [diff] [blame] | 1027 | parts = fdt_subnode_offset(blob, noff, |
| 1028 | "partitions"); |
| 1029 | if (parts < 0) |
| 1030 | parts = noff; |
| 1031 | |
| 1032 | if (fdt_node_set_part_info(blob, parts, dev)) |
Anatolij Gustschin | 6c44c38 | 2010-03-16 17:10:05 +0100 | [diff] [blame] | 1033 | return; /* return on error */ |
| 1034 | } |
Anatolij Gustschin | 6c44c38 | 2010-03-16 17:10:05 +0100 | [diff] [blame] | 1035 | } |
| 1036 | } |
| 1037 | } |
| 1038 | #endif |
Kumar Gala | f4ad4fd | 2010-03-30 10:19:26 -0500 | [diff] [blame] | 1039 | |
| 1040 | void fdt_del_node_and_alias(void *blob, const char *alias) |
| 1041 | { |
| 1042 | int off = fdt_path_offset(blob, alias); |
| 1043 | |
| 1044 | if (off < 0) |
| 1045 | return; |
| 1046 | |
| 1047 | fdt_del_node(blob, off); |
| 1048 | |
| 1049 | off = fdt_path_offset(blob, "/aliases"); |
| 1050 | fdt_delprop(blob, off, alias); |
| 1051 | } |
Kumar Gala | b87e793 | 2010-06-16 14:27:38 -0500 | [diff] [blame] | 1052 | |
Kumar Gala | b87e793 | 2010-06-16 14:27:38 -0500 | [diff] [blame] | 1053 | /* Max address size we deal with */ |
| 1054 | #define OF_MAX_ADDR_CELLS 4 |
Bin Meng | d9a0093 | 2015-12-07 01:39:47 -0800 | [diff] [blame] | 1055 | #define OF_BAD_ADDR FDT_ADDR_T_NONE |
Dario Binacchi | f8fc703 | 2021-05-01 17:05:26 +0200 | [diff] [blame] | 1056 | #define OF_CHECK_COUNTS(na, ns) ((na) > 0 && (na) <= OF_MAX_ADDR_CELLS && \ |
| 1057 | (ns) > 0) |
Kumar Gala | b87e793 | 2010-06-16 14:27:38 -0500 | [diff] [blame] | 1058 | |
| 1059 | /* Debug utility */ |
| 1060 | #ifdef DEBUG |
Kim Phillips | 6542c07 | 2013-01-16 14:00:11 +0000 | [diff] [blame] | 1061 | static void of_dump_addr(const char *s, const fdt32_t *addr, int na) |
Kumar Gala | b87e793 | 2010-06-16 14:27:38 -0500 | [diff] [blame] | 1062 | { |
| 1063 | printf("%s", s); |
| 1064 | while(na--) |
| 1065 | printf(" %08x", *(addr++)); |
| 1066 | printf("\n"); |
| 1067 | } |
| 1068 | #else |
Kim Phillips | 6542c07 | 2013-01-16 14:00:11 +0000 | [diff] [blame] | 1069 | static void of_dump_addr(const char *s, const fdt32_t *addr, int na) { } |
Kumar Gala | b87e793 | 2010-06-16 14:27:38 -0500 | [diff] [blame] | 1070 | #endif |
| 1071 | |
Paul Burton | dbef2f0 | 2016-05-17 07:43:24 +0100 | [diff] [blame] | 1072 | /** |
| 1073 | * struct of_bus - Callbacks for bus specific translators |
Paul Burton | 03f08c5 | 2016-05-17 07:43:25 +0100 | [diff] [blame] | 1074 | * @name: A string used to identify this bus in debug output. |
| 1075 | * @addresses: The name of the DT property from which addresses are |
| 1076 | * to be read, typically "reg". |
Paul Burton | dbef2f0 | 2016-05-17 07:43:24 +0100 | [diff] [blame] | 1077 | * @match: Return non-zero if the node whose parent is at |
| 1078 | * parentoffset in the FDT blob corresponds to a bus |
| 1079 | * of this type, otherwise return zero. If NULL a match |
| 1080 | * is assumed. |
Paul Burton | 03f08c5 | 2016-05-17 07:43:25 +0100 | [diff] [blame] | 1081 | * @count_cells:Count how many cells (be32 values) a node whose parent |
| 1082 | * is at parentoffset in the FDT blob will require to |
| 1083 | * represent its address (written to *addrc) & size |
| 1084 | * (written to *sizec). |
| 1085 | * @map: Map the address addr from the address space of this |
| 1086 | * bus to that of its parent, making use of the ranges |
| 1087 | * read from DT to an array at range. na and ns are the |
| 1088 | * number of cells (be32 values) used to hold and address |
| 1089 | * or size, respectively, for this bus. pna is the number |
| 1090 | * of cells used to hold an address for the parent bus. |
| 1091 | * Returns the address in the address space of the parent |
| 1092 | * bus. |
| 1093 | * @translate: Update the value of the address cells at addr within an |
| 1094 | * FDT by adding offset to it. na specifies the number of |
| 1095 | * cells used to hold the address being translated. Returns |
| 1096 | * zero on success, non-zero on error. |
Paul Burton | dbef2f0 | 2016-05-17 07:43:24 +0100 | [diff] [blame] | 1097 | * |
| 1098 | * Each bus type will include a struct of_bus in the of_busses array, |
| 1099 | * providing implementations of some or all of the functions used to |
| 1100 | * match the bus & handle address translation for its children. |
| 1101 | */ |
Kumar Gala | b87e793 | 2010-06-16 14:27:38 -0500 | [diff] [blame] | 1102 | struct of_bus { |
| 1103 | const char *name; |
| 1104 | const char *addresses; |
Stephen Warren | c99581b | 2016-08-05 09:47:50 -0600 | [diff] [blame] | 1105 | int (*match)(const void *blob, int parentoffset); |
| 1106 | void (*count_cells)(const void *blob, int parentoffset, |
Kumar Gala | b87e793 | 2010-06-16 14:27:38 -0500 | [diff] [blame] | 1107 | int *addrc, int *sizec); |
Kim Phillips | 6542c07 | 2013-01-16 14:00:11 +0000 | [diff] [blame] | 1108 | u64 (*map)(fdt32_t *addr, const fdt32_t *range, |
Kumar Gala | b87e793 | 2010-06-16 14:27:38 -0500 | [diff] [blame] | 1109 | int na, int ns, int pna); |
Kim Phillips | 6542c07 | 2013-01-16 14:00:11 +0000 | [diff] [blame] | 1110 | int (*translate)(fdt32_t *addr, u64 offset, int na); |
Kumar Gala | b87e793 | 2010-06-16 14:27:38 -0500 | [diff] [blame] | 1111 | }; |
| 1112 | |
| 1113 | /* Default translator (generic bus) */ |
Simon Glass | bb7c01e | 2017-05-18 20:09:26 -0600 | [diff] [blame] | 1114 | void fdt_support_default_count_cells(const void *blob, int parentoffset, |
Kumar Gala | b87e793 | 2010-06-16 14:27:38 -0500 | [diff] [blame] | 1115 | int *addrc, int *sizec) |
| 1116 | { |
Kim Phillips | 6542c07 | 2013-01-16 14:00:11 +0000 | [diff] [blame] | 1117 | const fdt32_t *prop; |
Scott Wood | 9989971 | 2010-08-12 18:37:39 -0500 | [diff] [blame] | 1118 | |
Simon Glass | 9fbc632 | 2014-10-23 18:58:57 -0600 | [diff] [blame] | 1119 | if (addrc) |
| 1120 | *addrc = fdt_address_cells(blob, parentoffset); |
Scott Wood | 9989971 | 2010-08-12 18:37:39 -0500 | [diff] [blame] | 1121 | |
| 1122 | if (sizec) { |
| 1123 | prop = fdt_getprop(blob, parentoffset, "#size-cells", NULL); |
| 1124 | if (prop) |
Kim Phillips | 6542c07 | 2013-01-16 14:00:11 +0000 | [diff] [blame] | 1125 | *sizec = be32_to_cpup(prop); |
Scott Wood | 9989971 | 2010-08-12 18:37:39 -0500 | [diff] [blame] | 1126 | else |
| 1127 | *sizec = 1; |
| 1128 | } |
Kumar Gala | b87e793 | 2010-06-16 14:27:38 -0500 | [diff] [blame] | 1129 | } |
| 1130 | |
Kim Phillips | 6542c07 | 2013-01-16 14:00:11 +0000 | [diff] [blame] | 1131 | static u64 of_bus_default_map(fdt32_t *addr, const fdt32_t *range, |
Kumar Gala | b87e793 | 2010-06-16 14:27:38 -0500 | [diff] [blame] | 1132 | int na, int ns, int pna) |
| 1133 | { |
| 1134 | u64 cp, s, da; |
| 1135 | |
Simon Glass | bb7c01e | 2017-05-18 20:09:26 -0600 | [diff] [blame] | 1136 | cp = fdt_read_number(range, na); |
| 1137 | s = fdt_read_number(range + na + pna, ns); |
| 1138 | da = fdt_read_number(addr, na); |
Kumar Gala | b87e793 | 2010-06-16 14:27:38 -0500 | [diff] [blame] | 1139 | |
Sekhar Nori | a98ba7f | 2018-12-06 15:20:47 +0530 | [diff] [blame] | 1140 | debug("OF: default map, cp=%llx, s=%llx, da=%llx\n", cp, s, da); |
Kumar Gala | b87e793 | 2010-06-16 14:27:38 -0500 | [diff] [blame] | 1141 | |
| 1142 | if (da < cp || da >= (cp + s)) |
| 1143 | return OF_BAD_ADDR; |
| 1144 | return da - cp; |
| 1145 | } |
| 1146 | |
Kim Phillips | 6542c07 | 2013-01-16 14:00:11 +0000 | [diff] [blame] | 1147 | static int of_bus_default_translate(fdt32_t *addr, u64 offset, int na) |
Kumar Gala | b87e793 | 2010-06-16 14:27:38 -0500 | [diff] [blame] | 1148 | { |
Simon Glass | bb7c01e | 2017-05-18 20:09:26 -0600 | [diff] [blame] | 1149 | u64 a = fdt_read_number(addr, na); |
Kumar Gala | b87e793 | 2010-06-16 14:27:38 -0500 | [diff] [blame] | 1150 | memset(addr, 0, na * 4); |
| 1151 | a += offset; |
| 1152 | if (na > 1) |
Kim Phillips | 6542c07 | 2013-01-16 14:00:11 +0000 | [diff] [blame] | 1153 | addr[na - 2] = cpu_to_fdt32(a >> 32); |
| 1154 | addr[na - 1] = cpu_to_fdt32(a & 0xffffffffu); |
Kumar Gala | b87e793 | 2010-06-16 14:27:38 -0500 | [diff] [blame] | 1155 | |
| 1156 | return 0; |
| 1157 | } |
| 1158 | |
Paul Burton | dbef2f0 | 2016-05-17 07:43:24 +0100 | [diff] [blame] | 1159 | #ifdef CONFIG_OF_ISA_BUS |
| 1160 | |
| 1161 | /* ISA bus translator */ |
Stephen Warren | c99581b | 2016-08-05 09:47:50 -0600 | [diff] [blame] | 1162 | static int of_bus_isa_match(const void *blob, int parentoffset) |
Paul Burton | dbef2f0 | 2016-05-17 07:43:24 +0100 | [diff] [blame] | 1163 | { |
| 1164 | const char *name; |
| 1165 | |
| 1166 | name = fdt_get_name(blob, parentoffset, NULL); |
| 1167 | if (!name) |
| 1168 | return 0; |
| 1169 | |
| 1170 | return !strcmp(name, "isa"); |
| 1171 | } |
| 1172 | |
Stephen Warren | c99581b | 2016-08-05 09:47:50 -0600 | [diff] [blame] | 1173 | static void of_bus_isa_count_cells(const void *blob, int parentoffset, |
Paul Burton | dbef2f0 | 2016-05-17 07:43:24 +0100 | [diff] [blame] | 1174 | int *addrc, int *sizec) |
| 1175 | { |
| 1176 | if (addrc) |
| 1177 | *addrc = 2; |
| 1178 | if (sizec) |
| 1179 | *sizec = 1; |
| 1180 | } |
| 1181 | |
| 1182 | static u64 of_bus_isa_map(fdt32_t *addr, const fdt32_t *range, |
| 1183 | int na, int ns, int pna) |
| 1184 | { |
| 1185 | u64 cp, s, da; |
| 1186 | |
| 1187 | /* Check address type match */ |
| 1188 | if ((addr[0] ^ range[0]) & cpu_to_be32(1)) |
| 1189 | return OF_BAD_ADDR; |
| 1190 | |
Simon Glass | bb7c01e | 2017-05-18 20:09:26 -0600 | [diff] [blame] | 1191 | cp = fdt_read_number(range + 1, na - 1); |
| 1192 | s = fdt_read_number(range + na + pna, ns); |
| 1193 | da = fdt_read_number(addr + 1, na - 1); |
Paul Burton | dbef2f0 | 2016-05-17 07:43:24 +0100 | [diff] [blame] | 1194 | |
Sekhar Nori | a98ba7f | 2018-12-06 15:20:47 +0530 | [diff] [blame] | 1195 | debug("OF: ISA map, cp=%llx, s=%llx, da=%llx\n", cp, s, da); |
Paul Burton | dbef2f0 | 2016-05-17 07:43:24 +0100 | [diff] [blame] | 1196 | |
| 1197 | if (da < cp || da >= (cp + s)) |
| 1198 | return OF_BAD_ADDR; |
| 1199 | return da - cp; |
| 1200 | } |
| 1201 | |
| 1202 | static int of_bus_isa_translate(fdt32_t *addr, u64 offset, int na) |
| 1203 | { |
| 1204 | return of_bus_default_translate(addr + 1, offset, na - 1); |
| 1205 | } |
| 1206 | |
| 1207 | #endif /* CONFIG_OF_ISA_BUS */ |
| 1208 | |
Kumar Gala | b87e793 | 2010-06-16 14:27:38 -0500 | [diff] [blame] | 1209 | /* Array of bus specific translators */ |
| 1210 | static struct of_bus of_busses[] = { |
Paul Burton | dbef2f0 | 2016-05-17 07:43:24 +0100 | [diff] [blame] | 1211 | #ifdef CONFIG_OF_ISA_BUS |
| 1212 | /* ISA */ |
| 1213 | { |
| 1214 | .name = "isa", |
| 1215 | .addresses = "reg", |
| 1216 | .match = of_bus_isa_match, |
| 1217 | .count_cells = of_bus_isa_count_cells, |
| 1218 | .map = of_bus_isa_map, |
| 1219 | .translate = of_bus_isa_translate, |
| 1220 | }, |
| 1221 | #endif /* CONFIG_OF_ISA_BUS */ |
Kumar Gala | b87e793 | 2010-06-16 14:27:38 -0500 | [diff] [blame] | 1222 | /* Default */ |
| 1223 | { |
| 1224 | .name = "default", |
| 1225 | .addresses = "reg", |
Simon Glass | bb7c01e | 2017-05-18 20:09:26 -0600 | [diff] [blame] | 1226 | .count_cells = fdt_support_default_count_cells, |
Kumar Gala | b87e793 | 2010-06-16 14:27:38 -0500 | [diff] [blame] | 1227 | .map = of_bus_default_map, |
| 1228 | .translate = of_bus_default_translate, |
| 1229 | }, |
| 1230 | }; |
| 1231 | |
Stephen Warren | c99581b | 2016-08-05 09:47:50 -0600 | [diff] [blame] | 1232 | static struct of_bus *of_match_bus(const void *blob, int parentoffset) |
Paul Burton | dbef2f0 | 2016-05-17 07:43:24 +0100 | [diff] [blame] | 1233 | { |
| 1234 | struct of_bus *bus; |
| 1235 | |
| 1236 | if (ARRAY_SIZE(of_busses) == 1) |
| 1237 | return of_busses; |
| 1238 | |
| 1239 | for (bus = of_busses; bus; bus++) { |
| 1240 | if (!bus->match || bus->match(blob, parentoffset)) |
| 1241 | return bus; |
| 1242 | } |
| 1243 | |
| 1244 | /* |
| 1245 | * We should always have matched the default bus at least, since |
| 1246 | * it has a NULL match field. If we didn't then it somehow isn't |
| 1247 | * in the of_busses array or something equally catastrophic has |
| 1248 | * gone wrong. |
| 1249 | */ |
| 1250 | assert(0); |
| 1251 | return NULL; |
| 1252 | } |
| 1253 | |
Stephen Warren | c99581b | 2016-08-05 09:47:50 -0600 | [diff] [blame] | 1254 | static int of_translate_one(const void *blob, int parent, struct of_bus *bus, |
Kim Phillips | 6542c07 | 2013-01-16 14:00:11 +0000 | [diff] [blame] | 1255 | struct of_bus *pbus, fdt32_t *addr, |
Kumar Gala | b87e793 | 2010-06-16 14:27:38 -0500 | [diff] [blame] | 1256 | int na, int ns, int pna, const char *rprop) |
| 1257 | { |
Kim Phillips | 6542c07 | 2013-01-16 14:00:11 +0000 | [diff] [blame] | 1258 | const fdt32_t *ranges; |
Kumar Gala | b87e793 | 2010-06-16 14:27:38 -0500 | [diff] [blame] | 1259 | int rlen; |
| 1260 | int rone; |
| 1261 | u64 offset = OF_BAD_ADDR; |
| 1262 | |
| 1263 | /* Normally, an absence of a "ranges" property means we are |
| 1264 | * crossing a non-translatable boundary, and thus the addresses |
| 1265 | * below the current not cannot be converted to CPU physical ones. |
| 1266 | * Unfortunately, while this is very clear in the spec, it's not |
| 1267 | * what Apple understood, and they do have things like /uni-n or |
| 1268 | * /ht nodes with no "ranges" property and a lot of perfectly |
| 1269 | * useable mapped devices below them. Thus we treat the absence of |
| 1270 | * "ranges" as equivalent to an empty "ranges" property which means |
| 1271 | * a 1:1 translation at that level. It's up to the caller not to try |
| 1272 | * to translate addresses that aren't supposed to be translated in |
| 1273 | * the first place. --BenH. |
| 1274 | */ |
Kim Phillips | 6542c07 | 2013-01-16 14:00:11 +0000 | [diff] [blame] | 1275 | ranges = fdt_getprop(blob, parent, rprop, &rlen); |
Kumar Gala | b87e793 | 2010-06-16 14:27:38 -0500 | [diff] [blame] | 1276 | if (ranges == NULL || rlen == 0) { |
Simon Glass | bb7c01e | 2017-05-18 20:09:26 -0600 | [diff] [blame] | 1277 | offset = fdt_read_number(addr, na); |
Kumar Gala | b87e793 | 2010-06-16 14:27:38 -0500 | [diff] [blame] | 1278 | memset(addr, 0, pna * 4); |
| 1279 | debug("OF: no ranges, 1:1 translation\n"); |
| 1280 | goto finish; |
| 1281 | } |
| 1282 | |
| 1283 | debug("OF: walking ranges...\n"); |
| 1284 | |
| 1285 | /* Now walk through the ranges */ |
| 1286 | rlen /= 4; |
| 1287 | rone = na + pna + ns; |
| 1288 | for (; rlen >= rone; rlen -= rone, ranges += rone) { |
| 1289 | offset = bus->map(addr, ranges, na, ns, pna); |
| 1290 | if (offset != OF_BAD_ADDR) |
| 1291 | break; |
| 1292 | } |
| 1293 | if (offset == OF_BAD_ADDR) { |
| 1294 | debug("OF: not found !\n"); |
| 1295 | return 1; |
| 1296 | } |
| 1297 | memcpy(addr, ranges + na, 4 * pna); |
| 1298 | |
| 1299 | finish: |
| 1300 | of_dump_addr("OF: parent translation for:", addr, pna); |
Masahiro Yamada | c7570a3 | 2018-08-06 20:47:40 +0900 | [diff] [blame] | 1301 | debug("OF: with offset: %llu\n", offset); |
Kumar Gala | b87e793 | 2010-06-16 14:27:38 -0500 | [diff] [blame] | 1302 | |
| 1303 | /* Translate it into parent bus space */ |
| 1304 | return pbus->translate(addr, offset, pna); |
| 1305 | } |
| 1306 | |
| 1307 | /* |
| 1308 | * Translate an address from the device-tree into a CPU physical address, |
| 1309 | * this walks up the tree and applies the various bus mappings on the |
| 1310 | * way. |
| 1311 | * |
| 1312 | * Note: We consider that crossing any level with #size-cells == 0 to mean |
| 1313 | * that translation is impossible (that is we are not dealing with a value |
| 1314 | * that can be mapped to a cpu physical address). This is not really specified |
| 1315 | * that way, but this is traditionally the way IBM at least do things |
| 1316 | */ |
Stephen Warren | c99581b | 2016-08-05 09:47:50 -0600 | [diff] [blame] | 1317 | static u64 __of_translate_address(const void *blob, int node_offset, |
| 1318 | const fdt32_t *in_addr, const char *rprop) |
Kumar Gala | b87e793 | 2010-06-16 14:27:38 -0500 | [diff] [blame] | 1319 | { |
| 1320 | int parent; |
| 1321 | struct of_bus *bus, *pbus; |
Kim Phillips | 6542c07 | 2013-01-16 14:00:11 +0000 | [diff] [blame] | 1322 | fdt32_t addr[OF_MAX_ADDR_CELLS]; |
Kumar Gala | b87e793 | 2010-06-16 14:27:38 -0500 | [diff] [blame] | 1323 | int na, ns, pna, pns; |
| 1324 | u64 result = OF_BAD_ADDR; |
| 1325 | |
| 1326 | debug("OF: ** translation for device %s **\n", |
| 1327 | fdt_get_name(blob, node_offset, NULL)); |
| 1328 | |
| 1329 | /* Get parent & match bus type */ |
| 1330 | parent = fdt_parent_offset(blob, node_offset); |
| 1331 | if (parent < 0) |
| 1332 | goto bail; |
Paul Burton | dbef2f0 | 2016-05-17 07:43:24 +0100 | [diff] [blame] | 1333 | bus = of_match_bus(blob, parent); |
Kumar Gala | b87e793 | 2010-06-16 14:27:38 -0500 | [diff] [blame] | 1334 | |
| 1335 | /* Cound address cells & copy address locally */ |
Scott Wood | 9989971 | 2010-08-12 18:37:39 -0500 | [diff] [blame] | 1336 | bus->count_cells(blob, parent, &na, &ns); |
Przemyslaw Marczak | f4d337c | 2016-01-12 15:40:44 +0100 | [diff] [blame] | 1337 | if (!OF_CHECK_COUNTS(na, ns)) { |
Kumar Gala | b87e793 | 2010-06-16 14:27:38 -0500 | [diff] [blame] | 1338 | printf("%s: Bad cell count for %s\n", __FUNCTION__, |
| 1339 | fdt_get_name(blob, node_offset, NULL)); |
| 1340 | goto bail; |
| 1341 | } |
| 1342 | memcpy(addr, in_addr, na * 4); |
| 1343 | |
| 1344 | debug("OF: bus is %s (na=%d, ns=%d) on %s\n", |
| 1345 | bus->name, na, ns, fdt_get_name(blob, parent, NULL)); |
| 1346 | of_dump_addr("OF: translating address:", addr, na); |
| 1347 | |
| 1348 | /* Translate */ |
| 1349 | for (;;) { |
| 1350 | /* Switch to parent bus */ |
| 1351 | node_offset = parent; |
| 1352 | parent = fdt_parent_offset(blob, node_offset); |
| 1353 | |
| 1354 | /* If root, we have finished */ |
| 1355 | if (parent < 0) { |
| 1356 | debug("OF: reached root node\n"); |
Simon Glass | bb7c01e | 2017-05-18 20:09:26 -0600 | [diff] [blame] | 1357 | result = fdt_read_number(addr, na); |
Kumar Gala | b87e793 | 2010-06-16 14:27:38 -0500 | [diff] [blame] | 1358 | break; |
| 1359 | } |
| 1360 | |
| 1361 | /* Get new parent bus and counts */ |
Paul Burton | dbef2f0 | 2016-05-17 07:43:24 +0100 | [diff] [blame] | 1362 | pbus = of_match_bus(blob, parent); |
Scott Wood | 9989971 | 2010-08-12 18:37:39 -0500 | [diff] [blame] | 1363 | pbus->count_cells(blob, parent, &pna, &pns); |
Przemyslaw Marczak | f4d337c | 2016-01-12 15:40:44 +0100 | [diff] [blame] | 1364 | if (!OF_CHECK_COUNTS(pna, pns)) { |
Kumar Gala | b87e793 | 2010-06-16 14:27:38 -0500 | [diff] [blame] | 1365 | printf("%s: Bad cell count for %s\n", __FUNCTION__, |
| 1366 | fdt_get_name(blob, node_offset, NULL)); |
| 1367 | break; |
| 1368 | } |
| 1369 | |
| 1370 | debug("OF: parent bus is %s (na=%d, ns=%d) on %s\n", |
| 1371 | pbus->name, pna, pns, fdt_get_name(blob, parent, NULL)); |
| 1372 | |
| 1373 | /* Apply bus translation */ |
| 1374 | if (of_translate_one(blob, node_offset, bus, pbus, |
| 1375 | addr, na, ns, pna, rprop)) |
| 1376 | break; |
| 1377 | |
| 1378 | /* Complete the move up one level */ |
| 1379 | na = pna; |
| 1380 | ns = pns; |
| 1381 | bus = pbus; |
| 1382 | |
| 1383 | of_dump_addr("OF: one level translation:", addr, na); |
| 1384 | } |
| 1385 | bail: |
| 1386 | |
| 1387 | return result; |
| 1388 | } |
| 1389 | |
Stephen Warren | c99581b | 2016-08-05 09:47:50 -0600 | [diff] [blame] | 1390 | u64 fdt_translate_address(const void *blob, int node_offset, |
| 1391 | const fdt32_t *in_addr) |
Kumar Gala | b87e793 | 2010-06-16 14:27:38 -0500 | [diff] [blame] | 1392 | { |
| 1393 | return __of_translate_address(blob, node_offset, in_addr, "ranges"); |
| 1394 | } |
Kumar Gala | 800d1d1 | 2010-07-04 12:48:21 -0500 | [diff] [blame] | 1395 | |
Fabien Dessenne | 22236e0 | 2019-05-31 15:11:30 +0200 | [diff] [blame] | 1396 | u64 fdt_translate_dma_address(const void *blob, int node_offset, |
| 1397 | const fdt32_t *in_addr) |
| 1398 | { |
| 1399 | return __of_translate_address(blob, node_offset, in_addr, "dma-ranges"); |
| 1400 | } |
| 1401 | |
Nicolas Saenz Julienne | 50d2fa4 | 2021-01-12 13:55:22 +0100 | [diff] [blame] | 1402 | int fdt_get_dma_range(const void *blob, int node, phys_addr_t *cpu, |
| 1403 | dma_addr_t *bus, u64 *size) |
| 1404 | { |
| 1405 | bool found_dma_ranges = false; |
| 1406 | struct of_bus *bus_node; |
| 1407 | const fdt32_t *ranges; |
| 1408 | int na, ns, pna, pns; |
| 1409 | int parent = node; |
| 1410 | int ret = 0; |
| 1411 | int len; |
| 1412 | |
| 1413 | /* Find the closest dma-ranges property */ |
| 1414 | while (parent >= 0) { |
| 1415 | ranges = fdt_getprop(blob, parent, "dma-ranges", &len); |
| 1416 | |
| 1417 | /* Ignore empty ranges, they imply no translation required */ |
| 1418 | if (ranges && len > 0) |
| 1419 | break; |
| 1420 | |
| 1421 | /* Once we find 'dma-ranges', then a missing one is an error */ |
| 1422 | if (found_dma_ranges && !ranges) { |
| 1423 | ret = -EINVAL; |
| 1424 | goto out; |
| 1425 | } |
| 1426 | |
| 1427 | if (ranges) |
| 1428 | found_dma_ranges = true; |
| 1429 | |
| 1430 | parent = fdt_parent_offset(blob, parent); |
| 1431 | } |
| 1432 | |
| 1433 | if (!ranges || parent < 0) { |
| 1434 | debug("no dma-ranges found for node %s\n", |
| 1435 | fdt_get_name(blob, node, NULL)); |
| 1436 | ret = -ENOENT; |
| 1437 | goto out; |
| 1438 | } |
| 1439 | |
| 1440 | /* switch to that node */ |
| 1441 | node = parent; |
| 1442 | parent = fdt_parent_offset(blob, node); |
| 1443 | if (parent < 0) { |
Vagrant Cascadian | d00c5c7 | 2021-12-21 13:06:58 -0800 | [diff] [blame] | 1444 | printf("Found dma-ranges in root node, shouldn't happen\n"); |
Nicolas Saenz Julienne | 50d2fa4 | 2021-01-12 13:55:22 +0100 | [diff] [blame] | 1445 | ret = -EINVAL; |
| 1446 | goto out; |
| 1447 | } |
| 1448 | |
| 1449 | /* Get the address sizes both for the bus and its parent */ |
| 1450 | bus_node = of_match_bus(blob, node); |
| 1451 | bus_node->count_cells(blob, node, &na, &ns); |
| 1452 | if (!OF_CHECK_COUNTS(na, ns)) { |
| 1453 | printf("%s: Bad cell count for %s\n", __FUNCTION__, |
| 1454 | fdt_get_name(blob, node, NULL)); |
| 1455 | return -EINVAL; |
| 1456 | goto out; |
| 1457 | } |
| 1458 | |
| 1459 | bus_node = of_match_bus(blob, parent); |
| 1460 | bus_node->count_cells(blob, parent, &pna, &pns); |
| 1461 | if (!OF_CHECK_COUNTS(pna, pns)) { |
| 1462 | printf("%s: Bad cell count for %s\n", __FUNCTION__, |
| 1463 | fdt_get_name(blob, parent, NULL)); |
| 1464 | return -EINVAL; |
| 1465 | goto out; |
| 1466 | } |
| 1467 | |
| 1468 | *bus = fdt_read_number(ranges, na); |
| 1469 | *cpu = fdt_translate_dma_address(blob, node, ranges + na); |
| 1470 | *size = fdt_read_number(ranges + na + pna, ns); |
| 1471 | out: |
| 1472 | return ret; |
| 1473 | } |
| 1474 | |
Kumar Gala | 800d1d1 | 2010-07-04 12:48:21 -0500 | [diff] [blame] | 1475 | /** |
| 1476 | * fdt_node_offset_by_compat_reg: Find a node that matches compatiable and |
| 1477 | * who's reg property matches a physical cpu address |
| 1478 | * |
| 1479 | * @blob: ptr to device tree |
| 1480 | * @compat: compatiable string to match |
| 1481 | * @compat_off: property name |
| 1482 | * |
| 1483 | */ |
| 1484 | int fdt_node_offset_by_compat_reg(void *blob, const char *compat, |
| 1485 | phys_addr_t compat_off) |
| 1486 | { |
Marek Behún | 5d6b448 | 2022-01-20 01:04:42 +0100 | [diff] [blame] | 1487 | int len, off; |
| 1488 | |
| 1489 | fdt_for_each_node_by_compatible(off, blob, -1, compat) { |
Kim Phillips | 6542c07 | 2013-01-16 14:00:11 +0000 | [diff] [blame] | 1490 | const fdt32_t *reg = fdt_getprop(blob, off, "reg", &len); |
Marek Behún | 5d6b448 | 2022-01-20 01:04:42 +0100 | [diff] [blame] | 1491 | if (reg && compat_off == fdt_translate_address(blob, off, reg)) |
| 1492 | return off; |
Kumar Gala | 800d1d1 | 2010-07-04 12:48:21 -0500 | [diff] [blame] | 1493 | } |
| 1494 | |
| 1495 | return -FDT_ERR_NOTFOUND; |
| 1496 | } |
| 1497 | |
Marek Behún | 4ef5ca6 | 2021-11-26 14:57:10 +0100 | [diff] [blame] | 1498 | static int vnode_offset_by_pathf(void *blob, const char *fmt, va_list ap) |
| 1499 | { |
| 1500 | char path[512]; |
| 1501 | int len; |
| 1502 | |
| 1503 | len = vsnprintf(path, sizeof(path), fmt, ap); |
| 1504 | if (len < 0 || len + 1 > sizeof(path)) |
| 1505 | return -FDT_ERR_NOSPACE; |
| 1506 | |
| 1507 | return fdt_path_offset(blob, path); |
| 1508 | } |
| 1509 | |
| 1510 | /** |
| 1511 | * fdt_node_offset_by_pathf: Find node offset by sprintf formatted path |
| 1512 | * |
| 1513 | * @blob: ptr to device tree |
| 1514 | * @fmt: path format |
| 1515 | * @ap: vsnprintf arguments |
| 1516 | */ |
| 1517 | int fdt_node_offset_by_pathf(void *blob, const char *fmt, ...) |
| 1518 | { |
| 1519 | va_list ap; |
| 1520 | int res; |
| 1521 | |
| 1522 | va_start(ap, fmt); |
| 1523 | res = vnode_offset_by_pathf(blob, fmt, ap); |
| 1524 | va_end(ap); |
| 1525 | |
| 1526 | return res; |
| 1527 | } |
| 1528 | |
Gerald Van Baren | af73788 | 2011-07-14 21:40:10 -0400 | [diff] [blame] | 1529 | /* |
Kumar Gala | f2f1c5a | 2011-08-01 00:23:23 -0500 | [diff] [blame] | 1530 | * fdt_set_phandle: Create a phandle property for the given node |
Gerald Van Baren | af73788 | 2011-07-14 21:40:10 -0400 | [diff] [blame] | 1531 | * |
| 1532 | * @fdt: ptr to device tree |
| 1533 | * @nodeoffset: node to update |
| 1534 | * @phandle: phandle value to set (must be unique) |
Kumar Gala | f2f1c5a | 2011-08-01 00:23:23 -0500 | [diff] [blame] | 1535 | */ |
| 1536 | int fdt_set_phandle(void *fdt, int nodeoffset, uint32_t phandle) |
Gerald Van Baren | af73788 | 2011-07-14 21:40:10 -0400 | [diff] [blame] | 1537 | { |
| 1538 | int ret; |
| 1539 | |
| 1540 | #ifdef DEBUG |
| 1541 | int off = fdt_node_offset_by_phandle(fdt, phandle); |
| 1542 | |
| 1543 | if ((off >= 0) && (off != nodeoffset)) { |
| 1544 | char buf[64]; |
| 1545 | |
| 1546 | fdt_get_path(fdt, nodeoffset, buf, sizeof(buf)); |
| 1547 | printf("Trying to update node %s with phandle %u ", |
| 1548 | buf, phandle); |
| 1549 | |
| 1550 | fdt_get_path(fdt, off, buf, sizeof(buf)); |
| 1551 | printf("that already exists in node %s.\n", buf); |
| 1552 | return -FDT_ERR_BADPHANDLE; |
| 1553 | } |
| 1554 | #endif |
| 1555 | |
| 1556 | ret = fdt_setprop_cell(fdt, nodeoffset, "phandle", phandle); |
Gerald Van Baren | af73788 | 2011-07-14 21:40:10 -0400 | [diff] [blame] | 1557 | |
| 1558 | return ret; |
| 1559 | } |
| 1560 | |
Kumar Gala | d5fc258 | 2011-08-01 00:25:20 -0500 | [diff] [blame] | 1561 | /* |
Marek Behún | ae0ef95 | 2021-11-26 14:57:09 +0100 | [diff] [blame] | 1562 | * fdt_create_phandle: Get or create a phandle property for the given node |
Kumar Gala | d5fc258 | 2011-08-01 00:25:20 -0500 | [diff] [blame] | 1563 | * |
| 1564 | * @fdt: ptr to device tree |
| 1565 | * @nodeoffset: node to update |
| 1566 | */ |
Timur Tabi | 0f46c80 | 2011-09-20 18:24:34 -0500 | [diff] [blame] | 1567 | unsigned int fdt_create_phandle(void *fdt, int nodeoffset) |
Kumar Gala | d5fc258 | 2011-08-01 00:25:20 -0500 | [diff] [blame] | 1568 | { |
| 1569 | /* see if there is a phandle already */ |
Marek Behún | 3577eba3 | 2021-11-26 14:57:07 +0100 | [diff] [blame] | 1570 | uint32_t phandle = fdt_get_phandle(fdt, nodeoffset); |
Kumar Gala | d5fc258 | 2011-08-01 00:25:20 -0500 | [diff] [blame] | 1571 | |
| 1572 | /* if we got 0, means no phandle so create one */ |
| 1573 | if (phandle == 0) { |
Timur Tabi | 0f46c80 | 2011-09-20 18:24:34 -0500 | [diff] [blame] | 1574 | int ret; |
| 1575 | |
Marek Behún | 3577eba3 | 2021-11-26 14:57:07 +0100 | [diff] [blame] | 1576 | ret = fdt_generate_phandle(fdt, &phandle); |
| 1577 | if (ret < 0) { |
| 1578 | printf("Can't generate phandle: %s\n", |
| 1579 | fdt_strerror(ret)); |
| 1580 | return 0; |
| 1581 | } |
| 1582 | |
Timur Tabi | 0f46c80 | 2011-09-20 18:24:34 -0500 | [diff] [blame] | 1583 | ret = fdt_set_phandle(fdt, nodeoffset, phandle); |
| 1584 | if (ret < 0) { |
| 1585 | printf("Can't set phandle %u: %s\n", phandle, |
| 1586 | fdt_strerror(ret)); |
| 1587 | return 0; |
| 1588 | } |
Kumar Gala | d5fc258 | 2011-08-01 00:25:20 -0500 | [diff] [blame] | 1589 | } |
| 1590 | |
| 1591 | return phandle; |
| 1592 | } |
| 1593 | |
Marek Behún | 4ef5ca6 | 2021-11-26 14:57:10 +0100 | [diff] [blame] | 1594 | /** |
| 1595 | * fdt_create_phandle_by_compatible: Get or create a phandle for first node with |
| 1596 | * given compatible |
| 1597 | * |
| 1598 | * @fdt: ptr to device tree |
| 1599 | * @compat: node's compatible string |
| 1600 | */ |
| 1601 | unsigned int fdt_create_phandle_by_compatible(void *fdt, const char *compat) |
| 1602 | { |
| 1603 | int offset = fdt_node_offset_by_compatible(fdt, -1, compat); |
| 1604 | |
| 1605 | if (offset < 0) { |
| 1606 | printf("Can't find node with compatible \"%s\": %s\n", compat, |
| 1607 | fdt_strerror(offset)); |
| 1608 | return 0; |
| 1609 | } |
| 1610 | |
| 1611 | return fdt_create_phandle(fdt, offset); |
| 1612 | } |
| 1613 | |
| 1614 | /** |
| 1615 | * fdt_create_phandle_by_pathf: Get or create a phandle for node given by |
| 1616 | * sprintf-formatted path |
| 1617 | * |
| 1618 | * @fdt: ptr to device tree |
| 1619 | * @fmt, ...: path format string and arguments to pass to sprintf |
| 1620 | */ |
| 1621 | unsigned int fdt_create_phandle_by_pathf(void *fdt, const char *fmt, ...) |
| 1622 | { |
| 1623 | va_list ap; |
| 1624 | int offset; |
| 1625 | |
| 1626 | va_start(ap, fmt); |
| 1627 | offset = vnode_offset_by_pathf(fdt, fmt, ap); |
| 1628 | va_end(ap); |
| 1629 | |
| 1630 | if (offset < 0) { |
| 1631 | printf("Can't find node by given path: %s\n", |
| 1632 | fdt_strerror(offset)); |
| 1633 | return 0; |
| 1634 | } |
| 1635 | |
| 1636 | return fdt_create_phandle(fdt, offset); |
| 1637 | } |
| 1638 | |
Shengzhou Liu | a7be802 | 2011-10-14 16:26:05 +0800 | [diff] [blame] | 1639 | /* |
| 1640 | * fdt_set_node_status: Set status for the given node |
| 1641 | * |
| 1642 | * @fdt: ptr to device tree |
| 1643 | * @nodeoffset: node to update |
Marek Behún | f872e83 | 2021-11-26 14:57:08 +0100 | [diff] [blame] | 1644 | * @status: FDT_STATUS_OKAY, FDT_STATUS_DISABLED, FDT_STATUS_FAIL |
Shengzhou Liu | a7be802 | 2011-10-14 16:26:05 +0800 | [diff] [blame] | 1645 | */ |
Marek Behún | f872e83 | 2021-11-26 14:57:08 +0100 | [diff] [blame] | 1646 | int fdt_set_node_status(void *fdt, int nodeoffset, enum fdt_status status) |
Shengzhou Liu | a7be802 | 2011-10-14 16:26:05 +0800 | [diff] [blame] | 1647 | { |
Shengzhou Liu | a7be802 | 2011-10-14 16:26:05 +0800 | [diff] [blame] | 1648 | int ret = 0; |
| 1649 | |
| 1650 | if (nodeoffset < 0) |
| 1651 | return nodeoffset; |
| 1652 | |
| 1653 | switch (status) { |
| 1654 | case FDT_STATUS_OKAY: |
| 1655 | ret = fdt_setprop_string(fdt, nodeoffset, "status", "okay"); |
| 1656 | break; |
| 1657 | case FDT_STATUS_DISABLED: |
| 1658 | ret = fdt_setprop_string(fdt, nodeoffset, "status", "disabled"); |
| 1659 | break; |
| 1660 | case FDT_STATUS_FAIL: |
| 1661 | ret = fdt_setprop_string(fdt, nodeoffset, "status", "fail"); |
| 1662 | break; |
Shengzhou Liu | a7be802 | 2011-10-14 16:26:05 +0800 | [diff] [blame] | 1663 | default: |
| 1664 | printf("Invalid fdt status: %x\n", status); |
| 1665 | ret = -1; |
| 1666 | break; |
| 1667 | } |
| 1668 | |
| 1669 | return ret; |
| 1670 | } |
| 1671 | |
| 1672 | /* |
| 1673 | * fdt_set_status_by_alias: Set status for the given node given an alias |
| 1674 | * |
| 1675 | * @fdt: ptr to device tree |
| 1676 | * @alias: alias of node to update |
Marek Behún | f872e83 | 2021-11-26 14:57:08 +0100 | [diff] [blame] | 1677 | * @status: FDT_STATUS_OKAY, FDT_STATUS_DISABLED, FDT_STATUS_FAIL |
Shengzhou Liu | a7be802 | 2011-10-14 16:26:05 +0800 | [diff] [blame] | 1678 | */ |
| 1679 | int fdt_set_status_by_alias(void *fdt, const char* alias, |
Marek Behún | f872e83 | 2021-11-26 14:57:08 +0100 | [diff] [blame] | 1680 | enum fdt_status status) |
Shengzhou Liu | a7be802 | 2011-10-14 16:26:05 +0800 | [diff] [blame] | 1681 | { |
| 1682 | int offset = fdt_path_offset(fdt, alias); |
| 1683 | |
Marek Behún | f872e83 | 2021-11-26 14:57:08 +0100 | [diff] [blame] | 1684 | return fdt_set_node_status(fdt, offset, status); |
Shengzhou Liu | a7be802 | 2011-10-14 16:26:05 +0800 | [diff] [blame] | 1685 | } |
| 1686 | |
Marek Behún | 4ef5ca6 | 2021-11-26 14:57:10 +0100 | [diff] [blame] | 1687 | /** |
| 1688 | * fdt_set_status_by_compatible: Set node status for first node with given |
| 1689 | * compatible |
| 1690 | * |
| 1691 | * @fdt: ptr to device tree |
| 1692 | * @compat: node's compatible string |
| 1693 | * @status: FDT_STATUS_OKAY, FDT_STATUS_DISABLED, FDT_STATUS_FAIL |
| 1694 | */ |
| 1695 | int fdt_set_status_by_compatible(void *fdt, const char *compat, |
| 1696 | enum fdt_status status) |
| 1697 | { |
| 1698 | int offset = fdt_node_offset_by_compatible(fdt, -1, compat); |
| 1699 | |
| 1700 | if (offset < 0) |
| 1701 | return offset; |
| 1702 | |
| 1703 | return fdt_set_node_status(fdt, offset, status); |
| 1704 | } |
| 1705 | |
| 1706 | /** |
| 1707 | * fdt_set_status_by_pathf: Set node status for node given by sprintf-formatted |
| 1708 | * path |
| 1709 | * |
| 1710 | * @fdt: ptr to device tree |
| 1711 | * @status: FDT_STATUS_OKAY, FDT_STATUS_DISABLED, FDT_STATUS_FAIL |
| 1712 | * @fmt, ...: path format string and arguments to pass to sprintf |
| 1713 | */ |
| 1714 | int fdt_set_status_by_pathf(void *fdt, enum fdt_status status, const char *fmt, |
| 1715 | ...) |
| 1716 | { |
| 1717 | va_list ap; |
| 1718 | int offset; |
| 1719 | |
| 1720 | va_start(ap, fmt); |
| 1721 | offset = vnode_offset_by_pathf(fdt, fmt, ap); |
| 1722 | va_end(ap); |
| 1723 | |
| 1724 | if (offset < 0) |
| 1725 | return offset; |
| 1726 | |
| 1727 | return fdt_set_node_status(fdt, offset, status); |
| 1728 | } |
| 1729 | |
Simon Glass | 03fbd25 | 2022-01-23 07:04:08 -0700 | [diff] [blame] | 1730 | #if defined(CONFIG_LCD) |
Anatolij Gustschin | 3b857f1 | 2010-08-18 11:25:20 +0200 | [diff] [blame] | 1731 | int fdt_add_edid(void *blob, const char *compat, unsigned char *edid_buf) |
| 1732 | { |
| 1733 | int noff; |
| 1734 | int ret; |
| 1735 | |
| 1736 | noff = fdt_node_offset_by_compatible(blob, -1, compat); |
| 1737 | if (noff != -FDT_ERR_NOTFOUND) { |
| 1738 | debug("%s: %s\n", fdt_get_name(blob, noff, 0), compat); |
| 1739 | add_edid: |
| 1740 | ret = fdt_setprop(blob, noff, "edid", edid_buf, 128); |
| 1741 | if (ret == -FDT_ERR_NOSPACE) { |
| 1742 | ret = fdt_increase_size(blob, 512); |
| 1743 | if (!ret) |
| 1744 | goto add_edid; |
| 1745 | else |
| 1746 | goto err_size; |
| 1747 | } else if (ret < 0) { |
| 1748 | printf("Can't add property: %s\n", fdt_strerror(ret)); |
| 1749 | return ret; |
| 1750 | } |
| 1751 | } |
| 1752 | return 0; |
| 1753 | err_size: |
| 1754 | printf("Can't increase blob size: %s\n", fdt_strerror(ret)); |
| 1755 | return ret; |
| 1756 | } |
| 1757 | #endif |
Timur Tabi | 8ebaf20 | 2011-05-03 13:24:07 -0500 | [diff] [blame] | 1758 | |
| 1759 | /* |
| 1760 | * Verify the physical address of device tree node for a given alias |
| 1761 | * |
| 1762 | * This function locates the device tree node of a given alias, and then |
| 1763 | * verifies that the physical address of that device matches the given |
| 1764 | * parameter. It displays a message if there is a mismatch. |
| 1765 | * |
| 1766 | * Returns 1 on success, 0 on failure |
| 1767 | */ |
| 1768 | int fdt_verify_alias_address(void *fdt, int anode, const char *alias, u64 addr) |
| 1769 | { |
| 1770 | const char *path; |
Kim Phillips | 6542c07 | 2013-01-16 14:00:11 +0000 | [diff] [blame] | 1771 | const fdt32_t *reg; |
Timur Tabi | 8ebaf20 | 2011-05-03 13:24:07 -0500 | [diff] [blame] | 1772 | int node, len; |
| 1773 | u64 dt_addr; |
| 1774 | |
| 1775 | path = fdt_getprop(fdt, anode, alias, NULL); |
| 1776 | if (!path) { |
| 1777 | /* If there's no such alias, then it's not a failure */ |
| 1778 | return 1; |
| 1779 | } |
| 1780 | |
| 1781 | node = fdt_path_offset(fdt, path); |
| 1782 | if (node < 0) { |
| 1783 | printf("Warning: device tree alias '%s' points to invalid " |
| 1784 | "node %s.\n", alias, path); |
| 1785 | return 0; |
| 1786 | } |
| 1787 | |
| 1788 | reg = fdt_getprop(fdt, node, "reg", &len); |
| 1789 | if (!reg) { |
| 1790 | printf("Warning: device tree node '%s' has no address.\n", |
| 1791 | path); |
| 1792 | return 0; |
| 1793 | } |
| 1794 | |
| 1795 | dt_addr = fdt_translate_address(fdt, node, reg); |
| 1796 | if (addr != dt_addr) { |
Masahiro Yamada | c7570a3 | 2018-08-06 20:47:40 +0900 | [diff] [blame] | 1797 | printf("Warning: U-Boot configured device %s at address %llu,\n" |
| 1798 | "but the device tree has it address %llx.\n", |
| 1799 | alias, addr, dt_addr); |
Timur Tabi | 8ebaf20 | 2011-05-03 13:24:07 -0500 | [diff] [blame] | 1800 | return 0; |
| 1801 | } |
| 1802 | |
| 1803 | return 1; |
| 1804 | } |
| 1805 | |
| 1806 | /* |
| 1807 | * Returns the base address of an SOC or PCI node |
| 1808 | */ |
Simon Glass | 293340a | 2017-05-18 20:09:00 -0600 | [diff] [blame] | 1809 | u64 fdt_get_base_address(const void *fdt, int node) |
Timur Tabi | 8ebaf20 | 2011-05-03 13:24:07 -0500 | [diff] [blame] | 1810 | { |
| 1811 | int size; |
Kim Phillips | 6542c07 | 2013-01-16 14:00:11 +0000 | [diff] [blame] | 1812 | const fdt32_t *prop; |
Timur Tabi | 8ebaf20 | 2011-05-03 13:24:07 -0500 | [diff] [blame] | 1813 | |
Simon Glass | 29f304b | 2017-07-04 13:31:20 -0600 | [diff] [blame] | 1814 | prop = fdt_getprop(fdt, node, "reg", &size); |
Timur Tabi | 8ebaf20 | 2011-05-03 13:24:07 -0500 | [diff] [blame] | 1815 | |
Masahiro Yamada | b1dd47c | 2019-06-27 16:39:57 +0900 | [diff] [blame] | 1816 | return prop ? fdt_translate_address(fdt, node, prop) : OF_BAD_ADDR; |
Timur Tabi | 8ebaf20 | 2011-05-03 13:24:07 -0500 | [diff] [blame] | 1817 | } |
Alexander Graf | 19555be | 2014-04-11 17:09:41 +0200 | [diff] [blame] | 1818 | |
| 1819 | /* |
Bin Meng | d67d7d0 | 2021-02-25 17:22:24 +0800 | [diff] [blame] | 1820 | * Read a property of size <prop_len>. Currently only supports 1 or 2 cells, |
| 1821 | * or 3 cells specially for a PCI address. |
Alexander Graf | 19555be | 2014-04-11 17:09:41 +0200 | [diff] [blame] | 1822 | */ |
| 1823 | static int fdt_read_prop(const fdt32_t *prop, int prop_len, int cell_off, |
| 1824 | uint64_t *val, int cells) |
| 1825 | { |
Bin Meng | d67d7d0 | 2021-02-25 17:22:24 +0800 | [diff] [blame] | 1826 | const fdt32_t *prop32; |
| 1827 | const unaligned_fdt64_t *prop64; |
Alexander Graf | 19555be | 2014-04-11 17:09:41 +0200 | [diff] [blame] | 1828 | |
| 1829 | if ((cell_off + cells) > prop_len) |
| 1830 | return -FDT_ERR_NOSPACE; |
| 1831 | |
Bin Meng | d67d7d0 | 2021-02-25 17:22:24 +0800 | [diff] [blame] | 1832 | prop32 = &prop[cell_off]; |
| 1833 | |
| 1834 | /* |
| 1835 | * Special handling for PCI address in PCI bus <ranges> |
| 1836 | * |
| 1837 | * PCI child address is made up of 3 cells. Advance the cell offset |
| 1838 | * by 1 so that the PCI child address can be correctly read. |
| 1839 | */ |
| 1840 | if (cells == 3) |
| 1841 | cell_off += 1; |
| 1842 | prop64 = (const fdt64_t *)&prop[cell_off]; |
| 1843 | |
Alexander Graf | 19555be | 2014-04-11 17:09:41 +0200 | [diff] [blame] | 1844 | switch (cells) { |
| 1845 | case 1: |
| 1846 | *val = fdt32_to_cpu(*prop32); |
| 1847 | break; |
| 1848 | case 2: |
Bin Meng | d67d7d0 | 2021-02-25 17:22:24 +0800 | [diff] [blame] | 1849 | case 3: |
Alexander Graf | 19555be | 2014-04-11 17:09:41 +0200 | [diff] [blame] | 1850 | *val = fdt64_to_cpu(*prop64); |
| 1851 | break; |
| 1852 | default: |
| 1853 | return -FDT_ERR_NOSPACE; |
| 1854 | } |
| 1855 | |
| 1856 | return 0; |
| 1857 | } |
| 1858 | |
| 1859 | /** |
| 1860 | * fdt_read_range - Read a node's n'th range property |
| 1861 | * |
| 1862 | * @fdt: ptr to device tree |
| 1863 | * @node: offset of node |
| 1864 | * @n: range index |
| 1865 | * @child_addr: pointer to storage for the "child address" field |
| 1866 | * @addr: pointer to storage for the CPU view translated physical start |
| 1867 | * @len: pointer to storage for the range length |
| 1868 | * |
| 1869 | * Convenience function that reads and interprets a specific range out of |
| 1870 | * a number of the "ranges" property array. |
| 1871 | */ |
| 1872 | int fdt_read_range(void *fdt, int node, int n, uint64_t *child_addr, |
| 1873 | uint64_t *addr, uint64_t *len) |
| 1874 | { |
| 1875 | int pnode = fdt_parent_offset(fdt, node); |
| 1876 | const fdt32_t *ranges; |
| 1877 | int pacells; |
| 1878 | int acells; |
| 1879 | int scells; |
| 1880 | int ranges_len; |
| 1881 | int cell = 0; |
| 1882 | int r = 0; |
| 1883 | |
| 1884 | /* |
| 1885 | * The "ranges" property is an array of |
| 1886 | * { <child address> <parent address> <size in child address space> } |
| 1887 | * |
| 1888 | * All 3 elements can span a diffent number of cells. Fetch their size. |
| 1889 | */ |
| 1890 | pacells = fdt_getprop_u32_default_node(fdt, pnode, 0, "#address-cells", 1); |
| 1891 | acells = fdt_getprop_u32_default_node(fdt, node, 0, "#address-cells", 1); |
| 1892 | scells = fdt_getprop_u32_default_node(fdt, node, 0, "#size-cells", 1); |
| 1893 | |
| 1894 | /* Now try to get the ranges property */ |
| 1895 | ranges = fdt_getprop(fdt, node, "ranges", &ranges_len); |
| 1896 | if (!ranges) |
| 1897 | return -FDT_ERR_NOTFOUND; |
| 1898 | ranges_len /= sizeof(uint32_t); |
| 1899 | |
| 1900 | /* Jump to the n'th entry */ |
| 1901 | cell = n * (pacells + acells + scells); |
| 1902 | |
| 1903 | /* Read <child address> */ |
| 1904 | if (child_addr) { |
| 1905 | r = fdt_read_prop(ranges, ranges_len, cell, child_addr, |
| 1906 | acells); |
| 1907 | if (r) |
| 1908 | return r; |
| 1909 | } |
| 1910 | cell += acells; |
| 1911 | |
| 1912 | /* Read <parent address> */ |
| 1913 | if (addr) |
| 1914 | *addr = fdt_translate_address(fdt, node, ranges + cell); |
| 1915 | cell += pacells; |
| 1916 | |
| 1917 | /* Read <size in child address space> */ |
| 1918 | if (len) { |
| 1919 | r = fdt_read_prop(ranges, ranges_len, cell, len, scells); |
| 1920 | if (r) |
| 1921 | return r; |
| 1922 | } |
| 1923 | |
| 1924 | return 0; |
| 1925 | } |
Hans de Goede | ea87637 | 2014-11-17 15:29:11 +0100 | [diff] [blame] | 1926 | |
| 1927 | /** |
| 1928 | * fdt_setup_simplefb_node - Fill and enable a simplefb node |
| 1929 | * |
| 1930 | * @fdt: ptr to device tree |
| 1931 | * @node: offset of the simplefb node |
| 1932 | * @base_address: framebuffer base address |
| 1933 | * @width: width in pixels |
| 1934 | * @height: height in pixels |
| 1935 | * @stride: bytes per line |
| 1936 | * @format: pixel format string |
| 1937 | * |
| 1938 | * Convenience function to fill and enable a simplefb node. |
| 1939 | */ |
| 1940 | int fdt_setup_simplefb_node(void *fdt, int node, u64 base_address, u32 width, |
| 1941 | u32 height, u32 stride, const char *format) |
| 1942 | { |
| 1943 | char name[32]; |
| 1944 | fdt32_t cells[4]; |
| 1945 | int i, addrc, sizec, ret; |
| 1946 | |
Simon Glass | bb7c01e | 2017-05-18 20:09:26 -0600 | [diff] [blame] | 1947 | fdt_support_default_count_cells(fdt, fdt_parent_offset(fdt, node), |
| 1948 | &addrc, &sizec); |
Hans de Goede | ea87637 | 2014-11-17 15:29:11 +0100 | [diff] [blame] | 1949 | i = 0; |
| 1950 | if (addrc == 2) |
| 1951 | cells[i++] = cpu_to_fdt32(base_address >> 32); |
| 1952 | cells[i++] = cpu_to_fdt32(base_address); |
| 1953 | if (sizec == 2) |
| 1954 | cells[i++] = 0; |
| 1955 | cells[i++] = cpu_to_fdt32(height * stride); |
| 1956 | |
| 1957 | ret = fdt_setprop(fdt, node, "reg", cells, sizeof(cells[0]) * i); |
| 1958 | if (ret < 0) |
| 1959 | return ret; |
| 1960 | |
Masahiro Yamada | c7570a3 | 2018-08-06 20:47:40 +0900 | [diff] [blame] | 1961 | snprintf(name, sizeof(name), "framebuffer@%llx", base_address); |
Hans de Goede | ea87637 | 2014-11-17 15:29:11 +0100 | [diff] [blame] | 1962 | ret = fdt_set_name(fdt, node, name); |
| 1963 | if (ret < 0) |
| 1964 | return ret; |
| 1965 | |
| 1966 | ret = fdt_setprop_u32(fdt, node, "width", width); |
| 1967 | if (ret < 0) |
| 1968 | return ret; |
| 1969 | |
| 1970 | ret = fdt_setprop_u32(fdt, node, "height", height); |
| 1971 | if (ret < 0) |
| 1972 | return ret; |
| 1973 | |
| 1974 | ret = fdt_setprop_u32(fdt, node, "stride", stride); |
| 1975 | if (ret < 0) |
| 1976 | return ret; |
| 1977 | |
| 1978 | ret = fdt_setprop_string(fdt, node, "format", format); |
| 1979 | if (ret < 0) |
| 1980 | return ret; |
| 1981 | |
| 1982 | ret = fdt_setprop_string(fdt, node, "status", "okay"); |
| 1983 | if (ret < 0) |
| 1984 | return ret; |
| 1985 | |
| 1986 | return 0; |
| 1987 | } |
Tim Harvey | 5d7c6b5 | 2015-04-08 11:45:39 -0700 | [diff] [blame] | 1988 | |
| 1989 | /* |
| 1990 | * Update native-mode in display-timings from display environment variable. |
| 1991 | * The node to update are specified by path. |
| 1992 | */ |
| 1993 | int fdt_fixup_display(void *blob, const char *path, const char *display) |
| 1994 | { |
| 1995 | int off, toff; |
| 1996 | |
| 1997 | if (!display || !path) |
| 1998 | return -FDT_ERR_NOTFOUND; |
| 1999 | |
| 2000 | toff = fdt_path_offset(blob, path); |
| 2001 | if (toff >= 0) |
| 2002 | toff = fdt_subnode_offset(blob, toff, "display-timings"); |
| 2003 | if (toff < 0) |
| 2004 | return toff; |
| 2005 | |
| 2006 | for (off = fdt_first_subnode(blob, toff); |
| 2007 | off >= 0; |
| 2008 | off = fdt_next_subnode(blob, off)) { |
| 2009 | uint32_t h = fdt_get_phandle(blob, off); |
| 2010 | debug("%s:0x%x\n", fdt_get_name(blob, off, NULL), |
| 2011 | fdt32_to_cpu(h)); |
| 2012 | if (strcasecmp(fdt_get_name(blob, off, NULL), display) == 0) |
| 2013 | return fdt_setprop_u32(blob, toff, "native-mode", h); |
| 2014 | } |
| 2015 | return toff; |
| 2016 | } |
Pantelis Antoniou | 592386d | 2017-09-04 23:12:11 +0300 | [diff] [blame] | 2017 | |
| 2018 | #ifdef CONFIG_OF_LIBFDT_OVERLAY |
| 2019 | /** |
| 2020 | * fdt_overlay_apply_verbose - Apply an overlay with verbose error reporting |
| 2021 | * |
| 2022 | * @fdt: ptr to device tree |
| 2023 | * @fdto: ptr to device tree overlay |
| 2024 | * |
| 2025 | * Convenience function to apply an overlay and display helpful messages |
| 2026 | * in the case of an error |
| 2027 | */ |
| 2028 | int fdt_overlay_apply_verbose(void *fdt, void *fdto) |
| 2029 | { |
| 2030 | int err; |
| 2031 | bool has_symbols; |
| 2032 | |
| 2033 | err = fdt_path_offset(fdt, "/__symbols__"); |
| 2034 | has_symbols = err >= 0; |
| 2035 | |
| 2036 | err = fdt_overlay_apply(fdt, fdto); |
| 2037 | if (err < 0) { |
| 2038 | printf("failed on fdt_overlay_apply(): %s\n", |
| 2039 | fdt_strerror(err)); |
| 2040 | if (!has_symbols) { |
| 2041 | printf("base fdt does did not have a /__symbols__ node\n"); |
| 2042 | printf("make sure you've compiled with -@\n"); |
| 2043 | } |
| 2044 | } |
| 2045 | return err; |
| 2046 | } |
| 2047 | #endif |
Kory Maincent | 623e1ac | 2021-05-04 19:31:21 +0200 | [diff] [blame] | 2048 | |
| 2049 | /** |
| 2050 | * fdt_valid() - Check if an FDT is valid. If not, change it to NULL |
| 2051 | * |
| 2052 | * @blobp: Pointer to FDT pointer |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 2053 | * Return: 1 if OK, 0 if bad (in which case *blobp is set to NULL) |
Kory Maincent | 623e1ac | 2021-05-04 19:31:21 +0200 | [diff] [blame] | 2054 | */ |
| 2055 | int fdt_valid(struct fdt_header **blobp) |
| 2056 | { |
| 2057 | const void *blob = *blobp; |
| 2058 | int err; |
| 2059 | |
| 2060 | if (!blob) { |
| 2061 | printf("The address of the fdt is invalid (NULL).\n"); |
| 2062 | return 0; |
| 2063 | } |
| 2064 | |
| 2065 | err = fdt_check_header(blob); |
| 2066 | if (err == 0) |
| 2067 | return 1; /* valid */ |
| 2068 | |
| 2069 | if (err < 0) { |
| 2070 | printf("libfdt fdt_check_header(): %s", fdt_strerror(err)); |
| 2071 | /* |
| 2072 | * Be more informative on bad version. |
| 2073 | */ |
| 2074 | if (err == -FDT_ERR_BADVERSION) { |
| 2075 | if (fdt_version(blob) < |
| 2076 | FDT_FIRST_SUPPORTED_VERSION) { |
| 2077 | printf(" - too old, fdt %d < %d", |
| 2078 | fdt_version(blob), |
| 2079 | FDT_FIRST_SUPPORTED_VERSION); |
| 2080 | } |
| 2081 | if (fdt_last_comp_version(blob) > |
| 2082 | FDT_LAST_SUPPORTED_VERSION) { |
| 2083 | printf(" - too new, fdt %d > %d", |
| 2084 | fdt_version(blob), |
| 2085 | FDT_LAST_SUPPORTED_VERSION); |
| 2086 | } |
| 2087 | } |
| 2088 | printf("\n"); |
| 2089 | *blobp = NULL; |
| 2090 | return 0; |
| 2091 | } |
| 2092 | return 1; |
| 2093 | } |