blob: f5983320c7cb8b518bfc70dd4680b749e45c2386 [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
Peng Faneae4de22018-01-10 13:20:37 +0800588bool is_usb_boot(void)
589{
590 return get_boot_device() == USB_BOOT;
591}
592
593#ifdef CONFIG_OF_SYSTEM_SETUP
Peng Fan435dc122020-07-09 14:06:49 +0800594bool check_fdt_new_path(void *blob)
595{
596 const char *soc_path = "/soc@0";
597 int nodeoff;
598
599 nodeoff = fdt_path_offset(blob, soc_path);
600 if (nodeoff < 0)
601 return false;
602
603 return true;
604}
605
606static int disable_fdt_nodes(void *blob, const char *const nodes_path[], int size_array)
607{
608 int i = 0;
609 int rc;
610 int nodeoff;
611 const char *status = "disabled";
612
613 for (i = 0; i < size_array; i++) {
614 nodeoff = fdt_path_offset(blob, nodes_path[i]);
615 if (nodeoff < 0)
616 continue; /* Not found, skip it */
617
618 printf("Found %s node\n", nodes_path[i]);
619
620add_status:
621 rc = fdt_setprop(blob, nodeoff, "status", status, strlen(status) + 1);
622 if (rc) {
623 if (rc == -FDT_ERR_NOSPACE) {
624 rc = fdt_increase_size(blob, 512);
625 if (!rc)
626 goto add_status;
627 }
628 printf("Unable to update property %s:%s, err=%s\n",
629 nodes_path[i], "status", fdt_strerror(rc));
630 } else {
631 printf("Modify %s:%s disabled\n",
632 nodes_path[i], "status");
633 }
634 }
635
636 return 0;
637}
638
639#ifdef CONFIG_IMX8MQ
640bool check_dcss_fused(void)
641{
642 struct ocotp_regs *ocotp = (struct ocotp_regs *)OCOTP_BASE_ADDR;
643 struct fuse_bank *bank = &ocotp->bank[1];
644 struct fuse_bank1_regs *fuse =
645 (struct fuse_bank1_regs *)bank->fuse_regs;
646 u32 value = readl(&fuse->tester4);
647
648 if (value & 0x4000000)
649 return true;
650
651 return false;
652}
653
654static int disable_mipi_dsi_nodes(void *blob)
655{
656 static const char * const nodes_path[] = {
657 "/mipi_dsi@30A00000",
658 "/mipi_dsi_bridge@30A00000",
659 "/dsi_phy@30A00300",
660 "/soc@0/bus@30800000/mipi_dsi@30a00000",
Peng Fan7d4195c2021-03-19 15:57:13 +0800661 "/soc@0/bus@30800000/dphy@30a00300",
662 "/soc@0/bus@30800000/mipi-dsi@30a00000",
Peng Fan435dc122020-07-09 14:06:49 +0800663 };
664
665 return disable_fdt_nodes(blob, nodes_path, ARRAY_SIZE(nodes_path));
666}
667
668static int disable_dcss_nodes(void *blob)
669{
670 static const char * const nodes_path[] = {
671 "/dcss@0x32e00000",
672 "/dcss@32e00000",
673 "/hdmi@32c00000",
674 "/hdmi_cec@32c33800",
675 "/hdmi_drm@32c00000",
676 "/display-subsystem",
677 "/sound-hdmi",
678 "/sound-hdmi-arc",
679 "/soc@0/bus@32c00000/display-controller@32e00000",
680 "/soc@0/bus@32c00000/hdmi@32c00000",
681 };
682
683 return disable_fdt_nodes(blob, nodes_path, ARRAY_SIZE(nodes_path));
684}
685
686static int check_mipi_dsi_nodes(void *blob)
687{
688 static const char * const lcdif_path[] = {
689 "/lcdif@30320000",
Peng Fan7d4195c2021-03-19 15:57:13 +0800690 "/soc@0/bus@30000000/lcdif@30320000",
691 "/soc@0/bus@30000000/lcd-controller@30320000"
Peng Fan435dc122020-07-09 14:06:49 +0800692 };
693 static const char * const mipi_dsi_path[] = {
694 "/mipi_dsi@30A00000",
695 "/soc@0/bus@30800000/mipi_dsi@30a00000"
696 };
697 static const char * const lcdif_ep_path[] = {
698 "/lcdif@30320000/port@0/mipi-dsi-endpoint",
Peng Fan7d4195c2021-03-19 15:57:13 +0800699 "/soc@0/bus@30000000/lcdif@30320000/port@0/endpoint",
700 "/soc@0/bus@30000000/lcd-controller@30320000/port@0/endpoint"
Peng Fan435dc122020-07-09 14:06:49 +0800701 };
702 static const char * const mipi_dsi_ep_path[] = {
703 "/mipi_dsi@30A00000/port@1/endpoint",
Peng Fan7d4195c2021-03-19 15:57:13 +0800704 "/soc@0/bus@30800000/mipi_dsi@30a00000/ports/port@0/endpoint",
705 "/soc@0/bus@30800000/mipi-dsi@30a00000/ports/port@0/endpoint@0"
Peng Fan435dc122020-07-09 14:06:49 +0800706 };
707
708 int lookup_node;
709 int nodeoff;
710 bool new_path = check_fdt_new_path(blob);
711 int i = new_path ? 1 : 0;
712
713 nodeoff = fdt_path_offset(blob, lcdif_path[i]);
714 if (nodeoff < 0 || !fdtdec_get_is_enabled(blob, nodeoff)) {
715 /*
716 * If can't find lcdif node or lcdif node is disabled,
717 * then disable all mipi dsi, since they only can input
718 * from DCSS
719 */
720 return disable_mipi_dsi_nodes(blob);
721 }
722
723 nodeoff = fdt_path_offset(blob, mipi_dsi_path[i]);
724 if (nodeoff < 0 || !fdtdec_get_is_enabled(blob, nodeoff))
725 return 0;
726
727 nodeoff = fdt_path_offset(blob, lcdif_ep_path[i]);
728 if (nodeoff < 0) {
729 /*
730 * If can't find lcdif endpoint, then disable all mipi dsi,
731 * since they only can input from DCSS
732 */
733 return disable_mipi_dsi_nodes(blob);
734 }
735
736 lookup_node = fdtdec_lookup_phandle(blob, nodeoff, "remote-endpoint");
737 nodeoff = fdt_path_offset(blob, mipi_dsi_ep_path[i]);
738
739 if (nodeoff > 0 && nodeoff == lookup_node)
740 return 0;
741
742 return disable_mipi_dsi_nodes(blob);
743}
744#endif
745
746int disable_vpu_nodes(void *blob)
747{
748 static const char * const nodes_path_8mq[] = {
749 "/vpu@38300000",
750 "/soc@0/vpu@38300000"
751 };
752
753 static const char * const nodes_path_8mm[] = {
754 "/vpu_g1@38300000",
755 "/vpu_g2@38310000",
756 "/vpu_h1@38320000"
757 };
758
759 static const char * const nodes_path_8mp[] = {
760 "/vpu_g1@38300000",
761 "/vpu_g2@38310000",
762 "/vpu_vc8000e@38320000"
763 };
764
765 if (is_imx8mq())
766 return disable_fdt_nodes(blob, nodes_path_8mq, ARRAY_SIZE(nodes_path_8mq));
767 else if (is_imx8mm())
768 return disable_fdt_nodes(blob, nodes_path_8mm, ARRAY_SIZE(nodes_path_8mm));
769 else if (is_imx8mp())
770 return disable_fdt_nodes(blob, nodes_path_8mp, ARRAY_SIZE(nodes_path_8mp));
771 else
772 return -EPERM;
773}
774
Ye Liee337ce2021-03-19 15:57:09 +0800775#ifdef CONFIG_IMX8MN_LOW_DRIVE_MODE
776static int low_drive_gpu_freq(void *blob)
777{
778 static const char *nodes_path_8mn[] = {
779 "/gpu@38000000",
780 "/soc@0/gpu@38000000"
781 };
782
783 int nodeoff, cnt, i;
784 u32 assignedclks[7];
785
786 nodeoff = fdt_path_offset(blob, nodes_path_8mn[0]);
787 if (nodeoff < 0)
788 return nodeoff;
789
790 cnt = fdtdec_get_int_array_count(blob, nodeoff, "assigned-clock-rates", assignedclks, 7);
791 if (cnt < 0)
792 return cnt;
793
794 if (cnt != 7)
795 printf("Warning: %s, assigned-clock-rates count %d\n", nodes_path_8mn[0], cnt);
796
797 assignedclks[cnt - 1] = 200000000;
798 assignedclks[cnt - 2] = 200000000;
799
800 for (i = 0; i < cnt; i++) {
801 debug("<%u>, ", assignedclks[i]);
802 assignedclks[i] = cpu_to_fdt32(assignedclks[i]);
803 }
804 debug("\n");
805
806 return fdt_setprop(blob, nodeoff, "assigned-clock-rates", &assignedclks, sizeof(assignedclks));
807}
808#endif
809
Peng Fan435dc122020-07-09 14:06:49 +0800810int disable_gpu_nodes(void *blob)
811{
812 static const char * const nodes_path_8mn[] = {
Peng Fan7d4195c2021-03-19 15:57:13 +0800813 "/gpu@38000000",
814 "/soc@/gpu@38000000"
Peng Fan435dc122020-07-09 14:06:49 +0800815 };
816
817 return disable_fdt_nodes(blob, nodes_path_8mn, ARRAY_SIZE(nodes_path_8mn));
818}
819
820int disable_npu_nodes(void *blob)
821{
822 static const char * const nodes_path_8mp[] = {
823 "/vipsi@38500000"
824 };
825
826 return disable_fdt_nodes(blob, nodes_path_8mp, ARRAY_SIZE(nodes_path_8mp));
827}
828
829int disable_isp_nodes(void *blob)
830{
831 static const char * const nodes_path_8mp[] = {
832 "/soc@0/bus@32c00000/camera/isp@32e10000",
833 "/soc@0/bus@32c00000/camera/isp@32e20000"
834 };
835
836 return disable_fdt_nodes(blob, nodes_path_8mp, ARRAY_SIZE(nodes_path_8mp));
837}
838
839int disable_dsp_nodes(void *blob)
840{
841 static const char * const nodes_path_8mp[] = {
842 "/dsp@3b6e8000"
843 };
844
845 return disable_fdt_nodes(blob, nodes_path_8mp, ARRAY_SIZE(nodes_path_8mp));
846}
847
Ye Li26517af2021-03-19 15:57:12 +0800848static void disable_thermal_cpu_nodes(void *blob, u32 disabled_cores)
849{
850 static const char * const thermal_path[] = {
851 "/thermal-zones/cpu-thermal/cooling-maps/map0"
852 };
853
854 int nodeoff, cnt, i, ret, j;
855 u32 cooling_dev[12];
856
857 for (i = 0; i < ARRAY_SIZE(thermal_path); i++) {
858 nodeoff = fdt_path_offset(blob, thermal_path[i]);
859 if (nodeoff < 0)
860 continue; /* Not found, skip it */
861
862 cnt = fdtdec_get_int_array_count(blob, nodeoff, "cooling-device", cooling_dev, 12);
863 if (cnt < 0)
864 continue;
865
866 if (cnt != 12)
867 printf("Warning: %s, cooling-device count %d\n", thermal_path[i], cnt);
868
869 for (j = 0; j < cnt; j++)
870 cooling_dev[j] = cpu_to_fdt32(cooling_dev[j]);
871
872 ret = fdt_setprop(blob, nodeoff, "cooling-device", &cooling_dev,
873 sizeof(u32) * (12 - disabled_cores * 3));
874 if (ret < 0) {
875 printf("Warning: %s, cooling-device setprop failed %d\n",
876 thermal_path[i], ret);
877 continue;
878 }
879
880 printf("Update node %s, cooling-device prop\n", thermal_path[i]);
881 }
882}
883
884static void disable_pmu_cpu_nodes(void *blob, u32 disabled_cores)
885{
886 static const char * const pmu_path[] = {
887 "/pmu"
888 };
889
890 int nodeoff, cnt, i, ret, j;
891 u32 irq_affinity[4];
892
893 for (i = 0; i < ARRAY_SIZE(pmu_path); i++) {
894 nodeoff = fdt_path_offset(blob, pmu_path[i]);
895 if (nodeoff < 0)
896 continue; /* Not found, skip it */
897
898 cnt = fdtdec_get_int_array_count(blob, nodeoff, "interrupt-affinity",
899 irq_affinity, 4);
900 if (cnt < 0)
901 continue;
902
903 if (cnt != 4)
904 printf("Warning: %s, interrupt-affinity count %d\n", pmu_path[i], cnt);
905
906 for (j = 0; j < cnt; j++)
907 irq_affinity[j] = cpu_to_fdt32(irq_affinity[j]);
908
909 ret = fdt_setprop(blob, nodeoff, "interrupt-affinity", &irq_affinity,
910 sizeof(u32) * (4 - disabled_cores));
911 if (ret < 0) {
912 printf("Warning: %s, interrupt-affinity setprop failed %d\n",
913 pmu_path[i], ret);
914 continue;
915 }
916
917 printf("Update node %s, interrupt-affinity prop\n", pmu_path[i]);
918 }
919}
920
Peng Fan435dc122020-07-09 14:06:49 +0800921static int disable_cpu_nodes(void *blob, u32 disabled_cores)
922{
923 static const char * const nodes_path[] = {
924 "/cpus/cpu@1",
925 "/cpus/cpu@2",
926 "/cpus/cpu@3",
927 };
928 u32 i = 0;
929 int rc;
930 int nodeoff;
931
932 if (disabled_cores > 3)
933 return -EINVAL;
934
935 i = 3 - disabled_cores;
936
937 for (; i < 3; i++) {
938 nodeoff = fdt_path_offset(blob, nodes_path[i]);
939 if (nodeoff < 0)
940 continue; /* Not found, skip it */
941
942 debug("Found %s node\n", nodes_path[i]);
943
944 rc = fdt_del_node(blob, nodeoff);
945 if (rc < 0) {
946 printf("Unable to delete node %s, err=%s\n",
947 nodes_path[i], fdt_strerror(rc));
948 } else {
949 printf("Delete node %s\n", nodes_path[i]);
950 }
951 }
952
Ye Li26517af2021-03-19 15:57:12 +0800953 disable_thermal_cpu_nodes(blob, disabled_cores);
954 disable_pmu_cpu_nodes(blob, disabled_cores);
955
Peng Fan435dc122020-07-09 14:06:49 +0800956 return 0;
957}
958
Masahiro Yamadaf7ed78b2020-06-26 15:13:33 +0900959int ft_system_setup(void *blob, struct bd_info *bd)
Peng Faneae4de22018-01-10 13:20:37 +0800960{
Peng Fan435dc122020-07-09 14:06:49 +0800961#ifdef CONFIG_IMX8MQ
Peng Faneae4de22018-01-10 13:20:37 +0800962 int i = 0;
963 int rc;
964 int nodeoff;
965
Peng Fan435dc122020-07-09 14:06:49 +0800966 if (get_boot_device() == USB_BOOT) {
967 disable_dcss_nodes(blob);
968
969 bool new_path = check_fdt_new_path(blob);
970 int v = new_path ? 1 : 0;
971 static const char * const usb_dwc3_path[] = {
972 "/usb@38100000/dwc3",
973 "/soc@0/usb@38100000"
974 };
975
976 nodeoff = fdt_path_offset(blob, usb_dwc3_path[v]);
977 if (nodeoff >= 0) {
978 const char *speed = "high-speed";
979
980 printf("Found %s node\n", usb_dwc3_path[v]);
981
982usb_modify_speed:
983
984 rc = fdt_setprop(blob, nodeoff, "maximum-speed", speed, strlen(speed) + 1);
985 if (rc) {
986 if (rc == -FDT_ERR_NOSPACE) {
987 rc = fdt_increase_size(blob, 512);
988 if (!rc)
989 goto usb_modify_speed;
990 }
991 printf("Unable to set property %s:%s, err=%s\n",
992 usb_dwc3_path[v], "maximum-speed", fdt_strerror(rc));
993 } else {
994 printf("Modify %s:%s = %s\n",
995 usb_dwc3_path[v], "maximum-speed", speed);
996 }
997 } else {
998 printf("Can't found %s node\n", usb_dwc3_path[v]);
999 }
1000 }
1001
Peng Faneae4de22018-01-10 13:20:37 +08001002 /* Disable the CPU idle for A0 chip since the HW does not support it */
1003 if (is_soc_rev(CHIP_REV_1_0)) {
1004 static const char * const nodes_path[] = {
1005 "/cpus/cpu@0",
1006 "/cpus/cpu@1",
1007 "/cpus/cpu@2",
1008 "/cpus/cpu@3",
1009 };
1010
1011 for (i = 0; i < ARRAY_SIZE(nodes_path); i++) {
1012 nodeoff = fdt_path_offset(blob, nodes_path[i]);
1013 if (nodeoff < 0)
1014 continue; /* Not found, skip it */
1015
Marek Vasute2e7a772020-04-24 21:37:33 +02001016 debug("Found %s node\n", nodes_path[i]);
Peng Faneae4de22018-01-10 13:20:37 +08001017
1018 rc = fdt_delprop(blob, nodeoff, "cpu-idle-states");
Marek Vasute2e7a772020-04-24 21:37:33 +02001019 if (rc == -FDT_ERR_NOTFOUND)
1020 continue;
Peng Faneae4de22018-01-10 13:20:37 +08001021 if (rc) {
1022 printf("Unable to update property %s:%s, err=%s\n",
1023 nodes_path[i], "status", fdt_strerror(rc));
1024 return rc;
1025 }
1026
Marek Vasute2e7a772020-04-24 21:37:33 +02001027 debug("Remove %s:%s\n", nodes_path[i],
Peng Faneae4de22018-01-10 13:20:37 +08001028 "cpu-idle-states");
1029 }
1030 }
1031
Peng Fan435dc122020-07-09 14:06:49 +08001032 if (is_imx8mql()) {
1033 disable_vpu_nodes(blob);
1034 if (check_dcss_fused()) {
1035 printf("DCSS is fused\n");
1036 disable_dcss_nodes(blob);
1037 check_mipi_dsi_nodes(blob);
1038 }
1039 }
1040
1041 if (is_imx8md())
1042 disable_cpu_nodes(blob, 2);
1043
1044#elif defined(CONFIG_IMX8MM)
1045 if (is_imx8mml() || is_imx8mmdl() || is_imx8mmsl())
1046 disable_vpu_nodes(blob);
1047
1048 if (is_imx8mmd() || is_imx8mmdl())
1049 disable_cpu_nodes(blob, 2);
1050 else if (is_imx8mms() || is_imx8mmsl())
1051 disable_cpu_nodes(blob, 3);
1052
1053#elif defined(CONFIG_IMX8MN)
1054 if (is_imx8mnl() || is_imx8mndl() || is_imx8mnsl())
1055 disable_gpu_nodes(blob);
Ye Liee337ce2021-03-19 15:57:09 +08001056#ifdef CONFIG_IMX8MN_LOW_DRIVE_MODE
1057 else {
1058 int ldm_gpu = low_drive_gpu_freq(blob);
1059
1060 if (ldm_gpu < 0)
1061 printf("Update GPU node assigned-clock-rates failed\n");
1062 else
1063 printf("Update GPU node assigned-clock-rates ok\n");
1064 }
1065#endif
Peng Fan435dc122020-07-09 14:06:49 +08001066
Ye Li715180e2021-03-19 15:57:11 +08001067 if (is_imx8mnd() || is_imx8mndl() || is_imx8mnud())
Peng Fan435dc122020-07-09 14:06:49 +08001068 disable_cpu_nodes(blob, 2);
Ye Li715180e2021-03-19 15:57:11 +08001069 else if (is_imx8mns() || is_imx8mnsl() || is_imx8mnus())
Peng Fan435dc122020-07-09 14:06:49 +08001070 disable_cpu_nodes(blob, 3);
1071
1072#elif defined(CONFIG_IMX8MP)
Peng Fan8a472a22020-09-16 15:17:22 +08001073 if (is_imx8mpl())
Peng Fan435dc122020-07-09 14:06:49 +08001074 disable_vpu_nodes(blob);
1075
Peng Fan8a472a22020-09-16 15:17:22 +08001076 if (is_imx8mpl() || is_imx8mp6())
Peng Fan435dc122020-07-09 14:06:49 +08001077 disable_npu_nodes(blob);
1078
Peng Fan8a472a22020-09-16 15:17:22 +08001079 if (is_imx8mpl())
Peng Fan435dc122020-07-09 14:06:49 +08001080 disable_isp_nodes(blob);
1081
Peng Fan8a472a22020-09-16 15:17:22 +08001082 if (is_imx8mpl() || is_imx8mp6())
Peng Fan435dc122020-07-09 14:06:49 +08001083 disable_dsp_nodes(blob);
1084
1085 if (is_imx8mpd())
1086 disable_cpu_nodes(blob, 2);
1087#endif
1088
Peng Faneae4de22018-01-10 13:20:37 +08001089 return 0;
1090}
1091#endif
1092
Marek Vasut64dc4de2020-04-29 15:04:21 +02001093#if !CONFIG_IS_ENABLED(SYSRESET)
Harald Seiler6f14d5f2020-12-15 16:47:52 +01001094void reset_cpu(void)
Peng Faneae4de22018-01-10 13:20:37 +08001095{
Claudius Heinee73f3942020-04-29 15:04:23 +02001096 struct watchdog_regs *wdog = (struct watchdog_regs *)WDOG1_BASE_ADDR;
Peng Faneae4de22018-01-10 13:20:37 +08001097
Ye Li54a915a2019-12-09 00:47:18 -08001098 /* Clear WDA to trigger WDOG_B immediately */
1099 writew((SET_WCR_WT(1) | WCR_WDT | WCR_WDE | WCR_SRS), &wdog->wcr);
Peng Fan24290d92019-08-27 06:25:41 +00001100
Ye Li54a915a2019-12-09 00:47:18 -08001101 while (1) {
1102 /*
Harald Seilerec0c4472020-04-29 15:04:22 +02001103 * spin for .5 seconds before reset
Ye Li54a915a2019-12-09 00:47:18 -08001104 */
1105 }
Peng Faneae4de22018-01-10 13:20:37 +08001106}
Peng Fan24290d92019-08-27 06:25:41 +00001107#endif
Peng Fan5760d8d2020-04-22 10:51:13 +08001108
1109#if defined(CONFIG_ARCH_MISC_INIT)
1110static void acquire_buildinfo(void)
1111{
1112 u64 atf_commit = 0;
Peng Fan45d843a2020-05-11 15:14:04 +08001113 struct arm_smccc_res res;
Peng Fan5760d8d2020-04-22 10:51:13 +08001114
1115 /* Get ARM Trusted Firmware commit id */
Peng Fan45d843a2020-05-11 15:14:04 +08001116 arm_smccc_smc(IMX_SIP_BUILDINFO, IMX_SIP_BUILDINFO_GET_COMMITHASH,
Fabio Estevam31e410f2020-07-17 16:36:54 -03001117 0, 0, 0, 0, 0, 0, &res);
Peng Fan45d843a2020-05-11 15:14:04 +08001118 atf_commit = res.a0;
Peng Fan5760d8d2020-04-22 10:51:13 +08001119 if (atf_commit == 0xffffffff) {
1120 debug("ATF does not support build info\n");
1121 atf_commit = 0x30; /* Display 0, 0 ascii is 0x30 */
1122 }
1123
1124 printf("\n BuildInfo:\n - ATF %s\n\n", (char *)&atf_commit);
1125}
1126
1127int arch_misc_init(void)
1128{
1129 acquire_buildinfo();
1130
1131 return 0;
1132}
1133#endif
Ye Li325cd012020-05-03 22:19:52 +08001134
1135void imx_tmu_arch_init(void *reg_base)
1136{
Ye Lia00f2f02020-05-03 22:19:53 +08001137 if (is_imx8mm() || is_imx8mn()) {
Ye Li325cd012020-05-03 22:19:52 +08001138 /* Load TCALIV and TASR from fuses */
1139 struct ocotp_regs *ocotp =
1140 (struct ocotp_regs *)OCOTP_BASE_ADDR;
1141 struct fuse_bank *bank = &ocotp->bank[3];
1142 struct fuse_bank3_regs *fuse =
1143 (struct fuse_bank3_regs *)bank->fuse_regs;
1144
1145 u32 tca_rt, tca_hr, tca_en;
1146 u32 buf_vref, buf_slope;
1147
1148 tca_rt = fuse->ana0 & 0xFF;
1149 tca_hr = (fuse->ana0 & 0xFF00) >> 8;
1150 tca_en = (fuse->ana0 & 0x2000000) >> 25;
1151
1152 buf_vref = (fuse->ana0 & 0x1F00000) >> 20;
1153 buf_slope = (fuse->ana0 & 0xF0000) >> 16;
1154
1155 writel(buf_vref | (buf_slope << 16), (ulong)reg_base + 0x28);
1156 writel((tca_en << 31) | (tca_hr << 16) | tca_rt,
1157 (ulong)reg_base + 0x30);
1158 }
Ye Li41a20252020-05-03 22:19:54 +08001159#ifdef CONFIG_IMX8MP
1160 /* Load TCALIV0/1/m40 and TRIM from fuses */
1161 struct ocotp_regs *ocotp = (struct ocotp_regs *)OCOTP_BASE_ADDR;
1162 struct fuse_bank *bank = &ocotp->bank[38];
1163 struct fuse_bank38_regs *fuse =
1164 (struct fuse_bank38_regs *)bank->fuse_regs;
1165 struct fuse_bank *bank2 = &ocotp->bank[39];
1166 struct fuse_bank39_regs *fuse2 =
1167 (struct fuse_bank39_regs *)bank2->fuse_regs;
1168 u32 buf_vref, buf_slope, bjt_cur, vlsb, bgr;
1169 u32 reg;
1170 u32 tca40[2], tca25[2], tca105[2];
1171
1172 /* For blank sample */
1173 if (!fuse->ana_trim2 && !fuse->ana_trim3 &&
1174 !fuse->ana_trim4 && !fuse2->ana_trim5) {
1175 /* Use a default 25C binary codes */
1176 tca25[0] = 1596;
Ye Lid756ca02020-05-03 22:19:55 +08001177 tca25[1] = 1596;
Ye Li41a20252020-05-03 22:19:54 +08001178 writel(tca25[0], (ulong)reg_base + 0x30);
Ye Lid756ca02020-05-03 22:19:55 +08001179 writel(tca25[1], (ulong)reg_base + 0x34);
Ye Li41a20252020-05-03 22:19:54 +08001180 return;
1181 }
1182
1183 buf_vref = (fuse->ana_trim2 & 0xc0) >> 6;
1184 buf_slope = (fuse->ana_trim2 & 0xF00) >> 8;
1185 bjt_cur = (fuse->ana_trim2 & 0xF000) >> 12;
1186 bgr = (fuse->ana_trim2 & 0xF0000) >> 16;
1187 vlsb = (fuse->ana_trim2 & 0xF00000) >> 20;
1188 writel(buf_vref | (buf_slope << 16), (ulong)reg_base + 0x28);
1189
1190 reg = (bgr << 28) | (bjt_cur << 20) | (vlsb << 12) | (1 << 7);
1191 writel(reg, (ulong)reg_base + 0x3c);
1192
1193 tca40[0] = (fuse->ana_trim3 & 0xFFF0000) >> 16;
1194 tca25[0] = (fuse->ana_trim3 & 0xF0000000) >> 28;
1195 tca25[0] |= ((fuse->ana_trim4 & 0xFF) << 4);
1196 tca105[0] = (fuse->ana_trim4 & 0xFFF00) >> 8;
1197 tca40[1] = (fuse->ana_trim4 & 0xFFF00000) >> 20;
1198 tca25[1] = fuse2->ana_trim5 & 0xFFF;
1199 tca105[1] = (fuse2->ana_trim5 & 0xFFF000) >> 12;
1200
1201 /* use 25c for 1p calibration */
1202 writel(tca25[0] | (tca105[0] << 16), (ulong)reg_base + 0x30);
1203 writel(tca25[1] | (tca105[1] << 16), (ulong)reg_base + 0x34);
1204 writel(tca40[0] | (tca40[1] << 16), (ulong)reg_base + 0x38);
1205#endif
Ye Li325cd012020-05-03 22:19:52 +08001206}
Peng Fana35215d2020-07-09 13:39:26 +08001207
1208#if defined(CONFIG_SPL_BUILD)
1209#if defined(CONFIG_IMX8MQ) || defined(CONFIG_IMX8MM) || defined(CONFIG_IMX8MN)
1210bool serror_need_skip = true;
1211
1212void do_error(struct pt_regs *pt_regs, unsigned int esr)
1213{
1214 /*
1215 * If stack is still in ROM reserved OCRAM not switch to SPL,
1216 * it is the ROM SError
1217 */
1218 ulong sp;
1219
1220 asm volatile("mov %0, sp" : "=r"(sp) : );
1221
1222 if (serror_need_skip && sp < 0x910000 && sp >= 0x900000) {
1223 /* Check for ERR050342, imx8mq HDCP enabled parts */
1224 if (is_imx8mq() && !(readl(OCOTP_BASE_ADDR + 0x450) & 0x08000000)) {
1225 serror_need_skip = false;
1226 return; /* Do nothing skip the SError in ROM */
1227 }
1228
1229 /* Check for ERR050350, field return mode for imx8mq, mm and mn */
1230 if (readl(OCOTP_BASE_ADDR + 0x630) & 0x1) {
1231 serror_need_skip = false;
1232 return; /* Do nothing skip the SError in ROM */
1233 }
1234 }
1235
1236 efi_restore_gd();
1237 printf("\"Error\" handler, esr 0x%08x\n", esr);
1238 show_regs(pt_regs);
1239 panic("Resetting CPU ...\n");
1240}
1241#endif
1242#endif
Ye Li0513f362019-07-15 01:16:46 -07001243
1244#if defined(CONFIG_IMX8MN) || defined(CONFIG_IMX8MP)
1245enum env_location env_get_location(enum env_operation op, int prio)
1246{
1247 enum boot_device dev = get_boot_device();
1248 enum env_location env_loc = ENVL_UNKNOWN;
1249
1250 if (prio)
1251 return env_loc;
1252
1253 switch (dev) {
1254#ifdef CONFIG_ENV_IS_IN_SPI_FLASH
1255 case QSPI_BOOT:
1256 env_loc = ENVL_SPI_FLASH;
1257 break;
1258#endif
1259#ifdef CONFIG_ENV_IS_IN_NAND
1260 case NAND_BOOT:
1261 env_loc = ENVL_NAND;
1262 break;
1263#endif
1264#ifdef CONFIG_ENV_IS_IN_MMC
1265 case SD1_BOOT:
1266 case SD2_BOOT:
1267 case SD3_BOOT:
1268 case MMC1_BOOT:
1269 case MMC2_BOOT:
1270 case MMC3_BOOT:
1271 env_loc = ENVL_MMC;
1272 break;
1273#endif
1274 default:
1275#if defined(CONFIG_ENV_IS_NOWHERE)
1276 env_loc = ENVL_NOWHERE;
1277#endif
1278 break;
1279 }
1280
1281 return env_loc;
1282}
1283
1284#ifndef ENV_IS_EMBEDDED
1285long long env_get_offset(long long defautl_offset)
1286{
1287 enum boot_device dev = get_boot_device();
1288
1289 switch (dev) {
1290 case NAND_BOOT:
1291 return (60 << 20); /* 60MB offset for NAND */
1292 default:
1293 break;
1294 }
1295
1296 return defautl_offset;
1297}
1298#endif
1299#endif