stm32-dma.c 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Driver for STM32 DMA controller
  4. *
  5. * Inspired by dma-jz4740.c and tegra20-apb-dma.c
  6. *
  7. * Copyright (C) M'boumba Cedric Madianga 2015
  8. * Author: M'boumba Cedric Madianga <[email protected]>
  9. * Pierre-Yves Mordret <[email protected]>
  10. */
  11. #include <linux/bitfield.h>
  12. #include <linux/clk.h>
  13. #include <linux/delay.h>
  14. #include <linux/dmaengine.h>
  15. #include <linux/dma-mapping.h>
  16. #include <linux/err.h>
  17. #include <linux/init.h>
  18. #include <linux/iopoll.h>
  19. #include <linux/jiffies.h>
  20. #include <linux/list.h>
  21. #include <linux/module.h>
  22. #include <linux/of.h>
  23. #include <linux/of_device.h>
  24. #include <linux/of_dma.h>
  25. #include <linux/platform_device.h>
  26. #include <linux/pm_runtime.h>
  27. #include <linux/reset.h>
  28. #include <linux/sched.h>
  29. #include <linux/slab.h>
  30. #include "virt-dma.h"
  31. #define STM32_DMA_LISR 0x0000 /* DMA Low Int Status Reg */
  32. #define STM32_DMA_HISR 0x0004 /* DMA High Int Status Reg */
  33. #define STM32_DMA_ISR(n) (((n) & 4) ? STM32_DMA_HISR : STM32_DMA_LISR)
  34. #define STM32_DMA_LIFCR 0x0008 /* DMA Low Int Flag Clear Reg */
  35. #define STM32_DMA_HIFCR 0x000c /* DMA High Int Flag Clear Reg */
  36. #define STM32_DMA_IFCR(n) (((n) & 4) ? STM32_DMA_HIFCR : STM32_DMA_LIFCR)
  37. #define STM32_DMA_TCI BIT(5) /* Transfer Complete Interrupt */
  38. #define STM32_DMA_HTI BIT(4) /* Half Transfer Interrupt */
  39. #define STM32_DMA_TEI BIT(3) /* Transfer Error Interrupt */
  40. #define STM32_DMA_DMEI BIT(2) /* Direct Mode Error Interrupt */
  41. #define STM32_DMA_FEI BIT(0) /* FIFO Error Interrupt */
  42. #define STM32_DMA_MASKI (STM32_DMA_TCI \
  43. | STM32_DMA_TEI \
  44. | STM32_DMA_DMEI \
  45. | STM32_DMA_FEI)
  46. /*
  47. * If (chan->id % 4) is 2 or 3, left shift the mask by 16 bits;
  48. * if (ch % 4) is 1 or 3, additionally left shift the mask by 6 bits.
  49. */
  50. #define STM32_DMA_FLAGS_SHIFT(n) ({ typeof(n) (_n) = (n); \
  51. (((_n) & 2) << 3) | (((_n) & 1) * 6); })
  52. /* DMA Stream x Configuration Register */
  53. #define STM32_DMA_SCR(x) (0x0010 + 0x18 * (x)) /* x = 0..7 */
  54. #define STM32_DMA_SCR_REQ_MASK GENMASK(27, 25)
  55. #define STM32_DMA_SCR_MBURST_MASK GENMASK(24, 23)
  56. #define STM32_DMA_SCR_PBURST_MASK GENMASK(22, 21)
  57. #define STM32_DMA_SCR_PL_MASK GENMASK(17, 16)
  58. #define STM32_DMA_SCR_MSIZE_MASK GENMASK(14, 13)
  59. #define STM32_DMA_SCR_PSIZE_MASK GENMASK(12, 11)
  60. #define STM32_DMA_SCR_DIR_MASK GENMASK(7, 6)
  61. #define STM32_DMA_SCR_TRBUFF BIT(20) /* Bufferable transfer for USART/UART */
  62. #define STM32_DMA_SCR_CT BIT(19) /* Target in double buffer */
  63. #define STM32_DMA_SCR_DBM BIT(18) /* Double Buffer Mode */
  64. #define STM32_DMA_SCR_PINCOS BIT(15) /* Peripheral inc offset size */
  65. #define STM32_DMA_SCR_MINC BIT(10) /* Memory increment mode */
  66. #define STM32_DMA_SCR_PINC BIT(9) /* Peripheral increment mode */
  67. #define STM32_DMA_SCR_CIRC BIT(8) /* Circular mode */
  68. #define STM32_DMA_SCR_PFCTRL BIT(5) /* Peripheral Flow Controller */
  69. #define STM32_DMA_SCR_TCIE BIT(4) /* Transfer Complete Int Enable
  70. */
  71. #define STM32_DMA_SCR_TEIE BIT(2) /* Transfer Error Int Enable */
  72. #define STM32_DMA_SCR_DMEIE BIT(1) /* Direct Mode Err Int Enable */
  73. #define STM32_DMA_SCR_EN BIT(0) /* Stream Enable */
  74. #define STM32_DMA_SCR_CFG_MASK (STM32_DMA_SCR_PINC \
  75. | STM32_DMA_SCR_MINC \
  76. | STM32_DMA_SCR_PINCOS \
  77. | STM32_DMA_SCR_PL_MASK)
  78. #define STM32_DMA_SCR_IRQ_MASK (STM32_DMA_SCR_TCIE \
  79. | STM32_DMA_SCR_TEIE \
  80. | STM32_DMA_SCR_DMEIE)
  81. /* DMA Stream x number of data register */
  82. #define STM32_DMA_SNDTR(x) (0x0014 + 0x18 * (x))
  83. /* DMA stream peripheral address register */
  84. #define STM32_DMA_SPAR(x) (0x0018 + 0x18 * (x))
  85. /* DMA stream x memory 0 address register */
  86. #define STM32_DMA_SM0AR(x) (0x001c + 0x18 * (x))
  87. /* DMA stream x memory 1 address register */
  88. #define STM32_DMA_SM1AR(x) (0x0020 + 0x18 * (x))
  89. /* DMA stream x FIFO control register */
  90. #define STM32_DMA_SFCR(x) (0x0024 + 0x18 * (x))
  91. #define STM32_DMA_SFCR_FTH_MASK GENMASK(1, 0)
  92. #define STM32_DMA_SFCR_FEIE BIT(7) /* FIFO error interrupt enable */
  93. #define STM32_DMA_SFCR_DMDIS BIT(2) /* Direct mode disable */
  94. #define STM32_DMA_SFCR_MASK (STM32_DMA_SFCR_FEIE \
  95. | STM32_DMA_SFCR_DMDIS)
  96. /* DMA direction */
  97. #define STM32_DMA_DEV_TO_MEM 0x00
  98. #define STM32_DMA_MEM_TO_DEV 0x01
  99. #define STM32_DMA_MEM_TO_MEM 0x02
  100. /* DMA priority level */
  101. #define STM32_DMA_PRIORITY_LOW 0x00
  102. #define STM32_DMA_PRIORITY_MEDIUM 0x01
  103. #define STM32_DMA_PRIORITY_HIGH 0x02
  104. #define STM32_DMA_PRIORITY_VERY_HIGH 0x03
  105. /* DMA FIFO threshold selection */
  106. #define STM32_DMA_FIFO_THRESHOLD_1QUARTERFULL 0x00
  107. #define STM32_DMA_FIFO_THRESHOLD_HALFFULL 0x01
  108. #define STM32_DMA_FIFO_THRESHOLD_3QUARTERSFULL 0x02
  109. #define STM32_DMA_FIFO_THRESHOLD_FULL 0x03
  110. #define STM32_DMA_FIFO_THRESHOLD_NONE 0x04
  111. #define STM32_DMA_MAX_DATA_ITEMS 0xffff
  112. /*
  113. * Valid transfer starts from @0 to @0xFFFE leading to unaligned scatter
  114. * gather at boundary. Thus it's safer to round down this value on FIFO
  115. * size (16 Bytes)
  116. */
  117. #define STM32_DMA_ALIGNED_MAX_DATA_ITEMS \
  118. ALIGN_DOWN(STM32_DMA_MAX_DATA_ITEMS, 16)
  119. #define STM32_DMA_MAX_CHANNELS 0x08
  120. #define STM32_DMA_MAX_REQUEST_ID 0x08
  121. #define STM32_DMA_MAX_DATA_PARAM 0x03
  122. #define STM32_DMA_FIFO_SIZE 16 /* FIFO is 16 bytes */
  123. #define STM32_DMA_MIN_BURST 4
  124. #define STM32_DMA_MAX_BURST 16
  125. /* DMA Features */
  126. #define STM32_DMA_THRESHOLD_FTR_MASK GENMASK(1, 0)
  127. #define STM32_DMA_DIRECT_MODE_MASK BIT(2)
  128. #define STM32_DMA_ALT_ACK_MODE_MASK BIT(4)
  129. #define STM32_DMA_MDMA_STREAM_ID_MASK GENMASK(19, 16)
  130. enum stm32_dma_width {
  131. STM32_DMA_BYTE,
  132. STM32_DMA_HALF_WORD,
  133. STM32_DMA_WORD,
  134. };
  135. enum stm32_dma_burst_size {
  136. STM32_DMA_BURST_SINGLE,
  137. STM32_DMA_BURST_INCR4,
  138. STM32_DMA_BURST_INCR8,
  139. STM32_DMA_BURST_INCR16,
  140. };
  141. /**
  142. * struct stm32_dma_cfg - STM32 DMA custom configuration
  143. * @channel_id: channel ID
  144. * @request_line: DMA request
  145. * @stream_config: 32bit mask specifying the DMA channel configuration
  146. * @features: 32bit mask specifying the DMA Feature list
  147. */
  148. struct stm32_dma_cfg {
  149. u32 channel_id;
  150. u32 request_line;
  151. u32 stream_config;
  152. u32 features;
  153. };
  154. struct stm32_dma_chan_reg {
  155. u32 dma_lisr;
  156. u32 dma_hisr;
  157. u32 dma_lifcr;
  158. u32 dma_hifcr;
  159. u32 dma_scr;
  160. u32 dma_sndtr;
  161. u32 dma_spar;
  162. u32 dma_sm0ar;
  163. u32 dma_sm1ar;
  164. u32 dma_sfcr;
  165. };
  166. struct stm32_dma_sg_req {
  167. u32 len;
  168. struct stm32_dma_chan_reg chan_reg;
  169. };
  170. struct stm32_dma_desc {
  171. struct virt_dma_desc vdesc;
  172. bool cyclic;
  173. u32 num_sgs;
  174. struct stm32_dma_sg_req sg_req[];
  175. };
  176. /**
  177. * struct stm32_dma_mdma_config - STM32 DMA MDMA configuration
  178. * @stream_id: DMA request to trigger STM32 MDMA transfer
  179. * @ifcr: DMA interrupt flag clear register address,
  180. * used by STM32 MDMA to clear DMA Transfer Complete flag
  181. * @tcf: DMA Transfer Complete flag
  182. */
  183. struct stm32_dma_mdma_config {
  184. u32 stream_id;
  185. u32 ifcr;
  186. u32 tcf;
  187. };
  188. struct stm32_dma_chan {
  189. struct virt_dma_chan vchan;
  190. bool config_init;
  191. bool busy;
  192. u32 id;
  193. u32 irq;
  194. struct stm32_dma_desc *desc;
  195. u32 next_sg;
  196. struct dma_slave_config dma_sconfig;
  197. struct stm32_dma_chan_reg chan_reg;
  198. u32 threshold;
  199. u32 mem_burst;
  200. u32 mem_width;
  201. enum dma_status status;
  202. bool trig_mdma;
  203. struct stm32_dma_mdma_config mdma_config;
  204. };
  205. struct stm32_dma_device {
  206. struct dma_device ddev;
  207. void __iomem *base;
  208. struct clk *clk;
  209. bool mem2mem;
  210. struct stm32_dma_chan chan[STM32_DMA_MAX_CHANNELS];
  211. };
  212. static struct stm32_dma_device *stm32_dma_get_dev(struct stm32_dma_chan *chan)
  213. {
  214. return container_of(chan->vchan.chan.device, struct stm32_dma_device,
  215. ddev);
  216. }
  217. static struct stm32_dma_chan *to_stm32_dma_chan(struct dma_chan *c)
  218. {
  219. return container_of(c, struct stm32_dma_chan, vchan.chan);
  220. }
  221. static struct stm32_dma_desc *to_stm32_dma_desc(struct virt_dma_desc *vdesc)
  222. {
  223. return container_of(vdesc, struct stm32_dma_desc, vdesc);
  224. }
  225. static struct device *chan2dev(struct stm32_dma_chan *chan)
  226. {
  227. return &chan->vchan.chan.dev->device;
  228. }
  229. static u32 stm32_dma_read(struct stm32_dma_device *dmadev, u32 reg)
  230. {
  231. return readl_relaxed(dmadev->base + reg);
  232. }
  233. static void stm32_dma_write(struct stm32_dma_device *dmadev, u32 reg, u32 val)
  234. {
  235. writel_relaxed(val, dmadev->base + reg);
  236. }
  237. static int stm32_dma_get_width(struct stm32_dma_chan *chan,
  238. enum dma_slave_buswidth width)
  239. {
  240. switch (width) {
  241. case DMA_SLAVE_BUSWIDTH_1_BYTE:
  242. return STM32_DMA_BYTE;
  243. case DMA_SLAVE_BUSWIDTH_2_BYTES:
  244. return STM32_DMA_HALF_WORD;
  245. case DMA_SLAVE_BUSWIDTH_4_BYTES:
  246. return STM32_DMA_WORD;
  247. default:
  248. dev_err(chan2dev(chan), "Dma bus width not supported\n");
  249. return -EINVAL;
  250. }
  251. }
  252. static enum dma_slave_buswidth stm32_dma_get_max_width(u32 buf_len,
  253. dma_addr_t buf_addr,
  254. u32 threshold)
  255. {
  256. enum dma_slave_buswidth max_width;
  257. if (threshold == STM32_DMA_FIFO_THRESHOLD_FULL)
  258. max_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  259. else
  260. max_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
  261. while ((buf_len < max_width || buf_len % max_width) &&
  262. max_width > DMA_SLAVE_BUSWIDTH_1_BYTE)
  263. max_width = max_width >> 1;
  264. if (buf_addr & (max_width - 1))
  265. max_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
  266. return max_width;
  267. }
  268. static bool stm32_dma_fifo_threshold_is_allowed(u32 burst, u32 threshold,
  269. enum dma_slave_buswidth width)
  270. {
  271. u32 remaining;
  272. if (threshold == STM32_DMA_FIFO_THRESHOLD_NONE)
  273. return false;
  274. if (width != DMA_SLAVE_BUSWIDTH_UNDEFINED) {
  275. if (burst != 0) {
  276. /*
  277. * If number of beats fit in several whole bursts
  278. * this configuration is allowed.
  279. */
  280. remaining = ((STM32_DMA_FIFO_SIZE / width) *
  281. (threshold + 1) / 4) % burst;
  282. if (remaining == 0)
  283. return true;
  284. } else {
  285. return true;
  286. }
  287. }
  288. return false;
  289. }
  290. static bool stm32_dma_is_burst_possible(u32 buf_len, u32 threshold)
  291. {
  292. /* If FIFO direct mode, burst is not possible */
  293. if (threshold == STM32_DMA_FIFO_THRESHOLD_NONE)
  294. return false;
  295. /*
  296. * Buffer or period length has to be aligned on FIFO depth.
  297. * Otherwise bytes may be stuck within FIFO at buffer or period
  298. * length.
  299. */
  300. return ((buf_len % ((threshold + 1) * 4)) == 0);
  301. }
  302. static u32 stm32_dma_get_best_burst(u32 buf_len, u32 max_burst, u32 threshold,
  303. enum dma_slave_buswidth width)
  304. {
  305. u32 best_burst = max_burst;
  306. if (best_burst == 1 || !stm32_dma_is_burst_possible(buf_len, threshold))
  307. return 0;
  308. while ((buf_len < best_burst * width && best_burst > 1) ||
  309. !stm32_dma_fifo_threshold_is_allowed(best_burst, threshold,
  310. width)) {
  311. if (best_burst > STM32_DMA_MIN_BURST)
  312. best_burst = best_burst >> 1;
  313. else
  314. best_burst = 0;
  315. }
  316. return best_burst;
  317. }
  318. static int stm32_dma_get_burst(struct stm32_dma_chan *chan, u32 maxburst)
  319. {
  320. switch (maxburst) {
  321. case 0:
  322. case 1:
  323. return STM32_DMA_BURST_SINGLE;
  324. case 4:
  325. return STM32_DMA_BURST_INCR4;
  326. case 8:
  327. return STM32_DMA_BURST_INCR8;
  328. case 16:
  329. return STM32_DMA_BURST_INCR16;
  330. default:
  331. dev_err(chan2dev(chan), "Dma burst size not supported\n");
  332. return -EINVAL;
  333. }
  334. }
  335. static void stm32_dma_set_fifo_config(struct stm32_dma_chan *chan,
  336. u32 src_burst, u32 dst_burst)
  337. {
  338. chan->chan_reg.dma_sfcr &= ~STM32_DMA_SFCR_MASK;
  339. chan->chan_reg.dma_scr &= ~STM32_DMA_SCR_DMEIE;
  340. if (!src_burst && !dst_burst) {
  341. /* Using direct mode */
  342. chan->chan_reg.dma_scr |= STM32_DMA_SCR_DMEIE;
  343. } else {
  344. /* Using FIFO mode */
  345. chan->chan_reg.dma_sfcr |= STM32_DMA_SFCR_MASK;
  346. }
  347. }
  348. static int stm32_dma_slave_config(struct dma_chan *c,
  349. struct dma_slave_config *config)
  350. {
  351. struct stm32_dma_chan *chan = to_stm32_dma_chan(c);
  352. memcpy(&chan->dma_sconfig, config, sizeof(*config));
  353. /* Check if user is requesting DMA to trigger STM32 MDMA */
  354. if (config->peripheral_size) {
  355. config->peripheral_config = &chan->mdma_config;
  356. config->peripheral_size = sizeof(chan->mdma_config);
  357. chan->trig_mdma = true;
  358. }
  359. chan->config_init = true;
  360. return 0;
  361. }
  362. static u32 stm32_dma_irq_status(struct stm32_dma_chan *chan)
  363. {
  364. struct stm32_dma_device *dmadev = stm32_dma_get_dev(chan);
  365. u32 flags, dma_isr;
  366. /*
  367. * Read "flags" from DMA_xISR register corresponding to the selected
  368. * DMA channel at the correct bit offset inside that register.
  369. */
  370. dma_isr = stm32_dma_read(dmadev, STM32_DMA_ISR(chan->id));
  371. flags = dma_isr >> STM32_DMA_FLAGS_SHIFT(chan->id);
  372. return flags & STM32_DMA_MASKI;
  373. }
  374. static void stm32_dma_irq_clear(struct stm32_dma_chan *chan, u32 flags)
  375. {
  376. struct stm32_dma_device *dmadev = stm32_dma_get_dev(chan);
  377. u32 dma_ifcr;
  378. /*
  379. * Write "flags" to the DMA_xIFCR register corresponding to the selected
  380. * DMA channel at the correct bit offset inside that register.
  381. */
  382. flags &= STM32_DMA_MASKI;
  383. dma_ifcr = flags << STM32_DMA_FLAGS_SHIFT(chan->id);
  384. stm32_dma_write(dmadev, STM32_DMA_IFCR(chan->id), dma_ifcr);
  385. }
  386. static int stm32_dma_disable_chan(struct stm32_dma_chan *chan)
  387. {
  388. struct stm32_dma_device *dmadev = stm32_dma_get_dev(chan);
  389. u32 dma_scr, id, reg;
  390. id = chan->id;
  391. reg = STM32_DMA_SCR(id);
  392. dma_scr = stm32_dma_read(dmadev, reg);
  393. if (dma_scr & STM32_DMA_SCR_EN) {
  394. dma_scr &= ~STM32_DMA_SCR_EN;
  395. stm32_dma_write(dmadev, reg, dma_scr);
  396. return readl_relaxed_poll_timeout_atomic(dmadev->base + reg,
  397. dma_scr, !(dma_scr & STM32_DMA_SCR_EN),
  398. 10, 1000000);
  399. }
  400. return 0;
  401. }
  402. static void stm32_dma_stop(struct stm32_dma_chan *chan)
  403. {
  404. struct stm32_dma_device *dmadev = stm32_dma_get_dev(chan);
  405. u32 dma_scr, dma_sfcr, status;
  406. int ret;
  407. /* Disable interrupts */
  408. dma_scr = stm32_dma_read(dmadev, STM32_DMA_SCR(chan->id));
  409. dma_scr &= ~STM32_DMA_SCR_IRQ_MASK;
  410. stm32_dma_write(dmadev, STM32_DMA_SCR(chan->id), dma_scr);
  411. dma_sfcr = stm32_dma_read(dmadev, STM32_DMA_SFCR(chan->id));
  412. dma_sfcr &= ~STM32_DMA_SFCR_FEIE;
  413. stm32_dma_write(dmadev, STM32_DMA_SFCR(chan->id), dma_sfcr);
  414. /* Disable DMA */
  415. ret = stm32_dma_disable_chan(chan);
  416. if (ret < 0)
  417. return;
  418. /* Clear interrupt status if it is there */
  419. status = stm32_dma_irq_status(chan);
  420. if (status) {
  421. dev_dbg(chan2dev(chan), "%s(): clearing interrupt: 0x%08x\n",
  422. __func__, status);
  423. stm32_dma_irq_clear(chan, status);
  424. }
  425. chan->busy = false;
  426. chan->status = DMA_COMPLETE;
  427. }
  428. static int stm32_dma_terminate_all(struct dma_chan *c)
  429. {
  430. struct stm32_dma_chan *chan = to_stm32_dma_chan(c);
  431. unsigned long flags;
  432. LIST_HEAD(head);
  433. spin_lock_irqsave(&chan->vchan.lock, flags);
  434. if (chan->desc) {
  435. dma_cookie_complete(&chan->desc->vdesc.tx);
  436. vchan_terminate_vdesc(&chan->desc->vdesc);
  437. if (chan->busy)
  438. stm32_dma_stop(chan);
  439. chan->desc = NULL;
  440. }
  441. vchan_get_all_descriptors(&chan->vchan, &head);
  442. spin_unlock_irqrestore(&chan->vchan.lock, flags);
  443. vchan_dma_desc_free_list(&chan->vchan, &head);
  444. return 0;
  445. }
  446. static void stm32_dma_synchronize(struct dma_chan *c)
  447. {
  448. struct stm32_dma_chan *chan = to_stm32_dma_chan(c);
  449. vchan_synchronize(&chan->vchan);
  450. }
  451. static void stm32_dma_dump_reg(struct stm32_dma_chan *chan)
  452. {
  453. struct stm32_dma_device *dmadev = stm32_dma_get_dev(chan);
  454. u32 scr = stm32_dma_read(dmadev, STM32_DMA_SCR(chan->id));
  455. u32 ndtr = stm32_dma_read(dmadev, STM32_DMA_SNDTR(chan->id));
  456. u32 spar = stm32_dma_read(dmadev, STM32_DMA_SPAR(chan->id));
  457. u32 sm0ar = stm32_dma_read(dmadev, STM32_DMA_SM0AR(chan->id));
  458. u32 sm1ar = stm32_dma_read(dmadev, STM32_DMA_SM1AR(chan->id));
  459. u32 sfcr = stm32_dma_read(dmadev, STM32_DMA_SFCR(chan->id));
  460. dev_dbg(chan2dev(chan), "SCR: 0x%08x\n", scr);
  461. dev_dbg(chan2dev(chan), "NDTR: 0x%08x\n", ndtr);
  462. dev_dbg(chan2dev(chan), "SPAR: 0x%08x\n", spar);
  463. dev_dbg(chan2dev(chan), "SM0AR: 0x%08x\n", sm0ar);
  464. dev_dbg(chan2dev(chan), "SM1AR: 0x%08x\n", sm1ar);
  465. dev_dbg(chan2dev(chan), "SFCR: 0x%08x\n", sfcr);
  466. }
  467. static void stm32_dma_sg_inc(struct stm32_dma_chan *chan)
  468. {
  469. chan->next_sg++;
  470. if (chan->desc->cyclic && (chan->next_sg == chan->desc->num_sgs))
  471. chan->next_sg = 0;
  472. }
  473. static void stm32_dma_configure_next_sg(struct stm32_dma_chan *chan);
  474. static void stm32_dma_start_transfer(struct stm32_dma_chan *chan)
  475. {
  476. struct stm32_dma_device *dmadev = stm32_dma_get_dev(chan);
  477. struct virt_dma_desc *vdesc;
  478. struct stm32_dma_sg_req *sg_req;
  479. struct stm32_dma_chan_reg *reg;
  480. u32 status;
  481. int ret;
  482. ret = stm32_dma_disable_chan(chan);
  483. if (ret < 0)
  484. return;
  485. if (!chan->desc) {
  486. vdesc = vchan_next_desc(&chan->vchan);
  487. if (!vdesc)
  488. return;
  489. list_del(&vdesc->node);
  490. chan->desc = to_stm32_dma_desc(vdesc);
  491. chan->next_sg = 0;
  492. }
  493. if (chan->next_sg == chan->desc->num_sgs)
  494. chan->next_sg = 0;
  495. sg_req = &chan->desc->sg_req[chan->next_sg];
  496. reg = &sg_req->chan_reg;
  497. /* When DMA triggers STM32 MDMA, DMA Transfer Complete is managed by STM32 MDMA */
  498. if (chan->trig_mdma && chan->dma_sconfig.direction != DMA_MEM_TO_DEV)
  499. reg->dma_scr &= ~STM32_DMA_SCR_TCIE;
  500. reg->dma_scr &= ~STM32_DMA_SCR_EN;
  501. stm32_dma_write(dmadev, STM32_DMA_SCR(chan->id), reg->dma_scr);
  502. stm32_dma_write(dmadev, STM32_DMA_SPAR(chan->id), reg->dma_spar);
  503. stm32_dma_write(dmadev, STM32_DMA_SM0AR(chan->id), reg->dma_sm0ar);
  504. stm32_dma_write(dmadev, STM32_DMA_SFCR(chan->id), reg->dma_sfcr);
  505. stm32_dma_write(dmadev, STM32_DMA_SM1AR(chan->id), reg->dma_sm1ar);
  506. stm32_dma_write(dmadev, STM32_DMA_SNDTR(chan->id), reg->dma_sndtr);
  507. stm32_dma_sg_inc(chan);
  508. /* Clear interrupt status if it is there */
  509. status = stm32_dma_irq_status(chan);
  510. if (status)
  511. stm32_dma_irq_clear(chan, status);
  512. if (chan->desc->cyclic)
  513. stm32_dma_configure_next_sg(chan);
  514. stm32_dma_dump_reg(chan);
  515. /* Start DMA */
  516. chan->busy = true;
  517. chan->status = DMA_IN_PROGRESS;
  518. reg->dma_scr |= STM32_DMA_SCR_EN;
  519. stm32_dma_write(dmadev, STM32_DMA_SCR(chan->id), reg->dma_scr);
  520. dev_dbg(chan2dev(chan), "vchan %pK: started\n", &chan->vchan);
  521. }
  522. static void stm32_dma_configure_next_sg(struct stm32_dma_chan *chan)
  523. {
  524. struct stm32_dma_device *dmadev = stm32_dma_get_dev(chan);
  525. struct stm32_dma_sg_req *sg_req;
  526. u32 dma_scr, dma_sm0ar, dma_sm1ar, id;
  527. id = chan->id;
  528. dma_scr = stm32_dma_read(dmadev, STM32_DMA_SCR(id));
  529. sg_req = &chan->desc->sg_req[chan->next_sg];
  530. if (dma_scr & STM32_DMA_SCR_CT) {
  531. dma_sm0ar = sg_req->chan_reg.dma_sm0ar;
  532. stm32_dma_write(dmadev, STM32_DMA_SM0AR(id), dma_sm0ar);
  533. dev_dbg(chan2dev(chan), "CT=1 <=> SM0AR: 0x%08x\n",
  534. stm32_dma_read(dmadev, STM32_DMA_SM0AR(id)));
  535. } else {
  536. dma_sm1ar = sg_req->chan_reg.dma_sm1ar;
  537. stm32_dma_write(dmadev, STM32_DMA_SM1AR(id), dma_sm1ar);
  538. dev_dbg(chan2dev(chan), "CT=0 <=> SM1AR: 0x%08x\n",
  539. stm32_dma_read(dmadev, STM32_DMA_SM1AR(id)));
  540. }
  541. }
  542. static void stm32_dma_handle_chan_paused(struct stm32_dma_chan *chan)
  543. {
  544. struct stm32_dma_device *dmadev = stm32_dma_get_dev(chan);
  545. u32 dma_scr;
  546. /*
  547. * Read and store current remaining data items and peripheral/memory addresses to be
  548. * updated on resume
  549. */
  550. dma_scr = stm32_dma_read(dmadev, STM32_DMA_SCR(chan->id));
  551. /*
  552. * Transfer can be paused while between a previous resume and reconfiguration on transfer
  553. * complete. If transfer is cyclic and CIRC and DBM have been deactivated for resume, need
  554. * to set it here in SCR backup to ensure a good reconfiguration on transfer complete.
  555. */
  556. if (chan->desc && chan->desc->cyclic) {
  557. if (chan->desc->num_sgs == 1)
  558. dma_scr |= STM32_DMA_SCR_CIRC;
  559. else
  560. dma_scr |= STM32_DMA_SCR_DBM;
  561. }
  562. chan->chan_reg.dma_scr = dma_scr;
  563. /*
  564. * Need to temporarily deactivate CIRC/DBM until next Transfer Complete interrupt, otherwise
  565. * on resume NDTR autoreload value will be wrong (lower than the initial period length)
  566. */
  567. if (chan->desc && chan->desc->cyclic) {
  568. dma_scr &= ~(STM32_DMA_SCR_DBM | STM32_DMA_SCR_CIRC);
  569. stm32_dma_write(dmadev, STM32_DMA_SCR(chan->id), dma_scr);
  570. }
  571. chan->chan_reg.dma_sndtr = stm32_dma_read(dmadev, STM32_DMA_SNDTR(chan->id));
  572. chan->status = DMA_PAUSED;
  573. dev_dbg(chan2dev(chan), "vchan %pK: paused\n", &chan->vchan);
  574. }
  575. static void stm32_dma_post_resume_reconfigure(struct stm32_dma_chan *chan)
  576. {
  577. struct stm32_dma_device *dmadev = stm32_dma_get_dev(chan);
  578. struct stm32_dma_sg_req *sg_req;
  579. u32 dma_scr, status, id;
  580. id = chan->id;
  581. dma_scr = stm32_dma_read(dmadev, STM32_DMA_SCR(id));
  582. /* Clear interrupt status if it is there */
  583. status = stm32_dma_irq_status(chan);
  584. if (status)
  585. stm32_dma_irq_clear(chan, status);
  586. if (!chan->next_sg)
  587. sg_req = &chan->desc->sg_req[chan->desc->num_sgs - 1];
  588. else
  589. sg_req = &chan->desc->sg_req[chan->next_sg - 1];
  590. /* Reconfigure NDTR with the initial value */
  591. stm32_dma_write(dmadev, STM32_DMA_SNDTR(chan->id), sg_req->chan_reg.dma_sndtr);
  592. /* Restore SPAR */
  593. stm32_dma_write(dmadev, STM32_DMA_SPAR(id), sg_req->chan_reg.dma_spar);
  594. /* Restore SM0AR/SM1AR whatever DBM/CT as they may have been modified */
  595. stm32_dma_write(dmadev, STM32_DMA_SM0AR(id), sg_req->chan_reg.dma_sm0ar);
  596. stm32_dma_write(dmadev, STM32_DMA_SM1AR(id), sg_req->chan_reg.dma_sm1ar);
  597. /* Reactivate CIRC/DBM if needed */
  598. if (chan->chan_reg.dma_scr & STM32_DMA_SCR_DBM) {
  599. dma_scr |= STM32_DMA_SCR_DBM;
  600. /* Restore CT */
  601. if (chan->chan_reg.dma_scr & STM32_DMA_SCR_CT)
  602. dma_scr &= ~STM32_DMA_SCR_CT;
  603. else
  604. dma_scr |= STM32_DMA_SCR_CT;
  605. } else if (chan->chan_reg.dma_scr & STM32_DMA_SCR_CIRC) {
  606. dma_scr |= STM32_DMA_SCR_CIRC;
  607. }
  608. stm32_dma_write(dmadev, STM32_DMA_SCR(chan->id), dma_scr);
  609. stm32_dma_configure_next_sg(chan);
  610. stm32_dma_dump_reg(chan);
  611. dma_scr |= STM32_DMA_SCR_EN;
  612. stm32_dma_write(dmadev, STM32_DMA_SCR(chan->id), dma_scr);
  613. dev_dbg(chan2dev(chan), "vchan %pK: reconfigured after pause/resume\n", &chan->vchan);
  614. }
  615. static void stm32_dma_handle_chan_done(struct stm32_dma_chan *chan, u32 scr)
  616. {
  617. if (!chan->desc)
  618. return;
  619. if (chan->desc->cyclic) {
  620. vchan_cyclic_callback(&chan->desc->vdesc);
  621. if (chan->trig_mdma)
  622. return;
  623. stm32_dma_sg_inc(chan);
  624. /* cyclic while CIRC/DBM disable => post resume reconfiguration needed */
  625. if (!(scr & (STM32_DMA_SCR_CIRC | STM32_DMA_SCR_DBM)))
  626. stm32_dma_post_resume_reconfigure(chan);
  627. else if (scr & STM32_DMA_SCR_DBM)
  628. stm32_dma_configure_next_sg(chan);
  629. } else {
  630. chan->busy = false;
  631. chan->status = DMA_COMPLETE;
  632. if (chan->next_sg == chan->desc->num_sgs) {
  633. vchan_cookie_complete(&chan->desc->vdesc);
  634. chan->desc = NULL;
  635. }
  636. stm32_dma_start_transfer(chan);
  637. }
  638. }
  639. static irqreturn_t stm32_dma_chan_irq(int irq, void *devid)
  640. {
  641. struct stm32_dma_chan *chan = devid;
  642. struct stm32_dma_device *dmadev = stm32_dma_get_dev(chan);
  643. u32 status, scr, sfcr;
  644. spin_lock(&chan->vchan.lock);
  645. status = stm32_dma_irq_status(chan);
  646. scr = stm32_dma_read(dmadev, STM32_DMA_SCR(chan->id));
  647. sfcr = stm32_dma_read(dmadev, STM32_DMA_SFCR(chan->id));
  648. if (status & STM32_DMA_FEI) {
  649. stm32_dma_irq_clear(chan, STM32_DMA_FEI);
  650. status &= ~STM32_DMA_FEI;
  651. if (sfcr & STM32_DMA_SFCR_FEIE) {
  652. if (!(scr & STM32_DMA_SCR_EN) &&
  653. !(status & STM32_DMA_TCI))
  654. dev_err(chan2dev(chan), "FIFO Error\n");
  655. else
  656. dev_dbg(chan2dev(chan), "FIFO over/underrun\n");
  657. }
  658. }
  659. if (status & STM32_DMA_DMEI) {
  660. stm32_dma_irq_clear(chan, STM32_DMA_DMEI);
  661. status &= ~STM32_DMA_DMEI;
  662. if (sfcr & STM32_DMA_SCR_DMEIE)
  663. dev_dbg(chan2dev(chan), "Direct mode overrun\n");
  664. }
  665. if (status & STM32_DMA_TCI) {
  666. stm32_dma_irq_clear(chan, STM32_DMA_TCI);
  667. if (scr & STM32_DMA_SCR_TCIE) {
  668. if (chan->status != DMA_PAUSED)
  669. stm32_dma_handle_chan_done(chan, scr);
  670. }
  671. status &= ~STM32_DMA_TCI;
  672. }
  673. if (status & STM32_DMA_HTI) {
  674. stm32_dma_irq_clear(chan, STM32_DMA_HTI);
  675. status &= ~STM32_DMA_HTI;
  676. }
  677. if (status) {
  678. stm32_dma_irq_clear(chan, status);
  679. dev_err(chan2dev(chan), "DMA error: status=0x%08x\n", status);
  680. if (!(scr & STM32_DMA_SCR_EN))
  681. dev_err(chan2dev(chan), "chan disabled by HW\n");
  682. }
  683. spin_unlock(&chan->vchan.lock);
  684. return IRQ_HANDLED;
  685. }
  686. static void stm32_dma_issue_pending(struct dma_chan *c)
  687. {
  688. struct stm32_dma_chan *chan = to_stm32_dma_chan(c);
  689. unsigned long flags;
  690. spin_lock_irqsave(&chan->vchan.lock, flags);
  691. if (vchan_issue_pending(&chan->vchan) && !chan->desc && !chan->busy) {
  692. dev_dbg(chan2dev(chan), "vchan %pK: issued\n", &chan->vchan);
  693. stm32_dma_start_transfer(chan);
  694. }
  695. spin_unlock_irqrestore(&chan->vchan.lock, flags);
  696. }
  697. static int stm32_dma_pause(struct dma_chan *c)
  698. {
  699. struct stm32_dma_chan *chan = to_stm32_dma_chan(c);
  700. unsigned long flags;
  701. int ret;
  702. if (chan->status != DMA_IN_PROGRESS)
  703. return -EPERM;
  704. spin_lock_irqsave(&chan->vchan.lock, flags);
  705. ret = stm32_dma_disable_chan(chan);
  706. if (!ret)
  707. stm32_dma_handle_chan_paused(chan);
  708. spin_unlock_irqrestore(&chan->vchan.lock, flags);
  709. return ret;
  710. }
  711. static int stm32_dma_resume(struct dma_chan *c)
  712. {
  713. struct stm32_dma_chan *chan = to_stm32_dma_chan(c);
  714. struct stm32_dma_device *dmadev = stm32_dma_get_dev(chan);
  715. struct stm32_dma_chan_reg chan_reg = chan->chan_reg;
  716. u32 id = chan->id, scr, ndtr, offset, spar, sm0ar, sm1ar;
  717. struct stm32_dma_sg_req *sg_req;
  718. unsigned long flags;
  719. if (chan->status != DMA_PAUSED)
  720. return -EPERM;
  721. scr = stm32_dma_read(dmadev, STM32_DMA_SCR(id));
  722. if (WARN_ON(scr & STM32_DMA_SCR_EN))
  723. return -EPERM;
  724. spin_lock_irqsave(&chan->vchan.lock, flags);
  725. /* sg_reg[prev_sg] contains original ndtr, sm0ar and sm1ar before pausing the transfer */
  726. if (!chan->next_sg)
  727. sg_req = &chan->desc->sg_req[chan->desc->num_sgs - 1];
  728. else
  729. sg_req = &chan->desc->sg_req[chan->next_sg - 1];
  730. ndtr = sg_req->chan_reg.dma_sndtr;
  731. offset = (ndtr - chan_reg.dma_sndtr);
  732. offset <<= FIELD_GET(STM32_DMA_SCR_PSIZE_MASK, chan_reg.dma_scr);
  733. spar = sg_req->chan_reg.dma_spar;
  734. sm0ar = sg_req->chan_reg.dma_sm0ar;
  735. sm1ar = sg_req->chan_reg.dma_sm1ar;
  736. /*
  737. * The peripheral and/or memory addresses have to be updated in order to adjust the
  738. * address pointers. Need to check increment.
  739. */
  740. if (chan_reg.dma_scr & STM32_DMA_SCR_PINC)
  741. stm32_dma_write(dmadev, STM32_DMA_SPAR(id), spar + offset);
  742. else
  743. stm32_dma_write(dmadev, STM32_DMA_SPAR(id), spar);
  744. if (!(chan_reg.dma_scr & STM32_DMA_SCR_MINC))
  745. offset = 0;
  746. /*
  747. * In case of DBM, the current target could be SM1AR.
  748. * Need to temporarily deactivate CIRC/DBM to finish the current transfer, so
  749. * SM0AR becomes the current target and must be updated with SM1AR + offset if CT=1.
  750. */
  751. if ((chan_reg.dma_scr & STM32_DMA_SCR_DBM) && (chan_reg.dma_scr & STM32_DMA_SCR_CT))
  752. stm32_dma_write(dmadev, STM32_DMA_SM1AR(id), sm1ar + offset);
  753. else
  754. stm32_dma_write(dmadev, STM32_DMA_SM0AR(id), sm0ar + offset);
  755. /* NDTR must be restored otherwise internal HW counter won't be correctly reset */
  756. stm32_dma_write(dmadev, STM32_DMA_SNDTR(id), chan_reg.dma_sndtr);
  757. /*
  758. * Need to temporarily deactivate CIRC/DBM until next Transfer Complete interrupt,
  759. * otherwise NDTR autoreload value will be wrong (lower than the initial period length)
  760. */
  761. if (chan_reg.dma_scr & (STM32_DMA_SCR_CIRC | STM32_DMA_SCR_DBM))
  762. chan_reg.dma_scr &= ~(STM32_DMA_SCR_CIRC | STM32_DMA_SCR_DBM);
  763. if (chan_reg.dma_scr & STM32_DMA_SCR_DBM)
  764. stm32_dma_configure_next_sg(chan);
  765. stm32_dma_dump_reg(chan);
  766. /* The stream may then be re-enabled to restart transfer from the point it was stopped */
  767. chan->status = DMA_IN_PROGRESS;
  768. chan_reg.dma_scr |= STM32_DMA_SCR_EN;
  769. stm32_dma_write(dmadev, STM32_DMA_SCR(id), chan_reg.dma_scr);
  770. spin_unlock_irqrestore(&chan->vchan.lock, flags);
  771. dev_dbg(chan2dev(chan), "vchan %pK: resumed\n", &chan->vchan);
  772. return 0;
  773. }
  774. static int stm32_dma_set_xfer_param(struct stm32_dma_chan *chan,
  775. enum dma_transfer_direction direction,
  776. enum dma_slave_buswidth *buswidth,
  777. u32 buf_len, dma_addr_t buf_addr)
  778. {
  779. enum dma_slave_buswidth src_addr_width, dst_addr_width;
  780. int src_bus_width, dst_bus_width;
  781. int src_burst_size, dst_burst_size;
  782. u32 src_maxburst, dst_maxburst, src_best_burst, dst_best_burst;
  783. u32 dma_scr, fifoth;
  784. src_addr_width = chan->dma_sconfig.src_addr_width;
  785. dst_addr_width = chan->dma_sconfig.dst_addr_width;
  786. src_maxburst = chan->dma_sconfig.src_maxburst;
  787. dst_maxburst = chan->dma_sconfig.dst_maxburst;
  788. fifoth = chan->threshold;
  789. switch (direction) {
  790. case DMA_MEM_TO_DEV:
  791. /* Set device data size */
  792. dst_bus_width = stm32_dma_get_width(chan, dst_addr_width);
  793. if (dst_bus_width < 0)
  794. return dst_bus_width;
  795. /* Set device burst size */
  796. dst_best_burst = stm32_dma_get_best_burst(buf_len,
  797. dst_maxburst,
  798. fifoth,
  799. dst_addr_width);
  800. dst_burst_size = stm32_dma_get_burst(chan, dst_best_burst);
  801. if (dst_burst_size < 0)
  802. return dst_burst_size;
  803. /* Set memory data size */
  804. src_addr_width = stm32_dma_get_max_width(buf_len, buf_addr,
  805. fifoth);
  806. chan->mem_width = src_addr_width;
  807. src_bus_width = stm32_dma_get_width(chan, src_addr_width);
  808. if (src_bus_width < 0)
  809. return src_bus_width;
  810. /*
  811. * Set memory burst size - burst not possible if address is not aligned on
  812. * the address boundary equal to the size of the transfer
  813. */
  814. if (buf_addr & (buf_len - 1))
  815. src_maxburst = 1;
  816. else
  817. src_maxburst = STM32_DMA_MAX_BURST;
  818. src_best_burst = stm32_dma_get_best_burst(buf_len,
  819. src_maxburst,
  820. fifoth,
  821. src_addr_width);
  822. src_burst_size = stm32_dma_get_burst(chan, src_best_burst);
  823. if (src_burst_size < 0)
  824. return src_burst_size;
  825. dma_scr = FIELD_PREP(STM32_DMA_SCR_DIR_MASK, STM32_DMA_MEM_TO_DEV) |
  826. FIELD_PREP(STM32_DMA_SCR_PSIZE_MASK, dst_bus_width) |
  827. FIELD_PREP(STM32_DMA_SCR_MSIZE_MASK, src_bus_width) |
  828. FIELD_PREP(STM32_DMA_SCR_PBURST_MASK, dst_burst_size) |
  829. FIELD_PREP(STM32_DMA_SCR_MBURST_MASK, src_burst_size);
  830. /* Set FIFO threshold */
  831. chan->chan_reg.dma_sfcr &= ~STM32_DMA_SFCR_FTH_MASK;
  832. if (fifoth != STM32_DMA_FIFO_THRESHOLD_NONE)
  833. chan->chan_reg.dma_sfcr |= FIELD_PREP(STM32_DMA_SFCR_FTH_MASK, fifoth);
  834. /* Set peripheral address */
  835. chan->chan_reg.dma_spar = chan->dma_sconfig.dst_addr;
  836. *buswidth = dst_addr_width;
  837. break;
  838. case DMA_DEV_TO_MEM:
  839. /* Set device data size */
  840. src_bus_width = stm32_dma_get_width(chan, src_addr_width);
  841. if (src_bus_width < 0)
  842. return src_bus_width;
  843. /* Set device burst size */
  844. src_best_burst = stm32_dma_get_best_burst(buf_len,
  845. src_maxburst,
  846. fifoth,
  847. src_addr_width);
  848. chan->mem_burst = src_best_burst;
  849. src_burst_size = stm32_dma_get_burst(chan, src_best_burst);
  850. if (src_burst_size < 0)
  851. return src_burst_size;
  852. /* Set memory data size */
  853. dst_addr_width = stm32_dma_get_max_width(buf_len, buf_addr,
  854. fifoth);
  855. chan->mem_width = dst_addr_width;
  856. dst_bus_width = stm32_dma_get_width(chan, dst_addr_width);
  857. if (dst_bus_width < 0)
  858. return dst_bus_width;
  859. /*
  860. * Set memory burst size - burst not possible if address is not aligned on
  861. * the address boundary equal to the size of the transfer
  862. */
  863. if (buf_addr & (buf_len - 1))
  864. dst_maxburst = 1;
  865. else
  866. dst_maxburst = STM32_DMA_MAX_BURST;
  867. dst_best_burst = stm32_dma_get_best_burst(buf_len,
  868. dst_maxburst,
  869. fifoth,
  870. dst_addr_width);
  871. chan->mem_burst = dst_best_burst;
  872. dst_burst_size = stm32_dma_get_burst(chan, dst_best_burst);
  873. if (dst_burst_size < 0)
  874. return dst_burst_size;
  875. dma_scr = FIELD_PREP(STM32_DMA_SCR_DIR_MASK, STM32_DMA_DEV_TO_MEM) |
  876. FIELD_PREP(STM32_DMA_SCR_PSIZE_MASK, src_bus_width) |
  877. FIELD_PREP(STM32_DMA_SCR_MSIZE_MASK, dst_bus_width) |
  878. FIELD_PREP(STM32_DMA_SCR_PBURST_MASK, src_burst_size) |
  879. FIELD_PREP(STM32_DMA_SCR_MBURST_MASK, dst_burst_size);
  880. /* Set FIFO threshold */
  881. chan->chan_reg.dma_sfcr &= ~STM32_DMA_SFCR_FTH_MASK;
  882. if (fifoth != STM32_DMA_FIFO_THRESHOLD_NONE)
  883. chan->chan_reg.dma_sfcr |= FIELD_PREP(STM32_DMA_SFCR_FTH_MASK, fifoth);
  884. /* Set peripheral address */
  885. chan->chan_reg.dma_spar = chan->dma_sconfig.src_addr;
  886. *buswidth = chan->dma_sconfig.src_addr_width;
  887. break;
  888. default:
  889. dev_err(chan2dev(chan), "Dma direction is not supported\n");
  890. return -EINVAL;
  891. }
  892. stm32_dma_set_fifo_config(chan, src_best_burst, dst_best_burst);
  893. /* Set DMA control register */
  894. chan->chan_reg.dma_scr &= ~(STM32_DMA_SCR_DIR_MASK |
  895. STM32_DMA_SCR_PSIZE_MASK | STM32_DMA_SCR_MSIZE_MASK |
  896. STM32_DMA_SCR_PBURST_MASK | STM32_DMA_SCR_MBURST_MASK);
  897. chan->chan_reg.dma_scr |= dma_scr;
  898. return 0;
  899. }
  900. static void stm32_dma_clear_reg(struct stm32_dma_chan_reg *regs)
  901. {
  902. memset(regs, 0, sizeof(struct stm32_dma_chan_reg));
  903. }
  904. static struct dma_async_tx_descriptor *stm32_dma_prep_slave_sg(
  905. struct dma_chan *c, struct scatterlist *sgl,
  906. u32 sg_len, enum dma_transfer_direction direction,
  907. unsigned long flags, void *context)
  908. {
  909. struct stm32_dma_chan *chan = to_stm32_dma_chan(c);
  910. struct stm32_dma_desc *desc;
  911. struct scatterlist *sg;
  912. enum dma_slave_buswidth buswidth;
  913. u32 nb_data_items;
  914. int i, ret;
  915. if (!chan->config_init) {
  916. dev_err(chan2dev(chan), "dma channel is not configured\n");
  917. return NULL;
  918. }
  919. if (sg_len < 1) {
  920. dev_err(chan2dev(chan), "Invalid segment length %d\n", sg_len);
  921. return NULL;
  922. }
  923. desc = kzalloc(struct_size(desc, sg_req, sg_len), GFP_NOWAIT);
  924. if (!desc)
  925. return NULL;
  926. /* Set peripheral flow controller */
  927. if (chan->dma_sconfig.device_fc)
  928. chan->chan_reg.dma_scr |= STM32_DMA_SCR_PFCTRL;
  929. else
  930. chan->chan_reg.dma_scr &= ~STM32_DMA_SCR_PFCTRL;
  931. /* Activate Double Buffer Mode if DMA triggers STM32 MDMA and more than 1 sg */
  932. if (chan->trig_mdma && sg_len > 1) {
  933. chan->chan_reg.dma_scr |= STM32_DMA_SCR_DBM;
  934. chan->chan_reg.dma_scr &= ~STM32_DMA_SCR_CT;
  935. }
  936. for_each_sg(sgl, sg, sg_len, i) {
  937. ret = stm32_dma_set_xfer_param(chan, direction, &buswidth,
  938. sg_dma_len(sg),
  939. sg_dma_address(sg));
  940. if (ret < 0)
  941. goto err;
  942. desc->sg_req[i].len = sg_dma_len(sg);
  943. nb_data_items = desc->sg_req[i].len / buswidth;
  944. if (nb_data_items > STM32_DMA_ALIGNED_MAX_DATA_ITEMS) {
  945. dev_err(chan2dev(chan), "nb items not supported\n");
  946. goto err;
  947. }
  948. stm32_dma_clear_reg(&desc->sg_req[i].chan_reg);
  949. desc->sg_req[i].chan_reg.dma_scr = chan->chan_reg.dma_scr;
  950. desc->sg_req[i].chan_reg.dma_sfcr = chan->chan_reg.dma_sfcr;
  951. desc->sg_req[i].chan_reg.dma_spar = chan->chan_reg.dma_spar;
  952. desc->sg_req[i].chan_reg.dma_sm0ar = sg_dma_address(sg);
  953. desc->sg_req[i].chan_reg.dma_sm1ar = sg_dma_address(sg);
  954. if (chan->trig_mdma)
  955. desc->sg_req[i].chan_reg.dma_sm1ar += sg_dma_len(sg);
  956. desc->sg_req[i].chan_reg.dma_sndtr = nb_data_items;
  957. }
  958. desc->num_sgs = sg_len;
  959. desc->cyclic = false;
  960. return vchan_tx_prep(&chan->vchan, &desc->vdesc, flags);
  961. err:
  962. kfree(desc);
  963. return NULL;
  964. }
  965. static struct dma_async_tx_descriptor *stm32_dma_prep_dma_cyclic(
  966. struct dma_chan *c, dma_addr_t buf_addr, size_t buf_len,
  967. size_t period_len, enum dma_transfer_direction direction,
  968. unsigned long flags)
  969. {
  970. struct stm32_dma_chan *chan = to_stm32_dma_chan(c);
  971. struct stm32_dma_desc *desc;
  972. enum dma_slave_buswidth buswidth;
  973. u32 num_periods, nb_data_items;
  974. int i, ret;
  975. if (!buf_len || !period_len) {
  976. dev_err(chan2dev(chan), "Invalid buffer/period len\n");
  977. return NULL;
  978. }
  979. if (!chan->config_init) {
  980. dev_err(chan2dev(chan), "dma channel is not configured\n");
  981. return NULL;
  982. }
  983. if (buf_len % period_len) {
  984. dev_err(chan2dev(chan), "buf_len not multiple of period_len\n");
  985. return NULL;
  986. }
  987. /*
  988. * We allow to take more number of requests till DMA is
  989. * not started. The driver will loop over all requests.
  990. * Once DMA is started then new requests can be queued only after
  991. * terminating the DMA.
  992. */
  993. if (chan->busy) {
  994. dev_err(chan2dev(chan), "Request not allowed when dma busy\n");
  995. return NULL;
  996. }
  997. ret = stm32_dma_set_xfer_param(chan, direction, &buswidth, period_len,
  998. buf_addr);
  999. if (ret < 0)
  1000. return NULL;
  1001. nb_data_items = period_len / buswidth;
  1002. if (nb_data_items > STM32_DMA_ALIGNED_MAX_DATA_ITEMS) {
  1003. dev_err(chan2dev(chan), "number of items not supported\n");
  1004. return NULL;
  1005. }
  1006. /* Enable Circular mode or double buffer mode */
  1007. if (buf_len == period_len) {
  1008. chan->chan_reg.dma_scr |= STM32_DMA_SCR_CIRC;
  1009. } else {
  1010. chan->chan_reg.dma_scr |= STM32_DMA_SCR_DBM;
  1011. chan->chan_reg.dma_scr &= ~STM32_DMA_SCR_CT;
  1012. }
  1013. /* Clear periph ctrl if client set it */
  1014. chan->chan_reg.dma_scr &= ~STM32_DMA_SCR_PFCTRL;
  1015. num_periods = buf_len / period_len;
  1016. desc = kzalloc(struct_size(desc, sg_req, num_periods), GFP_NOWAIT);
  1017. if (!desc)
  1018. return NULL;
  1019. for (i = 0; i < num_periods; i++) {
  1020. desc->sg_req[i].len = period_len;
  1021. stm32_dma_clear_reg(&desc->sg_req[i].chan_reg);
  1022. desc->sg_req[i].chan_reg.dma_scr = chan->chan_reg.dma_scr;
  1023. desc->sg_req[i].chan_reg.dma_sfcr = chan->chan_reg.dma_sfcr;
  1024. desc->sg_req[i].chan_reg.dma_spar = chan->chan_reg.dma_spar;
  1025. desc->sg_req[i].chan_reg.dma_sm0ar = buf_addr;
  1026. desc->sg_req[i].chan_reg.dma_sm1ar = buf_addr;
  1027. if (chan->trig_mdma)
  1028. desc->sg_req[i].chan_reg.dma_sm1ar += period_len;
  1029. desc->sg_req[i].chan_reg.dma_sndtr = nb_data_items;
  1030. if (!chan->trig_mdma)
  1031. buf_addr += period_len;
  1032. }
  1033. desc->num_sgs = num_periods;
  1034. desc->cyclic = true;
  1035. return vchan_tx_prep(&chan->vchan, &desc->vdesc, flags);
  1036. }
  1037. static struct dma_async_tx_descriptor *stm32_dma_prep_dma_memcpy(
  1038. struct dma_chan *c, dma_addr_t dest,
  1039. dma_addr_t src, size_t len, unsigned long flags)
  1040. {
  1041. struct stm32_dma_chan *chan = to_stm32_dma_chan(c);
  1042. enum dma_slave_buswidth max_width;
  1043. struct stm32_dma_desc *desc;
  1044. size_t xfer_count, offset;
  1045. u32 num_sgs, best_burst, dma_burst, threshold;
  1046. int i;
  1047. num_sgs = DIV_ROUND_UP(len, STM32_DMA_ALIGNED_MAX_DATA_ITEMS);
  1048. desc = kzalloc(struct_size(desc, sg_req, num_sgs), GFP_NOWAIT);
  1049. if (!desc)
  1050. return NULL;
  1051. threshold = chan->threshold;
  1052. for (offset = 0, i = 0; offset < len; offset += xfer_count, i++) {
  1053. xfer_count = min_t(size_t, len - offset,
  1054. STM32_DMA_ALIGNED_MAX_DATA_ITEMS);
  1055. /* Compute best burst size */
  1056. max_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
  1057. best_burst = stm32_dma_get_best_burst(len, STM32_DMA_MAX_BURST,
  1058. threshold, max_width);
  1059. dma_burst = stm32_dma_get_burst(chan, best_burst);
  1060. stm32_dma_clear_reg(&desc->sg_req[i].chan_reg);
  1061. desc->sg_req[i].chan_reg.dma_scr =
  1062. FIELD_PREP(STM32_DMA_SCR_DIR_MASK, STM32_DMA_MEM_TO_MEM) |
  1063. FIELD_PREP(STM32_DMA_SCR_PBURST_MASK, dma_burst) |
  1064. FIELD_PREP(STM32_DMA_SCR_MBURST_MASK, dma_burst) |
  1065. STM32_DMA_SCR_MINC |
  1066. STM32_DMA_SCR_PINC |
  1067. STM32_DMA_SCR_TCIE |
  1068. STM32_DMA_SCR_TEIE;
  1069. desc->sg_req[i].chan_reg.dma_sfcr |= STM32_DMA_SFCR_MASK;
  1070. desc->sg_req[i].chan_reg.dma_sfcr |= FIELD_PREP(STM32_DMA_SFCR_FTH_MASK, threshold);
  1071. desc->sg_req[i].chan_reg.dma_spar = src + offset;
  1072. desc->sg_req[i].chan_reg.dma_sm0ar = dest + offset;
  1073. desc->sg_req[i].chan_reg.dma_sndtr = xfer_count;
  1074. desc->sg_req[i].len = xfer_count;
  1075. }
  1076. desc->num_sgs = num_sgs;
  1077. desc->cyclic = false;
  1078. return vchan_tx_prep(&chan->vchan, &desc->vdesc, flags);
  1079. }
  1080. static u32 stm32_dma_get_remaining_bytes(struct stm32_dma_chan *chan)
  1081. {
  1082. u32 dma_scr, width, ndtr;
  1083. struct stm32_dma_device *dmadev = stm32_dma_get_dev(chan);
  1084. dma_scr = stm32_dma_read(dmadev, STM32_DMA_SCR(chan->id));
  1085. width = FIELD_GET(STM32_DMA_SCR_PSIZE_MASK, dma_scr);
  1086. ndtr = stm32_dma_read(dmadev, STM32_DMA_SNDTR(chan->id));
  1087. return ndtr << width;
  1088. }
  1089. /**
  1090. * stm32_dma_is_current_sg - check that expected sg_req is currently transferred
  1091. * @chan: dma channel
  1092. *
  1093. * This function called when IRQ are disable, checks that the hardware has not
  1094. * switched on the next transfer in double buffer mode. The test is done by
  1095. * comparing the next_sg memory address with the hardware related register
  1096. * (based on CT bit value).
  1097. *
  1098. * Returns true if expected current transfer is still running or double
  1099. * buffer mode is not activated.
  1100. */
  1101. static bool stm32_dma_is_current_sg(struct stm32_dma_chan *chan)
  1102. {
  1103. struct stm32_dma_device *dmadev = stm32_dma_get_dev(chan);
  1104. struct stm32_dma_sg_req *sg_req;
  1105. u32 dma_scr, dma_smar, id, period_len;
  1106. id = chan->id;
  1107. dma_scr = stm32_dma_read(dmadev, STM32_DMA_SCR(id));
  1108. /* In cyclic CIRC but not DBM, CT is not used */
  1109. if (!(dma_scr & STM32_DMA_SCR_DBM))
  1110. return true;
  1111. sg_req = &chan->desc->sg_req[chan->next_sg];
  1112. period_len = sg_req->len;
  1113. /* DBM - take care of a previous pause/resume not yet post reconfigured */
  1114. if (dma_scr & STM32_DMA_SCR_CT) {
  1115. dma_smar = stm32_dma_read(dmadev, STM32_DMA_SM0AR(id));
  1116. /*
  1117. * If transfer has been pause/resumed,
  1118. * SM0AR is in the range of [SM0AR:SM0AR+period_len]
  1119. */
  1120. return (dma_smar >= sg_req->chan_reg.dma_sm0ar &&
  1121. dma_smar < sg_req->chan_reg.dma_sm0ar + period_len);
  1122. }
  1123. dma_smar = stm32_dma_read(dmadev, STM32_DMA_SM1AR(id));
  1124. /*
  1125. * If transfer has been pause/resumed,
  1126. * SM1AR is in the range of [SM1AR:SM1AR+period_len]
  1127. */
  1128. return (dma_smar >= sg_req->chan_reg.dma_sm1ar &&
  1129. dma_smar < sg_req->chan_reg.dma_sm1ar + period_len);
  1130. }
  1131. static size_t stm32_dma_desc_residue(struct stm32_dma_chan *chan,
  1132. struct stm32_dma_desc *desc,
  1133. u32 next_sg)
  1134. {
  1135. u32 modulo, burst_size;
  1136. u32 residue;
  1137. u32 n_sg = next_sg;
  1138. struct stm32_dma_sg_req *sg_req = &chan->desc->sg_req[chan->next_sg];
  1139. int i;
  1140. /*
  1141. * Calculate the residue means compute the descriptors
  1142. * information:
  1143. * - the sg_req currently transferred
  1144. * - the Hardware remaining position in this sg (NDTR bits field).
  1145. *
  1146. * A race condition may occur if DMA is running in cyclic or double
  1147. * buffer mode, since the DMA register are automatically reloaded at end
  1148. * of period transfer. The hardware may have switched to the next
  1149. * transfer (CT bit updated) just before the position (SxNDTR reg) is
  1150. * read.
  1151. * In this case the SxNDTR reg could (or not) correspond to the new
  1152. * transfer position, and not the expected one.
  1153. * The strategy implemented in the stm32 driver is to:
  1154. * - read the SxNDTR register
  1155. * - crosscheck that hardware is still in current transfer.
  1156. * In case of switch, we can assume that the DMA is at the beginning of
  1157. * the next transfer. So we approximate the residue in consequence, by
  1158. * pointing on the beginning of next transfer.
  1159. *
  1160. * This race condition doesn't apply for none cyclic mode, as double
  1161. * buffer is not used. In such situation registers are updated by the
  1162. * software.
  1163. */
  1164. residue = stm32_dma_get_remaining_bytes(chan);
  1165. if ((chan->desc->cyclic || chan->trig_mdma) && !stm32_dma_is_current_sg(chan)) {
  1166. n_sg++;
  1167. if (n_sg == chan->desc->num_sgs)
  1168. n_sg = 0;
  1169. if (!chan->trig_mdma)
  1170. residue = sg_req->len;
  1171. }
  1172. /*
  1173. * In cyclic mode, for the last period, residue = remaining bytes
  1174. * from NDTR,
  1175. * else for all other periods in cyclic mode, and in sg mode,
  1176. * residue = remaining bytes from NDTR + remaining
  1177. * periods/sg to be transferred
  1178. */
  1179. if ((!chan->desc->cyclic && !chan->trig_mdma) || n_sg != 0)
  1180. for (i = n_sg; i < desc->num_sgs; i++)
  1181. residue += desc->sg_req[i].len;
  1182. if (!chan->mem_burst)
  1183. return residue;
  1184. burst_size = chan->mem_burst * chan->mem_width;
  1185. modulo = residue % burst_size;
  1186. if (modulo)
  1187. residue = residue - modulo + burst_size;
  1188. return residue;
  1189. }
  1190. static enum dma_status stm32_dma_tx_status(struct dma_chan *c,
  1191. dma_cookie_t cookie,
  1192. struct dma_tx_state *state)
  1193. {
  1194. struct stm32_dma_chan *chan = to_stm32_dma_chan(c);
  1195. struct virt_dma_desc *vdesc;
  1196. enum dma_status status;
  1197. unsigned long flags;
  1198. u32 residue = 0;
  1199. status = dma_cookie_status(c, cookie, state);
  1200. if (status == DMA_COMPLETE)
  1201. return status;
  1202. status = chan->status;
  1203. if (!state)
  1204. return status;
  1205. spin_lock_irqsave(&chan->vchan.lock, flags);
  1206. vdesc = vchan_find_desc(&chan->vchan, cookie);
  1207. if (chan->desc && cookie == chan->desc->vdesc.tx.cookie)
  1208. residue = stm32_dma_desc_residue(chan, chan->desc,
  1209. chan->next_sg);
  1210. else if (vdesc)
  1211. residue = stm32_dma_desc_residue(chan,
  1212. to_stm32_dma_desc(vdesc), 0);
  1213. dma_set_residue(state, residue);
  1214. spin_unlock_irqrestore(&chan->vchan.lock, flags);
  1215. return status;
  1216. }
  1217. static int stm32_dma_alloc_chan_resources(struct dma_chan *c)
  1218. {
  1219. struct stm32_dma_chan *chan = to_stm32_dma_chan(c);
  1220. struct stm32_dma_device *dmadev = stm32_dma_get_dev(chan);
  1221. int ret;
  1222. chan->config_init = false;
  1223. ret = pm_runtime_resume_and_get(dmadev->ddev.dev);
  1224. if (ret < 0)
  1225. return ret;
  1226. ret = stm32_dma_disable_chan(chan);
  1227. if (ret < 0)
  1228. pm_runtime_put(dmadev->ddev.dev);
  1229. return ret;
  1230. }
  1231. static void stm32_dma_free_chan_resources(struct dma_chan *c)
  1232. {
  1233. struct stm32_dma_chan *chan = to_stm32_dma_chan(c);
  1234. struct stm32_dma_device *dmadev = stm32_dma_get_dev(chan);
  1235. unsigned long flags;
  1236. dev_dbg(chan2dev(chan), "Freeing channel %d\n", chan->id);
  1237. if (chan->busy) {
  1238. spin_lock_irqsave(&chan->vchan.lock, flags);
  1239. stm32_dma_stop(chan);
  1240. chan->desc = NULL;
  1241. spin_unlock_irqrestore(&chan->vchan.lock, flags);
  1242. }
  1243. pm_runtime_put(dmadev->ddev.dev);
  1244. vchan_free_chan_resources(to_virt_chan(c));
  1245. stm32_dma_clear_reg(&chan->chan_reg);
  1246. chan->threshold = 0;
  1247. }
  1248. static void stm32_dma_desc_free(struct virt_dma_desc *vdesc)
  1249. {
  1250. kfree(container_of(vdesc, struct stm32_dma_desc, vdesc));
  1251. }
  1252. static void stm32_dma_set_config(struct stm32_dma_chan *chan,
  1253. struct stm32_dma_cfg *cfg)
  1254. {
  1255. stm32_dma_clear_reg(&chan->chan_reg);
  1256. chan->chan_reg.dma_scr = cfg->stream_config & STM32_DMA_SCR_CFG_MASK;
  1257. chan->chan_reg.dma_scr |= FIELD_PREP(STM32_DMA_SCR_REQ_MASK, cfg->request_line);
  1258. /* Enable Interrupts */
  1259. chan->chan_reg.dma_scr |= STM32_DMA_SCR_TEIE | STM32_DMA_SCR_TCIE;
  1260. chan->threshold = FIELD_GET(STM32_DMA_THRESHOLD_FTR_MASK, cfg->features);
  1261. if (FIELD_GET(STM32_DMA_DIRECT_MODE_MASK, cfg->features))
  1262. chan->threshold = STM32_DMA_FIFO_THRESHOLD_NONE;
  1263. if (FIELD_GET(STM32_DMA_ALT_ACK_MODE_MASK, cfg->features))
  1264. chan->chan_reg.dma_scr |= STM32_DMA_SCR_TRBUFF;
  1265. chan->mdma_config.stream_id = FIELD_GET(STM32_DMA_MDMA_STREAM_ID_MASK, cfg->features);
  1266. }
  1267. static struct dma_chan *stm32_dma_of_xlate(struct of_phandle_args *dma_spec,
  1268. struct of_dma *ofdma)
  1269. {
  1270. struct stm32_dma_device *dmadev = ofdma->of_dma_data;
  1271. struct device *dev = dmadev->ddev.dev;
  1272. struct stm32_dma_cfg cfg;
  1273. struct stm32_dma_chan *chan;
  1274. struct dma_chan *c;
  1275. if (dma_spec->args_count < 4) {
  1276. dev_err(dev, "Bad number of cells\n");
  1277. return NULL;
  1278. }
  1279. cfg.channel_id = dma_spec->args[0];
  1280. cfg.request_line = dma_spec->args[1];
  1281. cfg.stream_config = dma_spec->args[2];
  1282. cfg.features = dma_spec->args[3];
  1283. if (cfg.channel_id >= STM32_DMA_MAX_CHANNELS ||
  1284. cfg.request_line >= STM32_DMA_MAX_REQUEST_ID) {
  1285. dev_err(dev, "Bad channel and/or request id\n");
  1286. return NULL;
  1287. }
  1288. chan = &dmadev->chan[cfg.channel_id];
  1289. c = dma_get_slave_channel(&chan->vchan.chan);
  1290. if (!c) {
  1291. dev_err(dev, "No more channels available\n");
  1292. return NULL;
  1293. }
  1294. stm32_dma_set_config(chan, &cfg);
  1295. return c;
  1296. }
  1297. static const struct of_device_id stm32_dma_of_match[] = {
  1298. { .compatible = "st,stm32-dma", },
  1299. { /* sentinel */ },
  1300. };
  1301. MODULE_DEVICE_TABLE(of, stm32_dma_of_match);
  1302. static int stm32_dma_probe(struct platform_device *pdev)
  1303. {
  1304. struct stm32_dma_chan *chan;
  1305. struct stm32_dma_device *dmadev;
  1306. struct dma_device *dd;
  1307. const struct of_device_id *match;
  1308. struct resource *res;
  1309. struct reset_control *rst;
  1310. int i, ret;
  1311. match = of_match_device(stm32_dma_of_match, &pdev->dev);
  1312. if (!match) {
  1313. dev_err(&pdev->dev, "Error: No device match found\n");
  1314. return -ENODEV;
  1315. }
  1316. dmadev = devm_kzalloc(&pdev->dev, sizeof(*dmadev), GFP_KERNEL);
  1317. if (!dmadev)
  1318. return -ENOMEM;
  1319. dd = &dmadev->ddev;
  1320. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1321. dmadev->base = devm_ioremap_resource(&pdev->dev, res);
  1322. if (IS_ERR(dmadev->base))
  1323. return PTR_ERR(dmadev->base);
  1324. dmadev->clk = devm_clk_get(&pdev->dev, NULL);
  1325. if (IS_ERR(dmadev->clk))
  1326. return dev_err_probe(&pdev->dev, PTR_ERR(dmadev->clk), "Can't get clock\n");
  1327. ret = clk_prepare_enable(dmadev->clk);
  1328. if (ret < 0) {
  1329. dev_err(&pdev->dev, "clk_prep_enable error: %d\n", ret);
  1330. return ret;
  1331. }
  1332. dmadev->mem2mem = of_property_read_bool(pdev->dev.of_node,
  1333. "st,mem2mem");
  1334. rst = devm_reset_control_get(&pdev->dev, NULL);
  1335. if (IS_ERR(rst)) {
  1336. ret = PTR_ERR(rst);
  1337. if (ret == -EPROBE_DEFER)
  1338. goto clk_free;
  1339. } else {
  1340. reset_control_assert(rst);
  1341. udelay(2);
  1342. reset_control_deassert(rst);
  1343. }
  1344. dma_set_max_seg_size(&pdev->dev, STM32_DMA_ALIGNED_MAX_DATA_ITEMS);
  1345. dma_cap_set(DMA_SLAVE, dd->cap_mask);
  1346. dma_cap_set(DMA_PRIVATE, dd->cap_mask);
  1347. dma_cap_set(DMA_CYCLIC, dd->cap_mask);
  1348. dd->device_alloc_chan_resources = stm32_dma_alloc_chan_resources;
  1349. dd->device_free_chan_resources = stm32_dma_free_chan_resources;
  1350. dd->device_tx_status = stm32_dma_tx_status;
  1351. dd->device_issue_pending = stm32_dma_issue_pending;
  1352. dd->device_prep_slave_sg = stm32_dma_prep_slave_sg;
  1353. dd->device_prep_dma_cyclic = stm32_dma_prep_dma_cyclic;
  1354. dd->device_config = stm32_dma_slave_config;
  1355. dd->device_pause = stm32_dma_pause;
  1356. dd->device_resume = stm32_dma_resume;
  1357. dd->device_terminate_all = stm32_dma_terminate_all;
  1358. dd->device_synchronize = stm32_dma_synchronize;
  1359. dd->src_addr_widths = BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) |
  1360. BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) |
  1361. BIT(DMA_SLAVE_BUSWIDTH_4_BYTES);
  1362. dd->dst_addr_widths = BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) |
  1363. BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) |
  1364. BIT(DMA_SLAVE_BUSWIDTH_4_BYTES);
  1365. dd->directions = BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV);
  1366. dd->residue_granularity = DMA_RESIDUE_GRANULARITY_BURST;
  1367. dd->copy_align = DMAENGINE_ALIGN_32_BYTES;
  1368. dd->max_burst = STM32_DMA_MAX_BURST;
  1369. dd->max_sg_burst = STM32_DMA_ALIGNED_MAX_DATA_ITEMS;
  1370. dd->descriptor_reuse = true;
  1371. dd->dev = &pdev->dev;
  1372. INIT_LIST_HEAD(&dd->channels);
  1373. if (dmadev->mem2mem) {
  1374. dma_cap_set(DMA_MEMCPY, dd->cap_mask);
  1375. dd->device_prep_dma_memcpy = stm32_dma_prep_dma_memcpy;
  1376. dd->directions |= BIT(DMA_MEM_TO_MEM);
  1377. }
  1378. for (i = 0; i < STM32_DMA_MAX_CHANNELS; i++) {
  1379. chan = &dmadev->chan[i];
  1380. chan->id = i;
  1381. chan->vchan.desc_free = stm32_dma_desc_free;
  1382. vchan_init(&chan->vchan, dd);
  1383. chan->mdma_config.ifcr = res->start;
  1384. chan->mdma_config.ifcr += STM32_DMA_IFCR(chan->id);
  1385. chan->mdma_config.tcf = STM32_DMA_TCI;
  1386. chan->mdma_config.tcf <<= STM32_DMA_FLAGS_SHIFT(chan->id);
  1387. }
  1388. ret = dma_async_device_register(dd);
  1389. if (ret)
  1390. goto clk_free;
  1391. for (i = 0; i < STM32_DMA_MAX_CHANNELS; i++) {
  1392. chan = &dmadev->chan[i];
  1393. ret = platform_get_irq(pdev, i);
  1394. if (ret < 0)
  1395. goto err_unregister;
  1396. chan->irq = ret;
  1397. ret = devm_request_irq(&pdev->dev, chan->irq,
  1398. stm32_dma_chan_irq, 0,
  1399. dev_name(chan2dev(chan)), chan);
  1400. if (ret) {
  1401. dev_err(&pdev->dev,
  1402. "request_irq failed with err %d channel %d\n",
  1403. ret, i);
  1404. goto err_unregister;
  1405. }
  1406. }
  1407. ret = of_dma_controller_register(pdev->dev.of_node,
  1408. stm32_dma_of_xlate, dmadev);
  1409. if (ret < 0) {
  1410. dev_err(&pdev->dev,
  1411. "STM32 DMA DMA OF registration failed %d\n", ret);
  1412. goto err_unregister;
  1413. }
  1414. platform_set_drvdata(pdev, dmadev);
  1415. pm_runtime_set_active(&pdev->dev);
  1416. pm_runtime_enable(&pdev->dev);
  1417. pm_runtime_get_noresume(&pdev->dev);
  1418. pm_runtime_put(&pdev->dev);
  1419. dev_info(&pdev->dev, "STM32 DMA driver registered\n");
  1420. return 0;
  1421. err_unregister:
  1422. dma_async_device_unregister(dd);
  1423. clk_free:
  1424. clk_disable_unprepare(dmadev->clk);
  1425. return ret;
  1426. }
  1427. #ifdef CONFIG_PM
  1428. static int stm32_dma_runtime_suspend(struct device *dev)
  1429. {
  1430. struct stm32_dma_device *dmadev = dev_get_drvdata(dev);
  1431. clk_disable_unprepare(dmadev->clk);
  1432. return 0;
  1433. }
  1434. static int stm32_dma_runtime_resume(struct device *dev)
  1435. {
  1436. struct stm32_dma_device *dmadev = dev_get_drvdata(dev);
  1437. int ret;
  1438. ret = clk_prepare_enable(dmadev->clk);
  1439. if (ret) {
  1440. dev_err(dev, "failed to prepare_enable clock\n");
  1441. return ret;
  1442. }
  1443. return 0;
  1444. }
  1445. #endif
  1446. #ifdef CONFIG_PM_SLEEP
  1447. static int stm32_dma_pm_suspend(struct device *dev)
  1448. {
  1449. struct stm32_dma_device *dmadev = dev_get_drvdata(dev);
  1450. int id, ret, scr;
  1451. ret = pm_runtime_resume_and_get(dev);
  1452. if (ret < 0)
  1453. return ret;
  1454. for (id = 0; id < STM32_DMA_MAX_CHANNELS; id++) {
  1455. scr = stm32_dma_read(dmadev, STM32_DMA_SCR(id));
  1456. if (scr & STM32_DMA_SCR_EN) {
  1457. dev_warn(dev, "Suspend is prevented by Chan %i\n", id);
  1458. return -EBUSY;
  1459. }
  1460. }
  1461. pm_runtime_put_sync(dev);
  1462. pm_runtime_force_suspend(dev);
  1463. return 0;
  1464. }
  1465. static int stm32_dma_pm_resume(struct device *dev)
  1466. {
  1467. return pm_runtime_force_resume(dev);
  1468. }
  1469. #endif
  1470. static const struct dev_pm_ops stm32_dma_pm_ops = {
  1471. SET_SYSTEM_SLEEP_PM_OPS(stm32_dma_pm_suspend, stm32_dma_pm_resume)
  1472. SET_RUNTIME_PM_OPS(stm32_dma_runtime_suspend,
  1473. stm32_dma_runtime_resume, NULL)
  1474. };
  1475. static struct platform_driver stm32_dma_driver = {
  1476. .driver = {
  1477. .name = "stm32-dma",
  1478. .of_match_table = stm32_dma_of_match,
  1479. .pm = &stm32_dma_pm_ops,
  1480. },
  1481. .probe = stm32_dma_probe,
  1482. };
  1483. static int __init stm32_dma_init(void)
  1484. {
  1485. return platform_driver_register(&stm32_dma_driver);
  1486. }
  1487. subsys_initcall(stm32_dma_init);