blob: 0bb9a73a8abcb383054d807c200b2f13cc6f8dfe [file] [log] [blame]
Philipp Tomsich5c360af2017-07-19 22:04:32 +02001/*
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 <spl.h>
11
12#if CONFIG_IS_ENABLED(OF_CONTROL)
Philipp Tomsich1c4f63a2017-09-29 19:27:56 +020013/**
14 * spl_node_to_boot_device() - maps from a DT-node to a SPL boot device
15 * @node: of_offset of the node
16 *
17 * The SPL framework uses BOOT_DEVICE_... constants to identify its boot
18 * sources. These may take on a device-specific meaning, depending on
19 * what nodes are enabled in a DTS (e.g. BOOT_DEVICE_MMC1 may refer to
20 * different controllers/block-devices, depending on which SD/MMC controllers
21 * are enabled in any given DTS). This function maps from a DT-node back
22 * onto a BOOT_DEVICE_... constant, considering the currently active devices.
23 *
24 * Returns
25 * -ENOENT, if no device matching the node could be found
26 * -ENOSYS, if the device matching the node can not be mapped onto a
27 * SPL boot device (e.g. the third MMC device)
28 * -1, for unspecified failures
29 * a positive integer (from the BOOT_DEVICE_... family) on succes.
30 */
31
Philipp Tomsich5c360af2017-07-19 22:04:32 +020032static int spl_node_to_boot_device(int node)
33{
34 struct udevice *parent;
35
36 /*
37 * This should eventually move into the SPL code, once SPL becomes
38 * aware of the block-device layer. Until then (and to avoid unneeded
39 * delays in getting this feature out, it lives at the board-level).
40 */
41 if (!uclass_get_device_by_of_offset(UCLASS_MMC, node, &parent)) {
42 struct udevice *dev;
43 struct blk_desc *desc = NULL;
44
45 for (device_find_first_child(parent, &dev);
46 dev;
47 device_find_next_child(&dev)) {
48 if (device_get_uclass_id(dev) == UCLASS_BLK) {
49 desc = dev_get_uclass_platdata(dev);
50 break;
51 }
52 }
53
54 if (!desc)
55 return -ENOENT;
56
57 switch (desc->devnum) {
58 case 0:
59 return BOOT_DEVICE_MMC1;
60 case 1:
61 return BOOT_DEVICE_MMC2;
62 default:
63 return -ENOSYS;
64 }
65 }
66
67 /*
68 * SPL doesn't differentiate SPI flashes, so we keep the detection
69 * brief and inaccurate... hopefully, the common SPL layer can be
70 * extended with awareness of the BLK layer (and matching OF_CONTROL)
71 * soon.
72 */
73 if (!uclass_get_device_by_of_offset(UCLASS_SPI_FLASH, node, &parent))
74 return BOOT_DEVICE_SPI;
75
76 return -1;
77}
78
79void board_boot_order(u32 *spl_boot_list)
80{
81 const void *blob = gd->fdt_blob;
82 int chosen_node = fdt_path_offset(blob, "/chosen");
83 int idx = 0;
84 int elem;
85 int boot_device;
86 int node;
87 const char *conf;
88
89 if (chosen_node < 0) {
90 debug("%s: /chosen not found, using spl_boot_device()\n",
91 __func__);
92 spl_boot_list[0] = spl_boot_device();
93 return;
94 }
95
96 for (elem = 0;
97 (conf = fdt_stringlist_get(blob, chosen_node,
98 "u-boot,spl-boot-order", elem, NULL));
99 elem++) {
100 /* First check if the list element is an alias */
101 const char *alias = fdt_get_alias(blob, conf);
102 if (alias)
103 conf = alias;
104
105 /* Try to resolve the config item (or alias) as a path */
106 node = fdt_path_offset(blob, conf);
107 if (node < 0) {
108 debug("%s: could not find %s in FDT", __func__, conf);
109 continue;
110 }
111
112 /* Try to map this back onto SPL boot devices */
113 boot_device = spl_node_to_boot_device(node);
114 if (boot_device < 0) {
115 debug("%s: could not map node @%x to a boot-device\n",
116 __func__, node);
117 continue;
118 }
119
120 spl_boot_list[idx++] = boot_device;
121 }
122
123 /* If we had no matches, fall back to spl_boot_device */
124 if (idx == 0)
125 spl_boot_list[0] = spl_boot_device();
126}
127#endif