blob: aeca82cdbf47d7a5e34a164dbfca90b8b8cf23f3 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Peng Fanfcdbde72018-01-10 13:20:37 +08002/*
3 * Copyright 2017 NXP
4 *
5 * Peng Fan <peng.fan@nxp.com>
Peng Fanfcdbde72018-01-10 13:20:37 +08006 */
7
8#include <common.h>
9#include <asm/arch/imx-regs.h>
10#include <asm/io.h>
11#include <asm/arch/clock.h>
12#include <asm/arch/sys_proto.h>
13#include <asm/mach-imx/hab.h>
14#include <asm/mach-imx/boot_mode.h>
15#include <asm/mach-imx/syscounter.h>
16#include <asm/armv8/mmu.h>
Peng Fane663c702019-08-27 06:25:58 +000017#include <dm/uclass.h>
Peng Fanfcdbde72018-01-10 13:20:37 +080018#include <errno.h>
19#include <fdt_support.h>
20#include <fsl_wdog.h>
21#include <imx_sip.h>
22
23DECLARE_GLOBAL_DATA_PTR;
24
Stefano Babicd714a752019-09-20 08:47:53 +020025#if defined(CONFIG_IMX_HAB)
Peng Fanfcdbde72018-01-10 13:20:37 +080026struct imx_sec_config_fuse_t const imx_sec_config_fuse = {
27 .bank = 1,
28 .word = 3,
29};
30#endif
31
32int timer_init(void)
33{
34#ifdef CONFIG_SPL_BUILD
35 struct sctr_regs *sctr = (struct sctr_regs *)SYSCNT_CTRL_BASE_ADDR;
36 unsigned long freq = readl(&sctr->cntfid0);
37
38 /* Update with accurate clock frequency */
39 asm volatile("msr cntfrq_el0, %0" : : "r" (freq) : "memory");
40
41 clrsetbits_le32(&sctr->cntcr, SC_CNTCR_FREQ0 | SC_CNTCR_FREQ1,
42 SC_CNTCR_FREQ0 | SC_CNTCR_ENABLE | SC_CNTCR_HDBG);
43#endif
44
45 gd->arch.tbl = 0;
46 gd->arch.tbu = 0;
47
48 return 0;
49}
50
51void enable_tzc380(void)
52{
53 struct iomuxc_gpr_base_regs *gpr =
54 (struct iomuxc_gpr_base_regs *)IOMUXC_GPR_BASE_ADDR;
55
56 /* Enable TZASC and lock setting */
57 setbits_le32(&gpr->gpr[10], GPR_TZASC_EN);
58 setbits_le32(&gpr->gpr[10], GPR_TZASC_EN_LOCK);
Peng Fandbb2b7e2019-08-27 06:25:30 +000059 if (IS_ENABLED(CONFIG_IMX8MM))
60 setbits_le32(&gpr->gpr[10], BIT(1));
Ye Lib3cf0a82019-08-27 06:25:34 +000061 /*
62 * set Region 0 attribute to allow secure and non-secure
63 * read/write permission. Found some masters like usb dwc3
64 * controllers can't work with secure memory.
65 */
66 writel(0xf0000000, TZASC_BASE_ADDR + 0x108);
Peng Fanfcdbde72018-01-10 13:20:37 +080067}
68
69void set_wdog_reset(struct wdog_regs *wdog)
70{
71 /*
72 * Output WDOG_B signal to reset external pmic or POR_B decided by
73 * the board design. Without external reset, the peripherals/DDR/
74 * PMIC are not reset, that may cause system working abnormal.
75 * WDZST bit is write-once only bit. Align this bit in kernel,
76 * otherwise kernel code will have no chance to set this bit.
77 */
78 setbits_le16(&wdog->wcr, WDOG_WDT_MASK | WDOG_WDZST_MASK);
79}
80
81static struct mm_region imx8m_mem_map[] = {
82 {
83 /* ROM */
84 .virt = 0x0UL,
85 .phys = 0x0UL,
86 .size = 0x100000UL,
87 .attrs = PTE_BLOCK_MEMTYPE(MT_NORMAL) |
88 PTE_BLOCK_OUTER_SHARE
89 }, {
Gary Bissoncb158852018-11-14 17:55:28 +010090 /* CAAM */
91 .virt = 0x100000UL,
92 .phys = 0x100000UL,
93 .size = 0x8000UL,
94 .attrs = PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
95 PTE_BLOCK_NON_SHARE |
96 PTE_BLOCK_PXN | PTE_BLOCK_UXN
97 }, {
98 /* TCM */
99 .virt = 0x7C0000UL,
100 .phys = 0x7C0000UL,
101 .size = 0x80000UL,
102 .attrs = PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
103 PTE_BLOCK_NON_SHARE |
104 PTE_BLOCK_PXN | PTE_BLOCK_UXN
105 }, {
Peng Fanfcdbde72018-01-10 13:20:37 +0800106 /* OCRAM */
107 .virt = 0x900000UL,
108 .phys = 0x900000UL,
109 .size = 0x200000UL,
110 .attrs = PTE_BLOCK_MEMTYPE(MT_NORMAL) |
111 PTE_BLOCK_OUTER_SHARE
112 }, {
113 /* AIPS */
114 .virt = 0xB00000UL,
115 .phys = 0xB00000UL,
116 .size = 0x3f500000UL,
117 .attrs = PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
118 PTE_BLOCK_NON_SHARE |
119 PTE_BLOCK_PXN | PTE_BLOCK_UXN
120 }, {
121 /* DRAM1 */
122 .virt = 0x40000000UL,
123 .phys = 0x40000000UL,
Peng Fan59efa6b2019-08-27 06:25:27 +0000124 .size = PHYS_SDRAM_SIZE,
Peng Fanfcdbde72018-01-10 13:20:37 +0800125 .attrs = PTE_BLOCK_MEMTYPE(MT_NORMAL) |
126 PTE_BLOCK_OUTER_SHARE
Peng Fan59efa6b2019-08-27 06:25:27 +0000127#ifdef PHYS_SDRAM_2_SIZE
Peng Fanfcdbde72018-01-10 13:20:37 +0800128 }, {
129 /* DRAM2 */
130 .virt = 0x100000000UL,
131 .phys = 0x100000000UL,
Peng Fan59efa6b2019-08-27 06:25:27 +0000132 .size = PHYS_SDRAM_2_SIZE,
Peng Fanfcdbde72018-01-10 13:20:37 +0800133 .attrs = PTE_BLOCK_MEMTYPE(MT_NORMAL) |
134 PTE_BLOCK_OUTER_SHARE
Peng Fan59efa6b2019-08-27 06:25:27 +0000135#endif
Peng Fanfcdbde72018-01-10 13:20:37 +0800136 }, {
137 /* List terminator */
138 0,
139 }
140};
141
142struct mm_region *mem_map = imx8m_mem_map;
143
Peng Fan59efa6b2019-08-27 06:25:27 +0000144void enable_caches(void)
145{
146 /*
147 * If OPTEE runs, remove OPTEE memory from MMU table to
148 * avoid speculative prefetch. OPTEE runs at the top of
149 * the first memory bank
150 */
151 if (rom_pointer[1])
152 imx8m_mem_map[5].size -= rom_pointer[1];
153
154 icache_enable();
155 dcache_enable();
156}
157
Peng Fan78db9a52019-08-27 06:25:17 +0000158static u32 get_cpu_variant_type(u32 type)
159{
160 struct ocotp_regs *ocotp = (struct ocotp_regs *)OCOTP_BASE_ADDR;
161 struct fuse_bank *bank = &ocotp->bank[1];
162 struct fuse_bank1_regs *fuse =
163 (struct fuse_bank1_regs *)bank->fuse_regs;
164
165 u32 value = readl(&fuse->tester4);
166
167 if (type == MXC_CPU_IMX8MM) {
168 switch (value & 0x3) {
169 case 2:
170 if (value & 0x1c0000)
171 return MXC_CPU_IMX8MMDL;
172 else
173 return MXC_CPU_IMX8MMD;
174 case 3:
175 if (value & 0x1c0000)
176 return MXC_CPU_IMX8MMSL;
177 else
178 return MXC_CPU_IMX8MMS;
179 default:
180 if (value & 0x1c0000)
181 return MXC_CPU_IMX8MML;
182 break;
183 }
184 }
185
186 return type;
187}
188
Peng Fanfcdbde72018-01-10 13:20:37 +0800189u32 get_cpu_rev(void)
190{
191 struct anamix_pll *ana_pll = (struct anamix_pll *)ANATOP_BASE_ADDR;
192 u32 reg = readl(&ana_pll->digprog);
193 u32 type = (reg >> 16) & 0xff;
Peng Fan78db9a52019-08-27 06:25:17 +0000194 u32 major_low = (reg >> 8) & 0xff;
Peng Fanfcdbde72018-01-10 13:20:37 +0800195 u32 rom_version;
196
197 reg &= 0xff;
198
Peng Fan78db9a52019-08-27 06:25:17 +0000199 /* i.MX8MM */
200 if (major_low == 0x41) {
201 type = get_cpu_variant_type(MXC_CPU_IMX8MM);
202 } else {
203 if (reg == CHIP_REV_1_0) {
204 /*
205 * For B0 chip, the DIGPROG is not updated, still TO1.0.
206 * we have to check ROM version further
207 */
208 rom_version = readl((void __iomem *)ROM_VERSION_A0);
209 if (rom_version != CHIP_REV_1_0) {
210 rom_version = readl((void __iomem *)ROM_VERSION_B0);
211 if (rom_version >= CHIP_REV_2_0)
212 reg = CHIP_REV_2_0;
213 }
Peng Fanfcdbde72018-01-10 13:20:37 +0800214 }
215 }
216
217 return (type << 12) | reg;
218}
219
220static void imx_set_wdog_powerdown(bool enable)
221{
222 struct wdog_regs *wdog1 = (struct wdog_regs *)WDOG1_BASE_ADDR;
223 struct wdog_regs *wdog2 = (struct wdog_regs *)WDOG2_BASE_ADDR;
224 struct wdog_regs *wdog3 = (struct wdog_regs *)WDOG3_BASE_ADDR;
225
226 /* Write to the PDE (Power Down Enable) bit */
227 writew(enable, &wdog1->wmcr);
228 writew(enable, &wdog2->wmcr);
229 writew(enable, &wdog3->wmcr);
230}
231
Peng Fane663c702019-08-27 06:25:58 +0000232int arch_cpu_init_dm(void)
233{
234 struct udevice *dev;
235 int ret;
236
237 ret = uclass_get_device_by_name(UCLASS_CLK,
238 "clock-controller@30380000",
239 &dev);
240 if (ret < 0) {
241 printf("Failed to find clock node. Check device tree\n");
242 return ret;
243 }
244
245 return 0;
246}
247
Peng Fanfcdbde72018-01-10 13:20:37 +0800248int arch_cpu_init(void)
249{
Peng Fan702339b2019-04-17 09:41:16 +0000250 struct ocotp_regs *ocotp = (struct ocotp_regs *)OCOTP_BASE_ADDR;
Peng Fanfcdbde72018-01-10 13:20:37 +0800251 /*
Peng Fan0528ba02019-08-27 06:25:37 +0000252 * ROM might disable clock for SCTR,
253 * enable the clock before timer_init.
254 */
255 if (IS_ENABLED(CONFIG_SPL_BUILD))
256 clock_enable(CCGR_SCTR, 1);
257 /*
Peng Fanfcdbde72018-01-10 13:20:37 +0800258 * Init timer at very early state, because sscg pll setting
259 * will use it
260 */
261 timer_init();
262
263 if (IS_ENABLED(CONFIG_SPL_BUILD)) {
264 clock_init();
265 imx_set_wdog_powerdown(false);
266 }
267
Peng Fan702339b2019-04-17 09:41:16 +0000268 if (is_imx8mq()) {
269 clock_enable(CCGR_OCOTP, 1);
270 if (readl(&ocotp->ctrl) & 0x200)
271 writel(0x200, &ocotp->ctrl_clr);
272 }
273
Peng Fanfcdbde72018-01-10 13:20:37 +0800274 return 0;
275}
276
277bool is_usb_boot(void)
278{
279 return get_boot_device() == USB_BOOT;
280}
281
282#ifdef CONFIG_OF_SYSTEM_SETUP
283int ft_system_setup(void *blob, bd_t *bd)
284{
285 int i = 0;
286 int rc;
287 int nodeoff;
288
289 /* Disable the CPU idle for A0 chip since the HW does not support it */
290 if (is_soc_rev(CHIP_REV_1_0)) {
291 static const char * const nodes_path[] = {
292 "/cpus/cpu@0",
293 "/cpus/cpu@1",
294 "/cpus/cpu@2",
295 "/cpus/cpu@3",
296 };
297
298 for (i = 0; i < ARRAY_SIZE(nodes_path); i++) {
299 nodeoff = fdt_path_offset(blob, nodes_path[i]);
300 if (nodeoff < 0)
301 continue; /* Not found, skip it */
302
303 printf("Found %s node\n", nodes_path[i]);
304
305 rc = fdt_delprop(blob, nodeoff, "cpu-idle-states");
306 if (rc) {
307 printf("Unable to update property %s:%s, err=%s\n",
308 nodes_path[i], "status", fdt_strerror(rc));
309 return rc;
310 }
311
312 printf("Remove %s:%s\n", nodes_path[i],
313 "cpu-idle-states");
314 }
315 }
316
317 return 0;
318}
319#endif
320
Peng Fand2041722019-08-27 06:25:41 +0000321#if defined(CONFIG_SPL_BUILD) || !defined(CONFIG_SYSRESET)
Peng Fanfcdbde72018-01-10 13:20:37 +0800322void reset_cpu(ulong addr)
323{
Peng Fand2041722019-08-27 06:25:41 +0000324 struct watchdog_regs *wdog = (struct watchdog_regs *)addr;
Peng Fanfcdbde72018-01-10 13:20:37 +0800325
Peng Fand2041722019-08-27 06:25:41 +0000326 if (!addr)
327 wdog = (struct watchdog_regs *)WDOG1_BASE_ADDR;
Peng Fanfcdbde72018-01-10 13:20:37 +0800328
Peng Fand2041722019-08-27 06:25:41 +0000329 /* Clear WDA to trigger WDOG_B immediately */
330 writew((WCR_WDE | WCR_SRS), &wdog->wcr);
331
332 while (1) {
333 /*
334 * spin for .5 seconds before reset
335 */
336 }
Peng Fanfcdbde72018-01-10 13:20:37 +0800337}
Peng Fand2041722019-08-27 06:25:41 +0000338#endif