imx8mp: Add fused parts support
[platform/kernel/u-boot.git] / arch / arm / mach-imx / imx8m / soc.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright 2017-2019 NXP
4  *
5  * Peng Fan <peng.fan@nxp.com>
6  */
7
8 #include <common.h>
9 #include <cpu_func.h>
10 #include <init.h>
11 #include <log.h>
12 #include <asm/arch/imx-regs.h>
13 #include <asm/io.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>
23 #include <errno.h>
24 #include <fdt_support.h>
25 #include <fsl_wdog.h>
26 #include <imx_sip.h>
27 #include <linux/arm-smccc.h>
28 #include <linux/bitops.h>
29
30 DECLARE_GLOBAL_DATA_PTR;
31
32 #if defined(CONFIG_IMX_HAB)
33 struct imx_sec_config_fuse_t const imx_sec_config_fuse = {
34         .bank = 1,
35         .word = 3,
36 };
37 #endif
38
39 int timer_init(void)
40 {
41 #ifdef CONFIG_SPL_BUILD
42         struct sctr_regs *sctr = (struct sctr_regs *)SYSCNT_CTRL_BASE_ADDR;
43         unsigned long freq = readl(&sctr->cntfid0);
44
45         /* Update with accurate clock frequency */
46         asm volatile("msr cntfrq_el0, %0" : : "r" (freq) : "memory");
47
48         clrsetbits_le32(&sctr->cntcr, SC_CNTCR_FREQ0 | SC_CNTCR_FREQ1,
49                         SC_CNTCR_FREQ0 | SC_CNTCR_ENABLE | SC_CNTCR_HDBG);
50 #endif
51
52         gd->arch.tbl = 0;
53         gd->arch.tbu = 0;
54
55         return 0;
56 }
57
58 void enable_tzc380(void)
59 {
60         struct iomuxc_gpr_base_regs *gpr =
61                 (struct iomuxc_gpr_base_regs *)IOMUXC_GPR_BASE_ADDR;
62
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));
68         /*
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.
72          */
73         writel(0xf0000000, TZASC_BASE_ADDR + 0x108);
74 }
75
76 void set_wdog_reset(struct wdog_regs *wdog)
77 {
78         /*
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.
84          */
85         setbits_le16(&wdog->wcr, WDOG_WDT_MASK | WDOG_WDZST_MASK);
86 }
87
88 static struct mm_region imx8m_mem_map[] = {
89         {
90                 /* ROM */
91                 .virt = 0x0UL,
92                 .phys = 0x0UL,
93                 .size = 0x100000UL,
94                 .attrs = PTE_BLOCK_MEMTYPE(MT_NORMAL) |
95                          PTE_BLOCK_OUTER_SHARE
96         }, {
97                 /* CAAM */
98                 .virt = 0x100000UL,
99                 .phys = 0x100000UL,
100                 .size = 0x8000UL,
101                 .attrs = PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
102                          PTE_BLOCK_NON_SHARE |
103                          PTE_BLOCK_PXN | PTE_BLOCK_UXN
104         }, {
105                 /* TCM */
106                 .virt = 0x7C0000UL,
107                 .phys = 0x7C0000UL,
108                 .size = 0x80000UL,
109                 .attrs = PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
110                          PTE_BLOCK_NON_SHARE |
111                          PTE_BLOCK_PXN | PTE_BLOCK_UXN
112         }, {
113                 /* OCRAM */
114                 .virt = 0x900000UL,
115                 .phys = 0x900000UL,
116                 .size = 0x200000UL,
117                 .attrs = PTE_BLOCK_MEMTYPE(MT_NORMAL) |
118                          PTE_BLOCK_OUTER_SHARE
119         }, {
120                 /* AIPS */
121                 .virt = 0xB00000UL,
122                 .phys = 0xB00000UL,
123                 .size = 0x3f500000UL,
124                 .attrs = PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
125                          PTE_BLOCK_NON_SHARE |
126                          PTE_BLOCK_PXN | PTE_BLOCK_UXN
127         }, {
128                 /* DRAM1 */
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
135         }, {
136                 /* DRAM2 */
137                 .virt = 0x100000000UL,
138                 .phys = 0x100000000UL,
139                 .size = PHYS_SDRAM_2_SIZE,
140                 .attrs = PTE_BLOCK_MEMTYPE(MT_NORMAL) |
141                          PTE_BLOCK_OUTER_SHARE
142 #endif
143         }, {
144                 /* List terminator */
145                 0,
146         }
147 };
148
149 struct mm_region *mem_map = imx8m_mem_map;
150
151 void enable_caches(void)
152 {
153         /*
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
157          */
158         if (rom_pointer[1])
159                 imx8m_mem_map[5].size -= rom_pointer[1];
160
161         icache_enable();
162         dcache_enable();
163 }
164
165 static u32 get_cpu_variant_type(u32 type)
166 {
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;
171
172         u32 value = readl(&fuse->tester4);
173
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;
179
180         } else if (type == MXC_CPU_IMX8MM) {
181                 switch (value & 0x3) {
182                 case 2:
183                         if (value & 0x1c0000)
184                                 return MXC_CPU_IMX8MMDL;
185                         else
186                                 return MXC_CPU_IMX8MMD;
187                 case 3:
188                         if (value & 0x1c0000)
189                                 return MXC_CPU_IMX8MMSL;
190                         else
191                                 return MXC_CPU_IMX8MMS;
192                 default:
193                         if (value & 0x1c0000)
194                                 return MXC_CPU_IMX8MML;
195                         break;
196                 }
197         } else if (type == MXC_CPU_IMX8MN) {
198                 switch (value & 0x3) {
199                 case 2:
200                         if (value & 0x1000000)
201                                 return MXC_CPU_IMX8MNDL;
202                         else
203                                 return MXC_CPU_IMX8MND;
204                 case 3:
205                         if (value & 0x1000000)
206                                 return MXC_CPU_IMX8MNSL;
207                         else
208                                 return MXC_CPU_IMX8MNS;
209                 default:
210                         if (value & 0x1000000)
211                                 return MXC_CPU_IMX8MNL;
212                         break;
213                 }
214         } else if (type == MXC_CPU_IMX8MP) {
215                 u32 value0 = readl(&fuse->tester3);
216                 u32 flag = 0;
217
218                 if ((value0 & 0xc0000) == 0x80000)
219                         return MXC_CPU_IMX8MPD;
220
221                         /* vpu disabled */
222                 if ((value0 & 0x43000000) == 0x43000000)
223                         flag = 1;
224
225                 /* npu disabled*/
226                 if ((value & 0x8) == 0x8)
227                         flag |= (1 << 1);
228
229                 /* isp disabled */
230                 if ((value & 0x3) == 0x3)
231                         flag |= (1 << 2);
232
233                 switch (flag) {
234                 case 7:
235                         return MXC_CPU_IMX8MPL;
236                 case 6:
237                         return MXC_CPU_IMX8MP5;
238                 case 2:
239                         return MXC_CPU_IMX8MP6;
240                 case 1:
241                         return MXC_CPU_IMX8MP7;
242                 default:
243                         break;
244                 }
245
246         }
247
248         return type;
249 }
250
251 u32 get_cpu_rev(void)
252 {
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;
257         u32 rom_version;
258
259         reg &= 0xff;
260
261         /* iMX8MP */
262         if (major_low == 0x43) {
263                 type = get_cpu_variant_type(MXC_CPU_IMX8MP);
264         } else if (major_low == 0x42) {
265                 /* iMX8MN */
266                 type = get_cpu_variant_type(MXC_CPU_IMX8MN);
267         } else if (major_low == 0x41) {
268                 type = get_cpu_variant_type(MXC_CPU_IMX8MM);
269         } else {
270                 if (reg == CHIP_REV_1_0) {
271                         /*
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.
276                          */
277                         if (readl((void __iomem *)(OCOTP_BASE_ADDR + 0x40)) == 0xff0055aa) {
278                                 reg = CHIP_REV_2_1;
279                         } else {
280                                 rom_version =
281                                         readl((void __iomem *)ROM_VERSION_A0);
282                                 if (rom_version != CHIP_REV_1_0) {
283                                         rom_version = readl((void __iomem *)ROM_VERSION_B0);
284                                         rom_version &= 0xff;
285                                         if (rom_version == CHIP_REV_2_0)
286                                                 reg = CHIP_REV_2_0;
287                                 }
288                         }
289                 }
290
291                 type = get_cpu_variant_type(type);
292         }
293
294         return (type << 12) | reg;
295 }
296
297 static void imx_set_wdog_powerdown(bool enable)
298 {
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;
302
303         /* Write to the PDE (Power Down Enable) bit */
304         writew(enable, &wdog1->wmcr);
305         writew(enable, &wdog2->wmcr);
306         writew(enable, &wdog3->wmcr);
307 }
308
309 int arch_cpu_init_dm(void)
310 {
311         struct udevice *dev;
312         int ret;
313
314         if (CONFIG_IS_ENABLED(CLK)) {
315                 ret = uclass_get_device_by_name(UCLASS_CLK,
316                                                 "clock-controller@30380000",
317                                                 &dev);
318                 if (ret < 0) {
319                         printf("Failed to find clock node. Check device tree\n");
320                         return ret;
321                 }
322         }
323
324         return 0;
325 }
326
327 int arch_cpu_init(void)
328 {
329         struct ocotp_regs *ocotp = (struct ocotp_regs *)OCOTP_BASE_ADDR;
330         /*
331          * ROM might disable clock for SCTR,
332          * enable the clock before timer_init.
333          */
334         if (IS_ENABLED(CONFIG_SPL_BUILD))
335                 clock_enable(CCGR_SCTR, 1);
336         /*
337          * Init timer at very early state, because sscg pll setting
338          * will use it
339          */
340         timer_init();
341
342         if (IS_ENABLED(CONFIG_SPL_BUILD)) {
343                 clock_init();
344                 imx_set_wdog_powerdown(false);
345         }
346
347         if (is_imx8mq()) {
348                 clock_enable(CCGR_OCOTP, 1);
349                 if (readl(&ocotp->ctrl) & 0x200)
350                         writel(0x200, &ocotp->ctrl_clr);
351         }
352
353         return 0;
354 }
355
356 #if defined(CONFIG_IMX8MN) || defined(CONFIG_IMX8MP)
357 struct rom_api *g_rom_api = (struct rom_api *)0x980;
358
359 enum boot_device get_boot_device(void)
360 {
361         volatile gd_t *pgd = gd;
362         int ret;
363         u32 boot;
364         u16 boot_type;
365         u8 boot_instance;
366         enum boot_device boot_dev = SD1_BOOT;
367
368         ret = g_rom_api->query_boot_infor(QUERY_BT_DEV, &boot,
369                                           ((uintptr_t)&boot) ^ QUERY_BT_DEV);
370         gd = pgd;
371
372         if (ret != ROM_API_OKAY) {
373                 puts("ROMAPI: failure at query_boot_info\n");
374                 return -1;
375         }
376
377         boot_type = boot >> 16;
378         boot_instance = (boot >> 8) & 0xff;
379
380         switch (boot_type) {
381         case BT_DEV_TYPE_SD:
382                 boot_dev = boot_instance + SD1_BOOT;
383                 break;
384         case BT_DEV_TYPE_MMC:
385                 boot_dev = boot_instance + MMC1_BOOT;
386                 break;
387         case BT_DEV_TYPE_NAND:
388                 boot_dev = NAND_BOOT;
389                 break;
390         case BT_DEV_TYPE_FLEXSPINOR:
391                 boot_dev = QSPI_BOOT;
392                 break;
393         case BT_DEV_TYPE_USB:
394                 boot_dev = USB_BOOT;
395                 break;
396         default:
397                 break;
398         }
399
400         return boot_dev;
401 }
402 #endif
403
404 bool is_usb_boot(void)
405 {
406         return get_boot_device() == USB_BOOT;
407 }
408
409 #ifdef CONFIG_OF_SYSTEM_SETUP
410 int ft_system_setup(void *blob, bd_t *bd)
411 {
412         int i = 0;
413         int rc;
414         int nodeoff;
415
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[] = {
419                         "/cpus/cpu@0",
420                         "/cpus/cpu@1",
421                         "/cpus/cpu@2",
422                         "/cpus/cpu@3",
423                 };
424
425                 for (i = 0; i < ARRAY_SIZE(nodes_path); i++) {
426                         nodeoff = fdt_path_offset(blob, nodes_path[i]);
427                         if (nodeoff < 0)
428                                 continue; /* Not found, skip it */
429
430                         debug("Found %s node\n", nodes_path[i]);
431
432                         rc = fdt_delprop(blob, nodeoff, "cpu-idle-states");
433                         if (rc == -FDT_ERR_NOTFOUND)
434                                 continue;
435                         if (rc) {
436                                 printf("Unable to update property %s:%s, err=%s\n",
437                                        nodes_path[i], "status", fdt_strerror(rc));
438                                 return rc;
439                         }
440
441                         debug("Remove %s:%s\n", nodes_path[i],
442                                "cpu-idle-states");
443                 }
444         }
445
446         return 0;
447 }
448 #endif
449
450 #if !CONFIG_IS_ENABLED(SYSRESET)
451 void reset_cpu(ulong addr)
452 {
453         struct watchdog_regs *wdog = (struct watchdog_regs *)WDOG1_BASE_ADDR;
454
455         /* Clear WDA to trigger WDOG_B immediately */
456         writew((SET_WCR_WT(1) | WCR_WDT | WCR_WDE | WCR_SRS), &wdog->wcr);
457
458         while (1) {
459                 /*
460                  * spin for .5 seconds before reset
461                  */
462         }
463 }
464 #endif
465
466 #if defined(CONFIG_ARCH_MISC_INIT)
467 static void acquire_buildinfo(void)
468 {
469         u64 atf_commit = 0;
470         struct arm_smccc_res res;
471
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);
475         atf_commit = res.a0;
476         if (atf_commit == 0xffffffff) {
477                 debug("ATF does not support build info\n");
478                 atf_commit = 0x30; /* Display 0, 0 ascii is 0x30 */
479         }
480
481         printf("\n BuildInfo:\n  - ATF %s\n\n", (char *)&atf_commit);
482 }
483
484 int arch_misc_init(void)
485 {
486         acquire_buildinfo();
487
488         return 0;
489 }
490 #endif
491
492 void imx_tmu_arch_init(void *reg_base)
493 {
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;
501
502                 u32 tca_rt, tca_hr, tca_en;
503                 u32 buf_vref, buf_slope;
504
505                 tca_rt = fuse->ana0 & 0xFF;
506                 tca_hr = (fuse->ana0 & 0xFF00) >> 8;
507                 tca_en = (fuse->ana0 & 0x2000000) >> 25;
508
509                 buf_vref = (fuse->ana0 & 0x1F00000) >> 20;
510                 buf_slope = (fuse->ana0 & 0xF0000) >> 16;
511
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);
515         }
516 #ifdef CONFIG_IMX8MP
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;
526         u32 reg;
527         u32 tca40[2], tca25[2], tca105[2];
528
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 */
533                 tca25[0] = 1596;
534                 tca25[1] = 1596;
535                 writel(tca25[0], (ulong)reg_base + 0x30);
536                 writel(tca25[1], (ulong)reg_base + 0x34);
537                 return;
538         }
539
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);
546
547         reg = (bgr << 28) | (bjt_cur << 20) | (vlsb << 12) | (1 << 7);
548         writel(reg, (ulong)reg_base + 0x3c);
549
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;
557
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);
562 #endif
563 }
564
565 #if defined(CONFIG_SPL_BUILD)
566 #if defined(CONFIG_IMX8MQ) || defined(CONFIG_IMX8MM) || defined(CONFIG_IMX8MN)
567 bool serror_need_skip = true;
568
569 void do_error(struct pt_regs *pt_regs, unsigned int esr)
570 {
571         /*
572          * If stack is still in ROM reserved OCRAM not switch to SPL,
573          * it is the ROM SError
574          */
575         ulong sp;
576
577         asm volatile("mov %0, sp" : "=r"(sp) : );
578
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 */
584                 }
585
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 */
590                 }
591         }
592
593         efi_restore_gd();
594         printf("\"Error\" handler, esr 0x%08x\n", esr);
595         show_regs(pt_regs);
596         panic("Resetting CPU ...\n");
597 }
598 #endif
599 #endif