Samuel Holland | b856664 | 2017-08-12 04:07:39 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2017-2018, ARM Limited and Contributors. All rights reserved. |
| 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | */ |
| 6 | |
Samuel Holland | b856664 | 2017-08-12 04:07:39 -0500 | [diff] [blame] | 7 | #include <assert.h> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 8 | |
Samuel Holland | b856664 | 2017-08-12 04:07:39 -0500 | [diff] [blame] | 9 | #include <platform_def.h> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 10 | |
| 11 | #include <arch_helpers.h> |
| 12 | #include <common/debug.h> |
| 13 | #include <drivers/arm/gicv2.h> |
| 14 | #include <drivers/delay_timer.h> |
| 15 | #include <lib/mmio.h> |
| 16 | #include <lib/psci/psci.h> |
| 17 | #include <plat/common/platform.h> |
| 18 | |
Samuel Holland | 0a9018c | 2017-08-12 04:07:39 -0500 | [diff] [blame] | 19 | #include <sunxi_cpucfg.h> |
Andre Przywara | 456208a | 2018-10-14 12:02:02 +0100 | [diff] [blame] | 20 | #include <sunxi_mmap.h> |
| 21 | #include <sunxi_private.h> |
Samuel Holland | b856664 | 2017-08-12 04:07:39 -0500 | [diff] [blame] | 22 | |
| 23 | #define SUNXI_WDOG0_CTRL_REG (SUNXI_WDOG_BASE + 0x0010) |
| 24 | #define SUNXI_WDOG0_CFG_REG (SUNXI_WDOG_BASE + 0x0014) |
| 25 | #define SUNXI_WDOG0_MODE_REG (SUNXI_WDOG_BASE + 0x0018) |
| 26 | |
Samuel Holland | 0a9018c | 2017-08-12 04:07:39 -0500 | [diff] [blame] | 27 | #define mpidr_is_valid(mpidr) ( \ |
| 28 | MPIDR_AFFLVL3_VAL(mpidr) == 0 && \ |
| 29 | MPIDR_AFFLVL2_VAL(mpidr) == 0 && \ |
| 30 | MPIDR_AFFLVL1_VAL(mpidr) < PLATFORM_CLUSTER_COUNT && \ |
| 31 | MPIDR_AFFLVL0_VAL(mpidr) < PLATFORM_MAX_CPUS_PER_CLUSTER) |
| 32 | |
| 33 | static int sunxi_pwr_domain_on(u_register_t mpidr) |
| 34 | { |
| 35 | if (mpidr_is_valid(mpidr) == 0) |
| 36 | return PSCI_E_INTERN_FAIL; |
| 37 | |
Samuel Holland | c629daf | 2019-02-17 15:33:33 -0600 | [diff] [blame] | 38 | sunxi_cpu_on(mpidr); |
Samuel Holland | 0a9018c | 2017-08-12 04:07:39 -0500 | [diff] [blame] | 39 | |
| 40 | return PSCI_E_SUCCESS; |
| 41 | } |
| 42 | |
| 43 | static void sunxi_pwr_domain_off(const psci_power_state_t *target_state) |
| 44 | { |
| 45 | gicv2_cpuif_disable(); |
| 46 | } |
| 47 | |
Andre Przywara | 6d0b81b | 2018-09-28 00:43:32 +0100 | [diff] [blame] | 48 | static void __dead2 sunxi_pwr_down_wfi(const psci_power_state_t *target_state) |
| 49 | { |
Samuel Holland | c629daf | 2019-02-17 15:33:33 -0600 | [diff] [blame] | 50 | sunxi_cpu_off(read_mpidr()); |
Andre Przywara | 6d0b81b | 2018-09-28 00:43:32 +0100 | [diff] [blame] | 51 | |
| 52 | while (1) |
| 53 | wfi(); |
| 54 | } |
| 55 | |
Samuel Holland | 0a9018c | 2017-08-12 04:07:39 -0500 | [diff] [blame] | 56 | static void sunxi_pwr_domain_on_finish(const psci_power_state_t *target_state) |
| 57 | { |
| 58 | gicv2_pcpu_distif_init(); |
| 59 | gicv2_cpuif_enable(); |
| 60 | } |
| 61 | |
Samuel Holland | b856664 | 2017-08-12 04:07:39 -0500 | [diff] [blame] | 62 | static void __dead2 sunxi_system_off(void) |
| 63 | { |
Samuel Holland | 321c0ab | 2017-08-12 04:07:39 -0500 | [diff] [blame] | 64 | /* Turn off all secondary CPUs */ |
Samuel Holland | c629daf | 2019-02-17 15:33:33 -0600 | [diff] [blame] | 65 | sunxi_disable_secondary_cpus(read_mpidr()); |
Samuel Holland | 321c0ab | 2017-08-12 04:07:39 -0500 | [diff] [blame] | 66 | |
Icenowy Zheng | bd57eb5 | 2018-07-22 21:52:50 +0800 | [diff] [blame] | 67 | sunxi_power_down(); |
Samuel Holland | b856664 | 2017-08-12 04:07:39 -0500 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | static void __dead2 sunxi_system_reset(void) |
| 71 | { |
| 72 | /* Reset the whole system when the watchdog times out */ |
| 73 | mmio_write_32(SUNXI_WDOG0_CFG_REG, 1); |
| 74 | /* Enable the watchdog with the shortest timeout (0.5 seconds) */ |
| 75 | mmio_write_32(SUNXI_WDOG0_MODE_REG, (0 << 4) | 1); |
| 76 | /* Wait for twice the watchdog timeout before panicking */ |
| 77 | mdelay(1000); |
| 78 | |
| 79 | ERROR("PSCI: System reset failed\n"); |
| 80 | wfi(); |
| 81 | panic(); |
| 82 | } |
| 83 | |
Samuel Holland | 0a9018c | 2017-08-12 04:07:39 -0500 | [diff] [blame] | 84 | static int sunxi_validate_ns_entrypoint(uintptr_t ns_entrypoint) |
| 85 | { |
| 86 | /* The non-secure entry point must be in DRAM */ |
Andre Przywara | 9f3cb8c | 2018-06-22 00:48:15 +0100 | [diff] [blame] | 87 | if (ns_entrypoint >= SUNXI_DRAM_BASE) |
Samuel Holland | 0a9018c | 2017-08-12 04:07:39 -0500 | [diff] [blame] | 88 | return PSCI_E_SUCCESS; |
| 89 | |
| 90 | return PSCI_E_INVALID_ADDRESS; |
| 91 | } |
| 92 | |
Samuel Holland | b856664 | 2017-08-12 04:07:39 -0500 | [diff] [blame] | 93 | static plat_psci_ops_t sunxi_psci_ops = { |
Samuel Holland | 0a9018c | 2017-08-12 04:07:39 -0500 | [diff] [blame] | 94 | .pwr_domain_on = sunxi_pwr_domain_on, |
| 95 | .pwr_domain_off = sunxi_pwr_domain_off, |
Andre Przywara | 6d0b81b | 2018-09-28 00:43:32 +0100 | [diff] [blame] | 96 | .pwr_domain_pwr_down_wfi = sunxi_pwr_down_wfi, |
Samuel Holland | 0a9018c | 2017-08-12 04:07:39 -0500 | [diff] [blame] | 97 | .pwr_domain_on_finish = sunxi_pwr_domain_on_finish, |
Samuel Holland | b856664 | 2017-08-12 04:07:39 -0500 | [diff] [blame] | 98 | .system_off = sunxi_system_off, |
| 99 | .system_reset = sunxi_system_reset, |
Samuel Holland | 0a9018c | 2017-08-12 04:07:39 -0500 | [diff] [blame] | 100 | .validate_ns_entrypoint = sunxi_validate_ns_entrypoint, |
Samuel Holland | b856664 | 2017-08-12 04:07:39 -0500 | [diff] [blame] | 101 | }; |
| 102 | |
| 103 | int plat_setup_psci_ops(uintptr_t sec_entrypoint, |
| 104 | const plat_psci_ops_t **psci_ops) |
| 105 | { |
| 106 | assert(psci_ops); |
| 107 | |
Samuel Holland | 0a9018c | 2017-08-12 04:07:39 -0500 | [diff] [blame] | 108 | for (int cpu = 0; cpu < PLATFORM_CORE_COUNT; cpu += 1) { |
| 109 | mmio_write_32(SUNXI_CPUCFG_RVBAR_LO_REG(cpu), |
| 110 | sec_entrypoint & 0xffffffff); |
| 111 | mmio_write_32(SUNXI_CPUCFG_RVBAR_HI_REG(cpu), |
| 112 | sec_entrypoint >> 32); |
| 113 | } |
| 114 | |
Samuel Holland | b856664 | 2017-08-12 04:07:39 -0500 | [diff] [blame] | 115 | *psci_ops = &sunxi_psci_ops; |
| 116 | |
| 117 | return 0; |
| 118 | } |