sun6i-dma.c 26 KB

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