2 * linux/arch/unicore32/kernel/dma.c
4 * Code specific to PKUnity SoC and UniCore ISA
6 * Maintained by GUAN Xue-tao <gxt@mprc.pku.edu.cn>
7 * Copyright (C) 2001-2010 Guan Xuetao
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.
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>
22 #include <mach/hardware.h>
28 void (*irq_handler
)(int, void *);
29 void (*err_handler
)(int, void *);
33 static struct dma_channel dma_channels
[MAX_DMA_CHANNELS
];
35 int puv3_request_dma(char *name
, puv3_dma_prio prio
,
36 void (*irq_handler
)(int, void *),
37 void (*err_handler
)(int, void *),
43 /* basic sanity checks */
47 local_irq_save(flags
);
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
) {
58 /* if requested prio group is full, try a hier priority */
59 } while (!found
&& prio
--);
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
;
67 printk(KERN_WARNING
"No more available DMA channels for %s\n",
72 local_irq_restore(flags
);
75 EXPORT_SYMBOL(puv3_request_dma
);
77 void puv3_free_dma(int dma_ch
)
81 if (!dma_channels
[dma_ch
].name
) {
83 "%s: trying to free channel %d which is already freed\n",
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
);
93 EXPORT_SYMBOL(puv3_free_dma
);
95 static irqreturn_t
dma_irq_handler(int irq
, void *dev_id
)
99 dint
= readl(DMAC_ITCSR
);
100 for (i
= 0; i
< MAX_DMA_CHANNELS
; i
++) {
101 if (dint
& DMAC_CHANNEL(i
)) {
102 struct dma_channel
*channel
= &dma_channels
[i
];
104 /* Clear TC interrupt of channel i */
105 writel(DMAC_CHANNEL(i
), DMAC_ITCCR
);
106 writel(0, DMAC_ITCCR
);
108 if (channel
->name
&& channel
->irq_handler
) {
109 channel
->irq_handler(i
, channel
->data
);
112 * IRQ for an unregistered DMA channel:
113 * let's clear the interrupts and disable it.
115 printk(KERN_WARNING
"spurious IRQ for"
116 " DMA channel %d\n", i
);
123 static irqreturn_t
dma_err_handler(int irq
, void *dev_id
)
127 dint
= readl(DMAC_IESR
);
128 for (i
= 0; i
< MAX_DMA_CHANNELS
; i
++) {
129 if (dint
& DMAC_CHANNEL(i
)) {
130 struct dma_channel
*channel
= &dma_channels
[i
];
132 /* Clear Err interrupt of channel i */
133 writel(DMAC_CHANNEL(i
), DMAC_IECR
);
134 writel(0, DMAC_IECR
);
136 if (channel
->name
&& channel
->err_handler
) {
137 channel
->err_handler(i
, channel
->data
);
140 * IRQ for an unregistered DMA channel:
141 * let's clear the interrupts and disable it.
143 printk(KERN_WARNING
"spurious IRQ for"
144 " DMA channel %d\n", i
);
151 int __init
puv3_init_dma(void)
155 /* dma channel priorities on v8 processors:
156 * ch 0 - 1 <--> (0) DMA_PRIO_HIGH
157 * ch 2 - 3 <--> (1) DMA_PRIO_MEDIUM
158 * ch 4 - 5 <--> (2) DMA_PRIO_LOW
160 for (i
= 0; i
< MAX_DMA_CHANNELS
; i
++) {
162 dma_channels
[i
].name
= NULL
;
163 dma_channels
[i
].prio
= min((i
& 0x7) >> 1, DMA_PRIO_LOW
);
166 ret
= request_irq(IRQ_DMA
, dma_irq_handler
, 0, "DMA", NULL
);
168 printk(KERN_CRIT
"Can't register IRQ for DMA\n");
172 ret
= request_irq(IRQ_DMAERR
, dma_err_handler
, 0, "DMAERR", NULL
);
174 printk(KERN_CRIT
"Can't register IRQ for DMAERR\n");
175 free_irq(IRQ_DMA
, "DMA");
182 postcore_initcall(puv3_init_dma
);