1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright 2017-2019 NXP
5 * Peng Fan <peng.fan@nxp.com>
12 #include <asm/arch/imx-regs.h>
14 #include <asm/arch/clock.h>
15 #include <asm/arch/sys_proto.h>
16 #include <asm/mach-imx/hab.h>
17 #include <asm/mach-imx/boot_mode.h>
18 #include <asm/mach-imx/syscounter.h>
19 #include <asm/ptrace.h>
20 #include <asm/armv8/mmu.h>
21 #include <dm/uclass.h>
22 #include <efi_loader.h>
24 #include <fdt_support.h>
27 #include <linux/arm-smccc.h>
28 #include <linux/bitops.h>
30 DECLARE_GLOBAL_DATA_PTR;
32 #if defined(CONFIG_IMX_HAB)
33 struct imx_sec_config_fuse_t const imx_sec_config_fuse = {
41 #ifdef CONFIG_SPL_BUILD
42 struct sctr_regs *sctr = (struct sctr_regs *)SYSCNT_CTRL_BASE_ADDR;
43 unsigned long freq = readl(&sctr->cntfid0);
45 /* Update with accurate clock frequency */
46 asm volatile("msr cntfrq_el0, %0" : : "r" (freq) : "memory");
48 clrsetbits_le32(&sctr->cntcr, SC_CNTCR_FREQ0 | SC_CNTCR_FREQ1,
49 SC_CNTCR_FREQ0 | SC_CNTCR_ENABLE | SC_CNTCR_HDBG);
58 void enable_tzc380(void)
60 struct iomuxc_gpr_base_regs *gpr =
61 (struct iomuxc_gpr_base_regs *)IOMUXC_GPR_BASE_ADDR;
63 /* Enable TZASC and lock setting */
64 setbits_le32(&gpr->gpr[10], GPR_TZASC_EN);
65 setbits_le32(&gpr->gpr[10], GPR_TZASC_EN_LOCK);
66 if (is_imx8mm() || is_imx8mn() || is_imx8mp())
67 setbits_le32(&gpr->gpr[10], BIT(1));
69 * set Region 0 attribute to allow secure and non-secure
70 * read/write permission. Found some masters like usb dwc3
71 * controllers can't work with secure memory.
73 writel(0xf0000000, TZASC_BASE_ADDR + 0x108);
76 void set_wdog_reset(struct wdog_regs *wdog)
79 * Output WDOG_B signal to reset external pmic or POR_B decided by
80 * the board design. Without external reset, the peripherals/DDR/
81 * PMIC are not reset, that may cause system working abnormal.
82 * WDZST bit is write-once only bit. Align this bit in kernel,
83 * otherwise kernel code will have no chance to set this bit.
85 setbits_le16(&wdog->wcr, WDOG_WDT_MASK | WDOG_WDZST_MASK);
88 static struct mm_region imx8m_mem_map[] = {
94 .attrs = PTE_BLOCK_MEMTYPE(MT_NORMAL) |
101 .attrs = PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
102 PTE_BLOCK_NON_SHARE |
103 PTE_BLOCK_PXN | PTE_BLOCK_UXN
109 .attrs = PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
110 PTE_BLOCK_NON_SHARE |
111 PTE_BLOCK_PXN | PTE_BLOCK_UXN
117 .attrs = PTE_BLOCK_MEMTYPE(MT_NORMAL) |
118 PTE_BLOCK_OUTER_SHARE
123 .size = 0x3f500000UL,
124 .attrs = PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
125 PTE_BLOCK_NON_SHARE |
126 PTE_BLOCK_PXN | PTE_BLOCK_UXN
129 .virt = 0x40000000UL,
130 .phys = 0x40000000UL,
131 .size = PHYS_SDRAM_SIZE,
132 .attrs = PTE_BLOCK_MEMTYPE(MT_NORMAL) |
133 PTE_BLOCK_OUTER_SHARE
134 #ifdef PHYS_SDRAM_2_SIZE
137 .virt = 0x100000000UL,
138 .phys = 0x100000000UL,
139 .size = PHYS_SDRAM_2_SIZE,
140 .attrs = PTE_BLOCK_MEMTYPE(MT_NORMAL) |
141 PTE_BLOCK_OUTER_SHARE
144 /* List terminator */
149 struct mm_region *mem_map = imx8m_mem_map;
151 void enable_caches(void)
154 * If OPTEE runs, remove OPTEE memory from MMU table to
155 * avoid speculative prefetch. OPTEE runs at the top of
156 * the first memory bank
159 imx8m_mem_map[5].size -= rom_pointer[1];
165 static u32 get_cpu_variant_type(u32 type)
167 struct ocotp_regs *ocotp = (struct ocotp_regs *)OCOTP_BASE_ADDR;
168 struct fuse_bank *bank = &ocotp->bank[1];
169 struct fuse_bank1_regs *fuse =
170 (struct fuse_bank1_regs *)bank->fuse_regs;
172 u32 value = readl(&fuse->tester4);
174 if (type == MXC_CPU_IMX8MQ) {
175 if ((value & 0x3) == 0x2)
176 return MXC_CPU_IMX8MD;
177 else if (value & 0x200000)
178 return MXC_CPU_IMX8MQL;
180 } else if (type == MXC_CPU_IMX8MM) {
181 switch (value & 0x3) {
183 if (value & 0x1c0000)
184 return MXC_CPU_IMX8MMDL;
186 return MXC_CPU_IMX8MMD;
188 if (value & 0x1c0000)
189 return MXC_CPU_IMX8MMSL;
191 return MXC_CPU_IMX8MMS;
193 if (value & 0x1c0000)
194 return MXC_CPU_IMX8MML;
197 } else if (type == MXC_CPU_IMX8MN) {
198 switch (value & 0x3) {
200 if (value & 0x1000000)
201 return MXC_CPU_IMX8MNDL;
203 return MXC_CPU_IMX8MND;
205 if (value & 0x1000000)
206 return MXC_CPU_IMX8MNSL;
208 return MXC_CPU_IMX8MNS;
210 if (value & 0x1000000)
211 return MXC_CPU_IMX8MNL;
214 } else if (type == MXC_CPU_IMX8MP) {
215 u32 value0 = readl(&fuse->tester3);
218 if ((value0 & 0xc0000) == 0x80000)
219 return MXC_CPU_IMX8MPD;
222 if ((value0 & 0x43000000) == 0x43000000)
226 if ((value & 0x8) == 0x8)
230 if ((value & 0x3) == 0x3)
235 return MXC_CPU_IMX8MPL;
237 return MXC_CPU_IMX8MP5;
239 return MXC_CPU_IMX8MP6;
241 return MXC_CPU_IMX8MP7;
251 u32 get_cpu_rev(void)
253 struct anamix_pll *ana_pll = (struct anamix_pll *)ANATOP_BASE_ADDR;
254 u32 reg = readl(&ana_pll->digprog);
255 u32 type = (reg >> 16) & 0xff;
256 u32 major_low = (reg >> 8) & 0xff;
262 if (major_low == 0x43) {
263 type = get_cpu_variant_type(MXC_CPU_IMX8MP);
264 } else if (major_low == 0x42) {
266 type = get_cpu_variant_type(MXC_CPU_IMX8MN);
267 } else if (major_low == 0x41) {
268 type = get_cpu_variant_type(MXC_CPU_IMX8MM);
270 if (reg == CHIP_REV_1_0) {
272 * For B0 chip, the DIGPROG is not updated,
273 * it is still TO1.0. we have to check ROM
274 * version or OCOTP_READ_FUSE_DATA.
275 * 0xff0055aa is magic number for B1.
277 if (readl((void __iomem *)(OCOTP_BASE_ADDR + 0x40)) == 0xff0055aa) {
281 readl((void __iomem *)ROM_VERSION_A0);
282 if (rom_version != CHIP_REV_1_0) {
283 rom_version = readl((void __iomem *)ROM_VERSION_B0);
285 if (rom_version == CHIP_REV_2_0)
291 type = get_cpu_variant_type(type);
294 return (type << 12) | reg;
297 static void imx_set_wdog_powerdown(bool enable)
299 struct wdog_regs *wdog1 = (struct wdog_regs *)WDOG1_BASE_ADDR;
300 struct wdog_regs *wdog2 = (struct wdog_regs *)WDOG2_BASE_ADDR;
301 struct wdog_regs *wdog3 = (struct wdog_regs *)WDOG3_BASE_ADDR;
303 /* Write to the PDE (Power Down Enable) bit */
304 writew(enable, &wdog1->wmcr);
305 writew(enable, &wdog2->wmcr);
306 writew(enable, &wdog3->wmcr);
309 int arch_cpu_init_dm(void)
314 if (CONFIG_IS_ENABLED(CLK)) {
315 ret = uclass_get_device_by_name(UCLASS_CLK,
316 "clock-controller@30380000",
319 printf("Failed to find clock node. Check device tree\n");
327 int arch_cpu_init(void)
329 struct ocotp_regs *ocotp = (struct ocotp_regs *)OCOTP_BASE_ADDR;
331 * ROM might disable clock for SCTR,
332 * enable the clock before timer_init.
334 if (IS_ENABLED(CONFIG_SPL_BUILD))
335 clock_enable(CCGR_SCTR, 1);
337 * Init timer at very early state, because sscg pll setting
342 if (IS_ENABLED(CONFIG_SPL_BUILD)) {
344 imx_set_wdog_powerdown(false);
348 clock_enable(CCGR_OCOTP, 1);
349 if (readl(&ocotp->ctrl) & 0x200)
350 writel(0x200, &ocotp->ctrl_clr);
356 #if defined(CONFIG_IMX8MN) || defined(CONFIG_IMX8MP)
357 struct rom_api *g_rom_api = (struct rom_api *)0x980;
359 enum boot_device get_boot_device(void)
361 volatile gd_t *pgd = gd;
366 enum boot_device boot_dev = SD1_BOOT;
368 ret = g_rom_api->query_boot_infor(QUERY_BT_DEV, &boot,
369 ((uintptr_t)&boot) ^ QUERY_BT_DEV);
372 if (ret != ROM_API_OKAY) {
373 puts("ROMAPI: failure at query_boot_info\n");
377 boot_type = boot >> 16;
378 boot_instance = (boot >> 8) & 0xff;
382 boot_dev = boot_instance + SD1_BOOT;
384 case BT_DEV_TYPE_MMC:
385 boot_dev = boot_instance + MMC1_BOOT;
387 case BT_DEV_TYPE_NAND:
388 boot_dev = NAND_BOOT;
390 case BT_DEV_TYPE_FLEXSPINOR:
391 boot_dev = QSPI_BOOT;
393 case BT_DEV_TYPE_USB:
404 bool is_usb_boot(void)
406 return get_boot_device() == USB_BOOT;
409 #ifdef CONFIG_OF_SYSTEM_SETUP
410 int ft_system_setup(void *blob, bd_t *bd)
416 /* Disable the CPU idle for A0 chip since the HW does not support it */
417 if (is_soc_rev(CHIP_REV_1_0)) {
418 static const char * const nodes_path[] = {
425 for (i = 0; i < ARRAY_SIZE(nodes_path); i++) {
426 nodeoff = fdt_path_offset(blob, nodes_path[i]);
428 continue; /* Not found, skip it */
430 debug("Found %s node\n", nodes_path[i]);
432 rc = fdt_delprop(blob, nodeoff, "cpu-idle-states");
433 if (rc == -FDT_ERR_NOTFOUND)
436 printf("Unable to update property %s:%s, err=%s\n",
437 nodes_path[i], "status", fdt_strerror(rc));
441 debug("Remove %s:%s\n", nodes_path[i],
450 #if !CONFIG_IS_ENABLED(SYSRESET)
451 void reset_cpu(ulong addr)
453 struct watchdog_regs *wdog = (struct watchdog_regs *)WDOG1_BASE_ADDR;
455 /* Clear WDA to trigger WDOG_B immediately */
456 writew((SET_WCR_WT(1) | WCR_WDT | WCR_WDE | WCR_SRS), &wdog->wcr);
460 * spin for .5 seconds before reset
466 #if defined(CONFIG_ARCH_MISC_INIT)
467 static void acquire_buildinfo(void)
470 struct arm_smccc_res res;
472 /* Get ARM Trusted Firmware commit id */
473 arm_smccc_smc(IMX_SIP_BUILDINFO, IMX_SIP_BUILDINFO_GET_COMMITHASH,
474 0, 0 , 0, 0, 0, 0, &res);
476 if (atf_commit == 0xffffffff) {
477 debug("ATF does not support build info\n");
478 atf_commit = 0x30; /* Display 0, 0 ascii is 0x30 */
481 printf("\n BuildInfo:\n - ATF %s\n\n", (char *)&atf_commit);
484 int arch_misc_init(void)
492 void imx_tmu_arch_init(void *reg_base)
494 if (is_imx8mm() || is_imx8mn()) {
495 /* Load TCALIV and TASR from fuses */
496 struct ocotp_regs *ocotp =
497 (struct ocotp_regs *)OCOTP_BASE_ADDR;
498 struct fuse_bank *bank = &ocotp->bank[3];
499 struct fuse_bank3_regs *fuse =
500 (struct fuse_bank3_regs *)bank->fuse_regs;
502 u32 tca_rt, tca_hr, tca_en;
503 u32 buf_vref, buf_slope;
505 tca_rt = fuse->ana0 & 0xFF;
506 tca_hr = (fuse->ana0 & 0xFF00) >> 8;
507 tca_en = (fuse->ana0 & 0x2000000) >> 25;
509 buf_vref = (fuse->ana0 & 0x1F00000) >> 20;
510 buf_slope = (fuse->ana0 & 0xF0000) >> 16;
512 writel(buf_vref | (buf_slope << 16), (ulong)reg_base + 0x28);
513 writel((tca_en << 31) | (tca_hr << 16) | tca_rt,
514 (ulong)reg_base + 0x30);
517 /* Load TCALIV0/1/m40 and TRIM from fuses */
518 struct ocotp_regs *ocotp = (struct ocotp_regs *)OCOTP_BASE_ADDR;
519 struct fuse_bank *bank = &ocotp->bank[38];
520 struct fuse_bank38_regs *fuse =
521 (struct fuse_bank38_regs *)bank->fuse_regs;
522 struct fuse_bank *bank2 = &ocotp->bank[39];
523 struct fuse_bank39_regs *fuse2 =
524 (struct fuse_bank39_regs *)bank2->fuse_regs;
525 u32 buf_vref, buf_slope, bjt_cur, vlsb, bgr;
527 u32 tca40[2], tca25[2], tca105[2];
529 /* For blank sample */
530 if (!fuse->ana_trim2 && !fuse->ana_trim3 &&
531 !fuse->ana_trim4 && !fuse2->ana_trim5) {
532 /* Use a default 25C binary codes */
535 writel(tca25[0], (ulong)reg_base + 0x30);
536 writel(tca25[1], (ulong)reg_base + 0x34);
540 buf_vref = (fuse->ana_trim2 & 0xc0) >> 6;
541 buf_slope = (fuse->ana_trim2 & 0xF00) >> 8;
542 bjt_cur = (fuse->ana_trim2 & 0xF000) >> 12;
543 bgr = (fuse->ana_trim2 & 0xF0000) >> 16;
544 vlsb = (fuse->ana_trim2 & 0xF00000) >> 20;
545 writel(buf_vref | (buf_slope << 16), (ulong)reg_base + 0x28);
547 reg = (bgr << 28) | (bjt_cur << 20) | (vlsb << 12) | (1 << 7);
548 writel(reg, (ulong)reg_base + 0x3c);
550 tca40[0] = (fuse->ana_trim3 & 0xFFF0000) >> 16;
551 tca25[0] = (fuse->ana_trim3 & 0xF0000000) >> 28;
552 tca25[0] |= ((fuse->ana_trim4 & 0xFF) << 4);
553 tca105[0] = (fuse->ana_trim4 & 0xFFF00) >> 8;
554 tca40[1] = (fuse->ana_trim4 & 0xFFF00000) >> 20;
555 tca25[1] = fuse2->ana_trim5 & 0xFFF;
556 tca105[1] = (fuse2->ana_trim5 & 0xFFF000) >> 12;
558 /* use 25c for 1p calibration */
559 writel(tca25[0] | (tca105[0] << 16), (ulong)reg_base + 0x30);
560 writel(tca25[1] | (tca105[1] << 16), (ulong)reg_base + 0x34);
561 writel(tca40[0] | (tca40[1] << 16), (ulong)reg_base + 0x38);
565 #if defined(CONFIG_SPL_BUILD)
566 #if defined(CONFIG_IMX8MQ) || defined(CONFIG_IMX8MM) || defined(CONFIG_IMX8MN)
567 bool serror_need_skip = true;
569 void do_error(struct pt_regs *pt_regs, unsigned int esr)
572 * If stack is still in ROM reserved OCRAM not switch to SPL,
573 * it is the ROM SError
577 asm volatile("mov %0, sp" : "=r"(sp) : );
579 if (serror_need_skip && sp < 0x910000 && sp >= 0x900000) {
580 /* Check for ERR050342, imx8mq HDCP enabled parts */
581 if (is_imx8mq() && !(readl(OCOTP_BASE_ADDR + 0x450) & 0x08000000)) {
582 serror_need_skip = false;
583 return; /* Do nothing skip the SError in ROM */
586 /* Check for ERR050350, field return mode for imx8mq, mm and mn */
587 if (readl(OCOTP_BASE_ADDR + 0x630) & 0x1) {
588 serror_need_skip = false;
589 return; /* Do nothing skip the SError in ROM */
594 printf("\"Error\" handler, esr 0x%08x\n", esr);
596 panic("Resetting CPU ...\n");