regs.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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. * Copyright (C) 2016 Intel Corporation
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. */
  12. #include <linux/bitops.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/dmaengine.h>
  15. #include <linux/io-64-nonatomic-hi-lo.h>
  16. #include "internal.h"
  17. #define DW_DMA_MAX_NR_REQUESTS 16
  18. /* flow controller */
  19. enum dw_dma_fc {
  20. DW_DMA_FC_D_M2M,
  21. DW_DMA_FC_D_M2P,
  22. DW_DMA_FC_D_P2M,
  23. DW_DMA_FC_D_P2P,
  24. DW_DMA_FC_P_P2M,
  25. DW_DMA_FC_SP_P2P,
  26. DW_DMA_FC_P_M2P,
  27. DW_DMA_FC_DP_P2P,
  28. };
  29. /*
  30. * Redefine this macro to handle differences between 32- and 64-bit
  31. * addressing, big vs. little endian, etc.
  32. */
  33. #define DW_REG(name) u32 name; u32 __pad_##name
  34. /* Hardware register definitions. */
  35. struct dw_dma_chan_regs {
  36. DW_REG(SAR); /* Source Address Register */
  37. DW_REG(DAR); /* Destination Address Register */
  38. DW_REG(LLP); /* Linked List Pointer */
  39. u32 CTL_LO; /* Control Register Low */
  40. u32 CTL_HI; /* Control Register High */
  41. DW_REG(SSTAT);
  42. DW_REG(DSTAT);
  43. DW_REG(SSTATAR);
  44. DW_REG(DSTATAR);
  45. u32 CFG_LO; /* Configuration Register Low */
  46. u32 CFG_HI; /* Configuration Register High */
  47. DW_REG(SGR);
  48. DW_REG(DSR);
  49. };
  50. struct dw_dma_irq_regs {
  51. DW_REG(XFER);
  52. DW_REG(BLOCK);
  53. DW_REG(SRC_TRAN);
  54. DW_REG(DST_TRAN);
  55. DW_REG(ERROR);
  56. };
  57. struct dw_dma_regs {
  58. /* per-channel registers */
  59. struct dw_dma_chan_regs CHAN[DW_DMA_MAX_NR_CHANNELS];
  60. /* irq handling */
  61. struct dw_dma_irq_regs RAW; /* r */
  62. struct dw_dma_irq_regs STATUS; /* r (raw & mask) */
  63. struct dw_dma_irq_regs MASK; /* rw (set = irq enabled) */
  64. struct dw_dma_irq_regs CLEAR; /* w (ack, affects "raw") */
  65. DW_REG(STATUS_INT); /* r */
  66. /* software handshaking */
  67. DW_REG(REQ_SRC);
  68. DW_REG(REQ_DST);
  69. DW_REG(SGL_REQ_SRC);
  70. DW_REG(SGL_REQ_DST);
  71. DW_REG(LAST_SRC);
  72. DW_REG(LAST_DST);
  73. /* miscellaneous */
  74. DW_REG(CFG);
  75. DW_REG(CH_EN);
  76. DW_REG(ID);
  77. DW_REG(TEST);
  78. /* iDMA 32-bit support */
  79. DW_REG(CLASS_PRIORITY0);
  80. DW_REG(CLASS_PRIORITY1);
  81. /* optional encoded params, 0x3c8..0x3f7 */
  82. u32 __reserved;
  83. /* per-channel configuration registers */
  84. u32 DWC_PARAMS[DW_DMA_MAX_NR_CHANNELS];
  85. u32 MULTI_BLK_TYPE;
  86. u32 MAX_BLK_SIZE;
  87. /* top-level parameters */
  88. u32 DW_PARAMS;
  89. /* component ID */
  90. u32 COMP_TYPE;
  91. u32 COMP_VERSION;
  92. /* iDMA 32-bit support */
  93. DW_REG(FIFO_PARTITION0);
  94. DW_REG(FIFO_PARTITION1);
  95. DW_REG(SAI_ERR);
  96. DW_REG(GLOBAL_CFG);
  97. };
  98. /* Bitfields in DW_PARAMS */
  99. #define DW_PARAMS_NR_CHAN 8 /* number of channels */
  100. #define DW_PARAMS_NR_MASTER 11 /* number of AHB masters */
  101. #define DW_PARAMS_DATA_WIDTH(n) (15 + 2 * (n))
  102. #define DW_PARAMS_DATA_WIDTH1 15 /* master 1 data width */
  103. #define DW_PARAMS_DATA_WIDTH2 17 /* master 2 data width */
  104. #define DW_PARAMS_DATA_WIDTH3 19 /* master 3 data width */
  105. #define DW_PARAMS_DATA_WIDTH4 21 /* master 4 data width */
  106. #define DW_PARAMS_EN 28 /* encoded parameters */
  107. /* Bitfields in DWC_PARAMS */
  108. #define DWC_PARAMS_MBLK_EN 11 /* multi block transfer */
  109. /* bursts size */
  110. enum dw_dma_msize {
  111. DW_DMA_MSIZE_1,
  112. DW_DMA_MSIZE_4,
  113. DW_DMA_MSIZE_8,
  114. DW_DMA_MSIZE_16,
  115. DW_DMA_MSIZE_32,
  116. DW_DMA_MSIZE_64,
  117. DW_DMA_MSIZE_128,
  118. DW_DMA_MSIZE_256,
  119. };
  120. /* Bitfields in LLP */
  121. #define DWC_LLP_LMS(x) ((x) & 3) /* list master select */
  122. #define DWC_LLP_LOC(x) ((x) & ~3) /* next lli */
  123. /* Bitfields in CTL_LO */
  124. #define DWC_CTLL_INT_EN (1 << 0) /* irqs enabled? */
  125. #define DWC_CTLL_DST_WIDTH(n) ((n)<<1) /* bytes per element */
  126. #define DWC_CTLL_SRC_WIDTH(n) ((n)<<4)
  127. #define DWC_CTLL_DST_INC (0<<7) /* DAR update/not */
  128. #define DWC_CTLL_DST_DEC (1<<7)
  129. #define DWC_CTLL_DST_FIX (2<<7)
  130. #define DWC_CTLL_SRC_INC (0<<9) /* SAR update/not */
  131. #define DWC_CTLL_SRC_DEC (1<<9)
  132. #define DWC_CTLL_SRC_FIX (2<<9)
  133. #define DWC_CTLL_DST_MSIZE(n) ((n)<<11) /* burst, #elements */
  134. #define DWC_CTLL_SRC_MSIZE(n) ((n)<<14)
  135. #define DWC_CTLL_S_GATH_EN (1 << 17) /* src gather, !FIX */
  136. #define DWC_CTLL_D_SCAT_EN (1 << 18) /* dst scatter, !FIX */
  137. #define DWC_CTLL_FC(n) ((n) << 20)
  138. #define DWC_CTLL_FC_M2M (0 << 20) /* mem-to-mem */
  139. #define DWC_CTLL_FC_M2P (1 << 20) /* mem-to-periph */
  140. #define DWC_CTLL_FC_P2M (2 << 20) /* periph-to-mem */
  141. #define DWC_CTLL_FC_P2P (3 << 20) /* periph-to-periph */
  142. /* plus 4 transfer types for peripheral-as-flow-controller */
  143. #define DWC_CTLL_DMS(n) ((n)<<23) /* dst master select */
  144. #define DWC_CTLL_SMS(n) ((n)<<25) /* src master select */
  145. #define DWC_CTLL_LLP_D_EN (1 << 27) /* dest block chain */
  146. #define DWC_CTLL_LLP_S_EN (1 << 28) /* src block chain */
  147. /* Bitfields in CTL_HI */
  148. #define DWC_CTLH_BLOCK_TS_MASK GENMASK(11, 0)
  149. #define DWC_CTLH_BLOCK_TS(x) ((x) & DWC_CTLH_BLOCK_TS_MASK)
  150. #define DWC_CTLH_DONE (1 << 12)
  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_PROTCTL_DATA (0 << 2) /* data access - always set */
  176. #define DWC_CFGH_PROTCTL_PRIV (1 << 2) /* privileged -> AHB HPROT[1] */
  177. #define DWC_CFGH_PROTCTL_BUFFER (2 << 2) /* bufferable -> AHB HPROT[2] */
  178. #define DWC_CFGH_PROTCTL_CACHE (4 << 2) /* cacheable -> AHB HPROT[3] */
  179. #define DWC_CFGH_DS_UPD_EN (1 << 5)
  180. #define DWC_CFGH_SS_UPD_EN (1 << 6)
  181. #define DWC_CFGH_SRC_PER(x) ((x) << 7)
  182. #define DWC_CFGH_DST_PER(x) ((x) << 11)
  183. /* Bitfields in SGR */
  184. #define DWC_SGR_SGI(x) ((x) << 0)
  185. #define DWC_SGR_SGC(x) ((x) << 20)
  186. /* Bitfields in DSR */
  187. #define DWC_DSR_DSI(x) ((x) << 0)
  188. #define DWC_DSR_DSC(x) ((x) << 20)
  189. /* Bitfields in CFG */
  190. #define DW_CFG_DMA_EN (1 << 0)
  191. /* iDMA 32-bit support */
  192. /* Bitfields in CTL_HI */
  193. #define IDMA32C_CTLH_BLOCK_TS_MASK GENMASK(16, 0)
  194. #define IDMA32C_CTLH_BLOCK_TS(x) ((x) & IDMA32C_CTLH_BLOCK_TS_MASK)
  195. #define IDMA32C_CTLH_DONE (1 << 17)
  196. /* Bitfields in CFG_LO */
  197. #define IDMA32C_CFGL_DST_BURST_ALIGN (1 << 0) /* dst burst align */
  198. #define IDMA32C_CFGL_SRC_BURST_ALIGN (1 << 1) /* src burst align */
  199. #define IDMA32C_CFGL_CH_DRAIN (1 << 10) /* drain FIFO */
  200. #define IDMA32C_CFGL_DST_OPT_BL (1 << 20) /* optimize dst burst length */
  201. #define IDMA32C_CFGL_SRC_OPT_BL (1 << 21) /* optimize src burst length */
  202. /* Bitfields in CFG_HI */
  203. #define IDMA32C_CFGH_SRC_PER(x) ((x) << 0)
  204. #define IDMA32C_CFGH_DST_PER(x) ((x) << 4)
  205. #define IDMA32C_CFGH_RD_ISSUE_THD(x) ((x) << 8)
  206. #define IDMA32C_CFGH_RW_ISSUE_THD(x) ((x) << 18)
  207. #define IDMA32C_CFGH_SRC_PER_EXT(x) ((x) << 28) /* src peripheral extension */
  208. #define IDMA32C_CFGH_DST_PER_EXT(x) ((x) << 30) /* dst peripheral extension */
  209. /* Bitfields in FIFO_PARTITION */
  210. #define IDMA32C_FP_PSIZE_CH0(x) ((x) << 0)
  211. #define IDMA32C_FP_PSIZE_CH1(x) ((x) << 13)
  212. #define IDMA32C_FP_UPDATE (1 << 26)
  213. enum dw_dmac_flags {
  214. DW_DMA_IS_CYCLIC = 0,
  215. DW_DMA_IS_SOFT_LLP = 1,
  216. DW_DMA_IS_PAUSED = 2,
  217. DW_DMA_IS_INITIALIZED = 3,
  218. };
  219. struct dw_dma_chan {
  220. struct dma_chan chan;
  221. void __iomem *ch_regs;
  222. u8 mask;
  223. u8 priority;
  224. enum dma_transfer_direction direction;
  225. /* software emulation of the LLP transfers */
  226. struct list_head *tx_node_active;
  227. spinlock_t lock;
  228. /* these other elements are all protected by lock */
  229. unsigned long flags;
  230. struct list_head active_list;
  231. struct list_head queue;
  232. unsigned int descs_allocated;
  233. /* hardware configuration */
  234. unsigned int block_size;
  235. bool nollp;
  236. /* custom slave configuration */
  237. struct dw_dma_slave dws;
  238. /* configuration passed via .device_config */
  239. struct dma_slave_config dma_sconfig;
  240. };
  241. static inline struct dw_dma_chan_regs __iomem *
  242. __dwc_regs(struct dw_dma_chan *dwc)
  243. {
  244. return dwc->ch_regs;
  245. }
  246. #define channel_readl(dwc, name) \
  247. readl(&(__dwc_regs(dwc)->name))
  248. #define channel_writel(dwc, name, val) \
  249. writel((val), &(__dwc_regs(dwc)->name))
  250. static inline struct dw_dma_chan *to_dw_dma_chan(struct dma_chan *chan)
  251. {
  252. return container_of(chan, struct dw_dma_chan, chan);
  253. }
  254. struct dw_dma {
  255. struct dma_device dma;
  256. char name[20];
  257. void __iomem *regs;
  258. struct dma_pool *desc_pool;
  259. struct tasklet_struct tasklet;
  260. /* channels */
  261. struct dw_dma_chan *chan;
  262. u8 all_chan_mask;
  263. u8 in_use;
  264. /* platform data */
  265. struct dw_dma_platform_data *pdata;
  266. };
  267. static inline struct dw_dma_regs __iomem *__dw_regs(struct dw_dma *dw)
  268. {
  269. return dw->regs;
  270. }
  271. #define dma_readl(dw, name) \
  272. readl(&(__dw_regs(dw)->name))
  273. #define dma_writel(dw, name, val) \
  274. writel((val), &(__dw_regs(dw)->name))
  275. #define idma32_readq(dw, name) \
  276. hi_lo_readq(&(__dw_regs(dw)->name))
  277. #define idma32_writeq(dw, name, val) \
  278. hi_lo_writeq((val), &(__dw_regs(dw)->name))
  279. #define channel_set_bit(dw, reg, mask) \
  280. dma_writel(dw, reg, ((mask) << 8) | (mask))
  281. #define channel_clear_bit(dw, reg, mask) \
  282. dma_writel(dw, reg, ((mask) << 8) | 0)
  283. static inline struct dw_dma *to_dw_dma(struct dma_device *ddev)
  284. {
  285. return container_of(ddev, struct dw_dma, dma);
  286. }
  287. /* LLI == Linked List Item; a.k.a. DMA block descriptor */
  288. struct dw_lli {
  289. /* values that are not changed by hardware */
  290. __le32 sar;
  291. __le32 dar;
  292. __le32 llp; /* chain to next lli */
  293. __le32 ctllo;
  294. /* values that may get written back: */
  295. __le32 ctlhi;
  296. /* sstat and dstat can snapshot peripheral register state.
  297. * silicon config may discard either or both...
  298. */
  299. __le32 sstat;
  300. __le32 dstat;
  301. };
  302. struct dw_desc {
  303. /* FIRST values the hardware uses */
  304. struct dw_lli lli;
  305. #define lli_set(d, reg, v) ((d)->lli.reg |= cpu_to_le32(v))
  306. #define lli_clear(d, reg, v) ((d)->lli.reg &= ~cpu_to_le32(v))
  307. #define lli_read(d, reg) le32_to_cpu((d)->lli.reg)
  308. #define lli_write(d, reg, v) ((d)->lli.reg = cpu_to_le32(v))
  309. /* THEN values for driver housekeeping */
  310. struct list_head desc_node;
  311. struct list_head tx_list;
  312. struct dma_async_tx_descriptor txd;
  313. size_t len;
  314. size_t total_len;
  315. u32 residue;
  316. };
  317. #define to_dw_desc(h) list_entry(h, struct dw_desc, desc_node)
  318. static inline struct dw_desc *
  319. txd_to_dw_desc(struct dma_async_tx_descriptor *txd)
  320. {
  321. return container_of(txd, struct dw_desc, txd);
  322. }