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 <arch_helpers.h> 8977001aaSXing Zheng #include <assert.h> 9977001aaSXing Zheng #include <debug.h> 10c6e15d14SDerek Basehore #include <delay_timer.h> 11977001aaSXing Zheng #include <m0_ctl.h> 12ee1ebbd1SIsla Mitchell #include <mmio.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 2487b5c17fSLin Huang /* document is wrong, PMU_CRU_GATEDIS_CON0 do not need set MASK BIT */ 2587b5c17fSLin Huang mmio_setbits_32(PMUCRU_BASE + PMUCRU_GATEDIS_CON0, 0x02); 26977001aaSXing Zheng 27977001aaSXing Zheng /* 28977001aaSXing Zheng * To switch the parent to xin24M and div == 1, 29977001aaSXing Zheng * 30977001aaSXing Zheng * We need to close most of the PLLs and clocks except the OSC 24MHz 31977001aaSXing Zheng * durning suspend, and this should be enough to supplies the ddrfreq, 32977001aaSXing Zheng * For the simple handle, we just keep the fixed 24MHz to supply the 33977001aaSXing Zheng * suspend and ddrfreq directly. 34977001aaSXing Zheng */ 35977001aaSXing Zheng mmio_write_32(PMUCRU_BASE + PMUCRU_CLKSEL_CON0, 36977001aaSXing Zheng BIT_WITH_WMSK(15) | BITS_WITH_WMASK(0x0, 0x1f, 8)); 37ca9286c6SLin Huang 38ca9286c6SLin Huang mmio_write_32(PMUCRU_BASE + PMUCRU_CLKGATE_CON2, WMSK_BIT(5)); 39977001aaSXing Zheng } 40977001aaSXing Zheng 41*ff4735cfSLin Huang void m0_configure_execute_addr(uintptr_t addr) 42*ff4735cfSLin Huang { 43*ff4735cfSLin Huang /* set the execute address for M0 */ 44*ff4735cfSLin Huang mmio_write_32(SGRF_BASE + SGRF_PMU_CON(3), 45*ff4735cfSLin Huang BITS_WITH_WMASK((addr >> 12) & 0xffff, 46*ff4735cfSLin Huang 0xffff, 0)); 47*ff4735cfSLin Huang mmio_write_32(SGRF_BASE + SGRF_PMU_CON(7), 48*ff4735cfSLin Huang BITS_WITH_WMASK((addr >> 28) & 0xf, 49*ff4735cfSLin Huang 0xf, 0)); 50*ff4735cfSLin Huang } 51*ff4735cfSLin Huang 52977001aaSXing Zheng void m0_start(void) 53977001aaSXing Zheng { 54ca9286c6SLin Huang /* enable clocks for M0 */ 55ca9286c6SLin Huang mmio_write_32(PMUCRU_BASE + PMUCRU_CLKGATE_CON2, 56ca9286c6SLin Huang BITS_WITH_WMASK(0x0, 0xf, 0)); 57ca9286c6SLin Huang 58977001aaSXing Zheng /* clean the PARAM_M0_DONE flag, mean that M0 will start working */ 59977001aaSXing Zheng mmio_write_32(M0_PARAM_ADDR + PARAM_M0_DONE, 0); 60c6e15d14SDerek Basehore dmbst(); 61977001aaSXing Zheng 62ca9286c6SLin Huang mmio_write_32(PMUCRU_BASE + PMUCRU_SOFTRST_CON0, 63ca9286c6SLin Huang BITS_WITH_WMASK(0x0, 0x4, 0)); 64977001aaSXing Zheng 65ca9286c6SLin Huang udelay(5); 66977001aaSXing Zheng /* start M0 */ 67977001aaSXing Zheng mmio_write_32(PMUCRU_BASE + PMUCRU_SOFTRST_CON0, 68ca9286c6SLin Huang BITS_WITH_WMASK(0x0, 0x20, 0)); 69ca9286c6SLin Huang dmbst(); 70977001aaSXing Zheng } 71977001aaSXing Zheng 72977001aaSXing Zheng void m0_stop(void) 73977001aaSXing Zheng { 74977001aaSXing Zheng /* stop M0 */ 75977001aaSXing Zheng mmio_write_32(PMUCRU_BASE + PMUCRU_SOFTRST_CON0, 76977001aaSXing Zheng BITS_WITH_WMASK(0x24, 0x24, 0)); 77977001aaSXing Zheng 78977001aaSXing Zheng /* disable clocks for M0 */ 79977001aaSXing Zheng mmio_write_32(PMUCRU_BASE + PMUCRU_CLKGATE_CON2, 80ca9286c6SLin Huang BITS_WITH_WMASK(0xf, 0xf, 0)); 81977001aaSXing Zheng } 82977001aaSXing Zheng 83977001aaSXing Zheng void m0_wait_done(void) 84977001aaSXing Zheng { 85ca9286c6SLin Huang do { 86c6e15d14SDerek Basehore /* 87c6e15d14SDerek Basehore * Don't starve the M0 for access to SRAM, so delay before 88c6e15d14SDerek Basehore * reading the PARAM_M0_DONE value again. 89c6e15d14SDerek Basehore */ 90c6e15d14SDerek Basehore udelay(5); 91977001aaSXing Zheng dsb(); 92ca9286c6SLin Huang } while (mmio_read_32(M0_PARAM_ADDR + PARAM_M0_DONE) != M0_DONE_FLAG); 93ca9286c6SLin Huang 94ca9286c6SLin Huang /* 95ca9286c6SLin Huang * Let the M0 settle into WFI before we leave. This is so we don't reset 96ca9286c6SLin Huang * the M0 in a bad spot which can cause problems with the M0. 97ca9286c6SLin Huang */ 98ca9286c6SLin Huang udelay(10); 99ca9286c6SLin Huang dsb(); 100c6e15d14SDerek Basehore } 101