2 * driver/dma/coh901318.c
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>
10 #include <linux/init.h>
11 #include <linux/module.h>
12 #include <linux/kernel.h> /* printk() */
13 #include <linux/fs.h> /* everything... */
14 #include <linux/slab.h> /* kmalloc() */
15 #include <linux/dmaengine.h>
16 #include <linux/platform_device.h>
17 #include <linux/device.h>
18 #include <linux/irqreturn.h>
19 #include <linux/interrupt.h>
21 #include <linux/uaccess.h>
22 #include <linux/debugfs.h>
23 #include <mach/coh901318.h>
25 #include "coh901318_lli.h"
27 #define COHC_2_DEV(cohc) (&cohc->chan.dev->device)
30 #define COH_DBG(x) ({ if (1) x; 0; })
32 #define COH_DBG(x) ({ if (0) x; 0; })
35 struct coh901318_desc {
36 struct dma_async_tx_descriptor desc;
37 struct list_head node;
38 struct scatterlist *sg;
40 struct coh901318_lli *lli;
41 enum dma_data_direction dir;
45 struct coh901318_base {
47 void __iomem *virtbase;
48 struct coh901318_pool pool;
50 struct dma_device dma_slave;
51 struct dma_device dma_memcpy;
52 struct coh901318_chan *chans;
53 struct coh901318_platform *platform;
56 struct coh901318_chan {
63 struct work_struct free_work;
66 struct tasklet_struct tasklet;
68 struct list_head active;
69 struct list_head queue;
70 struct list_head free;
72 unsigned long nbr_active_done;
75 struct coh901318_base *base;
78 static void coh901318_list_print(struct coh901318_chan *cohc,
79 struct coh901318_lli *lli)
81 struct coh901318_lli *l = lli;
85 dev_vdbg(COHC_2_DEV(cohc), "i %d, lli %p, ctrl 0x%x, src 0x%x"
86 ", dst 0x%x, link 0x%x virt_link_addr 0x%p\n",
87 i, l, l->control, l->src_addr, l->dst_addr,
88 l->link_addr, l->virt_link_addr);
90 l = l->virt_link_addr;
94 #ifdef CONFIG_DEBUG_FS
96 #define COH901318_DEBUGFS_ASSIGN(x, y) (x = y)
98 static struct coh901318_base *debugfs_dma_base;
99 static struct dentry *dma_dentry;
101 static int coh901318_debugfs_open(struct inode *inode, struct file *file)
104 file->private_data = inode->i_private;
108 static int coh901318_debugfs_read(struct file *file, char __user *buf,
109 size_t count, loff_t *f_pos)
111 u64 started_channels = debugfs_dma_base->pm.started_channels;
112 int pool_count = debugfs_dma_base->pool.debugfs_pool_counter;
119 dev_buf = kmalloc(4*1024, GFP_KERNEL);
124 tmp += sprintf(tmp, "DMA -- enabled dma channels\n");
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);
130 tmp += sprintf(tmp, "Pool alloc nbr %d\n", pool_count);
131 dev_size = tmp - dev_buf;
133 /* No more to read if offset != 0 */
134 if (*f_pos > dev_size)
137 if (count > dev_size - *f_pos)
138 count = dev_size - *f_pos;
140 if (copy_to_user(buf, dev_buf + *f_pos, count))
153 static const struct file_operations coh901318_debugfs_status_operations = {
154 .owner = THIS_MODULE,
155 .open = coh901318_debugfs_open,
156 .read = coh901318_debugfs_read,
160 static int __init init_coh901318_debugfs(void)
163 dma_dentry = debugfs_create_dir("dma", NULL);
165 (void) debugfs_create_file("status",
168 &coh901318_debugfs_status_operations);
172 static void __exit exit_coh901318_debugfs(void)
174 debugfs_remove_recursive(dma_dentry);
177 module_init(init_coh901318_debugfs);
178 module_exit(exit_coh901318_debugfs);
181 #define COH901318_DEBUGFS_ASSIGN(x, y)
183 #endif /* CONFIG_DEBUG_FS */
185 static inline struct coh901318_chan *to_coh901318_chan(struct dma_chan *chan)
187 return container_of(chan, struct coh901318_chan, chan);
190 static inline dma_addr_t
191 cohc_dev_addr(struct coh901318_chan *cohc)
193 return cohc->base->platform->chan_conf[cohc->id].dev_addr;
196 static inline const struct coh901318_params *
197 cohc_chan_param(struct coh901318_chan *cohc)
199 return &cohc->base->platform->chan_conf[cohc->id].param;
202 static inline const struct coh_dma_channel *
203 cohc_chan_conf(struct coh901318_chan *cohc)
205 return &cohc->base->platform->chan_conf[cohc->id];
208 static void enable_powersave(struct coh901318_chan *cohc)
211 struct powersave *pm = &cohc->base->pm;
213 spin_lock_irqsave(&pm->lock, flags);
215 pm->started_channels &= ~(1ULL << cohc->id);
217 if (!pm->started_channels) {
218 /* DMA no longer intends to access memory */
219 cohc->base->platform->access_memory_state(cohc->base->dev,
223 spin_unlock_irqrestore(&pm->lock, flags);
225 static void disable_powersave(struct coh901318_chan *cohc)
228 struct powersave *pm = &cohc->base->pm;
230 spin_lock_irqsave(&pm->lock, flags);
232 if (!pm->started_channels) {
233 /* DMA intends to access memory */
234 cohc->base->platform->access_memory_state(cohc->base->dev,
238 pm->started_channels |= (1ULL << cohc->id);
240 spin_unlock_irqrestore(&pm->lock, flags);
243 static inline int coh901318_set_ctrl(struct coh901318_chan *cohc, u32 control)
245 int channel = cohc->id;
246 void __iomem *virtbase = cohc->base->virtbase;
249 virtbase + COH901318_CX_CTRL +
250 COH901318_CX_CTRL_SPACING * channel);
254 static inline int coh901318_set_conf(struct coh901318_chan *cohc, u32 conf)
256 int channel = cohc->id;
257 void __iomem *virtbase = cohc->base->virtbase;
260 virtbase + COH901318_CX_CFG +
261 COH901318_CX_CFG_SPACING*channel);
266 static int coh901318_start(struct coh901318_chan *cohc)
269 int channel = cohc->id;
270 void __iomem *virtbase = cohc->base->virtbase;
272 disable_powersave(cohc);
274 val = readl(virtbase + COH901318_CX_CFG +
275 COH901318_CX_CFG_SPACING * channel);
278 val |= COH901318_CX_CFG_CH_ENABLE;
279 writel(val, virtbase + COH901318_CX_CFG +
280 COH901318_CX_CFG_SPACING * channel);
285 static int coh901318_prep_linked_list(struct coh901318_chan *cohc,
286 struct coh901318_lli *lli)
288 int channel = cohc->id;
289 void __iomem *virtbase = cohc->base->virtbase;
291 BUG_ON(readl(virtbase + COH901318_CX_STAT +
292 COH901318_CX_STAT_SPACING*channel) &
293 COH901318_CX_STAT_ACTIVE);
295 writel(lli->src_addr,
296 virtbase + COH901318_CX_SRC_ADDR +
297 COH901318_CX_SRC_ADDR_SPACING * channel);
299 writel(lli->dst_addr, virtbase +
300 COH901318_CX_DST_ADDR +
301 COH901318_CX_DST_ADDR_SPACING * channel);
303 writel(lli->link_addr, virtbase + COH901318_CX_LNK_ADDR +
304 COH901318_CX_LNK_ADDR_SPACING * channel);
306 writel(lli->control, virtbase + COH901318_CX_CTRL +
307 COH901318_CX_CTRL_SPACING * channel);
312 coh901318_assign_cookie(struct coh901318_chan *cohc,
313 struct coh901318_desc *cohd)
315 dma_cookie_t cookie = cohc->chan.cookie;
320 cohc->chan.cookie = cookie;
321 cohd->desc.cookie = cookie;
326 static struct coh901318_desc *
327 coh901318_desc_get(struct coh901318_chan *cohc)
329 struct coh901318_desc *desc;
331 if (list_empty(&cohc->free)) {
332 /* alloc new desc because we're out of used ones
333 * TODO: alloc a pile of descs instead of just one,
334 * avoid many small allocations.
336 desc = kzalloc(sizeof(struct coh901318_desc), GFP_NOWAIT);
339 INIT_LIST_HEAD(&desc->node);
340 dma_async_tx_descriptor_init(&desc->desc, &cohc->chan);
342 /* Reuse an old desc. */
343 desc = list_first_entry(&cohc->free,
344 struct coh901318_desc,
346 list_del(&desc->node);
347 /* Initialize it a bit so it's not insane */
350 desc->desc.callback = NULL;
351 desc->desc.callback_param = NULL;
359 coh901318_desc_free(struct coh901318_chan *cohc, struct coh901318_desc *cohd)
361 list_add_tail(&cohd->node, &cohc->free);
364 /* call with irq lock held */
366 coh901318_desc_submit(struct coh901318_chan *cohc, struct coh901318_desc *desc)
368 list_add_tail(&desc->node, &cohc->active);
371 static struct coh901318_desc *
372 coh901318_first_active_get(struct coh901318_chan *cohc)
374 struct coh901318_desc *d;
376 if (list_empty(&cohc->active))
379 d = list_first_entry(&cohc->active,
380 struct coh901318_desc,
386 coh901318_desc_remove(struct coh901318_desc *cohd)
388 list_del(&cohd->node);
392 coh901318_desc_queue(struct coh901318_chan *cohc, struct coh901318_desc *desc)
394 list_add_tail(&desc->node, &cohc->queue);
397 static struct coh901318_desc *
398 coh901318_first_queued(struct coh901318_chan *cohc)
400 struct coh901318_desc *d;
402 if (list_empty(&cohc->queue))
405 d = list_first_entry(&cohc->queue,
406 struct coh901318_desc,
411 static inline u32 coh901318_get_bytes_in_lli(struct coh901318_lli *in_lli)
413 struct coh901318_lli *lli = in_lli;
417 bytes += lli->control & COH901318_CX_CTRL_TC_VALUE_MASK;
418 lli = lli->virt_link_addr;
424 * Get the number of bytes left to transfer on this channel,
425 * it is unwise to call this before stopping the channel for
426 * absolute measures, but for a rough guess you can still call
429 u32 coh901318_get_bytes_left(struct dma_chan *chan)
431 struct coh901318_chan *cohc = to_coh901318_chan(chan);
432 struct coh901318_desc *cohd;
433 struct list_head *pos;
438 spin_lock_irqsave(&cohc->lock, flags);
441 * If there are many queued jobs, we iterate and add the
442 * size of them all. We take a special look on the first
443 * job though, since it is probably active.
445 list_for_each(pos, &cohc->active) {
447 * The first job in the list will be working on the
448 * hardware. The job can be stopped but still active,
449 * so that the transfer counter is somewhere inside
452 cohd = list_entry(pos, struct coh901318_desc, node);
455 struct coh901318_lli *lli;
458 /* Read current transfer count value */
459 left = readl(cohc->base->virtbase +
461 COH901318_CX_CTRL_SPACING * cohc->id) &
462 COH901318_CX_CTRL_TC_VALUE_MASK;
464 /* See if the transfer is linked... */
465 ladd = readl(cohc->base->virtbase +
466 COH901318_CX_LNK_ADDR +
467 COH901318_CX_LNK_ADDR_SPACING *
469 ~COH901318_CX_LNK_LINK_IMMEDIATE;
470 /* Single transaction */
475 * Linked transaction, follow the lli, find the
476 * currently processing lli, and proceed to the next
479 while (lli && lli->link_addr != ladd)
480 lli = lli->virt_link_addr;
483 lli = lli->virt_link_addr;
486 * Follow remaining lli links around to count the total
487 * number of bytes left
489 left += coh901318_get_bytes_in_lli(lli);
491 left += coh901318_get_bytes_in_lli(cohd->lli);
496 /* Also count bytes in the queued jobs */
497 list_for_each(pos, &cohc->queue) {
498 cohd = list_entry(pos, struct coh901318_desc, node);
499 left += coh901318_get_bytes_in_lli(cohd->lli);
502 spin_unlock_irqrestore(&cohc->lock, flags);
506 EXPORT_SYMBOL(coh901318_get_bytes_left);
509 /* Stops a transfer without losing data. Enables power save.
510 Use this function in conjunction with coh901318_continue(..)
512 void coh901318_stop(struct dma_chan *chan)
516 struct coh901318_chan *cohc = to_coh901318_chan(chan);
517 int channel = cohc->id;
518 void __iomem *virtbase = cohc->base->virtbase;
520 spin_lock_irqsave(&cohc->lock, flags);
522 /* Disable channel in HW */
523 val = readl(virtbase + COH901318_CX_CFG +
524 COH901318_CX_CFG_SPACING * channel);
526 /* Stopping infinit transfer */
527 if ((val & COH901318_CX_CTRL_TC_ENABLE) == 0 &&
528 (val & COH901318_CX_CFG_CH_ENABLE))
532 val &= ~COH901318_CX_CFG_CH_ENABLE;
533 /* Enable twice, HW bug work around */
534 writel(val, virtbase + COH901318_CX_CFG +
535 COH901318_CX_CFG_SPACING * channel);
536 writel(val, virtbase + COH901318_CX_CFG +
537 COH901318_CX_CFG_SPACING * channel);
539 /* Spin-wait for it to actually go inactive */
540 while (readl(virtbase + COH901318_CX_STAT+COH901318_CX_STAT_SPACING *
541 channel) & COH901318_CX_STAT_ACTIVE)
544 /* Check if we stopped an active job */
545 if ((readl(virtbase + COH901318_CX_CTRL+COH901318_CX_CTRL_SPACING *
546 channel) & COH901318_CX_CTRL_TC_VALUE_MASK) > 0)
549 enable_powersave(cohc);
551 spin_unlock_irqrestore(&cohc->lock, flags);
553 EXPORT_SYMBOL(coh901318_stop);
555 /* Continues a transfer that has been stopped via 300_dma_stop(..).
556 Power save is handled.
558 void coh901318_continue(struct dma_chan *chan)
562 struct coh901318_chan *cohc = to_coh901318_chan(chan);
563 int channel = cohc->id;
565 spin_lock_irqsave(&cohc->lock, flags);
567 disable_powersave(cohc);
570 /* Enable channel in HW */
571 val = readl(cohc->base->virtbase + COH901318_CX_CFG +
572 COH901318_CX_CFG_SPACING * channel);
574 val |= COH901318_CX_CFG_CH_ENABLE;
576 writel(val, cohc->base->virtbase + COH901318_CX_CFG +
577 COH901318_CX_CFG_SPACING*channel);
582 spin_unlock_irqrestore(&cohc->lock, flags);
584 EXPORT_SYMBOL(coh901318_continue);
586 bool coh901318_filter_id(struct dma_chan *chan, void *chan_id)
588 unsigned int ch_nr = (unsigned int) chan_id;
590 if (ch_nr == to_coh901318_chan(chan)->id)
595 EXPORT_SYMBOL(coh901318_filter_id);
598 * DMA channel allocation
600 static int coh901318_config(struct coh901318_chan *cohc,
601 struct coh901318_params *param)
604 const struct coh901318_params *p;
605 int channel = cohc->id;
606 void __iomem *virtbase = cohc->base->virtbase;
608 spin_lock_irqsave(&cohc->lock, flags);
613 p = &cohc->base->platform->chan_conf[channel].param;
615 /* Clear any pending BE or TC interrupt */
617 writel(1 << channel, virtbase + COH901318_BE_INT_CLEAR1);
618 writel(1 << channel, virtbase + COH901318_TC_INT_CLEAR1);
620 writel(1 << (channel - 32), virtbase +
621 COH901318_BE_INT_CLEAR2);
622 writel(1 << (channel - 32), virtbase +
623 COH901318_TC_INT_CLEAR2);
626 coh901318_set_conf(cohc, p->config);
627 coh901318_set_ctrl(cohc, p->ctrl_lli_last);
629 spin_unlock_irqrestore(&cohc->lock, flags);
634 /* must lock when calling this function
635 * start queued jobs, if any
636 * TODO: start all queued jobs in one go
638 * Returns descriptor if queued job is started otherwise NULL.
639 * If the queue is empty NULL is returned.
641 static struct coh901318_desc *coh901318_queue_start(struct coh901318_chan *cohc)
643 struct coh901318_desc *cohd;
646 * start queued jobs, if any
647 * TODO: transmit all queued jobs in one go
649 cohd = coh901318_first_queued(cohc);
652 /* Remove from queue */
653 coh901318_desc_remove(cohd);
654 /* initiate DMA job */
657 coh901318_desc_submit(cohc, cohd);
659 coh901318_prep_linked_list(cohc, cohd->lli);
661 /* start dma job on this channel */
662 coh901318_start(cohc);
670 * This tasklet is called from the interrupt handler to
671 * handle each descriptor (DMA job) that is sent to a channel.
673 static void dma_tasklet(unsigned long data)
675 struct coh901318_chan *cohc = (struct coh901318_chan *) data;
676 struct coh901318_desc *cohd_fin;
678 dma_async_tx_callback callback;
679 void *callback_param;
681 dev_vdbg(COHC_2_DEV(cohc), "[%s] chan_id %d"
682 " nbr_active_done %ld\n", __func__,
683 cohc->id, cohc->nbr_active_done);
685 spin_lock_irqsave(&cohc->lock, flags);
687 /* get first active descriptor entry from list */
688 cohd_fin = coh901318_first_active_get(cohc);
690 if (cohd_fin == NULL)
693 /* locate callback to client */
694 callback = cohd_fin->desc.callback;
695 callback_param = cohd_fin->desc.callback_param;
697 /* sign this job as completed on the channel */
698 cohc->completed = cohd_fin->desc.cookie;
700 /* release the lli allocation and remove the descriptor */
701 coh901318_lli_free(&cohc->base->pool, &cohd_fin->lli);
703 /* return desc to free-list */
704 coh901318_desc_remove(cohd_fin);
705 coh901318_desc_free(cohc, cohd_fin);
707 spin_unlock_irqrestore(&cohc->lock, flags);
709 /* Call the callback when we're done */
711 callback(callback_param);
713 spin_lock_irqsave(&cohc->lock, flags);
716 * If another interrupt fired while the tasklet was scheduling,
717 * we don't get called twice, so we have this number of active
718 * counter that keep track of the number of IRQs expected to
719 * be handled for this channel. If there happen to be more than
720 * one IRQ to be ack:ed, we simply schedule this tasklet again.
722 cohc->nbr_active_done--;
723 if (cohc->nbr_active_done) {
724 dev_dbg(COHC_2_DEV(cohc), "scheduling tasklet again, new IRQs "
725 "came in while we were scheduling this tasklet\n");
726 if (cohc_chan_conf(cohc)->priority_high)
727 tasklet_hi_schedule(&cohc->tasklet);
729 tasklet_schedule(&cohc->tasklet);
732 spin_unlock_irqrestore(&cohc->lock, flags);
737 spin_unlock_irqrestore(&cohc->lock, flags);
738 dev_err(COHC_2_DEV(cohc), "[%s] No active dma desc\n", __func__);
742 /* called from interrupt context */
743 static void dma_tc_handle(struct coh901318_chan *cohc)
746 * If the channel is not allocated, then we shouldn't have
747 * any TC interrupts on it.
749 if (!cohc->allocated) {
750 dev_err(COHC_2_DEV(cohc), "spurious interrupt from "
751 "unallocated channel\n");
755 spin_lock(&cohc->lock);
758 * When we reach this point, at least one queue item
759 * should have been moved over from cohc->queue to
760 * cohc->active and run to completion, that is why we're
761 * getting a terminal count interrupt is it not?
762 * If you get this BUG() the most probable cause is that
763 * the individual nodes in the lli chain have IRQ enabled,
764 * so check your platform config for lli chain ctrl.
766 BUG_ON(list_empty(&cohc->active));
768 cohc->nbr_active_done++;
771 * This attempt to take a job from cohc->queue, put it
772 * into cohc->active and start it.
774 if (coh901318_queue_start(cohc) == NULL)
777 spin_unlock(&cohc->lock);
780 * This tasklet will remove items from cohc->active
781 * and thus terminates them.
783 if (cohc_chan_conf(cohc)->priority_high)
784 tasklet_hi_schedule(&cohc->tasklet);
786 tasklet_schedule(&cohc->tasklet);
790 static irqreturn_t dma_irq_handler(int irq, void *dev_id)
796 struct coh901318_base *base = dev_id;
797 struct coh901318_chan *cohc;
798 void __iomem *virtbase = base->virtbase;
800 status1 = readl(virtbase + COH901318_INT_STATUS1);
801 status2 = readl(virtbase + COH901318_INT_STATUS2);
803 if (unlikely(status1 == 0 && status2 == 0)) {
804 dev_warn(base->dev, "spurious DMA IRQ from no channel!\n");
808 /* TODO: consider handle IRQ in tasklet here to
809 * minimize interrupt latency */
811 /* Check the first 32 DMA channels for IRQ */
813 /* Find first bit set, return as a number. */
814 i = ffs(status1) - 1;
817 cohc = &base->chans[ch];
818 spin_lock(&cohc->lock);
820 /* Mask off this bit */
821 status1 &= ~(1 << i);
822 /* Check the individual channel bits */
823 if (test_bit(i, virtbase + COH901318_BE_INT_STATUS1)) {
824 dev_crit(COHC_2_DEV(cohc),
825 "DMA bus error on channel %d!\n", ch);
827 /* Clear BE interrupt */
828 __set_bit(i, virtbase + COH901318_BE_INT_CLEAR1);
830 /* Caused by TC, really? */
831 if (unlikely(!test_bit(i, virtbase +
832 COH901318_TC_INT_STATUS1))) {
833 dev_warn(COHC_2_DEV(cohc),
834 "ignoring interrupt not caused by terminal count on channel %d\n", ch);
835 /* Clear TC interrupt */
837 __set_bit(i, virtbase + COH901318_TC_INT_CLEAR1);
839 /* Enable powersave if transfer has finished */
840 if (!(readl(virtbase + COH901318_CX_STAT +
841 COH901318_CX_STAT_SPACING*ch) &
842 COH901318_CX_STAT_ENABLED)) {
843 enable_powersave(cohc);
846 /* Must clear TC interrupt before calling
848 * in case tc_handle initate a new dma job
850 __set_bit(i, virtbase + COH901318_TC_INT_CLEAR1);
855 spin_unlock(&cohc->lock);
858 /* Check the remaining 32 DMA channels for IRQ */
860 /* Find first bit set, return as a number. */
861 i = ffs(status2) - 1;
863 cohc = &base->chans[ch];
864 spin_lock(&cohc->lock);
866 /* Mask off this bit */
867 status2 &= ~(1 << i);
868 /* Check the individual channel bits */
869 if (test_bit(i, virtbase + COH901318_BE_INT_STATUS2)) {
870 dev_crit(COHC_2_DEV(cohc),
871 "DMA bus error on channel %d!\n", ch);
872 /* Clear BE interrupt */
874 __set_bit(i, virtbase + COH901318_BE_INT_CLEAR2);
876 /* Caused by TC, really? */
877 if (unlikely(!test_bit(i, virtbase +
878 COH901318_TC_INT_STATUS2))) {
879 dev_warn(COHC_2_DEV(cohc),
880 "ignoring interrupt not caused by terminal count on channel %d\n", ch);
881 /* Clear TC interrupt */
882 __set_bit(i, virtbase + COH901318_TC_INT_CLEAR2);
885 /* Enable powersave if transfer has finished */
886 if (!(readl(virtbase + COH901318_CX_STAT +
887 COH901318_CX_STAT_SPACING*ch) &
888 COH901318_CX_STAT_ENABLED)) {
889 enable_powersave(cohc);
891 /* Must clear TC interrupt before calling
893 * in case tc_handle initate a new dma job
895 __set_bit(i, virtbase + COH901318_TC_INT_CLEAR2);
900 spin_unlock(&cohc->lock);
906 static int coh901318_alloc_chan_resources(struct dma_chan *chan)
908 struct coh901318_chan *cohc = to_coh901318_chan(chan);
911 dev_vdbg(COHC_2_DEV(cohc), "[%s] DMA channel %d\n",
914 if (chan->client_count > 1)
917 spin_lock_irqsave(&cohc->lock, flags);
919 coh901318_config(cohc, NULL);
922 cohc->completed = chan->cookie = 1;
924 spin_unlock_irqrestore(&cohc->lock, flags);
930 coh901318_free_chan_resources(struct dma_chan *chan)
932 struct coh901318_chan *cohc = to_coh901318_chan(chan);
933 int channel = cohc->id;
936 spin_lock_irqsave(&cohc->lock, flags);
939 writel(0x00000000U, cohc->base->virtbase + COH901318_CX_CFG +
940 COH901318_CX_CFG_SPACING*channel);
941 writel(0x00000000U, cohc->base->virtbase + COH901318_CX_CTRL +
942 COH901318_CX_CTRL_SPACING*channel);
946 spin_unlock_irqrestore(&cohc->lock, flags);
948 chan->device->device_terminate_all(chan);
953 coh901318_tx_submit(struct dma_async_tx_descriptor *tx)
955 struct coh901318_desc *cohd = container_of(tx, struct coh901318_desc,
957 struct coh901318_chan *cohc = to_coh901318_chan(tx->chan);
960 spin_lock_irqsave(&cohc->lock, flags);
962 tx->cookie = coh901318_assign_cookie(cohc, cohd);
964 coh901318_desc_queue(cohc, cohd);
966 spin_unlock_irqrestore(&cohc->lock, flags);
971 static struct dma_async_tx_descriptor *
972 coh901318_prep_memcpy(struct dma_chan *chan, dma_addr_t dest, dma_addr_t src,
973 size_t size, unsigned long flags)
975 struct coh901318_lli *lli;
976 struct coh901318_desc *cohd;
978 struct coh901318_chan *cohc = to_coh901318_chan(chan);
980 u32 ctrl_last = cohc_chan_param(cohc)->ctrl_lli_last;
983 spin_lock_irqsave(&cohc->lock, flg);
985 dev_vdbg(COHC_2_DEV(cohc),
986 "[%s] channel %d src 0x%x dest 0x%x size %d\n",
987 __func__, cohc->id, src, dest, size);
989 if (flags & DMA_PREP_INTERRUPT)
990 /* Trigger interrupt after last lli */
991 ctrl_last |= COH901318_CX_CTRL_TC_IRQ_ENABLE;
993 lli_len = size >> MAX_DMA_PACKET_SIZE_SHIFT;
994 if ((lli_len << MAX_DMA_PACKET_SIZE_SHIFT) < size)
997 lli = coh901318_lli_alloc(&cohc->base->pool, lli_len);
1002 ret = coh901318_lli_fill_memcpy(
1003 &cohc->base->pool, lli, src, size, dest,
1004 cohc_chan_param(cohc)->ctrl_lli_chained,
1009 COH_DBG(coh901318_list_print(cohc, lli));
1011 /* Pick a descriptor to handle this transfer */
1012 cohd = coh901318_desc_get(cohc);
1014 cohd->flags = flags;
1015 cohd->desc.tx_submit = coh901318_tx_submit;
1017 spin_unlock_irqrestore(&cohc->lock, flg);
1021 spin_unlock_irqrestore(&cohc->lock, flg);
1025 static struct dma_async_tx_descriptor *
1026 coh901318_prep_slave_sg(struct dma_chan *chan, struct scatterlist *sgl,
1027 unsigned int sg_len, enum dma_data_direction direction,
1028 unsigned long flags)
1030 struct coh901318_chan *cohc = to_coh901318_chan(chan);
1031 struct coh901318_lli *lli;
1032 struct coh901318_desc *cohd;
1033 const struct coh901318_params *params;
1034 struct scatterlist *sg;
1038 u32 ctrl_chained = cohc_chan_param(cohc)->ctrl_lli_chained;
1039 u32 ctrl = cohc_chan_param(cohc)->ctrl_lli;
1040 u32 ctrl_last = cohc_chan_param(cohc)->ctrl_lli_last;
1047 if (sgl->length == 0)
1050 spin_lock_irqsave(&cohc->lock, flg);
1052 dev_vdbg(COHC_2_DEV(cohc), "[%s] sg_len %d dir %d\n",
1053 __func__, sg_len, direction);
1055 if (flags & DMA_PREP_INTERRUPT)
1056 /* Trigger interrupt after last lli */
1057 ctrl_last |= COH901318_CX_CTRL_TC_IRQ_ENABLE;
1059 params = cohc_chan_param(cohc);
1060 config = params->config;
1062 if (direction == DMA_TO_DEVICE) {
1063 u32 tx_flags = COH901318_CX_CTRL_PRDD_SOURCE |
1064 COH901318_CX_CTRL_SRC_ADDR_INC_ENABLE;
1066 config |= COH901318_CX_CFG_RM_MEMORY_TO_PRIMARY;
1067 ctrl_chained |= tx_flags;
1068 ctrl_last |= tx_flags;
1070 } else if (direction == DMA_FROM_DEVICE) {
1071 u32 rx_flags = COH901318_CX_CTRL_PRDD_DEST |
1072 COH901318_CX_CTRL_DST_ADDR_INC_ENABLE;
1074 config |= COH901318_CX_CFG_RM_PRIMARY_TO_MEMORY;
1075 ctrl_chained |= rx_flags;
1076 ctrl_last |= rx_flags;
1081 coh901318_set_conf(cohc, config);
1083 /* The dma only supports transmitting packages up to
1084 * MAX_DMA_PACKET_SIZE. Calculate to total number of
1085 * dma elemts required to send the entire sg list
1087 for_each_sg(sgl, sg, sg_len, i) {
1088 unsigned int factor;
1089 size = sg_dma_len(sg);
1091 if (size <= MAX_DMA_PACKET_SIZE) {
1096 factor = size >> MAX_DMA_PACKET_SIZE_SHIFT;
1097 if ((factor << MAX_DMA_PACKET_SIZE_SHIFT) < size)
1103 pr_debug("Allocate %d lli:s for this transfer\n", len);
1104 lli = coh901318_lli_alloc(&cohc->base->pool, len);
1109 /* initiate allocated lli list */
1110 ret = coh901318_lli_fill_sg(&cohc->base->pool, lli, sgl, sg_len,
1111 cohc_dev_addr(cohc),
1115 direction, COH901318_CX_CTRL_TC_IRQ_ENABLE);
1119 COH_DBG(coh901318_list_print(cohc, lli));
1121 /* Pick a descriptor to handle this transfer */
1122 cohd = coh901318_desc_get(cohc);
1123 cohd->dir = direction;
1124 cohd->flags = flags;
1125 cohd->desc.tx_submit = coh901318_tx_submit;
1128 spin_unlock_irqrestore(&cohc->lock, flg);
1134 spin_unlock_irqrestore(&cohc->lock, flg);
1139 static enum dma_status
1140 coh901318_is_tx_complete(struct dma_chan *chan,
1141 dma_cookie_t cookie, dma_cookie_t *done,
1144 struct coh901318_chan *cohc = to_coh901318_chan(chan);
1145 dma_cookie_t last_used;
1146 dma_cookie_t last_complete;
1149 last_complete = cohc->completed;
1150 last_used = chan->cookie;
1152 ret = dma_async_is_complete(cookie, last_complete, last_used);
1155 *done = last_complete;
1163 coh901318_issue_pending(struct dma_chan *chan)
1165 struct coh901318_chan *cohc = to_coh901318_chan(chan);
1166 unsigned long flags;
1168 spin_lock_irqsave(&cohc->lock, flags);
1171 * Busy means that pending jobs are already being processed,
1172 * and then there is no point in starting the queue: the
1173 * terminal count interrupt on the channel will take the next
1174 * job on the queue and execute it anyway.
1177 coh901318_queue_start(cohc);
1179 spin_unlock_irqrestore(&cohc->lock, flags);
1183 coh901318_terminate_all(struct dma_chan *chan)
1185 unsigned long flags;
1186 struct coh901318_chan *cohc = to_coh901318_chan(chan);
1187 struct coh901318_desc *cohd;
1188 void __iomem *virtbase = cohc->base->virtbase;
1190 coh901318_stop(chan);
1192 spin_lock_irqsave(&cohc->lock, flags);
1194 /* Clear any pending BE or TC interrupt */
1195 if (cohc->id < 32) {
1196 writel(1 << cohc->id, virtbase + COH901318_BE_INT_CLEAR1);
1197 writel(1 << cohc->id, virtbase + COH901318_TC_INT_CLEAR1);
1199 writel(1 << (cohc->id - 32), virtbase +
1200 COH901318_BE_INT_CLEAR2);
1201 writel(1 << (cohc->id - 32), virtbase +
1202 COH901318_TC_INT_CLEAR2);
1205 enable_powersave(cohc);
1207 while ((cohd = coh901318_first_active_get(cohc))) {
1208 /* release the lli allocation*/
1209 coh901318_lli_free(&cohc->base->pool, &cohd->lli);
1211 /* return desc to free-list */
1212 coh901318_desc_remove(cohd);
1213 coh901318_desc_free(cohc, cohd);
1216 while ((cohd = coh901318_first_queued(cohc))) {
1217 /* release the lli allocation*/
1218 coh901318_lli_free(&cohc->base->pool, &cohd->lli);
1220 /* return desc to free-list */
1221 coh901318_desc_remove(cohd);
1222 coh901318_desc_free(cohc, cohd);
1226 cohc->nbr_active_done = 0;
1229 spin_unlock_irqrestore(&cohc->lock, flags);
1231 void coh901318_base_init(struct dma_device *dma, const int *pick_chans,
1232 struct coh901318_base *base)
1236 struct coh901318_chan *cohc;
1238 INIT_LIST_HEAD(&dma->channels);
1240 for (chans_i = 0; pick_chans[chans_i] != -1; chans_i += 2) {
1241 for (i = pick_chans[chans_i]; i <= pick_chans[chans_i+1]; i++) {
1242 cohc = &base->chans[i];
1245 cohc->chan.device = dma;
1248 /* TODO: do we really need this lock if only one
1249 * client is connected to each channel?
1252 spin_lock_init(&cohc->lock);
1254 cohc->nbr_active_done = 0;
1256 INIT_LIST_HEAD(&cohc->free);
1257 INIT_LIST_HEAD(&cohc->active);
1258 INIT_LIST_HEAD(&cohc->queue);
1260 tasklet_init(&cohc->tasklet, dma_tasklet,
1261 (unsigned long) cohc);
1263 list_add_tail(&cohc->chan.device_node,
1269 static int __init coh901318_probe(struct platform_device *pdev)
1272 struct coh901318_platform *pdata;
1273 struct coh901318_base *base;
1275 struct resource *io;
1277 io = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1279 goto err_get_resource;
1281 /* Map DMA controller registers to virtual memory */
1282 if (request_mem_region(io->start,
1284 pdev->dev.driver->name) == NULL) {
1286 goto err_request_mem;
1289 pdata = pdev->dev.platform_data;
1291 goto err_no_platformdata;
1293 base = kmalloc(ALIGN(sizeof(struct coh901318_base), 4) +
1294 pdata->max_channels *
1295 sizeof(struct coh901318_chan),
1298 goto err_alloc_coh_dma_channels;
1300 base->chans = ((void *)base) + ALIGN(sizeof(struct coh901318_base), 4);
1302 base->virtbase = ioremap(io->start, resource_size(io));
1303 if (!base->virtbase) {
1305 goto err_no_ioremap;
1308 base->dev = &pdev->dev;
1309 base->platform = pdata;
1310 spin_lock_init(&base->pm.lock);
1311 base->pm.started_channels = 0;
1313 COH901318_DEBUGFS_ASSIGN(debugfs_dma_base, base);
1315 platform_set_drvdata(pdev, base);
1317 irq = platform_get_irq(pdev, 0);
1321 err = request_irq(irq, dma_irq_handler, IRQF_DISABLED,
1324 dev_crit(&pdev->dev,
1325 "Cannot allocate IRQ for DMA controller!\n");
1326 goto err_request_irq;
1329 err = coh901318_pool_create(&base->pool, &pdev->dev,
1330 sizeof(struct coh901318_lli),
1333 goto err_pool_create;
1335 /* init channels for device transfers */
1336 coh901318_base_init(&base->dma_slave, base->platform->chans_slave,
1339 dma_cap_zero(base->dma_slave.cap_mask);
1340 dma_cap_set(DMA_SLAVE, base->dma_slave.cap_mask);
1342 base->dma_slave.device_alloc_chan_resources = coh901318_alloc_chan_resources;
1343 base->dma_slave.device_free_chan_resources = coh901318_free_chan_resources;
1344 base->dma_slave.device_prep_slave_sg = coh901318_prep_slave_sg;
1345 base->dma_slave.device_is_tx_complete = coh901318_is_tx_complete;
1346 base->dma_slave.device_issue_pending = coh901318_issue_pending;
1347 base->dma_slave.device_terminate_all = coh901318_terminate_all;
1348 base->dma_slave.dev = &pdev->dev;
1350 err = dma_async_device_register(&base->dma_slave);
1353 goto err_register_slave;
1355 /* init channels for memcpy */
1356 coh901318_base_init(&base->dma_memcpy, base->platform->chans_memcpy,
1359 dma_cap_zero(base->dma_memcpy.cap_mask);
1360 dma_cap_set(DMA_MEMCPY, base->dma_memcpy.cap_mask);
1362 base->dma_memcpy.device_alloc_chan_resources = coh901318_alloc_chan_resources;
1363 base->dma_memcpy.device_free_chan_resources = coh901318_free_chan_resources;
1364 base->dma_memcpy.device_prep_dma_memcpy = coh901318_prep_memcpy;
1365 base->dma_memcpy.device_is_tx_complete = coh901318_is_tx_complete;
1366 base->dma_memcpy.device_issue_pending = coh901318_issue_pending;
1367 base->dma_memcpy.device_terminate_all = coh901318_terminate_all;
1368 base->dma_memcpy.dev = &pdev->dev;
1370 * This controller can only access address at even 32bit boundaries,
1373 base->dma_memcpy.copy_align = 2;
1374 err = dma_async_device_register(&base->dma_memcpy);
1377 goto err_register_memcpy;
1379 dev_info(&pdev->dev, "Initialized COH901318 DMA on virtual base 0x%08x\n",
1380 (u32) base->virtbase);
1384 err_register_memcpy:
1385 dma_async_device_unregister(&base->dma_slave);
1387 coh901318_pool_destroy(&base->pool);
1389 free_irq(platform_get_irq(pdev, 0), base);
1392 iounmap(base->virtbase);
1395 err_alloc_coh_dma_channels:
1396 err_no_platformdata:
1397 release_mem_region(pdev->resource->start,
1398 resource_size(pdev->resource));
1404 static int __exit coh901318_remove(struct platform_device *pdev)
1406 struct coh901318_base *base = platform_get_drvdata(pdev);
1408 dma_async_device_unregister(&base->dma_memcpy);
1409 dma_async_device_unregister(&base->dma_slave);
1410 coh901318_pool_destroy(&base->pool);
1411 free_irq(platform_get_irq(pdev, 0), base);
1412 iounmap(base->virtbase);
1414 release_mem_region(pdev->resource->start,
1415 resource_size(pdev->resource));
1420 static struct platform_driver coh901318_driver = {
1421 .remove = __exit_p(coh901318_remove),
1423 .name = "coh901318",
1427 int __init coh901318_init(void)
1429 return platform_driver_probe(&coh901318_driver, coh901318_probe);
1431 subsys_initcall(coh901318_init);
1433 void __exit coh901318_exit(void)
1435 platform_driver_unregister(&coh901318_driver);
1437 module_exit(coh901318_exit);
1439 MODULE_LICENSE("GPL");
1440 MODULE_AUTHOR("Per Friden");