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