spl-boot-order.c 4.4 KB

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