spm.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2011-2014, The Linux Foundation. All rights reserved.
  4. * Copyright (c) 2014,2015, Linaro Ltd.
  5. *
  6. * SAW power controller driver
  7. */
  8. #include <linux/kernel.h>
  9. #include <linux/init.h>
  10. #include <linux/io.h>
  11. #include <linux/slab.h>
  12. #include <linux/of.h>
  13. #include <linux/of_address.h>
  14. #include <linux/of_device.h>
  15. #include <linux/err.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/cpuidle.h>
  18. #include <linux/cpu_pm.h>
  19. #include <linux/qcom_scm.h>
  20. #include <asm/cpuidle.h>
  21. #include <asm/proc-fns.h>
  22. #include <asm/suspend.h>
  23. #define MAX_PMIC_DATA 2
  24. #define MAX_SEQ_DATA 64
  25. #define SPM_CTL_INDEX 0x7f
  26. #define SPM_CTL_INDEX_SHIFT 4
  27. #define SPM_CTL_EN BIT(0)
  28. enum pm_sleep_mode {
  29. PM_SLEEP_MODE_STBY,
  30. PM_SLEEP_MODE_RET,
  31. PM_SLEEP_MODE_SPC,
  32. PM_SLEEP_MODE_PC,
  33. PM_SLEEP_MODE_NR,
  34. };
  35. enum spm_reg {
  36. SPM_REG_CFG,
  37. SPM_REG_SPM_CTL,
  38. SPM_REG_DLY,
  39. SPM_REG_PMIC_DLY,
  40. SPM_REG_PMIC_DATA_0,
  41. SPM_REG_PMIC_DATA_1,
  42. SPM_REG_VCTL,
  43. SPM_REG_SEQ_ENTRY,
  44. SPM_REG_SPM_STS,
  45. SPM_REG_PMIC_STS,
  46. SPM_REG_NR,
  47. };
  48. struct spm_reg_data {
  49. const u8 *reg_offset;
  50. u32 spm_cfg;
  51. u32 spm_dly;
  52. u32 pmic_dly;
  53. u32 pmic_data[MAX_PMIC_DATA];
  54. u8 seq[MAX_SEQ_DATA];
  55. u8 start_index[PM_SLEEP_MODE_NR];
  56. };
  57. struct spm_driver_data {
  58. void __iomem *reg_base;
  59. const struct spm_reg_data *reg_data;
  60. };
  61. static const u8 spm_reg_offset_v2_1[SPM_REG_NR] = {
  62. [SPM_REG_CFG] = 0x08,
  63. [SPM_REG_SPM_CTL] = 0x30,
  64. [SPM_REG_DLY] = 0x34,
  65. [SPM_REG_SEQ_ENTRY] = 0x80,
  66. };
  67. /* SPM register data for 8974, 8084 */
  68. static const struct spm_reg_data spm_reg_8974_8084_cpu = {
  69. .reg_offset = spm_reg_offset_v2_1,
  70. .spm_cfg = 0x1,
  71. .spm_dly = 0x3C102800,
  72. .seq = { 0x03, 0x0B, 0x0F, 0x00, 0x20, 0x80, 0x10, 0xE8, 0x5B, 0x03,
  73. 0x3B, 0xE8, 0x5B, 0x82, 0x10, 0x0B, 0x30, 0x06, 0x26, 0x30,
  74. 0x0F },
  75. .start_index[PM_SLEEP_MODE_STBY] = 0,
  76. .start_index[PM_SLEEP_MODE_SPC] = 3,
  77. };
  78. static const u8 spm_reg_offset_v1_1[SPM_REG_NR] = {
  79. [SPM_REG_CFG] = 0x08,
  80. [SPM_REG_SPM_CTL] = 0x20,
  81. [SPM_REG_PMIC_DLY] = 0x24,
  82. [SPM_REG_PMIC_DATA_0] = 0x28,
  83. [SPM_REG_PMIC_DATA_1] = 0x2C,
  84. [SPM_REG_SEQ_ENTRY] = 0x80,
  85. };
  86. /* SPM register data for 8064 */
  87. static const struct spm_reg_data spm_reg_8064_cpu = {
  88. .reg_offset = spm_reg_offset_v1_1,
  89. .spm_cfg = 0x1F,
  90. .pmic_dly = 0x02020004,
  91. .pmic_data[0] = 0x0084009C,
  92. .pmic_data[1] = 0x00A4001C,
  93. .seq = { 0x03, 0x0F, 0x00, 0x24, 0x54, 0x10, 0x09, 0x03, 0x01,
  94. 0x10, 0x54, 0x30, 0x0C, 0x24, 0x30, 0x0F },
  95. .start_index[PM_SLEEP_MODE_STBY] = 0,
  96. .start_index[PM_SLEEP_MODE_SPC] = 2,
  97. };
  98. static DEFINE_PER_CPU(struct spm_driver_data *, cpu_spm_drv);
  99. typedef int (*idle_fn)(void);
  100. static DEFINE_PER_CPU(idle_fn*, qcom_idle_ops);
  101. static inline void spm_register_write(struct spm_driver_data *drv,
  102. enum spm_reg reg, u32 val)
  103. {
  104. if (drv->reg_data->reg_offset[reg])
  105. writel_relaxed(val, drv->reg_base +
  106. drv->reg_data->reg_offset[reg]);
  107. }
  108. /* Ensure a guaranteed write, before return */
  109. static inline void spm_register_write_sync(struct spm_driver_data *drv,
  110. enum spm_reg reg, u32 val)
  111. {
  112. u32 ret;
  113. if (!drv->reg_data->reg_offset[reg])
  114. return;
  115. do {
  116. writel_relaxed(val, drv->reg_base +
  117. drv->reg_data->reg_offset[reg]);
  118. ret = readl_relaxed(drv->reg_base +
  119. drv->reg_data->reg_offset[reg]);
  120. if (ret == val)
  121. break;
  122. cpu_relax();
  123. } while (1);
  124. }
  125. static inline u32 spm_register_read(struct spm_driver_data *drv,
  126. enum spm_reg reg)
  127. {
  128. return readl_relaxed(drv->reg_base + drv->reg_data->reg_offset[reg]);
  129. }
  130. static void spm_set_low_power_mode(struct spm_driver_data *drv,
  131. enum pm_sleep_mode mode)
  132. {
  133. u32 start_index;
  134. u32 ctl_val;
  135. start_index = drv->reg_data->start_index[mode];
  136. ctl_val = spm_register_read(drv, SPM_REG_SPM_CTL);
  137. ctl_val &= ~(SPM_CTL_INDEX << SPM_CTL_INDEX_SHIFT);
  138. ctl_val |= start_index << SPM_CTL_INDEX_SHIFT;
  139. ctl_val |= SPM_CTL_EN;
  140. spm_register_write_sync(drv, SPM_REG_SPM_CTL, ctl_val);
  141. }
  142. static int qcom_pm_collapse(unsigned long int unused)
  143. {
  144. qcom_scm_cpu_power_down(QCOM_SCM_CPU_PWR_DOWN_L2_ON);
  145. /*
  146. * Returns here only if there was a pending interrupt and we did not
  147. * power down as a result.
  148. */
  149. return -1;
  150. }
  151. static int qcom_cpu_spc(void)
  152. {
  153. int ret;
  154. struct spm_driver_data *drv = __this_cpu_read(cpu_spm_drv);
  155. spm_set_low_power_mode(drv, PM_SLEEP_MODE_SPC);
  156. ret = cpu_suspend(0, qcom_pm_collapse);
  157. /*
  158. * ARM common code executes WFI without calling into our driver and
  159. * if the SPM mode is not reset, then we may accidently power down the
  160. * cpu when we intended only to gate the cpu clock.
  161. * Ensure the state is set to standby before returning.
  162. */
  163. spm_set_low_power_mode(drv, PM_SLEEP_MODE_STBY);
  164. return ret;
  165. }
  166. static int qcom_idle_enter(unsigned long index)
  167. {
  168. return __this_cpu_read(qcom_idle_ops)[index]();
  169. }
  170. static const struct of_device_id qcom_idle_state_match[] __initconst = {
  171. { .compatible = "qcom,idle-state-spc", .data = qcom_cpu_spc },
  172. { },
  173. };
  174. static int __init qcom_cpuidle_init(struct device_node *cpu_node, int cpu)
  175. {
  176. const struct of_device_id *match_id;
  177. struct device_node *state_node;
  178. int i;
  179. int state_count = 1;
  180. idle_fn idle_fns[CPUIDLE_STATE_MAX];
  181. idle_fn *fns;
  182. cpumask_t mask;
  183. bool use_scm_power_down = false;
  184. if (!qcom_scm_is_available())
  185. return -EPROBE_DEFER;
  186. for (i = 0; ; i++) {
  187. state_node = of_parse_phandle(cpu_node, "cpu-idle-states", i);
  188. if (!state_node)
  189. break;
  190. if (!of_device_is_available(state_node))
  191. continue;
  192. if (i == CPUIDLE_STATE_MAX) {
  193. pr_warn("%s: cpuidle states reached max possible\n",
  194. __func__);
  195. break;
  196. }
  197. match_id = of_match_node(qcom_idle_state_match, state_node);
  198. if (!match_id)
  199. return -ENODEV;
  200. idle_fns[state_count] = match_id->data;
  201. /* Check if any of the states allow power down */
  202. if (match_id->data == qcom_cpu_spc)
  203. use_scm_power_down = true;
  204. state_count++;
  205. }
  206. if (state_count == 1)
  207. goto check_spm;
  208. fns = devm_kcalloc(get_cpu_device(cpu), state_count, sizeof(*fns),
  209. GFP_KERNEL);
  210. if (!fns)
  211. return -ENOMEM;
  212. for (i = 1; i < state_count; i++)
  213. fns[i] = idle_fns[i];
  214. if (use_scm_power_down) {
  215. /* We have atleast one power down mode */
  216. cpumask_clear(&mask);
  217. cpumask_set_cpu(cpu, &mask);
  218. qcom_scm_set_warm_boot_addr(cpu_resume_arm, &mask);
  219. }
  220. per_cpu(qcom_idle_ops, cpu) = fns;
  221. /*
  222. * SPM probe for the cpu should have happened by now, if the
  223. * SPM device does not exist, return -ENXIO to indicate that the
  224. * cpu does not support idle states.
  225. */
  226. check_spm:
  227. return per_cpu(cpu_spm_drv, cpu) ? 0 : -ENXIO;
  228. }
  229. static const struct cpuidle_ops qcom_cpuidle_ops __initconst = {
  230. .suspend = qcom_idle_enter,
  231. .init = qcom_cpuidle_init,
  232. };
  233. CPUIDLE_METHOD_OF_DECLARE(qcom_idle_v1, "qcom,kpss-acc-v1", &qcom_cpuidle_ops);
  234. CPUIDLE_METHOD_OF_DECLARE(qcom_idle_v2, "qcom,kpss-acc-v2", &qcom_cpuidle_ops);
  235. static struct spm_driver_data *spm_get_drv(struct platform_device *pdev,
  236. int *spm_cpu)
  237. {
  238. struct spm_driver_data *drv = NULL;
  239. struct device_node *cpu_node, *saw_node;
  240. int cpu;
  241. bool found = 0;
  242. for_each_possible_cpu(cpu) {
  243. cpu_node = of_cpu_device_node_get(cpu);
  244. if (!cpu_node)
  245. continue;
  246. saw_node = of_parse_phandle(cpu_node, "qcom,saw", 0);
  247. found = (saw_node == pdev->dev.of_node);
  248. of_node_put(saw_node);
  249. of_node_put(cpu_node);
  250. if (found)
  251. break;
  252. }
  253. if (found) {
  254. drv = devm_kzalloc(&pdev->dev, sizeof(*drv), GFP_KERNEL);
  255. if (drv)
  256. *spm_cpu = cpu;
  257. }
  258. return drv;
  259. }
  260. static const struct of_device_id spm_match_table[] = {
  261. { .compatible = "qcom,msm8974-saw2-v2.1-cpu",
  262. .data = &spm_reg_8974_8084_cpu },
  263. { .compatible = "qcom,apq8084-saw2-v2.1-cpu",
  264. .data = &spm_reg_8974_8084_cpu },
  265. { .compatible = "qcom,apq8064-saw2-v1.1-cpu",
  266. .data = &spm_reg_8064_cpu },
  267. { },
  268. };
  269. static int spm_dev_probe(struct platform_device *pdev)
  270. {
  271. struct spm_driver_data *drv;
  272. struct resource *res;
  273. const struct of_device_id *match_id;
  274. void __iomem *addr;
  275. int cpu;
  276. drv = spm_get_drv(pdev, &cpu);
  277. if (!drv)
  278. return -EINVAL;
  279. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  280. drv->reg_base = devm_ioremap_resource(&pdev->dev, res);
  281. if (IS_ERR(drv->reg_base))
  282. return PTR_ERR(drv->reg_base);
  283. match_id = of_match_node(spm_match_table, pdev->dev.of_node);
  284. if (!match_id)
  285. return -ENODEV;
  286. drv->reg_data = match_id->data;
  287. /* Write the SPM sequences first.. */
  288. addr = drv->reg_base + drv->reg_data->reg_offset[SPM_REG_SEQ_ENTRY];
  289. __iowrite32_copy(addr, drv->reg_data->seq,
  290. ARRAY_SIZE(drv->reg_data->seq) / 4);
  291. /*
  292. * ..and then the control registers.
  293. * On some SoC if the control registers are written first and if the
  294. * CPU was held in reset, the reset signal could trigger the SPM state
  295. * machine, before the sequences are completely written.
  296. */
  297. spm_register_write(drv, SPM_REG_CFG, drv->reg_data->spm_cfg);
  298. spm_register_write(drv, SPM_REG_DLY, drv->reg_data->spm_dly);
  299. spm_register_write(drv, SPM_REG_PMIC_DLY, drv->reg_data->pmic_dly);
  300. spm_register_write(drv, SPM_REG_PMIC_DATA_0,
  301. drv->reg_data->pmic_data[0]);
  302. spm_register_write(drv, SPM_REG_PMIC_DATA_1,
  303. drv->reg_data->pmic_data[1]);
  304. /* Set up Standby as the default low power mode */
  305. spm_set_low_power_mode(drv, PM_SLEEP_MODE_STBY);
  306. per_cpu(cpu_spm_drv, cpu) = drv;
  307. return 0;
  308. }
  309. static struct platform_driver spm_driver = {
  310. .probe = spm_dev_probe,
  311. .driver = {
  312. .name = "saw",
  313. .of_match_table = spm_match_table,
  314. },
  315. };
  316. builtin_platform_driver(spm_driver);