1 // SPDX-License-Identifier: GPL-2.0-only
3 * linux/arch/unicore32/kernel/dma.c
5 * Code specific to PKUnity SoC and UniCore ISA
7 * Maintained by GUAN Xue-tao <gxt@mprc.pku.edu.cn>
8 * Copyright (C) 2001-2010 Guan Xuetao
11 #include <linux/module.h>
12 #include <linux/init.h>
13 #include <linux/kernel.h>
14 #include <linux/interrupt.h>
15 #include <linux/errno.h>
19 #include <mach/hardware.h>
25 void (*irq_handler)(int, void *);
26 void (*err_handler)(int, void *);
30 static struct dma_channel dma_channels[MAX_DMA_CHANNELS];
32 int puv3_request_dma(char *name, puv3_dma_prio prio,
33 void (*irq_handler)(int, void *),
34 void (*err_handler)(int, void *),
40 /* basic sanity checks */
44 local_irq_save(flags);
47 /* try grabbing a DMA channel with the requested priority */
48 for (i = 0; i < MAX_DMA_CHANNELS; i++) {
49 if ((dma_channels[i].prio == prio) &&
50 !dma_channels[i].name) {
55 /* if requested prio group is full, try a hier priority */
56 } while (!found && prio--);
59 dma_channels[i].name = name;
60 dma_channels[i].irq_handler = irq_handler;
61 dma_channels[i].err_handler = err_handler;
62 dma_channels[i].data = data;
64 printk(KERN_WARNING "No more available DMA channels for %s\n",
69 local_irq_restore(flags);
72 EXPORT_SYMBOL(puv3_request_dma);
74 void puv3_free_dma(int dma_ch)
78 if (!dma_channels[dma_ch].name) {
80 "%s: trying to free channel %d which is already freed\n",
85 local_irq_save(flags);
86 dma_channels[dma_ch].name = NULL;
87 dma_channels[dma_ch].err_handler = NULL;
88 local_irq_restore(flags);
90 EXPORT_SYMBOL(puv3_free_dma);
92 static irqreturn_t dma_irq_handler(int irq, void *dev_id)
96 dint = readl(DMAC_ITCSR);
97 for (i = 0; i < MAX_DMA_CHANNELS; i++) {
98 if (dint & DMAC_CHANNEL(i)) {
99 struct dma_channel *channel = &dma_channels[i];
101 /* Clear TC interrupt of channel i */
102 writel(DMAC_CHANNEL(i), DMAC_ITCCR);
103 writel(0, DMAC_ITCCR);
105 if (channel->name && channel->irq_handler) {
106 channel->irq_handler(i, channel->data);
109 * IRQ for an unregistered DMA channel:
110 * let's clear the interrupts and disable it.
112 printk(KERN_WARNING "spurious IRQ for"
113 " DMA channel %d\n", i);
120 static irqreturn_t dma_err_handler(int irq, void *dev_id)
124 dint = readl(DMAC_IESR);
125 for (i = 0; i < MAX_DMA_CHANNELS; i++) {
126 if (dint & DMAC_CHANNEL(i)) {
127 struct dma_channel *channel = &dma_channels[i];
129 /* Clear Err interrupt of channel i */
130 writel(DMAC_CHANNEL(i), DMAC_IECR);
131 writel(0, DMAC_IECR);
133 if (channel->name && channel->err_handler) {
134 channel->err_handler(i, channel->data);
137 * IRQ for an unregistered DMA channel:
138 * let's clear the interrupts and disable it.
140 printk(KERN_WARNING "spurious IRQ for"
141 " DMA channel %d\n", i);
148 int __init puv3_init_dma(void)
152 /* dma channel priorities on v8 processors:
153 * ch 0 - 1 <--> (0) DMA_PRIO_HIGH
154 * ch 2 - 3 <--> (1) DMA_PRIO_MEDIUM
155 * ch 4 - 5 <--> (2) DMA_PRIO_LOW
157 for (i = 0; i < MAX_DMA_CHANNELS; i++) {
159 dma_channels[i].name = NULL;
160 dma_channels[i].prio = min((i & 0x7) >> 1, DMA_PRIO_LOW);
163 ret = request_irq(IRQ_DMA, dma_irq_handler, 0, "DMA", NULL);
165 printk(KERN_CRIT "Can't register IRQ for DMA\n");
169 ret = request_irq(IRQ_DMAERR, dma_err_handler, 0, "DMAERR", NULL);
171 printk(KERN_CRIT "Can't register IRQ for DMAERR\n");
172 free_irq(IRQ_DMA, "DMA");
179 postcore_initcall(puv3_init_dma);