Sumit Garg | 754073f | 2018-06-15 15:29:02 +0530 | [diff] [blame] | 1 | /* |
Louis Mayencourt | 1c819c3 | 2020-01-24 13:30:28 +0000 | [diff] [blame] | 2 | * Copyright (c) 2018-2020, ARM Limited and Contributors. All rights reserved. |
Sumit Garg | 754073f | 2018-06-15 15:29:02 +0530 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | */ |
| 6 | |
Sumit Garg | 754073f | 2018-06-15 15:29:02 +0530 | [diff] [blame] | 7 | #include <assert.h> |
Sumit Garg | 754073f | 2018-06-15 15:29:02 +0530 | [diff] [blame] | 8 | #include <errno.h> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 9 | |
Sumit Garg | 754073f | 2018-06-15 15:29:02 +0530 | [diff] [blame] | 10 | #include <platform_def.h> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 11 | |
| 12 | #include <arch_helpers.h> |
| 13 | #include <common/debug.h> |
| 14 | #include <drivers/delay_timer.h> |
| 15 | #include <drivers/generic_delay_timer.h> |
| 16 | #include <lib/cassert.h> |
| 17 | #include <lib/psci/psci.h> |
| 18 | |
Sumit Garg | 754073f | 2018-06-15 15:29:02 +0530 | [diff] [blame] | 19 | #include <sq_common.h> |
| 20 | #include "sq_scpi.h" |
Sumit Garg | 754073f | 2018-06-15 15:29:02 +0530 | [diff] [blame] | 21 | |
Sumit Garg | 754073f | 2018-06-15 15:29:02 +0530 | [diff] [blame] | 22 | uintptr_t sq_sec_entrypoint; |
| 23 | |
| 24 | int sq_pwr_domain_on(u_register_t mpidr) |
| 25 | { |
Masahisa Kojima | ebfd8eb | 2019-03-07 10:41:54 +0900 | [diff] [blame] | 26 | #if SQ_USE_SCMI_DRIVER |
| 27 | sq_scmi_on(mpidr); |
| 28 | #else |
Sumit Garg | 754073f | 2018-06-15 15:29:02 +0530 | [diff] [blame] | 29 | /* |
| 30 | * SCP takes care of powering up parent power domains so we |
| 31 | * only need to care about level 0 |
| 32 | */ |
| 33 | scpi_set_sq_power_state(mpidr, scpi_power_on, scpi_power_on, |
| 34 | scpi_power_on); |
Masahisa Kojima | ebfd8eb | 2019-03-07 10:41:54 +0900 | [diff] [blame] | 35 | #endif |
Sumit Garg | 754073f | 2018-06-15 15:29:02 +0530 | [diff] [blame] | 36 | |
| 37 | return PSCI_E_SUCCESS; |
| 38 | } |
| 39 | |
| 40 | static void sq_pwr_domain_on_finisher_common( |
| 41 | const psci_power_state_t *target_state) |
| 42 | { |
| 43 | assert(SQ_CORE_PWR_STATE(target_state) == SQ_LOCAL_STATE_OFF); |
| 44 | |
| 45 | /* |
| 46 | * Perform the common cluster specific operations i.e enable coherency |
| 47 | * if this cluster was off. |
| 48 | */ |
| 49 | if (SQ_CLUSTER_PWR_STATE(target_state) == SQ_LOCAL_STATE_OFF) |
| 50 | plat_sq_interconnect_enter_coherency(); |
| 51 | } |
| 52 | |
| 53 | void sq_pwr_domain_on_finish(const psci_power_state_t *target_state) |
| 54 | { |
| 55 | /* Assert that the system power domain need not be initialized */ |
| 56 | assert(SQ_SYSTEM_PWR_STATE(target_state) == SQ_LOCAL_STATE_RUN); |
| 57 | |
| 58 | sq_pwr_domain_on_finisher_common(target_state); |
| 59 | |
| 60 | /* Program the gic per-cpu distributor or re-distributor interface */ |
| 61 | sq_gic_pcpu_init(); |
| 62 | |
| 63 | /* Enable the gic cpu interface */ |
| 64 | sq_gic_cpuif_enable(); |
| 65 | } |
| 66 | |
Masahisa Kojima | ebfd8eb | 2019-03-07 10:41:54 +0900 | [diff] [blame] | 67 | #if !SQ_USE_SCMI_DRIVER |
Sumit Garg | 754073f | 2018-06-15 15:29:02 +0530 | [diff] [blame] | 68 | static void sq_power_down_common(const psci_power_state_t *target_state) |
| 69 | { |
| 70 | uint32_t cluster_state = scpi_power_on; |
| 71 | uint32_t system_state = scpi_power_on; |
| 72 | |
| 73 | /* Prevent interrupts from spuriously waking up this cpu */ |
| 74 | sq_gic_cpuif_disable(); |
| 75 | |
| 76 | /* Check if power down at system power domain level is requested */ |
| 77 | if (SQ_SYSTEM_PWR_STATE(target_state) == SQ_LOCAL_STATE_OFF) |
| 78 | system_state = scpi_power_retention; |
| 79 | |
| 80 | /* Cluster is to be turned off, so disable coherency */ |
| 81 | if (SQ_CLUSTER_PWR_STATE(target_state) == SQ_LOCAL_STATE_OFF) { |
| 82 | plat_sq_interconnect_exit_coherency(); |
| 83 | cluster_state = scpi_power_off; |
| 84 | } |
| 85 | |
| 86 | /* |
| 87 | * Ask the SCP to power down the appropriate components depending upon |
| 88 | * their state. |
| 89 | */ |
| 90 | scpi_set_sq_power_state(read_mpidr_el1(), |
| 91 | scpi_power_off, |
| 92 | cluster_state, |
| 93 | system_state); |
| 94 | } |
Masahisa Kojima | ebfd8eb | 2019-03-07 10:41:54 +0900 | [diff] [blame] | 95 | #endif |
Sumit Garg | 754073f | 2018-06-15 15:29:02 +0530 | [diff] [blame] | 96 | |
| 97 | void sq_pwr_domain_off(const psci_power_state_t *target_state) |
| 98 | { |
Masahisa Kojima | ebfd8eb | 2019-03-07 10:41:54 +0900 | [diff] [blame] | 99 | #if SQ_USE_SCMI_DRIVER |
Masahisa Kojima | 804b6b5 | 2021-08-03 16:48:53 +0900 | [diff] [blame] | 100 | /* Prevent interrupts from spuriously waking up this cpu */ |
| 101 | sq_gic_cpuif_disable(); |
| 102 | |
| 103 | /* Cluster is to be turned off, so disable coherency */ |
| 104 | if (SQ_CLUSTER_PWR_STATE(target_state) == SQ_LOCAL_STATE_OFF) { |
| 105 | plat_sq_interconnect_exit_coherency(); |
| 106 | } |
| 107 | |
Masahisa Kojima | ebfd8eb | 2019-03-07 10:41:54 +0900 | [diff] [blame] | 108 | sq_scmi_off(target_state); |
| 109 | #else |
Sumit Garg | 754073f | 2018-06-15 15:29:02 +0530 | [diff] [blame] | 110 | sq_power_down_common(target_state); |
Masahisa Kojima | ebfd8eb | 2019-03-07 10:41:54 +0900 | [diff] [blame] | 111 | #endif |
Sumit Garg | 754073f | 2018-06-15 15:29:02 +0530 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | void __dead2 sq_system_off(void) |
| 115 | { |
| 116 | volatile uint32_t *gpio = (uint32_t *)PLAT_SQ_GPIO_BASE; |
| 117 | |
| 118 | /* set PD[9] high to power off the system */ |
| 119 | gpio[5] |= 0x2; /* set output */ |
| 120 | gpio[1] |= 0x2; /* set high */ |
| 121 | dmbst(); |
| 122 | |
| 123 | generic_delay_timer_init(); |
| 124 | |
| 125 | mdelay(1); |
| 126 | |
| 127 | while (1) { |
| 128 | gpio[1] &= ~0x2; /* set low */ |
| 129 | dmbst(); |
| 130 | |
| 131 | mdelay(1); |
| 132 | |
| 133 | gpio[1] |= 0x2; /* set high */ |
| 134 | dmbst(); |
| 135 | |
| 136 | mdelay(100); |
| 137 | } |
| 138 | |
| 139 | wfi(); |
| 140 | ERROR("SQ System Off: operation not handled.\n"); |
| 141 | panic(); |
| 142 | } |
| 143 | |
| 144 | void __dead2 sq_system_reset(void) |
| 145 | { |
Masahisa Kojima | ebfd8eb | 2019-03-07 10:41:54 +0900 | [diff] [blame] | 146 | #if SQ_USE_SCMI_DRIVER |
| 147 | sq_scmi_sys_reboot(); |
| 148 | #else |
Sumit Garg | 754073f | 2018-06-15 15:29:02 +0530 | [diff] [blame] | 149 | uint32_t response; |
| 150 | |
| 151 | /* Send the system reset request to the SCP */ |
| 152 | response = scpi_sys_power_state(scpi_system_reboot); |
| 153 | |
| 154 | if (response != SCP_OK) { |
| 155 | ERROR("SQ System Reset: SCP error %u.\n", response); |
| 156 | panic(); |
| 157 | } |
| 158 | wfi(); |
| 159 | ERROR("SQ System Reset: operation not handled.\n"); |
| 160 | panic(); |
Masahisa Kojima | ebfd8eb | 2019-03-07 10:41:54 +0900 | [diff] [blame] | 161 | #endif |
Sumit Garg | 754073f | 2018-06-15 15:29:02 +0530 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | void sq_cpu_standby(plat_local_state_t cpu_state) |
| 165 | { |
Louis Mayencourt | 1c819c3 | 2020-01-24 13:30:28 +0000 | [diff] [blame] | 166 | u_register_t scr; |
Sumit Garg | 754073f | 2018-06-15 15:29:02 +0530 | [diff] [blame] | 167 | |
| 168 | assert(cpu_state == SQ_LOCAL_STATE_RET); |
| 169 | |
| 170 | scr = read_scr_el3(); |
| 171 | /* Enable PhysicalIRQ bit for NS world to wake the CPU */ |
| 172 | write_scr_el3(scr | SCR_IRQ_BIT); |
| 173 | isb(); |
| 174 | dsb(); |
| 175 | wfi(); |
| 176 | |
| 177 | /* |
| 178 | * Restore SCR to the original value, synchronisation of scr_el3 is |
| 179 | * done by eret while el3_exit to save some execution cycles. |
| 180 | */ |
| 181 | write_scr_el3(scr); |
| 182 | } |
| 183 | |
| 184 | const plat_psci_ops_t sq_psci_ops = { |
| 185 | .pwr_domain_on = sq_pwr_domain_on, |
| 186 | .pwr_domain_off = sq_pwr_domain_off, |
| 187 | .pwr_domain_on_finish = sq_pwr_domain_on_finish, |
| 188 | .cpu_standby = sq_cpu_standby, |
| 189 | .system_off = sq_system_off, |
| 190 | .system_reset = sq_system_reset, |
| 191 | }; |
| 192 | |
| 193 | int plat_setup_psci_ops(uintptr_t sec_entrypoint, |
| 194 | const struct plat_psci_ops **psci_ops) |
| 195 | { |
| 196 | sq_sec_entrypoint = sec_entrypoint; |
| 197 | flush_dcache_range((uint64_t)&sq_sec_entrypoint, |
| 198 | sizeof(sq_sec_entrypoint)); |
| 199 | |
| 200 | *psci_ops = &sq_psci_ops; |
| 201 | |
| 202 | return 0; |
| 203 | } |