Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Peng Fan | d8c75bc | 2017-08-17 17:48:50 +0800 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2015-2016 Freescale Semiconductor, Inc. |
| 4 | * Copyright 2017 NXP |
Peng Fan | d8c75bc | 2017-08-17 17:48:50 +0800 | [diff] [blame] | 5 | */ |
| 6 | |
Peng Fan | fcd53ce | 2015-10-23 10:13:04 +0800 | [diff] [blame] | 7 | #include <asm/io.h> |
| 8 | #include <asm/psci.h> |
Chen-Yu Tsai | 7ca1450 | 2016-06-19 12:38:41 +0800 | [diff] [blame] | 9 | #include <asm/secure.h> |
Peng Fan | fcd53ce | 2015-10-23 10:13:04 +0800 | [diff] [blame] | 10 | #include <asm/arch/imx-regs.h> |
Stefan Agner | 158a102 | 2018-06-24 21:09:56 +0200 | [diff] [blame^] | 11 | #include <linux/bitops.h> |
Peng Fan | fcd53ce | 2015-10-23 10:13:04 +0800 | [diff] [blame] | 12 | #include <common.h> |
Anson Huang | f20bc13 | 2018-01-07 14:34:31 +0800 | [diff] [blame] | 13 | #include <fsl_wdog.h> |
Peng Fan | fcd53ce | 2015-10-23 10:13:04 +0800 | [diff] [blame] | 14 | |
| 15 | #define GPC_CPU_PGC_SW_PDN_REQ 0xfc |
| 16 | #define GPC_CPU_PGC_SW_PUP_REQ 0xf0 |
| 17 | #define GPC_PGC_C1 0x840 |
| 18 | |
| 19 | #define BM_CPU_PGC_SW_PDN_PUP_REQ_CORE1_A7 0x2 |
| 20 | |
| 21 | /* below is for i.MX7D */ |
| 22 | #define SRC_GPR1_MX7D 0x074 |
| 23 | #define SRC_A7RCR0 0x004 |
| 24 | #define SRC_A7RCR1 0x008 |
| 25 | |
| 26 | #define BP_SRC_A7RCR0_A7_CORE_RESET0 0 |
| 27 | #define BP_SRC_A7RCR1_A7_CORE1_ENABLE 1 |
| 28 | |
Anson Huang | f46fb18 | 2018-01-07 14:34:32 +0800 | [diff] [blame] | 29 | #define SNVS_LPCR 0x38 |
| 30 | #define BP_SNVS_LPCR_DP_EN 0x20 |
| 31 | #define BP_SNVS_LPCR_TOP 0x40 |
| 32 | |
| 33 | #define CCM_CCGR_SNVS 0x4250 |
| 34 | |
Anson Huang | f20bc13 | 2018-01-07 14:34:31 +0800 | [diff] [blame] | 35 | #define CCM_ROOT_WDOG 0xbb80 |
| 36 | #define CCM_CCGR_WDOG1 0x49c0 |
| 37 | |
Stefan Agner | 158a102 | 2018-06-24 21:09:56 +0200 | [diff] [blame^] | 38 | #define MPIDR_AFF0 GENMASK(7, 0) |
| 39 | |
| 40 | #define IMX7D_PSCI_NR_CPUS 2 |
| 41 | #if IMX7D_PSCI_NR_CPUS > CONFIG_ARMV7_PSCI_NR_CPUS |
| 42 | #error "invalid value for CONFIG_ARMV7_PSCI_NR_CPUS" |
| 43 | #endif |
| 44 | |
| 45 | u8 psci_state[IMX7D_PSCI_NR_CPUS] __secure_data = { |
| 46 | PSCI_AFFINITY_LEVEL_ON, |
| 47 | PSCI_AFFINITY_LEVEL_OFF}; |
| 48 | |
| 49 | static inline void psci_set_state(int cpu, u8 state) |
| 50 | { |
| 51 | psci_state[cpu] = state; |
| 52 | dsb(); |
| 53 | isb(); |
| 54 | } |
| 55 | |
Peng Fan | fcd53ce | 2015-10-23 10:13:04 +0800 | [diff] [blame] | 56 | static inline void imx_gpcv2_set_m_core_pgc(bool enable, u32 offset) |
| 57 | { |
| 58 | writel(enable, GPC_IPS_BASE_ADDR + offset); |
| 59 | } |
| 60 | |
| 61 | __secure void imx_gpcv2_set_core1_power(bool pdn) |
| 62 | { |
| 63 | u32 reg = pdn ? GPC_CPU_PGC_SW_PUP_REQ : GPC_CPU_PGC_SW_PDN_REQ; |
| 64 | u32 val; |
| 65 | |
| 66 | imx_gpcv2_set_m_core_pgc(true, GPC_PGC_C1); |
| 67 | |
| 68 | val = readl(GPC_IPS_BASE_ADDR + reg); |
| 69 | val |= BM_CPU_PGC_SW_PDN_PUP_REQ_CORE1_A7; |
| 70 | writel(val, GPC_IPS_BASE_ADDR + reg); |
| 71 | |
| 72 | while ((readl(GPC_IPS_BASE_ADDR + reg) & |
| 73 | BM_CPU_PGC_SW_PDN_PUP_REQ_CORE1_A7) != 0) |
| 74 | ; |
| 75 | |
| 76 | imx_gpcv2_set_m_core_pgc(false, GPC_PGC_C1); |
| 77 | } |
| 78 | |
| 79 | __secure void imx_enable_cpu_ca7(int cpu, bool enable) |
| 80 | { |
| 81 | u32 mask, val; |
| 82 | |
| 83 | mask = 1 << (BP_SRC_A7RCR1_A7_CORE1_ENABLE + cpu - 1); |
| 84 | val = readl(SRC_BASE_ADDR + SRC_A7RCR1); |
| 85 | val = enable ? val | mask : val & ~mask; |
| 86 | writel(val, SRC_BASE_ADDR + SRC_A7RCR1); |
| 87 | } |
| 88 | |
Stefan Agner | 158a102 | 2018-06-24 21:09:56 +0200 | [diff] [blame^] | 89 | __secure void psci_arch_cpu_entry(void) |
| 90 | { |
| 91 | u32 cpu = psci_get_cpu_id(); |
| 92 | |
| 93 | psci_set_state(cpu, PSCI_AFFINITY_LEVEL_ON); |
| 94 | } |
| 95 | |
Stefan Agner | 4bc4f13 | 2018-06-24 21:09:55 +0200 | [diff] [blame] | 96 | __secure s32 psci_cpu_on(u32 __always_unused function_id, u32 mpidr, u32 ep, |
| 97 | u32 context_id) |
Peng Fan | fcd53ce | 2015-10-23 10:13:04 +0800 | [diff] [blame] | 98 | { |
Stefan Agner | 158a102 | 2018-06-24 21:09:56 +0200 | [diff] [blame^] | 99 | u32 cpu = mpidr & MPIDR_AFF0; |
| 100 | |
| 101 | if (mpidr & ~MPIDR_AFF0) |
| 102 | return ARM_PSCI_RET_INVAL; |
| 103 | |
| 104 | if (cpu >= IMX7D_PSCI_NR_CPUS) |
| 105 | return ARM_PSCI_RET_INVAL; |
| 106 | |
| 107 | if (psci_state[cpu] == PSCI_AFFINITY_LEVEL_ON) |
| 108 | return ARM_PSCI_RET_ALREADY_ON; |
| 109 | |
| 110 | if (psci_state[cpu] == PSCI_AFFINITY_LEVEL_ON_PENDING) |
| 111 | return ARM_PSCI_RET_ON_PENDING; |
Stefan Agner | 4bc4f13 | 2018-06-24 21:09:55 +0200 | [diff] [blame] | 112 | |
| 113 | psci_save(cpu, ep, context_id); |
| 114 | |
| 115 | writel((u32)psci_cpu_entry, SRC_BASE_ADDR + cpu * 8 + SRC_GPR1_MX7D); |
Stefan Agner | 158a102 | 2018-06-24 21:09:56 +0200 | [diff] [blame^] | 116 | |
| 117 | psci_set_state(cpu, PSCI_AFFINITY_LEVEL_ON_PENDING); |
| 118 | |
Peng Fan | fcd53ce | 2015-10-23 10:13:04 +0800 | [diff] [blame] | 119 | imx_gpcv2_set_core1_power(true); |
| 120 | imx_enable_cpu_ca7(cpu, true); |
Stefan Agner | 158a102 | 2018-06-24 21:09:56 +0200 | [diff] [blame^] | 121 | |
| 122 | return ARM_PSCI_RET_SUCCESS; |
Peng Fan | fcd53ce | 2015-10-23 10:13:04 +0800 | [diff] [blame] | 123 | } |
| 124 | |
Stefan Agner | 4bc4f13 | 2018-06-24 21:09:55 +0200 | [diff] [blame] | 125 | __secure s32 psci_cpu_off(void) |
Peng Fan | fcd53ce | 2015-10-23 10:13:04 +0800 | [diff] [blame] | 126 | { |
Stefan Agner | 4bc4f13 | 2018-06-24 21:09:55 +0200 | [diff] [blame] | 127 | int cpu; |
| 128 | |
Stefan Agner | 4bc4f13 | 2018-06-24 21:09:55 +0200 | [diff] [blame] | 129 | cpu = psci_get_cpu_id(); |
Stefan Agner | 158a102 | 2018-06-24 21:09:56 +0200 | [diff] [blame^] | 130 | |
| 131 | psci_cpu_off_common(); |
| 132 | psci_set_state(cpu, PSCI_AFFINITY_LEVEL_OFF); |
| 133 | |
Peng Fan | fcd53ce | 2015-10-23 10:13:04 +0800 | [diff] [blame] | 134 | imx_enable_cpu_ca7(cpu, false); |
| 135 | imx_gpcv2_set_core1_power(false); |
| 136 | writel(0, SRC_BASE_ADDR + cpu * 8 + SRC_GPR1_MX7D + 4); |
Stefan Agner | 4bc4f13 | 2018-06-24 21:09:55 +0200 | [diff] [blame] | 137 | |
| 138 | while (1) |
| 139 | wfi(); |
Peng Fan | fcd53ce | 2015-10-23 10:13:04 +0800 | [diff] [blame] | 140 | } |
Anson Huang | f20bc13 | 2018-01-07 14:34:31 +0800 | [diff] [blame] | 141 | |
Stefan Agner | 4bc4f13 | 2018-06-24 21:09:55 +0200 | [diff] [blame] | 142 | __secure void psci_system_reset(void) |
Anson Huang | f20bc13 | 2018-01-07 14:34:31 +0800 | [diff] [blame] | 143 | { |
| 144 | struct wdog_regs *wdog = (struct wdog_regs *)WDOG1_BASE_ADDR; |
| 145 | |
| 146 | /* make sure WDOG1 clock is enabled */ |
| 147 | writel(0x1 << 28, CCM_BASE_ADDR + CCM_ROOT_WDOG); |
| 148 | writel(0x3, CCM_BASE_ADDR + CCM_CCGR_WDOG1); |
| 149 | writew(WCR_WDE, &wdog->wcr); |
Stefan Agner | 4bc4f13 | 2018-06-24 21:09:55 +0200 | [diff] [blame] | 150 | |
| 151 | while (1) |
| 152 | wfi(); |
Anson Huang | f20bc13 | 2018-01-07 14:34:31 +0800 | [diff] [blame] | 153 | } |
Anson Huang | f46fb18 | 2018-01-07 14:34:32 +0800 | [diff] [blame] | 154 | |
Stefan Agner | 4bc4f13 | 2018-06-24 21:09:55 +0200 | [diff] [blame] | 155 | __secure void psci_system_off(void) |
Anson Huang | f46fb18 | 2018-01-07 14:34:32 +0800 | [diff] [blame] | 156 | { |
| 157 | u32 val; |
| 158 | |
| 159 | /* make sure SNVS clock is enabled */ |
| 160 | writel(0x3, CCM_BASE_ADDR + CCM_CCGR_SNVS); |
| 161 | |
| 162 | val = readl(SNVS_BASE_ADDR + SNVS_LPCR); |
| 163 | val |= BP_SNVS_LPCR_DP_EN | BP_SNVS_LPCR_TOP; |
| 164 | writel(val, SNVS_BASE_ADDR + SNVS_LPCR); |
Stefan Agner | 4bc4f13 | 2018-06-24 21:09:55 +0200 | [diff] [blame] | 165 | |
| 166 | while (1) |
| 167 | wfi(); |
Anson Huang | f46fb18 | 2018-01-07 14:34:32 +0800 | [diff] [blame] | 168 | } |
Stefan Agner | 158a102 | 2018-06-24 21:09:56 +0200 | [diff] [blame^] | 169 | |
| 170 | __secure u32 psci_version(void) |
| 171 | { |
| 172 | return ARM_PSCI_VER_1_0; |
| 173 | } |
| 174 | |
| 175 | __secure s32 psci_cpu_suspend(u32 __always_unused function_id, u32 power_state, |
| 176 | u32 entry_point_address, |
| 177 | u32 context_id) |
| 178 | { |
| 179 | return ARM_PSCI_RET_INVAL; |
| 180 | } |
| 181 | |
| 182 | __secure s32 psci_affinity_info(u32 __always_unused function_id, |
| 183 | u32 target_affinity, |
| 184 | u32 lowest_affinity_level) |
| 185 | { |
| 186 | u32 cpu = target_affinity & MPIDR_AFF0; |
| 187 | |
| 188 | if (lowest_affinity_level > 0) |
| 189 | return ARM_PSCI_RET_INVAL; |
| 190 | |
| 191 | if (target_affinity & ~MPIDR_AFF0) |
| 192 | return ARM_PSCI_RET_INVAL; |
| 193 | |
| 194 | if (cpu >= IMX7D_PSCI_NR_CPUS) |
| 195 | return ARM_PSCI_RET_INVAL; |
| 196 | |
| 197 | return psci_state[cpu]; |
| 198 | } |
| 199 | |
| 200 | __secure s32 psci_features(u32 __always_unused function_id, u32 psci_fid) |
| 201 | { |
| 202 | switch (psci_fid) { |
| 203 | case ARM_PSCI_0_2_FN_PSCI_VERSION: |
| 204 | case ARM_PSCI_0_2_FN_CPU_OFF: |
| 205 | case ARM_PSCI_0_2_FN_CPU_ON: |
| 206 | case ARM_PSCI_0_2_FN_AFFINITY_INFO: |
| 207 | case ARM_PSCI_0_2_FN_SYSTEM_OFF: |
| 208 | case ARM_PSCI_0_2_FN_SYSTEM_RESET: |
| 209 | case ARM_PSCI_1_0_FN_PSCI_FEATURES: |
| 210 | return 0x0; |
| 211 | } |
| 212 | return ARM_PSCI_RET_NI; |
| 213 | } |