sun6i-dma.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102
  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_start_desc(struct sun6i_vchan *vchan)
  299. {
  300. struct sun6i_dma_dev *sdev = to_sun6i_dma_dev(vchan->vc.chan.device);
  301. struct virt_dma_desc *desc = vchan_next_desc(&vchan->vc);
  302. struct sun6i_pchan *pchan = vchan->phy;
  303. u32 irq_val, irq_reg, irq_offset;
  304. if (!pchan)
  305. return -EAGAIN;
  306. if (!desc) {
  307. pchan->desc = NULL;
  308. pchan->done = NULL;
  309. return -EAGAIN;
  310. }
  311. list_del(&desc->node);
  312. pchan->desc = to_sun6i_desc(&desc->tx);
  313. pchan->done = NULL;
  314. sun6i_dma_dump_lli(vchan, pchan->desc->v_lli);
  315. irq_reg = pchan->idx / DMA_IRQ_CHAN_NR;
  316. irq_offset = pchan->idx % DMA_IRQ_CHAN_NR;
  317. irq_val = readl(sdev->base + DMA_IRQ_EN(irq_offset));
  318. irq_val |= DMA_IRQ_QUEUE << (irq_offset * DMA_IRQ_CHAN_WIDTH);
  319. writel(irq_val, sdev->base + DMA_IRQ_EN(irq_offset));
  320. writel(pchan->desc->p_lli, pchan->base + DMA_CHAN_LLI_ADDR);
  321. writel(DMA_CHAN_ENABLE_START, pchan->base + DMA_CHAN_ENABLE);
  322. sun6i_dma_dump_com_regs(sdev);
  323. sun6i_dma_dump_chan_regs(sdev, pchan);
  324. return 0;
  325. }
  326. static void sun6i_dma_tasklet(unsigned long data)
  327. {
  328. struct sun6i_dma_dev *sdev = (struct sun6i_dma_dev *)data;
  329. const struct sun6i_dma_config *cfg = sdev->cfg;
  330. struct sun6i_vchan *vchan;
  331. struct sun6i_pchan *pchan;
  332. unsigned int pchan_alloc = 0;
  333. unsigned int pchan_idx;
  334. list_for_each_entry(vchan, &sdev->slave.channels, vc.chan.device_node) {
  335. spin_lock_irq(&vchan->vc.lock);
  336. pchan = vchan->phy;
  337. if (pchan && pchan->done) {
  338. if (sun6i_dma_start_desc(vchan)) {
  339. /*
  340. * No current txd associated with this channel
  341. */
  342. dev_dbg(sdev->slave.dev, "pchan %u: free\n",
  343. pchan->idx);
  344. /* Mark this channel free */
  345. vchan->phy = NULL;
  346. pchan->vchan = NULL;
  347. }
  348. }
  349. spin_unlock_irq(&vchan->vc.lock);
  350. }
  351. spin_lock_irq(&sdev->lock);
  352. for (pchan_idx = 0; pchan_idx < cfg->nr_max_channels; pchan_idx++) {
  353. pchan = &sdev->pchans[pchan_idx];
  354. if (pchan->vchan || list_empty(&sdev->pending))
  355. continue;
  356. vchan = list_first_entry(&sdev->pending,
  357. struct sun6i_vchan, node);
  358. /* Remove from pending channels */
  359. list_del_init(&vchan->node);
  360. pchan_alloc |= BIT(pchan_idx);
  361. /* Mark this channel allocated */
  362. pchan->vchan = vchan;
  363. vchan->phy = pchan;
  364. dev_dbg(sdev->slave.dev, "pchan %u: alloc vchan %p\n",
  365. pchan->idx, &vchan->vc);
  366. }
  367. spin_unlock_irq(&sdev->lock);
  368. for (pchan_idx = 0; pchan_idx < cfg->nr_max_channels; pchan_idx++) {
  369. if (!(pchan_alloc & BIT(pchan_idx)))
  370. continue;
  371. pchan = sdev->pchans + pchan_idx;
  372. vchan = pchan->vchan;
  373. if (vchan) {
  374. spin_lock_irq(&vchan->vc.lock);
  375. sun6i_dma_start_desc(vchan);
  376. spin_unlock_irq(&vchan->vc.lock);
  377. }
  378. }
  379. }
  380. static irqreturn_t sun6i_dma_interrupt(int irq, void *dev_id)
  381. {
  382. struct sun6i_dma_dev *sdev = dev_id;
  383. struct sun6i_vchan *vchan;
  384. struct sun6i_pchan *pchan;
  385. int i, j, ret = IRQ_NONE;
  386. u32 status;
  387. for (i = 0; i < sdev->cfg->nr_max_channels / DMA_IRQ_CHAN_NR; i++) {
  388. status = readl(sdev->base + DMA_IRQ_STAT(i));
  389. if (!status)
  390. continue;
  391. dev_dbg(sdev->slave.dev, "DMA irq status %s: 0x%x\n",
  392. i ? "high" : "low", status);
  393. writel(status, sdev->base + DMA_IRQ_STAT(i));
  394. for (j = 0; (j < DMA_IRQ_CHAN_NR) && status; j++) {
  395. if (status & DMA_IRQ_QUEUE) {
  396. pchan = sdev->pchans + j;
  397. vchan = pchan->vchan;
  398. if (vchan) {
  399. spin_lock(&vchan->vc.lock);
  400. vchan_cookie_complete(&pchan->desc->vd);
  401. pchan->done = pchan->desc;
  402. spin_unlock(&vchan->vc.lock);
  403. }
  404. }
  405. status = status >> DMA_IRQ_CHAN_WIDTH;
  406. }
  407. if (!atomic_read(&sdev->tasklet_shutdown))
  408. tasklet_schedule(&sdev->task);
  409. ret = IRQ_HANDLED;
  410. }
  411. return ret;
  412. }
  413. static struct dma_async_tx_descriptor *sun6i_dma_prep_dma_memcpy(
  414. struct dma_chan *chan, dma_addr_t dest, dma_addr_t src,
  415. size_t len, unsigned long flags)
  416. {
  417. struct sun6i_dma_dev *sdev = to_sun6i_dma_dev(chan->device);
  418. struct sun6i_vchan *vchan = to_sun6i_vchan(chan);
  419. struct sun6i_dma_lli *v_lli;
  420. struct sun6i_desc *txd;
  421. dma_addr_t p_lli;
  422. s8 burst, width;
  423. dev_dbg(chan2dev(chan),
  424. "%s; chan: %d, dest: %pad, src: %pad, len: %zu. flags: 0x%08lx\n",
  425. __func__, vchan->vc.chan.chan_id, &dest, &src, len, flags);
  426. if (!len)
  427. return NULL;
  428. txd = kzalloc(sizeof(*txd), GFP_NOWAIT);
  429. if (!txd)
  430. return NULL;
  431. v_lli = dma_pool_alloc(sdev->pool, GFP_NOWAIT, &p_lli);
  432. if (!v_lli) {
  433. dev_err(sdev->slave.dev, "Failed to alloc lli memory\n");
  434. goto err_txd_free;
  435. }
  436. v_lli->src = src;
  437. v_lli->dst = dest;
  438. v_lli->len = len;
  439. v_lli->para = NORMAL_WAIT;
  440. burst = convert_burst(8);
  441. width = convert_buswidth(DMA_SLAVE_BUSWIDTH_4_BYTES);
  442. v_lli->cfg |= DMA_CHAN_CFG_SRC_DRQ(DRQ_SDRAM) |
  443. DMA_CHAN_CFG_DST_DRQ(DRQ_SDRAM) |
  444. DMA_CHAN_CFG_DST_LINEAR_MODE |
  445. DMA_CHAN_CFG_SRC_LINEAR_MODE |
  446. DMA_CHAN_CFG_SRC_BURST(burst) |
  447. DMA_CHAN_CFG_SRC_WIDTH(width) |
  448. DMA_CHAN_CFG_DST_BURST(burst) |
  449. DMA_CHAN_CFG_DST_WIDTH(width);
  450. sun6i_dma_lli_add(NULL, v_lli, p_lli, txd);
  451. sun6i_dma_dump_lli(vchan, v_lli);
  452. return vchan_tx_prep(&vchan->vc, &txd->vd, flags);
  453. err_txd_free:
  454. kfree(txd);
  455. return NULL;
  456. }
  457. static struct dma_async_tx_descriptor *sun6i_dma_prep_slave_sg(
  458. struct dma_chan *chan, struct scatterlist *sgl,
  459. unsigned int sg_len, enum dma_transfer_direction dir,
  460. unsigned long flags, void *context)
  461. {
  462. struct sun6i_dma_dev *sdev = to_sun6i_dma_dev(chan->device);
  463. struct sun6i_vchan *vchan = to_sun6i_vchan(chan);
  464. struct dma_slave_config *sconfig = &vchan->cfg;
  465. struct sun6i_dma_lli *v_lli, *prev = NULL;
  466. struct sun6i_desc *txd;
  467. struct scatterlist *sg;
  468. dma_addr_t p_lli;
  469. int i, ret;
  470. if (!sgl)
  471. return NULL;
  472. if (!is_slave_direction(dir)) {
  473. dev_err(chan2dev(chan), "Invalid DMA direction\n");
  474. return NULL;
  475. }
  476. txd = kzalloc(sizeof(*txd), GFP_NOWAIT);
  477. if (!txd)
  478. return NULL;
  479. for_each_sg(sgl, sg, sg_len, i) {
  480. v_lli = dma_pool_alloc(sdev->pool, GFP_NOWAIT, &p_lli);
  481. if (!v_lli)
  482. goto err_lli_free;
  483. if (dir == DMA_MEM_TO_DEV) {
  484. ret = sun6i_dma_cfg_lli(v_lli, sg_dma_address(sg),
  485. sconfig->dst_addr, sg_dma_len(sg),
  486. sconfig);
  487. if (ret)
  488. goto err_cur_lli_free;
  489. v_lli->cfg |= DMA_CHAN_CFG_DST_IO_MODE |
  490. DMA_CHAN_CFG_SRC_LINEAR_MODE |
  491. DMA_CHAN_CFG_SRC_DRQ(DRQ_SDRAM) |
  492. DMA_CHAN_CFG_DST_DRQ(vchan->port);
  493. dev_dbg(chan2dev(chan),
  494. "%s; chan: %d, dest: %pad, src: %pad, len: %u. flags: 0x%08lx\n",
  495. __func__, vchan->vc.chan.chan_id,
  496. &sconfig->dst_addr, &sg_dma_address(sg),
  497. sg_dma_len(sg), flags);
  498. } else {
  499. ret = sun6i_dma_cfg_lli(v_lli, sconfig->src_addr,
  500. sg_dma_address(sg), sg_dma_len(sg),
  501. sconfig);
  502. if (ret)
  503. goto err_cur_lli_free;
  504. v_lli->cfg |= DMA_CHAN_CFG_DST_LINEAR_MODE |
  505. DMA_CHAN_CFG_SRC_IO_MODE |
  506. DMA_CHAN_CFG_DST_DRQ(DRQ_SDRAM) |
  507. DMA_CHAN_CFG_SRC_DRQ(vchan->port);
  508. dev_dbg(chan2dev(chan),
  509. "%s; chan: %d, dest: %pad, src: %pad, len: %u. flags: 0x%08lx\n",
  510. __func__, vchan->vc.chan.chan_id,
  511. &sg_dma_address(sg), &sconfig->src_addr,
  512. sg_dma_len(sg), flags);
  513. }
  514. prev = sun6i_dma_lli_add(prev, v_lli, p_lli, txd);
  515. }
  516. dev_dbg(chan2dev(chan), "First: %pad\n", &txd->p_lli);
  517. for (prev = txd->v_lli; prev; prev = prev->v_lli_next)
  518. sun6i_dma_dump_lli(vchan, prev);
  519. return vchan_tx_prep(&vchan->vc, &txd->vd, flags);
  520. err_cur_lli_free:
  521. dma_pool_free(sdev->pool, v_lli, p_lli);
  522. err_lli_free:
  523. for (prev = txd->v_lli; prev; prev = prev->v_lli_next)
  524. dma_pool_free(sdev->pool, prev, virt_to_phys(prev));
  525. kfree(txd);
  526. return NULL;
  527. }
  528. static int sun6i_dma_config(struct dma_chan *chan,
  529. struct dma_slave_config *config)
  530. {
  531. struct sun6i_vchan *vchan = to_sun6i_vchan(chan);
  532. memcpy(&vchan->cfg, config, sizeof(*config));
  533. return 0;
  534. }
  535. static int sun6i_dma_pause(struct dma_chan *chan)
  536. {
  537. struct sun6i_dma_dev *sdev = to_sun6i_dma_dev(chan->device);
  538. struct sun6i_vchan *vchan = to_sun6i_vchan(chan);
  539. struct sun6i_pchan *pchan = vchan->phy;
  540. dev_dbg(chan2dev(chan), "vchan %p: pause\n", &vchan->vc);
  541. if (pchan) {
  542. writel(DMA_CHAN_PAUSE_PAUSE,
  543. pchan->base + DMA_CHAN_PAUSE);
  544. } else {
  545. spin_lock(&sdev->lock);
  546. list_del_init(&vchan->node);
  547. spin_unlock(&sdev->lock);
  548. }
  549. return 0;
  550. }
  551. static int sun6i_dma_resume(struct dma_chan *chan)
  552. {
  553. struct sun6i_dma_dev *sdev = to_sun6i_dma_dev(chan->device);
  554. struct sun6i_vchan *vchan = to_sun6i_vchan(chan);
  555. struct sun6i_pchan *pchan = vchan->phy;
  556. unsigned long flags;
  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. return 0;
  569. }
  570. static int sun6i_dma_terminate_all(struct dma_chan *chan)
  571. {
  572. struct sun6i_dma_dev *sdev = to_sun6i_dma_dev(chan->device);
  573. struct sun6i_vchan *vchan = to_sun6i_vchan(chan);
  574. struct sun6i_pchan *pchan = vchan->phy;
  575. unsigned long flags;
  576. LIST_HEAD(head);
  577. spin_lock(&sdev->lock);
  578. list_del_init(&vchan->node);
  579. spin_unlock(&sdev->lock);
  580. spin_lock_irqsave(&vchan->vc.lock, flags);
  581. vchan_get_all_descriptors(&vchan->vc, &head);
  582. if (pchan) {
  583. writel(DMA_CHAN_ENABLE_STOP, pchan->base + DMA_CHAN_ENABLE);
  584. writel(DMA_CHAN_PAUSE_RESUME, pchan->base + DMA_CHAN_PAUSE);
  585. vchan->phy = NULL;
  586. pchan->vchan = NULL;
  587. pchan->desc = NULL;
  588. pchan->done = NULL;
  589. }
  590. spin_unlock_irqrestore(&vchan->vc.lock, flags);
  591. vchan_dma_desc_free_list(&vchan->vc, &head);
  592. return 0;
  593. }
  594. static enum dma_status sun6i_dma_tx_status(struct dma_chan *chan,
  595. dma_cookie_t cookie,
  596. struct dma_tx_state *state)
  597. {
  598. struct sun6i_vchan *vchan = to_sun6i_vchan(chan);
  599. struct sun6i_pchan *pchan = vchan->phy;
  600. struct sun6i_dma_lli *lli;
  601. struct virt_dma_desc *vd;
  602. struct sun6i_desc *txd;
  603. enum dma_status ret;
  604. unsigned long flags;
  605. size_t bytes = 0;
  606. ret = dma_cookie_status(chan, cookie, state);
  607. if (ret == DMA_COMPLETE)
  608. return ret;
  609. spin_lock_irqsave(&vchan->vc.lock, flags);
  610. vd = vchan_find_desc(&vchan->vc, cookie);
  611. txd = to_sun6i_desc(&vd->tx);
  612. if (vd) {
  613. for (lli = txd->v_lli; lli != NULL; lli = lli->v_lli_next)
  614. bytes += lli->len;
  615. } else if (!pchan || !pchan->desc) {
  616. bytes = 0;
  617. } else {
  618. bytes = readl(pchan->base + DMA_CHAN_CUR_CNT);
  619. }
  620. spin_unlock_irqrestore(&vchan->vc.lock, flags);
  621. dma_set_residue(state, bytes);
  622. return ret;
  623. }
  624. static void sun6i_dma_issue_pending(struct dma_chan *chan)
  625. {
  626. struct sun6i_dma_dev *sdev = to_sun6i_dma_dev(chan->device);
  627. struct sun6i_vchan *vchan = to_sun6i_vchan(chan);
  628. unsigned long flags;
  629. spin_lock_irqsave(&vchan->vc.lock, flags);
  630. if (vchan_issue_pending(&vchan->vc)) {
  631. spin_lock(&sdev->lock);
  632. if (!vchan->phy && list_empty(&vchan->node)) {
  633. list_add_tail(&vchan->node, &sdev->pending);
  634. tasklet_schedule(&sdev->task);
  635. dev_dbg(chan2dev(chan), "vchan %p: issued\n",
  636. &vchan->vc);
  637. }
  638. spin_unlock(&sdev->lock);
  639. } else {
  640. dev_dbg(chan2dev(chan), "vchan %p: nothing to issue\n",
  641. &vchan->vc);
  642. }
  643. spin_unlock_irqrestore(&vchan->vc.lock, flags);
  644. }
  645. static void sun6i_dma_free_chan_resources(struct dma_chan *chan)
  646. {
  647. struct sun6i_dma_dev *sdev = to_sun6i_dma_dev(chan->device);
  648. struct sun6i_vchan *vchan = to_sun6i_vchan(chan);
  649. unsigned long flags;
  650. spin_lock_irqsave(&sdev->lock, flags);
  651. list_del_init(&vchan->node);
  652. spin_unlock_irqrestore(&sdev->lock, flags);
  653. vchan_free_chan_resources(&vchan->vc);
  654. }
  655. static struct dma_chan *sun6i_dma_of_xlate(struct of_phandle_args *dma_spec,
  656. struct of_dma *ofdma)
  657. {
  658. struct sun6i_dma_dev *sdev = ofdma->of_dma_data;
  659. struct sun6i_vchan *vchan;
  660. struct dma_chan *chan;
  661. u8 port = dma_spec->args[0];
  662. if (port > sdev->cfg->nr_max_requests)
  663. return NULL;
  664. chan = dma_get_any_slave_channel(&sdev->slave);
  665. if (!chan)
  666. return NULL;
  667. vchan = to_sun6i_vchan(chan);
  668. vchan->port = port;
  669. return chan;
  670. }
  671. static inline void sun6i_kill_tasklet(struct sun6i_dma_dev *sdev)
  672. {
  673. /* Disable all interrupts from DMA */
  674. writel(0, sdev->base + DMA_IRQ_EN(0));
  675. writel(0, sdev->base + DMA_IRQ_EN(1));
  676. /* Prevent spurious interrupts from scheduling the tasklet */
  677. atomic_inc(&sdev->tasklet_shutdown);
  678. /* Make sure we won't have any further interrupts */
  679. devm_free_irq(sdev->slave.dev, sdev->irq, sdev);
  680. /* Actually prevent the tasklet from being scheduled */
  681. tasklet_kill(&sdev->task);
  682. }
  683. static inline void sun6i_dma_free(struct sun6i_dma_dev *sdev)
  684. {
  685. int i;
  686. for (i = 0; i < sdev->cfg->nr_max_vchans; i++) {
  687. struct sun6i_vchan *vchan = &sdev->vchans[i];
  688. list_del(&vchan->vc.chan.device_node);
  689. tasklet_kill(&vchan->vc.task);
  690. }
  691. }
  692. /*
  693. * For A31:
  694. *
  695. * There's 16 physical channels that can work in parallel.
  696. *
  697. * However we have 30 different endpoints for our requests.
  698. *
  699. * Since the channels are able to handle only an unidirectional
  700. * transfer, we need to allocate more virtual channels so that
  701. * everyone can grab one channel.
  702. *
  703. * Some devices can't work in both direction (mostly because it
  704. * wouldn't make sense), so we have a bit fewer virtual channels than
  705. * 2 channels per endpoints.
  706. */
  707. static struct sun6i_dma_config sun6i_a31_dma_cfg = {
  708. .nr_max_channels = 16,
  709. .nr_max_requests = 30,
  710. .nr_max_vchans = 53,
  711. };
  712. /*
  713. * The A23 only has 8 physical channels, a maximum DRQ port id of 24,
  714. * and a total of 37 usable source and destination endpoints.
  715. */
  716. static struct sun6i_dma_config sun8i_a23_dma_cfg = {
  717. .nr_max_channels = 8,
  718. .nr_max_requests = 24,
  719. .nr_max_vchans = 37,
  720. };
  721. /*
  722. * The H3 has 12 physical channels, a maximum DRQ port id of 27,
  723. * and a total of 34 usable source and destination endpoints.
  724. */
  725. static struct sun6i_dma_config sun8i_h3_dma_cfg = {
  726. .nr_max_channels = 12,
  727. .nr_max_requests = 27,
  728. .nr_max_vchans = 34,
  729. };
  730. static const struct of_device_id sun6i_dma_match[] = {
  731. { .compatible = "allwinner,sun6i-a31-dma", .data = &sun6i_a31_dma_cfg },
  732. { .compatible = "allwinner,sun8i-a23-dma", .data = &sun8i_a23_dma_cfg },
  733. { .compatible = "allwinner,sun8i-h3-dma", .data = &sun8i_h3_dma_cfg },
  734. { /* sentinel */ }
  735. };
  736. MODULE_DEVICE_TABLE(of, sun6i_dma_match);
  737. static int sun6i_dma_probe(struct platform_device *pdev)
  738. {
  739. const struct of_device_id *device;
  740. struct sun6i_dma_dev *sdc;
  741. struct resource *res;
  742. int ret, i;
  743. sdc = devm_kzalloc(&pdev->dev, sizeof(*sdc), GFP_KERNEL);
  744. if (!sdc)
  745. return -ENOMEM;
  746. device = of_match_device(sun6i_dma_match, &pdev->dev);
  747. if (!device)
  748. return -ENODEV;
  749. sdc->cfg = device->data;
  750. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  751. sdc->base = devm_ioremap_resource(&pdev->dev, res);
  752. if (IS_ERR(sdc->base))
  753. return PTR_ERR(sdc->base);
  754. sdc->irq = platform_get_irq(pdev, 0);
  755. if (sdc->irq < 0) {
  756. dev_err(&pdev->dev, "Cannot claim IRQ\n");
  757. return sdc->irq;
  758. }
  759. sdc->clk = devm_clk_get(&pdev->dev, NULL);
  760. if (IS_ERR(sdc->clk)) {
  761. dev_err(&pdev->dev, "No clock specified\n");
  762. return PTR_ERR(sdc->clk);
  763. }
  764. sdc->rstc = devm_reset_control_get(&pdev->dev, NULL);
  765. if (IS_ERR(sdc->rstc)) {
  766. dev_err(&pdev->dev, "No reset controller specified\n");
  767. return PTR_ERR(sdc->rstc);
  768. }
  769. sdc->pool = dmam_pool_create(dev_name(&pdev->dev), &pdev->dev,
  770. sizeof(struct sun6i_dma_lli), 4, 0);
  771. if (!sdc->pool) {
  772. dev_err(&pdev->dev, "No memory for descriptors dma pool\n");
  773. return -ENOMEM;
  774. }
  775. platform_set_drvdata(pdev, sdc);
  776. INIT_LIST_HEAD(&sdc->pending);
  777. spin_lock_init(&sdc->lock);
  778. dma_cap_set(DMA_PRIVATE, sdc->slave.cap_mask);
  779. dma_cap_set(DMA_MEMCPY, sdc->slave.cap_mask);
  780. dma_cap_set(DMA_SLAVE, sdc->slave.cap_mask);
  781. INIT_LIST_HEAD(&sdc->slave.channels);
  782. sdc->slave.device_free_chan_resources = sun6i_dma_free_chan_resources;
  783. sdc->slave.device_tx_status = sun6i_dma_tx_status;
  784. sdc->slave.device_issue_pending = sun6i_dma_issue_pending;
  785. sdc->slave.device_prep_slave_sg = sun6i_dma_prep_slave_sg;
  786. sdc->slave.device_prep_dma_memcpy = sun6i_dma_prep_dma_memcpy;
  787. sdc->slave.copy_align = DMAENGINE_ALIGN_4_BYTES;
  788. sdc->slave.device_config = sun6i_dma_config;
  789. sdc->slave.device_pause = sun6i_dma_pause;
  790. sdc->slave.device_resume = sun6i_dma_resume;
  791. sdc->slave.device_terminate_all = sun6i_dma_terminate_all;
  792. sdc->slave.src_addr_widths = BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) |
  793. BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) |
  794. BIT(DMA_SLAVE_BUSWIDTH_4_BYTES);
  795. sdc->slave.dst_addr_widths = BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) |
  796. BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) |
  797. BIT(DMA_SLAVE_BUSWIDTH_4_BYTES);
  798. sdc->slave.directions = BIT(DMA_DEV_TO_MEM) |
  799. BIT(DMA_MEM_TO_DEV);
  800. sdc->slave.residue_granularity = DMA_RESIDUE_GRANULARITY_BURST;
  801. sdc->slave.dev = &pdev->dev;
  802. sdc->pchans = devm_kcalloc(&pdev->dev, sdc->cfg->nr_max_channels,
  803. sizeof(struct sun6i_pchan), GFP_KERNEL);
  804. if (!sdc->pchans)
  805. return -ENOMEM;
  806. sdc->vchans = devm_kcalloc(&pdev->dev, sdc->cfg->nr_max_vchans,
  807. sizeof(struct sun6i_vchan), GFP_KERNEL);
  808. if (!sdc->vchans)
  809. return -ENOMEM;
  810. tasklet_init(&sdc->task, sun6i_dma_tasklet, (unsigned long)sdc);
  811. for (i = 0; i < sdc->cfg->nr_max_channels; i++) {
  812. struct sun6i_pchan *pchan = &sdc->pchans[i];
  813. pchan->idx = i;
  814. pchan->base = sdc->base + 0x100 + i * 0x40;
  815. }
  816. for (i = 0; i < sdc->cfg->nr_max_vchans; i++) {
  817. struct sun6i_vchan *vchan = &sdc->vchans[i];
  818. INIT_LIST_HEAD(&vchan->node);
  819. vchan->vc.desc_free = sun6i_dma_free_desc;
  820. vchan_init(&vchan->vc, &sdc->slave);
  821. }
  822. ret = reset_control_deassert(sdc->rstc);
  823. if (ret) {
  824. dev_err(&pdev->dev, "Couldn't deassert the device from reset\n");
  825. goto err_chan_free;
  826. }
  827. ret = clk_prepare_enable(sdc->clk);
  828. if (ret) {
  829. dev_err(&pdev->dev, "Couldn't enable the clock\n");
  830. goto err_reset_assert;
  831. }
  832. ret = devm_request_irq(&pdev->dev, sdc->irq, sun6i_dma_interrupt, 0,
  833. dev_name(&pdev->dev), sdc);
  834. if (ret) {
  835. dev_err(&pdev->dev, "Cannot request IRQ\n");
  836. goto err_clk_disable;
  837. }
  838. ret = dma_async_device_register(&sdc->slave);
  839. if (ret) {
  840. dev_warn(&pdev->dev, "Failed to register DMA engine device\n");
  841. goto err_irq_disable;
  842. }
  843. ret = of_dma_controller_register(pdev->dev.of_node, sun6i_dma_of_xlate,
  844. sdc);
  845. if (ret) {
  846. dev_err(&pdev->dev, "of_dma_controller_register failed\n");
  847. goto err_dma_unregister;
  848. }
  849. /*
  850. * sun8i variant requires us to toggle a dma gating register,
  851. * as seen in Allwinner's SDK. This register is not documented
  852. * in the A23 user manual.
  853. */
  854. if (of_device_is_compatible(pdev->dev.of_node,
  855. "allwinner,sun8i-a23-dma"))
  856. writel(SUN8I_DMA_GATE_ENABLE, sdc->base + SUN8I_DMA_GATE);
  857. return 0;
  858. err_dma_unregister:
  859. dma_async_device_unregister(&sdc->slave);
  860. err_irq_disable:
  861. sun6i_kill_tasklet(sdc);
  862. err_clk_disable:
  863. clk_disable_unprepare(sdc->clk);
  864. err_reset_assert:
  865. reset_control_assert(sdc->rstc);
  866. err_chan_free:
  867. sun6i_dma_free(sdc);
  868. return ret;
  869. }
  870. static int sun6i_dma_remove(struct platform_device *pdev)
  871. {
  872. struct sun6i_dma_dev *sdc = platform_get_drvdata(pdev);
  873. of_dma_controller_free(pdev->dev.of_node);
  874. dma_async_device_unregister(&sdc->slave);
  875. sun6i_kill_tasklet(sdc);
  876. clk_disable_unprepare(sdc->clk);
  877. reset_control_assert(sdc->rstc);
  878. sun6i_dma_free(sdc);
  879. return 0;
  880. }
  881. static struct platform_driver sun6i_dma_driver = {
  882. .probe = sun6i_dma_probe,
  883. .remove = sun6i_dma_remove,
  884. .driver = {
  885. .name = "sun6i-dma",
  886. .of_match_table = sun6i_dma_match,
  887. },
  888. };
  889. module_platform_driver(sun6i_dma_driver);
  890. MODULE_DESCRIPTION("Allwinner A31 DMA Controller Driver");
  891. MODULE_AUTHOR("Sugar <shuge@allwinnertech.com>");
  892. MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>");
  893. MODULE_LICENSE("GPL");