sun6i-dma.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251
  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. * In the datasheets/user manuals of newer Allwinner SoCs, a special
  86. * bit (bit 2 at register 0x20) is present.
  87. * It's named "DMA MCLK interface circuit auto gating bit" in the
  88. * documents, and the footnote of this register says that this bit
  89. * should be set up when initializing the DMA controller.
  90. * Allwinner A23/A33 user manuals do not have this bit documented,
  91. * however these SoCs really have and need this bit, as seen in the
  92. * BSP kernel source code.
  93. */
  94. bool gate_needed;
  95. };
  96. /*
  97. * Hardware representation of the LLI
  98. *
  99. * The hardware will be fed the physical address of this structure,
  100. * and read its content in order to start the transfer.
  101. */
  102. struct sun6i_dma_lli {
  103. u32 cfg;
  104. u32 src;
  105. u32 dst;
  106. u32 len;
  107. u32 para;
  108. u32 p_lli_next;
  109. /*
  110. * This field is not used by the DMA controller, but will be
  111. * used by the CPU to go through the list (mostly for dumping
  112. * or freeing it).
  113. */
  114. struct sun6i_dma_lli *v_lli_next;
  115. };
  116. struct sun6i_desc {
  117. struct virt_dma_desc vd;
  118. dma_addr_t p_lli;
  119. struct sun6i_dma_lli *v_lli;
  120. };
  121. struct sun6i_pchan {
  122. u32 idx;
  123. void __iomem *base;
  124. struct sun6i_vchan *vchan;
  125. struct sun6i_desc *desc;
  126. struct sun6i_desc *done;
  127. };
  128. struct sun6i_vchan {
  129. struct virt_dma_chan vc;
  130. struct list_head node;
  131. struct dma_slave_config cfg;
  132. struct sun6i_pchan *phy;
  133. u8 port;
  134. u8 irq_type;
  135. bool cyclic;
  136. };
  137. struct sun6i_dma_dev {
  138. struct dma_device slave;
  139. void __iomem *base;
  140. struct clk *clk;
  141. int irq;
  142. spinlock_t lock;
  143. struct reset_control *rstc;
  144. struct tasklet_struct task;
  145. atomic_t tasklet_shutdown;
  146. struct list_head pending;
  147. struct dma_pool *pool;
  148. struct sun6i_pchan *pchans;
  149. struct sun6i_vchan *vchans;
  150. const struct sun6i_dma_config *cfg;
  151. };
  152. static struct device *chan2dev(struct dma_chan *chan)
  153. {
  154. return &chan->dev->device;
  155. }
  156. static inline struct sun6i_dma_dev *to_sun6i_dma_dev(struct dma_device *d)
  157. {
  158. return container_of(d, struct sun6i_dma_dev, slave);
  159. }
  160. static inline struct sun6i_vchan *to_sun6i_vchan(struct dma_chan *chan)
  161. {
  162. return container_of(chan, struct sun6i_vchan, vc.chan);
  163. }
  164. static inline struct sun6i_desc *
  165. to_sun6i_desc(struct dma_async_tx_descriptor *tx)
  166. {
  167. return container_of(tx, struct sun6i_desc, vd.tx);
  168. }
  169. static inline void sun6i_dma_dump_com_regs(struct sun6i_dma_dev *sdev)
  170. {
  171. dev_dbg(sdev->slave.dev, "Common register:\n"
  172. "\tmask0(%04x): 0x%08x\n"
  173. "\tmask1(%04x): 0x%08x\n"
  174. "\tpend0(%04x): 0x%08x\n"
  175. "\tpend1(%04x): 0x%08x\n"
  176. "\tstats(%04x): 0x%08x\n",
  177. DMA_IRQ_EN(0), readl(sdev->base + DMA_IRQ_EN(0)),
  178. DMA_IRQ_EN(1), readl(sdev->base + DMA_IRQ_EN(1)),
  179. DMA_IRQ_STAT(0), readl(sdev->base + DMA_IRQ_STAT(0)),
  180. DMA_IRQ_STAT(1), readl(sdev->base + DMA_IRQ_STAT(1)),
  181. DMA_STAT, readl(sdev->base + DMA_STAT));
  182. }
  183. static inline void sun6i_dma_dump_chan_regs(struct sun6i_dma_dev *sdev,
  184. struct sun6i_pchan *pchan)
  185. {
  186. phys_addr_t reg = virt_to_phys(pchan->base);
  187. dev_dbg(sdev->slave.dev, "Chan %d reg: %pa\n"
  188. "\t___en(%04x): \t0x%08x\n"
  189. "\tpause(%04x): \t0x%08x\n"
  190. "\tstart(%04x): \t0x%08x\n"
  191. "\t__cfg(%04x): \t0x%08x\n"
  192. "\t__src(%04x): \t0x%08x\n"
  193. "\t__dst(%04x): \t0x%08x\n"
  194. "\tcount(%04x): \t0x%08x\n"
  195. "\t_para(%04x): \t0x%08x\n\n",
  196. pchan->idx, &reg,
  197. DMA_CHAN_ENABLE,
  198. readl(pchan->base + DMA_CHAN_ENABLE),
  199. DMA_CHAN_PAUSE,
  200. readl(pchan->base + DMA_CHAN_PAUSE),
  201. DMA_CHAN_LLI_ADDR,
  202. readl(pchan->base + DMA_CHAN_LLI_ADDR),
  203. DMA_CHAN_CUR_CFG,
  204. readl(pchan->base + DMA_CHAN_CUR_CFG),
  205. DMA_CHAN_CUR_SRC,
  206. readl(pchan->base + DMA_CHAN_CUR_SRC),
  207. DMA_CHAN_CUR_DST,
  208. readl(pchan->base + DMA_CHAN_CUR_DST),
  209. DMA_CHAN_CUR_CNT,
  210. readl(pchan->base + DMA_CHAN_CUR_CNT),
  211. DMA_CHAN_CUR_PARA,
  212. readl(pchan->base + DMA_CHAN_CUR_PARA));
  213. }
  214. static inline s8 convert_burst(u32 maxburst)
  215. {
  216. switch (maxburst) {
  217. case 1:
  218. return 0;
  219. case 8:
  220. return 2;
  221. default:
  222. return -EINVAL;
  223. }
  224. }
  225. static inline s8 convert_buswidth(enum dma_slave_buswidth addr_width)
  226. {
  227. if ((addr_width < DMA_SLAVE_BUSWIDTH_1_BYTE) ||
  228. (addr_width > DMA_SLAVE_BUSWIDTH_4_BYTES))
  229. return -EINVAL;
  230. return addr_width >> 1;
  231. }
  232. static size_t sun6i_get_chan_size(struct sun6i_pchan *pchan)
  233. {
  234. struct sun6i_desc *txd = pchan->desc;
  235. struct sun6i_dma_lli *lli;
  236. size_t bytes;
  237. dma_addr_t pos;
  238. pos = readl(pchan->base + DMA_CHAN_LLI_ADDR);
  239. bytes = readl(pchan->base + DMA_CHAN_CUR_CNT);
  240. if (pos == LLI_LAST_ITEM)
  241. return bytes;
  242. for (lli = txd->v_lli; lli; lli = lli->v_lli_next) {
  243. if (lli->p_lli_next == pos) {
  244. for (lli = lli->v_lli_next; lli; lli = lli->v_lli_next)
  245. bytes += lli->len;
  246. break;
  247. }
  248. }
  249. return bytes;
  250. }
  251. static void *sun6i_dma_lli_add(struct sun6i_dma_lli *prev,
  252. struct sun6i_dma_lli *next,
  253. dma_addr_t next_phy,
  254. struct sun6i_desc *txd)
  255. {
  256. if ((!prev && !txd) || !next)
  257. return NULL;
  258. if (!prev) {
  259. txd->p_lli = next_phy;
  260. txd->v_lli = next;
  261. } else {
  262. prev->p_lli_next = next_phy;
  263. prev->v_lli_next = next;
  264. }
  265. next->p_lli_next = LLI_LAST_ITEM;
  266. next->v_lli_next = NULL;
  267. return next;
  268. }
  269. static inline void sun6i_dma_dump_lli(struct sun6i_vchan *vchan,
  270. struct sun6i_dma_lli *lli)
  271. {
  272. phys_addr_t p_lli = virt_to_phys(lli);
  273. dev_dbg(chan2dev(&vchan->vc.chan),
  274. "\n\tdesc: p - %pa v - 0x%p\n"
  275. "\t\tc - 0x%08x s - 0x%08x d - 0x%08x\n"
  276. "\t\tl - 0x%08x p - 0x%08x n - 0x%08x\n",
  277. &p_lli, lli,
  278. lli->cfg, lli->src, lli->dst,
  279. lli->len, lli->para, lli->p_lli_next);
  280. }
  281. static void sun6i_dma_free_desc(struct virt_dma_desc *vd)
  282. {
  283. struct sun6i_desc *txd = to_sun6i_desc(&vd->tx);
  284. struct sun6i_dma_dev *sdev = to_sun6i_dma_dev(vd->tx.chan->device);
  285. struct sun6i_dma_lli *v_lli, *v_next;
  286. dma_addr_t p_lli, p_next;
  287. if (unlikely(!txd))
  288. return;
  289. p_lli = txd->p_lli;
  290. v_lli = txd->v_lli;
  291. while (v_lli) {
  292. v_next = v_lli->v_lli_next;
  293. p_next = v_lli->p_lli_next;
  294. dma_pool_free(sdev->pool, v_lli, p_lli);
  295. v_lli = v_next;
  296. p_lli = p_next;
  297. }
  298. kfree(txd);
  299. }
  300. static int sun6i_dma_start_desc(struct sun6i_vchan *vchan)
  301. {
  302. struct sun6i_dma_dev *sdev = to_sun6i_dma_dev(vchan->vc.chan.device);
  303. struct virt_dma_desc *desc = vchan_next_desc(&vchan->vc);
  304. struct sun6i_pchan *pchan = vchan->phy;
  305. u32 irq_val, irq_reg, irq_offset;
  306. if (!pchan)
  307. return -EAGAIN;
  308. if (!desc) {
  309. pchan->desc = NULL;
  310. pchan->done = NULL;
  311. return -EAGAIN;
  312. }
  313. list_del(&desc->node);
  314. pchan->desc = to_sun6i_desc(&desc->tx);
  315. pchan->done = NULL;
  316. sun6i_dma_dump_lli(vchan, pchan->desc->v_lli);
  317. irq_reg = pchan->idx / DMA_IRQ_CHAN_NR;
  318. irq_offset = pchan->idx % DMA_IRQ_CHAN_NR;
  319. vchan->irq_type = vchan->cyclic ? DMA_IRQ_PKG : DMA_IRQ_QUEUE;
  320. irq_val = readl(sdev->base + DMA_IRQ_EN(irq_reg));
  321. irq_val &= ~((DMA_IRQ_HALF | DMA_IRQ_PKG | DMA_IRQ_QUEUE) <<
  322. (irq_offset * DMA_IRQ_CHAN_WIDTH));
  323. irq_val |= vchan->irq_type << (irq_offset * DMA_IRQ_CHAN_WIDTH);
  324. writel(irq_val, sdev->base + DMA_IRQ_EN(irq_reg));
  325. writel(pchan->desc->p_lli, pchan->base + DMA_CHAN_LLI_ADDR);
  326. writel(DMA_CHAN_ENABLE_START, pchan->base + DMA_CHAN_ENABLE);
  327. sun6i_dma_dump_com_regs(sdev);
  328. sun6i_dma_dump_chan_regs(sdev, pchan);
  329. return 0;
  330. }
  331. static void sun6i_dma_tasklet(unsigned long data)
  332. {
  333. struct sun6i_dma_dev *sdev = (struct sun6i_dma_dev *)data;
  334. const struct sun6i_dma_config *cfg = sdev->cfg;
  335. struct sun6i_vchan *vchan;
  336. struct sun6i_pchan *pchan;
  337. unsigned int pchan_alloc = 0;
  338. unsigned int pchan_idx;
  339. list_for_each_entry(vchan, &sdev->slave.channels, vc.chan.device_node) {
  340. spin_lock_irq(&vchan->vc.lock);
  341. pchan = vchan->phy;
  342. if (pchan && pchan->done) {
  343. if (sun6i_dma_start_desc(vchan)) {
  344. /*
  345. * No current txd associated with this channel
  346. */
  347. dev_dbg(sdev->slave.dev, "pchan %u: free\n",
  348. pchan->idx);
  349. /* Mark this channel free */
  350. vchan->phy = NULL;
  351. pchan->vchan = NULL;
  352. }
  353. }
  354. spin_unlock_irq(&vchan->vc.lock);
  355. }
  356. spin_lock_irq(&sdev->lock);
  357. for (pchan_idx = 0; pchan_idx < cfg->nr_max_channels; pchan_idx++) {
  358. pchan = &sdev->pchans[pchan_idx];
  359. if (pchan->vchan || list_empty(&sdev->pending))
  360. continue;
  361. vchan = list_first_entry(&sdev->pending,
  362. struct sun6i_vchan, node);
  363. /* Remove from pending channels */
  364. list_del_init(&vchan->node);
  365. pchan_alloc |= BIT(pchan_idx);
  366. /* Mark this channel allocated */
  367. pchan->vchan = vchan;
  368. vchan->phy = pchan;
  369. dev_dbg(sdev->slave.dev, "pchan %u: alloc vchan %p\n",
  370. pchan->idx, &vchan->vc);
  371. }
  372. spin_unlock_irq(&sdev->lock);
  373. for (pchan_idx = 0; pchan_idx < cfg->nr_max_channels; pchan_idx++) {
  374. if (!(pchan_alloc & BIT(pchan_idx)))
  375. continue;
  376. pchan = sdev->pchans + pchan_idx;
  377. vchan = pchan->vchan;
  378. if (vchan) {
  379. spin_lock_irq(&vchan->vc.lock);
  380. sun6i_dma_start_desc(vchan);
  381. spin_unlock_irq(&vchan->vc.lock);
  382. }
  383. }
  384. }
  385. static irqreturn_t sun6i_dma_interrupt(int irq, void *dev_id)
  386. {
  387. struct sun6i_dma_dev *sdev = dev_id;
  388. struct sun6i_vchan *vchan;
  389. struct sun6i_pchan *pchan;
  390. int i, j, ret = IRQ_NONE;
  391. u32 status;
  392. for (i = 0; i < sdev->cfg->nr_max_channels / DMA_IRQ_CHAN_NR; i++) {
  393. status = readl(sdev->base + DMA_IRQ_STAT(i));
  394. if (!status)
  395. continue;
  396. dev_dbg(sdev->slave.dev, "DMA irq status %s: 0x%x\n",
  397. i ? "high" : "low", status);
  398. writel(status, sdev->base + DMA_IRQ_STAT(i));
  399. for (j = 0; (j < DMA_IRQ_CHAN_NR) && status; j++) {
  400. pchan = sdev->pchans + j;
  401. vchan = pchan->vchan;
  402. if (vchan && (status & vchan->irq_type)) {
  403. if (vchan->cyclic) {
  404. vchan_cyclic_callback(&pchan->desc->vd);
  405. } else {
  406. spin_lock(&vchan->vc.lock);
  407. vchan_cookie_complete(&pchan->desc->vd);
  408. pchan->done = pchan->desc;
  409. spin_unlock(&vchan->vc.lock);
  410. }
  411. }
  412. status = status >> DMA_IRQ_CHAN_WIDTH;
  413. }
  414. if (!atomic_read(&sdev->tasklet_shutdown))
  415. tasklet_schedule(&sdev->task);
  416. ret = IRQ_HANDLED;
  417. }
  418. return ret;
  419. }
  420. static int set_config(struct sun6i_dma_dev *sdev,
  421. struct dma_slave_config *sconfig,
  422. enum dma_transfer_direction direction,
  423. u32 *p_cfg)
  424. {
  425. s8 src_width, dst_width, src_burst, dst_burst;
  426. switch (direction) {
  427. case DMA_MEM_TO_DEV:
  428. src_burst = convert_burst(sconfig->src_maxburst ?
  429. sconfig->src_maxburst : 8);
  430. src_width = convert_buswidth(sconfig->src_addr_width !=
  431. DMA_SLAVE_BUSWIDTH_UNDEFINED ?
  432. sconfig->src_addr_width :
  433. DMA_SLAVE_BUSWIDTH_4_BYTES);
  434. dst_burst = convert_burst(sconfig->dst_maxburst);
  435. dst_width = convert_buswidth(sconfig->dst_addr_width);
  436. break;
  437. case DMA_DEV_TO_MEM:
  438. src_burst = convert_burst(sconfig->src_maxburst);
  439. src_width = convert_buswidth(sconfig->src_addr_width);
  440. dst_burst = convert_burst(sconfig->dst_maxburst ?
  441. sconfig->dst_maxburst : 8);
  442. dst_width = convert_buswidth(sconfig->dst_addr_width !=
  443. DMA_SLAVE_BUSWIDTH_UNDEFINED ?
  444. sconfig->dst_addr_width :
  445. DMA_SLAVE_BUSWIDTH_4_BYTES);
  446. break;
  447. default:
  448. return -EINVAL;
  449. }
  450. if (src_burst < 0)
  451. return src_burst;
  452. if (src_width < 0)
  453. return src_width;
  454. if (dst_burst < 0)
  455. return dst_burst;
  456. if (dst_width < 0)
  457. return dst_width;
  458. *p_cfg = DMA_CHAN_CFG_SRC_BURST(src_burst) |
  459. DMA_CHAN_CFG_SRC_WIDTH(src_width) |
  460. DMA_CHAN_CFG_DST_BURST(dst_burst) |
  461. DMA_CHAN_CFG_DST_WIDTH(dst_width);
  462. return 0;
  463. }
  464. static struct dma_async_tx_descriptor *sun6i_dma_prep_dma_memcpy(
  465. struct dma_chan *chan, dma_addr_t dest, dma_addr_t src,
  466. size_t len, unsigned long flags)
  467. {
  468. struct sun6i_dma_dev *sdev = to_sun6i_dma_dev(chan->device);
  469. struct sun6i_vchan *vchan = to_sun6i_vchan(chan);
  470. struct sun6i_dma_lli *v_lli;
  471. struct sun6i_desc *txd;
  472. dma_addr_t p_lli;
  473. s8 burst, width;
  474. dev_dbg(chan2dev(chan),
  475. "%s; chan: %d, dest: %pad, src: %pad, len: %zu. flags: 0x%08lx\n",
  476. __func__, vchan->vc.chan.chan_id, &dest, &src, len, flags);
  477. if (!len)
  478. return NULL;
  479. txd = kzalloc(sizeof(*txd), GFP_NOWAIT);
  480. if (!txd)
  481. return NULL;
  482. v_lli = dma_pool_alloc(sdev->pool, GFP_NOWAIT, &p_lli);
  483. if (!v_lli) {
  484. dev_err(sdev->slave.dev, "Failed to alloc lli memory\n");
  485. goto err_txd_free;
  486. }
  487. v_lli->src = src;
  488. v_lli->dst = dest;
  489. v_lli->len = len;
  490. v_lli->para = NORMAL_WAIT;
  491. burst = convert_burst(8);
  492. width = convert_buswidth(DMA_SLAVE_BUSWIDTH_4_BYTES);
  493. v_lli->cfg = DMA_CHAN_CFG_SRC_DRQ(DRQ_SDRAM) |
  494. DMA_CHAN_CFG_DST_DRQ(DRQ_SDRAM) |
  495. DMA_CHAN_CFG_DST_LINEAR_MODE |
  496. DMA_CHAN_CFG_SRC_LINEAR_MODE |
  497. DMA_CHAN_CFG_SRC_BURST(burst) |
  498. DMA_CHAN_CFG_SRC_WIDTH(width) |
  499. DMA_CHAN_CFG_DST_BURST(burst) |
  500. DMA_CHAN_CFG_DST_WIDTH(width);
  501. sun6i_dma_lli_add(NULL, v_lli, p_lli, txd);
  502. sun6i_dma_dump_lli(vchan, v_lli);
  503. return vchan_tx_prep(&vchan->vc, &txd->vd, flags);
  504. err_txd_free:
  505. kfree(txd);
  506. return NULL;
  507. }
  508. static struct dma_async_tx_descriptor *sun6i_dma_prep_slave_sg(
  509. struct dma_chan *chan, struct scatterlist *sgl,
  510. unsigned int sg_len, enum dma_transfer_direction dir,
  511. unsigned long flags, void *context)
  512. {
  513. struct sun6i_dma_dev *sdev = to_sun6i_dma_dev(chan->device);
  514. struct sun6i_vchan *vchan = to_sun6i_vchan(chan);
  515. struct dma_slave_config *sconfig = &vchan->cfg;
  516. struct sun6i_dma_lli *v_lli, *prev = NULL;
  517. struct sun6i_desc *txd;
  518. struct scatterlist *sg;
  519. dma_addr_t p_lli;
  520. u32 lli_cfg;
  521. int i, ret;
  522. if (!sgl)
  523. return NULL;
  524. ret = set_config(sdev, sconfig, dir, &lli_cfg);
  525. if (ret) {
  526. dev_err(chan2dev(chan), "Invalid DMA configuration\n");
  527. return NULL;
  528. }
  529. txd = kzalloc(sizeof(*txd), GFP_NOWAIT);
  530. if (!txd)
  531. return NULL;
  532. for_each_sg(sgl, sg, sg_len, i) {
  533. v_lli = dma_pool_alloc(sdev->pool, GFP_NOWAIT, &p_lli);
  534. if (!v_lli)
  535. goto err_lli_free;
  536. v_lli->len = sg_dma_len(sg);
  537. v_lli->para = NORMAL_WAIT;
  538. if (dir == DMA_MEM_TO_DEV) {
  539. v_lli->src = sg_dma_address(sg);
  540. v_lli->dst = sconfig->dst_addr;
  541. v_lli->cfg = lli_cfg |
  542. DMA_CHAN_CFG_DST_IO_MODE |
  543. DMA_CHAN_CFG_SRC_LINEAR_MODE |
  544. DMA_CHAN_CFG_SRC_DRQ(DRQ_SDRAM) |
  545. DMA_CHAN_CFG_DST_DRQ(vchan->port);
  546. dev_dbg(chan2dev(chan),
  547. "%s; chan: %d, dest: %pad, src: %pad, len: %u. flags: 0x%08lx\n",
  548. __func__, vchan->vc.chan.chan_id,
  549. &sconfig->dst_addr, &sg_dma_address(sg),
  550. sg_dma_len(sg), flags);
  551. } else {
  552. v_lli->src = sconfig->src_addr;
  553. v_lli->dst = sg_dma_address(sg);
  554. v_lli->cfg = lli_cfg |
  555. DMA_CHAN_CFG_DST_LINEAR_MODE |
  556. DMA_CHAN_CFG_SRC_IO_MODE |
  557. DMA_CHAN_CFG_DST_DRQ(DRQ_SDRAM) |
  558. DMA_CHAN_CFG_SRC_DRQ(vchan->port);
  559. dev_dbg(chan2dev(chan),
  560. "%s; chan: %d, dest: %pad, src: %pad, len: %u. flags: 0x%08lx\n",
  561. __func__, vchan->vc.chan.chan_id,
  562. &sg_dma_address(sg), &sconfig->src_addr,
  563. sg_dma_len(sg), flags);
  564. }
  565. prev = sun6i_dma_lli_add(prev, v_lli, p_lli, txd);
  566. }
  567. dev_dbg(chan2dev(chan), "First: %pad\n", &txd->p_lli);
  568. for (prev = txd->v_lli; prev; prev = prev->v_lli_next)
  569. sun6i_dma_dump_lli(vchan, prev);
  570. return vchan_tx_prep(&vchan->vc, &txd->vd, flags);
  571. err_lli_free:
  572. for (prev = txd->v_lli; prev; prev = prev->v_lli_next)
  573. dma_pool_free(sdev->pool, prev, virt_to_phys(prev));
  574. kfree(txd);
  575. return NULL;
  576. }
  577. static struct dma_async_tx_descriptor *sun6i_dma_prep_dma_cyclic(
  578. struct dma_chan *chan,
  579. dma_addr_t buf_addr,
  580. size_t buf_len,
  581. size_t period_len,
  582. enum dma_transfer_direction dir,
  583. unsigned long flags)
  584. {
  585. struct sun6i_dma_dev *sdev = to_sun6i_dma_dev(chan->device);
  586. struct sun6i_vchan *vchan = to_sun6i_vchan(chan);
  587. struct dma_slave_config *sconfig = &vchan->cfg;
  588. struct sun6i_dma_lli *v_lli, *prev = NULL;
  589. struct sun6i_desc *txd;
  590. dma_addr_t p_lli;
  591. u32 lli_cfg;
  592. unsigned int i, periods = buf_len / period_len;
  593. int ret;
  594. ret = set_config(sdev, sconfig, dir, &lli_cfg);
  595. if (ret) {
  596. dev_err(chan2dev(chan), "Invalid DMA configuration\n");
  597. return NULL;
  598. }
  599. txd = kzalloc(sizeof(*txd), GFP_NOWAIT);
  600. if (!txd)
  601. return NULL;
  602. for (i = 0; i < periods; i++) {
  603. v_lli = dma_pool_alloc(sdev->pool, GFP_NOWAIT, &p_lli);
  604. if (!v_lli) {
  605. dev_err(sdev->slave.dev, "Failed to alloc lli memory\n");
  606. goto err_lli_free;
  607. }
  608. v_lli->len = period_len;
  609. v_lli->para = NORMAL_WAIT;
  610. if (dir == DMA_MEM_TO_DEV) {
  611. v_lli->src = buf_addr + period_len * i;
  612. v_lli->dst = sconfig->dst_addr;
  613. v_lli->cfg = lli_cfg |
  614. DMA_CHAN_CFG_DST_IO_MODE |
  615. DMA_CHAN_CFG_SRC_LINEAR_MODE |
  616. DMA_CHAN_CFG_SRC_DRQ(DRQ_SDRAM) |
  617. DMA_CHAN_CFG_DST_DRQ(vchan->port);
  618. } else {
  619. v_lli->src = sconfig->src_addr;
  620. v_lli->dst = buf_addr + period_len * i;
  621. v_lli->cfg = lli_cfg |
  622. DMA_CHAN_CFG_DST_LINEAR_MODE |
  623. DMA_CHAN_CFG_SRC_IO_MODE |
  624. DMA_CHAN_CFG_DST_DRQ(DRQ_SDRAM) |
  625. DMA_CHAN_CFG_SRC_DRQ(vchan->port);
  626. }
  627. prev = sun6i_dma_lli_add(prev, v_lli, p_lli, txd);
  628. }
  629. prev->p_lli_next = txd->p_lli; /* cyclic list */
  630. vchan->cyclic = true;
  631. return vchan_tx_prep(&vchan->vc, &txd->vd, flags);
  632. err_lli_free:
  633. for (prev = txd->v_lli; prev; prev = prev->v_lli_next)
  634. dma_pool_free(sdev->pool, prev, virt_to_phys(prev));
  635. kfree(txd);
  636. return NULL;
  637. }
  638. static int sun6i_dma_config(struct dma_chan *chan,
  639. struct dma_slave_config *config)
  640. {
  641. struct sun6i_vchan *vchan = to_sun6i_vchan(chan);
  642. memcpy(&vchan->cfg, config, sizeof(*config));
  643. return 0;
  644. }
  645. static int sun6i_dma_pause(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. struct sun6i_pchan *pchan = vchan->phy;
  650. dev_dbg(chan2dev(chan), "vchan %p: pause\n", &vchan->vc);
  651. if (pchan) {
  652. writel(DMA_CHAN_PAUSE_PAUSE,
  653. pchan->base + DMA_CHAN_PAUSE);
  654. } else {
  655. spin_lock(&sdev->lock);
  656. list_del_init(&vchan->node);
  657. spin_unlock(&sdev->lock);
  658. }
  659. return 0;
  660. }
  661. static int sun6i_dma_resume(struct dma_chan *chan)
  662. {
  663. struct sun6i_dma_dev *sdev = to_sun6i_dma_dev(chan->device);
  664. struct sun6i_vchan *vchan = to_sun6i_vchan(chan);
  665. struct sun6i_pchan *pchan = vchan->phy;
  666. unsigned long flags;
  667. dev_dbg(chan2dev(chan), "vchan %p: resume\n", &vchan->vc);
  668. spin_lock_irqsave(&vchan->vc.lock, flags);
  669. if (pchan) {
  670. writel(DMA_CHAN_PAUSE_RESUME,
  671. pchan->base + DMA_CHAN_PAUSE);
  672. } else if (!list_empty(&vchan->vc.desc_issued)) {
  673. spin_lock(&sdev->lock);
  674. list_add_tail(&vchan->node, &sdev->pending);
  675. spin_unlock(&sdev->lock);
  676. }
  677. spin_unlock_irqrestore(&vchan->vc.lock, flags);
  678. return 0;
  679. }
  680. static int sun6i_dma_terminate_all(struct dma_chan *chan)
  681. {
  682. struct sun6i_dma_dev *sdev = to_sun6i_dma_dev(chan->device);
  683. struct sun6i_vchan *vchan = to_sun6i_vchan(chan);
  684. struct sun6i_pchan *pchan = vchan->phy;
  685. unsigned long flags;
  686. LIST_HEAD(head);
  687. spin_lock(&sdev->lock);
  688. list_del_init(&vchan->node);
  689. spin_unlock(&sdev->lock);
  690. spin_lock_irqsave(&vchan->vc.lock, flags);
  691. if (vchan->cyclic) {
  692. vchan->cyclic = false;
  693. if (pchan && pchan->desc) {
  694. struct virt_dma_desc *vd = &pchan->desc->vd;
  695. struct virt_dma_chan *vc = &vchan->vc;
  696. list_add_tail(&vd->node, &vc->desc_completed);
  697. }
  698. }
  699. vchan_get_all_descriptors(&vchan->vc, &head);
  700. if (pchan) {
  701. writel(DMA_CHAN_ENABLE_STOP, pchan->base + DMA_CHAN_ENABLE);
  702. writel(DMA_CHAN_PAUSE_RESUME, pchan->base + DMA_CHAN_PAUSE);
  703. vchan->phy = NULL;
  704. pchan->vchan = NULL;
  705. pchan->desc = NULL;
  706. pchan->done = NULL;
  707. }
  708. spin_unlock_irqrestore(&vchan->vc.lock, flags);
  709. vchan_dma_desc_free_list(&vchan->vc, &head);
  710. return 0;
  711. }
  712. static enum dma_status sun6i_dma_tx_status(struct dma_chan *chan,
  713. dma_cookie_t cookie,
  714. struct dma_tx_state *state)
  715. {
  716. struct sun6i_vchan *vchan = to_sun6i_vchan(chan);
  717. struct sun6i_pchan *pchan = vchan->phy;
  718. struct sun6i_dma_lli *lli;
  719. struct virt_dma_desc *vd;
  720. struct sun6i_desc *txd;
  721. enum dma_status ret;
  722. unsigned long flags;
  723. size_t bytes = 0;
  724. ret = dma_cookie_status(chan, cookie, state);
  725. if (ret == DMA_COMPLETE || !state)
  726. return ret;
  727. spin_lock_irqsave(&vchan->vc.lock, flags);
  728. vd = vchan_find_desc(&vchan->vc, cookie);
  729. txd = to_sun6i_desc(&vd->tx);
  730. if (vd) {
  731. for (lli = txd->v_lli; lli != NULL; lli = lli->v_lli_next)
  732. bytes += lli->len;
  733. } else if (!pchan || !pchan->desc) {
  734. bytes = 0;
  735. } else {
  736. bytes = sun6i_get_chan_size(pchan);
  737. }
  738. spin_unlock_irqrestore(&vchan->vc.lock, flags);
  739. dma_set_residue(state, bytes);
  740. return ret;
  741. }
  742. static void sun6i_dma_issue_pending(struct dma_chan *chan)
  743. {
  744. struct sun6i_dma_dev *sdev = to_sun6i_dma_dev(chan->device);
  745. struct sun6i_vchan *vchan = to_sun6i_vchan(chan);
  746. unsigned long flags;
  747. spin_lock_irqsave(&vchan->vc.lock, flags);
  748. if (vchan_issue_pending(&vchan->vc)) {
  749. spin_lock(&sdev->lock);
  750. if (!vchan->phy && list_empty(&vchan->node)) {
  751. list_add_tail(&vchan->node, &sdev->pending);
  752. tasklet_schedule(&sdev->task);
  753. dev_dbg(chan2dev(chan), "vchan %p: issued\n",
  754. &vchan->vc);
  755. }
  756. spin_unlock(&sdev->lock);
  757. } else {
  758. dev_dbg(chan2dev(chan), "vchan %p: nothing to issue\n",
  759. &vchan->vc);
  760. }
  761. spin_unlock_irqrestore(&vchan->vc.lock, flags);
  762. }
  763. static void sun6i_dma_free_chan_resources(struct dma_chan *chan)
  764. {
  765. struct sun6i_dma_dev *sdev = to_sun6i_dma_dev(chan->device);
  766. struct sun6i_vchan *vchan = to_sun6i_vchan(chan);
  767. unsigned long flags;
  768. spin_lock_irqsave(&sdev->lock, flags);
  769. list_del_init(&vchan->node);
  770. spin_unlock_irqrestore(&sdev->lock, flags);
  771. vchan_free_chan_resources(&vchan->vc);
  772. }
  773. static struct dma_chan *sun6i_dma_of_xlate(struct of_phandle_args *dma_spec,
  774. struct of_dma *ofdma)
  775. {
  776. struct sun6i_dma_dev *sdev = ofdma->of_dma_data;
  777. struct sun6i_vchan *vchan;
  778. struct dma_chan *chan;
  779. u8 port = dma_spec->args[0];
  780. if (port > sdev->cfg->nr_max_requests)
  781. return NULL;
  782. chan = dma_get_any_slave_channel(&sdev->slave);
  783. if (!chan)
  784. return NULL;
  785. vchan = to_sun6i_vchan(chan);
  786. vchan->port = port;
  787. return chan;
  788. }
  789. static inline void sun6i_kill_tasklet(struct sun6i_dma_dev *sdev)
  790. {
  791. /* Disable all interrupts from DMA */
  792. writel(0, sdev->base + DMA_IRQ_EN(0));
  793. writel(0, sdev->base + DMA_IRQ_EN(1));
  794. /* Prevent spurious interrupts from scheduling the tasklet */
  795. atomic_inc(&sdev->tasklet_shutdown);
  796. /* Make sure we won't have any further interrupts */
  797. devm_free_irq(sdev->slave.dev, sdev->irq, sdev);
  798. /* Actually prevent the tasklet from being scheduled */
  799. tasklet_kill(&sdev->task);
  800. }
  801. static inline void sun6i_dma_free(struct sun6i_dma_dev *sdev)
  802. {
  803. int i;
  804. for (i = 0; i < sdev->cfg->nr_max_vchans; i++) {
  805. struct sun6i_vchan *vchan = &sdev->vchans[i];
  806. list_del(&vchan->vc.chan.device_node);
  807. tasklet_kill(&vchan->vc.task);
  808. }
  809. }
  810. /*
  811. * For A31:
  812. *
  813. * There's 16 physical channels that can work in parallel.
  814. *
  815. * However we have 30 different endpoints for our requests.
  816. *
  817. * Since the channels are able to handle only an unidirectional
  818. * transfer, we need to allocate more virtual channels so that
  819. * everyone can grab one channel.
  820. *
  821. * Some devices can't work in both direction (mostly because it
  822. * wouldn't make sense), so we have a bit fewer virtual channels than
  823. * 2 channels per endpoints.
  824. */
  825. static struct sun6i_dma_config sun6i_a31_dma_cfg = {
  826. .nr_max_channels = 16,
  827. .nr_max_requests = 30,
  828. .nr_max_vchans = 53,
  829. };
  830. /*
  831. * The A23 only has 8 physical channels, a maximum DRQ port id of 24,
  832. * and a total of 37 usable source and destination endpoints.
  833. */
  834. static struct sun6i_dma_config sun8i_a23_dma_cfg = {
  835. .nr_max_channels = 8,
  836. .nr_max_requests = 24,
  837. .nr_max_vchans = 37,
  838. .gate_needed = true,
  839. };
  840. static struct sun6i_dma_config sun8i_a83t_dma_cfg = {
  841. .nr_max_channels = 8,
  842. .nr_max_requests = 28,
  843. .nr_max_vchans = 39,
  844. };
  845. /*
  846. * The H3 has 12 physical channels, a maximum DRQ port id of 27,
  847. * and a total of 34 usable source and destination endpoints.
  848. */
  849. static struct sun6i_dma_config sun8i_h3_dma_cfg = {
  850. .nr_max_channels = 12,
  851. .nr_max_requests = 27,
  852. .nr_max_vchans = 34,
  853. };
  854. /*
  855. * The V3s have only 8 physical channels, a maximum DRQ port id of 23,
  856. * and a total of 24 usable source and destination endpoints.
  857. */
  858. static struct sun6i_dma_config sun8i_v3s_dma_cfg = {
  859. .nr_max_channels = 8,
  860. .nr_max_requests = 23,
  861. .nr_max_vchans = 24,
  862. .gate_needed = true,
  863. };
  864. static const struct of_device_id sun6i_dma_match[] = {
  865. { .compatible = "allwinner,sun6i-a31-dma", .data = &sun6i_a31_dma_cfg },
  866. { .compatible = "allwinner,sun8i-a23-dma", .data = &sun8i_a23_dma_cfg },
  867. { .compatible = "allwinner,sun8i-a83t-dma", .data = &sun8i_a83t_dma_cfg },
  868. { .compatible = "allwinner,sun8i-h3-dma", .data = &sun8i_h3_dma_cfg },
  869. { .compatible = "allwinner,sun8i-v3s-dma", .data = &sun8i_v3s_dma_cfg },
  870. { /* sentinel */ }
  871. };
  872. MODULE_DEVICE_TABLE(of, sun6i_dma_match);
  873. static int sun6i_dma_probe(struct platform_device *pdev)
  874. {
  875. const struct of_device_id *device;
  876. struct sun6i_dma_dev *sdc;
  877. struct resource *res;
  878. int ret, i;
  879. sdc = devm_kzalloc(&pdev->dev, sizeof(*sdc), GFP_KERNEL);
  880. if (!sdc)
  881. return -ENOMEM;
  882. device = of_match_device(sun6i_dma_match, &pdev->dev);
  883. if (!device)
  884. return -ENODEV;
  885. sdc->cfg = device->data;
  886. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  887. sdc->base = devm_ioremap_resource(&pdev->dev, res);
  888. if (IS_ERR(sdc->base))
  889. return PTR_ERR(sdc->base);
  890. sdc->irq = platform_get_irq(pdev, 0);
  891. if (sdc->irq < 0) {
  892. dev_err(&pdev->dev, "Cannot claim IRQ\n");
  893. return sdc->irq;
  894. }
  895. sdc->clk = devm_clk_get(&pdev->dev, NULL);
  896. if (IS_ERR(sdc->clk)) {
  897. dev_err(&pdev->dev, "No clock specified\n");
  898. return PTR_ERR(sdc->clk);
  899. }
  900. sdc->rstc = devm_reset_control_get(&pdev->dev, NULL);
  901. if (IS_ERR(sdc->rstc)) {
  902. dev_err(&pdev->dev, "No reset controller specified\n");
  903. return PTR_ERR(sdc->rstc);
  904. }
  905. sdc->pool = dmam_pool_create(dev_name(&pdev->dev), &pdev->dev,
  906. sizeof(struct sun6i_dma_lli), 4, 0);
  907. if (!sdc->pool) {
  908. dev_err(&pdev->dev, "No memory for descriptors dma pool\n");
  909. return -ENOMEM;
  910. }
  911. platform_set_drvdata(pdev, sdc);
  912. INIT_LIST_HEAD(&sdc->pending);
  913. spin_lock_init(&sdc->lock);
  914. dma_cap_set(DMA_PRIVATE, sdc->slave.cap_mask);
  915. dma_cap_set(DMA_MEMCPY, sdc->slave.cap_mask);
  916. dma_cap_set(DMA_SLAVE, sdc->slave.cap_mask);
  917. dma_cap_set(DMA_CYCLIC, sdc->slave.cap_mask);
  918. INIT_LIST_HEAD(&sdc->slave.channels);
  919. sdc->slave.device_free_chan_resources = sun6i_dma_free_chan_resources;
  920. sdc->slave.device_tx_status = sun6i_dma_tx_status;
  921. sdc->slave.device_issue_pending = sun6i_dma_issue_pending;
  922. sdc->slave.device_prep_slave_sg = sun6i_dma_prep_slave_sg;
  923. sdc->slave.device_prep_dma_memcpy = sun6i_dma_prep_dma_memcpy;
  924. sdc->slave.device_prep_dma_cyclic = sun6i_dma_prep_dma_cyclic;
  925. sdc->slave.copy_align = DMAENGINE_ALIGN_4_BYTES;
  926. sdc->slave.device_config = sun6i_dma_config;
  927. sdc->slave.device_pause = sun6i_dma_pause;
  928. sdc->slave.device_resume = sun6i_dma_resume;
  929. sdc->slave.device_terminate_all = sun6i_dma_terminate_all;
  930. sdc->slave.src_addr_widths = BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) |
  931. BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) |
  932. BIT(DMA_SLAVE_BUSWIDTH_4_BYTES);
  933. sdc->slave.dst_addr_widths = BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) |
  934. BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) |
  935. BIT(DMA_SLAVE_BUSWIDTH_4_BYTES);
  936. sdc->slave.directions = BIT(DMA_DEV_TO_MEM) |
  937. BIT(DMA_MEM_TO_DEV);
  938. sdc->slave.residue_granularity = DMA_RESIDUE_GRANULARITY_BURST;
  939. sdc->slave.dev = &pdev->dev;
  940. sdc->pchans = devm_kcalloc(&pdev->dev, sdc->cfg->nr_max_channels,
  941. sizeof(struct sun6i_pchan), GFP_KERNEL);
  942. if (!sdc->pchans)
  943. return -ENOMEM;
  944. sdc->vchans = devm_kcalloc(&pdev->dev, sdc->cfg->nr_max_vchans,
  945. sizeof(struct sun6i_vchan), GFP_KERNEL);
  946. if (!sdc->vchans)
  947. return -ENOMEM;
  948. tasklet_init(&sdc->task, sun6i_dma_tasklet, (unsigned long)sdc);
  949. for (i = 0; i < sdc->cfg->nr_max_channels; i++) {
  950. struct sun6i_pchan *pchan = &sdc->pchans[i];
  951. pchan->idx = i;
  952. pchan->base = sdc->base + 0x100 + i * 0x40;
  953. }
  954. for (i = 0; i < sdc->cfg->nr_max_vchans; i++) {
  955. struct sun6i_vchan *vchan = &sdc->vchans[i];
  956. INIT_LIST_HEAD(&vchan->node);
  957. vchan->vc.desc_free = sun6i_dma_free_desc;
  958. vchan_init(&vchan->vc, &sdc->slave);
  959. }
  960. ret = reset_control_deassert(sdc->rstc);
  961. if (ret) {
  962. dev_err(&pdev->dev, "Couldn't deassert the device from reset\n");
  963. goto err_chan_free;
  964. }
  965. ret = clk_prepare_enable(sdc->clk);
  966. if (ret) {
  967. dev_err(&pdev->dev, "Couldn't enable the clock\n");
  968. goto err_reset_assert;
  969. }
  970. ret = devm_request_irq(&pdev->dev, sdc->irq, sun6i_dma_interrupt, 0,
  971. dev_name(&pdev->dev), sdc);
  972. if (ret) {
  973. dev_err(&pdev->dev, "Cannot request IRQ\n");
  974. goto err_clk_disable;
  975. }
  976. ret = dma_async_device_register(&sdc->slave);
  977. if (ret) {
  978. dev_warn(&pdev->dev, "Failed to register DMA engine device\n");
  979. goto err_irq_disable;
  980. }
  981. ret = of_dma_controller_register(pdev->dev.of_node, sun6i_dma_of_xlate,
  982. sdc);
  983. if (ret) {
  984. dev_err(&pdev->dev, "of_dma_controller_register failed\n");
  985. goto err_dma_unregister;
  986. }
  987. if (sdc->cfg->gate_needed)
  988. writel(SUN8I_DMA_GATE_ENABLE, sdc->base + SUN8I_DMA_GATE);
  989. return 0;
  990. err_dma_unregister:
  991. dma_async_device_unregister(&sdc->slave);
  992. err_irq_disable:
  993. sun6i_kill_tasklet(sdc);
  994. err_clk_disable:
  995. clk_disable_unprepare(sdc->clk);
  996. err_reset_assert:
  997. reset_control_assert(sdc->rstc);
  998. err_chan_free:
  999. sun6i_dma_free(sdc);
  1000. return ret;
  1001. }
  1002. static int sun6i_dma_remove(struct platform_device *pdev)
  1003. {
  1004. struct sun6i_dma_dev *sdc = platform_get_drvdata(pdev);
  1005. of_dma_controller_free(pdev->dev.of_node);
  1006. dma_async_device_unregister(&sdc->slave);
  1007. sun6i_kill_tasklet(sdc);
  1008. clk_disable_unprepare(sdc->clk);
  1009. reset_control_assert(sdc->rstc);
  1010. sun6i_dma_free(sdc);
  1011. return 0;
  1012. }
  1013. static struct platform_driver sun6i_dma_driver = {
  1014. .probe = sun6i_dma_probe,
  1015. .remove = sun6i_dma_remove,
  1016. .driver = {
  1017. .name = "sun6i-dma",
  1018. .of_match_table = sun6i_dma_match,
  1019. },
  1020. };
  1021. module_platform_driver(sun6i_dma_driver);
  1022. MODULE_DESCRIPTION("Allwinner A31 DMA Controller Driver");
  1023. MODULE_AUTHOR("Sugar <shuge@allwinnertech.com>");
  1024. MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>");
  1025. MODULE_LICENSE("GPL");