xref: /rk3399_ARM-atf/plat/rockchip/rk3399/drivers/pmu/m0_ctl.c (revision e3525114394324b7d4be104ccba24f0ca8ca8c6b)
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>
39*e3525114SXing Zheng #include <secure.h>
40977001aaSXing Zheng #include <soc.h>
41977001aaSXing Zheng 
42977001aaSXing Zheng void m0_init(void)
43977001aaSXing Zheng {
44977001aaSXing Zheng 	/* secure config for M0 */
45977001aaSXing Zheng 	mmio_write_32(SGRF_BASE + SGRF_PMU_CON(0), WMSK_BIT(7));
46*e3525114SXing Zheng 	mmio_write_32(SGRF_BASE + SGRF_SOC_CON(6), WMSK_BIT(12));
47977001aaSXing Zheng 
48977001aaSXing Zheng 	/* set the execute address for M0 */
49977001aaSXing Zheng 	mmio_write_32(SGRF_BASE + SGRF_PMU_CON(3),
50977001aaSXing Zheng 		      BITS_WITH_WMASK((M0_BINCODE_BASE >> 12) & 0xffff,
51977001aaSXing Zheng 				      0xffff, 0));
52977001aaSXing Zheng 	mmio_write_32(SGRF_BASE + SGRF_PMU_CON(7),
53977001aaSXing Zheng 		      BITS_WITH_WMASK((M0_BINCODE_BASE >> 28) & 0xf,
54977001aaSXing Zheng 				      0xf, 0));
55977001aaSXing Zheng 
5687b5c17fSLin Huang 	/* document is wrong, PMU_CRU_GATEDIS_CON0 do not need set MASK BIT */
5787b5c17fSLin Huang 	mmio_setbits_32(PMUCRU_BASE + PMUCRU_GATEDIS_CON0, 0x02);
58977001aaSXing Zheng 
59977001aaSXing Zheng 	/*
60977001aaSXing Zheng 	 * To switch the parent to xin24M and div == 1,
61977001aaSXing Zheng 	 *
62977001aaSXing Zheng 	 * We need to close most of the PLLs and clocks except the OSC 24MHz
63977001aaSXing Zheng 	 * durning suspend, and this should be enough to supplies the ddrfreq,
64977001aaSXing Zheng 	 * For the simple handle, we just keep the fixed 24MHz to supply the
65977001aaSXing Zheng 	 * suspend and ddrfreq directly.
66977001aaSXing Zheng 	 */
67977001aaSXing Zheng 	mmio_write_32(PMUCRU_BASE + PMUCRU_CLKSEL_CON0,
68977001aaSXing Zheng 		      BIT_WITH_WMSK(15) | BITS_WITH_WMASK(0x0, 0x1f, 8));
69ca9286c6SLin Huang 
70ca9286c6SLin Huang 	mmio_write_32(PMUCRU_BASE + PMUCRU_CLKGATE_CON2, WMSK_BIT(5));
71977001aaSXing Zheng }
72977001aaSXing Zheng 
73977001aaSXing Zheng void m0_start(void)
74977001aaSXing Zheng {
75ca9286c6SLin Huang 	/* enable clocks for M0 */
76ca9286c6SLin Huang 	mmio_write_32(PMUCRU_BASE + PMUCRU_CLKGATE_CON2,
77ca9286c6SLin Huang 		      BITS_WITH_WMASK(0x0, 0xf, 0));
78ca9286c6SLin Huang 
79977001aaSXing Zheng 	/* clean the PARAM_M0_DONE flag, mean that M0 will start working */
80977001aaSXing Zheng 	mmio_write_32(M0_PARAM_ADDR + PARAM_M0_DONE, 0);
81c6e15d14SDerek Basehore 	dmbst();
82977001aaSXing Zheng 
83ca9286c6SLin Huang 	mmio_write_32(PMUCRU_BASE + PMUCRU_SOFTRST_CON0,
84ca9286c6SLin Huang 		      BITS_WITH_WMASK(0x0, 0x4, 0));
85977001aaSXing Zheng 
86ca9286c6SLin Huang 	udelay(5);
87977001aaSXing Zheng 	/* start M0 */
88977001aaSXing Zheng 	mmio_write_32(PMUCRU_BASE + PMUCRU_SOFTRST_CON0,
89ca9286c6SLin Huang 		      BITS_WITH_WMASK(0x0, 0x20, 0));
90ca9286c6SLin Huang 	dmbst();
91977001aaSXing Zheng }
92977001aaSXing Zheng 
93977001aaSXing Zheng void m0_stop(void)
94977001aaSXing Zheng {
95977001aaSXing Zheng 	/* stop M0 */
96977001aaSXing Zheng 	mmio_write_32(PMUCRU_BASE + PMUCRU_SOFTRST_CON0,
97977001aaSXing Zheng 		      BITS_WITH_WMASK(0x24, 0x24, 0));
98977001aaSXing Zheng 
99977001aaSXing Zheng 	/* disable clocks for M0 */
100977001aaSXing Zheng 	mmio_write_32(PMUCRU_BASE + PMUCRU_CLKGATE_CON2,
101ca9286c6SLin Huang 		      BITS_WITH_WMASK(0xf, 0xf, 0));
102977001aaSXing Zheng }
103977001aaSXing Zheng 
104977001aaSXing Zheng void m0_wait_done(void)
105977001aaSXing Zheng {
106ca9286c6SLin Huang 	do {
107c6e15d14SDerek Basehore 		/*
108c6e15d14SDerek Basehore 		 * Don't starve the M0 for access to SRAM, so delay before
109c6e15d14SDerek Basehore 		 * reading the PARAM_M0_DONE value again.
110c6e15d14SDerek Basehore 		 */
111c6e15d14SDerek Basehore 		udelay(5);
112977001aaSXing Zheng 		dsb();
113ca9286c6SLin Huang 	} while (mmio_read_32(M0_PARAM_ADDR + PARAM_M0_DONE) != M0_DONE_FLAG);
114ca9286c6SLin Huang 
115ca9286c6SLin Huang 	/*
116ca9286c6SLin Huang 	 * Let the M0 settle into WFI before we leave. This is so we don't reset
117ca9286c6SLin Huang 	 * the M0 in a bad spot which can cause problems with the M0.
118ca9286c6SLin Huang 	 */
119ca9286c6SLin Huang 	udelay(10);
120ca9286c6SLin Huang 	dsb();
121c6e15d14SDerek Basehore }
122