blob: bc41cbc687180d05b9cf6813f3f842a94e17a2e8 [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.
Peng Fanb5a90292017-02-22 16:21:43 +08004 */
Simon Glass1e268642020-05-10 11:39:55 -06005
6#include <common.h>
Simon Glassafb02152019-12-28 10:45:01 -07007#include <cpu_func.h>
Simon Glassa7b51302019-11-14 12:57:46 -07008#include <init.h>
Simon Glass0f2af882020-05-10 11:40:05 -06009#include <log.h>
Peng Fanb5a90292017-02-22 16:21:43 +080010#include <asm/io.h>
11#include <asm/arch/clock.h>
12#include <asm/arch/imx-regs.h>
13#include <asm/arch/sys_proto.h>
Peng Fanb1d6be92019-07-22 01:24:37 +000014#include <asm/mach-imx/boot_mode.h>
Stefano Babic33731bc2017-06-29 10:16:06 +020015#include <asm/mach-imx/hab.h>
Sven Schwermer2645cfa2022-01-02 20:36:56 +010016#include <asm/mach-imx/sys_proto.h>
Ricardo Salveti6ac789b2021-08-31 14:53:42 +030017#include <asm/setup.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060018#include <linux/bitops.h>
Peng Fanb5a90292017-02-22 16:21:43 +080019
Fabio Estevamd1d70232019-11-05 09:47:51 -030020#define PMC0_BASE_ADDR 0x410a1000
21#define PMC0_CTRL 0x28
22#define PMC0_CTRL_LDOEN BIT(31)
23#define PMC0_CTRL_LDOOKDIS BIT(30)
24#define PMC0_CTRL_PMC1ON BIT(24)
25#define PMC1_BASE_ADDR 0x40400000
26#define PMC1_RUN 0x8
27#define PMC1_STOP 0x10
28#define PMC1_VLPS 0x14
Fabio Estevam04c71e72019-11-05 09:47:52 -030029#define PMC1_LDOVL_SHIFT 16
30#define PMC1_LDOVL_MASK (0x3f << PMC1_LDOVL_SHIFT)
31#define PMC1_LDOVL_900 0x1e
32#define PMC1_LDOVL_950 0x23
Fabio Estevamd1d70232019-11-05 09:47:51 -030033#define PMC1_STATUS 0x20
34#define PMC1_STATUS_LDOVLF BIT(8)
35
Peng Fanb5a90292017-02-22 16:21:43 +080036static char *get_reset_cause(char *);
37
Stefano Babicf8b509b2019-09-20 08:47:53 +020038#if defined(CONFIG_IMX_HAB)
Peng Fana26ba6d2017-02-22 16:21:53 +080039struct imx_sec_config_fuse_t const imx_sec_config_fuse = {
40 .bank = 29,
41 .word = 6,
42};
43#endif
44
Peng Fan67753cf2019-07-22 01:25:05 +000045#define ROM_VERSION_ADDR 0x80
Peng Fanb5a90292017-02-22 16:21:43 +080046u32 get_cpu_rev(void)
47{
Peng Fan67753cf2019-07-22 01:25:05 +000048 /* Check the ROM version for cpu revision */
49 u32 rom_version = readl((void __iomem *)ROM_VERSION_ADDR);
50
51 return (MXC_CPU_MX7ULP << 12) | (rom_version & 0xFF);
Peng Fanb5a90292017-02-22 16:21:43 +080052}
53
54#ifdef CONFIG_REVISION_TAG
55u32 __weak get_board_rev(void)
56{
57 return get_cpu_rev();
58}
59#endif
60
61enum bt_mode get_boot_mode(void)
62{
63 u32 bt0_cfg = 0;
64
65 bt0_cfg = readl(CMC0_RBASE + 0x40);
66 bt0_cfg &= (BT0CFG_LPBOOT_MASK | BT0CFG_DUALBOOT_MASK);
67
68 if (!(bt0_cfg & BT0CFG_LPBOOT_MASK)) {
69 /* No low power boot */
70 if (bt0_cfg & BT0CFG_DUALBOOT_MASK)
71 return DUAL_BOOT;
72 else
73 return SINGLE_BOOT;
74 }
75
76 return LOW_POWER_BOOT;
77}
78
79int arch_cpu_init(void)
80{
Sven Schwermer2645cfa2022-01-02 20:36:56 +010081 enable_ca7_smp();
Peng Fanb5a90292017-02-22 16:21:43 +080082 return 0;
83}
84
85#ifdef CONFIG_BOARD_POSTCLK_INIT
86int board_postclk_init(void)
87{
88 return 0;
89}
90#endif
91
92#define UNLOCK_WORD0 0xC520 /* 1st unlock word */
93#define UNLOCK_WORD1 0xD928 /* 2nd unlock word */
94#define REFRESH_WORD0 0xA602 /* 1st refresh word */
95#define REFRESH_WORD1 0xB480 /* 2nd refresh word */
96
97static void disable_wdog(u32 wdog_base)
98{
Ye Lia24ea7c2021-09-23 17:01:15 +030099 u32 val_cs = readl(wdog_base + 0x00);
100
101 if (!(val_cs & 0x80))
102 return;
103
104 dmb();
105 __raw_writel(REFRESH_WORD0, (wdog_base + 0x04)); /* Refresh the CNT */
106 __raw_writel(REFRESH_WORD1, (wdog_base + 0x04));
107 dmb();
108
109 if (!(val_cs & 800)) {
110 dmb();
111 __raw_writel(UNLOCK_WORD0, (wdog_base + 0x04));
112 __raw_writel(UNLOCK_WORD1, (wdog_base + 0x04));
113 dmb();
114
115 while (!(readl(wdog_base + 0x00) & 0x800));
116 }
117 dmb();
118 __raw_writel(0x0, wdog_base + 0x0C); /* Set WIN to 0 */
119 __raw_writel(0x400, wdog_base + 0x08); /* Set timeout to default 0x400 */
120 __raw_writel(0x120, wdog_base + 0x00); /* Disable it and set update */
121 dmb();
Peng Fanb5a90292017-02-22 16:21:43 +0800122
Ye Lia24ea7c2021-09-23 17:01:15 +0300123 while (!(readl(wdog_base + 0x00) & 0x400));
Peng Fanb5a90292017-02-22 16:21:43 +0800124}
125
126void init_wdog(void)
127{
128 /*
129 * ROM will configure WDOG1, disable it or enable it
130 * depending on FUSE. The update bit is set for reconfigurable.
131 * We have to use unlock sequence to reconfigure it.
132 * WDOG2 is not touched by ROM, so it will have default value
133 * which is enabled. We can directly configure it.
134 * To simplify the codes, we still use same reconfigure
135 * process as WDOG1. Because the update bit is not set for
136 * WDOG2, the unlock sequence won't take effect really.
137 * It actually directly configure the wdog.
138 * In this function, we will disable both WDOG1 and WDOG2,
139 * and set update bit for both. So that kernel can reconfigure them.
140 */
141 disable_wdog(WDG1_RBASE);
142 disable_wdog(WDG2_RBASE);
143}
144
Fabio Estevam93ee0ab2020-02-03 09:01:09 -0300145static bool ldo_mode_is_enabled(void)
146{
147 unsigned int reg;
148
149 reg = readl(PMC0_BASE_ADDR + PMC0_CTRL);
150 if (reg & PMC0_CTRL_LDOEN)
151 return true;
152 else
153 return false;
154}
155
Jorge Ramirez-Ortiz409cf642020-01-17 10:50:25 +0100156#if !defined(CONFIG_SPL) || (defined(CONFIG_SPL) && defined(CONFIG_SPL_BUILD))
Fabio Estevamd1d70232019-11-05 09:47:51 -0300157#if defined(CONFIG_LDO_ENABLED_MODE)
158static void init_ldo_mode(void)
159{
160 unsigned int reg;
161
Fabio Estevam93ee0ab2020-02-03 09:01:09 -0300162 if (ldo_mode_is_enabled())
163 return;
164
Fabio Estevamd1d70232019-11-05 09:47:51 -0300165 /* Set LDOOKDIS */
166 setbits_le32(PMC0_BASE_ADDR + PMC0_CTRL, PMC0_CTRL_LDOOKDIS);
167
168 /* Set LDOVL to 0.95V in PMC1_RUN */
169 reg = readl(PMC1_BASE_ADDR + PMC1_RUN);
Fabio Estevam04c71e72019-11-05 09:47:52 -0300170 reg &= ~PMC1_LDOVL_MASK;
171 reg |= (PMC1_LDOVL_950 << PMC1_LDOVL_SHIFT);
Fabio Estevamd1d70232019-11-05 09:47:51 -0300172 writel(PMC1_BASE_ADDR + PMC1_RUN, reg);
173
174 /* Wait for LDOVLF to be cleared */
175 reg = readl(PMC1_BASE_ADDR + PMC1_STATUS);
176 while (reg & PMC1_STATUS_LDOVLF)
177 ;
178
179 /* Set LDOVL to 0.95V in PMC1_STOP */
180 reg = readl(PMC1_BASE_ADDR + PMC1_STOP);
Fabio Estevam04c71e72019-11-05 09:47:52 -0300181 reg &= ~PMC1_LDOVL_MASK;
182 reg |= (PMC1_LDOVL_950 << PMC1_LDOVL_SHIFT);
Fabio Estevamd1d70232019-11-05 09:47:51 -0300183 writel(PMC1_BASE_ADDR + PMC1_STOP, reg);
184
185 /* Set LDOVL to 0.90V in PMC1_VLPS */
186 reg = readl(PMC1_BASE_ADDR + PMC1_VLPS);
Fabio Estevam04c71e72019-11-05 09:47:52 -0300187 reg &= ~PMC1_LDOVL_MASK;
188 reg |= (PMC1_LDOVL_900 << PMC1_LDOVL_SHIFT);
Fabio Estevamd1d70232019-11-05 09:47:51 -0300189 writel(PMC1_BASE_ADDR + PMC1_VLPS, reg);
190
191 /* Set LDOEN bit */
192 setbits_le32(PMC0_BASE_ADDR + PMC0_CTRL, PMC0_CTRL_LDOEN);
193
194 /* Set the PMC1ON bit */
195 setbits_le32(PMC0_BASE_ADDR + PMC0_CTRL, PMC0_CTRL_PMC1ON);
196}
197#endif
Peng Fanb5a90292017-02-22 16:21:43 +0800198
199void s_init(void)
200{
201 /* Disable wdog */
202 init_wdog();
203
204 /* clock configuration. */
205 clock_init();
206
Bai Pingb1b61c62019-07-22 01:24:42 +0000207 if (soc_rev() < CHIP_REV_2_0) {
208 /* enable dumb pmic */
209 writel((readl(SNVS_LP_LPCR) | SNVS_LPCR_DPEN), SNVS_LP_LPCR);
210 }
Fabio Estevamd1d70232019-11-05 09:47:51 -0300211
212#if defined(CONFIG_LDO_ENABLED_MODE)
213 init_ldo_mode();
214#endif
Peng Fanb5a90292017-02-22 16:21:43 +0800215 return;
216}
Jorge Ramirez-Ortiz409cf642020-01-17 10:50:25 +0100217#endif
Peng Fanb5a90292017-02-22 16:21:43 +0800218
219#ifndef CONFIG_ULP_WATCHDOG
Harald Seiler6f14d5f2020-12-15 16:47:52 +0100220void reset_cpu(void)
Peng Fanb5a90292017-02-22 16:21:43 +0800221{
222 setbits_le32(SIM0_RBASE, SIM_SOPT1_A7_SW_RESET);
223 while (1)
224 ;
225}
226#endif
227
228#if defined(CONFIG_DISPLAY_CPUINFO)
229const char *get_imx_type(u32 imxtype)
230{
231 return "7ULP";
232}
233
234int print_cpuinfo(void)
235{
236 u32 cpurev;
237 char cause[18];
238
239 cpurev = get_cpu_rev();
240
241 printf("CPU: Freescale i.MX%s rev%d.%d at %d MHz\n",
242 get_imx_type((cpurev & 0xFF000) >> 12),
243 (cpurev & 0x000F0) >> 4, (cpurev & 0x0000F) >> 0,
244 mxc_get_clock(MXC_ARM_CLK) / 1000000);
245
246 printf("Reset cause: %s\n", get_reset_cause(cause));
247
248 printf("Boot mode: ");
249 switch (get_boot_mode()) {
250 case LOW_POWER_BOOT:
251 printf("Low power boot\n");
252 break;
253 case DUAL_BOOT:
254 printf("Dual boot\n");
255 break;
256 case SINGLE_BOOT:
257 default:
258 printf("Single boot\n");
259 break;
260 }
261
Fabio Estevama320c122019-11-05 09:47:50 -0300262 if (ldo_mode_is_enabled())
263 printf("PMC1: LDO enabled mode\n");
264 else
265 printf("PMC1: LDO bypass mode\n");
266
Peng Fanb5a90292017-02-22 16:21:43 +0800267 return 0;
268}
269#endif
270
271#define CMC_SRS_TAMPER (1 << 31)
272#define CMC_SRS_SECURITY (1 << 30)
273#define CMC_SRS_TZWDG (1 << 29)
274#define CMC_SRS_JTAG_RST (1 << 28)
275#define CMC_SRS_CORE1 (1 << 16)
276#define CMC_SRS_LOCKUP (1 << 15)
277#define CMC_SRS_SW (1 << 14)
278#define CMC_SRS_WDG (1 << 13)
279#define CMC_SRS_PIN_RESET (1 << 8)
280#define CMC_SRS_WARM (1 << 4)
281#define CMC_SRS_HVD (1 << 3)
282#define CMC_SRS_LVD (1 << 2)
283#define CMC_SRS_POR (1 << 1)
284#define CMC_SRS_WUP (1 << 0)
285
286static u32 reset_cause = -1;
287
288static char *get_reset_cause(char *ret)
289{
290 u32 cause1, cause = 0, srs = 0;
291 u32 *reg_ssrs = (u32 *)(SRC_BASE_ADDR + 0x28);
292 u32 *reg_srs = (u32 *)(SRC_BASE_ADDR + 0x20);
293
294 if (!ret)
295 return "null";
296
297 srs = readl(reg_srs);
298 cause1 = readl(reg_ssrs);
299 writel(cause1, reg_ssrs);
300
301 reset_cause = cause1;
302
303 cause = cause1 & (CMC_SRS_POR | CMC_SRS_WUP | CMC_SRS_WARM);
304
305 switch (cause) {
306 case CMC_SRS_POR:
307 sprintf(ret, "%s", "POR");
308 break;
309 case CMC_SRS_WUP:
310 sprintf(ret, "%s", "WUP");
311 break;
312 case CMC_SRS_WARM:
313 cause = cause1 & (CMC_SRS_WDG | CMC_SRS_SW |
314 CMC_SRS_JTAG_RST);
315 switch (cause) {
316 case CMC_SRS_WDG:
317 sprintf(ret, "%s", "WARM-WDG");
318 break;
319 case CMC_SRS_SW:
320 sprintf(ret, "%s", "WARM-SW");
321 break;
322 case CMC_SRS_JTAG_RST:
323 sprintf(ret, "%s", "WARM-JTAG");
324 break;
325 default:
326 sprintf(ret, "%s", "WARM-UNKN");
327 break;
328 }
329 break;
330 default:
331 sprintf(ret, "%s-%X", "UNKN", cause1);
332 break;
333 }
334
335 debug("[%X] SRS[%X] %X - ", cause1, srs, srs^cause1);
336 return ret;
337}
338
339#ifdef CONFIG_ENV_IS_IN_MMC
340__weak int board_mmc_get_env_dev(int devno)
341{
342 return CONFIG_SYS_MMC_ENV_DEV;
343}
344
345int mmc_get_env_dev(void)
346{
347 int devno = 0;
348 u32 bt1_cfg = 0;
349
350 /* If not boot from sd/mmc, use default value */
351 if (get_boot_mode() == LOW_POWER_BOOT)
352 return CONFIG_SYS_MMC_ENV_DEV;
353
354 bt1_cfg = readl(CMC1_RBASE + 0x40);
355 devno = (bt1_cfg >> 9) & 0x7;
356
357 return board_mmc_get_env_dev(devno);
358}
359#endif
Peng Fanb1d6be92019-07-22 01:24:37 +0000360
361enum boot_device get_boot_device(void)
362{
363 struct bootrom_sw_info **p =
364 (struct bootrom_sw_info **)ROM_SW_INFO_ADDR;
365
366 enum boot_device boot_dev = SD1_BOOT;
367 u8 boot_type = (*p)->boot_dev_type;
368 u8 boot_instance = (*p)->boot_dev_instance;
369
370 switch (boot_type) {
371 case BOOT_TYPE_SD:
372 boot_dev = boot_instance + SD1_BOOT;
373 break;
374 case BOOT_TYPE_MMC:
375 boot_dev = boot_instance + MMC1_BOOT;
376 break;
377 case BOOT_TYPE_USB:
378 boot_dev = USB_BOOT;
379 break;
380 default:
381 break;
382 }
383
384 return boot_dev;
385}
Ricardo Salveti6ac789b2021-08-31 14:53:42 +0300386
387#ifdef CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG
388/*
389 * OCOTP_CFG (SJC CHALLENGE, Unique ID)
390 * i.MX 7ULP Applications Processor Reference Manual, Rev. 0, 09/2020
391 *
392 * OCOTP_CFG0 offset 0x4B0: 15:0 -> 15:0 bits of Unique ID
393 * OCOTP_CFG1 offset 0x4C0: 15:0 -> 31:16 bits of Unique ID
394 * OCOTP_CFG2 offset 0x4D0: 15:0 -> 47:32 bits of Unique ID
395 * OCOTP_CFG3 offset 0x4E0: 15:0 -> 63:48 bits of Unique ID
396 */
397void get_board_serial(struct tag_serialnr *serialnr)
398{
399 struct ocotp_regs *ocotp = (struct ocotp_regs *)OCOTP_BASE_ADDR;
400 struct fuse_bank *bank = &ocotp->bank[1];
401 struct fuse_bank1_regs *fuse =
402 (struct fuse_bank1_regs *)bank->fuse_regs;
403
404 serialnr->low = (fuse->cfg0 & 0xFFFF) + ((fuse->cfg1 & 0xFFFF) << 16);
405 serialnr->high = (fuse->cfg2 & 0xFFFF) + ((fuse->cfg3 & 0xFFFF) << 16);
406}
407#endif /* CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG */