Linux 3.17-rc2
[linux/fpc-iii.git] / arch / unicore32 / kernel / dma.c
blobed2d4d78d9c434282bbc05f4e10837bb11c37a0a
1 /*
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>
19 #include <linux/io.h>
21 #include <asm/irq.h>
22 #include <mach/hardware.h>
23 #include <mach/dma.h>
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;
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 *),
38 void *data)
40 unsigned long flags;
41 int i, found = 0;
43 /* basic sanity checks */
44 if (!name)
45 return -EINVAL;
47 local_irq_save(flags);
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;
58 /* if requested prio group is full, try a hier priority */
59 } while (!found && prio--);
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;
72 local_irq_restore(flags);
73 return i;
75 EXPORT_SYMBOL(puv3_request_dma);
77 void puv3_free_dma(int dma_ch)
79 unsigned long flags;
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;
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)
97 int i, dint;
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);
110 } else {
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);
120 return IRQ_HANDLED;
123 static irqreturn_t dma_err_handler(int irq, void *dev_id)
125 int i, dint;
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);
138 } else {
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);
148 return IRQ_HANDLED;
151 int __init puv3_init_dma(void)
153 int i, ret;
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++) {
161 puv3_stop_dma(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);
167 if (ret) {
168 printk(KERN_CRIT "Can't register IRQ for DMA\n");
169 return ret;
172 ret = request_irq(IRQ_DMAERR, dma_err_handler, 0, "DMAERR", NULL);
173 if (ret) {
174 printk(KERN_CRIT "Can't register IRQ for DMAERR\n");
175 free_irq(IRQ_DMA, "DMA");
176 return ret;
179 return 0;
182 postcore_initcall(puv3_init_dma);