1 /*
2 * (C) Copyright 2017 Theobroma Systems Design und Consulting GmbH
3 *
4 * SPDX-License-Identifier: GPL-2.0+
5 */
6
7 #include <common.h>
8 #include <dm.h>
9 #include <mmc.h>
10 #include <nand.h>
11 #include <spl.h>
12
13 #ifdef CONFIG_SPL_RAM_DEVICE
board_boot_order(u32 * spl_boot_list)14 void board_boot_order(u32 *spl_boot_list)
15 {
16 spl_boot_list[0] = BOOT_DEVICE_RAM;
17 }
18 #else
19
20 #if CONFIG_IS_ENABLED(OF_CONTROL) && ! CONFIG_IS_ENABLED(OF_PLATDATA)
21 /**
22 * spl_node_to_boot_device() - maps from a DT-node to a SPL boot device
23 * @node: of_offset of the node
24 *
25 * The SPL framework uses BOOT_DEVICE_... constants to identify its boot
26 * sources. These may take on a device-specific meaning, depending on
27 * what nodes are enabled in a DTS (e.g. BOOT_DEVICE_MMC1 may refer to
28 * different controllers/block-devices, depending on which SD/MMC controllers
29 * are enabled in any given DTS). This function maps from a DT-node back
30 * onto a BOOT_DEVICE_... constant, considering the currently active devices.
31 *
32 * Returns
33 * -ENOENT, if no device matching the node could be found
34 * -ENOSYS, if the device matching the node can not be mapped onto a
35 * SPL boot device (e.g. the third MMC device)
36 * -1, for unspecified failures
37 * a positive integer (from the BOOT_DEVICE_... family) on succes.
38 */
spl_node_to_boot_device(int node)39 static int spl_node_to_boot_device(int node)
40 {
41 struct udevice *parent;
42
43 if (!uclass_get_device_by_of_offset(UCLASS_UFS, node, &parent))
44 return BOOT_DEVICE_UFS;
45
46 /*
47 * This should eventually move into the SPL code, once SPL becomes
48 * aware of the block-device layer. Until then (and to avoid unneeded
49 * delays in getting this feature out, it lives at the board-level).
50 */
51 if (!uclass_get_device_by_of_offset(UCLASS_MMC, node, &parent)) {
52 struct udevice *dev;
53 struct blk_desc *desc = NULL;
54
55 for (device_find_first_child(parent, &dev);
56 dev;
57 device_find_next_child(&dev)) {
58 if (device_get_uclass_id(dev) == UCLASS_BLK) {
59 desc = dev_get_uclass_platdata(dev);
60 break;
61 }
62 }
63
64 if (!desc)
65 return -ENOENT;
66
67 switch (desc->devnum) {
68 case 0:
69 return BOOT_DEVICE_MMC1;
70 case 1:
71 return BOOT_DEVICE_MMC2;
72 default:
73 return -ENOSYS;
74 }
75 }
76
77 /*
78 * SPL doesn't differentiate SPI flashes, so we keep the detection
79 * brief and inaccurate... hopefully, the common SPL layer can be
80 * extended with awareness of the BLK layer (and matching OF_CONTROL)
81 * soon.
82 */
83 if (!uclass_get_device_by_of_offset(UCLASS_SPI_FLASH, node, &parent))
84 #ifndef CONFIG_SPL_MTD_SUPPORT
85 return BOOT_DEVICE_SPI;
86 #else
87 return BOOT_DEVICE_MTD_BLK_SPI_NOR;
88
89 if (!uclass_get_device_by_of_offset(UCLASS_MTD, node, &parent)) {
90 struct udevice *dev;
91 struct blk_desc *desc = NULL;
92
93 for (device_find_first_child(parent, &dev);
94 dev;
95 device_find_next_child(&dev)) {
96 if (device_get_uclass_id(dev) == UCLASS_BLK) {
97 desc = dev_get_uclass_platdata(dev);
98 break;
99 }
100 }
101
102 if (!desc)
103 return -ENOENT;
104
105 switch (desc->devnum) {
106 case 0:
107 return BOOT_DEVICE_MTD_BLK_NAND;
108 case 1:
109 return BOOT_DEVICE_MTD_BLK_SPI_NAND;
110 default:
111 return -ENOSYS;
112 }
113 }
114 #endif
115
116 /*
117 * This should eventually move into the SPL code, once SPL becomes
118 * aware of the block-device layer. Until then (and to avoid unneeded
119 * delays in getting this feature out, it lives at the board-level).
120 */
121 if (!uclass_get_device_by_of_offset(UCLASS_RKNAND, node, &parent))
122 return BOOT_DEVICE_RKNAND;
123
124 return -1;
125 }
126
127 /**
128 * board_spl_was_booted_from() - retrieves the of-path the SPL was loaded from
129 *
130 * To support a 'same-as-spl' specification in the search-order for the next
131 * stage, we need a SoC- or board-specific way to handshake with what 'came
132 * before us' (either a BROM or TPL stage) and map the info retrieved onto
133 * a OF path.
134 *
135 * Returns
136 * NULL, on failure or if the device could not be identified
137 * a of_path (a string), on success
138 */
board_spl_was_booted_from(void)139 __weak const char *board_spl_was_booted_from(void)
140 {
141 debug("%s: no support for 'same-as-spl' for this board\n", __func__);
142 return NULL;
143 }
144
board_boot_order(u32 * spl_boot_list)145 void board_boot_order(u32 *spl_boot_list)
146 {
147 const void *blob = gd->fdt_blob;
148 int chosen_node = fdt_path_offset(blob, "/chosen");
149 int idx = 0;
150 int elem;
151 int boot_device;
152 int node;
153 const char *conf;
154
155 if (chosen_node < 0) {
156 debug("%s: /chosen not found, using spl_boot_device()\n",
157 __func__);
158 spl_boot_list[0] = spl_boot_device();
159 return;
160 }
161
162 for (elem = 0;
163 (conf = fdt_stringlist_get(blob, chosen_node,
164 "u-boot,spl-boot-order", elem, NULL));
165 elem++) {
166 const char *alias;
167
168 /* Handle the case of 'same device the SPL was loaded from' */
169 if (strncmp(conf, "same-as-spl", 11) == 0) {
170 conf = board_spl_was_booted_from();
171 if (!conf)
172 continue;
173 }
174
175 /* First check if the list element is an alias */
176 alias = fdt_get_alias(blob, conf);
177 if (alias)
178 conf = alias;
179
180 /* Try to resolve the config item (or alias) as a path */
181 node = fdt_path_offset(blob, conf);
182 if (node < 0) {
183 debug("%s: could not find %s in FDT", __func__, conf);
184 continue;
185 }
186
187 /* Try to map this back onto SPL boot devices */
188 boot_device = spl_node_to_boot_device(node);
189 if (boot_device < 0) {
190 debug("%s: could not map node @%x to a boot-device\n",
191 __func__, node);
192 continue;
193 }
194
195 spl_boot_list[idx++] = boot_device;
196 }
197
198 /* If we had no matches, fall back to spl_boot_device */
199 if (idx == 0)
200 spl_boot_list[0] = spl_boot_device();
201 }
202 #endif
203 #endif
204