sun6i-dma.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089
  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. static const struct of_device_id sun6i_dma_match[] = {
  722. { .compatible = "allwinner,sun6i-a31-dma", .data = &sun6i_a31_dma_cfg },
  723. { .compatible = "allwinner,sun8i-a23-dma", .data = &sun8i_a23_dma_cfg },
  724. { /* sentinel */ }
  725. };
  726. static int sun6i_dma_probe(struct platform_device *pdev)
  727. {
  728. const struct of_device_id *device;
  729. struct sun6i_dma_dev *sdc;
  730. struct resource *res;
  731. int ret, i;
  732. sdc = devm_kzalloc(&pdev->dev, sizeof(*sdc), GFP_KERNEL);
  733. if (!sdc)
  734. return -ENOMEM;
  735. device = of_match_device(sun6i_dma_match, &pdev->dev);
  736. if (!device)
  737. return -ENODEV;
  738. sdc->cfg = device->data;
  739. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  740. sdc->base = devm_ioremap_resource(&pdev->dev, res);
  741. if (IS_ERR(sdc->base))
  742. return PTR_ERR(sdc->base);
  743. sdc->irq = platform_get_irq(pdev, 0);
  744. if (sdc->irq < 0) {
  745. dev_err(&pdev->dev, "Cannot claim IRQ\n");
  746. return sdc->irq;
  747. }
  748. sdc->clk = devm_clk_get(&pdev->dev, NULL);
  749. if (IS_ERR(sdc->clk)) {
  750. dev_err(&pdev->dev, "No clock specified\n");
  751. return PTR_ERR(sdc->clk);
  752. }
  753. sdc->rstc = devm_reset_control_get(&pdev->dev, NULL);
  754. if (IS_ERR(sdc->rstc)) {
  755. dev_err(&pdev->dev, "No reset controller specified\n");
  756. return PTR_ERR(sdc->rstc);
  757. }
  758. sdc->pool = dmam_pool_create(dev_name(&pdev->dev), &pdev->dev,
  759. sizeof(struct sun6i_dma_lli), 4, 0);
  760. if (!sdc->pool) {
  761. dev_err(&pdev->dev, "No memory for descriptors dma pool\n");
  762. return -ENOMEM;
  763. }
  764. platform_set_drvdata(pdev, sdc);
  765. INIT_LIST_HEAD(&sdc->pending);
  766. spin_lock_init(&sdc->lock);
  767. dma_cap_set(DMA_PRIVATE, sdc->slave.cap_mask);
  768. dma_cap_set(DMA_MEMCPY, sdc->slave.cap_mask);
  769. dma_cap_set(DMA_SLAVE, sdc->slave.cap_mask);
  770. INIT_LIST_HEAD(&sdc->slave.channels);
  771. sdc->slave.device_free_chan_resources = sun6i_dma_free_chan_resources;
  772. sdc->slave.device_tx_status = sun6i_dma_tx_status;
  773. sdc->slave.device_issue_pending = sun6i_dma_issue_pending;
  774. sdc->slave.device_prep_slave_sg = sun6i_dma_prep_slave_sg;
  775. sdc->slave.device_prep_dma_memcpy = sun6i_dma_prep_dma_memcpy;
  776. sdc->slave.copy_align = 4;
  777. sdc->slave.device_config = sun6i_dma_config;
  778. sdc->slave.device_pause = sun6i_dma_pause;
  779. sdc->slave.device_resume = sun6i_dma_resume;
  780. sdc->slave.device_terminate_all = sun6i_dma_terminate_all;
  781. sdc->slave.src_addr_widths = BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) |
  782. BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) |
  783. BIT(DMA_SLAVE_BUSWIDTH_4_BYTES);
  784. sdc->slave.dst_addr_widths = BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) |
  785. BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) |
  786. BIT(DMA_SLAVE_BUSWIDTH_4_BYTES);
  787. sdc->slave.directions = BIT(DMA_DEV_TO_MEM) |
  788. BIT(DMA_MEM_TO_DEV);
  789. sdc->slave.residue_granularity = DMA_RESIDUE_GRANULARITY_BURST;
  790. sdc->slave.dev = &pdev->dev;
  791. sdc->pchans = devm_kcalloc(&pdev->dev, sdc->cfg->nr_max_channels,
  792. sizeof(struct sun6i_pchan), GFP_KERNEL);
  793. if (!sdc->pchans)
  794. return -ENOMEM;
  795. sdc->vchans = devm_kcalloc(&pdev->dev, sdc->cfg->nr_max_vchans,
  796. sizeof(struct sun6i_vchan), GFP_KERNEL);
  797. if (!sdc->vchans)
  798. return -ENOMEM;
  799. tasklet_init(&sdc->task, sun6i_dma_tasklet, (unsigned long)sdc);
  800. for (i = 0; i < sdc->cfg->nr_max_channels; i++) {
  801. struct sun6i_pchan *pchan = &sdc->pchans[i];
  802. pchan->idx = i;
  803. pchan->base = sdc->base + 0x100 + i * 0x40;
  804. }
  805. for (i = 0; i < sdc->cfg->nr_max_vchans; i++) {
  806. struct sun6i_vchan *vchan = &sdc->vchans[i];
  807. INIT_LIST_HEAD(&vchan->node);
  808. vchan->vc.desc_free = sun6i_dma_free_desc;
  809. vchan_init(&vchan->vc, &sdc->slave);
  810. }
  811. ret = reset_control_deassert(sdc->rstc);
  812. if (ret) {
  813. dev_err(&pdev->dev, "Couldn't deassert the device from reset\n");
  814. goto err_chan_free;
  815. }
  816. ret = clk_prepare_enable(sdc->clk);
  817. if (ret) {
  818. dev_err(&pdev->dev, "Couldn't enable the clock\n");
  819. goto err_reset_assert;
  820. }
  821. ret = devm_request_irq(&pdev->dev, sdc->irq, sun6i_dma_interrupt, 0,
  822. dev_name(&pdev->dev), sdc);
  823. if (ret) {
  824. dev_err(&pdev->dev, "Cannot request IRQ\n");
  825. goto err_clk_disable;
  826. }
  827. ret = dma_async_device_register(&sdc->slave);
  828. if (ret) {
  829. dev_warn(&pdev->dev, "Failed to register DMA engine device\n");
  830. goto err_irq_disable;
  831. }
  832. ret = of_dma_controller_register(pdev->dev.of_node, sun6i_dma_of_xlate,
  833. sdc);
  834. if (ret) {
  835. dev_err(&pdev->dev, "of_dma_controller_register failed\n");
  836. goto err_dma_unregister;
  837. }
  838. /*
  839. * sun8i variant requires us to toggle a dma gating register,
  840. * as seen in Allwinner's SDK. This register is not documented
  841. * in the A23 user manual.
  842. */
  843. if (of_device_is_compatible(pdev->dev.of_node,
  844. "allwinner,sun8i-a23-dma"))
  845. writel(SUN8I_DMA_GATE_ENABLE, sdc->base + SUN8I_DMA_GATE);
  846. return 0;
  847. err_dma_unregister:
  848. dma_async_device_unregister(&sdc->slave);
  849. err_irq_disable:
  850. sun6i_kill_tasklet(sdc);
  851. err_clk_disable:
  852. clk_disable_unprepare(sdc->clk);
  853. err_reset_assert:
  854. reset_control_assert(sdc->rstc);
  855. err_chan_free:
  856. sun6i_dma_free(sdc);
  857. return ret;
  858. }
  859. static int sun6i_dma_remove(struct platform_device *pdev)
  860. {
  861. struct sun6i_dma_dev *sdc = platform_get_drvdata(pdev);
  862. of_dma_controller_free(pdev->dev.of_node);
  863. dma_async_device_unregister(&sdc->slave);
  864. sun6i_kill_tasklet(sdc);
  865. clk_disable_unprepare(sdc->clk);
  866. reset_control_assert(sdc->rstc);
  867. sun6i_dma_free(sdc);
  868. return 0;
  869. }
  870. static struct platform_driver sun6i_dma_driver = {
  871. .probe = sun6i_dma_probe,
  872. .remove = sun6i_dma_remove,
  873. .driver = {
  874. .name = "sun6i-dma",
  875. .of_match_table = sun6i_dma_match,
  876. },
  877. };
  878. module_platform_driver(sun6i_dma_driver);
  879. MODULE_DESCRIPTION("Allwinner A31 DMA Controller Driver");
  880. MODULE_AUTHOR("Sugar <shuge@allwinnertech.com>");
  881. MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>");
  882. MODULE_LICENSE("GPL");