pm.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. /*
  2. * Copyright (c) 2014, Fuzhou Rockchip Electronics Co., Ltd
  3. * Author: Tony Xie <tony.xie@rock-chips.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms and conditions of the GNU General Public License,
  7. * version 2, as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. *
  14. */
  15. #include <linux/init.h>
  16. #include <linux/io.h>
  17. #include <linux/kernel.h>
  18. #include <linux/of.h>
  19. #include <linux/of_address.h>
  20. #include <linux/regmap.h>
  21. #include <linux/suspend.h>
  22. #include <linux/mfd/syscon.h>
  23. #include <linux/regulator/machine.h>
  24. #include <asm/cacheflush.h>
  25. #include <asm/tlbflush.h>
  26. #include <asm/suspend.h>
  27. #include "pm.h"
  28. /* These enum are option of low power mode */
  29. enum {
  30. ROCKCHIP_ARM_OFF_LOGIC_NORMAL = 0,
  31. ROCKCHIP_ARM_OFF_LOGIC_DEEP = 1,
  32. };
  33. struct rockchip_pm_data {
  34. const struct platform_suspend_ops *ops;
  35. int (*init)(struct device_node *np);
  36. };
  37. static void __iomem *rk3288_bootram_base;
  38. static phys_addr_t rk3288_bootram_phy;
  39. static struct regmap *pmu_regmap;
  40. static struct regmap *sgrf_regmap;
  41. static struct regmap *grf_regmap;
  42. static u32 rk3288_pmu_pwr_mode_con;
  43. static u32 rk3288_sgrf_soc_con0;
  44. static u32 rk3288_sgrf_cpu_con0;
  45. static inline u32 rk3288_l2_config(void)
  46. {
  47. u32 l2ctlr;
  48. asm("mrc p15, 1, %0, c9, c0, 2" : "=r" (l2ctlr));
  49. return l2ctlr;
  50. }
  51. static void rk3288_config_bootdata(void)
  52. {
  53. rkpm_bootdata_cpusp = rk3288_bootram_phy + (SZ_4K - 8);
  54. rkpm_bootdata_cpu_code = virt_to_phys(cpu_resume);
  55. rkpm_bootdata_l2ctlr_f = 1;
  56. rkpm_bootdata_l2ctlr = rk3288_l2_config();
  57. }
  58. #define GRF_UOC0_CON0 0x320
  59. #define GRF_UOC1_CON0 0x334
  60. #define GRF_UOC2_CON0 0x348
  61. #define GRF_SIDDQ BIT(13)
  62. static bool rk3288_slp_disable_osc(void)
  63. {
  64. static const u32 reg_offset[] = { GRF_UOC0_CON0, GRF_UOC1_CON0,
  65. GRF_UOC2_CON0 };
  66. u32 reg, i;
  67. /*
  68. * if any usb phy is still on(GRF_SIDDQ==0), that means we need the
  69. * function of usb wakeup, so do not switch to 32khz, since the usb phy
  70. * clk does not connect to 32khz osc
  71. */
  72. for (i = 0; i < ARRAY_SIZE(reg_offset); i++) {
  73. regmap_read(grf_regmap, reg_offset[i], &reg);
  74. if (!(reg & GRF_SIDDQ))
  75. return false;
  76. }
  77. return true;
  78. }
  79. static void rk3288_slp_mode_set(int level)
  80. {
  81. u32 mode_set, mode_set1;
  82. bool osc_disable = rk3288_slp_disable_osc();
  83. regmap_read(sgrf_regmap, RK3288_SGRF_CPU_CON0, &rk3288_sgrf_cpu_con0);
  84. regmap_read(sgrf_regmap, RK3288_SGRF_SOC_CON0, &rk3288_sgrf_soc_con0);
  85. regmap_read(pmu_regmap, RK3288_PMU_PWRMODE_CON,
  86. &rk3288_pmu_pwr_mode_con);
  87. /*
  88. * SGRF_FAST_BOOT_EN - system to boot from FAST_BOOT_ADDR
  89. * PCLK_WDT_GATE - disable WDT during suspend.
  90. */
  91. regmap_write(sgrf_regmap, RK3288_SGRF_SOC_CON0,
  92. SGRF_PCLK_WDT_GATE | SGRF_FAST_BOOT_EN
  93. | SGRF_PCLK_WDT_GATE_WRITE | SGRF_FAST_BOOT_EN_WRITE);
  94. /*
  95. * The dapswjdp can not auto reset before resume, that cause it may
  96. * access some illegal address during resume. Let's disable it before
  97. * suspend, and the MASKROM will enable it back.
  98. */
  99. regmap_write(sgrf_regmap, RK3288_SGRF_CPU_CON0, SGRF_DAPDEVICEEN_WRITE);
  100. /* booting address of resuming system is from this register value */
  101. regmap_write(sgrf_regmap, RK3288_SGRF_FAST_BOOT_ADDR,
  102. rk3288_bootram_phy);
  103. mode_set = BIT(PMU_GLOBAL_INT_DISABLE) | BIT(PMU_L2FLUSH_EN) |
  104. BIT(PMU_SREF0_ENTER_EN) | BIT(PMU_SREF1_ENTER_EN) |
  105. BIT(PMU_DDR0_GATING_EN) | BIT(PMU_DDR1_GATING_EN) |
  106. BIT(PMU_PWR_MODE_EN) | BIT(PMU_CHIP_PD_EN) |
  107. BIT(PMU_SCU_EN);
  108. mode_set1 = BIT(PMU_CLR_CORE) | BIT(PMU_CLR_CPUP);
  109. if (level == ROCKCHIP_ARM_OFF_LOGIC_DEEP) {
  110. /* arm off, logic deep sleep */
  111. mode_set |= BIT(PMU_BUS_PD_EN) | BIT(PMU_PMU_USE_LF) |
  112. BIT(PMU_DDR1IO_RET_EN) | BIT(PMU_DDR0IO_RET_EN) |
  113. BIT(PMU_ALIVE_USE_LF) | BIT(PMU_PLL_PD_EN);
  114. if (osc_disable)
  115. mode_set |= BIT(PMU_OSC_24M_DIS);
  116. mode_set1 |= BIT(PMU_CLR_ALIVE) | BIT(PMU_CLR_BUS) |
  117. BIT(PMU_CLR_PERI) | BIT(PMU_CLR_DMA);
  118. regmap_write(pmu_regmap, RK3288_PMU_WAKEUP_CFG1,
  119. PMU_ARMINT_WAKEUP_EN);
  120. /*
  121. * In deep suspend we use PMU_PMU_USE_LF to let the rk3288
  122. * switch its main clock supply to the alternative 32kHz
  123. * source. Therefore set 30ms on a 32kHz clock for pmic
  124. * stabilization. Similar 30ms on 24MHz for the other
  125. * mode below.
  126. */
  127. regmap_write(pmu_regmap, RK3288_PMU_STABL_CNT, 32 * 30);
  128. /* only wait for stabilization, if we turned the osc off */
  129. regmap_write(pmu_regmap, RK3288_PMU_OSC_CNT,
  130. osc_disable ? 32 * 30 : 0);
  131. } else {
  132. /*
  133. * arm off, logic normal
  134. * if pmu_clk_core_src_gate_en is not set,
  135. * wakeup will be error
  136. */
  137. mode_set |= BIT(PMU_CLK_CORE_SRC_GATE_EN);
  138. regmap_write(pmu_regmap, RK3288_PMU_WAKEUP_CFG1,
  139. PMU_ARMINT_WAKEUP_EN | PMU_GPIOINT_WAKEUP_EN);
  140. /* 30ms on a 24MHz clock for pmic stabilization */
  141. regmap_write(pmu_regmap, RK3288_PMU_STABL_CNT, 24000 * 30);
  142. /* oscillator is still running, so no need to wait */
  143. regmap_write(pmu_regmap, RK3288_PMU_OSC_CNT, 0);
  144. }
  145. regmap_write(pmu_regmap, RK3288_PMU_PWRMODE_CON, mode_set);
  146. regmap_write(pmu_regmap, RK3288_PMU_PWRMODE_CON1, mode_set1);
  147. }
  148. static void rk3288_slp_mode_set_resume(void)
  149. {
  150. regmap_write(sgrf_regmap, RK3288_SGRF_CPU_CON0,
  151. rk3288_sgrf_cpu_con0 | SGRF_DAPDEVICEEN_WRITE);
  152. regmap_write(pmu_regmap, RK3288_PMU_PWRMODE_CON,
  153. rk3288_pmu_pwr_mode_con);
  154. regmap_write(sgrf_regmap, RK3288_SGRF_SOC_CON0,
  155. rk3288_sgrf_soc_con0 | SGRF_PCLK_WDT_GATE_WRITE
  156. | SGRF_FAST_BOOT_EN_WRITE);
  157. }
  158. static int rockchip_lpmode_enter(unsigned long arg)
  159. {
  160. flush_cache_all();
  161. cpu_do_idle();
  162. pr_err("%s: Failed to suspend\n", __func__);
  163. return 1;
  164. }
  165. static int rk3288_suspend_enter(suspend_state_t state)
  166. {
  167. local_fiq_disable();
  168. rk3288_slp_mode_set(ROCKCHIP_ARM_OFF_LOGIC_NORMAL);
  169. cpu_suspend(0, rockchip_lpmode_enter);
  170. rk3288_slp_mode_set_resume();
  171. local_fiq_enable();
  172. return 0;
  173. }
  174. static int rk3288_suspend_prepare(void)
  175. {
  176. return regulator_suspend_prepare(PM_SUSPEND_MEM);
  177. }
  178. static void rk3288_suspend_finish(void)
  179. {
  180. if (regulator_suspend_finish())
  181. pr_err("%s: Suspend finish failed\n", __func__);
  182. }
  183. static int rk3288_suspend_init(struct device_node *np)
  184. {
  185. struct device_node *sram_np;
  186. struct resource res;
  187. int ret;
  188. pmu_regmap = syscon_node_to_regmap(np);
  189. if (IS_ERR(pmu_regmap)) {
  190. pr_err("%s: could not find pmu regmap\n", __func__);
  191. return PTR_ERR(pmu_regmap);
  192. }
  193. sgrf_regmap = syscon_regmap_lookup_by_compatible(
  194. "rockchip,rk3288-sgrf");
  195. if (IS_ERR(sgrf_regmap)) {
  196. pr_err("%s: could not find sgrf regmap\n", __func__);
  197. return PTR_ERR(sgrf_regmap);
  198. }
  199. grf_regmap = syscon_regmap_lookup_by_compatible(
  200. "rockchip,rk3288-grf");
  201. if (IS_ERR(grf_regmap)) {
  202. pr_err("%s: could not find grf regmap\n", __func__);
  203. return PTR_ERR(grf_regmap);
  204. }
  205. sram_np = of_find_compatible_node(NULL, NULL,
  206. "rockchip,rk3288-pmu-sram");
  207. if (!sram_np) {
  208. pr_err("%s: could not find bootram dt node\n", __func__);
  209. return -ENODEV;
  210. }
  211. rk3288_bootram_base = of_iomap(sram_np, 0);
  212. if (!rk3288_bootram_base) {
  213. pr_err("%s: could not map bootram base\n", __func__);
  214. return -ENOMEM;
  215. }
  216. ret = of_address_to_resource(sram_np, 0, &res);
  217. if (ret) {
  218. pr_err("%s: could not get bootram phy addr\n", __func__);
  219. return ret;
  220. }
  221. rk3288_bootram_phy = res.start;
  222. of_node_put(sram_np);
  223. rk3288_config_bootdata();
  224. /* copy resume code and data to bootsram */
  225. memcpy(rk3288_bootram_base, rockchip_slp_cpu_resume,
  226. rk3288_bootram_sz);
  227. return 0;
  228. }
  229. static const struct platform_suspend_ops rk3288_suspend_ops = {
  230. .enter = rk3288_suspend_enter,
  231. .valid = suspend_valid_only_mem,
  232. .prepare = rk3288_suspend_prepare,
  233. .finish = rk3288_suspend_finish,
  234. };
  235. static const struct rockchip_pm_data rk3288_pm_data __initconst = {
  236. .ops = &rk3288_suspend_ops,
  237. .init = rk3288_suspend_init,
  238. };
  239. static const struct of_device_id rockchip_pmu_of_device_ids[] __initconst = {
  240. {
  241. .compatible = "rockchip,rk3288-pmu",
  242. .data = &rk3288_pm_data,
  243. },
  244. { /* sentinel */ },
  245. };
  246. void __init rockchip_suspend_init(void)
  247. {
  248. const struct rockchip_pm_data *pm_data;
  249. const struct of_device_id *match;
  250. struct device_node *np;
  251. int ret;
  252. np = of_find_matching_node_and_match(NULL, rockchip_pmu_of_device_ids,
  253. &match);
  254. if (!match) {
  255. pr_err("Failed to find PMU node\n");
  256. return;
  257. }
  258. pm_data = (struct rockchip_pm_data *) match->data;
  259. if (pm_data->init) {
  260. ret = pm_data->init(np);
  261. if (ret) {
  262. pr_err("%s: matches init error %d\n", __func__, ret);
  263. return;
  264. }
  265. }
  266. suspend_set_ops(pm_data->ops);
  267. }