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>
31 #include <mach/irqs.h>
32 #include <mach/iomap.h>
33 #include <mach/suspend.h>
35 #define APB_DMA_GEN 0x000
36 #define GEN_ENABLE (1<<31)
38 #define APB_DMA_CNTRL 0x010
40 #define APB_DMA_IRQ_MASK 0x01c
42 #define APB_DMA_IRQ_MASK_SET 0x020
44 #define APB_DMA_CHAN_CSR 0x000
45 #define CSR_ENB (1<<31)
46 #define CSR_IE_EOC (1<<30)
47 #define CSR_HOLD (1<<29)
48 #define CSR_DIR (1<<28)
49 #define CSR_ONCE (1<<27)
50 #define CSR_FLOW (1<<21)
51 #define CSR_REQ_SEL_SHIFT 16
52 #define CSR_REQ_SEL_MASK (0x1F<<CSR_REQ_SEL_SHIFT)
53 #define CSR_REQ_SEL_INVALID (31<<CSR_REQ_SEL_SHIFT)
54 #define CSR_WCOUNT_SHIFT 2
55 #define CSR_WCOUNT_MASK 0xFFFC
57 #define APB_DMA_CHAN_STA 0x004
58 #define STA_BUSY (1<<31)
59 #define STA_ISE_EOC (1<<30)
60 #define STA_HALT (1<<29)
61 #define STA_PING_PONG (1<<28)
62 #define STA_COUNT_SHIFT 2
63 #define STA_COUNT_MASK 0xFFFC
65 #define APB_DMA_CHAN_AHB_PTR 0x010
67 #define APB_DMA_CHAN_AHB_SEQ 0x014
68 #define AHB_SEQ_INTR_ENB (1<<31)
69 #define AHB_SEQ_BUS_WIDTH_SHIFT 28
70 #define AHB_SEQ_BUS_WIDTH_MASK (0x7<<AHB_SEQ_BUS_WIDTH_SHIFT)
71 #define AHB_SEQ_BUS_WIDTH_8 (0<<AHB_SEQ_BUS_WIDTH_SHIFT)
72 #define AHB_SEQ_BUS_WIDTH_16 (1<<AHB_SEQ_BUS_WIDTH_SHIFT)
73 #define AHB_SEQ_BUS_WIDTH_32 (2<<AHB_SEQ_BUS_WIDTH_SHIFT)
74 #define AHB_SEQ_BUS_WIDTH_64 (3<<AHB_SEQ_BUS_WIDTH_SHIFT)
75 #define AHB_SEQ_BUS_WIDTH_128 (4<<AHB_SEQ_BUS_WIDTH_SHIFT)
76 #define AHB_SEQ_DATA_SWAP (1<<27)
77 #define AHB_SEQ_BURST_MASK (0x7<<24)
78 #define AHB_SEQ_BURST_1 (4<<24)
79 #define AHB_SEQ_BURST_4 (5<<24)
80 #define AHB_SEQ_BURST_8 (6<<24)
81 #define AHB_SEQ_DBL_BUF (1<<19)
82 #define AHB_SEQ_WRAP_SHIFT 16
83 #define AHB_SEQ_WRAP_MASK (0x7<<AHB_SEQ_WRAP_SHIFT)
85 #define APB_DMA_CHAN_APB_PTR 0x018
87 #define APB_DMA_CHAN_APB_SEQ 0x01c
88 #define APB_SEQ_BUS_WIDTH_SHIFT 28
89 #define APB_SEQ_BUS_WIDTH_MASK (0x7<<APB_SEQ_BUS_WIDTH_SHIFT)
90 #define APB_SEQ_BUS_WIDTH_8 (0<<APB_SEQ_BUS_WIDTH_SHIFT)
91 #define APB_SEQ_BUS_WIDTH_16 (1<<APB_SEQ_BUS_WIDTH_SHIFT)
92 #define APB_SEQ_BUS_WIDTH_32 (2<<APB_SEQ_BUS_WIDTH_SHIFT)
93 #define APB_SEQ_BUS_WIDTH_64 (3<<APB_SEQ_BUS_WIDTH_SHIFT)
94 #define APB_SEQ_BUS_WIDTH_128 (4<<APB_SEQ_BUS_WIDTH_SHIFT)
95 #define APB_SEQ_DATA_SWAP (1<<27)
96 #define APB_SEQ_WRAP_SHIFT 16
97 #define APB_SEQ_WRAP_MASK (0x7<<APB_SEQ_WRAP_SHIFT)
99 #define TEGRA_SYSTEM_DMA_CH_NR 16
100 #define TEGRA_SYSTEM_DMA_AVP_CH_NUM 4
101 #define TEGRA_SYSTEM_DMA_CH_MIN 0
102 #define TEGRA_SYSTEM_DMA_CH_MAX \
103 (TEGRA_SYSTEM_DMA_CH_NR - TEGRA_SYSTEM_DMA_AVP_CH_NUM - 1)
105 #define NV_DMA_MAX_TRASFER_SIZE 0x10000
107 const unsigned int ahb_addr_wrap_table
[8] = {
108 0, 32, 64, 128, 256, 512, 1024, 2048
111 const unsigned int apb_addr_wrap_table
[8] = {0, 1, 2, 4, 8, 16, 32, 64};
113 const unsigned int bus_width_table
[5] = {8, 16, 32, 64, 128};
115 #define TEGRA_DMA_NAME_SIZE 16
116 struct tegra_dma_channel
{
117 struct list_head list
;
120 char name
[TEGRA_DMA_NAME_SIZE
];
124 int req_transfer_count
;
127 #define NV_DMA_MAX_CHANNELS 32
129 static DEFINE_MUTEX(tegra_dma_lock
);
131 static DECLARE_BITMAP(channel_usage
, NV_DMA_MAX_CHANNELS
);
132 static struct tegra_dma_channel dma_channels
[NV_DMA_MAX_CHANNELS
];
134 static void tegra_dma_update_hw(struct tegra_dma_channel
*ch
,
135 struct tegra_dma_req
*req
);
136 static void tegra_dma_update_hw_partial(struct tegra_dma_channel
*ch
,
137 struct tegra_dma_req
*req
);
138 static void tegra_dma_stop(struct tegra_dma_channel
*ch
);
140 void tegra_dma_flush(struct tegra_dma_channel
*ch
)
143 EXPORT_SYMBOL(tegra_dma_flush
);
145 void tegra_dma_dequeue(struct tegra_dma_channel
*ch
)
147 struct tegra_dma_req
*req
;
149 if (tegra_dma_is_empty(ch
))
152 req
= list_entry(ch
->list
.next
, typeof(*req
), node
);
154 tegra_dma_dequeue_req(ch
, req
);
158 void tegra_dma_stop(struct tegra_dma_channel
*ch
)
163 csr
= readl(ch
->addr
+ APB_DMA_CHAN_CSR
);
165 writel(csr
, ch
->addr
+ APB_DMA_CHAN_CSR
);
168 writel(csr
, ch
->addr
+ APB_DMA_CHAN_CSR
);
170 status
= readl(ch
->addr
+ APB_DMA_CHAN_STA
);
171 if (status
& STA_ISE_EOC
)
172 writel(status
, ch
->addr
+ APB_DMA_CHAN_STA
);
175 int tegra_dma_cancel(struct tegra_dma_channel
*ch
)
178 unsigned long irq_flags
;
180 spin_lock_irqsave(&ch
->lock
, irq_flags
);
181 while (!list_empty(&ch
->list
))
182 list_del(ch
->list
.next
);
184 csr
= readl(ch
->addr
+ APB_DMA_CHAN_CSR
);
185 csr
&= ~CSR_REQ_SEL_MASK
;
186 csr
|= CSR_REQ_SEL_INVALID
;
187 writel(csr
, ch
->addr
+ APB_DMA_CHAN_CSR
);
191 spin_unlock_irqrestore(&ch
->lock
, irq_flags
);
195 int tegra_dma_dequeue_req(struct tegra_dma_channel
*ch
,
196 struct tegra_dma_req
*_req
)
200 struct tegra_dma_req
*req
= NULL
;
202 unsigned long irq_flags
;
204 int req_transfer_count
;
206 spin_lock_irqsave(&ch
->lock
, irq_flags
);
207 list_for_each_entry(req
, &ch
->list
, node
) {
209 list_del(&req
->node
);
215 spin_unlock_irqrestore(&ch
->lock
, irq_flags
);
219 /* STOP the DMA and get the transfer count.
220 * Getting the transfer count is tricky.
221 * - Change the source selector to invalid to stop the DMA from
223 * - Read the status register to know the number of pending
224 * bytes to be transfered.
225 * - Finally stop or program the DMA to the next buffer in the
228 csr
= readl(ch
->addr
+ APB_DMA_CHAN_CSR
);
229 csr
&= ~CSR_REQ_SEL_MASK
;
230 csr
|= CSR_REQ_SEL_INVALID
;
231 writel(csr
, ch
->addr
+ APB_DMA_CHAN_CSR
);
233 /* Get the transfer count */
234 status
= readl(ch
->addr
+ APB_DMA_CHAN_STA
);
235 to_transfer
= (status
& STA_COUNT_MASK
) >> STA_COUNT_SHIFT
;
236 req_transfer_count
= ch
->req_transfer_count
;
237 req_transfer_count
+= 1;
240 req
->bytes_transferred
= req_transfer_count
;
242 if (status
& STA_BUSY
)
243 req
->bytes_transferred
-= to_transfer
;
245 /* In continous transfer mode, DMA only tracks the count of the
246 * half DMA buffer. So, if the DMA already finished half the DMA
247 * then add the half buffer to the completed count.
249 * FIXME: There can be a race here. What if the req to
250 * dequue happens at the same time as the DMA just moved to
251 * the new buffer and SW didn't yet received the interrupt?
253 if (ch
->mode
& TEGRA_DMA_MODE_CONTINOUS
)
254 if (req
->buffer_status
== TEGRA_DMA_REQ_BUF_STATUS_HALF_FULL
)
255 req
->bytes_transferred
+= req_transfer_count
;
257 req
->bytes_transferred
*= 4;
260 if (!list_empty(&ch
->list
)) {
261 /* if the list is not empty, queue the next request */
262 struct tegra_dma_req
*next_req
;
263 next_req
= list_entry(ch
->list
.next
,
264 typeof(*next_req
), node
);
265 tegra_dma_update_hw(ch
, next_req
);
267 req
->status
= -TEGRA_DMA_REQ_ERROR_ABORTED
;
269 spin_unlock_irqrestore(&ch
->lock
, irq_flags
);
271 /* Callback should be called without any lock */
275 EXPORT_SYMBOL(tegra_dma_dequeue_req
);
277 bool tegra_dma_is_empty(struct tegra_dma_channel
*ch
)
279 unsigned long irq_flags
;
282 spin_lock_irqsave(&ch
->lock
, irq_flags
);
283 if (list_empty(&ch
->list
))
287 spin_unlock_irqrestore(&ch
->lock
, irq_flags
);
290 EXPORT_SYMBOL(tegra_dma_is_empty
);
292 bool tegra_dma_is_req_inflight(struct tegra_dma_channel
*ch
,
293 struct tegra_dma_req
*_req
)
295 unsigned long irq_flags
;
296 struct tegra_dma_req
*req
;
298 spin_lock_irqsave(&ch
->lock
, irq_flags
);
299 list_for_each_entry(req
, &ch
->list
, node
) {
301 spin_unlock_irqrestore(&ch
->lock
, irq_flags
);
305 spin_unlock_irqrestore(&ch
->lock
, irq_flags
);
308 EXPORT_SYMBOL(tegra_dma_is_req_inflight
);
310 int tegra_dma_enqueue_req(struct tegra_dma_channel
*ch
,
311 struct tegra_dma_req
*req
)
313 unsigned long irq_flags
;
314 struct tegra_dma_req
*_req
;
317 if (req
->size
> NV_DMA_MAX_TRASFER_SIZE
||
318 req
->source_addr
& 0x3 || req
->dest_addr
& 0x3) {
319 pr_err("Invalid DMA request for channel %d\n", ch
->id
);
323 spin_lock_irqsave(&ch
->lock
, irq_flags
);
325 list_for_each_entry(_req
, &ch
->list
, node
) {
327 spin_unlock_irqrestore(&ch
->lock
, irq_flags
);
332 req
->bytes_transferred
= 0;
334 req
->buffer_status
= 0;
335 if (list_empty(&ch
->list
))
338 list_add_tail(&req
->node
, &ch
->list
);
341 tegra_dma_update_hw(ch
, req
);
343 spin_unlock_irqrestore(&ch
->lock
, irq_flags
);
347 EXPORT_SYMBOL(tegra_dma_enqueue_req
);
349 struct tegra_dma_channel
*tegra_dma_allocate_channel(int mode
)
352 struct tegra_dma_channel
*ch
= NULL
;
354 mutex_lock(&tegra_dma_lock
);
356 /* first channel is the shared channel */
357 if (mode
& TEGRA_DMA_SHARED
) {
358 channel
= TEGRA_SYSTEM_DMA_CH_MIN
;
360 channel
= find_first_zero_bit(channel_usage
,
361 ARRAY_SIZE(dma_channels
));
362 if (channel
>= ARRAY_SIZE(dma_channels
))
365 __set_bit(channel
, channel_usage
);
366 ch
= &dma_channels
[channel
];
370 mutex_unlock(&tegra_dma_lock
);
373 EXPORT_SYMBOL(tegra_dma_allocate_channel
);
375 void tegra_dma_free_channel(struct tegra_dma_channel
*ch
)
377 if (ch
->mode
& TEGRA_DMA_SHARED
)
379 tegra_dma_cancel(ch
);
380 mutex_lock(&tegra_dma_lock
);
381 __clear_bit(ch
->id
, channel_usage
);
382 mutex_unlock(&tegra_dma_lock
);
384 EXPORT_SYMBOL(tegra_dma_free_channel
);
386 static void tegra_dma_update_hw_partial(struct tegra_dma_channel
*ch
,
387 struct tegra_dma_req
*req
)
392 if (req
->to_memory
) {
393 apb_ptr
= req
->source_addr
;
394 ahb_ptr
= req
->dest_addr
;
396 apb_ptr
= req
->dest_addr
;
397 ahb_ptr
= req
->source_addr
;
399 writel(apb_ptr
, ch
->addr
+ APB_DMA_CHAN_APB_PTR
);
400 writel(ahb_ptr
, ch
->addr
+ APB_DMA_CHAN_AHB_PTR
);
402 req
->status
= TEGRA_DMA_REQ_INFLIGHT
;
406 static void tegra_dma_update_hw(struct tegra_dma_channel
*ch
,
407 struct tegra_dma_req
*req
)
421 csr
= CSR_IE_EOC
| CSR_FLOW
;
422 ahb_seq
= AHB_SEQ_INTR_ENB
| AHB_SEQ_BURST_1
;
425 csr
|= req
->req_sel
<< CSR_REQ_SEL_SHIFT
;
427 /* One shot mode is always single buffered,
428 * continuous mode is always double buffered
430 if (ch
->mode
& TEGRA_DMA_MODE_ONESHOT
) {
432 ch
->req_transfer_count
= (req
->size
>> 2) - 1;
434 ahb_seq
|= AHB_SEQ_DBL_BUF
;
436 /* In double buffered mode, we set the size to half the
437 * requested size and interrupt when half the buffer
439 ch
->req_transfer_count
= (req
->size
>> 3) - 1;
442 csr
|= ch
->req_transfer_count
<< CSR_WCOUNT_SHIFT
;
444 if (req
->to_memory
) {
445 apb_ptr
= req
->source_addr
;
446 ahb_ptr
= req
->dest_addr
;
448 apb_addr_wrap
= req
->source_wrap
;
449 ahb_addr_wrap
= req
->dest_wrap
;
450 apb_bus_width
= req
->source_bus_width
;
451 ahb_bus_width
= req
->dest_bus_width
;
455 apb_ptr
= req
->dest_addr
;
456 ahb_ptr
= req
->source_addr
;
458 apb_addr_wrap
= req
->dest_wrap
;
459 ahb_addr_wrap
= req
->source_wrap
;
460 apb_bus_width
= req
->dest_bus_width
;
461 ahb_bus_width
= req
->source_bus_width
;
467 /* set address wrap for APB size */
470 if (apb_addr_wrap_table
[index
] == apb_addr_wrap
)
473 } while (index
< ARRAY_SIZE(apb_addr_wrap_table
));
474 BUG_ON(index
== ARRAY_SIZE(apb_addr_wrap_table
));
475 apb_seq
|= index
<< APB_SEQ_WRAP_SHIFT
;
477 /* set address wrap for AHB size */
480 if (ahb_addr_wrap_table
[index
] == ahb_addr_wrap
)
483 } while (index
< ARRAY_SIZE(ahb_addr_wrap_table
));
484 BUG_ON(index
== ARRAY_SIZE(ahb_addr_wrap_table
));
485 ahb_seq
|= index
<< AHB_SEQ_WRAP_SHIFT
;
487 for (index
= 0; index
< ARRAY_SIZE(bus_width_table
); index
++) {
488 if (bus_width_table
[index
] == ahb_bus_width
)
491 BUG_ON(index
== ARRAY_SIZE(bus_width_table
));
492 ahb_seq
|= index
<< AHB_SEQ_BUS_WIDTH_SHIFT
;
494 for (index
= 0; index
< ARRAY_SIZE(bus_width_table
); index
++) {
495 if (bus_width_table
[index
] == apb_bus_width
)
498 BUG_ON(index
== ARRAY_SIZE(bus_width_table
));
499 apb_seq
|= index
<< APB_SEQ_BUS_WIDTH_SHIFT
;
501 writel(csr
, ch
->addr
+ APB_DMA_CHAN_CSR
);
502 writel(apb_seq
, ch
->addr
+ APB_DMA_CHAN_APB_SEQ
);
503 writel(apb_ptr
, ch
->addr
+ APB_DMA_CHAN_APB_PTR
);
504 writel(ahb_seq
, ch
->addr
+ APB_DMA_CHAN_AHB_SEQ
);
505 writel(ahb_ptr
, ch
->addr
+ APB_DMA_CHAN_AHB_PTR
);
508 writel(csr
, ch
->addr
+ APB_DMA_CHAN_CSR
);
510 req
->status
= TEGRA_DMA_REQ_INFLIGHT
;
513 static void handle_oneshot_dma(struct tegra_dma_channel
*ch
)
515 struct tegra_dma_req
*req
;
516 unsigned long irq_flags
;
518 spin_lock_irqsave(&ch
->lock
, irq_flags
);
519 if (list_empty(&ch
->list
)) {
520 spin_unlock_irqrestore(&ch
->lock
, irq_flags
);
524 req
= list_entry(ch
->list
.next
, typeof(*req
), node
);
526 int bytes_transferred
;
528 bytes_transferred
= ch
->req_transfer_count
;
529 bytes_transferred
+= 1;
530 bytes_transferred
<<= 2;
532 list_del(&req
->node
);
533 req
->bytes_transferred
= bytes_transferred
;
534 req
->status
= TEGRA_DMA_REQ_SUCCESS
;
536 spin_unlock_irqrestore(&ch
->lock
, irq_flags
);
537 /* Callback should be called without any lock */
538 pr_debug("%s: transferred %d bytes\n", __func__
,
539 req
->bytes_transferred
);
541 spin_lock_irqsave(&ch
->lock
, irq_flags
);
544 if (!list_empty(&ch
->list
)) {
545 req
= list_entry(ch
->list
.next
, typeof(*req
), node
);
546 /* the complete function we just called may have enqueued
547 another req, in which case dma has already started */
548 if (req
->status
!= TEGRA_DMA_REQ_INFLIGHT
)
549 tegra_dma_update_hw(ch
, req
);
551 spin_unlock_irqrestore(&ch
->lock
, irq_flags
);
554 static void handle_continuous_dma(struct tegra_dma_channel
*ch
)
556 struct tegra_dma_req
*req
;
557 unsigned long irq_flags
;
559 spin_lock_irqsave(&ch
->lock
, irq_flags
);
560 if (list_empty(&ch
->list
)) {
561 spin_unlock_irqrestore(&ch
->lock
, irq_flags
);
565 req
= list_entry(ch
->list
.next
, typeof(*req
), node
);
567 if (req
->buffer_status
== TEGRA_DMA_REQ_BUF_STATUS_EMPTY
) {
568 bool is_dma_ping_complete
;
569 is_dma_ping_complete
= (readl(ch
->addr
+ APB_DMA_CHAN_STA
)
570 & STA_PING_PONG
) ? true : false;
572 is_dma_ping_complete
= !is_dma_ping_complete
;
573 /* Out of sync - Release current buffer */
574 if (!is_dma_ping_complete
) {
575 int bytes_transferred
;
577 bytes_transferred
= ch
->req_transfer_count
;
578 bytes_transferred
+= 1;
579 bytes_transferred
<<= 3;
580 req
->buffer_status
= TEGRA_DMA_REQ_BUF_STATUS_FULL
;
581 req
->bytes_transferred
= bytes_transferred
;
582 req
->status
= TEGRA_DMA_REQ_SUCCESS
;
585 if (!list_is_last(&req
->node
, &ch
->list
)) {
586 struct tegra_dma_req
*next_req
;
588 next_req
= list_entry(req
->node
.next
,
589 typeof(*next_req
), node
);
590 tegra_dma_update_hw(ch
, next_req
);
593 list_del(&req
->node
);
595 /* DMA lock is NOT held when callbak is called */
596 spin_unlock_irqrestore(&ch
->lock
, irq_flags
);
600 /* Load the next request into the hardware, if available
602 if (!list_is_last(&req
->node
, &ch
->list
)) {
603 struct tegra_dma_req
*next_req
;
605 next_req
= list_entry(req
->node
.next
,
606 typeof(*next_req
), node
);
607 tegra_dma_update_hw_partial(ch
, next_req
);
609 req
->buffer_status
= TEGRA_DMA_REQ_BUF_STATUS_HALF_FULL
;
610 req
->status
= TEGRA_DMA_REQ_SUCCESS
;
611 /* DMA lock is NOT held when callback is called */
612 spin_unlock_irqrestore(&ch
->lock
, irq_flags
);
613 if (likely(req
->threshold
))
617 } else if (req
->buffer_status
==
618 TEGRA_DMA_REQ_BUF_STATUS_HALF_FULL
) {
619 /* Callback when the buffer is completely full (i.e on
620 * the second interrupt */
621 int bytes_transferred
;
623 bytes_transferred
= ch
->req_transfer_count
;
624 bytes_transferred
+= 1;
625 bytes_transferred
<<= 3;
627 req
->buffer_status
= TEGRA_DMA_REQ_BUF_STATUS_FULL
;
628 req
->bytes_transferred
= bytes_transferred
;
629 req
->status
= TEGRA_DMA_REQ_SUCCESS
;
630 list_del(&req
->node
);
632 /* DMA lock is NOT held when callbak is called */
633 spin_unlock_irqrestore(&ch
->lock
, irq_flags
);
641 spin_unlock_irqrestore(&ch
->lock
, irq_flags
);
644 static irqreturn_t
dma_isr(int irq
, void *data
)
646 struct tegra_dma_channel
*ch
= data
;
647 unsigned long status
;
649 status
= readl(ch
->addr
+ APB_DMA_CHAN_STA
);
650 if (status
& STA_ISE_EOC
)
651 writel(status
, ch
->addr
+ APB_DMA_CHAN_STA
);
653 pr_warning("Got a spurious ISR for DMA channel %d\n", ch
->id
);
656 return IRQ_WAKE_THREAD
;
659 static irqreturn_t
dma_thread_fn(int irq
, void *data
)
661 struct tegra_dma_channel
*ch
= data
;
663 if (ch
->mode
& TEGRA_DMA_MODE_ONESHOT
)
664 handle_oneshot_dma(ch
);
666 handle_continuous_dma(ch
);
672 int __init
tegra_dma_init(void)
679 addr
= IO_ADDRESS(TEGRA_APB_DMA_BASE
);
680 writel(GEN_ENABLE
, addr
+ APB_DMA_GEN
);
681 writel(0, addr
+ APB_DMA_CNTRL
);
682 writel(0xFFFFFFFFul
>> (31 - TEGRA_SYSTEM_DMA_CH_MAX
),
683 addr
+ APB_DMA_IRQ_MASK_SET
);
685 memset(channel_usage
, 0, sizeof(channel_usage
));
686 memset(dma_channels
, 0, sizeof(dma_channels
));
688 /* Reserve all the channels we are not supposed to touch */
689 for (i
= 0; i
< TEGRA_SYSTEM_DMA_CH_MIN
; i
++)
690 __set_bit(i
, channel_usage
);
692 for (i
= TEGRA_SYSTEM_DMA_CH_MIN
; i
<= TEGRA_SYSTEM_DMA_CH_MAX
; i
++) {
693 struct tegra_dma_channel
*ch
= &dma_channels
[i
];
695 __clear_bit(i
, channel_usage
);
698 snprintf(ch
->name
, TEGRA_DMA_NAME_SIZE
, "dma_channel_%d", i
);
700 ch
->addr
= IO_ADDRESS(TEGRA_APB_DMA_CH0_BASE
+
701 TEGRA_APB_DMA_CH0_SIZE
* i
);
703 spin_lock_init(&ch
->lock
);
704 INIT_LIST_HEAD(&ch
->list
);
706 irq
= INT_APB_DMA_CH0
+ i
;
707 ret
= request_threaded_irq(irq
, dma_isr
, dma_thread_fn
, 0,
708 dma_channels
[i
].name
, ch
);
710 pr_err("Failed to register IRQ %d for DMA %d\n",
716 /* mark the shared channel allocated */
717 __set_bit(TEGRA_SYSTEM_DMA_CH_MIN
, channel_usage
);
719 for (i
= TEGRA_SYSTEM_DMA_CH_MAX
+1; i
< NV_DMA_MAX_CHANNELS
; i
++)
720 __set_bit(i
, channel_usage
);
724 writel(0, addr
+ APB_DMA_GEN
);
725 for (i
= TEGRA_SYSTEM_DMA_CH_MIN
; i
<= TEGRA_SYSTEM_DMA_CH_MAX
; i
++) {
726 struct tegra_dma_channel
*ch
= &dma_channels
[i
];
728 free_irq(ch
->irq
, ch
);
734 static u32 apb_dma
[5*TEGRA_SYSTEM_DMA_CH_NR
+ 3];
736 void tegra_dma_suspend(void)
738 void __iomem
*addr
= IO_ADDRESS(TEGRA_APB_DMA_BASE
);
742 *ctx
++ = readl(addr
+ APB_DMA_GEN
);
743 *ctx
++ = readl(addr
+ APB_DMA_CNTRL
);
744 *ctx
++ = readl(addr
+ APB_DMA_IRQ_MASK
);
746 for (i
= 0; i
< TEGRA_SYSTEM_DMA_CH_NR
; i
++) {
747 addr
= IO_ADDRESS(TEGRA_APB_DMA_CH0_BASE
+
748 TEGRA_APB_DMA_CH0_SIZE
* i
);
750 *ctx
++ = readl(addr
+ APB_DMA_CHAN_CSR
);
751 *ctx
++ = readl(addr
+ APB_DMA_CHAN_AHB_PTR
);
752 *ctx
++ = readl(addr
+ APB_DMA_CHAN_AHB_SEQ
);
753 *ctx
++ = readl(addr
+ APB_DMA_CHAN_APB_PTR
);
754 *ctx
++ = readl(addr
+ APB_DMA_CHAN_APB_SEQ
);
758 void tegra_dma_resume(void)
760 void __iomem
*addr
= IO_ADDRESS(TEGRA_APB_DMA_BASE
);
764 writel(*ctx
++, addr
+ APB_DMA_GEN
);
765 writel(*ctx
++, addr
+ APB_DMA_CNTRL
);
766 writel(*ctx
++, addr
+ APB_DMA_IRQ_MASK
);
768 for (i
= 0; i
< TEGRA_SYSTEM_DMA_CH_NR
; i
++) {
769 addr
= IO_ADDRESS(TEGRA_APB_DMA_CH0_BASE
+
770 TEGRA_APB_DMA_CH0_SIZE
* i
);
772 writel(*ctx
++, addr
+ APB_DMA_CHAN_CSR
);
773 writel(*ctx
++, addr
+ APB_DMA_CHAN_AHB_PTR
);
774 writel(*ctx
++, addr
+ APB_DMA_CHAN_AHB_SEQ
);
775 writel(*ctx
++, addr
+ APB_DMA_CHAN_APB_PTR
);
776 writel(*ctx
++, addr
+ APB_DMA_CHAN_APB_SEQ
);