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