xref: /rk3399_ARM-atf/plat/rockchip/rk3399/drivers/m0/src/dram.c (revision 977001aa877f90dfbc8033f8b266b7488c442038)
1*977001aaSXing Zheng /*
2*977001aaSXing Zheng  * Copyright (c) 2016, ARM Limited and Contributors. All rights reserved.
3*977001aaSXing Zheng  *
4*977001aaSXing Zheng  * Redistribution and use in source and binary forms, with or without
5*977001aaSXing Zheng  * modification, are permitted provided that the following conditions are met:
6*977001aaSXing Zheng  *
7*977001aaSXing Zheng  * Redistributions of source code must retain the above copyright notice, this
8*977001aaSXing Zheng  * list of conditions and the following disclaimer.
9*977001aaSXing Zheng  *
10*977001aaSXing Zheng  * Redistributions in binary form must reproduce the above copyright notice,
11*977001aaSXing Zheng  * this list of conditions and the following disclaimer in the documentation
12*977001aaSXing Zheng  * and/or other materials provided with the distribution.
13*977001aaSXing Zheng  *
14*977001aaSXing Zheng  * Neither the name of ARM nor the names of its contributors may be used
15*977001aaSXing Zheng  * to endorse or promote products derived from this software without specific
16*977001aaSXing Zheng  * prior written permission.
17*977001aaSXing Zheng  *
18*977001aaSXing Zheng  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19*977001aaSXing Zheng  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20*977001aaSXing Zheng  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21*977001aaSXing Zheng  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22*977001aaSXing Zheng  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23*977001aaSXing Zheng  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24*977001aaSXing Zheng  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25*977001aaSXing Zheng  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26*977001aaSXing Zheng  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27*977001aaSXing Zheng  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28*977001aaSXing Zheng  * POSSIBILITY OF SUCH DAMAGE.
29*977001aaSXing Zheng  */
30*977001aaSXing Zheng 
31*977001aaSXing Zheng #include <m0_param.h>
32*977001aaSXing Zheng #include "rk3399_mcu.h"
33*977001aaSXing Zheng 
34*977001aaSXing Zheng /* PMU */
35*977001aaSXing Zheng #define PMU_PWRDN_ST		0x18
36*977001aaSXing Zheng #define PMU_BUS_IDLE_REQ	0x60
37*977001aaSXing Zheng #define PMU_BUS_IDLE_ST		0x64
38*977001aaSXing Zheng #define PMU_NOC_AUTO_ENA	0xd8
39*977001aaSXing Zheng 
40*977001aaSXing Zheng /* PMU_BUS_IDLE_REQ */
41*977001aaSXing Zheng #define IDLE_REQ_MSCH1		(1 << 19)
42*977001aaSXing Zheng #define IDLE_REQ_MSCH0		(1 << 18)
43*977001aaSXing Zheng 
44*977001aaSXing Zheng /* #define PMU_BUS_IDLE_ST */
45*977001aaSXing Zheng #define IDLE_MSCH1		(1 << 19)
46*977001aaSXing Zheng #define IDLE_MSCH0		(1 << 18)
47*977001aaSXing Zheng 
48*977001aaSXing Zheng #define PD_VOP_PWR_STAT		(1 << 20)
49*977001aaSXing Zheng 
50*977001aaSXing Zheng /* CRU */
51*977001aaSXing Zheng #define CRU_DPLL_CON0		0x40
52*977001aaSXing Zheng #define CRU_DPLL_CON1		0x44
53*977001aaSXing Zheng #define CRU_DPLL_CON2		0x48
54*977001aaSXing Zheng #define CRU_DPLL_CON3		0x4c
55*977001aaSXing Zheng #define CRU_DPLL_CON4		0x50
56*977001aaSXing Zheng #define CRU_DPLL_CON5		0x54
57*977001aaSXing Zheng 
58*977001aaSXing Zheng #define CRU_DPLL_CON2		0x48
59*977001aaSXing Zheng #define CRU_DPLL_CON3		0x4c
60*977001aaSXing Zheng #define CRU_CLKGATE10_CON	0x328
61*977001aaSXing Zheng #define CRU_CLKGATE28_CON	0x370
62*977001aaSXing Zheng 
63*977001aaSXing Zheng /* CRU_CLKGATE10_CON */
64*977001aaSXing Zheng #define ACLK_VOP0_PRE_SRC_EN	(1 << 8)
65*977001aaSXing Zheng #define HCLK_VOP0_PRE_EN	(1 << 9)
66*977001aaSXing Zheng #define ACLK_VOP1_PRE_SRC_EN	(1 << 10)
67*977001aaSXing Zheng #define HCLK_VOP1_PRE_EN	(1 << 11)
68*977001aaSXing Zheng #define DCLK_VOP0_SRC_EN	(1 << 12)
69*977001aaSXing Zheng #define DCLK_VOP1_SRC_EN	(1 << 13)
70*977001aaSXing Zheng 
71*977001aaSXing Zheng /* CRU_CLKGATE28_CON */
72*977001aaSXing Zheng #define HCLK_VOP0_EN		(1 << 2)
73*977001aaSXing Zheng #define ACLK_VOP0_EN		(1 << 3)
74*977001aaSXing Zheng #define HCLK_VOP1_EN		(1 << 6)
75*977001aaSXing Zheng #define ACLK_VOP1_EN		(1 << 7)
76*977001aaSXing Zheng 
77*977001aaSXing Zheng /* CRU_PLL_CON3 */
78*977001aaSXing Zheng #define PLL_SLOW_MODE		0
79*977001aaSXing Zheng #define PLL_NORMAL_MODE		1
80*977001aaSXing Zheng #define PLL_MODE(n)		((0x3 << (8 + 16)) | ((n) << 8))
81*977001aaSXing Zheng #define PLL_POWER_DOWN(n)	((0x1 << (0 + 16)) | ((n) << 0))
82*977001aaSXing Zheng 
83*977001aaSXing Zheng /* PMU CRU */
84*977001aaSXing Zheng #define PMU_CRU_GATEDIS_CON0	0x130
85*977001aaSXing Zheng 
86*977001aaSXing Zheng /* VOP */
87*977001aaSXing Zheng #define VOP_SYS_CTRL		0x8
88*977001aaSXing Zheng #define VOP_SYS_CTRL1		0xc
89*977001aaSXing Zheng #define	VOP_INTR_CLEAR0		0x284
90*977001aaSXing Zheng #define VOP_INTR_RAW_STATUS0	0x28c
91*977001aaSXing Zheng 
92*977001aaSXing Zheng /* VOP_SYS_CTRL */
93*977001aaSXing Zheng #define VOP_STANDBY_EN		(1 << 22)
94*977001aaSXing Zheng 
95*977001aaSXing Zheng /* VOP_INTR_CLEAR0 */
96*977001aaSXing Zheng #define	INT_CLR_DMA_FINISH	(1 << 15)
97*977001aaSXing Zheng #define INT_CLR_LINE_FLAG1	(1 << 4)
98*977001aaSXing Zheng #define INT_CLR_LINE_FLAG0	(1 << 3)
99*977001aaSXing Zheng 
100*977001aaSXing Zheng /* VOP_INTR_RAW_STATUS0 */
101*977001aaSXing Zheng #define	INT_RAW_STATUS_DMA_FINISH	(1 << 15)
102*977001aaSXing Zheng #define INT_RAW_STATUS_LINE_FLAG1	(1 << 4)
103*977001aaSXing Zheng #define INT_RAW_STATUS_LINE_FLAG0	(1 << 3)
104*977001aaSXing Zheng 
105*977001aaSXing Zheng /* CIC */
106*977001aaSXing Zheng #define CIC_CTRL0		0
107*977001aaSXing Zheng #define CIC_CTRL1		0x4
108*977001aaSXing Zheng #define CIC_STATUS0		0x10
109*977001aaSXing Zheng 
110*977001aaSXing Zheng struct ddr_freq_param {
111*977001aaSXing Zheng 	uint32_t vop_big_en;
112*977001aaSXing Zheng 	uint32_t vop_lit_en;
113*977001aaSXing Zheng 	uint32_t dclk0_div;
114*977001aaSXing Zheng 	uint32_t dclk1_div;
115*977001aaSXing Zheng };
116*977001aaSXing Zheng 
117*977001aaSXing Zheng static struct ddr_freq_param rk3399_ddr_arg;
118*977001aaSXing Zheng 
119*977001aaSXing Zheng static void get_vop_status(void)
120*977001aaSXing Zheng {
121*977001aaSXing Zheng 	rk3399_ddr_arg.vop_big_en = 0;
122*977001aaSXing Zheng 	rk3399_ddr_arg.vop_lit_en = 0;
123*977001aaSXing Zheng 
124*977001aaSXing Zheng 	if ((mmio_read_32(PMU_BASE + PMU_PWRDN_ST) & PD_VOP_PWR_STAT) == 0) {
125*977001aaSXing Zheng 		/* get vop0 status */
126*977001aaSXing Zheng 		if ((mmio_read_32(CRU_BASE_ADDR + CRU_CLKGATE10_CON) &
127*977001aaSXing Zheng 				  (DCLK_VOP0_SRC_EN | ACLK_VOP0_PRE_SRC_EN |
128*977001aaSXing Zheng 				   HCLK_VOP0_PRE_EN)) == 0)
129*977001aaSXing Zheng 			if ((mmio_read_32(CRU_BASE_ADDR + CRU_CLKGATE28_CON) &
130*977001aaSXing Zheng 					 (HCLK_VOP0_EN | ACLK_VOP0_EN)) == 0)
131*977001aaSXing Zheng 				if ((mmio_read_32(VOP_BIG_BASE_ADDR +
132*977001aaSXing Zheng 						  VOP_SYS_CTRL) &
133*977001aaSXing Zheng 						  VOP_STANDBY_EN) == 0)
134*977001aaSXing Zheng 					rk3399_ddr_arg.vop_big_en = 1;
135*977001aaSXing Zheng 
136*977001aaSXing Zheng 		/* get vop1 satus */
137*977001aaSXing Zheng 		if ((mmio_read_32(CRU_BASE_ADDR + CRU_CLKGATE10_CON) &
138*977001aaSXing Zheng 				  (DCLK_VOP1_SRC_EN | ACLK_VOP1_PRE_SRC_EN |
139*977001aaSXing Zheng 				   HCLK_VOP1_PRE_EN)) == 0)
140*977001aaSXing Zheng 			if ((mmio_read_32(CRU_BASE_ADDR + CRU_CLKGATE28_CON) &
141*977001aaSXing Zheng 					  (HCLK_VOP1_EN | ACLK_VOP1_EN)) == 0)
142*977001aaSXing Zheng 				if ((mmio_read_32(VOP_LITE_BASE_ADDR +
143*977001aaSXing Zheng 						  VOP_SYS_CTRL) &
144*977001aaSXing Zheng 						  VOP_STANDBY_EN) == 0)
145*977001aaSXing Zheng 					rk3399_ddr_arg.vop_lit_en = 1;
146*977001aaSXing Zheng 	}
147*977001aaSXing Zheng }
148*977001aaSXing Zheng 
149*977001aaSXing Zheng static void wait_vop_dma_finish(void)
150*977001aaSXing Zheng {
151*977001aaSXing Zheng 	uint32_t vop_adr;
152*977001aaSXing Zheng 
153*977001aaSXing Zheng 	get_vop_status();
154*977001aaSXing Zheng 
155*977001aaSXing Zheng 	if (rk3399_ddr_arg.vop_big_en)
156*977001aaSXing Zheng 		vop_adr = VOP_BIG_BASE_ADDR;
157*977001aaSXing Zheng 	else if (rk3399_ddr_arg.vop_lit_en)
158*977001aaSXing Zheng 		vop_adr = VOP_LITE_BASE_ADDR;
159*977001aaSXing Zheng 	else
160*977001aaSXing Zheng 		return;
161*977001aaSXing Zheng 
162*977001aaSXing Zheng 	/* clean dma finish irq and wait for it */
163*977001aaSXing Zheng 	mmio_write_32(vop_adr + VOP_INTR_CLEAR0,
164*977001aaSXing Zheng 		      INT_CLR_DMA_FINISH | (INT_CLR_DMA_FINISH << 16));
165*977001aaSXing Zheng 
166*977001aaSXing Zheng 	while ((mmio_read_32(vop_adr + VOP_INTR_RAW_STATUS0) &
167*977001aaSXing Zheng 		INT_RAW_STATUS_DMA_FINISH) == 0)
168*977001aaSXing Zheng 		;
169*977001aaSXing Zheng }
170*977001aaSXing Zheng 
171*977001aaSXing Zheng static void idle_port(void)
172*977001aaSXing Zheng {
173*977001aaSXing Zheng 	mmio_write_32(PMU_CRU_BASE_ADDR + PMU_CRU_GATEDIS_CON0, 0x3fffffff);
174*977001aaSXing Zheng 	mmio_setbits_32(PMU_BASE + PMU_BUS_IDLE_REQ,
175*977001aaSXing Zheng 			IDLE_REQ_MSCH0 | IDLE_REQ_MSCH1);
176*977001aaSXing Zheng 	while ((mmio_read_32(PMU_BASE + PMU_BUS_IDLE_ST) &
177*977001aaSXing Zheng 		(IDLE_MSCH1 | IDLE_MSCH0)) != (IDLE_MSCH1 | IDLE_MSCH0))
178*977001aaSXing Zheng 		continue;
179*977001aaSXing Zheng }
180*977001aaSXing Zheng 
181*977001aaSXing Zheng static void deidle_port(void)
182*977001aaSXing Zheng {
183*977001aaSXing Zheng 	mmio_clrbits_32(PMU_BASE + PMU_BUS_IDLE_REQ,
184*977001aaSXing Zheng 			IDLE_REQ_MSCH0 | IDLE_REQ_MSCH1);
185*977001aaSXing Zheng 	while (mmio_read_32(PMU_BASE + PMU_BUS_IDLE_ST) &
186*977001aaSXing Zheng 	       (IDLE_MSCH1 | IDLE_MSCH0))
187*977001aaSXing Zheng 		continue;
188*977001aaSXing Zheng }
189*977001aaSXing Zheng 
190*977001aaSXing Zheng static void ddr_set_pll(void)
191*977001aaSXing Zheng {
192*977001aaSXing Zheng 	mmio_write_32(CRU_BASE_ADDR + CRU_DPLL_CON3, PLL_MODE(PLL_SLOW_MODE));
193*977001aaSXing Zheng 
194*977001aaSXing Zheng 	mmio_write_32(CRU_BASE_ADDR + CRU_DPLL_CON3, PLL_POWER_DOWN(1));
195*977001aaSXing Zheng 	mmio_write_32(CRU_BASE_ADDR + CRU_DPLL_CON0,
196*977001aaSXing Zheng 		      mmio_read_32(PARAM_ADDR + PARAM_DPLL_CON0));
197*977001aaSXing Zheng 	mmio_write_32(CRU_BASE_ADDR + CRU_DPLL_CON1,
198*977001aaSXing Zheng 		      mmio_read_32(PARAM_ADDR + PARAM_DPLL_CON1));
199*977001aaSXing Zheng 	mmio_write_32(CRU_BASE_ADDR + CRU_DPLL_CON3, PLL_POWER_DOWN(0));
200*977001aaSXing Zheng 
201*977001aaSXing Zheng 	while ((mmio_read_32(CRU_BASE_ADDR + CRU_DPLL_CON2) & (1u << 31)) == 0)
202*977001aaSXing Zheng 		continue;
203*977001aaSXing Zheng 
204*977001aaSXing Zheng 	mmio_write_32(CRU_BASE_ADDR + CRU_DPLL_CON3, PLL_MODE(PLL_NORMAL_MODE));
205*977001aaSXing Zheng }
206*977001aaSXing Zheng 
207*977001aaSXing Zheng void handle_dram(void)
208*977001aaSXing Zheng {
209*977001aaSXing Zheng 	wait_vop_dma_finish();
210*977001aaSXing Zheng 	idle_port();
211*977001aaSXing Zheng 
212*977001aaSXing Zheng 	mmio_write_32(CIC_BASE_ADDR + CIC_CTRL0,
213*977001aaSXing Zheng 		      (((0x3 << 4) | (1 << 2) | 1) << 16) |
214*977001aaSXing Zheng 		      (1 << 2) | 1 |
215*977001aaSXing Zheng 		      mmio_read_32(PARAM_ADDR + PARAM_FREQ_SELECT));
216*977001aaSXing Zheng 	while ((mmio_read_32(CIC_BASE_ADDR + CIC_STATUS0) & (1 << 2)) == 0)
217*977001aaSXing Zheng 		continue;
218*977001aaSXing Zheng 
219*977001aaSXing Zheng 	ddr_set_pll();
220*977001aaSXing Zheng 	mmio_write_32(CIC_BASE_ADDR + CIC_CTRL0, 0x20002);
221*977001aaSXing Zheng 	while ((mmio_read_32(CIC_BASE_ADDR + CIC_STATUS0) & (1 << 0)) == 0)
222*977001aaSXing Zheng 		continue;
223*977001aaSXing Zheng 
224*977001aaSXing Zheng 	deidle_port();
225*977001aaSXing Zheng }
226