xref: /rk3399_ARM-atf/plat/rockchip/rk3399/drivers/pmu/m0_ctl.c (revision 82cb2c1ad9897473743f08437d0a3995bed561b9)
1977001aaSXing Zheng /*
2977001aaSXing Zheng  * Copyright (c) 2016, ARM Limited and Contributors. All rights reserved.
3977001aaSXing Zheng  *
4*82cb2c1aSdp-arm  * SPDX-License-Identifier: BSD-3-Clause
5977001aaSXing Zheng  */
6977001aaSXing Zheng 
7977001aaSXing Zheng #include <arch_helpers.h>
8977001aaSXing Zheng #include <assert.h>
9977001aaSXing Zheng #include <debug.h>
10c6e15d14SDerek Basehore #include <delay_timer.h>
11977001aaSXing Zheng #include <mmio.h>
12977001aaSXing Zheng #include <m0_ctl.h>
13977001aaSXing Zheng #include <plat_private.h>
14977001aaSXing Zheng #include <rk3399_def.h>
15e3525114SXing Zheng #include <secure.h>
16977001aaSXing Zheng #include <soc.h>
17977001aaSXing Zheng 
18977001aaSXing Zheng void m0_init(void)
19977001aaSXing Zheng {
20977001aaSXing Zheng 	/* secure config for M0 */
21977001aaSXing Zheng 	mmio_write_32(SGRF_BASE + SGRF_PMU_CON(0), WMSK_BIT(7));
22e3525114SXing Zheng 	mmio_write_32(SGRF_BASE + SGRF_SOC_CON(6), WMSK_BIT(12));
23977001aaSXing Zheng 
24977001aaSXing Zheng 	/* set the execute address for M0 */
25977001aaSXing Zheng 	mmio_write_32(SGRF_BASE + SGRF_PMU_CON(3),
26977001aaSXing Zheng 		      BITS_WITH_WMASK((M0_BINCODE_BASE >> 12) & 0xffff,
27977001aaSXing Zheng 				      0xffff, 0));
28977001aaSXing Zheng 	mmio_write_32(SGRF_BASE + SGRF_PMU_CON(7),
29977001aaSXing Zheng 		      BITS_WITH_WMASK((M0_BINCODE_BASE >> 28) & 0xf,
30977001aaSXing Zheng 				      0xf, 0));
31977001aaSXing Zheng 
3287b5c17fSLin Huang 	/* document is wrong, PMU_CRU_GATEDIS_CON0 do not need set MASK BIT */
3387b5c17fSLin Huang 	mmio_setbits_32(PMUCRU_BASE + PMUCRU_GATEDIS_CON0, 0x02);
34977001aaSXing Zheng 
35977001aaSXing Zheng 	/*
36977001aaSXing Zheng 	 * To switch the parent to xin24M and div == 1,
37977001aaSXing Zheng 	 *
38977001aaSXing Zheng 	 * We need to close most of the PLLs and clocks except the OSC 24MHz
39977001aaSXing Zheng 	 * durning suspend, and this should be enough to supplies the ddrfreq,
40977001aaSXing Zheng 	 * For the simple handle, we just keep the fixed 24MHz to supply the
41977001aaSXing Zheng 	 * suspend and ddrfreq directly.
42977001aaSXing Zheng 	 */
43977001aaSXing Zheng 	mmio_write_32(PMUCRU_BASE + PMUCRU_CLKSEL_CON0,
44977001aaSXing Zheng 		      BIT_WITH_WMSK(15) | BITS_WITH_WMASK(0x0, 0x1f, 8));
45ca9286c6SLin Huang 
46ca9286c6SLin Huang 	mmio_write_32(PMUCRU_BASE + PMUCRU_CLKGATE_CON2, WMSK_BIT(5));
47977001aaSXing Zheng }
48977001aaSXing Zheng 
49977001aaSXing Zheng void m0_start(void)
50977001aaSXing Zheng {
51ca9286c6SLin Huang 	/* enable clocks for M0 */
52ca9286c6SLin Huang 	mmio_write_32(PMUCRU_BASE + PMUCRU_CLKGATE_CON2,
53ca9286c6SLin Huang 		      BITS_WITH_WMASK(0x0, 0xf, 0));
54ca9286c6SLin Huang 
55977001aaSXing Zheng 	/* clean the PARAM_M0_DONE flag, mean that M0 will start working */
56977001aaSXing Zheng 	mmio_write_32(M0_PARAM_ADDR + PARAM_M0_DONE, 0);
57c6e15d14SDerek Basehore 	dmbst();
58977001aaSXing Zheng 
59ca9286c6SLin Huang 	mmio_write_32(PMUCRU_BASE + PMUCRU_SOFTRST_CON0,
60ca9286c6SLin Huang 		      BITS_WITH_WMASK(0x0, 0x4, 0));
61977001aaSXing Zheng 
62ca9286c6SLin Huang 	udelay(5);
63977001aaSXing Zheng 	/* start M0 */
64977001aaSXing Zheng 	mmio_write_32(PMUCRU_BASE + PMUCRU_SOFTRST_CON0,
65ca9286c6SLin Huang 		      BITS_WITH_WMASK(0x0, 0x20, 0));
66ca9286c6SLin Huang 	dmbst();
67977001aaSXing Zheng }
68977001aaSXing Zheng 
69977001aaSXing Zheng void m0_stop(void)
70977001aaSXing Zheng {
71977001aaSXing Zheng 	/* stop M0 */
72977001aaSXing Zheng 	mmio_write_32(PMUCRU_BASE + PMUCRU_SOFTRST_CON0,
73977001aaSXing Zheng 		      BITS_WITH_WMASK(0x24, 0x24, 0));
74977001aaSXing Zheng 
75977001aaSXing Zheng 	/* disable clocks for M0 */
76977001aaSXing Zheng 	mmio_write_32(PMUCRU_BASE + PMUCRU_CLKGATE_CON2,
77ca9286c6SLin Huang 		      BITS_WITH_WMASK(0xf, 0xf, 0));
78977001aaSXing Zheng }
79977001aaSXing Zheng 
80977001aaSXing Zheng void m0_wait_done(void)
81977001aaSXing Zheng {
82ca9286c6SLin Huang 	do {
83c6e15d14SDerek Basehore 		/*
84c6e15d14SDerek Basehore 		 * Don't starve the M0 for access to SRAM, so delay before
85c6e15d14SDerek Basehore 		 * reading the PARAM_M0_DONE value again.
86c6e15d14SDerek Basehore 		 */
87c6e15d14SDerek Basehore 		udelay(5);
88977001aaSXing Zheng 		dsb();
89ca9286c6SLin Huang 	} while (mmio_read_32(M0_PARAM_ADDR + PARAM_M0_DONE) != M0_DONE_FLAG);
90ca9286c6SLin Huang 
91ca9286c6SLin Huang 	/*
92ca9286c6SLin Huang 	 * Let the M0 settle into WFI before we leave. This is so we don't reset
93ca9286c6SLin Huang 	 * the M0 in a bad spot which can cause problems with the M0.
94ca9286c6SLin Huang 	 */
95ca9286c6SLin Huang 	udelay(10);
96ca9286c6SLin Huang 	dsb();
97c6e15d14SDerek Basehore }
98