ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / arch / sh / drivers / dma / dma-sh.c
1 /*
2  * arch/sh/kernel/cpu/dma.c
3  *
4  * Copyright (C) 2000 Takashi YOSHII
5  * Copyright (C) 2003 Paul Mundt
6  *
7  * PC like DMA API for SuperH's DMAC.
8  *
9  * This file is subject to the terms and conditions of the GNU General Public
10  * License.  See the file "COPYING" in the main directory of this archive
11  * for more details.
12  */
13
14 #include <linux/config.h>
15 #include <linux/init.h>
16 #include <linux/irq.h>
17 #include <linux/interrupt.h>
18 #include <linux/module.h>
19 #include <asm/signal.h>
20 #include <asm/irq.h>
21 #include <asm/dma.h>
22 #include <asm/io.h>
23 #include "dma-sh.h"
24
25 /*
26  * The SuperH DMAC supports a number of transmit sizes, we list them here,
27  * with their respective values as they appear in the CHCR registers.
28  *
29  * Defaults to a 64-bit transfer size.
30  */
31 enum {
32         XMIT_SZ_64BIT   = 0,
33         XMIT_SZ_8BIT    = 1,
34         XMIT_SZ_16BIT   = 2,
35         XMIT_SZ_32BIT   = 3,
36         XMIT_SZ_256BIT  = 4,
37 };
38
39 /*
40  * The DMA count is defined as the number of bytes to transfer.
41  */
42 static unsigned int ts_shift[] = {
43         [XMIT_SZ_64BIT]         3,
44         [XMIT_SZ_8BIT]          0,
45         [XMIT_SZ_16BIT]         1,
46         [XMIT_SZ_32BIT]         2,
47         [XMIT_SZ_256BIT]        5,
48 };
49
50 struct sh_dmac_channel {
51         unsigned long sar;
52         unsigned long dar;
53         unsigned long dmatcr;
54         unsigned long chcr;
55 } __attribute__ ((aligned(16)));
56
57 struct sh_dmac_info {
58         struct sh_dmac_channel channel[4];
59         unsigned long dmaor;
60 };
61
62 static volatile struct sh_dmac_info *sh_dmac = (volatile struct sh_dmac_info *)SH_DMAC_BASE;
63
64 static inline unsigned int get_dmte_irq(unsigned int chan)
65 {
66         unsigned int irq;
67
68         /* 
69          * Normally we could just do DMTE0_IRQ + chan outright, though in the
70          * case of the 7751R, the DMTE IRQs for channels > 4 start right above
71          * the SCIF
72          */
73
74         if (chan < 4) {
75                 irq = DMTE0_IRQ + chan;
76         } else {
77                 irq = DMTE4_IRQ + chan - 4;
78         }
79
80         return irq;
81 }
82
83 /*
84  * We determine the correct shift size based off of the CHCR transmit size
85  * for the given channel. Since we know that it will take:
86  *
87  *      info->count >> ts_shift[transmit_size]
88  *
89  * iterations to complete the transfer.
90  */
91 static inline unsigned int calc_xmit_shift(struct dma_info *info)
92 {
93         return ts_shift[(sh_dmac->channel[info->chan].chcr >> 4) & 0x0007];
94 }
95
96 /*
97  * The transfer end interrupt must read the chcr register to end the
98  * hardware interrupt active condition.
99  * Besides that it needs to waken any waiting process, which should handle
100  * setting up the next transfer.
101  */
102 static irqreturn_t dma_tei(int irq, void *dev_id, struct pt_regs *regs)
103 {
104         struct dma_info * info = (struct dma_info *)dev_id;
105         u32 chcr = sh_dmac->channel[info->chan].chcr;
106
107         if (!(chcr & CHCR_TE))
108                 return IRQ_NONE;
109
110         sh_dmac->channel[info->chan].chcr = chcr & ~(CHCR_IE | CHCR_DE);
111
112         wake_up(&info->wait_queue);
113
114         return IRQ_HANDLED;
115 }
116
117 static int sh_dmac_request_dma(struct dma_info *info)
118 {
119         return request_irq(get_dmte_irq(info->chan), dma_tei,
120                            SA_INTERRUPT, "DMAC Transfer End", info);
121 }
122
123 static void sh_dmac_free_dma(struct dma_info *info)
124 {
125         free_irq(get_dmte_irq(info->chan), info);
126 }
127
128 static void sh_dmac_configure_channel(struct dma_info *info, unsigned long chcr)
129 {
130         if (!chcr)
131                 chcr = RS_DUAL;
132
133         sh_dmac->channel[info->chan].chcr = chcr;
134
135         info->configured = 1;
136 }
137
138 static void sh_dmac_enable_dma(struct dma_info *info)
139 {
140         int irq = get_dmte_irq(info->chan);
141
142         sh_dmac->channel[info->chan].chcr |= (CHCR_DE | CHCR_IE);
143         enable_irq(irq);
144 }
145
146 static void sh_dmac_disable_dma(struct dma_info *info)
147 {
148         int irq = get_dmte_irq(info->chan);
149
150         disable_irq(irq);
151         sh_dmac->channel[info->chan].chcr &= ~(CHCR_DE | CHCR_TE | CHCR_IE);
152 }
153
154 static int sh_dmac_xfer_dma(struct dma_info *info)
155 {
156         /* 
157          * If we haven't pre-configured the channel with special flags, use
158          * the defaults.
159          */
160         if (!info->configured)
161                 sh_dmac_configure_channel(info, 0);
162
163         sh_dmac_disable_dma(info);
164         
165         /* 
166          * Single-address mode usage note!
167          *
168          * It's important that we don't accidentally write any value to SAR/DAR
169          * (this includes 0) that hasn't been directly specified by the user if
170          * we're in single-address mode.
171          *
172          * In this case, only one address can be defined, anything else will
173          * result in a DMA address error interrupt (at least on the SH-4),
174          * which will subsequently halt the transfer.
175          *
176          * Channel 2 on the Dreamcast is a special case, as this is used for
177          * cascading to the PVR2 DMAC. In this case, we still need to write
178          * SAR and DAR, regardless of value, in order for cascading to work.
179          */
180         if (info->sar || (mach_is_dreamcast() && info->chan == 2))
181                 sh_dmac->channel[info->chan].sar = info->sar;
182         if (info->dar || (mach_is_dreamcast() && info->chan == 2))
183                 sh_dmac->channel[info->chan].dar = info->dar;
184         
185         sh_dmac->channel[info->chan].dmatcr = info->count >> calc_xmit_shift(info);
186
187         sh_dmac_enable_dma(info);
188
189         return 0;
190 }
191
192 static int sh_dmac_get_dma_residue(struct dma_info *info)
193 {
194         if (!(sh_dmac->channel[info->chan].chcr & CHCR_DE))
195                 return 0;
196
197         return sh_dmac->channel[info->chan].dmatcr << calc_xmit_shift(info);
198 }
199
200 #if defined(CONFIG_CPU_SH4)
201 static irqreturn_t dma_err(int irq, void *dev_id, struct pt_regs *regs)
202 {
203         printk("DMAE: DMAOR=%lx\n", sh_dmac->dmaor);
204
205         sh_dmac->dmaor &= ~(DMAOR_NMIF | DMAOR_AE);
206         sh_dmac->dmaor |= DMAOR_DME;
207
208         disable_irq(irq);
209
210         return IRQ_HANDLED;
211 }
212 #endif
213
214 static struct dma_ops sh_dmac_ops = {
215         .name           = "SuperH DMAC",
216         .request        = sh_dmac_request_dma,
217         .free           = sh_dmac_free_dma,
218         .get_residue    = sh_dmac_get_dma_residue,
219         .xfer           = sh_dmac_xfer_dma,
220         .configure      = sh_dmac_configure_channel,
221 };
222         
223 static int __init sh_dmac_init(void)
224 {
225         int i;
226
227 #ifdef CONFIG_CPU_SH4
228         make_ipr_irq(DMAE_IRQ, DMA_IPR_ADDR, DMA_IPR_POS, DMA_PRIORITY);
229         i = request_irq(DMAE_IRQ, dma_err, SA_INTERRUPT, "DMAC Address Error", 0);
230         if (i < 0)
231                 return i;
232 #endif
233
234         for (i = 0; i < MAX_DMAC_CHANNELS; i++) {
235                 int irq = get_dmte_irq(i);
236
237                 make_ipr_irq(irq, DMA_IPR_ADDR, DMA_IPR_POS, DMA_PRIORITY);
238
239                 dma_info[i].ops = &sh_dmac_ops;
240                 dma_info[i].tei_capable = 1;
241         }
242
243         sh_dmac->dmaor |= 0x8000 | DMAOR_DME;
244
245         return register_dmac(&sh_dmac_ops);
246 }
247
248 static void __exit sh_dmac_exit(void)
249 {
250 #ifdef CONFIG_CPU_SH4
251         free_irq(DMAE_IRQ, 0);
252 #endif
253 }
254
255 subsys_initcall(sh_dmac_init);
256 module_exit(sh_dmac_exit);
257
258 MODULE_LICENSE("GPL");
259