2 * Copyright (C) 2018, STMicroelectronics - All Rights Reserved
4 * SPDX-License-Identifier: GPL-2.0+ BSD-3-Clause
9 #include <asm/arch/stm32.h>
10 #include <asm/arch/sys_proto.h>
11 #include <dm/uclass.h>
14 #define RCC_TZCR (STM32_RCC_BASE + 0x00)
15 #define RCC_DBGCFGR (STM32_RCC_BASE + 0x080C)
16 #define RCC_BDCR (STM32_RCC_BASE + 0x0140)
17 #define RCC_MP_APB5ENSETR (STM32_RCC_BASE + 0x0208)
18 #define RCC_BDCR_VSWRST BIT(31)
19 #define RCC_BDCR_RTCSRC GENMASK(17, 16)
20 #define RCC_DBGCFGR_DBGCKEN BIT(8)
22 /* Security register */
23 #define ETZPC_TZMA1_SIZE (STM32_ETZPC_BASE + 0x04)
24 #define ETZPC_DECPROT0 (STM32_ETZPC_BASE + 0x10)
26 #define TZC_GATE_KEEPER (STM32_TZC_BASE + 0x008)
27 #define TZC_REGION_ATTRIBUTE0 (STM32_TZC_BASE + 0x110)
28 #define TZC_REGION_ID_ACCESS0 (STM32_TZC_BASE + 0x114)
30 #define TAMP_CR1 (STM32_TAMP_BASE + 0x00)
32 #define PWR_CR1 (STM32_PWR_BASE + 0x00)
33 #define PWR_CR1_DBP BIT(8)
36 #define DBGMCU_IDC (STM32_DBGMCU_BASE + 0x00)
37 #define DBGMCU_APB4FZ1 (STM32_DBGMCU_BASE + 0x2C)
38 #define DBGMCU_APB4FZ1_IWDG2 BIT(2)
39 #define DBGMCU_IDC_DEV_ID_MASK GENMASK(11, 0)
40 #define DBGMCU_IDC_DEV_ID_SHIFT 0
41 #define DBGMCU_IDC_REV_ID_MASK GENMASK(31, 16)
42 #define DBGMCU_IDC_REV_ID_SHIFT 16
44 /* boot interface from Bootrom
45 * - boot instance = bit 31:16
46 * - boot device = bit 15:0
48 #define BOOTROM_PARAM_ADDR 0x2FFC0078
49 #define BOOTROM_MODE_MASK GENMASK(15, 0)
50 #define BOOTROM_MODE_SHIFT 0
51 #define BOOTROM_INSTANCE_MASK GENMASK(31, 16)
52 #define BOOTROM_INSTANCE_SHIFT 16
54 #if !defined(CONFIG_SPL) || defined(CONFIG_SPL_BUILD)
55 static void security_init(void)
57 /* Disable the backup domain write protection */
58 /* the protection is enable at each reset by hardware */
59 /* And must be disable by software */
60 setbits_le32(PWR_CR1, PWR_CR1_DBP);
62 while (!(readl(PWR_CR1) & PWR_CR1_DBP))
65 /* If RTC clock isn't enable so this is a cold boot then we need
66 * to reset the backup domain
68 if (!(readl(RCC_BDCR) & RCC_BDCR_RTCSRC)) {
69 setbits_le32(RCC_BDCR, RCC_BDCR_VSWRST);
70 while (!(readl(RCC_BDCR) & RCC_BDCR_VSWRST))
72 clrbits_le32(RCC_BDCR, RCC_BDCR_VSWRST);
75 /* allow non secure access in Write/Read for all peripheral */
76 writel(GENMASK(25, 0), ETZPC_DECPROT0);
78 /* Open SYSRAM for no secure access */
79 writel(0x0, ETZPC_TZMA1_SIZE);
81 /* enable TZC1 TZC2 clock */
82 writel(BIT(11) | BIT(12), RCC_MP_APB5ENSETR);
84 /* Region 0 set to no access by default */
85 /* bit 0 / 16 => nsaid0 read/write Enable
86 * bit 1 / 17 => nsaid1 read/write Enable
88 * bit 15 / 31 => nsaid15 read/write Enable
90 writel(0xFFFFFFFF, TZC_REGION_ID_ACCESS0);
91 /* bit 30 / 31 => Secure Global Enable : write/read */
92 /* bit 0 / 1 => Region Enable for filter 0/1 */
93 writel(BIT(0) | BIT(1) | BIT(30) | BIT(31), TZC_REGION_ATTRIBUTE0);
95 /* Enable Filter 0 and 1 */
96 setbits_le32(TZC_GATE_KEEPER, BIT(0) | BIT(1));
98 /* RCC trust zone deactivated */
99 writel(0x0, RCC_TZCR);
101 /* TAMP: deactivate the internal tamper
102 * Bit 23 ITAMP8E: monotonic counter overflow
103 * Bit 20 ITAMP5E: RTC calendar overflow
104 * Bit 19 ITAMP4E: HSE monitoring
105 * Bit 18 ITAMP3E: LSE monitoring
106 * Bit 16 ITAMP1E: RTC power domain supply monitoring
108 writel(0x0, TAMP_CR1);
114 static void dbgmcu_init(void)
116 setbits_le32(RCC_DBGCFGR, RCC_DBGCFGR_DBGCKEN);
118 /* Freeze IWDG2 if Cortex-A7 is in debug mode */
119 setbits_le32(DBGMCU_APB4FZ1, DBGMCU_APB4FZ1_IWDG2);
121 #endif /* !defined(CONFIG_SPL) || defined(CONFIG_SPL_BUILD) */
123 static u32 get_bootmode(void)
126 #if !defined(CONFIG_SPL) || defined(CONFIG_SPL_BUILD)
127 u32 bootrom_itf = readl(BOOTROM_PARAM_ADDR);
128 u32 bootrom_device, bootrom_instance;
131 (bootrom_itf & BOOTROM_MODE_MASK) >> BOOTROM_MODE_SHIFT;
133 (bootrom_itf & BOOTROM_INSTANCE_MASK) >> BOOTROM_INSTANCE_SHIFT;
135 ((bootrom_device << BOOT_TYPE_SHIFT) & BOOT_TYPE_MASK) |
136 ((bootrom_instance << BOOT_INSTANCE_SHIFT) &
139 /* save the boot mode in TAMP backup register */
140 clrsetbits_le32(TAMP_BOOT_CONTEXT,
142 boot_mode << TAMP_BOOT_MODE_SHIFT);
144 /* read TAMP backup register */
145 boot_mode = (readl(TAMP_BOOT_CONTEXT) & TAMP_BOOT_MODE_MASK) >>
146 TAMP_BOOT_MODE_SHIFT;
154 int arch_cpu_init(void)
156 /* early armv7 timer init: needed for polling */
159 #if !defined(CONFIG_SPL) || defined(CONFIG_SPL_BUILD)
164 /* get bootmode from BootRom context: saved in TAMP register */
170 void enable_caches(void)
172 /* Enable D-cache. I-cache is already enabled in start.S */
176 static u32 read_idc(void)
178 setbits_le32(RCC_DBGCFGR, RCC_DBGCFGR_DBGCKEN);
180 return readl(DBGMCU_IDC);
183 u32 get_cpu_rev(void)
185 return (read_idc() & DBGMCU_IDC_REV_ID_MASK) >> DBGMCU_IDC_REV_ID_SHIFT;
188 u32 get_cpu_type(void)
190 return (read_idc() & DBGMCU_IDC_DEV_ID_MASK) >> DBGMCU_IDC_DEV_ID_SHIFT;
193 #if defined(CONFIG_DISPLAY_CPUINFO)
194 int print_cpuinfo(void)
198 switch (get_cpu_type()) {
199 case CPU_STMP32MP15x:
207 switch (get_cpu_rev()) {
219 printf("CPU: STM32MP%s.%s\n", cpu_s, cpu_r);
223 #endif /* CONFIG_DISPLAY_CPUINFO */
225 static void setup_boot_mode(void)
228 u32 boot_ctx = readl(TAMP_BOOT_CONTEXT);
230 (boot_ctx & TAMP_BOOT_MODE_MASK) >> TAMP_BOOT_MODE_SHIFT;
231 int instance = (boot_mode & TAMP_BOOT_INSTANCE_MASK) - 1;
233 pr_debug("%s: boot_ctx=0x%x => boot_mode=%x, instance=%d\n",
234 __func__, boot_ctx, boot_mode, instance);
236 switch (boot_mode & TAMP_BOOT_DEVICE_MASK) {
237 case BOOT_SERIAL_UART:
238 sprintf(cmd, "%d", instance);
239 env_set("boot_device", "uart");
240 env_set("boot_instance", cmd);
242 case BOOT_SERIAL_USB:
243 env_set("boot_device", "usb");
244 env_set("boot_instance", "0");
247 case BOOT_FLASH_EMMC:
248 sprintf(cmd, "%d", instance);
249 env_set("boot_device", "mmc");
250 env_set("boot_instance", cmd);
252 case BOOT_FLASH_NAND:
253 env_set("boot_device", "nand");
254 env_set("boot_instance", "0");
257 env_set("boot_device", "nor");
258 env_set("boot_instance", "0");
261 pr_debug("unexpected boot mode = %x\n", boot_mode);
266 int arch_misc_init(void)