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