1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright 2012 Marvell International Ltd. 4 */ 5 6 #include <linux/err.h> 7 #include <linux/module.h> 8 #include <linux/init.h> 9 #include <linux/types.h> 10 #include <linux/interrupt.h> 11 #include <linux/dma-mapping.h> 12 #include <linux/slab.h> 13 #include <linux/dmaengine.h> 14 #include <linux/platform_device.h> 15 #include <linux/device.h> 16 #include <linux/platform_data/mmp_dma.h> 17 #include <linux/dmapool.h> 18 #include <linux/clk.h> 19 #include <linux/reset.h> 20 #include <linux/of_dma.h> 21 #include <linux/of.h> 22 23 #include "dmaengine.h" 24 25 #define DCSR 0x0000 26 #define DALGN 0x00a0 27 #define DINT 0x00f0 28 #define DDADR(n) (0x0200 + ((n) << 4)) 29 #define DSADR(n) (0x0204 + ((n) << 4)) 30 #define DTADR(n) (0x0208 + ((n) << 4)) 31 #define DDADRH(n) (0x0300 + ((n) << 4)) 32 #define DSADRH(n) (0x0304 + ((n) << 4)) 33 #define DTADRH(n) (0x0308 + ((n) << 4)) 34 #define DCMD 0x020c 35 36 #define DCSR_RUN BIT(31) /* Run Bit (read / write) */ 37 #define DCSR_NODESC BIT(30) /* No-Descriptor Fetch (read / write) */ 38 #define DCSR_STOPIRQEN BIT(29) /* Stop Interrupt Enable (read / write) */ 39 #define DCSR_REQPEND BIT(8) /* Request Pending (read-only) */ 40 #define DCSR_STOPSTATE BIT(3) /* Stop State (read-only) */ 41 #define DCSR_ENDINTR BIT(2) /* End Interrupt (read / write) */ 42 #define DCSR_STARTINTR BIT(1) /* Start Interrupt (read / write) */ 43 #define DCSR_BUSERR BIT(0) /* Bus Error Interrupt (read / write) */ 44 45 #define DCSR_EORIRQEN BIT(28) /* End of Receive Interrupt Enable (R/W) */ 46 #define DCSR_EORJMPEN BIT(27) /* Jump to next descriptor on EOR */ 47 #define DCSR_EORSTOPEN BIT(26) /* STOP on an EOR */ 48 #define DCSR_SETCMPST BIT(25) /* Set Descriptor Compare Status */ 49 #define DCSR_CLRCMPST BIT(24) /* Clear Descriptor Compare Status */ 50 #define DCSR_LPAEEN BIT(21) /* Long Physical Address Extension Enable */ 51 #define DCSR_CMPST BIT(10) /* The Descriptor Compare Status */ 52 #define DCSR_EORINTR BIT(9) /* The end of Receive */ 53 54 #define DRCMR_BASE 0x0100 55 #define DRCMR_EXT_BASE_K3 0x1000 56 #define DRCMR_EXT_BASE_DEFAULT 0x1100 57 #define DRCMR_REQ_LIMIT 64 58 #define DRCMR_MAPVLD BIT(7) /* Map Valid (read / write) */ 59 #define DRCMR_CHLNUM 0x1f /* mask for Channel Number (read / write) */ 60 61 #define DDADR_DESCADDR 0xfffffff0 /* Address of next descriptor (mask) */ 62 #define DDADR_STOP BIT(0) /* Stop (read / write) */ 63 64 #define DCMD_INCSRCADDR BIT(31) /* Source Address Increment Setting. */ 65 #define DCMD_INCTRGADDR BIT(30) /* Target Address Increment Setting. */ 66 #define DCMD_FLOWSRC BIT(29) /* Flow Control by the source. */ 67 #define DCMD_FLOWTRG BIT(28) /* Flow Control by the target. */ 68 #define DCMD_STARTIRQEN BIT(22) /* Start Interrupt Enable */ 69 #define DCMD_ENDIRQEN BIT(21) /* End Interrupt Enable */ 70 #define DCMD_ENDIAN BIT(18) /* Device Endian-ness. */ 71 #define DCMD_BURST8 (1 << 16) /* 8 byte burst */ 72 #define DCMD_BURST16 (2 << 16) /* 16 byte burst */ 73 #define DCMD_BURST32 (3 << 16) /* 32 byte burst */ 74 #define DCMD_WIDTH1 (1 << 14) /* 1 byte width */ 75 #define DCMD_WIDTH2 (2 << 14) /* 2 byte width (HalfWord) */ 76 #define DCMD_WIDTH4 (3 << 14) /* 4 byte width (Word) */ 77 #define DCMD_LENGTH 0x01fff /* length mask (max = 8K - 1) */ 78 79 #define PDMA_MAX_DESC_BYTES DCMD_LENGTH 80 81 struct mmp_pdma_desc_hw { 82 u32 ddadr; /* Points to the next descriptor + flags */ 83 u32 dsadr; /* DSADR value for the current transfer */ 84 u32 dtadr; /* DTADR value for the current transfer */ 85 u32 dcmd; /* DCMD value for the current transfer */ 86 /* 87 * The following 32-bit words are only used in the 64-bit, ie. 88 * LPAE (Long Physical Address Extension) mode. 89 * They are used to specify the high 32 bits of the descriptor's 90 * addresses. 91 */ 92 u32 ddadrh; /* High 32-bit of DDADR */ 93 u32 dsadrh; /* High 32-bit of DSADR */ 94 u32 dtadrh; /* High 32-bit of DTADR */ 95 u32 rsvd; /* reserved */ 96 } __aligned(32); 97 98 struct mmp_pdma_desc_sw { 99 struct mmp_pdma_desc_hw desc; 100 struct list_head node; 101 struct list_head tx_list; 102 struct dma_async_tx_descriptor async_tx; 103 }; 104 105 struct mmp_pdma_phy; 106 107 struct mmp_pdma_chan { 108 struct device *dev; 109 struct dma_chan chan; 110 struct dma_async_tx_descriptor desc; 111 struct mmp_pdma_phy *phy; 112 enum dma_transfer_direction dir; 113 struct dma_slave_config slave_config; 114 115 struct mmp_pdma_desc_sw *cyclic_first; /* first desc_sw if channel 116 * is in cyclic mode */ 117 118 /* channel's basic info */ 119 struct tasklet_struct tasklet; 120 u32 dcmd; 121 u32 drcmr; 122 u32 dev_addr; 123 124 /* list for desc */ 125 spinlock_t desc_lock; /* Descriptor list lock */ 126 struct list_head chain_pending; /* Link descriptors queue for pending */ 127 struct list_head chain_running; /* Link descriptors queue for running */ 128 bool idle; /* channel statue machine */ 129 bool byte_align; 130 131 struct dma_pool *desc_pool; /* Descriptors pool */ 132 }; 133 134 struct mmp_pdma_phy { 135 int idx; 136 void __iomem *base; 137 struct mmp_pdma_chan *vchan; 138 }; 139 140 /** 141 * struct mmp_pdma_ops - Operations for the MMP PDMA controller 142 * 143 * Hardware Register Operations (read/write hardware registers): 144 * @write_next_addr: Function to program address of next descriptor into 145 * DDADR/DDADRH 146 * @read_src_addr: Function to read the source address from DSADR/DSADRH 147 * @read_dst_addr: Function to read the destination address from DTADR/DTADRH 148 * 149 * Descriptor Memory Operations (manipulate descriptor structs in memory): 150 * @set_desc_next_addr: Function to set next descriptor address in descriptor 151 * @set_desc_src_addr: Function to set the source address in descriptor 152 * @set_desc_dst_addr: Function to set the destination address in descriptor 153 * @get_desc_src_addr: Function to get the source address from descriptor 154 * @get_desc_dst_addr: Function to get the destination address from descriptor 155 * 156 * Controller Configuration: 157 * @run_bits: Control bits in DCSR register for channel start/stop 158 * @dma_width: DMA addressing width in bits (32 or 64). Determines the 159 * DMA mask capability of the controller hardware. 160 * @drcmr_ext_base: Base DRCMR address for extended requests 161 */ 162 struct mmp_pdma_ops { 163 /* Hardware Register Operations */ 164 void (*write_next_addr)(struct mmp_pdma_phy *phy, dma_addr_t addr); 165 u64 (*read_src_addr)(struct mmp_pdma_phy *phy); 166 u64 (*read_dst_addr)(struct mmp_pdma_phy *phy); 167 168 /* Descriptor Memory Operations */ 169 void (*set_desc_next_addr)(struct mmp_pdma_desc_hw *desc, 170 dma_addr_t addr); 171 void (*set_desc_src_addr)(struct mmp_pdma_desc_hw *desc, 172 dma_addr_t addr); 173 void (*set_desc_dst_addr)(struct mmp_pdma_desc_hw *desc, 174 dma_addr_t addr); 175 u64 (*get_desc_src_addr)(const struct mmp_pdma_desc_hw *desc); 176 u64 (*get_desc_dst_addr)(const struct mmp_pdma_desc_hw *desc); 177 178 /* Controller Configuration */ 179 u32 run_bits; 180 u32 dma_width; 181 u32 drcmr_ext_base; 182 }; 183 184 struct mmp_pdma_device { 185 int dma_channels; 186 void __iomem *base; 187 struct device *dev; 188 struct dma_device device; 189 struct mmp_pdma_phy *phy; 190 const struct mmp_pdma_ops *ops; 191 spinlock_t phy_lock; /* protect alloc/free phy channels */ 192 }; 193 194 #define tx_to_mmp_pdma_desc(tx) \ 195 container_of(tx, struct mmp_pdma_desc_sw, async_tx) 196 #define to_mmp_pdma_desc(lh) \ 197 container_of(lh, struct mmp_pdma_desc_sw, node) 198 #define to_mmp_pdma_chan(dchan) \ 199 container_of(dchan, struct mmp_pdma_chan, chan) 200 #define to_mmp_pdma_dev(dmadev) \ 201 container_of(dmadev, struct mmp_pdma_device, device) 202 203 static u32 mmp_pdma_get_drcmr(struct mmp_pdma_device *pdev, u32 drcmr) 204 { 205 if (drcmr < DRCMR_REQ_LIMIT) 206 return DRCMR_BASE + (drcmr << 2); 207 return pdev->ops->drcmr_ext_base + ((drcmr - DRCMR_REQ_LIMIT) << 2); 208 } 209 210 /* For 32-bit PDMA */ 211 static void write_next_addr_32(struct mmp_pdma_phy *phy, dma_addr_t addr) 212 { 213 writel(addr, phy->base + DDADR(phy->idx)); 214 } 215 216 static u64 read_src_addr_32(struct mmp_pdma_phy *phy) 217 { 218 return readl(phy->base + DSADR(phy->idx)); 219 } 220 221 static u64 read_dst_addr_32(struct mmp_pdma_phy *phy) 222 { 223 return readl(phy->base + DTADR(phy->idx)); 224 } 225 226 static void set_desc_next_addr_32(struct mmp_pdma_desc_hw *desc, dma_addr_t addr) 227 { 228 desc->ddadr = addr; 229 } 230 231 static void set_desc_src_addr_32(struct mmp_pdma_desc_hw *desc, dma_addr_t addr) 232 { 233 desc->dsadr = addr; 234 } 235 236 static void set_desc_dst_addr_32(struct mmp_pdma_desc_hw *desc, dma_addr_t addr) 237 { 238 desc->dtadr = addr; 239 } 240 241 static u64 get_desc_src_addr_32(const struct mmp_pdma_desc_hw *desc) 242 { 243 return desc->dsadr; 244 } 245 246 static u64 get_desc_dst_addr_32(const struct mmp_pdma_desc_hw *desc) 247 { 248 return desc->dtadr; 249 } 250 251 /* For 64-bit PDMA */ 252 static void write_next_addr_64(struct mmp_pdma_phy *phy, dma_addr_t addr) 253 { 254 writel(lower_32_bits(addr), phy->base + DDADR(phy->idx)); 255 writel(upper_32_bits(addr), phy->base + DDADRH(phy->idx)); 256 } 257 258 static u64 read_src_addr_64(struct mmp_pdma_phy *phy) 259 { 260 u32 low = readl(phy->base + DSADR(phy->idx)); 261 u32 high = readl(phy->base + DSADRH(phy->idx)); 262 263 return ((u64)high << 32) | low; 264 } 265 266 static u64 read_dst_addr_64(struct mmp_pdma_phy *phy) 267 { 268 u32 low = readl(phy->base + DTADR(phy->idx)); 269 u32 high = readl(phy->base + DTADRH(phy->idx)); 270 271 return ((u64)high << 32) | low; 272 } 273 274 static void set_desc_next_addr_64(struct mmp_pdma_desc_hw *desc, dma_addr_t addr) 275 { 276 desc->ddadr = lower_32_bits(addr); 277 desc->ddadrh = upper_32_bits(addr); 278 } 279 280 static void set_desc_src_addr_64(struct mmp_pdma_desc_hw *desc, dma_addr_t addr) 281 { 282 desc->dsadr = lower_32_bits(addr); 283 desc->dsadrh = upper_32_bits(addr); 284 } 285 286 static void set_desc_dst_addr_64(struct mmp_pdma_desc_hw *desc, dma_addr_t addr) 287 { 288 desc->dtadr = lower_32_bits(addr); 289 desc->dtadrh = upper_32_bits(addr); 290 } 291 292 static u64 get_desc_src_addr_64(const struct mmp_pdma_desc_hw *desc) 293 { 294 return ((u64)desc->dsadrh << 32) | desc->dsadr; 295 } 296 297 static u64 get_desc_dst_addr_64(const struct mmp_pdma_desc_hw *desc) 298 { 299 return ((u64)desc->dtadrh << 32) | desc->dtadr; 300 } 301 302 static int mmp_pdma_config_write(struct dma_chan *dchan, 303 struct dma_slave_config *cfg, 304 enum dma_transfer_direction direction); 305 306 static void enable_chan(struct mmp_pdma_phy *phy) 307 { 308 u32 reg, dalgn; 309 struct mmp_pdma_device *pdev; 310 311 if (!phy->vchan) 312 return; 313 314 pdev = to_mmp_pdma_dev(phy->vchan->chan.device); 315 316 reg = mmp_pdma_get_drcmr(pdev, phy->vchan->drcmr); 317 writel(DRCMR_MAPVLD | phy->idx, phy->base + reg); 318 319 dalgn = readl(phy->base + DALGN); 320 if (phy->vchan->byte_align) 321 dalgn |= 1 << phy->idx; 322 else 323 dalgn &= ~(1 << phy->idx); 324 writel(dalgn, phy->base + DALGN); 325 326 reg = (phy->idx << 2) + DCSR; 327 writel(readl(phy->base + reg) | pdev->ops->run_bits, 328 phy->base + reg); 329 } 330 331 static void disable_chan(struct mmp_pdma_phy *phy) 332 { 333 u32 reg, dcsr; 334 335 if (!phy) 336 return; 337 338 reg = (phy->idx << 2) + DCSR; 339 dcsr = readl(phy->base + reg); 340 341 if (phy->vchan) { 342 struct mmp_pdma_device *pdev; 343 344 pdev = to_mmp_pdma_dev(phy->vchan->chan.device); 345 writel(dcsr & ~pdev->ops->run_bits, phy->base + reg); 346 } else { 347 /* If no vchan, just clear the RUN bit */ 348 writel(dcsr & ~DCSR_RUN, phy->base + reg); 349 } 350 } 351 352 static int clear_chan_irq(struct mmp_pdma_phy *phy) 353 { 354 u32 dcsr; 355 u32 dint = readl(phy->base + DINT); 356 u32 reg = (phy->idx << 2) + DCSR; 357 358 if (!(dint & BIT(phy->idx))) 359 return -EAGAIN; 360 361 /* clear irq */ 362 dcsr = readl(phy->base + reg); 363 writel(dcsr, phy->base + reg); 364 if ((dcsr & DCSR_BUSERR) && (phy->vchan)) 365 dev_warn(phy->vchan->dev, "DCSR_BUSERR\n"); 366 367 return 0; 368 } 369 370 static irqreturn_t mmp_pdma_chan_handler(int irq, void *dev_id) 371 { 372 struct mmp_pdma_phy *phy = dev_id; 373 374 if (clear_chan_irq(phy) != 0) 375 return IRQ_NONE; 376 377 tasklet_schedule(&phy->vchan->tasklet); 378 return IRQ_HANDLED; 379 } 380 381 static irqreturn_t mmp_pdma_int_handler(int irq, void *dev_id) 382 { 383 struct mmp_pdma_device *pdev = dev_id; 384 struct mmp_pdma_phy *phy; 385 u32 dint = readl(pdev->base + DINT); 386 int i, ret; 387 int irq_num = 0; 388 389 while (dint) { 390 i = __ffs(dint); 391 /* only handle interrupts belonging to pdma driver*/ 392 if (i >= pdev->dma_channels) 393 break; 394 dint &= (dint - 1); 395 phy = &pdev->phy[i]; 396 ret = mmp_pdma_chan_handler(irq, phy); 397 if (ret == IRQ_HANDLED) 398 irq_num++; 399 } 400 401 if (irq_num) 402 return IRQ_HANDLED; 403 404 return IRQ_NONE; 405 } 406 407 /* lookup free phy channel as descending priority */ 408 static struct mmp_pdma_phy *lookup_phy(struct mmp_pdma_chan *pchan) 409 { 410 int prio, i; 411 struct mmp_pdma_device *pdev = to_mmp_pdma_dev(pchan->chan.device); 412 struct mmp_pdma_phy *phy, *found = NULL; 413 unsigned long flags; 414 415 /* 416 * dma channel priorities 417 * ch 0 - 3, 16 - 19 <--> (0) 418 * ch 4 - 7, 20 - 23 <--> (1) 419 * ch 8 - 11, 24 - 27 <--> (2) 420 * ch 12 - 15, 28 - 31 <--> (3) 421 */ 422 423 spin_lock_irqsave(&pdev->phy_lock, flags); 424 for (prio = 0; prio <= ((pdev->dma_channels - 1) & 0xf) >> 2; prio++) { 425 for (i = 0; i < pdev->dma_channels; i++) { 426 if (prio != (i & 0xf) >> 2) 427 continue; 428 phy = &pdev->phy[i]; 429 if (!phy->vchan) { 430 phy->vchan = pchan; 431 found = phy; 432 goto out_unlock; 433 } 434 } 435 } 436 437 out_unlock: 438 spin_unlock_irqrestore(&pdev->phy_lock, flags); 439 return found; 440 } 441 442 static void mmp_pdma_free_phy(struct mmp_pdma_chan *pchan) 443 { 444 struct mmp_pdma_device *pdev = to_mmp_pdma_dev(pchan->chan.device); 445 unsigned long flags; 446 u32 reg; 447 448 if (!pchan->phy) 449 return; 450 451 /* clear the channel mapping in DRCMR */ 452 reg = mmp_pdma_get_drcmr(pdev, pchan->drcmr); 453 writel(0, pchan->phy->base + reg); 454 455 spin_lock_irqsave(&pdev->phy_lock, flags); 456 pchan->phy->vchan = NULL; 457 pchan->phy = NULL; 458 spin_unlock_irqrestore(&pdev->phy_lock, flags); 459 } 460 461 /* 462 * start_pending_queue - transfer any pending transactions 463 * pending list ==> running list 464 */ 465 static void start_pending_queue(struct mmp_pdma_chan *chan) 466 { 467 struct mmp_pdma_desc_sw *desc; 468 struct mmp_pdma_device *pdev = to_mmp_pdma_dev(chan->chan.device); 469 470 /* still in running, irq will start the pending list */ 471 if (!chan->idle) { 472 dev_dbg(chan->dev, "DMA controller still busy\n"); 473 return; 474 } 475 476 if (list_empty(&chan->chain_pending)) { 477 /* chance to re-fetch phy channel with higher prio */ 478 mmp_pdma_free_phy(chan); 479 dev_dbg(chan->dev, "no pending list\n"); 480 return; 481 } 482 483 if (!chan->phy) { 484 chan->phy = lookup_phy(chan); 485 if (!chan->phy) { 486 dev_dbg(chan->dev, "no free dma channel\n"); 487 return; 488 } 489 } 490 491 /* 492 * pending -> running 493 * reintilize pending list 494 */ 495 desc = list_first_entry(&chan->chain_pending, 496 struct mmp_pdma_desc_sw, node); 497 list_splice_tail_init(&chan->chain_pending, &chan->chain_running); 498 499 /* 500 * Program the descriptor's address into the DMA controller, 501 * then start the DMA transaction 502 */ 503 pdev->ops->write_next_addr(chan->phy, desc->async_tx.phys); 504 enable_chan(chan->phy); 505 chan->idle = false; 506 } 507 508 509 /* desc->tx_list ==> pending list */ 510 static dma_cookie_t mmp_pdma_tx_submit(struct dma_async_tx_descriptor *tx) 511 { 512 struct mmp_pdma_chan *chan = to_mmp_pdma_chan(tx->chan); 513 struct mmp_pdma_desc_sw *desc = tx_to_mmp_pdma_desc(tx); 514 struct mmp_pdma_desc_sw *child; 515 unsigned long flags; 516 dma_cookie_t cookie = -EBUSY; 517 518 spin_lock_irqsave(&chan->desc_lock, flags); 519 520 list_for_each_entry(child, &desc->tx_list, node) { 521 cookie = dma_cookie_assign(&child->async_tx); 522 } 523 524 /* softly link to pending list - desc->tx_list ==> pending list */ 525 list_splice_tail_init(&desc->tx_list, &chan->chain_pending); 526 527 spin_unlock_irqrestore(&chan->desc_lock, flags); 528 529 return cookie; 530 } 531 532 static struct mmp_pdma_desc_sw * 533 mmp_pdma_alloc_descriptor(struct mmp_pdma_chan *chan) 534 { 535 struct mmp_pdma_desc_sw *desc; 536 dma_addr_t pdesc; 537 538 desc = dma_pool_zalloc(chan->desc_pool, GFP_ATOMIC, &pdesc); 539 if (!desc) { 540 dev_err(chan->dev, "out of memory for link descriptor\n"); 541 return NULL; 542 } 543 544 INIT_LIST_HEAD(&desc->tx_list); 545 dma_async_tx_descriptor_init(&desc->async_tx, &chan->chan); 546 /* each desc has submit */ 547 desc->async_tx.tx_submit = mmp_pdma_tx_submit; 548 desc->async_tx.phys = pdesc; 549 550 return desc; 551 } 552 553 /* 554 * mmp_pdma_alloc_chan_resources - Allocate resources for DMA channel. 555 * 556 * This function will create a dma pool for descriptor allocation. 557 * Request irq only when channel is requested 558 * Return - The number of allocated descriptors. 559 */ 560 561 static int mmp_pdma_alloc_chan_resources(struct dma_chan *dchan) 562 { 563 struct mmp_pdma_chan *chan = to_mmp_pdma_chan(dchan); 564 565 if (chan->desc_pool) 566 return 1; 567 568 chan->desc_pool = dma_pool_create(dev_name(&dchan->dev->device), 569 chan->dev, 570 sizeof(struct mmp_pdma_desc_sw), 571 __alignof__(struct mmp_pdma_desc_sw), 572 0); 573 if (!chan->desc_pool) { 574 dev_err(chan->dev, "unable to allocate descriptor pool\n"); 575 return -ENOMEM; 576 } 577 578 mmp_pdma_free_phy(chan); 579 chan->idle = true; 580 chan->dev_addr = 0; 581 return 1; 582 } 583 584 static void mmp_pdma_free_desc_list(struct mmp_pdma_chan *chan, 585 struct list_head *list) 586 { 587 struct mmp_pdma_desc_sw *desc, *_desc; 588 589 list_for_each_entry_safe(desc, _desc, list, node) { 590 list_del(&desc->node); 591 dma_pool_free(chan->desc_pool, desc, desc->async_tx.phys); 592 } 593 } 594 595 static void mmp_pdma_free_chan_resources(struct dma_chan *dchan) 596 { 597 struct mmp_pdma_chan *chan = to_mmp_pdma_chan(dchan); 598 unsigned long flags; 599 600 spin_lock_irqsave(&chan->desc_lock, flags); 601 mmp_pdma_free_desc_list(chan, &chan->chain_pending); 602 mmp_pdma_free_desc_list(chan, &chan->chain_running); 603 spin_unlock_irqrestore(&chan->desc_lock, flags); 604 605 dma_pool_destroy(chan->desc_pool); 606 chan->desc_pool = NULL; 607 chan->idle = true; 608 chan->dev_addr = 0; 609 mmp_pdma_free_phy(chan); 610 return; 611 } 612 613 static struct dma_async_tx_descriptor * 614 mmp_pdma_prep_memcpy(struct dma_chan *dchan, 615 dma_addr_t dma_dst, dma_addr_t dma_src, 616 size_t len, unsigned long flags) 617 { 618 struct mmp_pdma_chan *chan; 619 struct mmp_pdma_device *pdev; 620 struct mmp_pdma_desc_sw *first = NULL, *prev = NULL, *new; 621 size_t copy = 0; 622 623 if (!dchan || !len) 624 return NULL; 625 626 pdev = to_mmp_pdma_dev(dchan->device); 627 chan = to_mmp_pdma_chan(dchan); 628 chan->byte_align = false; 629 630 if (!chan->dir) { 631 chan->dir = DMA_MEM_TO_MEM; 632 chan->dcmd = DCMD_INCTRGADDR | DCMD_INCSRCADDR; 633 chan->dcmd |= DCMD_BURST32; 634 } 635 636 do { 637 /* Allocate the link descriptor from DMA pool */ 638 new = mmp_pdma_alloc_descriptor(chan); 639 if (!new) { 640 dev_err(chan->dev, "no memory for desc\n"); 641 goto fail; 642 } 643 644 copy = min_t(size_t, len, PDMA_MAX_DESC_BYTES); 645 if (dma_src & 0x7 || dma_dst & 0x7) 646 chan->byte_align = true; 647 648 new->desc.dcmd = chan->dcmd | (DCMD_LENGTH & copy); 649 pdev->ops->set_desc_src_addr(&new->desc, dma_src); 650 pdev->ops->set_desc_dst_addr(&new->desc, dma_dst); 651 652 if (!first) 653 first = new; 654 else 655 pdev->ops->set_desc_next_addr(&prev->desc, 656 new->async_tx.phys); 657 658 new->async_tx.cookie = 0; 659 async_tx_ack(&new->async_tx); 660 661 prev = new; 662 len -= copy; 663 664 if (chan->dir == DMA_MEM_TO_DEV) { 665 dma_src += copy; 666 } else if (chan->dir == DMA_DEV_TO_MEM) { 667 dma_dst += copy; 668 } else if (chan->dir == DMA_MEM_TO_MEM) { 669 dma_src += copy; 670 dma_dst += copy; 671 } 672 673 /* Insert the link descriptor to the LD ring */ 674 list_add_tail(&new->node, &first->tx_list); 675 } while (len); 676 677 first->async_tx.flags = flags; /* client is in control of this ack */ 678 first->async_tx.cookie = -EBUSY; 679 680 /* last desc and fire IRQ */ 681 new->desc.ddadr = DDADR_STOP; 682 new->desc.dcmd |= DCMD_ENDIRQEN; 683 684 chan->cyclic_first = NULL; 685 686 return &first->async_tx; 687 688 fail: 689 if (first) 690 mmp_pdma_free_desc_list(chan, &first->tx_list); 691 return NULL; 692 } 693 694 static struct dma_async_tx_descriptor * 695 mmp_pdma_prep_slave_sg(struct dma_chan *dchan, struct scatterlist *sgl, 696 unsigned int sg_len, enum dma_transfer_direction dir, 697 unsigned long flags, void *context) 698 { 699 struct mmp_pdma_chan *chan = to_mmp_pdma_chan(dchan); 700 struct mmp_pdma_device *pdev = to_mmp_pdma_dev(dchan->device); 701 struct mmp_pdma_desc_sw *first = NULL, *prev = NULL, *new = NULL; 702 size_t len, avail; 703 struct scatterlist *sg; 704 dma_addr_t addr; 705 int i; 706 707 if ((sgl == NULL) || (sg_len == 0)) 708 return NULL; 709 710 chan->byte_align = false; 711 712 mmp_pdma_config_write(dchan, &chan->slave_config, dir); 713 714 for_each_sg(sgl, sg, sg_len, i) { 715 addr = sg_dma_address(sg); 716 avail = sg_dma_len(sgl); 717 718 do { 719 len = min_t(size_t, avail, PDMA_MAX_DESC_BYTES); 720 if (addr & 0x7) 721 chan->byte_align = true; 722 723 /* allocate and populate the descriptor */ 724 new = mmp_pdma_alloc_descriptor(chan); 725 if (!new) { 726 dev_err(chan->dev, "no memory for desc\n"); 727 goto fail; 728 } 729 730 new->desc.dcmd = chan->dcmd | (DCMD_LENGTH & len); 731 if (dir == DMA_MEM_TO_DEV) { 732 pdev->ops->set_desc_src_addr(&new->desc, addr); 733 new->desc.dtadr = chan->dev_addr; 734 } else { 735 new->desc.dsadr = chan->dev_addr; 736 pdev->ops->set_desc_dst_addr(&new->desc, addr); 737 } 738 739 if (!first) 740 first = new; 741 else 742 pdev->ops->set_desc_next_addr(&prev->desc, 743 new->async_tx.phys); 744 745 new->async_tx.cookie = 0; 746 async_tx_ack(&new->async_tx); 747 prev = new; 748 749 /* Insert the link descriptor to the LD ring */ 750 list_add_tail(&new->node, &first->tx_list); 751 752 /* update metadata */ 753 addr += len; 754 avail -= len; 755 } while (avail); 756 } 757 758 first->async_tx.cookie = -EBUSY; 759 first->async_tx.flags = flags; 760 761 /* last desc and fire IRQ */ 762 new->desc.ddadr = DDADR_STOP; 763 new->desc.dcmd |= DCMD_ENDIRQEN; 764 765 chan->dir = dir; 766 chan->cyclic_first = NULL; 767 768 return &first->async_tx; 769 770 fail: 771 if (first) 772 mmp_pdma_free_desc_list(chan, &first->tx_list); 773 return NULL; 774 } 775 776 static struct dma_async_tx_descriptor * 777 mmp_pdma_prep_dma_cyclic(struct dma_chan *dchan, 778 dma_addr_t buf_addr, size_t len, size_t period_len, 779 enum dma_transfer_direction direction, 780 unsigned long flags) 781 { 782 struct mmp_pdma_chan *chan; 783 struct mmp_pdma_device *pdev; 784 struct mmp_pdma_desc_sw *first = NULL, *prev = NULL, *new; 785 dma_addr_t dma_src, dma_dst; 786 787 if (!dchan || !len || !period_len) 788 return NULL; 789 790 pdev = to_mmp_pdma_dev(dchan->device); 791 792 /* the buffer length must be a multiple of period_len */ 793 if (len % period_len != 0) 794 return NULL; 795 796 if (period_len > PDMA_MAX_DESC_BYTES) 797 return NULL; 798 799 chan = to_mmp_pdma_chan(dchan); 800 mmp_pdma_config_write(dchan, &chan->slave_config, direction); 801 802 switch (direction) { 803 case DMA_MEM_TO_DEV: 804 dma_src = buf_addr; 805 dma_dst = chan->dev_addr; 806 break; 807 case DMA_DEV_TO_MEM: 808 dma_dst = buf_addr; 809 dma_src = chan->dev_addr; 810 break; 811 default: 812 dev_err(chan->dev, "Unsupported direction for cyclic DMA\n"); 813 return NULL; 814 } 815 816 chan->dir = direction; 817 818 do { 819 /* Allocate the link descriptor from DMA pool */ 820 new = mmp_pdma_alloc_descriptor(chan); 821 if (!new) { 822 dev_err(chan->dev, "no memory for desc\n"); 823 goto fail; 824 } 825 826 new->desc.dcmd = (chan->dcmd | DCMD_ENDIRQEN | 827 (DCMD_LENGTH & period_len)); 828 pdev->ops->set_desc_src_addr(&new->desc, dma_src); 829 pdev->ops->set_desc_dst_addr(&new->desc, dma_dst); 830 831 if (!first) 832 first = new; 833 else 834 pdev->ops->set_desc_next_addr(&prev->desc, 835 new->async_tx.phys); 836 837 new->async_tx.cookie = 0; 838 async_tx_ack(&new->async_tx); 839 840 prev = new; 841 len -= period_len; 842 843 if (chan->dir == DMA_MEM_TO_DEV) 844 dma_src += period_len; 845 else 846 dma_dst += period_len; 847 848 /* Insert the link descriptor to the LD ring */ 849 list_add_tail(&new->node, &first->tx_list); 850 } while (len); 851 852 first->async_tx.flags = flags; /* client is in control of this ack */ 853 first->async_tx.cookie = -EBUSY; 854 855 /* make the cyclic link */ 856 pdev->ops->set_desc_next_addr(&new->desc, first->async_tx.phys); 857 chan->cyclic_first = first; 858 859 return &first->async_tx; 860 861 fail: 862 if (first) 863 mmp_pdma_free_desc_list(chan, &first->tx_list); 864 return NULL; 865 } 866 867 static int mmp_pdma_config_write(struct dma_chan *dchan, 868 struct dma_slave_config *cfg, 869 enum dma_transfer_direction direction) 870 { 871 struct mmp_pdma_chan *chan = to_mmp_pdma_chan(dchan); 872 u32 maxburst = 0, addr = 0; 873 enum dma_slave_buswidth width = DMA_SLAVE_BUSWIDTH_UNDEFINED; 874 875 if (!dchan) 876 return -EINVAL; 877 878 if (direction == DMA_DEV_TO_MEM) { 879 chan->dcmd = DCMD_INCTRGADDR | DCMD_FLOWSRC; 880 maxburst = cfg->src_maxburst; 881 width = cfg->src_addr_width; 882 addr = cfg->src_addr; 883 } else if (direction == DMA_MEM_TO_DEV) { 884 chan->dcmd = DCMD_INCSRCADDR | DCMD_FLOWTRG; 885 maxburst = cfg->dst_maxburst; 886 width = cfg->dst_addr_width; 887 addr = cfg->dst_addr; 888 } 889 890 if (width == DMA_SLAVE_BUSWIDTH_1_BYTE) 891 chan->dcmd |= DCMD_WIDTH1; 892 else if (width == DMA_SLAVE_BUSWIDTH_2_BYTES) 893 chan->dcmd |= DCMD_WIDTH2; 894 else if (width == DMA_SLAVE_BUSWIDTH_4_BYTES) 895 chan->dcmd |= DCMD_WIDTH4; 896 897 if (maxburst == 8) 898 chan->dcmd |= DCMD_BURST8; 899 else if (maxburst == 16) 900 chan->dcmd |= DCMD_BURST16; 901 else if (maxburst == 32) 902 chan->dcmd |= DCMD_BURST32; 903 904 chan->dir = direction; 905 chan->dev_addr = addr; 906 907 return 0; 908 } 909 910 static int mmp_pdma_config(struct dma_chan *dchan, 911 struct dma_slave_config *cfg) 912 { 913 struct mmp_pdma_chan *chan = to_mmp_pdma_chan(dchan); 914 915 memcpy(&chan->slave_config, cfg, sizeof(*cfg)); 916 return 0; 917 } 918 919 static int mmp_pdma_terminate_all(struct dma_chan *dchan) 920 { 921 struct mmp_pdma_chan *chan = to_mmp_pdma_chan(dchan); 922 unsigned long flags; 923 924 if (!dchan) 925 return -EINVAL; 926 927 disable_chan(chan->phy); 928 mmp_pdma_free_phy(chan); 929 spin_lock_irqsave(&chan->desc_lock, flags); 930 mmp_pdma_free_desc_list(chan, &chan->chain_pending); 931 mmp_pdma_free_desc_list(chan, &chan->chain_running); 932 spin_unlock_irqrestore(&chan->desc_lock, flags); 933 chan->idle = true; 934 935 return 0; 936 } 937 938 static unsigned int mmp_pdma_residue(struct mmp_pdma_chan *chan, 939 dma_cookie_t cookie) 940 { 941 struct mmp_pdma_desc_sw *sw; 942 struct mmp_pdma_device *pdev = to_mmp_pdma_dev(chan->chan.device); 943 unsigned long flags; 944 u64 curr; 945 u32 residue = 0; 946 bool passed = false; 947 bool cyclic = chan->cyclic_first != NULL; 948 949 /* 950 * If the channel does not have a phy pointer anymore, it has already 951 * been completed. Therefore, its residue is 0. 952 */ 953 if (!chan->phy) 954 return 0; 955 956 if (chan->dir == DMA_DEV_TO_MEM) 957 curr = pdev->ops->read_dst_addr(chan->phy); 958 else 959 curr = pdev->ops->read_src_addr(chan->phy); 960 961 spin_lock_irqsave(&chan->desc_lock, flags); 962 963 list_for_each_entry(sw, &chan->chain_running, node) { 964 u64 start, end; 965 u32 len; 966 967 if (chan->dir == DMA_DEV_TO_MEM) 968 start = pdev->ops->get_desc_dst_addr(&sw->desc); 969 else 970 start = pdev->ops->get_desc_src_addr(&sw->desc); 971 972 len = sw->desc.dcmd & DCMD_LENGTH; 973 end = start + len; 974 975 /* 976 * 'passed' will be latched once we found the descriptor which 977 * lies inside the boundaries of the curr pointer. All 978 * descriptors that occur in the list _after_ we found that 979 * partially handled descriptor are still to be processed and 980 * are hence added to the residual bytes counter. 981 */ 982 983 if (passed) { 984 residue += len; 985 } else if (curr >= start && curr <= end) { 986 residue += (u32)(end - curr); 987 passed = true; 988 } 989 990 /* 991 * Descriptors that have the ENDIRQEN bit set mark the end of a 992 * transaction chain, and the cookie assigned with it has been 993 * returned previously from mmp_pdma_tx_submit(). 994 * 995 * In case we have multiple transactions in the running chain, 996 * and the cookie does not match the one the user asked us 997 * about, reset the state variables and start over. 998 * 999 * This logic does not apply to cyclic transactions, where all 1000 * descriptors have the ENDIRQEN bit set, and for which we 1001 * can't have multiple transactions on one channel anyway. 1002 */ 1003 if (cyclic || !(sw->desc.dcmd & DCMD_ENDIRQEN)) 1004 continue; 1005 1006 if (sw->async_tx.cookie == cookie) { 1007 spin_unlock_irqrestore(&chan->desc_lock, flags); 1008 return residue; 1009 } else { 1010 residue = 0; 1011 passed = false; 1012 } 1013 } 1014 1015 spin_unlock_irqrestore(&chan->desc_lock, flags); 1016 1017 /* We should only get here in case of cyclic transactions */ 1018 return residue; 1019 } 1020 1021 static enum dma_status mmp_pdma_tx_status(struct dma_chan *dchan, 1022 dma_cookie_t cookie, 1023 struct dma_tx_state *txstate) 1024 { 1025 struct mmp_pdma_chan *chan = to_mmp_pdma_chan(dchan); 1026 enum dma_status ret; 1027 1028 ret = dma_cookie_status(dchan, cookie, txstate); 1029 if (likely(ret != DMA_ERROR)) 1030 dma_set_residue(txstate, mmp_pdma_residue(chan, cookie)); 1031 1032 return ret; 1033 } 1034 1035 /* 1036 * mmp_pdma_issue_pending - Issue the DMA start command 1037 * pending list ==> running list 1038 */ 1039 static void mmp_pdma_issue_pending(struct dma_chan *dchan) 1040 { 1041 struct mmp_pdma_chan *chan = to_mmp_pdma_chan(dchan); 1042 unsigned long flags; 1043 1044 spin_lock_irqsave(&chan->desc_lock, flags); 1045 start_pending_queue(chan); 1046 spin_unlock_irqrestore(&chan->desc_lock, flags); 1047 } 1048 1049 /* 1050 * dma_do_tasklet 1051 * Do call back 1052 * Start pending list 1053 */ 1054 static void dma_do_tasklet(struct tasklet_struct *t) 1055 { 1056 struct mmp_pdma_chan *chan = from_tasklet(chan, t, tasklet); 1057 struct mmp_pdma_desc_sw *desc, *_desc; 1058 LIST_HEAD(chain_cleanup); 1059 unsigned long flags; 1060 struct dmaengine_desc_callback cb; 1061 1062 if (chan->cyclic_first) { 1063 spin_lock_irqsave(&chan->desc_lock, flags); 1064 desc = chan->cyclic_first; 1065 dmaengine_desc_get_callback(&desc->async_tx, &cb); 1066 spin_unlock_irqrestore(&chan->desc_lock, flags); 1067 1068 dmaengine_desc_callback_invoke(&cb, NULL); 1069 1070 return; 1071 } 1072 1073 /* submit pending list; callback for each desc; free desc */ 1074 spin_lock_irqsave(&chan->desc_lock, flags); 1075 1076 list_for_each_entry_safe(desc, _desc, &chan->chain_running, node) { 1077 /* 1078 * move the descriptors to a temporary list so we can drop 1079 * the lock during the entire cleanup operation 1080 */ 1081 list_move(&desc->node, &chain_cleanup); 1082 1083 /* 1084 * Look for the first list entry which has the ENDIRQEN flag 1085 * set. That is the descriptor we got an interrupt for, so 1086 * complete that transaction and its cookie. 1087 */ 1088 if (desc->desc.dcmd & DCMD_ENDIRQEN) { 1089 dma_cookie_t cookie = desc->async_tx.cookie; 1090 dma_cookie_complete(&desc->async_tx); 1091 dev_dbg(chan->dev, "completed_cookie=%d\n", cookie); 1092 break; 1093 } 1094 } 1095 1096 /* 1097 * The hardware is idle and ready for more when the 1098 * chain_running list is empty. 1099 */ 1100 chan->idle = list_empty(&chan->chain_running); 1101 1102 /* Start any pending transactions automatically */ 1103 start_pending_queue(chan); 1104 spin_unlock_irqrestore(&chan->desc_lock, flags); 1105 1106 /* Run the callback for each descriptor, in order */ 1107 list_for_each_entry_safe(desc, _desc, &chain_cleanup, node) { 1108 struct dma_async_tx_descriptor *txd = &desc->async_tx; 1109 1110 /* Remove from the list of transactions */ 1111 list_del(&desc->node); 1112 /* Run the link descriptor callback function */ 1113 dmaengine_desc_get_callback(txd, &cb); 1114 dmaengine_desc_callback_invoke(&cb, NULL); 1115 1116 dma_pool_free(chan->desc_pool, desc, txd->phys); 1117 } 1118 } 1119 1120 static void mmp_pdma_remove(struct platform_device *op) 1121 { 1122 struct mmp_pdma_device *pdev = platform_get_drvdata(op); 1123 struct mmp_pdma_phy *phy; 1124 int i, irq = 0, irq_num = 0; 1125 1126 if (op->dev.of_node) 1127 of_dma_controller_free(op->dev.of_node); 1128 1129 for (i = 0; i < pdev->dma_channels; i++) { 1130 if (platform_get_irq(op, i) > 0) 1131 irq_num++; 1132 } 1133 1134 if (irq_num != pdev->dma_channels) { 1135 irq = platform_get_irq(op, 0); 1136 devm_free_irq(&op->dev, irq, pdev); 1137 } else { 1138 for (i = 0; i < pdev->dma_channels; i++) { 1139 phy = &pdev->phy[i]; 1140 irq = platform_get_irq(op, i); 1141 devm_free_irq(&op->dev, irq, phy); 1142 } 1143 } 1144 1145 dma_async_device_unregister(&pdev->device); 1146 } 1147 1148 static int mmp_pdma_chan_init(struct mmp_pdma_device *pdev, int idx, int irq) 1149 { 1150 struct mmp_pdma_phy *phy = &pdev->phy[idx]; 1151 struct mmp_pdma_chan *chan; 1152 int ret; 1153 1154 chan = devm_kzalloc(pdev->dev, sizeof(*chan), GFP_KERNEL); 1155 if (chan == NULL) 1156 return -ENOMEM; 1157 1158 phy->idx = idx; 1159 phy->base = pdev->base; 1160 1161 if (irq) { 1162 ret = devm_request_irq(pdev->dev, irq, mmp_pdma_chan_handler, 1163 IRQF_SHARED, "pdma", phy); 1164 if (ret) { 1165 dev_err(pdev->dev, "channel request irq fail!\n"); 1166 return ret; 1167 } 1168 } 1169 1170 spin_lock_init(&chan->desc_lock); 1171 chan->dev = pdev->dev; 1172 chan->chan.device = &pdev->device; 1173 tasklet_setup(&chan->tasklet, dma_do_tasklet); 1174 INIT_LIST_HEAD(&chan->chain_pending); 1175 INIT_LIST_HEAD(&chan->chain_running); 1176 1177 /* register virt channel to dma engine */ 1178 list_add_tail(&chan->chan.device_node, &pdev->device.channels); 1179 1180 return 0; 1181 } 1182 1183 static const struct mmp_pdma_ops marvell_pdma_v1_ops = { 1184 .write_next_addr = write_next_addr_32, 1185 .read_src_addr = read_src_addr_32, 1186 .read_dst_addr = read_dst_addr_32, 1187 .set_desc_next_addr = set_desc_next_addr_32, 1188 .set_desc_src_addr = set_desc_src_addr_32, 1189 .set_desc_dst_addr = set_desc_dst_addr_32, 1190 .get_desc_src_addr = get_desc_src_addr_32, 1191 .get_desc_dst_addr = get_desc_dst_addr_32, 1192 .run_bits = (DCSR_RUN), 1193 .dma_width = 32, 1194 .drcmr_ext_base = DRCMR_EXT_BASE_DEFAULT, 1195 }; 1196 1197 static const struct mmp_pdma_ops spacemit_k1_pdma_ops = { 1198 .write_next_addr = write_next_addr_64, 1199 .read_src_addr = read_src_addr_64, 1200 .read_dst_addr = read_dst_addr_64, 1201 .set_desc_next_addr = set_desc_next_addr_64, 1202 .set_desc_src_addr = set_desc_src_addr_64, 1203 .set_desc_dst_addr = set_desc_dst_addr_64, 1204 .get_desc_src_addr = get_desc_src_addr_64, 1205 .get_desc_dst_addr = get_desc_dst_addr_64, 1206 .run_bits = (DCSR_RUN | DCSR_LPAEEN), 1207 .dma_width = 64, 1208 .drcmr_ext_base = DRCMR_EXT_BASE_DEFAULT, 1209 }; 1210 1211 static const struct mmp_pdma_ops spacemit_k3_pdma_ops = { 1212 .write_next_addr = write_next_addr_64, 1213 .read_src_addr = read_src_addr_64, 1214 .read_dst_addr = read_dst_addr_64, 1215 .set_desc_next_addr = set_desc_next_addr_64, 1216 .set_desc_src_addr = set_desc_src_addr_64, 1217 .set_desc_dst_addr = set_desc_dst_addr_64, 1218 .get_desc_src_addr = get_desc_src_addr_64, 1219 .get_desc_dst_addr = get_desc_dst_addr_64, 1220 .run_bits = (DCSR_RUN | DCSR_LPAEEN | DCSR_EORIRQEN | DCSR_EORSTOPEN), 1221 .dma_width = 64, 1222 .drcmr_ext_base = DRCMR_EXT_BASE_K3, 1223 }; 1224 1225 static const struct of_device_id mmp_pdma_dt_ids[] = { 1226 { 1227 .compatible = "marvell,pdma-1.0", 1228 .data = &marvell_pdma_v1_ops 1229 }, { 1230 .compatible = "spacemit,k1-pdma", 1231 .data = &spacemit_k1_pdma_ops 1232 }, { 1233 .compatible = "spacemit,k3-pdma", 1234 .data = &spacemit_k3_pdma_ops 1235 }, { 1236 /* sentinel */ 1237 } 1238 }; 1239 MODULE_DEVICE_TABLE(of, mmp_pdma_dt_ids); 1240 1241 static struct dma_chan *mmp_pdma_dma_xlate(struct of_phandle_args *dma_spec, 1242 struct of_dma *ofdma) 1243 { 1244 struct mmp_pdma_device *d = ofdma->of_dma_data; 1245 struct dma_chan *chan; 1246 1247 chan = dma_get_any_slave_channel(&d->device); 1248 if (!chan) 1249 return NULL; 1250 1251 to_mmp_pdma_chan(chan)->drcmr = dma_spec->args[0]; 1252 1253 return chan; 1254 } 1255 1256 static int mmp_pdma_probe(struct platform_device *op) 1257 { 1258 struct mmp_pdma_device *pdev; 1259 struct mmp_dma_platdata *pdata = dev_get_platdata(&op->dev); 1260 struct clk *clk; 1261 struct reset_control *rst; 1262 int i, ret, irq = 0; 1263 int dma_channels = 0, irq_num = 0; 1264 const enum dma_slave_buswidth widths = 1265 DMA_SLAVE_BUSWIDTH_1_BYTE | DMA_SLAVE_BUSWIDTH_2_BYTES | 1266 DMA_SLAVE_BUSWIDTH_4_BYTES; 1267 1268 pdev = devm_kzalloc(&op->dev, sizeof(*pdev), GFP_KERNEL); 1269 if (!pdev) 1270 return -ENOMEM; 1271 1272 pdev->dev = &op->dev; 1273 1274 spin_lock_init(&pdev->phy_lock); 1275 1276 pdev->base = devm_platform_ioremap_resource(op, 0); 1277 if (IS_ERR(pdev->base)) 1278 return PTR_ERR(pdev->base); 1279 1280 clk = devm_clk_get_optional_enabled(pdev->dev, NULL); 1281 if (IS_ERR(clk)) 1282 return PTR_ERR(clk); 1283 1284 rst = devm_reset_control_get_optional_exclusive_deasserted(pdev->dev, 1285 NULL); 1286 if (IS_ERR(rst)) 1287 return PTR_ERR(rst); 1288 1289 pdev->ops = of_device_get_match_data(&op->dev); 1290 if (!pdev->ops) 1291 return -ENODEV; 1292 1293 if (pdev->dev->of_node) { 1294 /* Parse new and deprecated dma-channels properties */ 1295 if (of_property_read_u32(pdev->dev->of_node, "dma-channels", 1296 &dma_channels)) 1297 of_property_read_u32(pdev->dev->of_node, "#dma-channels", 1298 &dma_channels); 1299 } else if (pdata && pdata->dma_channels) { 1300 dma_channels = pdata->dma_channels; 1301 } else { 1302 dma_channels = 32; /* default 32 channel */ 1303 } 1304 pdev->dma_channels = dma_channels; 1305 1306 for (i = 0; i < dma_channels; i++) { 1307 if (platform_get_irq_optional(op, i) > 0) 1308 irq_num++; 1309 } 1310 1311 pdev->phy = devm_kcalloc(pdev->dev, dma_channels, sizeof(*pdev->phy), 1312 GFP_KERNEL); 1313 if (pdev->phy == NULL) 1314 return -ENOMEM; 1315 1316 INIT_LIST_HEAD(&pdev->device.channels); 1317 1318 if (irq_num != dma_channels) { 1319 /* all chan share one irq, demux inside */ 1320 irq = platform_get_irq(op, 0); 1321 ret = devm_request_irq(pdev->dev, irq, mmp_pdma_int_handler, 1322 IRQF_SHARED, "pdma", pdev); 1323 if (ret) 1324 return ret; 1325 } 1326 1327 for (i = 0; i < dma_channels; i++) { 1328 irq = (irq_num != dma_channels) ? 0 : platform_get_irq(op, i); 1329 ret = mmp_pdma_chan_init(pdev, i, irq); 1330 if (ret) 1331 return ret; 1332 } 1333 1334 dma_cap_set(DMA_SLAVE, pdev->device.cap_mask); 1335 dma_cap_set(DMA_MEMCPY, pdev->device.cap_mask); 1336 dma_cap_set(DMA_CYCLIC, pdev->device.cap_mask); 1337 dma_cap_set(DMA_PRIVATE, pdev->device.cap_mask); 1338 pdev->device.dev = &op->dev; 1339 pdev->device.device_alloc_chan_resources = mmp_pdma_alloc_chan_resources; 1340 pdev->device.device_free_chan_resources = mmp_pdma_free_chan_resources; 1341 pdev->device.device_tx_status = mmp_pdma_tx_status; 1342 pdev->device.device_prep_dma_memcpy = mmp_pdma_prep_memcpy; 1343 pdev->device.device_prep_slave_sg = mmp_pdma_prep_slave_sg; 1344 pdev->device.device_prep_dma_cyclic = mmp_pdma_prep_dma_cyclic; 1345 pdev->device.device_issue_pending = mmp_pdma_issue_pending; 1346 pdev->device.device_config = mmp_pdma_config; 1347 pdev->device.device_terminate_all = mmp_pdma_terminate_all; 1348 pdev->device.copy_align = DMAENGINE_ALIGN_8_BYTES; 1349 pdev->device.src_addr_widths = widths; 1350 pdev->device.dst_addr_widths = widths; 1351 pdev->device.directions = BIT(DMA_MEM_TO_DEV) | BIT(DMA_DEV_TO_MEM); 1352 pdev->device.residue_granularity = DMA_RESIDUE_GRANULARITY_DESCRIPTOR; 1353 1354 /* Set DMA mask based on controller hardware capabilities */ 1355 dma_set_mask_and_coherent(pdev->dev, 1356 DMA_BIT_MASK(pdev->ops->dma_width)); 1357 1358 ret = dma_async_device_register(&pdev->device); 1359 if (ret) { 1360 dev_err(pdev->device.dev, "unable to register\n"); 1361 return ret; 1362 } 1363 1364 if (op->dev.of_node) { 1365 /* Device-tree DMA controller registration */ 1366 ret = of_dma_controller_register(op->dev.of_node, 1367 mmp_pdma_dma_xlate, pdev); 1368 if (ret < 0) { 1369 dev_err(&op->dev, "of_dma_controller_register failed\n"); 1370 dma_async_device_unregister(&pdev->device); 1371 return ret; 1372 } 1373 } 1374 1375 platform_set_drvdata(op, pdev); 1376 dev_info(pdev->device.dev, "initialized %d channels\n", dma_channels); 1377 return 0; 1378 } 1379 1380 static const struct platform_device_id mmp_pdma_id_table[] = { 1381 { "mmp-pdma", }, 1382 { }, 1383 }; 1384 1385 static struct platform_driver mmp_pdma_driver = { 1386 .driver = { 1387 .name = "mmp-pdma", 1388 .of_match_table = mmp_pdma_dt_ids, 1389 }, 1390 .id_table = mmp_pdma_id_table, 1391 .probe = mmp_pdma_probe, 1392 .remove = mmp_pdma_remove, 1393 }; 1394 1395 module_platform_driver(mmp_pdma_driver); 1396 1397 MODULE_DESCRIPTION("MARVELL MMP Peripheral DMA Driver"); 1398 MODULE_AUTHOR("Marvell International Ltd."); 1399 MODULE_LICENSE("GPL v2"); 1400