blob: c0eec08d8f959a5692f861af794abf9b606582e5 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * arch/sh/drivers/dma/dma-api.c
3 *
4 * SuperH-specific DMA management API
5 *
Paul Mundt0d831772006-01-16 22:14:09 -08006 * Copyright (C) 2003, 2004, 2005 Paul Mundt
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/spinlock.h>
15#include <linux/proc_fs.h>
David Howells64f09622013-04-11 00:01:27 +010016#include <linux/seq_file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/list.h>
Paul Mundt0d831772006-01-16 22:14:09 -080018#include <linux/platform_device.h>
Mark Glaisherdb9b99d2006-11-24 15:13:52 +090019#include <linux/mm.h>
Manuel Laussbdff33d2007-05-31 13:44:17 +090020#include <linux/sched.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090021#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <asm/dma.h>
23
24DEFINE_SPINLOCK(dma_spin_lock);
25static LIST_HEAD(registered_dmac_list);
26
Linus Torvalds1da177e2005-04-16 15:20:36 -070027struct dma_info *get_dma_info(unsigned int chan)
28{
Paul Mundt0d831772006-01-16 22:14:09 -080029 struct dma_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
31 /*
32 * Look for each DMAC's range to determine who the owner of
33 * the channel is.
34 */
Paul Mundt0d831772006-01-16 22:14:09 -080035 list_for_each_entry(info, &registered_dmac_list, list) {
Adrian McMenamineb695db2007-07-24 13:30:55 +090036 if ((chan < info->first_vchannel_nr) ||
37 (chan >= info->first_vchannel_nr + info->nr_channels))
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 continue;
39
40 return info;
41 }
42
43 return NULL;
44}
Mark Glaisherdb9b99d2006-11-24 15:13:52 +090045EXPORT_SYMBOL(get_dma_info);
46
47struct dma_info *get_dma_info_by_name(const char *dmac_name)
48{
49 struct dma_info *info;
50
51 list_for_each_entry(info, &registered_dmac_list, list) {
52 if (dmac_name && (strcmp(dmac_name, info->name) != 0))
53 continue;
54 else
55 return info;
56 }
57
58 return NULL;
59}
60EXPORT_SYMBOL(get_dma_info_by_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Paul Mundt0d831772006-01-16 22:14:09 -080062static unsigned int get_nr_channels(void)
63{
64 struct dma_info *info;
65 unsigned int nr = 0;
66
67 if (unlikely(list_empty(&registered_dmac_list)))
68 return nr;
69
70 list_for_each_entry(info, &registered_dmac_list, list)
71 nr += info->nr_channels;
72
73 return nr;
74}
75
Linus Torvalds1da177e2005-04-16 15:20:36 -070076struct dma_channel *get_dma_channel(unsigned int chan)
77{
78 struct dma_info *info = get_dma_info(chan);
Mark Glaisherdb9b99d2006-11-24 15:13:52 +090079 struct dma_channel *channel;
80 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
Mark Glaisherdb9b99d2006-11-24 15:13:52 +090082 if (unlikely(!info))
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 return ERR_PTR(-EINVAL);
84
Mark Glaisherdb9b99d2006-11-24 15:13:52 +090085 for (i = 0; i < info->nr_channels; i++) {
86 channel = &info->channels[i];
Adrian McMenamineb695db2007-07-24 13:30:55 +090087 if (channel->vchan == chan)
Mark Glaisherdb9b99d2006-11-24 15:13:52 +090088 return channel;
89 }
90
91 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092}
Mark Glaisherdb9b99d2006-11-24 15:13:52 +090093EXPORT_SYMBOL(get_dma_channel);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
95int get_dma_residue(unsigned int chan)
96{
97 struct dma_info *info = get_dma_info(chan);
Mark Glaisherdb9b99d2006-11-24 15:13:52 +090098 struct dma_channel *channel = get_dma_channel(chan);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
100 if (info->ops->get_residue)
101 return info->ops->get_residue(channel);
102
103 return 0;
104}
Mark Glaisherdb9b99d2006-11-24 15:13:52 +0900105EXPORT_SYMBOL(get_dma_residue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
Mark Glaisherdb9b99d2006-11-24 15:13:52 +0900107static int search_cap(const char **haystack, const char *needle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108{
Mark Glaisherdb9b99d2006-11-24 15:13:52 +0900109 const char **p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
Mark Glaisherdb9b99d2006-11-24 15:13:52 +0900111 for (p = haystack; *p; p++)
112 if (strcmp(*p, needle) == 0)
113 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
115 return 0;
116}
117
Mark Glaisherdb9b99d2006-11-24 15:13:52 +0900118/**
119 * request_dma_bycap - Allocate a DMA channel based on its capabilities
120 * @dmac: List of DMA controllers to search
Simon Arlotte868d612007-05-14 08:15:10 +0900121 * @caps: List of capabilities
Mark Glaisherdb9b99d2006-11-24 15:13:52 +0900122 *
123 * Search all channels of all DMA controllers to find a channel which
124 * matches the requested capabilities. The result is the channel
125 * number if a match is found, or %-ENODEV if no match is found.
126 *
127 * Note that not all DMA controllers export capabilities, in which
128 * case they can never be allocated using this API, and so
129 * request_dma() must be used specifying the channel number.
130 */
131int request_dma_bycap(const char **dmac, const char **caps, const char *dev_id)
132{
133 unsigned int found = 0;
134 struct dma_info *info;
135 const char **p;
136 int i;
137
138 BUG_ON(!dmac || !caps);
139
140 list_for_each_entry(info, &registered_dmac_list, list)
141 if (strcmp(*dmac, info->name) == 0) {
142 found = 1;
143 break;
144 }
145
146 if (!found)
147 return -ENODEV;
148
149 for (i = 0; i < info->nr_channels; i++) {
150 struct dma_channel *channel = &info->channels[i];
151
152 if (unlikely(!channel->caps))
153 continue;
154
155 for (p = caps; *p; p++) {
156 if (!search_cap(channel->caps, *p))
157 break;
158 if (request_dma(channel->chan, dev_id) == 0)
159 return channel->chan;
160 }
161 }
162
163 return -EINVAL;
164}
165EXPORT_SYMBOL(request_dma_bycap);
166
167int dmac_search_free_channel(const char *dev_id)
168{
169 struct dma_channel *channel = { 0 };
170 struct dma_info *info = get_dma_info(0);
171 int i;
172
173 for (i = 0; i < info->nr_channels; i++) {
174 channel = &info->channels[i];
175 if (unlikely(!channel))
176 return -ENODEV;
177
178 if (atomic_read(&channel->busy) == 0)
179 break;
180 }
181
182 if (info->ops->request) {
183 int result = info->ops->request(channel);
184 if (result)
185 return result;
186
187 atomic_set(&channel->busy, 1);
188 return channel->chan;
189 }
190
191 return -ENOSYS;
192}
193
194int request_dma(unsigned int chan, const char *dev_id)
195{
196 struct dma_channel *channel = { 0 };
197 struct dma_info *info = get_dma_info(chan);
198 int result;
199
200 channel = get_dma_channel(chan);
201 if (atomic_xchg(&channel->busy, 1))
202 return -EBUSY;
203
204 strlcpy(channel->dev_id, dev_id, sizeof(channel->dev_id));
205
206 if (info->ops->request) {
207 result = info->ops->request(channel);
208 if (result)
209 atomic_set(&channel->busy, 0);
210
211 return result;
212 }
213
214 return 0;
215}
216EXPORT_SYMBOL(request_dma);
217
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218void free_dma(unsigned int chan)
219{
220 struct dma_info *info = get_dma_info(chan);
Mark Glaisherdb9b99d2006-11-24 15:13:52 +0900221 struct dma_channel *channel = get_dma_channel(chan);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
223 if (info->ops->free)
224 info->ops->free(channel);
225
226 atomic_set(&channel->busy, 0);
227}
Mark Glaisherdb9b99d2006-11-24 15:13:52 +0900228EXPORT_SYMBOL(free_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
230void dma_wait_for_completion(unsigned int chan)
231{
232 struct dma_info *info = get_dma_info(chan);
Mark Glaisherdb9b99d2006-11-24 15:13:52 +0900233 struct dma_channel *channel = get_dma_channel(chan);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
235 if (channel->flags & DMA_TEI_CAPABLE) {
236 wait_event(channel->wait_queue,
237 (info->ops->get_residue(channel) == 0));
238 return;
239 }
240
241 while (info->ops->get_residue(channel))
242 cpu_relax();
243}
Mark Glaisherdb9b99d2006-11-24 15:13:52 +0900244EXPORT_SYMBOL(dma_wait_for_completion);
245
246int register_chan_caps(const char *dmac, struct dma_chan_caps *caps)
247{
248 struct dma_info *info;
249 unsigned int found = 0;
250 int i;
251
252 list_for_each_entry(info, &registered_dmac_list, list)
253 if (strcmp(dmac, info->name) == 0) {
254 found = 1;
255 break;
256 }
257
258 if (unlikely(!found))
259 return -ENODEV;
260
261 for (i = 0; i < info->nr_channels; i++, caps++) {
262 struct dma_channel *channel;
263
264 if ((info->first_channel_nr + i) != caps->ch_num)
265 return -EINVAL;
266
267 channel = &info->channels[i];
268 channel->caps = caps->caplist;
269 }
270
271 return 0;
272}
273EXPORT_SYMBOL(register_chan_caps);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
275void dma_configure_channel(unsigned int chan, unsigned long flags)
276{
277 struct dma_info *info = get_dma_info(chan);
Mark Glaisherdb9b99d2006-11-24 15:13:52 +0900278 struct dma_channel *channel = get_dma_channel(chan);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
280 if (info->ops->configure)
281 info->ops->configure(channel, flags);
282}
Mark Glaisherdb9b99d2006-11-24 15:13:52 +0900283EXPORT_SYMBOL(dma_configure_channel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
285int dma_xfer(unsigned int chan, unsigned long from,
286 unsigned long to, size_t size, unsigned int mode)
287{
288 struct dma_info *info = get_dma_info(chan);
Mark Glaisherdb9b99d2006-11-24 15:13:52 +0900289 struct dma_channel *channel = get_dma_channel(chan);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
291 channel->sar = from;
292 channel->dar = to;
293 channel->count = size;
294 channel->mode = mode;
295
296 return info->ops->xfer(channel);
297}
Mark Glaisherdb9b99d2006-11-24 15:13:52 +0900298EXPORT_SYMBOL(dma_xfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
Mark Glaisherdb9b99d2006-11-24 15:13:52 +0900300int dma_extend(unsigned int chan, unsigned long op, void *param)
301{
302 struct dma_info *info = get_dma_info(chan);
303 struct dma_channel *channel = get_dma_channel(chan);
304
305 if (info->ops->extend)
306 return info->ops->extend(channel, op, param);
307
308 return -ENOSYS;
309}
310EXPORT_SYMBOL(dma_extend);
311
David Howells64f09622013-04-11 00:01:27 +0100312static int dma_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313{
David Howells64f09622013-04-11 00:01:27 +0100314 struct dma_info *info = v;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
316 if (list_empty(&registered_dmac_list))
317 return 0;
318
319 /*
320 * Iterate over each registered DMAC
321 */
Paul Mundt0d831772006-01-16 22:14:09 -0800322 list_for_each_entry(info, &registered_dmac_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 int i;
324
325 /*
326 * Iterate over each channel
327 */
328 for (i = 0; i < info->nr_channels; i++) {
329 struct dma_channel *channel = info->channels + i;
330
331 if (!(channel->flags & DMA_CONFIGURED))
332 continue;
333
David Howells64f09622013-04-11 00:01:27 +0100334 seq_printf(m, "%2d: %14s %s\n", i,
335 info->name, channel->dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 }
337 }
338
David Howells64f09622013-04-11 00:01:27 +0100339 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
David Howells64f09622013-04-11 00:01:27 +0100342static int dma_proc_open(struct inode *inode, struct file *file)
343{
344 return single_open(file, dma_proc_show, NULL);
345}
346
347static const struct file_operations dma_proc_fops = {
348 .open = dma_proc_open,
349 .read = seq_read,
350 .llseek = seq_lseek,
Al Viroc39b9fd2013-05-05 00:11:01 -0400351 .release = single_release,
David Howells64f09622013-04-11 00:01:27 +0100352};
353
Paul Mundt0d831772006-01-16 22:14:09 -0800354int register_dmac(struct dma_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355{
Paul Mundt0d831772006-01-16 22:14:09 -0800356 unsigned int total_channels, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
358 INIT_LIST_HEAD(&info->list);
359
360 printk(KERN_INFO "DMA: Registering %s handler (%d channel%s).\n",
Mark Glaisherdb9b99d2006-11-24 15:13:52 +0900361 info->name, info->nr_channels, info->nr_channels > 1 ? "s" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
363 BUG_ON((info->flags & DMAC_CHANNELS_CONFIGURED) && !info->channels);
364
Stephen Rothwell222dc792008-02-02 23:03:47 +1100365 info->pdev = platform_device_register_simple(info->name, -1,
Paul Mundt0d831772006-01-16 22:14:09 -0800366 NULL, 0);
367 if (IS_ERR(info->pdev))
368 return PTR_ERR(info->pdev);
369
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 /*
371 * Don't touch pre-configured channels
372 */
373 if (!(info->flags & DMAC_CHANNELS_CONFIGURED)) {
374 unsigned int size;
375
376 size = sizeof(struct dma_channel) * info->nr_channels;
377
Mark Glaisherdb9b99d2006-11-24 15:13:52 +0900378 info->channels = kzalloc(size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 if (!info->channels)
380 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 }
382
Paul Mundt0d831772006-01-16 22:14:09 -0800383 total_channels = get_nr_channels();
Adrian McMenamineb695db2007-07-24 13:30:55 +0900384 info->first_vchannel_nr = total_channels;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 for (i = 0; i < info->nr_channels; i++) {
Mark Glaisherdb9b99d2006-11-24 15:13:52 +0900386 struct dma_channel *chan = &info->channels[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
Mark Glaisherdb9b99d2006-11-24 15:13:52 +0900388 atomic_set(&chan->busy, 0);
389
390 chan->chan = info->first_channel_nr + i;
391 chan->vchan = info->first_channel_nr + i + total_channels;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
393 memcpy(chan->dev_id, "Unused", 7);
394
395 if (info->flags & DMAC_CHANNELS_TEI_CAPABLE)
396 chan->flags |= DMA_TEI_CAPABLE;
397
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 init_waitqueue_head(&chan->wait_queue);
Paul Mundt0d831772006-01-16 22:14:09 -0800399 dma_create_sysfs_files(chan, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 }
401
402 list_add(&info->list, &registered_dmac_list);
403
404 return 0;
405}
Mark Glaisherdb9b99d2006-11-24 15:13:52 +0900406EXPORT_SYMBOL(register_dmac);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
Paul Mundt0d831772006-01-16 22:14:09 -0800408void unregister_dmac(struct dma_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409{
Paul Mundt0d831772006-01-16 22:14:09 -0800410 unsigned int i;
411
412 for (i = 0; i < info->nr_channels; i++)
413 dma_remove_sysfs_files(info->channels + i, info);
414
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 if (!(info->flags & DMAC_CHANNELS_CONFIGURED))
416 kfree(info->channels);
417
418 list_del(&info->list);
Paul Mundt0d831772006-01-16 22:14:09 -0800419 platform_device_unregister(info->pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420}
Mark Glaisherdb9b99d2006-11-24 15:13:52 +0900421EXPORT_SYMBOL(unregister_dmac);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
423static int __init dma_api_init(void)
424{
Mark Glaisherdb9b99d2006-11-24 15:13:52 +0900425 printk(KERN_NOTICE "DMA: Registering DMA API.\n");
David Howells64f09622013-04-11 00:01:27 +0100426 return proc_create("dma", 0, NULL, &dma_proc_fops) ? 0 : -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428subsys_initcall(dma_api_init);
429
430MODULE_AUTHOR("Paul Mundt <lethal@linux-sh.org>");
431MODULE_DESCRIPTION("DMA API for SuperH");
432MODULE_LICENSE("GPL");