blob: 5fdd38bcda23a023f48b2c7817ca120ab9e612d8 [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>
Linus Walleij9f575d92013-01-04 10:35:06 +010024#include <linux/platform_data/dma-coh901318.h>
Linus Walleij61f135b2009-11-19 19:49:17 +010025#include <mach/coh901318.h>
26
27#include "coh901318_lli.h"
Russell King - ARM Linuxd2ebfb32012-03-06 22:34:26 +000028#include "dmaengine.h"
Linus Walleij61f135b2009-11-19 19:49:17 +010029
30#define COHC_2_DEV(cohc) (&cohc->chan.dev->device)
31
32#ifdef VERBOSE_DEBUG
33#define COH_DBG(x) ({ if (1) x; 0; })
34#else
35#define COH_DBG(x) ({ if (0) x; 0; })
36#endif
37
38struct coh901318_desc {
39 struct dma_async_tx_descriptor desc;
40 struct list_head node;
41 struct scatterlist *sg;
42 unsigned int sg_len;
Linus Walleijcecd87d2010-03-04 14:31:47 +010043 struct coh901318_lli *lli;
Vinod Kouldb8196d2011-10-13 22:34:23 +053044 enum dma_transfer_direction dir;
Linus Walleij61f135b2009-11-19 19:49:17 +010045 unsigned long flags;
Linus Walleijb89243d2011-07-01 16:47:28 +020046 u32 head_config;
47 u32 head_ctrl;
Linus Walleij61f135b2009-11-19 19:49:17 +010048};
49
50struct coh901318_base {
51 struct device *dev;
52 void __iomem *virtbase;
53 struct coh901318_pool pool;
54 struct powersave pm;
55 struct dma_device dma_slave;
56 struct dma_device dma_memcpy;
57 struct coh901318_chan *chans;
58 struct coh901318_platform *platform;
59};
60
61struct coh901318_chan {
62 spinlock_t lock;
63 int allocated;
Linus Walleij61f135b2009-11-19 19:49:17 +010064 int id;
65 int stopped;
66
67 struct work_struct free_work;
68 struct dma_chan chan;
69
70 struct tasklet_struct tasklet;
71
72 struct list_head active;
73 struct list_head queue;
74 struct list_head free;
75
76 unsigned long nbr_active_done;
77 unsigned long busy;
Linus Walleij61f135b2009-11-19 19:49:17 +010078
Linus Walleij128f9042010-08-04 13:37:53 +020079 u32 runtime_addr;
80 u32 runtime_ctrl;
81
Linus Walleij61f135b2009-11-19 19:49:17 +010082 struct coh901318_base *base;
83};
84
85static void coh901318_list_print(struct coh901318_chan *cohc,
86 struct coh901318_lli *lli)
87{
Linus Walleij848ad122010-03-02 14:17:15 -070088 struct coh901318_lli *l = lli;
Linus Walleij61f135b2009-11-19 19:49:17 +010089 int i = 0;
90
Linus Walleij848ad122010-03-02 14:17:15 -070091 while (l) {
Linus Walleij61f135b2009-11-19 19:49:17 +010092 dev_vdbg(COHC_2_DEV(cohc), "i %d, lli %p, ctrl 0x%x, src 0x%x"
Linus Walleij848ad122010-03-02 14:17:15 -070093 ", dst 0x%x, link 0x%x virt_link_addr 0x%p\n",
Linus Walleij61f135b2009-11-19 19:49:17 +010094 i, l, l->control, l->src_addr, l->dst_addr,
Linus Walleij848ad122010-03-02 14:17:15 -070095 l->link_addr, l->virt_link_addr);
Linus Walleij61f135b2009-11-19 19:49:17 +010096 i++;
Linus Walleij848ad122010-03-02 14:17:15 -070097 l = l->virt_link_addr;
Linus Walleij61f135b2009-11-19 19:49:17 +010098 }
99}
100
101#ifdef CONFIG_DEBUG_FS
102
103#define COH901318_DEBUGFS_ASSIGN(x, y) (x = y)
104
105static struct coh901318_base *debugfs_dma_base;
106static struct dentry *dma_dentry;
107
Linus Walleij61f135b2009-11-19 19:49:17 +0100108static int coh901318_debugfs_read(struct file *file, char __user *buf,
109 size_t count, loff_t *f_pos)
110{
111 u64 started_channels = debugfs_dma_base->pm.started_channels;
112 int pool_count = debugfs_dma_base->pool.debugfs_pool_counter;
113 int i;
114 int ret = 0;
115 char *dev_buf;
116 char *tmp;
117 int dev_size;
118
119 dev_buf = kmalloc(4*1024, GFP_KERNEL);
120 if (dev_buf == NULL)
121 goto err_kmalloc;
122 tmp = dev_buf;
123
Linus Walleij848ad122010-03-02 14:17:15 -0700124 tmp += sprintf(tmp, "DMA -- enabled dma channels\n");
Linus Walleij61f135b2009-11-19 19:49:17 +0100125
126 for (i = 0; i < debugfs_dma_base->platform->max_channels; i++)
127 if (started_channels & (1 << i))
128 tmp += sprintf(tmp, "channel %d\n", i);
129
130 tmp += sprintf(tmp, "Pool alloc nbr %d\n", pool_count);
131 dev_size = tmp - dev_buf;
132
133 /* No more to read if offset != 0 */
134 if (*f_pos > dev_size)
135 goto out;
136
137 if (count > dev_size - *f_pos)
138 count = dev_size - *f_pos;
139
140 if (copy_to_user(buf, dev_buf + *f_pos, count))
141 ret = -EINVAL;
142 ret = count;
143 *f_pos += count;
144
145 out:
146 kfree(dev_buf);
147 return ret;
148
149 err_kmalloc:
150 return 0;
151}
152
153static const struct file_operations coh901318_debugfs_status_operations = {
154 .owner = THIS_MODULE,
Stephen Boyd234e3402012-04-05 14:25:11 -0700155 .open = simple_open,
Linus Walleij61f135b2009-11-19 19:49:17 +0100156 .read = coh901318_debugfs_read,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200157 .llseek = default_llseek,
Linus Walleij61f135b2009-11-19 19:49:17 +0100158};
159
160
161static int __init init_coh901318_debugfs(void)
162{
163
164 dma_dentry = debugfs_create_dir("dma", NULL);
165
166 (void) debugfs_create_file("status",
167 S_IFREG | S_IRUGO,
168 dma_dentry, NULL,
169 &coh901318_debugfs_status_operations);
170 return 0;
171}
172
173static void __exit exit_coh901318_debugfs(void)
174{
175 debugfs_remove_recursive(dma_dentry);
176}
177
178module_init(init_coh901318_debugfs);
179module_exit(exit_coh901318_debugfs);
180#else
181
182#define COH901318_DEBUGFS_ASSIGN(x, y)
183
184#endif /* CONFIG_DEBUG_FS */
185
186static inline struct coh901318_chan *to_coh901318_chan(struct dma_chan *chan)
187{
188 return container_of(chan, struct coh901318_chan, chan);
189}
190
191static inline dma_addr_t
192cohc_dev_addr(struct coh901318_chan *cohc)
193{
Linus Walleij128f9042010-08-04 13:37:53 +0200194 /* Runtime supplied address will take precedence */
195 if (cohc->runtime_addr)
196 return cohc->runtime_addr;
Linus Walleij61f135b2009-11-19 19:49:17 +0100197 return cohc->base->platform->chan_conf[cohc->id].dev_addr;
198}
199
200static inline const struct coh901318_params *
201cohc_chan_param(struct coh901318_chan *cohc)
202{
203 return &cohc->base->platform->chan_conf[cohc->id].param;
204}
205
206static inline const struct coh_dma_channel *
207cohc_chan_conf(struct coh901318_chan *cohc)
208{
209 return &cohc->base->platform->chan_conf[cohc->id];
210}
211
212static void enable_powersave(struct coh901318_chan *cohc)
213{
214 unsigned long flags;
215 struct powersave *pm = &cohc->base->pm;
216
217 spin_lock_irqsave(&pm->lock, flags);
218
219 pm->started_channels &= ~(1ULL << cohc->id);
220
221 if (!pm->started_channels) {
222 /* DMA no longer intends to access memory */
223 cohc->base->platform->access_memory_state(cohc->base->dev,
224 false);
225 }
226
227 spin_unlock_irqrestore(&pm->lock, flags);
228}
229static void disable_powersave(struct coh901318_chan *cohc)
230{
231 unsigned long flags;
232 struct powersave *pm = &cohc->base->pm;
233
234 spin_lock_irqsave(&pm->lock, flags);
235
236 if (!pm->started_channels) {
237 /* DMA intends to access memory */
238 cohc->base->platform->access_memory_state(cohc->base->dev,
239 true);
240 }
241
242 pm->started_channels |= (1ULL << cohc->id);
243
244 spin_unlock_irqrestore(&pm->lock, flags);
245}
246
247static inline int coh901318_set_ctrl(struct coh901318_chan *cohc, u32 control)
248{
249 int channel = cohc->id;
250 void __iomem *virtbase = cohc->base->virtbase;
251
252 writel(control,
253 virtbase + COH901318_CX_CTRL +
254 COH901318_CX_CTRL_SPACING * channel);
255 return 0;
256}
257
258static inline int coh901318_set_conf(struct coh901318_chan *cohc, u32 conf)
259{
260 int channel = cohc->id;
261 void __iomem *virtbase = cohc->base->virtbase;
262
263 writel(conf,
264 virtbase + COH901318_CX_CFG +
265 COH901318_CX_CFG_SPACING*channel);
266 return 0;
267}
268
269
270static int coh901318_start(struct coh901318_chan *cohc)
271{
272 u32 val;
273 int channel = cohc->id;
274 void __iomem *virtbase = cohc->base->virtbase;
275
276 disable_powersave(cohc);
277
278 val = readl(virtbase + COH901318_CX_CFG +
279 COH901318_CX_CFG_SPACING * channel);
280
281 /* Enable channel */
282 val |= COH901318_CX_CFG_CH_ENABLE;
283 writel(val, virtbase + COH901318_CX_CFG +
284 COH901318_CX_CFG_SPACING * channel);
285
286 return 0;
287}
288
289static int coh901318_prep_linked_list(struct coh901318_chan *cohc,
Linus Walleijcecd87d2010-03-04 14:31:47 +0100290 struct coh901318_lli *lli)
Linus Walleij61f135b2009-11-19 19:49:17 +0100291{
292 int channel = cohc->id;
293 void __iomem *virtbase = cohc->base->virtbase;
294
295 BUG_ON(readl(virtbase + COH901318_CX_STAT +
296 COH901318_CX_STAT_SPACING*channel) &
297 COH901318_CX_STAT_ACTIVE);
298
Linus Walleijcecd87d2010-03-04 14:31:47 +0100299 writel(lli->src_addr,
Linus Walleij61f135b2009-11-19 19:49:17 +0100300 virtbase + COH901318_CX_SRC_ADDR +
301 COH901318_CX_SRC_ADDR_SPACING * channel);
302
Linus Walleijcecd87d2010-03-04 14:31:47 +0100303 writel(lli->dst_addr, virtbase +
Linus Walleij61f135b2009-11-19 19:49:17 +0100304 COH901318_CX_DST_ADDR +
305 COH901318_CX_DST_ADDR_SPACING * channel);
306
Linus Walleijcecd87d2010-03-04 14:31:47 +0100307 writel(lli->link_addr, virtbase + COH901318_CX_LNK_ADDR +
Linus Walleij61f135b2009-11-19 19:49:17 +0100308 COH901318_CX_LNK_ADDR_SPACING * channel);
309
Linus Walleijcecd87d2010-03-04 14:31:47 +0100310 writel(lli->control, virtbase + COH901318_CX_CTRL +
Linus Walleij61f135b2009-11-19 19:49:17 +0100311 COH901318_CX_CTRL_SPACING * channel);
312
313 return 0;
314}
Linus Walleij61f135b2009-11-19 19:49:17 +0100315
316static struct coh901318_desc *
317coh901318_desc_get(struct coh901318_chan *cohc)
318{
319 struct coh901318_desc *desc;
320
321 if (list_empty(&cohc->free)) {
322 /* alloc new desc because we're out of used ones
323 * TODO: alloc a pile of descs instead of just one,
324 * avoid many small allocations.
325 */
Linus Walleijb87108a2010-03-02 14:17:20 -0700326 desc = kzalloc(sizeof(struct coh901318_desc), GFP_NOWAIT);
Linus Walleij61f135b2009-11-19 19:49:17 +0100327 if (desc == NULL)
328 goto out;
329 INIT_LIST_HEAD(&desc->node);
Linus Walleijb87108a2010-03-02 14:17:20 -0700330 dma_async_tx_descriptor_init(&desc->desc, &cohc->chan);
Linus Walleij61f135b2009-11-19 19:49:17 +0100331 } else {
332 /* Reuse an old desc. */
333 desc = list_first_entry(&cohc->free,
334 struct coh901318_desc,
335 node);
336 list_del(&desc->node);
Linus Walleijb87108a2010-03-02 14:17:20 -0700337 /* Initialize it a bit so it's not insane */
338 desc->sg = NULL;
339 desc->sg_len = 0;
340 desc->desc.callback = NULL;
341 desc->desc.callback_param = NULL;
Linus Walleij61f135b2009-11-19 19:49:17 +0100342 }
343
344 out:
345 return desc;
346}
347
348static void
349coh901318_desc_free(struct coh901318_chan *cohc, struct coh901318_desc *cohd)
350{
351 list_add_tail(&cohd->node, &cohc->free);
352}
353
354/* call with irq lock held */
355static void
356coh901318_desc_submit(struct coh901318_chan *cohc, struct coh901318_desc *desc)
357{
358 list_add_tail(&desc->node, &cohc->active);
Linus Walleij61f135b2009-11-19 19:49:17 +0100359}
360
361static struct coh901318_desc *
362coh901318_first_active_get(struct coh901318_chan *cohc)
363{
364 struct coh901318_desc *d;
365
366 if (list_empty(&cohc->active))
367 return NULL;
368
369 d = list_first_entry(&cohc->active,
370 struct coh901318_desc,
371 node);
372 return d;
373}
374
375static void
376coh901318_desc_remove(struct coh901318_desc *cohd)
377{
378 list_del(&cohd->node);
379}
380
381static void
382coh901318_desc_queue(struct coh901318_chan *cohc, struct coh901318_desc *desc)
383{
384 list_add_tail(&desc->node, &cohc->queue);
385}
386
387static struct coh901318_desc *
388coh901318_first_queued(struct coh901318_chan *cohc)
389{
390 struct coh901318_desc *d;
391
392 if (list_empty(&cohc->queue))
393 return NULL;
394
395 d = list_first_entry(&cohc->queue,
396 struct coh901318_desc,
397 node);
398 return d;
399}
400
Linus Walleij84c84472010-03-04 14:40:30 +0100401static inline u32 coh901318_get_bytes_in_lli(struct coh901318_lli *in_lli)
402{
403 struct coh901318_lli *lli = in_lli;
404 u32 bytes = 0;
405
406 while (lli) {
407 bytes += lli->control & COH901318_CX_CTRL_TC_VALUE_MASK;
408 lli = lli->virt_link_addr;
409 }
410 return bytes;
411}
412
Linus Walleij61f135b2009-11-19 19:49:17 +0100413/*
Linus Walleij84c84472010-03-04 14:40:30 +0100414 * Get the number of bytes left to transfer on this channel,
415 * it is unwise to call this before stopping the channel for
416 * absolute measures, but for a rough guess you can still call
417 * it.
Linus Walleij61f135b2009-11-19 19:49:17 +0100418 */
Linus Walleij07934482010-03-26 16:50:49 -0700419static u32 coh901318_get_bytes_left(struct dma_chan *chan)
Linus Walleij61f135b2009-11-19 19:49:17 +0100420{
Linus Walleij61f135b2009-11-19 19:49:17 +0100421 struct coh901318_chan *cohc = to_coh901318_chan(chan);
Linus Walleij84c84472010-03-04 14:40:30 +0100422 struct coh901318_desc *cohd;
423 struct list_head *pos;
424 unsigned long flags;
425 u32 left = 0;
426 int i = 0;
Linus Walleij61f135b2009-11-19 19:49:17 +0100427
428 spin_lock_irqsave(&cohc->lock, flags);
429
Linus Walleij84c84472010-03-04 14:40:30 +0100430 /*
431 * If there are many queued jobs, we iterate and add the
432 * size of them all. We take a special look on the first
433 * job though, since it is probably active.
434 */
435 list_for_each(pos, &cohc->active) {
436 /*
437 * The first job in the list will be working on the
438 * hardware. The job can be stopped but still active,
439 * so that the transfer counter is somewhere inside
440 * the buffer.
441 */
442 cohd = list_entry(pos, struct coh901318_desc, node);
443
444 if (i == 0) {
445 struct coh901318_lli *lli;
446 dma_addr_t ladd;
447
448 /* Read current transfer count value */
449 left = readl(cohc->base->virtbase +
450 COH901318_CX_CTRL +
451 COH901318_CX_CTRL_SPACING * cohc->id) &
452 COH901318_CX_CTRL_TC_VALUE_MASK;
453
454 /* See if the transfer is linked... */
455 ladd = readl(cohc->base->virtbase +
456 COH901318_CX_LNK_ADDR +
457 COH901318_CX_LNK_ADDR_SPACING *
458 cohc->id) &
459 ~COH901318_CX_LNK_LINK_IMMEDIATE;
460 /* Single transaction */
461 if (!ladd)
462 continue;
463
464 /*
465 * Linked transaction, follow the lli, find the
466 * currently processing lli, and proceed to the next
467 */
468 lli = cohd->lli;
469 while (lli && lli->link_addr != ladd)
470 lli = lli->virt_link_addr;
471
472 if (lli)
473 lli = lli->virt_link_addr;
474
475 /*
476 * Follow remaining lli links around to count the total
477 * number of bytes left
478 */
479 left += coh901318_get_bytes_in_lli(lli);
480 } else {
481 left += coh901318_get_bytes_in_lli(cohd->lli);
482 }
483 i++;
484 }
485
486 /* Also count bytes in the queued jobs */
487 list_for_each(pos, &cohc->queue) {
488 cohd = list_entry(pos, struct coh901318_desc, node);
489 left += coh901318_get_bytes_in_lli(cohd->lli);
490 }
Linus Walleij61f135b2009-11-19 19:49:17 +0100491
492 spin_unlock_irqrestore(&cohc->lock, flags);
493
Linus Walleij84c84472010-03-04 14:40:30 +0100494 return left;
Linus Walleij61f135b2009-11-19 19:49:17 +0100495}
Linus Walleij61f135b2009-11-19 19:49:17 +0100496
Linus Walleijc3635c72010-03-26 16:44:01 -0700497/*
498 * Pauses a transfer without losing data. Enables power save.
499 * Use this function in conjunction with coh901318_resume.
500 */
501static void coh901318_pause(struct dma_chan *chan)
Linus Walleij61f135b2009-11-19 19:49:17 +0100502{
503 u32 val;
504 unsigned long flags;
505 struct coh901318_chan *cohc = to_coh901318_chan(chan);
506 int channel = cohc->id;
507 void __iomem *virtbase = cohc->base->virtbase;
508
509 spin_lock_irqsave(&cohc->lock, flags);
510
511 /* Disable channel in HW */
512 val = readl(virtbase + COH901318_CX_CFG +
513 COH901318_CX_CFG_SPACING * channel);
514
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300515 /* Stopping infinite transfer */
Linus Walleij61f135b2009-11-19 19:49:17 +0100516 if ((val & COH901318_CX_CTRL_TC_ENABLE) == 0 &&
517 (val & COH901318_CX_CFG_CH_ENABLE))
518 cohc->stopped = 1;
519
520
521 val &= ~COH901318_CX_CFG_CH_ENABLE;
522 /* Enable twice, HW bug work around */
523 writel(val, virtbase + COH901318_CX_CFG +
524 COH901318_CX_CFG_SPACING * channel);
525 writel(val, virtbase + COH901318_CX_CFG +
526 COH901318_CX_CFG_SPACING * channel);
527
528 /* Spin-wait for it to actually go inactive */
529 while (readl(virtbase + COH901318_CX_STAT+COH901318_CX_STAT_SPACING *
530 channel) & COH901318_CX_STAT_ACTIVE)
531 cpu_relax();
532
533 /* Check if we stopped an active job */
534 if ((readl(virtbase + COH901318_CX_CTRL+COH901318_CX_CTRL_SPACING *
535 channel) & COH901318_CX_CTRL_TC_VALUE_MASK) > 0)
536 cohc->stopped = 1;
537
538 enable_powersave(cohc);
539
540 spin_unlock_irqrestore(&cohc->lock, flags);
541}
Linus Walleij61f135b2009-11-19 19:49:17 +0100542
Linus Walleijc3635c72010-03-26 16:44:01 -0700543/* Resumes a transfer that has been stopped via 300_dma_stop(..).
Linus Walleij61f135b2009-11-19 19:49:17 +0100544 Power save is handled.
545*/
Linus Walleijc3635c72010-03-26 16:44:01 -0700546static void coh901318_resume(struct dma_chan *chan)
Linus Walleij61f135b2009-11-19 19:49:17 +0100547{
548 u32 val;
549 unsigned long flags;
550 struct coh901318_chan *cohc = to_coh901318_chan(chan);
551 int channel = cohc->id;
552
553 spin_lock_irqsave(&cohc->lock, flags);
554
555 disable_powersave(cohc);
556
557 if (cohc->stopped) {
558 /* Enable channel in HW */
559 val = readl(cohc->base->virtbase + COH901318_CX_CFG +
560 COH901318_CX_CFG_SPACING * channel);
561
562 val |= COH901318_CX_CFG_CH_ENABLE;
563
564 writel(val, cohc->base->virtbase + COH901318_CX_CFG +
565 COH901318_CX_CFG_SPACING*channel);
566
567 cohc->stopped = 0;
568 }
569
570 spin_unlock_irqrestore(&cohc->lock, flags);
571}
Linus Walleij61f135b2009-11-19 19:49:17 +0100572
573bool coh901318_filter_id(struct dma_chan *chan, void *chan_id)
574{
575 unsigned int ch_nr = (unsigned int) chan_id;
576
577 if (ch_nr == to_coh901318_chan(chan)->id)
578 return true;
579
580 return false;
581}
582EXPORT_SYMBOL(coh901318_filter_id);
583
584/*
585 * DMA channel allocation
586 */
587static int coh901318_config(struct coh901318_chan *cohc,
588 struct coh901318_params *param)
589{
590 unsigned long flags;
591 const struct coh901318_params *p;
592 int channel = cohc->id;
593 void __iomem *virtbase = cohc->base->virtbase;
594
595 spin_lock_irqsave(&cohc->lock, flags);
596
597 if (param)
598 p = param;
599 else
600 p = &cohc->base->platform->chan_conf[channel].param;
601
602 /* Clear any pending BE or TC interrupt */
603 if (channel < 32) {
604 writel(1 << channel, virtbase + COH901318_BE_INT_CLEAR1);
605 writel(1 << channel, virtbase + COH901318_TC_INT_CLEAR1);
606 } else {
607 writel(1 << (channel - 32), virtbase +
608 COH901318_BE_INT_CLEAR2);
609 writel(1 << (channel - 32), virtbase +
610 COH901318_TC_INT_CLEAR2);
611 }
612
613 coh901318_set_conf(cohc, p->config);
614 coh901318_set_ctrl(cohc, p->ctrl_lli_last);
615
616 spin_unlock_irqrestore(&cohc->lock, flags);
617
618 return 0;
619}
620
621/* must lock when calling this function
622 * start queued jobs, if any
623 * TODO: start all queued jobs in one go
624 *
625 * Returns descriptor if queued job is started otherwise NULL.
626 * If the queue is empty NULL is returned.
627 */
628static struct coh901318_desc *coh901318_queue_start(struct coh901318_chan *cohc)
629{
Linus Walleijcecd87d2010-03-04 14:31:47 +0100630 struct coh901318_desc *cohd;
Linus Walleij61f135b2009-11-19 19:49:17 +0100631
Linus Walleijcecd87d2010-03-04 14:31:47 +0100632 /*
633 * start queued jobs, if any
Linus Walleij61f135b2009-11-19 19:49:17 +0100634 * TODO: transmit all queued jobs in one go
635 */
Linus Walleijcecd87d2010-03-04 14:31:47 +0100636 cohd = coh901318_first_queued(cohc);
Linus Walleij61f135b2009-11-19 19:49:17 +0100637
Linus Walleijcecd87d2010-03-04 14:31:47 +0100638 if (cohd != NULL) {
Linus Walleij61f135b2009-11-19 19:49:17 +0100639 /* Remove from queue */
Linus Walleijcecd87d2010-03-04 14:31:47 +0100640 coh901318_desc_remove(cohd);
Linus Walleij61f135b2009-11-19 19:49:17 +0100641 /* initiate DMA job */
642 cohc->busy = 1;
643
Linus Walleijcecd87d2010-03-04 14:31:47 +0100644 coh901318_desc_submit(cohc, cohd);
Linus Walleij61f135b2009-11-19 19:49:17 +0100645
Linus Walleijb89243d2011-07-01 16:47:28 +0200646 /* Program the transaction head */
647 coh901318_set_conf(cohc, cohd->head_config);
648 coh901318_set_ctrl(cohc, cohd->head_ctrl);
Linus Walleijcecd87d2010-03-04 14:31:47 +0100649 coh901318_prep_linked_list(cohc, cohd->lli);
Linus Walleij61f135b2009-11-19 19:49:17 +0100650
Linus Walleijcecd87d2010-03-04 14:31:47 +0100651 /* start dma job on this channel */
Linus Walleij61f135b2009-11-19 19:49:17 +0100652 coh901318_start(cohc);
653
654 }
655
Linus Walleijcecd87d2010-03-04 14:31:47 +0100656 return cohd;
Linus Walleij61f135b2009-11-19 19:49:17 +0100657}
658
Linus Walleij848ad122010-03-02 14:17:15 -0700659/*
660 * This tasklet is called from the interrupt handler to
661 * handle each descriptor (DMA job) that is sent to a channel.
662 */
Linus Walleij61f135b2009-11-19 19:49:17 +0100663static void dma_tasklet(unsigned long data)
664{
665 struct coh901318_chan *cohc = (struct coh901318_chan *) data;
666 struct coh901318_desc *cohd_fin;
667 unsigned long flags;
668 dma_async_tx_callback callback;
669 void *callback_param;
670
Linus Walleij848ad122010-03-02 14:17:15 -0700671 dev_vdbg(COHC_2_DEV(cohc), "[%s] chan_id %d"
672 " nbr_active_done %ld\n", __func__,
673 cohc->id, cohc->nbr_active_done);
674
Linus Walleij61f135b2009-11-19 19:49:17 +0100675 spin_lock_irqsave(&cohc->lock, flags);
676
Linus Walleij848ad122010-03-02 14:17:15 -0700677 /* get first active descriptor entry from list */
Linus Walleij61f135b2009-11-19 19:49:17 +0100678 cohd_fin = coh901318_first_active_get(cohc);
679
Linus Walleij61f135b2009-11-19 19:49:17 +0100680 if (cohd_fin == NULL)
681 goto err;
682
Linus Walleij0b588282010-03-02 14:17:44 -0700683 /* locate callback to client */
Linus Walleij61f135b2009-11-19 19:49:17 +0100684 callback = cohd_fin->desc.callback;
685 callback_param = cohd_fin->desc.callback_param;
686
Linus Walleij0b588282010-03-02 14:17:44 -0700687 /* sign this job as completed on the channel */
Russell King - ARM Linuxf7fbce02012-03-06 22:35:07 +0000688 dma_cookie_complete(&cohd_fin->desc);
Linus Walleij61f135b2009-11-19 19:49:17 +0100689
Linus Walleij0b588282010-03-02 14:17:44 -0700690 /* release the lli allocation and remove the descriptor */
Linus Walleijcecd87d2010-03-04 14:31:47 +0100691 coh901318_lli_free(&cohc->base->pool, &cohd_fin->lli);
Linus Walleij0b588282010-03-02 14:17:44 -0700692
693 /* return desc to free-list */
694 coh901318_desc_remove(cohd_fin);
695 coh901318_desc_free(cohc, cohd_fin);
696
697 spin_unlock_irqrestore(&cohc->lock, flags);
698
699 /* Call the callback when we're done */
700 if (callback)
701 callback(callback_param);
702
703 spin_lock_irqsave(&cohc->lock, flags);
Linus Walleij61f135b2009-11-19 19:49:17 +0100704
Linus Walleij848ad122010-03-02 14:17:15 -0700705 /*
706 * If another interrupt fired while the tasklet was scheduling,
707 * we don't get called twice, so we have this number of active
708 * counter that keep track of the number of IRQs expected to
709 * be handled for this channel. If there happen to be more than
710 * one IRQ to be ack:ed, we simply schedule this tasklet again.
711 */
Linus Walleij0b588282010-03-02 14:17:44 -0700712 cohc->nbr_active_done--;
Linus Walleij61f135b2009-11-19 19:49:17 +0100713 if (cohc->nbr_active_done) {
Linus Walleij848ad122010-03-02 14:17:15 -0700714 dev_dbg(COHC_2_DEV(cohc), "scheduling tasklet again, new IRQs "
715 "came in while we were scheduling this tasklet\n");
Linus Walleij61f135b2009-11-19 19:49:17 +0100716 if (cohc_chan_conf(cohc)->priority_high)
717 tasklet_hi_schedule(&cohc->tasklet);
718 else
719 tasklet_schedule(&cohc->tasklet);
720 }
Linus Walleij61f135b2009-11-19 19:49:17 +0100721
Linus Walleij0b588282010-03-02 14:17:44 -0700722 spin_unlock_irqrestore(&cohc->lock, flags);
Linus Walleij61f135b2009-11-19 19:49:17 +0100723
724 return;
725
726 err:
727 spin_unlock_irqrestore(&cohc->lock, flags);
728 dev_err(COHC_2_DEV(cohc), "[%s] No active dma desc\n", __func__);
729}
730
731
732/* called from interrupt context */
733static void dma_tc_handle(struct coh901318_chan *cohc)
734{
Linus Walleijcecd87d2010-03-04 14:31:47 +0100735 /*
736 * If the channel is not allocated, then we shouldn't have
737 * any TC interrupts on it.
738 */
739 if (!cohc->allocated) {
740 dev_err(COHC_2_DEV(cohc), "spurious interrupt from "
741 "unallocated channel\n");
Linus Walleij61f135b2009-11-19 19:49:17 +0100742 return;
Linus Walleijcecd87d2010-03-04 14:31:47 +0100743 }
Linus Walleij61f135b2009-11-19 19:49:17 +0100744
Linus Walleij0b588282010-03-02 14:17:44 -0700745 spin_lock(&cohc->lock);
Linus Walleij61f135b2009-11-19 19:49:17 +0100746
Linus Walleijcecd87d2010-03-04 14:31:47 +0100747 /*
748 * When we reach this point, at least one queue item
749 * should have been moved over from cohc->queue to
750 * cohc->active and run to completion, that is why we're
751 * getting a terminal count interrupt is it not?
752 * If you get this BUG() the most probable cause is that
753 * the individual nodes in the lli chain have IRQ enabled,
754 * so check your platform config for lli chain ctrl.
755 */
756 BUG_ON(list_empty(&cohc->active));
757
Linus Walleij61f135b2009-11-19 19:49:17 +0100758 cohc->nbr_active_done++;
759
Linus Walleijcecd87d2010-03-04 14:31:47 +0100760 /*
761 * This attempt to take a job from cohc->queue, put it
762 * into cohc->active and start it.
763 */
Linus Walleij0b588282010-03-02 14:17:44 -0700764 if (coh901318_queue_start(cohc) == NULL)
Linus Walleij61f135b2009-11-19 19:49:17 +0100765 cohc->busy = 0;
766
Linus Walleij0b588282010-03-02 14:17:44 -0700767 spin_unlock(&cohc->lock);
768
Linus Walleijcecd87d2010-03-04 14:31:47 +0100769 /*
770 * This tasklet will remove items from cohc->active
771 * and thus terminates them.
772 */
Linus Walleij61f135b2009-11-19 19:49:17 +0100773 if (cohc_chan_conf(cohc)->priority_high)
774 tasklet_hi_schedule(&cohc->tasklet);
775 else
776 tasklet_schedule(&cohc->tasklet);
777}
778
779
780static irqreturn_t dma_irq_handler(int irq, void *dev_id)
781{
782 u32 status1;
783 u32 status2;
784 int i;
785 int ch;
786 struct coh901318_base *base = dev_id;
787 struct coh901318_chan *cohc;
788 void __iomem *virtbase = base->virtbase;
789
790 status1 = readl(virtbase + COH901318_INT_STATUS1);
791 status2 = readl(virtbase + COH901318_INT_STATUS2);
792
793 if (unlikely(status1 == 0 && status2 == 0)) {
794 dev_warn(base->dev, "spurious DMA IRQ from no channel!\n");
795 return IRQ_HANDLED;
796 }
797
798 /* TODO: consider handle IRQ in tasklet here to
799 * minimize interrupt latency */
800
801 /* Check the first 32 DMA channels for IRQ */
802 while (status1) {
803 /* Find first bit set, return as a number. */
804 i = ffs(status1) - 1;
805 ch = i;
806
807 cohc = &base->chans[ch];
808 spin_lock(&cohc->lock);
809
810 /* Mask off this bit */
811 status1 &= ~(1 << i);
812 /* Check the individual channel bits */
813 if (test_bit(i, virtbase + COH901318_BE_INT_STATUS1)) {
814 dev_crit(COHC_2_DEV(cohc),
815 "DMA bus error on channel %d!\n", ch);
816 BUG_ON(1);
817 /* Clear BE interrupt */
818 __set_bit(i, virtbase + COH901318_BE_INT_CLEAR1);
819 } else {
820 /* Caused by TC, really? */
821 if (unlikely(!test_bit(i, virtbase +
822 COH901318_TC_INT_STATUS1))) {
823 dev_warn(COHC_2_DEV(cohc),
824 "ignoring interrupt not caused by terminal count on channel %d\n", ch);
825 /* Clear TC interrupt */
826 BUG_ON(1);
827 __set_bit(i, virtbase + COH901318_TC_INT_CLEAR1);
828 } else {
829 /* Enable powersave if transfer has finished */
830 if (!(readl(virtbase + COH901318_CX_STAT +
831 COH901318_CX_STAT_SPACING*ch) &
832 COH901318_CX_STAT_ENABLED)) {
833 enable_powersave(cohc);
834 }
835
836 /* Must clear TC interrupt before calling
837 * dma_tc_handle
Justin P. Mattockbc0b44c2011-01-28 11:48:18 -0800838 * in case tc_handle initiate a new dma job
Linus Walleij61f135b2009-11-19 19:49:17 +0100839 */
840 __set_bit(i, virtbase + COH901318_TC_INT_CLEAR1);
841
842 dma_tc_handle(cohc);
843 }
844 }
845 spin_unlock(&cohc->lock);
846 }
847
848 /* Check the remaining 32 DMA channels for IRQ */
849 while (status2) {
850 /* Find first bit set, return as a number. */
851 i = ffs(status2) - 1;
852 ch = i + 32;
853 cohc = &base->chans[ch];
854 spin_lock(&cohc->lock);
855
856 /* Mask off this bit */
857 status2 &= ~(1 << i);
858 /* Check the individual channel bits */
859 if (test_bit(i, virtbase + COH901318_BE_INT_STATUS2)) {
860 dev_crit(COHC_2_DEV(cohc),
861 "DMA bus error on channel %d!\n", ch);
862 /* Clear BE interrupt */
863 BUG_ON(1);
864 __set_bit(i, virtbase + COH901318_BE_INT_CLEAR2);
865 } else {
866 /* Caused by TC, really? */
867 if (unlikely(!test_bit(i, virtbase +
868 COH901318_TC_INT_STATUS2))) {
869 dev_warn(COHC_2_DEV(cohc),
870 "ignoring interrupt not caused by terminal count on channel %d\n", ch);
871 /* Clear TC interrupt */
872 __set_bit(i, virtbase + COH901318_TC_INT_CLEAR2);
873 BUG_ON(1);
874 } else {
875 /* Enable powersave if transfer has finished */
876 if (!(readl(virtbase + COH901318_CX_STAT +
877 COH901318_CX_STAT_SPACING*ch) &
878 COH901318_CX_STAT_ENABLED)) {
879 enable_powersave(cohc);
880 }
881 /* Must clear TC interrupt before calling
882 * dma_tc_handle
Justin P. Mattockbc0b44c2011-01-28 11:48:18 -0800883 * in case tc_handle initiate a new dma job
Linus Walleij61f135b2009-11-19 19:49:17 +0100884 */
885 __set_bit(i, virtbase + COH901318_TC_INT_CLEAR2);
886
887 dma_tc_handle(cohc);
888 }
889 }
890 spin_unlock(&cohc->lock);
891 }
892
893 return IRQ_HANDLED;
894}
895
896static int coh901318_alloc_chan_resources(struct dma_chan *chan)
897{
898 struct coh901318_chan *cohc = to_coh901318_chan(chan);
Linus Walleij84c84472010-03-04 14:40:30 +0100899 unsigned long flags;
Linus Walleij61f135b2009-11-19 19:49:17 +0100900
901 dev_vdbg(COHC_2_DEV(cohc), "[%s] DMA channel %d\n",
902 __func__, cohc->id);
903
904 if (chan->client_count > 1)
905 return -EBUSY;
906
Linus Walleij84c84472010-03-04 14:40:30 +0100907 spin_lock_irqsave(&cohc->lock, flags);
908
Linus Walleij61f135b2009-11-19 19:49:17 +0100909 coh901318_config(cohc, NULL);
910
911 cohc->allocated = 1;
Russell King - ARM Linuxd3ee98cdc2012-03-06 22:35:47 +0000912 dma_cookie_init(chan);
Linus Walleij61f135b2009-11-19 19:49:17 +0100913
Linus Walleij84c84472010-03-04 14:40:30 +0100914 spin_unlock_irqrestore(&cohc->lock, flags);
915
Linus Walleij61f135b2009-11-19 19:49:17 +0100916 return 1;
917}
918
919static void
920coh901318_free_chan_resources(struct dma_chan *chan)
921{
922 struct coh901318_chan *cohc = to_coh901318_chan(chan);
923 int channel = cohc->id;
924 unsigned long flags;
925
926 spin_lock_irqsave(&cohc->lock, flags);
927
928 /* Disable HW */
929 writel(0x00000000U, cohc->base->virtbase + COH901318_CX_CFG +
930 COH901318_CX_CFG_SPACING*channel);
931 writel(0x00000000U, cohc->base->virtbase + COH901318_CX_CTRL +
932 COH901318_CX_CTRL_SPACING*channel);
933
934 cohc->allocated = 0;
935
936 spin_unlock_irqrestore(&cohc->lock, flags);
937
Linus Walleij05827632010-05-17 16:30:42 -0700938 chan->device->device_control(chan, DMA_TERMINATE_ALL, 0);
Linus Walleij61f135b2009-11-19 19:49:17 +0100939}
940
941
942static dma_cookie_t
943coh901318_tx_submit(struct dma_async_tx_descriptor *tx)
944{
945 struct coh901318_desc *cohd = container_of(tx, struct coh901318_desc,
946 desc);
947 struct coh901318_chan *cohc = to_coh901318_chan(tx->chan);
948 unsigned long flags;
Russell King - ARM Linux884485e2012-03-06 22:34:46 +0000949 dma_cookie_t cookie;
Linus Walleij61f135b2009-11-19 19:49:17 +0100950
951 spin_lock_irqsave(&cohc->lock, flags);
Russell King - ARM Linux884485e2012-03-06 22:34:46 +0000952 cookie = dma_cookie_assign(tx);
Linus Walleij61f135b2009-11-19 19:49:17 +0100953
954 coh901318_desc_queue(cohc, cohd);
955
956 spin_unlock_irqrestore(&cohc->lock, flags);
957
Russell King - ARM Linux884485e2012-03-06 22:34:46 +0000958 return cookie;
Linus Walleij61f135b2009-11-19 19:49:17 +0100959}
960
961static struct dma_async_tx_descriptor *
962coh901318_prep_memcpy(struct dma_chan *chan, dma_addr_t dest, dma_addr_t src,
963 size_t size, unsigned long flags)
964{
Linus Walleijcecd87d2010-03-04 14:31:47 +0100965 struct coh901318_lli *lli;
Linus Walleij61f135b2009-11-19 19:49:17 +0100966 struct coh901318_desc *cohd;
967 unsigned long flg;
968 struct coh901318_chan *cohc = to_coh901318_chan(chan);
969 int lli_len;
970 u32 ctrl_last = cohc_chan_param(cohc)->ctrl_lli_last;
Linus Walleijb87108a2010-03-02 14:17:20 -0700971 int ret;
Linus Walleij61f135b2009-11-19 19:49:17 +0100972
973 spin_lock_irqsave(&cohc->lock, flg);
974
975 dev_vdbg(COHC_2_DEV(cohc),
976 "[%s] channel %d src 0x%x dest 0x%x size %d\n",
977 __func__, cohc->id, src, dest, size);
978
979 if (flags & DMA_PREP_INTERRUPT)
980 /* Trigger interrupt after last lli */
981 ctrl_last |= COH901318_CX_CTRL_TC_IRQ_ENABLE;
982
983 lli_len = size >> MAX_DMA_PACKET_SIZE_SHIFT;
984 if ((lli_len << MAX_DMA_PACKET_SIZE_SHIFT) < size)
985 lli_len++;
986
Linus Walleijcecd87d2010-03-04 14:31:47 +0100987 lli = coh901318_lli_alloc(&cohc->base->pool, lli_len);
Linus Walleij61f135b2009-11-19 19:49:17 +0100988
Linus Walleijcecd87d2010-03-04 14:31:47 +0100989 if (lli == NULL)
Linus Walleij61f135b2009-11-19 19:49:17 +0100990 goto err;
991
Linus Walleijb87108a2010-03-02 14:17:20 -0700992 ret = coh901318_lli_fill_memcpy(
Linus Walleijcecd87d2010-03-04 14:31:47 +0100993 &cohc->base->pool, lli, src, size, dest,
Linus Walleijb87108a2010-03-02 14:17:20 -0700994 cohc_chan_param(cohc)->ctrl_lli_chained,
995 ctrl_last);
996 if (ret)
997 goto err;
Linus Walleij61f135b2009-11-19 19:49:17 +0100998
Linus Walleijcecd87d2010-03-04 14:31:47 +0100999 COH_DBG(coh901318_list_print(cohc, lli));
Linus Walleij61f135b2009-11-19 19:49:17 +01001000
Linus Walleijb87108a2010-03-02 14:17:20 -07001001 /* Pick a descriptor to handle this transfer */
1002 cohd = coh901318_desc_get(cohc);
Linus Walleijcecd87d2010-03-04 14:31:47 +01001003 cohd->lli = lli;
Linus Walleijb87108a2010-03-02 14:17:20 -07001004 cohd->flags = flags;
Linus Walleij61f135b2009-11-19 19:49:17 +01001005 cohd->desc.tx_submit = coh901318_tx_submit;
1006
1007 spin_unlock_irqrestore(&cohc->lock, flg);
1008
1009 return &cohd->desc;
1010 err:
1011 spin_unlock_irqrestore(&cohc->lock, flg);
1012 return NULL;
1013}
1014
1015static struct dma_async_tx_descriptor *
1016coh901318_prep_slave_sg(struct dma_chan *chan, struct scatterlist *sgl,
Vinod Kouldb8196d2011-10-13 22:34:23 +05301017 unsigned int sg_len, enum dma_transfer_direction direction,
Alexandre Bounine185ecb52012-03-08 15:35:13 -05001018 unsigned long flags, void *context)
Linus Walleij61f135b2009-11-19 19:49:17 +01001019{
1020 struct coh901318_chan *cohc = to_coh901318_chan(chan);
Linus Walleijcecd87d2010-03-04 14:31:47 +01001021 struct coh901318_lli *lli;
Linus Walleij61f135b2009-11-19 19:49:17 +01001022 struct coh901318_desc *cohd;
Linus Walleij516fd432010-03-02 20:12:46 +01001023 const struct coh901318_params *params;
Linus Walleij61f135b2009-11-19 19:49:17 +01001024 struct scatterlist *sg;
1025 int len = 0;
1026 int size;
1027 int i;
1028 u32 ctrl_chained = cohc_chan_param(cohc)->ctrl_lli_chained;
1029 u32 ctrl = cohc_chan_param(cohc)->ctrl_lli;
1030 u32 ctrl_last = cohc_chan_param(cohc)->ctrl_lli_last;
Linus Walleij516fd432010-03-02 20:12:46 +01001031 u32 config;
Linus Walleij61f135b2009-11-19 19:49:17 +01001032 unsigned long flg;
Linus Walleij0b588282010-03-02 14:17:44 -07001033 int ret;
Linus Walleij61f135b2009-11-19 19:49:17 +01001034
1035 if (!sgl)
1036 goto out;
Lars-Peter Clausenfdaf9c42012-04-25 20:50:52 +02001037 if (sg_dma_len(sgl) == 0)
Linus Walleij61f135b2009-11-19 19:49:17 +01001038 goto out;
1039
1040 spin_lock_irqsave(&cohc->lock, flg);
1041
1042 dev_vdbg(COHC_2_DEV(cohc), "[%s] sg_len %d dir %d\n",
1043 __func__, sg_len, direction);
1044
1045 if (flags & DMA_PREP_INTERRUPT)
1046 /* Trigger interrupt after last lli */
1047 ctrl_last |= COH901318_CX_CTRL_TC_IRQ_ENABLE;
1048
Linus Walleij516fd432010-03-02 20:12:46 +01001049 params = cohc_chan_param(cohc);
1050 config = params->config;
Linus Walleij128f9042010-08-04 13:37:53 +02001051 /*
1052 * Add runtime-specific control on top, make
1053 * sure the bits you set per peripheral channel are
1054 * cleared in the default config from the platform.
1055 */
1056 ctrl_chained |= cohc->runtime_ctrl;
1057 ctrl_last |= cohc->runtime_ctrl;
1058 ctrl |= cohc->runtime_ctrl;
Linus Walleij516fd432010-03-02 20:12:46 +01001059
Vinod Kouldb8196d2011-10-13 22:34:23 +05301060 if (direction == DMA_MEM_TO_DEV) {
Linus Walleij61f135b2009-11-19 19:49:17 +01001061 u32 tx_flags = COH901318_CX_CTRL_PRDD_SOURCE |
1062 COH901318_CX_CTRL_SRC_ADDR_INC_ENABLE;
1063
Linus Walleij516fd432010-03-02 20:12:46 +01001064 config |= COH901318_CX_CFG_RM_MEMORY_TO_PRIMARY;
Linus Walleij61f135b2009-11-19 19:49:17 +01001065 ctrl_chained |= tx_flags;
1066 ctrl_last |= tx_flags;
1067 ctrl |= tx_flags;
Vinod Kouldb8196d2011-10-13 22:34:23 +05301068 } else if (direction == DMA_DEV_TO_MEM) {
Linus Walleij61f135b2009-11-19 19:49:17 +01001069 u32 rx_flags = COH901318_CX_CTRL_PRDD_DEST |
1070 COH901318_CX_CTRL_DST_ADDR_INC_ENABLE;
1071
Linus Walleij516fd432010-03-02 20:12:46 +01001072 config |= COH901318_CX_CFG_RM_PRIMARY_TO_MEMORY;
Linus Walleij61f135b2009-11-19 19:49:17 +01001073 ctrl_chained |= rx_flags;
1074 ctrl_last |= rx_flags;
1075 ctrl |= rx_flags;
1076 } else
1077 goto err_direction;
1078
Linus Walleij61f135b2009-11-19 19:49:17 +01001079 /* The dma only supports transmitting packages up to
1080 * MAX_DMA_PACKET_SIZE. Calculate to total number of
1081 * dma elemts required to send the entire sg list
1082 */
1083 for_each_sg(sgl, sg, sg_len, i) {
1084 unsigned int factor;
1085 size = sg_dma_len(sg);
1086
1087 if (size <= MAX_DMA_PACKET_SIZE) {
1088 len++;
1089 continue;
1090 }
1091
1092 factor = size >> MAX_DMA_PACKET_SIZE_SHIFT;
1093 if ((factor << MAX_DMA_PACKET_SIZE_SHIFT) < size)
1094 factor++;
1095
1096 len += factor;
1097 }
1098
Linus Walleij848ad122010-03-02 14:17:15 -07001099 pr_debug("Allocate %d lli:s for this transfer\n", len);
Linus Walleijcecd87d2010-03-04 14:31:47 +01001100 lli = coh901318_lli_alloc(&cohc->base->pool, len);
Linus Walleij61f135b2009-11-19 19:49:17 +01001101
Linus Walleijcecd87d2010-03-04 14:31:47 +01001102 if (lli == NULL)
Linus Walleij61f135b2009-11-19 19:49:17 +01001103 goto err_dma_alloc;
1104
Linus Walleijcecd87d2010-03-04 14:31:47 +01001105 /* initiate allocated lli list */
1106 ret = coh901318_lli_fill_sg(&cohc->base->pool, lli, sgl, sg_len,
Linus Walleij0b588282010-03-02 14:17:44 -07001107 cohc_dev_addr(cohc),
1108 ctrl_chained,
1109 ctrl,
1110 ctrl_last,
1111 direction, COH901318_CX_CTRL_TC_IRQ_ENABLE);
1112 if (ret)
1113 goto err_lli_fill;
Linus Walleij61f135b2009-11-19 19:49:17 +01001114
Linus Walleij128f9042010-08-04 13:37:53 +02001115
Linus Walleijcecd87d2010-03-04 14:31:47 +01001116 COH_DBG(coh901318_list_print(cohc, lli));
Linus Walleij61f135b2009-11-19 19:49:17 +01001117
Linus Walleijb87108a2010-03-02 14:17:20 -07001118 /* Pick a descriptor to handle this transfer */
1119 cohd = coh901318_desc_get(cohc);
Linus Walleijb89243d2011-07-01 16:47:28 +02001120 cohd->head_config = config;
1121 /*
1122 * Set the default head ctrl for the channel to the one from the
1123 * lli, things may have changed due to odd buffer alignment
1124 * etc.
1125 */
1126 cohd->head_ctrl = lli->control;
Linus Walleijb87108a2010-03-02 14:17:20 -07001127 cohd->dir = direction;
1128 cohd->flags = flags;
1129 cohd->desc.tx_submit = coh901318_tx_submit;
Linus Walleijcecd87d2010-03-04 14:31:47 +01001130 cohd->lli = lli;
Linus Walleijb87108a2010-03-02 14:17:20 -07001131
Linus Walleij61f135b2009-11-19 19:49:17 +01001132 spin_unlock_irqrestore(&cohc->lock, flg);
1133
1134 return &cohd->desc;
Linus Walleij0b588282010-03-02 14:17:44 -07001135 err_lli_fill:
Linus Walleij61f135b2009-11-19 19:49:17 +01001136 err_dma_alloc:
1137 err_direction:
Linus Walleij61f135b2009-11-19 19:49:17 +01001138 spin_unlock_irqrestore(&cohc->lock, flg);
1139 out:
1140 return NULL;
1141}
1142
1143static enum dma_status
Linus Walleij07934482010-03-26 16:50:49 -07001144coh901318_tx_status(struct dma_chan *chan, dma_cookie_t cookie,
1145 struct dma_tx_state *txstate)
Linus Walleij61f135b2009-11-19 19:49:17 +01001146{
1147 struct coh901318_chan *cohc = to_coh901318_chan(chan);
Russell King - ARM Linux96a2af42012-03-06 22:35:27 +00001148 enum dma_status ret;
Linus Walleij61f135b2009-11-19 19:49:17 +01001149
Russell King - ARM Linux96a2af42012-03-06 22:35:27 +00001150 ret = dma_cookie_status(chan, cookie, txstate);
1151 /* FIXME: should be conditional on ret != DMA_SUCCESS? */
1152 dma_set_residue(txstate, coh901318_get_bytes_left(chan));
Linus Walleij61f135b2009-11-19 19:49:17 +01001153
Linus Walleij07934482010-03-26 16:50:49 -07001154 if (ret == DMA_IN_PROGRESS && cohc->stopped)
1155 ret = DMA_PAUSED;
Linus Walleij61f135b2009-11-19 19:49:17 +01001156
1157 return ret;
1158}
1159
1160static void
1161coh901318_issue_pending(struct dma_chan *chan)
1162{
1163 struct coh901318_chan *cohc = to_coh901318_chan(chan);
1164 unsigned long flags;
1165
1166 spin_lock_irqsave(&cohc->lock, flags);
1167
Linus Walleijcecd87d2010-03-04 14:31:47 +01001168 /*
1169 * Busy means that pending jobs are already being processed,
1170 * and then there is no point in starting the queue: the
1171 * terminal count interrupt on the channel will take the next
1172 * job on the queue and execute it anyway.
1173 */
Linus Walleij61f135b2009-11-19 19:49:17 +01001174 if (!cohc->busy)
1175 coh901318_queue_start(cohc);
1176
1177 spin_unlock_irqrestore(&cohc->lock, flags);
1178}
1179
Linus Walleij128f9042010-08-04 13:37:53 +02001180/*
1181 * Here we wrap in the runtime dma control interface
1182 */
1183struct burst_table {
1184 int burst_8bit;
1185 int burst_16bit;
1186 int burst_32bit;
1187 u32 reg;
1188};
1189
1190static const struct burst_table burst_sizes[] = {
1191 {
1192 .burst_8bit = 64,
1193 .burst_16bit = 32,
1194 .burst_32bit = 16,
1195 .reg = COH901318_CX_CTRL_BURST_COUNT_64_BYTES,
1196 },
1197 {
1198 .burst_8bit = 48,
1199 .burst_16bit = 24,
1200 .burst_32bit = 12,
1201 .reg = COH901318_CX_CTRL_BURST_COUNT_48_BYTES,
1202 },
1203 {
1204 .burst_8bit = 32,
1205 .burst_16bit = 16,
1206 .burst_32bit = 8,
1207 .reg = COH901318_CX_CTRL_BURST_COUNT_32_BYTES,
1208 },
1209 {
1210 .burst_8bit = 16,
1211 .burst_16bit = 8,
1212 .burst_32bit = 4,
1213 .reg = COH901318_CX_CTRL_BURST_COUNT_16_BYTES,
1214 },
1215 {
1216 .burst_8bit = 8,
1217 .burst_16bit = 4,
1218 .burst_32bit = 2,
1219 .reg = COH901318_CX_CTRL_BURST_COUNT_8_BYTES,
1220 },
1221 {
1222 .burst_8bit = 4,
1223 .burst_16bit = 2,
1224 .burst_32bit = 1,
1225 .reg = COH901318_CX_CTRL_BURST_COUNT_4_BYTES,
1226 },
1227 {
1228 .burst_8bit = 2,
1229 .burst_16bit = 1,
1230 .burst_32bit = 0,
1231 .reg = COH901318_CX_CTRL_BURST_COUNT_2_BYTES,
1232 },
1233 {
1234 .burst_8bit = 1,
1235 .burst_16bit = 0,
1236 .burst_32bit = 0,
1237 .reg = COH901318_CX_CTRL_BURST_COUNT_1_BYTE,
1238 },
1239};
1240
1241static void coh901318_dma_set_runtimeconfig(struct dma_chan *chan,
1242 struct dma_slave_config *config)
1243{
1244 struct coh901318_chan *cohc = to_coh901318_chan(chan);
1245 dma_addr_t addr;
1246 enum dma_slave_buswidth addr_width;
1247 u32 maxburst;
1248 u32 runtime_ctrl = 0;
1249 int i = 0;
1250
1251 /* We only support mem to per or per to mem transfers */
Vinod Kouldb8196d2011-10-13 22:34:23 +05301252 if (config->direction == DMA_DEV_TO_MEM) {
Linus Walleij128f9042010-08-04 13:37:53 +02001253 addr = config->src_addr;
1254 addr_width = config->src_addr_width;
1255 maxburst = config->src_maxburst;
Vinod Kouldb8196d2011-10-13 22:34:23 +05301256 } else if (config->direction == DMA_MEM_TO_DEV) {
Linus Walleij128f9042010-08-04 13:37:53 +02001257 addr = config->dst_addr;
1258 addr_width = config->dst_addr_width;
1259 maxburst = config->dst_maxburst;
1260 } else {
1261 dev_err(COHC_2_DEV(cohc), "illegal channel mode\n");
1262 return;
1263 }
1264
1265 dev_dbg(COHC_2_DEV(cohc), "configure channel for %d byte transfers\n",
1266 addr_width);
1267 switch (addr_width) {
1268 case DMA_SLAVE_BUSWIDTH_1_BYTE:
1269 runtime_ctrl |=
1270 COH901318_CX_CTRL_SRC_BUS_SIZE_8_BITS |
1271 COH901318_CX_CTRL_DST_BUS_SIZE_8_BITS;
1272
1273 while (i < ARRAY_SIZE(burst_sizes)) {
1274 if (burst_sizes[i].burst_8bit <= maxburst)
1275 break;
1276 i++;
1277 }
1278
1279 break;
1280 case DMA_SLAVE_BUSWIDTH_2_BYTES:
1281 runtime_ctrl |=
1282 COH901318_CX_CTRL_SRC_BUS_SIZE_16_BITS |
1283 COH901318_CX_CTRL_DST_BUS_SIZE_16_BITS;
1284
1285 while (i < ARRAY_SIZE(burst_sizes)) {
1286 if (burst_sizes[i].burst_16bit <= maxburst)
1287 break;
1288 i++;
1289 }
1290
1291 break;
1292 case DMA_SLAVE_BUSWIDTH_4_BYTES:
1293 /* Direction doesn't matter here, it's 32/32 bits */
1294 runtime_ctrl |=
1295 COH901318_CX_CTRL_SRC_BUS_SIZE_32_BITS |
1296 COH901318_CX_CTRL_DST_BUS_SIZE_32_BITS;
1297
1298 while (i < ARRAY_SIZE(burst_sizes)) {
1299 if (burst_sizes[i].burst_32bit <= maxburst)
1300 break;
1301 i++;
1302 }
1303
1304 break;
1305 default:
1306 dev_err(COHC_2_DEV(cohc),
1307 "bad runtimeconfig: alien address width\n");
1308 return;
1309 }
1310
1311 runtime_ctrl |= burst_sizes[i].reg;
1312 dev_dbg(COHC_2_DEV(cohc),
1313 "selected burst size %d bytes for address width %d bytes, maxburst %d\n",
1314 burst_sizes[i].burst_8bit, addr_width, maxburst);
1315
1316 cohc->runtime_addr = addr;
1317 cohc->runtime_ctrl = runtime_ctrl;
1318}
1319
Linus Walleijc3635c72010-03-26 16:44:01 -07001320static int
Linus Walleij05827632010-05-17 16:30:42 -07001321coh901318_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd,
1322 unsigned long arg)
Linus Walleij61f135b2009-11-19 19:49:17 +01001323{
1324 unsigned long flags;
1325 struct coh901318_chan *cohc = to_coh901318_chan(chan);
1326 struct coh901318_desc *cohd;
1327 void __iomem *virtbase = cohc->base->virtbase;
1328
Linus Walleij128f9042010-08-04 13:37:53 +02001329 if (cmd == DMA_SLAVE_CONFIG) {
1330 struct dma_slave_config *config =
1331 (struct dma_slave_config *) arg;
1332
1333 coh901318_dma_set_runtimeconfig(chan, config);
1334 return 0;
1335 }
1336
Linus Walleijc3635c72010-03-26 16:44:01 -07001337 if (cmd == DMA_PAUSE) {
1338 coh901318_pause(chan);
1339 return 0;
1340 }
Linus Walleij61f135b2009-11-19 19:49:17 +01001341
Linus Walleijc3635c72010-03-26 16:44:01 -07001342 if (cmd == DMA_RESUME) {
1343 coh901318_resume(chan);
1344 return 0;
1345 }
1346
1347 if (cmd != DMA_TERMINATE_ALL)
1348 return -ENXIO;
1349
1350 /* The remainder of this function terminates the transfer */
1351 coh901318_pause(chan);
Linus Walleij61f135b2009-11-19 19:49:17 +01001352 spin_lock_irqsave(&cohc->lock, flags);
1353
1354 /* Clear any pending BE or TC interrupt */
1355 if (cohc->id < 32) {
1356 writel(1 << cohc->id, virtbase + COH901318_BE_INT_CLEAR1);
1357 writel(1 << cohc->id, virtbase + COH901318_TC_INT_CLEAR1);
1358 } else {
1359 writel(1 << (cohc->id - 32), virtbase +
1360 COH901318_BE_INT_CLEAR2);
1361 writel(1 << (cohc->id - 32), virtbase +
1362 COH901318_TC_INT_CLEAR2);
1363 }
1364
1365 enable_powersave(cohc);
1366
1367 while ((cohd = coh901318_first_active_get(cohc))) {
1368 /* release the lli allocation*/
Linus Walleijcecd87d2010-03-04 14:31:47 +01001369 coh901318_lli_free(&cohc->base->pool, &cohd->lli);
Linus Walleij61f135b2009-11-19 19:49:17 +01001370
Linus Walleij61f135b2009-11-19 19:49:17 +01001371 /* return desc to free-list */
Linus Walleij848ad122010-03-02 14:17:15 -07001372 coh901318_desc_remove(cohd);
Linus Walleij61f135b2009-11-19 19:49:17 +01001373 coh901318_desc_free(cohc, cohd);
1374 }
1375
1376 while ((cohd = coh901318_first_queued(cohc))) {
1377 /* release the lli allocation*/
Linus Walleijcecd87d2010-03-04 14:31:47 +01001378 coh901318_lli_free(&cohc->base->pool, &cohd->lli);
Linus Walleij61f135b2009-11-19 19:49:17 +01001379
Linus Walleij61f135b2009-11-19 19:49:17 +01001380 /* return desc to free-list */
Linus Walleij848ad122010-03-02 14:17:15 -07001381 coh901318_desc_remove(cohd);
Linus Walleij61f135b2009-11-19 19:49:17 +01001382 coh901318_desc_free(cohc, cohd);
1383 }
1384
1385
1386 cohc->nbr_active_done = 0;
1387 cohc->busy = 0;
Linus Walleij61f135b2009-11-19 19:49:17 +01001388
1389 spin_unlock_irqrestore(&cohc->lock, flags);
Linus Walleijc3635c72010-03-26 16:44:01 -07001390
1391 return 0;
Linus Walleij61f135b2009-11-19 19:49:17 +01001392}
Linus Walleij128f9042010-08-04 13:37:53 +02001393
Linus Walleij61f135b2009-11-19 19:49:17 +01001394void coh901318_base_init(struct dma_device *dma, const int *pick_chans,
1395 struct coh901318_base *base)
1396{
1397 int chans_i;
1398 int i = 0;
1399 struct coh901318_chan *cohc;
1400
1401 INIT_LIST_HEAD(&dma->channels);
1402
1403 for (chans_i = 0; pick_chans[chans_i] != -1; chans_i += 2) {
1404 for (i = pick_chans[chans_i]; i <= pick_chans[chans_i+1]; i++) {
1405 cohc = &base->chans[i];
1406
1407 cohc->base = base;
1408 cohc->chan.device = dma;
1409 cohc->id = i;
1410
1411 /* TODO: do we really need this lock if only one
1412 * client is connected to each channel?
1413 */
1414
1415 spin_lock_init(&cohc->lock);
1416
Linus Walleij61f135b2009-11-19 19:49:17 +01001417 cohc->nbr_active_done = 0;
1418 cohc->busy = 0;
1419 INIT_LIST_HEAD(&cohc->free);
1420 INIT_LIST_HEAD(&cohc->active);
1421 INIT_LIST_HEAD(&cohc->queue);
1422
1423 tasklet_init(&cohc->tasklet, dma_tasklet,
1424 (unsigned long) cohc);
1425
1426 list_add_tail(&cohc->chan.device_node,
1427 &dma->channels);
1428 }
1429 }
1430}
1431
1432static int __init coh901318_probe(struct platform_device *pdev)
1433{
1434 int err = 0;
1435 struct coh901318_platform *pdata;
1436 struct coh901318_base *base;
1437 int irq;
1438 struct resource *io;
1439
1440 io = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1441 if (!io)
Linus Walleijf7ceb362012-06-12 20:19:24 +02001442 return -ENODEV;
Linus Walleij61f135b2009-11-19 19:49:17 +01001443
1444 /* Map DMA controller registers to virtual memory */
Linus Walleijf7ceb362012-06-12 20:19:24 +02001445 if (devm_request_mem_region(&pdev->dev,
1446 io->start,
1447 resource_size(io),
1448 pdev->dev.driver->name) == NULL)
1449 return -ENOMEM;
Linus Walleij61f135b2009-11-19 19:49:17 +01001450
1451 pdata = pdev->dev.platform_data;
1452 if (!pdata)
Linus Walleijf7ceb362012-06-12 20:19:24 +02001453 return -ENODEV;
Linus Walleij61f135b2009-11-19 19:49:17 +01001454
Linus Walleijf7ceb362012-06-12 20:19:24 +02001455 base = devm_kzalloc(&pdev->dev,
1456 ALIGN(sizeof(struct coh901318_base), 4) +
1457 pdata->max_channels *
1458 sizeof(struct coh901318_chan),
1459 GFP_KERNEL);
Linus Walleij61f135b2009-11-19 19:49:17 +01001460 if (!base)
Linus Walleijf7ceb362012-06-12 20:19:24 +02001461 return -ENOMEM;
Linus Walleij61f135b2009-11-19 19:49:17 +01001462
1463 base->chans = ((void *)base) + ALIGN(sizeof(struct coh901318_base), 4);
1464
Linus Walleijf7ceb362012-06-12 20:19:24 +02001465 base->virtbase = devm_ioremap(&pdev->dev, io->start, resource_size(io));
1466 if (!base->virtbase)
1467 return -ENOMEM;
Linus Walleij61f135b2009-11-19 19:49:17 +01001468
1469 base->dev = &pdev->dev;
1470 base->platform = pdata;
1471 spin_lock_init(&base->pm.lock);
1472 base->pm.started_channels = 0;
1473
1474 COH901318_DEBUGFS_ASSIGN(debugfs_dma_base, base);
1475
Linus Walleij61f135b2009-11-19 19:49:17 +01001476 irq = platform_get_irq(pdev, 0);
1477 if (irq < 0)
Linus Walleijf7ceb362012-06-12 20:19:24 +02001478 return irq;
Linus Walleij61f135b2009-11-19 19:49:17 +01001479
Linus Walleijf7ceb362012-06-12 20:19:24 +02001480 err = devm_request_irq(&pdev->dev, irq, dma_irq_handler, IRQF_DISABLED,
1481 "coh901318", base);
1482 if (err)
1483 return err;
Linus Walleij61f135b2009-11-19 19:49:17 +01001484
1485 err = coh901318_pool_create(&base->pool, &pdev->dev,
1486 sizeof(struct coh901318_lli),
1487 32);
1488 if (err)
Linus Walleijf7ceb362012-06-12 20:19:24 +02001489 return err;
Linus Walleij61f135b2009-11-19 19:49:17 +01001490
1491 /* init channels for device transfers */
1492 coh901318_base_init(&base->dma_slave, base->platform->chans_slave,
1493 base);
1494
1495 dma_cap_zero(base->dma_slave.cap_mask);
1496 dma_cap_set(DMA_SLAVE, base->dma_slave.cap_mask);
1497
1498 base->dma_slave.device_alloc_chan_resources = coh901318_alloc_chan_resources;
1499 base->dma_slave.device_free_chan_resources = coh901318_free_chan_resources;
1500 base->dma_slave.device_prep_slave_sg = coh901318_prep_slave_sg;
Linus Walleij07934482010-03-26 16:50:49 -07001501 base->dma_slave.device_tx_status = coh901318_tx_status;
Linus Walleij61f135b2009-11-19 19:49:17 +01001502 base->dma_slave.device_issue_pending = coh901318_issue_pending;
Linus Walleijc3635c72010-03-26 16:44:01 -07001503 base->dma_slave.device_control = coh901318_control;
Linus Walleij61f135b2009-11-19 19:49:17 +01001504 base->dma_slave.dev = &pdev->dev;
1505
1506 err = dma_async_device_register(&base->dma_slave);
1507
1508 if (err)
1509 goto err_register_slave;
1510
1511 /* init channels for memcpy */
1512 coh901318_base_init(&base->dma_memcpy, base->platform->chans_memcpy,
1513 base);
1514
1515 dma_cap_zero(base->dma_memcpy.cap_mask);
1516 dma_cap_set(DMA_MEMCPY, base->dma_memcpy.cap_mask);
1517
1518 base->dma_memcpy.device_alloc_chan_resources = coh901318_alloc_chan_resources;
1519 base->dma_memcpy.device_free_chan_resources = coh901318_free_chan_resources;
1520 base->dma_memcpy.device_prep_dma_memcpy = coh901318_prep_memcpy;
Linus Walleij07934482010-03-26 16:50:49 -07001521 base->dma_memcpy.device_tx_status = coh901318_tx_status;
Linus Walleij61f135b2009-11-19 19:49:17 +01001522 base->dma_memcpy.device_issue_pending = coh901318_issue_pending;
Linus Walleijc3635c72010-03-26 16:44:01 -07001523 base->dma_memcpy.device_control = coh901318_control;
Linus Walleij61f135b2009-11-19 19:49:17 +01001524 base->dma_memcpy.dev = &pdev->dev;
Linus Walleij516fd432010-03-02 20:12:46 +01001525 /*
1526 * This controller can only access address at even 32bit boundaries,
1527 * i.e. 2^2
1528 */
1529 base->dma_memcpy.copy_align = 2;
Linus Walleij61f135b2009-11-19 19:49:17 +01001530 err = dma_async_device_register(&base->dma_memcpy);
1531
1532 if (err)
1533 goto err_register_memcpy;
1534
Linus Walleijf7ceb362012-06-12 20:19:24 +02001535 platform_set_drvdata(pdev, base);
Linus Walleij848ad122010-03-02 14:17:15 -07001536 dev_info(&pdev->dev, "Initialized COH901318 DMA on virtual base 0x%08x\n",
Linus Walleij61f135b2009-11-19 19:49:17 +01001537 (u32) base->virtbase);
1538
1539 return err;
1540
1541 err_register_memcpy:
1542 dma_async_device_unregister(&base->dma_slave);
1543 err_register_slave:
1544 coh901318_pool_destroy(&base->pool);
Linus Walleij61f135b2009-11-19 19:49:17 +01001545 return err;
1546}
1547
1548static int __exit coh901318_remove(struct platform_device *pdev)
1549{
1550 struct coh901318_base *base = platform_get_drvdata(pdev);
1551
1552 dma_async_device_unregister(&base->dma_memcpy);
1553 dma_async_device_unregister(&base->dma_slave);
1554 coh901318_pool_destroy(&base->pool);
Linus Walleij61f135b2009-11-19 19:49:17 +01001555 return 0;
1556}
1557
1558
1559static struct platform_driver coh901318_driver = {
1560 .remove = __exit_p(coh901318_remove),
1561 .driver = {
1562 .name = "coh901318",
1563 },
1564};
1565
1566int __init coh901318_init(void)
1567{
1568 return platform_driver_probe(&coh901318_driver, coh901318_probe);
1569}
Linus Walleija0eb2212011-05-18 14:18:57 +02001570subsys_initcall(coh901318_init);
Linus Walleij61f135b2009-11-19 19:49:17 +01001571
1572void __exit coh901318_exit(void)
1573{
1574 platform_driver_unregister(&coh901318_driver);
1575}
1576module_exit(coh901318_exit);
1577
1578MODULE_LICENSE("GPL");
1579MODULE_AUTHOR("Per Friden");