fsl-edma.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108
  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->iscyclic) {
  584. list_del(&fsl_chan->edesc->vdesc.node);
  585. vchan_cookie_complete(&fsl_chan->edesc->vdesc);
  586. fsl_chan->edesc = NULL;
  587. fsl_chan->status = DMA_COMPLETE;
  588. fsl_chan->idle = true;
  589. } else {
  590. vchan_cyclic_callback(&fsl_chan->edesc->vdesc);
  591. }
  592. if (!fsl_chan->edesc)
  593. fsl_edma_xfer_desc(fsl_chan);
  594. spin_unlock(&fsl_chan->vchan.lock);
  595. }
  596. }
  597. return IRQ_HANDLED;
  598. }
  599. static irqreturn_t fsl_edma_err_handler(int irq, void *dev_id)
  600. {
  601. struct fsl_edma_engine *fsl_edma = dev_id;
  602. unsigned int err, ch;
  603. err = edma_readl(fsl_edma, fsl_edma->membase + EDMA_ERR);
  604. if (!err)
  605. return IRQ_NONE;
  606. for (ch = 0; ch < fsl_edma->n_chans; ch++) {
  607. if (err & (0x1 << ch)) {
  608. fsl_edma_disable_request(&fsl_edma->chans[ch]);
  609. edma_writeb(fsl_edma, EDMA_CERR_CERR(ch),
  610. fsl_edma->membase + EDMA_CERR);
  611. fsl_edma->chans[ch].status = DMA_ERROR;
  612. fsl_edma->chans[ch].idle = true;
  613. }
  614. }
  615. return IRQ_HANDLED;
  616. }
  617. static irqreturn_t fsl_edma_irq_handler(int irq, void *dev_id)
  618. {
  619. if (fsl_edma_tx_handler(irq, dev_id) == IRQ_HANDLED)
  620. return IRQ_HANDLED;
  621. return fsl_edma_err_handler(irq, dev_id);
  622. }
  623. static void fsl_edma_issue_pending(struct dma_chan *chan)
  624. {
  625. struct fsl_edma_chan *fsl_chan = to_fsl_edma_chan(chan);
  626. unsigned long flags;
  627. spin_lock_irqsave(&fsl_chan->vchan.lock, flags);
  628. if (unlikely(fsl_chan->pm_state != RUNNING)) {
  629. spin_unlock_irqrestore(&fsl_chan->vchan.lock, flags);
  630. /* cannot submit due to suspend */
  631. return;
  632. }
  633. if (vchan_issue_pending(&fsl_chan->vchan) && !fsl_chan->edesc)
  634. fsl_edma_xfer_desc(fsl_chan);
  635. spin_unlock_irqrestore(&fsl_chan->vchan.lock, flags);
  636. }
  637. static struct dma_chan *fsl_edma_xlate(struct of_phandle_args *dma_spec,
  638. struct of_dma *ofdma)
  639. {
  640. struct fsl_edma_engine *fsl_edma = ofdma->of_dma_data;
  641. struct dma_chan *chan, *_chan;
  642. struct fsl_edma_chan *fsl_chan;
  643. unsigned long chans_per_mux = fsl_edma->n_chans / DMAMUX_NR;
  644. if (dma_spec->args_count != 2)
  645. return NULL;
  646. mutex_lock(&fsl_edma->fsl_edma_mutex);
  647. list_for_each_entry_safe(chan, _chan, &fsl_edma->dma_dev.channels, device_node) {
  648. if (chan->client_count)
  649. continue;
  650. if ((chan->chan_id / chans_per_mux) == dma_spec->args[0]) {
  651. chan = dma_get_slave_channel(chan);
  652. if (chan) {
  653. chan->device->privatecnt++;
  654. fsl_chan = to_fsl_edma_chan(chan);
  655. fsl_chan->slave_id = dma_spec->args[1];
  656. fsl_edma_chan_mux(fsl_chan, fsl_chan->slave_id,
  657. true);
  658. mutex_unlock(&fsl_edma->fsl_edma_mutex);
  659. return chan;
  660. }
  661. }
  662. }
  663. mutex_unlock(&fsl_edma->fsl_edma_mutex);
  664. return NULL;
  665. }
  666. static int fsl_edma_alloc_chan_resources(struct dma_chan *chan)
  667. {
  668. struct fsl_edma_chan *fsl_chan = to_fsl_edma_chan(chan);
  669. fsl_chan->tcd_pool = dma_pool_create("tcd_pool", chan->device->dev,
  670. sizeof(struct fsl_edma_hw_tcd),
  671. 32, 0);
  672. return 0;
  673. }
  674. static void fsl_edma_free_chan_resources(struct dma_chan *chan)
  675. {
  676. struct fsl_edma_chan *fsl_chan = to_fsl_edma_chan(chan);
  677. unsigned long flags;
  678. LIST_HEAD(head);
  679. spin_lock_irqsave(&fsl_chan->vchan.lock, flags);
  680. fsl_edma_disable_request(fsl_chan);
  681. fsl_edma_chan_mux(fsl_chan, 0, false);
  682. fsl_chan->edesc = NULL;
  683. vchan_get_all_descriptors(&fsl_chan->vchan, &head);
  684. spin_unlock_irqrestore(&fsl_chan->vchan.lock, flags);
  685. vchan_dma_desc_free_list(&fsl_chan->vchan, &head);
  686. dma_pool_destroy(fsl_chan->tcd_pool);
  687. fsl_chan->tcd_pool = NULL;
  688. }
  689. static int
  690. fsl_edma_irq_init(struct platform_device *pdev, struct fsl_edma_engine *fsl_edma)
  691. {
  692. int ret;
  693. fsl_edma->txirq = platform_get_irq_byname(pdev, "edma-tx");
  694. if (fsl_edma->txirq < 0) {
  695. dev_err(&pdev->dev, "Can't get edma-tx irq.\n");
  696. return fsl_edma->txirq;
  697. }
  698. fsl_edma->errirq = platform_get_irq_byname(pdev, "edma-err");
  699. if (fsl_edma->errirq < 0) {
  700. dev_err(&pdev->dev, "Can't get edma-err irq.\n");
  701. return fsl_edma->errirq;
  702. }
  703. if (fsl_edma->txirq == fsl_edma->errirq) {
  704. ret = devm_request_irq(&pdev->dev, fsl_edma->txirq,
  705. fsl_edma_irq_handler, 0, "eDMA", fsl_edma);
  706. if (ret) {
  707. dev_err(&pdev->dev, "Can't register eDMA IRQ.\n");
  708. return ret;
  709. }
  710. } else {
  711. ret = devm_request_irq(&pdev->dev, fsl_edma->txirq,
  712. fsl_edma_tx_handler, 0, "eDMA tx", fsl_edma);
  713. if (ret) {
  714. dev_err(&pdev->dev, "Can't register eDMA tx IRQ.\n");
  715. return ret;
  716. }
  717. ret = devm_request_irq(&pdev->dev, fsl_edma->errirq,
  718. fsl_edma_err_handler, 0, "eDMA err", fsl_edma);
  719. if (ret) {
  720. dev_err(&pdev->dev, "Can't register eDMA err IRQ.\n");
  721. return ret;
  722. }
  723. }
  724. return 0;
  725. }
  726. static void fsl_edma_irq_exit(
  727. struct platform_device *pdev, struct fsl_edma_engine *fsl_edma)
  728. {
  729. if (fsl_edma->txirq == fsl_edma->errirq) {
  730. devm_free_irq(&pdev->dev, fsl_edma->txirq, fsl_edma);
  731. } else {
  732. devm_free_irq(&pdev->dev, fsl_edma->txirq, fsl_edma);
  733. devm_free_irq(&pdev->dev, fsl_edma->errirq, fsl_edma);
  734. }
  735. }
  736. static void fsl_disable_clocks(struct fsl_edma_engine *fsl_edma, int nr_clocks)
  737. {
  738. int i;
  739. for (i = 0; i < nr_clocks; i++)
  740. clk_disable_unprepare(fsl_edma->muxclk[i]);
  741. }
  742. static int fsl_edma_probe(struct platform_device *pdev)
  743. {
  744. struct device_node *np = pdev->dev.of_node;
  745. struct fsl_edma_engine *fsl_edma;
  746. struct fsl_edma_chan *fsl_chan;
  747. struct resource *res;
  748. int len, chans;
  749. int ret, i;
  750. ret = of_property_read_u32(np, "dma-channels", &chans);
  751. if (ret) {
  752. dev_err(&pdev->dev, "Can't get dma-channels.\n");
  753. return ret;
  754. }
  755. len = sizeof(*fsl_edma) + sizeof(*fsl_chan) * chans;
  756. fsl_edma = devm_kzalloc(&pdev->dev, len, GFP_KERNEL);
  757. if (!fsl_edma)
  758. return -ENOMEM;
  759. fsl_edma->n_chans = chans;
  760. mutex_init(&fsl_edma->fsl_edma_mutex);
  761. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  762. fsl_edma->membase = devm_ioremap_resource(&pdev->dev, res);
  763. if (IS_ERR(fsl_edma->membase))
  764. return PTR_ERR(fsl_edma->membase);
  765. for (i = 0; i < DMAMUX_NR; i++) {
  766. char clkname[32];
  767. res = platform_get_resource(pdev, IORESOURCE_MEM, 1 + i);
  768. fsl_edma->muxbase[i] = devm_ioremap_resource(&pdev->dev, res);
  769. if (IS_ERR(fsl_edma->muxbase[i])) {
  770. /* on error: disable all previously enabled clks */
  771. fsl_disable_clocks(fsl_edma, i);
  772. return PTR_ERR(fsl_edma->muxbase[i]);
  773. }
  774. sprintf(clkname, "dmamux%d", i);
  775. fsl_edma->muxclk[i] = devm_clk_get(&pdev->dev, clkname);
  776. if (IS_ERR(fsl_edma->muxclk[i])) {
  777. dev_err(&pdev->dev, "Missing DMAMUX block clock.\n");
  778. /* on error: disable all previously enabled clks */
  779. fsl_disable_clocks(fsl_edma, i);
  780. return PTR_ERR(fsl_edma->muxclk[i]);
  781. }
  782. ret = clk_prepare_enable(fsl_edma->muxclk[i]);
  783. if (ret)
  784. /* on error: disable all previously enabled clks */
  785. fsl_disable_clocks(fsl_edma, i);
  786. }
  787. fsl_edma->big_endian = of_property_read_bool(np, "big-endian");
  788. INIT_LIST_HEAD(&fsl_edma->dma_dev.channels);
  789. for (i = 0; i < fsl_edma->n_chans; i++) {
  790. struct fsl_edma_chan *fsl_chan = &fsl_edma->chans[i];
  791. fsl_chan->edma = fsl_edma;
  792. fsl_chan->pm_state = RUNNING;
  793. fsl_chan->slave_id = 0;
  794. fsl_chan->idle = true;
  795. fsl_chan->vchan.desc_free = fsl_edma_free_desc;
  796. vchan_init(&fsl_chan->vchan, &fsl_edma->dma_dev);
  797. edma_writew(fsl_edma, 0x0, fsl_edma->membase + EDMA_TCD_CSR(i));
  798. fsl_edma_chan_mux(fsl_chan, 0, false);
  799. }
  800. edma_writel(fsl_edma, ~0, fsl_edma->membase + EDMA_INTR);
  801. ret = fsl_edma_irq_init(pdev, fsl_edma);
  802. if (ret)
  803. return ret;
  804. dma_cap_set(DMA_PRIVATE, fsl_edma->dma_dev.cap_mask);
  805. dma_cap_set(DMA_SLAVE, fsl_edma->dma_dev.cap_mask);
  806. dma_cap_set(DMA_CYCLIC, fsl_edma->dma_dev.cap_mask);
  807. fsl_edma->dma_dev.dev = &pdev->dev;
  808. fsl_edma->dma_dev.device_alloc_chan_resources
  809. = fsl_edma_alloc_chan_resources;
  810. fsl_edma->dma_dev.device_free_chan_resources
  811. = fsl_edma_free_chan_resources;
  812. fsl_edma->dma_dev.device_tx_status = fsl_edma_tx_status;
  813. fsl_edma->dma_dev.device_prep_slave_sg = fsl_edma_prep_slave_sg;
  814. fsl_edma->dma_dev.device_prep_dma_cyclic = fsl_edma_prep_dma_cyclic;
  815. fsl_edma->dma_dev.device_config = fsl_edma_slave_config;
  816. fsl_edma->dma_dev.device_pause = fsl_edma_pause;
  817. fsl_edma->dma_dev.device_resume = fsl_edma_resume;
  818. fsl_edma->dma_dev.device_terminate_all = fsl_edma_terminate_all;
  819. fsl_edma->dma_dev.device_issue_pending = fsl_edma_issue_pending;
  820. fsl_edma->dma_dev.src_addr_widths = FSL_EDMA_BUSWIDTHS;
  821. fsl_edma->dma_dev.dst_addr_widths = FSL_EDMA_BUSWIDTHS;
  822. fsl_edma->dma_dev.directions = BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV);
  823. platform_set_drvdata(pdev, fsl_edma);
  824. ret = dma_async_device_register(&fsl_edma->dma_dev);
  825. if (ret) {
  826. dev_err(&pdev->dev,
  827. "Can't register Freescale eDMA engine. (%d)\n", ret);
  828. fsl_disable_clocks(fsl_edma, DMAMUX_NR);
  829. return ret;
  830. }
  831. ret = of_dma_controller_register(np, fsl_edma_xlate, fsl_edma);
  832. if (ret) {
  833. dev_err(&pdev->dev,
  834. "Can't register Freescale eDMA of_dma. (%d)\n", ret);
  835. dma_async_device_unregister(&fsl_edma->dma_dev);
  836. fsl_disable_clocks(fsl_edma, DMAMUX_NR);
  837. return ret;
  838. }
  839. /* enable round robin arbitration */
  840. edma_writel(fsl_edma, EDMA_CR_ERGA | EDMA_CR_ERCA, fsl_edma->membase + EDMA_CR);
  841. return 0;
  842. }
  843. static void fsl_edma_cleanup_vchan(struct dma_device *dmadev)
  844. {
  845. struct fsl_edma_chan *chan, *_chan;
  846. list_for_each_entry_safe(chan, _chan,
  847. &dmadev->channels, vchan.chan.device_node) {
  848. list_del(&chan->vchan.chan.device_node);
  849. tasklet_kill(&chan->vchan.task);
  850. }
  851. }
  852. static int fsl_edma_remove(struct platform_device *pdev)
  853. {
  854. struct device_node *np = pdev->dev.of_node;
  855. struct fsl_edma_engine *fsl_edma = platform_get_drvdata(pdev);
  856. fsl_edma_irq_exit(pdev, fsl_edma);
  857. fsl_edma_cleanup_vchan(&fsl_edma->dma_dev);
  858. of_dma_controller_free(np);
  859. dma_async_device_unregister(&fsl_edma->dma_dev);
  860. fsl_disable_clocks(fsl_edma, DMAMUX_NR);
  861. return 0;
  862. }
  863. static int fsl_edma_suspend_late(struct device *dev)
  864. {
  865. struct fsl_edma_engine *fsl_edma = dev_get_drvdata(dev);
  866. struct fsl_edma_chan *fsl_chan;
  867. unsigned long flags;
  868. int i;
  869. for (i = 0; i < fsl_edma->n_chans; i++) {
  870. fsl_chan = &fsl_edma->chans[i];
  871. spin_lock_irqsave(&fsl_chan->vchan.lock, flags);
  872. /* Make sure chan is idle or will force disable. */
  873. if (unlikely(!fsl_chan->idle)) {
  874. dev_warn(dev, "WARN: There is non-idle channel.");
  875. fsl_edma_disable_request(fsl_chan);
  876. fsl_edma_chan_mux(fsl_chan, 0, false);
  877. }
  878. fsl_chan->pm_state = SUSPENDED;
  879. spin_unlock_irqrestore(&fsl_chan->vchan.lock, flags);
  880. }
  881. return 0;
  882. }
  883. static int fsl_edma_resume_early(struct device *dev)
  884. {
  885. struct fsl_edma_engine *fsl_edma = dev_get_drvdata(dev);
  886. struct fsl_edma_chan *fsl_chan;
  887. int i;
  888. for (i = 0; i < fsl_edma->n_chans; i++) {
  889. fsl_chan = &fsl_edma->chans[i];
  890. fsl_chan->pm_state = RUNNING;
  891. edma_writew(fsl_edma, 0x0, fsl_edma->membase + EDMA_TCD_CSR(i));
  892. if (fsl_chan->slave_id != 0)
  893. fsl_edma_chan_mux(fsl_chan, fsl_chan->slave_id, true);
  894. }
  895. edma_writel(fsl_edma, EDMA_CR_ERGA | EDMA_CR_ERCA,
  896. fsl_edma->membase + EDMA_CR);
  897. return 0;
  898. }
  899. /*
  900. * eDMA provides the service to others, so it should be suspend late
  901. * and resume early. When eDMA suspend, all of the clients should stop
  902. * the DMA data transmission and let the channel idle.
  903. */
  904. static const struct dev_pm_ops fsl_edma_pm_ops = {
  905. .suspend_late = fsl_edma_suspend_late,
  906. .resume_early = fsl_edma_resume_early,
  907. };
  908. static const struct of_device_id fsl_edma_dt_ids[] = {
  909. { .compatible = "fsl,vf610-edma", },
  910. { /* sentinel */ }
  911. };
  912. MODULE_DEVICE_TABLE(of, fsl_edma_dt_ids);
  913. static struct platform_driver fsl_edma_driver = {
  914. .driver = {
  915. .name = "fsl-edma",
  916. .of_match_table = fsl_edma_dt_ids,
  917. .pm = &fsl_edma_pm_ops,
  918. },
  919. .probe = fsl_edma_probe,
  920. .remove = fsl_edma_remove,
  921. };
  922. static int __init fsl_edma_init(void)
  923. {
  924. return platform_driver_register(&fsl_edma_driver);
  925. }
  926. subsys_initcall(fsl_edma_init);
  927. static void __exit fsl_edma_exit(void)
  928. {
  929. platform_driver_unregister(&fsl_edma_driver);
  930. }
  931. module_exit(fsl_edma_exit);
  932. MODULE_ALIAS("platform:fsl-edma");
  933. MODULE_DESCRIPTION("Freescale eDMA engine driver");
  934. MODULE_LICENSE("GPL v2");