stm32-dma.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135
  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, sfcr;
  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. sfcr = stm32_dma_read(dmadev, STM32_DMA_SFCR(chan->id));
  452. if ((status & STM32_DMA_TCI) && (scr & STM32_DMA_SCR_TCIE)) {
  453. stm32_dma_irq_clear(chan, STM32_DMA_TCI);
  454. stm32_dma_handle_chan_done(chan);
  455. } else {
  456. stm32_dma_irq_clear(chan, status);
  457. dev_err(chan2dev(chan), "DMA error: status=0x%08x\n", status);
  458. }
  459. spin_unlock(&chan->vchan.lock);
  460. return IRQ_HANDLED;
  461. }
  462. static void stm32_dma_issue_pending(struct dma_chan *c)
  463. {
  464. struct stm32_dma_chan *chan = to_stm32_dma_chan(c);
  465. unsigned long flags;
  466. int ret;
  467. spin_lock_irqsave(&chan->vchan.lock, flags);
  468. if (!chan->busy) {
  469. if (vchan_issue_pending(&chan->vchan) && !chan->desc) {
  470. ret = stm32_dma_start_transfer(chan);
  471. if ((!ret) && (chan->desc->cyclic))
  472. stm32_dma_configure_next_sg(chan);
  473. }
  474. }
  475. spin_unlock_irqrestore(&chan->vchan.lock, flags);
  476. }
  477. static int stm32_dma_set_xfer_param(struct stm32_dma_chan *chan,
  478. enum dma_transfer_direction direction,
  479. enum dma_slave_buswidth *buswidth)
  480. {
  481. enum dma_slave_buswidth src_addr_width, dst_addr_width;
  482. int src_bus_width, dst_bus_width;
  483. int src_burst_size, dst_burst_size;
  484. u32 src_maxburst, dst_maxburst;
  485. dma_addr_t src_addr, dst_addr;
  486. u32 dma_scr = 0;
  487. src_addr_width = chan->dma_sconfig.src_addr_width;
  488. dst_addr_width = chan->dma_sconfig.dst_addr_width;
  489. src_maxburst = chan->dma_sconfig.src_maxburst;
  490. dst_maxburst = chan->dma_sconfig.dst_maxburst;
  491. src_addr = chan->dma_sconfig.src_addr;
  492. dst_addr = chan->dma_sconfig.dst_addr;
  493. switch (direction) {
  494. case DMA_MEM_TO_DEV:
  495. dst_bus_width = stm32_dma_get_width(chan, dst_addr_width);
  496. if (dst_bus_width < 0)
  497. return dst_bus_width;
  498. dst_burst_size = stm32_dma_get_burst(chan, dst_maxburst);
  499. if (dst_burst_size < 0)
  500. return dst_burst_size;
  501. if (!src_addr_width)
  502. src_addr_width = dst_addr_width;
  503. src_bus_width = stm32_dma_get_width(chan, src_addr_width);
  504. if (src_bus_width < 0)
  505. return src_bus_width;
  506. src_burst_size = stm32_dma_get_burst(chan, src_maxburst);
  507. if (src_burst_size < 0)
  508. return src_burst_size;
  509. dma_scr = STM32_DMA_SCR_DIR(STM32_DMA_MEM_TO_DEV) |
  510. STM32_DMA_SCR_PSIZE(dst_bus_width) |
  511. STM32_DMA_SCR_MSIZE(src_bus_width) |
  512. STM32_DMA_SCR_PBURST(dst_burst_size) |
  513. STM32_DMA_SCR_MBURST(src_burst_size);
  514. chan->chan_reg.dma_spar = chan->dma_sconfig.dst_addr;
  515. *buswidth = dst_addr_width;
  516. break;
  517. case DMA_DEV_TO_MEM:
  518. src_bus_width = stm32_dma_get_width(chan, src_addr_width);
  519. if (src_bus_width < 0)
  520. return src_bus_width;
  521. src_burst_size = stm32_dma_get_burst(chan, src_maxburst);
  522. if (src_burst_size < 0)
  523. return src_burst_size;
  524. if (!dst_addr_width)
  525. dst_addr_width = src_addr_width;
  526. dst_bus_width = stm32_dma_get_width(chan, dst_addr_width);
  527. if (dst_bus_width < 0)
  528. return dst_bus_width;
  529. dst_burst_size = stm32_dma_get_burst(chan, dst_maxburst);
  530. if (dst_burst_size < 0)
  531. return dst_burst_size;
  532. dma_scr = STM32_DMA_SCR_DIR(STM32_DMA_DEV_TO_MEM) |
  533. STM32_DMA_SCR_PSIZE(src_bus_width) |
  534. STM32_DMA_SCR_MSIZE(dst_bus_width) |
  535. STM32_DMA_SCR_PBURST(src_burst_size) |
  536. STM32_DMA_SCR_MBURST(dst_burst_size);
  537. chan->chan_reg.dma_spar = chan->dma_sconfig.src_addr;
  538. *buswidth = chan->dma_sconfig.src_addr_width;
  539. break;
  540. default:
  541. dev_err(chan2dev(chan), "Dma direction is not supported\n");
  542. return -EINVAL;
  543. }
  544. stm32_dma_set_fifo_config(chan, src_maxburst, dst_maxburst);
  545. chan->chan_reg.dma_scr &= ~(STM32_DMA_SCR_DIR_MASK |
  546. STM32_DMA_SCR_PSIZE_MASK | STM32_DMA_SCR_MSIZE_MASK |
  547. STM32_DMA_SCR_PBURST_MASK | STM32_DMA_SCR_MBURST_MASK);
  548. chan->chan_reg.dma_scr |= dma_scr;
  549. return 0;
  550. }
  551. static void stm32_dma_clear_reg(struct stm32_dma_chan_reg *regs)
  552. {
  553. memset(regs, 0, sizeof(struct stm32_dma_chan_reg));
  554. }
  555. static struct dma_async_tx_descriptor *stm32_dma_prep_slave_sg(
  556. struct dma_chan *c, struct scatterlist *sgl,
  557. u32 sg_len, enum dma_transfer_direction direction,
  558. unsigned long flags, void *context)
  559. {
  560. struct stm32_dma_chan *chan = to_stm32_dma_chan(c);
  561. struct stm32_dma_desc *desc;
  562. struct scatterlist *sg;
  563. enum dma_slave_buswidth buswidth;
  564. u32 nb_data_items;
  565. int i, ret;
  566. if (!chan->config_init) {
  567. dev_err(chan2dev(chan), "dma channel is not configured\n");
  568. return NULL;
  569. }
  570. if (sg_len < 1) {
  571. dev_err(chan2dev(chan), "Invalid segment length %d\n", sg_len);
  572. return NULL;
  573. }
  574. desc = stm32_dma_alloc_desc(sg_len);
  575. if (!desc)
  576. return NULL;
  577. ret = stm32_dma_set_xfer_param(chan, direction, &buswidth);
  578. if (ret < 0)
  579. goto err;
  580. /* Set peripheral flow controller */
  581. if (chan->dma_sconfig.device_fc)
  582. chan->chan_reg.dma_scr |= STM32_DMA_SCR_PFCTRL;
  583. else
  584. chan->chan_reg.dma_scr &= ~STM32_DMA_SCR_PFCTRL;
  585. for_each_sg(sgl, sg, sg_len, i) {
  586. desc->sg_req[i].len = sg_dma_len(sg);
  587. nb_data_items = desc->sg_req[i].len / buswidth;
  588. if (nb_data_items > STM32_DMA_MAX_DATA_ITEMS) {
  589. dev_err(chan2dev(chan), "nb items not supported\n");
  590. goto err;
  591. }
  592. stm32_dma_clear_reg(&desc->sg_req[i].chan_reg);
  593. desc->sg_req[i].chan_reg.dma_scr = chan->chan_reg.dma_scr;
  594. desc->sg_req[i].chan_reg.dma_sfcr = chan->chan_reg.dma_sfcr;
  595. desc->sg_req[i].chan_reg.dma_spar = chan->chan_reg.dma_spar;
  596. desc->sg_req[i].chan_reg.dma_sm0ar = sg_dma_address(sg);
  597. desc->sg_req[i].chan_reg.dma_sm1ar = sg_dma_address(sg);
  598. desc->sg_req[i].chan_reg.dma_sndtr = nb_data_items;
  599. }
  600. desc->num_sgs = sg_len;
  601. desc->cyclic = false;
  602. return vchan_tx_prep(&chan->vchan, &desc->vdesc, flags);
  603. err:
  604. kfree(desc);
  605. return NULL;
  606. }
  607. static struct dma_async_tx_descriptor *stm32_dma_prep_dma_cyclic(
  608. struct dma_chan *c, dma_addr_t buf_addr, size_t buf_len,
  609. size_t period_len, enum dma_transfer_direction direction,
  610. unsigned long flags)
  611. {
  612. struct stm32_dma_chan *chan = to_stm32_dma_chan(c);
  613. struct stm32_dma_desc *desc;
  614. enum dma_slave_buswidth buswidth;
  615. u32 num_periods, nb_data_items;
  616. int i, ret;
  617. if (!buf_len || !period_len) {
  618. dev_err(chan2dev(chan), "Invalid buffer/period len\n");
  619. return NULL;
  620. }
  621. if (!chan->config_init) {
  622. dev_err(chan2dev(chan), "dma channel is not configured\n");
  623. return NULL;
  624. }
  625. if (buf_len % period_len) {
  626. dev_err(chan2dev(chan), "buf_len not multiple of period_len\n");
  627. return NULL;
  628. }
  629. /*
  630. * We allow to take more number of requests till DMA is
  631. * not started. The driver will loop over all requests.
  632. * Once DMA is started then new requests can be queued only after
  633. * terminating the DMA.
  634. */
  635. if (chan->busy) {
  636. dev_err(chan2dev(chan), "Request not allowed when dma busy\n");
  637. return NULL;
  638. }
  639. ret = stm32_dma_set_xfer_param(chan, direction, &buswidth);
  640. if (ret < 0)
  641. return NULL;
  642. nb_data_items = period_len / buswidth;
  643. if (nb_data_items > STM32_DMA_MAX_DATA_ITEMS) {
  644. dev_err(chan2dev(chan), "number of items not supported\n");
  645. return NULL;
  646. }
  647. /* Enable Circular mode or double buffer mode */
  648. if (buf_len == period_len)
  649. chan->chan_reg.dma_scr |= STM32_DMA_SCR_CIRC;
  650. else
  651. chan->chan_reg.dma_scr |= STM32_DMA_SCR_DBM;
  652. /* Clear periph ctrl if client set it */
  653. chan->chan_reg.dma_scr &= ~STM32_DMA_SCR_PFCTRL;
  654. num_periods = buf_len / period_len;
  655. desc = stm32_dma_alloc_desc(num_periods);
  656. if (!desc)
  657. return NULL;
  658. for (i = 0; i < num_periods; i++) {
  659. desc->sg_req[i].len = period_len;
  660. stm32_dma_clear_reg(&desc->sg_req[i].chan_reg);
  661. desc->sg_req[i].chan_reg.dma_scr = chan->chan_reg.dma_scr;
  662. desc->sg_req[i].chan_reg.dma_sfcr = chan->chan_reg.dma_sfcr;
  663. desc->sg_req[i].chan_reg.dma_spar = chan->chan_reg.dma_spar;
  664. desc->sg_req[i].chan_reg.dma_sm0ar = buf_addr;
  665. desc->sg_req[i].chan_reg.dma_sm1ar = buf_addr;
  666. desc->sg_req[i].chan_reg.dma_sndtr = nb_data_items;
  667. buf_addr += period_len;
  668. }
  669. desc->num_sgs = num_periods;
  670. desc->cyclic = true;
  671. return vchan_tx_prep(&chan->vchan, &desc->vdesc, flags);
  672. }
  673. static struct dma_async_tx_descriptor *stm32_dma_prep_dma_memcpy(
  674. struct dma_chan *c, dma_addr_t dest,
  675. dma_addr_t src, size_t len, unsigned long flags)
  676. {
  677. struct stm32_dma_chan *chan = to_stm32_dma_chan(c);
  678. u32 num_sgs;
  679. struct stm32_dma_desc *desc;
  680. size_t xfer_count, offset;
  681. int i;
  682. num_sgs = DIV_ROUND_UP(len, STM32_DMA_MAX_DATA_ITEMS);
  683. desc = stm32_dma_alloc_desc(num_sgs);
  684. if (!desc)
  685. return NULL;
  686. for (offset = 0, i = 0; offset < len; offset += xfer_count, i++) {
  687. xfer_count = min_t(size_t, len - offset,
  688. STM32_DMA_MAX_DATA_ITEMS);
  689. desc->sg_req[i].len = xfer_count;
  690. stm32_dma_clear_reg(&desc->sg_req[i].chan_reg);
  691. desc->sg_req[i].chan_reg.dma_scr =
  692. STM32_DMA_SCR_DIR(STM32_DMA_MEM_TO_MEM) |
  693. STM32_DMA_SCR_MINC |
  694. STM32_DMA_SCR_PINC |
  695. STM32_DMA_SCR_TCIE |
  696. STM32_DMA_SCR_TEIE;
  697. desc->sg_req[i].chan_reg.dma_sfcr = STM32_DMA_SFCR_DMDIS |
  698. STM32_DMA_SFCR_FTH(STM32_DMA_FIFO_THRESHOLD_FULL) |
  699. STM32_DMA_SFCR_FEIE;
  700. desc->sg_req[i].chan_reg.dma_spar = src + offset;
  701. desc->sg_req[i].chan_reg.dma_sm0ar = dest + offset;
  702. desc->sg_req[i].chan_reg.dma_sndtr = xfer_count;
  703. }
  704. desc->num_sgs = num_sgs;
  705. desc->cyclic = false;
  706. return vchan_tx_prep(&chan->vchan, &desc->vdesc, flags);
  707. }
  708. static size_t stm32_dma_desc_residue(struct stm32_dma_chan *chan,
  709. struct stm32_dma_desc *desc,
  710. u32 next_sg)
  711. {
  712. struct stm32_dma_device *dmadev = stm32_dma_get_dev(chan);
  713. u32 dma_scr, width, residue, count;
  714. int i;
  715. residue = 0;
  716. for (i = next_sg; i < desc->num_sgs; i++)
  717. residue += desc->sg_req[i].len;
  718. if (next_sg != 0) {
  719. dma_scr = stm32_dma_read(dmadev, STM32_DMA_SCR(chan->id));
  720. width = STM32_DMA_SCR_PSIZE_GET(dma_scr);
  721. count = stm32_dma_read(dmadev, STM32_DMA_SNDTR(chan->id));
  722. residue += count << width;
  723. }
  724. return residue;
  725. }
  726. static enum dma_status stm32_dma_tx_status(struct dma_chan *c,
  727. dma_cookie_t cookie,
  728. struct dma_tx_state *state)
  729. {
  730. struct stm32_dma_chan *chan = to_stm32_dma_chan(c);
  731. struct virt_dma_desc *vdesc;
  732. enum dma_status status;
  733. unsigned long flags;
  734. u32 residue = 0;
  735. status = dma_cookie_status(c, cookie, state);
  736. if ((status == DMA_COMPLETE) || (!state))
  737. return status;
  738. spin_lock_irqsave(&chan->vchan.lock, flags);
  739. vdesc = vchan_find_desc(&chan->vchan, cookie);
  740. if (chan->desc && cookie == chan->desc->vdesc.tx.cookie)
  741. residue = stm32_dma_desc_residue(chan, chan->desc,
  742. chan->next_sg);
  743. else if (vdesc)
  744. residue = stm32_dma_desc_residue(chan,
  745. to_stm32_dma_desc(vdesc), 0);
  746. dma_set_residue(state, residue);
  747. spin_unlock_irqrestore(&chan->vchan.lock, flags);
  748. return status;
  749. }
  750. static int stm32_dma_alloc_chan_resources(struct dma_chan *c)
  751. {
  752. struct stm32_dma_chan *chan = to_stm32_dma_chan(c);
  753. struct stm32_dma_device *dmadev = stm32_dma_get_dev(chan);
  754. int ret;
  755. chan->config_init = false;
  756. ret = clk_prepare_enable(dmadev->clk);
  757. if (ret < 0) {
  758. dev_err(chan2dev(chan), "clk_prepare_enable failed: %d\n", ret);
  759. return ret;
  760. }
  761. ret = stm32_dma_disable_chan(chan);
  762. if (ret < 0)
  763. clk_disable_unprepare(dmadev->clk);
  764. return ret;
  765. }
  766. static void stm32_dma_free_chan_resources(struct dma_chan *c)
  767. {
  768. struct stm32_dma_chan *chan = to_stm32_dma_chan(c);
  769. struct stm32_dma_device *dmadev = stm32_dma_get_dev(chan);
  770. unsigned long flags;
  771. dev_dbg(chan2dev(chan), "Freeing channel %d\n", chan->id);
  772. if (chan->busy) {
  773. spin_lock_irqsave(&chan->vchan.lock, flags);
  774. stm32_dma_stop(chan);
  775. chan->desc = NULL;
  776. spin_unlock_irqrestore(&chan->vchan.lock, flags);
  777. }
  778. clk_disable_unprepare(dmadev->clk);
  779. vchan_free_chan_resources(to_virt_chan(c));
  780. }
  781. static void stm32_dma_desc_free(struct virt_dma_desc *vdesc)
  782. {
  783. kfree(container_of(vdesc, struct stm32_dma_desc, vdesc));
  784. }
  785. static void stm32_dma_set_config(struct stm32_dma_chan *chan,
  786. struct stm32_dma_cfg *cfg)
  787. {
  788. stm32_dma_clear_reg(&chan->chan_reg);
  789. chan->chan_reg.dma_scr = cfg->stream_config & STM32_DMA_SCR_CFG_MASK;
  790. chan->chan_reg.dma_scr |= STM32_DMA_SCR_REQ(cfg->request_line);
  791. /* Enable Interrupts */
  792. chan->chan_reg.dma_scr |= STM32_DMA_SCR_TEIE | STM32_DMA_SCR_TCIE;
  793. chan->chan_reg.dma_sfcr = cfg->threshold & STM32_DMA_SFCR_FTH_MASK;
  794. }
  795. static struct dma_chan *stm32_dma_of_xlate(struct of_phandle_args *dma_spec,
  796. struct of_dma *ofdma)
  797. {
  798. struct stm32_dma_device *dmadev = ofdma->of_dma_data;
  799. struct stm32_dma_cfg cfg;
  800. struct stm32_dma_chan *chan;
  801. struct dma_chan *c;
  802. if (dma_spec->args_count < 4)
  803. return NULL;
  804. cfg.channel_id = dma_spec->args[0];
  805. cfg.request_line = dma_spec->args[1];
  806. cfg.stream_config = dma_spec->args[2];
  807. cfg.threshold = dma_spec->args[3];
  808. if ((cfg.channel_id >= STM32_DMA_MAX_CHANNELS) || (cfg.request_line >=
  809. STM32_DMA_MAX_REQUEST_ID))
  810. return NULL;
  811. chan = &dmadev->chan[cfg.channel_id];
  812. c = dma_get_slave_channel(&chan->vchan.chan);
  813. if (c)
  814. stm32_dma_set_config(chan, &cfg);
  815. return c;
  816. }
  817. static const struct of_device_id stm32_dma_of_match[] = {
  818. { .compatible = "st,stm32-dma", },
  819. { /* sentinel */ },
  820. };
  821. MODULE_DEVICE_TABLE(of, stm32_dma_of_match);
  822. static int stm32_dma_probe(struct platform_device *pdev)
  823. {
  824. struct stm32_dma_chan *chan;
  825. struct stm32_dma_device *dmadev;
  826. struct dma_device *dd;
  827. const struct of_device_id *match;
  828. struct resource *res;
  829. int i, ret;
  830. match = of_match_device(stm32_dma_of_match, &pdev->dev);
  831. if (!match) {
  832. dev_err(&pdev->dev, "Error: No device match found\n");
  833. return -ENODEV;
  834. }
  835. dmadev = devm_kzalloc(&pdev->dev, sizeof(*dmadev), GFP_KERNEL);
  836. if (!dmadev)
  837. return -ENOMEM;
  838. dd = &dmadev->ddev;
  839. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  840. dmadev->base = devm_ioremap_resource(&pdev->dev, res);
  841. if (IS_ERR(dmadev->base))
  842. return PTR_ERR(dmadev->base);
  843. dmadev->clk = devm_clk_get(&pdev->dev, NULL);
  844. if (IS_ERR(dmadev->clk)) {
  845. dev_err(&pdev->dev, "Error: Missing controller clock\n");
  846. return PTR_ERR(dmadev->clk);
  847. }
  848. dmadev->mem2mem = of_property_read_bool(pdev->dev.of_node,
  849. "st,mem2mem");
  850. dmadev->rst = devm_reset_control_get(&pdev->dev, NULL);
  851. if (!IS_ERR(dmadev->rst)) {
  852. reset_control_assert(dmadev->rst);
  853. udelay(2);
  854. reset_control_deassert(dmadev->rst);
  855. }
  856. dma_cap_set(DMA_SLAVE, dd->cap_mask);
  857. dma_cap_set(DMA_PRIVATE, dd->cap_mask);
  858. dma_cap_set(DMA_CYCLIC, dd->cap_mask);
  859. dd->device_alloc_chan_resources = stm32_dma_alloc_chan_resources;
  860. dd->device_free_chan_resources = stm32_dma_free_chan_resources;
  861. dd->device_tx_status = stm32_dma_tx_status;
  862. dd->device_issue_pending = stm32_dma_issue_pending;
  863. dd->device_prep_slave_sg = stm32_dma_prep_slave_sg;
  864. dd->device_prep_dma_cyclic = stm32_dma_prep_dma_cyclic;
  865. dd->device_config = stm32_dma_slave_config;
  866. dd->device_terminate_all = stm32_dma_terminate_all;
  867. dd->src_addr_widths = BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) |
  868. BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) |
  869. BIT(DMA_SLAVE_BUSWIDTH_4_BYTES);
  870. dd->dst_addr_widths = BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) |
  871. BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) |
  872. BIT(DMA_SLAVE_BUSWIDTH_4_BYTES);
  873. dd->directions = BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV);
  874. dd->residue_granularity = DMA_RESIDUE_GRANULARITY_BURST;
  875. dd->dev = &pdev->dev;
  876. INIT_LIST_HEAD(&dd->channels);
  877. if (dmadev->mem2mem) {
  878. dma_cap_set(DMA_MEMCPY, dd->cap_mask);
  879. dd->device_prep_dma_memcpy = stm32_dma_prep_dma_memcpy;
  880. dd->directions |= BIT(DMA_MEM_TO_MEM);
  881. }
  882. for (i = 0; i < STM32_DMA_MAX_CHANNELS; i++) {
  883. chan = &dmadev->chan[i];
  884. chan->id = i;
  885. chan->vchan.desc_free = stm32_dma_desc_free;
  886. vchan_init(&chan->vchan, dd);
  887. }
  888. ret = dma_async_device_register(dd);
  889. if (ret)
  890. return ret;
  891. for (i = 0; i < STM32_DMA_MAX_CHANNELS; i++) {
  892. chan = &dmadev->chan[i];
  893. res = platform_get_resource(pdev, IORESOURCE_IRQ, i);
  894. if (!res) {
  895. ret = -EINVAL;
  896. dev_err(&pdev->dev, "No irq resource for chan %d\n", i);
  897. goto err_unregister;
  898. }
  899. chan->irq = res->start;
  900. ret = devm_request_irq(&pdev->dev, chan->irq,
  901. stm32_dma_chan_irq, 0,
  902. dev_name(chan2dev(chan)), chan);
  903. if (ret) {
  904. dev_err(&pdev->dev,
  905. "request_irq failed with err %d channel %d\n",
  906. ret, i);
  907. goto err_unregister;
  908. }
  909. }
  910. ret = of_dma_controller_register(pdev->dev.of_node,
  911. stm32_dma_of_xlate, dmadev);
  912. if (ret < 0) {
  913. dev_err(&pdev->dev,
  914. "STM32 DMA DMA OF registration failed %d\n", ret);
  915. goto err_unregister;
  916. }
  917. platform_set_drvdata(pdev, dmadev);
  918. dev_info(&pdev->dev, "STM32 DMA driver registered\n");
  919. return 0;
  920. err_unregister:
  921. dma_async_device_unregister(dd);
  922. return ret;
  923. }
  924. static struct platform_driver stm32_dma_driver = {
  925. .driver = {
  926. .name = "stm32-dma",
  927. .of_match_table = stm32_dma_of_match,
  928. },
  929. };
  930. static int __init stm32_dma_init(void)
  931. {
  932. return platform_driver_probe(&stm32_dma_driver, stm32_dma_probe);
  933. }
  934. subsys_initcall(stm32_dma_init);