sun6i-dma.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029
  1. /*
  2. * Copyright (C) 2013-2014 Allwinner Tech Co., Ltd
  3. * Author: Sugar <shuge@allwinnertech.com>
  4. *
  5. * Copyright (C) 2014 Maxime Ripard
  6. * Maxime Ripard <maxime.ripard@free-electrons.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. */
  13. #include <linux/clk.h>
  14. #include <linux/delay.h>
  15. #include <linux/dmaengine.h>
  16. #include <linux/dmapool.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/module.h>
  19. #include <linux/of_dma.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/reset.h>
  22. #include <linux/slab.h>
  23. #include <linux/types.h>
  24. #include "virt-dma.h"
  25. /*
  26. * There's 16 physical channels that can work in parallel.
  27. *
  28. * However we have 30 different endpoints for our requests.
  29. *
  30. * Since the channels are able to handle only an unidirectional
  31. * transfer, we need to allocate more virtual channels so that
  32. * everyone can grab one channel.
  33. *
  34. * Some devices can't work in both direction (mostly because it
  35. * wouldn't make sense), so we have a bit fewer virtual channels than
  36. * 2 channels per endpoints.
  37. */
  38. #define NR_MAX_CHANNELS 16
  39. #define NR_MAX_REQUESTS 30
  40. #define NR_MAX_VCHANS 53
  41. /*
  42. * Common registers
  43. */
  44. #define DMA_IRQ_EN(x) ((x) * 0x04)
  45. #define DMA_IRQ_HALF BIT(0)
  46. #define DMA_IRQ_PKG BIT(1)
  47. #define DMA_IRQ_QUEUE BIT(2)
  48. #define DMA_IRQ_CHAN_NR 8
  49. #define DMA_IRQ_CHAN_WIDTH 4
  50. #define DMA_IRQ_STAT(x) ((x) * 0x04 + 0x10)
  51. #define DMA_STAT 0x30
  52. /*
  53. * Channels specific registers
  54. */
  55. #define DMA_CHAN_ENABLE 0x00
  56. #define DMA_CHAN_ENABLE_START BIT(0)
  57. #define DMA_CHAN_ENABLE_STOP 0
  58. #define DMA_CHAN_PAUSE 0x04
  59. #define DMA_CHAN_PAUSE_PAUSE BIT(1)
  60. #define DMA_CHAN_PAUSE_RESUME 0
  61. #define DMA_CHAN_LLI_ADDR 0x08
  62. #define DMA_CHAN_CUR_CFG 0x0c
  63. #define DMA_CHAN_CFG_SRC_DRQ(x) ((x) & 0x1f)
  64. #define DMA_CHAN_CFG_SRC_IO_MODE BIT(5)
  65. #define DMA_CHAN_CFG_SRC_LINEAR_MODE (0 << 5)
  66. #define DMA_CHAN_CFG_SRC_BURST(x) (((x) & 0x3) << 7)
  67. #define DMA_CHAN_CFG_SRC_WIDTH(x) (((x) & 0x3) << 9)
  68. #define DMA_CHAN_CFG_DST_DRQ(x) (DMA_CHAN_CFG_SRC_DRQ(x) << 16)
  69. #define DMA_CHAN_CFG_DST_IO_MODE (DMA_CHAN_CFG_SRC_IO_MODE << 16)
  70. #define DMA_CHAN_CFG_DST_LINEAR_MODE (DMA_CHAN_CFG_SRC_LINEAR_MODE << 16)
  71. #define DMA_CHAN_CFG_DST_BURST(x) (DMA_CHAN_CFG_SRC_BURST(x) << 16)
  72. #define DMA_CHAN_CFG_DST_WIDTH(x) (DMA_CHAN_CFG_SRC_WIDTH(x) << 16)
  73. #define DMA_CHAN_CUR_SRC 0x10
  74. #define DMA_CHAN_CUR_DST 0x14
  75. #define DMA_CHAN_CUR_CNT 0x18
  76. #define DMA_CHAN_CUR_PARA 0x1c
  77. /*
  78. * Various hardware related defines
  79. */
  80. #define LLI_LAST_ITEM 0xfffff800
  81. #define NORMAL_WAIT 8
  82. #define DRQ_SDRAM 1
  83. /*
  84. * Hardware representation of the LLI
  85. *
  86. * The hardware will be fed the physical address of this structure,
  87. * and read its content in order to start the transfer.
  88. */
  89. struct sun6i_dma_lli {
  90. u32 cfg;
  91. u32 src;
  92. u32 dst;
  93. u32 len;
  94. u32 para;
  95. u32 p_lli_next;
  96. /*
  97. * This field is not used by the DMA controller, but will be
  98. * used by the CPU to go through the list (mostly for dumping
  99. * or freeing it).
  100. */
  101. struct sun6i_dma_lli *v_lli_next;
  102. };
  103. struct sun6i_desc {
  104. struct virt_dma_desc vd;
  105. dma_addr_t p_lli;
  106. struct sun6i_dma_lli *v_lli;
  107. };
  108. struct sun6i_pchan {
  109. u32 idx;
  110. void __iomem *base;
  111. struct sun6i_vchan *vchan;
  112. struct sun6i_desc *desc;
  113. struct sun6i_desc *done;
  114. };
  115. struct sun6i_vchan {
  116. struct virt_dma_chan vc;
  117. struct list_head node;
  118. struct dma_slave_config cfg;
  119. struct sun6i_pchan *phy;
  120. u8 port;
  121. };
  122. struct sun6i_dma_dev {
  123. struct dma_device slave;
  124. void __iomem *base;
  125. struct clk *clk;
  126. int irq;
  127. spinlock_t lock;
  128. struct reset_control *rstc;
  129. struct tasklet_struct task;
  130. atomic_t tasklet_shutdown;
  131. struct list_head pending;
  132. struct dma_pool *pool;
  133. struct sun6i_pchan *pchans;
  134. struct sun6i_vchan *vchans;
  135. };
  136. static struct device *chan2dev(struct dma_chan *chan)
  137. {
  138. return &chan->dev->device;
  139. }
  140. static inline struct sun6i_dma_dev *to_sun6i_dma_dev(struct dma_device *d)
  141. {
  142. return container_of(d, struct sun6i_dma_dev, slave);
  143. }
  144. static inline struct sun6i_vchan *to_sun6i_vchan(struct dma_chan *chan)
  145. {
  146. return container_of(chan, struct sun6i_vchan, vc.chan);
  147. }
  148. static inline struct sun6i_desc *
  149. to_sun6i_desc(struct dma_async_tx_descriptor *tx)
  150. {
  151. return container_of(tx, struct sun6i_desc, vd.tx);
  152. }
  153. static inline void sun6i_dma_dump_com_regs(struct sun6i_dma_dev *sdev)
  154. {
  155. dev_dbg(sdev->slave.dev, "Common register:\n"
  156. "\tmask0(%04x): 0x%08x\n"
  157. "\tmask1(%04x): 0x%08x\n"
  158. "\tpend0(%04x): 0x%08x\n"
  159. "\tpend1(%04x): 0x%08x\n"
  160. "\tstats(%04x): 0x%08x\n",
  161. DMA_IRQ_EN(0), readl(sdev->base + DMA_IRQ_EN(0)),
  162. DMA_IRQ_EN(1), readl(sdev->base + DMA_IRQ_EN(1)),
  163. DMA_IRQ_STAT(0), readl(sdev->base + DMA_IRQ_STAT(0)),
  164. DMA_IRQ_STAT(1), readl(sdev->base + DMA_IRQ_STAT(1)),
  165. DMA_STAT, readl(sdev->base + DMA_STAT));
  166. }
  167. static inline void sun6i_dma_dump_chan_regs(struct sun6i_dma_dev *sdev,
  168. struct sun6i_pchan *pchan)
  169. {
  170. phys_addr_t reg = virt_to_phys(pchan->base);
  171. dev_dbg(sdev->slave.dev, "Chan %d reg: %pa\n"
  172. "\t___en(%04x): \t0x%08x\n"
  173. "\tpause(%04x): \t0x%08x\n"
  174. "\tstart(%04x): \t0x%08x\n"
  175. "\t__cfg(%04x): \t0x%08x\n"
  176. "\t__src(%04x): \t0x%08x\n"
  177. "\t__dst(%04x): \t0x%08x\n"
  178. "\tcount(%04x): \t0x%08x\n"
  179. "\t_para(%04x): \t0x%08x\n\n",
  180. pchan->idx, &reg,
  181. DMA_CHAN_ENABLE,
  182. readl(pchan->base + DMA_CHAN_ENABLE),
  183. DMA_CHAN_PAUSE,
  184. readl(pchan->base + DMA_CHAN_PAUSE),
  185. DMA_CHAN_LLI_ADDR,
  186. readl(pchan->base + DMA_CHAN_LLI_ADDR),
  187. DMA_CHAN_CUR_CFG,
  188. readl(pchan->base + DMA_CHAN_CUR_CFG),
  189. DMA_CHAN_CUR_SRC,
  190. readl(pchan->base + DMA_CHAN_CUR_SRC),
  191. DMA_CHAN_CUR_DST,
  192. readl(pchan->base + DMA_CHAN_CUR_DST),
  193. DMA_CHAN_CUR_CNT,
  194. readl(pchan->base + DMA_CHAN_CUR_CNT),
  195. DMA_CHAN_CUR_PARA,
  196. readl(pchan->base + DMA_CHAN_CUR_PARA));
  197. }
  198. static inline s8 convert_burst(u32 maxburst)
  199. {
  200. switch (maxburst) {
  201. case 1:
  202. return 0;
  203. case 8:
  204. return 2;
  205. default:
  206. return -EINVAL;
  207. }
  208. }
  209. static inline s8 convert_buswidth(enum dma_slave_buswidth addr_width)
  210. {
  211. if ((addr_width < DMA_SLAVE_BUSWIDTH_1_BYTE) ||
  212. (addr_width > DMA_SLAVE_BUSWIDTH_4_BYTES))
  213. return -EINVAL;
  214. return addr_width >> 1;
  215. }
  216. static void *sun6i_dma_lli_add(struct sun6i_dma_lli *prev,
  217. struct sun6i_dma_lli *next,
  218. dma_addr_t next_phy,
  219. struct sun6i_desc *txd)
  220. {
  221. if ((!prev && !txd) || !next)
  222. return NULL;
  223. if (!prev) {
  224. txd->p_lli = next_phy;
  225. txd->v_lli = next;
  226. } else {
  227. prev->p_lli_next = next_phy;
  228. prev->v_lli_next = next;
  229. }
  230. next->p_lli_next = LLI_LAST_ITEM;
  231. next->v_lli_next = NULL;
  232. return next;
  233. }
  234. static inline int sun6i_dma_cfg_lli(struct sun6i_dma_lli *lli,
  235. dma_addr_t src,
  236. dma_addr_t dst, u32 len,
  237. struct dma_slave_config *config)
  238. {
  239. u8 src_width, dst_width, src_burst, dst_burst;
  240. if (!config)
  241. return -EINVAL;
  242. src_burst = convert_burst(config->src_maxburst);
  243. if (src_burst)
  244. return src_burst;
  245. dst_burst = convert_burst(config->dst_maxburst);
  246. if (dst_burst)
  247. return dst_burst;
  248. src_width = convert_buswidth(config->src_addr_width);
  249. if (src_width)
  250. return src_width;
  251. dst_width = convert_buswidth(config->dst_addr_width);
  252. if (dst_width)
  253. return dst_width;
  254. lli->cfg = DMA_CHAN_CFG_SRC_BURST(src_burst) |
  255. DMA_CHAN_CFG_SRC_WIDTH(src_width) |
  256. DMA_CHAN_CFG_DST_BURST(dst_burst) |
  257. DMA_CHAN_CFG_DST_WIDTH(dst_width);
  258. lli->src = src;
  259. lli->dst = dst;
  260. lli->len = len;
  261. lli->para = NORMAL_WAIT;
  262. return 0;
  263. }
  264. static inline void sun6i_dma_dump_lli(struct sun6i_vchan *vchan,
  265. struct sun6i_dma_lli *lli)
  266. {
  267. phys_addr_t p_lli = virt_to_phys(lli);
  268. dev_dbg(chan2dev(&vchan->vc.chan),
  269. "\n\tdesc: p - %pa v - 0x%p\n"
  270. "\t\tc - 0x%08x s - 0x%08x d - 0x%08x\n"
  271. "\t\tl - 0x%08x p - 0x%08x n - 0x%08x\n",
  272. &p_lli, lli,
  273. lli->cfg, lli->src, lli->dst,
  274. lli->len, lli->para, lli->p_lli_next);
  275. }
  276. static void sun6i_dma_free_desc(struct virt_dma_desc *vd)
  277. {
  278. struct sun6i_desc *txd = to_sun6i_desc(&vd->tx);
  279. struct sun6i_dma_dev *sdev = to_sun6i_dma_dev(vd->tx.chan->device);
  280. struct sun6i_dma_lli *v_lli, *v_next;
  281. dma_addr_t p_lli, p_next;
  282. if (unlikely(!txd))
  283. return;
  284. p_lli = txd->p_lli;
  285. v_lli = txd->v_lli;
  286. while (v_lli) {
  287. v_next = v_lli->v_lli_next;
  288. p_next = v_lli->p_lli_next;
  289. dma_pool_free(sdev->pool, v_lli, p_lli);
  290. v_lli = v_next;
  291. p_lli = p_next;
  292. }
  293. kfree(txd);
  294. }
  295. static int sun6i_dma_terminate_all(struct sun6i_vchan *vchan)
  296. {
  297. struct sun6i_dma_dev *sdev = to_sun6i_dma_dev(vchan->vc.chan.device);
  298. struct sun6i_pchan *pchan = vchan->phy;
  299. unsigned long flags;
  300. LIST_HEAD(head);
  301. spin_lock(&sdev->lock);
  302. list_del_init(&vchan->node);
  303. spin_unlock(&sdev->lock);
  304. spin_lock_irqsave(&vchan->vc.lock, flags);
  305. vchan_get_all_descriptors(&vchan->vc, &head);
  306. if (pchan) {
  307. writel(DMA_CHAN_ENABLE_STOP, pchan->base + DMA_CHAN_ENABLE);
  308. writel(DMA_CHAN_PAUSE_RESUME, pchan->base + DMA_CHAN_PAUSE);
  309. vchan->phy = NULL;
  310. pchan->vchan = NULL;
  311. pchan->desc = NULL;
  312. pchan->done = NULL;
  313. }
  314. spin_unlock_irqrestore(&vchan->vc.lock, flags);
  315. vchan_dma_desc_free_list(&vchan->vc, &head);
  316. return 0;
  317. }
  318. static int sun6i_dma_start_desc(struct sun6i_vchan *vchan)
  319. {
  320. struct sun6i_dma_dev *sdev = to_sun6i_dma_dev(vchan->vc.chan.device);
  321. struct virt_dma_desc *desc = vchan_next_desc(&vchan->vc);
  322. struct sun6i_pchan *pchan = vchan->phy;
  323. u32 irq_val, irq_reg, irq_offset;
  324. if (!pchan)
  325. return -EAGAIN;
  326. if (!desc) {
  327. pchan->desc = NULL;
  328. pchan->done = NULL;
  329. return -EAGAIN;
  330. }
  331. list_del(&desc->node);
  332. pchan->desc = to_sun6i_desc(&desc->tx);
  333. pchan->done = NULL;
  334. sun6i_dma_dump_lli(vchan, pchan->desc->v_lli);
  335. irq_reg = pchan->idx / DMA_IRQ_CHAN_NR;
  336. irq_offset = pchan->idx % DMA_IRQ_CHAN_NR;
  337. irq_val = readl(sdev->base + DMA_IRQ_EN(irq_offset));
  338. irq_val |= DMA_IRQ_QUEUE << (irq_offset * DMA_IRQ_CHAN_WIDTH);
  339. writel(irq_val, sdev->base + DMA_IRQ_EN(irq_offset));
  340. writel(pchan->desc->p_lli, pchan->base + DMA_CHAN_LLI_ADDR);
  341. writel(DMA_CHAN_ENABLE_START, pchan->base + DMA_CHAN_ENABLE);
  342. sun6i_dma_dump_com_regs(sdev);
  343. sun6i_dma_dump_chan_regs(sdev, pchan);
  344. return 0;
  345. }
  346. static void sun6i_dma_tasklet(unsigned long data)
  347. {
  348. struct sun6i_dma_dev *sdev = (struct sun6i_dma_dev *)data;
  349. struct sun6i_vchan *vchan;
  350. struct sun6i_pchan *pchan;
  351. unsigned int pchan_alloc = 0;
  352. unsigned int pchan_idx;
  353. list_for_each_entry(vchan, &sdev->slave.channels, vc.chan.device_node) {
  354. spin_lock_irq(&vchan->vc.lock);
  355. pchan = vchan->phy;
  356. if (pchan && pchan->done) {
  357. if (sun6i_dma_start_desc(vchan)) {
  358. /*
  359. * No current txd associated with this channel
  360. */
  361. dev_dbg(sdev->slave.dev, "pchan %u: free\n",
  362. pchan->idx);
  363. /* Mark this channel free */
  364. vchan->phy = NULL;
  365. pchan->vchan = NULL;
  366. }
  367. }
  368. spin_unlock_irq(&vchan->vc.lock);
  369. }
  370. spin_lock_irq(&sdev->lock);
  371. for (pchan_idx = 0; pchan_idx < NR_MAX_CHANNELS; pchan_idx++) {
  372. pchan = &sdev->pchans[pchan_idx];
  373. if (pchan->vchan || list_empty(&sdev->pending))
  374. continue;
  375. vchan = list_first_entry(&sdev->pending,
  376. struct sun6i_vchan, node);
  377. /* Remove from pending channels */
  378. list_del_init(&vchan->node);
  379. pchan_alloc |= BIT(pchan_idx);
  380. /* Mark this channel allocated */
  381. pchan->vchan = vchan;
  382. vchan->phy = pchan;
  383. dev_dbg(sdev->slave.dev, "pchan %u: alloc vchan %p\n",
  384. pchan->idx, &vchan->vc);
  385. }
  386. spin_unlock_irq(&sdev->lock);
  387. for (pchan_idx = 0; pchan_idx < NR_MAX_CHANNELS; pchan_idx++) {
  388. if (!(pchan_alloc & BIT(pchan_idx)))
  389. continue;
  390. pchan = sdev->pchans + pchan_idx;
  391. vchan = pchan->vchan;
  392. if (vchan) {
  393. spin_lock_irq(&vchan->vc.lock);
  394. sun6i_dma_start_desc(vchan);
  395. spin_unlock_irq(&vchan->vc.lock);
  396. }
  397. }
  398. }
  399. static irqreturn_t sun6i_dma_interrupt(int irq, void *dev_id)
  400. {
  401. struct sun6i_dma_dev *sdev = dev_id;
  402. struct sun6i_vchan *vchan;
  403. struct sun6i_pchan *pchan;
  404. int i, j, ret = IRQ_NONE;
  405. u32 status;
  406. for (i = 0; i < 2; i++) {
  407. status = readl(sdev->base + DMA_IRQ_STAT(i));
  408. if (!status)
  409. continue;
  410. dev_dbg(sdev->slave.dev, "DMA irq status %s: 0x%x\n",
  411. i ? "high" : "low", status);
  412. writel(status, sdev->base + DMA_IRQ_STAT(i));
  413. for (j = 0; (j < 8) && status; j++) {
  414. if (status & DMA_IRQ_QUEUE) {
  415. pchan = sdev->pchans + j;
  416. vchan = pchan->vchan;
  417. if (vchan) {
  418. spin_lock(&vchan->vc.lock);
  419. vchan_cookie_complete(&pchan->desc->vd);
  420. pchan->done = pchan->desc;
  421. spin_unlock(&vchan->vc.lock);
  422. }
  423. }
  424. status = status >> 4;
  425. }
  426. if (!atomic_read(&sdev->tasklet_shutdown))
  427. tasklet_schedule(&sdev->task);
  428. ret = IRQ_HANDLED;
  429. }
  430. return ret;
  431. }
  432. static struct dma_async_tx_descriptor *sun6i_dma_prep_dma_memcpy(
  433. struct dma_chan *chan, dma_addr_t dest, dma_addr_t src,
  434. size_t len, unsigned long flags)
  435. {
  436. struct sun6i_dma_dev *sdev = to_sun6i_dma_dev(chan->device);
  437. struct sun6i_vchan *vchan = to_sun6i_vchan(chan);
  438. struct sun6i_dma_lli *v_lli;
  439. struct sun6i_desc *txd;
  440. dma_addr_t p_lli;
  441. s8 burst, width;
  442. dev_dbg(chan2dev(chan),
  443. "%s; chan: %d, dest: %pad, src: %pad, len: %zu. flags: 0x%08lx\n",
  444. __func__, vchan->vc.chan.chan_id, &dest, &src, len, flags);
  445. if (!len)
  446. return NULL;
  447. txd = kzalloc(sizeof(*txd), GFP_NOWAIT);
  448. if (!txd)
  449. return NULL;
  450. v_lli = dma_pool_alloc(sdev->pool, GFP_NOWAIT, &p_lli);
  451. if (!v_lli) {
  452. dev_err(sdev->slave.dev, "Failed to alloc lli memory\n");
  453. goto err_txd_free;
  454. }
  455. v_lli->src = src;
  456. v_lli->dst = dest;
  457. v_lli->len = len;
  458. v_lli->para = NORMAL_WAIT;
  459. burst = convert_burst(8);
  460. width = convert_buswidth(DMA_SLAVE_BUSWIDTH_4_BYTES);
  461. v_lli->cfg |= DMA_CHAN_CFG_SRC_DRQ(DRQ_SDRAM) |
  462. DMA_CHAN_CFG_DST_DRQ(DRQ_SDRAM) |
  463. DMA_CHAN_CFG_DST_LINEAR_MODE |
  464. DMA_CHAN_CFG_SRC_LINEAR_MODE |
  465. DMA_CHAN_CFG_SRC_BURST(burst) |
  466. DMA_CHAN_CFG_SRC_WIDTH(width) |
  467. DMA_CHAN_CFG_DST_BURST(burst) |
  468. DMA_CHAN_CFG_DST_WIDTH(width);
  469. sun6i_dma_lli_add(NULL, v_lli, p_lli, txd);
  470. sun6i_dma_dump_lli(vchan, v_lli);
  471. return vchan_tx_prep(&vchan->vc, &txd->vd, flags);
  472. err_txd_free:
  473. kfree(txd);
  474. return NULL;
  475. }
  476. static struct dma_async_tx_descriptor *sun6i_dma_prep_slave_sg(
  477. struct dma_chan *chan, struct scatterlist *sgl,
  478. unsigned int sg_len, enum dma_transfer_direction dir,
  479. unsigned long flags, void *context)
  480. {
  481. struct sun6i_dma_dev *sdev = to_sun6i_dma_dev(chan->device);
  482. struct sun6i_vchan *vchan = to_sun6i_vchan(chan);
  483. struct dma_slave_config *sconfig = &vchan->cfg;
  484. struct sun6i_dma_lli *v_lli, *prev = NULL;
  485. struct sun6i_desc *txd;
  486. struct scatterlist *sg;
  487. dma_addr_t p_lli;
  488. int i, ret;
  489. if (!sgl)
  490. return NULL;
  491. if (!is_slave_direction(dir)) {
  492. dev_err(chan2dev(chan), "Invalid DMA direction\n");
  493. return NULL;
  494. }
  495. txd = kzalloc(sizeof(*txd), GFP_NOWAIT);
  496. if (!txd)
  497. return NULL;
  498. for_each_sg(sgl, sg, sg_len, i) {
  499. v_lli = dma_pool_alloc(sdev->pool, GFP_NOWAIT, &p_lli);
  500. if (!v_lli)
  501. goto err_lli_free;
  502. if (dir == DMA_MEM_TO_DEV) {
  503. ret = sun6i_dma_cfg_lli(v_lli, sg_dma_address(sg),
  504. sconfig->dst_addr, sg_dma_len(sg),
  505. sconfig);
  506. if (ret)
  507. goto err_cur_lli_free;
  508. v_lli->cfg |= DMA_CHAN_CFG_DST_IO_MODE |
  509. DMA_CHAN_CFG_SRC_LINEAR_MODE |
  510. DMA_CHAN_CFG_SRC_DRQ(DRQ_SDRAM) |
  511. DMA_CHAN_CFG_DST_DRQ(vchan->port);
  512. dev_dbg(chan2dev(chan),
  513. "%s; chan: %d, dest: %pad, src: %pad, len: %u. flags: 0x%08lx\n",
  514. __func__, vchan->vc.chan.chan_id,
  515. &sconfig->dst_addr, &sg_dma_address(sg),
  516. sg_dma_len(sg), flags);
  517. } else {
  518. ret = sun6i_dma_cfg_lli(v_lli, sconfig->src_addr,
  519. sg_dma_address(sg), sg_dma_len(sg),
  520. sconfig);
  521. if (ret)
  522. goto err_cur_lli_free;
  523. v_lli->cfg |= DMA_CHAN_CFG_DST_LINEAR_MODE |
  524. DMA_CHAN_CFG_SRC_IO_MODE |
  525. DMA_CHAN_CFG_DST_DRQ(DRQ_SDRAM) |
  526. DMA_CHAN_CFG_SRC_DRQ(vchan->port);
  527. dev_dbg(chan2dev(chan),
  528. "%s; chan: %d, dest: %pad, src: %pad, len: %u. flags: 0x%08lx\n",
  529. __func__, vchan->vc.chan.chan_id,
  530. &sg_dma_address(sg), &sconfig->src_addr,
  531. sg_dma_len(sg), flags);
  532. }
  533. prev = sun6i_dma_lli_add(prev, v_lli, p_lli, txd);
  534. }
  535. dev_dbg(chan2dev(chan), "First: %pad\n", &txd->p_lli);
  536. for (prev = txd->v_lli; prev; prev = prev->v_lli_next)
  537. sun6i_dma_dump_lli(vchan, prev);
  538. return vchan_tx_prep(&vchan->vc, &txd->vd, flags);
  539. err_cur_lli_free:
  540. dma_pool_free(sdev->pool, v_lli, p_lli);
  541. err_lli_free:
  542. for (prev = txd->v_lli; prev; prev = prev->v_lli_next)
  543. dma_pool_free(sdev->pool, prev, virt_to_phys(prev));
  544. kfree(txd);
  545. return NULL;
  546. }
  547. static int sun6i_dma_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd,
  548. unsigned long arg)
  549. {
  550. struct sun6i_dma_dev *sdev = to_sun6i_dma_dev(chan->device);
  551. struct sun6i_vchan *vchan = to_sun6i_vchan(chan);
  552. struct sun6i_pchan *pchan = vchan->phy;
  553. unsigned long flags;
  554. int ret = 0;
  555. switch (cmd) {
  556. case DMA_RESUME:
  557. dev_dbg(chan2dev(chan), "vchan %p: resume\n", &vchan->vc);
  558. spin_lock_irqsave(&vchan->vc.lock, flags);
  559. if (pchan) {
  560. writel(DMA_CHAN_PAUSE_RESUME,
  561. pchan->base + DMA_CHAN_PAUSE);
  562. } else if (!list_empty(&vchan->vc.desc_issued)) {
  563. spin_lock(&sdev->lock);
  564. list_add_tail(&vchan->node, &sdev->pending);
  565. spin_unlock(&sdev->lock);
  566. }
  567. spin_unlock_irqrestore(&vchan->vc.lock, flags);
  568. break;
  569. case DMA_PAUSE:
  570. dev_dbg(chan2dev(chan), "vchan %p: pause\n", &vchan->vc);
  571. if (pchan) {
  572. writel(DMA_CHAN_PAUSE_PAUSE,
  573. pchan->base + DMA_CHAN_PAUSE);
  574. } else {
  575. spin_lock(&sdev->lock);
  576. list_del_init(&vchan->node);
  577. spin_unlock(&sdev->lock);
  578. }
  579. break;
  580. case DMA_TERMINATE_ALL:
  581. ret = sun6i_dma_terminate_all(vchan);
  582. break;
  583. case DMA_SLAVE_CONFIG:
  584. memcpy(&vchan->cfg, (void *)arg, sizeof(struct dma_slave_config));
  585. break;
  586. default:
  587. ret = -ENXIO;
  588. break;
  589. }
  590. return ret;
  591. }
  592. static enum dma_status sun6i_dma_tx_status(struct dma_chan *chan,
  593. dma_cookie_t cookie,
  594. struct dma_tx_state *state)
  595. {
  596. struct sun6i_vchan *vchan = to_sun6i_vchan(chan);
  597. struct sun6i_pchan *pchan = vchan->phy;
  598. struct sun6i_dma_lli *lli;
  599. struct virt_dma_desc *vd;
  600. struct sun6i_desc *txd;
  601. enum dma_status ret;
  602. unsigned long flags;
  603. size_t bytes = 0;
  604. ret = dma_cookie_status(chan, cookie, state);
  605. if (ret == DMA_COMPLETE)
  606. return ret;
  607. spin_lock_irqsave(&vchan->vc.lock, flags);
  608. vd = vchan_find_desc(&vchan->vc, cookie);
  609. txd = to_sun6i_desc(&vd->tx);
  610. if (vd) {
  611. for (lli = txd->v_lli; lli != NULL; lli = lli->v_lli_next)
  612. bytes += lli->len;
  613. } else if (!pchan || !pchan->desc) {
  614. bytes = 0;
  615. } else {
  616. bytes = readl(pchan->base + DMA_CHAN_CUR_CNT);
  617. }
  618. spin_unlock_irqrestore(&vchan->vc.lock, flags);
  619. dma_set_residue(state, bytes);
  620. return ret;
  621. }
  622. static void sun6i_dma_issue_pending(struct dma_chan *chan)
  623. {
  624. struct sun6i_dma_dev *sdev = to_sun6i_dma_dev(chan->device);
  625. struct sun6i_vchan *vchan = to_sun6i_vchan(chan);
  626. unsigned long flags;
  627. spin_lock_irqsave(&vchan->vc.lock, flags);
  628. if (vchan_issue_pending(&vchan->vc)) {
  629. spin_lock(&sdev->lock);
  630. if (!vchan->phy && list_empty(&vchan->node)) {
  631. list_add_tail(&vchan->node, &sdev->pending);
  632. tasklet_schedule(&sdev->task);
  633. dev_dbg(chan2dev(chan), "vchan %p: issued\n",
  634. &vchan->vc);
  635. }
  636. spin_unlock(&sdev->lock);
  637. } else {
  638. dev_dbg(chan2dev(chan), "vchan %p: nothing to issue\n",
  639. &vchan->vc);
  640. }
  641. spin_unlock_irqrestore(&vchan->vc.lock, flags);
  642. }
  643. static int sun6i_dma_alloc_chan_resources(struct dma_chan *chan)
  644. {
  645. return 0;
  646. }
  647. static void sun6i_dma_free_chan_resources(struct dma_chan *chan)
  648. {
  649. struct sun6i_dma_dev *sdev = to_sun6i_dma_dev(chan->device);
  650. struct sun6i_vchan *vchan = to_sun6i_vchan(chan);
  651. unsigned long flags;
  652. spin_lock_irqsave(&sdev->lock, flags);
  653. list_del_init(&vchan->node);
  654. spin_unlock_irqrestore(&sdev->lock, flags);
  655. vchan_free_chan_resources(&vchan->vc);
  656. }
  657. static struct dma_chan *sun6i_dma_of_xlate(struct of_phandle_args *dma_spec,
  658. struct of_dma *ofdma)
  659. {
  660. struct sun6i_dma_dev *sdev = ofdma->of_dma_data;
  661. struct sun6i_vchan *vchan;
  662. struct dma_chan *chan;
  663. u8 port = dma_spec->args[0];
  664. if (port > NR_MAX_REQUESTS)
  665. return NULL;
  666. chan = dma_get_any_slave_channel(&sdev->slave);
  667. if (!chan)
  668. return NULL;
  669. vchan = to_sun6i_vchan(chan);
  670. vchan->port = port;
  671. return chan;
  672. }
  673. static inline void sun6i_kill_tasklet(struct sun6i_dma_dev *sdev)
  674. {
  675. /* Disable all interrupts from DMA */
  676. writel(0, sdev->base + DMA_IRQ_EN(0));
  677. writel(0, sdev->base + DMA_IRQ_EN(1));
  678. /* Prevent spurious interrupts from scheduling the tasklet */
  679. atomic_inc(&sdev->tasklet_shutdown);
  680. /* Make sure we won't have any further interrupts */
  681. devm_free_irq(sdev->slave.dev, sdev->irq, sdev);
  682. /* Actually prevent the tasklet from being scheduled */
  683. tasklet_kill(&sdev->task);
  684. }
  685. static inline void sun6i_dma_free(struct sun6i_dma_dev *sdev)
  686. {
  687. int i;
  688. for (i = 0; i < NR_MAX_VCHANS; i++) {
  689. struct sun6i_vchan *vchan = &sdev->vchans[i];
  690. list_del(&vchan->vc.chan.device_node);
  691. tasklet_kill(&vchan->vc.task);
  692. }
  693. }
  694. static int sun6i_dma_probe(struct platform_device *pdev)
  695. {
  696. struct sun6i_dma_dev *sdc;
  697. struct resource *res;
  698. int ret, i;
  699. sdc = devm_kzalloc(&pdev->dev, sizeof(*sdc), GFP_KERNEL);
  700. if (!sdc)
  701. return -ENOMEM;
  702. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  703. sdc->base = devm_ioremap_resource(&pdev->dev, res);
  704. if (IS_ERR(sdc->base))
  705. return PTR_ERR(sdc->base);
  706. sdc->irq = platform_get_irq(pdev, 0);
  707. if (sdc->irq < 0) {
  708. dev_err(&pdev->dev, "Cannot claim IRQ\n");
  709. return sdc->irq;
  710. }
  711. sdc->clk = devm_clk_get(&pdev->dev, NULL);
  712. if (IS_ERR(sdc->clk)) {
  713. dev_err(&pdev->dev, "No clock specified\n");
  714. return PTR_ERR(sdc->clk);
  715. }
  716. sdc->rstc = devm_reset_control_get(&pdev->dev, NULL);
  717. if (IS_ERR(sdc->rstc)) {
  718. dev_err(&pdev->dev, "No reset controller specified\n");
  719. return PTR_ERR(sdc->rstc);
  720. }
  721. sdc->pool = dmam_pool_create(dev_name(&pdev->dev), &pdev->dev,
  722. sizeof(struct sun6i_dma_lli), 4, 0);
  723. if (!sdc->pool) {
  724. dev_err(&pdev->dev, "No memory for descriptors dma pool\n");
  725. return -ENOMEM;
  726. }
  727. platform_set_drvdata(pdev, sdc);
  728. INIT_LIST_HEAD(&sdc->pending);
  729. spin_lock_init(&sdc->lock);
  730. dma_cap_set(DMA_PRIVATE, sdc->slave.cap_mask);
  731. dma_cap_set(DMA_MEMCPY, sdc->slave.cap_mask);
  732. dma_cap_set(DMA_SLAVE, sdc->slave.cap_mask);
  733. INIT_LIST_HEAD(&sdc->slave.channels);
  734. sdc->slave.device_alloc_chan_resources = sun6i_dma_alloc_chan_resources;
  735. sdc->slave.device_free_chan_resources = sun6i_dma_free_chan_resources;
  736. sdc->slave.device_tx_status = sun6i_dma_tx_status;
  737. sdc->slave.device_issue_pending = sun6i_dma_issue_pending;
  738. sdc->slave.device_prep_slave_sg = sun6i_dma_prep_slave_sg;
  739. sdc->slave.device_prep_dma_memcpy = sun6i_dma_prep_dma_memcpy;
  740. sdc->slave.device_control = sun6i_dma_control;
  741. sdc->slave.chancnt = NR_MAX_VCHANS;
  742. sdc->slave.copy_align = 4;
  743. sdc->slave.dev = &pdev->dev;
  744. sdc->pchans = devm_kcalloc(&pdev->dev, NR_MAX_CHANNELS,
  745. sizeof(struct sun6i_pchan), GFP_KERNEL);
  746. if (!sdc->pchans)
  747. return -ENOMEM;
  748. sdc->vchans = devm_kcalloc(&pdev->dev, NR_MAX_VCHANS,
  749. sizeof(struct sun6i_vchan), GFP_KERNEL);
  750. if (!sdc->vchans)
  751. return -ENOMEM;
  752. tasklet_init(&sdc->task, sun6i_dma_tasklet, (unsigned long)sdc);
  753. for (i = 0; i < NR_MAX_CHANNELS; i++) {
  754. struct sun6i_pchan *pchan = &sdc->pchans[i];
  755. pchan->idx = i;
  756. pchan->base = sdc->base + 0x100 + i * 0x40;
  757. }
  758. for (i = 0; i < NR_MAX_VCHANS; i++) {
  759. struct sun6i_vchan *vchan = &sdc->vchans[i];
  760. INIT_LIST_HEAD(&vchan->node);
  761. vchan->vc.desc_free = sun6i_dma_free_desc;
  762. vchan_init(&vchan->vc, &sdc->slave);
  763. }
  764. ret = reset_control_deassert(sdc->rstc);
  765. if (ret) {
  766. dev_err(&pdev->dev, "Couldn't deassert the device from reset\n");
  767. goto err_chan_free;
  768. }
  769. ret = clk_prepare_enable(sdc->clk);
  770. if (ret) {
  771. dev_err(&pdev->dev, "Couldn't enable the clock\n");
  772. goto err_reset_assert;
  773. }
  774. ret = devm_request_irq(&pdev->dev, sdc->irq, sun6i_dma_interrupt, 0,
  775. dev_name(&pdev->dev), sdc);
  776. if (ret) {
  777. dev_err(&pdev->dev, "Cannot request IRQ\n");
  778. goto err_clk_disable;
  779. }
  780. ret = dma_async_device_register(&sdc->slave);
  781. if (ret) {
  782. dev_warn(&pdev->dev, "Failed to register DMA engine device\n");
  783. goto err_irq_disable;
  784. }
  785. ret = of_dma_controller_register(pdev->dev.of_node, sun6i_dma_of_xlate,
  786. sdc);
  787. if (ret) {
  788. dev_err(&pdev->dev, "of_dma_controller_register failed\n");
  789. goto err_dma_unregister;
  790. }
  791. return 0;
  792. err_dma_unregister:
  793. dma_async_device_unregister(&sdc->slave);
  794. err_irq_disable:
  795. sun6i_kill_tasklet(sdc);
  796. err_clk_disable:
  797. clk_disable_unprepare(sdc->clk);
  798. err_reset_assert:
  799. reset_control_assert(sdc->rstc);
  800. err_chan_free:
  801. sun6i_dma_free(sdc);
  802. return ret;
  803. }
  804. static int sun6i_dma_remove(struct platform_device *pdev)
  805. {
  806. struct sun6i_dma_dev *sdc = platform_get_drvdata(pdev);
  807. of_dma_controller_free(pdev->dev.of_node);
  808. dma_async_device_unregister(&sdc->slave);
  809. sun6i_kill_tasklet(sdc);
  810. clk_disable_unprepare(sdc->clk);
  811. reset_control_assert(sdc->rstc);
  812. sun6i_dma_free(sdc);
  813. return 0;
  814. }
  815. static struct of_device_id sun6i_dma_match[] = {
  816. { .compatible = "allwinner,sun6i-a31-dma" },
  817. { /* sentinel */ }
  818. };
  819. static struct platform_driver sun6i_dma_driver = {
  820. .probe = sun6i_dma_probe,
  821. .remove = sun6i_dma_remove,
  822. .driver = {
  823. .name = "sun6i-dma",
  824. .of_match_table = sun6i_dma_match,
  825. },
  826. };
  827. module_platform_driver(sun6i_dma_driver);
  828. MODULE_DESCRIPTION("Allwinner A31 DMA Controller Driver");
  829. MODULE_AUTHOR("Sugar <shuge@allwinnertech.com>");
  830. MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>");
  831. MODULE_LICENSE("GPL");