]> git.ipfire.org Git - thirdparty/u-boot.git/blame - arch/arm/mach-rockchip/spl-boot-order.c
Revert "Merge patch series "arm: dts: am62-beagleplay: Fix Beagleplay Ethernet""
[thirdparty/u-boot.git] / arch / arm / mach-rockchip / spl-boot-order.c
CommitLineData
83d290c5 1// SPDX-License-Identifier: GPL-2.0+
9b0bc593
PT
2/*
3 * (C) Copyright 2017 Theobroma Systems Design und Consulting GmbH
9b0bc593
PT
4 */
5
d678a59d 6#include <common.h>
9b0bc593 7#include <dm.h>
948b315e 8#include <fdt_support.h>
f7ae49fc 9#include <log.h>
9b0bc593
PT
10#include <mmc.h>
11#include <spl.h>
401d1c4f 12#include <asm/global_data.h>
d57e16c7 13#include <dm/uclass-internal.h>
9b0bc593 14
032906a8 15#if CONFIG_IS_ENABLED(OF_LIBFDT)
dbad01ca
PT
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
6f29ce00 32 * a positive integer (from the BOOT_DEVICE_... family) on success.
dbad01ca
PT
33 */
34
9b0bc593
PT
35static 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
32f2ca2a 42 * delays in getting this feature out), it lives at the board-level.
9b0bc593
PT
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) {
caa4daa2 52 desc = dev_get_uclass_plat(dev);
9b0bc593
PT
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 }
68 }
69
70 /*
71 * SPL doesn't differentiate SPI flashes, so we keep the detection
72 * brief and inaccurate... hopefully, the common SPL layer can be
73 * extended with awareness of the BLK layer (and matching OF_CONTROL)
74 * soon.
75 */
76 if (!uclass_get_device_by_of_offset(UCLASS_SPI_FLASH, node, &parent))
77 return BOOT_DEVICE_SPI;
78
79 return -1;
80}
81
80e9f88e
PT
82/**
83 * board_spl_was_booted_from() - retrieves the of-path the SPL was loaded from
84 *
85 * To support a 'same-as-spl' specification in the search-order for the next
86 * stage, we need a SoC- or board-specific way to handshake with what 'came
87 * before us' (either a BROM or TPL stage) and map the info retrieved onto
88 * a OF path.
89 *
90 * Returns
91 * NULL, on failure or if the device could not be identified
92 * a of_path (a string), on success
93 */
94__weak const char *board_spl_was_booted_from(void)
95{
96 debug("%s: no support for 'same-as-spl' for this board\n", __func__);
97 return NULL;
98}
99
9b0bc593
PT
100void board_boot_order(u32 *spl_boot_list)
101{
caa4daa2 102 /* In case of no fdt (or only plat), use spl_boot_device() */
e68a8436
UR
103 if (!CONFIG_IS_ENABLED(OF_CONTROL) || CONFIG_IS_ENABLED(OF_PLATDATA)) {
104 spl_boot_list[0] = spl_boot_device();
105 return;
106 }
107
9b0bc593
PT
108 const void *blob = gd->fdt_blob;
109 int chosen_node = fdt_path_offset(blob, "/chosen");
110 int idx = 0;
111 int elem;
112 int boot_device;
113 int node;
114 const char *conf;
115
116 if (chosen_node < 0) {
117 debug("%s: /chosen not found, using spl_boot_device()\n",
118 __func__);
119 spl_boot_list[0] = spl_boot_device();
120 return;
121 }
122
123 for (elem = 0;
124 (conf = fdt_stringlist_get(blob, chosen_node,
125 "u-boot,spl-boot-order", elem, NULL));
126 elem++) {
80e9f88e
PT
127 const char *alias;
128
129 /* Handle the case of 'same device the SPL was loaded from' */
130 if (strncmp(conf, "same-as-spl", 11) == 0) {
131 conf = board_spl_was_booted_from();
132 if (!conf)
133 continue;
134 }
135
9b0bc593 136 /* First check if the list element is an alias */
80e9f88e 137 alias = fdt_get_alias(blob, conf);
9b0bc593
PT
138 if (alias)
139 conf = alias;
140
141 /* Try to resolve the config item (or alias) as a path */
142 node = fdt_path_offset(blob, conf);
143 if (node < 0) {
891d4d1f 144 debug("%s: could not find %s in FDT\n", __func__, conf);
9b0bc593
PT
145 continue;
146 }
147
148 /* Try to map this back onto SPL boot devices */
149 boot_device = spl_node_to_boot_device(node);
150 if (boot_device < 0) {
50ac349d
CO
151 debug("%s: could not map node %s to a boot-device\n",
152 __func__, conf);
9b0bc593
PT
153 continue;
154 }
155
156 spl_boot_list[idx++] = boot_device;
157 }
158
159 /* If we had no matches, fall back to spl_boot_device */
160 if (idx == 0)
161 spl_boot_list[0] = spl_boot_device();
162}
948b315e 163
d57e16c7 164int spl_decode_boot_device(u32 boot_device, char *buf, size_t buflen)
948b315e 165{
d57e16c7
QS
166 struct udevice *dev;
167#if CONFIG_IS_ENABLED(BLK)
168 int dev_num;
169#endif
170 int ret;
171
172 if (boot_device == BOOT_DEVICE_SPI) {
173 /* Revert spl_node_to_boot_device() logic to find appropriate SPI flash device */
174
175 /*
176 * Devices with multiple SPI flash devices will take the first SPI flash found in
177 * /chosen/u-boot,spl-boot-order.
178 */
179 const void *blob = gd->fdt_blob;
180 int chosen_node = fdt_path_offset(blob, "/chosen");
181 int elem;
182 int node;
183 const char *conf;
184
185 if (chosen_node < 0) {
186 debug("%s: /chosen not found\n", __func__);
187 return -ENODEV;
188 }
189
190 for (elem = 0;
191 (conf = fdt_stringlist_get(blob, chosen_node,
192 "u-boot,spl-boot-order", elem, NULL));
193 elem++) {
194 const char *alias;
195
196 /* Handle the case of 'same device the SPL was loaded from' */
197 if (strncmp(conf, "same-as-spl", 11) == 0) {
198 conf = board_spl_was_booted_from();
199 if (!conf)
200 continue;
201 }
202
203 /* First check if the list element is an alias */
204 alias = fdt_get_alias(blob, conf);
205 if (alias)
206 conf = alias;
207
208 /* Try to resolve the config item (or alias) as a path */
209 node = fdt_path_offset(blob, conf);
210 if (node < 0) {
211 debug("%s: could not find %s in FDT\n", __func__, conf);
212 continue;
213 }
948b315e 214
d57e16c7
QS
215 ret = uclass_find_device_by_of_offset(UCLASS_SPI_FLASH, node, &dev);
216 if (ret) {
217 debug("%s: could not find udevice for %s\n", __func__, conf);
218 continue;
219 }
948b315e 220
d57e16c7
QS
221 return ofnode_get_path(dev_ofnode(dev), buf, buflen);
222 }
948b315e 223
d57e16c7
QS
224 return -ENODEV;
225 }
226
227#if CONFIG_IS_ENABLED(BLK)
228 dev_num = (boot_device == BOOT_DEVICE_MMC1) ? 0 : 1;
229
230 ret = blk_find_device(UCLASS_MMC, dev_num, &dev);
231 if (ret) {
232 debug("%s: could not find blk device for MMC device %d: %d\n",
233 __func__, dev_num, ret);
234 return ret;
235 }
236
237 dev = dev_get_parent(dev);
238 return ofnode_get_path(dev_ofnode(dev), buf, buflen);
239#else
240 return -ENODEV;
241#endif
948b315e
QS
242}
243
244void spl_perform_fixups(struct spl_image_info *spl_image)
245{
246 void *blob = spl_image_fdt_addr(spl_image);
d57e16c7
QS
247 char boot_ofpath[512];
248 int chosen, ret;
948b315e
QS
249
250 /*
251 * Inject the ofpath of the device the full U-Boot (or Linux in
252 * Falcon-mode) was booted from into the FDT, if a FDT has been
253 * loaded at the same time.
254 */
255 if (!blob)
256 return;
257
d57e16c7
QS
258 ret = spl_decode_boot_device(spl_image->boot_device, boot_ofpath, sizeof(boot_ofpath));
259 if (ret) {
260 pr_err("%s: could not map boot_device to ofpath: %d\n", __func__, ret);
948b315e
QS
261 return;
262 }
263
264 chosen = fdt_find_or_add_subnode(blob, 0, "chosen");
265 if (chosen < 0) {
266 pr_err("%s: could not find/create '/chosen'\n", __func__);
267 return;
268 }
269 fdt_setprop_string(blob, chosen,
270 "u-boot,spl-boot-device", boot_ofpath);
271}
9b0bc593 272#endif