stm32-dma.c 32 KB

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