stm32-dma.c 31 KB

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