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