ti-emif-pm.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. /*
  2. * TI AM33XX SRAM EMIF Driver
  3. *
  4. * Copyright (C) 2016 Texas Instruments Inc.
  5. * Dave Gerlach
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * 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/err.h>
  17. #include <linux/genalloc.h>
  18. #include <linux/io.h>
  19. #include <linux/kernel.h>
  20. #include <linux/module.h>
  21. #include <linux/of.h>
  22. #include <linux/of_platform.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/pm_runtime.h>
  25. #include <linux/sram.h>
  26. #include <linux/ti-emif-sram.h>
  27. #include <asm/fncpy.h>
  28. #include "emif.h"
  29. #define TI_EMIF_SRAM_SYMBOL_OFFSET(sym) ((unsigned long)(sym) - \
  30. (unsigned long)&ti_emif_sram)
  31. #define EMIF_POWER_MGMT_WAIT_SELF_REFRESH_8192_CYCLES 0x00a0
  32. static phys_addr_t ti_emif_sram_phys, ti_emif_sram_data_phys;
  33. static unsigned long ti_emif_sram_virt, ti_emif_sram_data_virt;
  34. static struct gen_pool *sram_pool_code, *sram_pool_data;
  35. static struct ti_emif_pm_data pm_data;
  36. static struct ti_emif_pm_functions pm_functions;
  37. static u32 sram_suspend_address(unsigned long addr)
  38. {
  39. return (ti_emif_sram_virt + TI_EMIF_SRAM_SYMBOL_OFFSET(addr));
  40. }
  41. static phys_addr_t sram_resume_address(unsigned long addr)
  42. {
  43. return ((unsigned long)ti_emif_sram_phys +
  44. TI_EMIF_SRAM_SYMBOL_OFFSET(addr));
  45. }
  46. static void ti_emif_free_sram(void)
  47. {
  48. gen_pool_free(sram_pool_code, ti_emif_sram_virt, ti_emif_sram_sz);
  49. gen_pool_free(sram_pool_data, ti_emif_sram_data_virt,
  50. sizeof(struct emif_regs_amx3));
  51. }
  52. static int ti_emif_prepare_push_sram(struct device *dev)
  53. {
  54. struct device_node *np = dev->of_node;
  55. int ret;
  56. sram_pool_code = of_gen_pool_get(np, "sram", 0);
  57. if (!sram_pool_code) {
  58. dev_err(dev, "Unable to get sram pool for ocmcram code\n");
  59. return -ENODEV;
  60. }
  61. ti_emif_sram_virt = gen_pool_alloc(sram_pool_code, ti_emif_sram_sz);
  62. if (!ti_emif_sram_virt) {
  63. dev_err(dev, "Unable to allocate code memory from ocmcram\n");
  64. return -ENOMEM;
  65. }
  66. /* Save physical address to calculate resume offset during pm init */
  67. ti_emif_sram_phys = gen_pool_virt_to_phys(sram_pool_code,
  68. ti_emif_sram_virt);
  69. /* Get sram pool for data section and allocate space */
  70. sram_pool_data = of_gen_pool_get(np, "sram", 1);
  71. if (!sram_pool_data) {
  72. dev_err(dev, "Unable to get sram pool for ocmcram data\n");
  73. ret = -ENODEV;
  74. goto err_free_sram_code;
  75. }
  76. ti_emif_sram_data_virt = gen_pool_alloc(sram_pool_data,
  77. sizeof(struct emif_regs_amx3));
  78. if (!ti_emif_sram_data_virt) {
  79. dev_err(dev, "Unable to allocate data memory from ocmcram\n");
  80. return -ENOMEM;
  81. goto err_free_sram_code;
  82. }
  83. /* Save physical address to calculate resume offset during pm init */
  84. ti_emif_sram_data_phys = gen_pool_virt_to_phys(sram_pool_data,
  85. ti_emif_sram_data_virt);
  86. /*
  87. * These functions are called during suspend path while MMU is
  88. * still on so add virtual base to offset for absolute address
  89. */
  90. pm_functions.save_context =
  91. sram_suspend_address((unsigned long)ti_emif_save_context);
  92. pm_functions.enter_sr =
  93. sram_suspend_address((unsigned long)ti_emif_enter_sr);
  94. pm_functions.abort_sr =
  95. sram_suspend_address((unsigned long)ti_emif_abort_sr);
  96. /*
  97. * These are called during resume path when MMU is not enabled
  98. * so physical address is used instead
  99. */
  100. pm_functions.restore_context =
  101. sram_resume_address((unsigned long)ti_emif_restore_context);
  102. pm_functions.exit_sr =
  103. sram_resume_address((unsigned long)ti_emif_exit_sr);
  104. pm_data.regs_virt = (struct emif_regs_amx3 *)ti_emif_sram_data_virt;
  105. pm_data.regs_phys = (struct emif_regs_amx3 *)ti_emif_sram_data_phys;
  106. return 0;
  107. err_free_sram_code:
  108. gen_pool_free(sram_pool_code, ti_emif_sram_virt, ti_emif_sram_sz);
  109. return ret;
  110. }
  111. static int ti_emif_push_sram(struct device *dev)
  112. {
  113. int ret;
  114. ret = sram_exec_copy(sram_pool_code, (void *)ti_emif_sram_virt,
  115. &ti_emif_sram, ti_emif_sram_sz);
  116. if (ret) {
  117. dev_err(dev, "Cannot copy emif code to sram\n");
  118. return ret;
  119. }
  120. ret = sram_exec_copy(sram_pool_code,
  121. (void *)sram_suspend_address((unsigned long)&ti_emif_pm_sram_data),
  122. &pm_data, sizeof(pm_data));
  123. if (ret) {
  124. dev_err(dev, "Cannot copy emif data to code sram\n");
  125. return ret;
  126. }
  127. return 0;
  128. }
  129. /*
  130. * Due to Usage Note 3.1.2 "DDR3: JEDEC Compliance for Maximum
  131. * Self-Refresh Command Limit" found in AM335x Silicon Errata
  132. * (Document SPRZ360F Revised November 2013) we must configure
  133. * the self refresh delay timer to 0xA (8192 cycles) to avoid
  134. * generating too many refresh command from the EMIF.
  135. */
  136. static void ti_emif_configure_sr_delay(void)
  137. {
  138. writel(EMIF_POWER_MGMT_WAIT_SELF_REFRESH_8192_CYCLES,
  139. (pm_data.ti_emif_base_addr_virt +
  140. EMIF_POWER_MANAGEMENT_CONTROL));
  141. writel(EMIF_POWER_MGMT_WAIT_SELF_REFRESH_8192_CYCLES,
  142. (pm_data.ti_emif_base_addr_virt +
  143. EMIF_POWER_MANAGEMENT_CTRL_SHDW));
  144. }
  145. /**
  146. * ti_emif_copy_pm_function_table - copy mapping of pm funcs in sram
  147. * @sram_pool: pointer to struct gen_pool where dst resides
  148. * @dst: void * to address that table should be copied
  149. *
  150. * Returns 0 if success other error code if table is not available
  151. */
  152. int ti_emif_copy_pm_function_table(struct gen_pool *sram_pool, void *dst)
  153. {
  154. int ret;
  155. if (!ti_emif_sram_virt)
  156. return -EINVAL;
  157. ret = sram_exec_copy(sram_pool, dst, &pm_functions,
  158. sizeof(pm_functions));
  159. return ret;
  160. }
  161. EXPORT_SYMBOL_GPL(ti_emif_copy_pm_function_table);
  162. /**
  163. * ti_emif_get_mem_type - return type for memory type in use
  164. *
  165. * Returns memory type value read from EMIF or error code if fails
  166. */
  167. int ti_emif_get_mem_type(void)
  168. {
  169. unsigned long temp;
  170. if (!pm_data.ti_emif_base_addr_virt ||
  171. IS_ERR(pm_data.ti_emif_base_addr_virt))
  172. return -ENODEV;
  173. temp = readl(pm_data.ti_emif_base_addr_virt +
  174. EMIF_SDRAM_CONFIG);
  175. temp = (temp & SDRAM_TYPE_MASK) >> SDRAM_TYPE_SHIFT;
  176. return temp;
  177. }
  178. EXPORT_SYMBOL_GPL(ti_emif_get_mem_type);
  179. static const struct of_device_id ti_emif_of_match[] = {
  180. { .compatible = "ti,emif-am3352", .data =
  181. (void *)EMIF_SRAM_AM33_REG_LAYOUT, },
  182. { .compatible = "ti,emif-am4372", .data =
  183. (void *)EMIF_SRAM_AM43_REG_LAYOUT, },
  184. {},
  185. };
  186. MODULE_DEVICE_TABLE(of, ti_emif_of_match);
  187. #ifdef CONFIG_PM_SLEEP
  188. static int ti_emif_resume(struct device *dev)
  189. {
  190. unsigned long tmp = __raw_readl((void *)ti_emif_sram_virt);
  191. /*
  192. * Check to see if what we are copying is already present in the
  193. * first byte at the destination, only copy if it is not which
  194. * indicates we have lost context and sram no longer contains
  195. * the PM code
  196. */
  197. if (tmp != ti_emif_sram)
  198. ti_emif_push_sram(dev);
  199. return 0;
  200. }
  201. #endif /* CONFIG_PM_SLEEP */
  202. static int ti_emif_probe(struct platform_device *pdev)
  203. {
  204. int ret = -ENODEV;
  205. struct resource *res;
  206. struct device *dev = &pdev->dev;
  207. const struct of_device_id *match;
  208. match = of_match_device(ti_emif_of_match, &pdev->dev);
  209. if (!match)
  210. return -ENODEV;
  211. pm_data.ti_emif_sram_config = (u32)match->data;
  212. pm_runtime_enable(dev);
  213. ret = pm_runtime_get_sync(dev);
  214. if (IS_ERR_VALUE(ret)) {
  215. dev_err(dev, "pm_runtime_get_sync() failed\n");
  216. goto fail_runtime_put;
  217. }
  218. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  219. pm_data.ti_emif_base_addr_virt = devm_ioremap_resource(dev, res);
  220. if (IS_ERR(pm_data.ti_emif_base_addr_virt)) {
  221. dev_err(dev, "could not ioremap emif mem\n");
  222. ret = PTR_ERR(pm_data.ti_emif_base_addr_virt);
  223. goto fail_runtime_put;
  224. }
  225. pm_data.ti_emif_base_addr_phys = res->start;
  226. ti_emif_configure_sr_delay();
  227. ret = ti_emif_prepare_push_sram(dev);
  228. if (ret)
  229. goto fail_runtime_put;
  230. ret = ti_emif_push_sram(dev);
  231. if (ret)
  232. goto fail_free_sram;
  233. return 0;
  234. fail_free_sram:
  235. ti_emif_free_sram();
  236. fail_runtime_put:
  237. pm_runtime_put_sync(dev);
  238. return ret;
  239. }
  240. static int ti_emif_remove(struct platform_device *pdev)
  241. {
  242. struct device *dev = &pdev->dev;
  243. pm_runtime_put_sync(dev);
  244. pm_runtime_disable(dev);
  245. ti_emif_free_sram();
  246. return 0;
  247. }
  248. static const struct dev_pm_ops ti_emif_pm_ops = {
  249. SET_SYSTEM_SLEEP_PM_OPS(NULL, ti_emif_resume)
  250. };
  251. static struct platform_driver ti_emif_driver = {
  252. .probe = ti_emif_probe,
  253. .remove = ti_emif_remove,
  254. .driver = {
  255. .name = KBUILD_MODNAME,
  256. .of_match_table = of_match_ptr(ti_emif_of_match),
  257. .pm = &ti_emif_pm_ops,
  258. },
  259. };
  260. module_platform_driver(ti_emif_driver);
  261. MODULE_AUTHOR("Dave Gerlach <d-gerlach@ti.com>");
  262. MODULE_DESCRIPTION("Texas Instruments SRAM EMIF driver");
  263. MODULE_LICENSE("GPL v2");