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