Merge branch '2020-07-29-add-brcm-ns3-support'
[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 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 #ifdef CONFIG_TFABOOT
37 #include <env_internal.h>
38 #endif
39 #if defined(CONFIG_TFABOOT) || defined(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
173 #define PROGRAM_USB_PHY_RX_OVRD_IN_HI(phy)      \
174         out_le16((phy) + DCSR_USB_PHY_RX_OVRD_IN_HI, USB_PHY_RX_EQ_VAL_1); \
175         out_le16((phy) + DCSR_USB_PHY_RX_OVRD_IN_HI, USB_PHY_RX_EQ_VAL_2); \
176         out_le16((phy) + DCSR_USB_PHY_RX_OVRD_IN_HI, USB_PHY_RX_EQ_VAL_3); \
177         out_le16((phy) + DCSR_USB_PHY_RX_OVRD_IN_HI, USB_PHY_RX_EQ_VAL_4)
178
179 #endif
180
181 static void erratum_a009007(void)
182 {
183 #if defined(CONFIG_ARCH_LS1043A) || defined(CONFIG_ARCH_LS1046A) || \
184         defined(CONFIG_ARCH_LS1012A)
185         void __iomem *usb_phy = (void __iomem *)SCFG_USB_PHY1;
186
187         PROGRAM_USB_PHY_RX_OVRD_IN_HI(usb_phy);
188 #if defined(CONFIG_ARCH_LS1043A) || defined(CONFIG_ARCH_LS1046A)
189         usb_phy = (void __iomem *)SCFG_USB_PHY2;
190         PROGRAM_USB_PHY_RX_OVRD_IN_HI(usb_phy);
191
192         usb_phy = (void __iomem *)SCFG_USB_PHY3;
193         PROGRAM_USB_PHY_RX_OVRD_IN_HI(usb_phy);
194 #endif
195 #elif defined(CONFIG_ARCH_LS2080A) || defined(CONFIG_ARCH_LS1088A) || \
196         defined(CONFIG_ARCH_LS1028A)
197         void __iomem *dcsr = (void __iomem *)DCSR_BASE;
198
199         PROGRAM_USB_PHY_RX_OVRD_IN_HI(dcsr + DCSR_USB_PHY1);
200         PROGRAM_USB_PHY_RX_OVRD_IN_HI(dcsr + DCSR_USB_PHY2);
201 #endif /* CONFIG_SYS_FSL_ERRATUM_A009007 */
202 }
203
204 #if defined(CONFIG_FSL_LSCH3)
205 static void erratum_a050106(void)
206 {
207 #if defined(CONFIG_ARCH_LX2160A)
208         void __iomem *dcsr = (void __iomem *)DCSR_BASE;
209
210         PROGRAM_USB_PHY_RX_OVRD_IN_HI(dcsr + DCSR_USB_PHY1);
211         PROGRAM_USB_PHY_RX_OVRD_IN_HI(dcsr + DCSR_USB_PHY2);
212 #endif
213 }
214 /*
215  * This erratum requires setting a value to eddrtqcr1 to
216  * optimal the DDR performance.
217  */
218 static void erratum_a008336(void)
219 {
220 #ifdef CONFIG_SYS_FSL_ERRATUM_A008336
221         u32 *eddrtqcr1;
222
223 #ifdef CONFIG_SYS_FSL_DCSR_DDR_ADDR
224         eddrtqcr1 = (void *)CONFIG_SYS_FSL_DCSR_DDR_ADDR + 0x800;
225         if (fsl_ddr_get_version(0) == 0x50200)
226                 out_le32(eddrtqcr1, 0x63b30002);
227 #endif
228 #ifdef CONFIG_SYS_FSL_DCSR_DDR2_ADDR
229         eddrtqcr1 = (void *)CONFIG_SYS_FSL_DCSR_DDR2_ADDR + 0x800;
230         if (fsl_ddr_get_version(0) == 0x50200)
231                 out_le32(eddrtqcr1, 0x63b30002);
232 #endif
233 #endif
234 }
235
236 /*
237  * This erratum requires a register write before being Memory
238  * controller 3 being enabled.
239  */
240 static void erratum_a008514(void)
241 {
242 #ifdef CONFIG_SYS_FSL_ERRATUM_A008514
243         u32 *eddrtqcr1;
244
245 #ifdef CONFIG_SYS_FSL_DCSR_DDR3_ADDR
246         eddrtqcr1 = (void *)CONFIG_SYS_FSL_DCSR_DDR3_ADDR + 0x800;
247         out_le32(eddrtqcr1, 0x63b20002);
248 #endif
249 #endif
250 }
251 #ifdef CONFIG_SYS_FSL_ERRATUM_A009635
252 #define PLATFORM_CYCLE_ENV_VAR  "a009635_interval_val"
253
254 static unsigned long get_internval_val_mhz(void)
255 {
256         char *interval = env_get(PLATFORM_CYCLE_ENV_VAR);
257         /*
258          *  interval is the number of platform cycles(MHz) between
259          *  wake up events generated by EPU.
260          */
261         ulong interval_mhz = get_bus_freq(0) / (1000 * 1000);
262
263         if (interval)
264                 interval_mhz = simple_strtoul(interval, NULL, 10);
265
266         return interval_mhz;
267 }
268
269 void erratum_a009635(void)
270 {
271         u32 val;
272         unsigned long interval_mhz = get_internval_val_mhz();
273
274         if (!interval_mhz)
275                 return;
276
277         val = in_le32(DCSR_CGACRE5);
278         writel(val | 0x00000200, DCSR_CGACRE5);
279
280         val = in_le32(EPU_EPCMPR5);
281         writel(interval_mhz, EPU_EPCMPR5);
282         val = in_le32(EPU_EPCCR5);
283         writel(val | 0x82820000, EPU_EPCCR5);
284         val = in_le32(EPU_EPSMCR5);
285         writel(val | 0x002f0000, EPU_EPSMCR5);
286         val = in_le32(EPU_EPECR5);
287         writel(val | 0x20000000, EPU_EPECR5);
288         val = in_le32(EPU_EPGCR);
289         writel(val | 0x80000000, EPU_EPGCR);
290 }
291 #endif  /* CONFIG_SYS_FSL_ERRATUM_A009635 */
292
293 static void erratum_rcw_src(void)
294 {
295 #if defined(CONFIG_SPL) && defined(CONFIG_NAND_BOOT)
296         u32 __iomem *dcfg_ccsr = (u32 __iomem *)DCFG_BASE;
297         u32 __iomem *dcfg_dcsr = (u32 __iomem *)DCFG_DCSR_BASE;
298         u32 val;
299
300         val = in_le32(dcfg_ccsr + DCFG_PORSR1 / 4);
301         val &= ~DCFG_PORSR1_RCW_SRC;
302         val |= DCFG_PORSR1_RCW_SRC_NOR;
303         out_le32(dcfg_dcsr + DCFG_DCSR_PORCR1 / 4, val);
304 #endif
305 }
306
307 #define I2C_DEBUG_REG 0x6
308 #define I2C_GLITCH_EN 0x8
309 /*
310  * This erratum requires setting glitch_en bit to enable
311  * digital glitch filter to improve clock stability.
312  */
313 #ifdef CONFIG_SYS_FSL_ERRATUM_A009203
314 static void erratum_a009203(void)
315 {
316 #ifdef CONFIG_SYS_I2C
317         u8 __iomem *ptr;
318 #ifdef I2C1_BASE_ADDR
319         ptr = (u8 __iomem *)(I2C1_BASE_ADDR + I2C_DEBUG_REG);
320
321         writeb(I2C_GLITCH_EN, ptr);
322 #endif
323 #ifdef I2C2_BASE_ADDR
324         ptr = (u8 __iomem *)(I2C2_BASE_ADDR + I2C_DEBUG_REG);
325
326         writeb(I2C_GLITCH_EN, ptr);
327 #endif
328 #ifdef I2C3_BASE_ADDR
329         ptr = (u8 __iomem *)(I2C3_BASE_ADDR + I2C_DEBUG_REG);
330
331         writeb(I2C_GLITCH_EN, ptr);
332 #endif
333 #ifdef I2C4_BASE_ADDR
334         ptr = (u8 __iomem *)(I2C4_BASE_ADDR + I2C_DEBUG_REG);
335
336         writeb(I2C_GLITCH_EN, ptr);
337 #endif
338 #endif
339 }
340 #endif
341
342 void bypass_smmu(void)
343 {
344         u32 val;
345         val = (in_le32(SMMU_SCR0) | SCR0_CLIENTPD_MASK) & ~(SCR0_USFCFG_MASK);
346         out_le32(SMMU_SCR0, val);
347         val = (in_le32(SMMU_NSCR0) | SCR0_CLIENTPD_MASK) & ~(SCR0_USFCFG_MASK);
348         out_le32(SMMU_NSCR0, val);
349 }
350 void fsl_lsch3_early_init_f(void)
351 {
352         erratum_rcw_src();
353 #ifdef CONFIG_FSL_IFC
354         init_early_memctl_regs();       /* tighten IFC timing */
355 #endif
356 #ifdef CONFIG_SYS_FSL_ERRATUM_A009203
357         erratum_a009203();
358 #endif
359         erratum_a008514();
360         erratum_a008336();
361         erratum_a009008();
362         erratum_a009798();
363         erratum_a008997();
364         erratum_a009007();
365         erratum_a050106();
366 #ifdef CONFIG_CHAIN_OF_TRUST
367         /* In case of Secure Boot, the IBR configures the SMMU
368         * to allow only Secure transactions.
369         * SMMU must be reset in bypass mode.
370         * Set the ClientPD bit and Clear the USFCFG Bit
371         */
372         if (fsl_check_boot_mode_secure() == 1)
373                 bypass_smmu();
374 #endif
375
376 #if defined(CONFIG_ARCH_LS1088A) || defined(CONFIG_ARCH_LS1028A) || \
377         defined(CONFIG_ARCH_LS2080A) || defined(CONFIG_ARCH_LX2160A)
378         set_icids();
379 #endif
380 }
381
382 /* Get VDD in the unit mV from voltage ID */
383 int get_core_volt_from_fuse(void)
384 {
385         struct ccsr_gur *gur = (void *)(CONFIG_SYS_FSL_GUTS_ADDR);
386         int vdd;
387         u32 fusesr;
388         u8 vid;
389
390         /* get the voltage ID from fuse status register */
391         fusesr = in_le32(&gur->dcfg_fusesr);
392         debug("%s: fusesr = 0x%x\n", __func__, fusesr);
393         vid = (fusesr >> FSL_CHASSIS3_DCFG_FUSESR_ALTVID_SHIFT) &
394                 FSL_CHASSIS3_DCFG_FUSESR_ALTVID_MASK;
395         if ((vid == 0) || (vid == FSL_CHASSIS3_DCFG_FUSESR_ALTVID_MASK)) {
396                 vid = (fusesr >> FSL_CHASSIS3_DCFG_FUSESR_VID_SHIFT) &
397                         FSL_CHASSIS3_DCFG_FUSESR_VID_MASK;
398         }
399         debug("%s: VID = 0x%x\n", __func__, vid);
400         switch (vid) {
401         case 0x00: /* VID isn't supported */
402                 vdd = -EINVAL;
403                 debug("%s: The VID feature is not supported\n", __func__);
404                 break;
405         case 0x08: /* 0.9V silicon */
406                 vdd = 900;
407                 break;
408         case 0x10: /* 1.0V silicon */
409                 vdd = 1000;
410                 break;
411         default:  /* Other core voltage */
412                 vdd = -EINVAL;
413                 debug("%s: The VID(%x) isn't supported\n", __func__, vid);
414                 break;
415         }
416         debug("%s: The required minimum volt of CORE is %dmV\n", __func__, vdd);
417
418         return vdd;
419 }
420
421 #elif defined(CONFIG_FSL_LSCH2)
422 /*
423  * This erratum requires setting a value to eddrtqcr1 to optimal
424  * the DDR performance. The eddrtqcr1 register is in SCFG space
425  * of LS1043A and the offset is 0x157_020c.
426  */
427 #if defined(CONFIG_SYS_FSL_ERRATUM_A009660) \
428         && defined(CONFIG_SYS_FSL_ERRATUM_A008514)
429 #error A009660 and A008514 can not be both enabled.
430 #endif
431
432 static void erratum_a009660(void)
433 {
434 #ifdef CONFIG_SYS_FSL_ERRATUM_A009660
435         u32 *eddrtqcr1 = (void *)CONFIG_SYS_FSL_SCFG_ADDR + 0x20c;
436         out_be32(eddrtqcr1, 0x63b20042);
437 #endif
438 }
439
440 static void erratum_a008850_early(void)
441 {
442 #ifdef CONFIG_SYS_FSL_ERRATUM_A008850
443         /* part 1 of 2 */
444         struct ccsr_cci400 __iomem *cci = (void *)(CONFIG_SYS_IMMR +
445                                                 CONFIG_SYS_CCI400_OFFSET);
446         struct ccsr_ddr __iomem *ddr = (void *)CONFIG_SYS_FSL_DDR_ADDR;
447
448         /* Skip if running at lower exception level */
449         if (current_el() < 3)
450                 return;
451
452         /* disables propagation of barrier transactions to DDRC from CCI400 */
453         out_le32(&cci->ctrl_ord, CCI400_CTRLORD_TERM_BARRIER);
454
455         /* disable the re-ordering in DDRC */
456         ddr_out32(&ddr->eor, DDR_EOR_RD_REOD_DIS | DDR_EOR_WD_REOD_DIS);
457 #endif
458 }
459
460 void erratum_a008850_post(void)
461 {
462 #ifdef CONFIG_SYS_FSL_ERRATUM_A008850
463         /* part 2 of 2 */
464         struct ccsr_cci400 __iomem *cci = (void *)(CONFIG_SYS_IMMR +
465                                                 CONFIG_SYS_CCI400_OFFSET);
466         struct ccsr_ddr __iomem *ddr = (void *)CONFIG_SYS_FSL_DDR_ADDR;
467         u32 tmp;
468
469         /* Skip if running at lower exception level */
470         if (current_el() < 3)
471                 return;
472
473         /* enable propagation of barrier transactions to DDRC from CCI400 */
474         out_le32(&cci->ctrl_ord, CCI400_CTRLORD_EN_BARRIER);
475
476         /* enable the re-ordering in DDRC */
477         tmp = ddr_in32(&ddr->eor);
478         tmp &= ~(DDR_EOR_RD_REOD_DIS | DDR_EOR_WD_REOD_DIS);
479         ddr_out32(&ddr->eor, tmp);
480 #endif
481 }
482
483 #ifdef CONFIG_SYS_FSL_ERRATUM_A010315
484 void erratum_a010315(void)
485 {
486         int i;
487
488         for (i = PCIE1; i <= PCIE4; i++)
489                 if (!is_serdes_configured(i)) {
490                         debug("PCIe%d: disabled all R/W permission!\n", i);
491                         set_pcie_ns_access(i, 0);
492                 }
493 }
494 #endif
495
496 static void erratum_a010539(void)
497 {
498 #if defined(CONFIG_SYS_FSL_ERRATUM_A010539) && defined(CONFIG_QSPI_BOOT)
499         struct ccsr_gur __iomem *gur = (void *)(CONFIG_SYS_FSL_GUTS_ADDR);
500         u32 porsr1;
501
502         porsr1 = in_be32(&gur->porsr1);
503         porsr1 &= ~FSL_CHASSIS2_CCSR_PORSR1_RCW_MASK;
504         out_be32((void *)(CONFIG_SYS_DCSR_DCFG_ADDR + DCFG_DCSR_PORCR1),
505                  porsr1);
506         out_be32((void *)(CONFIG_SYS_FSL_SCFG_ADDR + 0x1a8), 0xffffffff);
507 #endif
508 }
509
510 /* Get VDD in the unit mV from voltage ID */
511 int get_core_volt_from_fuse(void)
512 {
513         struct ccsr_gur *gur = (void *)(CONFIG_SYS_FSL_GUTS_ADDR);
514         int vdd;
515         u32 fusesr;
516         u8 vid;
517
518         fusesr = in_be32(&gur->dcfg_fusesr);
519         debug("%s: fusesr = 0x%x\n", __func__, fusesr);
520         vid = (fusesr >> FSL_CHASSIS2_DCFG_FUSESR_ALTVID_SHIFT) &
521                 FSL_CHASSIS2_DCFG_FUSESR_ALTVID_MASK;
522         if ((vid == 0) || (vid == FSL_CHASSIS2_DCFG_FUSESR_ALTVID_MASK)) {
523                 vid = (fusesr >> FSL_CHASSIS2_DCFG_FUSESR_VID_SHIFT) &
524                         FSL_CHASSIS2_DCFG_FUSESR_VID_MASK;
525         }
526         debug("%s: VID = 0x%x\n", __func__, vid);
527         switch (vid) {
528         case 0x00: /* VID isn't supported */
529                 vdd = -EINVAL;
530                 debug("%s: The VID feature is not supported\n", __func__);
531                 break;
532         case 0x08: /* 0.9V silicon */
533                 vdd = 900;
534                 break;
535         case 0x10: /* 1.0V silicon */
536                 vdd = 1000;
537                 break;
538         default:  /* Other core voltage */
539                 vdd = -EINVAL;
540                 printf("%s: The VID(%x) isn't supported\n", __func__, vid);
541                 break;
542         }
543         debug("%s: The required minimum volt of CORE is %dmV\n", __func__, vdd);
544
545         return vdd;
546 }
547
548 __weak int board_switch_core_volt(u32 vdd)
549 {
550         return 0;
551 }
552
553 static int setup_core_volt(u32 vdd)
554 {
555         return board_setup_core_volt(vdd);
556 }
557
558 #ifdef CONFIG_SYS_FSL_DDR
559 static void ddr_enable_0v9_volt(bool en)
560 {
561         struct ccsr_ddr __iomem *ddr = (void *)CONFIG_SYS_FSL_DDR_ADDR;
562         u32 tmp;
563
564         tmp = ddr_in32(&ddr->ddr_cdr1);
565
566         if (en)
567                 tmp |= DDR_CDR1_V0PT9_EN;
568         else
569                 tmp &= ~DDR_CDR1_V0PT9_EN;
570
571         ddr_out32(&ddr->ddr_cdr1, tmp);
572 }
573 #endif
574
575 int setup_chip_volt(void)
576 {
577         int vdd;
578
579         vdd = get_core_volt_from_fuse();
580         /* Nothing to do for silicons doesn't support VID */
581         if (vdd < 0)
582                 return vdd;
583
584         if (setup_core_volt(vdd))
585                 printf("%s: Switch core VDD to %dmV failed\n", __func__, vdd);
586 #ifdef CONFIG_SYS_HAS_SERDES
587         if (setup_serdes_volt(vdd))
588                 printf("%s: Switch SVDD to %dmV failed\n", __func__, vdd);
589 #endif
590
591 #ifdef CONFIG_SYS_FSL_DDR
592         if (vdd == 900)
593                 ddr_enable_0v9_volt(true);
594 #endif
595
596         return 0;
597 }
598
599 #ifdef CONFIG_FSL_PFE
600 void init_pfe_scfg_dcfg_regs(void)
601 {
602         struct ccsr_scfg *scfg = (struct ccsr_scfg *)CONFIG_SYS_FSL_SCFG_ADDR;
603         u32 ecccr2;
604
605         out_be32(&scfg->pfeasbcr,
606                  in_be32(&scfg->pfeasbcr) | SCFG_PFEASBCR_AWCACHE0);
607         out_be32(&scfg->pfebsbcr,
608                  in_be32(&scfg->pfebsbcr) | SCFG_PFEASBCR_AWCACHE0);
609
610         /* CCI-400 QoS settings for PFE */
611         out_be32(&scfg->wr_qos1, (unsigned int)(SCFG_WR_QOS1_PFE1_QOS
612                  | SCFG_WR_QOS1_PFE2_QOS));
613         out_be32(&scfg->rd_qos1, (unsigned int)(SCFG_RD_QOS1_PFE1_QOS
614                  | SCFG_RD_QOS1_PFE2_QOS));
615
616         ecccr2 = in_be32(CONFIG_SYS_DCSR_DCFG_ADDR + DCFG_DCSR_ECCCR2);
617         out_be32((void *)CONFIG_SYS_DCSR_DCFG_ADDR + DCFG_DCSR_ECCCR2,
618                  ecccr2 | (unsigned int)DISABLE_PFE_ECC);
619 }
620 #endif
621
622 void fsl_lsch2_early_init_f(void)
623 {
624         struct ccsr_cci400 *cci = (struct ccsr_cci400 *)(CONFIG_SYS_IMMR +
625                                         CONFIG_SYS_CCI400_OFFSET);
626         struct ccsr_scfg *scfg = (struct ccsr_scfg *)CONFIG_SYS_FSL_SCFG_ADDR;
627 #if defined(CONFIG_FSL_QSPI) && defined(CONFIG_TFABOOT)
628         enum boot_src src;
629 #endif
630
631 #ifdef CONFIG_LAYERSCAPE_NS_ACCESS
632         enable_layerscape_ns_access();
633 #endif
634
635 #ifdef CONFIG_FSL_IFC
636         init_early_memctl_regs();       /* tighten IFC timing */
637 #endif
638
639 #if defined(CONFIG_FSL_QSPI) && defined(CONFIG_TFABOOT)
640         src = get_boot_src();
641         if (src != BOOT_SOURCE_QSPI_NOR)
642                 out_be32(&scfg->qspi_cfg, SCFG_QSPI_CLKSEL);
643 #else
644 #if defined(CONFIG_FSL_QSPI) && !defined(CONFIG_QSPI_BOOT)
645         out_be32(&scfg->qspi_cfg, SCFG_QSPI_CLKSEL);
646 #endif
647 #endif
648         /* Make SEC reads and writes snoopable */
649 #if defined(CONFIG_ARCH_LS1043A) || defined(CONFIG_ARCH_LS1046A)
650         setbits_be32(&scfg->snpcnfgcr, SCFG_SNPCNFGCR_SECRDSNP |
651                         SCFG_SNPCNFGCR_SECWRSNP | SCFG_SNPCNFGCR_USB1RDSNP |
652                         SCFG_SNPCNFGCR_USB1WRSNP | SCFG_SNPCNFGCR_USB2RDSNP |
653                         SCFG_SNPCNFGCR_USB2WRSNP | SCFG_SNPCNFGCR_USB3RDSNP |
654                         SCFG_SNPCNFGCR_USB3WRSNP | SCFG_SNPCNFGCR_SATARDSNP |
655                         SCFG_SNPCNFGCR_SATAWRSNP);
656 #elif defined(CONFIG_ARCH_LS1012A)
657         setbits_be32(&scfg->snpcnfgcr, SCFG_SNPCNFGCR_SECRDSNP |
658                         SCFG_SNPCNFGCR_SECWRSNP | SCFG_SNPCNFGCR_USB1RDSNP |
659                         SCFG_SNPCNFGCR_USB1WRSNP | SCFG_SNPCNFGCR_SATARDSNP |
660                         SCFG_SNPCNFGCR_SATAWRSNP);
661 #else
662         setbits_be32(&scfg->snpcnfgcr, SCFG_SNPCNFGCR_SECRDSNP |
663                      SCFG_SNPCNFGCR_SECWRSNP |
664                      SCFG_SNPCNFGCR_SATARDSNP |
665                      SCFG_SNPCNFGCR_SATAWRSNP);
666 #endif
667
668         /*
669          * Enable snoop requests and DVM message requests for
670          * Slave insterface S4 (A53 core cluster)
671          */
672         if (current_el() == 3) {
673                 out_le32(&cci->slave[4].snoop_ctrl,
674                          CCI400_DVM_MESSAGE_REQ_EN | CCI400_SNOOP_REQ_EN);
675         }
676
677         /*
678          * Program Central Security Unit (CSU) to grant access
679          * permission for USB 2.0 controller
680          */
681 #if defined(CONFIG_ARCH_LS1012A) && defined(CONFIG_USB_EHCI_FSL)
682         if (current_el() == 3)
683                 set_devices_ns_access(CSU_CSLX_USB_2, CSU_ALL_RW);
684 #endif
685         /* Erratum */
686         erratum_a008850_early(); /* part 1 of 2 */
687         erratum_a009660();
688         erratum_a010539();
689         erratum_a009008();
690         erratum_a009798();
691         erratum_a008997();
692         erratum_a009007();
693
694 #if defined(CONFIG_ARCH_LS1043A) || defined(CONFIG_ARCH_LS1046A)
695         set_icids();
696 #endif
697 }
698 #endif
699
700 #ifdef CONFIG_FSPI_AHB_EN_4BYTE
701 int fspi_ahb_init(void)
702 {
703         /* Enable 4bytes address support and fast read */
704         u32 *fspi_lut, lut_key, *fspi_key;
705
706         fspi_key = (void *)SYS_NXP_FSPI_ADDR + SYS_NXP_FSPI_LUTKEY_BASE_ADDR;
707         fspi_lut = (void *)SYS_NXP_FSPI_ADDR + SYS_NXP_FSPI_LUT_BASE_ADDR;
708
709         lut_key = in_be32(fspi_key);
710
711         if (lut_key == SYS_NXP_FSPI_LUTKEY) {
712                 /* That means the register is BE */
713                 out_be32(fspi_key, SYS_NXP_FSPI_LUTKEY);
714                 /* Unlock the lut table */
715                 out_be32(fspi_key + 1, SYS_NXP_FSPI_LUTCR_UNLOCK);
716                 /* Create READ LUT */
717                 out_be32(fspi_lut, 0x0820040c);
718                 out_be32(fspi_lut + 1, 0x24003008);
719                 out_be32(fspi_lut + 2, 0x00000000);
720                 /* Lock the lut table */
721                 out_be32(fspi_key, SYS_NXP_FSPI_LUTKEY);
722                 out_be32(fspi_key + 1, SYS_NXP_FSPI_LUTCR_LOCK);
723         } else {
724                 /* That means the register is LE */
725                 out_le32(fspi_key, SYS_NXP_FSPI_LUTKEY);
726                 /* Unlock the lut table */
727                 out_le32(fspi_key + 1, SYS_NXP_FSPI_LUTCR_UNLOCK);
728                 /* Create READ LUT */
729                 out_le32(fspi_lut, 0x0820040c);
730                 out_le32(fspi_lut + 1, 0x24003008);
731                 out_le32(fspi_lut + 2, 0x00000000);
732                 /* Lock the lut table */
733                 out_le32(fspi_key, SYS_NXP_FSPI_LUTKEY);
734                 out_le32(fspi_key + 1, SYS_NXP_FSPI_LUTCR_LOCK);
735         }
736
737         return 0;
738 }
739 #endif
740
741 #ifdef CONFIG_QSPI_AHB_INIT
742 /* Enable 4bytes address support and fast read */
743 int qspi_ahb_init(void)
744 {
745         u32 *qspi_lut, lut_key, *qspi_key;
746
747         qspi_key = (void *)SYS_FSL_QSPI_ADDR + 0x300;
748         qspi_lut = (void *)SYS_FSL_QSPI_ADDR + 0x310;
749
750         lut_key = in_be32(qspi_key);
751
752         if (lut_key == 0x5af05af0) {
753                 /* That means the register is BE */
754                 out_be32(qspi_key, 0x5af05af0);
755                 /* Unlock the lut table */
756                 out_be32(qspi_key + 1, 0x00000002);
757                 out_be32(qspi_lut, 0x0820040c);
758                 out_be32(qspi_lut + 1, 0x1c080c08);
759                 out_be32(qspi_lut + 2, 0x00002400);
760                 /* Lock the lut table */
761                 out_be32(qspi_key, 0x5af05af0);
762                 out_be32(qspi_key + 1, 0x00000001);
763         } else {
764                 /* That means the register is LE */
765                 out_le32(qspi_key, 0x5af05af0);
766                 /* Unlock the lut table */
767                 out_le32(qspi_key + 1, 0x00000002);
768                 out_le32(qspi_lut, 0x0820040c);
769                 out_le32(qspi_lut + 1, 0x1c080c08);
770                 out_le32(qspi_lut + 2, 0x00002400);
771                 /* Lock the lut table */
772                 out_le32(qspi_key, 0x5af05af0);
773                 out_le32(qspi_key + 1, 0x00000001);
774         }
775
776         return 0;
777 }
778 #endif
779
780 #ifdef CONFIG_TFABOOT
781 #define MAX_BOOTCMD_SIZE        512
782
783 int fsl_setenv_bootcmd(void)
784 {
785         int ret;
786         enum boot_src src = get_boot_src();
787         char bootcmd_str[MAX_BOOTCMD_SIZE];
788
789         switch (src) {
790 #ifdef IFC_NOR_BOOTCOMMAND
791         case BOOT_SOURCE_IFC_NOR:
792                 sprintf(bootcmd_str, IFC_NOR_BOOTCOMMAND);
793                 break;
794 #endif
795 #ifdef QSPI_NOR_BOOTCOMMAND
796         case BOOT_SOURCE_QSPI_NOR:
797                 sprintf(bootcmd_str, QSPI_NOR_BOOTCOMMAND);
798                 break;
799 #endif
800 #ifdef XSPI_NOR_BOOTCOMMAND
801         case BOOT_SOURCE_XSPI_NOR:
802                 sprintf(bootcmd_str, XSPI_NOR_BOOTCOMMAND);
803                 break;
804 #endif
805 #ifdef IFC_NAND_BOOTCOMMAND
806         case BOOT_SOURCE_IFC_NAND:
807                 sprintf(bootcmd_str, IFC_NAND_BOOTCOMMAND);
808                 break;
809 #endif
810 #ifdef QSPI_NAND_BOOTCOMMAND
811         case BOOT_SOURCE_QSPI_NAND:
812                 sprintf(bootcmd_str, QSPI_NAND_BOOTCOMMAND);
813                 break;
814 #endif
815 #ifdef XSPI_NAND_BOOTCOMMAND
816         case BOOT_SOURCE_XSPI_NAND:
817                 sprintf(bootcmd_str, XSPI_NAND_BOOTCOMMAND);
818                 break;
819 #endif
820 #ifdef SD_BOOTCOMMAND
821         case BOOT_SOURCE_SD_MMC:
822                 sprintf(bootcmd_str, SD_BOOTCOMMAND);
823                 break;
824 #endif
825 #ifdef SD2_BOOTCOMMAND
826         case BOOT_SOURCE_SD_MMC2:
827                 sprintf(bootcmd_str, SD2_BOOTCOMMAND);
828                 break;
829 #endif
830         default:
831 #ifdef QSPI_NOR_BOOTCOMMAND
832                 sprintf(bootcmd_str, QSPI_NOR_BOOTCOMMAND);
833 #endif
834                 break;
835         }
836
837         ret = env_set("bootcmd", bootcmd_str);
838         if (ret) {
839                 printf("Failed to set bootcmd: ret = %d\n", ret);
840                 return ret;
841         }
842         return 0;
843 }
844
845 int fsl_setenv_mcinitcmd(void)
846 {
847         int ret = 0;
848         enum boot_src src = get_boot_src();
849
850         switch (src) {
851 #ifdef IFC_MC_INIT_CMD
852         case BOOT_SOURCE_IFC_NAND:
853         case BOOT_SOURCE_IFC_NOR:
854         ret = env_set("mcinitcmd", IFC_MC_INIT_CMD);
855                 break;
856 #endif
857 #ifdef QSPI_MC_INIT_CMD
858         case BOOT_SOURCE_QSPI_NAND:
859         case BOOT_SOURCE_QSPI_NOR:
860         ret = env_set("mcinitcmd", QSPI_MC_INIT_CMD);
861                 break;
862 #endif
863 #ifdef XSPI_MC_INIT_CMD
864         case BOOT_SOURCE_XSPI_NAND:
865         case BOOT_SOURCE_XSPI_NOR:
866         ret = env_set("mcinitcmd", XSPI_MC_INIT_CMD);
867                 break;
868 #endif
869 #ifdef SD_MC_INIT_CMD
870         case BOOT_SOURCE_SD_MMC:
871         ret = env_set("mcinitcmd", SD_MC_INIT_CMD);
872                 break;
873 #endif
874 #ifdef SD2_MC_INIT_CMD
875         case BOOT_SOURCE_SD_MMC2:
876         ret = env_set("mcinitcmd", SD2_MC_INIT_CMD);
877                 break;
878 #endif
879         default:
880 #ifdef QSPI_MC_INIT_CMD
881         ret = env_set("mcinitcmd", QSPI_MC_INIT_CMD);
882 #endif
883                 break;
884         }
885
886         if (ret) {
887                 printf("Failed to set mcinitcmd: ret = %d\n", ret);
888                 return ret;
889         }
890         return 0;
891 }
892 #endif
893
894 #ifdef CONFIG_BOARD_LATE_INIT
895 __weak int fsl_board_late_init(void)
896 {
897         return 0;
898 }
899
900 int board_late_init(void)
901 {
902 #ifdef CONFIG_CHAIN_OF_TRUST
903         fsl_setenv_chain_of_trust();
904 #endif
905 #ifdef CONFIG_TFABOOT
906         /*
907          * check if gd->env_addr is default_environment; then setenv bootcmd
908          * and mcinitcmd.
909          */
910 #ifdef CONFIG_SYS_RELOC_GD_ENV_ADDR
911         if (gd->env_addr == (ulong)&default_environment[0]) {
912 #else
913         if (gd->env_addr + gd->reloc_off == (ulong)&default_environment[0]) {
914 #endif
915                 fsl_setenv_bootcmd();
916                 fsl_setenv_mcinitcmd();
917         }
918
919         /*
920          * If the boot mode is secure, default environment is not present then
921          * setenv command needs to be run by default
922          */
923 #ifdef CONFIG_CHAIN_OF_TRUST
924         if ((fsl_check_boot_mode_secure() == 1)) {
925                 fsl_setenv_bootcmd();
926                 fsl_setenv_mcinitcmd();
927         }
928 #endif
929 #endif
930 #ifdef CONFIG_QSPI_AHB_INIT
931         qspi_ahb_init();
932 #endif
933 #ifdef CONFIG_FSPI_AHB_EN_4BYTE
934         fspi_ahb_init();
935 #endif
936
937         return fsl_board_late_init();
938 }
939 #endif