spm.c 9.5 KB

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