16fba6e04STony Xie /* 26fba6e04STony Xie * Copyright (c) 2016, ARM Limited and Contributors. All rights reserved. 36fba6e04STony Xie * 46fba6e04STony Xie * Redistribution and use in source and binary forms, with or without 56fba6e04STony Xie * modification, are permitted provided that the following conditions are met: 66fba6e04STony Xie * 76fba6e04STony Xie * Redistributions of source code must retain the above copyright notice, this 86fba6e04STony Xie * list of conditions and the following disclaimer. 96fba6e04STony Xie * 106fba6e04STony Xie * Redistributions in binary form must reproduce the above copyright notice, 116fba6e04STony Xie * this list of conditions and the following disclaimer in the documentation 126fba6e04STony Xie * and/or other materials provided with the distribution. 136fba6e04STony Xie * 146fba6e04STony Xie * Neither the name of ARM nor the names of its contributors may be used 156fba6e04STony Xie * to endorse or promote products derived from this software without specific 166fba6e04STony Xie * prior written permission. 176fba6e04STony Xie * 186fba6e04STony Xie * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 196fba6e04STony Xie * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 206fba6e04STony Xie * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 216fba6e04STony Xie * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 226fba6e04STony Xie * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 236fba6e04STony Xie * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 246fba6e04STony Xie * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 256fba6e04STony Xie * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 266fba6e04STony Xie * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 276fba6e04STony Xie * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 286fba6e04STony Xie * POSSIBILITY OF SUCH DAMAGE. 296fba6e04STony Xie */ 306fba6e04STony Xie 316fba6e04STony Xie #include <arch_helpers.h> 32941c7147SXing Zheng #include <assert.h> 336fba6e04STony Xie #include <debug.h> 346fba6e04STony Xie #include <delay_timer.h> 35f91b969cSDerek Basehore #include <dfs.h> 36f91b969cSDerek Basehore #include <dram.h> 376fba6e04STony Xie #include <mmio.h> 38977001aaSXing Zheng #include <m0_ctl.h> 396fba6e04STony Xie #include <platform_def.h> 406fba6e04STony Xie #include <plat_private.h> 416fba6e04STony Xie #include <rk3399_def.h> 42*e3525114SXing Zheng #include <secure.h> 436fba6e04STony Xie #include <soc.h> 446fba6e04STony Xie 456fba6e04STony Xie /* Table of regions to map using the MMU. */ 466fba6e04STony Xie const mmap_region_t plat_rk_mmap[] = { 471830f790SXing Zheng MAP_REGION_FLAT(DEV_RNG0_BASE, DEV_RNG0_SIZE, 489ec78bdfSTony Xie MT_DEVICE | MT_RW | MT_SECURE), 494c127e68SCaesar Wang MAP_REGION_FLAT(PMUSRAM_BASE, PMUSRAM_SIZE, 504c127e68SCaesar Wang MT_MEMORY | MT_RW | MT_SECURE), 519ec78bdfSTony Xie 526fba6e04STony Xie { 0 } 536fba6e04STony Xie }; 546fba6e04STony Xie 556fba6e04STony Xie /* The RockChip power domain tree descriptor */ 566fba6e04STony Xie const unsigned char rockchip_power_domain_tree_desc[] = { 576fba6e04STony Xie /* No of root nodes */ 586fba6e04STony Xie PLATFORM_SYSTEM_COUNT, 596fba6e04STony Xie /* No of children for the root node */ 606fba6e04STony Xie PLATFORM_CLUSTER_COUNT, 616fba6e04STony Xie /* No of children for the first cluster node */ 626fba6e04STony Xie PLATFORM_CLUSTER0_CORE_COUNT, 636fba6e04STony Xie /* No of children for the second cluster node */ 646fba6e04STony Xie PLATFORM_CLUSTER1_CORE_COUNT 656fba6e04STony Xie }; 666fba6e04STony Xie 67*e3525114SXing Zheng /* sleep data for pll suspend */ 68*e3525114SXing Zheng static struct deepsleep_data_s slp_data; 69941c7147SXing Zheng 706fba6e04STony Xie static void set_pll_slow_mode(uint32_t pll_id) 716fba6e04STony Xie { 726fba6e04STony Xie if (pll_id == PPLL_ID) 736fba6e04STony Xie mmio_write_32(PMUCRU_BASE + PMUCRU_PPLL_CON(3), PLL_SLOW_MODE); 746fba6e04STony Xie else 756fba6e04STony Xie mmio_write_32((CRU_BASE + 766fba6e04STony Xie CRU_PLL_CON(pll_id, 3)), PLL_SLOW_MODE); 776fba6e04STony Xie } 786fba6e04STony Xie 796fba6e04STony Xie static void set_pll_normal_mode(uint32_t pll_id) 806fba6e04STony Xie { 816fba6e04STony Xie if (pll_id == PPLL_ID) 826fba6e04STony Xie mmio_write_32(PMUCRU_BASE + PMUCRU_PPLL_CON(3), PLL_NOMAL_MODE); 836fba6e04STony Xie else 846fba6e04STony Xie mmio_write_32(CRU_BASE + 856fba6e04STony Xie CRU_PLL_CON(pll_id, 3), PLL_NOMAL_MODE); 866fba6e04STony Xie } 876fba6e04STony Xie 886fba6e04STony Xie static void set_pll_bypass(uint32_t pll_id) 896fba6e04STony Xie { 906fba6e04STony Xie if (pll_id == PPLL_ID) 916fba6e04STony Xie mmio_write_32(PMUCRU_BASE + 926fba6e04STony Xie PMUCRU_PPLL_CON(3), PLL_BYPASS_MODE); 936fba6e04STony Xie else 946fba6e04STony Xie mmio_write_32(CRU_BASE + 956fba6e04STony Xie CRU_PLL_CON(pll_id, 3), PLL_BYPASS_MODE); 966fba6e04STony Xie } 976fba6e04STony Xie 986fba6e04STony Xie static void _pll_suspend(uint32_t pll_id) 996fba6e04STony Xie { 1006fba6e04STony Xie set_pll_slow_mode(pll_id); 1016fba6e04STony Xie set_pll_bypass(pll_id); 1026fba6e04STony Xie } 1036fba6e04STony Xie 1044c127e68SCaesar Wang /** 1054c127e68SCaesar Wang * disable_dvfs_plls - To suspend the specific PLLs 1064c127e68SCaesar Wang * 1074c127e68SCaesar Wang * When we close the center logic, the DPLL will be closed, 1084c127e68SCaesar Wang * so we need to keep the ABPLL and switch to it to supply 1094c127e68SCaesar Wang * clock for DDR during suspend, then we should not close 1104c127e68SCaesar Wang * the ABPLL and exclude ABPLL_ID. 1114c127e68SCaesar Wang */ 1125d3b1067SCaesar Wang void disable_dvfs_plls(void) 1135d3b1067SCaesar Wang { 1145d3b1067SCaesar Wang _pll_suspend(CPLL_ID); 1155d3b1067SCaesar Wang _pll_suspend(NPLL_ID); 1165d3b1067SCaesar Wang _pll_suspend(VPLL_ID); 1175d3b1067SCaesar Wang _pll_suspend(GPLL_ID); 1185d3b1067SCaesar Wang _pll_suspend(ALPLL_ID); 1195d3b1067SCaesar Wang } 1205d3b1067SCaesar Wang 1214c127e68SCaesar Wang /** 1224c127e68SCaesar Wang * disable_nodvfs_plls - To suspend the PPLL 1234c127e68SCaesar Wang */ 1245d3b1067SCaesar Wang void disable_nodvfs_plls(void) 1255d3b1067SCaesar Wang { 1265d3b1067SCaesar Wang _pll_suspend(PPLL_ID); 1275d3b1067SCaesar Wang } 1285d3b1067SCaesar Wang 1294c127e68SCaesar Wang /** 1304c127e68SCaesar Wang * restore_pll - Copy PLL settings from memory to a PLL. 1314c127e68SCaesar Wang * 1324c127e68SCaesar Wang * This will copy PLL settings from an array in memory to the memory mapped 1334c127e68SCaesar Wang * registers for a PLL. 1344c127e68SCaesar Wang * 1354c127e68SCaesar Wang * Note that: above the PLL exclude PPLL. 1364c127e68SCaesar Wang * 1374c127e68SCaesar Wang * pll_id: One of the values from enum plls_id 1384c127e68SCaesar Wang * src: Pointer to the array of values to restore from 1394c127e68SCaesar Wang */ 1404c127e68SCaesar Wang static void restore_pll(int pll_id, uint32_t *src) 1414c127e68SCaesar Wang { 1424c127e68SCaesar Wang /* Nice to have PLL off while configuring */ 1434c127e68SCaesar Wang mmio_write_32((CRU_BASE + CRU_PLL_CON(pll_id, 3)), PLL_SLOW_MODE); 1444c127e68SCaesar Wang 1454c127e68SCaesar Wang mmio_write_32(CRU_BASE + CRU_PLL_CON(pll_id, 0), src[0] | REG_SOC_WMSK); 1464c127e68SCaesar Wang mmio_write_32(CRU_BASE + CRU_PLL_CON(pll_id, 1), src[1] | REG_SOC_WMSK); 1474c127e68SCaesar Wang mmio_write_32(CRU_BASE + CRU_PLL_CON(pll_id, 2), src[2]); 1484c127e68SCaesar Wang mmio_write_32(CRU_BASE + CRU_PLL_CON(pll_id, 4), src[4] | REG_SOC_WMSK); 1494c127e68SCaesar Wang mmio_write_32(CRU_BASE + CRU_PLL_CON(pll_id, 5), src[5] | REG_SOC_WMSK); 1504c127e68SCaesar Wang 1514c127e68SCaesar Wang /* Do PLL_CON3 since that will enable things */ 1524c127e68SCaesar Wang mmio_write_32(CRU_BASE + CRU_PLL_CON(pll_id, 3), src[3] | REG_SOC_WMSK); 1534c127e68SCaesar Wang 1544c127e68SCaesar Wang /* Wait for PLL lock done */ 1554c127e68SCaesar Wang while ((mmio_read_32(CRU_BASE + CRU_PLL_CON(pll_id, 2)) & 1564c127e68SCaesar Wang 0x80000000) == 0x0) 1574c127e68SCaesar Wang ; 1584c127e68SCaesar Wang } 1594c127e68SCaesar Wang 1604c127e68SCaesar Wang /** 1614c127e68SCaesar Wang * save_pll - Copy PLL settings a PLL to memory 1624c127e68SCaesar Wang * 1634c127e68SCaesar Wang * This will copy PLL settings from the memory mapped registers for a PLL to 1644c127e68SCaesar Wang * an array in memory. 1654c127e68SCaesar Wang * 1664c127e68SCaesar Wang * Note that: above the PLL exclude PPLL. 1674c127e68SCaesar Wang * 1684c127e68SCaesar Wang * pll_id: One of the values from enum plls_id 1694c127e68SCaesar Wang * src: Pointer to the array of values to save to. 1704c127e68SCaesar Wang */ 1714c127e68SCaesar Wang static void save_pll(uint32_t *dst, int pll_id) 1724c127e68SCaesar Wang { 1734c127e68SCaesar Wang int i; 1744c127e68SCaesar Wang 1754c127e68SCaesar Wang for (i = 0; i < PLL_CON_COUNT; i++) 1764c127e68SCaesar Wang dst[i] = mmio_read_32(CRU_BASE + CRU_PLL_CON(pll_id, i)); 1774c127e68SCaesar Wang } 1784c127e68SCaesar Wang 1794c127e68SCaesar Wang /** 1804c127e68SCaesar Wang * prepare_abpll_for_ddrctrl - Copy DPLL settings to ABPLL 1814c127e68SCaesar Wang * 1824c127e68SCaesar Wang * This will copy DPLL settings from the memory mapped registers for a PLL to 1834c127e68SCaesar Wang * an array in memory. 1844c127e68SCaesar Wang */ 1854c127e68SCaesar Wang void prepare_abpll_for_ddrctrl(void) 1864c127e68SCaesar Wang { 1874c127e68SCaesar Wang save_pll(slp_data.plls_con[ABPLL_ID], ABPLL_ID); 1884c127e68SCaesar Wang save_pll(slp_data.plls_con[DPLL_ID], DPLL_ID); 1894c127e68SCaesar Wang 1904c127e68SCaesar Wang restore_pll(ABPLL_ID, slp_data.plls_con[DPLL_ID]); 1914c127e68SCaesar Wang } 1924c127e68SCaesar Wang 1934c127e68SCaesar Wang void restore_abpll(void) 1944c127e68SCaesar Wang { 1954c127e68SCaesar Wang restore_pll(ABPLL_ID, slp_data.plls_con[ABPLL_ID]); 1964c127e68SCaesar Wang } 1974c127e68SCaesar Wang 1984c127e68SCaesar Wang void restore_dpll(void) 1994c127e68SCaesar Wang { 2004c127e68SCaesar Wang restore_pll(DPLL_ID, slp_data.plls_con[DPLL_ID]); 2014c127e68SCaesar Wang } 2024c127e68SCaesar Wang 2039ec78bdfSTony Xie void clk_gate_con_save(void) 2049ec78bdfSTony Xie { 2059ec78bdfSTony Xie uint32_t i = 0; 2069ec78bdfSTony Xie 2079ec78bdfSTony Xie for (i = 0; i < PMUCRU_GATE_COUNT; i++) 2089ec78bdfSTony Xie slp_data.pmucru_gate_con[i] = 2099ec78bdfSTony Xie mmio_read_32(PMUCRU_BASE + PMUCRU_GATE_CON(i)); 2109ec78bdfSTony Xie 2119ec78bdfSTony Xie for (i = 0; i < CRU_GATE_COUNT; i++) 2129ec78bdfSTony Xie slp_data.cru_gate_con[i] = 2139ec78bdfSTony Xie mmio_read_32(CRU_BASE + CRU_GATE_CON(i)); 2149ec78bdfSTony Xie } 2159ec78bdfSTony Xie 2169ec78bdfSTony Xie void clk_gate_con_disable(void) 2179ec78bdfSTony Xie { 2189ec78bdfSTony Xie uint32_t i; 2199ec78bdfSTony Xie 2209ec78bdfSTony Xie for (i = 0; i < PMUCRU_GATE_COUNT; i++) 2219ec78bdfSTony Xie mmio_write_32(PMUCRU_BASE + PMUCRU_GATE_CON(i), REG_SOC_WMSK); 2229ec78bdfSTony Xie 2239ec78bdfSTony Xie for (i = 0; i < CRU_GATE_COUNT; i++) 2249ec78bdfSTony Xie mmio_write_32(CRU_BASE + CRU_GATE_CON(i), REG_SOC_WMSK); 2259ec78bdfSTony Xie } 2269ec78bdfSTony Xie 2279ec78bdfSTony Xie void clk_gate_con_restore(void) 2289ec78bdfSTony Xie { 2299ec78bdfSTony Xie uint32_t i; 2309ec78bdfSTony Xie 2319ec78bdfSTony Xie for (i = 0; i < PMUCRU_GATE_COUNT; i++) 2329ec78bdfSTony Xie mmio_write_32(PMUCRU_BASE + PMUCRU_GATE_CON(i), 2339ec78bdfSTony Xie REG_SOC_WMSK | slp_data.pmucru_gate_con[i]); 2349ec78bdfSTony Xie 2359ec78bdfSTony Xie for (i = 0; i < CRU_GATE_COUNT; i++) 2369ec78bdfSTony Xie mmio_write_32(CRU_BASE + CRU_GATE_CON(i), 2379ec78bdfSTony Xie REG_SOC_WMSK | slp_data.cru_gate_con[i]); 2389ec78bdfSTony Xie } 2399ec78bdfSTony Xie 2406fba6e04STony Xie static void set_plls_nobypass(uint32_t pll_id) 2416fba6e04STony Xie { 2426fba6e04STony Xie if (pll_id == PPLL_ID) 2436fba6e04STony Xie mmio_write_32(PMUCRU_BASE + PMUCRU_PPLL_CON(3), 2446fba6e04STony Xie PLL_NO_BYPASS_MODE); 2456fba6e04STony Xie else 2466fba6e04STony Xie mmio_write_32(CRU_BASE + CRU_PLL_CON(pll_id, 3), 2476fba6e04STony Xie PLL_NO_BYPASS_MODE); 2486fba6e04STony Xie } 2496fba6e04STony Xie 2505d3b1067SCaesar Wang static void _pll_resume(uint32_t pll_id) 2515d3b1067SCaesar Wang { 2525d3b1067SCaesar Wang set_plls_nobypass(pll_id); 2535d3b1067SCaesar Wang set_pll_normal_mode(pll_id); 2545d3b1067SCaesar Wang } 2555d3b1067SCaesar Wang 2564c127e68SCaesar Wang /** 2574c127e68SCaesar Wang * enable_dvfs_plls - To resume the specific PLLs 2584c127e68SCaesar Wang * 2594c127e68SCaesar Wang * Please see the comment at the disable_dvfs_plls() 2604c127e68SCaesar Wang * we don't suspend the ABPLL, so don't need resume 2614c127e68SCaesar Wang * it too. 2624c127e68SCaesar Wang */ 2635d3b1067SCaesar Wang void enable_dvfs_plls(void) 2646fba6e04STony Xie { 2655d3b1067SCaesar Wang _pll_resume(ALPLL_ID); 2665d3b1067SCaesar Wang _pll_resume(GPLL_ID); 2675d3b1067SCaesar Wang _pll_resume(VPLL_ID); 2685d3b1067SCaesar Wang _pll_resume(NPLL_ID); 2695d3b1067SCaesar Wang _pll_resume(CPLL_ID); 2706fba6e04STony Xie } 2715d3b1067SCaesar Wang 2724c127e68SCaesar Wang /** 2734c127e68SCaesar Wang * enable_nodvfs_plls - To resume the PPLL 2744c127e68SCaesar Wang */ 2755d3b1067SCaesar Wang void enable_nodvfs_plls(void) 2765d3b1067SCaesar Wang { 2775d3b1067SCaesar Wang _pll_resume(PPLL_ID); 2786fba6e04STony Xie } 2796fba6e04STony Xie 2806fba6e04STony Xie void soc_global_soft_reset_init(void) 2816fba6e04STony Xie { 2826fba6e04STony Xie mmio_write_32(PMUCRU_BASE + CRU_PMU_RSTHOLD_CON(1), 2836fba6e04STony Xie CRU_PMU_SGRF_RST_RLS); 284f47a25ddSCaesar Wang 285f47a25ddSCaesar Wang mmio_clrbits_32(CRU_BASE + CRU_GLB_RST_CON, 286f47a25ddSCaesar Wang CRU_PMU_WDTRST_MSK | CRU_PMU_FIRST_SFTRST_MSK); 2876fba6e04STony Xie } 2886fba6e04STony Xie 2896fba6e04STony Xie void __dead2 soc_global_soft_reset(void) 2906fba6e04STony Xie { 2916fba6e04STony Xie set_pll_slow_mode(VPLL_ID); 2926fba6e04STony Xie set_pll_slow_mode(NPLL_ID); 2936fba6e04STony Xie set_pll_slow_mode(GPLL_ID); 2946fba6e04STony Xie set_pll_slow_mode(CPLL_ID); 2956fba6e04STony Xie set_pll_slow_mode(PPLL_ID); 2966fba6e04STony Xie set_pll_slow_mode(ABPLL_ID); 2976fba6e04STony Xie set_pll_slow_mode(ALPLL_ID); 298f47a25ddSCaesar Wang 299f47a25ddSCaesar Wang dsb(); 300f47a25ddSCaesar Wang 3016fba6e04STony Xie mmio_write_32(CRU_BASE + CRU_GLB_SRST_FST, GLB_SRST_FST_CFG_VAL); 3026fba6e04STony Xie 3036fba6e04STony Xie /* 3046fba6e04STony Xie * Maybe the HW needs some times to reset the system, 3056fba6e04STony Xie * so we do not hope the core to excute valid codes. 3066fba6e04STony Xie */ 3076fba6e04STony Xie while (1) 3086fba6e04STony Xie ; 3096fba6e04STony Xie } 3106fba6e04STony Xie 3116fba6e04STony Xie void plat_rockchip_soc_init(void) 3126fba6e04STony Xie { 3136fba6e04STony Xie secure_timer_init(); 314*e3525114SXing Zheng secure_sgrf_init(); 315941c7147SXing Zheng secure_sgrf_ddr_rgn_init(); 3166fba6e04STony Xie soc_global_soft_reset_init(); 3179901dcf6SCaesar Wang plat_rockchip_gpio_init(); 318977001aaSXing Zheng m0_init(); 319613038bcSCaesar Wang dram_init(); 320f91b969cSDerek Basehore dram_dfs_init(); 3216fba6e04STony Xie } 322