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