ark-dma.h 12 KB

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