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