sysfw-loader.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * K3: System Firmware Loader
  4. *
  5. * Copyright (C) 2019 Texas Instruments Incorporated - http://www.ti.com/
  6. * Andreas Dannenberg <dannenberg@ti.com>
  7. */
  8. #include <common.h>
  9. #include <spl.h>
  10. #include <malloc.h>
  11. #include <remoteproc.h>
  12. #include <linux/soc/ti/ti_sci_protocol.h>
  13. #include <g_dnl.h>
  14. #include <usb.h>
  15. #include <dfu.h>
  16. #include <dm/uclass-internal.h>
  17. #include <spi_flash.h>
  18. #include <asm/arch/sys_proto.h>
  19. #include "common.h"
  20. DECLARE_GLOBAL_DATA_PTR;
  21. /* Name of the FIT image nodes for SYSFW and its config data */
  22. #define SYSFW_FIRMWARE "sysfw.bin"
  23. #define SYSFW_CFG_BOARD "board-cfg.bin"
  24. #define SYSFW_CFG_PM "pm-cfg.bin"
  25. #define SYSFW_CFG_RM "rm-cfg.bin"
  26. #define SYSFW_CFG_SEC "sec-cfg.bin"
  27. static bool sysfw_loaded;
  28. static void *sysfw_load_address;
  29. /*
  30. * Populate SPL hook to override the default load address used by the SPL
  31. * loader function with a custom address for SYSFW loading.
  32. */
  33. struct image_header *spl_get_load_buffer(ssize_t offset, size_t size)
  34. {
  35. if (sysfw_loaded)
  36. return (struct image_header *)(CONFIG_SYS_TEXT_BASE + offset);
  37. else if (sysfw_load_address)
  38. return sysfw_load_address;
  39. else
  40. panic("SYSFW load address not defined!");
  41. }
  42. /*
  43. * Populate SPL hook to skip the default SPL loader FIT post-processing steps
  44. * during SYSFW loading and return to the calling function so we can perform
  45. * our own custom processing.
  46. */
  47. bool spl_load_simple_fit_skip_processing(void)
  48. {
  49. return !sysfw_loaded;
  50. }
  51. static int fit_get_data_by_name(const void *fit, int images, const char *name,
  52. const void **addr, size_t *size)
  53. {
  54. int node_offset;
  55. node_offset = fdt_subnode_offset(fit, images, name);
  56. if (node_offset < 0)
  57. return -ENOENT;
  58. return fit_image_get_data(fit, node_offset, addr, size);
  59. }
  60. static void k3_sysfw_load_using_fit(void *fit)
  61. {
  62. int images;
  63. const void *sysfw_addr;
  64. size_t sysfw_size;
  65. int ret;
  66. /* Find the node holding the images information */
  67. images = fdt_path_offset(fit, FIT_IMAGES_PATH);
  68. if (images < 0)
  69. panic("Cannot find /images node (%d)\n", images);
  70. /* Extract System Firmware (SYSFW) image from FIT */
  71. ret = fit_get_data_by_name(fit, images, SYSFW_FIRMWARE,
  72. &sysfw_addr, &sysfw_size);
  73. if (ret < 0)
  74. panic("Error accessing %s node in FIT (%d)\n", SYSFW_FIRMWARE,
  75. ret);
  76. /*
  77. * Start up system controller firmware
  78. *
  79. * It is assumed that remoteproc device 0 is the corresponding
  80. * system-controller that runs SYSFW. Make sure DT reflects the same.
  81. */
  82. ret = rproc_dev_init(0);
  83. if (ret)
  84. panic("rproc failed to be initialized (%d)\n", ret);
  85. ret = rproc_load(0, (ulong)sysfw_addr, (ulong)sysfw_size);
  86. if (ret)
  87. panic("Firmware failed to start on rproc (%d)\n", ret);
  88. ret = rproc_start(0);
  89. if (ret)
  90. panic("Firmware init failed on rproc (%d)\n", ret);
  91. }
  92. static void k3_sysfw_configure_using_fit(void *fit,
  93. struct ti_sci_handle *ti_sci)
  94. {
  95. struct ti_sci_board_ops *board_ops = &ti_sci->ops.board_ops;
  96. int images;
  97. const void *cfg_fragment_addr;
  98. size_t cfg_fragment_size;
  99. int ret;
  100. /* Find the node holding the images information */
  101. images = fdt_path_offset(fit, FIT_IMAGES_PATH);
  102. if (images < 0)
  103. panic("Cannot find /images node (%d)\n", images);
  104. /* Extract board configuration from FIT */
  105. ret = fit_get_data_by_name(fit, images, SYSFW_CFG_BOARD,
  106. &cfg_fragment_addr, &cfg_fragment_size);
  107. if (ret < 0)
  108. panic("Error accessing %s node in FIT (%d)\n", SYSFW_CFG_BOARD,
  109. ret);
  110. /* Apply board configuration to SYSFW */
  111. ret = board_ops->board_config(ti_sci,
  112. (u64)(u32)cfg_fragment_addr,
  113. (u32)cfg_fragment_size);
  114. if (ret)
  115. panic("Failed to set board configuration (%d)\n", ret);
  116. /* Extract power/clock (PM) specific configuration from FIT */
  117. ret = fit_get_data_by_name(fit, images, SYSFW_CFG_PM,
  118. &cfg_fragment_addr, &cfg_fragment_size);
  119. if (ret < 0)
  120. panic("Error accessing %s node in FIT (%d)\n", SYSFW_CFG_PM,
  121. ret);
  122. /* Apply power/clock (PM) specific configuration to SYSFW */
  123. ret = board_ops->board_config_pm(ti_sci,
  124. (u64)(u32)cfg_fragment_addr,
  125. (u32)cfg_fragment_size);
  126. if (ret)
  127. panic("Failed to set board PM configuration (%d)\n", ret);
  128. /* Extract resource management (RM) specific configuration from FIT */
  129. ret = fit_get_data_by_name(fit, images, SYSFW_CFG_RM,
  130. &cfg_fragment_addr, &cfg_fragment_size);
  131. if (ret < 0)
  132. panic("Error accessing %s node in FIT (%d)\n", SYSFW_CFG_RM,
  133. ret);
  134. /* Apply resource management (RM) configuration to SYSFW */
  135. ret = board_ops->board_config_rm(ti_sci,
  136. (u64)(u32)cfg_fragment_addr,
  137. (u32)cfg_fragment_size);
  138. if (ret)
  139. panic("Failed to set board RM configuration (%d)\n", ret);
  140. /* Extract security specific configuration from FIT */
  141. ret = fit_get_data_by_name(fit, images, SYSFW_CFG_SEC,
  142. &cfg_fragment_addr, &cfg_fragment_size);
  143. if (ret < 0)
  144. panic("Error accessing %s node in FIT (%d)\n", SYSFW_CFG_SEC,
  145. ret);
  146. /* Apply security configuration to SYSFW */
  147. ret = board_ops->board_config_security(ti_sci,
  148. (u64)(u32)cfg_fragment_addr,
  149. (u32)cfg_fragment_size);
  150. if (ret)
  151. panic("Failed to set board security configuration (%d)\n",
  152. ret);
  153. }
  154. #if CONFIG_IS_ENABLED(DFU)
  155. static int k3_sysfw_dfu_download(void *addr)
  156. {
  157. char dfu_str[50];
  158. int ret;
  159. sprintf(dfu_str, "sysfw.itb ram 0x%p 0x%x", addr,
  160. CONFIG_K3_SYSFW_IMAGE_SIZE_MAX);
  161. ret = dfu_config_entities(dfu_str, "ram", "0");
  162. if (ret) {
  163. dfu_free_entities();
  164. goto exit;
  165. }
  166. run_usb_dnl_gadget(0, "usb_dnl_dfu");
  167. exit:
  168. dfu_free_entities();
  169. return ret;
  170. }
  171. #endif
  172. #if CONFIG_IS_ENABLED(SPI_LOAD)
  173. static void *k3_sysfw_get_spi_addr(void)
  174. {
  175. struct udevice *dev;
  176. fdt_addr_t addr;
  177. int ret;
  178. ret = uclass_find_device_by_seq(UCLASS_SPI, CONFIG_SF_DEFAULT_BUS,
  179. true, &dev);
  180. if (ret)
  181. return NULL;
  182. addr = dev_read_addr_index(dev, 1);
  183. if (addr == FDT_ADDR_T_NONE)
  184. return NULL;
  185. return (void *)(addr + CONFIG_K3_SYSFW_IMAGE_SPI_OFFS);
  186. }
  187. #endif
  188. void k3_sysfw_loader(void (*config_pm_pre_callback) (void),
  189. void (*config_pm_done_callback)(void))
  190. {
  191. struct spl_image_info spl_image = { 0 };
  192. struct spl_boot_device bootdev = { 0 };
  193. struct ti_sci_handle *ti_sci;
  194. int ret = 0;
  195. /* Reserve a block of aligned memory for loading the SYSFW image */
  196. sysfw_load_address = memalign(ARCH_DMA_MINALIGN,
  197. CONFIG_K3_SYSFW_IMAGE_SIZE_MAX);
  198. if (!sysfw_load_address)
  199. panic("Error allocating %u bytes of memory for SYSFW image\n",
  200. CONFIG_K3_SYSFW_IMAGE_SIZE_MAX);
  201. debug("%s: allocated %u bytes at 0x%p\n", __func__,
  202. CONFIG_K3_SYSFW_IMAGE_SIZE_MAX, sysfw_load_address);
  203. /* Set load address for legacy modes that bypass spl_get_load_buffer */
  204. spl_image.load_addr = (uintptr_t)sysfw_load_address;
  205. bootdev.boot_device = spl_boot_device();
  206. /* Load combined System Controller firmware and config data image */
  207. switch (bootdev.boot_device) {
  208. #if CONFIG_IS_ENABLED(MMC_SUPPORT)
  209. case BOOT_DEVICE_MMC1:
  210. case BOOT_DEVICE_MMC2:
  211. case BOOT_DEVICE_MMC2_2:
  212. ret = spl_mmc_load(&spl_image, &bootdev,
  213. #ifdef CONFIG_K3_SYSFW_IMAGE_NAME
  214. CONFIG_K3_SYSFW_IMAGE_NAME,
  215. #else
  216. NULL,
  217. #endif
  218. #ifdef CONFIG_K3_SYSFW_IMAGE_MMCSD_RAW_MODE_PART
  219. CONFIG_K3_SYSFW_IMAGE_MMCSD_RAW_MODE_PART,
  220. #else
  221. 0,
  222. #endif
  223. #ifdef CONFIG_K3_SYSFW_IMAGE_MMCSD_RAW_MODE_SECT
  224. CONFIG_K3_SYSFW_IMAGE_MMCSD_RAW_MODE_SECT);
  225. #else
  226. 0);
  227. #endif
  228. break;
  229. #endif
  230. #if CONFIG_IS_ENABLED(SPI_LOAD)
  231. case BOOT_DEVICE_SPI:
  232. sysfw_load_address = k3_sysfw_get_spi_addr();
  233. if (!sysfw_load_address)
  234. ret = -ENODEV;
  235. break;
  236. #endif
  237. #if CONFIG_IS_ENABLED(YMODEM_SUPPORT)
  238. case BOOT_DEVICE_UART:
  239. #ifdef CONFIG_K3_EARLY_CONS
  240. /*
  241. * Establish a serial console if not yet available as required
  242. * for UART-based boot. For this use the early console feature
  243. * that allows setting up a UART for use before SYSFW has been
  244. * brought up. Note that the associated UART module's clocks
  245. * must have gotten enabled by the ROM bootcode which will be
  246. * the case when continuing to boot serially from the same
  247. * UART that the ROM loaded the initial bootloader from.
  248. */
  249. if (!gd->have_console)
  250. early_console_init();
  251. #endif
  252. ret = spl_ymodem_load_image(&spl_image, &bootdev);
  253. break;
  254. #endif
  255. #if CONFIG_IS_ENABLED(DFU)
  256. case BOOT_DEVICE_DFU:
  257. ret = k3_sysfw_dfu_download(sysfw_load_address);
  258. break;
  259. #endif
  260. default:
  261. panic("Loading SYSFW image from device %u not supported!\n",
  262. bootdev.boot_device);
  263. }
  264. if (ret)
  265. panic("Error %d occurred during loading SYSFW image!\n", ret);
  266. /*
  267. * Now that SYSFW got loaded set helper flag to restore regular SPL
  268. * loader behavior so we can later boot into the next stage as expected.
  269. */
  270. sysfw_loaded = true;
  271. /* Ensure the SYSFW image is in FIT format */
  272. if (image_get_magic((const image_header_t *)sysfw_load_address) !=
  273. FDT_MAGIC)
  274. panic("SYSFW image not in FIT format!\n");
  275. /* Extract and start SYSFW */
  276. k3_sysfw_load_using_fit(sysfw_load_address);
  277. /* Get handle for accessing SYSFW services */
  278. ti_sci = get_ti_sci_handle();
  279. if (config_pm_pre_callback)
  280. config_pm_pre_callback();
  281. /* Parse and apply the different SYSFW configuration fragments */
  282. k3_sysfw_configure_using_fit(sysfw_load_address, ti_sci);
  283. /*
  284. * Now that all clocks and PM aspects are setup, invoke a user-
  285. * provided callback function. Usually this callback would be used
  286. * to setup or re-configure the U-Boot console UART.
  287. */
  288. if (config_pm_done_callback)
  289. config_pm_done_callback();
  290. }