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