2 * arch/sh/drivers/dma/dma-api.c
4 * SuperH-specific DMA management API
6 * Copyright (C) 2003, 2004, 2005 Paul Mundt
8 * This file is subject to the terms and conditions of the GNU General Public
9 * License. See the file "COPYING" in the main directory of this archive
12 #include <linux/init.h>
13 #include <linux/module.h>
14 #include <linux/spinlock.h>
15 #include <linux/proc_fs.h>
16 #include <linux/list.h>
17 #include <linux/platform_device.h>
19 #include <linux/sched.h>
22 DEFINE_SPINLOCK(dma_spin_lock);
23 static LIST_HEAD(registered_dmac_list);
25 struct dma_info *get_dma_info(unsigned int chan)
27 struct dma_info *info;
30 * Look for each DMAC's range to determine who the owner of
33 list_for_each_entry(info, ®istered_dmac_list, list) {
34 if ((chan < info->first_vchannel_nr) ||
35 (chan >= info->first_vchannel_nr + info->nr_channels))
43 EXPORT_SYMBOL(get_dma_info);
45 struct dma_info *get_dma_info_by_name(const char *dmac_name)
47 struct dma_info *info;
49 list_for_each_entry(info, ®istered_dmac_list, list) {
50 if (dmac_name && (strcmp(dmac_name, info->name) != 0))
58 EXPORT_SYMBOL(get_dma_info_by_name);
60 static unsigned int get_nr_channels(void)
62 struct dma_info *info;
65 if (unlikely(list_empty(®istered_dmac_list)))
68 list_for_each_entry(info, ®istered_dmac_list, list)
69 nr += info->nr_channels;
74 struct dma_channel *get_dma_channel(unsigned int chan)
76 struct dma_info *info = get_dma_info(chan);
77 struct dma_channel *channel;
81 return ERR_PTR(-EINVAL);
83 for (i = 0; i < info->nr_channels; i++) {
84 channel = &info->channels[i];
85 if (channel->vchan == chan)
91 EXPORT_SYMBOL(get_dma_channel);
93 int get_dma_residue(unsigned int chan)
95 struct dma_info *info = get_dma_info(chan);
96 struct dma_channel *channel = get_dma_channel(chan);
98 if (info->ops->get_residue)
99 return info->ops->get_residue(channel);
103 EXPORT_SYMBOL(get_dma_residue);
105 static int search_cap(const char **haystack, const char *needle)
109 for (p = haystack; *p; p++)
110 if (strcmp(*p, needle) == 0)
117 * request_dma_bycap - Allocate a DMA channel based on its capabilities
118 * @dmac: List of DMA controllers to search
119 * @caps: List of capabilities
121 * Search all channels of all DMA controllers to find a channel which
122 * matches the requested capabilities. The result is the channel
123 * number if a match is found, or %-ENODEV if no match is found.
125 * Note that not all DMA controllers export capabilities, in which
126 * case they can never be allocated using this API, and so
127 * request_dma() must be used specifying the channel number.
129 int request_dma_bycap(const char **dmac, const char **caps, const char *dev_id)
131 unsigned int found = 0;
132 struct dma_info *info;
136 BUG_ON(!dmac || !caps);
138 list_for_each_entry(info, ®istered_dmac_list, list)
139 if (strcmp(*dmac, info->name) == 0) {
147 for (i = 0; i < info->nr_channels; i++) {
148 struct dma_channel *channel = &info->channels[i];
150 if (unlikely(!channel->caps))
153 for (p = caps; *p; p++) {
154 if (!search_cap(channel->caps, *p))
156 if (request_dma(channel->chan, dev_id) == 0)
157 return channel->chan;
163 EXPORT_SYMBOL(request_dma_bycap);
165 int dmac_search_free_channel(const char *dev_id)
167 struct dma_channel *channel = { 0 };
168 struct dma_info *info = get_dma_info(0);
171 for (i = 0; i < info->nr_channels; i++) {
172 channel = &info->channels[i];
173 if (unlikely(!channel))
176 if (atomic_read(&channel->busy) == 0)
180 if (info->ops->request) {
181 int result = info->ops->request(channel);
185 atomic_set(&channel->busy, 1);
186 return channel->chan;
192 int request_dma(unsigned int chan, const char *dev_id)
194 struct dma_channel *channel = { 0 };
195 struct dma_info *info = get_dma_info(chan);
198 channel = get_dma_channel(chan);
199 if (atomic_xchg(&channel->busy, 1))
202 strlcpy(channel->dev_id, dev_id, sizeof(channel->dev_id));
204 if (info->ops->request) {
205 result = info->ops->request(channel);
207 atomic_set(&channel->busy, 0);
214 EXPORT_SYMBOL(request_dma);
216 void free_dma(unsigned int chan)
218 struct dma_info *info = get_dma_info(chan);
219 struct dma_channel *channel = get_dma_channel(chan);
222 info->ops->free(channel);
224 atomic_set(&channel->busy, 0);
226 EXPORT_SYMBOL(free_dma);
228 void dma_wait_for_completion(unsigned int chan)
230 struct dma_info *info = get_dma_info(chan);
231 struct dma_channel *channel = get_dma_channel(chan);
233 if (channel->flags & DMA_TEI_CAPABLE) {
234 wait_event(channel->wait_queue,
235 (info->ops->get_residue(channel) == 0));
239 while (info->ops->get_residue(channel))
242 EXPORT_SYMBOL(dma_wait_for_completion);
244 int register_chan_caps(const char *dmac, struct dma_chan_caps *caps)
246 struct dma_info *info;
247 unsigned int found = 0;
250 list_for_each_entry(info, ®istered_dmac_list, list)
251 if (strcmp(dmac, info->name) == 0) {
256 if (unlikely(!found))
259 for (i = 0; i < info->nr_channels; i++, caps++) {
260 struct dma_channel *channel;
262 if ((info->first_channel_nr + i) != caps->ch_num)
265 channel = &info->channels[i];
266 channel->caps = caps->caplist;
271 EXPORT_SYMBOL(register_chan_caps);
273 void dma_configure_channel(unsigned int chan, unsigned long flags)
275 struct dma_info *info = get_dma_info(chan);
276 struct dma_channel *channel = get_dma_channel(chan);
278 if (info->ops->configure)
279 info->ops->configure(channel, flags);
281 EXPORT_SYMBOL(dma_configure_channel);
283 int dma_xfer(unsigned int chan, unsigned long from,
284 unsigned long to, size_t size, unsigned int mode)
286 struct dma_info *info = get_dma_info(chan);
287 struct dma_channel *channel = get_dma_channel(chan);
291 channel->count = size;
292 channel->mode = mode;
294 return info->ops->xfer(channel);
296 EXPORT_SYMBOL(dma_xfer);
298 int dma_extend(unsigned int chan, unsigned long op, void *param)
300 struct dma_info *info = get_dma_info(chan);
301 struct dma_channel *channel = get_dma_channel(chan);
303 if (info->ops->extend)
304 return info->ops->extend(channel, op, param);
308 EXPORT_SYMBOL(dma_extend);
310 static int dma_read_proc(char *buf, char **start, off_t off,
311 int len, int *eof, void *data)
313 struct dma_info *info;
316 if (list_empty(®istered_dmac_list))
320 * Iterate over each registered DMAC
322 list_for_each_entry(info, ®istered_dmac_list, list) {
326 * Iterate over each channel
328 for (i = 0; i < info->nr_channels; i++) {
329 struct dma_channel *channel = info->channels + i;
331 if (!(channel->flags & DMA_CONFIGURED))
334 p += sprintf(p, "%2d: %14s %s\n", i,
335 info->name, channel->dev_id);
342 int register_dmac(struct dma_info *info)
344 unsigned int total_channels, i;
346 INIT_LIST_HEAD(&info->list);
348 printk(KERN_INFO "DMA: Registering %s handler (%d channel%s).\n",
349 info->name, info->nr_channels, info->nr_channels > 1 ? "s" : "");
351 BUG_ON((info->flags & DMAC_CHANNELS_CONFIGURED) && !info->channels);
353 info->pdev = platform_device_register_simple(info->name, -1,
355 if (IS_ERR(info->pdev))
356 return PTR_ERR(info->pdev);
359 * Don't touch pre-configured channels
361 if (!(info->flags & DMAC_CHANNELS_CONFIGURED)) {
364 size = sizeof(struct dma_channel) * info->nr_channels;
366 info->channels = kzalloc(size, GFP_KERNEL);
371 total_channels = get_nr_channels();
372 info->first_vchannel_nr = total_channels;
373 for (i = 0; i < info->nr_channels; i++) {
374 struct dma_channel *chan = &info->channels[i];
376 atomic_set(&chan->busy, 0);
378 chan->chan = info->first_channel_nr + i;
379 chan->vchan = info->first_channel_nr + i + total_channels;
381 memcpy(chan->dev_id, "Unused", 7);
383 if (info->flags & DMAC_CHANNELS_TEI_CAPABLE)
384 chan->flags |= DMA_TEI_CAPABLE;
386 init_waitqueue_head(&chan->wait_queue);
387 dma_create_sysfs_files(chan, info);
390 list_add(&info->list, ®istered_dmac_list);
394 EXPORT_SYMBOL(register_dmac);
396 void unregister_dmac(struct dma_info *info)
400 for (i = 0; i < info->nr_channels; i++)
401 dma_remove_sysfs_files(info->channels + i, info);
403 if (!(info->flags & DMAC_CHANNELS_CONFIGURED))
404 kfree(info->channels);
406 list_del(&info->list);
407 platform_device_unregister(info->pdev);
409 EXPORT_SYMBOL(unregister_dmac);
411 static int __init dma_api_init(void)
413 printk(KERN_NOTICE "DMA: Registering DMA API.\n");
414 create_proc_read_entry("dma", 0, 0, dma_read_proc, 0);
417 subsys_initcall(dma_api_init);
419 MODULE_AUTHOR("Paul Mundt <lethal@linux-sh.org>");
420 MODULE_DESCRIPTION("DMA API for SuperH");
421 MODULE_LICENSE("GPL");