blob: 7f097d68503a07ef247f7497b92b7c80f1bcbd9d [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>
Simon Glass4dcacfc2020-05-10 11:40:13 -060016#include <linux/bitops.h>
Peng Fanb5a90292017-02-22 16:21:43 +080017
Fabio Estevamd1d70232019-11-05 09:47:51 -030018#define PMC0_BASE_ADDR 0x410a1000
19#define PMC0_CTRL 0x28
20#define PMC0_CTRL_LDOEN BIT(31)
21#define PMC0_CTRL_LDOOKDIS BIT(30)
22#define PMC0_CTRL_PMC1ON BIT(24)
23#define PMC1_BASE_ADDR 0x40400000
24#define PMC1_RUN 0x8
25#define PMC1_STOP 0x10
26#define PMC1_VLPS 0x14
Fabio Estevam04c71e72019-11-05 09:47:52 -030027#define PMC1_LDOVL_SHIFT 16
28#define PMC1_LDOVL_MASK (0x3f << PMC1_LDOVL_SHIFT)
29#define PMC1_LDOVL_900 0x1e
30#define PMC1_LDOVL_950 0x23
Fabio Estevamd1d70232019-11-05 09:47:51 -030031#define PMC1_STATUS 0x20
32#define PMC1_STATUS_LDOVLF BIT(8)
33
Peng Fanb5a90292017-02-22 16:21:43 +080034static char *get_reset_cause(char *);
35
Stefano Babicf8b509b2019-09-20 08:47:53 +020036#if defined(CONFIG_IMX_HAB)
Peng Fana26ba6d2017-02-22 16:21:53 +080037struct imx_sec_config_fuse_t const imx_sec_config_fuse = {
38 .bank = 29,
39 .word = 6,
40};
41#endif
42
Peng Fan67753cf2019-07-22 01:25:05 +000043#define ROM_VERSION_ADDR 0x80
Peng Fanb5a90292017-02-22 16:21:43 +080044u32 get_cpu_rev(void)
45{
Peng Fan67753cf2019-07-22 01:25:05 +000046 /* Check the ROM version for cpu revision */
47 u32 rom_version = readl((void __iomem *)ROM_VERSION_ADDR);
48
49 return (MXC_CPU_MX7ULP << 12) | (rom_version & 0xFF);
Peng Fanb5a90292017-02-22 16:21:43 +080050}
51
52#ifdef CONFIG_REVISION_TAG
53u32 __weak get_board_rev(void)
54{
55 return get_cpu_rev();
56}
57#endif
58
59enum bt_mode get_boot_mode(void)
60{
61 u32 bt0_cfg = 0;
62
63 bt0_cfg = readl(CMC0_RBASE + 0x40);
64 bt0_cfg &= (BT0CFG_LPBOOT_MASK | BT0CFG_DUALBOOT_MASK);
65
66 if (!(bt0_cfg & BT0CFG_LPBOOT_MASK)) {
67 /* No low power boot */
68 if (bt0_cfg & BT0CFG_DUALBOOT_MASK)
69 return DUAL_BOOT;
70 else
71 return SINGLE_BOOT;
72 }
73
74 return LOW_POWER_BOOT;
75}
76
77int arch_cpu_init(void)
78{
79 return 0;
80}
81
82#ifdef CONFIG_BOARD_POSTCLK_INIT
83int board_postclk_init(void)
84{
85 return 0;
86}
87#endif
88
89#define UNLOCK_WORD0 0xC520 /* 1st unlock word */
90#define UNLOCK_WORD1 0xD928 /* 2nd unlock word */
91#define REFRESH_WORD0 0xA602 /* 1st refresh word */
92#define REFRESH_WORD1 0xB480 /* 2nd refresh word */
93
94static void disable_wdog(u32 wdog_base)
95{
Ye Lia24ea7c2021-09-23 17:01:15 +030096 u32 val_cs = readl(wdog_base + 0x00);
97
98 if (!(val_cs & 0x80))
99 return;
100
101 dmb();
102 __raw_writel(REFRESH_WORD0, (wdog_base + 0x04)); /* Refresh the CNT */
103 __raw_writel(REFRESH_WORD1, (wdog_base + 0x04));
104 dmb();
105
106 if (!(val_cs & 800)) {
107 dmb();
108 __raw_writel(UNLOCK_WORD0, (wdog_base + 0x04));
109 __raw_writel(UNLOCK_WORD1, (wdog_base + 0x04));
110 dmb();
111
112 while (!(readl(wdog_base + 0x00) & 0x800));
113 }
114 dmb();
115 __raw_writel(0x0, wdog_base + 0x0C); /* Set WIN to 0 */
116 __raw_writel(0x400, wdog_base + 0x08); /* Set timeout to default 0x400 */
117 __raw_writel(0x120, wdog_base + 0x00); /* Disable it and set update */
118 dmb();
Peng Fanb5a90292017-02-22 16:21:43 +0800119
Ye Lia24ea7c2021-09-23 17:01:15 +0300120 while (!(readl(wdog_base + 0x00) & 0x400));
Peng Fanb5a90292017-02-22 16:21:43 +0800121}
122
123void init_wdog(void)
124{
125 /*
126 * ROM will configure WDOG1, disable it or enable it
127 * depending on FUSE. The update bit is set for reconfigurable.
128 * We have to use unlock sequence to reconfigure it.
129 * WDOG2 is not touched by ROM, so it will have default value
130 * which is enabled. We can directly configure it.
131 * To simplify the codes, we still use same reconfigure
132 * process as WDOG1. Because the update bit is not set for
133 * WDOG2, the unlock sequence won't take effect really.
134 * It actually directly configure the wdog.
135 * In this function, we will disable both WDOG1 and WDOG2,
136 * and set update bit for both. So that kernel can reconfigure them.
137 */
138 disable_wdog(WDG1_RBASE);
139 disable_wdog(WDG2_RBASE);
140}
141
Fabio Estevam93ee0ab2020-02-03 09:01:09 -0300142static bool ldo_mode_is_enabled(void)
143{
144 unsigned int reg;
145
146 reg = readl(PMC0_BASE_ADDR + PMC0_CTRL);
147 if (reg & PMC0_CTRL_LDOEN)
148 return true;
149 else
150 return false;
151}
152
Jorge Ramirez-Ortiz409cf642020-01-17 10:50:25 +0100153#if !defined(CONFIG_SPL) || (defined(CONFIG_SPL) && defined(CONFIG_SPL_BUILD))
Fabio Estevamd1d70232019-11-05 09:47:51 -0300154#if defined(CONFIG_LDO_ENABLED_MODE)
155static void init_ldo_mode(void)
156{
157 unsigned int reg;
158
Fabio Estevam93ee0ab2020-02-03 09:01:09 -0300159 if (ldo_mode_is_enabled())
160 return;
161
Fabio Estevamd1d70232019-11-05 09:47:51 -0300162 /* Set LDOOKDIS */
163 setbits_le32(PMC0_BASE_ADDR + PMC0_CTRL, PMC0_CTRL_LDOOKDIS);
164
165 /* Set LDOVL to 0.95V in PMC1_RUN */
166 reg = readl(PMC1_BASE_ADDR + PMC1_RUN);
Fabio Estevam04c71e72019-11-05 09:47:52 -0300167 reg &= ~PMC1_LDOVL_MASK;
168 reg |= (PMC1_LDOVL_950 << PMC1_LDOVL_SHIFT);
Fabio Estevamd1d70232019-11-05 09:47:51 -0300169 writel(PMC1_BASE_ADDR + PMC1_RUN, reg);
170
171 /* Wait for LDOVLF to be cleared */
172 reg = readl(PMC1_BASE_ADDR + PMC1_STATUS);
173 while (reg & PMC1_STATUS_LDOVLF)
174 ;
175
176 /* Set LDOVL to 0.95V in PMC1_STOP */
177 reg = readl(PMC1_BASE_ADDR + PMC1_STOP);
Fabio Estevam04c71e72019-11-05 09:47:52 -0300178 reg &= ~PMC1_LDOVL_MASK;
179 reg |= (PMC1_LDOVL_950 << PMC1_LDOVL_SHIFT);
Fabio Estevamd1d70232019-11-05 09:47:51 -0300180 writel(PMC1_BASE_ADDR + PMC1_STOP, reg);
181
182 /* Set LDOVL to 0.90V in PMC1_VLPS */
183 reg = readl(PMC1_BASE_ADDR + PMC1_VLPS);
Fabio Estevam04c71e72019-11-05 09:47:52 -0300184 reg &= ~PMC1_LDOVL_MASK;
185 reg |= (PMC1_LDOVL_900 << PMC1_LDOVL_SHIFT);
Fabio Estevamd1d70232019-11-05 09:47:51 -0300186 writel(PMC1_BASE_ADDR + PMC1_VLPS, reg);
187
188 /* Set LDOEN bit */
189 setbits_le32(PMC0_BASE_ADDR + PMC0_CTRL, PMC0_CTRL_LDOEN);
190
191 /* Set the PMC1ON bit */
192 setbits_le32(PMC0_BASE_ADDR + PMC0_CTRL, PMC0_CTRL_PMC1ON);
193}
194#endif
Peng Fanb5a90292017-02-22 16:21:43 +0800195
196void s_init(void)
197{
198 /* Disable wdog */
199 init_wdog();
200
201 /* clock configuration. */
202 clock_init();
203
Bai Pingb1b61c62019-07-22 01:24:42 +0000204 if (soc_rev() < CHIP_REV_2_0) {
205 /* enable dumb pmic */
206 writel((readl(SNVS_LP_LPCR) | SNVS_LPCR_DPEN), SNVS_LP_LPCR);
207 }
Fabio Estevamd1d70232019-11-05 09:47:51 -0300208
209#if defined(CONFIG_LDO_ENABLED_MODE)
210 init_ldo_mode();
211#endif
Peng Fanb5a90292017-02-22 16:21:43 +0800212 return;
213}
Jorge Ramirez-Ortiz409cf642020-01-17 10:50:25 +0100214#endif
Peng Fanb5a90292017-02-22 16:21:43 +0800215
216#ifndef CONFIG_ULP_WATCHDOG
Harald Seiler6f14d5f2020-12-15 16:47:52 +0100217void reset_cpu(void)
Peng Fanb5a90292017-02-22 16:21:43 +0800218{
219 setbits_le32(SIM0_RBASE, SIM_SOPT1_A7_SW_RESET);
220 while (1)
221 ;
222}
223#endif
224
225#if defined(CONFIG_DISPLAY_CPUINFO)
226const char *get_imx_type(u32 imxtype)
227{
228 return "7ULP";
229}
230
231int print_cpuinfo(void)
232{
233 u32 cpurev;
234 char cause[18];
235
236 cpurev = get_cpu_rev();
237
238 printf("CPU: Freescale i.MX%s rev%d.%d at %d MHz\n",
239 get_imx_type((cpurev & 0xFF000) >> 12),
240 (cpurev & 0x000F0) >> 4, (cpurev & 0x0000F) >> 0,
241 mxc_get_clock(MXC_ARM_CLK) / 1000000);
242
243 printf("Reset cause: %s\n", get_reset_cause(cause));
244
245 printf("Boot mode: ");
246 switch (get_boot_mode()) {
247 case LOW_POWER_BOOT:
248 printf("Low power boot\n");
249 break;
250 case DUAL_BOOT:
251 printf("Dual boot\n");
252 break;
253 case SINGLE_BOOT:
254 default:
255 printf("Single boot\n");
256 break;
257 }
258
Fabio Estevama320c122019-11-05 09:47:50 -0300259 if (ldo_mode_is_enabled())
260 printf("PMC1: LDO enabled mode\n");
261 else
262 printf("PMC1: LDO bypass mode\n");
263
Peng Fanb5a90292017-02-22 16:21:43 +0800264 return 0;
265}
266#endif
267
268#define CMC_SRS_TAMPER (1 << 31)
269#define CMC_SRS_SECURITY (1 << 30)
270#define CMC_SRS_TZWDG (1 << 29)
271#define CMC_SRS_JTAG_RST (1 << 28)
272#define CMC_SRS_CORE1 (1 << 16)
273#define CMC_SRS_LOCKUP (1 << 15)
274#define CMC_SRS_SW (1 << 14)
275#define CMC_SRS_WDG (1 << 13)
276#define CMC_SRS_PIN_RESET (1 << 8)
277#define CMC_SRS_WARM (1 << 4)
278#define CMC_SRS_HVD (1 << 3)
279#define CMC_SRS_LVD (1 << 2)
280#define CMC_SRS_POR (1 << 1)
281#define CMC_SRS_WUP (1 << 0)
282
283static u32 reset_cause = -1;
284
285static char *get_reset_cause(char *ret)
286{
287 u32 cause1, cause = 0, srs = 0;
288 u32 *reg_ssrs = (u32 *)(SRC_BASE_ADDR + 0x28);
289 u32 *reg_srs = (u32 *)(SRC_BASE_ADDR + 0x20);
290
291 if (!ret)
292 return "null";
293
294 srs = readl(reg_srs);
295 cause1 = readl(reg_ssrs);
296 writel(cause1, reg_ssrs);
297
298 reset_cause = cause1;
299
300 cause = cause1 & (CMC_SRS_POR | CMC_SRS_WUP | CMC_SRS_WARM);
301
302 switch (cause) {
303 case CMC_SRS_POR:
304 sprintf(ret, "%s", "POR");
305 break;
306 case CMC_SRS_WUP:
307 sprintf(ret, "%s", "WUP");
308 break;
309 case CMC_SRS_WARM:
310 cause = cause1 & (CMC_SRS_WDG | CMC_SRS_SW |
311 CMC_SRS_JTAG_RST);
312 switch (cause) {
313 case CMC_SRS_WDG:
314 sprintf(ret, "%s", "WARM-WDG");
315 break;
316 case CMC_SRS_SW:
317 sprintf(ret, "%s", "WARM-SW");
318 break;
319 case CMC_SRS_JTAG_RST:
320 sprintf(ret, "%s", "WARM-JTAG");
321 break;
322 default:
323 sprintf(ret, "%s", "WARM-UNKN");
324 break;
325 }
326 break;
327 default:
328 sprintf(ret, "%s-%X", "UNKN", cause1);
329 break;
330 }
331
332 debug("[%X] SRS[%X] %X - ", cause1, srs, srs^cause1);
333 return ret;
334}
335
336#ifdef CONFIG_ENV_IS_IN_MMC
337__weak int board_mmc_get_env_dev(int devno)
338{
339 return CONFIG_SYS_MMC_ENV_DEV;
340}
341
342int mmc_get_env_dev(void)
343{
344 int devno = 0;
345 u32 bt1_cfg = 0;
346
347 /* If not boot from sd/mmc, use default value */
348 if (get_boot_mode() == LOW_POWER_BOOT)
349 return CONFIG_SYS_MMC_ENV_DEV;
350
351 bt1_cfg = readl(CMC1_RBASE + 0x40);
352 devno = (bt1_cfg >> 9) & 0x7;
353
354 return board_mmc_get_env_dev(devno);
355}
356#endif
Peng Fanb1d6be92019-07-22 01:24:37 +0000357
358enum boot_device get_boot_device(void)
359{
360 struct bootrom_sw_info **p =
361 (struct bootrom_sw_info **)ROM_SW_INFO_ADDR;
362
363 enum boot_device boot_dev = SD1_BOOT;
364 u8 boot_type = (*p)->boot_dev_type;
365 u8 boot_instance = (*p)->boot_dev_instance;
366
367 switch (boot_type) {
368 case BOOT_TYPE_SD:
369 boot_dev = boot_instance + SD1_BOOT;
370 break;
371 case BOOT_TYPE_MMC:
372 boot_dev = boot_instance + MMC1_BOOT;
373 break;
374 case BOOT_TYPE_USB:
375 boot_dev = USB_BOOT;
376 break;
377 default:
378 break;
379 }
380
381 return boot_dev;
382}