stm32mp_common.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. /*
  2. * Copyright (c) 2015-2024, Arm Limited and Contributors. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <assert.h>
  7. #include <errno.h>
  8. #include <arch_helpers.h>
  9. #include <common/debug.h>
  10. #include <drivers/clk.h>
  11. #include <drivers/delay_timer.h>
  12. #include <drivers/st/stm32_console.h>
  13. #include <drivers/st/stm32mp_clkfunc.h>
  14. #include <drivers/st/stm32mp_reset.h>
  15. #include <lib/mmio.h>
  16. #include <lib/smccc.h>
  17. #include <lib/xlat_tables/xlat_tables_v2.h>
  18. #include <plat/common/platform.h>
  19. #include <services/arm_arch_svc.h>
  20. #include <platform_def.h>
  21. #define HEADER_VERSION_MAJOR_MASK GENMASK(23, 16)
  22. #define RESET_TIMEOUT_US_1MS 1000U
  23. /* Internal layout of the 32bit OTP word board_id */
  24. #define BOARD_ID_BOARD_NB_MASK GENMASK_32(31, 16)
  25. #define BOARD_ID_BOARD_NB_SHIFT 16
  26. #define BOARD_ID_VARCPN_MASK GENMASK_32(15, 12)
  27. #define BOARD_ID_VARCPN_SHIFT 12
  28. #define BOARD_ID_REVISION_MASK GENMASK_32(11, 8)
  29. #define BOARD_ID_REVISION_SHIFT 8
  30. #define BOARD_ID_VARFG_MASK GENMASK_32(7, 4)
  31. #define BOARD_ID_VARFG_SHIFT 4
  32. #define BOARD_ID_BOM_MASK GENMASK_32(3, 0)
  33. #define BOARD_ID2NB(_id) (((_id) & BOARD_ID_BOARD_NB_MASK) >> \
  34. BOARD_ID_BOARD_NB_SHIFT)
  35. #define BOARD_ID2VARCPN(_id) (((_id) & BOARD_ID_VARCPN_MASK) >> \
  36. BOARD_ID_VARCPN_SHIFT)
  37. #define BOARD_ID2REV(_id) (((_id) & BOARD_ID_REVISION_MASK) >> \
  38. BOARD_ID_REVISION_SHIFT)
  39. #define BOARD_ID2VARFG(_id) (((_id) & BOARD_ID_VARFG_MASK) >> \
  40. BOARD_ID_VARFG_SHIFT)
  41. #define BOARD_ID2BOM(_id) ((_id) & BOARD_ID_BOM_MASK)
  42. #define BOOT_AUTH_MASK GENMASK_32(23, 20)
  43. #define BOOT_AUTH_SHIFT 20
  44. #define BOOT_PART_MASK GENMASK_32(19, 16)
  45. #define BOOT_PART_SHIFT 16
  46. #define BOOT_ITF_MASK GENMASK_32(15, 12)
  47. #define BOOT_ITF_SHIFT 12
  48. #define BOOT_INST_MASK GENMASK_32(11, 8)
  49. #define BOOT_INST_SHIFT 8
  50. /* Layout for fwu update information. */
  51. #define FWU_INFO_IDX_MSK GENMASK(3, 0)
  52. #define FWU_INFO_IDX_OFF U(0)
  53. #define FWU_INFO_CNT_MSK GENMASK(7, 4)
  54. #define FWU_INFO_CNT_OFF U(4)
  55. static console_t console;
  56. uintptr_t plat_get_ns_image_entrypoint(void)
  57. {
  58. return BL33_BASE;
  59. }
  60. unsigned int plat_get_syscnt_freq2(void)
  61. {
  62. return read_cntfrq_el0();
  63. }
  64. static uintptr_t boot_ctx_address;
  65. static uint16_t boot_itf_selected;
  66. void stm32mp_save_boot_ctx_address(uintptr_t address)
  67. {
  68. boot_api_context_t *boot_context = (boot_api_context_t *)address;
  69. boot_ctx_address = address;
  70. boot_itf_selected = boot_context->boot_interface_selected;
  71. }
  72. uintptr_t stm32mp_get_boot_ctx_address(void)
  73. {
  74. return boot_ctx_address;
  75. }
  76. uint16_t stm32mp_get_boot_itf_selected(void)
  77. {
  78. return boot_itf_selected;
  79. }
  80. uintptr_t stm32mp_ddrctrl_base(void)
  81. {
  82. return DDRCTRL_BASE;
  83. }
  84. uintptr_t stm32mp_ddrphyc_base(void)
  85. {
  86. return DDRPHYC_BASE;
  87. }
  88. uintptr_t stm32mp_pwr_base(void)
  89. {
  90. return PWR_BASE;
  91. }
  92. uintptr_t stm32mp_rcc_base(void)
  93. {
  94. return RCC_BASE;
  95. }
  96. bool stm32mp_lock_available(void)
  97. {
  98. const uint32_t c_m_bits = SCTLR_M_BIT | SCTLR_C_BIT;
  99. /* The spinlocks are used only when MMU and data cache are enabled */
  100. #ifdef __aarch64__
  101. return (read_sctlr_el3() & c_m_bits) == c_m_bits;
  102. #else
  103. return (read_sctlr() & c_m_bits) == c_m_bits;
  104. #endif
  105. }
  106. int stm32mp_map_ddr_non_cacheable(void)
  107. {
  108. return mmap_add_dynamic_region(STM32MP_DDR_BASE, STM32MP_DDR_BASE,
  109. STM32MP_DDR_MAX_SIZE,
  110. MT_NON_CACHEABLE | MT_RW | MT_SECURE);
  111. }
  112. int stm32mp_unmap_ddr(void)
  113. {
  114. return mmap_remove_dynamic_region(STM32MP_DDR_BASE,
  115. STM32MP_DDR_MAX_SIZE);
  116. }
  117. int stm32_get_otp_index(const char *otp_name, uint32_t *otp_idx,
  118. uint32_t *otp_len)
  119. {
  120. assert(otp_name != NULL);
  121. assert(otp_idx != NULL);
  122. return dt_find_otp_name(otp_name, otp_idx, otp_len);
  123. }
  124. int stm32_get_otp_value(const char *otp_name, uint32_t *otp_val)
  125. {
  126. uint32_t otp_idx;
  127. assert(otp_name != NULL);
  128. assert(otp_val != NULL);
  129. if (stm32_get_otp_index(otp_name, &otp_idx, NULL) != 0) {
  130. return -1;
  131. }
  132. if (stm32_get_otp_value_from_idx(otp_idx, otp_val) != 0) {
  133. ERROR("BSEC: %s Read Error\n", otp_name);
  134. return -1;
  135. }
  136. return 0;
  137. }
  138. int stm32_get_otp_value_from_idx(const uint32_t otp_idx, uint32_t *otp_val)
  139. {
  140. uint32_t ret = BSEC_NOT_SUPPORTED;
  141. assert(otp_val != NULL);
  142. #if defined(IMAGE_BL2)
  143. ret = stm32_otp_shadow_read(otp_val, otp_idx);
  144. #elif defined(IMAGE_BL31) || defined(IMAGE_BL32)
  145. ret = stm32_otp_read(otp_val, otp_idx);
  146. #else
  147. #error "Not supported"
  148. #endif
  149. if (ret != BSEC_OK) {
  150. ERROR("BSEC: idx=%u Read Error\n", otp_idx);
  151. return -1;
  152. }
  153. return 0;
  154. }
  155. #if defined(IMAGE_BL2)
  156. static void reset_uart(uint32_t reset)
  157. {
  158. int ret;
  159. ret = stm32mp_reset_assert(reset, RESET_TIMEOUT_US_1MS);
  160. if (ret != 0) {
  161. panic();
  162. }
  163. udelay(2);
  164. ret = stm32mp_reset_deassert(reset, RESET_TIMEOUT_US_1MS);
  165. if (ret != 0) {
  166. panic();
  167. }
  168. mdelay(1);
  169. }
  170. #endif
  171. static void set_console(uintptr_t base, uint32_t clk_rate)
  172. {
  173. unsigned int console_flags;
  174. if (console_stm32_register(base, clk_rate,
  175. (uint32_t)STM32MP_UART_BAUDRATE, &console) == 0) {
  176. panic();
  177. }
  178. console_flags = CONSOLE_FLAG_BOOT | CONSOLE_FLAG_CRASH |
  179. CONSOLE_FLAG_TRANSLATE_CRLF;
  180. #if !defined(IMAGE_BL2) && defined(DEBUG)
  181. console_flags |= CONSOLE_FLAG_RUNTIME;
  182. #endif
  183. console_set_scope(&console, console_flags);
  184. }
  185. int stm32mp_uart_console_setup(void)
  186. {
  187. struct dt_node_info dt_uart_info;
  188. uint32_t clk_rate = 0U;
  189. int result;
  190. uint32_t boot_itf __unused;
  191. uint32_t boot_instance __unused;
  192. result = dt_get_stdout_uart_info(&dt_uart_info);
  193. if ((result <= 0) ||
  194. (dt_uart_info.status == DT_DISABLED)) {
  195. return -ENODEV;
  196. }
  197. #if defined(IMAGE_BL2)
  198. if ((dt_uart_info.clock < 0) ||
  199. (dt_uart_info.reset < 0)) {
  200. return -ENODEV;
  201. }
  202. #endif
  203. #if STM32MP_UART_PROGRAMMER || !defined(IMAGE_BL2)
  204. stm32_get_boot_interface(&boot_itf, &boot_instance);
  205. if ((boot_itf == BOOT_API_CTX_BOOT_INTERFACE_SEL_SERIAL_UART) &&
  206. (get_uart_address(boot_instance) == dt_uart_info.base)) {
  207. return -EACCES;
  208. }
  209. #endif
  210. #if defined(IMAGE_BL2)
  211. if (dt_set_stdout_pinctrl() != 0) {
  212. return -ENODEV;
  213. }
  214. clk_enable((unsigned long)dt_uart_info.clock);
  215. reset_uart((uint32_t)dt_uart_info.reset);
  216. clk_rate = clk_get_rate((unsigned long)dt_uart_info.clock);
  217. #endif
  218. set_console(dt_uart_info.base, clk_rate);
  219. return 0;
  220. }
  221. #if EARLY_CONSOLE
  222. void plat_setup_early_console(void)
  223. {
  224. #if defined(IMAGE_BL2) || STM32MP_RECONFIGURE_CONSOLE
  225. plat_crash_console_init();
  226. #endif
  227. set_console(STM32MP_DEBUG_USART_BASE, STM32MP_DEBUG_USART_CLK_FRQ);
  228. NOTICE("Early console setup\n");
  229. }
  230. #endif /* EARLY_CONSOLE */
  231. /*****************************************************************************
  232. * plat_is_smccc_feature_available() - This function checks whether SMCCC
  233. * feature is availabile for platform.
  234. * @fid: SMCCC function id
  235. *
  236. * Return SMC_ARCH_CALL_SUCCESS if SMCCC feature is available and
  237. * SMC_ARCH_CALL_NOT_SUPPORTED otherwise.
  238. *****************************************************************************/
  239. int32_t plat_is_smccc_feature_available(u_register_t fid)
  240. {
  241. switch (fid) {
  242. case SMCCC_ARCH_SOC_ID:
  243. return SMC_ARCH_CALL_SUCCESS;
  244. default:
  245. return SMC_ARCH_CALL_NOT_SUPPORTED;
  246. }
  247. }
  248. /* Get SOC version */
  249. int32_t plat_get_soc_version(void)
  250. {
  251. uint32_t chip_id = stm32mp_get_chip_dev_id();
  252. uint32_t manfid = SOC_ID_SET_JEP_106(JEDEC_ST_BKID, JEDEC_ST_MFID);
  253. return (int32_t)(manfid | (chip_id & SOC_ID_IMPL_DEF_MASK));
  254. }
  255. /* Get SOC revision */
  256. int32_t plat_get_soc_revision(void)
  257. {
  258. return (int32_t)(stm32mp_get_chip_version() & SOC_ID_REV_MASK);
  259. }
  260. void stm32_display_board_info(uint32_t board_id)
  261. {
  262. char rev[2];
  263. rev[0] = BOARD_ID2REV(board_id) - 1 + 'A';
  264. rev[1] = '\0';
  265. NOTICE("Board: MB%04x Var%u.%u Rev.%s-%02u\n",
  266. BOARD_ID2NB(board_id),
  267. BOARD_ID2VARCPN(board_id),
  268. BOARD_ID2VARFG(board_id),
  269. rev,
  270. BOARD_ID2BOM(board_id));
  271. }
  272. void stm32_save_boot_info(boot_api_context_t *boot_context)
  273. {
  274. uint32_t auth_status;
  275. assert(boot_context->boot_interface_instance <= (BOOT_INST_MASK >> BOOT_INST_SHIFT));
  276. assert(boot_context->boot_interface_selected <= (BOOT_ITF_MASK >> BOOT_ITF_SHIFT));
  277. assert(boot_context->boot_partition_used_toboot <= (BOOT_PART_MASK >> BOOT_PART_SHIFT));
  278. switch (boot_context->auth_status) {
  279. case BOOT_API_CTX_AUTH_NO:
  280. auth_status = 0x0U;
  281. break;
  282. case BOOT_API_CTX_AUTH_SUCCESS:
  283. auth_status = 0x2U;
  284. break;
  285. case BOOT_API_CTX_AUTH_FAILED:
  286. default:
  287. auth_status = 0x1U;
  288. break;
  289. }
  290. clk_enable(TAMP_BKP_REG_CLK);
  291. mmio_clrsetbits_32(stm32_get_bkpr_boot_mode_addr(),
  292. BOOT_ITF_MASK | BOOT_INST_MASK | BOOT_PART_MASK | BOOT_AUTH_MASK,
  293. (boot_context->boot_interface_instance << BOOT_INST_SHIFT) |
  294. (boot_context->boot_interface_selected << BOOT_ITF_SHIFT) |
  295. (boot_context->boot_partition_used_toboot << BOOT_PART_SHIFT) |
  296. (auth_status << BOOT_AUTH_SHIFT));
  297. clk_disable(TAMP_BKP_REG_CLK);
  298. }
  299. void stm32_get_boot_interface(uint32_t *interface, uint32_t *instance)
  300. {
  301. static uint32_t itf;
  302. if (itf == 0U) {
  303. clk_enable(TAMP_BKP_REG_CLK);
  304. itf = mmio_read_32(stm32_get_bkpr_boot_mode_addr()) &
  305. (BOOT_ITF_MASK | BOOT_INST_MASK);
  306. clk_disable(TAMP_BKP_REG_CLK);
  307. }
  308. *interface = (itf & BOOT_ITF_MASK) >> BOOT_ITF_SHIFT;
  309. *instance = (itf & BOOT_INST_MASK) >> BOOT_INST_SHIFT;
  310. }
  311. #if PSA_FWU_SUPPORT
  312. void stm32_fwu_set_boot_idx(void)
  313. {
  314. clk_enable(TAMP_BKP_REG_CLK);
  315. mmio_clrsetbits_32(stm32_get_bkpr_fwu_info_addr(),
  316. FWU_INFO_IDX_MSK,
  317. (plat_fwu_get_boot_idx() << FWU_INFO_IDX_OFF) &
  318. FWU_INFO_IDX_MSK);
  319. clk_disable(TAMP_BKP_REG_CLK);
  320. }
  321. uint32_t stm32_get_and_dec_fwu_trial_boot_cnt(void)
  322. {
  323. uintptr_t bkpr_fwu_cnt = stm32_get_bkpr_fwu_info_addr();
  324. uint32_t try_cnt;
  325. clk_enable(TAMP_BKP_REG_CLK);
  326. try_cnt = (mmio_read_32(bkpr_fwu_cnt) & FWU_INFO_CNT_MSK) >> FWU_INFO_CNT_OFF;
  327. assert(try_cnt <= FWU_MAX_TRIAL_REBOOT);
  328. if (try_cnt != 0U) {
  329. mmio_clrsetbits_32(bkpr_fwu_cnt, FWU_INFO_CNT_MSK,
  330. (try_cnt - 1U) << FWU_INFO_CNT_OFF);
  331. }
  332. clk_disable(TAMP_BKP_REG_CLK);
  333. return try_cnt;
  334. }
  335. void stm32_set_max_fwu_trial_boot_cnt(void)
  336. {
  337. uintptr_t bkpr_fwu_cnt = stm32_get_bkpr_fwu_info_addr();
  338. clk_enable(TAMP_BKP_REG_CLK);
  339. mmio_clrsetbits_32(bkpr_fwu_cnt, FWU_INFO_CNT_MSK,
  340. (FWU_MAX_TRIAL_REBOOT << FWU_INFO_CNT_OFF) & FWU_INFO_CNT_MSK);
  341. clk_disable(TAMP_BKP_REG_CLK);
  342. }
  343. void stm32_clear_fwu_trial_boot_cnt(void)
  344. {
  345. uintptr_t bkpr_fwu_cnt = stm32_get_bkpr_fwu_info_addr();
  346. clk_enable(TAMP_BKP_REG_CLK);
  347. mmio_clrbits_32(bkpr_fwu_cnt, FWU_INFO_CNT_MSK);
  348. clk_disable(TAMP_BKP_REG_CLK);
  349. }
  350. #endif /* PSA_FWU_SUPPORT */