blob: 0cd3da20c9574dd5ffb6d3802162c2208083213a [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Philipp Tomsich9b0bc592017-07-19 22:04:32 +02002/*
3 * (C) Copyright 2017 Theobroma Systems Design und Consulting GmbH
Philipp Tomsich9b0bc592017-07-19 22:04:32 +02004 */
5
6#include <common.h>
7#include <dm.h>
Quentin Schulz948b3152024-01-18 14:55:57 +01008#include <fdt_support.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -06009#include <log.h>
Philipp Tomsich9b0bc592017-07-19 22:04:32 +020010#include <mmc.h>
11#include <spl.h>
Simon Glass401d1c42020-10-30 21:38:53 -060012#include <asm/global_data.h>
Philipp Tomsich9b0bc592017-07-19 22:04:32 +020013
Kever Yang032906a2019-07-17 18:12:20 +080014#if CONFIG_IS_ENABLED(OF_LIBFDT)
Philipp Tomsichdbad01c2017-09-29 19:27:56 +020015/**
16 * spl_node_to_boot_device() - maps from a DT-node to a SPL boot device
17 * @node: of_offset of the node
18 *
19 * The SPL framework uses BOOT_DEVICE_... constants to identify its boot
20 * sources. These may take on a device-specific meaning, depending on
21 * what nodes are enabled in a DTS (e.g. BOOT_DEVICE_MMC1 may refer to
22 * different controllers/block-devices, depending on which SD/MMC controllers
23 * are enabled in any given DTS). This function maps from a DT-node back
24 * onto a BOOT_DEVICE_... constant, considering the currently active devices.
25 *
26 * Returns
27 * -ENOENT, if no device matching the node could be found
28 * -ENOSYS, if the device matching the node can not be mapped onto a
29 * SPL boot device (e.g. the third MMC device)
30 * -1, for unspecified failures
31 * a positive integer (from the BOOT_DEVICE_... family) on succes.
32 */
33
Philipp Tomsich9b0bc592017-07-19 22:04:32 +020034static int spl_node_to_boot_device(int node)
35{
36 struct udevice *parent;
37
38 /*
39 * This should eventually move into the SPL code, once SPL becomes
40 * aware of the block-device layer. Until then (and to avoid unneeded
Thomas Hebb32f2ca22019-11-13 18:18:03 -080041 * delays in getting this feature out), it lives at the board-level.
Philipp Tomsich9b0bc592017-07-19 22:04:32 +020042 */
43 if (!uclass_get_device_by_of_offset(UCLASS_MMC, node, &parent)) {
44 struct udevice *dev;
45 struct blk_desc *desc = NULL;
46
47 for (device_find_first_child(parent, &dev);
48 dev;
49 device_find_next_child(&dev)) {
50 if (device_get_uclass_id(dev) == UCLASS_BLK) {
Simon Glasscaa4daa2020-12-03 16:55:18 -070051 desc = dev_get_uclass_plat(dev);
Philipp Tomsich9b0bc592017-07-19 22:04:32 +020052 break;
53 }
54 }
55
56 if (!desc)
57 return -ENOENT;
58
59 switch (desc->devnum) {
60 case 0:
61 return BOOT_DEVICE_MMC1;
62 case 1:
63 return BOOT_DEVICE_MMC2;
64 default:
65 return -ENOSYS;
66 }
Simon Glass3523c072019-01-21 14:53:28 -070067 } else if (!uclass_get_device_by_of_offset(UCLASS_SPI_FLASH, node,
68 &parent)) {
69 return BOOT_DEVICE_SPI;
Philipp Tomsich9b0bc592017-07-19 22:04:32 +020070 }
71
72 /*
73 * SPL doesn't differentiate SPI flashes, so we keep the detection
74 * brief and inaccurate... hopefully, the common SPL layer can be
75 * extended with awareness of the BLK layer (and matching OF_CONTROL)
76 * soon.
77 */
78 if (!uclass_get_device_by_of_offset(UCLASS_SPI_FLASH, node, &parent))
79 return BOOT_DEVICE_SPI;
80
81 return -1;
82}
83
Philipp Tomsich80e9f882017-09-29 19:27:57 +020084/**
85 * board_spl_was_booted_from() - retrieves the of-path the SPL was loaded from
86 *
87 * To support a 'same-as-spl' specification in the search-order for the next
88 * stage, we need a SoC- or board-specific way to handshake with what 'came
89 * before us' (either a BROM or TPL stage) and map the info retrieved onto
90 * a OF path.
91 *
92 * Returns
93 * NULL, on failure or if the device could not be identified
94 * a of_path (a string), on success
95 */
96__weak const char *board_spl_was_booted_from(void)
97{
98 debug("%s: no support for 'same-as-spl' for this board\n", __func__);
99 return NULL;
100}
101
Philipp Tomsich9b0bc592017-07-19 22:04:32 +0200102void board_boot_order(u32 *spl_boot_list)
103{
Simon Glasscaa4daa2020-12-03 16:55:18 -0700104 /* In case of no fdt (or only plat), use spl_boot_device() */
Urja Rannikkoe68a8432020-05-13 19:15:22 +0000105 if (!CONFIG_IS_ENABLED(OF_CONTROL) || CONFIG_IS_ENABLED(OF_PLATDATA)) {
106 spl_boot_list[0] = spl_boot_device();
107 return;
108 }
109
Philipp Tomsich9b0bc592017-07-19 22:04:32 +0200110 const void *blob = gd->fdt_blob;
111 int chosen_node = fdt_path_offset(blob, "/chosen");
112 int idx = 0;
113 int elem;
114 int boot_device;
115 int node;
116 const char *conf;
117
118 if (chosen_node < 0) {
119 debug("%s: /chosen not found, using spl_boot_device()\n",
120 __func__);
121 spl_boot_list[0] = spl_boot_device();
122 return;
123 }
124
125 for (elem = 0;
126 (conf = fdt_stringlist_get(blob, chosen_node,
127 "u-boot,spl-boot-order", elem, NULL));
128 elem++) {
Philipp Tomsich80e9f882017-09-29 19:27:57 +0200129 const char *alias;
130
131 /* Handle the case of 'same device the SPL was loaded from' */
132 if (strncmp(conf, "same-as-spl", 11) == 0) {
133 conf = board_spl_was_booted_from();
134 if (!conf)
135 continue;
136 }
137
Philipp Tomsich9b0bc592017-07-19 22:04:32 +0200138 /* First check if the list element is an alias */
Philipp Tomsich80e9f882017-09-29 19:27:57 +0200139 alias = fdt_get_alias(blob, conf);
Philipp Tomsich9b0bc592017-07-19 22:04:32 +0200140 if (alias)
141 conf = alias;
142
143 /* Try to resolve the config item (or alias) as a path */
144 node = fdt_path_offset(blob, conf);
145 if (node < 0) {
Jagan Teki891d4d12019-09-17 11:40:39 +0530146 debug("%s: could not find %s in FDT\n", __func__, conf);
Philipp Tomsich9b0bc592017-07-19 22:04:32 +0200147 continue;
148 }
149
150 /* Try to map this back onto SPL boot devices */
151 boot_device = spl_node_to_boot_device(node);
152 if (boot_device < 0) {
153 debug("%s: could not map node @%x to a boot-device\n",
154 __func__, node);
155 continue;
156 }
157
158 spl_boot_list[idx++] = boot_device;
159 }
160
161 /* If we had no matches, fall back to spl_boot_device */
162 if (idx == 0)
163 spl_boot_list[0] = spl_boot_device();
164}
Quentin Schulz948b3152024-01-18 14:55:57 +0100165
166__weak const char * const spl_boot_devices[BOOT_DEVICE_NONE + 1] = {};
167
168const char *spl_decode_boot_device(u32 boot_device)
169{
170 const char *spl_bootdevice_ofpath = NULL;
171
172 if (boot_device < ARRAY_SIZE(spl_boot_devices))
173 spl_bootdevice_ofpath = spl_boot_devices[boot_device];
174
175 if (spl_bootdevice_ofpath)
176 debug("%s: spl_bootdevice_id %x maps to '%s'\n",
177 __func__, boot_device, spl_bootdevice_ofpath);
178 else
179 debug("%s: failed to resolve spl_bootdevice_id %x\n",
180 __func__, boot_device);
181
182 return spl_bootdevice_ofpath;
183}
184
185void spl_perform_fixups(struct spl_image_info *spl_image)
186{
187 void *blob = spl_image_fdt_addr(spl_image);
188 const char *boot_ofpath;
189 int chosen;
190
191 /*
192 * Inject the ofpath of the device the full U-Boot (or Linux in
193 * Falcon-mode) was booted from into the FDT, if a FDT has been
194 * loaded at the same time.
195 */
196 if (!blob)
197 return;
198
199 boot_ofpath = spl_decode_boot_device(spl_image->boot_device);
200 if (!boot_ofpath) {
201 pr_err("%s: could not map boot_device to ofpath\n", __func__);
202 return;
203 }
204
205 chosen = fdt_find_or_add_subnode(blob, 0, "chosen");
206 if (chosen < 0) {
207 pr_err("%s: could not find/create '/chosen'\n", __func__);
208 return;
209 }
210 fdt_setprop_string(blob, chosen,
211 "u-boot,spl-boot-device", boot_ofpath);
212}
Philipp Tomsich9b0bc592017-07-19 22:04:32 +0200213#endif