sudmac.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. /*
  2. * Renesas SUDMAC support
  3. *
  4. * Copyright (C) 2013 Renesas Solutions Corp.
  5. *
  6. * based on drivers/dma/sh/shdma.c:
  7. * Copyright (C) 2011-2012 Guennadi Liakhovetski <g.liakhovetski@gmx.de>
  8. * Copyright (C) 2009 Nobuhiro Iwamatsu <iwamatsu.nobuhiro@renesas.com>
  9. * Copyright (C) 2009 Renesas Solutions, Inc. All rights reserved.
  10. * Copyright (C) 2007 Freescale Semiconductor, Inc. All rights reserved.
  11. *
  12. * This is free software; you can redistribute it and/or modify
  13. * it under the terms of version 2 of the GNU General Public License as
  14. * published by the Free Software Foundation.
  15. */
  16. #include <linux/dmaengine.h>
  17. #include <linux/err.h>
  18. #include <linux/init.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/module.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/slab.h>
  23. #include <linux/sudmac.h>
  24. struct sudmac_chan {
  25. struct shdma_chan shdma_chan;
  26. void __iomem *base;
  27. char dev_id[16]; /* unique name per DMAC of channel */
  28. u32 offset; /* for CFG, BA, BBC, CA, CBC, DEN */
  29. u32 cfg;
  30. u32 dint_end_bit;
  31. };
  32. struct sudmac_device {
  33. struct shdma_dev shdma_dev;
  34. struct sudmac_pdata *pdata;
  35. void __iomem *chan_reg;
  36. };
  37. struct sudmac_regs {
  38. u32 base_addr;
  39. u32 base_byte_count;
  40. };
  41. struct sudmac_desc {
  42. struct sudmac_regs hw;
  43. struct shdma_desc shdma_desc;
  44. };
  45. #define to_chan(schan) container_of(schan, struct sudmac_chan, shdma_chan)
  46. #define to_desc(sdesc) container_of(sdesc, struct sudmac_desc, shdma_desc)
  47. #define to_sdev(sc) container_of(sc->shdma_chan.dma_chan.device, \
  48. struct sudmac_device, shdma_dev.dma_dev)
  49. /* SUDMAC register */
  50. #define SUDMAC_CH0CFG 0x00
  51. #define SUDMAC_CH0BA 0x10
  52. #define SUDMAC_CH0BBC 0x18
  53. #define SUDMAC_CH0CA 0x20
  54. #define SUDMAC_CH0CBC 0x28
  55. #define SUDMAC_CH0DEN 0x30
  56. #define SUDMAC_DSTSCLR 0x38
  57. #define SUDMAC_DBUFCTRL 0x3C
  58. #define SUDMAC_DINTCTRL 0x40
  59. #define SUDMAC_DINTSTS 0x44
  60. #define SUDMAC_DINTSTSCLR 0x48
  61. #define SUDMAC_CH0SHCTRL 0x50
  62. /* Definitions for the sudmac_channel.config */
  63. #define SUDMAC_SENDBUFM 0x1000 /* b12: Transmit Buffer Mode */
  64. #define SUDMAC_RCVENDM 0x0100 /* b8: Receive Data Transfer End Mode */
  65. #define SUDMAC_LBA_WAIT 0x0030 /* b5-4: Local Bus Access Wait */
  66. /* Definitions for the sudmac_channel.dint_end_bit */
  67. #define SUDMAC_CH1ENDE 0x0002 /* b1: Ch1 DMA Transfer End Int Enable */
  68. #define SUDMAC_CH0ENDE 0x0001 /* b0: Ch0 DMA Transfer End Int Enable */
  69. #define SUDMAC_DRV_NAME "sudmac"
  70. static void sudmac_writel(struct sudmac_chan *sc, u32 data, u32 reg)
  71. {
  72. iowrite32(data, sc->base + reg);
  73. }
  74. static u32 sudmac_readl(struct sudmac_chan *sc, u32 reg)
  75. {
  76. return ioread32(sc->base + reg);
  77. }
  78. static bool sudmac_is_busy(struct sudmac_chan *sc)
  79. {
  80. u32 den = sudmac_readl(sc, SUDMAC_CH0DEN + sc->offset);
  81. if (den)
  82. return true; /* working */
  83. return false; /* waiting */
  84. }
  85. static void sudmac_set_reg(struct sudmac_chan *sc, struct sudmac_regs *hw,
  86. struct shdma_desc *sdesc)
  87. {
  88. sudmac_writel(sc, sc->cfg, SUDMAC_CH0CFG + sc->offset);
  89. sudmac_writel(sc, hw->base_addr, SUDMAC_CH0BA + sc->offset);
  90. sudmac_writel(sc, hw->base_byte_count, SUDMAC_CH0BBC + sc->offset);
  91. }
  92. static void sudmac_start(struct sudmac_chan *sc)
  93. {
  94. u32 dintctrl = sudmac_readl(sc, SUDMAC_DINTCTRL);
  95. sudmac_writel(sc, dintctrl | sc->dint_end_bit, SUDMAC_DINTCTRL);
  96. sudmac_writel(sc, 1, SUDMAC_CH0DEN + sc->offset);
  97. }
  98. static void sudmac_start_xfer(struct shdma_chan *schan,
  99. struct shdma_desc *sdesc)
  100. {
  101. struct sudmac_chan *sc = to_chan(schan);
  102. struct sudmac_desc *sd = to_desc(sdesc);
  103. sudmac_set_reg(sc, &sd->hw, sdesc);
  104. sudmac_start(sc);
  105. }
  106. static bool sudmac_channel_busy(struct shdma_chan *schan)
  107. {
  108. struct sudmac_chan *sc = to_chan(schan);
  109. return sudmac_is_busy(sc);
  110. }
  111. static void sudmac_setup_xfer(struct shdma_chan *schan, int slave_id)
  112. {
  113. }
  114. static const struct sudmac_slave_config *sudmac_find_slave(
  115. struct sudmac_chan *sc, int slave_id)
  116. {
  117. struct sudmac_device *sdev = to_sdev(sc);
  118. struct sudmac_pdata *pdata = sdev->pdata;
  119. const struct sudmac_slave_config *cfg;
  120. int i;
  121. for (i = 0, cfg = pdata->slave; i < pdata->slave_num; i++, cfg++)
  122. if (cfg->slave_id == slave_id)
  123. return cfg;
  124. return NULL;
  125. }
  126. static int sudmac_set_slave(struct shdma_chan *schan, int slave_id,
  127. dma_addr_t slave_addr, bool try)
  128. {
  129. struct sudmac_chan *sc = to_chan(schan);
  130. const struct sudmac_slave_config *cfg = sudmac_find_slave(sc, slave_id);
  131. if (!cfg)
  132. return -ENODEV;
  133. return 0;
  134. }
  135. static inline void sudmac_dma_halt(struct sudmac_chan *sc)
  136. {
  137. u32 dintctrl = sudmac_readl(sc, SUDMAC_DINTCTRL);
  138. sudmac_writel(sc, 0, SUDMAC_CH0DEN + sc->offset);
  139. sudmac_writel(sc, dintctrl & ~sc->dint_end_bit, SUDMAC_DINTCTRL);
  140. sudmac_writel(sc, sc->dint_end_bit, SUDMAC_DINTSTSCLR);
  141. }
  142. static int sudmac_desc_setup(struct shdma_chan *schan,
  143. struct shdma_desc *sdesc,
  144. dma_addr_t src, dma_addr_t dst, size_t *len)
  145. {
  146. struct sudmac_chan *sc = to_chan(schan);
  147. struct sudmac_desc *sd = to_desc(sdesc);
  148. dev_dbg(sc->shdma_chan.dev, "%s: src=%pad, dst=%pad, len=%zu\n",
  149. __func__, &src, &dst, *len);
  150. if (*len > schan->max_xfer_len)
  151. *len = schan->max_xfer_len;
  152. if (dst)
  153. sd->hw.base_addr = dst;
  154. else if (src)
  155. sd->hw.base_addr = src;
  156. sd->hw.base_byte_count = *len;
  157. return 0;
  158. }
  159. static void sudmac_halt(struct shdma_chan *schan)
  160. {
  161. struct sudmac_chan *sc = to_chan(schan);
  162. sudmac_dma_halt(sc);
  163. }
  164. static bool sudmac_chan_irq(struct shdma_chan *schan, int irq)
  165. {
  166. struct sudmac_chan *sc = to_chan(schan);
  167. u32 dintsts = sudmac_readl(sc, SUDMAC_DINTSTS);
  168. if (!(dintsts & sc->dint_end_bit))
  169. return false;
  170. /* DMA stop */
  171. sudmac_dma_halt(sc);
  172. return true;
  173. }
  174. static size_t sudmac_get_partial(struct shdma_chan *schan,
  175. struct shdma_desc *sdesc)
  176. {
  177. struct sudmac_chan *sc = to_chan(schan);
  178. struct sudmac_desc *sd = to_desc(sdesc);
  179. u32 current_byte_count = sudmac_readl(sc, SUDMAC_CH0CBC + sc->offset);
  180. return sd->hw.base_byte_count - current_byte_count;
  181. }
  182. static bool sudmac_desc_completed(struct shdma_chan *schan,
  183. struct shdma_desc *sdesc)
  184. {
  185. struct sudmac_chan *sc = to_chan(schan);
  186. struct sudmac_desc *sd = to_desc(sdesc);
  187. u32 current_addr = sudmac_readl(sc, SUDMAC_CH0CA + sc->offset);
  188. return sd->hw.base_addr + sd->hw.base_byte_count == current_addr;
  189. }
  190. static int sudmac_chan_probe(struct sudmac_device *su_dev, int id, int irq,
  191. unsigned long flags)
  192. {
  193. struct shdma_dev *sdev = &su_dev->shdma_dev;
  194. struct platform_device *pdev = to_platform_device(sdev->dma_dev.dev);
  195. struct sudmac_chan *sc;
  196. struct shdma_chan *schan;
  197. int err;
  198. sc = devm_kzalloc(&pdev->dev, sizeof(struct sudmac_chan), GFP_KERNEL);
  199. if (!sc)
  200. return -ENOMEM;
  201. schan = &sc->shdma_chan;
  202. schan->max_xfer_len = 64 * 1024 * 1024 - 1;
  203. shdma_chan_probe(sdev, schan, id);
  204. sc->base = su_dev->chan_reg;
  205. /* get platform_data */
  206. sc->offset = su_dev->pdata->channel->offset;
  207. if (su_dev->pdata->channel->config & SUDMAC_TX_BUFFER_MODE)
  208. sc->cfg |= SUDMAC_SENDBUFM;
  209. if (su_dev->pdata->channel->config & SUDMAC_RX_END_MODE)
  210. sc->cfg |= SUDMAC_RCVENDM;
  211. sc->cfg |= (su_dev->pdata->channel->wait << 4) & SUDMAC_LBA_WAIT;
  212. if (su_dev->pdata->channel->dint_end_bit & SUDMAC_DMA_BIT_CH0)
  213. sc->dint_end_bit |= SUDMAC_CH0ENDE;
  214. if (su_dev->pdata->channel->dint_end_bit & SUDMAC_DMA_BIT_CH1)
  215. sc->dint_end_bit |= SUDMAC_CH1ENDE;
  216. /* set up channel irq */
  217. if (pdev->id >= 0)
  218. snprintf(sc->dev_id, sizeof(sc->dev_id), "sudmac%d.%d",
  219. pdev->id, id);
  220. else
  221. snprintf(sc->dev_id, sizeof(sc->dev_id), "sudmac%d", id);
  222. err = shdma_request_irq(schan, irq, flags, sc->dev_id);
  223. if (err) {
  224. dev_err(sdev->dma_dev.dev,
  225. "DMA channel %d request_irq failed %d\n", id, err);
  226. goto err_no_irq;
  227. }
  228. return 0;
  229. err_no_irq:
  230. /* remove from dmaengine device node */
  231. shdma_chan_remove(schan);
  232. return err;
  233. }
  234. static void sudmac_chan_remove(struct sudmac_device *su_dev)
  235. {
  236. struct shdma_chan *schan;
  237. int i;
  238. shdma_for_each_chan(schan, &su_dev->shdma_dev, i) {
  239. BUG_ON(!schan);
  240. shdma_chan_remove(schan);
  241. }
  242. }
  243. static dma_addr_t sudmac_slave_addr(struct shdma_chan *schan)
  244. {
  245. /* SUDMAC doesn't need the address */
  246. return 0;
  247. }
  248. static struct shdma_desc *sudmac_embedded_desc(void *buf, int i)
  249. {
  250. return &((struct sudmac_desc *)buf)[i].shdma_desc;
  251. }
  252. static const struct shdma_ops sudmac_shdma_ops = {
  253. .desc_completed = sudmac_desc_completed,
  254. .halt_channel = sudmac_halt,
  255. .channel_busy = sudmac_channel_busy,
  256. .slave_addr = sudmac_slave_addr,
  257. .desc_setup = sudmac_desc_setup,
  258. .set_slave = sudmac_set_slave,
  259. .setup_xfer = sudmac_setup_xfer,
  260. .start_xfer = sudmac_start_xfer,
  261. .embedded_desc = sudmac_embedded_desc,
  262. .chan_irq = sudmac_chan_irq,
  263. .get_partial = sudmac_get_partial,
  264. };
  265. static int sudmac_probe(struct platform_device *pdev)
  266. {
  267. struct sudmac_pdata *pdata = dev_get_platdata(&pdev->dev);
  268. int err, i;
  269. struct sudmac_device *su_dev;
  270. struct dma_device *dma_dev;
  271. struct resource *chan, *irq_res;
  272. /* get platform data */
  273. if (!pdata)
  274. return -ENODEV;
  275. irq_res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  276. if (!irq_res)
  277. return -ENODEV;
  278. err = -ENOMEM;
  279. su_dev = devm_kzalloc(&pdev->dev, sizeof(struct sudmac_device),
  280. GFP_KERNEL);
  281. if (!su_dev)
  282. return err;
  283. dma_dev = &su_dev->shdma_dev.dma_dev;
  284. chan = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  285. su_dev->chan_reg = devm_ioremap_resource(&pdev->dev, chan);
  286. if (IS_ERR(su_dev->chan_reg))
  287. return PTR_ERR(su_dev->chan_reg);
  288. dma_cap_set(DMA_SLAVE, dma_dev->cap_mask);
  289. su_dev->shdma_dev.ops = &sudmac_shdma_ops;
  290. su_dev->shdma_dev.desc_size = sizeof(struct sudmac_desc);
  291. err = shdma_init(&pdev->dev, &su_dev->shdma_dev, pdata->channel_num);
  292. if (err < 0)
  293. return err;
  294. /* platform data */
  295. su_dev->pdata = dev_get_platdata(&pdev->dev);
  296. platform_set_drvdata(pdev, su_dev);
  297. /* Create DMA Channel */
  298. for (i = 0; i < pdata->channel_num; i++) {
  299. err = sudmac_chan_probe(su_dev, i, irq_res->start, IRQF_SHARED);
  300. if (err)
  301. goto chan_probe_err;
  302. }
  303. err = dma_async_device_register(&su_dev->shdma_dev.dma_dev);
  304. if (err < 0)
  305. goto chan_probe_err;
  306. return err;
  307. chan_probe_err:
  308. sudmac_chan_remove(su_dev);
  309. shdma_cleanup(&su_dev->shdma_dev);
  310. return err;
  311. }
  312. static int sudmac_remove(struct platform_device *pdev)
  313. {
  314. struct sudmac_device *su_dev = platform_get_drvdata(pdev);
  315. struct dma_device *dma_dev = &su_dev->shdma_dev.dma_dev;
  316. dma_async_device_unregister(dma_dev);
  317. sudmac_chan_remove(su_dev);
  318. shdma_cleanup(&su_dev->shdma_dev);
  319. return 0;
  320. }
  321. static struct platform_driver sudmac_driver = {
  322. .driver = {
  323. .name = SUDMAC_DRV_NAME,
  324. },
  325. .probe = sudmac_probe,
  326. .remove = sudmac_remove,
  327. };
  328. module_platform_driver(sudmac_driver);
  329. MODULE_AUTHOR("Yoshihiro Shimoda");
  330. MODULE_DESCRIPTION("Renesas SUDMAC driver");
  331. MODULE_LICENSE("GPL v2");
  332. MODULE_ALIAS("platform:" SUDMAC_DRV_NAME);