ti-emif-pm.c 9.3 KB

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