xref: /rk3399_ARM-atf/plat/rockchip/rk3399/drivers/m0/src/dram.c (revision 50bde47fe3f861a707d48cb36e556369a03f36f6)
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 
311830f790SXing Zheng #include <dram_regs.h>
32977001aaSXing Zheng #include <m0_param.h>
331830f790SXing Zheng #include <pmu_bits.h>
341830f790SXing Zheng #include <pmu_regs.h>
351830f790SXing Zheng #include "misc_regs.h"
36977001aaSXing Zheng #include "rk3399_mcu.h"
37977001aaSXing Zheng 
381830f790SXing Zheng static uint32_t gatedis_con0;
39ef0a6bfcSLin Huang 
40977001aaSXing Zheng static void idle_port(void)
41977001aaSXing Zheng {
421830f790SXing Zheng 	gatedis_con0 = mmio_read_32(PMUCRU_BASE + PMU_CRU_GATEDIS_CON0);
431830f790SXing Zheng 	mmio_write_32(PMUCRU_BASE + PMU_CRU_GATEDIS_CON0, 0x3fffffff);
441830f790SXing Zheng 
45977001aaSXing Zheng 	mmio_setbits_32(PMU_BASE + PMU_BUS_IDLE_REQ,
461830f790SXing Zheng 			(1 << PMU_IDLE_REQ_MSCH0) | (1 << PMU_IDLE_REQ_MSCH1));
47977001aaSXing Zheng 	while ((mmio_read_32(PMU_BASE + PMU_BUS_IDLE_ST) &
481830f790SXing Zheng 		((1 << PMU_IDLE_ST_MSCH1) | (1 << PMU_IDLE_ST_MSCH0))) !=
491830f790SXing Zheng 		((1 << PMU_IDLE_ST_MSCH1) | (1 << PMU_IDLE_ST_MSCH0)))
50977001aaSXing Zheng 		continue;
51977001aaSXing Zheng }
52977001aaSXing Zheng 
53977001aaSXing Zheng static void deidle_port(void)
54977001aaSXing Zheng {
55977001aaSXing Zheng 	mmio_clrbits_32(PMU_BASE + PMU_BUS_IDLE_REQ,
561830f790SXing Zheng 			(1 << PMU_IDLE_REQ_MSCH0) | (1 << PMU_IDLE_REQ_MSCH1));
57977001aaSXing Zheng 	while (mmio_read_32(PMU_BASE + PMU_BUS_IDLE_ST) &
581830f790SXing Zheng 	       ((1 << PMU_IDLE_ST_MSCH1) | (1 << PMU_IDLE_ST_MSCH0)))
59977001aaSXing Zheng 		continue;
60ef0a6bfcSLin Huang 
61ef0a6bfcSLin Huang 	/* document is wrong, PMU_CRU_GATEDIS_CON0 do not need set MASK BIT */
621830f790SXing Zheng 	mmio_write_32(PMUCRU_BASE + PMU_CRU_GATEDIS_CON0, gatedis_con0);
63977001aaSXing Zheng }
64977001aaSXing Zheng 
65977001aaSXing Zheng static void ddr_set_pll(void)
66977001aaSXing Zheng {
671830f790SXing Zheng 	mmio_write_32(CRU_BASE + CRU_DPLL_CON3, PLL_MODE(PLL_SLOW_MODE));
68977001aaSXing Zheng 
691830f790SXing Zheng 	mmio_write_32(CRU_BASE + CRU_DPLL_CON3, PLL_POWER_DOWN(1));
701830f790SXing Zheng 	mmio_write_32(CRU_BASE + CRU_DPLL_CON0,
71977001aaSXing Zheng 		      mmio_read_32(PARAM_ADDR + PARAM_DPLL_CON0));
721830f790SXing Zheng 	mmio_write_32(CRU_BASE + CRU_DPLL_CON1,
73977001aaSXing Zheng 		      mmio_read_32(PARAM_ADDR + PARAM_DPLL_CON1));
741830f790SXing Zheng 	mmio_write_32(CRU_BASE + CRU_DPLL_CON3, PLL_POWER_DOWN(0));
75977001aaSXing Zheng 
761830f790SXing Zheng 	while ((mmio_read_32(CRU_BASE + CRU_DPLL_CON2) & (1u << 31)) == 0)
77977001aaSXing Zheng 		continue;
78977001aaSXing Zheng 
791830f790SXing Zheng 	mmio_write_32(CRU_BASE + CRU_DPLL_CON3, PLL_MODE(PLL_NORMAL_MODE));
80977001aaSXing Zheng }
81977001aaSXing Zheng 
82977001aaSXing Zheng void handle_dram(void)
83977001aaSXing Zheng {
84*50bde47fSDerek Basehore 	mmio_setbits_32(PHY_REG(0, 927), (1 << 22));
85*50bde47fSDerek Basehore 	mmio_setbits_32(PHY_REG(1, 927), (1 << 22));
86977001aaSXing Zheng 	idle_port();
87977001aaSXing Zheng 
881830f790SXing Zheng 	mmio_write_32(CIC_BASE + CIC_CTRL0,
89977001aaSXing Zheng 		      (((0x3 << 4) | (1 << 2) | 1) << 16) |
90977001aaSXing Zheng 		      (1 << 2) | 1 |
91977001aaSXing Zheng 		      mmio_read_32(PARAM_ADDR + PARAM_FREQ_SELECT));
921830f790SXing Zheng 	while ((mmio_read_32(CIC_BASE + CIC_STATUS0) & (1 << 2)) == 0)
93977001aaSXing Zheng 		continue;
94977001aaSXing Zheng 
95977001aaSXing Zheng 	ddr_set_pll();
961830f790SXing Zheng 	mmio_write_32(CIC_BASE + CIC_CTRL0, 0x20002);
971830f790SXing Zheng 	while ((mmio_read_32(CIC_BASE + CIC_STATUS0) & (1 << 0)) == 0)
98977001aaSXing Zheng 		continue;
99977001aaSXing Zheng 
100977001aaSXing Zheng 	deidle_port();
101*50bde47fSDerek Basehore 	mmio_clrbits_32(PHY_REG(0, 927), (1 << 22));
102*50bde47fSDerek Basehore 	mmio_clrbits_32(PHY_REG(1, 927), (1 << 22));
103977001aaSXing Zheng }
104