da8xx_remoteproc.c 8.0 KB

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