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