Pull style into test branch
[linux-2.6] / arch / sh / drivers / dma / dma-api.c
1 /*
2  * arch/sh/drivers/dma/dma-api.c
3  *
4  * SuperH-specific DMA management API
5  *
6  * Copyright (C) 2003, 2004, 2005  Paul Mundt
7  *
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.
11  */
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 <asm/dma.h>
20
21 DEFINE_SPINLOCK(dma_spin_lock);
22 static LIST_HEAD(registered_dmac_list);
23
24 struct dma_info *get_dma_info(unsigned int chan)
25 {
26         struct dma_info *info;
27
28         /*
29          * Look for each DMAC's range to determine who the owner of
30          * the channel is.
31          */
32         list_for_each_entry(info, &registered_dmac_list, list) {
33                 if ((chan <  info->first_channel_nr) ||
34                     (chan >= info->first_channel_nr + info->nr_channels))
35                         continue;
36
37                 return info;
38         }
39
40         return NULL;
41 }
42 EXPORT_SYMBOL(get_dma_info);
43
44 struct dma_info *get_dma_info_by_name(const char *dmac_name)
45 {
46         struct dma_info *info;
47
48         list_for_each_entry(info, &registered_dmac_list, list) {
49                 if (dmac_name && (strcmp(dmac_name, info->name) != 0))
50                         continue;
51                 else
52                         return info;
53         }
54
55         return NULL;
56 }
57 EXPORT_SYMBOL(get_dma_info_by_name);
58
59 static unsigned int get_nr_channels(void)
60 {
61         struct dma_info *info;
62         unsigned int nr = 0;
63
64         if (unlikely(list_empty(&registered_dmac_list)))
65                 return nr;
66
67         list_for_each_entry(info, &registered_dmac_list, list)
68                 nr += info->nr_channels;
69
70         return nr;
71 }
72
73 struct dma_channel *get_dma_channel(unsigned int chan)
74 {
75         struct dma_info *info = get_dma_info(chan);
76         struct dma_channel *channel;
77         int i;
78
79         if (unlikely(!info))
80                 return ERR_PTR(-EINVAL);
81
82         for (i = 0; i < info->nr_channels; i++) {
83                 channel = &info->channels[i];
84                 if (channel->chan == chan)
85                         return channel;
86         }
87
88         return NULL;
89 }
90 EXPORT_SYMBOL(get_dma_channel);
91
92 int get_dma_residue(unsigned int chan)
93 {
94         struct dma_info *info = get_dma_info(chan);
95         struct dma_channel *channel = get_dma_channel(chan);
96
97         if (info->ops->get_residue)
98                 return info->ops->get_residue(channel);
99
100         return 0;
101 }
102 EXPORT_SYMBOL(get_dma_residue);
103
104 static int search_cap(const char **haystack, const char *needle)
105 {
106         const char **p;
107
108         for (p = haystack; *p; p++)
109                 if (strcmp(*p, needle) == 0)
110                         return 1;
111
112         return 0;
113 }
114
115 /**
116  * request_dma_bycap - Allocate a DMA channel based on its capabilities
117  * @dmac: List of DMA controllers to search
118  * @caps: List of capabilites
119  *
120  * Search all channels of all DMA controllers to find a channel which
121  * matches the requested capabilities. The result is the channel
122  * number if a match is found, or %-ENODEV if no match is found.
123  *
124  * Note that not all DMA controllers export capabilities, in which
125  * case they can never be allocated using this API, and so
126  * request_dma() must be used specifying the channel number.
127  */
128 int request_dma_bycap(const char **dmac, const char **caps, const char *dev_id)
129 {
130         unsigned int found = 0;
131         struct dma_info *info;
132         const char **p;
133         int i;
134
135         BUG_ON(!dmac || !caps);
136
137         list_for_each_entry(info, &registered_dmac_list, list)
138                 if (strcmp(*dmac, info->name) == 0) {
139                         found = 1;
140                         break;
141                 }
142
143         if (!found)
144                 return -ENODEV;
145
146         for (i = 0; i < info->nr_channels; i++) {
147                 struct dma_channel *channel = &info->channels[i];
148
149                 if (unlikely(!channel->caps))
150                         continue;
151
152                 for (p = caps; *p; p++) {
153                         if (!search_cap(channel->caps, *p))
154                                 break;
155                         if (request_dma(channel->chan, dev_id) == 0)
156                                 return channel->chan;
157                 }
158         }
159
160         return -EINVAL;
161 }
162 EXPORT_SYMBOL(request_dma_bycap);
163
164 int dmac_search_free_channel(const char *dev_id)
165 {
166         struct dma_channel *channel = { 0 };
167         struct dma_info *info = get_dma_info(0);
168         int i;
169
170         for (i = 0; i < info->nr_channels; i++) {
171                 channel = &info->channels[i];
172                 if (unlikely(!channel))
173                         return -ENODEV;
174
175                 if (atomic_read(&channel->busy) == 0)
176                         break;
177         }
178
179         if (info->ops->request) {
180                 int result = info->ops->request(channel);
181                 if (result)
182                         return result;
183
184                 atomic_set(&channel->busy, 1);
185                 return channel->chan;
186         }
187
188         return -ENOSYS;
189 }
190
191 int request_dma(unsigned int chan, const char *dev_id)
192 {
193         struct dma_channel *channel = { 0 };
194         struct dma_info *info = get_dma_info(chan);
195         int result;
196
197         channel = get_dma_channel(chan);
198         if (atomic_xchg(&channel->busy, 1))
199                 return -EBUSY;
200
201         strlcpy(channel->dev_id, dev_id, sizeof(channel->dev_id));
202
203         if (info->ops->request) {
204                 result = info->ops->request(channel);
205                 if (result)
206                         atomic_set(&channel->busy, 0);
207
208                 return result;
209         }
210
211         return 0;
212 }
213 EXPORT_SYMBOL(request_dma);
214
215 void free_dma(unsigned int chan)
216 {
217         struct dma_info *info = get_dma_info(chan);
218         struct dma_channel *channel = get_dma_channel(chan);
219
220         if (info->ops->free)
221                 info->ops->free(channel);
222
223         atomic_set(&channel->busy, 0);
224 }
225 EXPORT_SYMBOL(free_dma);
226
227 void dma_wait_for_completion(unsigned int chan)
228 {
229         struct dma_info *info = get_dma_info(chan);
230         struct dma_channel *channel = get_dma_channel(chan);
231
232         if (channel->flags & DMA_TEI_CAPABLE) {
233                 wait_event(channel->wait_queue,
234                            (info->ops->get_residue(channel) == 0));
235                 return;
236         }
237
238         while (info->ops->get_residue(channel))
239                 cpu_relax();
240 }
241 EXPORT_SYMBOL(dma_wait_for_completion);
242
243 int register_chan_caps(const char *dmac, struct dma_chan_caps *caps)
244 {
245         struct dma_info *info;
246         unsigned int found = 0;
247         int i;
248
249         list_for_each_entry(info, &registered_dmac_list, list)
250                 if (strcmp(dmac, info->name) == 0) {
251                         found = 1;
252                         break;
253                 }
254
255         if (unlikely(!found))
256                 return -ENODEV;
257
258         for (i = 0; i < info->nr_channels; i++, caps++) {
259                 struct dma_channel *channel;
260
261                 if ((info->first_channel_nr + i) != caps->ch_num)
262                         return -EINVAL;
263
264                 channel = &info->channels[i];
265                 channel->caps = caps->caplist;
266         }
267
268         return 0;
269 }
270 EXPORT_SYMBOL(register_chan_caps);
271
272 void dma_configure_channel(unsigned int chan, unsigned long flags)
273 {
274         struct dma_info *info = get_dma_info(chan);
275         struct dma_channel *channel = get_dma_channel(chan);
276
277         if (info->ops->configure)
278                 info->ops->configure(channel, flags);
279 }
280 EXPORT_SYMBOL(dma_configure_channel);
281
282 int dma_xfer(unsigned int chan, unsigned long from,
283              unsigned long to, size_t size, unsigned int mode)
284 {
285         struct dma_info *info = get_dma_info(chan);
286         struct dma_channel *channel = get_dma_channel(chan);
287
288         channel->sar    = from;
289         channel->dar    = to;
290         channel->count  = size;
291         channel->mode   = mode;
292
293         return info->ops->xfer(channel);
294 }
295 EXPORT_SYMBOL(dma_xfer);
296
297 int dma_extend(unsigned int chan, unsigned long op, void *param)
298 {
299         struct dma_info *info = get_dma_info(chan);
300         struct dma_channel *channel = get_dma_channel(chan);
301
302         if (info->ops->extend)
303                 return info->ops->extend(channel, op, param);
304
305         return -ENOSYS;
306 }
307 EXPORT_SYMBOL(dma_extend);
308
309 static int dma_read_proc(char *buf, char **start, off_t off,
310                          int len, int *eof, void *data)
311 {
312         struct dma_info *info;
313         char *p = buf;
314
315         if (list_empty(&registered_dmac_list))
316                 return 0;
317
318         /*
319          * Iterate over each registered DMAC
320          */
321         list_for_each_entry(info, &registered_dmac_list, list) {
322                 int i;
323
324                 /*
325                  * Iterate over each channel
326                  */
327                 for (i = 0; i < info->nr_channels; i++) {
328                         struct dma_channel *channel = info->channels + i;
329
330                         if (!(channel->flags & DMA_CONFIGURED))
331                                 continue;
332
333                         p += sprintf(p, "%2d: %14s    %s\n", i,
334                                      info->name, channel->dev_id);
335                 }
336         }
337
338         return p - buf;
339 }
340
341 int register_dmac(struct dma_info *info)
342 {
343         unsigned int total_channels, i;
344
345         INIT_LIST_HEAD(&info->list);
346
347         printk(KERN_INFO "DMA: Registering %s handler (%d channel%s).\n",
348                info->name, info->nr_channels, info->nr_channels > 1 ? "s" : "");
349
350         BUG_ON((info->flags & DMAC_CHANNELS_CONFIGURED) && !info->channels);
351
352         info->pdev = platform_device_register_simple((char *)info->name, -1,
353                                                      NULL, 0);
354         if (IS_ERR(info->pdev))
355                 return PTR_ERR(info->pdev);
356
357         /*
358          * Don't touch pre-configured channels
359          */
360         if (!(info->flags & DMAC_CHANNELS_CONFIGURED)) {
361                 unsigned int size;
362
363                 size = sizeof(struct dma_channel) * info->nr_channels;
364
365                 info->channels = kzalloc(size, GFP_KERNEL);
366                 if (!info->channels)
367                         return -ENOMEM;
368         }
369
370         total_channels = get_nr_channels();
371         for (i = 0; i < info->nr_channels; i++) {
372                 struct dma_channel *chan = &info->channels[i];
373
374                 atomic_set(&chan->busy, 0);
375
376                 chan->chan  = info->first_channel_nr + i;
377                 chan->vchan = info->first_channel_nr + i + total_channels;
378
379                 memcpy(chan->dev_id, "Unused", 7);
380
381                 if (info->flags & DMAC_CHANNELS_TEI_CAPABLE)
382                         chan->flags |= DMA_TEI_CAPABLE;
383
384                 init_waitqueue_head(&chan->wait_queue);
385                 dma_create_sysfs_files(chan, info);
386         }
387
388         list_add(&info->list, &registered_dmac_list);
389
390         return 0;
391 }
392 EXPORT_SYMBOL(register_dmac);
393
394 void unregister_dmac(struct dma_info *info)
395 {
396         unsigned int i;
397
398         for (i = 0; i < info->nr_channels; i++)
399                 dma_remove_sysfs_files(info->channels + i, info);
400
401         if (!(info->flags & DMAC_CHANNELS_CONFIGURED))
402                 kfree(info->channels);
403
404         list_del(&info->list);
405         platform_device_unregister(info->pdev);
406 }
407 EXPORT_SYMBOL(unregister_dmac);
408
409 static int __init dma_api_init(void)
410 {
411         printk(KERN_NOTICE "DMA: Registering DMA API.\n");
412         create_proc_read_entry("dma", 0, 0, dma_read_proc, 0);
413         return 0;
414 }
415 subsys_initcall(dma_api_init);
416
417 MODULE_AUTHOR("Paul Mundt <lethal@linux-sh.org>");
418 MODULE_DESCRIPTION("DMA API for SuperH");
419 MODULE_LICENSE("GPL");