blob: 61d331e018195c1e69d1b68e449013b54972b19e [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Peng Fanb5a90292017-02-22 16:21:43 +08002/*
3 * Copyright (C) 2016 Freescale Semiconductor, Inc.
Gaurav Jain714e7e32022-03-24 11:50:31 +05304 * Copyright 2021 NXP
Peng Fanb5a90292017-02-22 16:21:43 +08005 */
Simon Glass1e268642020-05-10 11:39:55 -06006
Tom Rinidec7ea02024-05-20 13:35:03 -06007#include <config.h>
Simon Glassafb02152019-12-28 10:45:01 -07008#include <cpu_func.h>
Simon Glassa7b51302019-11-14 12:57:46 -07009#include <init.h>
Simon Glass0f2af882020-05-10 11:40:05 -060010#include <log.h>
Peng Fanb5a90292017-02-22 16:21:43 +080011#include <asm/io.h>
12#include <asm/arch/clock.h>
13#include <asm/arch/imx-regs.h>
14#include <asm/arch/sys_proto.h>
Peng Fanb1d6be92019-07-22 01:24:37 +000015#include <asm/mach-imx/boot_mode.h>
Stefano Babic33731bc2017-06-29 10:16:06 +020016#include <asm/mach-imx/hab.h>
Sven Schwermer2645cfa2022-01-02 20:36:56 +010017#include <asm/mach-imx/sys_proto.h>
Ricardo Salveti6ac789b2021-08-31 14:53:42 +030018#include <asm/setup.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060019#include <linux/bitops.h>
Gaurav Jain714e7e32022-03-24 11:50:31 +053020#include <dm.h>
Peng Fanb5a90292017-02-22 16:21:43 +080021
Fabio Estevamd1d70232019-11-05 09:47:51 -030022#define PMC0_BASE_ADDR 0x410a1000
23#define PMC0_CTRL 0x28
24#define PMC0_CTRL_LDOEN BIT(31)
25#define PMC0_CTRL_LDOOKDIS BIT(30)
26#define PMC0_CTRL_PMC1ON BIT(24)
27#define PMC1_BASE_ADDR 0x40400000
28#define PMC1_RUN 0x8
29#define PMC1_STOP 0x10
30#define PMC1_VLPS 0x14
Fabio Estevam04c71e72019-11-05 09:47:52 -030031#define PMC1_LDOVL_SHIFT 16
32#define PMC1_LDOVL_MASK (0x3f << PMC1_LDOVL_SHIFT)
33#define PMC1_LDOVL_900 0x1e
34#define PMC1_LDOVL_950 0x23
Fabio Estevamd1d70232019-11-05 09:47:51 -030035#define PMC1_STATUS 0x20
36#define PMC1_STATUS_LDOVLF BIT(8)
37
Peng Fanb5a90292017-02-22 16:21:43 +080038static char *get_reset_cause(char *);
39
Stefano Babicf8b509b2019-09-20 08:47:53 +020040#if defined(CONFIG_IMX_HAB)
Paul Geurtsdf0f95a2024-11-01 09:49:20 +010041struct imx_fuse const imx_sec_config_fuse = {
Peng Fana26ba6d2017-02-22 16:21:53 +080042 .bank = 29,
43 .word = 6,
44};
Paul Geurtsfea40042024-11-01 09:49:21 +010045
46struct imx_fuse const imx_field_return_fuse = {
47 .bank = 9,
48 .word = 6,
49};
Peng Fana26ba6d2017-02-22 16:21:53 +080050#endif
51
Peng Fan67753cf2019-07-22 01:25:05 +000052#define ROM_VERSION_ADDR 0x80
Peng Fanb5a90292017-02-22 16:21:43 +080053u32 get_cpu_rev(void)
54{
Peng Fan67753cf2019-07-22 01:25:05 +000055 /* Check the ROM version for cpu revision */
56 u32 rom_version = readl((void __iomem *)ROM_VERSION_ADDR);
57
58 return (MXC_CPU_MX7ULP << 12) | (rom_version & 0xFF);
Peng Fanb5a90292017-02-22 16:21:43 +080059}
60
61#ifdef CONFIG_REVISION_TAG
62u32 __weak get_board_rev(void)
63{
64 return get_cpu_rev();
65}
66#endif
67
68enum bt_mode get_boot_mode(void)
69{
70 u32 bt0_cfg = 0;
71
72 bt0_cfg = readl(CMC0_RBASE + 0x40);
73 bt0_cfg &= (BT0CFG_LPBOOT_MASK | BT0CFG_DUALBOOT_MASK);
74
75 if (!(bt0_cfg & BT0CFG_LPBOOT_MASK)) {
76 /* No low power boot */
77 if (bt0_cfg & BT0CFG_DUALBOOT_MASK)
78 return DUAL_BOOT;
79 else
80 return SINGLE_BOOT;
81 }
82
83 return LOW_POWER_BOOT;
84}
85
86int arch_cpu_init(void)
87{
Sven Schwermer2645cfa2022-01-02 20:36:56 +010088 enable_ca7_smp();
Peng Fanb5a90292017-02-22 16:21:43 +080089 return 0;
90}
91
Gaurav Jain714e7e32022-03-24 11:50:31 +053092#if defined(CONFIG_ARCH_MISC_INIT)
93int arch_misc_init(void)
94{
95 if (IS_ENABLED(CONFIG_FSL_CAAM)) {
96 struct udevice *dev;
97 int ret;
98
99 ret = uclass_get_device_by_driver(UCLASS_MISC, DM_DRIVER_GET(caam_jr), &dev);
100 if (ret)
Ye Liec346892022-05-11 13:56:20 +0530101 printf("Failed to initialize caam_jr: %d\n", ret);
Gaurav Jain714e7e32022-03-24 11:50:31 +0530102 }
103
104 return 0;
105}
106#endif
107
Peng Fanb5a90292017-02-22 16:21:43 +0800108#ifdef CONFIG_BOARD_POSTCLK_INIT
109int board_postclk_init(void)
110{
111 return 0;
112}
113#endif
114
115#define UNLOCK_WORD0 0xC520 /* 1st unlock word */
116#define UNLOCK_WORD1 0xD928 /* 2nd unlock word */
117#define REFRESH_WORD0 0xA602 /* 1st refresh word */
118#define REFRESH_WORD1 0xB480 /* 2nd refresh word */
119
120static void disable_wdog(u32 wdog_base)
121{
Ye Lia24ea7c2021-09-23 17:01:15 +0300122 u32 val_cs = readl(wdog_base + 0x00);
123
124 if (!(val_cs & 0x80))
125 return;
126
127 dmb();
128 __raw_writel(REFRESH_WORD0, (wdog_base + 0x04)); /* Refresh the CNT */
129 __raw_writel(REFRESH_WORD1, (wdog_base + 0x04));
130 dmb();
131
132 if (!(val_cs & 800)) {
133 dmb();
134 __raw_writel(UNLOCK_WORD0, (wdog_base + 0x04));
135 __raw_writel(UNLOCK_WORD1, (wdog_base + 0x04));
136 dmb();
137
138 while (!(readl(wdog_base + 0x00) & 0x800));
139 }
140 dmb();
141 __raw_writel(0x0, wdog_base + 0x0C); /* Set WIN to 0 */
142 __raw_writel(0x400, wdog_base + 0x08); /* Set timeout to default 0x400 */
143 __raw_writel(0x120, wdog_base + 0x00); /* Disable it and set update */
144 dmb();
Peng Fanb5a90292017-02-22 16:21:43 +0800145
Ye Lia24ea7c2021-09-23 17:01:15 +0300146 while (!(readl(wdog_base + 0x00) & 0x400));
Peng Fanb5a90292017-02-22 16:21:43 +0800147}
148
149void init_wdog(void)
150{
151 /*
152 * ROM will configure WDOG1, disable it or enable it
153 * depending on FUSE. The update bit is set for reconfigurable.
154 * We have to use unlock sequence to reconfigure it.
155 * WDOG2 is not touched by ROM, so it will have default value
156 * which is enabled. We can directly configure it.
157 * To simplify the codes, we still use same reconfigure
158 * process as WDOG1. Because the update bit is not set for
159 * WDOG2, the unlock sequence won't take effect really.
160 * It actually directly configure the wdog.
161 * In this function, we will disable both WDOG1 and WDOG2,
162 * and set update bit for both. So that kernel can reconfigure them.
163 */
164 disable_wdog(WDG1_RBASE);
165 disable_wdog(WDG2_RBASE);
166}
167
Fabio Estevam93ee0ab2020-02-03 09:01:09 -0300168static bool ldo_mode_is_enabled(void)
169{
170 unsigned int reg;
171
172 reg = readl(PMC0_BASE_ADDR + PMC0_CTRL);
173 if (reg & PMC0_CTRL_LDOEN)
174 return true;
175 else
176 return false;
177}
178
Simon Glass85ed77d2024-09-29 19:49:46 -0600179#if !defined(CONFIG_SPL) || (defined(CONFIG_SPL) && defined(CONFIG_XPL_BUILD))
Fabio Estevamd1d70232019-11-05 09:47:51 -0300180#if defined(CONFIG_LDO_ENABLED_MODE)
181static void init_ldo_mode(void)
182{
183 unsigned int reg;
184
Fabio Estevam93ee0ab2020-02-03 09:01:09 -0300185 if (ldo_mode_is_enabled())
186 return;
187
Fabio Estevamd1d70232019-11-05 09:47:51 -0300188 /* Set LDOOKDIS */
189 setbits_le32(PMC0_BASE_ADDR + PMC0_CTRL, PMC0_CTRL_LDOOKDIS);
190
191 /* Set LDOVL to 0.95V in PMC1_RUN */
192 reg = readl(PMC1_BASE_ADDR + PMC1_RUN);
Fabio Estevam04c71e72019-11-05 09:47:52 -0300193 reg &= ~PMC1_LDOVL_MASK;
194 reg |= (PMC1_LDOVL_950 << PMC1_LDOVL_SHIFT);
Fabio Estevamd1d70232019-11-05 09:47:51 -0300195 writel(PMC1_BASE_ADDR + PMC1_RUN, reg);
196
197 /* Wait for LDOVLF to be cleared */
198 reg = readl(PMC1_BASE_ADDR + PMC1_STATUS);
199 while (reg & PMC1_STATUS_LDOVLF)
200 ;
201
202 /* Set LDOVL to 0.95V in PMC1_STOP */
203 reg = readl(PMC1_BASE_ADDR + PMC1_STOP);
Fabio Estevam04c71e72019-11-05 09:47:52 -0300204 reg &= ~PMC1_LDOVL_MASK;
205 reg |= (PMC1_LDOVL_950 << PMC1_LDOVL_SHIFT);
Fabio Estevamd1d70232019-11-05 09:47:51 -0300206 writel(PMC1_BASE_ADDR + PMC1_STOP, reg);
207
208 /* Set LDOVL to 0.90V in PMC1_VLPS */
209 reg = readl(PMC1_BASE_ADDR + PMC1_VLPS);
Fabio Estevam04c71e72019-11-05 09:47:52 -0300210 reg &= ~PMC1_LDOVL_MASK;
211 reg |= (PMC1_LDOVL_900 << PMC1_LDOVL_SHIFT);
Fabio Estevamd1d70232019-11-05 09:47:51 -0300212 writel(PMC1_BASE_ADDR + PMC1_VLPS, reg);
213
214 /* Set LDOEN bit */
215 setbits_le32(PMC0_BASE_ADDR + PMC0_CTRL, PMC0_CTRL_LDOEN);
216
217 /* Set the PMC1ON bit */
218 setbits_le32(PMC0_BASE_ADDR + PMC0_CTRL, PMC0_CTRL_PMC1ON);
219}
220#endif
Peng Fanb5a90292017-02-22 16:21:43 +0800221
222void s_init(void)
223{
224 /* Disable wdog */
225 init_wdog();
226
227 /* clock configuration. */
228 clock_init();
229
Bai Pingb1b61c62019-07-22 01:24:42 +0000230 if (soc_rev() < CHIP_REV_2_0) {
231 /* enable dumb pmic */
232 writel((readl(SNVS_LP_LPCR) | SNVS_LPCR_DPEN), SNVS_LP_LPCR);
233 }
Fabio Estevamd1d70232019-11-05 09:47:51 -0300234
235#if defined(CONFIG_LDO_ENABLED_MODE)
236 init_ldo_mode();
237#endif
Peng Fanb5a90292017-02-22 16:21:43 +0800238 return;
239}
Jorge Ramirez-Ortiz409cf642020-01-17 10:50:25 +0100240#endif
Peng Fanb5a90292017-02-22 16:21:43 +0800241
242#ifndef CONFIG_ULP_WATCHDOG
Harald Seiler6f14d5f2020-12-15 16:47:52 +0100243void reset_cpu(void)
Peng Fanb5a90292017-02-22 16:21:43 +0800244{
245 setbits_le32(SIM0_RBASE, SIM_SOPT1_A7_SW_RESET);
246 while (1)
247 ;
248}
249#endif
250
251#if defined(CONFIG_DISPLAY_CPUINFO)
252const char *get_imx_type(u32 imxtype)
253{
254 return "7ULP";
255}
256
257int print_cpuinfo(void)
258{
259 u32 cpurev;
260 char cause[18];
261
262 cpurev = get_cpu_rev();
263
264 printf("CPU: Freescale i.MX%s rev%d.%d at %d MHz\n",
265 get_imx_type((cpurev & 0xFF000) >> 12),
266 (cpurev & 0x000F0) >> 4, (cpurev & 0x0000F) >> 0,
267 mxc_get_clock(MXC_ARM_CLK) / 1000000);
268
269 printf("Reset cause: %s\n", get_reset_cause(cause));
270
271 printf("Boot mode: ");
272 switch (get_boot_mode()) {
273 case LOW_POWER_BOOT:
274 printf("Low power boot\n");
275 break;
276 case DUAL_BOOT:
277 printf("Dual boot\n");
278 break;
279 case SINGLE_BOOT:
280 default:
281 printf("Single boot\n");
282 break;
283 }
284
Fabio Estevama320c122019-11-05 09:47:50 -0300285 if (ldo_mode_is_enabled())
286 printf("PMC1: LDO enabled mode\n");
287 else
288 printf("PMC1: LDO bypass mode\n");
289
Peng Fanb5a90292017-02-22 16:21:43 +0800290 return 0;
291}
292#endif
293
294#define CMC_SRS_TAMPER (1 << 31)
295#define CMC_SRS_SECURITY (1 << 30)
296#define CMC_SRS_TZWDG (1 << 29)
297#define CMC_SRS_JTAG_RST (1 << 28)
298#define CMC_SRS_CORE1 (1 << 16)
299#define CMC_SRS_LOCKUP (1 << 15)
300#define CMC_SRS_SW (1 << 14)
301#define CMC_SRS_WDG (1 << 13)
302#define CMC_SRS_PIN_RESET (1 << 8)
303#define CMC_SRS_WARM (1 << 4)
304#define CMC_SRS_HVD (1 << 3)
305#define CMC_SRS_LVD (1 << 2)
306#define CMC_SRS_POR (1 << 1)
307#define CMC_SRS_WUP (1 << 0)
308
309static u32 reset_cause = -1;
310
311static char *get_reset_cause(char *ret)
312{
313 u32 cause1, cause = 0, srs = 0;
314 u32 *reg_ssrs = (u32 *)(SRC_BASE_ADDR + 0x28);
315 u32 *reg_srs = (u32 *)(SRC_BASE_ADDR + 0x20);
316
317 if (!ret)
318 return "null";
319
320 srs = readl(reg_srs);
321 cause1 = readl(reg_ssrs);
322 writel(cause1, reg_ssrs);
323
324 reset_cause = cause1;
325
326 cause = cause1 & (CMC_SRS_POR | CMC_SRS_WUP | CMC_SRS_WARM);
327
328 switch (cause) {
329 case CMC_SRS_POR:
330 sprintf(ret, "%s", "POR");
331 break;
332 case CMC_SRS_WUP:
333 sprintf(ret, "%s", "WUP");
334 break;
335 case CMC_SRS_WARM:
336 cause = cause1 & (CMC_SRS_WDG | CMC_SRS_SW |
337 CMC_SRS_JTAG_RST);
338 switch (cause) {
339 case CMC_SRS_WDG:
340 sprintf(ret, "%s", "WARM-WDG");
341 break;
342 case CMC_SRS_SW:
343 sprintf(ret, "%s", "WARM-SW");
344 break;
345 case CMC_SRS_JTAG_RST:
346 sprintf(ret, "%s", "WARM-JTAG");
347 break;
348 default:
349 sprintf(ret, "%s", "WARM-UNKN");
350 break;
351 }
352 break;
353 default:
354 sprintf(ret, "%s-%X", "UNKN", cause1);
355 break;
356 }
357
358 debug("[%X] SRS[%X] %X - ", cause1, srs, srs^cause1);
359 return ret;
360}
361
362#ifdef CONFIG_ENV_IS_IN_MMC
363__weak int board_mmc_get_env_dev(int devno)
364{
365 return CONFIG_SYS_MMC_ENV_DEV;
366}
367
368int mmc_get_env_dev(void)
369{
370 int devno = 0;
371 u32 bt1_cfg = 0;
372
373 /* If not boot from sd/mmc, use default value */
374 if (get_boot_mode() == LOW_POWER_BOOT)
375 return CONFIG_SYS_MMC_ENV_DEV;
376
377 bt1_cfg = readl(CMC1_RBASE + 0x40);
378 devno = (bt1_cfg >> 9) & 0x7;
379
380 return board_mmc_get_env_dev(devno);
381}
382#endif
Peng Fanb1d6be92019-07-22 01:24:37 +0000383
384enum boot_device get_boot_device(void)
385{
386 struct bootrom_sw_info **p =
387 (struct bootrom_sw_info **)ROM_SW_INFO_ADDR;
388
389 enum boot_device boot_dev = SD1_BOOT;
390 u8 boot_type = (*p)->boot_dev_type;
391 u8 boot_instance = (*p)->boot_dev_instance;
392
393 switch (boot_type) {
394 case BOOT_TYPE_SD:
395 boot_dev = boot_instance + SD1_BOOT;
396 break;
397 case BOOT_TYPE_MMC:
398 boot_dev = boot_instance + MMC1_BOOT;
399 break;
400 case BOOT_TYPE_USB:
401 boot_dev = USB_BOOT;
402 break;
403 default:
404 break;
405 }
406
407 return boot_dev;
408}
Ricardo Salveti6ac789b2021-08-31 14:53:42 +0300409
410#ifdef CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG
411/*
412 * OCOTP_CFG (SJC CHALLENGE, Unique ID)
413 * i.MX 7ULP Applications Processor Reference Manual, Rev. 0, 09/2020
414 *
415 * OCOTP_CFG0 offset 0x4B0: 15:0 -> 15:0 bits of Unique ID
416 * OCOTP_CFG1 offset 0x4C0: 15:0 -> 31:16 bits of Unique ID
417 * OCOTP_CFG2 offset 0x4D0: 15:0 -> 47:32 bits of Unique ID
418 * OCOTP_CFG3 offset 0x4E0: 15:0 -> 63:48 bits of Unique ID
419 */
420void get_board_serial(struct tag_serialnr *serialnr)
421{
422 struct ocotp_regs *ocotp = (struct ocotp_regs *)OCOTP_BASE_ADDR;
423 struct fuse_bank *bank = &ocotp->bank[1];
424 struct fuse_bank1_regs *fuse =
425 (struct fuse_bank1_regs *)bank->fuse_regs;
426
427 serialnr->low = (fuse->cfg0 & 0xFFFF) + ((fuse->cfg1 & 0xFFFF) << 16);
428 serialnr->high = (fuse->cfg2 & 0xFFFF) + ((fuse->cfg3 & 0xFFFF) << 16);
429}
430#endif /* CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG */