Lines Matching +full:fu540 +full:- +full:c000 +full:- +full:v1

1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * SiFive FU540 Platform DMA driver
7 * - drivers/dma/fsl-edma.c
8 * - drivers/dma/dw-edma/
9 * - drivers/dma/pxa-dma.c
12 * - Chapter 12 "Platform DMA Engine (PDMA)" of
13 * SiFive FU540-C000 v1.0
14 * https://static.dev.sifive.com/FU540-C000-v1.0.pdf
21 #include <linux/dma-mapping.h>
25 #include "sf-pdma.h"
57 spin_lock_irqsave(&chan->lock, flags); in sf_pdma_alloc_desc()
59 if (chan->desc && !chan->desc->in_use) { in sf_pdma_alloc_desc()
60 spin_unlock_irqrestore(&chan->lock, flags); in sf_pdma_alloc_desc()
61 return chan->desc; in sf_pdma_alloc_desc()
64 spin_unlock_irqrestore(&chan->lock, flags); in sf_pdma_alloc_desc()
70 desc->chan = chan; in sf_pdma_alloc_desc()
78 desc->xfer_type = PDMA_FULL_SPEED; in sf_pdma_fill_desc()
79 desc->xfer_size = size; in sf_pdma_fill_desc()
80 desc->dst_addr = dst; in sf_pdma_fill_desc()
81 desc->src_addr = src; in sf_pdma_fill_desc()
86 struct pdma_regs *regs = &chan->regs; in sf_pdma_disclaim_chan()
88 writel(PDMA_CLEAR_CTRL, regs->ctrl); in sf_pdma_disclaim_chan()
99 dev_err(chan->pdma->dma_dev.dev, in sf_pdma_prep_dma_memcpy()
108 desc->in_use = true; in sf_pdma_prep_dma_memcpy()
109 desc->dirn = DMA_MEM_TO_MEM; in sf_pdma_prep_dma_memcpy()
110 desc->async_tx = vchan_tx_prep(&chan->vchan, &desc->vdesc, flags); in sf_pdma_prep_dma_memcpy()
112 spin_lock_irqsave(&chan->vchan.lock, flags); in sf_pdma_prep_dma_memcpy()
113 chan->desc = desc; in sf_pdma_prep_dma_memcpy()
115 spin_unlock_irqrestore(&chan->vchan.lock, flags); in sf_pdma_prep_dma_memcpy()
117 return desc->async_tx; in sf_pdma_prep_dma_memcpy()
125 memcpy(&chan->cfg, cfg, sizeof(*cfg)); in sf_pdma_slave_config()
133 struct pdma_regs *regs = &chan->regs; in sf_pdma_alloc_chan_resources()
136 writel(PDMA_CLAIM_MASK, regs->ctrl); in sf_pdma_alloc_chan_resources()
143 struct pdma_regs *regs = &chan->regs; in sf_pdma_disable_request()
145 writel(readl(regs->ctrl) & ~PDMA_RUN_MASK, regs->ctrl); in sf_pdma_disable_request()
154 spin_lock_irqsave(&chan->vchan.lock, flags); in sf_pdma_free_chan_resources()
156 kfree(chan->desc); in sf_pdma_free_chan_resources()
157 chan->desc = NULL; in sf_pdma_free_chan_resources()
158 vchan_get_all_descriptors(&chan->vchan, &head); in sf_pdma_free_chan_resources()
160 spin_unlock_irqrestore(&chan->vchan.lock, flags); in sf_pdma_free_chan_resources()
161 vchan_dma_desc_free_list(&chan->vchan, &head); in sf_pdma_free_chan_resources()
168 struct pdma_regs *regs = &chan->regs; in sf_pdma_desc_residue()
174 spin_lock_irqsave(&chan->vchan.lock, flags); in sf_pdma_desc_residue()
176 tx = &chan->desc->vdesc.tx; in sf_pdma_desc_residue()
177 if (cookie == tx->chan->completed_cookie) in sf_pdma_desc_residue()
180 if (cookie == tx->cookie) { in sf_pdma_desc_residue()
181 residue = readq(regs->residue); in sf_pdma_desc_residue()
183 vd = vchan_find_desc(&chan->vchan, cookie); in sf_pdma_desc_residue()
188 residue = desc->xfer_size; in sf_pdma_desc_residue()
192 spin_unlock_irqrestore(&chan->vchan.lock, flags); in sf_pdma_desc_residue()
218 spin_lock_irqsave(&chan->vchan.lock, flags); in sf_pdma_terminate_all()
220 kfree(chan->desc); in sf_pdma_terminate_all()
221 chan->desc = NULL; in sf_pdma_terminate_all()
222 chan->xfer_err = false; in sf_pdma_terminate_all()
223 vchan_get_all_descriptors(&chan->vchan, &head); in sf_pdma_terminate_all()
224 spin_unlock_irqrestore(&chan->vchan.lock, flags); in sf_pdma_terminate_all()
225 vchan_dma_desc_free_list(&chan->vchan, &head); in sf_pdma_terminate_all()
232 struct pdma_regs *regs = &chan->regs; in sf_pdma_enable_request()
240 writel(v, regs->ctrl); in sf_pdma_enable_request()
245 struct sf_pdma_desc *desc = chan->desc; in sf_pdma_xfer_desc()
246 struct pdma_regs *regs = &chan->regs; in sf_pdma_xfer_desc()
249 dev_err(chan->pdma->dma_dev.dev, "NULL desc.\n"); in sf_pdma_xfer_desc()
253 writel(desc->xfer_type, regs->xfer_type); in sf_pdma_xfer_desc()
254 writeq(desc->xfer_size, regs->xfer_size); in sf_pdma_xfer_desc()
255 writeq(desc->dst_addr, regs->dst_addr); in sf_pdma_xfer_desc()
256 writeq(desc->src_addr, regs->src_addr); in sf_pdma_xfer_desc()
258 chan->desc = desc; in sf_pdma_xfer_desc()
259 chan->status = DMA_IN_PROGRESS; in sf_pdma_xfer_desc()
268 spin_lock_irqsave(&chan->vchan.lock, flags); in sf_pdma_issue_pending()
270 if (vchan_issue_pending(&chan->vchan) && chan->desc) in sf_pdma_issue_pending()
273 spin_unlock_irqrestore(&chan->vchan.lock, flags); in sf_pdma_issue_pending()
281 desc->in_use = false; in sf_pdma_free_desc()
289 spin_lock_irqsave(&chan->lock, flags); in sf_pdma_donebh_tasklet()
290 if (chan->xfer_err) { in sf_pdma_donebh_tasklet()
291 chan->retries = MAX_RETRY; in sf_pdma_donebh_tasklet()
292 chan->status = DMA_COMPLETE; in sf_pdma_donebh_tasklet()
293 chan->xfer_err = false; in sf_pdma_donebh_tasklet()
295 spin_unlock_irqrestore(&chan->lock, flags); in sf_pdma_donebh_tasklet()
297 spin_lock_irqsave(&chan->vchan.lock, flags); in sf_pdma_donebh_tasklet()
298 list_del(&chan->desc->vdesc.node); in sf_pdma_donebh_tasklet()
299 vchan_cookie_complete(&chan->desc->vdesc); in sf_pdma_donebh_tasklet()
300 spin_unlock_irqrestore(&chan->vchan.lock, flags); in sf_pdma_donebh_tasklet()
306 struct sf_pdma_desc *desc = chan->desc; in sf_pdma_errbh_tasklet()
309 spin_lock_irqsave(&chan->lock, flags); in sf_pdma_errbh_tasklet()
310 if (chan->retries <= 0) { in sf_pdma_errbh_tasklet()
312 spin_unlock_irqrestore(&chan->lock, flags); in sf_pdma_errbh_tasklet()
313 dmaengine_desc_get_callback_invoke(desc->async_tx, NULL); in sf_pdma_errbh_tasklet()
316 chan->retries--; in sf_pdma_errbh_tasklet()
317 chan->xfer_err = true; in sf_pdma_errbh_tasklet()
318 chan->status = DMA_ERROR; in sf_pdma_errbh_tasklet()
321 spin_unlock_irqrestore(&chan->lock, flags); in sf_pdma_errbh_tasklet()
328 struct pdma_regs *regs = &chan->regs; in sf_pdma_done_isr()
332 spin_lock_irqsave(&chan->vchan.lock, flags); in sf_pdma_done_isr()
333 writel((readl(regs->ctrl)) & ~PDMA_DONE_STATUS_MASK, regs->ctrl); in sf_pdma_done_isr()
334 residue = readq(regs->residue); in sf_pdma_done_isr()
337 tasklet_hi_schedule(&chan->done_tasklet); in sf_pdma_done_isr()
340 struct sf_pdma_desc *desc = chan->desc; in sf_pdma_done_isr()
342 desc->src_addr += desc->xfer_size - residue; in sf_pdma_done_isr()
343 desc->dst_addr += desc->xfer_size - residue; in sf_pdma_done_isr()
344 desc->xfer_size = residue; in sf_pdma_done_isr()
349 spin_unlock_irqrestore(&chan->vchan.lock, flags); in sf_pdma_done_isr()
357 struct pdma_regs *regs = &chan->regs; in sf_pdma_err_isr()
360 spin_lock_irqsave(&chan->lock, flags); in sf_pdma_err_isr()
361 writel((readl(regs->ctrl)) & ~PDMA_ERR_STATUS_MASK, regs->ctrl); in sf_pdma_err_isr()
362 spin_unlock_irqrestore(&chan->lock, flags); in sf_pdma_err_isr()
364 tasklet_schedule(&chan->err_tasklet); in sf_pdma_err_isr()
370 * sf_pdma_irq_init() - Init PDMA IRQ Handlers
375 * make sure the pointer passed in are non-NULL. This function should be called
381 * * 0 - OK to init all IRQ handlers
382 * * -EINVAL - Fail to request IRQ
389 for (i = 0; i < pdma->n_chans; i++) { in sf_pdma_irq_init()
390 chan = &pdma->chans[i]; in sf_pdma_irq_init()
394 dev_err(&pdev->dev, "ch(%d) Can't get done irq.\n", i); in sf_pdma_irq_init()
395 return -EINVAL; in sf_pdma_irq_init()
398 r = devm_request_irq(&pdev->dev, irq, sf_pdma_done_isr, 0, in sf_pdma_irq_init()
399 dev_name(&pdev->dev), (void *)chan); in sf_pdma_irq_init()
401 dev_err(&pdev->dev, "Fail to attach done ISR: %d\n", r); in sf_pdma_irq_init()
402 return -EINVAL; in sf_pdma_irq_init()
405 chan->txirq = irq; in sf_pdma_irq_init()
409 dev_err(&pdev->dev, "ch(%d) Can't get err irq.\n", i); in sf_pdma_irq_init()
410 return -EINVAL; in sf_pdma_irq_init()
413 r = devm_request_irq(&pdev->dev, irq, sf_pdma_err_isr, 0, in sf_pdma_irq_init()
414 dev_name(&pdev->dev), (void *)chan); in sf_pdma_irq_init()
416 dev_err(&pdev->dev, "Fail to attach err ISR: %d\n", r); in sf_pdma_irq_init()
417 return -EINVAL; in sf_pdma_irq_init()
420 chan->errirq = irq; in sf_pdma_irq_init()
427 * sf_pdma_setup_chans() - Init settings of each channel
431 * the pointer passed in are non-NULL. This function should be called only
443 INIT_LIST_HEAD(&pdma->dma_dev.channels); in sf_pdma_setup_chans()
445 for (i = 0; i < pdma->n_chans; i++) { in sf_pdma_setup_chans()
446 chan = &pdma->chans[i]; in sf_pdma_setup_chans()
448 chan->regs.ctrl = in sf_pdma_setup_chans()
450 chan->regs.xfer_type = in sf_pdma_setup_chans()
452 chan->regs.xfer_size = in sf_pdma_setup_chans()
454 chan->regs.dst_addr = in sf_pdma_setup_chans()
456 chan->regs.src_addr = in sf_pdma_setup_chans()
458 chan->regs.act_type = in sf_pdma_setup_chans()
460 chan->regs.residue = in sf_pdma_setup_chans()
462 chan->regs.cur_dst_addr = in sf_pdma_setup_chans()
464 chan->regs.cur_src_addr = in sf_pdma_setup_chans()
467 chan->pdma = pdma; in sf_pdma_setup_chans()
468 chan->pm_state = RUNNING; in sf_pdma_setup_chans()
469 chan->slave_id = i; in sf_pdma_setup_chans()
470 chan->xfer_err = false; in sf_pdma_setup_chans()
471 spin_lock_init(&chan->lock); in sf_pdma_setup_chans()
473 chan->vchan.desc_free = sf_pdma_free_desc; in sf_pdma_setup_chans()
474 vchan_init(&chan->vchan, &pdma->dma_dev); in sf_pdma_setup_chans()
476 writel(PDMA_CLEAR_CTRL, chan->regs.ctrl); in sf_pdma_setup_chans()
478 tasklet_setup(&chan->done_tasklet, sf_pdma_donebh_tasklet); in sf_pdma_setup_chans()
479 tasklet_setup(&chan->err_tasklet, sf_pdma_errbh_tasklet); in sf_pdma_setup_chans()
498 pdma = devm_kzalloc(&pdev->dev, len, GFP_KERNEL); in sf_pdma_probe()
500 return -ENOMEM; in sf_pdma_probe()
502 pdma->n_chans = chans; in sf_pdma_probe()
505 pdma->membase = devm_ioremap_resource(&pdev->dev, res); in sf_pdma_probe()
506 if (IS_ERR(pdma->membase)) in sf_pdma_probe()
507 return PTR_ERR(pdma->membase); in sf_pdma_probe()
515 pdma->dma_dev.dev = &pdev->dev; in sf_pdma_probe()
518 dma_cap_set(DMA_MEMCPY, pdma->dma_dev.cap_mask); in sf_pdma_probe()
519 pdma->dma_dev.copy_align = 2; in sf_pdma_probe()
520 pdma->dma_dev.src_addr_widths = widths; in sf_pdma_probe()
521 pdma->dma_dev.dst_addr_widths = widths; in sf_pdma_probe()
522 pdma->dma_dev.directions = BIT(DMA_MEM_TO_MEM); in sf_pdma_probe()
523 pdma->dma_dev.residue_granularity = DMA_RESIDUE_GRANULARITY_DESCRIPTOR; in sf_pdma_probe()
524 pdma->dma_dev.descriptor_reuse = true; in sf_pdma_probe()
527 pdma->dma_dev.device_alloc_chan_resources = in sf_pdma_probe()
529 pdma->dma_dev.device_free_chan_resources = in sf_pdma_probe()
531 pdma->dma_dev.device_tx_status = sf_pdma_tx_status; in sf_pdma_probe()
532 pdma->dma_dev.device_prep_dma_memcpy = sf_pdma_prep_dma_memcpy; in sf_pdma_probe()
533 pdma->dma_dev.device_config = sf_pdma_slave_config; in sf_pdma_probe()
534 pdma->dma_dev.device_terminate_all = sf_pdma_terminate_all; in sf_pdma_probe()
535 pdma->dma_dev.device_issue_pending = sf_pdma_issue_pending; in sf_pdma_probe()
539 ret = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)); in sf_pdma_probe()
541 dev_warn(&pdev->dev, in sf_pdma_probe()
544 ret = dma_async_device_register(&pdma->dma_dev); in sf_pdma_probe()
546 dev_err(&pdev->dev, in sf_pdma_probe()
561 ch = &pdma->chans[i]; in sf_pdma_remove()
563 devm_free_irq(&pdev->dev, ch->txirq, ch); in sf_pdma_remove()
564 devm_free_irq(&pdev->dev, ch->errirq, ch); in sf_pdma_remove()
565 list_del(&ch->vchan.chan.device_node); in sf_pdma_remove()
566 tasklet_kill(&ch->vchan.task); in sf_pdma_remove()
567 tasklet_kill(&ch->done_tasklet); in sf_pdma_remove()
568 tasklet_kill(&ch->err_tasklet); in sf_pdma_remove()
571 dma_async_device_unregister(&pdma->dma_dev); in sf_pdma_remove()
577 { .compatible = "sifive,fu540-c000-pdma" },
586 .name = "sf-pdma",