2005-04-16 22:20:36 +00:00
|
|
|
/*
|
|
|
|
* arch/sh/drivers/dma/dma-api.c
|
|
|
|
*
|
|
|
|
* SuperH-specific DMA management API
|
|
|
|
*
|
2006-01-17 06:14:09 +00:00
|
|
|
* Copyright (C) 2003, 2004, 2005 Paul Mundt
|
2005-04-16 22:20:36 +00:00
|
|
|
*
|
|
|
|
* This file is subject to the terms and conditions of the GNU General Public
|
|
|
|
* License. See the file "COPYING" in the main directory of this archive
|
|
|
|
* for more details.
|
|
|
|
*/
|
|
|
|
#include <linux/init.h>
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/spinlock.h>
|
|
|
|
#include <linux/proc_fs.h>
|
|
|
|
#include <linux/list.h>
|
2006-01-17 06:14:09 +00:00
|
|
|
#include <linux/platform_device.h>
|
2006-11-24 06:13:52 +00:00
|
|
|
#include <linux/mm.h>
|
2005-04-16 22:20:36 +00:00
|
|
|
#include <asm/dma.h>
|
|
|
|
|
|
|
|
DEFINE_SPINLOCK(dma_spin_lock);
|
|
|
|
static LIST_HEAD(registered_dmac_list);
|
|
|
|
|
|
|
|
struct dma_info *get_dma_info(unsigned int chan)
|
|
|
|
{
|
2006-01-17 06:14:09 +00:00
|
|
|
struct dma_info *info;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Look for each DMAC's range to determine who the owner of
|
|
|
|
* the channel is.
|
|
|
|
*/
|
2006-01-17 06:14:09 +00:00
|
|
|
list_for_each_entry(info, ®istered_dmac_list, list) {
|
2006-11-24 06:13:52 +00:00
|
|
|
if ((chan < info->first_channel_nr) ||
|
|
|
|
(chan >= info->first_channel_nr + info->nr_channels))
|
2005-04-16 22:20:36 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
return info;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
2006-11-24 06:13:52 +00:00
|
|
|
EXPORT_SYMBOL(get_dma_info);
|
|
|
|
|
|
|
|
struct dma_info *get_dma_info_by_name(const char *dmac_name)
|
|
|
|
{
|
|
|
|
struct dma_info *info;
|
|
|
|
|
|
|
|
list_for_each_entry(info, ®istered_dmac_list, list) {
|
|
|
|
if (dmac_name && (strcmp(dmac_name, info->name) != 0))
|
|
|
|
continue;
|
|
|
|
else
|
|
|
|
return info;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(get_dma_info_by_name);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2006-01-17 06:14:09 +00:00
|
|
|
static unsigned int get_nr_channels(void)
|
|
|
|
{
|
|
|
|
struct dma_info *info;
|
|
|
|
unsigned int nr = 0;
|
|
|
|
|
|
|
|
if (unlikely(list_empty(®istered_dmac_list)))
|
|
|
|
return nr;
|
|
|
|
|
|
|
|
list_for_each_entry(info, ®istered_dmac_list, list)
|
|
|
|
nr += info->nr_channels;
|
|
|
|
|
|
|
|
return nr;
|
|
|
|
}
|
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
struct dma_channel *get_dma_channel(unsigned int chan)
|
|
|
|
{
|
|
|
|
struct dma_info *info = get_dma_info(chan);
|
2006-11-24 06:13:52 +00:00
|
|
|
struct dma_channel *channel;
|
|
|
|
int i;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2006-11-24 06:13:52 +00:00
|
|
|
if (unlikely(!info))
|
2005-04-16 22:20:36 +00:00
|
|
|
return ERR_PTR(-EINVAL);
|
|
|
|
|
2006-11-24 06:13:52 +00:00
|
|
|
for (i = 0; i < info->nr_channels; i++) {
|
|
|
|
channel = &info->channels[i];
|
|
|
|
if (channel->chan == chan)
|
|
|
|
return channel;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
2006-11-24 06:13:52 +00:00
|
|
|
EXPORT_SYMBOL(get_dma_channel);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
int get_dma_residue(unsigned int chan)
|
|
|
|
{
|
|
|
|
struct dma_info *info = get_dma_info(chan);
|
2006-11-24 06:13:52 +00:00
|
|
|
struct dma_channel *channel = get_dma_channel(chan);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
if (info->ops->get_residue)
|
|
|
|
return info->ops->get_residue(channel);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2006-11-24 06:13:52 +00:00
|
|
|
EXPORT_SYMBOL(get_dma_residue);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2006-11-24 06:13:52 +00:00
|
|
|
static int search_cap(const char **haystack, const char *needle)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2006-11-24 06:13:52 +00:00
|
|
|
const char **p;
|
|
|
|
|
|
|
|
for (p = haystack; *p; p++)
|
|
|
|
if (strcmp(*p, needle) == 0)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* request_dma_bycap - Allocate a DMA channel based on its capabilities
|
|
|
|
* @dmac: List of DMA controllers to search
|
2007-05-13 23:15:10 +00:00
|
|
|
* @caps: List of capabilities
|
2006-11-24 06:13:52 +00:00
|
|
|
*
|
|
|
|
* Search all channels of all DMA controllers to find a channel which
|
|
|
|
* matches the requested capabilities. The result is the channel
|
|
|
|
* number if a match is found, or %-ENODEV if no match is found.
|
|
|
|
*
|
|
|
|
* Note that not all DMA controllers export capabilities, in which
|
|
|
|
* case they can never be allocated using this API, and so
|
|
|
|
* request_dma() must be used specifying the channel number.
|
|
|
|
*/
|
|
|
|
int request_dma_bycap(const char **dmac, const char **caps, const char *dev_id)
|
|
|
|
{
|
|
|
|
unsigned int found = 0;
|
|
|
|
struct dma_info *info;
|
|
|
|
const char **p;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
BUG_ON(!dmac || !caps);
|
|
|
|
|
|
|
|
list_for_each_entry(info, ®istered_dmac_list, list)
|
|
|
|
if (strcmp(*dmac, info->name) == 0) {
|
|
|
|
found = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!found)
|
|
|
|
return -ENODEV;
|
|
|
|
|
|
|
|
for (i = 0; i < info->nr_channels; i++) {
|
|
|
|
struct dma_channel *channel = &info->channels[i];
|
|
|
|
|
|
|
|
if (unlikely(!channel->caps))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
for (p = caps; *p; p++) {
|
|
|
|
if (!search_cap(channel->caps, *p))
|
|
|
|
break;
|
|
|
|
if (request_dma(channel->chan, dev_id) == 0)
|
|
|
|
return channel->chan;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(request_dma_bycap);
|
|
|
|
|
|
|
|
int dmac_search_free_channel(const char *dev_id)
|
|
|
|
{
|
|
|
|
struct dma_channel *channel = { 0 };
|
|
|
|
struct dma_info *info = get_dma_info(0);
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < info->nr_channels; i++) {
|
|
|
|
channel = &info->channels[i];
|
|
|
|
if (unlikely(!channel))
|
|
|
|
return -ENODEV;
|
|
|
|
|
|
|
|
if (atomic_read(&channel->busy) == 0)
|
|
|
|
break;
|
|
|
|
}
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2006-11-24 06:13:52 +00:00
|
|
|
if (info->ops->request) {
|
|
|
|
int result = info->ops->request(channel);
|
|
|
|
if (result)
|
|
|
|
return result;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2006-11-24 06:13:52 +00:00
|
|
|
atomic_set(&channel->busy, 1);
|
|
|
|
return channel->chan;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
2006-11-24 06:13:52 +00:00
|
|
|
return -ENOSYS;
|
|
|
|
}
|
|
|
|
|
|
|
|
int request_dma(unsigned int chan, const char *dev_id)
|
|
|
|
{
|
|
|
|
struct dma_channel *channel = { 0 };
|
|
|
|
struct dma_info *info = get_dma_info(chan);
|
|
|
|
int result;
|
|
|
|
|
|
|
|
channel = get_dma_channel(chan);
|
|
|
|
if (atomic_xchg(&channel->busy, 1))
|
|
|
|
return -EBUSY;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
strlcpy(channel->dev_id, dev_id, sizeof(channel->dev_id));
|
|
|
|
|
2006-11-24 06:13:52 +00:00
|
|
|
if (info->ops->request) {
|
|
|
|
result = info->ops->request(channel);
|
|
|
|
if (result)
|
|
|
|
atomic_set(&channel->busy, 0);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2006-11-24 06:13:52 +00:00
|
|
|
return result;
|
|
|
|
}
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2006-11-24 06:13:52 +00:00
|
|
|
EXPORT_SYMBOL(request_dma);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
void free_dma(unsigned int chan)
|
|
|
|
{
|
|
|
|
struct dma_info *info = get_dma_info(chan);
|
2006-11-24 06:13:52 +00:00
|
|
|
struct dma_channel *channel = get_dma_channel(chan);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
if (info->ops->free)
|
|
|
|
info->ops->free(channel);
|
|
|
|
|
|
|
|
atomic_set(&channel->busy, 0);
|
|
|
|
}
|
2006-11-24 06:13:52 +00:00
|
|
|
EXPORT_SYMBOL(free_dma);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
void dma_wait_for_completion(unsigned int chan)
|
|
|
|
{
|
|
|
|
struct dma_info *info = get_dma_info(chan);
|
2006-11-24 06:13:52 +00:00
|
|
|
struct dma_channel *channel = get_dma_channel(chan);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
if (channel->flags & DMA_TEI_CAPABLE) {
|
|
|
|
wait_event(channel->wait_queue,
|
|
|
|
(info->ops->get_residue(channel) == 0));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (info->ops->get_residue(channel))
|
|
|
|
cpu_relax();
|
|
|
|
}
|
2006-11-24 06:13:52 +00:00
|
|
|
EXPORT_SYMBOL(dma_wait_for_completion);
|
|
|
|
|
|
|
|
int register_chan_caps(const char *dmac, struct dma_chan_caps *caps)
|
|
|
|
{
|
|
|
|
struct dma_info *info;
|
|
|
|
unsigned int found = 0;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
list_for_each_entry(info, ®istered_dmac_list, list)
|
|
|
|
if (strcmp(dmac, info->name) == 0) {
|
|
|
|
found = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (unlikely(!found))
|
|
|
|
return -ENODEV;
|
|
|
|
|
|
|
|
for (i = 0; i < info->nr_channels; i++, caps++) {
|
|
|
|
struct dma_channel *channel;
|
|
|
|
|
|
|
|
if ((info->first_channel_nr + i) != caps->ch_num)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
channel = &info->channels[i];
|
|
|
|
channel->caps = caps->caplist;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(register_chan_caps);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
void dma_configure_channel(unsigned int chan, unsigned long flags)
|
|
|
|
{
|
|
|
|
struct dma_info *info = get_dma_info(chan);
|
2006-11-24 06:13:52 +00:00
|
|
|
struct dma_channel *channel = get_dma_channel(chan);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
if (info->ops->configure)
|
|
|
|
info->ops->configure(channel, flags);
|
|
|
|
}
|
2006-11-24 06:13:52 +00:00
|
|
|
EXPORT_SYMBOL(dma_configure_channel);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
int dma_xfer(unsigned int chan, unsigned long from,
|
|
|
|
unsigned long to, size_t size, unsigned int mode)
|
|
|
|
{
|
|
|
|
struct dma_info *info = get_dma_info(chan);
|
2006-11-24 06:13:52 +00:00
|
|
|
struct dma_channel *channel = get_dma_channel(chan);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
channel->sar = from;
|
|
|
|
channel->dar = to;
|
|
|
|
channel->count = size;
|
|
|
|
channel->mode = mode;
|
|
|
|
|
|
|
|
return info->ops->xfer(channel);
|
|
|
|
}
|
2006-11-24 06:13:52 +00:00
|
|
|
EXPORT_SYMBOL(dma_xfer);
|
|
|
|
|
|
|
|
int dma_extend(unsigned int chan, unsigned long op, void *param)
|
|
|
|
{
|
|
|
|
struct dma_info *info = get_dma_info(chan);
|
|
|
|
struct dma_channel *channel = get_dma_channel(chan);
|
|
|
|
|
|
|
|
if (info->ops->extend)
|
|
|
|
return info->ops->extend(channel, op, param);
|
|
|
|
|
|
|
|
return -ENOSYS;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(dma_extend);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
static int dma_read_proc(char *buf, char **start, off_t off,
|
|
|
|
int len, int *eof, void *data)
|
|
|
|
{
|
2006-01-17 06:14:09 +00:00
|
|
|
struct dma_info *info;
|
2005-04-16 22:20:36 +00:00
|
|
|
char *p = buf;
|
|
|
|
|
|
|
|
if (list_empty(®istered_dmac_list))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Iterate over each registered DMAC
|
|
|
|
*/
|
2006-01-17 06:14:09 +00:00
|
|
|
list_for_each_entry(info, ®istered_dmac_list, list) {
|
2005-04-16 22:20:36 +00:00
|
|
|
int i;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Iterate over each channel
|
|
|
|
*/
|
|
|
|
for (i = 0; i < info->nr_channels; i++) {
|
|
|
|
struct dma_channel *channel = info->channels + i;
|
|
|
|
|
|
|
|
if (!(channel->flags & DMA_CONFIGURED))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
p += sprintf(p, "%2d: %14s %s\n", i,
|
|
|
|
info->name, channel->dev_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return p - buf;
|
|
|
|
}
|
|
|
|
|
2006-01-17 06:14:09 +00:00
|
|
|
int register_dmac(struct dma_info *info)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2006-01-17 06:14:09 +00:00
|
|
|
unsigned int total_channels, i;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
INIT_LIST_HEAD(&info->list);
|
|
|
|
|
|
|
|
printk(KERN_INFO "DMA: Registering %s handler (%d channel%s).\n",
|
2006-11-24 06:13:52 +00:00
|
|
|
info->name, info->nr_channels, info->nr_channels > 1 ? "s" : "");
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
BUG_ON((info->flags & DMAC_CHANNELS_CONFIGURED) && !info->channels);
|
|
|
|
|
2006-01-17 06:14:09 +00:00
|
|
|
info->pdev = platform_device_register_simple((char *)info->name, -1,
|
|
|
|
NULL, 0);
|
|
|
|
if (IS_ERR(info->pdev))
|
|
|
|
return PTR_ERR(info->pdev);
|
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
/*
|
|
|
|
* Don't touch pre-configured channels
|
|
|
|
*/
|
|
|
|
if (!(info->flags & DMAC_CHANNELS_CONFIGURED)) {
|
|
|
|
unsigned int size;
|
|
|
|
|
|
|
|
size = sizeof(struct dma_channel) * info->nr_channels;
|
|
|
|
|
2006-11-24 06:13:52 +00:00
|
|
|
info->channels = kzalloc(size, GFP_KERNEL);
|
2005-04-16 22:20:36 +00:00
|
|
|
if (!info->channels)
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
2006-01-17 06:14:09 +00:00
|
|
|
total_channels = get_nr_channels();
|
2005-04-16 22:20:36 +00:00
|
|
|
for (i = 0; i < info->nr_channels; i++) {
|
2006-11-24 06:13:52 +00:00
|
|
|
struct dma_channel *chan = &info->channels[i];
|
|
|
|
|
|
|
|
atomic_set(&chan->busy, 0);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2006-11-24 06:13:52 +00:00
|
|
|
chan->chan = info->first_channel_nr + i;
|
|
|
|
chan->vchan = info->first_channel_nr + i + total_channels;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
memcpy(chan->dev_id, "Unused", 7);
|
|
|
|
|
|
|
|
if (info->flags & DMAC_CHANNELS_TEI_CAPABLE)
|
|
|
|
chan->flags |= DMA_TEI_CAPABLE;
|
|
|
|
|
|
|
|
init_waitqueue_head(&chan->wait_queue);
|
2006-01-17 06:14:09 +00:00
|
|
|
dma_create_sysfs_files(chan, info);
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
list_add(&info->list, ®istered_dmac_list);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2006-11-24 06:13:52 +00:00
|
|
|
EXPORT_SYMBOL(register_dmac);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2006-01-17 06:14:09 +00:00
|
|
|
void unregister_dmac(struct dma_info *info)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2006-01-17 06:14:09 +00:00
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
for (i = 0; i < info->nr_channels; i++)
|
|
|
|
dma_remove_sysfs_files(info->channels + i, info);
|
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
if (!(info->flags & DMAC_CHANNELS_CONFIGURED))
|
|
|
|
kfree(info->channels);
|
|
|
|
|
|
|
|
list_del(&info->list);
|
2006-01-17 06:14:09 +00:00
|
|
|
platform_device_unregister(info->pdev);
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
2006-11-24 06:13:52 +00:00
|
|
|
EXPORT_SYMBOL(unregister_dmac);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
static int __init dma_api_init(void)
|
|
|
|
{
|
2006-11-24 06:13:52 +00:00
|
|
|
printk(KERN_NOTICE "DMA: Registering DMA API.\n");
|
2005-04-16 22:20:36 +00:00
|
|
|
create_proc_read_entry("dma", 0, 0, dma_read_proc, 0);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
subsys_initcall(dma_api_init);
|
|
|
|
|
|
|
|
MODULE_AUTHOR("Paul Mundt <lethal@linux-sh.org>");
|
|
|
|
MODULE_DESCRIPTION("DMA API for SuperH");
|
|
|
|
MODULE_LICENSE("GPL");
|