da8xx_remoteproc.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /*
  2. * Remote processor machine-specific module for DA8XX
  3. *
  4. * Copyright (C) 2013 Texas Instruments, Inc.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * version 2 as published by the Free Software Foundation.
  9. */
  10. #include <linux/bitops.h>
  11. #include <linux/clk.h>
  12. #include <linux/err.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/io.h>
  15. #include <linux/irq.h>
  16. #include <linux/kernel.h>
  17. #include <linux/module.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/remoteproc.h>
  20. #include <mach/clock.h> /* for davinci_clk_reset_assert/deassert() */
  21. #include "remoteproc_internal.h"
  22. static char *da8xx_fw_name;
  23. module_param(da8xx_fw_name, charp, S_IRUGO);
  24. MODULE_PARM_DESC(da8xx_fw_name,
  25. "Name of DSP firmware file in /lib/firmware (if not specified defaults to 'rproc-dsp-fw')");
  26. /*
  27. * OMAP-L138 Technical References:
  28. * http://www.ti.com/product/omap-l138
  29. */
  30. #define SYSCFG_CHIPSIG0 BIT(0)
  31. #define SYSCFG_CHIPSIG1 BIT(1)
  32. #define SYSCFG_CHIPSIG2 BIT(2)
  33. #define SYSCFG_CHIPSIG3 BIT(3)
  34. #define SYSCFG_CHIPSIG4 BIT(4)
  35. /**
  36. * struct da8xx_rproc - da8xx remote processor instance state
  37. * @rproc: rproc handle
  38. * @dsp_clk: placeholder for platform's DSP clk
  39. * @ack_fxn: chip-specific ack function for ack'ing irq
  40. * @irq_data: ack_fxn function parameter
  41. * @chipsig: virt ptr to DSP interrupt registers (CHIPSIG & CHIPSIG_CLR)
  42. * @bootreg: virt ptr to DSP boot address register (HOST1CFG)
  43. * @irq: irq # used by this instance
  44. */
  45. struct da8xx_rproc {
  46. struct rproc *rproc;
  47. struct clk *dsp_clk;
  48. void (*ack_fxn)(struct irq_data *data);
  49. struct irq_data *irq_data;
  50. void __iomem *chipsig;
  51. void __iomem *bootreg;
  52. int irq;
  53. };
  54. /**
  55. * handle_event() - inbound virtqueue message workqueue function
  56. *
  57. * This function is registered as a kernel thread and is scheduled by the
  58. * kernel handler.
  59. */
  60. static irqreturn_t handle_event(int irq, void *p)
  61. {
  62. struct rproc *rproc = (struct rproc *)p;
  63. /* Process incoming buffers on all our vrings */
  64. rproc_vq_interrupt(rproc, 0);
  65. rproc_vq_interrupt(rproc, 1);
  66. return IRQ_HANDLED;
  67. }
  68. /**
  69. * da8xx_rproc_callback() - inbound virtqueue message handler
  70. *
  71. * This handler is invoked directly by the kernel whenever the remote
  72. * core (DSP) has modified the state of a virtqueue. There is no
  73. * "payload" message indicating the virtqueue index as is the case with
  74. * mailbox-based implementations on OMAP4. As such, this handler "polls"
  75. * each known virtqueue index for every invocation.
  76. */
  77. static irqreturn_t da8xx_rproc_callback(int irq, void *p)
  78. {
  79. struct rproc *rproc = (struct rproc *)p;
  80. struct da8xx_rproc *drproc = (struct da8xx_rproc *)rproc->priv;
  81. u32 chipsig;
  82. chipsig = readl(drproc->chipsig);
  83. if (chipsig & SYSCFG_CHIPSIG0) {
  84. /* Clear interrupt level source */
  85. writel(SYSCFG_CHIPSIG0, drproc->chipsig + 4);
  86. /*
  87. * ACK intr to AINTC.
  88. *
  89. * It has already been ack'ed by the kernel before calling
  90. * this function, but since the ARM<->DSP interrupts in the
  91. * CHIPSIG register are "level" instead of "pulse" variety,
  92. * we need to ack it after taking down the level else we'll
  93. * be called again immediately after returning.
  94. */
  95. drproc->ack_fxn(drproc->irq_data);
  96. return IRQ_WAKE_THREAD;
  97. }
  98. return IRQ_HANDLED;
  99. }
  100. static int da8xx_rproc_start(struct rproc *rproc)
  101. {
  102. struct device *dev = rproc->dev.parent;
  103. struct da8xx_rproc *drproc = (struct da8xx_rproc *)rproc->priv;
  104. struct clk *dsp_clk = drproc->dsp_clk;
  105. /* hw requires the start (boot) address be on 1KB boundary */
  106. if (rproc->bootaddr & 0x3ff) {
  107. dev_err(dev, "invalid boot address: must be aligned to 1KB\n");
  108. return -EINVAL;
  109. }
  110. writel(rproc->bootaddr, drproc->bootreg);
  111. clk_enable(dsp_clk);
  112. davinci_clk_reset_deassert(dsp_clk);
  113. return 0;
  114. }
  115. static int da8xx_rproc_stop(struct rproc *rproc)
  116. {
  117. struct da8xx_rproc *drproc = rproc->priv;
  118. clk_disable(drproc->dsp_clk);
  119. return 0;
  120. }
  121. /* kick a virtqueue */
  122. static void da8xx_rproc_kick(struct rproc *rproc, int vqid)
  123. {
  124. struct da8xx_rproc *drproc = (struct da8xx_rproc *)rproc->priv;
  125. /* Interupt remote proc */
  126. writel(SYSCFG_CHIPSIG2, drproc->chipsig);
  127. }
  128. static struct rproc_ops da8xx_rproc_ops = {
  129. .start = da8xx_rproc_start,
  130. .stop = da8xx_rproc_stop,
  131. .kick = da8xx_rproc_kick,
  132. };
  133. static int reset_assert(struct device *dev)
  134. {
  135. struct clk *dsp_clk;
  136. dsp_clk = clk_get(dev, NULL);
  137. if (IS_ERR(dsp_clk)) {
  138. dev_err(dev, "clk_get error: %ld\n", PTR_ERR(dsp_clk));
  139. return PTR_ERR(dsp_clk);
  140. }
  141. davinci_clk_reset_assert(dsp_clk);
  142. clk_put(dsp_clk);
  143. return 0;
  144. }
  145. static int da8xx_rproc_probe(struct platform_device *pdev)
  146. {
  147. struct device *dev = &pdev->dev;
  148. struct da8xx_rproc *drproc;
  149. struct rproc *rproc;
  150. struct irq_data *irq_data;
  151. struct resource *bootreg_res;
  152. struct resource *chipsig_res;
  153. struct clk *dsp_clk;
  154. void __iomem *chipsig;
  155. void __iomem *bootreg;
  156. int irq;
  157. int ret;
  158. irq = platform_get_irq(pdev, 0);
  159. if (irq < 0) {
  160. dev_err(dev, "platform_get_irq(pdev, 0) error: %d\n", irq);
  161. return irq;
  162. }
  163. irq_data = irq_get_irq_data(irq);
  164. if (!irq_data) {
  165. dev_err(dev, "irq_get_irq_data(%d): NULL\n", irq);
  166. return -EINVAL;
  167. }
  168. bootreg_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  169. bootreg = devm_ioremap_resource(dev, bootreg_res);
  170. if (IS_ERR(bootreg))
  171. return PTR_ERR(bootreg);
  172. chipsig_res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  173. chipsig = devm_ioremap_resource(dev, chipsig_res);
  174. if (IS_ERR(chipsig))
  175. return PTR_ERR(chipsig);
  176. dsp_clk = devm_clk_get(dev, NULL);
  177. if (IS_ERR(dsp_clk)) {
  178. dev_err(dev, "clk_get error: %ld\n", PTR_ERR(dsp_clk));
  179. return PTR_ERR(dsp_clk);
  180. }
  181. rproc = rproc_alloc(dev, "dsp", &da8xx_rproc_ops, da8xx_fw_name,
  182. sizeof(*drproc));
  183. if (!rproc)
  184. return -ENOMEM;
  185. drproc = rproc->priv;
  186. drproc->rproc = rproc;
  187. rproc->has_iommu = false;
  188. platform_set_drvdata(pdev, rproc);
  189. /* everything the ISR needs is now setup, so hook it up */
  190. ret = devm_request_threaded_irq(dev, irq, da8xx_rproc_callback,
  191. handle_event, 0, "da8xx-remoteproc",
  192. rproc);
  193. if (ret) {
  194. dev_err(dev, "devm_request_threaded_irq error: %d\n", ret);
  195. goto free_rproc;
  196. }
  197. /*
  198. * rproc_add() can end up enabling the DSP's clk with the DSP
  199. * *not* in reset, but da8xx_rproc_start() needs the DSP to be
  200. * held in reset at the time it is called.
  201. */
  202. ret = reset_assert(dev);
  203. if (ret)
  204. goto free_rproc;
  205. drproc->chipsig = chipsig;
  206. drproc->bootreg = bootreg;
  207. drproc->ack_fxn = irq_data->chip->irq_ack;
  208. drproc->irq_data = irq_data;
  209. drproc->irq = irq;
  210. drproc->dsp_clk = dsp_clk;
  211. ret = rproc_add(rproc);
  212. if (ret) {
  213. dev_err(dev, "rproc_add failed: %d\n", ret);
  214. goto free_rproc;
  215. }
  216. return 0;
  217. free_rproc:
  218. rproc_put(rproc);
  219. return ret;
  220. }
  221. static int da8xx_rproc_remove(struct platform_device *pdev)
  222. {
  223. struct device *dev = &pdev->dev;
  224. struct rproc *rproc = platform_get_drvdata(pdev);
  225. struct da8xx_rproc *drproc = (struct da8xx_rproc *)rproc->priv;
  226. /*
  227. * It's important to place the DSP in reset before going away,
  228. * since a subsequent insmod of this module may enable the DSP's
  229. * clock before its program/boot-address has been loaded and
  230. * before this module's probe has had a chance to reset the DSP.
  231. * Without the reset, the DSP can lockup permanently when it
  232. * begins executing garbage.
  233. */
  234. reset_assert(dev);
  235. /*
  236. * The devm subsystem might end up releasing things before
  237. * freeing the irq, thus allowing an interrupt to sneak in while
  238. * the device is being removed. This should prevent that.
  239. */
  240. disable_irq(drproc->irq);
  241. rproc_del(rproc);
  242. rproc_put(rproc);
  243. return 0;
  244. }
  245. static struct platform_driver da8xx_rproc_driver = {
  246. .probe = da8xx_rproc_probe,
  247. .remove = da8xx_rproc_remove,
  248. .driver = {
  249. .name = "davinci-rproc",
  250. },
  251. };
  252. module_platform_driver(da8xx_rproc_driver);
  253. MODULE_LICENSE("GPL v2");
  254. MODULE_DESCRIPTION("DA8XX Remote Processor control driver");