1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright 2017-2019 NXP
10 #include <asm/arch/imx-regs.h>
12 #include <asm/arch/clock.h>
13 #include <asm/arch/sys_proto.h>
14 #include <asm/mach-imx/hab.h>
15 #include <asm/mach-imx/boot_mode.h>
16 #include <asm/mach-imx/syscounter.h>
17 #include <asm/armv8/mmu.h>
18 #include <dm/uclass.h>
20 #include <fdt_support.h>
24 DECLARE_GLOBAL_DATA_PTR;
26 #if defined(CONFIG_IMX_HAB)
27 struct imx_sec_config_fuse_t const imx_sec_config_fuse = {
35 #ifdef CONFIG_SPL_BUILD
36 struct sctr_regs *sctr = (struct sctr_regs *)SYSCNT_CTRL_BASE_ADDR;
37 unsigned long freq = readl(&sctr->cntfid0);
39 /* Update with accurate clock frequency */
40 asm volatile("msr cntfrq_el0, %0" : : "r" (freq) : "memory");
42 clrsetbits_le32(&sctr->cntcr, SC_CNTCR_FREQ0 | SC_CNTCR_FREQ1,
43 SC_CNTCR_FREQ0 | SC_CNTCR_ENABLE | SC_CNTCR_HDBG);
52 void enable_tzc380(void)
54 struct iomuxc_gpr_base_regs *gpr =
55 (struct iomuxc_gpr_base_regs *)IOMUXC_GPR_BASE_ADDR;
57 /* Enable TZASC and lock setting */
58 setbits_le32(&gpr->gpr[10], GPR_TZASC_EN);
59 setbits_le32(&gpr->gpr[10], GPR_TZASC_EN_LOCK);
60 if (is_imx8mm() || is_imx8mn() || is_imx8mp())
61 setbits_le32(&gpr->gpr[10], BIT(1));
63 * set Region 0 attribute to allow secure and non-secure
64 * read/write permission. Found some masters like usb dwc3
65 * controllers can't work with secure memory.
67 writel(0xf0000000, TZASC_BASE_ADDR + 0x108);
70 void set_wdog_reset(struct wdog_regs *wdog)
73 * Output WDOG_B signal to reset external pmic or POR_B decided by
74 * the board design. Without external reset, the peripherals/DDR/
75 * PMIC are not reset, that may cause system working abnormal.
76 * WDZST bit is write-once only bit. Align this bit in kernel,
77 * otherwise kernel code will have no chance to set this bit.
79 setbits_le16(&wdog->wcr, WDOG_WDT_MASK | WDOG_WDZST_MASK);
82 static struct mm_region imx8m_mem_map[] = {
88 .attrs = PTE_BLOCK_MEMTYPE(MT_NORMAL) |
95 .attrs = PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
97 PTE_BLOCK_PXN | PTE_BLOCK_UXN
103 .attrs = PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
104 PTE_BLOCK_NON_SHARE |
105 PTE_BLOCK_PXN | PTE_BLOCK_UXN
111 .attrs = PTE_BLOCK_MEMTYPE(MT_NORMAL) |
112 PTE_BLOCK_OUTER_SHARE
117 .size = 0x3f500000UL,
118 .attrs = PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
119 PTE_BLOCK_NON_SHARE |
120 PTE_BLOCK_PXN | PTE_BLOCK_UXN
123 .virt = 0x40000000UL,
124 .phys = 0x40000000UL,
125 .size = PHYS_SDRAM_SIZE,
126 .attrs = PTE_BLOCK_MEMTYPE(MT_NORMAL) |
127 PTE_BLOCK_OUTER_SHARE
128 #ifdef PHYS_SDRAM_2_SIZE
131 .virt = 0x100000000UL,
132 .phys = 0x100000000UL,
133 .size = PHYS_SDRAM_2_SIZE,
134 .attrs = PTE_BLOCK_MEMTYPE(MT_NORMAL) |
135 PTE_BLOCK_OUTER_SHARE
138 /* List terminator */
143 struct mm_region *mem_map = imx8m_mem_map;
145 void enable_caches(void)
148 * If OPTEE runs, remove OPTEE memory from MMU table to
149 * avoid speculative prefetch. OPTEE runs at the top of
150 * the first memory bank
153 imx8m_mem_map[5].size -= rom_pointer[1];
159 static u32 get_cpu_variant_type(u32 type)
161 struct ocotp_regs *ocotp = (struct ocotp_regs *)OCOTP_BASE_ADDR;
162 struct fuse_bank *bank = &ocotp->bank[1];
163 struct fuse_bank1_regs *fuse =
164 (struct fuse_bank1_regs *)bank->fuse_regs;
166 u32 value = readl(&fuse->tester4);
168 if (type == MXC_CPU_IMX8MQ) {
169 if ((value & 0x3) == 0x2)
170 return MXC_CPU_IMX8MD;
171 else if (value & 0x200000)
172 return MXC_CPU_IMX8MQL;
174 } else if (type == MXC_CPU_IMX8MM) {
175 switch (value & 0x3) {
177 if (value & 0x1c0000)
178 return MXC_CPU_IMX8MMDL;
180 return MXC_CPU_IMX8MMD;
182 if (value & 0x1c0000)
183 return MXC_CPU_IMX8MMSL;
185 return MXC_CPU_IMX8MMS;
187 if (value & 0x1c0000)
188 return MXC_CPU_IMX8MML;
191 } else if (type == MXC_CPU_IMX8MN) {
192 switch (value & 0x3) {
194 if (value & 0x1000000)
195 return MXC_CPU_IMX8MNDL;
197 return MXC_CPU_IMX8MND;
199 if (value & 0x1000000)
200 return MXC_CPU_IMX8MNSL;
202 return MXC_CPU_IMX8MNS;
204 if (value & 0x1000000)
205 return MXC_CPU_IMX8MNL;
213 u32 get_cpu_rev(void)
215 struct anamix_pll *ana_pll = (struct anamix_pll *)ANATOP_BASE_ADDR;
216 u32 reg = readl(&ana_pll->digprog);
217 u32 type = (reg >> 16) & 0xff;
218 u32 major_low = (reg >> 8) & 0xff;
224 if (major_low == 0x43) {
225 return (MXC_CPU_IMX8MP << 12) | reg;
226 } else if (major_low == 0x42) {
228 type = get_cpu_variant_type(MXC_CPU_IMX8MN);
229 } else if (major_low == 0x41) {
230 type = get_cpu_variant_type(MXC_CPU_IMX8MM);
232 if (reg == CHIP_REV_1_0) {
234 * For B0 chip, the DIGPROG is not updated,
235 * it is still TO1.0. we have to check ROM
236 * version or OCOTP_READ_FUSE_DATA.
237 * 0xff0055aa is magic number for B1.
239 if (readl((void __iomem *)(OCOTP_BASE_ADDR + 0x40)) == 0xff0055aa) {
243 readl((void __iomem *)ROM_VERSION_A0);
244 if (rom_version != CHIP_REV_1_0) {
245 rom_version = readl((void __iomem *)ROM_VERSION_B0);
247 if (rom_version == CHIP_REV_2_0)
253 type = get_cpu_variant_type(type);
256 return (type << 12) | reg;
259 static void imx_set_wdog_powerdown(bool enable)
261 struct wdog_regs *wdog1 = (struct wdog_regs *)WDOG1_BASE_ADDR;
262 struct wdog_regs *wdog2 = (struct wdog_regs *)WDOG2_BASE_ADDR;
263 struct wdog_regs *wdog3 = (struct wdog_regs *)WDOG3_BASE_ADDR;
265 /* Write to the PDE (Power Down Enable) bit */
266 writew(enable, &wdog1->wmcr);
267 writew(enable, &wdog2->wmcr);
268 writew(enable, &wdog3->wmcr);
271 int arch_cpu_init_dm(void)
276 if (CONFIG_IS_ENABLED(CLK)) {
277 ret = uclass_get_device_by_name(UCLASS_CLK,
278 "clock-controller@30380000",
281 printf("Failed to find clock node. Check device tree\n");
289 int arch_cpu_init(void)
291 struct ocotp_regs *ocotp = (struct ocotp_regs *)OCOTP_BASE_ADDR;
293 * ROM might disable clock for SCTR,
294 * enable the clock before timer_init.
296 if (IS_ENABLED(CONFIG_SPL_BUILD))
297 clock_enable(CCGR_SCTR, 1);
299 * Init timer at very early state, because sscg pll setting
304 if (IS_ENABLED(CONFIG_SPL_BUILD)) {
306 imx_set_wdog_powerdown(false);
310 clock_enable(CCGR_OCOTP, 1);
311 if (readl(&ocotp->ctrl) & 0x200)
312 writel(0x200, &ocotp->ctrl_clr);
318 #if defined(CONFIG_IMX8MN) || defined(CONFIG_IMX8MP)
319 struct rom_api *g_rom_api = (struct rom_api *)0x980;
321 enum boot_device get_boot_device(void)
323 volatile gd_t *pgd = gd;
328 enum boot_device boot_dev = SD1_BOOT;
330 ret = g_rom_api->query_boot_infor(QUERY_BT_DEV, &boot,
331 ((uintptr_t)&boot) ^ QUERY_BT_DEV);
334 if (ret != ROM_API_OKAY) {
335 puts("ROMAPI: failure at query_boot_info\n");
339 boot_type = boot >> 16;
340 boot_instance = (boot >> 8) & 0xff;
344 boot_dev = boot_instance + SD1_BOOT;
346 case BT_DEV_TYPE_MMC:
347 boot_dev = boot_instance + MMC1_BOOT;
349 case BT_DEV_TYPE_NAND:
350 boot_dev = NAND_BOOT;
352 case BT_DEV_TYPE_FLEXSPINOR:
353 boot_dev = QSPI_BOOT;
355 case BT_DEV_TYPE_USB:
366 bool is_usb_boot(void)
368 return get_boot_device() == USB_BOOT;
371 #ifdef CONFIG_OF_SYSTEM_SETUP
372 int ft_system_setup(void *blob, bd_t *bd)
378 /* Disable the CPU idle for A0 chip since the HW does not support it */
379 if (is_soc_rev(CHIP_REV_1_0)) {
380 static const char * const nodes_path[] = {
387 for (i = 0; i < ARRAY_SIZE(nodes_path); i++) {
388 nodeoff = fdt_path_offset(blob, nodes_path[i]);
390 continue; /* Not found, skip it */
392 debug("Found %s node\n", nodes_path[i]);
394 rc = fdt_delprop(blob, nodeoff, "cpu-idle-states");
395 if (rc == -FDT_ERR_NOTFOUND)
398 printf("Unable to update property %s:%s, err=%s\n",
399 nodes_path[i], "status", fdt_strerror(rc));
403 debug("Remove %s:%s\n", nodes_path[i],
412 #if !CONFIG_IS_ENABLED(SYSRESET)
413 void reset_cpu(ulong addr)
415 struct watchdog_regs *wdog = (struct watchdog_regs *)WDOG1_BASE_ADDR;
417 /* Clear WDA to trigger WDOG_B immediately */
418 writew((SET_WCR_WT(1) | WCR_WDT | WCR_WDE | WCR_SRS), &wdog->wcr);
422 * spin for .5 seconds before reset
428 #if defined(CONFIG_ARCH_MISC_INIT)
429 static void acquire_buildinfo(void)
433 /* Get ARM Trusted Firmware commit id */
434 atf_commit = call_imx_sip(IMX_SIP_BUILDINFO,
435 IMX_SIP_BUILDINFO_GET_COMMITHASH, 0, 0, 0);
436 if (atf_commit == 0xffffffff) {
437 debug("ATF does not support build info\n");
438 atf_commit = 0x30; /* Display 0, 0 ascii is 0x30 */
441 printf("\n BuildInfo:\n - ATF %s\n\n", (char *)&atf_commit);
444 int arch_misc_init(void)
452 void imx_tmu_arch_init(void *reg_base)
454 if (is_imx8mm() || is_imx8mn()) {
455 /* Load TCALIV and TASR from fuses */
456 struct ocotp_regs *ocotp =
457 (struct ocotp_regs *)OCOTP_BASE_ADDR;
458 struct fuse_bank *bank = &ocotp->bank[3];
459 struct fuse_bank3_regs *fuse =
460 (struct fuse_bank3_regs *)bank->fuse_regs;
462 u32 tca_rt, tca_hr, tca_en;
463 u32 buf_vref, buf_slope;
465 tca_rt = fuse->ana0 & 0xFF;
466 tca_hr = (fuse->ana0 & 0xFF00) >> 8;
467 tca_en = (fuse->ana0 & 0x2000000) >> 25;
469 buf_vref = (fuse->ana0 & 0x1F00000) >> 20;
470 buf_slope = (fuse->ana0 & 0xF0000) >> 16;
472 writel(buf_vref | (buf_slope << 16), (ulong)reg_base + 0x28);
473 writel((tca_en << 31) | (tca_hr << 16) | tca_rt,
474 (ulong)reg_base + 0x30);
477 /* Load TCALIV0/1/m40 and TRIM from fuses */
478 struct ocotp_regs *ocotp = (struct ocotp_regs *)OCOTP_BASE_ADDR;
479 struct fuse_bank *bank = &ocotp->bank[38];
480 struct fuse_bank38_regs *fuse =
481 (struct fuse_bank38_regs *)bank->fuse_regs;
482 struct fuse_bank *bank2 = &ocotp->bank[39];
483 struct fuse_bank39_regs *fuse2 =
484 (struct fuse_bank39_regs *)bank2->fuse_regs;
485 u32 buf_vref, buf_slope, bjt_cur, vlsb, bgr;
487 u32 tca40[2], tca25[2], tca105[2];
489 /* For blank sample */
490 if (!fuse->ana_trim2 && !fuse->ana_trim3 &&
491 !fuse->ana_trim4 && !fuse2->ana_trim5) {
492 /* Use a default 25C binary codes */
495 writel(tca25[0], (ulong)reg_base + 0x30);
496 writel(tca25[1], (ulong)reg_base + 0x34);
500 buf_vref = (fuse->ana_trim2 & 0xc0) >> 6;
501 buf_slope = (fuse->ana_trim2 & 0xF00) >> 8;
502 bjt_cur = (fuse->ana_trim2 & 0xF000) >> 12;
503 bgr = (fuse->ana_trim2 & 0xF0000) >> 16;
504 vlsb = (fuse->ana_trim2 & 0xF00000) >> 20;
505 writel(buf_vref | (buf_slope << 16), (ulong)reg_base + 0x28);
507 reg = (bgr << 28) | (bjt_cur << 20) | (vlsb << 12) | (1 << 7);
508 writel(reg, (ulong)reg_base + 0x3c);
510 tca40[0] = (fuse->ana_trim3 & 0xFFF0000) >> 16;
511 tca25[0] = (fuse->ana_trim3 & 0xF0000000) >> 28;
512 tca25[0] |= ((fuse->ana_trim4 & 0xFF) << 4);
513 tca105[0] = (fuse->ana_trim4 & 0xFFF00) >> 8;
514 tca40[1] = (fuse->ana_trim4 & 0xFFF00000) >> 20;
515 tca25[1] = fuse2->ana_trim5 & 0xFFF;
516 tca105[1] = (fuse2->ana_trim5 & 0xFFF000) >> 12;
518 /* use 25c for 1p calibration */
519 writel(tca25[0] | (tca105[0] << 16), (ulong)reg_base + 0x30);
520 writel(tca25[1] | (tca105[1] << 16), (ulong)reg_base + 0x34);
521 writel(tca40[0] | (tca40[1] << 16), (ulong)reg_base + 0x38);