spm.c 9.6 KB

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