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