xref: /rk3399_ARM-atf/plat/rockchip/rk3399/drivers/m0/src/dram.c (revision a82ec8145961e57d19cdb71ad9823fd99f7f7c53)
1977001aaSXing Zheng /*
2977001aaSXing Zheng  * Copyright (c) 2016, ARM Limited and Contributors. All rights reserved.
3977001aaSXing Zheng  *
4977001aaSXing Zheng  * Redistribution and use in source and binary forms, with or without
5977001aaSXing Zheng  * modification, are permitted provided that the following conditions are met:
6977001aaSXing Zheng  *
7977001aaSXing Zheng  * Redistributions of source code must retain the above copyright notice, this
8977001aaSXing Zheng  * list of conditions and the following disclaimer.
9977001aaSXing Zheng  *
10977001aaSXing Zheng  * Redistributions in binary form must reproduce the above copyright notice,
11977001aaSXing Zheng  * this list of conditions and the following disclaimer in the documentation
12977001aaSXing Zheng  * and/or other materials provided with the distribution.
13977001aaSXing Zheng  *
14977001aaSXing Zheng  * Neither the name of ARM nor the names of its contributors may be used
15977001aaSXing Zheng  * to endorse or promote products derived from this software without specific
16977001aaSXing Zheng  * prior written permission.
17977001aaSXing Zheng  *
18977001aaSXing Zheng  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19977001aaSXing Zheng  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20977001aaSXing Zheng  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21977001aaSXing Zheng  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22977001aaSXing Zheng  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23977001aaSXing Zheng  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24977001aaSXing Zheng  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25977001aaSXing Zheng  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26977001aaSXing Zheng  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27977001aaSXing Zheng  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28977001aaSXing Zheng  * POSSIBILITY OF SUCH DAMAGE.
29977001aaSXing Zheng  */
30977001aaSXing Zheng 
31977001aaSXing Zheng #include <m0_param.h>
32977001aaSXing Zheng #include "rk3399_mcu.h"
33977001aaSXing Zheng 
34977001aaSXing Zheng /* PMU */
35977001aaSXing Zheng #define PMU_PWRDN_ST		0x18
36977001aaSXing Zheng #define PMU_BUS_IDLE_REQ	0x60
37977001aaSXing Zheng #define PMU_BUS_IDLE_ST		0x64
38977001aaSXing Zheng #define PMU_NOC_AUTO_ENA	0xd8
39977001aaSXing Zheng 
40977001aaSXing Zheng /* PMU_BUS_IDLE_REQ */
41977001aaSXing Zheng #define IDLE_REQ_MSCH1		(1 << 19)
42977001aaSXing Zheng #define IDLE_REQ_MSCH0		(1 << 18)
43977001aaSXing Zheng 
44977001aaSXing Zheng /* #define PMU_BUS_IDLE_ST */
45977001aaSXing Zheng #define IDLE_MSCH1		(1 << 19)
46977001aaSXing Zheng #define IDLE_MSCH0		(1 << 18)
47977001aaSXing Zheng 
48977001aaSXing Zheng #define PD_VOP_PWR_STAT		(1 << 20)
49977001aaSXing Zheng 
50977001aaSXing Zheng /* CRU */
51977001aaSXing Zheng #define CRU_DPLL_CON0		0x40
52977001aaSXing Zheng #define CRU_DPLL_CON1		0x44
53977001aaSXing Zheng #define CRU_DPLL_CON2		0x48
54977001aaSXing Zheng #define CRU_DPLL_CON3		0x4c
55977001aaSXing Zheng #define CRU_DPLL_CON4		0x50
56977001aaSXing Zheng #define CRU_DPLL_CON5		0x54
57977001aaSXing Zheng 
58977001aaSXing Zheng #define CRU_DPLL_CON2		0x48
59977001aaSXing Zheng #define CRU_DPLL_CON3		0x4c
60977001aaSXing Zheng #define CRU_CLKGATE10_CON	0x328
61977001aaSXing Zheng #define CRU_CLKGATE28_CON	0x370
62977001aaSXing Zheng 
63977001aaSXing Zheng /* CRU_CLKGATE10_CON */
64977001aaSXing Zheng #define ACLK_VOP0_PRE_SRC_EN	(1 << 8)
65977001aaSXing Zheng #define HCLK_VOP0_PRE_EN	(1 << 9)
66977001aaSXing Zheng #define ACLK_VOP1_PRE_SRC_EN	(1 << 10)
67977001aaSXing Zheng #define HCLK_VOP1_PRE_EN	(1 << 11)
68977001aaSXing Zheng #define DCLK_VOP0_SRC_EN	(1 << 12)
69977001aaSXing Zheng #define DCLK_VOP1_SRC_EN	(1 << 13)
70977001aaSXing Zheng 
71977001aaSXing Zheng /* CRU_CLKGATE28_CON */
72977001aaSXing Zheng #define HCLK_VOP0_EN		(1 << 2)
73977001aaSXing Zheng #define ACLK_VOP0_EN		(1 << 3)
74977001aaSXing Zheng #define HCLK_VOP1_EN		(1 << 6)
75977001aaSXing Zheng #define ACLK_VOP1_EN		(1 << 7)
76977001aaSXing Zheng 
77977001aaSXing Zheng /* CRU_PLL_CON3 */
78977001aaSXing Zheng #define PLL_SLOW_MODE		0
79977001aaSXing Zheng #define PLL_NORMAL_MODE		1
80977001aaSXing Zheng #define PLL_MODE(n)		((0x3 << (8 + 16)) | ((n) << 8))
81977001aaSXing Zheng #define PLL_POWER_DOWN(n)	((0x1 << (0 + 16)) | ((n) << 0))
82977001aaSXing Zheng 
83977001aaSXing Zheng /* PMU CRU */
84977001aaSXing Zheng #define PMU_CRU_GATEDIS_CON0	0x130
85977001aaSXing Zheng 
86977001aaSXing Zheng /* VOP */
87977001aaSXing Zheng #define VOP_SYS_CTRL		0x8
88977001aaSXing Zheng #define VOP_SYS_CTRL1		0xc
89*a82ec814SLin Huang #define VOP_WIN0_CTRL0		0x30
90977001aaSXing Zheng #define	VOP_INTR_CLEAR0		0x284
91977001aaSXing Zheng #define VOP_INTR_RAW_STATUS0	0x28c
92977001aaSXing Zheng 
93977001aaSXing Zheng /* VOP_SYS_CTRL */
94*a82ec814SLin Huang #define VOP_DMA_STOP_EN		(1 << 21)
95977001aaSXing Zheng #define VOP_STANDBY_EN		(1 << 22)
96977001aaSXing Zheng 
97*a82ec814SLin Huang /* VOP_WIN0_CTRL0 */
98*a82ec814SLin Huang #define WB_ENABLE		(1 << 0)
99*a82ec814SLin Huang 
100977001aaSXing Zheng /* VOP_INTR_CLEAR0 */
101977001aaSXing Zheng #define	INT_CLR_DMA_FINISH	(1 << 15)
102977001aaSXing Zheng #define INT_CLR_LINE_FLAG1	(1 << 4)
103977001aaSXing Zheng #define INT_CLR_LINE_FLAG0	(1 << 3)
104977001aaSXing Zheng 
105977001aaSXing Zheng /* VOP_INTR_RAW_STATUS0 */
106977001aaSXing Zheng #define	INT_RAW_STATUS_DMA_FINISH	(1 << 15)
107977001aaSXing Zheng #define INT_RAW_STATUS_LINE_FLAG1	(1 << 4)
108977001aaSXing Zheng #define INT_RAW_STATUS_LINE_FLAG0	(1 << 3)
109977001aaSXing Zheng 
110977001aaSXing Zheng /* CIC */
111977001aaSXing Zheng #define CIC_CTRL0		0
112977001aaSXing Zheng #define CIC_CTRL1		0x4
113977001aaSXing Zheng #define CIC_STATUS0		0x10
114977001aaSXing Zheng 
115*a82ec814SLin Huang static inline int check_dma_status(uint32_t vop_addr, uint32_t *clr_dma_flag)
116977001aaSXing Zheng {
117*a82ec814SLin Huang 	if (*clr_dma_flag) {
118*a82ec814SLin Huang 		mmio_write_32(vop_addr + VOP_INTR_CLEAR0, 0x80008000);
119*a82ec814SLin Huang 		*clr_dma_flag = 0;
120977001aaSXing Zheng 	}
121977001aaSXing Zheng 
122*a82ec814SLin Huang 	if ((mmio_read_32(vop_addr + VOP_SYS_CTRL) &
123*a82ec814SLin Huang 	     (VOP_STANDBY_EN | VOP_DMA_STOP_EN)) ||
124*a82ec814SLin Huang 	    !(mmio_read_32(vop_addr + VOP_WIN0_CTRL0) & WB_ENABLE) ||
125*a82ec814SLin Huang 	    (mmio_read_32(vop_addr + VOP_INTR_RAW_STATUS0) &
126*a82ec814SLin Huang 	    INT_RAW_STATUS_DMA_FINISH))
127*a82ec814SLin Huang 		return 1;
128*a82ec814SLin Huang 
129*a82ec814SLin Huang 	return 0;
130*a82ec814SLin Huang }
131*a82ec814SLin Huang 
132*a82ec814SLin Huang static int wait_vop_dma_finish(void)
133977001aaSXing Zheng {
134*a82ec814SLin Huang 	uint32_t clr_dma_flag = 1;
135*a82ec814SLin Huang 	uint32_t ret = 0;
136977001aaSXing Zheng 
137*a82ec814SLin Huang 	stopwatch_init_usecs_expire(60000);
138*a82ec814SLin Huang 	while (((mmio_read_32(PMU_BASE + PMU_PWRDN_ST) &
139*a82ec814SLin Huang 		PD_VOP_PWR_STAT) == 0)) {
140*a82ec814SLin Huang 		/*
141*a82ec814SLin Huang 		 * VOPL case:
142*a82ec814SLin Huang 		 * CRU_CLKGATE10_CON(bit10): ACLK_VOP1_PRE_SRC_EN
143*a82ec814SLin Huang 		 * CRU_CLKGATE10_CON(bit11): HCLK_VOP1_PRE_EN
144*a82ec814SLin Huang 		 * CRU_CLKGATE10_CON(bit13): DCLK_VOP1_SRC_EN
145*a82ec814SLin Huang 		 * CRU_CLKGATE28_CON(bit7): ACLK_VOP1_EN
146*a82ec814SLin Huang 		 * CRU_CLKGATE28_CON(bit6): HCLK_VOP1_EN
147*a82ec814SLin Huang 		 *
148*a82ec814SLin Huang 		 * VOPB case:
149*a82ec814SLin Huang 		 * CRU_CLKGATE10_CON(bit8): ACLK_VOP0_PRE_SRC_EN
150*a82ec814SLin Huang 		 * CRU_CLKGATE10_CON(bit9): HCLK_VOP0_PRE_EN
151*a82ec814SLin Huang 		 * CRU_CLKGATE10_CON(bit12): DCLK_VOP0_SRC_EN
152*a82ec814SLin Huang 		 * CRU_CLKGATE28_CON(bit3): ACLK_VOP0_EN
153*a82ec814SLin Huang 		 * CRU_CLKGATE28_CON(bit2): HCLK_VOP0_EN
154*a82ec814SLin Huang 		 */
155*a82ec814SLin Huang 		if (((mmio_read_32(CRU_BASE_ADDR + CRU_CLKGATE10_CON) &
156*a82ec814SLin Huang 		      0x2c00) == 0) &&
157*a82ec814SLin Huang 		    ((mmio_read_32(CRU_BASE_ADDR + CRU_CLKGATE28_CON) &
158*a82ec814SLin Huang 		      0xc0) == 0)) {
159*a82ec814SLin Huang 			if (check_dma_status(VOP_LITE_BASE_ADDR, &clr_dma_flag))
160977001aaSXing Zheng 				return;
161*a82ec814SLin Huang 		} else if (((mmio_read_32(CRU_BASE_ADDR + CRU_CLKGATE10_CON) &
162*a82ec814SLin Huang 			     0x1300) == 0) &&
163*a82ec814SLin Huang 			   ((mmio_read_32(CRU_BASE_ADDR + CRU_CLKGATE28_CON) &
164*a82ec814SLin Huang 			     0x0c) == 0)) {
165*a82ec814SLin Huang 			if (check_dma_status(VOP_BIG_BASE_ADDR, &clr_dma_flag))
166*a82ec814SLin Huang 				return;
167*a82ec814SLin Huang 		} else {
168*a82ec814SLin Huang 			/* No VOPs are enabled, so don't wait. */
169*a82ec814SLin Huang 			return;
170*a82ec814SLin Huang 		}
171977001aaSXing Zheng 
172*a82ec814SLin Huang 		if (stopwatch_expired()) {
173*a82ec814SLin Huang 			ret = 1;
174*a82ec814SLin Huang 			goto out;
175*a82ec814SLin Huang 		}
176*a82ec814SLin Huang 	}
177977001aaSXing Zheng 
178*a82ec814SLin Huang out:
179*a82ec814SLin Huang 	stopwatch_reset();
180*a82ec814SLin Huang 	return ret;
181977001aaSXing Zheng }
182977001aaSXing Zheng 
183977001aaSXing Zheng static void idle_port(void)
184977001aaSXing Zheng {
185977001aaSXing Zheng 	mmio_write_32(PMU_CRU_BASE_ADDR + PMU_CRU_GATEDIS_CON0, 0x3fffffff);
186977001aaSXing Zheng 	mmio_setbits_32(PMU_BASE + PMU_BUS_IDLE_REQ,
187977001aaSXing Zheng 			IDLE_REQ_MSCH0 | IDLE_REQ_MSCH1);
188977001aaSXing Zheng 	while ((mmio_read_32(PMU_BASE + PMU_BUS_IDLE_ST) &
189977001aaSXing Zheng 		(IDLE_MSCH1 | IDLE_MSCH0)) != (IDLE_MSCH1 | IDLE_MSCH0))
190977001aaSXing Zheng 		continue;
191977001aaSXing Zheng }
192977001aaSXing Zheng 
193977001aaSXing Zheng static void deidle_port(void)
194977001aaSXing Zheng {
195977001aaSXing Zheng 	mmio_clrbits_32(PMU_BASE + PMU_BUS_IDLE_REQ,
196977001aaSXing Zheng 			IDLE_REQ_MSCH0 | IDLE_REQ_MSCH1);
197977001aaSXing Zheng 	while (mmio_read_32(PMU_BASE + PMU_BUS_IDLE_ST) &
198977001aaSXing Zheng 	       (IDLE_MSCH1 | IDLE_MSCH0))
199977001aaSXing Zheng 		continue;
200977001aaSXing Zheng }
201977001aaSXing Zheng 
202977001aaSXing Zheng static void ddr_set_pll(void)
203977001aaSXing Zheng {
204977001aaSXing Zheng 	mmio_write_32(CRU_BASE_ADDR + CRU_DPLL_CON3, PLL_MODE(PLL_SLOW_MODE));
205977001aaSXing Zheng 
206977001aaSXing Zheng 	mmio_write_32(CRU_BASE_ADDR + CRU_DPLL_CON3, PLL_POWER_DOWN(1));
207977001aaSXing Zheng 	mmio_write_32(CRU_BASE_ADDR + CRU_DPLL_CON0,
208977001aaSXing Zheng 		      mmio_read_32(PARAM_ADDR + PARAM_DPLL_CON0));
209977001aaSXing Zheng 	mmio_write_32(CRU_BASE_ADDR + CRU_DPLL_CON1,
210977001aaSXing Zheng 		      mmio_read_32(PARAM_ADDR + PARAM_DPLL_CON1));
211977001aaSXing Zheng 	mmio_write_32(CRU_BASE_ADDR + CRU_DPLL_CON3, PLL_POWER_DOWN(0));
212977001aaSXing Zheng 
213977001aaSXing Zheng 	while ((mmio_read_32(CRU_BASE_ADDR + CRU_DPLL_CON2) & (1u << 31)) == 0)
214977001aaSXing Zheng 		continue;
215977001aaSXing Zheng 
216977001aaSXing Zheng 	mmio_write_32(CRU_BASE_ADDR + CRU_DPLL_CON3, PLL_MODE(PLL_NORMAL_MODE));
217977001aaSXing Zheng }
218977001aaSXing Zheng 
219977001aaSXing Zheng void handle_dram(void)
220977001aaSXing Zheng {
221977001aaSXing Zheng 	wait_vop_dma_finish();
222*a82ec814SLin Huang 
223977001aaSXing Zheng 	idle_port();
224977001aaSXing Zheng 
225977001aaSXing Zheng 	mmio_write_32(CIC_BASE_ADDR + CIC_CTRL0,
226977001aaSXing Zheng 		      (((0x3 << 4) | (1 << 2) | 1) << 16) |
227977001aaSXing Zheng 		      (1 << 2) | 1 |
228977001aaSXing Zheng 		      mmio_read_32(PARAM_ADDR + PARAM_FREQ_SELECT));
229977001aaSXing Zheng 	while ((mmio_read_32(CIC_BASE_ADDR + CIC_STATUS0) & (1 << 2)) == 0)
230977001aaSXing Zheng 		continue;
231977001aaSXing Zheng 
232977001aaSXing Zheng 	ddr_set_pll();
233977001aaSXing Zheng 	mmio_write_32(CIC_BASE_ADDR + CIC_CTRL0, 0x20002);
234977001aaSXing Zheng 	while ((mmio_read_32(CIC_BASE_ADDR + CIC_STATUS0) & (1 << 0)) == 0)
235977001aaSXing Zheng 		continue;
236977001aaSXing Zheng 
237977001aaSXing Zheng 	deidle_port();
238977001aaSXing Zheng }
239