blob: f096fe538d88d3472ef2a00f1e8822f1c9c2eb9d [file] [log] [blame]
Patrick Delaunaye4bdd542022-05-20 18:24:42 +02001// SPDX-License-Identifier: GPL-2.0-or-later OR BSD-3-Clause
2/*
3 * Copyright (C) 2021, STMicroelectronics - All Rights Reserved
4 */
5
6#define LOG_CATEGORY LOGC_ARCH
7
Patrick Delaunaye4bdd542022-05-20 18:24:42 +02008#include <env.h>
9#include <log.h>
10#include <asm/io.h>
11#include <asm/arch/bsec.h>
12#include <asm/arch/stm32.h>
13#include <asm/arch/sys_proto.h>
14#include <dm/device.h>
15#include <dm/uclass.h>
Marek Vasut852b8282024-04-19 05:59:05 +020016#include <linux/bitfield.h>
Patrick Delaunaye4bdd542022-05-20 18:24:42 +020017
18/* RCC register */
19#define RCC_TZCR (STM32_RCC_BASE + 0x00)
20#define RCC_BDCR (STM32_RCC_BASE + 0x0140)
21#define RCC_MP_APB5ENSETR (STM32_RCC_BASE + 0x0208)
22#define RCC_MP_AHB5ENSETR (STM32_RCC_BASE + 0x0210)
23#define RCC_DBGCFGR (STM32_RCC_BASE + 0x080C)
24
25#define RCC_BDCR_VSWRST BIT(31)
26#define RCC_BDCR_RTCSRC GENMASK(17, 16)
27
28#define RCC_DBGCFGR_DBGCKEN BIT(8)
29
30/* DBGMCU register */
31#define DBGMCU_IDC (STM32_DBGMCU_BASE + 0x00)
32#define DBGMCU_APB4FZ1 (STM32_DBGMCU_BASE + 0x2C)
33#define DBGMCU_APB4FZ1_IWDG2 BIT(2)
34
35/* Security register */
36#define ETZPC_TZMA1_SIZE (STM32_ETZPC_BASE + 0x04)
37#define ETZPC_DECPROT0 (STM32_ETZPC_BASE + 0x10)
38
39#define TZC_GATE_KEEPER (STM32_TZC_BASE + 0x008)
40#define TZC_REGION_ATTRIBUTE0 (STM32_TZC_BASE + 0x110)
41#define TZC_REGION_ID_ACCESS0 (STM32_TZC_BASE + 0x114)
42
43#define TAMP_CR1 (STM32_TAMP_BASE + 0x00)
Marek Vasut852b8282024-04-19 05:59:05 +020044#define TAMP_SMCR (STM32_TAMP_BASE + 0x20)
45#define TAMP_SMCR_BKPRWDPROT GENMASK(7, 0)
46#define TAMP_SMCR_BKPWDPROT GENMASK(23, 16)
Patrick Delaunaye4bdd542022-05-20 18:24:42 +020047
48#define PWR_CR1 (STM32_PWR_BASE + 0x00)
49#define PWR_MCUCR (STM32_PWR_BASE + 0x14)
50#define PWR_CR1_DBP BIT(8)
51#define PWR_MCUCR_SBF BIT(6)
52
53/* GPIOZ registers */
54#define GPIOZ_SECCFGR 0x54004030
55
56/* DBGMCU register */
57#define DBGMCU_IDC (STM32_DBGMCU_BASE + 0x00)
58#define DBGMCU_IDC_DEV_ID_MASK GENMASK(11, 0)
59#define DBGMCU_IDC_DEV_ID_SHIFT 0
60#define DBGMCU_IDC_REV_ID_MASK GENMASK(31, 16)
61#define DBGMCU_IDC_REV_ID_SHIFT 16
62
63/* boot interface from Bootrom
64 * - boot instance = bit 31:16
65 * - boot device = bit 15:0
66 */
67#define BOOTROM_PARAM_ADDR 0x2FFC0078
68#define BOOTROM_MODE_MASK GENMASK(15, 0)
69#define BOOTROM_MODE_SHIFT 0
70#define BOOTROM_INSTANCE_MASK GENMASK(31, 16)
71#define BOOTROM_INSTANCE_SHIFT 16
72
73/* Device Part Number (RPN) = OTP_DATA1 lower 8 bits */
74#define RPN_SHIFT 0
75#define RPN_MASK GENMASK(7, 0)
76
77/* Package = bit 27:29 of OTP16 => STM32MP15_PKG defines
78 * - 100: LBGA448 (FFI) => AA = LFBGA 18x18mm 448 balls p. 0.8mm
79 * - 011: LBGA354 (LCI) => AB = LFBGA 16x16mm 359 balls p. 0.8mm
80 * - 010: TFBGA361 (FFC) => AC = TFBGA 12x12mm 361 balls p. 0.5mm
81 * - 001: TFBGA257 (LCC) => AD = TFBGA 10x10mm 257 balls p. 0.5mm
82 * - others: Reserved
83 */
84#define PKG_SHIFT 27
85#define PKG_MASK GENMASK(2, 0)
86
87static void security_init(void)
88{
89 /* Disable the backup domain write protection */
90 /* the protection is enable at each reset by hardware */
91 /* And must be disable by software */
92 setbits_le32(PWR_CR1, PWR_CR1_DBP);
93
94 while (!(readl(PWR_CR1) & PWR_CR1_DBP))
95 ;
96
97 /* If RTC clock isn't enable so this is a cold boot then we need
98 * to reset the backup domain
99 */
100 if (!(readl(RCC_BDCR) & RCC_BDCR_RTCSRC)) {
101 setbits_le32(RCC_BDCR, RCC_BDCR_VSWRST);
102 while (!(readl(RCC_BDCR) & RCC_BDCR_VSWRST))
103 ;
104 clrbits_le32(RCC_BDCR, RCC_BDCR_VSWRST);
105 }
106
107 /* allow non secure access in Write/Read for all peripheral */
108 writel(GENMASK(25, 0), ETZPC_DECPROT0);
109
110 /* Open SYSRAM for no secure access */
111 writel(0x0, ETZPC_TZMA1_SIZE);
112
113 /* enable TZC1 TZC2 clock */
114 writel(BIT(11) | BIT(12), RCC_MP_APB5ENSETR);
115
116 /* Region 0 set to no access by default */
117 /* bit 0 / 16 => nsaid0 read/write Enable
118 * bit 1 / 17 => nsaid1 read/write Enable
119 * ...
120 * bit 15 / 31 => nsaid15 read/write Enable
121 */
122 writel(0xFFFFFFFF, TZC_REGION_ID_ACCESS0);
123 /* bit 30 / 31 => Secure Global Enable : write/read */
124 /* bit 0 / 1 => Region Enable for filter 0/1 */
125 writel(BIT(0) | BIT(1) | BIT(30) | BIT(31), TZC_REGION_ATTRIBUTE0);
126
127 /* Enable Filter 0 and 1 */
128 setbits_le32(TZC_GATE_KEEPER, BIT(0) | BIT(1));
129
130 /* RCC trust zone deactivated */
131 writel(0x0, RCC_TZCR);
132
133 /* TAMP: deactivate the internal tamper
134 * Bit 23 ITAMP8E: monotonic counter overflow
135 * Bit 20 ITAMP5E: RTC calendar overflow
136 * Bit 19 ITAMP4E: HSE monitoring
137 * Bit 18 ITAMP3E: LSE monitoring
138 * Bit 16 ITAMP1E: RTC power domain supply monitoring
139 */
140 writel(0x0, TAMP_CR1);
141
Marek Vasut852b8282024-04-19 05:59:05 +0200142 /*
143 * TAMP: Configure non-zero secure protection settings. This is
144 * checked by BootROM function 35ac on OTP-CLOSED device during
145 * CPU core 1 release from endless loop. If secure protection
146 * fields are zero, the core 1 is not released from endless
147 * loop on second SGI0.
148 */
149 clrsetbits_le32(TAMP_SMCR,
150 TAMP_SMCR_BKPRWDPROT | TAMP_SMCR_BKPWDPROT,
151 FIELD_PREP(TAMP_SMCR_BKPRWDPROT, 0x20) |
152 FIELD_PREP(TAMP_SMCR_BKPWDPROT, 0x20));
153
Patrick Delaunaye4bdd542022-05-20 18:24:42 +0200154 /* GPIOZ: deactivate the security */
155 writel(BIT(0), RCC_MP_AHB5ENSETR);
156 writel(0x0, GPIOZ_SECCFGR);
157}
158
159/*
160 * Debug init
161 */
162void dbgmcu_init(void)
163{
164 /*
165 * Freeze IWDG2 if Cortex-A7 is in debug mode
166 * done in TF-A for TRUSTED boot and
167 * DBGMCU access is controlled by BSEC_DENABLE.DBGSWENABLE
168 */
169 if (bsec_dbgswenable()) {
170 setbits_le32(RCC_DBGCFGR, RCC_DBGCFGR_DBGCKEN);
171 setbits_le32(DBGMCU_APB4FZ1, DBGMCU_APB4FZ1_IWDG2);
172 }
173}
174
175void spl_board_init(void)
176{
177 struct udevice *dev;
178 int ret;
179
180 dbgmcu_init();
181
182 /* force probe of BSEC driver to shadow the upper OTP */
183 ret = uclass_get_device_by_driver(UCLASS_MISC, DM_DRIVER_GET(stm32mp_bsec), &dev);
184 if (ret)
185 log_warning("BSEC probe failed: %d\n", ret);
186}
187
188/* get bootmode from ROM code boot context: saved in TAMP register */
189static void update_bootmode(void)
190{
191 u32 boot_mode;
192 u32 bootrom_itf = readl(BOOTROM_PARAM_ADDR);
193 u32 bootrom_device, bootrom_instance;
194
195 /* enable TAMP clock = RTCAPBEN */
196 writel(BIT(8), RCC_MP_APB5ENSETR);
197
198 /* read bootrom context */
199 bootrom_device =
200 (bootrom_itf & BOOTROM_MODE_MASK) >> BOOTROM_MODE_SHIFT;
201 bootrom_instance =
202 (bootrom_itf & BOOTROM_INSTANCE_MASK) >> BOOTROM_INSTANCE_SHIFT;
203 boot_mode =
204 ((bootrom_device << BOOT_TYPE_SHIFT) & BOOT_TYPE_MASK) |
205 ((bootrom_instance << BOOT_INSTANCE_SHIFT) &
206 BOOT_INSTANCE_MASK);
207
208 /* save the boot mode in TAMP backup register */
209 clrsetbits_le32(TAMP_BOOT_CONTEXT,
210 TAMP_BOOT_MODE_MASK,
211 boot_mode << TAMP_BOOT_MODE_SHIFT);
212}
213
214/* weak function: STM32MP15x mach init for boot without TFA */
215void stm32mp_cpu_init(void)
216{
217 if (IS_ENABLED(CONFIG_SPL_BUILD)) {
218 security_init();
219 update_bootmode();
220 }
221
222 /* reset copro state in SPL, when used, or in U-Boot */
223 if (!IS_ENABLED(CONFIG_SPL) || IS_ENABLED(CONFIG_SPL_BUILD)) {
224 /* Reset Coprocessor state unless it wakes up from Standby power mode */
225 if (!(readl(PWR_MCUCR) & PWR_MCUCR_SBF)) {
226 writel(TAMP_COPRO_STATE_OFF, TAMP_COPRO_STATE);
227 writel(0, TAMP_COPRO_RSC_TBL_ADDRESS);
228 }
229 }
230}
231
232static u32 read_idc(void)
233{
234 /* DBGMCU access is controlled by BSEC_DENABLE.DBGSWENABLE */
235 if (bsec_dbgswenable()) {
236 setbits_le32(RCC_DBGCFGR, RCC_DBGCFGR_DBGCKEN);
237
238 return readl(DBGMCU_IDC);
239 }
240
241 return CPU_DEV_STM32MP15; /* STM32MP15x and unknown revision */
242}
243
244u32 get_cpu_dev(void)
245{
246 return (read_idc() & DBGMCU_IDC_DEV_ID_MASK) >> DBGMCU_IDC_DEV_ID_SHIFT;
247}
248
249u32 get_cpu_rev(void)
250{
251 return (read_idc() & DBGMCU_IDC_REV_ID_MASK) >> DBGMCU_IDC_REV_ID_SHIFT;
252}
253
254/* Get Device Part Number (RPN) from OTP */
255static u32 get_cpu_rpn(void)
256{
257 return get_otp(BSEC_OTP_RPN, RPN_SHIFT, RPN_MASK);
258}
259
260u32 get_cpu_type(void)
261{
262 return (get_cpu_dev() << 16) | get_cpu_rpn();
263}
264
Patrick Delaunay6425f582022-05-20 18:24:47 +0200265int get_eth_nb(void)
266{
267 return 1;
268}
269
Patrick Delaunaye4bdd542022-05-20 18:24:42 +0200270/* Get Package options from OTP */
271u32 get_cpu_package(void)
272{
273 return get_otp(BSEC_OTP_PKG, PKG_SHIFT, PKG_MASK);
274}
275
276static const char * const soc_type[] = {
277 "????",
278 "151C", "151A", "151F", "151D",
279 "153C", "153A", "153F", "153D",
280 "157C", "157A", "157F", "157D"
281};
282
283static const char * const soc_pkg[] = { "??", "AD", "AC", "AB", "AA" };
Patrick Delaunayc4a76ff2023-04-27 15:36:33 +0200284static const char * const soc_rev[] = { "?", "A", "B", "Z", "Y"};
Patrick Delaunaye4bdd542022-05-20 18:24:42 +0200285
286static void get_cpu_string_offsets(unsigned int *type, unsigned int *pkg,
287 unsigned int *rev)
288{
289 u32 cpu_type = get_cpu_type();
290 u32 ct = cpu_type & ~(BIT(7) | BIT(0));
291 u32 cm = ((cpu_type & BIT(7)) >> 6) | (cpu_type & BIT(0));
Patrick Delaunaye4bdd542022-05-20 18:24:42 +0200292
293 /* Bits 0 and 7 are the ACDF, 00:C 01:A 10:F 11:D */
294 switch (ct) {
295 case CPU_STM32MP151Cxx:
296 *type = cm + 1;
297 break;
298 case CPU_STM32MP153Cxx:
299 *type = cm + 5;
300 break;
301 case CPU_STM32MP157Cxx:
302 *type = cm + 9;
303 break;
304 default:
305 *type = 0;
306 break;
307 }
308
309 /* Package */
Patrick Delaunay14704dc2022-06-20 09:50:01 +0200310 *pkg = get_cpu_package();
311 if (*pkg > STM32MP15_PKG_AA_LBGA448)
312 *pkg = STM32MP15_PKG_UNKNOWN;
Patrick Delaunaye4bdd542022-05-20 18:24:42 +0200313
314 /* Revision */
315 switch (get_cpu_rev()) {
316 case CPU_REV1:
317 *rev = 1;
318 break;
319 case CPU_REV2:
320 *rev = 2;
321 break;
322 case CPU_REV2_1:
323 *rev = 3;
324 break;
Patrick Delaunayc4a76ff2023-04-27 15:36:33 +0200325 case CPU_REV2_2:
326 *rev = 4;
327 break;
Patrick Delaunaye4bdd542022-05-20 18:24:42 +0200328 default:
329 *rev = 0;
330 break;
331 }
332}
333
334void get_soc_name(char name[SOC_NAME_SIZE])
335{
336 unsigned int type, pkg, rev;
337
338 get_cpu_string_offsets(&type, &pkg, &rev);
339
Marek Vasut33d49f12024-04-14 20:39:29 +0200340 if (bsec_dbgswenable()) {
341 snprintf(name, SOC_NAME_SIZE, "STM32MP%s%s Rev.%s",
342 soc_type[type], soc_pkg[pkg], soc_rev[rev]);
343 } else {
344 /*
345 * SoC revision is only accessible via DBUMCU IDC register,
346 * which requires BSEC.DENABLE DBGSWENABLE bit to be set to
347 * make the register accessible, otherwise an access to the
348 * register triggers bus fault. As BSEC.DBGSWENABLE is zero
349 * in case of an OTP-CLOSED system, do NOT set DBGSWENABLE
350 * bit as this might open a brief window for timing attacks.
351 * Instead, report that this system is OTP-CLOSED and do not
352 * report any SoC revision to avoid confusing users.
353 */
354 snprintf(name, SOC_NAME_SIZE, "STM32MP%s%s SEC/C",
355 soc_type[type], soc_pkg[pkg]);
356 }
Patrick Delaunaye4bdd542022-05-20 18:24:42 +0200357}
358
359static void setup_soc_type_pkg_rev(void)
360{
361 unsigned int type, pkg, rev;
362
363 get_cpu_string_offsets(&type, &pkg, &rev);
364
365 env_set("soc_type", soc_type[type]);
366 env_set("soc_pkg", soc_pkg[pkg]);
367 env_set("soc_rev", soc_rev[rev]);
368}
369
370/* weak function called in arch_misc_init */
371void stm32mp_misc_init(void)
372{
373 setup_soc_type_pkg_rev();
374}