xref: /rk3399_ARM-atf/plat/rockchip/rk3399/drivers/pmu/m0_ctl.c (revision 87b5c17f915e3eccb4711e2adcf4e22cbb8af101)
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 <arch_helpers.h>
32977001aaSXing Zheng #include <assert.h>
33977001aaSXing Zheng #include <debug.h>
34c6e15d14SDerek Basehore #include <delay_timer.h>
35977001aaSXing Zheng #include <mmio.h>
36977001aaSXing Zheng #include <m0_ctl.h>
37977001aaSXing Zheng #include <plat_private.h>
38977001aaSXing Zheng #include <rk3399_def.h>
39977001aaSXing Zheng #include <soc.h>
40977001aaSXing Zheng 
41977001aaSXing Zheng void m0_init(void)
42977001aaSXing Zheng {
43977001aaSXing Zheng 	/* secure config for M0 */
44977001aaSXing Zheng 	mmio_write_32(SGRF_BASE + SGRF_PMU_CON(0), WMSK_BIT(7));
45977001aaSXing Zheng 	mmio_write_32(SGRF_BASE + SGRF_SOC_CON6, WMSK_BIT(12));
46977001aaSXing Zheng 
47977001aaSXing Zheng 	/* set the execute address for M0 */
48977001aaSXing Zheng 	mmio_write_32(SGRF_BASE + SGRF_PMU_CON(3),
49977001aaSXing Zheng 		      BITS_WITH_WMASK((M0_BINCODE_BASE >> 12) & 0xffff,
50977001aaSXing Zheng 				      0xffff, 0));
51977001aaSXing Zheng 	mmio_write_32(SGRF_BASE + SGRF_PMU_CON(7),
52977001aaSXing Zheng 		      BITS_WITH_WMASK((M0_BINCODE_BASE >> 28) & 0xf,
53977001aaSXing Zheng 				      0xf, 0));
54977001aaSXing Zheng 
55*87b5c17fSLin Huang 	/* document is wrong, PMU_CRU_GATEDIS_CON0 do not need set MASK BIT */
56*87b5c17fSLin Huang 	mmio_setbits_32(PMUCRU_BASE + PMUCRU_GATEDIS_CON0, 0x02);
57977001aaSXing Zheng 
58977001aaSXing Zheng 	/*
59977001aaSXing Zheng 	 * To switch the parent to xin24M and div == 1,
60977001aaSXing Zheng 	 *
61977001aaSXing Zheng 	 * We need to close most of the PLLs and clocks except the OSC 24MHz
62977001aaSXing Zheng 	 * durning suspend, and this should be enough to supplies the ddrfreq,
63977001aaSXing Zheng 	 * For the simple handle, we just keep the fixed 24MHz to supply the
64977001aaSXing Zheng 	 * suspend and ddrfreq directly.
65977001aaSXing Zheng 	 */
66977001aaSXing Zheng 	mmio_write_32(PMUCRU_BASE + PMUCRU_CLKSEL_CON0,
67977001aaSXing Zheng 		      BIT_WITH_WMSK(15) | BITS_WITH_WMASK(0x0, 0x1f, 8));
68ca9286c6SLin Huang 
69ca9286c6SLin Huang 	mmio_write_32(PMUCRU_BASE + PMUCRU_CLKGATE_CON2, WMSK_BIT(5));
70977001aaSXing Zheng }
71977001aaSXing Zheng 
72977001aaSXing Zheng void m0_start(void)
73977001aaSXing Zheng {
74ca9286c6SLin Huang 	/* enable clocks for M0 */
75ca9286c6SLin Huang 	mmio_write_32(PMUCRU_BASE + PMUCRU_CLKGATE_CON2,
76ca9286c6SLin Huang 		      BITS_WITH_WMASK(0x0, 0xf, 0));
77ca9286c6SLin Huang 
78977001aaSXing Zheng 	/* clean the PARAM_M0_DONE flag, mean that M0 will start working */
79977001aaSXing Zheng 	mmio_write_32(M0_PARAM_ADDR + PARAM_M0_DONE, 0);
80c6e15d14SDerek Basehore 	dmbst();
81977001aaSXing Zheng 
82ca9286c6SLin Huang 	mmio_write_32(PMUCRU_BASE + PMUCRU_SOFTRST_CON0,
83ca9286c6SLin Huang 		      BITS_WITH_WMASK(0x0, 0x4, 0));
84977001aaSXing Zheng 
85ca9286c6SLin Huang 	udelay(5);
86977001aaSXing Zheng 	/* start M0 */
87977001aaSXing Zheng 	mmio_write_32(PMUCRU_BASE + PMUCRU_SOFTRST_CON0,
88ca9286c6SLin Huang 		      BITS_WITH_WMASK(0x0, 0x20, 0));
89ca9286c6SLin Huang 	dmbst();
90977001aaSXing Zheng }
91977001aaSXing Zheng 
92977001aaSXing Zheng void m0_stop(void)
93977001aaSXing Zheng {
94977001aaSXing Zheng 	/* stop M0 */
95977001aaSXing Zheng 	mmio_write_32(PMUCRU_BASE + PMUCRU_SOFTRST_CON0,
96977001aaSXing Zheng 		      BITS_WITH_WMASK(0x24, 0x24, 0));
97977001aaSXing Zheng 
98977001aaSXing Zheng 	/* disable clocks for M0 */
99977001aaSXing Zheng 	mmio_write_32(PMUCRU_BASE + PMUCRU_CLKGATE_CON2,
100ca9286c6SLin Huang 		      BITS_WITH_WMASK(0xf, 0xf, 0));
101977001aaSXing Zheng }
102977001aaSXing Zheng 
103977001aaSXing Zheng void m0_wait_done(void)
104977001aaSXing Zheng {
105ca9286c6SLin Huang 	do {
106c6e15d14SDerek Basehore 		/*
107c6e15d14SDerek Basehore 		 * Don't starve the M0 for access to SRAM, so delay before
108c6e15d14SDerek Basehore 		 * reading the PARAM_M0_DONE value again.
109c6e15d14SDerek Basehore 		 */
110c6e15d14SDerek Basehore 		udelay(5);
111977001aaSXing Zheng 		dsb();
112ca9286c6SLin Huang 	} while (mmio_read_32(M0_PARAM_ADDR + PARAM_M0_DONE) != M0_DONE_FLAG);
113ca9286c6SLin Huang 
114ca9286c6SLin Huang 	/*
115ca9286c6SLin Huang 	 * Let the M0 settle into WFI before we leave. This is so we don't reset
116ca9286c6SLin Huang 	 * the M0 in a bad spot which can cause problems with the M0.
117ca9286c6SLin Huang 	 */
118ca9286c6SLin Huang 	udelay(10);
119ca9286c6SLin Huang 	dsb();
120c6e15d14SDerek Basehore }
121