cpu.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /*
  2. * Copyright (C) 2018, STMicroelectronics - All Rights Reserved
  3. *
  4. * SPDX-License-Identifier: GPL-2.0+ BSD-3-Clause
  5. */
  6. #include <common.h>
  7. #include <clk.h>
  8. #include <asm/io.h>
  9. #include <asm/arch/stm32.h>
  10. #include <asm/arch/sys_proto.h>
  11. #include <dm/uclass.h>
  12. /* RCC register */
  13. #define RCC_TZCR (STM32_RCC_BASE + 0x00)
  14. #define RCC_DBGCFGR (STM32_RCC_BASE + 0x080C)
  15. #define RCC_BDCR (STM32_RCC_BASE + 0x0140)
  16. #define RCC_MP_APB5ENSETR (STM32_RCC_BASE + 0x0208)
  17. #define RCC_BDCR_VSWRST BIT(31)
  18. #define RCC_BDCR_RTCSRC GENMASK(17, 16)
  19. #define RCC_DBGCFGR_DBGCKEN BIT(8)
  20. /* Security register */
  21. #define ETZPC_TZMA1_SIZE (STM32_ETZPC_BASE + 0x04)
  22. #define ETZPC_DECPROT0 (STM32_ETZPC_BASE + 0x10)
  23. #define TZC_GATE_KEEPER (STM32_TZC_BASE + 0x008)
  24. #define TZC_REGION_ATTRIBUTE0 (STM32_TZC_BASE + 0x110)
  25. #define TZC_REGION_ID_ACCESS0 (STM32_TZC_BASE + 0x114)
  26. #define TAMP_CR1 (STM32_TAMP_BASE + 0x00)
  27. #define PWR_CR1 (STM32_PWR_BASE + 0x00)
  28. #define PWR_CR1_DBP BIT(8)
  29. /* DBGMCU register */
  30. #define DBGMCU_IDC (STM32_DBGMCU_BASE + 0x00)
  31. #define DBGMCU_APB4FZ1 (STM32_DBGMCU_BASE + 0x2C)
  32. #define DBGMCU_APB4FZ1_IWDG2 BIT(2)
  33. #define DBGMCU_IDC_DEV_ID_MASK GENMASK(11, 0)
  34. #define DBGMCU_IDC_DEV_ID_SHIFT 0
  35. #define DBGMCU_IDC_REV_ID_MASK GENMASK(31, 16)
  36. #define DBGMCU_IDC_REV_ID_SHIFT 16
  37. /* boot interface from Bootrom
  38. * - boot instance = bit 31:16
  39. * - boot device = bit 15:0
  40. */
  41. #define BOOTROM_PARAM_ADDR 0x2FFC0078
  42. #define BOOTROM_MODE_MASK GENMASK(15, 0)
  43. #define BOOTROM_MODE_SHIFT 0
  44. #define BOOTROM_INSTANCE_MASK GENMASK(31, 16)
  45. #define BOOTROM_INSTANCE_SHIFT 16
  46. #if !defined(CONFIG_SPL) || defined(CONFIG_SPL_BUILD)
  47. static void security_init(void)
  48. {
  49. /* Disable the backup domain write protection */
  50. /* the protection is enable at each reset by hardware */
  51. /* And must be disable by software */
  52. setbits_le32(PWR_CR1, PWR_CR1_DBP);
  53. while (!(readl(PWR_CR1) & PWR_CR1_DBP))
  54. ;
  55. /* If RTC clock isn't enable so this is a cold boot then we need
  56. * to reset the backup domain
  57. */
  58. if (!(readl(RCC_BDCR) & RCC_BDCR_RTCSRC)) {
  59. setbits_le32(RCC_BDCR, RCC_BDCR_VSWRST);
  60. while (!(readl(RCC_BDCR) & RCC_BDCR_VSWRST))
  61. ;
  62. clrbits_le32(RCC_BDCR, RCC_BDCR_VSWRST);
  63. }
  64. /* allow non secure access in Write/Read for all peripheral */
  65. writel(GENMASK(25, 0), ETZPC_DECPROT0);
  66. /* Open SYSRAM for no secure access */
  67. writel(0x0, ETZPC_TZMA1_SIZE);
  68. /* enable TZC1 TZC2 clock */
  69. writel(BIT(11) | BIT(12), RCC_MP_APB5ENSETR);
  70. /* Region 0 set to no access by default */
  71. /* bit 0 / 16 => nsaid0 read/write Enable
  72. * bit 1 / 17 => nsaid1 read/write Enable
  73. * ...
  74. * bit 15 / 31 => nsaid15 read/write Enable
  75. */
  76. writel(0xFFFFFFFF, TZC_REGION_ID_ACCESS0);
  77. /* bit 30 / 31 => Secure Global Enable : write/read */
  78. /* bit 0 / 1 => Region Enable for filter 0/1 */
  79. writel(BIT(0) | BIT(1) | BIT(30) | BIT(31), TZC_REGION_ATTRIBUTE0);
  80. /* Enable Filter 0 and 1 */
  81. setbits_le32(TZC_GATE_KEEPER, BIT(0) | BIT(1));
  82. /* RCC trust zone deactivated */
  83. writel(0x0, RCC_TZCR);
  84. /* TAMP: deactivate the internal tamper
  85. * Bit 23 ITAMP8E: monotonic counter overflow
  86. * Bit 20 ITAMP5E: RTC calendar overflow
  87. * Bit 19 ITAMP4E: HSE monitoring
  88. * Bit 18 ITAMP3E: LSE monitoring
  89. * Bit 16 ITAMP1E: RTC power domain supply monitoring
  90. */
  91. writel(0x0, TAMP_CR1);
  92. }
  93. /*
  94. * Debug init
  95. */
  96. static void dbgmcu_init(void)
  97. {
  98. setbits_le32(RCC_DBGCFGR, RCC_DBGCFGR_DBGCKEN);
  99. /* Freeze IWDG2 if Cortex-A7 is in debug mode */
  100. setbits_le32(DBGMCU_APB4FZ1, DBGMCU_APB4FZ1_IWDG2);
  101. }
  102. #endif /* !defined(CONFIG_SPL) || defined(CONFIG_SPL_BUILD) */
  103. static u32 get_bootmode(void)
  104. {
  105. u32 boot_mode;
  106. #if !defined(CONFIG_SPL) || defined(CONFIG_SPL_BUILD)
  107. u32 bootrom_itf = readl(BOOTROM_PARAM_ADDR);
  108. u32 bootrom_device, bootrom_instance;
  109. bootrom_device =
  110. (bootrom_itf & BOOTROM_MODE_MASK) >> BOOTROM_MODE_SHIFT;
  111. bootrom_instance =
  112. (bootrom_itf & BOOTROM_INSTANCE_MASK) >> BOOTROM_INSTANCE_SHIFT;
  113. boot_mode =
  114. ((bootrom_device << BOOT_TYPE_SHIFT) & BOOT_TYPE_MASK) |
  115. ((bootrom_instance << BOOT_INSTANCE_SHIFT) &
  116. BOOT_INSTANCE_MASK);
  117. /* save the boot mode in TAMP backup register */
  118. clrsetbits_le32(TAMP_BOOT_CONTEXT,
  119. TAMP_BOOT_MODE_MASK,
  120. boot_mode << TAMP_BOOT_MODE_SHIFT);
  121. #else
  122. /* read TAMP backup register */
  123. boot_mode = (readl(TAMP_BOOT_CONTEXT) & TAMP_BOOT_MODE_MASK) >>
  124. TAMP_BOOT_MODE_SHIFT;
  125. #endif
  126. return boot_mode;
  127. }
  128. /*
  129. * Early system init
  130. */
  131. int arch_cpu_init(void)
  132. {
  133. /* early armv7 timer init: needed for polling */
  134. timer_init();
  135. #if !defined(CONFIG_SPL) || defined(CONFIG_SPL_BUILD)
  136. dbgmcu_init();
  137. security_init();
  138. #endif
  139. /* get bootmode from BootRom context: saved in TAMP register */
  140. get_bootmode();
  141. return 0;
  142. }
  143. void enable_caches(void)
  144. {
  145. /* Enable D-cache. I-cache is already enabled in start.S */
  146. dcache_enable();
  147. }
  148. static u32 read_idc(void)
  149. {
  150. setbits_le32(RCC_DBGCFGR, RCC_DBGCFGR_DBGCKEN);
  151. return readl(DBGMCU_IDC);
  152. }
  153. u32 get_cpu_rev(void)
  154. {
  155. return (read_idc() & DBGMCU_IDC_REV_ID_MASK) >> DBGMCU_IDC_REV_ID_SHIFT;
  156. }
  157. u32 get_cpu_type(void)
  158. {
  159. return (read_idc() & DBGMCU_IDC_DEV_ID_MASK) >> DBGMCU_IDC_DEV_ID_SHIFT;
  160. }
  161. #if defined(CONFIG_DISPLAY_CPUINFO)
  162. int print_cpuinfo(void)
  163. {
  164. char *cpu_s, *cpu_r;
  165. switch (get_cpu_type()) {
  166. case CPU_STMP32MP15x:
  167. cpu_s = "15x";
  168. break;
  169. default:
  170. cpu_s = "?";
  171. break;
  172. }
  173. switch (get_cpu_rev()) {
  174. case CPU_REVA:
  175. cpu_r = "A";
  176. break;
  177. case CPU_REVB:
  178. cpu_r = "B";
  179. break;
  180. default:
  181. cpu_r = "?";
  182. break;
  183. }
  184. printf("CPU: STM32MP%s.%s\n", cpu_s, cpu_r);
  185. return 0;
  186. }
  187. #endif /* CONFIG_DISPLAY_CPUINFO */
  188. static void setup_boot_mode(void)
  189. {
  190. char cmd[60];
  191. u32 boot_ctx = readl(TAMP_BOOT_CONTEXT);
  192. u32 boot_mode =
  193. (boot_ctx & TAMP_BOOT_MODE_MASK) >> TAMP_BOOT_MODE_SHIFT;
  194. int instance = (boot_mode & TAMP_BOOT_INSTANCE_MASK) - 1;
  195. pr_debug("%s: boot_ctx=0x%x => boot_mode=%x, instance=%d\n",
  196. __func__, boot_ctx, boot_mode, instance);
  197. switch (boot_mode & TAMP_BOOT_DEVICE_MASK) {
  198. case BOOT_SERIAL_UART:
  199. sprintf(cmd, "%d", instance);
  200. env_set("boot_device", "uart");
  201. env_set("boot_instance", cmd);
  202. break;
  203. case BOOT_SERIAL_USB:
  204. env_set("boot_device", "usb");
  205. env_set("boot_instance", "0");
  206. break;
  207. case BOOT_FLASH_SD:
  208. case BOOT_FLASH_EMMC:
  209. sprintf(cmd, "%d", instance);
  210. env_set("boot_device", "mmc");
  211. env_set("boot_instance", cmd);
  212. break;
  213. case BOOT_FLASH_NAND:
  214. env_set("boot_device", "nand");
  215. env_set("boot_instance", "0");
  216. break;
  217. case BOOT_FLASH_NOR:
  218. env_set("boot_device", "nor");
  219. env_set("boot_instance", "0");
  220. break;
  221. default:
  222. pr_debug("unexpected boot mode = %x\n", boot_mode);
  223. break;
  224. }
  225. }
  226. int arch_misc_init(void)
  227. {
  228. setup_boot_mode();
  229. return 0;
  230. }
  231. void reset_cpu(ulong addr)
  232. {
  233. }