ingenic_rproc.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Ingenic JZ47xx remoteproc driver
  4. * Copyright 2019, Paul Cercueil <[email protected]>
  5. */
  6. #include <linux/bitops.h>
  7. #include <linux/clk.h>
  8. #include <linux/err.h>
  9. #include <linux/interrupt.h>
  10. #include <linux/io.h>
  11. #include <linux/module.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/remoteproc.h>
  14. #include "remoteproc_internal.h"
  15. #define REG_AUX_CTRL 0x0
  16. #define REG_AUX_MSG_ACK 0x10
  17. #define REG_AUX_MSG 0x14
  18. #define REG_CORE_MSG_ACK 0x18
  19. #define REG_CORE_MSG 0x1C
  20. #define AUX_CTRL_SLEEP BIT(31)
  21. #define AUX_CTRL_MSG_IRQ_EN BIT(3)
  22. #define AUX_CTRL_NMI_RESETS BIT(2)
  23. #define AUX_CTRL_NMI BIT(1)
  24. #define AUX_CTRL_SW_RESET BIT(0)
  25. static bool auto_boot;
  26. module_param(auto_boot, bool, 0400);
  27. MODULE_PARM_DESC(auto_boot,
  28. "Auto-boot the remote processor [default=false]");
  29. struct vpu_mem_map {
  30. const char *name;
  31. unsigned int da;
  32. };
  33. struct vpu_mem_info {
  34. const struct vpu_mem_map *map;
  35. unsigned long len;
  36. void __iomem *base;
  37. };
  38. static const struct vpu_mem_map vpu_mem_map[] = {
  39. { "tcsm0", 0x132b0000 },
  40. { "tcsm1", 0xf4000000 },
  41. { "sram", 0x132f0000 },
  42. };
  43. /**
  44. * struct vpu - Ingenic VPU remoteproc private structure
  45. * @irq: interrupt number
  46. * @clks: pointers to the VPU and AUX clocks
  47. * @aux_base: raw pointer to the AUX interface registers
  48. * @mem_info: array of struct vpu_mem_info, which contain the mapping info of
  49. * each of the external memories
  50. * @dev: private pointer to the device
  51. */
  52. struct vpu {
  53. int irq;
  54. struct clk_bulk_data clks[2];
  55. void __iomem *aux_base;
  56. struct vpu_mem_info mem_info[ARRAY_SIZE(vpu_mem_map)];
  57. struct device *dev;
  58. };
  59. static int ingenic_rproc_prepare(struct rproc *rproc)
  60. {
  61. struct vpu *vpu = rproc->priv;
  62. int ret;
  63. /* The clocks must be enabled for the firmware to be loaded in TCSM */
  64. ret = clk_bulk_prepare_enable(ARRAY_SIZE(vpu->clks), vpu->clks);
  65. if (ret)
  66. dev_err(vpu->dev, "Unable to start clocks: %d\n", ret);
  67. return ret;
  68. }
  69. static int ingenic_rproc_unprepare(struct rproc *rproc)
  70. {
  71. struct vpu *vpu = rproc->priv;
  72. clk_bulk_disable_unprepare(ARRAY_SIZE(vpu->clks), vpu->clks);
  73. return 0;
  74. }
  75. static int ingenic_rproc_start(struct rproc *rproc)
  76. {
  77. struct vpu *vpu = rproc->priv;
  78. u32 ctrl;
  79. enable_irq(vpu->irq);
  80. /* Reset the AUX and enable message IRQ */
  81. ctrl = AUX_CTRL_NMI_RESETS | AUX_CTRL_NMI | AUX_CTRL_MSG_IRQ_EN;
  82. writel(ctrl, vpu->aux_base + REG_AUX_CTRL);
  83. return 0;
  84. }
  85. static int ingenic_rproc_stop(struct rproc *rproc)
  86. {
  87. struct vpu *vpu = rproc->priv;
  88. disable_irq(vpu->irq);
  89. /* Keep AUX in reset mode */
  90. writel(AUX_CTRL_SW_RESET, vpu->aux_base + REG_AUX_CTRL);
  91. return 0;
  92. }
  93. static void ingenic_rproc_kick(struct rproc *rproc, int vqid)
  94. {
  95. struct vpu *vpu = rproc->priv;
  96. writel(vqid, vpu->aux_base + REG_CORE_MSG);
  97. }
  98. static void *ingenic_rproc_da_to_va(struct rproc *rproc, u64 da, size_t len, bool *is_iomem)
  99. {
  100. struct vpu *vpu = rproc->priv;
  101. void __iomem *va = NULL;
  102. unsigned int i;
  103. for (i = 0; i < ARRAY_SIZE(vpu_mem_map); i++) {
  104. const struct vpu_mem_info *info = &vpu->mem_info[i];
  105. const struct vpu_mem_map *map = info->map;
  106. if (da >= map->da && (da + len) < (map->da + info->len)) {
  107. va = info->base + (da - map->da);
  108. break;
  109. }
  110. }
  111. return (__force void *)va;
  112. }
  113. static const struct rproc_ops ingenic_rproc_ops = {
  114. .prepare = ingenic_rproc_prepare,
  115. .unprepare = ingenic_rproc_unprepare,
  116. .start = ingenic_rproc_start,
  117. .stop = ingenic_rproc_stop,
  118. .kick = ingenic_rproc_kick,
  119. .da_to_va = ingenic_rproc_da_to_va,
  120. };
  121. static irqreturn_t vpu_interrupt(int irq, void *data)
  122. {
  123. struct rproc *rproc = data;
  124. struct vpu *vpu = rproc->priv;
  125. u32 vring;
  126. vring = readl(vpu->aux_base + REG_AUX_MSG);
  127. /* Ack the interrupt */
  128. writel(0, vpu->aux_base + REG_AUX_MSG_ACK);
  129. return rproc_vq_interrupt(rproc, vring);
  130. }
  131. static int ingenic_rproc_probe(struct platform_device *pdev)
  132. {
  133. struct device *dev = &pdev->dev;
  134. struct resource *mem;
  135. struct rproc *rproc;
  136. struct vpu *vpu;
  137. unsigned int i;
  138. int ret;
  139. rproc = devm_rproc_alloc(dev, "ingenic-vpu",
  140. &ingenic_rproc_ops, NULL, sizeof(*vpu));
  141. if (!rproc)
  142. return -ENOMEM;
  143. rproc->auto_boot = auto_boot;
  144. vpu = rproc->priv;
  145. vpu->dev = &pdev->dev;
  146. platform_set_drvdata(pdev, vpu);
  147. mem = platform_get_resource_byname(pdev, IORESOURCE_MEM, "aux");
  148. vpu->aux_base = devm_ioremap_resource(dev, mem);
  149. if (IS_ERR(vpu->aux_base)) {
  150. dev_err(dev, "Failed to ioremap\n");
  151. return PTR_ERR(vpu->aux_base);
  152. }
  153. for (i = 0; i < ARRAY_SIZE(vpu_mem_map); i++) {
  154. mem = platform_get_resource_byname(pdev, IORESOURCE_MEM,
  155. vpu_mem_map[i].name);
  156. vpu->mem_info[i].base = devm_ioremap_resource(dev, mem);
  157. if (IS_ERR(vpu->mem_info[i].base)) {
  158. ret = PTR_ERR(vpu->mem_info[i].base);
  159. dev_err(dev, "Failed to ioremap\n");
  160. return ret;
  161. }
  162. vpu->mem_info[i].len = resource_size(mem);
  163. vpu->mem_info[i].map = &vpu_mem_map[i];
  164. }
  165. vpu->clks[0].id = "vpu";
  166. vpu->clks[1].id = "aux";
  167. ret = devm_clk_bulk_get(dev, ARRAY_SIZE(vpu->clks), vpu->clks);
  168. if (ret) {
  169. dev_err(dev, "Failed to get clocks\n");
  170. return ret;
  171. }
  172. vpu->irq = platform_get_irq(pdev, 0);
  173. if (vpu->irq < 0)
  174. return vpu->irq;
  175. ret = devm_request_irq(dev, vpu->irq, vpu_interrupt, IRQF_NO_AUTOEN,
  176. "VPU", rproc);
  177. if (ret < 0) {
  178. dev_err(dev, "Failed to request IRQ\n");
  179. return ret;
  180. }
  181. ret = devm_rproc_add(dev, rproc);
  182. if (ret) {
  183. dev_err(dev, "Failed to register remote processor\n");
  184. return ret;
  185. }
  186. return 0;
  187. }
  188. static const struct of_device_id ingenic_rproc_of_matches[] = {
  189. { .compatible = "ingenic,jz4770-vpu-rproc", },
  190. {}
  191. };
  192. MODULE_DEVICE_TABLE(of, ingenic_rproc_of_matches);
  193. static struct platform_driver ingenic_rproc_driver = {
  194. .probe = ingenic_rproc_probe,
  195. .driver = {
  196. .name = "ingenic-vpu",
  197. .of_match_table = ingenic_rproc_of_matches,
  198. },
  199. };
  200. module_platform_driver(ingenic_rproc_driver);
  201. MODULE_LICENSE("GPL");
  202. MODULE_AUTHOR("Paul Cercueil <[email protected]>");
  203. MODULE_DESCRIPTION("Ingenic JZ47xx Remote Processor control driver");