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;
103 if (d->dir == DMA_DEV_TO_MEM) {
104 c->plat->dma_write(sg->addr, CDSA, c->dma_ch);
105 c->plat->dma_write(0, CDEI, c->dma_ch);
106 c->plat->dma_write(0, CDFI, c->dma_ch);
108 c->plat->dma_write(sg->addr, CSSA, c->dma_ch);
109 c->plat->dma_write(0, CSEI, c->dma_ch);
110 c->plat->dma_write(0, CSFI, c->dma_ch);
113 c->plat->dma_write(sg->en, CEN, c->dma_ch);
114 c->plat->dma_write(sg->fn, CFN, c->dma_ch);
116 omap_start_dma(c->dma_ch);
119 static void omap_dma_start_desc(struct omap_chan *c)
121 struct virt_dma_desc *vd = vchan_next_desc(&c->vc);
132 c->desc = d = to_omap_dma_desc(&vd->tx);
135 if (d->dir == DMA_DEV_TO_MEM) {
137 val = c->plat->dma_read(CSDP, c->dma_ch);
138 val &= ~(0x1f << 9 | 0x1f << 2);
139 val |= OMAP_DMA_PORT_EMIFF << 9;
140 val |= d->periph_port << 2;
141 c->plat->dma_write(val, CSDP, c->dma_ch);
144 val = c->plat->dma_read(CCR, c->dma_ch);
145 val &= ~(0x03 << 14 | 0x03 << 12);
146 val |= OMAP_DMA_AMODE_POST_INC << 14;
147 val |= OMAP_DMA_AMODE_CONSTANT << 12;
148 c->plat->dma_write(val, CCR, c->dma_ch);
150 c->plat->dma_write(d->dev_addr, CSSA, c->dma_ch);
151 c->plat->dma_write(0, CSEI, c->dma_ch);
152 c->plat->dma_write(d->fi, CSFI, c->dma_ch);
155 val = c->plat->dma_read(CSDP, c->dma_ch);
156 val &= ~(0x1f << 9 | 0x1f << 2);
157 val |= d->periph_port << 9;
158 val |= OMAP_DMA_PORT_EMIFF << 2;
159 c->plat->dma_write(val, CSDP, c->dma_ch);
162 val = c->plat->dma_read(CCR, c->dma_ch);
163 val &= ~(0x03 << 12 | 0x03 << 14);
164 val |= OMAP_DMA_AMODE_CONSTANT << 14;
165 val |= OMAP_DMA_AMODE_POST_INC << 12;
166 c->plat->dma_write(val, CCR, c->dma_ch);
168 c->plat->dma_write(d->dev_addr, CDSA, c->dma_ch);
169 c->plat->dma_write(0, CDEI, c->dma_ch);
170 c->plat->dma_write(d->fi, CDFI, c->dma_ch);
173 val = c->plat->dma_read(CSDP, c->dma_ch);
176 c->plat->dma_write(val, CSDP, c->dma_ch);
179 val = c->plat->dma_read(CCR, c->dma_ch);
181 if (d->sync_mode == OMAP_DMA_SYNC_FRAME)
183 c->plat->dma_write(val, CCR, c->dma_ch);
185 val = c->plat->dma_read(CCR2, c->dma_ch);
187 if (d->sync_mode == OMAP_DMA_SYNC_BLOCK)
189 c->plat->dma_write(val, CCR2, c->dma_ch);
190 } else if (c->dma_sig) {
191 val = c->plat->dma_read(CCR, c->dma_ch);
193 /* DMA_SYNCHRO_CONTROL_UPPER depends on the channel number */
194 val &= ~(1 << 24 | 1 << 23 | 3 << 19 | 1 << 18 | 1 << 5 | 0x1f);
195 val |= (c->dma_sig & ~0x1f) << 14;
196 val |= c->dma_sig & 0x1f;
198 if (d->sync_mode & OMAP_DMA_SYNC_FRAME)
201 if (d->sync_mode & OMAP_DMA_SYNC_BLOCK)
204 switch (d->sync_type) {
205 case OMAP_DMA_DST_SYNC_PREFETCH:/* dest synch */
206 val |= 1 << 23; /* Prefetch */
211 val |= 1 << 24; /* source synch */
214 c->plat->dma_write(val, CCR, c->dma_ch);
217 omap_dma_start_sg(c, d, 0);
220 static void omap_dma_callback(int ch, u16 status, void *data)
222 struct omap_chan *c = data;
226 spin_lock_irqsave(&c->vc.lock, flags);
230 if (++c->sgidx < d->sglen) {
231 omap_dma_start_sg(c, d, c->sgidx);
233 omap_dma_start_desc(c);
234 vchan_cookie_complete(&d->vd);
237 vchan_cyclic_callback(&d->vd);
240 spin_unlock_irqrestore(&c->vc.lock, flags);
244 * This callback schedules all pending channels. We could be more
245 * clever here by postponing allocation of the real DMA channels to
246 * this point, and freeing them when our virtual channel becomes idle.
248 * We would then need to deal with 'all channels in-use'
250 static void omap_dma_sched(unsigned long data)
252 struct omap_dmadev *d = (struct omap_dmadev *)data;
255 spin_lock_irq(&d->lock);
256 list_splice_tail_init(&d->pending, &head);
257 spin_unlock_irq(&d->lock);
259 while (!list_empty(&head)) {
260 struct omap_chan *c = list_first_entry(&head,
261 struct omap_chan, node);
263 spin_lock_irq(&c->vc.lock);
264 list_del_init(&c->node);
265 omap_dma_start_desc(c);
266 spin_unlock_irq(&c->vc.lock);
270 static int omap_dma_alloc_chan_resources(struct dma_chan *chan)
272 struct omap_chan *c = to_omap_dma_chan(chan);
274 dev_dbg(c->vc.chan.device->dev, "allocating channel for %u\n", c->dma_sig);
276 return omap_request_dma(c->dma_sig, "DMA engine",
277 omap_dma_callback, c, &c->dma_ch);
280 static void omap_dma_free_chan_resources(struct dma_chan *chan)
282 struct omap_chan *c = to_omap_dma_chan(chan);
284 vchan_free_chan_resources(&c->vc);
285 omap_free_dma(c->dma_ch);
287 dev_dbg(c->vc.chan.device->dev, "freeing channel for %u\n", c->dma_sig);
290 static size_t omap_dma_sg_size(struct omap_sg *sg)
292 return sg->en * sg->fn;
295 static size_t omap_dma_desc_size(struct omap_desc *d)
300 for (size = i = 0; i < d->sglen; i++)
301 size += omap_dma_sg_size(&d->sg[i]);
303 return size * es_bytes[d->es];
306 static size_t omap_dma_desc_size_pos(struct omap_desc *d, dma_addr_t addr)
309 size_t size, es_size = es_bytes[d->es];
311 for (size = i = 0; i < d->sglen; i++) {
312 size_t this_size = omap_dma_sg_size(&d->sg[i]) * es_size;
316 else if (addr >= d->sg[i].addr &&
317 addr < d->sg[i].addr + this_size)
318 size += d->sg[i].addr + this_size - addr;
323 static enum dma_status omap_dma_tx_status(struct dma_chan *chan,
324 dma_cookie_t cookie, struct dma_tx_state *txstate)
326 struct omap_chan *c = to_omap_dma_chan(chan);
327 struct virt_dma_desc *vd;
331 ret = dma_cookie_status(chan, cookie, txstate);
332 if (ret == DMA_COMPLETE || !txstate)
335 spin_lock_irqsave(&c->vc.lock, flags);
336 vd = vchan_find_desc(&c->vc, cookie);
338 txstate->residue = omap_dma_desc_size(to_omap_dma_desc(&vd->tx));
339 } else if (c->desc && c->desc->vd.tx.cookie == cookie) {
340 struct omap_desc *d = c->desc;
343 if (d->dir == DMA_MEM_TO_DEV)
344 pos = omap_get_dma_src_pos(c->dma_ch);
345 else if (d->dir == DMA_DEV_TO_MEM)
346 pos = omap_get_dma_dst_pos(c->dma_ch);
350 txstate->residue = omap_dma_desc_size_pos(d, pos);
352 txstate->residue = 0;
354 spin_unlock_irqrestore(&c->vc.lock, flags);
359 static void omap_dma_issue_pending(struct dma_chan *chan)
361 struct omap_chan *c = to_omap_dma_chan(chan);
364 spin_lock_irqsave(&c->vc.lock, flags);
365 if (vchan_issue_pending(&c->vc) && !c->desc) {
367 * c->cyclic is used only by audio and in this case the DMA need
368 * to be started without delay.
371 struct omap_dmadev *d = to_omap_dma_dev(chan->device);
373 if (list_empty(&c->node))
374 list_add_tail(&c->node, &d->pending);
375 spin_unlock(&d->lock);
376 tasklet_schedule(&d->task);
378 omap_dma_start_desc(c);
381 spin_unlock_irqrestore(&c->vc.lock, flags);
384 static struct dma_async_tx_descriptor *omap_dma_prep_slave_sg(
385 struct dma_chan *chan, struct scatterlist *sgl, unsigned sglen,
386 enum dma_transfer_direction dir, unsigned long tx_flags, void *context)
388 struct omap_chan *c = to_omap_dma_chan(chan);
389 enum dma_slave_buswidth dev_width;
390 struct scatterlist *sgent;
393 unsigned i, j = 0, es, en, frame_bytes, sync_type;
396 if (dir == DMA_DEV_TO_MEM) {
397 dev_addr = c->cfg.src_addr;
398 dev_width = c->cfg.src_addr_width;
399 burst = c->cfg.src_maxburst;
400 sync_type = OMAP_DMA_SRC_SYNC;
401 } else if (dir == DMA_MEM_TO_DEV) {
402 dev_addr = c->cfg.dst_addr;
403 dev_width = c->cfg.dst_addr_width;
404 burst = c->cfg.dst_maxburst;
405 sync_type = OMAP_DMA_DST_SYNC;
407 dev_err(chan->device->dev, "%s: bad direction?\n", __func__);
411 /* Bus width translates to the element size (ES) */
413 case DMA_SLAVE_BUSWIDTH_1_BYTE:
414 es = OMAP_DMA_DATA_TYPE_S8;
416 case DMA_SLAVE_BUSWIDTH_2_BYTES:
417 es = OMAP_DMA_DATA_TYPE_S16;
419 case DMA_SLAVE_BUSWIDTH_4_BYTES:
420 es = OMAP_DMA_DATA_TYPE_S32;
422 default: /* not reached */
426 /* Now allocate and setup the descriptor. */
427 d = kzalloc(sizeof(*d) + sglen * sizeof(d->sg[0]), GFP_ATOMIC);
432 d->dev_addr = dev_addr;
434 d->sync_mode = OMAP_DMA_SYNC_FRAME;
435 d->sync_type = sync_type;
436 d->periph_port = OMAP_DMA_PORT_TIPB;
439 * Build our scatterlist entries: each contains the address,
440 * the number of elements (EN) in each frame, and the number of
441 * frames (FN). Number of bytes for this entry = ES * EN * FN.
443 * Burst size translates to number of elements with frame sync.
444 * Note: DMA engine defines burst to be the number of dev-width
448 frame_bytes = es_bytes[es] * en;
449 for_each_sg(sgl, sgent, sglen, i) {
450 d->sg[j].addr = sg_dma_address(sgent);
452 d->sg[j].fn = sg_dma_len(sgent) / frame_bytes;
458 return vchan_tx_prep(&c->vc, &d->vd, tx_flags);
461 static struct dma_async_tx_descriptor *omap_dma_prep_dma_cyclic(
462 struct dma_chan *chan, dma_addr_t buf_addr, size_t buf_len,
463 size_t period_len, enum dma_transfer_direction dir, unsigned long flags,
466 struct omap_chan *c = to_omap_dma_chan(chan);
467 enum dma_slave_buswidth dev_width;
470 unsigned es, sync_type;
473 if (dir == DMA_DEV_TO_MEM) {
474 dev_addr = c->cfg.src_addr;
475 dev_width = c->cfg.src_addr_width;
476 burst = c->cfg.src_maxburst;
477 sync_type = OMAP_DMA_SRC_SYNC;
478 } else if (dir == DMA_MEM_TO_DEV) {
479 dev_addr = c->cfg.dst_addr;
480 dev_width = c->cfg.dst_addr_width;
481 burst = c->cfg.dst_maxburst;
482 sync_type = OMAP_DMA_DST_SYNC;
484 dev_err(chan->device->dev, "%s: bad direction?\n", __func__);
488 /* Bus width translates to the element size (ES) */
490 case DMA_SLAVE_BUSWIDTH_1_BYTE:
491 es = OMAP_DMA_DATA_TYPE_S8;
493 case DMA_SLAVE_BUSWIDTH_2_BYTES:
494 es = OMAP_DMA_DATA_TYPE_S16;
496 case DMA_SLAVE_BUSWIDTH_4_BYTES:
497 es = OMAP_DMA_DATA_TYPE_S32;
499 default: /* not reached */
503 /* Now allocate and setup the descriptor. */
504 d = kzalloc(sizeof(*d) + sizeof(d->sg[0]), GFP_ATOMIC);
509 d->dev_addr = dev_addr;
513 d->sync_mode = OMAP_DMA_SYNC_PACKET;
515 d->sync_mode = OMAP_DMA_SYNC_ELEMENT;
516 d->sync_type = sync_type;
517 d->periph_port = OMAP_DMA_PORT_MPUI;
518 d->sg[0].addr = buf_addr;
519 d->sg[0].en = period_len / es_bytes[es];
520 d->sg[0].fn = buf_len / period_len;
525 omap_dma_link_lch(c->dma_ch, c->dma_ch);
527 if (flags & DMA_PREP_INTERRUPT)
528 omap_enable_dma_irq(c->dma_ch, OMAP_DMA_FRAME_IRQ);
530 omap_disable_dma_irq(c->dma_ch, OMAP_DMA_BLOCK_IRQ);
533 if (dma_omap2plus()) {
536 val = c->plat->dma_read(CSDP, c->dma_ch);
537 val |= 0x03 << 7; /* src burst mode 16 */
538 val |= 0x03 << 14; /* dst burst mode 16 */
539 c->plat->dma_write(val, CSDP, c->dma_ch);
542 return vchan_tx_prep(&c->vc, &d->vd, flags);
545 static int omap_dma_slave_config(struct omap_chan *c, struct dma_slave_config *cfg)
547 if (cfg->src_addr_width == DMA_SLAVE_BUSWIDTH_8_BYTES ||
548 cfg->dst_addr_width == DMA_SLAVE_BUSWIDTH_8_BYTES)
551 memcpy(&c->cfg, cfg, sizeof(c->cfg));
556 static int omap_dma_terminate_all(struct omap_chan *c)
558 struct omap_dmadev *d = to_omap_dma_dev(c->vc.chan.device);
562 spin_lock_irqsave(&c->vc.lock, flags);
564 /* Prevent this channel being scheduled */
566 list_del_init(&c->node);
567 spin_unlock(&d->lock);
570 * Stop DMA activity: we assume the callback will not be called
571 * after omap_stop_dma() returns (even if it does, it will see
572 * c->desc is NULL and exit.)
576 /* Avoid stopping the dma twice */
578 omap_stop_dma(c->dma_ch);
584 omap_dma_unlink_lch(c->dma_ch, c->dma_ch);
587 vchan_get_all_descriptors(&c->vc, &head);
588 spin_unlock_irqrestore(&c->vc.lock, flags);
589 vchan_dma_desc_free_list(&c->vc, &head);
594 static int omap_dma_pause(struct omap_chan *c)
596 /* Pause/Resume only allowed with cyclic mode */
601 omap_stop_dma(c->dma_ch);
608 static int omap_dma_resume(struct omap_chan *c)
610 /* Pause/Resume only allowed with cyclic mode */
615 omap_start_dma(c->dma_ch);
622 static int omap_dma_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd,
625 struct omap_chan *c = to_omap_dma_chan(chan);
629 case DMA_SLAVE_CONFIG:
630 ret = omap_dma_slave_config(c, (struct dma_slave_config *)arg);
633 case DMA_TERMINATE_ALL:
634 ret = omap_dma_terminate_all(c);
638 ret = omap_dma_pause(c);
642 ret = omap_dma_resume(c);
653 static int omap_dma_chan_init(struct omap_dmadev *od, int dma_sig)
657 c = kzalloc(sizeof(*c), GFP_KERNEL);
662 c->dma_sig = dma_sig;
663 c->vc.desc_free = omap_dma_desc_free;
664 vchan_init(&c->vc, &od->ddev);
665 INIT_LIST_HEAD(&c->node);
672 static void omap_dma_free(struct omap_dmadev *od)
674 tasklet_kill(&od->task);
675 while (!list_empty(&od->ddev.channels)) {
676 struct omap_chan *c = list_first_entry(&od->ddev.channels,
677 struct omap_chan, vc.chan.device_node);
679 list_del(&c->vc.chan.device_node);
680 tasklet_kill(&c->vc.task);
685 static int omap_dma_probe(struct platform_device *pdev)
687 struct omap_dmadev *od;
690 od = devm_kzalloc(&pdev->dev, sizeof(*od), GFP_KERNEL);
694 od->plat = omap_get_plat_info();
696 return -EPROBE_DEFER;
698 dma_cap_set(DMA_SLAVE, od->ddev.cap_mask);
699 dma_cap_set(DMA_CYCLIC, od->ddev.cap_mask);
700 od->ddev.device_alloc_chan_resources = omap_dma_alloc_chan_resources;
701 od->ddev.device_free_chan_resources = omap_dma_free_chan_resources;
702 od->ddev.device_tx_status = omap_dma_tx_status;
703 od->ddev.device_issue_pending = omap_dma_issue_pending;
704 od->ddev.device_prep_slave_sg = omap_dma_prep_slave_sg;
705 od->ddev.device_prep_dma_cyclic = omap_dma_prep_dma_cyclic;
706 od->ddev.device_control = omap_dma_control;
707 od->ddev.dev = &pdev->dev;
708 INIT_LIST_HEAD(&od->ddev.channels);
709 INIT_LIST_HEAD(&od->pending);
710 spin_lock_init(&od->lock);
712 tasklet_init(&od->task, omap_dma_sched, (unsigned long)od);
714 for (i = 0; i < 127; i++) {
715 rc = omap_dma_chan_init(od, i);
722 rc = dma_async_device_register(&od->ddev);
724 pr_warn("OMAP-DMA: failed to register slave DMA engine device: %d\n",
730 platform_set_drvdata(pdev, od);
732 if (pdev->dev.of_node) {
733 omap_dma_info.dma_cap = od->ddev.cap_mask;
735 /* Device-tree DMA controller registration */
736 rc = of_dma_controller_register(pdev->dev.of_node,
737 of_dma_simple_xlate, &omap_dma_info);
739 pr_warn("OMAP-DMA: failed to register DMA controller\n");
740 dma_async_device_unregister(&od->ddev);
745 dev_info(&pdev->dev, "OMAP DMA engine driver\n");
750 static int omap_dma_remove(struct platform_device *pdev)
752 struct omap_dmadev *od = platform_get_drvdata(pdev);
754 if (pdev->dev.of_node)
755 of_dma_controller_free(pdev->dev.of_node);
757 dma_async_device_unregister(&od->ddev);
763 static const struct of_device_id omap_dma_match[] = {
764 { .compatible = "ti,omap2420-sdma", },
765 { .compatible = "ti,omap2430-sdma", },
766 { .compatible = "ti,omap3430-sdma", },
767 { .compatible = "ti,omap3630-sdma", },
768 { .compatible = "ti,omap4430-sdma", },
771 MODULE_DEVICE_TABLE(of, omap_dma_match);
773 static struct platform_driver omap_dma_driver = {
774 .probe = omap_dma_probe,
775 .remove = omap_dma_remove,
777 .name = "omap-dma-engine",
778 .owner = THIS_MODULE,
779 .of_match_table = of_match_ptr(omap_dma_match),
783 bool omap_dma_filter_fn(struct dma_chan *chan, void *param)
785 if (chan->device->dev->driver == &omap_dma_driver.driver) {
786 struct omap_chan *c = to_omap_dma_chan(chan);
787 unsigned req = *(unsigned *)param;
789 return req == c->dma_sig;
793 EXPORT_SYMBOL_GPL(omap_dma_filter_fn);
795 static int omap_dma_init(void)
797 return platform_driver_register(&omap_dma_driver);
799 subsys_initcall(omap_dma_init);
801 static void __exit omap_dma_exit(void)
803 platform_driver_unregister(&omap_dma_driver);
805 module_exit(omap_dma_exit);
807 MODULE_AUTHOR("Russell King");
808 MODULE_LICENSE("GPL");