mt_sleep.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. #include <linux/init.h>
  2. #include <linux/module.h>
  3. #include <linux/kernel.h>
  4. #include <linux/spinlock.h>
  5. #include <linux/suspend.h>
  6. #include <linux/console.h>
  7. /* #include <linux/aee.h> */
  8. #include <linux/proc_fs.h>
  9. #include <linux/fs.h>
  10. #include <linux/vmalloc.h>
  11. #include <linux/uaccess.h>
  12. #include <mt-plat/sync_write.h>
  13. #include "mt_sleep.h"
  14. #include "mt_spm.h"
  15. #include "mt_spm_sleep.h"
  16. #include "mt_spm_idle.h"
  17. #include "mt_spm_mtcmos.h"
  18. #include "mt_clkmgr.h"
  19. /*
  20. #if defined(CONFIG_ARCH_MT6735M)
  21. #include <mt_soc_afe_control.h>
  22. #endif
  23. */
  24. /**************************************
  25. * only for internal debug
  26. **************************************/
  27. #ifdef CONFIG_MTK_LDVT
  28. #define SLP_SLEEP_DPIDLE_EN 1
  29. #define SLP_REPLACE_DEF_WAKESRC 1
  30. #define SLP_SUSPEND_LOG_EN 1
  31. #else
  32. /*
  33. #if defined(CONFIG_ARCH_MT6735M)
  34. #define SLP_SLEEP_DPIDLE_EN 1
  35. #else
  36. */
  37. #define SLP_SLEEP_DPIDLE_EN 0
  38. /*
  39. #endif
  40. */
  41. #define SLP_REPLACE_DEF_WAKESRC 0
  42. #define SLP_SUSPEND_LOG_EN 1
  43. #endif
  44. /**************************************
  45. * SW code for suspend
  46. **************************************/
  47. #define slp_read(addr) (*(volatile u32 *)(addr))
  48. #define slp_write(addr, val) mt65xx_reg_sync_writel(val, addr)
  49. /*
  50. #define slp_emerg(fmt, args...) pr_debug(KERN_EMERG "[SLP] " fmt, ##args)
  51. #define slp_alert(fmt, args...) pr_debug(KERN_ALERT "[SLP] " fmt, ##args)
  52. #define slp_crit(fmt, args...) pr_debug(KERN_CRIT "[SLP] " fmt, ##args)
  53. #define slp_crit2(fmt, args...) pr_debug(KERN_CRIT "[SLP] " fmt, ##args)
  54. #define slp_error(fmt, args...) pr_err(KERN_ERR "[SLP] " fmt, ##args)
  55. #define slp_warning(fmt, args...) pr_debug(KERN_WARNING "[SLP] " fmt, ##args)
  56. #define slp_notice(fmt, args...) pr_debug(KERN_NOTICE "[SLP] " fmt, ##args)
  57. #define slp_info(fmt, args...) pr_debug(KERN_INFO "[SLP] " fmt, ##args)
  58. #define slp_debug(fmt, args...) pr_debug(KERN_DEBUG "[SLP] " fmt, ##args)
  59. */
  60. #define slp_emerg(fmt, args...) pr_debug("[SLP] " fmt, ##args)
  61. #define slp_alert(fmt, args...) pr_debug("[SLP] " fmt, ##args)
  62. #define slp_crit(fmt, args...) pr_debug("[SLP] " fmt, ##args)
  63. #define slp_crit2(fmt, args...) pr_debug("[SLP] " fmt, ##args)
  64. #define slp_error(fmt, args...) pr_err("[SLP] " fmt, ##args)
  65. #define slp_warning(fmt, args...) pr_debug("[SLP] " fmt, ##args)
  66. #define slp_notice(fmt, args...) pr_debug("[SLP] " fmt, ##args)
  67. #define slp_info(fmt, args...) pr_debug("[SLP] " fmt, ##args)
  68. #define slp_debug(fmt, args...) pr_debug("[SLP] " fmt, ##args)
  69. static DEFINE_SPINLOCK(slp_lock);
  70. static wake_reason_t slp_wake_reason = WR_NONE;
  71. /* static bool slp_ck26m_on = 0; */
  72. /* static bool slp_dump_gpio = 0; */
  73. bool slp_ck26m_on = 0;
  74. static bool slp_pars_dpd = 1;
  75. static bool slp_chk_golden = 1;
  76. bool slp_dump_gpio = 0;
  77. static bool slp_dump_regs = 1;
  78. static bool slp_check_mtcmos_pll = 1;
  79. static u32 slp_spm_flags = {
  80. #if defined(CONFIG_ARCH_MT6735)
  81. 0
  82. #elif defined(CONFIG_ARCH_MT6735M)
  83. 0
  84. #elif defined(CONFIG_ARCH_MT6753)
  85. #ifdef SPM_LEGACY_SUSPEND
  86. SPM_CPU_PDN_DIS |
  87. SPM_INFRA_PDN_DIS |
  88. SPM_DDRPHY_PDN_DIS |
  89. SPM_PASR_DIS |
  90. SPM_CPU_DVS_DIS |
  91. SPM_DISABLE_ATF_ABORT
  92. #else
  93. 0
  94. #endif
  95. #else
  96. /* ERROR */
  97. #endif
  98. };
  99. #if SLP_SLEEP_DPIDLE_EN
  100. static u32 slp_spm_deepidle_flags = {
  101. 0
  102. };
  103. #endif
  104. /* static u32 slp_spm_data = 0; */
  105. u32 slp_spm_data = 0;
  106. #if 1
  107. static int slp_suspend_ops_valid(suspend_state_t state)
  108. {
  109. return state == PM_SUSPEND_MEM;
  110. }
  111. static int slp_suspend_ops_begin(suspend_state_t state)
  112. {
  113. /* legacy log */
  114. slp_notice("@@@Chip_pm_begin(%u)(%u)@@@\n", is_cpu_pdn(slp_spm_flags), is_infra_pdn(slp_spm_flags));
  115. slp_wake_reason = WR_NONE;
  116. return 0;
  117. }
  118. static int slp_suspend_ops_prepare(void)
  119. {
  120. /* legacy log */
  121. slp_crit2("@@@Chip_pm_prepare@@@\n");
  122. return 0;
  123. }
  124. #ifdef CONFIG_MTKPASR
  125. /* PASR/DPD Preliminary operations */
  126. /* extern void mtkpasr_phaseone_ops(void); */
  127. static int slp_suspend_ops_prepare_late(void)
  128. {
  129. slp_notice("[%s]\n", __func__);
  130. mtkpasr_phaseone_ops();
  131. return 0;
  132. }
  133. static void slp_suspend_ops_wake(void)
  134. {
  135. slp_notice("[%s]\n", __func__);
  136. }
  137. /* PASR/DPD SW operations */
  138. /*
  139. extern int configure_mrw_pasr(u32 segment_rank0, u32 segment_rank1);
  140. extern int pasr_enter(u32 *sr, u32 *dpd);
  141. extern int pasr_exit(void);
  142. extern unsigned long mtkpasr_enable_sr;
  143. extern void enter_pasr_dpd_config(unsigned char segment_rank0, unsigned char segment_rank1);
  144. extern void exit_pasr_dpd_config(void);
  145. */
  146. static int enter_pasrdpd(void)
  147. {
  148. int error = 0;
  149. u32 sr = 0, dpd = 0;
  150. slp_crit2("@@@[%s]@@@\n", __func__);
  151. /* Setup SPM wakeup event firstly */
  152. spm_set_wakeup_src_check();
  153. /* Start PASR/DPD SW operations */
  154. error = pasr_enter(&sr, &dpd);
  155. if (error) {
  156. slp_crit2("[PM_WAKEUP] Failed to enter PASR!\n");
  157. } else {
  158. /* Call SPM/DPD control API */
  159. slp_crit2("MR17[0x%x] DPD[0x%x]\n", sr, dpd);
  160. /* Should configure SR */
  161. if (mtkpasr_enable_sr == 0) {
  162. sr = 0x0;
  163. slp_crit2("[%s][%d] No configuration on SR\n", __func__, __LINE__);
  164. }
  165. /* Configure PASR */
  166. enter_pasr_dpd_config((sr & 0xFF), (sr >> 0x8));
  167. }
  168. slp_crit2("Bye [%s]\n", __func__);
  169. return error;
  170. }
  171. static void leave_pasrdpd(void)
  172. {
  173. slp_crit2("@@@[%s]@@@\n", __func__);
  174. /* Disable PASR */
  175. exit_pasr_dpd_config();
  176. slp_crit2("[%d]\n", __LINE__);
  177. /* End PASR/DPD SW operations */
  178. pasr_exit();
  179. slp_crit2("Bye [%s]\n", __func__);
  180. }
  181. #endif
  182. static int slp_suspend_ops_enter(suspend_state_t state)
  183. {
  184. int ret = 0;
  185. /*
  186. #if defined(CONFIG_ARCH_MT6735M)
  187. int fm_radio_is_playing = 0;
  188. if (ConditionEnterSuspend() == true)
  189. fm_radio_is_playing = 0;
  190. else
  191. fm_radio_is_playing = 1;
  192. #endif
  193. */
  194. #ifdef CONFIG_MTKPASR
  195. /* PASR SW operations */
  196. enter_pasrdpd();
  197. #endif
  198. /* legacy log */
  199. slp_crit2("@@@Chip_pm_enter@@@\n");
  200. #if 0
  201. if (slp_dump_gpio)
  202. gpio_dump_regs();
  203. #endif
  204. #if 0
  205. if (slp_dump_regs)
  206. slp_dump_pm_regs();
  207. #endif
  208. if (slp_check_mtcmos_pll)
  209. slp_check_pm_mtcmos_pll();
  210. if (!spm_cpusys0_can_power_down()) {
  211. slp_error("CANNOT SLEEP DUE TO CPU1/2/3 PON\n");
  212. ret = -EPERM;
  213. goto LEAVE_SLEEP;
  214. }
  215. if (is_infra_pdn(slp_spm_flags) && !is_cpu_pdn(slp_spm_flags)) {
  216. slp_error("CANNOT SLEEP DUE TO INFRA PDN BUT CPU PON\n");
  217. ret = -EPERM;
  218. goto LEAVE_SLEEP;
  219. }
  220. /*
  221. #if defined(CONFIG_ARCH_MT6735M)
  222. #if SLP_SLEEP_DPIDLE_EN
  223. if ((slp_ck26m_on) || (fm_radio_is_playing))
  224. slp_wake_reason = spm_go_to_sleep_dpidle(slp_spm_deepidle_flags, slp_spm_data);
  225. else
  226. #endif
  227. #endif
  228. */
  229. slp_wake_reason = spm_go_to_sleep(slp_spm_flags, slp_spm_data);
  230. LEAVE_SLEEP:
  231. #ifdef CONFIG_MTKPASR
  232. /* PASR SW operations */
  233. leave_pasrdpd();
  234. #endif
  235. return ret;
  236. }
  237. static void slp_suspend_ops_finish(void)
  238. {
  239. /* legacy log */
  240. slp_crit2("@@@Chip_pm_finish@@@\n");
  241. }
  242. static void slp_suspend_ops_end(void)
  243. {
  244. /* legacy log */
  245. slp_notice("@@@Chip_pm_end@@@\n");
  246. }
  247. static const struct platform_suspend_ops slp_suspend_ops = {
  248. .valid = slp_suspend_ops_valid,
  249. .begin = slp_suspend_ops_begin,
  250. .prepare = slp_suspend_ops_prepare,
  251. .enter = slp_suspend_ops_enter,
  252. .finish = slp_suspend_ops_finish,
  253. .end = slp_suspend_ops_end,
  254. #ifdef CONFIG_MTKPASR
  255. .prepare_late = slp_suspend_ops_prepare_late,
  256. .wake = slp_suspend_ops_wake,
  257. #endif
  258. };
  259. #endif
  260. __attribute__((weak)) int spm_set_dpidle_wakesrc(u32 wakesrc, bool enable, bool replace)
  261. {
  262. return 0;
  263. }
  264. /*
  265. * wakesrc : WAKE_SRC_XXX
  266. * enable : enable or disable @wakesrc
  267. * ck26m_on: if true, mean @wakesrc needs 26M to work
  268. */
  269. int slp_set_wakesrc(u32 wakesrc, bool enable, bool ck26m_on)
  270. {
  271. int r;
  272. unsigned long flags;
  273. slp_notice("wakesrc = 0x%x, enable = %u, ck26m_on = %u\n",
  274. wakesrc, enable, ck26m_on);
  275. #if SLP_REPLACE_DEF_WAKESRC
  276. if (wakesrc & WAKE_SRC_CFG_KEY)
  277. #else
  278. if (!(wakesrc & WAKE_SRC_CFG_KEY))
  279. #endif
  280. return -EPERM;
  281. spin_lock_irqsave(&slp_lock, flags);
  282. #if SLP_REPLACE_DEF_WAKESRC
  283. if (ck26m_on)
  284. r = spm_set_dpidle_wakesrc(wakesrc, enable, true);
  285. else
  286. r = spm_set_sleep_wakesrc(wakesrc, enable, true);
  287. #else
  288. if (ck26m_on)
  289. r = spm_set_dpidle_wakesrc(wakesrc & ~WAKE_SRC_CFG_KEY, enable, false);
  290. else
  291. r = spm_set_sleep_wakesrc(wakesrc & ~WAKE_SRC_CFG_KEY, enable, false);
  292. #endif
  293. if (!r)
  294. slp_ck26m_on = ck26m_on;
  295. spin_unlock_irqrestore(&slp_lock, flags);
  296. return r;
  297. }
  298. wake_reason_t slp_get_wake_reason(void)
  299. {
  300. return slp_wake_reason;
  301. }
  302. bool slp_will_infra_pdn(void)
  303. {
  304. return is_infra_pdn(slp_spm_flags);
  305. }
  306. /*
  307. * en: 1: enable pasr, 0: disable pasr
  308. * value: pasr setting (RK1, MR17 for RK0)
  309. */
  310. void slp_pasr_en(bool en, u32 value)
  311. {
  312. if (slp_pars_dpd) {
  313. if (en) {
  314. slp_spm_flags &= ~SPM_PASR_DIS;
  315. slp_spm_data = value;
  316. } else {
  317. slp_spm_flags |= SPM_PASR_DIS;
  318. slp_spm_data = 0;
  319. }
  320. }
  321. }
  322. void slp_module_init(void)
  323. {
  324. spm_output_sleep_option();
  325. slp_notice("SLEEP_DPIDLE_EN:%d, REPLACE_DEF_WAKESRC:%d, SUSPEND_LOG_EN:%d\n",
  326. SLP_SLEEP_DPIDLE_EN, SLP_REPLACE_DEF_WAKESRC, SLP_SUSPEND_LOG_EN);
  327. suspend_set_ops(&slp_suspend_ops);
  328. #if SLP_SUSPEND_LOG_EN
  329. console_suspend_enabled = 0;
  330. #endif
  331. }
  332. /*
  333. #ifdef CONFIG_MTK_FPGA
  334. static int __init spm_fpga_module_init(void)
  335. {
  336. spm_module_init();
  337. slp_module_init();
  338. return 0;
  339. }
  340. arch_initcall(spm_fpga_module_init);
  341. #else
  342. #endif
  343. */
  344. module_param(slp_ck26m_on, bool, 0644);
  345. module_param(slp_pars_dpd, bool, 0644);
  346. module_param(slp_spm_flags, uint, 0644);
  347. module_param(slp_chk_golden, bool, 0644);
  348. module_param(slp_dump_gpio, bool, 0644);
  349. module_param(slp_dump_regs, bool, 0644);
  350. module_param(slp_check_mtcmos_pll, bool, 0644);
  351. MODULE_DESCRIPTION("Sleep Driver v0.1");