sysfw-loader.c 9.3 KB

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