1e84a324bSAshish Kumar /*
2e84a324bSAshish Kumar  * Copyright 2017 NXP
3e84a324bSAshish Kumar  *
4e84a324bSAshish Kumar  * SPDX-License-Identifier:	GPL-2.0+
5e84a324bSAshish Kumar  */
6e84a324bSAshish Kumar #include <common.h>
7e84a324bSAshish Kumar #include <i2c.h>
8e84a324bSAshish Kumar #include <malloc.h>
9e84a324bSAshish Kumar #include <errno.h>
10e84a324bSAshish Kumar #include <netdev.h>
11e84a324bSAshish Kumar #include <fsl_ifc.h>
12e84a324bSAshish Kumar #include <fsl_ddr.h>
13e84a324bSAshish Kumar #include <fsl_sec.h>
14e84a324bSAshish Kumar #include <asm/io.h>
15e84a324bSAshish Kumar #include <fdt_support.h>
16e84a324bSAshish Kumar #include <libfdt.h>
17e84a324bSAshish Kumar #include <fsl-mc/fsl_mc.h>
18e84a324bSAshish Kumar #include <environment.h>
19e84a324bSAshish Kumar #include <asm/arch-fsl-layerscape/soc.h>
20e84a324bSAshish Kumar #include <asm/arch/ppa.h>
2144cdb5b6SYangbo Lu #include <hwconfig.h>
22*ef0789b7SRajesh Bhagat #include <asm/arch/fsl_serdes.h>
23*ef0789b7SRajesh Bhagat #include <asm/arch/soc.h>
24e84a324bSAshish Kumar 
25e84a324bSAshish Kumar #include "../common/qixis.h"
26e84a324bSAshish Kumar #include "ls1088a_qixis.h"
27*ef0789b7SRajesh Bhagat #include "../common/vid.h"
28*ef0789b7SRajesh Bhagat #include <fsl_immap.h>
29e84a324bSAshish Kumar 
30e84a324bSAshish Kumar DECLARE_GLOBAL_DATA_PTR;
31e84a324bSAshish Kumar 
3210e7eaf0SSumit Garg int board_early_init_f(void)
3310e7eaf0SSumit Garg {
3410e7eaf0SSumit Garg 	fsl_lsch3_early_init_f();
3510e7eaf0SSumit Garg 	return 0;
3610e7eaf0SSumit Garg }
3710e7eaf0SSumit Garg 
3810e7eaf0SSumit Garg #ifdef CONFIG_FSL_QIXIS
39e84a324bSAshish Kumar unsigned long long get_qixis_addr(void)
40e84a324bSAshish Kumar {
41e84a324bSAshish Kumar 	unsigned long long addr;
42e84a324bSAshish Kumar 
43e84a324bSAshish Kumar 	if (gd->flags & GD_FLG_RELOC)
44e84a324bSAshish Kumar 		addr = QIXIS_BASE_PHYS;
45e84a324bSAshish Kumar 	else
46e84a324bSAshish Kumar 		addr = QIXIS_BASE_PHYS_EARLY;
47e84a324bSAshish Kumar 
48e84a324bSAshish Kumar 	/*
49e84a324bSAshish Kumar 	 * IFC address under 256MB is mapped to 0x30000000, any address above
50e84a324bSAshish Kumar 	 * is mapped to 0x5_10000000 up to 4GB.
51e84a324bSAshish Kumar 	 */
52e84a324bSAshish Kumar 	addr = addr  > 0x10000000 ? addr + 0x500000000ULL : addr + 0x30000000;
53e84a324bSAshish Kumar 
54e84a324bSAshish Kumar 	return addr;
55e84a324bSAshish Kumar }
5610e7eaf0SSumit Garg #endif
57e84a324bSAshish Kumar 
58*ef0789b7SRajesh Bhagat #if defined(CONFIG_VID)
59*ef0789b7SRajesh Bhagat int init_func_vid(void)
60*ef0789b7SRajesh Bhagat {
61*ef0789b7SRajesh Bhagat 	if (adjust_vdd(0) < 0)
62*ef0789b7SRajesh Bhagat 		printf("core voltage not adjusted\n");
63*ef0789b7SRajesh Bhagat 
64*ef0789b7SRajesh Bhagat 	return 0;
65*ef0789b7SRajesh Bhagat }
66*ef0789b7SRajesh Bhagat #endif
67*ef0789b7SRajesh Bhagat 
6810e7eaf0SSumit Garg #if !defined(CONFIG_SPL_BUILD)
69e84a324bSAshish Kumar int checkboard(void)
70e84a324bSAshish Kumar {
71e84a324bSAshish Kumar 	char buf[64];
72e84a324bSAshish Kumar 	u8 sw;
73e84a324bSAshish Kumar 	static const char *const freq[] = {"100", "125", "156.25",
74e84a324bSAshish Kumar 					    "100 separate SSCG"};
75e84a324bSAshish Kumar 	int clock;
76e84a324bSAshish Kumar 
777769776aSAshish Kumar #ifdef CONFIG_TARGET_LS1088AQDS
787769776aSAshish Kumar 	printf("Board: LS1088A-QDS, ");
797769776aSAshish Kumar #else
80e84a324bSAshish Kumar 	printf("Board: LS1088A-RDB, ");
817769776aSAshish Kumar #endif
82e84a324bSAshish Kumar 
83e84a324bSAshish Kumar 	sw = QIXIS_READ(arch);
84e84a324bSAshish Kumar 	printf("Board Arch: V%d, ", sw >> 4);
85e84a324bSAshish Kumar 
867769776aSAshish Kumar #ifdef CONFIG_TARGET_LS1088AQDS
877769776aSAshish Kumar 	printf("Board version: %c, boot from ", (sw & 0xf) + 'A' - 1);
887769776aSAshish Kumar #else
89e84a324bSAshish Kumar 	printf("Board version: %c, boot from ", (sw & 0xf) + 'A');
907769776aSAshish Kumar #endif
91e84a324bSAshish Kumar 
92e84a324bSAshish Kumar 	memset((u8 *)buf, 0x00, ARRAY_SIZE(buf));
93e84a324bSAshish Kumar 
94e84a324bSAshish Kumar 	sw = QIXIS_READ(brdcfg[0]);
95e84a324bSAshish Kumar 	sw = (sw & QIXIS_LBMAP_MASK) >> QIXIS_LBMAP_SHIFT;
96e84a324bSAshish Kumar 
97e84a324bSAshish Kumar #ifdef CONFIG_SD_BOOT
98e84a324bSAshish Kumar 	puts("SD card\n");
99e84a324bSAshish Kumar #endif
100e84a324bSAshish Kumar 	switch (sw) {
1017769776aSAshish Kumar #ifdef CONFIG_TARGET_LS1088AQDS
102e84a324bSAshish Kumar 	case 0:
1037769776aSAshish Kumar 	case 1:
1047769776aSAshish Kumar 	case 2:
1057769776aSAshish Kumar 	case 3:
1067769776aSAshish Kumar 	case 4:
1077769776aSAshish Kumar 	case 5:
1087769776aSAshish Kumar 	case 6:
1097769776aSAshish Kumar 	case 7:
1107769776aSAshish Kumar 		printf("vBank: %d\n", sw);
1117769776aSAshish Kumar 		break;
1127769776aSAshish Kumar 	case 8:
1137769776aSAshish Kumar 		puts("PromJet\n");
1147769776aSAshish Kumar 		break;
1157769776aSAshish Kumar 	case 15:
1167769776aSAshish Kumar 		puts("IFCCard\n");
1177769776aSAshish Kumar 		break;
1187769776aSAshish Kumar 	case 14:
1197769776aSAshish Kumar #else
1207769776aSAshish Kumar 	case 0:
1217769776aSAshish Kumar #endif
122e84a324bSAshish Kumar 		puts("QSPI:");
123e84a324bSAshish Kumar 		sw = QIXIS_READ(brdcfg[0]);
124e84a324bSAshish Kumar 		sw = (sw & QIXIS_QMAP_MASK) >> QIXIS_QMAP_SHIFT;
125e84a324bSAshish Kumar 		if (sw == 0 || sw == 4)
126e84a324bSAshish Kumar 			puts("0\n");
127e84a324bSAshish Kumar 		else if (sw == 1)
128e84a324bSAshish Kumar 			puts("1\n");
129e84a324bSAshish Kumar 		else
130e84a324bSAshish Kumar 			puts("EMU\n");
131e84a324bSAshish Kumar 		break;
132e84a324bSAshish Kumar 
133e84a324bSAshish Kumar 	default:
134e84a324bSAshish Kumar 		printf("invalid setting of SW%u\n", QIXIS_LBMAP_SWITCH);
135e84a324bSAshish Kumar 		break;
136e84a324bSAshish Kumar 	}
137e84a324bSAshish Kumar 
1387769776aSAshish Kumar #ifdef CONFIG_TARGET_LS1088AQDS
1397769776aSAshish Kumar 	printf("FPGA: v%d (%s), build %d",
1407769776aSAshish Kumar 	       (int)QIXIS_READ(scver), qixis_read_tag(buf),
1417769776aSAshish Kumar 	       (int)qixis_read_minor());
1427769776aSAshish Kumar 	/* the timestamp string contains "\n" at the end */
1437769776aSAshish Kumar 	printf(" on %s", qixis_read_time(buf));
1447769776aSAshish Kumar #else
145e84a324bSAshish Kumar 	printf("CPLD: v%d.%d\n", QIXIS_READ(scver), QIXIS_READ(tagdata));
1467769776aSAshish Kumar #endif
147e84a324bSAshish Kumar 
148e84a324bSAshish Kumar 	/*
149e84a324bSAshish Kumar 	 * Display the actual SERDES reference clocks as configured by the
150e84a324bSAshish Kumar 	 * dip switches on the board.  Note that the SWx registers could
151e84a324bSAshish Kumar 	 * technically be set to force the reference clocks to match the
152e84a324bSAshish Kumar 	 * values that the SERDES expects (or vice versa).  For now, however,
153e84a324bSAshish Kumar 	 * we just display both values and hope the user notices when they
154e84a324bSAshish Kumar 	 * don't match.
155e84a324bSAshish Kumar 	 */
156e84a324bSAshish Kumar 	puts("SERDES1 Reference : ");
157e84a324bSAshish Kumar 	sw = QIXIS_READ(brdcfg[2]);
158e84a324bSAshish Kumar 	clock = (sw >> 6) & 3;
159e84a324bSAshish Kumar 	printf("Clock1 = %sMHz ", freq[clock]);
160e84a324bSAshish Kumar 	clock = (sw >> 4) & 3;
161e84a324bSAshish Kumar 	printf("Clock2 = %sMHz", freq[clock]);
162e84a324bSAshish Kumar 
163e84a324bSAshish Kumar 	puts("\nSERDES2 Reference : ");
164e84a324bSAshish Kumar 	clock = (sw >> 2) & 3;
165e84a324bSAshish Kumar 	printf("Clock1 = %sMHz ", freq[clock]);
166e84a324bSAshish Kumar 	clock = (sw >> 0) & 3;
167e84a324bSAshish Kumar 	printf("Clock2 = %sMHz\n", freq[clock]);
168e84a324bSAshish Kumar 
169e84a324bSAshish Kumar 	return 0;
170e84a324bSAshish Kumar }
171e84a324bSAshish Kumar 
172e84a324bSAshish Kumar bool if_board_diff_clk(void)
173e84a324bSAshish Kumar {
1747769776aSAshish Kumar #ifdef CONFIG_TARGET_LS1088AQDS
1757769776aSAshish Kumar 	u8 diff_conf = QIXIS_READ(brdcfg[11]);
1767769776aSAshish Kumar 	return diff_conf & 0x40;
1777769776aSAshish Kumar #else
178e84a324bSAshish Kumar 	u8 diff_conf = QIXIS_READ(dutcfg[11]);
179e84a324bSAshish Kumar 	return diff_conf & 0x80;
1807769776aSAshish Kumar #endif
181e84a324bSAshish Kumar }
182e84a324bSAshish Kumar 
183e84a324bSAshish Kumar unsigned long get_board_sys_clk(void)
184e84a324bSAshish Kumar {
185e84a324bSAshish Kumar 	u8 sysclk_conf = QIXIS_READ(brdcfg[1]);
186e84a324bSAshish Kumar 
187e84a324bSAshish Kumar 	switch (sysclk_conf & 0x0f) {
188e84a324bSAshish Kumar 	case QIXIS_SYSCLK_83:
189e84a324bSAshish Kumar 		return 83333333;
190e84a324bSAshish Kumar 	case QIXIS_SYSCLK_100:
191e84a324bSAshish Kumar 		return 100000000;
192e84a324bSAshish Kumar 	case QIXIS_SYSCLK_125:
193e84a324bSAshish Kumar 		return 125000000;
194e84a324bSAshish Kumar 	case QIXIS_SYSCLK_133:
195e84a324bSAshish Kumar 		return 133333333;
196e84a324bSAshish Kumar 	case QIXIS_SYSCLK_150:
197e84a324bSAshish Kumar 		return 150000000;
198e84a324bSAshish Kumar 	case QIXIS_SYSCLK_160:
199e84a324bSAshish Kumar 		return 160000000;
200e84a324bSAshish Kumar 	case QIXIS_SYSCLK_166:
201e84a324bSAshish Kumar 		return 166666666;
202e84a324bSAshish Kumar 	}
203e84a324bSAshish Kumar 
204e84a324bSAshish Kumar 	return 66666666;
205e84a324bSAshish Kumar }
206e84a324bSAshish Kumar 
207e84a324bSAshish Kumar unsigned long get_board_ddr_clk(void)
208e84a324bSAshish Kumar {
209e84a324bSAshish Kumar 	u8 ddrclk_conf = QIXIS_READ(brdcfg[1]);
210e84a324bSAshish Kumar 
211e84a324bSAshish Kumar 	if (if_board_diff_clk())
212e84a324bSAshish Kumar 		return get_board_sys_clk();
213e84a324bSAshish Kumar 	switch ((ddrclk_conf & 0x30) >> 4) {
214e84a324bSAshish Kumar 	case QIXIS_DDRCLK_100:
215e84a324bSAshish Kumar 		return 100000000;
216e84a324bSAshish Kumar 	case QIXIS_DDRCLK_125:
217e84a324bSAshish Kumar 		return 125000000;
218e84a324bSAshish Kumar 	case QIXIS_DDRCLK_133:
219e84a324bSAshish Kumar 		return 133333333;
220e84a324bSAshish Kumar 	}
221e84a324bSAshish Kumar 
222e84a324bSAshish Kumar 	return 66666666;
223e84a324bSAshish Kumar }
224e84a324bSAshish Kumar 
225e84a324bSAshish Kumar int select_i2c_ch_pca9547(u8 ch)
226e84a324bSAshish Kumar {
227e84a324bSAshish Kumar 	int ret;
228e84a324bSAshish Kumar 
229e84a324bSAshish Kumar 	ret = i2c_write(I2C_MUX_PCA_ADDR_PRI, 0, 1, &ch, 1);
230e84a324bSAshish Kumar 	if (ret) {
231e84a324bSAshish Kumar 		puts("PCA: failed to select proper channel\n");
232e84a324bSAshish Kumar 		return ret;
233e84a324bSAshish Kumar 	}
234e84a324bSAshish Kumar 
235e84a324bSAshish Kumar 	return 0;
236e84a324bSAshish Kumar }
237e84a324bSAshish Kumar 
238e84a324bSAshish Kumar void board_retimer_init(void)
239e84a324bSAshish Kumar {
240e84a324bSAshish Kumar 	u8 reg;
241e84a324bSAshish Kumar 
242e84a324bSAshish Kumar 	/* Retimer is connected to I2C1_CH5 */
243e84a324bSAshish Kumar 	select_i2c_ch_pca9547(I2C_MUX_CH5);
244e84a324bSAshish Kumar 
245e84a324bSAshish Kumar 	/* Access to Control/Shared register */
246e84a324bSAshish Kumar 	reg = 0x0;
247e84a324bSAshish Kumar 	i2c_write(I2C_RETIMER_ADDR, 0xff, 1, &reg, 1);
248e84a324bSAshish Kumar 
249e84a324bSAshish Kumar 	/* Read device revision and ID */
250e84a324bSAshish Kumar 	i2c_read(I2C_RETIMER_ADDR, 1, 1, &reg, 1);
251e84a324bSAshish Kumar 	debug("Retimer version id = 0x%x\n", reg);
252e84a324bSAshish Kumar 
253e84a324bSAshish Kumar 	/* Enable Broadcast. All writes target all channel register sets */
254e84a324bSAshish Kumar 	reg = 0x0c;
255e84a324bSAshish Kumar 	i2c_write(I2C_RETIMER_ADDR, 0xff, 1, &reg, 1);
256e84a324bSAshish Kumar 
257e84a324bSAshish Kumar 	/* Reset Channel Registers */
258e84a324bSAshish Kumar 	i2c_read(I2C_RETIMER_ADDR, 0, 1, &reg, 1);
259e84a324bSAshish Kumar 	reg |= 0x4;
260e84a324bSAshish Kumar 	i2c_write(I2C_RETIMER_ADDR, 0, 1, &reg, 1);
261e84a324bSAshish Kumar 
262e84a324bSAshish Kumar 	/* Set data rate as 10.3125 Gbps */
263e84a324bSAshish Kumar 	reg = 0x90;
264e84a324bSAshish Kumar 	i2c_write(I2C_RETIMER_ADDR, 0x60, 1, &reg, 1);
265e84a324bSAshish Kumar 	reg = 0xb3;
266e84a324bSAshish Kumar 	i2c_write(I2C_RETIMER_ADDR, 0x61, 1, &reg, 1);
267e84a324bSAshish Kumar 	reg = 0x90;
268e84a324bSAshish Kumar 	i2c_write(I2C_RETIMER_ADDR, 0x62, 1, &reg, 1);
269e84a324bSAshish Kumar 	reg = 0xb3;
270e84a324bSAshish Kumar 	i2c_write(I2C_RETIMER_ADDR, 0x63, 1, &reg, 1);
271e84a324bSAshish Kumar 	reg = 0xcd;
272e84a324bSAshish Kumar 	i2c_write(I2C_RETIMER_ADDR, 0x64, 1, &reg, 1);
273e84a324bSAshish Kumar 
274e84a324bSAshish Kumar 	/* Select VCO Divider to full rate (000) */
275e84a324bSAshish Kumar 	i2c_read(I2C_RETIMER_ADDR, 0x2F, 1, &reg, 1);
276e84a324bSAshish Kumar 	reg &= 0x0f;
277e84a324bSAshish Kumar 	reg |= 0x70;
278e84a324bSAshish Kumar 	i2c_write(I2C_RETIMER_ADDR, 0x2F, 1, &reg, 1);
279e84a324bSAshish Kumar 
2807769776aSAshish Kumar #ifdef	CONFIG_TARGET_LS1088AQDS
2817769776aSAshish Kumar 	/* Retimer is connected to I2C1_CH5 */
2827769776aSAshish Kumar 	select_i2c_ch_pca9547(I2C_MUX_CH5);
283e84a324bSAshish Kumar 
2847769776aSAshish Kumar 	/* Access to Control/Shared register */
2857769776aSAshish Kumar 	reg = 0x0;
2867769776aSAshish Kumar 	i2c_write(I2C_RETIMER_ADDR2, 0xff, 1, &reg, 1);
2877769776aSAshish Kumar 
2887769776aSAshish Kumar 	/* Read device revision and ID */
2897769776aSAshish Kumar 	i2c_read(I2C_RETIMER_ADDR2, 1, 1, &reg, 1);
2907769776aSAshish Kumar 	debug("Retimer version id = 0x%x\n", reg);
2917769776aSAshish Kumar 
2927769776aSAshish Kumar 	/* Enable Broadcast. All writes target all channel register sets */
2937769776aSAshish Kumar 	reg = 0x0c;
2947769776aSAshish Kumar 	i2c_write(I2C_RETIMER_ADDR2, 0xff, 1, &reg, 1);
2957769776aSAshish Kumar 
2967769776aSAshish Kumar 	/* Reset Channel Registers */
2977769776aSAshish Kumar 	i2c_read(I2C_RETIMER_ADDR2, 0, 1, &reg, 1);
2987769776aSAshish Kumar 	reg |= 0x4;
2997769776aSAshish Kumar 	i2c_write(I2C_RETIMER_ADDR2, 0, 1, &reg, 1);
3007769776aSAshish Kumar 
3017769776aSAshish Kumar 	/* Set data rate as 10.3125 Gbps */
3027769776aSAshish Kumar 	reg = 0x90;
3037769776aSAshish Kumar 	i2c_write(I2C_RETIMER_ADDR2, 0x60, 1, &reg, 1);
3047769776aSAshish Kumar 	reg = 0xb3;
3057769776aSAshish Kumar 	i2c_write(I2C_RETIMER_ADDR2, 0x61, 1, &reg, 1);
3067769776aSAshish Kumar 	reg = 0x90;
3077769776aSAshish Kumar 	i2c_write(I2C_RETIMER_ADDR2, 0x62, 1, &reg, 1);
3087769776aSAshish Kumar 	reg = 0xb3;
3097769776aSAshish Kumar 	i2c_write(I2C_RETIMER_ADDR2, 0x63, 1, &reg, 1);
3107769776aSAshish Kumar 	reg = 0xcd;
3117769776aSAshish Kumar 	i2c_write(I2C_RETIMER_ADDR2, 0x64, 1, &reg, 1);
3127769776aSAshish Kumar 
3137769776aSAshish Kumar 	/* Select VCO Divider to full rate (000) */
3147769776aSAshish Kumar 	i2c_read(I2C_RETIMER_ADDR2, 0x2F, 1, &reg, 1);
3157769776aSAshish Kumar 	reg &= 0x0f;
3167769776aSAshish Kumar 	reg |= 0x70;
3177769776aSAshish Kumar 	i2c_write(I2C_RETIMER_ADDR2, 0x2F, 1, &reg, 1);
3187769776aSAshish Kumar #endif
319e84a324bSAshish Kumar 	/*return the default channel*/
320e84a324bSAshish Kumar 	select_i2c_ch_pca9547(I2C_MUX_CH_DEFAULT);
321e84a324bSAshish Kumar }
322e84a324bSAshish Kumar 
32344cdb5b6SYangbo Lu #ifdef CONFIG_MISC_INIT_R
32444cdb5b6SYangbo Lu int misc_init_r(void)
32544cdb5b6SYangbo Lu {
32644cdb5b6SYangbo Lu #ifdef CONFIG_TARGET_LS1088ARDB
32744cdb5b6SYangbo Lu 	u8 brdcfg5;
32844cdb5b6SYangbo Lu 
32944cdb5b6SYangbo Lu 	if (hwconfig("esdhc-force-sd")) {
33044cdb5b6SYangbo Lu 		brdcfg5 = QIXIS_READ(brdcfg[5]);
33144cdb5b6SYangbo Lu 		brdcfg5 &= ~BRDCFG5_SPISDHC_MASK;
33244cdb5b6SYangbo Lu 		brdcfg5 |= BRDCFG5_FORCE_SD;
33344cdb5b6SYangbo Lu 		QIXIS_WRITE(brdcfg[5], brdcfg5);
33444cdb5b6SYangbo Lu 	}
33544cdb5b6SYangbo Lu #endif
33644cdb5b6SYangbo Lu 	return 0;
33744cdb5b6SYangbo Lu }
33844cdb5b6SYangbo Lu #endif
33944cdb5b6SYangbo Lu 
340*ef0789b7SRajesh Bhagat int i2c_multiplexer_select_vid_channel(u8 channel)
341*ef0789b7SRajesh Bhagat {
342*ef0789b7SRajesh Bhagat 	return select_i2c_ch_pca9547(channel);
343*ef0789b7SRajesh Bhagat }
344*ef0789b7SRajesh Bhagat 
345*ef0789b7SRajesh Bhagat #ifdef CONFIG_TARGET_LS1088AQDS
346*ef0789b7SRajesh Bhagat /* read the current value(SVDD) of the LTM Regulator Voltage */
347*ef0789b7SRajesh Bhagat int get_serdes_volt(void)
348*ef0789b7SRajesh Bhagat {
349*ef0789b7SRajesh Bhagat 	int  ret, vcode = 0;
350*ef0789b7SRajesh Bhagat 	u8 chan = PWM_CHANNEL0;
351*ef0789b7SRajesh Bhagat 
352*ef0789b7SRajesh Bhagat 	/* Select the PAGE 0 using PMBus commands PAGE for VDD */
353*ef0789b7SRajesh Bhagat 	ret = i2c_write(I2C_SVDD_MONITOR_ADDR,
354*ef0789b7SRajesh Bhagat 			PMBUS_CMD_PAGE, 1, &chan, 1);
355*ef0789b7SRajesh Bhagat 	if (ret) {
356*ef0789b7SRajesh Bhagat 		printf("VID: failed to select VDD Page 0\n");
357*ef0789b7SRajesh Bhagat 		return ret;
358*ef0789b7SRajesh Bhagat 	}
359*ef0789b7SRajesh Bhagat 
360*ef0789b7SRajesh Bhagat 	/* Read the output voltage using PMBus command READ_VOUT */
361*ef0789b7SRajesh Bhagat 	ret = i2c_read(I2C_SVDD_MONITOR_ADDR,
362*ef0789b7SRajesh Bhagat 		       PMBUS_CMD_READ_VOUT, 1, (void *)&vcode, 2);
363*ef0789b7SRajesh Bhagat 	if (ret) {
364*ef0789b7SRajesh Bhagat 		printf("VID: failed to read the volatge\n");
365*ef0789b7SRajesh Bhagat 		return ret;
366*ef0789b7SRajesh Bhagat 	}
367*ef0789b7SRajesh Bhagat 
368*ef0789b7SRajesh Bhagat 	return vcode;
369*ef0789b7SRajesh Bhagat }
370*ef0789b7SRajesh Bhagat 
371*ef0789b7SRajesh Bhagat int set_serdes_volt(int svdd)
372*ef0789b7SRajesh Bhagat {
373*ef0789b7SRajesh Bhagat 	int ret, vdd_last;
374*ef0789b7SRajesh Bhagat 	u8 buff[5] = {0x04, PWM_CHANNEL0, PMBUS_CMD_VOUT_COMMAND,
375*ef0789b7SRajesh Bhagat 			svdd & 0xFF, (svdd & 0xFF00) >> 8};
376*ef0789b7SRajesh Bhagat 
377*ef0789b7SRajesh Bhagat 	/* Write the desired voltage code to the SVDD regulator */
378*ef0789b7SRajesh Bhagat 	ret = i2c_write(I2C_SVDD_MONITOR_ADDR,
379*ef0789b7SRajesh Bhagat 			PMBUS_CMD_PAGE_PLUS_WRITE, 1, (void *)&buff, 5);
380*ef0789b7SRajesh Bhagat 	if (ret) {
381*ef0789b7SRajesh Bhagat 		printf("VID: I2C failed to write to the volatge regulator\n");
382*ef0789b7SRajesh Bhagat 		return -1;
383*ef0789b7SRajesh Bhagat 	}
384*ef0789b7SRajesh Bhagat 
385*ef0789b7SRajesh Bhagat 	/* Wait for the volatge to get to the desired value */
386*ef0789b7SRajesh Bhagat 	do {
387*ef0789b7SRajesh Bhagat 		vdd_last = get_serdes_volt();
388*ef0789b7SRajesh Bhagat 		if (vdd_last < 0) {
389*ef0789b7SRajesh Bhagat 			printf("VID: Couldn't read sensor abort VID adjust\n");
390*ef0789b7SRajesh Bhagat 			return -1;
391*ef0789b7SRajesh Bhagat 		}
392*ef0789b7SRajesh Bhagat 	} while (vdd_last != svdd);
393*ef0789b7SRajesh Bhagat 
394*ef0789b7SRajesh Bhagat 	return 1;
395*ef0789b7SRajesh Bhagat }
396*ef0789b7SRajesh Bhagat #else
397*ef0789b7SRajesh Bhagat int get_serdes_volt(void)
398*ef0789b7SRajesh Bhagat {
399*ef0789b7SRajesh Bhagat 	return 0;
400*ef0789b7SRajesh Bhagat }
401*ef0789b7SRajesh Bhagat 
402*ef0789b7SRajesh Bhagat int set_serdes_volt(int svdd)
403*ef0789b7SRajesh Bhagat {
404*ef0789b7SRajesh Bhagat 	int ret;
405*ef0789b7SRajesh Bhagat 	u8 brdcfg4;
406*ef0789b7SRajesh Bhagat 
407*ef0789b7SRajesh Bhagat 	printf("SVDD changing of RDB\n");
408*ef0789b7SRajesh Bhagat 
409*ef0789b7SRajesh Bhagat 	/* Read the BRDCFG54 via CLPD */
410*ef0789b7SRajesh Bhagat 	ret = i2c_read(CONFIG_SYS_I2C_FPGA_ADDR,
411*ef0789b7SRajesh Bhagat 		       QIXIS_BRDCFG4_OFFSET, 1, (void *)&brdcfg4, 1);
412*ef0789b7SRajesh Bhagat 	if (ret) {
413*ef0789b7SRajesh Bhagat 		printf("VID: I2C failed to read the CPLD BRDCFG4\n");
414*ef0789b7SRajesh Bhagat 		return -1;
415*ef0789b7SRajesh Bhagat 	}
416*ef0789b7SRajesh Bhagat 
417*ef0789b7SRajesh Bhagat 	brdcfg4 = brdcfg4 | 0x08;
418*ef0789b7SRajesh Bhagat 
419*ef0789b7SRajesh Bhagat 	/* Write to the BRDCFG4 */
420*ef0789b7SRajesh Bhagat 	ret = i2c_write(CONFIG_SYS_I2C_FPGA_ADDR,
421*ef0789b7SRajesh Bhagat 			QIXIS_BRDCFG4_OFFSET, 1, (void *)&brdcfg4, 1);
422*ef0789b7SRajesh Bhagat 	if (ret) {
423*ef0789b7SRajesh Bhagat 		debug("VID: I2C failed to set the SVDD CPLD BRDCFG4\n");
424*ef0789b7SRajesh Bhagat 		return -1;
425*ef0789b7SRajesh Bhagat 	}
426*ef0789b7SRajesh Bhagat 
427*ef0789b7SRajesh Bhagat 	/* Wait for the volatge to get to the desired value */
428*ef0789b7SRajesh Bhagat 	udelay(10000);
429*ef0789b7SRajesh Bhagat 
430*ef0789b7SRajesh Bhagat 	return 1;
431*ef0789b7SRajesh Bhagat }
432*ef0789b7SRajesh Bhagat #endif
433*ef0789b7SRajesh Bhagat 
434*ef0789b7SRajesh Bhagat /* this function disables the SERDES, changes the SVDD Voltage and enables it*/
435*ef0789b7SRajesh Bhagat int board_adjust_vdd(int vdd)
436*ef0789b7SRajesh Bhagat {
437*ef0789b7SRajesh Bhagat 	int ret = 0;
438*ef0789b7SRajesh Bhagat 
439*ef0789b7SRajesh Bhagat 	debug("%s: vdd = %d\n", __func__, vdd);
440*ef0789b7SRajesh Bhagat 
441*ef0789b7SRajesh Bhagat 	/* Special settings to be performed when voltage is 900mV */
442*ef0789b7SRajesh Bhagat 	if (vdd == 900) {
443*ef0789b7SRajesh Bhagat 		ret = setup_serdes_volt(vdd);
444*ef0789b7SRajesh Bhagat 		if (ret < 0) {
445*ef0789b7SRajesh Bhagat 			ret = -1;
446*ef0789b7SRajesh Bhagat 			goto exit;
447*ef0789b7SRajesh Bhagat 		}
448*ef0789b7SRajesh Bhagat 	}
449*ef0789b7SRajesh Bhagat exit:
450*ef0789b7SRajesh Bhagat 	return ret;
451*ef0789b7SRajesh Bhagat }
452*ef0789b7SRajesh Bhagat 
453e84a324bSAshish Kumar int board_init(void)
454e84a324bSAshish Kumar {
455e84a324bSAshish Kumar 	init_final_memctl_regs();
456e84a324bSAshish Kumar #if defined(CONFIG_TARGET_LS1088ARDB) && defined(CONFIG_FSL_MC_ENET)
457e84a324bSAshish Kumar 	u32 __iomem *irq_ccsr = (u32 __iomem *)ISC_BASE;
458e84a324bSAshish Kumar #endif
459e84a324bSAshish Kumar 
460e84a324bSAshish Kumar 	select_i2c_ch_pca9547(I2C_MUX_CH_DEFAULT);
461e84a324bSAshish Kumar 	board_retimer_init();
462e84a324bSAshish Kumar 
463e84a324bSAshish Kumar #ifdef CONFIG_ENV_IS_NOWHERE
464e84a324bSAshish Kumar 	gd->env_addr = (ulong)&default_environment[0];
465e84a324bSAshish Kumar #endif
466e84a324bSAshish Kumar 
467e84a324bSAshish Kumar #if defined(CONFIG_TARGET_LS1088ARDB) && defined(CONFIG_FSL_MC_ENET)
468e84a324bSAshish Kumar 	/* invert AQR105 IRQ pins polarity */
469e84a324bSAshish Kumar 	out_le32(irq_ccsr + IRQCR_OFFSET / 4, AQR105_IRQ_MASK);
470e84a324bSAshish Kumar #endif
471e84a324bSAshish Kumar 
47230c41d21SUdit Agarwal #ifdef CONFIG_FSL_CAAM
47330c41d21SUdit Agarwal 	sec_init();
47430c41d21SUdit Agarwal #endif
475e84a324bSAshish Kumar #ifdef CONFIG_FSL_LS_PPA
476e84a324bSAshish Kumar 	ppa_init();
477e84a324bSAshish Kumar #endif
478e84a324bSAshish Kumar 	return 0;
479e84a324bSAshish Kumar }
480e84a324bSAshish Kumar 
481e84a324bSAshish Kumar void detail_board_ddr_info(void)
482e84a324bSAshish Kumar {
483e84a324bSAshish Kumar 	puts("\nDDR    ");
484e84a324bSAshish Kumar 	print_size(gd->bd->bi_dram[0].size + gd->bd->bi_dram[1].size, "");
485e84a324bSAshish Kumar 	print_ddr_info(0);
486e84a324bSAshish Kumar }
487e84a324bSAshish Kumar 
488e84a324bSAshish Kumar #if defined(CONFIG_ARCH_MISC_INIT)
489e84a324bSAshish Kumar int arch_misc_init(void)
490e84a324bSAshish Kumar {
491e84a324bSAshish Kumar 	return 0;
492e84a324bSAshish Kumar }
493e84a324bSAshish Kumar #endif
494e84a324bSAshish Kumar 
495e84a324bSAshish Kumar #ifdef CONFIG_FSL_MC_ENET
496e84a324bSAshish Kumar void fdt_fixup_board_enet(void *fdt)
497e84a324bSAshish Kumar {
498e84a324bSAshish Kumar 	int offset;
499e84a324bSAshish Kumar 
500e84a324bSAshish Kumar 	offset = fdt_path_offset(fdt, "/fsl-mc");
501e84a324bSAshish Kumar 
502e84a324bSAshish Kumar 	if (offset < 0)
503e84a324bSAshish Kumar 		offset = fdt_path_offset(fdt, "/fsl,dprc@0");
504e84a324bSAshish Kumar 
505e84a324bSAshish Kumar 	if (offset < 0) {
506e84a324bSAshish Kumar 		printf("%s: ERROR: fsl-mc node not found in device tree (error %d)\n",
507e84a324bSAshish Kumar 		       __func__, offset);
508e84a324bSAshish Kumar 		return;
509e84a324bSAshish Kumar 	}
510e84a324bSAshish Kumar 
51170a131ebSYogesh Gaur 	if ((get_mc_boot_status() == 0) && (get_dpl_apply_status() == 0))
512e84a324bSAshish Kumar 		fdt_status_okay(fdt, offset);
513e84a324bSAshish Kumar 	else
514e84a324bSAshish Kumar 		fdt_status_fail(fdt, offset);
515e84a324bSAshish Kumar }
516e84a324bSAshish Kumar #endif
517e84a324bSAshish Kumar 
518e84a324bSAshish Kumar #ifdef CONFIG_OF_BOARD_SETUP
5196b6b7e8aSAshish Kumar void fsl_fdt_fixup_flash(void *fdt)
5206b6b7e8aSAshish Kumar {
5216b6b7e8aSAshish Kumar 	int offset;
5226b6b7e8aSAshish Kumar 
5236b6b7e8aSAshish Kumar /*
5246b6b7e8aSAshish Kumar  * IFC-NOR and QSPI are muxed on SoC.
5256b6b7e8aSAshish Kumar  * So disable IFC node in dts if QSPI is enabled or
5266b6b7e8aSAshish Kumar  * disable QSPI node in dts in case QSPI is not enabled.
5276b6b7e8aSAshish Kumar  */
5286b6b7e8aSAshish Kumar 
5296b6b7e8aSAshish Kumar #ifdef CONFIG_FSL_QSPI
5306b6b7e8aSAshish Kumar 	offset = fdt_path_offset(fdt, "/soc/ifc/nor");
5316b6b7e8aSAshish Kumar 
5326b6b7e8aSAshish Kumar 	if (offset < 0)
5336b6b7e8aSAshish Kumar 		offset = fdt_path_offset(fdt, "/ifc/nor");
5346b6b7e8aSAshish Kumar #else
5356b6b7e8aSAshish Kumar 	offset = fdt_path_offset(fdt, "/soc/quadspi");
5366b6b7e8aSAshish Kumar 
5376b6b7e8aSAshish Kumar 	if (offset < 0)
5386b6b7e8aSAshish Kumar 		offset = fdt_path_offset(fdt, "/quadspi");
5396b6b7e8aSAshish Kumar #endif
5406b6b7e8aSAshish Kumar 	if (offset < 0)
5416b6b7e8aSAshish Kumar 		return;
5426b6b7e8aSAshish Kumar 
5436b6b7e8aSAshish Kumar 	fdt_status_disabled(fdt, offset);
5446b6b7e8aSAshish Kumar }
5456b6b7e8aSAshish Kumar 
546e84a324bSAshish Kumar int ft_board_setup(void *blob, bd_t *bd)
547e84a324bSAshish Kumar {
548e84a324bSAshish Kumar 	int err, i;
549e84a324bSAshish Kumar 	u64 base[CONFIG_NR_DRAM_BANKS];
550e84a324bSAshish Kumar 	u64 size[CONFIG_NR_DRAM_BANKS];
551e84a324bSAshish Kumar 
552e84a324bSAshish Kumar 	ft_cpu_setup(blob, bd);
553e84a324bSAshish Kumar 
554e84a324bSAshish Kumar 	/* fixup DT for the two GPP DDR banks */
555e84a324bSAshish Kumar 	for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
556e84a324bSAshish Kumar 		base[i] = gd->bd->bi_dram[i].start;
557e84a324bSAshish Kumar 		size[i] = gd->bd->bi_dram[i].size;
558e84a324bSAshish Kumar 	}
559e84a324bSAshish Kumar 
560e84a324bSAshish Kumar #ifdef CONFIG_RESV_RAM
561e84a324bSAshish Kumar 	/* reduce size if reserved memory is within this bank */
562e84a324bSAshish Kumar 	if (gd->arch.resv_ram >= base[0] &&
563e84a324bSAshish Kumar 	    gd->arch.resv_ram < base[0] + size[0])
564e84a324bSAshish Kumar 		size[0] = gd->arch.resv_ram - base[0];
565e84a324bSAshish Kumar 	else if (gd->arch.resv_ram >= base[1] &&
566e84a324bSAshish Kumar 		 gd->arch.resv_ram < base[1] + size[1])
567e84a324bSAshish Kumar 		size[1] = gd->arch.resv_ram - base[1];
568e84a324bSAshish Kumar #endif
569e84a324bSAshish Kumar 
570e84a324bSAshish Kumar 	fdt_fixup_memory_banks(blob, base, size, CONFIG_NR_DRAM_BANKS);
571e84a324bSAshish Kumar 
5726b6b7e8aSAshish Kumar 	fsl_fdt_fixup_flash(blob);
5736b6b7e8aSAshish Kumar 
574e84a324bSAshish Kumar #ifdef CONFIG_FSL_MC_ENET
575e84a324bSAshish Kumar 	fdt_fixup_board_enet(blob);
576e84a324bSAshish Kumar 	err = fsl_mc_ldpaa_exit(bd);
577e84a324bSAshish Kumar 	if (err)
578e84a324bSAshish Kumar 		return err;
579e84a324bSAshish Kumar #endif
580e84a324bSAshish Kumar 
581e84a324bSAshish Kumar 	return 0;
582e84a324bSAshish Kumar }
583e84a324bSAshish Kumar #endif
58410e7eaf0SSumit Garg #endif /* defined(CONFIG_SPL_BUILD) */
585