stm32mp1_syscfg.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. /*
  2. * Copyright (c) 2019-2024, STMicroelectronics - All Rights Reserved
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <assert.h>
  7. #include <errno.h>
  8. #include <common/debug.h>
  9. #include <drivers/clk.h>
  10. #include <drivers/delay_timer.h>
  11. #include <drivers/st/stpmic1.h>
  12. #include <lib/mmio.h>
  13. #include <lib/utils_def.h>
  14. #include <libfdt.h>
  15. #include <platform_def.h>
  16. #include <stm32mp_common.h>
  17. #include <stm32mp_dt.h>
  18. #include <stm32mp1_private.h>
  19. /*
  20. * SYSCFG REGISTER OFFSET (base relative)
  21. */
  22. #define SYSCFG_BOOTR 0x00U
  23. #define SYSCFG_BOOTCR 0x0CU
  24. #if STM32MP15
  25. #define SYSCFG_IOCTRLSETR 0x18U
  26. #define SYSCFG_ICNR 0x1CU
  27. #endif
  28. #define SYSCFG_CMPCR 0x20U
  29. #define SYSCFG_CMPENSETR 0x24U
  30. #define SYSCFG_CMPENCLRR 0x28U
  31. #if STM32MP13
  32. #define SYSCFG_CMPSD1CR 0x30U
  33. #define SYSCFG_CMPSD1ENSETR 0x34U
  34. #define SYSCFG_CMPSD1ENCLRR 0x38U
  35. #define SYSCFG_CMPSD2CR 0x40U
  36. #define SYSCFG_CMPSD2ENSETR 0x44U
  37. #define SYSCFG_CMPSD2ENCLRR 0x48U
  38. #define SYSCFG_HSLVEN0R 0x50U
  39. #endif
  40. #define SYSCFG_IDC 0x380U
  41. #define CMPCR_CMPENSETR_OFFSET 0x4U
  42. #define CMPCR_CMPENCLRR_OFFSET 0x8U
  43. /*
  44. * SYSCFG_BOOTR Register
  45. */
  46. #define SYSCFG_BOOTR_BOOT_MASK GENMASK(2, 0)
  47. #if STM32MP15
  48. #define SYSCFG_BOOTR_BOOTPD_MASK GENMASK(6, 4)
  49. #define SYSCFG_BOOTR_BOOTPD_SHIFT 4
  50. #endif
  51. /*
  52. * SYSCFG_BOOTCR Register
  53. */
  54. #define SYSCFG_BOOTCR_BMEN BIT(0)
  55. /*
  56. * SYSCFG_IOCTRLSETR Register
  57. */
  58. #define SYSCFG_IOCTRLSETR_HSLVEN_TRACE BIT(0)
  59. #define SYSCFG_IOCTRLSETR_HSLVEN_QUADSPI BIT(1)
  60. #define SYSCFG_IOCTRLSETR_HSLVEN_ETH BIT(2)
  61. #define SYSCFG_IOCTRLSETR_HSLVEN_SDMMC BIT(3)
  62. #define SYSCFG_IOCTRLSETR_HSLVEN_SPI BIT(4)
  63. /*
  64. * SYSCFG_ICNR Register
  65. */
  66. #define SYSCFG_ICNR_AXI_M9 BIT(9)
  67. /*
  68. * SYSCFG_CMPCR Register
  69. */
  70. #define SYSCFG_CMPCR_SW_CTRL BIT(1)
  71. #define SYSCFG_CMPCR_READY BIT(8)
  72. #define SYSCFG_CMPCR_RANSRC GENMASK(19, 16)
  73. #define SYSCFG_CMPCR_RANSRC_SHIFT 16
  74. #define SYSCFG_CMPCR_RAPSRC GENMASK(23, 20)
  75. #define SYSCFG_CMPCR_ANSRC_SHIFT 24
  76. #define SYSCFG_CMPCR_READY_TIMEOUT_US 10000U
  77. /*
  78. * SYSCFG_CMPENSETR Register
  79. */
  80. #define SYSCFG_CMPENSETR_MPU_EN BIT(0)
  81. /*
  82. * HSLV definitions
  83. */
  84. #define HSLV_IDX_TPIU 0U
  85. #define HSLV_IDX_QSPI 1U
  86. #define HSLV_IDX_ETH1 2U
  87. #define HSLV_IDX_ETH2 3U
  88. #define HSLV_IDX_SDMMC1 4U
  89. #define HSLV_IDX_SDMMC2 5U
  90. #define HSLV_IDX_SPI1 6U
  91. #define HSLV_IDX_SPI2 7U
  92. #define HSLV_IDX_SPI3 8U
  93. #define HSLV_IDX_SPI4 9U
  94. #define HSLV_IDX_SPI5 10U
  95. #define HSLV_IDX_LTDC 11U
  96. #define HSLV_NB_IDX 12U
  97. #define HSLV_KEY 0x1018U
  98. /*
  99. * SYSCFG_IDC Register
  100. */
  101. #define SYSCFG_IDC_DEV_ID_MASK GENMASK(11, 0)
  102. #define SYSCFG_IDC_REV_ID_MASK GENMASK(31, 16)
  103. #define SYSCFG_IDC_REV_ID_SHIFT 16
  104. static void enable_io_comp_cell_finish(uintptr_t cmpcr_off)
  105. {
  106. uint64_t start;
  107. start = timeout_init_us(SYSCFG_CMPCR_READY_TIMEOUT_US);
  108. while ((mmio_read_32(SYSCFG_BASE + cmpcr_off) & SYSCFG_CMPCR_READY) == 0U) {
  109. if (timeout_elapsed(start)) {
  110. /* Failure on IO compensation enable is not a issue: warn only. */
  111. WARN("IO compensation cell not ready\n");
  112. break;
  113. }
  114. }
  115. mmio_clrbits_32(SYSCFG_BASE + cmpcr_off, SYSCFG_CMPCR_SW_CTRL);
  116. }
  117. static void disable_io_comp_cell(uintptr_t cmpcr_off)
  118. {
  119. uint32_t value;
  120. if (((mmio_read_32(SYSCFG_BASE + cmpcr_off) & SYSCFG_CMPCR_READY) == 0U) ||
  121. ((mmio_read_32(SYSCFG_BASE + cmpcr_off + CMPCR_CMPENSETR_OFFSET) &
  122. SYSCFG_CMPENSETR_MPU_EN) == 0U)) {
  123. return;
  124. }
  125. value = mmio_read_32(SYSCFG_BASE + cmpcr_off) >> SYSCFG_CMPCR_ANSRC_SHIFT;
  126. mmio_clrbits_32(SYSCFG_BASE + cmpcr_off, SYSCFG_CMPCR_RANSRC | SYSCFG_CMPCR_RAPSRC);
  127. value <<= SYSCFG_CMPCR_RANSRC_SHIFT;
  128. value |= mmio_read_32(SYSCFG_BASE + cmpcr_off);
  129. mmio_write_32(SYSCFG_BASE + cmpcr_off, value | SYSCFG_CMPCR_SW_CTRL);
  130. mmio_setbits_32(SYSCFG_BASE + cmpcr_off + CMPCR_CMPENCLRR_OFFSET, SYSCFG_CMPENSETR_MPU_EN);
  131. }
  132. #if STM32MP13
  133. static int get_regu_max_voltage(void *fdt, int sdmmc_node,
  134. const char *regu_name, uint32_t *regu_val)
  135. {
  136. int node;
  137. const fdt32_t *cuint;
  138. cuint = fdt_getprop(fdt, sdmmc_node, regu_name, NULL);
  139. if (cuint == NULL) {
  140. return -ENODEV;
  141. }
  142. node = fdt_node_offset_by_phandle(fdt, fdt32_to_cpu(*cuint));
  143. if (node < 0) {
  144. return -ENODEV;
  145. }
  146. cuint = fdt_getprop(fdt, node, "regulator-max-microvolt", NULL);
  147. if (cuint == NULL) {
  148. return -ENODEV;
  149. }
  150. *regu_val = fdt32_to_cpu(*cuint);
  151. return 0;
  152. }
  153. static bool sdmmc_is_low_voltage(uintptr_t sdmmc_base)
  154. {
  155. int ret;
  156. int node;
  157. void *fdt = NULL;
  158. uint32_t regu_max_val;
  159. if (fdt_get_address(&fdt) == 0) {
  160. return false;
  161. }
  162. if (fdt == NULL) {
  163. return false;
  164. }
  165. node = dt_match_instance_by_compatible(DT_SDMMC2_COMPAT, sdmmc_base);
  166. if (node < 0) {
  167. /* No SD or eMMC device on this instance, enable HSLV */
  168. return true;
  169. }
  170. ret = get_regu_max_voltage(fdt, node, "vqmmc-supply", &regu_max_val);
  171. if ((ret < 0) || (regu_max_val > 1800000U)) {
  172. /*
  173. * The vqmmc-supply property should always be present for eMMC.
  174. * For SD-card, if it is not, then the card only supports 3.3V.
  175. */
  176. return false;
  177. }
  178. return true;
  179. }
  180. static void enable_hslv_by_index(uint32_t index)
  181. {
  182. bool apply_hslv;
  183. assert(index < HSLV_NB_IDX);
  184. switch (index) {
  185. case HSLV_IDX_SDMMC1:
  186. apply_hslv = sdmmc_is_low_voltage(STM32MP_SDMMC1_BASE);
  187. break;
  188. case HSLV_IDX_SDMMC2:
  189. apply_hslv = sdmmc_is_low_voltage(STM32MP_SDMMC2_BASE);
  190. break;
  191. default:
  192. apply_hslv = true;
  193. break;
  194. }
  195. if (apply_hslv) {
  196. uint32_t reg_offset = index * sizeof(uint32_t);
  197. mmio_write_32(SYSCFG_BASE + SYSCFG_HSLVEN0R + reg_offset, HSLV_KEY);
  198. }
  199. }
  200. #endif
  201. static void enable_high_speed_mode_low_voltage(void)
  202. {
  203. #if STM32MP13
  204. uint32_t idx;
  205. for (idx = 0U; idx < HSLV_NB_IDX; idx++) {
  206. enable_hslv_by_index(idx);
  207. }
  208. #endif
  209. #if STM32MP15
  210. mmio_write_32(SYSCFG_BASE + SYSCFG_IOCTRLSETR,
  211. SYSCFG_IOCTRLSETR_HSLVEN_TRACE |
  212. SYSCFG_IOCTRLSETR_HSLVEN_QUADSPI |
  213. SYSCFG_IOCTRLSETR_HSLVEN_ETH |
  214. SYSCFG_IOCTRLSETR_HSLVEN_SDMMC |
  215. SYSCFG_IOCTRLSETR_HSLVEN_SPI);
  216. #endif
  217. }
  218. static void stm32mp_syscfg_set_hslv(void)
  219. {
  220. uint32_t otp_value;
  221. uint32_t vdd_voltage;
  222. bool product_below_2v5;
  223. /*
  224. * High Speed Low Voltage Pad mode Enable for SPI, SDMMC, ETH, QSPI
  225. * and TRACE. Needed above ~50MHz and conditioned by AFMUX selection.
  226. * It could be disabled for low frequencies or if AFMUX is selected
  227. * but the function is not used, typically for TRACE.
  228. * If high speed low voltage pad mode is node enable, platform will
  229. * over consume.
  230. *
  231. * WARNING:
  232. * Enabling High Speed mode while VDD > 2.7V
  233. * with the OTP product_below_2v5 (OTP 18, BIT 13)
  234. * erroneously set to 1 can damage the SoC!
  235. * => TF-A enables the low power mode only if VDD < 2.7V (in DT)
  236. * but this value needs to be consistent with board design.
  237. */
  238. if (stm32_get_otp_value(HW2_OTP, &otp_value) != 0) {
  239. panic();
  240. }
  241. product_below_2v5 = (otp_value & HW2_OTP_PRODUCT_BELOW_2V5) != 0U;
  242. /* Get VDD supply */
  243. vdd_voltage = dt_get_pwr_vdd_voltage();
  244. /* Check if VDD is Low Voltage */
  245. if (vdd_voltage == 0U) {
  246. WARN("VDD unknown\n");
  247. } else if (vdd_voltage < 2700000U) {
  248. enable_high_speed_mode_low_voltage();
  249. if (!product_below_2v5) {
  250. INFO("Product_below_2v5=0: HSLVEN protected by HW\n");
  251. }
  252. } else {
  253. if (product_below_2v5) {
  254. ERROR("Product_below_2v5=1:\n");
  255. ERROR("\tHSLVEN update is destructive,\n");
  256. ERROR("\tno update as VDD > 2.7V\n");
  257. panic();
  258. }
  259. }
  260. }
  261. void stm32mp_syscfg_init(void)
  262. {
  263. #if STM32MP15
  264. uint32_t bootr;
  265. /*
  266. * Interconnect update : select master using the port 1.
  267. * LTDC = AXI_M9.
  268. */
  269. mmio_write_32(SYSCFG_BASE + SYSCFG_ICNR, SYSCFG_ICNR_AXI_M9);
  270. /* Disable Pull-Down for boot pin connected to VDD */
  271. bootr = mmio_read_32(SYSCFG_BASE + SYSCFG_BOOTR) &
  272. SYSCFG_BOOTR_BOOT_MASK;
  273. mmio_clrsetbits_32(SYSCFG_BASE + SYSCFG_BOOTR, SYSCFG_BOOTR_BOOTPD_MASK,
  274. bootr << SYSCFG_BOOTR_BOOTPD_SHIFT);
  275. #endif
  276. stm32mp_syscfg_set_hslv();
  277. stm32mp_syscfg_enable_io_compensation_start();
  278. }
  279. void stm32mp_syscfg_enable_io_compensation_start(void)
  280. {
  281. /*
  282. * Activate automatic I/O compensation.
  283. * Warning: need to ensure CSI enabled and ready in clock driver.
  284. * Enable non-secure clock, we assume non-secure is suspended.
  285. */
  286. clk_enable(SYSCFG);
  287. mmio_setbits_32(SYSCFG_BASE + CMPCR_CMPENSETR_OFFSET + SYSCFG_CMPCR,
  288. SYSCFG_CMPENSETR_MPU_EN);
  289. #if STM32MP13
  290. mmio_setbits_32(SYSCFG_BASE + CMPCR_CMPENSETR_OFFSET + SYSCFG_CMPSD1CR,
  291. SYSCFG_CMPENSETR_MPU_EN);
  292. mmio_setbits_32(SYSCFG_BASE + CMPCR_CMPENSETR_OFFSET + SYSCFG_CMPSD2CR,
  293. SYSCFG_CMPENSETR_MPU_EN);
  294. #endif
  295. }
  296. void stm32mp_syscfg_enable_io_compensation_finish(void)
  297. {
  298. enable_io_comp_cell_finish(SYSCFG_CMPCR);
  299. #if STM32MP13
  300. enable_io_comp_cell_finish(SYSCFG_CMPSD1CR);
  301. enable_io_comp_cell_finish(SYSCFG_CMPSD2CR);
  302. #endif
  303. }
  304. void stm32mp_syscfg_disable_io_compensation(void)
  305. {
  306. clk_enable(SYSCFG);
  307. /*
  308. * Deactivate automatic I/O compensation.
  309. * Warning: CSI is disabled automatically in STOP if not
  310. * requested for other usages and always OFF in STANDBY.
  311. * Disable non-secure SYSCFG clock, we assume non-secure is suspended.
  312. */
  313. disable_io_comp_cell(SYSCFG_CMPCR);
  314. #if STM32MP13
  315. disable_io_comp_cell(SYSCFG_CMPSD1CR);
  316. disable_io_comp_cell(SYSCFG_CMPSD2CR);
  317. #endif
  318. clk_disable(SYSCFG);
  319. }
  320. /*
  321. * @brief Get silicon revision from SYSCFG registers.
  322. * @retval chip version (REV_ID).
  323. */
  324. uint32_t stm32mp_syscfg_get_chip_version(void)
  325. {
  326. return (mmio_read_32(SYSCFG_BASE + SYSCFG_IDC) &
  327. SYSCFG_IDC_REV_ID_MASK) >> SYSCFG_IDC_REV_ID_SHIFT;
  328. }
  329. /*
  330. * @brief Get device ID from SYSCFG registers.
  331. * @retval device ID (DEV_ID).
  332. */
  333. uint32_t stm32mp_syscfg_get_chip_dev_id(void)
  334. {
  335. return mmio_read_32(SYSCFG_BASE + SYSCFG_IDC) & SYSCFG_IDC_DEV_ID_MASK;
  336. }
  337. #if STM32MP13
  338. void stm32mp_syscfg_boot_mode_enable(void)
  339. {
  340. mmio_setbits_32(SYSCFG_BASE + SYSCFG_BOOTCR, SYSCFG_BOOTCR_BMEN);
  341. }
  342. void stm32mp_syscfg_boot_mode_disable(void)
  343. {
  344. mmio_clrbits_32(SYSCFG_BASE + SYSCFG_BOOTCR, SYSCFG_BOOTCR_BMEN);
  345. }
  346. #endif