stm32-dma.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137
  1. /*
  2. * Driver for STM32 DMA controller
  3. *
  4. * Inspired by dma-jz4740.c and tegra20-apb-dma.c
  5. *
  6. * Copyright (C) M'boumba Cedric Madianga 2015
  7. * Author: M'boumba Cedric Madianga <cedric.madianga@gmail.com>
  8. *
  9. * License terms: GNU General Public License (GPL), version 2
  10. */
  11. #include <linux/clk.h>
  12. #include <linux/delay.h>
  13. #include <linux/dmaengine.h>
  14. #include <linux/dma-mapping.h>
  15. #include <linux/err.h>
  16. #include <linux/init.h>
  17. #include <linux/jiffies.h>
  18. #include <linux/list.h>
  19. #include <linux/module.h>
  20. #include <linux/of.h>
  21. #include <linux/of_device.h>
  22. #include <linux/of_dma.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/reset.h>
  25. #include <linux/sched.h>
  26. #include <linux/slab.h>
  27. #include "virt-dma.h"
  28. #define STM32_DMA_LISR 0x0000 /* DMA Low Int Status Reg */
  29. #define STM32_DMA_HISR 0x0004 /* DMA High Int Status Reg */
  30. #define STM32_DMA_LIFCR 0x0008 /* DMA Low Int Flag Clear Reg */
  31. #define STM32_DMA_HIFCR 0x000c /* DMA High Int Flag Clear Reg */
  32. #define STM32_DMA_TCI BIT(5) /* Transfer Complete Interrupt */
  33. #define STM32_DMA_TEI BIT(3) /* Transfer Error Interrupt */
  34. #define STM32_DMA_DMEI BIT(2) /* Direct Mode Error Interrupt */
  35. #define STM32_DMA_FEI BIT(0) /* FIFO Error Interrupt */
  36. /* DMA Stream x Configuration Register */
  37. #define STM32_DMA_SCR(x) (0x0010 + 0x18 * (x)) /* x = 0..7 */
  38. #define STM32_DMA_SCR_REQ(n) ((n & 0x7) << 25)
  39. #define STM32_DMA_SCR_MBURST_MASK GENMASK(24, 23)
  40. #define STM32_DMA_SCR_MBURST(n) ((n & 0x3) << 23)
  41. #define STM32_DMA_SCR_PBURST_MASK GENMASK(22, 21)
  42. #define STM32_DMA_SCR_PBURST(n) ((n & 0x3) << 21)
  43. #define STM32_DMA_SCR_PL_MASK GENMASK(17, 16)
  44. #define STM32_DMA_SCR_PL(n) ((n & 0x3) << 16)
  45. #define STM32_DMA_SCR_MSIZE_MASK GENMASK(14, 13)
  46. #define STM32_DMA_SCR_MSIZE(n) ((n & 0x3) << 13)
  47. #define STM32_DMA_SCR_PSIZE_MASK GENMASK(12, 11)
  48. #define STM32_DMA_SCR_PSIZE(n) ((n & 0x3) << 11)
  49. #define STM32_DMA_SCR_PSIZE_GET(n) ((n & STM32_DMA_SCR_PSIZE_MASK) >> 11)
  50. #define STM32_DMA_SCR_DIR_MASK GENMASK(7, 6)
  51. #define STM32_DMA_SCR_DIR(n) ((n & 0x3) << 6)
  52. #define STM32_DMA_SCR_CT BIT(19) /* Target in double buffer */
  53. #define STM32_DMA_SCR_DBM BIT(18) /* Double Buffer Mode */
  54. #define STM32_DMA_SCR_PINCOS BIT(15) /* Peripheral inc offset size */
  55. #define STM32_DMA_SCR_MINC BIT(10) /* Memory increment mode */
  56. #define STM32_DMA_SCR_PINC BIT(9) /* Peripheral increment mode */
  57. #define STM32_DMA_SCR_CIRC BIT(8) /* Circular mode */
  58. #define STM32_DMA_SCR_PFCTRL BIT(5) /* Peripheral Flow Controller */
  59. #define STM32_DMA_SCR_TCIE BIT(4) /* Transfer Cplete Int Enable*/
  60. #define STM32_DMA_SCR_TEIE BIT(2) /* Transfer Error Int Enable */
  61. #define STM32_DMA_SCR_DMEIE BIT(1) /* Direct Mode Err Int Enable */
  62. #define STM32_DMA_SCR_EN BIT(0) /* Stream Enable */
  63. #define STM32_DMA_SCR_CFG_MASK (STM32_DMA_SCR_PINC \
  64. | STM32_DMA_SCR_MINC \
  65. | STM32_DMA_SCR_PINCOS \
  66. | STM32_DMA_SCR_PL_MASK)
  67. #define STM32_DMA_SCR_IRQ_MASK (STM32_DMA_SCR_TCIE \
  68. | STM32_DMA_SCR_TEIE \
  69. | STM32_DMA_SCR_DMEIE)
  70. /* DMA Stream x number of data register */
  71. #define STM32_DMA_SNDTR(x) (0x0014 + 0x18 * (x))
  72. /* DMA stream peripheral address register */
  73. #define STM32_DMA_SPAR(x) (0x0018 + 0x18 * (x))
  74. /* DMA stream x memory 0 address register */
  75. #define STM32_DMA_SM0AR(x) (0x001c + 0x18 * (x))
  76. /* DMA stream x memory 1 address register */
  77. #define STM32_DMA_SM1AR(x) (0x0020 + 0x18 * (x))
  78. /* DMA stream x FIFO control register */
  79. #define STM32_DMA_SFCR(x) (0x0024 + 0x18 * (x))
  80. #define STM32_DMA_SFCR_FTH_MASK GENMASK(1, 0)
  81. #define STM32_DMA_SFCR_FTH(n) (n & STM32_DMA_SFCR_FTH_MASK)
  82. #define STM32_DMA_SFCR_FEIE BIT(7) /* FIFO error interrupt enable */
  83. #define STM32_DMA_SFCR_DMDIS BIT(2) /* Direct mode disable */
  84. #define STM32_DMA_SFCR_MASK (STM32_DMA_SFCR_FEIE \
  85. | STM32_DMA_SFCR_DMDIS)
  86. /* DMA direction */
  87. #define STM32_DMA_DEV_TO_MEM 0x00
  88. #define STM32_DMA_MEM_TO_DEV 0x01
  89. #define STM32_DMA_MEM_TO_MEM 0x02
  90. /* DMA priority level */
  91. #define STM32_DMA_PRIORITY_LOW 0x00
  92. #define STM32_DMA_PRIORITY_MEDIUM 0x01
  93. #define STM32_DMA_PRIORITY_HIGH 0x02
  94. #define STM32_DMA_PRIORITY_VERY_HIGH 0x03
  95. /* DMA FIFO threshold selection */
  96. #define STM32_DMA_FIFO_THRESHOLD_1QUARTERFULL 0x00
  97. #define STM32_DMA_FIFO_THRESHOLD_HALFFULL 0x01
  98. #define STM32_DMA_FIFO_THRESHOLD_3QUARTERSFULL 0x02
  99. #define STM32_DMA_FIFO_THRESHOLD_FULL 0x03
  100. #define STM32_DMA_MAX_DATA_ITEMS 0xffff
  101. #define STM32_DMA_MAX_CHANNELS 0x08
  102. #define STM32_DMA_MAX_REQUEST_ID 0x08
  103. #define STM32_DMA_MAX_DATA_PARAM 0x03
  104. enum stm32_dma_width {
  105. STM32_DMA_BYTE,
  106. STM32_DMA_HALF_WORD,
  107. STM32_DMA_WORD,
  108. };
  109. enum stm32_dma_burst_size {
  110. STM32_DMA_BURST_SINGLE,
  111. STM32_DMA_BURST_INCR4,
  112. STM32_DMA_BURST_INCR8,
  113. STM32_DMA_BURST_INCR16,
  114. };
  115. struct stm32_dma_cfg {
  116. u32 channel_id;
  117. u32 request_line;
  118. u32 stream_config;
  119. u32 threshold;
  120. };
  121. struct stm32_dma_chan_reg {
  122. u32 dma_lisr;
  123. u32 dma_hisr;
  124. u32 dma_lifcr;
  125. u32 dma_hifcr;
  126. u32 dma_scr;
  127. u32 dma_sndtr;
  128. u32 dma_spar;
  129. u32 dma_sm0ar;
  130. u32 dma_sm1ar;
  131. u32 dma_sfcr;
  132. };
  133. struct stm32_dma_sg_req {
  134. u32 len;
  135. struct stm32_dma_chan_reg chan_reg;
  136. };
  137. struct stm32_dma_desc {
  138. struct virt_dma_desc vdesc;
  139. bool cyclic;
  140. u32 num_sgs;
  141. struct stm32_dma_sg_req sg_req[];
  142. };
  143. struct stm32_dma_chan {
  144. struct virt_dma_chan vchan;
  145. bool config_init;
  146. bool busy;
  147. u32 id;
  148. u32 irq;
  149. struct stm32_dma_desc *desc;
  150. u32 next_sg;
  151. struct dma_slave_config dma_sconfig;
  152. struct stm32_dma_chan_reg chan_reg;
  153. };
  154. struct stm32_dma_device {
  155. struct dma_device ddev;
  156. void __iomem *base;
  157. struct clk *clk;
  158. struct reset_control *rst;
  159. bool mem2mem;
  160. struct stm32_dma_chan chan[STM32_DMA_MAX_CHANNELS];
  161. };
  162. static struct stm32_dma_device *stm32_dma_get_dev(struct stm32_dma_chan *chan)
  163. {
  164. return container_of(chan->vchan.chan.device, struct stm32_dma_device,
  165. ddev);
  166. }
  167. static struct stm32_dma_chan *to_stm32_dma_chan(struct dma_chan *c)
  168. {
  169. return container_of(c, struct stm32_dma_chan, vchan.chan);
  170. }
  171. static struct stm32_dma_desc *to_stm32_dma_desc(struct virt_dma_desc *vdesc)
  172. {
  173. return container_of(vdesc, struct stm32_dma_desc, vdesc);
  174. }
  175. static struct device *chan2dev(struct stm32_dma_chan *chan)
  176. {
  177. return &chan->vchan.chan.dev->device;
  178. }
  179. static u32 stm32_dma_read(struct stm32_dma_device *dmadev, u32 reg)
  180. {
  181. return readl_relaxed(dmadev->base + reg);
  182. }
  183. static void stm32_dma_write(struct stm32_dma_device *dmadev, u32 reg, u32 val)
  184. {
  185. writel_relaxed(val, dmadev->base + reg);
  186. }
  187. static struct stm32_dma_desc *stm32_dma_alloc_desc(u32 num_sgs)
  188. {
  189. return kzalloc(sizeof(struct stm32_dma_desc) +
  190. sizeof(struct stm32_dma_sg_req) * num_sgs, GFP_NOWAIT);
  191. }
  192. static int stm32_dma_get_width(struct stm32_dma_chan *chan,
  193. enum dma_slave_buswidth width)
  194. {
  195. switch (width) {
  196. case DMA_SLAVE_BUSWIDTH_1_BYTE:
  197. return STM32_DMA_BYTE;
  198. case DMA_SLAVE_BUSWIDTH_2_BYTES:
  199. return STM32_DMA_HALF_WORD;
  200. case DMA_SLAVE_BUSWIDTH_4_BYTES:
  201. return STM32_DMA_WORD;
  202. default:
  203. dev_err(chan2dev(chan), "Dma bus width not supported\n");
  204. return -EINVAL;
  205. }
  206. }
  207. static int stm32_dma_get_burst(struct stm32_dma_chan *chan, u32 maxburst)
  208. {
  209. switch (maxburst) {
  210. case 0:
  211. case 1:
  212. return STM32_DMA_BURST_SINGLE;
  213. case 4:
  214. return STM32_DMA_BURST_INCR4;
  215. case 8:
  216. return STM32_DMA_BURST_INCR8;
  217. case 16:
  218. return STM32_DMA_BURST_INCR16;
  219. default:
  220. dev_err(chan2dev(chan), "Dma burst size not supported\n");
  221. return -EINVAL;
  222. }
  223. }
  224. static void stm32_dma_set_fifo_config(struct stm32_dma_chan *chan,
  225. u32 src_maxburst, u32 dst_maxburst)
  226. {
  227. chan->chan_reg.dma_sfcr &= ~STM32_DMA_SFCR_MASK;
  228. chan->chan_reg.dma_scr &= ~STM32_DMA_SCR_DMEIE;
  229. if ((!src_maxburst) && (!dst_maxburst)) {
  230. /* Using direct mode */
  231. chan->chan_reg.dma_scr |= STM32_DMA_SCR_DMEIE;
  232. } else {
  233. /* Using FIFO mode */
  234. chan->chan_reg.dma_sfcr |= STM32_DMA_SFCR_MASK;
  235. }
  236. }
  237. static int stm32_dma_slave_config(struct dma_chan *c,
  238. struct dma_slave_config *config)
  239. {
  240. struct stm32_dma_chan *chan = to_stm32_dma_chan(c);
  241. memcpy(&chan->dma_sconfig, config, sizeof(*config));
  242. chan->config_init = true;
  243. return 0;
  244. }
  245. static u32 stm32_dma_irq_status(struct stm32_dma_chan *chan)
  246. {
  247. struct stm32_dma_device *dmadev = stm32_dma_get_dev(chan);
  248. u32 flags, dma_isr;
  249. /*
  250. * Read "flags" from DMA_xISR register corresponding to the selected
  251. * DMA channel at the correct bit offset inside that register.
  252. *
  253. * If (ch % 4) is 2 or 3, left shift the mask by 16 bits.
  254. * If (ch % 4) is 1 or 3, additionally left shift the mask by 6 bits.
  255. */
  256. if (chan->id & 4)
  257. dma_isr = stm32_dma_read(dmadev, STM32_DMA_HISR);
  258. else
  259. dma_isr = stm32_dma_read(dmadev, STM32_DMA_LISR);
  260. flags = dma_isr >> (((chan->id & 2) << 3) | ((chan->id & 1) * 6));
  261. return flags;
  262. }
  263. static void stm32_dma_irq_clear(struct stm32_dma_chan *chan, u32 flags)
  264. {
  265. struct stm32_dma_device *dmadev = stm32_dma_get_dev(chan);
  266. u32 dma_ifcr;
  267. /*
  268. * Write "flags" to the DMA_xIFCR register corresponding to the selected
  269. * DMA channel at the correct bit offset inside that register.
  270. *
  271. * If (ch % 4) is 2 or 3, left shift the mask by 16 bits.
  272. * If (ch % 4) is 1 or 3, additionally left shift the mask by 6 bits.
  273. */
  274. dma_ifcr = flags << (((chan->id & 2) << 3) | ((chan->id & 1) * 6));
  275. if (chan->id & 4)
  276. stm32_dma_write(dmadev, STM32_DMA_HIFCR, dma_ifcr);
  277. else
  278. stm32_dma_write(dmadev, STM32_DMA_LIFCR, dma_ifcr);
  279. }
  280. static int stm32_dma_disable_chan(struct stm32_dma_chan *chan)
  281. {
  282. struct stm32_dma_device *dmadev = stm32_dma_get_dev(chan);
  283. unsigned long timeout = jiffies + msecs_to_jiffies(5000);
  284. u32 dma_scr, id;
  285. id = chan->id;
  286. dma_scr = stm32_dma_read(dmadev, STM32_DMA_SCR(id));
  287. if (dma_scr & STM32_DMA_SCR_EN) {
  288. dma_scr &= ~STM32_DMA_SCR_EN;
  289. stm32_dma_write(dmadev, STM32_DMA_SCR(id), dma_scr);
  290. do {
  291. dma_scr = stm32_dma_read(dmadev, STM32_DMA_SCR(id));
  292. dma_scr &= STM32_DMA_SCR_EN;
  293. if (!dma_scr)
  294. break;
  295. if (time_after_eq(jiffies, timeout)) {
  296. dev_err(chan2dev(chan), "%s: timeout!\n",
  297. __func__);
  298. return -EBUSY;
  299. }
  300. cond_resched();
  301. } while (1);
  302. }
  303. return 0;
  304. }
  305. static void stm32_dma_stop(struct stm32_dma_chan *chan)
  306. {
  307. struct stm32_dma_device *dmadev = stm32_dma_get_dev(chan);
  308. u32 dma_scr, dma_sfcr, status;
  309. int ret;
  310. /* Disable interrupts */
  311. dma_scr = stm32_dma_read(dmadev, STM32_DMA_SCR(chan->id));
  312. dma_scr &= ~STM32_DMA_SCR_IRQ_MASK;
  313. stm32_dma_write(dmadev, STM32_DMA_SCR(chan->id), dma_scr);
  314. dma_sfcr = stm32_dma_read(dmadev, STM32_DMA_SFCR(chan->id));
  315. dma_sfcr &= ~STM32_DMA_SFCR_FEIE;
  316. stm32_dma_write(dmadev, STM32_DMA_SFCR(chan->id), dma_sfcr);
  317. /* Disable DMA */
  318. ret = stm32_dma_disable_chan(chan);
  319. if (ret < 0)
  320. return;
  321. /* Clear interrupt status if it is there */
  322. status = stm32_dma_irq_status(chan);
  323. if (status) {
  324. dev_dbg(chan2dev(chan), "%s(): clearing interrupt: 0x%08x\n",
  325. __func__, status);
  326. stm32_dma_irq_clear(chan, status);
  327. }
  328. chan->busy = false;
  329. }
  330. static int stm32_dma_terminate_all(struct dma_chan *c)
  331. {
  332. struct stm32_dma_chan *chan = to_stm32_dma_chan(c);
  333. unsigned long flags;
  334. LIST_HEAD(head);
  335. spin_lock_irqsave(&chan->vchan.lock, flags);
  336. if (chan->busy) {
  337. stm32_dma_stop(chan);
  338. chan->desc = NULL;
  339. }
  340. vchan_get_all_descriptors(&chan->vchan, &head);
  341. spin_unlock_irqrestore(&chan->vchan.lock, flags);
  342. vchan_dma_desc_free_list(&chan->vchan, &head);
  343. return 0;
  344. }
  345. static void stm32_dma_dump_reg(struct stm32_dma_chan *chan)
  346. {
  347. struct stm32_dma_device *dmadev = stm32_dma_get_dev(chan);
  348. u32 scr = stm32_dma_read(dmadev, STM32_DMA_SCR(chan->id));
  349. u32 ndtr = stm32_dma_read(dmadev, STM32_DMA_SNDTR(chan->id));
  350. u32 spar = stm32_dma_read(dmadev, STM32_DMA_SPAR(chan->id));
  351. u32 sm0ar = stm32_dma_read(dmadev, STM32_DMA_SM0AR(chan->id));
  352. u32 sm1ar = stm32_dma_read(dmadev, STM32_DMA_SM1AR(chan->id));
  353. u32 sfcr = stm32_dma_read(dmadev, STM32_DMA_SFCR(chan->id));
  354. dev_dbg(chan2dev(chan), "SCR: 0x%08x\n", scr);
  355. dev_dbg(chan2dev(chan), "NDTR: 0x%08x\n", ndtr);
  356. dev_dbg(chan2dev(chan), "SPAR: 0x%08x\n", spar);
  357. dev_dbg(chan2dev(chan), "SM0AR: 0x%08x\n", sm0ar);
  358. dev_dbg(chan2dev(chan), "SM1AR: 0x%08x\n", sm1ar);
  359. dev_dbg(chan2dev(chan), "SFCR: 0x%08x\n", sfcr);
  360. }
  361. static int stm32_dma_start_transfer(struct stm32_dma_chan *chan)
  362. {
  363. struct stm32_dma_device *dmadev = stm32_dma_get_dev(chan);
  364. struct virt_dma_desc *vdesc;
  365. struct stm32_dma_sg_req *sg_req;
  366. struct stm32_dma_chan_reg *reg;
  367. u32 status;
  368. int ret;
  369. ret = stm32_dma_disable_chan(chan);
  370. if (ret < 0)
  371. return ret;
  372. if (!chan->desc) {
  373. vdesc = vchan_next_desc(&chan->vchan);
  374. if (!vdesc)
  375. return -EPERM;
  376. chan->desc = to_stm32_dma_desc(vdesc);
  377. chan->next_sg = 0;
  378. }
  379. if (chan->next_sg == chan->desc->num_sgs)
  380. chan->next_sg = 0;
  381. sg_req = &chan->desc->sg_req[chan->next_sg];
  382. reg = &sg_req->chan_reg;
  383. stm32_dma_write(dmadev, STM32_DMA_SCR(chan->id), reg->dma_scr);
  384. stm32_dma_write(dmadev, STM32_DMA_SPAR(chan->id), reg->dma_spar);
  385. stm32_dma_write(dmadev, STM32_DMA_SM0AR(chan->id), reg->dma_sm0ar);
  386. stm32_dma_write(dmadev, STM32_DMA_SFCR(chan->id), reg->dma_sfcr);
  387. stm32_dma_write(dmadev, STM32_DMA_SM1AR(chan->id), reg->dma_sm1ar);
  388. stm32_dma_write(dmadev, STM32_DMA_SNDTR(chan->id), reg->dma_sndtr);
  389. chan->next_sg++;
  390. /* Clear interrupt status if it is there */
  391. status = stm32_dma_irq_status(chan);
  392. if (status)
  393. stm32_dma_irq_clear(chan, status);
  394. stm32_dma_dump_reg(chan);
  395. /* Start DMA */
  396. reg->dma_scr |= STM32_DMA_SCR_EN;
  397. stm32_dma_write(dmadev, STM32_DMA_SCR(chan->id), reg->dma_scr);
  398. chan->busy = true;
  399. return 0;
  400. }
  401. static void stm32_dma_configure_next_sg(struct stm32_dma_chan *chan)
  402. {
  403. struct stm32_dma_device *dmadev = stm32_dma_get_dev(chan);
  404. struct stm32_dma_sg_req *sg_req;
  405. u32 dma_scr, dma_sm0ar, dma_sm1ar, id;
  406. id = chan->id;
  407. dma_scr = stm32_dma_read(dmadev, STM32_DMA_SCR(id));
  408. if (dma_scr & STM32_DMA_SCR_DBM) {
  409. if (chan->next_sg == chan->desc->num_sgs)
  410. chan->next_sg = 0;
  411. sg_req = &chan->desc->sg_req[chan->next_sg];
  412. if (dma_scr & STM32_DMA_SCR_CT) {
  413. dma_sm0ar = sg_req->chan_reg.dma_sm0ar;
  414. stm32_dma_write(dmadev, STM32_DMA_SM0AR(id), dma_sm0ar);
  415. dev_dbg(chan2dev(chan), "CT=1 <=> SM0AR: 0x%08x\n",
  416. stm32_dma_read(dmadev, STM32_DMA_SM0AR(id)));
  417. } else {
  418. dma_sm1ar = sg_req->chan_reg.dma_sm1ar;
  419. stm32_dma_write(dmadev, STM32_DMA_SM1AR(id), dma_sm1ar);
  420. dev_dbg(chan2dev(chan), "CT=0 <=> SM1AR: 0x%08x\n",
  421. stm32_dma_read(dmadev, STM32_DMA_SM1AR(id)));
  422. }
  423. chan->next_sg++;
  424. }
  425. }
  426. static void stm32_dma_handle_chan_done(struct stm32_dma_chan *chan)
  427. {
  428. if (chan->desc) {
  429. if (chan->desc->cyclic) {
  430. vchan_cyclic_callback(&chan->desc->vdesc);
  431. stm32_dma_configure_next_sg(chan);
  432. } else {
  433. chan->busy = false;
  434. if (chan->next_sg == chan->desc->num_sgs) {
  435. list_del(&chan->desc->vdesc.node);
  436. vchan_cookie_complete(&chan->desc->vdesc);
  437. chan->desc = NULL;
  438. }
  439. stm32_dma_start_transfer(chan);
  440. }
  441. }
  442. }
  443. static irqreturn_t stm32_dma_chan_irq(int irq, void *devid)
  444. {
  445. struct stm32_dma_chan *chan = devid;
  446. struct stm32_dma_device *dmadev = stm32_dma_get_dev(chan);
  447. u32 status, scr;
  448. spin_lock(&chan->vchan.lock);
  449. status = stm32_dma_irq_status(chan);
  450. scr = stm32_dma_read(dmadev, STM32_DMA_SCR(chan->id));
  451. if ((status & STM32_DMA_TCI) && (scr & STM32_DMA_SCR_TCIE)) {
  452. stm32_dma_irq_clear(chan, STM32_DMA_TCI);
  453. stm32_dma_handle_chan_done(chan);
  454. } else {
  455. stm32_dma_irq_clear(chan, status);
  456. dev_err(chan2dev(chan), "DMA error: status=0x%08x\n", status);
  457. }
  458. spin_unlock(&chan->vchan.lock);
  459. return IRQ_HANDLED;
  460. }
  461. static void stm32_dma_issue_pending(struct dma_chan *c)
  462. {
  463. struct stm32_dma_chan *chan = to_stm32_dma_chan(c);
  464. unsigned long flags;
  465. int ret;
  466. spin_lock_irqsave(&chan->vchan.lock, flags);
  467. if (!chan->busy) {
  468. if (vchan_issue_pending(&chan->vchan) && !chan->desc) {
  469. ret = stm32_dma_start_transfer(chan);
  470. if ((!ret) && (chan->desc->cyclic))
  471. stm32_dma_configure_next_sg(chan);
  472. }
  473. }
  474. spin_unlock_irqrestore(&chan->vchan.lock, flags);
  475. }
  476. static int stm32_dma_set_xfer_param(struct stm32_dma_chan *chan,
  477. enum dma_transfer_direction direction,
  478. enum dma_slave_buswidth *buswidth)
  479. {
  480. enum dma_slave_buswidth src_addr_width, dst_addr_width;
  481. int src_bus_width, dst_bus_width;
  482. int src_burst_size, dst_burst_size;
  483. u32 src_maxburst, dst_maxburst;
  484. u32 dma_scr = 0;
  485. src_addr_width = chan->dma_sconfig.src_addr_width;
  486. dst_addr_width = chan->dma_sconfig.dst_addr_width;
  487. src_maxburst = chan->dma_sconfig.src_maxburst;
  488. dst_maxburst = chan->dma_sconfig.dst_maxburst;
  489. switch (direction) {
  490. case DMA_MEM_TO_DEV:
  491. dst_bus_width = stm32_dma_get_width(chan, dst_addr_width);
  492. if (dst_bus_width < 0)
  493. return dst_bus_width;
  494. dst_burst_size = stm32_dma_get_burst(chan, dst_maxburst);
  495. if (dst_burst_size < 0)
  496. return dst_burst_size;
  497. if (!src_addr_width)
  498. src_addr_width = dst_addr_width;
  499. src_bus_width = stm32_dma_get_width(chan, src_addr_width);
  500. if (src_bus_width < 0)
  501. return src_bus_width;
  502. src_burst_size = stm32_dma_get_burst(chan, src_maxburst);
  503. if (src_burst_size < 0)
  504. return src_burst_size;
  505. dma_scr = STM32_DMA_SCR_DIR(STM32_DMA_MEM_TO_DEV) |
  506. STM32_DMA_SCR_PSIZE(dst_bus_width) |
  507. STM32_DMA_SCR_MSIZE(src_bus_width) |
  508. STM32_DMA_SCR_PBURST(dst_burst_size) |
  509. STM32_DMA_SCR_MBURST(src_burst_size);
  510. chan->chan_reg.dma_spar = chan->dma_sconfig.dst_addr;
  511. *buswidth = dst_addr_width;
  512. break;
  513. case DMA_DEV_TO_MEM:
  514. src_bus_width = stm32_dma_get_width(chan, src_addr_width);
  515. if (src_bus_width < 0)
  516. return src_bus_width;
  517. src_burst_size = stm32_dma_get_burst(chan, src_maxburst);
  518. if (src_burst_size < 0)
  519. return src_burst_size;
  520. if (!dst_addr_width)
  521. dst_addr_width = src_addr_width;
  522. dst_bus_width = stm32_dma_get_width(chan, dst_addr_width);
  523. if (dst_bus_width < 0)
  524. return dst_bus_width;
  525. dst_burst_size = stm32_dma_get_burst(chan, dst_maxburst);
  526. if (dst_burst_size < 0)
  527. return dst_burst_size;
  528. dma_scr = STM32_DMA_SCR_DIR(STM32_DMA_DEV_TO_MEM) |
  529. STM32_DMA_SCR_PSIZE(src_bus_width) |
  530. STM32_DMA_SCR_MSIZE(dst_bus_width) |
  531. STM32_DMA_SCR_PBURST(src_burst_size) |
  532. STM32_DMA_SCR_MBURST(dst_burst_size);
  533. chan->chan_reg.dma_spar = chan->dma_sconfig.src_addr;
  534. *buswidth = chan->dma_sconfig.src_addr_width;
  535. break;
  536. default:
  537. dev_err(chan2dev(chan), "Dma direction is not supported\n");
  538. return -EINVAL;
  539. }
  540. stm32_dma_set_fifo_config(chan, src_maxburst, dst_maxburst);
  541. chan->chan_reg.dma_scr &= ~(STM32_DMA_SCR_DIR_MASK |
  542. STM32_DMA_SCR_PSIZE_MASK | STM32_DMA_SCR_MSIZE_MASK |
  543. STM32_DMA_SCR_PBURST_MASK | STM32_DMA_SCR_MBURST_MASK);
  544. chan->chan_reg.dma_scr |= dma_scr;
  545. return 0;
  546. }
  547. static void stm32_dma_clear_reg(struct stm32_dma_chan_reg *regs)
  548. {
  549. memset(regs, 0, sizeof(struct stm32_dma_chan_reg));
  550. }
  551. static struct dma_async_tx_descriptor *stm32_dma_prep_slave_sg(
  552. struct dma_chan *c, struct scatterlist *sgl,
  553. u32 sg_len, enum dma_transfer_direction direction,
  554. unsigned long flags, void *context)
  555. {
  556. struct stm32_dma_chan *chan = to_stm32_dma_chan(c);
  557. struct stm32_dma_desc *desc;
  558. struct scatterlist *sg;
  559. enum dma_slave_buswidth buswidth;
  560. u32 nb_data_items;
  561. int i, ret;
  562. if (!chan->config_init) {
  563. dev_err(chan2dev(chan), "dma channel is not configured\n");
  564. return NULL;
  565. }
  566. if (sg_len < 1) {
  567. dev_err(chan2dev(chan), "Invalid segment length %d\n", sg_len);
  568. return NULL;
  569. }
  570. desc = stm32_dma_alloc_desc(sg_len);
  571. if (!desc)
  572. return NULL;
  573. ret = stm32_dma_set_xfer_param(chan, direction, &buswidth);
  574. if (ret < 0)
  575. goto err;
  576. /* Set peripheral flow controller */
  577. if (chan->dma_sconfig.device_fc)
  578. chan->chan_reg.dma_scr |= STM32_DMA_SCR_PFCTRL;
  579. else
  580. chan->chan_reg.dma_scr &= ~STM32_DMA_SCR_PFCTRL;
  581. for_each_sg(sgl, sg, sg_len, i) {
  582. desc->sg_req[i].len = sg_dma_len(sg);
  583. nb_data_items = desc->sg_req[i].len / buswidth;
  584. if (nb_data_items > STM32_DMA_MAX_DATA_ITEMS) {
  585. dev_err(chan2dev(chan), "nb items not supported\n");
  586. goto err;
  587. }
  588. stm32_dma_clear_reg(&desc->sg_req[i].chan_reg);
  589. desc->sg_req[i].chan_reg.dma_scr = chan->chan_reg.dma_scr;
  590. desc->sg_req[i].chan_reg.dma_sfcr = chan->chan_reg.dma_sfcr;
  591. desc->sg_req[i].chan_reg.dma_spar = chan->chan_reg.dma_spar;
  592. desc->sg_req[i].chan_reg.dma_sm0ar = sg_dma_address(sg);
  593. desc->sg_req[i].chan_reg.dma_sm1ar = sg_dma_address(sg);
  594. desc->sg_req[i].chan_reg.dma_sndtr = nb_data_items;
  595. }
  596. desc->num_sgs = sg_len;
  597. desc->cyclic = false;
  598. return vchan_tx_prep(&chan->vchan, &desc->vdesc, flags);
  599. err:
  600. kfree(desc);
  601. return NULL;
  602. }
  603. static struct dma_async_tx_descriptor *stm32_dma_prep_dma_cyclic(
  604. struct dma_chan *c, dma_addr_t buf_addr, size_t buf_len,
  605. size_t period_len, enum dma_transfer_direction direction,
  606. unsigned long flags)
  607. {
  608. struct stm32_dma_chan *chan = to_stm32_dma_chan(c);
  609. struct stm32_dma_desc *desc;
  610. enum dma_slave_buswidth buswidth;
  611. u32 num_periods, nb_data_items;
  612. int i, ret;
  613. if (!buf_len || !period_len) {
  614. dev_err(chan2dev(chan), "Invalid buffer/period len\n");
  615. return NULL;
  616. }
  617. if (!chan->config_init) {
  618. dev_err(chan2dev(chan), "dma channel is not configured\n");
  619. return NULL;
  620. }
  621. if (buf_len % period_len) {
  622. dev_err(chan2dev(chan), "buf_len not multiple of period_len\n");
  623. return NULL;
  624. }
  625. /*
  626. * We allow to take more number of requests till DMA is
  627. * not started. The driver will loop over all requests.
  628. * Once DMA is started then new requests can be queued only after
  629. * terminating the DMA.
  630. */
  631. if (chan->busy) {
  632. dev_err(chan2dev(chan), "Request not allowed when dma busy\n");
  633. return NULL;
  634. }
  635. ret = stm32_dma_set_xfer_param(chan, direction, &buswidth);
  636. if (ret < 0)
  637. return NULL;
  638. nb_data_items = period_len / buswidth;
  639. if (nb_data_items > STM32_DMA_MAX_DATA_ITEMS) {
  640. dev_err(chan2dev(chan), "number of items not supported\n");
  641. return NULL;
  642. }
  643. /* Enable Circular mode or double buffer mode */
  644. if (buf_len == period_len)
  645. chan->chan_reg.dma_scr |= STM32_DMA_SCR_CIRC;
  646. else
  647. chan->chan_reg.dma_scr |= STM32_DMA_SCR_DBM;
  648. /* Clear periph ctrl if client set it */
  649. chan->chan_reg.dma_scr &= ~STM32_DMA_SCR_PFCTRL;
  650. num_periods = buf_len / period_len;
  651. desc = stm32_dma_alloc_desc(num_periods);
  652. if (!desc)
  653. return NULL;
  654. for (i = 0; i < num_periods; i++) {
  655. desc->sg_req[i].len = period_len;
  656. stm32_dma_clear_reg(&desc->sg_req[i].chan_reg);
  657. desc->sg_req[i].chan_reg.dma_scr = chan->chan_reg.dma_scr;
  658. desc->sg_req[i].chan_reg.dma_sfcr = chan->chan_reg.dma_sfcr;
  659. desc->sg_req[i].chan_reg.dma_spar = chan->chan_reg.dma_spar;
  660. desc->sg_req[i].chan_reg.dma_sm0ar = buf_addr;
  661. desc->sg_req[i].chan_reg.dma_sm1ar = buf_addr;
  662. desc->sg_req[i].chan_reg.dma_sndtr = nb_data_items;
  663. buf_addr += period_len;
  664. }
  665. desc->num_sgs = num_periods;
  666. desc->cyclic = true;
  667. return vchan_tx_prep(&chan->vchan, &desc->vdesc, flags);
  668. }
  669. static struct dma_async_tx_descriptor *stm32_dma_prep_dma_memcpy(
  670. struct dma_chan *c, dma_addr_t dest,
  671. dma_addr_t src, size_t len, unsigned long flags)
  672. {
  673. struct stm32_dma_chan *chan = to_stm32_dma_chan(c);
  674. u32 num_sgs;
  675. struct stm32_dma_desc *desc;
  676. size_t xfer_count, offset;
  677. int i;
  678. num_sgs = DIV_ROUND_UP(len, STM32_DMA_MAX_DATA_ITEMS);
  679. desc = stm32_dma_alloc_desc(num_sgs);
  680. if (!desc)
  681. return NULL;
  682. for (offset = 0, i = 0; offset < len; offset += xfer_count, i++) {
  683. xfer_count = min_t(size_t, len - offset,
  684. STM32_DMA_MAX_DATA_ITEMS);
  685. desc->sg_req[i].len = xfer_count;
  686. stm32_dma_clear_reg(&desc->sg_req[i].chan_reg);
  687. desc->sg_req[i].chan_reg.dma_scr =
  688. STM32_DMA_SCR_DIR(STM32_DMA_MEM_TO_MEM) |
  689. STM32_DMA_SCR_MINC |
  690. STM32_DMA_SCR_PINC |
  691. STM32_DMA_SCR_TCIE |
  692. STM32_DMA_SCR_TEIE;
  693. desc->sg_req[i].chan_reg.dma_sfcr = STM32_DMA_SFCR_DMDIS |
  694. STM32_DMA_SFCR_FTH(STM32_DMA_FIFO_THRESHOLD_FULL) |
  695. STM32_DMA_SFCR_FEIE;
  696. desc->sg_req[i].chan_reg.dma_spar = src + offset;
  697. desc->sg_req[i].chan_reg.dma_sm0ar = dest + offset;
  698. desc->sg_req[i].chan_reg.dma_sndtr = xfer_count;
  699. }
  700. desc->num_sgs = num_sgs;
  701. desc->cyclic = false;
  702. return vchan_tx_prep(&chan->vchan, &desc->vdesc, flags);
  703. }
  704. static size_t stm32_dma_desc_residue(struct stm32_dma_chan *chan,
  705. struct stm32_dma_desc *desc,
  706. u32 next_sg)
  707. {
  708. struct stm32_dma_device *dmadev = stm32_dma_get_dev(chan);
  709. u32 dma_scr, width, residue, count;
  710. int i;
  711. residue = 0;
  712. for (i = next_sg; i < desc->num_sgs; i++)
  713. residue += desc->sg_req[i].len;
  714. if (next_sg != 0) {
  715. dma_scr = stm32_dma_read(dmadev, STM32_DMA_SCR(chan->id));
  716. width = STM32_DMA_SCR_PSIZE_GET(dma_scr);
  717. count = stm32_dma_read(dmadev, STM32_DMA_SNDTR(chan->id));
  718. residue += count << width;
  719. }
  720. return residue;
  721. }
  722. static enum dma_status stm32_dma_tx_status(struct dma_chan *c,
  723. dma_cookie_t cookie,
  724. struct dma_tx_state *state)
  725. {
  726. struct stm32_dma_chan *chan = to_stm32_dma_chan(c);
  727. struct virt_dma_desc *vdesc;
  728. enum dma_status status;
  729. unsigned long flags;
  730. u32 residue;
  731. status = dma_cookie_status(c, cookie, state);
  732. if ((status == DMA_COMPLETE) || (!state))
  733. return status;
  734. spin_lock_irqsave(&chan->vchan.lock, flags);
  735. vdesc = vchan_find_desc(&chan->vchan, cookie);
  736. if (cookie == chan->desc->vdesc.tx.cookie) {
  737. residue = stm32_dma_desc_residue(chan, chan->desc,
  738. chan->next_sg);
  739. } else if (vdesc) {
  740. residue = stm32_dma_desc_residue(chan,
  741. to_stm32_dma_desc(vdesc), 0);
  742. } else {
  743. residue = 0;
  744. }
  745. dma_set_residue(state, residue);
  746. spin_unlock_irqrestore(&chan->vchan.lock, flags);
  747. return status;
  748. }
  749. static int stm32_dma_alloc_chan_resources(struct dma_chan *c)
  750. {
  751. struct stm32_dma_chan *chan = to_stm32_dma_chan(c);
  752. struct stm32_dma_device *dmadev = stm32_dma_get_dev(chan);
  753. int ret;
  754. chan->config_init = false;
  755. ret = clk_prepare_enable(dmadev->clk);
  756. if (ret < 0) {
  757. dev_err(chan2dev(chan), "clk_prepare_enable failed: %d\n", ret);
  758. return ret;
  759. }
  760. ret = stm32_dma_disable_chan(chan);
  761. if (ret < 0)
  762. clk_disable_unprepare(dmadev->clk);
  763. return ret;
  764. }
  765. static void stm32_dma_free_chan_resources(struct dma_chan *c)
  766. {
  767. struct stm32_dma_chan *chan = to_stm32_dma_chan(c);
  768. struct stm32_dma_device *dmadev = stm32_dma_get_dev(chan);
  769. unsigned long flags;
  770. dev_dbg(chan2dev(chan), "Freeing channel %d\n", chan->id);
  771. if (chan->busy) {
  772. spin_lock_irqsave(&chan->vchan.lock, flags);
  773. stm32_dma_stop(chan);
  774. chan->desc = NULL;
  775. spin_unlock_irqrestore(&chan->vchan.lock, flags);
  776. }
  777. clk_disable_unprepare(dmadev->clk);
  778. vchan_free_chan_resources(to_virt_chan(c));
  779. }
  780. static void stm32_dma_desc_free(struct virt_dma_desc *vdesc)
  781. {
  782. kfree(container_of(vdesc, struct stm32_dma_desc, vdesc));
  783. }
  784. static void stm32_dma_set_config(struct stm32_dma_chan *chan,
  785. struct stm32_dma_cfg *cfg)
  786. {
  787. stm32_dma_clear_reg(&chan->chan_reg);
  788. chan->chan_reg.dma_scr = cfg->stream_config & STM32_DMA_SCR_CFG_MASK;
  789. chan->chan_reg.dma_scr |= STM32_DMA_SCR_REQ(cfg->request_line);
  790. /* Enable Interrupts */
  791. chan->chan_reg.dma_scr |= STM32_DMA_SCR_TEIE | STM32_DMA_SCR_TCIE;
  792. chan->chan_reg.dma_sfcr = cfg->threshold & STM32_DMA_SFCR_FTH_MASK;
  793. }
  794. static struct dma_chan *stm32_dma_of_xlate(struct of_phandle_args *dma_spec,
  795. struct of_dma *ofdma)
  796. {
  797. struct stm32_dma_device *dmadev = ofdma->of_dma_data;
  798. struct stm32_dma_cfg cfg;
  799. struct stm32_dma_chan *chan;
  800. struct dma_chan *c;
  801. if (dma_spec->args_count < 3)
  802. return NULL;
  803. cfg.channel_id = dma_spec->args[0];
  804. cfg.request_line = dma_spec->args[1];
  805. cfg.stream_config = dma_spec->args[2];
  806. cfg.threshold = 0;
  807. if ((cfg.channel_id >= STM32_DMA_MAX_CHANNELS) || (cfg.request_line >=
  808. STM32_DMA_MAX_REQUEST_ID))
  809. return NULL;
  810. if (dma_spec->args_count > 3)
  811. cfg.threshold = dma_spec->args[3];
  812. chan = &dmadev->chan[cfg.channel_id];
  813. c = dma_get_slave_channel(&chan->vchan.chan);
  814. if (c)
  815. stm32_dma_set_config(chan, &cfg);
  816. return c;
  817. }
  818. static const struct of_device_id stm32_dma_of_match[] = {
  819. { .compatible = "st,stm32-dma", },
  820. { /* sentinel */ },
  821. };
  822. MODULE_DEVICE_TABLE(of, stm32_dma_of_match);
  823. static int stm32_dma_probe(struct platform_device *pdev)
  824. {
  825. struct stm32_dma_chan *chan;
  826. struct stm32_dma_device *dmadev;
  827. struct dma_device *dd;
  828. const struct of_device_id *match;
  829. struct resource *res;
  830. int i, ret;
  831. match = of_match_device(stm32_dma_of_match, &pdev->dev);
  832. if (!match) {
  833. dev_err(&pdev->dev, "Error: No device match found\n");
  834. return -ENODEV;
  835. }
  836. dmadev = devm_kzalloc(&pdev->dev, sizeof(*dmadev), GFP_KERNEL);
  837. if (!dmadev)
  838. return -ENOMEM;
  839. dd = &dmadev->ddev;
  840. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  841. dmadev->base = devm_ioremap_resource(&pdev->dev, res);
  842. if (IS_ERR(dmadev->base))
  843. return PTR_ERR(dmadev->base);
  844. dmadev->clk = devm_clk_get(&pdev->dev, NULL);
  845. if (IS_ERR(dmadev->clk)) {
  846. dev_err(&pdev->dev, "Error: Missing controller clock\n");
  847. return PTR_ERR(dmadev->clk);
  848. }
  849. dmadev->mem2mem = of_property_read_bool(pdev->dev.of_node,
  850. "st,mem2mem");
  851. dmadev->rst = devm_reset_control_get(&pdev->dev, NULL);
  852. if (!IS_ERR(dmadev->rst)) {
  853. reset_control_assert(dmadev->rst);
  854. udelay(2);
  855. reset_control_deassert(dmadev->rst);
  856. }
  857. dma_cap_set(DMA_SLAVE, dd->cap_mask);
  858. dma_cap_set(DMA_PRIVATE, dd->cap_mask);
  859. dma_cap_set(DMA_CYCLIC, dd->cap_mask);
  860. dd->device_alloc_chan_resources = stm32_dma_alloc_chan_resources;
  861. dd->device_free_chan_resources = stm32_dma_free_chan_resources;
  862. dd->device_tx_status = stm32_dma_tx_status;
  863. dd->device_issue_pending = stm32_dma_issue_pending;
  864. dd->device_prep_slave_sg = stm32_dma_prep_slave_sg;
  865. dd->device_prep_dma_cyclic = stm32_dma_prep_dma_cyclic;
  866. dd->device_config = stm32_dma_slave_config;
  867. dd->device_terminate_all = stm32_dma_terminate_all;
  868. dd->src_addr_widths = BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) |
  869. BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) |
  870. BIT(DMA_SLAVE_BUSWIDTH_4_BYTES);
  871. dd->dst_addr_widths = BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) |
  872. BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) |
  873. BIT(DMA_SLAVE_BUSWIDTH_4_BYTES);
  874. dd->directions = BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV);
  875. dd->residue_granularity = DMA_RESIDUE_GRANULARITY_BURST;
  876. dd->dev = &pdev->dev;
  877. INIT_LIST_HEAD(&dd->channels);
  878. if (dmadev->mem2mem) {
  879. dma_cap_set(DMA_MEMCPY, dd->cap_mask);
  880. dd->device_prep_dma_memcpy = stm32_dma_prep_dma_memcpy;
  881. dd->directions |= BIT(DMA_MEM_TO_MEM);
  882. }
  883. for (i = 0; i < STM32_DMA_MAX_CHANNELS; i++) {
  884. chan = &dmadev->chan[i];
  885. chan->id = i;
  886. chan->vchan.desc_free = stm32_dma_desc_free;
  887. vchan_init(&chan->vchan, dd);
  888. }
  889. ret = dma_async_device_register(dd);
  890. if (ret)
  891. return ret;
  892. for (i = 0; i < STM32_DMA_MAX_CHANNELS; i++) {
  893. chan = &dmadev->chan[i];
  894. res = platform_get_resource(pdev, IORESOURCE_IRQ, i);
  895. if (!res) {
  896. ret = -EINVAL;
  897. dev_err(&pdev->dev, "No irq resource for chan %d\n", i);
  898. goto err_unregister;
  899. }
  900. chan->irq = res->start;
  901. ret = devm_request_irq(&pdev->dev, chan->irq,
  902. stm32_dma_chan_irq, 0,
  903. dev_name(chan2dev(chan)), chan);
  904. if (ret) {
  905. dev_err(&pdev->dev,
  906. "request_irq failed with err %d channel %d\n",
  907. ret, i);
  908. goto err_unregister;
  909. }
  910. }
  911. ret = of_dma_controller_register(pdev->dev.of_node,
  912. stm32_dma_of_xlate, dmadev);
  913. if (ret < 0) {
  914. dev_err(&pdev->dev,
  915. "STM32 DMA DMA OF registration failed %d\n", ret);
  916. goto err_unregister;
  917. }
  918. platform_set_drvdata(pdev, dmadev);
  919. dev_info(&pdev->dev, "STM32 DMA driver registered\n");
  920. return 0;
  921. err_unregister:
  922. dma_async_device_unregister(dd);
  923. return ret;
  924. }
  925. static struct platform_driver stm32_dma_driver = {
  926. .driver = {
  927. .name = "stm32-dma",
  928. .of_match_table = stm32_dma_of_match,
  929. },
  930. };
  931. static int __init stm32_dma_init(void)
  932. {
  933. return platform_driver_probe(&stm32_dma_driver, stm32_dma_probe);
  934. }
  935. subsys_initcall(stm32_dma_init);