1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Driver for Audio DMA Controller (ADMAC) on t8103 (M1) and other Apple chips 4 * 5 * Copyright (C) The Asahi Linux Contributors 6 */ 7 8 #include <linux/bits.h> 9 #include <linux/bitfield.h> 10 #include <linux/device.h> 11 #include <linux/init.h> 12 #include <linux/module.h> 13 #include <linux/of.h> 14 #include <linux/of_dma.h> 15 #include <linux/platform_device.h> 16 #include <linux/reset.h> 17 #include <linux/spinlock.h> 18 #include <linux/interrupt.h> 19 20 #include "dmaengine.h" 21 22 #define NCHANNELS_MAX 64 23 #define IRQ_NOUTPUTS 4 24 25 /* 26 * For allocation purposes we split the cache 27 * memory into blocks of fixed size (given in bytes). 28 */ 29 #define SRAM_BLOCK 2048 30 31 #define RING_WRITE_SLOT GENMASK(1, 0) 32 #define RING_READ_SLOT GENMASK(5, 4) 33 #define RING_FULL BIT(9) 34 #define RING_EMPTY BIT(8) 35 #define RING_ERR BIT(10) 36 37 #define STATUS_DESC_DONE BIT(0) 38 #define STATUS_ERR BIT(6) 39 40 #define FLAG_DESC_NOTIFY BIT(16) 41 42 #define T8122_UNK_28_VAL 0x200000 43 44 #define REG_TX_START 0x0000 45 #define REG_TX_STOP 0x0004 46 #define REG_RX_START 0x0008 47 #define REG_RX_STOP 0x000c 48 #define REG_UNK_28 0x0028 49 #define REG_UNK_2C 0x002c 50 #define REG_IMPRINT 0x0090 51 #define REG_TX_SRAM_SIZE 0x0094 52 #define REG_RX_SRAM_SIZE 0x0098 53 54 #define REG_CHAN_CTL(ch) (0x8000 + (ch) * 0x200) 55 #define REG_CHAN_CTL_RST_RINGS BIT(0) 56 57 #define REG_DESC_RING(ch) (0x8070 + (ch) * 0x200) 58 #define REG_REPORT_RING(ch) (0x8074 + (ch) * 0x200) 59 60 #define REG_RESIDUE(ch) (0x8064 + (ch) * 0x200) 61 62 #define REG_BUS_WIDTH(ch) (0x8040 + (ch) * 0x200) 63 64 #define BUS_WIDTH_WORD_SIZE GENMASK(3, 0) 65 #define BUS_WIDTH_FRAME_SIZE GENMASK(7, 4) 66 #define BUS_WIDTH_8BIT 0x00 67 #define BUS_WIDTH_16BIT 0x01 68 #define BUS_WIDTH_32BIT 0x02 69 #define BUS_WIDTH_FRAME_2_WORDS 0x10 70 #define BUS_WIDTH_FRAME_4_WORDS 0x20 71 72 #define REG_CHAN_SRAM_CARVEOUT(ch) (0x8050 + (ch) * 0x200) 73 #define CHAN_SRAM_CARVEOUT_SIZE GENMASK(31, 16) 74 #define CHAN_SRAM_CARVEOUT_BASE GENMASK(15, 0) 75 76 #define REG_CHAN_FIFOCTL(ch) (0x8054 + (ch) * 0x200) 77 #define CHAN_FIFOCTL_LIMIT GENMASK(31, 16) 78 #define CHAN_FIFOCTL_THRESHOLD GENMASK(15, 0) 79 80 #define REG_DESC_WRITE(ch) (0x10000 + ((ch) / 2) * 0x4 + ((ch) & 1) * 0x4000) 81 #define REG_REPORT_READ(ch) (0x10100 + ((ch) / 2) * 0x4 + ((ch) & 1) * 0x4000) 82 83 #define REG_TX_INTSTATE(idx) (0x0030 + (idx) * 4) 84 #define REG_RX_INTSTATE(idx) (0x0040 + (idx) * 4) 85 #define REG_GLOBAL_INTSTATE(idx) (0x0050 + (idx) * 4) 86 #define REG_CHAN_INTSTATUS(ch, idx) (0x8010 + (ch) * 0x200 + (idx) * 4) 87 #define REG_CHAN_INTMASK(ch, idx) (0x8020 + (ch) * 0x200 + (idx) * 4) 88 89 struct admac_data; 90 struct admac_tx; 91 92 struct admac_hw { 93 bool set_unk28; 94 }; 95 96 struct admac_chan { 97 unsigned int no; 98 struct admac_data *host; 99 struct dma_chan chan; 100 struct tasklet_struct tasklet; 101 102 u32 carveout; 103 104 spinlock_t lock; 105 struct admac_tx *current_tx; 106 int nperiod_acks; 107 108 /* 109 * We maintain a 'submitted' and 'issued' list mainly for interface 110 * correctness. Typical use of the driver (per channel) will be 111 * prepping, submitting and issuing a single cyclic transaction which 112 * will stay current until terminate_all is called. 113 */ 114 struct list_head submitted; 115 struct list_head issued; 116 117 struct list_head to_free; 118 }; 119 120 struct admac_sram { 121 u32 size; 122 /* 123 * SRAM_CARVEOUT has 16-bit fields, so the SRAM cannot be larger than 124 * 64K and a 32-bit bitfield over 2K blocks covers it. 125 */ 126 u32 allocated; 127 }; 128 129 struct admac_data { 130 struct dma_device dma; 131 struct device *dev; 132 __iomem void *base; 133 struct reset_control *rstc; 134 135 struct mutex cache_alloc_lock; 136 struct admac_sram txcache, rxcache; 137 138 const struct admac_hw *hw; 139 int irq; 140 int irq_index; 141 int nchannels; 142 struct admac_chan channels[] __counted_by(nchannels); 143 }; 144 145 struct admac_tx { 146 struct dma_async_tx_descriptor tx; 147 bool cyclic; 148 dma_addr_t buf_addr; 149 dma_addr_t buf_end; 150 size_t buf_len; 151 size_t period_len; 152 153 size_t submitted_pos; 154 size_t reclaimed_pos; 155 156 struct list_head node; 157 }; 158 159 static int admac_alloc_sram_carveout(struct admac_data *ad, 160 enum dma_transfer_direction dir, 161 u32 *out) 162 { 163 struct admac_sram *sram; 164 int i, ret = 0, nblocks; 165 ad->txcache.size = readl_relaxed(ad->base + REG_TX_SRAM_SIZE); 166 ad->rxcache.size = readl_relaxed(ad->base + REG_RX_SRAM_SIZE); 167 168 if (dir == DMA_MEM_TO_DEV) 169 sram = &ad->txcache; 170 else 171 sram = &ad->rxcache; 172 173 mutex_lock(&ad->cache_alloc_lock); 174 175 nblocks = sram->size / SRAM_BLOCK; 176 for (i = 0; i < nblocks; i++) 177 if (!(sram->allocated & BIT(i))) 178 break; 179 180 if (i < nblocks) { 181 *out = FIELD_PREP(CHAN_SRAM_CARVEOUT_BASE, i * SRAM_BLOCK) | 182 FIELD_PREP(CHAN_SRAM_CARVEOUT_SIZE, SRAM_BLOCK); 183 sram->allocated |= BIT(i); 184 } else { 185 ret = -EBUSY; 186 } 187 188 mutex_unlock(&ad->cache_alloc_lock); 189 190 return ret; 191 } 192 193 static void admac_free_sram_carveout(struct admac_data *ad, 194 enum dma_transfer_direction dir, 195 u32 carveout) 196 { 197 struct admac_sram *sram; 198 u32 base = FIELD_GET(CHAN_SRAM_CARVEOUT_BASE, carveout); 199 int i; 200 201 if (dir == DMA_MEM_TO_DEV) 202 sram = &ad->txcache; 203 else 204 sram = &ad->rxcache; 205 206 if (WARN_ON(base >= sram->size)) 207 return; 208 209 mutex_lock(&ad->cache_alloc_lock); 210 i = base / SRAM_BLOCK; 211 sram->allocated &= ~BIT(i); 212 mutex_unlock(&ad->cache_alloc_lock); 213 } 214 215 static void admac_modify(struct admac_data *ad, int reg, u32 mask, u32 val) 216 { 217 void __iomem *addr = ad->base + reg; 218 u32 curr = readl_relaxed(addr); 219 220 writel_relaxed((curr & ~mask) | (val & mask), addr); 221 } 222 223 static struct admac_chan *to_admac_chan(struct dma_chan *chan) 224 { 225 return container_of(chan, struct admac_chan, chan); 226 } 227 228 static struct admac_tx *to_admac_tx(struct dma_async_tx_descriptor *tx) 229 { 230 return container_of(tx, struct admac_tx, tx); 231 } 232 233 static enum dma_transfer_direction admac_chan_direction(int channo) 234 { 235 /* Channel directions are hardwired */ 236 return (channo & 1) ? DMA_DEV_TO_MEM : DMA_MEM_TO_DEV; 237 } 238 239 static dma_cookie_t admac_tx_submit(struct dma_async_tx_descriptor *tx) 240 { 241 struct admac_tx *adtx = to_admac_tx(tx); 242 struct admac_chan *adchan = to_admac_chan(tx->chan); 243 unsigned long flags; 244 dma_cookie_t cookie; 245 246 spin_lock_irqsave(&adchan->lock, flags); 247 cookie = dma_cookie_assign(tx); 248 list_add_tail(&adtx->node, &adchan->submitted); 249 spin_unlock_irqrestore(&adchan->lock, flags); 250 251 return cookie; 252 } 253 254 static int admac_desc_free(struct dma_async_tx_descriptor *tx) 255 { 256 kfree(to_admac_tx(tx)); 257 258 return 0; 259 } 260 261 static struct dma_async_tx_descriptor *admac_prep_dma_cyclic( 262 struct dma_chan *chan, dma_addr_t buf_addr, size_t buf_len, 263 size_t period_len, enum dma_transfer_direction direction, 264 unsigned long flags) 265 { 266 struct admac_chan *adchan = container_of(chan, struct admac_chan, chan); 267 struct admac_tx *adtx; 268 269 if (direction != admac_chan_direction(adchan->no)) 270 return NULL; 271 272 adtx = kzalloc_obj(*adtx, GFP_NOWAIT); 273 if (!adtx) 274 return NULL; 275 276 adtx->cyclic = true; 277 278 adtx->buf_addr = buf_addr; 279 adtx->buf_len = buf_len; 280 adtx->buf_end = buf_addr + buf_len; 281 adtx->period_len = period_len; 282 283 adtx->submitted_pos = 0; 284 adtx->reclaimed_pos = 0; 285 286 dma_async_tx_descriptor_init(&adtx->tx, chan); 287 adtx->tx.tx_submit = admac_tx_submit; 288 adtx->tx.desc_free = admac_desc_free; 289 290 return &adtx->tx; 291 } 292 293 /* 294 * Write one hardware descriptor for a dmaengine cyclic transaction. 295 */ 296 static void admac_cyclic_write_one_desc(struct admac_data *ad, int channo, 297 struct admac_tx *tx) 298 { 299 dma_addr_t addr; 300 301 addr = tx->buf_addr + (tx->submitted_pos % tx->buf_len); 302 303 /* If happens means we have buggy code */ 304 WARN_ON_ONCE(addr + tx->period_len > tx->buf_end); 305 306 dev_dbg(ad->dev, "ch%d descriptor: addr=0x%pad len=0x%zx flags=0x%lx\n", 307 channo, &addr, tx->period_len, FLAG_DESC_NOTIFY); 308 309 writel_relaxed(lower_32_bits(addr), ad->base + REG_DESC_WRITE(channo)); 310 writel_relaxed(upper_32_bits(addr), ad->base + REG_DESC_WRITE(channo)); 311 writel_relaxed(tx->period_len, ad->base + REG_DESC_WRITE(channo)); 312 writel_relaxed(FLAG_DESC_NOTIFY, ad->base + REG_DESC_WRITE(channo)); 313 314 tx->submitted_pos += tx->period_len; 315 tx->submitted_pos %= 2 * tx->buf_len; 316 } 317 318 /* 319 * Write all the hardware descriptors for a dmaengine cyclic 320 * transaction there is space for. 321 */ 322 static void admac_cyclic_write_desc(struct admac_data *ad, int channo, 323 struct admac_tx *tx) 324 { 325 int i; 326 327 for (i = 0; i < 4; i++) { 328 if (readl_relaxed(ad->base + REG_DESC_RING(channo)) & RING_FULL) 329 break; 330 admac_cyclic_write_one_desc(ad, channo, tx); 331 } 332 } 333 334 static int admac_ring_noccupied_slots(int ringval) 335 { 336 int wrslot = FIELD_GET(RING_WRITE_SLOT, ringval); 337 int rdslot = FIELD_GET(RING_READ_SLOT, ringval); 338 339 if (wrslot != rdslot) { 340 return (wrslot + 4 - rdslot) % 4; 341 } else { 342 WARN_ON((ringval & (RING_FULL | RING_EMPTY)) == 0); 343 344 if (ringval & RING_FULL) 345 return 4; 346 else 347 return 0; 348 } 349 } 350 351 /* 352 * Read from hardware the residue of a cyclic dmaengine transaction. 353 */ 354 static u32 admac_cyclic_read_residue(struct admac_data *ad, int channo, 355 struct admac_tx *adtx) 356 { 357 u32 ring1, ring2; 358 u32 residue1, residue2; 359 int nreports; 360 size_t pos; 361 362 ring1 = readl_relaxed(ad->base + REG_REPORT_RING(channo)); 363 residue1 = readl_relaxed(ad->base + REG_RESIDUE(channo)); 364 ring2 = readl_relaxed(ad->base + REG_REPORT_RING(channo)); 365 residue2 = readl_relaxed(ad->base + REG_RESIDUE(channo)); 366 367 if (residue2 > residue1) { 368 /* 369 * Controller must have loaded next descriptor between 370 * the two residue reads 371 */ 372 nreports = admac_ring_noccupied_slots(ring1) + 1; 373 } else { 374 /* No descriptor load between the two reads, ring2 is safe to use */ 375 nreports = admac_ring_noccupied_slots(ring2); 376 } 377 378 pos = adtx->reclaimed_pos + adtx->period_len * (nreports + 1) - residue2; 379 380 return adtx->buf_len - pos % adtx->buf_len; 381 } 382 383 static enum dma_status admac_tx_status(struct dma_chan *chan, dma_cookie_t cookie, 384 struct dma_tx_state *txstate) 385 { 386 struct admac_chan *adchan = to_admac_chan(chan); 387 struct admac_data *ad = adchan->host; 388 struct admac_tx *adtx; 389 390 enum dma_status ret; 391 size_t residue; 392 unsigned long flags; 393 394 ret = dma_cookie_status(chan, cookie, txstate); 395 if (ret == DMA_COMPLETE || !txstate) 396 return ret; 397 398 spin_lock_irqsave(&adchan->lock, flags); 399 adtx = adchan->current_tx; 400 401 if (adtx && adtx->tx.cookie == cookie) { 402 ret = DMA_IN_PROGRESS; 403 residue = admac_cyclic_read_residue(ad, adchan->no, adtx); 404 } else { 405 ret = DMA_IN_PROGRESS; 406 residue = 0; 407 list_for_each_entry(adtx, &adchan->issued, node) { 408 if (adtx->tx.cookie == cookie) { 409 residue = adtx->buf_len; 410 break; 411 } 412 } 413 } 414 spin_unlock_irqrestore(&adchan->lock, flags); 415 416 dma_set_residue(txstate, residue); 417 return ret; 418 } 419 420 static void admac_start_chan(struct admac_chan *adchan) 421 { 422 struct admac_data *ad = adchan->host; 423 u32 startbit = 1 << (adchan->no / 2); 424 425 writel_relaxed(STATUS_DESC_DONE | STATUS_ERR, 426 ad->base + REG_CHAN_INTSTATUS(adchan->no, ad->irq_index)); 427 writel_relaxed(STATUS_DESC_DONE | STATUS_ERR, 428 ad->base + REG_CHAN_INTMASK(adchan->no, ad->irq_index)); 429 430 switch (admac_chan_direction(adchan->no)) { 431 case DMA_MEM_TO_DEV: 432 writel_relaxed(startbit, ad->base + REG_TX_START); 433 break; 434 case DMA_DEV_TO_MEM: 435 writel_relaxed(startbit, ad->base + REG_RX_START); 436 break; 437 default: 438 break; 439 } 440 dev_dbg(adchan->host->dev, "ch%d start\n", adchan->no); 441 } 442 443 static void admac_stop_chan(struct admac_chan *adchan) 444 { 445 struct admac_data *ad = adchan->host; 446 u32 stopbit = 1 << (adchan->no / 2); 447 448 switch (admac_chan_direction(adchan->no)) { 449 case DMA_MEM_TO_DEV: 450 writel_relaxed(stopbit, ad->base + REG_TX_STOP); 451 break; 452 case DMA_DEV_TO_MEM: 453 writel_relaxed(stopbit, ad->base + REG_RX_STOP); 454 break; 455 default: 456 break; 457 } 458 dev_dbg(adchan->host->dev, "ch%d stop\n", adchan->no); 459 } 460 461 static void admac_reset_rings(struct admac_chan *adchan) 462 { 463 struct admac_data *ad = adchan->host; 464 465 writel_relaxed(REG_CHAN_CTL_RST_RINGS, 466 ad->base + REG_CHAN_CTL(adchan->no)); 467 writel_relaxed(0, ad->base + REG_CHAN_CTL(adchan->no)); 468 } 469 470 static void admac_start_current_tx(struct admac_chan *adchan) 471 { 472 struct admac_data *ad = adchan->host; 473 int ch = adchan->no; 474 475 admac_reset_rings(adchan); 476 writel_relaxed(0, ad->base + REG_CHAN_CTL(ch)); 477 478 admac_cyclic_write_one_desc(ad, ch, adchan->current_tx); 479 admac_start_chan(adchan); 480 admac_cyclic_write_desc(ad, ch, adchan->current_tx); 481 } 482 483 static void admac_issue_pending(struct dma_chan *chan) 484 { 485 struct admac_chan *adchan = to_admac_chan(chan); 486 struct admac_tx *tx; 487 unsigned long flags; 488 489 spin_lock_irqsave(&adchan->lock, flags); 490 list_splice_tail_init(&adchan->submitted, &adchan->issued); 491 if (!list_empty(&adchan->issued) && !adchan->current_tx) { 492 tx = list_first_entry(&adchan->issued, struct admac_tx, node); 493 list_del(&tx->node); 494 495 adchan->current_tx = tx; 496 adchan->nperiod_acks = 0; 497 admac_start_current_tx(adchan); 498 } 499 spin_unlock_irqrestore(&adchan->lock, flags); 500 } 501 502 static int admac_pause(struct dma_chan *chan) 503 { 504 struct admac_chan *adchan = to_admac_chan(chan); 505 506 admac_stop_chan(adchan); 507 508 return 0; 509 } 510 511 static int admac_resume(struct dma_chan *chan) 512 { 513 struct admac_chan *adchan = to_admac_chan(chan); 514 515 admac_start_chan(adchan); 516 517 return 0; 518 } 519 520 static int admac_terminate_all(struct dma_chan *chan) 521 { 522 struct admac_chan *adchan = to_admac_chan(chan); 523 unsigned long flags; 524 525 spin_lock_irqsave(&adchan->lock, flags); 526 admac_stop_chan(adchan); 527 admac_reset_rings(adchan); 528 529 if (adchan->current_tx) { 530 list_add_tail(&adchan->current_tx->node, &adchan->to_free); 531 adchan->current_tx = NULL; 532 } 533 /* 534 * Descriptors can only be freed after the tasklet 535 * has been killed (in admac_synchronize). 536 */ 537 list_splice_tail_init(&adchan->submitted, &adchan->to_free); 538 list_splice_tail_init(&adchan->issued, &adchan->to_free); 539 spin_unlock_irqrestore(&adchan->lock, flags); 540 541 return 0; 542 } 543 544 static void admac_synchronize(struct dma_chan *chan) 545 { 546 struct admac_chan *adchan = to_admac_chan(chan); 547 struct admac_tx *adtx, *_adtx; 548 unsigned long flags; 549 LIST_HEAD(head); 550 551 spin_lock_irqsave(&adchan->lock, flags); 552 list_splice_tail_init(&adchan->to_free, &head); 553 spin_unlock_irqrestore(&adchan->lock, flags); 554 555 tasklet_kill(&adchan->tasklet); 556 557 list_for_each_entry_safe(adtx, _adtx, &head, node) { 558 list_del(&adtx->node); 559 admac_desc_free(&adtx->tx); 560 } 561 } 562 563 static int admac_alloc_chan_resources(struct dma_chan *chan) 564 { 565 struct admac_chan *adchan = to_admac_chan(chan); 566 struct admac_data *ad = adchan->host; 567 int ret; 568 569 dma_cookie_init(&adchan->chan); 570 ret = admac_alloc_sram_carveout(ad, admac_chan_direction(adchan->no), 571 &adchan->carveout); 572 if (ret < 0) 573 return ret; 574 575 writel_relaxed(adchan->carveout, 576 ad->base + REG_CHAN_SRAM_CARVEOUT(adchan->no)); 577 return 0; 578 } 579 580 static void admac_free_chan_resources(struct dma_chan *chan) 581 { 582 struct admac_chan *adchan = to_admac_chan(chan); 583 584 admac_terminate_all(chan); 585 admac_synchronize(chan); 586 admac_free_sram_carveout(adchan->host, admac_chan_direction(adchan->no), 587 adchan->carveout); 588 } 589 590 static struct dma_chan *admac_dma_of_xlate(struct of_phandle_args *dma_spec, 591 struct of_dma *ofdma) 592 { 593 struct admac_data *ad = (struct admac_data *) ofdma->of_dma_data; 594 unsigned int index; 595 596 if (dma_spec->args_count != 1) 597 return NULL; 598 599 index = dma_spec->args[0]; 600 601 if (index >= ad->nchannels) { 602 dev_err(ad->dev, "channel index %u out of bounds\n", index); 603 return NULL; 604 } 605 606 return dma_get_slave_channel(&ad->channels[index].chan); 607 } 608 609 static int admac_drain_reports(struct admac_data *ad, int channo) 610 { 611 int count; 612 613 for (count = 0; count < 4; count++) { 614 u32 countval_hi, countval_lo, unk1, flags; 615 616 if (readl_relaxed(ad->base + REG_REPORT_RING(channo)) & RING_EMPTY) 617 break; 618 619 countval_lo = readl_relaxed(ad->base + REG_REPORT_READ(channo)); 620 countval_hi = readl_relaxed(ad->base + REG_REPORT_READ(channo)); 621 unk1 = readl_relaxed(ad->base + REG_REPORT_READ(channo)); 622 flags = readl_relaxed(ad->base + REG_REPORT_READ(channo)); 623 624 dev_dbg(ad->dev, "ch%d report: countval=0x%llx unk1=0x%x flags=0x%x\n", 625 channo, ((u64) countval_hi) << 32 | countval_lo, unk1, flags); 626 } 627 628 return count; 629 } 630 631 static void admac_handle_status_err(struct admac_data *ad, int channo) 632 { 633 bool handled = false; 634 635 if (readl_relaxed(ad->base + REG_DESC_RING(channo)) & RING_ERR) { 636 writel_relaxed(RING_ERR, ad->base + REG_DESC_RING(channo)); 637 dev_err_ratelimited(ad->dev, "ch%d descriptor ring error\n", channo); 638 handled = true; 639 } 640 641 if (readl_relaxed(ad->base + REG_REPORT_RING(channo)) & RING_ERR) { 642 writel_relaxed(RING_ERR, ad->base + REG_REPORT_RING(channo)); 643 dev_err_ratelimited(ad->dev, "ch%d report ring error\n", channo); 644 handled = true; 645 } 646 647 if (unlikely(!handled)) { 648 dev_err(ad->dev, "ch%d unknown error, masking errors as cause of IRQs\n", channo); 649 admac_modify(ad, REG_CHAN_INTMASK(channo, ad->irq_index), 650 STATUS_ERR, 0); 651 } 652 } 653 654 static void admac_handle_status_desc_done(struct admac_data *ad, int channo) 655 { 656 struct admac_chan *adchan = &ad->channels[channo]; 657 unsigned long flags; 658 int nreports; 659 660 writel_relaxed(STATUS_DESC_DONE, 661 ad->base + REG_CHAN_INTSTATUS(channo, ad->irq_index)); 662 663 spin_lock_irqsave(&adchan->lock, flags); 664 nreports = admac_drain_reports(ad, channo); 665 666 if (adchan->current_tx) { 667 struct admac_tx *tx = adchan->current_tx; 668 669 adchan->nperiod_acks += nreports; 670 tx->reclaimed_pos += nreports * tx->period_len; 671 tx->reclaimed_pos %= 2 * tx->buf_len; 672 673 admac_cyclic_write_desc(ad, channo, tx); 674 tasklet_schedule(&adchan->tasklet); 675 } 676 spin_unlock_irqrestore(&adchan->lock, flags); 677 } 678 679 static void admac_handle_chan_int(struct admac_data *ad, int no) 680 { 681 u32 cause = readl_relaxed(ad->base + REG_CHAN_INTSTATUS(no, ad->irq_index)); 682 683 if (cause & STATUS_ERR) 684 admac_handle_status_err(ad, no); 685 686 if (cause & STATUS_DESC_DONE) 687 admac_handle_status_desc_done(ad, no); 688 } 689 690 static irqreturn_t admac_interrupt(int irq, void *devid) 691 { 692 struct admac_data *ad = devid; 693 u32 rx_intstate, tx_intstate, global_intstate; 694 int i; 695 696 rx_intstate = readl_relaxed(ad->base + REG_RX_INTSTATE(ad->irq_index)); 697 tx_intstate = readl_relaxed(ad->base + REG_TX_INTSTATE(ad->irq_index)); 698 global_intstate = readl_relaxed(ad->base + REG_GLOBAL_INTSTATE(ad->irq_index)); 699 700 if (!tx_intstate && !rx_intstate && !global_intstate) 701 return IRQ_NONE; 702 703 for (i = 0; i < ad->nchannels; i += 2) { 704 if (tx_intstate & 1) 705 admac_handle_chan_int(ad, i); 706 tx_intstate >>= 1; 707 } 708 709 for (i = 1; i < ad->nchannels; i += 2) { 710 if (rx_intstate & 1) 711 admac_handle_chan_int(ad, i); 712 rx_intstate >>= 1; 713 } 714 715 if (global_intstate) { 716 dev_warn(ad->dev, "clearing unknown global interrupt flag: %x\n", 717 global_intstate); 718 writel_relaxed(~(u32) 0, ad->base + REG_GLOBAL_INTSTATE(ad->irq_index)); 719 } 720 721 return IRQ_HANDLED; 722 } 723 724 static void admac_chan_tasklet(struct tasklet_struct *t) 725 { 726 struct admac_chan *adchan = from_tasklet(adchan, t, tasklet); 727 struct admac_tx *adtx; 728 struct dmaengine_desc_callback cb; 729 struct dmaengine_result tx_result; 730 int nacks; 731 732 spin_lock_irq(&adchan->lock); 733 adtx = adchan->current_tx; 734 nacks = adchan->nperiod_acks; 735 adchan->nperiod_acks = 0; 736 spin_unlock_irq(&adchan->lock); 737 738 if (!adtx || !nacks) 739 return; 740 741 tx_result.result = DMA_TRANS_NOERROR; 742 tx_result.residue = 0; 743 744 dmaengine_desc_get_callback(&adtx->tx, &cb); 745 while (nacks--) 746 dmaengine_desc_callback_invoke(&cb, &tx_result); 747 } 748 749 static int admac_device_config(struct dma_chan *chan, 750 struct dma_slave_config *config) 751 { 752 struct admac_chan *adchan = to_admac_chan(chan); 753 struct admac_data *ad = adchan->host; 754 bool is_tx = admac_chan_direction(adchan->no) == DMA_MEM_TO_DEV; 755 int wordsize = 0; 756 u32 bus_width = readl_relaxed(ad->base + REG_BUS_WIDTH(adchan->no)) & 757 ~(BUS_WIDTH_WORD_SIZE | BUS_WIDTH_FRAME_SIZE); 758 759 if (ad->hw->set_unk28) { 760 writel_relaxed(T8122_UNK_28_VAL, ad->base + REG_UNK_28); 761 writel_relaxed(T8122_UNK_28_VAL, ad->base + REG_UNK_2C); 762 } 763 764 switch (is_tx ? config->dst_addr_width : config->src_addr_width) { 765 case DMA_SLAVE_BUSWIDTH_1_BYTE: 766 wordsize = 1; 767 bus_width |= BUS_WIDTH_8BIT; 768 break; 769 case DMA_SLAVE_BUSWIDTH_2_BYTES: 770 wordsize = 2; 771 bus_width |= BUS_WIDTH_16BIT; 772 break; 773 case DMA_SLAVE_BUSWIDTH_4_BYTES: 774 wordsize = 4; 775 bus_width |= BUS_WIDTH_32BIT; 776 break; 777 default: 778 return -EINVAL; 779 } 780 781 /* 782 * We take port_window_size to be the number of words in a frame. 783 * 784 * The controller has some means of out-of-band signalling, to the peripheral, 785 * of words position in a frame. That's where the importance of this control 786 * comes from. 787 */ 788 switch (is_tx ? config->dst_port_window_size : config->src_port_window_size) { 789 case 0 ... 1: 790 break; 791 case 2: 792 bus_width |= BUS_WIDTH_FRAME_2_WORDS; 793 break; 794 case 4: 795 bus_width |= BUS_WIDTH_FRAME_4_WORDS; 796 break; 797 default: 798 return -EINVAL; 799 } 800 801 writel_relaxed(bus_width, ad->base + REG_BUS_WIDTH(adchan->no)); 802 803 /* 804 * By FIFOCTL_LIMIT we seem to set the maximal number of bytes allowed to be 805 * held in controller's per-channel FIFO. Transfers seem to be triggered 806 * around the time FIFO occupancy touches FIFOCTL_THRESHOLD. 807 * 808 * The numbers we set are more or less arbitrary. 809 */ 810 writel_relaxed(FIELD_PREP(CHAN_FIFOCTL_LIMIT, 0x30 * wordsize) 811 | FIELD_PREP(CHAN_FIFOCTL_THRESHOLD, 0x18 * wordsize), 812 ad->base + REG_CHAN_FIFOCTL(adchan->no)); 813 814 return 0; 815 } 816 817 static const struct admac_hw admac_base_hw = {}; 818 819 static const struct admac_hw admac_t8122_hw = { 820 .set_unk28 = true, 821 }; 822 823 static int admac_probe(struct platform_device *pdev) 824 { 825 struct device_node *np = pdev->dev.of_node; 826 struct admac_data *ad; 827 struct dma_device *dma; 828 const struct admac_hw *hw; 829 int nchannels; 830 int err, irq, i; 831 832 err = of_property_read_u32(np, "dma-channels", &nchannels); 833 if (err || nchannels > NCHANNELS_MAX) { 834 dev_err(&pdev->dev, "missing or invalid dma-channels property\n"); 835 return -EINVAL; 836 } 837 838 ad = devm_kzalloc(&pdev->dev, struct_size(ad, channels, nchannels), GFP_KERNEL); 839 if (!ad) 840 return -ENOMEM; 841 842 platform_set_drvdata(pdev, ad); 843 ad->dev = &pdev->dev; 844 ad->nchannels = nchannels; 845 mutex_init(&ad->cache_alloc_lock); 846 hw = of_device_get_match_data(&pdev->dev); 847 ad->hw = hw ? hw : &admac_base_hw; 848 849 /* 850 * The controller has 4 IRQ outputs. Try them all until 851 * we find one we can use. 852 */ 853 for (i = 0; i < IRQ_NOUTPUTS; i++) { 854 irq = platform_get_irq_optional(pdev, i); 855 if (irq >= 0) { 856 ad->irq_index = i; 857 break; 858 } 859 } 860 861 if (irq < 0) 862 return dev_err_probe(&pdev->dev, irq, "no usable interrupt\n"); 863 ad->irq = irq; 864 865 ad->base = devm_platform_ioremap_resource(pdev, 0); 866 if (IS_ERR(ad->base)) 867 return dev_err_probe(&pdev->dev, PTR_ERR(ad->base), 868 "unable to obtain MMIO resource\n"); 869 870 ad->rstc = devm_reset_control_get_optional_shared(&pdev->dev, NULL); 871 if (IS_ERR(ad->rstc)) 872 return PTR_ERR(ad->rstc); 873 874 dma = &ad->dma; 875 876 dma_cap_set(DMA_PRIVATE, dma->cap_mask); 877 dma_cap_set(DMA_CYCLIC, dma->cap_mask); 878 879 dma->dev = &pdev->dev; 880 dma->device_alloc_chan_resources = admac_alloc_chan_resources; 881 dma->device_free_chan_resources = admac_free_chan_resources; 882 dma->device_tx_status = admac_tx_status; 883 dma->device_issue_pending = admac_issue_pending; 884 dma->device_terminate_all = admac_terminate_all; 885 dma->device_synchronize = admac_synchronize; 886 dma->device_prep_dma_cyclic = admac_prep_dma_cyclic; 887 dma->device_config = admac_device_config; 888 dma->device_pause = admac_pause; 889 dma->device_resume = admac_resume; 890 891 dma->directions = BIT(DMA_MEM_TO_DEV) | BIT(DMA_DEV_TO_MEM); 892 dma->residue_granularity = DMA_RESIDUE_GRANULARITY_BURST; 893 dma->src_addr_widths = BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) | 894 BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) | 895 BIT(DMA_SLAVE_BUSWIDTH_4_BYTES); 896 dma->dst_addr_widths = BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) | 897 BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) | 898 BIT(DMA_SLAVE_BUSWIDTH_4_BYTES); 899 900 INIT_LIST_HEAD(&dma->channels); 901 for (i = 0; i < nchannels; i++) { 902 struct admac_chan *adchan = &ad->channels[i]; 903 904 adchan->host = ad; 905 adchan->no = i; 906 adchan->chan.device = &ad->dma; 907 spin_lock_init(&adchan->lock); 908 INIT_LIST_HEAD(&adchan->submitted); 909 INIT_LIST_HEAD(&adchan->issued); 910 INIT_LIST_HEAD(&adchan->to_free); 911 list_add_tail(&adchan->chan.device_node, &dma->channels); 912 tasklet_setup(&adchan->tasklet, admac_chan_tasklet); 913 } 914 915 err = reset_control_reset(ad->rstc); 916 if (err) 917 return dev_err_probe(&pdev->dev, err, 918 "unable to trigger reset\n"); 919 920 err = request_irq(irq, admac_interrupt, 0, dev_name(&pdev->dev), ad); 921 if (err) { 922 dev_err_probe(&pdev->dev, err, 923 "unable to register interrupt\n"); 924 goto free_reset; 925 } 926 927 err = dma_async_device_register(&ad->dma); 928 if (err) { 929 dev_err_probe(&pdev->dev, err, "failed to register DMA device\n"); 930 goto free_irq; 931 } 932 933 err = of_dma_controller_register(pdev->dev.of_node, admac_dma_of_xlate, ad); 934 if (err) { 935 dma_async_device_unregister(&ad->dma); 936 dev_err_probe(&pdev->dev, err, "failed to register with OF\n"); 937 goto free_irq; 938 } 939 940 dev_info(&pdev->dev, "Audio DMA Controller\n"); 941 942 return 0; 943 944 free_irq: 945 free_irq(ad->irq, ad); 946 free_reset: 947 reset_control_rearm(ad->rstc); 948 return err; 949 } 950 951 static void admac_remove(struct platform_device *pdev) 952 { 953 struct admac_data *ad = platform_get_drvdata(pdev); 954 955 of_dma_controller_free(pdev->dev.of_node); 956 dma_async_device_unregister(&ad->dma); 957 free_irq(ad->irq, ad); 958 reset_control_rearm(ad->rstc); 959 } 960 961 static const struct of_device_id admac_of_match[] = { 962 { .compatible = "apple,t8122-admac", .data = &admac_t8122_hw }, 963 { .compatible = "apple,t8103-admac", }, 964 { .compatible = "apple,admac", }, 965 { } 966 }; 967 MODULE_DEVICE_TABLE(of, admac_of_match); 968 969 static struct platform_driver apple_admac_driver = { 970 .driver = { 971 .name = "apple-admac", 972 .of_match_table = admac_of_match, 973 }, 974 .probe = admac_probe, 975 .remove = admac_remove, 976 }; 977 module_platform_driver(apple_admac_driver); 978 979 MODULE_AUTHOR("Martin Povišer <povik+lin@cutebit.org>"); 980 MODULE_DESCRIPTION("Driver for Audio DMA Controller (ADMAC) on Apple SoCs"); 981 MODULE_LICENSE("GPL"); 982