1 /* linux/drivers/dma/pl330.c
3 * Copyright (C) 2010 Samsung Electronics Co. Ltd.
4 * Jaswinder Singh <jassi.brar@samsung.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
13 #include <linux/init.h>
14 #include <linux/slab.h>
15 #include <linux/module.h>
16 #include <linux/dmaengine.h>
17 #include <linux/interrupt.h>
18 #include <linux/amba/bus.h>
19 #include <linux/amba/pl330.h>
20 #include <linux/pm_runtime.h>
21 #include <linux/scatterlist.h>
23 #define NR_DEFAULT_DESC 16
26 /* In the DMAC pool */
29 * Allocted to some channel during prep_xxx
30 * Also may be sitting on the work_list.
34 * Sitting on the work_list and already submitted
35 * to the PL330 core. Not more than two descriptors
36 * of a channel can be BUSY at any time.
40 * Sitting on the channel work_list but xfer done
46 struct dma_pl330_chan {
47 /* Schedule desc completion */
48 struct tasklet_struct task;
50 /* DMA-Engine Channel */
53 /* Last completed cookie */
54 dma_cookie_t completed;
56 /* List of to be xfered descriptors */
57 struct list_head work_list;
59 /* Pointer to the DMAC that manages this channel,
60 * NULL if the channel is available to be acquired.
61 * As the parent, this DMAC also provides descriptors
64 struct dma_pl330_dmac *dmac;
66 /* To protect channel manipulation */
69 /* Token of a hardware channel thread of PL330 DMAC
70 * NULL if the channel is available to be acquired.
74 /* For D-to-M and M-to-D channels */
75 int burst_sz; /* the peripheral fifo width */
76 int burst_len; /* the number of burst */
80 struct dma_pl330_dmac {
81 struct pl330_info pif;
83 /* DMA-Engine Device */
84 struct dma_device ddma;
86 /* Pool of descriptors available for the DMAC's channels */
87 struct list_head desc_pool;
88 /* To protect desc_pool manipulation */
91 /* Peripheral channels connected to this DMAC */
92 struct dma_pl330_chan *peripherals; /* keep at end */
97 struct dma_pl330_desc {
98 /* To attach to a queue as child */
99 struct list_head node;
101 /* Descriptor for the DMA Engine API */
102 struct dma_async_tx_descriptor txd;
104 /* Xfer for PL330 core */
105 struct pl330_xfer px;
107 struct pl330_reqcfg rqcfg;
108 struct pl330_req req;
110 enum desc_status status;
112 /* The channel which currently holds this desc */
113 struct dma_pl330_chan *pchan;
116 static inline struct dma_pl330_chan *
117 to_pchan(struct dma_chan *ch)
122 return container_of(ch, struct dma_pl330_chan, chan);
125 static inline struct dma_pl330_desc *
126 to_desc(struct dma_async_tx_descriptor *tx)
128 return container_of(tx, struct dma_pl330_desc, txd);
131 static inline void free_desc_list(struct list_head *list)
133 struct dma_pl330_dmac *pdmac;
134 struct dma_pl330_desc *desc;
135 struct dma_pl330_chan *pch;
138 if (list_empty(list))
141 /* Finish off the work list */
142 list_for_each_entry(desc, list, node) {
143 dma_async_tx_callback callback;
146 /* All desc in a list belong to same channel */
148 callback = desc->txd.callback;
149 param = desc->txd.callback_param;
159 spin_lock_irqsave(&pdmac->pool_lock, flags);
160 list_splice_tail_init(list, &pdmac->desc_pool);
161 spin_unlock_irqrestore(&pdmac->pool_lock, flags);
164 static inline void fill_queue(struct dma_pl330_chan *pch)
166 struct dma_pl330_desc *desc;
169 list_for_each_entry(desc, &pch->work_list, node) {
171 /* If already submitted */
172 if (desc->status == BUSY)
175 ret = pl330_submit_req(pch->pl330_chid,
180 } else if (ret == -EAGAIN) {
181 /* QFull or DMAC Dying */
184 /* Unacceptable request */
186 dev_err(pch->dmac->pif.dev, "%s:%d Bad Desc(%d)\n",
187 __func__, __LINE__, desc->txd.cookie);
188 tasklet_schedule(&pch->task);
193 static void pl330_tasklet(unsigned long data)
195 struct dma_pl330_chan *pch = (struct dma_pl330_chan *)data;
196 struct dma_pl330_desc *desc, *_dt;
200 spin_lock_irqsave(&pch->lock, flags);
202 /* Pick up ripe tomatoes */
203 list_for_each_entry_safe(desc, _dt, &pch->work_list, node)
204 if (desc->status == DONE) {
205 pch->completed = desc->txd.cookie;
206 list_move_tail(&desc->node, &list);
209 /* Try to submit a req imm. next to the last completed cookie */
212 /* Make sure the PL330 Channel thread is active */
213 pl330_chan_ctrl(pch->pl330_chid, PL330_OP_START);
215 spin_unlock_irqrestore(&pch->lock, flags);
217 free_desc_list(&list);
220 static void dma_pl330_rqcb(void *token, enum pl330_op_err err)
222 struct dma_pl330_desc *desc = token;
223 struct dma_pl330_chan *pch = desc->pchan;
226 /* If desc aborted */
230 spin_lock_irqsave(&pch->lock, flags);
234 spin_unlock_irqrestore(&pch->lock, flags);
236 tasklet_schedule(&pch->task);
239 static int pl330_alloc_chan_resources(struct dma_chan *chan)
241 struct dma_pl330_chan *pch = to_pchan(chan);
242 struct dma_pl330_dmac *pdmac = pch->dmac;
245 spin_lock_irqsave(&pch->lock, flags);
247 pch->completed = chan->cookie = 1;
249 pch->pl330_chid = pl330_request_channel(&pdmac->pif);
250 if (!pch->pl330_chid) {
251 spin_unlock_irqrestore(&pch->lock, flags);
255 tasklet_init(&pch->task, pl330_tasklet, (unsigned long) pch);
257 spin_unlock_irqrestore(&pch->lock, flags);
262 static int pl330_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd, unsigned long arg)
264 struct dma_pl330_chan *pch = to_pchan(chan);
265 struct dma_pl330_desc *desc;
267 struct dma_pl330_dmac *pdmac = pch->dmac;
268 struct dma_slave_config *slave_config;
271 case DMA_TERMINATE_ALL:
272 spin_lock_irqsave(&pch->lock, flags);
274 /* FLUSH the PL330 Channel thread */
275 pl330_chan_ctrl(pch->pl330_chid, PL330_OP_FLUSH);
277 /* Mark all desc done */
278 list_for_each_entry(desc, &pch->work_list, node)
281 spin_unlock_irqrestore(&pch->lock, flags);
283 pl330_tasklet((unsigned long) pch);
285 case DMA_SLAVE_CONFIG:
286 slave_config = (struct dma_slave_config *)arg;
288 if (slave_config->direction == DMA_TO_DEVICE) {
289 if (slave_config->dst_addr)
290 pch->fifo_addr = slave_config->dst_addr;
291 if (slave_config->dst_addr_width)
292 pch->burst_sz = __ffs(slave_config->dst_addr_width);
293 if (slave_config->dst_maxburst)
294 pch->burst_len = slave_config->dst_maxburst;
295 } else if (slave_config->direction == DMA_FROM_DEVICE) {
296 if (slave_config->src_addr)
297 pch->fifo_addr = slave_config->src_addr;
298 if (slave_config->src_addr_width)
299 pch->burst_sz = __ffs(slave_config->src_addr_width);
300 if (slave_config->src_maxburst)
301 pch->burst_len = slave_config->src_maxburst;
305 dev_err(pch->dmac->pif.dev, "Not supported command.\n");
312 static void pl330_free_chan_resources(struct dma_chan *chan)
314 struct dma_pl330_chan *pch = to_pchan(chan);
317 spin_lock_irqsave(&pch->lock, flags);
319 tasklet_kill(&pch->task);
321 pl330_release_channel(pch->pl330_chid);
322 pch->pl330_chid = NULL;
324 spin_unlock_irqrestore(&pch->lock, flags);
327 static enum dma_status
328 pl330_tx_status(struct dma_chan *chan, dma_cookie_t cookie,
329 struct dma_tx_state *txstate)
331 struct dma_pl330_chan *pch = to_pchan(chan);
332 dma_cookie_t last_done, last_used;
335 last_done = pch->completed;
336 last_used = chan->cookie;
338 ret = dma_async_is_complete(cookie, last_done, last_used);
340 dma_set_tx_state(txstate, last_done, last_used, 0);
345 static void pl330_issue_pending(struct dma_chan *chan)
347 pl330_tasklet((unsigned long) to_pchan(chan));
351 * We returned the last one of the circular list of descriptor(s)
352 * from prep_xxx, so the argument to submit corresponds to the last
353 * descriptor of the list.
355 static dma_cookie_t pl330_tx_submit(struct dma_async_tx_descriptor *tx)
357 struct dma_pl330_desc *desc, *last = to_desc(tx);
358 struct dma_pl330_chan *pch = to_pchan(tx->chan);
362 spin_lock_irqsave(&pch->lock, flags);
364 /* Assign cookies to all nodes */
365 cookie = tx->chan->cookie;
367 while (!list_empty(&last->node)) {
368 desc = list_entry(last->node.next, struct dma_pl330_desc, node);
372 desc->txd.cookie = cookie;
374 list_move_tail(&desc->node, &pch->work_list);
379 last->txd.cookie = cookie;
381 list_add_tail(&last->node, &pch->work_list);
383 tx->chan->cookie = cookie;
385 spin_unlock_irqrestore(&pch->lock, flags);
390 static inline void _init_desc(struct dma_pl330_desc *desc)
393 desc->req.x = &desc->px;
394 desc->req.token = desc;
395 desc->rqcfg.swap = SWAP_NO;
396 desc->rqcfg.privileged = 0;
397 desc->rqcfg.insnaccess = 0;
398 desc->rqcfg.scctl = SCCTRL0;
399 desc->rqcfg.dcctl = DCCTRL0;
400 desc->req.cfg = &desc->rqcfg;
401 desc->req.xfer_cb = dma_pl330_rqcb;
402 desc->txd.tx_submit = pl330_tx_submit;
404 INIT_LIST_HEAD(&desc->node);
407 /* Returns the number of descriptors added to the DMAC pool */
408 int add_desc(struct dma_pl330_dmac *pdmac, gfp_t flg, int count)
410 struct dma_pl330_desc *desc;
417 desc = kmalloc(count * sizeof(*desc), flg);
421 spin_lock_irqsave(&pdmac->pool_lock, flags);
423 for (i = 0; i < count; i++) {
424 _init_desc(&desc[i]);
425 list_add_tail(&desc[i].node, &pdmac->desc_pool);
428 spin_unlock_irqrestore(&pdmac->pool_lock, flags);
433 static struct dma_pl330_desc *
434 pluck_desc(struct dma_pl330_dmac *pdmac)
436 struct dma_pl330_desc *desc = NULL;
442 spin_lock_irqsave(&pdmac->pool_lock, flags);
444 if (!list_empty(&pdmac->desc_pool)) {
445 desc = list_entry(pdmac->desc_pool.next,
446 struct dma_pl330_desc, node);
448 list_del_init(&desc->node);
451 desc->txd.callback = NULL;
454 spin_unlock_irqrestore(&pdmac->pool_lock, flags);
459 static struct dma_pl330_desc *pl330_get_desc(struct dma_pl330_chan *pch)
461 struct dma_pl330_dmac *pdmac = pch->dmac;
462 struct dma_pl330_peri *peri = pch->chan.private;
463 struct dma_pl330_desc *desc;
465 /* Pluck one desc from the pool of DMAC */
466 desc = pluck_desc(pdmac);
468 /* If the DMAC pool is empty, alloc new */
470 if (!add_desc(pdmac, GFP_ATOMIC, 1))
474 desc = pluck_desc(pdmac);
476 dev_err(pch->dmac->pif.dev,
477 "%s:%d ALERT!\n", __func__, __LINE__);
482 /* Initialize the descriptor */
484 desc->txd.cookie = 0;
485 async_tx_ack(&desc->txd);
488 desc->req.rqtype = peri->rqtype;
489 desc->req.peri = pch->chan.chan_id;
491 desc->req.rqtype = MEMTOMEM;
495 dma_async_tx_descriptor_init(&desc->txd, &pch->chan);
500 static inline void fill_px(struct pl330_xfer *px,
501 dma_addr_t dst, dma_addr_t src, size_t len)
509 static struct dma_pl330_desc *
510 __pl330_prep_dma_memcpy(struct dma_pl330_chan *pch, dma_addr_t dst,
511 dma_addr_t src, size_t len)
513 struct dma_pl330_desc *desc = pl330_get_desc(pch);
516 dev_err(pch->dmac->pif.dev, "%s:%d Unable to fetch desc\n",
522 * Ideally we should lookout for reqs bigger than
523 * those that can be programmed with 256 bytes of
524 * MC buffer, but considering a req size is seldom
525 * going to be word-unaligned and more than 200MB,
527 * Also, should the limit is reached we'd rather
528 * have the platform increase MC buffer size than
529 * complicating this API driver.
531 fill_px(&desc->px, dst, src, len);
536 /* Call after fixing burst size */
537 static inline int get_burst_len(struct dma_pl330_desc *desc, size_t len)
539 struct dma_pl330_chan *pch = desc->pchan;
540 struct pl330_info *pi = &pch->dmac->pif;
543 burst_len = pi->pcfg.data_bus_width / 8;
544 burst_len *= pi->pcfg.data_buf_dep;
545 burst_len >>= desc->rqcfg.brst_size;
547 /* src/dst_burst_len can't be more than 16 */
551 while (burst_len > 1) {
552 if (!(len % (burst_len << desc->rqcfg.brst_size)))
560 static struct dma_async_tx_descriptor *
561 pl330_prep_dma_memcpy(struct dma_chan *chan, dma_addr_t dst,
562 dma_addr_t src, size_t len, unsigned long flags)
564 struct dma_pl330_desc *desc;
565 struct dma_pl330_chan *pch = to_pchan(chan);
566 struct dma_pl330_peri *peri = chan->private;
567 struct pl330_info *pi;
570 if (unlikely(!pch || !len))
573 if (peri && peri->rqtype != MEMTOMEM)
576 pi = &pch->dmac->pif;
578 desc = __pl330_prep_dma_memcpy(pch, dst, src, len);
582 desc->rqcfg.src_inc = 1;
583 desc->rqcfg.dst_inc = 1;
585 /* Select max possible burst size */
586 burst = pi->pcfg.data_bus_width / 8;
594 desc->rqcfg.brst_size = 0;
595 while (burst != (1 << desc->rqcfg.brst_size))
596 desc->rqcfg.brst_size++;
598 desc->rqcfg.brst_len = get_burst_len(desc, len);
600 desc->txd.flags = flags;
605 static struct dma_async_tx_descriptor *
606 pl330_prep_slave_sg(struct dma_chan *chan, struct scatterlist *sgl,
607 unsigned int sg_len, enum dma_data_direction direction,
610 struct dma_pl330_desc *first, *desc = NULL;
611 struct dma_pl330_chan *pch = to_pchan(chan);
612 struct dma_pl330_peri *peri = chan->private;
613 struct scatterlist *sg;
618 if (unlikely(!pch || !sgl || !sg_len || !peri))
621 /* Make sure the direction is consistent */
622 if ((direction == DMA_TO_DEVICE &&
623 peri->rqtype != MEMTODEV) ||
624 (direction == DMA_FROM_DEVICE &&
625 peri->rqtype != DEVTOMEM)) {
626 dev_err(pch->dmac->pif.dev, "%s:%d Invalid Direction\n",
631 addr = pch->fifo_addr;
635 for_each_sg(sgl, sg, sg_len, i) {
637 desc = pl330_get_desc(pch);
639 struct dma_pl330_dmac *pdmac = pch->dmac;
641 dev_err(pch->dmac->pif.dev,
642 "%s:%d Unable to fetch desc\n",
647 spin_lock_irqsave(&pdmac->pool_lock, flags);
649 while (!list_empty(&first->node)) {
650 desc = list_entry(first->node.next,
651 struct dma_pl330_desc, node);
652 list_move_tail(&desc->node, &pdmac->desc_pool);
655 list_move_tail(&first->node, &pdmac->desc_pool);
657 spin_unlock_irqrestore(&pdmac->pool_lock, flags);
665 list_add_tail(&desc->node, &first->node);
667 if (direction == DMA_TO_DEVICE) {
668 desc->rqcfg.src_inc = 1;
669 desc->rqcfg.dst_inc = 0;
671 addr, sg_dma_address(sg), sg_dma_len(sg));
673 desc->rqcfg.src_inc = 0;
674 desc->rqcfg.dst_inc = 1;
676 sg_dma_address(sg), addr, sg_dma_len(sg));
679 desc->rqcfg.brst_size = pch->burst_sz;
680 desc->rqcfg.brst_len = 1;
683 /* Return the last desc in the chain */
684 desc->txd.flags = flg;
688 static irqreturn_t pl330_irq_handler(int irq, void *data)
690 if (pl330_update(data))
697 pl330_probe(struct amba_device *adev, const struct amba_id *id)
699 struct dma_pl330_platdata *pdat;
700 struct dma_pl330_dmac *pdmac;
701 struct dma_pl330_chan *pch;
702 struct pl330_info *pi;
703 struct dma_device *pd;
704 struct resource *res;
708 pdat = adev->dev.platform_data;
710 /* Allocate a new DMAC and its Channels */
711 pdmac = kzalloc(sizeof(*pdmac), GFP_KERNEL);
713 dev_err(&adev->dev, "unable to allocate mem\n");
718 pi->dev = &adev->dev;
719 pi->pl330_data = NULL;
720 pi->mcbufsz = pdat ? pdat->mcbuf_sz : 0;
723 request_mem_region(res->start, resource_size(res), "dma-pl330");
725 pi->base = ioremap(res->start, resource_size(res));
731 pdmac->clk = clk_get(&adev->dev, "dma");
732 if (IS_ERR(pdmac->clk)) {
733 dev_err(&adev->dev, "Cannot get operation clock.\n");
738 amba_set_drvdata(adev, pdmac);
740 #ifdef CONFIG_PM_RUNTIME
741 /* to use the runtime PM helper functions */
742 pm_runtime_enable(&adev->dev);
744 /* enable the power domain */
745 if (pm_runtime_get_sync(&adev->dev)) {
746 dev_err(&adev->dev, "failed to get runtime pm\n");
752 clk_enable(pdmac->clk);
756 ret = request_irq(irq, pl330_irq_handler, 0,
757 dev_name(&adev->dev), pi);
765 INIT_LIST_HEAD(&pdmac->desc_pool);
766 spin_lock_init(&pdmac->pool_lock);
768 /* Create a descriptor pool of default size */
769 if (!add_desc(pdmac, GFP_KERNEL, NR_DEFAULT_DESC))
770 dev_warn(&adev->dev, "unable to allocate desc\n");
773 INIT_LIST_HEAD(&pd->channels);
775 /* Initialize channel parameters */
776 num_chan = max(pdat ? pdat->nr_valid_peri : 0, (u8)pi->pcfg.num_chan);
777 pdmac->peripherals = kzalloc(num_chan * sizeof(*pch), GFP_KERNEL);
779 for (i = 0; i < num_chan; i++) {
780 pch = &pdmac->peripherals[i];
782 struct dma_pl330_peri *peri = &pdat->peri[i];
784 switch (peri->rqtype) {
786 dma_cap_set(DMA_MEMCPY, pd->cap_mask);
790 dma_cap_set(DMA_SLAVE, pd->cap_mask);
793 dev_err(&adev->dev, "DEVTODEV Not Supported\n");
796 pch->chan.private = peri;
798 dma_cap_set(DMA_MEMCPY, pd->cap_mask);
799 pch->chan.private = NULL;
802 INIT_LIST_HEAD(&pch->work_list);
803 spin_lock_init(&pch->lock);
804 pch->pl330_chid = NULL;
805 pch->chan.device = pd;
806 pch->chan.chan_id = i;
809 /* Add the channel to the DMAC list */
811 list_add_tail(&pch->chan.device_node, &pd->channels);
814 pd->dev = &adev->dev;
816 pd->device_alloc_chan_resources = pl330_alloc_chan_resources;
817 pd->device_free_chan_resources = pl330_free_chan_resources;
818 pd->device_prep_dma_memcpy = pl330_prep_dma_memcpy;
819 pd->device_tx_status = pl330_tx_status;
820 pd->device_prep_slave_sg = pl330_prep_slave_sg;
821 pd->device_control = pl330_control;
822 pd->device_issue_pending = pl330_issue_pending;
824 ret = dma_async_device_register(pd);
826 dev_err(&adev->dev, "unable to register DMAC\n");
831 "Loaded driver for PL330 DMAC-%d\n", adev->periphid);
833 "\tDBUFF-%ux%ubytes Num_Chans-%u Num_Peri-%u Num_Events-%u\n",
834 pi->pcfg.data_buf_dep,
835 pi->pcfg.data_bus_width / 8, pi->pcfg.num_chan,
836 pi->pcfg.num_peri, pi->pcfg.num_events);
847 release_mem_region(res->start, resource_size(res));
853 static int __devexit pl330_remove(struct amba_device *adev)
855 struct dma_pl330_dmac *pdmac = amba_get_drvdata(adev);
856 struct dma_pl330_chan *pch, *_p;
857 struct pl330_info *pi;
858 struct resource *res;
864 amba_set_drvdata(adev, NULL);
867 list_for_each_entry_safe(pch, _p, &pdmac->ddma.channels,
870 /* Remove the channel */
871 list_del(&pch->chan.device_node);
873 /* Flush the channel */
874 pl330_control(&pch->chan, DMA_TERMINATE_ALL, 0);
875 pl330_free_chan_resources(&pch->chan);
888 release_mem_region(res->start, resource_size(res));
890 #ifdef CONFIG_PM_RUNTIME
891 pm_runtime_put(&adev->dev);
892 pm_runtime_disable(&adev->dev);
894 clk_disable(pdmac->clk);
902 static struct amba_id pl330_ids[] = {
910 #ifdef CONFIG_PM_RUNTIME
911 static int pl330_runtime_suspend(struct device *dev)
913 struct dma_pl330_dmac *pdmac = dev_get_drvdata(dev);
916 dev_err(dev, "failed to get dmac\n");
920 clk_disable(pdmac->clk);
925 static int pl330_runtime_resume(struct device *dev)
927 struct dma_pl330_dmac *pdmac = dev_get_drvdata(dev);
930 dev_err(dev, "failed to get dmac\n");
934 clk_enable(pdmac->clk);
939 #define pl330_runtime_suspend NULL
940 #define pl330_runtime_resume NULL
941 #endif /* CONFIG_PM_RUNTIME */
943 static const struct dev_pm_ops pl330_pm_ops = {
944 .runtime_suspend = pl330_runtime_suspend,
945 .runtime_resume = pl330_runtime_resume,
948 static struct amba_driver pl330_driver = {
950 .owner = THIS_MODULE,
954 .id_table = pl330_ids,
955 .probe = pl330_probe,
956 .remove = pl330_remove,
959 static int __init pl330_init(void)
961 return amba_driver_register(&pl330_driver);
963 module_init(pl330_init);
965 static void __exit pl330_exit(void)
967 amba_driver_unregister(&pl330_driver);
970 module_exit(pl330_exit);
972 MODULE_AUTHOR("Jaswinder Singh <jassi.brar@samsung.com>");
973 MODULE_DESCRIPTION("API Driver for PL330 DMAC");
974 MODULE_LICENSE("GPL");