blob: 7f8fdc7005e8cc03a212ba4eabba94d32965311a [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 Glass1f911d42019-12-28 10:45:01 -07005#include <cpu_func.h>
Simon Glassa7b51302019-11-14 12:57:46 -07006#include <init.h>
Peng Fanb5a90292017-02-22 16:21:43 +08007#include <asm/io.h>
8#include <asm/arch/clock.h>
9#include <asm/arch/imx-regs.h>
10#include <asm/arch/sys_proto.h>
Peng Fanb1d6be92019-07-22 01:24:37 +000011#include <asm/mach-imx/boot_mode.h>
Stefano Babic33731bc2017-06-29 10:16:06 +020012#include <asm/mach-imx/hab.h>
Peng Fanb5a90292017-02-22 16:21:43 +080013
Fabio Estevamd1d70232019-11-05 09:47:51 -030014#define PMC0_BASE_ADDR 0x410a1000
15#define PMC0_CTRL 0x28
16#define PMC0_CTRL_LDOEN BIT(31)
17#define PMC0_CTRL_LDOOKDIS BIT(30)
18#define PMC0_CTRL_PMC1ON BIT(24)
19#define PMC1_BASE_ADDR 0x40400000
20#define PMC1_RUN 0x8
21#define PMC1_STOP 0x10
22#define PMC1_VLPS 0x14
Fabio Estevam04c71e72019-11-05 09:47:52 -030023#define PMC1_LDOVL_SHIFT 16
24#define PMC1_LDOVL_MASK (0x3f << PMC1_LDOVL_SHIFT)
25#define PMC1_LDOVL_900 0x1e
26#define PMC1_LDOVL_950 0x23
Fabio Estevamd1d70232019-11-05 09:47:51 -030027#define PMC1_STATUS 0x20
28#define PMC1_STATUS_LDOVLF BIT(8)
29
Peng Fanb5a90292017-02-22 16:21:43 +080030static char *get_reset_cause(char *);
31
Stefano Babicf8b509b2019-09-20 08:47:53 +020032#if defined(CONFIG_IMX_HAB)
Peng Fana26ba6d2017-02-22 16:21:53 +080033struct imx_sec_config_fuse_t const imx_sec_config_fuse = {
34 .bank = 29,
35 .word = 6,
36};
37#endif
38
Peng Fan67753cf2019-07-22 01:25:05 +000039#define ROM_VERSION_ADDR 0x80
Peng Fanb5a90292017-02-22 16:21:43 +080040u32 get_cpu_rev(void)
41{
Peng Fan67753cf2019-07-22 01:25:05 +000042 /* Check the ROM version for cpu revision */
43 u32 rom_version = readl((void __iomem *)ROM_VERSION_ADDR);
44
45 return (MXC_CPU_MX7ULP << 12) | (rom_version & 0xFF);
Peng Fanb5a90292017-02-22 16:21:43 +080046}
47
48#ifdef CONFIG_REVISION_TAG
49u32 __weak get_board_rev(void)
50{
51 return get_cpu_rev();
52}
53#endif
54
55enum bt_mode get_boot_mode(void)
56{
57 u32 bt0_cfg = 0;
58
59 bt0_cfg = readl(CMC0_RBASE + 0x40);
60 bt0_cfg &= (BT0CFG_LPBOOT_MASK | BT0CFG_DUALBOOT_MASK);
61
62 if (!(bt0_cfg & BT0CFG_LPBOOT_MASK)) {
63 /* No low power boot */
64 if (bt0_cfg & BT0CFG_DUALBOOT_MASK)
65 return DUAL_BOOT;
66 else
67 return SINGLE_BOOT;
68 }
69
70 return LOW_POWER_BOOT;
71}
72
73int arch_cpu_init(void)
74{
75 return 0;
76}
77
78#ifdef CONFIG_BOARD_POSTCLK_INIT
79int board_postclk_init(void)
80{
81 return 0;
82}
83#endif
84
85#define UNLOCK_WORD0 0xC520 /* 1st unlock word */
86#define UNLOCK_WORD1 0xD928 /* 2nd unlock word */
87#define REFRESH_WORD0 0xA602 /* 1st refresh word */
88#define REFRESH_WORD1 0xB480 /* 2nd refresh word */
89
90static void disable_wdog(u32 wdog_base)
91{
92 writel(UNLOCK_WORD0, (wdog_base + 0x04));
93 writel(UNLOCK_WORD1, (wdog_base + 0x04));
94 writel(0x0, (wdog_base + 0x0C)); /* Set WIN to 0 */
95 writel(0x400, (wdog_base + 0x08)); /* Set timeout to default 0x400 */
96 writel(0x120, (wdog_base + 0x00)); /* Disable it and set update */
97
98 writel(REFRESH_WORD0, (wdog_base + 0x04)); /* Refresh the CNT */
99 writel(REFRESH_WORD1, (wdog_base + 0x04));
100}
101
102void init_wdog(void)
103{
104 /*
105 * ROM will configure WDOG1, disable it or enable it
106 * depending on FUSE. The update bit is set for reconfigurable.
107 * We have to use unlock sequence to reconfigure it.
108 * WDOG2 is not touched by ROM, so it will have default value
109 * which is enabled. We can directly configure it.
110 * To simplify the codes, we still use same reconfigure
111 * process as WDOG1. Because the update bit is not set for
112 * WDOG2, the unlock sequence won't take effect really.
113 * It actually directly configure the wdog.
114 * In this function, we will disable both WDOG1 and WDOG2,
115 * and set update bit for both. So that kernel can reconfigure them.
116 */
117 disable_wdog(WDG1_RBASE);
118 disable_wdog(WDG2_RBASE);
119}
120
Fabio Estevamd1d70232019-11-05 09:47:51 -0300121#if defined(CONFIG_LDO_ENABLED_MODE)
122static void init_ldo_mode(void)
123{
124 unsigned int reg;
125
126 /* Set LDOOKDIS */
127 setbits_le32(PMC0_BASE_ADDR + PMC0_CTRL, PMC0_CTRL_LDOOKDIS);
128
129 /* Set LDOVL to 0.95V in PMC1_RUN */
130 reg = readl(PMC1_BASE_ADDR + PMC1_RUN);
Fabio Estevam04c71e72019-11-05 09:47:52 -0300131 reg &= ~PMC1_LDOVL_MASK;
132 reg |= (PMC1_LDOVL_950 << PMC1_LDOVL_SHIFT);
Fabio Estevamd1d70232019-11-05 09:47:51 -0300133 writel(PMC1_BASE_ADDR + PMC1_RUN, reg);
134
135 /* Wait for LDOVLF to be cleared */
136 reg = readl(PMC1_BASE_ADDR + PMC1_STATUS);
137 while (reg & PMC1_STATUS_LDOVLF)
138 ;
139
140 /* Set LDOVL to 0.95V in PMC1_STOP */
141 reg = readl(PMC1_BASE_ADDR + PMC1_STOP);
Fabio Estevam04c71e72019-11-05 09:47:52 -0300142 reg &= ~PMC1_LDOVL_MASK;
143 reg |= (PMC1_LDOVL_950 << PMC1_LDOVL_SHIFT);
Fabio Estevamd1d70232019-11-05 09:47:51 -0300144 writel(PMC1_BASE_ADDR + PMC1_STOP, reg);
145
146 /* Set LDOVL to 0.90V in PMC1_VLPS */
147 reg = readl(PMC1_BASE_ADDR + PMC1_VLPS);
Fabio Estevam04c71e72019-11-05 09:47:52 -0300148 reg &= ~PMC1_LDOVL_MASK;
149 reg |= (PMC1_LDOVL_900 << PMC1_LDOVL_SHIFT);
Fabio Estevamd1d70232019-11-05 09:47:51 -0300150 writel(PMC1_BASE_ADDR + PMC1_VLPS, reg);
151
152 /* Set LDOEN bit */
153 setbits_le32(PMC0_BASE_ADDR + PMC0_CTRL, PMC0_CTRL_LDOEN);
154
155 /* Set the PMC1ON bit */
156 setbits_le32(PMC0_BASE_ADDR + PMC0_CTRL, PMC0_CTRL_PMC1ON);
157}
158#endif
Peng Fanb5a90292017-02-22 16:21:43 +0800159
160void s_init(void)
161{
162 /* Disable wdog */
163 init_wdog();
164
165 /* clock configuration. */
166 clock_init();
167
Bai Pingb1b61c62019-07-22 01:24:42 +0000168 if (soc_rev() < CHIP_REV_2_0) {
169 /* enable dumb pmic */
170 writel((readl(SNVS_LP_LPCR) | SNVS_LPCR_DPEN), SNVS_LP_LPCR);
171 }
Fabio Estevamd1d70232019-11-05 09:47:51 -0300172
173#if defined(CONFIG_LDO_ENABLED_MODE)
174 init_ldo_mode();
175#endif
Peng Fanb5a90292017-02-22 16:21:43 +0800176 return;
177}
178
179#ifndef CONFIG_ULP_WATCHDOG
180void reset_cpu(ulong addr)
181{
182 setbits_le32(SIM0_RBASE, SIM_SOPT1_A7_SW_RESET);
183 while (1)
184 ;
185}
186#endif
187
188#if defined(CONFIG_DISPLAY_CPUINFO)
189const char *get_imx_type(u32 imxtype)
190{
191 return "7ULP";
192}
193
Fabio Estevama320c122019-11-05 09:47:50 -0300194#define PMC0_BASE_ADDR 0x410a1000
195#define PMC0_CTRL 0x28
196#define PMC0_CTRL_LDOEN BIT(31)
197
198static bool ldo_mode_is_enabled(void)
199{
200 unsigned int reg;
201
202 reg = readl(PMC0_BASE_ADDR + PMC0_CTRL);
203 if (reg & PMC0_CTRL_LDOEN)
204 return true;
205 else
206 return false;
207}
208
Peng Fanb5a90292017-02-22 16:21:43 +0800209int print_cpuinfo(void)
210{
211 u32 cpurev;
212 char cause[18];
213
214 cpurev = get_cpu_rev();
215
216 printf("CPU: Freescale i.MX%s rev%d.%d at %d MHz\n",
217 get_imx_type((cpurev & 0xFF000) >> 12),
218 (cpurev & 0x000F0) >> 4, (cpurev & 0x0000F) >> 0,
219 mxc_get_clock(MXC_ARM_CLK) / 1000000);
220
221 printf("Reset cause: %s\n", get_reset_cause(cause));
222
223 printf("Boot mode: ");
224 switch (get_boot_mode()) {
225 case LOW_POWER_BOOT:
226 printf("Low power boot\n");
227 break;
228 case DUAL_BOOT:
229 printf("Dual boot\n");
230 break;
231 case SINGLE_BOOT:
232 default:
233 printf("Single boot\n");
234 break;
235 }
236
Fabio Estevama320c122019-11-05 09:47:50 -0300237 if (ldo_mode_is_enabled())
238 printf("PMC1: LDO enabled mode\n");
239 else
240 printf("PMC1: LDO bypass mode\n");
241
Peng Fanb5a90292017-02-22 16:21:43 +0800242 return 0;
243}
244#endif
245
246#define CMC_SRS_TAMPER (1 << 31)
247#define CMC_SRS_SECURITY (1 << 30)
248#define CMC_SRS_TZWDG (1 << 29)
249#define CMC_SRS_JTAG_RST (1 << 28)
250#define CMC_SRS_CORE1 (1 << 16)
251#define CMC_SRS_LOCKUP (1 << 15)
252#define CMC_SRS_SW (1 << 14)
253#define CMC_SRS_WDG (1 << 13)
254#define CMC_SRS_PIN_RESET (1 << 8)
255#define CMC_SRS_WARM (1 << 4)
256#define CMC_SRS_HVD (1 << 3)
257#define CMC_SRS_LVD (1 << 2)
258#define CMC_SRS_POR (1 << 1)
259#define CMC_SRS_WUP (1 << 0)
260
261static u32 reset_cause = -1;
262
263static char *get_reset_cause(char *ret)
264{
265 u32 cause1, cause = 0, srs = 0;
266 u32 *reg_ssrs = (u32 *)(SRC_BASE_ADDR + 0x28);
267 u32 *reg_srs = (u32 *)(SRC_BASE_ADDR + 0x20);
268
269 if (!ret)
270 return "null";
271
272 srs = readl(reg_srs);
273 cause1 = readl(reg_ssrs);
274 writel(cause1, reg_ssrs);
275
276 reset_cause = cause1;
277
278 cause = cause1 & (CMC_SRS_POR | CMC_SRS_WUP | CMC_SRS_WARM);
279
280 switch (cause) {
281 case CMC_SRS_POR:
282 sprintf(ret, "%s", "POR");
283 break;
284 case CMC_SRS_WUP:
285 sprintf(ret, "%s", "WUP");
286 break;
287 case CMC_SRS_WARM:
288 cause = cause1 & (CMC_SRS_WDG | CMC_SRS_SW |
289 CMC_SRS_JTAG_RST);
290 switch (cause) {
291 case CMC_SRS_WDG:
292 sprintf(ret, "%s", "WARM-WDG");
293 break;
294 case CMC_SRS_SW:
295 sprintf(ret, "%s", "WARM-SW");
296 break;
297 case CMC_SRS_JTAG_RST:
298 sprintf(ret, "%s", "WARM-JTAG");
299 break;
300 default:
301 sprintf(ret, "%s", "WARM-UNKN");
302 break;
303 }
304 break;
305 default:
306 sprintf(ret, "%s-%X", "UNKN", cause1);
307 break;
308 }
309
310 debug("[%X] SRS[%X] %X - ", cause1, srs, srs^cause1);
311 return ret;
312}
313
314#ifdef CONFIG_ENV_IS_IN_MMC
315__weak int board_mmc_get_env_dev(int devno)
316{
317 return CONFIG_SYS_MMC_ENV_DEV;
318}
319
320int mmc_get_env_dev(void)
321{
322 int devno = 0;
323 u32 bt1_cfg = 0;
324
325 /* If not boot from sd/mmc, use default value */
326 if (get_boot_mode() == LOW_POWER_BOOT)
327 return CONFIG_SYS_MMC_ENV_DEV;
328
329 bt1_cfg = readl(CMC1_RBASE + 0x40);
330 devno = (bt1_cfg >> 9) & 0x7;
331
332 return board_mmc_get_env_dev(devno);
333}
334#endif
Peng Fanb1d6be92019-07-22 01:24:37 +0000335
336enum boot_device get_boot_device(void)
337{
338 struct bootrom_sw_info **p =
339 (struct bootrom_sw_info **)ROM_SW_INFO_ADDR;
340
341 enum boot_device boot_dev = SD1_BOOT;
342 u8 boot_type = (*p)->boot_dev_type;
343 u8 boot_instance = (*p)->boot_dev_instance;
344
345 switch (boot_type) {
346 case BOOT_TYPE_SD:
347 boot_dev = boot_instance + SD1_BOOT;
348 break;
349 case BOOT_TYPE_MMC:
350 boot_dev = boot_instance + MMC1_BOOT;
351 break;
352 case BOOT_TYPE_USB:
353 boot_dev = USB_BOOT;
354 break;
355 default:
356 break;
357 }
358
359 return boot_dev;
360}