bl2_plat_setup.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. /*
  2. * Copyright (c) 2023-2024, STMicroelectronics - All Rights Reserved
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <assert.h>
  7. #include <cdefs.h>
  8. #include <errno.h>
  9. #include <stdint.h>
  10. #include <common/debug.h>
  11. #include <common/desc_image_load.h>
  12. #include <drivers/clk.h>
  13. #include <drivers/mmc.h>
  14. #include <drivers/st/regulator_fixed.h>
  15. #include <drivers/st/stm32mp2_ddr_helpers.h>
  16. #include <drivers/st/stm32mp2_ram.h>
  17. #include <drivers/st/stm32mp_pmic2.h>
  18. #include <drivers/st/stm32mp_risab_regs.h>
  19. #include <lib/fconf/fconf.h>
  20. #include <lib/fconf/fconf_dyn_cfg_getter.h>
  21. #include <lib/mmio.h>
  22. #include <lib/optee_utils.h>
  23. #include <lib/xlat_tables/xlat_tables_v2.h>
  24. #include <plat/common/platform.h>
  25. #include <platform_def.h>
  26. #include <stm32mp_common.h>
  27. #include <stm32mp_dt.h>
  28. #define BOOT_CTX_ADDR 0x0e000020UL
  29. static void print_reset_reason(void)
  30. {
  31. uint32_t rstsr = mmio_read_32(stm32mp_rcc_base() + RCC_C1BOOTRSTSCLRR);
  32. if (rstsr == 0U) {
  33. WARN("Reset reason unknown\n");
  34. return;
  35. }
  36. INFO("Reset reason (0x%x):\n", rstsr);
  37. if ((rstsr & RCC_C1BOOTRSTSCLRR_PADRSTF) == 0U) {
  38. if ((rstsr & RCC_C1BOOTRSTSCLRR_STBYC1RSTF) != 0U) {
  39. INFO("System exits from Standby for CA35\n");
  40. return;
  41. }
  42. if ((rstsr & RCC_C1BOOTRSTSCLRR_D1STBYRSTF) != 0U) {
  43. INFO("D1 domain exits from DStandby\n");
  44. return;
  45. }
  46. }
  47. if ((rstsr & RCC_C1BOOTRSTSCLRR_PORRSTF) != 0U) {
  48. INFO(" Power-on Reset (rst_por)\n");
  49. return;
  50. }
  51. if ((rstsr & RCC_C1BOOTRSTSCLRR_BORRSTF) != 0U) {
  52. INFO(" Brownout Reset (rst_bor)\n");
  53. return;
  54. }
  55. if ((rstsr & RCC_C1BOOTRSTSSETR_SYSC2RSTF) != 0U) {
  56. INFO(" System reset (SYSRST) by M33\n");
  57. return;
  58. }
  59. if ((rstsr & RCC_C1BOOTRSTSSETR_SYSC1RSTF) != 0U) {
  60. INFO(" System reset (SYSRST) by A35\n");
  61. return;
  62. }
  63. if ((rstsr & RCC_C1BOOTRSTSCLRR_HCSSRSTF) != 0U) {
  64. INFO(" Clock failure on HSE\n");
  65. return;
  66. }
  67. if ((rstsr & RCC_C1BOOTRSTSCLRR_IWDG1SYSRSTF) != 0U) {
  68. INFO(" IWDG1 system reset (rst_iwdg1)\n");
  69. return;
  70. }
  71. if ((rstsr & RCC_C1BOOTRSTSCLRR_IWDG2SYSRSTF) != 0U) {
  72. INFO(" IWDG2 system reset (rst_iwdg2)\n");
  73. return;
  74. }
  75. if ((rstsr & RCC_C1BOOTRSTSCLRR_IWDG3SYSRSTF) != 0U) {
  76. INFO(" IWDG3 system reset (rst_iwdg3)\n");
  77. return;
  78. }
  79. if ((rstsr & RCC_C1BOOTRSTSCLRR_IWDG4SYSRSTF) != 0U) {
  80. INFO(" IWDG4 system reset (rst_iwdg4)\n");
  81. return;
  82. }
  83. if ((rstsr & RCC_C1BOOTRSTSCLRR_IWDG5SYSRSTF) != 0U) {
  84. INFO(" IWDG5 system reset (rst_iwdg5)\n");
  85. return;
  86. }
  87. if ((rstsr & RCC_C1BOOTRSTSCLRR_C1P1RSTF) != 0U) {
  88. INFO(" A35 processor core 1 reset\n");
  89. return;
  90. }
  91. if ((rstsr & RCC_C1BOOTRSTSCLRR_PADRSTF) != 0U) {
  92. INFO(" Pad Reset from NRST\n");
  93. return;
  94. }
  95. if ((rstsr & RCC_C1BOOTRSTSCLRR_VCORERSTF) != 0U) {
  96. INFO(" Reset due to a failure of VDD_CORE\n");
  97. return;
  98. }
  99. if ((rstsr & RCC_C1BOOTRSTSCLRR_C1RSTF) != 0U) {
  100. INFO(" A35 processor reset\n");
  101. return;
  102. }
  103. ERROR(" Unidentified reset reason\n");
  104. }
  105. void bl2_el3_early_platform_setup(u_register_t arg0 __unused,
  106. u_register_t arg1 __unused,
  107. u_register_t arg2 __unused,
  108. u_register_t arg3 __unused)
  109. {
  110. stm32mp_save_boot_ctx_address(BOOT_CTX_ADDR);
  111. }
  112. void bl2_platform_setup(void)
  113. {
  114. int ret;
  115. ret = stm32mp2_ddr_probe();
  116. if (ret != 0) {
  117. ERROR("DDR probe: error %d\n", ret);
  118. panic();
  119. }
  120. /* Map DDR for binary load, now with cacheable attribute */
  121. ret = mmap_add_dynamic_region(STM32MP_DDR_BASE, STM32MP_DDR_BASE,
  122. STM32MP_DDR_MAX_SIZE, MT_MEMORY | MT_RW | MT_SECURE);
  123. if (ret < 0) {
  124. ERROR("DDR mapping: error %d\n", ret);
  125. panic();
  126. }
  127. }
  128. static void reset_backup_domain(void)
  129. {
  130. uintptr_t pwr_base = stm32mp_pwr_base();
  131. uintptr_t rcc_base = stm32mp_rcc_base();
  132. /*
  133. * Disable the backup domain write protection.
  134. * The protection is enable at each reset by hardware
  135. * and must be disabled by software.
  136. */
  137. mmio_setbits_32(pwr_base + PWR_BDCR1, PWR_BDCR1_DBD3P);
  138. while ((mmio_read_32(pwr_base + PWR_BDCR1) & PWR_BDCR1_DBD3P) == 0U) {
  139. ;
  140. }
  141. /* Reset backup domain on cold boot cases */
  142. if ((mmio_read_32(rcc_base + RCC_BDCR) & RCC_BDCR_RTCCKEN) == 0U) {
  143. mmio_setbits_32(rcc_base + RCC_BDCR, RCC_BDCR_VSWRST);
  144. while ((mmio_read_32(rcc_base + RCC_BDCR) & RCC_BDCR_VSWRST) == 0U) {
  145. ;
  146. }
  147. mmio_clrbits_32(rcc_base + RCC_BDCR, RCC_BDCR_VSWRST);
  148. }
  149. }
  150. void bl2_el3_plat_arch_setup(void)
  151. {
  152. const char *board_model;
  153. boot_api_context_t *boot_context =
  154. (boot_api_context_t *)stm32mp_get_boot_ctx_address();
  155. if (stm32_otp_probe() != 0U) {
  156. EARLY_ERROR("OTP probe failed\n");
  157. panic();
  158. }
  159. mmap_add_region(BL_CODE_BASE, BL_CODE_BASE,
  160. BL_CODE_END - BL_CODE_BASE,
  161. MT_CODE | MT_SECURE);
  162. configure_mmu();
  163. if (dt_open_and_check(STM32MP_DTB_BASE) < 0) {
  164. panic();
  165. }
  166. reset_backup_domain();
  167. /*
  168. * Initialize DDR sub-system clock. This needs to be done before enabling DDR PLL (PLL2),
  169. * and so before stm32mp2_clk_init().
  170. */
  171. ddr_sub_system_clk_init();
  172. if (stm32mp2_clk_init() < 0) {
  173. panic();
  174. }
  175. #if STM32MP_DDR_FIP_IO_STORAGE
  176. /*
  177. * RISAB3 setup (dedicated for SRAM1)
  178. *
  179. * Allow secure read/writes data accesses to non-secure
  180. * blocks or pages, all RISAB registers are writable.
  181. * DDR firmwares are saved there before being loaded in DDRPHY memory.
  182. */
  183. mmio_write_32(RISAB3_BASE + RISAB_CR, RISAB_CR_SRWIAD);
  184. #endif
  185. stm32_save_boot_info(boot_context);
  186. if (stm32mp_uart_console_setup() != 0) {
  187. goto skip_console_init;
  188. }
  189. stm32mp_print_cpuinfo();
  190. board_model = dt_get_board_model();
  191. if (board_model != NULL) {
  192. NOTICE("Model: %s\n", board_model);
  193. }
  194. stm32mp_print_boardinfo();
  195. print_reset_reason();
  196. skip_console_init:
  197. if (fixed_regulator_register() != 0) {
  198. panic();
  199. }
  200. if (dt_pmic_status() > 0) {
  201. initialize_pmic();
  202. }
  203. fconf_populate("TB_FW", STM32MP_DTB_BASE);
  204. /*
  205. * RISAB5 setup (dedicated for RETRAM)
  206. *
  207. * Allow secure read/writes data accesses to non-secure
  208. * blocks or pages, all RISAB registers are writable.
  209. * DDR retention registers are saved there and restored
  210. * when exiting standby low power state.
  211. */
  212. mmio_write_32(RISAB5_BASE + RISAB_CR, RISAB_CR_SRWIAD);
  213. stm32mp_io_setup();
  214. }
  215. /*******************************************************************************
  216. * This function can be used by the platforms to update/use image
  217. * information for given `image_id`.
  218. ******************************************************************************/
  219. int bl2_plat_handle_post_image_load(unsigned int image_id)
  220. {
  221. int err = 0;
  222. bl_mem_params_node_t *bl_mem_params = get_bl_mem_params_node(image_id);
  223. bl_mem_params_node_t *pager_mem_params;
  224. const struct dyn_cfg_dtb_info_t *config_info;
  225. unsigned int i;
  226. const unsigned int image_ids[] = {
  227. BL31_IMAGE_ID,
  228. SOC_FW_CONFIG_ID,
  229. BL32_IMAGE_ID,
  230. BL33_IMAGE_ID,
  231. HW_CONFIG_ID,
  232. };
  233. assert(bl_mem_params != NULL);
  234. #if STM32MP_SDMMC || STM32MP_EMMC
  235. /*
  236. * Invalidate remaining data read from MMC but not flushed by load_image_flush().
  237. * We take the worst case which is 2 MMC blocks.
  238. */
  239. if ((image_id != FW_CONFIG_ID) &&
  240. ((bl_mem_params->image_info.h.attr & IMAGE_ATTRIB_SKIP_LOADING) == 0U)) {
  241. inv_dcache_range(bl_mem_params->image_info.image_base +
  242. bl_mem_params->image_info.image_size,
  243. 2U * MMC_BLOCK_SIZE);
  244. }
  245. #endif /* STM32MP_SDMMC || STM32MP_EMMC */
  246. switch (image_id) {
  247. case FW_CONFIG_ID:
  248. /* Set global DTB info for fixed fw_config information */
  249. set_config_info(STM32MP_FW_CONFIG_BASE, ~0UL, STM32MP_FW_CONFIG_MAX_SIZE,
  250. FW_CONFIG_ID);
  251. fconf_populate("FW_CONFIG", STM32MP_FW_CONFIG_BASE);
  252. /* Iterate through all the fw config IDs */
  253. for (i = 0U; i < ARRAY_SIZE(image_ids); i++) {
  254. bl_mem_params = get_bl_mem_params_node(image_ids[i]);
  255. assert(bl_mem_params != NULL);
  256. config_info = FCONF_GET_PROPERTY(dyn_cfg, dtb, image_ids[i]);
  257. if (config_info == NULL) {
  258. continue;
  259. }
  260. bl_mem_params->image_info.image_base = config_info->config_addr;
  261. bl_mem_params->image_info.image_max_size = config_info->config_max_size;
  262. bl_mem_params->image_info.h.attr &= ~IMAGE_ATTRIB_SKIP_LOADING;
  263. switch (image_ids[i]) {
  264. case BL31_IMAGE_ID:
  265. bl_mem_params->ep_info.pc = config_info->config_addr;
  266. break;
  267. case BL32_IMAGE_ID:
  268. bl_mem_params->ep_info.pc = config_info->config_addr;
  269. /* In case of OPTEE, initialize address space with tos_fw addr */
  270. pager_mem_params = get_bl_mem_params_node(BL32_EXTRA1_IMAGE_ID);
  271. if (pager_mem_params != NULL) {
  272. pager_mem_params->image_info.image_base =
  273. config_info->config_addr;
  274. pager_mem_params->image_info.image_max_size =
  275. config_info->config_max_size;
  276. }
  277. break;
  278. case BL33_IMAGE_ID:
  279. bl_mem_params->ep_info.pc = config_info->config_addr;
  280. break;
  281. case HW_CONFIG_ID:
  282. case SOC_FW_CONFIG_ID:
  283. break;
  284. default:
  285. return -EINVAL;
  286. }
  287. }
  288. /*
  289. * After this step, the BL2 device tree area will be overwritten
  290. * with BL31 binary, no other data should be read from BL2 DT.
  291. */
  292. break;
  293. case BL32_IMAGE_ID:
  294. if ((bl_mem_params->image_info.image_base != 0UL) &&
  295. (optee_header_is_valid(bl_mem_params->image_info.image_base))) {
  296. /* BL32 is OP-TEE header */
  297. bl_mem_params->ep_info.pc = bl_mem_params->image_info.image_base;
  298. pager_mem_params = get_bl_mem_params_node(BL32_EXTRA1_IMAGE_ID);
  299. assert(pager_mem_params != NULL);
  300. err = parse_optee_header(&bl_mem_params->ep_info,
  301. &pager_mem_params->image_info,
  302. NULL);
  303. if (err != 0) {
  304. ERROR("OPTEE header parse error.\n");
  305. panic();
  306. }
  307. /* Set optee boot info from parsed header data */
  308. bl_mem_params->ep_info.args.arg0 = 0U; /* Unused */
  309. bl_mem_params->ep_info.args.arg1 = 0U; /* Unused */
  310. bl_mem_params->ep_info.args.arg2 = 0U; /* No DT supported */
  311. }
  312. break;
  313. case BL33_IMAGE_ID:
  314. default:
  315. /* Do nothing in default case */
  316. break;
  317. }
  318. return err;
  319. }