blob: 897663dbc5f4deb72f99965d8a3b9a114ae1b7f4 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Eric Miaobd5ce432009-01-20 12:06:01 +08002 * linux/arch/arm/plat-pxa/dma.c
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 * PXA DMA registration and IRQ dispatching
5 *
6 * Author: Nicolas Pitre
7 * Created: Nov 15, 2001
8 * Copyright: MontaVista Software Inc.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
13 */
14
15#include <linux/module.h>
16#include <linux/init.h>
17#include <linux/kernel.h>
18#include <linux/interrupt.h>
19#include <linux/errno.h>
20
21#include <asm/system.h>
22#include <asm/irq.h>
Russell Kinga09e64f2008-08-05 16:14:15 +010023#include <mach/hardware.h>
Russell Kingdcea83a2008-11-29 11:40:28 +000024#include <mach/dma.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
Eric Miaof53f0662007-06-22 05:40:17 +010026struct dma_channel {
Linus Torvalds1da177e2005-04-16 15:20:36 -070027 char *name;
Eric Miaof53f0662007-06-22 05:40:17 +010028 pxa_dma_prio prio;
Linus Torvalds0cd61b62006-10-06 10:53:39 -070029 void (*irq_handler)(int, void *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 void *data;
Eric Miaof53f0662007-06-22 05:40:17 +010031};
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
Eric Miaof53f0662007-06-22 05:40:17 +010033static struct dma_channel *dma_channels;
34static int num_dma_channels;
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
36int pxa_request_dma (char *name, pxa_dma_prio prio,
Eric Miaobd5ce432009-01-20 12:06:01 +080037 void (*irq_handler)(int, void *),
38 void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -070039{
40 unsigned long flags;
41 int i, found = 0;
42
43 /* basic sanity checks */
44 if (!name || !irq_handler)
45 return -EINVAL;
46
47 local_irq_save(flags);
48
Nicolas Pitre99532552006-05-05 22:32:24 +010049 do {
50 /* try grabbing a DMA channel with the requested priority */
Eric Miaof53f0662007-06-22 05:40:17 +010051 for (i = 0; i < num_dma_channels; i++) {
52 if ((dma_channels[i].prio == prio) &&
53 !dma_channels[i].name) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 found = 1;
55 break;
56 }
57 }
Nicolas Pitre99532552006-05-05 22:32:24 +010058 /* if requested prio group is full, try a hier priority */
59 } while (!found && prio--);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
61 if (found) {
62 DCSR(i) = DCSR_STARTINTR|DCSR_ENDINTR|DCSR_BUSERR;
63 dma_channels[i].name = name;
64 dma_channels[i].irq_handler = irq_handler;
65 dma_channels[i].data = data;
66 } else {
67 printk (KERN_WARNING "No more available DMA channels for %s\n", name);
68 i = -ENODEV;
69 }
70
71 local_irq_restore(flags);
72 return i;
73}
Robert Jarzmik43c63422009-08-08 23:07:20 +020074EXPORT_SYMBOL(pxa_request_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
76void pxa_free_dma (int dma_ch)
77{
78 unsigned long flags;
79
80 if (!dma_channels[dma_ch].name) {
81 printk (KERN_CRIT
82 "%s: trying to free channel %d which is already freed\n",
Harvey Harrison8e86f422008-03-04 15:08:02 -080083 __func__, dma_ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 return;
85 }
86
87 local_irq_save(flags);
88 DCSR(dma_ch) = DCSR_STARTINTR|DCSR_ENDINTR|DCSR_BUSERR;
89 dma_channels[dma_ch].name = NULL;
90 local_irq_restore(flags);
91}
Robert Jarzmik43c63422009-08-08 23:07:20 +020092EXPORT_SYMBOL(pxa_free_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
Linus Torvalds0cd61b62006-10-06 10:53:39 -070094static irqreturn_t dma_irq_handler(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -070095{
96 int i, dint = DINT;
97
Eric Miaof53f0662007-06-22 05:40:17 +010098 for (i = 0; i < num_dma_channels; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 if (dint & (1 << i)) {
100 struct dma_channel *channel = &dma_channels[i];
101 if (channel->name && channel->irq_handler) {
Linus Torvalds0cd61b62006-10-06 10:53:39 -0700102 channel->irq_handler(i, channel->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 } else {
104 /*
105 * IRQ for an unregistered DMA channel:
106 * let's clear the interrupts and disable it.
107 */
108 printk (KERN_WARNING "spurious IRQ for DMA channel %d\n", i);
109 DCSR(i) = DCSR_STARTINTR|DCSR_ENDINTR|DCSR_BUSERR;
110 }
111 }
112 }
113 return IRQ_HANDLED;
114}
115
Eric Miaofef1f992009-01-02 16:26:33 +0800116int __init pxa_init_dma(int irq, int num_ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117{
Eric Miaof53f0662007-06-22 05:40:17 +0100118 int i, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
Eric Miaof53f0662007-06-22 05:40:17 +0100120 dma_channels = kzalloc(sizeof(struct dma_channel) * num_ch, GFP_KERNEL);
121 if (dma_channels == NULL)
122 return -ENOMEM;
123
Eric Miao26a55222009-01-21 11:29:19 +0800124 /* dma channel priorities on pxa2xx processors:
125 * ch 0 - 3, 16 - 19 <--> (0) DMA_PRIO_HIGH
126 * ch 4 - 7, 20 - 23 <--> (1) DMA_PRIO_MEDIUM
127 * ch 8 - 15, 24 - 31 <--> (2) DMA_PRIO_LOW
128 */
129 for (i = 0; i < num_ch; i++) {
130 DCSR(i) = 0;
131 dma_channels[i].prio = min((i & 0xf) >> 2, DMA_PRIO_LOW);
132 }
133
Eric Miaofef1f992009-01-02 16:26:33 +0800134 ret = request_irq(irq, dma_irq_handler, IRQF_DISABLED, "DMA", NULL);
Eric Miaof53f0662007-06-22 05:40:17 +0100135 if (ret) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 printk (KERN_CRIT "Wow! Can't register IRQ for DMA\n");
Eric Miaof53f0662007-06-22 05:40:17 +0100137 kfree(dma_channels);
138 return ret;
139 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
Eric Miaof53f0662007-06-22 05:40:17 +0100141 num_dma_channels = num_ch;
142 return 0;
143}