rcar-audmapp.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. /*
  2. * This is for Renesas R-Car Audio-DMAC-peri-peri.
  3. *
  4. * Copyright (C) 2014 Renesas Electronics Corporation
  5. * Copyright (C) 2014 Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
  6. *
  7. * based on the drivers/dma/sh/shdma.c
  8. *
  9. * Copyright (C) 2011-2012 Guennadi Liakhovetski <g.liakhovetski@gmx.de>
  10. * Copyright (C) 2009 Nobuhiro Iwamatsu <iwamatsu.nobuhiro@renesas.com>
  11. * Copyright (C) 2009 Renesas Solutions, Inc. All rights reserved.
  12. * Copyright (C) 2007 Freescale Semiconductor, Inc. All rights reserved.
  13. *
  14. * This is free software; you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License as published by
  16. * the Free Software Foundation; either version 2 of the License, or
  17. * (at your option) any later version.
  18. *
  19. */
  20. #include <linux/delay.h>
  21. #include <linux/init.h>
  22. #include <linux/module.h>
  23. #include <linux/slab.h>
  24. #include <linux/dmaengine.h>
  25. #include <linux/of_dma.h>
  26. #include <linux/platform_data/dma-rcar-audmapp.h>
  27. #include <linux/platform_device.h>
  28. #include <linux/shdma-base.h>
  29. /*
  30. * DMA register
  31. */
  32. #define PDMASAR 0x00
  33. #define PDMADAR 0x04
  34. #define PDMACHCR 0x0c
  35. /* PDMACHCR */
  36. #define PDMACHCR_DE (1 << 0)
  37. #define AUDMAPP_MAX_CHANNELS 29
  38. /* Default MEMCPY transfer size = 2^2 = 4 bytes */
  39. #define LOG2_DEFAULT_XFER_SIZE 2
  40. #define AUDMAPP_SLAVE_NUMBER 256
  41. #define AUDMAPP_LEN_MAX (16 * 1024 * 1024)
  42. struct audmapp_chan {
  43. struct shdma_chan shdma_chan;
  44. void __iomem *base;
  45. dma_addr_t slave_addr;
  46. u32 chcr;
  47. };
  48. struct audmapp_device {
  49. struct shdma_dev shdma_dev;
  50. struct audmapp_pdata *pdata;
  51. struct device *dev;
  52. void __iomem *chan_reg;
  53. };
  54. struct audmapp_desc {
  55. struct shdma_desc shdma_desc;
  56. dma_addr_t src;
  57. dma_addr_t dst;
  58. };
  59. #define to_shdma_chan(c) container_of(c, struct shdma_chan, dma_chan)
  60. #define to_chan(chan) container_of(chan, struct audmapp_chan, shdma_chan)
  61. #define to_desc(sdesc) container_of(sdesc, struct audmapp_desc, shdma_desc)
  62. #define to_dev(chan) container_of(chan->shdma_chan.dma_chan.device, \
  63. struct audmapp_device, shdma_dev.dma_dev)
  64. static void audmapp_write(struct audmapp_chan *auchan, u32 data, u32 reg)
  65. {
  66. struct audmapp_device *audev = to_dev(auchan);
  67. struct device *dev = audev->dev;
  68. dev_dbg(dev, "w %p : %08x\n", auchan->base + reg, data);
  69. iowrite32(data, auchan->base + reg);
  70. }
  71. static u32 audmapp_read(struct audmapp_chan *auchan, u32 reg)
  72. {
  73. return ioread32(auchan->base + reg);
  74. }
  75. static void audmapp_halt(struct shdma_chan *schan)
  76. {
  77. struct audmapp_chan *auchan = to_chan(schan);
  78. int i;
  79. audmapp_write(auchan, 0, PDMACHCR);
  80. for (i = 0; i < 1024; i++) {
  81. if (0 == audmapp_read(auchan, PDMACHCR))
  82. return;
  83. udelay(1);
  84. }
  85. }
  86. static void audmapp_start_xfer(struct shdma_chan *schan,
  87. struct shdma_desc *sdesc)
  88. {
  89. struct audmapp_chan *auchan = to_chan(schan);
  90. struct audmapp_device *audev = to_dev(auchan);
  91. struct audmapp_desc *desc = to_desc(sdesc);
  92. struct device *dev = audev->dev;
  93. u32 chcr = auchan->chcr | PDMACHCR_DE;
  94. dev_dbg(dev, "src/dst/chcr = %pad/%pad/%08x\n",
  95. &desc->src, &desc->dst, chcr);
  96. audmapp_write(auchan, desc->src, PDMASAR);
  97. audmapp_write(auchan, desc->dst, PDMADAR);
  98. audmapp_write(auchan, chcr, PDMACHCR);
  99. }
  100. static void audmapp_get_config(struct audmapp_chan *auchan, int slave_id,
  101. u32 *chcr, dma_addr_t *dst)
  102. {
  103. struct audmapp_device *audev = to_dev(auchan);
  104. struct audmapp_pdata *pdata = audev->pdata;
  105. struct audmapp_slave_config *cfg;
  106. int i;
  107. *chcr = 0;
  108. *dst = 0;
  109. if (!pdata) { /* DT */
  110. *chcr = ((u32)slave_id) << 16;
  111. auchan->shdma_chan.slave_id = (slave_id) >> 8;
  112. return;
  113. }
  114. /* non-DT */
  115. if (slave_id >= AUDMAPP_SLAVE_NUMBER)
  116. return;
  117. for (i = 0, cfg = pdata->slave; i < pdata->slave_num; i++, cfg++)
  118. if (cfg->slave_id == slave_id) {
  119. *chcr = cfg->chcr;
  120. *dst = cfg->dst;
  121. break;
  122. }
  123. }
  124. static int audmapp_set_slave(struct shdma_chan *schan, int slave_id,
  125. dma_addr_t slave_addr, bool try)
  126. {
  127. struct audmapp_chan *auchan = to_chan(schan);
  128. u32 chcr;
  129. dma_addr_t dst;
  130. audmapp_get_config(auchan, slave_id, &chcr, &dst);
  131. if (try)
  132. return 0;
  133. auchan->chcr = chcr;
  134. auchan->slave_addr = slave_addr ? : dst;
  135. return 0;
  136. }
  137. static int audmapp_desc_setup(struct shdma_chan *schan,
  138. struct shdma_desc *sdesc,
  139. dma_addr_t src, dma_addr_t dst, size_t *len)
  140. {
  141. struct audmapp_desc *desc = to_desc(sdesc);
  142. if (*len > (size_t)AUDMAPP_LEN_MAX)
  143. *len = (size_t)AUDMAPP_LEN_MAX;
  144. desc->src = src;
  145. desc->dst = dst;
  146. return 0;
  147. }
  148. static void audmapp_setup_xfer(struct shdma_chan *schan,
  149. int slave_id)
  150. {
  151. }
  152. static dma_addr_t audmapp_slave_addr(struct shdma_chan *schan)
  153. {
  154. struct audmapp_chan *auchan = to_chan(schan);
  155. return auchan->slave_addr;
  156. }
  157. static bool audmapp_channel_busy(struct shdma_chan *schan)
  158. {
  159. struct audmapp_chan *auchan = to_chan(schan);
  160. u32 chcr = audmapp_read(auchan, PDMACHCR);
  161. return chcr & ~PDMACHCR_DE;
  162. }
  163. static bool audmapp_desc_completed(struct shdma_chan *schan,
  164. struct shdma_desc *sdesc)
  165. {
  166. return true;
  167. }
  168. static struct shdma_desc *audmapp_embedded_desc(void *buf, int i)
  169. {
  170. return &((struct audmapp_desc *)buf)[i].shdma_desc;
  171. }
  172. static const struct shdma_ops audmapp_shdma_ops = {
  173. .halt_channel = audmapp_halt,
  174. .desc_setup = audmapp_desc_setup,
  175. .set_slave = audmapp_set_slave,
  176. .start_xfer = audmapp_start_xfer,
  177. .embedded_desc = audmapp_embedded_desc,
  178. .setup_xfer = audmapp_setup_xfer,
  179. .slave_addr = audmapp_slave_addr,
  180. .channel_busy = audmapp_channel_busy,
  181. .desc_completed = audmapp_desc_completed,
  182. };
  183. static int audmapp_chan_probe(struct platform_device *pdev,
  184. struct audmapp_device *audev, int id)
  185. {
  186. struct shdma_dev *sdev = &audev->shdma_dev;
  187. struct audmapp_chan *auchan;
  188. struct shdma_chan *schan;
  189. struct device *dev = audev->dev;
  190. auchan = devm_kzalloc(dev, sizeof(*auchan), GFP_KERNEL);
  191. if (!auchan)
  192. return -ENOMEM;
  193. schan = &auchan->shdma_chan;
  194. schan->max_xfer_len = AUDMAPP_LEN_MAX;
  195. shdma_chan_probe(sdev, schan, id);
  196. auchan->base = audev->chan_reg + 0x20 + (0x10 * id);
  197. dev_dbg(dev, "%02d : %p / %p", id, auchan->base, audev->chan_reg);
  198. return 0;
  199. }
  200. static void audmapp_chan_remove(struct audmapp_device *audev)
  201. {
  202. struct dma_device *dma_dev = &audev->shdma_dev.dma_dev;
  203. struct shdma_chan *schan;
  204. int i;
  205. shdma_for_each_chan(schan, &audev->shdma_dev, i) {
  206. BUG_ON(!schan);
  207. shdma_chan_remove(schan);
  208. }
  209. dma_dev->chancnt = 0;
  210. }
  211. static struct dma_chan *audmapp_of_xlate(struct of_phandle_args *dma_spec,
  212. struct of_dma *ofdma)
  213. {
  214. dma_cap_mask_t mask;
  215. struct dma_chan *chan;
  216. u32 chcr = dma_spec->args[0];
  217. if (dma_spec->args_count != 1)
  218. return NULL;
  219. dma_cap_zero(mask);
  220. dma_cap_set(DMA_SLAVE, mask);
  221. chan = dma_request_channel(mask, shdma_chan_filter, NULL);
  222. if (chan)
  223. to_shdma_chan(chan)->hw_req = chcr;
  224. return chan;
  225. }
  226. static int audmapp_probe(struct platform_device *pdev)
  227. {
  228. struct audmapp_pdata *pdata = pdev->dev.platform_data;
  229. struct device_node *np = pdev->dev.of_node;
  230. struct audmapp_device *audev;
  231. struct shdma_dev *sdev;
  232. struct dma_device *dma_dev;
  233. struct resource *res;
  234. int err, i;
  235. if (np)
  236. of_dma_controller_register(np, audmapp_of_xlate, pdev);
  237. else if (!pdata)
  238. return -ENODEV;
  239. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  240. audev = devm_kzalloc(&pdev->dev, sizeof(*audev), GFP_KERNEL);
  241. if (!audev)
  242. return -ENOMEM;
  243. audev->dev = &pdev->dev;
  244. audev->pdata = pdata;
  245. audev->chan_reg = devm_ioremap_resource(&pdev->dev, res);
  246. if (IS_ERR(audev->chan_reg))
  247. return PTR_ERR(audev->chan_reg);
  248. sdev = &audev->shdma_dev;
  249. sdev->ops = &audmapp_shdma_ops;
  250. sdev->desc_size = sizeof(struct audmapp_desc);
  251. dma_dev = &sdev->dma_dev;
  252. dma_dev->copy_align = LOG2_DEFAULT_XFER_SIZE;
  253. dma_cap_set(DMA_SLAVE, dma_dev->cap_mask);
  254. err = shdma_init(&pdev->dev, sdev, AUDMAPP_MAX_CHANNELS);
  255. if (err < 0)
  256. return err;
  257. platform_set_drvdata(pdev, audev);
  258. /* Create DMA Channel */
  259. for (i = 0; i < AUDMAPP_MAX_CHANNELS; i++) {
  260. err = audmapp_chan_probe(pdev, audev, i);
  261. if (err)
  262. goto chan_probe_err;
  263. }
  264. err = dma_async_device_register(dma_dev);
  265. if (err < 0)
  266. goto chan_probe_err;
  267. return err;
  268. chan_probe_err:
  269. audmapp_chan_remove(audev);
  270. shdma_cleanup(sdev);
  271. return err;
  272. }
  273. static int audmapp_remove(struct platform_device *pdev)
  274. {
  275. struct audmapp_device *audev = platform_get_drvdata(pdev);
  276. struct dma_device *dma_dev = &audev->shdma_dev.dma_dev;
  277. dma_async_device_unregister(dma_dev);
  278. audmapp_chan_remove(audev);
  279. shdma_cleanup(&audev->shdma_dev);
  280. return 0;
  281. }
  282. static const struct of_device_id audmapp_of_match[] = {
  283. { .compatible = "renesas,rcar-audmapp", },
  284. {},
  285. };
  286. static struct platform_driver audmapp_driver = {
  287. .probe = audmapp_probe,
  288. .remove = audmapp_remove,
  289. .driver = {
  290. .owner = THIS_MODULE,
  291. .name = "rcar-audmapp-engine",
  292. .of_match_table = audmapp_of_match,
  293. },
  294. };
  295. module_platform_driver(audmapp_driver);
  296. MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");
  297. MODULE_DESCRIPTION("Renesas R-Car Audio DMAC peri-peri driver");
  298. MODULE_LICENSE("GPL");