ti-emif-pm.c 9.5 KB

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