blob: 3c210ba9f93853b269a9f9fdaac097341d1ed9e8 [file] [log] [blame]
Rongjun Yingca21a142011-10-27 19:22:39 -07001/*
2 * DMA controller driver for CSR SiRFprimaII
3 *
4 * Copyright (c) 2011 Cambridge Silicon Radio Limited, a CSR plc group company.
5 *
6 * Licensed under GPLv2 or later.
7 */
8
9#include <linux/module.h>
10#include <linux/dmaengine.h>
11#include <linux/dma-mapping.h>
12#include <linux/interrupt.h>
13#include <linux/io.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>
20
Vinod Koul949ff5b2012-03-13 11:58:12 +053021#include "dmaengine.h"
22
Rongjun Yingca21a142011-10-27 19:22:39 -070023#define SIRFSOC_DMA_DESCRIPTORS 16
24#define SIRFSOC_DMA_CHANNELS 16
25
26#define SIRFSOC_DMA_CH_ADDR 0x00
27#define SIRFSOC_DMA_CH_XLEN 0x04
28#define SIRFSOC_DMA_CH_YLEN 0x08
29#define SIRFSOC_DMA_CH_CTRL 0x0C
30
31#define SIRFSOC_DMA_WIDTH_0 0x100
32#define SIRFSOC_DMA_CH_VALID 0x140
33#define SIRFSOC_DMA_CH_INT 0x144
34#define SIRFSOC_DMA_INT_EN 0x148
Barry Songf7d935d2012-11-01 22:54:43 +080035#define SIRFSOC_DMA_INT_EN_CLR 0x14C
Rongjun Yingca21a142011-10-27 19:22:39 -070036#define SIRFSOC_DMA_CH_LOOP_CTRL 0x150
Barry Songf7d935d2012-11-01 22:54:43 +080037#define SIRFSOC_DMA_CH_LOOP_CTRL_CLR 0x15C
Rongjun Yingca21a142011-10-27 19:22:39 -070038
39#define SIRFSOC_DMA_MODE_CTRL_BIT 4
40#define SIRFSOC_DMA_DIR_CTRL_BIT 5
41
42/* xlen and dma_width register is in 4 bytes boundary */
43#define SIRFSOC_DMA_WORD_LEN 4
44
45struct sirfsoc_dma_desc {
46 struct dma_async_tx_descriptor desc;
47 struct list_head node;
48
49 /* SiRFprimaII 2D-DMA parameters */
50
51 int xlen; /* DMA xlen */
52 int ylen; /* DMA ylen */
53 int width; /* DMA width */
54 int dir;
55 bool cyclic; /* is loop DMA? */
56 u32 addr; /* DMA buffer address */
57};
58
59struct sirfsoc_dma_chan {
60 struct dma_chan chan;
61 struct list_head free;
62 struct list_head prepared;
63 struct list_head queued;
64 struct list_head active;
65 struct list_head completed;
Rongjun Yingca21a142011-10-27 19:22:39 -070066 unsigned long happened_cyclic;
67 unsigned long completed_cyclic;
68
69 /* Lock for this structure */
70 spinlock_t lock;
71
72 int mode;
73};
74
75struct sirfsoc_dma {
76 struct dma_device dma;
77 struct tasklet_struct tasklet;
78 struct sirfsoc_dma_chan channels[SIRFSOC_DMA_CHANNELS];
79 void __iomem *base;
80 int irq;
Barry Songf7d935d2012-11-01 22:54:43 +080081 bool is_marco;
Rongjun Yingca21a142011-10-27 19:22:39 -070082};
83
84#define DRV_NAME "sirfsoc_dma"
85
86/* Convert struct dma_chan to struct sirfsoc_dma_chan */
87static inline
88struct sirfsoc_dma_chan *dma_chan_to_sirfsoc_dma_chan(struct dma_chan *c)
89{
90 return container_of(c, struct sirfsoc_dma_chan, chan);
91}
92
93/* Convert struct dma_chan to struct sirfsoc_dma */
94static inline struct sirfsoc_dma *dma_chan_to_sirfsoc_dma(struct dma_chan *c)
95{
96 struct sirfsoc_dma_chan *schan = dma_chan_to_sirfsoc_dma_chan(c);
97 return container_of(schan, struct sirfsoc_dma, channels[c->chan_id]);
98}
99
100/* Execute all queued DMA descriptors */
101static void sirfsoc_dma_execute(struct sirfsoc_dma_chan *schan)
102{
103 struct sirfsoc_dma *sdma = dma_chan_to_sirfsoc_dma(&schan->chan);
104 int cid = schan->chan.chan_id;
105 struct sirfsoc_dma_desc *sdesc = NULL;
106
107 /*
108 * lock has been held by functions calling this, so we don't hold
109 * lock again
110 */
111
112 sdesc = list_first_entry(&schan->queued, struct sirfsoc_dma_desc,
113 node);
114 /* Move the first queued descriptor to active list */
Barry Song26fd1222012-09-27 16:36:10 +0800115 list_move_tail(&sdesc->node, &schan->active);
Rongjun Yingca21a142011-10-27 19:22:39 -0700116
117 /* Start the DMA transfer */
118 writel_relaxed(sdesc->width, sdma->base + SIRFSOC_DMA_WIDTH_0 +
119 cid * 4);
120 writel_relaxed(cid | (schan->mode << SIRFSOC_DMA_MODE_CTRL_BIT) |
121 (sdesc->dir << SIRFSOC_DMA_DIR_CTRL_BIT),
122 sdma->base + cid * 0x10 + SIRFSOC_DMA_CH_CTRL);
123 writel_relaxed(sdesc->xlen, sdma->base + cid * 0x10 +
124 SIRFSOC_DMA_CH_XLEN);
125 writel_relaxed(sdesc->ylen, sdma->base + cid * 0x10 +
126 SIRFSOC_DMA_CH_YLEN);
127 writel_relaxed(readl_relaxed(sdma->base + SIRFSOC_DMA_INT_EN) |
128 (1 << cid), sdma->base + SIRFSOC_DMA_INT_EN);
129
130 /*
131 * writel has an implict memory write barrier to make sure data is
132 * flushed into memory before starting DMA
133 */
134 writel(sdesc->addr >> 2, sdma->base + cid * 0x10 + SIRFSOC_DMA_CH_ADDR);
135
136 if (sdesc->cyclic) {
137 writel((1 << cid) | 1 << (cid + 16) |
138 readl_relaxed(sdma->base + SIRFSOC_DMA_CH_LOOP_CTRL),
139 sdma->base + SIRFSOC_DMA_CH_LOOP_CTRL);
140 schan->happened_cyclic = schan->completed_cyclic = 0;
141 }
142}
143
144/* Interrupt handler */
145static irqreturn_t sirfsoc_dma_irq(int irq, void *data)
146{
147 struct sirfsoc_dma *sdma = data;
148 struct sirfsoc_dma_chan *schan;
149 struct sirfsoc_dma_desc *sdesc = NULL;
150 u32 is;
151 int ch;
152
153 is = readl(sdma->base + SIRFSOC_DMA_CH_INT);
154 while ((ch = fls(is) - 1) >= 0) {
155 is &= ~(1 << ch);
156 writel_relaxed(1 << ch, sdma->base + SIRFSOC_DMA_CH_INT);
157 schan = &sdma->channels[ch];
158
159 spin_lock(&schan->lock);
160
161 sdesc = list_first_entry(&schan->active, struct sirfsoc_dma_desc,
162 node);
163 if (!sdesc->cyclic) {
164 /* Execute queued descriptors */
165 list_splice_tail_init(&schan->active, &schan->completed);
166 if (!list_empty(&schan->queued))
167 sirfsoc_dma_execute(schan);
168 } else
169 schan->happened_cyclic++;
170
171 spin_unlock(&schan->lock);
172 }
173
174 /* Schedule tasklet */
175 tasklet_schedule(&sdma->tasklet);
176
177 return IRQ_HANDLED;
178}
179
180/* process completed descriptors */
181static void sirfsoc_dma_process_completed(struct sirfsoc_dma *sdma)
182{
183 dma_cookie_t last_cookie = 0;
184 struct sirfsoc_dma_chan *schan;
185 struct sirfsoc_dma_desc *sdesc;
186 struct dma_async_tx_descriptor *desc;
187 unsigned long flags;
188 unsigned long happened_cyclic;
189 LIST_HEAD(list);
190 int i;
191
192 for (i = 0; i < sdma->dma.chancnt; i++) {
193 schan = &sdma->channels[i];
194
195 /* Get all completed descriptors */
196 spin_lock_irqsave(&schan->lock, flags);
197 if (!list_empty(&schan->completed)) {
198 list_splice_tail_init(&schan->completed, &list);
199 spin_unlock_irqrestore(&schan->lock, flags);
200
201 /* Execute callbacks and run dependencies */
202 list_for_each_entry(sdesc, &list, node) {
203 desc = &sdesc->desc;
204
205 if (desc->callback)
206 desc->callback(desc->callback_param);
207
208 last_cookie = desc->cookie;
209 dma_run_dependencies(desc);
210 }
211
212 /* Free descriptors */
213 spin_lock_irqsave(&schan->lock, flags);
214 list_splice_tail_init(&list, &schan->free);
Russell King - ARM Linux4d4e58d2012-03-06 22:34:06 +0000215 schan->chan.completed_cookie = last_cookie;
Rongjun Yingca21a142011-10-27 19:22:39 -0700216 spin_unlock_irqrestore(&schan->lock, flags);
217 } else {
218 /* for cyclic channel, desc is always in active list */
219 sdesc = list_first_entry(&schan->active, struct sirfsoc_dma_desc,
220 node);
221
222 if (!sdesc || (sdesc && !sdesc->cyclic)) {
223 /* without active cyclic DMA */
224 spin_unlock_irqrestore(&schan->lock, flags);
225 continue;
226 }
227
228 /* cyclic DMA */
229 happened_cyclic = schan->happened_cyclic;
230 spin_unlock_irqrestore(&schan->lock, flags);
231
232 desc = &sdesc->desc;
233 while (happened_cyclic != schan->completed_cyclic) {
234 if (desc->callback)
235 desc->callback(desc->callback_param);
236 schan->completed_cyclic++;
237 }
238 }
239 }
240}
241
242/* DMA Tasklet */
243static void sirfsoc_dma_tasklet(unsigned long data)
244{
245 struct sirfsoc_dma *sdma = (void *)data;
246
247 sirfsoc_dma_process_completed(sdma);
248}
249
250/* Submit descriptor to hardware */
251static dma_cookie_t sirfsoc_dma_tx_submit(struct dma_async_tx_descriptor *txd)
252{
253 struct sirfsoc_dma_chan *schan = dma_chan_to_sirfsoc_dma_chan(txd->chan);
254 struct sirfsoc_dma_desc *sdesc;
255 unsigned long flags;
256 dma_cookie_t cookie;
257
258 sdesc = container_of(txd, struct sirfsoc_dma_desc, desc);
259
260 spin_lock_irqsave(&schan->lock, flags);
261
262 /* Move descriptor to queue */
263 list_move_tail(&sdesc->node, &schan->queued);
264
Russell King - ARM Linux884485e2012-03-06 22:34:46 +0000265 cookie = dma_cookie_assign(txd);
Rongjun Yingca21a142011-10-27 19:22:39 -0700266
267 spin_unlock_irqrestore(&schan->lock, flags);
268
269 return cookie;
270}
271
272static int sirfsoc_dma_slave_config(struct sirfsoc_dma_chan *schan,
273 struct dma_slave_config *config)
274{
275 unsigned long flags;
276
277 if ((config->src_addr_width != DMA_SLAVE_BUSWIDTH_4_BYTES) ||
278 (config->dst_addr_width != DMA_SLAVE_BUSWIDTH_4_BYTES))
279 return -EINVAL;
280
281 spin_lock_irqsave(&schan->lock, flags);
282 schan->mode = (config->src_maxburst == 4 ? 1 : 0);
283 spin_unlock_irqrestore(&schan->lock, flags);
284
285 return 0;
286}
287
288static int sirfsoc_dma_terminate_all(struct sirfsoc_dma_chan *schan)
289{
290 struct sirfsoc_dma *sdma = dma_chan_to_sirfsoc_dma(&schan->chan);
291 int cid = schan->chan.chan_id;
292 unsigned long flags;
293
Barry Songf7d935d2012-11-01 22:54:43 +0800294 if (!sdma->is_marco) {
295 writel_relaxed(readl_relaxed(sdma->base + SIRFSOC_DMA_INT_EN) &
296 ~(1 << cid), sdma->base + SIRFSOC_DMA_INT_EN);
297 writel_relaxed(readl_relaxed(sdma->base + SIRFSOC_DMA_CH_LOOP_CTRL)
298 & ~((1 << cid) | 1 << (cid + 16)),
Rongjun Yingca21a142011-10-27 19:22:39 -0700299 sdma->base + SIRFSOC_DMA_CH_LOOP_CTRL);
Barry Songf7d935d2012-11-01 22:54:43 +0800300 } else {
301 writel_relaxed(1 << cid, sdma->base + SIRFSOC_DMA_INT_EN_CLR);
302 writel_relaxed((1 << cid) | 1 << (cid + 16),
303 sdma->base + SIRFSOC_DMA_CH_LOOP_CTRL_CLR);
304 }
305
306 writel_relaxed(1 << cid, sdma->base + SIRFSOC_DMA_CH_VALID);
Rongjun Yingca21a142011-10-27 19:22:39 -0700307
308 spin_lock_irqsave(&schan->lock, flags);
309 list_splice_tail_init(&schan->active, &schan->free);
310 list_splice_tail_init(&schan->queued, &schan->free);
311 spin_unlock_irqrestore(&schan->lock, flags);
312
313 return 0;
314}
315
316static int sirfsoc_dma_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd,
317 unsigned long arg)
318{
319 struct dma_slave_config *config;
320 struct sirfsoc_dma_chan *schan = dma_chan_to_sirfsoc_dma_chan(chan);
321
322 switch (cmd) {
323 case DMA_TERMINATE_ALL:
324 return sirfsoc_dma_terminate_all(schan);
325 case DMA_SLAVE_CONFIG:
326 config = (struct dma_slave_config *)arg;
327 return sirfsoc_dma_slave_config(schan, config);
328
329 default:
330 break;
331 }
332
333 return -ENOSYS;
334}
335
336/* Alloc channel resources */
337static int sirfsoc_dma_alloc_chan_resources(struct dma_chan *chan)
338{
339 struct sirfsoc_dma *sdma = dma_chan_to_sirfsoc_dma(chan);
340 struct sirfsoc_dma_chan *schan = dma_chan_to_sirfsoc_dma_chan(chan);
341 struct sirfsoc_dma_desc *sdesc;
342 unsigned long flags;
343 LIST_HEAD(descs);
344 int i;
345
346 /* Alloc descriptors for this channel */
347 for (i = 0; i < SIRFSOC_DMA_DESCRIPTORS; i++) {
348 sdesc = kzalloc(sizeof(*sdesc), GFP_KERNEL);
349 if (!sdesc) {
350 dev_notice(sdma->dma.dev, "Memory allocation error. "
351 "Allocated only %u descriptors\n", i);
352 break;
353 }
354
355 dma_async_tx_descriptor_init(&sdesc->desc, chan);
356 sdesc->desc.flags = DMA_CTRL_ACK;
357 sdesc->desc.tx_submit = sirfsoc_dma_tx_submit;
358
359 list_add_tail(&sdesc->node, &descs);
360 }
361
362 /* Return error only if no descriptors were allocated */
363 if (i == 0)
364 return -ENOMEM;
365
366 spin_lock_irqsave(&schan->lock, flags);
367
368 list_splice_tail_init(&descs, &schan->free);
369 spin_unlock_irqrestore(&schan->lock, flags);
370
371 return i;
372}
373
374/* Free channel resources */
375static void sirfsoc_dma_free_chan_resources(struct dma_chan *chan)
376{
377 struct sirfsoc_dma_chan *schan = dma_chan_to_sirfsoc_dma_chan(chan);
378 struct sirfsoc_dma_desc *sdesc, *tmp;
379 unsigned long flags;
380 LIST_HEAD(descs);
381
382 spin_lock_irqsave(&schan->lock, flags);
383
384 /* Channel must be idle */
385 BUG_ON(!list_empty(&schan->prepared));
386 BUG_ON(!list_empty(&schan->queued));
387 BUG_ON(!list_empty(&schan->active));
388 BUG_ON(!list_empty(&schan->completed));
389
390 /* Move data */
391 list_splice_tail_init(&schan->free, &descs);
392
393 spin_unlock_irqrestore(&schan->lock, flags);
394
395 /* Free descriptors */
396 list_for_each_entry_safe(sdesc, tmp, &descs, node)
397 kfree(sdesc);
398}
399
400/* Send pending descriptor to hardware */
401static void sirfsoc_dma_issue_pending(struct dma_chan *chan)
402{
403 struct sirfsoc_dma_chan *schan = dma_chan_to_sirfsoc_dma_chan(chan);
404 unsigned long flags;
405
406 spin_lock_irqsave(&schan->lock, flags);
407
408 if (list_empty(&schan->active) && !list_empty(&schan->queued))
409 sirfsoc_dma_execute(schan);
410
411 spin_unlock_irqrestore(&schan->lock, flags);
412}
413
414/* Check request completion status */
415static enum dma_status
416sirfsoc_dma_tx_status(struct dma_chan *chan, dma_cookie_t cookie,
417 struct dma_tx_state *txstate)
418{
419 struct sirfsoc_dma_chan *schan = dma_chan_to_sirfsoc_dma_chan(chan);
420 unsigned long flags;
Russell King - ARM Linux96a2af42012-03-06 22:35:27 +0000421 enum dma_status ret;
Rongjun Yingca21a142011-10-27 19:22:39 -0700422
423 spin_lock_irqsave(&schan->lock, flags);
Russell King - ARM Linux96a2af42012-03-06 22:35:27 +0000424 ret = dma_cookie_status(chan, cookie, txstate);
Rongjun Yingca21a142011-10-27 19:22:39 -0700425 spin_unlock_irqrestore(&schan->lock, flags);
426
Russell King - ARM Linux96a2af42012-03-06 22:35:27 +0000427 return ret;
Rongjun Yingca21a142011-10-27 19:22:39 -0700428}
429
430static struct dma_async_tx_descriptor *sirfsoc_dma_prep_interleaved(
431 struct dma_chan *chan, struct dma_interleaved_template *xt,
432 unsigned long flags)
433{
434 struct sirfsoc_dma *sdma = dma_chan_to_sirfsoc_dma(chan);
435 struct sirfsoc_dma_chan *schan = dma_chan_to_sirfsoc_dma_chan(chan);
436 struct sirfsoc_dma_desc *sdesc = NULL;
437 unsigned long iflags;
438 int ret;
439
Barry Song5997e082012-09-27 16:35:38 +0800440 if ((xt->dir != DMA_MEM_TO_DEV) && (xt->dir != DMA_DEV_TO_MEM)) {
Rongjun Yingca21a142011-10-27 19:22:39 -0700441 ret = -EINVAL;
442 goto err_dir;
443 }
444
445 /* Get free descriptor */
446 spin_lock_irqsave(&schan->lock, iflags);
447 if (!list_empty(&schan->free)) {
448 sdesc = list_first_entry(&schan->free, struct sirfsoc_dma_desc,
449 node);
450 list_del(&sdesc->node);
451 }
452 spin_unlock_irqrestore(&schan->lock, iflags);
453
454 if (!sdesc) {
455 /* try to free completed descriptors */
456 sirfsoc_dma_process_completed(sdma);
457 ret = 0;
458 goto no_desc;
459 }
460
461 /* Place descriptor in prepared list */
462 spin_lock_irqsave(&schan->lock, iflags);
463
464 /*
465 * Number of chunks in a frame can only be 1 for prima2
466 * and ylen (number of frame - 1) must be at least 0
467 */
468 if ((xt->frame_size == 1) && (xt->numf > 0)) {
469 sdesc->cyclic = 0;
470 sdesc->xlen = xt->sgl[0].size / SIRFSOC_DMA_WORD_LEN;
471 sdesc->width = (xt->sgl[0].size + xt->sgl[0].icg) /
472 SIRFSOC_DMA_WORD_LEN;
473 sdesc->ylen = xt->numf - 1;
474 if (xt->dir == DMA_MEM_TO_DEV) {
475 sdesc->addr = xt->src_start;
476 sdesc->dir = 1;
477 } else {
478 sdesc->addr = xt->dst_start;
479 sdesc->dir = 0;
480 }
481
482 list_add_tail(&sdesc->node, &schan->prepared);
483 } else {
484 pr_err("sirfsoc DMA Invalid xfer\n");
485 ret = -EINVAL;
486 goto err_xfer;
487 }
488 spin_unlock_irqrestore(&schan->lock, iflags);
489
490 return &sdesc->desc;
491err_xfer:
492 spin_unlock_irqrestore(&schan->lock, iflags);
493no_desc:
494err_dir:
495 return ERR_PTR(ret);
496}
497
498static struct dma_async_tx_descriptor *
499sirfsoc_dma_prep_cyclic(struct dma_chan *chan, dma_addr_t addr,
500 size_t buf_len, size_t period_len,
Peter Ujfalusiec8b5e42012-09-14 15:05:47 +0300501 enum dma_transfer_direction direction, unsigned long flags, void *context)
Rongjun Yingca21a142011-10-27 19:22:39 -0700502{
503 struct sirfsoc_dma_chan *schan = dma_chan_to_sirfsoc_dma_chan(chan);
504 struct sirfsoc_dma_desc *sdesc = NULL;
505 unsigned long iflags;
506
507 /*
508 * we only support cycle transfer with 2 period
509 * If the X-length is set to 0, it would be the loop mode.
510 * The DMA address keeps increasing until reaching the end of a loop
511 * area whose size is defined by (DMA_WIDTH x (Y_LENGTH + 1)). Then
512 * the DMA address goes back to the beginning of this area.
513 * In loop mode, the DMA data region is divided into two parts, BUFA
514 * and BUFB. DMA controller generates interrupts twice in each loop:
515 * when the DMA address reaches the end of BUFA or the end of the
516 * BUFB
517 */
518 if (buf_len != 2 * period_len)
519 return ERR_PTR(-EINVAL);
520
521 /* Get free descriptor */
522 spin_lock_irqsave(&schan->lock, iflags);
523 if (!list_empty(&schan->free)) {
524 sdesc = list_first_entry(&schan->free, struct sirfsoc_dma_desc,
525 node);
526 list_del(&sdesc->node);
527 }
528 spin_unlock_irqrestore(&schan->lock, iflags);
529
530 if (!sdesc)
531 return 0;
532
533 /* Place descriptor in prepared list */
534 spin_lock_irqsave(&schan->lock, iflags);
535 sdesc->addr = addr;
536 sdesc->cyclic = 1;
537 sdesc->xlen = 0;
538 sdesc->ylen = buf_len / SIRFSOC_DMA_WORD_LEN - 1;
539 sdesc->width = 1;
540 list_add_tail(&sdesc->node, &schan->prepared);
541 spin_unlock_irqrestore(&schan->lock, iflags);
542
543 return &sdesc->desc;
544}
545
546/*
547 * The DMA controller consists of 16 independent DMA channels.
548 * Each channel is allocated to a different function
549 */
550bool sirfsoc_dma_filter_id(struct dma_chan *chan, void *chan_id)
551{
552 unsigned int ch_nr = (unsigned int) chan_id;
553
554 if (ch_nr == chan->chan_id +
555 chan->device->dev_id * SIRFSOC_DMA_CHANNELS)
556 return true;
557
558 return false;
559}
560EXPORT_SYMBOL(sirfsoc_dma_filter_id);
561
Bill Pemberton463a1f82012-11-19 13:22:55 -0500562static int sirfsoc_dma_probe(struct platform_device *op)
Rongjun Yingca21a142011-10-27 19:22:39 -0700563{
564 struct device_node *dn = op->dev.of_node;
565 struct device *dev = &op->dev;
566 struct dma_device *dma;
567 struct sirfsoc_dma *sdma;
568 struct sirfsoc_dma_chan *schan;
569 struct resource res;
570 ulong regs_start, regs_size;
571 u32 id;
572 int ret, i;
573
574 sdma = devm_kzalloc(dev, sizeof(*sdma), GFP_KERNEL);
575 if (!sdma) {
576 dev_err(dev, "Memory exhausted!\n");
577 return -ENOMEM;
578 }
579
Barry Songf7d935d2012-11-01 22:54:43 +0800580 if (of_device_is_compatible(dn, "sirf,marco-dmac"))
581 sdma->is_marco = true;
582
Rongjun Yingca21a142011-10-27 19:22:39 -0700583 if (of_property_read_u32(dn, "cell-index", &id)) {
584 dev_err(dev, "Fail to get DMAC index\n");
Julia Lawall94d39012012-08-04 10:35:30 +0200585 return -ENODEV;
Rongjun Yingca21a142011-10-27 19:22:39 -0700586 }
587
588 sdma->irq = irq_of_parse_and_map(dn, 0);
589 if (sdma->irq == NO_IRQ) {
590 dev_err(dev, "Error mapping IRQ!\n");
Julia Lawall94d39012012-08-04 10:35:30 +0200591 return -EINVAL;
Rongjun Yingca21a142011-10-27 19:22:39 -0700592 }
593
594 ret = of_address_to_resource(dn, 0, &res);
595 if (ret) {
596 dev_err(dev, "Error parsing memory region!\n");
Julia Lawall94d39012012-08-04 10:35:30 +0200597 goto irq_dispose;
Rongjun Yingca21a142011-10-27 19:22:39 -0700598 }
599
600 regs_start = res.start;
601 regs_size = resource_size(&res);
602
603 sdma->base = devm_ioremap(dev, regs_start, regs_size);
604 if (!sdma->base) {
605 dev_err(dev, "Error mapping memory region!\n");
606 ret = -ENOMEM;
607 goto irq_dispose;
608 }
609
Julia Lawall94d39012012-08-04 10:35:30 +0200610 ret = request_irq(sdma->irq, &sirfsoc_dma_irq, 0, DRV_NAME, sdma);
Rongjun Yingca21a142011-10-27 19:22:39 -0700611 if (ret) {
612 dev_err(dev, "Error requesting IRQ!\n");
613 ret = -EINVAL;
Julia Lawall94d39012012-08-04 10:35:30 +0200614 goto irq_dispose;
Rongjun Yingca21a142011-10-27 19:22:39 -0700615 }
616
617 dma = &sdma->dma;
618 dma->dev = dev;
619 dma->chancnt = SIRFSOC_DMA_CHANNELS;
620
621 dma->device_alloc_chan_resources = sirfsoc_dma_alloc_chan_resources;
622 dma->device_free_chan_resources = sirfsoc_dma_free_chan_resources;
623 dma->device_issue_pending = sirfsoc_dma_issue_pending;
624 dma->device_control = sirfsoc_dma_control;
625 dma->device_tx_status = sirfsoc_dma_tx_status;
626 dma->device_prep_interleaved_dma = sirfsoc_dma_prep_interleaved;
627 dma->device_prep_dma_cyclic = sirfsoc_dma_prep_cyclic;
628
629 INIT_LIST_HEAD(&dma->channels);
630 dma_cap_set(DMA_SLAVE, dma->cap_mask);
631 dma_cap_set(DMA_CYCLIC, dma->cap_mask);
632 dma_cap_set(DMA_INTERLEAVE, dma->cap_mask);
633 dma_cap_set(DMA_PRIVATE, dma->cap_mask);
634
635 for (i = 0; i < dma->chancnt; i++) {
636 schan = &sdma->channels[i];
637
638 schan->chan.device = dma;
Russell King - ARM Linuxd3ee98cdc2012-03-06 22:35:47 +0000639 dma_cookie_init(&schan->chan);
Rongjun Yingca21a142011-10-27 19:22:39 -0700640
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);
646
647 spin_lock_init(&schan->lock);
648 list_add_tail(&schan->chan.device_node, &dma->channels);
649 }
650
651 tasklet_init(&sdma->tasklet, sirfsoc_dma_tasklet, (unsigned long)sdma);
652
653 /* Register DMA engine */
654 dev_set_drvdata(dev, sdma);
655 ret = dma_async_device_register(dma);
656 if (ret)
657 goto free_irq;
658
659 dev_info(dev, "initialized SIRFSOC DMAC driver\n");
660
661 return 0;
662
663free_irq:
Julia Lawall94d39012012-08-04 10:35:30 +0200664 free_irq(sdma->irq, sdma);
Rongjun Yingca21a142011-10-27 19:22:39 -0700665irq_dispose:
666 irq_dispose_mapping(sdma->irq);
Rongjun Yingca21a142011-10-27 19:22:39 -0700667 return ret;
668}
669
670static int __devexit sirfsoc_dma_remove(struct platform_device *op)
671{
672 struct device *dev = &op->dev;
673 struct sirfsoc_dma *sdma = dev_get_drvdata(dev);
674
675 dma_async_device_unregister(&sdma->dma);
Julia Lawall94d39012012-08-04 10:35:30 +0200676 free_irq(sdma->irq, sdma);
Rongjun Yingca21a142011-10-27 19:22:39 -0700677 irq_dispose_mapping(sdma->irq);
Rongjun Yingca21a142011-10-27 19:22:39 -0700678 return 0;
679}
680
681static struct of_device_id sirfsoc_dma_match[] = {
682 { .compatible = "sirf,prima2-dmac", },
Barry Songf7d935d2012-11-01 22:54:43 +0800683 { .compatible = "sirf,marco-dmac", },
Rongjun Yingca21a142011-10-27 19:22:39 -0700684 {},
685};
686
687static struct platform_driver sirfsoc_dma_driver = {
688 .probe = sirfsoc_dma_probe,
Bill Pembertona7d6e3e2012-11-19 13:20:04 -0500689 .remove = sirfsoc_dma_remove,
Rongjun Yingca21a142011-10-27 19:22:39 -0700690 .driver = {
691 .name = DRV_NAME,
692 .owner = THIS_MODULE,
693 .of_match_table = sirfsoc_dma_match,
694 },
695};
696
Axel Linc94e9102011-11-26 15:11:12 +0800697module_platform_driver(sirfsoc_dma_driver);
Rongjun Yingca21a142011-10-27 19:22:39 -0700698
699MODULE_AUTHOR("Rongjun Ying <rongjun.ying@csr.com>, "
700 "Barry Song <baohua.song@csr.com>");
701MODULE_DESCRIPTION("SIRFSOC DMA control driver");
702MODULE_LICENSE("GPL v2");