158032586SSamuel Holland /* 258032586SSamuel Holland * Copyright (c) 2017-2018, ARM Limited and Contributors. All rights reserved. 358032586SSamuel Holland * 458032586SSamuel Holland * SPDX-License-Identifier: BSD-3-Clause 558032586SSamuel Holland */ 658032586SSamuel Holland 758032586SSamuel Holland #include <arch_helpers.h> 858032586SSamuel Holland #include <assert.h> 958032586SSamuel Holland #include <debug.h> 1058032586SSamuel Holland #include <delay_timer.h> 11*560581ecSSamuel Holland #include <gicv2.h> 1258032586SSamuel Holland #include <mmio.h> 1358032586SSamuel Holland #include <platform.h> 1458032586SSamuel Holland #include <platform_def.h> 1558032586SSamuel Holland #include <psci.h> 1658032586SSamuel Holland #include <sunxi_mmap.h> 17*560581ecSSamuel Holland #include <sunxi_cpucfg.h> 1858032586SSamuel Holland 1958032586SSamuel Holland #define SUNXI_WDOG0_CTRL_REG (SUNXI_WDOG_BASE + 0x0010) 2058032586SSamuel Holland #define SUNXI_WDOG0_CFG_REG (SUNXI_WDOG_BASE + 0x0014) 2158032586SSamuel Holland #define SUNXI_WDOG0_MODE_REG (SUNXI_WDOG_BASE + 0x0018) 2258032586SSamuel Holland 23333d66cfSSamuel Holland #include "sunxi_private.h" 24333d66cfSSamuel Holland 25*560581ecSSamuel Holland #define mpidr_is_valid(mpidr) ( \ 26*560581ecSSamuel Holland MPIDR_AFFLVL3_VAL(mpidr) == 0 && \ 27*560581ecSSamuel Holland MPIDR_AFFLVL2_VAL(mpidr) == 0 && \ 28*560581ecSSamuel Holland MPIDR_AFFLVL1_VAL(mpidr) < PLATFORM_CLUSTER_COUNT && \ 29*560581ecSSamuel Holland MPIDR_AFFLVL0_VAL(mpidr) < PLATFORM_MAX_CPUS_PER_CLUSTER) 30*560581ecSSamuel Holland 31*560581ecSSamuel Holland static int sunxi_pwr_domain_on(u_register_t mpidr) 32*560581ecSSamuel Holland { 33*560581ecSSamuel Holland if (mpidr_is_valid(mpidr) == 0) 34*560581ecSSamuel Holland return PSCI_E_INTERN_FAIL; 35*560581ecSSamuel Holland 36*560581ecSSamuel Holland sunxi_cpu_on(MPIDR_AFFLVL1_VAL(mpidr), MPIDR_AFFLVL0_VAL(mpidr)); 37*560581ecSSamuel Holland 38*560581ecSSamuel Holland return PSCI_E_SUCCESS; 39*560581ecSSamuel Holland } 40*560581ecSSamuel Holland 41*560581ecSSamuel Holland static void sunxi_pwr_domain_off(const psci_power_state_t *target_state) 42*560581ecSSamuel Holland { 43*560581ecSSamuel Holland gicv2_cpuif_disable(); 44*560581ecSSamuel Holland } 45*560581ecSSamuel Holland 46*560581ecSSamuel Holland static void sunxi_pwr_domain_on_finish(const psci_power_state_t *target_state) 47*560581ecSSamuel Holland { 48*560581ecSSamuel Holland gicv2_pcpu_distif_init(); 49*560581ecSSamuel Holland gicv2_cpuif_enable(); 50*560581ecSSamuel Holland } 51*560581ecSSamuel Holland 5258032586SSamuel Holland static void __dead2 sunxi_system_off(void) 5358032586SSamuel Holland { 54333d66cfSSamuel Holland /* Turn off all secondary CPUs */ 55333d66cfSSamuel Holland sunxi_disable_secondary_cpus(plat_my_core_pos()); 56333d66cfSSamuel Holland 5758032586SSamuel Holland ERROR("PSCI: Full shutdown not implemented, halting\n"); 5858032586SSamuel Holland wfi(); 5958032586SSamuel Holland panic(); 6058032586SSamuel Holland } 6158032586SSamuel Holland 6258032586SSamuel Holland static void __dead2 sunxi_system_reset(void) 6358032586SSamuel Holland { 6458032586SSamuel Holland /* Reset the whole system when the watchdog times out */ 6558032586SSamuel Holland mmio_write_32(SUNXI_WDOG0_CFG_REG, 1); 6658032586SSamuel Holland /* Enable the watchdog with the shortest timeout (0.5 seconds) */ 6758032586SSamuel Holland mmio_write_32(SUNXI_WDOG0_MODE_REG, (0 << 4) | 1); 6858032586SSamuel Holland /* Wait for twice the watchdog timeout before panicking */ 6958032586SSamuel Holland mdelay(1000); 7058032586SSamuel Holland 7158032586SSamuel Holland ERROR("PSCI: System reset failed\n"); 7258032586SSamuel Holland wfi(); 7358032586SSamuel Holland panic(); 7458032586SSamuel Holland } 7558032586SSamuel Holland 76*560581ecSSamuel Holland static int sunxi_validate_ns_entrypoint(uintptr_t ns_entrypoint) 77*560581ecSSamuel Holland { 78*560581ecSSamuel Holland /* The non-secure entry point must be in DRAM */ 79*560581ecSSamuel Holland if (ns_entrypoint >= SUNXI_DRAM_BASE && 80*560581ecSSamuel Holland ns_entrypoint < SUNXI_DRAM_BASE + SUNXI_DRAM_SIZE) 81*560581ecSSamuel Holland return PSCI_E_SUCCESS; 82*560581ecSSamuel Holland 83*560581ecSSamuel Holland return PSCI_E_INVALID_ADDRESS; 84*560581ecSSamuel Holland } 85*560581ecSSamuel Holland 8658032586SSamuel Holland static plat_psci_ops_t sunxi_psci_ops = { 87*560581ecSSamuel Holland .pwr_domain_on = sunxi_pwr_domain_on, 88*560581ecSSamuel Holland .pwr_domain_off = sunxi_pwr_domain_off, 89*560581ecSSamuel Holland .pwr_domain_on_finish = sunxi_pwr_domain_on_finish, 9058032586SSamuel Holland .system_off = sunxi_system_off, 9158032586SSamuel Holland .system_reset = sunxi_system_reset, 92*560581ecSSamuel Holland .validate_ns_entrypoint = sunxi_validate_ns_entrypoint, 9358032586SSamuel Holland }; 9458032586SSamuel Holland 9558032586SSamuel Holland int plat_setup_psci_ops(uintptr_t sec_entrypoint, 9658032586SSamuel Holland const plat_psci_ops_t **psci_ops) 9758032586SSamuel Holland { 9858032586SSamuel Holland assert(psci_ops); 9958032586SSamuel Holland 100*560581ecSSamuel Holland for (int cpu = 0; cpu < PLATFORM_CORE_COUNT; cpu += 1) { 101*560581ecSSamuel Holland mmio_write_32(SUNXI_CPUCFG_RVBAR_LO_REG(cpu), 102*560581ecSSamuel Holland sec_entrypoint & 0xffffffff); 103*560581ecSSamuel Holland mmio_write_32(SUNXI_CPUCFG_RVBAR_HI_REG(cpu), 104*560581ecSSamuel Holland sec_entrypoint >> 32); 105*560581ecSSamuel Holland } 106*560581ecSSamuel Holland 10758032586SSamuel Holland *psci_ops = &sunxi_psci_ops; 10858032586SSamuel Holland 10958032586SSamuel Holland return 0; 11058032586SSamuel Holland } 111