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 static 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 #ifndef CONFIG_SPL_MTD_SUPPORT 75 return BOOT_DEVICE_SPI; 76 #else 77 return BOOT_DEVICE_MTD_BLK_SPI_NOR; 78 79 if (!uclass_get_device_by_of_offset(UCLASS_MTD, node, &parent)) { 80 struct udevice *dev; 81 struct blk_desc *desc = NULL; 82 83 for (device_find_first_child(parent, &dev); 84 dev; 85 device_find_next_child(&dev)) { 86 if (device_get_uclass_id(dev) == UCLASS_BLK) { 87 desc = dev_get_uclass_platdata(dev); 88 break; 89 } 90 } 91 92 if (!desc) 93 return -ENOENT; 94 95 switch (desc->devnum) { 96 case 0: 97 return BOOT_DEVICE_MTD_BLK_NAND; 98 case 1: 99 return BOOT_DEVICE_MTD_BLK_SPI_NAND; 100 default: 101 return -ENOSYS; 102 } 103 } 104 #endif 105 106 return -1; 107 } 108 109 /** 110 * board_spl_was_booted_from() - retrieves the of-path the SPL was loaded from 111 * 112 * To support a 'same-as-spl' specification in the search-order for the next 113 * stage, we need a SoC- or board-specific way to handshake with what 'came 114 * before us' (either a BROM or TPL stage) and map the info retrieved onto 115 * a OF path. 116 * 117 * Returns 118 * NULL, on failure or if the device could not be identified 119 * a of_path (a string), on success 120 */ 121 __weak const char *board_spl_was_booted_from(void) 122 { 123 debug("%s: no support for 'same-as-spl' for this board\n", __func__); 124 return NULL; 125 } 126 127 void board_boot_order(u32 *spl_boot_list) 128 { 129 const void *blob = gd->fdt_blob; 130 int chosen_node = fdt_path_offset(blob, "/chosen"); 131 int idx = 0; 132 int elem; 133 int boot_device; 134 int node; 135 const char *conf; 136 137 if (chosen_node < 0) { 138 debug("%s: /chosen not found, using spl_boot_device()\n", 139 __func__); 140 spl_boot_list[0] = spl_boot_device(); 141 return; 142 } 143 144 for (elem = 0; 145 (conf = fdt_stringlist_get(blob, chosen_node, 146 "u-boot,spl-boot-order", elem, NULL)); 147 elem++) { 148 const char *alias; 149 150 /* Handle the case of 'same device the SPL was loaded from' */ 151 if (strncmp(conf, "same-as-spl", 11) == 0) { 152 conf = board_spl_was_booted_from(); 153 if (!conf) 154 continue; 155 } 156 157 /* First check if the list element is an alias */ 158 alias = fdt_get_alias(blob, conf); 159 if (alias) 160 conf = alias; 161 162 /* Try to resolve the config item (or alias) as a path */ 163 node = fdt_path_offset(blob, conf); 164 if (node < 0) { 165 debug("%s: could not find %s in FDT", __func__, conf); 166 continue; 167 } 168 169 /* Try to map this back onto SPL boot devices */ 170 boot_device = spl_node_to_boot_device(node); 171 if (boot_device < 0) { 172 debug("%s: could not map node @%x to a boot-device\n", 173 __func__, node); 174 continue; 175 } 176 177 spl_boot_list[idx++] = boot_device; 178 } 179 180 /* If we had no matches, fall back to spl_boot_device */ 181 if (idx == 0) 182 spl_boot_list[0] = spl_boot_device(); 183 } 184 #endif 185