sun6i-dma.c 31 KB

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