1 // SPDX-License-Identifier: GPL-2.0-only
3 * DMA driver for Nvidia's Tegra20 APB DMA controller.
5 * Copyright (c) 2012-2013, NVIDIA CORPORATION. All rights reserved.
8 #include <linux/bitops.h>
10 #include <linux/delay.h>
11 #include <linux/dmaengine.h>
12 #include <linux/dma-mapping.h>
13 #include <linux/err.h>
14 #include <linux/init.h>
15 #include <linux/interrupt.h>
18 #include <linux/module.h>
20 #include <linux/of_device.h>
21 #include <linux/of_dma.h>
22 #include <linux/platform_device.h>
24 #include <linux/pm_runtime.h>
25 #include <linux/reset.h>
26 #include <linux/slab.h>
27 #include <linux/wait.h>
29 #include "dmaengine.h"
31 #define CREATE_TRACE_POINTS
32 #include <trace/events/tegra_apb_dma.h>
34 #define TEGRA_APBDMA_GENERAL 0x0
35 #define TEGRA_APBDMA_GENERAL_ENABLE BIT(31)
37 #define TEGRA_APBDMA_CONTROL 0x010
38 #define TEGRA_APBDMA_IRQ_MASK 0x01c
39 #define TEGRA_APBDMA_IRQ_MASK_SET 0x020
42 #define TEGRA_APBDMA_CHAN_CSR 0x00
43 #define TEGRA_APBDMA_CSR_ENB BIT(31)
44 #define TEGRA_APBDMA_CSR_IE_EOC BIT(30)
45 #define TEGRA_APBDMA_CSR_HOLD BIT(29)
46 #define TEGRA_APBDMA_CSR_DIR BIT(28)
47 #define TEGRA_APBDMA_CSR_ONCE BIT(27)
48 #define TEGRA_APBDMA_CSR_FLOW BIT(21)
49 #define TEGRA_APBDMA_CSR_REQ_SEL_SHIFT 16
50 #define TEGRA_APBDMA_CSR_REQ_SEL_MASK 0x1F
51 #define TEGRA_APBDMA_CSR_WCOUNT_MASK 0xFFFC
54 #define TEGRA_APBDMA_CHAN_STATUS 0x004
55 #define TEGRA_APBDMA_STATUS_BUSY BIT(31)
56 #define TEGRA_APBDMA_STATUS_ISE_EOC BIT(30)
57 #define TEGRA_APBDMA_STATUS_HALT BIT(29)
58 #define TEGRA_APBDMA_STATUS_PING_PONG BIT(28)
59 #define TEGRA_APBDMA_STATUS_COUNT_SHIFT 2
60 #define TEGRA_APBDMA_STATUS_COUNT_MASK 0xFFFC
62 #define TEGRA_APBDMA_CHAN_CSRE 0x00C
63 #define TEGRA_APBDMA_CHAN_CSRE_PAUSE BIT(31)
65 /* AHB memory address */
66 #define TEGRA_APBDMA_CHAN_AHBPTR 0x010
68 /* AHB sequence register */
69 #define TEGRA_APBDMA_CHAN_AHBSEQ 0x14
70 #define TEGRA_APBDMA_AHBSEQ_INTR_ENB BIT(31)
71 #define TEGRA_APBDMA_AHBSEQ_BUS_WIDTH_8 (0 << 28)
72 #define TEGRA_APBDMA_AHBSEQ_BUS_WIDTH_16 (1 << 28)
73 #define TEGRA_APBDMA_AHBSEQ_BUS_WIDTH_32 (2 << 28)
74 #define TEGRA_APBDMA_AHBSEQ_BUS_WIDTH_64 (3 << 28)
75 #define TEGRA_APBDMA_AHBSEQ_BUS_WIDTH_128 (4 << 28)
76 #define TEGRA_APBDMA_AHBSEQ_DATA_SWAP BIT(27)
77 #define TEGRA_APBDMA_AHBSEQ_BURST_1 (4 << 24)
78 #define TEGRA_APBDMA_AHBSEQ_BURST_4 (5 << 24)
79 #define TEGRA_APBDMA_AHBSEQ_BURST_8 (6 << 24)
80 #define TEGRA_APBDMA_AHBSEQ_DBL_BUF BIT(19)
81 #define TEGRA_APBDMA_AHBSEQ_WRAP_SHIFT 16
82 #define TEGRA_APBDMA_AHBSEQ_WRAP_NONE 0
85 #define TEGRA_APBDMA_CHAN_APBPTR 0x018
87 /* APB sequence register */
88 #define TEGRA_APBDMA_CHAN_APBSEQ 0x01c
89 #define TEGRA_APBDMA_APBSEQ_BUS_WIDTH_8 (0 << 28)
90 #define TEGRA_APBDMA_APBSEQ_BUS_WIDTH_16 (1 << 28)
91 #define TEGRA_APBDMA_APBSEQ_BUS_WIDTH_32 (2 << 28)
92 #define TEGRA_APBDMA_APBSEQ_BUS_WIDTH_64 (3 << 28)
93 #define TEGRA_APBDMA_APBSEQ_BUS_WIDTH_128 (4 << 28)
94 #define TEGRA_APBDMA_APBSEQ_DATA_SWAP BIT(27)
95 #define TEGRA_APBDMA_APBSEQ_WRAP_WORD_1 (1 << 16)
97 /* Tegra148 specific registers */
98 #define TEGRA_APBDMA_CHAN_WCOUNT 0x20
100 #define TEGRA_APBDMA_CHAN_WORD_TRANSFER 0x24
103 * If any burst is in flight and DMA paused then this is the time to complete
104 * on-flight burst and update DMA status register.
106 #define TEGRA_APBDMA_BURST_COMPLETE_TIME 20
108 /* Channel base address offset from APBDMA base address */
109 #define TEGRA_APBDMA_CHANNEL_BASE_ADD_OFFSET 0x1000
111 #define TEGRA_APBDMA_SLAVE_ID_INVALID (TEGRA_APBDMA_CSR_REQ_SEL_MASK + 1)
116 * tegra_dma_chip_data Tegra chip specific DMA data
117 * @nr_channels: Number of channels available in the controller.
118 * @channel_reg_size: Channel register size/stride.
119 * @max_dma_count: Maximum DMA transfer count supported by DMA controller.
120 * @support_channel_pause: Support channel wise pause of dma.
121 * @support_separate_wcount_reg: Support separate word count register.
123 struct tegra_dma_chip_data {
124 unsigned int nr_channels;
125 unsigned int channel_reg_size;
126 unsigned int max_dma_count;
127 bool support_channel_pause;
128 bool support_separate_wcount_reg;
131 /* DMA channel registers */
132 struct tegra_dma_channel_regs {
142 * tegra_dma_sg_req: DMA request details to configure hardware. This
143 * contains the details for one transfer to configure DMA hw.
144 * The client's request for data transfer can be broken into multiple
145 * sub-transfer as per requester details and hw support.
146 * This sub transfer get added in the list of transfer and point to Tegra
147 * DMA descriptor which manages the transfer details.
149 struct tegra_dma_sg_req {
150 struct tegra_dma_channel_regs ch_regs;
151 unsigned int req_len;
154 struct list_head node;
155 struct tegra_dma_desc *dma_desc;
156 unsigned int words_xferred;
160 * tegra_dma_desc: Tegra DMA descriptors which manages the client requests.
161 * This descriptor keep track of transfer status, callbacks and request
164 struct tegra_dma_desc {
165 struct dma_async_tx_descriptor txd;
166 unsigned int bytes_requested;
167 unsigned int bytes_transferred;
168 enum dma_status dma_status;
169 struct list_head node;
170 struct list_head tx_list;
171 struct list_head cb_node;
172 unsigned int cb_count;
175 struct tegra_dma_channel;
177 typedef void (*dma_isr_handler)(struct tegra_dma_channel *tdc,
180 /* tegra_dma_channel: Channel specific information */
181 struct tegra_dma_channel {
182 struct dma_chan dma_chan;
186 void __iomem *chan_addr;
189 struct tegra_dma *tdma;
192 /* Different lists for managing the requests */
193 struct list_head free_sg_req;
194 struct list_head pending_sg_req;
195 struct list_head free_dma_desc;
196 struct list_head cb_desc;
198 /* ISR handler and tasklet for bottom half of isr handling */
199 dma_isr_handler isr_handler;
200 struct tasklet_struct tasklet;
202 /* Channel-slave specific configuration */
203 unsigned int slave_id;
204 struct dma_slave_config dma_sconfig;
205 struct tegra_dma_channel_regs channel_reg;
207 struct wait_queue_head wq;
210 /* tegra_dma: Tegra DMA specific information */
212 struct dma_device dma_dev;
215 struct reset_control *rst;
216 spinlock_t global_lock;
217 void __iomem *base_addr;
218 const struct tegra_dma_chip_data *chip_data;
221 * Counter for managing global pausing of the DMA controller.
222 * Only applicable for devices that don't support individual
225 u32 global_pause_count;
227 /* Last member of the structure */
228 struct tegra_dma_channel channels[];
231 static inline void tdma_write(struct tegra_dma *tdma, u32 reg, u32 val)
233 writel(val, tdma->base_addr + reg);
236 static inline void tdc_write(struct tegra_dma_channel *tdc,
239 writel(val, tdc->chan_addr + reg);
242 static inline u32 tdc_read(struct tegra_dma_channel *tdc, u32 reg)
244 return readl(tdc->chan_addr + reg);
247 static inline struct tegra_dma_channel *to_tegra_dma_chan(struct dma_chan *dc)
249 return container_of(dc, struct tegra_dma_channel, dma_chan);
252 static inline struct tegra_dma_desc *
253 txd_to_tegra_dma_desc(struct dma_async_tx_descriptor *td)
255 return container_of(td, struct tegra_dma_desc, txd);
258 static inline struct device *tdc2dev(struct tegra_dma_channel *tdc)
260 return &tdc->dma_chan.dev->device;
263 static dma_cookie_t tegra_dma_tx_submit(struct dma_async_tx_descriptor *tx);
265 /* Get DMA desc from free list, if not there then allocate it. */
266 static struct tegra_dma_desc *tegra_dma_desc_get(struct tegra_dma_channel *tdc)
268 struct tegra_dma_desc *dma_desc;
271 spin_lock_irqsave(&tdc->lock, flags);
273 /* Do not allocate if desc are waiting for ack */
274 list_for_each_entry(dma_desc, &tdc->free_dma_desc, node) {
275 if (async_tx_test_ack(&dma_desc->txd) && !dma_desc->cb_count) {
276 list_del(&dma_desc->node);
277 spin_unlock_irqrestore(&tdc->lock, flags);
278 dma_desc->txd.flags = 0;
283 spin_unlock_irqrestore(&tdc->lock, flags);
285 /* Allocate DMA desc */
286 dma_desc = kzalloc(sizeof(*dma_desc), GFP_NOWAIT);
290 dma_async_tx_descriptor_init(&dma_desc->txd, &tdc->dma_chan);
291 dma_desc->txd.tx_submit = tegra_dma_tx_submit;
292 dma_desc->txd.flags = 0;
297 static void tegra_dma_desc_put(struct tegra_dma_channel *tdc,
298 struct tegra_dma_desc *dma_desc)
302 spin_lock_irqsave(&tdc->lock, flags);
303 if (!list_empty(&dma_desc->tx_list))
304 list_splice_init(&dma_desc->tx_list, &tdc->free_sg_req);
305 list_add_tail(&dma_desc->node, &tdc->free_dma_desc);
306 spin_unlock_irqrestore(&tdc->lock, flags);
309 static struct tegra_dma_sg_req *
310 tegra_dma_sg_req_get(struct tegra_dma_channel *tdc)
312 struct tegra_dma_sg_req *sg_req;
315 spin_lock_irqsave(&tdc->lock, flags);
316 if (!list_empty(&tdc->free_sg_req)) {
317 sg_req = list_first_entry(&tdc->free_sg_req, typeof(*sg_req),
319 list_del(&sg_req->node);
320 spin_unlock_irqrestore(&tdc->lock, flags);
323 spin_unlock_irqrestore(&tdc->lock, flags);
325 sg_req = kzalloc(sizeof(*sg_req), GFP_NOWAIT);
330 static int tegra_dma_slave_config(struct dma_chan *dc,
331 struct dma_slave_config *sconfig)
333 struct tegra_dma_channel *tdc = to_tegra_dma_chan(dc);
335 if (!list_empty(&tdc->pending_sg_req)) {
336 dev_err(tdc2dev(tdc), "Configuration not allowed\n");
340 memcpy(&tdc->dma_sconfig, sconfig, sizeof(*sconfig));
341 tdc->config_init = true;
346 static void tegra_dma_global_pause(struct tegra_dma_channel *tdc,
347 bool wait_for_burst_complete)
349 struct tegra_dma *tdma = tdc->tdma;
351 spin_lock(&tdma->global_lock);
353 if (tdc->tdma->global_pause_count == 0) {
354 tdma_write(tdma, TEGRA_APBDMA_GENERAL, 0);
355 if (wait_for_burst_complete)
356 udelay(TEGRA_APBDMA_BURST_COMPLETE_TIME);
359 tdc->tdma->global_pause_count++;
361 spin_unlock(&tdma->global_lock);
364 static void tegra_dma_global_resume(struct tegra_dma_channel *tdc)
366 struct tegra_dma *tdma = tdc->tdma;
368 spin_lock(&tdma->global_lock);
370 if (WARN_ON(tdc->tdma->global_pause_count == 0))
373 if (--tdc->tdma->global_pause_count == 0)
374 tdma_write(tdma, TEGRA_APBDMA_GENERAL,
375 TEGRA_APBDMA_GENERAL_ENABLE);
378 spin_unlock(&tdma->global_lock);
381 static void tegra_dma_pause(struct tegra_dma_channel *tdc,
382 bool wait_for_burst_complete)
384 struct tegra_dma *tdma = tdc->tdma;
386 if (tdma->chip_data->support_channel_pause) {
387 tdc_write(tdc, TEGRA_APBDMA_CHAN_CSRE,
388 TEGRA_APBDMA_CHAN_CSRE_PAUSE);
389 if (wait_for_burst_complete)
390 udelay(TEGRA_APBDMA_BURST_COMPLETE_TIME);
392 tegra_dma_global_pause(tdc, wait_for_burst_complete);
396 static void tegra_dma_resume(struct tegra_dma_channel *tdc)
398 struct tegra_dma *tdma = tdc->tdma;
400 if (tdma->chip_data->support_channel_pause)
401 tdc_write(tdc, TEGRA_APBDMA_CHAN_CSRE, 0);
403 tegra_dma_global_resume(tdc);
406 static void tegra_dma_stop(struct tegra_dma_channel *tdc)
410 /* Disable interrupts */
411 csr = tdc_read(tdc, TEGRA_APBDMA_CHAN_CSR);
412 csr &= ~TEGRA_APBDMA_CSR_IE_EOC;
413 tdc_write(tdc, TEGRA_APBDMA_CHAN_CSR, csr);
416 csr &= ~TEGRA_APBDMA_CSR_ENB;
417 tdc_write(tdc, TEGRA_APBDMA_CHAN_CSR, csr);
419 /* Clear interrupt status if it is there */
420 status = tdc_read(tdc, TEGRA_APBDMA_CHAN_STATUS);
421 if (status & TEGRA_APBDMA_STATUS_ISE_EOC) {
422 dev_dbg(tdc2dev(tdc), "%s():clearing interrupt\n", __func__);
423 tdc_write(tdc, TEGRA_APBDMA_CHAN_STATUS, status);
428 static void tegra_dma_start(struct tegra_dma_channel *tdc,
429 struct tegra_dma_sg_req *sg_req)
431 struct tegra_dma_channel_regs *ch_regs = &sg_req->ch_regs;
433 tdc_write(tdc, TEGRA_APBDMA_CHAN_CSR, ch_regs->csr);
434 tdc_write(tdc, TEGRA_APBDMA_CHAN_APBSEQ, ch_regs->apb_seq);
435 tdc_write(tdc, TEGRA_APBDMA_CHAN_APBPTR, ch_regs->apb_ptr);
436 tdc_write(tdc, TEGRA_APBDMA_CHAN_AHBSEQ, ch_regs->ahb_seq);
437 tdc_write(tdc, TEGRA_APBDMA_CHAN_AHBPTR, ch_regs->ahb_ptr);
438 if (tdc->tdma->chip_data->support_separate_wcount_reg)
439 tdc_write(tdc, TEGRA_APBDMA_CHAN_WCOUNT, ch_regs->wcount);
442 tdc_write(tdc, TEGRA_APBDMA_CHAN_CSR,
443 ch_regs->csr | TEGRA_APBDMA_CSR_ENB);
446 static void tegra_dma_configure_for_next(struct tegra_dma_channel *tdc,
447 struct tegra_dma_sg_req *nsg_req)
449 unsigned long status;
452 * The DMA controller reloads the new configuration for next transfer
453 * after last burst of current transfer completes.
454 * If there is no IEC status then this makes sure that last burst
455 * has not be completed. There may be case that last burst is on
456 * flight and so it can complete but because DMA is paused, it
457 * will not generates interrupt as well as not reload the new
459 * If there is already IEC status then interrupt handler need to
460 * load new configuration.
462 tegra_dma_pause(tdc, false);
463 status = tdc_read(tdc, TEGRA_APBDMA_CHAN_STATUS);
466 * If interrupt is pending then do nothing as the ISR will handle
467 * the programing for new request.
469 if (status & TEGRA_APBDMA_STATUS_ISE_EOC) {
470 dev_err(tdc2dev(tdc),
471 "Skipping new configuration as interrupt is pending\n");
472 tegra_dma_resume(tdc);
476 /* Safe to program new configuration */
477 tdc_write(tdc, TEGRA_APBDMA_CHAN_APBPTR, nsg_req->ch_regs.apb_ptr);
478 tdc_write(tdc, TEGRA_APBDMA_CHAN_AHBPTR, nsg_req->ch_regs.ahb_ptr);
479 if (tdc->tdma->chip_data->support_separate_wcount_reg)
480 tdc_write(tdc, TEGRA_APBDMA_CHAN_WCOUNT,
481 nsg_req->ch_regs.wcount);
482 tdc_write(tdc, TEGRA_APBDMA_CHAN_CSR,
483 nsg_req->ch_regs.csr | TEGRA_APBDMA_CSR_ENB);
484 nsg_req->configured = true;
485 nsg_req->words_xferred = 0;
487 tegra_dma_resume(tdc);
490 static void tdc_start_head_req(struct tegra_dma_channel *tdc)
492 struct tegra_dma_sg_req *sg_req;
494 sg_req = list_first_entry(&tdc->pending_sg_req, typeof(*sg_req), node);
495 tegra_dma_start(tdc, sg_req);
496 sg_req->configured = true;
497 sg_req->words_xferred = 0;
501 static void tdc_configure_next_head_desc(struct tegra_dma_channel *tdc)
503 struct tegra_dma_sg_req *hsgreq, *hnsgreq;
505 hsgreq = list_first_entry(&tdc->pending_sg_req, typeof(*hsgreq), node);
506 if (!list_is_last(&hsgreq->node, &tdc->pending_sg_req)) {
507 hnsgreq = list_first_entry(&hsgreq->node, typeof(*hnsgreq),
509 tegra_dma_configure_for_next(tdc, hnsgreq);
513 static inline unsigned int
514 get_current_xferred_count(struct tegra_dma_channel *tdc,
515 struct tegra_dma_sg_req *sg_req,
516 unsigned long status)
518 return sg_req->req_len - (status & TEGRA_APBDMA_STATUS_COUNT_MASK) - 4;
521 static void tegra_dma_abort_all(struct tegra_dma_channel *tdc)
523 struct tegra_dma_desc *dma_desc;
524 struct tegra_dma_sg_req *sgreq;
526 while (!list_empty(&tdc->pending_sg_req)) {
527 sgreq = list_first_entry(&tdc->pending_sg_req, typeof(*sgreq),
529 list_move_tail(&sgreq->node, &tdc->free_sg_req);
530 if (sgreq->last_sg) {
531 dma_desc = sgreq->dma_desc;
532 dma_desc->dma_status = DMA_ERROR;
533 list_add_tail(&dma_desc->node, &tdc->free_dma_desc);
535 /* Add in cb list if it is not there. */
536 if (!dma_desc->cb_count)
537 list_add_tail(&dma_desc->cb_node,
539 dma_desc->cb_count++;
542 tdc->isr_handler = NULL;
545 static bool handle_continuous_head_request(struct tegra_dma_channel *tdc,
548 struct tegra_dma_sg_req *hsgreq;
551 * Check that head req on list should be in flight.
552 * If it is not in flight then abort transfer as
553 * looping of transfer can not continue.
555 hsgreq = list_first_entry(&tdc->pending_sg_req, typeof(*hsgreq), node);
556 if (!hsgreq->configured) {
558 pm_runtime_put(tdc->tdma->dev);
559 dev_err(tdc2dev(tdc), "DMA transfer underflow, aborting DMA\n");
560 tegra_dma_abort_all(tdc);
564 /* Configure next request */
566 tdc_configure_next_head_desc(tdc);
571 static void handle_once_dma_done(struct tegra_dma_channel *tdc,
574 struct tegra_dma_desc *dma_desc;
575 struct tegra_dma_sg_req *sgreq;
578 sgreq = list_first_entry(&tdc->pending_sg_req, typeof(*sgreq), node);
579 dma_desc = sgreq->dma_desc;
580 dma_desc->bytes_transferred += sgreq->req_len;
582 list_del(&sgreq->node);
583 if (sgreq->last_sg) {
584 dma_desc->dma_status = DMA_COMPLETE;
585 dma_cookie_complete(&dma_desc->txd);
586 if (!dma_desc->cb_count)
587 list_add_tail(&dma_desc->cb_node, &tdc->cb_desc);
588 dma_desc->cb_count++;
589 list_add_tail(&dma_desc->node, &tdc->free_dma_desc);
591 list_add_tail(&sgreq->node, &tdc->free_sg_req);
593 /* Do not start DMA if it is going to be terminate */
597 if (list_empty(&tdc->pending_sg_req)) {
598 pm_runtime_put(tdc->tdma->dev);
602 tdc_start_head_req(tdc);
605 static void handle_cont_sngl_cycle_dma_done(struct tegra_dma_channel *tdc,
608 struct tegra_dma_desc *dma_desc;
609 struct tegra_dma_sg_req *sgreq;
612 sgreq = list_first_entry(&tdc->pending_sg_req, typeof(*sgreq), node);
613 dma_desc = sgreq->dma_desc;
614 /* if we dma for long enough the transfer count will wrap */
615 dma_desc->bytes_transferred =
616 (dma_desc->bytes_transferred + sgreq->req_len) %
617 dma_desc->bytes_requested;
619 /* Callback need to be call */
620 if (!dma_desc->cb_count)
621 list_add_tail(&dma_desc->cb_node, &tdc->cb_desc);
622 dma_desc->cb_count++;
624 sgreq->words_xferred = 0;
626 /* If not last req then put at end of pending list */
627 if (!list_is_last(&sgreq->node, &tdc->pending_sg_req)) {
628 list_move_tail(&sgreq->node, &tdc->pending_sg_req);
629 sgreq->configured = false;
630 st = handle_continuous_head_request(tdc, to_terminate);
632 dma_desc->dma_status = DMA_ERROR;
636 static void tegra_dma_tasklet(struct tasklet_struct *t)
638 struct tegra_dma_channel *tdc = from_tasklet(tdc, t, tasklet);
639 struct dmaengine_desc_callback cb;
640 struct tegra_dma_desc *dma_desc;
641 unsigned int cb_count;
644 spin_lock_irqsave(&tdc->lock, flags);
645 while (!list_empty(&tdc->cb_desc)) {
646 dma_desc = list_first_entry(&tdc->cb_desc, typeof(*dma_desc),
648 list_del(&dma_desc->cb_node);
649 dmaengine_desc_get_callback(&dma_desc->txd, &cb);
650 cb_count = dma_desc->cb_count;
651 dma_desc->cb_count = 0;
652 trace_tegra_dma_complete_cb(&tdc->dma_chan, cb_count,
654 spin_unlock_irqrestore(&tdc->lock, flags);
656 dmaengine_desc_callback_invoke(&cb, NULL);
657 spin_lock_irqsave(&tdc->lock, flags);
659 spin_unlock_irqrestore(&tdc->lock, flags);
662 static irqreturn_t tegra_dma_isr(int irq, void *dev_id)
664 struct tegra_dma_channel *tdc = dev_id;
667 spin_lock(&tdc->lock);
669 trace_tegra_dma_isr(&tdc->dma_chan, irq);
670 status = tdc_read(tdc, TEGRA_APBDMA_CHAN_STATUS);
671 if (status & TEGRA_APBDMA_STATUS_ISE_EOC) {
672 tdc_write(tdc, TEGRA_APBDMA_CHAN_STATUS, status);
673 tdc->isr_handler(tdc, false);
674 tasklet_schedule(&tdc->tasklet);
675 wake_up_all(&tdc->wq);
676 spin_unlock(&tdc->lock);
680 spin_unlock(&tdc->lock);
681 dev_info(tdc2dev(tdc), "Interrupt already served status 0x%08x\n",
687 static dma_cookie_t tegra_dma_tx_submit(struct dma_async_tx_descriptor *txd)
689 struct tegra_dma_desc *dma_desc = txd_to_tegra_dma_desc(txd);
690 struct tegra_dma_channel *tdc = to_tegra_dma_chan(txd->chan);
694 spin_lock_irqsave(&tdc->lock, flags);
695 dma_desc->dma_status = DMA_IN_PROGRESS;
696 cookie = dma_cookie_assign(&dma_desc->txd);
697 list_splice_tail_init(&dma_desc->tx_list, &tdc->pending_sg_req);
698 spin_unlock_irqrestore(&tdc->lock, flags);
703 static void tegra_dma_issue_pending(struct dma_chan *dc)
705 struct tegra_dma_channel *tdc = to_tegra_dma_chan(dc);
709 spin_lock_irqsave(&tdc->lock, flags);
710 if (list_empty(&tdc->pending_sg_req)) {
711 dev_err(tdc2dev(tdc), "No DMA request\n");
715 err = pm_runtime_resume_and_get(tdc->tdma->dev);
717 dev_err(tdc2dev(tdc), "Failed to enable DMA\n");
721 tdc_start_head_req(tdc);
723 /* Continuous single mode: Configure next req */
726 * Wait for 1 burst time for configure DMA for
729 udelay(TEGRA_APBDMA_BURST_COMPLETE_TIME);
730 tdc_configure_next_head_desc(tdc);
734 spin_unlock_irqrestore(&tdc->lock, flags);
737 static int tegra_dma_terminate_all(struct dma_chan *dc)
739 struct tegra_dma_channel *tdc = to_tegra_dma_chan(dc);
740 struct tegra_dma_desc *dma_desc;
741 struct tegra_dma_sg_req *sgreq;
746 spin_lock_irqsave(&tdc->lock, flags);
751 /* Pause DMA before checking the queue status */
752 tegra_dma_pause(tdc, true);
754 status = tdc_read(tdc, TEGRA_APBDMA_CHAN_STATUS);
755 if (status & TEGRA_APBDMA_STATUS_ISE_EOC) {
756 dev_dbg(tdc2dev(tdc), "%s():handling isr\n", __func__);
757 tdc->isr_handler(tdc, true);
758 status = tdc_read(tdc, TEGRA_APBDMA_CHAN_STATUS);
760 if (tdc->tdma->chip_data->support_separate_wcount_reg)
761 wcount = tdc_read(tdc, TEGRA_APBDMA_CHAN_WORD_TRANSFER);
765 was_busy = tdc->busy;
768 if (!list_empty(&tdc->pending_sg_req) && was_busy) {
769 sgreq = list_first_entry(&tdc->pending_sg_req, typeof(*sgreq),
771 sgreq->dma_desc->bytes_transferred +=
772 get_current_xferred_count(tdc, sgreq, wcount);
774 tegra_dma_resume(tdc);
776 pm_runtime_put(tdc->tdma->dev);
777 wake_up_all(&tdc->wq);
780 tegra_dma_abort_all(tdc);
782 while (!list_empty(&tdc->cb_desc)) {
783 dma_desc = list_first_entry(&tdc->cb_desc, typeof(*dma_desc),
785 list_del(&dma_desc->cb_node);
786 dma_desc->cb_count = 0;
788 spin_unlock_irqrestore(&tdc->lock, flags);
793 static bool tegra_dma_eoc_interrupt_deasserted(struct tegra_dma_channel *tdc)
798 spin_lock_irqsave(&tdc->lock, flags);
799 status = tdc_read(tdc, TEGRA_APBDMA_CHAN_STATUS);
800 spin_unlock_irqrestore(&tdc->lock, flags);
802 return !(status & TEGRA_APBDMA_STATUS_ISE_EOC);
805 static void tegra_dma_synchronize(struct dma_chan *dc)
807 struct tegra_dma_channel *tdc = to_tegra_dma_chan(dc);
810 err = pm_runtime_resume_and_get(tdc->tdma->dev);
812 dev_err(tdc2dev(tdc), "Failed to synchronize DMA: %d\n", err);
817 * CPU, which handles interrupt, could be busy in
818 * uninterruptible state, in this case sibling CPU
819 * should wait until interrupt is handled.
821 wait_event(tdc->wq, tegra_dma_eoc_interrupt_deasserted(tdc));
823 tasklet_kill(&tdc->tasklet);
825 pm_runtime_put(tdc->tdma->dev);
828 static unsigned int tegra_dma_sg_bytes_xferred(struct tegra_dma_channel *tdc,
829 struct tegra_dma_sg_req *sg_req)
831 u32 status, wcount = 0;
833 if (!list_is_first(&sg_req->node, &tdc->pending_sg_req))
836 if (tdc->tdma->chip_data->support_separate_wcount_reg)
837 wcount = tdc_read(tdc, TEGRA_APBDMA_CHAN_WORD_TRANSFER);
839 status = tdc_read(tdc, TEGRA_APBDMA_CHAN_STATUS);
841 if (!tdc->tdma->chip_data->support_separate_wcount_reg)
844 if (status & TEGRA_APBDMA_STATUS_ISE_EOC)
845 return sg_req->req_len;
847 wcount = get_current_xferred_count(tdc, sg_req, wcount);
851 * If wcount wasn't ever polled for this SG before, then
852 * simply assume that transfer hasn't started yet.
854 * Otherwise it's the end of the transfer.
856 * The alternative would be to poll the status register
857 * until EOC bit is set or wcount goes UP. That's so
858 * because EOC bit is getting set only after the last
859 * burst's completion and counter is less than the actual
860 * transfer size by 4 bytes. The counter value wraps around
861 * in a cyclic mode before EOC is set(!), so we can't easily
862 * distinguish start of transfer from its end.
864 if (sg_req->words_xferred)
865 wcount = sg_req->req_len - 4;
867 } else if (wcount < sg_req->words_xferred) {
869 * This case will never happen for a non-cyclic transfer.
871 * For a cyclic transfer, although it is possible for the
872 * next transfer to have already started (resetting the word
873 * count), this case should still not happen because we should
874 * have detected that the EOC bit is set and hence the transfer
879 wcount = sg_req->req_len - 4;
881 sg_req->words_xferred = wcount;
887 static enum dma_status tegra_dma_tx_status(struct dma_chan *dc,
889 struct dma_tx_state *txstate)
891 struct tegra_dma_channel *tdc = to_tegra_dma_chan(dc);
892 struct tegra_dma_desc *dma_desc;
893 struct tegra_dma_sg_req *sg_req;
896 unsigned int residual;
897 unsigned int bytes = 0;
899 ret = dma_cookie_status(dc, cookie, txstate);
900 if (ret == DMA_COMPLETE)
903 spin_lock_irqsave(&tdc->lock, flags);
905 /* Check on wait_ack desc status */
906 list_for_each_entry(dma_desc, &tdc->free_dma_desc, node) {
907 if (dma_desc->txd.cookie == cookie) {
908 ret = dma_desc->dma_status;
913 /* Check in pending list */
914 list_for_each_entry(sg_req, &tdc->pending_sg_req, node) {
915 dma_desc = sg_req->dma_desc;
916 if (dma_desc->txd.cookie == cookie) {
917 bytes = tegra_dma_sg_bytes_xferred(tdc, sg_req);
918 ret = dma_desc->dma_status;
923 dev_dbg(tdc2dev(tdc), "cookie %d not found\n", cookie);
927 if (dma_desc && txstate) {
928 residual = dma_desc->bytes_requested -
929 ((dma_desc->bytes_transferred + bytes) %
930 dma_desc->bytes_requested);
931 dma_set_residue(txstate, residual);
934 trace_tegra_dma_tx_status(&tdc->dma_chan, cookie, txstate);
935 spin_unlock_irqrestore(&tdc->lock, flags);
940 static inline unsigned int get_bus_width(struct tegra_dma_channel *tdc,
941 enum dma_slave_buswidth slave_bw)
944 case DMA_SLAVE_BUSWIDTH_1_BYTE:
945 return TEGRA_APBDMA_APBSEQ_BUS_WIDTH_8;
946 case DMA_SLAVE_BUSWIDTH_2_BYTES:
947 return TEGRA_APBDMA_APBSEQ_BUS_WIDTH_16;
948 case DMA_SLAVE_BUSWIDTH_4_BYTES:
949 return TEGRA_APBDMA_APBSEQ_BUS_WIDTH_32;
950 case DMA_SLAVE_BUSWIDTH_8_BYTES:
951 return TEGRA_APBDMA_APBSEQ_BUS_WIDTH_64;
953 dev_warn(tdc2dev(tdc),
954 "slave bw is not supported, using 32bits\n");
955 return TEGRA_APBDMA_APBSEQ_BUS_WIDTH_32;
959 static inline unsigned int get_burst_size(struct tegra_dma_channel *tdc,
961 enum dma_slave_buswidth slave_bw,
964 unsigned int burst_byte, burst_ahb_width;
967 * burst_size from client is in terms of the bus_width.
968 * convert them into AHB memory width which is 4 byte.
970 burst_byte = burst_size * slave_bw;
971 burst_ahb_width = burst_byte / 4;
973 /* If burst size is 0 then calculate the burst size based on length */
974 if (!burst_ahb_width) {
976 return TEGRA_APBDMA_AHBSEQ_BURST_1;
977 else if ((len >> 4) & 0x1)
978 return TEGRA_APBDMA_AHBSEQ_BURST_4;
980 return TEGRA_APBDMA_AHBSEQ_BURST_8;
982 if (burst_ahb_width < 4)
983 return TEGRA_APBDMA_AHBSEQ_BURST_1;
984 else if (burst_ahb_width < 8)
985 return TEGRA_APBDMA_AHBSEQ_BURST_4;
987 return TEGRA_APBDMA_AHBSEQ_BURST_8;
990 static int get_transfer_param(struct tegra_dma_channel *tdc,
991 enum dma_transfer_direction direction,
995 unsigned int *burst_size,
996 enum dma_slave_buswidth *slave_bw)
1000 *apb_addr = tdc->dma_sconfig.dst_addr;
1001 *apb_seq = get_bus_width(tdc, tdc->dma_sconfig.dst_addr_width);
1002 *burst_size = tdc->dma_sconfig.dst_maxburst;
1003 *slave_bw = tdc->dma_sconfig.dst_addr_width;
1004 *csr = TEGRA_APBDMA_CSR_DIR;
1007 case DMA_DEV_TO_MEM:
1008 *apb_addr = tdc->dma_sconfig.src_addr;
1009 *apb_seq = get_bus_width(tdc, tdc->dma_sconfig.src_addr_width);
1010 *burst_size = tdc->dma_sconfig.src_maxburst;
1011 *slave_bw = tdc->dma_sconfig.src_addr_width;
1016 dev_err(tdc2dev(tdc), "DMA direction is not supported\n");
1023 static void tegra_dma_prep_wcount(struct tegra_dma_channel *tdc,
1024 struct tegra_dma_channel_regs *ch_regs,
1027 u32 len_field = (len - 4) & 0xFFFC;
1029 if (tdc->tdma->chip_data->support_separate_wcount_reg)
1030 ch_regs->wcount = len_field;
1032 ch_regs->csr |= len_field;
1035 static struct dma_async_tx_descriptor *
1036 tegra_dma_prep_slave_sg(struct dma_chan *dc,
1037 struct scatterlist *sgl,
1038 unsigned int sg_len,
1039 enum dma_transfer_direction direction,
1040 unsigned long flags,
1043 struct tegra_dma_channel *tdc = to_tegra_dma_chan(dc);
1044 struct tegra_dma_sg_req *sg_req = NULL;
1045 u32 csr, ahb_seq, apb_ptr, apb_seq;
1046 enum dma_slave_buswidth slave_bw;
1047 struct tegra_dma_desc *dma_desc;
1048 struct list_head req_list;
1049 struct scatterlist *sg;
1050 unsigned int burst_size;
1053 if (!tdc->config_init) {
1054 dev_err(tdc2dev(tdc), "DMA channel is not configured\n");
1058 dev_err(tdc2dev(tdc), "Invalid segment length %d\n", sg_len);
1062 if (get_transfer_param(tdc, direction, &apb_ptr, &apb_seq, &csr,
1063 &burst_size, &slave_bw) < 0)
1066 INIT_LIST_HEAD(&req_list);
1068 ahb_seq = TEGRA_APBDMA_AHBSEQ_INTR_ENB;
1069 ahb_seq |= TEGRA_APBDMA_AHBSEQ_WRAP_NONE <<
1070 TEGRA_APBDMA_AHBSEQ_WRAP_SHIFT;
1071 ahb_seq |= TEGRA_APBDMA_AHBSEQ_BUS_WIDTH_32;
1073 csr |= TEGRA_APBDMA_CSR_ONCE;
1075 if (tdc->slave_id != TEGRA_APBDMA_SLAVE_ID_INVALID) {
1076 csr |= TEGRA_APBDMA_CSR_FLOW;
1077 csr |= tdc->slave_id << TEGRA_APBDMA_CSR_REQ_SEL_SHIFT;
1080 if (flags & DMA_PREP_INTERRUPT) {
1081 csr |= TEGRA_APBDMA_CSR_IE_EOC;
1087 apb_seq |= TEGRA_APBDMA_APBSEQ_WRAP_WORD_1;
1089 dma_desc = tegra_dma_desc_get(tdc);
1091 dev_err(tdc2dev(tdc), "DMA descriptors not available\n");
1094 INIT_LIST_HEAD(&dma_desc->tx_list);
1095 INIT_LIST_HEAD(&dma_desc->cb_node);
1096 dma_desc->cb_count = 0;
1097 dma_desc->bytes_requested = 0;
1098 dma_desc->bytes_transferred = 0;
1099 dma_desc->dma_status = DMA_IN_PROGRESS;
1101 /* Make transfer requests */
1102 for_each_sg(sgl, sg, sg_len, i) {
1105 mem = sg_dma_address(sg);
1106 len = sg_dma_len(sg);
1108 if ((len & 3) || (mem & 3) ||
1109 len > tdc->tdma->chip_data->max_dma_count) {
1110 dev_err(tdc2dev(tdc),
1111 "DMA length/memory address is not supported\n");
1112 tegra_dma_desc_put(tdc, dma_desc);
1116 sg_req = tegra_dma_sg_req_get(tdc);
1118 dev_err(tdc2dev(tdc), "DMA sg-req not available\n");
1119 tegra_dma_desc_put(tdc, dma_desc);
1123 ahb_seq |= get_burst_size(tdc, burst_size, slave_bw, len);
1124 dma_desc->bytes_requested += len;
1126 sg_req->ch_regs.apb_ptr = apb_ptr;
1127 sg_req->ch_regs.ahb_ptr = mem;
1128 sg_req->ch_regs.csr = csr;
1129 tegra_dma_prep_wcount(tdc, &sg_req->ch_regs, len);
1130 sg_req->ch_regs.apb_seq = apb_seq;
1131 sg_req->ch_regs.ahb_seq = ahb_seq;
1132 sg_req->configured = false;
1133 sg_req->last_sg = false;
1134 sg_req->dma_desc = dma_desc;
1135 sg_req->req_len = len;
1137 list_add_tail(&sg_req->node, &dma_desc->tx_list);
1139 sg_req->last_sg = true;
1140 if (flags & DMA_CTRL_ACK)
1141 dma_desc->txd.flags = DMA_CTRL_ACK;
1144 * Make sure that mode should not be conflicting with currently
1147 if (!tdc->isr_handler) {
1148 tdc->isr_handler = handle_once_dma_done;
1149 tdc->cyclic = false;
1152 dev_err(tdc2dev(tdc), "DMA configured in cyclic mode\n");
1153 tegra_dma_desc_put(tdc, dma_desc);
1158 return &dma_desc->txd;
1161 static struct dma_async_tx_descriptor *
1162 tegra_dma_prep_dma_cyclic(struct dma_chan *dc, dma_addr_t buf_addr,
1165 enum dma_transfer_direction direction,
1166 unsigned long flags)
1168 struct tegra_dma_channel *tdc = to_tegra_dma_chan(dc);
1169 struct tegra_dma_sg_req *sg_req = NULL;
1170 u32 csr, ahb_seq, apb_ptr, apb_seq;
1171 enum dma_slave_buswidth slave_bw;
1172 struct tegra_dma_desc *dma_desc;
1173 dma_addr_t mem = buf_addr;
1174 unsigned int burst_size;
1175 size_t len, remain_len;
1177 if (!buf_len || !period_len) {
1178 dev_err(tdc2dev(tdc), "Invalid buffer/period len\n");
1182 if (!tdc->config_init) {
1183 dev_err(tdc2dev(tdc), "DMA slave is not configured\n");
1188 * We allow to take more number of requests till DMA is
1189 * not started. The driver will loop over all requests.
1190 * Once DMA is started then new requests can be queued only after
1191 * terminating the DMA.
1194 dev_err(tdc2dev(tdc), "Request not allowed when DMA running\n");
1199 * We only support cycle transfer when buf_len is multiple of
1202 if (buf_len % period_len) {
1203 dev_err(tdc2dev(tdc), "buf_len is not multiple of period_len\n");
1208 if ((len & 3) || (buf_addr & 3) ||
1209 len > tdc->tdma->chip_data->max_dma_count) {
1210 dev_err(tdc2dev(tdc), "Req len/mem address is not correct\n");
1214 if (get_transfer_param(tdc, direction, &apb_ptr, &apb_seq, &csr,
1215 &burst_size, &slave_bw) < 0)
1218 ahb_seq = TEGRA_APBDMA_AHBSEQ_INTR_ENB;
1219 ahb_seq |= TEGRA_APBDMA_AHBSEQ_WRAP_NONE <<
1220 TEGRA_APBDMA_AHBSEQ_WRAP_SHIFT;
1221 ahb_seq |= TEGRA_APBDMA_AHBSEQ_BUS_WIDTH_32;
1223 if (tdc->slave_id != TEGRA_APBDMA_SLAVE_ID_INVALID) {
1224 csr |= TEGRA_APBDMA_CSR_FLOW;
1225 csr |= tdc->slave_id << TEGRA_APBDMA_CSR_REQ_SEL_SHIFT;
1228 if (flags & DMA_PREP_INTERRUPT) {
1229 csr |= TEGRA_APBDMA_CSR_IE_EOC;
1235 apb_seq |= TEGRA_APBDMA_APBSEQ_WRAP_WORD_1;
1237 dma_desc = tegra_dma_desc_get(tdc);
1239 dev_err(tdc2dev(tdc), "not enough descriptors available\n");
1243 INIT_LIST_HEAD(&dma_desc->tx_list);
1244 INIT_LIST_HEAD(&dma_desc->cb_node);
1245 dma_desc->cb_count = 0;
1247 dma_desc->bytes_transferred = 0;
1248 dma_desc->bytes_requested = buf_len;
1249 remain_len = buf_len;
1251 /* Split transfer equal to period size */
1252 while (remain_len) {
1253 sg_req = tegra_dma_sg_req_get(tdc);
1255 dev_err(tdc2dev(tdc), "DMA sg-req not available\n");
1256 tegra_dma_desc_put(tdc, dma_desc);
1260 ahb_seq |= get_burst_size(tdc, burst_size, slave_bw, len);
1261 sg_req->ch_regs.apb_ptr = apb_ptr;
1262 sg_req->ch_regs.ahb_ptr = mem;
1263 sg_req->ch_regs.csr = csr;
1264 tegra_dma_prep_wcount(tdc, &sg_req->ch_regs, len);
1265 sg_req->ch_regs.apb_seq = apb_seq;
1266 sg_req->ch_regs.ahb_seq = ahb_seq;
1267 sg_req->configured = false;
1268 sg_req->last_sg = false;
1269 sg_req->dma_desc = dma_desc;
1270 sg_req->req_len = len;
1272 list_add_tail(&sg_req->node, &dma_desc->tx_list);
1276 sg_req->last_sg = true;
1277 if (flags & DMA_CTRL_ACK)
1278 dma_desc->txd.flags = DMA_CTRL_ACK;
1281 * Make sure that mode should not be conflicting with currently
1284 if (!tdc->isr_handler) {
1285 tdc->isr_handler = handle_cont_sngl_cycle_dma_done;
1289 dev_err(tdc2dev(tdc), "DMA configuration conflict\n");
1290 tegra_dma_desc_put(tdc, dma_desc);
1295 return &dma_desc->txd;
1298 static int tegra_dma_alloc_chan_resources(struct dma_chan *dc)
1300 struct tegra_dma_channel *tdc = to_tegra_dma_chan(dc);
1302 dma_cookie_init(&tdc->dma_chan);
1307 static void tegra_dma_free_chan_resources(struct dma_chan *dc)
1309 struct tegra_dma_channel *tdc = to_tegra_dma_chan(dc);
1310 struct tegra_dma_desc *dma_desc;
1311 struct tegra_dma_sg_req *sg_req;
1312 struct list_head dma_desc_list;
1313 struct list_head sg_req_list;
1315 INIT_LIST_HEAD(&dma_desc_list);
1316 INIT_LIST_HEAD(&sg_req_list);
1318 dev_dbg(tdc2dev(tdc), "Freeing channel %d\n", tdc->id);
1320 tegra_dma_terminate_all(dc);
1321 tasklet_kill(&tdc->tasklet);
1323 list_splice_init(&tdc->pending_sg_req, &sg_req_list);
1324 list_splice_init(&tdc->free_sg_req, &sg_req_list);
1325 list_splice_init(&tdc->free_dma_desc, &dma_desc_list);
1326 INIT_LIST_HEAD(&tdc->cb_desc);
1327 tdc->config_init = false;
1328 tdc->isr_handler = NULL;
1330 while (!list_empty(&dma_desc_list)) {
1331 dma_desc = list_first_entry(&dma_desc_list, typeof(*dma_desc),
1333 list_del(&dma_desc->node);
1337 while (!list_empty(&sg_req_list)) {
1338 sg_req = list_first_entry(&sg_req_list, typeof(*sg_req), node);
1339 list_del(&sg_req->node);
1343 tdc->slave_id = TEGRA_APBDMA_SLAVE_ID_INVALID;
1346 static struct dma_chan *tegra_dma_of_xlate(struct of_phandle_args *dma_spec,
1347 struct of_dma *ofdma)
1349 struct tegra_dma *tdma = ofdma->of_dma_data;
1350 struct tegra_dma_channel *tdc;
1351 struct dma_chan *chan;
1353 if (dma_spec->args[0] > TEGRA_APBDMA_CSR_REQ_SEL_MASK) {
1354 dev_err(tdma->dev, "Invalid slave id: %d\n", dma_spec->args[0]);
1358 chan = dma_get_any_slave_channel(&tdma->dma_dev);
1362 tdc = to_tegra_dma_chan(chan);
1363 tdc->slave_id = dma_spec->args[0];
1368 /* Tegra20 specific DMA controller information */
1369 static const struct tegra_dma_chip_data tegra20_dma_chip_data = {
1371 .channel_reg_size = 0x20,
1372 .max_dma_count = 1024UL * 64,
1373 .support_channel_pause = false,
1374 .support_separate_wcount_reg = false,
1377 /* Tegra30 specific DMA controller information */
1378 static const struct tegra_dma_chip_data tegra30_dma_chip_data = {
1380 .channel_reg_size = 0x20,
1381 .max_dma_count = 1024UL * 64,
1382 .support_channel_pause = false,
1383 .support_separate_wcount_reg = false,
1386 /* Tegra114 specific DMA controller information */
1387 static const struct tegra_dma_chip_data tegra114_dma_chip_data = {
1389 .channel_reg_size = 0x20,
1390 .max_dma_count = 1024UL * 64,
1391 .support_channel_pause = true,
1392 .support_separate_wcount_reg = false,
1395 /* Tegra148 specific DMA controller information */
1396 static const struct tegra_dma_chip_data tegra148_dma_chip_data = {
1398 .channel_reg_size = 0x40,
1399 .max_dma_count = 1024UL * 64,
1400 .support_channel_pause = true,
1401 .support_separate_wcount_reg = true,
1404 static int tegra_dma_init_hw(struct tegra_dma *tdma)
1408 err = reset_control_assert(tdma->rst);
1410 dev_err(tdma->dev, "failed to assert reset: %d\n", err);
1414 err = clk_enable(tdma->dma_clk);
1416 dev_err(tdma->dev, "failed to enable clk: %d\n", err);
1420 /* reset DMA controller */
1422 reset_control_deassert(tdma->rst);
1424 /* enable global DMA registers */
1425 tdma_write(tdma, TEGRA_APBDMA_GENERAL, TEGRA_APBDMA_GENERAL_ENABLE);
1426 tdma_write(tdma, TEGRA_APBDMA_CONTROL, 0);
1427 tdma_write(tdma, TEGRA_APBDMA_IRQ_MASK_SET, 0xFFFFFFFF);
1429 clk_disable(tdma->dma_clk);
1434 static int tegra_dma_probe(struct platform_device *pdev)
1436 const struct tegra_dma_chip_data *cdata;
1437 struct tegra_dma *tdma;
1442 cdata = of_device_get_match_data(&pdev->dev);
1443 size = struct_size(tdma, channels, cdata->nr_channels);
1445 tdma = devm_kzalloc(&pdev->dev, size, GFP_KERNEL);
1449 tdma->dev = &pdev->dev;
1450 tdma->chip_data = cdata;
1451 platform_set_drvdata(pdev, tdma);
1453 tdma->base_addr = devm_platform_ioremap_resource(pdev, 0);
1454 if (IS_ERR(tdma->base_addr))
1455 return PTR_ERR(tdma->base_addr);
1457 tdma->dma_clk = devm_clk_get(&pdev->dev, NULL);
1458 if (IS_ERR(tdma->dma_clk)) {
1459 dev_err(&pdev->dev, "Error: Missing controller clock\n");
1460 return PTR_ERR(tdma->dma_clk);
1463 tdma->rst = devm_reset_control_get(&pdev->dev, "dma");
1464 if (IS_ERR(tdma->rst)) {
1465 dev_err(&pdev->dev, "Error: Missing reset\n");
1466 return PTR_ERR(tdma->rst);
1469 spin_lock_init(&tdma->global_lock);
1471 ret = clk_prepare(tdma->dma_clk);
1475 ret = tegra_dma_init_hw(tdma);
1477 goto err_clk_unprepare;
1479 pm_runtime_irq_safe(&pdev->dev);
1480 pm_runtime_enable(&pdev->dev);
1482 INIT_LIST_HEAD(&tdma->dma_dev.channels);
1483 for (i = 0; i < cdata->nr_channels; i++) {
1484 struct tegra_dma_channel *tdc = &tdma->channels[i];
1487 tdc->chan_addr = tdma->base_addr +
1488 TEGRA_APBDMA_CHANNEL_BASE_ADD_OFFSET +
1489 (i * cdata->channel_reg_size);
1491 irq = platform_get_irq(pdev, i);
1494 goto err_pm_disable;
1497 snprintf(tdc->name, sizeof(tdc->name), "apbdma.%d", i);
1498 ret = devm_request_irq(&pdev->dev, irq, tegra_dma_isr, 0,
1502 "request_irq failed with err %d channel %d\n",
1504 goto err_pm_disable;
1507 tdc->dma_chan.device = &tdma->dma_dev;
1508 dma_cookie_init(&tdc->dma_chan);
1509 list_add_tail(&tdc->dma_chan.device_node,
1510 &tdma->dma_dev.channels);
1513 tdc->slave_id = TEGRA_APBDMA_SLAVE_ID_INVALID;
1515 tasklet_setup(&tdc->tasklet, tegra_dma_tasklet);
1516 spin_lock_init(&tdc->lock);
1517 init_waitqueue_head(&tdc->wq);
1519 INIT_LIST_HEAD(&tdc->pending_sg_req);
1520 INIT_LIST_HEAD(&tdc->free_sg_req);
1521 INIT_LIST_HEAD(&tdc->free_dma_desc);
1522 INIT_LIST_HEAD(&tdc->cb_desc);
1525 dma_cap_set(DMA_SLAVE, tdma->dma_dev.cap_mask);
1526 dma_cap_set(DMA_PRIVATE, tdma->dma_dev.cap_mask);
1527 dma_cap_set(DMA_CYCLIC, tdma->dma_dev.cap_mask);
1529 tdma->global_pause_count = 0;
1530 tdma->dma_dev.dev = &pdev->dev;
1531 tdma->dma_dev.device_alloc_chan_resources =
1532 tegra_dma_alloc_chan_resources;
1533 tdma->dma_dev.device_free_chan_resources =
1534 tegra_dma_free_chan_resources;
1535 tdma->dma_dev.device_prep_slave_sg = tegra_dma_prep_slave_sg;
1536 tdma->dma_dev.device_prep_dma_cyclic = tegra_dma_prep_dma_cyclic;
1537 tdma->dma_dev.src_addr_widths = BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) |
1538 BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) |
1539 BIT(DMA_SLAVE_BUSWIDTH_4_BYTES) |
1540 BIT(DMA_SLAVE_BUSWIDTH_8_BYTES);
1541 tdma->dma_dev.dst_addr_widths = BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) |
1542 BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) |
1543 BIT(DMA_SLAVE_BUSWIDTH_4_BYTES) |
1544 BIT(DMA_SLAVE_BUSWIDTH_8_BYTES);
1545 tdma->dma_dev.directions = BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV);
1546 tdma->dma_dev.residue_granularity = DMA_RESIDUE_GRANULARITY_BURST;
1547 tdma->dma_dev.device_config = tegra_dma_slave_config;
1548 tdma->dma_dev.device_terminate_all = tegra_dma_terminate_all;
1549 tdma->dma_dev.device_synchronize = tegra_dma_synchronize;
1550 tdma->dma_dev.device_tx_status = tegra_dma_tx_status;
1551 tdma->dma_dev.device_issue_pending = tegra_dma_issue_pending;
1553 ret = dma_async_device_register(&tdma->dma_dev);
1556 "Tegra20 APB DMA driver registration failed %d\n", ret);
1557 goto err_pm_disable;
1560 ret = of_dma_controller_register(pdev->dev.of_node,
1561 tegra_dma_of_xlate, tdma);
1564 "Tegra20 APB DMA OF registration failed %d\n", ret);
1565 goto err_unregister_dma_dev;
1568 dev_info(&pdev->dev, "Tegra20 APB DMA driver registered %u channels\n",
1569 cdata->nr_channels);
1573 err_unregister_dma_dev:
1574 dma_async_device_unregister(&tdma->dma_dev);
1577 pm_runtime_disable(&pdev->dev);
1580 clk_unprepare(tdma->dma_clk);
1585 static int tegra_dma_remove(struct platform_device *pdev)
1587 struct tegra_dma *tdma = platform_get_drvdata(pdev);
1589 of_dma_controller_free(pdev->dev.of_node);
1590 dma_async_device_unregister(&tdma->dma_dev);
1591 pm_runtime_disable(&pdev->dev);
1592 clk_unprepare(tdma->dma_clk);
1597 static int __maybe_unused tegra_dma_runtime_suspend(struct device *dev)
1599 struct tegra_dma *tdma = dev_get_drvdata(dev);
1601 clk_disable(tdma->dma_clk);
1606 static int __maybe_unused tegra_dma_runtime_resume(struct device *dev)
1608 struct tegra_dma *tdma = dev_get_drvdata(dev);
1610 return clk_enable(tdma->dma_clk);
1613 static int __maybe_unused tegra_dma_dev_suspend(struct device *dev)
1615 struct tegra_dma *tdma = dev_get_drvdata(dev);
1616 unsigned long flags;
1620 for (i = 0; i < tdma->chip_data->nr_channels; i++) {
1621 struct tegra_dma_channel *tdc = &tdma->channels[i];
1623 tasklet_kill(&tdc->tasklet);
1625 spin_lock_irqsave(&tdc->lock, flags);
1627 spin_unlock_irqrestore(&tdc->lock, flags);
1630 dev_err(tdma->dev, "channel %u busy\n", i);
1635 return pm_runtime_force_suspend(dev);
1638 static int __maybe_unused tegra_dma_dev_resume(struct device *dev)
1640 struct tegra_dma *tdma = dev_get_drvdata(dev);
1643 err = tegra_dma_init_hw(tdma);
1647 return pm_runtime_force_resume(dev);
1650 static const struct dev_pm_ops tegra_dma_dev_pm_ops = {
1651 SET_RUNTIME_PM_OPS(tegra_dma_runtime_suspend, tegra_dma_runtime_resume,
1653 SET_SYSTEM_SLEEP_PM_OPS(tegra_dma_dev_suspend, tegra_dma_dev_resume)
1656 static const struct of_device_id tegra_dma_of_match[] = {
1658 .compatible = "nvidia,tegra148-apbdma",
1659 .data = &tegra148_dma_chip_data,
1661 .compatible = "nvidia,tegra114-apbdma",
1662 .data = &tegra114_dma_chip_data,
1664 .compatible = "nvidia,tegra30-apbdma",
1665 .data = &tegra30_dma_chip_data,
1667 .compatible = "nvidia,tegra20-apbdma",
1668 .data = &tegra20_dma_chip_data,
1672 MODULE_DEVICE_TABLE(of, tegra_dma_of_match);
1674 static struct platform_driver tegra_dmac_driver = {
1676 .name = "tegra-apbdma",
1677 .pm = &tegra_dma_dev_pm_ops,
1678 .of_match_table = tegra_dma_of_match,
1680 .probe = tegra_dma_probe,
1681 .remove = tegra_dma_remove,
1684 module_platform_driver(tegra_dmac_driver);
1686 MODULE_DESCRIPTION("NVIDIA Tegra APB DMA Controller driver");
1687 MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
1688 MODULE_LICENSE("GPL v2");