spi-topcliff-pch: add recovery processing in case wait-event timeout
[zen-stable.git] / arch / sh / drivers / dma / dma-api.c
blobf46848f088e47c2eb900b890cbe656580f5f3a37
1 /*
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
10 * for more details.
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>
18 #include <linux/mm.h>
19 #include <linux/sched.h>
20 #include <linux/slab.h>
21 #include <asm/dma.h>
23 DEFINE_SPINLOCK(dma_spin_lock);
24 static LIST_HEAD(registered_dmac_list);
26 struct dma_info *get_dma_info(unsigned int chan)
28 struct dma_info *info;
31 * Look for each DMAC's range to determine who the owner of
32 * the channel is.
34 list_for_each_entry(info, &registered_dmac_list, list) {
35 if ((chan < info->first_vchannel_nr) ||
36 (chan >= info->first_vchannel_nr + info->nr_channels))
37 continue;
39 return info;
42 return NULL;
44 EXPORT_SYMBOL(get_dma_info);
46 struct dma_info *get_dma_info_by_name(const char *dmac_name)
48 struct dma_info *info;
50 list_for_each_entry(info, &registered_dmac_list, list) {
51 if (dmac_name && (strcmp(dmac_name, info->name) != 0))
52 continue;
53 else
54 return info;
57 return NULL;
59 EXPORT_SYMBOL(get_dma_info_by_name);
61 static unsigned int get_nr_channels(void)
63 struct dma_info *info;
64 unsigned int nr = 0;
66 if (unlikely(list_empty(&registered_dmac_list)))
67 return nr;
69 list_for_each_entry(info, &registered_dmac_list, list)
70 nr += info->nr_channels;
72 return nr;
75 struct dma_channel *get_dma_channel(unsigned int chan)
77 struct dma_info *info = get_dma_info(chan);
78 struct dma_channel *channel;
79 int i;
81 if (unlikely(!info))
82 return ERR_PTR(-EINVAL);
84 for (i = 0; i < info->nr_channels; i++) {
85 channel = &info->channels[i];
86 if (channel->vchan == chan)
87 return channel;
90 return NULL;
92 EXPORT_SYMBOL(get_dma_channel);
94 int get_dma_residue(unsigned int chan)
96 struct dma_info *info = get_dma_info(chan);
97 struct dma_channel *channel = get_dma_channel(chan);
99 if (info->ops->get_residue)
100 return info->ops->get_residue(channel);
102 return 0;
104 EXPORT_SYMBOL(get_dma_residue);
106 static int search_cap(const char **haystack, const char *needle)
108 const char **p;
110 for (p = haystack; *p; p++)
111 if (strcmp(*p, needle) == 0)
112 return 1;
114 return 0;
118 * request_dma_bycap - Allocate a DMA channel based on its capabilities
119 * @dmac: List of DMA controllers to search
120 * @caps: List of capabilities
122 * Search all channels of all DMA controllers to find a channel which
123 * matches the requested capabilities. The result is the channel
124 * number if a match is found, or %-ENODEV if no match is found.
126 * Note that not all DMA controllers export capabilities, in which
127 * case they can never be allocated using this API, and so
128 * request_dma() must be used specifying the channel number.
130 int request_dma_bycap(const char **dmac, const char **caps, const char *dev_id)
132 unsigned int found = 0;
133 struct dma_info *info;
134 const char **p;
135 int i;
137 BUG_ON(!dmac || !caps);
139 list_for_each_entry(info, &registered_dmac_list, list)
140 if (strcmp(*dmac, info->name) == 0) {
141 found = 1;
142 break;
145 if (!found)
146 return -ENODEV;
148 for (i = 0; i < info->nr_channels; i++) {
149 struct dma_channel *channel = &info->channels[i];
151 if (unlikely(!channel->caps))
152 continue;
154 for (p = caps; *p; p++) {
155 if (!search_cap(channel->caps, *p))
156 break;
157 if (request_dma(channel->chan, dev_id) == 0)
158 return channel->chan;
162 return -EINVAL;
164 EXPORT_SYMBOL(request_dma_bycap);
166 int dmac_search_free_channel(const char *dev_id)
168 struct dma_channel *channel = { 0 };
169 struct dma_info *info = get_dma_info(0);
170 int i;
172 for (i = 0; i < info->nr_channels; i++) {
173 channel = &info->channels[i];
174 if (unlikely(!channel))
175 return -ENODEV;
177 if (atomic_read(&channel->busy) == 0)
178 break;
181 if (info->ops->request) {
182 int result = info->ops->request(channel);
183 if (result)
184 return result;
186 atomic_set(&channel->busy, 1);
187 return channel->chan;
190 return -ENOSYS;
193 int request_dma(unsigned int chan, const char *dev_id)
195 struct dma_channel *channel = { 0 };
196 struct dma_info *info = get_dma_info(chan);
197 int result;
199 channel = get_dma_channel(chan);
200 if (atomic_xchg(&channel->busy, 1))
201 return -EBUSY;
203 strlcpy(channel->dev_id, dev_id, sizeof(channel->dev_id));
205 if (info->ops->request) {
206 result = info->ops->request(channel);
207 if (result)
208 atomic_set(&channel->busy, 0);
210 return result;
213 return 0;
215 EXPORT_SYMBOL(request_dma);
217 void free_dma(unsigned int chan)
219 struct dma_info *info = get_dma_info(chan);
220 struct dma_channel *channel = get_dma_channel(chan);
222 if (info->ops->free)
223 info->ops->free(channel);
225 atomic_set(&channel->busy, 0);
227 EXPORT_SYMBOL(free_dma);
229 void dma_wait_for_completion(unsigned int chan)
231 struct dma_info *info = get_dma_info(chan);
232 struct dma_channel *channel = get_dma_channel(chan);
234 if (channel->flags & DMA_TEI_CAPABLE) {
235 wait_event(channel->wait_queue,
236 (info->ops->get_residue(channel) == 0));
237 return;
240 while (info->ops->get_residue(channel))
241 cpu_relax();
243 EXPORT_SYMBOL(dma_wait_for_completion);
245 int register_chan_caps(const char *dmac, struct dma_chan_caps *caps)
247 struct dma_info *info;
248 unsigned int found = 0;
249 int i;
251 list_for_each_entry(info, &registered_dmac_list, list)
252 if (strcmp(dmac, info->name) == 0) {
253 found = 1;
254 break;
257 if (unlikely(!found))
258 return -ENODEV;
260 for (i = 0; i < info->nr_channels; i++, caps++) {
261 struct dma_channel *channel;
263 if ((info->first_channel_nr + i) != caps->ch_num)
264 return -EINVAL;
266 channel = &info->channels[i];
267 channel->caps = caps->caplist;
270 return 0;
272 EXPORT_SYMBOL(register_chan_caps);
274 void dma_configure_channel(unsigned int chan, unsigned long flags)
276 struct dma_info *info = get_dma_info(chan);
277 struct dma_channel *channel = get_dma_channel(chan);
279 if (info->ops->configure)
280 info->ops->configure(channel, flags);
282 EXPORT_SYMBOL(dma_configure_channel);
284 int dma_xfer(unsigned int chan, unsigned long from,
285 unsigned long to, size_t size, unsigned int mode)
287 struct dma_info *info = get_dma_info(chan);
288 struct dma_channel *channel = get_dma_channel(chan);
290 channel->sar = from;
291 channel->dar = to;
292 channel->count = size;
293 channel->mode = mode;
295 return info->ops->xfer(channel);
297 EXPORT_SYMBOL(dma_xfer);
299 int dma_extend(unsigned int chan, unsigned long op, void *param)
301 struct dma_info *info = get_dma_info(chan);
302 struct dma_channel *channel = get_dma_channel(chan);
304 if (info->ops->extend)
305 return info->ops->extend(channel, op, param);
307 return -ENOSYS;
309 EXPORT_SYMBOL(dma_extend);
311 static int dma_read_proc(char *buf, char **start, off_t off,
312 int len, int *eof, void *data)
314 struct dma_info *info;
315 char *p = buf;
317 if (list_empty(&registered_dmac_list))
318 return 0;
321 * Iterate over each registered DMAC
323 list_for_each_entry(info, &registered_dmac_list, list) {
324 int i;
327 * Iterate over each channel
329 for (i = 0; i < info->nr_channels; i++) {
330 struct dma_channel *channel = info->channels + i;
332 if (!(channel->flags & DMA_CONFIGURED))
333 continue;
335 p += sprintf(p, "%2d: %14s %s\n", i,
336 info->name, channel->dev_id);
340 return p - buf;
343 int register_dmac(struct dma_info *info)
345 unsigned int total_channels, i;
347 INIT_LIST_HEAD(&info->list);
349 printk(KERN_INFO "DMA: Registering %s handler (%d channel%s).\n",
350 info->name, info->nr_channels, info->nr_channels > 1 ? "s" : "");
352 BUG_ON((info->flags & DMAC_CHANNELS_CONFIGURED) && !info->channels);
354 info->pdev = platform_device_register_simple(info->name, -1,
355 NULL, 0);
356 if (IS_ERR(info->pdev))
357 return PTR_ERR(info->pdev);
360 * Don't touch pre-configured channels
362 if (!(info->flags & DMAC_CHANNELS_CONFIGURED)) {
363 unsigned int size;
365 size = sizeof(struct dma_channel) * info->nr_channels;
367 info->channels = kzalloc(size, GFP_KERNEL);
368 if (!info->channels)
369 return -ENOMEM;
372 total_channels = get_nr_channels();
373 info->first_vchannel_nr = total_channels;
374 for (i = 0; i < info->nr_channels; i++) {
375 struct dma_channel *chan = &info->channels[i];
377 atomic_set(&chan->busy, 0);
379 chan->chan = info->first_channel_nr + i;
380 chan->vchan = info->first_channel_nr + i + total_channels;
382 memcpy(chan->dev_id, "Unused", 7);
384 if (info->flags & DMAC_CHANNELS_TEI_CAPABLE)
385 chan->flags |= DMA_TEI_CAPABLE;
387 init_waitqueue_head(&chan->wait_queue);
388 dma_create_sysfs_files(chan, info);
391 list_add(&info->list, &registered_dmac_list);
393 return 0;
395 EXPORT_SYMBOL(register_dmac);
397 void unregister_dmac(struct dma_info *info)
399 unsigned int i;
401 for (i = 0; i < info->nr_channels; i++)
402 dma_remove_sysfs_files(info->channels + i, info);
404 if (!(info->flags & DMAC_CHANNELS_CONFIGURED))
405 kfree(info->channels);
407 list_del(&info->list);
408 platform_device_unregister(info->pdev);
410 EXPORT_SYMBOL(unregister_dmac);
412 static int __init dma_api_init(void)
414 printk(KERN_NOTICE "DMA: Registering DMA API.\n");
415 return create_proc_read_entry("dma", 0, 0, dma_read_proc, 0)
416 ? 0 : -ENOMEM;
418 subsys_initcall(dma_api_init);
420 MODULE_AUTHOR("Paul Mundt <lethal@linux-sh.org>");
421 MODULE_DESCRIPTION("DMA API for SuperH");
422 MODULE_LICENSE("GPL");