blob: a63badcd2d6ec92ea330331b7869ee936d2fcdce [file] [log] [blame]
Linus Walleij61f135b2009-11-19 19:49:17 +01001/*
2 * driver/dma/coh901318.c
3 *
4 * Copyright (C) 2007-2009 ST-Ericsson
5 * License terms: GNU General Public License (GPL) version 2
6 * DMA driver for COH 901 318
7 * Author: Per Friden <per.friden@stericsson.com>
8 */
9
10#include <linux/init.h>
11#include <linux/module.h>
12#include <linux/kernel.h> /* printk() */
13#include <linux/fs.h> /* everything... */
Alexey Dobriyanb7f080c2011-06-16 11:01:34 +000014#include <linux/scatterlist.h>
Linus Walleij61f135b2009-11-19 19:49:17 +010015#include <linux/slab.h> /* kmalloc() */
16#include <linux/dmaengine.h>
17#include <linux/platform_device.h>
18#include <linux/device.h>
19#include <linux/irqreturn.h>
20#include <linux/interrupt.h>
21#include <linux/io.h>
22#include <linux/uaccess.h>
23#include <linux/debugfs.h>
24#include <mach/coh901318.h>
25
26#include "coh901318_lli.h"
27
28#define COHC_2_DEV(cohc) (&cohc->chan.dev->device)
29
30#ifdef VERBOSE_DEBUG
31#define COH_DBG(x) ({ if (1) x; 0; })
32#else
33#define COH_DBG(x) ({ if (0) x; 0; })
34#endif
35
36struct coh901318_desc {
37 struct dma_async_tx_descriptor desc;
38 struct list_head node;
39 struct scatterlist *sg;
40 unsigned int sg_len;
Linus Walleijcecd87d2010-03-04 14:31:47 +010041 struct coh901318_lli *lli;
Vinod Kouldb8196d2011-10-13 22:34:23 +053042 enum dma_transfer_direction dir;
Linus Walleij61f135b2009-11-19 19:49:17 +010043 unsigned long flags;
Linus Walleijb89243d2011-07-01 16:47:28 +020044 u32 head_config;
45 u32 head_ctrl;
Linus Walleij61f135b2009-11-19 19:49:17 +010046};
47
48struct coh901318_base {
49 struct device *dev;
50 void __iomem *virtbase;
51 struct coh901318_pool pool;
52 struct powersave pm;
53 struct dma_device dma_slave;
54 struct dma_device dma_memcpy;
55 struct coh901318_chan *chans;
56 struct coh901318_platform *platform;
57};
58
59struct coh901318_chan {
60 spinlock_t lock;
61 int allocated;
62 int completed;
63 int id;
64 int stopped;
65
66 struct work_struct free_work;
67 struct dma_chan chan;
68
69 struct tasklet_struct tasklet;
70
71 struct list_head active;
72 struct list_head queue;
73 struct list_head free;
74
75 unsigned long nbr_active_done;
76 unsigned long busy;
Linus Walleij61f135b2009-11-19 19:49:17 +010077
Linus Walleij128f9042010-08-04 13:37:53 +020078 u32 runtime_addr;
79 u32 runtime_ctrl;
80
Linus Walleij61f135b2009-11-19 19:49:17 +010081 struct coh901318_base *base;
82};
83
84static void coh901318_list_print(struct coh901318_chan *cohc,
85 struct coh901318_lli *lli)
86{
Linus Walleij848ad122010-03-02 14:17:15 -070087 struct coh901318_lli *l = lli;
Linus Walleij61f135b2009-11-19 19:49:17 +010088 int i = 0;
89
Linus Walleij848ad122010-03-02 14:17:15 -070090 while (l) {
Linus Walleij61f135b2009-11-19 19:49:17 +010091 dev_vdbg(COHC_2_DEV(cohc), "i %d, lli %p, ctrl 0x%x, src 0x%x"
Linus Walleij848ad122010-03-02 14:17:15 -070092 ", dst 0x%x, link 0x%x virt_link_addr 0x%p\n",
Linus Walleij61f135b2009-11-19 19:49:17 +010093 i, l, l->control, l->src_addr, l->dst_addr,
Linus Walleij848ad122010-03-02 14:17:15 -070094 l->link_addr, l->virt_link_addr);
Linus Walleij61f135b2009-11-19 19:49:17 +010095 i++;
Linus Walleij848ad122010-03-02 14:17:15 -070096 l = l->virt_link_addr;
Linus Walleij61f135b2009-11-19 19:49:17 +010097 }
98}
99
100#ifdef CONFIG_DEBUG_FS
101
102#define COH901318_DEBUGFS_ASSIGN(x, y) (x = y)
103
104static struct coh901318_base *debugfs_dma_base;
105static struct dentry *dma_dentry;
106
Linus Walleij61f135b2009-11-19 19:49:17 +0100107static int coh901318_debugfs_read(struct file *file, char __user *buf,
108 size_t count, loff_t *f_pos)
109{
110 u64 started_channels = debugfs_dma_base->pm.started_channels;
111 int pool_count = debugfs_dma_base->pool.debugfs_pool_counter;
112 int i;
113 int ret = 0;
114 char *dev_buf;
115 char *tmp;
116 int dev_size;
117
118 dev_buf = kmalloc(4*1024, GFP_KERNEL);
119 if (dev_buf == NULL)
120 goto err_kmalloc;
121 tmp = dev_buf;
122
Linus Walleij848ad122010-03-02 14:17:15 -0700123 tmp += sprintf(tmp, "DMA -- enabled dma channels\n");
Linus Walleij61f135b2009-11-19 19:49:17 +0100124
125 for (i = 0; i < debugfs_dma_base->platform->max_channels; i++)
126 if (started_channels & (1 << i))
127 tmp += sprintf(tmp, "channel %d\n", i);
128
129 tmp += sprintf(tmp, "Pool alloc nbr %d\n", pool_count);
130 dev_size = tmp - dev_buf;
131
132 /* No more to read if offset != 0 */
133 if (*f_pos > dev_size)
134 goto out;
135
136 if (count > dev_size - *f_pos)
137 count = dev_size - *f_pos;
138
139 if (copy_to_user(buf, dev_buf + *f_pos, count))
140 ret = -EINVAL;
141 ret = count;
142 *f_pos += count;
143
144 out:
145 kfree(dev_buf);
146 return ret;
147
148 err_kmalloc:
149 return 0;
150}
151
152static const struct file_operations coh901318_debugfs_status_operations = {
153 .owner = THIS_MODULE,
Stephen Boyd234e3402012-04-05 14:25:11 -0700154 .open = simple_open,
Linus Walleij61f135b2009-11-19 19:49:17 +0100155 .read = coh901318_debugfs_read,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200156 .llseek = default_llseek,
Linus Walleij61f135b2009-11-19 19:49:17 +0100157};
158
159
160static int __init init_coh901318_debugfs(void)
161{
162
163 dma_dentry = debugfs_create_dir("dma", NULL);
164
165 (void) debugfs_create_file("status",
166 S_IFREG | S_IRUGO,
167 dma_dentry, NULL,
168 &coh901318_debugfs_status_operations);
169 return 0;
170}
171
172static void __exit exit_coh901318_debugfs(void)
173{
174 debugfs_remove_recursive(dma_dentry);
175}
176
177module_init(init_coh901318_debugfs);
178module_exit(exit_coh901318_debugfs);
179#else
180
181#define COH901318_DEBUGFS_ASSIGN(x, y)
182
183#endif /* CONFIG_DEBUG_FS */
184
185static inline struct coh901318_chan *to_coh901318_chan(struct dma_chan *chan)
186{
187 return container_of(chan, struct coh901318_chan, chan);
188}
189
190static inline dma_addr_t
191cohc_dev_addr(struct coh901318_chan *cohc)
192{
Linus Walleij128f9042010-08-04 13:37:53 +0200193 /* Runtime supplied address will take precedence */
194 if (cohc->runtime_addr)
195 return cohc->runtime_addr;
Linus Walleij61f135b2009-11-19 19:49:17 +0100196 return cohc->base->platform->chan_conf[cohc->id].dev_addr;
197}
198
199static inline const struct coh901318_params *
200cohc_chan_param(struct coh901318_chan *cohc)
201{
202 return &cohc->base->platform->chan_conf[cohc->id].param;
203}
204
205static inline const struct coh_dma_channel *
206cohc_chan_conf(struct coh901318_chan *cohc)
207{
208 return &cohc->base->platform->chan_conf[cohc->id];
209}
210
211static void enable_powersave(struct coh901318_chan *cohc)
212{
213 unsigned long flags;
214 struct powersave *pm = &cohc->base->pm;
215
216 spin_lock_irqsave(&pm->lock, flags);
217
218 pm->started_channels &= ~(1ULL << cohc->id);
219
220 if (!pm->started_channels) {
221 /* DMA no longer intends to access memory */
222 cohc->base->platform->access_memory_state(cohc->base->dev,
223 false);
224 }
225
226 spin_unlock_irqrestore(&pm->lock, flags);
227}
228static void disable_powersave(struct coh901318_chan *cohc)
229{
230 unsigned long flags;
231 struct powersave *pm = &cohc->base->pm;
232
233 spin_lock_irqsave(&pm->lock, flags);
234
235 if (!pm->started_channels) {
236 /* DMA intends to access memory */
237 cohc->base->platform->access_memory_state(cohc->base->dev,
238 true);
239 }
240
241 pm->started_channels |= (1ULL << cohc->id);
242
243 spin_unlock_irqrestore(&pm->lock, flags);
244}
245
246static inline int coh901318_set_ctrl(struct coh901318_chan *cohc, u32 control)
247{
248 int channel = cohc->id;
249 void __iomem *virtbase = cohc->base->virtbase;
250
251 writel(control,
252 virtbase + COH901318_CX_CTRL +
253 COH901318_CX_CTRL_SPACING * channel);
254 return 0;
255}
256
257static inline int coh901318_set_conf(struct coh901318_chan *cohc, u32 conf)
258{
259 int channel = cohc->id;
260 void __iomem *virtbase = cohc->base->virtbase;
261
262 writel(conf,
263 virtbase + COH901318_CX_CFG +
264 COH901318_CX_CFG_SPACING*channel);
265 return 0;
266}
267
268
269static int coh901318_start(struct coh901318_chan *cohc)
270{
271 u32 val;
272 int channel = cohc->id;
273 void __iomem *virtbase = cohc->base->virtbase;
274
275 disable_powersave(cohc);
276
277 val = readl(virtbase + COH901318_CX_CFG +
278 COH901318_CX_CFG_SPACING * channel);
279
280 /* Enable channel */
281 val |= COH901318_CX_CFG_CH_ENABLE;
282 writel(val, virtbase + COH901318_CX_CFG +
283 COH901318_CX_CFG_SPACING * channel);
284
285 return 0;
286}
287
288static int coh901318_prep_linked_list(struct coh901318_chan *cohc,
Linus Walleijcecd87d2010-03-04 14:31:47 +0100289 struct coh901318_lli *lli)
Linus Walleij61f135b2009-11-19 19:49:17 +0100290{
291 int channel = cohc->id;
292 void __iomem *virtbase = cohc->base->virtbase;
293
294 BUG_ON(readl(virtbase + COH901318_CX_STAT +
295 COH901318_CX_STAT_SPACING*channel) &
296 COH901318_CX_STAT_ACTIVE);
297
Linus Walleijcecd87d2010-03-04 14:31:47 +0100298 writel(lli->src_addr,
Linus Walleij61f135b2009-11-19 19:49:17 +0100299 virtbase + COH901318_CX_SRC_ADDR +
300 COH901318_CX_SRC_ADDR_SPACING * channel);
301
Linus Walleijcecd87d2010-03-04 14:31:47 +0100302 writel(lli->dst_addr, virtbase +
Linus Walleij61f135b2009-11-19 19:49:17 +0100303 COH901318_CX_DST_ADDR +
304 COH901318_CX_DST_ADDR_SPACING * channel);
305
Linus Walleijcecd87d2010-03-04 14:31:47 +0100306 writel(lli->link_addr, virtbase + COH901318_CX_LNK_ADDR +
Linus Walleij61f135b2009-11-19 19:49:17 +0100307 COH901318_CX_LNK_ADDR_SPACING * channel);
308
Linus Walleijcecd87d2010-03-04 14:31:47 +0100309 writel(lli->control, virtbase + COH901318_CX_CTRL +
Linus Walleij61f135b2009-11-19 19:49:17 +0100310 COH901318_CX_CTRL_SPACING * channel);
311
312 return 0;
313}
314static dma_cookie_t
315coh901318_assign_cookie(struct coh901318_chan *cohc,
316 struct coh901318_desc *cohd)
317{
318 dma_cookie_t cookie = cohc->chan.cookie;
319
320 if (++cookie < 0)
321 cookie = 1;
322
323 cohc->chan.cookie = cookie;
324 cohd->desc.cookie = cookie;
325
326 return cookie;
327}
328
329static struct coh901318_desc *
330coh901318_desc_get(struct coh901318_chan *cohc)
331{
332 struct coh901318_desc *desc;
333
334 if (list_empty(&cohc->free)) {
335 /* alloc new desc because we're out of used ones
336 * TODO: alloc a pile of descs instead of just one,
337 * avoid many small allocations.
338 */
Linus Walleijb87108a2010-03-02 14:17:20 -0700339 desc = kzalloc(sizeof(struct coh901318_desc), GFP_NOWAIT);
Linus Walleij61f135b2009-11-19 19:49:17 +0100340 if (desc == NULL)
341 goto out;
342 INIT_LIST_HEAD(&desc->node);
Linus Walleijb87108a2010-03-02 14:17:20 -0700343 dma_async_tx_descriptor_init(&desc->desc, &cohc->chan);
Linus Walleij61f135b2009-11-19 19:49:17 +0100344 } else {
345 /* Reuse an old desc. */
346 desc = list_first_entry(&cohc->free,
347 struct coh901318_desc,
348 node);
349 list_del(&desc->node);
Linus Walleijb87108a2010-03-02 14:17:20 -0700350 /* Initialize it a bit so it's not insane */
351 desc->sg = NULL;
352 desc->sg_len = 0;
353 desc->desc.callback = NULL;
354 desc->desc.callback_param = NULL;
Linus Walleij61f135b2009-11-19 19:49:17 +0100355 }
356
357 out:
358 return desc;
359}
360
361static void
362coh901318_desc_free(struct coh901318_chan *cohc, struct coh901318_desc *cohd)
363{
364 list_add_tail(&cohd->node, &cohc->free);
365}
366
367/* call with irq lock held */
368static void
369coh901318_desc_submit(struct coh901318_chan *cohc, struct coh901318_desc *desc)
370{
371 list_add_tail(&desc->node, &cohc->active);
Linus Walleij61f135b2009-11-19 19:49:17 +0100372}
373
374static struct coh901318_desc *
375coh901318_first_active_get(struct coh901318_chan *cohc)
376{
377 struct coh901318_desc *d;
378
379 if (list_empty(&cohc->active))
380 return NULL;
381
382 d = list_first_entry(&cohc->active,
383 struct coh901318_desc,
384 node);
385 return d;
386}
387
388static void
389coh901318_desc_remove(struct coh901318_desc *cohd)
390{
391 list_del(&cohd->node);
392}
393
394static void
395coh901318_desc_queue(struct coh901318_chan *cohc, struct coh901318_desc *desc)
396{
397 list_add_tail(&desc->node, &cohc->queue);
398}
399
400static struct coh901318_desc *
401coh901318_first_queued(struct coh901318_chan *cohc)
402{
403 struct coh901318_desc *d;
404
405 if (list_empty(&cohc->queue))
406 return NULL;
407
408 d = list_first_entry(&cohc->queue,
409 struct coh901318_desc,
410 node);
411 return d;
412}
413
Linus Walleij84c84472010-03-04 14:40:30 +0100414static inline u32 coh901318_get_bytes_in_lli(struct coh901318_lli *in_lli)
415{
416 struct coh901318_lli *lli = in_lli;
417 u32 bytes = 0;
418
419 while (lli) {
420 bytes += lli->control & COH901318_CX_CTRL_TC_VALUE_MASK;
421 lli = lli->virt_link_addr;
422 }
423 return bytes;
424}
425
Linus Walleij61f135b2009-11-19 19:49:17 +0100426/*
Linus Walleij84c84472010-03-04 14:40:30 +0100427 * Get the number of bytes left to transfer on this channel,
428 * it is unwise to call this before stopping the channel for
429 * absolute measures, but for a rough guess you can still call
430 * it.
Linus Walleij61f135b2009-11-19 19:49:17 +0100431 */
Linus Walleij07934482010-03-26 16:50:49 -0700432static u32 coh901318_get_bytes_left(struct dma_chan *chan)
Linus Walleij61f135b2009-11-19 19:49:17 +0100433{
Linus Walleij61f135b2009-11-19 19:49:17 +0100434 struct coh901318_chan *cohc = to_coh901318_chan(chan);
Linus Walleij84c84472010-03-04 14:40:30 +0100435 struct coh901318_desc *cohd;
436 struct list_head *pos;
437 unsigned long flags;
438 u32 left = 0;
439 int i = 0;
Linus Walleij61f135b2009-11-19 19:49:17 +0100440
441 spin_lock_irqsave(&cohc->lock, flags);
442
Linus Walleij84c84472010-03-04 14:40:30 +0100443 /*
444 * If there are many queued jobs, we iterate and add the
445 * size of them all. We take a special look on the first
446 * job though, since it is probably active.
447 */
448 list_for_each(pos, &cohc->active) {
449 /*
450 * The first job in the list will be working on the
451 * hardware. The job can be stopped but still active,
452 * so that the transfer counter is somewhere inside
453 * the buffer.
454 */
455 cohd = list_entry(pos, struct coh901318_desc, node);
456
457 if (i == 0) {
458 struct coh901318_lli *lli;
459 dma_addr_t ladd;
460
461 /* Read current transfer count value */
462 left = readl(cohc->base->virtbase +
463 COH901318_CX_CTRL +
464 COH901318_CX_CTRL_SPACING * cohc->id) &
465 COH901318_CX_CTRL_TC_VALUE_MASK;
466
467 /* See if the transfer is linked... */
468 ladd = readl(cohc->base->virtbase +
469 COH901318_CX_LNK_ADDR +
470 COH901318_CX_LNK_ADDR_SPACING *
471 cohc->id) &
472 ~COH901318_CX_LNK_LINK_IMMEDIATE;
473 /* Single transaction */
474 if (!ladd)
475 continue;
476
477 /*
478 * Linked transaction, follow the lli, find the
479 * currently processing lli, and proceed to the next
480 */
481 lli = cohd->lli;
482 while (lli && lli->link_addr != ladd)
483 lli = lli->virt_link_addr;
484
485 if (lli)
486 lli = lli->virt_link_addr;
487
488 /*
489 * Follow remaining lli links around to count the total
490 * number of bytes left
491 */
492 left += coh901318_get_bytes_in_lli(lli);
493 } else {
494 left += coh901318_get_bytes_in_lli(cohd->lli);
495 }
496 i++;
497 }
498
499 /* Also count bytes in the queued jobs */
500 list_for_each(pos, &cohc->queue) {
501 cohd = list_entry(pos, struct coh901318_desc, node);
502 left += coh901318_get_bytes_in_lli(cohd->lli);
503 }
Linus Walleij61f135b2009-11-19 19:49:17 +0100504
505 spin_unlock_irqrestore(&cohc->lock, flags);
506
Linus Walleij84c84472010-03-04 14:40:30 +0100507 return left;
Linus Walleij61f135b2009-11-19 19:49:17 +0100508}
Linus Walleij61f135b2009-11-19 19:49:17 +0100509
Linus Walleijc3635c72010-03-26 16:44:01 -0700510/*
511 * Pauses a transfer without losing data. Enables power save.
512 * Use this function in conjunction with coh901318_resume.
513 */
514static void coh901318_pause(struct dma_chan *chan)
Linus Walleij61f135b2009-11-19 19:49:17 +0100515{
516 u32 val;
517 unsigned long flags;
518 struct coh901318_chan *cohc = to_coh901318_chan(chan);
519 int channel = cohc->id;
520 void __iomem *virtbase = cohc->base->virtbase;
521
522 spin_lock_irqsave(&cohc->lock, flags);
523
524 /* Disable channel in HW */
525 val = readl(virtbase + COH901318_CX_CFG +
526 COH901318_CX_CFG_SPACING * channel);
527
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300528 /* Stopping infinite transfer */
Linus Walleij61f135b2009-11-19 19:49:17 +0100529 if ((val & COH901318_CX_CTRL_TC_ENABLE) == 0 &&
530 (val & COH901318_CX_CFG_CH_ENABLE))
531 cohc->stopped = 1;
532
533
534 val &= ~COH901318_CX_CFG_CH_ENABLE;
535 /* Enable twice, HW bug work around */
536 writel(val, virtbase + COH901318_CX_CFG +
537 COH901318_CX_CFG_SPACING * channel);
538 writel(val, virtbase + COH901318_CX_CFG +
539 COH901318_CX_CFG_SPACING * channel);
540
541 /* Spin-wait for it to actually go inactive */
542 while (readl(virtbase + COH901318_CX_STAT+COH901318_CX_STAT_SPACING *
543 channel) & COH901318_CX_STAT_ACTIVE)
544 cpu_relax();
545
546 /* Check if we stopped an active job */
547 if ((readl(virtbase + COH901318_CX_CTRL+COH901318_CX_CTRL_SPACING *
548 channel) & COH901318_CX_CTRL_TC_VALUE_MASK) > 0)
549 cohc->stopped = 1;
550
551 enable_powersave(cohc);
552
553 spin_unlock_irqrestore(&cohc->lock, flags);
554}
Linus Walleij61f135b2009-11-19 19:49:17 +0100555
Linus Walleijc3635c72010-03-26 16:44:01 -0700556/* Resumes a transfer that has been stopped via 300_dma_stop(..).
Linus Walleij61f135b2009-11-19 19:49:17 +0100557 Power save is handled.
558*/
Linus Walleijc3635c72010-03-26 16:44:01 -0700559static void coh901318_resume(struct dma_chan *chan)
Linus Walleij61f135b2009-11-19 19:49:17 +0100560{
561 u32 val;
562 unsigned long flags;
563 struct coh901318_chan *cohc = to_coh901318_chan(chan);
564 int channel = cohc->id;
565
566 spin_lock_irqsave(&cohc->lock, flags);
567
568 disable_powersave(cohc);
569
570 if (cohc->stopped) {
571 /* Enable channel in HW */
572 val = readl(cohc->base->virtbase + COH901318_CX_CFG +
573 COH901318_CX_CFG_SPACING * channel);
574
575 val |= COH901318_CX_CFG_CH_ENABLE;
576
577 writel(val, cohc->base->virtbase + COH901318_CX_CFG +
578 COH901318_CX_CFG_SPACING*channel);
579
580 cohc->stopped = 0;
581 }
582
583 spin_unlock_irqrestore(&cohc->lock, flags);
584}
Linus Walleij61f135b2009-11-19 19:49:17 +0100585
586bool coh901318_filter_id(struct dma_chan *chan, void *chan_id)
587{
588 unsigned int ch_nr = (unsigned int) chan_id;
589
590 if (ch_nr == to_coh901318_chan(chan)->id)
591 return true;
592
593 return false;
594}
595EXPORT_SYMBOL(coh901318_filter_id);
596
597/*
598 * DMA channel allocation
599 */
600static int coh901318_config(struct coh901318_chan *cohc,
601 struct coh901318_params *param)
602{
603 unsigned long flags;
604 const struct coh901318_params *p;
605 int channel = cohc->id;
606 void __iomem *virtbase = cohc->base->virtbase;
607
608 spin_lock_irqsave(&cohc->lock, flags);
609
610 if (param)
611 p = param;
612 else
613 p = &cohc->base->platform->chan_conf[channel].param;
614
615 /* Clear any pending BE or TC interrupt */
616 if (channel < 32) {
617 writel(1 << channel, virtbase + COH901318_BE_INT_CLEAR1);
618 writel(1 << channel, virtbase + COH901318_TC_INT_CLEAR1);
619 } else {
620 writel(1 << (channel - 32), virtbase +
621 COH901318_BE_INT_CLEAR2);
622 writel(1 << (channel - 32), virtbase +
623 COH901318_TC_INT_CLEAR2);
624 }
625
626 coh901318_set_conf(cohc, p->config);
627 coh901318_set_ctrl(cohc, p->ctrl_lli_last);
628
629 spin_unlock_irqrestore(&cohc->lock, flags);
630
631 return 0;
632}
633
634/* must lock when calling this function
635 * start queued jobs, if any
636 * TODO: start all queued jobs in one go
637 *
638 * Returns descriptor if queued job is started otherwise NULL.
639 * If the queue is empty NULL is returned.
640 */
641static struct coh901318_desc *coh901318_queue_start(struct coh901318_chan *cohc)
642{
Linus Walleijcecd87d2010-03-04 14:31:47 +0100643 struct coh901318_desc *cohd;
Linus Walleij61f135b2009-11-19 19:49:17 +0100644
Linus Walleijcecd87d2010-03-04 14:31:47 +0100645 /*
646 * start queued jobs, if any
Linus Walleij61f135b2009-11-19 19:49:17 +0100647 * TODO: transmit all queued jobs in one go
648 */
Linus Walleijcecd87d2010-03-04 14:31:47 +0100649 cohd = coh901318_first_queued(cohc);
Linus Walleij61f135b2009-11-19 19:49:17 +0100650
Linus Walleijcecd87d2010-03-04 14:31:47 +0100651 if (cohd != NULL) {
Linus Walleij61f135b2009-11-19 19:49:17 +0100652 /* Remove from queue */
Linus Walleijcecd87d2010-03-04 14:31:47 +0100653 coh901318_desc_remove(cohd);
Linus Walleij61f135b2009-11-19 19:49:17 +0100654 /* initiate DMA job */
655 cohc->busy = 1;
656
Linus Walleijcecd87d2010-03-04 14:31:47 +0100657 coh901318_desc_submit(cohc, cohd);
Linus Walleij61f135b2009-11-19 19:49:17 +0100658
Linus Walleijb89243d2011-07-01 16:47:28 +0200659 /* Program the transaction head */
660 coh901318_set_conf(cohc, cohd->head_config);
661 coh901318_set_ctrl(cohc, cohd->head_ctrl);
Linus Walleijcecd87d2010-03-04 14:31:47 +0100662 coh901318_prep_linked_list(cohc, cohd->lli);
Linus Walleij61f135b2009-11-19 19:49:17 +0100663
Linus Walleijcecd87d2010-03-04 14:31:47 +0100664 /* start dma job on this channel */
Linus Walleij61f135b2009-11-19 19:49:17 +0100665 coh901318_start(cohc);
666
667 }
668
Linus Walleijcecd87d2010-03-04 14:31:47 +0100669 return cohd;
Linus Walleij61f135b2009-11-19 19:49:17 +0100670}
671
Linus Walleij848ad122010-03-02 14:17:15 -0700672/*
673 * This tasklet is called from the interrupt handler to
674 * handle each descriptor (DMA job) that is sent to a channel.
675 */
Linus Walleij61f135b2009-11-19 19:49:17 +0100676static void dma_tasklet(unsigned long data)
677{
678 struct coh901318_chan *cohc = (struct coh901318_chan *) data;
679 struct coh901318_desc *cohd_fin;
680 unsigned long flags;
681 dma_async_tx_callback callback;
682 void *callback_param;
683
Linus Walleij848ad122010-03-02 14:17:15 -0700684 dev_vdbg(COHC_2_DEV(cohc), "[%s] chan_id %d"
685 " nbr_active_done %ld\n", __func__,
686 cohc->id, cohc->nbr_active_done);
687
Linus Walleij61f135b2009-11-19 19:49:17 +0100688 spin_lock_irqsave(&cohc->lock, flags);
689
Linus Walleij848ad122010-03-02 14:17:15 -0700690 /* get first active descriptor entry from list */
Linus Walleij61f135b2009-11-19 19:49:17 +0100691 cohd_fin = coh901318_first_active_get(cohc);
692
Linus Walleij61f135b2009-11-19 19:49:17 +0100693 if (cohd_fin == NULL)
694 goto err;
695
Linus Walleij0b588282010-03-02 14:17:44 -0700696 /* locate callback to client */
Linus Walleij61f135b2009-11-19 19:49:17 +0100697 callback = cohd_fin->desc.callback;
698 callback_param = cohd_fin->desc.callback_param;
699
Linus Walleij0b588282010-03-02 14:17:44 -0700700 /* sign this job as completed on the channel */
701 cohc->completed = cohd_fin->desc.cookie;
Linus Walleij61f135b2009-11-19 19:49:17 +0100702
Linus Walleij0b588282010-03-02 14:17:44 -0700703 /* release the lli allocation and remove the descriptor */
Linus Walleijcecd87d2010-03-04 14:31:47 +0100704 coh901318_lli_free(&cohc->base->pool, &cohd_fin->lli);
Linus Walleij0b588282010-03-02 14:17:44 -0700705
706 /* return desc to free-list */
707 coh901318_desc_remove(cohd_fin);
708 coh901318_desc_free(cohc, cohd_fin);
709
710 spin_unlock_irqrestore(&cohc->lock, flags);
711
712 /* Call the callback when we're done */
713 if (callback)
714 callback(callback_param);
715
716 spin_lock_irqsave(&cohc->lock, flags);
Linus Walleij61f135b2009-11-19 19:49:17 +0100717
Linus Walleij848ad122010-03-02 14:17:15 -0700718 /*
719 * If another interrupt fired while the tasklet was scheduling,
720 * we don't get called twice, so we have this number of active
721 * counter that keep track of the number of IRQs expected to
722 * be handled for this channel. If there happen to be more than
723 * one IRQ to be ack:ed, we simply schedule this tasklet again.
724 */
Linus Walleij0b588282010-03-02 14:17:44 -0700725 cohc->nbr_active_done--;
Linus Walleij61f135b2009-11-19 19:49:17 +0100726 if (cohc->nbr_active_done) {
Linus Walleij848ad122010-03-02 14:17:15 -0700727 dev_dbg(COHC_2_DEV(cohc), "scheduling tasklet again, new IRQs "
728 "came in while we were scheduling this tasklet\n");
Linus Walleij61f135b2009-11-19 19:49:17 +0100729 if (cohc_chan_conf(cohc)->priority_high)
730 tasklet_hi_schedule(&cohc->tasklet);
731 else
732 tasklet_schedule(&cohc->tasklet);
733 }
Linus Walleij61f135b2009-11-19 19:49:17 +0100734
Linus Walleij0b588282010-03-02 14:17:44 -0700735 spin_unlock_irqrestore(&cohc->lock, flags);
Linus Walleij61f135b2009-11-19 19:49:17 +0100736
737 return;
738
739 err:
740 spin_unlock_irqrestore(&cohc->lock, flags);
741 dev_err(COHC_2_DEV(cohc), "[%s] No active dma desc\n", __func__);
742}
743
744
745/* called from interrupt context */
746static void dma_tc_handle(struct coh901318_chan *cohc)
747{
Linus Walleijcecd87d2010-03-04 14:31:47 +0100748 /*
749 * If the channel is not allocated, then we shouldn't have
750 * any TC interrupts on it.
751 */
752 if (!cohc->allocated) {
753 dev_err(COHC_2_DEV(cohc), "spurious interrupt from "
754 "unallocated channel\n");
Linus Walleij61f135b2009-11-19 19:49:17 +0100755 return;
Linus Walleijcecd87d2010-03-04 14:31:47 +0100756 }
Linus Walleij61f135b2009-11-19 19:49:17 +0100757
Linus Walleij0b588282010-03-02 14:17:44 -0700758 spin_lock(&cohc->lock);
Linus Walleij61f135b2009-11-19 19:49:17 +0100759
Linus Walleijcecd87d2010-03-04 14:31:47 +0100760 /*
761 * When we reach this point, at least one queue item
762 * should have been moved over from cohc->queue to
763 * cohc->active and run to completion, that is why we're
764 * getting a terminal count interrupt is it not?
765 * If you get this BUG() the most probable cause is that
766 * the individual nodes in the lli chain have IRQ enabled,
767 * so check your platform config for lli chain ctrl.
768 */
769 BUG_ON(list_empty(&cohc->active));
770
Linus Walleij61f135b2009-11-19 19:49:17 +0100771 cohc->nbr_active_done++;
772
Linus Walleijcecd87d2010-03-04 14:31:47 +0100773 /*
774 * This attempt to take a job from cohc->queue, put it
775 * into cohc->active and start it.
776 */
Linus Walleij0b588282010-03-02 14:17:44 -0700777 if (coh901318_queue_start(cohc) == NULL)
Linus Walleij61f135b2009-11-19 19:49:17 +0100778 cohc->busy = 0;
779
Linus Walleij0b588282010-03-02 14:17:44 -0700780 spin_unlock(&cohc->lock);
781
Linus Walleijcecd87d2010-03-04 14:31:47 +0100782 /*
783 * This tasklet will remove items from cohc->active
784 * and thus terminates them.
785 */
Linus Walleij61f135b2009-11-19 19:49:17 +0100786 if (cohc_chan_conf(cohc)->priority_high)
787 tasklet_hi_schedule(&cohc->tasklet);
788 else
789 tasklet_schedule(&cohc->tasklet);
790}
791
792
793static irqreturn_t dma_irq_handler(int irq, void *dev_id)
794{
795 u32 status1;
796 u32 status2;
797 int i;
798 int ch;
799 struct coh901318_base *base = dev_id;
800 struct coh901318_chan *cohc;
801 void __iomem *virtbase = base->virtbase;
802
803 status1 = readl(virtbase + COH901318_INT_STATUS1);
804 status2 = readl(virtbase + COH901318_INT_STATUS2);
805
806 if (unlikely(status1 == 0 && status2 == 0)) {
807 dev_warn(base->dev, "spurious DMA IRQ from no channel!\n");
808 return IRQ_HANDLED;
809 }
810
811 /* TODO: consider handle IRQ in tasklet here to
812 * minimize interrupt latency */
813
814 /* Check the first 32 DMA channels for IRQ */
815 while (status1) {
816 /* Find first bit set, return as a number. */
817 i = ffs(status1) - 1;
818 ch = i;
819
820 cohc = &base->chans[ch];
821 spin_lock(&cohc->lock);
822
823 /* Mask off this bit */
824 status1 &= ~(1 << i);
825 /* Check the individual channel bits */
826 if (test_bit(i, virtbase + COH901318_BE_INT_STATUS1)) {
827 dev_crit(COHC_2_DEV(cohc),
828 "DMA bus error on channel %d!\n", ch);
829 BUG_ON(1);
830 /* Clear BE interrupt */
831 __set_bit(i, virtbase + COH901318_BE_INT_CLEAR1);
832 } else {
833 /* Caused by TC, really? */
834 if (unlikely(!test_bit(i, virtbase +
835 COH901318_TC_INT_STATUS1))) {
836 dev_warn(COHC_2_DEV(cohc),
837 "ignoring interrupt not caused by terminal count on channel %d\n", ch);
838 /* Clear TC interrupt */
839 BUG_ON(1);
840 __set_bit(i, virtbase + COH901318_TC_INT_CLEAR1);
841 } else {
842 /* Enable powersave if transfer has finished */
843 if (!(readl(virtbase + COH901318_CX_STAT +
844 COH901318_CX_STAT_SPACING*ch) &
845 COH901318_CX_STAT_ENABLED)) {
846 enable_powersave(cohc);
847 }
848
849 /* Must clear TC interrupt before calling
850 * dma_tc_handle
Justin P. Mattockbc0b44c2011-01-28 11:48:18 -0800851 * in case tc_handle initiate a new dma job
Linus Walleij61f135b2009-11-19 19:49:17 +0100852 */
853 __set_bit(i, virtbase + COH901318_TC_INT_CLEAR1);
854
855 dma_tc_handle(cohc);
856 }
857 }
858 spin_unlock(&cohc->lock);
859 }
860
861 /* Check the remaining 32 DMA channels for IRQ */
862 while (status2) {
863 /* Find first bit set, return as a number. */
864 i = ffs(status2) - 1;
865 ch = i + 32;
866 cohc = &base->chans[ch];
867 spin_lock(&cohc->lock);
868
869 /* Mask off this bit */
870 status2 &= ~(1 << i);
871 /* Check the individual channel bits */
872 if (test_bit(i, virtbase + COH901318_BE_INT_STATUS2)) {
873 dev_crit(COHC_2_DEV(cohc),
874 "DMA bus error on channel %d!\n", ch);
875 /* Clear BE interrupt */
876 BUG_ON(1);
877 __set_bit(i, virtbase + COH901318_BE_INT_CLEAR2);
878 } else {
879 /* Caused by TC, really? */
880 if (unlikely(!test_bit(i, virtbase +
881 COH901318_TC_INT_STATUS2))) {
882 dev_warn(COHC_2_DEV(cohc),
883 "ignoring interrupt not caused by terminal count on channel %d\n", ch);
884 /* Clear TC interrupt */
885 __set_bit(i, virtbase + COH901318_TC_INT_CLEAR2);
886 BUG_ON(1);
887 } else {
888 /* Enable powersave if transfer has finished */
889 if (!(readl(virtbase + COH901318_CX_STAT +
890 COH901318_CX_STAT_SPACING*ch) &
891 COH901318_CX_STAT_ENABLED)) {
892 enable_powersave(cohc);
893 }
894 /* Must clear TC interrupt before calling
895 * dma_tc_handle
Justin P. Mattockbc0b44c2011-01-28 11:48:18 -0800896 * in case tc_handle initiate a new dma job
Linus Walleij61f135b2009-11-19 19:49:17 +0100897 */
898 __set_bit(i, virtbase + COH901318_TC_INT_CLEAR2);
899
900 dma_tc_handle(cohc);
901 }
902 }
903 spin_unlock(&cohc->lock);
904 }
905
906 return IRQ_HANDLED;
907}
908
909static int coh901318_alloc_chan_resources(struct dma_chan *chan)
910{
911 struct coh901318_chan *cohc = to_coh901318_chan(chan);
Linus Walleij84c84472010-03-04 14:40:30 +0100912 unsigned long flags;
Linus Walleij61f135b2009-11-19 19:49:17 +0100913
914 dev_vdbg(COHC_2_DEV(cohc), "[%s] DMA channel %d\n",
915 __func__, cohc->id);
916
917 if (chan->client_count > 1)
918 return -EBUSY;
919
Linus Walleij84c84472010-03-04 14:40:30 +0100920 spin_lock_irqsave(&cohc->lock, flags);
921
Linus Walleij61f135b2009-11-19 19:49:17 +0100922 coh901318_config(cohc, NULL);
923
924 cohc->allocated = 1;
925 cohc->completed = chan->cookie = 1;
926
Linus Walleij84c84472010-03-04 14:40:30 +0100927 spin_unlock_irqrestore(&cohc->lock, flags);
928
Linus Walleij61f135b2009-11-19 19:49:17 +0100929 return 1;
930}
931
932static void
933coh901318_free_chan_resources(struct dma_chan *chan)
934{
935 struct coh901318_chan *cohc = to_coh901318_chan(chan);
936 int channel = cohc->id;
937 unsigned long flags;
938
939 spin_lock_irqsave(&cohc->lock, flags);
940
941 /* Disable HW */
942 writel(0x00000000U, cohc->base->virtbase + COH901318_CX_CFG +
943 COH901318_CX_CFG_SPACING*channel);
944 writel(0x00000000U, cohc->base->virtbase + COH901318_CX_CTRL +
945 COH901318_CX_CTRL_SPACING*channel);
946
947 cohc->allocated = 0;
948
949 spin_unlock_irqrestore(&cohc->lock, flags);
950
Linus Walleij05827632010-05-17 16:30:42 -0700951 chan->device->device_control(chan, DMA_TERMINATE_ALL, 0);
Linus Walleij61f135b2009-11-19 19:49:17 +0100952}
953
954
955static dma_cookie_t
956coh901318_tx_submit(struct dma_async_tx_descriptor *tx)
957{
958 struct coh901318_desc *cohd = container_of(tx, struct coh901318_desc,
959 desc);
960 struct coh901318_chan *cohc = to_coh901318_chan(tx->chan);
961 unsigned long flags;
962
963 spin_lock_irqsave(&cohc->lock, flags);
964
965 tx->cookie = coh901318_assign_cookie(cohc, cohd);
966
967 coh901318_desc_queue(cohc, cohd);
968
969 spin_unlock_irqrestore(&cohc->lock, flags);
970
971 return tx->cookie;
972}
973
974static struct dma_async_tx_descriptor *
975coh901318_prep_memcpy(struct dma_chan *chan, dma_addr_t dest, dma_addr_t src,
976 size_t size, unsigned long flags)
977{
Linus Walleijcecd87d2010-03-04 14:31:47 +0100978 struct coh901318_lli *lli;
Linus Walleij61f135b2009-11-19 19:49:17 +0100979 struct coh901318_desc *cohd;
980 unsigned long flg;
981 struct coh901318_chan *cohc = to_coh901318_chan(chan);
982 int lli_len;
983 u32 ctrl_last = cohc_chan_param(cohc)->ctrl_lli_last;
Linus Walleijb87108a2010-03-02 14:17:20 -0700984 int ret;
Linus Walleij61f135b2009-11-19 19:49:17 +0100985
986 spin_lock_irqsave(&cohc->lock, flg);
987
988 dev_vdbg(COHC_2_DEV(cohc),
989 "[%s] channel %d src 0x%x dest 0x%x size %d\n",
990 __func__, cohc->id, src, dest, size);
991
992 if (flags & DMA_PREP_INTERRUPT)
993 /* Trigger interrupt after last lli */
994 ctrl_last |= COH901318_CX_CTRL_TC_IRQ_ENABLE;
995
996 lli_len = size >> MAX_DMA_PACKET_SIZE_SHIFT;
997 if ((lli_len << MAX_DMA_PACKET_SIZE_SHIFT) < size)
998 lli_len++;
999
Linus Walleijcecd87d2010-03-04 14:31:47 +01001000 lli = coh901318_lli_alloc(&cohc->base->pool, lli_len);
Linus Walleij61f135b2009-11-19 19:49:17 +01001001
Linus Walleijcecd87d2010-03-04 14:31:47 +01001002 if (lli == NULL)
Linus Walleij61f135b2009-11-19 19:49:17 +01001003 goto err;
1004
Linus Walleijb87108a2010-03-02 14:17:20 -07001005 ret = coh901318_lli_fill_memcpy(
Linus Walleijcecd87d2010-03-04 14:31:47 +01001006 &cohc->base->pool, lli, src, size, dest,
Linus Walleijb87108a2010-03-02 14:17:20 -07001007 cohc_chan_param(cohc)->ctrl_lli_chained,
1008 ctrl_last);
1009 if (ret)
1010 goto err;
Linus Walleij61f135b2009-11-19 19:49:17 +01001011
Linus Walleijcecd87d2010-03-04 14:31:47 +01001012 COH_DBG(coh901318_list_print(cohc, lli));
Linus Walleij61f135b2009-11-19 19:49:17 +01001013
Linus Walleijb87108a2010-03-02 14:17:20 -07001014 /* Pick a descriptor to handle this transfer */
1015 cohd = coh901318_desc_get(cohc);
Linus Walleijcecd87d2010-03-04 14:31:47 +01001016 cohd->lli = lli;
Linus Walleijb87108a2010-03-02 14:17:20 -07001017 cohd->flags = flags;
Linus Walleij61f135b2009-11-19 19:49:17 +01001018 cohd->desc.tx_submit = coh901318_tx_submit;
1019
1020 spin_unlock_irqrestore(&cohc->lock, flg);
1021
1022 return &cohd->desc;
1023 err:
1024 spin_unlock_irqrestore(&cohc->lock, flg);
1025 return NULL;
1026}
1027
1028static struct dma_async_tx_descriptor *
1029coh901318_prep_slave_sg(struct dma_chan *chan, struct scatterlist *sgl,
Vinod Kouldb8196d2011-10-13 22:34:23 +05301030 unsigned int sg_len, enum dma_transfer_direction direction,
Linus Walleij61f135b2009-11-19 19:49:17 +01001031 unsigned long flags)
1032{
1033 struct coh901318_chan *cohc = to_coh901318_chan(chan);
Linus Walleijcecd87d2010-03-04 14:31:47 +01001034 struct coh901318_lli *lli;
Linus Walleij61f135b2009-11-19 19:49:17 +01001035 struct coh901318_desc *cohd;
Linus Walleij516fd432010-03-02 20:12:46 +01001036 const struct coh901318_params *params;
Linus Walleij61f135b2009-11-19 19:49:17 +01001037 struct scatterlist *sg;
1038 int len = 0;
1039 int size;
1040 int i;
1041 u32 ctrl_chained = cohc_chan_param(cohc)->ctrl_lli_chained;
1042 u32 ctrl = cohc_chan_param(cohc)->ctrl_lli;
1043 u32 ctrl_last = cohc_chan_param(cohc)->ctrl_lli_last;
Linus Walleij516fd432010-03-02 20:12:46 +01001044 u32 config;
Linus Walleij61f135b2009-11-19 19:49:17 +01001045 unsigned long flg;
Linus Walleij0b588282010-03-02 14:17:44 -07001046 int ret;
Linus Walleij61f135b2009-11-19 19:49:17 +01001047
1048 if (!sgl)
1049 goto out;
1050 if (sgl->length == 0)
1051 goto out;
1052
1053 spin_lock_irqsave(&cohc->lock, flg);
1054
1055 dev_vdbg(COHC_2_DEV(cohc), "[%s] sg_len %d dir %d\n",
1056 __func__, sg_len, direction);
1057
1058 if (flags & DMA_PREP_INTERRUPT)
1059 /* Trigger interrupt after last lli */
1060 ctrl_last |= COH901318_CX_CTRL_TC_IRQ_ENABLE;
1061
Linus Walleij516fd432010-03-02 20:12:46 +01001062 params = cohc_chan_param(cohc);
1063 config = params->config;
Linus Walleij128f9042010-08-04 13:37:53 +02001064 /*
1065 * Add runtime-specific control on top, make
1066 * sure the bits you set per peripheral channel are
1067 * cleared in the default config from the platform.
1068 */
1069 ctrl_chained |= cohc->runtime_ctrl;
1070 ctrl_last |= cohc->runtime_ctrl;
1071 ctrl |= cohc->runtime_ctrl;
Linus Walleij516fd432010-03-02 20:12:46 +01001072
Vinod Kouldb8196d2011-10-13 22:34:23 +05301073 if (direction == DMA_MEM_TO_DEV) {
Linus Walleij61f135b2009-11-19 19:49:17 +01001074 u32 tx_flags = COH901318_CX_CTRL_PRDD_SOURCE |
1075 COH901318_CX_CTRL_SRC_ADDR_INC_ENABLE;
1076
Linus Walleij516fd432010-03-02 20:12:46 +01001077 config |= COH901318_CX_CFG_RM_MEMORY_TO_PRIMARY;
Linus Walleij61f135b2009-11-19 19:49:17 +01001078 ctrl_chained |= tx_flags;
1079 ctrl_last |= tx_flags;
1080 ctrl |= tx_flags;
Vinod Kouldb8196d2011-10-13 22:34:23 +05301081 } else if (direction == DMA_DEV_TO_MEM) {
Linus Walleij61f135b2009-11-19 19:49:17 +01001082 u32 rx_flags = COH901318_CX_CTRL_PRDD_DEST |
1083 COH901318_CX_CTRL_DST_ADDR_INC_ENABLE;
1084
Linus Walleij516fd432010-03-02 20:12:46 +01001085 config |= COH901318_CX_CFG_RM_PRIMARY_TO_MEMORY;
Linus Walleij61f135b2009-11-19 19:49:17 +01001086 ctrl_chained |= rx_flags;
1087 ctrl_last |= rx_flags;
1088 ctrl |= rx_flags;
1089 } else
1090 goto err_direction;
1091
Linus Walleij61f135b2009-11-19 19:49:17 +01001092 /* The dma only supports transmitting packages up to
1093 * MAX_DMA_PACKET_SIZE. Calculate to total number of
1094 * dma elemts required to send the entire sg list
1095 */
1096 for_each_sg(sgl, sg, sg_len, i) {
1097 unsigned int factor;
1098 size = sg_dma_len(sg);
1099
1100 if (size <= MAX_DMA_PACKET_SIZE) {
1101 len++;
1102 continue;
1103 }
1104
1105 factor = size >> MAX_DMA_PACKET_SIZE_SHIFT;
1106 if ((factor << MAX_DMA_PACKET_SIZE_SHIFT) < size)
1107 factor++;
1108
1109 len += factor;
1110 }
1111
Linus Walleij848ad122010-03-02 14:17:15 -07001112 pr_debug("Allocate %d lli:s for this transfer\n", len);
Linus Walleijcecd87d2010-03-04 14:31:47 +01001113 lli = coh901318_lli_alloc(&cohc->base->pool, len);
Linus Walleij61f135b2009-11-19 19:49:17 +01001114
Linus Walleijcecd87d2010-03-04 14:31:47 +01001115 if (lli == NULL)
Linus Walleij61f135b2009-11-19 19:49:17 +01001116 goto err_dma_alloc;
1117
Linus Walleijcecd87d2010-03-04 14:31:47 +01001118 /* initiate allocated lli list */
1119 ret = coh901318_lli_fill_sg(&cohc->base->pool, lli, sgl, sg_len,
Linus Walleij0b588282010-03-02 14:17:44 -07001120 cohc_dev_addr(cohc),
1121 ctrl_chained,
1122 ctrl,
1123 ctrl_last,
1124 direction, COH901318_CX_CTRL_TC_IRQ_ENABLE);
1125 if (ret)
1126 goto err_lli_fill;
Linus Walleij61f135b2009-11-19 19:49:17 +01001127
Linus Walleij128f9042010-08-04 13:37:53 +02001128
Linus Walleijcecd87d2010-03-04 14:31:47 +01001129 COH_DBG(coh901318_list_print(cohc, lli));
Linus Walleij61f135b2009-11-19 19:49:17 +01001130
Linus Walleijb87108a2010-03-02 14:17:20 -07001131 /* Pick a descriptor to handle this transfer */
1132 cohd = coh901318_desc_get(cohc);
Linus Walleijb89243d2011-07-01 16:47:28 +02001133 cohd->head_config = config;
1134 /*
1135 * Set the default head ctrl for the channel to the one from the
1136 * lli, things may have changed due to odd buffer alignment
1137 * etc.
1138 */
1139 cohd->head_ctrl = lli->control;
Linus Walleijb87108a2010-03-02 14:17:20 -07001140 cohd->dir = direction;
1141 cohd->flags = flags;
1142 cohd->desc.tx_submit = coh901318_tx_submit;
Linus Walleijcecd87d2010-03-04 14:31:47 +01001143 cohd->lli = lli;
Linus Walleijb87108a2010-03-02 14:17:20 -07001144
Linus Walleij61f135b2009-11-19 19:49:17 +01001145 spin_unlock_irqrestore(&cohc->lock, flg);
1146
1147 return &cohd->desc;
Linus Walleij0b588282010-03-02 14:17:44 -07001148 err_lli_fill:
Linus Walleij61f135b2009-11-19 19:49:17 +01001149 err_dma_alloc:
1150 err_direction:
Linus Walleij61f135b2009-11-19 19:49:17 +01001151 spin_unlock_irqrestore(&cohc->lock, flg);
1152 out:
1153 return NULL;
1154}
1155
1156static enum dma_status
Linus Walleij07934482010-03-26 16:50:49 -07001157coh901318_tx_status(struct dma_chan *chan, dma_cookie_t cookie,
1158 struct dma_tx_state *txstate)
Linus Walleij61f135b2009-11-19 19:49:17 +01001159{
1160 struct coh901318_chan *cohc = to_coh901318_chan(chan);
1161 dma_cookie_t last_used;
1162 dma_cookie_t last_complete;
1163 int ret;
1164
1165 last_complete = cohc->completed;
1166 last_used = chan->cookie;
1167
1168 ret = dma_async_is_complete(cookie, last_complete, last_used);
1169
Dan Williamsbca34692010-03-26 16:52:10 -07001170 dma_set_tx_state(txstate, last_complete, last_used,
1171 coh901318_get_bytes_left(chan));
Linus Walleij07934482010-03-26 16:50:49 -07001172 if (ret == DMA_IN_PROGRESS && cohc->stopped)
1173 ret = DMA_PAUSED;
Linus Walleij61f135b2009-11-19 19:49:17 +01001174
1175 return ret;
1176}
1177
1178static void
1179coh901318_issue_pending(struct dma_chan *chan)
1180{
1181 struct coh901318_chan *cohc = to_coh901318_chan(chan);
1182 unsigned long flags;
1183
1184 spin_lock_irqsave(&cohc->lock, flags);
1185
Linus Walleijcecd87d2010-03-04 14:31:47 +01001186 /*
1187 * Busy means that pending jobs are already being processed,
1188 * and then there is no point in starting the queue: the
1189 * terminal count interrupt on the channel will take the next
1190 * job on the queue and execute it anyway.
1191 */
Linus Walleij61f135b2009-11-19 19:49:17 +01001192 if (!cohc->busy)
1193 coh901318_queue_start(cohc);
1194
1195 spin_unlock_irqrestore(&cohc->lock, flags);
1196}
1197
Linus Walleij128f9042010-08-04 13:37:53 +02001198/*
1199 * Here we wrap in the runtime dma control interface
1200 */
1201struct burst_table {
1202 int burst_8bit;
1203 int burst_16bit;
1204 int burst_32bit;
1205 u32 reg;
1206};
1207
1208static const struct burst_table burst_sizes[] = {
1209 {
1210 .burst_8bit = 64,
1211 .burst_16bit = 32,
1212 .burst_32bit = 16,
1213 .reg = COH901318_CX_CTRL_BURST_COUNT_64_BYTES,
1214 },
1215 {
1216 .burst_8bit = 48,
1217 .burst_16bit = 24,
1218 .burst_32bit = 12,
1219 .reg = COH901318_CX_CTRL_BURST_COUNT_48_BYTES,
1220 },
1221 {
1222 .burst_8bit = 32,
1223 .burst_16bit = 16,
1224 .burst_32bit = 8,
1225 .reg = COH901318_CX_CTRL_BURST_COUNT_32_BYTES,
1226 },
1227 {
1228 .burst_8bit = 16,
1229 .burst_16bit = 8,
1230 .burst_32bit = 4,
1231 .reg = COH901318_CX_CTRL_BURST_COUNT_16_BYTES,
1232 },
1233 {
1234 .burst_8bit = 8,
1235 .burst_16bit = 4,
1236 .burst_32bit = 2,
1237 .reg = COH901318_CX_CTRL_BURST_COUNT_8_BYTES,
1238 },
1239 {
1240 .burst_8bit = 4,
1241 .burst_16bit = 2,
1242 .burst_32bit = 1,
1243 .reg = COH901318_CX_CTRL_BURST_COUNT_4_BYTES,
1244 },
1245 {
1246 .burst_8bit = 2,
1247 .burst_16bit = 1,
1248 .burst_32bit = 0,
1249 .reg = COH901318_CX_CTRL_BURST_COUNT_2_BYTES,
1250 },
1251 {
1252 .burst_8bit = 1,
1253 .burst_16bit = 0,
1254 .burst_32bit = 0,
1255 .reg = COH901318_CX_CTRL_BURST_COUNT_1_BYTE,
1256 },
1257};
1258
1259static void coh901318_dma_set_runtimeconfig(struct dma_chan *chan,
1260 struct dma_slave_config *config)
1261{
1262 struct coh901318_chan *cohc = to_coh901318_chan(chan);
1263 dma_addr_t addr;
1264 enum dma_slave_buswidth addr_width;
1265 u32 maxburst;
1266 u32 runtime_ctrl = 0;
1267 int i = 0;
1268
1269 /* We only support mem to per or per to mem transfers */
Vinod Kouldb8196d2011-10-13 22:34:23 +05301270 if (config->direction == DMA_DEV_TO_MEM) {
Linus Walleij128f9042010-08-04 13:37:53 +02001271 addr = config->src_addr;
1272 addr_width = config->src_addr_width;
1273 maxburst = config->src_maxburst;
Vinod Kouldb8196d2011-10-13 22:34:23 +05301274 } else if (config->direction == DMA_MEM_TO_DEV) {
Linus Walleij128f9042010-08-04 13:37:53 +02001275 addr = config->dst_addr;
1276 addr_width = config->dst_addr_width;
1277 maxburst = config->dst_maxburst;
1278 } else {
1279 dev_err(COHC_2_DEV(cohc), "illegal channel mode\n");
1280 return;
1281 }
1282
1283 dev_dbg(COHC_2_DEV(cohc), "configure channel for %d byte transfers\n",
1284 addr_width);
1285 switch (addr_width) {
1286 case DMA_SLAVE_BUSWIDTH_1_BYTE:
1287 runtime_ctrl |=
1288 COH901318_CX_CTRL_SRC_BUS_SIZE_8_BITS |
1289 COH901318_CX_CTRL_DST_BUS_SIZE_8_BITS;
1290
1291 while (i < ARRAY_SIZE(burst_sizes)) {
1292 if (burst_sizes[i].burst_8bit <= maxburst)
1293 break;
1294 i++;
1295 }
1296
1297 break;
1298 case DMA_SLAVE_BUSWIDTH_2_BYTES:
1299 runtime_ctrl |=
1300 COH901318_CX_CTRL_SRC_BUS_SIZE_16_BITS |
1301 COH901318_CX_CTRL_DST_BUS_SIZE_16_BITS;
1302
1303 while (i < ARRAY_SIZE(burst_sizes)) {
1304 if (burst_sizes[i].burst_16bit <= maxburst)
1305 break;
1306 i++;
1307 }
1308
1309 break;
1310 case DMA_SLAVE_BUSWIDTH_4_BYTES:
1311 /* Direction doesn't matter here, it's 32/32 bits */
1312 runtime_ctrl |=
1313 COH901318_CX_CTRL_SRC_BUS_SIZE_32_BITS |
1314 COH901318_CX_CTRL_DST_BUS_SIZE_32_BITS;
1315
1316 while (i < ARRAY_SIZE(burst_sizes)) {
1317 if (burst_sizes[i].burst_32bit <= maxburst)
1318 break;
1319 i++;
1320 }
1321
1322 break;
1323 default:
1324 dev_err(COHC_2_DEV(cohc),
1325 "bad runtimeconfig: alien address width\n");
1326 return;
1327 }
1328
1329 runtime_ctrl |= burst_sizes[i].reg;
1330 dev_dbg(COHC_2_DEV(cohc),
1331 "selected burst size %d bytes for address width %d bytes, maxburst %d\n",
1332 burst_sizes[i].burst_8bit, addr_width, maxburst);
1333
1334 cohc->runtime_addr = addr;
1335 cohc->runtime_ctrl = runtime_ctrl;
1336}
1337
Linus Walleijc3635c72010-03-26 16:44:01 -07001338static int
Linus Walleij05827632010-05-17 16:30:42 -07001339coh901318_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd,
1340 unsigned long arg)
Linus Walleij61f135b2009-11-19 19:49:17 +01001341{
1342 unsigned long flags;
1343 struct coh901318_chan *cohc = to_coh901318_chan(chan);
1344 struct coh901318_desc *cohd;
1345 void __iomem *virtbase = cohc->base->virtbase;
1346
Linus Walleij128f9042010-08-04 13:37:53 +02001347 if (cmd == DMA_SLAVE_CONFIG) {
1348 struct dma_slave_config *config =
1349 (struct dma_slave_config *) arg;
1350
1351 coh901318_dma_set_runtimeconfig(chan, config);
1352 return 0;
1353 }
1354
Linus Walleijc3635c72010-03-26 16:44:01 -07001355 if (cmd == DMA_PAUSE) {
1356 coh901318_pause(chan);
1357 return 0;
1358 }
Linus Walleij61f135b2009-11-19 19:49:17 +01001359
Linus Walleijc3635c72010-03-26 16:44:01 -07001360 if (cmd == DMA_RESUME) {
1361 coh901318_resume(chan);
1362 return 0;
1363 }
1364
1365 if (cmd != DMA_TERMINATE_ALL)
1366 return -ENXIO;
1367
1368 /* The remainder of this function terminates the transfer */
1369 coh901318_pause(chan);
Linus Walleij61f135b2009-11-19 19:49:17 +01001370 spin_lock_irqsave(&cohc->lock, flags);
1371
1372 /* Clear any pending BE or TC interrupt */
1373 if (cohc->id < 32) {
1374 writel(1 << cohc->id, virtbase + COH901318_BE_INT_CLEAR1);
1375 writel(1 << cohc->id, virtbase + COH901318_TC_INT_CLEAR1);
1376 } else {
1377 writel(1 << (cohc->id - 32), virtbase +
1378 COH901318_BE_INT_CLEAR2);
1379 writel(1 << (cohc->id - 32), virtbase +
1380 COH901318_TC_INT_CLEAR2);
1381 }
1382
1383 enable_powersave(cohc);
1384
1385 while ((cohd = coh901318_first_active_get(cohc))) {
1386 /* release the lli allocation*/
Linus Walleijcecd87d2010-03-04 14:31:47 +01001387 coh901318_lli_free(&cohc->base->pool, &cohd->lli);
Linus Walleij61f135b2009-11-19 19:49:17 +01001388
Linus Walleij61f135b2009-11-19 19:49:17 +01001389 /* return desc to free-list */
Linus Walleij848ad122010-03-02 14:17:15 -07001390 coh901318_desc_remove(cohd);
Linus Walleij61f135b2009-11-19 19:49:17 +01001391 coh901318_desc_free(cohc, cohd);
1392 }
1393
1394 while ((cohd = coh901318_first_queued(cohc))) {
1395 /* release the lli allocation*/
Linus Walleijcecd87d2010-03-04 14:31:47 +01001396 coh901318_lli_free(&cohc->base->pool, &cohd->lli);
Linus Walleij61f135b2009-11-19 19:49:17 +01001397
Linus Walleij61f135b2009-11-19 19:49:17 +01001398 /* return desc to free-list */
Linus Walleij848ad122010-03-02 14:17:15 -07001399 coh901318_desc_remove(cohd);
Linus Walleij61f135b2009-11-19 19:49:17 +01001400 coh901318_desc_free(cohc, cohd);
1401 }
1402
1403
1404 cohc->nbr_active_done = 0;
1405 cohc->busy = 0;
Linus Walleij61f135b2009-11-19 19:49:17 +01001406
1407 spin_unlock_irqrestore(&cohc->lock, flags);
Linus Walleijc3635c72010-03-26 16:44:01 -07001408
1409 return 0;
Linus Walleij61f135b2009-11-19 19:49:17 +01001410}
Linus Walleij128f9042010-08-04 13:37:53 +02001411
Linus Walleij61f135b2009-11-19 19:49:17 +01001412void coh901318_base_init(struct dma_device *dma, const int *pick_chans,
1413 struct coh901318_base *base)
1414{
1415 int chans_i;
1416 int i = 0;
1417 struct coh901318_chan *cohc;
1418
1419 INIT_LIST_HEAD(&dma->channels);
1420
1421 for (chans_i = 0; pick_chans[chans_i] != -1; chans_i += 2) {
1422 for (i = pick_chans[chans_i]; i <= pick_chans[chans_i+1]; i++) {
1423 cohc = &base->chans[i];
1424
1425 cohc->base = base;
1426 cohc->chan.device = dma;
1427 cohc->id = i;
1428
1429 /* TODO: do we really need this lock if only one
1430 * client is connected to each channel?
1431 */
1432
1433 spin_lock_init(&cohc->lock);
1434
Linus Walleij61f135b2009-11-19 19:49:17 +01001435 cohc->nbr_active_done = 0;
1436 cohc->busy = 0;
1437 INIT_LIST_HEAD(&cohc->free);
1438 INIT_LIST_HEAD(&cohc->active);
1439 INIT_LIST_HEAD(&cohc->queue);
1440
1441 tasklet_init(&cohc->tasklet, dma_tasklet,
1442 (unsigned long) cohc);
1443
1444 list_add_tail(&cohc->chan.device_node,
1445 &dma->channels);
1446 }
1447 }
1448}
1449
1450static int __init coh901318_probe(struct platform_device *pdev)
1451{
1452 int err = 0;
1453 struct coh901318_platform *pdata;
1454 struct coh901318_base *base;
1455 int irq;
1456 struct resource *io;
1457
1458 io = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1459 if (!io)
1460 goto err_get_resource;
1461
1462 /* Map DMA controller registers to virtual memory */
1463 if (request_mem_region(io->start,
1464 resource_size(io),
1465 pdev->dev.driver->name) == NULL) {
1466 err = -EBUSY;
1467 goto err_request_mem;
1468 }
1469
1470 pdata = pdev->dev.platform_data;
1471 if (!pdata)
1472 goto err_no_platformdata;
1473
1474 base = kmalloc(ALIGN(sizeof(struct coh901318_base), 4) +
1475 pdata->max_channels *
1476 sizeof(struct coh901318_chan),
1477 GFP_KERNEL);
1478 if (!base)
1479 goto err_alloc_coh_dma_channels;
1480
1481 base->chans = ((void *)base) + ALIGN(sizeof(struct coh901318_base), 4);
1482
1483 base->virtbase = ioremap(io->start, resource_size(io));
1484 if (!base->virtbase) {
1485 err = -ENOMEM;
1486 goto err_no_ioremap;
1487 }
1488
1489 base->dev = &pdev->dev;
1490 base->platform = pdata;
1491 spin_lock_init(&base->pm.lock);
1492 base->pm.started_channels = 0;
1493
1494 COH901318_DEBUGFS_ASSIGN(debugfs_dma_base, base);
1495
1496 platform_set_drvdata(pdev, base);
1497
1498 irq = platform_get_irq(pdev, 0);
1499 if (irq < 0)
1500 goto err_no_irq;
1501
1502 err = request_irq(irq, dma_irq_handler, IRQF_DISABLED,
1503 "coh901318", base);
1504 if (err) {
1505 dev_crit(&pdev->dev,
1506 "Cannot allocate IRQ for DMA controller!\n");
1507 goto err_request_irq;
1508 }
1509
1510 err = coh901318_pool_create(&base->pool, &pdev->dev,
1511 sizeof(struct coh901318_lli),
1512 32);
1513 if (err)
1514 goto err_pool_create;
1515
1516 /* init channels for device transfers */
1517 coh901318_base_init(&base->dma_slave, base->platform->chans_slave,
1518 base);
1519
1520 dma_cap_zero(base->dma_slave.cap_mask);
1521 dma_cap_set(DMA_SLAVE, base->dma_slave.cap_mask);
1522
1523 base->dma_slave.device_alloc_chan_resources = coh901318_alloc_chan_resources;
1524 base->dma_slave.device_free_chan_resources = coh901318_free_chan_resources;
1525 base->dma_slave.device_prep_slave_sg = coh901318_prep_slave_sg;
Linus Walleij07934482010-03-26 16:50:49 -07001526 base->dma_slave.device_tx_status = coh901318_tx_status;
Linus Walleij61f135b2009-11-19 19:49:17 +01001527 base->dma_slave.device_issue_pending = coh901318_issue_pending;
Linus Walleijc3635c72010-03-26 16:44:01 -07001528 base->dma_slave.device_control = coh901318_control;
Linus Walleij61f135b2009-11-19 19:49:17 +01001529 base->dma_slave.dev = &pdev->dev;
1530
1531 err = dma_async_device_register(&base->dma_slave);
1532
1533 if (err)
1534 goto err_register_slave;
1535
1536 /* init channels for memcpy */
1537 coh901318_base_init(&base->dma_memcpy, base->platform->chans_memcpy,
1538 base);
1539
1540 dma_cap_zero(base->dma_memcpy.cap_mask);
1541 dma_cap_set(DMA_MEMCPY, base->dma_memcpy.cap_mask);
1542
1543 base->dma_memcpy.device_alloc_chan_resources = coh901318_alloc_chan_resources;
1544 base->dma_memcpy.device_free_chan_resources = coh901318_free_chan_resources;
1545 base->dma_memcpy.device_prep_dma_memcpy = coh901318_prep_memcpy;
Linus Walleij07934482010-03-26 16:50:49 -07001546 base->dma_memcpy.device_tx_status = coh901318_tx_status;
Linus Walleij61f135b2009-11-19 19:49:17 +01001547 base->dma_memcpy.device_issue_pending = coh901318_issue_pending;
Linus Walleijc3635c72010-03-26 16:44:01 -07001548 base->dma_memcpy.device_control = coh901318_control;
Linus Walleij61f135b2009-11-19 19:49:17 +01001549 base->dma_memcpy.dev = &pdev->dev;
Linus Walleij516fd432010-03-02 20:12:46 +01001550 /*
1551 * This controller can only access address at even 32bit boundaries,
1552 * i.e. 2^2
1553 */
1554 base->dma_memcpy.copy_align = 2;
Linus Walleij61f135b2009-11-19 19:49:17 +01001555 err = dma_async_device_register(&base->dma_memcpy);
1556
1557 if (err)
1558 goto err_register_memcpy;
1559
Linus Walleij848ad122010-03-02 14:17:15 -07001560 dev_info(&pdev->dev, "Initialized COH901318 DMA on virtual base 0x%08x\n",
Linus Walleij61f135b2009-11-19 19:49:17 +01001561 (u32) base->virtbase);
1562
1563 return err;
1564
1565 err_register_memcpy:
1566 dma_async_device_unregister(&base->dma_slave);
1567 err_register_slave:
1568 coh901318_pool_destroy(&base->pool);
1569 err_pool_create:
1570 free_irq(platform_get_irq(pdev, 0), base);
1571 err_request_irq:
1572 err_no_irq:
1573 iounmap(base->virtbase);
1574 err_no_ioremap:
1575 kfree(base);
1576 err_alloc_coh_dma_channels:
1577 err_no_platformdata:
1578 release_mem_region(pdev->resource->start,
1579 resource_size(pdev->resource));
1580 err_request_mem:
1581 err_get_resource:
1582 return err;
1583}
1584
1585static int __exit coh901318_remove(struct platform_device *pdev)
1586{
1587 struct coh901318_base *base = platform_get_drvdata(pdev);
1588
1589 dma_async_device_unregister(&base->dma_memcpy);
1590 dma_async_device_unregister(&base->dma_slave);
1591 coh901318_pool_destroy(&base->pool);
1592 free_irq(platform_get_irq(pdev, 0), base);
Linus Walleij61f135b2009-11-19 19:49:17 +01001593 iounmap(base->virtbase);
Julia Lawall0794ec82009-12-22 21:30:59 +01001594 kfree(base);
Linus Walleij61f135b2009-11-19 19:49:17 +01001595 release_mem_region(pdev->resource->start,
1596 resource_size(pdev->resource));
1597 return 0;
1598}
1599
1600
1601static struct platform_driver coh901318_driver = {
1602 .remove = __exit_p(coh901318_remove),
1603 .driver = {
1604 .name = "coh901318",
1605 },
1606};
1607
1608int __init coh901318_init(void)
1609{
1610 return platform_driver_probe(&coh901318_driver, coh901318_probe);
1611}
Linus Walleija0eb2212011-05-18 14:18:57 +02001612subsys_initcall(coh901318_init);
Linus Walleij61f135b2009-11-19 19:49:17 +01001613
1614void __exit coh901318_exit(void)
1615{
1616 platform_driver_unregister(&coh901318_driver);
1617}
1618module_exit(coh901318_exit);
1619
1620MODULE_LICENSE("GPL");
1621MODULE_AUTHOR("Per Friden");