1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Core driver for the Synopsys DesignWare DMA Controller 4 * 5 * Copyright (C) 2007-2008 Atmel Corporation 6 * Copyright (C) 2010-2011 ST Microelectronics 7 * Copyright (C) 2013 Intel Corporation 8 */ 9 10 #include <linux/bitops.h> 11 #include <linux/delay.h> 12 #include <linux/dmaengine.h> 13 #include <linux/dma-mapping.h> 14 #include <linux/dmapool.h> 15 #include <linux/err.h> 16 #include <linux/init.h> 17 #include <linux/interrupt.h> 18 #include <linux/io.h> 19 #include <linux/log2.h> 20 #include <linux/mm.h> 21 #include <linux/module.h> 22 #include <linux/slab.h> 23 #include <linux/pm_runtime.h> 24 25 #include "../dmaengine.h" 26 #include "internal.h" 27 28 /* 29 * This supports the Synopsys "DesignWare AHB Central DMA Controller", 30 * (DW_ahb_dmac) which is used with various AMBA 2.0 systems (not all 31 * of which use ARM any more). See the "Databook" from Synopsys for 32 * information beyond what licensees probably provide. 33 */ 34 35 /* The set of bus widths supported by the DMA controller */ 36 #define DW_DMA_BUSWIDTHS \ 37 BIT(DMA_SLAVE_BUSWIDTH_UNDEFINED) | \ 38 BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) | \ 39 BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) | \ 40 BIT(DMA_SLAVE_BUSWIDTH_4_BYTES) 41 42 /*----------------------------------------------------------------------*/ 43 44 static struct dw_desc *dwc_first_active(struct dw_dma_chan *dwc) 45 { 46 return to_dw_desc(dwc->active_list.next); 47 } 48 49 static dma_cookie_t dwc_tx_submit(struct dma_async_tx_descriptor *tx) 50 { 51 struct dw_desc *desc = txd_to_dw_desc(tx); 52 struct dw_dma_chan *dwc = to_dw_dma_chan(tx->chan); 53 dma_cookie_t cookie; 54 unsigned long flags; 55 56 spin_lock_irqsave(&dwc->lock, flags); 57 cookie = dma_cookie_assign(tx); 58 59 /* 60 * REVISIT: We should attempt to chain as many descriptors as 61 * possible, perhaps even appending to those already submitted 62 * for DMA. But this is hard to do in a race-free manner. 63 */ 64 65 list_add_tail(&desc->desc_node, &dwc->queue); 66 spin_unlock_irqrestore(&dwc->lock, flags); 67 dev_vdbg(dmaengine_chan_dev(tx->chan), "%s: queued %u\n", 68 __func__, desc->txd.cookie); 69 70 return cookie; 71 } 72 73 static struct dw_desc *dwc_desc_get(struct dw_dma_chan *dwc) 74 { 75 struct dw_dma *dw = to_dw_dma(dwc->chan.device); 76 struct dw_desc *desc; 77 dma_addr_t phys; 78 79 desc = dma_pool_zalloc(dw->desc_pool, GFP_ATOMIC, &phys); 80 if (!desc) 81 return NULL; 82 83 dwc->descs_allocated++; 84 INIT_LIST_HEAD(&desc->tx_list); 85 dma_async_tx_descriptor_init(&desc->txd, &dwc->chan); 86 desc->txd.tx_submit = dwc_tx_submit; 87 desc->txd.flags = DMA_CTRL_ACK; 88 desc->txd.phys = phys; 89 return desc; 90 } 91 92 static void dwc_desc_put(struct dw_dma_chan *dwc, struct dw_desc *desc) 93 { 94 struct dw_dma *dw = to_dw_dma(dwc->chan.device); 95 struct dw_desc *child, *_next; 96 97 if (unlikely(!desc)) 98 return; 99 100 list_for_each_entry_safe(child, _next, &desc->tx_list, desc_node) { 101 list_del(&child->desc_node); 102 dma_pool_free(dw->desc_pool, child, child->txd.phys); 103 dwc->descs_allocated--; 104 } 105 106 dma_pool_free(dw->desc_pool, desc, desc->txd.phys); 107 dwc->descs_allocated--; 108 } 109 110 static void dwc_initialize(struct dw_dma_chan *dwc) 111 { 112 struct dw_dma *dw = to_dw_dma(dwc->chan.device); 113 114 dw->initialize_chan(dwc); 115 116 /* Enable interrupts */ 117 channel_set_bit(dw, MASK.XFER, dwc->mask); 118 channel_set_bit(dw, MASK.ERROR, dwc->mask); 119 } 120 121 /*----------------------------------------------------------------------*/ 122 123 static inline void dwc_dump_chan_regs(struct dw_dma_chan *dwc) 124 { 125 dev_err(dmaengine_chan_dev(&dwc->chan), 126 " SAR: 0x%x DAR: 0x%x LLP: 0x%x CTL: 0x%x:%08x\n", 127 channel_readl(dwc, SAR), 128 channel_readl(dwc, DAR), 129 channel_readl(dwc, LLP), 130 channel_readl(dwc, CTL_HI), 131 channel_readl(dwc, CTL_LO)); 132 } 133 134 static inline void dwc_chan_disable(struct dw_dma *dw, struct dw_dma_chan *dwc) 135 { 136 channel_clear_bit(dw, CH_EN, dwc->mask); 137 while (dma_readl(dw, CH_EN) & dwc->mask) 138 cpu_relax(); 139 } 140 141 /*----------------------------------------------------------------------*/ 142 143 /* Perform single block transfer */ 144 static inline void dwc_do_single_block(struct dw_dma_chan *dwc, 145 struct dw_desc *desc) 146 { 147 struct dw_dma *dw = to_dw_dma(dwc->chan.device); 148 u32 ctllo; 149 150 /* 151 * Software emulation of LLP mode relies on interrupts to continue 152 * multi block transfer. 153 */ 154 ctllo = lli_read(desc, ctllo) | DWC_CTLL_INT_EN; 155 156 channel_writel(dwc, SAR, lli_read(desc, sar)); 157 channel_writel(dwc, DAR, lli_read(desc, dar)); 158 channel_writel(dwc, CTL_LO, ctllo); 159 channel_writel(dwc, CTL_HI, lli_read(desc, ctlhi)); 160 channel_set_bit(dw, CH_EN, dwc->mask); 161 162 /* Move pointer to next descriptor */ 163 dwc->tx_node_active = dwc->tx_node_active->next; 164 } 165 166 /* Called with dwc->lock held and bh disabled */ 167 static void dwc_dostart(struct dw_dma_chan *dwc, struct dw_desc *first) 168 { 169 struct dw_dma *dw = to_dw_dma(dwc->chan.device); 170 u8 lms = DWC_LLP_LMS(dwc->dws.m_master); 171 unsigned long was_soft_llp; 172 173 /* ASSERT: channel is idle */ 174 if (dma_readl(dw, CH_EN) & dwc->mask) { 175 dev_err(dmaengine_chan_dev(&dwc->chan), 176 "%s: BUG: Attempted to start non-idle channel\n", 177 __func__); 178 dwc_dump_chan_regs(dwc); 179 180 /* The tasklet will hopefully advance the queue... */ 181 return; 182 } 183 184 if (dwc->nollp) { 185 was_soft_llp = test_and_set_bit(DW_DMA_IS_SOFT_LLP, 186 &dwc->flags); 187 if (was_soft_llp) { 188 dev_err(dmaengine_chan_dev(&dwc->chan), 189 "BUG: Attempted to start new LLP transfer inside ongoing one\n"); 190 return; 191 } 192 193 dwc_initialize(dwc); 194 195 first->residue = first->total_len; 196 dwc->tx_node_active = &first->tx_list; 197 198 /* Submit first block */ 199 dwc_do_single_block(dwc, first); 200 201 return; 202 } 203 204 dwc_initialize(dwc); 205 206 channel_writel(dwc, LLP, first->txd.phys | lms); 207 channel_writel(dwc, CTL_LO, DWC_CTLL_LLP_D_EN | DWC_CTLL_LLP_S_EN); 208 channel_writel(dwc, CTL_HI, 0); 209 channel_set_bit(dw, CH_EN, dwc->mask); 210 } 211 212 static void dwc_dostart_first_queued(struct dw_dma_chan *dwc) 213 { 214 struct dw_desc *desc; 215 216 if (list_empty(&dwc->queue)) 217 return; 218 219 list_move(dwc->queue.next, &dwc->active_list); 220 desc = dwc_first_active(dwc); 221 dev_vdbg(dmaengine_chan_dev(&dwc->chan), "%s: started %u\n", __func__, desc->txd.cookie); 222 dwc_dostart(dwc, desc); 223 } 224 225 /*----------------------------------------------------------------------*/ 226 227 static void 228 dwc_descriptor_complete(struct dw_dma_chan *dwc, struct dw_desc *desc, 229 bool callback_required) 230 { 231 struct dma_async_tx_descriptor *txd = &desc->txd; 232 struct dw_desc *child; 233 unsigned long flags; 234 struct dmaengine_desc_callback cb; 235 236 dev_vdbg(dmaengine_chan_dev(&dwc->chan), "descriptor %u complete\n", txd->cookie); 237 238 spin_lock_irqsave(&dwc->lock, flags); 239 dma_cookie_complete(txd); 240 if (callback_required) 241 dmaengine_desc_get_callback(txd, &cb); 242 else 243 memset(&cb, 0, sizeof(cb)); 244 245 /* async_tx_ack */ 246 list_for_each_entry(child, &desc->tx_list, desc_node) 247 async_tx_ack(&child->txd); 248 async_tx_ack(&desc->txd); 249 dwc_desc_put(dwc, desc); 250 spin_unlock_irqrestore(&dwc->lock, flags); 251 252 dmaengine_desc_callback_invoke(&cb, NULL); 253 } 254 255 static void dwc_complete_all(struct dw_dma *dw, struct dw_dma_chan *dwc) 256 { 257 struct dw_desc *desc, *_desc; 258 LIST_HEAD(list); 259 unsigned long flags; 260 261 spin_lock_irqsave(&dwc->lock, flags); 262 if (dma_readl(dw, CH_EN) & dwc->mask) { 263 dev_err(dmaengine_chan_dev(&dwc->chan), 264 "BUG: XFER bit set, but channel not idle!\n"); 265 266 /* Try to continue after resetting the channel... */ 267 dwc_chan_disable(dw, dwc); 268 } 269 270 /* 271 * Submit queued descriptors ASAP, i.e. before we go through 272 * the completed ones. 273 */ 274 list_splice_init(&dwc->active_list, &list); 275 dwc_dostart_first_queued(dwc); 276 277 spin_unlock_irqrestore(&dwc->lock, flags); 278 279 list_for_each_entry_safe(desc, _desc, &list, desc_node) 280 dwc_descriptor_complete(dwc, desc, true); 281 } 282 283 /* Returns how many bytes were already received from source */ 284 static inline u32 dwc_get_sent(struct dw_dma_chan *dwc) 285 { 286 struct dw_dma *dw = to_dw_dma(dwc->chan.device); 287 u32 ctlhi = channel_readl(dwc, CTL_HI); 288 u32 ctllo = channel_readl(dwc, CTL_LO); 289 290 return dw->block2bytes(dwc, ctlhi, ctllo >> 4 & 7); 291 } 292 293 static void dwc_scan_descriptors(struct dw_dma *dw, struct dw_dma_chan *dwc) 294 { 295 dma_addr_t llp; 296 struct dw_desc *desc, *_desc; 297 struct dw_desc *child; 298 u32 status_xfer; 299 unsigned long flags; 300 301 spin_lock_irqsave(&dwc->lock, flags); 302 llp = channel_readl(dwc, LLP); 303 status_xfer = dma_readl(dw, RAW.XFER); 304 305 if (status_xfer & dwc->mask) { 306 /* Everything we've submitted is done */ 307 dma_writel(dw, CLEAR.XFER, dwc->mask); 308 309 if (test_bit(DW_DMA_IS_SOFT_LLP, &dwc->flags)) { 310 struct list_head *head, *active = dwc->tx_node_active; 311 312 /* 313 * We are inside first active descriptor. 314 * Otherwise something is really wrong. 315 */ 316 desc = dwc_first_active(dwc); 317 318 head = &desc->tx_list; 319 if (active != head) { 320 /* Update residue to reflect last sent descriptor */ 321 if (active == head->next) 322 desc->residue -= desc->len; 323 else 324 desc->residue -= to_dw_desc(active->prev)->len; 325 326 child = to_dw_desc(active); 327 328 /* Submit next block */ 329 dwc_do_single_block(dwc, child); 330 331 spin_unlock_irqrestore(&dwc->lock, flags); 332 return; 333 } 334 335 /* We are done here */ 336 clear_bit(DW_DMA_IS_SOFT_LLP, &dwc->flags); 337 } 338 339 spin_unlock_irqrestore(&dwc->lock, flags); 340 341 dwc_complete_all(dw, dwc); 342 return; 343 } 344 345 if (list_empty(&dwc->active_list)) { 346 spin_unlock_irqrestore(&dwc->lock, flags); 347 return; 348 } 349 350 if (test_bit(DW_DMA_IS_SOFT_LLP, &dwc->flags)) { 351 dev_vdbg(dmaengine_chan_dev(&dwc->chan), "%s: soft LLP mode\n", __func__); 352 spin_unlock_irqrestore(&dwc->lock, flags); 353 return; 354 } 355 356 dev_vdbg(dmaengine_chan_dev(&dwc->chan), "%s: llp=%pad\n", __func__, &llp); 357 358 list_for_each_entry_safe(desc, _desc, &dwc->active_list, desc_node) { 359 /* Initial residue value */ 360 desc->residue = desc->total_len; 361 362 /* Check first descriptors addr */ 363 if (desc->txd.phys == DWC_LLP_LOC(llp)) { 364 spin_unlock_irqrestore(&dwc->lock, flags); 365 return; 366 } 367 368 /* Check first descriptors llp */ 369 if (lli_read(desc, llp) == llp) { 370 /* This one is currently in progress */ 371 desc->residue -= dwc_get_sent(dwc); 372 spin_unlock_irqrestore(&dwc->lock, flags); 373 return; 374 } 375 376 desc->residue -= desc->len; 377 list_for_each_entry(child, &desc->tx_list, desc_node) { 378 if (lli_read(child, llp) == llp) { 379 /* Currently in progress */ 380 desc->residue -= dwc_get_sent(dwc); 381 spin_unlock_irqrestore(&dwc->lock, flags); 382 return; 383 } 384 desc->residue -= child->len; 385 } 386 387 /* 388 * No descriptors so far seem to be in progress, i.e. 389 * this one must be done. 390 */ 391 spin_unlock_irqrestore(&dwc->lock, flags); 392 dwc_descriptor_complete(dwc, desc, true); 393 spin_lock_irqsave(&dwc->lock, flags); 394 } 395 396 dev_err(dmaengine_chan_dev(&dwc->chan), 397 "BUG: All descriptors done, but channel not idle!\n"); 398 399 /* Try to continue after resetting the channel... */ 400 dwc_chan_disable(dw, dwc); 401 402 dwc_dostart_first_queued(dwc); 403 spin_unlock_irqrestore(&dwc->lock, flags); 404 } 405 406 static inline void dwc_dump_lli(struct dw_dma_chan *dwc, struct dw_desc *desc) 407 { 408 dev_crit(dmaengine_chan_dev(&dwc->chan), " desc: s0x%x d0x%x l0x%x c0x%x:%x\n", 409 lli_read(desc, sar), 410 lli_read(desc, dar), 411 lli_read(desc, llp), 412 lli_read(desc, ctlhi), 413 lli_read(desc, ctllo)); 414 } 415 416 static void dwc_handle_error(struct dw_dma *dw, struct dw_dma_chan *dwc) 417 { 418 struct dw_desc *bad_desc; 419 struct dw_desc *child; 420 unsigned long flags; 421 422 dwc_scan_descriptors(dw, dwc); 423 424 spin_lock_irqsave(&dwc->lock, flags); 425 426 /* 427 * The descriptor currently at the head of the active list is 428 * borked. Since we don't have any way to report errors, we'll 429 * just have to scream loudly and try to carry on. 430 */ 431 bad_desc = dwc_first_active(dwc); 432 list_del_init(&bad_desc->desc_node); 433 list_move(dwc->queue.next, dwc->active_list.prev); 434 435 /* Clear the error flag and try to restart the controller */ 436 dma_writel(dw, CLEAR.ERROR, dwc->mask); 437 if (!list_empty(&dwc->active_list)) 438 dwc_dostart(dwc, dwc_first_active(dwc)); 439 440 /* 441 * WARN may seem harsh, but since this only happens 442 * when someone submits a bad physical address in a 443 * descriptor, we should consider ourselves lucky that the 444 * controller flagged an error instead of scribbling over 445 * random memory locations. 446 */ 447 dev_WARN(dmaengine_chan_dev(&dwc->chan), "Bad descriptor submitted for DMA!\n" 448 " cookie: %d\n", bad_desc->txd.cookie); 449 dwc_dump_lli(dwc, bad_desc); 450 list_for_each_entry(child, &bad_desc->tx_list, desc_node) 451 dwc_dump_lli(dwc, child); 452 453 spin_unlock_irqrestore(&dwc->lock, flags); 454 455 /* Pretend the descriptor completed successfully */ 456 dwc_descriptor_complete(dwc, bad_desc, true); 457 } 458 459 static void dw_dma_tasklet(struct tasklet_struct *t) 460 { 461 struct dw_dma *dw = from_tasklet(dw, t, tasklet); 462 struct dw_dma_chan *dwc; 463 u32 status_xfer; 464 u32 status_err; 465 unsigned int i; 466 467 status_xfer = dma_readl(dw, RAW.XFER); 468 status_err = dma_readl(dw, RAW.ERROR); 469 470 dev_vdbg(dw->dma.dev, "%s: status_err=%x\n", __func__, status_err); 471 472 for (i = 0; i < dw->dma.chancnt; i++) { 473 dwc = &dw->chan[i]; 474 if (test_bit(DW_DMA_IS_CYCLIC, &dwc->flags)) 475 dev_vdbg(dw->dma.dev, "Cyclic xfer is not implemented\n"); 476 else if (status_err & (1 << i)) 477 dwc_handle_error(dw, dwc); 478 else if (status_xfer & (1 << i)) 479 dwc_scan_descriptors(dw, dwc); 480 } 481 482 /* Re-enable interrupts */ 483 channel_set_bit(dw, MASK.XFER, dw->all_chan_mask); 484 channel_set_bit(dw, MASK.ERROR, dw->all_chan_mask); 485 } 486 487 static irqreturn_t dw_dma_interrupt(int irq, void *dev_id) 488 { 489 struct dw_dma *dw = dev_id; 490 u32 status; 491 492 /* Check if we have any interrupt from the DMAC which is not in use */ 493 if (!dw->in_use) 494 return IRQ_NONE; 495 496 status = dma_readl(dw, STATUS_INT); 497 dev_vdbg(dw->dma.dev, "%s: status=0x%x\n", __func__, status); 498 499 /* Check if we have any interrupt from the DMAC */ 500 if (!status) 501 return IRQ_NONE; 502 503 /* 504 * Just disable the interrupts. We'll turn them back on in the 505 * softirq handler. 506 */ 507 channel_clear_bit(dw, MASK.XFER, dw->all_chan_mask); 508 channel_clear_bit(dw, MASK.BLOCK, dw->all_chan_mask); 509 channel_clear_bit(dw, MASK.ERROR, dw->all_chan_mask); 510 511 status = dma_readl(dw, STATUS_INT); 512 if (status) { 513 dev_err(dw->dma.dev, 514 "BUG: Unexpected interrupts pending: 0x%x\n", 515 status); 516 517 /* Try to recover */ 518 channel_clear_bit(dw, MASK.XFER, (1 << 8) - 1); 519 channel_clear_bit(dw, MASK.BLOCK, (1 << 8) - 1); 520 channel_clear_bit(dw, MASK.SRC_TRAN, (1 << 8) - 1); 521 channel_clear_bit(dw, MASK.DST_TRAN, (1 << 8) - 1); 522 channel_clear_bit(dw, MASK.ERROR, (1 << 8) - 1); 523 } 524 525 tasklet_schedule(&dw->tasklet); 526 527 return IRQ_HANDLED; 528 } 529 530 /*----------------------------------------------------------------------*/ 531 532 static struct dma_async_tx_descriptor * 533 dwc_prep_dma_memcpy(struct dma_chan *chan, dma_addr_t dest, dma_addr_t src, 534 size_t len, unsigned long flags) 535 { 536 struct dw_dma_chan *dwc = to_dw_dma_chan(chan); 537 struct dw_dma *dw = to_dw_dma(chan->device); 538 struct dw_desc *desc; 539 struct dw_desc *first; 540 struct dw_desc *prev; 541 size_t xfer_count; 542 size_t offset; 543 u8 m_master = dwc->dws.m_master; 544 unsigned int src_width; 545 unsigned int dst_width; 546 unsigned int data_width = dw->pdata->data_width[m_master]; 547 u32 ctllo, ctlhi; 548 u8 lms = DWC_LLP_LMS(m_master); 549 550 dev_vdbg(dmaengine_chan_dev(chan), 551 "%s: d%pad s%pad l0x%zx f0x%lx\n", __func__, 552 &dest, &src, len, flags); 553 554 if (unlikely(!len)) { 555 dev_dbg(dmaengine_chan_dev(chan), "%s: length is zero!\n", __func__); 556 return NULL; 557 } 558 559 dwc->direction = DMA_MEM_TO_MEM; 560 561 src_width = dst_width = __ffs(data_width | src | dest | len); 562 563 ctllo = dw->prepare_ctllo(dwc) 564 | DWC_CTLL_DST_WIDTH(dst_width) 565 | DWC_CTLL_SRC_WIDTH(src_width) 566 | DWC_CTLL_DST_INC 567 | DWC_CTLL_SRC_INC 568 | DWC_CTLL_FC_M2M; 569 prev = first = NULL; 570 571 for (offset = 0; offset < len; offset += xfer_count) { 572 desc = dwc_desc_get(dwc); 573 if (!desc) 574 goto err_desc_get; 575 576 ctlhi = dw->bytes2block(dwc, len - offset, src_width, &xfer_count); 577 578 lli_write(desc, sar, src + offset); 579 lli_write(desc, dar, dest + offset); 580 lli_write(desc, ctllo, ctllo); 581 lli_write(desc, ctlhi, ctlhi); 582 desc->len = xfer_count; 583 584 if (!first) { 585 first = desc; 586 } else { 587 lli_write(prev, llp, desc->txd.phys | lms); 588 list_add_tail(&desc->desc_node, &first->tx_list); 589 } 590 prev = desc; 591 } 592 593 if (flags & DMA_PREP_INTERRUPT) 594 /* Trigger interrupt after last block */ 595 lli_set(prev, ctllo, DWC_CTLL_INT_EN); 596 597 prev->lli.llp = 0; 598 lli_clear(prev, ctllo, DWC_CTLL_LLP_D_EN | DWC_CTLL_LLP_S_EN); 599 first->txd.flags = flags; 600 first->total_len = len; 601 602 return &first->txd; 603 604 err_desc_get: 605 dwc_desc_put(dwc, first); 606 return NULL; 607 } 608 609 static struct dma_async_tx_descriptor * 610 dwc_prep_slave_sg(struct dma_chan *chan, struct scatterlist *sgl, 611 unsigned int sg_len, enum dma_transfer_direction direction, 612 unsigned long flags, void *context) 613 { 614 struct dw_dma_chan *dwc = to_dw_dma_chan(chan); 615 struct dw_dma *dw = to_dw_dma(chan->device); 616 struct dma_slave_config *sconfig = &dwc->dma_sconfig; 617 struct dw_desc *prev; 618 struct dw_desc *first; 619 u32 ctllo, ctlhi; 620 u8 lms = DWC_LLP_LMS(dwc->dws.m_master); 621 dma_addr_t reg; 622 unsigned int reg_width; 623 unsigned int mem_width; 624 unsigned int i; 625 struct scatterlist *sg; 626 size_t total_len = 0; 627 628 dev_vdbg(dmaengine_chan_dev(chan), "%s\n", __func__); 629 630 if (unlikely(!is_slave_direction(direction) || !sg_len)) 631 return NULL; 632 633 dwc->direction = direction; 634 635 prev = first = NULL; 636 637 switch (direction) { 638 case DMA_MEM_TO_DEV: 639 reg_width = __ffs(sconfig->dst_addr_width); 640 reg = sconfig->dst_addr; 641 ctllo = dw->prepare_ctllo(dwc) 642 | DWC_CTLL_DST_WIDTH(reg_width) 643 | DWC_CTLL_DST_FIX 644 | DWC_CTLL_SRC_INC; 645 646 ctllo |= sconfig->device_fc ? DWC_CTLL_FC(DW_DMA_FC_P_M2P) : 647 DWC_CTLL_FC(DW_DMA_FC_D_M2P); 648 649 for_each_sg(sgl, sg, sg_len, i) { 650 struct dw_desc *desc; 651 u32 len, mem; 652 size_t dlen; 653 654 mem = sg_dma_address(sg); 655 len = sg_dma_len(sg); 656 657 mem_width = __ffs(sconfig->src_addr_width | mem | len); 658 659 slave_sg_todev_fill_desc: 660 desc = dwc_desc_get(dwc); 661 if (!desc) 662 goto err_desc_get; 663 664 ctlhi = dw->bytes2block(dwc, len, mem_width, &dlen); 665 666 lli_write(desc, sar, mem); 667 lli_write(desc, dar, reg); 668 lli_write(desc, ctlhi, ctlhi); 669 lli_write(desc, ctllo, ctllo | DWC_CTLL_SRC_WIDTH(mem_width)); 670 desc->len = dlen; 671 672 if (!first) { 673 first = desc; 674 } else { 675 lli_write(prev, llp, desc->txd.phys | lms); 676 list_add_tail(&desc->desc_node, &first->tx_list); 677 } 678 prev = desc; 679 680 mem += dlen; 681 len -= dlen; 682 total_len += dlen; 683 684 if (len) 685 goto slave_sg_todev_fill_desc; 686 } 687 break; 688 case DMA_DEV_TO_MEM: 689 reg_width = __ffs(sconfig->src_addr_width); 690 reg = sconfig->src_addr; 691 ctllo = dw->prepare_ctllo(dwc) 692 | DWC_CTLL_SRC_WIDTH(reg_width) 693 | DWC_CTLL_DST_INC 694 | DWC_CTLL_SRC_FIX; 695 696 ctllo |= sconfig->device_fc ? DWC_CTLL_FC(DW_DMA_FC_P_P2M) : 697 DWC_CTLL_FC(DW_DMA_FC_D_P2M); 698 699 for_each_sg(sgl, sg, sg_len, i) { 700 struct dw_desc *desc; 701 u32 len, mem; 702 size_t dlen; 703 704 mem = sg_dma_address(sg); 705 len = sg_dma_len(sg); 706 707 slave_sg_fromdev_fill_desc: 708 desc = dwc_desc_get(dwc); 709 if (!desc) 710 goto err_desc_get; 711 712 ctlhi = dw->bytes2block(dwc, len, reg_width, &dlen); 713 714 lli_write(desc, sar, reg); 715 lli_write(desc, dar, mem); 716 lli_write(desc, ctlhi, ctlhi); 717 mem_width = __ffs(sconfig->dst_addr_width | mem); 718 lli_write(desc, ctllo, ctllo | DWC_CTLL_DST_WIDTH(mem_width)); 719 desc->len = dlen; 720 721 if (!first) { 722 first = desc; 723 } else { 724 lli_write(prev, llp, desc->txd.phys | lms); 725 list_add_tail(&desc->desc_node, &first->tx_list); 726 } 727 prev = desc; 728 729 mem += dlen; 730 len -= dlen; 731 total_len += dlen; 732 733 if (len) 734 goto slave_sg_fromdev_fill_desc; 735 } 736 break; 737 default: 738 return NULL; 739 } 740 741 if (flags & DMA_PREP_INTERRUPT) 742 /* Trigger interrupt after last block */ 743 lli_set(prev, ctllo, DWC_CTLL_INT_EN); 744 745 prev->lli.llp = 0; 746 lli_clear(prev, ctllo, DWC_CTLL_LLP_D_EN | DWC_CTLL_LLP_S_EN); 747 first->total_len = total_len; 748 749 return &first->txd; 750 751 err_desc_get: 752 dev_err(dmaengine_chan_dev(chan), 753 "not enough descriptors available. Direction %d\n", direction); 754 dwc_desc_put(dwc, first); 755 return NULL; 756 } 757 758 bool dw_dma_filter(struct dma_chan *chan, void *param) 759 { 760 struct dw_dma_chan *dwc = to_dw_dma_chan(chan); 761 struct dw_dma_slave *dws = param; 762 763 if (dws->dma_dev != chan->device->dev) 764 return false; 765 766 /* permit channels in accordance with the channels mask */ 767 if (dws->channels && !(dws->channels & dwc->mask)) 768 return false; 769 770 /* We have to copy data since dws can be temporary storage */ 771 memcpy(&dwc->dws, dws, sizeof(struct dw_dma_slave)); 772 773 return true; 774 } 775 EXPORT_SYMBOL_GPL(dw_dma_filter); 776 777 static int dwc_verify_maxburst(struct dma_chan *chan) 778 { 779 struct dw_dma_chan *dwc = to_dw_dma_chan(chan); 780 781 dwc->dma_sconfig.src_maxburst = 782 clamp(dwc->dma_sconfig.src_maxburst, 1U, dwc->max_burst); 783 dwc->dma_sconfig.dst_maxburst = 784 clamp(dwc->dma_sconfig.dst_maxburst, 1U, dwc->max_burst); 785 786 dwc->dma_sconfig.src_maxburst = 787 rounddown_pow_of_two(dwc->dma_sconfig.src_maxburst); 788 dwc->dma_sconfig.dst_maxburst = 789 rounddown_pow_of_two(dwc->dma_sconfig.dst_maxburst); 790 791 return 0; 792 } 793 794 static int dwc_verify_p_buswidth(struct dma_chan *chan) 795 { 796 struct dw_dma_chan *dwc = to_dw_dma_chan(chan); 797 struct dw_dma *dw = to_dw_dma(chan->device); 798 u32 reg_width, max_width; 799 800 if (dwc->dma_sconfig.direction == DMA_MEM_TO_DEV) 801 reg_width = dwc->dma_sconfig.dst_addr_width; 802 else if (dwc->dma_sconfig.direction == DMA_DEV_TO_MEM) 803 reg_width = dwc->dma_sconfig.src_addr_width; 804 else /* DMA_MEM_TO_MEM */ 805 return 0; 806 807 max_width = dw->pdata->data_width[dwc->dws.p_master]; 808 809 /* Fall-back to 1-byte transfer width if undefined */ 810 if (reg_width == DMA_SLAVE_BUSWIDTH_UNDEFINED) 811 reg_width = DMA_SLAVE_BUSWIDTH_1_BYTE; 812 else if (!is_power_of_2(reg_width) || reg_width > max_width) 813 return -EINVAL; 814 else /* bus width is valid */ 815 return 0; 816 817 /* Update undefined addr width value */ 818 if (dwc->dma_sconfig.direction == DMA_MEM_TO_DEV) 819 dwc->dma_sconfig.dst_addr_width = reg_width; 820 else /* DMA_DEV_TO_MEM */ 821 dwc->dma_sconfig.src_addr_width = reg_width; 822 823 return 0; 824 } 825 826 static int dwc_verify_m_buswidth(struct dma_chan *chan) 827 { 828 struct dw_dma_chan *dwc = to_dw_dma_chan(chan); 829 struct dw_dma *dw = to_dw_dma(chan->device); 830 u32 reg_width, reg_burst, mem_width; 831 832 mem_width = dw->pdata->data_width[dwc->dws.m_master]; 833 834 /* 835 * It's possible to have a data portion locked in the DMA FIFO in case 836 * of the channel suspension. Subsequent channel disabling will cause 837 * that data silent loss. In order to prevent that maintain the src and 838 * dst transfer widths coherency by means of the relation: 839 * (CTLx.SRC_TR_WIDTH * CTLx.SRC_MSIZE >= CTLx.DST_TR_WIDTH) 840 * Look for the details in the commit message that brings this change. 841 * 842 * Note the DMA configs utilized in the calculations below must have 843 * been verified to have correct values by this method call. 844 */ 845 if (dwc->dma_sconfig.direction == DMA_MEM_TO_DEV) { 846 reg_width = dwc->dma_sconfig.dst_addr_width; 847 if (mem_width < reg_width) 848 return -EINVAL; 849 850 dwc->dma_sconfig.src_addr_width = mem_width; 851 } else if (dwc->dma_sconfig.direction == DMA_DEV_TO_MEM) { 852 reg_width = dwc->dma_sconfig.src_addr_width; 853 reg_burst = dwc->dma_sconfig.src_maxburst; 854 855 dwc->dma_sconfig.dst_addr_width = min(mem_width, reg_width * reg_burst); 856 } 857 858 return 0; 859 } 860 861 static int dwc_config(struct dma_chan *chan, struct dma_slave_config *sconfig) 862 { 863 struct dw_dma_chan *dwc = to_dw_dma_chan(chan); 864 int ret; 865 866 memcpy(&dwc->dma_sconfig, sconfig, sizeof(*sconfig)); 867 868 ret = dwc_verify_maxburst(chan); 869 if (ret) 870 return ret; 871 872 ret = dwc_verify_p_buswidth(chan); 873 if (ret) 874 return ret; 875 876 ret = dwc_verify_m_buswidth(chan); 877 if (ret) 878 return ret; 879 880 return 0; 881 } 882 883 static void dwc_chan_pause(struct dw_dma_chan *dwc, bool drain) 884 { 885 struct dw_dma *dw = to_dw_dma(dwc->chan.device); 886 unsigned int count = 20; /* timeout iterations */ 887 888 dw->suspend_chan(dwc, drain); 889 890 while (!(channel_readl(dwc, CFG_LO) & DWC_CFGL_FIFO_EMPTY) && count--) 891 udelay(2); 892 893 set_bit(DW_DMA_IS_PAUSED, &dwc->flags); 894 } 895 896 static int dwc_pause(struct dma_chan *chan) 897 { 898 struct dw_dma_chan *dwc = to_dw_dma_chan(chan); 899 unsigned long flags; 900 901 spin_lock_irqsave(&dwc->lock, flags); 902 dwc_chan_pause(dwc, false); 903 spin_unlock_irqrestore(&dwc->lock, flags); 904 905 return 0; 906 } 907 908 static inline void dwc_chan_resume(struct dw_dma_chan *dwc, bool drain) 909 { 910 struct dw_dma *dw = to_dw_dma(dwc->chan.device); 911 912 dw->resume_chan(dwc, drain); 913 914 clear_bit(DW_DMA_IS_PAUSED, &dwc->flags); 915 } 916 917 static int dwc_resume(struct dma_chan *chan) 918 { 919 struct dw_dma_chan *dwc = to_dw_dma_chan(chan); 920 unsigned long flags; 921 922 spin_lock_irqsave(&dwc->lock, flags); 923 924 if (test_bit(DW_DMA_IS_PAUSED, &dwc->flags)) 925 dwc_chan_resume(dwc, false); 926 927 spin_unlock_irqrestore(&dwc->lock, flags); 928 929 return 0; 930 } 931 932 static int dwc_terminate_all(struct dma_chan *chan) 933 { 934 struct dw_dma_chan *dwc = to_dw_dma_chan(chan); 935 struct dw_dma *dw = to_dw_dma(chan->device); 936 struct dw_desc *desc, *_desc; 937 unsigned long flags; 938 LIST_HEAD(list); 939 940 spin_lock_irqsave(&dwc->lock, flags); 941 942 clear_bit(DW_DMA_IS_SOFT_LLP, &dwc->flags); 943 944 dwc_chan_pause(dwc, true); 945 946 dwc_chan_disable(dw, dwc); 947 948 dwc_chan_resume(dwc, true); 949 950 /* active_list entries will end up before queued entries */ 951 list_splice_init(&dwc->queue, &list); 952 list_splice_init(&dwc->active_list, &list); 953 954 spin_unlock_irqrestore(&dwc->lock, flags); 955 956 /* Flush all pending and queued descriptors */ 957 list_for_each_entry_safe(desc, _desc, &list, desc_node) 958 dwc_descriptor_complete(dwc, desc, false); 959 960 return 0; 961 } 962 963 static struct dw_desc *dwc_find_desc(struct dw_dma_chan *dwc, dma_cookie_t c) 964 { 965 struct dw_desc *desc; 966 967 list_for_each_entry(desc, &dwc->active_list, desc_node) 968 if (desc->txd.cookie == c) 969 return desc; 970 971 return NULL; 972 } 973 974 static u32 dwc_get_residue_and_status(struct dw_dma_chan *dwc, dma_cookie_t cookie, 975 enum dma_status *status) 976 { 977 struct dw_desc *desc; 978 unsigned long flags; 979 u32 residue; 980 981 spin_lock_irqsave(&dwc->lock, flags); 982 983 desc = dwc_find_desc(dwc, cookie); 984 if (desc) { 985 if (desc == dwc_first_active(dwc)) { 986 residue = desc->residue; 987 if (test_bit(DW_DMA_IS_SOFT_LLP, &dwc->flags) && residue) 988 residue -= dwc_get_sent(dwc); 989 if (test_bit(DW_DMA_IS_PAUSED, &dwc->flags)) 990 *status = DMA_PAUSED; 991 } else { 992 residue = desc->total_len; 993 } 994 } else { 995 residue = 0; 996 } 997 998 spin_unlock_irqrestore(&dwc->lock, flags); 999 return residue; 1000 } 1001 1002 static enum dma_status 1003 dwc_tx_status(struct dma_chan *chan, 1004 dma_cookie_t cookie, 1005 struct dma_tx_state *txstate) 1006 { 1007 struct dw_dma_chan *dwc = to_dw_dma_chan(chan); 1008 enum dma_status ret; 1009 1010 ret = dma_cookie_status(chan, cookie, txstate); 1011 if (ret == DMA_COMPLETE) 1012 return ret; 1013 1014 dwc_scan_descriptors(to_dw_dma(chan->device), dwc); 1015 1016 ret = dma_cookie_status(chan, cookie, txstate); 1017 if (ret == DMA_COMPLETE) 1018 return ret; 1019 1020 dma_set_residue(txstate, dwc_get_residue_and_status(dwc, cookie, &ret)); 1021 return ret; 1022 } 1023 1024 static void dwc_issue_pending(struct dma_chan *chan) 1025 { 1026 struct dw_dma_chan *dwc = to_dw_dma_chan(chan); 1027 unsigned long flags; 1028 1029 spin_lock_irqsave(&dwc->lock, flags); 1030 if (list_empty(&dwc->active_list)) 1031 dwc_dostart_first_queued(dwc); 1032 spin_unlock_irqrestore(&dwc->lock, flags); 1033 } 1034 1035 /*----------------------------------------------------------------------*/ 1036 1037 void do_dw_dma_off(struct dw_dma *dw) 1038 { 1039 dma_writel(dw, CFG, 0); 1040 1041 channel_clear_bit(dw, MASK.XFER, dw->all_chan_mask); 1042 channel_clear_bit(dw, MASK.BLOCK, dw->all_chan_mask); 1043 channel_clear_bit(dw, MASK.SRC_TRAN, dw->all_chan_mask); 1044 channel_clear_bit(dw, MASK.DST_TRAN, dw->all_chan_mask); 1045 channel_clear_bit(dw, MASK.ERROR, dw->all_chan_mask); 1046 1047 while (dma_readl(dw, CFG) & DW_CFG_DMA_EN) 1048 cpu_relax(); 1049 } 1050 1051 void do_dw_dma_on(struct dw_dma *dw) 1052 { 1053 dma_writel(dw, CFG, DW_CFG_DMA_EN); 1054 } 1055 1056 static int dwc_alloc_chan_resources(struct dma_chan *chan) 1057 { 1058 struct dw_dma_chan *dwc = to_dw_dma_chan(chan); 1059 struct dw_dma *dw = to_dw_dma(chan->device); 1060 1061 dev_vdbg(dmaengine_chan_dev(chan), "%s\n", __func__); 1062 1063 /* ASSERT: channel is idle */ 1064 if (dma_readl(dw, CH_EN) & dwc->mask) { 1065 dev_dbg(dmaengine_chan_dev(chan), "DMA channel not idle?\n"); 1066 return -EIO; 1067 } 1068 1069 dma_cookie_init(chan); 1070 1071 /* 1072 * NOTE: some controllers may have additional features that we 1073 * need to initialize here, like "scatter-gather" (which 1074 * doesn't mean what you think it means), and status writeback. 1075 */ 1076 1077 /* 1078 * We need controller-specific data to set up slave transfers. 1079 */ 1080 if (chan->private && !dw_dma_filter(chan, chan->private)) { 1081 dev_warn(dmaengine_chan_dev(chan), "Wrong controller-specific data\n"); 1082 return -EINVAL; 1083 } 1084 1085 /* Enable controller here if needed */ 1086 if (!dw->in_use) 1087 do_dw_dma_on(dw); 1088 dw->in_use |= dwc->mask; 1089 1090 return 0; 1091 } 1092 1093 static void dwc_free_chan_resources(struct dma_chan *chan) 1094 { 1095 struct dw_dma_chan *dwc = to_dw_dma_chan(chan); 1096 struct dw_dma *dw = to_dw_dma(chan->device); 1097 unsigned long flags; 1098 1099 dev_dbg(dmaengine_chan_dev(chan), "%s: descs allocated=%u\n", __func__, 1100 dwc->descs_allocated); 1101 1102 /* ASSERT: channel is idle */ 1103 BUG_ON(!list_empty(&dwc->active_list)); 1104 BUG_ON(!list_empty(&dwc->queue)); 1105 BUG_ON(dma_readl(to_dw_dma(chan->device), CH_EN) & dwc->mask); 1106 1107 spin_lock_irqsave(&dwc->lock, flags); 1108 1109 /* Clear custom channel configuration */ 1110 memset(&dwc->dws, 0, sizeof(struct dw_dma_slave)); 1111 1112 /* Disable interrupts */ 1113 channel_clear_bit(dw, MASK.XFER, dwc->mask); 1114 channel_clear_bit(dw, MASK.BLOCK, dwc->mask); 1115 channel_clear_bit(dw, MASK.ERROR, dwc->mask); 1116 1117 spin_unlock_irqrestore(&dwc->lock, flags); 1118 1119 /* Disable controller in case it was a last user */ 1120 dw->in_use &= ~dwc->mask; 1121 if (!dw->in_use) 1122 do_dw_dma_off(dw); 1123 1124 dev_vdbg(dmaengine_chan_dev(chan), "%s: done\n", __func__); 1125 } 1126 1127 static void dwc_caps(struct dma_chan *chan, struct dma_slave_caps *caps) 1128 { 1129 struct dw_dma_chan *dwc = to_dw_dma_chan(chan); 1130 1131 caps->max_burst = dwc->max_burst; 1132 1133 /* 1134 * It might be crucial for some devices to have the hardware 1135 * accelerated multi-block transfers supported, aka LLPs in DW DMAC 1136 * notation. So if LLPs are supported then max_sg_burst is set to 1137 * zero which means unlimited number of SG entries can be handled in a 1138 * single DMA transaction, otherwise it's just one SG entry. 1139 */ 1140 if (dwc->nollp) 1141 caps->max_sg_burst = 1; 1142 else 1143 caps->max_sg_burst = 0; 1144 } 1145 1146 int do_dma_probe(struct dw_dma_chip *chip) 1147 { 1148 struct dw_dma *dw = chip->dw; 1149 struct dw_dma_platform_data *pdata; 1150 bool autocfg = false; 1151 unsigned int dw_params; 1152 unsigned int i; 1153 int ret; 1154 1155 dw->pdata = devm_kzalloc(chip->dev, sizeof(*dw->pdata), GFP_KERNEL); 1156 if (!dw->pdata) 1157 return -ENOMEM; 1158 1159 dw->regs = chip->regs; 1160 1161 pm_runtime_get_sync(chip->dev); 1162 1163 if (!chip->pdata) { 1164 dw_params = dma_readl(dw, DW_PARAMS); 1165 dev_dbg(chip->dev, "DW_PARAMS: 0x%08x\n", dw_params); 1166 1167 autocfg = dw_params >> DW_PARAMS_EN & 1; 1168 if (!autocfg) { 1169 ret = -EINVAL; 1170 goto err_pdata; 1171 } 1172 1173 /* Reassign the platform data pointer */ 1174 pdata = dw->pdata; 1175 1176 /* Get hardware configuration parameters */ 1177 pdata->nr_channels = (dw_params >> DW_PARAMS_NR_CHAN & 7) + 1; 1178 pdata->nr_masters = (dw_params >> DW_PARAMS_NR_MASTER & 3) + 1; 1179 for (i = 0; i < pdata->nr_masters; i++) { 1180 pdata->data_width[i] = 1181 4 << (dw_params >> DW_PARAMS_DATA_WIDTH(i) & 3); 1182 } 1183 pdata->block_size = dma_readl(dw, MAX_BLK_SIZE); 1184 1185 /* Fill platform data with the default values */ 1186 pdata->chan_allocation_order = CHAN_ALLOCATION_ASCENDING; 1187 pdata->chan_priority = CHAN_PRIORITY_ASCENDING; 1188 } else if (chip->pdata->nr_channels > DW_DMA_MAX_NR_CHANNELS) { 1189 ret = -EINVAL; 1190 goto err_pdata; 1191 } else { 1192 memcpy(dw->pdata, chip->pdata, sizeof(*dw->pdata)); 1193 1194 /* Reassign the platform data pointer */ 1195 pdata = dw->pdata; 1196 } 1197 1198 dw->chan = devm_kcalloc(chip->dev, pdata->nr_channels, sizeof(*dw->chan), 1199 GFP_KERNEL); 1200 if (!dw->chan) { 1201 ret = -ENOMEM; 1202 goto err_pdata; 1203 } 1204 1205 /* Calculate all channel mask before DMA setup */ 1206 dw->all_chan_mask = (1 << pdata->nr_channels) - 1; 1207 1208 /* Force dma off, just in case */ 1209 dw->disable(dw); 1210 1211 /* Device and instance ID for IRQ and DMA pool */ 1212 dw->set_device_name(dw, chip->id); 1213 1214 /* Create a pool of consistent memory blocks for hardware descriptors */ 1215 dw->desc_pool = dmam_pool_create(dw->name, chip->dev, 1216 sizeof(struct dw_desc), 4, 0); 1217 if (!dw->desc_pool) { 1218 dev_err(chip->dev, "No memory for descriptors dma pool\n"); 1219 ret = -ENOMEM; 1220 goto err_pdata; 1221 } 1222 1223 tasklet_setup(&dw->tasklet, dw_dma_tasklet); 1224 1225 ret = request_irq(chip->irq, dw_dma_interrupt, IRQF_SHARED, 1226 dw->name, dw); 1227 if (ret) 1228 goto err_pdata; 1229 1230 INIT_LIST_HEAD(&dw->dma.channels); 1231 for (i = 0; i < pdata->nr_channels; i++) { 1232 struct dw_dma_chan *dwc = &dw->chan[i]; 1233 1234 dwc->chan.device = &dw->dma; 1235 dma_cookie_init(&dwc->chan); 1236 if (pdata->chan_allocation_order == CHAN_ALLOCATION_ASCENDING) 1237 list_add_tail(&dwc->chan.device_node, 1238 &dw->dma.channels); 1239 else 1240 list_add(&dwc->chan.device_node, &dw->dma.channels); 1241 1242 /* 7 is highest priority & 0 is lowest. */ 1243 if (pdata->chan_priority == CHAN_PRIORITY_ASCENDING) 1244 dwc->priority = pdata->nr_channels - i - 1; 1245 else 1246 dwc->priority = i; 1247 1248 dwc->ch_regs = &__dw_regs(dw)->CHAN[i]; 1249 spin_lock_init(&dwc->lock); 1250 dwc->mask = 1 << i; 1251 1252 INIT_LIST_HEAD(&dwc->active_list); 1253 INIT_LIST_HEAD(&dwc->queue); 1254 1255 channel_clear_bit(dw, CH_EN, dwc->mask); 1256 1257 dwc->direction = DMA_TRANS_NONE; 1258 1259 /* Hardware configuration */ 1260 if (autocfg) { 1261 unsigned int r = DW_DMA_MAX_NR_CHANNELS - i - 1; 1262 void __iomem *addr = &__dw_regs(dw)->DWC_PARAMS[r]; 1263 unsigned int dwc_params = readl(addr); 1264 1265 dev_dbg(chip->dev, "DWC_PARAMS[%d]: 0x%08x\n", i, 1266 dwc_params); 1267 1268 /* 1269 * Decode maximum block size for given channel. The 1270 * stored 4 bit value represents blocks from 0x00 for 3 1271 * up to 0x0a for 4095. 1272 */ 1273 dwc->block_size = 1274 (4 << ((pdata->block_size >> 4 * i) & 0xf)) - 1; 1275 1276 /* 1277 * According to the DW DMA databook the true scatter- 1278 * gether LLPs aren't available if either multi-block 1279 * config is disabled (CHx_MULTI_BLK_EN == 0) or the 1280 * LLP register is hard-coded to zeros 1281 * (CHx_HC_LLP == 1). 1282 */ 1283 dwc->nollp = 1284 (dwc_params >> DWC_PARAMS_MBLK_EN & 0x1) == 0 || 1285 (dwc_params >> DWC_PARAMS_HC_LLP & 0x1) == 1; 1286 dwc->max_burst = 1287 (0x4 << (dwc_params >> DWC_PARAMS_MSIZE & 0x7)); 1288 } else { 1289 dwc->block_size = pdata->block_size; 1290 dwc->nollp = !pdata->multi_block[i]; 1291 dwc->max_burst = pdata->max_burst[i] ?: DW_DMA_MAX_BURST; 1292 } 1293 } 1294 1295 /* Clear all interrupts on all channels. */ 1296 dma_writel(dw, CLEAR.XFER, dw->all_chan_mask); 1297 dma_writel(dw, CLEAR.BLOCK, dw->all_chan_mask); 1298 dma_writel(dw, CLEAR.SRC_TRAN, dw->all_chan_mask); 1299 dma_writel(dw, CLEAR.DST_TRAN, dw->all_chan_mask); 1300 dma_writel(dw, CLEAR.ERROR, dw->all_chan_mask); 1301 1302 /* Set capabilities */ 1303 dma_cap_set(DMA_SLAVE, dw->dma.cap_mask); 1304 dma_cap_set(DMA_PRIVATE, dw->dma.cap_mask); 1305 dma_cap_set(DMA_MEMCPY, dw->dma.cap_mask); 1306 1307 dw->dma.dev = chip->dev; 1308 dw->dma.device_alloc_chan_resources = dwc_alloc_chan_resources; 1309 dw->dma.device_free_chan_resources = dwc_free_chan_resources; 1310 1311 dw->dma.device_prep_dma_memcpy = dwc_prep_dma_memcpy; 1312 dw->dma.device_prep_slave_sg = dwc_prep_slave_sg; 1313 1314 dw->dma.device_caps = dwc_caps; 1315 dw->dma.device_config = dwc_config; 1316 dw->dma.device_pause = dwc_pause; 1317 dw->dma.device_resume = dwc_resume; 1318 dw->dma.device_terminate_all = dwc_terminate_all; 1319 1320 dw->dma.device_tx_status = dwc_tx_status; 1321 dw->dma.device_issue_pending = dwc_issue_pending; 1322 1323 /* DMA capabilities */ 1324 dw->dma.min_burst = DW_DMA_MIN_BURST; 1325 dw->dma.max_burst = DW_DMA_MAX_BURST; 1326 dw->dma.src_addr_widths = DW_DMA_BUSWIDTHS; 1327 dw->dma.dst_addr_widths = DW_DMA_BUSWIDTHS; 1328 dw->dma.directions = BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV) | 1329 BIT(DMA_MEM_TO_MEM); 1330 dw->dma.residue_granularity = DMA_RESIDUE_GRANULARITY_BURST; 1331 1332 /* 1333 * For now there is no hardware with non uniform maximum block size 1334 * across all of the device channels, so we set the maximum segment 1335 * size as the block size found for the very first channel. 1336 */ 1337 dma_set_max_seg_size(dw->dma.dev, dw->chan[0].block_size); 1338 1339 ret = dma_async_device_register(&dw->dma); 1340 if (ret) 1341 goto err_dma_register; 1342 1343 dev_info(chip->dev, "DesignWare DMA Controller, %d channels\n", 1344 pdata->nr_channels); 1345 1346 pm_runtime_put_sync_suspend(chip->dev); 1347 1348 return 0; 1349 1350 err_dma_register: 1351 free_irq(chip->irq, dw); 1352 err_pdata: 1353 pm_runtime_put_sync_suspend(chip->dev); 1354 return ret; 1355 } 1356 1357 int do_dma_remove(struct dw_dma_chip *chip) 1358 { 1359 struct dw_dma *dw = chip->dw; 1360 struct dw_dma_chan *dwc, *_dwc; 1361 1362 pm_runtime_get_sync(chip->dev); 1363 1364 do_dw_dma_off(dw); 1365 dma_async_device_unregister(&dw->dma); 1366 1367 free_irq(chip->irq, dw); 1368 tasklet_kill(&dw->tasklet); 1369 1370 list_for_each_entry_safe(dwc, _dwc, &dw->dma.channels, 1371 chan.device_node) { 1372 list_del(&dwc->chan.device_node); 1373 channel_clear_bit(dw, CH_EN, dwc->mask); 1374 } 1375 1376 pm_runtime_put_sync_suspend(chip->dev); 1377 return 0; 1378 } 1379 1380 int do_dw_dma_disable(struct dw_dma_chip *chip) 1381 { 1382 struct dw_dma *dw = chip->dw; 1383 1384 dw->disable(dw); 1385 return 0; 1386 } 1387 EXPORT_SYMBOL_GPL(do_dw_dma_disable); 1388 1389 int do_dw_dma_enable(struct dw_dma_chip *chip) 1390 { 1391 struct dw_dma *dw = chip->dw; 1392 1393 dw->enable(dw); 1394 return 0; 1395 } 1396 EXPORT_SYMBOL_GPL(do_dw_dma_enable); 1397 1398 MODULE_LICENSE("GPL v2"); 1399 MODULE_DESCRIPTION("Synopsys DesignWare DMA Controller core driver"); 1400 MODULE_AUTHOR("Haavard Skinnemoen (Atmel)"); 1401 MODULE_AUTHOR("Viresh Kumar <vireshk@kernel.org>"); 1402