sun6i-dma.c 41 KB

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