0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #include <linux/bitops.h>
0011 #include <linux/interrupt.h>
0012 #include <linux/dmaengine.h>
0013
0014 #include <linux/io-64-nonatomic-hi-lo.h>
0015
0016 #include "internal.h"
0017
0018 #define DW_DMA_MAX_NR_REQUESTS 16
0019
0020
0021 enum dw_dma_fc {
0022 DW_DMA_FC_D_M2M,
0023 DW_DMA_FC_D_M2P,
0024 DW_DMA_FC_D_P2M,
0025 DW_DMA_FC_D_P2P,
0026 DW_DMA_FC_P_P2M,
0027 DW_DMA_FC_SP_P2P,
0028 DW_DMA_FC_P_M2P,
0029 DW_DMA_FC_DP_P2P,
0030 };
0031
0032
0033
0034
0035
0036 #define DW_REG(name) u32 name; u32 __pad_##name
0037
0038
0039 struct dw_dma_chan_regs {
0040 DW_REG(SAR);
0041 DW_REG(DAR);
0042 DW_REG(LLP);
0043 u32 CTL_LO;
0044 u32 CTL_HI;
0045 DW_REG(SSTAT);
0046 DW_REG(DSTAT);
0047 DW_REG(SSTATAR);
0048 DW_REG(DSTATAR);
0049 u32 CFG_LO;
0050 u32 CFG_HI;
0051 DW_REG(SGR);
0052 DW_REG(DSR);
0053 };
0054
0055 struct dw_dma_irq_regs {
0056 DW_REG(XFER);
0057 DW_REG(BLOCK);
0058 DW_REG(SRC_TRAN);
0059 DW_REG(DST_TRAN);
0060 DW_REG(ERROR);
0061 };
0062
0063 struct dw_dma_regs {
0064
0065 struct dw_dma_chan_regs CHAN[DW_DMA_MAX_NR_CHANNELS];
0066
0067
0068 struct dw_dma_irq_regs RAW;
0069 struct dw_dma_irq_regs STATUS;
0070 struct dw_dma_irq_regs MASK;
0071 struct dw_dma_irq_regs CLEAR;
0072
0073 DW_REG(STATUS_INT);
0074
0075
0076 DW_REG(REQ_SRC);
0077 DW_REG(REQ_DST);
0078 DW_REG(SGL_REQ_SRC);
0079 DW_REG(SGL_REQ_DST);
0080 DW_REG(LAST_SRC);
0081 DW_REG(LAST_DST);
0082
0083
0084 DW_REG(CFG);
0085 DW_REG(CH_EN);
0086 DW_REG(ID);
0087 DW_REG(TEST);
0088
0089
0090 DW_REG(CLASS_PRIORITY0);
0091 DW_REG(CLASS_PRIORITY1);
0092
0093
0094 u32 __reserved;
0095
0096
0097 u32 DWC_PARAMS[DW_DMA_MAX_NR_CHANNELS];
0098 u32 MULTI_BLK_TYPE;
0099 u32 MAX_BLK_SIZE;
0100
0101
0102 u32 DW_PARAMS;
0103
0104
0105 u32 COMP_TYPE;
0106 u32 COMP_VERSION;
0107
0108
0109 DW_REG(FIFO_PARTITION0);
0110 DW_REG(FIFO_PARTITION1);
0111
0112 DW_REG(SAI_ERR);
0113 DW_REG(GLOBAL_CFG);
0114 };
0115
0116
0117 #define DW_PARAMS_NR_CHAN 8
0118 #define DW_PARAMS_NR_MASTER 11
0119 #define DW_PARAMS_DATA_WIDTH(n) (15 + 2 * (n))
0120 #define DW_PARAMS_DATA_WIDTH1 15
0121 #define DW_PARAMS_DATA_WIDTH2 17
0122 #define DW_PARAMS_DATA_WIDTH3 19
0123 #define DW_PARAMS_DATA_WIDTH4 21
0124 #define DW_PARAMS_EN 28
0125
0126
0127 #define DWC_PARAMS_MBLK_EN 11
0128 #define DWC_PARAMS_HC_LLP 13
0129 #define DWC_PARAMS_MSIZE 16
0130
0131
0132 enum dw_dma_msize {
0133 DW_DMA_MSIZE_1,
0134 DW_DMA_MSIZE_4,
0135 DW_DMA_MSIZE_8,
0136 DW_DMA_MSIZE_16,
0137 DW_DMA_MSIZE_32,
0138 DW_DMA_MSIZE_64,
0139 DW_DMA_MSIZE_128,
0140 DW_DMA_MSIZE_256,
0141 };
0142
0143
0144 #define DWC_LLP_LMS(x) ((x) & 3)
0145 #define DWC_LLP_LOC(x) ((x) & ~3)
0146
0147
0148 #define DWC_CTLL_INT_EN (1 << 0)
0149 #define DWC_CTLL_DST_WIDTH(n) ((n)<<1)
0150 #define DWC_CTLL_SRC_WIDTH(n) ((n)<<4)
0151 #define DWC_CTLL_DST_INC (0<<7)
0152 #define DWC_CTLL_DST_DEC (1<<7)
0153 #define DWC_CTLL_DST_FIX (2<<7)
0154 #define DWC_CTLL_SRC_INC (0<<9)
0155 #define DWC_CTLL_SRC_DEC (1<<9)
0156 #define DWC_CTLL_SRC_FIX (2<<9)
0157 #define DWC_CTLL_DST_MSIZE(n) ((n)<<11)
0158 #define DWC_CTLL_SRC_MSIZE(n) ((n)<<14)
0159 #define DWC_CTLL_S_GATH_EN (1 << 17)
0160 #define DWC_CTLL_D_SCAT_EN (1 << 18)
0161 #define DWC_CTLL_FC(n) ((n) << 20)
0162 #define DWC_CTLL_FC_M2M (0 << 20)
0163 #define DWC_CTLL_FC_M2P (1 << 20)
0164 #define DWC_CTLL_FC_P2M (2 << 20)
0165 #define DWC_CTLL_FC_P2P (3 << 20)
0166
0167 #define DWC_CTLL_DMS(n) ((n)<<23)
0168 #define DWC_CTLL_SMS(n) ((n)<<25)
0169 #define DWC_CTLL_LLP_D_EN (1 << 27)
0170 #define DWC_CTLL_LLP_S_EN (1 << 28)
0171
0172
0173 #define DWC_CTLH_BLOCK_TS_MASK GENMASK(11, 0)
0174 #define DWC_CTLH_BLOCK_TS(x) ((x) & DWC_CTLH_BLOCK_TS_MASK)
0175 #define DWC_CTLH_DONE (1 << 12)
0176
0177
0178 #define DWC_CFGL_CH_PRIOR_MASK (0x7 << 5)
0179 #define DWC_CFGL_CH_PRIOR(x) ((x) << 5)
0180 #define DWC_CFGL_CH_SUSP (1 << 8)
0181 #define DWC_CFGL_FIFO_EMPTY (1 << 9)
0182 #define DWC_CFGL_HS_DST (1 << 10)
0183 #define DWC_CFGL_HS_SRC (1 << 11)
0184 #define DWC_CFGL_LOCK_CH_XFER (0 << 12)
0185 #define DWC_CFGL_LOCK_CH_BLOCK (1 << 12)
0186 #define DWC_CFGL_LOCK_CH_XACT (2 << 12)
0187 #define DWC_CFGL_LOCK_BUS_XFER (0 << 14)
0188 #define DWC_CFGL_LOCK_BUS_BLOCK (1 << 14)
0189 #define DWC_CFGL_LOCK_BUS_XACT (2 << 14)
0190 #define DWC_CFGL_LOCK_CH (1 << 15)
0191 #define DWC_CFGL_LOCK_BUS (1 << 16)
0192 #define DWC_CFGL_HS_DST_POL (1 << 18)
0193 #define DWC_CFGL_HS_SRC_POL (1 << 19)
0194 #define DWC_CFGL_MAX_BURST(x) ((x) << 20)
0195 #define DWC_CFGL_RELOAD_SAR (1 << 30)
0196 #define DWC_CFGL_RELOAD_DAR (1 << 31)
0197
0198
0199 #define DWC_CFGH_FCMODE (1 << 0)
0200 #define DWC_CFGH_FIFO_MODE (1 << 1)
0201 #define DWC_CFGH_PROTCTL(x) ((x) << 2)
0202 #define DWC_CFGH_PROTCTL_DATA (0 << 2)
0203 #define DWC_CFGH_PROTCTL_PRIV (1 << 2)
0204 #define DWC_CFGH_PROTCTL_BUFFER (2 << 2)
0205 #define DWC_CFGH_PROTCTL_CACHE (4 << 2)
0206 #define DWC_CFGH_DS_UPD_EN (1 << 5)
0207 #define DWC_CFGH_SS_UPD_EN (1 << 6)
0208 #define DWC_CFGH_SRC_PER(x) ((x) << 7)
0209 #define DWC_CFGH_DST_PER(x) ((x) << 11)
0210
0211
0212 #define DWC_SGR_SGI(x) ((x) << 0)
0213 #define DWC_SGR_SGC(x) ((x) << 20)
0214
0215
0216 #define DWC_DSR_DSI(x) ((x) << 0)
0217 #define DWC_DSR_DSC(x) ((x) << 20)
0218
0219
0220 #define DW_CFG_DMA_EN (1 << 0)
0221
0222
0223
0224
0225 enum idma32_msize {
0226 IDMA32_MSIZE_1,
0227 IDMA32_MSIZE_2,
0228 IDMA32_MSIZE_4,
0229 IDMA32_MSIZE_8,
0230 IDMA32_MSIZE_16,
0231 IDMA32_MSIZE_32,
0232 };
0233
0234
0235 #define IDMA32C_CTLH_BLOCK_TS_MASK GENMASK(16, 0)
0236 #define IDMA32C_CTLH_BLOCK_TS(x) ((x) & IDMA32C_CTLH_BLOCK_TS_MASK)
0237 #define IDMA32C_CTLH_DONE (1 << 17)
0238
0239
0240 #define IDMA32C_CFGL_DST_BURST_ALIGN (1 << 0)
0241 #define IDMA32C_CFGL_SRC_BURST_ALIGN (1 << 1)
0242 #define IDMA32C_CFGL_CH_DRAIN (1 << 10)
0243 #define IDMA32C_CFGL_DST_OPT_BL (1 << 20)
0244 #define IDMA32C_CFGL_SRC_OPT_BL (1 << 21)
0245
0246
0247 #define IDMA32C_CFGH_SRC_PER(x) ((x) << 0)
0248 #define IDMA32C_CFGH_DST_PER(x) ((x) << 4)
0249 #define IDMA32C_CFGH_RD_ISSUE_THD(x) ((x) << 8)
0250 #define IDMA32C_CFGH_RW_ISSUE_THD(x) ((x) << 18)
0251 #define IDMA32C_CFGH_SRC_PER_EXT(x) ((x) << 28)
0252 #define IDMA32C_CFGH_DST_PER_EXT(x) ((x) << 30)
0253
0254
0255 #define IDMA32C_FP_PSIZE_CH0(x) ((x) << 0)
0256 #define IDMA32C_FP_PSIZE_CH1(x) ((x) << 13)
0257 #define IDMA32C_FP_UPDATE (1 << 26)
0258
0259 enum dw_dmac_flags {
0260 DW_DMA_IS_CYCLIC = 0,
0261 DW_DMA_IS_SOFT_LLP = 1,
0262 DW_DMA_IS_PAUSED = 2,
0263 DW_DMA_IS_INITIALIZED = 3,
0264 };
0265
0266 struct dw_dma_chan {
0267 struct dma_chan chan;
0268 void __iomem *ch_regs;
0269 u8 mask;
0270 u8 priority;
0271 enum dma_transfer_direction direction;
0272
0273
0274 struct list_head *tx_node_active;
0275
0276 spinlock_t lock;
0277
0278
0279 unsigned long flags;
0280 struct list_head active_list;
0281 struct list_head queue;
0282
0283 unsigned int descs_allocated;
0284
0285
0286 unsigned int block_size;
0287 bool nollp;
0288 u32 max_burst;
0289
0290
0291 struct dw_dma_slave dws;
0292
0293
0294 struct dma_slave_config dma_sconfig;
0295 };
0296
0297 static inline struct dw_dma_chan_regs __iomem *
0298 __dwc_regs(struct dw_dma_chan *dwc)
0299 {
0300 return dwc->ch_regs;
0301 }
0302
0303 #define channel_readl(dwc, name) \
0304 readl(&(__dwc_regs(dwc)->name))
0305 #define channel_writel(dwc, name, val) \
0306 writel((val), &(__dwc_regs(dwc)->name))
0307
0308 static inline struct dw_dma_chan *to_dw_dma_chan(struct dma_chan *chan)
0309 {
0310 return container_of(chan, struct dw_dma_chan, chan);
0311 }
0312
0313 struct dw_dma {
0314 struct dma_device dma;
0315 char name[20];
0316 void __iomem *regs;
0317 struct dma_pool *desc_pool;
0318 struct tasklet_struct tasklet;
0319
0320
0321 struct dw_dma_chan *chan;
0322 u8 all_chan_mask;
0323 u8 in_use;
0324
0325
0326 void (*initialize_chan)(struct dw_dma_chan *dwc);
0327 void (*suspend_chan)(struct dw_dma_chan *dwc, bool drain);
0328 void (*resume_chan)(struct dw_dma_chan *dwc, bool drain);
0329 u32 (*prepare_ctllo)(struct dw_dma_chan *dwc);
0330 void (*encode_maxburst)(struct dw_dma_chan *dwc, u32 *maxburst);
0331 u32 (*bytes2block)(struct dw_dma_chan *dwc, size_t bytes,
0332 unsigned int width, size_t *len);
0333 size_t (*block2bytes)(struct dw_dma_chan *dwc, u32 block, u32 width);
0334
0335
0336 void (*set_device_name)(struct dw_dma *dw, int id);
0337 void (*disable)(struct dw_dma *dw);
0338 void (*enable)(struct dw_dma *dw);
0339
0340
0341 struct dw_dma_platform_data *pdata;
0342 };
0343
0344 static inline struct dw_dma_regs __iomem *__dw_regs(struct dw_dma *dw)
0345 {
0346 return dw->regs;
0347 }
0348
0349 #define dma_readl(dw, name) \
0350 readl(&(__dw_regs(dw)->name))
0351 #define dma_writel(dw, name, val) \
0352 writel((val), &(__dw_regs(dw)->name))
0353
0354 #define idma32_readq(dw, name) \
0355 hi_lo_readq(&(__dw_regs(dw)->name))
0356 #define idma32_writeq(dw, name, val) \
0357 hi_lo_writeq((val), &(__dw_regs(dw)->name))
0358
0359 #define channel_set_bit(dw, reg, mask) \
0360 dma_writel(dw, reg, ((mask) << 8) | (mask))
0361 #define channel_clear_bit(dw, reg, mask) \
0362 dma_writel(dw, reg, ((mask) << 8) | 0)
0363
0364 static inline struct dw_dma *to_dw_dma(struct dma_device *ddev)
0365 {
0366 return container_of(ddev, struct dw_dma, dma);
0367 }
0368
0369
0370 struct dw_lli {
0371
0372 __le32 sar;
0373 __le32 dar;
0374 __le32 llp;
0375 __le32 ctllo;
0376
0377 __le32 ctlhi;
0378
0379
0380
0381 __le32 sstat;
0382 __le32 dstat;
0383 };
0384
0385 struct dw_desc {
0386
0387 struct dw_lli lli;
0388
0389 #define lli_set(d, reg, v) ((d)->lli.reg |= cpu_to_le32(v))
0390 #define lli_clear(d, reg, v) ((d)->lli.reg &= ~cpu_to_le32(v))
0391 #define lli_read(d, reg) le32_to_cpu((d)->lli.reg)
0392 #define lli_write(d, reg, v) ((d)->lli.reg = cpu_to_le32(v))
0393
0394
0395 struct list_head desc_node;
0396 struct list_head tx_list;
0397 struct dma_async_tx_descriptor txd;
0398 size_t len;
0399 size_t total_len;
0400 u32 residue;
0401 };
0402
0403 #define to_dw_desc(h) list_entry(h, struct dw_desc, desc_node)
0404
0405 static inline struct dw_desc *
0406 txd_to_dw_desc(struct dma_async_tx_descriptor *txd)
0407 {
0408 return container_of(txd, struct dw_desc, txd);
0409 }