1 /* SPDX-License-Identifier: GPL-2.0+ */ 2 /* 3 * Copyright 2013-2014 Freescale Semiconductor, Inc. 4 * Copyright 2018 Angelo Dureghello <angelo@sysam.it> 5 */ 6 #ifndef _FSL_EDMA_COMMON_H_ 7 #define _FSL_EDMA_COMMON_H_ 8 9 #include <linux/dma-direction.h> 10 #include <linux/platform_device.h> 11 #include "virt-dma.h" 12 13 #define EDMA_CR_EDBG BIT(1) 14 #define EDMA_CR_ERCA BIT(2) 15 #define EDMA_CR_ERGA BIT(3) 16 #define EDMA_CR_HOE BIT(4) 17 #define EDMA_CR_HALT BIT(5) 18 #define EDMA_CR_CLM BIT(6) 19 #define EDMA_CR_EMLM BIT(7) 20 #define EDMA_CR_ECX BIT(16) 21 #define EDMA_CR_CX BIT(17) 22 23 #define EDMA_SEEI_SEEI(x) ((x) & GENMASK(4, 0)) 24 #define EDMA_CEEI_CEEI(x) ((x) & GENMASK(4, 0)) 25 #define EDMA_CINT_CINT(x) ((x) & GENMASK(4, 0)) 26 #define EDMA_CERR_CERR(x) ((x) & GENMASK(4, 0)) 27 28 #define EDMA_TCD_ATTR_DSIZE(x) (((x) & GENMASK(2, 0))) 29 #define EDMA_TCD_ATTR_DMOD(x) (((x) & GENMASK(4, 0)) << 3) 30 #define EDMA_TCD_ATTR_SSIZE(x) (((x) & GENMASK(2, 0)) << 8) 31 #define EDMA_TCD_ATTR_SMOD(x) (((x) & GENMASK(4, 0)) << 11) 32 33 #define EDMA_TCD_ITER_MASK GENMASK(14, 0) 34 #define EDMA_TCD_CITER_CITER(x) ((x) & EDMA_TCD_ITER_MASK) 35 #define EDMA_TCD_BITER_BITER(x) ((x) & EDMA_TCD_ITER_MASK) 36 37 #define EDMA_TCD_CSR_START BIT(0) 38 #define EDMA_TCD_CSR_INT_MAJOR BIT(1) 39 #define EDMA_TCD_CSR_INT_HALF BIT(2) 40 #define EDMA_TCD_CSR_D_REQ BIT(3) 41 #define EDMA_TCD_CSR_E_SG BIT(4) 42 #define EDMA_TCD_CSR_E_LINK BIT(5) 43 #define EDMA_TCD_CSR_ACTIVE BIT(6) 44 #define EDMA_TCD_CSR_DONE BIT(7) 45 #define EDMA_TCD_CSR_LINKCH GENMASK(12, 8) 46 47 #define EDMA_V3_TCD_NBYTES_MLOFF_NBYTES(x) ((x) & GENMASK(9, 0)) 48 #define EDMA_V3_TCD_NBYTES_MLOFF(x) (x << 10) 49 #define EDMA_V3_TCD_NBYTES_DMLOE (1 << 30) 50 #define EDMA_V3_TCD_NBYTES_SMLOE (1 << 31) 51 52 #define EDMAMUX_CHCFG_DIS 0x0 53 #define EDMAMUX_CHCFG_ENBL 0x80 54 #define EDMAMUX_CHCFG_SOURCE(n) ((n) & 0x3F) 55 56 #define DMAMUX_NR 2 57 58 #define EDMA_TCD 0x1000 59 60 #define FSL_EDMA_BUSWIDTHS (BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) | \ 61 BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) | \ 62 BIT(DMA_SLAVE_BUSWIDTH_4_BYTES) | \ 63 BIT(DMA_SLAVE_BUSWIDTH_8_BYTES)) 64 65 #define EDMA_V3_CH_SBR_RD BIT(22) 66 #define EDMA_V3_CH_SBR_WR BIT(21) 67 #define EDMA_V3_CH_CSR_ERQ BIT(0) 68 #define EDMA_V3_CH_CSR_EARQ BIT(1) 69 #define EDMA_V3_CH_CSR_EEI BIT(2) 70 #define EDMA_V3_CH_CSR_DONE BIT(30) 71 #define EDMA_V3_CH_CSR_ACTIVE BIT(31) 72 #define EDMA_V3_CH_ES_ERR BIT(31) 73 #define EDMA_V3_MP_ES_VLD BIT(31) 74 75 #define EDMA_V3_CH_ERR_DBE BIT(0) 76 #define EDMA_V3_CH_ERR_SBE BIT(1) 77 #define EDMA_V3_CH_ERR_SGE BIT(2) 78 #define EDMA_V3_CH_ERR_NCE BIT(3) 79 #define EDMA_V3_CH_ERR_DOE BIT(4) 80 #define EDMA_V3_CH_ERR_DAE BIT(5) 81 #define EDMA_V3_CH_ERR_SOE BIT(6) 82 #define EDMA_V3_CH_ERR_SAE BIT(7) 83 #define EDMA_V3_CH_ERR_ECX BIT(8) 84 #define EDMA_V3_CH_ERR_UCE BIT(9) 85 #define EDMA_V3_CH_ERR BIT(31) 86 87 enum fsl_edma_pm_state { 88 RUNNING = 0, 89 SUSPENDED, 90 }; 91 92 struct fsl_edma_hw_tcd { 93 __le32 saddr; 94 __le16 soff; 95 __le16 attr; 96 __le32 nbytes; 97 __le32 slast; 98 __le32 daddr; 99 __le16 doff; 100 __le16 citer; 101 __le32 dlast_sga; 102 __le16 csr; 103 __le16 biter; 104 }; 105 106 struct fsl_edma_hw_tcd64 { 107 __le64 saddr; 108 __le16 soff; 109 __le16 attr; 110 __le32 nbytes; 111 __le64 slast; 112 __le64 daddr; 113 __le64 dlast_sga; 114 __le16 doff; 115 __le16 citer; 116 __le16 csr; 117 __le16 biter; 118 } __packed; 119 120 struct fsl_edma3_ch_reg { 121 __le32 ch_csr; 122 __le32 ch_es; 123 __le32 ch_int; 124 __le32 ch_sbr; 125 __le32 ch_pri; 126 __le32 ch_mux; 127 __le32 ch_mattr; /* edma4, reserved for edma3 */ 128 __le32 ch_reserved; 129 union { 130 struct fsl_edma_hw_tcd tcd; 131 struct fsl_edma_hw_tcd64 tcd64; 132 }; 133 } __packed; 134 135 /* 136 * These are iomem pointers, for both v32 and v64. 137 */ 138 struct edma_regs { 139 void __iomem *cr; 140 void __iomem *es; 141 void __iomem *erqh; 142 void __iomem *erql; /* aka erq on v32 */ 143 void __iomem *eeih; 144 void __iomem *eeil; /* aka eei on v32 */ 145 void __iomem *seei; 146 void __iomem *ceei; 147 void __iomem *serq; 148 void __iomem *cerq; 149 void __iomem *cint; 150 void __iomem *cerr; 151 void __iomem *ssrt; 152 void __iomem *cdne; 153 void __iomem *inth; 154 void __iomem *intl; 155 void __iomem *errh; 156 void __iomem *errl; 157 }; 158 159 struct fsl_edma_sw_tcd { 160 dma_addr_t ptcd; 161 void *vtcd; 162 }; 163 164 struct fsl_edma_chan { 165 struct virt_dma_chan vchan; 166 enum dma_status status; 167 enum fsl_edma_pm_state pm_state; 168 struct fsl_edma_engine *edma; 169 struct fsl_edma_desc *edesc; 170 struct dma_slave_config cfg; 171 u32 attr; 172 bool is_sw; 173 u8 link_sg_id; 174 struct dma_pool *tcd_pool; 175 dma_addr_t dma_dev_addr; 176 u32 dma_dev_size; 177 enum dma_data_direction dma_dir; 178 char chan_name[32]; 179 char errirq_name[36]; 180 void __iomem *tcd; 181 void __iomem *mux_addr; 182 u32 real_count; 183 struct work_struct issue_worker; 184 struct platform_device *pdev; 185 struct device *pd_dev; 186 struct device_link *pd_dev_link; 187 u32 srcid; 188 struct clk *clk; 189 int priority; 190 int hw_chanid; 191 int txirq; 192 int errirq; 193 irqreturn_t (*irq_handler)(int irq, void *dev_id); 194 irqreturn_t (*errirq_handler)(int irq, void *dev_id); 195 bool is_rxchan; 196 bool is_remote; 197 bool is_multi_fifo; 198 }; 199 200 struct fsl_edma_desc { 201 struct virt_dma_desc vdesc; 202 struct fsl_edma_chan *echan; 203 bool iscyclic; 204 u8 link_sg_id; 205 enum dma_transfer_direction dirn; 206 unsigned int n_tcds; 207 struct fsl_edma_sw_tcd tcd[]; 208 }; 209 210 #define FSL_EDMA_DRV_HAS_DMACLK BIT(0) 211 #define FSL_EDMA_DRV_MUX_SWAP BIT(1) 212 #define FSL_EDMA_DRV_CONFIG32 BIT(2) 213 #define FSL_EDMA_DRV_WRAP_IO BIT(3) 214 #define FSL_EDMA_DRV_EDMA64 BIT(4) 215 #define FSL_EDMA_DRV_HAS_PD BIT(5) 216 #define FSL_EDMA_DRV_HAS_CHCLK BIT(6) 217 #define FSL_EDMA_DRV_HAS_CHMUX BIT(7) 218 #define FSL_EDMA_DRV_MEM_REMOTE BIT(8) 219 /* control and status register is in tcd address space, edma3 reg layout */ 220 #define FSL_EDMA_DRV_SPLIT_REG BIT(9) 221 #define FSL_EDMA_DRV_BUS_8BYTE BIT(10) 222 #define FSL_EDMA_DRV_DEV_TO_DEV BIT(11) 223 #define FSL_EDMA_DRV_ALIGN_64BYTE BIT(12) 224 /* Need clean CHn_CSR DONE before enable TCD's ESG */ 225 #define FSL_EDMA_DRV_CLEAR_DONE_E_SG BIT(13) 226 /* Need clean CHn_CSR DONE before enable TCD's MAJORELINK */ 227 #define FSL_EDMA_DRV_CLEAR_DONE_E_LINK BIT(14) 228 #define FSL_EDMA_DRV_TCD64 BIT(15) 229 /* All channel ERR IRQ share one IRQ line */ 230 #define FSL_EDMA_DRV_ERRIRQ_SHARE BIT(16) 231 /* Major link channel in CSR used for linking SG descriptors */ 232 #define FSL_EDMA_DRV_CSR_LINKCH BIT(17) 233 234 235 #define FSL_EDMA_DRV_EDMA3 (FSL_EDMA_DRV_SPLIT_REG | \ 236 FSL_EDMA_DRV_BUS_8BYTE | \ 237 FSL_EDMA_DRV_DEV_TO_DEV | \ 238 FSL_EDMA_DRV_ALIGN_64BYTE | \ 239 FSL_EDMA_DRV_CLEAR_DONE_E_SG | \ 240 FSL_EDMA_DRV_CLEAR_DONE_E_LINK | \ 241 FSL_EDMA_DRV_CSR_LINKCH) 242 243 #define FSL_EDMA_DRV_EDMA4 (FSL_EDMA_DRV_SPLIT_REG | \ 244 FSL_EDMA_DRV_BUS_8BYTE | \ 245 FSL_EDMA_DRV_DEV_TO_DEV | \ 246 FSL_EDMA_DRV_ALIGN_64BYTE | \ 247 FSL_EDMA_DRV_CLEAR_DONE_E_LINK | \ 248 FSL_EDMA_DRV_CSR_LINKCH) 249 250 struct fsl_edma_drvdata { 251 u32 dmamuxs; /* only used before v3 */ 252 u32 chreg_off; 253 u32 chreg_space_sz; 254 u32 flags; 255 u32 mux_off; /* channel mux register offset */ 256 u32 mux_skip; /* how much skip for each channel */ 257 int (*setup_irq)(struct platform_device *pdev, 258 struct fsl_edma_engine *fsl_edma); 259 }; 260 261 struct fsl_edma_engine { 262 struct dma_device dma_dev; 263 void __iomem *membase; 264 void __iomem *muxbase[DMAMUX_NR]; 265 struct clk *muxclk[DMAMUX_NR]; 266 struct clk *dmaclk; 267 struct mutex fsl_edma_mutex; 268 const struct fsl_edma_drvdata *drvdata; 269 u32 n_chans; 270 int txirq; 271 int txirq_16_31; 272 int errirq; 273 bool big_endian; 274 struct edma_regs regs; 275 u64 chan_masked; 276 struct fsl_edma_chan chans[] __counted_by(n_chans); 277 }; 278 279 static inline u32 fsl_edma_drvflags(struct fsl_edma_chan *fsl_chan) 280 { 281 return fsl_chan->edma->drvdata->flags; 282 } 283 284 #define edma_read_tcdreg_c(chan, _tcd, __name) \ 285 _Generic(((_tcd)->__name), \ 286 __iomem __le64 : edma_readq(chan->edma, &(_tcd)->__name), \ 287 __iomem __le32 : edma_readl(chan->edma, &(_tcd)->__name), \ 288 __iomem __le16 : edma_readw(chan->edma, &(_tcd)->__name) \ 289 ) 290 291 #define edma_read_tcdreg(chan, __name) \ 292 ((fsl_edma_drvflags(chan) & FSL_EDMA_DRV_TCD64) ? \ 293 edma_read_tcdreg_c(chan, ((struct fsl_edma_hw_tcd64 __iomem *)chan->tcd), __name) : \ 294 edma_read_tcdreg_c(chan, ((struct fsl_edma_hw_tcd __iomem *)chan->tcd), __name) \ 295 ) 296 297 #define edma_write_tcdreg_c(chan, _tcd, _val, __name) \ 298 _Generic((_tcd->__name), \ 299 __iomem __le64 : edma_writeq(chan->edma, (u64 __force)(_val), &_tcd->__name), \ 300 __iomem __le32 : edma_writel(chan->edma, (u32 __force)(_val), &_tcd->__name), \ 301 __iomem __le16 : edma_writew(chan->edma, (u16 __force)(_val), &_tcd->__name), \ 302 __iomem u8 : edma_writeb(chan->edma, _val, &_tcd->__name) \ 303 ) 304 305 #define edma_write_tcdreg(chan, val, __name) \ 306 do { \ 307 struct fsl_edma_hw_tcd64 __iomem *tcd64_r = (struct fsl_edma_hw_tcd64 __iomem *)chan->tcd; \ 308 struct fsl_edma_hw_tcd __iomem *tcd_r = (struct fsl_edma_hw_tcd __iomem *)chan->tcd; \ 309 \ 310 if (fsl_edma_drvflags(chan) & FSL_EDMA_DRV_TCD64) \ 311 edma_write_tcdreg_c(chan, tcd64_r, val, __name); \ 312 else \ 313 edma_write_tcdreg_c(chan, tcd_r, val, __name); \ 314 } while (0) 315 316 #define edma_cp_tcd_to_reg(chan, __tcd, __name) \ 317 do { \ 318 struct fsl_edma_hw_tcd64 __iomem *tcd64_r = (struct fsl_edma_hw_tcd64 __iomem *)chan->tcd; \ 319 struct fsl_edma_hw_tcd __iomem *tcd_r = (struct fsl_edma_hw_tcd __iomem *)chan->tcd; \ 320 struct fsl_edma_hw_tcd64 *tcd64_m = (struct fsl_edma_hw_tcd64 *)__tcd; \ 321 struct fsl_edma_hw_tcd *tcd_m = (struct fsl_edma_hw_tcd *)__tcd; \ 322 \ 323 if (fsl_edma_drvflags(chan) & FSL_EDMA_DRV_TCD64) \ 324 edma_write_tcdreg_c(chan, tcd64_r, tcd64_m->__name, __name); \ 325 else \ 326 edma_write_tcdreg_c(chan, tcd_r, tcd_m->__name, __name); \ 327 } while (0) 328 329 #define edma_readl_chreg(chan, __name) \ 330 edma_readl(chan->edma, \ 331 (void __iomem *)&(container_of(((__force void *)chan->tcd),\ 332 struct fsl_edma3_ch_reg, tcd)->__name)) 333 334 #define edma_writel_chreg(chan, val, __name) \ 335 edma_writel(chan->edma, val, \ 336 (void __iomem *)&(container_of(((__force void *)chan->tcd),\ 337 struct fsl_edma3_ch_reg, tcd)->__name)) 338 339 #define fsl_edma_get_tcd(_chan, _tcd, _field) \ 340 (fsl_edma_drvflags(_chan) & FSL_EDMA_DRV_TCD64 ? (((struct fsl_edma_hw_tcd64 *)_tcd)->_field) : \ 341 (((struct fsl_edma_hw_tcd *)_tcd)->_field)) 342 343 #define fsl_edma_le_to_cpu(x) \ 344 _Generic((x), \ 345 __le64 : le64_to_cpu((x)), \ 346 __le32 : le32_to_cpu((x)), \ 347 __le16 : le16_to_cpu((x)) \ 348 ) 349 350 #define fsl_edma_get_tcd_to_cpu(_chan, _tcd, _field) \ 351 (fsl_edma_drvflags(_chan) & FSL_EDMA_DRV_TCD64 ? \ 352 fsl_edma_le_to_cpu(((struct fsl_edma_hw_tcd64 *)_tcd)->_field) : \ 353 fsl_edma_le_to_cpu(((struct fsl_edma_hw_tcd *)_tcd)->_field)) 354 355 #define fsl_edma_set_tcd_to_le_c(_tcd, _val, _field) \ 356 _Generic(((_tcd)->_field), \ 357 __le64 : (_tcd)->_field = cpu_to_le64(_val), \ 358 __le32 : (_tcd)->_field = cpu_to_le32(_val), \ 359 __le16 : (_tcd)->_field = cpu_to_le16(_val) \ 360 ) 361 362 #define fsl_edma_set_tcd_to_le(_chan, _tcd, _val, _field) \ 363 do { \ 364 if (fsl_edma_drvflags(_chan) & FSL_EDMA_DRV_TCD64) \ 365 fsl_edma_set_tcd_to_le_c((struct fsl_edma_hw_tcd64 *)_tcd, _val, _field); \ 366 else \ 367 fsl_edma_set_tcd_to_le_c((struct fsl_edma_hw_tcd *)_tcd, _val, _field); \ 368 } while (0) 369 370 /* Need after struct defination */ 371 #include "fsl-edma-trace.h" 372 373 /* 374 * R/W functions for big- or little-endian registers: 375 * The eDMA controller's endian is independent of the CPU core's endian. 376 * For the big-endian IP module, the offset for 8-bit or 16-bit registers 377 * should also be swapped opposite to that in little-endian IP. 378 */ 379 static inline u64 edma_readq(struct fsl_edma_engine *edma, void __iomem *addr) 380 { 381 u64 l, h; 382 383 if (edma->big_endian) { 384 l = ioread32be(addr); 385 h = ioread32be(addr + 4); 386 } else { 387 l = ioread32(addr); 388 h = ioread32(addr + 4); 389 } 390 391 trace_edma_readl(edma, addr, l); 392 trace_edma_readl(edma, addr + 4, h); 393 394 return (h << 32) | l; 395 } 396 397 static inline u32 edma_readl(struct fsl_edma_engine *edma, void __iomem *addr) 398 { 399 u32 val; 400 401 if (edma->big_endian) 402 val = ioread32be(addr); 403 else 404 val = ioread32(addr); 405 406 trace_edma_readl(edma, addr, val); 407 408 return val; 409 } 410 411 static inline u16 edma_readw(struct fsl_edma_engine *edma, void __iomem *addr) 412 { 413 u16 val; 414 415 if (edma->big_endian) 416 val = ioread16be(addr); 417 else 418 val = ioread16(addr); 419 420 trace_edma_readw(edma, addr, val); 421 422 return val; 423 } 424 425 static inline void edma_writeb(struct fsl_edma_engine *edma, 426 u8 val, void __iomem *addr) 427 { 428 /* swap the reg offset for these in big-endian mode */ 429 if (edma->big_endian) 430 iowrite8(val, (void __iomem *)((unsigned long)addr ^ 0x3)); 431 else 432 iowrite8(val, addr); 433 434 trace_edma_writeb(edma, addr, val); 435 } 436 437 static inline void edma_writew(struct fsl_edma_engine *edma, 438 u16 val, void __iomem *addr) 439 { 440 /* swap the reg offset for these in big-endian mode */ 441 if (edma->big_endian) 442 iowrite16be(val, (void __iomem *)((unsigned long)addr ^ 0x2)); 443 else 444 iowrite16(val, addr); 445 446 trace_edma_writew(edma, addr, val); 447 } 448 449 static inline void edma_writel(struct fsl_edma_engine *edma, 450 u32 val, void __iomem *addr) 451 { 452 if (edma->big_endian) 453 iowrite32be(val, addr); 454 else 455 iowrite32(val, addr); 456 457 trace_edma_writel(edma, addr, val); 458 } 459 460 static inline void edma_writeq(struct fsl_edma_engine *edma, 461 u64 val, void __iomem *addr) 462 { 463 if (edma->big_endian) { 464 iowrite32be(val & 0xFFFFFFFF, addr); 465 iowrite32be(val >> 32, addr + 4); 466 } else { 467 iowrite32(val & 0xFFFFFFFF, addr); 468 iowrite32(val >> 32, addr + 4); 469 } 470 471 trace_edma_writel(edma, addr, val & 0xFFFFFFFF); 472 trace_edma_writel(edma, addr + 4, val >> 32); 473 } 474 475 static inline struct fsl_edma_chan *to_fsl_edma_chan(struct dma_chan *chan) 476 { 477 return container_of(chan, struct fsl_edma_chan, vchan.chan); 478 } 479 480 static inline struct fsl_edma_desc *to_fsl_edma_desc(struct virt_dma_desc *vd) 481 { 482 return container_of(vd, struct fsl_edma_desc, vdesc); 483 } 484 485 static inline void fsl_edma_err_chan_handler(struct fsl_edma_chan *fsl_chan) 486 { 487 fsl_chan->status = DMA_ERROR; 488 } 489 490 void fsl_edma_tx_chan_handler(struct fsl_edma_chan *fsl_chan); 491 void fsl_edma_disable_request(struct fsl_edma_chan *fsl_chan); 492 void fsl_edma_chan_mux(struct fsl_edma_chan *fsl_chan, 493 unsigned int slot, bool enable); 494 void fsl_edma_free_desc(struct virt_dma_desc *vdesc); 495 int fsl_edma_terminate_all(struct dma_chan *chan); 496 int fsl_edma_pause(struct dma_chan *chan); 497 int fsl_edma_resume(struct dma_chan *chan); 498 int fsl_edma_slave_config(struct dma_chan *chan, 499 struct dma_slave_config *cfg); 500 enum dma_status fsl_edma_tx_status(struct dma_chan *chan, 501 dma_cookie_t cookie, struct dma_tx_state *txstate); 502 struct dma_async_tx_descriptor *fsl_edma_prep_dma_cyclic( 503 struct dma_chan *chan, dma_addr_t dma_addr, size_t buf_len, 504 size_t period_len, enum dma_transfer_direction direction, 505 unsigned long flags); 506 struct dma_async_tx_descriptor *fsl_edma_prep_peripheral_dma_vec( 507 struct dma_chan *chan, const struct dma_vec *vecs, 508 size_t nb, enum dma_transfer_direction direction, 509 unsigned long flags); 510 struct dma_async_tx_descriptor *fsl_edma_prep_slave_sg( 511 struct dma_chan *chan, struct scatterlist *sgl, 512 unsigned int sg_len, enum dma_transfer_direction direction, 513 unsigned long flags, void *context); 514 struct dma_async_tx_descriptor *fsl_edma_prep_memcpy( 515 struct dma_chan *chan, dma_addr_t dma_dst, dma_addr_t dma_src, 516 size_t len, unsigned long flags); 517 void fsl_edma_xfer_desc(struct fsl_edma_chan *fsl_chan); 518 void fsl_edma_issue_pending(struct dma_chan *chan); 519 int fsl_edma_alloc_chan_resources(struct dma_chan *chan); 520 void fsl_edma_free_chan_resources(struct dma_chan *chan); 521 void fsl_edma_cleanup_vchan(struct dma_device *dmadev); 522 void fsl_edma_setup_regs(struct fsl_edma_engine *edma); 523 524 #endif /* _FSL_EDMA_COMMON_H_ */ 525