ARM: tegra: Prevent requeuing in-progress DMA requests
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / arch / arm / mach-tegra / dma.c
blob4625e3ae766ec20e8ae938bbd941ad1aa22f8e9d
1 /*
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
16 * more details.
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.
23 #include <linux/io.h>
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 <mach/dma.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;
118 int id;
119 spinlock_t lock;
120 char name[TEGRA_DMA_NAME_SIZE];
121 void __iomem *addr;
122 int mode;
123 int irq;
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))
150 return;
152 req = list_entry(ch->list.next, typeof(*req), node);
154 tegra_dma_dequeue_req(ch, req);
155 return;
158 void tegra_dma_stop(struct tegra_dma_channel *ch)
160 u32 csr;
161 u32 status;
163 csr = readl(ch->addr + APB_DMA_CHAN_CSR);
164 csr &= ~CSR_IE_EOC;
165 writel(csr, ch->addr + APB_DMA_CHAN_CSR);
167 csr &= ~CSR_ENB;
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)
177 u32 csr;
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);
189 tegra_dma_stop(ch);
191 spin_unlock_irqrestore(&ch->lock, irq_flags);
192 return 0;
195 int tegra_dma_dequeue_req(struct tegra_dma_channel *ch,
196 struct tegra_dma_req *_req)
198 unsigned int csr;
199 unsigned int status;
200 struct tegra_dma_req *req = NULL;
201 int found = 0;
202 unsigned long irq_flags;
203 int to_transfer;
204 int req_transfer_count;
206 spin_lock_irqsave(&ch->lock, irq_flags);
207 list_for_each_entry(req, &ch->list, node) {
208 if (req == _req) {
209 list_del(&req->node);
210 found = 1;
211 break;
214 if (!found) {
215 spin_unlock_irqrestore(&ch->lock, irq_flags);
216 return 0;
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
222 * FIFO to memory.
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
226 * list.
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;
238 to_transfer += 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;
259 tegra_dma_stop(ch);
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 */
272 req->complete(req);
273 return 0;
275 EXPORT_SYMBOL(tegra_dma_dequeue_req);
277 bool tegra_dma_is_empty(struct tegra_dma_channel *ch)
279 unsigned long irq_flags;
280 bool is_empty;
282 spin_lock_irqsave(&ch->lock, irq_flags);
283 if (list_empty(&ch->list))
284 is_empty = true;
285 else
286 is_empty = false;
287 spin_unlock_irqrestore(&ch->lock, irq_flags);
288 return is_empty;
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) {
300 if (req == _req) {
301 spin_unlock_irqrestore(&ch->lock, irq_flags);
302 return true;
305 spin_unlock_irqrestore(&ch->lock, irq_flags);
306 return false;
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;
315 int start_dma = 0;
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);
320 return -EINVAL;
323 spin_lock_irqsave(&ch->lock, irq_flags);
325 list_for_each_entry(_req, &ch->list, node) {
326 if (req == _req) {
327 spin_unlock_irqrestore(&ch->lock, irq_flags);
328 return -EEXIST;
332 req->bytes_transferred = 0;
333 req->status = 0;
334 req->buffer_status = 0;
335 if (list_empty(&ch->list))
336 start_dma = 1;
338 list_add_tail(&req->node, &ch->list);
340 if (start_dma)
341 tegra_dma_update_hw(ch, req);
343 spin_unlock_irqrestore(&ch->lock, irq_flags);
345 return 0;
347 EXPORT_SYMBOL(tegra_dma_enqueue_req);
349 struct tegra_dma_channel *tegra_dma_allocate_channel(int mode)
351 int channel;
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;
359 } else {
360 channel = find_first_zero_bit(channel_usage,
361 ARRAY_SIZE(dma_channels));
362 if (channel >= ARRAY_SIZE(dma_channels))
363 goto out;
365 __set_bit(channel, channel_usage);
366 ch = &dma_channels[channel];
367 ch->mode = mode;
369 out:
370 mutex_unlock(&tegra_dma_lock);
371 return ch;
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)
378 return;
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)
389 u32 apb_ptr;
390 u32 ahb_ptr;
392 if (req->to_memory) {
393 apb_ptr = req->source_addr;
394 ahb_ptr = req->dest_addr;
395 } else {
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;
403 return;
406 static void tegra_dma_update_hw(struct tegra_dma_channel *ch,
407 struct tegra_dma_req *req)
409 int ahb_addr_wrap;
410 int apb_addr_wrap;
411 int ahb_bus_width;
412 int apb_bus_width;
413 int index;
415 u32 ahb_seq;
416 u32 apb_seq;
417 u32 ahb_ptr;
418 u32 apb_ptr;
419 u32 csr;
421 csr = CSR_IE_EOC | CSR_FLOW;
422 ahb_seq = AHB_SEQ_INTR_ENB | AHB_SEQ_BURST_1;
423 apb_seq = 0;
425 csr |= req->req_sel << CSR_REQ_SEL_SHIFT;
427 /* One shot mode is always single buffered,
428 * continuous mode is always double buffered
429 * */
430 if (ch->mode & TEGRA_DMA_MODE_ONESHOT) {
431 csr |= CSR_ONCE;
432 ch->req_transfer_count = (req->size >> 2) - 1;
433 } else {
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
438 * is full */
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;
453 } else {
454 csr |= CSR_DIR;
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;
464 apb_addr_wrap >>= 2;
465 ahb_addr_wrap >>= 2;
467 /* set address wrap for APB size */
468 index = 0;
469 do {
470 if (apb_addr_wrap_table[index] == apb_addr_wrap)
471 break;
472 index++;
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 */
478 index = 0;
479 do {
480 if (ahb_addr_wrap_table[index] == ahb_addr_wrap)
481 break;
482 index++;
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)
489 break;
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)
496 break;
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);
507 csr |= CSR_ENB;
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);
521 return;
524 req = list_entry(ch->list.next, typeof(*req), node);
525 if (req) {
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);
540 req->complete(req);
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);
562 return;
565 req = list_entry(ch->list.next, typeof(*req), node);
566 if (req) {
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;
571 if (req->to_memory)
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;
583 tegra_dma_stop(ch);
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);
597 req->complete(req);
598 return;
600 /* Load the next request into the hardware, if available
601 * */
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))
614 req->threshold(req);
615 return;
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);
634 req->complete(req);
635 return;
637 } else {
638 BUG();
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);
652 else {
653 pr_warning("Got a spurious ISR for DMA channel %d\n", ch->id);
654 return IRQ_HANDLED;
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);
665 else
666 handle_continuous_dma(ch);
669 return IRQ_HANDLED;
672 int __init tegra_dma_init(void)
674 int ret = 0;
675 int i;
676 unsigned int irq;
677 void __iomem *addr;
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);
697 ch->id = i;
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);
709 if (ret) {
710 pr_err("Failed to register IRQ %d for DMA %d\n",
711 irq, i);
712 goto fail;
714 ch->irq = irq;
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);
722 return ret;
723 fail:
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];
727 if (ch->irq)
728 free_irq(ch->irq, ch);
730 return ret;
733 #ifdef CONFIG_PM
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);
739 u32 *ctx = apb_dma;
740 int i;
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);
761 u32 *ctx = apb_dma;
762 int i;
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);
780 #endif