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