kvm tools, setup: Create private directory
[linux-2.6/next.git] / arch / arm / mach-tegra / dma.c
blobf4ef5eb317bd4311eaa0f67f15842d450d00b991
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 <linux/clk.h>
31 #include <mach/dma.h>
32 #include <mach/irqs.h>
33 #include <mach/iomap.h>
34 #include <mach/suspend.h>
36 #define APB_DMA_GEN 0x000
37 #define GEN_ENABLE (1<<31)
39 #define APB_DMA_CNTRL 0x010
41 #define APB_DMA_IRQ_MASK 0x01c
43 #define APB_DMA_IRQ_MASK_SET 0x020
45 #define APB_DMA_CHAN_CSR 0x000
46 #define CSR_ENB (1<<31)
47 #define CSR_IE_EOC (1<<30)
48 #define CSR_HOLD (1<<29)
49 #define CSR_DIR (1<<28)
50 #define CSR_ONCE (1<<27)
51 #define CSR_FLOW (1<<21)
52 #define CSR_REQ_SEL_SHIFT 16
53 #define CSR_REQ_SEL_MASK (0x1F<<CSR_REQ_SEL_SHIFT)
54 #define CSR_REQ_SEL_INVALID (31<<CSR_REQ_SEL_SHIFT)
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 const unsigned int ahb_addr_wrap_table[8] = {
109 0, 32, 64, 128, 256, 512, 1024, 2048
112 const unsigned int apb_addr_wrap_table[8] = {0, 1, 2, 4, 8, 16, 32, 64};
114 const unsigned int bus_width_table[5] = {8, 16, 32, 64, 128};
116 #define TEGRA_DMA_NAME_SIZE 16
117 struct tegra_dma_channel {
118 struct list_head list;
119 int id;
120 spinlock_t lock;
121 char name[TEGRA_DMA_NAME_SIZE];
122 void __iomem *addr;
123 int mode;
124 int irq;
125 int req_transfer_count;
128 #define NV_DMA_MAX_CHANNELS 32
130 static bool tegra_dma_initialized;
131 static DEFINE_MUTEX(tegra_dma_lock);
133 static DECLARE_BITMAP(channel_usage, NV_DMA_MAX_CHANNELS);
134 static struct tegra_dma_channel dma_channels[NV_DMA_MAX_CHANNELS];
136 static void tegra_dma_update_hw(struct tegra_dma_channel *ch,
137 struct tegra_dma_req *req);
138 static void tegra_dma_update_hw_partial(struct tegra_dma_channel *ch,
139 struct tegra_dma_req *req);
140 static void tegra_dma_stop(struct tegra_dma_channel *ch);
142 void tegra_dma_flush(struct tegra_dma_channel *ch)
145 EXPORT_SYMBOL(tegra_dma_flush);
147 void tegra_dma_dequeue(struct tegra_dma_channel *ch)
149 struct tegra_dma_req *req;
151 if (tegra_dma_is_empty(ch))
152 return;
154 req = list_entry(ch->list.next, typeof(*req), node);
156 tegra_dma_dequeue_req(ch, req);
157 return;
160 void tegra_dma_stop(struct tegra_dma_channel *ch)
162 u32 csr;
163 u32 status;
165 csr = readl(ch->addr + APB_DMA_CHAN_CSR);
166 csr &= ~CSR_IE_EOC;
167 writel(csr, ch->addr + APB_DMA_CHAN_CSR);
169 csr &= ~CSR_ENB;
170 writel(csr, ch->addr + APB_DMA_CHAN_CSR);
172 status = readl(ch->addr + APB_DMA_CHAN_STA);
173 if (status & STA_ISE_EOC)
174 writel(status, ch->addr + APB_DMA_CHAN_STA);
177 int tegra_dma_cancel(struct tegra_dma_channel *ch)
179 u32 csr;
180 unsigned long irq_flags;
182 spin_lock_irqsave(&ch->lock, irq_flags);
183 while (!list_empty(&ch->list))
184 list_del(ch->list.next);
186 csr = readl(ch->addr + APB_DMA_CHAN_CSR);
187 csr &= ~CSR_REQ_SEL_MASK;
188 csr |= CSR_REQ_SEL_INVALID;
189 writel(csr, ch->addr + APB_DMA_CHAN_CSR);
191 tegra_dma_stop(ch);
193 spin_unlock_irqrestore(&ch->lock, irq_flags);
194 return 0;
197 int tegra_dma_dequeue_req(struct tegra_dma_channel *ch,
198 struct tegra_dma_req *_req)
200 unsigned int csr;
201 unsigned int status;
202 struct tegra_dma_req *req = NULL;
203 int found = 0;
204 unsigned long irq_flags;
205 int to_transfer;
206 int req_transfer_count;
208 spin_lock_irqsave(&ch->lock, irq_flags);
209 list_for_each_entry(req, &ch->list, node) {
210 if (req == _req) {
211 list_del(&req->node);
212 found = 1;
213 break;
216 if (!found) {
217 spin_unlock_irqrestore(&ch->lock, irq_flags);
218 return 0;
221 /* STOP the DMA and get the transfer count.
222 * Getting the transfer count is tricky.
223 * - Change the source selector to invalid to stop the DMA from
224 * FIFO to memory.
225 * - Read the status register to know the number of pending
226 * bytes to be transferred.
227 * - Finally stop or program the DMA to the next buffer in the
228 * list.
230 csr = readl(ch->addr + APB_DMA_CHAN_CSR);
231 csr &= ~CSR_REQ_SEL_MASK;
232 csr |= CSR_REQ_SEL_INVALID;
233 writel(csr, ch->addr + APB_DMA_CHAN_CSR);
235 /* Get the transfer count */
236 status = readl(ch->addr + APB_DMA_CHAN_STA);
237 to_transfer = (status & STA_COUNT_MASK) >> STA_COUNT_SHIFT;
238 req_transfer_count = ch->req_transfer_count;
239 req_transfer_count += 1;
240 to_transfer += 1;
242 req->bytes_transferred = req_transfer_count;
244 if (status & STA_BUSY)
245 req->bytes_transferred -= to_transfer;
247 /* In continuous transfer mode, DMA only tracks the count of the
248 * half DMA buffer. So, if the DMA already finished half the DMA
249 * then add the half buffer to the completed count.
251 * FIXME: There can be a race here. What if the req to
252 * dequue happens at the same time as the DMA just moved to
253 * the new buffer and SW didn't yet received the interrupt?
255 if (ch->mode & TEGRA_DMA_MODE_CONTINOUS)
256 if (req->buffer_status == TEGRA_DMA_REQ_BUF_STATUS_HALF_FULL)
257 req->bytes_transferred += req_transfer_count;
259 req->bytes_transferred *= 4;
261 tegra_dma_stop(ch);
262 if (!list_empty(&ch->list)) {
263 /* if the list is not empty, queue the next request */
264 struct tegra_dma_req *next_req;
265 next_req = list_entry(ch->list.next,
266 typeof(*next_req), node);
267 tegra_dma_update_hw(ch, next_req);
269 req->status = -TEGRA_DMA_REQ_ERROR_ABORTED;
271 spin_unlock_irqrestore(&ch->lock, irq_flags);
273 /* Callback should be called without any lock */
274 req->complete(req);
275 return 0;
277 EXPORT_SYMBOL(tegra_dma_dequeue_req);
279 bool tegra_dma_is_empty(struct tegra_dma_channel *ch)
281 unsigned long irq_flags;
282 bool is_empty;
284 spin_lock_irqsave(&ch->lock, irq_flags);
285 if (list_empty(&ch->list))
286 is_empty = true;
287 else
288 is_empty = false;
289 spin_unlock_irqrestore(&ch->lock, irq_flags);
290 return is_empty;
292 EXPORT_SYMBOL(tegra_dma_is_empty);
294 bool tegra_dma_is_req_inflight(struct tegra_dma_channel *ch,
295 struct tegra_dma_req *_req)
297 unsigned long irq_flags;
298 struct tegra_dma_req *req;
300 spin_lock_irqsave(&ch->lock, irq_flags);
301 list_for_each_entry(req, &ch->list, node) {
302 if (req == _req) {
303 spin_unlock_irqrestore(&ch->lock, irq_flags);
304 return true;
307 spin_unlock_irqrestore(&ch->lock, irq_flags);
308 return false;
310 EXPORT_SYMBOL(tegra_dma_is_req_inflight);
312 int tegra_dma_enqueue_req(struct tegra_dma_channel *ch,
313 struct tegra_dma_req *req)
315 unsigned long irq_flags;
316 struct tegra_dma_req *_req;
317 int start_dma = 0;
319 if (req->size > NV_DMA_MAX_TRASFER_SIZE ||
320 req->source_addr & 0x3 || req->dest_addr & 0x3) {
321 pr_err("Invalid DMA request for channel %d\n", ch->id);
322 return -EINVAL;
325 spin_lock_irqsave(&ch->lock, irq_flags);
327 list_for_each_entry(_req, &ch->list, node) {
328 if (req == _req) {
329 spin_unlock_irqrestore(&ch->lock, irq_flags);
330 return -EEXIST;
334 req->bytes_transferred = 0;
335 req->status = 0;
336 req->buffer_status = 0;
337 if (list_empty(&ch->list))
338 start_dma = 1;
340 list_add_tail(&req->node, &ch->list);
342 if (start_dma)
343 tegra_dma_update_hw(ch, req);
345 spin_unlock_irqrestore(&ch->lock, irq_flags);
347 return 0;
349 EXPORT_SYMBOL(tegra_dma_enqueue_req);
351 struct tegra_dma_channel *tegra_dma_allocate_channel(int mode)
353 int channel;
354 struct tegra_dma_channel *ch = NULL;
356 if (WARN_ON(!tegra_dma_initialized))
357 return NULL;
359 mutex_lock(&tegra_dma_lock);
361 /* first channel is the shared channel */
362 if (mode & TEGRA_DMA_SHARED) {
363 channel = TEGRA_SYSTEM_DMA_CH_MIN;
364 } else {
365 channel = find_first_zero_bit(channel_usage,
366 ARRAY_SIZE(dma_channels));
367 if (channel >= ARRAY_SIZE(dma_channels))
368 goto out;
370 __set_bit(channel, channel_usage);
371 ch = &dma_channels[channel];
372 ch->mode = mode;
374 out:
375 mutex_unlock(&tegra_dma_lock);
376 return ch;
378 EXPORT_SYMBOL(tegra_dma_allocate_channel);
380 void tegra_dma_free_channel(struct tegra_dma_channel *ch)
382 if (ch->mode & TEGRA_DMA_SHARED)
383 return;
384 tegra_dma_cancel(ch);
385 mutex_lock(&tegra_dma_lock);
386 __clear_bit(ch->id, channel_usage);
387 mutex_unlock(&tegra_dma_lock);
389 EXPORT_SYMBOL(tegra_dma_free_channel);
391 static void tegra_dma_update_hw_partial(struct tegra_dma_channel *ch,
392 struct tegra_dma_req *req)
394 u32 apb_ptr;
395 u32 ahb_ptr;
397 if (req->to_memory) {
398 apb_ptr = req->source_addr;
399 ahb_ptr = req->dest_addr;
400 } else {
401 apb_ptr = req->dest_addr;
402 ahb_ptr = req->source_addr;
404 writel(apb_ptr, ch->addr + APB_DMA_CHAN_APB_PTR);
405 writel(ahb_ptr, ch->addr + APB_DMA_CHAN_AHB_PTR);
407 req->status = TEGRA_DMA_REQ_INFLIGHT;
408 return;
411 static void tegra_dma_update_hw(struct tegra_dma_channel *ch,
412 struct tegra_dma_req *req)
414 int ahb_addr_wrap;
415 int apb_addr_wrap;
416 int ahb_bus_width;
417 int apb_bus_width;
418 int index;
420 u32 ahb_seq;
421 u32 apb_seq;
422 u32 ahb_ptr;
423 u32 apb_ptr;
424 u32 csr;
426 csr = CSR_IE_EOC | CSR_FLOW;
427 ahb_seq = AHB_SEQ_INTR_ENB | AHB_SEQ_BURST_1;
428 apb_seq = 0;
430 csr |= req->req_sel << CSR_REQ_SEL_SHIFT;
432 /* One shot mode is always single buffered,
433 * continuous mode is always double buffered
434 * */
435 if (ch->mode & TEGRA_DMA_MODE_ONESHOT) {
436 csr |= CSR_ONCE;
437 ch->req_transfer_count = (req->size >> 2) - 1;
438 } else {
439 ahb_seq |= AHB_SEQ_DBL_BUF;
441 /* In double buffered mode, we set the size to half the
442 * requested size and interrupt when half the buffer
443 * is full */
444 ch->req_transfer_count = (req->size >> 3) - 1;
447 csr |= ch->req_transfer_count << CSR_WCOUNT_SHIFT;
449 if (req->to_memory) {
450 apb_ptr = req->source_addr;
451 ahb_ptr = req->dest_addr;
453 apb_addr_wrap = req->source_wrap;
454 ahb_addr_wrap = req->dest_wrap;
455 apb_bus_width = req->source_bus_width;
456 ahb_bus_width = req->dest_bus_width;
458 } else {
459 csr |= CSR_DIR;
460 apb_ptr = req->dest_addr;
461 ahb_ptr = req->source_addr;
463 apb_addr_wrap = req->dest_wrap;
464 ahb_addr_wrap = req->source_wrap;
465 apb_bus_width = req->dest_bus_width;
466 ahb_bus_width = req->source_bus_width;
469 apb_addr_wrap >>= 2;
470 ahb_addr_wrap >>= 2;
472 /* set address wrap for APB size */
473 index = 0;
474 do {
475 if (apb_addr_wrap_table[index] == apb_addr_wrap)
476 break;
477 index++;
478 } while (index < ARRAY_SIZE(apb_addr_wrap_table));
479 BUG_ON(index == ARRAY_SIZE(apb_addr_wrap_table));
480 apb_seq |= index << APB_SEQ_WRAP_SHIFT;
482 /* set address wrap for AHB size */
483 index = 0;
484 do {
485 if (ahb_addr_wrap_table[index] == ahb_addr_wrap)
486 break;
487 index++;
488 } while (index < ARRAY_SIZE(ahb_addr_wrap_table));
489 BUG_ON(index == ARRAY_SIZE(ahb_addr_wrap_table));
490 ahb_seq |= index << AHB_SEQ_WRAP_SHIFT;
492 for (index = 0; index < ARRAY_SIZE(bus_width_table); index++) {
493 if (bus_width_table[index] == ahb_bus_width)
494 break;
496 BUG_ON(index == ARRAY_SIZE(bus_width_table));
497 ahb_seq |= index << AHB_SEQ_BUS_WIDTH_SHIFT;
499 for (index = 0; index < ARRAY_SIZE(bus_width_table); index++) {
500 if (bus_width_table[index] == apb_bus_width)
501 break;
503 BUG_ON(index == ARRAY_SIZE(bus_width_table));
504 apb_seq |= index << APB_SEQ_BUS_WIDTH_SHIFT;
506 writel(csr, ch->addr + APB_DMA_CHAN_CSR);
507 writel(apb_seq, ch->addr + APB_DMA_CHAN_APB_SEQ);
508 writel(apb_ptr, ch->addr + APB_DMA_CHAN_APB_PTR);
509 writel(ahb_seq, ch->addr + APB_DMA_CHAN_AHB_SEQ);
510 writel(ahb_ptr, ch->addr + APB_DMA_CHAN_AHB_PTR);
512 csr |= CSR_ENB;
513 writel(csr, ch->addr + APB_DMA_CHAN_CSR);
515 req->status = TEGRA_DMA_REQ_INFLIGHT;
518 static void handle_oneshot_dma(struct tegra_dma_channel *ch)
520 struct tegra_dma_req *req;
521 unsigned long irq_flags;
523 spin_lock_irqsave(&ch->lock, irq_flags);
524 if (list_empty(&ch->list)) {
525 spin_unlock_irqrestore(&ch->lock, irq_flags);
526 return;
529 req = list_entry(ch->list.next, typeof(*req), node);
530 if (req) {
531 int bytes_transferred;
533 bytes_transferred = ch->req_transfer_count;
534 bytes_transferred += 1;
535 bytes_transferred <<= 2;
537 list_del(&req->node);
538 req->bytes_transferred = bytes_transferred;
539 req->status = TEGRA_DMA_REQ_SUCCESS;
541 spin_unlock_irqrestore(&ch->lock, irq_flags);
542 /* Callback should be called without any lock */
543 pr_debug("%s: transferred %d bytes\n", __func__,
544 req->bytes_transferred);
545 req->complete(req);
546 spin_lock_irqsave(&ch->lock, irq_flags);
549 if (!list_empty(&ch->list)) {
550 req = list_entry(ch->list.next, typeof(*req), node);
551 /* the complete function we just called may have enqueued
552 another req, in which case dma has already started */
553 if (req->status != TEGRA_DMA_REQ_INFLIGHT)
554 tegra_dma_update_hw(ch, req);
556 spin_unlock_irqrestore(&ch->lock, irq_flags);
559 static void handle_continuous_dma(struct tegra_dma_channel *ch)
561 struct tegra_dma_req *req;
562 unsigned long irq_flags;
564 spin_lock_irqsave(&ch->lock, irq_flags);
565 if (list_empty(&ch->list)) {
566 spin_unlock_irqrestore(&ch->lock, irq_flags);
567 return;
570 req = list_entry(ch->list.next, typeof(*req), node);
571 if (req) {
572 if (req->buffer_status == TEGRA_DMA_REQ_BUF_STATUS_EMPTY) {
573 bool is_dma_ping_complete;
574 is_dma_ping_complete = (readl(ch->addr + APB_DMA_CHAN_STA)
575 & STA_PING_PONG) ? true : false;
576 if (req->to_memory)
577 is_dma_ping_complete = !is_dma_ping_complete;
578 /* Out of sync - Release current buffer */
579 if (!is_dma_ping_complete) {
580 int bytes_transferred;
582 bytes_transferred = ch->req_transfer_count;
583 bytes_transferred += 1;
584 bytes_transferred <<= 3;
585 req->buffer_status = TEGRA_DMA_REQ_BUF_STATUS_FULL;
586 req->bytes_transferred = bytes_transferred;
587 req->status = TEGRA_DMA_REQ_SUCCESS;
588 tegra_dma_stop(ch);
590 if (!list_is_last(&req->node, &ch->list)) {
591 struct tegra_dma_req *next_req;
593 next_req = list_entry(req->node.next,
594 typeof(*next_req), node);
595 tegra_dma_update_hw(ch, next_req);
598 list_del(&req->node);
600 /* DMA lock is NOT held when callbak is called */
601 spin_unlock_irqrestore(&ch->lock, irq_flags);
602 req->complete(req);
603 return;
605 /* Load the next request into the hardware, if available
606 * */
607 if (!list_is_last(&req->node, &ch->list)) {
608 struct tegra_dma_req *next_req;
610 next_req = list_entry(req->node.next,
611 typeof(*next_req), node);
612 tegra_dma_update_hw_partial(ch, next_req);
614 req->buffer_status = TEGRA_DMA_REQ_BUF_STATUS_HALF_FULL;
615 req->status = TEGRA_DMA_REQ_SUCCESS;
616 /* DMA lock is NOT held when callback is called */
617 spin_unlock_irqrestore(&ch->lock, irq_flags);
618 if (likely(req->threshold))
619 req->threshold(req);
620 return;
622 } else if (req->buffer_status ==
623 TEGRA_DMA_REQ_BUF_STATUS_HALF_FULL) {
624 /* Callback when the buffer is completely full (i.e on
625 * the second interrupt */
626 int bytes_transferred;
628 bytes_transferred = ch->req_transfer_count;
629 bytes_transferred += 1;
630 bytes_transferred <<= 3;
632 req->buffer_status = TEGRA_DMA_REQ_BUF_STATUS_FULL;
633 req->bytes_transferred = bytes_transferred;
634 req->status = TEGRA_DMA_REQ_SUCCESS;
635 list_del(&req->node);
637 /* DMA lock is NOT held when callbak is called */
638 spin_unlock_irqrestore(&ch->lock, irq_flags);
639 req->complete(req);
640 return;
642 } else {
643 BUG();
646 spin_unlock_irqrestore(&ch->lock, irq_flags);
649 static irqreturn_t dma_isr(int irq, void *data)
651 struct tegra_dma_channel *ch = data;
652 unsigned long status;
654 status = readl(ch->addr + APB_DMA_CHAN_STA);
655 if (status & STA_ISE_EOC)
656 writel(status, ch->addr + APB_DMA_CHAN_STA);
657 else {
658 pr_warning("Got a spurious ISR for DMA channel %d\n", ch->id);
659 return IRQ_HANDLED;
661 return IRQ_WAKE_THREAD;
664 static irqreturn_t dma_thread_fn(int irq, void *data)
666 struct tegra_dma_channel *ch = data;
668 if (ch->mode & TEGRA_DMA_MODE_ONESHOT)
669 handle_oneshot_dma(ch);
670 else
671 handle_continuous_dma(ch);
674 return IRQ_HANDLED;
677 int __init tegra_dma_init(void)
679 int ret = 0;
680 int i;
681 unsigned int irq;
682 void __iomem *addr;
683 struct clk *c;
685 bitmap_fill(channel_usage, NV_DMA_MAX_CHANNELS);
687 c = clk_get_sys("tegra-dma", NULL);
688 if (IS_ERR(c)) {
689 pr_err("Unable to get clock for APB DMA\n");
690 ret = PTR_ERR(c);
691 goto fail;
693 ret = clk_enable(c);
694 if (ret != 0) {
695 pr_err("Unable to enable clock for APB DMA\n");
696 goto fail;
699 addr = IO_ADDRESS(TEGRA_APB_DMA_BASE);
700 writel(GEN_ENABLE, addr + APB_DMA_GEN);
701 writel(0, addr + APB_DMA_CNTRL);
702 writel(0xFFFFFFFFul >> (31 - TEGRA_SYSTEM_DMA_CH_MAX),
703 addr + APB_DMA_IRQ_MASK_SET);
705 for (i = TEGRA_SYSTEM_DMA_CH_MIN; i <= TEGRA_SYSTEM_DMA_CH_MAX; i++) {
706 struct tegra_dma_channel *ch = &dma_channels[i];
708 ch->id = i;
709 snprintf(ch->name, TEGRA_DMA_NAME_SIZE, "dma_channel_%d", i);
711 ch->addr = IO_ADDRESS(TEGRA_APB_DMA_CH0_BASE +
712 TEGRA_APB_DMA_CH0_SIZE * i);
714 spin_lock_init(&ch->lock);
715 INIT_LIST_HEAD(&ch->list);
717 irq = INT_APB_DMA_CH0 + i;
718 ret = request_threaded_irq(irq, dma_isr, dma_thread_fn, 0,
719 dma_channels[i].name, ch);
720 if (ret) {
721 pr_err("Failed to register IRQ %d for DMA %d\n",
722 irq, i);
723 goto fail;
725 ch->irq = irq;
727 __clear_bit(i, channel_usage);
729 /* mark the shared channel allocated */
730 __set_bit(TEGRA_SYSTEM_DMA_CH_MIN, channel_usage);
732 tegra_dma_initialized = true;
734 return 0;
735 fail:
736 writel(0, addr + APB_DMA_GEN);
737 for (i = TEGRA_SYSTEM_DMA_CH_MIN; i <= TEGRA_SYSTEM_DMA_CH_MAX; i++) {
738 struct tegra_dma_channel *ch = &dma_channels[i];
739 if (ch->irq)
740 free_irq(ch->irq, ch);
742 return ret;
744 postcore_initcall(tegra_dma_init);
746 #ifdef CONFIG_PM
747 static u32 apb_dma[5*TEGRA_SYSTEM_DMA_CH_NR + 3];
749 void tegra_dma_suspend(void)
751 void __iomem *addr = IO_ADDRESS(TEGRA_APB_DMA_BASE);
752 u32 *ctx = apb_dma;
753 int i;
755 *ctx++ = readl(addr + APB_DMA_GEN);
756 *ctx++ = readl(addr + APB_DMA_CNTRL);
757 *ctx++ = readl(addr + APB_DMA_IRQ_MASK);
759 for (i = 0; i < TEGRA_SYSTEM_DMA_CH_NR; i++) {
760 addr = IO_ADDRESS(TEGRA_APB_DMA_CH0_BASE +
761 TEGRA_APB_DMA_CH0_SIZE * i);
763 *ctx++ = readl(addr + APB_DMA_CHAN_CSR);
764 *ctx++ = readl(addr + APB_DMA_CHAN_AHB_PTR);
765 *ctx++ = readl(addr + APB_DMA_CHAN_AHB_SEQ);
766 *ctx++ = readl(addr + APB_DMA_CHAN_APB_PTR);
767 *ctx++ = readl(addr + APB_DMA_CHAN_APB_SEQ);
771 void tegra_dma_resume(void)
773 void __iomem *addr = IO_ADDRESS(TEGRA_APB_DMA_BASE);
774 u32 *ctx = apb_dma;
775 int i;
777 writel(*ctx++, addr + APB_DMA_GEN);
778 writel(*ctx++, addr + APB_DMA_CNTRL);
779 writel(*ctx++, addr + APB_DMA_IRQ_MASK);
781 for (i = 0; i < TEGRA_SYSTEM_DMA_CH_NR; i++) {
782 addr = IO_ADDRESS(TEGRA_APB_DMA_CH0_BASE +
783 TEGRA_APB_DMA_CH0_SIZE * i);
785 writel(*ctx++, addr + APB_DMA_CHAN_CSR);
786 writel(*ctx++, addr + APB_DMA_CHAN_AHB_PTR);
787 writel(*ctx++, addr + APB_DMA_CHAN_AHB_SEQ);
788 writel(*ctx++, addr + APB_DMA_CHAN_APB_PTR);
789 writel(*ctx++, addr + APB_DMA_CHAN_APB_SEQ);
793 #endif