1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Driver for the Cirrus Logic EP93xx DMA Controller 4 * 5 * Copyright (C) 2011 Mika Westerberg 6 * 7 * DMA M2P implementation is based on the original 8 * arch/arm/mach-ep93xx/dma-m2p.c which has following copyrights: 9 * 10 * Copyright (C) 2006 Lennert Buytenhek <buytenh@wantstofly.org> 11 * Copyright (C) 2006 Applied Data Systems 12 * Copyright (C) 2009 Ryan Mallon <rmallon@gmail.com> 13 * 14 * This driver is based on dw_dmac and amba-pl08x drivers. 15 */ 16 17 #include <linux/clk.h> 18 #include <linux/init.h> 19 #include <linux/interrupt.h> 20 #include <linux/dma-mapping.h> 21 #include <linux/dmaengine.h> 22 #include <linux/module.h> 23 #include <linux/of_dma.h> 24 #include <linux/overflow.h> 25 #include <linux/platform_device.h> 26 #include <linux/slab.h> 27 28 #include "dmaengine.h" 29 30 /* M2P registers */ 31 #define M2P_CONTROL 0x0000 32 #define M2P_CONTROL_STALLINT BIT(0) 33 #define M2P_CONTROL_NFBINT BIT(1) 34 #define M2P_CONTROL_CH_ERROR_INT BIT(3) 35 #define M2P_CONTROL_ENABLE BIT(4) 36 #define M2P_CONTROL_ICE BIT(6) 37 38 #define M2P_INTERRUPT 0x0004 39 #define M2P_INTERRUPT_STALL BIT(0) 40 #define M2P_INTERRUPT_NFB BIT(1) 41 #define M2P_INTERRUPT_ERROR BIT(3) 42 43 #define M2P_PPALLOC 0x0008 44 #define M2P_STATUS 0x000c 45 46 #define M2P_MAXCNT0 0x0020 47 #define M2P_BASE0 0x0024 48 #define M2P_MAXCNT1 0x0030 49 #define M2P_BASE1 0x0034 50 51 #define M2P_STATE_IDLE 0 52 #define M2P_STATE_STALL 1 53 #define M2P_STATE_ON 2 54 #define M2P_STATE_NEXT 3 55 56 /* M2M registers */ 57 #define M2M_CONTROL 0x0000 58 #define M2M_CONTROL_DONEINT BIT(2) 59 #define M2M_CONTROL_ENABLE BIT(3) 60 #define M2M_CONTROL_START BIT(4) 61 #define M2M_CONTROL_DAH BIT(11) 62 #define M2M_CONTROL_SAH BIT(12) 63 #define M2M_CONTROL_PW_SHIFT 9 64 #define M2M_CONTROL_PW_8 (0 << M2M_CONTROL_PW_SHIFT) 65 #define M2M_CONTROL_PW_16 (1 << M2M_CONTROL_PW_SHIFT) 66 #define M2M_CONTROL_PW_32 (2 << M2M_CONTROL_PW_SHIFT) 67 #define M2M_CONTROL_PW_MASK (3 << M2M_CONTROL_PW_SHIFT) 68 #define M2M_CONTROL_TM_SHIFT 13 69 #define M2M_CONTROL_TM_TX (1 << M2M_CONTROL_TM_SHIFT) 70 #define M2M_CONTROL_TM_RX (2 << M2M_CONTROL_TM_SHIFT) 71 #define M2M_CONTROL_NFBINT BIT(21) 72 #define M2M_CONTROL_RSS_SHIFT 22 73 #define M2M_CONTROL_RSS_SSPRX (1 << M2M_CONTROL_RSS_SHIFT) 74 #define M2M_CONTROL_RSS_SSPTX (2 << M2M_CONTROL_RSS_SHIFT) 75 #define M2M_CONTROL_RSS_IDE (3 << M2M_CONTROL_RSS_SHIFT) 76 #define M2M_CONTROL_NO_HDSK BIT(24) 77 #define M2M_CONTROL_PWSC_SHIFT 25 78 79 #define M2M_INTERRUPT 0x0004 80 #define M2M_INTERRUPT_MASK 6 81 82 #define M2M_STATUS 0x000c 83 #define M2M_STATUS_CTL_SHIFT 1 84 #define M2M_STATUS_CTL_IDLE (0 << M2M_STATUS_CTL_SHIFT) 85 #define M2M_STATUS_CTL_STALL (1 << M2M_STATUS_CTL_SHIFT) 86 #define M2M_STATUS_CTL_MEMRD (2 << M2M_STATUS_CTL_SHIFT) 87 #define M2M_STATUS_CTL_MEMWR (3 << M2M_STATUS_CTL_SHIFT) 88 #define M2M_STATUS_CTL_BWCWAIT (4 << M2M_STATUS_CTL_SHIFT) 89 #define M2M_STATUS_CTL_MASK (7 << M2M_STATUS_CTL_SHIFT) 90 #define M2M_STATUS_BUF_SHIFT 4 91 #define M2M_STATUS_BUF_NO (0 << M2M_STATUS_BUF_SHIFT) 92 #define M2M_STATUS_BUF_ON (1 << M2M_STATUS_BUF_SHIFT) 93 #define M2M_STATUS_BUF_NEXT (2 << M2M_STATUS_BUF_SHIFT) 94 #define M2M_STATUS_BUF_MASK (3 << M2M_STATUS_BUF_SHIFT) 95 #define M2M_STATUS_DONE BIT(6) 96 97 #define M2M_BCR0 0x0010 98 #define M2M_BCR1 0x0014 99 #define M2M_SAR_BASE0 0x0018 100 #define M2M_SAR_BASE1 0x001c 101 #define M2M_DAR_BASE0 0x002c 102 #define M2M_DAR_BASE1 0x0030 103 104 #define DMA_MAX_CHAN_BYTES 0xffff 105 #define DMA_MAX_CHAN_DESCRIPTORS 32 106 107 /* 108 * M2P channels. 109 * 110 * Note that these values are also directly used for setting the PPALLOC 111 * register. 112 */ 113 #define EP93XX_DMA_I2S1 0 114 #define EP93XX_DMA_I2S2 1 115 #define EP93XX_DMA_AAC1 2 116 #define EP93XX_DMA_AAC2 3 117 #define EP93XX_DMA_AAC3 4 118 #define EP93XX_DMA_I2S3 5 119 #define EP93XX_DMA_UART1 6 120 #define EP93XX_DMA_UART2 7 121 #define EP93XX_DMA_UART3 8 122 #define EP93XX_DMA_IRDA 9 123 /* M2M channels */ 124 #define EP93XX_DMA_SSP 10 125 #define EP93XX_DMA_IDE 11 126 127 enum ep93xx_dma_type { 128 M2P_DMA, 129 M2M_DMA, 130 }; 131 132 struct ep93xx_dma_engine; 133 static int ep93xx_dma_slave_config_write(struct dma_chan *chan, 134 enum dma_transfer_direction dir, 135 struct dma_slave_config *config); 136 137 /** 138 * struct ep93xx_dma_desc - EP93xx specific transaction descriptor 139 * @src_addr: source address of the transaction 140 * @dst_addr: destination address of the transaction 141 * @size: size of the transaction (in bytes) 142 * @complete: this descriptor is completed 143 * @txd: dmaengine API descriptor 144 * @tx_list: list of linked descriptors 145 * @node: link used for putting this into a channel queue 146 */ 147 struct ep93xx_dma_desc { 148 u32 src_addr; 149 u32 dst_addr; 150 size_t size; 151 bool complete; 152 struct dma_async_tx_descriptor txd; 153 struct list_head tx_list; 154 struct list_head node; 155 }; 156 157 struct ep93xx_dma_chan_cfg { 158 u8 port; 159 enum dma_transfer_direction dir; 160 }; 161 162 /** 163 * struct ep93xx_dma_chan - an EP93xx DMA M2P/M2M channel 164 * @chan: dmaengine API channel 165 * @edma: pointer to the engine device 166 * @regs: memory mapped registers 167 * @dma_cfg: channel number, direction 168 * @irq: interrupt number of the channel 169 * @clk: clock used by this channel 170 * @tasklet: channel specific tasklet used for callbacks 171 * @lock: lock protecting the fields following 172 * @flags: flags for the channel 173 * @buffer: which buffer to use next (0/1) 174 * @active: flattened chain of descriptors currently being processed 175 * @queue: pending descriptors which are handled next 176 * @free_list: list of free descriptors which can be used 177 * @runtime_addr: physical address currently used as dest/src (M2M only). This 178 * is set via .device_config before slave operation is 179 * prepared 180 * @runtime_ctrl: M2M runtime values for the control register. 181 * @slave_config: slave configuration 182 * 183 * As EP93xx DMA controller doesn't support real chained DMA descriptors we 184 * will have slightly different scheme here: @active points to a head of 185 * flattened DMA descriptor chain. 186 * 187 * @queue holds pending transactions. These are linked through the first 188 * descriptor in the chain. When a descriptor is moved to the @active queue, 189 * the first and chained descriptors are flattened into a single list. 190 * 191 */ 192 struct ep93xx_dma_chan { 193 struct dma_chan chan; 194 const struct ep93xx_dma_engine *edma; 195 void __iomem *regs; 196 struct ep93xx_dma_chan_cfg dma_cfg; 197 int irq; 198 struct clk *clk; 199 struct tasklet_struct tasklet; 200 /* protects the fields following */ 201 spinlock_t lock; 202 unsigned long flags; 203 /* Channel is configured for cyclic transfers */ 204 #define EP93XX_DMA_IS_CYCLIC 0 205 206 int buffer; 207 struct list_head active; 208 struct list_head queue; 209 struct list_head free_list; 210 u32 runtime_addr; 211 u32 runtime_ctrl; 212 struct dma_slave_config slave_config; 213 }; 214 215 /** 216 * struct ep93xx_dma_engine - the EP93xx DMA engine instance 217 * @dma_dev: holds the dmaengine device 218 * @m2m: is this an M2M or M2P device 219 * @hw_setup: method which sets the channel up for operation 220 * @hw_synchronize: synchronizes DMA channel termination to current context 221 * @hw_shutdown: shuts the channel down and flushes whatever is left 222 * @hw_submit: pushes active descriptor(s) to the hardware 223 * @hw_interrupt: handle the interrupt 224 * @num_channels: number of channels for this instance 225 * @channels: array of channels 226 * 227 * There is one instance of this struct for the M2P channels and one for the 228 * M2M channels. hw_xxx() methods are used to perform operations which are 229 * different on M2M and M2P channels. These methods are called with channel 230 * lock held and interrupts disabled so they cannot sleep. 231 */ 232 struct ep93xx_dma_engine { 233 struct dma_device dma_dev; 234 bool m2m; 235 int (*hw_setup)(struct ep93xx_dma_chan *); 236 void (*hw_synchronize)(struct ep93xx_dma_chan *); 237 void (*hw_shutdown)(struct ep93xx_dma_chan *); 238 void (*hw_submit)(struct ep93xx_dma_chan *); 239 int (*hw_interrupt)(struct ep93xx_dma_chan *); 240 #define INTERRUPT_UNKNOWN 0 241 #define INTERRUPT_DONE 1 242 #define INTERRUPT_NEXT_BUFFER 2 243 244 size_t num_channels; 245 struct ep93xx_dma_chan channels[] __counted_by(num_channels); 246 }; 247 248 struct ep93xx_edma_data { 249 u32 id; 250 size_t num_channels; 251 }; 252 253 static inline struct device *chan2dev(struct ep93xx_dma_chan *edmac) 254 { 255 return &edmac->chan.dev->device; 256 } 257 258 static struct ep93xx_dma_chan *to_ep93xx_dma_chan(struct dma_chan *chan) 259 { 260 return container_of(chan, struct ep93xx_dma_chan, chan); 261 } 262 263 static inline bool ep93xx_dma_chan_is_m2p(struct dma_chan *chan) 264 { 265 if (device_is_compatible(chan->device->dev, "cirrus,ep9301-dma-m2p")) 266 return true; 267 268 return !strcmp(dev_name(chan->device->dev), "ep93xx-dma-m2p"); 269 } 270 271 /* 272 * ep93xx_dma_chan_direction - returns direction the channel can be used 273 * 274 * This function can be used in filter functions to find out whether the 275 * channel supports given DMA direction. Only M2P channels have such 276 * limitation, for M2M channels the direction is configurable. 277 */ 278 static inline enum dma_transfer_direction 279 ep93xx_dma_chan_direction(struct dma_chan *chan) 280 { 281 if (!ep93xx_dma_chan_is_m2p(chan)) 282 return DMA_TRANS_NONE; 283 284 /* even channels are for TX, odd for RX */ 285 return (chan->chan_id % 2 == 0) ? DMA_MEM_TO_DEV : DMA_DEV_TO_MEM; 286 } 287 288 /** 289 * ep93xx_dma_set_active - set new active descriptor chain 290 * @edmac: channel 291 * @desc: head of the new active descriptor chain 292 * 293 * Sets @desc to be the head of the new active descriptor chain. This is the 294 * chain which is processed next. The active list must be empty before calling 295 * this function. 296 * 297 * Called with @edmac->lock held and interrupts disabled. 298 */ 299 static void ep93xx_dma_set_active(struct ep93xx_dma_chan *edmac, 300 struct ep93xx_dma_desc *desc) 301 { 302 BUG_ON(!list_empty(&edmac->active)); 303 304 list_add_tail(&desc->node, &edmac->active); 305 306 /* Flatten the @desc->tx_list chain into @edmac->active list */ 307 while (!list_empty(&desc->tx_list)) { 308 struct ep93xx_dma_desc *d = list_first_entry(&desc->tx_list, 309 struct ep93xx_dma_desc, node); 310 311 /* 312 * We copy the callback parameters from the first descriptor 313 * to all the chained descriptors. This way we can call the 314 * callback without having to find out the first descriptor in 315 * the chain. Useful for cyclic transfers. 316 */ 317 d->txd.callback = desc->txd.callback; 318 d->txd.callback_param = desc->txd.callback_param; 319 320 list_move_tail(&d->node, &edmac->active); 321 } 322 } 323 324 /* Called with @edmac->lock held and interrupts disabled */ 325 static struct ep93xx_dma_desc * 326 ep93xx_dma_get_active(struct ep93xx_dma_chan *edmac) 327 { 328 return list_first_entry_or_null(&edmac->active, 329 struct ep93xx_dma_desc, node); 330 } 331 332 /** 333 * ep93xx_dma_advance_active - advances to the next active descriptor 334 * @edmac: channel 335 * 336 * Function advances active descriptor to the next in the @edmac->active and 337 * returns %true if we still have descriptors in the chain to process. 338 * Otherwise returns %false. 339 * 340 * When the channel is in cyclic mode always returns %true. 341 * 342 * Called with @edmac->lock held and interrupts disabled. 343 */ 344 static bool ep93xx_dma_advance_active(struct ep93xx_dma_chan *edmac) 345 { 346 struct ep93xx_dma_desc *desc; 347 348 list_rotate_left(&edmac->active); 349 350 if (test_bit(EP93XX_DMA_IS_CYCLIC, &edmac->flags)) 351 return true; 352 353 desc = ep93xx_dma_get_active(edmac); 354 if (!desc) 355 return false; 356 357 /* 358 * If txd.cookie is set it means that we are back in the first 359 * descriptor in the chain and hence done with it. 360 */ 361 return !desc->txd.cookie; 362 } 363 364 /* 365 * M2P DMA implementation 366 */ 367 368 static void m2p_set_control(struct ep93xx_dma_chan *edmac, u32 control) 369 { 370 writel(control, edmac->regs + M2P_CONTROL); 371 /* 372 * EP93xx User's Guide states that we must perform a dummy read after 373 * write to the control register. 374 */ 375 readl(edmac->regs + M2P_CONTROL); 376 } 377 378 static int m2p_hw_setup(struct ep93xx_dma_chan *edmac) 379 { 380 u32 control; 381 382 writel(edmac->dma_cfg.port & 0xf, edmac->regs + M2P_PPALLOC); 383 384 control = M2P_CONTROL_CH_ERROR_INT | M2P_CONTROL_ICE 385 | M2P_CONTROL_ENABLE; 386 m2p_set_control(edmac, control); 387 388 edmac->buffer = 0; 389 390 return 0; 391 } 392 393 static inline u32 m2p_channel_state(struct ep93xx_dma_chan *edmac) 394 { 395 return (readl(edmac->regs + M2P_STATUS) >> 4) & 0x3; 396 } 397 398 static void m2p_hw_synchronize(struct ep93xx_dma_chan *edmac) 399 { 400 unsigned long flags; 401 u32 control; 402 403 spin_lock_irqsave(&edmac->lock, flags); 404 control = readl(edmac->regs + M2P_CONTROL); 405 control &= ~(M2P_CONTROL_STALLINT | M2P_CONTROL_NFBINT); 406 m2p_set_control(edmac, control); 407 spin_unlock_irqrestore(&edmac->lock, flags); 408 409 while (m2p_channel_state(edmac) >= M2P_STATE_ON) 410 schedule(); 411 } 412 413 static void m2p_hw_shutdown(struct ep93xx_dma_chan *edmac) 414 { 415 m2p_set_control(edmac, 0); 416 417 while (m2p_channel_state(edmac) != M2P_STATE_IDLE) 418 dev_warn(chan2dev(edmac), "M2P: Not yet IDLE\n"); 419 } 420 421 static void m2p_fill_desc(struct ep93xx_dma_chan *edmac) 422 { 423 struct ep93xx_dma_desc *desc; 424 u32 bus_addr; 425 426 desc = ep93xx_dma_get_active(edmac); 427 if (!desc) { 428 dev_warn(chan2dev(edmac), "M2P: empty descriptor list\n"); 429 return; 430 } 431 432 if (ep93xx_dma_chan_direction(&edmac->chan) == DMA_MEM_TO_DEV) 433 bus_addr = desc->src_addr; 434 else 435 bus_addr = desc->dst_addr; 436 437 if (edmac->buffer == 0) { 438 writel(desc->size, edmac->regs + M2P_MAXCNT0); 439 writel(bus_addr, edmac->regs + M2P_BASE0); 440 } else { 441 writel(desc->size, edmac->regs + M2P_MAXCNT1); 442 writel(bus_addr, edmac->regs + M2P_BASE1); 443 } 444 445 edmac->buffer ^= 1; 446 } 447 448 static void m2p_hw_submit(struct ep93xx_dma_chan *edmac) 449 { 450 u32 control = readl(edmac->regs + M2P_CONTROL); 451 452 m2p_fill_desc(edmac); 453 control |= M2P_CONTROL_STALLINT; 454 455 if (ep93xx_dma_advance_active(edmac)) { 456 m2p_fill_desc(edmac); 457 control |= M2P_CONTROL_NFBINT; 458 } 459 460 m2p_set_control(edmac, control); 461 } 462 463 static int m2p_hw_interrupt(struct ep93xx_dma_chan *edmac) 464 { 465 u32 irq_status = readl(edmac->regs + M2P_INTERRUPT); 466 u32 control; 467 468 if (irq_status & M2P_INTERRUPT_ERROR) { 469 struct ep93xx_dma_desc *desc = ep93xx_dma_get_active(edmac); 470 471 /* Clear the error interrupt */ 472 writel(1, edmac->regs + M2P_INTERRUPT); 473 474 /* 475 * It seems that there is no easy way of reporting errors back 476 * to client so we just report the error here and continue as 477 * usual. 478 * 479 * Revisit this when there is a mechanism to report back the 480 * errors. 481 */ 482 dev_err(chan2dev(edmac), 483 "DMA transfer failed! Details:\n" 484 "\tcookie : %d\n" 485 "\tsrc_addr : 0x%08x\n" 486 "\tdst_addr : 0x%08x\n" 487 "\tsize : %zu\n", 488 desc->txd.cookie, desc->src_addr, desc->dst_addr, 489 desc->size); 490 } 491 492 /* 493 * Even latest E2 silicon revision sometimes assert STALL interrupt 494 * instead of NFB. Therefore we treat them equally, basing on the 495 * amount of data we still have to transfer. 496 */ 497 if (!(irq_status & (M2P_INTERRUPT_STALL | M2P_INTERRUPT_NFB))) 498 return INTERRUPT_UNKNOWN; 499 500 if (ep93xx_dma_advance_active(edmac)) { 501 m2p_fill_desc(edmac); 502 return INTERRUPT_NEXT_BUFFER; 503 } 504 505 /* Disable interrupts */ 506 control = readl(edmac->regs + M2P_CONTROL); 507 control &= ~(M2P_CONTROL_STALLINT | M2P_CONTROL_NFBINT); 508 m2p_set_control(edmac, control); 509 510 return INTERRUPT_DONE; 511 } 512 513 /* 514 * M2M DMA implementation 515 */ 516 517 static int m2m_hw_setup(struct ep93xx_dma_chan *edmac) 518 { 519 u32 control = 0; 520 521 if (edmac->dma_cfg.dir == DMA_MEM_TO_MEM) { 522 /* This is memcpy channel, nothing to configure */ 523 writel(control, edmac->regs + M2M_CONTROL); 524 return 0; 525 } 526 527 switch (edmac->dma_cfg.port) { 528 case EP93XX_DMA_SSP: 529 /* 530 * This was found via experimenting - anything less than 5 531 * causes the channel to perform only a partial transfer which 532 * leads to problems since we don't get DONE interrupt then. 533 */ 534 control = (5 << M2M_CONTROL_PWSC_SHIFT); 535 control |= M2M_CONTROL_NO_HDSK; 536 537 if (edmac->dma_cfg.dir == DMA_MEM_TO_DEV) { 538 control |= M2M_CONTROL_DAH; 539 control |= M2M_CONTROL_TM_TX; 540 control |= M2M_CONTROL_RSS_SSPTX; 541 } else { 542 control |= M2M_CONTROL_SAH; 543 control |= M2M_CONTROL_TM_RX; 544 control |= M2M_CONTROL_RSS_SSPRX; 545 } 546 break; 547 548 case EP93XX_DMA_IDE: 549 /* 550 * This IDE part is totally untested. Values below are taken 551 * from the EP93xx Users's Guide and might not be correct. 552 */ 553 if (edmac->dma_cfg.dir == DMA_MEM_TO_DEV) { 554 /* Worst case from the UG */ 555 control = (3 << M2M_CONTROL_PWSC_SHIFT); 556 control |= M2M_CONTROL_DAH; 557 control |= M2M_CONTROL_TM_TX; 558 } else { 559 control = (2 << M2M_CONTROL_PWSC_SHIFT); 560 control |= M2M_CONTROL_SAH; 561 control |= M2M_CONTROL_TM_RX; 562 } 563 564 control |= M2M_CONTROL_NO_HDSK; 565 control |= M2M_CONTROL_RSS_IDE; 566 control |= M2M_CONTROL_PW_16; 567 break; 568 569 default: 570 return -EINVAL; 571 } 572 573 writel(control, edmac->regs + M2M_CONTROL); 574 return 0; 575 } 576 577 static void m2m_hw_shutdown(struct ep93xx_dma_chan *edmac) 578 { 579 /* Just disable the channel */ 580 writel(0, edmac->regs + M2M_CONTROL); 581 } 582 583 static void m2m_fill_desc(struct ep93xx_dma_chan *edmac) 584 { 585 struct ep93xx_dma_desc *desc; 586 587 desc = ep93xx_dma_get_active(edmac); 588 if (!desc) { 589 dev_warn(chan2dev(edmac), "M2M: empty descriptor list\n"); 590 return; 591 } 592 593 if (edmac->buffer == 0) { 594 writel(desc->src_addr, edmac->regs + M2M_SAR_BASE0); 595 writel(desc->dst_addr, edmac->regs + M2M_DAR_BASE0); 596 writel(desc->size, edmac->regs + M2M_BCR0); 597 } else { 598 writel(desc->src_addr, edmac->regs + M2M_SAR_BASE1); 599 writel(desc->dst_addr, edmac->regs + M2M_DAR_BASE1); 600 writel(desc->size, edmac->regs + M2M_BCR1); 601 } 602 603 edmac->buffer ^= 1; 604 } 605 606 static void m2m_hw_submit(struct ep93xx_dma_chan *edmac) 607 { 608 u32 control = readl(edmac->regs + M2M_CONTROL); 609 610 /* 611 * Since we allow clients to configure PW (peripheral width) we always 612 * clear PW bits here and then set them according what is given in 613 * the runtime configuration. 614 */ 615 control &= ~M2M_CONTROL_PW_MASK; 616 control |= edmac->runtime_ctrl; 617 618 m2m_fill_desc(edmac); 619 control |= M2M_CONTROL_DONEINT; 620 621 if (ep93xx_dma_advance_active(edmac)) { 622 m2m_fill_desc(edmac); 623 control |= M2M_CONTROL_NFBINT; 624 } 625 626 /* 627 * Now we can finally enable the channel. For M2M channel this must be 628 * done _after_ the BCRx registers are programmed. 629 */ 630 control |= M2M_CONTROL_ENABLE; 631 writel(control, edmac->regs + M2M_CONTROL); 632 633 if (edmac->dma_cfg.dir == DMA_MEM_TO_MEM) { 634 /* 635 * For memcpy channels the software trigger must be asserted 636 * in order to start the memcpy operation. 637 */ 638 control |= M2M_CONTROL_START; 639 writel(control, edmac->regs + M2M_CONTROL); 640 } 641 } 642 643 /* 644 * According to EP93xx User's Guide, we should receive DONE interrupt when all 645 * M2M DMA controller transactions complete normally. This is not always the 646 * case - sometimes EP93xx M2M DMA asserts DONE interrupt when the DMA channel 647 * is still running (channel Buffer FSM in DMA_BUF_ON state, and channel 648 * Control FSM in DMA_MEM_RD state, observed at least in IDE-DMA operation). 649 * In effect, disabling the channel when only DONE bit is set could stop 650 * currently running DMA transfer. To avoid this, we use Buffer FSM and 651 * Control FSM to check current state of DMA channel. 652 */ 653 static int m2m_hw_interrupt(struct ep93xx_dma_chan *edmac) 654 { 655 u32 status = readl(edmac->regs + M2M_STATUS); 656 u32 ctl_fsm = status & M2M_STATUS_CTL_MASK; 657 u32 buf_fsm = status & M2M_STATUS_BUF_MASK; 658 bool done = status & M2M_STATUS_DONE; 659 bool last_done; 660 u32 control; 661 struct ep93xx_dma_desc *desc; 662 663 /* Accept only DONE and NFB interrupts */ 664 if (!(readl(edmac->regs + M2M_INTERRUPT) & M2M_INTERRUPT_MASK)) 665 return INTERRUPT_UNKNOWN; 666 667 if (done) { 668 /* Clear the DONE bit */ 669 writel(0, edmac->regs + M2M_INTERRUPT); 670 } 671 672 /* 673 * Check whether we are done with descriptors or not. This, together 674 * with DMA channel state, determines action to take in interrupt. 675 */ 676 desc = ep93xx_dma_get_active(edmac); 677 last_done = !desc || desc->txd.cookie; 678 679 /* 680 * Use M2M DMA Buffer FSM and Control FSM to check current state of 681 * DMA channel. Using DONE and NFB bits from channel status register 682 * or bits from channel interrupt register is not reliable. 683 */ 684 if (!last_done && 685 (buf_fsm == M2M_STATUS_BUF_NO || 686 buf_fsm == M2M_STATUS_BUF_ON)) { 687 /* 688 * Two buffers are ready for update when Buffer FSM is in 689 * DMA_NO_BUF state. Only one buffer can be prepared without 690 * disabling the channel or polling the DONE bit. 691 * To simplify things, always prepare only one buffer. 692 */ 693 if (ep93xx_dma_advance_active(edmac)) { 694 m2m_fill_desc(edmac); 695 if (done && edmac->dma_cfg.dir == DMA_MEM_TO_MEM) { 696 /* Software trigger for memcpy channel */ 697 control = readl(edmac->regs + M2M_CONTROL); 698 control |= M2M_CONTROL_START; 699 writel(control, edmac->regs + M2M_CONTROL); 700 } 701 return INTERRUPT_NEXT_BUFFER; 702 } else { 703 last_done = true; 704 } 705 } 706 707 /* 708 * Disable the channel only when Buffer FSM is in DMA_NO_BUF state 709 * and Control FSM is in DMA_STALL state. 710 */ 711 if (last_done && 712 buf_fsm == M2M_STATUS_BUF_NO && 713 ctl_fsm == M2M_STATUS_CTL_STALL) { 714 /* Disable interrupts and the channel */ 715 control = readl(edmac->regs + M2M_CONTROL); 716 control &= ~(M2M_CONTROL_DONEINT | M2M_CONTROL_NFBINT 717 | M2M_CONTROL_ENABLE); 718 writel(control, edmac->regs + M2M_CONTROL); 719 return INTERRUPT_DONE; 720 } 721 722 /* 723 * Nothing to do this time. 724 */ 725 return INTERRUPT_NEXT_BUFFER; 726 } 727 728 /* 729 * DMA engine API implementation 730 */ 731 732 static struct ep93xx_dma_desc * 733 ep93xx_dma_desc_get(struct ep93xx_dma_chan *edmac) 734 { 735 struct ep93xx_dma_desc *desc, *_desc; 736 struct ep93xx_dma_desc *ret = NULL; 737 unsigned long flags; 738 739 spin_lock_irqsave(&edmac->lock, flags); 740 list_for_each_entry_safe(desc, _desc, &edmac->free_list, node) { 741 if (async_tx_test_ack(&desc->txd)) { 742 list_del_init(&desc->node); 743 744 /* Re-initialize the descriptor */ 745 desc->src_addr = 0; 746 desc->dst_addr = 0; 747 desc->size = 0; 748 desc->complete = false; 749 desc->txd.cookie = 0; 750 desc->txd.callback = NULL; 751 desc->txd.callback_param = NULL; 752 753 ret = desc; 754 break; 755 } 756 } 757 spin_unlock_irqrestore(&edmac->lock, flags); 758 return ret; 759 } 760 761 static void ep93xx_dma_desc_put(struct ep93xx_dma_chan *edmac, 762 struct ep93xx_dma_desc *desc) 763 { 764 if (desc) { 765 unsigned long flags; 766 767 spin_lock_irqsave(&edmac->lock, flags); 768 list_splice_init(&desc->tx_list, &edmac->free_list); 769 list_add(&desc->node, &edmac->free_list); 770 spin_unlock_irqrestore(&edmac->lock, flags); 771 } 772 } 773 774 /** 775 * ep93xx_dma_advance_work - start processing the next pending transaction 776 * @edmac: channel 777 * 778 * If we have pending transactions queued and we are currently idling, this 779 * function takes the next queued transaction from the @edmac->queue and 780 * pushes it to the hardware for execution. 781 */ 782 static void ep93xx_dma_advance_work(struct ep93xx_dma_chan *edmac) 783 { 784 struct ep93xx_dma_desc *new; 785 unsigned long flags; 786 787 spin_lock_irqsave(&edmac->lock, flags); 788 if (!list_empty(&edmac->active) || list_empty(&edmac->queue)) { 789 spin_unlock_irqrestore(&edmac->lock, flags); 790 return; 791 } 792 793 /* Take the next descriptor from the pending queue */ 794 new = list_first_entry(&edmac->queue, struct ep93xx_dma_desc, node); 795 list_del_init(&new->node); 796 797 ep93xx_dma_set_active(edmac, new); 798 799 /* Push it to the hardware */ 800 edmac->edma->hw_submit(edmac); 801 spin_unlock_irqrestore(&edmac->lock, flags); 802 } 803 804 static void ep93xx_dma_tasklet(struct tasklet_struct *t) 805 { 806 struct ep93xx_dma_chan *edmac = from_tasklet(edmac, t, tasklet); 807 struct ep93xx_dma_desc *desc, *d; 808 struct dmaengine_desc_callback cb; 809 LIST_HEAD(list); 810 811 memset(&cb, 0, sizeof(cb)); 812 spin_lock_irq(&edmac->lock); 813 /* 814 * If dma_terminate_all() was called before we get to run, the active 815 * list has become empty. If that happens we aren't supposed to do 816 * anything more than call ep93xx_dma_advance_work(). 817 */ 818 desc = ep93xx_dma_get_active(edmac); 819 if (desc) { 820 if (desc->complete) { 821 /* mark descriptor complete for non cyclic case only */ 822 if (!test_bit(EP93XX_DMA_IS_CYCLIC, &edmac->flags)) 823 dma_cookie_complete(&desc->txd); 824 list_splice_init(&edmac->active, &list); 825 } 826 dmaengine_desc_get_callback(&desc->txd, &cb); 827 } 828 spin_unlock_irq(&edmac->lock); 829 830 /* Pick up the next descriptor from the queue */ 831 ep93xx_dma_advance_work(edmac); 832 833 /* Now we can release all the chained descriptors */ 834 list_for_each_entry_safe(desc, d, &list, node) { 835 dma_descriptor_unmap(&desc->txd); 836 ep93xx_dma_desc_put(edmac, desc); 837 } 838 839 dmaengine_desc_callback_invoke(&cb, NULL); 840 } 841 842 static irqreturn_t ep93xx_dma_interrupt(int irq, void *dev_id) 843 { 844 struct ep93xx_dma_chan *edmac = dev_id; 845 struct ep93xx_dma_desc *desc; 846 irqreturn_t ret = IRQ_HANDLED; 847 848 spin_lock(&edmac->lock); 849 850 desc = ep93xx_dma_get_active(edmac); 851 if (!desc) { 852 dev_warn(chan2dev(edmac), 853 "got interrupt while active list is empty\n"); 854 spin_unlock(&edmac->lock); 855 return IRQ_NONE; 856 } 857 858 switch (edmac->edma->hw_interrupt(edmac)) { 859 case INTERRUPT_DONE: 860 desc->complete = true; 861 tasklet_schedule(&edmac->tasklet); 862 break; 863 864 case INTERRUPT_NEXT_BUFFER: 865 if (test_bit(EP93XX_DMA_IS_CYCLIC, &edmac->flags)) 866 tasklet_schedule(&edmac->tasklet); 867 break; 868 869 default: 870 dev_warn(chan2dev(edmac), "unknown interrupt!\n"); 871 ret = IRQ_NONE; 872 break; 873 } 874 875 spin_unlock(&edmac->lock); 876 return ret; 877 } 878 879 /** 880 * ep93xx_dma_tx_submit - set the prepared descriptor(s) to be executed 881 * @tx: descriptor to be executed 882 * 883 * Function will execute given descriptor on the hardware or if the hardware 884 * is busy, queue the descriptor to be executed later on. Returns cookie which 885 * can be used to poll the status of the descriptor. 886 */ 887 static dma_cookie_t ep93xx_dma_tx_submit(struct dma_async_tx_descriptor *tx) 888 { 889 struct ep93xx_dma_chan *edmac = to_ep93xx_dma_chan(tx->chan); 890 struct ep93xx_dma_desc *desc; 891 dma_cookie_t cookie; 892 unsigned long flags; 893 894 spin_lock_irqsave(&edmac->lock, flags); 895 cookie = dma_cookie_assign(tx); 896 897 desc = container_of(tx, struct ep93xx_dma_desc, txd); 898 899 /* 900 * If nothing is currently processed, we push this descriptor 901 * directly to the hardware. Otherwise we put the descriptor 902 * to the pending queue. 903 */ 904 if (list_empty(&edmac->active)) { 905 ep93xx_dma_set_active(edmac, desc); 906 edmac->edma->hw_submit(edmac); 907 } else { 908 list_add_tail(&desc->node, &edmac->queue); 909 } 910 911 spin_unlock_irqrestore(&edmac->lock, flags); 912 return cookie; 913 } 914 915 /** 916 * ep93xx_dma_alloc_chan_resources - allocate resources for the channel 917 * @chan: channel to allocate resources 918 * 919 * Function allocates necessary resources for the given DMA channel and 920 * returns number of allocated descriptors for the channel. Negative errno 921 * is returned in case of failure. 922 */ 923 static int ep93xx_dma_alloc_chan_resources(struct dma_chan *chan) 924 { 925 struct ep93xx_dma_chan *edmac = to_ep93xx_dma_chan(chan); 926 const char *name = dma_chan_name(chan); 927 int ret, i; 928 929 /* Sanity check the channel parameters */ 930 if (!edmac->edma->m2m) { 931 if (edmac->dma_cfg.port > EP93XX_DMA_IRDA) 932 return -EINVAL; 933 if (edmac->dma_cfg.dir != ep93xx_dma_chan_direction(chan)) 934 return -EINVAL; 935 } else { 936 if (edmac->dma_cfg.dir != DMA_MEM_TO_MEM) { 937 switch (edmac->dma_cfg.port) { 938 case EP93XX_DMA_SSP: 939 case EP93XX_DMA_IDE: 940 if (!is_slave_direction(edmac->dma_cfg.dir)) 941 return -EINVAL; 942 break; 943 default: 944 return -EINVAL; 945 } 946 } 947 } 948 949 ret = clk_prepare_enable(edmac->clk); 950 if (ret) 951 return ret; 952 953 ret = request_irq(edmac->irq, ep93xx_dma_interrupt, 0, name, edmac); 954 if (ret) 955 goto fail_clk_disable; 956 957 spin_lock_irq(&edmac->lock); 958 dma_cookie_init(&edmac->chan); 959 ret = edmac->edma->hw_setup(edmac); 960 spin_unlock_irq(&edmac->lock); 961 962 if (ret) 963 goto fail_free_irq; 964 965 for (i = 0; i < DMA_MAX_CHAN_DESCRIPTORS; i++) { 966 struct ep93xx_dma_desc *desc; 967 968 desc = kzalloc_obj(*desc); 969 if (!desc) { 970 dev_warn(chan2dev(edmac), "not enough descriptors\n"); 971 break; 972 } 973 974 INIT_LIST_HEAD(&desc->tx_list); 975 976 dma_async_tx_descriptor_init(&desc->txd, chan); 977 desc->txd.flags = DMA_CTRL_ACK; 978 desc->txd.tx_submit = ep93xx_dma_tx_submit; 979 980 ep93xx_dma_desc_put(edmac, desc); 981 } 982 983 return i; 984 985 fail_free_irq: 986 free_irq(edmac->irq, edmac); 987 fail_clk_disable: 988 clk_disable_unprepare(edmac->clk); 989 990 return ret; 991 } 992 993 /** 994 * ep93xx_dma_free_chan_resources - release resources for the channel 995 * @chan: channel 996 * 997 * Function releases all the resources allocated for the given channel. 998 * The channel must be idle when this is called. 999 */ 1000 static void ep93xx_dma_free_chan_resources(struct dma_chan *chan) 1001 { 1002 struct ep93xx_dma_chan *edmac = to_ep93xx_dma_chan(chan); 1003 struct ep93xx_dma_desc *desc, *d; 1004 unsigned long flags; 1005 LIST_HEAD(list); 1006 1007 BUG_ON(!list_empty(&edmac->active)); 1008 BUG_ON(!list_empty(&edmac->queue)); 1009 1010 spin_lock_irqsave(&edmac->lock, flags); 1011 edmac->edma->hw_shutdown(edmac); 1012 edmac->runtime_addr = 0; 1013 edmac->runtime_ctrl = 0; 1014 edmac->buffer = 0; 1015 list_splice_init(&edmac->free_list, &list); 1016 spin_unlock_irqrestore(&edmac->lock, flags); 1017 1018 list_for_each_entry_safe(desc, d, &list, node) 1019 kfree(desc); 1020 1021 clk_disable_unprepare(edmac->clk); 1022 free_irq(edmac->irq, edmac); 1023 } 1024 1025 /** 1026 * ep93xx_dma_prep_dma_memcpy - prepare a memcpy DMA operation 1027 * @chan: channel 1028 * @dest: destination bus address 1029 * @src: source bus address 1030 * @len: size of the transaction 1031 * @flags: flags for the descriptor 1032 * 1033 * Returns a valid DMA descriptor or %NULL in case of failure. 1034 */ 1035 static struct dma_async_tx_descriptor * 1036 ep93xx_dma_prep_dma_memcpy(struct dma_chan *chan, dma_addr_t dest, 1037 dma_addr_t src, size_t len, unsigned long flags) 1038 { 1039 struct ep93xx_dma_chan *edmac = to_ep93xx_dma_chan(chan); 1040 struct ep93xx_dma_desc *desc, *first; 1041 size_t bytes, offset; 1042 1043 first = NULL; 1044 for (offset = 0; offset < len; offset += bytes) { 1045 desc = ep93xx_dma_desc_get(edmac); 1046 if (!desc) { 1047 dev_warn(chan2dev(edmac), "couldn't get descriptor\n"); 1048 goto fail; 1049 } 1050 1051 bytes = min_t(size_t, len - offset, DMA_MAX_CHAN_BYTES); 1052 1053 desc->src_addr = src + offset; 1054 desc->dst_addr = dest + offset; 1055 desc->size = bytes; 1056 1057 if (!first) 1058 first = desc; 1059 else 1060 list_add_tail(&desc->node, &first->tx_list); 1061 } 1062 1063 first->txd.cookie = -EBUSY; 1064 first->txd.flags = flags; 1065 1066 return &first->txd; 1067 fail: 1068 ep93xx_dma_desc_put(edmac, first); 1069 return NULL; 1070 } 1071 1072 /** 1073 * ep93xx_dma_prep_slave_sg - prepare a slave DMA operation 1074 * @chan: channel 1075 * @sgl: list of buffers to transfer 1076 * @sg_len: number of entries in @sgl 1077 * @dir: direction of the DMA transfer 1078 * @flags: flags for the descriptor 1079 * @context: operation context (ignored) 1080 * 1081 * Returns a valid DMA descriptor or %NULL in case of failure. 1082 */ 1083 static struct dma_async_tx_descriptor * 1084 ep93xx_dma_prep_slave_sg(struct dma_chan *chan, struct scatterlist *sgl, 1085 unsigned int sg_len, enum dma_transfer_direction dir, 1086 unsigned long flags, void *context) 1087 { 1088 struct ep93xx_dma_chan *edmac = to_ep93xx_dma_chan(chan); 1089 struct ep93xx_dma_desc *desc, *first; 1090 struct scatterlist *sg; 1091 int i; 1092 1093 if (!edmac->edma->m2m && dir != ep93xx_dma_chan_direction(chan)) { 1094 dev_warn(chan2dev(edmac), 1095 "channel was configured with different direction\n"); 1096 return NULL; 1097 } 1098 1099 if (test_bit(EP93XX_DMA_IS_CYCLIC, &edmac->flags)) { 1100 dev_warn(chan2dev(edmac), 1101 "channel is already used for cyclic transfers\n"); 1102 return NULL; 1103 } 1104 1105 ep93xx_dma_slave_config_write(chan, dir, &edmac->slave_config); 1106 1107 first = NULL; 1108 for_each_sg(sgl, sg, sg_len, i) { 1109 size_t len = sg_dma_len(sg); 1110 1111 if (len > DMA_MAX_CHAN_BYTES) { 1112 dev_warn(chan2dev(edmac), "too big transfer size %zu\n", 1113 len); 1114 goto fail; 1115 } 1116 1117 desc = ep93xx_dma_desc_get(edmac); 1118 if (!desc) { 1119 dev_warn(chan2dev(edmac), "couldn't get descriptor\n"); 1120 goto fail; 1121 } 1122 1123 if (dir == DMA_MEM_TO_DEV) { 1124 desc->src_addr = sg_dma_address(sg); 1125 desc->dst_addr = edmac->runtime_addr; 1126 } else { 1127 desc->src_addr = edmac->runtime_addr; 1128 desc->dst_addr = sg_dma_address(sg); 1129 } 1130 desc->size = len; 1131 1132 if (!first) 1133 first = desc; 1134 else 1135 list_add_tail(&desc->node, &first->tx_list); 1136 } 1137 1138 first->txd.cookie = -EBUSY; 1139 first->txd.flags = flags; 1140 1141 return &first->txd; 1142 1143 fail: 1144 ep93xx_dma_desc_put(edmac, first); 1145 return NULL; 1146 } 1147 1148 /** 1149 * ep93xx_dma_prep_dma_cyclic - prepare a cyclic DMA operation 1150 * @chan: channel 1151 * @dma_addr: DMA mapped address of the buffer 1152 * @buf_len: length of the buffer (in bytes) 1153 * @period_len: length of a single period 1154 * @dir: direction of the operation 1155 * @flags: tx descriptor status flags 1156 * 1157 * Prepares a descriptor for cyclic DMA operation. This means that once the 1158 * descriptor is submitted, we will be submitting in a @period_len sized 1159 * buffers and calling callback once the period has been elapsed. Transfer 1160 * terminates only when client calls dmaengine_terminate_all() for this 1161 * channel. 1162 * 1163 * Returns a valid DMA descriptor or %NULL in case of failure. 1164 */ 1165 static struct dma_async_tx_descriptor * 1166 ep93xx_dma_prep_dma_cyclic(struct dma_chan *chan, dma_addr_t dma_addr, 1167 size_t buf_len, size_t period_len, 1168 enum dma_transfer_direction dir, unsigned long flags) 1169 { 1170 struct ep93xx_dma_chan *edmac = to_ep93xx_dma_chan(chan); 1171 struct ep93xx_dma_desc *desc, *first; 1172 size_t offset = 0; 1173 1174 if (!edmac->edma->m2m && dir != ep93xx_dma_chan_direction(chan)) { 1175 dev_warn(chan2dev(edmac), 1176 "channel was configured with different direction\n"); 1177 return NULL; 1178 } 1179 1180 if (test_and_set_bit(EP93XX_DMA_IS_CYCLIC, &edmac->flags)) { 1181 dev_warn(chan2dev(edmac), 1182 "channel is already used for cyclic transfers\n"); 1183 return NULL; 1184 } 1185 1186 if (period_len > DMA_MAX_CHAN_BYTES) { 1187 dev_warn(chan2dev(edmac), "too big period length %zu\n", 1188 period_len); 1189 return NULL; 1190 } 1191 1192 ep93xx_dma_slave_config_write(chan, dir, &edmac->slave_config); 1193 1194 /* Split the buffer into period size chunks */ 1195 first = NULL; 1196 for (offset = 0; offset < buf_len; offset += period_len) { 1197 desc = ep93xx_dma_desc_get(edmac); 1198 if (!desc) { 1199 dev_warn(chan2dev(edmac), "couldn't get descriptor\n"); 1200 goto fail; 1201 } 1202 1203 if (dir == DMA_MEM_TO_DEV) { 1204 desc->src_addr = dma_addr + offset; 1205 desc->dst_addr = edmac->runtime_addr; 1206 } else { 1207 desc->src_addr = edmac->runtime_addr; 1208 desc->dst_addr = dma_addr + offset; 1209 } 1210 1211 desc->size = period_len; 1212 1213 if (!first) 1214 first = desc; 1215 else 1216 list_add_tail(&desc->node, &first->tx_list); 1217 } 1218 1219 first->txd.cookie = -EBUSY; 1220 1221 return &first->txd; 1222 1223 fail: 1224 ep93xx_dma_desc_put(edmac, first); 1225 return NULL; 1226 } 1227 1228 /** 1229 * ep93xx_dma_synchronize - Synchronizes the termination of transfers to the 1230 * current context. 1231 * @chan: channel 1232 * 1233 * Synchronizes the DMA channel termination to the current context. When this 1234 * function returns it is guaranteed that all transfers for previously issued 1235 * descriptors have stopped and it is safe to free the memory associated 1236 * with them. Furthermore it is guaranteed that all complete callback functions 1237 * for a previously submitted descriptor have finished running and it is safe to 1238 * free resources accessed from within the complete callbacks. 1239 */ 1240 static void ep93xx_dma_synchronize(struct dma_chan *chan) 1241 { 1242 struct ep93xx_dma_chan *edmac = to_ep93xx_dma_chan(chan); 1243 1244 if (edmac->edma->hw_synchronize) 1245 edmac->edma->hw_synchronize(edmac); 1246 } 1247 1248 /** 1249 * ep93xx_dma_terminate_all - terminate all transactions 1250 * @chan: channel 1251 * 1252 * Stops all DMA transactions. All descriptors are put back to the 1253 * @edmac->free_list and callbacks are _not_ called. 1254 */ 1255 static int ep93xx_dma_terminate_all(struct dma_chan *chan) 1256 { 1257 struct ep93xx_dma_chan *edmac = to_ep93xx_dma_chan(chan); 1258 struct ep93xx_dma_desc *desc, *_d; 1259 unsigned long flags; 1260 LIST_HEAD(list); 1261 1262 spin_lock_irqsave(&edmac->lock, flags); 1263 /* First we disable and flush the DMA channel */ 1264 edmac->edma->hw_shutdown(edmac); 1265 clear_bit(EP93XX_DMA_IS_CYCLIC, &edmac->flags); 1266 list_splice_init(&edmac->active, &list); 1267 list_splice_init(&edmac->queue, &list); 1268 /* 1269 * We then re-enable the channel. This way we can continue submitting 1270 * the descriptors by just calling ->hw_submit() again. 1271 */ 1272 edmac->edma->hw_setup(edmac); 1273 spin_unlock_irqrestore(&edmac->lock, flags); 1274 1275 list_for_each_entry_safe(desc, _d, &list, node) 1276 ep93xx_dma_desc_put(edmac, desc); 1277 1278 return 0; 1279 } 1280 1281 static int ep93xx_dma_slave_config(struct dma_chan *chan, 1282 struct dma_slave_config *config) 1283 { 1284 struct ep93xx_dma_chan *edmac = to_ep93xx_dma_chan(chan); 1285 1286 memcpy(&edmac->slave_config, config, sizeof(*config)); 1287 1288 return 0; 1289 } 1290 1291 static int ep93xx_dma_slave_config_write(struct dma_chan *chan, 1292 enum dma_transfer_direction dir, 1293 struct dma_slave_config *config) 1294 { 1295 struct ep93xx_dma_chan *edmac = to_ep93xx_dma_chan(chan); 1296 enum dma_slave_buswidth width; 1297 unsigned long flags; 1298 u32 addr, ctrl; 1299 1300 if (!edmac->edma->m2m) 1301 return -EINVAL; 1302 1303 switch (dir) { 1304 case DMA_DEV_TO_MEM: 1305 width = config->src_addr_width; 1306 addr = config->src_addr; 1307 break; 1308 1309 case DMA_MEM_TO_DEV: 1310 width = config->dst_addr_width; 1311 addr = config->dst_addr; 1312 break; 1313 1314 default: 1315 return -EINVAL; 1316 } 1317 1318 switch (width) { 1319 case DMA_SLAVE_BUSWIDTH_1_BYTE: 1320 ctrl = 0; 1321 break; 1322 case DMA_SLAVE_BUSWIDTH_2_BYTES: 1323 ctrl = M2M_CONTROL_PW_16; 1324 break; 1325 case DMA_SLAVE_BUSWIDTH_4_BYTES: 1326 ctrl = M2M_CONTROL_PW_32; 1327 break; 1328 default: 1329 return -EINVAL; 1330 } 1331 1332 spin_lock_irqsave(&edmac->lock, flags); 1333 edmac->runtime_addr = addr; 1334 edmac->runtime_ctrl = ctrl; 1335 spin_unlock_irqrestore(&edmac->lock, flags); 1336 1337 return 0; 1338 } 1339 1340 /** 1341 * ep93xx_dma_tx_status - check if a transaction is completed 1342 * @chan: channel 1343 * @cookie: transaction specific cookie 1344 * @state: state of the transaction is stored here if given 1345 * 1346 * This function can be used to query state of a given transaction. 1347 */ 1348 static enum dma_status ep93xx_dma_tx_status(struct dma_chan *chan, 1349 dma_cookie_t cookie, 1350 struct dma_tx_state *state) 1351 { 1352 return dma_cookie_status(chan, cookie, state); 1353 } 1354 1355 /** 1356 * ep93xx_dma_issue_pending - push pending transactions to the hardware 1357 * @chan: channel 1358 * 1359 * When this function is called, all pending transactions are pushed to the 1360 * hardware and executed. 1361 */ 1362 static void ep93xx_dma_issue_pending(struct dma_chan *chan) 1363 { 1364 ep93xx_dma_advance_work(to_ep93xx_dma_chan(chan)); 1365 } 1366 1367 static struct ep93xx_dma_engine *ep93xx_dma_of_probe(struct platform_device *pdev) 1368 { 1369 const struct ep93xx_edma_data *data; 1370 struct device *dev = &pdev->dev; 1371 struct ep93xx_dma_engine *edma; 1372 struct dma_device *dma_dev; 1373 char dma_clk_name[5]; 1374 int i; 1375 1376 data = device_get_match_data(dev); 1377 if (!data) 1378 return ERR_PTR(dev_err_probe(dev, -ENODEV, "No device match found\n")); 1379 1380 edma = devm_kzalloc(dev, struct_size(edma, channels, data->num_channels), 1381 GFP_KERNEL); 1382 if (!edma) 1383 return ERR_PTR(-ENOMEM); 1384 1385 edma->m2m = data->id; 1386 edma->num_channels = data->num_channels; 1387 dma_dev = &edma->dma_dev; 1388 1389 INIT_LIST_HEAD(&dma_dev->channels); 1390 for (i = 0; i < edma->num_channels; i++) { 1391 struct ep93xx_dma_chan *edmac = &edma->channels[i]; 1392 int len; 1393 1394 edmac->chan.device = dma_dev; 1395 edmac->regs = devm_platform_ioremap_resource(pdev, i); 1396 if (IS_ERR(edmac->regs)) 1397 return ERR_CAST(edmac->regs); 1398 1399 edmac->irq = fwnode_irq_get(dev_fwnode(dev), i); 1400 if (edmac->irq < 0) 1401 return ERR_PTR(edmac->irq); 1402 1403 edmac->edma = edma; 1404 1405 if (edma->m2m) 1406 len = snprintf(dma_clk_name, sizeof(dma_clk_name), "m2m%u", i); 1407 else 1408 len = snprintf(dma_clk_name, sizeof(dma_clk_name), "m2p%u", i); 1409 if (len >= sizeof(dma_clk_name)) 1410 return ERR_PTR(-ENOBUFS); 1411 1412 edmac->clk = devm_clk_get(dev, dma_clk_name); 1413 if (IS_ERR(edmac->clk)) { 1414 dev_err_probe(dev, PTR_ERR(edmac->clk), 1415 "no %s clock found\n", dma_clk_name); 1416 return ERR_CAST(edmac->clk); 1417 } 1418 1419 spin_lock_init(&edmac->lock); 1420 INIT_LIST_HEAD(&edmac->active); 1421 INIT_LIST_HEAD(&edmac->queue); 1422 INIT_LIST_HEAD(&edmac->free_list); 1423 tasklet_setup(&edmac->tasklet, ep93xx_dma_tasklet); 1424 1425 list_add_tail(&edmac->chan.device_node, 1426 &dma_dev->channels); 1427 } 1428 1429 return edma; 1430 } 1431 1432 static bool ep93xx_m2p_dma_filter(struct dma_chan *chan, void *filter_param) 1433 { 1434 struct ep93xx_dma_chan *echan = to_ep93xx_dma_chan(chan); 1435 struct ep93xx_dma_chan_cfg *cfg = filter_param; 1436 1437 if (cfg->dir != ep93xx_dma_chan_direction(chan)) 1438 return false; 1439 1440 echan->dma_cfg = *cfg; 1441 return true; 1442 } 1443 1444 static struct dma_chan *ep93xx_m2p_dma_of_xlate(struct of_phandle_args *dma_spec, 1445 struct of_dma *ofdma) 1446 { 1447 struct ep93xx_dma_engine *edma = ofdma->of_dma_data; 1448 dma_cap_mask_t mask = edma->dma_dev.cap_mask; 1449 struct ep93xx_dma_chan_cfg dma_cfg; 1450 u8 port = dma_spec->args[0]; 1451 u8 direction = dma_spec->args[1]; 1452 1453 if (port > EP93XX_DMA_IRDA) 1454 return NULL; 1455 1456 if (!is_slave_direction(direction)) 1457 return NULL; 1458 1459 dma_cfg.port = port; 1460 dma_cfg.dir = direction; 1461 1462 return __dma_request_channel(&mask, ep93xx_m2p_dma_filter, &dma_cfg, ofdma->of_node); 1463 } 1464 1465 static bool ep93xx_m2m_dma_filter(struct dma_chan *chan, void *filter_param) 1466 { 1467 struct ep93xx_dma_chan *echan = to_ep93xx_dma_chan(chan); 1468 struct ep93xx_dma_chan_cfg *cfg = filter_param; 1469 1470 echan->dma_cfg = *cfg; 1471 1472 return true; 1473 } 1474 1475 static struct dma_chan *ep93xx_m2m_dma_of_xlate(struct of_phandle_args *dma_spec, 1476 struct of_dma *ofdma) 1477 { 1478 struct ep93xx_dma_engine *edma = ofdma->of_dma_data; 1479 dma_cap_mask_t mask = edma->dma_dev.cap_mask; 1480 struct ep93xx_dma_chan_cfg dma_cfg; 1481 u8 port = dma_spec->args[0]; 1482 u8 direction = dma_spec->args[1]; 1483 1484 if (!is_slave_direction(direction)) 1485 return NULL; 1486 1487 switch (port) { 1488 case EP93XX_DMA_SSP: 1489 case EP93XX_DMA_IDE: 1490 break; 1491 default: 1492 return NULL; 1493 } 1494 1495 dma_cfg.port = port; 1496 dma_cfg.dir = direction; 1497 1498 return __dma_request_channel(&mask, ep93xx_m2m_dma_filter, &dma_cfg, ofdma->of_node); 1499 } 1500 1501 static int ep93xx_dma_probe(struct platform_device *pdev) 1502 { 1503 struct ep93xx_dma_engine *edma; 1504 struct dma_device *dma_dev; 1505 int ret; 1506 1507 edma = ep93xx_dma_of_probe(pdev); 1508 if (IS_ERR(edma)) 1509 return PTR_ERR(edma); 1510 1511 dma_dev = &edma->dma_dev; 1512 1513 dma_cap_zero(dma_dev->cap_mask); 1514 dma_cap_set(DMA_SLAVE, dma_dev->cap_mask); 1515 dma_cap_set(DMA_CYCLIC, dma_dev->cap_mask); 1516 1517 dma_dev->dev = &pdev->dev; 1518 dma_dev->device_alloc_chan_resources = ep93xx_dma_alloc_chan_resources; 1519 dma_dev->device_free_chan_resources = ep93xx_dma_free_chan_resources; 1520 dma_dev->device_prep_slave_sg = ep93xx_dma_prep_slave_sg; 1521 dma_dev->device_prep_dma_cyclic = ep93xx_dma_prep_dma_cyclic; 1522 dma_dev->device_config = ep93xx_dma_slave_config; 1523 dma_dev->device_synchronize = ep93xx_dma_synchronize; 1524 dma_dev->device_terminate_all = ep93xx_dma_terminate_all; 1525 dma_dev->device_issue_pending = ep93xx_dma_issue_pending; 1526 dma_dev->device_tx_status = ep93xx_dma_tx_status; 1527 1528 dma_set_max_seg_size(dma_dev->dev, DMA_MAX_CHAN_BYTES); 1529 1530 if (edma->m2m) { 1531 dma_cap_set(DMA_MEMCPY, dma_dev->cap_mask); 1532 dma_dev->device_prep_dma_memcpy = ep93xx_dma_prep_dma_memcpy; 1533 1534 edma->hw_setup = m2m_hw_setup; 1535 edma->hw_shutdown = m2m_hw_shutdown; 1536 edma->hw_submit = m2m_hw_submit; 1537 edma->hw_interrupt = m2m_hw_interrupt; 1538 } else { 1539 dma_cap_set(DMA_PRIVATE, dma_dev->cap_mask); 1540 1541 edma->hw_synchronize = m2p_hw_synchronize; 1542 edma->hw_setup = m2p_hw_setup; 1543 edma->hw_shutdown = m2p_hw_shutdown; 1544 edma->hw_submit = m2p_hw_submit; 1545 edma->hw_interrupt = m2p_hw_interrupt; 1546 } 1547 1548 ret = dma_async_device_register(dma_dev); 1549 if (ret) 1550 return ret; 1551 1552 if (edma->m2m) { 1553 ret = of_dma_controller_register(pdev->dev.of_node, ep93xx_m2m_dma_of_xlate, 1554 edma); 1555 } else { 1556 ret = of_dma_controller_register(pdev->dev.of_node, ep93xx_m2p_dma_of_xlate, 1557 edma); 1558 } 1559 if (ret) 1560 goto err_dma_unregister; 1561 1562 dev_info(dma_dev->dev, "EP93xx M2%s DMA ready\n", edma->m2m ? "M" : "P"); 1563 1564 return 0; 1565 1566 err_dma_unregister: 1567 dma_async_device_unregister(dma_dev); 1568 1569 return ret; 1570 } 1571 1572 static const struct ep93xx_edma_data edma_m2p = { 1573 .id = M2P_DMA, 1574 .num_channels = 10, 1575 }; 1576 1577 static const struct ep93xx_edma_data edma_m2m = { 1578 .id = M2M_DMA, 1579 .num_channels = 2, 1580 }; 1581 1582 static const struct of_device_id ep93xx_dma_of_ids[] = { 1583 { .compatible = "cirrus,ep9301-dma-m2p", .data = &edma_m2p }, 1584 { .compatible = "cirrus,ep9301-dma-m2m", .data = &edma_m2m }, 1585 { /* sentinel */ } 1586 }; 1587 MODULE_DEVICE_TABLE(of, ep93xx_dma_of_ids); 1588 1589 static struct platform_driver ep93xx_dma_driver = { 1590 .driver = { 1591 .name = "ep93xx-dma", 1592 .of_match_table = ep93xx_dma_of_ids, 1593 }, 1594 .probe = ep93xx_dma_probe, 1595 }; 1596 1597 module_platform_driver(ep93xx_dma_driver); 1598 1599 MODULE_AUTHOR("Mika Westerberg <mika.westerberg@iki.fi>"); 1600 MODULE_DESCRIPTION("EP93xx DMA driver"); 1601