dma.c 3.3 KB
Newer Older
L
Linus Torvalds 已提交
1
/*
2
 *  linux/arch/arm/plat-pxa/dma.c
L
Linus Torvalds 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 *
 *  PXA DMA registration and IRQ dispatching
 *
 *  Author:	Nicolas Pitre
 *  Created:	Nov 15, 2001
 *  Copyright:	MontaVista Software Inc.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License version 2 as
 *  published by the Free Software Foundation.
 */

#include <linux/module.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/interrupt.h>
#include <linux/errno.h>

#include <asm/system.h>
#include <asm/irq.h>
23
#include <mach/hardware.h>
24
#include <mach/dma.h>
L
Linus Torvalds 已提交
25

26
struct dma_channel {
L
Linus Torvalds 已提交
27
	char *name;
28
	pxa_dma_prio prio;
29
	void (*irq_handler)(int, void *);
L
Linus Torvalds 已提交
30
	void *data;
31
};
L
Linus Torvalds 已提交
32

33 34
static struct dma_channel *dma_channels;
static int num_dma_channels;
L
Linus Torvalds 已提交
35 36

int pxa_request_dma (char *name, pxa_dma_prio prio,
37 38
			void (*irq_handler)(int, void *),
			void *data)
L
Linus Torvalds 已提交
39 40 41 42 43 44 45 46 47 48
{
	unsigned long flags;
	int i, found = 0;

	/* basic sanity checks */
	if (!name || !irq_handler)
		return -EINVAL;

	local_irq_save(flags);

49 50
	do {
		/* try grabbing a DMA channel with the requested priority */
51 52 53
		for (i = 0; i < num_dma_channels; i++) {
			if ((dma_channels[i].prio == prio) &&
			    !dma_channels[i].name) {
L
Linus Torvalds 已提交
54 55 56 57
				found = 1;
				break;
			}
		}
58 59
		/* if requested prio group is full, try a hier priority */
	} while (!found && prio--);
L
Linus Torvalds 已提交
60 61 62 63 64 65 66 67 68 69 70 71 72 73

	if (found) {
		DCSR(i) = DCSR_STARTINTR|DCSR_ENDINTR|DCSR_BUSERR;
		dma_channels[i].name = name;
		dma_channels[i].irq_handler = irq_handler;
		dma_channels[i].data = data;
	} else {
		printk (KERN_WARNING "No more available DMA channels for %s\n", name);
		i = -ENODEV;
	}

	local_irq_restore(flags);
	return i;
}
74
EXPORT_SYMBOL(pxa_request_dma);
L
Linus Torvalds 已提交
75 76 77 78 79 80 81 82

void pxa_free_dma (int dma_ch)
{
	unsigned long flags;

	if (!dma_channels[dma_ch].name) {
		printk (KERN_CRIT
			"%s: trying to free channel %d which is already freed\n",
83
			__func__, dma_ch);
L
Linus Torvalds 已提交
84 85 86 87 88 89 90 91
		return;
	}

	local_irq_save(flags);
	DCSR(dma_ch) = DCSR_STARTINTR|DCSR_ENDINTR|DCSR_BUSERR;
	dma_channels[dma_ch].name = NULL;
	local_irq_restore(flags);
}
92
EXPORT_SYMBOL(pxa_free_dma);
L
Linus Torvalds 已提交
93

94
static irqreturn_t dma_irq_handler(int irq, void *dev_id)
L
Linus Torvalds 已提交
95 96 97
{
	int i, dint = DINT;

98
	for (i = 0; i < num_dma_channels; i++) {
L
Linus Torvalds 已提交
99 100 101
		if (dint & (1 << i)) {
			struct dma_channel *channel = &dma_channels[i];
			if (channel->name && channel->irq_handler) {
102
				channel->irq_handler(i, channel->data);
L
Linus Torvalds 已提交
103 104 105 106 107 108 109 110 111 112 113 114 115
			} else {
				/*
				 * IRQ for an unregistered DMA channel:
				 * let's clear the interrupts and disable it.
				 */
				printk (KERN_WARNING "spurious IRQ for DMA channel %d\n", i);
				DCSR(i) = DCSR_STARTINTR|DCSR_ENDINTR|DCSR_BUSERR;
			}
		}
	}
	return IRQ_HANDLED;
}

116
int __init pxa_init_dma(int irq, int num_ch)
L
Linus Torvalds 已提交
117
{
118
	int i, ret;
L
Linus Torvalds 已提交
119

120 121 122 123 124 125 126 127 128
	dma_channels = kzalloc(sizeof(struct dma_channel) * num_ch, GFP_KERNEL);
	if (dma_channels == NULL)
		return -ENOMEM;

	/* dma channel priorities on pxa2xx processors:
	 * ch 0 - 3,  16 - 19  <--> (0) DMA_PRIO_HIGH
	 * ch 4 - 7,  20 - 23  <--> (1) DMA_PRIO_MEDIUM
	 * ch 8 - 15, 24 - 31  <--> (2) DMA_PRIO_LOW
	 */
129 130
	for (i = 0; i < num_ch; i++) {
		DCSR(i) = 0;
131
		dma_channels[i].prio = min((i & 0xf) >> 2, DMA_PRIO_LOW);
132 133
	}

134
	ret = request_irq(irq, dma_irq_handler, IRQF_DISABLED, "DMA", NULL);
135 136 137 138 139
	if (ret) {
		printk (KERN_CRIT "Wow!  Can't register IRQ for DMA\n");
		kfree(dma_channels);
		return ret;
	}
140 141 142 143

	num_dma_channels = num_ch;
	return 0;
}