2 * arch/arm/mach-tegra/dma.c
4 * System DMA driver for NVIDIA Tegra SoCs
6 * Copyright (c) 2008-2009, NVIDIA Corporation.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24 #include <linux/interrupt.h>
25 #include <linux/module.h>
26 #include <linux/spinlock.h>
27 #include <linux/err.h>
28 #include <linux/irq.h>
29 #include <linux/delay.h>
30 #include <linux/clk.h>
32 #include <mach/irqs.h>
33 #include <mach/iomap.h>
34 #include <mach/suspend.h>
38 #define APB_DMA_GEN 0x000
39 #define GEN_ENABLE (1<<31)
41 #define APB_DMA_CNTRL 0x010
43 #define APB_DMA_IRQ_MASK 0x01c
45 #define APB_DMA_IRQ_MASK_SET 0x020
47 #define APB_DMA_CHAN_CSR 0x000
48 #define CSR_ENB (1<<31)
49 #define CSR_IE_EOC (1<<30)
50 #define CSR_HOLD (1<<29)
51 #define CSR_DIR (1<<28)
52 #define CSR_ONCE (1<<27)
53 #define CSR_FLOW (1<<21)
54 #define CSR_REQ_SEL_SHIFT 16
55 #define CSR_WCOUNT_SHIFT 2
56 #define CSR_WCOUNT_MASK 0xFFFC
58 #define APB_DMA_CHAN_STA 0x004
59 #define STA_BUSY (1<<31)
60 #define STA_ISE_EOC (1<<30)
61 #define STA_HALT (1<<29)
62 #define STA_PING_PONG (1<<28)
63 #define STA_COUNT_SHIFT 2
64 #define STA_COUNT_MASK 0xFFFC
66 #define APB_DMA_CHAN_AHB_PTR 0x010
68 #define APB_DMA_CHAN_AHB_SEQ 0x014
69 #define AHB_SEQ_INTR_ENB (1<<31)
70 #define AHB_SEQ_BUS_WIDTH_SHIFT 28
71 #define AHB_SEQ_BUS_WIDTH_MASK (0x7<<AHB_SEQ_BUS_WIDTH_SHIFT)
72 #define AHB_SEQ_BUS_WIDTH_8 (0<<AHB_SEQ_BUS_WIDTH_SHIFT)
73 #define AHB_SEQ_BUS_WIDTH_16 (1<<AHB_SEQ_BUS_WIDTH_SHIFT)
74 #define AHB_SEQ_BUS_WIDTH_32 (2<<AHB_SEQ_BUS_WIDTH_SHIFT)
75 #define AHB_SEQ_BUS_WIDTH_64 (3<<AHB_SEQ_BUS_WIDTH_SHIFT)
76 #define AHB_SEQ_BUS_WIDTH_128 (4<<AHB_SEQ_BUS_WIDTH_SHIFT)
77 #define AHB_SEQ_DATA_SWAP (1<<27)
78 #define AHB_SEQ_BURST_MASK (0x7<<24)
79 #define AHB_SEQ_BURST_1 (4<<24)
80 #define AHB_SEQ_BURST_4 (5<<24)
81 #define AHB_SEQ_BURST_8 (6<<24)
82 #define AHB_SEQ_DBL_BUF (1<<19)
83 #define AHB_SEQ_WRAP_SHIFT 16
84 #define AHB_SEQ_WRAP_MASK (0x7<<AHB_SEQ_WRAP_SHIFT)
86 #define APB_DMA_CHAN_APB_PTR 0x018
88 #define APB_DMA_CHAN_APB_SEQ 0x01c
89 #define APB_SEQ_BUS_WIDTH_SHIFT 28
90 #define APB_SEQ_BUS_WIDTH_MASK (0x7<<APB_SEQ_BUS_WIDTH_SHIFT)
91 #define APB_SEQ_BUS_WIDTH_8 (0<<APB_SEQ_BUS_WIDTH_SHIFT)
92 #define APB_SEQ_BUS_WIDTH_16 (1<<APB_SEQ_BUS_WIDTH_SHIFT)
93 #define APB_SEQ_BUS_WIDTH_32 (2<<APB_SEQ_BUS_WIDTH_SHIFT)
94 #define APB_SEQ_BUS_WIDTH_64 (3<<APB_SEQ_BUS_WIDTH_SHIFT)
95 #define APB_SEQ_BUS_WIDTH_128 (4<<APB_SEQ_BUS_WIDTH_SHIFT)
96 #define APB_SEQ_DATA_SWAP (1<<27)
97 #define APB_SEQ_WRAP_SHIFT 16
98 #define APB_SEQ_WRAP_MASK (0x7<<APB_SEQ_WRAP_SHIFT)
100 #define TEGRA_SYSTEM_DMA_CH_NR 16
101 #define TEGRA_SYSTEM_DMA_AVP_CH_NUM 4
102 #define TEGRA_SYSTEM_DMA_CH_MIN 0
103 #define TEGRA_SYSTEM_DMA_CH_MAX \
104 (TEGRA_SYSTEM_DMA_CH_NR - TEGRA_SYSTEM_DMA_AVP_CH_NUM - 1)
106 #define NV_DMA_MAX_TRASFER_SIZE 0x10000
108 static const unsigned int ahb_addr_wrap_table
[8] = {
109 0, 32, 64, 128, 256, 512, 1024, 2048
112 static const unsigned int apb_addr_wrap_table
[8] = {
113 0, 1, 2, 4, 8, 16, 32, 64
116 static const unsigned int bus_width_table
[5] = {
120 #define TEGRA_DMA_NAME_SIZE 16
121 struct tegra_dma_channel
{
122 struct list_head list
;
125 char name
[TEGRA_DMA_NAME_SIZE
];
129 int req_transfer_count
;
132 #define NV_DMA_MAX_CHANNELS 32
134 static bool tegra_dma_initialized
;
135 static DEFINE_MUTEX(tegra_dma_lock
);
136 static DEFINE_SPINLOCK(enable_lock
);
138 static DECLARE_BITMAP(channel_usage
, NV_DMA_MAX_CHANNELS
);
139 static struct tegra_dma_channel dma_channels
[NV_DMA_MAX_CHANNELS
];
141 static void tegra_dma_update_hw(struct tegra_dma_channel
*ch
,
142 struct tegra_dma_req
*req
);
143 static void tegra_dma_update_hw_partial(struct tegra_dma_channel
*ch
,
144 struct tegra_dma_req
*req
);
145 static void tegra_dma_stop(struct tegra_dma_channel
*ch
);
147 void tegra_dma_flush(struct tegra_dma_channel
*ch
)
150 EXPORT_SYMBOL(tegra_dma_flush
);
152 void tegra_dma_dequeue(struct tegra_dma_channel
*ch
)
154 struct tegra_dma_req
*req
;
156 if (tegra_dma_is_empty(ch
))
159 req
= list_entry(ch
->list
.next
, typeof(*req
), node
);
161 tegra_dma_dequeue_req(ch
, req
);
165 static void tegra_dma_stop(struct tegra_dma_channel
*ch
)
170 csr
= readl(ch
->addr
+ APB_DMA_CHAN_CSR
);
172 writel(csr
, ch
->addr
+ APB_DMA_CHAN_CSR
);
175 writel(csr
, ch
->addr
+ APB_DMA_CHAN_CSR
);
177 status
= readl(ch
->addr
+ APB_DMA_CHAN_STA
);
178 if (status
& STA_ISE_EOC
)
179 writel(status
, ch
->addr
+ APB_DMA_CHAN_STA
);
182 static int tegra_dma_cancel(struct tegra_dma_channel
*ch
)
184 unsigned long irq_flags
;
186 spin_lock_irqsave(&ch
->lock
, irq_flags
);
187 while (!list_empty(&ch
->list
))
188 list_del(ch
->list
.next
);
192 spin_unlock_irqrestore(&ch
->lock
, irq_flags
);
196 static unsigned int get_channel_status(struct tegra_dma_channel
*ch
,
197 struct tegra_dma_req
*req
, bool is_stop_dma
)
199 void __iomem
*addr
= IO_ADDRESS(TEGRA_APB_DMA_BASE
);
204 * STOP the DMA and get the transfer count.
205 * Getting the transfer count is tricky.
206 * - Globally disable DMA on all channels
207 * - Read the channel's status register to know the number
208 * of pending bytes to be transfered.
209 * - Stop the dma channel
210 * - Globally re-enable DMA to resume other transfers
212 spin_lock(&enable_lock
);
213 writel(0, addr
+ APB_DMA_GEN
);
215 status
= readl(ch
->addr
+ APB_DMA_CHAN_STA
);
217 writel(GEN_ENABLE
, addr
+ APB_DMA_GEN
);
218 spin_unlock(&enable_lock
);
219 if (status
& STA_ISE_EOC
) {
220 pr_err("Got Dma Int here clearing");
221 writel(status
, ch
->addr
+ APB_DMA_CHAN_STA
);
223 req
->status
= TEGRA_DMA_REQ_ERROR_ABORTED
;
225 status
= readl(ch
->addr
+ APB_DMA_CHAN_STA
);
230 /* should be called with the channel lock held */
231 static unsigned int dma_active_count(struct tegra_dma_channel
*ch
,
232 struct tegra_dma_req
*req
, unsigned int status
)
234 unsigned int to_transfer
;
235 unsigned int req_transfer_count
;
236 unsigned int bytes_transferred
;
238 to_transfer
= ((status
& STA_COUNT_MASK
) >> STA_COUNT_SHIFT
) + 1;
239 req_transfer_count
= ch
->req_transfer_count
+ 1;
240 bytes_transferred
= req_transfer_count
;
241 if (status
& STA_BUSY
)
242 bytes_transferred
-= to_transfer
;
244 * In continuous transfer mode, DMA only tracks the count of the
245 * half DMA buffer. So, if the DMA already finished half the DMA
246 * then add the half buffer to the completed count.
248 if (ch
->mode
& TEGRA_DMA_MODE_CONTINOUS
) {
249 if (req
->buffer_status
== TEGRA_DMA_REQ_BUF_STATUS_HALF_FULL
)
250 bytes_transferred
+= req_transfer_count
;
251 if (status
& STA_ISE_EOC
)
252 bytes_transferred
+= req_transfer_count
;
254 bytes_transferred
*= 4;
255 return bytes_transferred
;
258 int tegra_dma_dequeue_req(struct tegra_dma_channel
*ch
,
259 struct tegra_dma_req
*_req
)
262 struct tegra_dma_req
*req
= NULL
;
264 unsigned long irq_flags
;
267 spin_lock_irqsave(&ch
->lock
, irq_flags
);
269 if (list_entry(ch
->list
.next
, struct tegra_dma_req
, node
) == _req
)
272 list_for_each_entry(req
, &ch
->list
, node
) {
274 list_del(&req
->node
);
280 spin_unlock_irqrestore(&ch
->lock
, irq_flags
);
287 status
= get_channel_status(ch
, req
, true);
288 req
->bytes_transferred
= dma_active_count(ch
, req
, status
);
290 if (!list_empty(&ch
->list
)) {
291 /* if the list is not empty, queue the next request */
292 struct tegra_dma_req
*next_req
;
293 next_req
= list_entry(ch
->list
.next
,
294 typeof(*next_req
), node
);
295 tegra_dma_update_hw(ch
, next_req
);
299 req
->status
= -TEGRA_DMA_REQ_ERROR_ABORTED
;
301 spin_unlock_irqrestore(&ch
->lock
, irq_flags
);
303 /* Callback should be called without any lock */
307 EXPORT_SYMBOL(tegra_dma_dequeue_req
);
309 bool tegra_dma_is_empty(struct tegra_dma_channel
*ch
)
311 unsigned long irq_flags
;
314 spin_lock_irqsave(&ch
->lock
, irq_flags
);
315 if (list_empty(&ch
->list
))
319 spin_unlock_irqrestore(&ch
->lock
, irq_flags
);
322 EXPORT_SYMBOL(tegra_dma_is_empty
);
324 bool tegra_dma_is_req_inflight(struct tegra_dma_channel
*ch
,
325 struct tegra_dma_req
*_req
)
327 unsigned long irq_flags
;
328 struct tegra_dma_req
*req
;
330 spin_lock_irqsave(&ch
->lock
, irq_flags
);
331 list_for_each_entry(req
, &ch
->list
, node
) {
333 spin_unlock_irqrestore(&ch
->lock
, irq_flags
);
337 spin_unlock_irqrestore(&ch
->lock
, irq_flags
);
340 EXPORT_SYMBOL(tegra_dma_is_req_inflight
);
342 int tegra_dma_enqueue_req(struct tegra_dma_channel
*ch
,
343 struct tegra_dma_req
*req
)
345 unsigned long irq_flags
;
346 struct tegra_dma_req
*_req
;
349 if (req
->size
> NV_DMA_MAX_TRASFER_SIZE
||
350 req
->source_addr
& 0x3 || req
->dest_addr
& 0x3) {
351 pr_err("Invalid DMA request for channel %d\n", ch
->id
);
355 spin_lock_irqsave(&ch
->lock
, irq_flags
);
357 list_for_each_entry(_req
, &ch
->list
, node
) {
359 spin_unlock_irqrestore(&ch
->lock
, irq_flags
);
364 req
->bytes_transferred
= 0;
366 req
->buffer_status
= 0;
367 if (list_empty(&ch
->list
))
370 list_add_tail(&req
->node
, &ch
->list
);
373 tegra_dma_update_hw(ch
, req
);
375 spin_unlock_irqrestore(&ch
->lock
, irq_flags
);
379 EXPORT_SYMBOL(tegra_dma_enqueue_req
);
381 struct tegra_dma_channel
*tegra_dma_allocate_channel(int mode
)
384 struct tegra_dma_channel
*ch
= NULL
;
386 if (!tegra_dma_initialized
)
389 mutex_lock(&tegra_dma_lock
);
391 /* first channel is the shared channel */
392 if (mode
& TEGRA_DMA_SHARED
) {
393 channel
= TEGRA_SYSTEM_DMA_CH_MIN
;
395 channel
= find_first_zero_bit(channel_usage
,
396 ARRAY_SIZE(dma_channels
));
397 if (channel
>= ARRAY_SIZE(dma_channels
))
400 __set_bit(channel
, channel_usage
);
401 ch
= &dma_channels
[channel
];
405 mutex_unlock(&tegra_dma_lock
);
408 EXPORT_SYMBOL(tegra_dma_allocate_channel
);
410 void tegra_dma_free_channel(struct tegra_dma_channel
*ch
)
412 if (ch
->mode
& TEGRA_DMA_SHARED
)
414 tegra_dma_cancel(ch
);
415 mutex_lock(&tegra_dma_lock
);
416 __clear_bit(ch
->id
, channel_usage
);
417 mutex_unlock(&tegra_dma_lock
);
419 EXPORT_SYMBOL(tegra_dma_free_channel
);
421 static void tegra_dma_update_hw_partial(struct tegra_dma_channel
*ch
,
422 struct tegra_dma_req
*req
)
427 if (req
->to_memory
) {
428 apb_ptr
= req
->source_addr
;
429 ahb_ptr
= req
->dest_addr
;
431 apb_ptr
= req
->dest_addr
;
432 ahb_ptr
= req
->source_addr
;
434 writel(apb_ptr
, ch
->addr
+ APB_DMA_CHAN_APB_PTR
);
435 writel(ahb_ptr
, ch
->addr
+ APB_DMA_CHAN_AHB_PTR
);
437 req
->status
= TEGRA_DMA_REQ_INFLIGHT
;
441 static void tegra_dma_update_hw(struct tegra_dma_channel
*ch
,
442 struct tegra_dma_req
*req
)
456 csr
= CSR_IE_EOC
| CSR_FLOW
;
457 ahb_seq
= AHB_SEQ_INTR_ENB
| AHB_SEQ_BURST_1
;
460 csr
|= req
->req_sel
<< CSR_REQ_SEL_SHIFT
;
462 /* One shot mode is always single buffered,
463 * continuous mode is always double buffered
465 if (ch
->mode
& TEGRA_DMA_MODE_ONESHOT
) {
467 ch
->req_transfer_count
= (req
->size
>> 2) - 1;
469 ahb_seq
|= AHB_SEQ_DBL_BUF
;
471 /* In double buffered mode, we set the size to half the
472 * requested size and interrupt when half the buffer
474 ch
->req_transfer_count
= (req
->size
>> 3) - 1;
477 csr
|= ch
->req_transfer_count
<< CSR_WCOUNT_SHIFT
;
479 if (req
->to_memory
) {
480 apb_ptr
= req
->source_addr
;
481 ahb_ptr
= req
->dest_addr
;
483 apb_addr_wrap
= req
->source_wrap
;
484 ahb_addr_wrap
= req
->dest_wrap
;
485 apb_bus_width
= req
->source_bus_width
;
486 ahb_bus_width
= req
->dest_bus_width
;
490 apb_ptr
= req
->dest_addr
;
491 ahb_ptr
= req
->source_addr
;
493 apb_addr_wrap
= req
->dest_wrap
;
494 ahb_addr_wrap
= req
->source_wrap
;
495 apb_bus_width
= req
->dest_bus_width
;
496 ahb_bus_width
= req
->source_bus_width
;
502 /* set address wrap for APB size */
505 if (apb_addr_wrap_table
[index
] == apb_addr_wrap
)
508 } while (index
< ARRAY_SIZE(apb_addr_wrap_table
));
509 BUG_ON(index
== ARRAY_SIZE(apb_addr_wrap_table
));
510 apb_seq
|= index
<< APB_SEQ_WRAP_SHIFT
;
512 /* set address wrap for AHB size */
515 if (ahb_addr_wrap_table
[index
] == ahb_addr_wrap
)
518 } while (index
< ARRAY_SIZE(ahb_addr_wrap_table
));
519 BUG_ON(index
== ARRAY_SIZE(ahb_addr_wrap_table
));
520 ahb_seq
|= index
<< AHB_SEQ_WRAP_SHIFT
;
522 for (index
= 0; index
< ARRAY_SIZE(bus_width_table
); index
++) {
523 if (bus_width_table
[index
] == ahb_bus_width
)
526 BUG_ON(index
== ARRAY_SIZE(bus_width_table
));
527 ahb_seq
|= index
<< AHB_SEQ_BUS_WIDTH_SHIFT
;
529 for (index
= 0; index
< ARRAY_SIZE(bus_width_table
); index
++) {
530 if (bus_width_table
[index
] == apb_bus_width
)
533 BUG_ON(index
== ARRAY_SIZE(bus_width_table
));
534 apb_seq
|= index
<< APB_SEQ_BUS_WIDTH_SHIFT
;
536 writel(csr
, ch
->addr
+ APB_DMA_CHAN_CSR
);
537 writel(apb_seq
, ch
->addr
+ APB_DMA_CHAN_APB_SEQ
);
538 writel(apb_ptr
, ch
->addr
+ APB_DMA_CHAN_APB_PTR
);
539 writel(ahb_seq
, ch
->addr
+ APB_DMA_CHAN_AHB_SEQ
);
540 writel(ahb_ptr
, ch
->addr
+ APB_DMA_CHAN_AHB_PTR
);
543 writel(csr
, ch
->addr
+ APB_DMA_CHAN_CSR
);
545 req
->status
= TEGRA_DMA_REQ_INFLIGHT
;
548 static void handle_oneshot_dma(struct tegra_dma_channel
*ch
)
550 struct tegra_dma_req
*req
;
551 unsigned long irq_flags
;
553 spin_lock_irqsave(&ch
->lock
, irq_flags
);
554 if (list_empty(&ch
->list
)) {
555 spin_unlock_irqrestore(&ch
->lock
, irq_flags
);
559 req
= list_entry(ch
->list
.next
, typeof(*req
), node
);
561 int bytes_transferred
;
563 bytes_transferred
= ch
->req_transfer_count
;
564 bytes_transferred
+= 1;
565 bytes_transferred
<<= 2;
567 list_del(&req
->node
);
568 req
->bytes_transferred
= bytes_transferred
;
569 req
->status
= TEGRA_DMA_REQ_SUCCESS
;
571 spin_unlock_irqrestore(&ch
->lock
, irq_flags
);
572 /* Callback should be called without any lock */
573 pr_debug("%s: transferred %d bytes\n", __func__
,
574 req
->bytes_transferred
);
576 spin_lock_irqsave(&ch
->lock
, irq_flags
);
579 if (!list_empty(&ch
->list
)) {
580 req
= list_entry(ch
->list
.next
, typeof(*req
), node
);
581 /* the complete function we just called may have enqueued
582 another req, in which case dma has already started */
583 if (req
->status
!= TEGRA_DMA_REQ_INFLIGHT
)
584 tegra_dma_update_hw(ch
, req
);
586 spin_unlock_irqrestore(&ch
->lock
, irq_flags
);
589 static void handle_continuous_dma(struct tegra_dma_channel
*ch
)
591 struct tegra_dma_req
*req
;
592 unsigned long irq_flags
;
594 spin_lock_irqsave(&ch
->lock
, irq_flags
);
595 if (list_empty(&ch
->list
)) {
596 spin_unlock_irqrestore(&ch
->lock
, irq_flags
);
600 req
= list_entry(ch
->list
.next
, typeof(*req
), node
);
602 if (req
->buffer_status
== TEGRA_DMA_REQ_BUF_STATUS_EMPTY
) {
603 bool is_dma_ping_complete
;
604 is_dma_ping_complete
= (readl(ch
->addr
+ APB_DMA_CHAN_STA
)
605 & STA_PING_PONG
) ? true : false;
607 is_dma_ping_complete
= !is_dma_ping_complete
;
608 /* Out of sync - Release current buffer */
609 if (!is_dma_ping_complete
) {
610 int bytes_transferred
;
612 bytes_transferred
= ch
->req_transfer_count
;
613 bytes_transferred
+= 1;
614 bytes_transferred
<<= 3;
615 req
->buffer_status
= TEGRA_DMA_REQ_BUF_STATUS_FULL
;
616 req
->bytes_transferred
= bytes_transferred
;
617 req
->status
= TEGRA_DMA_REQ_SUCCESS
;
620 if (!list_is_last(&req
->node
, &ch
->list
)) {
621 struct tegra_dma_req
*next_req
;
623 next_req
= list_entry(req
->node
.next
,
624 typeof(*next_req
), node
);
625 tegra_dma_update_hw(ch
, next_req
);
628 list_del(&req
->node
);
630 /* DMA lock is NOT held when callbak is called */
631 spin_unlock_irqrestore(&ch
->lock
, irq_flags
);
635 /* Load the next request into the hardware, if available
637 if (!list_is_last(&req
->node
, &ch
->list
)) {
638 struct tegra_dma_req
*next_req
;
640 next_req
= list_entry(req
->node
.next
,
641 typeof(*next_req
), node
);
642 tegra_dma_update_hw_partial(ch
, next_req
);
644 req
->buffer_status
= TEGRA_DMA_REQ_BUF_STATUS_HALF_FULL
;
645 req
->status
= TEGRA_DMA_REQ_SUCCESS
;
646 /* DMA lock is NOT held when callback is called */
647 spin_unlock_irqrestore(&ch
->lock
, irq_flags
);
648 if (likely(req
->threshold
))
652 } else if (req
->buffer_status
==
653 TEGRA_DMA_REQ_BUF_STATUS_HALF_FULL
) {
654 /* Callback when the buffer is completely full (i.e on
655 * the second interrupt */
656 int bytes_transferred
;
658 bytes_transferred
= ch
->req_transfer_count
;
659 bytes_transferred
+= 1;
660 bytes_transferred
<<= 3;
662 req
->buffer_status
= TEGRA_DMA_REQ_BUF_STATUS_FULL
;
663 req
->bytes_transferred
= bytes_transferred
;
664 req
->status
= TEGRA_DMA_REQ_SUCCESS
;
665 list_del(&req
->node
);
667 /* DMA lock is NOT held when callbak is called */
668 spin_unlock_irqrestore(&ch
->lock
, irq_flags
);
676 spin_unlock_irqrestore(&ch
->lock
, irq_flags
);
679 static irqreturn_t
dma_isr(int irq
, void *data
)
681 struct tegra_dma_channel
*ch
= data
;
682 unsigned long status
;
684 status
= readl(ch
->addr
+ APB_DMA_CHAN_STA
);
685 if (status
& STA_ISE_EOC
)
686 writel(status
, ch
->addr
+ APB_DMA_CHAN_STA
);
688 pr_warning("Got a spurious ISR for DMA channel %d\n", ch
->id
);
691 return IRQ_WAKE_THREAD
;
694 static irqreturn_t
dma_thread_fn(int irq
, void *data
)
696 struct tegra_dma_channel
*ch
= data
;
698 if (ch
->mode
& TEGRA_DMA_MODE_ONESHOT
)
699 handle_oneshot_dma(ch
);
701 handle_continuous_dma(ch
);
707 int __init
tegra_dma_init(void)
715 bitmap_fill(channel_usage
, NV_DMA_MAX_CHANNELS
);
717 c
= clk_get_sys("tegra-apbdma", NULL
);
719 pr_err("Unable to get clock for APB DMA\n");
723 ret
= clk_prepare_enable(c
);
725 pr_err("Unable to enable clock for APB DMA\n");
729 addr
= IO_ADDRESS(TEGRA_APB_DMA_BASE
);
730 writel(GEN_ENABLE
, addr
+ APB_DMA_GEN
);
731 writel(0, addr
+ APB_DMA_CNTRL
);
732 writel(0xFFFFFFFFul
>> (31 - TEGRA_SYSTEM_DMA_CH_MAX
),
733 addr
+ APB_DMA_IRQ_MASK_SET
);
735 for (i
= TEGRA_SYSTEM_DMA_CH_MIN
; i
<= TEGRA_SYSTEM_DMA_CH_MAX
; i
++) {
736 struct tegra_dma_channel
*ch
= &dma_channels
[i
];
739 snprintf(ch
->name
, TEGRA_DMA_NAME_SIZE
, "dma_channel_%d", i
);
741 ch
->addr
= IO_ADDRESS(TEGRA_APB_DMA_CH0_BASE
+
742 TEGRA_APB_DMA_CH0_SIZE
* i
);
744 spin_lock_init(&ch
->lock
);
745 INIT_LIST_HEAD(&ch
->list
);
747 irq
= INT_APB_DMA_CH0
+ i
;
748 ret
= request_threaded_irq(irq
, dma_isr
, dma_thread_fn
, 0,
749 dma_channels
[i
].name
, ch
);
751 pr_err("Failed to register IRQ %d for DMA %d\n",
757 __clear_bit(i
, channel_usage
);
759 /* mark the shared channel allocated */
760 __set_bit(TEGRA_SYSTEM_DMA_CH_MIN
, channel_usage
);
762 tegra_dma_initialized
= true;
766 writel(0, addr
+ APB_DMA_GEN
);
767 for (i
= TEGRA_SYSTEM_DMA_CH_MIN
; i
<= TEGRA_SYSTEM_DMA_CH_MAX
; i
++) {
768 struct tegra_dma_channel
*ch
= &dma_channels
[i
];
770 free_irq(ch
->irq
, ch
);
774 postcore_initcall(tegra_dma_init
);
777 static u32 apb_dma
[5*TEGRA_SYSTEM_DMA_CH_NR
+ 3];
779 void tegra_dma_suspend(void)
781 void __iomem
*addr
= IO_ADDRESS(TEGRA_APB_DMA_BASE
);
785 *ctx
++ = readl(addr
+ APB_DMA_GEN
);
786 *ctx
++ = readl(addr
+ APB_DMA_CNTRL
);
787 *ctx
++ = readl(addr
+ APB_DMA_IRQ_MASK
);
789 for (i
= 0; i
< TEGRA_SYSTEM_DMA_CH_NR
; i
++) {
790 addr
= IO_ADDRESS(TEGRA_APB_DMA_CH0_BASE
+
791 TEGRA_APB_DMA_CH0_SIZE
* i
);
793 *ctx
++ = readl(addr
+ APB_DMA_CHAN_CSR
);
794 *ctx
++ = readl(addr
+ APB_DMA_CHAN_AHB_PTR
);
795 *ctx
++ = readl(addr
+ APB_DMA_CHAN_AHB_SEQ
);
796 *ctx
++ = readl(addr
+ APB_DMA_CHAN_APB_PTR
);
797 *ctx
++ = readl(addr
+ APB_DMA_CHAN_APB_SEQ
);
801 void tegra_dma_resume(void)
803 void __iomem
*addr
= IO_ADDRESS(TEGRA_APB_DMA_BASE
);
807 writel(*ctx
++, addr
+ APB_DMA_GEN
);
808 writel(*ctx
++, addr
+ APB_DMA_CNTRL
);
809 writel(*ctx
++, addr
+ APB_DMA_IRQ_MASK
);
811 for (i
= 0; i
< TEGRA_SYSTEM_DMA_CH_NR
; i
++) {
812 addr
= IO_ADDRESS(TEGRA_APB_DMA_CH0_BASE
+
813 TEGRA_APB_DMA_CH0_SIZE
* i
);
815 writel(*ctx
++, addr
+ APB_DMA_CHAN_CSR
);
816 writel(*ctx
++, addr
+ APB_DMA_CHAN_AHB_PTR
);
817 writel(*ctx
++, addr
+ APB_DMA_CHAN_AHB_SEQ
);
818 writel(*ctx
++, addr
+ APB_DMA_CHAN_APB_PTR
);
819 writel(*ctx
++, addr
+ APB_DMA_CHAN_APB_SEQ
);