xref: /rk3399_ARM-atf/plat/rockchip/rk3399/drivers/pmu/m0_ctl.c (revision d0d0f171643a22bbc3d06f5b6dde40cc1d9d5d11)
1977001aaSXing Zheng /*
2977001aaSXing Zheng  * Copyright (c) 2016, ARM Limited and Contributors. All rights reserved.
3977001aaSXing Zheng  *
482cb2c1aSdp-arm  * SPDX-License-Identifier: BSD-3-Clause
5977001aaSXing Zheng  */
6977001aaSXing Zheng 
7977001aaSXing Zheng #include <assert.h>
809d40e0eSAntonio Nino Diaz 
909d40e0eSAntonio Nino Diaz #include <arch_helpers.h>
1009d40e0eSAntonio Nino Diaz #include <common/debug.h>
1109d40e0eSAntonio Nino Diaz #include <drivers/delay_timer.h>
1209d40e0eSAntonio Nino Diaz #include <lib/mmio.h>
1309d40e0eSAntonio Nino Diaz 
14977001aaSXing Zheng #include <m0_ctl.h>
15977001aaSXing Zheng #include <plat_private.h>
16977001aaSXing Zheng #include <rk3399_def.h>
17e3525114SXing Zheng #include <secure.h>
18977001aaSXing Zheng #include <soc.h>
19977001aaSXing Zheng 
m0_init(void)20977001aaSXing Zheng void m0_init(void)
21977001aaSXing Zheng {
22977001aaSXing Zheng 	/* secure config for M0 */
23977001aaSXing Zheng 	mmio_write_32(SGRF_BASE + SGRF_PMU_CON(0), WMSK_BIT(7));
24e3525114SXing Zheng 	mmio_write_32(SGRF_BASE + SGRF_SOC_CON(6), WMSK_BIT(12));
25977001aaSXing Zheng 
2687b5c17fSLin Huang 	/* document is wrong, PMU_CRU_GATEDIS_CON0 do not need set MASK BIT */
2787b5c17fSLin Huang 	mmio_setbits_32(PMUCRU_BASE + PMUCRU_GATEDIS_CON0, 0x02);
28977001aaSXing Zheng 
29977001aaSXing Zheng 	/*
30977001aaSXing Zheng 	 * To switch the parent to xin24M and div == 1,
31977001aaSXing Zheng 	 *
32977001aaSXing Zheng 	 * We need to close most of the PLLs and clocks except the OSC 24MHz
33977001aaSXing Zheng 	 * durning suspend, and this should be enough to supplies the ddrfreq,
34977001aaSXing Zheng 	 * For the simple handle, we just keep the fixed 24MHz to supply the
35977001aaSXing Zheng 	 * suspend and ddrfreq directly.
36977001aaSXing Zheng 	 */
37977001aaSXing Zheng 	mmio_write_32(PMUCRU_BASE + PMUCRU_CLKSEL_CON0,
38977001aaSXing Zheng 		      BIT_WITH_WMSK(15) | BITS_WITH_WMASK(0x0, 0x1f, 8));
39ca9286c6SLin Huang 
40ca9286c6SLin Huang 	mmio_write_32(PMUCRU_BASE + PMUCRU_CLKGATE_CON2, WMSK_BIT(5));
41977001aaSXing Zheng }
42977001aaSXing Zheng 
m0_configure_execute_addr(uintptr_t addr)43ff4735cfSLin Huang void m0_configure_execute_addr(uintptr_t addr)
44ff4735cfSLin Huang {
45ff4735cfSLin Huang 	/* set the execute address for M0 */
46ff4735cfSLin Huang 	mmio_write_32(SGRF_BASE + SGRF_PMU_CON(3),
47ff4735cfSLin Huang 		      BITS_WITH_WMASK((addr >> 12) & 0xffff,
48*79ca7807SJustin Chadwell 				      0xffffu, 0));
49ff4735cfSLin Huang 	mmio_write_32(SGRF_BASE + SGRF_PMU_CON(7),
50ff4735cfSLin Huang 		      BITS_WITH_WMASK((addr >> 28) & 0xf,
51*79ca7807SJustin Chadwell 				      0xfu, 0));
52ff4735cfSLin Huang }
53ff4735cfSLin Huang 
m0_start(void)54977001aaSXing Zheng void m0_start(void)
55977001aaSXing Zheng {
56ca9286c6SLin Huang 	/* enable clocks for M0 */
57ca9286c6SLin Huang 	mmio_write_32(PMUCRU_BASE + PMUCRU_CLKGATE_CON2,
58ca9286c6SLin Huang 		      BITS_WITH_WMASK(0x0, 0xf, 0));
59ca9286c6SLin Huang 
60977001aaSXing Zheng 	/* clean the PARAM_M0_DONE flag, mean that M0 will start working */
61977001aaSXing Zheng 	mmio_write_32(M0_PARAM_ADDR + PARAM_M0_DONE, 0);
62c6e15d14SDerek Basehore 	dmbst();
63977001aaSXing Zheng 
64ca9286c6SLin Huang 	mmio_write_32(PMUCRU_BASE + PMUCRU_SOFTRST_CON0,
65ca9286c6SLin Huang 		      BITS_WITH_WMASK(0x0, 0x4, 0));
66977001aaSXing Zheng 
67ca9286c6SLin Huang 	udelay(5);
68977001aaSXing Zheng 	/* start M0 */
69977001aaSXing Zheng 	mmio_write_32(PMUCRU_BASE + PMUCRU_SOFTRST_CON0,
70ca9286c6SLin Huang 		      BITS_WITH_WMASK(0x0, 0x20, 0));
71ca9286c6SLin Huang 	dmbst();
72977001aaSXing Zheng }
73977001aaSXing Zheng 
m0_stop(void)74977001aaSXing Zheng void m0_stop(void)
75977001aaSXing Zheng {
76977001aaSXing Zheng 	/* stop M0 */
77977001aaSXing Zheng 	mmio_write_32(PMUCRU_BASE + PMUCRU_SOFTRST_CON0,
78977001aaSXing Zheng 		      BITS_WITH_WMASK(0x24, 0x24, 0));
79977001aaSXing Zheng 
80977001aaSXing Zheng 	/* disable clocks for M0 */
81977001aaSXing Zheng 	mmio_write_32(PMUCRU_BASE + PMUCRU_CLKGATE_CON2,
82ca9286c6SLin Huang 		      BITS_WITH_WMASK(0xf, 0xf, 0));
83977001aaSXing Zheng }
84977001aaSXing Zheng 
m0_wait_done(void)85977001aaSXing Zheng void m0_wait_done(void)
86977001aaSXing Zheng {
87ca9286c6SLin Huang 	do {
88c6e15d14SDerek Basehore 		/*
89c6e15d14SDerek Basehore 		 * Don't starve the M0 for access to SRAM, so delay before
90c6e15d14SDerek Basehore 		 * reading the PARAM_M0_DONE value again.
91c6e15d14SDerek Basehore 		 */
92c6e15d14SDerek Basehore 		udelay(5);
93977001aaSXing Zheng 		dsb();
94ca9286c6SLin Huang 	} while (mmio_read_32(M0_PARAM_ADDR + PARAM_M0_DONE) != M0_DONE_FLAG);
95ca9286c6SLin Huang 
96ca9286c6SLin Huang 	/*
97ca9286c6SLin Huang 	 * Let the M0 settle into WFI before we leave. This is so we don't reset
98ca9286c6SLin Huang 	 * the M0 in a bad spot which can cause problems with the M0.
99ca9286c6SLin Huang 	 */
100ca9286c6SLin Huang 	udelay(10);
101ca9286c6SLin Huang 	dsb();
102c6e15d14SDerek Basehore }
103