Revert "arm64: Layerscape: Survive LPI one-way reset workaround"
[platform/kernel/u-boot.git] / arch / arm / cpu / armv8 / fsl-layerscape / soc.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright 2014-2015 Freescale Semiconductor
4  * Copyright 2019-2021 NXP
5  */
6
7 #include <common.h>
8 #include <clock_legacy.h>
9 #include <cpu_func.h>
10 #include <env.h>
11 #include <fsl_immap.h>
12 #include <fsl_ifc.h>
13 #include <init.h>
14 #include <linux/sizes.h>
15 #include <log.h>
16 #include <asm/arch/fsl_serdes.h>
17 #include <asm/arch/soc.h>
18 #include <asm/cache.h>
19 #include <asm/io.h>
20 #include <asm/global_data.h>
21 #include <asm/arch-fsl-layerscape/config.h>
22 #include <asm/arch-fsl-layerscape/ns_access.h>
23 #include <asm/arch-fsl-layerscape/fsl_icid.h>
24 #include <asm/gic-v3.h>
25 #ifdef CONFIG_LAYERSCAPE_NS_ACCESS
26 #include <fsl_csu.h>
27 #endif
28 #ifdef CONFIG_SYS_FSL_DDR
29 #include <fsl_ddr_sdram.h>
30 #include <fsl_ddr.h>
31 #endif
32 #ifdef CONFIG_CHAIN_OF_TRUST
33 #include <fsl_validate.h>
34 #endif
35 #include <fsl_immap.h>
36 #include <dm.h>
37 #include <dm/device_compat.h>
38 #include <linux/err.h>
39 #ifdef CONFIG_GIC_V3_ITS
40 DECLARE_GLOBAL_DATA_PTR;
41 #endif
42
43 #ifdef CONFIG_GIC_V3_ITS
44 int ls_gic_rd_tables_init(void *blob)
45 {
46         int ret;
47
48         ret = gic_lpi_tables_init();
49         if (ret)
50                 debug("%s: failed to init gic-lpi-tables\n", __func__);
51
52         return ret;
53 }
54 #endif
55
56 bool soc_has_dp_ddr(void)
57 {
58         struct ccsr_gur __iomem *gur = (void *)(CONFIG_SYS_FSL_GUTS_ADDR);
59         u32 svr = gur_in32(&gur->svr);
60
61         /* LS2085A, LS2088A, LS2048A has DP_DDR */
62         if ((SVR_SOC_VER(svr) == SVR_LS2085A) ||
63             (SVR_SOC_VER(svr) == SVR_LS2088A) ||
64             (SVR_SOC_VER(svr) == SVR_LS2048A))
65                 return true;
66
67         return false;
68 }
69
70 bool soc_has_aiop(void)
71 {
72         struct ccsr_gur __iomem *gur = (void *)(CONFIG_SYS_FSL_GUTS_ADDR);
73         u32 svr = gur_in32(&gur->svr);
74
75         /* LS2085A has AIOP */
76         if (SVR_SOC_VER(svr) == SVR_LS2085A)
77                 return true;
78
79         return false;
80 }
81
82 static inline void set_usb_txvreftune(u32 __iomem *scfg, u32 offset)
83 {
84         scfg_clrsetbits32(scfg + offset / 4,
85                         0xF << 6,
86                         SCFG_USB_TXVREFTUNE << 6);
87 }
88
89 static void erratum_a009008(void)
90 {
91 #ifdef CONFIG_SYS_FSL_ERRATUM_A009008
92         u32 __iomem *scfg = (u32 __iomem *)SCFG_BASE;
93
94 #if defined(CONFIG_ARCH_LS1043A) || defined(CONFIG_ARCH_LS1046A) || \
95         defined(CONFIG_ARCH_LS1012A)
96         set_usb_txvreftune(scfg, SCFG_USB3PRM1CR_USB1);
97 #if defined(CONFIG_ARCH_LS1043A) || defined(CONFIG_ARCH_LS1046A)
98         set_usb_txvreftune(scfg, SCFG_USB3PRM1CR_USB2);
99         set_usb_txvreftune(scfg, SCFG_USB3PRM1CR_USB3);
100 #endif
101 #elif defined(CONFIG_ARCH_LS2080A)
102         set_usb_txvreftune(scfg, SCFG_USB3PRM1CR);
103 #endif
104 #endif /* CONFIG_SYS_FSL_ERRATUM_A009008 */
105 }
106
107 static inline void set_usb_sqrxtune(u32 __iomem *scfg, u32 offset)
108 {
109         scfg_clrbits32(scfg + offset / 4,
110                         SCFG_USB_SQRXTUNE_MASK << 23);
111 }
112
113 static void erratum_a009798(void)
114 {
115 #ifdef CONFIG_SYS_FSL_ERRATUM_A009798
116         u32 __iomem *scfg = (u32 __iomem *)SCFG_BASE;
117
118 #if defined(CONFIG_ARCH_LS1043A) || defined(CONFIG_ARCH_LS1046A) || \
119         defined(CONFIG_ARCH_LS1012A)
120         set_usb_sqrxtune(scfg, SCFG_USB3PRM1CR_USB1);
121 #if defined(CONFIG_ARCH_LS1043A) || defined(CONFIG_ARCH_LS1046A)
122         set_usb_sqrxtune(scfg, SCFG_USB3PRM1CR_USB2);
123         set_usb_sqrxtune(scfg, SCFG_USB3PRM1CR_USB3);
124 #endif
125 #elif defined(CONFIG_ARCH_LS2080A)
126         set_usb_sqrxtune(scfg, SCFG_USB3PRM1CR);
127 #endif
128 #endif /* CONFIG_SYS_FSL_ERRATUM_A009798 */
129 }
130
131 #if defined(CONFIG_ARCH_LS1043A) || defined(CONFIG_ARCH_LS1046A) || \
132         defined(CONFIG_ARCH_LS1012A)
133 static inline void set_usb_pcstxswingfull(u32 __iomem *scfg, u32 offset)
134 {
135         scfg_clrsetbits32(scfg + offset / 4,
136                         0x7F << 9,
137                         SCFG_USB_PCSTXSWINGFULL << 9);
138 }
139 #endif
140
141 static void erratum_a008997(void)
142 {
143 #ifdef CONFIG_SYS_FSL_ERRATUM_A008997
144 #if defined(CONFIG_ARCH_LS1043A) || defined(CONFIG_ARCH_LS1046A) || \
145         defined(CONFIG_ARCH_LS1012A)
146         u32 __iomem *scfg = (u32 __iomem *)SCFG_BASE;
147
148         set_usb_pcstxswingfull(scfg, SCFG_USB3PRM2CR_USB1);
149 #if defined(CONFIG_ARCH_LS1043A) || defined(CONFIG_ARCH_LS1046A)
150         set_usb_pcstxswingfull(scfg, SCFG_USB3PRM2CR_USB2);
151         set_usb_pcstxswingfull(scfg, SCFG_USB3PRM2CR_USB3);
152 #endif
153 #elif defined(CONFIG_ARCH_LS1028A)
154         clrsetbits_le32(DCSR_BASE +  DCSR_USB_IOCR1,
155                         0x7F << 11,
156                         DCSR_USB_PCSTXSWINGFULL << 11);
157 #endif
158 #endif /* CONFIG_SYS_FSL_ERRATUM_A008997 */
159 }
160
161 #if defined(CONFIG_ARCH_LS1043A) || defined(CONFIG_ARCH_LS1046A) || \
162         defined(CONFIG_ARCH_LS1012A)
163
164 #define PROGRAM_USB_PHY_RX_OVRD_IN_HI(phy)      \
165         out_be16((phy) + SCFG_USB_PHY_RX_OVRD_IN_HI, USB_PHY_RX_EQ_VAL_1);      \
166         out_be16((phy) + SCFG_USB_PHY_RX_OVRD_IN_HI, USB_PHY_RX_EQ_VAL_2);      \
167         out_be16((phy) + SCFG_USB_PHY_RX_OVRD_IN_HI, USB_PHY_RX_EQ_VAL_3);      \
168         out_be16((phy) + SCFG_USB_PHY_RX_OVRD_IN_HI, USB_PHY_RX_EQ_VAL_4)
169
170 #elif defined(CONFIG_ARCH_LS2080A) || defined(CONFIG_ARCH_LS1088A) || \
171         defined(CONFIG_ARCH_LS1028A) || defined(CONFIG_ARCH_LX2160A) || \
172         defined(CONFIG_ARCH_LX2162A)
173
174 #define PROGRAM_USB_PHY_RX_OVRD_IN_HI(phy)      \
175         out_le16((phy) + DCSR_USB_PHY_RX_OVRD_IN_HI, USB_PHY_RX_EQ_VAL_1); \
176         out_le16((phy) + DCSR_USB_PHY_RX_OVRD_IN_HI, USB_PHY_RX_EQ_VAL_2); \
177         out_le16((phy) + DCSR_USB_PHY_RX_OVRD_IN_HI, USB_PHY_RX_EQ_VAL_3); \
178         out_le16((phy) + DCSR_USB_PHY_RX_OVRD_IN_HI, USB_PHY_RX_EQ_VAL_4)
179
180 #endif
181
182 static void erratum_a009007(void)
183 {
184 #if defined(CONFIG_ARCH_LS1043A) || defined(CONFIG_ARCH_LS1046A) || \
185         defined(CONFIG_ARCH_LS1012A)
186         void __iomem *usb_phy = (void __iomem *)SCFG_USB_PHY1;
187
188         PROGRAM_USB_PHY_RX_OVRD_IN_HI(usb_phy);
189 #if defined(CONFIG_ARCH_LS1043A) || defined(CONFIG_ARCH_LS1046A)
190         usb_phy = (void __iomem *)SCFG_USB_PHY2;
191         PROGRAM_USB_PHY_RX_OVRD_IN_HI(usb_phy);
192
193         usb_phy = (void __iomem *)SCFG_USB_PHY3;
194         PROGRAM_USB_PHY_RX_OVRD_IN_HI(usb_phy);
195 #endif
196 #elif defined(CONFIG_ARCH_LS2080A) || defined(CONFIG_ARCH_LS1088A) || \
197         defined(CONFIG_ARCH_LS1028A)
198         void __iomem *dcsr = (void __iomem *)DCSR_BASE;
199
200         PROGRAM_USB_PHY_RX_OVRD_IN_HI(dcsr + DCSR_USB_PHY1);
201         PROGRAM_USB_PHY_RX_OVRD_IN_HI(dcsr + DCSR_USB_PHY2);
202 #endif /* CONFIG_SYS_FSL_ERRATUM_A009007 */
203 }
204
205 #if defined(CONFIG_FSL_LSCH3)
206 static void erratum_a050204(void)
207 {
208 #if defined(CONFIG_ARCH_LX2160A) || defined(CONFIG_ARCH_LX2162A)
209         void __iomem *dcsr = (void __iomem *)DCSR_BASE;
210
211         PROGRAM_USB_PHY_RX_OVRD_IN_HI(dcsr + DCSR_USB_PHY1);
212         PROGRAM_USB_PHY_RX_OVRD_IN_HI(dcsr + DCSR_USB_PHY2);
213 #endif
214 }
215 /*
216  * This erratum requires setting a value to eddrtqcr1 to
217  * optimal the DDR performance.
218  */
219 static void erratum_a008336(void)
220 {
221 #ifdef CONFIG_SYS_FSL_ERRATUM_A008336
222         u32 *eddrtqcr1;
223
224 #ifdef CONFIG_SYS_FSL_DCSR_DDR_ADDR
225         eddrtqcr1 = (void *)CONFIG_SYS_FSL_DCSR_DDR_ADDR + 0x800;
226         if (fsl_ddr_get_version(0) == 0x50200)
227                 out_le32(eddrtqcr1, 0x63b30002);
228 #endif
229 #ifdef CONFIG_SYS_FSL_DCSR_DDR2_ADDR
230         eddrtqcr1 = (void *)CONFIG_SYS_FSL_DCSR_DDR2_ADDR + 0x800;
231         if (fsl_ddr_get_version(0) == 0x50200)
232                 out_le32(eddrtqcr1, 0x63b30002);
233 #endif
234 #endif
235 }
236
237 /*
238  * This erratum requires a register write before being Memory
239  * controller 3 being enabled.
240  */
241 static void erratum_a008514(void)
242 {
243 #ifdef CONFIG_SYS_FSL_ERRATUM_A008514
244         u32 *eddrtqcr1;
245
246 #ifdef CONFIG_SYS_FSL_DCSR_DDR3_ADDR
247         eddrtqcr1 = (void *)CONFIG_SYS_FSL_DCSR_DDR3_ADDR + 0x800;
248         out_le32(eddrtqcr1, 0x63b20002);
249 #endif
250 #endif
251 }
252 #ifdef CONFIG_SYS_FSL_ERRATUM_A009635
253 #define PLATFORM_CYCLE_ENV_VAR  "a009635_interval_val"
254
255 static unsigned long get_internval_val_mhz(void)
256 {
257         char *interval = env_get(PLATFORM_CYCLE_ENV_VAR);
258         /*
259          *  interval is the number of platform cycles(MHz) between
260          *  wake up events generated by EPU.
261          */
262         ulong interval_mhz = get_bus_freq(0) / (1000 * 1000);
263
264         if (interval)
265                 interval_mhz = dectoul(interval, NULL);
266
267         return interval_mhz;
268 }
269
270 void erratum_a009635(void)
271 {
272         u32 val;
273         unsigned long interval_mhz = get_internval_val_mhz();
274
275         if (!interval_mhz)
276                 return;
277
278         val = in_le32(DCSR_CGACRE5);
279         writel(val | 0x00000200, DCSR_CGACRE5);
280
281         val = in_le32(EPU_EPCMPR5);
282         writel(interval_mhz, EPU_EPCMPR5);
283         val = in_le32(EPU_EPCCR5);
284         writel(val | 0x82820000, EPU_EPCCR5);
285         val = in_le32(EPU_EPSMCR5);
286         writel(val | 0x002f0000, EPU_EPSMCR5);
287         val = in_le32(EPU_EPECR5);
288         writel(val | 0x20000000, EPU_EPECR5);
289         val = in_le32(EPU_EPGCR);
290         writel(val | 0x80000000, EPU_EPGCR);
291 }
292 #endif  /* CONFIG_SYS_FSL_ERRATUM_A009635 */
293
294 static void erratum_rcw_src(void)
295 {
296 #if defined(CONFIG_SPL) && defined(CONFIG_NAND_BOOT)
297         u32 __iomem *dcfg_ccsr = (u32 __iomem *)DCFG_BASE;
298         u32 __iomem *dcfg_dcsr = (u32 __iomem *)DCFG_DCSR_BASE;
299         u32 val;
300
301         val = in_le32(dcfg_ccsr + DCFG_PORSR1 / 4);
302         val &= ~DCFG_PORSR1_RCW_SRC;
303         val |= DCFG_PORSR1_RCW_SRC_NOR;
304         out_le32(dcfg_dcsr + DCFG_DCSR_PORCR1 / 4, val);
305 #endif
306 }
307
308 #define I2C_DEBUG_REG 0x6
309 #define I2C_GLITCH_EN 0x8
310 /*
311  * This erratum requires setting glitch_en bit to enable
312  * digital glitch filter to improve clock stability.
313  */
314 #ifdef CONFIG_SYS_FSL_ERRATUM_A009203
315 static void erratum_a009203(void)
316 {
317 #if CONFIG_IS_ENABLED(SYS_I2C_LEGACY)
318         u8 __iomem *ptr;
319 #ifdef I2C1_BASE_ADDR
320         ptr = (u8 __iomem *)(I2C1_BASE_ADDR + I2C_DEBUG_REG);
321
322         writeb(I2C_GLITCH_EN, ptr);
323 #endif
324 #ifdef I2C2_BASE_ADDR
325         ptr = (u8 __iomem *)(I2C2_BASE_ADDR + I2C_DEBUG_REG);
326
327         writeb(I2C_GLITCH_EN, ptr);
328 #endif
329 #ifdef I2C3_BASE_ADDR
330         ptr = (u8 __iomem *)(I2C3_BASE_ADDR + I2C_DEBUG_REG);
331
332         writeb(I2C_GLITCH_EN, ptr);
333 #endif
334 #ifdef I2C4_BASE_ADDR
335         ptr = (u8 __iomem *)(I2C4_BASE_ADDR + I2C_DEBUG_REG);
336
337         writeb(I2C_GLITCH_EN, ptr);
338 #endif
339 #endif
340 }
341 #endif
342
343 void bypass_smmu(void)
344 {
345         u32 val;
346         val = (in_le32(SMMU_SCR0) | SCR0_CLIENTPD_MASK) & ~(SCR0_USFCFG_MASK);
347         out_le32(SMMU_SCR0, val);
348         val = (in_le32(SMMU_NSCR0) | SCR0_CLIENTPD_MASK) & ~(SCR0_USFCFG_MASK);
349         out_le32(SMMU_NSCR0, val);
350 }
351 void fsl_lsch3_early_init_f(void)
352 {
353         erratum_rcw_src();
354 #ifdef CONFIG_FSL_IFC
355         init_early_memctl_regs();       /* tighten IFC timing */
356 #endif
357 #ifdef CONFIG_SYS_FSL_ERRATUM_A009203
358         erratum_a009203();
359 #endif
360         erratum_a008514();
361         erratum_a008336();
362         erratum_a009008();
363         erratum_a009798();
364         erratum_a008997();
365         erratum_a009007();
366         erratum_a050204();
367 #ifdef CONFIG_CHAIN_OF_TRUST
368         /* In case of Secure Boot, the IBR configures the SMMU
369         * to allow only Secure transactions.
370         * SMMU must be reset in bypass mode.
371         * Set the ClientPD bit and Clear the USFCFG Bit
372         */
373         if (fsl_check_boot_mode_secure() == 1)
374                 bypass_smmu();
375 #endif
376
377 #if defined(CONFIG_ARCH_LS1088A) || defined(CONFIG_ARCH_LS1028A) || \
378         defined(CONFIG_ARCH_LS2080A) || defined(CONFIG_ARCH_LX2160A) || \
379         defined(CONFIG_ARCH_LX2162A)
380         set_icids();
381 #endif
382 }
383
384 /* Get VDD in the unit mV from voltage ID */
385 int get_core_volt_from_fuse(void)
386 {
387         struct ccsr_gur *gur = (void *)(CONFIG_SYS_FSL_GUTS_ADDR);
388         int vdd;
389         u32 fusesr;
390         u8 vid;
391
392         /* get the voltage ID from fuse status register */
393         fusesr = in_le32(&gur->dcfg_fusesr);
394         debug("%s: fusesr = 0x%x\n", __func__, fusesr);
395         vid = (fusesr >> FSL_CHASSIS3_DCFG_FUSESR_ALTVID_SHIFT) &
396                 FSL_CHASSIS3_DCFG_FUSESR_ALTVID_MASK;
397         if ((vid == 0) || (vid == FSL_CHASSIS3_DCFG_FUSESR_ALTVID_MASK)) {
398                 vid = (fusesr >> FSL_CHASSIS3_DCFG_FUSESR_VID_SHIFT) &
399                         FSL_CHASSIS3_DCFG_FUSESR_VID_MASK;
400         }
401         debug("%s: VID = 0x%x\n", __func__, vid);
402         switch (vid) {
403         case 0x00: /* VID isn't supported */
404                 vdd = -EINVAL;
405                 debug("%s: The VID feature is not supported\n", __func__);
406                 break;
407         case 0x08: /* 0.9V silicon */
408                 vdd = 900;
409                 break;
410         case 0x10: /* 1.0V silicon */
411                 vdd = 1000;
412                 break;
413         default:  /* Other core voltage */
414                 vdd = -EINVAL;
415                 debug("%s: The VID(%x) isn't supported\n", __func__, vid);
416                 break;
417         }
418         debug("%s: The required minimum volt of CORE is %dmV\n", __func__, vdd);
419
420         return vdd;
421 }
422
423 #elif defined(CONFIG_FSL_LSCH2)
424 /*
425  * This erratum requires setting a value to eddrtqcr1 to optimal
426  * the DDR performance. The eddrtqcr1 register is in SCFG space
427  * of LS1043A and the offset is 0x157_020c.
428  */
429 #if defined(CONFIG_SYS_FSL_ERRATUM_A009660) \
430         && defined(CONFIG_SYS_FSL_ERRATUM_A008514)
431 #error A009660 and A008514 can not be both enabled.
432 #endif
433
434 static void erratum_a009660(void)
435 {
436 #ifdef CONFIG_SYS_FSL_ERRATUM_A009660
437         u32 *eddrtqcr1 = (void *)CONFIG_SYS_FSL_SCFG_ADDR + 0x20c;
438         out_be32(eddrtqcr1, 0x63b20042);
439 #endif
440 }
441
442 static void erratum_a008850_early(void)
443 {
444 #ifdef CONFIG_SYS_FSL_ERRATUM_A008850
445         /* part 1 of 2 */
446         struct ccsr_cci400 __iomem *cci = (void *)(CONFIG_SYS_IMMR +
447                                                 CONFIG_SYS_CCI400_OFFSET);
448         struct ccsr_ddr __iomem *ddr = (void *)CONFIG_SYS_FSL_DDR_ADDR;
449
450         /* Skip if running at lower exception level */
451         if (current_el() < 3)
452                 return;
453
454         /* disables propagation of barrier transactions to DDRC from CCI400 */
455         out_le32(&cci->ctrl_ord, CCI400_CTRLORD_TERM_BARRIER);
456
457         /* disable the re-ordering in DDRC */
458         ddr_out32(&ddr->eor, DDR_EOR_RD_REOD_DIS | DDR_EOR_WD_REOD_DIS);
459 #endif
460 }
461
462 void erratum_a008850_post(void)
463 {
464 #ifdef CONFIG_SYS_FSL_ERRATUM_A008850
465         /* part 2 of 2 */
466         struct ccsr_cci400 __iomem *cci = (void *)(CONFIG_SYS_IMMR +
467                                                 CONFIG_SYS_CCI400_OFFSET);
468         struct ccsr_ddr __iomem *ddr = (void *)CONFIG_SYS_FSL_DDR_ADDR;
469         u32 tmp;
470
471         /* Skip if running at lower exception level */
472         if (current_el() < 3)
473                 return;
474
475         /* enable propagation of barrier transactions to DDRC from CCI400 */
476         out_le32(&cci->ctrl_ord, CCI400_CTRLORD_EN_BARRIER);
477
478         /* enable the re-ordering in DDRC */
479         tmp = ddr_in32(&ddr->eor);
480         tmp &= ~(DDR_EOR_RD_REOD_DIS | DDR_EOR_WD_REOD_DIS);
481         ddr_out32(&ddr->eor, tmp);
482 #endif
483 }
484
485 #ifdef CONFIG_SYS_FSL_ERRATUM_A010315
486 void erratum_a010315(void)
487 {
488         int i;
489
490         for (i = PCIE1; i <= PCIE4; i++)
491                 if (!is_serdes_configured(i)) {
492                         debug("PCIe%d: disabled all R/W permission!\n", i);
493                         set_pcie_ns_access(i, 0);
494                 }
495 }
496 #endif
497
498 static void erratum_a010539(void)
499 {
500 #if defined(CONFIG_SYS_FSL_ERRATUM_A010539) && defined(CONFIG_QSPI_BOOT)
501         struct ccsr_gur __iomem *gur = (void *)(CONFIG_SYS_FSL_GUTS_ADDR);
502         u32 porsr1;
503
504         porsr1 = in_be32(&gur->porsr1);
505         porsr1 &= ~FSL_CHASSIS2_CCSR_PORSR1_RCW_MASK;
506         out_be32((void *)(CONFIG_SYS_DCSR_DCFG_ADDR + DCFG_DCSR_PORCR1),
507                  porsr1);
508         out_be32((void *)(CONFIG_SYS_FSL_SCFG_ADDR + 0x1a8), 0xffffffff);
509 #endif
510 }
511
512 /* Get VDD in the unit mV from voltage ID */
513 int get_core_volt_from_fuse(void)
514 {
515         struct ccsr_gur *gur = (void *)(CONFIG_SYS_FSL_GUTS_ADDR);
516         int vdd;
517         u32 fusesr;
518         u8 vid;
519
520         fusesr = in_be32(&gur->dcfg_fusesr);
521         debug("%s: fusesr = 0x%x\n", __func__, fusesr);
522         vid = (fusesr >> FSL_CHASSIS2_DCFG_FUSESR_ALTVID_SHIFT) &
523                 FSL_CHASSIS2_DCFG_FUSESR_ALTVID_MASK;
524         if ((vid == 0) || (vid == FSL_CHASSIS2_DCFG_FUSESR_ALTVID_MASK)) {
525                 vid = (fusesr >> FSL_CHASSIS2_DCFG_FUSESR_VID_SHIFT) &
526                         FSL_CHASSIS2_DCFG_FUSESR_VID_MASK;
527         }
528         debug("%s: VID = 0x%x\n", __func__, vid);
529         switch (vid) {
530         case 0x00: /* VID isn't supported */
531                 vdd = -EINVAL;
532                 debug("%s: The VID feature is not supported\n", __func__);
533                 break;
534         case 0x08: /* 0.9V silicon */
535                 vdd = 900;
536                 break;
537         case 0x10: /* 1.0V silicon */
538                 vdd = 1000;
539                 break;
540         default:  /* Other core voltage */
541                 vdd = -EINVAL;
542                 printf("%s: The VID(%x) isn't supported\n", __func__, vid);
543                 break;
544         }
545         debug("%s: The required minimum volt of CORE is %dmV\n", __func__, vdd);
546
547         return vdd;
548 }
549
550 __weak int board_switch_core_volt(u32 vdd)
551 {
552         return 0;
553 }
554
555 static int setup_core_volt(u32 vdd)
556 {
557         return board_setup_core_volt(vdd);
558 }
559
560 #ifdef CONFIG_SYS_FSL_DDR
561 static void ddr_enable_0v9_volt(bool en)
562 {
563         struct ccsr_ddr __iomem *ddr = (void *)CONFIG_SYS_FSL_DDR_ADDR;
564         u32 tmp;
565
566         tmp = ddr_in32(&ddr->ddr_cdr1);
567
568         if (en)
569                 tmp |= DDR_CDR1_V0PT9_EN;
570         else
571                 tmp &= ~DDR_CDR1_V0PT9_EN;
572
573         ddr_out32(&ddr->ddr_cdr1, tmp);
574 }
575 #endif
576
577 int setup_chip_volt(void)
578 {
579         int vdd;
580
581         vdd = get_core_volt_from_fuse();
582         /* Nothing to do for silicons doesn't support VID */
583         if (vdd < 0)
584                 return vdd;
585
586         if (setup_core_volt(vdd))
587                 printf("%s: Switch core VDD to %dmV failed\n", __func__, vdd);
588 #ifdef CONFIG_SYS_HAS_SERDES
589         if (setup_serdes_volt(vdd))
590                 printf("%s: Switch SVDD to %dmV failed\n", __func__, vdd);
591 #endif
592
593 #ifdef CONFIG_SYS_FSL_DDR
594         if (vdd == 900)
595                 ddr_enable_0v9_volt(true);
596 #endif
597
598         return 0;
599 }
600
601 #ifdef CONFIG_FSL_PFE
602 void init_pfe_scfg_dcfg_regs(void)
603 {
604         struct ccsr_scfg *scfg = (struct ccsr_scfg *)CONFIG_SYS_FSL_SCFG_ADDR;
605         u32 ecccr2;
606
607         out_be32(&scfg->pfeasbcr,
608                  in_be32(&scfg->pfeasbcr) | SCFG_PFEASBCR_AWCACHE0);
609         out_be32(&scfg->pfebsbcr,
610                  in_be32(&scfg->pfebsbcr) | SCFG_PFEASBCR_AWCACHE0);
611
612         /* CCI-400 QoS settings for PFE */
613         out_be32(&scfg->wr_qos1, (unsigned int)(SCFG_WR_QOS1_PFE1_QOS
614                  | SCFG_WR_QOS1_PFE2_QOS));
615         out_be32(&scfg->rd_qos1, (unsigned int)(SCFG_RD_QOS1_PFE1_QOS
616                  | SCFG_RD_QOS1_PFE2_QOS));
617
618         ecccr2 = in_be32(CONFIG_SYS_DCSR_DCFG_ADDR + DCFG_DCSR_ECCCR2);
619         out_be32((void *)CONFIG_SYS_DCSR_DCFG_ADDR + DCFG_DCSR_ECCCR2,
620                  ecccr2 | (unsigned int)DISABLE_PFE_ECC);
621 }
622 #endif
623
624 void fsl_lsch2_early_init_f(void)
625 {
626         struct ccsr_cci400 *cci = (struct ccsr_cci400 *)(CONFIG_SYS_IMMR +
627                                         CONFIG_SYS_CCI400_OFFSET);
628         struct ccsr_scfg *scfg = (struct ccsr_scfg *)CONFIG_SYS_FSL_SCFG_ADDR;
629 #if defined(CONFIG_FSL_QSPI) && defined(CONFIG_TFABOOT)
630         enum boot_src src;
631 #endif
632
633 #ifdef CONFIG_LAYERSCAPE_NS_ACCESS
634         enable_layerscape_ns_access();
635 #endif
636
637 #ifdef CONFIG_FSL_IFC
638         init_early_memctl_regs();       /* tighten IFC timing */
639 #endif
640
641 #if defined(CONFIG_FSL_QSPI) && defined(CONFIG_TFABOOT)
642         src = get_boot_src();
643         if (src != BOOT_SOURCE_QSPI_NOR)
644                 out_be32(&scfg->qspi_cfg, SCFG_QSPI_CLKSEL);
645 #else
646 #if defined(CONFIG_FSL_QSPI) && !defined(CONFIG_QSPI_BOOT)
647         out_be32(&scfg->qspi_cfg, SCFG_QSPI_CLKSEL);
648 #endif
649 #endif
650         /* Make SEC reads and writes snoopable */
651 #if defined(CONFIG_ARCH_LS1043A) || defined(CONFIG_ARCH_LS1046A)
652         setbits_be32(&scfg->snpcnfgcr, SCFG_SNPCNFGCR_SECRDSNP |
653                         SCFG_SNPCNFGCR_SECWRSNP | SCFG_SNPCNFGCR_USB1RDSNP |
654                         SCFG_SNPCNFGCR_USB1WRSNP | SCFG_SNPCNFGCR_USB2RDSNP |
655                         SCFG_SNPCNFGCR_USB2WRSNP | SCFG_SNPCNFGCR_USB3RDSNP |
656                         SCFG_SNPCNFGCR_USB3WRSNP | SCFG_SNPCNFGCR_SATARDSNP |
657                         SCFG_SNPCNFGCR_SATAWRSNP);
658 #elif defined(CONFIG_ARCH_LS1012A)
659         setbits_be32(&scfg->snpcnfgcr, SCFG_SNPCNFGCR_SECRDSNP |
660                         SCFG_SNPCNFGCR_SECWRSNP | SCFG_SNPCNFGCR_USB1RDSNP |
661                         SCFG_SNPCNFGCR_USB1WRSNP | SCFG_SNPCNFGCR_SATARDSNP |
662                         SCFG_SNPCNFGCR_SATAWRSNP);
663 #else
664         setbits_be32(&scfg->snpcnfgcr, SCFG_SNPCNFGCR_SECRDSNP |
665                      SCFG_SNPCNFGCR_SECWRSNP |
666                      SCFG_SNPCNFGCR_SATARDSNP |
667                      SCFG_SNPCNFGCR_SATAWRSNP);
668 #endif
669
670         /*
671          * Enable snoop requests and DVM message requests for
672          * Slave insterface S4 (A53 core cluster)
673          */
674         if (current_el() == 3) {
675                 out_le32(&cci->slave[4].snoop_ctrl,
676                          CCI400_DVM_MESSAGE_REQ_EN | CCI400_SNOOP_REQ_EN);
677         }
678
679         /*
680          * Program Central Security Unit (CSU) to grant access
681          * permission for USB 2.0 controller
682          */
683 #if defined(CONFIG_ARCH_LS1012A) && defined(CONFIG_USB_EHCI_FSL)
684         if (current_el() == 3)
685                 set_devices_ns_access(CSU_CSLX_USB_2, CSU_ALL_RW);
686 #endif
687         /* Erratum */
688         erratum_a008850_early(); /* part 1 of 2 */
689         erratum_a009660();
690         erratum_a010539();
691         erratum_a009008();
692         erratum_a009798();
693         erratum_a008997();
694         erratum_a009007();
695
696 #if defined(CONFIG_ARCH_LS1043A) || defined(CONFIG_ARCH_LS1046A)
697         set_icids();
698 #endif
699 }
700 #endif
701
702 #ifdef CONFIG_FSPI_AHB_EN_4BYTE
703 int fspi_ahb_init(void)
704 {
705         /* Enable 4bytes address support and fast read */
706         u32 *fspi_lut, lut_key, *fspi_key;
707
708         fspi_key = (void *)SYS_NXP_FSPI_ADDR + SYS_NXP_FSPI_LUTKEY_BASE_ADDR;
709         fspi_lut = (void *)SYS_NXP_FSPI_ADDR + SYS_NXP_FSPI_LUT_BASE_ADDR;
710
711         lut_key = in_be32(fspi_key);
712
713         if (lut_key == SYS_NXP_FSPI_LUTKEY) {
714                 /* That means the register is BE */
715                 out_be32(fspi_key, SYS_NXP_FSPI_LUTKEY);
716                 /* Unlock the lut table */
717                 out_be32(fspi_key + 1, SYS_NXP_FSPI_LUTCR_UNLOCK);
718                 /* Create READ LUT */
719                 out_be32(fspi_lut, 0x0820040c);
720                 out_be32(fspi_lut + 1, 0x24003008);
721                 out_be32(fspi_lut + 2, 0x00000000);
722                 /* Lock the lut table */
723                 out_be32(fspi_key, SYS_NXP_FSPI_LUTKEY);
724                 out_be32(fspi_key + 1, SYS_NXP_FSPI_LUTCR_LOCK);
725         } else {
726                 /* That means the register is LE */
727                 out_le32(fspi_key, SYS_NXP_FSPI_LUTKEY);
728                 /* Unlock the lut table */
729                 out_le32(fspi_key + 1, SYS_NXP_FSPI_LUTCR_UNLOCK);
730                 /* Create READ LUT */
731                 out_le32(fspi_lut, 0x0820040c);
732                 out_le32(fspi_lut + 1, 0x24003008);
733                 out_le32(fspi_lut + 2, 0x00000000);
734                 /* Lock the lut table */
735                 out_le32(fspi_key, SYS_NXP_FSPI_LUTKEY);
736                 out_le32(fspi_key + 1, SYS_NXP_FSPI_LUTCR_LOCK);
737         }
738
739         return 0;
740 }
741 #endif
742
743 #ifdef CONFIG_QSPI_AHB_INIT
744 /* Enable 4bytes address support and fast read */
745 int qspi_ahb_init(void)
746 {
747         u32 *qspi_lut, lut_key, *qspi_key;
748
749         qspi_key = (void *)SYS_FSL_QSPI_ADDR + 0x300;
750         qspi_lut = (void *)SYS_FSL_QSPI_ADDR + 0x310;
751
752         lut_key = in_be32(qspi_key);
753
754         if (lut_key == 0x5af05af0) {
755                 /* That means the register is BE */
756                 out_be32(qspi_key, 0x5af05af0);
757                 /* Unlock the lut table */
758                 out_be32(qspi_key + 1, 0x00000002);
759                 out_be32(qspi_lut, 0x0820040c);
760                 out_be32(qspi_lut + 1, 0x1c080c08);
761                 out_be32(qspi_lut + 2, 0x00002400);
762                 /* Lock the lut table */
763                 out_be32(qspi_key, 0x5af05af0);
764                 out_be32(qspi_key + 1, 0x00000001);
765         } else {
766                 /* That means the register is LE */
767                 out_le32(qspi_key, 0x5af05af0);
768                 /* Unlock the lut table */
769                 out_le32(qspi_key + 1, 0x00000002);
770                 out_le32(qspi_lut, 0x0820040c);
771                 out_le32(qspi_lut + 1, 0x1c080c08);
772                 out_le32(qspi_lut + 2, 0x00002400);
773                 /* Lock the lut table */
774                 out_le32(qspi_key, 0x5af05af0);
775                 out_le32(qspi_key + 1, 0x00000001);
776         }
777
778         return 0;
779 }
780 #endif
781
782 #ifdef CONFIG_TFABOOT
783 #define MAX_BOOTCMD_SIZE        512
784
785 int fsl_setenv_bootcmd(void)
786 {
787         int ret;
788         enum boot_src src = get_boot_src();
789         char bootcmd_str[MAX_BOOTCMD_SIZE];
790
791         switch (src) {
792 #ifdef IFC_NOR_BOOTCOMMAND
793         case BOOT_SOURCE_IFC_NOR:
794                 sprintf(bootcmd_str, IFC_NOR_BOOTCOMMAND);
795                 break;
796 #endif
797 #ifdef QSPI_NOR_BOOTCOMMAND
798         case BOOT_SOURCE_QSPI_NOR:
799                 sprintf(bootcmd_str, QSPI_NOR_BOOTCOMMAND);
800                 break;
801 #endif
802 #ifdef XSPI_NOR_BOOTCOMMAND
803         case BOOT_SOURCE_XSPI_NOR:
804                 sprintf(bootcmd_str, XSPI_NOR_BOOTCOMMAND);
805                 break;
806 #endif
807 #ifdef IFC_NAND_BOOTCOMMAND
808         case BOOT_SOURCE_IFC_NAND:
809                 sprintf(bootcmd_str, IFC_NAND_BOOTCOMMAND);
810                 break;
811 #endif
812 #ifdef QSPI_NAND_BOOTCOMMAND
813         case BOOT_SOURCE_QSPI_NAND:
814                 sprintf(bootcmd_str, QSPI_NAND_BOOTCOMMAND);
815                 break;
816 #endif
817 #ifdef XSPI_NAND_BOOTCOMMAND
818         case BOOT_SOURCE_XSPI_NAND:
819                 sprintf(bootcmd_str, XSPI_NAND_BOOTCOMMAND);
820                 break;
821 #endif
822 #ifdef SD_BOOTCOMMAND
823         case BOOT_SOURCE_SD_MMC:
824                 sprintf(bootcmd_str, SD_BOOTCOMMAND);
825                 break;
826 #endif
827 #ifdef SD2_BOOTCOMMAND
828         case BOOT_SOURCE_SD_MMC2:
829                 sprintf(bootcmd_str, SD2_BOOTCOMMAND);
830                 break;
831 #endif
832         default:
833 #ifdef QSPI_NOR_BOOTCOMMAND
834                 sprintf(bootcmd_str, QSPI_NOR_BOOTCOMMAND);
835 #endif
836                 break;
837         }
838
839         ret = env_set("bootcmd", bootcmd_str);
840         if (ret) {
841                 printf("Failed to set bootcmd: ret = %d\n", ret);
842                 return ret;
843         }
844         return 0;
845 }
846
847 int fsl_setenv_mcinitcmd(void)
848 {
849         int ret = 0;
850         enum boot_src src = get_boot_src();
851
852         switch (src) {
853 #ifdef IFC_MC_INIT_CMD
854         case BOOT_SOURCE_IFC_NAND:
855         case BOOT_SOURCE_IFC_NOR:
856         ret = env_set("mcinitcmd", IFC_MC_INIT_CMD);
857                 break;
858 #endif
859 #ifdef QSPI_MC_INIT_CMD
860         case BOOT_SOURCE_QSPI_NAND:
861         case BOOT_SOURCE_QSPI_NOR:
862         ret = env_set("mcinitcmd", QSPI_MC_INIT_CMD);
863                 break;
864 #endif
865 #ifdef XSPI_MC_INIT_CMD
866         case BOOT_SOURCE_XSPI_NAND:
867         case BOOT_SOURCE_XSPI_NOR:
868         ret = env_set("mcinitcmd", XSPI_MC_INIT_CMD);
869                 break;
870 #endif
871 #ifdef SD_MC_INIT_CMD
872         case BOOT_SOURCE_SD_MMC:
873         ret = env_set("mcinitcmd", SD_MC_INIT_CMD);
874                 break;
875 #endif
876 #ifdef SD2_MC_INIT_CMD
877         case BOOT_SOURCE_SD_MMC2:
878         ret = env_set("mcinitcmd", SD2_MC_INIT_CMD);
879                 break;
880 #endif
881         default:
882 #ifdef QSPI_MC_INIT_CMD
883         ret = env_set("mcinitcmd", QSPI_MC_INIT_CMD);
884 #endif
885                 break;
886         }
887
888         if (ret) {
889                 printf("Failed to set mcinitcmd: ret = %d\n", ret);
890                 return ret;
891         }
892         return 0;
893 }
894 #endif
895
896 #ifdef CONFIG_BOARD_LATE_INIT
897 __weak int fsl_board_late_init(void)
898 {
899         return 0;
900 }
901
902 #define DWC3_GSBUSCFG0                  0xc100
903 #define DWC3_GSBUSCFG0_CACHETYPE_SHIFT  16
904 #define DWC3_GSBUSCFG0_CACHETYPE(n)        (((n) & 0xffff)            \
905         << DWC3_GSBUSCFG0_CACHETYPE_SHIFT)
906
907 void enable_dwc3_snooping(void)
908 {
909         int ret;
910         u32 val;
911         struct udevice *bus;
912         struct uclass *uc;
913         fdt_addr_t dwc3_base;
914
915         ret = uclass_get(UCLASS_USB, &uc);
916         if (ret)
917                 return;
918
919         uclass_foreach_dev(bus, uc) {
920                 if (!strcmp(bus->driver->of_match->compatible, "fsl,layerscape-dwc3")) {
921                         dwc3_base = devfdt_get_addr(bus);
922                         if (dwc3_base == FDT_ADDR_T_NONE) {
923                                 dev_err(bus, "dwc3 regs missing\n");
924                                 continue;
925                         }
926                         val = in_le32(dwc3_base + DWC3_GSBUSCFG0);
927                         val &= ~DWC3_GSBUSCFG0_CACHETYPE(~0);
928                         val |= DWC3_GSBUSCFG0_CACHETYPE(0x2222);
929                         writel(val, dwc3_base + DWC3_GSBUSCFG0);
930                 }
931         }
932 }
933
934 int board_late_init(void)
935 {
936 #ifdef CONFIG_CHAIN_OF_TRUST
937         fsl_setenv_chain_of_trust();
938 #endif
939 #ifdef CONFIG_TFABOOT
940         /*
941          * Set bootcmd and mcinitcmd if "fsl_bootcmd_mcinitcmd_set" does
942          * not exists in env
943          */
944         if (env_get_yesno("fsl_bootcmd_mcinitcmd_set") <= 0) {
945                 // Set bootcmd and mcinitcmd as per boot source
946                 fsl_setenv_bootcmd();
947                 fsl_setenv_mcinitcmd();
948                 env_set("fsl_bootcmd_mcinitcmd_set", "y");
949         }
950 #endif
951 #ifdef CONFIG_QSPI_AHB_INIT
952         qspi_ahb_init();
953 #endif
954 #ifdef CONFIG_FSPI_AHB_EN_4BYTE
955         fspi_ahb_init();
956 #endif
957
958         if (IS_ENABLED(CONFIG_DM))
959                 enable_dwc3_snooping();
960
961         return fsl_board_late_init();
962 }
963 #endif