diff options
author | GuanXuetao <gxt@mprc.pku.edu.cn> | 2011-01-15 05:18:29 -0500 |
---|---|---|
committer | GuanXuetao <gxt@mprc.pku.edu.cn> | 2011-03-16 21:19:09 -0400 |
commit | 10c9c10c31514564b09c153432a42ffaea3ce831 (patch) | |
tree | 04a60b9a1e48eaa2d9346e265a1c2fe2db5ec670 /arch/unicore32/kernel | |
parent | 56372b0b2f533c9a25bd40a0577405f6ddb7cff2 (diff) |
unicore32 core architecture: mm related: consistent device DMA handling
This patch implements consistent device DMA handling of memory management.
DMA device operations are also here.
Signed-off-by: Guan Xuetao <gxt@mprc.pku.edu.cn>
Reviewed-by: Arnd Bergmann <arnd@arndb.de>
Diffstat (limited to 'arch/unicore32/kernel')
-rw-r--r-- | arch/unicore32/kernel/dma.c | 180 |
1 files changed, 180 insertions, 0 deletions
diff --git a/arch/unicore32/kernel/dma.c b/arch/unicore32/kernel/dma.c new file mode 100644 index 000000000000..b8dcc2514e9a --- /dev/null +++ b/arch/unicore32/kernel/dma.c | |||
@@ -0,0 +1,180 @@ | |||
1 | /* | ||
2 | * linux/arch/unicore32/kernel/dma.c | ||
3 | * | ||
4 | * Code specific to PKUnity SoC and UniCore ISA | ||
5 | * | ||
6 | * Maintained by GUAN Xue-tao <gxt@mprc.pku.edu.cn> | ||
7 | * Copyright (C) 2001-2010 Guan Xuetao | ||
8 | * | ||
9 | * This program is free software; you can redistribute it and/or modify | ||
10 | * it under the terms of the GNU General Public License version 2 as | ||
11 | * published by the Free Software Foundation. | ||
12 | */ | ||
13 | |||
14 | #include <linux/module.h> | ||
15 | #include <linux/init.h> | ||
16 | #include <linux/kernel.h> | ||
17 | #include <linux/interrupt.h> | ||
18 | #include <linux/errno.h> | ||
19 | |||
20 | #include <asm/system.h> | ||
21 | #include <asm/irq.h> | ||
22 | #include <mach/hardware.h> | ||
23 | #include <mach/dma.h> | ||
24 | |||
25 | struct dma_channel { | ||
26 | char *name; | ||
27 | puv3_dma_prio prio; | ||
28 | void (*irq_handler)(int, void *); | ||
29 | void (*err_handler)(int, void *); | ||
30 | void *data; | ||
31 | }; | ||
32 | |||
33 | static struct dma_channel dma_channels[MAX_DMA_CHANNELS]; | ||
34 | |||
35 | int puv3_request_dma(char *name, puv3_dma_prio prio, | ||
36 | void (*irq_handler)(int, void *), | ||
37 | void (*err_handler)(int, void *), | ||
38 | void *data) | ||
39 | { | ||
40 | unsigned long flags; | ||
41 | int i, found = 0; | ||
42 | |||
43 | /* basic sanity checks */ | ||
44 | if (!name) | ||
45 | return -EINVAL; | ||
46 | |||
47 | local_irq_save(flags); | ||
48 | |||
49 | do { | ||
50 | /* try grabbing a DMA channel with the requested priority */ | ||
51 | for (i = 0; i < MAX_DMA_CHANNELS; i++) { | ||
52 | if ((dma_channels[i].prio == prio) && | ||
53 | !dma_channels[i].name) { | ||
54 | found = 1; | ||
55 | break; | ||
56 | } | ||
57 | } | ||
58 | /* if requested prio group is full, try a hier priority */ | ||
59 | } while (!found && prio--); | ||
60 | |||
61 | if (found) { | ||
62 | dma_channels[i].name = name; | ||
63 | dma_channels[i].irq_handler = irq_handler; | ||
64 | dma_channels[i].err_handler = err_handler; | ||
65 | dma_channels[i].data = data; | ||
66 | } else { | ||
67 | printk(KERN_WARNING "No more available DMA channels for %s\n", | ||
68 | name); | ||
69 | i = -ENODEV; | ||
70 | } | ||
71 | |||
72 | local_irq_restore(flags); | ||
73 | return i; | ||
74 | } | ||
75 | EXPORT_SYMBOL(puv3_request_dma); | ||
76 | |||
77 | void puv3_free_dma(int dma_ch) | ||
78 | { | ||
79 | unsigned long flags; | ||
80 | |||
81 | if (!dma_channels[dma_ch].name) { | ||
82 | printk(KERN_CRIT | ||
83 | "%s: trying to free channel %d which is already freed\n", | ||
84 | __func__, dma_ch); | ||
85 | return; | ||
86 | } | ||
87 | |||
88 | local_irq_save(flags); | ||
89 | dma_channels[dma_ch].name = NULL; | ||
90 | dma_channels[dma_ch].err_handler = NULL; | ||
91 | local_irq_restore(flags); | ||
92 | } | ||
93 | EXPORT_SYMBOL(puv3_free_dma); | ||
94 | |||
95 | static irqreturn_t dma_irq_handler(int irq, void *dev_id) | ||
96 | { | ||
97 | int i, dint = DMAC_ITCSR; | ||
98 | |||
99 | for (i = 0; i < MAX_DMA_CHANNELS; i++) { | ||
100 | if (dint & DMAC_CHANNEL(i)) { | ||
101 | struct dma_channel *channel = &dma_channels[i]; | ||
102 | |||
103 | /* Clear TC interrupt of channel i */ | ||
104 | DMAC_ITCCR = DMAC_CHANNEL(i); | ||
105 | DMAC_ITCCR = 0; | ||
106 | |||
107 | if (channel->name && channel->irq_handler) { | ||
108 | channel->irq_handler(i, channel->data); | ||
109 | } else { | ||
110 | /* | ||
111 | * IRQ for an unregistered DMA channel: | ||
112 | * let's clear the interrupts and disable it. | ||
113 | */ | ||
114 | printk(KERN_WARNING "spurious IRQ for" | ||
115 | " DMA channel %d\n", i); | ||
116 | } | ||
117 | } | ||
118 | } | ||
119 | return IRQ_HANDLED; | ||
120 | } | ||
121 | |||
122 | static irqreturn_t dma_err_handler(int irq, void *dev_id) | ||
123 | { | ||
124 | int i, dint = DMAC_IESR; | ||
125 | |||
126 | for (i = 0; i < MAX_DMA_CHANNELS; i++) { | ||
127 | if (dint & DMAC_CHANNEL(i)) { | ||
128 | struct dma_channel *channel = &dma_channels[i]; | ||
129 | |||
130 | /* Clear Err interrupt of channel i */ | ||
131 | DMAC_IECR = DMAC_CHANNEL(i); | ||
132 | DMAC_IECR = 0; | ||
133 | |||
134 | if (channel->name && channel->err_handler) { | ||
135 | channel->err_handler(i, channel->data); | ||
136 | } else { | ||
137 | /* | ||
138 | * IRQ for an unregistered DMA channel: | ||
139 | * let's clear the interrupts and disable it. | ||
140 | */ | ||
141 | printk(KERN_WARNING "spurious IRQ for" | ||
142 | " DMA channel %d\n", i); | ||
143 | } | ||
144 | } | ||
145 | } | ||
146 | return IRQ_HANDLED; | ||
147 | } | ||
148 | |||
149 | int __init puv3_init_dma(void) | ||
150 | { | ||
151 | int i, ret; | ||
152 | |||
153 | /* dma channel priorities on v8 processors: | ||
154 | * ch 0 - 1 <--> (0) DMA_PRIO_HIGH | ||
155 | * ch 2 - 3 <--> (1) DMA_PRIO_MEDIUM | ||
156 | * ch 4 - 5 <--> (2) DMA_PRIO_LOW | ||
157 | */ | ||
158 | for (i = 0; i < MAX_DMA_CHANNELS; i++) { | ||
159 | puv3_stop_dma(i); | ||
160 | dma_channels[i].name = NULL; | ||
161 | dma_channels[i].prio = min((i & 0x7) >> 1, DMA_PRIO_LOW); | ||
162 | } | ||
163 | |||
164 | ret = request_irq(IRQ_DMA, dma_irq_handler, 0, "DMA", NULL); | ||
165 | if (ret) { | ||
166 | printk(KERN_CRIT "Can't register IRQ for DMA\n"); | ||
167 | return ret; | ||
168 | } | ||
169 | |||
170 | ret = request_irq(IRQ_DMAERR, dma_err_handler, 0, "DMAERR", NULL); | ||
171 | if (ret) { | ||
172 | printk(KERN_CRIT "Can't register IRQ for DMAERR\n"); | ||
173 | free_irq(IRQ_DMA, "DMA"); | ||
174 | return ret; | ||
175 | } | ||
176 | |||
177 | return 0; | ||
178 | } | ||
179 | |||
180 | postcore_initcall(puv3_init_dma); | ||