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