blob: f2ddc834d4b1d6697d2f449f751af296efb15e25 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Peng Faneae4de22018-01-10 13:20:37 +08002/*
Ye Li325cd012020-05-03 22:19:52 +08003 * Copyright 2017-2019 NXP
Peng Faneae4de22018-01-10 13:20:37 +08004 *
5 * Peng Fan <peng.fan@nxp.com>
Peng Faneae4de22018-01-10 13:20:37 +08006 */
7
8#include <common.h>
Simon Glass1d91ba72019-11-14 12:57:37 -07009#include <cpu_func.h>
Simon Glass97589732020-05-10 11:40:02 -060010#include <init.h>
Simon Glass0f2af882020-05-10 11:40:05 -060011#include <log.h>
Peng Faneae4de22018-01-10 13:20:37 +080012#include <asm/arch/imx-regs.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060013#include <asm/global_data.h>
Peng Faneae4de22018-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 Fana35215d2020-07-09 13:39:26 +080020#include <asm/ptrace.h>
Peng Faneae4de22018-01-10 13:20:37 +080021#include <asm/armv8/mmu.h>
Peng Fanc98e0322019-08-27 06:25:58 +000022#include <dm/uclass.h>
Peng Fana35215d2020-07-09 13:39:26 +080023#include <efi_loader.h>
Ye Li0513f362019-07-15 01:16:46 -070024#include <env.h>
25#include <env_internal.h>
Peng Faneae4de22018-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 Fan45d843a2020-05-11 15:14:04 +080030#include <linux/arm-smccc.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060031#include <linux/bitops.h>
Peng Faneae4de22018-01-10 13:20:37 +080032
33DECLARE_GLOBAL_DATA_PTR;
34
Stefano Babicf8b509b2019-09-20 08:47:53 +020035#if defined(CONFIG_IMX_HAB)
Peng Faneae4de22018-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 Fanc0e47fc2019-12-27 10:19:42 +080069 if (is_imx8mm() || is_imx8mn() || is_imx8mp())
Peng Fan99047fc2019-08-27 06:25:30 +000070 setbits_le32(&gpr->gpr[10], BIT(1));
Ye Li4c97c462019-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 Faneae4de22018-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 Bisson5c72a452018-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 Vasutb1738e02021-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 Bisson5c72a452018-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 Faneae4de22018-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 Fanb749b5e2019-08-27 06:25:27 +0000141 .size = PHYS_SDRAM_SIZE,
Peng Faneae4de22018-01-10 13:20:37 +0800142 .attrs = PTE_BLOCK_MEMTYPE(MT_NORMAL) |
143 PTE_BLOCK_OUTER_SHARE
Peng Fanb749b5e2019-08-27 06:25:27 +0000144#ifdef PHYS_SDRAM_2_SIZE
Peng Faneae4de22018-01-10 13:20:37 +0800145 }, {
146 /* DRAM2 */
147 .virt = 0x100000000UL,
148 .phys = 0x100000000UL,
Peng Fanb749b5e2019-08-27 06:25:27 +0000149 .size = PHYS_SDRAM_2_SIZE,
Peng Faneae4de22018-01-10 13:20:37 +0800150 .attrs = PTE_BLOCK_MEMTYPE(MT_NORMAL) |
151 PTE_BLOCK_OUTER_SHARE
Peng Fanb749b5e2019-08-27 06:25:27 +0000152#endif
Peng Faneae4de22018-01-10 13:20:37 +0800153 }, {
Peng Fanfa35c3d2020-07-09 15:26:06 +0800154 /* empty entrie to split table entry 5 if needed when TEEs are used */
155 0,
156 }, {
Peng Faneae4de22018-01-10 13:20:37 +0800157 /* List terminator */
158 0,
159 }
160};
161
162struct mm_region *mem_map = imx8m_mem_map;
163
Marek Vasute48aac02021-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 Fanb749b5e2019-08-27 06:25:27 +0000175void enable_caches(void)
176{
Peng Fanfa35c3d2020-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 Vasute48aac02021-02-27 14:59:00 +0100188 int entry = imx8m_find_dram_entry_in_mem_map();
Peng Fanfa35c3d2020-07-09 15:26:06 +0800189 u64 attrs = imx8m_mem_map[entry].attrs;
190
Marek Vasute48aac02021-02-27 14:59:00 +0100191 while (i < CONFIG_NR_DRAM_BANKS &&
192 entry < ARRAY_SIZE(imx8m_mem_map)) {
Peng Fanfa35c3d2020-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 Fanb749b5e2019-08-27 06:25:27 +0000204
205 icache_enable();
206 dcache_enable();
207}
208
Peng Fanfa35c3d2020-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 Vasute48aac02021-02-27 14:59:00 +0100220 unsigned int entry = imx8m_find_dram_entry_in_mem_map();
Peng Fanfa35c3d2020-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 Harvey32927bc2020-09-25 08:08:35 -0700234 /* also update the SDRAM size in the mem_map used externally */
Marek Vasute48aac02021-02-27 14:59:00 +0100235 imx8m_mem_map[entry].size = sdram_size;
Tim Harvey32927bc2020-09-25 08:08:35 -0700236
Peng Fanfa35c3d2020-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
Frieder Schrempf159879e2021-06-07 14:36:44 +0200299ulong board_get_usable_ram_top(ulong total_size)
300{
301 /*
302 * Some IPs have their accessible address space restricted by
303 * the interconnect. Let's make sure U-Boot only ever uses the
304 * space below the 4G address boundary (which is 3GiB big),
305 * even when the effective available memory is bigger.
306 */
307 if (PHYS_SDRAM + gd->ram_size > 0x80000000)
308 return 0x80000000;
309
310 return PHYS_SDRAM + gd->ram_size;
311}
312
Peng Fan1caffdf2019-08-27 06:25:17 +0000313static u32 get_cpu_variant_type(u32 type)
314{
315 struct ocotp_regs *ocotp = (struct ocotp_regs *)OCOTP_BASE_ADDR;
316 struct fuse_bank *bank = &ocotp->bank[1];
317 struct fuse_bank1_regs *fuse =
318 (struct fuse_bank1_regs *)bank->fuse_regs;
319
320 u32 value = readl(&fuse->tester4);
321
Peng Fan67815082020-02-05 17:34:54 +0800322 if (type == MXC_CPU_IMX8MQ) {
323 if ((value & 0x3) == 0x2)
324 return MXC_CPU_IMX8MD;
325 else if (value & 0x200000)
326 return MXC_CPU_IMX8MQL;
327
328 } else if (type == MXC_CPU_IMX8MM) {
Peng Fan1caffdf2019-08-27 06:25:17 +0000329 switch (value & 0x3) {
330 case 2:
331 if (value & 0x1c0000)
332 return MXC_CPU_IMX8MMDL;
333 else
334 return MXC_CPU_IMX8MMD;
335 case 3:
336 if (value & 0x1c0000)
337 return MXC_CPU_IMX8MMSL;
338 else
339 return MXC_CPU_IMX8MMS;
340 default:
341 if (value & 0x1c0000)
342 return MXC_CPU_IMX8MML;
343 break;
344 }
Peng Fan1a07d912020-02-05 17:39:27 +0800345 } else if (type == MXC_CPU_IMX8MN) {
346 switch (value & 0x3) {
347 case 2:
Ye Li715180e2021-03-19 15:57:11 +0800348 if (value & 0x1000000) {
349 if (value & 0x10000000) /* MIPI DSI */
350 return MXC_CPU_IMX8MNUD;
351 else
352 return MXC_CPU_IMX8MNDL;
353 } else {
Peng Fan1a07d912020-02-05 17:39:27 +0800354 return MXC_CPU_IMX8MND;
Ye Li715180e2021-03-19 15:57:11 +0800355 }
Peng Fan1a07d912020-02-05 17:39:27 +0800356 case 3:
Ye Li715180e2021-03-19 15:57:11 +0800357 if (value & 0x1000000) {
358 if (value & 0x10000000) /* MIPI DSI */
359 return MXC_CPU_IMX8MNUS;
360 else
361 return MXC_CPU_IMX8MNSL;
362 } else {
Peng Fan1a07d912020-02-05 17:39:27 +0800363 return MXC_CPU_IMX8MNS;
Ye Li715180e2021-03-19 15:57:11 +0800364 }
Peng Fan1a07d912020-02-05 17:39:27 +0800365 default:
Ye Li715180e2021-03-19 15:57:11 +0800366 if (value & 0x1000000) {
367 if (value & 0x10000000) /* MIPI DSI */
368 return MXC_CPU_IMX8MNUQ;
369 else
370 return MXC_CPU_IMX8MNL;
371 }
Peng Fan1a07d912020-02-05 17:39:27 +0800372 break;
373 }
Ye Lid2d754f2020-04-20 20:12:54 -0700374 } else if (type == MXC_CPU_IMX8MP) {
375 u32 value0 = readl(&fuse->tester3);
376 u32 flag = 0;
377
378 if ((value0 & 0xc0000) == 0x80000)
379 return MXC_CPU_IMX8MPD;
380
381 /* vpu disabled */
382 if ((value0 & 0x43000000) == 0x43000000)
383 flag = 1;
384
385 /* npu disabled*/
386 if ((value & 0x8) == 0x8)
387 flag |= (1 << 1);
388
389 /* isp disabled */
390 if ((value & 0x3) == 0x3)
391 flag |= (1 << 2);
392
393 switch (flag) {
394 case 7:
395 return MXC_CPU_IMX8MPL;
Ye Lid2d754f2020-04-20 20:12:54 -0700396 case 2:
397 return MXC_CPU_IMX8MP6;
Ye Lid2d754f2020-04-20 20:12:54 -0700398 default:
399 break;
400 }
401
Peng Fan1caffdf2019-08-27 06:25:17 +0000402 }
403
404 return type;
405}
406
Peng Faneae4de22018-01-10 13:20:37 +0800407u32 get_cpu_rev(void)
408{
409 struct anamix_pll *ana_pll = (struct anamix_pll *)ANATOP_BASE_ADDR;
410 u32 reg = readl(&ana_pll->digprog);
411 u32 type = (reg >> 16) & 0xff;
Peng Fan1caffdf2019-08-27 06:25:17 +0000412 u32 major_low = (reg >> 8) & 0xff;
Peng Faneae4de22018-01-10 13:20:37 +0800413 u32 rom_version;
414
415 reg &= 0xff;
416
Peng Fan69cec072019-12-27 10:14:02 +0800417 /* iMX8MP */
418 if (major_low == 0x43) {
Ye Lid2d754f2020-04-20 20:12:54 -0700419 type = get_cpu_variant_type(MXC_CPU_IMX8MP);
Peng Fan69cec072019-12-27 10:14:02 +0800420 } else if (major_low == 0x42) {
421 /* iMX8MN */
Peng Fan1a07d912020-02-05 17:39:27 +0800422 type = get_cpu_variant_type(MXC_CPU_IMX8MN);
Peng Fan5d2f2062019-06-27 17:23:49 +0800423 } else if (major_low == 0x41) {
Peng Fan1caffdf2019-08-27 06:25:17 +0000424 type = get_cpu_variant_type(MXC_CPU_IMX8MM);
425 } else {
426 if (reg == CHIP_REV_1_0) {
427 /*
Peng Fanc23fbdd2019-10-16 10:24:17 +0000428 * For B0 chip, the DIGPROG is not updated,
429 * it is still TO1.0. we have to check ROM
430 * version or OCOTP_READ_FUSE_DATA.
431 * 0xff0055aa is magic number for B1.
Peng Fan1caffdf2019-08-27 06:25:17 +0000432 */
Peng Fanc23fbdd2019-10-16 10:24:17 +0000433 if (readl((void __iomem *)(OCOTP_BASE_ADDR + 0x40)) == 0xff0055aa) {
Ye Lic963ed12021-03-19 15:57:16 +0800434 /*
435 * B2 uses same DIGPROG and OCOTP_READ_FUSE_DATA value with B1,
436 * so have to check ROM to distinguish them
437 */
438 rom_version = readl((void __iomem *)ROM_VERSION_B0);
439 rom_version &= 0xff;
440 if (rom_version == CHIP_REV_2_2)
441 reg = CHIP_REV_2_2;
442 else
443 reg = CHIP_REV_2_1;
Peng Fanc23fbdd2019-10-16 10:24:17 +0000444 } else {
445 rom_version =
446 readl((void __iomem *)ROM_VERSION_A0);
447 if (rom_version != CHIP_REV_1_0) {
448 rom_version = readl((void __iomem *)ROM_VERSION_B0);
Patrick Wildtd4a78b92019-11-19 09:42:06 +0100449 rom_version &= 0xff;
Peng Fanc23fbdd2019-10-16 10:24:17 +0000450 if (rom_version == CHIP_REV_2_0)
451 reg = CHIP_REV_2_0;
452 }
Peng Fan1caffdf2019-08-27 06:25:17 +0000453 }
Peng Faneae4de22018-01-10 13:20:37 +0800454 }
Peng Fan67815082020-02-05 17:34:54 +0800455
456 type = get_cpu_variant_type(type);
Peng Faneae4de22018-01-10 13:20:37 +0800457 }
458
459 return (type << 12) | reg;
460}
461
462static void imx_set_wdog_powerdown(bool enable)
463{
464 struct wdog_regs *wdog1 = (struct wdog_regs *)WDOG1_BASE_ADDR;
465 struct wdog_regs *wdog2 = (struct wdog_regs *)WDOG2_BASE_ADDR;
466 struct wdog_regs *wdog3 = (struct wdog_regs *)WDOG3_BASE_ADDR;
467
468 /* Write to the PDE (Power Down Enable) bit */
469 writew(enable, &wdog1->wmcr);
470 writew(enable, &wdog2->wmcr);
471 writew(enable, &wdog3->wmcr);
472}
473
Peng Fanc98e0322019-08-27 06:25:58 +0000474int arch_cpu_init_dm(void)
475{
476 struct udevice *dev;
477 int ret;
478
Peng Fan3c073342019-10-16 03:01:51 +0000479 if (CONFIG_IS_ENABLED(CLK)) {
480 ret = uclass_get_device_by_name(UCLASS_CLK,
481 "clock-controller@30380000",
482 &dev);
483 if (ret < 0) {
484 printf("Failed to find clock node. Check device tree\n");
485 return ret;
486 }
Peng Fanc98e0322019-08-27 06:25:58 +0000487 }
488
489 return 0;
490}
491
Peng Faneae4de22018-01-10 13:20:37 +0800492int arch_cpu_init(void)
493{
Peng Fanc0b30d72019-04-17 09:41:16 +0000494 struct ocotp_regs *ocotp = (struct ocotp_regs *)OCOTP_BASE_ADDR;
Peng Faneae4de22018-01-10 13:20:37 +0800495 /*
Peng Fand0ca2892019-08-27 06:25:37 +0000496 * ROM might disable clock for SCTR,
497 * enable the clock before timer_init.
498 */
499 if (IS_ENABLED(CONFIG_SPL_BUILD))
500 clock_enable(CCGR_SCTR, 1);
501 /*
Peng Faneae4de22018-01-10 13:20:37 +0800502 * Init timer at very early state, because sscg pll setting
503 * will use it
504 */
505 timer_init();
506
507 if (IS_ENABLED(CONFIG_SPL_BUILD)) {
508 clock_init();
509 imx_set_wdog_powerdown(false);
Peng Fan9cf2aa32020-07-09 13:52:41 +0800510
511 if (is_imx8md() || is_imx8mmd() || is_imx8mmdl() || is_imx8mms() ||
512 is_imx8mmsl() || is_imx8mnd() || is_imx8mndl() || is_imx8mns() ||
Ye Li715180e2021-03-19 15:57:11 +0800513 is_imx8mnsl() || is_imx8mpd() || is_imx8mnud() || is_imx8mnus()) {
Peng Fan9cf2aa32020-07-09 13:52:41 +0800514 /* Power down cpu core 1, 2 and 3 for iMX8M Dual core or Single core */
515 struct pgc_reg *pgc_core1 = (struct pgc_reg *)(GPC_BASE_ADDR + 0x840);
516 struct pgc_reg *pgc_core2 = (struct pgc_reg *)(GPC_BASE_ADDR + 0x880);
517 struct pgc_reg *pgc_core3 = (struct pgc_reg *)(GPC_BASE_ADDR + 0x8C0);
518 struct gpc_reg *gpc = (struct gpc_reg *)GPC_BASE_ADDR;
519
520 writel(0x1, &pgc_core2->pgcr);
521 writel(0x1, &pgc_core3->pgcr);
Ye Li715180e2021-03-19 15:57:11 +0800522 if (is_imx8mms() || is_imx8mmsl() || is_imx8mns() || is_imx8mnsl() || is_imx8mnus()) {
Peng Fan9cf2aa32020-07-09 13:52:41 +0800523 writel(0x1, &pgc_core1->pgcr);
524 writel(0xE, &gpc->cpu_pgc_dn_trg);
525 } else {
526 writel(0xC, &gpc->cpu_pgc_dn_trg);
527 }
528 }
Peng Faneae4de22018-01-10 13:20:37 +0800529 }
530
Peng Fanc0b30d72019-04-17 09:41:16 +0000531 if (is_imx8mq()) {
532 clock_enable(CCGR_OCOTP, 1);
533 if (readl(&ocotp->ctrl) & 0x200)
534 writel(0x200, &ocotp->ctrl_clr);
535 }
536
Peng Faneae4de22018-01-10 13:20:37 +0800537 return 0;
538}
539
Peng Fanc9823b02019-09-16 03:09:36 +0000540#if defined(CONFIG_IMX8MN) || defined(CONFIG_IMX8MP)
541struct rom_api *g_rom_api = (struct rom_api *)0x980;
542
543enum boot_device get_boot_device(void)
544{
545 volatile gd_t *pgd = gd;
546 int ret;
547 u32 boot;
548 u16 boot_type;
549 u8 boot_instance;
550 enum boot_device boot_dev = SD1_BOOT;
551
552 ret = g_rom_api->query_boot_infor(QUERY_BT_DEV, &boot,
553 ((uintptr_t)&boot) ^ QUERY_BT_DEV);
Marek Behúna8cf1552021-05-20 13:24:10 +0200554 set_gd(pgd);
Peng Fanc9823b02019-09-16 03:09:36 +0000555
556 if (ret != ROM_API_OKAY) {
557 puts("ROMAPI: failure at query_boot_info\n");
558 return -1;
559 }
560
561 boot_type = boot >> 16;
562 boot_instance = (boot >> 8) & 0xff;
563
564 switch (boot_type) {
565 case BT_DEV_TYPE_SD:
566 boot_dev = boot_instance + SD1_BOOT;
567 break;
568 case BT_DEV_TYPE_MMC:
569 boot_dev = boot_instance + MMC1_BOOT;
570 break;
571 case BT_DEV_TYPE_NAND:
572 boot_dev = NAND_BOOT;
573 break;
574 case BT_DEV_TYPE_FLEXSPINOR:
575 boot_dev = QSPI_BOOT;
576 break;
577 case BT_DEV_TYPE_USB:
578 boot_dev = USB_BOOT;
579 break;
580 default:
581 break;
582 }
583
584 return boot_dev;
585}
586#endif
587
Marek Vasut520ded02021-07-03 04:55:33 +0200588#if defined(CONFIG_IMX8M)
589#include <spl.h>
590int spl_mmc_emmc_boot_partition(struct mmc *mmc)
591{
592 u32 *rom_log_addr = (u32 *)0x9e0;
593 u32 *rom_log;
594 u8 event_id;
595 int i, part;
596
597 part = default_spl_mmc_emmc_boot_partition(mmc);
598
599 /* If the ROM event log pointer is not valid. */
600 if (*rom_log_addr < 0x900000 || *rom_log_addr >= 0xb00000 ||
601 *rom_log_addr & 0x3)
602 return part;
603
604 /* Parse the ROM event ID version 2 log */
605 rom_log = (u32 *)(uintptr_t)(*rom_log_addr);
606 for (i = 0; i < 128; i++) {
607 event_id = rom_log[i] >> 24;
608 switch (event_id) {
609 case 0x00: /* End of list */
610 return part;
611 /* Log entries with 1 parameter, skip 1 */
612 case 0x80: /* Start to perform the device initialization */
613 case 0x81: /* The boot device initialization completes */
614 case 0x8f: /* The boot device initialization fails */
615 case 0x90: /* Start to read data from boot device */
616 case 0x91: /* Reading data from boot device completes */
617 case 0x9f: /* Reading data from boot device fails */
618 i += 1;
619 continue;
620 /* Log entries with 2 parameters, skip 2 */
621 case 0xa0: /* Image authentication result */
622 case 0xc0: /* Jump to the boot image soon */
623 i += 2;
624 continue;
625 /* Boot from the secondary boot image */
626 case 0x51:
627 /*
628 * Swap the eMMC boot partitions in case there was a
629 * fallback event (i.e. primary image was corrupted
630 * and that corruption was recognized by the BootROM),
631 * so the SPL loads the rest of the U-Boot from the
632 * correct eMMC boot partition, since the BootROM
633 * leaves the boot partition set to the corrupted one.
634 */
635 if (part == 1)
636 part = 2;
637 else if (part == 2)
638 part = 1;
639 continue;
640 default:
641 continue;
642 }
643 }
644
645 return part;
646}
647#endif
648
Peng Faneae4de22018-01-10 13:20:37 +0800649bool is_usb_boot(void)
650{
651 return get_boot_device() == USB_BOOT;
652}
653
654#ifdef CONFIG_OF_SYSTEM_SETUP
Peng Fan435dc122020-07-09 14:06:49 +0800655bool check_fdt_new_path(void *blob)
656{
657 const char *soc_path = "/soc@0";
658 int nodeoff;
659
660 nodeoff = fdt_path_offset(blob, soc_path);
661 if (nodeoff < 0)
662 return false;
663
664 return true;
665}
666
667static int disable_fdt_nodes(void *blob, const char *const nodes_path[], int size_array)
668{
669 int i = 0;
670 int rc;
671 int nodeoff;
672 const char *status = "disabled";
673
674 for (i = 0; i < size_array; i++) {
675 nodeoff = fdt_path_offset(blob, nodes_path[i]);
676 if (nodeoff < 0)
677 continue; /* Not found, skip it */
678
679 printf("Found %s node\n", nodes_path[i]);
680
681add_status:
682 rc = fdt_setprop(blob, nodeoff, "status", status, strlen(status) + 1);
683 if (rc) {
684 if (rc == -FDT_ERR_NOSPACE) {
685 rc = fdt_increase_size(blob, 512);
686 if (!rc)
687 goto add_status;
688 }
689 printf("Unable to update property %s:%s, err=%s\n",
690 nodes_path[i], "status", fdt_strerror(rc));
691 } else {
692 printf("Modify %s:%s disabled\n",
693 nodes_path[i], "status");
694 }
695 }
696
697 return 0;
698}
699
700#ifdef CONFIG_IMX8MQ
701bool check_dcss_fused(void)
702{
703 struct ocotp_regs *ocotp = (struct ocotp_regs *)OCOTP_BASE_ADDR;
704 struct fuse_bank *bank = &ocotp->bank[1];
705 struct fuse_bank1_regs *fuse =
706 (struct fuse_bank1_regs *)bank->fuse_regs;
707 u32 value = readl(&fuse->tester4);
708
709 if (value & 0x4000000)
710 return true;
711
712 return false;
713}
714
715static int disable_mipi_dsi_nodes(void *blob)
716{
717 static const char * const nodes_path[] = {
718 "/mipi_dsi@30A00000",
719 "/mipi_dsi_bridge@30A00000",
720 "/dsi_phy@30A00300",
721 "/soc@0/bus@30800000/mipi_dsi@30a00000",
Peng Fan7d4195c2021-03-19 15:57:13 +0800722 "/soc@0/bus@30800000/dphy@30a00300",
723 "/soc@0/bus@30800000/mipi-dsi@30a00000",
Peng Fan435dc122020-07-09 14:06:49 +0800724 };
725
726 return disable_fdt_nodes(blob, nodes_path, ARRAY_SIZE(nodes_path));
727}
728
729static int disable_dcss_nodes(void *blob)
730{
731 static const char * const nodes_path[] = {
732 "/dcss@0x32e00000",
733 "/dcss@32e00000",
734 "/hdmi@32c00000",
735 "/hdmi_cec@32c33800",
736 "/hdmi_drm@32c00000",
737 "/display-subsystem",
738 "/sound-hdmi",
739 "/sound-hdmi-arc",
740 "/soc@0/bus@32c00000/display-controller@32e00000",
741 "/soc@0/bus@32c00000/hdmi@32c00000",
742 };
743
744 return disable_fdt_nodes(blob, nodes_path, ARRAY_SIZE(nodes_path));
745}
746
747static int check_mipi_dsi_nodes(void *blob)
748{
749 static const char * const lcdif_path[] = {
750 "/lcdif@30320000",
Peng Fan7d4195c2021-03-19 15:57:13 +0800751 "/soc@0/bus@30000000/lcdif@30320000",
752 "/soc@0/bus@30000000/lcd-controller@30320000"
Peng Fan435dc122020-07-09 14:06:49 +0800753 };
754 static const char * const mipi_dsi_path[] = {
755 "/mipi_dsi@30A00000",
756 "/soc@0/bus@30800000/mipi_dsi@30a00000"
757 };
758 static const char * const lcdif_ep_path[] = {
759 "/lcdif@30320000/port@0/mipi-dsi-endpoint",
Peng Fan7d4195c2021-03-19 15:57:13 +0800760 "/soc@0/bus@30000000/lcdif@30320000/port@0/endpoint",
761 "/soc@0/bus@30000000/lcd-controller@30320000/port@0/endpoint"
Peng Fan435dc122020-07-09 14:06:49 +0800762 };
763 static const char * const mipi_dsi_ep_path[] = {
764 "/mipi_dsi@30A00000/port@1/endpoint",
Peng Fan7d4195c2021-03-19 15:57:13 +0800765 "/soc@0/bus@30800000/mipi_dsi@30a00000/ports/port@0/endpoint",
766 "/soc@0/bus@30800000/mipi-dsi@30a00000/ports/port@0/endpoint@0"
Peng Fan435dc122020-07-09 14:06:49 +0800767 };
768
769 int lookup_node;
770 int nodeoff;
771 bool new_path = check_fdt_new_path(blob);
772 int i = new_path ? 1 : 0;
773
774 nodeoff = fdt_path_offset(blob, lcdif_path[i]);
775 if (nodeoff < 0 || !fdtdec_get_is_enabled(blob, nodeoff)) {
776 /*
777 * If can't find lcdif node or lcdif node is disabled,
778 * then disable all mipi dsi, since they only can input
779 * from DCSS
780 */
781 return disable_mipi_dsi_nodes(blob);
782 }
783
784 nodeoff = fdt_path_offset(blob, mipi_dsi_path[i]);
785 if (nodeoff < 0 || !fdtdec_get_is_enabled(blob, nodeoff))
786 return 0;
787
788 nodeoff = fdt_path_offset(blob, lcdif_ep_path[i]);
789 if (nodeoff < 0) {
790 /*
791 * If can't find lcdif endpoint, then disable all mipi dsi,
792 * since they only can input from DCSS
793 */
794 return disable_mipi_dsi_nodes(blob);
795 }
796
797 lookup_node = fdtdec_lookup_phandle(blob, nodeoff, "remote-endpoint");
798 nodeoff = fdt_path_offset(blob, mipi_dsi_ep_path[i]);
799
800 if (nodeoff > 0 && nodeoff == lookup_node)
801 return 0;
802
803 return disable_mipi_dsi_nodes(blob);
804}
805#endif
806
807int disable_vpu_nodes(void *blob)
808{
809 static const char * const nodes_path_8mq[] = {
810 "/vpu@38300000",
811 "/soc@0/vpu@38300000"
812 };
813
814 static const char * const nodes_path_8mm[] = {
815 "/vpu_g1@38300000",
816 "/vpu_g2@38310000",
817 "/vpu_h1@38320000"
818 };
819
820 static const char * const nodes_path_8mp[] = {
821 "/vpu_g1@38300000",
822 "/vpu_g2@38310000",
823 "/vpu_vc8000e@38320000"
824 };
825
826 if (is_imx8mq())
827 return disable_fdt_nodes(blob, nodes_path_8mq, ARRAY_SIZE(nodes_path_8mq));
828 else if (is_imx8mm())
829 return disable_fdt_nodes(blob, nodes_path_8mm, ARRAY_SIZE(nodes_path_8mm));
830 else if (is_imx8mp())
831 return disable_fdt_nodes(blob, nodes_path_8mp, ARRAY_SIZE(nodes_path_8mp));
832 else
833 return -EPERM;
834}
835
Ye Liee337ce2021-03-19 15:57:09 +0800836#ifdef CONFIG_IMX8MN_LOW_DRIVE_MODE
837static int low_drive_gpu_freq(void *blob)
838{
839 static const char *nodes_path_8mn[] = {
840 "/gpu@38000000",
841 "/soc@0/gpu@38000000"
842 };
843
844 int nodeoff, cnt, i;
845 u32 assignedclks[7];
846
847 nodeoff = fdt_path_offset(blob, nodes_path_8mn[0]);
848 if (nodeoff < 0)
849 return nodeoff;
850
851 cnt = fdtdec_get_int_array_count(blob, nodeoff, "assigned-clock-rates", assignedclks, 7);
852 if (cnt < 0)
853 return cnt;
854
855 if (cnt != 7)
856 printf("Warning: %s, assigned-clock-rates count %d\n", nodes_path_8mn[0], cnt);
857
858 assignedclks[cnt - 1] = 200000000;
859 assignedclks[cnt - 2] = 200000000;
860
861 for (i = 0; i < cnt; i++) {
862 debug("<%u>, ", assignedclks[i]);
863 assignedclks[i] = cpu_to_fdt32(assignedclks[i]);
864 }
865 debug("\n");
866
867 return fdt_setprop(blob, nodeoff, "assigned-clock-rates", &assignedclks, sizeof(assignedclks));
868}
869#endif
870
Peng Fan435dc122020-07-09 14:06:49 +0800871int disable_gpu_nodes(void *blob)
872{
873 static const char * const nodes_path_8mn[] = {
Peng Fan7d4195c2021-03-19 15:57:13 +0800874 "/gpu@38000000",
875 "/soc@/gpu@38000000"
Peng Fan435dc122020-07-09 14:06:49 +0800876 };
877
878 return disable_fdt_nodes(blob, nodes_path_8mn, ARRAY_SIZE(nodes_path_8mn));
879}
880
881int disable_npu_nodes(void *blob)
882{
883 static const char * const nodes_path_8mp[] = {
884 "/vipsi@38500000"
885 };
886
887 return disable_fdt_nodes(blob, nodes_path_8mp, ARRAY_SIZE(nodes_path_8mp));
888}
889
890int disable_isp_nodes(void *blob)
891{
892 static const char * const nodes_path_8mp[] = {
893 "/soc@0/bus@32c00000/camera/isp@32e10000",
894 "/soc@0/bus@32c00000/camera/isp@32e20000"
895 };
896
897 return disable_fdt_nodes(blob, nodes_path_8mp, ARRAY_SIZE(nodes_path_8mp));
898}
899
900int disable_dsp_nodes(void *blob)
901{
902 static const char * const nodes_path_8mp[] = {
903 "/dsp@3b6e8000"
904 };
905
906 return disable_fdt_nodes(blob, nodes_path_8mp, ARRAY_SIZE(nodes_path_8mp));
907}
908
Ye Li26517af2021-03-19 15:57:12 +0800909static void disable_thermal_cpu_nodes(void *blob, u32 disabled_cores)
910{
911 static const char * const thermal_path[] = {
912 "/thermal-zones/cpu-thermal/cooling-maps/map0"
913 };
914
915 int nodeoff, cnt, i, ret, j;
916 u32 cooling_dev[12];
917
918 for (i = 0; i < ARRAY_SIZE(thermal_path); i++) {
919 nodeoff = fdt_path_offset(blob, thermal_path[i]);
920 if (nodeoff < 0)
921 continue; /* Not found, skip it */
922
923 cnt = fdtdec_get_int_array_count(blob, nodeoff, "cooling-device", cooling_dev, 12);
924 if (cnt < 0)
925 continue;
926
927 if (cnt != 12)
928 printf("Warning: %s, cooling-device count %d\n", thermal_path[i], cnt);
929
930 for (j = 0; j < cnt; j++)
931 cooling_dev[j] = cpu_to_fdt32(cooling_dev[j]);
932
933 ret = fdt_setprop(blob, nodeoff, "cooling-device", &cooling_dev,
934 sizeof(u32) * (12 - disabled_cores * 3));
935 if (ret < 0) {
936 printf("Warning: %s, cooling-device setprop failed %d\n",
937 thermal_path[i], ret);
938 continue;
939 }
940
941 printf("Update node %s, cooling-device prop\n", thermal_path[i]);
942 }
943}
944
945static void disable_pmu_cpu_nodes(void *blob, u32 disabled_cores)
946{
947 static const char * const pmu_path[] = {
948 "/pmu"
949 };
950
951 int nodeoff, cnt, i, ret, j;
952 u32 irq_affinity[4];
953
954 for (i = 0; i < ARRAY_SIZE(pmu_path); i++) {
955 nodeoff = fdt_path_offset(blob, pmu_path[i]);
956 if (nodeoff < 0)
957 continue; /* Not found, skip it */
958
959 cnt = fdtdec_get_int_array_count(blob, nodeoff, "interrupt-affinity",
960 irq_affinity, 4);
961 if (cnt < 0)
962 continue;
963
964 if (cnt != 4)
965 printf("Warning: %s, interrupt-affinity count %d\n", pmu_path[i], cnt);
966
967 for (j = 0; j < cnt; j++)
968 irq_affinity[j] = cpu_to_fdt32(irq_affinity[j]);
969
970 ret = fdt_setprop(blob, nodeoff, "interrupt-affinity", &irq_affinity,
971 sizeof(u32) * (4 - disabled_cores));
972 if (ret < 0) {
973 printf("Warning: %s, interrupt-affinity setprop failed %d\n",
974 pmu_path[i], ret);
975 continue;
976 }
977
978 printf("Update node %s, interrupt-affinity prop\n", pmu_path[i]);
979 }
980}
981
Peng Fan435dc122020-07-09 14:06:49 +0800982static int disable_cpu_nodes(void *blob, u32 disabled_cores)
983{
984 static const char * const nodes_path[] = {
985 "/cpus/cpu@1",
986 "/cpus/cpu@2",
987 "/cpus/cpu@3",
988 };
989 u32 i = 0;
990 int rc;
991 int nodeoff;
992
993 if (disabled_cores > 3)
994 return -EINVAL;
995
996 i = 3 - disabled_cores;
997
998 for (; i < 3; i++) {
999 nodeoff = fdt_path_offset(blob, nodes_path[i]);
1000 if (nodeoff < 0)
1001 continue; /* Not found, skip it */
1002
1003 debug("Found %s node\n", nodes_path[i]);
1004
1005 rc = fdt_del_node(blob, nodeoff);
1006 if (rc < 0) {
1007 printf("Unable to delete node %s, err=%s\n",
1008 nodes_path[i], fdt_strerror(rc));
1009 } else {
1010 printf("Delete node %s\n", nodes_path[i]);
1011 }
1012 }
1013
Ye Li26517af2021-03-19 15:57:12 +08001014 disable_thermal_cpu_nodes(blob, disabled_cores);
1015 disable_pmu_cpu_nodes(blob, disabled_cores);
1016
Peng Fan435dc122020-07-09 14:06:49 +08001017 return 0;
1018}
1019
Masahiro Yamadaf7ed78b2020-06-26 15:13:33 +09001020int ft_system_setup(void *blob, struct bd_info *bd)
Peng Faneae4de22018-01-10 13:20:37 +08001021{
Peng Fan435dc122020-07-09 14:06:49 +08001022#ifdef CONFIG_IMX8MQ
Peng Faneae4de22018-01-10 13:20:37 +08001023 int i = 0;
1024 int rc;
1025 int nodeoff;
1026
Peng Fan435dc122020-07-09 14:06:49 +08001027 if (get_boot_device() == USB_BOOT) {
1028 disable_dcss_nodes(blob);
1029
1030 bool new_path = check_fdt_new_path(blob);
1031 int v = new_path ? 1 : 0;
1032 static const char * const usb_dwc3_path[] = {
1033 "/usb@38100000/dwc3",
1034 "/soc@0/usb@38100000"
1035 };
1036
1037 nodeoff = fdt_path_offset(blob, usb_dwc3_path[v]);
1038 if (nodeoff >= 0) {
1039 const char *speed = "high-speed";
1040
1041 printf("Found %s node\n", usb_dwc3_path[v]);
1042
1043usb_modify_speed:
1044
1045 rc = fdt_setprop(blob, nodeoff, "maximum-speed", speed, strlen(speed) + 1);
1046 if (rc) {
1047 if (rc == -FDT_ERR_NOSPACE) {
1048 rc = fdt_increase_size(blob, 512);
1049 if (!rc)
1050 goto usb_modify_speed;
1051 }
1052 printf("Unable to set property %s:%s, err=%s\n",
1053 usb_dwc3_path[v], "maximum-speed", fdt_strerror(rc));
1054 } else {
1055 printf("Modify %s:%s = %s\n",
1056 usb_dwc3_path[v], "maximum-speed", speed);
1057 }
1058 } else {
1059 printf("Can't found %s node\n", usb_dwc3_path[v]);
1060 }
1061 }
1062
Peng Faneae4de22018-01-10 13:20:37 +08001063 /* Disable the CPU idle for A0 chip since the HW does not support it */
1064 if (is_soc_rev(CHIP_REV_1_0)) {
1065 static const char * const nodes_path[] = {
1066 "/cpus/cpu@0",
1067 "/cpus/cpu@1",
1068 "/cpus/cpu@2",
1069 "/cpus/cpu@3",
1070 };
1071
1072 for (i = 0; i < ARRAY_SIZE(nodes_path); i++) {
1073 nodeoff = fdt_path_offset(blob, nodes_path[i]);
1074 if (nodeoff < 0)
1075 continue; /* Not found, skip it */
1076
Marek Vasute2e7a772020-04-24 21:37:33 +02001077 debug("Found %s node\n", nodes_path[i]);
Peng Faneae4de22018-01-10 13:20:37 +08001078
1079 rc = fdt_delprop(blob, nodeoff, "cpu-idle-states");
Marek Vasute2e7a772020-04-24 21:37:33 +02001080 if (rc == -FDT_ERR_NOTFOUND)
1081 continue;
Peng Faneae4de22018-01-10 13:20:37 +08001082 if (rc) {
1083 printf("Unable to update property %s:%s, err=%s\n",
1084 nodes_path[i], "status", fdt_strerror(rc));
1085 return rc;
1086 }
1087
Marek Vasute2e7a772020-04-24 21:37:33 +02001088 debug("Remove %s:%s\n", nodes_path[i],
Peng Faneae4de22018-01-10 13:20:37 +08001089 "cpu-idle-states");
1090 }
1091 }
1092
Peng Fan435dc122020-07-09 14:06:49 +08001093 if (is_imx8mql()) {
1094 disable_vpu_nodes(blob);
1095 if (check_dcss_fused()) {
1096 printf("DCSS is fused\n");
1097 disable_dcss_nodes(blob);
1098 check_mipi_dsi_nodes(blob);
1099 }
1100 }
1101
1102 if (is_imx8md())
1103 disable_cpu_nodes(blob, 2);
1104
1105#elif defined(CONFIG_IMX8MM)
1106 if (is_imx8mml() || is_imx8mmdl() || is_imx8mmsl())
1107 disable_vpu_nodes(blob);
1108
1109 if (is_imx8mmd() || is_imx8mmdl())
1110 disable_cpu_nodes(blob, 2);
1111 else if (is_imx8mms() || is_imx8mmsl())
1112 disable_cpu_nodes(blob, 3);
1113
1114#elif defined(CONFIG_IMX8MN)
1115 if (is_imx8mnl() || is_imx8mndl() || is_imx8mnsl())
1116 disable_gpu_nodes(blob);
Ye Liee337ce2021-03-19 15:57:09 +08001117#ifdef CONFIG_IMX8MN_LOW_DRIVE_MODE
1118 else {
1119 int ldm_gpu = low_drive_gpu_freq(blob);
1120
1121 if (ldm_gpu < 0)
1122 printf("Update GPU node assigned-clock-rates failed\n");
1123 else
1124 printf("Update GPU node assigned-clock-rates ok\n");
1125 }
1126#endif
Peng Fan435dc122020-07-09 14:06:49 +08001127
Ye Li715180e2021-03-19 15:57:11 +08001128 if (is_imx8mnd() || is_imx8mndl() || is_imx8mnud())
Peng Fan435dc122020-07-09 14:06:49 +08001129 disable_cpu_nodes(blob, 2);
Ye Li715180e2021-03-19 15:57:11 +08001130 else if (is_imx8mns() || is_imx8mnsl() || is_imx8mnus())
Peng Fan435dc122020-07-09 14:06:49 +08001131 disable_cpu_nodes(blob, 3);
1132
1133#elif defined(CONFIG_IMX8MP)
Peng Fan8a472a22020-09-16 15:17:22 +08001134 if (is_imx8mpl())
Peng Fan435dc122020-07-09 14:06:49 +08001135 disable_vpu_nodes(blob);
1136
Peng Fan8a472a22020-09-16 15:17:22 +08001137 if (is_imx8mpl() || is_imx8mp6())
Peng Fan435dc122020-07-09 14:06:49 +08001138 disable_npu_nodes(blob);
1139
Peng Fan8a472a22020-09-16 15:17:22 +08001140 if (is_imx8mpl())
Peng Fan435dc122020-07-09 14:06:49 +08001141 disable_isp_nodes(blob);
1142
Peng Fan8a472a22020-09-16 15:17:22 +08001143 if (is_imx8mpl() || is_imx8mp6())
Peng Fan435dc122020-07-09 14:06:49 +08001144 disable_dsp_nodes(blob);
1145
1146 if (is_imx8mpd())
1147 disable_cpu_nodes(blob, 2);
1148#endif
1149
Peng Faneae4de22018-01-10 13:20:37 +08001150 return 0;
1151}
1152#endif
1153
Marek Vasut64dc4de2020-04-29 15:04:21 +02001154#if !CONFIG_IS_ENABLED(SYSRESET)
Harald Seiler6f14d5f2020-12-15 16:47:52 +01001155void reset_cpu(void)
Peng Faneae4de22018-01-10 13:20:37 +08001156{
Claudius Heinee73f3942020-04-29 15:04:23 +02001157 struct watchdog_regs *wdog = (struct watchdog_regs *)WDOG1_BASE_ADDR;
Peng Faneae4de22018-01-10 13:20:37 +08001158
Ye Li54a915a2019-12-09 00:47:18 -08001159 /* Clear WDA to trigger WDOG_B immediately */
1160 writew((SET_WCR_WT(1) | WCR_WDT | WCR_WDE | WCR_SRS), &wdog->wcr);
Peng Fan24290d92019-08-27 06:25:41 +00001161
Ye Li54a915a2019-12-09 00:47:18 -08001162 while (1) {
1163 /*
Harald Seilerec0c4472020-04-29 15:04:22 +02001164 * spin for .5 seconds before reset
Ye Li54a915a2019-12-09 00:47:18 -08001165 */
1166 }
Peng Faneae4de22018-01-10 13:20:37 +08001167}
Peng Fan24290d92019-08-27 06:25:41 +00001168#endif
Peng Fan5760d8d2020-04-22 10:51:13 +08001169
1170#if defined(CONFIG_ARCH_MISC_INIT)
1171static void acquire_buildinfo(void)
1172{
1173 u64 atf_commit = 0;
Peng Fan45d843a2020-05-11 15:14:04 +08001174 struct arm_smccc_res res;
Peng Fan5760d8d2020-04-22 10:51:13 +08001175
1176 /* Get ARM Trusted Firmware commit id */
Peng Fan45d843a2020-05-11 15:14:04 +08001177 arm_smccc_smc(IMX_SIP_BUILDINFO, IMX_SIP_BUILDINFO_GET_COMMITHASH,
Fabio Estevam31e410f2020-07-17 16:36:54 -03001178 0, 0, 0, 0, 0, 0, &res);
Peng Fan45d843a2020-05-11 15:14:04 +08001179 atf_commit = res.a0;
Peng Fan5760d8d2020-04-22 10:51:13 +08001180 if (atf_commit == 0xffffffff) {
1181 debug("ATF does not support build info\n");
1182 atf_commit = 0x30; /* Display 0, 0 ascii is 0x30 */
1183 }
1184
1185 printf("\n BuildInfo:\n - ATF %s\n\n", (char *)&atf_commit);
1186}
1187
1188int arch_misc_init(void)
1189{
1190 acquire_buildinfo();
1191
1192 return 0;
1193}
1194#endif
Ye Li325cd012020-05-03 22:19:52 +08001195
1196void imx_tmu_arch_init(void *reg_base)
1197{
Ye Lia00f2f02020-05-03 22:19:53 +08001198 if (is_imx8mm() || is_imx8mn()) {
Ye Li325cd012020-05-03 22:19:52 +08001199 /* Load TCALIV and TASR from fuses */
1200 struct ocotp_regs *ocotp =
1201 (struct ocotp_regs *)OCOTP_BASE_ADDR;
1202 struct fuse_bank *bank = &ocotp->bank[3];
1203 struct fuse_bank3_regs *fuse =
1204 (struct fuse_bank3_regs *)bank->fuse_regs;
1205
1206 u32 tca_rt, tca_hr, tca_en;
1207 u32 buf_vref, buf_slope;
1208
1209 tca_rt = fuse->ana0 & 0xFF;
1210 tca_hr = (fuse->ana0 & 0xFF00) >> 8;
1211 tca_en = (fuse->ana0 & 0x2000000) >> 25;
1212
1213 buf_vref = (fuse->ana0 & 0x1F00000) >> 20;
1214 buf_slope = (fuse->ana0 & 0xF0000) >> 16;
1215
1216 writel(buf_vref | (buf_slope << 16), (ulong)reg_base + 0x28);
1217 writel((tca_en << 31) | (tca_hr << 16) | tca_rt,
1218 (ulong)reg_base + 0x30);
1219 }
Ye Li41a20252020-05-03 22:19:54 +08001220#ifdef CONFIG_IMX8MP
1221 /* Load TCALIV0/1/m40 and TRIM from fuses */
1222 struct ocotp_regs *ocotp = (struct ocotp_regs *)OCOTP_BASE_ADDR;
1223 struct fuse_bank *bank = &ocotp->bank[38];
1224 struct fuse_bank38_regs *fuse =
1225 (struct fuse_bank38_regs *)bank->fuse_regs;
1226 struct fuse_bank *bank2 = &ocotp->bank[39];
1227 struct fuse_bank39_regs *fuse2 =
1228 (struct fuse_bank39_regs *)bank2->fuse_regs;
1229 u32 buf_vref, buf_slope, bjt_cur, vlsb, bgr;
1230 u32 reg;
1231 u32 tca40[2], tca25[2], tca105[2];
1232
1233 /* For blank sample */
1234 if (!fuse->ana_trim2 && !fuse->ana_trim3 &&
1235 !fuse->ana_trim4 && !fuse2->ana_trim5) {
1236 /* Use a default 25C binary codes */
1237 tca25[0] = 1596;
Ye Lid756ca02020-05-03 22:19:55 +08001238 tca25[1] = 1596;
Ye Li41a20252020-05-03 22:19:54 +08001239 writel(tca25[0], (ulong)reg_base + 0x30);
Ye Lid756ca02020-05-03 22:19:55 +08001240 writel(tca25[1], (ulong)reg_base + 0x34);
Ye Li41a20252020-05-03 22:19:54 +08001241 return;
1242 }
1243
1244 buf_vref = (fuse->ana_trim2 & 0xc0) >> 6;
1245 buf_slope = (fuse->ana_trim2 & 0xF00) >> 8;
1246 bjt_cur = (fuse->ana_trim2 & 0xF000) >> 12;
1247 bgr = (fuse->ana_trim2 & 0xF0000) >> 16;
1248 vlsb = (fuse->ana_trim2 & 0xF00000) >> 20;
1249 writel(buf_vref | (buf_slope << 16), (ulong)reg_base + 0x28);
1250
1251 reg = (bgr << 28) | (bjt_cur << 20) | (vlsb << 12) | (1 << 7);
1252 writel(reg, (ulong)reg_base + 0x3c);
1253
1254 tca40[0] = (fuse->ana_trim3 & 0xFFF0000) >> 16;
1255 tca25[0] = (fuse->ana_trim3 & 0xF0000000) >> 28;
1256 tca25[0] |= ((fuse->ana_trim4 & 0xFF) << 4);
1257 tca105[0] = (fuse->ana_trim4 & 0xFFF00) >> 8;
1258 tca40[1] = (fuse->ana_trim4 & 0xFFF00000) >> 20;
1259 tca25[1] = fuse2->ana_trim5 & 0xFFF;
1260 tca105[1] = (fuse2->ana_trim5 & 0xFFF000) >> 12;
1261
1262 /* use 25c for 1p calibration */
1263 writel(tca25[0] | (tca105[0] << 16), (ulong)reg_base + 0x30);
1264 writel(tca25[1] | (tca105[1] << 16), (ulong)reg_base + 0x34);
1265 writel(tca40[0] | (tca40[1] << 16), (ulong)reg_base + 0x38);
1266#endif
Ye Li325cd012020-05-03 22:19:52 +08001267}
Peng Fana35215d2020-07-09 13:39:26 +08001268
1269#if defined(CONFIG_SPL_BUILD)
1270#if defined(CONFIG_IMX8MQ) || defined(CONFIG_IMX8MM) || defined(CONFIG_IMX8MN)
1271bool serror_need_skip = true;
1272
1273void do_error(struct pt_regs *pt_regs, unsigned int esr)
1274{
1275 /*
1276 * If stack is still in ROM reserved OCRAM not switch to SPL,
1277 * it is the ROM SError
1278 */
1279 ulong sp;
1280
1281 asm volatile("mov %0, sp" : "=r"(sp) : );
1282
1283 if (serror_need_skip && sp < 0x910000 && sp >= 0x900000) {
1284 /* Check for ERR050342, imx8mq HDCP enabled parts */
1285 if (is_imx8mq() && !(readl(OCOTP_BASE_ADDR + 0x450) & 0x08000000)) {
1286 serror_need_skip = false;
1287 return; /* Do nothing skip the SError in ROM */
1288 }
1289
1290 /* Check for ERR050350, field return mode for imx8mq, mm and mn */
1291 if (readl(OCOTP_BASE_ADDR + 0x630) & 0x1) {
1292 serror_need_skip = false;
1293 return; /* Do nothing skip the SError in ROM */
1294 }
1295 }
1296
1297 efi_restore_gd();
1298 printf("\"Error\" handler, esr 0x%08x\n", esr);
1299 show_regs(pt_regs);
1300 panic("Resetting CPU ...\n");
1301}
1302#endif
1303#endif
Ye Li0513f362019-07-15 01:16:46 -07001304
1305#if defined(CONFIG_IMX8MN) || defined(CONFIG_IMX8MP)
1306enum env_location env_get_location(enum env_operation op, int prio)
1307{
1308 enum boot_device dev = get_boot_device();
1309 enum env_location env_loc = ENVL_UNKNOWN;
1310
1311 if (prio)
1312 return env_loc;
1313
1314 switch (dev) {
1315#ifdef CONFIG_ENV_IS_IN_SPI_FLASH
1316 case QSPI_BOOT:
1317 env_loc = ENVL_SPI_FLASH;
1318 break;
1319#endif
1320#ifdef CONFIG_ENV_IS_IN_NAND
1321 case NAND_BOOT:
1322 env_loc = ENVL_NAND;
1323 break;
1324#endif
1325#ifdef CONFIG_ENV_IS_IN_MMC
1326 case SD1_BOOT:
1327 case SD2_BOOT:
1328 case SD3_BOOT:
1329 case MMC1_BOOT:
1330 case MMC2_BOOT:
1331 case MMC3_BOOT:
1332 env_loc = ENVL_MMC;
1333 break;
1334#endif
1335 default:
1336#if defined(CONFIG_ENV_IS_NOWHERE)
1337 env_loc = ENVL_NOWHERE;
1338#endif
1339 break;
1340 }
1341
1342 return env_loc;
1343}
1344
1345#ifndef ENV_IS_EMBEDDED
1346long long env_get_offset(long long defautl_offset)
1347{
1348 enum boot_device dev = get_boot_device();
1349
1350 switch (dev) {
1351 case NAND_BOOT:
1352 return (60 << 20); /* 60MB offset for NAND */
1353 default:
1354 break;
1355 }
1356
1357 return defautl_offset;
1358}
1359#endif
1360#endif