sysfw-loader.c 9.3 KB

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