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