blob: 5456c10fb177041eba1a0da41c5939fcdf7b0ae6 [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 }, {
108 /* TCM */
109 .virt = 0x7C0000UL,
110 .phys = 0x7C0000UL,
111 .size = 0x80000UL,
112 .attrs = PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
113 PTE_BLOCK_NON_SHARE |
114 PTE_BLOCK_PXN | PTE_BLOCK_UXN
115 }, {
Peng Fanfcdbde72018-01-10 13:20:37 +0800116 /* OCRAM */
117 .virt = 0x900000UL,
118 .phys = 0x900000UL,
119 .size = 0x200000UL,
120 .attrs = PTE_BLOCK_MEMTYPE(MT_NORMAL) |
121 PTE_BLOCK_OUTER_SHARE
122 }, {
123 /* AIPS */
124 .virt = 0xB00000UL,
125 .phys = 0xB00000UL,
126 .size = 0x3f500000UL,
127 .attrs = PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
128 PTE_BLOCK_NON_SHARE |
129 PTE_BLOCK_PXN | PTE_BLOCK_UXN
130 }, {
131 /* DRAM1 */
132 .virt = 0x40000000UL,
133 .phys = 0x40000000UL,
Peng Fan59efa6b2019-08-27 06:25:27 +0000134 .size = PHYS_SDRAM_SIZE,
Peng Fanfcdbde72018-01-10 13:20:37 +0800135 .attrs = PTE_BLOCK_MEMTYPE(MT_NORMAL) |
136 PTE_BLOCK_OUTER_SHARE
Peng Fan59efa6b2019-08-27 06:25:27 +0000137#ifdef PHYS_SDRAM_2_SIZE
Peng Fanfcdbde72018-01-10 13:20:37 +0800138 }, {
139 /* DRAM2 */
140 .virt = 0x100000000UL,
141 .phys = 0x100000000UL,
Peng Fan59efa6b2019-08-27 06:25:27 +0000142 .size = PHYS_SDRAM_2_SIZE,
Peng Fanfcdbde72018-01-10 13:20:37 +0800143 .attrs = PTE_BLOCK_MEMTYPE(MT_NORMAL) |
144 PTE_BLOCK_OUTER_SHARE
Peng Fan59efa6b2019-08-27 06:25:27 +0000145#endif
Peng Fanfcdbde72018-01-10 13:20:37 +0800146 }, {
Peng Fan3c417282020-07-09 15:26:06 +0800147 /* empty entrie to split table entry 5 if needed when TEEs are used */
148 0,
149 }, {
Peng Fanfcdbde72018-01-10 13:20:37 +0800150 /* List terminator */
151 0,
152 }
153};
154
155struct mm_region *mem_map = imx8m_mem_map;
156
Peng Fan59efa6b2019-08-27 06:25:27 +0000157void enable_caches(void)
158{
Peng Fan3c417282020-07-09 15:26:06 +0800159 /* If OPTEE runs, remove OPTEE memory from MMU table to avoid speculative prefetch */
160 if (rom_pointer[1]) {
161 /*
162 * TEE are loaded, So the ddr bank structures
163 * have been modified update mmu table accordingly
164 */
165 int i = 0;
166 /*
167 * please make sure that entry initial value matches
168 * imx8m_mem_map for DRAM1
169 */
170 int entry = 5;
171 u64 attrs = imx8m_mem_map[entry].attrs;
172
173 while (i < CONFIG_NR_DRAM_BANKS && entry < 8) {
174 if (gd->bd->bi_dram[i].start == 0)
175 break;
176 imx8m_mem_map[entry].phys = gd->bd->bi_dram[i].start;
177 imx8m_mem_map[entry].virt = gd->bd->bi_dram[i].start;
178 imx8m_mem_map[entry].size = gd->bd->bi_dram[i].size;
179 imx8m_mem_map[entry].attrs = attrs;
180 debug("Added memory mapping (%d): %llx %llx\n", entry,
181 imx8m_mem_map[entry].phys, imx8m_mem_map[entry].size);
182 i++; entry++;
183 }
184 }
Peng Fan59efa6b2019-08-27 06:25:27 +0000185
186 icache_enable();
187 dcache_enable();
188}
189
Peng Fan3c417282020-07-09 15:26:06 +0800190__weak int board_phys_sdram_size(phys_size_t *size)
191{
192 if (!size)
193 return -EINVAL;
194
195 *size = PHYS_SDRAM_SIZE;
196 return 0;
197}
198
199int dram_init(void)
200{
201 phys_size_t sdram_size;
202 int ret;
203
204 ret = board_phys_sdram_size(&sdram_size);
205 if (ret)
206 return ret;
207
208 /* rom_pointer[1] contains the size of TEE occupies */
209 if (rom_pointer[1])
210 gd->ram_size = sdram_size - rom_pointer[1];
211 else
212 gd->ram_size = sdram_size;
213
Tim Harveybc479b22020-09-25 08:08:35 -0700214 /* also update the SDRAM size in the mem_map used externally */
215 imx8m_mem_map[5].size = sdram_size;
216
Peng Fan3c417282020-07-09 15:26:06 +0800217#ifdef PHYS_SDRAM_2_SIZE
218 gd->ram_size += PHYS_SDRAM_2_SIZE;
219#endif
220
221 return 0;
222}
223
224int dram_init_banksize(void)
225{
226 int bank = 0;
227 int ret;
228 phys_size_t sdram_size;
229
230 ret = board_phys_sdram_size(&sdram_size);
231 if (ret)
232 return ret;
233
234 gd->bd->bi_dram[bank].start = PHYS_SDRAM;
235 if (rom_pointer[1]) {
236 phys_addr_t optee_start = (phys_addr_t)rom_pointer[0];
237 phys_size_t optee_size = (size_t)rom_pointer[1];
238
239 gd->bd->bi_dram[bank].size = optee_start - gd->bd->bi_dram[bank].start;
240 if ((optee_start + optee_size) < (PHYS_SDRAM + sdram_size)) {
241 if (++bank >= CONFIG_NR_DRAM_BANKS) {
242 puts("CONFIG_NR_DRAM_BANKS is not enough\n");
243 return -1;
244 }
245
246 gd->bd->bi_dram[bank].start = optee_start + optee_size;
247 gd->bd->bi_dram[bank].size = PHYS_SDRAM +
248 sdram_size - gd->bd->bi_dram[bank].start;
249 }
250 } else {
251 gd->bd->bi_dram[bank].size = sdram_size;
252 }
253
254#ifdef PHYS_SDRAM_2_SIZE
255 if (++bank >= CONFIG_NR_DRAM_BANKS) {
256 puts("CONFIG_NR_DRAM_BANKS is not enough for SDRAM_2\n");
257 return -1;
258 }
259 gd->bd->bi_dram[bank].start = PHYS_SDRAM_2;
260 gd->bd->bi_dram[bank].size = PHYS_SDRAM_2_SIZE;
261#endif
262
263 return 0;
264}
265
266phys_size_t get_effective_memsize(void)
267{
268 /* return the first bank as effective memory */
269 if (rom_pointer[1])
270 return ((phys_addr_t)rom_pointer[0] - PHYS_SDRAM);
271
272#ifdef PHYS_SDRAM_2_SIZE
273 return gd->ram_size - PHYS_SDRAM_2_SIZE;
274#else
275 return gd->ram_size;
276#endif
277}
278
Peng Fan78db9a52019-08-27 06:25:17 +0000279static u32 get_cpu_variant_type(u32 type)
280{
281 struct ocotp_regs *ocotp = (struct ocotp_regs *)OCOTP_BASE_ADDR;
282 struct fuse_bank *bank = &ocotp->bank[1];
283 struct fuse_bank1_regs *fuse =
284 (struct fuse_bank1_regs *)bank->fuse_regs;
285
286 u32 value = readl(&fuse->tester4);
287
Peng Fancb1a1de2020-02-05 17:34:54 +0800288 if (type == MXC_CPU_IMX8MQ) {
289 if ((value & 0x3) == 0x2)
290 return MXC_CPU_IMX8MD;
291 else if (value & 0x200000)
292 return MXC_CPU_IMX8MQL;
293
294 } else if (type == MXC_CPU_IMX8MM) {
Peng Fan78db9a52019-08-27 06:25:17 +0000295 switch (value & 0x3) {
296 case 2:
297 if (value & 0x1c0000)
298 return MXC_CPU_IMX8MMDL;
299 else
300 return MXC_CPU_IMX8MMD;
301 case 3:
302 if (value & 0x1c0000)
303 return MXC_CPU_IMX8MMSL;
304 else
305 return MXC_CPU_IMX8MMS;
306 default:
307 if (value & 0x1c0000)
308 return MXC_CPU_IMX8MML;
309 break;
310 }
Peng Fanc9154032020-02-05 17:39:27 +0800311 } else if (type == MXC_CPU_IMX8MN) {
312 switch (value & 0x3) {
313 case 2:
314 if (value & 0x1000000)
315 return MXC_CPU_IMX8MNDL;
316 else
317 return MXC_CPU_IMX8MND;
318 case 3:
319 if (value & 0x1000000)
320 return MXC_CPU_IMX8MNSL;
321 else
322 return MXC_CPU_IMX8MNS;
323 default:
324 if (value & 0x1000000)
325 return MXC_CPU_IMX8MNL;
326 break;
327 }
Ye Lid1eee7e2020-04-20 20:12:54 -0700328 } else if (type == MXC_CPU_IMX8MP) {
329 u32 value0 = readl(&fuse->tester3);
330 u32 flag = 0;
331
332 if ((value0 & 0xc0000) == 0x80000)
333 return MXC_CPU_IMX8MPD;
334
335 /* vpu disabled */
336 if ((value0 & 0x43000000) == 0x43000000)
337 flag = 1;
338
339 /* npu disabled*/
340 if ((value & 0x8) == 0x8)
341 flag |= (1 << 1);
342
343 /* isp disabled */
344 if ((value & 0x3) == 0x3)
345 flag |= (1 << 2);
346
347 switch (flag) {
348 case 7:
349 return MXC_CPU_IMX8MPL;
Ye Lid1eee7e2020-04-20 20:12:54 -0700350 case 2:
351 return MXC_CPU_IMX8MP6;
Ye Lid1eee7e2020-04-20 20:12:54 -0700352 default:
353 break;
354 }
355
Peng Fan78db9a52019-08-27 06:25:17 +0000356 }
357
358 return type;
359}
360
Peng Fanfcdbde72018-01-10 13:20:37 +0800361u32 get_cpu_rev(void)
362{
363 struct anamix_pll *ana_pll = (struct anamix_pll *)ANATOP_BASE_ADDR;
364 u32 reg = readl(&ana_pll->digprog);
365 u32 type = (reg >> 16) & 0xff;
Peng Fan78db9a52019-08-27 06:25:17 +0000366 u32 major_low = (reg >> 8) & 0xff;
Peng Fanfcdbde72018-01-10 13:20:37 +0800367 u32 rom_version;
368
369 reg &= 0xff;
370
Peng Fan625b03d2019-12-27 10:14:02 +0800371 /* iMX8MP */
372 if (major_low == 0x43) {
Ye Lid1eee7e2020-04-20 20:12:54 -0700373 type = get_cpu_variant_type(MXC_CPU_IMX8MP);
Peng Fan625b03d2019-12-27 10:14:02 +0800374 } else if (major_low == 0x42) {
375 /* iMX8MN */
Peng Fanc9154032020-02-05 17:39:27 +0800376 type = get_cpu_variant_type(MXC_CPU_IMX8MN);
Peng Fan24341312019-06-27 17:23:49 +0800377 } else if (major_low == 0x41) {
Peng Fan78db9a52019-08-27 06:25:17 +0000378 type = get_cpu_variant_type(MXC_CPU_IMX8MM);
379 } else {
380 if (reg == CHIP_REV_1_0) {
381 /*
Peng Fan9e094452019-10-16 10:24:17 +0000382 * For B0 chip, the DIGPROG is not updated,
383 * it is still TO1.0. we have to check ROM
384 * version or OCOTP_READ_FUSE_DATA.
385 * 0xff0055aa is magic number for B1.
Peng Fan78db9a52019-08-27 06:25:17 +0000386 */
Peng Fan9e094452019-10-16 10:24:17 +0000387 if (readl((void __iomem *)(OCOTP_BASE_ADDR + 0x40)) == 0xff0055aa) {
388 reg = CHIP_REV_2_1;
389 } else {
390 rom_version =
391 readl((void __iomem *)ROM_VERSION_A0);
392 if (rom_version != CHIP_REV_1_0) {
393 rom_version = readl((void __iomem *)ROM_VERSION_B0);
Patrick Wildt6a4b07e2019-11-19 09:42:06 +0100394 rom_version &= 0xff;
Peng Fan9e094452019-10-16 10:24:17 +0000395 if (rom_version == CHIP_REV_2_0)
396 reg = CHIP_REV_2_0;
397 }
Peng Fan78db9a52019-08-27 06:25:17 +0000398 }
Peng Fanfcdbde72018-01-10 13:20:37 +0800399 }
Peng Fancb1a1de2020-02-05 17:34:54 +0800400
401 type = get_cpu_variant_type(type);
Peng Fanfcdbde72018-01-10 13:20:37 +0800402 }
403
404 return (type << 12) | reg;
405}
406
407static void imx_set_wdog_powerdown(bool enable)
408{
409 struct wdog_regs *wdog1 = (struct wdog_regs *)WDOG1_BASE_ADDR;
410 struct wdog_regs *wdog2 = (struct wdog_regs *)WDOG2_BASE_ADDR;
411 struct wdog_regs *wdog3 = (struct wdog_regs *)WDOG3_BASE_ADDR;
412
413 /* Write to the PDE (Power Down Enable) bit */
414 writew(enable, &wdog1->wmcr);
415 writew(enable, &wdog2->wmcr);
416 writew(enable, &wdog3->wmcr);
417}
418
Peng Fane663c702019-08-27 06:25:58 +0000419int arch_cpu_init_dm(void)
420{
421 struct udevice *dev;
422 int ret;
423
Peng Fancd7c8062019-10-16 03:01:51 +0000424 if (CONFIG_IS_ENABLED(CLK)) {
425 ret = uclass_get_device_by_name(UCLASS_CLK,
426 "clock-controller@30380000",
427 &dev);
428 if (ret < 0) {
429 printf("Failed to find clock node. Check device tree\n");
430 return ret;
431 }
Peng Fane663c702019-08-27 06:25:58 +0000432 }
433
434 return 0;
435}
436
Peng Fanfcdbde72018-01-10 13:20:37 +0800437int arch_cpu_init(void)
438{
Peng Fan702339b2019-04-17 09:41:16 +0000439 struct ocotp_regs *ocotp = (struct ocotp_regs *)OCOTP_BASE_ADDR;
Peng Fanfcdbde72018-01-10 13:20:37 +0800440 /*
Peng Fan0528ba02019-08-27 06:25:37 +0000441 * ROM might disable clock for SCTR,
442 * enable the clock before timer_init.
443 */
444 if (IS_ENABLED(CONFIG_SPL_BUILD))
445 clock_enable(CCGR_SCTR, 1);
446 /*
Peng Fanfcdbde72018-01-10 13:20:37 +0800447 * Init timer at very early state, because sscg pll setting
448 * will use it
449 */
450 timer_init();
451
452 if (IS_ENABLED(CONFIG_SPL_BUILD)) {
453 clock_init();
454 imx_set_wdog_powerdown(false);
Peng Fan7a42bf02020-07-09 13:52:41 +0800455
456 if (is_imx8md() || is_imx8mmd() || is_imx8mmdl() || is_imx8mms() ||
457 is_imx8mmsl() || is_imx8mnd() || is_imx8mndl() || is_imx8mns() ||
458 is_imx8mnsl() || is_imx8mpd()) {
459 /* Power down cpu core 1, 2 and 3 for iMX8M Dual core or Single core */
460 struct pgc_reg *pgc_core1 = (struct pgc_reg *)(GPC_BASE_ADDR + 0x840);
461 struct pgc_reg *pgc_core2 = (struct pgc_reg *)(GPC_BASE_ADDR + 0x880);
462 struct pgc_reg *pgc_core3 = (struct pgc_reg *)(GPC_BASE_ADDR + 0x8C0);
463 struct gpc_reg *gpc = (struct gpc_reg *)GPC_BASE_ADDR;
464
465 writel(0x1, &pgc_core2->pgcr);
466 writel(0x1, &pgc_core3->pgcr);
467 if (is_imx8mms() || is_imx8mmsl() || is_imx8mns() || is_imx8mnsl()) {
468 writel(0x1, &pgc_core1->pgcr);
469 writel(0xE, &gpc->cpu_pgc_dn_trg);
470 } else {
471 writel(0xC, &gpc->cpu_pgc_dn_trg);
472 }
473 }
Peng Fanfcdbde72018-01-10 13:20:37 +0800474 }
475
Peng Fan702339b2019-04-17 09:41:16 +0000476 if (is_imx8mq()) {
477 clock_enable(CCGR_OCOTP, 1);
478 if (readl(&ocotp->ctrl) & 0x200)
479 writel(0x200, &ocotp->ctrl_clr);
480 }
481
Peng Fanfcdbde72018-01-10 13:20:37 +0800482 return 0;
483}
484
Peng Fanb1821372019-09-16 03:09:36 +0000485#if defined(CONFIG_IMX8MN) || defined(CONFIG_IMX8MP)
486struct rom_api *g_rom_api = (struct rom_api *)0x980;
487
488enum boot_device get_boot_device(void)
489{
490 volatile gd_t *pgd = gd;
491 int ret;
492 u32 boot;
493 u16 boot_type;
494 u8 boot_instance;
495 enum boot_device boot_dev = SD1_BOOT;
496
497 ret = g_rom_api->query_boot_infor(QUERY_BT_DEV, &boot,
498 ((uintptr_t)&boot) ^ QUERY_BT_DEV);
499 gd = pgd;
500
501 if (ret != ROM_API_OKAY) {
502 puts("ROMAPI: failure at query_boot_info\n");
503 return -1;
504 }
505
506 boot_type = boot >> 16;
507 boot_instance = (boot >> 8) & 0xff;
508
509 switch (boot_type) {
510 case BT_DEV_TYPE_SD:
511 boot_dev = boot_instance + SD1_BOOT;
512 break;
513 case BT_DEV_TYPE_MMC:
514 boot_dev = boot_instance + MMC1_BOOT;
515 break;
516 case BT_DEV_TYPE_NAND:
517 boot_dev = NAND_BOOT;
518 break;
519 case BT_DEV_TYPE_FLEXSPINOR:
520 boot_dev = QSPI_BOOT;
521 break;
522 case BT_DEV_TYPE_USB:
523 boot_dev = USB_BOOT;
524 break;
525 default:
526 break;
527 }
528
529 return boot_dev;
530}
531#endif
532
Peng Fanfcdbde72018-01-10 13:20:37 +0800533bool is_usb_boot(void)
534{
535 return get_boot_device() == USB_BOOT;
536}
537
538#ifdef CONFIG_OF_SYSTEM_SETUP
Peng Fan6036dba2020-07-09 14:06:49 +0800539bool check_fdt_new_path(void *blob)
540{
541 const char *soc_path = "/soc@0";
542 int nodeoff;
543
544 nodeoff = fdt_path_offset(blob, soc_path);
545 if (nodeoff < 0)
546 return false;
547
548 return true;
549}
550
551static int disable_fdt_nodes(void *blob, const char *const nodes_path[], int size_array)
Peng Fanfcdbde72018-01-10 13:20:37 +0800552{
553 int i = 0;
554 int rc;
555 int nodeoff;
Peng Fan6036dba2020-07-09 14:06:49 +0800556 const char *status = "disabled";
557
558 for (i = 0; i < size_array; i++) {
559 nodeoff = fdt_path_offset(blob, nodes_path[i]);
560 if (nodeoff < 0)
561 continue; /* Not found, skip it */
562
563 printf("Found %s node\n", nodes_path[i]);
564
565add_status:
566 rc = fdt_setprop(blob, nodeoff, "status", status, strlen(status) + 1);
567 if (rc) {
568 if (rc == -FDT_ERR_NOSPACE) {
569 rc = fdt_increase_size(blob, 512);
570 if (!rc)
571 goto add_status;
572 }
573 printf("Unable to update property %s:%s, err=%s\n",
574 nodes_path[i], "status", fdt_strerror(rc));
575 } else {
576 printf("Modify %s:%s disabled\n",
577 nodes_path[i], "status");
578 }
579 }
580
581 return 0;
582}
583
584#ifdef CONFIG_IMX8MQ
585bool check_dcss_fused(void)
586{
587 struct ocotp_regs *ocotp = (struct ocotp_regs *)OCOTP_BASE_ADDR;
588 struct fuse_bank *bank = &ocotp->bank[1];
589 struct fuse_bank1_regs *fuse =
590 (struct fuse_bank1_regs *)bank->fuse_regs;
591 u32 value = readl(&fuse->tester4);
592
593 if (value & 0x4000000)
594 return true;
595
596 return false;
597}
598
599static int disable_mipi_dsi_nodes(void *blob)
600{
601 static const char * const nodes_path[] = {
602 "/mipi_dsi@30A00000",
603 "/mipi_dsi_bridge@30A00000",
604 "/dsi_phy@30A00300",
605 "/soc@0/bus@30800000/mipi_dsi@30a00000",
606 "/soc@0/bus@30800000/dphy@30a00300"
607 };
608
609 return disable_fdt_nodes(blob, nodes_path, ARRAY_SIZE(nodes_path));
610}
611
612static int disable_dcss_nodes(void *blob)
613{
614 static const char * const nodes_path[] = {
615 "/dcss@0x32e00000",
616 "/dcss@32e00000",
617 "/hdmi@32c00000",
618 "/hdmi_cec@32c33800",
619 "/hdmi_drm@32c00000",
620 "/display-subsystem",
621 "/sound-hdmi",
622 "/sound-hdmi-arc",
623 "/soc@0/bus@32c00000/display-controller@32e00000",
624 "/soc@0/bus@32c00000/hdmi@32c00000",
625 };
626
627 return disable_fdt_nodes(blob, nodes_path, ARRAY_SIZE(nodes_path));
628}
629
630static int check_mipi_dsi_nodes(void *blob)
631{
632 static const char * const lcdif_path[] = {
633 "/lcdif@30320000",
634 "/soc@0/bus@30000000/lcdif@30320000"
635 };
636 static const char * const mipi_dsi_path[] = {
637 "/mipi_dsi@30A00000",
638 "/soc@0/bus@30800000/mipi_dsi@30a00000"
639 };
640 static const char * const lcdif_ep_path[] = {
641 "/lcdif@30320000/port@0/mipi-dsi-endpoint",
642 "/soc@0/bus@30000000/lcdif@30320000/port@0/endpoint"
643 };
644 static const char * const mipi_dsi_ep_path[] = {
645 "/mipi_dsi@30A00000/port@1/endpoint",
646 "/soc@0/bus@30800000/mipi_dsi@30a00000/ports/port@0/endpoint"
647 };
648
649 int lookup_node;
650 int nodeoff;
651 bool new_path = check_fdt_new_path(blob);
652 int i = new_path ? 1 : 0;
653
654 nodeoff = fdt_path_offset(blob, lcdif_path[i]);
655 if (nodeoff < 0 || !fdtdec_get_is_enabled(blob, nodeoff)) {
656 /*
657 * If can't find lcdif node or lcdif node is disabled,
658 * then disable all mipi dsi, since they only can input
659 * from DCSS
660 */
661 return disable_mipi_dsi_nodes(blob);
662 }
663
664 nodeoff = fdt_path_offset(blob, mipi_dsi_path[i]);
665 if (nodeoff < 0 || !fdtdec_get_is_enabled(blob, nodeoff))
666 return 0;
667
668 nodeoff = fdt_path_offset(blob, lcdif_ep_path[i]);
669 if (nodeoff < 0) {
670 /*
671 * If can't find lcdif endpoint, then disable all mipi dsi,
672 * since they only can input from DCSS
673 */
674 return disable_mipi_dsi_nodes(blob);
675 }
676
677 lookup_node = fdtdec_lookup_phandle(blob, nodeoff, "remote-endpoint");
678 nodeoff = fdt_path_offset(blob, mipi_dsi_ep_path[i]);
679
680 if (nodeoff > 0 && nodeoff == lookup_node)
681 return 0;
682
683 return disable_mipi_dsi_nodes(blob);
684}
685#endif
686
687int disable_vpu_nodes(void *blob)
688{
689 static const char * const nodes_path_8mq[] = {
690 "/vpu@38300000",
691 "/soc@0/vpu@38300000"
692 };
693
694 static const char * const nodes_path_8mm[] = {
695 "/vpu_g1@38300000",
696 "/vpu_g2@38310000",
697 "/vpu_h1@38320000"
698 };
699
700 static const char * const nodes_path_8mp[] = {
701 "/vpu_g1@38300000",
702 "/vpu_g2@38310000",
703 "/vpu_vc8000e@38320000"
704 };
705
706 if (is_imx8mq())
707 return disable_fdt_nodes(blob, nodes_path_8mq, ARRAY_SIZE(nodes_path_8mq));
708 else if (is_imx8mm())
709 return disable_fdt_nodes(blob, nodes_path_8mm, ARRAY_SIZE(nodes_path_8mm));
710 else if (is_imx8mp())
711 return disable_fdt_nodes(blob, nodes_path_8mp, ARRAY_SIZE(nodes_path_8mp));
712 else
713 return -EPERM;
714}
715
716int disable_gpu_nodes(void *blob)
717{
718 static const char * const nodes_path_8mn[] = {
719 "/gpu@38000000"
720 };
721
722 return disable_fdt_nodes(blob, nodes_path_8mn, ARRAY_SIZE(nodes_path_8mn));
723}
724
725int disable_npu_nodes(void *blob)
726{
727 static const char * const nodes_path_8mp[] = {
728 "/vipsi@38500000"
729 };
730
731 return disable_fdt_nodes(blob, nodes_path_8mp, ARRAY_SIZE(nodes_path_8mp));
732}
733
734int disable_isp_nodes(void *blob)
735{
736 static const char * const nodes_path_8mp[] = {
737 "/soc@0/bus@32c00000/camera/isp@32e10000",
738 "/soc@0/bus@32c00000/camera/isp@32e20000"
739 };
740
741 return disable_fdt_nodes(blob, nodes_path_8mp, ARRAY_SIZE(nodes_path_8mp));
742}
743
744int disable_dsp_nodes(void *blob)
745{
746 static const char * const nodes_path_8mp[] = {
747 "/dsp@3b6e8000"
748 };
749
750 return disable_fdt_nodes(blob, nodes_path_8mp, ARRAY_SIZE(nodes_path_8mp));
751}
752
753static int disable_cpu_nodes(void *blob, u32 disabled_cores)
754{
755 static const char * const nodes_path[] = {
756 "/cpus/cpu@1",
757 "/cpus/cpu@2",
758 "/cpus/cpu@3",
759 };
760 u32 i = 0;
761 int rc;
762 int nodeoff;
763
764 if (disabled_cores > 3)
765 return -EINVAL;
766
767 i = 3 - disabled_cores;
768
769 for (; i < 3; i++) {
770 nodeoff = fdt_path_offset(blob, nodes_path[i]);
771 if (nodeoff < 0)
772 continue; /* Not found, skip it */
773
774 debug("Found %s node\n", nodes_path[i]);
775
776 rc = fdt_del_node(blob, nodeoff);
777 if (rc < 0) {
778 printf("Unable to delete node %s, err=%s\n",
779 nodes_path[i], fdt_strerror(rc));
780 } else {
781 printf("Delete node %s\n", nodes_path[i]);
782 }
783 }
784
785 return 0;
786}
787
Masahiro Yamadab75d8dc2020-06-26 15:13:33 +0900788int ft_system_setup(void *blob, struct bd_info *bd)
Peng Fan6036dba2020-07-09 14:06:49 +0800789{
790#ifdef CONFIG_IMX8MQ
791 int i = 0;
792 int rc;
793 int nodeoff;
794
795 if (get_boot_device() == USB_BOOT) {
796 disable_dcss_nodes(blob);
797
798 bool new_path = check_fdt_new_path(blob);
799 int v = new_path ? 1 : 0;
800 static const char * const usb_dwc3_path[] = {
801 "/usb@38100000/dwc3",
802 "/soc@0/usb@38100000"
803 };
804
805 nodeoff = fdt_path_offset(blob, usb_dwc3_path[v]);
806 if (nodeoff >= 0) {
807 const char *speed = "high-speed";
808
809 printf("Found %s node\n", usb_dwc3_path[v]);
810
811usb_modify_speed:
812
813 rc = fdt_setprop(blob, nodeoff, "maximum-speed", speed, strlen(speed) + 1);
814 if (rc) {
815 if (rc == -FDT_ERR_NOSPACE) {
816 rc = fdt_increase_size(blob, 512);
817 if (!rc)
818 goto usb_modify_speed;
819 }
820 printf("Unable to set property %s:%s, err=%s\n",
821 usb_dwc3_path[v], "maximum-speed", fdt_strerror(rc));
822 } else {
823 printf("Modify %s:%s = %s\n",
824 usb_dwc3_path[v], "maximum-speed", speed);
825 }
826 } else {
827 printf("Can't found %s node\n", usb_dwc3_path[v]);
828 }
829 }
Peng Fanfcdbde72018-01-10 13:20:37 +0800830
831 /* Disable the CPU idle for A0 chip since the HW does not support it */
832 if (is_soc_rev(CHIP_REV_1_0)) {
833 static const char * const nodes_path[] = {
834 "/cpus/cpu@0",
835 "/cpus/cpu@1",
836 "/cpus/cpu@2",
837 "/cpus/cpu@3",
838 };
839
840 for (i = 0; i < ARRAY_SIZE(nodes_path); i++) {
841 nodeoff = fdt_path_offset(blob, nodes_path[i]);
842 if (nodeoff < 0)
843 continue; /* Not found, skip it */
844
Marek Vasutdabaabd2020-04-24 21:37:33 +0200845 debug("Found %s node\n", nodes_path[i]);
Peng Fanfcdbde72018-01-10 13:20:37 +0800846
847 rc = fdt_delprop(blob, nodeoff, "cpu-idle-states");
Marek Vasutdabaabd2020-04-24 21:37:33 +0200848 if (rc == -FDT_ERR_NOTFOUND)
849 continue;
Peng Fanfcdbde72018-01-10 13:20:37 +0800850 if (rc) {
851 printf("Unable to update property %s:%s, err=%s\n",
852 nodes_path[i], "status", fdt_strerror(rc));
853 return rc;
854 }
855
Marek Vasutdabaabd2020-04-24 21:37:33 +0200856 debug("Remove %s:%s\n", nodes_path[i],
Peng Fanfcdbde72018-01-10 13:20:37 +0800857 "cpu-idle-states");
858 }
859 }
860
Peng Fan6036dba2020-07-09 14:06:49 +0800861 if (is_imx8mql()) {
862 disable_vpu_nodes(blob);
863 if (check_dcss_fused()) {
864 printf("DCSS is fused\n");
865 disable_dcss_nodes(blob);
866 check_mipi_dsi_nodes(blob);
867 }
868 }
869
870 if (is_imx8md())
871 disable_cpu_nodes(blob, 2);
872
873#elif defined(CONFIG_IMX8MM)
874 if (is_imx8mml() || is_imx8mmdl() || is_imx8mmsl())
875 disable_vpu_nodes(blob);
876
877 if (is_imx8mmd() || is_imx8mmdl())
878 disable_cpu_nodes(blob, 2);
879 else if (is_imx8mms() || is_imx8mmsl())
880 disable_cpu_nodes(blob, 3);
881
882#elif defined(CONFIG_IMX8MN)
883 if (is_imx8mnl() || is_imx8mndl() || is_imx8mnsl())
884 disable_gpu_nodes(blob);
885
886 if (is_imx8mnd() || is_imx8mndl())
887 disable_cpu_nodes(blob, 2);
888 else if (is_imx8mns() || is_imx8mnsl())
889 disable_cpu_nodes(blob, 3);
890
891#elif defined(CONFIG_IMX8MP)
Peng Fan00982222020-09-16 15:17:22 +0800892 if (is_imx8mpl())
Peng Fan6036dba2020-07-09 14:06:49 +0800893 disable_vpu_nodes(blob);
894
Peng Fan00982222020-09-16 15:17:22 +0800895 if (is_imx8mpl() || is_imx8mp6())
Peng Fan6036dba2020-07-09 14:06:49 +0800896 disable_npu_nodes(blob);
897
Peng Fan00982222020-09-16 15:17:22 +0800898 if (is_imx8mpl())
Peng Fan6036dba2020-07-09 14:06:49 +0800899 disable_isp_nodes(blob);
900
Peng Fan00982222020-09-16 15:17:22 +0800901 if (is_imx8mpl() || is_imx8mp6())
Peng Fan6036dba2020-07-09 14:06:49 +0800902 disable_dsp_nodes(blob);
903
904 if (is_imx8mpd())
905 disable_cpu_nodes(blob, 2);
906#endif
907
Peng Fanfcdbde72018-01-10 13:20:37 +0800908 return 0;
909}
910#endif
911
Marek Vasutefa1a622020-04-29 15:04:21 +0200912#if !CONFIG_IS_ENABLED(SYSRESET)
Peng Fanfcdbde72018-01-10 13:20:37 +0800913void reset_cpu(ulong addr)
914{
Claudius Heinec5635a02020-04-29 15:04:23 +0200915 struct watchdog_regs *wdog = (struct watchdog_regs *)WDOG1_BASE_ADDR;
Peng Fanfcdbde72018-01-10 13:20:37 +0800916
Ye Li72479742019-12-09 00:47:18 -0800917 /* Clear WDA to trigger WDOG_B immediately */
918 writew((SET_WCR_WT(1) | WCR_WDT | WCR_WDE | WCR_SRS), &wdog->wcr);
Peng Fand2041722019-08-27 06:25:41 +0000919
Ye Li72479742019-12-09 00:47:18 -0800920 while (1) {
921 /*
Harald Seiler568af922020-04-29 15:04:22 +0200922 * spin for .5 seconds before reset
Ye Li72479742019-12-09 00:47:18 -0800923 */
924 }
Peng Fanfcdbde72018-01-10 13:20:37 +0800925}
Peng Fand2041722019-08-27 06:25:41 +0000926#endif
Peng Fana07bcec2020-04-22 10:51:13 +0800927
928#if defined(CONFIG_ARCH_MISC_INIT)
929static void acquire_buildinfo(void)
930{
931 u64 atf_commit = 0;
Peng Fana2f143e2020-05-11 15:14:04 +0800932 struct arm_smccc_res res;
Peng Fana07bcec2020-04-22 10:51:13 +0800933
934 /* Get ARM Trusted Firmware commit id */
Peng Fana2f143e2020-05-11 15:14:04 +0800935 arm_smccc_smc(IMX_SIP_BUILDINFO, IMX_SIP_BUILDINFO_GET_COMMITHASH,
Fabio Estevam68a699e2020-07-17 16:36:54 -0300936 0, 0, 0, 0, 0, 0, &res);
Peng Fana2f143e2020-05-11 15:14:04 +0800937 atf_commit = res.a0;
Peng Fana07bcec2020-04-22 10:51:13 +0800938 if (atf_commit == 0xffffffff) {
939 debug("ATF does not support build info\n");
940 atf_commit = 0x30; /* Display 0, 0 ascii is 0x30 */
941 }
942
943 printf("\n BuildInfo:\n - ATF %s\n\n", (char *)&atf_commit);
944}
945
946int arch_misc_init(void)
947{
948 acquire_buildinfo();
949
950 return 0;
951}
952#endif
Ye Li70487ff2020-05-03 22:19:52 +0800953
954void imx_tmu_arch_init(void *reg_base)
955{
Ye Li94c693d2020-05-03 22:19:53 +0800956 if (is_imx8mm() || is_imx8mn()) {
Ye Li70487ff2020-05-03 22:19:52 +0800957 /* Load TCALIV and TASR from fuses */
958 struct ocotp_regs *ocotp =
959 (struct ocotp_regs *)OCOTP_BASE_ADDR;
960 struct fuse_bank *bank = &ocotp->bank[3];
961 struct fuse_bank3_regs *fuse =
962 (struct fuse_bank3_regs *)bank->fuse_regs;
963
964 u32 tca_rt, tca_hr, tca_en;
965 u32 buf_vref, buf_slope;
966
967 tca_rt = fuse->ana0 & 0xFF;
968 tca_hr = (fuse->ana0 & 0xFF00) >> 8;
969 tca_en = (fuse->ana0 & 0x2000000) >> 25;
970
971 buf_vref = (fuse->ana0 & 0x1F00000) >> 20;
972 buf_slope = (fuse->ana0 & 0xF0000) >> 16;
973
974 writel(buf_vref | (buf_slope << 16), (ulong)reg_base + 0x28);
975 writel((tca_en << 31) | (tca_hr << 16) | tca_rt,
976 (ulong)reg_base + 0x30);
977 }
Ye Liebb9aab2020-05-03 22:19:54 +0800978#ifdef CONFIG_IMX8MP
979 /* Load TCALIV0/1/m40 and TRIM from fuses */
980 struct ocotp_regs *ocotp = (struct ocotp_regs *)OCOTP_BASE_ADDR;
981 struct fuse_bank *bank = &ocotp->bank[38];
982 struct fuse_bank38_regs *fuse =
983 (struct fuse_bank38_regs *)bank->fuse_regs;
984 struct fuse_bank *bank2 = &ocotp->bank[39];
985 struct fuse_bank39_regs *fuse2 =
986 (struct fuse_bank39_regs *)bank2->fuse_regs;
987 u32 buf_vref, buf_slope, bjt_cur, vlsb, bgr;
988 u32 reg;
989 u32 tca40[2], tca25[2], tca105[2];
990
991 /* For blank sample */
992 if (!fuse->ana_trim2 && !fuse->ana_trim3 &&
993 !fuse->ana_trim4 && !fuse2->ana_trim5) {
994 /* Use a default 25C binary codes */
995 tca25[0] = 1596;
Ye Li3462b552020-05-03 22:19:55 +0800996 tca25[1] = 1596;
Ye Liebb9aab2020-05-03 22:19:54 +0800997 writel(tca25[0], (ulong)reg_base + 0x30);
Ye Li3462b552020-05-03 22:19:55 +0800998 writel(tca25[1], (ulong)reg_base + 0x34);
Ye Liebb9aab2020-05-03 22:19:54 +0800999 return;
1000 }
1001
1002 buf_vref = (fuse->ana_trim2 & 0xc0) >> 6;
1003 buf_slope = (fuse->ana_trim2 & 0xF00) >> 8;
1004 bjt_cur = (fuse->ana_trim2 & 0xF000) >> 12;
1005 bgr = (fuse->ana_trim2 & 0xF0000) >> 16;
1006 vlsb = (fuse->ana_trim2 & 0xF00000) >> 20;
1007 writel(buf_vref | (buf_slope << 16), (ulong)reg_base + 0x28);
1008
1009 reg = (bgr << 28) | (bjt_cur << 20) | (vlsb << 12) | (1 << 7);
1010 writel(reg, (ulong)reg_base + 0x3c);
1011
1012 tca40[0] = (fuse->ana_trim3 & 0xFFF0000) >> 16;
1013 tca25[0] = (fuse->ana_trim3 & 0xF0000000) >> 28;
1014 tca25[0] |= ((fuse->ana_trim4 & 0xFF) << 4);
1015 tca105[0] = (fuse->ana_trim4 & 0xFFF00) >> 8;
1016 tca40[1] = (fuse->ana_trim4 & 0xFFF00000) >> 20;
1017 tca25[1] = fuse2->ana_trim5 & 0xFFF;
1018 tca105[1] = (fuse2->ana_trim5 & 0xFFF000) >> 12;
1019
1020 /* use 25c for 1p calibration */
1021 writel(tca25[0] | (tca105[0] << 16), (ulong)reg_base + 0x30);
1022 writel(tca25[1] | (tca105[1] << 16), (ulong)reg_base + 0x34);
1023 writel(tca40[0] | (tca40[1] << 16), (ulong)reg_base + 0x38);
1024#endif
Ye Li70487ff2020-05-03 22:19:52 +08001025}
Peng Fan2f3c9202020-07-09 13:39:26 +08001026
1027#if defined(CONFIG_SPL_BUILD)
1028#if defined(CONFIG_IMX8MQ) || defined(CONFIG_IMX8MM) || defined(CONFIG_IMX8MN)
1029bool serror_need_skip = true;
1030
1031void do_error(struct pt_regs *pt_regs, unsigned int esr)
1032{
1033 /*
1034 * If stack is still in ROM reserved OCRAM not switch to SPL,
1035 * it is the ROM SError
1036 */
1037 ulong sp;
1038
1039 asm volatile("mov %0, sp" : "=r"(sp) : );
1040
1041 if (serror_need_skip && sp < 0x910000 && sp >= 0x900000) {
1042 /* Check for ERR050342, imx8mq HDCP enabled parts */
1043 if (is_imx8mq() && !(readl(OCOTP_BASE_ADDR + 0x450) & 0x08000000)) {
1044 serror_need_skip = false;
1045 return; /* Do nothing skip the SError in ROM */
1046 }
1047
1048 /* Check for ERR050350, field return mode for imx8mq, mm and mn */
1049 if (readl(OCOTP_BASE_ADDR + 0x630) & 0x1) {
1050 serror_need_skip = false;
1051 return; /* Do nothing skip the SError in ROM */
1052 }
1053 }
1054
1055 efi_restore_gd();
1056 printf("\"Error\" handler, esr 0x%08x\n", esr);
1057 show_regs(pt_regs);
1058 panic("Resetting CPU ...\n");
1059}
1060#endif
1061#endif
Ye Li2707faf2019-07-15 01:16:46 -07001062
1063#if defined(CONFIG_IMX8MN) || defined(CONFIG_IMX8MP)
1064enum env_location env_get_location(enum env_operation op, int prio)
1065{
1066 enum boot_device dev = get_boot_device();
1067 enum env_location env_loc = ENVL_UNKNOWN;
1068
1069 if (prio)
1070 return env_loc;
1071
1072 switch (dev) {
1073#ifdef CONFIG_ENV_IS_IN_SPI_FLASH
1074 case QSPI_BOOT:
1075 env_loc = ENVL_SPI_FLASH;
1076 break;
1077#endif
1078#ifdef CONFIG_ENV_IS_IN_NAND
1079 case NAND_BOOT:
1080 env_loc = ENVL_NAND;
1081 break;
1082#endif
1083#ifdef CONFIG_ENV_IS_IN_MMC
1084 case SD1_BOOT:
1085 case SD2_BOOT:
1086 case SD3_BOOT:
1087 case MMC1_BOOT:
1088 case MMC2_BOOT:
1089 case MMC3_BOOT:
1090 env_loc = ENVL_MMC;
1091 break;
1092#endif
1093 default:
1094#if defined(CONFIG_ENV_IS_NOWHERE)
1095 env_loc = ENVL_NOWHERE;
1096#endif
1097 break;
1098 }
1099
1100 return env_loc;
1101}
1102
1103#ifndef ENV_IS_EMBEDDED
1104long long env_get_offset(long long defautl_offset)
1105{
1106 enum boot_device dev = get_boot_device();
1107
1108 switch (dev) {
1109 case NAND_BOOT:
1110 return (60 << 20); /* 60MB offset for NAND */
1111 default:
1112 break;
1113 }
1114
1115 return defautl_offset;
1116}
1117#endif
1118#endif