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