2005-04-17 00:20:36 +02:00
|
|
|
/*
|
|
|
|
* linux/arch/arm/kernel/dma.c
|
|
|
|
*
|
|
|
|
* Copyright (C) 1995-2000 Russell King
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* Front-end to the DMA handling. This handles the allocation/freeing
|
|
|
|
* of DMA channels, and provides a unified interface to the machines
|
|
|
|
* DMA facilities.
|
|
|
|
*/
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/init.h>
|
|
|
|
#include <linux/spinlock.h>
|
|
|
|
#include <linux/errno.h>
|
2008-12-08 18:50:25 +01:00
|
|
|
#include <linux/scatterlist.h>
|
2009-12-24 19:32:13 +01:00
|
|
|
#include <linux/seq_file.h>
|
|
|
|
#include <linux/proc_fs.h>
|
2005-04-17 00:20:36 +02:00
|
|
|
|
|
|
|
#include <asm/dma.h>
|
|
|
|
|
|
|
|
#include <asm/mach/dma.h>
|
|
|
|
|
2009-07-03 15:44:46 +02:00
|
|
|
DEFINE_RAW_SPINLOCK(dma_spin_lock);
|
2006-01-04 16:52:45 +01:00
|
|
|
EXPORT_SYMBOL(dma_spin_lock);
|
2005-04-17 00:20:36 +02:00
|
|
|
|
2008-12-08 17:33:30 +01:00
|
|
|
static dma_t *dma_chan[MAX_DMA_CHANNELS];
|
2005-04-17 00:20:36 +02:00
|
|
|
|
2008-12-08 17:08:48 +01:00
|
|
|
static inline dma_t *dma_channel(unsigned int chan)
|
|
|
|
{
|
2008-12-08 17:33:30 +01:00
|
|
|
if (chan >= MAX_DMA_CHANNELS)
|
2008-12-08 17:08:48 +01:00
|
|
|
return NULL;
|
|
|
|
|
2008-12-08 17:33:30 +01:00
|
|
|
return dma_chan[chan];
|
|
|
|
}
|
|
|
|
|
|
|
|
int __init isa_dma_add(unsigned int chan, dma_t *dma)
|
|
|
|
{
|
|
|
|
if (!dma->d_ops)
|
|
|
|
return -EINVAL;
|
2008-12-08 18:50:25 +01:00
|
|
|
|
|
|
|
sg_init_table(&dma->buf, 1);
|
|
|
|
|
2008-12-08 17:33:30 +01:00
|
|
|
if (dma_chan[chan])
|
|
|
|
return -EBUSY;
|
|
|
|
dma_chan[chan] = dma;
|
|
|
|
return 0;
|
2008-12-08 17:08:48 +01:00
|
|
|
}
|
|
|
|
|
2005-04-17 00:20:36 +02:00
|
|
|
/*
|
|
|
|
* Request DMA channel
|
|
|
|
*
|
|
|
|
* On certain platforms, we have to allocate an interrupt as well...
|
|
|
|
*/
|
2008-12-08 16:58:50 +01:00
|
|
|
int request_dma(unsigned int chan, const char *device_id)
|
2005-04-17 00:20:36 +02:00
|
|
|
{
|
2008-12-08 17:08:48 +01:00
|
|
|
dma_t *dma = dma_channel(chan);
|
2005-04-17 00:20:36 +02:00
|
|
|
int ret;
|
|
|
|
|
2008-12-08 17:08:48 +01:00
|
|
|
if (!dma)
|
2005-04-17 00:20:36 +02:00
|
|
|
goto bad_dma;
|
|
|
|
|
|
|
|
if (xchg(&dma->lock, 1) != 0)
|
|
|
|
goto busy;
|
|
|
|
|
|
|
|
dma->device_id = device_id;
|
|
|
|
dma->active = 0;
|
|
|
|
dma->invalid = 1;
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
if (dma->d_ops->request)
|
2008-12-08 16:58:50 +01:00
|
|
|
ret = dma->d_ops->request(chan, dma);
|
2005-04-17 00:20:36 +02:00
|
|
|
|
|
|
|
if (ret)
|
|
|
|
xchg(&dma->lock, 0);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
bad_dma:
|
2014-10-28 12:26:42 +01:00
|
|
|
pr_err("dma: trying to allocate DMA%d\n", chan);
|
2005-04-17 00:20:36 +02:00
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
busy:
|
|
|
|
return -EBUSY;
|
|
|
|
}
|
2006-01-04 16:52:45 +01:00
|
|
|
EXPORT_SYMBOL(request_dma);
|
2005-04-17 00:20:36 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Free DMA channel
|
|
|
|
*
|
|
|
|
* On certain platforms, we have to free interrupt as well...
|
|
|
|
*/
|
2008-12-08 16:58:50 +01:00
|
|
|
void free_dma(unsigned int chan)
|
2005-04-17 00:20:36 +02:00
|
|
|
{
|
2008-12-08 17:08:48 +01:00
|
|
|
dma_t *dma = dma_channel(chan);
|
2005-04-17 00:20:36 +02:00
|
|
|
|
2008-12-08 17:08:48 +01:00
|
|
|
if (!dma)
|
2005-04-17 00:20:36 +02:00
|
|
|
goto bad_dma;
|
|
|
|
|
|
|
|
if (dma->active) {
|
2014-10-28 12:26:42 +01:00
|
|
|
pr_err("dma%d: freeing active DMA\n", chan);
|
2008-12-08 16:58:50 +01:00
|
|
|
dma->d_ops->disable(chan, dma);
|
2005-04-17 00:20:36 +02:00
|
|
|
dma->active = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (xchg(&dma->lock, 0) != 0) {
|
|
|
|
if (dma->d_ops->free)
|
2008-12-08 16:58:50 +01:00
|
|
|
dma->d_ops->free(chan, dma);
|
2005-04-17 00:20:36 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-10-28 12:26:42 +01:00
|
|
|
pr_err("dma%d: trying to free free DMA\n", chan);
|
2005-04-17 00:20:36 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
bad_dma:
|
2014-10-28 12:26:42 +01:00
|
|
|
pr_err("dma: trying to free DMA%d\n", chan);
|
2005-04-17 00:20:36 +02:00
|
|
|
}
|
2006-01-04 16:52:45 +01:00
|
|
|
EXPORT_SYMBOL(free_dma);
|
2005-04-17 00:20:36 +02:00
|
|
|
|
|
|
|
/* Set DMA Scatter-Gather list
|
|
|
|
*/
|
2008-12-08 16:58:50 +01:00
|
|
|
void set_dma_sg (unsigned int chan, struct scatterlist *sg, int nr_sg)
|
2005-04-17 00:20:36 +02:00
|
|
|
{
|
2008-12-08 17:08:48 +01:00
|
|
|
dma_t *dma = dma_channel(chan);
|
2005-04-17 00:20:36 +02:00
|
|
|
|
|
|
|
if (dma->active)
|
2014-10-28 12:26:42 +01:00
|
|
|
pr_err("dma%d: altering DMA SG while DMA active\n", chan);
|
2005-04-17 00:20:36 +02:00
|
|
|
|
|
|
|
dma->sg = sg;
|
|
|
|
dma->sgcount = nr_sg;
|
|
|
|
dma->invalid = 1;
|
|
|
|
}
|
2006-01-04 16:52:45 +01:00
|
|
|
EXPORT_SYMBOL(set_dma_sg);
|
2005-04-17 00:20:36 +02:00
|
|
|
|
|
|
|
/* Set DMA address
|
|
|
|
*
|
|
|
|
* Copy address to the structure, and set the invalid bit
|
|
|
|
*/
|
2008-12-08 16:58:50 +01:00
|
|
|
void __set_dma_addr (unsigned int chan, void *addr)
|
2005-04-17 00:20:36 +02:00
|
|
|
{
|
2008-12-08 17:08:48 +01:00
|
|
|
dma_t *dma = dma_channel(chan);
|
2005-04-17 00:20:36 +02:00
|
|
|
|
|
|
|
if (dma->active)
|
2014-10-28 12:26:42 +01:00
|
|
|
pr_err("dma%d: altering DMA address while DMA active\n", chan);
|
2005-04-17 00:20:36 +02:00
|
|
|
|
2006-01-04 16:08:30 +01:00
|
|
|
dma->sg = NULL;
|
|
|
|
dma->addr = addr;
|
2005-04-17 00:20:36 +02:00
|
|
|
dma->invalid = 1;
|
|
|
|
}
|
2006-01-04 16:52:45 +01:00
|
|
|
EXPORT_SYMBOL(__set_dma_addr);
|
2005-04-17 00:20:36 +02:00
|
|
|
|
|
|
|
/* Set DMA byte count
|
|
|
|
*
|
|
|
|
* Copy address to the structure, and set the invalid bit
|
|
|
|
*/
|
2008-12-08 16:58:50 +01:00
|
|
|
void set_dma_count (unsigned int chan, unsigned long count)
|
2005-04-17 00:20:36 +02:00
|
|
|
{
|
2008-12-08 17:08:48 +01:00
|
|
|
dma_t *dma = dma_channel(chan);
|
2005-04-17 00:20:36 +02:00
|
|
|
|
|
|
|
if (dma->active)
|
2014-10-28 12:26:42 +01:00
|
|
|
pr_err("dma%d: altering DMA count while DMA active\n", chan);
|
2005-04-17 00:20:36 +02:00
|
|
|
|
2006-01-04 16:08:30 +01:00
|
|
|
dma->sg = NULL;
|
|
|
|
dma->count = count;
|
2005-04-17 00:20:36 +02:00
|
|
|
dma->invalid = 1;
|
|
|
|
}
|
2006-01-04 16:52:45 +01:00
|
|
|
EXPORT_SYMBOL(set_dma_count);
|
2005-04-17 00:20:36 +02:00
|
|
|
|
|
|
|
/* Set DMA direction mode
|
|
|
|
*/
|
2009-01-02 13:34:55 +01:00
|
|
|
void set_dma_mode (unsigned int chan, unsigned int mode)
|
2005-04-17 00:20:36 +02:00
|
|
|
{
|
2008-12-08 17:08:48 +01:00
|
|
|
dma_t *dma = dma_channel(chan);
|
2005-04-17 00:20:36 +02:00
|
|
|
|
|
|
|
if (dma->active)
|
2014-10-28 12:26:42 +01:00
|
|
|
pr_err("dma%d: altering DMA mode while DMA active\n", chan);
|
2005-04-17 00:20:36 +02:00
|
|
|
|
|
|
|
dma->dma_mode = mode;
|
|
|
|
dma->invalid = 1;
|
|
|
|
}
|
2006-01-04 16:52:45 +01:00
|
|
|
EXPORT_SYMBOL(set_dma_mode);
|
2005-04-17 00:20:36 +02:00
|
|
|
|
|
|
|
/* Enable DMA channel
|
|
|
|
*/
|
2008-12-08 16:58:50 +01:00
|
|
|
void enable_dma (unsigned int chan)
|
2005-04-17 00:20:36 +02:00
|
|
|
{
|
2008-12-08 17:08:48 +01:00
|
|
|
dma_t *dma = dma_channel(chan);
|
2005-04-17 00:20:36 +02:00
|
|
|
|
|
|
|
if (!dma->lock)
|
|
|
|
goto free_dma;
|
|
|
|
|
|
|
|
if (dma->active == 0) {
|
|
|
|
dma->active = 1;
|
2008-12-08 16:58:50 +01:00
|
|
|
dma->d_ops->enable(chan, dma);
|
2005-04-17 00:20:36 +02:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
|
|
|
|
free_dma:
|
2014-10-28 12:26:42 +01:00
|
|
|
pr_err("dma%d: trying to enable free DMA\n", chan);
|
2005-04-17 00:20:36 +02:00
|
|
|
BUG();
|
|
|
|
}
|
2006-01-04 16:52:45 +01:00
|
|
|
EXPORT_SYMBOL(enable_dma);
|
2005-04-17 00:20:36 +02:00
|
|
|
|
|
|
|
/* Disable DMA channel
|
|
|
|
*/
|
2008-12-08 16:58:50 +01:00
|
|
|
void disable_dma (unsigned int chan)
|
2005-04-17 00:20:36 +02:00
|
|
|
{
|
2008-12-08 17:08:48 +01:00
|
|
|
dma_t *dma = dma_channel(chan);
|
2005-04-17 00:20:36 +02:00
|
|
|
|
|
|
|
if (!dma->lock)
|
|
|
|
goto free_dma;
|
|
|
|
|
|
|
|
if (dma->active == 1) {
|
|
|
|
dma->active = 0;
|
2008-12-08 16:58:50 +01:00
|
|
|
dma->d_ops->disable(chan, dma);
|
2005-04-17 00:20:36 +02:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
|
|
|
|
free_dma:
|
2014-10-28 12:26:42 +01:00
|
|
|
pr_err("dma%d: trying to disable free DMA\n", chan);
|
2005-04-17 00:20:36 +02:00
|
|
|
BUG();
|
|
|
|
}
|
2006-01-04 16:52:45 +01:00
|
|
|
EXPORT_SYMBOL(disable_dma);
|
2005-04-17 00:20:36 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Is the specified DMA channel active?
|
|
|
|
*/
|
2008-12-08 16:58:50 +01:00
|
|
|
int dma_channel_active(unsigned int chan)
|
2005-04-17 00:20:36 +02:00
|
|
|
{
|
2008-12-08 17:08:48 +01:00
|
|
|
dma_t *dma = dma_channel(chan);
|
|
|
|
return dma->active;
|
2005-04-17 00:20:36 +02:00
|
|
|
}
|
2007-03-31 22:36:53 +02:00
|
|
|
EXPORT_SYMBOL(dma_channel_active);
|
2005-04-17 00:20:36 +02:00
|
|
|
|
2008-12-08 16:58:50 +01:00
|
|
|
void set_dma_page(unsigned int chan, char pagenr)
|
2005-04-17 00:20:36 +02:00
|
|
|
{
|
2014-10-28 12:26:42 +01:00
|
|
|
pr_err("dma%d: trying to set_dma_page\n", chan);
|
2005-04-17 00:20:36 +02:00
|
|
|
}
|
2006-01-04 16:52:45 +01:00
|
|
|
EXPORT_SYMBOL(set_dma_page);
|
2005-04-17 00:20:36 +02:00
|
|
|
|
2008-12-08 16:58:50 +01:00
|
|
|
void set_dma_speed(unsigned int chan, int cycle_ns)
|
2005-04-17 00:20:36 +02:00
|
|
|
{
|
2008-12-08 17:08:48 +01:00
|
|
|
dma_t *dma = dma_channel(chan);
|
2005-04-17 00:20:36 +02:00
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
if (dma->d_ops->setspeed)
|
2008-12-08 16:58:50 +01:00
|
|
|
ret = dma->d_ops->setspeed(chan, dma, cycle_ns);
|
2005-04-17 00:20:36 +02:00
|
|
|
dma->speed = ret;
|
|
|
|
}
|
2006-01-04 16:52:45 +01:00
|
|
|
EXPORT_SYMBOL(set_dma_speed);
|
2005-04-17 00:20:36 +02:00
|
|
|
|
2008-12-08 16:58:50 +01:00
|
|
|
int get_dma_residue(unsigned int chan)
|
2005-04-17 00:20:36 +02:00
|
|
|
{
|
2008-12-08 17:08:48 +01:00
|
|
|
dma_t *dma = dma_channel(chan);
|
2005-04-17 00:20:36 +02:00
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
if (dma->d_ops->residue)
|
2008-12-08 16:58:50 +01:00
|
|
|
ret = dma->d_ops->residue(chan, dma);
|
2005-04-17 00:20:36 +02:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2006-01-04 16:52:45 +01:00
|
|
|
EXPORT_SYMBOL(get_dma_residue);
|
2009-12-24 19:32:13 +01:00
|
|
|
|
|
|
|
#ifdef CONFIG_PROC_FS
|
|
|
|
static int proc_dma_show(struct seq_file *m, void *v)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0 ; i < MAX_DMA_CHANNELS ; i++) {
|
|
|
|
dma_t *dma = dma_channel(i);
|
|
|
|
if (dma && dma->lock)
|
|
|
|
seq_printf(m, "%2d: %s\n", i, dma->device_id);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int proc_dma_open(struct inode *inode, struct file *file)
|
|
|
|
{
|
|
|
|
return single_open(file, proc_dma_show, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct file_operations proc_dma_operations = {
|
|
|
|
.open = proc_dma_open,
|
|
|
|
.read = seq_read,
|
|
|
|
.llseek = seq_lseek,
|
|
|
|
.release = single_release,
|
|
|
|
};
|
|
|
|
|
|
|
|
static int __init proc_dma_init(void)
|
|
|
|
{
|
|
|
|
proc_create("dma", 0, NULL, &proc_dma_operations);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
__initcall(proc_dma_init);
|
|
|
|
#endif
|