2 * OMAP DMAengine support
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
8 #include <linux/dmaengine.h>
9 #include <linux/dma-mapping.h>
10 #include <linux/err.h>
11 #include <linux/init.h>
12 #include <linux/interrupt.h>
13 #include <linux/list.h>
14 #include <linux/module.h>
15 #include <linux/omap-dma.h>
16 #include <linux/platform_device.h>
17 #include <linux/slab.h>
18 #include <linux/spinlock.h>
19 #include <linux/of_dma.h>
20 #include <linux/of_device.h>
25 struct dma_device ddev;
27 struct tasklet_struct task;
28 struct list_head pending;
29 struct omap_system_dma_plat_info *plat;
33 struct virt_dma_chan vc;
34 struct list_head node;
35 struct omap_system_dma_plat_info *plat;
37 struct dma_slave_config cfg;
43 struct omap_desc *desc;
49 uint32_t en; /* number of elements (24-bit) */
50 uint32_t fn; /* number of frames (16-bit) */
54 struct virt_dma_desc vd;
55 enum dma_transfer_direction dir;
58 int16_t fi; /* for OMAP_DMA_SYNC_PACKET */
59 uint8_t es; /* OMAP_DMA_DATA_TYPE_xxx */
60 uint8_t sync_mode; /* OMAP_DMA_SYNC_xxx */
61 uint8_t sync_type; /* OMAP_DMA_xxx_SYNC* */
62 uint8_t periph_port; /* Peripheral port */
68 static const unsigned es_bytes[] = {
69 [OMAP_DMA_DATA_TYPE_S8] = 1,
70 [OMAP_DMA_DATA_TYPE_S16] = 2,
71 [OMAP_DMA_DATA_TYPE_S32] = 4,
74 static struct of_dma_filter_info omap_dma_info = {
75 .filter_fn = omap_dma_filter_fn,
78 static inline struct omap_dmadev *to_omap_dma_dev(struct dma_device *d)
80 return container_of(d, struct omap_dmadev, ddev);
83 static inline struct omap_chan *to_omap_dma_chan(struct dma_chan *c)
85 return container_of(c, struct omap_chan, vc.chan);
88 static inline struct omap_desc *to_omap_dma_desc(struct dma_async_tx_descriptor *t)
90 return container_of(t, struct omap_desc, vd.tx);
93 static void omap_dma_desc_free(struct virt_dma_desc *vd)
95 kfree(container_of(vd, struct omap_desc, vd));
98 static void omap_dma_start_sg(struct omap_chan *c, struct omap_desc *d,
101 struct omap_sg *sg = d->sg + idx;
104 if (d->dir == DMA_DEV_TO_MEM) {
106 val = c->plat->dma_read(CSDP, c->dma_ch);
108 val |= OMAP_DMA_PORT_EMIFF << 9;
109 c->plat->dma_write(val, CSDP, c->dma_ch);
112 val = c->plat->dma_read(CCR, c->dma_ch);
113 val &= ~(0x03 << 14);
114 val |= OMAP_DMA_AMODE_POST_INC << 14;
115 c->plat->dma_write(val, CCR, c->dma_ch);
117 c->plat->dma_write(sg->addr, CDSA, c->dma_ch);
118 c->plat->dma_write(0, CDEI, c->dma_ch);
119 c->plat->dma_write(0, CDFI, c->dma_ch);
122 val = c->plat->dma_read(CSDP, c->dma_ch);
124 val |= OMAP_DMA_PORT_EMIFF << 2;
125 c->plat->dma_write(val, CSDP, c->dma_ch);
128 val = c->plat->dma_read(CCR, c->dma_ch);
129 val &= ~(0x03 << 12);
130 val |= OMAP_DMA_AMODE_POST_INC << 12;
131 c->plat->dma_write(val, CCR, c->dma_ch);
133 c->plat->dma_write(sg->addr, CSSA, c->dma_ch);
134 c->plat->dma_write(0, CSEI, c->dma_ch);
135 c->plat->dma_write(0, CSFI, c->dma_ch);
138 val = c->plat->dma_read(CSDP, c->dma_ch);
141 c->plat->dma_write(val, CSDP, c->dma_ch);
144 val = c->plat->dma_read(CCR, c->dma_ch);
146 if (d->sync_mode == OMAP_DMA_SYNC_FRAME)
148 c->plat->dma_write(val, CCR, c->dma_ch);
150 val = c->plat->dma_read(CCR2, c->dma_ch);
152 if (d->sync_mode == OMAP_DMA_SYNC_BLOCK)
154 c->plat->dma_write(val, CCR2, c->dma_ch);
155 } else if (c->dma_sig) {
156 val = c->plat->dma_read(CCR, c->dma_ch);
158 /* DMA_SYNCHRO_CONTROL_UPPER depends on the channel number */
159 val &= ~((1 << 23) | (3 << 19) | 0x1f);
160 val |= (c->dma_sig & ~0x1f) << 14;
161 val |= c->dma_sig & 0x1f;
163 if (d->sync_mode & OMAP_DMA_SYNC_FRAME)
168 if (d->sync_mode & OMAP_DMA_SYNC_BLOCK)
173 switch (d->sync_type) {
174 case OMAP_DMA_DST_SYNC_PREFETCH:
175 val &= ~(1 << 24); /* dest synch */
176 val |= 1 << 23; /* Prefetch */
179 val &= ~(1 << 24); /* dest synch */
182 val |= 1 << 24; /* source synch */
185 c->plat->dma_write(val, CCR, c->dma_ch);
188 c->plat->dma_write(sg->en, CEN, c->dma_ch);
189 c->plat->dma_write(sg->fn, CFN, c->dma_ch);
191 omap_start_dma(c->dma_ch);
194 static void omap_dma_start_desc(struct omap_chan *c)
196 struct virt_dma_desc *vd = vchan_next_desc(&c->vc);
207 c->desc = d = to_omap_dma_desc(&vd->tx);
210 if (d->dir == DMA_DEV_TO_MEM) {
212 val = c->plat->dma_read(CSDP, c->dma_ch);
214 val |= d->periph_port << 2;
215 c->plat->dma_write(val, CSDP, c->dma_ch);
218 val = c->plat->dma_read(CCR, c->dma_ch);
219 val &= ~(0x03 << 12);
220 val |= OMAP_DMA_AMODE_CONSTANT << 12;
221 c->plat->dma_write(val, CCR, c->dma_ch);
223 c->plat->dma_write(d->dev_addr, CSSA, c->dma_ch);
224 c->plat->dma_write(0, CSEI, c->dma_ch);
225 c->plat->dma_write(d->fi, CSFI, c->dma_ch);
228 val = c->plat->dma_read(CSDP, c->dma_ch);
230 val |= d->periph_port << 9;
231 c->plat->dma_write(val, CSDP, c->dma_ch);
234 val = c->plat->dma_read(CCR, c->dma_ch);
235 val &= ~(0x03 << 14);
236 val |= OMAP_DMA_AMODE_CONSTANT << 14;
237 c->plat->dma_write(val, CCR, c->dma_ch);
239 c->plat->dma_write(d->dev_addr, CDSA, c->dma_ch);
240 c->plat->dma_write(0, CDEI, c->dma_ch);
241 c->plat->dma_write(d->fi, CDFI, c->dma_ch);
244 omap_dma_start_sg(c, d, 0);
247 static void omap_dma_callback(int ch, u16 status, void *data)
249 struct omap_chan *c = data;
253 spin_lock_irqsave(&c->vc.lock, flags);
257 if (++c->sgidx < d->sglen) {
258 omap_dma_start_sg(c, d, c->sgidx);
260 omap_dma_start_desc(c);
261 vchan_cookie_complete(&d->vd);
264 vchan_cyclic_callback(&d->vd);
267 spin_unlock_irqrestore(&c->vc.lock, flags);
271 * This callback schedules all pending channels. We could be more
272 * clever here by postponing allocation of the real DMA channels to
273 * this point, and freeing them when our virtual channel becomes idle.
275 * We would then need to deal with 'all channels in-use'
277 static void omap_dma_sched(unsigned long data)
279 struct omap_dmadev *d = (struct omap_dmadev *)data;
282 spin_lock_irq(&d->lock);
283 list_splice_tail_init(&d->pending, &head);
284 spin_unlock_irq(&d->lock);
286 while (!list_empty(&head)) {
287 struct omap_chan *c = list_first_entry(&head,
288 struct omap_chan, node);
290 spin_lock_irq(&c->vc.lock);
291 list_del_init(&c->node);
292 omap_dma_start_desc(c);
293 spin_unlock_irq(&c->vc.lock);
297 static int omap_dma_alloc_chan_resources(struct dma_chan *chan)
299 struct omap_chan *c = to_omap_dma_chan(chan);
301 dev_dbg(c->vc.chan.device->dev, "allocating channel for %u\n", c->dma_sig);
303 return omap_request_dma(c->dma_sig, "DMA engine",
304 omap_dma_callback, c, &c->dma_ch);
307 static void omap_dma_free_chan_resources(struct dma_chan *chan)
309 struct omap_chan *c = to_omap_dma_chan(chan);
311 vchan_free_chan_resources(&c->vc);
312 omap_free_dma(c->dma_ch);
314 dev_dbg(c->vc.chan.device->dev, "freeing channel for %u\n", c->dma_sig);
317 static size_t omap_dma_sg_size(struct omap_sg *sg)
319 return sg->en * sg->fn;
322 static size_t omap_dma_desc_size(struct omap_desc *d)
327 for (size = i = 0; i < d->sglen; i++)
328 size += omap_dma_sg_size(&d->sg[i]);
330 return size * es_bytes[d->es];
333 static size_t omap_dma_desc_size_pos(struct omap_desc *d, dma_addr_t addr)
336 size_t size, es_size = es_bytes[d->es];
338 for (size = i = 0; i < d->sglen; i++) {
339 size_t this_size = omap_dma_sg_size(&d->sg[i]) * es_size;
343 else if (addr >= d->sg[i].addr &&
344 addr < d->sg[i].addr + this_size)
345 size += d->sg[i].addr + this_size - addr;
350 static enum dma_status omap_dma_tx_status(struct dma_chan *chan,
351 dma_cookie_t cookie, struct dma_tx_state *txstate)
353 struct omap_chan *c = to_omap_dma_chan(chan);
354 struct virt_dma_desc *vd;
358 ret = dma_cookie_status(chan, cookie, txstate);
359 if (ret == DMA_COMPLETE || !txstate)
362 spin_lock_irqsave(&c->vc.lock, flags);
363 vd = vchan_find_desc(&c->vc, cookie);
365 txstate->residue = omap_dma_desc_size(to_omap_dma_desc(&vd->tx));
366 } else if (c->desc && c->desc->vd.tx.cookie == cookie) {
367 struct omap_desc *d = c->desc;
370 if (d->dir == DMA_MEM_TO_DEV)
371 pos = omap_get_dma_src_pos(c->dma_ch);
372 else if (d->dir == DMA_DEV_TO_MEM)
373 pos = omap_get_dma_dst_pos(c->dma_ch);
377 txstate->residue = omap_dma_desc_size_pos(d, pos);
379 txstate->residue = 0;
381 spin_unlock_irqrestore(&c->vc.lock, flags);
386 static void omap_dma_issue_pending(struct dma_chan *chan)
388 struct omap_chan *c = to_omap_dma_chan(chan);
391 spin_lock_irqsave(&c->vc.lock, flags);
392 if (vchan_issue_pending(&c->vc) && !c->desc) {
394 * c->cyclic is used only by audio and in this case the DMA need
395 * to be started without delay.
398 struct omap_dmadev *d = to_omap_dma_dev(chan->device);
400 if (list_empty(&c->node))
401 list_add_tail(&c->node, &d->pending);
402 spin_unlock(&d->lock);
403 tasklet_schedule(&d->task);
405 omap_dma_start_desc(c);
408 spin_unlock_irqrestore(&c->vc.lock, flags);
411 static struct dma_async_tx_descriptor *omap_dma_prep_slave_sg(
412 struct dma_chan *chan, struct scatterlist *sgl, unsigned sglen,
413 enum dma_transfer_direction dir, unsigned long tx_flags, void *context)
415 struct omap_chan *c = to_omap_dma_chan(chan);
416 enum dma_slave_buswidth dev_width;
417 struct scatterlist *sgent;
420 unsigned i, j = 0, es, en, frame_bytes, sync_type;
423 if (dir == DMA_DEV_TO_MEM) {
424 dev_addr = c->cfg.src_addr;
425 dev_width = c->cfg.src_addr_width;
426 burst = c->cfg.src_maxburst;
427 sync_type = OMAP_DMA_SRC_SYNC;
428 } else if (dir == DMA_MEM_TO_DEV) {
429 dev_addr = c->cfg.dst_addr;
430 dev_width = c->cfg.dst_addr_width;
431 burst = c->cfg.dst_maxburst;
432 sync_type = OMAP_DMA_DST_SYNC;
434 dev_err(chan->device->dev, "%s: bad direction?\n", __func__);
438 /* Bus width translates to the element size (ES) */
440 case DMA_SLAVE_BUSWIDTH_1_BYTE:
441 es = OMAP_DMA_DATA_TYPE_S8;
443 case DMA_SLAVE_BUSWIDTH_2_BYTES:
444 es = OMAP_DMA_DATA_TYPE_S16;
446 case DMA_SLAVE_BUSWIDTH_4_BYTES:
447 es = OMAP_DMA_DATA_TYPE_S32;
449 default: /* not reached */
453 /* Now allocate and setup the descriptor. */
454 d = kzalloc(sizeof(*d) + sglen * sizeof(d->sg[0]), GFP_ATOMIC);
459 d->dev_addr = dev_addr;
461 d->sync_mode = OMAP_DMA_SYNC_FRAME;
462 d->sync_type = sync_type;
463 d->periph_port = OMAP_DMA_PORT_TIPB;
466 * Build our scatterlist entries: each contains the address,
467 * the number of elements (EN) in each frame, and the number of
468 * frames (FN). Number of bytes for this entry = ES * EN * FN.
470 * Burst size translates to number of elements with frame sync.
471 * Note: DMA engine defines burst to be the number of dev-width
475 frame_bytes = es_bytes[es] * en;
476 for_each_sg(sgl, sgent, sglen, i) {
477 d->sg[j].addr = sg_dma_address(sgent);
479 d->sg[j].fn = sg_dma_len(sgent) / frame_bytes;
485 return vchan_tx_prep(&c->vc, &d->vd, tx_flags);
488 static struct dma_async_tx_descriptor *omap_dma_prep_dma_cyclic(
489 struct dma_chan *chan, dma_addr_t buf_addr, size_t buf_len,
490 size_t period_len, enum dma_transfer_direction dir, unsigned long flags,
493 struct omap_chan *c = to_omap_dma_chan(chan);
494 enum dma_slave_buswidth dev_width;
497 unsigned es, sync_type;
500 if (dir == DMA_DEV_TO_MEM) {
501 dev_addr = c->cfg.src_addr;
502 dev_width = c->cfg.src_addr_width;
503 burst = c->cfg.src_maxburst;
504 sync_type = OMAP_DMA_SRC_SYNC;
505 } else if (dir == DMA_MEM_TO_DEV) {
506 dev_addr = c->cfg.dst_addr;
507 dev_width = c->cfg.dst_addr_width;
508 burst = c->cfg.dst_maxburst;
509 sync_type = OMAP_DMA_DST_SYNC;
511 dev_err(chan->device->dev, "%s: bad direction?\n", __func__);
515 /* Bus width translates to the element size (ES) */
517 case DMA_SLAVE_BUSWIDTH_1_BYTE:
518 es = OMAP_DMA_DATA_TYPE_S8;
520 case DMA_SLAVE_BUSWIDTH_2_BYTES:
521 es = OMAP_DMA_DATA_TYPE_S16;
523 case DMA_SLAVE_BUSWIDTH_4_BYTES:
524 es = OMAP_DMA_DATA_TYPE_S32;
526 default: /* not reached */
530 /* Now allocate and setup the descriptor. */
531 d = kzalloc(sizeof(*d) + sizeof(d->sg[0]), GFP_ATOMIC);
536 d->dev_addr = dev_addr;
540 d->sync_mode = OMAP_DMA_SYNC_PACKET;
542 d->sync_mode = OMAP_DMA_SYNC_ELEMENT;
543 d->sync_type = sync_type;
544 d->periph_port = OMAP_DMA_PORT_MPUI;
545 d->sg[0].addr = buf_addr;
546 d->sg[0].en = period_len / es_bytes[es];
547 d->sg[0].fn = buf_len / period_len;
552 omap_dma_link_lch(c->dma_ch, c->dma_ch);
554 if (flags & DMA_PREP_INTERRUPT)
555 omap_enable_dma_irq(c->dma_ch, OMAP_DMA_FRAME_IRQ);
557 omap_disable_dma_irq(c->dma_ch, OMAP_DMA_BLOCK_IRQ);
560 if (dma_omap2plus()) {
563 val = c->plat->dma_read(CSDP, c->dma_ch);
564 val |= 0x03 << 7; /* src burst mode 16 */
565 val |= 0x03 << 14; /* dst burst mode 16 */
566 c->plat->dma_write(val, CSDP, c->dma_ch);
569 return vchan_tx_prep(&c->vc, &d->vd, flags);
572 static int omap_dma_slave_config(struct omap_chan *c, struct dma_slave_config *cfg)
574 if (cfg->src_addr_width == DMA_SLAVE_BUSWIDTH_8_BYTES ||
575 cfg->dst_addr_width == DMA_SLAVE_BUSWIDTH_8_BYTES)
578 memcpy(&c->cfg, cfg, sizeof(c->cfg));
583 static int omap_dma_terminate_all(struct omap_chan *c)
585 struct omap_dmadev *d = to_omap_dma_dev(c->vc.chan.device);
589 spin_lock_irqsave(&c->vc.lock, flags);
591 /* Prevent this channel being scheduled */
593 list_del_init(&c->node);
594 spin_unlock(&d->lock);
597 * Stop DMA activity: we assume the callback will not be called
598 * after omap_stop_dma() returns (even if it does, it will see
599 * c->desc is NULL and exit.)
603 /* Avoid stopping the dma twice */
605 omap_stop_dma(c->dma_ch);
611 omap_dma_unlink_lch(c->dma_ch, c->dma_ch);
614 vchan_get_all_descriptors(&c->vc, &head);
615 spin_unlock_irqrestore(&c->vc.lock, flags);
616 vchan_dma_desc_free_list(&c->vc, &head);
621 static int omap_dma_pause(struct omap_chan *c)
623 /* Pause/Resume only allowed with cyclic mode */
628 omap_stop_dma(c->dma_ch);
635 static int omap_dma_resume(struct omap_chan *c)
637 /* Pause/Resume only allowed with cyclic mode */
642 omap_start_dma(c->dma_ch);
649 static int omap_dma_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd,
652 struct omap_chan *c = to_omap_dma_chan(chan);
656 case DMA_SLAVE_CONFIG:
657 ret = omap_dma_slave_config(c, (struct dma_slave_config *)arg);
660 case DMA_TERMINATE_ALL:
661 ret = omap_dma_terminate_all(c);
665 ret = omap_dma_pause(c);
669 ret = omap_dma_resume(c);
680 static int omap_dma_chan_init(struct omap_dmadev *od, int dma_sig)
684 c = kzalloc(sizeof(*c), GFP_KERNEL);
689 c->dma_sig = dma_sig;
690 c->vc.desc_free = omap_dma_desc_free;
691 vchan_init(&c->vc, &od->ddev);
692 INIT_LIST_HEAD(&c->node);
699 static void omap_dma_free(struct omap_dmadev *od)
701 tasklet_kill(&od->task);
702 while (!list_empty(&od->ddev.channels)) {
703 struct omap_chan *c = list_first_entry(&od->ddev.channels,
704 struct omap_chan, vc.chan.device_node);
706 list_del(&c->vc.chan.device_node);
707 tasklet_kill(&c->vc.task);
712 static int omap_dma_probe(struct platform_device *pdev)
714 struct omap_dmadev *od;
717 od = devm_kzalloc(&pdev->dev, sizeof(*od), GFP_KERNEL);
721 od->plat = omap_get_plat_info();
723 return -EPROBE_DEFER;
725 dma_cap_set(DMA_SLAVE, od->ddev.cap_mask);
726 dma_cap_set(DMA_CYCLIC, od->ddev.cap_mask);
727 od->ddev.device_alloc_chan_resources = omap_dma_alloc_chan_resources;
728 od->ddev.device_free_chan_resources = omap_dma_free_chan_resources;
729 od->ddev.device_tx_status = omap_dma_tx_status;
730 od->ddev.device_issue_pending = omap_dma_issue_pending;
731 od->ddev.device_prep_slave_sg = omap_dma_prep_slave_sg;
732 od->ddev.device_prep_dma_cyclic = omap_dma_prep_dma_cyclic;
733 od->ddev.device_control = omap_dma_control;
734 od->ddev.dev = &pdev->dev;
735 INIT_LIST_HEAD(&od->ddev.channels);
736 INIT_LIST_HEAD(&od->pending);
737 spin_lock_init(&od->lock);
739 tasklet_init(&od->task, omap_dma_sched, (unsigned long)od);
741 for (i = 0; i < 127; i++) {
742 rc = omap_dma_chan_init(od, i);
749 rc = dma_async_device_register(&od->ddev);
751 pr_warn("OMAP-DMA: failed to register slave DMA engine device: %d\n",
757 platform_set_drvdata(pdev, od);
759 if (pdev->dev.of_node) {
760 omap_dma_info.dma_cap = od->ddev.cap_mask;
762 /* Device-tree DMA controller registration */
763 rc = of_dma_controller_register(pdev->dev.of_node,
764 of_dma_simple_xlate, &omap_dma_info);
766 pr_warn("OMAP-DMA: failed to register DMA controller\n");
767 dma_async_device_unregister(&od->ddev);
772 dev_info(&pdev->dev, "OMAP DMA engine driver\n");
777 static int omap_dma_remove(struct platform_device *pdev)
779 struct omap_dmadev *od = platform_get_drvdata(pdev);
781 if (pdev->dev.of_node)
782 of_dma_controller_free(pdev->dev.of_node);
784 dma_async_device_unregister(&od->ddev);
790 static const struct of_device_id omap_dma_match[] = {
791 { .compatible = "ti,omap2420-sdma", },
792 { .compatible = "ti,omap2430-sdma", },
793 { .compatible = "ti,omap3430-sdma", },
794 { .compatible = "ti,omap3630-sdma", },
795 { .compatible = "ti,omap4430-sdma", },
798 MODULE_DEVICE_TABLE(of, omap_dma_match);
800 static struct platform_driver omap_dma_driver = {
801 .probe = omap_dma_probe,
802 .remove = omap_dma_remove,
804 .name = "omap-dma-engine",
805 .owner = THIS_MODULE,
806 .of_match_table = of_match_ptr(omap_dma_match),
810 bool omap_dma_filter_fn(struct dma_chan *chan, void *param)
812 if (chan->device->dev->driver == &omap_dma_driver.driver) {
813 struct omap_chan *c = to_omap_dma_chan(chan);
814 unsigned req = *(unsigned *)param;
816 return req == c->dma_sig;
820 EXPORT_SYMBOL_GPL(omap_dma_filter_fn);
822 static int omap_dma_init(void)
824 return platform_driver_register(&omap_dma_driver);
826 subsys_initcall(omap_dma_init);
828 static void __exit omap_dma_exit(void)
830 platform_driver_unregister(&omap_dma_driver);
832 module_exit(omap_dma_exit);
834 MODULE_AUTHOR("Russell King");
835 MODULE_LICENSE("GPL");