fsl-edma.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115
  1. /*
  2. * drivers/dma/fsl-edma.c
  3. *
  4. * Copyright 2013-2014 Freescale Semiconductor, Inc.
  5. *
  6. * Driver for the Freescale eDMA engine with flexible channel multiplexing
  7. * capability for DMA request sources. The eDMA block can be found on some
  8. * Vybrid and Layerscape SoCs.
  9. *
  10. * This program is free software; you can redistribute it and/or modify it
  11. * under the terms of the GNU General Public License as published by the
  12. * Free Software Foundation; either version 2 of the License, or (at your
  13. * option) any later version.
  14. */
  15. #include <linux/init.h>
  16. #include <linux/module.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/clk.h>
  19. #include <linux/dma-mapping.h>
  20. #include <linux/dmapool.h>
  21. #include <linux/slab.h>
  22. #include <linux/spinlock.h>
  23. #include <linux/of.h>
  24. #include <linux/of_device.h>
  25. #include <linux/of_address.h>
  26. #include <linux/of_irq.h>
  27. #include <linux/of_dma.h>
  28. #include "virt-dma.h"
  29. #define EDMA_CR 0x00
  30. #define EDMA_ES 0x04
  31. #define EDMA_ERQ 0x0C
  32. #define EDMA_EEI 0x14
  33. #define EDMA_SERQ 0x1B
  34. #define EDMA_CERQ 0x1A
  35. #define EDMA_SEEI 0x19
  36. #define EDMA_CEEI 0x18
  37. #define EDMA_CINT 0x1F
  38. #define EDMA_CERR 0x1E
  39. #define EDMA_SSRT 0x1D
  40. #define EDMA_CDNE 0x1C
  41. #define EDMA_INTR 0x24
  42. #define EDMA_ERR 0x2C
  43. #define EDMA_TCD_SADDR(x) (0x1000 + 32 * (x))
  44. #define EDMA_TCD_SOFF(x) (0x1004 + 32 * (x))
  45. #define EDMA_TCD_ATTR(x) (0x1006 + 32 * (x))
  46. #define EDMA_TCD_NBYTES(x) (0x1008 + 32 * (x))
  47. #define EDMA_TCD_SLAST(x) (0x100C + 32 * (x))
  48. #define EDMA_TCD_DADDR(x) (0x1010 + 32 * (x))
  49. #define EDMA_TCD_DOFF(x) (0x1014 + 32 * (x))
  50. #define EDMA_TCD_CITER_ELINK(x) (0x1016 + 32 * (x))
  51. #define EDMA_TCD_CITER(x) (0x1016 + 32 * (x))
  52. #define EDMA_TCD_DLAST_SGA(x) (0x1018 + 32 * (x))
  53. #define EDMA_TCD_CSR(x) (0x101C + 32 * (x))
  54. #define EDMA_TCD_BITER_ELINK(x) (0x101E + 32 * (x))
  55. #define EDMA_TCD_BITER(x) (0x101E + 32 * (x))
  56. #define EDMA_CR_EDBG BIT(1)
  57. #define EDMA_CR_ERCA BIT(2)
  58. #define EDMA_CR_ERGA BIT(3)
  59. #define EDMA_CR_HOE BIT(4)
  60. #define EDMA_CR_HALT BIT(5)
  61. #define EDMA_CR_CLM BIT(6)
  62. #define EDMA_CR_EMLM BIT(7)
  63. #define EDMA_CR_ECX BIT(16)
  64. #define EDMA_CR_CX BIT(17)
  65. #define EDMA_SEEI_SEEI(x) ((x) & 0x1F)
  66. #define EDMA_CEEI_CEEI(x) ((x) & 0x1F)
  67. #define EDMA_CINT_CINT(x) ((x) & 0x1F)
  68. #define EDMA_CERR_CERR(x) ((x) & 0x1F)
  69. #define EDMA_TCD_ATTR_DSIZE(x) (((x) & 0x0007))
  70. #define EDMA_TCD_ATTR_DMOD(x) (((x) & 0x001F) << 3)
  71. #define EDMA_TCD_ATTR_SSIZE(x) (((x) & 0x0007) << 8)
  72. #define EDMA_TCD_ATTR_SMOD(x) (((x) & 0x001F) << 11)
  73. #define EDMA_TCD_ATTR_SSIZE_8BIT (0x0000)
  74. #define EDMA_TCD_ATTR_SSIZE_16BIT (0x0100)
  75. #define EDMA_TCD_ATTR_SSIZE_32BIT (0x0200)
  76. #define EDMA_TCD_ATTR_SSIZE_64BIT (0x0300)
  77. #define EDMA_TCD_ATTR_SSIZE_32BYTE (0x0500)
  78. #define EDMA_TCD_ATTR_DSIZE_8BIT (0x0000)
  79. #define EDMA_TCD_ATTR_DSIZE_16BIT (0x0001)
  80. #define EDMA_TCD_ATTR_DSIZE_32BIT (0x0002)
  81. #define EDMA_TCD_ATTR_DSIZE_64BIT (0x0003)
  82. #define EDMA_TCD_ATTR_DSIZE_32BYTE (0x0005)
  83. #define EDMA_TCD_SOFF_SOFF(x) (x)
  84. #define EDMA_TCD_NBYTES_NBYTES(x) (x)
  85. #define EDMA_TCD_SLAST_SLAST(x) (x)
  86. #define EDMA_TCD_DADDR_DADDR(x) (x)
  87. #define EDMA_TCD_CITER_CITER(x) ((x) & 0x7FFF)
  88. #define EDMA_TCD_DOFF_DOFF(x) (x)
  89. #define EDMA_TCD_DLAST_SGA_DLAST_SGA(x) (x)
  90. #define EDMA_TCD_BITER_BITER(x) ((x) & 0x7FFF)
  91. #define EDMA_TCD_CSR_START BIT(0)
  92. #define EDMA_TCD_CSR_INT_MAJOR BIT(1)
  93. #define EDMA_TCD_CSR_INT_HALF BIT(2)
  94. #define EDMA_TCD_CSR_D_REQ BIT(3)
  95. #define EDMA_TCD_CSR_E_SG BIT(4)
  96. #define EDMA_TCD_CSR_E_LINK BIT(5)
  97. #define EDMA_TCD_CSR_ACTIVE BIT(6)
  98. #define EDMA_TCD_CSR_DONE BIT(7)
  99. #define EDMAMUX_CHCFG_DIS 0x0
  100. #define EDMAMUX_CHCFG_ENBL 0x80
  101. #define EDMAMUX_CHCFG_SOURCE(n) ((n) & 0x3F)
  102. #define DMAMUX_NR 2
  103. #define FSL_EDMA_BUSWIDTHS BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) | \
  104. BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) | \
  105. BIT(DMA_SLAVE_BUSWIDTH_4_BYTES) | \
  106. BIT(DMA_SLAVE_BUSWIDTH_8_BYTES)
  107. enum fsl_edma_pm_state {
  108. RUNNING = 0,
  109. SUSPENDED,
  110. };
  111. struct fsl_edma_hw_tcd {
  112. __le32 saddr;
  113. __le16 soff;
  114. __le16 attr;
  115. __le32 nbytes;
  116. __le32 slast;
  117. __le32 daddr;
  118. __le16 doff;
  119. __le16 citer;
  120. __le32 dlast_sga;
  121. __le16 csr;
  122. __le16 biter;
  123. };
  124. struct fsl_edma_sw_tcd {
  125. dma_addr_t ptcd;
  126. struct fsl_edma_hw_tcd *vtcd;
  127. };
  128. struct fsl_edma_slave_config {
  129. enum dma_transfer_direction dir;
  130. enum dma_slave_buswidth addr_width;
  131. u32 dev_addr;
  132. u32 burst;
  133. u32 attr;
  134. };
  135. struct fsl_edma_chan {
  136. struct virt_dma_chan vchan;
  137. enum dma_status status;
  138. enum fsl_edma_pm_state pm_state;
  139. bool idle;
  140. u32 slave_id;
  141. struct fsl_edma_engine *edma;
  142. struct fsl_edma_desc *edesc;
  143. struct fsl_edma_slave_config fsc;
  144. struct dma_pool *tcd_pool;
  145. };
  146. struct fsl_edma_desc {
  147. struct virt_dma_desc vdesc;
  148. struct fsl_edma_chan *echan;
  149. bool iscyclic;
  150. unsigned int n_tcds;
  151. struct fsl_edma_sw_tcd tcd[];
  152. };
  153. struct fsl_edma_engine {
  154. struct dma_device dma_dev;
  155. void __iomem *membase;
  156. void __iomem *muxbase[DMAMUX_NR];
  157. struct clk *muxclk[DMAMUX_NR];
  158. struct mutex fsl_edma_mutex;
  159. u32 n_chans;
  160. int txirq;
  161. int errirq;
  162. bool big_endian;
  163. struct fsl_edma_chan chans[];
  164. };
  165. /*
  166. * R/W functions for big- or little-endian registers:
  167. * The eDMA controller's endian is independent of the CPU core's endian.
  168. * For the big-endian IP module, the offset for 8-bit or 16-bit registers
  169. * should also be swapped opposite to that in little-endian IP.
  170. */
  171. static u32 edma_readl(struct fsl_edma_engine *edma, void __iomem *addr)
  172. {
  173. if (edma->big_endian)
  174. return ioread32be(addr);
  175. else
  176. return ioread32(addr);
  177. }
  178. static void edma_writeb(struct fsl_edma_engine *edma, u8 val, void __iomem *addr)
  179. {
  180. /* swap the reg offset for these in big-endian mode */
  181. if (edma->big_endian)
  182. iowrite8(val, (void __iomem *)((unsigned long)addr ^ 0x3));
  183. else
  184. iowrite8(val, addr);
  185. }
  186. static void edma_writew(struct fsl_edma_engine *edma, u16 val, void __iomem *addr)
  187. {
  188. /* swap the reg offset for these in big-endian mode */
  189. if (edma->big_endian)
  190. iowrite16be(val, (void __iomem *)((unsigned long)addr ^ 0x2));
  191. else
  192. iowrite16(val, addr);
  193. }
  194. static void edma_writel(struct fsl_edma_engine *edma, u32 val, void __iomem *addr)
  195. {
  196. if (edma->big_endian)
  197. iowrite32be(val, addr);
  198. else
  199. iowrite32(val, addr);
  200. }
  201. static struct fsl_edma_chan *to_fsl_edma_chan(struct dma_chan *chan)
  202. {
  203. return container_of(chan, struct fsl_edma_chan, vchan.chan);
  204. }
  205. static struct fsl_edma_desc *to_fsl_edma_desc(struct virt_dma_desc *vd)
  206. {
  207. return container_of(vd, struct fsl_edma_desc, vdesc);
  208. }
  209. static void fsl_edma_enable_request(struct fsl_edma_chan *fsl_chan)
  210. {
  211. void __iomem *addr = fsl_chan->edma->membase;
  212. u32 ch = fsl_chan->vchan.chan.chan_id;
  213. edma_writeb(fsl_chan->edma, EDMA_SEEI_SEEI(ch), addr + EDMA_SEEI);
  214. edma_writeb(fsl_chan->edma, ch, addr + EDMA_SERQ);
  215. }
  216. static void fsl_edma_disable_request(struct fsl_edma_chan *fsl_chan)
  217. {
  218. void __iomem *addr = fsl_chan->edma->membase;
  219. u32 ch = fsl_chan->vchan.chan.chan_id;
  220. edma_writeb(fsl_chan->edma, ch, addr + EDMA_CERQ);
  221. edma_writeb(fsl_chan->edma, EDMA_CEEI_CEEI(ch), addr + EDMA_CEEI);
  222. }
  223. static void fsl_edma_chan_mux(struct fsl_edma_chan *fsl_chan,
  224. unsigned int slot, bool enable)
  225. {
  226. u32 ch = fsl_chan->vchan.chan.chan_id;
  227. void __iomem *muxaddr;
  228. unsigned chans_per_mux, ch_off;
  229. chans_per_mux = fsl_chan->edma->n_chans / DMAMUX_NR;
  230. ch_off = fsl_chan->vchan.chan.chan_id % chans_per_mux;
  231. muxaddr = fsl_chan->edma->muxbase[ch / chans_per_mux];
  232. slot = EDMAMUX_CHCFG_SOURCE(slot);
  233. if (enable)
  234. iowrite8(EDMAMUX_CHCFG_ENBL | slot, muxaddr + ch_off);
  235. else
  236. iowrite8(EDMAMUX_CHCFG_DIS, muxaddr + ch_off);
  237. }
  238. static unsigned int fsl_edma_get_tcd_attr(enum dma_slave_buswidth addr_width)
  239. {
  240. switch (addr_width) {
  241. case 1:
  242. return EDMA_TCD_ATTR_SSIZE_8BIT | EDMA_TCD_ATTR_DSIZE_8BIT;
  243. case 2:
  244. return EDMA_TCD_ATTR_SSIZE_16BIT | EDMA_TCD_ATTR_DSIZE_16BIT;
  245. case 4:
  246. return EDMA_TCD_ATTR_SSIZE_32BIT | EDMA_TCD_ATTR_DSIZE_32BIT;
  247. case 8:
  248. return EDMA_TCD_ATTR_SSIZE_64BIT | EDMA_TCD_ATTR_DSIZE_64BIT;
  249. default:
  250. return EDMA_TCD_ATTR_SSIZE_32BIT | EDMA_TCD_ATTR_DSIZE_32BIT;
  251. }
  252. }
  253. static void fsl_edma_free_desc(struct virt_dma_desc *vdesc)
  254. {
  255. struct fsl_edma_desc *fsl_desc;
  256. int i;
  257. fsl_desc = to_fsl_edma_desc(vdesc);
  258. for (i = 0; i < fsl_desc->n_tcds; i++)
  259. dma_pool_free(fsl_desc->echan->tcd_pool, fsl_desc->tcd[i].vtcd,
  260. fsl_desc->tcd[i].ptcd);
  261. kfree(fsl_desc);
  262. }
  263. static int fsl_edma_terminate_all(struct dma_chan *chan)
  264. {
  265. struct fsl_edma_chan *fsl_chan = to_fsl_edma_chan(chan);
  266. unsigned long flags;
  267. LIST_HEAD(head);
  268. spin_lock_irqsave(&fsl_chan->vchan.lock, flags);
  269. fsl_edma_disable_request(fsl_chan);
  270. fsl_chan->edesc = NULL;
  271. fsl_chan->idle = true;
  272. vchan_get_all_descriptors(&fsl_chan->vchan, &head);
  273. spin_unlock_irqrestore(&fsl_chan->vchan.lock, flags);
  274. vchan_dma_desc_free_list(&fsl_chan->vchan, &head);
  275. return 0;
  276. }
  277. static int fsl_edma_pause(struct dma_chan *chan)
  278. {
  279. struct fsl_edma_chan *fsl_chan = to_fsl_edma_chan(chan);
  280. unsigned long flags;
  281. spin_lock_irqsave(&fsl_chan->vchan.lock, flags);
  282. if (fsl_chan->edesc) {
  283. fsl_edma_disable_request(fsl_chan);
  284. fsl_chan->status = DMA_PAUSED;
  285. fsl_chan->idle = true;
  286. }
  287. spin_unlock_irqrestore(&fsl_chan->vchan.lock, flags);
  288. return 0;
  289. }
  290. static int fsl_edma_resume(struct dma_chan *chan)
  291. {
  292. struct fsl_edma_chan *fsl_chan = to_fsl_edma_chan(chan);
  293. unsigned long flags;
  294. spin_lock_irqsave(&fsl_chan->vchan.lock, flags);
  295. if (fsl_chan->edesc) {
  296. fsl_edma_enable_request(fsl_chan);
  297. fsl_chan->status = DMA_IN_PROGRESS;
  298. fsl_chan->idle = false;
  299. }
  300. spin_unlock_irqrestore(&fsl_chan->vchan.lock, flags);
  301. return 0;
  302. }
  303. static int fsl_edma_slave_config(struct dma_chan *chan,
  304. struct dma_slave_config *cfg)
  305. {
  306. struct fsl_edma_chan *fsl_chan = to_fsl_edma_chan(chan);
  307. fsl_chan->fsc.dir = cfg->direction;
  308. if (cfg->direction == DMA_DEV_TO_MEM) {
  309. fsl_chan->fsc.dev_addr = cfg->src_addr;
  310. fsl_chan->fsc.addr_width = cfg->src_addr_width;
  311. fsl_chan->fsc.burst = cfg->src_maxburst;
  312. fsl_chan->fsc.attr = fsl_edma_get_tcd_attr(cfg->src_addr_width);
  313. } else if (cfg->direction == DMA_MEM_TO_DEV) {
  314. fsl_chan->fsc.dev_addr = cfg->dst_addr;
  315. fsl_chan->fsc.addr_width = cfg->dst_addr_width;
  316. fsl_chan->fsc.burst = cfg->dst_maxburst;
  317. fsl_chan->fsc.attr = fsl_edma_get_tcd_attr(cfg->dst_addr_width);
  318. } else {
  319. return -EINVAL;
  320. }
  321. return 0;
  322. }
  323. static size_t fsl_edma_desc_residue(struct fsl_edma_chan *fsl_chan,
  324. struct virt_dma_desc *vdesc, bool in_progress)
  325. {
  326. struct fsl_edma_desc *edesc = fsl_chan->edesc;
  327. void __iomem *addr = fsl_chan->edma->membase;
  328. u32 ch = fsl_chan->vchan.chan.chan_id;
  329. enum dma_transfer_direction dir = fsl_chan->fsc.dir;
  330. dma_addr_t cur_addr, dma_addr;
  331. size_t len, size;
  332. int i;
  333. /* calculate the total size in this desc */
  334. for (len = i = 0; i < fsl_chan->edesc->n_tcds; i++)
  335. len += le32_to_cpu(edesc->tcd[i].vtcd->nbytes)
  336. * le16_to_cpu(edesc->tcd[i].vtcd->biter);
  337. if (!in_progress)
  338. return len;
  339. if (dir == DMA_MEM_TO_DEV)
  340. cur_addr = edma_readl(fsl_chan->edma, addr + EDMA_TCD_SADDR(ch));
  341. else
  342. cur_addr = edma_readl(fsl_chan->edma, addr + EDMA_TCD_DADDR(ch));
  343. /* figure out the finished and calculate the residue */
  344. for (i = 0; i < fsl_chan->edesc->n_tcds; i++) {
  345. size = le32_to_cpu(edesc->tcd[i].vtcd->nbytes)
  346. * le16_to_cpu(edesc->tcd[i].vtcd->biter);
  347. if (dir == DMA_MEM_TO_DEV)
  348. dma_addr = le32_to_cpu(edesc->tcd[i].vtcd->saddr);
  349. else
  350. dma_addr = le32_to_cpu(edesc->tcd[i].vtcd->daddr);
  351. len -= size;
  352. if (cur_addr >= dma_addr && cur_addr < dma_addr + size) {
  353. len += dma_addr + size - cur_addr;
  354. break;
  355. }
  356. }
  357. return len;
  358. }
  359. static enum dma_status fsl_edma_tx_status(struct dma_chan *chan,
  360. dma_cookie_t cookie, struct dma_tx_state *txstate)
  361. {
  362. struct fsl_edma_chan *fsl_chan = to_fsl_edma_chan(chan);
  363. struct virt_dma_desc *vdesc;
  364. enum dma_status status;
  365. unsigned long flags;
  366. status = dma_cookie_status(chan, cookie, txstate);
  367. if (status == DMA_COMPLETE)
  368. return status;
  369. if (!txstate)
  370. return fsl_chan->status;
  371. spin_lock_irqsave(&fsl_chan->vchan.lock, flags);
  372. vdesc = vchan_find_desc(&fsl_chan->vchan, cookie);
  373. if (fsl_chan->edesc && cookie == fsl_chan->edesc->vdesc.tx.cookie)
  374. txstate->residue = fsl_edma_desc_residue(fsl_chan, vdesc, true);
  375. else if (vdesc)
  376. txstate->residue = fsl_edma_desc_residue(fsl_chan, vdesc, false);
  377. else
  378. txstate->residue = 0;
  379. spin_unlock_irqrestore(&fsl_chan->vchan.lock, flags);
  380. return fsl_chan->status;
  381. }
  382. static void fsl_edma_set_tcd_regs(struct fsl_edma_chan *fsl_chan,
  383. struct fsl_edma_hw_tcd *tcd)
  384. {
  385. struct fsl_edma_engine *edma = fsl_chan->edma;
  386. void __iomem *addr = fsl_chan->edma->membase;
  387. u32 ch = fsl_chan->vchan.chan.chan_id;
  388. /*
  389. * TCD parameters are stored in struct fsl_edma_hw_tcd in little
  390. * endian format. However, we need to load the TCD registers in
  391. * big- or little-endian obeying the eDMA engine model endian.
  392. */
  393. edma_writew(edma, 0, addr + EDMA_TCD_CSR(ch));
  394. edma_writel(edma, le32_to_cpu(tcd->saddr), addr + EDMA_TCD_SADDR(ch));
  395. edma_writel(edma, le32_to_cpu(tcd->daddr), addr + EDMA_TCD_DADDR(ch));
  396. edma_writew(edma, le16_to_cpu(tcd->attr), addr + EDMA_TCD_ATTR(ch));
  397. edma_writew(edma, le16_to_cpu(tcd->soff), addr + EDMA_TCD_SOFF(ch));
  398. edma_writel(edma, le32_to_cpu(tcd->nbytes), addr + EDMA_TCD_NBYTES(ch));
  399. edma_writel(edma, le32_to_cpu(tcd->slast), addr + EDMA_TCD_SLAST(ch));
  400. edma_writew(edma, le16_to_cpu(tcd->citer), addr + EDMA_TCD_CITER(ch));
  401. edma_writew(edma, le16_to_cpu(tcd->biter), addr + EDMA_TCD_BITER(ch));
  402. edma_writew(edma, le16_to_cpu(tcd->doff), addr + EDMA_TCD_DOFF(ch));
  403. edma_writel(edma, le32_to_cpu(tcd->dlast_sga), addr + EDMA_TCD_DLAST_SGA(ch));
  404. edma_writew(edma, le16_to_cpu(tcd->csr), addr + EDMA_TCD_CSR(ch));
  405. }
  406. static inline
  407. void fsl_edma_fill_tcd(struct fsl_edma_hw_tcd *tcd, u32 src, u32 dst,
  408. u16 attr, u16 soff, u32 nbytes, u32 slast, u16 citer,
  409. u16 biter, u16 doff, u32 dlast_sga, bool major_int,
  410. bool disable_req, bool enable_sg)
  411. {
  412. u16 csr = 0;
  413. /*
  414. * eDMA hardware SGs require the TCDs to be stored in little
  415. * endian format irrespective of the register endian model.
  416. * So we put the value in little endian in memory, waiting
  417. * for fsl_edma_set_tcd_regs doing the swap.
  418. */
  419. tcd->saddr = cpu_to_le32(src);
  420. tcd->daddr = cpu_to_le32(dst);
  421. tcd->attr = cpu_to_le16(attr);
  422. tcd->soff = cpu_to_le16(EDMA_TCD_SOFF_SOFF(soff));
  423. tcd->nbytes = cpu_to_le32(EDMA_TCD_NBYTES_NBYTES(nbytes));
  424. tcd->slast = cpu_to_le32(EDMA_TCD_SLAST_SLAST(slast));
  425. tcd->citer = cpu_to_le16(EDMA_TCD_CITER_CITER(citer));
  426. tcd->doff = cpu_to_le16(EDMA_TCD_DOFF_DOFF(doff));
  427. tcd->dlast_sga = cpu_to_le32(EDMA_TCD_DLAST_SGA_DLAST_SGA(dlast_sga));
  428. tcd->biter = cpu_to_le16(EDMA_TCD_BITER_BITER(biter));
  429. if (major_int)
  430. csr |= EDMA_TCD_CSR_INT_MAJOR;
  431. if (disable_req)
  432. csr |= EDMA_TCD_CSR_D_REQ;
  433. if (enable_sg)
  434. csr |= EDMA_TCD_CSR_E_SG;
  435. tcd->csr = cpu_to_le16(csr);
  436. }
  437. static struct fsl_edma_desc *fsl_edma_alloc_desc(struct fsl_edma_chan *fsl_chan,
  438. int sg_len)
  439. {
  440. struct fsl_edma_desc *fsl_desc;
  441. int i;
  442. fsl_desc = kzalloc(sizeof(*fsl_desc) + sizeof(struct fsl_edma_sw_tcd) * sg_len,
  443. GFP_NOWAIT);
  444. if (!fsl_desc)
  445. return NULL;
  446. fsl_desc->echan = fsl_chan;
  447. fsl_desc->n_tcds = sg_len;
  448. for (i = 0; i < sg_len; i++) {
  449. fsl_desc->tcd[i].vtcd = dma_pool_alloc(fsl_chan->tcd_pool,
  450. GFP_NOWAIT, &fsl_desc->tcd[i].ptcd);
  451. if (!fsl_desc->tcd[i].vtcd)
  452. goto err;
  453. }
  454. return fsl_desc;
  455. err:
  456. while (--i >= 0)
  457. dma_pool_free(fsl_chan->tcd_pool, fsl_desc->tcd[i].vtcd,
  458. fsl_desc->tcd[i].ptcd);
  459. kfree(fsl_desc);
  460. return NULL;
  461. }
  462. static struct dma_async_tx_descriptor *fsl_edma_prep_dma_cyclic(
  463. struct dma_chan *chan, dma_addr_t dma_addr, size_t buf_len,
  464. size_t period_len, enum dma_transfer_direction direction,
  465. unsigned long flags)
  466. {
  467. struct fsl_edma_chan *fsl_chan = to_fsl_edma_chan(chan);
  468. struct fsl_edma_desc *fsl_desc;
  469. dma_addr_t dma_buf_next;
  470. int sg_len, i;
  471. u32 src_addr, dst_addr, last_sg, nbytes;
  472. u16 soff, doff, iter;
  473. if (!is_slave_direction(fsl_chan->fsc.dir))
  474. return NULL;
  475. sg_len = buf_len / period_len;
  476. fsl_desc = fsl_edma_alloc_desc(fsl_chan, sg_len);
  477. if (!fsl_desc)
  478. return NULL;
  479. fsl_desc->iscyclic = true;
  480. dma_buf_next = dma_addr;
  481. nbytes = fsl_chan->fsc.addr_width * fsl_chan->fsc.burst;
  482. iter = period_len / nbytes;
  483. for (i = 0; i < sg_len; i++) {
  484. if (dma_buf_next >= dma_addr + buf_len)
  485. dma_buf_next = dma_addr;
  486. /* get next sg's physical address */
  487. last_sg = fsl_desc->tcd[(i + 1) % sg_len].ptcd;
  488. if (fsl_chan->fsc.dir == DMA_MEM_TO_DEV) {
  489. src_addr = dma_buf_next;
  490. dst_addr = fsl_chan->fsc.dev_addr;
  491. soff = fsl_chan->fsc.addr_width;
  492. doff = 0;
  493. } else {
  494. src_addr = fsl_chan->fsc.dev_addr;
  495. dst_addr = dma_buf_next;
  496. soff = 0;
  497. doff = fsl_chan->fsc.addr_width;
  498. }
  499. fsl_edma_fill_tcd(fsl_desc->tcd[i].vtcd, src_addr, dst_addr,
  500. fsl_chan->fsc.attr, soff, nbytes, 0, iter,
  501. iter, doff, last_sg, true, false, true);
  502. dma_buf_next += period_len;
  503. }
  504. return vchan_tx_prep(&fsl_chan->vchan, &fsl_desc->vdesc, flags);
  505. }
  506. static struct dma_async_tx_descriptor *fsl_edma_prep_slave_sg(
  507. struct dma_chan *chan, struct scatterlist *sgl,
  508. unsigned int sg_len, enum dma_transfer_direction direction,
  509. unsigned long flags, void *context)
  510. {
  511. struct fsl_edma_chan *fsl_chan = to_fsl_edma_chan(chan);
  512. struct fsl_edma_desc *fsl_desc;
  513. struct scatterlist *sg;
  514. u32 src_addr, dst_addr, last_sg, nbytes;
  515. u16 soff, doff, iter;
  516. int i;
  517. if (!is_slave_direction(fsl_chan->fsc.dir))
  518. return NULL;
  519. fsl_desc = fsl_edma_alloc_desc(fsl_chan, sg_len);
  520. if (!fsl_desc)
  521. return NULL;
  522. fsl_desc->iscyclic = false;
  523. nbytes = fsl_chan->fsc.addr_width * fsl_chan->fsc.burst;
  524. for_each_sg(sgl, sg, sg_len, i) {
  525. /* get next sg's physical address */
  526. last_sg = fsl_desc->tcd[(i + 1) % sg_len].ptcd;
  527. if (fsl_chan->fsc.dir == DMA_MEM_TO_DEV) {
  528. src_addr = sg_dma_address(sg);
  529. dst_addr = fsl_chan->fsc.dev_addr;
  530. soff = fsl_chan->fsc.addr_width;
  531. doff = 0;
  532. } else {
  533. src_addr = fsl_chan->fsc.dev_addr;
  534. dst_addr = sg_dma_address(sg);
  535. soff = 0;
  536. doff = fsl_chan->fsc.addr_width;
  537. }
  538. iter = sg_dma_len(sg) / nbytes;
  539. if (i < sg_len - 1) {
  540. last_sg = fsl_desc->tcd[(i + 1)].ptcd;
  541. fsl_edma_fill_tcd(fsl_desc->tcd[i].vtcd, src_addr,
  542. dst_addr, fsl_chan->fsc.attr, soff,
  543. nbytes, 0, iter, iter, doff, last_sg,
  544. false, false, true);
  545. } else {
  546. last_sg = 0;
  547. fsl_edma_fill_tcd(fsl_desc->tcd[i].vtcd, src_addr,
  548. dst_addr, fsl_chan->fsc.attr, soff,
  549. nbytes, 0, iter, iter, doff, last_sg,
  550. true, true, false);
  551. }
  552. }
  553. return vchan_tx_prep(&fsl_chan->vchan, &fsl_desc->vdesc, flags);
  554. }
  555. static void fsl_edma_xfer_desc(struct fsl_edma_chan *fsl_chan)
  556. {
  557. struct virt_dma_desc *vdesc;
  558. vdesc = vchan_next_desc(&fsl_chan->vchan);
  559. if (!vdesc)
  560. return;
  561. fsl_chan->edesc = to_fsl_edma_desc(vdesc);
  562. fsl_edma_set_tcd_regs(fsl_chan, fsl_chan->edesc->tcd[0].vtcd);
  563. fsl_edma_enable_request(fsl_chan);
  564. fsl_chan->status = DMA_IN_PROGRESS;
  565. fsl_chan->idle = false;
  566. }
  567. static irqreturn_t fsl_edma_tx_handler(int irq, void *dev_id)
  568. {
  569. struct fsl_edma_engine *fsl_edma = dev_id;
  570. unsigned int intr, ch;
  571. void __iomem *base_addr;
  572. struct fsl_edma_chan *fsl_chan;
  573. base_addr = fsl_edma->membase;
  574. intr = edma_readl(fsl_edma, base_addr + EDMA_INTR);
  575. if (!intr)
  576. return IRQ_NONE;
  577. for (ch = 0; ch < fsl_edma->n_chans; ch++) {
  578. if (intr & (0x1 << ch)) {
  579. edma_writeb(fsl_edma, EDMA_CINT_CINT(ch),
  580. base_addr + EDMA_CINT);
  581. fsl_chan = &fsl_edma->chans[ch];
  582. spin_lock(&fsl_chan->vchan.lock);
  583. if (!fsl_chan->edesc) {
  584. /* terminate_all called before */
  585. spin_unlock(&fsl_chan->vchan.lock);
  586. continue;
  587. }
  588. if (!fsl_chan->edesc->iscyclic) {
  589. list_del(&fsl_chan->edesc->vdesc.node);
  590. vchan_cookie_complete(&fsl_chan->edesc->vdesc);
  591. fsl_chan->edesc = NULL;
  592. fsl_chan->status = DMA_COMPLETE;
  593. fsl_chan->idle = true;
  594. } else {
  595. vchan_cyclic_callback(&fsl_chan->edesc->vdesc);
  596. }
  597. if (!fsl_chan->edesc)
  598. fsl_edma_xfer_desc(fsl_chan);
  599. spin_unlock(&fsl_chan->vchan.lock);
  600. }
  601. }
  602. return IRQ_HANDLED;
  603. }
  604. static irqreturn_t fsl_edma_err_handler(int irq, void *dev_id)
  605. {
  606. struct fsl_edma_engine *fsl_edma = dev_id;
  607. unsigned int err, ch;
  608. err = edma_readl(fsl_edma, fsl_edma->membase + EDMA_ERR);
  609. if (!err)
  610. return IRQ_NONE;
  611. for (ch = 0; ch < fsl_edma->n_chans; ch++) {
  612. if (err & (0x1 << ch)) {
  613. fsl_edma_disable_request(&fsl_edma->chans[ch]);
  614. edma_writeb(fsl_edma, EDMA_CERR_CERR(ch),
  615. fsl_edma->membase + EDMA_CERR);
  616. fsl_edma->chans[ch].status = DMA_ERROR;
  617. fsl_edma->chans[ch].idle = true;
  618. }
  619. }
  620. return IRQ_HANDLED;
  621. }
  622. static irqreturn_t fsl_edma_irq_handler(int irq, void *dev_id)
  623. {
  624. if (fsl_edma_tx_handler(irq, dev_id) == IRQ_HANDLED)
  625. return IRQ_HANDLED;
  626. return fsl_edma_err_handler(irq, dev_id);
  627. }
  628. static void fsl_edma_issue_pending(struct dma_chan *chan)
  629. {
  630. struct fsl_edma_chan *fsl_chan = to_fsl_edma_chan(chan);
  631. unsigned long flags;
  632. spin_lock_irqsave(&fsl_chan->vchan.lock, flags);
  633. if (unlikely(fsl_chan->pm_state != RUNNING)) {
  634. spin_unlock_irqrestore(&fsl_chan->vchan.lock, flags);
  635. /* cannot submit due to suspend */
  636. return;
  637. }
  638. if (vchan_issue_pending(&fsl_chan->vchan) && !fsl_chan->edesc)
  639. fsl_edma_xfer_desc(fsl_chan);
  640. spin_unlock_irqrestore(&fsl_chan->vchan.lock, flags);
  641. }
  642. static struct dma_chan *fsl_edma_xlate(struct of_phandle_args *dma_spec,
  643. struct of_dma *ofdma)
  644. {
  645. struct fsl_edma_engine *fsl_edma = ofdma->of_dma_data;
  646. struct dma_chan *chan, *_chan;
  647. struct fsl_edma_chan *fsl_chan;
  648. unsigned long chans_per_mux = fsl_edma->n_chans / DMAMUX_NR;
  649. if (dma_spec->args_count != 2)
  650. return NULL;
  651. mutex_lock(&fsl_edma->fsl_edma_mutex);
  652. list_for_each_entry_safe(chan, _chan, &fsl_edma->dma_dev.channels, device_node) {
  653. if (chan->client_count)
  654. continue;
  655. if ((chan->chan_id / chans_per_mux) == dma_spec->args[0]) {
  656. chan = dma_get_slave_channel(chan);
  657. if (chan) {
  658. chan->device->privatecnt++;
  659. fsl_chan = to_fsl_edma_chan(chan);
  660. fsl_chan->slave_id = dma_spec->args[1];
  661. fsl_edma_chan_mux(fsl_chan, fsl_chan->slave_id,
  662. true);
  663. mutex_unlock(&fsl_edma->fsl_edma_mutex);
  664. return chan;
  665. }
  666. }
  667. }
  668. mutex_unlock(&fsl_edma->fsl_edma_mutex);
  669. return NULL;
  670. }
  671. static int fsl_edma_alloc_chan_resources(struct dma_chan *chan)
  672. {
  673. struct fsl_edma_chan *fsl_chan = to_fsl_edma_chan(chan);
  674. fsl_chan->tcd_pool = dma_pool_create("tcd_pool", chan->device->dev,
  675. sizeof(struct fsl_edma_hw_tcd),
  676. 32, 0);
  677. return 0;
  678. }
  679. static void fsl_edma_free_chan_resources(struct dma_chan *chan)
  680. {
  681. struct fsl_edma_chan *fsl_chan = to_fsl_edma_chan(chan);
  682. unsigned long flags;
  683. LIST_HEAD(head);
  684. spin_lock_irqsave(&fsl_chan->vchan.lock, flags);
  685. fsl_edma_disable_request(fsl_chan);
  686. fsl_edma_chan_mux(fsl_chan, 0, false);
  687. fsl_chan->edesc = NULL;
  688. vchan_get_all_descriptors(&fsl_chan->vchan, &head);
  689. spin_unlock_irqrestore(&fsl_chan->vchan.lock, flags);
  690. vchan_dma_desc_free_list(&fsl_chan->vchan, &head);
  691. dma_pool_destroy(fsl_chan->tcd_pool);
  692. fsl_chan->tcd_pool = NULL;
  693. }
  694. static int
  695. fsl_edma_irq_init(struct platform_device *pdev, struct fsl_edma_engine *fsl_edma)
  696. {
  697. int ret;
  698. fsl_edma->txirq = platform_get_irq_byname(pdev, "edma-tx");
  699. if (fsl_edma->txirq < 0) {
  700. dev_err(&pdev->dev, "Can't get edma-tx irq.\n");
  701. return fsl_edma->txirq;
  702. }
  703. fsl_edma->errirq = platform_get_irq_byname(pdev, "edma-err");
  704. if (fsl_edma->errirq < 0) {
  705. dev_err(&pdev->dev, "Can't get edma-err irq.\n");
  706. return fsl_edma->errirq;
  707. }
  708. if (fsl_edma->txirq == fsl_edma->errirq) {
  709. ret = devm_request_irq(&pdev->dev, fsl_edma->txirq,
  710. fsl_edma_irq_handler, 0, "eDMA", fsl_edma);
  711. if (ret) {
  712. dev_err(&pdev->dev, "Can't register eDMA IRQ.\n");
  713. return ret;
  714. }
  715. } else {
  716. ret = devm_request_irq(&pdev->dev, fsl_edma->txirq,
  717. fsl_edma_tx_handler, 0, "eDMA tx", fsl_edma);
  718. if (ret) {
  719. dev_err(&pdev->dev, "Can't register eDMA tx IRQ.\n");
  720. return ret;
  721. }
  722. ret = devm_request_irq(&pdev->dev, fsl_edma->errirq,
  723. fsl_edma_err_handler, 0, "eDMA err", fsl_edma);
  724. if (ret) {
  725. dev_err(&pdev->dev, "Can't register eDMA err IRQ.\n");
  726. return ret;
  727. }
  728. }
  729. return 0;
  730. }
  731. static void fsl_edma_irq_exit(
  732. struct platform_device *pdev, struct fsl_edma_engine *fsl_edma)
  733. {
  734. if (fsl_edma->txirq == fsl_edma->errirq) {
  735. devm_free_irq(&pdev->dev, fsl_edma->txirq, fsl_edma);
  736. } else {
  737. devm_free_irq(&pdev->dev, fsl_edma->txirq, fsl_edma);
  738. devm_free_irq(&pdev->dev, fsl_edma->errirq, fsl_edma);
  739. }
  740. }
  741. static void fsl_disable_clocks(struct fsl_edma_engine *fsl_edma, int nr_clocks)
  742. {
  743. int i;
  744. for (i = 0; i < nr_clocks; i++)
  745. clk_disable_unprepare(fsl_edma->muxclk[i]);
  746. }
  747. static int fsl_edma_probe(struct platform_device *pdev)
  748. {
  749. struct device_node *np = pdev->dev.of_node;
  750. struct fsl_edma_engine *fsl_edma;
  751. struct fsl_edma_chan *fsl_chan;
  752. struct resource *res;
  753. int len, chans;
  754. int ret, i;
  755. ret = of_property_read_u32(np, "dma-channels", &chans);
  756. if (ret) {
  757. dev_err(&pdev->dev, "Can't get dma-channels.\n");
  758. return ret;
  759. }
  760. len = sizeof(*fsl_edma) + sizeof(*fsl_chan) * chans;
  761. fsl_edma = devm_kzalloc(&pdev->dev, len, GFP_KERNEL);
  762. if (!fsl_edma)
  763. return -ENOMEM;
  764. fsl_edma->n_chans = chans;
  765. mutex_init(&fsl_edma->fsl_edma_mutex);
  766. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  767. fsl_edma->membase = devm_ioremap_resource(&pdev->dev, res);
  768. if (IS_ERR(fsl_edma->membase))
  769. return PTR_ERR(fsl_edma->membase);
  770. for (i = 0; i < DMAMUX_NR; i++) {
  771. char clkname[32];
  772. res = platform_get_resource(pdev, IORESOURCE_MEM, 1 + i);
  773. fsl_edma->muxbase[i] = devm_ioremap_resource(&pdev->dev, res);
  774. if (IS_ERR(fsl_edma->muxbase[i])) {
  775. /* on error: disable all previously enabled clks */
  776. fsl_disable_clocks(fsl_edma, i);
  777. return PTR_ERR(fsl_edma->muxbase[i]);
  778. }
  779. sprintf(clkname, "dmamux%d", i);
  780. fsl_edma->muxclk[i] = devm_clk_get(&pdev->dev, clkname);
  781. if (IS_ERR(fsl_edma->muxclk[i])) {
  782. dev_err(&pdev->dev, "Missing DMAMUX block clock.\n");
  783. /* on error: disable all previously enabled clks */
  784. fsl_disable_clocks(fsl_edma, i);
  785. return PTR_ERR(fsl_edma->muxclk[i]);
  786. }
  787. ret = clk_prepare_enable(fsl_edma->muxclk[i]);
  788. if (ret)
  789. /* on error: disable all previously enabled clks */
  790. fsl_disable_clocks(fsl_edma, i);
  791. }
  792. fsl_edma->big_endian = of_property_read_bool(np, "big-endian");
  793. INIT_LIST_HEAD(&fsl_edma->dma_dev.channels);
  794. for (i = 0; i < fsl_edma->n_chans; i++) {
  795. struct fsl_edma_chan *fsl_chan = &fsl_edma->chans[i];
  796. fsl_chan->edma = fsl_edma;
  797. fsl_chan->pm_state = RUNNING;
  798. fsl_chan->slave_id = 0;
  799. fsl_chan->idle = true;
  800. fsl_chan->vchan.desc_free = fsl_edma_free_desc;
  801. vchan_init(&fsl_chan->vchan, &fsl_edma->dma_dev);
  802. edma_writew(fsl_edma, 0x0, fsl_edma->membase + EDMA_TCD_CSR(i));
  803. fsl_edma_chan_mux(fsl_chan, 0, false);
  804. }
  805. edma_writel(fsl_edma, ~0, fsl_edma->membase + EDMA_INTR);
  806. ret = fsl_edma_irq_init(pdev, fsl_edma);
  807. if (ret)
  808. return ret;
  809. dma_cap_set(DMA_PRIVATE, fsl_edma->dma_dev.cap_mask);
  810. dma_cap_set(DMA_SLAVE, fsl_edma->dma_dev.cap_mask);
  811. dma_cap_set(DMA_CYCLIC, fsl_edma->dma_dev.cap_mask);
  812. fsl_edma->dma_dev.dev = &pdev->dev;
  813. fsl_edma->dma_dev.device_alloc_chan_resources
  814. = fsl_edma_alloc_chan_resources;
  815. fsl_edma->dma_dev.device_free_chan_resources
  816. = fsl_edma_free_chan_resources;
  817. fsl_edma->dma_dev.device_tx_status = fsl_edma_tx_status;
  818. fsl_edma->dma_dev.device_prep_slave_sg = fsl_edma_prep_slave_sg;
  819. fsl_edma->dma_dev.device_prep_dma_cyclic = fsl_edma_prep_dma_cyclic;
  820. fsl_edma->dma_dev.device_config = fsl_edma_slave_config;
  821. fsl_edma->dma_dev.device_pause = fsl_edma_pause;
  822. fsl_edma->dma_dev.device_resume = fsl_edma_resume;
  823. fsl_edma->dma_dev.device_terminate_all = fsl_edma_terminate_all;
  824. fsl_edma->dma_dev.device_issue_pending = fsl_edma_issue_pending;
  825. fsl_edma->dma_dev.src_addr_widths = FSL_EDMA_BUSWIDTHS;
  826. fsl_edma->dma_dev.dst_addr_widths = FSL_EDMA_BUSWIDTHS;
  827. fsl_edma->dma_dev.directions = BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV);
  828. platform_set_drvdata(pdev, fsl_edma);
  829. ret = dma_async_device_register(&fsl_edma->dma_dev);
  830. if (ret) {
  831. dev_err(&pdev->dev,
  832. "Can't register Freescale eDMA engine. (%d)\n", ret);
  833. fsl_disable_clocks(fsl_edma, DMAMUX_NR);
  834. return ret;
  835. }
  836. ret = of_dma_controller_register(np, fsl_edma_xlate, fsl_edma);
  837. if (ret) {
  838. dev_err(&pdev->dev,
  839. "Can't register Freescale eDMA of_dma. (%d)\n", ret);
  840. dma_async_device_unregister(&fsl_edma->dma_dev);
  841. fsl_disable_clocks(fsl_edma, DMAMUX_NR);
  842. return ret;
  843. }
  844. /* enable round robin arbitration */
  845. edma_writel(fsl_edma, EDMA_CR_ERGA | EDMA_CR_ERCA, fsl_edma->membase + EDMA_CR);
  846. return 0;
  847. }
  848. static void fsl_edma_cleanup_vchan(struct dma_device *dmadev)
  849. {
  850. struct fsl_edma_chan *chan, *_chan;
  851. list_for_each_entry_safe(chan, _chan,
  852. &dmadev->channels, vchan.chan.device_node) {
  853. list_del(&chan->vchan.chan.device_node);
  854. tasklet_kill(&chan->vchan.task);
  855. }
  856. }
  857. static int fsl_edma_remove(struct platform_device *pdev)
  858. {
  859. struct device_node *np = pdev->dev.of_node;
  860. struct fsl_edma_engine *fsl_edma = platform_get_drvdata(pdev);
  861. fsl_edma_irq_exit(pdev, fsl_edma);
  862. fsl_edma_cleanup_vchan(&fsl_edma->dma_dev);
  863. of_dma_controller_free(np);
  864. dma_async_device_unregister(&fsl_edma->dma_dev);
  865. fsl_disable_clocks(fsl_edma, DMAMUX_NR);
  866. return 0;
  867. }
  868. static int fsl_edma_suspend_late(struct device *dev)
  869. {
  870. struct fsl_edma_engine *fsl_edma = dev_get_drvdata(dev);
  871. struct fsl_edma_chan *fsl_chan;
  872. unsigned long flags;
  873. int i;
  874. for (i = 0; i < fsl_edma->n_chans; i++) {
  875. fsl_chan = &fsl_edma->chans[i];
  876. spin_lock_irqsave(&fsl_chan->vchan.lock, flags);
  877. /* Make sure chan is idle or will force disable. */
  878. if (unlikely(!fsl_chan->idle)) {
  879. dev_warn(dev, "WARN: There is non-idle channel.");
  880. fsl_edma_disable_request(fsl_chan);
  881. fsl_edma_chan_mux(fsl_chan, 0, false);
  882. }
  883. fsl_chan->pm_state = SUSPENDED;
  884. spin_unlock_irqrestore(&fsl_chan->vchan.lock, flags);
  885. }
  886. return 0;
  887. }
  888. static int fsl_edma_resume_early(struct device *dev)
  889. {
  890. struct fsl_edma_engine *fsl_edma = dev_get_drvdata(dev);
  891. struct fsl_edma_chan *fsl_chan;
  892. int i;
  893. for (i = 0; i < fsl_edma->n_chans; i++) {
  894. fsl_chan = &fsl_edma->chans[i];
  895. fsl_chan->pm_state = RUNNING;
  896. edma_writew(fsl_edma, 0x0, fsl_edma->membase + EDMA_TCD_CSR(i));
  897. if (fsl_chan->slave_id != 0)
  898. fsl_edma_chan_mux(fsl_chan, fsl_chan->slave_id, true);
  899. }
  900. edma_writel(fsl_edma, EDMA_CR_ERGA | EDMA_CR_ERCA,
  901. fsl_edma->membase + EDMA_CR);
  902. return 0;
  903. }
  904. /*
  905. * eDMA provides the service to others, so it should be suspend late
  906. * and resume early. When eDMA suspend, all of the clients should stop
  907. * the DMA data transmission and let the channel idle.
  908. */
  909. static const struct dev_pm_ops fsl_edma_pm_ops = {
  910. .suspend_late = fsl_edma_suspend_late,
  911. .resume_early = fsl_edma_resume_early,
  912. };
  913. static const struct of_device_id fsl_edma_dt_ids[] = {
  914. { .compatible = "fsl,vf610-edma", },
  915. { /* sentinel */ }
  916. };
  917. MODULE_DEVICE_TABLE(of, fsl_edma_dt_ids);
  918. static struct platform_driver fsl_edma_driver = {
  919. .driver = {
  920. .name = "fsl-edma",
  921. .of_match_table = fsl_edma_dt_ids,
  922. .pm = &fsl_edma_pm_ops,
  923. },
  924. .probe = fsl_edma_probe,
  925. .remove = fsl_edma_remove,
  926. };
  927. static int __init fsl_edma_init(void)
  928. {
  929. return platform_driver_register(&fsl_edma_driver);
  930. }
  931. subsys_initcall(fsl_edma_init);
  932. static void __exit fsl_edma_exit(void)
  933. {
  934. platform_driver_unregister(&fsl_edma_driver);
  935. }
  936. module_exit(fsl_edma_exit);
  937. MODULE_ALIAS("platform:fsl-edma");
  938. MODULE_DESCRIPTION("Freescale eDMA engine driver");
  939. MODULE_LICENSE("GPL v2");