2 * DMA controller driver for CSR SiRFprimaII
4 * Copyright (c) 2011 Cambridge Silicon Radio Limited, a CSR plc group company.
6 * Licensed under GPLv2 or later.
9 #include <linux/module.h>
10 #include <linux/dmaengine.h>
11 #include <linux/dma-mapping.h>
12 #include <linux/interrupt.h>
14 #include <linux/slab.h>
15 #include <linux/of_irq.h>
16 #include <linux/of_address.h>
17 #include <linux/of_device.h>
18 #include <linux/of_platform.h>
19 #include <linux/sirfsoc_dma.h>
21 #define SIRFSOC_DMA_DESCRIPTORS 16
22 #define SIRFSOC_DMA_CHANNELS 16
24 #define SIRFSOC_DMA_CH_ADDR 0x00
25 #define SIRFSOC_DMA_CH_XLEN 0x04
26 #define SIRFSOC_DMA_CH_YLEN 0x08
27 #define SIRFSOC_DMA_CH_CTRL 0x0C
29 #define SIRFSOC_DMA_WIDTH_0 0x100
30 #define SIRFSOC_DMA_CH_VALID 0x140
31 #define SIRFSOC_DMA_CH_INT 0x144
32 #define SIRFSOC_DMA_INT_EN 0x148
33 #define SIRFSOC_DMA_CH_LOOP_CTRL 0x150
35 #define SIRFSOC_DMA_MODE_CTRL_BIT 4
36 #define SIRFSOC_DMA_DIR_CTRL_BIT 5
38 /* xlen and dma_width register is in 4 bytes boundary */
39 #define SIRFSOC_DMA_WORD_LEN 4
41 struct sirfsoc_dma_desc {
42 struct dma_async_tx_descriptor desc;
43 struct list_head node;
45 /* SiRFprimaII 2D-DMA parameters */
47 int xlen; /* DMA xlen */
48 int ylen; /* DMA ylen */
49 int width; /* DMA width */
51 bool cyclic; /* is loop DMA? */
52 u32 addr; /* DMA buffer address */
55 struct sirfsoc_dma_chan {
57 struct list_head free;
58 struct list_head prepared;
59 struct list_head queued;
60 struct list_head active;
61 struct list_head completed;
62 dma_cookie_t completed_cookie;
63 unsigned long happened_cyclic;
64 unsigned long completed_cyclic;
66 /* Lock for this structure */
73 struct dma_device dma;
74 struct tasklet_struct tasklet;
75 struct sirfsoc_dma_chan channels[SIRFSOC_DMA_CHANNELS];
80 #define DRV_NAME "sirfsoc_dma"
82 /* Convert struct dma_chan to struct sirfsoc_dma_chan */
84 struct sirfsoc_dma_chan *dma_chan_to_sirfsoc_dma_chan(struct dma_chan *c)
86 return container_of(c, struct sirfsoc_dma_chan, chan);
89 /* Convert struct dma_chan to struct sirfsoc_dma */
90 static inline struct sirfsoc_dma *dma_chan_to_sirfsoc_dma(struct dma_chan *c)
92 struct sirfsoc_dma_chan *schan = dma_chan_to_sirfsoc_dma_chan(c);
93 return container_of(schan, struct sirfsoc_dma, channels[c->chan_id]);
96 /* Execute all queued DMA descriptors */
97 static void sirfsoc_dma_execute(struct sirfsoc_dma_chan *schan)
99 struct sirfsoc_dma *sdma = dma_chan_to_sirfsoc_dma(&schan->chan);
100 int cid = schan->chan.chan_id;
101 struct sirfsoc_dma_desc *sdesc = NULL;
104 * lock has been held by functions calling this, so we don't hold
108 sdesc = list_first_entry(&schan->queued, struct sirfsoc_dma_desc,
110 /* Move the first queued descriptor to active list */
111 list_move_tail(&schan->queued, &schan->active);
113 /* Start the DMA transfer */
114 writel_relaxed(sdesc->width, sdma->base + SIRFSOC_DMA_WIDTH_0 +
116 writel_relaxed(cid | (schan->mode << SIRFSOC_DMA_MODE_CTRL_BIT) |
117 (sdesc->dir << SIRFSOC_DMA_DIR_CTRL_BIT),
118 sdma->base + cid * 0x10 + SIRFSOC_DMA_CH_CTRL);
119 writel_relaxed(sdesc->xlen, sdma->base + cid * 0x10 +
120 SIRFSOC_DMA_CH_XLEN);
121 writel_relaxed(sdesc->ylen, sdma->base + cid * 0x10 +
122 SIRFSOC_DMA_CH_YLEN);
123 writel_relaxed(readl_relaxed(sdma->base + SIRFSOC_DMA_INT_EN) |
124 (1 << cid), sdma->base + SIRFSOC_DMA_INT_EN);
127 * writel has an implict memory write barrier to make sure data is
128 * flushed into memory before starting DMA
130 writel(sdesc->addr >> 2, sdma->base + cid * 0x10 + SIRFSOC_DMA_CH_ADDR);
133 writel((1 << cid) | 1 << (cid + 16) |
134 readl_relaxed(sdma->base + SIRFSOC_DMA_CH_LOOP_CTRL),
135 sdma->base + SIRFSOC_DMA_CH_LOOP_CTRL);
136 schan->happened_cyclic = schan->completed_cyclic = 0;
140 /* Interrupt handler */
141 static irqreturn_t sirfsoc_dma_irq(int irq, void *data)
143 struct sirfsoc_dma *sdma = data;
144 struct sirfsoc_dma_chan *schan;
145 struct sirfsoc_dma_desc *sdesc = NULL;
149 is = readl(sdma->base + SIRFSOC_DMA_CH_INT);
150 while ((ch = fls(is) - 1) >= 0) {
152 writel_relaxed(1 << ch, sdma->base + SIRFSOC_DMA_CH_INT);
153 schan = &sdma->channels[ch];
155 spin_lock(&schan->lock);
157 sdesc = list_first_entry(&schan->active, struct sirfsoc_dma_desc,
159 if (!sdesc->cyclic) {
160 /* Execute queued descriptors */
161 list_splice_tail_init(&schan->active, &schan->completed);
162 if (!list_empty(&schan->queued))
163 sirfsoc_dma_execute(schan);
165 schan->happened_cyclic++;
167 spin_unlock(&schan->lock);
170 /* Schedule tasklet */
171 tasklet_schedule(&sdma->tasklet);
176 /* process completed descriptors */
177 static void sirfsoc_dma_process_completed(struct sirfsoc_dma *sdma)
179 dma_cookie_t last_cookie = 0;
180 struct sirfsoc_dma_chan *schan;
181 struct sirfsoc_dma_desc *sdesc;
182 struct dma_async_tx_descriptor *desc;
184 unsigned long happened_cyclic;
188 for (i = 0; i < sdma->dma.chancnt; i++) {
189 schan = &sdma->channels[i];
191 /* Get all completed descriptors */
192 spin_lock_irqsave(&schan->lock, flags);
193 if (!list_empty(&schan->completed)) {
194 list_splice_tail_init(&schan->completed, &list);
195 spin_unlock_irqrestore(&schan->lock, flags);
197 /* Execute callbacks and run dependencies */
198 list_for_each_entry(sdesc, &list, node) {
202 desc->callback(desc->callback_param);
204 last_cookie = desc->cookie;
205 dma_run_dependencies(desc);
208 /* Free descriptors */
209 spin_lock_irqsave(&schan->lock, flags);
210 list_splice_tail_init(&list, &schan->free);
211 schan->completed_cookie = last_cookie;
212 spin_unlock_irqrestore(&schan->lock, flags);
214 /* for cyclic channel, desc is always in active list */
215 sdesc = list_first_entry(&schan->active, struct sirfsoc_dma_desc,
218 if (!sdesc || (sdesc && !sdesc->cyclic)) {
219 /* without active cyclic DMA */
220 spin_unlock_irqrestore(&schan->lock, flags);
225 happened_cyclic = schan->happened_cyclic;
226 spin_unlock_irqrestore(&schan->lock, flags);
229 while (happened_cyclic != schan->completed_cyclic) {
231 desc->callback(desc->callback_param);
232 schan->completed_cyclic++;
239 static void sirfsoc_dma_tasklet(unsigned long data)
241 struct sirfsoc_dma *sdma = (void *)data;
243 sirfsoc_dma_process_completed(sdma);
246 /* Submit descriptor to hardware */
247 static dma_cookie_t sirfsoc_dma_tx_submit(struct dma_async_tx_descriptor *txd)
249 struct sirfsoc_dma_chan *schan = dma_chan_to_sirfsoc_dma_chan(txd->chan);
250 struct sirfsoc_dma_desc *sdesc;
254 sdesc = container_of(txd, struct sirfsoc_dma_desc, desc);
256 spin_lock_irqsave(&schan->lock, flags);
258 /* Move descriptor to queue */
259 list_move_tail(&sdesc->node, &schan->queued);
262 cookie = schan->chan.cookie + 1;
266 schan->chan.cookie = cookie;
267 sdesc->desc.cookie = cookie;
269 spin_unlock_irqrestore(&schan->lock, flags);
274 static int sirfsoc_dma_slave_config(struct sirfsoc_dma_chan *schan,
275 struct dma_slave_config *config)
279 if ((config->src_addr_width != DMA_SLAVE_BUSWIDTH_4_BYTES) ||
280 (config->dst_addr_width != DMA_SLAVE_BUSWIDTH_4_BYTES))
283 spin_lock_irqsave(&schan->lock, flags);
284 schan->mode = (config->src_maxburst == 4 ? 1 : 0);
285 spin_unlock_irqrestore(&schan->lock, flags);
290 static int sirfsoc_dma_terminate_all(struct sirfsoc_dma_chan *schan)
292 struct sirfsoc_dma *sdma = dma_chan_to_sirfsoc_dma(&schan->chan);
293 int cid = schan->chan.chan_id;
296 writel_relaxed(readl_relaxed(sdma->base + SIRFSOC_DMA_INT_EN) &
297 ~(1 << cid), sdma->base + SIRFSOC_DMA_INT_EN);
298 writel_relaxed(1 << cid, sdma->base + SIRFSOC_DMA_CH_VALID);
300 writel_relaxed(readl_relaxed(sdma->base + SIRFSOC_DMA_CH_LOOP_CTRL)
301 & ~((1 << cid) | 1 << (cid + 16)),
302 sdma->base + SIRFSOC_DMA_CH_LOOP_CTRL);
304 spin_lock_irqsave(&schan->lock, flags);
305 list_splice_tail_init(&schan->active, &schan->free);
306 list_splice_tail_init(&schan->queued, &schan->free);
307 spin_unlock_irqrestore(&schan->lock, flags);
312 static int sirfsoc_dma_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd,
315 struct dma_slave_config *config;
316 struct sirfsoc_dma_chan *schan = dma_chan_to_sirfsoc_dma_chan(chan);
319 case DMA_TERMINATE_ALL:
320 return sirfsoc_dma_terminate_all(schan);
321 case DMA_SLAVE_CONFIG:
322 config = (struct dma_slave_config *)arg;
323 return sirfsoc_dma_slave_config(schan, config);
332 /* Alloc channel resources */
333 static int sirfsoc_dma_alloc_chan_resources(struct dma_chan *chan)
335 struct sirfsoc_dma *sdma = dma_chan_to_sirfsoc_dma(chan);
336 struct sirfsoc_dma_chan *schan = dma_chan_to_sirfsoc_dma_chan(chan);
337 struct sirfsoc_dma_desc *sdesc;
342 /* Alloc descriptors for this channel */
343 for (i = 0; i < SIRFSOC_DMA_DESCRIPTORS; i++) {
344 sdesc = kzalloc(sizeof(*sdesc), GFP_KERNEL);
346 dev_notice(sdma->dma.dev, "Memory allocation error. "
347 "Allocated only %u descriptors\n", i);
351 dma_async_tx_descriptor_init(&sdesc->desc, chan);
352 sdesc->desc.flags = DMA_CTRL_ACK;
353 sdesc->desc.tx_submit = sirfsoc_dma_tx_submit;
355 list_add_tail(&sdesc->node, &descs);
358 /* Return error only if no descriptors were allocated */
362 spin_lock_irqsave(&schan->lock, flags);
364 list_splice_tail_init(&descs, &schan->free);
365 spin_unlock_irqrestore(&schan->lock, flags);
370 /* Free channel resources */
371 static void sirfsoc_dma_free_chan_resources(struct dma_chan *chan)
373 struct sirfsoc_dma_chan *schan = dma_chan_to_sirfsoc_dma_chan(chan);
374 struct sirfsoc_dma_desc *sdesc, *tmp;
378 spin_lock_irqsave(&schan->lock, flags);
380 /* Channel must be idle */
381 BUG_ON(!list_empty(&schan->prepared));
382 BUG_ON(!list_empty(&schan->queued));
383 BUG_ON(!list_empty(&schan->active));
384 BUG_ON(!list_empty(&schan->completed));
387 list_splice_tail_init(&schan->free, &descs);
389 spin_unlock_irqrestore(&schan->lock, flags);
391 /* Free descriptors */
392 list_for_each_entry_safe(sdesc, tmp, &descs, node)
396 /* Send pending descriptor to hardware */
397 static void sirfsoc_dma_issue_pending(struct dma_chan *chan)
399 struct sirfsoc_dma_chan *schan = dma_chan_to_sirfsoc_dma_chan(chan);
402 spin_lock_irqsave(&schan->lock, flags);
404 if (list_empty(&schan->active) && !list_empty(&schan->queued))
405 sirfsoc_dma_execute(schan);
407 spin_unlock_irqrestore(&schan->lock, flags);
410 /* Check request completion status */
411 static enum dma_status
412 sirfsoc_dma_tx_status(struct dma_chan *chan, dma_cookie_t cookie,
413 struct dma_tx_state *txstate)
415 struct sirfsoc_dma_chan *schan = dma_chan_to_sirfsoc_dma_chan(chan);
417 dma_cookie_t last_used;
418 dma_cookie_t last_complete;
420 spin_lock_irqsave(&schan->lock, flags);
421 last_used = schan->chan.cookie;
422 last_complete = schan->completed_cookie;
423 spin_unlock_irqrestore(&schan->lock, flags);
425 dma_set_tx_state(txstate, last_complete, last_used, 0);
426 return dma_async_is_complete(cookie, last_complete, last_used);
429 static struct dma_async_tx_descriptor *sirfsoc_dma_prep_interleaved(
430 struct dma_chan *chan, struct dma_interleaved_template *xt,
433 struct sirfsoc_dma *sdma = dma_chan_to_sirfsoc_dma(chan);
434 struct sirfsoc_dma_chan *schan = dma_chan_to_sirfsoc_dma_chan(chan);
435 struct sirfsoc_dma_desc *sdesc = NULL;
436 unsigned long iflags;
439 if ((xt->dir != DMA_MEM_TO_DEV) || (xt->dir != DMA_DEV_TO_MEM)) {
444 /* Get free descriptor */
445 spin_lock_irqsave(&schan->lock, iflags);
446 if (!list_empty(&schan->free)) {
447 sdesc = list_first_entry(&schan->free, struct sirfsoc_dma_desc,
449 list_del(&sdesc->node);
451 spin_unlock_irqrestore(&schan->lock, iflags);
454 /* try to free completed descriptors */
455 sirfsoc_dma_process_completed(sdma);
460 /* Place descriptor in prepared list */
461 spin_lock_irqsave(&schan->lock, iflags);
464 * Number of chunks in a frame can only be 1 for prima2
465 * and ylen (number of frame - 1) must be at least 0
467 if ((xt->frame_size == 1) && (xt->numf > 0)) {
469 sdesc->xlen = xt->sgl[0].size / SIRFSOC_DMA_WORD_LEN;
470 sdesc->width = (xt->sgl[0].size + xt->sgl[0].icg) /
471 SIRFSOC_DMA_WORD_LEN;
472 sdesc->ylen = xt->numf - 1;
473 if (xt->dir == DMA_MEM_TO_DEV) {
474 sdesc->addr = xt->src_start;
477 sdesc->addr = xt->dst_start;
481 list_add_tail(&sdesc->node, &schan->prepared);
483 pr_err("sirfsoc DMA Invalid xfer\n");
487 spin_unlock_irqrestore(&schan->lock, iflags);
491 spin_unlock_irqrestore(&schan->lock, iflags);
497 static struct dma_async_tx_descriptor *
498 sirfsoc_dma_prep_cyclic(struct dma_chan *chan, dma_addr_t addr,
499 size_t buf_len, size_t period_len,
500 enum dma_transfer_direction direction)
502 struct sirfsoc_dma_chan *schan = dma_chan_to_sirfsoc_dma_chan(chan);
503 struct sirfsoc_dma_desc *sdesc = NULL;
504 unsigned long iflags;
507 * we only support cycle transfer with 2 period
508 * If the X-length is set to 0, it would be the loop mode.
509 * The DMA address keeps increasing until reaching the end of a loop
510 * area whose size is defined by (DMA_WIDTH x (Y_LENGTH + 1)). Then
511 * the DMA address goes back to the beginning of this area.
512 * In loop mode, the DMA data region is divided into two parts, BUFA
513 * and BUFB. DMA controller generates interrupts twice in each loop:
514 * when the DMA address reaches the end of BUFA or the end of the
517 if (buf_len != 2 * period_len)
518 return ERR_PTR(-EINVAL);
520 /* Get free descriptor */
521 spin_lock_irqsave(&schan->lock, iflags);
522 if (!list_empty(&schan->free)) {
523 sdesc = list_first_entry(&schan->free, struct sirfsoc_dma_desc,
525 list_del(&sdesc->node);
527 spin_unlock_irqrestore(&schan->lock, iflags);
532 /* Place descriptor in prepared list */
533 spin_lock_irqsave(&schan->lock, iflags);
537 sdesc->ylen = buf_len / SIRFSOC_DMA_WORD_LEN - 1;
539 list_add_tail(&sdesc->node, &schan->prepared);
540 spin_unlock_irqrestore(&schan->lock, iflags);
546 * The DMA controller consists of 16 independent DMA channels.
547 * Each channel is allocated to a different function
549 bool sirfsoc_dma_filter_id(struct dma_chan *chan, void *chan_id)
551 unsigned int ch_nr = (unsigned int) chan_id;
553 if (ch_nr == chan->chan_id +
554 chan->device->dev_id * SIRFSOC_DMA_CHANNELS)
559 EXPORT_SYMBOL(sirfsoc_dma_filter_id);
561 static int __devinit sirfsoc_dma_probe(struct platform_device *op)
563 struct device_node *dn = op->dev.of_node;
564 struct device *dev = &op->dev;
565 struct dma_device *dma;
566 struct sirfsoc_dma *sdma;
567 struct sirfsoc_dma_chan *schan;
569 ulong regs_start, regs_size;
573 sdma = devm_kzalloc(dev, sizeof(*sdma), GFP_KERNEL);
575 dev_err(dev, "Memory exhausted!\n");
579 if (of_property_read_u32(dn, "cell-index", &id)) {
580 dev_err(dev, "Fail to get DMAC index\n");
585 sdma->irq = irq_of_parse_and_map(dn, 0);
586 if (sdma->irq == NO_IRQ) {
587 dev_err(dev, "Error mapping IRQ!\n");
592 ret = of_address_to_resource(dn, 0, &res);
594 dev_err(dev, "Error parsing memory region!\n");
598 regs_start = res.start;
599 regs_size = resource_size(&res);
601 sdma->base = devm_ioremap(dev, regs_start, regs_size);
603 dev_err(dev, "Error mapping memory region!\n");
608 ret = devm_request_irq(dev, sdma->irq, &sirfsoc_dma_irq, 0, DRV_NAME,
611 dev_err(dev, "Error requesting IRQ!\n");
618 dma->chancnt = SIRFSOC_DMA_CHANNELS;
620 dma->device_alloc_chan_resources = sirfsoc_dma_alloc_chan_resources;
621 dma->device_free_chan_resources = sirfsoc_dma_free_chan_resources;
622 dma->device_issue_pending = sirfsoc_dma_issue_pending;
623 dma->device_control = sirfsoc_dma_control;
624 dma->device_tx_status = sirfsoc_dma_tx_status;
625 dma->device_prep_interleaved_dma = sirfsoc_dma_prep_interleaved;
626 dma->device_prep_dma_cyclic = sirfsoc_dma_prep_cyclic;
628 INIT_LIST_HEAD(&dma->channels);
629 dma_cap_set(DMA_SLAVE, dma->cap_mask);
630 dma_cap_set(DMA_CYCLIC, dma->cap_mask);
631 dma_cap_set(DMA_INTERLEAVE, dma->cap_mask);
632 dma_cap_set(DMA_PRIVATE, dma->cap_mask);
634 for (i = 0; i < dma->chancnt; i++) {
635 schan = &sdma->channels[i];
637 schan->chan.device = dma;
638 schan->chan.cookie = 1;
639 schan->completed_cookie = schan->chan.cookie;
641 INIT_LIST_HEAD(&schan->free);
642 INIT_LIST_HEAD(&schan->prepared);
643 INIT_LIST_HEAD(&schan->queued);
644 INIT_LIST_HEAD(&schan->active);
645 INIT_LIST_HEAD(&schan->completed);
647 spin_lock_init(&schan->lock);
648 list_add_tail(&schan->chan.device_node, &dma->channels);
651 tasklet_init(&sdma->tasklet, sirfsoc_dma_tasklet, (unsigned long)sdma);
653 /* Register DMA engine */
654 dev_set_drvdata(dev, sdma);
655 ret = dma_async_device_register(dma);
659 dev_info(dev, "initialized SIRFSOC DMAC driver\n");
664 devm_free_irq(dev, sdma->irq, sdma);
666 irq_dispose_mapping(sdma->irq);
670 devm_kfree(dev, sdma);
674 static int __devexit sirfsoc_dma_remove(struct platform_device *op)
676 struct device *dev = &op->dev;
677 struct sirfsoc_dma *sdma = dev_get_drvdata(dev);
679 dma_async_device_unregister(&sdma->dma);
680 devm_free_irq(dev, sdma->irq, sdma);
681 irq_dispose_mapping(sdma->irq);
683 devm_kfree(dev, sdma);
687 static struct of_device_id sirfsoc_dma_match[] = {
688 { .compatible = "sirf,prima2-dmac", },
692 static struct platform_driver sirfsoc_dma_driver = {
693 .probe = sirfsoc_dma_probe,
694 .remove = __devexit_p(sirfsoc_dma_remove),
697 .owner = THIS_MODULE,
698 .of_match_table = sirfsoc_dma_match,
702 module_platform_driver(sirfsoc_dma_driver);
704 MODULE_AUTHOR("Rongjun Ying <rongjun.ying@csr.com>, "
705 "Barry Song <baohua.song@csr.com>");
706 MODULE_DESCRIPTION("SIRFSOC DMA control driver");
707 MODULE_LICENSE("GPL v2");