blob: c2e40230072f68458bcd371f4c9326e98463a02e [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Philipp Tomsich5c360af2017-07-19 22:04:32 +02002/*
3 * (C) Copyright 2017 Theobroma Systems Design und Consulting GmbH
Philipp Tomsich5c360af2017-07-19 22:04:32 +02004 */
5
6#include <common.h>
7#include <dm.h>
Simon Glass0f2af882020-05-10 11:40:05 -06008#include <log.h>
Philipp Tomsich5c360af2017-07-19 22:04:32 +02009#include <mmc.h>
10#include <spl.h>
11
Kever Yangfec76f52019-07-17 18:12:20 +080012#if CONFIG_IS_ENABLED(OF_LIBFDT)
Philipp Tomsich1c4f63a2017-09-29 19:27:56 +020013/**
14 * spl_node_to_boot_device() - maps from a DT-node to a SPL boot device
15 * @node: of_offset of the node
16 *
17 * The SPL framework uses BOOT_DEVICE_... constants to identify its boot
18 * sources. These may take on a device-specific meaning, depending on
19 * what nodes are enabled in a DTS (e.g. BOOT_DEVICE_MMC1 may refer to
20 * different controllers/block-devices, depending on which SD/MMC controllers
21 * are enabled in any given DTS). This function maps from a DT-node back
22 * onto a BOOT_DEVICE_... constant, considering the currently active devices.
23 *
24 * Returns
25 * -ENOENT, if no device matching the node could be found
26 * -ENOSYS, if the device matching the node can not be mapped onto a
27 * SPL boot device (e.g. the third MMC device)
28 * -1, for unspecified failures
29 * a positive integer (from the BOOT_DEVICE_... family) on succes.
30 */
31
Philipp Tomsich5c360af2017-07-19 22:04:32 +020032static int spl_node_to_boot_device(int node)
33{
34 struct udevice *parent;
35
36 /*
37 * This should eventually move into the SPL code, once SPL becomes
38 * aware of the block-device layer. Until then (and to avoid unneeded
Thomas Hebbfd37f242019-11-13 18:18:03 -080039 * delays in getting this feature out), it lives at the board-level.
Philipp Tomsich5c360af2017-07-19 22:04:32 +020040 */
41 if (!uclass_get_device_by_of_offset(UCLASS_MMC, node, &parent)) {
42 struct udevice *dev;
43 struct blk_desc *desc = NULL;
44
45 for (device_find_first_child(parent, &dev);
46 dev;
47 device_find_next_child(&dev)) {
48 if (device_get_uclass_id(dev) == UCLASS_BLK) {
49 desc = dev_get_uclass_platdata(dev);
50 break;
51 }
52 }
53
54 if (!desc)
55 return -ENOENT;
56
57 switch (desc->devnum) {
58 case 0:
59 return BOOT_DEVICE_MMC1;
60 case 1:
61 return BOOT_DEVICE_MMC2;
62 default:
63 return -ENOSYS;
64 }
Simon Glassa1766a52019-01-21 14:53:28 -070065 } else if (!uclass_get_device_by_of_offset(UCLASS_SPI_FLASH, node,
66 &parent)) {
67 return BOOT_DEVICE_SPI;
Philipp Tomsich5c360af2017-07-19 22:04:32 +020068 }
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
Philipp Tomsichec83ff52017-09-29 19:27:57 +020082/**
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
Philipp Tomsich5c360af2017-07-19 22:04:32 +0200100void board_boot_order(u32 *spl_boot_list)
101{
102 const void *blob = gd->fdt_blob;
103 int chosen_node = fdt_path_offset(blob, "/chosen");
104 int idx = 0;
105 int elem;
106 int boot_device;
107 int node;
108 const char *conf;
109
110 if (chosen_node < 0) {
111 debug("%s: /chosen not found, using spl_boot_device()\n",
112 __func__);
113 spl_boot_list[0] = spl_boot_device();
114 return;
115 }
116
117 for (elem = 0;
118 (conf = fdt_stringlist_get(blob, chosen_node,
119 "u-boot,spl-boot-order", elem, NULL));
120 elem++) {
Philipp Tomsichec83ff52017-09-29 19:27:57 +0200121 const char *alias;
122
123 /* Handle the case of 'same device the SPL was loaded from' */
124 if (strncmp(conf, "same-as-spl", 11) == 0) {
125 conf = board_spl_was_booted_from();
126 if (!conf)
127 continue;
128 }
129
Philipp Tomsich5c360af2017-07-19 22:04:32 +0200130 /* First check if the list element is an alias */
Philipp Tomsichec83ff52017-09-29 19:27:57 +0200131 alias = fdt_get_alias(blob, conf);
Philipp Tomsich5c360af2017-07-19 22:04:32 +0200132 if (alias)
133 conf = alias;
134
135 /* Try to resolve the config item (or alias) as a path */
136 node = fdt_path_offset(blob, conf);
137 if (node < 0) {
Jagan Tekie07a2ee2019-09-17 11:40:39 +0530138 debug("%s: could not find %s in FDT\n", __func__, conf);
Philipp Tomsich5c360af2017-07-19 22:04:32 +0200139 continue;
140 }
141
142 /* Try to map this back onto SPL boot devices */
143 boot_device = spl_node_to_boot_device(node);
144 if (boot_device < 0) {
145 debug("%s: could not map node @%x to a boot-device\n",
146 __func__, node);
147 continue;
148 }
149
150 spl_boot_list[idx++] = boot_device;
151 }
152
153 /* If we had no matches, fall back to spl_boot_device */
154 if (idx == 0)
155 spl_boot_list[0] = spl_boot_device();
156}
157#endif