blob: 3bfdc3f7743110961b96eab0c74c2e2d29d86df5 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Scott Woodf64c98c2015-03-20 19:28:12 -07002/*
Mingkai Hu0e58b512015-10-26 19:47:50 +08003 * Copyright 2014-2015 Freescale Semiconductor
Ran Wang13a84a52021-06-16 17:53:19 +05304 * Copyright 2019-2021 NXP
Scott Woodf64c98c2015-03-20 19:28:12 -07005 */
6
7#include <common.h>
Simon Glass85d65312019-12-28 10:44:58 -07008#include <clock_legacy.h>
Hou Zhiqiang031bb872020-04-28 10:19:32 +08009#include <cpu_func.h>
Simon Glass5e6201b2019-08-01 09:46:51 -060010#include <env.h>
Ashish Kumar11234062017-08-11 11:09:14 +053011#include <fsl_immap.h>
Scott Woodf64c98c2015-03-20 19:28:12 -070012#include <fsl_ifc.h>
Simon Glassa7b51302019-11-14 12:57:46 -070013#include <init.h>
Hou Zhiqiang031bb872020-04-28 10:19:32 +080014#include <linux/sizes.h>
Simon Glass0f2af882020-05-10 11:40:05 -060015#include <log.h>
Hou Zhiqiang4b23ca82016-08-02 19:03:27 +080016#include <asm/arch/fsl_serdes.h>
Mingkai Hu0e58b512015-10-26 19:47:50 +080017#include <asm/arch/soc.h>
Simon Glass274e0b02020-05-10 11:39:56 -060018#include <asm/cache.h>
Scott Woodae1df322015-03-20 19:28:13 -070019#include <asm/io.h>
Scott Wood8e728cd2015-03-24 13:25:02 -070020#include <asm/global_data.h>
Prabhakar Kushwaha22cfe962015-11-05 12:00:14 +053021#include <asm/arch-fsl-layerscape/config.h>
Ran Wang4e7cdcf2018-08-10 15:00:00 +080022#include <asm/arch-fsl-layerscape/ns_access.h>
Laurentiu Tudor512d13e2018-08-09 15:19:46 +030023#include <asm/arch-fsl-layerscape/fsl_icid.h>
Hou Zhiqiang031bb872020-04-28 10:19:32 +080024#include <asm/gic-v3.h>
Hou Zhiqiang4b23ca82016-08-02 19:03:27 +080025#ifdef CONFIG_LAYERSCAPE_NS_ACCESS
Hou Zhiqiang5ac9a5c2016-08-02 19:03:23 +080026#include <fsl_csu.h>
Hou Zhiqiang4b23ca82016-08-02 19:03:27 +080027#endif
Prabhakar Kushwahad169ebe2016-06-03 18:41:31 +053028#ifdef CONFIG_SYS_FSL_DDR
Shengzhou Liuddf060b2016-04-07 16:22:21 +080029#include <fsl_ddr_sdram.h>
30#include <fsl_ddr.h>
Prabhakar Kushwahad169ebe2016-06-03 18:41:31 +053031#endif
Aneesh Bansal39d5b3b2016-01-22 16:37:26 +053032#ifdef CONFIG_CHAIN_OF_TRUST
33#include <fsl_validate.h>
34#endif
Ashish Kumarb25faa22017-08-31 16:12:53 +053035#include <fsl_immap.h>
Ran Wangba7cd0f2020-08-05 15:07:27 +080036#include <dm.h>
Tom Rinifb6abdd2020-10-15 21:44:15 -040037#include <dm/device_compat.h>
Ran Wangba7cd0f2020-08-05 15:07:27 +080038#include <linux/err.h>
Alban Bedel0b8932f2020-11-17 16:20:04 +010039#ifdef CONFIG_GIC_V3_ITS
Pankit Gargbdbf84f2018-11-05 18:01:52 +000040DECLARE_GLOBAL_DATA_PTR;
41#endif
Scott Wood8e728cd2015-03-24 13:25:02 -070042
Hou Zhiqiang031bb872020-04-28 10:19:32 +080043#ifdef CONFIG_GIC_V3_ITS
Michael Walle29b1d332021-10-27 18:54:54 +020044#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)
48static int fdt_add_resv_mem_gic_rd_tables(void *blob, u64 base, size_t size)
49{
50 int err;
51 struct fdt_memory gic_rd_tables;
52
53 gic_rd_tables.start = base;
54 gic_rd_tables.end = base + size - 1;
55 err = fdtdec_add_reserved_memory(blob, "gic-rd-tables", &gic_rd_tables,
56 NULL, 0, NULL, 0);
57 if (err < 0)
58 debug("%s: failed to add reserved memory: %d\n", __func__, err);
59
60 return err;
61}
62
Hou Zhiqiang031bb872020-04-28 10:19:32 +080063int ls_gic_rd_tables_init(void *blob)
64{
Michael Walle29b1d332021-10-27 18:54:54 +020065 u64 gic_lpi_base;
Tom Rini4e87d9d2021-10-27 18:54:53 +020066 int ret;
Hou Zhiqiang031bb872020-04-28 10:19:32 +080067
Michael Walle29b1d332021-10-27 18:54:54 +020068 gic_lpi_base = ALIGN(gd->arch.resv_ram - GIC_LPI_SIZE, SZ_64K);
69 ret = fdt_add_resv_mem_gic_rd_tables(blob, gic_lpi_base, GIC_LPI_SIZE);
70 if (ret)
71 return ret;
72
73 ret = gic_lpi_tables_init(gic_lpi_base, cpu_numcores());
Hou Zhiqiang031bb872020-04-28 10:19:32 +080074 if (ret)
75 debug("%s: failed to init gic-lpi-tables\n", __func__);
76
77 return ret;
78}
79#endif
80
York Suncbe8e1c2016-04-04 11:41:26 -070081bool soc_has_dp_ddr(void)
82{
Tom Rini376b88a2022-10-28 20:27:13 -040083 struct ccsr_gur __iomem *gur = (void *)(CFG_SYS_FSL_GUTS_ADDR);
York Suncbe8e1c2016-04-04 11:41:26 -070084 u32 svr = gur_in32(&gur->svr);
85
Priyanka Jain4a6f1732016-11-17 12:29:55 +053086 /* LS2085A, LS2088A, LS2048A has DP_DDR */
87 if ((SVR_SOC_VER(svr) == SVR_LS2085A) ||
88 (SVR_SOC_VER(svr) == SVR_LS2088A) ||
89 (SVR_SOC_VER(svr) == SVR_LS2048A))
York Suncbe8e1c2016-04-04 11:41:26 -070090 return true;
91
92 return false;
93}
94
95bool soc_has_aiop(void)
96{
Tom Rini376b88a2022-10-28 20:27:13 -040097 struct ccsr_gur __iomem *gur = (void *)(CFG_SYS_FSL_GUTS_ADDR);
York Suncbe8e1c2016-04-04 11:41:26 -070098 u32 svr = gur_in32(&gur->svr);
99
100 /* LS2085A has AIOP */
Prabhakar Kushwahaac7f2422016-06-24 13:48:13 +0530101 if (SVR_SOC_VER(svr) == SVR_LS2085A)
York Suncbe8e1c2016-04-04 11:41:26 -0700102 return true;
103
104 return false;
105}
106
Ran Wangb358b7b2017-09-04 18:46:48 +0800107static inline void set_usb_txvreftune(u32 __iomem *scfg, u32 offset)
108{
109 scfg_clrsetbits32(scfg + offset / 4,
110 0xF << 6,
111 SCFG_USB_TXVREFTUNE << 6);
112}
113
114static void erratum_a009008(void)
115{
116#ifdef CONFIG_SYS_FSL_ERRATUM_A009008
117 u32 __iomem *scfg = (u32 __iomem *)SCFG_BASE;
Ran Wang9e8fabc2017-09-04 18:46:49 +0800118
Ran Wang02dc77b2017-11-13 16:14:48 +0800119#if defined(CONFIG_ARCH_LS1043A) || defined(CONFIG_ARCH_LS1046A) || \
120 defined(CONFIG_ARCH_LS1012A)
Ran Wangb358b7b2017-09-04 18:46:48 +0800121 set_usb_txvreftune(scfg, SCFG_USB3PRM1CR_USB1);
Ran Wang02dc77b2017-11-13 16:14:48 +0800122#if defined(CONFIG_ARCH_LS1043A) || defined(CONFIG_ARCH_LS1046A)
Ran Wangb358b7b2017-09-04 18:46:48 +0800123 set_usb_txvreftune(scfg, SCFG_USB3PRM1CR_USB2);
124 set_usb_txvreftune(scfg, SCFG_USB3PRM1CR_USB3);
Ran Wang02dc77b2017-11-13 16:14:48 +0800125#endif
Ran Wangb358b7b2017-09-04 18:46:48 +0800126#elif defined(CONFIG_ARCH_LS2080A)
127 set_usb_txvreftune(scfg, SCFG_USB3PRM1CR);
128#endif
129#endif /* CONFIG_SYS_FSL_ERRATUM_A009008 */
130}
131
Ran Wang9e8fabc2017-09-04 18:46:49 +0800132static inline void set_usb_sqrxtune(u32 __iomem *scfg, u32 offset)
133{
134 scfg_clrbits32(scfg + offset / 4,
135 SCFG_USB_SQRXTUNE_MASK << 23);
136}
137
138static void erratum_a009798(void)
139{
140#ifdef CONFIG_SYS_FSL_ERRATUM_A009798
141 u32 __iomem *scfg = (u32 __iomem *)SCFG_BASE;
142
Ran Wang02dc77b2017-11-13 16:14:48 +0800143#if defined(CONFIG_ARCH_LS1043A) || defined(CONFIG_ARCH_LS1046A) || \
144 defined(CONFIG_ARCH_LS1012A)
Ran Wang9e8fabc2017-09-04 18:46:49 +0800145 set_usb_sqrxtune(scfg, SCFG_USB3PRM1CR_USB1);
Ran Wang02dc77b2017-11-13 16:14:48 +0800146#if defined(CONFIG_ARCH_LS1043A) || defined(CONFIG_ARCH_LS1046A)
Ran Wang9e8fabc2017-09-04 18:46:49 +0800147 set_usb_sqrxtune(scfg, SCFG_USB3PRM1CR_USB2);
148 set_usb_sqrxtune(scfg, SCFG_USB3PRM1CR_USB3);
Ran Wang02dc77b2017-11-13 16:14:48 +0800149#endif
Ran Wang9e8fabc2017-09-04 18:46:49 +0800150#elif defined(CONFIG_ARCH_LS2080A)
151 set_usb_sqrxtune(scfg, SCFG_USB3PRM1CR);
152#endif
153#endif /* CONFIG_SYS_FSL_ERRATUM_A009798 */
154}
155
Ran Wang02dc77b2017-11-13 16:14:48 +0800156#if defined(CONFIG_ARCH_LS1043A) || defined(CONFIG_ARCH_LS1046A) || \
157 defined(CONFIG_ARCH_LS1012A)
Ran Wange64f7472017-09-04 18:46:50 +0800158static inline void set_usb_pcstxswingfull(u32 __iomem *scfg, u32 offset)
159{
160 scfg_clrsetbits32(scfg + offset / 4,
161 0x7F << 9,
162 SCFG_USB_PCSTXSWINGFULL << 9);
163}
164#endif
165
166static void erratum_a008997(void)
167{
168#ifdef CONFIG_SYS_FSL_ERRATUM_A008997
Ran Wang02dc77b2017-11-13 16:14:48 +0800169#if defined(CONFIG_ARCH_LS1043A) || defined(CONFIG_ARCH_LS1046A) || \
170 defined(CONFIG_ARCH_LS1012A)
Ran Wange64f7472017-09-04 18:46:50 +0800171 u32 __iomem *scfg = (u32 __iomem *)SCFG_BASE;
172
173 set_usb_pcstxswingfull(scfg, SCFG_USB3PRM2CR_USB1);
Ran Wang02dc77b2017-11-13 16:14:48 +0800174#if defined(CONFIG_ARCH_LS1043A) || defined(CONFIG_ARCH_LS1046A)
Ran Wange64f7472017-09-04 18:46:50 +0800175 set_usb_pcstxswingfull(scfg, SCFG_USB3PRM2CR_USB2);
176 set_usb_pcstxswingfull(scfg, SCFG_USB3PRM2CR_USB3);
177#endif
Ran Wange118acb2019-05-14 17:34:56 +0800178#elif defined(CONFIG_ARCH_LS1028A)
179 clrsetbits_le32(DCSR_BASE + DCSR_USB_IOCR1,
180 0x7F << 11,
181 DCSR_USB_PCSTXSWINGFULL << 11);
Ran Wang02dc77b2017-11-13 16:14:48 +0800182#endif
Ran Wange64f7472017-09-04 18:46:50 +0800183#endif /* CONFIG_SYS_FSL_ERRATUM_A008997 */
184}
185
Ran Wang02dc77b2017-11-13 16:14:48 +0800186#if defined(CONFIG_ARCH_LS1043A) || defined(CONFIG_ARCH_LS1046A) || \
187 defined(CONFIG_ARCH_LS1012A)
Ran Wang3ba69482017-09-04 18:46:51 +0800188
189#define PROGRAM_USB_PHY_RX_OVRD_IN_HI(phy) \
190 out_be16((phy) + SCFG_USB_PHY_RX_OVRD_IN_HI, USB_PHY_RX_EQ_VAL_1); \
191 out_be16((phy) + SCFG_USB_PHY_RX_OVRD_IN_HI, USB_PHY_RX_EQ_VAL_2); \
192 out_be16((phy) + SCFG_USB_PHY_RX_OVRD_IN_HI, USB_PHY_RX_EQ_VAL_3); \
193 out_be16((phy) + SCFG_USB_PHY_RX_OVRD_IN_HI, USB_PHY_RX_EQ_VAL_4)
194
Yinbo Zhu5c3767e2019-05-14 17:34:57 +0800195#elif defined(CONFIG_ARCH_LS2080A) || defined(CONFIG_ARCH_LS1088A) || \
Meenakshi Aggarwalccb5d5d2020-10-29 19:16:16 +0530196 defined(CONFIG_ARCH_LS1028A) || defined(CONFIG_ARCH_LX2160A) || \
197 defined(CONFIG_ARCH_LX2162A)
Ran Wang3ba69482017-09-04 18:46:51 +0800198
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
207static void erratum_a009007(void)
208{
Sean Anderson307df402022-02-22 13:38:39 -0500209 if (!IS_ENABLED(CONFIG_SYS_FSL_ERRATUM_A009007))
210 return;
211
Ran Wang02dc77b2017-11-13 16:14:48 +0800212#if defined(CONFIG_ARCH_LS1043A) || defined(CONFIG_ARCH_LS1046A) || \
213 defined(CONFIG_ARCH_LS1012A)
Ran Wang3ba69482017-09-04 18:46:51 +0800214 void __iomem *usb_phy = (void __iomem *)SCFG_USB_PHY1;
215
216 PROGRAM_USB_PHY_RX_OVRD_IN_HI(usb_phy);
Ran Wang02dc77b2017-11-13 16:14:48 +0800217#if defined(CONFIG_ARCH_LS1043A) || defined(CONFIG_ARCH_LS1046A)
Ran Wang3ba69482017-09-04 18:46:51 +0800218 usb_phy = (void __iomem *)SCFG_USB_PHY2;
219 PROGRAM_USB_PHY_RX_OVRD_IN_HI(usb_phy);
220
221 usb_phy = (void __iomem *)SCFG_USB_PHY3;
222 PROGRAM_USB_PHY_RX_OVRD_IN_HI(usb_phy);
Ran Wang02dc77b2017-11-13 16:14:48 +0800223#endif
Yinbo Zhu5c3767e2019-05-14 17:34:57 +0800224#elif defined(CONFIG_ARCH_LS2080A) || defined(CONFIG_ARCH_LS1088A) || \
225 defined(CONFIG_ARCH_LS1028A)
Ran Wang3ba69482017-09-04 18:46:51 +0800226 void __iomem *dcsr = (void __iomem *)DCSR_BASE;
227
228 PROGRAM_USB_PHY_RX_OVRD_IN_HI(dcsr + DCSR_USB_PHY1);
229 PROGRAM_USB_PHY_RX_OVRD_IN_HI(dcsr + DCSR_USB_PHY2);
230#endif /* CONFIG_SYS_FSL_ERRATUM_A009007 */
231}
232
Shengzhou Liua3117ee2016-11-11 18:11:05 +0800233#if defined(CONFIG_FSL_LSCH3)
Ran Wang13a84a52021-06-16 17:53:19 +0530234static void erratum_a050204(void)
Ran Wangd0270dc2019-11-26 11:40:40 +0800235{
Meenakshi Aggarwalccb5d5d2020-10-29 19:16:16 +0530236#if defined(CONFIG_ARCH_LX2160A) || defined(CONFIG_ARCH_LX2162A)
Ran Wangd0270dc2019-11-26 11:40:40 +0800237 void __iomem *dcsr = (void __iomem *)DCSR_BASE;
238
239 PROGRAM_USB_PHY_RX_OVRD_IN_HI(dcsr + DCSR_USB_PHY1);
240 PROGRAM_USB_PHY_RX_OVRD_IN_HI(dcsr + DCSR_USB_PHY2);
241#endif
242}
Yao Yuanfae88052015-12-05 14:59:14 +0800243/*
244 * This erratum requires setting a value to eddrtqcr1 to
245 * optimal the DDR performance.
246 */
247static void erratum_a008336(void)
248{
Shengzhou Liua3117ee2016-11-11 18:11:05 +0800249#ifdef CONFIG_SYS_FSL_ERRATUM_A008336
Yao Yuanfae88052015-12-05 14:59:14 +0800250 u32 *eddrtqcr1;
251
Tom Rini376b88a2022-10-28 20:27:13 -0400252#ifdef CFG_SYS_FSL_DCSR_DDR_ADDR
253 eddrtqcr1 = (void *)CFG_SYS_FSL_DCSR_DDR_ADDR + 0x800;
Shengzhou Liu7beb0c42016-08-26 18:30:38 +0800254 if (fsl_ddr_get_version(0) == 0x50200)
255 out_le32(eddrtqcr1, 0x63b30002);
Yao Yuanfae88052015-12-05 14:59:14 +0800256#endif
Tom Rini376b88a2022-10-28 20:27:13 -0400257#ifdef CFG_SYS_FSL_DCSR_DDR2_ADDR
258 eddrtqcr1 = (void *)CFG_SYS_FSL_DCSR_DDR2_ADDR + 0x800;
Shengzhou Liu7beb0c42016-08-26 18:30:38 +0800259 if (fsl_ddr_get_version(0) == 0x50200)
260 out_le32(eddrtqcr1, 0x63b30002);
Yao Yuanfae88052015-12-05 14:59:14 +0800261#endif
262#endif
263}
264
265/*
266 * This erratum requires a register write before being Memory
267 * controller 3 being enabled.
268 */
269static void erratum_a008514(void)
270{
Shengzhou Liua3117ee2016-11-11 18:11:05 +0800271#ifdef CONFIG_SYS_FSL_ERRATUM_A008514
Yao Yuanfae88052015-12-05 14:59:14 +0800272 u32 *eddrtqcr1;
273
Tom Rini376b88a2022-10-28 20:27:13 -0400274#ifdef CFG_SYS_FSL_DCSR_DDR3_ADDR
275 eddrtqcr1 = (void *)CFG_SYS_FSL_DCSR_DDR3_ADDR + 0x800;
Yao Yuanfae88052015-12-05 14:59:14 +0800276 out_le32(eddrtqcr1, 0x63b20002);
277#endif
278#endif
279}
Prabhakar Kushwaha22cfe962015-11-05 12:00:14 +0530280#ifdef CONFIG_SYS_FSL_ERRATUM_A009635
281#define PLATFORM_CYCLE_ENV_VAR "a009635_interval_val"
282
283static unsigned long get_internval_val_mhz(void)
284{
Simon Glass64b723f2017-08-03 12:22:12 -0600285 char *interval = env_get(PLATFORM_CYCLE_ENV_VAR);
Prabhakar Kushwaha22cfe962015-11-05 12:00:14 +0530286 /*
287 * interval is the number of platform cycles(MHz) between
288 * wake up events generated by EPU.
289 */
290 ulong interval_mhz = get_bus_freq(0) / (1000 * 1000);
291
292 if (interval)
Simon Glassff9b9032021-07-24 09:03:30 -0600293 interval_mhz = dectoul(interval, NULL);
Prabhakar Kushwaha22cfe962015-11-05 12:00:14 +0530294
295 return interval_mhz;
296}
297
298void erratum_a009635(void)
299{
300 u32 val;
301 unsigned long interval_mhz = get_internval_val_mhz();
302
303 if (!interval_mhz)
304 return;
305
306 val = in_le32(DCSR_CGACRE5);
307 writel(val | 0x00000200, DCSR_CGACRE5);
308
309 val = in_le32(EPU_EPCMPR5);
310 writel(interval_mhz, EPU_EPCMPR5);
311 val = in_le32(EPU_EPCCR5);
312 writel(val | 0x82820000, EPU_EPCCR5);
313 val = in_le32(EPU_EPSMCR5);
314 writel(val | 0x002f0000, EPU_EPSMCR5);
315 val = in_le32(EPU_EPECR5);
316 writel(val | 0x20000000, EPU_EPECR5);
317 val = in_le32(EPU_EPGCR);
318 writel(val | 0x80000000, EPU_EPGCR);
319}
320#endif /* CONFIG_SYS_FSL_ERRATUM_A009635 */
321
Scott Wood8e728cd2015-03-24 13:25:02 -0700322static void erratum_rcw_src(void)
323{
Santan Kumar99136482017-05-05 15:42:28 +0530324#if defined(CONFIG_SPL) && defined(CONFIG_NAND_BOOT)
Scott Wood8e728cd2015-03-24 13:25:02 -0700325 u32 __iomem *dcfg_ccsr = (u32 __iomem *)DCFG_BASE;
326 u32 __iomem *dcfg_dcsr = (u32 __iomem *)DCFG_DCSR_BASE;
327 u32 val;
328
329 val = in_le32(dcfg_ccsr + DCFG_PORSR1 / 4);
330 val &= ~DCFG_PORSR1_RCW_SRC;
331 val |= DCFG_PORSR1_RCW_SRC_NOR;
332 out_le32(dcfg_dcsr + DCFG_DCSR_PORCR1 / 4, val);
333#endif
334}
335
York Sun0404a392015-03-23 10:41:35 -0700336#define I2C_DEBUG_REG 0x6
337#define I2C_GLITCH_EN 0x8
338/*
339 * This erratum requires setting glitch_en bit to enable
340 * digital glitch filter to improve clock stability.
341 */
Ashish kumar3b52a232017-02-23 16:03:57 +0530342#ifdef CONFIG_SYS_FSL_ERRATUM_A009203
York Sun0404a392015-03-23 10:41:35 -0700343static void erratum_a009203(void)
344{
Tom Rini52b2e262021-08-18 23:12:24 -0400345#if CONFIG_IS_ENABLED(SYS_I2C_LEGACY)
Sriram Dashafa125b2017-09-04 15:45:02 +0530346 u8 __iomem *ptr;
York Sun0404a392015-03-23 10:41:35 -0700347#ifdef I2C1_BASE_ADDR
348 ptr = (u8 __iomem *)(I2C1_BASE_ADDR + I2C_DEBUG_REG);
349
350 writeb(I2C_GLITCH_EN, ptr);
351#endif
352#ifdef I2C2_BASE_ADDR
353 ptr = (u8 __iomem *)(I2C2_BASE_ADDR + I2C_DEBUG_REG);
354
355 writeb(I2C_GLITCH_EN, ptr);
356#endif
357#ifdef I2C3_BASE_ADDR
358 ptr = (u8 __iomem *)(I2C3_BASE_ADDR + I2C_DEBUG_REG);
359
360 writeb(I2C_GLITCH_EN, ptr);
361#endif
362#ifdef I2C4_BASE_ADDR
363 ptr = (u8 __iomem *)(I2C4_BASE_ADDR + I2C_DEBUG_REG);
364
365 writeb(I2C_GLITCH_EN, ptr);
366#endif
367#endif
368}
Ashish kumar3b52a232017-02-23 16:03:57 +0530369#endif
Shengzhou Liua3117ee2016-11-11 18:11:05 +0800370
Saksham Jain5d8ffe12016-03-23 16:24:40 +0530371void bypass_smmu(void)
372{
373 u32 val;
374 val = (in_le32(SMMU_SCR0) | SCR0_CLIENTPD_MASK) & ~(SCR0_USFCFG_MASK);
375 out_le32(SMMU_SCR0, val);
376 val = (in_le32(SMMU_NSCR0) | SCR0_CLIENTPD_MASK) & ~(SCR0_USFCFG_MASK);
377 out_le32(SMMU_NSCR0, val);
378}
Scott Woodf64c98c2015-03-20 19:28:12 -0700379void fsl_lsch3_early_init_f(void)
380{
Scott Wood8e728cd2015-03-24 13:25:02 -0700381 erratum_rcw_src();
Sriram Dash36a4a342017-09-04 15:44:05 +0530382#ifdef CONFIG_FSL_IFC
Scott Woodf64c98c2015-03-20 19:28:12 -0700383 init_early_memctl_regs(); /* tighten IFC timing */
Sriram Dash36a4a342017-09-04 15:44:05 +0530384#endif
Ashish kumar3b52a232017-02-23 16:03:57 +0530385#ifdef CONFIG_SYS_FSL_ERRATUM_A009203
York Sun0404a392015-03-23 10:41:35 -0700386 erratum_a009203();
Ashish kumar3b52a232017-02-23 16:03:57 +0530387#endif
Yao Yuanfae88052015-12-05 14:59:14 +0800388 erratum_a008514();
389 erratum_a008336();
Ran Wangb358b7b2017-09-04 18:46:48 +0800390 erratum_a009008();
Ran Wang9e8fabc2017-09-04 18:46:49 +0800391 erratum_a009798();
Ran Wange64f7472017-09-04 18:46:50 +0800392 erratum_a008997();
Ran Wang3ba69482017-09-04 18:46:51 +0800393 erratum_a009007();
Ran Wang13a84a52021-06-16 17:53:19 +0530394 erratum_a050204();
Saksham Jain5d8ffe12016-03-23 16:24:40 +0530395#ifdef CONFIG_CHAIN_OF_TRUST
396 /* In case of Secure Boot, the IBR configures the SMMU
397 * to allow only Secure transactions.
398 * SMMU must be reset in bypass mode.
399 * Set the ClientPD bit and Clear the USFCFG Bit
400 */
401 if (fsl_check_boot_mode_secure() == 1)
402 bypass_smmu();
403#endif
Laurentiu Tudor7690ea72019-07-30 17:29:58 +0300404
Laurentiu Tudor4adff392019-10-18 09:01:54 +0000405#if defined(CONFIG_ARCH_LS1088A) || defined(CONFIG_ARCH_LS1028A) || \
Meenakshi Aggarwalccb5d5d2020-10-29 19:16:16 +0530406 defined(CONFIG_ARCH_LS2080A) || defined(CONFIG_ARCH_LX2160A) || \
407 defined(CONFIG_ARCH_LX2162A)
Laurentiu Tudor7690ea72019-07-30 17:29:58 +0300408 set_icids();
409#endif
Scott Woodf64c98c2015-03-20 19:28:12 -0700410}
Mingkai Hue4e93ea2015-10-26 19:47:51 +0800411
Rajesh Bhagat814e0772018-01-17 16:13:00 +0530412/* Get VDD in the unit mV from voltage ID */
413int get_core_volt_from_fuse(void)
414{
Tom Rini376b88a2022-10-28 20:27:13 -0400415 struct ccsr_gur *gur = (void *)(CFG_SYS_FSL_GUTS_ADDR);
Rajesh Bhagat814e0772018-01-17 16:13:00 +0530416 int vdd;
417 u32 fusesr;
418 u8 vid;
419
420 /* get the voltage ID from fuse status register */
421 fusesr = in_le32(&gur->dcfg_fusesr);
422 debug("%s: fusesr = 0x%x\n", __func__, fusesr);
423 vid = (fusesr >> FSL_CHASSIS3_DCFG_FUSESR_ALTVID_SHIFT) &
424 FSL_CHASSIS3_DCFG_FUSESR_ALTVID_MASK;
425 if ((vid == 0) || (vid == FSL_CHASSIS3_DCFG_FUSESR_ALTVID_MASK)) {
426 vid = (fusesr >> FSL_CHASSIS3_DCFG_FUSESR_VID_SHIFT) &
427 FSL_CHASSIS3_DCFG_FUSESR_VID_MASK;
428 }
429 debug("%s: VID = 0x%x\n", __func__, vid);
430 switch (vid) {
431 case 0x00: /* VID isn't supported */
432 vdd = -EINVAL;
433 debug("%s: The VID feature is not supported\n", __func__);
434 break;
435 case 0x08: /* 0.9V silicon */
436 vdd = 900;
437 break;
438 case 0x10: /* 1.0V silicon */
439 vdd = 1000;
440 break;
441 default: /* Other core voltage */
442 vdd = -EINVAL;
443 debug("%s: The VID(%x) isn't supported\n", __func__, vid);
444 break;
445 }
446 debug("%s: The required minimum volt of CORE is %dmV\n", __func__, vdd);
447
448 return vdd;
449}
450
Prabhakar Kushwaha1966d012016-06-03 18:41:27 +0530451#elif defined(CONFIG_FSL_LSCH2)
Mingkai Hu172081c2016-02-02 11:28:03 +0800452/*
453 * This erratum requires setting a value to eddrtqcr1 to optimal
454 * the DDR performance. The eddrtqcr1 register is in SCFG space
455 * of LS1043A and the offset is 0x157_020c.
456 */
457#if defined(CONFIG_SYS_FSL_ERRATUM_A009660) \
458 && defined(CONFIG_SYS_FSL_ERRATUM_A008514)
459#error A009660 and A008514 can not be both enabled.
460#endif
461
462static void erratum_a009660(void)
463{
464#ifdef CONFIG_SYS_FSL_ERRATUM_A009660
Tom Rini376b88a2022-10-28 20:27:13 -0400465 u32 *eddrtqcr1 = (void *)CFG_SYS_FSL_SCFG_ADDR + 0x20c;
Mingkai Hu172081c2016-02-02 11:28:03 +0800466 out_be32(eddrtqcr1, 0x63b20042);
467#endif
468}
469
Shengzhou Liuddf060b2016-04-07 16:22:21 +0800470static void erratum_a008850_early(void)
471{
472#ifdef CONFIG_SYS_FSL_ERRATUM_A008850
473 /* part 1 of 2 */
Ashish Kumar11234062017-08-11 11:09:14 +0530474 struct ccsr_cci400 __iomem *cci = (void *)(CONFIG_SYS_IMMR +
475 CONFIG_SYS_CCI400_OFFSET);
Tom Rini376b88a2022-10-28 20:27:13 -0400476 struct ccsr_ddr __iomem *ddr = (void *)CFG_SYS_FSL_DDR_ADDR;
Shengzhou Liuddf060b2016-04-07 16:22:21 +0800477
York Sune6b871e2017-05-15 08:51:59 -0700478 /* Skip if running at lower exception level */
479 if (current_el() < 3)
480 return;
481
Shengzhou Liuddf060b2016-04-07 16:22:21 +0800482 /* disables propagation of barrier transactions to DDRC from CCI400 */
483 out_le32(&cci->ctrl_ord, CCI400_CTRLORD_TERM_BARRIER);
484
485 /* disable the re-ordering in DDRC */
486 ddr_out32(&ddr->eor, DDR_EOR_RD_REOD_DIS | DDR_EOR_WD_REOD_DIS);
487#endif
488}
489
490void erratum_a008850_post(void)
491{
492#ifdef CONFIG_SYS_FSL_ERRATUM_A008850
493 /* part 2 of 2 */
Ashish Kumar11234062017-08-11 11:09:14 +0530494 struct ccsr_cci400 __iomem *cci = (void *)(CONFIG_SYS_IMMR +
495 CONFIG_SYS_CCI400_OFFSET);
Tom Rini376b88a2022-10-28 20:27:13 -0400496 struct ccsr_ddr __iomem *ddr = (void *)CFG_SYS_FSL_DDR_ADDR;
Shengzhou Liuddf060b2016-04-07 16:22:21 +0800497 u32 tmp;
498
York Sune6b871e2017-05-15 08:51:59 -0700499 /* Skip if running at lower exception level */
500 if (current_el() < 3)
501 return;
502
Shengzhou Liuddf060b2016-04-07 16:22:21 +0800503 /* enable propagation of barrier transactions to DDRC from CCI400 */
504 out_le32(&cci->ctrl_ord, CCI400_CTRLORD_EN_BARRIER);
505
506 /* enable the re-ordering in DDRC */
507 tmp = ddr_in32(&ddr->eor);
508 tmp &= ~(DDR_EOR_RD_REOD_DIS | DDR_EOR_WD_REOD_DIS);
509 ddr_out32(&ddr->eor, tmp);
510#endif
511}
Hou Zhiqiang4b23ca82016-08-02 19:03:27 +0800512
513#ifdef CONFIG_SYS_FSL_ERRATUM_A010315
514void erratum_a010315(void)
515{
516 int i;
517
518 for (i = PCIE1; i <= PCIE4; i++)
519 if (!is_serdes_configured(i)) {
520 debug("PCIe%d: disabled all R/W permission!\n", i);
521 set_pcie_ns_access(i, 0);
522 }
523}
524#endif
Shengzhou Liuddf060b2016-04-07 16:22:21 +0800525
Hou Zhiqiangc06b30a2016-09-29 12:42:44 +0800526static void erratum_a010539(void)
527{
528#if defined(CONFIG_SYS_FSL_ERRATUM_A010539) && defined(CONFIG_QSPI_BOOT)
Tom Rini376b88a2022-10-28 20:27:13 -0400529 struct ccsr_gur __iomem *gur = (void *)(CFG_SYS_FSL_GUTS_ADDR);
Hou Zhiqiangc06b30a2016-09-29 12:42:44 +0800530 u32 porsr1;
531
532 porsr1 = in_be32(&gur->porsr1);
533 porsr1 &= ~FSL_CHASSIS2_CCSR_PORSR1_RCW_MASK;
Tom Rini6a5dccc2022-11-16 13:10:41 -0500534 out_be32((void *)(CFG_SYS_DCSR_DCFG_ADDR + DCFG_DCSR_PORCR1),
Hou Zhiqiangc06b30a2016-09-29 12:42:44 +0800535 porsr1);
Tom Rini376b88a2022-10-28 20:27:13 -0400536 out_be32((void *)(CFG_SYS_FSL_SCFG_ADDR + 0x1a8), 0xffffffff);
Hou Zhiqiangc06b30a2016-09-29 12:42:44 +0800537#endif
538}
539
Hou Zhiqiang4ad59992016-12-09 16:09:00 +0800540/* Get VDD in the unit mV from voltage ID */
541int get_core_volt_from_fuse(void)
542{
Tom Rini376b88a2022-10-28 20:27:13 -0400543 struct ccsr_gur *gur = (void *)(CFG_SYS_FSL_GUTS_ADDR);
Hou Zhiqiang4ad59992016-12-09 16:09:00 +0800544 int vdd;
545 u32 fusesr;
546 u8 vid;
547
548 fusesr = in_be32(&gur->dcfg_fusesr);
549 debug("%s: fusesr = 0x%x\n", __func__, fusesr);
550 vid = (fusesr >> FSL_CHASSIS2_DCFG_FUSESR_ALTVID_SHIFT) &
551 FSL_CHASSIS2_DCFG_FUSESR_ALTVID_MASK;
552 if ((vid == 0) || (vid == FSL_CHASSIS2_DCFG_FUSESR_ALTVID_MASK)) {
553 vid = (fusesr >> FSL_CHASSIS2_DCFG_FUSESR_VID_SHIFT) &
554 FSL_CHASSIS2_DCFG_FUSESR_VID_MASK;
555 }
556 debug("%s: VID = 0x%x\n", __func__, vid);
557 switch (vid) {
558 case 0x00: /* VID isn't supported */
559 vdd = -EINVAL;
560 debug("%s: The VID feature is not supported\n", __func__);
561 break;
562 case 0x08: /* 0.9V silicon */
563 vdd = 900;
564 break;
565 case 0x10: /* 1.0V silicon */
566 vdd = 1000;
567 break;
568 default: /* Other core voltage */
569 vdd = -EINVAL;
570 printf("%s: The VID(%x) isn't supported\n", __func__, vid);
571 break;
572 }
573 debug("%s: The required minimum volt of CORE is %dmV\n", __func__, vdd);
574
575 return vdd;
576}
577
Hou Zhiqiang4ad59992016-12-09 16:09:00 +0800578static int setup_core_volt(u32 vdd)
579{
580 return board_setup_core_volt(vdd);
581}
582
583#ifdef CONFIG_SYS_FSL_DDR
584static void ddr_enable_0v9_volt(bool en)
585{
Tom Rini376b88a2022-10-28 20:27:13 -0400586 struct ccsr_ddr __iomem *ddr = (void *)CFG_SYS_FSL_DDR_ADDR;
Hou Zhiqiang4ad59992016-12-09 16:09:00 +0800587 u32 tmp;
588
589 tmp = ddr_in32(&ddr->ddr_cdr1);
590
591 if (en)
592 tmp |= DDR_CDR1_V0PT9_EN;
593 else
594 tmp &= ~DDR_CDR1_V0PT9_EN;
595
596 ddr_out32(&ddr->ddr_cdr1, tmp);
597}
598#endif
599
600int setup_chip_volt(void)
601{
602 int vdd;
603
604 vdd = get_core_volt_from_fuse();
605 /* Nothing to do for silicons doesn't support VID */
606 if (vdd < 0)
607 return vdd;
608
609 if (setup_core_volt(vdd))
610 printf("%s: Switch core VDD to %dmV failed\n", __func__, vdd);
611#ifdef CONFIG_SYS_HAS_SERDES
612 if (setup_serdes_volt(vdd))
613 printf("%s: Switch SVDD to %dmV failed\n", __func__, vdd);
614#endif
615
616#ifdef CONFIG_SYS_FSL_DDR
617 if (vdd == 900)
618 ddr_enable_0v9_volt(true);
619#endif
620
621 return 0;
622}
623
Calvin Johnson6d6ef012018-03-08 15:30:33 +0530624#ifdef CONFIG_FSL_PFE
625void init_pfe_scfg_dcfg_regs(void)
626{
Tom Rini376b88a2022-10-28 20:27:13 -0400627 struct ccsr_scfg *scfg = (struct ccsr_scfg *)CFG_SYS_FSL_SCFG_ADDR;
Calvin Johnson6d6ef012018-03-08 15:30:33 +0530628 u32 ecccr2;
629
630 out_be32(&scfg->pfeasbcr,
631 in_be32(&scfg->pfeasbcr) | SCFG_PFEASBCR_AWCACHE0);
632 out_be32(&scfg->pfebsbcr,
633 in_be32(&scfg->pfebsbcr) | SCFG_PFEASBCR_AWCACHE0);
634
635 /* CCI-400 QoS settings for PFE */
636 out_be32(&scfg->wr_qos1, (unsigned int)(SCFG_WR_QOS1_PFE1_QOS
637 | SCFG_WR_QOS1_PFE2_QOS));
638 out_be32(&scfg->rd_qos1, (unsigned int)(SCFG_RD_QOS1_PFE1_QOS
639 | SCFG_RD_QOS1_PFE2_QOS));
640
Tom Rini6a5dccc2022-11-16 13:10:41 -0500641 ecccr2 = in_be32(CFG_SYS_DCSR_DCFG_ADDR + DCFG_DCSR_ECCCR2);
642 out_be32((void *)CFG_SYS_DCSR_DCFG_ADDR + DCFG_DCSR_ECCCR2,
Calvin Johnson6d6ef012018-03-08 15:30:33 +0530643 ecccr2 | (unsigned int)DISABLE_PFE_ECC);
644}
645#endif
646
Mingkai Hue4e93ea2015-10-26 19:47:51 +0800647void fsl_lsch2_early_init_f(void)
648{
Ashish Kumar11234062017-08-11 11:09:14 +0530649 struct ccsr_cci400 *cci = (struct ccsr_cci400 *)(CONFIG_SYS_IMMR +
650 CONFIG_SYS_CCI400_OFFSET);
Tom Rini376b88a2022-10-28 20:27:13 -0400651 struct ccsr_scfg *scfg = (struct ccsr_scfg *)CFG_SYS_FSL_SCFG_ADDR;
Pankit Garg41bde722019-05-29 12:12:36 +0000652#if defined(CONFIG_FSL_QSPI) && defined(CONFIG_TFABOOT)
653 enum boot_src src;
654#endif
Mingkai Hue4e93ea2015-10-26 19:47:51 +0800655
Hou Zhiqiang5ac9a5c2016-08-02 19:03:23 +0800656#ifdef CONFIG_LAYERSCAPE_NS_ACCESS
657 enable_layerscape_ns_access();
658#endif
659
Mingkai Hue4e93ea2015-10-26 19:47:51 +0800660#ifdef CONFIG_FSL_IFC
661 init_early_memctl_regs(); /* tighten IFC timing */
662#endif
663
Pankit Garg41bde722019-05-29 12:12:36 +0000664#if defined(CONFIG_FSL_QSPI) && defined(CONFIG_TFABOOT)
665 src = get_boot_src();
666 if (src != BOOT_SOURCE_QSPI_NOR)
667 out_be32(&scfg->qspi_cfg, SCFG_QSPI_CLKSEL);
668#else
Qianyu Gong5ab2d0a2016-03-16 18:01:52 +0800669#if defined(CONFIG_FSL_QSPI) && !defined(CONFIG_QSPI_BOOT)
Gong Qianyu760df892016-01-25 15:16:06 +0800670 out_be32(&scfg->qspi_cfg, SCFG_QSPI_CLKSEL);
671#endif
Pankit Garg41bde722019-05-29 12:12:36 +0000672#endif
Aneesh Bansal13d984d2015-12-08 13:54:27 +0530673 /* Make SEC reads and writes snoopable */
Ran Wangc75026e2019-09-20 17:34:29 +0800674#if defined(CONFIG_ARCH_LS1043A) || defined(CONFIG_ARCH_LS1046A)
675 setbits_be32(&scfg->snpcnfgcr, SCFG_SNPCNFGCR_SECRDSNP |
676 SCFG_SNPCNFGCR_SECWRSNP | SCFG_SNPCNFGCR_USB1RDSNP |
677 SCFG_SNPCNFGCR_USB1WRSNP | SCFG_SNPCNFGCR_USB2RDSNP |
678 SCFG_SNPCNFGCR_USB2WRSNP | SCFG_SNPCNFGCR_USB3RDSNP |
679 SCFG_SNPCNFGCR_USB3WRSNP | SCFG_SNPCNFGCR_SATARDSNP |
Sean Andersone7e395d2022-09-23 12:05:51 -0400680 SCFG_SNPCNFGCR_SATAWRSNP | SCFG_SNPCNFGCR_EDMASNP);
Ran Wanga7576692019-12-26 18:11:17 +0800681#elif defined(CONFIG_ARCH_LS1012A)
682 setbits_be32(&scfg->snpcnfgcr, SCFG_SNPCNFGCR_SECRDSNP |
683 SCFG_SNPCNFGCR_SECWRSNP | SCFG_SNPCNFGCR_USB1RDSNP |
684 SCFG_SNPCNFGCR_USB1WRSNP | SCFG_SNPCNFGCR_SATARDSNP |
685 SCFG_SNPCNFGCR_SATAWRSNP);
Ran Wangc75026e2019-09-20 17:34:29 +0800686#else
Aneesh Bansal13d984d2015-12-08 13:54:27 +0530687 setbits_be32(&scfg->snpcnfgcr, SCFG_SNPCNFGCR_SECRDSNP |
Tang Yuantian2945ae02016-08-08 15:07:20 +0800688 SCFG_SNPCNFGCR_SECWRSNP |
689 SCFG_SNPCNFGCR_SATARDSNP |
690 SCFG_SNPCNFGCR_SATAWRSNP);
Ran Wangc75026e2019-09-20 17:34:29 +0800691#endif
Aneesh Bansal13d984d2015-12-08 13:54:27 +0530692
Mingkai Hue4e93ea2015-10-26 19:47:51 +0800693 /*
694 * Enable snoop requests and DVM message requests for
695 * Slave insterface S4 (A53 core cluster)
696 */
York Sune6b871e2017-05-15 08:51:59 -0700697 if (current_el() == 3) {
698 out_le32(&cci->slave[4].snoop_ctrl,
699 CCI400_DVM_MESSAGE_REQ_EN | CCI400_SNOOP_REQ_EN);
700 }
Mingkai Hu8beb0752015-12-07 16:58:54 +0800701
Ran Wang4e7cdcf2018-08-10 15:00:00 +0800702 /*
703 * Program Central Security Unit (CSU) to grant access
704 * permission for USB 2.0 controller
705 */
706#if defined(CONFIG_ARCH_LS1012A) && defined(CONFIG_USB_EHCI_FSL)
707 if (current_el() == 3)
708 set_devices_ns_access(CSU_CSLX_USB_2, CSU_ALL_RW);
709#endif
Mingkai Hu8beb0752015-12-07 16:58:54 +0800710 /* Erratum */
Shengzhou Liuddf060b2016-04-07 16:22:21 +0800711 erratum_a008850_early(); /* part 1 of 2 */
Mingkai Hu172081c2016-02-02 11:28:03 +0800712 erratum_a009660();
Hou Zhiqiangc06b30a2016-09-29 12:42:44 +0800713 erratum_a010539();
Ran Wangb358b7b2017-09-04 18:46:48 +0800714 erratum_a009008();
Ran Wang9e8fabc2017-09-04 18:46:49 +0800715 erratum_a009798();
Ran Wange64f7472017-09-04 18:46:50 +0800716 erratum_a008997();
Ran Wang3ba69482017-09-04 18:46:51 +0800717 erratum_a009007();
Laurentiu Tudor512d13e2018-08-09 15:19:46 +0300718
Laurentiu Tudor22012d52018-08-27 17:33:59 +0300719#if defined(CONFIG_ARCH_LS1043A) || defined(CONFIG_ARCH_LS1046A)
Laurentiu Tudor512d13e2018-08-09 15:19:46 +0300720 set_icids();
721#endif
Mingkai Hue4e93ea2015-10-26 19:47:51 +0800722}
Mingkai Hu0e58b512015-10-26 19:47:50 +0800723#endif
Scott Wood8e728cd2015-03-24 13:25:02 -0700724
Kuldeep Singh34aafb02019-11-21 17:15:17 +0530725#ifdef CONFIG_FSPI_AHB_EN_4BYTE
726int fspi_ahb_init(void)
727{
728 /* Enable 4bytes address support and fast read */
729 u32 *fspi_lut, lut_key, *fspi_key;
730
731 fspi_key = (void *)SYS_NXP_FSPI_ADDR + SYS_NXP_FSPI_LUTKEY_BASE_ADDR;
732 fspi_lut = (void *)SYS_NXP_FSPI_ADDR + SYS_NXP_FSPI_LUT_BASE_ADDR;
733
734 lut_key = in_be32(fspi_key);
735
736 if (lut_key == SYS_NXP_FSPI_LUTKEY) {
737 /* That means the register is BE */
738 out_be32(fspi_key, SYS_NXP_FSPI_LUTKEY);
739 /* Unlock the lut table */
740 out_be32(fspi_key + 1, SYS_NXP_FSPI_LUTCR_UNLOCK);
741 /* Create READ LUT */
742 out_be32(fspi_lut, 0x0820040c);
743 out_be32(fspi_lut + 1, 0x24003008);
744 out_be32(fspi_lut + 2, 0x00000000);
745 /* Lock the lut table */
746 out_be32(fspi_key, SYS_NXP_FSPI_LUTKEY);
747 out_be32(fspi_key + 1, SYS_NXP_FSPI_LUTCR_LOCK);
748 } else {
749 /* That means the register is LE */
750 out_le32(fspi_key, SYS_NXP_FSPI_LUTKEY);
751 /* Unlock the lut table */
752 out_le32(fspi_key + 1, SYS_NXP_FSPI_LUTCR_UNLOCK);
753 /* Create READ LUT */
754 out_le32(fspi_lut, 0x0820040c);
755 out_le32(fspi_lut + 1, 0x24003008);
756 out_le32(fspi_lut + 2, 0x00000000);
757 /* Lock the lut table */
758 out_le32(fspi_key, SYS_NXP_FSPI_LUTKEY);
759 out_le32(fspi_key + 1, SYS_NXP_FSPI_LUTCR_LOCK);
760 }
761
762 return 0;
763}
764#endif
765
Yuan Yao52ae4fd2016-12-01 10:13:52 +0800766#ifdef CONFIG_QSPI_AHB_INIT
767/* Enable 4bytes address support and fast read */
768int qspi_ahb_init(void)
769{
770 u32 *qspi_lut, lut_key, *qspi_key;
771
772 qspi_key = (void *)SYS_FSL_QSPI_ADDR + 0x300;
773 qspi_lut = (void *)SYS_FSL_QSPI_ADDR + 0x310;
774
775 lut_key = in_be32(qspi_key);
776
777 if (lut_key == 0x5af05af0) {
778 /* That means the register is BE */
779 out_be32(qspi_key, 0x5af05af0);
780 /* Unlock the lut table */
781 out_be32(qspi_key + 1, 0x00000002);
782 out_be32(qspi_lut, 0x0820040c);
783 out_be32(qspi_lut + 1, 0x1c080c08);
784 out_be32(qspi_lut + 2, 0x00002400);
785 /* Lock the lut table */
786 out_be32(qspi_key, 0x5af05af0);
787 out_be32(qspi_key + 1, 0x00000001);
788 } else {
789 /* That means the register is LE */
790 out_le32(qspi_key, 0x5af05af0);
791 /* Unlock the lut table */
792 out_le32(qspi_key + 1, 0x00000002);
793 out_le32(qspi_lut, 0x0820040c);
794 out_le32(qspi_lut + 1, 0x1c080c08);
795 out_le32(qspi_lut + 2, 0x00002400);
796 /* Lock the lut table */
797 out_le32(qspi_key, 0x5af05af0);
798 out_le32(qspi_key + 1, 0x00000001);
799 }
800
Pankit Gargbdbf84f2018-11-05 18:01:52 +0000801 return 0;
802}
803#endif
804
805#ifdef CONFIG_TFABOOT
Rajesh Bhagat5b73c902018-12-27 04:37:49 +0000806#define MAX_BOOTCMD_SIZE 512
Pankit Gargbdbf84f2018-11-05 18:01:52 +0000807
Mathew McBrideb56ee9d2023-07-21 04:39:29 +0000808__weak int fsl_setenv_bootcmd(void)
Pankit Gargbdbf84f2018-11-05 18:01:52 +0000809{
810 int ret;
811 enum boot_src src = get_boot_src();
812 char bootcmd_str[MAX_BOOTCMD_SIZE];
813
814 switch (src) {
815#ifdef IFC_NOR_BOOTCOMMAND
816 case BOOT_SOURCE_IFC_NOR:
817 sprintf(bootcmd_str, IFC_NOR_BOOTCOMMAND);
818 break;
819#endif
820#ifdef QSPI_NOR_BOOTCOMMAND
821 case BOOT_SOURCE_QSPI_NOR:
822 sprintf(bootcmd_str, QSPI_NOR_BOOTCOMMAND);
823 break;
824#endif
825#ifdef XSPI_NOR_BOOTCOMMAND
826 case BOOT_SOURCE_XSPI_NOR:
827 sprintf(bootcmd_str, XSPI_NOR_BOOTCOMMAND);
828 break;
829#endif
830#ifdef IFC_NAND_BOOTCOMMAND
831 case BOOT_SOURCE_IFC_NAND:
832 sprintf(bootcmd_str, IFC_NAND_BOOTCOMMAND);
833 break;
834#endif
835#ifdef QSPI_NAND_BOOTCOMMAND
836 case BOOT_SOURCE_QSPI_NAND:
837 sprintf(bootcmd_str, QSPI_NAND_BOOTCOMMAND);
838 break;
839#endif
840#ifdef XSPI_NAND_BOOTCOMMAND
841 case BOOT_SOURCE_XSPI_NAND:
842 sprintf(bootcmd_str, XSPI_NAND_BOOTCOMMAND);
843 break;
844#endif
845#ifdef SD_BOOTCOMMAND
846 case BOOT_SOURCE_SD_MMC:
847 sprintf(bootcmd_str, SD_BOOTCOMMAND);
848 break;
849#endif
850#ifdef SD2_BOOTCOMMAND
851 case BOOT_SOURCE_SD_MMC2:
852 sprintf(bootcmd_str, SD2_BOOTCOMMAND);
853 break;
854#endif
855 default:
856#ifdef QSPI_NOR_BOOTCOMMAND
857 sprintf(bootcmd_str, QSPI_NOR_BOOTCOMMAND);
858#endif
859 break;
860 }
861
862 ret = env_set("bootcmd", bootcmd_str);
863 if (ret) {
864 printf("Failed to set bootcmd: ret = %d\n", ret);
865 return ret;
866 }
Yuan Yao52ae4fd2016-12-01 10:13:52 +0800867 return 0;
868}
Pankit Garg82fcc462018-11-05 18:02:31 +0000869
870int fsl_setenv_mcinitcmd(void)
871{
872 int ret = 0;
873 enum boot_src src = get_boot_src();
874
875 switch (src) {
876#ifdef IFC_MC_INIT_CMD
877 case BOOT_SOURCE_IFC_NAND:
878 case BOOT_SOURCE_IFC_NOR:
879 ret = env_set("mcinitcmd", IFC_MC_INIT_CMD);
880 break;
881#endif
882#ifdef QSPI_MC_INIT_CMD
883 case BOOT_SOURCE_QSPI_NAND:
884 case BOOT_SOURCE_QSPI_NOR:
885 ret = env_set("mcinitcmd", QSPI_MC_INIT_CMD);
886 break;
887#endif
888#ifdef XSPI_MC_INIT_CMD
889 case BOOT_SOURCE_XSPI_NAND:
890 case BOOT_SOURCE_XSPI_NOR:
891 ret = env_set("mcinitcmd", XSPI_MC_INIT_CMD);
892 break;
893#endif
894#ifdef SD_MC_INIT_CMD
895 case BOOT_SOURCE_SD_MMC:
896 ret = env_set("mcinitcmd", SD_MC_INIT_CMD);
897 break;
898#endif
899#ifdef SD2_MC_INIT_CMD
900 case BOOT_SOURCE_SD_MMC2:
901 ret = env_set("mcinitcmd", SD2_MC_INIT_CMD);
902 break;
903#endif
904 default:
905#ifdef QSPI_MC_INIT_CMD
906 ret = env_set("mcinitcmd", QSPI_MC_INIT_CMD);
907#endif
908 break;
909 }
910
911 if (ret) {
912 printf("Failed to set mcinitcmd: ret = %d\n", ret);
913 return ret;
914 }
915 return 0;
916}
Yuan Yao52ae4fd2016-12-01 10:13:52 +0800917#endif
918
Mingkai Hu0e58b512015-10-26 19:47:50 +0800919#ifdef CONFIG_BOARD_LATE_INIT
Michael Wallefc667ea2019-10-21 22:37:45 +0200920__weak int fsl_board_late_init(void)
921{
922 return 0;
923}
924
Ran Wangba7cd0f2020-08-05 15:07:27 +0800925#define DWC3_GSBUSCFG0 0xc100
926#define DWC3_GSBUSCFG0_CACHETYPE_SHIFT 16
927#define DWC3_GSBUSCFG0_CACHETYPE(n) (((n) & 0xffff) \
928 << DWC3_GSBUSCFG0_CACHETYPE_SHIFT)
929
Michael Walle32825142021-10-15 15:15:19 +0200930static void enable_dwc3_snooping(void)
Ran Wangba7cd0f2020-08-05 15:07:27 +0800931{
Michael Walle32825142021-10-15 15:15:19 +0200932 static const char * const compatibles[] = {
933 "fsl,layerscape-dwc3",
934 "fsl,ls1028a-dwc3",
935 };
Ran Wangba7cd0f2020-08-05 15:07:27 +0800936 fdt_addr_t dwc3_base;
Michael Walle32825142021-10-15 15:15:19 +0200937 ofnode node;
938 u32 val;
939 int i;
Ran Wangba7cd0f2020-08-05 15:07:27 +0800940
Michael Walle32825142021-10-15 15:15:19 +0200941 for (i = 0; i < ARRAY_SIZE(compatibles); i++) {
942 ofnode_for_each_compatible_node(node, compatibles[i]) {
943 dwc3_base = ofnode_get_addr(node);
944 if (dwc3_base == FDT_ADDR_T_NONE)
Ran Wangba7cd0f2020-08-05 15:07:27 +0800945 continue;
Michael Walle32825142021-10-15 15:15:19 +0200946
Ran Wangba7cd0f2020-08-05 15:07:27 +0800947 val = in_le32(dwc3_base + DWC3_GSBUSCFG0);
948 val &= ~DWC3_GSBUSCFG0_CACHETYPE(~0);
949 val |= DWC3_GSBUSCFG0_CACHETYPE(0x2222);
950 writel(val, dwc3_base + DWC3_GSBUSCFG0);
951 }
952 }
953}
954
Mingkai Hu0e58b512015-10-26 19:47:50 +0800955int board_late_init(void)
Scott Wood8e728cd2015-03-24 13:25:02 -0700956{
Aneesh Bansal39d5b3b2016-01-22 16:37:26 +0530957#ifdef CONFIG_CHAIN_OF_TRUST
958 fsl_setenv_chain_of_trust();
959#endif
Pankit Gargbdbf84f2018-11-05 18:01:52 +0000960#ifdef CONFIG_TFABOOT
961 /*
Wasim Khan5f745142021-08-02 10:34:52 +0200962 * Set bootcmd and mcinitcmd if "fsl_bootcmd_mcinitcmd_set" does
963 * not exists in env
Rajesh Bhagat5b73c902018-12-27 04:37:49 +0000964 */
Wasim Khan5f745142021-08-02 10:34:52 +0200965 if (env_get_yesno("fsl_bootcmd_mcinitcmd_set") <= 0) {
966 // Set bootcmd and mcinitcmd as per boot source
Rajesh Bhagat5b73c902018-12-27 04:37:49 +0000967 fsl_setenv_bootcmd();
968 fsl_setenv_mcinitcmd();
Wasim Khan5f745142021-08-02 10:34:52 +0200969 env_set("fsl_bootcmd_mcinitcmd_set", "y");
970 }
Pankit Gargbdbf84f2018-11-05 18:01:52 +0000971#endif
Yuan Yao52ae4fd2016-12-01 10:13:52 +0800972#ifdef CONFIG_QSPI_AHB_INIT
973 qspi_ahb_init();
974#endif
Kuldeep Singh34aafb02019-11-21 17:15:17 +0530975#ifdef CONFIG_FSPI_AHB_EN_4BYTE
976 fspi_ahb_init();
977#endif
Tang Yuantian57894be2015-12-09 15:32:18 +0800978
Ran Wangba7cd0f2020-08-05 15:07:27 +0800979 if (IS_ENABLED(CONFIG_DM))
980 enable_dwc3_snooping();
981
Michael Wallefc667ea2019-10-21 22:37:45 +0200982 return fsl_board_late_init();
Scott Wood8e728cd2015-03-24 13:25:02 -0700983}
984#endif