ARM: dts: stm32: Add DHSOM based DRC02 board
[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 #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  * This erratum requires setting a value to eddrtqcr1 to optimal
450  * the DDR performance. The eddrtqcr1 register is in SCFG space
451  * of LS1043A and the offset is 0x157_020c.
452  */
453 #if defined(CONFIG_SYS_FSL_ERRATUM_A009660) \
454         && defined(CONFIG_SYS_FSL_ERRATUM_A008514)
455 #error A009660 and A008514 can not be both enabled.
456 #endif
457
458 static void erratum_a009660(void)
459 {
460 #ifdef CONFIG_SYS_FSL_ERRATUM_A009660
461         u32 *eddrtqcr1 = (void *)CONFIG_SYS_FSL_SCFG_ADDR + 0x20c;
462         out_be32(eddrtqcr1, 0x63b20042);
463 #endif
464 }
465
466 static void erratum_a008850_early(void)
467 {
468 #ifdef CONFIG_SYS_FSL_ERRATUM_A008850
469         /* part 1 of 2 */
470         struct ccsr_cci400 __iomem *cci = (void *)(CONFIG_SYS_IMMR +
471                                                 CONFIG_SYS_CCI400_OFFSET);
472         struct ccsr_ddr __iomem *ddr = (void *)CONFIG_SYS_FSL_DDR_ADDR;
473
474         /* Skip if running at lower exception level */
475         if (current_el() < 3)
476                 return;
477
478         /* disables propagation of barrier transactions to DDRC from CCI400 */
479         out_le32(&cci->ctrl_ord, CCI400_CTRLORD_TERM_BARRIER);
480
481         /* disable the re-ordering in DDRC */
482         ddr_out32(&ddr->eor, DDR_EOR_RD_REOD_DIS | DDR_EOR_WD_REOD_DIS);
483 #endif
484 }
485
486 void erratum_a008850_post(void)
487 {
488 #ifdef CONFIG_SYS_FSL_ERRATUM_A008850
489         /* part 2 of 2 */
490         struct ccsr_cci400 __iomem *cci = (void *)(CONFIG_SYS_IMMR +
491                                                 CONFIG_SYS_CCI400_OFFSET);
492         struct ccsr_ddr __iomem *ddr = (void *)CONFIG_SYS_FSL_DDR_ADDR;
493         u32 tmp;
494
495         /* Skip if running at lower exception level */
496         if (current_el() < 3)
497                 return;
498
499         /* enable propagation of barrier transactions to DDRC from CCI400 */
500         out_le32(&cci->ctrl_ord, CCI400_CTRLORD_EN_BARRIER);
501
502         /* enable the re-ordering in DDRC */
503         tmp = ddr_in32(&ddr->eor);
504         tmp &= ~(DDR_EOR_RD_REOD_DIS | DDR_EOR_WD_REOD_DIS);
505         ddr_out32(&ddr->eor, tmp);
506 #endif
507 }
508
509 #ifdef CONFIG_SYS_FSL_ERRATUM_A010315
510 void erratum_a010315(void)
511 {
512         int i;
513
514         for (i = PCIE1; i <= PCIE4; i++)
515                 if (!is_serdes_configured(i)) {
516                         debug("PCIe%d: disabled all R/W permission!\n", i);
517                         set_pcie_ns_access(i, 0);
518                 }
519 }
520 #endif
521
522 static void erratum_a010539(void)
523 {
524 #if defined(CONFIG_SYS_FSL_ERRATUM_A010539) && defined(CONFIG_QSPI_BOOT)
525         struct ccsr_gur __iomem *gur = (void *)(CONFIG_SYS_FSL_GUTS_ADDR);
526         u32 porsr1;
527
528         porsr1 = in_be32(&gur->porsr1);
529         porsr1 &= ~FSL_CHASSIS2_CCSR_PORSR1_RCW_MASK;
530         out_be32((void *)(CONFIG_SYS_DCSR_DCFG_ADDR + DCFG_DCSR_PORCR1),
531                  porsr1);
532         out_be32((void *)(CONFIG_SYS_FSL_SCFG_ADDR + 0x1a8), 0xffffffff);
533 #endif
534 }
535
536 /* Get VDD in the unit mV from voltage ID */
537 int get_core_volt_from_fuse(void)
538 {
539         struct ccsr_gur *gur = (void *)(CONFIG_SYS_FSL_GUTS_ADDR);
540         int vdd;
541         u32 fusesr;
542         u8 vid;
543
544         fusesr = in_be32(&gur->dcfg_fusesr);
545         debug("%s: fusesr = 0x%x\n", __func__, fusesr);
546         vid = (fusesr >> FSL_CHASSIS2_DCFG_FUSESR_ALTVID_SHIFT) &
547                 FSL_CHASSIS2_DCFG_FUSESR_ALTVID_MASK;
548         if ((vid == 0) || (vid == FSL_CHASSIS2_DCFG_FUSESR_ALTVID_MASK)) {
549                 vid = (fusesr >> FSL_CHASSIS2_DCFG_FUSESR_VID_SHIFT) &
550                         FSL_CHASSIS2_DCFG_FUSESR_VID_MASK;
551         }
552         debug("%s: VID = 0x%x\n", __func__, vid);
553         switch (vid) {
554         case 0x00: /* VID isn't supported */
555                 vdd = -EINVAL;
556                 debug("%s: The VID feature is not supported\n", __func__);
557                 break;
558         case 0x08: /* 0.9V silicon */
559                 vdd = 900;
560                 break;
561         case 0x10: /* 1.0V silicon */
562                 vdd = 1000;
563                 break;
564         default:  /* Other core voltage */
565                 vdd = -EINVAL;
566                 printf("%s: The VID(%x) isn't supported\n", __func__, vid);
567                 break;
568         }
569         debug("%s: The required minimum volt of CORE is %dmV\n", __func__, vdd);
570
571         return vdd;
572 }
573
574 __weak int board_switch_core_volt(u32 vdd)
575 {
576         return 0;
577 }
578
579 static int setup_core_volt(u32 vdd)
580 {
581         return board_setup_core_volt(vdd);
582 }
583
584 #ifdef CONFIG_SYS_FSL_DDR
585 static void ddr_enable_0v9_volt(bool en)
586 {
587         struct ccsr_ddr __iomem *ddr = (void *)CONFIG_SYS_FSL_DDR_ADDR;
588         u32 tmp;
589
590         tmp = ddr_in32(&ddr->ddr_cdr1);
591
592         if (en)
593                 tmp |= DDR_CDR1_V0PT9_EN;
594         else
595                 tmp &= ~DDR_CDR1_V0PT9_EN;
596
597         ddr_out32(&ddr->ddr_cdr1, tmp);
598 }
599 #endif
600
601 int setup_chip_volt(void)
602 {
603         int vdd;
604
605         vdd = get_core_volt_from_fuse();
606         /* Nothing to do for silicons doesn't support VID */
607         if (vdd < 0)
608                 return vdd;
609
610         if (setup_core_volt(vdd))
611                 printf("%s: Switch core VDD to %dmV failed\n", __func__, vdd);
612 #ifdef CONFIG_SYS_HAS_SERDES
613         if (setup_serdes_volt(vdd))
614                 printf("%s: Switch SVDD to %dmV failed\n", __func__, vdd);
615 #endif
616
617 #ifdef CONFIG_SYS_FSL_DDR
618         if (vdd == 900)
619                 ddr_enable_0v9_volt(true);
620 #endif
621
622         return 0;
623 }
624
625 #ifdef CONFIG_FSL_PFE
626 void init_pfe_scfg_dcfg_regs(void)
627 {
628         struct ccsr_scfg *scfg = (struct ccsr_scfg *)CONFIG_SYS_FSL_SCFG_ADDR;
629         u32 ecccr2;
630
631         out_be32(&scfg->pfeasbcr,
632                  in_be32(&scfg->pfeasbcr) | SCFG_PFEASBCR_AWCACHE0);
633         out_be32(&scfg->pfebsbcr,
634                  in_be32(&scfg->pfebsbcr) | SCFG_PFEASBCR_AWCACHE0);
635
636         /* CCI-400 QoS settings for PFE */
637         out_be32(&scfg->wr_qos1, (unsigned int)(SCFG_WR_QOS1_PFE1_QOS
638                  | SCFG_WR_QOS1_PFE2_QOS));
639         out_be32(&scfg->rd_qos1, (unsigned int)(SCFG_RD_QOS1_PFE1_QOS
640                  | SCFG_RD_QOS1_PFE2_QOS));
641
642         ecccr2 = in_be32(CONFIG_SYS_DCSR_DCFG_ADDR + DCFG_DCSR_ECCCR2);
643         out_be32((void *)CONFIG_SYS_DCSR_DCFG_ADDR + DCFG_DCSR_ECCCR2,
644                  ecccr2 | (unsigned int)DISABLE_PFE_ECC);
645 }
646 #endif
647
648 void fsl_lsch2_early_init_f(void)
649 {
650         struct ccsr_cci400 *cci = (struct ccsr_cci400 *)(CONFIG_SYS_IMMR +
651                                         CONFIG_SYS_CCI400_OFFSET);
652         struct ccsr_scfg *scfg = (struct ccsr_scfg *)CONFIG_SYS_FSL_SCFG_ADDR;
653 #if defined(CONFIG_FSL_QSPI) && defined(CONFIG_TFABOOT)
654         enum boot_src src;
655 #endif
656
657 #ifdef CONFIG_LAYERSCAPE_NS_ACCESS
658         enable_layerscape_ns_access();
659 #endif
660
661 #ifdef CONFIG_FSL_IFC
662         init_early_memctl_regs();       /* tighten IFC timing */
663 #endif
664
665 #if defined(CONFIG_FSL_QSPI) && defined(CONFIG_TFABOOT)
666         src = get_boot_src();
667         if (src != BOOT_SOURCE_QSPI_NOR)
668                 out_be32(&scfg->qspi_cfg, SCFG_QSPI_CLKSEL);
669 #else
670 #if defined(CONFIG_FSL_QSPI) && !defined(CONFIG_QSPI_BOOT)
671         out_be32(&scfg->qspi_cfg, SCFG_QSPI_CLKSEL);
672 #endif
673 #endif
674         /* Make SEC reads and writes snoopable */
675 #if defined(CONFIG_ARCH_LS1043A) || defined(CONFIG_ARCH_LS1046A)
676         setbits_be32(&scfg->snpcnfgcr, SCFG_SNPCNFGCR_SECRDSNP |
677                         SCFG_SNPCNFGCR_SECWRSNP | SCFG_SNPCNFGCR_USB1RDSNP |
678                         SCFG_SNPCNFGCR_USB1WRSNP | SCFG_SNPCNFGCR_USB2RDSNP |
679                         SCFG_SNPCNFGCR_USB2WRSNP | SCFG_SNPCNFGCR_USB3RDSNP |
680                         SCFG_SNPCNFGCR_USB3WRSNP | SCFG_SNPCNFGCR_SATARDSNP |
681                         SCFG_SNPCNFGCR_SATAWRSNP);
682 #elif defined(CONFIG_ARCH_LS1012A)
683         setbits_be32(&scfg->snpcnfgcr, SCFG_SNPCNFGCR_SECRDSNP |
684                         SCFG_SNPCNFGCR_SECWRSNP | SCFG_SNPCNFGCR_USB1RDSNP |
685                         SCFG_SNPCNFGCR_USB1WRSNP | SCFG_SNPCNFGCR_SATARDSNP |
686                         SCFG_SNPCNFGCR_SATAWRSNP);
687 #else
688         setbits_be32(&scfg->snpcnfgcr, SCFG_SNPCNFGCR_SECRDSNP |
689                      SCFG_SNPCNFGCR_SECWRSNP |
690                      SCFG_SNPCNFGCR_SATARDSNP |
691                      SCFG_SNPCNFGCR_SATAWRSNP);
692 #endif
693
694         /*
695          * Enable snoop requests and DVM message requests for
696          * Slave insterface S4 (A53 core cluster)
697          */
698         if (current_el() == 3) {
699                 out_le32(&cci->slave[4].snoop_ctrl,
700                          CCI400_DVM_MESSAGE_REQ_EN | CCI400_SNOOP_REQ_EN);
701         }
702
703         /*
704          * Program Central Security Unit (CSU) to grant access
705          * permission for USB 2.0 controller
706          */
707 #if defined(CONFIG_ARCH_LS1012A) && defined(CONFIG_USB_EHCI_FSL)
708         if (current_el() == 3)
709                 set_devices_ns_access(CSU_CSLX_USB_2, CSU_ALL_RW);
710 #endif
711         /* Erratum */
712         erratum_a008850_early(); /* part 1 of 2 */
713         erratum_a009660();
714         erratum_a010539();
715         erratum_a009008();
716         erratum_a009798();
717         erratum_a008997();
718         erratum_a009007();
719
720 #if defined(CONFIG_ARCH_LS1043A) || defined(CONFIG_ARCH_LS1046A)
721         set_icids();
722 #endif
723 }
724 #endif
725
726 #ifdef CONFIG_FSPI_AHB_EN_4BYTE
727 int fspi_ahb_init(void)
728 {
729         /* Enable 4bytes address support and fast read */
730         u32 *fspi_lut, lut_key, *fspi_key;
731
732         fspi_key = (void *)SYS_NXP_FSPI_ADDR + SYS_NXP_FSPI_LUTKEY_BASE_ADDR;
733         fspi_lut = (void *)SYS_NXP_FSPI_ADDR + SYS_NXP_FSPI_LUT_BASE_ADDR;
734
735         lut_key = in_be32(fspi_key);
736
737         if (lut_key == SYS_NXP_FSPI_LUTKEY) {
738                 /* That means the register is BE */
739                 out_be32(fspi_key, SYS_NXP_FSPI_LUTKEY);
740                 /* Unlock the lut table */
741                 out_be32(fspi_key + 1, SYS_NXP_FSPI_LUTCR_UNLOCK);
742                 /* Create READ LUT */
743                 out_be32(fspi_lut, 0x0820040c);
744                 out_be32(fspi_lut + 1, 0x24003008);
745                 out_be32(fspi_lut + 2, 0x00000000);
746                 /* Lock the lut table */
747                 out_be32(fspi_key, SYS_NXP_FSPI_LUTKEY);
748                 out_be32(fspi_key + 1, SYS_NXP_FSPI_LUTCR_LOCK);
749         } else {
750                 /* That means the register is LE */
751                 out_le32(fspi_key, SYS_NXP_FSPI_LUTKEY);
752                 /* Unlock the lut table */
753                 out_le32(fspi_key + 1, SYS_NXP_FSPI_LUTCR_UNLOCK);
754                 /* Create READ LUT */
755                 out_le32(fspi_lut, 0x0820040c);
756                 out_le32(fspi_lut + 1, 0x24003008);
757                 out_le32(fspi_lut + 2, 0x00000000);
758                 /* Lock the lut table */
759                 out_le32(fspi_key, SYS_NXP_FSPI_LUTKEY);
760                 out_le32(fspi_key + 1, SYS_NXP_FSPI_LUTCR_LOCK);
761         }
762
763         return 0;
764 }
765 #endif
766
767 #ifdef CONFIG_QSPI_AHB_INIT
768 /* Enable 4bytes address support and fast read */
769 int qspi_ahb_init(void)
770 {
771         u32 *qspi_lut, lut_key, *qspi_key;
772
773         qspi_key = (void *)SYS_FSL_QSPI_ADDR + 0x300;
774         qspi_lut = (void *)SYS_FSL_QSPI_ADDR + 0x310;
775
776         lut_key = in_be32(qspi_key);
777
778         if (lut_key == 0x5af05af0) {
779                 /* That means the register is BE */
780                 out_be32(qspi_key, 0x5af05af0);
781                 /* Unlock the lut table */
782                 out_be32(qspi_key + 1, 0x00000002);
783                 out_be32(qspi_lut, 0x0820040c);
784                 out_be32(qspi_lut + 1, 0x1c080c08);
785                 out_be32(qspi_lut + 2, 0x00002400);
786                 /* Lock the lut table */
787                 out_be32(qspi_key, 0x5af05af0);
788                 out_be32(qspi_key + 1, 0x00000001);
789         } else {
790                 /* That means the register is LE */
791                 out_le32(qspi_key, 0x5af05af0);
792                 /* Unlock the lut table */
793                 out_le32(qspi_key + 1, 0x00000002);
794                 out_le32(qspi_lut, 0x0820040c);
795                 out_le32(qspi_lut + 1, 0x1c080c08);
796                 out_le32(qspi_lut + 2, 0x00002400);
797                 /* Lock the lut table */
798                 out_le32(qspi_key, 0x5af05af0);
799                 out_le32(qspi_key + 1, 0x00000001);
800         }
801
802         return 0;
803 }
804 #endif
805
806 #ifdef CONFIG_TFABOOT
807 #define MAX_BOOTCMD_SIZE        512
808
809 int fsl_setenv_bootcmd(void)
810 {
811         int ret;
812         enum boot_src src = get_boot_src();
813         char bootcmd_str[MAX_BOOTCMD_SIZE];
814
815         switch (src) {
816 #ifdef IFC_NOR_BOOTCOMMAND
817         case BOOT_SOURCE_IFC_NOR:
818                 sprintf(bootcmd_str, IFC_NOR_BOOTCOMMAND);
819                 break;
820 #endif
821 #ifdef QSPI_NOR_BOOTCOMMAND
822         case BOOT_SOURCE_QSPI_NOR:
823                 sprintf(bootcmd_str, QSPI_NOR_BOOTCOMMAND);
824                 break;
825 #endif
826 #ifdef XSPI_NOR_BOOTCOMMAND
827         case BOOT_SOURCE_XSPI_NOR:
828                 sprintf(bootcmd_str, XSPI_NOR_BOOTCOMMAND);
829                 break;
830 #endif
831 #ifdef IFC_NAND_BOOTCOMMAND
832         case BOOT_SOURCE_IFC_NAND:
833                 sprintf(bootcmd_str, IFC_NAND_BOOTCOMMAND);
834                 break;
835 #endif
836 #ifdef QSPI_NAND_BOOTCOMMAND
837         case BOOT_SOURCE_QSPI_NAND:
838                 sprintf(bootcmd_str, QSPI_NAND_BOOTCOMMAND);
839                 break;
840 #endif
841 #ifdef XSPI_NAND_BOOTCOMMAND
842         case BOOT_SOURCE_XSPI_NAND:
843                 sprintf(bootcmd_str, XSPI_NAND_BOOTCOMMAND);
844                 break;
845 #endif
846 #ifdef SD_BOOTCOMMAND
847         case BOOT_SOURCE_SD_MMC:
848                 sprintf(bootcmd_str, SD_BOOTCOMMAND);
849                 break;
850 #endif
851 #ifdef SD2_BOOTCOMMAND
852         case BOOT_SOURCE_SD_MMC2:
853                 sprintf(bootcmd_str, SD2_BOOTCOMMAND);
854                 break;
855 #endif
856         default:
857 #ifdef QSPI_NOR_BOOTCOMMAND
858                 sprintf(bootcmd_str, QSPI_NOR_BOOTCOMMAND);
859 #endif
860                 break;
861         }
862
863         ret = env_set("bootcmd", bootcmd_str);
864         if (ret) {
865                 printf("Failed to set bootcmd: ret = %d\n", ret);
866                 return ret;
867         }
868         return 0;
869 }
870
871 int fsl_setenv_mcinitcmd(void)
872 {
873         int ret = 0;
874         enum boot_src src = get_boot_src();
875
876         switch (src) {
877 #ifdef IFC_MC_INIT_CMD
878         case BOOT_SOURCE_IFC_NAND:
879         case BOOT_SOURCE_IFC_NOR:
880         ret = env_set("mcinitcmd", IFC_MC_INIT_CMD);
881                 break;
882 #endif
883 #ifdef QSPI_MC_INIT_CMD
884         case BOOT_SOURCE_QSPI_NAND:
885         case BOOT_SOURCE_QSPI_NOR:
886         ret = env_set("mcinitcmd", QSPI_MC_INIT_CMD);
887                 break;
888 #endif
889 #ifdef XSPI_MC_INIT_CMD
890         case BOOT_SOURCE_XSPI_NAND:
891         case BOOT_SOURCE_XSPI_NOR:
892         ret = env_set("mcinitcmd", XSPI_MC_INIT_CMD);
893                 break;
894 #endif
895 #ifdef SD_MC_INIT_CMD
896         case BOOT_SOURCE_SD_MMC:
897         ret = env_set("mcinitcmd", SD_MC_INIT_CMD);
898                 break;
899 #endif
900 #ifdef SD2_MC_INIT_CMD
901         case BOOT_SOURCE_SD_MMC2:
902         ret = env_set("mcinitcmd", SD2_MC_INIT_CMD);
903                 break;
904 #endif
905         default:
906 #ifdef QSPI_MC_INIT_CMD
907         ret = env_set("mcinitcmd", QSPI_MC_INIT_CMD);
908 #endif
909                 break;
910         }
911
912         if (ret) {
913                 printf("Failed to set mcinitcmd: ret = %d\n", ret);
914                 return ret;
915         }
916         return 0;
917 }
918 #endif
919
920 #ifdef CONFIG_BOARD_LATE_INIT
921 __weak int fsl_board_late_init(void)
922 {
923         return 0;
924 }
925
926 int board_late_init(void)
927 {
928 #ifdef CONFIG_CHAIN_OF_TRUST
929         fsl_setenv_chain_of_trust();
930 #endif
931 #ifdef CONFIG_TFABOOT
932         /*
933          * check if gd->env_addr is default_environment; then setenv bootcmd
934          * and mcinitcmd.
935          */
936 #ifdef CONFIG_SYS_RELOC_GD_ENV_ADDR
937         if (gd->env_addr == (ulong)&default_environment[0]) {
938 #else
939         if (gd->env_addr + gd->reloc_off == (ulong)&default_environment[0]) {
940 #endif
941                 fsl_setenv_bootcmd();
942                 fsl_setenv_mcinitcmd();
943         }
944
945         /*
946          * If the boot mode is secure, default environment is not present then
947          * setenv command needs to be run by default
948          */
949 #ifdef CONFIG_CHAIN_OF_TRUST
950         if ((fsl_check_boot_mode_secure() == 1)) {
951                 fsl_setenv_bootcmd();
952                 fsl_setenv_mcinitcmd();
953         }
954 #endif
955 #endif
956 #ifdef CONFIG_QSPI_AHB_INIT
957         qspi_ahb_init();
958 #endif
959 #ifdef CONFIG_FSPI_AHB_EN_4BYTE
960         fspi_ahb_init();
961 #endif
962
963         return fsl_board_late_init();
964 }
965 #endif