Antonio Nino Diaz | ae6779e | 2017-11-06 14:49:04 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2015-2017, ARM Limited and Contributors. All rights reserved. |
| 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | */ |
| 6 | |
| 7 | #include <arch_helpers.h> |
| 8 | #include <assert.h> |
| 9 | #include <console.h> |
| 10 | #include <debug.h> |
| 11 | #include <mmio.h> |
| 12 | #include <platform_def.h> |
| 13 | #include <platform.h> |
| 14 | #include <psci.h> |
| 15 | |
| 16 | #include "rpi3_hw.h" |
| 17 | |
| 18 | /* |
| 19 | * The secure entry point to be used on warm reset. |
| 20 | */ |
| 21 | static uintptr_t secure_entrypoint; |
| 22 | |
| 23 | /* Make composite power state parameter till power level 0 */ |
| 24 | #if PSCI_EXTENDED_STATE_ID |
| 25 | |
| 26 | #define rpi3_make_pwrstate_lvl0(lvl0_state, pwr_lvl, type) \ |
| 27 | (((lvl0_state) << PSTATE_ID_SHIFT) | \ |
| 28 | ((type) << PSTATE_TYPE_SHIFT)) |
| 29 | |
| 30 | #else |
| 31 | |
| 32 | #define rpi3_make_pwrstate_lvl0(lvl0_state, pwr_lvl, type) \ |
| 33 | (((lvl0_state) << PSTATE_ID_SHIFT) | \ |
| 34 | ((pwr_lvl) << PSTATE_PWR_LVL_SHIFT) | \ |
| 35 | ((type) << PSTATE_TYPE_SHIFT)) |
| 36 | |
| 37 | #endif /* PSCI_EXTENDED_STATE_ID */ |
| 38 | |
| 39 | #define rpi3_make_pwrstate_lvl1(lvl1_state, lvl0_state, pwr_lvl, type) \ |
| 40 | (((lvl1_state) << PLAT_LOCAL_PSTATE_WIDTH) | \ |
| 41 | rpi3_make_pwrstate_lvl0(lvl0_state, pwr_lvl, type)) |
| 42 | |
| 43 | /* |
| 44 | * The table storing the valid idle power states. Ensure that the |
| 45 | * array entries are populated in ascending order of state-id to |
| 46 | * enable us to use binary search during power state validation. |
| 47 | * The table must be terminated by a NULL entry. |
| 48 | */ |
| 49 | static const unsigned int rpi3_pm_idle_states[] = { |
| 50 | /* State-id - 0x01 */ |
| 51 | rpi3_make_pwrstate_lvl1(PLAT_LOCAL_STATE_RUN, PLAT_LOCAL_STATE_RET, |
| 52 | MPIDR_AFFLVL0, PSTATE_TYPE_STANDBY), |
| 53 | /* State-id - 0x02 */ |
| 54 | rpi3_make_pwrstate_lvl1(PLAT_LOCAL_STATE_RUN, PLAT_LOCAL_STATE_OFF, |
| 55 | MPIDR_AFFLVL0, PSTATE_TYPE_POWERDOWN), |
| 56 | /* State-id - 0x22 */ |
| 57 | rpi3_make_pwrstate_lvl1(PLAT_LOCAL_STATE_OFF, PLAT_LOCAL_STATE_OFF, |
| 58 | MPIDR_AFFLVL1, PSTATE_TYPE_POWERDOWN), |
| 59 | 0, |
| 60 | }; |
| 61 | |
| 62 | /******************************************************************************* |
| 63 | * Platform handler called to check the validity of the power state |
| 64 | * parameter. The power state parameter has to be a composite power state. |
| 65 | ******************************************************************************/ |
| 66 | static int rpi3_validate_power_state(unsigned int power_state, |
| 67 | psci_power_state_t *req_state) |
| 68 | { |
| 69 | unsigned int state_id; |
| 70 | int i; |
| 71 | |
| 72 | assert(req_state != 0); |
| 73 | |
| 74 | /* |
| 75 | * Currently we are using a linear search for finding the matching |
| 76 | * entry in the idle power state array. This can be made a binary |
| 77 | * search if the number of entries justify the additional complexity. |
| 78 | */ |
| 79 | for (i = 0; rpi3_pm_idle_states[i] != 0; i++) { |
| 80 | if (power_state == rpi3_pm_idle_states[i]) { |
| 81 | break; |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | /* Return error if entry not found in the idle state array */ |
| 86 | if (!rpi3_pm_idle_states[i]) { |
| 87 | return PSCI_E_INVALID_PARAMS; |
| 88 | } |
| 89 | |
| 90 | i = 0; |
| 91 | state_id = psci_get_pstate_id(power_state); |
| 92 | |
| 93 | /* Parse the State ID and populate the state info parameter */ |
| 94 | while (state_id) { |
| 95 | req_state->pwr_domain_state[i++] = state_id & |
| 96 | PLAT_LOCAL_PSTATE_MASK; |
| 97 | state_id >>= PLAT_LOCAL_PSTATE_WIDTH; |
| 98 | } |
| 99 | |
| 100 | return PSCI_E_SUCCESS; |
| 101 | } |
| 102 | |
| 103 | /******************************************************************************* |
| 104 | * Platform handler called when a CPU is about to enter standby. |
| 105 | ******************************************************************************/ |
| 106 | static void rpi3_cpu_standby(plat_local_state_t cpu_state) |
| 107 | { |
| 108 | assert(cpu_state == PLAT_LOCAL_STATE_RET); |
| 109 | |
| 110 | /* |
| 111 | * Enter standby state. |
| 112 | * dsb is good practice before using wfi to enter low power states |
| 113 | */ |
| 114 | dsb(); |
| 115 | wfi(); |
| 116 | } |
| 117 | |
| 118 | /******************************************************************************* |
| 119 | * Platform handler called when a power domain is about to be turned on. The |
| 120 | * mpidr determines the CPU to be turned on. |
| 121 | ******************************************************************************/ |
| 122 | static int rpi3_pwr_domain_on(u_register_t mpidr) |
| 123 | { |
| 124 | int rc = PSCI_E_SUCCESS; |
| 125 | unsigned int pos = plat_core_pos_by_mpidr(mpidr); |
| 126 | uint64_t *hold_base = (uint64_t *)PLAT_RPI3_TM_HOLD_BASE; |
| 127 | |
| 128 | assert(pos < PLATFORM_CORE_COUNT); |
| 129 | |
| 130 | hold_base[pos] = PLAT_RPI3_TM_HOLD_STATE_GO; |
| 131 | |
| 132 | /* Make sure that the write has completed */ |
| 133 | dsb(); |
| 134 | isb(); |
| 135 | |
| 136 | sev(); |
| 137 | |
| 138 | return rc; |
| 139 | } |
| 140 | |
| 141 | /******************************************************************************* |
| 142 | * Platform handler called when a power domain has just been powered on after |
| 143 | * being turned off earlier. The target_state encodes the low power state that |
| 144 | * each level has woken up from. |
| 145 | ******************************************************************************/ |
| 146 | void rpi3_pwr_domain_on_finish(const psci_power_state_t *target_state) |
| 147 | { |
| 148 | assert(target_state->pwr_domain_state[MPIDR_AFFLVL0] == |
| 149 | PLAT_LOCAL_STATE_OFF); |
| 150 | } |
| 151 | |
| 152 | /******************************************************************************* |
| 153 | * Platform handler to reboot the system |
| 154 | ******************************************************************************/ |
| 155 | #define RESET_TIMEOUT 10 |
| 156 | |
| 157 | static void __dead2 rpi3_system_reset(void) |
| 158 | { |
| 159 | /* Setup watchdog for reset */ |
| 160 | |
| 161 | static const uintptr_t base = RPI3_PM_BASE; |
| 162 | uint32_t rstc; |
| 163 | |
| 164 | INFO("rpi3: PSCI System Reset: invoking watchdog reset\n"); |
| 165 | |
| 166 | console_flush(); |
| 167 | |
| 168 | rstc = mmio_read_32(base + RPI3_PM_RSTC_OFFSET); |
| 169 | rstc &= ~RPI3_PM_RSTC_WRCFG_MASK; |
| 170 | rstc |= RPI3_PM_RSTC_WRCFG_FULL_RESET; |
| 171 | |
| 172 | dmbst(); |
| 173 | |
| 174 | /* |
| 175 | * Watchdog timer = Timer clock / 16 |
| 176 | * Password (31:16) | Value (11:0) |
| 177 | */ |
| 178 | mmio_write_32(base + RPI3_PM_WDOG_OFFSET, |
| 179 | RPI3_PM_PASSWORD | RESET_TIMEOUT); |
| 180 | mmio_write_32(base + RPI3_PM_RSTC_OFFSET, |
| 181 | RPI3_PM_PASSWORD | rstc); |
| 182 | |
| 183 | for (;;) { |
| 184 | wfi(); |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | /******************************************************************************* |
| 189 | * Platform handlers and setup function. |
| 190 | ******************************************************************************/ |
| 191 | static const plat_psci_ops_t plat_rpi3_psci_pm_ops = { |
| 192 | .cpu_standby = rpi3_cpu_standby, |
| 193 | .pwr_domain_on = rpi3_pwr_domain_on, |
| 194 | .pwr_domain_on_finish = rpi3_pwr_domain_on_finish, |
| 195 | .system_reset = rpi3_system_reset, |
| 196 | .validate_power_state = rpi3_validate_power_state, |
| 197 | }; |
| 198 | |
| 199 | int plat_setup_psci_ops(uintptr_t sec_entrypoint, |
| 200 | const plat_psci_ops_t **psci_ops) |
| 201 | { |
| 202 | uintptr_t *mailbox = (void *)PLAT_RPI3_TRUSTED_MAILBOX_BASE; |
| 203 | |
| 204 | *mailbox = sec_entrypoint; |
| 205 | secure_entrypoint = (uintptr_t)sec_entrypoint; |
| 206 | *psci_ops = &plat_rpi3_psci_pm_ops; |
| 207 | |
| 208 | return 0; |
| 209 | } |