blob: 838f0a37496829f54a4de51cfa508dec6725c3ce [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 Glass7fe32b32022-03-04 08:43:05 -070010#include <event.h>
Simon Glass691d7192020-05-10 11:40:02 -060011#include <init.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060012#include <log.h>
Peng Fanfcdbde72018-01-10 13:20:37 +080013#include <asm/arch/imx-regs.h>
Simon Glass401d1c42020-10-30 21:38:53 -060014#include <asm/global_data.h>
Peng Fanfcdbde72018-01-10 13:20:37 +080015#include <asm/io.h>
16#include <asm/arch/clock.h>
17#include <asm/arch/sys_proto.h>
18#include <asm/mach-imx/hab.h>
19#include <asm/mach-imx/boot_mode.h>
20#include <asm/mach-imx/syscounter.h>
Peng Fan2f3c9202020-07-09 13:39:26 +080021#include <asm/ptrace.h>
Peng Fanfcdbde72018-01-10 13:20:37 +080022#include <asm/armv8/mmu.h>
Peng Fane663c702019-08-27 06:25:58 +000023#include <dm/uclass.h>
Peng Fan2f3c9202020-07-09 13:39:26 +080024#include <efi_loader.h>
Ye Li2707faf2019-07-15 01:16:46 -070025#include <env.h>
26#include <env_internal.h>
Peng Fanfcdbde72018-01-10 13:20:37 +080027#include <errno.h>
28#include <fdt_support.h>
29#include <fsl_wdog.h>
30#include <imx_sip.h>
Peng Fana2f143e2020-05-11 15:14:04 +080031#include <linux/arm-smccc.h>
Simon Glasscd93d622020-05-10 11:40:13 -060032#include <linux/bitops.h>
Peng Fanfcdbde72018-01-10 13:20:37 +080033
34DECLARE_GLOBAL_DATA_PTR;
35
Stefano Babicd714a752019-09-20 08:47:53 +020036#if defined(CONFIG_IMX_HAB)
Peng Fanfcdbde72018-01-10 13:20:37 +080037struct imx_sec_config_fuse_t const imx_sec_config_fuse = {
38 .bank = 1,
39 .word = 3,
40};
41#endif
42
43int timer_init(void)
44{
45#ifdef CONFIG_SPL_BUILD
46 struct sctr_regs *sctr = (struct sctr_regs *)SYSCNT_CTRL_BASE_ADDR;
47 unsigned long freq = readl(&sctr->cntfid0);
48
49 /* Update with accurate clock frequency */
50 asm volatile("msr cntfrq_el0, %0" : : "r" (freq) : "memory");
51
52 clrsetbits_le32(&sctr->cntcr, SC_CNTCR_FREQ0 | SC_CNTCR_FREQ1,
53 SC_CNTCR_FREQ0 | SC_CNTCR_ENABLE | SC_CNTCR_HDBG);
54#endif
55
56 gd->arch.tbl = 0;
57 gd->arch.tbu = 0;
58
59 return 0;
60}
61
62void enable_tzc380(void)
63{
64 struct iomuxc_gpr_base_regs *gpr =
65 (struct iomuxc_gpr_base_regs *)IOMUXC_GPR_BASE_ADDR;
66
67 /* Enable TZASC and lock setting */
68 setbits_le32(&gpr->gpr[10], GPR_TZASC_EN);
69 setbits_le32(&gpr->gpr[10], GPR_TZASC_EN_LOCK);
Andrey Zhizhikin1289ff72022-01-24 21:48:09 +010070
71 /*
72 * According to TRM, TZASC_ID_SWAP_BYPASS should be set in
73 * order to avoid AXI Bus errors when GPU is in use
74 */
Peng Fana07c7182019-12-27 10:19:42 +080075 if (is_imx8mm() || is_imx8mn() || is_imx8mp())
Andrey Zhizhikin1289ff72022-01-24 21:48:09 +010076 setbits_le32(&gpr->gpr[10], GPR_TZASC_ID_SWAP_BYPASS);
77
78 /*
79 * imx8mn and imx8mp implements the lock bit for
80 * TZASC_ID_SWAP_BYPASS, enable it to lock settings
81 */
82 if (is_imx8mn() || is_imx8mp())
83 setbits_le32(&gpr->gpr[10], GPR_TZASC_ID_SWAP_BYPASS_LOCK);
84
Ye Lib3cf0a82019-08-27 06:25:34 +000085 /*
86 * set Region 0 attribute to allow secure and non-secure
87 * read/write permission. Found some masters like usb dwc3
88 * controllers can't work with secure memory.
89 */
90 writel(0xf0000000, TZASC_BASE_ADDR + 0x108);
Peng Fanfcdbde72018-01-10 13:20:37 +080091}
92
93void set_wdog_reset(struct wdog_regs *wdog)
94{
95 /*
96 * Output WDOG_B signal to reset external pmic or POR_B decided by
97 * the board design. Without external reset, the peripherals/DDR/
98 * PMIC are not reset, that may cause system working abnormal.
99 * WDZST bit is write-once only bit. Align this bit in kernel,
100 * otherwise kernel code will have no chance to set this bit.
101 */
102 setbits_le16(&wdog->wcr, WDOG_WDT_MASK | WDOG_WDZST_MASK);
103}
104
105static struct mm_region imx8m_mem_map[] = {
106 {
107 /* ROM */
108 .virt = 0x0UL,
109 .phys = 0x0UL,
110 .size = 0x100000UL,
111 .attrs = PTE_BLOCK_MEMTYPE(MT_NORMAL) |
112 PTE_BLOCK_OUTER_SHARE
113 }, {
Gary Bissoncb158852018-11-14 17:55:28 +0100114 /* CAAM */
115 .virt = 0x100000UL,
116 .phys = 0x100000UL,
117 .size = 0x8000UL,
118 .attrs = PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
119 PTE_BLOCK_NON_SHARE |
120 PTE_BLOCK_PXN | PTE_BLOCK_UXN
121 }, {
Marek Vasut09d86ea2021-02-25 21:52:26 +0100122 /* OCRAM_S */
123 .virt = 0x180000UL,
124 .phys = 0x180000UL,
125 .size = 0x8000UL,
126 .attrs = PTE_BLOCK_MEMTYPE(MT_NORMAL) |
127 PTE_BLOCK_OUTER_SHARE
128 }, {
Gary Bissoncb158852018-11-14 17:55:28 +0100129 /* TCM */
130 .virt = 0x7C0000UL,
131 .phys = 0x7C0000UL,
132 .size = 0x80000UL,
133 .attrs = PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
134 PTE_BLOCK_NON_SHARE |
135 PTE_BLOCK_PXN | PTE_BLOCK_UXN
136 }, {
Peng Fanfcdbde72018-01-10 13:20:37 +0800137 /* OCRAM */
138 .virt = 0x900000UL,
139 .phys = 0x900000UL,
140 .size = 0x200000UL,
141 .attrs = PTE_BLOCK_MEMTYPE(MT_NORMAL) |
142 PTE_BLOCK_OUTER_SHARE
143 }, {
144 /* AIPS */
145 .virt = 0xB00000UL,
146 .phys = 0xB00000UL,
147 .size = 0x3f500000UL,
148 .attrs = PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
149 PTE_BLOCK_NON_SHARE |
150 PTE_BLOCK_PXN | PTE_BLOCK_UXN
151 }, {
152 /* DRAM1 */
153 .virt = 0x40000000UL,
154 .phys = 0x40000000UL,
Peng Fan59efa6b2019-08-27 06:25:27 +0000155 .size = PHYS_SDRAM_SIZE,
Peng Fanfcdbde72018-01-10 13:20:37 +0800156 .attrs = PTE_BLOCK_MEMTYPE(MT_NORMAL) |
157 PTE_BLOCK_OUTER_SHARE
Peng Fan59efa6b2019-08-27 06:25:27 +0000158#ifdef PHYS_SDRAM_2_SIZE
Peng Fanfcdbde72018-01-10 13:20:37 +0800159 }, {
160 /* DRAM2 */
161 .virt = 0x100000000UL,
162 .phys = 0x100000000UL,
Peng Fan59efa6b2019-08-27 06:25:27 +0000163 .size = PHYS_SDRAM_2_SIZE,
Peng Fanfcdbde72018-01-10 13:20:37 +0800164 .attrs = PTE_BLOCK_MEMTYPE(MT_NORMAL) |
165 PTE_BLOCK_OUTER_SHARE
Peng Fan59efa6b2019-08-27 06:25:27 +0000166#endif
Peng Fanfcdbde72018-01-10 13:20:37 +0800167 }, {
Peng Fan3c417282020-07-09 15:26:06 +0800168 /* empty entrie to split table entry 5 if needed when TEEs are used */
169 0,
170 }, {
Peng Fanfcdbde72018-01-10 13:20:37 +0800171 /* List terminator */
172 0,
173 }
174};
175
176struct mm_region *mem_map = imx8m_mem_map;
177
Marek Vasut076dc922021-02-27 14:59:00 +0100178static unsigned int imx8m_find_dram_entry_in_mem_map(void)
179{
180 int i;
181
182 for (i = 0; i < ARRAY_SIZE(imx8m_mem_map); i++)
183 if (imx8m_mem_map[i].phys == CONFIG_SYS_SDRAM_BASE)
184 return i;
185
186 hang(); /* Entry not found, this must never happen. */
187}
188
Peng Fan59efa6b2019-08-27 06:25:27 +0000189void enable_caches(void)
190{
Peng Fan3c417282020-07-09 15:26:06 +0800191 /* If OPTEE runs, remove OPTEE memory from MMU table to avoid speculative prefetch */
192 if (rom_pointer[1]) {
193 /*
194 * TEE are loaded, So the ddr bank structures
195 * have been modified update mmu table accordingly
196 */
197 int i = 0;
198 /*
199 * please make sure that entry initial value matches
200 * imx8m_mem_map for DRAM1
201 */
Marek Vasut076dc922021-02-27 14:59:00 +0100202 int entry = imx8m_find_dram_entry_in_mem_map();
Peng Fan3c417282020-07-09 15:26:06 +0800203 u64 attrs = imx8m_mem_map[entry].attrs;
204
Marek Vasut076dc922021-02-27 14:59:00 +0100205 while (i < CONFIG_NR_DRAM_BANKS &&
206 entry < ARRAY_SIZE(imx8m_mem_map)) {
Peng Fan3c417282020-07-09 15:26:06 +0800207 if (gd->bd->bi_dram[i].start == 0)
208 break;
209 imx8m_mem_map[entry].phys = gd->bd->bi_dram[i].start;
210 imx8m_mem_map[entry].virt = gd->bd->bi_dram[i].start;
211 imx8m_mem_map[entry].size = gd->bd->bi_dram[i].size;
212 imx8m_mem_map[entry].attrs = attrs;
213 debug("Added memory mapping (%d): %llx %llx\n", entry,
214 imx8m_mem_map[entry].phys, imx8m_mem_map[entry].size);
215 i++; entry++;
216 }
217 }
Peng Fan59efa6b2019-08-27 06:25:27 +0000218
219 icache_enable();
220 dcache_enable();
221}
222
Peng Fan3c417282020-07-09 15:26:06 +0800223__weak int board_phys_sdram_size(phys_size_t *size)
224{
225 if (!size)
226 return -EINVAL;
227
228 *size = PHYS_SDRAM_SIZE;
229 return 0;
230}
231
232int dram_init(void)
233{
Marek Vasut076dc922021-02-27 14:59:00 +0100234 unsigned int entry = imx8m_find_dram_entry_in_mem_map();
Peng Fan3c417282020-07-09 15:26:06 +0800235 phys_size_t sdram_size;
236 int ret;
237
238 ret = board_phys_sdram_size(&sdram_size);
239 if (ret)
240 return ret;
241
242 /* rom_pointer[1] contains the size of TEE occupies */
243 if (rom_pointer[1])
244 gd->ram_size = sdram_size - rom_pointer[1];
245 else
246 gd->ram_size = sdram_size;
247
Tim Harveybc479b22020-09-25 08:08:35 -0700248 /* also update the SDRAM size in the mem_map used externally */
Marek Vasut076dc922021-02-27 14:59:00 +0100249 imx8m_mem_map[entry].size = sdram_size;
Tim Harveybc479b22020-09-25 08:08:35 -0700250
Peng Fan3c417282020-07-09 15:26:06 +0800251#ifdef PHYS_SDRAM_2_SIZE
252 gd->ram_size += PHYS_SDRAM_2_SIZE;
253#endif
254
255 return 0;
256}
257
258int dram_init_banksize(void)
259{
260 int bank = 0;
261 int ret;
262 phys_size_t sdram_size;
263
264 ret = board_phys_sdram_size(&sdram_size);
265 if (ret)
266 return ret;
267
268 gd->bd->bi_dram[bank].start = PHYS_SDRAM;
269 if (rom_pointer[1]) {
270 phys_addr_t optee_start = (phys_addr_t)rom_pointer[0];
271 phys_size_t optee_size = (size_t)rom_pointer[1];
272
273 gd->bd->bi_dram[bank].size = optee_start - gd->bd->bi_dram[bank].start;
274 if ((optee_start + optee_size) < (PHYS_SDRAM + sdram_size)) {
275 if (++bank >= CONFIG_NR_DRAM_BANKS) {
276 puts("CONFIG_NR_DRAM_BANKS is not enough\n");
277 return -1;
278 }
279
280 gd->bd->bi_dram[bank].start = optee_start + optee_size;
281 gd->bd->bi_dram[bank].size = PHYS_SDRAM +
282 sdram_size - gd->bd->bi_dram[bank].start;
283 }
284 } else {
285 gd->bd->bi_dram[bank].size = sdram_size;
286 }
287
288#ifdef PHYS_SDRAM_2_SIZE
289 if (++bank >= CONFIG_NR_DRAM_BANKS) {
290 puts("CONFIG_NR_DRAM_BANKS is not enough for SDRAM_2\n");
291 return -1;
292 }
293 gd->bd->bi_dram[bank].start = PHYS_SDRAM_2;
294 gd->bd->bi_dram[bank].size = PHYS_SDRAM_2_SIZE;
295#endif
296
297 return 0;
298}
299
300phys_size_t get_effective_memsize(void)
301{
302 /* return the first bank as effective memory */
303 if (rom_pointer[1])
304 return ((phys_addr_t)rom_pointer[0] - PHYS_SDRAM);
305
306#ifdef PHYS_SDRAM_2_SIZE
307 return gd->ram_size - PHYS_SDRAM_2_SIZE;
308#else
309 return gd->ram_size;
310#endif
311}
312
Frieder Schrempfe27bddf2021-06-07 14:36:44 +0200313ulong board_get_usable_ram_top(ulong total_size)
314{
Ying-Chun Liu (PaulLiu)5a6af8c2021-08-23 10:43:06 +0800315 ulong top_addr = PHYS_SDRAM + gd->ram_size;
316
Frieder Schrempfe27bddf2021-06-07 14:36:44 +0200317 /*
318 * Some IPs have their accessible address space restricted by
319 * the interconnect. Let's make sure U-Boot only ever uses the
320 * space below the 4G address boundary (which is 3GiB big),
321 * even when the effective available memory is bigger.
322 */
Ying-Chun Liu (PaulLiu)5a6af8c2021-08-23 10:43:06 +0800323 if (top_addr > 0x80000000)
324 top_addr = 0x80000000;
Frieder Schrempfe27bddf2021-06-07 14:36:44 +0200325
Ying-Chun Liu (PaulLiu)5a6af8c2021-08-23 10:43:06 +0800326 /*
327 * rom_pointer[0] stores the TEE memory start address.
328 * rom_pointer[1] stores the size TEE uses.
329 * We need to reserve the memory region for TEE.
330 */
331 if (rom_pointer[0] && rom_pointer[1] && top_addr > rom_pointer[0])
332 top_addr = rom_pointer[0];
333
334 return top_addr;
Frieder Schrempfe27bddf2021-06-07 14:36:44 +0200335}
336
Peng Fan78db9a52019-08-27 06:25:17 +0000337static u32 get_cpu_variant_type(u32 type)
338{
339 struct ocotp_regs *ocotp = (struct ocotp_regs *)OCOTP_BASE_ADDR;
340 struct fuse_bank *bank = &ocotp->bank[1];
341 struct fuse_bank1_regs *fuse =
342 (struct fuse_bank1_regs *)bank->fuse_regs;
343
344 u32 value = readl(&fuse->tester4);
345
Peng Fancb1a1de2020-02-05 17:34:54 +0800346 if (type == MXC_CPU_IMX8MQ) {
347 if ((value & 0x3) == 0x2)
348 return MXC_CPU_IMX8MD;
349 else if (value & 0x200000)
350 return MXC_CPU_IMX8MQL;
351
352 } else if (type == MXC_CPU_IMX8MM) {
Peng Fan78db9a52019-08-27 06:25:17 +0000353 switch (value & 0x3) {
354 case 2:
355 if (value & 0x1c0000)
356 return MXC_CPU_IMX8MMDL;
357 else
358 return MXC_CPU_IMX8MMD;
359 case 3:
360 if (value & 0x1c0000)
361 return MXC_CPU_IMX8MMSL;
362 else
363 return MXC_CPU_IMX8MMS;
364 default:
365 if (value & 0x1c0000)
366 return MXC_CPU_IMX8MML;
367 break;
368 }
Peng Fanc9154032020-02-05 17:39:27 +0800369 } else if (type == MXC_CPU_IMX8MN) {
370 switch (value & 0x3) {
371 case 2:
Ye Lic4f78cb2021-03-19 15:57:11 +0800372 if (value & 0x1000000) {
373 if (value & 0x10000000) /* MIPI DSI */
374 return MXC_CPU_IMX8MNUD;
375 else
376 return MXC_CPU_IMX8MNDL;
377 } else {
Peng Fanc9154032020-02-05 17:39:27 +0800378 return MXC_CPU_IMX8MND;
Ye Lic4f78cb2021-03-19 15:57:11 +0800379 }
Peng Fanc9154032020-02-05 17:39:27 +0800380 case 3:
Ye Lic4f78cb2021-03-19 15:57:11 +0800381 if (value & 0x1000000) {
382 if (value & 0x10000000) /* MIPI DSI */
383 return MXC_CPU_IMX8MNUS;
384 else
385 return MXC_CPU_IMX8MNSL;
386 } else {
Peng Fanc9154032020-02-05 17:39:27 +0800387 return MXC_CPU_IMX8MNS;
Ye Lic4f78cb2021-03-19 15:57:11 +0800388 }
Peng Fanc9154032020-02-05 17:39:27 +0800389 default:
Ye Lic4f78cb2021-03-19 15:57:11 +0800390 if (value & 0x1000000) {
391 if (value & 0x10000000) /* MIPI DSI */
392 return MXC_CPU_IMX8MNUQ;
393 else
394 return MXC_CPU_IMX8MNL;
395 }
Peng Fanc9154032020-02-05 17:39:27 +0800396 break;
397 }
Ye Lid1eee7e2020-04-20 20:12:54 -0700398 } else if (type == MXC_CPU_IMX8MP) {
399 u32 value0 = readl(&fuse->tester3);
400 u32 flag = 0;
401
402 if ((value0 & 0xc0000) == 0x80000)
403 return MXC_CPU_IMX8MPD;
404
405 /* vpu disabled */
406 if ((value0 & 0x43000000) == 0x43000000)
407 flag = 1;
408
409 /* npu disabled*/
410 if ((value & 0x8) == 0x8)
411 flag |= (1 << 1);
412
413 /* isp disabled */
414 if ((value & 0x3) == 0x3)
415 flag |= (1 << 2);
416
417 switch (flag) {
418 case 7:
419 return MXC_CPU_IMX8MPL;
Ye Lid1eee7e2020-04-20 20:12:54 -0700420 case 2:
421 return MXC_CPU_IMX8MP6;
Ye Lid1eee7e2020-04-20 20:12:54 -0700422 default:
423 break;
424 }
425
Peng Fan78db9a52019-08-27 06:25:17 +0000426 }
427
428 return type;
429}
430
Peng Fanfcdbde72018-01-10 13:20:37 +0800431u32 get_cpu_rev(void)
432{
433 struct anamix_pll *ana_pll = (struct anamix_pll *)ANATOP_BASE_ADDR;
434 u32 reg = readl(&ana_pll->digprog);
435 u32 type = (reg >> 16) & 0xff;
Peng Fan78db9a52019-08-27 06:25:17 +0000436 u32 major_low = (reg >> 8) & 0xff;
Peng Fanfcdbde72018-01-10 13:20:37 +0800437 u32 rom_version;
438
439 reg &= 0xff;
440
Peng Fan625b03d2019-12-27 10:14:02 +0800441 /* iMX8MP */
442 if (major_low == 0x43) {
Ye Lid1eee7e2020-04-20 20:12:54 -0700443 type = get_cpu_variant_type(MXC_CPU_IMX8MP);
Peng Fan625b03d2019-12-27 10:14:02 +0800444 } else if (major_low == 0x42) {
445 /* iMX8MN */
Peng Fanc9154032020-02-05 17:39:27 +0800446 type = get_cpu_variant_type(MXC_CPU_IMX8MN);
Peng Fan24341312019-06-27 17:23:49 +0800447 } else if (major_low == 0x41) {
Peng Fan78db9a52019-08-27 06:25:17 +0000448 type = get_cpu_variant_type(MXC_CPU_IMX8MM);
449 } else {
450 if (reg == CHIP_REV_1_0) {
451 /*
Peng Fan9e094452019-10-16 10:24:17 +0000452 * For B0 chip, the DIGPROG is not updated,
453 * it is still TO1.0. we have to check ROM
454 * version or OCOTP_READ_FUSE_DATA.
455 * 0xff0055aa is magic number for B1.
Peng Fan78db9a52019-08-27 06:25:17 +0000456 */
Peng Fan9e094452019-10-16 10:24:17 +0000457 if (readl((void __iomem *)(OCOTP_BASE_ADDR + 0x40)) == 0xff0055aa) {
Ye Liea2b26fb2021-03-19 15:57:16 +0800458 /*
459 * B2 uses same DIGPROG and OCOTP_READ_FUSE_DATA value with B1,
460 * so have to check ROM to distinguish them
461 */
462 rom_version = readl((void __iomem *)ROM_VERSION_B0);
463 rom_version &= 0xff;
464 if (rom_version == CHIP_REV_2_2)
465 reg = CHIP_REV_2_2;
466 else
467 reg = CHIP_REV_2_1;
Peng Fan9e094452019-10-16 10:24:17 +0000468 } else {
469 rom_version =
470 readl((void __iomem *)ROM_VERSION_A0);
471 if (rom_version != CHIP_REV_1_0) {
472 rom_version = readl((void __iomem *)ROM_VERSION_B0);
Patrick Wildt6a4b07e2019-11-19 09:42:06 +0100473 rom_version &= 0xff;
Peng Fan9e094452019-10-16 10:24:17 +0000474 if (rom_version == CHIP_REV_2_0)
475 reg = CHIP_REV_2_0;
476 }
Peng Fan78db9a52019-08-27 06:25:17 +0000477 }
Peng Fanfcdbde72018-01-10 13:20:37 +0800478 }
Peng Fancb1a1de2020-02-05 17:34:54 +0800479
480 type = get_cpu_variant_type(type);
Peng Fanfcdbde72018-01-10 13:20:37 +0800481 }
482
483 return (type << 12) | reg;
484}
485
486static void imx_set_wdog_powerdown(bool enable)
487{
488 struct wdog_regs *wdog1 = (struct wdog_regs *)WDOG1_BASE_ADDR;
489 struct wdog_regs *wdog2 = (struct wdog_regs *)WDOG2_BASE_ADDR;
490 struct wdog_regs *wdog3 = (struct wdog_regs *)WDOG3_BASE_ADDR;
491
492 /* Write to the PDE (Power Down Enable) bit */
493 writew(enable, &wdog1->wmcr);
494 writew(enable, &wdog2->wmcr);
495 writew(enable, &wdog3->wmcr);
496}
497
Simon Glass7fe32b32022-03-04 08:43:05 -0700498static int imx8m_check_clock(void *ctx, struct event *event)
Peng Fane663c702019-08-27 06:25:58 +0000499{
500 struct udevice *dev;
501 int ret;
502
Peng Fancd7c8062019-10-16 03:01:51 +0000503 if (CONFIG_IS_ENABLED(CLK)) {
504 ret = uclass_get_device_by_name(UCLASS_CLK,
505 "clock-controller@30380000",
506 &dev);
507 if (ret < 0) {
508 printf("Failed to find clock node. Check device tree\n");
509 return ret;
510 }
Peng Fane663c702019-08-27 06:25:58 +0000511 }
512
513 return 0;
514}
Simon Glass7fe32b32022-03-04 08:43:05 -0700515EVENT_SPY(EVT_DM_POST_INIT, imx8m_check_clock);
Peng Fane663c702019-08-27 06:25:58 +0000516
Peng Fanfcdbde72018-01-10 13:20:37 +0800517int arch_cpu_init(void)
518{
Peng Fan702339b2019-04-17 09:41:16 +0000519 struct ocotp_regs *ocotp = (struct ocotp_regs *)OCOTP_BASE_ADDR;
Peng Fanfcdbde72018-01-10 13:20:37 +0800520 /*
Peng Fan0528ba02019-08-27 06:25:37 +0000521 * ROM might disable clock for SCTR,
522 * enable the clock before timer_init.
523 */
524 if (IS_ENABLED(CONFIG_SPL_BUILD))
525 clock_enable(CCGR_SCTR, 1);
526 /*
Peng Fanfcdbde72018-01-10 13:20:37 +0800527 * Init timer at very early state, because sscg pll setting
528 * will use it
529 */
530 timer_init();
531
532 if (IS_ENABLED(CONFIG_SPL_BUILD)) {
533 clock_init();
534 imx_set_wdog_powerdown(false);
Peng Fan7a42bf02020-07-09 13:52:41 +0800535
536 if (is_imx8md() || is_imx8mmd() || is_imx8mmdl() || is_imx8mms() ||
537 is_imx8mmsl() || is_imx8mnd() || is_imx8mndl() || is_imx8mns() ||
Ye Lic4f78cb2021-03-19 15:57:11 +0800538 is_imx8mnsl() || is_imx8mpd() || is_imx8mnud() || is_imx8mnus()) {
Peng Fan7a42bf02020-07-09 13:52:41 +0800539 /* Power down cpu core 1, 2 and 3 for iMX8M Dual core or Single core */
540 struct pgc_reg *pgc_core1 = (struct pgc_reg *)(GPC_BASE_ADDR + 0x840);
541 struct pgc_reg *pgc_core2 = (struct pgc_reg *)(GPC_BASE_ADDR + 0x880);
542 struct pgc_reg *pgc_core3 = (struct pgc_reg *)(GPC_BASE_ADDR + 0x8C0);
543 struct gpc_reg *gpc = (struct gpc_reg *)GPC_BASE_ADDR;
544
545 writel(0x1, &pgc_core2->pgcr);
546 writel(0x1, &pgc_core3->pgcr);
Ye Lic4f78cb2021-03-19 15:57:11 +0800547 if (is_imx8mms() || is_imx8mmsl() || is_imx8mns() || is_imx8mnsl() || is_imx8mnus()) {
Peng Fan7a42bf02020-07-09 13:52:41 +0800548 writel(0x1, &pgc_core1->pgcr);
549 writel(0xE, &gpc->cpu_pgc_dn_trg);
550 } else {
551 writel(0xC, &gpc->cpu_pgc_dn_trg);
552 }
553 }
Peng Fanfcdbde72018-01-10 13:20:37 +0800554 }
555
Peng Fan702339b2019-04-17 09:41:16 +0000556 if (is_imx8mq()) {
557 clock_enable(CCGR_OCOTP, 1);
558 if (readl(&ocotp->ctrl) & 0x200)
559 writel(0x200, &ocotp->ctrl_clr);
560 }
561
Peng Fanfcdbde72018-01-10 13:20:37 +0800562 return 0;
563}
564
Peng Fanb1821372019-09-16 03:09:36 +0000565#if defined(CONFIG_IMX8MN) || defined(CONFIG_IMX8MP)
566struct rom_api *g_rom_api = (struct rom_api *)0x980;
567
568enum boot_device get_boot_device(void)
569{
570 volatile gd_t *pgd = gd;
571 int ret;
572 u32 boot;
573 u16 boot_type;
574 u8 boot_instance;
575 enum boot_device boot_dev = SD1_BOOT;
576
577 ret = g_rom_api->query_boot_infor(QUERY_BT_DEV, &boot,
578 ((uintptr_t)&boot) ^ QUERY_BT_DEV);
Marek Behún86c5e212021-05-20 13:24:10 +0200579 set_gd(pgd);
Peng Fanb1821372019-09-16 03:09:36 +0000580
581 if (ret != ROM_API_OKAY) {
582 puts("ROMAPI: failure at query_boot_info\n");
583 return -1;
584 }
585
586 boot_type = boot >> 16;
587 boot_instance = (boot >> 8) & 0xff;
588
589 switch (boot_type) {
590 case BT_DEV_TYPE_SD:
591 boot_dev = boot_instance + SD1_BOOT;
592 break;
593 case BT_DEV_TYPE_MMC:
594 boot_dev = boot_instance + MMC1_BOOT;
595 break;
596 case BT_DEV_TYPE_NAND:
597 boot_dev = NAND_BOOT;
598 break;
599 case BT_DEV_TYPE_FLEXSPINOR:
600 boot_dev = QSPI_BOOT;
601 break;
602 case BT_DEV_TYPE_USB:
603 boot_dev = USB_BOOT;
604 break;
605 default:
606 break;
607 }
608
609 return boot_dev;
610}
611#endif
612
Marek Vasuta5ee05c2021-07-03 04:55:33 +0200613#if defined(CONFIG_IMX8M)
614#include <spl.h>
615int spl_mmc_emmc_boot_partition(struct mmc *mmc)
616{
617 u32 *rom_log_addr = (u32 *)0x9e0;
618 u32 *rom_log;
619 u8 event_id;
620 int i, part;
621
622 part = default_spl_mmc_emmc_boot_partition(mmc);
623
624 /* If the ROM event log pointer is not valid. */
625 if (*rom_log_addr < 0x900000 || *rom_log_addr >= 0xb00000 ||
626 *rom_log_addr & 0x3)
627 return part;
628
629 /* Parse the ROM event ID version 2 log */
630 rom_log = (u32 *)(uintptr_t)(*rom_log_addr);
631 for (i = 0; i < 128; i++) {
632 event_id = rom_log[i] >> 24;
633 switch (event_id) {
634 case 0x00: /* End of list */
635 return part;
636 /* Log entries with 1 parameter, skip 1 */
637 case 0x80: /* Start to perform the device initialization */
638 case 0x81: /* The boot device initialization completes */
639 case 0x8f: /* The boot device initialization fails */
640 case 0x90: /* Start to read data from boot device */
641 case 0x91: /* Reading data from boot device completes */
642 case 0x9f: /* Reading data from boot device fails */
643 i += 1;
644 continue;
645 /* Log entries with 2 parameters, skip 2 */
646 case 0xa0: /* Image authentication result */
647 case 0xc0: /* Jump to the boot image soon */
648 i += 2;
649 continue;
650 /* Boot from the secondary boot image */
651 case 0x51:
652 /*
653 * Swap the eMMC boot partitions in case there was a
654 * fallback event (i.e. primary image was corrupted
655 * and that corruption was recognized by the BootROM),
656 * so the SPL loads the rest of the U-Boot from the
657 * correct eMMC boot partition, since the BootROM
658 * leaves the boot partition set to the corrupted one.
659 */
660 if (part == 1)
661 part = 2;
662 else if (part == 2)
663 part = 1;
664 continue;
665 default:
666 continue;
667 }
668 }
669
670 return part;
671}
672#endif
673
Peng Fanfcdbde72018-01-10 13:20:37 +0800674bool is_usb_boot(void)
675{
676 return get_boot_device() == USB_BOOT;
677}
678
679#ifdef CONFIG_OF_SYSTEM_SETUP
Peng Fan6036dba2020-07-09 14:06:49 +0800680bool check_fdt_new_path(void *blob)
681{
682 const char *soc_path = "/soc@0";
683 int nodeoff;
684
685 nodeoff = fdt_path_offset(blob, soc_path);
686 if (nodeoff < 0)
687 return false;
688
689 return true;
690}
691
692static int disable_fdt_nodes(void *blob, const char *const nodes_path[], int size_array)
Peng Fanfcdbde72018-01-10 13:20:37 +0800693{
694 int i = 0;
695 int rc;
696 int nodeoff;
Peng Fan6036dba2020-07-09 14:06:49 +0800697 const char *status = "disabled";
698
699 for (i = 0; i < size_array; i++) {
700 nodeoff = fdt_path_offset(blob, nodes_path[i]);
701 if (nodeoff < 0)
702 continue; /* Not found, skip it */
703
704 printf("Found %s node\n", nodes_path[i]);
705
706add_status:
707 rc = fdt_setprop(blob, nodeoff, "status", status, strlen(status) + 1);
708 if (rc) {
709 if (rc == -FDT_ERR_NOSPACE) {
710 rc = fdt_increase_size(blob, 512);
711 if (!rc)
712 goto add_status;
713 }
714 printf("Unable to update property %s:%s, err=%s\n",
715 nodes_path[i], "status", fdt_strerror(rc));
716 } else {
717 printf("Modify %s:%s disabled\n",
718 nodes_path[i], "status");
719 }
720 }
721
722 return 0;
723}
724
725#ifdef CONFIG_IMX8MQ
726bool check_dcss_fused(void)
727{
728 struct ocotp_regs *ocotp = (struct ocotp_regs *)OCOTP_BASE_ADDR;
729 struct fuse_bank *bank = &ocotp->bank[1];
730 struct fuse_bank1_regs *fuse =
731 (struct fuse_bank1_regs *)bank->fuse_regs;
732 u32 value = readl(&fuse->tester4);
733
734 if (value & 0x4000000)
735 return true;
736
737 return false;
738}
739
740static int disable_mipi_dsi_nodes(void *blob)
741{
742 static const char * const nodes_path[] = {
743 "/mipi_dsi@30A00000",
744 "/mipi_dsi_bridge@30A00000",
745 "/dsi_phy@30A00300",
746 "/soc@0/bus@30800000/mipi_dsi@30a00000",
Peng Fanc0e2f762021-03-19 15:57:13 +0800747 "/soc@0/bus@30800000/dphy@30a00300",
748 "/soc@0/bus@30800000/mipi-dsi@30a00000",
Peng Fan6036dba2020-07-09 14:06:49 +0800749 };
750
751 return disable_fdt_nodes(blob, nodes_path, ARRAY_SIZE(nodes_path));
752}
753
754static int disable_dcss_nodes(void *blob)
755{
756 static const char * const nodes_path[] = {
757 "/dcss@0x32e00000",
758 "/dcss@32e00000",
759 "/hdmi@32c00000",
760 "/hdmi_cec@32c33800",
761 "/hdmi_drm@32c00000",
762 "/display-subsystem",
763 "/sound-hdmi",
764 "/sound-hdmi-arc",
765 "/soc@0/bus@32c00000/display-controller@32e00000",
766 "/soc@0/bus@32c00000/hdmi@32c00000",
767 };
768
769 return disable_fdt_nodes(blob, nodes_path, ARRAY_SIZE(nodes_path));
770}
771
772static int check_mipi_dsi_nodes(void *blob)
773{
774 static const char * const lcdif_path[] = {
775 "/lcdif@30320000",
Peng Fanc0e2f762021-03-19 15:57:13 +0800776 "/soc@0/bus@30000000/lcdif@30320000",
777 "/soc@0/bus@30000000/lcd-controller@30320000"
Peng Fan6036dba2020-07-09 14:06:49 +0800778 };
779 static const char * const mipi_dsi_path[] = {
780 "/mipi_dsi@30A00000",
781 "/soc@0/bus@30800000/mipi_dsi@30a00000"
782 };
783 static const char * const lcdif_ep_path[] = {
784 "/lcdif@30320000/port@0/mipi-dsi-endpoint",
Peng Fanc0e2f762021-03-19 15:57:13 +0800785 "/soc@0/bus@30000000/lcdif@30320000/port@0/endpoint",
786 "/soc@0/bus@30000000/lcd-controller@30320000/port@0/endpoint"
Peng Fan6036dba2020-07-09 14:06:49 +0800787 };
788 static const char * const mipi_dsi_ep_path[] = {
789 "/mipi_dsi@30A00000/port@1/endpoint",
Peng Fanc0e2f762021-03-19 15:57:13 +0800790 "/soc@0/bus@30800000/mipi_dsi@30a00000/ports/port@0/endpoint",
791 "/soc@0/bus@30800000/mipi-dsi@30a00000/ports/port@0/endpoint@0"
Peng Fan6036dba2020-07-09 14:06:49 +0800792 };
793
794 int lookup_node;
795 int nodeoff;
796 bool new_path = check_fdt_new_path(blob);
797 int i = new_path ? 1 : 0;
798
799 nodeoff = fdt_path_offset(blob, lcdif_path[i]);
800 if (nodeoff < 0 || !fdtdec_get_is_enabled(blob, nodeoff)) {
801 /*
802 * If can't find lcdif node or lcdif node is disabled,
803 * then disable all mipi dsi, since they only can input
804 * from DCSS
805 */
806 return disable_mipi_dsi_nodes(blob);
807 }
808
809 nodeoff = fdt_path_offset(blob, mipi_dsi_path[i]);
810 if (nodeoff < 0 || !fdtdec_get_is_enabled(blob, nodeoff))
811 return 0;
812
813 nodeoff = fdt_path_offset(blob, lcdif_ep_path[i]);
814 if (nodeoff < 0) {
815 /*
816 * If can't find lcdif endpoint, then disable all mipi dsi,
817 * since they only can input from DCSS
818 */
819 return disable_mipi_dsi_nodes(blob);
820 }
821
822 lookup_node = fdtdec_lookup_phandle(blob, nodeoff, "remote-endpoint");
823 nodeoff = fdt_path_offset(blob, mipi_dsi_ep_path[i]);
824
825 if (nodeoff > 0 && nodeoff == lookup_node)
826 return 0;
827
828 return disable_mipi_dsi_nodes(blob);
829}
830#endif
831
832int disable_vpu_nodes(void *blob)
833{
834 static const char * const nodes_path_8mq[] = {
835 "/vpu@38300000",
836 "/soc@0/vpu@38300000"
837 };
838
839 static const char * const nodes_path_8mm[] = {
840 "/vpu_g1@38300000",
841 "/vpu_g2@38310000",
842 "/vpu_h1@38320000"
843 };
844
845 static const char * const nodes_path_8mp[] = {
846 "/vpu_g1@38300000",
847 "/vpu_g2@38310000",
848 "/vpu_vc8000e@38320000"
849 };
850
851 if (is_imx8mq())
852 return disable_fdt_nodes(blob, nodes_path_8mq, ARRAY_SIZE(nodes_path_8mq));
853 else if (is_imx8mm())
854 return disable_fdt_nodes(blob, nodes_path_8mm, ARRAY_SIZE(nodes_path_8mm));
855 else if (is_imx8mp())
856 return disable_fdt_nodes(blob, nodes_path_8mp, ARRAY_SIZE(nodes_path_8mp));
857 else
858 return -EPERM;
859}
860
Ye Li98bcdf12021-03-19 15:57:09 +0800861#ifdef CONFIG_IMX8MN_LOW_DRIVE_MODE
862static int low_drive_gpu_freq(void *blob)
863{
864 static const char *nodes_path_8mn[] = {
865 "/gpu@38000000",
866 "/soc@0/gpu@38000000"
867 };
868
869 int nodeoff, cnt, i;
870 u32 assignedclks[7];
871
872 nodeoff = fdt_path_offset(blob, nodes_path_8mn[0]);
873 if (nodeoff < 0)
874 return nodeoff;
875
876 cnt = fdtdec_get_int_array_count(blob, nodeoff, "assigned-clock-rates", assignedclks, 7);
877 if (cnt < 0)
878 return cnt;
879
880 if (cnt != 7)
881 printf("Warning: %s, assigned-clock-rates count %d\n", nodes_path_8mn[0], cnt);
882
883 assignedclks[cnt - 1] = 200000000;
884 assignedclks[cnt - 2] = 200000000;
885
886 for (i = 0; i < cnt; i++) {
887 debug("<%u>, ", assignedclks[i]);
888 assignedclks[i] = cpu_to_fdt32(assignedclks[i]);
889 }
890 debug("\n");
891
892 return fdt_setprop(blob, nodeoff, "assigned-clock-rates", &assignedclks, sizeof(assignedclks));
893}
894#endif
895
Peng Fan6036dba2020-07-09 14:06:49 +0800896int disable_gpu_nodes(void *blob)
897{
898 static const char * const nodes_path_8mn[] = {
Peng Fanc0e2f762021-03-19 15:57:13 +0800899 "/gpu@38000000",
900 "/soc@/gpu@38000000"
Peng Fan6036dba2020-07-09 14:06:49 +0800901 };
902
903 return disable_fdt_nodes(blob, nodes_path_8mn, ARRAY_SIZE(nodes_path_8mn));
904}
905
906int disable_npu_nodes(void *blob)
907{
908 static const char * const nodes_path_8mp[] = {
909 "/vipsi@38500000"
910 };
911
912 return disable_fdt_nodes(blob, nodes_path_8mp, ARRAY_SIZE(nodes_path_8mp));
913}
914
915int disable_isp_nodes(void *blob)
916{
917 static const char * const nodes_path_8mp[] = {
918 "/soc@0/bus@32c00000/camera/isp@32e10000",
919 "/soc@0/bus@32c00000/camera/isp@32e20000"
920 };
921
922 return disable_fdt_nodes(blob, nodes_path_8mp, ARRAY_SIZE(nodes_path_8mp));
923}
924
925int disable_dsp_nodes(void *blob)
926{
927 static const char * const nodes_path_8mp[] = {
928 "/dsp@3b6e8000"
929 };
930
931 return disable_fdt_nodes(blob, nodes_path_8mp, ARRAY_SIZE(nodes_path_8mp));
932}
933
Ye Li4e83c052021-03-19 15:57:12 +0800934static void disable_thermal_cpu_nodes(void *blob, u32 disabled_cores)
935{
936 static const char * const thermal_path[] = {
937 "/thermal-zones/cpu-thermal/cooling-maps/map0"
938 };
939
940 int nodeoff, cnt, i, ret, j;
941 u32 cooling_dev[12];
942
943 for (i = 0; i < ARRAY_SIZE(thermal_path); i++) {
944 nodeoff = fdt_path_offset(blob, thermal_path[i]);
945 if (nodeoff < 0)
946 continue; /* Not found, skip it */
947
948 cnt = fdtdec_get_int_array_count(blob, nodeoff, "cooling-device", cooling_dev, 12);
949 if (cnt < 0)
950 continue;
951
952 if (cnt != 12)
953 printf("Warning: %s, cooling-device count %d\n", thermal_path[i], cnt);
954
955 for (j = 0; j < cnt; j++)
956 cooling_dev[j] = cpu_to_fdt32(cooling_dev[j]);
957
958 ret = fdt_setprop(blob, nodeoff, "cooling-device", &cooling_dev,
959 sizeof(u32) * (12 - disabled_cores * 3));
960 if (ret < 0) {
961 printf("Warning: %s, cooling-device setprop failed %d\n",
962 thermal_path[i], ret);
963 continue;
964 }
965
966 printf("Update node %s, cooling-device prop\n", thermal_path[i]);
967 }
968}
969
970static void disable_pmu_cpu_nodes(void *blob, u32 disabled_cores)
971{
972 static const char * const pmu_path[] = {
973 "/pmu"
974 };
975
976 int nodeoff, cnt, i, ret, j;
977 u32 irq_affinity[4];
978
979 for (i = 0; i < ARRAY_SIZE(pmu_path); i++) {
980 nodeoff = fdt_path_offset(blob, pmu_path[i]);
981 if (nodeoff < 0)
982 continue; /* Not found, skip it */
983
984 cnt = fdtdec_get_int_array_count(blob, nodeoff, "interrupt-affinity",
985 irq_affinity, 4);
986 if (cnt < 0)
987 continue;
988
989 if (cnt != 4)
990 printf("Warning: %s, interrupt-affinity count %d\n", pmu_path[i], cnt);
991
992 for (j = 0; j < cnt; j++)
993 irq_affinity[j] = cpu_to_fdt32(irq_affinity[j]);
994
995 ret = fdt_setprop(blob, nodeoff, "interrupt-affinity", &irq_affinity,
996 sizeof(u32) * (4 - disabled_cores));
997 if (ret < 0) {
998 printf("Warning: %s, interrupt-affinity setprop failed %d\n",
999 pmu_path[i], ret);
1000 continue;
1001 }
1002
1003 printf("Update node %s, interrupt-affinity prop\n", pmu_path[i]);
1004 }
1005}
1006
Peng Fan6036dba2020-07-09 14:06:49 +08001007static int disable_cpu_nodes(void *blob, u32 disabled_cores)
1008{
1009 static const char * const nodes_path[] = {
1010 "/cpus/cpu@1",
1011 "/cpus/cpu@2",
1012 "/cpus/cpu@3",
1013 };
1014 u32 i = 0;
1015 int rc;
1016 int nodeoff;
1017
1018 if (disabled_cores > 3)
1019 return -EINVAL;
1020
1021 i = 3 - disabled_cores;
1022
1023 for (; i < 3; i++) {
1024 nodeoff = fdt_path_offset(blob, nodes_path[i]);
1025 if (nodeoff < 0)
1026 continue; /* Not found, skip it */
1027
1028 debug("Found %s node\n", nodes_path[i]);
1029
1030 rc = fdt_del_node(blob, nodeoff);
1031 if (rc < 0) {
1032 printf("Unable to delete node %s, err=%s\n",
1033 nodes_path[i], fdt_strerror(rc));
1034 } else {
1035 printf("Delete node %s\n", nodes_path[i]);
1036 }
1037 }
1038
Ye Li4e83c052021-03-19 15:57:12 +08001039 disable_thermal_cpu_nodes(blob, disabled_cores);
1040 disable_pmu_cpu_nodes(blob, disabled_cores);
1041
Peng Fan6036dba2020-07-09 14:06:49 +08001042 return 0;
1043}
1044
Masahiro Yamadab75d8dc2020-06-26 15:13:33 +09001045int ft_system_setup(void *blob, struct bd_info *bd)
Peng Fan6036dba2020-07-09 14:06:49 +08001046{
1047#ifdef CONFIG_IMX8MQ
1048 int i = 0;
1049 int rc;
1050 int nodeoff;
1051
1052 if (get_boot_device() == USB_BOOT) {
1053 disable_dcss_nodes(blob);
1054
1055 bool new_path = check_fdt_new_path(blob);
1056 int v = new_path ? 1 : 0;
1057 static const char * const usb_dwc3_path[] = {
1058 "/usb@38100000/dwc3",
1059 "/soc@0/usb@38100000"
1060 };
1061
1062 nodeoff = fdt_path_offset(blob, usb_dwc3_path[v]);
1063 if (nodeoff >= 0) {
1064 const char *speed = "high-speed";
1065
1066 printf("Found %s node\n", usb_dwc3_path[v]);
1067
1068usb_modify_speed:
1069
1070 rc = fdt_setprop(blob, nodeoff, "maximum-speed", speed, strlen(speed) + 1);
1071 if (rc) {
1072 if (rc == -FDT_ERR_NOSPACE) {
1073 rc = fdt_increase_size(blob, 512);
1074 if (!rc)
1075 goto usb_modify_speed;
1076 }
1077 printf("Unable to set property %s:%s, err=%s\n",
1078 usb_dwc3_path[v], "maximum-speed", fdt_strerror(rc));
1079 } else {
1080 printf("Modify %s:%s = %s\n",
1081 usb_dwc3_path[v], "maximum-speed", speed);
1082 }
1083 } else {
1084 printf("Can't found %s node\n", usb_dwc3_path[v]);
1085 }
1086 }
Peng Fanfcdbde72018-01-10 13:20:37 +08001087
1088 /* Disable the CPU idle for A0 chip since the HW does not support it */
1089 if (is_soc_rev(CHIP_REV_1_0)) {
1090 static const char * const nodes_path[] = {
1091 "/cpus/cpu@0",
1092 "/cpus/cpu@1",
1093 "/cpus/cpu@2",
1094 "/cpus/cpu@3",
1095 };
1096
1097 for (i = 0; i < ARRAY_SIZE(nodes_path); i++) {
1098 nodeoff = fdt_path_offset(blob, nodes_path[i]);
1099 if (nodeoff < 0)
1100 continue; /* Not found, skip it */
1101
Marek Vasutdabaabd2020-04-24 21:37:33 +02001102 debug("Found %s node\n", nodes_path[i]);
Peng Fanfcdbde72018-01-10 13:20:37 +08001103
1104 rc = fdt_delprop(blob, nodeoff, "cpu-idle-states");
Marek Vasutdabaabd2020-04-24 21:37:33 +02001105 if (rc == -FDT_ERR_NOTFOUND)
1106 continue;
Peng Fanfcdbde72018-01-10 13:20:37 +08001107 if (rc) {
1108 printf("Unable to update property %s:%s, err=%s\n",
1109 nodes_path[i], "status", fdt_strerror(rc));
1110 return rc;
1111 }
1112
Marek Vasutdabaabd2020-04-24 21:37:33 +02001113 debug("Remove %s:%s\n", nodes_path[i],
Peng Fanfcdbde72018-01-10 13:20:37 +08001114 "cpu-idle-states");
1115 }
1116 }
1117
Peng Fan6036dba2020-07-09 14:06:49 +08001118 if (is_imx8mql()) {
1119 disable_vpu_nodes(blob);
1120 if (check_dcss_fused()) {
1121 printf("DCSS is fused\n");
1122 disable_dcss_nodes(blob);
1123 check_mipi_dsi_nodes(blob);
1124 }
1125 }
1126
1127 if (is_imx8md())
1128 disable_cpu_nodes(blob, 2);
1129
1130#elif defined(CONFIG_IMX8MM)
1131 if (is_imx8mml() || is_imx8mmdl() || is_imx8mmsl())
1132 disable_vpu_nodes(blob);
1133
1134 if (is_imx8mmd() || is_imx8mmdl())
1135 disable_cpu_nodes(blob, 2);
1136 else if (is_imx8mms() || is_imx8mmsl())
1137 disable_cpu_nodes(blob, 3);
1138
1139#elif defined(CONFIG_IMX8MN)
1140 if (is_imx8mnl() || is_imx8mndl() || is_imx8mnsl())
1141 disable_gpu_nodes(blob);
Ye Li98bcdf12021-03-19 15:57:09 +08001142#ifdef CONFIG_IMX8MN_LOW_DRIVE_MODE
1143 else {
1144 int ldm_gpu = low_drive_gpu_freq(blob);
1145
1146 if (ldm_gpu < 0)
1147 printf("Update GPU node assigned-clock-rates failed\n");
1148 else
1149 printf("Update GPU node assigned-clock-rates ok\n");
1150 }
1151#endif
Peng Fan6036dba2020-07-09 14:06:49 +08001152
Ye Lic4f78cb2021-03-19 15:57:11 +08001153 if (is_imx8mnd() || is_imx8mndl() || is_imx8mnud())
Peng Fan6036dba2020-07-09 14:06:49 +08001154 disable_cpu_nodes(blob, 2);
Ye Lic4f78cb2021-03-19 15:57:11 +08001155 else if (is_imx8mns() || is_imx8mnsl() || is_imx8mnus())
Peng Fan6036dba2020-07-09 14:06:49 +08001156 disable_cpu_nodes(blob, 3);
1157
1158#elif defined(CONFIG_IMX8MP)
Peng Fan00982222020-09-16 15:17:22 +08001159 if (is_imx8mpl())
Peng Fan6036dba2020-07-09 14:06:49 +08001160 disable_vpu_nodes(blob);
1161
Peng Fan00982222020-09-16 15:17:22 +08001162 if (is_imx8mpl() || is_imx8mp6())
Peng Fan6036dba2020-07-09 14:06:49 +08001163 disable_npu_nodes(blob);
1164
Peng Fan00982222020-09-16 15:17:22 +08001165 if (is_imx8mpl())
Peng Fan6036dba2020-07-09 14:06:49 +08001166 disable_isp_nodes(blob);
1167
Peng Fan00982222020-09-16 15:17:22 +08001168 if (is_imx8mpl() || is_imx8mp6())
Peng Fan6036dba2020-07-09 14:06:49 +08001169 disable_dsp_nodes(blob);
1170
1171 if (is_imx8mpd())
1172 disable_cpu_nodes(blob, 2);
1173#endif
1174
Peng Fanfcdbde72018-01-10 13:20:37 +08001175 return 0;
1176}
1177#endif
1178
Marek Vasutefa1a622020-04-29 15:04:21 +02001179#if !CONFIG_IS_ENABLED(SYSRESET)
Harald Seiler35b65dd2020-12-15 16:47:52 +01001180void reset_cpu(void)
Peng Fanfcdbde72018-01-10 13:20:37 +08001181{
Claudius Heinec5635a02020-04-29 15:04:23 +02001182 struct watchdog_regs *wdog = (struct watchdog_regs *)WDOG1_BASE_ADDR;
Peng Fanfcdbde72018-01-10 13:20:37 +08001183
Ye Li72479742019-12-09 00:47:18 -08001184 /* Clear WDA to trigger WDOG_B immediately */
1185 writew((SET_WCR_WT(1) | WCR_WDT | WCR_WDE | WCR_SRS), &wdog->wcr);
Peng Fand2041722019-08-27 06:25:41 +00001186
Ye Li72479742019-12-09 00:47:18 -08001187 while (1) {
1188 /*
Harald Seiler568af922020-04-29 15:04:22 +02001189 * spin for .5 seconds before reset
Ye Li72479742019-12-09 00:47:18 -08001190 */
1191 }
Peng Fanfcdbde72018-01-10 13:20:37 +08001192}
Peng Fand2041722019-08-27 06:25:41 +00001193#endif
Peng Fana07bcec2020-04-22 10:51:13 +08001194
1195#if defined(CONFIG_ARCH_MISC_INIT)
1196static void acquire_buildinfo(void)
1197{
1198 u64 atf_commit = 0;
Peng Fana2f143e2020-05-11 15:14:04 +08001199 struct arm_smccc_res res;
Peng Fana07bcec2020-04-22 10:51:13 +08001200
1201 /* Get ARM Trusted Firmware commit id */
Peng Fana2f143e2020-05-11 15:14:04 +08001202 arm_smccc_smc(IMX_SIP_BUILDINFO, IMX_SIP_BUILDINFO_GET_COMMITHASH,
Fabio Estevam68a699e2020-07-17 16:36:54 -03001203 0, 0, 0, 0, 0, 0, &res);
Peng Fana2f143e2020-05-11 15:14:04 +08001204 atf_commit = res.a0;
Peng Fana07bcec2020-04-22 10:51:13 +08001205 if (atf_commit == 0xffffffff) {
1206 debug("ATF does not support build info\n");
1207 atf_commit = 0x30; /* Display 0, 0 ascii is 0x30 */
1208 }
1209
1210 printf("\n BuildInfo:\n - ATF %s\n\n", (char *)&atf_commit);
1211}
1212
1213int arch_misc_init(void)
1214{
1215 acquire_buildinfo();
1216
1217 return 0;
1218}
1219#endif
Ye Li70487ff2020-05-03 22:19:52 +08001220
1221void imx_tmu_arch_init(void *reg_base)
1222{
Ye Li94c693d2020-05-03 22:19:53 +08001223 if (is_imx8mm() || is_imx8mn()) {
Ye Li70487ff2020-05-03 22:19:52 +08001224 /* Load TCALIV and TASR from fuses */
1225 struct ocotp_regs *ocotp =
1226 (struct ocotp_regs *)OCOTP_BASE_ADDR;
1227 struct fuse_bank *bank = &ocotp->bank[3];
1228 struct fuse_bank3_regs *fuse =
1229 (struct fuse_bank3_regs *)bank->fuse_regs;
1230
1231 u32 tca_rt, tca_hr, tca_en;
1232 u32 buf_vref, buf_slope;
1233
1234 tca_rt = fuse->ana0 & 0xFF;
1235 tca_hr = (fuse->ana0 & 0xFF00) >> 8;
1236 tca_en = (fuse->ana0 & 0x2000000) >> 25;
1237
1238 buf_vref = (fuse->ana0 & 0x1F00000) >> 20;
1239 buf_slope = (fuse->ana0 & 0xF0000) >> 16;
1240
1241 writel(buf_vref | (buf_slope << 16), (ulong)reg_base + 0x28);
1242 writel((tca_en << 31) | (tca_hr << 16) | tca_rt,
1243 (ulong)reg_base + 0x30);
1244 }
Ye Liebb9aab2020-05-03 22:19:54 +08001245#ifdef CONFIG_IMX8MP
1246 /* Load TCALIV0/1/m40 and TRIM from fuses */
1247 struct ocotp_regs *ocotp = (struct ocotp_regs *)OCOTP_BASE_ADDR;
1248 struct fuse_bank *bank = &ocotp->bank[38];
1249 struct fuse_bank38_regs *fuse =
1250 (struct fuse_bank38_regs *)bank->fuse_regs;
1251 struct fuse_bank *bank2 = &ocotp->bank[39];
1252 struct fuse_bank39_regs *fuse2 =
1253 (struct fuse_bank39_regs *)bank2->fuse_regs;
1254 u32 buf_vref, buf_slope, bjt_cur, vlsb, bgr;
1255 u32 reg;
1256 u32 tca40[2], tca25[2], tca105[2];
1257
1258 /* For blank sample */
1259 if (!fuse->ana_trim2 && !fuse->ana_trim3 &&
1260 !fuse->ana_trim4 && !fuse2->ana_trim5) {
1261 /* Use a default 25C binary codes */
1262 tca25[0] = 1596;
Ye Li3462b552020-05-03 22:19:55 +08001263 tca25[1] = 1596;
Ye Liebb9aab2020-05-03 22:19:54 +08001264 writel(tca25[0], (ulong)reg_base + 0x30);
Ye Li3462b552020-05-03 22:19:55 +08001265 writel(tca25[1], (ulong)reg_base + 0x34);
Ye Liebb9aab2020-05-03 22:19:54 +08001266 return;
1267 }
1268
1269 buf_vref = (fuse->ana_trim2 & 0xc0) >> 6;
1270 buf_slope = (fuse->ana_trim2 & 0xF00) >> 8;
1271 bjt_cur = (fuse->ana_trim2 & 0xF000) >> 12;
1272 bgr = (fuse->ana_trim2 & 0xF0000) >> 16;
1273 vlsb = (fuse->ana_trim2 & 0xF00000) >> 20;
1274 writel(buf_vref | (buf_slope << 16), (ulong)reg_base + 0x28);
1275
1276 reg = (bgr << 28) | (bjt_cur << 20) | (vlsb << 12) | (1 << 7);
1277 writel(reg, (ulong)reg_base + 0x3c);
1278
1279 tca40[0] = (fuse->ana_trim3 & 0xFFF0000) >> 16;
1280 tca25[0] = (fuse->ana_trim3 & 0xF0000000) >> 28;
1281 tca25[0] |= ((fuse->ana_trim4 & 0xFF) << 4);
1282 tca105[0] = (fuse->ana_trim4 & 0xFFF00) >> 8;
1283 tca40[1] = (fuse->ana_trim4 & 0xFFF00000) >> 20;
1284 tca25[1] = fuse2->ana_trim5 & 0xFFF;
1285 tca105[1] = (fuse2->ana_trim5 & 0xFFF000) >> 12;
1286
1287 /* use 25c for 1p calibration */
1288 writel(tca25[0] | (tca105[0] << 16), (ulong)reg_base + 0x30);
1289 writel(tca25[1] | (tca105[1] << 16), (ulong)reg_base + 0x34);
1290 writel(tca40[0] | (tca40[1] << 16), (ulong)reg_base + 0x38);
1291#endif
Ye Li70487ff2020-05-03 22:19:52 +08001292}
Peng Fan2f3c9202020-07-09 13:39:26 +08001293
1294#if defined(CONFIG_SPL_BUILD)
1295#if defined(CONFIG_IMX8MQ) || defined(CONFIG_IMX8MM) || defined(CONFIG_IMX8MN)
1296bool serror_need_skip = true;
1297
1298void do_error(struct pt_regs *pt_regs, unsigned int esr)
1299{
1300 /*
1301 * If stack is still in ROM reserved OCRAM not switch to SPL,
1302 * it is the ROM SError
1303 */
1304 ulong sp;
1305
1306 asm volatile("mov %0, sp" : "=r"(sp) : );
1307
1308 if (serror_need_skip && sp < 0x910000 && sp >= 0x900000) {
1309 /* Check for ERR050342, imx8mq HDCP enabled parts */
1310 if (is_imx8mq() && !(readl(OCOTP_BASE_ADDR + 0x450) & 0x08000000)) {
1311 serror_need_skip = false;
1312 return; /* Do nothing skip the SError in ROM */
1313 }
1314
1315 /* Check for ERR050350, field return mode for imx8mq, mm and mn */
1316 if (readl(OCOTP_BASE_ADDR + 0x630) & 0x1) {
1317 serror_need_skip = false;
1318 return; /* Do nothing skip the SError in ROM */
1319 }
1320 }
1321
1322 efi_restore_gd();
1323 printf("\"Error\" handler, esr 0x%08x\n", esr);
1324 show_regs(pt_regs);
1325 panic("Resetting CPU ...\n");
1326}
1327#endif
1328#endif
Ye Li2707faf2019-07-15 01:16:46 -07001329
1330#if defined(CONFIG_IMX8MN) || defined(CONFIG_IMX8MP)
1331enum env_location env_get_location(enum env_operation op, int prio)
1332{
1333 enum boot_device dev = get_boot_device();
Ye Li2707faf2019-07-15 01:16:46 -07001334
1335 if (prio)
Ricardo Salveti89ca5a72021-10-20 16:16:26 -03001336 return ENVL_UNKNOWN;
Ye Li2707faf2019-07-15 01:16:46 -07001337
1338 switch (dev) {
Ye Li2707faf2019-07-15 01:16:46 -07001339 case QSPI_BOOT:
Ricardo Salveti89ca5a72021-10-20 16:16:26 -03001340 if (IS_ENABLED(CONFIG_ENV_IS_IN_SPI_FLASH))
1341 return ENVL_SPI_FLASH;
1342 return ENVL_NOWHERE;
Ye Li2707faf2019-07-15 01:16:46 -07001343 case NAND_BOOT:
Ricardo Salveti89ca5a72021-10-20 16:16:26 -03001344 if (IS_ENABLED(CONFIG_ENV_IS_IN_NAND))
1345 return ENVL_NAND;
1346 return ENVL_NOWHERE;
Ye Li2707faf2019-07-15 01:16:46 -07001347 case SD1_BOOT:
1348 case SD2_BOOT:
1349 case SD3_BOOT:
1350 case MMC1_BOOT:
1351 case MMC2_BOOT:
1352 case MMC3_BOOT:
Ricardo Salveti89ca5a72021-10-20 16:16:26 -03001353 if (IS_ENABLED(CONFIG_ENV_IS_IN_MMC))
1354 return ENVL_MMC;
1355 else if (IS_ENABLED(CONFIG_ENV_IS_IN_EXT4))
1356 return ENVL_EXT4;
1357 else if (IS_ENABLED(CONFIG_ENV_IS_IN_FAT))
1358 return ENVL_FAT;
1359 return ENVL_NOWHERE;
Ye Li2707faf2019-07-15 01:16:46 -07001360 default:
Ricardo Salveti89ca5a72021-10-20 16:16:26 -03001361 return ENVL_NOWHERE;
Ye Li2707faf2019-07-15 01:16:46 -07001362 }
Ye Li2707faf2019-07-15 01:16:46 -07001363}
1364
Ye Li2707faf2019-07-15 01:16:46 -07001365#endif