158032586SSamuel Holland /* 2*e382c88eSSamuel Holland * Copyright (c) 2017-2020, ARM Limited and Contributors. All rights reserved. 358032586SSamuel Holland * 458032586SSamuel Holland * SPDX-License-Identifier: BSD-3-Clause 558032586SSamuel Holland */ 658032586SSamuel Holland 758032586SSamuel Holland #include <assert.h> 809d40e0eSAntonio Nino Diaz 958032586SSamuel Holland #include <platform_def.h> 1009d40e0eSAntonio Nino Diaz 1109d40e0eSAntonio Nino Diaz #include <arch_helpers.h> 1209d40e0eSAntonio Nino Diaz #include <common/debug.h> 13*e382c88eSSamuel Holland #include <drivers/arm/css/css_scpi.h> 1409d40e0eSAntonio Nino Diaz #include <drivers/arm/gicv2.h> 1509d40e0eSAntonio Nino Diaz #include <drivers/delay_timer.h> 1609d40e0eSAntonio Nino Diaz #include <lib/mmio.h> 1709d40e0eSAntonio Nino Diaz #include <lib/psci/psci.h> 1809d40e0eSAntonio Nino Diaz #include <plat/common/platform.h> 1909d40e0eSAntonio Nino Diaz 20560581ecSSamuel Holland #include <sunxi_cpucfg.h> 21*e382c88eSSamuel Holland #include <sunxi_def.h> 224ec1a239SAndre Przywara #include <sunxi_mmap.h> 234ec1a239SAndre Przywara #include <sunxi_private.h> 2458032586SSamuel Holland 25523ab5beSClément Péron #define SUNXI_WDOG0_CTRL_REG (SUNXI_R_WDOG_BASE + 0x0010) 26523ab5beSClément Péron #define SUNXI_WDOG0_CFG_REG (SUNXI_R_WDOG_BASE + 0x0014) 27523ab5beSClément Péron #define SUNXI_WDOG0_MODE_REG (SUNXI_R_WDOG_BASE + 0x0018) 2858032586SSamuel Holland 29*e382c88eSSamuel Holland #define CPU_PWR_LVL MPIDR_AFFLVL0 30*e382c88eSSamuel Holland #define CLUSTER_PWR_LVL MPIDR_AFFLVL1 31*e382c88eSSamuel Holland #define SYSTEM_PWR_LVL MPIDR_AFFLVL2 32*e382c88eSSamuel Holland 33*e382c88eSSamuel Holland #define CPU_PWR_STATE(state) \ 34*e382c88eSSamuel Holland ((state)->pwr_domain_state[CPU_PWR_LVL]) 35*e382c88eSSamuel Holland #define CLUSTER_PWR_STATE(state) \ 36*e382c88eSSamuel Holland ((state)->pwr_domain_state[CLUSTER_PWR_LVL]) 37*e382c88eSSamuel Holland #define SYSTEM_PWR_STATE(state) \ 38*e382c88eSSamuel Holland ((state)->pwr_domain_state[SYSTEM_PWR_LVL]) 39*e382c88eSSamuel Holland 40*e382c88eSSamuel Holland #define mpidr_is_valid(mpidr) (plat_core_pos_by_mpidr(mpidr) >= 0) 41*e382c88eSSamuel Holland 42*e382c88eSSamuel Holland /* 43*e382c88eSSamuel Holland * The addresses for the SCP exception vectors are defined in the or1k 44*e382c88eSSamuel Holland * architecture specification. 45*e382c88eSSamuel Holland */ 46*e382c88eSSamuel Holland #define OR1K_VEC_FIRST 0x01 47*e382c88eSSamuel Holland #define OR1K_VEC_LAST 0x0e 48*e382c88eSSamuel Holland #define OR1K_VEC_ADDR(n) (0x100 * (n)) 49*e382c88eSSamuel Holland 50*e382c88eSSamuel Holland /* 51*e382c88eSSamuel Holland * This magic value is the little-endian representation of the or1k 52*e382c88eSSamuel Holland * instruction "l.mfspr r2, r0, 0x12", which is guaranteed to be the 53*e382c88eSSamuel Holland * first instruction in the SCP firmware. 54*e382c88eSSamuel Holland */ 55*e382c88eSSamuel Holland #define SCP_FIRMWARE_MAGIC 0xb4400012 56*e382c88eSSamuel Holland 57*e382c88eSSamuel Holland static bool scpi_available; 58*e382c88eSSamuel Holland 59*e382c88eSSamuel Holland static inline scpi_power_state_t scpi_map_state(plat_local_state_t psci_state) 60*e382c88eSSamuel Holland { 61*e382c88eSSamuel Holland if (is_local_state_run(psci_state)) 62*e382c88eSSamuel Holland return scpi_power_on; 63*e382c88eSSamuel Holland if (is_local_state_retn(psci_state)) 64*e382c88eSSamuel Holland return scpi_power_retention; 65*e382c88eSSamuel Holland return scpi_power_off; 66*e382c88eSSamuel Holland } 67*e382c88eSSamuel Holland 68*e382c88eSSamuel Holland static void sunxi_cpu_standby(plat_local_state_t cpu_state) 69*e382c88eSSamuel Holland { 70*e382c88eSSamuel Holland u_register_t scr = read_scr_el3(); 71*e382c88eSSamuel Holland 72*e382c88eSSamuel Holland assert(is_local_state_retn(cpu_state)); 73*e382c88eSSamuel Holland 74*e382c88eSSamuel Holland write_scr_el3(scr | SCR_IRQ_BIT); 75*e382c88eSSamuel Holland wfi(); 76*e382c88eSSamuel Holland write_scr_el3(scr); 77*e382c88eSSamuel Holland } 78560581ecSSamuel Holland 79560581ecSSamuel Holland static int sunxi_pwr_domain_on(u_register_t mpidr) 80560581ecSSamuel Holland { 81560581ecSSamuel Holland if (mpidr_is_valid(mpidr) == 0) 82560581ecSSamuel Holland return PSCI_E_INTERN_FAIL; 83560581ecSSamuel Holland 84*e382c88eSSamuel Holland if (scpi_available) { 85*e382c88eSSamuel Holland scpi_set_css_power_state(mpidr, 86*e382c88eSSamuel Holland scpi_power_on, 87*e382c88eSSamuel Holland scpi_power_on, 88*e382c88eSSamuel Holland scpi_power_on); 89*e382c88eSSamuel Holland } else { 905d4bd66dSSamuel Holland sunxi_cpu_on(mpidr); 91*e382c88eSSamuel Holland } 92560581ecSSamuel Holland 93560581ecSSamuel Holland return PSCI_E_SUCCESS; 94560581ecSSamuel Holland } 95560581ecSSamuel Holland 96560581ecSSamuel Holland static void sunxi_pwr_domain_off(const psci_power_state_t *target_state) 97560581ecSSamuel Holland { 98*e382c88eSSamuel Holland plat_local_state_t cpu_pwr_state = CPU_PWR_STATE(target_state); 99*e382c88eSSamuel Holland plat_local_state_t cluster_pwr_state = CLUSTER_PWR_STATE(target_state); 100*e382c88eSSamuel Holland plat_local_state_t system_pwr_state = SYSTEM_PWR_STATE(target_state); 101*e382c88eSSamuel Holland 102*e382c88eSSamuel Holland if (is_local_state_off(cpu_pwr_state)) 103560581ecSSamuel Holland gicv2_cpuif_disable(); 104*e382c88eSSamuel Holland 105*e382c88eSSamuel Holland if (scpi_available) { 106*e382c88eSSamuel Holland scpi_set_css_power_state(read_mpidr(), 107*e382c88eSSamuel Holland scpi_map_state(cpu_pwr_state), 108*e382c88eSSamuel Holland scpi_map_state(cluster_pwr_state), 109*e382c88eSSamuel Holland scpi_map_state(system_pwr_state)); 110*e382c88eSSamuel Holland } 111560581ecSSamuel Holland } 112560581ecSSamuel Holland 1137db0c960SAndre Przywara static void __dead2 sunxi_pwr_down_wfi(const psci_power_state_t *target_state) 1147db0c960SAndre Przywara { 1155d4bd66dSSamuel Holland sunxi_cpu_off(read_mpidr()); 1167db0c960SAndre Przywara 1177db0c960SAndre Przywara while (1) 1187db0c960SAndre Przywara wfi(); 1197db0c960SAndre Przywara } 1207db0c960SAndre Przywara 121560581ecSSamuel Holland static void sunxi_pwr_domain_on_finish(const psci_power_state_t *target_state) 122560581ecSSamuel Holland { 123*e382c88eSSamuel Holland if (is_local_state_off(SYSTEM_PWR_STATE(target_state))) 124*e382c88eSSamuel Holland gicv2_distif_init(); 125*e382c88eSSamuel Holland if (is_local_state_off(CPU_PWR_STATE(target_state))) { 126560581ecSSamuel Holland gicv2_pcpu_distif_init(); 127560581ecSSamuel Holland gicv2_cpuif_enable(); 128560581ecSSamuel Holland } 129*e382c88eSSamuel Holland } 130560581ecSSamuel Holland 13158032586SSamuel Holland static void __dead2 sunxi_system_off(void) 13258032586SSamuel Holland { 133*e382c88eSSamuel Holland gicv2_cpuif_disable(); 134*e382c88eSSamuel Holland 135*e382c88eSSamuel Holland if (scpi_available) { 136*e382c88eSSamuel Holland /* Send the power down request to the SCP */ 137*e382c88eSSamuel Holland uint32_t ret = scpi_sys_power_state(scpi_system_shutdown); 138*e382c88eSSamuel Holland 139*e382c88eSSamuel Holland if (ret != SCP_OK) 140*e382c88eSSamuel Holland ERROR("PSCI: SCPI %s failed: %d\n", "shutdown", ret); 141*e382c88eSSamuel Holland } 142*e382c88eSSamuel Holland 143333d66cfSSamuel Holland /* Turn off all secondary CPUs */ 1445d4bd66dSSamuel Holland sunxi_disable_secondary_cpus(read_mpidr()); 145333d66cfSSamuel Holland 1465069c1cfSIcenowy Zheng sunxi_power_down(); 147818e6732SSamuel Holland 148818e6732SSamuel Holland udelay(1000); 149818e6732SSamuel Holland ERROR("PSCI: Cannot turn off system, halting\n"); 150818e6732SSamuel Holland wfi(); 151818e6732SSamuel Holland panic(); 15258032586SSamuel Holland } 15358032586SSamuel Holland 15458032586SSamuel Holland static void __dead2 sunxi_system_reset(void) 15558032586SSamuel Holland { 156*e382c88eSSamuel Holland gicv2_cpuif_disable(); 157*e382c88eSSamuel Holland 158*e382c88eSSamuel Holland if (scpi_available) { 159*e382c88eSSamuel Holland /* Send the system reset request to the SCP */ 160*e382c88eSSamuel Holland uint32_t ret = scpi_sys_power_state(scpi_system_reboot); 161*e382c88eSSamuel Holland 162*e382c88eSSamuel Holland if (ret != SCP_OK) 163*e382c88eSSamuel Holland ERROR("PSCI: SCPI %s failed: %d\n", "reboot", ret); 164*e382c88eSSamuel Holland } 165*e382c88eSSamuel Holland 16658032586SSamuel Holland /* Reset the whole system when the watchdog times out */ 16758032586SSamuel Holland mmio_write_32(SUNXI_WDOG0_CFG_REG, 1); 16858032586SSamuel Holland /* Enable the watchdog with the shortest timeout (0.5 seconds) */ 16958032586SSamuel Holland mmio_write_32(SUNXI_WDOG0_MODE_REG, (0 << 4) | 1); 17058032586SSamuel Holland /* Wait for twice the watchdog timeout before panicking */ 17158032586SSamuel Holland mdelay(1000); 17258032586SSamuel Holland 17358032586SSamuel Holland ERROR("PSCI: System reset failed\n"); 17458032586SSamuel Holland wfi(); 17558032586SSamuel Holland panic(); 17658032586SSamuel Holland } 17758032586SSamuel Holland 178*e382c88eSSamuel Holland static int sunxi_validate_power_state(unsigned int power_state, 179*e382c88eSSamuel Holland psci_power_state_t *req_state) 180*e382c88eSSamuel Holland { 181*e382c88eSSamuel Holland unsigned int power_level = psci_get_pstate_pwrlvl(power_state); 182*e382c88eSSamuel Holland unsigned int type = psci_get_pstate_type(power_state); 183*e382c88eSSamuel Holland 184*e382c88eSSamuel Holland assert(req_state != NULL); 185*e382c88eSSamuel Holland 186*e382c88eSSamuel Holland if (power_level > PLAT_MAX_PWR_LVL) 187*e382c88eSSamuel Holland return PSCI_E_INVALID_PARAMS; 188*e382c88eSSamuel Holland 189*e382c88eSSamuel Holland if (type == PSTATE_TYPE_STANDBY) { 190*e382c88eSSamuel Holland /* Only one retention power state is supported. */ 191*e382c88eSSamuel Holland if (psci_get_pstate_id(power_state) > 0) 192*e382c88eSSamuel Holland return PSCI_E_INVALID_PARAMS; 193*e382c88eSSamuel Holland /* The SoC cannot be suspended without losing state */ 194*e382c88eSSamuel Holland if (power_level == SYSTEM_PWR_LVL) 195*e382c88eSSamuel Holland return PSCI_E_INVALID_PARAMS; 196*e382c88eSSamuel Holland for (unsigned int i = 0; i <= power_level; ++i) 197*e382c88eSSamuel Holland req_state->pwr_domain_state[i] = PLAT_MAX_RET_STATE; 198*e382c88eSSamuel Holland } else { 199*e382c88eSSamuel Holland /* Only one off power state is supported. */ 200*e382c88eSSamuel Holland if (psci_get_pstate_id(power_state) > 0) 201*e382c88eSSamuel Holland return PSCI_E_INVALID_PARAMS; 202*e382c88eSSamuel Holland for (unsigned int i = 0; i <= power_level; ++i) 203*e382c88eSSamuel Holland req_state->pwr_domain_state[i] = PLAT_MAX_OFF_STATE; 204*e382c88eSSamuel Holland } 205*e382c88eSSamuel Holland /* Higher power domain levels should all remain running */ 206*e382c88eSSamuel Holland for (unsigned int i = power_level + 1; i <= PLAT_MAX_PWR_LVL; ++i) 207*e382c88eSSamuel Holland req_state->pwr_domain_state[i] = PSCI_LOCAL_STATE_RUN; 208*e382c88eSSamuel Holland 209*e382c88eSSamuel Holland return PSCI_E_SUCCESS; 210*e382c88eSSamuel Holland } 211*e382c88eSSamuel Holland 212560581ecSSamuel Holland static int sunxi_validate_ns_entrypoint(uintptr_t ns_entrypoint) 213560581ecSSamuel Holland { 214560581ecSSamuel Holland /* The non-secure entry point must be in DRAM */ 215c520be4bSAndre Przywara if (ns_entrypoint >= SUNXI_DRAM_BASE) 216560581ecSSamuel Holland return PSCI_E_SUCCESS; 217560581ecSSamuel Holland 218560581ecSSamuel Holland return PSCI_E_INVALID_ADDRESS; 219560581ecSSamuel Holland } 220560581ecSSamuel Holland 221*e382c88eSSamuel Holland static void sunxi_get_sys_suspend_power_state(psci_power_state_t *req_state) 222*e382c88eSSamuel Holland { 223*e382c88eSSamuel Holland assert(req_state); 224*e382c88eSSamuel Holland 225*e382c88eSSamuel Holland for (unsigned int i = 0; i <= PLAT_MAX_PWR_LVL; ++i) 226*e382c88eSSamuel Holland req_state->pwr_domain_state[i] = PLAT_MAX_OFF_STATE; 227*e382c88eSSamuel Holland } 228*e382c88eSSamuel Holland 229*e382c88eSSamuel Holland static int sunxi_get_node_hw_state(u_register_t mpidr, 230*e382c88eSSamuel Holland unsigned int power_level) 231*e382c88eSSamuel Holland { 232*e382c88eSSamuel Holland unsigned int cluster_state, cpu_state; 233*e382c88eSSamuel Holland unsigned int cpu = MPIDR_AFFLVL0_VAL(mpidr); 234*e382c88eSSamuel Holland 235*e382c88eSSamuel Holland /* SoC power level (always on if PSCI works). */ 236*e382c88eSSamuel Holland if (power_level == SYSTEM_PWR_LVL) 237*e382c88eSSamuel Holland return HW_ON; 238*e382c88eSSamuel Holland if (scpi_get_css_power_state(mpidr, &cpu_state, &cluster_state)) 239*e382c88eSSamuel Holland return PSCI_E_NOT_SUPPORTED; 240*e382c88eSSamuel Holland /* Cluster power level (full power state available). */ 241*e382c88eSSamuel Holland if (power_level == CLUSTER_PWR_LVL) { 242*e382c88eSSamuel Holland if (cluster_state == scpi_power_on) 243*e382c88eSSamuel Holland return HW_ON; 244*e382c88eSSamuel Holland if (cluster_state == scpi_power_retention) 245*e382c88eSSamuel Holland return HW_STANDBY; 246*e382c88eSSamuel Holland return HW_OFF; 247*e382c88eSSamuel Holland } 248*e382c88eSSamuel Holland /* CPU power level (one bit boolean for on or off). */ 249*e382c88eSSamuel Holland return ((cpu_state & BIT(cpu)) != 0) ? HW_ON : HW_OFF; 250*e382c88eSSamuel Holland } 251*e382c88eSSamuel Holland 25258032586SSamuel Holland static plat_psci_ops_t sunxi_psci_ops = { 253*e382c88eSSamuel Holland .cpu_standby = sunxi_cpu_standby, 254560581ecSSamuel Holland .pwr_domain_on = sunxi_pwr_domain_on, 255560581ecSSamuel Holland .pwr_domain_off = sunxi_pwr_domain_off, 256560581ecSSamuel Holland .pwr_domain_on_finish = sunxi_pwr_domain_on_finish, 25758032586SSamuel Holland .system_off = sunxi_system_off, 25858032586SSamuel Holland .system_reset = sunxi_system_reset, 259*e382c88eSSamuel Holland .validate_power_state = sunxi_validate_power_state, 260560581ecSSamuel Holland .validate_ns_entrypoint = sunxi_validate_ns_entrypoint, 26158032586SSamuel Holland }; 26258032586SSamuel Holland 26358032586SSamuel Holland int plat_setup_psci_ops(uintptr_t sec_entrypoint, 26458032586SSamuel Holland const plat_psci_ops_t **psci_ops) 26558032586SSamuel Holland { 26658032586SSamuel Holland assert(psci_ops); 26758032586SSamuel Holland 268*e382c88eSSamuel Holland /* Program all CPU entry points. */ 269*e382c88eSSamuel Holland for (unsigned int cpu = 0; cpu < PLATFORM_CORE_COUNT; ++cpu) { 270560581ecSSamuel Holland mmio_write_32(SUNXI_CPUCFG_RVBAR_LO_REG(cpu), 271560581ecSSamuel Holland sec_entrypoint & 0xffffffff); 272560581ecSSamuel Holland mmio_write_32(SUNXI_CPUCFG_RVBAR_HI_REG(cpu), 273560581ecSSamuel Holland sec_entrypoint >> 32); 274560581ecSSamuel Holland } 275560581ecSSamuel Holland 276*e382c88eSSamuel Holland /* Check for a valid SCP firmware, and boot the SCP if found. */ 277*e382c88eSSamuel Holland if (mmio_read_32(SUNXI_SCP_BASE) == SCP_FIRMWARE_MAGIC) { 278*e382c88eSSamuel Holland /* Program SCP exception vectors to the firmware entrypoint. */ 279*e382c88eSSamuel Holland for (unsigned int i = OR1K_VEC_FIRST; i <= OR1K_VEC_LAST; ++i) { 280*e382c88eSSamuel Holland uint32_t vector = SUNXI_SRAM_A2_BASE + OR1K_VEC_ADDR(i); 281*e382c88eSSamuel Holland uint32_t offset = SUNXI_SCP_BASE - vector; 282*e382c88eSSamuel Holland 283*e382c88eSSamuel Holland mmio_write_32(vector, offset >> 2); 284*e382c88eSSamuel Holland clean_dcache_range(vector, sizeof(uint32_t)); 285*e382c88eSSamuel Holland } 286*e382c88eSSamuel Holland /* Take the SCP out of reset. */ 287*e382c88eSSamuel Holland mmio_setbits_32(SUNXI_R_CPUCFG_BASE, BIT(0)); 288*e382c88eSSamuel Holland /* Wait for the SCP firmware to boot. */ 289*e382c88eSSamuel Holland if (scpi_wait_ready() == 0) 290*e382c88eSSamuel Holland scpi_available = true; 291*e382c88eSSamuel Holland } 292*e382c88eSSamuel Holland 293*e382c88eSSamuel Holland NOTICE("PSCI: System suspend is %s\n", 294*e382c88eSSamuel Holland scpi_available ? "available via SCPI" : "unavailable"); 295*e382c88eSSamuel Holland if (scpi_available) { 296*e382c88eSSamuel Holland /* Suspend is only available via SCPI. */ 297*e382c88eSSamuel Holland sunxi_psci_ops.pwr_domain_suspend = sunxi_pwr_domain_off; 298*e382c88eSSamuel Holland sunxi_psci_ops.pwr_domain_suspend_finish = sunxi_pwr_domain_on_finish; 299*e382c88eSSamuel Holland sunxi_psci_ops.get_sys_suspend_power_state = sunxi_get_sys_suspend_power_state; 300*e382c88eSSamuel Holland sunxi_psci_ops.get_node_hw_state = sunxi_get_node_hw_state; 301*e382c88eSSamuel Holland } else { 302*e382c88eSSamuel Holland /* This is only needed when SCPI is unavailable. */ 303*e382c88eSSamuel Holland sunxi_psci_ops.pwr_domain_pwr_down_wfi = sunxi_pwr_down_wfi; 304*e382c88eSSamuel Holland } 305*e382c88eSSamuel Holland 30658032586SSamuel Holland *psci_ops = &sunxi_psci_ops; 30758032586SSamuel Holland 30858032586SSamuel Holland return 0; 30958032586SSamuel Holland } 310