regs.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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. /* To access the registers in early stage of probe */
  100. #define dma_read_byaddr(addr, name) \
  101. dma_readl_native((addr) + offsetof(struct dw_dma_regs, name))
  102. /* Bitfields in DW_PARAMS */
  103. #define DW_PARAMS_NR_CHAN 8 /* number of channels */
  104. #define DW_PARAMS_NR_MASTER 11 /* number of AHB masters */
  105. #define DW_PARAMS_DATA_WIDTH(n) (15 + 2 * (n))
  106. #define DW_PARAMS_DATA_WIDTH1 15 /* master 1 data width */
  107. #define DW_PARAMS_DATA_WIDTH2 17 /* master 2 data width */
  108. #define DW_PARAMS_DATA_WIDTH3 19 /* master 3 data width */
  109. #define DW_PARAMS_DATA_WIDTH4 21 /* master 4 data width */
  110. #define DW_PARAMS_EN 28 /* encoded parameters */
  111. /* Bitfields in DWC_PARAMS */
  112. #define DWC_PARAMS_MBLK_EN 11 /* multi block transfer */
  113. /* bursts size */
  114. enum dw_dma_msize {
  115. DW_DMA_MSIZE_1,
  116. DW_DMA_MSIZE_4,
  117. DW_DMA_MSIZE_8,
  118. DW_DMA_MSIZE_16,
  119. DW_DMA_MSIZE_32,
  120. DW_DMA_MSIZE_64,
  121. DW_DMA_MSIZE_128,
  122. DW_DMA_MSIZE_256,
  123. };
  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<<7) /* 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. };
  191. struct dw_dma_chan {
  192. struct dma_chan chan;
  193. void __iomem *ch_regs;
  194. u8 mask;
  195. u8 priority;
  196. enum dma_transfer_direction direction;
  197. bool paused;
  198. bool initialized;
  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 list_head free_list;
  207. u32 residue;
  208. struct dw_cyclic_desc *cdesc;
  209. unsigned int descs_allocated;
  210. /* hardware configuration */
  211. unsigned int block_size;
  212. bool nollp;
  213. /* custom slave configuration */
  214. u8 src_id;
  215. u8 dst_id;
  216. u8 src_master;
  217. u8 dst_master;
  218. /* configuration passed via DMA_SLAVE_CONFIG */
  219. struct dma_slave_config dma_sconfig;
  220. };
  221. static inline struct dw_dma_chan_regs __iomem *
  222. __dwc_regs(struct dw_dma_chan *dwc)
  223. {
  224. return dwc->ch_regs;
  225. }
  226. #define channel_readl(dwc, name) \
  227. dma_readl_native(&(__dwc_regs(dwc)->name))
  228. #define channel_writel(dwc, name, val) \
  229. dma_writel_native((val), &(__dwc_regs(dwc)->name))
  230. static inline struct dw_dma_chan *to_dw_dma_chan(struct dma_chan *chan)
  231. {
  232. return container_of(chan, struct dw_dma_chan, chan);
  233. }
  234. struct dw_dma {
  235. struct dma_device dma;
  236. void __iomem *regs;
  237. struct dma_pool *desc_pool;
  238. struct tasklet_struct tasklet;
  239. /* channels */
  240. struct dw_dma_chan *chan;
  241. u8 all_chan_mask;
  242. u8 in_use;
  243. /* hardware configuration */
  244. unsigned char nr_masters;
  245. unsigned char data_width[4];
  246. };
  247. static inline struct dw_dma_regs __iomem *__dw_regs(struct dw_dma *dw)
  248. {
  249. return dw->regs;
  250. }
  251. #define dma_readl(dw, name) \
  252. dma_readl_native(&(__dw_regs(dw)->name))
  253. #define dma_writel(dw, name, val) \
  254. dma_writel_native((val), &(__dw_regs(dw)->name))
  255. #define channel_set_bit(dw, reg, mask) \
  256. dma_writel(dw, reg, ((mask) << 8) | (mask))
  257. #define channel_clear_bit(dw, reg, mask) \
  258. dma_writel(dw, reg, ((mask) << 8) | 0)
  259. static inline struct dw_dma *to_dw_dma(struct dma_device *ddev)
  260. {
  261. return container_of(ddev, struct dw_dma, dma);
  262. }
  263. /* LLI == Linked List Item; a.k.a. DMA block descriptor */
  264. struct dw_lli {
  265. /* values that are not changed by hardware */
  266. u32 sar;
  267. u32 dar;
  268. u32 llp; /* chain to next lli */
  269. u32 ctllo;
  270. /* values that may get written back: */
  271. u32 ctlhi;
  272. /* sstat and dstat can snapshot peripheral register state.
  273. * silicon config may discard either or both...
  274. */
  275. u32 sstat;
  276. u32 dstat;
  277. };
  278. struct dw_desc {
  279. /* FIRST values the hardware uses */
  280. struct dw_lli lli;
  281. /* THEN values for driver housekeeping */
  282. struct list_head desc_node;
  283. struct list_head tx_list;
  284. struct dma_async_tx_descriptor txd;
  285. size_t len;
  286. size_t total_len;
  287. };
  288. #define to_dw_desc(h) list_entry(h, struct dw_desc, desc_node)
  289. static inline struct dw_desc *
  290. txd_to_dw_desc(struct dma_async_tx_descriptor *txd)
  291. {
  292. return container_of(txd, struct dw_desc, txd);
  293. }