wkup_m3_rproc.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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. static int wkup_m3_rproc_probe(struct platform_device *pdev)
  108. {
  109. struct device *dev = &pdev->dev;
  110. struct wkup_m3_platform_data *pdata = dev->platform_data;
  111. /* umem always needs to be processed first */
  112. const char *mem_names[WKUPM3_MEM_MAX] = { "umem", "dmem" };
  113. struct wkup_m3_rproc *wkupm3;
  114. const char *fw_name;
  115. struct rproc *rproc;
  116. struct resource *res;
  117. const __be32 *addrp;
  118. u32 l4_offset = 0;
  119. u64 size;
  120. int ret;
  121. int i;
  122. if (!(pdata && pdata->deassert_reset && pdata->assert_reset &&
  123. pdata->reset_name)) {
  124. dev_err(dev, "Platform data missing!\n");
  125. return -ENODEV;
  126. }
  127. ret = of_property_read_string(dev->of_node, "ti,pm-firmware",
  128. &fw_name);
  129. if (ret) {
  130. dev_err(dev, "No firmware filename given\n");
  131. return -ENODEV;
  132. }
  133. pm_runtime_enable(&pdev->dev);
  134. ret = pm_runtime_get_sync(&pdev->dev);
  135. if (ret < 0) {
  136. dev_err(&pdev->dev, "pm_runtime_get_sync() failed\n");
  137. goto err;
  138. }
  139. rproc = rproc_alloc(dev, "wkup_m3", &wkup_m3_rproc_ops,
  140. fw_name, sizeof(*wkupm3));
  141. if (!rproc) {
  142. ret = -ENOMEM;
  143. goto err;
  144. }
  145. wkupm3 = rproc->priv;
  146. wkupm3->rproc = rproc;
  147. wkupm3->pdev = pdev;
  148. for (i = 0; i < ARRAY_SIZE(mem_names); i++) {
  149. res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
  150. mem_names[i]);
  151. wkupm3->mem[i].cpu_addr = devm_ioremap_resource(dev, res);
  152. if (IS_ERR(wkupm3->mem[i].cpu_addr)) {
  153. dev_err(&pdev->dev, "devm_ioremap_resource failed for resource %d\n",
  154. i);
  155. ret = PTR_ERR(wkupm3->mem[i].cpu_addr);
  156. goto err;
  157. }
  158. wkupm3->mem[i].bus_addr = res->start;
  159. wkupm3->mem[i].size = resource_size(res);
  160. addrp = of_get_address(dev->of_node, i, &size, NULL);
  161. /*
  162. * The wkupm3 has umem at address 0 in its view, so the device
  163. * addresses for each memory region is computed as a relative
  164. * offset of the bus address for umem, and therefore needs to be
  165. * processed first.
  166. */
  167. if (!strcmp(mem_names[i], "umem"))
  168. l4_offset = be32_to_cpu(*addrp);
  169. wkupm3->mem[i].dev_addr = be32_to_cpu(*addrp) - l4_offset;
  170. }
  171. dev_set_drvdata(dev, rproc);
  172. ret = rproc_add(rproc);
  173. if (ret) {
  174. dev_err(dev, "rproc_add failed\n");
  175. goto err_put_rproc;
  176. }
  177. return 0;
  178. err_put_rproc:
  179. rproc_put(rproc);
  180. err:
  181. pm_runtime_put_noidle(dev);
  182. pm_runtime_disable(dev);
  183. return ret;
  184. }
  185. static int wkup_m3_rproc_remove(struct platform_device *pdev)
  186. {
  187. struct rproc *rproc = platform_get_drvdata(pdev);
  188. rproc_del(rproc);
  189. rproc_put(rproc);
  190. pm_runtime_put_sync(&pdev->dev);
  191. pm_runtime_disable(&pdev->dev);
  192. return 0;
  193. }
  194. #ifdef CONFIG_PM
  195. static int wkup_m3_rpm_suspend(struct device *dev)
  196. {
  197. return -EBUSY;
  198. }
  199. static int wkup_m3_rpm_resume(struct device *dev)
  200. {
  201. return 0;
  202. }
  203. #endif
  204. static const struct dev_pm_ops wkup_m3_rproc_pm_ops = {
  205. SET_RUNTIME_PM_OPS(wkup_m3_rpm_suspend, wkup_m3_rpm_resume, NULL)
  206. };
  207. static struct platform_driver wkup_m3_rproc_driver = {
  208. .probe = wkup_m3_rproc_probe,
  209. .remove = wkup_m3_rproc_remove,
  210. .driver = {
  211. .name = "wkup_m3_rproc",
  212. .of_match_table = wkup_m3_rproc_of_match,
  213. .pm = &wkup_m3_rproc_pm_ops,
  214. },
  215. };
  216. module_platform_driver(wkup_m3_rproc_driver);
  217. MODULE_LICENSE("GPL v2");
  218. MODULE_DESCRIPTION("TI Wakeup M3 remote processor control driver");
  219. MODULE_AUTHOR("Dave Gerlach <d-gerlach@ti.com>");