wkup_m3_rproc.c 6.0 KB

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