Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Philipp Tomsich | 5c360af | 2017-07-19 22:04:32 +0200 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2017 Theobroma Systems Design und Consulting GmbH |
Philipp Tomsich | 5c360af | 2017-07-19 22:04:32 +0200 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <common.h> |
| 7 | #include <dm.h> |
Quentin Schulz | 082652e | 2024-01-18 14:55:57 +0100 | [diff] [blame] | 8 | #include <fdt_support.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 9 | #include <log.h> |
Philipp Tomsich | 5c360af | 2017-07-19 22:04:32 +0200 | [diff] [blame] | 10 | #include <mmc.h> |
| 11 | #include <spl.h> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 12 | #include <asm/global_data.h> |
Quentin Schulz | ccedc90 | 2024-01-18 14:55:58 +0100 | [diff] [blame] | 13 | #include <dm/uclass-internal.h> |
Philipp Tomsich | 5c360af | 2017-07-19 22:04:32 +0200 | [diff] [blame] | 14 | |
Kever Yang | fec76f5 | 2019-07-17 18:12:20 +0800 | [diff] [blame] | 15 | #if CONFIG_IS_ENABLED(OF_LIBFDT) |
Philipp Tomsich | 1c4f63a | 2017-09-29 19:27:56 +0200 | [diff] [blame] | 16 | /** |
| 17 | * spl_node_to_boot_device() - maps from a DT-node to a SPL boot device |
| 18 | * @node: of_offset of the node |
| 19 | * |
| 20 | * The SPL framework uses BOOT_DEVICE_... constants to identify its boot |
| 21 | * sources. These may take on a device-specific meaning, depending on |
| 22 | * what nodes are enabled in a DTS (e.g. BOOT_DEVICE_MMC1 may refer to |
| 23 | * different controllers/block-devices, depending on which SD/MMC controllers |
| 24 | * are enabled in any given DTS). This function maps from a DT-node back |
| 25 | * onto a BOOT_DEVICE_... constant, considering the currently active devices. |
| 26 | * |
| 27 | * Returns |
| 28 | * -ENOENT, if no device matching the node could be found |
| 29 | * -ENOSYS, if the device matching the node can not be mapped onto a |
| 30 | * SPL boot device (e.g. the third MMC device) |
| 31 | * -1, for unspecified failures |
| 32 | * a positive integer (from the BOOT_DEVICE_... family) on succes. |
| 33 | */ |
| 34 | |
Philipp Tomsich | 5c360af | 2017-07-19 22:04:32 +0200 | [diff] [blame] | 35 | static int spl_node_to_boot_device(int node) |
| 36 | { |
| 37 | struct udevice *parent; |
| 38 | |
| 39 | /* |
| 40 | * This should eventually move into the SPL code, once SPL becomes |
| 41 | * aware of the block-device layer. Until then (and to avoid unneeded |
Thomas Hebb | fd37f24 | 2019-11-13 18:18:03 -0800 | [diff] [blame] | 42 | * delays in getting this feature out), it lives at the board-level. |
Philipp Tomsich | 5c360af | 2017-07-19 22:04:32 +0200 | [diff] [blame] | 43 | */ |
| 44 | if (!uclass_get_device_by_of_offset(UCLASS_MMC, node, &parent)) { |
| 45 | struct udevice *dev; |
| 46 | struct blk_desc *desc = NULL; |
| 47 | |
| 48 | for (device_find_first_child(parent, &dev); |
| 49 | dev; |
| 50 | device_find_next_child(&dev)) { |
| 51 | if (device_get_uclass_id(dev) == UCLASS_BLK) { |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 52 | desc = dev_get_uclass_plat(dev); |
Philipp Tomsich | 5c360af | 2017-07-19 22:04:32 +0200 | [diff] [blame] | 53 | break; |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | if (!desc) |
| 58 | return -ENOENT; |
| 59 | |
| 60 | switch (desc->devnum) { |
| 61 | case 0: |
| 62 | return BOOT_DEVICE_MMC1; |
| 63 | case 1: |
| 64 | return BOOT_DEVICE_MMC2; |
| 65 | default: |
| 66 | return -ENOSYS; |
| 67 | } |
Simon Glass | a1766a5 | 2019-01-21 14:53:28 -0700 | [diff] [blame] | 68 | } else if (!uclass_get_device_by_of_offset(UCLASS_SPI_FLASH, node, |
| 69 | &parent)) { |
| 70 | return BOOT_DEVICE_SPI; |
Philipp Tomsich | 5c360af | 2017-07-19 22:04:32 +0200 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | /* |
| 74 | * SPL doesn't differentiate SPI flashes, so we keep the detection |
| 75 | * brief and inaccurate... hopefully, the common SPL layer can be |
| 76 | * extended with awareness of the BLK layer (and matching OF_CONTROL) |
| 77 | * soon. |
| 78 | */ |
| 79 | if (!uclass_get_device_by_of_offset(UCLASS_SPI_FLASH, node, &parent)) |
| 80 | return BOOT_DEVICE_SPI; |
| 81 | |
| 82 | return -1; |
| 83 | } |
| 84 | |
Philipp Tomsich | ec83ff5 | 2017-09-29 19:27:57 +0200 | [diff] [blame] | 85 | /** |
| 86 | * board_spl_was_booted_from() - retrieves the of-path the SPL was loaded from |
| 87 | * |
| 88 | * To support a 'same-as-spl' specification in the search-order for the next |
| 89 | * stage, we need a SoC- or board-specific way to handshake with what 'came |
| 90 | * before us' (either a BROM or TPL stage) and map the info retrieved onto |
| 91 | * a OF path. |
| 92 | * |
| 93 | * Returns |
| 94 | * NULL, on failure or if the device could not be identified |
| 95 | * a of_path (a string), on success |
| 96 | */ |
| 97 | __weak const char *board_spl_was_booted_from(void) |
| 98 | { |
| 99 | debug("%s: no support for 'same-as-spl' for this board\n", __func__); |
| 100 | return NULL; |
| 101 | } |
| 102 | |
Philipp Tomsich | 5c360af | 2017-07-19 22:04:32 +0200 | [diff] [blame] | 103 | void board_boot_order(u32 *spl_boot_list) |
| 104 | { |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 105 | /* In case of no fdt (or only plat), use spl_boot_device() */ |
Urja Rannikko | f0f5652 | 2020-05-13 19:15:22 +0000 | [diff] [blame] | 106 | if (!CONFIG_IS_ENABLED(OF_CONTROL) || CONFIG_IS_ENABLED(OF_PLATDATA)) { |
| 107 | spl_boot_list[0] = spl_boot_device(); |
| 108 | return; |
| 109 | } |
| 110 | |
Philipp Tomsich | 5c360af | 2017-07-19 22:04:32 +0200 | [diff] [blame] | 111 | const void *blob = gd->fdt_blob; |
| 112 | int chosen_node = fdt_path_offset(blob, "/chosen"); |
| 113 | int idx = 0; |
| 114 | int elem; |
| 115 | int boot_device; |
| 116 | int node; |
| 117 | const char *conf; |
| 118 | |
| 119 | if (chosen_node < 0) { |
| 120 | debug("%s: /chosen not found, using spl_boot_device()\n", |
| 121 | __func__); |
| 122 | spl_boot_list[0] = spl_boot_device(); |
| 123 | return; |
| 124 | } |
| 125 | |
| 126 | for (elem = 0; |
| 127 | (conf = fdt_stringlist_get(blob, chosen_node, |
| 128 | "u-boot,spl-boot-order", elem, NULL)); |
| 129 | elem++) { |
Philipp Tomsich | ec83ff5 | 2017-09-29 19:27:57 +0200 | [diff] [blame] | 130 | const char *alias; |
| 131 | |
| 132 | /* Handle the case of 'same device the SPL was loaded from' */ |
| 133 | if (strncmp(conf, "same-as-spl", 11) == 0) { |
| 134 | conf = board_spl_was_booted_from(); |
| 135 | if (!conf) |
| 136 | continue; |
| 137 | } |
| 138 | |
Philipp Tomsich | 5c360af | 2017-07-19 22:04:32 +0200 | [diff] [blame] | 139 | /* First check if the list element is an alias */ |
Philipp Tomsich | ec83ff5 | 2017-09-29 19:27:57 +0200 | [diff] [blame] | 140 | alias = fdt_get_alias(blob, conf); |
Philipp Tomsich | 5c360af | 2017-07-19 22:04:32 +0200 | [diff] [blame] | 141 | if (alias) |
| 142 | conf = alias; |
| 143 | |
| 144 | /* Try to resolve the config item (or alias) as a path */ |
| 145 | node = fdt_path_offset(blob, conf); |
| 146 | if (node < 0) { |
Jagan Teki | e07a2ee | 2019-09-17 11:40:39 +0530 | [diff] [blame] | 147 | debug("%s: could not find %s in FDT\n", __func__, conf); |
Philipp Tomsich | 5c360af | 2017-07-19 22:04:32 +0200 | [diff] [blame] | 148 | continue; |
| 149 | } |
| 150 | |
| 151 | /* Try to map this back onto SPL boot devices */ |
| 152 | boot_device = spl_node_to_boot_device(node); |
| 153 | if (boot_device < 0) { |
| 154 | debug("%s: could not map node @%x to a boot-device\n", |
| 155 | __func__, node); |
| 156 | continue; |
| 157 | } |
| 158 | |
| 159 | spl_boot_list[idx++] = boot_device; |
| 160 | } |
| 161 | |
| 162 | /* If we had no matches, fall back to spl_boot_device */ |
| 163 | if (idx == 0) |
| 164 | spl_boot_list[0] = spl_boot_device(); |
| 165 | } |
Quentin Schulz | 082652e | 2024-01-18 14:55:57 +0100 | [diff] [blame] | 166 | |
Quentin Schulz | ccedc90 | 2024-01-18 14:55:58 +0100 | [diff] [blame] | 167 | int spl_decode_boot_device(u32 boot_device, char *buf, size_t buflen) |
Quentin Schulz | 082652e | 2024-01-18 14:55:57 +0100 | [diff] [blame] | 168 | { |
Quentin Schulz | ccedc90 | 2024-01-18 14:55:58 +0100 | [diff] [blame] | 169 | struct udevice *dev; |
| 170 | #if CONFIG_IS_ENABLED(BLK) |
| 171 | int dev_num; |
| 172 | #endif |
| 173 | int ret; |
| 174 | |
| 175 | if (boot_device == BOOT_DEVICE_SPI) { |
| 176 | /* Revert spl_node_to_boot_device() logic to find appropriate SPI flash device */ |
| 177 | |
| 178 | /* |
| 179 | * Devices with multiple SPI flash devices will take the first SPI flash found in |
| 180 | * /chosen/u-boot,spl-boot-order. |
| 181 | */ |
| 182 | const void *blob = gd->fdt_blob; |
| 183 | int chosen_node = fdt_path_offset(blob, "/chosen"); |
| 184 | int elem; |
| 185 | int node; |
| 186 | const char *conf; |
| 187 | |
| 188 | if (chosen_node < 0) { |
| 189 | debug("%s: /chosen not found\n", __func__); |
| 190 | return -ENODEV; |
| 191 | } |
| 192 | |
| 193 | for (elem = 0; |
| 194 | (conf = fdt_stringlist_get(blob, chosen_node, |
| 195 | "u-boot,spl-boot-order", elem, NULL)); |
| 196 | elem++) { |
| 197 | const char *alias; |
| 198 | |
| 199 | /* Handle the case of 'same device the SPL was loaded from' */ |
| 200 | if (strncmp(conf, "same-as-spl", 11) == 0) { |
| 201 | conf = board_spl_was_booted_from(); |
| 202 | if (!conf) |
| 203 | continue; |
| 204 | } |
| 205 | |
| 206 | /* First check if the list element is an alias */ |
| 207 | alias = fdt_get_alias(blob, conf); |
| 208 | if (alias) |
| 209 | conf = alias; |
| 210 | |
| 211 | /* Try to resolve the config item (or alias) as a path */ |
| 212 | node = fdt_path_offset(blob, conf); |
| 213 | if (node < 0) { |
| 214 | debug("%s: could not find %s in FDT\n", __func__, conf); |
| 215 | continue; |
| 216 | } |
| 217 | |
| 218 | ret = uclass_find_device_by_of_offset(UCLASS_SPI_FLASH, node, &dev); |
| 219 | if (ret) { |
| 220 | debug("%s: could not find udevice for %s\n", __func__, conf); |
| 221 | continue; |
| 222 | } |
Quentin Schulz | 082652e | 2024-01-18 14:55:57 +0100 | [diff] [blame] | 223 | |
Quentin Schulz | ccedc90 | 2024-01-18 14:55:58 +0100 | [diff] [blame] | 224 | return ofnode_get_path(dev_ofnode(dev), buf, buflen); |
| 225 | } |
| 226 | |
| 227 | return -ENODEV; |
| 228 | } |
Quentin Schulz | 082652e | 2024-01-18 14:55:57 +0100 | [diff] [blame] | 229 | |
Quentin Schulz | ccedc90 | 2024-01-18 14:55:58 +0100 | [diff] [blame] | 230 | #if CONFIG_IS_ENABLED(BLK) |
| 231 | dev_num = (boot_device == BOOT_DEVICE_MMC1) ? 0 : 1; |
Quentin Schulz | 082652e | 2024-01-18 14:55:57 +0100 | [diff] [blame] | 232 | |
Quentin Schulz | ccedc90 | 2024-01-18 14:55:58 +0100 | [diff] [blame] | 233 | ret = blk_find_device(UCLASS_MMC, dev_num, &dev); |
| 234 | if (ret) { |
| 235 | debug("%s: could not find blk device for MMC device %d: %d\n", |
| 236 | __func__, dev_num, ret); |
| 237 | return ret; |
| 238 | } |
| 239 | |
| 240 | dev = dev_get_parent(dev); |
| 241 | return ofnode_get_path(dev_ofnode(dev), buf, buflen); |
| 242 | #else |
| 243 | return -ENODEV; |
| 244 | #endif |
Quentin Schulz | 082652e | 2024-01-18 14:55:57 +0100 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | void spl_perform_fixups(struct spl_image_info *spl_image) |
| 248 | { |
| 249 | void *blob = spl_image_fdt_addr(spl_image); |
Quentin Schulz | ccedc90 | 2024-01-18 14:55:58 +0100 | [diff] [blame] | 250 | char boot_ofpath[512]; |
| 251 | int chosen, ret; |
Quentin Schulz | 082652e | 2024-01-18 14:55:57 +0100 | [diff] [blame] | 252 | |
| 253 | /* |
| 254 | * Inject the ofpath of the device the full U-Boot (or Linux in |
| 255 | * Falcon-mode) was booted from into the FDT, if a FDT has been |
| 256 | * loaded at the same time. |
| 257 | */ |
| 258 | if (!blob) |
| 259 | return; |
| 260 | |
Quentin Schulz | ccedc90 | 2024-01-18 14:55:58 +0100 | [diff] [blame] | 261 | ret = spl_decode_boot_device(spl_image->boot_device, boot_ofpath, sizeof(boot_ofpath)); |
| 262 | if (ret) { |
| 263 | pr_err("%s: could not map boot_device to ofpath: %d\n", __func__, ret); |
Quentin Schulz | 082652e | 2024-01-18 14:55:57 +0100 | [diff] [blame] | 264 | return; |
| 265 | } |
| 266 | |
| 267 | chosen = fdt_find_or_add_subnode(blob, 0, "chosen"); |
| 268 | if (chosen < 0) { |
| 269 | pr_err("%s: could not find/create '/chosen'\n", __func__); |
| 270 | return; |
| 271 | } |
| 272 | fdt_setprop_string(blob, chosen, |
| 273 | "u-boot,spl-boot-device", boot_ofpath); |
| 274 | } |
Philipp Tomsich | 5c360af | 2017-07-19 22:04:32 +0200 | [diff] [blame] | 275 | #endif |