fsl-edma-common.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. /* SPDX-License-Identifier: GPL-2.0+ */
  2. /*
  3. * Copyright 2013-2014 Freescale Semiconductor, Inc.
  4. * Copyright 2018 Angelo Dureghello <angelo@sysam.it>
  5. */
  6. #ifndef _FSL_EDMA_COMMON_H_
  7. #define _FSL_EDMA_COMMON_H_
  8. #include <linux/dma-direction.h>
  9. #include <linux/platform_device.h>
  10. #include "virt-dma.h"
  11. #define EDMA_CR_EDBG BIT(1)
  12. #define EDMA_CR_ERCA BIT(2)
  13. #define EDMA_CR_ERGA BIT(3)
  14. #define EDMA_CR_HOE BIT(4)
  15. #define EDMA_CR_HALT BIT(5)
  16. #define EDMA_CR_CLM BIT(6)
  17. #define EDMA_CR_EMLM BIT(7)
  18. #define EDMA_CR_ECX BIT(16)
  19. #define EDMA_CR_CX BIT(17)
  20. #define EDMA_SEEI_SEEI(x) ((x) & GENMASK(4, 0))
  21. #define EDMA_CEEI_CEEI(x) ((x) & GENMASK(4, 0))
  22. #define EDMA_CINT_CINT(x) ((x) & GENMASK(4, 0))
  23. #define EDMA_CERR_CERR(x) ((x) & GENMASK(4, 0))
  24. #define EDMA_TCD_ATTR_DSIZE(x) (((x) & GENMASK(2, 0)))
  25. #define EDMA_TCD_ATTR_DMOD(x) (((x) & GENMASK(4, 0)) << 3)
  26. #define EDMA_TCD_ATTR_SSIZE(x) (((x) & GENMASK(2, 0)) << 8)
  27. #define EDMA_TCD_ATTR_SMOD(x) (((x) & GENMASK(4, 0)) << 11)
  28. #define EDMA_TCD_ITER_MASK GENMASK(14, 0)
  29. #define EDMA_TCD_CITER_CITER(x) ((x) & EDMA_TCD_ITER_MASK)
  30. #define EDMA_TCD_BITER_BITER(x) ((x) & EDMA_TCD_ITER_MASK)
  31. #define EDMA_TCD_CSR_START BIT(0)
  32. #define EDMA_TCD_CSR_INT_MAJOR BIT(1)
  33. #define EDMA_TCD_CSR_INT_HALF BIT(2)
  34. #define EDMA_TCD_CSR_D_REQ BIT(3)
  35. #define EDMA_TCD_CSR_E_SG BIT(4)
  36. #define EDMA_TCD_CSR_E_LINK BIT(5)
  37. #define EDMA_TCD_CSR_ACTIVE BIT(6)
  38. #define EDMA_TCD_CSR_DONE BIT(7)
  39. #define EDMA_V3_TCD_NBYTES_MLOFF_NBYTES(x) ((x) & GENMASK(9, 0))
  40. #define EDMA_V3_TCD_NBYTES_MLOFF(x) (x << 10)
  41. #define EDMA_V3_TCD_NBYTES_DMLOE (1 << 30)
  42. #define EDMA_V3_TCD_NBYTES_SMLOE (1 << 31)
  43. #define EDMAMUX_CHCFG_DIS 0x0
  44. #define EDMAMUX_CHCFG_ENBL 0x80
  45. #define EDMAMUX_CHCFG_SOURCE(n) ((n) & 0x3F)
  46. #define DMAMUX_NR 2
  47. #define EDMA_TCD 0x1000
  48. #define FSL_EDMA_BUSWIDTHS (BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) | \
  49. BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) | \
  50. BIT(DMA_SLAVE_BUSWIDTH_4_BYTES) | \
  51. BIT(DMA_SLAVE_BUSWIDTH_8_BYTES))
  52. #define EDMA_V3_CH_SBR_RD BIT(22)
  53. #define EDMA_V3_CH_SBR_WR BIT(21)
  54. #define EDMA_V3_CH_CSR_ERQ BIT(0)
  55. #define EDMA_V3_CH_CSR_EARQ BIT(1)
  56. #define EDMA_V3_CH_CSR_EEI BIT(2)
  57. #define EDMA_V3_CH_CSR_DONE BIT(30)
  58. #define EDMA_V3_CH_CSR_ACTIVE BIT(31)
  59. enum fsl_edma_pm_state {
  60. RUNNING = 0,
  61. SUSPENDED,
  62. };
  63. struct fsl_edma_hw_tcd {
  64. __le32 saddr;
  65. __le16 soff;
  66. __le16 attr;
  67. __le32 nbytes;
  68. __le32 slast;
  69. __le32 daddr;
  70. __le16 doff;
  71. __le16 citer;
  72. __le32 dlast_sga;
  73. __le16 csr;
  74. __le16 biter;
  75. };
  76. struct fsl_edma_hw_tcd64 {
  77. __le64 saddr;
  78. __le16 soff;
  79. __le16 attr;
  80. __le32 nbytes;
  81. __le64 slast;
  82. __le64 daddr;
  83. __le64 dlast_sga;
  84. __le16 doff;
  85. __le16 citer;
  86. __le16 csr;
  87. __le16 biter;
  88. } __packed;
  89. struct fsl_edma3_ch_reg {
  90. __le32 ch_csr;
  91. __le32 ch_es;
  92. __le32 ch_int;
  93. __le32 ch_sbr;
  94. __le32 ch_pri;
  95. __le32 ch_mux;
  96. __le32 ch_mattr; /* edma4, reserved for edma3 */
  97. __le32 ch_reserved;
  98. union {
  99. struct fsl_edma_hw_tcd tcd;
  100. struct fsl_edma_hw_tcd64 tcd64;
  101. };
  102. } __packed;
  103. /*
  104. * These are iomem pointers, for both v32 and v64.
  105. */
  106. struct edma_regs {
  107. void __iomem *cr;
  108. void __iomem *es;
  109. void __iomem *erqh;
  110. void __iomem *erql; /* aka erq on v32 */
  111. void __iomem *eeih;
  112. void __iomem *eeil; /* aka eei on v32 */
  113. void __iomem *seei;
  114. void __iomem *ceei;
  115. void __iomem *serq;
  116. void __iomem *cerq;
  117. void __iomem *cint;
  118. void __iomem *cerr;
  119. void __iomem *ssrt;
  120. void __iomem *cdne;
  121. void __iomem *inth;
  122. void __iomem *intl;
  123. void __iomem *errh;
  124. void __iomem *errl;
  125. };
  126. struct fsl_edma_sw_tcd {
  127. dma_addr_t ptcd;
  128. void *vtcd;
  129. };
  130. struct fsl_edma_chan {
  131. struct virt_dma_chan vchan;
  132. enum dma_status status;
  133. enum fsl_edma_pm_state pm_state;
  134. struct fsl_edma_engine *edma;
  135. struct fsl_edma_desc *edesc;
  136. struct dma_slave_config cfg;
  137. u32 attr;
  138. bool is_sw;
  139. struct dma_pool *tcd_pool;
  140. dma_addr_t dma_dev_addr;
  141. u32 dma_dev_size;
  142. enum dma_data_direction dma_dir;
  143. char chan_name[32];
  144. void __iomem *tcd;
  145. void __iomem *mux_addr;
  146. u32 real_count;
  147. struct work_struct issue_worker;
  148. struct platform_device *pdev;
  149. struct device *pd_dev;
  150. struct device_link *pd_dev_link;
  151. u32 srcid;
  152. struct clk *clk;
  153. int priority;
  154. int hw_chanid;
  155. int txirq;
  156. irqreturn_t (*irq_handler)(int irq, void *dev_id);
  157. bool is_rxchan;
  158. bool is_remote;
  159. bool is_multi_fifo;
  160. };
  161. struct fsl_edma_desc {
  162. struct virt_dma_desc vdesc;
  163. struct fsl_edma_chan *echan;
  164. bool iscyclic;
  165. enum dma_transfer_direction dirn;
  166. unsigned int n_tcds;
  167. struct fsl_edma_sw_tcd tcd[];
  168. };
  169. #define FSL_EDMA_DRV_HAS_DMACLK BIT(0)
  170. #define FSL_EDMA_DRV_MUX_SWAP BIT(1)
  171. #define FSL_EDMA_DRV_CONFIG32 BIT(2)
  172. #define FSL_EDMA_DRV_WRAP_IO BIT(3)
  173. #define FSL_EDMA_DRV_EDMA64 BIT(4)
  174. #define FSL_EDMA_DRV_HAS_PD BIT(5)
  175. #define FSL_EDMA_DRV_HAS_CHCLK BIT(6)
  176. #define FSL_EDMA_DRV_HAS_CHMUX BIT(7)
  177. #define FSL_EDMA_DRV_MEM_REMOTE BIT(8)
  178. /* control and status register is in tcd address space, edma3 reg layout */
  179. #define FSL_EDMA_DRV_SPLIT_REG BIT(9)
  180. #define FSL_EDMA_DRV_BUS_8BYTE BIT(10)
  181. #define FSL_EDMA_DRV_DEV_TO_DEV BIT(11)
  182. #define FSL_EDMA_DRV_ALIGN_64BYTE BIT(12)
  183. /* Need clean CHn_CSR DONE before enable TCD's ESG */
  184. #define FSL_EDMA_DRV_CLEAR_DONE_E_SG BIT(13)
  185. /* Need clean CHn_CSR DONE before enable TCD's MAJORELINK */
  186. #define FSL_EDMA_DRV_CLEAR_DONE_E_LINK BIT(14)
  187. #define FSL_EDMA_DRV_TCD64 BIT(15)
  188. #define FSL_EDMA_DRV_EDMA3 (FSL_EDMA_DRV_SPLIT_REG | \
  189. FSL_EDMA_DRV_BUS_8BYTE | \
  190. FSL_EDMA_DRV_DEV_TO_DEV | \
  191. FSL_EDMA_DRV_ALIGN_64BYTE | \
  192. FSL_EDMA_DRV_CLEAR_DONE_E_SG | \
  193. FSL_EDMA_DRV_CLEAR_DONE_E_LINK)
  194. #define FSL_EDMA_DRV_EDMA4 (FSL_EDMA_DRV_SPLIT_REG | \
  195. FSL_EDMA_DRV_BUS_8BYTE | \
  196. FSL_EDMA_DRV_DEV_TO_DEV | \
  197. FSL_EDMA_DRV_ALIGN_64BYTE | \
  198. FSL_EDMA_DRV_CLEAR_DONE_E_LINK)
  199. struct fsl_edma_drvdata {
  200. u32 dmamuxs; /* only used before v3 */
  201. u32 chreg_off;
  202. u32 chreg_space_sz;
  203. u32 flags;
  204. u32 mux_off; /* channel mux register offset */
  205. u32 mux_skip; /* how much skip for each channel */
  206. int (*setup_irq)(struct platform_device *pdev,
  207. struct fsl_edma_engine *fsl_edma);
  208. };
  209. struct fsl_edma_engine {
  210. struct dma_device dma_dev;
  211. void __iomem *membase;
  212. void __iomem *muxbase[DMAMUX_NR];
  213. struct clk *muxclk[DMAMUX_NR];
  214. struct clk *dmaclk;
  215. struct mutex fsl_edma_mutex;
  216. const struct fsl_edma_drvdata *drvdata;
  217. u32 n_chans;
  218. int txirq;
  219. int errirq;
  220. bool big_endian;
  221. struct edma_regs regs;
  222. u64 chan_masked;
  223. struct fsl_edma_chan chans[] __counted_by(n_chans);
  224. };
  225. static inline u32 fsl_edma_drvflags(struct fsl_edma_chan *fsl_chan)
  226. {
  227. return fsl_chan->edma->drvdata->flags;
  228. }
  229. #define edma_read_tcdreg_c(chan, _tcd, __name) \
  230. _Generic(((_tcd)->__name), \
  231. __iomem __le64 : edma_readq(chan->edma, &(_tcd)->__name), \
  232. __iomem __le32 : edma_readl(chan->edma, &(_tcd)->__name), \
  233. __iomem __le16 : edma_readw(chan->edma, &(_tcd)->__name) \
  234. )
  235. #define edma_read_tcdreg(chan, __name) \
  236. ((fsl_edma_drvflags(chan) & FSL_EDMA_DRV_TCD64) ? \
  237. edma_read_tcdreg_c(chan, ((struct fsl_edma_hw_tcd64 __iomem *)chan->tcd), __name) : \
  238. edma_read_tcdreg_c(chan, ((struct fsl_edma_hw_tcd __iomem *)chan->tcd), __name) \
  239. )
  240. #define edma_write_tcdreg_c(chan, _tcd, _val, __name) \
  241. _Generic((_tcd->__name), \
  242. __iomem __le64 : edma_writeq(chan->edma, (u64 __force)(_val), &_tcd->__name), \
  243. __iomem __le32 : edma_writel(chan->edma, (u32 __force)(_val), &_tcd->__name), \
  244. __iomem __le16 : edma_writew(chan->edma, (u16 __force)(_val), &_tcd->__name), \
  245. __iomem u8 : edma_writeb(chan->edma, _val, &_tcd->__name) \
  246. )
  247. #define edma_write_tcdreg(chan, val, __name) \
  248. do { \
  249. struct fsl_edma_hw_tcd64 __iomem *tcd64_r = (struct fsl_edma_hw_tcd64 __iomem *)chan->tcd; \
  250. struct fsl_edma_hw_tcd __iomem *tcd_r = (struct fsl_edma_hw_tcd __iomem *)chan->tcd; \
  251. \
  252. if (fsl_edma_drvflags(chan) & FSL_EDMA_DRV_TCD64) \
  253. edma_write_tcdreg_c(chan, tcd64_r, val, __name); \
  254. else \
  255. edma_write_tcdreg_c(chan, tcd_r, val, __name); \
  256. } while (0)
  257. #define edma_cp_tcd_to_reg(chan, __tcd, __name) \
  258. do { \
  259. struct fsl_edma_hw_tcd64 __iomem *tcd64_r = (struct fsl_edma_hw_tcd64 __iomem *)chan->tcd; \
  260. struct fsl_edma_hw_tcd __iomem *tcd_r = (struct fsl_edma_hw_tcd __iomem *)chan->tcd; \
  261. struct fsl_edma_hw_tcd64 *tcd64_m = (struct fsl_edma_hw_tcd64 *)__tcd; \
  262. struct fsl_edma_hw_tcd *tcd_m = (struct fsl_edma_hw_tcd *)__tcd; \
  263. \
  264. if (fsl_edma_drvflags(chan) & FSL_EDMA_DRV_TCD64) \
  265. edma_write_tcdreg_c(chan, tcd64_r, tcd64_m->__name, __name); \
  266. else \
  267. edma_write_tcdreg_c(chan, tcd_r, tcd_m->__name, __name); \
  268. } while (0)
  269. #define edma_readl_chreg(chan, __name) \
  270. edma_readl(chan->edma, \
  271. (void __iomem *)&(container_of(((__force void *)chan->tcd),\
  272. struct fsl_edma3_ch_reg, tcd)->__name))
  273. #define edma_writel_chreg(chan, val, __name) \
  274. edma_writel(chan->edma, val, \
  275. (void __iomem *)&(container_of(((__force void *)chan->tcd),\
  276. struct fsl_edma3_ch_reg, tcd)->__name))
  277. #define fsl_edma_get_tcd(_chan, _tcd, _field) \
  278. (fsl_edma_drvflags(_chan) & FSL_EDMA_DRV_TCD64 ? (((struct fsl_edma_hw_tcd64 *)_tcd)->_field) : \
  279. (((struct fsl_edma_hw_tcd *)_tcd)->_field))
  280. #define fsl_edma_le_to_cpu(x) \
  281. _Generic((x), \
  282. __le64 : le64_to_cpu((x)), \
  283. __le32 : le32_to_cpu((x)), \
  284. __le16 : le16_to_cpu((x)) \
  285. )
  286. #define fsl_edma_get_tcd_to_cpu(_chan, _tcd, _field) \
  287. (fsl_edma_drvflags(_chan) & FSL_EDMA_DRV_TCD64 ? \
  288. fsl_edma_le_to_cpu(((struct fsl_edma_hw_tcd64 *)_tcd)->_field) : \
  289. fsl_edma_le_to_cpu(((struct fsl_edma_hw_tcd *)_tcd)->_field))
  290. #define fsl_edma_set_tcd_to_le_c(_tcd, _val, _field) \
  291. _Generic(((_tcd)->_field), \
  292. __le64 : (_tcd)->_field = cpu_to_le64(_val), \
  293. __le32 : (_tcd)->_field = cpu_to_le32(_val), \
  294. __le16 : (_tcd)->_field = cpu_to_le16(_val) \
  295. )
  296. #define fsl_edma_set_tcd_to_le(_chan, _tcd, _val, _field) \
  297. do { \
  298. if (fsl_edma_drvflags(_chan) & FSL_EDMA_DRV_TCD64) \
  299. fsl_edma_set_tcd_to_le_c((struct fsl_edma_hw_tcd64 *)_tcd, _val, _field); \
  300. else \
  301. fsl_edma_set_tcd_to_le_c((struct fsl_edma_hw_tcd *)_tcd, _val, _field); \
  302. } while (0)
  303. /* Need after struct defination */
  304. #include "fsl-edma-trace.h"
  305. /*
  306. * R/W functions for big- or little-endian registers:
  307. * The eDMA controller's endian is independent of the CPU core's endian.
  308. * For the big-endian IP module, the offset for 8-bit or 16-bit registers
  309. * should also be swapped opposite to that in little-endian IP.
  310. */
  311. static inline u64 edma_readq(struct fsl_edma_engine *edma, void __iomem *addr)
  312. {
  313. u64 l, h;
  314. if (edma->big_endian) {
  315. l = ioread32be(addr);
  316. h = ioread32be(addr + 4);
  317. } else {
  318. l = ioread32(addr);
  319. h = ioread32(addr + 4);
  320. }
  321. trace_edma_readl(edma, addr, l);
  322. trace_edma_readl(edma, addr + 4, h);
  323. return (h << 32) | l;
  324. }
  325. static inline u32 edma_readl(struct fsl_edma_engine *edma, void __iomem *addr)
  326. {
  327. u32 val;
  328. if (edma->big_endian)
  329. val = ioread32be(addr);
  330. else
  331. val = ioread32(addr);
  332. trace_edma_readl(edma, addr, val);
  333. return val;
  334. }
  335. static inline u16 edma_readw(struct fsl_edma_engine *edma, void __iomem *addr)
  336. {
  337. u16 val;
  338. if (edma->big_endian)
  339. val = ioread16be(addr);
  340. else
  341. val = ioread16(addr);
  342. trace_edma_readw(edma, addr, val);
  343. return val;
  344. }
  345. static inline void edma_writeb(struct fsl_edma_engine *edma,
  346. u8 val, void __iomem *addr)
  347. {
  348. /* swap the reg offset for these in big-endian mode */
  349. if (edma->big_endian)
  350. iowrite8(val, (void __iomem *)((unsigned long)addr ^ 0x3));
  351. else
  352. iowrite8(val, addr);
  353. trace_edma_writeb(edma, addr, val);
  354. }
  355. static inline void edma_writew(struct fsl_edma_engine *edma,
  356. u16 val, void __iomem *addr)
  357. {
  358. /* swap the reg offset for these in big-endian mode */
  359. if (edma->big_endian)
  360. iowrite16be(val, (void __iomem *)((unsigned long)addr ^ 0x2));
  361. else
  362. iowrite16(val, addr);
  363. trace_edma_writew(edma, addr, val);
  364. }
  365. static inline void edma_writel(struct fsl_edma_engine *edma,
  366. u32 val, void __iomem *addr)
  367. {
  368. if (edma->big_endian)
  369. iowrite32be(val, addr);
  370. else
  371. iowrite32(val, addr);
  372. trace_edma_writel(edma, addr, val);
  373. }
  374. static inline void edma_writeq(struct fsl_edma_engine *edma,
  375. u64 val, void __iomem *addr)
  376. {
  377. if (edma->big_endian) {
  378. iowrite32be(val & 0xFFFFFFFF, addr);
  379. iowrite32be(val >> 32, addr + 4);
  380. } else {
  381. iowrite32(val & 0xFFFFFFFF, addr);
  382. iowrite32(val >> 32, addr + 4);
  383. }
  384. trace_edma_writel(edma, addr, val & 0xFFFFFFFF);
  385. trace_edma_writel(edma, addr + 4, val >> 32);
  386. }
  387. static inline struct fsl_edma_chan *to_fsl_edma_chan(struct dma_chan *chan)
  388. {
  389. return container_of(chan, struct fsl_edma_chan, vchan.chan);
  390. }
  391. static inline struct fsl_edma_desc *to_fsl_edma_desc(struct virt_dma_desc *vd)
  392. {
  393. return container_of(vd, struct fsl_edma_desc, vdesc);
  394. }
  395. static inline void fsl_edma_err_chan_handler(struct fsl_edma_chan *fsl_chan)
  396. {
  397. fsl_chan->status = DMA_ERROR;
  398. }
  399. void fsl_edma_tx_chan_handler(struct fsl_edma_chan *fsl_chan);
  400. void fsl_edma_disable_request(struct fsl_edma_chan *fsl_chan);
  401. void fsl_edma_chan_mux(struct fsl_edma_chan *fsl_chan,
  402. unsigned int slot, bool enable);
  403. void fsl_edma_free_desc(struct virt_dma_desc *vdesc);
  404. int fsl_edma_terminate_all(struct dma_chan *chan);
  405. int fsl_edma_pause(struct dma_chan *chan);
  406. int fsl_edma_resume(struct dma_chan *chan);
  407. int fsl_edma_slave_config(struct dma_chan *chan,
  408. struct dma_slave_config *cfg);
  409. enum dma_status fsl_edma_tx_status(struct dma_chan *chan,
  410. dma_cookie_t cookie, struct dma_tx_state *txstate);
  411. struct dma_async_tx_descriptor *fsl_edma_prep_dma_cyclic(
  412. struct dma_chan *chan, dma_addr_t dma_addr, size_t buf_len,
  413. size_t period_len, enum dma_transfer_direction direction,
  414. unsigned long flags);
  415. struct dma_async_tx_descriptor *fsl_edma_prep_slave_sg(
  416. struct dma_chan *chan, struct scatterlist *sgl,
  417. unsigned int sg_len, enum dma_transfer_direction direction,
  418. unsigned long flags, void *context);
  419. struct dma_async_tx_descriptor *fsl_edma_prep_memcpy(
  420. struct dma_chan *chan, dma_addr_t dma_dst, dma_addr_t dma_src,
  421. size_t len, unsigned long flags);
  422. void fsl_edma_xfer_desc(struct fsl_edma_chan *fsl_chan);
  423. void fsl_edma_issue_pending(struct dma_chan *chan);
  424. int fsl_edma_alloc_chan_resources(struct dma_chan *chan);
  425. void fsl_edma_free_chan_resources(struct dma_chan *chan);
  426. void fsl_edma_cleanup_vchan(struct dma_device *dmadev);
  427. void fsl_edma_setup_regs(struct fsl_edma_engine *edma);
  428. #endif /* _FSL_EDMA_COMMON_H_ */