sun6i-dma.c 35 KB

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