pm33xx.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * AM33XX Power Management Routines
  4. *
  5. * Copyright (C) 2012-2018 Texas Instruments Incorporated - http://www.ti.com/
  6. * Vaibhav Bedia, Dave Gerlach
  7. */
  8. #include <linux/cpu.h>
  9. #include <linux/err.h>
  10. #include <linux/genalloc.h>
  11. #include <linux/kernel.h>
  12. #include <linux/init.h>
  13. #include <linux/io.h>
  14. #include <linux/module.h>
  15. #include <linux/of.h>
  16. #include <linux/platform_data/pm33xx.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/sizes.h>
  19. #include <linux/sram.h>
  20. #include <linux/suspend.h>
  21. #include <linux/ti-emif-sram.h>
  22. #include <linux/wkup_m3_ipc.h>
  23. #include <asm/proc-fns.h>
  24. #include <asm/suspend.h>
  25. #include <asm/system_misc.h>
  26. #define AMX3_PM_SRAM_SYMBOL_OFFSET(sym) ((unsigned long)(sym) - \
  27. (unsigned long)pm_sram->do_wfi)
  28. static int (*am33xx_do_wfi_sram)(unsigned long unused);
  29. static phys_addr_t am33xx_do_wfi_sram_phys;
  30. static struct gen_pool *sram_pool, *sram_pool_data;
  31. static unsigned long ocmcram_location, ocmcram_location_data;
  32. static struct am33xx_pm_platform_data *pm_ops;
  33. static struct am33xx_pm_sram_addr *pm_sram;
  34. static struct device *pm33xx_dev;
  35. static struct wkup_m3_ipc *m3_ipc;
  36. static unsigned long suspend_wfi_flags;
  37. static u32 sram_suspend_address(unsigned long addr)
  38. {
  39. return ((unsigned long)am33xx_do_wfi_sram +
  40. AMX3_PM_SRAM_SYMBOL_OFFSET(addr));
  41. }
  42. #ifdef CONFIG_SUSPEND
  43. static int am33xx_pm_suspend(suspend_state_t suspend_state)
  44. {
  45. int i, ret = 0;
  46. ret = pm_ops->soc_suspend((unsigned long)suspend_state,
  47. am33xx_do_wfi_sram, suspend_wfi_flags);
  48. if (ret) {
  49. dev_err(pm33xx_dev, "PM: Kernel suspend failure\n");
  50. } else {
  51. i = m3_ipc->ops->request_pm_status(m3_ipc);
  52. switch (i) {
  53. case 0:
  54. dev_info(pm33xx_dev,
  55. "PM: Successfully put all powerdomains to target state\n");
  56. break;
  57. case 1:
  58. dev_err(pm33xx_dev,
  59. "PM: Could not transition all powerdomains to target state\n");
  60. ret = -1;
  61. break;
  62. default:
  63. dev_err(pm33xx_dev,
  64. "PM: CM3 returned unknown result = %d\n", i);
  65. ret = -1;
  66. }
  67. }
  68. return ret;
  69. }
  70. static int am33xx_pm_enter(suspend_state_t suspend_state)
  71. {
  72. int ret = 0;
  73. switch (suspend_state) {
  74. case PM_SUSPEND_MEM:
  75. case PM_SUSPEND_STANDBY:
  76. ret = am33xx_pm_suspend(suspend_state);
  77. break;
  78. default:
  79. ret = -EINVAL;
  80. }
  81. return ret;
  82. }
  83. static int am33xx_pm_begin(suspend_state_t state)
  84. {
  85. int ret = -EINVAL;
  86. switch (state) {
  87. case PM_SUSPEND_MEM:
  88. ret = m3_ipc->ops->prepare_low_power(m3_ipc, WKUP_M3_DEEPSLEEP);
  89. break;
  90. case PM_SUSPEND_STANDBY:
  91. ret = m3_ipc->ops->prepare_low_power(m3_ipc, WKUP_M3_STANDBY);
  92. break;
  93. }
  94. return ret;
  95. }
  96. static void am33xx_pm_end(void)
  97. {
  98. m3_ipc->ops->finish_low_power(m3_ipc);
  99. }
  100. static int am33xx_pm_valid(suspend_state_t state)
  101. {
  102. switch (state) {
  103. case PM_SUSPEND_STANDBY:
  104. case PM_SUSPEND_MEM:
  105. return 1;
  106. default:
  107. return 0;
  108. }
  109. }
  110. static const struct platform_suspend_ops am33xx_pm_ops = {
  111. .begin = am33xx_pm_begin,
  112. .end = am33xx_pm_end,
  113. .enter = am33xx_pm_enter,
  114. .valid = am33xx_pm_valid,
  115. };
  116. #endif /* CONFIG_SUSPEND */
  117. static void am33xx_pm_set_ipc_ops(void)
  118. {
  119. u32 resume_address;
  120. int temp;
  121. temp = ti_emif_get_mem_type();
  122. if (temp < 0) {
  123. dev_err(pm33xx_dev, "PM: Cannot determine memory type, no PM available\n");
  124. return;
  125. }
  126. m3_ipc->ops->set_mem_type(m3_ipc, temp);
  127. /* Physical resume address to be used by ROM code */
  128. resume_address = am33xx_do_wfi_sram_phys +
  129. *pm_sram->resume_offset + 0x4;
  130. m3_ipc->ops->set_resume_address(m3_ipc, (void *)resume_address);
  131. }
  132. static void am33xx_pm_free_sram(void)
  133. {
  134. gen_pool_free(sram_pool, ocmcram_location, *pm_sram->do_wfi_sz);
  135. gen_pool_free(sram_pool_data, ocmcram_location_data,
  136. sizeof(struct am33xx_pm_ro_sram_data));
  137. }
  138. /*
  139. * Push the minimal suspend-resume code to SRAM
  140. */
  141. static int am33xx_pm_alloc_sram(void)
  142. {
  143. struct device_node *np;
  144. int ret = 0;
  145. np = of_find_compatible_node(NULL, NULL, "ti,omap3-mpu");
  146. if (!np) {
  147. np = of_find_compatible_node(NULL, NULL, "ti,omap4-mpu");
  148. if (!np) {
  149. dev_err(pm33xx_dev, "PM: %s: Unable to find device node for mpu\n",
  150. __func__);
  151. return -ENODEV;
  152. }
  153. }
  154. sram_pool = of_gen_pool_get(np, "pm-sram", 0);
  155. if (!sram_pool) {
  156. dev_err(pm33xx_dev, "PM: %s: Unable to get sram pool for ocmcram\n",
  157. __func__);
  158. ret = -ENODEV;
  159. goto mpu_put_node;
  160. }
  161. sram_pool_data = of_gen_pool_get(np, "pm-sram", 1);
  162. if (!sram_pool_data) {
  163. dev_err(pm33xx_dev, "PM: %s: Unable to get sram data pool for ocmcram\n",
  164. __func__);
  165. ret = -ENODEV;
  166. goto mpu_put_node;
  167. }
  168. ocmcram_location = gen_pool_alloc(sram_pool, *pm_sram->do_wfi_sz);
  169. if (!ocmcram_location) {
  170. dev_err(pm33xx_dev, "PM: %s: Unable to allocate memory from ocmcram\n",
  171. __func__);
  172. ret = -ENOMEM;
  173. goto mpu_put_node;
  174. }
  175. ocmcram_location_data = gen_pool_alloc(sram_pool_data,
  176. sizeof(struct emif_regs_amx3));
  177. if (!ocmcram_location_data) {
  178. dev_err(pm33xx_dev, "PM: Unable to allocate memory from ocmcram\n");
  179. gen_pool_free(sram_pool, ocmcram_location, *pm_sram->do_wfi_sz);
  180. ret = -ENOMEM;
  181. }
  182. mpu_put_node:
  183. of_node_put(np);
  184. return ret;
  185. }
  186. static int am33xx_push_sram_idle(void)
  187. {
  188. struct am33xx_pm_ro_sram_data ro_sram_data;
  189. int ret;
  190. u32 table_addr, ro_data_addr;
  191. void *copy_addr;
  192. ro_sram_data.amx3_pm_sram_data_virt = ocmcram_location_data;
  193. ro_sram_data.amx3_pm_sram_data_phys =
  194. gen_pool_virt_to_phys(sram_pool_data, ocmcram_location_data);
  195. ro_sram_data.rtc_base_virt = pm_ops->get_rtc_base_addr();
  196. /* Save physical address to calculate resume offset during pm init */
  197. am33xx_do_wfi_sram_phys = gen_pool_virt_to_phys(sram_pool,
  198. ocmcram_location);
  199. am33xx_do_wfi_sram = sram_exec_copy(sram_pool, (void *)ocmcram_location,
  200. pm_sram->do_wfi,
  201. *pm_sram->do_wfi_sz);
  202. if (!am33xx_do_wfi_sram) {
  203. dev_err(pm33xx_dev,
  204. "PM: %s: am33xx_do_wfi copy to sram failed\n",
  205. __func__);
  206. return -ENODEV;
  207. }
  208. table_addr =
  209. sram_suspend_address((unsigned long)pm_sram->emif_sram_table);
  210. ret = ti_emif_copy_pm_function_table(sram_pool, (void *)table_addr);
  211. if (ret) {
  212. dev_dbg(pm33xx_dev,
  213. "PM: %s: EMIF function copy failed\n", __func__);
  214. return -EPROBE_DEFER;
  215. }
  216. ro_data_addr =
  217. sram_suspend_address((unsigned long)pm_sram->ro_sram_data);
  218. copy_addr = sram_exec_copy(sram_pool, (void *)ro_data_addr,
  219. &ro_sram_data,
  220. sizeof(ro_sram_data));
  221. if (!copy_addr) {
  222. dev_err(pm33xx_dev,
  223. "PM: %s: ro_sram_data copy to sram failed\n",
  224. __func__);
  225. return -ENODEV;
  226. }
  227. return 0;
  228. }
  229. static int am33xx_pm_probe(struct platform_device *pdev)
  230. {
  231. struct device *dev = &pdev->dev;
  232. int ret;
  233. if (!of_machine_is_compatible("ti,am33xx") &&
  234. !of_machine_is_compatible("ti,am43"))
  235. return -ENODEV;
  236. pm_ops = dev->platform_data;
  237. if (!pm_ops) {
  238. dev_err(dev, "PM: Cannot get core PM ops!\n");
  239. return -ENODEV;
  240. }
  241. pm_sram = pm_ops->get_sram_addrs();
  242. if (!pm_sram) {
  243. dev_err(dev, "PM: Cannot get PM asm function addresses!!\n");
  244. return -ENODEV;
  245. }
  246. pm33xx_dev = dev;
  247. ret = am33xx_pm_alloc_sram();
  248. if (ret)
  249. return ret;
  250. ret = am33xx_push_sram_idle();
  251. if (ret)
  252. goto err_free_sram;
  253. m3_ipc = wkup_m3_ipc_get();
  254. if (!m3_ipc) {
  255. dev_dbg(dev, "PM: Cannot get wkup_m3_ipc handle\n");
  256. ret = -EPROBE_DEFER;
  257. goto err_free_sram;
  258. }
  259. am33xx_pm_set_ipc_ops();
  260. #ifdef CONFIG_SUSPEND
  261. suspend_set_ops(&am33xx_pm_ops);
  262. #endif /* CONFIG_SUSPEND */
  263. /*
  264. * For a system suspend we must flush the caches, we want
  265. * the DDR in self-refresh, we want to save the context
  266. * of the EMIF, and we want the wkup_m3 to handle low-power
  267. * transition.
  268. */
  269. suspend_wfi_flags |= WFI_FLAG_FLUSH_CACHE;
  270. suspend_wfi_flags |= WFI_FLAG_SELF_REFRESH;
  271. suspend_wfi_flags |= WFI_FLAG_SAVE_EMIF;
  272. suspend_wfi_flags |= WFI_FLAG_WAKE_M3;
  273. ret = pm_ops->init();
  274. if (ret) {
  275. dev_err(dev, "Unable to call core pm init!\n");
  276. ret = -ENODEV;
  277. goto err_put_wkup_m3_ipc;
  278. }
  279. return 0;
  280. err_put_wkup_m3_ipc:
  281. wkup_m3_ipc_put(m3_ipc);
  282. err_free_sram:
  283. am33xx_pm_free_sram();
  284. pm33xx_dev = NULL;
  285. return ret;
  286. }
  287. static int am33xx_pm_remove(struct platform_device *pdev)
  288. {
  289. suspend_set_ops(NULL);
  290. wkup_m3_ipc_put(m3_ipc);
  291. am33xx_pm_free_sram();
  292. return 0;
  293. }
  294. static struct platform_driver am33xx_pm_driver = {
  295. .driver = {
  296. .name = "pm33xx",
  297. },
  298. .probe = am33xx_pm_probe,
  299. .remove = am33xx_pm_remove,
  300. };
  301. module_platform_driver(am33xx_pm_driver);
  302. MODULE_ALIAS("platform:pm33xx");
  303. MODULE_LICENSE("GPL v2");
  304. MODULE_DESCRIPTION("am33xx power management driver");