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