ark-axi-dma.c 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646
  1. // SPDX-License-Identifier: GPL-2.0
  2. // (C) 2017-2018 Synopsys, Inc. (www.synopsys.com)
  3. /*
  4. * Synopsys DesignWare AXI DMA Controller driver.
  5. *
  6. * Author: Eugeniy Paltsev <Eugeniy.Paltsev@synopsys.com>
  7. */
  8. #include <linux/bitops.h>
  9. #include <linux/delay.h>
  10. #include <linux/device.h>
  11. #include <linux/dmaengine.h>
  12. #include <linux/dmapool.h>
  13. #include <linux/dma-mapping.h>
  14. #include <linux/err.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/io.h>
  17. #include <linux/iopoll.h>
  18. #include <linux/io-64-nonatomic-lo-hi.h>
  19. #include <linux/kernel.h>
  20. #include <linux/module.h>
  21. #include <linux/of.h>
  22. #include <linux/of_dma.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/pm_runtime.h>
  25. #include <linux/property.h>
  26. #include <linux/slab.h>
  27. #include <linux/types.h>
  28. #include "ark-axi-dma.h"
  29. #include "dmaengine.h"
  30. #include "virt-dma.h"
  31. /*
  32. * The set of bus widths supported by the DMA controller. DW AXI DMAC supports
  33. * master data bus width up to 512 bits (for both AXI master interfaces), but
  34. * it depends on IP block configurarion.
  35. */
  36. #define AXI_DMA_BUSWIDTHS \
  37. (DMA_SLAVE_BUSWIDTH_1_BYTE | \
  38. DMA_SLAVE_BUSWIDTH_2_BYTES | \
  39. DMA_SLAVE_BUSWIDTH_4_BYTES | \
  40. DMA_SLAVE_BUSWIDTH_8_BYTES | \
  41. DMA_SLAVE_BUSWIDTH_16_BYTES | \
  42. DMA_SLAVE_BUSWIDTH_32_BYTES | \
  43. DMA_SLAVE_BUSWIDTH_64_BYTES)
  44. static inline void
  45. axi_dma_iowrite32(struct axi_dma_chip *chip, u32 reg, u32 val)
  46. {
  47. iowrite32(val, chip->regs + reg);
  48. }
  49. static inline u32 axi_dma_ioread32(struct axi_dma_chip *chip, u32 reg)
  50. {
  51. return ioread32(chip->regs + reg);
  52. }
  53. static inline void
  54. axi_chan_iowrite32(struct axi_dma_chan *chan, u32 reg, u32 val)
  55. {
  56. iowrite32(val, chan->chan_regs + reg);
  57. }
  58. static inline u32 axi_chan_ioread32(struct axi_dma_chan *chan, u32 reg)
  59. {
  60. return ioread32(chan->chan_regs + reg);
  61. }
  62. static inline void
  63. axi_chan_iowrite64(struct axi_dma_chan *chan, u32 reg, u64 val)
  64. {
  65. /*
  66. * We split one 64 bit write for two 32 bit write as some HW doesn't
  67. * support 64 bit access.
  68. */
  69. iowrite32(lower_32_bits(val), chan->chan_regs + reg);
  70. iowrite32(upper_32_bits(val), chan->chan_regs + reg + 4);
  71. }
  72. static inline void axi_dma_disable(struct axi_dma_chip *chip)
  73. {
  74. u32 val;
  75. val = axi_dma_ioread32(chip, DMAC_CFG);
  76. val &= ~DMAC_EN_MASK;
  77. axi_dma_iowrite32(chip, DMAC_CFG, val);
  78. }
  79. static inline void axi_dma_enable(struct axi_dma_chip *chip)
  80. {
  81. u32 val;
  82. val = axi_dma_ioread32(chip, DMAC_CFG);
  83. val |= DMAC_EN_MASK;
  84. axi_dma_iowrite32(chip, DMAC_CFG, val);
  85. }
  86. static inline void axi_dma_irq_disable(struct axi_dma_chip *chip)
  87. {
  88. u32 val;
  89. val = axi_dma_ioread32(chip, DMAC_CFG);
  90. val &= ~INT_EN_MASK;
  91. axi_dma_iowrite32(chip, DMAC_CFG, val);
  92. }
  93. static inline void axi_dma_irq_enable(struct axi_dma_chip *chip)
  94. {
  95. u32 val;
  96. val = axi_dma_ioread32(chip, DMAC_CFG);
  97. val |= INT_EN_MASK;
  98. axi_dma_iowrite32(chip, DMAC_CFG, val);
  99. }
  100. static inline void axi_chan_irq_disable(struct axi_dma_chan *chan, u32 irq_mask)
  101. {
  102. u32 val;
  103. if (likely(irq_mask == DWAXIDMAC_IRQ_ALL)) {
  104. axi_chan_iowrite32(chan, CH_INTSTATUS_ENA, DWAXIDMAC_IRQ_NONE);
  105. } else {
  106. val = axi_chan_ioread32(chan, CH_INTSTATUS_ENA);
  107. val &= ~irq_mask;
  108. axi_chan_iowrite32(chan, CH_INTSTATUS_ENA, val);
  109. }
  110. }
  111. static inline void axi_chan_irq_set(struct axi_dma_chan *chan, u32 irq_mask)
  112. {
  113. axi_chan_iowrite32(chan, CH_INTSTATUS_ENA, irq_mask);
  114. }
  115. static inline void axi_chan_irq_sig_set(struct axi_dma_chan *chan, u32 irq_mask)
  116. {
  117. axi_chan_iowrite32(chan, CH_INTSIGNAL_ENA, irq_mask);
  118. }
  119. static inline void axi_chan_irq_clear(struct axi_dma_chan *chan, u32 irq_mask)
  120. {
  121. axi_chan_iowrite32(chan, CH_INTCLEAR, irq_mask);
  122. }
  123. static inline u32 axi_chan_irq_read(struct axi_dma_chan *chan)
  124. {
  125. return axi_chan_ioread32(chan, CH_INTSTATUS);
  126. }
  127. static inline void axi_chan_disable(struct axi_dma_chan *chan)
  128. {
  129. u32 val;
  130. //printk(KERN_ALERT "++++++++++++++axi_chan_disable\n");
  131. val = axi_dma_ioread32(chan->chip, DMAC_CHEN);
  132. val &= ~(BIT(chan->id) << DMAC_CHAN_EN_SHIFT);
  133. val |= BIT(chan->id) << DMAC_CHAN_EN_WE_SHIFT;
  134. axi_dma_iowrite32(chan->chip, DMAC_CHEN, val);
  135. }
  136. static inline void axi_chan_enable(struct axi_dma_chan *chan)
  137. {
  138. u32 val;
  139. //printk(KERN_ALERT "++++++++++++++axi_chan_enable\n");
  140. val = axi_dma_ioread32(chan->chip, DMAC_CHEN);
  141. val |= BIT(chan->id) << DMAC_CHAN_EN_SHIFT |
  142. BIT(chan->id) << DMAC_CHAN_EN_WE_SHIFT;
  143. axi_dma_iowrite32(chan->chip, DMAC_CHEN, val);
  144. }
  145. static inline bool axi_chan_is_hw_enable(struct axi_dma_chan *chan)
  146. {
  147. u32 val;
  148. val = axi_dma_ioread32(chan->chip, DMAC_CHEN);
  149. return !!(val & (BIT(chan->id) << DMAC_CHAN_EN_SHIFT));
  150. }
  151. static inline void axi_chan_abort(struct axi_dma_chan *chan)
  152. {
  153. u32 val;
  154. val = axi_dma_ioread32(chan->chip, DMAC_CHEN_H);
  155. val |= BIT(chan->id) << DMAC_CHAN_EN_SHIFT;
  156. val |= BIT(chan->id) << DMAC_CHAN_EN_WE_SHIFT;
  157. axi_dma_iowrite32(chan->chip, DMAC_CHEN_H, val);
  158. }
  159. static void axi_dma_hw_init(struct axi_dma_chip *chip)
  160. {
  161. u32 i;
  162. for (i = 0; i < chip->dw->hdata->nr_channels; i++) {
  163. axi_chan_irq_disable(&chip->dw->chan[i], DWAXIDMAC_IRQ_ALL);
  164. axi_chan_disable(&chip->dw->chan[i]);
  165. }
  166. }
  167. static u32 axi_chan_get_xfer_width(struct axi_dma_chan *chan, dma_addr_t src,
  168. dma_addr_t dst, size_t len)
  169. {
  170. u32 max_width = chan->chip->dw->hdata->m_data_width;
  171. return __ffs(src | dst | len | BIT(max_width));
  172. }
  173. static inline const char *axi_chan_name(struct axi_dma_chan *chan)
  174. {
  175. return dma_chan_name(&chan->vc.chan);
  176. }
  177. static struct axi_dma_desc *axi_desc_alloc(u32 num)
  178. {
  179. struct axi_dma_desc *desc;
  180. desc = kzalloc(sizeof(*desc), GFP_NOWAIT);
  181. if (!desc)
  182. return NULL;
  183. desc->hw_desc = kcalloc(num, sizeof(*desc->hw_desc), GFP_NOWAIT);
  184. if (!desc->hw_desc) {
  185. kfree(desc);
  186. return NULL;
  187. }
  188. return desc;
  189. }
  190. static struct axi_dma_lli *axi_desc_get(struct axi_dma_chan *chan,
  191. dma_addr_t *addr)
  192. {
  193. struct axi_dma_lli *lli;
  194. dma_addr_t phys;
  195. lli = dma_pool_zalloc(chan->desc_pool, GFP_NOWAIT, &phys);
  196. if (unlikely(!lli)) {
  197. dev_err(chan2dev(chan), "%s: not enough descriptors available\n",
  198. axi_chan_name(chan));
  199. return NULL;
  200. }
  201. atomic_inc(&chan->descs_allocated);
  202. *addr = phys;
  203. return lli;
  204. }
  205. static void axi_desc_put(struct axi_dma_desc *desc)
  206. {
  207. struct axi_dma_chan *chan = desc->chan;
  208. int count = atomic_read(&chan->descs_allocated);
  209. struct axi_dma_hw_desc *hw_desc;
  210. int descs_put;
  211. for (descs_put = 0; descs_put < count; descs_put++) {
  212. hw_desc = &desc->hw_desc[descs_put];
  213. dma_pool_free(chan->desc_pool, hw_desc->lli, hw_desc->llp);
  214. }
  215. kfree(desc->hw_desc);
  216. kfree(desc);
  217. atomic_sub(descs_put, &chan->descs_allocated);
  218. dev_vdbg(chan2dev(chan), "%s: %d descs put, %d still allocated\n",
  219. axi_chan_name(chan), descs_put,
  220. atomic_read(&chan->descs_allocated));
  221. }
  222. static void vchan_desc_put(struct virt_dma_desc *vdesc)
  223. {
  224. axi_desc_put(vd_to_axi_desc(vdesc));
  225. }
  226. /* Returns how many bytes were already received from source */
  227. static inline u32 dma_chan_get_sent(struct axi_dma_chan *chan)
  228. {
  229. u32 block_ts = axi_chan_ioread32(chan, CH_STATUS);
  230. u32 ctllo = axi_chan_ioread32(chan, CH_CTL_L);
  231. return (block_ts & DMAX_MAX_BLK_MASK) * (1 << (ctllo >> CH_CTL_L_SRC_WIDTH_POS & 7));
  232. }
  233. static enum dma_status
  234. dma_chan_tx_status(struct dma_chan *dchan, dma_cookie_t cookie,
  235. struct dma_tx_state *txstate)
  236. {
  237. struct axi_dma_chan *chan = dchan_to_axi_dma_chan(dchan);
  238. struct virt_dma_desc *vdesc;
  239. enum dma_status status;
  240. u32 completed_length;
  241. unsigned long flags;
  242. u32 completed_blocks;
  243. size_t bytes = 0;
  244. u32 length;
  245. u32 len;
  246. status = dma_cookie_status(dchan, cookie, txstate);
  247. if (status == DMA_COMPLETE || !txstate)
  248. return status;
  249. spin_lock_irqsave(&chan->vc.lock, flags);
  250. vdesc = vchan_find_desc(&chan->vc, cookie);
  251. if (vdesc) {
  252. length = vd_to_axi_desc(vdesc)->length;
  253. len = vd_to_axi_desc(vdesc)->hw_desc[0].len;
  254. if (length == len) {
  255. bytes = length - dma_chan_get_sent(chan);
  256. } else {
  257. if (chan->cyclic) {
  258. int count = atomic_read(&chan->descs_allocated);
  259. struct axi_dma_desc *desc = vd_to_axi_desc(vdesc);
  260. u32 sentf, sentr;
  261. u64 llp;
  262. int i;
  263. sentf = dma_chan_get_sent(chan);
  264. llp = lo_hi_readq(chan->chan_regs + CH_LLP);
  265. sentr = dma_chan_get_sent(chan);
  266. if (sentr < sentf)
  267. llp = lo_hi_readq(chan->chan_regs + CH_LLP);
  268. bytes = length;
  269. for (i = 0; i < count; i++) {
  270. if (llp == DWC_LLP_LOC(desc->hw_desc[i].lli->llp)) {
  271. bytes -= sentr;
  272. break;
  273. } else {
  274. bytes -= desc->hw_desc[i].len;
  275. }
  276. }
  277. } else {
  278. completed_blocks = vd_to_axi_desc(vdesc)->completed_blocks;
  279. completed_length = completed_blocks * len;
  280. bytes = length - completed_length;
  281. }
  282. }
  283. } else {
  284. bytes = 0;//vd_to_axi_desc(vdesc)->length;
  285. }
  286. spin_unlock_irqrestore(&chan->vc.lock, flags);
  287. dma_set_residue(txstate, bytes);
  288. if (chan->is_paused && status == DMA_IN_PROGRESS)
  289. status = DMA_PAUSED;
  290. return status;
  291. }
  292. static void write_desc_llp(struct axi_dma_hw_desc *desc, dma_addr_t adr)
  293. {
  294. desc->lli->llp = cpu_to_le64(adr);
  295. }
  296. static void write_chan_llp(struct axi_dma_chan *chan, dma_addr_t adr)
  297. {
  298. axi_chan_iowrite64(chan, CH_LLP, adr);
  299. }
  300. /* Called in chan locked context */
  301. static void axi_chan_block_xfer_start(struct axi_dma_chan *chan,
  302. struct axi_dma_desc *first)
  303. {
  304. u32 priority = chan->chip->dw->hdata->priority[chan->id];
  305. u32 reg, irq_mask;
  306. u8 lms = 0; /* Select AXI0 master for LLI fetching */
  307. if (unlikely(axi_chan_is_hw_enable(chan))) {
  308. dev_err(chan2dev(chan), "%s is non-idle!\n",
  309. axi_chan_name(chan));
  310. return;
  311. }
  312. axi_dma_enable(chan->chip);
  313. reg = (DWAXIDMAC_MBLK_TYPE_LL << CH_CFG_L_DST_MULTBLK_TYPE_POS |
  314. DWAXIDMAC_MBLK_TYPE_LL << CH_CFG_L_SRC_MULTBLK_TYPE_POS);
  315. if (chan->direction == DMA_MEM_TO_DEV)
  316. reg |= (chan->hw_handshake_num << CH_CFG_L_DST_PER_POS);
  317. else if (chan->direction == DMA_DEV_TO_MEM)
  318. reg |= (chan->hw_handshake_num << CH_CFG_L_SRC_PER_POS);
  319. axi_chan_iowrite32(chan, CH_CFG_L, reg);
  320. reg = (DWAXIDMAC_TT_FC_MEM_TO_MEM_DMAC << CH_CFG_H_TT_FC_POS |
  321. priority << CH_CFG_H_PRIORITY_POS |
  322. DWAXIDMAC_HS_SEL_HW << CH_CFG_H_HS_SEL_DST_POS |
  323. DWAXIDMAC_HS_SEL_HW << CH_CFG_H_HS_SEL_SRC_POS);
  324. switch (chan->direction) {
  325. case DMA_MEM_TO_DEV:
  326. reg |= (chan->config.device_fc ?
  327. DWAXIDMAC_TT_FC_MEM_TO_PER_DST :
  328. DWAXIDMAC_TT_FC_MEM_TO_PER_DMAC)
  329. << CH_CFG_H_TT_FC_POS;
  330. break;
  331. case DMA_DEV_TO_MEM:
  332. reg |= (chan->config.device_fc ?
  333. DWAXIDMAC_TT_FC_PER_TO_MEM_SRC :
  334. DWAXIDMAC_TT_FC_PER_TO_MEM_DMAC)
  335. << CH_CFG_H_TT_FC_POS;
  336. break;
  337. default:
  338. break;
  339. }
  340. axi_chan_iowrite32(chan, CH_CFG_H, reg);
  341. write_chan_llp(chan, first->hw_desc[0].llp | lms);
  342. irq_mask = DWAXIDMAC_IRQ_DMA_TRF | DWAXIDMAC_IRQ_ALL_ERR;
  343. if (chan->cyclic)
  344. irq_mask |= DWAXIDMAC_IRQ_BLOCK_TRF;
  345. axi_chan_irq_sig_set(chan, irq_mask);
  346. /* Generate 'suspend' status but don't generate interrupt */
  347. irq_mask |= DWAXIDMAC_IRQ_SUSPENDED;
  348. axi_chan_irq_set(chan, irq_mask);
  349. axi_chan_enable(chan);
  350. }
  351. static void axi_chan_start_first_queued(struct axi_dma_chan *chan)
  352. {
  353. struct axi_dma_desc *desc;
  354. struct virt_dma_desc *vd;
  355. vd = vchan_next_desc(&chan->vc);
  356. if (!vd)
  357. return;
  358. desc = vd_to_axi_desc(vd);
  359. dev_vdbg(chan2dev(chan), "%s: started %u\n", axi_chan_name(chan),
  360. vd->tx.cookie);
  361. axi_chan_block_xfer_start(chan, desc);
  362. }
  363. static void dma_chan_issue_pending(struct dma_chan *dchan)
  364. {
  365. struct axi_dma_chan *chan = dchan_to_axi_dma_chan(dchan);
  366. unsigned long flags;
  367. spin_lock_irqsave(&chan->vc.lock, flags);
  368. if (vchan_issue_pending(&chan->vc))
  369. axi_chan_start_first_queued(chan);
  370. #ifdef DMA_GUARD_TIMER
  371. if (chan->cyclic) {
  372. ktime_t kt = ktime_set(0, DMA_GUARD_TIMER_PERIOD);
  373. hrtimer_start(&chan->hrt, kt, HRTIMER_MODE_REL);
  374. }
  375. #endif
  376. spin_unlock_irqrestore(&chan->vc.lock, flags);
  377. }
  378. static void dw_axi_dma_synchronize(struct dma_chan *dchan)
  379. {
  380. struct axi_dma_chan *chan = dchan_to_axi_dma_chan(dchan);
  381. vchan_synchronize(&chan->vc);
  382. }
  383. #ifdef DMA_GUARD_TIMER
  384. static enum hrtimer_restart dma_chan_hrtimer_callback(struct hrtimer *hrt)
  385. {
  386. struct axi_dma_chan *chan = container_of(hrt, struct axi_dma_chan, hrt);
  387. printk(KERN_ALERT "dma_chan_hrtimer_callback.\n");
  388. if (axi_chan_is_hw_enable(chan)) {
  389. axi_chan_abort(chan);
  390. udelay(100);
  391. if (!axi_chan_is_hw_enable(chan)) {
  392. struct virt_dma_desc *vd = vchan_next_desc(&chan->vc);
  393. int count = atomic_read(&chan->descs_allocated);
  394. struct axi_dma_desc *desc;
  395. int i;
  396. if (vd) {
  397. desc = vd_to_axi_desc(vd);
  398. if (desc) {
  399. for (i = 0; i < count; i++)
  400. desc->hw_desc[i].lli->ctl_hi |= CH_CTL_H_LLI_VALID;
  401. /* Try to restart the controller */
  402. axi_chan_start_first_queued(chan);
  403. }
  404. }
  405. }
  406. }
  407. hrtimer_forward_now(hrt, ns_to_ktime(DMA_GUARD_TIMER_PERIOD));
  408. return HRTIMER_RESTART;
  409. }
  410. #endif
  411. static int dma_chan_alloc_chan_resources(struct dma_chan *dchan)
  412. {
  413. struct axi_dma_chan *chan = dchan_to_axi_dma_chan(dchan);
  414. /* ASSERT: channel is idle */
  415. if (axi_chan_is_hw_enable(chan)) {
  416. dev_err(chan2dev(chan), "%s is non-idle!\n",
  417. axi_chan_name(chan));
  418. return -EBUSY;
  419. }
  420. #ifdef DMA_GUARD_TIMER
  421. /* initialize rt timer */
  422. hrtimer_init(&chan->hrt, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  423. chan->hrt.function = dma_chan_hrtimer_callback;
  424. #endif
  425. /* LLI address must be aligned to a 64-byte boundary */
  426. chan->desc_pool = dma_pool_create(dev_name(chan2dev(chan)),
  427. chan->chip->dev,
  428. sizeof(struct axi_dma_lli),
  429. 64, 0);
  430. if (!chan->desc_pool) {
  431. dev_err(chan2dev(chan), "No memory for descriptors\n");
  432. return -ENOMEM;
  433. }
  434. dev_vdbg(dchan2dev(dchan), "%s: allocating\n", axi_chan_name(chan));
  435. pm_runtime_get(chan->chip->dev);
  436. return 0;
  437. }
  438. static void dma_chan_free_chan_resources(struct dma_chan *dchan)
  439. {
  440. struct axi_dma_chan *chan = dchan_to_axi_dma_chan(dchan);
  441. /* ASSERT: channel is idle */
  442. if (axi_chan_is_hw_enable(chan))
  443. dev_err(dchan2dev(dchan), "%s is non-idle!\n",
  444. axi_chan_name(chan));
  445. #ifdef DMA_GUARD_TIMER
  446. if (chan->cyclic)
  447. hrtimer_cancel(&chan->hrt);
  448. #endif
  449. axi_chan_disable(chan);
  450. axi_chan_irq_disable(chan, DWAXIDMAC_IRQ_ALL);
  451. vchan_free_chan_resources(&chan->vc);
  452. dma_pool_destroy(chan->desc_pool);
  453. chan->desc_pool = NULL;
  454. dev_vdbg(dchan2dev(dchan),
  455. "%s: free resources, descriptor still allocated: %u\n",
  456. axi_chan_name(chan), atomic_read(&chan->descs_allocated));
  457. pm_runtime_put(chan->chip->dev);
  458. }
  459. /*
  460. * If DW_axi_dmac sees CHx_CTL.ShadowReg_Or_LLI_Last bit of the fetched LLI
  461. * as 1, it understands that the current block is the final block in the
  462. * transfer and completes the DMA transfer operation at the end of current
  463. * block transfer.
  464. */
  465. static void set_desc_last(struct axi_dma_hw_desc *desc)
  466. {
  467. u32 val;
  468. val = le32_to_cpu(desc->lli->ctl_hi);
  469. val |= CH_CTL_H_LLI_LAST;
  470. desc->lli->ctl_hi = cpu_to_le32(val);
  471. }
  472. static void write_desc_sar(struct axi_dma_hw_desc *desc, dma_addr_t adr)
  473. {
  474. desc->lli->sar = cpu_to_le64(adr);
  475. }
  476. static void write_desc_dar(struct axi_dma_hw_desc *desc, dma_addr_t adr)
  477. {
  478. desc->lli->dar = cpu_to_le64(adr);
  479. }
  480. static void set_desc_src_master(struct axi_dma_hw_desc *desc)
  481. {
  482. u32 val;
  483. /* Select AXI0 for source master */
  484. val = le32_to_cpu(desc->lli->ctl_lo);
  485. val &= ~CH_CTL_L_SRC_MAST;
  486. desc->lli->ctl_lo = cpu_to_le32(val);
  487. }
  488. static void set_desc_dest_master(struct axi_dma_hw_desc *hw_desc,
  489. struct axi_dma_desc *desc)
  490. {
  491. u32 val;
  492. /* Select AXI1 for source master if available */
  493. val = le32_to_cpu(hw_desc->lli->ctl_lo);
  494. if (desc->chan->chip->dw->hdata->nr_masters > 1)
  495. val |= CH_CTL_L_DST_MAST;
  496. else
  497. val &= ~CH_CTL_L_DST_MAST;
  498. hw_desc->lli->ctl_lo = cpu_to_le32(val);
  499. }
  500. static int dw_axi_dma_set_hw_desc(struct axi_dma_chan *chan,
  501. struct axi_dma_hw_desc *hw_desc,
  502. dma_addr_t mem_addr, size_t len)
  503. {
  504. unsigned int data_width = BIT(chan->chip->dw->hdata->m_data_width);
  505. unsigned int reg_width;
  506. unsigned int mem_width;
  507. dma_addr_t device_addr;
  508. size_t axi_block_ts;
  509. size_t block_ts;
  510. u32 ctllo, ctlhi;
  511. u32 burst_len;
  512. u8 dmsize, smsize;
  513. u8 dms, sms;
  514. axi_block_ts = chan->chip->dw->hdata->block_size[chan->id];
  515. mem_width = __ffs(data_width | mem_addr | len);
  516. if (mem_width > DWAXIDMAC_TRANS_WIDTH_32)
  517. mem_width = DWAXIDMAC_TRANS_WIDTH_32;
  518. if (!IS_ALIGNED(mem_addr, 4)) {
  519. dev_err(chan->chip->dev, "invalid buffer alignment\n");
  520. return -EINVAL;
  521. }
  522. switch (chan->direction) {
  523. case DMA_MEM_TO_DEV:
  524. reg_width = __ffs(chan->config.dst_addr_width);
  525. device_addr = chan->config.dst_addr;
  526. ctllo = reg_width << CH_CTL_L_DST_WIDTH_POS |
  527. mem_width << CH_CTL_L_SRC_WIDTH_POS |
  528. DWAXIDMAC_CH_CTL_L_NOINC << CH_CTL_L_DST_INC_POS |
  529. DWAXIDMAC_CH_CTL_L_INC << CH_CTL_L_SRC_INC_POS;
  530. block_ts = len >> mem_width;
  531. break;
  532. case DMA_DEV_TO_MEM:
  533. reg_width = __ffs(chan->config.src_addr_width);
  534. device_addr = chan->config.src_addr;
  535. ctllo = reg_width << CH_CTL_L_SRC_WIDTH_POS |
  536. mem_width << CH_CTL_L_DST_WIDTH_POS |
  537. DWAXIDMAC_CH_CTL_L_INC << CH_CTL_L_DST_INC_POS |
  538. DWAXIDMAC_CH_CTL_L_NOINC << CH_CTL_L_SRC_INC_POS;
  539. block_ts = len >> reg_width;
  540. break;
  541. default:
  542. return -EINVAL;
  543. }
  544. if (block_ts > axi_block_ts)
  545. return -EINVAL;
  546. hw_desc->lli = axi_desc_get(chan, &hw_desc->llp);
  547. if (unlikely(!hw_desc->lli))
  548. return -ENOMEM;
  549. ctlhi = CH_CTL_H_LLI_VALID;
  550. if (chan->cyclic)
  551. ctlhi |= CH_CTL_H_IOC_BLKTFR_EN;
  552. if (chan->chip->dw->hdata->restrict_axi_burst_len) {
  553. burst_len = chan->chip->dw->hdata->axi_rw_burst_len;
  554. ctlhi |= CH_CTL_H_ARLEN_EN | CH_CTL_H_AWLEN_EN |
  555. burst_len << CH_CTL_H_ARLEN_POS |
  556. burst_len << CH_CTL_H_AWLEN_POS;
  557. }
  558. hw_desc->lli->ctl_hi = cpu_to_le32(ctlhi);
  559. if (chan->direction == DMA_MEM_TO_DEV) {
  560. write_desc_sar(hw_desc, mem_addr);
  561. write_desc_dar(hw_desc, device_addr);
  562. } else {
  563. write_desc_sar(hw_desc, device_addr);
  564. write_desc_dar(hw_desc, mem_addr);
  565. }
  566. hw_desc->lli->block_ts_lo = cpu_to_le32(block_ts - 1);
  567. dmsize = is_slave_direction(chan->direction) ?
  568. chan->config.dst_maxburst : DWAXIDMAC_BURST_TRANS_LEN_4;
  569. smsize = is_slave_direction(chan->direction) ?
  570. chan->config.src_maxburst : DWAXIDMAC_BURST_TRANS_LEN_4;
  571. ctllo |= dmsize << CH_CTL_L_DST_MSIZE_POS | smsize << CH_CTL_L_SRC_MSIZE_POS;
  572. dms = chan->direction == DMA_MEM_TO_DEV ? chan->p_master : chan->m_master;
  573. sms = chan->direction == DMA_DEV_TO_MEM ? chan->p_master : chan->m_master;
  574. ctllo |= DWC_CTLL_DMS(dms) | DWC_CTLL_SMS(sms);
  575. hw_desc->lli->ctl_lo = cpu_to_le32(ctllo);
  576. set_desc_src_master(hw_desc);
  577. hw_desc->len = len;
  578. return 0;
  579. }
  580. static size_t calculate_block_len(struct axi_dma_chan *chan,
  581. dma_addr_t dma_addr, size_t buf_len,
  582. enum dma_transfer_direction direction)
  583. {
  584. u32 data_width, reg_width, mem_width;
  585. size_t axi_block_ts, block_len;
  586. axi_block_ts = chan->chip->dw->hdata->block_size[chan->id];
  587. switch (direction) {
  588. case DMA_MEM_TO_DEV:
  589. data_width = BIT(chan->chip->dw->hdata->m_data_width);
  590. mem_width = __ffs(data_width | dma_addr | buf_len);
  591. if (mem_width > DWAXIDMAC_TRANS_WIDTH_32)
  592. mem_width = DWAXIDMAC_TRANS_WIDTH_32;
  593. block_len = axi_block_ts << mem_width;
  594. break;
  595. case DMA_DEV_TO_MEM:
  596. reg_width = __ffs(chan->config.src_addr_width);
  597. block_len = axi_block_ts << reg_width;
  598. break;
  599. default:
  600. block_len = 0;
  601. }
  602. return block_len;
  603. }
  604. static struct dma_async_tx_descriptor *
  605. dw_axi_dma_chan_prep_cyclic(struct dma_chan *dchan, dma_addr_t dma_addr,
  606. size_t buf_len, size_t period_len,
  607. enum dma_transfer_direction direction,
  608. unsigned long flags)
  609. {
  610. struct axi_dma_chan *chan = dchan_to_axi_dma_chan(dchan);
  611. struct axi_dma_hw_desc *hw_desc = NULL;
  612. struct axi_dma_desc *desc = NULL;
  613. dma_addr_t src_addr = dma_addr;
  614. u32 num_periods, num_segments;
  615. size_t axi_block_len;
  616. u32 total_segments;
  617. u32 segment_len;
  618. unsigned int i;
  619. int status;
  620. u64 llp = 0;
  621. u8 lms = DWC_LLP_LMS(chan->m_master);
  622. num_periods = buf_len / period_len;
  623. axi_block_len = calculate_block_len(chan, dma_addr, buf_len, direction);
  624. if (axi_block_len == 0)
  625. return NULL;
  626. num_segments = DIV_ROUND_UP(period_len, axi_block_len);
  627. segment_len = DIV_ROUND_UP(period_len, num_segments);
  628. total_segments = num_periods * num_segments;
  629. desc = axi_desc_alloc(total_segments);
  630. if (unlikely(!desc))
  631. goto err_desc_get;
  632. chan->direction = direction;
  633. desc->chan = chan;
  634. chan->cyclic = true;
  635. desc->length = 0;
  636. desc->period_len = period_len;
  637. for (i = 0; i < total_segments; i++) {
  638. hw_desc = &desc->hw_desc[i];
  639. status = dw_axi_dma_set_hw_desc(chan, hw_desc, src_addr,
  640. segment_len);
  641. if (status < 0)
  642. goto err_desc_get;
  643. desc->length += hw_desc->len;
  644. /* Set end-of-link to the linked descriptor, so that cyclic
  645. * callback function can be triggered during interrupt.
  646. */
  647. //set_desc_last(hw_desc);
  648. src_addr += segment_len;
  649. }
  650. llp = desc->hw_desc[0].llp;
  651. /* Managed transfer list */
  652. do {
  653. hw_desc = &desc->hw_desc[--total_segments];
  654. write_desc_llp(hw_desc, llp | lms);
  655. llp = hw_desc->llp;
  656. } while (total_segments);
  657. return vchan_tx_prep(&chan->vc, &desc->vd, flags);
  658. err_desc_get:
  659. if (desc)
  660. axi_desc_put(desc);
  661. return NULL;
  662. }
  663. static struct dma_async_tx_descriptor *
  664. dw_axi_dma_chan_prep_slave_sg(struct dma_chan *dchan, struct scatterlist *sgl,
  665. unsigned int sg_len,
  666. enum dma_transfer_direction direction,
  667. unsigned long flags, void *context)
  668. {
  669. struct axi_dma_chan *chan = dchan_to_axi_dma_chan(dchan);
  670. struct axi_dma_hw_desc *hw_desc = NULL;
  671. struct axi_dma_desc *desc = NULL;
  672. u32 num_segments, segment_len;
  673. unsigned int loop = 0;
  674. struct scatterlist *sg;
  675. size_t axi_block_len;
  676. u32 len, num_sgs = 0;
  677. unsigned int i;
  678. dma_addr_t mem;
  679. int status;
  680. u64 llp = 0;
  681. u8 lms = DWC_LLP_LMS(chan->m_master);
  682. if (unlikely(!is_slave_direction(direction) || !sg_len))
  683. return NULL;
  684. mem = sg_dma_address(sgl);
  685. len = sg_dma_len(sgl);
  686. axi_block_len = calculate_block_len(chan, mem, len, direction);
  687. if (axi_block_len == 0)
  688. return NULL;
  689. for_each_sg(sgl, sg, sg_len, i)
  690. num_sgs += DIV_ROUND_UP(sg_dma_len(sg), axi_block_len);
  691. desc = axi_desc_alloc(num_sgs);
  692. if (unlikely(!desc))
  693. goto err_desc_get;
  694. desc->chan = chan;
  695. desc->length = 0;
  696. chan->direction = direction;
  697. for_each_sg(sgl, sg, sg_len, i) {
  698. mem = sg_dma_address(sg);
  699. len = sg_dma_len(sg);
  700. num_segments = DIV_ROUND_UP(sg_dma_len(sg), axi_block_len);
  701. segment_len = DIV_ROUND_UP(sg_dma_len(sg), num_segments);
  702. do {
  703. hw_desc = &desc->hw_desc[loop++];
  704. status = dw_axi_dma_set_hw_desc(chan, hw_desc, mem, segment_len);
  705. if (status < 0)
  706. goto err_desc_get;
  707. desc->length += hw_desc->len;
  708. len -= segment_len;
  709. mem += segment_len;
  710. } while (len >= segment_len);
  711. }
  712. /* Set end-of-link to the last link descriptor of list */
  713. set_desc_last(&desc->hw_desc[num_sgs - 1]);
  714. /* Managed transfer list */
  715. do {
  716. hw_desc = &desc->hw_desc[--num_sgs];
  717. write_desc_llp(hw_desc, llp | lms);
  718. llp = hw_desc->llp;
  719. } while (num_sgs);
  720. return vchan_tx_prep(&chan->vc, &desc->vd, flags);
  721. err_desc_get:
  722. if (desc)
  723. axi_desc_put(desc);
  724. return NULL;
  725. }
  726. static struct dma_async_tx_descriptor *
  727. dma_chan_prep_dma_memcpy(struct dma_chan *dchan, dma_addr_t dst_adr,
  728. dma_addr_t src_adr, size_t len, unsigned long flags)
  729. {
  730. struct axi_dma_chan *chan = dchan_to_axi_dma_chan(dchan);
  731. size_t block_ts, max_block_ts, xfer_len;
  732. struct axi_dma_hw_desc *hw_desc = NULL;
  733. struct axi_dma_desc *desc = NULL;
  734. u32 xfer_width, reg, num;
  735. u64 llp = 0;
  736. u8 lms = 0; /* Select AXI0 master for LLI fetching */
  737. dev_dbg(chan2dev(chan), "%s: memcpy: src: %pad dst: %pad length: %zd flags: %#lx",
  738. axi_chan_name(chan), &src_adr, &dst_adr, len, flags);
  739. max_block_ts = chan->chip->dw->hdata->block_size[chan->id];
  740. xfer_width = axi_chan_get_xfer_width(chan, src_adr, dst_adr, len);
  741. num = DIV_ROUND_UP(len, max_block_ts << xfer_width);
  742. desc = axi_desc_alloc(num);
  743. if (unlikely(!desc))
  744. goto err_desc_get;
  745. desc->chan = chan;
  746. num = 0;
  747. desc->length = 0;
  748. while (len) {
  749. xfer_len = len;
  750. hw_desc = &desc->hw_desc[num];
  751. /*
  752. * Take care for the alignment.
  753. * Actually source and destination widths can be different, but
  754. * make them same to be simpler.
  755. */
  756. xfer_width = axi_chan_get_xfer_width(chan, src_adr, dst_adr, xfer_len);
  757. /*
  758. * block_ts indicates the total number of data of width
  759. * to be transferred in a DMA block transfer.
  760. * BLOCK_TS register should be set to block_ts - 1
  761. */
  762. block_ts = xfer_len >> xfer_width;
  763. if (block_ts > max_block_ts) {
  764. block_ts = max_block_ts;
  765. xfer_len = max_block_ts << xfer_width;
  766. }
  767. hw_desc->lli = axi_desc_get(chan, &hw_desc->llp);
  768. if (unlikely(!hw_desc->lli))
  769. goto err_desc_get;
  770. write_desc_sar(hw_desc, src_adr);
  771. write_desc_dar(hw_desc, dst_adr);
  772. hw_desc->lli->block_ts_lo = cpu_to_le32(block_ts - 1);
  773. reg = CH_CTL_H_LLI_VALID;
  774. if (chan->chip->dw->hdata->restrict_axi_burst_len) {
  775. u32 burst_len = chan->chip->dw->hdata->axi_rw_burst_len;
  776. reg |= (CH_CTL_H_ARLEN_EN |
  777. burst_len << CH_CTL_H_ARLEN_POS |
  778. CH_CTL_H_AWLEN_EN |
  779. burst_len << CH_CTL_H_AWLEN_POS);
  780. }
  781. hw_desc->lli->ctl_hi = cpu_to_le32(reg);
  782. reg = (DWAXIDMAC_BURST_TRANS_LEN_4 << CH_CTL_L_DST_MSIZE_POS |
  783. DWAXIDMAC_BURST_TRANS_LEN_4 << CH_CTL_L_SRC_MSIZE_POS |
  784. xfer_width << CH_CTL_L_DST_WIDTH_POS |
  785. xfer_width << CH_CTL_L_SRC_WIDTH_POS |
  786. DWAXIDMAC_CH_CTL_L_INC << CH_CTL_L_DST_INC_POS |
  787. DWAXIDMAC_CH_CTL_L_INC << CH_CTL_L_SRC_INC_POS);
  788. hw_desc->lli->ctl_lo = cpu_to_le32(reg);
  789. set_desc_src_master(hw_desc);
  790. set_desc_dest_master(hw_desc, desc);
  791. hw_desc->len = xfer_len;
  792. desc->length += hw_desc->len;
  793. /* update the length and addresses for the next loop cycle */
  794. len -= xfer_len;
  795. dst_adr += xfer_len;
  796. src_adr += xfer_len;
  797. num++;
  798. }
  799. /* Set end-of-link to the last link descriptor of list */
  800. set_desc_last(&desc->hw_desc[num - 1]);
  801. /* Managed transfer list */
  802. do {
  803. hw_desc = &desc->hw_desc[--num];
  804. write_desc_llp(hw_desc, llp | lms);
  805. llp = hw_desc->llp;
  806. } while (num);
  807. return vchan_tx_prep(&chan->vc, &desc->vd, flags);
  808. err_desc_get:
  809. if (desc)
  810. axi_desc_put(desc);
  811. return NULL;
  812. }
  813. /*
  814. * Fix sconfig's burst size according to dw_dmac. We need to convert them as:
  815. * 1 -> 0, 4 -> 1, 8 -> 2, 16 -> 3.
  816. *
  817. * NOTE: burst size 2 is not supported by controller.
  818. *
  819. * This can be done by finding least significant bit set: n & (n - 1)
  820. */
  821. static inline void convert_burst(u32 *maxburst)
  822. {
  823. if (*maxburst > 1)
  824. *maxburst = fls(*maxburst) - 2;
  825. else
  826. *maxburst = 0;
  827. }
  828. static int dw_axi_dma_chan_slave_config(struct dma_chan *dchan,
  829. struct dma_slave_config *config)
  830. {
  831. struct axi_dma_chan *chan = dchan_to_axi_dma_chan(dchan);
  832. memcpy(&chan->config, config, sizeof(*config));
  833. if (chan->config.src_maxburst == 0)
  834. chan->config.src_maxburst = chan->config.dst_maxburst;
  835. if (chan->config.dst_maxburst == 0)
  836. chan->config.dst_maxburst = chan->config.src_maxburst;
  837. convert_burst(&chan->config.src_maxburst);
  838. convert_burst(&chan->config.dst_maxburst);
  839. return 0;
  840. }
  841. static void axi_chan_dump_lli(struct axi_dma_chan *chan,
  842. struct axi_dma_hw_desc *desc)
  843. {
  844. dev_err(dchan2dev(&chan->vc.chan),
  845. "SAR: 0x%llx DAR: 0x%llx LLP: 0x%llx BTS 0x%x CTL: 0x%x:%08x",
  846. le64_to_cpu(desc->lli->sar),
  847. le64_to_cpu(desc->lli->dar),
  848. le64_to_cpu(desc->lli->llp),
  849. le32_to_cpu(desc->lli->block_ts_lo),
  850. le32_to_cpu(desc->lli->ctl_hi),
  851. le32_to_cpu(desc->lli->ctl_lo));
  852. }
  853. static void axi_chan_list_dump_lli(struct axi_dma_chan *chan,
  854. struct axi_dma_desc *desc_head)
  855. {
  856. int count = atomic_read(&chan->descs_allocated);
  857. int i;
  858. for (i = 0; i < count; i++)
  859. axi_chan_dump_lli(chan, &desc_head->hw_desc[i]);
  860. }
  861. static noinline void axi_chan_handle_err(struct axi_dma_chan *chan, u32 status)
  862. {
  863. struct virt_dma_desc *vd;
  864. unsigned long flags;
  865. printk(KERN_ALERT "axi_chan_handle_err status=0x%x.\n", status);
  866. spin_lock_irqsave(&chan->vc.lock, flags);
  867. #ifdef DMA_GUARD_TIMER
  868. if (chan->cyclic)
  869. hrtimer_cancel(&chan->hrt);
  870. #endif
  871. axi_chan_disable(chan);
  872. /* The bad descriptor currently is in the head of vc list */
  873. vd = vchan_next_desc(&chan->vc);
  874. if (!vd) {
  875. spin_unlock_irqrestore(&chan->vc.lock, flags);
  876. return;
  877. }
  878. if (!chan->cyclic) {
  879. /* Remove the completed descriptor from issued list */
  880. list_del(&vd->node);
  881. vchan_cookie_complete(vd);
  882. }
  883. /* WARN about bad descriptor */
  884. dev_err(chan2dev(chan),
  885. "Bad descriptor submitted for %s, cookie: %d, irq: 0x%08x\n",
  886. axi_chan_name(chan), vd->tx.cookie, status);
  887. axi_chan_list_dump_lli(chan, vd_to_axi_desc(vd));
  888. if (chan->cyclic && (status & DWAXIDMAC_IRQ_INVALID_ERR)) {
  889. int count = atomic_read(&chan->descs_allocated);
  890. struct axi_dma_desc *desc;
  891. int i;
  892. desc = vd_to_axi_desc(vd);
  893. if (desc) {
  894. for (i = 0; i < count; i++)
  895. desc->hw_desc[i].lli->ctl_hi |= CH_CTL_H_LLI_VALID;
  896. }
  897. axi_chan_enable(chan);
  898. } else {
  899. /* Try to restart the controller */
  900. axi_chan_start_first_queued(chan);
  901. }
  902. spin_unlock_irqrestore(&chan->vc.lock, flags);
  903. }
  904. static void axi_chan_block_xfer_complete(struct axi_dma_chan *chan)
  905. {
  906. int count = atomic_read(&chan->descs_allocated);
  907. struct axi_dma_hw_desc *hw_desc;
  908. struct axi_dma_desc *desc;
  909. struct virt_dma_desc *vd;
  910. unsigned long flags;
  911. u64 llp;
  912. int i;
  913. spin_lock_irqsave(&chan->vc.lock, flags);
  914. /* The completed descriptor currently is in the head of vc list */
  915. vd = vchan_next_desc(&chan->vc);
  916. if (!vd) {
  917. spin_unlock_irqrestore(&chan->vc.lock, flags);
  918. return;
  919. }
  920. if (chan->cyclic) {
  921. #ifdef DMA_GUARD_TIMER
  922. ktime_t kt = ktime_set(0, DMA_GUARD_TIMER_PERIOD);
  923. hrtimer_start(&chan->hrt, kt, HRTIMER_MODE_REL);
  924. #endif
  925. desc = vd_to_axi_desc(vd);
  926. if (desc) {
  927. llp = lo_hi_readq(chan->chan_regs + CH_LLP);
  928. for (i = 0; i < count; i++) {
  929. hw_desc = &desc->hw_desc[i];
  930. hw_desc->lli->ctl_hi |= CH_CTL_H_LLI_VALID;
  931. if (llp == DWC_LLP_LOC(hw_desc->llp)) {
  932. axi_chan_irq_clear(chan, hw_desc->lli->status_lo);
  933. if (i == 0)
  934. desc->completed_blocks = count - 1;
  935. else
  936. desc->completed_blocks = i - 1;
  937. if (((hw_desc->len * (i + 1)) % desc->period_len) == 0)
  938. vchan_cyclic_callback(vd);
  939. break;
  940. }
  941. }
  942. //axi_chan_enable(chan);
  943. }
  944. } else {
  945. if (unlikely(axi_chan_is_hw_enable(chan))) {
  946. dev_err(chan2dev(chan), "BUG: %s caught DWAXIDMAC_IRQ_DMA_TRF, but channel not idle!\n",
  947. axi_chan_name(chan));
  948. axi_chan_disable(chan);
  949. }
  950. /* Remove the completed descriptor from issued list before completing */
  951. list_del(&vd->node);
  952. vchan_cookie_complete(vd);
  953. /* Submit queued descriptors after processing the completed ones */
  954. axi_chan_start_first_queued(chan);
  955. }
  956. spin_unlock_irqrestore(&chan->vc.lock, flags);
  957. }
  958. static irqreturn_t dw_axi_dma_interrupt(int irq, void *dev_id)
  959. {
  960. struct axi_dma_chip *chip = dev_id;
  961. struct dw_axi_dma *dw = chip->dw;
  962. struct axi_dma_chan *chan;
  963. u32 status, i;
  964. /* Disable DMAC inerrupts. We'll enable them after processing chanels */
  965. axi_dma_irq_disable(chip);
  966. /* Poll, clear and process every chanel interrupt status */
  967. for (i = 0; i < dw->hdata->nr_channels; i++) {
  968. chan = &dw->chan[i];
  969. status = axi_chan_irq_read(chan);
  970. axi_chan_irq_clear(chan, status);
  971. dev_vdbg(chip->dev, "%s %u IRQ status: 0x%08x\n",
  972. axi_chan_name(chan), i, status);
  973. if (status & DWAXIDMAC_IRQ_ALL_ERR)
  974. axi_chan_handle_err(chan, status);
  975. else if (status & (DWAXIDMAC_IRQ_DMA_TRF | DWAXIDMAC_IRQ_BLOCK_TRF))
  976. axi_chan_block_xfer_complete(chan);
  977. }
  978. /* Re-enable interrupts */
  979. axi_dma_irq_enable(chip);
  980. return IRQ_HANDLED;
  981. }
  982. static int dma_chan_terminate_all(struct dma_chan *dchan)
  983. {
  984. struct axi_dma_chan *chan = dchan_to_axi_dma_chan(dchan);
  985. u32 chan_active = BIT(chan->id) << DMAC_CHAN_EN_SHIFT;
  986. unsigned long flags;
  987. u32 val;
  988. int ret;
  989. LIST_HEAD(head);
  990. #ifdef DMA_GUARD_TIMER
  991. if (chan->cyclic)
  992. hrtimer_cancel(&chan->hrt);
  993. #endif
  994. axi_chan_disable(chan);
  995. ret = readl_poll_timeout_atomic(chan->chip->regs + DMAC_CHEN, val,
  996. !(val & chan_active), 1000, 10000);
  997. if (ret == -ETIMEDOUT) {
  998. dev_warn(dchan2dev(dchan),
  999. "%s failed to stop\n", axi_chan_name(chan));
  1000. axi_chan_abort(chan);
  1001. }
  1002. spin_lock_irqsave(&chan->vc.lock, flags);
  1003. vchan_get_all_descriptors(&chan->vc, &head);
  1004. chan->cyclic = false;
  1005. spin_unlock_irqrestore(&chan->vc.lock, flags);
  1006. vchan_dma_desc_free_list(&chan->vc, &head);
  1007. dev_vdbg(dchan2dev(dchan), "terminated: %s\n", axi_chan_name(chan));
  1008. return 0;
  1009. }
  1010. static int dma_chan_pause(struct dma_chan *dchan)
  1011. {
  1012. struct axi_dma_chan *chan = dchan_to_axi_dma_chan(dchan);
  1013. unsigned long flags;
  1014. unsigned int timeout = 20; /* timeout iterations */
  1015. u32 val;
  1016. spin_lock_irqsave(&chan->vc.lock, flags);
  1017. #ifdef DMA_GUARD_TIMER
  1018. if (chan->cyclic)
  1019. hrtimer_cancel(&chan->hrt);
  1020. #endif
  1021. val = axi_dma_ioread32(chan->chip, DMAC_CHEN);
  1022. val |= BIT(chan->id) << DMAC_CHAN_SUSP_SHIFT |
  1023. BIT(chan->id) << DMAC_CHAN_SUSP_WE_SHIFT;
  1024. axi_dma_iowrite32(chan->chip, DMAC_CHEN, val);
  1025. do {
  1026. if (axi_chan_irq_read(chan) & DWAXIDMAC_IRQ_SUSPENDED)
  1027. break;
  1028. udelay(2);
  1029. } while (--timeout);
  1030. axi_chan_irq_clear(chan, DWAXIDMAC_IRQ_SUSPENDED);
  1031. chan->is_paused = true;
  1032. spin_unlock_irqrestore(&chan->vc.lock, flags);
  1033. return timeout ? 0 : -EAGAIN;
  1034. }
  1035. /* Called in chan locked context */
  1036. static inline void axi_chan_resume(struct axi_dma_chan *chan)
  1037. {
  1038. u32 val;
  1039. val = axi_dma_ioread32(chan->chip, DMAC_CHEN);
  1040. val &= ~(BIT(chan->id) << DMAC_CHAN_SUSP_SHIFT);
  1041. val |= (BIT(chan->id) << DMAC_CHAN_SUSP_WE_SHIFT);
  1042. axi_dma_iowrite32(chan->chip, DMAC_CHEN, val);
  1043. chan->is_paused = false;
  1044. }
  1045. static int dma_chan_resume(struct dma_chan *dchan)
  1046. {
  1047. struct axi_dma_chan *chan = dchan_to_axi_dma_chan(dchan);
  1048. unsigned long flags;
  1049. spin_lock_irqsave(&chan->vc.lock, flags);
  1050. if (chan->is_paused)
  1051. axi_chan_resume(chan);
  1052. #ifdef DMA_GUARD_TIMER
  1053. if (chan->cyclic) {
  1054. ktime_t kt = ktime_set(0, DMA_GUARD_TIMER_PERIOD);
  1055. hrtimer_start(&chan->hrt, kt, HRTIMER_MODE_REL);
  1056. }
  1057. #endif
  1058. spin_unlock_irqrestore(&chan->vc.lock, flags);
  1059. return 0;
  1060. }
  1061. static int axi_dma_suspend(struct axi_dma_chip *chip)
  1062. {
  1063. axi_dma_irq_disable(chip);
  1064. axi_dma_disable(chip);
  1065. clk_disable_unprepare(chip->core_clk);
  1066. clk_disable_unprepare(chip->cfgr_clk);
  1067. return 0;
  1068. }
  1069. static int axi_dma_resume(struct axi_dma_chip *chip)
  1070. {
  1071. int ret;
  1072. ret = clk_prepare_enable(chip->cfgr_clk);
  1073. if (ret < 0)
  1074. return ret;
  1075. ret = clk_prepare_enable(chip->core_clk);
  1076. if (ret < 0)
  1077. return ret;
  1078. axi_dma_enable(chip);
  1079. axi_dma_irq_enable(chip);
  1080. return 0;
  1081. }
  1082. static int __maybe_unused axi_dma_runtime_suspend(struct device *dev)
  1083. {
  1084. struct axi_dma_chip *chip = dev_get_drvdata(dev);
  1085. return axi_dma_suspend(chip);
  1086. }
  1087. static int __maybe_unused axi_dma_runtime_resume(struct device *dev)
  1088. {
  1089. struct axi_dma_chip *chip = dev_get_drvdata(dev);
  1090. return axi_dma_resume(chip);
  1091. }
  1092. static struct dma_chan *dw_axi_dma_of_xlate(struct of_phandle_args *dma_spec,
  1093. struct of_dma *ofdma)
  1094. {
  1095. struct dw_axi_dma *dw = ofdma->of_dma_data;
  1096. struct axi_dma_chan *chan;
  1097. struct dma_chan *dchan;
  1098. dchan = dma_get_any_slave_channel(&dw->dma);
  1099. if (!dchan)
  1100. return NULL;
  1101. chan = dchan_to_axi_dma_chan(dchan);
  1102. chan->hw_handshake_num = dma_spec->args[0];
  1103. chan->m_master = dma_spec->args[1];
  1104. chan->p_master = dma_spec->args[2];
  1105. if (WARN_ON(chan->hw_handshake_num >= DMAC_MAX_NR_REQUESTS ||
  1106. chan->m_master >= chan->chip->dw->hdata->nr_masters ||
  1107. chan->p_master >= chan->chip->dw->hdata->nr_masters))
  1108. return NULL;
  1109. return dchan;
  1110. }
  1111. static int parse_device_properties(struct axi_dma_chip *chip)
  1112. {
  1113. struct device *dev = chip->dev;
  1114. u32 tmp, carr[DMAC_MAX_CHANNELS];
  1115. int ret;
  1116. ret = device_property_read_u32(dev, "dma-channels", &tmp);
  1117. if (ret)
  1118. return ret;
  1119. if (tmp == 0 || tmp > DMAC_MAX_CHANNELS)
  1120. return -EINVAL;
  1121. chip->dw->hdata->nr_channels = tmp;
  1122. ret = device_property_read_u32(dev, "snps,dma-masters", &tmp);
  1123. if (ret)
  1124. return ret;
  1125. if (tmp == 0 || tmp > DMAC_MAX_MASTERS)
  1126. return -EINVAL;
  1127. chip->dw->hdata->nr_masters = tmp;
  1128. ret = device_property_read_u32(dev, "snps,data-width", &tmp);
  1129. if (ret)
  1130. return ret;
  1131. if (tmp > DWAXIDMAC_TRANS_WIDTH_MAX)
  1132. return -EINVAL;
  1133. chip->dw->hdata->m_data_width = tmp;
  1134. ret = device_property_read_u32_array(dev, "snps,block-size", carr,
  1135. chip->dw->hdata->nr_channels);
  1136. if (ret)
  1137. return ret;
  1138. for (tmp = 0; tmp < chip->dw->hdata->nr_channels; tmp++) {
  1139. if (carr[tmp] == 0 || carr[tmp] > DMAC_MAX_BLK_SIZE)
  1140. return -EINVAL;
  1141. chip->dw->hdata->block_size[tmp] = carr[tmp];
  1142. }
  1143. ret = device_property_read_u32_array(dev, "snps,priority", carr,
  1144. chip->dw->hdata->nr_channels);
  1145. if (ret)
  1146. return ret;
  1147. /* Priority value must be programmed within [0:nr_channels-1] range */
  1148. for (tmp = 0; tmp < chip->dw->hdata->nr_channels; tmp++) {
  1149. if (carr[tmp] >= chip->dw->hdata->nr_channels)
  1150. return -EINVAL;
  1151. chip->dw->hdata->priority[tmp] = carr[tmp];
  1152. }
  1153. /* axi-max-burst-len is optional property */
  1154. ret = device_property_read_u32(dev, "snps,axi-max-burst-len", &tmp);
  1155. if (!ret) {
  1156. if (tmp > DWAXIDMAC_ARWLEN_MAX + 1)
  1157. return -EINVAL;
  1158. if (tmp < DWAXIDMAC_ARWLEN_MIN + 1)
  1159. return -EINVAL;
  1160. chip->dw->hdata->restrict_axi_burst_len = true;
  1161. chip->dw->hdata->axi_rw_burst_len = tmp;
  1162. }
  1163. return 0;
  1164. }
  1165. static int dw_probe(struct platform_device *pdev)
  1166. {
  1167. //struct device_node *node = pdev->dev.of_node;
  1168. struct axi_dma_chip *chip;
  1169. struct resource *mem;
  1170. struct dw_axi_dma *dw;
  1171. struct dw_axi_dma_hcfg *hdata;
  1172. u32 i;
  1173. int ret;
  1174. printk(KERN_ALERT "################dw_probe################\n");
  1175. chip = devm_kzalloc(&pdev->dev, sizeof(*chip), GFP_KERNEL);
  1176. if (!chip)
  1177. return -ENOMEM;
  1178. dw = devm_kzalloc(&pdev->dev, sizeof(*dw), GFP_KERNEL);
  1179. if (!dw)
  1180. return -ENOMEM;
  1181. hdata = devm_kzalloc(&pdev->dev, sizeof(*hdata), GFP_KERNEL);
  1182. if (!hdata)
  1183. return -ENOMEM;
  1184. chip->dw = dw;
  1185. chip->dev = &pdev->dev;
  1186. chip->dw->hdata = hdata;
  1187. chip->irq = platform_get_irq(pdev, 0);
  1188. if (chip->irq < 0)
  1189. return chip->irq;
  1190. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1191. chip->regs = devm_ioremap_resource(chip->dev, mem);
  1192. if (IS_ERR(chip->regs))
  1193. return PTR_ERR(chip->regs);
  1194. chip->core_clk = devm_clk_get(chip->dev, "core-clk");
  1195. if (IS_ERR(chip->core_clk))
  1196. return PTR_ERR(chip->core_clk);
  1197. chip->cfgr_clk = devm_clk_get(chip->dev, "cfgr-clk");
  1198. if (IS_ERR(chip->cfgr_clk))
  1199. return PTR_ERR(chip->cfgr_clk);
  1200. ret = parse_device_properties(chip);
  1201. if (ret)
  1202. return ret;
  1203. dw->chan = devm_kcalloc(chip->dev, hdata->nr_channels,
  1204. sizeof(*dw->chan), GFP_KERNEL);
  1205. if (!dw->chan)
  1206. return -ENOMEM;
  1207. ret = devm_request_irq(chip->dev, chip->irq, dw_axi_dma_interrupt,
  1208. IRQF_SHARED, KBUILD_MODNAME, chip);
  1209. if (ret)
  1210. return ret;
  1211. INIT_LIST_HEAD(&dw->dma.channels);
  1212. for (i = 0; i < hdata->nr_channels; i++) {
  1213. struct axi_dma_chan *chan = &dw->chan[i];
  1214. chan->chip = chip;
  1215. chan->id = i;
  1216. chan->chan_regs = chip->regs + COMMON_REG_LEN + i * CHAN_REG_LEN;
  1217. atomic_set(&chan->descs_allocated, 0);
  1218. chan->vc.desc_free = vchan_desc_put;
  1219. vchan_init(&chan->vc, &dw->dma);
  1220. }
  1221. /* Set capabilities */
  1222. dma_cap_set(DMA_MEMCPY, dw->dma.cap_mask);
  1223. dma_cap_set(DMA_SLAVE, dw->dma.cap_mask);
  1224. dma_cap_set(DMA_CYCLIC, dw->dma.cap_mask);
  1225. /* DMA capabilities */
  1226. dw->dma.chancnt = hdata->nr_channels;
  1227. dw->dma.max_burst = hdata->axi_rw_burst_len;
  1228. dw->dma.src_addr_widths = AXI_DMA_BUSWIDTHS;
  1229. dw->dma.dst_addr_widths = AXI_DMA_BUSWIDTHS;
  1230. dw->dma.directions = BIT(DMA_MEM_TO_MEM);
  1231. dw->dma.directions |= BIT(DMA_MEM_TO_DEV) | BIT(DMA_DEV_TO_MEM);
  1232. dw->dma.residue_granularity = DMA_RESIDUE_GRANULARITY_BURST;
  1233. dw->dma.dev = chip->dev;
  1234. dw->dma.device_tx_status = dma_chan_tx_status;
  1235. dw->dma.device_issue_pending = dma_chan_issue_pending;
  1236. dw->dma.device_terminate_all = dma_chan_terminate_all;
  1237. dw->dma.device_pause = dma_chan_pause;
  1238. dw->dma.device_resume = dma_chan_resume;
  1239. dw->dma.device_alloc_chan_resources = dma_chan_alloc_chan_resources;
  1240. dw->dma.device_free_chan_resources = dma_chan_free_chan_resources;
  1241. dw->dma.device_prep_dma_memcpy = dma_chan_prep_dma_memcpy;
  1242. dw->dma.device_synchronize = dw_axi_dma_synchronize;
  1243. dw->dma.device_config = dw_axi_dma_chan_slave_config;
  1244. dw->dma.device_prep_slave_sg = dw_axi_dma_chan_prep_slave_sg;
  1245. dw->dma.device_prep_dma_cyclic = dw_axi_dma_chan_prep_cyclic;
  1246. /*
  1247. * Synopsis DesignWare AxiDMA datasheet mentioned Maximum
  1248. * supported blocks is 1024. Device register width is 4 bytes.
  1249. * Therefore, set constraint to 1024 * 4.
  1250. */
  1251. dw->dma.dev->dma_parms = &dw->dma_parms;
  1252. dma_set_max_seg_size(&pdev->dev, MAX_BLOCK_SIZE);
  1253. platform_set_drvdata(pdev, chip);
  1254. pm_runtime_enable(chip->dev);
  1255. /*
  1256. * We can't just call pm_runtime_get here instead of
  1257. * pm_runtime_get_noresume + axi_dma_resume because we need
  1258. * driver to work also without Runtime PM.
  1259. */
  1260. pm_runtime_get_noresume(chip->dev);
  1261. ret = axi_dma_resume(chip);
  1262. if (ret < 0)
  1263. goto err_pm_disable;
  1264. axi_dma_hw_init(chip);
  1265. pm_runtime_put(chip->dev);
  1266. ret = dmaenginem_async_device_register(&dw->dma);
  1267. if (ret)
  1268. goto err_pm_disable;
  1269. /* Register with OF helpers for DMA lookups */
  1270. ret = of_dma_controller_register(pdev->dev.of_node,
  1271. dw_axi_dma_of_xlate, dw);
  1272. if (ret < 0)
  1273. dev_warn(&pdev->dev,
  1274. "Failed to register OF DMA controller, fallback to MEM_TO_MEM mode\n");
  1275. dev_info(chip->dev, "DesignWare AXI DMA Controller, %d channels\n",
  1276. dw->hdata->nr_channels);
  1277. return 0;
  1278. err_pm_disable:
  1279. pm_runtime_disable(chip->dev);
  1280. return ret;
  1281. }
  1282. static int dw_remove(struct platform_device *pdev)
  1283. {
  1284. struct axi_dma_chip *chip = platform_get_drvdata(pdev);
  1285. struct dw_axi_dma *dw = chip->dw;
  1286. struct axi_dma_chan *chan, *_chan;
  1287. u32 i;
  1288. /* Enable clk before accessing to registers */
  1289. clk_prepare_enable(chip->cfgr_clk);
  1290. clk_prepare_enable(chip->core_clk);
  1291. axi_dma_irq_disable(chip);
  1292. for (i = 0; i < dw->hdata->nr_channels; i++) {
  1293. #ifdef DMA_GUARD_TIMER
  1294. if (chip->dw->chan[i].cyclic)
  1295. hrtimer_cancel(&chip->dw->chan[i].hrt);
  1296. #endif
  1297. axi_chan_disable(&chip->dw->chan[i]);
  1298. axi_chan_irq_disable(&chip->dw->chan[i], DWAXIDMAC_IRQ_ALL);
  1299. }
  1300. axi_dma_disable(chip);
  1301. pm_runtime_disable(chip->dev);
  1302. axi_dma_suspend(chip);
  1303. devm_free_irq(chip->dev, chip->irq, chip);
  1304. of_dma_controller_free(chip->dev->of_node);
  1305. list_for_each_entry_safe(chan, _chan, &dw->dma.channels,
  1306. vc.chan.device_node) {
  1307. list_del(&chan->vc.chan.device_node);
  1308. tasklet_kill(&chan->vc.task);
  1309. }
  1310. return 0;
  1311. }
  1312. static const struct dev_pm_ops dw_axi_dma_pm_ops = {
  1313. SET_RUNTIME_PM_OPS(axi_dma_runtime_suspend, axi_dma_runtime_resume, NULL)
  1314. };
  1315. static const struct of_device_id dw_dma_of_id_table[] = {
  1316. { .compatible = "snps,axi-dma-1.01a" },
  1317. {}
  1318. };
  1319. MODULE_DEVICE_TABLE(of, dw_dma_of_id_table);
  1320. static struct platform_driver dw_driver = {
  1321. .probe = dw_probe,
  1322. .remove = dw_remove,
  1323. .driver = {
  1324. .name = KBUILD_MODNAME,
  1325. .of_match_table = dw_dma_of_id_table,
  1326. .pm = &dw_axi_dma_pm_ops,
  1327. },
  1328. };
  1329. module_platform_driver(dw_driver);
  1330. MODULE_LICENSE("GPL v2");
  1331. MODULE_DESCRIPTION("Synopsys DesignWare AXI DMA Controller platform driver");
  1332. MODULE_AUTHOR("Eugeniy Paltsev <Eugeniy.Paltsev@synopsys.com>");