da8xx_remoteproc.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Remote processor machine-specific module for DA8XX
  4. *
  5. * Copyright (C) 2013 Texas Instruments, Inc.
  6. */
  7. #include <linux/bitops.h>
  8. #include <linux/clk.h>
  9. #include <linux/reset.h>
  10. #include <linux/err.h>
  11. #include <linux/interrupt.h>
  12. #include <linux/io.h>
  13. #include <linux/irq.h>
  14. #include <linux/kernel.h>
  15. #include <linux/module.h>
  16. #include <linux/of_reserved_mem.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/remoteproc.h>
  19. #include "remoteproc_internal.h"
  20. static char *da8xx_fw_name;
  21. module_param(da8xx_fw_name, charp, 0444);
  22. MODULE_PARM_DESC(da8xx_fw_name,
  23. "Name of DSP firmware file in /lib/firmware (if not specified defaults to 'rproc-dsp-fw')");
  24. /*
  25. * OMAP-L138 Technical References:
  26. * http://www.ti.com/product/omap-l138
  27. */
  28. #define SYSCFG_CHIPSIG0 BIT(0)
  29. #define SYSCFG_CHIPSIG1 BIT(1)
  30. #define SYSCFG_CHIPSIG2 BIT(2)
  31. #define SYSCFG_CHIPSIG3 BIT(3)
  32. #define SYSCFG_CHIPSIG4 BIT(4)
  33. #define DA8XX_RPROC_LOCAL_ADDRESS_MASK (SZ_16M - 1)
  34. /**
  35. * struct da8xx_rproc_mem - internal memory structure
  36. * @cpu_addr: MPU virtual address of the memory region
  37. * @bus_addr: Bus address used to access the memory region
  38. * @dev_addr: Device address of the memory region from DSP view
  39. * @size: Size of the memory region
  40. */
  41. struct da8xx_rproc_mem {
  42. void __iomem *cpu_addr;
  43. phys_addr_t bus_addr;
  44. u32 dev_addr;
  45. size_t size;
  46. };
  47. /**
  48. * struct da8xx_rproc - da8xx remote processor instance state
  49. * @rproc: rproc handle
  50. * @mem: internal memory regions data
  51. * @num_mems: number of internal memory regions
  52. * @dsp_clk: placeholder for platform's DSP clk
  53. * @ack_fxn: chip-specific ack function for ack'ing irq
  54. * @irq_data: ack_fxn function parameter
  55. * @chipsig: virt ptr to DSP interrupt registers (CHIPSIG & CHIPSIG_CLR)
  56. * @bootreg: virt ptr to DSP boot address register (HOST1CFG)
  57. * @irq: irq # used by this instance
  58. */
  59. struct da8xx_rproc {
  60. struct rproc *rproc;
  61. struct da8xx_rproc_mem *mem;
  62. int num_mems;
  63. struct clk *dsp_clk;
  64. struct reset_control *dsp_reset;
  65. void (*ack_fxn)(struct irq_data *data);
  66. struct irq_data *irq_data;
  67. void __iomem *chipsig;
  68. void __iomem *bootreg;
  69. int irq;
  70. };
  71. /**
  72. * handle_event() - inbound virtqueue message workqueue function
  73. *
  74. * This function is registered as a kernel thread and is scheduled by the
  75. * kernel handler.
  76. */
  77. static irqreturn_t handle_event(int irq, void *p)
  78. {
  79. struct rproc *rproc = (struct rproc *)p;
  80. /* Process incoming buffers on all our vrings */
  81. rproc_vq_interrupt(rproc, 0);
  82. rproc_vq_interrupt(rproc, 1);
  83. return IRQ_HANDLED;
  84. }
  85. /**
  86. * da8xx_rproc_callback() - inbound virtqueue message handler
  87. *
  88. * This handler is invoked directly by the kernel whenever the remote
  89. * core (DSP) has modified the state of a virtqueue. There is no
  90. * "payload" message indicating the virtqueue index as is the case with
  91. * mailbox-based implementations on OMAP4. As such, this handler "polls"
  92. * each known virtqueue index for every invocation.
  93. */
  94. static irqreturn_t da8xx_rproc_callback(int irq, void *p)
  95. {
  96. struct rproc *rproc = (struct rproc *)p;
  97. struct da8xx_rproc *drproc = (struct da8xx_rproc *)rproc->priv;
  98. u32 chipsig;
  99. chipsig = readl(drproc->chipsig);
  100. if (chipsig & SYSCFG_CHIPSIG0) {
  101. /* Clear interrupt level source */
  102. writel(SYSCFG_CHIPSIG0, drproc->chipsig + 4);
  103. /*
  104. * ACK intr to AINTC.
  105. *
  106. * It has already been ack'ed by the kernel before calling
  107. * this function, but since the ARM<->DSP interrupts in the
  108. * CHIPSIG register are "level" instead of "pulse" variety,
  109. * we need to ack it after taking down the level else we'll
  110. * be called again immediately after returning.
  111. */
  112. drproc->ack_fxn(drproc->irq_data);
  113. return IRQ_WAKE_THREAD;
  114. }
  115. return IRQ_HANDLED;
  116. }
  117. static int da8xx_rproc_start(struct rproc *rproc)
  118. {
  119. struct device *dev = rproc->dev.parent;
  120. struct da8xx_rproc *drproc = (struct da8xx_rproc *)rproc->priv;
  121. struct clk *dsp_clk = drproc->dsp_clk;
  122. struct reset_control *dsp_reset = drproc->dsp_reset;
  123. int ret;
  124. /* hw requires the start (boot) address be on 1KB boundary */
  125. if (rproc->bootaddr & 0x3ff) {
  126. dev_err(dev, "invalid boot address: must be aligned to 1KB\n");
  127. return -EINVAL;
  128. }
  129. writel(rproc->bootaddr, drproc->bootreg);
  130. ret = clk_prepare_enable(dsp_clk);
  131. if (ret) {
  132. dev_err(dev, "clk_prepare_enable() failed: %d\n", ret);
  133. return ret;
  134. }
  135. ret = reset_control_deassert(dsp_reset);
  136. if (ret) {
  137. dev_err(dev, "reset_control_deassert() failed: %d\n", ret);
  138. clk_disable_unprepare(dsp_clk);
  139. return ret;
  140. }
  141. return 0;
  142. }
  143. static int da8xx_rproc_stop(struct rproc *rproc)
  144. {
  145. struct da8xx_rproc *drproc = rproc->priv;
  146. struct device *dev = rproc->dev.parent;
  147. int ret;
  148. ret = reset_control_assert(drproc->dsp_reset);
  149. if (ret) {
  150. dev_err(dev, "reset_control_assert() failed: %d\n", ret);
  151. return ret;
  152. }
  153. clk_disable_unprepare(drproc->dsp_clk);
  154. return 0;
  155. }
  156. /* kick a virtqueue */
  157. static void da8xx_rproc_kick(struct rproc *rproc, int vqid)
  158. {
  159. struct da8xx_rproc *drproc = (struct da8xx_rproc *)rproc->priv;
  160. /* Interrupt remote proc */
  161. writel(SYSCFG_CHIPSIG2, drproc->chipsig);
  162. }
  163. static const struct rproc_ops da8xx_rproc_ops = {
  164. .start = da8xx_rproc_start,
  165. .stop = da8xx_rproc_stop,
  166. .kick = da8xx_rproc_kick,
  167. };
  168. static int da8xx_rproc_get_internal_memories(struct platform_device *pdev,
  169. struct da8xx_rproc *drproc)
  170. {
  171. static const char * const mem_names[] = {"l2sram", "l1pram", "l1dram"};
  172. int num_mems = ARRAY_SIZE(mem_names);
  173. struct device *dev = &pdev->dev;
  174. struct resource *res;
  175. int i;
  176. drproc->mem = devm_kcalloc(dev, num_mems, sizeof(*drproc->mem),
  177. GFP_KERNEL);
  178. if (!drproc->mem)
  179. return -ENOMEM;
  180. for (i = 0; i < num_mems; i++) {
  181. res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
  182. mem_names[i]);
  183. drproc->mem[i].cpu_addr = devm_ioremap_resource(dev, res);
  184. if (IS_ERR(drproc->mem[i].cpu_addr)) {
  185. dev_err(dev, "failed to parse and map %s memory\n",
  186. mem_names[i]);
  187. return PTR_ERR(drproc->mem[i].cpu_addr);
  188. }
  189. drproc->mem[i].bus_addr = res->start;
  190. drproc->mem[i].dev_addr =
  191. res->start & DA8XX_RPROC_LOCAL_ADDRESS_MASK;
  192. drproc->mem[i].size = resource_size(res);
  193. dev_dbg(dev, "memory %8s: bus addr %pa size 0x%zx va %p da 0x%x\n",
  194. mem_names[i], &drproc->mem[i].bus_addr,
  195. drproc->mem[i].size, drproc->mem[i].cpu_addr,
  196. drproc->mem[i].dev_addr);
  197. }
  198. drproc->num_mems = num_mems;
  199. return 0;
  200. }
  201. static int da8xx_rproc_probe(struct platform_device *pdev)
  202. {
  203. struct device *dev = &pdev->dev;
  204. struct da8xx_rproc *drproc;
  205. struct rproc *rproc;
  206. struct irq_data *irq_data;
  207. struct resource *bootreg_res;
  208. struct resource *chipsig_res;
  209. struct clk *dsp_clk;
  210. struct reset_control *dsp_reset;
  211. void __iomem *chipsig;
  212. void __iomem *bootreg;
  213. int irq;
  214. int ret;
  215. irq = platform_get_irq(pdev, 0);
  216. if (irq < 0)
  217. return irq;
  218. irq_data = irq_get_irq_data(irq);
  219. if (!irq_data) {
  220. dev_err(dev, "irq_get_irq_data(%d): NULL\n", irq);
  221. return -EINVAL;
  222. }
  223. bootreg_res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
  224. "host1cfg");
  225. bootreg = devm_ioremap_resource(dev, bootreg_res);
  226. if (IS_ERR(bootreg))
  227. return PTR_ERR(bootreg);
  228. chipsig_res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
  229. "chipsig");
  230. chipsig = devm_ioremap_resource(dev, chipsig_res);
  231. if (IS_ERR(chipsig))
  232. return PTR_ERR(chipsig);
  233. dsp_clk = devm_clk_get(dev, NULL);
  234. if (IS_ERR(dsp_clk)) {
  235. dev_err(dev, "clk_get error: %ld\n", PTR_ERR(dsp_clk));
  236. return PTR_ERR(dsp_clk);
  237. }
  238. dsp_reset = devm_reset_control_get_exclusive(dev, NULL);
  239. if (IS_ERR(dsp_reset)) {
  240. if (PTR_ERR(dsp_reset) != -EPROBE_DEFER)
  241. dev_err(dev, "unable to get reset control: %ld\n",
  242. PTR_ERR(dsp_reset));
  243. return PTR_ERR(dsp_reset);
  244. }
  245. if (dev->of_node) {
  246. ret = of_reserved_mem_device_init(dev);
  247. if (ret) {
  248. dev_err(dev, "device does not have specific CMA pool: %d\n",
  249. ret);
  250. return ret;
  251. }
  252. }
  253. rproc = rproc_alloc(dev, "dsp", &da8xx_rproc_ops, da8xx_fw_name,
  254. sizeof(*drproc));
  255. if (!rproc) {
  256. ret = -ENOMEM;
  257. goto free_mem;
  258. }
  259. /* error recovery is not supported at present */
  260. rproc->recovery_disabled = true;
  261. drproc = rproc->priv;
  262. drproc->rproc = rproc;
  263. drproc->dsp_clk = dsp_clk;
  264. drproc->dsp_reset = dsp_reset;
  265. rproc->has_iommu = false;
  266. ret = da8xx_rproc_get_internal_memories(pdev, drproc);
  267. if (ret)
  268. goto free_rproc;
  269. platform_set_drvdata(pdev, rproc);
  270. /* everything the ISR needs is now setup, so hook it up */
  271. ret = devm_request_threaded_irq(dev, irq, da8xx_rproc_callback,
  272. handle_event, 0, "da8xx-remoteproc",
  273. rproc);
  274. if (ret) {
  275. dev_err(dev, "devm_request_threaded_irq error: %d\n", ret);
  276. goto free_rproc;
  277. }
  278. /*
  279. * rproc_add() can end up enabling the DSP's clk with the DSP
  280. * *not* in reset, but da8xx_rproc_start() needs the DSP to be
  281. * held in reset at the time it is called.
  282. */
  283. ret = reset_control_assert(dsp_reset);
  284. if (ret)
  285. goto free_rproc;
  286. drproc->chipsig = chipsig;
  287. drproc->bootreg = bootreg;
  288. drproc->ack_fxn = irq_data->chip->irq_ack;
  289. drproc->irq_data = irq_data;
  290. drproc->irq = irq;
  291. ret = rproc_add(rproc);
  292. if (ret) {
  293. dev_err(dev, "rproc_add failed: %d\n", ret);
  294. goto free_rproc;
  295. }
  296. return 0;
  297. free_rproc:
  298. rproc_free(rproc);
  299. free_mem:
  300. if (dev->of_node)
  301. of_reserved_mem_device_release(dev);
  302. return ret;
  303. }
  304. static int da8xx_rproc_remove(struct platform_device *pdev)
  305. {
  306. struct rproc *rproc = platform_get_drvdata(pdev);
  307. struct da8xx_rproc *drproc = (struct da8xx_rproc *)rproc->priv;
  308. struct device *dev = &pdev->dev;
  309. /*
  310. * The devm subsystem might end up releasing things before
  311. * freeing the irq, thus allowing an interrupt to sneak in while
  312. * the device is being removed. This should prevent that.
  313. */
  314. disable_irq(drproc->irq);
  315. rproc_del(rproc);
  316. rproc_free(rproc);
  317. if (dev->of_node)
  318. of_reserved_mem_device_release(dev);
  319. return 0;
  320. }
  321. static const struct of_device_id davinci_rproc_of_match[] __maybe_unused = {
  322. { .compatible = "ti,da850-dsp", },
  323. { /* sentinel */ },
  324. };
  325. MODULE_DEVICE_TABLE(of, davinci_rproc_of_match);
  326. static struct platform_driver da8xx_rproc_driver = {
  327. .probe = da8xx_rproc_probe,
  328. .remove = da8xx_rproc_remove,
  329. .driver = {
  330. .name = "davinci-rproc",
  331. .of_match_table = of_match_ptr(davinci_rproc_of_match),
  332. },
  333. };
  334. module_platform_driver(da8xx_rproc_driver);
  335. MODULE_LICENSE("GPL v2");
  336. MODULE_DESCRIPTION("DA8XX Remote Processor control driver");