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 <assert.h> 8*09d40e0eSAntonio Nino Diaz 958032586SSamuel Holland #include <platform_def.h> 10*09d40e0eSAntonio Nino Diaz 11*09d40e0eSAntonio Nino Diaz #include <arch_helpers.h> 12*09d40e0eSAntonio Nino Diaz #include <common/debug.h> 13*09d40e0eSAntonio Nino Diaz #include <drivers/arm/gicv2.h> 14*09d40e0eSAntonio Nino Diaz #include <drivers/delay_timer.h> 15*09d40e0eSAntonio Nino Diaz #include <lib/mmio.h> 16*09d40e0eSAntonio Nino Diaz #include <lib/psci/psci.h> 17*09d40e0eSAntonio Nino Diaz #include <plat/common/platform.h> 18*09d40e0eSAntonio Nino Diaz 19560581ecSSamuel Holland #include <sunxi_cpucfg.h> 204ec1a239SAndre Przywara #include <sunxi_mmap.h> 214ec1a239SAndre Przywara #include <sunxi_private.h> 2258032586SSamuel Holland 2358032586SSamuel Holland #define SUNXI_WDOG0_CTRL_REG (SUNXI_WDOG_BASE + 0x0010) 2458032586SSamuel Holland #define SUNXI_WDOG0_CFG_REG (SUNXI_WDOG_BASE + 0x0014) 2558032586SSamuel Holland #define SUNXI_WDOG0_MODE_REG (SUNXI_WDOG_BASE + 0x0018) 2658032586SSamuel Holland 27560581ecSSamuel Holland #define mpidr_is_valid(mpidr) ( \ 28560581ecSSamuel Holland MPIDR_AFFLVL3_VAL(mpidr) == 0 && \ 29560581ecSSamuel Holland MPIDR_AFFLVL2_VAL(mpidr) == 0 && \ 30560581ecSSamuel Holland MPIDR_AFFLVL1_VAL(mpidr) < PLATFORM_CLUSTER_COUNT && \ 31560581ecSSamuel Holland MPIDR_AFFLVL0_VAL(mpidr) < PLATFORM_MAX_CPUS_PER_CLUSTER) 32560581ecSSamuel Holland 33560581ecSSamuel Holland static int sunxi_pwr_domain_on(u_register_t mpidr) 34560581ecSSamuel Holland { 35560581ecSSamuel Holland if (mpidr_is_valid(mpidr) == 0) 36560581ecSSamuel Holland return PSCI_E_INTERN_FAIL; 37560581ecSSamuel Holland 38560581ecSSamuel Holland sunxi_cpu_on(MPIDR_AFFLVL1_VAL(mpidr), MPIDR_AFFLVL0_VAL(mpidr)); 39560581ecSSamuel Holland 40560581ecSSamuel Holland return PSCI_E_SUCCESS; 41560581ecSSamuel Holland } 42560581ecSSamuel Holland 43560581ecSSamuel Holland static void sunxi_pwr_domain_off(const psci_power_state_t *target_state) 44560581ecSSamuel Holland { 45560581ecSSamuel Holland gicv2_cpuif_disable(); 46560581ecSSamuel Holland } 47560581ecSSamuel Holland 487db0c960SAndre Przywara static void __dead2 sunxi_pwr_down_wfi(const psci_power_state_t *target_state) 497db0c960SAndre Przywara { 507db0c960SAndre Przywara u_register_t mpidr = read_mpidr(); 517db0c960SAndre Przywara 527db0c960SAndre Przywara sunxi_cpu_off(MPIDR_AFFLVL1_VAL(mpidr), MPIDR_AFFLVL0_VAL(mpidr)); 537db0c960SAndre Przywara 547db0c960SAndre Przywara while (1) 557db0c960SAndre Przywara wfi(); 567db0c960SAndre Przywara } 577db0c960SAndre Przywara 58560581ecSSamuel Holland static void sunxi_pwr_domain_on_finish(const psci_power_state_t *target_state) 59560581ecSSamuel Holland { 60560581ecSSamuel Holland gicv2_pcpu_distif_init(); 61560581ecSSamuel Holland gicv2_cpuif_enable(); 62560581ecSSamuel Holland } 63560581ecSSamuel Holland 6458032586SSamuel Holland static void __dead2 sunxi_system_off(void) 6558032586SSamuel Holland { 66333d66cfSSamuel Holland /* Turn off all secondary CPUs */ 67333d66cfSSamuel Holland sunxi_disable_secondary_cpus(plat_my_core_pos()); 68333d66cfSSamuel Holland 695069c1cfSIcenowy Zheng sunxi_power_down(); 7058032586SSamuel Holland } 7158032586SSamuel Holland 7258032586SSamuel Holland static void __dead2 sunxi_system_reset(void) 7358032586SSamuel Holland { 7458032586SSamuel Holland /* Reset the whole system when the watchdog times out */ 7558032586SSamuel Holland mmio_write_32(SUNXI_WDOG0_CFG_REG, 1); 7658032586SSamuel Holland /* Enable the watchdog with the shortest timeout (0.5 seconds) */ 7758032586SSamuel Holland mmio_write_32(SUNXI_WDOG0_MODE_REG, (0 << 4) | 1); 7858032586SSamuel Holland /* Wait for twice the watchdog timeout before panicking */ 7958032586SSamuel Holland mdelay(1000); 8058032586SSamuel Holland 8158032586SSamuel Holland ERROR("PSCI: System reset failed\n"); 8258032586SSamuel Holland wfi(); 8358032586SSamuel Holland panic(); 8458032586SSamuel Holland } 8558032586SSamuel Holland 86560581ecSSamuel Holland static int sunxi_validate_ns_entrypoint(uintptr_t ns_entrypoint) 87560581ecSSamuel Holland { 88560581ecSSamuel Holland /* The non-secure entry point must be in DRAM */ 89c520be4bSAndre Przywara if (ns_entrypoint >= SUNXI_DRAM_BASE) 90560581ecSSamuel Holland return PSCI_E_SUCCESS; 91560581ecSSamuel Holland 92560581ecSSamuel Holland return PSCI_E_INVALID_ADDRESS; 93560581ecSSamuel Holland } 94560581ecSSamuel Holland 9558032586SSamuel Holland static plat_psci_ops_t sunxi_psci_ops = { 96560581ecSSamuel Holland .pwr_domain_on = sunxi_pwr_domain_on, 97560581ecSSamuel Holland .pwr_domain_off = sunxi_pwr_domain_off, 987db0c960SAndre Przywara .pwr_domain_pwr_down_wfi = sunxi_pwr_down_wfi, 99560581ecSSamuel Holland .pwr_domain_on_finish = sunxi_pwr_domain_on_finish, 10058032586SSamuel Holland .system_off = sunxi_system_off, 10158032586SSamuel Holland .system_reset = sunxi_system_reset, 102560581ecSSamuel Holland .validate_ns_entrypoint = sunxi_validate_ns_entrypoint, 10358032586SSamuel Holland }; 10458032586SSamuel Holland 10558032586SSamuel Holland int plat_setup_psci_ops(uintptr_t sec_entrypoint, 10658032586SSamuel Holland const plat_psci_ops_t **psci_ops) 10758032586SSamuel Holland { 10858032586SSamuel Holland assert(psci_ops); 10958032586SSamuel Holland 110560581ecSSamuel Holland for (int cpu = 0; cpu < PLATFORM_CORE_COUNT; cpu += 1) { 111560581ecSSamuel Holland mmio_write_32(SUNXI_CPUCFG_RVBAR_LO_REG(cpu), 112560581ecSSamuel Holland sec_entrypoint & 0xffffffff); 113560581ecSSamuel Holland mmio_write_32(SUNXI_CPUCFG_RVBAR_HI_REG(cpu), 114560581ecSSamuel Holland sec_entrypoint >> 32); 115560581ecSSamuel Holland } 116560581ecSSamuel Holland 11758032586SSamuel Holland *psci_ops = &sunxi_psci_ops; 11858032586SSamuel Holland 11958032586SSamuel Holland return 0; 12058032586SSamuel Holland } 121