wkup_m3_rproc.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /*
  2. * TI AMx3 Wakeup M3 Remote Processor driver
  3. *
  4. * Copyright (C) 2014-2015 Texas Instruments, Inc.
  5. *
  6. * Dave Gerlach <d-gerlach@ti.com>
  7. * Suman Anna <s-anna@ti.com>
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License
  11. * version 2 as published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. */
  18. #include <linux/err.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/kernel.h>
  21. #include <linux/module.h>
  22. #include <linux/of_device.h>
  23. #include <linux/of_address.h>
  24. #include <linux/platform_device.h>
  25. #include <linux/pm_runtime.h>
  26. #include <linux/remoteproc.h>
  27. #include <linux/platform_data/wkup_m3.h>
  28. #include "remoteproc_internal.h"
  29. #define WKUPM3_MEM_MAX 2
  30. /**
  31. * struct wkup_m3_mem - WkupM3 internal memory structure
  32. * @cpu_addr: MPU virtual address of the memory region
  33. * @bus_addr: Bus address used to access the memory region
  34. * @dev_addr: Device address from Wakeup M3 view
  35. * @size: Size of the memory region
  36. */
  37. struct wkup_m3_mem {
  38. void __iomem *cpu_addr;
  39. phys_addr_t bus_addr;
  40. u32 dev_addr;
  41. size_t size;
  42. };
  43. /**
  44. * struct wkup_m3_rproc - WkupM3 remote processor state
  45. * @rproc: rproc handle
  46. * @pdev: pointer to platform device
  47. * @mem: WkupM3 memory information
  48. */
  49. struct wkup_m3_rproc {
  50. struct rproc *rproc;
  51. struct platform_device *pdev;
  52. struct wkup_m3_mem mem[WKUPM3_MEM_MAX];
  53. };
  54. static int wkup_m3_rproc_start(struct rproc *rproc)
  55. {
  56. struct wkup_m3_rproc *wkupm3 = rproc->priv;
  57. struct platform_device *pdev = wkupm3->pdev;
  58. struct device *dev = &pdev->dev;
  59. struct wkup_m3_platform_data *pdata = dev_get_platdata(dev);
  60. if (pdata->deassert_reset(pdev, pdata->reset_name)) {
  61. dev_err(dev, "Unable to reset wkup_m3!\n");
  62. return -ENODEV;
  63. }
  64. return 0;
  65. }
  66. static int wkup_m3_rproc_stop(struct rproc *rproc)
  67. {
  68. struct wkup_m3_rproc *wkupm3 = rproc->priv;
  69. struct platform_device *pdev = wkupm3->pdev;
  70. struct device *dev = &pdev->dev;
  71. struct wkup_m3_platform_data *pdata = dev_get_platdata(dev);
  72. if (pdata->assert_reset(pdev, pdata->reset_name)) {
  73. dev_err(dev, "Unable to assert reset of wkup_m3!\n");
  74. return -ENODEV;
  75. }
  76. return 0;
  77. }
  78. static void *wkup_m3_rproc_da_to_va(struct rproc *rproc, u64 da, int len)
  79. {
  80. struct wkup_m3_rproc *wkupm3 = rproc->priv;
  81. void *va = NULL;
  82. int i;
  83. u32 offset;
  84. if (len <= 0)
  85. return NULL;
  86. for (i = 0; i < WKUPM3_MEM_MAX; i++) {
  87. if (da >= wkupm3->mem[i].dev_addr && da + len <=
  88. wkupm3->mem[i].dev_addr + wkupm3->mem[i].size) {
  89. offset = da - wkupm3->mem[i].dev_addr;
  90. /* __force to make sparse happy with type conversion */
  91. va = (__force void *)(wkupm3->mem[i].cpu_addr + offset);
  92. break;
  93. }
  94. }
  95. return va;
  96. }
  97. static struct rproc_ops wkup_m3_rproc_ops = {
  98. .start = wkup_m3_rproc_start,
  99. .stop = wkup_m3_rproc_stop,
  100. .da_to_va = wkup_m3_rproc_da_to_va,
  101. };
  102. static const struct of_device_id wkup_m3_rproc_of_match[] = {
  103. { .compatible = "ti,am3352-wkup-m3", },
  104. { .compatible = "ti,am4372-wkup-m3", },
  105. {},
  106. };
  107. MODULE_DEVICE_TABLE(of, wkup_m3_rproc_of_match);
  108. static int wkup_m3_rproc_probe(struct platform_device *pdev)
  109. {
  110. struct device *dev = &pdev->dev;
  111. struct wkup_m3_platform_data *pdata = dev->platform_data;
  112. /* umem always needs to be processed first */
  113. const char *mem_names[WKUPM3_MEM_MAX] = { "umem", "dmem" };
  114. struct wkup_m3_rproc *wkupm3;
  115. const char *fw_name;
  116. struct rproc *rproc;
  117. struct resource *res;
  118. const __be32 *addrp;
  119. u32 l4_offset = 0;
  120. u64 size;
  121. int ret;
  122. int i;
  123. if (!(pdata && pdata->deassert_reset && pdata->assert_reset &&
  124. pdata->reset_name)) {
  125. dev_err(dev, "Platform data missing!\n");
  126. return -ENODEV;
  127. }
  128. ret = of_property_read_string(dev->of_node, "ti,pm-firmware",
  129. &fw_name);
  130. if (ret) {
  131. dev_err(dev, "No firmware filename given\n");
  132. return -ENODEV;
  133. }
  134. pm_runtime_enable(&pdev->dev);
  135. ret = pm_runtime_get_sync(&pdev->dev);
  136. if (ret < 0) {
  137. dev_err(&pdev->dev, "pm_runtime_get_sync() failed\n");
  138. goto err;
  139. }
  140. rproc = rproc_alloc(dev, "wkup_m3", &wkup_m3_rproc_ops,
  141. fw_name, sizeof(*wkupm3));
  142. if (!rproc) {
  143. ret = -ENOMEM;
  144. goto err;
  145. }
  146. rproc->auto_boot = false;
  147. wkupm3 = rproc->priv;
  148. wkupm3->rproc = rproc;
  149. wkupm3->pdev = pdev;
  150. for (i = 0; i < ARRAY_SIZE(mem_names); i++) {
  151. res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
  152. mem_names[i]);
  153. wkupm3->mem[i].cpu_addr = devm_ioremap_resource(dev, res);
  154. if (IS_ERR(wkupm3->mem[i].cpu_addr)) {
  155. dev_err(&pdev->dev, "devm_ioremap_resource failed for resource %d\n",
  156. i);
  157. ret = PTR_ERR(wkupm3->mem[i].cpu_addr);
  158. goto err;
  159. }
  160. wkupm3->mem[i].bus_addr = res->start;
  161. wkupm3->mem[i].size = resource_size(res);
  162. addrp = of_get_address(dev->of_node, i, &size, NULL);
  163. /*
  164. * The wkupm3 has umem at address 0 in its view, so the device
  165. * addresses for each memory region is computed as a relative
  166. * offset of the bus address for umem, and therefore needs to be
  167. * processed first.
  168. */
  169. if (!strcmp(mem_names[i], "umem"))
  170. l4_offset = be32_to_cpu(*addrp);
  171. wkupm3->mem[i].dev_addr = be32_to_cpu(*addrp) - l4_offset;
  172. }
  173. dev_set_drvdata(dev, rproc);
  174. ret = rproc_add(rproc);
  175. if (ret) {
  176. dev_err(dev, "rproc_add failed\n");
  177. goto err_put_rproc;
  178. }
  179. return 0;
  180. err_put_rproc:
  181. rproc_free(rproc);
  182. err:
  183. pm_runtime_put_noidle(dev);
  184. pm_runtime_disable(dev);
  185. return ret;
  186. }
  187. static int wkup_m3_rproc_remove(struct platform_device *pdev)
  188. {
  189. struct rproc *rproc = platform_get_drvdata(pdev);
  190. rproc_del(rproc);
  191. rproc_free(rproc);
  192. pm_runtime_put_sync(&pdev->dev);
  193. pm_runtime_disable(&pdev->dev);
  194. return 0;
  195. }
  196. #ifdef CONFIG_PM
  197. static int wkup_m3_rpm_suspend(struct device *dev)
  198. {
  199. return -EBUSY;
  200. }
  201. static int wkup_m3_rpm_resume(struct device *dev)
  202. {
  203. return 0;
  204. }
  205. #endif
  206. static const struct dev_pm_ops wkup_m3_rproc_pm_ops = {
  207. SET_RUNTIME_PM_OPS(wkup_m3_rpm_suspend, wkup_m3_rpm_resume, NULL)
  208. };
  209. static struct platform_driver wkup_m3_rproc_driver = {
  210. .probe = wkup_m3_rproc_probe,
  211. .remove = wkup_m3_rproc_remove,
  212. .driver = {
  213. .name = "wkup_m3_rproc",
  214. .of_match_table = wkup_m3_rproc_of_match,
  215. .pm = &wkup_m3_rproc_pm_ops,
  216. },
  217. };
  218. module_platform_driver(wkup_m3_rproc_driver);
  219. MODULE_LICENSE("GPL v2");
  220. MODULE_DESCRIPTION("TI Wakeup M3 remote processor control driver");
  221. MODULE_AUTHOR("Dave Gerlach <d-gerlach@ti.com>");