blob: 7ff8d8fa8c4f7b466489405b6de1b74c3cadd5e4 [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 }, {
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 Faneae4de22018-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 Fanb749b5e2019-08-27 06:25:27 +0000134 .size = PHYS_SDRAM_SIZE,
Peng Faneae4de22018-01-10 13:20:37 +0800135 .attrs = PTE_BLOCK_MEMTYPE(MT_NORMAL) |
136 PTE_BLOCK_OUTER_SHARE
Peng Fanb749b5e2019-08-27 06:25:27 +0000137#ifdef PHYS_SDRAM_2_SIZE
Peng Faneae4de22018-01-10 13:20:37 +0800138 }, {
139 /* DRAM2 */
140 .virt = 0x100000000UL,
141 .phys = 0x100000000UL,
Peng Fanb749b5e2019-08-27 06:25:27 +0000142 .size = PHYS_SDRAM_2_SIZE,
Peng Faneae4de22018-01-10 13:20:37 +0800143 .attrs = PTE_BLOCK_MEMTYPE(MT_NORMAL) |
144 PTE_BLOCK_OUTER_SHARE
Peng Fanb749b5e2019-08-27 06:25:27 +0000145#endif
Peng Faneae4de22018-01-10 13:20:37 +0800146 }, {
Peng Fanfa35c3d2020-07-09 15:26:06 +0800147 /* empty entrie to split table entry 5 if needed when TEEs are used */
148 0,
149 }, {
Peng Faneae4de22018-01-10 13:20:37 +0800150 /* List terminator */
151 0,
152 }
153};
154
155struct mm_region *mem_map = imx8m_mem_map;
156
Marek Vasute48aac02021-02-27 14:59:00 +0100157static unsigned int imx8m_find_dram_entry_in_mem_map(void)
158{
159 int i;
160
161 for (i = 0; i < ARRAY_SIZE(imx8m_mem_map); i++)
162 if (imx8m_mem_map[i].phys == CONFIG_SYS_SDRAM_BASE)
163 return i;
164
165 hang(); /* Entry not found, this must never happen. */
166}
167
Peng Fanb749b5e2019-08-27 06:25:27 +0000168void enable_caches(void)
169{
Peng Fanfa35c3d2020-07-09 15:26:06 +0800170 /* If OPTEE runs, remove OPTEE memory from MMU table to avoid speculative prefetch */
171 if (rom_pointer[1]) {
172 /*
173 * TEE are loaded, So the ddr bank structures
174 * have been modified update mmu table accordingly
175 */
176 int i = 0;
177 /*
178 * please make sure that entry initial value matches
179 * imx8m_mem_map for DRAM1
180 */
Marek Vasute48aac02021-02-27 14:59:00 +0100181 int entry = imx8m_find_dram_entry_in_mem_map();
Peng Fanfa35c3d2020-07-09 15:26:06 +0800182 u64 attrs = imx8m_mem_map[entry].attrs;
183
Marek Vasute48aac02021-02-27 14:59:00 +0100184 while (i < CONFIG_NR_DRAM_BANKS &&
185 entry < ARRAY_SIZE(imx8m_mem_map)) {
Peng Fanfa35c3d2020-07-09 15:26:06 +0800186 if (gd->bd->bi_dram[i].start == 0)
187 break;
188 imx8m_mem_map[entry].phys = gd->bd->bi_dram[i].start;
189 imx8m_mem_map[entry].virt = gd->bd->bi_dram[i].start;
190 imx8m_mem_map[entry].size = gd->bd->bi_dram[i].size;
191 imx8m_mem_map[entry].attrs = attrs;
192 debug("Added memory mapping (%d): %llx %llx\n", entry,
193 imx8m_mem_map[entry].phys, imx8m_mem_map[entry].size);
194 i++; entry++;
195 }
196 }
Peng Fanb749b5e2019-08-27 06:25:27 +0000197
198 icache_enable();
199 dcache_enable();
200}
201
Peng Fanfa35c3d2020-07-09 15:26:06 +0800202__weak int board_phys_sdram_size(phys_size_t *size)
203{
204 if (!size)
205 return -EINVAL;
206
207 *size = PHYS_SDRAM_SIZE;
208 return 0;
209}
210
211int dram_init(void)
212{
Marek Vasute48aac02021-02-27 14:59:00 +0100213 unsigned int entry = imx8m_find_dram_entry_in_mem_map();
Peng Fanfa35c3d2020-07-09 15:26:06 +0800214 phys_size_t sdram_size;
215 int ret;
216
217 ret = board_phys_sdram_size(&sdram_size);
218 if (ret)
219 return ret;
220
221 /* rom_pointer[1] contains the size of TEE occupies */
222 if (rom_pointer[1])
223 gd->ram_size = sdram_size - rom_pointer[1];
224 else
225 gd->ram_size = sdram_size;
226
Tim Harvey32927bc2020-09-25 08:08:35 -0700227 /* also update the SDRAM size in the mem_map used externally */
Marek Vasute48aac02021-02-27 14:59:00 +0100228 imx8m_mem_map[entry].size = sdram_size;
Tim Harvey32927bc2020-09-25 08:08:35 -0700229
Peng Fanfa35c3d2020-07-09 15:26:06 +0800230#ifdef PHYS_SDRAM_2_SIZE
231 gd->ram_size += PHYS_SDRAM_2_SIZE;
232#endif
233
234 return 0;
235}
236
237int dram_init_banksize(void)
238{
239 int bank = 0;
240 int ret;
241 phys_size_t sdram_size;
242
243 ret = board_phys_sdram_size(&sdram_size);
244 if (ret)
245 return ret;
246
247 gd->bd->bi_dram[bank].start = PHYS_SDRAM;
248 if (rom_pointer[1]) {
249 phys_addr_t optee_start = (phys_addr_t)rom_pointer[0];
250 phys_size_t optee_size = (size_t)rom_pointer[1];
251
252 gd->bd->bi_dram[bank].size = optee_start - gd->bd->bi_dram[bank].start;
253 if ((optee_start + optee_size) < (PHYS_SDRAM + sdram_size)) {
254 if (++bank >= CONFIG_NR_DRAM_BANKS) {
255 puts("CONFIG_NR_DRAM_BANKS is not enough\n");
256 return -1;
257 }
258
259 gd->bd->bi_dram[bank].start = optee_start + optee_size;
260 gd->bd->bi_dram[bank].size = PHYS_SDRAM +
261 sdram_size - gd->bd->bi_dram[bank].start;
262 }
263 } else {
264 gd->bd->bi_dram[bank].size = sdram_size;
265 }
266
267#ifdef PHYS_SDRAM_2_SIZE
268 if (++bank >= CONFIG_NR_DRAM_BANKS) {
269 puts("CONFIG_NR_DRAM_BANKS is not enough for SDRAM_2\n");
270 return -1;
271 }
272 gd->bd->bi_dram[bank].start = PHYS_SDRAM_2;
273 gd->bd->bi_dram[bank].size = PHYS_SDRAM_2_SIZE;
274#endif
275
276 return 0;
277}
278
279phys_size_t get_effective_memsize(void)
280{
281 /* return the first bank as effective memory */
282 if (rom_pointer[1])
283 return ((phys_addr_t)rom_pointer[0] - PHYS_SDRAM);
284
285#ifdef PHYS_SDRAM_2_SIZE
286 return gd->ram_size - PHYS_SDRAM_2_SIZE;
287#else
288 return gd->ram_size;
289#endif
290}
291
Peng Fan1caffdf2019-08-27 06:25:17 +0000292static u32 get_cpu_variant_type(u32 type)
293{
294 struct ocotp_regs *ocotp = (struct ocotp_regs *)OCOTP_BASE_ADDR;
295 struct fuse_bank *bank = &ocotp->bank[1];
296 struct fuse_bank1_regs *fuse =
297 (struct fuse_bank1_regs *)bank->fuse_regs;
298
299 u32 value = readl(&fuse->tester4);
300
Peng Fan67815082020-02-05 17:34:54 +0800301 if (type == MXC_CPU_IMX8MQ) {
302 if ((value & 0x3) == 0x2)
303 return MXC_CPU_IMX8MD;
304 else if (value & 0x200000)
305 return MXC_CPU_IMX8MQL;
306
307 } else if (type == MXC_CPU_IMX8MM) {
Peng Fan1caffdf2019-08-27 06:25:17 +0000308 switch (value & 0x3) {
309 case 2:
310 if (value & 0x1c0000)
311 return MXC_CPU_IMX8MMDL;
312 else
313 return MXC_CPU_IMX8MMD;
314 case 3:
315 if (value & 0x1c0000)
316 return MXC_CPU_IMX8MMSL;
317 else
318 return MXC_CPU_IMX8MMS;
319 default:
320 if (value & 0x1c0000)
321 return MXC_CPU_IMX8MML;
322 break;
323 }
Peng Fan1a07d912020-02-05 17:39:27 +0800324 } else if (type == MXC_CPU_IMX8MN) {
325 switch (value & 0x3) {
326 case 2:
Ye Li715180e2021-03-19 15:57:11 +0800327 if (value & 0x1000000) {
328 if (value & 0x10000000) /* MIPI DSI */
329 return MXC_CPU_IMX8MNUD;
330 else
331 return MXC_CPU_IMX8MNDL;
332 } else {
Peng Fan1a07d912020-02-05 17:39:27 +0800333 return MXC_CPU_IMX8MND;
Ye Li715180e2021-03-19 15:57:11 +0800334 }
Peng Fan1a07d912020-02-05 17:39:27 +0800335 case 3:
Ye Li715180e2021-03-19 15:57:11 +0800336 if (value & 0x1000000) {
337 if (value & 0x10000000) /* MIPI DSI */
338 return MXC_CPU_IMX8MNUS;
339 else
340 return MXC_CPU_IMX8MNSL;
341 } else {
Peng Fan1a07d912020-02-05 17:39:27 +0800342 return MXC_CPU_IMX8MNS;
Ye Li715180e2021-03-19 15:57:11 +0800343 }
Peng Fan1a07d912020-02-05 17:39:27 +0800344 default:
Ye Li715180e2021-03-19 15:57:11 +0800345 if (value & 0x1000000) {
346 if (value & 0x10000000) /* MIPI DSI */
347 return MXC_CPU_IMX8MNUQ;
348 else
349 return MXC_CPU_IMX8MNL;
350 }
Peng Fan1a07d912020-02-05 17:39:27 +0800351 break;
352 }
Ye Lid2d754f2020-04-20 20:12:54 -0700353 } else if (type == MXC_CPU_IMX8MP) {
354 u32 value0 = readl(&fuse->tester3);
355 u32 flag = 0;
356
357 if ((value0 & 0xc0000) == 0x80000)
358 return MXC_CPU_IMX8MPD;
359
360 /* vpu disabled */
361 if ((value0 & 0x43000000) == 0x43000000)
362 flag = 1;
363
364 /* npu disabled*/
365 if ((value & 0x8) == 0x8)
366 flag |= (1 << 1);
367
368 /* isp disabled */
369 if ((value & 0x3) == 0x3)
370 flag |= (1 << 2);
371
372 switch (flag) {
373 case 7:
374 return MXC_CPU_IMX8MPL;
Ye Lid2d754f2020-04-20 20:12:54 -0700375 case 2:
376 return MXC_CPU_IMX8MP6;
Ye Lid2d754f2020-04-20 20:12:54 -0700377 default:
378 break;
379 }
380
Peng Fan1caffdf2019-08-27 06:25:17 +0000381 }
382
383 return type;
384}
385
Peng Faneae4de22018-01-10 13:20:37 +0800386u32 get_cpu_rev(void)
387{
388 struct anamix_pll *ana_pll = (struct anamix_pll *)ANATOP_BASE_ADDR;
389 u32 reg = readl(&ana_pll->digprog);
390 u32 type = (reg >> 16) & 0xff;
Peng Fan1caffdf2019-08-27 06:25:17 +0000391 u32 major_low = (reg >> 8) & 0xff;
Peng Faneae4de22018-01-10 13:20:37 +0800392 u32 rom_version;
393
394 reg &= 0xff;
395
Peng Fan69cec072019-12-27 10:14:02 +0800396 /* iMX8MP */
397 if (major_low == 0x43) {
Ye Lid2d754f2020-04-20 20:12:54 -0700398 type = get_cpu_variant_type(MXC_CPU_IMX8MP);
Peng Fan69cec072019-12-27 10:14:02 +0800399 } else if (major_low == 0x42) {
400 /* iMX8MN */
Peng Fan1a07d912020-02-05 17:39:27 +0800401 type = get_cpu_variant_type(MXC_CPU_IMX8MN);
Peng Fan5d2f2062019-06-27 17:23:49 +0800402 } else if (major_low == 0x41) {
Peng Fan1caffdf2019-08-27 06:25:17 +0000403 type = get_cpu_variant_type(MXC_CPU_IMX8MM);
404 } else {
405 if (reg == CHIP_REV_1_0) {
406 /*
Peng Fanc23fbdd2019-10-16 10:24:17 +0000407 * For B0 chip, the DIGPROG is not updated,
408 * it is still TO1.0. we have to check ROM
409 * version or OCOTP_READ_FUSE_DATA.
410 * 0xff0055aa is magic number for B1.
Peng Fan1caffdf2019-08-27 06:25:17 +0000411 */
Peng Fanc23fbdd2019-10-16 10:24:17 +0000412 if (readl((void __iomem *)(OCOTP_BASE_ADDR + 0x40)) == 0xff0055aa) {
413 reg = CHIP_REV_2_1;
414 } else {
415 rom_version =
416 readl((void __iomem *)ROM_VERSION_A0);
417 if (rom_version != CHIP_REV_1_0) {
418 rom_version = readl((void __iomem *)ROM_VERSION_B0);
Patrick Wildtd4a78b92019-11-19 09:42:06 +0100419 rom_version &= 0xff;
Peng Fanc23fbdd2019-10-16 10:24:17 +0000420 if (rom_version == CHIP_REV_2_0)
421 reg = CHIP_REV_2_0;
422 }
Peng Fan1caffdf2019-08-27 06:25:17 +0000423 }
Peng Faneae4de22018-01-10 13:20:37 +0800424 }
Peng Fan67815082020-02-05 17:34:54 +0800425
426 type = get_cpu_variant_type(type);
Peng Faneae4de22018-01-10 13:20:37 +0800427 }
428
429 return (type << 12) | reg;
430}
431
432static void imx_set_wdog_powerdown(bool enable)
433{
434 struct wdog_regs *wdog1 = (struct wdog_regs *)WDOG1_BASE_ADDR;
435 struct wdog_regs *wdog2 = (struct wdog_regs *)WDOG2_BASE_ADDR;
436 struct wdog_regs *wdog3 = (struct wdog_regs *)WDOG3_BASE_ADDR;
437
438 /* Write to the PDE (Power Down Enable) bit */
439 writew(enable, &wdog1->wmcr);
440 writew(enable, &wdog2->wmcr);
441 writew(enable, &wdog3->wmcr);
442}
443
Peng Fanc98e0322019-08-27 06:25:58 +0000444int arch_cpu_init_dm(void)
445{
446 struct udevice *dev;
447 int ret;
448
Peng Fan3c073342019-10-16 03:01:51 +0000449 if (CONFIG_IS_ENABLED(CLK)) {
450 ret = uclass_get_device_by_name(UCLASS_CLK,
451 "clock-controller@30380000",
452 &dev);
453 if (ret < 0) {
454 printf("Failed to find clock node. Check device tree\n");
455 return ret;
456 }
Peng Fanc98e0322019-08-27 06:25:58 +0000457 }
458
459 return 0;
460}
461
Peng Faneae4de22018-01-10 13:20:37 +0800462int arch_cpu_init(void)
463{
Peng Fanc0b30d72019-04-17 09:41:16 +0000464 struct ocotp_regs *ocotp = (struct ocotp_regs *)OCOTP_BASE_ADDR;
Peng Faneae4de22018-01-10 13:20:37 +0800465 /*
Peng Fand0ca2892019-08-27 06:25:37 +0000466 * ROM might disable clock for SCTR,
467 * enable the clock before timer_init.
468 */
469 if (IS_ENABLED(CONFIG_SPL_BUILD))
470 clock_enable(CCGR_SCTR, 1);
471 /*
Peng Faneae4de22018-01-10 13:20:37 +0800472 * Init timer at very early state, because sscg pll setting
473 * will use it
474 */
475 timer_init();
476
477 if (IS_ENABLED(CONFIG_SPL_BUILD)) {
478 clock_init();
479 imx_set_wdog_powerdown(false);
Peng Fan9cf2aa32020-07-09 13:52:41 +0800480
481 if (is_imx8md() || is_imx8mmd() || is_imx8mmdl() || is_imx8mms() ||
482 is_imx8mmsl() || is_imx8mnd() || is_imx8mndl() || is_imx8mns() ||
Ye Li715180e2021-03-19 15:57:11 +0800483 is_imx8mnsl() || is_imx8mpd() || is_imx8mnud() || is_imx8mnus()) {
Peng Fan9cf2aa32020-07-09 13:52:41 +0800484 /* Power down cpu core 1, 2 and 3 for iMX8M Dual core or Single core */
485 struct pgc_reg *pgc_core1 = (struct pgc_reg *)(GPC_BASE_ADDR + 0x840);
486 struct pgc_reg *pgc_core2 = (struct pgc_reg *)(GPC_BASE_ADDR + 0x880);
487 struct pgc_reg *pgc_core3 = (struct pgc_reg *)(GPC_BASE_ADDR + 0x8C0);
488 struct gpc_reg *gpc = (struct gpc_reg *)GPC_BASE_ADDR;
489
490 writel(0x1, &pgc_core2->pgcr);
491 writel(0x1, &pgc_core3->pgcr);
Ye Li715180e2021-03-19 15:57:11 +0800492 if (is_imx8mms() || is_imx8mmsl() || is_imx8mns() || is_imx8mnsl() || is_imx8mnus()) {
Peng Fan9cf2aa32020-07-09 13:52:41 +0800493 writel(0x1, &pgc_core1->pgcr);
494 writel(0xE, &gpc->cpu_pgc_dn_trg);
495 } else {
496 writel(0xC, &gpc->cpu_pgc_dn_trg);
497 }
498 }
Peng Faneae4de22018-01-10 13:20:37 +0800499 }
500
Peng Fanc0b30d72019-04-17 09:41:16 +0000501 if (is_imx8mq()) {
502 clock_enable(CCGR_OCOTP, 1);
503 if (readl(&ocotp->ctrl) & 0x200)
504 writel(0x200, &ocotp->ctrl_clr);
505 }
506
Peng Faneae4de22018-01-10 13:20:37 +0800507 return 0;
508}
509
Peng Fanc9823b02019-09-16 03:09:36 +0000510#if defined(CONFIG_IMX8MN) || defined(CONFIG_IMX8MP)
511struct rom_api *g_rom_api = (struct rom_api *)0x980;
512
513enum boot_device get_boot_device(void)
514{
515 volatile gd_t *pgd = gd;
516 int ret;
517 u32 boot;
518 u16 boot_type;
519 u8 boot_instance;
520 enum boot_device boot_dev = SD1_BOOT;
521
522 ret = g_rom_api->query_boot_infor(QUERY_BT_DEV, &boot,
523 ((uintptr_t)&boot) ^ QUERY_BT_DEV);
524 gd = pgd;
525
526 if (ret != ROM_API_OKAY) {
527 puts("ROMAPI: failure at query_boot_info\n");
528 return -1;
529 }
530
531 boot_type = boot >> 16;
532 boot_instance = (boot >> 8) & 0xff;
533
534 switch (boot_type) {
535 case BT_DEV_TYPE_SD:
536 boot_dev = boot_instance + SD1_BOOT;
537 break;
538 case BT_DEV_TYPE_MMC:
539 boot_dev = boot_instance + MMC1_BOOT;
540 break;
541 case BT_DEV_TYPE_NAND:
542 boot_dev = NAND_BOOT;
543 break;
544 case BT_DEV_TYPE_FLEXSPINOR:
545 boot_dev = QSPI_BOOT;
546 break;
547 case BT_DEV_TYPE_USB:
548 boot_dev = USB_BOOT;
549 break;
550 default:
551 break;
552 }
553
554 return boot_dev;
555}
556#endif
557
Peng Faneae4de22018-01-10 13:20:37 +0800558bool is_usb_boot(void)
559{
560 return get_boot_device() == USB_BOOT;
561}
562
563#ifdef CONFIG_OF_SYSTEM_SETUP
Peng Fan435dc122020-07-09 14:06:49 +0800564bool check_fdt_new_path(void *blob)
565{
566 const char *soc_path = "/soc@0";
567 int nodeoff;
568
569 nodeoff = fdt_path_offset(blob, soc_path);
570 if (nodeoff < 0)
571 return false;
572
573 return true;
574}
575
576static int disable_fdt_nodes(void *blob, const char *const nodes_path[], int size_array)
577{
578 int i = 0;
579 int rc;
580 int nodeoff;
581 const char *status = "disabled";
582
583 for (i = 0; i < size_array; i++) {
584 nodeoff = fdt_path_offset(blob, nodes_path[i]);
585 if (nodeoff < 0)
586 continue; /* Not found, skip it */
587
588 printf("Found %s node\n", nodes_path[i]);
589
590add_status:
591 rc = fdt_setprop(blob, nodeoff, "status", status, strlen(status) + 1);
592 if (rc) {
593 if (rc == -FDT_ERR_NOSPACE) {
594 rc = fdt_increase_size(blob, 512);
595 if (!rc)
596 goto add_status;
597 }
598 printf("Unable to update property %s:%s, err=%s\n",
599 nodes_path[i], "status", fdt_strerror(rc));
600 } else {
601 printf("Modify %s:%s disabled\n",
602 nodes_path[i], "status");
603 }
604 }
605
606 return 0;
607}
608
609#ifdef CONFIG_IMX8MQ
610bool check_dcss_fused(void)
611{
612 struct ocotp_regs *ocotp = (struct ocotp_regs *)OCOTP_BASE_ADDR;
613 struct fuse_bank *bank = &ocotp->bank[1];
614 struct fuse_bank1_regs *fuse =
615 (struct fuse_bank1_regs *)bank->fuse_regs;
616 u32 value = readl(&fuse->tester4);
617
618 if (value & 0x4000000)
619 return true;
620
621 return false;
622}
623
624static int disable_mipi_dsi_nodes(void *blob)
625{
626 static const char * const nodes_path[] = {
627 "/mipi_dsi@30A00000",
628 "/mipi_dsi_bridge@30A00000",
629 "/dsi_phy@30A00300",
630 "/soc@0/bus@30800000/mipi_dsi@30a00000",
631 "/soc@0/bus@30800000/dphy@30a00300"
632 };
633
634 return disable_fdt_nodes(blob, nodes_path, ARRAY_SIZE(nodes_path));
635}
636
637static int disable_dcss_nodes(void *blob)
638{
639 static const char * const nodes_path[] = {
640 "/dcss@0x32e00000",
641 "/dcss@32e00000",
642 "/hdmi@32c00000",
643 "/hdmi_cec@32c33800",
644 "/hdmi_drm@32c00000",
645 "/display-subsystem",
646 "/sound-hdmi",
647 "/sound-hdmi-arc",
648 "/soc@0/bus@32c00000/display-controller@32e00000",
649 "/soc@0/bus@32c00000/hdmi@32c00000",
650 };
651
652 return disable_fdt_nodes(blob, nodes_path, ARRAY_SIZE(nodes_path));
653}
654
655static int check_mipi_dsi_nodes(void *blob)
656{
657 static const char * const lcdif_path[] = {
658 "/lcdif@30320000",
659 "/soc@0/bus@30000000/lcdif@30320000"
660 };
661 static const char * const mipi_dsi_path[] = {
662 "/mipi_dsi@30A00000",
663 "/soc@0/bus@30800000/mipi_dsi@30a00000"
664 };
665 static const char * const lcdif_ep_path[] = {
666 "/lcdif@30320000/port@0/mipi-dsi-endpoint",
667 "/soc@0/bus@30000000/lcdif@30320000/port@0/endpoint"
668 };
669 static const char * const mipi_dsi_ep_path[] = {
670 "/mipi_dsi@30A00000/port@1/endpoint",
671 "/soc@0/bus@30800000/mipi_dsi@30a00000/ports/port@0/endpoint"
672 };
673
674 int lookup_node;
675 int nodeoff;
676 bool new_path = check_fdt_new_path(blob);
677 int i = new_path ? 1 : 0;
678
679 nodeoff = fdt_path_offset(blob, lcdif_path[i]);
680 if (nodeoff < 0 || !fdtdec_get_is_enabled(blob, nodeoff)) {
681 /*
682 * If can't find lcdif node or lcdif node is disabled,
683 * then disable all mipi dsi, since they only can input
684 * from DCSS
685 */
686 return disable_mipi_dsi_nodes(blob);
687 }
688
689 nodeoff = fdt_path_offset(blob, mipi_dsi_path[i]);
690 if (nodeoff < 0 || !fdtdec_get_is_enabled(blob, nodeoff))
691 return 0;
692
693 nodeoff = fdt_path_offset(blob, lcdif_ep_path[i]);
694 if (nodeoff < 0) {
695 /*
696 * If can't find lcdif endpoint, then disable all mipi dsi,
697 * since they only can input from DCSS
698 */
699 return disable_mipi_dsi_nodes(blob);
700 }
701
702 lookup_node = fdtdec_lookup_phandle(blob, nodeoff, "remote-endpoint");
703 nodeoff = fdt_path_offset(blob, mipi_dsi_ep_path[i]);
704
705 if (nodeoff > 0 && nodeoff == lookup_node)
706 return 0;
707
708 return disable_mipi_dsi_nodes(blob);
709}
710#endif
711
712int disable_vpu_nodes(void *blob)
713{
714 static const char * const nodes_path_8mq[] = {
715 "/vpu@38300000",
716 "/soc@0/vpu@38300000"
717 };
718
719 static const char * const nodes_path_8mm[] = {
720 "/vpu_g1@38300000",
721 "/vpu_g2@38310000",
722 "/vpu_h1@38320000"
723 };
724
725 static const char * const nodes_path_8mp[] = {
726 "/vpu_g1@38300000",
727 "/vpu_g2@38310000",
728 "/vpu_vc8000e@38320000"
729 };
730
731 if (is_imx8mq())
732 return disable_fdt_nodes(blob, nodes_path_8mq, ARRAY_SIZE(nodes_path_8mq));
733 else if (is_imx8mm())
734 return disable_fdt_nodes(blob, nodes_path_8mm, ARRAY_SIZE(nodes_path_8mm));
735 else if (is_imx8mp())
736 return disable_fdt_nodes(blob, nodes_path_8mp, ARRAY_SIZE(nodes_path_8mp));
737 else
738 return -EPERM;
739}
740
Ye Liee337ce2021-03-19 15:57:09 +0800741#ifdef CONFIG_IMX8MN_LOW_DRIVE_MODE
742static int low_drive_gpu_freq(void *blob)
743{
744 static const char *nodes_path_8mn[] = {
745 "/gpu@38000000",
746 "/soc@0/gpu@38000000"
747 };
748
749 int nodeoff, cnt, i;
750 u32 assignedclks[7];
751
752 nodeoff = fdt_path_offset(blob, nodes_path_8mn[0]);
753 if (nodeoff < 0)
754 return nodeoff;
755
756 cnt = fdtdec_get_int_array_count(blob, nodeoff, "assigned-clock-rates", assignedclks, 7);
757 if (cnt < 0)
758 return cnt;
759
760 if (cnt != 7)
761 printf("Warning: %s, assigned-clock-rates count %d\n", nodes_path_8mn[0], cnt);
762
763 assignedclks[cnt - 1] = 200000000;
764 assignedclks[cnt - 2] = 200000000;
765
766 for (i = 0; i < cnt; i++) {
767 debug("<%u>, ", assignedclks[i]);
768 assignedclks[i] = cpu_to_fdt32(assignedclks[i]);
769 }
770 debug("\n");
771
772 return fdt_setprop(blob, nodeoff, "assigned-clock-rates", &assignedclks, sizeof(assignedclks));
773}
774#endif
775
Peng Fan435dc122020-07-09 14:06:49 +0800776int disable_gpu_nodes(void *blob)
777{
778 static const char * const nodes_path_8mn[] = {
779 "/gpu@38000000"
780 };
781
782 return disable_fdt_nodes(blob, nodes_path_8mn, ARRAY_SIZE(nodes_path_8mn));
783}
784
785int disable_npu_nodes(void *blob)
786{
787 static const char * const nodes_path_8mp[] = {
788 "/vipsi@38500000"
789 };
790
791 return disable_fdt_nodes(blob, nodes_path_8mp, ARRAY_SIZE(nodes_path_8mp));
792}
793
794int disable_isp_nodes(void *blob)
795{
796 static const char * const nodes_path_8mp[] = {
797 "/soc@0/bus@32c00000/camera/isp@32e10000",
798 "/soc@0/bus@32c00000/camera/isp@32e20000"
799 };
800
801 return disable_fdt_nodes(blob, nodes_path_8mp, ARRAY_SIZE(nodes_path_8mp));
802}
803
804int disable_dsp_nodes(void *blob)
805{
806 static const char * const nodes_path_8mp[] = {
807 "/dsp@3b6e8000"
808 };
809
810 return disable_fdt_nodes(blob, nodes_path_8mp, ARRAY_SIZE(nodes_path_8mp));
811}
812
Ye Li26517af2021-03-19 15:57:12 +0800813static void disable_thermal_cpu_nodes(void *blob, u32 disabled_cores)
814{
815 static const char * const thermal_path[] = {
816 "/thermal-zones/cpu-thermal/cooling-maps/map0"
817 };
818
819 int nodeoff, cnt, i, ret, j;
820 u32 cooling_dev[12];
821
822 for (i = 0; i < ARRAY_SIZE(thermal_path); i++) {
823 nodeoff = fdt_path_offset(blob, thermal_path[i]);
824 if (nodeoff < 0)
825 continue; /* Not found, skip it */
826
827 cnt = fdtdec_get_int_array_count(blob, nodeoff, "cooling-device", cooling_dev, 12);
828 if (cnt < 0)
829 continue;
830
831 if (cnt != 12)
832 printf("Warning: %s, cooling-device count %d\n", thermal_path[i], cnt);
833
834 for (j = 0; j < cnt; j++)
835 cooling_dev[j] = cpu_to_fdt32(cooling_dev[j]);
836
837 ret = fdt_setprop(blob, nodeoff, "cooling-device", &cooling_dev,
838 sizeof(u32) * (12 - disabled_cores * 3));
839 if (ret < 0) {
840 printf("Warning: %s, cooling-device setprop failed %d\n",
841 thermal_path[i], ret);
842 continue;
843 }
844
845 printf("Update node %s, cooling-device prop\n", thermal_path[i]);
846 }
847}
848
849static void disable_pmu_cpu_nodes(void *blob, u32 disabled_cores)
850{
851 static const char * const pmu_path[] = {
852 "/pmu"
853 };
854
855 int nodeoff, cnt, i, ret, j;
856 u32 irq_affinity[4];
857
858 for (i = 0; i < ARRAY_SIZE(pmu_path); i++) {
859 nodeoff = fdt_path_offset(blob, pmu_path[i]);
860 if (nodeoff < 0)
861 continue; /* Not found, skip it */
862
863 cnt = fdtdec_get_int_array_count(blob, nodeoff, "interrupt-affinity",
864 irq_affinity, 4);
865 if (cnt < 0)
866 continue;
867
868 if (cnt != 4)
869 printf("Warning: %s, interrupt-affinity count %d\n", pmu_path[i], cnt);
870
871 for (j = 0; j < cnt; j++)
872 irq_affinity[j] = cpu_to_fdt32(irq_affinity[j]);
873
874 ret = fdt_setprop(blob, nodeoff, "interrupt-affinity", &irq_affinity,
875 sizeof(u32) * (4 - disabled_cores));
876 if (ret < 0) {
877 printf("Warning: %s, interrupt-affinity setprop failed %d\n",
878 pmu_path[i], ret);
879 continue;
880 }
881
882 printf("Update node %s, interrupt-affinity prop\n", pmu_path[i]);
883 }
884}
885
Peng Fan435dc122020-07-09 14:06:49 +0800886static int disable_cpu_nodes(void *blob, u32 disabled_cores)
887{
888 static const char * const nodes_path[] = {
889 "/cpus/cpu@1",
890 "/cpus/cpu@2",
891 "/cpus/cpu@3",
892 };
893 u32 i = 0;
894 int rc;
895 int nodeoff;
896
897 if (disabled_cores > 3)
898 return -EINVAL;
899
900 i = 3 - disabled_cores;
901
902 for (; i < 3; i++) {
903 nodeoff = fdt_path_offset(blob, nodes_path[i]);
904 if (nodeoff < 0)
905 continue; /* Not found, skip it */
906
907 debug("Found %s node\n", nodes_path[i]);
908
909 rc = fdt_del_node(blob, nodeoff);
910 if (rc < 0) {
911 printf("Unable to delete node %s, err=%s\n",
912 nodes_path[i], fdt_strerror(rc));
913 } else {
914 printf("Delete node %s\n", nodes_path[i]);
915 }
916 }
917
Ye Li26517af2021-03-19 15:57:12 +0800918 disable_thermal_cpu_nodes(blob, disabled_cores);
919 disable_pmu_cpu_nodes(blob, disabled_cores);
920
Peng Fan435dc122020-07-09 14:06:49 +0800921 return 0;
922}
923
Masahiro Yamadaf7ed78b2020-06-26 15:13:33 +0900924int ft_system_setup(void *blob, struct bd_info *bd)
Peng Faneae4de22018-01-10 13:20:37 +0800925{
Peng Fan435dc122020-07-09 14:06:49 +0800926#ifdef CONFIG_IMX8MQ
Peng Faneae4de22018-01-10 13:20:37 +0800927 int i = 0;
928 int rc;
929 int nodeoff;
930
Peng Fan435dc122020-07-09 14:06:49 +0800931 if (get_boot_device() == USB_BOOT) {
932 disable_dcss_nodes(blob);
933
934 bool new_path = check_fdt_new_path(blob);
935 int v = new_path ? 1 : 0;
936 static const char * const usb_dwc3_path[] = {
937 "/usb@38100000/dwc3",
938 "/soc@0/usb@38100000"
939 };
940
941 nodeoff = fdt_path_offset(blob, usb_dwc3_path[v]);
942 if (nodeoff >= 0) {
943 const char *speed = "high-speed";
944
945 printf("Found %s node\n", usb_dwc3_path[v]);
946
947usb_modify_speed:
948
949 rc = fdt_setprop(blob, nodeoff, "maximum-speed", speed, strlen(speed) + 1);
950 if (rc) {
951 if (rc == -FDT_ERR_NOSPACE) {
952 rc = fdt_increase_size(blob, 512);
953 if (!rc)
954 goto usb_modify_speed;
955 }
956 printf("Unable to set property %s:%s, err=%s\n",
957 usb_dwc3_path[v], "maximum-speed", fdt_strerror(rc));
958 } else {
959 printf("Modify %s:%s = %s\n",
960 usb_dwc3_path[v], "maximum-speed", speed);
961 }
962 } else {
963 printf("Can't found %s node\n", usb_dwc3_path[v]);
964 }
965 }
966
Peng Faneae4de22018-01-10 13:20:37 +0800967 /* Disable the CPU idle for A0 chip since the HW does not support it */
968 if (is_soc_rev(CHIP_REV_1_0)) {
969 static const char * const nodes_path[] = {
970 "/cpus/cpu@0",
971 "/cpus/cpu@1",
972 "/cpus/cpu@2",
973 "/cpus/cpu@3",
974 };
975
976 for (i = 0; i < ARRAY_SIZE(nodes_path); i++) {
977 nodeoff = fdt_path_offset(blob, nodes_path[i]);
978 if (nodeoff < 0)
979 continue; /* Not found, skip it */
980
Marek Vasute2e7a772020-04-24 21:37:33 +0200981 debug("Found %s node\n", nodes_path[i]);
Peng Faneae4de22018-01-10 13:20:37 +0800982
983 rc = fdt_delprop(blob, nodeoff, "cpu-idle-states");
Marek Vasute2e7a772020-04-24 21:37:33 +0200984 if (rc == -FDT_ERR_NOTFOUND)
985 continue;
Peng Faneae4de22018-01-10 13:20:37 +0800986 if (rc) {
987 printf("Unable to update property %s:%s, err=%s\n",
988 nodes_path[i], "status", fdt_strerror(rc));
989 return rc;
990 }
991
Marek Vasute2e7a772020-04-24 21:37:33 +0200992 debug("Remove %s:%s\n", nodes_path[i],
Peng Faneae4de22018-01-10 13:20:37 +0800993 "cpu-idle-states");
994 }
995 }
996
Peng Fan435dc122020-07-09 14:06:49 +0800997 if (is_imx8mql()) {
998 disable_vpu_nodes(blob);
999 if (check_dcss_fused()) {
1000 printf("DCSS is fused\n");
1001 disable_dcss_nodes(blob);
1002 check_mipi_dsi_nodes(blob);
1003 }
1004 }
1005
1006 if (is_imx8md())
1007 disable_cpu_nodes(blob, 2);
1008
1009#elif defined(CONFIG_IMX8MM)
1010 if (is_imx8mml() || is_imx8mmdl() || is_imx8mmsl())
1011 disable_vpu_nodes(blob);
1012
1013 if (is_imx8mmd() || is_imx8mmdl())
1014 disable_cpu_nodes(blob, 2);
1015 else if (is_imx8mms() || is_imx8mmsl())
1016 disable_cpu_nodes(blob, 3);
1017
1018#elif defined(CONFIG_IMX8MN)
1019 if (is_imx8mnl() || is_imx8mndl() || is_imx8mnsl())
1020 disable_gpu_nodes(blob);
Ye Liee337ce2021-03-19 15:57:09 +08001021#ifdef CONFIG_IMX8MN_LOW_DRIVE_MODE
1022 else {
1023 int ldm_gpu = low_drive_gpu_freq(blob);
1024
1025 if (ldm_gpu < 0)
1026 printf("Update GPU node assigned-clock-rates failed\n");
1027 else
1028 printf("Update GPU node assigned-clock-rates ok\n");
1029 }
1030#endif
Peng Fan435dc122020-07-09 14:06:49 +08001031
Ye Li715180e2021-03-19 15:57:11 +08001032 if (is_imx8mnd() || is_imx8mndl() || is_imx8mnud())
Peng Fan435dc122020-07-09 14:06:49 +08001033 disable_cpu_nodes(blob, 2);
Ye Li715180e2021-03-19 15:57:11 +08001034 else if (is_imx8mns() || is_imx8mnsl() || is_imx8mnus())
Peng Fan435dc122020-07-09 14:06:49 +08001035 disable_cpu_nodes(blob, 3);
1036
1037#elif defined(CONFIG_IMX8MP)
Peng Fan8a472a22020-09-16 15:17:22 +08001038 if (is_imx8mpl())
Peng Fan435dc122020-07-09 14:06:49 +08001039 disable_vpu_nodes(blob);
1040
Peng Fan8a472a22020-09-16 15:17:22 +08001041 if (is_imx8mpl() || is_imx8mp6())
Peng Fan435dc122020-07-09 14:06:49 +08001042 disable_npu_nodes(blob);
1043
Peng Fan8a472a22020-09-16 15:17:22 +08001044 if (is_imx8mpl())
Peng Fan435dc122020-07-09 14:06:49 +08001045 disable_isp_nodes(blob);
1046
Peng Fan8a472a22020-09-16 15:17:22 +08001047 if (is_imx8mpl() || is_imx8mp6())
Peng Fan435dc122020-07-09 14:06:49 +08001048 disable_dsp_nodes(blob);
1049
1050 if (is_imx8mpd())
1051 disable_cpu_nodes(blob, 2);
1052#endif
1053
Peng Faneae4de22018-01-10 13:20:37 +08001054 return 0;
1055}
1056#endif
1057
Marek Vasut64dc4de2020-04-29 15:04:21 +02001058#if !CONFIG_IS_ENABLED(SYSRESET)
Harald Seiler6f14d5f2020-12-15 16:47:52 +01001059void reset_cpu(void)
Peng Faneae4de22018-01-10 13:20:37 +08001060{
Claudius Heinee73f3942020-04-29 15:04:23 +02001061 struct watchdog_regs *wdog = (struct watchdog_regs *)WDOG1_BASE_ADDR;
Peng Faneae4de22018-01-10 13:20:37 +08001062
Ye Li54a915a2019-12-09 00:47:18 -08001063 /* Clear WDA to trigger WDOG_B immediately */
1064 writew((SET_WCR_WT(1) | WCR_WDT | WCR_WDE | WCR_SRS), &wdog->wcr);
Peng Fan24290d92019-08-27 06:25:41 +00001065
Ye Li54a915a2019-12-09 00:47:18 -08001066 while (1) {
1067 /*
Harald Seilerec0c4472020-04-29 15:04:22 +02001068 * spin for .5 seconds before reset
Ye Li54a915a2019-12-09 00:47:18 -08001069 */
1070 }
Peng Faneae4de22018-01-10 13:20:37 +08001071}
Peng Fan24290d92019-08-27 06:25:41 +00001072#endif
Peng Fan5760d8d2020-04-22 10:51:13 +08001073
1074#if defined(CONFIG_ARCH_MISC_INIT)
1075static void acquire_buildinfo(void)
1076{
1077 u64 atf_commit = 0;
Peng Fan45d843a2020-05-11 15:14:04 +08001078 struct arm_smccc_res res;
Peng Fan5760d8d2020-04-22 10:51:13 +08001079
1080 /* Get ARM Trusted Firmware commit id */
Peng Fan45d843a2020-05-11 15:14:04 +08001081 arm_smccc_smc(IMX_SIP_BUILDINFO, IMX_SIP_BUILDINFO_GET_COMMITHASH,
Fabio Estevam31e410f2020-07-17 16:36:54 -03001082 0, 0, 0, 0, 0, 0, &res);
Peng Fan45d843a2020-05-11 15:14:04 +08001083 atf_commit = res.a0;
Peng Fan5760d8d2020-04-22 10:51:13 +08001084 if (atf_commit == 0xffffffff) {
1085 debug("ATF does not support build info\n");
1086 atf_commit = 0x30; /* Display 0, 0 ascii is 0x30 */
1087 }
1088
1089 printf("\n BuildInfo:\n - ATF %s\n\n", (char *)&atf_commit);
1090}
1091
1092int arch_misc_init(void)
1093{
1094 acquire_buildinfo();
1095
1096 return 0;
1097}
1098#endif
Ye Li325cd012020-05-03 22:19:52 +08001099
1100void imx_tmu_arch_init(void *reg_base)
1101{
Ye Lia00f2f02020-05-03 22:19:53 +08001102 if (is_imx8mm() || is_imx8mn()) {
Ye Li325cd012020-05-03 22:19:52 +08001103 /* Load TCALIV and TASR from fuses */
1104 struct ocotp_regs *ocotp =
1105 (struct ocotp_regs *)OCOTP_BASE_ADDR;
1106 struct fuse_bank *bank = &ocotp->bank[3];
1107 struct fuse_bank3_regs *fuse =
1108 (struct fuse_bank3_regs *)bank->fuse_regs;
1109
1110 u32 tca_rt, tca_hr, tca_en;
1111 u32 buf_vref, buf_slope;
1112
1113 tca_rt = fuse->ana0 & 0xFF;
1114 tca_hr = (fuse->ana0 & 0xFF00) >> 8;
1115 tca_en = (fuse->ana0 & 0x2000000) >> 25;
1116
1117 buf_vref = (fuse->ana0 & 0x1F00000) >> 20;
1118 buf_slope = (fuse->ana0 & 0xF0000) >> 16;
1119
1120 writel(buf_vref | (buf_slope << 16), (ulong)reg_base + 0x28);
1121 writel((tca_en << 31) | (tca_hr << 16) | tca_rt,
1122 (ulong)reg_base + 0x30);
1123 }
Ye Li41a20252020-05-03 22:19:54 +08001124#ifdef CONFIG_IMX8MP
1125 /* Load TCALIV0/1/m40 and TRIM from fuses */
1126 struct ocotp_regs *ocotp = (struct ocotp_regs *)OCOTP_BASE_ADDR;
1127 struct fuse_bank *bank = &ocotp->bank[38];
1128 struct fuse_bank38_regs *fuse =
1129 (struct fuse_bank38_regs *)bank->fuse_regs;
1130 struct fuse_bank *bank2 = &ocotp->bank[39];
1131 struct fuse_bank39_regs *fuse2 =
1132 (struct fuse_bank39_regs *)bank2->fuse_regs;
1133 u32 buf_vref, buf_slope, bjt_cur, vlsb, bgr;
1134 u32 reg;
1135 u32 tca40[2], tca25[2], tca105[2];
1136
1137 /* For blank sample */
1138 if (!fuse->ana_trim2 && !fuse->ana_trim3 &&
1139 !fuse->ana_trim4 && !fuse2->ana_trim5) {
1140 /* Use a default 25C binary codes */
1141 tca25[0] = 1596;
Ye Lid756ca02020-05-03 22:19:55 +08001142 tca25[1] = 1596;
Ye Li41a20252020-05-03 22:19:54 +08001143 writel(tca25[0], (ulong)reg_base + 0x30);
Ye Lid756ca02020-05-03 22:19:55 +08001144 writel(tca25[1], (ulong)reg_base + 0x34);
Ye Li41a20252020-05-03 22:19:54 +08001145 return;
1146 }
1147
1148 buf_vref = (fuse->ana_trim2 & 0xc0) >> 6;
1149 buf_slope = (fuse->ana_trim2 & 0xF00) >> 8;
1150 bjt_cur = (fuse->ana_trim2 & 0xF000) >> 12;
1151 bgr = (fuse->ana_trim2 & 0xF0000) >> 16;
1152 vlsb = (fuse->ana_trim2 & 0xF00000) >> 20;
1153 writel(buf_vref | (buf_slope << 16), (ulong)reg_base + 0x28);
1154
1155 reg = (bgr << 28) | (bjt_cur << 20) | (vlsb << 12) | (1 << 7);
1156 writel(reg, (ulong)reg_base + 0x3c);
1157
1158 tca40[0] = (fuse->ana_trim3 & 0xFFF0000) >> 16;
1159 tca25[0] = (fuse->ana_trim3 & 0xF0000000) >> 28;
1160 tca25[0] |= ((fuse->ana_trim4 & 0xFF) << 4);
1161 tca105[0] = (fuse->ana_trim4 & 0xFFF00) >> 8;
1162 tca40[1] = (fuse->ana_trim4 & 0xFFF00000) >> 20;
1163 tca25[1] = fuse2->ana_trim5 & 0xFFF;
1164 tca105[1] = (fuse2->ana_trim5 & 0xFFF000) >> 12;
1165
1166 /* use 25c for 1p calibration */
1167 writel(tca25[0] | (tca105[0] << 16), (ulong)reg_base + 0x30);
1168 writel(tca25[1] | (tca105[1] << 16), (ulong)reg_base + 0x34);
1169 writel(tca40[0] | (tca40[1] << 16), (ulong)reg_base + 0x38);
1170#endif
Ye Li325cd012020-05-03 22:19:52 +08001171}
Peng Fana35215d2020-07-09 13:39:26 +08001172
1173#if defined(CONFIG_SPL_BUILD)
1174#if defined(CONFIG_IMX8MQ) || defined(CONFIG_IMX8MM) || defined(CONFIG_IMX8MN)
1175bool serror_need_skip = true;
1176
1177void do_error(struct pt_regs *pt_regs, unsigned int esr)
1178{
1179 /*
1180 * If stack is still in ROM reserved OCRAM not switch to SPL,
1181 * it is the ROM SError
1182 */
1183 ulong sp;
1184
1185 asm volatile("mov %0, sp" : "=r"(sp) : );
1186
1187 if (serror_need_skip && sp < 0x910000 && sp >= 0x900000) {
1188 /* Check for ERR050342, imx8mq HDCP enabled parts */
1189 if (is_imx8mq() && !(readl(OCOTP_BASE_ADDR + 0x450) & 0x08000000)) {
1190 serror_need_skip = false;
1191 return; /* Do nothing skip the SError in ROM */
1192 }
1193
1194 /* Check for ERR050350, field return mode for imx8mq, mm and mn */
1195 if (readl(OCOTP_BASE_ADDR + 0x630) & 0x1) {
1196 serror_need_skip = false;
1197 return; /* Do nothing skip the SError in ROM */
1198 }
1199 }
1200
1201 efi_restore_gd();
1202 printf("\"Error\" handler, esr 0x%08x\n", esr);
1203 show_regs(pt_regs);
1204 panic("Resetting CPU ...\n");
1205}
1206#endif
1207#endif
Ye Li0513f362019-07-15 01:16:46 -07001208
1209#if defined(CONFIG_IMX8MN) || defined(CONFIG_IMX8MP)
1210enum env_location env_get_location(enum env_operation op, int prio)
1211{
1212 enum boot_device dev = get_boot_device();
1213 enum env_location env_loc = ENVL_UNKNOWN;
1214
1215 if (prio)
1216 return env_loc;
1217
1218 switch (dev) {
1219#ifdef CONFIG_ENV_IS_IN_SPI_FLASH
1220 case QSPI_BOOT:
1221 env_loc = ENVL_SPI_FLASH;
1222 break;
1223#endif
1224#ifdef CONFIG_ENV_IS_IN_NAND
1225 case NAND_BOOT:
1226 env_loc = ENVL_NAND;
1227 break;
1228#endif
1229#ifdef CONFIG_ENV_IS_IN_MMC
1230 case SD1_BOOT:
1231 case SD2_BOOT:
1232 case SD3_BOOT:
1233 case MMC1_BOOT:
1234 case MMC2_BOOT:
1235 case MMC3_BOOT:
1236 env_loc = ENVL_MMC;
1237 break;
1238#endif
1239 default:
1240#if defined(CONFIG_ENV_IS_NOWHERE)
1241 env_loc = ENVL_NOWHERE;
1242#endif
1243 break;
1244 }
1245
1246 return env_loc;
1247}
1248
1249#ifndef ENV_IS_EMBEDDED
1250long long env_get_offset(long long defautl_offset)
1251{
1252 enum boot_device dev = get_boot_device();
1253
1254 switch (dev) {
1255 case NAND_BOOT:
1256 return (60 << 20); /* 60MB offset for NAND */
1257 default:
1258 break;
1259 }
1260
1261 return defautl_offset;
1262}
1263#endif
1264#endif