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 /*
44 * This should eventually move into the SPL code, once SPL becomes
45 * aware of the block-device layer. Until then (and to avoid unneeded
46 * delays in getting this feature out, it lives at the board-level).
47 */
48 if (!uclass_get_device_by_of_offset(UCLASS_MMC, node, &parent)) {
49 struct udevice *dev;
50 struct blk_desc *desc = NULL;
51
52 for (device_find_first_child(parent, &dev);
53 dev;
54 device_find_next_child(&dev)) {
55 if (device_get_uclass_id(dev) == UCLASS_BLK) {
56 desc = dev_get_uclass_platdata(dev);
57 break;
58 }
59 }
60
61 if (!desc)
62 return -ENOENT;
63
64 switch (desc->devnum) {
65 case 0:
66 return BOOT_DEVICE_MMC1;
67 case 1:
68 return BOOT_DEVICE_MMC2;
69 default:
70 return -ENOSYS;
71 }
72 }
73
74 /*
75 * SPL doesn't differentiate SPI flashes, so we keep the detection
76 * brief and inaccurate... hopefully, the common SPL layer can be
77 * extended with awareness of the BLK layer (and matching OF_CONTROL)
78 * soon.
79 */
80 if (!uclass_get_device_by_of_offset(UCLASS_SPI_FLASH, node, &parent))
81 #ifndef CONFIG_SPL_MTD_SUPPORT
82 return BOOT_DEVICE_SPI;
83 #else
84 return BOOT_DEVICE_MTD_BLK_SPI_NOR;
85
86 if (!uclass_get_device_by_of_offset(UCLASS_MTD, node, &parent)) {
87 struct udevice *dev;
88 struct blk_desc *desc = NULL;
89
90 for (device_find_first_child(parent, &dev);
91 dev;
92 device_find_next_child(&dev)) {
93 if (device_get_uclass_id(dev) == UCLASS_BLK) {
94 desc = dev_get_uclass_platdata(dev);
95 break;
96 }
97 }
98
99 if (!desc)
100 return -ENOENT;
101
102 switch (desc->devnum) {
103 case 0:
104 return BOOT_DEVICE_MTD_BLK_NAND;
105 case 1:
106 return BOOT_DEVICE_MTD_BLK_SPI_NAND;
107 default:
108 return -ENOSYS;
109 }
110 }
111 #endif
112
113 /*
114 * This should eventually move into the SPL code, once SPL becomes
115 * aware of the block-device layer. Until then (and to avoid unneeded
116 * delays in getting this feature out, it lives at the board-level).
117 */
118 if (!uclass_get_device_by_of_offset(UCLASS_RKNAND, node, &parent))
119 return BOOT_DEVICE_RKNAND;
120
121 return -1;
122 }
123
124 /**
125 * board_spl_was_booted_from() - retrieves the of-path the SPL was loaded from
126 *
127 * To support a 'same-as-spl' specification in the search-order for the next
128 * stage, we need a SoC- or board-specific way to handshake with what 'came
129 * before us' (either a BROM or TPL stage) and map the info retrieved onto
130 * a OF path.
131 *
132 * Returns
133 * NULL, on failure or if the device could not be identified
134 * a of_path (a string), on success
135 */
board_spl_was_booted_from(void)136 __weak const char *board_spl_was_booted_from(void)
137 {
138 debug("%s: no support for 'same-as-spl' for this board\n", __func__);
139 return NULL;
140 }
141
board_boot_order(u32 * spl_boot_list)142 void board_boot_order(u32 *spl_boot_list)
143 {
144 const void *blob = gd->fdt_blob;
145 int chosen_node = fdt_path_offset(blob, "/chosen");
146 int idx = 0;
147 int elem;
148 int boot_device;
149 int node;
150 const char *conf;
151
152 if (chosen_node < 0) {
153 debug("%s: /chosen not found, using spl_boot_device()\n",
154 __func__);
155 spl_boot_list[0] = spl_boot_device();
156 return;
157 }
158
159 for (elem = 0;
160 (conf = fdt_stringlist_get(blob, chosen_node,
161 "u-boot,spl-boot-order", elem, NULL));
162 elem++) {
163 const char *alias;
164
165 /* Handle the case of 'same device the SPL was loaded from' */
166 if (strncmp(conf, "same-as-spl", 11) == 0) {
167 conf = board_spl_was_booted_from();
168 if (!conf)
169 continue;
170 }
171
172 /* First check if the list element is an alias */
173 alias = fdt_get_alias(blob, conf);
174 if (alias)
175 conf = alias;
176
177 /* Try to resolve the config item (or alias) as a path */
178 node = fdt_path_offset(blob, conf);
179 if (node < 0) {
180 debug("%s: could not find %s in FDT", __func__, conf);
181 continue;
182 }
183
184 /* Try to map this back onto SPL boot devices */
185 boot_device = spl_node_to_boot_device(node);
186 if (boot_device < 0) {
187 debug("%s: could not map node @%x to a boot-device\n",
188 __func__, node);
189 continue;
190 }
191
192 spl_boot_list[idx++] = boot_device;
193 }
194
195 /* If we had no matches, fall back to spl_boot_device */
196 if (idx == 0)
197 spl_boot_list[0] = spl_boot_device();
198 }
199 #endif
200 #endif
201