stm32-dma.c 31 KB

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