xref: /arm-trusted-firmware/plat/rockchip/rk3399/drivers/dram/suspend.c (revision 91f16700b400a8c0651d24a598fc48ee2997a0d7)
1*91f16700Schasinglulu /*
2*91f16700Schasinglulu  * Copyright (c) 2016-2021, ARM Limited and Contributors. All rights reserved.
3*91f16700Schasinglulu  *
4*91f16700Schasinglulu  * SPDX-License-Identifier: BSD-3-Clause
5*91f16700Schasinglulu  */
6*91f16700Schasinglulu 
7*91f16700Schasinglulu #include <platform_def.h>
8*91f16700Schasinglulu 
9*91f16700Schasinglulu #include <arch_helpers.h>
10*91f16700Schasinglulu #include <common/debug.h>
11*91f16700Schasinglulu 
12*91f16700Schasinglulu #include <dram.h>
13*91f16700Schasinglulu #include <plat_private.h>
14*91f16700Schasinglulu #include <pmu.h>
15*91f16700Schasinglulu #include <pmu_bits.h>
16*91f16700Schasinglulu #include <pmu_regs.h>
17*91f16700Schasinglulu #include <rk3399_def.h>
18*91f16700Schasinglulu #include <secure.h>
19*91f16700Schasinglulu #include <soc.h>
20*91f16700Schasinglulu #include <suspend.h>
21*91f16700Schasinglulu 
22*91f16700Schasinglulu #define PMUGRF_OS_REG0			0x300
23*91f16700Schasinglulu #define PMUGRF_OS_REG1			0x304
24*91f16700Schasinglulu #define PMUGRF_OS_REG2			0x308
25*91f16700Schasinglulu #define PMUGRF_OS_REG3			0x30c
26*91f16700Schasinglulu 
27*91f16700Schasinglulu #define CRU_SFTRST_DDR_CTRL(ch, n)	((0x1 << (8 + 16 + (ch) * 4)) | \
28*91f16700Schasinglulu 					 ((n) << (8 + (ch) * 4)))
29*91f16700Schasinglulu #define CRU_SFTRST_DDR_PHY(ch, n)	((0x1 << (9 + 16 + (ch) * 4)) | \
30*91f16700Schasinglulu 					 ((n) << (9 + (ch) * 4)))
31*91f16700Schasinglulu 
32*91f16700Schasinglulu #define FBDIV_ENC(n)			((n) << 16)
33*91f16700Schasinglulu #define FBDIV_DEC(n)			(((n) >> 16) & 0xfff)
34*91f16700Schasinglulu #define POSTDIV2_ENC(n)			((n) << 12)
35*91f16700Schasinglulu #define POSTDIV2_DEC(n)			(((n) >> 12) & 0x7)
36*91f16700Schasinglulu #define POSTDIV1_ENC(n)			((n) << 8)
37*91f16700Schasinglulu #define POSTDIV1_DEC(n)			(((n) >> 8) & 0x7)
38*91f16700Schasinglulu #define REFDIV_ENC(n)			(n)
39*91f16700Schasinglulu #define REFDIV_DEC(n)			((n) & 0x3f)
40*91f16700Schasinglulu 
41*91f16700Schasinglulu /* PMU CRU */
42*91f16700Schasinglulu #define PMUCRU_RSTNHOLD_CON0		0x120
43*91f16700Schasinglulu #define PMUCRU_RSTNHOLD_CON1		0x124
44*91f16700Schasinglulu 
45*91f16700Schasinglulu #define PRESET_GPIO0_HOLD(n)		(((n) << 7) | WMSK_BIT(7))
46*91f16700Schasinglulu #define PRESET_GPIO1_HOLD(n)		(((n) << 8) | WMSK_BIT(8))
47*91f16700Schasinglulu 
48*91f16700Schasinglulu #define SYS_COUNTER_FREQ_IN_MHZ		(SYS_COUNTER_FREQ_IN_TICKS / 1000000)
49*91f16700Schasinglulu 
50*91f16700Schasinglulu __pmusramdata uint32_t dpll_data[PLL_CON_COUNT];
51*91f16700Schasinglulu __pmusramdata uint32_t cru_clksel_con6;
52*91f16700Schasinglulu __pmusramdata uint8_t pmu_enable_watchdog0;
53*91f16700Schasinglulu 
54*91f16700Schasinglulu /*
55*91f16700Schasinglulu  * Copy @num registers from @src to @dst
56*91f16700Schasinglulu  */
57*91f16700Schasinglulu static __pmusramfunc void sram_regcpy(uintptr_t dst, uintptr_t src,
58*91f16700Schasinglulu 		uint32_t num)
59*91f16700Schasinglulu {
60*91f16700Schasinglulu 	while (num--) {
61*91f16700Schasinglulu 		mmio_write_32(dst, mmio_read_32(src));
62*91f16700Schasinglulu 		dst += sizeof(uint32_t);
63*91f16700Schasinglulu 		src += sizeof(uint32_t);
64*91f16700Schasinglulu 	}
65*91f16700Schasinglulu }
66*91f16700Schasinglulu 
67*91f16700Schasinglulu /*
68*91f16700Schasinglulu  * Copy @num registers from @src to @dst
69*91f16700Schasinglulu  * This is intentionally a copy of the sram_regcpy function. PMUSRAM functions
70*91f16700Schasinglulu  * cannot be called from code running in DRAM.
71*91f16700Schasinglulu  */
72*91f16700Schasinglulu static void dram_regcpy(uintptr_t dst, uintptr_t src, uint32_t num)
73*91f16700Schasinglulu {
74*91f16700Schasinglulu 	while (num--) {
75*91f16700Schasinglulu 		mmio_write_32(dst, mmio_read_32(src));
76*91f16700Schasinglulu 		dst += sizeof(uint32_t);
77*91f16700Schasinglulu 		src += sizeof(uint32_t);
78*91f16700Schasinglulu 	}
79*91f16700Schasinglulu }
80*91f16700Schasinglulu 
81*91f16700Schasinglulu static __pmusramfunc uint32_t sram_get_timer_value(void)
82*91f16700Schasinglulu {
83*91f16700Schasinglulu 	/*
84*91f16700Schasinglulu 	 * Generic delay timer implementation expects the timer to be a down
85*91f16700Schasinglulu 	 * counter. We apply bitwise NOT operator to the tick values returned
86*91f16700Schasinglulu 	 * by read_cntpct_el0() to simulate the down counter.
87*91f16700Schasinglulu 	 */
88*91f16700Schasinglulu 	return (uint32_t)(~read_cntpct_el0());
89*91f16700Schasinglulu }
90*91f16700Schasinglulu 
91*91f16700Schasinglulu static __pmusramfunc void sram_udelay(uint32_t usec)
92*91f16700Schasinglulu {
93*91f16700Schasinglulu 	uint32_t start, cnt, delta, total_ticks;
94*91f16700Schasinglulu 
95*91f16700Schasinglulu 	/* counter is decreasing */
96*91f16700Schasinglulu 	start = sram_get_timer_value();
97*91f16700Schasinglulu 	total_ticks = usec * SYS_COUNTER_FREQ_IN_MHZ;
98*91f16700Schasinglulu 	do {
99*91f16700Schasinglulu 		cnt = sram_get_timer_value();
100*91f16700Schasinglulu 		if (cnt > start) {
101*91f16700Schasinglulu 			delta = UINT32_MAX - cnt;
102*91f16700Schasinglulu 			delta += start;
103*91f16700Schasinglulu 		} else
104*91f16700Schasinglulu 			delta = start - cnt;
105*91f16700Schasinglulu 	} while (delta <= total_ticks);
106*91f16700Schasinglulu }
107*91f16700Schasinglulu 
108*91f16700Schasinglulu static __pmusramfunc void configure_sgrf(void)
109*91f16700Schasinglulu {
110*91f16700Schasinglulu 	/*
111*91f16700Schasinglulu 	 * SGRF_DDR_RGN_DPLL_CLK and SGRF_DDR_RGN_RTC_CLK:
112*91f16700Schasinglulu 	 * IC ECO bug, need to set this register.
113*91f16700Schasinglulu 	 *
114*91f16700Schasinglulu 	 * SGRF_DDR_RGN_BYPS:
115*91f16700Schasinglulu 	 * After the PD_CENTER suspend/resume, the DDR region
116*91f16700Schasinglulu 	 * related registers in the SGRF will be reset, we
117*91f16700Schasinglulu 	 * need to re-initialize them.
118*91f16700Schasinglulu 	 */
119*91f16700Schasinglulu 	mmio_write_32(SGRF_BASE + SGRF_DDRRGN_CON0_16(16),
120*91f16700Schasinglulu 		      SGRF_DDR_RGN_DPLL_CLK |
121*91f16700Schasinglulu 		      SGRF_DDR_RGN_RTC_CLK |
122*91f16700Schasinglulu 		      SGRF_DDR_RGN_BYPS);
123*91f16700Schasinglulu }
124*91f16700Schasinglulu 
125*91f16700Schasinglulu static __pmusramfunc void rkclk_ddr_reset(uint32_t channel, uint32_t ctl,
126*91f16700Schasinglulu 		uint32_t phy)
127*91f16700Schasinglulu {
128*91f16700Schasinglulu 	channel &= 0x1;
129*91f16700Schasinglulu 	ctl &= 0x1;
130*91f16700Schasinglulu 	phy &= 0x1;
131*91f16700Schasinglulu 	mmio_write_32(CRU_BASE + CRU_SOFTRST_CON(4),
132*91f16700Schasinglulu 		      CRU_SFTRST_DDR_CTRL(channel, ctl) |
133*91f16700Schasinglulu 		      CRU_SFTRST_DDR_PHY(channel, phy));
134*91f16700Schasinglulu }
135*91f16700Schasinglulu 
136*91f16700Schasinglulu static __pmusramfunc void phy_pctrl_reset(uint32_t ch)
137*91f16700Schasinglulu {
138*91f16700Schasinglulu 	rkclk_ddr_reset(ch, 1, 1);
139*91f16700Schasinglulu 	sram_udelay(10);
140*91f16700Schasinglulu 	rkclk_ddr_reset(ch, 1, 0);
141*91f16700Schasinglulu 	sram_udelay(10);
142*91f16700Schasinglulu 	rkclk_ddr_reset(ch, 0, 0);
143*91f16700Schasinglulu 	sram_udelay(10);
144*91f16700Schasinglulu }
145*91f16700Schasinglulu 
146*91f16700Schasinglulu static __pmusramfunc void set_cs_training_index(uint32_t ch, uint32_t rank)
147*91f16700Schasinglulu {
148*91f16700Schasinglulu 	uint32_t byte;
149*91f16700Schasinglulu 
150*91f16700Schasinglulu 	/* PHY_8/136/264/392 phy_per_cs_training_index_X 1bit offset_24 */
151*91f16700Schasinglulu 	for (byte = 0; byte < 4; byte++)
152*91f16700Schasinglulu 		mmio_clrsetbits_32(PHY_REG(ch, 8 + (128 * byte)), 0x1 << 24,
153*91f16700Schasinglulu 				   rank << 24);
154*91f16700Schasinglulu }
155*91f16700Schasinglulu 
156*91f16700Schasinglulu static __pmusramfunc void select_per_cs_training_index(uint32_t ch,
157*91f16700Schasinglulu 		uint32_t rank)
158*91f16700Schasinglulu {
159*91f16700Schasinglulu 	/* PHY_84 PHY_PER_CS_TRAINING_EN_0 1bit offset_16 */
160*91f16700Schasinglulu 	if ((mmio_read_32(PHY_REG(ch, 84)) >> 16) & 1)
161*91f16700Schasinglulu 		set_cs_training_index(ch, rank);
162*91f16700Schasinglulu }
163*91f16700Schasinglulu 
164*91f16700Schasinglulu static __pmusramfunc void override_write_leveling_value(uint32_t ch)
165*91f16700Schasinglulu {
166*91f16700Schasinglulu 	uint32_t byte;
167*91f16700Schasinglulu 
168*91f16700Schasinglulu 	for (byte = 0; byte < 4; byte++) {
169*91f16700Schasinglulu 		/*
170*91f16700Schasinglulu 		 * PHY_8/136/264/392
171*91f16700Schasinglulu 		 * phy_per_cs_training_multicast_en_X 1bit offset_16
172*91f16700Schasinglulu 		 */
173*91f16700Schasinglulu 		mmio_clrsetbits_32(PHY_REG(ch, 8 + (128 * byte)), 0x1 << 16,
174*91f16700Schasinglulu 				   1 << 16);
175*91f16700Schasinglulu 		mmio_clrsetbits_32(PHY_REG(ch, 63 + (128 * byte)),
176*91f16700Schasinglulu 				   0xffffu << 16,
177*91f16700Schasinglulu 				   0x200 << 16);
178*91f16700Schasinglulu 	}
179*91f16700Schasinglulu 
180*91f16700Schasinglulu 	/* CTL_200 ctrlupd_req 1bit offset_8 */
181*91f16700Schasinglulu 	mmio_clrsetbits_32(CTL_REG(ch, 200), 0x1 << 8, 0x1 << 8);
182*91f16700Schasinglulu }
183*91f16700Schasinglulu 
184*91f16700Schasinglulu static __pmusramfunc int data_training(uint32_t ch,
185*91f16700Schasinglulu 		struct rk3399_sdram_params *sdram_params,
186*91f16700Schasinglulu 		uint32_t training_flag)
187*91f16700Schasinglulu {
188*91f16700Schasinglulu 	uint32_t obs_0, obs_1, obs_2, obs_3, obs_err = 0;
189*91f16700Schasinglulu 	uint32_t rank = sdram_params->ch[ch].rank;
190*91f16700Schasinglulu 	uint32_t rank_mask;
191*91f16700Schasinglulu 	uint32_t i, tmp;
192*91f16700Schasinglulu 
193*91f16700Schasinglulu 	if (sdram_params->dramtype == LPDDR4)
194*91f16700Schasinglulu 		rank_mask = (rank == 1) ? 0x5 : 0xf;
195*91f16700Schasinglulu 	else
196*91f16700Schasinglulu 		rank_mask = (rank == 1) ? 0x1 : 0x3;
197*91f16700Schasinglulu 
198*91f16700Schasinglulu 	/* PHY_927 PHY_PAD_DQS_DRIVE  RPULL offset_22 */
199*91f16700Schasinglulu 	mmio_setbits_32(PHY_REG(ch, 927), (1 << 22));
200*91f16700Schasinglulu 
201*91f16700Schasinglulu 	if (training_flag == PI_FULL_TRAINING) {
202*91f16700Schasinglulu 		if (sdram_params->dramtype == LPDDR4) {
203*91f16700Schasinglulu 			training_flag = PI_WRITE_LEVELING |
204*91f16700Schasinglulu 					PI_READ_GATE_TRAINING |
205*91f16700Schasinglulu 					PI_READ_LEVELING |
206*91f16700Schasinglulu 					PI_WDQ_LEVELING;
207*91f16700Schasinglulu 		} else if (sdram_params->dramtype == LPDDR3) {
208*91f16700Schasinglulu 			training_flag = PI_CA_TRAINING | PI_WRITE_LEVELING |
209*91f16700Schasinglulu 					PI_READ_GATE_TRAINING;
210*91f16700Schasinglulu 		} else if (sdram_params->dramtype == DDR3) {
211*91f16700Schasinglulu 			training_flag = PI_WRITE_LEVELING |
212*91f16700Schasinglulu 					PI_READ_GATE_TRAINING |
213*91f16700Schasinglulu 					PI_READ_LEVELING;
214*91f16700Schasinglulu 		}
215*91f16700Schasinglulu 	}
216*91f16700Schasinglulu 
217*91f16700Schasinglulu 	/* ca training(LPDDR4,LPDDR3 support) */
218*91f16700Schasinglulu 	if ((training_flag & PI_CA_TRAINING) == PI_CA_TRAINING) {
219*91f16700Schasinglulu 		for (i = 0; i < 4; i++) {
220*91f16700Schasinglulu 			if (!(rank_mask & (1 << i)))
221*91f16700Schasinglulu 				continue;
222*91f16700Schasinglulu 
223*91f16700Schasinglulu 			select_per_cs_training_index(ch, i);
224*91f16700Schasinglulu 			/* PI_100 PI_CALVL_EN:RW:8:2 */
225*91f16700Schasinglulu 			mmio_clrsetbits_32(PI_REG(ch, 100), 0x3 << 8, 0x2 << 8);
226*91f16700Schasinglulu 
227*91f16700Schasinglulu 			/* PI_92 PI_CALVL_REQ:WR:16:1,PI_CALVL_CS:RW:24:2 */
228*91f16700Schasinglulu 			mmio_clrsetbits_32(PI_REG(ch, 92),
229*91f16700Schasinglulu 					   (0x1 << 16) | (0x3 << 24),
230*91f16700Schasinglulu 					   (0x1 << 16) | (i << 24));
231*91f16700Schasinglulu 			while (1) {
232*91f16700Schasinglulu 				/* PI_174 PI_INT_STATUS:RD:8:18 */
233*91f16700Schasinglulu 				tmp = mmio_read_32(PI_REG(ch, 174)) >> 8;
234*91f16700Schasinglulu 
235*91f16700Schasinglulu 				/*
236*91f16700Schasinglulu 				 * check status obs
237*91f16700Schasinglulu 				 * PHY_532/660/788 phy_adr_calvl_obs1_:0:32
238*91f16700Schasinglulu 				 */
239*91f16700Schasinglulu 				obs_0 = mmio_read_32(PHY_REG(ch, 532));
240*91f16700Schasinglulu 				obs_1 = mmio_read_32(PHY_REG(ch, 660));
241*91f16700Schasinglulu 				obs_2 = mmio_read_32(PHY_REG(ch, 788));
242*91f16700Schasinglulu 				if (((obs_0 >> 30) & 0x3) ||
243*91f16700Schasinglulu 				    ((obs_1 >> 30) & 0x3) ||
244*91f16700Schasinglulu 				    ((obs_2 >> 30) & 0x3))
245*91f16700Schasinglulu 					obs_err = 1;
246*91f16700Schasinglulu 				if ((((tmp >> 11) & 0x1) == 0x1) &&
247*91f16700Schasinglulu 				    (((tmp >> 13) & 0x1) == 0x1) &&
248*91f16700Schasinglulu 				    (((tmp >> 5) & 0x1) == 0x0) &&
249*91f16700Schasinglulu 				    (obs_err == 0))
250*91f16700Schasinglulu 					break;
251*91f16700Schasinglulu 				else if ((((tmp >> 5) & 0x1) == 0x1) ||
252*91f16700Schasinglulu 					 (obs_err == 1))
253*91f16700Schasinglulu 					return -1;
254*91f16700Schasinglulu 			}
255*91f16700Schasinglulu 			/* clear interrupt,PI_175 PI_INT_ACK:WR:0:17 */
256*91f16700Schasinglulu 			mmio_write_32(PI_REG(ch, 175), 0x00003f7c);
257*91f16700Schasinglulu 		}
258*91f16700Schasinglulu 		mmio_clrbits_32(PI_REG(ch, 100), 0x3 << 8);
259*91f16700Schasinglulu 	}
260*91f16700Schasinglulu 
261*91f16700Schasinglulu 	/* write leveling(LPDDR4,LPDDR3,DDR3 support) */
262*91f16700Schasinglulu 	if ((training_flag & PI_WRITE_LEVELING) == PI_WRITE_LEVELING) {
263*91f16700Schasinglulu 		for (i = 0; i < rank; i++) {
264*91f16700Schasinglulu 			select_per_cs_training_index(ch, i);
265*91f16700Schasinglulu 			/* PI_60 PI_WRLVL_EN:RW:8:2 */
266*91f16700Schasinglulu 			mmio_clrsetbits_32(PI_REG(ch, 60), 0x3 << 8, 0x2 << 8);
267*91f16700Schasinglulu 			/* PI_59 PI_WRLVL_REQ:WR:8:1,PI_WRLVL_CS:RW:16:2 */
268*91f16700Schasinglulu 			mmio_clrsetbits_32(PI_REG(ch, 59),
269*91f16700Schasinglulu 					   (0x1 << 8) | (0x3 << 16),
270*91f16700Schasinglulu 					   (0x1 << 8) | (i << 16));
271*91f16700Schasinglulu 
272*91f16700Schasinglulu 			while (1) {
273*91f16700Schasinglulu 				/* PI_174 PI_INT_STATUS:RD:8:18 */
274*91f16700Schasinglulu 				tmp = mmio_read_32(PI_REG(ch, 174)) >> 8;
275*91f16700Schasinglulu 
276*91f16700Schasinglulu 				/*
277*91f16700Schasinglulu 				 * check status obs, if error maybe can not
278*91f16700Schasinglulu 				 * get leveling done PHY_40/168/296/424
279*91f16700Schasinglulu 				 * phy_wrlvl_status_obs_X:0:13
280*91f16700Schasinglulu 				 */
281*91f16700Schasinglulu 				obs_0 = mmio_read_32(PHY_REG(ch, 40));
282*91f16700Schasinglulu 				obs_1 = mmio_read_32(PHY_REG(ch, 168));
283*91f16700Schasinglulu 				obs_2 = mmio_read_32(PHY_REG(ch, 296));
284*91f16700Schasinglulu 				obs_3 = mmio_read_32(PHY_REG(ch, 424));
285*91f16700Schasinglulu 				if (((obs_0 >> 12) & 0x1) ||
286*91f16700Schasinglulu 				    ((obs_1 >> 12) & 0x1) ||
287*91f16700Schasinglulu 				    ((obs_2 >> 12) & 0x1) ||
288*91f16700Schasinglulu 				    ((obs_3 >> 12) & 0x1))
289*91f16700Schasinglulu 					obs_err = 1;
290*91f16700Schasinglulu 				if ((((tmp >> 10) & 0x1) == 0x1) &&
291*91f16700Schasinglulu 				    (((tmp >> 13) & 0x1) == 0x1) &&
292*91f16700Schasinglulu 				    (((tmp >> 4) & 0x1) == 0x0) &&
293*91f16700Schasinglulu 				    (obs_err == 0))
294*91f16700Schasinglulu 					break;
295*91f16700Schasinglulu 				else if ((((tmp >> 4) & 0x1) == 0x1) ||
296*91f16700Schasinglulu 					 (obs_err == 1))
297*91f16700Schasinglulu 					return -1;
298*91f16700Schasinglulu 			}
299*91f16700Schasinglulu 
300*91f16700Schasinglulu 			/* clear interrupt,PI_175 PI_INT_ACK:WR:0:17 */
301*91f16700Schasinglulu 			mmio_write_32(PI_REG(ch, 175), 0x00003f7c);
302*91f16700Schasinglulu 		}
303*91f16700Schasinglulu 		override_write_leveling_value(ch);
304*91f16700Schasinglulu 		mmio_clrbits_32(PI_REG(ch, 60), 0x3 << 8);
305*91f16700Schasinglulu 	}
306*91f16700Schasinglulu 
307*91f16700Schasinglulu 	/* read gate training(LPDDR4,LPDDR3,DDR3 support) */
308*91f16700Schasinglulu 	if ((training_flag & PI_READ_GATE_TRAINING) == PI_READ_GATE_TRAINING) {
309*91f16700Schasinglulu 		for (i = 0; i < rank; i++) {
310*91f16700Schasinglulu 			select_per_cs_training_index(ch, i);
311*91f16700Schasinglulu 			/* PI_80 PI_RDLVL_GATE_EN:RW:24:2 */
312*91f16700Schasinglulu 			mmio_clrsetbits_32(PI_REG(ch, 80), 0x3 << 24,
313*91f16700Schasinglulu 					   0x2 << 24);
314*91f16700Schasinglulu 			/*
315*91f16700Schasinglulu 			 * PI_74 PI_RDLVL_GATE_REQ:WR:16:1
316*91f16700Schasinglulu 			 * PI_RDLVL_CS:RW:24:2
317*91f16700Schasinglulu 			 */
318*91f16700Schasinglulu 			mmio_clrsetbits_32(PI_REG(ch, 74),
319*91f16700Schasinglulu 					   (0x1 << 16) | (0x3 << 24),
320*91f16700Schasinglulu 					   (0x1 << 16) | (i << 24));
321*91f16700Schasinglulu 
322*91f16700Schasinglulu 			while (1) {
323*91f16700Schasinglulu 				/* PI_174 PI_INT_STATUS:RD:8:18 */
324*91f16700Schasinglulu 				tmp = mmio_read_32(PI_REG(ch, 174)) >> 8;
325*91f16700Schasinglulu 
326*91f16700Schasinglulu 				/*
327*91f16700Schasinglulu 				 * check status obs
328*91f16700Schasinglulu 				 * PHY_43/171/299/427
329*91f16700Schasinglulu 				 *     PHY_GTLVL_STATUS_OBS_x:16:8
330*91f16700Schasinglulu 				 */
331*91f16700Schasinglulu 				obs_0 = mmio_read_32(PHY_REG(ch, 43));
332*91f16700Schasinglulu 				obs_1 = mmio_read_32(PHY_REG(ch, 171));
333*91f16700Schasinglulu 				obs_2 = mmio_read_32(PHY_REG(ch, 299));
334*91f16700Schasinglulu 				obs_3 = mmio_read_32(PHY_REG(ch, 427));
335*91f16700Schasinglulu 				if (((obs_0 >> (16 + 6)) & 0x3) ||
336*91f16700Schasinglulu 				    ((obs_1 >> (16 + 6)) & 0x3) ||
337*91f16700Schasinglulu 				    ((obs_2 >> (16 + 6)) & 0x3) ||
338*91f16700Schasinglulu 				    ((obs_3 >> (16 + 6)) & 0x3))
339*91f16700Schasinglulu 					obs_err = 1;
340*91f16700Schasinglulu 				if ((((tmp >> 9) & 0x1) == 0x1) &&
341*91f16700Schasinglulu 				    (((tmp >> 13) & 0x1) == 0x1) &&
342*91f16700Schasinglulu 				    (((tmp >> 3) & 0x1) == 0x0) &&
343*91f16700Schasinglulu 				    (obs_err == 0))
344*91f16700Schasinglulu 					break;
345*91f16700Schasinglulu 				else if ((((tmp >> 3) & 0x1) == 0x1) ||
346*91f16700Schasinglulu 					 (obs_err == 1))
347*91f16700Schasinglulu 					return -1;
348*91f16700Schasinglulu 			}
349*91f16700Schasinglulu 			/* clear interrupt,PI_175 PI_INT_ACK:WR:0:17 */
350*91f16700Schasinglulu 			mmio_write_32(PI_REG(ch, 175), 0x00003f7c);
351*91f16700Schasinglulu 		}
352*91f16700Schasinglulu 		mmio_clrbits_32(PI_REG(ch, 80), 0x3 << 24);
353*91f16700Schasinglulu 	}
354*91f16700Schasinglulu 
355*91f16700Schasinglulu 	/* read leveling(LPDDR4,LPDDR3,DDR3 support) */
356*91f16700Schasinglulu 	if ((training_flag & PI_READ_LEVELING) == PI_READ_LEVELING) {
357*91f16700Schasinglulu 		for (i = 0; i < rank; i++) {
358*91f16700Schasinglulu 			select_per_cs_training_index(ch, i);
359*91f16700Schasinglulu 			/* PI_80 PI_RDLVL_EN:RW:16:2 */
360*91f16700Schasinglulu 			mmio_clrsetbits_32(PI_REG(ch, 80), 0x3 << 16,
361*91f16700Schasinglulu 					   0x2 << 16);
362*91f16700Schasinglulu 			/* PI_74 PI_RDLVL_REQ:WR:8:1,PI_RDLVL_CS:RW:24:2 */
363*91f16700Schasinglulu 			mmio_clrsetbits_32(PI_REG(ch, 74),
364*91f16700Schasinglulu 					   (0x1 << 8) | (0x3 << 24),
365*91f16700Schasinglulu 					   (0x1 << 8) | (i << 24));
366*91f16700Schasinglulu 			while (1) {
367*91f16700Schasinglulu 				/* PI_174 PI_INT_STATUS:RD:8:18 */
368*91f16700Schasinglulu 				tmp = mmio_read_32(PI_REG(ch, 174)) >> 8;
369*91f16700Schasinglulu 
370*91f16700Schasinglulu 				/*
371*91f16700Schasinglulu 				 * make sure status obs not report error bit
372*91f16700Schasinglulu 				 * PHY_46/174/302/430
373*91f16700Schasinglulu 				 *     phy_rdlvl_status_obs_X:16:8
374*91f16700Schasinglulu 				 */
375*91f16700Schasinglulu 				if ((((tmp >> 8) & 0x1) == 0x1) &&
376*91f16700Schasinglulu 				    (((tmp >> 13) & 0x1) == 0x1) &&
377*91f16700Schasinglulu 				    (((tmp >> 2) & 0x1) == 0x0))
378*91f16700Schasinglulu 					break;
379*91f16700Schasinglulu 				else if (((tmp >> 2) & 0x1) == 0x1)
380*91f16700Schasinglulu 					return -1;
381*91f16700Schasinglulu 			}
382*91f16700Schasinglulu 			/* clear interrupt,PI_175 PI_INT_ACK:WR:0:17 */
383*91f16700Schasinglulu 			mmio_write_32(PI_REG(ch, 175), 0x00003f7c);
384*91f16700Schasinglulu 		}
385*91f16700Schasinglulu 		mmio_clrbits_32(PI_REG(ch, 80), 0x3 << 16);
386*91f16700Schasinglulu 	}
387*91f16700Schasinglulu 
388*91f16700Schasinglulu 	/* wdq leveling(LPDDR4 support) */
389*91f16700Schasinglulu 	if ((training_flag & PI_WDQ_LEVELING) == PI_WDQ_LEVELING) {
390*91f16700Schasinglulu 		for (i = 0; i < 4; i++) {
391*91f16700Schasinglulu 			if (!(rank_mask & (1 << i)))
392*91f16700Schasinglulu 				continue;
393*91f16700Schasinglulu 
394*91f16700Schasinglulu 			select_per_cs_training_index(ch, i);
395*91f16700Schasinglulu 			/*
396*91f16700Schasinglulu 			 * disable PI_WDQLVL_VREF_EN before wdq leveling?
397*91f16700Schasinglulu 			 * PI_181 PI_WDQLVL_VREF_EN:RW:8:1
398*91f16700Schasinglulu 			 */
399*91f16700Schasinglulu 			mmio_clrbits_32(PI_REG(ch, 181), 0x1 << 8);
400*91f16700Schasinglulu 			/* PI_124 PI_WDQLVL_EN:RW:16:2 */
401*91f16700Schasinglulu 			mmio_clrsetbits_32(PI_REG(ch, 124), 0x3 << 16,
402*91f16700Schasinglulu 					   0x2 << 16);
403*91f16700Schasinglulu 			/* PI_121 PI_WDQLVL_REQ:WR:8:1,PI_WDQLVL_CS:RW:16:2 */
404*91f16700Schasinglulu 			mmio_clrsetbits_32(PI_REG(ch, 121),
405*91f16700Schasinglulu 					   (0x1 << 8) | (0x3 << 16),
406*91f16700Schasinglulu 					   (0x1 << 8) | (i << 16));
407*91f16700Schasinglulu 			while (1) {
408*91f16700Schasinglulu 				/* PI_174 PI_INT_STATUS:RD:8:18 */
409*91f16700Schasinglulu 				tmp = mmio_read_32(PI_REG(ch, 174)) >> 8;
410*91f16700Schasinglulu 				if ((((tmp >> 12) & 0x1) == 0x1) &&
411*91f16700Schasinglulu 				    (((tmp >> 13) & 0x1) == 0x1) &&
412*91f16700Schasinglulu 				    (((tmp >> 6) & 0x1) == 0x0))
413*91f16700Schasinglulu 					break;
414*91f16700Schasinglulu 				else if (((tmp >> 6) & 0x1) == 0x1)
415*91f16700Schasinglulu 					return -1;
416*91f16700Schasinglulu 			}
417*91f16700Schasinglulu 			/* clear interrupt,PI_175 PI_INT_ACK:WR:0:17 */
418*91f16700Schasinglulu 			mmio_write_32(PI_REG(ch, 175), 0x00003f7c);
419*91f16700Schasinglulu 		}
420*91f16700Schasinglulu 		mmio_clrbits_32(PI_REG(ch, 124), 0x3 << 16);
421*91f16700Schasinglulu 	}
422*91f16700Schasinglulu 
423*91f16700Schasinglulu 	/* PHY_927 PHY_PAD_DQS_DRIVE  RPULL offset_22 */
424*91f16700Schasinglulu 	mmio_clrbits_32(PHY_REG(ch, 927), (1 << 22));
425*91f16700Schasinglulu 
426*91f16700Schasinglulu 	return 0;
427*91f16700Schasinglulu }
428*91f16700Schasinglulu 
429*91f16700Schasinglulu static __pmusramfunc void set_ddrconfig(
430*91f16700Schasinglulu 		struct rk3399_sdram_params *sdram_params,
431*91f16700Schasinglulu 		unsigned char channel, uint32_t ddrconfig)
432*91f16700Schasinglulu {
433*91f16700Schasinglulu 	/* only need to set ddrconfig */
434*91f16700Schasinglulu 	struct rk3399_sdram_channel *ch = &sdram_params->ch[channel];
435*91f16700Schasinglulu 	unsigned int cs0_cap = 0;
436*91f16700Schasinglulu 	unsigned int cs1_cap = 0;
437*91f16700Schasinglulu 
438*91f16700Schasinglulu 	cs0_cap = (1 << (ch->cs0_row + ch->col + ch->bk + ch->bw - 20));
439*91f16700Schasinglulu 	if (ch->rank > 1)
440*91f16700Schasinglulu 		cs1_cap = cs0_cap >> (ch->cs0_row - ch->cs1_row);
441*91f16700Schasinglulu 	if (ch->row_3_4) {
442*91f16700Schasinglulu 		cs0_cap = cs0_cap * 3 / 4;
443*91f16700Schasinglulu 		cs1_cap = cs1_cap * 3 / 4;
444*91f16700Schasinglulu 	}
445*91f16700Schasinglulu 
446*91f16700Schasinglulu 	mmio_write_32(MSCH_BASE(channel) + MSCH_DEVICECONF,
447*91f16700Schasinglulu 		      ddrconfig | (ddrconfig << 6));
448*91f16700Schasinglulu 	mmio_write_32(MSCH_BASE(channel) + MSCH_DEVICESIZE,
449*91f16700Schasinglulu 		      ((cs0_cap / 32) & 0xff) | (((cs1_cap / 32) & 0xff) << 8));
450*91f16700Schasinglulu }
451*91f16700Schasinglulu 
452*91f16700Schasinglulu static __pmusramfunc void dram_all_config(
453*91f16700Schasinglulu 		struct rk3399_sdram_params *sdram_params)
454*91f16700Schasinglulu {
455*91f16700Schasinglulu 	unsigned int i;
456*91f16700Schasinglulu 
457*91f16700Schasinglulu 	for (i = 0; i < 2; i++) {
458*91f16700Schasinglulu 		struct rk3399_sdram_channel *info = &sdram_params->ch[i];
459*91f16700Schasinglulu 		struct rk3399_msch_timings *noc = &info->noc_timings;
460*91f16700Schasinglulu 
461*91f16700Schasinglulu 		if (sdram_params->ch[i].col == 0)
462*91f16700Schasinglulu 			continue;
463*91f16700Schasinglulu 
464*91f16700Schasinglulu 		mmio_write_32(MSCH_BASE(i) + MSCH_DDRTIMINGA0,
465*91f16700Schasinglulu 			      noc->ddrtiminga0.d32);
466*91f16700Schasinglulu 		mmio_write_32(MSCH_BASE(i) + MSCH_DDRTIMINGB0,
467*91f16700Schasinglulu 			      noc->ddrtimingb0.d32);
468*91f16700Schasinglulu 		mmio_write_32(MSCH_BASE(i) + MSCH_DDRTIMINGC0,
469*91f16700Schasinglulu 			      noc->ddrtimingc0.d32);
470*91f16700Schasinglulu 		mmio_write_32(MSCH_BASE(i) + MSCH_DEVTODEV0,
471*91f16700Schasinglulu 			      noc->devtodev0.d32);
472*91f16700Schasinglulu 		mmio_write_32(MSCH_BASE(i) + MSCH_DDRMODE, noc->ddrmode.d32);
473*91f16700Schasinglulu 
474*91f16700Schasinglulu 		/* rank 1 memory clock disable (dfi_dram_clk_disable = 1) */
475*91f16700Schasinglulu 		if (sdram_params->ch[i].rank == 1)
476*91f16700Schasinglulu 			mmio_setbits_32(CTL_REG(i, 276), 1 << 17);
477*91f16700Schasinglulu 	}
478*91f16700Schasinglulu 
479*91f16700Schasinglulu 	DDR_STRIDE(sdram_params->stride);
480*91f16700Schasinglulu 
481*91f16700Schasinglulu 	/* reboot hold register set */
482*91f16700Schasinglulu 	mmio_write_32(PMUCRU_BASE + CRU_PMU_RSTHOLD_CON(1),
483*91f16700Schasinglulu 		      CRU_PMU_SGRF_RST_RLS |
484*91f16700Schasinglulu 		      PRESET_GPIO0_HOLD(1) |
485*91f16700Schasinglulu 		      PRESET_GPIO1_HOLD(1));
486*91f16700Schasinglulu 	mmio_clrsetbits_32(CRU_BASE + CRU_GLB_RST_CON, 0x3, 0x3);
487*91f16700Schasinglulu }
488*91f16700Schasinglulu 
489*91f16700Schasinglulu static __pmusramfunc void pctl_cfg(uint32_t ch,
490*91f16700Schasinglulu 		struct rk3399_sdram_params *sdram_params)
491*91f16700Schasinglulu {
492*91f16700Schasinglulu 	const uint32_t *params_ctl = sdram_params->pctl_regs.denali_ctl;
493*91f16700Schasinglulu 	const uint32_t *params_pi = sdram_params->pi_regs.denali_pi;
494*91f16700Schasinglulu 	const struct rk3399_ddr_publ_regs *phy_regs = &sdram_params->phy_regs;
495*91f16700Schasinglulu 	uint32_t tmp, tmp1, tmp2, i;
496*91f16700Schasinglulu 
497*91f16700Schasinglulu 	/*
498*91f16700Schasinglulu 	 * Workaround controller bug:
499*91f16700Schasinglulu 	 * Do not program DRAM_CLASS until NO_PHY_IND_TRAIN_INT is programmed
500*91f16700Schasinglulu 	 */
501*91f16700Schasinglulu 	sram_regcpy(CTL_REG(ch, 1), (uintptr_t)&params_ctl[1],
502*91f16700Schasinglulu 		    CTL_REG_NUM - 1);
503*91f16700Schasinglulu 	mmio_write_32(CTL_REG(ch, 0), params_ctl[0]);
504*91f16700Schasinglulu 	sram_regcpy(PI_REG(ch, 0), (uintptr_t)&params_pi[0],
505*91f16700Schasinglulu 		    PI_REG_NUM);
506*91f16700Schasinglulu 
507*91f16700Schasinglulu 	sram_regcpy(PHY_REG(ch, 910), (uintptr_t)&phy_regs->phy896[910 - 896],
508*91f16700Schasinglulu 		    3);
509*91f16700Schasinglulu 
510*91f16700Schasinglulu 	mmio_clrsetbits_32(CTL_REG(ch, 68), PWRUP_SREFRESH_EXIT,
511*91f16700Schasinglulu 				PWRUP_SREFRESH_EXIT);
512*91f16700Schasinglulu 
513*91f16700Schasinglulu 	/* PHY_DLL_RST_EN */
514*91f16700Schasinglulu 	mmio_clrsetbits_32(PHY_REG(ch, 957), 0x3 << 24, 1 << 24);
515*91f16700Schasinglulu 	dmbst();
516*91f16700Schasinglulu 
517*91f16700Schasinglulu 	mmio_setbits_32(PI_REG(ch, 0), START);
518*91f16700Schasinglulu 	mmio_setbits_32(CTL_REG(ch, 0), START);
519*91f16700Schasinglulu 
520*91f16700Schasinglulu 	/* wait lock */
521*91f16700Schasinglulu 	while (1) {
522*91f16700Schasinglulu 		tmp = mmio_read_32(PHY_REG(ch, 920));
523*91f16700Schasinglulu 		tmp1 = mmio_read_32(PHY_REG(ch, 921));
524*91f16700Schasinglulu 		tmp2 = mmio_read_32(PHY_REG(ch, 922));
525*91f16700Schasinglulu 		if ((((tmp >> 16) & 0x1) == 0x1) &&
526*91f16700Schasinglulu 		     (((tmp1 >> 16) & 0x1) == 0x1) &&
527*91f16700Schasinglulu 		     (((tmp1 >> 0) & 0x1) == 0x1) &&
528*91f16700Schasinglulu 		     (((tmp2 >> 0) & 0x1) == 0x1))
529*91f16700Schasinglulu 			break;
530*91f16700Schasinglulu 		/* if PLL bypass,don't need wait lock */
531*91f16700Schasinglulu 		if (mmio_read_32(PHY_REG(ch, 911)) & 0x1)
532*91f16700Schasinglulu 			break;
533*91f16700Schasinglulu 	}
534*91f16700Schasinglulu 
535*91f16700Schasinglulu 	sram_regcpy(PHY_REG(ch, 896), (uintptr_t)&phy_regs->phy896[0], 63);
536*91f16700Schasinglulu 
537*91f16700Schasinglulu 	for (i = 0; i < 4; i++)
538*91f16700Schasinglulu 		sram_regcpy(PHY_REG(ch, 128 * i),
539*91f16700Schasinglulu 			    (uintptr_t)&phy_regs->phy0[0], 91);
540*91f16700Schasinglulu 
541*91f16700Schasinglulu 	for (i = 0; i < 3; i++)
542*91f16700Schasinglulu 		sram_regcpy(PHY_REG(ch, 512 + 128 * i),
543*91f16700Schasinglulu 				(uintptr_t)&phy_regs->phy512[i][0], 38);
544*91f16700Schasinglulu }
545*91f16700Schasinglulu 
546*91f16700Schasinglulu static __pmusramfunc int dram_switch_to_next_index(
547*91f16700Schasinglulu 		struct rk3399_sdram_params *sdram_params)
548*91f16700Schasinglulu {
549*91f16700Schasinglulu 	uint32_t ch, ch_count;
550*91f16700Schasinglulu 	uint32_t fn = ((mmio_read_32(CTL_REG(0, 111)) >> 16) + 1) & 0x1;
551*91f16700Schasinglulu 
552*91f16700Schasinglulu 	mmio_write_32(CIC_BASE + CIC_CTRL0,
553*91f16700Schasinglulu 		      (((0x3 << 4) | (1 << 2) | 1) << 16) |
554*91f16700Schasinglulu 		      (fn << 4) | (1 << 2) | 1);
555*91f16700Schasinglulu 	while (!(mmio_read_32(CIC_BASE + CIC_STATUS0) & (1 << 2)))
556*91f16700Schasinglulu 		;
557*91f16700Schasinglulu 
558*91f16700Schasinglulu 	mmio_write_32(CIC_BASE + CIC_CTRL0, 0x20002);
559*91f16700Schasinglulu 	while (!(mmio_read_32(CIC_BASE + CIC_STATUS0) & (1 << 0)))
560*91f16700Schasinglulu 		;
561*91f16700Schasinglulu 
562*91f16700Schasinglulu 	ch_count = sdram_params->num_channels;
563*91f16700Schasinglulu 
564*91f16700Schasinglulu 	/* LPDDR4 f2 can't do training, all training will fail */
565*91f16700Schasinglulu 	for (ch = 0; ch < ch_count; ch++) {
566*91f16700Schasinglulu 		/*
567*91f16700Schasinglulu 		 * Without this disabled for LPDDR4 we end up writing 0's
568*91f16700Schasinglulu 		 * in place of real data in an interesting pattern.
569*91f16700Schasinglulu 		 */
570*91f16700Schasinglulu 		if (sdram_params->dramtype != LPDDR4) {
571*91f16700Schasinglulu 			mmio_clrsetbits_32(PHY_REG(ch, 896), (0x3 << 8) | 1,
572*91f16700Schasinglulu 					fn << 8);
573*91f16700Schasinglulu 		}
574*91f16700Schasinglulu 
575*91f16700Schasinglulu 		/* data_training failed */
576*91f16700Schasinglulu 		if (data_training(ch, sdram_params, PI_FULL_TRAINING))
577*91f16700Schasinglulu 			return -1;
578*91f16700Schasinglulu 	}
579*91f16700Schasinglulu 
580*91f16700Schasinglulu 	return 0;
581*91f16700Schasinglulu }
582*91f16700Schasinglulu 
583*91f16700Schasinglulu /*
584*91f16700Schasinglulu  * Needs to be done for both channels at once in case of a shared reset signal
585*91f16700Schasinglulu  * between channels.
586*91f16700Schasinglulu  */
587*91f16700Schasinglulu static __pmusramfunc int pctl_start(uint32_t channel_mask,
588*91f16700Schasinglulu 		struct rk3399_sdram_params *sdram_params)
589*91f16700Schasinglulu {
590*91f16700Schasinglulu 	uint32_t count;
591*91f16700Schasinglulu 	uint32_t byte;
592*91f16700Schasinglulu 
593*91f16700Schasinglulu 	mmio_setbits_32(CTL_REG(0, 68), PWRUP_SREFRESH_EXIT);
594*91f16700Schasinglulu 	mmio_setbits_32(CTL_REG(1, 68), PWRUP_SREFRESH_EXIT);
595*91f16700Schasinglulu 
596*91f16700Schasinglulu 	/* need de-access IO retention before controller START */
597*91f16700Schasinglulu 	if (channel_mask & (1 << 0))
598*91f16700Schasinglulu 		mmio_setbits_32(PMU_BASE + PMU_PWRMODE_CON, (1 << 19));
599*91f16700Schasinglulu 	if (channel_mask & (1 << 1))
600*91f16700Schasinglulu 		mmio_setbits_32(PMU_BASE + PMU_PWRMODE_CON, (1 << 23));
601*91f16700Schasinglulu 
602*91f16700Schasinglulu 	/* PHY_DLL_RST_EN */
603*91f16700Schasinglulu 	if (channel_mask & (1 << 0))
604*91f16700Schasinglulu 		mmio_clrsetbits_32(PHY_REG(0, 957), 0x3 << 24,
605*91f16700Schasinglulu 				   0x2 << 24);
606*91f16700Schasinglulu 	if (channel_mask & (1 << 1))
607*91f16700Schasinglulu 		mmio_clrsetbits_32(PHY_REG(1, 957), 0x3 << 24,
608*91f16700Schasinglulu 				   0x2 << 24);
609*91f16700Schasinglulu 
610*91f16700Schasinglulu 	/* check ERROR bit */
611*91f16700Schasinglulu 	if (channel_mask & (1 << 0)) {
612*91f16700Schasinglulu 		count = 0;
613*91f16700Schasinglulu 		while (!(mmio_read_32(CTL_REG(0, 203)) & (1 << 3))) {
614*91f16700Schasinglulu 			/* CKE is low, loop 10ms */
615*91f16700Schasinglulu 			if (count > 100)
616*91f16700Schasinglulu 				return -1;
617*91f16700Schasinglulu 
618*91f16700Schasinglulu 			sram_udelay(100);
619*91f16700Schasinglulu 			count++;
620*91f16700Schasinglulu 		}
621*91f16700Schasinglulu 
622*91f16700Schasinglulu 		mmio_clrbits_32(CTL_REG(0, 68), PWRUP_SREFRESH_EXIT);
623*91f16700Schasinglulu 
624*91f16700Schasinglulu 		/* Restore the PHY_RX_CAL_DQS value */
625*91f16700Schasinglulu 		for (byte = 0; byte < 4; byte++)
626*91f16700Schasinglulu 			mmio_clrsetbits_32(PHY_REG(0, 57 + 128 * byte),
627*91f16700Schasinglulu 					   0xfff << 16,
628*91f16700Schasinglulu 					   sdram_params->rx_cal_dqs[0][byte]);
629*91f16700Schasinglulu 	}
630*91f16700Schasinglulu 	if (channel_mask & (1 << 1)) {
631*91f16700Schasinglulu 		count = 0;
632*91f16700Schasinglulu 		while (!(mmio_read_32(CTL_REG(1, 203)) & (1 << 3))) {
633*91f16700Schasinglulu 			/* CKE is low, loop 10ms */
634*91f16700Schasinglulu 			if (count > 100)
635*91f16700Schasinglulu 				return -1;
636*91f16700Schasinglulu 
637*91f16700Schasinglulu 			sram_udelay(100);
638*91f16700Schasinglulu 			count++;
639*91f16700Schasinglulu 		}
640*91f16700Schasinglulu 
641*91f16700Schasinglulu 		mmio_clrbits_32(CTL_REG(1, 68), PWRUP_SREFRESH_EXIT);
642*91f16700Schasinglulu 
643*91f16700Schasinglulu 		/* Restore the PHY_RX_CAL_DQS value */
644*91f16700Schasinglulu 		for (byte = 0; byte < 4; byte++)
645*91f16700Schasinglulu 			mmio_clrsetbits_32(PHY_REG(1, 57 + 128 * byte),
646*91f16700Schasinglulu 					   0xfff << 16,
647*91f16700Schasinglulu 					   sdram_params->rx_cal_dqs[1][byte]);
648*91f16700Schasinglulu 	}
649*91f16700Schasinglulu 
650*91f16700Schasinglulu 	return 0;
651*91f16700Schasinglulu }
652*91f16700Schasinglulu 
653*91f16700Schasinglulu __pmusramfunc static void pmusram_restore_pll(int pll_id, uint32_t *src)
654*91f16700Schasinglulu {
655*91f16700Schasinglulu 	mmio_write_32((CRU_BASE + CRU_PLL_CON(pll_id, 3)), PLL_SLOW_MODE);
656*91f16700Schasinglulu 
657*91f16700Schasinglulu 	mmio_write_32(CRU_BASE + CRU_PLL_CON(pll_id, 0), src[0] | REG_SOC_WMSK);
658*91f16700Schasinglulu 	mmio_write_32(CRU_BASE + CRU_PLL_CON(pll_id, 1), src[1] | REG_SOC_WMSK);
659*91f16700Schasinglulu 	mmio_write_32(CRU_BASE + CRU_PLL_CON(pll_id, 2), src[2]);
660*91f16700Schasinglulu 	mmio_write_32(CRU_BASE + CRU_PLL_CON(pll_id, 4), src[4] | REG_SOC_WMSK);
661*91f16700Schasinglulu 	mmio_write_32(CRU_BASE + CRU_PLL_CON(pll_id, 5), src[5] | REG_SOC_WMSK);
662*91f16700Schasinglulu 
663*91f16700Schasinglulu 	mmio_write_32(CRU_BASE + CRU_PLL_CON(pll_id, 3), src[3] | REG_SOC_WMSK);
664*91f16700Schasinglulu 
665*91f16700Schasinglulu 	while ((mmio_read_32(CRU_BASE + CRU_PLL_CON(pll_id, 2)) &
666*91f16700Schasinglulu 		(1U << 31)) == 0x0)
667*91f16700Schasinglulu 		;
668*91f16700Schasinglulu }
669*91f16700Schasinglulu 
670*91f16700Schasinglulu __pmusramfunc static void pmusram_enable_watchdog(void)
671*91f16700Schasinglulu {
672*91f16700Schasinglulu 	/* Make the watchdog use the first global reset. */
673*91f16700Schasinglulu 	mmio_write_32(CRU_BASE + CRU_GLB_RST_CON, 1 << 1);
674*91f16700Schasinglulu 
675*91f16700Schasinglulu 	/*
676*91f16700Schasinglulu 	 * This gives the system ~8 seconds before reset. The pclk for the
677*91f16700Schasinglulu 	 * watchdog is 4MHz on reset. The value of 0x9 in WDT_TORR means that
678*91f16700Schasinglulu 	 * the watchdog will wait for 0x1ffffff cycles before resetting.
679*91f16700Schasinglulu 	 */
680*91f16700Schasinglulu 	mmio_write_32(WDT0_BASE + 4, 0x9);
681*91f16700Schasinglulu 
682*91f16700Schasinglulu 	/* Enable the watchdog */
683*91f16700Schasinglulu 	mmio_setbits_32(WDT0_BASE, 0x1);
684*91f16700Schasinglulu 
685*91f16700Schasinglulu 	/* Magic reset the watchdog timer value for WDT_CRR. */
686*91f16700Schasinglulu 	mmio_write_32(WDT0_BASE + 0xc, 0x76);
687*91f16700Schasinglulu 
688*91f16700Schasinglulu 	secure_watchdog_ungate();
689*91f16700Schasinglulu 
690*91f16700Schasinglulu 	/* The watchdog is in PD_ALIVE, so deidle it. */
691*91f16700Schasinglulu 	mmio_clrbits_32(PMU_BASE + PMU_BUS_CLR, PMU_CLR_ALIVE);
692*91f16700Schasinglulu }
693*91f16700Schasinglulu 
694*91f16700Schasinglulu void dmc_suspend(void)
695*91f16700Schasinglulu {
696*91f16700Schasinglulu 	struct rk3399_sdram_params *sdram_params = &sdram_config;
697*91f16700Schasinglulu 	struct rk3399_ddr_publ_regs *phy_regs;
698*91f16700Schasinglulu 	uint32_t *params_ctl;
699*91f16700Schasinglulu 	uint32_t *params_pi;
700*91f16700Schasinglulu 	uint32_t refdiv, postdiv2, postdiv1, fbdiv;
701*91f16700Schasinglulu 	uint32_t ch, byte, i;
702*91f16700Schasinglulu 
703*91f16700Schasinglulu 	phy_regs = &sdram_params->phy_regs;
704*91f16700Schasinglulu 	params_ctl = sdram_params->pctl_regs.denali_ctl;
705*91f16700Schasinglulu 	params_pi = sdram_params->pi_regs.denali_pi;
706*91f16700Schasinglulu 
707*91f16700Schasinglulu 	/* save dpll register and ddr clock register value to pmusram */
708*91f16700Schasinglulu 	cru_clksel_con6 = mmio_read_32(CRU_BASE + CRU_CLKSEL_CON6);
709*91f16700Schasinglulu 	for (i = 0; i < PLL_CON_COUNT; i++)
710*91f16700Schasinglulu 		dpll_data[i] = mmio_read_32(CRU_BASE + CRU_PLL_CON(DPLL_ID, i));
711*91f16700Schasinglulu 
712*91f16700Schasinglulu 	fbdiv = dpll_data[0] & 0xfff;
713*91f16700Schasinglulu 	postdiv2 = POSTDIV2_DEC(dpll_data[1]);
714*91f16700Schasinglulu 	postdiv1 = POSTDIV1_DEC(dpll_data[1]);
715*91f16700Schasinglulu 	refdiv = REFDIV_DEC(dpll_data[1]);
716*91f16700Schasinglulu 
717*91f16700Schasinglulu 	sdram_params->ddr_freq = ((fbdiv * 24) /
718*91f16700Schasinglulu 				(refdiv * postdiv1 * postdiv2)) * MHz;
719*91f16700Schasinglulu 
720*91f16700Schasinglulu 	INFO("sdram_params->ddr_freq = %d\n", sdram_params->ddr_freq);
721*91f16700Schasinglulu 	sdram_params->odt = (((mmio_read_32(PHY_REG(0, 5)) >> 16) &
722*91f16700Schasinglulu 			       0x7) != 0) ? 1 : 0;
723*91f16700Schasinglulu 
724*91f16700Schasinglulu 	/* copy the registers CTL PI and PHY */
725*91f16700Schasinglulu 	dram_regcpy((uintptr_t)&params_ctl[0], CTL_REG(0, 0), CTL_REG_NUM);
726*91f16700Schasinglulu 
727*91f16700Schasinglulu 	/* mask DENALI_CTL_00_DATA.START, only copy here, will trigger later */
728*91f16700Schasinglulu 	params_ctl[0] &= ~(0x1 << 0);
729*91f16700Schasinglulu 
730*91f16700Schasinglulu 	dram_regcpy((uintptr_t)&params_pi[0], PI_REG(0, 0),
731*91f16700Schasinglulu 		    PI_REG_NUM);
732*91f16700Schasinglulu 
733*91f16700Schasinglulu 	/* mask DENALI_PI_00_DATA.START, only copy here, will trigger later*/
734*91f16700Schasinglulu 	params_pi[0] &= ~(0x1 << 0);
735*91f16700Schasinglulu 
736*91f16700Schasinglulu 	dram_regcpy((uintptr_t)&phy_regs->phy0[0],
737*91f16700Schasinglulu 			    PHY_REG(0, 0), 91);
738*91f16700Schasinglulu 
739*91f16700Schasinglulu 	for (i = 0; i < 3; i++)
740*91f16700Schasinglulu 		dram_regcpy((uintptr_t)&phy_regs->phy512[i][0],
741*91f16700Schasinglulu 			    PHY_REG(0, 512 + 128 * i), 38);
742*91f16700Schasinglulu 
743*91f16700Schasinglulu 	dram_regcpy((uintptr_t)&phy_regs->phy896[0], PHY_REG(0, 896), 63);
744*91f16700Schasinglulu 
745*91f16700Schasinglulu 	for (ch = 0; ch < sdram_params->num_channels; ch++) {
746*91f16700Schasinglulu 		for (byte = 0; byte < 4; byte++)
747*91f16700Schasinglulu 			sdram_params->rx_cal_dqs[ch][byte] = (0xfff << 16) &
748*91f16700Schasinglulu 				mmio_read_32(PHY_REG(ch, 57 + byte * 128));
749*91f16700Schasinglulu 	}
750*91f16700Schasinglulu 
751*91f16700Schasinglulu 	/* set DENALI_PHY_957_DATA.PHY_DLL_RST_EN = 0x1 */
752*91f16700Schasinglulu 	phy_regs->phy896[957 - 896] &= ~(0x3 << 24);
753*91f16700Schasinglulu 	phy_regs->phy896[957 - 896] |= 1 << 24;
754*91f16700Schasinglulu 	phy_regs->phy896[0] |= 1;
755*91f16700Schasinglulu 	phy_regs->phy896[0] &= ~(0x3 << 8);
756*91f16700Schasinglulu }
757*91f16700Schasinglulu 
758*91f16700Schasinglulu __pmusramfunc void phy_dll_bypass_set(uint32_t ch, uint32_t freq)
759*91f16700Schasinglulu {
760*91f16700Schasinglulu 	if (freq <= (125 * 1000 * 1000)) {
761*91f16700Schasinglulu 		/* Set master mode to SW for slices*/
762*91f16700Schasinglulu 		mmio_setbits_32(PHY_REG(ch, 86), 3 << 10);
763*91f16700Schasinglulu 		mmio_setbits_32(PHY_REG(ch, 214), 3 << 10);
764*91f16700Schasinglulu 		mmio_setbits_32(PHY_REG(ch, 342), 3 << 10);
765*91f16700Schasinglulu 		mmio_setbits_32(PHY_REG(ch, 470), 3 << 10);
766*91f16700Schasinglulu 		/* Set master mode to SW for address slices*/
767*91f16700Schasinglulu 		mmio_setbits_32(PHY_REG(ch, 547), 3 << 18);
768*91f16700Schasinglulu 		mmio_setbits_32(PHY_REG(ch, 675), 3 << 18);
769*91f16700Schasinglulu 		mmio_setbits_32(PHY_REG(ch, 803), 3 << 18);
770*91f16700Schasinglulu 	} else {
771*91f16700Schasinglulu 		/* Clear SW master mode for slices*/
772*91f16700Schasinglulu 		mmio_clrbits_32(PHY_REG(ch, 86), 3 << 10);
773*91f16700Schasinglulu 		mmio_clrbits_32(PHY_REG(ch, 214), 3 << 10);
774*91f16700Schasinglulu 		mmio_clrbits_32(PHY_REG(ch, 342), 3 << 10);
775*91f16700Schasinglulu 		mmio_clrbits_32(PHY_REG(ch, 470), 3 << 10);
776*91f16700Schasinglulu 		/* Clear SW master mode for address slices*/
777*91f16700Schasinglulu 		mmio_clrbits_32(PHY_REG(ch, 547), 3 << 18);
778*91f16700Schasinglulu 		mmio_clrbits_32(PHY_REG(ch, 675), 3 << 18);
779*91f16700Schasinglulu 		mmio_clrbits_32(PHY_REG(ch, 803), 3 << 18);
780*91f16700Schasinglulu 	}
781*91f16700Schasinglulu }
782*91f16700Schasinglulu 
783*91f16700Schasinglulu __pmusramfunc void dmc_resume(void)
784*91f16700Schasinglulu {
785*91f16700Schasinglulu 	struct rk3399_sdram_params *sdram_params = &sdram_config;
786*91f16700Schasinglulu 	uint32_t channel_mask = 0;
787*91f16700Schasinglulu 	uint32_t channel;
788*91f16700Schasinglulu 
789*91f16700Schasinglulu 	/*
790*91f16700Schasinglulu 	 * We can't turn off the watchdog, so if we have not turned it on before
791*91f16700Schasinglulu 	 * we should not turn it on here.
792*91f16700Schasinglulu 	 */
793*91f16700Schasinglulu 	if ((pmu_enable_watchdog0 & 0x1) == 0x1) {
794*91f16700Schasinglulu 		pmusram_enable_watchdog();
795*91f16700Schasinglulu 	}
796*91f16700Schasinglulu 	pmu_sgrf_rst_hld_release();
797*91f16700Schasinglulu 	restore_pmu_rsthold();
798*91f16700Schasinglulu 	sram_secure_timer_init();
799*91f16700Schasinglulu 
800*91f16700Schasinglulu 	/*
801*91f16700Schasinglulu 	 * we switch ddr clock to abpll when suspend,
802*91f16700Schasinglulu 	 * we set back to dpll here
803*91f16700Schasinglulu 	 */
804*91f16700Schasinglulu 	mmio_write_32(CRU_BASE + CRU_CLKSEL_CON6,
805*91f16700Schasinglulu 			cru_clksel_con6 | REG_SOC_WMSK);
806*91f16700Schasinglulu 	pmusram_restore_pll(DPLL_ID, dpll_data);
807*91f16700Schasinglulu 
808*91f16700Schasinglulu 	configure_sgrf();
809*91f16700Schasinglulu 
810*91f16700Schasinglulu retry:
811*91f16700Schasinglulu 	for (channel = 0; channel < sdram_params->num_channels; channel++) {
812*91f16700Schasinglulu 		phy_pctrl_reset(channel);
813*91f16700Schasinglulu 		/*
814*91f16700Schasinglulu 		 * Without this, LPDDR4 will write 0's in place of real data
815*91f16700Schasinglulu 		 * in a strange pattern.
816*91f16700Schasinglulu 		 */
817*91f16700Schasinglulu 		if (sdram_params->dramtype == LPDDR4) {
818*91f16700Schasinglulu 			phy_dll_bypass_set(channel, sdram_params->ddr_freq);
819*91f16700Schasinglulu 		}
820*91f16700Schasinglulu 		pctl_cfg(channel, sdram_params);
821*91f16700Schasinglulu 	}
822*91f16700Schasinglulu 
823*91f16700Schasinglulu 	for (channel = 0; channel < 2; channel++) {
824*91f16700Schasinglulu 		if (sdram_params->ch[channel].col)
825*91f16700Schasinglulu 			channel_mask |= 1 << channel;
826*91f16700Schasinglulu 	}
827*91f16700Schasinglulu 
828*91f16700Schasinglulu 	if (pctl_start(channel_mask, sdram_params) < 0)
829*91f16700Schasinglulu 		goto retry;
830*91f16700Schasinglulu 
831*91f16700Schasinglulu 	for (channel = 0; channel < sdram_params->num_channels; channel++) {
832*91f16700Schasinglulu 		/* LPDDR2/LPDDR3 need to wait DAI complete, max 10us */
833*91f16700Schasinglulu 		if (sdram_params->dramtype == LPDDR3)
834*91f16700Schasinglulu 			sram_udelay(10);
835*91f16700Schasinglulu 
836*91f16700Schasinglulu 		/*
837*91f16700Schasinglulu 		 * Training here will always fail for LPDDR4, so skip it
838*91f16700Schasinglulu 		 * If traning fail, retry to do it again.
839*91f16700Schasinglulu 		 */
840*91f16700Schasinglulu 		if (sdram_params->dramtype != LPDDR4 &&
841*91f16700Schasinglulu 		    data_training(channel, sdram_params, PI_FULL_TRAINING))
842*91f16700Schasinglulu 			goto retry;
843*91f16700Schasinglulu 
844*91f16700Schasinglulu 		set_ddrconfig(sdram_params, channel,
845*91f16700Schasinglulu 			      sdram_params->ch[channel].ddrconfig);
846*91f16700Schasinglulu 	}
847*91f16700Schasinglulu 
848*91f16700Schasinglulu 	dram_all_config(sdram_params);
849*91f16700Schasinglulu 
850*91f16700Schasinglulu 	/* Switch to index 1 and prepare for DDR frequency switch. */
851*91f16700Schasinglulu 	dram_switch_to_next_index(sdram_params);
852*91f16700Schasinglulu }
853