regs.h 9.7 KB

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