blob: 36abb2e57f0f69221fe7287e39acb86c8d1b00d2 [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/*
Ye Li70487ff2020-05-03 22:19:52 +08003 * Copyright 2017-2019 NXP
Peng Fanfcdbde72018-01-10 13:20:37 +08004 *
5 * Peng Fan <peng.fan@nxp.com>
Peng Fanfcdbde72018-01-10 13:20:37 +08006 */
7
8#include <common.h>
Simon Glass9edefc22019-11-14 12:57:37 -07009#include <cpu_func.h>
Simon Glass691d7192020-05-10 11:40:02 -060010#include <init.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060011#include <log.h>
Peng Fanfcdbde72018-01-10 13:20:37 +080012#include <asm/arch/imx-regs.h>
Simon Glass401d1c42020-10-30 21:38:53 -060013#include <asm/global_data.h>
Peng Fanfcdbde72018-01-10 13:20:37 +080014#include <asm/io.h>
15#include <asm/arch/clock.h>
16#include <asm/arch/sys_proto.h>
17#include <asm/mach-imx/hab.h>
18#include <asm/mach-imx/boot_mode.h>
19#include <asm/mach-imx/syscounter.h>
Peng Fan2f3c9202020-07-09 13:39:26 +080020#include <asm/ptrace.h>
Peng Fanfcdbde72018-01-10 13:20:37 +080021#include <asm/armv8/mmu.h>
Peng Fane663c702019-08-27 06:25:58 +000022#include <dm/uclass.h>
Peng Fan2f3c9202020-07-09 13:39:26 +080023#include <efi_loader.h>
Ye Li2707faf2019-07-15 01:16:46 -070024#include <env.h>
25#include <env_internal.h>
Peng Fanfcdbde72018-01-10 13:20:37 +080026#include <errno.h>
27#include <fdt_support.h>
28#include <fsl_wdog.h>
29#include <imx_sip.h>
Peng Fana2f143e2020-05-11 15:14:04 +080030#include <linux/arm-smccc.h>
Simon Glasscd93d622020-05-10 11:40:13 -060031#include <linux/bitops.h>
Peng Fanfcdbde72018-01-10 13:20:37 +080032
33DECLARE_GLOBAL_DATA_PTR;
34
Stefano Babicd714a752019-09-20 08:47:53 +020035#if defined(CONFIG_IMX_HAB)
Peng Fanfcdbde72018-01-10 13:20:37 +080036struct imx_sec_config_fuse_t const imx_sec_config_fuse = {
37 .bank = 1,
38 .word = 3,
39};
40#endif
41
42int timer_init(void)
43{
44#ifdef CONFIG_SPL_BUILD
45 struct sctr_regs *sctr = (struct sctr_regs *)SYSCNT_CTRL_BASE_ADDR;
46 unsigned long freq = readl(&sctr->cntfid0);
47
48 /* Update with accurate clock frequency */
49 asm volatile("msr cntfrq_el0, %0" : : "r" (freq) : "memory");
50
51 clrsetbits_le32(&sctr->cntcr, SC_CNTCR_FREQ0 | SC_CNTCR_FREQ1,
52 SC_CNTCR_FREQ0 | SC_CNTCR_ENABLE | SC_CNTCR_HDBG);
53#endif
54
55 gd->arch.tbl = 0;
56 gd->arch.tbu = 0;
57
58 return 0;
59}
60
61void enable_tzc380(void)
62{
63 struct iomuxc_gpr_base_regs *gpr =
64 (struct iomuxc_gpr_base_regs *)IOMUXC_GPR_BASE_ADDR;
65
66 /* Enable TZASC and lock setting */
67 setbits_le32(&gpr->gpr[10], GPR_TZASC_EN);
68 setbits_le32(&gpr->gpr[10], GPR_TZASC_EN_LOCK);
Peng Fana07c7182019-12-27 10:19:42 +080069 if (is_imx8mm() || is_imx8mn() || is_imx8mp())
Peng Fandbb2b7e2019-08-27 06:25:30 +000070 setbits_le32(&gpr->gpr[10], BIT(1));
Ye Lib3cf0a82019-08-27 06:25:34 +000071 /*
72 * set Region 0 attribute to allow secure and non-secure
73 * read/write permission. Found some masters like usb dwc3
74 * controllers can't work with secure memory.
75 */
76 writel(0xf0000000, TZASC_BASE_ADDR + 0x108);
Peng Fanfcdbde72018-01-10 13:20:37 +080077}
78
79void set_wdog_reset(struct wdog_regs *wdog)
80{
81 /*
82 * Output WDOG_B signal to reset external pmic or POR_B decided by
83 * the board design. Without external reset, the peripherals/DDR/
84 * PMIC are not reset, that may cause system working abnormal.
85 * WDZST bit is write-once only bit. Align this bit in kernel,
86 * otherwise kernel code will have no chance to set this bit.
87 */
88 setbits_le16(&wdog->wcr, WDOG_WDT_MASK | WDOG_WDZST_MASK);
89}
90
91static struct mm_region imx8m_mem_map[] = {
92 {
93 /* ROM */
94 .virt = 0x0UL,
95 .phys = 0x0UL,
96 .size = 0x100000UL,
97 .attrs = PTE_BLOCK_MEMTYPE(MT_NORMAL) |
98 PTE_BLOCK_OUTER_SHARE
99 }, {
Gary Bissoncb158852018-11-14 17:55:28 +0100100 /* CAAM */
101 .virt = 0x100000UL,
102 .phys = 0x100000UL,
103 .size = 0x8000UL,
104 .attrs = PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
105 PTE_BLOCK_NON_SHARE |
106 PTE_BLOCK_PXN | PTE_BLOCK_UXN
107 }, {
Marek Vasut09d86ea2021-02-25 21:52:26 +0100108 /* OCRAM_S */
109 .virt = 0x180000UL,
110 .phys = 0x180000UL,
111 .size = 0x8000UL,
112 .attrs = PTE_BLOCK_MEMTYPE(MT_NORMAL) |
113 PTE_BLOCK_OUTER_SHARE
114 }, {
Gary Bissoncb158852018-11-14 17:55:28 +0100115 /* TCM */
116 .virt = 0x7C0000UL,
117 .phys = 0x7C0000UL,
118 .size = 0x80000UL,
119 .attrs = PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
120 PTE_BLOCK_NON_SHARE |
121 PTE_BLOCK_PXN | PTE_BLOCK_UXN
122 }, {
Peng Fanfcdbde72018-01-10 13:20:37 +0800123 /* OCRAM */
124 .virt = 0x900000UL,
125 .phys = 0x900000UL,
126 .size = 0x200000UL,
127 .attrs = PTE_BLOCK_MEMTYPE(MT_NORMAL) |
128 PTE_BLOCK_OUTER_SHARE
129 }, {
130 /* AIPS */
131 .virt = 0xB00000UL,
132 .phys = 0xB00000UL,
133 .size = 0x3f500000UL,
134 .attrs = PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
135 PTE_BLOCK_NON_SHARE |
136 PTE_BLOCK_PXN | PTE_BLOCK_UXN
137 }, {
138 /* DRAM1 */
139 .virt = 0x40000000UL,
140 .phys = 0x40000000UL,
Peng Fan59efa6b2019-08-27 06:25:27 +0000141 .size = PHYS_SDRAM_SIZE,
Peng Fanfcdbde72018-01-10 13:20:37 +0800142 .attrs = PTE_BLOCK_MEMTYPE(MT_NORMAL) |
143 PTE_BLOCK_OUTER_SHARE
Peng Fan59efa6b2019-08-27 06:25:27 +0000144#ifdef PHYS_SDRAM_2_SIZE
Peng Fanfcdbde72018-01-10 13:20:37 +0800145 }, {
146 /* DRAM2 */
147 .virt = 0x100000000UL,
148 .phys = 0x100000000UL,
Peng Fan59efa6b2019-08-27 06:25:27 +0000149 .size = PHYS_SDRAM_2_SIZE,
Peng Fanfcdbde72018-01-10 13:20:37 +0800150 .attrs = PTE_BLOCK_MEMTYPE(MT_NORMAL) |
151 PTE_BLOCK_OUTER_SHARE
Peng Fan59efa6b2019-08-27 06:25:27 +0000152#endif
Peng Fanfcdbde72018-01-10 13:20:37 +0800153 }, {
Peng Fan3c417282020-07-09 15:26:06 +0800154 /* empty entrie to split table entry 5 if needed when TEEs are used */
155 0,
156 }, {
Peng Fanfcdbde72018-01-10 13:20:37 +0800157 /* List terminator */
158 0,
159 }
160};
161
162struct mm_region *mem_map = imx8m_mem_map;
163
Marek Vasut076dc922021-02-27 14:59:00 +0100164static unsigned int imx8m_find_dram_entry_in_mem_map(void)
165{
166 int i;
167
168 for (i = 0; i < ARRAY_SIZE(imx8m_mem_map); i++)
169 if (imx8m_mem_map[i].phys == CONFIG_SYS_SDRAM_BASE)
170 return i;
171
172 hang(); /* Entry not found, this must never happen. */
173}
174
Peng Fan59efa6b2019-08-27 06:25:27 +0000175void enable_caches(void)
176{
Peng Fan3c417282020-07-09 15:26:06 +0800177 /* If OPTEE runs, remove OPTEE memory from MMU table to avoid speculative prefetch */
178 if (rom_pointer[1]) {
179 /*
180 * TEE are loaded, So the ddr bank structures
181 * have been modified update mmu table accordingly
182 */
183 int i = 0;
184 /*
185 * please make sure that entry initial value matches
186 * imx8m_mem_map for DRAM1
187 */
Marek Vasut076dc922021-02-27 14:59:00 +0100188 int entry = imx8m_find_dram_entry_in_mem_map();
Peng Fan3c417282020-07-09 15:26:06 +0800189 u64 attrs = imx8m_mem_map[entry].attrs;
190
Marek Vasut076dc922021-02-27 14:59:00 +0100191 while (i < CONFIG_NR_DRAM_BANKS &&
192 entry < ARRAY_SIZE(imx8m_mem_map)) {
Peng Fan3c417282020-07-09 15:26:06 +0800193 if (gd->bd->bi_dram[i].start == 0)
194 break;
195 imx8m_mem_map[entry].phys = gd->bd->bi_dram[i].start;
196 imx8m_mem_map[entry].virt = gd->bd->bi_dram[i].start;
197 imx8m_mem_map[entry].size = gd->bd->bi_dram[i].size;
198 imx8m_mem_map[entry].attrs = attrs;
199 debug("Added memory mapping (%d): %llx %llx\n", entry,
200 imx8m_mem_map[entry].phys, imx8m_mem_map[entry].size);
201 i++; entry++;
202 }
203 }
Peng Fan59efa6b2019-08-27 06:25:27 +0000204
205 icache_enable();
206 dcache_enable();
207}
208
Peng Fan3c417282020-07-09 15:26:06 +0800209__weak int board_phys_sdram_size(phys_size_t *size)
210{
211 if (!size)
212 return -EINVAL;
213
214 *size = PHYS_SDRAM_SIZE;
215 return 0;
216}
217
218int dram_init(void)
219{
Marek Vasut076dc922021-02-27 14:59:00 +0100220 unsigned int entry = imx8m_find_dram_entry_in_mem_map();
Peng Fan3c417282020-07-09 15:26:06 +0800221 phys_size_t sdram_size;
222 int ret;
223
224 ret = board_phys_sdram_size(&sdram_size);
225 if (ret)
226 return ret;
227
228 /* rom_pointer[1] contains the size of TEE occupies */
229 if (rom_pointer[1])
230 gd->ram_size = sdram_size - rom_pointer[1];
231 else
232 gd->ram_size = sdram_size;
233
Tim Harveybc479b22020-09-25 08:08:35 -0700234 /* also update the SDRAM size in the mem_map used externally */
Marek Vasut076dc922021-02-27 14:59:00 +0100235 imx8m_mem_map[entry].size = sdram_size;
Tim Harveybc479b22020-09-25 08:08:35 -0700236
Peng Fan3c417282020-07-09 15:26:06 +0800237#ifdef PHYS_SDRAM_2_SIZE
238 gd->ram_size += PHYS_SDRAM_2_SIZE;
239#endif
240
241 return 0;
242}
243
244int dram_init_banksize(void)
245{
246 int bank = 0;
247 int ret;
248 phys_size_t sdram_size;
249
250 ret = board_phys_sdram_size(&sdram_size);
251 if (ret)
252 return ret;
253
254 gd->bd->bi_dram[bank].start = PHYS_SDRAM;
255 if (rom_pointer[1]) {
256 phys_addr_t optee_start = (phys_addr_t)rom_pointer[0];
257 phys_size_t optee_size = (size_t)rom_pointer[1];
258
259 gd->bd->bi_dram[bank].size = optee_start - gd->bd->bi_dram[bank].start;
260 if ((optee_start + optee_size) < (PHYS_SDRAM + sdram_size)) {
261 if (++bank >= CONFIG_NR_DRAM_BANKS) {
262 puts("CONFIG_NR_DRAM_BANKS is not enough\n");
263 return -1;
264 }
265
266 gd->bd->bi_dram[bank].start = optee_start + optee_size;
267 gd->bd->bi_dram[bank].size = PHYS_SDRAM +
268 sdram_size - gd->bd->bi_dram[bank].start;
269 }
270 } else {
271 gd->bd->bi_dram[bank].size = sdram_size;
272 }
273
274#ifdef PHYS_SDRAM_2_SIZE
275 if (++bank >= CONFIG_NR_DRAM_BANKS) {
276 puts("CONFIG_NR_DRAM_BANKS is not enough for SDRAM_2\n");
277 return -1;
278 }
279 gd->bd->bi_dram[bank].start = PHYS_SDRAM_2;
280 gd->bd->bi_dram[bank].size = PHYS_SDRAM_2_SIZE;
281#endif
282
283 return 0;
284}
285
286phys_size_t get_effective_memsize(void)
287{
288 /* return the first bank as effective memory */
289 if (rom_pointer[1])
290 return ((phys_addr_t)rom_pointer[0] - PHYS_SDRAM);
291
292#ifdef PHYS_SDRAM_2_SIZE
293 return gd->ram_size - PHYS_SDRAM_2_SIZE;
294#else
295 return gd->ram_size;
296#endif
297}
298
Peng Fan78db9a52019-08-27 06:25:17 +0000299static u32 get_cpu_variant_type(u32 type)
300{
301 struct ocotp_regs *ocotp = (struct ocotp_regs *)OCOTP_BASE_ADDR;
302 struct fuse_bank *bank = &ocotp->bank[1];
303 struct fuse_bank1_regs *fuse =
304 (struct fuse_bank1_regs *)bank->fuse_regs;
305
306 u32 value = readl(&fuse->tester4);
307
Peng Fancb1a1de2020-02-05 17:34:54 +0800308 if (type == MXC_CPU_IMX8MQ) {
309 if ((value & 0x3) == 0x2)
310 return MXC_CPU_IMX8MD;
311 else if (value & 0x200000)
312 return MXC_CPU_IMX8MQL;
313
314 } else if (type == MXC_CPU_IMX8MM) {
Peng Fan78db9a52019-08-27 06:25:17 +0000315 switch (value & 0x3) {
316 case 2:
317 if (value & 0x1c0000)
318 return MXC_CPU_IMX8MMDL;
319 else
320 return MXC_CPU_IMX8MMD;
321 case 3:
322 if (value & 0x1c0000)
323 return MXC_CPU_IMX8MMSL;
324 else
325 return MXC_CPU_IMX8MMS;
326 default:
327 if (value & 0x1c0000)
328 return MXC_CPU_IMX8MML;
329 break;
330 }
Peng Fanc9154032020-02-05 17:39:27 +0800331 } else if (type == MXC_CPU_IMX8MN) {
332 switch (value & 0x3) {
333 case 2:
Ye Lic4f78cb2021-03-19 15:57:11 +0800334 if (value & 0x1000000) {
335 if (value & 0x10000000) /* MIPI DSI */
336 return MXC_CPU_IMX8MNUD;
337 else
338 return MXC_CPU_IMX8MNDL;
339 } else {
Peng Fanc9154032020-02-05 17:39:27 +0800340 return MXC_CPU_IMX8MND;
Ye Lic4f78cb2021-03-19 15:57:11 +0800341 }
Peng Fanc9154032020-02-05 17:39:27 +0800342 case 3:
Ye Lic4f78cb2021-03-19 15:57:11 +0800343 if (value & 0x1000000) {
344 if (value & 0x10000000) /* MIPI DSI */
345 return MXC_CPU_IMX8MNUS;
346 else
347 return MXC_CPU_IMX8MNSL;
348 } else {
Peng Fanc9154032020-02-05 17:39:27 +0800349 return MXC_CPU_IMX8MNS;
Ye Lic4f78cb2021-03-19 15:57:11 +0800350 }
Peng Fanc9154032020-02-05 17:39:27 +0800351 default:
Ye Lic4f78cb2021-03-19 15:57:11 +0800352 if (value & 0x1000000) {
353 if (value & 0x10000000) /* MIPI DSI */
354 return MXC_CPU_IMX8MNUQ;
355 else
356 return MXC_CPU_IMX8MNL;
357 }
Peng Fanc9154032020-02-05 17:39:27 +0800358 break;
359 }
Ye Lid1eee7e2020-04-20 20:12:54 -0700360 } else if (type == MXC_CPU_IMX8MP) {
361 u32 value0 = readl(&fuse->tester3);
362 u32 flag = 0;
363
364 if ((value0 & 0xc0000) == 0x80000)
365 return MXC_CPU_IMX8MPD;
366
367 /* vpu disabled */
368 if ((value0 & 0x43000000) == 0x43000000)
369 flag = 1;
370
371 /* npu disabled*/
372 if ((value & 0x8) == 0x8)
373 flag |= (1 << 1);
374
375 /* isp disabled */
376 if ((value & 0x3) == 0x3)
377 flag |= (1 << 2);
378
379 switch (flag) {
380 case 7:
381 return MXC_CPU_IMX8MPL;
Ye Lid1eee7e2020-04-20 20:12:54 -0700382 case 2:
383 return MXC_CPU_IMX8MP6;
Ye Lid1eee7e2020-04-20 20:12:54 -0700384 default:
385 break;
386 }
387
Peng Fan78db9a52019-08-27 06:25:17 +0000388 }
389
390 return type;
391}
392
Peng Fanfcdbde72018-01-10 13:20:37 +0800393u32 get_cpu_rev(void)
394{
395 struct anamix_pll *ana_pll = (struct anamix_pll *)ANATOP_BASE_ADDR;
396 u32 reg = readl(&ana_pll->digprog);
397 u32 type = (reg >> 16) & 0xff;
Peng Fan78db9a52019-08-27 06:25:17 +0000398 u32 major_low = (reg >> 8) & 0xff;
Peng Fanfcdbde72018-01-10 13:20:37 +0800399 u32 rom_version;
400
401 reg &= 0xff;
402
Peng Fan625b03d2019-12-27 10:14:02 +0800403 /* iMX8MP */
404 if (major_low == 0x43) {
Ye Lid1eee7e2020-04-20 20:12:54 -0700405 type = get_cpu_variant_type(MXC_CPU_IMX8MP);
Peng Fan625b03d2019-12-27 10:14:02 +0800406 } else if (major_low == 0x42) {
407 /* iMX8MN */
Peng Fanc9154032020-02-05 17:39:27 +0800408 type = get_cpu_variant_type(MXC_CPU_IMX8MN);
Peng Fan24341312019-06-27 17:23:49 +0800409 } else if (major_low == 0x41) {
Peng Fan78db9a52019-08-27 06:25:17 +0000410 type = get_cpu_variant_type(MXC_CPU_IMX8MM);
411 } else {
412 if (reg == CHIP_REV_1_0) {
413 /*
Peng Fan9e094452019-10-16 10:24:17 +0000414 * For B0 chip, the DIGPROG is not updated,
415 * it is still TO1.0. we have to check ROM
416 * version or OCOTP_READ_FUSE_DATA.
417 * 0xff0055aa is magic number for B1.
Peng Fan78db9a52019-08-27 06:25:17 +0000418 */
Peng Fan9e094452019-10-16 10:24:17 +0000419 if (readl((void __iomem *)(OCOTP_BASE_ADDR + 0x40)) == 0xff0055aa) {
Ye Liea2b26fb2021-03-19 15:57:16 +0800420 /*
421 * B2 uses same DIGPROG and OCOTP_READ_FUSE_DATA value with B1,
422 * so have to check ROM to distinguish them
423 */
424 rom_version = readl((void __iomem *)ROM_VERSION_B0);
425 rom_version &= 0xff;
426 if (rom_version == CHIP_REV_2_2)
427 reg = CHIP_REV_2_2;
428 else
429 reg = CHIP_REV_2_1;
Peng Fan9e094452019-10-16 10:24:17 +0000430 } else {
431 rom_version =
432 readl((void __iomem *)ROM_VERSION_A0);
433 if (rom_version != CHIP_REV_1_0) {
434 rom_version = readl((void __iomem *)ROM_VERSION_B0);
Patrick Wildt6a4b07e2019-11-19 09:42:06 +0100435 rom_version &= 0xff;
Peng Fan9e094452019-10-16 10:24:17 +0000436 if (rom_version == CHIP_REV_2_0)
437 reg = CHIP_REV_2_0;
438 }
Peng Fan78db9a52019-08-27 06:25:17 +0000439 }
Peng Fanfcdbde72018-01-10 13:20:37 +0800440 }
Peng Fancb1a1de2020-02-05 17:34:54 +0800441
442 type = get_cpu_variant_type(type);
Peng Fanfcdbde72018-01-10 13:20:37 +0800443 }
444
445 return (type << 12) | reg;
446}
447
448static void imx_set_wdog_powerdown(bool enable)
449{
450 struct wdog_regs *wdog1 = (struct wdog_regs *)WDOG1_BASE_ADDR;
451 struct wdog_regs *wdog2 = (struct wdog_regs *)WDOG2_BASE_ADDR;
452 struct wdog_regs *wdog3 = (struct wdog_regs *)WDOG3_BASE_ADDR;
453
454 /* Write to the PDE (Power Down Enable) bit */
455 writew(enable, &wdog1->wmcr);
456 writew(enable, &wdog2->wmcr);
457 writew(enable, &wdog3->wmcr);
458}
459
Peng Fane663c702019-08-27 06:25:58 +0000460int arch_cpu_init_dm(void)
461{
462 struct udevice *dev;
463 int ret;
464
Peng Fancd7c8062019-10-16 03:01:51 +0000465 if (CONFIG_IS_ENABLED(CLK)) {
466 ret = uclass_get_device_by_name(UCLASS_CLK,
467 "clock-controller@30380000",
468 &dev);
469 if (ret < 0) {
470 printf("Failed to find clock node. Check device tree\n");
471 return ret;
472 }
Peng Fane663c702019-08-27 06:25:58 +0000473 }
474
475 return 0;
476}
477
Peng Fanfcdbde72018-01-10 13:20:37 +0800478int arch_cpu_init(void)
479{
Peng Fan702339b2019-04-17 09:41:16 +0000480 struct ocotp_regs *ocotp = (struct ocotp_regs *)OCOTP_BASE_ADDR;
Peng Fanfcdbde72018-01-10 13:20:37 +0800481 /*
Peng Fan0528ba02019-08-27 06:25:37 +0000482 * ROM might disable clock for SCTR,
483 * enable the clock before timer_init.
484 */
485 if (IS_ENABLED(CONFIG_SPL_BUILD))
486 clock_enable(CCGR_SCTR, 1);
487 /*
Peng Fanfcdbde72018-01-10 13:20:37 +0800488 * Init timer at very early state, because sscg pll setting
489 * will use it
490 */
491 timer_init();
492
493 if (IS_ENABLED(CONFIG_SPL_BUILD)) {
494 clock_init();
495 imx_set_wdog_powerdown(false);
Peng Fan7a42bf02020-07-09 13:52:41 +0800496
497 if (is_imx8md() || is_imx8mmd() || is_imx8mmdl() || is_imx8mms() ||
498 is_imx8mmsl() || is_imx8mnd() || is_imx8mndl() || is_imx8mns() ||
Ye Lic4f78cb2021-03-19 15:57:11 +0800499 is_imx8mnsl() || is_imx8mpd() || is_imx8mnud() || is_imx8mnus()) {
Peng Fan7a42bf02020-07-09 13:52:41 +0800500 /* Power down cpu core 1, 2 and 3 for iMX8M Dual core or Single core */
501 struct pgc_reg *pgc_core1 = (struct pgc_reg *)(GPC_BASE_ADDR + 0x840);
502 struct pgc_reg *pgc_core2 = (struct pgc_reg *)(GPC_BASE_ADDR + 0x880);
503 struct pgc_reg *pgc_core3 = (struct pgc_reg *)(GPC_BASE_ADDR + 0x8C0);
504 struct gpc_reg *gpc = (struct gpc_reg *)GPC_BASE_ADDR;
505
506 writel(0x1, &pgc_core2->pgcr);
507 writel(0x1, &pgc_core3->pgcr);
Ye Lic4f78cb2021-03-19 15:57:11 +0800508 if (is_imx8mms() || is_imx8mmsl() || is_imx8mns() || is_imx8mnsl() || is_imx8mnus()) {
Peng Fan7a42bf02020-07-09 13:52:41 +0800509 writel(0x1, &pgc_core1->pgcr);
510 writel(0xE, &gpc->cpu_pgc_dn_trg);
511 } else {
512 writel(0xC, &gpc->cpu_pgc_dn_trg);
513 }
514 }
Peng Fanfcdbde72018-01-10 13:20:37 +0800515 }
516
Peng Fan702339b2019-04-17 09:41:16 +0000517 if (is_imx8mq()) {
518 clock_enable(CCGR_OCOTP, 1);
519 if (readl(&ocotp->ctrl) & 0x200)
520 writel(0x200, &ocotp->ctrl_clr);
521 }
522
Peng Fanfcdbde72018-01-10 13:20:37 +0800523 return 0;
524}
525
Peng Fanb1821372019-09-16 03:09:36 +0000526#if defined(CONFIG_IMX8MN) || defined(CONFIG_IMX8MP)
527struct rom_api *g_rom_api = (struct rom_api *)0x980;
528
529enum boot_device get_boot_device(void)
530{
531 volatile gd_t *pgd = gd;
532 int ret;
533 u32 boot;
534 u16 boot_type;
535 u8 boot_instance;
536 enum boot_device boot_dev = SD1_BOOT;
537
538 ret = g_rom_api->query_boot_infor(QUERY_BT_DEV, &boot,
539 ((uintptr_t)&boot) ^ QUERY_BT_DEV);
540 gd = pgd;
541
542 if (ret != ROM_API_OKAY) {
543 puts("ROMAPI: failure at query_boot_info\n");
544 return -1;
545 }
546
547 boot_type = boot >> 16;
548 boot_instance = (boot >> 8) & 0xff;
549
550 switch (boot_type) {
551 case BT_DEV_TYPE_SD:
552 boot_dev = boot_instance + SD1_BOOT;
553 break;
554 case BT_DEV_TYPE_MMC:
555 boot_dev = boot_instance + MMC1_BOOT;
556 break;
557 case BT_DEV_TYPE_NAND:
558 boot_dev = NAND_BOOT;
559 break;
560 case BT_DEV_TYPE_FLEXSPINOR:
561 boot_dev = QSPI_BOOT;
562 break;
563 case BT_DEV_TYPE_USB:
564 boot_dev = USB_BOOT;
565 break;
566 default:
567 break;
568 }
569
570 return boot_dev;
571}
572#endif
573
Peng Fanfcdbde72018-01-10 13:20:37 +0800574bool is_usb_boot(void)
575{
576 return get_boot_device() == USB_BOOT;
577}
578
579#ifdef CONFIG_OF_SYSTEM_SETUP
Peng Fan6036dba2020-07-09 14:06:49 +0800580bool check_fdt_new_path(void *blob)
581{
582 const char *soc_path = "/soc@0";
583 int nodeoff;
584
585 nodeoff = fdt_path_offset(blob, soc_path);
586 if (nodeoff < 0)
587 return false;
588
589 return true;
590}
591
592static int disable_fdt_nodes(void *blob, const char *const nodes_path[], int size_array)
Peng Fanfcdbde72018-01-10 13:20:37 +0800593{
594 int i = 0;
595 int rc;
596 int nodeoff;
Peng Fan6036dba2020-07-09 14:06:49 +0800597 const char *status = "disabled";
598
599 for (i = 0; i < size_array; i++) {
600 nodeoff = fdt_path_offset(blob, nodes_path[i]);
601 if (nodeoff < 0)
602 continue; /* Not found, skip it */
603
604 printf("Found %s node\n", nodes_path[i]);
605
606add_status:
607 rc = fdt_setprop(blob, nodeoff, "status", status, strlen(status) + 1);
608 if (rc) {
609 if (rc == -FDT_ERR_NOSPACE) {
610 rc = fdt_increase_size(blob, 512);
611 if (!rc)
612 goto add_status;
613 }
614 printf("Unable to update property %s:%s, err=%s\n",
615 nodes_path[i], "status", fdt_strerror(rc));
616 } else {
617 printf("Modify %s:%s disabled\n",
618 nodes_path[i], "status");
619 }
620 }
621
622 return 0;
623}
624
625#ifdef CONFIG_IMX8MQ
626bool check_dcss_fused(void)
627{
628 struct ocotp_regs *ocotp = (struct ocotp_regs *)OCOTP_BASE_ADDR;
629 struct fuse_bank *bank = &ocotp->bank[1];
630 struct fuse_bank1_regs *fuse =
631 (struct fuse_bank1_regs *)bank->fuse_regs;
632 u32 value = readl(&fuse->tester4);
633
634 if (value & 0x4000000)
635 return true;
636
637 return false;
638}
639
640static int disable_mipi_dsi_nodes(void *blob)
641{
642 static const char * const nodes_path[] = {
643 "/mipi_dsi@30A00000",
644 "/mipi_dsi_bridge@30A00000",
645 "/dsi_phy@30A00300",
646 "/soc@0/bus@30800000/mipi_dsi@30a00000",
Peng Fanc0e2f762021-03-19 15:57:13 +0800647 "/soc@0/bus@30800000/dphy@30a00300",
648 "/soc@0/bus@30800000/mipi-dsi@30a00000",
Peng Fan6036dba2020-07-09 14:06:49 +0800649 };
650
651 return disable_fdt_nodes(blob, nodes_path, ARRAY_SIZE(nodes_path));
652}
653
654static int disable_dcss_nodes(void *blob)
655{
656 static const char * const nodes_path[] = {
657 "/dcss@0x32e00000",
658 "/dcss@32e00000",
659 "/hdmi@32c00000",
660 "/hdmi_cec@32c33800",
661 "/hdmi_drm@32c00000",
662 "/display-subsystem",
663 "/sound-hdmi",
664 "/sound-hdmi-arc",
665 "/soc@0/bus@32c00000/display-controller@32e00000",
666 "/soc@0/bus@32c00000/hdmi@32c00000",
667 };
668
669 return disable_fdt_nodes(blob, nodes_path, ARRAY_SIZE(nodes_path));
670}
671
672static int check_mipi_dsi_nodes(void *blob)
673{
674 static const char * const lcdif_path[] = {
675 "/lcdif@30320000",
Peng Fanc0e2f762021-03-19 15:57:13 +0800676 "/soc@0/bus@30000000/lcdif@30320000",
677 "/soc@0/bus@30000000/lcd-controller@30320000"
Peng Fan6036dba2020-07-09 14:06:49 +0800678 };
679 static const char * const mipi_dsi_path[] = {
680 "/mipi_dsi@30A00000",
681 "/soc@0/bus@30800000/mipi_dsi@30a00000"
682 };
683 static const char * const lcdif_ep_path[] = {
684 "/lcdif@30320000/port@0/mipi-dsi-endpoint",
Peng Fanc0e2f762021-03-19 15:57:13 +0800685 "/soc@0/bus@30000000/lcdif@30320000/port@0/endpoint",
686 "/soc@0/bus@30000000/lcd-controller@30320000/port@0/endpoint"
Peng Fan6036dba2020-07-09 14:06:49 +0800687 };
688 static const char * const mipi_dsi_ep_path[] = {
689 "/mipi_dsi@30A00000/port@1/endpoint",
Peng Fanc0e2f762021-03-19 15:57:13 +0800690 "/soc@0/bus@30800000/mipi_dsi@30a00000/ports/port@0/endpoint",
691 "/soc@0/bus@30800000/mipi-dsi@30a00000/ports/port@0/endpoint@0"
Peng Fan6036dba2020-07-09 14:06:49 +0800692 };
693
694 int lookup_node;
695 int nodeoff;
696 bool new_path = check_fdt_new_path(blob);
697 int i = new_path ? 1 : 0;
698
699 nodeoff = fdt_path_offset(blob, lcdif_path[i]);
700 if (nodeoff < 0 || !fdtdec_get_is_enabled(blob, nodeoff)) {
701 /*
702 * If can't find lcdif node or lcdif node is disabled,
703 * then disable all mipi dsi, since they only can input
704 * from DCSS
705 */
706 return disable_mipi_dsi_nodes(blob);
707 }
708
709 nodeoff = fdt_path_offset(blob, mipi_dsi_path[i]);
710 if (nodeoff < 0 || !fdtdec_get_is_enabled(blob, nodeoff))
711 return 0;
712
713 nodeoff = fdt_path_offset(blob, lcdif_ep_path[i]);
714 if (nodeoff < 0) {
715 /*
716 * If can't find lcdif endpoint, then disable all mipi dsi,
717 * since they only can input from DCSS
718 */
719 return disable_mipi_dsi_nodes(blob);
720 }
721
722 lookup_node = fdtdec_lookup_phandle(blob, nodeoff, "remote-endpoint");
723 nodeoff = fdt_path_offset(blob, mipi_dsi_ep_path[i]);
724
725 if (nodeoff > 0 && nodeoff == lookup_node)
726 return 0;
727
728 return disable_mipi_dsi_nodes(blob);
729}
730#endif
731
732int disable_vpu_nodes(void *blob)
733{
734 static const char * const nodes_path_8mq[] = {
735 "/vpu@38300000",
736 "/soc@0/vpu@38300000"
737 };
738
739 static const char * const nodes_path_8mm[] = {
740 "/vpu_g1@38300000",
741 "/vpu_g2@38310000",
742 "/vpu_h1@38320000"
743 };
744
745 static const char * const nodes_path_8mp[] = {
746 "/vpu_g1@38300000",
747 "/vpu_g2@38310000",
748 "/vpu_vc8000e@38320000"
749 };
750
751 if (is_imx8mq())
752 return disable_fdt_nodes(blob, nodes_path_8mq, ARRAY_SIZE(nodes_path_8mq));
753 else if (is_imx8mm())
754 return disable_fdt_nodes(blob, nodes_path_8mm, ARRAY_SIZE(nodes_path_8mm));
755 else if (is_imx8mp())
756 return disable_fdt_nodes(blob, nodes_path_8mp, ARRAY_SIZE(nodes_path_8mp));
757 else
758 return -EPERM;
759}
760
Ye Li98bcdf12021-03-19 15:57:09 +0800761#ifdef CONFIG_IMX8MN_LOW_DRIVE_MODE
762static int low_drive_gpu_freq(void *blob)
763{
764 static const char *nodes_path_8mn[] = {
765 "/gpu@38000000",
766 "/soc@0/gpu@38000000"
767 };
768
769 int nodeoff, cnt, i;
770 u32 assignedclks[7];
771
772 nodeoff = fdt_path_offset(blob, nodes_path_8mn[0]);
773 if (nodeoff < 0)
774 return nodeoff;
775
776 cnt = fdtdec_get_int_array_count(blob, nodeoff, "assigned-clock-rates", assignedclks, 7);
777 if (cnt < 0)
778 return cnt;
779
780 if (cnt != 7)
781 printf("Warning: %s, assigned-clock-rates count %d\n", nodes_path_8mn[0], cnt);
782
783 assignedclks[cnt - 1] = 200000000;
784 assignedclks[cnt - 2] = 200000000;
785
786 for (i = 0; i < cnt; i++) {
787 debug("<%u>, ", assignedclks[i]);
788 assignedclks[i] = cpu_to_fdt32(assignedclks[i]);
789 }
790 debug("\n");
791
792 return fdt_setprop(blob, nodeoff, "assigned-clock-rates", &assignedclks, sizeof(assignedclks));
793}
794#endif
795
Peng Fan6036dba2020-07-09 14:06:49 +0800796int disable_gpu_nodes(void *blob)
797{
798 static const char * const nodes_path_8mn[] = {
Peng Fanc0e2f762021-03-19 15:57:13 +0800799 "/gpu@38000000",
800 "/soc@/gpu@38000000"
Peng Fan6036dba2020-07-09 14:06:49 +0800801 };
802
803 return disable_fdt_nodes(blob, nodes_path_8mn, ARRAY_SIZE(nodes_path_8mn));
804}
805
806int disable_npu_nodes(void *blob)
807{
808 static const char * const nodes_path_8mp[] = {
809 "/vipsi@38500000"
810 };
811
812 return disable_fdt_nodes(blob, nodes_path_8mp, ARRAY_SIZE(nodes_path_8mp));
813}
814
815int disable_isp_nodes(void *blob)
816{
817 static const char * const nodes_path_8mp[] = {
818 "/soc@0/bus@32c00000/camera/isp@32e10000",
819 "/soc@0/bus@32c00000/camera/isp@32e20000"
820 };
821
822 return disable_fdt_nodes(blob, nodes_path_8mp, ARRAY_SIZE(nodes_path_8mp));
823}
824
825int disable_dsp_nodes(void *blob)
826{
827 static const char * const nodes_path_8mp[] = {
828 "/dsp@3b6e8000"
829 };
830
831 return disable_fdt_nodes(blob, nodes_path_8mp, ARRAY_SIZE(nodes_path_8mp));
832}
833
Ye Li4e83c052021-03-19 15:57:12 +0800834static void disable_thermal_cpu_nodes(void *blob, u32 disabled_cores)
835{
836 static const char * const thermal_path[] = {
837 "/thermal-zones/cpu-thermal/cooling-maps/map0"
838 };
839
840 int nodeoff, cnt, i, ret, j;
841 u32 cooling_dev[12];
842
843 for (i = 0; i < ARRAY_SIZE(thermal_path); i++) {
844 nodeoff = fdt_path_offset(blob, thermal_path[i]);
845 if (nodeoff < 0)
846 continue; /* Not found, skip it */
847
848 cnt = fdtdec_get_int_array_count(blob, nodeoff, "cooling-device", cooling_dev, 12);
849 if (cnt < 0)
850 continue;
851
852 if (cnt != 12)
853 printf("Warning: %s, cooling-device count %d\n", thermal_path[i], cnt);
854
855 for (j = 0; j < cnt; j++)
856 cooling_dev[j] = cpu_to_fdt32(cooling_dev[j]);
857
858 ret = fdt_setprop(blob, nodeoff, "cooling-device", &cooling_dev,
859 sizeof(u32) * (12 - disabled_cores * 3));
860 if (ret < 0) {
861 printf("Warning: %s, cooling-device setprop failed %d\n",
862 thermal_path[i], ret);
863 continue;
864 }
865
866 printf("Update node %s, cooling-device prop\n", thermal_path[i]);
867 }
868}
869
870static void disable_pmu_cpu_nodes(void *blob, u32 disabled_cores)
871{
872 static const char * const pmu_path[] = {
873 "/pmu"
874 };
875
876 int nodeoff, cnt, i, ret, j;
877 u32 irq_affinity[4];
878
879 for (i = 0; i < ARRAY_SIZE(pmu_path); i++) {
880 nodeoff = fdt_path_offset(blob, pmu_path[i]);
881 if (nodeoff < 0)
882 continue; /* Not found, skip it */
883
884 cnt = fdtdec_get_int_array_count(blob, nodeoff, "interrupt-affinity",
885 irq_affinity, 4);
886 if (cnt < 0)
887 continue;
888
889 if (cnt != 4)
890 printf("Warning: %s, interrupt-affinity count %d\n", pmu_path[i], cnt);
891
892 for (j = 0; j < cnt; j++)
893 irq_affinity[j] = cpu_to_fdt32(irq_affinity[j]);
894
895 ret = fdt_setprop(blob, nodeoff, "interrupt-affinity", &irq_affinity,
896 sizeof(u32) * (4 - disabled_cores));
897 if (ret < 0) {
898 printf("Warning: %s, interrupt-affinity setprop failed %d\n",
899 pmu_path[i], ret);
900 continue;
901 }
902
903 printf("Update node %s, interrupt-affinity prop\n", pmu_path[i]);
904 }
905}
906
Peng Fan6036dba2020-07-09 14:06:49 +0800907static int disable_cpu_nodes(void *blob, u32 disabled_cores)
908{
909 static const char * const nodes_path[] = {
910 "/cpus/cpu@1",
911 "/cpus/cpu@2",
912 "/cpus/cpu@3",
913 };
914 u32 i = 0;
915 int rc;
916 int nodeoff;
917
918 if (disabled_cores > 3)
919 return -EINVAL;
920
921 i = 3 - disabled_cores;
922
923 for (; i < 3; i++) {
924 nodeoff = fdt_path_offset(blob, nodes_path[i]);
925 if (nodeoff < 0)
926 continue; /* Not found, skip it */
927
928 debug("Found %s node\n", nodes_path[i]);
929
930 rc = fdt_del_node(blob, nodeoff);
931 if (rc < 0) {
932 printf("Unable to delete node %s, err=%s\n",
933 nodes_path[i], fdt_strerror(rc));
934 } else {
935 printf("Delete node %s\n", nodes_path[i]);
936 }
937 }
938
Ye Li4e83c052021-03-19 15:57:12 +0800939 disable_thermal_cpu_nodes(blob, disabled_cores);
940 disable_pmu_cpu_nodes(blob, disabled_cores);
941
Peng Fan6036dba2020-07-09 14:06:49 +0800942 return 0;
943}
944
Masahiro Yamadab75d8dc2020-06-26 15:13:33 +0900945int ft_system_setup(void *blob, struct bd_info *bd)
Peng Fan6036dba2020-07-09 14:06:49 +0800946{
947#ifdef CONFIG_IMX8MQ
948 int i = 0;
949 int rc;
950 int nodeoff;
951
952 if (get_boot_device() == USB_BOOT) {
953 disable_dcss_nodes(blob);
954
955 bool new_path = check_fdt_new_path(blob);
956 int v = new_path ? 1 : 0;
957 static const char * const usb_dwc3_path[] = {
958 "/usb@38100000/dwc3",
959 "/soc@0/usb@38100000"
960 };
961
962 nodeoff = fdt_path_offset(blob, usb_dwc3_path[v]);
963 if (nodeoff >= 0) {
964 const char *speed = "high-speed";
965
966 printf("Found %s node\n", usb_dwc3_path[v]);
967
968usb_modify_speed:
969
970 rc = fdt_setprop(blob, nodeoff, "maximum-speed", speed, strlen(speed) + 1);
971 if (rc) {
972 if (rc == -FDT_ERR_NOSPACE) {
973 rc = fdt_increase_size(blob, 512);
974 if (!rc)
975 goto usb_modify_speed;
976 }
977 printf("Unable to set property %s:%s, err=%s\n",
978 usb_dwc3_path[v], "maximum-speed", fdt_strerror(rc));
979 } else {
980 printf("Modify %s:%s = %s\n",
981 usb_dwc3_path[v], "maximum-speed", speed);
982 }
983 } else {
984 printf("Can't found %s node\n", usb_dwc3_path[v]);
985 }
986 }
Peng Fanfcdbde72018-01-10 13:20:37 +0800987
988 /* Disable the CPU idle for A0 chip since the HW does not support it */
989 if (is_soc_rev(CHIP_REV_1_0)) {
990 static const char * const nodes_path[] = {
991 "/cpus/cpu@0",
992 "/cpus/cpu@1",
993 "/cpus/cpu@2",
994 "/cpus/cpu@3",
995 };
996
997 for (i = 0; i < ARRAY_SIZE(nodes_path); i++) {
998 nodeoff = fdt_path_offset(blob, nodes_path[i]);
999 if (nodeoff < 0)
1000 continue; /* Not found, skip it */
1001
Marek Vasutdabaabd2020-04-24 21:37:33 +02001002 debug("Found %s node\n", nodes_path[i]);
Peng Fanfcdbde72018-01-10 13:20:37 +08001003
1004 rc = fdt_delprop(blob, nodeoff, "cpu-idle-states");
Marek Vasutdabaabd2020-04-24 21:37:33 +02001005 if (rc == -FDT_ERR_NOTFOUND)
1006 continue;
Peng Fanfcdbde72018-01-10 13:20:37 +08001007 if (rc) {
1008 printf("Unable to update property %s:%s, err=%s\n",
1009 nodes_path[i], "status", fdt_strerror(rc));
1010 return rc;
1011 }
1012
Marek Vasutdabaabd2020-04-24 21:37:33 +02001013 debug("Remove %s:%s\n", nodes_path[i],
Peng Fanfcdbde72018-01-10 13:20:37 +08001014 "cpu-idle-states");
1015 }
1016 }
1017
Peng Fan6036dba2020-07-09 14:06:49 +08001018 if (is_imx8mql()) {
1019 disable_vpu_nodes(blob);
1020 if (check_dcss_fused()) {
1021 printf("DCSS is fused\n");
1022 disable_dcss_nodes(blob);
1023 check_mipi_dsi_nodes(blob);
1024 }
1025 }
1026
1027 if (is_imx8md())
1028 disable_cpu_nodes(blob, 2);
1029
1030#elif defined(CONFIG_IMX8MM)
1031 if (is_imx8mml() || is_imx8mmdl() || is_imx8mmsl())
1032 disable_vpu_nodes(blob);
1033
1034 if (is_imx8mmd() || is_imx8mmdl())
1035 disable_cpu_nodes(blob, 2);
1036 else if (is_imx8mms() || is_imx8mmsl())
1037 disable_cpu_nodes(blob, 3);
1038
1039#elif defined(CONFIG_IMX8MN)
1040 if (is_imx8mnl() || is_imx8mndl() || is_imx8mnsl())
1041 disable_gpu_nodes(blob);
Ye Li98bcdf12021-03-19 15:57:09 +08001042#ifdef CONFIG_IMX8MN_LOW_DRIVE_MODE
1043 else {
1044 int ldm_gpu = low_drive_gpu_freq(blob);
1045
1046 if (ldm_gpu < 0)
1047 printf("Update GPU node assigned-clock-rates failed\n");
1048 else
1049 printf("Update GPU node assigned-clock-rates ok\n");
1050 }
1051#endif
Peng Fan6036dba2020-07-09 14:06:49 +08001052
Ye Lic4f78cb2021-03-19 15:57:11 +08001053 if (is_imx8mnd() || is_imx8mndl() || is_imx8mnud())
Peng Fan6036dba2020-07-09 14:06:49 +08001054 disable_cpu_nodes(blob, 2);
Ye Lic4f78cb2021-03-19 15:57:11 +08001055 else if (is_imx8mns() || is_imx8mnsl() || is_imx8mnus())
Peng Fan6036dba2020-07-09 14:06:49 +08001056 disable_cpu_nodes(blob, 3);
1057
1058#elif defined(CONFIG_IMX8MP)
Peng Fan00982222020-09-16 15:17:22 +08001059 if (is_imx8mpl())
Peng Fan6036dba2020-07-09 14:06:49 +08001060 disable_vpu_nodes(blob);
1061
Peng Fan00982222020-09-16 15:17:22 +08001062 if (is_imx8mpl() || is_imx8mp6())
Peng Fan6036dba2020-07-09 14:06:49 +08001063 disable_npu_nodes(blob);
1064
Peng Fan00982222020-09-16 15:17:22 +08001065 if (is_imx8mpl())
Peng Fan6036dba2020-07-09 14:06:49 +08001066 disable_isp_nodes(blob);
1067
Peng Fan00982222020-09-16 15:17:22 +08001068 if (is_imx8mpl() || is_imx8mp6())
Peng Fan6036dba2020-07-09 14:06:49 +08001069 disable_dsp_nodes(blob);
1070
1071 if (is_imx8mpd())
1072 disable_cpu_nodes(blob, 2);
1073#endif
1074
Peng Fanfcdbde72018-01-10 13:20:37 +08001075 return 0;
1076}
1077#endif
1078
Marek Vasutefa1a622020-04-29 15:04:21 +02001079#if !CONFIG_IS_ENABLED(SYSRESET)
Harald Seiler35b65dd2020-12-15 16:47:52 +01001080void reset_cpu(void)
Peng Fanfcdbde72018-01-10 13:20:37 +08001081{
Claudius Heinec5635a02020-04-29 15:04:23 +02001082 struct watchdog_regs *wdog = (struct watchdog_regs *)WDOG1_BASE_ADDR;
Peng Fanfcdbde72018-01-10 13:20:37 +08001083
Ye Li72479742019-12-09 00:47:18 -08001084 /* Clear WDA to trigger WDOG_B immediately */
1085 writew((SET_WCR_WT(1) | WCR_WDT | WCR_WDE | WCR_SRS), &wdog->wcr);
Peng Fand2041722019-08-27 06:25:41 +00001086
Ye Li72479742019-12-09 00:47:18 -08001087 while (1) {
1088 /*
Harald Seiler568af922020-04-29 15:04:22 +02001089 * spin for .5 seconds before reset
Ye Li72479742019-12-09 00:47:18 -08001090 */
1091 }
Peng Fanfcdbde72018-01-10 13:20:37 +08001092}
Peng Fand2041722019-08-27 06:25:41 +00001093#endif
Peng Fana07bcec2020-04-22 10:51:13 +08001094
1095#if defined(CONFIG_ARCH_MISC_INIT)
1096static void acquire_buildinfo(void)
1097{
1098 u64 atf_commit = 0;
Peng Fana2f143e2020-05-11 15:14:04 +08001099 struct arm_smccc_res res;
Peng Fana07bcec2020-04-22 10:51:13 +08001100
1101 /* Get ARM Trusted Firmware commit id */
Peng Fana2f143e2020-05-11 15:14:04 +08001102 arm_smccc_smc(IMX_SIP_BUILDINFO, IMX_SIP_BUILDINFO_GET_COMMITHASH,
Fabio Estevam68a699e2020-07-17 16:36:54 -03001103 0, 0, 0, 0, 0, 0, &res);
Peng Fana2f143e2020-05-11 15:14:04 +08001104 atf_commit = res.a0;
Peng Fana07bcec2020-04-22 10:51:13 +08001105 if (atf_commit == 0xffffffff) {
1106 debug("ATF does not support build info\n");
1107 atf_commit = 0x30; /* Display 0, 0 ascii is 0x30 */
1108 }
1109
1110 printf("\n BuildInfo:\n - ATF %s\n\n", (char *)&atf_commit);
1111}
1112
1113int arch_misc_init(void)
1114{
1115 acquire_buildinfo();
1116
1117 return 0;
1118}
1119#endif
Ye Li70487ff2020-05-03 22:19:52 +08001120
1121void imx_tmu_arch_init(void *reg_base)
1122{
Ye Li94c693d2020-05-03 22:19:53 +08001123 if (is_imx8mm() || is_imx8mn()) {
Ye Li70487ff2020-05-03 22:19:52 +08001124 /* Load TCALIV and TASR from fuses */
1125 struct ocotp_regs *ocotp =
1126 (struct ocotp_regs *)OCOTP_BASE_ADDR;
1127 struct fuse_bank *bank = &ocotp->bank[3];
1128 struct fuse_bank3_regs *fuse =
1129 (struct fuse_bank3_regs *)bank->fuse_regs;
1130
1131 u32 tca_rt, tca_hr, tca_en;
1132 u32 buf_vref, buf_slope;
1133
1134 tca_rt = fuse->ana0 & 0xFF;
1135 tca_hr = (fuse->ana0 & 0xFF00) >> 8;
1136 tca_en = (fuse->ana0 & 0x2000000) >> 25;
1137
1138 buf_vref = (fuse->ana0 & 0x1F00000) >> 20;
1139 buf_slope = (fuse->ana0 & 0xF0000) >> 16;
1140
1141 writel(buf_vref | (buf_slope << 16), (ulong)reg_base + 0x28);
1142 writel((tca_en << 31) | (tca_hr << 16) | tca_rt,
1143 (ulong)reg_base + 0x30);
1144 }
Ye Liebb9aab2020-05-03 22:19:54 +08001145#ifdef CONFIG_IMX8MP
1146 /* Load TCALIV0/1/m40 and TRIM from fuses */
1147 struct ocotp_regs *ocotp = (struct ocotp_regs *)OCOTP_BASE_ADDR;
1148 struct fuse_bank *bank = &ocotp->bank[38];
1149 struct fuse_bank38_regs *fuse =
1150 (struct fuse_bank38_regs *)bank->fuse_regs;
1151 struct fuse_bank *bank2 = &ocotp->bank[39];
1152 struct fuse_bank39_regs *fuse2 =
1153 (struct fuse_bank39_regs *)bank2->fuse_regs;
1154 u32 buf_vref, buf_slope, bjt_cur, vlsb, bgr;
1155 u32 reg;
1156 u32 tca40[2], tca25[2], tca105[2];
1157
1158 /* For blank sample */
1159 if (!fuse->ana_trim2 && !fuse->ana_trim3 &&
1160 !fuse->ana_trim4 && !fuse2->ana_trim5) {
1161 /* Use a default 25C binary codes */
1162 tca25[0] = 1596;
Ye Li3462b552020-05-03 22:19:55 +08001163 tca25[1] = 1596;
Ye Liebb9aab2020-05-03 22:19:54 +08001164 writel(tca25[0], (ulong)reg_base + 0x30);
Ye Li3462b552020-05-03 22:19:55 +08001165 writel(tca25[1], (ulong)reg_base + 0x34);
Ye Liebb9aab2020-05-03 22:19:54 +08001166 return;
1167 }
1168
1169 buf_vref = (fuse->ana_trim2 & 0xc0) >> 6;
1170 buf_slope = (fuse->ana_trim2 & 0xF00) >> 8;
1171 bjt_cur = (fuse->ana_trim2 & 0xF000) >> 12;
1172 bgr = (fuse->ana_trim2 & 0xF0000) >> 16;
1173 vlsb = (fuse->ana_trim2 & 0xF00000) >> 20;
1174 writel(buf_vref | (buf_slope << 16), (ulong)reg_base + 0x28);
1175
1176 reg = (bgr << 28) | (bjt_cur << 20) | (vlsb << 12) | (1 << 7);
1177 writel(reg, (ulong)reg_base + 0x3c);
1178
1179 tca40[0] = (fuse->ana_trim3 & 0xFFF0000) >> 16;
1180 tca25[0] = (fuse->ana_trim3 & 0xF0000000) >> 28;
1181 tca25[0] |= ((fuse->ana_trim4 & 0xFF) << 4);
1182 tca105[0] = (fuse->ana_trim4 & 0xFFF00) >> 8;
1183 tca40[1] = (fuse->ana_trim4 & 0xFFF00000) >> 20;
1184 tca25[1] = fuse2->ana_trim5 & 0xFFF;
1185 tca105[1] = (fuse2->ana_trim5 & 0xFFF000) >> 12;
1186
1187 /* use 25c for 1p calibration */
1188 writel(tca25[0] | (tca105[0] << 16), (ulong)reg_base + 0x30);
1189 writel(tca25[1] | (tca105[1] << 16), (ulong)reg_base + 0x34);
1190 writel(tca40[0] | (tca40[1] << 16), (ulong)reg_base + 0x38);
1191#endif
Ye Li70487ff2020-05-03 22:19:52 +08001192}
Peng Fan2f3c9202020-07-09 13:39:26 +08001193
1194#if defined(CONFIG_SPL_BUILD)
1195#if defined(CONFIG_IMX8MQ) || defined(CONFIG_IMX8MM) || defined(CONFIG_IMX8MN)
1196bool serror_need_skip = true;
1197
1198void do_error(struct pt_regs *pt_regs, unsigned int esr)
1199{
1200 /*
1201 * If stack is still in ROM reserved OCRAM not switch to SPL,
1202 * it is the ROM SError
1203 */
1204 ulong sp;
1205
1206 asm volatile("mov %0, sp" : "=r"(sp) : );
1207
1208 if (serror_need_skip && sp < 0x910000 && sp >= 0x900000) {
1209 /* Check for ERR050342, imx8mq HDCP enabled parts */
1210 if (is_imx8mq() && !(readl(OCOTP_BASE_ADDR + 0x450) & 0x08000000)) {
1211 serror_need_skip = false;
1212 return; /* Do nothing skip the SError in ROM */
1213 }
1214
1215 /* Check for ERR050350, field return mode for imx8mq, mm and mn */
1216 if (readl(OCOTP_BASE_ADDR + 0x630) & 0x1) {
1217 serror_need_skip = false;
1218 return; /* Do nothing skip the SError in ROM */
1219 }
1220 }
1221
1222 efi_restore_gd();
1223 printf("\"Error\" handler, esr 0x%08x\n", esr);
1224 show_regs(pt_regs);
1225 panic("Resetting CPU ...\n");
1226}
1227#endif
1228#endif
Ye Li2707faf2019-07-15 01:16:46 -07001229
1230#if defined(CONFIG_IMX8MN) || defined(CONFIG_IMX8MP)
1231enum env_location env_get_location(enum env_operation op, int prio)
1232{
1233 enum boot_device dev = get_boot_device();
1234 enum env_location env_loc = ENVL_UNKNOWN;
1235
1236 if (prio)
1237 return env_loc;
1238
1239 switch (dev) {
1240#ifdef CONFIG_ENV_IS_IN_SPI_FLASH
1241 case QSPI_BOOT:
1242 env_loc = ENVL_SPI_FLASH;
1243 break;
1244#endif
1245#ifdef CONFIG_ENV_IS_IN_NAND
1246 case NAND_BOOT:
1247 env_loc = ENVL_NAND;
1248 break;
1249#endif
1250#ifdef CONFIG_ENV_IS_IN_MMC
1251 case SD1_BOOT:
1252 case SD2_BOOT:
1253 case SD3_BOOT:
1254 case MMC1_BOOT:
1255 case MMC2_BOOT:
1256 case MMC3_BOOT:
1257 env_loc = ENVL_MMC;
1258 break;
1259#endif
1260 default:
1261#if defined(CONFIG_ENV_IS_NOWHERE)
1262 env_loc = ENVL_NOWHERE;
1263#endif
1264 break;
1265 }
1266
1267 return env_loc;
1268}
1269
1270#ifndef ENV_IS_EMBEDDED
1271long long env_get_offset(long long defautl_offset)
1272{
1273 enum boot_device dev = get_boot_device();
1274
1275 switch (dev) {
1276 case NAND_BOOT:
1277 return (60 << 20); /* 60MB offset for NAND */
1278 default:
1279 break;
1280 }
1281
1282 return defautl_offset;
1283}
1284#endif
1285#endif