stm32mp1_private.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683
  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 <drivers/clk.h>
  8. #include <drivers/st/stm32_gpio.h>
  9. #include <drivers/st/stm32_iwdg.h>
  10. #include <lib/mmio.h>
  11. #include <lib/xlat_tables/xlat_tables_v2.h>
  12. #include <libfdt.h>
  13. #include <plat/common/platform.h>
  14. #include <platform_def.h>
  15. #if STM32MP13
  16. #define TAMP_BOOT_MODE_BACKUP_REG_ID U(30)
  17. #endif
  18. #if STM32MP15
  19. #define TAMP_BOOT_MODE_BACKUP_REG_ID U(20)
  20. #endif
  21. /*
  22. * Backup register to store fwu update information.
  23. * It should be writeable only by secure world, but also readable by non secure
  24. * (so it should be in Zone 2).
  25. */
  26. #define TAMP_BOOT_FWU_INFO_REG_ID U(10)
  27. #if defined(IMAGE_BL2)
  28. #define MAP_SEC_SYSRAM MAP_REGION_FLAT(STM32MP_SYSRAM_BASE, \
  29. STM32MP_SYSRAM_SIZE, \
  30. MT_MEMORY | \
  31. MT_RW | \
  32. MT_SECURE | \
  33. MT_EXECUTE_NEVER)
  34. #elif defined(IMAGE_BL32)
  35. #define MAP_SEC_SYSRAM MAP_REGION_FLAT(STM32MP_SEC_SYSRAM_BASE, \
  36. STM32MP_SEC_SYSRAM_SIZE, \
  37. MT_MEMORY | \
  38. MT_RW | \
  39. MT_SECURE | \
  40. MT_EXECUTE_NEVER)
  41. /* Non-secure SYSRAM is used a uncached memory for SCMI message transfer */
  42. #define MAP_NS_SYSRAM MAP_REGION_FLAT(STM32MP_NS_SYSRAM_BASE, \
  43. STM32MP_NS_SYSRAM_SIZE, \
  44. MT_DEVICE | \
  45. MT_RW | \
  46. MT_NS | \
  47. MT_EXECUTE_NEVER)
  48. #endif
  49. #if STM32MP13
  50. #define MAP_SRAM_ALL MAP_REGION_FLAT(SRAMS_BASE, \
  51. SRAMS_SIZE_2MB_ALIGNED, \
  52. MT_MEMORY | \
  53. MT_RW | \
  54. MT_SECURE | \
  55. MT_EXECUTE_NEVER)
  56. #endif
  57. #define MAP_DEVICE1 MAP_REGION_FLAT(STM32MP1_DEVICE1_BASE, \
  58. STM32MP1_DEVICE1_SIZE, \
  59. MT_DEVICE | \
  60. MT_RW | \
  61. MT_SECURE | \
  62. MT_EXECUTE_NEVER)
  63. #define MAP_DEVICE2 MAP_REGION_FLAT(STM32MP1_DEVICE2_BASE, \
  64. STM32MP1_DEVICE2_SIZE, \
  65. MT_DEVICE | \
  66. MT_RW | \
  67. MT_SECURE | \
  68. MT_EXECUTE_NEVER)
  69. #if defined(IMAGE_BL2)
  70. static const mmap_region_t stm32mp1_mmap[] = {
  71. MAP_SEC_SYSRAM,
  72. #if STM32MP13
  73. MAP_SRAM_ALL,
  74. #endif
  75. MAP_DEVICE1,
  76. #if STM32MP_RAW_NAND
  77. MAP_DEVICE2,
  78. #endif
  79. {0}
  80. };
  81. #endif
  82. #if defined(IMAGE_BL32)
  83. static const mmap_region_t stm32mp1_mmap[] = {
  84. MAP_SEC_SYSRAM,
  85. MAP_NS_SYSRAM,
  86. MAP_DEVICE1,
  87. MAP_DEVICE2,
  88. {0}
  89. };
  90. #endif
  91. void configure_mmu(void)
  92. {
  93. mmap_add(stm32mp1_mmap);
  94. init_xlat_tables();
  95. enable_mmu_svc_mon(0);
  96. }
  97. uintptr_t stm32_get_gpio_bank_base(unsigned int bank)
  98. {
  99. #if STM32MP13
  100. assert(bank <= GPIO_BANK_I);
  101. #endif
  102. #if STM32MP15
  103. if (bank == GPIO_BANK_Z) {
  104. return GPIOZ_BASE;
  105. }
  106. assert(bank <= GPIO_BANK_K);
  107. #endif
  108. return GPIOA_BASE + (bank * GPIO_BANK_OFFSET);
  109. }
  110. uint32_t stm32_get_gpio_bank_offset(unsigned int bank)
  111. {
  112. #if STM32MP13
  113. assert(bank <= GPIO_BANK_I);
  114. #endif
  115. #if STM32MP15
  116. if (bank == GPIO_BANK_Z) {
  117. return 0;
  118. }
  119. assert(bank <= GPIO_BANK_K);
  120. #endif
  121. return bank * GPIO_BANK_OFFSET;
  122. }
  123. bool stm32_gpio_is_secure_at_reset(unsigned int bank)
  124. {
  125. #if STM32MP13
  126. return true;
  127. #endif
  128. #if STM32MP15
  129. if (bank == GPIO_BANK_Z) {
  130. return true;
  131. }
  132. return false;
  133. #endif
  134. }
  135. unsigned long stm32_get_gpio_bank_clock(unsigned int bank)
  136. {
  137. #if STM32MP13
  138. assert(bank <= GPIO_BANK_I);
  139. #endif
  140. #if STM32MP15
  141. if (bank == GPIO_BANK_Z) {
  142. return GPIOZ;
  143. }
  144. assert(bank <= GPIO_BANK_K);
  145. #endif
  146. return GPIOA + (bank - GPIO_BANK_A);
  147. }
  148. int stm32_get_gpio_bank_pinctrl_node(void *fdt, unsigned int bank)
  149. {
  150. const char *node_compatible = NULL;
  151. switch (bank) {
  152. case GPIO_BANK_A:
  153. case GPIO_BANK_B:
  154. case GPIO_BANK_C:
  155. case GPIO_BANK_D:
  156. case GPIO_BANK_E:
  157. case GPIO_BANK_F:
  158. case GPIO_BANK_G:
  159. case GPIO_BANK_H:
  160. case GPIO_BANK_I:
  161. #if STM32MP13
  162. node_compatible = "st,stm32mp135-pinctrl";
  163. break;
  164. #endif
  165. #if STM32MP15
  166. case GPIO_BANK_J:
  167. case GPIO_BANK_K:
  168. node_compatible = "st,stm32mp157-pinctrl";
  169. break;
  170. case GPIO_BANK_Z:
  171. node_compatible = "st,stm32mp157-z-pinctrl";
  172. break;
  173. #endif
  174. default:
  175. panic();
  176. }
  177. return fdt_node_offset_by_compatible(fdt, -1, node_compatible);
  178. }
  179. #if STM32MP_UART_PROGRAMMER || !defined(IMAGE_BL2)
  180. /*
  181. * UART Management
  182. */
  183. static const uintptr_t stm32mp1_uart_addresses[8] = {
  184. USART1_BASE,
  185. USART2_BASE,
  186. USART3_BASE,
  187. UART4_BASE,
  188. UART5_BASE,
  189. USART6_BASE,
  190. UART7_BASE,
  191. UART8_BASE,
  192. };
  193. uintptr_t get_uart_address(uint32_t instance_nb)
  194. {
  195. if ((instance_nb == 0U) ||
  196. (instance_nb > ARRAY_SIZE(stm32mp1_uart_addresses))) {
  197. return 0U;
  198. }
  199. return stm32mp1_uart_addresses[instance_nb - 1U];
  200. }
  201. #endif
  202. #if STM32MP_USB_PROGRAMMER
  203. struct gpio_bank_pin_list {
  204. uint32_t bank;
  205. uint32_t pin;
  206. };
  207. static const struct gpio_bank_pin_list gpio_list[] = {
  208. { /* USART2_RX: GPIOA3 */
  209. .bank = 0U,
  210. .pin = 3U,
  211. },
  212. { /* USART3_RX: GPIOB12 */
  213. .bank = 1U,
  214. .pin = 12U,
  215. },
  216. { /* UART4_RX: GPIOB2 */
  217. .bank = 1U,
  218. .pin = 2U,
  219. },
  220. { /* UART5_RX: GPIOB4 */
  221. .bank = 1U,
  222. .pin = 5U,
  223. },
  224. { /* USART6_RX: GPIOC7 */
  225. .bank = 2U,
  226. .pin = 7U,
  227. },
  228. { /* UART7_RX: GPIOF6 */
  229. .bank = 5U,
  230. .pin = 6U,
  231. },
  232. { /* UART8_RX: GPIOE0 */
  233. .bank = 4U,
  234. .pin = 0U,
  235. },
  236. };
  237. void stm32mp1_deconfigure_uart_pins(void)
  238. {
  239. size_t i;
  240. for (i = 0U; i < ARRAY_SIZE(gpio_list); i++) {
  241. set_gpio_reset_cfg(gpio_list[i].bank, gpio_list[i].pin);
  242. }
  243. }
  244. #endif
  245. uint32_t stm32mp_get_chip_version(void)
  246. {
  247. #if STM32MP13
  248. return stm32mp_syscfg_get_chip_version();
  249. #endif
  250. #if STM32MP15
  251. uint32_t version = 0U;
  252. if (stm32mp1_dbgmcu_get_chip_version(&version) < 0) {
  253. INFO("Cannot get CPU version, debug disabled\n");
  254. return 0U;
  255. }
  256. return version;
  257. #endif
  258. }
  259. uint32_t stm32mp_get_chip_dev_id(void)
  260. {
  261. #if STM32MP13
  262. return stm32mp_syscfg_get_chip_dev_id();
  263. #endif
  264. #if STM32MP15
  265. uint32_t dev_id;
  266. if (stm32mp1_dbgmcu_get_chip_dev_id(&dev_id) < 0) {
  267. INFO("Use default chip ID, debug disabled\n");
  268. dev_id = STM32MP1_CHIP_ID;
  269. }
  270. return dev_id;
  271. #endif
  272. }
  273. static uint32_t get_part_number(void)
  274. {
  275. static uint32_t part_number;
  276. if (part_number != 0U) {
  277. return part_number;
  278. }
  279. if (stm32_get_otp_value(PART_NUMBER_OTP, &part_number) != 0) {
  280. panic();
  281. }
  282. part_number = (part_number & PART_NUMBER_OTP_PART_MASK) >>
  283. PART_NUMBER_OTP_PART_SHIFT;
  284. part_number |= stm32mp_get_chip_dev_id() << 16;
  285. return part_number;
  286. }
  287. #if STM32MP15
  288. static uint32_t get_cpu_package(void)
  289. {
  290. uint32_t package;
  291. if (stm32_get_otp_value(PACKAGE_OTP, &package) != 0) {
  292. panic();
  293. }
  294. package = (package & PACKAGE_OTP_PKG_MASK) >>
  295. PACKAGE_OTP_PKG_SHIFT;
  296. return package;
  297. }
  298. #endif
  299. void stm32mp_get_soc_name(char name[STM32_SOC_NAME_SIZE])
  300. {
  301. const char *cpu_s, *cpu_r, *pkg;
  302. /* MPUs Part Numbers */
  303. switch (get_part_number()) {
  304. #if STM32MP13
  305. case STM32MP135F_PART_NB:
  306. cpu_s = "135F";
  307. break;
  308. case STM32MP135D_PART_NB:
  309. cpu_s = "135D";
  310. break;
  311. case STM32MP135C_PART_NB:
  312. cpu_s = "135C";
  313. break;
  314. case STM32MP135A_PART_NB:
  315. cpu_s = "135A";
  316. break;
  317. case STM32MP133F_PART_NB:
  318. cpu_s = "133F";
  319. break;
  320. case STM32MP133D_PART_NB:
  321. cpu_s = "133D";
  322. break;
  323. case STM32MP133C_PART_NB:
  324. cpu_s = "133C";
  325. break;
  326. case STM32MP133A_PART_NB:
  327. cpu_s = "133A";
  328. break;
  329. case STM32MP131F_PART_NB:
  330. cpu_s = "131F";
  331. break;
  332. case STM32MP131D_PART_NB:
  333. cpu_s = "131D";
  334. break;
  335. case STM32MP131C_PART_NB:
  336. cpu_s = "131C";
  337. break;
  338. case STM32MP131A_PART_NB:
  339. cpu_s = "131A";
  340. break;
  341. #endif
  342. #if STM32MP15
  343. case STM32MP157C_PART_NB:
  344. cpu_s = "157C";
  345. break;
  346. case STM32MP157A_PART_NB:
  347. cpu_s = "157A";
  348. break;
  349. case STM32MP153C_PART_NB:
  350. cpu_s = "153C";
  351. break;
  352. case STM32MP153A_PART_NB:
  353. cpu_s = "153A";
  354. break;
  355. case STM32MP151C_PART_NB:
  356. cpu_s = "151C";
  357. break;
  358. case STM32MP151A_PART_NB:
  359. cpu_s = "151A";
  360. break;
  361. case STM32MP157F_PART_NB:
  362. cpu_s = "157F";
  363. break;
  364. case STM32MP157D_PART_NB:
  365. cpu_s = "157D";
  366. break;
  367. case STM32MP153F_PART_NB:
  368. cpu_s = "153F";
  369. break;
  370. case STM32MP153D_PART_NB:
  371. cpu_s = "153D";
  372. break;
  373. case STM32MP151F_PART_NB:
  374. cpu_s = "151F";
  375. break;
  376. case STM32MP151D_PART_NB:
  377. cpu_s = "151D";
  378. break;
  379. #endif
  380. default:
  381. cpu_s = "????";
  382. break;
  383. }
  384. /* Package */
  385. #if STM32MP13
  386. /* On STM32MP13, package is not present in OTP */
  387. pkg = "";
  388. #endif
  389. #if STM32MP15
  390. switch (get_cpu_package()) {
  391. case PKG_AA_LFBGA448:
  392. pkg = "AA";
  393. break;
  394. case PKG_AB_LFBGA354:
  395. pkg = "AB";
  396. break;
  397. case PKG_AC_TFBGA361:
  398. pkg = "AC";
  399. break;
  400. case PKG_AD_TFBGA257:
  401. pkg = "AD";
  402. break;
  403. default:
  404. pkg = "??";
  405. break;
  406. }
  407. #endif
  408. /* REVISION */
  409. switch (stm32mp_get_chip_version()) {
  410. case STM32MP1_REV_B:
  411. cpu_r = "B";
  412. break;
  413. #if STM32MP13
  414. case STM32MP1_REV_Y:
  415. cpu_r = "Y";
  416. break;
  417. #endif
  418. case STM32MP1_REV_Z:
  419. cpu_r = "Z";
  420. break;
  421. default:
  422. cpu_r = "?";
  423. break;
  424. }
  425. snprintf(name, STM32_SOC_NAME_SIZE,
  426. "STM32MP%s%s Rev.%s", cpu_s, pkg, cpu_r);
  427. }
  428. void stm32mp_print_cpuinfo(void)
  429. {
  430. char name[STM32_SOC_NAME_SIZE];
  431. stm32mp_get_soc_name(name);
  432. NOTICE("CPU: %s\n", name);
  433. }
  434. void stm32mp_print_boardinfo(void)
  435. {
  436. uint32_t board_id = 0U;
  437. if (stm32_get_otp_value(BOARD_ID_OTP, &board_id) != 0) {
  438. return;
  439. }
  440. if (board_id != 0U) {
  441. stm32_display_board_info(board_id);
  442. }
  443. }
  444. /* Return true when SoC provides a single Cortex-A7 core, and false otherwise */
  445. bool stm32mp_is_single_core(void)
  446. {
  447. #if STM32MP13
  448. return true;
  449. #endif
  450. #if STM32MP15
  451. bool single_core = false;
  452. switch (get_part_number()) {
  453. case STM32MP151A_PART_NB:
  454. case STM32MP151C_PART_NB:
  455. case STM32MP151D_PART_NB:
  456. case STM32MP151F_PART_NB:
  457. single_core = true;
  458. break;
  459. default:
  460. break;
  461. }
  462. return single_core;
  463. #endif
  464. }
  465. /* Return true when device is in closed state */
  466. uint32_t stm32mp_check_closed_device(void)
  467. {
  468. uint32_t value;
  469. if (stm32_get_otp_value(CFG0_OTP, &value) != 0) {
  470. return STM32MP_CHIP_SEC_CLOSED;
  471. }
  472. #if STM32MP13
  473. value = (value & CFG0_OTP_MODE_MASK) >> CFG0_OTP_MODE_SHIFT;
  474. switch (value) {
  475. case CFG0_OPEN_DEVICE:
  476. return STM32MP_CHIP_SEC_OPEN;
  477. case CFG0_CLOSED_DEVICE:
  478. case CFG0_CLOSED_DEVICE_NO_BOUNDARY_SCAN:
  479. case CFG0_CLOSED_DEVICE_NO_JTAG:
  480. return STM32MP_CHIP_SEC_CLOSED;
  481. default:
  482. panic();
  483. }
  484. #endif
  485. #if STM32MP15
  486. if ((value & CFG0_CLOSED_DEVICE) == CFG0_CLOSED_DEVICE) {
  487. return STM32MP_CHIP_SEC_CLOSED;
  488. } else {
  489. return STM32MP_CHIP_SEC_OPEN;
  490. }
  491. #endif
  492. }
  493. /* Return true when device supports secure boot */
  494. bool stm32mp_is_auth_supported(void)
  495. {
  496. bool supported = false;
  497. switch (get_part_number()) {
  498. #if STM32MP13
  499. case STM32MP131C_PART_NB:
  500. case STM32MP131F_PART_NB:
  501. case STM32MP133C_PART_NB:
  502. case STM32MP133F_PART_NB:
  503. case STM32MP135C_PART_NB:
  504. case STM32MP135F_PART_NB:
  505. #endif
  506. #if STM32MP15
  507. case STM32MP151C_PART_NB:
  508. case STM32MP151F_PART_NB:
  509. case STM32MP153C_PART_NB:
  510. case STM32MP153F_PART_NB:
  511. case STM32MP157C_PART_NB:
  512. case STM32MP157F_PART_NB:
  513. #endif
  514. supported = true;
  515. break;
  516. default:
  517. break;
  518. }
  519. return supported;
  520. }
  521. uint32_t stm32_iwdg_get_instance(uintptr_t base)
  522. {
  523. switch (base) {
  524. case IWDG1_BASE:
  525. return IWDG1_INST;
  526. case IWDG2_BASE:
  527. return IWDG2_INST;
  528. default:
  529. panic();
  530. }
  531. }
  532. uint32_t stm32_iwdg_get_otp_config(uint32_t iwdg_inst)
  533. {
  534. uint32_t iwdg_cfg = 0U;
  535. uint32_t otp_value;
  536. if (stm32_get_otp_value(HW2_OTP, &otp_value) != 0) {
  537. panic();
  538. }
  539. if ((otp_value & BIT(iwdg_inst + HW2_OTP_IWDG_HW_POS)) != 0U) {
  540. iwdg_cfg |= IWDG_HW_ENABLED;
  541. }
  542. if ((otp_value & BIT(iwdg_inst + HW2_OTP_IWDG_FZ_STOP_POS)) != 0U) {
  543. iwdg_cfg |= IWDG_DISABLE_ON_STOP;
  544. }
  545. if ((otp_value & BIT(iwdg_inst + HW2_OTP_IWDG_FZ_STANDBY_POS)) != 0U) {
  546. iwdg_cfg |= IWDG_DISABLE_ON_STANDBY;
  547. }
  548. return iwdg_cfg;
  549. }
  550. #if defined(IMAGE_BL2)
  551. uint32_t stm32_iwdg_shadow_update(uint32_t iwdg_inst, uint32_t flags)
  552. {
  553. uint32_t otp_value;
  554. uint32_t otp;
  555. uint32_t result;
  556. if (stm32_get_otp_index(HW2_OTP, &otp, NULL) != 0) {
  557. panic();
  558. }
  559. if (stm32_get_otp_value(HW2_OTP, &otp_value) != 0) {
  560. panic();
  561. }
  562. if ((flags & IWDG_DISABLE_ON_STOP) != 0) {
  563. otp_value |= BIT(iwdg_inst + HW2_OTP_IWDG_FZ_STOP_POS);
  564. }
  565. if ((flags & IWDG_DISABLE_ON_STANDBY) != 0) {
  566. otp_value |= BIT(iwdg_inst + HW2_OTP_IWDG_FZ_STANDBY_POS);
  567. }
  568. result = bsec_write_otp(otp_value, otp);
  569. if (result != BSEC_OK) {
  570. return result;
  571. }
  572. /* Sticky lock OTP_IWDG (read and write) */
  573. if ((bsec_set_sr_lock(otp) != BSEC_OK) ||
  574. (bsec_set_sw_lock(otp) != BSEC_OK)) {
  575. return BSEC_LOCK_FAIL;
  576. }
  577. return BSEC_OK;
  578. }
  579. #endif
  580. bool stm32mp_is_wakeup_from_standby(void)
  581. {
  582. /* TODO add source code to determine if platform is waking up from standby mode */
  583. return false;
  584. }
  585. uintptr_t stm32_get_bkpr_boot_mode_addr(void)
  586. {
  587. return tamp_bkpr(TAMP_BOOT_MODE_BACKUP_REG_ID);
  588. }
  589. #if PSA_FWU_SUPPORT
  590. uintptr_t stm32_get_bkpr_fwu_info_addr(void)
  591. {
  592. return tamp_bkpr(TAMP_BOOT_FWU_INFO_REG_ID);
  593. }
  594. #endif /* PSA_FWU_SUPPORT */