regs.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Driver for the Synopsys DesignWare AHB DMA Controller
  4. *
  5. * Copyright (C) 2005-2007 Atmel Corporation
  6. * Copyright (C) 2010-2011 ST Microelectronics
  7. * Copyright (C) 2016 Intel Corporation
  8. */
  9. #include <linux/bitops.h>
  10. #include <linux/interrupt.h>
  11. #include <linux/dmaengine.h>
  12. #include <linux/io-64-nonatomic-hi-lo.h>
  13. #include "internal.h"
  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. /* iDMA 32-bit support */
  76. DW_REG(CLASS_PRIORITY0);
  77. DW_REG(CLASS_PRIORITY1);
  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. /* component ID */
  87. u32 COMP_TYPE;
  88. u32 COMP_VERSION;
  89. /* iDMA 32-bit support */
  90. DW_REG(FIFO_PARTITION0);
  91. DW_REG(FIFO_PARTITION1);
  92. DW_REG(SAI_ERR);
  93. DW_REG(GLOBAL_CFG);
  94. };
  95. /* Bitfields in DW_PARAMS */
  96. #define DW_PARAMS_NR_CHAN 8 /* number of channels */
  97. #define DW_PARAMS_NR_MASTER 11 /* number of AHB masters */
  98. #define DW_PARAMS_DATA_WIDTH(n) (15 + 2 * (n))
  99. #define DW_PARAMS_DATA_WIDTH1 15 /* master 1 data width */
  100. #define DW_PARAMS_DATA_WIDTH2 17 /* master 2 data width */
  101. #define DW_PARAMS_DATA_WIDTH3 19 /* master 3 data width */
  102. #define DW_PARAMS_DATA_WIDTH4 21 /* master 4 data width */
  103. #define DW_PARAMS_EN 28 /* encoded parameters */
  104. /* Bitfields in DWC_PARAMS */
  105. #define DWC_PARAMS_MBLK_EN 11 /* multi block transfer */
  106. #define DWC_PARAMS_HC_LLP 13 /* set LLP register to zero */
  107. #define DWC_PARAMS_MSIZE 16 /* max group transaction size */
  108. /* bursts size */
  109. enum dw_dma_msize {
  110. DW_DMA_MSIZE_1,
  111. DW_DMA_MSIZE_4,
  112. DW_DMA_MSIZE_8,
  113. DW_DMA_MSIZE_16,
  114. DW_DMA_MSIZE_32,
  115. DW_DMA_MSIZE_64,
  116. DW_DMA_MSIZE_128,
  117. DW_DMA_MSIZE_256,
  118. };
  119. /* Bitfields in LLP */
  120. #define DWC_LLP_LMS(x) ((x) & 3) /* list master select */
  121. #define DWC_LLP_LOC(x) ((x) & ~3) /* next lli */
  122. /* Bitfields in CTL_LO */
  123. #define DWC_CTLL_INT_EN (1 << 0) /* irqs enabled? */
  124. #define DWC_CTLL_DST_WIDTH(n) ((n)<<1) /* bytes per element */
  125. #define DWC_CTLL_SRC_WIDTH(n) ((n)<<4)
  126. #define DWC_CTLL_DST_INC (0<<7) /* DAR update/not */
  127. #define DWC_CTLL_DST_DEC (1<<7)
  128. #define DWC_CTLL_DST_FIX (2<<7)
  129. #define DWC_CTLL_SRC_INC (0<<9) /* SAR update/not */
  130. #define DWC_CTLL_SRC_DEC (1<<9)
  131. #define DWC_CTLL_SRC_FIX (2<<9)
  132. #define DWC_CTLL_DST_MSIZE(n) ((n)<<11) /* burst, #elements */
  133. #define DWC_CTLL_SRC_MSIZE(n) ((n)<<14)
  134. #define DWC_CTLL_S_GATH_EN (1 << 17) /* src gather, !FIX */
  135. #define DWC_CTLL_D_SCAT_EN (1 << 18) /* dst scatter, !FIX */
  136. #define DWC_CTLL_FC(n) ((n) << 20)
  137. #define DWC_CTLL_FC_M2M (0 << 20) /* mem-to-mem */
  138. #define DWC_CTLL_FC_M2P (1 << 20) /* mem-to-periph */
  139. #define DWC_CTLL_FC_P2M (2 << 20) /* periph-to-mem */
  140. #define DWC_CTLL_FC_P2P (3 << 20) /* periph-to-periph */
  141. /* plus 4 transfer types for peripheral-as-flow-controller */
  142. #define DWC_CTLL_DMS(n) ((n)<<23) /* dst master select */
  143. #define DWC_CTLL_SMS(n) ((n)<<25) /* src master select */
  144. #define DWC_CTLL_LLP_D_EN (1 << 27) /* dest block chain */
  145. #define DWC_CTLL_LLP_S_EN (1 << 28) /* src block chain */
  146. /* Bitfields in CTL_HI */
  147. #define DWC_CTLH_BLOCK_TS_MASK GENMASK(11, 0)
  148. #define DWC_CTLH_BLOCK_TS(x) ((x) & DWC_CTLH_BLOCK_TS_MASK)
  149. #define DWC_CTLH_DONE (1 << 12)
  150. /* Bitfields in CFG_LO */
  151. #define DWC_CFGL_CH_PRIOR_MASK (0x7 << 5) /* priority mask */
  152. #define DWC_CFGL_CH_PRIOR(x) ((x) << 5) /* priority */
  153. #define DWC_CFGL_CH_SUSP (1 << 8) /* pause xfer */
  154. #define DWC_CFGL_FIFO_EMPTY (1 << 9) /* pause xfer */
  155. #define DWC_CFGL_HS_DST (1 << 10) /* handshake w/dst */
  156. #define DWC_CFGL_HS_SRC (1 << 11) /* handshake w/src */
  157. #define DWC_CFGL_LOCK_CH_XFER (0 << 12) /* scope of LOCK_CH */
  158. #define DWC_CFGL_LOCK_CH_BLOCK (1 << 12)
  159. #define DWC_CFGL_LOCK_CH_XACT (2 << 12)
  160. #define DWC_CFGL_LOCK_BUS_XFER (0 << 14) /* scope of LOCK_BUS */
  161. #define DWC_CFGL_LOCK_BUS_BLOCK (1 << 14)
  162. #define DWC_CFGL_LOCK_BUS_XACT (2 << 14)
  163. #define DWC_CFGL_LOCK_CH (1 << 15) /* channel lockout */
  164. #define DWC_CFGL_LOCK_BUS (1 << 16) /* busmaster lockout */
  165. #define DWC_CFGL_HS_DST_POL (1 << 18) /* dst handshake active low */
  166. #define DWC_CFGL_HS_SRC_POL (1 << 19) /* src handshake active low */
  167. #define DWC_CFGL_MAX_BURST(x) ((x) << 20)
  168. #define DWC_CFGL_RELOAD_SAR (1 << 30)
  169. #define DWC_CFGL_RELOAD_DAR (1 << 31)
  170. /* Bitfields in CFG_HI */
  171. #define DWC_CFGH_FCMODE (1 << 0)
  172. #define DWC_CFGH_FIFO_MODE (1 << 1)
  173. #define DWC_CFGH_PROTCTL(x) ((x) << 2)
  174. #define DWC_CFGH_PROTCTL_DATA (0 << 2) /* data access - always set */
  175. #define DWC_CFGH_PROTCTL_PRIV (1 << 2) /* privileged -> AHB HPROT[1] */
  176. #define DWC_CFGH_PROTCTL_BUFFER (2 << 2) /* bufferable -> AHB HPROT[2] */
  177. #define DWC_CFGH_PROTCTL_CACHE (4 << 2) /* cacheable -> AHB HPROT[3] */
  178. #define DWC_CFGH_DS_UPD_EN (1 << 5)
  179. #define DWC_CFGH_SS_UPD_EN (1 << 6)
  180. #define DWC_CFGH_SRC_PER(x) ((x) << 7)
  181. #define DWC_CFGH_DST_PER(x) ((x) << 11)
  182. /* Bitfields in SGR */
  183. #define DWC_SGR_SGI(x) ((x) << 0)
  184. #define DWC_SGR_SGC(x) ((x) << 20)
  185. /* Bitfields in DSR */
  186. #define DWC_DSR_DSI(x) ((x) << 0)
  187. #define DWC_DSR_DSC(x) ((x) << 20)
  188. /* Bitfields in CFG */
  189. #define DW_CFG_DMA_EN (1 << 0)
  190. /* iDMA 32-bit support */
  191. /* bursts size */
  192. enum idma32_msize {
  193. IDMA32_MSIZE_1,
  194. IDMA32_MSIZE_2,
  195. IDMA32_MSIZE_4,
  196. IDMA32_MSIZE_8,
  197. IDMA32_MSIZE_16,
  198. IDMA32_MSIZE_32,
  199. };
  200. /* Bitfields in CTL_HI */
  201. #define IDMA32C_CTLH_BLOCK_TS_MASK GENMASK(16, 0)
  202. #define IDMA32C_CTLH_BLOCK_TS(x) ((x) & IDMA32C_CTLH_BLOCK_TS_MASK)
  203. #define IDMA32C_CTLH_DONE (1 << 17)
  204. /* Bitfields in CFG_LO */
  205. #define IDMA32C_CFGL_DST_BURST_ALIGN (1 << 0) /* dst burst align */
  206. #define IDMA32C_CFGL_SRC_BURST_ALIGN (1 << 1) /* src burst align */
  207. #define IDMA32C_CFGL_CH_DRAIN (1 << 10) /* drain FIFO */
  208. #define IDMA32C_CFGL_DST_OPT_BL (1 << 20) /* optimize dst burst length */
  209. #define IDMA32C_CFGL_SRC_OPT_BL (1 << 21) /* optimize src burst length */
  210. /* Bitfields in CFG_HI */
  211. #define IDMA32C_CFGH_SRC_PER(x) ((x) << 0)
  212. #define IDMA32C_CFGH_DST_PER(x) ((x) << 4)
  213. #define IDMA32C_CFGH_RD_ISSUE_THD(x) ((x) << 8)
  214. #define IDMA32C_CFGH_RW_ISSUE_THD(x) ((x) << 18)
  215. #define IDMA32C_CFGH_SRC_PER_EXT(x) ((x) << 28) /* src peripheral extension */
  216. #define IDMA32C_CFGH_DST_PER_EXT(x) ((x) << 30) /* dst peripheral extension */
  217. /* Bitfields in FIFO_PARTITION */
  218. #define IDMA32C_FP_PSIZE_CH0(x) ((x) << 0)
  219. #define IDMA32C_FP_PSIZE_CH1(x) ((x) << 13)
  220. #define IDMA32C_FP_UPDATE (1 << 26)
  221. enum dw_dmac_flags {
  222. DW_DMA_IS_CYCLIC = 0,
  223. DW_DMA_IS_SOFT_LLP = 1,
  224. DW_DMA_IS_PAUSED = 2,
  225. DW_DMA_IS_INITIALIZED = 3,
  226. };
  227. struct dw_dma_chan {
  228. struct dma_chan chan;
  229. void __iomem *ch_regs;
  230. u8 mask;
  231. u8 priority;
  232. enum dma_transfer_direction direction;
  233. /* software emulation of the LLP transfers */
  234. struct list_head *tx_node_active;
  235. spinlock_t lock;
  236. /* these other elements are all protected by lock */
  237. unsigned long flags;
  238. struct list_head active_list;
  239. struct list_head queue;
  240. unsigned int descs_allocated;
  241. /* hardware configuration */
  242. unsigned int block_size;
  243. bool nollp;
  244. u32 max_burst;
  245. /* custom slave configuration */
  246. struct dw_dma_slave dws;
  247. /* configuration passed via .device_config */
  248. struct dma_slave_config dma_sconfig;
  249. };
  250. static inline struct dw_dma_chan_regs __iomem *
  251. __dwc_regs(struct dw_dma_chan *dwc)
  252. {
  253. return dwc->ch_regs;
  254. }
  255. #define channel_readl(dwc, name) \
  256. readl(&(__dwc_regs(dwc)->name))
  257. #define channel_writel(dwc, name, val) \
  258. writel((val), &(__dwc_regs(dwc)->name))
  259. static inline struct dw_dma_chan *to_dw_dma_chan(struct dma_chan *chan)
  260. {
  261. return container_of(chan, struct dw_dma_chan, chan);
  262. }
  263. struct dw_dma {
  264. struct dma_device dma;
  265. char name[20];
  266. void __iomem *regs;
  267. struct dma_pool *desc_pool;
  268. struct tasklet_struct tasklet;
  269. /* channels */
  270. struct dw_dma_chan *chan;
  271. u8 all_chan_mask;
  272. u8 in_use;
  273. /* Channel operations */
  274. void (*initialize_chan)(struct dw_dma_chan *dwc);
  275. void (*suspend_chan)(struct dw_dma_chan *dwc, bool drain);
  276. void (*resume_chan)(struct dw_dma_chan *dwc, bool drain);
  277. u32 (*prepare_ctllo)(struct dw_dma_chan *dwc);
  278. void (*encode_maxburst)(struct dw_dma_chan *dwc, u32 *maxburst);
  279. u32 (*bytes2block)(struct dw_dma_chan *dwc, size_t bytes,
  280. unsigned int width, size_t *len);
  281. size_t (*block2bytes)(struct dw_dma_chan *dwc, u32 block, u32 width);
  282. /* Device operations */
  283. void (*set_device_name)(struct dw_dma *dw, int id);
  284. void (*disable)(struct dw_dma *dw);
  285. void (*enable)(struct dw_dma *dw);
  286. /* platform data */
  287. struct dw_dma_platform_data *pdata;
  288. };
  289. static inline struct dw_dma_regs __iomem *__dw_regs(struct dw_dma *dw)
  290. {
  291. return dw->regs;
  292. }
  293. #define dma_readl(dw, name) \
  294. readl(&(__dw_regs(dw)->name))
  295. #define dma_writel(dw, name, val) \
  296. writel((val), &(__dw_regs(dw)->name))
  297. #define idma32_readq(dw, name) \
  298. hi_lo_readq(&(__dw_regs(dw)->name))
  299. #define idma32_writeq(dw, name, val) \
  300. hi_lo_writeq((val), &(__dw_regs(dw)->name))
  301. #define channel_set_bit(dw, reg, mask) \
  302. dma_writel(dw, reg, ((mask) << 8) | (mask))
  303. #define channel_clear_bit(dw, reg, mask) \
  304. dma_writel(dw, reg, ((mask) << 8) | 0)
  305. static inline struct dw_dma *to_dw_dma(struct dma_device *ddev)
  306. {
  307. return container_of(ddev, struct dw_dma, dma);
  308. }
  309. /* LLI == Linked List Item; a.k.a. DMA block descriptor */
  310. struct dw_lli {
  311. /* values that are not changed by hardware */
  312. __le32 sar;
  313. __le32 dar;
  314. __le32 llp; /* chain to next lli */
  315. __le32 ctllo;
  316. /* values that may get written back: */
  317. __le32 ctlhi;
  318. /* sstat and dstat can snapshot peripheral register state.
  319. * silicon config may discard either or both...
  320. */
  321. __le32 sstat;
  322. __le32 dstat;
  323. };
  324. struct dw_desc {
  325. /* FIRST values the hardware uses */
  326. struct dw_lli lli;
  327. #define lli_set(d, reg, v) ((d)->lli.reg |= cpu_to_le32(v))
  328. #define lli_clear(d, reg, v) ((d)->lli.reg &= ~cpu_to_le32(v))
  329. #define lli_read(d, reg) le32_to_cpu((d)->lli.reg)
  330. #define lli_write(d, reg, v) ((d)->lli.reg = cpu_to_le32(v))
  331. /* THEN values for driver housekeeping */
  332. struct list_head desc_node;
  333. struct list_head tx_list;
  334. struct dma_async_tx_descriptor txd;
  335. size_t len;
  336. size_t total_len;
  337. u32 residue;
  338. };
  339. #define to_dw_desc(h) list_entry(h, struct dw_desc, desc_node)
  340. static inline struct dw_desc *
  341. txd_to_dw_desc(struct dma_async_tx_descriptor *txd)
  342. {
  343. return container_of(txd, struct dw_desc, txd);
  344. }