1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2011-2014, The Linux Foundation. All rights reserved.
4 * Copyright (c) 2014,2015, Linaro Ltd.
6 * SAW power controller driver
9 #include <linux/kernel.h>
10 #include <linux/init.h>
12 #include <linux/slab.h>
14 #include <linux/of_address.h>
15 #include <linux/of_device.h>
16 #include <linux/err.h>
17 #include <linux/platform_device.h>
18 #include <linux/cpuidle.h>
19 #include <linux/cpu_pm.h>
20 #include <linux/qcom_scm.h>
22 #include <asm/proc-fns.h>
23 #include <asm/suspend.h>
25 #include "dt_idle_states.h"
27 #define MAX_PMIC_DATA 2
28 #define MAX_SEQ_DATA 64
29 #define SPM_CTL_INDEX 0x7f
30 #define SPM_CTL_INDEX_SHIFT 4
31 #define SPM_CTL_EN BIT(0)
60 u32 pmic_data[MAX_PMIC_DATA];
62 u8 start_index[PM_SLEEP_MODE_NR];
65 struct spm_driver_data {
66 struct cpuidle_driver cpuidle_driver;
67 void __iomem *reg_base;
68 const struct spm_reg_data *reg_data;
71 static const u8 spm_reg_offset_v2_1[SPM_REG_NR] = {
73 [SPM_REG_SPM_CTL] = 0x30,
75 [SPM_REG_SEQ_ENTRY] = 0x80,
78 /* SPM register data for 8974, 8084 */
79 static const struct spm_reg_data spm_reg_8974_8084_cpu = {
80 .reg_offset = spm_reg_offset_v2_1,
82 .spm_dly = 0x3C102800,
83 .seq = { 0x03, 0x0B, 0x0F, 0x00, 0x20, 0x80, 0x10, 0xE8, 0x5B, 0x03,
84 0x3B, 0xE8, 0x5B, 0x82, 0x10, 0x0B, 0x30, 0x06, 0x26, 0x30,
86 .start_index[PM_SLEEP_MODE_STBY] = 0,
87 .start_index[PM_SLEEP_MODE_SPC] = 3,
90 /* SPM register data for 8226 */
91 static const struct spm_reg_data spm_reg_8226_cpu = {
92 .reg_offset = spm_reg_offset_v2_1,
94 .spm_dly = 0x3C102800,
95 .seq = { 0x60, 0x03, 0x60, 0x0B, 0x0F, 0x20, 0x10, 0x80, 0x30, 0x90,
96 0x5B, 0x60, 0x03, 0x60, 0x3B, 0x76, 0x76, 0x0B, 0x94, 0x5B,
97 0x80, 0x10, 0x26, 0x30, 0x0F },
98 .start_index[PM_SLEEP_MODE_STBY] = 0,
99 .start_index[PM_SLEEP_MODE_SPC] = 5,
102 static const u8 spm_reg_offset_v1_1[SPM_REG_NR] = {
103 [SPM_REG_CFG] = 0x08,
104 [SPM_REG_SPM_CTL] = 0x20,
105 [SPM_REG_PMIC_DLY] = 0x24,
106 [SPM_REG_PMIC_DATA_0] = 0x28,
107 [SPM_REG_PMIC_DATA_1] = 0x2C,
108 [SPM_REG_SEQ_ENTRY] = 0x80,
111 /* SPM register data for 8064 */
112 static const struct spm_reg_data spm_reg_8064_cpu = {
113 .reg_offset = spm_reg_offset_v1_1,
115 .pmic_dly = 0x02020004,
116 .pmic_data[0] = 0x0084009C,
117 .pmic_data[1] = 0x00A4001C,
118 .seq = { 0x03, 0x0F, 0x00, 0x24, 0x54, 0x10, 0x09, 0x03, 0x01,
119 0x10, 0x54, 0x30, 0x0C, 0x24, 0x30, 0x0F },
120 .start_index[PM_SLEEP_MODE_STBY] = 0,
121 .start_index[PM_SLEEP_MODE_SPC] = 2,
124 static inline void spm_register_write(struct spm_driver_data *drv,
125 enum spm_reg reg, u32 val)
127 if (drv->reg_data->reg_offset[reg])
128 writel_relaxed(val, drv->reg_base +
129 drv->reg_data->reg_offset[reg]);
132 /* Ensure a guaranteed write, before return */
133 static inline void spm_register_write_sync(struct spm_driver_data *drv,
134 enum spm_reg reg, u32 val)
138 if (!drv->reg_data->reg_offset[reg])
142 writel_relaxed(val, drv->reg_base +
143 drv->reg_data->reg_offset[reg]);
144 ret = readl_relaxed(drv->reg_base +
145 drv->reg_data->reg_offset[reg]);
152 static inline u32 spm_register_read(struct spm_driver_data *drv,
155 return readl_relaxed(drv->reg_base + drv->reg_data->reg_offset[reg]);
158 static void spm_set_low_power_mode(struct spm_driver_data *drv,
159 enum pm_sleep_mode mode)
164 start_index = drv->reg_data->start_index[mode];
166 ctl_val = spm_register_read(drv, SPM_REG_SPM_CTL);
167 ctl_val &= ~(SPM_CTL_INDEX << SPM_CTL_INDEX_SHIFT);
168 ctl_val |= start_index << SPM_CTL_INDEX_SHIFT;
169 ctl_val |= SPM_CTL_EN;
170 spm_register_write_sync(drv, SPM_REG_SPM_CTL, ctl_val);
173 static int qcom_pm_collapse(unsigned long int unused)
175 qcom_scm_cpu_power_down(QCOM_SCM_CPU_PWR_DOWN_L2_ON);
178 * Returns here only if there was a pending interrupt and we did not
179 * power down as a result.
184 static int qcom_cpu_spc(struct spm_driver_data *drv)
188 spm_set_low_power_mode(drv, PM_SLEEP_MODE_SPC);
189 ret = cpu_suspend(0, qcom_pm_collapse);
191 * ARM common code executes WFI without calling into our driver and
192 * if the SPM mode is not reset, then we may accidently power down the
193 * cpu when we intended only to gate the cpu clock.
194 * Ensure the state is set to standby before returning.
196 spm_set_low_power_mode(drv, PM_SLEEP_MODE_STBY);
201 static int spm_enter_idle_state(struct cpuidle_device *dev,
202 struct cpuidle_driver *drv, int idx)
204 struct spm_driver_data *data = container_of(drv, struct spm_driver_data,
207 return CPU_PM_CPU_IDLE_ENTER_PARAM(qcom_cpu_spc, idx, data);
210 static struct cpuidle_driver qcom_spm_idle_driver = {
212 .owner = THIS_MODULE,
214 .enter = spm_enter_idle_state,
216 .target_residency = 1,
217 .power_usage = UINT_MAX,
223 static const struct of_device_id qcom_idle_state_match[] = {
224 { .compatible = "qcom,idle-state-spc", .data = spm_enter_idle_state },
228 static int spm_cpuidle_init(struct cpuidle_driver *drv, int cpu)
232 memcpy(drv, &qcom_spm_idle_driver, sizeof(*drv));
233 drv->cpumask = (struct cpumask *)cpumask_of(cpu);
235 /* Parse idle states from device tree */
236 ret = dt_init_idle_driver(drv, qcom_idle_state_match, 1);
238 return ret ? : -ENODEV;
240 /* We have atleast one power down mode */
241 return qcom_scm_set_warm_boot_addr(cpu_resume_arm, drv->cpumask);
244 static struct spm_driver_data *spm_get_drv(struct platform_device *pdev,
247 struct spm_driver_data *drv = NULL;
248 struct device_node *cpu_node, *saw_node;
252 for_each_possible_cpu(cpu) {
253 cpu_node = of_cpu_device_node_get(cpu);
256 saw_node = of_parse_phandle(cpu_node, "qcom,saw", 0);
257 found = (saw_node == pdev->dev.of_node);
258 of_node_put(saw_node);
259 of_node_put(cpu_node);
265 drv = devm_kzalloc(&pdev->dev, sizeof(*drv), GFP_KERNEL);
273 static const struct of_device_id spm_match_table[] = {
274 { .compatible = "qcom,msm8226-saw2-v2.1-cpu",
275 .data = &spm_reg_8226_cpu },
276 { .compatible = "qcom,msm8974-saw2-v2.1-cpu",
277 .data = &spm_reg_8974_8084_cpu },
278 { .compatible = "qcom,apq8084-saw2-v2.1-cpu",
279 .data = &spm_reg_8974_8084_cpu },
280 { .compatible = "qcom,apq8064-saw2-v1.1-cpu",
281 .data = &spm_reg_8064_cpu },
285 static int spm_dev_probe(struct platform_device *pdev)
287 struct spm_driver_data *drv;
288 struct resource *res;
289 const struct of_device_id *match_id;
293 if (!qcom_scm_is_available())
294 return -EPROBE_DEFER;
296 drv = spm_get_drv(pdev, &cpu);
299 platform_set_drvdata(pdev, drv);
301 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
302 drv->reg_base = devm_ioremap_resource(&pdev->dev, res);
303 if (IS_ERR(drv->reg_base))
304 return PTR_ERR(drv->reg_base);
306 match_id = of_match_node(spm_match_table, pdev->dev.of_node);
310 drv->reg_data = match_id->data;
312 ret = spm_cpuidle_init(&drv->cpuidle_driver, cpu);
316 /* Write the SPM sequences first.. */
317 addr = drv->reg_base + drv->reg_data->reg_offset[SPM_REG_SEQ_ENTRY];
318 __iowrite32_copy(addr, drv->reg_data->seq,
319 ARRAY_SIZE(drv->reg_data->seq) / 4);
322 * ..and then the control registers.
323 * On some SoC if the control registers are written first and if the
324 * CPU was held in reset, the reset signal could trigger the SPM state
325 * machine, before the sequences are completely written.
327 spm_register_write(drv, SPM_REG_CFG, drv->reg_data->spm_cfg);
328 spm_register_write(drv, SPM_REG_DLY, drv->reg_data->spm_dly);
329 spm_register_write(drv, SPM_REG_PMIC_DLY, drv->reg_data->pmic_dly);
330 spm_register_write(drv, SPM_REG_PMIC_DATA_0,
331 drv->reg_data->pmic_data[0]);
332 spm_register_write(drv, SPM_REG_PMIC_DATA_1,
333 drv->reg_data->pmic_data[1]);
335 /* Set up Standby as the default low power mode */
336 spm_set_low_power_mode(drv, PM_SLEEP_MODE_STBY);
338 return cpuidle_register(&drv->cpuidle_driver, NULL);
341 static int spm_dev_remove(struct platform_device *pdev)
343 struct spm_driver_data *drv = platform_get_drvdata(pdev);
345 cpuidle_unregister(&drv->cpuidle_driver);
349 static struct platform_driver spm_driver = {
350 .probe = spm_dev_probe,
351 .remove = spm_dev_remove,
354 .of_match_table = spm_match_table,
358 builtin_platform_driver(spm_driver);