regs.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. /*
  2. * Driver for the Synopsys DesignWare AHB DMA Controller
  3. *
  4. * Copyright (C) 2005-2007 Atmel Corporation
  5. * Copyright (C) 2010-2011 ST Microelectronics
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <linux/interrupt.h>
  12. #include <linux/dmaengine.h>
  13. #include <linux/dw_dmac.h>
  14. #define DW_DMA_MAX_NR_CHANNELS 8
  15. #define DW_DMA_MAX_NR_REQUESTS 16
  16. /* flow controller */
  17. enum dw_dma_fc {
  18. DW_DMA_FC_D_M2M,
  19. DW_DMA_FC_D_M2P,
  20. DW_DMA_FC_D_P2M,
  21. DW_DMA_FC_D_P2P,
  22. DW_DMA_FC_P_P2M,
  23. DW_DMA_FC_SP_P2P,
  24. DW_DMA_FC_P_M2P,
  25. DW_DMA_FC_DP_P2P,
  26. };
  27. /*
  28. * Redefine this macro to handle differences between 32- and 64-bit
  29. * addressing, big vs. little endian, etc.
  30. */
  31. #define DW_REG(name) u32 name; u32 __pad_##name
  32. /* Hardware register definitions. */
  33. struct dw_dma_chan_regs {
  34. DW_REG(SAR); /* Source Address Register */
  35. DW_REG(DAR); /* Destination Address Register */
  36. DW_REG(LLP); /* Linked List Pointer */
  37. u32 CTL_LO; /* Control Register Low */
  38. u32 CTL_HI; /* Control Register High */
  39. DW_REG(SSTAT);
  40. DW_REG(DSTAT);
  41. DW_REG(SSTATAR);
  42. DW_REG(DSTATAR);
  43. u32 CFG_LO; /* Configuration Register Low */
  44. u32 CFG_HI; /* Configuration Register High */
  45. DW_REG(SGR);
  46. DW_REG(DSR);
  47. };
  48. struct dw_dma_irq_regs {
  49. DW_REG(XFER);
  50. DW_REG(BLOCK);
  51. DW_REG(SRC_TRAN);
  52. DW_REG(DST_TRAN);
  53. DW_REG(ERROR);
  54. };
  55. struct dw_dma_regs {
  56. /* per-channel registers */
  57. struct dw_dma_chan_regs CHAN[DW_DMA_MAX_NR_CHANNELS];
  58. /* irq handling */
  59. struct dw_dma_irq_regs RAW; /* r */
  60. struct dw_dma_irq_regs STATUS; /* r (raw & mask) */
  61. struct dw_dma_irq_regs MASK; /* rw (set = irq enabled) */
  62. struct dw_dma_irq_regs CLEAR; /* w (ack, affects "raw") */
  63. DW_REG(STATUS_INT); /* r */
  64. /* software handshaking */
  65. DW_REG(REQ_SRC);
  66. DW_REG(REQ_DST);
  67. DW_REG(SGL_REQ_SRC);
  68. DW_REG(SGL_REQ_DST);
  69. DW_REG(LAST_SRC);
  70. DW_REG(LAST_DST);
  71. /* miscellaneous */
  72. DW_REG(CFG);
  73. DW_REG(CH_EN);
  74. DW_REG(ID);
  75. DW_REG(TEST);
  76. /* reserved */
  77. DW_REG(__reserved0);
  78. DW_REG(__reserved1);
  79. /* optional encoded params, 0x3c8..0x3f7 */
  80. u32 __reserved;
  81. /* per-channel configuration registers */
  82. u32 DWC_PARAMS[DW_DMA_MAX_NR_CHANNELS];
  83. u32 MULTI_BLK_TYPE;
  84. u32 MAX_BLK_SIZE;
  85. /* top-level parameters */
  86. u32 DW_PARAMS;
  87. };
  88. /*
  89. * Big endian I/O access when reading and writing to the DMA controller
  90. * registers. This is needed on some platforms, like the Atmel AVR32
  91. * architecture.
  92. */
  93. #ifdef CONFIG_DW_DMAC_BIG_ENDIAN_IO
  94. #define dma_readl_native ioread32be
  95. #define dma_writel_native iowrite32be
  96. #else
  97. #define dma_readl_native readl
  98. #define dma_writel_native writel
  99. #endif
  100. /* To access the registers in early stage of probe */
  101. #define dma_read_byaddr(addr, name) \
  102. dma_readl_native((addr) + offsetof(struct dw_dma_regs, name))
  103. /* Bitfields in DW_PARAMS */
  104. #define DW_PARAMS_NR_CHAN 8 /* number of channels */
  105. #define DW_PARAMS_NR_MASTER 11 /* number of AHB masters */
  106. #define DW_PARAMS_DATA_WIDTH(n) (15 + 2 * (n))
  107. #define DW_PARAMS_DATA_WIDTH1 15 /* master 1 data width */
  108. #define DW_PARAMS_DATA_WIDTH2 17 /* master 2 data width */
  109. #define DW_PARAMS_DATA_WIDTH3 19 /* master 3 data width */
  110. #define DW_PARAMS_DATA_WIDTH4 21 /* master 4 data width */
  111. #define DW_PARAMS_EN 28 /* encoded parameters */
  112. /* Bitfields in DWC_PARAMS */
  113. #define DWC_PARAMS_MBLK_EN 11 /* multi block transfer */
  114. /* Bitfields in CTL_LO */
  115. #define DWC_CTLL_INT_EN (1 << 0) /* irqs enabled? */
  116. #define DWC_CTLL_DST_WIDTH(n) ((n)<<1) /* bytes per element */
  117. #define DWC_CTLL_SRC_WIDTH(n) ((n)<<4)
  118. #define DWC_CTLL_DST_INC (0<<7) /* DAR update/not */
  119. #define DWC_CTLL_DST_DEC (1<<7)
  120. #define DWC_CTLL_DST_FIX (2<<7)
  121. #define DWC_CTLL_SRC_INC (0<<7) /* SAR update/not */
  122. #define DWC_CTLL_SRC_DEC (1<<9)
  123. #define DWC_CTLL_SRC_FIX (2<<9)
  124. #define DWC_CTLL_DST_MSIZE(n) ((n)<<11) /* burst, #elements */
  125. #define DWC_CTLL_SRC_MSIZE(n) ((n)<<14)
  126. #define DWC_CTLL_S_GATH_EN (1 << 17) /* src gather, !FIX */
  127. #define DWC_CTLL_D_SCAT_EN (1 << 18) /* dst scatter, !FIX */
  128. #define DWC_CTLL_FC(n) ((n) << 20)
  129. #define DWC_CTLL_FC_M2M (0 << 20) /* mem-to-mem */
  130. #define DWC_CTLL_FC_M2P (1 << 20) /* mem-to-periph */
  131. #define DWC_CTLL_FC_P2M (2 << 20) /* periph-to-mem */
  132. #define DWC_CTLL_FC_P2P (3 << 20) /* periph-to-periph */
  133. /* plus 4 transfer types for peripheral-as-flow-controller */
  134. #define DWC_CTLL_DMS(n) ((n)<<23) /* dst master select */
  135. #define DWC_CTLL_SMS(n) ((n)<<25) /* src master select */
  136. #define DWC_CTLL_LLP_D_EN (1 << 27) /* dest block chain */
  137. #define DWC_CTLL_LLP_S_EN (1 << 28) /* src block chain */
  138. /* Bitfields in CTL_HI */
  139. #define DWC_CTLH_DONE 0x00001000
  140. #define DWC_CTLH_BLOCK_TS_MASK 0x00000fff
  141. /* Bitfields in CFG_LO. Platform-configurable bits are in <linux/dw_dmac.h> */
  142. #define DWC_CFGL_CH_PRIOR_MASK (0x7 << 5) /* priority mask */
  143. #define DWC_CFGL_CH_PRIOR(x) ((x) << 5) /* priority */
  144. #define DWC_CFGL_CH_SUSP (1 << 8) /* pause xfer */
  145. #define DWC_CFGL_FIFO_EMPTY (1 << 9) /* pause xfer */
  146. #define DWC_CFGL_HS_DST (1 << 10) /* handshake w/dst */
  147. #define DWC_CFGL_HS_SRC (1 << 11) /* handshake w/src */
  148. #define DWC_CFGL_MAX_BURST(x) ((x) << 20)
  149. #define DWC_CFGL_RELOAD_SAR (1 << 30)
  150. #define DWC_CFGL_RELOAD_DAR (1 << 31)
  151. /* Bitfields in CFG_HI. Platform-configurable bits are in <linux/dw_dmac.h> */
  152. #define DWC_CFGH_DS_UPD_EN (1 << 5)
  153. #define DWC_CFGH_SS_UPD_EN (1 << 6)
  154. /* Bitfields in SGR */
  155. #define DWC_SGR_SGI(x) ((x) << 0)
  156. #define DWC_SGR_SGC(x) ((x) << 20)
  157. /* Bitfields in DSR */
  158. #define DWC_DSR_DSI(x) ((x) << 0)
  159. #define DWC_DSR_DSC(x) ((x) << 20)
  160. /* Bitfields in CFG */
  161. #define DW_CFG_DMA_EN (1 << 0)
  162. enum dw_dmac_flags {
  163. DW_DMA_IS_CYCLIC = 0,
  164. DW_DMA_IS_SOFT_LLP = 1,
  165. };
  166. struct dw_dma_chan {
  167. struct dma_chan chan;
  168. void __iomem *ch_regs;
  169. u8 mask;
  170. u8 priority;
  171. enum dma_transfer_direction direction;
  172. bool paused;
  173. bool initialized;
  174. /* software emulation of the LLP transfers */
  175. struct list_head *tx_node_active;
  176. spinlock_t lock;
  177. /* these other elements are all protected by lock */
  178. unsigned long flags;
  179. struct list_head active_list;
  180. struct list_head queue;
  181. struct list_head free_list;
  182. u32 residue;
  183. struct dw_cyclic_desc *cdesc;
  184. unsigned int descs_allocated;
  185. /* hardware configuration */
  186. unsigned int block_size;
  187. bool nollp;
  188. /* custom slave configuration */
  189. unsigned int request_line;
  190. unsigned char src_master;
  191. unsigned char dst_master;
  192. /* configuration passed via DMA_SLAVE_CONFIG */
  193. struct dma_slave_config dma_sconfig;
  194. };
  195. static inline struct dw_dma_chan_regs __iomem *
  196. __dwc_regs(struct dw_dma_chan *dwc)
  197. {
  198. return dwc->ch_regs;
  199. }
  200. #define channel_readl(dwc, name) \
  201. dma_readl_native(&(__dwc_regs(dwc)->name))
  202. #define channel_writel(dwc, name, val) \
  203. dma_writel_native((val), &(__dwc_regs(dwc)->name))
  204. static inline struct dw_dma_chan *to_dw_dma_chan(struct dma_chan *chan)
  205. {
  206. return container_of(chan, struct dw_dma_chan, chan);
  207. }
  208. struct dw_dma {
  209. struct dma_device dma;
  210. void __iomem *regs;
  211. struct dma_pool *desc_pool;
  212. struct tasklet_struct tasklet;
  213. struct clk *clk;
  214. u8 all_chan_mask;
  215. /* hardware configuration */
  216. unsigned char nr_masters;
  217. unsigned char data_width[4];
  218. struct dw_dma_chan chan[0];
  219. };
  220. static inline struct dw_dma_regs __iomem *__dw_regs(struct dw_dma *dw)
  221. {
  222. return dw->regs;
  223. }
  224. #define dma_readl(dw, name) \
  225. dma_readl_native(&(__dw_regs(dw)->name))
  226. #define dma_writel(dw, name, val) \
  227. dma_writel_native((val), &(__dw_regs(dw)->name))
  228. #define channel_set_bit(dw, reg, mask) \
  229. dma_writel(dw, reg, ((mask) << 8) | (mask))
  230. #define channel_clear_bit(dw, reg, mask) \
  231. dma_writel(dw, reg, ((mask) << 8) | 0)
  232. static inline struct dw_dma *to_dw_dma(struct dma_device *ddev)
  233. {
  234. return container_of(ddev, struct dw_dma, dma);
  235. }
  236. /* LLI == Linked List Item; a.k.a. DMA block descriptor */
  237. struct dw_lli {
  238. /* values that are not changed by hardware */
  239. u32 sar;
  240. u32 dar;
  241. u32 llp; /* chain to next lli */
  242. u32 ctllo;
  243. /* values that may get written back: */
  244. u32 ctlhi;
  245. /* sstat and dstat can snapshot peripheral register state.
  246. * silicon config may discard either or both...
  247. */
  248. u32 sstat;
  249. u32 dstat;
  250. };
  251. struct dw_desc {
  252. /* FIRST values the hardware uses */
  253. struct dw_lli lli;
  254. /* THEN values for driver housekeeping */
  255. struct list_head desc_node;
  256. struct list_head tx_list;
  257. struct dma_async_tx_descriptor txd;
  258. size_t len;
  259. size_t total_len;
  260. };
  261. #define to_dw_desc(h) list_entry(h, struct dw_desc, desc_node)
  262. static inline struct dw_desc *
  263. txd_to_dw_desc(struct dma_async_tx_descriptor *txd)
  264. {
  265. return container_of(txd, struct dw_desc, txd);
  266. }