sysfw-loader.c 9.3 KB

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