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 omap_set_dma_dest_params(c->dma_ch, OMAP_DMA_PORT_EMIFF,
105 OMAP_DMA_AMODE_POST_INC, sg->addr, 0, 0);
107 omap_set_dma_src_params(c->dma_ch, OMAP_DMA_PORT_EMIFF,
108 OMAP_DMA_AMODE_POST_INC, sg->addr, 0, 0);
110 omap_set_dma_transfer_params(c->dma_ch, d->es, sg->en, sg->fn,
111 d->sync_mode, c->dma_sig, d->sync_type);
113 omap_start_dma(c->dma_ch);
116 static void omap_dma_start_desc(struct omap_chan *c)
118 struct virt_dma_desc *vd = vchan_next_desc(&c->vc);
128 c->desc = d = to_omap_dma_desc(&vd->tx);
131 if (d->dir == DMA_DEV_TO_MEM)
132 omap_set_dma_src_params(c->dma_ch, d->periph_port,
133 OMAP_DMA_AMODE_CONSTANT, d->dev_addr, 0, d->fi);
135 omap_set_dma_dest_params(c->dma_ch, d->periph_port,
136 OMAP_DMA_AMODE_CONSTANT, d->dev_addr, 0, d->fi);
138 omap_dma_start_sg(c, d, 0);
141 static void omap_dma_callback(int ch, u16 status, void *data)
143 struct omap_chan *c = data;
147 spin_lock_irqsave(&c->vc.lock, flags);
151 if (++c->sgidx < d->sglen) {
152 omap_dma_start_sg(c, d, c->sgidx);
154 omap_dma_start_desc(c);
155 vchan_cookie_complete(&d->vd);
158 vchan_cyclic_callback(&d->vd);
161 spin_unlock_irqrestore(&c->vc.lock, flags);
165 * This callback schedules all pending channels. We could be more
166 * clever here by postponing allocation of the real DMA channels to
167 * this point, and freeing them when our virtual channel becomes idle.
169 * We would then need to deal with 'all channels in-use'
171 static void omap_dma_sched(unsigned long data)
173 struct omap_dmadev *d = (struct omap_dmadev *)data;
176 spin_lock_irq(&d->lock);
177 list_splice_tail_init(&d->pending, &head);
178 spin_unlock_irq(&d->lock);
180 while (!list_empty(&head)) {
181 struct omap_chan *c = list_first_entry(&head,
182 struct omap_chan, node);
184 spin_lock_irq(&c->vc.lock);
185 list_del_init(&c->node);
186 omap_dma_start_desc(c);
187 spin_unlock_irq(&c->vc.lock);
191 static int omap_dma_alloc_chan_resources(struct dma_chan *chan)
193 struct omap_chan *c = to_omap_dma_chan(chan);
195 dev_dbg(c->vc.chan.device->dev, "allocating channel for %u\n", c->dma_sig);
197 return omap_request_dma(c->dma_sig, "DMA engine",
198 omap_dma_callback, c, &c->dma_ch);
201 static void omap_dma_free_chan_resources(struct dma_chan *chan)
203 struct omap_chan *c = to_omap_dma_chan(chan);
205 vchan_free_chan_resources(&c->vc);
206 omap_free_dma(c->dma_ch);
208 dev_dbg(c->vc.chan.device->dev, "freeing channel for %u\n", c->dma_sig);
211 static size_t omap_dma_sg_size(struct omap_sg *sg)
213 return sg->en * sg->fn;
216 static size_t omap_dma_desc_size(struct omap_desc *d)
221 for (size = i = 0; i < d->sglen; i++)
222 size += omap_dma_sg_size(&d->sg[i]);
224 return size * es_bytes[d->es];
227 static size_t omap_dma_desc_size_pos(struct omap_desc *d, dma_addr_t addr)
230 size_t size, es_size = es_bytes[d->es];
232 for (size = i = 0; i < d->sglen; i++) {
233 size_t this_size = omap_dma_sg_size(&d->sg[i]) * es_size;
237 else if (addr >= d->sg[i].addr &&
238 addr < d->sg[i].addr + this_size)
239 size += d->sg[i].addr + this_size - addr;
244 static enum dma_status omap_dma_tx_status(struct dma_chan *chan,
245 dma_cookie_t cookie, struct dma_tx_state *txstate)
247 struct omap_chan *c = to_omap_dma_chan(chan);
248 struct virt_dma_desc *vd;
252 ret = dma_cookie_status(chan, cookie, txstate);
253 if (ret == DMA_COMPLETE || !txstate)
256 spin_lock_irqsave(&c->vc.lock, flags);
257 vd = vchan_find_desc(&c->vc, cookie);
259 txstate->residue = omap_dma_desc_size(to_omap_dma_desc(&vd->tx));
260 } else if (c->desc && c->desc->vd.tx.cookie == cookie) {
261 struct omap_desc *d = c->desc;
264 if (d->dir == DMA_MEM_TO_DEV)
265 pos = omap_get_dma_src_pos(c->dma_ch);
266 else if (d->dir == DMA_DEV_TO_MEM)
267 pos = omap_get_dma_dst_pos(c->dma_ch);
271 txstate->residue = omap_dma_desc_size_pos(d, pos);
273 txstate->residue = 0;
275 spin_unlock_irqrestore(&c->vc.lock, flags);
280 static void omap_dma_issue_pending(struct dma_chan *chan)
282 struct omap_chan *c = to_omap_dma_chan(chan);
285 spin_lock_irqsave(&c->vc.lock, flags);
286 if (vchan_issue_pending(&c->vc) && !c->desc) {
288 * c->cyclic is used only by audio and in this case the DMA need
289 * to be started without delay.
292 struct omap_dmadev *d = to_omap_dma_dev(chan->device);
294 if (list_empty(&c->node))
295 list_add_tail(&c->node, &d->pending);
296 spin_unlock(&d->lock);
297 tasklet_schedule(&d->task);
299 omap_dma_start_desc(c);
302 spin_unlock_irqrestore(&c->vc.lock, flags);
305 static struct dma_async_tx_descriptor *omap_dma_prep_slave_sg(
306 struct dma_chan *chan, struct scatterlist *sgl, unsigned sglen,
307 enum dma_transfer_direction dir, unsigned long tx_flags, void *context)
309 struct omap_chan *c = to_omap_dma_chan(chan);
310 enum dma_slave_buswidth dev_width;
311 struct scatterlist *sgent;
314 unsigned i, j = 0, es, en, frame_bytes, sync_type;
317 if (dir == DMA_DEV_TO_MEM) {
318 dev_addr = c->cfg.src_addr;
319 dev_width = c->cfg.src_addr_width;
320 burst = c->cfg.src_maxburst;
321 sync_type = OMAP_DMA_SRC_SYNC;
322 } else if (dir == DMA_MEM_TO_DEV) {
323 dev_addr = c->cfg.dst_addr;
324 dev_width = c->cfg.dst_addr_width;
325 burst = c->cfg.dst_maxburst;
326 sync_type = OMAP_DMA_DST_SYNC;
328 dev_err(chan->device->dev, "%s: bad direction?\n", __func__);
332 /* Bus width translates to the element size (ES) */
334 case DMA_SLAVE_BUSWIDTH_1_BYTE:
335 es = OMAP_DMA_DATA_TYPE_S8;
337 case DMA_SLAVE_BUSWIDTH_2_BYTES:
338 es = OMAP_DMA_DATA_TYPE_S16;
340 case DMA_SLAVE_BUSWIDTH_4_BYTES:
341 es = OMAP_DMA_DATA_TYPE_S32;
343 default: /* not reached */
347 /* Now allocate and setup the descriptor. */
348 d = kzalloc(sizeof(*d) + sglen * sizeof(d->sg[0]), GFP_ATOMIC);
353 d->dev_addr = dev_addr;
355 d->sync_mode = OMAP_DMA_SYNC_FRAME;
356 d->sync_type = sync_type;
357 d->periph_port = OMAP_DMA_PORT_TIPB;
360 * Build our scatterlist entries: each contains the address,
361 * the number of elements (EN) in each frame, and the number of
362 * frames (FN). Number of bytes for this entry = ES * EN * FN.
364 * Burst size translates to number of elements with frame sync.
365 * Note: DMA engine defines burst to be the number of dev-width
369 frame_bytes = es_bytes[es] * en;
370 for_each_sg(sgl, sgent, sglen, i) {
371 d->sg[j].addr = sg_dma_address(sgent);
373 d->sg[j].fn = sg_dma_len(sgent) / frame_bytes;
379 return vchan_tx_prep(&c->vc, &d->vd, tx_flags);
382 static struct dma_async_tx_descriptor *omap_dma_prep_dma_cyclic(
383 struct dma_chan *chan, dma_addr_t buf_addr, size_t buf_len,
384 size_t period_len, enum dma_transfer_direction dir, unsigned long flags,
387 struct omap_chan *c = to_omap_dma_chan(chan);
388 enum dma_slave_buswidth dev_width;
391 unsigned es, sync_type;
394 if (dir == DMA_DEV_TO_MEM) {
395 dev_addr = c->cfg.src_addr;
396 dev_width = c->cfg.src_addr_width;
397 burst = c->cfg.src_maxburst;
398 sync_type = OMAP_DMA_SRC_SYNC;
399 } else if (dir == DMA_MEM_TO_DEV) {
400 dev_addr = c->cfg.dst_addr;
401 dev_width = c->cfg.dst_addr_width;
402 burst = c->cfg.dst_maxburst;
403 sync_type = OMAP_DMA_DST_SYNC;
405 dev_err(chan->device->dev, "%s: bad direction?\n", __func__);
409 /* Bus width translates to the element size (ES) */
411 case DMA_SLAVE_BUSWIDTH_1_BYTE:
412 es = OMAP_DMA_DATA_TYPE_S8;
414 case DMA_SLAVE_BUSWIDTH_2_BYTES:
415 es = OMAP_DMA_DATA_TYPE_S16;
417 case DMA_SLAVE_BUSWIDTH_4_BYTES:
418 es = OMAP_DMA_DATA_TYPE_S32;
420 default: /* not reached */
424 /* Now allocate and setup the descriptor. */
425 d = kzalloc(sizeof(*d) + sizeof(d->sg[0]), GFP_ATOMIC);
430 d->dev_addr = dev_addr;
434 d->sync_mode = OMAP_DMA_SYNC_PACKET;
436 d->sync_mode = OMAP_DMA_SYNC_ELEMENT;
437 d->sync_type = sync_type;
438 d->periph_port = OMAP_DMA_PORT_MPUI;
439 d->sg[0].addr = buf_addr;
440 d->sg[0].en = period_len / es_bytes[es];
441 d->sg[0].fn = buf_len / period_len;
446 omap_dma_link_lch(c->dma_ch, c->dma_ch);
448 if (flags & DMA_PREP_INTERRUPT)
449 omap_enable_dma_irq(c->dma_ch, OMAP_DMA_FRAME_IRQ);
451 omap_disable_dma_irq(c->dma_ch, OMAP_DMA_BLOCK_IRQ);
454 if (dma_omap2plus()) {
455 omap_set_dma_src_burst_mode(c->dma_ch, OMAP_DMA_DATA_BURST_16);
456 omap_set_dma_dest_burst_mode(c->dma_ch, OMAP_DMA_DATA_BURST_16);
459 return vchan_tx_prep(&c->vc, &d->vd, flags);
462 static int omap_dma_slave_config(struct omap_chan *c, struct dma_slave_config *cfg)
464 if (cfg->src_addr_width == DMA_SLAVE_BUSWIDTH_8_BYTES ||
465 cfg->dst_addr_width == DMA_SLAVE_BUSWIDTH_8_BYTES)
468 memcpy(&c->cfg, cfg, sizeof(c->cfg));
473 static int omap_dma_terminate_all(struct omap_chan *c)
475 struct omap_dmadev *d = to_omap_dma_dev(c->vc.chan.device);
479 spin_lock_irqsave(&c->vc.lock, flags);
481 /* Prevent this channel being scheduled */
483 list_del_init(&c->node);
484 spin_unlock(&d->lock);
487 * Stop DMA activity: we assume the callback will not be called
488 * after omap_stop_dma() returns (even if it does, it will see
489 * c->desc is NULL and exit.)
493 /* Avoid stopping the dma twice */
495 omap_stop_dma(c->dma_ch);
501 omap_dma_unlink_lch(c->dma_ch, c->dma_ch);
504 vchan_get_all_descriptors(&c->vc, &head);
505 spin_unlock_irqrestore(&c->vc.lock, flags);
506 vchan_dma_desc_free_list(&c->vc, &head);
511 static int omap_dma_pause(struct omap_chan *c)
513 /* Pause/Resume only allowed with cyclic mode */
518 omap_stop_dma(c->dma_ch);
525 static int omap_dma_resume(struct omap_chan *c)
527 /* Pause/Resume only allowed with cyclic mode */
532 omap_start_dma(c->dma_ch);
539 static int omap_dma_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd,
542 struct omap_chan *c = to_omap_dma_chan(chan);
546 case DMA_SLAVE_CONFIG:
547 ret = omap_dma_slave_config(c, (struct dma_slave_config *)arg);
550 case DMA_TERMINATE_ALL:
551 ret = omap_dma_terminate_all(c);
555 ret = omap_dma_pause(c);
559 ret = omap_dma_resume(c);
570 static int omap_dma_chan_init(struct omap_dmadev *od, int dma_sig)
574 c = kzalloc(sizeof(*c), GFP_KERNEL);
579 c->dma_sig = dma_sig;
580 c->vc.desc_free = omap_dma_desc_free;
581 vchan_init(&c->vc, &od->ddev);
582 INIT_LIST_HEAD(&c->node);
589 static void omap_dma_free(struct omap_dmadev *od)
591 tasklet_kill(&od->task);
592 while (!list_empty(&od->ddev.channels)) {
593 struct omap_chan *c = list_first_entry(&od->ddev.channels,
594 struct omap_chan, vc.chan.device_node);
596 list_del(&c->vc.chan.device_node);
597 tasklet_kill(&c->vc.task);
602 static int omap_dma_probe(struct platform_device *pdev)
604 struct omap_dmadev *od;
607 od = devm_kzalloc(&pdev->dev, sizeof(*od), GFP_KERNEL);
611 od->plat = omap_get_plat_info();
613 return -EPROBE_DEFER;
615 dma_cap_set(DMA_SLAVE, od->ddev.cap_mask);
616 dma_cap_set(DMA_CYCLIC, od->ddev.cap_mask);
617 od->ddev.device_alloc_chan_resources = omap_dma_alloc_chan_resources;
618 od->ddev.device_free_chan_resources = omap_dma_free_chan_resources;
619 od->ddev.device_tx_status = omap_dma_tx_status;
620 od->ddev.device_issue_pending = omap_dma_issue_pending;
621 od->ddev.device_prep_slave_sg = omap_dma_prep_slave_sg;
622 od->ddev.device_prep_dma_cyclic = omap_dma_prep_dma_cyclic;
623 od->ddev.device_control = omap_dma_control;
624 od->ddev.dev = &pdev->dev;
625 INIT_LIST_HEAD(&od->ddev.channels);
626 INIT_LIST_HEAD(&od->pending);
627 spin_lock_init(&od->lock);
629 tasklet_init(&od->task, omap_dma_sched, (unsigned long)od);
631 for (i = 0; i < 127; i++) {
632 rc = omap_dma_chan_init(od, i);
639 rc = dma_async_device_register(&od->ddev);
641 pr_warn("OMAP-DMA: failed to register slave DMA engine device: %d\n",
647 platform_set_drvdata(pdev, od);
649 if (pdev->dev.of_node) {
650 omap_dma_info.dma_cap = od->ddev.cap_mask;
652 /* Device-tree DMA controller registration */
653 rc = of_dma_controller_register(pdev->dev.of_node,
654 of_dma_simple_xlate, &omap_dma_info);
656 pr_warn("OMAP-DMA: failed to register DMA controller\n");
657 dma_async_device_unregister(&od->ddev);
662 dev_info(&pdev->dev, "OMAP DMA engine driver\n");
667 static int omap_dma_remove(struct platform_device *pdev)
669 struct omap_dmadev *od = platform_get_drvdata(pdev);
671 if (pdev->dev.of_node)
672 of_dma_controller_free(pdev->dev.of_node);
674 dma_async_device_unregister(&od->ddev);
680 static const struct of_device_id omap_dma_match[] = {
681 { .compatible = "ti,omap2420-sdma", },
682 { .compatible = "ti,omap2430-sdma", },
683 { .compatible = "ti,omap3430-sdma", },
684 { .compatible = "ti,omap3630-sdma", },
685 { .compatible = "ti,omap4430-sdma", },
688 MODULE_DEVICE_TABLE(of, omap_dma_match);
690 static struct platform_driver omap_dma_driver = {
691 .probe = omap_dma_probe,
692 .remove = omap_dma_remove,
694 .name = "omap-dma-engine",
695 .owner = THIS_MODULE,
696 .of_match_table = of_match_ptr(omap_dma_match),
700 bool omap_dma_filter_fn(struct dma_chan *chan, void *param)
702 if (chan->device->dev->driver == &omap_dma_driver.driver) {
703 struct omap_chan *c = to_omap_dma_chan(chan);
704 unsigned req = *(unsigned *)param;
706 return req == c->dma_sig;
710 EXPORT_SYMBOL_GPL(omap_dma_filter_fn);
712 static int omap_dma_init(void)
714 return platform_driver_register(&omap_dma_driver);
716 subsys_initcall(omap_dma_init);
718 static void __exit omap_dma_exit(void)
720 platform_driver_unregister(&omap_dma_driver);
722 module_exit(omap_dma_exit);
724 MODULE_AUTHOR("Russell King");
725 MODULE_LICENSE("GPL");