stm32-dma.c 32 KB

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