blob: 0c6e903629fd2915d75db5d8b29116a24703678d [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
Frieder Schrempfe27bddf2021-06-07 14:36:44 +0200299ulong board_get_usable_ram_top(ulong total_size)
300{
Ying-Chun Liu (PaulLiu)5a6af8c2021-08-23 10:43:06 +0800301 ulong top_addr = PHYS_SDRAM + gd->ram_size;
302
Frieder Schrempfe27bddf2021-06-07 14:36:44 +0200303 /*
304 * Some IPs have their accessible address space restricted by
305 * the interconnect. Let's make sure U-Boot only ever uses the
306 * space below the 4G address boundary (which is 3GiB big),
307 * even when the effective available memory is bigger.
308 */
Ying-Chun Liu (PaulLiu)5a6af8c2021-08-23 10:43:06 +0800309 if (top_addr > 0x80000000)
310 top_addr = 0x80000000;
Frieder Schrempfe27bddf2021-06-07 14:36:44 +0200311
Ying-Chun Liu (PaulLiu)5a6af8c2021-08-23 10:43:06 +0800312 /*
313 * rom_pointer[0] stores the TEE memory start address.
314 * rom_pointer[1] stores the size TEE uses.
315 * We need to reserve the memory region for TEE.
316 */
317 if (rom_pointer[0] && rom_pointer[1] && top_addr > rom_pointer[0])
318 top_addr = rom_pointer[0];
319
320 return top_addr;
Frieder Schrempfe27bddf2021-06-07 14:36:44 +0200321}
322
Peng Fan78db9a52019-08-27 06:25:17 +0000323static u32 get_cpu_variant_type(u32 type)
324{
325 struct ocotp_regs *ocotp = (struct ocotp_regs *)OCOTP_BASE_ADDR;
326 struct fuse_bank *bank = &ocotp->bank[1];
327 struct fuse_bank1_regs *fuse =
328 (struct fuse_bank1_regs *)bank->fuse_regs;
329
330 u32 value = readl(&fuse->tester4);
331
Peng Fancb1a1de2020-02-05 17:34:54 +0800332 if (type == MXC_CPU_IMX8MQ) {
333 if ((value & 0x3) == 0x2)
334 return MXC_CPU_IMX8MD;
335 else if (value & 0x200000)
336 return MXC_CPU_IMX8MQL;
337
338 } else if (type == MXC_CPU_IMX8MM) {
Peng Fan78db9a52019-08-27 06:25:17 +0000339 switch (value & 0x3) {
340 case 2:
341 if (value & 0x1c0000)
342 return MXC_CPU_IMX8MMDL;
343 else
344 return MXC_CPU_IMX8MMD;
345 case 3:
346 if (value & 0x1c0000)
347 return MXC_CPU_IMX8MMSL;
348 else
349 return MXC_CPU_IMX8MMS;
350 default:
351 if (value & 0x1c0000)
352 return MXC_CPU_IMX8MML;
353 break;
354 }
Peng Fanc9154032020-02-05 17:39:27 +0800355 } else if (type == MXC_CPU_IMX8MN) {
356 switch (value & 0x3) {
357 case 2:
Ye Lic4f78cb2021-03-19 15:57:11 +0800358 if (value & 0x1000000) {
359 if (value & 0x10000000) /* MIPI DSI */
360 return MXC_CPU_IMX8MNUD;
361 else
362 return MXC_CPU_IMX8MNDL;
363 } else {
Peng Fanc9154032020-02-05 17:39:27 +0800364 return MXC_CPU_IMX8MND;
Ye Lic4f78cb2021-03-19 15:57:11 +0800365 }
Peng Fanc9154032020-02-05 17:39:27 +0800366 case 3:
Ye Lic4f78cb2021-03-19 15:57:11 +0800367 if (value & 0x1000000) {
368 if (value & 0x10000000) /* MIPI DSI */
369 return MXC_CPU_IMX8MNUS;
370 else
371 return MXC_CPU_IMX8MNSL;
372 } else {
Peng Fanc9154032020-02-05 17:39:27 +0800373 return MXC_CPU_IMX8MNS;
Ye Lic4f78cb2021-03-19 15:57:11 +0800374 }
Peng Fanc9154032020-02-05 17:39:27 +0800375 default:
Ye Lic4f78cb2021-03-19 15:57:11 +0800376 if (value & 0x1000000) {
377 if (value & 0x10000000) /* MIPI DSI */
378 return MXC_CPU_IMX8MNUQ;
379 else
380 return MXC_CPU_IMX8MNL;
381 }
Peng Fanc9154032020-02-05 17:39:27 +0800382 break;
383 }
Ye Lid1eee7e2020-04-20 20:12:54 -0700384 } else if (type == MXC_CPU_IMX8MP) {
385 u32 value0 = readl(&fuse->tester3);
386 u32 flag = 0;
387
388 if ((value0 & 0xc0000) == 0x80000)
389 return MXC_CPU_IMX8MPD;
390
391 /* vpu disabled */
392 if ((value0 & 0x43000000) == 0x43000000)
393 flag = 1;
394
395 /* npu disabled*/
396 if ((value & 0x8) == 0x8)
397 flag |= (1 << 1);
398
399 /* isp disabled */
400 if ((value & 0x3) == 0x3)
401 flag |= (1 << 2);
402
403 switch (flag) {
404 case 7:
405 return MXC_CPU_IMX8MPL;
Ye Lid1eee7e2020-04-20 20:12:54 -0700406 case 2:
407 return MXC_CPU_IMX8MP6;
Ye Lid1eee7e2020-04-20 20:12:54 -0700408 default:
409 break;
410 }
411
Peng Fan78db9a52019-08-27 06:25:17 +0000412 }
413
414 return type;
415}
416
Peng Fanfcdbde72018-01-10 13:20:37 +0800417u32 get_cpu_rev(void)
418{
419 struct anamix_pll *ana_pll = (struct anamix_pll *)ANATOP_BASE_ADDR;
420 u32 reg = readl(&ana_pll->digprog);
421 u32 type = (reg >> 16) & 0xff;
Peng Fan78db9a52019-08-27 06:25:17 +0000422 u32 major_low = (reg >> 8) & 0xff;
Peng Fanfcdbde72018-01-10 13:20:37 +0800423 u32 rom_version;
424
425 reg &= 0xff;
426
Peng Fan625b03d2019-12-27 10:14:02 +0800427 /* iMX8MP */
428 if (major_low == 0x43) {
Ye Lid1eee7e2020-04-20 20:12:54 -0700429 type = get_cpu_variant_type(MXC_CPU_IMX8MP);
Peng Fan625b03d2019-12-27 10:14:02 +0800430 } else if (major_low == 0x42) {
431 /* iMX8MN */
Peng Fanc9154032020-02-05 17:39:27 +0800432 type = get_cpu_variant_type(MXC_CPU_IMX8MN);
Peng Fan24341312019-06-27 17:23:49 +0800433 } else if (major_low == 0x41) {
Peng Fan78db9a52019-08-27 06:25:17 +0000434 type = get_cpu_variant_type(MXC_CPU_IMX8MM);
435 } else {
436 if (reg == CHIP_REV_1_0) {
437 /*
Peng Fan9e094452019-10-16 10:24:17 +0000438 * For B0 chip, the DIGPROG is not updated,
439 * it is still TO1.0. we have to check ROM
440 * version or OCOTP_READ_FUSE_DATA.
441 * 0xff0055aa is magic number for B1.
Peng Fan78db9a52019-08-27 06:25:17 +0000442 */
Peng Fan9e094452019-10-16 10:24:17 +0000443 if (readl((void __iomem *)(OCOTP_BASE_ADDR + 0x40)) == 0xff0055aa) {
Ye Liea2b26fb2021-03-19 15:57:16 +0800444 /*
445 * B2 uses same DIGPROG and OCOTP_READ_FUSE_DATA value with B1,
446 * so have to check ROM to distinguish them
447 */
448 rom_version = readl((void __iomem *)ROM_VERSION_B0);
449 rom_version &= 0xff;
450 if (rom_version == CHIP_REV_2_2)
451 reg = CHIP_REV_2_2;
452 else
453 reg = CHIP_REV_2_1;
Peng Fan9e094452019-10-16 10:24:17 +0000454 } else {
455 rom_version =
456 readl((void __iomem *)ROM_VERSION_A0);
457 if (rom_version != CHIP_REV_1_0) {
458 rom_version = readl((void __iomem *)ROM_VERSION_B0);
Patrick Wildt6a4b07e2019-11-19 09:42:06 +0100459 rom_version &= 0xff;
Peng Fan9e094452019-10-16 10:24:17 +0000460 if (rom_version == CHIP_REV_2_0)
461 reg = CHIP_REV_2_0;
462 }
Peng Fan78db9a52019-08-27 06:25:17 +0000463 }
Peng Fanfcdbde72018-01-10 13:20:37 +0800464 }
Peng Fancb1a1de2020-02-05 17:34:54 +0800465
466 type = get_cpu_variant_type(type);
Peng Fanfcdbde72018-01-10 13:20:37 +0800467 }
468
469 return (type << 12) | reg;
470}
471
472static void imx_set_wdog_powerdown(bool enable)
473{
474 struct wdog_regs *wdog1 = (struct wdog_regs *)WDOG1_BASE_ADDR;
475 struct wdog_regs *wdog2 = (struct wdog_regs *)WDOG2_BASE_ADDR;
476 struct wdog_regs *wdog3 = (struct wdog_regs *)WDOG3_BASE_ADDR;
477
478 /* Write to the PDE (Power Down Enable) bit */
479 writew(enable, &wdog1->wmcr);
480 writew(enable, &wdog2->wmcr);
481 writew(enable, &wdog3->wmcr);
482}
483
Peng Fane663c702019-08-27 06:25:58 +0000484int arch_cpu_init_dm(void)
485{
486 struct udevice *dev;
487 int ret;
488
Peng Fancd7c8062019-10-16 03:01:51 +0000489 if (CONFIG_IS_ENABLED(CLK)) {
490 ret = uclass_get_device_by_name(UCLASS_CLK,
491 "clock-controller@30380000",
492 &dev);
493 if (ret < 0) {
494 printf("Failed to find clock node. Check device tree\n");
495 return ret;
496 }
Peng Fane663c702019-08-27 06:25:58 +0000497 }
498
499 return 0;
500}
501
Peng Fanfcdbde72018-01-10 13:20:37 +0800502int arch_cpu_init(void)
503{
Peng Fan702339b2019-04-17 09:41:16 +0000504 struct ocotp_regs *ocotp = (struct ocotp_regs *)OCOTP_BASE_ADDR;
Peng Fanfcdbde72018-01-10 13:20:37 +0800505 /*
Peng Fan0528ba02019-08-27 06:25:37 +0000506 * ROM might disable clock for SCTR,
507 * enable the clock before timer_init.
508 */
509 if (IS_ENABLED(CONFIG_SPL_BUILD))
510 clock_enable(CCGR_SCTR, 1);
511 /*
Peng Fanfcdbde72018-01-10 13:20:37 +0800512 * Init timer at very early state, because sscg pll setting
513 * will use it
514 */
515 timer_init();
516
517 if (IS_ENABLED(CONFIG_SPL_BUILD)) {
518 clock_init();
519 imx_set_wdog_powerdown(false);
Peng Fan7a42bf02020-07-09 13:52:41 +0800520
521 if (is_imx8md() || is_imx8mmd() || is_imx8mmdl() || is_imx8mms() ||
522 is_imx8mmsl() || is_imx8mnd() || is_imx8mndl() || is_imx8mns() ||
Ye Lic4f78cb2021-03-19 15:57:11 +0800523 is_imx8mnsl() || is_imx8mpd() || is_imx8mnud() || is_imx8mnus()) {
Peng Fan7a42bf02020-07-09 13:52:41 +0800524 /* Power down cpu core 1, 2 and 3 for iMX8M Dual core or Single core */
525 struct pgc_reg *pgc_core1 = (struct pgc_reg *)(GPC_BASE_ADDR + 0x840);
526 struct pgc_reg *pgc_core2 = (struct pgc_reg *)(GPC_BASE_ADDR + 0x880);
527 struct pgc_reg *pgc_core3 = (struct pgc_reg *)(GPC_BASE_ADDR + 0x8C0);
528 struct gpc_reg *gpc = (struct gpc_reg *)GPC_BASE_ADDR;
529
530 writel(0x1, &pgc_core2->pgcr);
531 writel(0x1, &pgc_core3->pgcr);
Ye Lic4f78cb2021-03-19 15:57:11 +0800532 if (is_imx8mms() || is_imx8mmsl() || is_imx8mns() || is_imx8mnsl() || is_imx8mnus()) {
Peng Fan7a42bf02020-07-09 13:52:41 +0800533 writel(0x1, &pgc_core1->pgcr);
534 writel(0xE, &gpc->cpu_pgc_dn_trg);
535 } else {
536 writel(0xC, &gpc->cpu_pgc_dn_trg);
537 }
538 }
Peng Fanfcdbde72018-01-10 13:20:37 +0800539 }
540
Peng Fan702339b2019-04-17 09:41:16 +0000541 if (is_imx8mq()) {
542 clock_enable(CCGR_OCOTP, 1);
543 if (readl(&ocotp->ctrl) & 0x200)
544 writel(0x200, &ocotp->ctrl_clr);
545 }
546
Peng Fanfcdbde72018-01-10 13:20:37 +0800547 return 0;
548}
549
Peng Fanb1821372019-09-16 03:09:36 +0000550#if defined(CONFIG_IMX8MN) || defined(CONFIG_IMX8MP)
551struct rom_api *g_rom_api = (struct rom_api *)0x980;
552
553enum boot_device get_boot_device(void)
554{
555 volatile gd_t *pgd = gd;
556 int ret;
557 u32 boot;
558 u16 boot_type;
559 u8 boot_instance;
560 enum boot_device boot_dev = SD1_BOOT;
561
562 ret = g_rom_api->query_boot_infor(QUERY_BT_DEV, &boot,
563 ((uintptr_t)&boot) ^ QUERY_BT_DEV);
Marek Behún86c5e212021-05-20 13:24:10 +0200564 set_gd(pgd);
Peng Fanb1821372019-09-16 03:09:36 +0000565
566 if (ret != ROM_API_OKAY) {
567 puts("ROMAPI: failure at query_boot_info\n");
568 return -1;
569 }
570
571 boot_type = boot >> 16;
572 boot_instance = (boot >> 8) & 0xff;
573
574 switch (boot_type) {
575 case BT_DEV_TYPE_SD:
576 boot_dev = boot_instance + SD1_BOOT;
577 break;
578 case BT_DEV_TYPE_MMC:
579 boot_dev = boot_instance + MMC1_BOOT;
580 break;
581 case BT_DEV_TYPE_NAND:
582 boot_dev = NAND_BOOT;
583 break;
584 case BT_DEV_TYPE_FLEXSPINOR:
585 boot_dev = QSPI_BOOT;
586 break;
587 case BT_DEV_TYPE_USB:
588 boot_dev = USB_BOOT;
589 break;
590 default:
591 break;
592 }
593
594 return boot_dev;
595}
596#endif
597
Marek Vasuta5ee05c2021-07-03 04:55:33 +0200598#if defined(CONFIG_IMX8M)
599#include <spl.h>
600int spl_mmc_emmc_boot_partition(struct mmc *mmc)
601{
602 u32 *rom_log_addr = (u32 *)0x9e0;
603 u32 *rom_log;
604 u8 event_id;
605 int i, part;
606
607 part = default_spl_mmc_emmc_boot_partition(mmc);
608
609 /* If the ROM event log pointer is not valid. */
610 if (*rom_log_addr < 0x900000 || *rom_log_addr >= 0xb00000 ||
611 *rom_log_addr & 0x3)
612 return part;
613
614 /* Parse the ROM event ID version 2 log */
615 rom_log = (u32 *)(uintptr_t)(*rom_log_addr);
616 for (i = 0; i < 128; i++) {
617 event_id = rom_log[i] >> 24;
618 switch (event_id) {
619 case 0x00: /* End of list */
620 return part;
621 /* Log entries with 1 parameter, skip 1 */
622 case 0x80: /* Start to perform the device initialization */
623 case 0x81: /* The boot device initialization completes */
624 case 0x8f: /* The boot device initialization fails */
625 case 0x90: /* Start to read data from boot device */
626 case 0x91: /* Reading data from boot device completes */
627 case 0x9f: /* Reading data from boot device fails */
628 i += 1;
629 continue;
630 /* Log entries with 2 parameters, skip 2 */
631 case 0xa0: /* Image authentication result */
632 case 0xc0: /* Jump to the boot image soon */
633 i += 2;
634 continue;
635 /* Boot from the secondary boot image */
636 case 0x51:
637 /*
638 * Swap the eMMC boot partitions in case there was a
639 * fallback event (i.e. primary image was corrupted
640 * and that corruption was recognized by the BootROM),
641 * so the SPL loads the rest of the U-Boot from the
642 * correct eMMC boot partition, since the BootROM
643 * leaves the boot partition set to the corrupted one.
644 */
645 if (part == 1)
646 part = 2;
647 else if (part == 2)
648 part = 1;
649 continue;
650 default:
651 continue;
652 }
653 }
654
655 return part;
656}
657#endif
658
Peng Fanfcdbde72018-01-10 13:20:37 +0800659bool is_usb_boot(void)
660{
661 return get_boot_device() == USB_BOOT;
662}
663
664#ifdef CONFIG_OF_SYSTEM_SETUP
Peng Fan6036dba2020-07-09 14:06:49 +0800665bool check_fdt_new_path(void *blob)
666{
667 const char *soc_path = "/soc@0";
668 int nodeoff;
669
670 nodeoff = fdt_path_offset(blob, soc_path);
671 if (nodeoff < 0)
672 return false;
673
674 return true;
675}
676
677static int disable_fdt_nodes(void *blob, const char *const nodes_path[], int size_array)
Peng Fanfcdbde72018-01-10 13:20:37 +0800678{
679 int i = 0;
680 int rc;
681 int nodeoff;
Peng Fan6036dba2020-07-09 14:06:49 +0800682 const char *status = "disabled";
683
684 for (i = 0; i < size_array; i++) {
685 nodeoff = fdt_path_offset(blob, nodes_path[i]);
686 if (nodeoff < 0)
687 continue; /* Not found, skip it */
688
689 printf("Found %s node\n", nodes_path[i]);
690
691add_status:
692 rc = fdt_setprop(blob, nodeoff, "status", status, strlen(status) + 1);
693 if (rc) {
694 if (rc == -FDT_ERR_NOSPACE) {
695 rc = fdt_increase_size(blob, 512);
696 if (!rc)
697 goto add_status;
698 }
699 printf("Unable to update property %s:%s, err=%s\n",
700 nodes_path[i], "status", fdt_strerror(rc));
701 } else {
702 printf("Modify %s:%s disabled\n",
703 nodes_path[i], "status");
704 }
705 }
706
707 return 0;
708}
709
710#ifdef CONFIG_IMX8MQ
711bool check_dcss_fused(void)
712{
713 struct ocotp_regs *ocotp = (struct ocotp_regs *)OCOTP_BASE_ADDR;
714 struct fuse_bank *bank = &ocotp->bank[1];
715 struct fuse_bank1_regs *fuse =
716 (struct fuse_bank1_regs *)bank->fuse_regs;
717 u32 value = readl(&fuse->tester4);
718
719 if (value & 0x4000000)
720 return true;
721
722 return false;
723}
724
725static int disable_mipi_dsi_nodes(void *blob)
726{
727 static const char * const nodes_path[] = {
728 "/mipi_dsi@30A00000",
729 "/mipi_dsi_bridge@30A00000",
730 "/dsi_phy@30A00300",
731 "/soc@0/bus@30800000/mipi_dsi@30a00000",
Peng Fanc0e2f762021-03-19 15:57:13 +0800732 "/soc@0/bus@30800000/dphy@30a00300",
733 "/soc@0/bus@30800000/mipi-dsi@30a00000",
Peng Fan6036dba2020-07-09 14:06:49 +0800734 };
735
736 return disable_fdt_nodes(blob, nodes_path, ARRAY_SIZE(nodes_path));
737}
738
739static int disable_dcss_nodes(void *blob)
740{
741 static const char * const nodes_path[] = {
742 "/dcss@0x32e00000",
743 "/dcss@32e00000",
744 "/hdmi@32c00000",
745 "/hdmi_cec@32c33800",
746 "/hdmi_drm@32c00000",
747 "/display-subsystem",
748 "/sound-hdmi",
749 "/sound-hdmi-arc",
750 "/soc@0/bus@32c00000/display-controller@32e00000",
751 "/soc@0/bus@32c00000/hdmi@32c00000",
752 };
753
754 return disable_fdt_nodes(blob, nodes_path, ARRAY_SIZE(nodes_path));
755}
756
757static int check_mipi_dsi_nodes(void *blob)
758{
759 static const char * const lcdif_path[] = {
760 "/lcdif@30320000",
Peng Fanc0e2f762021-03-19 15:57:13 +0800761 "/soc@0/bus@30000000/lcdif@30320000",
762 "/soc@0/bus@30000000/lcd-controller@30320000"
Peng Fan6036dba2020-07-09 14:06:49 +0800763 };
764 static const char * const mipi_dsi_path[] = {
765 "/mipi_dsi@30A00000",
766 "/soc@0/bus@30800000/mipi_dsi@30a00000"
767 };
768 static const char * const lcdif_ep_path[] = {
769 "/lcdif@30320000/port@0/mipi-dsi-endpoint",
Peng Fanc0e2f762021-03-19 15:57:13 +0800770 "/soc@0/bus@30000000/lcdif@30320000/port@0/endpoint",
771 "/soc@0/bus@30000000/lcd-controller@30320000/port@0/endpoint"
Peng Fan6036dba2020-07-09 14:06:49 +0800772 };
773 static const char * const mipi_dsi_ep_path[] = {
774 "/mipi_dsi@30A00000/port@1/endpoint",
Peng Fanc0e2f762021-03-19 15:57:13 +0800775 "/soc@0/bus@30800000/mipi_dsi@30a00000/ports/port@0/endpoint",
776 "/soc@0/bus@30800000/mipi-dsi@30a00000/ports/port@0/endpoint@0"
Peng Fan6036dba2020-07-09 14:06:49 +0800777 };
778
779 int lookup_node;
780 int nodeoff;
781 bool new_path = check_fdt_new_path(blob);
782 int i = new_path ? 1 : 0;
783
784 nodeoff = fdt_path_offset(blob, lcdif_path[i]);
785 if (nodeoff < 0 || !fdtdec_get_is_enabled(blob, nodeoff)) {
786 /*
787 * If can't find lcdif node or lcdif node is disabled,
788 * then disable all mipi dsi, since they only can input
789 * from DCSS
790 */
791 return disable_mipi_dsi_nodes(blob);
792 }
793
794 nodeoff = fdt_path_offset(blob, mipi_dsi_path[i]);
795 if (nodeoff < 0 || !fdtdec_get_is_enabled(blob, nodeoff))
796 return 0;
797
798 nodeoff = fdt_path_offset(blob, lcdif_ep_path[i]);
799 if (nodeoff < 0) {
800 /*
801 * If can't find lcdif endpoint, then disable all mipi dsi,
802 * since they only can input from DCSS
803 */
804 return disable_mipi_dsi_nodes(blob);
805 }
806
807 lookup_node = fdtdec_lookup_phandle(blob, nodeoff, "remote-endpoint");
808 nodeoff = fdt_path_offset(blob, mipi_dsi_ep_path[i]);
809
810 if (nodeoff > 0 && nodeoff == lookup_node)
811 return 0;
812
813 return disable_mipi_dsi_nodes(blob);
814}
815#endif
816
817int disable_vpu_nodes(void *blob)
818{
819 static const char * const nodes_path_8mq[] = {
820 "/vpu@38300000",
821 "/soc@0/vpu@38300000"
822 };
823
824 static const char * const nodes_path_8mm[] = {
825 "/vpu_g1@38300000",
826 "/vpu_g2@38310000",
827 "/vpu_h1@38320000"
828 };
829
830 static const char * const nodes_path_8mp[] = {
831 "/vpu_g1@38300000",
832 "/vpu_g2@38310000",
833 "/vpu_vc8000e@38320000"
834 };
835
836 if (is_imx8mq())
837 return disable_fdt_nodes(blob, nodes_path_8mq, ARRAY_SIZE(nodes_path_8mq));
838 else if (is_imx8mm())
839 return disable_fdt_nodes(blob, nodes_path_8mm, ARRAY_SIZE(nodes_path_8mm));
840 else if (is_imx8mp())
841 return disable_fdt_nodes(blob, nodes_path_8mp, ARRAY_SIZE(nodes_path_8mp));
842 else
843 return -EPERM;
844}
845
Ye Li98bcdf12021-03-19 15:57:09 +0800846#ifdef CONFIG_IMX8MN_LOW_DRIVE_MODE
847static int low_drive_gpu_freq(void *blob)
848{
849 static const char *nodes_path_8mn[] = {
850 "/gpu@38000000",
851 "/soc@0/gpu@38000000"
852 };
853
854 int nodeoff, cnt, i;
855 u32 assignedclks[7];
856
857 nodeoff = fdt_path_offset(blob, nodes_path_8mn[0]);
858 if (nodeoff < 0)
859 return nodeoff;
860
861 cnt = fdtdec_get_int_array_count(blob, nodeoff, "assigned-clock-rates", assignedclks, 7);
862 if (cnt < 0)
863 return cnt;
864
865 if (cnt != 7)
866 printf("Warning: %s, assigned-clock-rates count %d\n", nodes_path_8mn[0], cnt);
867
868 assignedclks[cnt - 1] = 200000000;
869 assignedclks[cnt - 2] = 200000000;
870
871 for (i = 0; i < cnt; i++) {
872 debug("<%u>, ", assignedclks[i]);
873 assignedclks[i] = cpu_to_fdt32(assignedclks[i]);
874 }
875 debug("\n");
876
877 return fdt_setprop(blob, nodeoff, "assigned-clock-rates", &assignedclks, sizeof(assignedclks));
878}
879#endif
880
Peng Fan6036dba2020-07-09 14:06:49 +0800881int disable_gpu_nodes(void *blob)
882{
883 static const char * const nodes_path_8mn[] = {
Peng Fanc0e2f762021-03-19 15:57:13 +0800884 "/gpu@38000000",
885 "/soc@/gpu@38000000"
Peng Fan6036dba2020-07-09 14:06:49 +0800886 };
887
888 return disable_fdt_nodes(blob, nodes_path_8mn, ARRAY_SIZE(nodes_path_8mn));
889}
890
891int disable_npu_nodes(void *blob)
892{
893 static const char * const nodes_path_8mp[] = {
894 "/vipsi@38500000"
895 };
896
897 return disable_fdt_nodes(blob, nodes_path_8mp, ARRAY_SIZE(nodes_path_8mp));
898}
899
900int disable_isp_nodes(void *blob)
901{
902 static const char * const nodes_path_8mp[] = {
903 "/soc@0/bus@32c00000/camera/isp@32e10000",
904 "/soc@0/bus@32c00000/camera/isp@32e20000"
905 };
906
907 return disable_fdt_nodes(blob, nodes_path_8mp, ARRAY_SIZE(nodes_path_8mp));
908}
909
910int disable_dsp_nodes(void *blob)
911{
912 static const char * const nodes_path_8mp[] = {
913 "/dsp@3b6e8000"
914 };
915
916 return disable_fdt_nodes(blob, nodes_path_8mp, ARRAY_SIZE(nodes_path_8mp));
917}
918
Ye Li4e83c052021-03-19 15:57:12 +0800919static void disable_thermal_cpu_nodes(void *blob, u32 disabled_cores)
920{
921 static const char * const thermal_path[] = {
922 "/thermal-zones/cpu-thermal/cooling-maps/map0"
923 };
924
925 int nodeoff, cnt, i, ret, j;
926 u32 cooling_dev[12];
927
928 for (i = 0; i < ARRAY_SIZE(thermal_path); i++) {
929 nodeoff = fdt_path_offset(blob, thermal_path[i]);
930 if (nodeoff < 0)
931 continue; /* Not found, skip it */
932
933 cnt = fdtdec_get_int_array_count(blob, nodeoff, "cooling-device", cooling_dev, 12);
934 if (cnt < 0)
935 continue;
936
937 if (cnt != 12)
938 printf("Warning: %s, cooling-device count %d\n", thermal_path[i], cnt);
939
940 for (j = 0; j < cnt; j++)
941 cooling_dev[j] = cpu_to_fdt32(cooling_dev[j]);
942
943 ret = fdt_setprop(blob, nodeoff, "cooling-device", &cooling_dev,
944 sizeof(u32) * (12 - disabled_cores * 3));
945 if (ret < 0) {
946 printf("Warning: %s, cooling-device setprop failed %d\n",
947 thermal_path[i], ret);
948 continue;
949 }
950
951 printf("Update node %s, cooling-device prop\n", thermal_path[i]);
952 }
953}
954
955static void disable_pmu_cpu_nodes(void *blob, u32 disabled_cores)
956{
957 static const char * const pmu_path[] = {
958 "/pmu"
959 };
960
961 int nodeoff, cnt, i, ret, j;
962 u32 irq_affinity[4];
963
964 for (i = 0; i < ARRAY_SIZE(pmu_path); i++) {
965 nodeoff = fdt_path_offset(blob, pmu_path[i]);
966 if (nodeoff < 0)
967 continue; /* Not found, skip it */
968
969 cnt = fdtdec_get_int_array_count(blob, nodeoff, "interrupt-affinity",
970 irq_affinity, 4);
971 if (cnt < 0)
972 continue;
973
974 if (cnt != 4)
975 printf("Warning: %s, interrupt-affinity count %d\n", pmu_path[i], cnt);
976
977 for (j = 0; j < cnt; j++)
978 irq_affinity[j] = cpu_to_fdt32(irq_affinity[j]);
979
980 ret = fdt_setprop(blob, nodeoff, "interrupt-affinity", &irq_affinity,
981 sizeof(u32) * (4 - disabled_cores));
982 if (ret < 0) {
983 printf("Warning: %s, interrupt-affinity setprop failed %d\n",
984 pmu_path[i], ret);
985 continue;
986 }
987
988 printf("Update node %s, interrupt-affinity prop\n", pmu_path[i]);
989 }
990}
991
Peng Fan6036dba2020-07-09 14:06:49 +0800992static int disable_cpu_nodes(void *blob, u32 disabled_cores)
993{
994 static const char * const nodes_path[] = {
995 "/cpus/cpu@1",
996 "/cpus/cpu@2",
997 "/cpus/cpu@3",
998 };
999 u32 i = 0;
1000 int rc;
1001 int nodeoff;
1002
1003 if (disabled_cores > 3)
1004 return -EINVAL;
1005
1006 i = 3 - disabled_cores;
1007
1008 for (; i < 3; i++) {
1009 nodeoff = fdt_path_offset(blob, nodes_path[i]);
1010 if (nodeoff < 0)
1011 continue; /* Not found, skip it */
1012
1013 debug("Found %s node\n", nodes_path[i]);
1014
1015 rc = fdt_del_node(blob, nodeoff);
1016 if (rc < 0) {
1017 printf("Unable to delete node %s, err=%s\n",
1018 nodes_path[i], fdt_strerror(rc));
1019 } else {
1020 printf("Delete node %s\n", nodes_path[i]);
1021 }
1022 }
1023
Ye Li4e83c052021-03-19 15:57:12 +08001024 disable_thermal_cpu_nodes(blob, disabled_cores);
1025 disable_pmu_cpu_nodes(blob, disabled_cores);
1026
Peng Fan6036dba2020-07-09 14:06:49 +08001027 return 0;
1028}
1029
Masahiro Yamadab75d8dc2020-06-26 15:13:33 +09001030int ft_system_setup(void *blob, struct bd_info *bd)
Peng Fan6036dba2020-07-09 14:06:49 +08001031{
1032#ifdef CONFIG_IMX8MQ
1033 int i = 0;
1034 int rc;
1035 int nodeoff;
1036
1037 if (get_boot_device() == USB_BOOT) {
1038 disable_dcss_nodes(blob);
1039
1040 bool new_path = check_fdt_new_path(blob);
1041 int v = new_path ? 1 : 0;
1042 static const char * const usb_dwc3_path[] = {
1043 "/usb@38100000/dwc3",
1044 "/soc@0/usb@38100000"
1045 };
1046
1047 nodeoff = fdt_path_offset(blob, usb_dwc3_path[v]);
1048 if (nodeoff >= 0) {
1049 const char *speed = "high-speed";
1050
1051 printf("Found %s node\n", usb_dwc3_path[v]);
1052
1053usb_modify_speed:
1054
1055 rc = fdt_setprop(blob, nodeoff, "maximum-speed", speed, strlen(speed) + 1);
1056 if (rc) {
1057 if (rc == -FDT_ERR_NOSPACE) {
1058 rc = fdt_increase_size(blob, 512);
1059 if (!rc)
1060 goto usb_modify_speed;
1061 }
1062 printf("Unable to set property %s:%s, err=%s\n",
1063 usb_dwc3_path[v], "maximum-speed", fdt_strerror(rc));
1064 } else {
1065 printf("Modify %s:%s = %s\n",
1066 usb_dwc3_path[v], "maximum-speed", speed);
1067 }
1068 } else {
1069 printf("Can't found %s node\n", usb_dwc3_path[v]);
1070 }
1071 }
Peng Fanfcdbde72018-01-10 13:20:37 +08001072
1073 /* Disable the CPU idle for A0 chip since the HW does not support it */
1074 if (is_soc_rev(CHIP_REV_1_0)) {
1075 static const char * const nodes_path[] = {
1076 "/cpus/cpu@0",
1077 "/cpus/cpu@1",
1078 "/cpus/cpu@2",
1079 "/cpus/cpu@3",
1080 };
1081
1082 for (i = 0; i < ARRAY_SIZE(nodes_path); i++) {
1083 nodeoff = fdt_path_offset(blob, nodes_path[i]);
1084 if (nodeoff < 0)
1085 continue; /* Not found, skip it */
1086
Marek Vasutdabaabd2020-04-24 21:37:33 +02001087 debug("Found %s node\n", nodes_path[i]);
Peng Fanfcdbde72018-01-10 13:20:37 +08001088
1089 rc = fdt_delprop(blob, nodeoff, "cpu-idle-states");
Marek Vasutdabaabd2020-04-24 21:37:33 +02001090 if (rc == -FDT_ERR_NOTFOUND)
1091 continue;
Peng Fanfcdbde72018-01-10 13:20:37 +08001092 if (rc) {
1093 printf("Unable to update property %s:%s, err=%s\n",
1094 nodes_path[i], "status", fdt_strerror(rc));
1095 return rc;
1096 }
1097
Marek Vasutdabaabd2020-04-24 21:37:33 +02001098 debug("Remove %s:%s\n", nodes_path[i],
Peng Fanfcdbde72018-01-10 13:20:37 +08001099 "cpu-idle-states");
1100 }
1101 }
1102
Peng Fan6036dba2020-07-09 14:06:49 +08001103 if (is_imx8mql()) {
1104 disable_vpu_nodes(blob);
1105 if (check_dcss_fused()) {
1106 printf("DCSS is fused\n");
1107 disable_dcss_nodes(blob);
1108 check_mipi_dsi_nodes(blob);
1109 }
1110 }
1111
1112 if (is_imx8md())
1113 disable_cpu_nodes(blob, 2);
1114
1115#elif defined(CONFIG_IMX8MM)
1116 if (is_imx8mml() || is_imx8mmdl() || is_imx8mmsl())
1117 disable_vpu_nodes(blob);
1118
1119 if (is_imx8mmd() || is_imx8mmdl())
1120 disable_cpu_nodes(blob, 2);
1121 else if (is_imx8mms() || is_imx8mmsl())
1122 disable_cpu_nodes(blob, 3);
1123
1124#elif defined(CONFIG_IMX8MN)
1125 if (is_imx8mnl() || is_imx8mndl() || is_imx8mnsl())
1126 disable_gpu_nodes(blob);
Ye Li98bcdf12021-03-19 15:57:09 +08001127#ifdef CONFIG_IMX8MN_LOW_DRIVE_MODE
1128 else {
1129 int ldm_gpu = low_drive_gpu_freq(blob);
1130
1131 if (ldm_gpu < 0)
1132 printf("Update GPU node assigned-clock-rates failed\n");
1133 else
1134 printf("Update GPU node assigned-clock-rates ok\n");
1135 }
1136#endif
Peng Fan6036dba2020-07-09 14:06:49 +08001137
Ye Lic4f78cb2021-03-19 15:57:11 +08001138 if (is_imx8mnd() || is_imx8mndl() || is_imx8mnud())
Peng Fan6036dba2020-07-09 14:06:49 +08001139 disable_cpu_nodes(blob, 2);
Ye Lic4f78cb2021-03-19 15:57:11 +08001140 else if (is_imx8mns() || is_imx8mnsl() || is_imx8mnus())
Peng Fan6036dba2020-07-09 14:06:49 +08001141 disable_cpu_nodes(blob, 3);
1142
1143#elif defined(CONFIG_IMX8MP)
Peng Fan00982222020-09-16 15:17:22 +08001144 if (is_imx8mpl())
Peng Fan6036dba2020-07-09 14:06:49 +08001145 disable_vpu_nodes(blob);
1146
Peng Fan00982222020-09-16 15:17:22 +08001147 if (is_imx8mpl() || is_imx8mp6())
Peng Fan6036dba2020-07-09 14:06:49 +08001148 disable_npu_nodes(blob);
1149
Peng Fan00982222020-09-16 15:17:22 +08001150 if (is_imx8mpl())
Peng Fan6036dba2020-07-09 14:06:49 +08001151 disable_isp_nodes(blob);
1152
Peng Fan00982222020-09-16 15:17:22 +08001153 if (is_imx8mpl() || is_imx8mp6())
Peng Fan6036dba2020-07-09 14:06:49 +08001154 disable_dsp_nodes(blob);
1155
1156 if (is_imx8mpd())
1157 disable_cpu_nodes(blob, 2);
1158#endif
1159
Peng Fanfcdbde72018-01-10 13:20:37 +08001160 return 0;
1161}
1162#endif
1163
Marek Vasutefa1a622020-04-29 15:04:21 +02001164#if !CONFIG_IS_ENABLED(SYSRESET)
Harald Seiler35b65dd2020-12-15 16:47:52 +01001165void reset_cpu(void)
Peng Fanfcdbde72018-01-10 13:20:37 +08001166{
Claudius Heinec5635a02020-04-29 15:04:23 +02001167 struct watchdog_regs *wdog = (struct watchdog_regs *)WDOG1_BASE_ADDR;
Peng Fanfcdbde72018-01-10 13:20:37 +08001168
Ye Li72479742019-12-09 00:47:18 -08001169 /* Clear WDA to trigger WDOG_B immediately */
1170 writew((SET_WCR_WT(1) | WCR_WDT | WCR_WDE | WCR_SRS), &wdog->wcr);
Peng Fand2041722019-08-27 06:25:41 +00001171
Ye Li72479742019-12-09 00:47:18 -08001172 while (1) {
1173 /*
Harald Seiler568af922020-04-29 15:04:22 +02001174 * spin for .5 seconds before reset
Ye Li72479742019-12-09 00:47:18 -08001175 */
1176 }
Peng Fanfcdbde72018-01-10 13:20:37 +08001177}
Peng Fand2041722019-08-27 06:25:41 +00001178#endif
Peng Fana07bcec2020-04-22 10:51:13 +08001179
1180#if defined(CONFIG_ARCH_MISC_INIT)
1181static void acquire_buildinfo(void)
1182{
1183 u64 atf_commit = 0;
Peng Fana2f143e2020-05-11 15:14:04 +08001184 struct arm_smccc_res res;
Peng Fana07bcec2020-04-22 10:51:13 +08001185
1186 /* Get ARM Trusted Firmware commit id */
Peng Fana2f143e2020-05-11 15:14:04 +08001187 arm_smccc_smc(IMX_SIP_BUILDINFO, IMX_SIP_BUILDINFO_GET_COMMITHASH,
Fabio Estevam68a699e2020-07-17 16:36:54 -03001188 0, 0, 0, 0, 0, 0, &res);
Peng Fana2f143e2020-05-11 15:14:04 +08001189 atf_commit = res.a0;
Peng Fana07bcec2020-04-22 10:51:13 +08001190 if (atf_commit == 0xffffffff) {
1191 debug("ATF does not support build info\n");
1192 atf_commit = 0x30; /* Display 0, 0 ascii is 0x30 */
1193 }
1194
1195 printf("\n BuildInfo:\n - ATF %s\n\n", (char *)&atf_commit);
1196}
1197
1198int arch_misc_init(void)
1199{
1200 acquire_buildinfo();
1201
1202 return 0;
1203}
1204#endif
Ye Li70487ff2020-05-03 22:19:52 +08001205
1206void imx_tmu_arch_init(void *reg_base)
1207{
Ye Li94c693d2020-05-03 22:19:53 +08001208 if (is_imx8mm() || is_imx8mn()) {
Ye Li70487ff2020-05-03 22:19:52 +08001209 /* Load TCALIV and TASR from fuses */
1210 struct ocotp_regs *ocotp =
1211 (struct ocotp_regs *)OCOTP_BASE_ADDR;
1212 struct fuse_bank *bank = &ocotp->bank[3];
1213 struct fuse_bank3_regs *fuse =
1214 (struct fuse_bank3_regs *)bank->fuse_regs;
1215
1216 u32 tca_rt, tca_hr, tca_en;
1217 u32 buf_vref, buf_slope;
1218
1219 tca_rt = fuse->ana0 & 0xFF;
1220 tca_hr = (fuse->ana0 & 0xFF00) >> 8;
1221 tca_en = (fuse->ana0 & 0x2000000) >> 25;
1222
1223 buf_vref = (fuse->ana0 & 0x1F00000) >> 20;
1224 buf_slope = (fuse->ana0 & 0xF0000) >> 16;
1225
1226 writel(buf_vref | (buf_slope << 16), (ulong)reg_base + 0x28);
1227 writel((tca_en << 31) | (tca_hr << 16) | tca_rt,
1228 (ulong)reg_base + 0x30);
1229 }
Ye Liebb9aab2020-05-03 22:19:54 +08001230#ifdef CONFIG_IMX8MP
1231 /* Load TCALIV0/1/m40 and TRIM from fuses */
1232 struct ocotp_regs *ocotp = (struct ocotp_regs *)OCOTP_BASE_ADDR;
1233 struct fuse_bank *bank = &ocotp->bank[38];
1234 struct fuse_bank38_regs *fuse =
1235 (struct fuse_bank38_regs *)bank->fuse_regs;
1236 struct fuse_bank *bank2 = &ocotp->bank[39];
1237 struct fuse_bank39_regs *fuse2 =
1238 (struct fuse_bank39_regs *)bank2->fuse_regs;
1239 u32 buf_vref, buf_slope, bjt_cur, vlsb, bgr;
1240 u32 reg;
1241 u32 tca40[2], tca25[2], tca105[2];
1242
1243 /* For blank sample */
1244 if (!fuse->ana_trim2 && !fuse->ana_trim3 &&
1245 !fuse->ana_trim4 && !fuse2->ana_trim5) {
1246 /* Use a default 25C binary codes */
1247 tca25[0] = 1596;
Ye Li3462b552020-05-03 22:19:55 +08001248 tca25[1] = 1596;
Ye Liebb9aab2020-05-03 22:19:54 +08001249 writel(tca25[0], (ulong)reg_base + 0x30);
Ye Li3462b552020-05-03 22:19:55 +08001250 writel(tca25[1], (ulong)reg_base + 0x34);
Ye Liebb9aab2020-05-03 22:19:54 +08001251 return;
1252 }
1253
1254 buf_vref = (fuse->ana_trim2 & 0xc0) >> 6;
1255 buf_slope = (fuse->ana_trim2 & 0xF00) >> 8;
1256 bjt_cur = (fuse->ana_trim2 & 0xF000) >> 12;
1257 bgr = (fuse->ana_trim2 & 0xF0000) >> 16;
1258 vlsb = (fuse->ana_trim2 & 0xF00000) >> 20;
1259 writel(buf_vref | (buf_slope << 16), (ulong)reg_base + 0x28);
1260
1261 reg = (bgr << 28) | (bjt_cur << 20) | (vlsb << 12) | (1 << 7);
1262 writel(reg, (ulong)reg_base + 0x3c);
1263
1264 tca40[0] = (fuse->ana_trim3 & 0xFFF0000) >> 16;
1265 tca25[0] = (fuse->ana_trim3 & 0xF0000000) >> 28;
1266 tca25[0] |= ((fuse->ana_trim4 & 0xFF) << 4);
1267 tca105[0] = (fuse->ana_trim4 & 0xFFF00) >> 8;
1268 tca40[1] = (fuse->ana_trim4 & 0xFFF00000) >> 20;
1269 tca25[1] = fuse2->ana_trim5 & 0xFFF;
1270 tca105[1] = (fuse2->ana_trim5 & 0xFFF000) >> 12;
1271
1272 /* use 25c for 1p calibration */
1273 writel(tca25[0] | (tca105[0] << 16), (ulong)reg_base + 0x30);
1274 writel(tca25[1] | (tca105[1] << 16), (ulong)reg_base + 0x34);
1275 writel(tca40[0] | (tca40[1] << 16), (ulong)reg_base + 0x38);
1276#endif
Ye Li70487ff2020-05-03 22:19:52 +08001277}
Peng Fan2f3c9202020-07-09 13:39:26 +08001278
1279#if defined(CONFIG_SPL_BUILD)
1280#if defined(CONFIG_IMX8MQ) || defined(CONFIG_IMX8MM) || defined(CONFIG_IMX8MN)
1281bool serror_need_skip = true;
1282
1283void do_error(struct pt_regs *pt_regs, unsigned int esr)
1284{
1285 /*
1286 * If stack is still in ROM reserved OCRAM not switch to SPL,
1287 * it is the ROM SError
1288 */
1289 ulong sp;
1290
1291 asm volatile("mov %0, sp" : "=r"(sp) : );
1292
1293 if (serror_need_skip && sp < 0x910000 && sp >= 0x900000) {
1294 /* Check for ERR050342, imx8mq HDCP enabled parts */
1295 if (is_imx8mq() && !(readl(OCOTP_BASE_ADDR + 0x450) & 0x08000000)) {
1296 serror_need_skip = false;
1297 return; /* Do nothing skip the SError in ROM */
1298 }
1299
1300 /* Check for ERR050350, field return mode for imx8mq, mm and mn */
1301 if (readl(OCOTP_BASE_ADDR + 0x630) & 0x1) {
1302 serror_need_skip = false;
1303 return; /* Do nothing skip the SError in ROM */
1304 }
1305 }
1306
1307 efi_restore_gd();
1308 printf("\"Error\" handler, esr 0x%08x\n", esr);
1309 show_regs(pt_regs);
1310 panic("Resetting CPU ...\n");
1311}
1312#endif
1313#endif
Ye Li2707faf2019-07-15 01:16:46 -07001314
1315#if defined(CONFIG_IMX8MN) || defined(CONFIG_IMX8MP)
1316enum env_location env_get_location(enum env_operation op, int prio)
1317{
1318 enum boot_device dev = get_boot_device();
Ye Li2707faf2019-07-15 01:16:46 -07001319
1320 if (prio)
Ricardo Salveti89ca5a72021-10-20 16:16:26 -03001321 return ENVL_UNKNOWN;
Ye Li2707faf2019-07-15 01:16:46 -07001322
1323 switch (dev) {
Ye Li2707faf2019-07-15 01:16:46 -07001324 case QSPI_BOOT:
Ricardo Salveti89ca5a72021-10-20 16:16:26 -03001325 if (IS_ENABLED(CONFIG_ENV_IS_IN_SPI_FLASH))
1326 return ENVL_SPI_FLASH;
1327 return ENVL_NOWHERE;
Ye Li2707faf2019-07-15 01:16:46 -07001328 case NAND_BOOT:
Ricardo Salveti89ca5a72021-10-20 16:16:26 -03001329 if (IS_ENABLED(CONFIG_ENV_IS_IN_NAND))
1330 return ENVL_NAND;
1331 return ENVL_NOWHERE;
Ye Li2707faf2019-07-15 01:16:46 -07001332 case SD1_BOOT:
1333 case SD2_BOOT:
1334 case SD3_BOOT:
1335 case MMC1_BOOT:
1336 case MMC2_BOOT:
1337 case MMC3_BOOT:
Ricardo Salveti89ca5a72021-10-20 16:16:26 -03001338 if (IS_ENABLED(CONFIG_ENV_IS_IN_MMC))
1339 return ENVL_MMC;
1340 else if (IS_ENABLED(CONFIG_ENV_IS_IN_EXT4))
1341 return ENVL_EXT4;
1342 else if (IS_ENABLED(CONFIG_ENV_IS_IN_FAT))
1343 return ENVL_FAT;
1344 return ENVL_NOWHERE;
Ye Li2707faf2019-07-15 01:16:46 -07001345 default:
Ricardo Salveti89ca5a72021-10-20 16:16:26 -03001346 return ENVL_NOWHERE;
Ye Li2707faf2019-07-15 01:16:46 -07001347 }
Ye Li2707faf2019-07-15 01:16:46 -07001348}
1349
1350#ifndef ENV_IS_EMBEDDED
1351long long env_get_offset(long long defautl_offset)
1352{
1353 enum boot_device dev = get_boot_device();
1354
1355 switch (dev) {
1356 case NAND_BOOT:
1357 return (60 << 20); /* 60MB offset for NAND */
1358 default:
1359 break;
1360 }
1361
1362 return defautl_offset;
1363}
1364#endif
1365#endif