fsl-edma.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991
  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. struct fsl_edma_hw_tcd {
  108. __le32 saddr;
  109. __le16 soff;
  110. __le16 attr;
  111. __le32 nbytes;
  112. __le32 slast;
  113. __le32 daddr;
  114. __le16 doff;
  115. __le16 citer;
  116. __le32 dlast_sga;
  117. __le16 csr;
  118. __le16 biter;
  119. };
  120. struct fsl_edma_sw_tcd {
  121. dma_addr_t ptcd;
  122. struct fsl_edma_hw_tcd *vtcd;
  123. };
  124. struct fsl_edma_slave_config {
  125. enum dma_transfer_direction dir;
  126. enum dma_slave_buswidth addr_width;
  127. u32 dev_addr;
  128. u32 burst;
  129. u32 attr;
  130. };
  131. struct fsl_edma_chan {
  132. struct virt_dma_chan vchan;
  133. enum dma_status status;
  134. struct fsl_edma_engine *edma;
  135. struct fsl_edma_desc *edesc;
  136. struct fsl_edma_slave_config fsc;
  137. struct dma_pool *tcd_pool;
  138. };
  139. struct fsl_edma_desc {
  140. struct virt_dma_desc vdesc;
  141. struct fsl_edma_chan *echan;
  142. bool iscyclic;
  143. unsigned int n_tcds;
  144. struct fsl_edma_sw_tcd tcd[];
  145. };
  146. struct fsl_edma_engine {
  147. struct dma_device dma_dev;
  148. void __iomem *membase;
  149. void __iomem *muxbase[DMAMUX_NR];
  150. struct clk *muxclk[DMAMUX_NR];
  151. struct mutex fsl_edma_mutex;
  152. u32 n_chans;
  153. int txirq;
  154. int errirq;
  155. bool big_endian;
  156. struct fsl_edma_chan chans[];
  157. };
  158. /*
  159. * R/W functions for big- or little-endian registers:
  160. * The eDMA controller's endian is independent of the CPU core's endian.
  161. * For the big-endian IP module, the offset for 8-bit or 16-bit registers
  162. * should also be swapped opposite to that in little-endian IP.
  163. */
  164. static u32 edma_readl(struct fsl_edma_engine *edma, void __iomem *addr)
  165. {
  166. if (edma->big_endian)
  167. return ioread32be(addr);
  168. else
  169. return ioread32(addr);
  170. }
  171. static void edma_writeb(struct fsl_edma_engine *edma, u8 val, void __iomem *addr)
  172. {
  173. /* swap the reg offset for these in big-endian mode */
  174. if (edma->big_endian)
  175. iowrite8(val, (void __iomem *)((unsigned long)addr ^ 0x3));
  176. else
  177. iowrite8(val, addr);
  178. }
  179. static void edma_writew(struct fsl_edma_engine *edma, u16 val, void __iomem *addr)
  180. {
  181. /* swap the reg offset for these in big-endian mode */
  182. if (edma->big_endian)
  183. iowrite16be(val, (void __iomem *)((unsigned long)addr ^ 0x2));
  184. else
  185. iowrite16(val, addr);
  186. }
  187. static void edma_writel(struct fsl_edma_engine *edma, u32 val, void __iomem *addr)
  188. {
  189. if (edma->big_endian)
  190. iowrite32be(val, addr);
  191. else
  192. iowrite32(val, addr);
  193. }
  194. static struct fsl_edma_chan *to_fsl_edma_chan(struct dma_chan *chan)
  195. {
  196. return container_of(chan, struct fsl_edma_chan, vchan.chan);
  197. }
  198. static struct fsl_edma_desc *to_fsl_edma_desc(struct virt_dma_desc *vd)
  199. {
  200. return container_of(vd, struct fsl_edma_desc, vdesc);
  201. }
  202. static void fsl_edma_enable_request(struct fsl_edma_chan *fsl_chan)
  203. {
  204. void __iomem *addr = fsl_chan->edma->membase;
  205. u32 ch = fsl_chan->vchan.chan.chan_id;
  206. edma_writeb(fsl_chan->edma, EDMA_SEEI_SEEI(ch), addr + EDMA_SEEI);
  207. edma_writeb(fsl_chan->edma, ch, addr + EDMA_SERQ);
  208. }
  209. static void fsl_edma_disable_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, ch, addr + EDMA_CERQ);
  214. edma_writeb(fsl_chan->edma, EDMA_CEEI_CEEI(ch), addr + EDMA_CEEI);
  215. }
  216. static void fsl_edma_chan_mux(struct fsl_edma_chan *fsl_chan,
  217. unsigned int slot, bool enable)
  218. {
  219. u32 ch = fsl_chan->vchan.chan.chan_id;
  220. void __iomem *muxaddr;
  221. unsigned chans_per_mux, ch_off;
  222. chans_per_mux = fsl_chan->edma->n_chans / DMAMUX_NR;
  223. ch_off = fsl_chan->vchan.chan.chan_id % chans_per_mux;
  224. muxaddr = fsl_chan->edma->muxbase[ch / chans_per_mux];
  225. slot = EDMAMUX_CHCFG_SOURCE(slot);
  226. if (enable)
  227. iowrite8(EDMAMUX_CHCFG_ENBL | slot, muxaddr + ch_off);
  228. else
  229. iowrite8(EDMAMUX_CHCFG_DIS, muxaddr + ch_off);
  230. }
  231. static unsigned int fsl_edma_get_tcd_attr(enum dma_slave_buswidth addr_width)
  232. {
  233. switch (addr_width) {
  234. case 1:
  235. return EDMA_TCD_ATTR_SSIZE_8BIT | EDMA_TCD_ATTR_DSIZE_8BIT;
  236. case 2:
  237. return EDMA_TCD_ATTR_SSIZE_16BIT | EDMA_TCD_ATTR_DSIZE_16BIT;
  238. case 4:
  239. return EDMA_TCD_ATTR_SSIZE_32BIT | EDMA_TCD_ATTR_DSIZE_32BIT;
  240. case 8:
  241. return EDMA_TCD_ATTR_SSIZE_64BIT | EDMA_TCD_ATTR_DSIZE_64BIT;
  242. default:
  243. return EDMA_TCD_ATTR_SSIZE_32BIT | EDMA_TCD_ATTR_DSIZE_32BIT;
  244. }
  245. }
  246. static void fsl_edma_free_desc(struct virt_dma_desc *vdesc)
  247. {
  248. struct fsl_edma_desc *fsl_desc;
  249. int i;
  250. fsl_desc = to_fsl_edma_desc(vdesc);
  251. for (i = 0; i < fsl_desc->n_tcds; i++)
  252. dma_pool_free(fsl_desc->echan->tcd_pool, fsl_desc->tcd[i].vtcd,
  253. fsl_desc->tcd[i].ptcd);
  254. kfree(fsl_desc);
  255. }
  256. static int fsl_edma_terminate_all(struct dma_chan *chan)
  257. {
  258. struct fsl_edma_chan *fsl_chan = to_fsl_edma_chan(chan);
  259. unsigned long flags;
  260. LIST_HEAD(head);
  261. spin_lock_irqsave(&fsl_chan->vchan.lock, flags);
  262. fsl_edma_disable_request(fsl_chan);
  263. fsl_chan->edesc = NULL;
  264. vchan_get_all_descriptors(&fsl_chan->vchan, &head);
  265. spin_unlock_irqrestore(&fsl_chan->vchan.lock, flags);
  266. vchan_dma_desc_free_list(&fsl_chan->vchan, &head);
  267. return 0;
  268. }
  269. static int fsl_edma_pause(struct dma_chan *chan)
  270. {
  271. struct fsl_edma_chan *fsl_chan = to_fsl_edma_chan(chan);
  272. unsigned long flags;
  273. spin_lock_irqsave(&fsl_chan->vchan.lock, flags);
  274. if (fsl_chan->edesc) {
  275. fsl_edma_disable_request(fsl_chan);
  276. fsl_chan->status = DMA_PAUSED;
  277. }
  278. spin_unlock_irqrestore(&fsl_chan->vchan.lock, flags);
  279. return 0;
  280. }
  281. static int fsl_edma_resume(struct dma_chan *chan)
  282. {
  283. struct fsl_edma_chan *fsl_chan = to_fsl_edma_chan(chan);
  284. unsigned long flags;
  285. spin_lock_irqsave(&fsl_chan->vchan.lock, flags);
  286. if (fsl_chan->edesc) {
  287. fsl_edma_enable_request(fsl_chan);
  288. fsl_chan->status = DMA_IN_PROGRESS;
  289. }
  290. spin_unlock_irqrestore(&fsl_chan->vchan.lock, flags);
  291. return 0;
  292. }
  293. static int fsl_edma_slave_config(struct dma_chan *chan,
  294. struct dma_slave_config *cfg)
  295. {
  296. struct fsl_edma_chan *fsl_chan = to_fsl_edma_chan(chan);
  297. fsl_chan->fsc.dir = cfg->direction;
  298. if (cfg->direction == DMA_DEV_TO_MEM) {
  299. fsl_chan->fsc.dev_addr = cfg->src_addr;
  300. fsl_chan->fsc.addr_width = cfg->src_addr_width;
  301. fsl_chan->fsc.burst = cfg->src_maxburst;
  302. fsl_chan->fsc.attr = fsl_edma_get_tcd_attr(cfg->src_addr_width);
  303. } else if (cfg->direction == DMA_MEM_TO_DEV) {
  304. fsl_chan->fsc.dev_addr = cfg->dst_addr;
  305. fsl_chan->fsc.addr_width = cfg->dst_addr_width;
  306. fsl_chan->fsc.burst = cfg->dst_maxburst;
  307. fsl_chan->fsc.attr = fsl_edma_get_tcd_attr(cfg->dst_addr_width);
  308. } else {
  309. return -EINVAL;
  310. }
  311. return 0;
  312. }
  313. static size_t fsl_edma_desc_residue(struct fsl_edma_chan *fsl_chan,
  314. struct virt_dma_desc *vdesc, bool in_progress)
  315. {
  316. struct fsl_edma_desc *edesc = fsl_chan->edesc;
  317. void __iomem *addr = fsl_chan->edma->membase;
  318. u32 ch = fsl_chan->vchan.chan.chan_id;
  319. enum dma_transfer_direction dir = fsl_chan->fsc.dir;
  320. dma_addr_t cur_addr, dma_addr;
  321. size_t len, size;
  322. int i;
  323. /* calculate the total size in this desc */
  324. for (len = i = 0; i < fsl_chan->edesc->n_tcds; i++)
  325. len += le32_to_cpu(edesc->tcd[i].vtcd->nbytes)
  326. * le16_to_cpu(edesc->tcd[i].vtcd->biter);
  327. if (!in_progress)
  328. return len;
  329. if (dir == DMA_MEM_TO_DEV)
  330. cur_addr = edma_readl(fsl_chan->edma, addr + EDMA_TCD_SADDR(ch));
  331. else
  332. cur_addr = edma_readl(fsl_chan->edma, addr + EDMA_TCD_DADDR(ch));
  333. /* figure out the finished and calculate the residue */
  334. for (i = 0; i < fsl_chan->edesc->n_tcds; i++) {
  335. size = le32_to_cpu(edesc->tcd[i].vtcd->nbytes)
  336. * le16_to_cpu(edesc->tcd[i].vtcd->biter);
  337. if (dir == DMA_MEM_TO_DEV)
  338. dma_addr = le32_to_cpu(edesc->tcd[i].vtcd->saddr);
  339. else
  340. dma_addr = le32_to_cpu(edesc->tcd[i].vtcd->daddr);
  341. len -= size;
  342. if (cur_addr >= dma_addr && cur_addr < dma_addr + size) {
  343. len += dma_addr + size - cur_addr;
  344. break;
  345. }
  346. }
  347. return len;
  348. }
  349. static enum dma_status fsl_edma_tx_status(struct dma_chan *chan,
  350. dma_cookie_t cookie, struct dma_tx_state *txstate)
  351. {
  352. struct fsl_edma_chan *fsl_chan = to_fsl_edma_chan(chan);
  353. struct virt_dma_desc *vdesc;
  354. enum dma_status status;
  355. unsigned long flags;
  356. status = dma_cookie_status(chan, cookie, txstate);
  357. if (status == DMA_COMPLETE)
  358. return status;
  359. if (!txstate)
  360. return fsl_chan->status;
  361. spin_lock_irqsave(&fsl_chan->vchan.lock, flags);
  362. vdesc = vchan_find_desc(&fsl_chan->vchan, cookie);
  363. if (fsl_chan->edesc && cookie == fsl_chan->edesc->vdesc.tx.cookie)
  364. txstate->residue = fsl_edma_desc_residue(fsl_chan, vdesc, true);
  365. else if (vdesc)
  366. txstate->residue = fsl_edma_desc_residue(fsl_chan, vdesc, false);
  367. else
  368. txstate->residue = 0;
  369. spin_unlock_irqrestore(&fsl_chan->vchan.lock, flags);
  370. return fsl_chan->status;
  371. }
  372. static void fsl_edma_set_tcd_regs(struct fsl_edma_chan *fsl_chan,
  373. struct fsl_edma_hw_tcd *tcd)
  374. {
  375. struct fsl_edma_engine *edma = fsl_chan->edma;
  376. void __iomem *addr = fsl_chan->edma->membase;
  377. u32 ch = fsl_chan->vchan.chan.chan_id;
  378. /*
  379. * TCD parameters are stored in struct fsl_edma_hw_tcd in little
  380. * endian format. However, we need to load the TCD registers in
  381. * big- or little-endian obeying the eDMA engine model endian.
  382. */
  383. edma_writew(edma, 0, addr + EDMA_TCD_CSR(ch));
  384. edma_writel(edma, le32_to_cpu(tcd->saddr), addr + EDMA_TCD_SADDR(ch));
  385. edma_writel(edma, le32_to_cpu(tcd->daddr), addr + EDMA_TCD_DADDR(ch));
  386. edma_writew(edma, le16_to_cpu(tcd->attr), addr + EDMA_TCD_ATTR(ch));
  387. edma_writew(edma, le16_to_cpu(tcd->soff), addr + EDMA_TCD_SOFF(ch));
  388. edma_writel(edma, le32_to_cpu(tcd->nbytes), addr + EDMA_TCD_NBYTES(ch));
  389. edma_writel(edma, le32_to_cpu(tcd->slast), addr + EDMA_TCD_SLAST(ch));
  390. edma_writew(edma, le16_to_cpu(tcd->citer), addr + EDMA_TCD_CITER(ch));
  391. edma_writew(edma, le16_to_cpu(tcd->biter), addr + EDMA_TCD_BITER(ch));
  392. edma_writew(edma, le16_to_cpu(tcd->doff), addr + EDMA_TCD_DOFF(ch));
  393. edma_writel(edma, le32_to_cpu(tcd->dlast_sga), addr + EDMA_TCD_DLAST_SGA(ch));
  394. edma_writew(edma, le16_to_cpu(tcd->csr), addr + EDMA_TCD_CSR(ch));
  395. }
  396. static inline
  397. void fsl_edma_fill_tcd(struct fsl_edma_hw_tcd *tcd, u32 src, u32 dst,
  398. u16 attr, u16 soff, u32 nbytes, u32 slast, u16 citer,
  399. u16 biter, u16 doff, u32 dlast_sga, bool major_int,
  400. bool disable_req, bool enable_sg)
  401. {
  402. u16 csr = 0;
  403. /*
  404. * eDMA hardware SGs require the TCDs to be stored in little
  405. * endian format irrespective of the register endian model.
  406. * So we put the value in little endian in memory, waiting
  407. * for fsl_edma_set_tcd_regs doing the swap.
  408. */
  409. tcd->saddr = cpu_to_le32(src);
  410. tcd->daddr = cpu_to_le32(dst);
  411. tcd->attr = cpu_to_le16(attr);
  412. tcd->soff = cpu_to_le16(EDMA_TCD_SOFF_SOFF(soff));
  413. tcd->nbytes = cpu_to_le32(EDMA_TCD_NBYTES_NBYTES(nbytes));
  414. tcd->slast = cpu_to_le32(EDMA_TCD_SLAST_SLAST(slast));
  415. tcd->citer = cpu_to_le16(EDMA_TCD_CITER_CITER(citer));
  416. tcd->doff = cpu_to_le16(EDMA_TCD_DOFF_DOFF(doff));
  417. tcd->dlast_sga = cpu_to_le32(EDMA_TCD_DLAST_SGA_DLAST_SGA(dlast_sga));
  418. tcd->biter = cpu_to_le16(EDMA_TCD_BITER_BITER(biter));
  419. if (major_int)
  420. csr |= EDMA_TCD_CSR_INT_MAJOR;
  421. if (disable_req)
  422. csr |= EDMA_TCD_CSR_D_REQ;
  423. if (enable_sg)
  424. csr |= EDMA_TCD_CSR_E_SG;
  425. tcd->csr = cpu_to_le16(csr);
  426. }
  427. static struct fsl_edma_desc *fsl_edma_alloc_desc(struct fsl_edma_chan *fsl_chan,
  428. int sg_len)
  429. {
  430. struct fsl_edma_desc *fsl_desc;
  431. int i;
  432. fsl_desc = kzalloc(sizeof(*fsl_desc) + sizeof(struct fsl_edma_sw_tcd) * sg_len,
  433. GFP_NOWAIT);
  434. if (!fsl_desc)
  435. return NULL;
  436. fsl_desc->echan = fsl_chan;
  437. fsl_desc->n_tcds = sg_len;
  438. for (i = 0; i < sg_len; i++) {
  439. fsl_desc->tcd[i].vtcd = dma_pool_alloc(fsl_chan->tcd_pool,
  440. GFP_NOWAIT, &fsl_desc->tcd[i].ptcd);
  441. if (!fsl_desc->tcd[i].vtcd)
  442. goto err;
  443. }
  444. return fsl_desc;
  445. err:
  446. while (--i >= 0)
  447. dma_pool_free(fsl_chan->tcd_pool, fsl_desc->tcd[i].vtcd,
  448. fsl_desc->tcd[i].ptcd);
  449. kfree(fsl_desc);
  450. return NULL;
  451. }
  452. static struct dma_async_tx_descriptor *fsl_edma_prep_dma_cyclic(
  453. struct dma_chan *chan, dma_addr_t dma_addr, size_t buf_len,
  454. size_t period_len, enum dma_transfer_direction direction,
  455. unsigned long flags)
  456. {
  457. struct fsl_edma_chan *fsl_chan = to_fsl_edma_chan(chan);
  458. struct fsl_edma_desc *fsl_desc;
  459. dma_addr_t dma_buf_next;
  460. int sg_len, i;
  461. u32 src_addr, dst_addr, last_sg, nbytes;
  462. u16 soff, doff, iter;
  463. if (!is_slave_direction(fsl_chan->fsc.dir))
  464. return NULL;
  465. sg_len = buf_len / period_len;
  466. fsl_desc = fsl_edma_alloc_desc(fsl_chan, sg_len);
  467. if (!fsl_desc)
  468. return NULL;
  469. fsl_desc->iscyclic = true;
  470. dma_buf_next = dma_addr;
  471. nbytes = fsl_chan->fsc.addr_width * fsl_chan->fsc.burst;
  472. iter = period_len / nbytes;
  473. for (i = 0; i < sg_len; i++) {
  474. if (dma_buf_next >= dma_addr + buf_len)
  475. dma_buf_next = dma_addr;
  476. /* get next sg's physical address */
  477. last_sg = fsl_desc->tcd[(i + 1) % sg_len].ptcd;
  478. if (fsl_chan->fsc.dir == DMA_MEM_TO_DEV) {
  479. src_addr = dma_buf_next;
  480. dst_addr = fsl_chan->fsc.dev_addr;
  481. soff = fsl_chan->fsc.addr_width;
  482. doff = 0;
  483. } else {
  484. src_addr = fsl_chan->fsc.dev_addr;
  485. dst_addr = dma_buf_next;
  486. soff = 0;
  487. doff = fsl_chan->fsc.addr_width;
  488. }
  489. fsl_edma_fill_tcd(fsl_desc->tcd[i].vtcd, src_addr, dst_addr,
  490. fsl_chan->fsc.attr, soff, nbytes, 0, iter,
  491. iter, doff, last_sg, true, false, true);
  492. dma_buf_next += period_len;
  493. }
  494. return vchan_tx_prep(&fsl_chan->vchan, &fsl_desc->vdesc, flags);
  495. }
  496. static struct dma_async_tx_descriptor *fsl_edma_prep_slave_sg(
  497. struct dma_chan *chan, struct scatterlist *sgl,
  498. unsigned int sg_len, enum dma_transfer_direction direction,
  499. unsigned long flags, void *context)
  500. {
  501. struct fsl_edma_chan *fsl_chan = to_fsl_edma_chan(chan);
  502. struct fsl_edma_desc *fsl_desc;
  503. struct scatterlist *sg;
  504. u32 src_addr, dst_addr, last_sg, nbytes;
  505. u16 soff, doff, iter;
  506. int i;
  507. if (!is_slave_direction(fsl_chan->fsc.dir))
  508. return NULL;
  509. fsl_desc = fsl_edma_alloc_desc(fsl_chan, sg_len);
  510. if (!fsl_desc)
  511. return NULL;
  512. fsl_desc->iscyclic = false;
  513. nbytes = fsl_chan->fsc.addr_width * fsl_chan->fsc.burst;
  514. for_each_sg(sgl, sg, sg_len, i) {
  515. /* get next sg's physical address */
  516. last_sg = fsl_desc->tcd[(i + 1) % sg_len].ptcd;
  517. if (fsl_chan->fsc.dir == DMA_MEM_TO_DEV) {
  518. src_addr = sg_dma_address(sg);
  519. dst_addr = fsl_chan->fsc.dev_addr;
  520. soff = fsl_chan->fsc.addr_width;
  521. doff = 0;
  522. } else {
  523. src_addr = fsl_chan->fsc.dev_addr;
  524. dst_addr = sg_dma_address(sg);
  525. soff = 0;
  526. doff = fsl_chan->fsc.addr_width;
  527. }
  528. iter = sg_dma_len(sg) / nbytes;
  529. if (i < sg_len - 1) {
  530. last_sg = fsl_desc->tcd[(i + 1)].ptcd;
  531. fsl_edma_fill_tcd(fsl_desc->tcd[i].vtcd, src_addr,
  532. dst_addr, fsl_chan->fsc.attr, soff,
  533. nbytes, 0, iter, iter, doff, last_sg,
  534. false, false, true);
  535. } else {
  536. last_sg = 0;
  537. fsl_edma_fill_tcd(fsl_desc->tcd[i].vtcd, src_addr,
  538. dst_addr, fsl_chan->fsc.attr, soff,
  539. nbytes, 0, iter, iter, doff, last_sg,
  540. true, true, false);
  541. }
  542. }
  543. return vchan_tx_prep(&fsl_chan->vchan, &fsl_desc->vdesc, flags);
  544. }
  545. static void fsl_edma_xfer_desc(struct fsl_edma_chan *fsl_chan)
  546. {
  547. struct virt_dma_desc *vdesc;
  548. vdesc = vchan_next_desc(&fsl_chan->vchan);
  549. if (!vdesc)
  550. return;
  551. fsl_chan->edesc = to_fsl_edma_desc(vdesc);
  552. fsl_edma_set_tcd_regs(fsl_chan, fsl_chan->edesc->tcd[0].vtcd);
  553. fsl_edma_enable_request(fsl_chan);
  554. fsl_chan->status = DMA_IN_PROGRESS;
  555. }
  556. static irqreturn_t fsl_edma_tx_handler(int irq, void *dev_id)
  557. {
  558. struct fsl_edma_engine *fsl_edma = dev_id;
  559. unsigned int intr, ch;
  560. void __iomem *base_addr;
  561. struct fsl_edma_chan *fsl_chan;
  562. base_addr = fsl_edma->membase;
  563. intr = edma_readl(fsl_edma, base_addr + EDMA_INTR);
  564. if (!intr)
  565. return IRQ_NONE;
  566. for (ch = 0; ch < fsl_edma->n_chans; ch++) {
  567. if (intr & (0x1 << ch)) {
  568. edma_writeb(fsl_edma, EDMA_CINT_CINT(ch),
  569. base_addr + EDMA_CINT);
  570. fsl_chan = &fsl_edma->chans[ch];
  571. spin_lock(&fsl_chan->vchan.lock);
  572. if (!fsl_chan->edesc->iscyclic) {
  573. list_del(&fsl_chan->edesc->vdesc.node);
  574. vchan_cookie_complete(&fsl_chan->edesc->vdesc);
  575. fsl_chan->edesc = NULL;
  576. fsl_chan->status = DMA_COMPLETE;
  577. } else {
  578. vchan_cyclic_callback(&fsl_chan->edesc->vdesc);
  579. }
  580. if (!fsl_chan->edesc)
  581. fsl_edma_xfer_desc(fsl_chan);
  582. spin_unlock(&fsl_chan->vchan.lock);
  583. }
  584. }
  585. return IRQ_HANDLED;
  586. }
  587. static irqreturn_t fsl_edma_err_handler(int irq, void *dev_id)
  588. {
  589. struct fsl_edma_engine *fsl_edma = dev_id;
  590. unsigned int err, ch;
  591. err = edma_readl(fsl_edma, fsl_edma->membase + EDMA_ERR);
  592. if (!err)
  593. return IRQ_NONE;
  594. for (ch = 0; ch < fsl_edma->n_chans; ch++) {
  595. if (err & (0x1 << ch)) {
  596. fsl_edma_disable_request(&fsl_edma->chans[ch]);
  597. edma_writeb(fsl_edma, EDMA_CERR_CERR(ch),
  598. fsl_edma->membase + EDMA_CERR);
  599. fsl_edma->chans[ch].status = DMA_ERROR;
  600. }
  601. }
  602. return IRQ_HANDLED;
  603. }
  604. static irqreturn_t fsl_edma_irq_handler(int irq, void *dev_id)
  605. {
  606. if (fsl_edma_tx_handler(irq, dev_id) == IRQ_HANDLED)
  607. return IRQ_HANDLED;
  608. return fsl_edma_err_handler(irq, dev_id);
  609. }
  610. static void fsl_edma_issue_pending(struct dma_chan *chan)
  611. {
  612. struct fsl_edma_chan *fsl_chan = to_fsl_edma_chan(chan);
  613. unsigned long flags;
  614. spin_lock_irqsave(&fsl_chan->vchan.lock, flags);
  615. if (vchan_issue_pending(&fsl_chan->vchan) && !fsl_chan->edesc)
  616. fsl_edma_xfer_desc(fsl_chan);
  617. spin_unlock_irqrestore(&fsl_chan->vchan.lock, flags);
  618. }
  619. static struct dma_chan *fsl_edma_xlate(struct of_phandle_args *dma_spec,
  620. struct of_dma *ofdma)
  621. {
  622. struct fsl_edma_engine *fsl_edma = ofdma->of_dma_data;
  623. struct dma_chan *chan, *_chan;
  624. unsigned long chans_per_mux = fsl_edma->n_chans / DMAMUX_NR;
  625. if (dma_spec->args_count != 2)
  626. return NULL;
  627. mutex_lock(&fsl_edma->fsl_edma_mutex);
  628. list_for_each_entry_safe(chan, _chan, &fsl_edma->dma_dev.channels, device_node) {
  629. if (chan->client_count)
  630. continue;
  631. if ((chan->chan_id / chans_per_mux) == dma_spec->args[0]) {
  632. chan = dma_get_slave_channel(chan);
  633. if (chan) {
  634. chan->device->privatecnt++;
  635. fsl_edma_chan_mux(to_fsl_edma_chan(chan),
  636. dma_spec->args[1], true);
  637. mutex_unlock(&fsl_edma->fsl_edma_mutex);
  638. return chan;
  639. }
  640. }
  641. }
  642. mutex_unlock(&fsl_edma->fsl_edma_mutex);
  643. return NULL;
  644. }
  645. static int fsl_edma_alloc_chan_resources(struct dma_chan *chan)
  646. {
  647. struct fsl_edma_chan *fsl_chan = to_fsl_edma_chan(chan);
  648. fsl_chan->tcd_pool = dma_pool_create("tcd_pool", chan->device->dev,
  649. sizeof(struct fsl_edma_hw_tcd),
  650. 32, 0);
  651. return 0;
  652. }
  653. static void fsl_edma_free_chan_resources(struct dma_chan *chan)
  654. {
  655. struct fsl_edma_chan *fsl_chan = to_fsl_edma_chan(chan);
  656. unsigned long flags;
  657. LIST_HEAD(head);
  658. spin_lock_irqsave(&fsl_chan->vchan.lock, flags);
  659. fsl_edma_disable_request(fsl_chan);
  660. fsl_edma_chan_mux(fsl_chan, 0, false);
  661. fsl_chan->edesc = NULL;
  662. vchan_get_all_descriptors(&fsl_chan->vchan, &head);
  663. spin_unlock_irqrestore(&fsl_chan->vchan.lock, flags);
  664. vchan_dma_desc_free_list(&fsl_chan->vchan, &head);
  665. dma_pool_destroy(fsl_chan->tcd_pool);
  666. fsl_chan->tcd_pool = NULL;
  667. }
  668. static int
  669. fsl_edma_irq_init(struct platform_device *pdev, struct fsl_edma_engine *fsl_edma)
  670. {
  671. int ret;
  672. fsl_edma->txirq = platform_get_irq_byname(pdev, "edma-tx");
  673. if (fsl_edma->txirq < 0) {
  674. dev_err(&pdev->dev, "Can't get edma-tx irq.\n");
  675. return fsl_edma->txirq;
  676. }
  677. fsl_edma->errirq = platform_get_irq_byname(pdev, "edma-err");
  678. if (fsl_edma->errirq < 0) {
  679. dev_err(&pdev->dev, "Can't get edma-err irq.\n");
  680. return fsl_edma->errirq;
  681. }
  682. if (fsl_edma->txirq == fsl_edma->errirq) {
  683. ret = devm_request_irq(&pdev->dev, fsl_edma->txirq,
  684. fsl_edma_irq_handler, 0, "eDMA", fsl_edma);
  685. if (ret) {
  686. dev_err(&pdev->dev, "Can't register eDMA IRQ.\n");
  687. return ret;
  688. }
  689. } else {
  690. ret = devm_request_irq(&pdev->dev, fsl_edma->txirq,
  691. fsl_edma_tx_handler, 0, "eDMA tx", fsl_edma);
  692. if (ret) {
  693. dev_err(&pdev->dev, "Can't register eDMA tx IRQ.\n");
  694. return ret;
  695. }
  696. ret = devm_request_irq(&pdev->dev, fsl_edma->errirq,
  697. fsl_edma_err_handler, 0, "eDMA err", fsl_edma);
  698. if (ret) {
  699. dev_err(&pdev->dev, "Can't register eDMA err IRQ.\n");
  700. return ret;
  701. }
  702. }
  703. return 0;
  704. }
  705. static int fsl_edma_probe(struct platform_device *pdev)
  706. {
  707. struct device_node *np = pdev->dev.of_node;
  708. struct fsl_edma_engine *fsl_edma;
  709. struct fsl_edma_chan *fsl_chan;
  710. struct resource *res;
  711. int len, chans;
  712. int ret, i;
  713. ret = of_property_read_u32(np, "dma-channels", &chans);
  714. if (ret) {
  715. dev_err(&pdev->dev, "Can't get dma-channels.\n");
  716. return ret;
  717. }
  718. len = sizeof(*fsl_edma) + sizeof(*fsl_chan) * chans;
  719. fsl_edma = devm_kzalloc(&pdev->dev, len, GFP_KERNEL);
  720. if (!fsl_edma)
  721. return -ENOMEM;
  722. fsl_edma->n_chans = chans;
  723. mutex_init(&fsl_edma->fsl_edma_mutex);
  724. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  725. fsl_edma->membase = devm_ioremap_resource(&pdev->dev, res);
  726. if (IS_ERR(fsl_edma->membase))
  727. return PTR_ERR(fsl_edma->membase);
  728. for (i = 0; i < DMAMUX_NR; i++) {
  729. char clkname[32];
  730. res = platform_get_resource(pdev, IORESOURCE_MEM, 1 + i);
  731. fsl_edma->muxbase[i] = devm_ioremap_resource(&pdev->dev, res);
  732. if (IS_ERR(fsl_edma->muxbase[i]))
  733. return PTR_ERR(fsl_edma->muxbase[i]);
  734. sprintf(clkname, "dmamux%d", i);
  735. fsl_edma->muxclk[i] = devm_clk_get(&pdev->dev, clkname);
  736. if (IS_ERR(fsl_edma->muxclk[i])) {
  737. dev_err(&pdev->dev, "Missing DMAMUX block clock.\n");
  738. return PTR_ERR(fsl_edma->muxclk[i]);
  739. }
  740. ret = clk_prepare_enable(fsl_edma->muxclk[i]);
  741. if (ret) {
  742. dev_err(&pdev->dev, "DMAMUX clk block failed.\n");
  743. return ret;
  744. }
  745. }
  746. fsl_edma->big_endian = of_property_read_bool(np, "big-endian");
  747. INIT_LIST_HEAD(&fsl_edma->dma_dev.channels);
  748. for (i = 0; i < fsl_edma->n_chans; i++) {
  749. struct fsl_edma_chan *fsl_chan = &fsl_edma->chans[i];
  750. fsl_chan->edma = fsl_edma;
  751. fsl_chan->vchan.desc_free = fsl_edma_free_desc;
  752. vchan_init(&fsl_chan->vchan, &fsl_edma->dma_dev);
  753. edma_writew(fsl_edma, 0x0, fsl_edma->membase + EDMA_TCD_CSR(i));
  754. fsl_edma_chan_mux(fsl_chan, 0, false);
  755. }
  756. edma_writel(fsl_edma, ~0, fsl_edma->membase + EDMA_INTR);
  757. ret = fsl_edma_irq_init(pdev, fsl_edma);
  758. if (ret)
  759. return ret;
  760. dma_cap_set(DMA_PRIVATE, fsl_edma->dma_dev.cap_mask);
  761. dma_cap_set(DMA_SLAVE, fsl_edma->dma_dev.cap_mask);
  762. dma_cap_set(DMA_CYCLIC, fsl_edma->dma_dev.cap_mask);
  763. fsl_edma->dma_dev.dev = &pdev->dev;
  764. fsl_edma->dma_dev.device_alloc_chan_resources
  765. = fsl_edma_alloc_chan_resources;
  766. fsl_edma->dma_dev.device_free_chan_resources
  767. = fsl_edma_free_chan_resources;
  768. fsl_edma->dma_dev.device_tx_status = fsl_edma_tx_status;
  769. fsl_edma->dma_dev.device_prep_slave_sg = fsl_edma_prep_slave_sg;
  770. fsl_edma->dma_dev.device_prep_dma_cyclic = fsl_edma_prep_dma_cyclic;
  771. fsl_edma->dma_dev.device_config = fsl_edma_slave_config;
  772. fsl_edma->dma_dev.device_pause = fsl_edma_pause;
  773. fsl_edma->dma_dev.device_resume = fsl_edma_resume;
  774. fsl_edma->dma_dev.device_terminate_all = fsl_edma_terminate_all;
  775. fsl_edma->dma_dev.device_issue_pending = fsl_edma_issue_pending;
  776. fsl_edma->dma_dev.src_addr_widths = FSL_EDMA_BUSWIDTHS;
  777. fsl_edma->dma_dev.dst_addr_widths = FSL_EDMA_BUSWIDTHS;
  778. fsl_edma->dma_dev.directions = BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV);
  779. platform_set_drvdata(pdev, fsl_edma);
  780. ret = dma_async_device_register(&fsl_edma->dma_dev);
  781. if (ret) {
  782. dev_err(&pdev->dev, "Can't register Freescale eDMA engine.\n");
  783. return ret;
  784. }
  785. ret = of_dma_controller_register(np, fsl_edma_xlate, fsl_edma);
  786. if (ret) {
  787. dev_err(&pdev->dev, "Can't register Freescale eDMA of_dma.\n");
  788. dma_async_device_unregister(&fsl_edma->dma_dev);
  789. return ret;
  790. }
  791. /* enable round robin arbitration */
  792. edma_writel(fsl_edma, EDMA_CR_ERGA | EDMA_CR_ERCA, fsl_edma->membase + EDMA_CR);
  793. return 0;
  794. }
  795. static int fsl_edma_remove(struct platform_device *pdev)
  796. {
  797. struct device_node *np = pdev->dev.of_node;
  798. struct fsl_edma_engine *fsl_edma = platform_get_drvdata(pdev);
  799. int i;
  800. of_dma_controller_free(np);
  801. dma_async_device_unregister(&fsl_edma->dma_dev);
  802. for (i = 0; i < DMAMUX_NR; i++)
  803. clk_disable_unprepare(fsl_edma->muxclk[i]);
  804. return 0;
  805. }
  806. static const struct of_device_id fsl_edma_dt_ids[] = {
  807. { .compatible = "fsl,vf610-edma", },
  808. { /* sentinel */ }
  809. };
  810. MODULE_DEVICE_TABLE(of, fsl_edma_dt_ids);
  811. static struct platform_driver fsl_edma_driver = {
  812. .driver = {
  813. .name = "fsl-edma",
  814. .of_match_table = fsl_edma_dt_ids,
  815. },
  816. .probe = fsl_edma_probe,
  817. .remove = fsl_edma_remove,
  818. };
  819. static int __init fsl_edma_init(void)
  820. {
  821. return platform_driver_register(&fsl_edma_driver);
  822. }
  823. subsys_initcall(fsl_edma_init);
  824. static void __exit fsl_edma_exit(void)
  825. {
  826. platform_driver_unregister(&fsl_edma_driver);
  827. }
  828. module_exit(fsl_edma_exit);
  829. MODULE_ALIAS("platform:fsl-edma");
  830. MODULE_DESCRIPTION("Freescale eDMA engine driver");
  831. MODULE_LICENSE("GPL v2");