blob: 6aec3ad814d37f16b69c51f44347d9826e411885 [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>
Barry Song2a766892013-07-30 17:44:34 +080012#include <linux/pm_runtime.h>
Rongjun Yingca21a142011-10-27 19:22:39 -070013#include <linux/interrupt.h>
14#include <linux/io.h>
15#include <linux/slab.h>
16#include <linux/of_irq.h>
17#include <linux/of_address.h>
18#include <linux/of_device.h>
19#include <linux/of_platform.h>
Barry Songa7e34062013-03-18 16:33:43 +080020#include <linux/clk.h>
Rongjun Yingca21a142011-10-27 19:22:39 -070021#include <linux/sirfsoc_dma.h>
22
Vinod Koul949ff5b2012-03-13 11:58:12 +053023#include "dmaengine.h"
24
Rongjun Yingca21a142011-10-27 19:22:39 -070025#define SIRFSOC_DMA_DESCRIPTORS 16
26#define SIRFSOC_DMA_CHANNELS 16
27
28#define SIRFSOC_DMA_CH_ADDR 0x00
29#define SIRFSOC_DMA_CH_XLEN 0x04
30#define SIRFSOC_DMA_CH_YLEN 0x08
31#define SIRFSOC_DMA_CH_CTRL 0x0C
32
33#define SIRFSOC_DMA_WIDTH_0 0x100
34#define SIRFSOC_DMA_CH_VALID 0x140
35#define SIRFSOC_DMA_CH_INT 0x144
36#define SIRFSOC_DMA_INT_EN 0x148
Barry Songf7d935d2012-11-01 22:54:43 +080037#define SIRFSOC_DMA_INT_EN_CLR 0x14C
Rongjun Yingca21a142011-10-27 19:22:39 -070038#define SIRFSOC_DMA_CH_LOOP_CTRL 0x150
Barry Songf7d935d2012-11-01 22:54:43 +080039#define SIRFSOC_DMA_CH_LOOP_CTRL_CLR 0x15C
Rongjun Yingca21a142011-10-27 19:22:39 -070040
41#define SIRFSOC_DMA_MODE_CTRL_BIT 4
42#define SIRFSOC_DMA_DIR_CTRL_BIT 5
43
44/* xlen and dma_width register is in 4 bytes boundary */
45#define SIRFSOC_DMA_WORD_LEN 4
46
47struct sirfsoc_dma_desc {
48 struct dma_async_tx_descriptor desc;
49 struct list_head node;
50
51 /* SiRFprimaII 2D-DMA parameters */
52
53 int xlen; /* DMA xlen */
54 int ylen; /* DMA ylen */
55 int width; /* DMA width */
56 int dir;
57 bool cyclic; /* is loop DMA? */
58 u32 addr; /* DMA buffer address */
59};
60
61struct sirfsoc_dma_chan {
62 struct dma_chan chan;
63 struct list_head free;
64 struct list_head prepared;
65 struct list_head queued;
66 struct list_head active;
67 struct list_head completed;
Rongjun Yingca21a142011-10-27 19:22:39 -070068 unsigned long happened_cyclic;
69 unsigned long completed_cyclic;
70
71 /* Lock for this structure */
72 spinlock_t lock;
73
74 int mode;
75};
76
Barry Song2a766892013-07-30 17:44:34 +080077struct sirfsoc_dma_regs {
78 u32 ctrl[SIRFSOC_DMA_CHANNELS];
79 u32 interrupt_en;
80};
81
Rongjun Yingca21a142011-10-27 19:22:39 -070082struct sirfsoc_dma {
83 struct dma_device dma;
84 struct tasklet_struct tasklet;
85 struct sirfsoc_dma_chan channels[SIRFSOC_DMA_CHANNELS];
86 void __iomem *base;
87 int irq;
Barry Songa7e34062013-03-18 16:33:43 +080088 struct clk *clk;
Barry Songf7d935d2012-11-01 22:54:43 +080089 bool is_marco;
Barry Song2a766892013-07-30 17:44:34 +080090 struct sirfsoc_dma_regs regs_save;
Rongjun Yingca21a142011-10-27 19:22:39 -070091};
92
93#define DRV_NAME "sirfsoc_dma"
94
Barry Song2a766892013-07-30 17:44:34 +080095static int sirfsoc_dma_runtime_suspend(struct device *dev);
96
Rongjun Yingca21a142011-10-27 19:22:39 -070097/* Convert struct dma_chan to struct sirfsoc_dma_chan */
98static inline
99struct sirfsoc_dma_chan *dma_chan_to_sirfsoc_dma_chan(struct dma_chan *c)
100{
101 return container_of(c, struct sirfsoc_dma_chan, chan);
102}
103
104/* Convert struct dma_chan to struct sirfsoc_dma */
105static inline struct sirfsoc_dma *dma_chan_to_sirfsoc_dma(struct dma_chan *c)
106{
107 struct sirfsoc_dma_chan *schan = dma_chan_to_sirfsoc_dma_chan(c);
108 return container_of(schan, struct sirfsoc_dma, channels[c->chan_id]);
109}
110
111/* Execute all queued DMA descriptors */
112static void sirfsoc_dma_execute(struct sirfsoc_dma_chan *schan)
113{
114 struct sirfsoc_dma *sdma = dma_chan_to_sirfsoc_dma(&schan->chan);
115 int cid = schan->chan.chan_id;
116 struct sirfsoc_dma_desc *sdesc = NULL;
117
118 /*
119 * lock has been held by functions calling this, so we don't hold
120 * lock again
121 */
122
123 sdesc = list_first_entry(&schan->queued, struct sirfsoc_dma_desc,
124 node);
125 /* Move the first queued descriptor to active list */
Barry Song26fd1222012-09-27 16:36:10 +0800126 list_move_tail(&sdesc->node, &schan->active);
Rongjun Yingca21a142011-10-27 19:22:39 -0700127
128 /* Start the DMA transfer */
129 writel_relaxed(sdesc->width, sdma->base + SIRFSOC_DMA_WIDTH_0 +
130 cid * 4);
131 writel_relaxed(cid | (schan->mode << SIRFSOC_DMA_MODE_CTRL_BIT) |
132 (sdesc->dir << SIRFSOC_DMA_DIR_CTRL_BIT),
133 sdma->base + cid * 0x10 + SIRFSOC_DMA_CH_CTRL);
134 writel_relaxed(sdesc->xlen, sdma->base + cid * 0x10 +
135 SIRFSOC_DMA_CH_XLEN);
136 writel_relaxed(sdesc->ylen, sdma->base + cid * 0x10 +
137 SIRFSOC_DMA_CH_YLEN);
138 writel_relaxed(readl_relaxed(sdma->base + SIRFSOC_DMA_INT_EN) |
139 (1 << cid), sdma->base + SIRFSOC_DMA_INT_EN);
140
141 /*
142 * writel has an implict memory write barrier to make sure data is
143 * flushed into memory before starting DMA
144 */
145 writel(sdesc->addr >> 2, sdma->base + cid * 0x10 + SIRFSOC_DMA_CH_ADDR);
146
147 if (sdesc->cyclic) {
148 writel((1 << cid) | 1 << (cid + 16) |
149 readl_relaxed(sdma->base + SIRFSOC_DMA_CH_LOOP_CTRL),
150 sdma->base + SIRFSOC_DMA_CH_LOOP_CTRL);
151 schan->happened_cyclic = schan->completed_cyclic = 0;
152 }
153}
154
155/* Interrupt handler */
156static irqreturn_t sirfsoc_dma_irq(int irq, void *data)
157{
158 struct sirfsoc_dma *sdma = data;
159 struct sirfsoc_dma_chan *schan;
160 struct sirfsoc_dma_desc *sdesc = NULL;
161 u32 is;
162 int ch;
163
164 is = readl(sdma->base + SIRFSOC_DMA_CH_INT);
165 while ((ch = fls(is) - 1) >= 0) {
166 is &= ~(1 << ch);
167 writel_relaxed(1 << ch, sdma->base + SIRFSOC_DMA_CH_INT);
168 schan = &sdma->channels[ch];
169
170 spin_lock(&schan->lock);
171
172 sdesc = list_first_entry(&schan->active, struct sirfsoc_dma_desc,
173 node);
174 if (!sdesc->cyclic) {
175 /* Execute queued descriptors */
176 list_splice_tail_init(&schan->active, &schan->completed);
177 if (!list_empty(&schan->queued))
178 sirfsoc_dma_execute(schan);
179 } else
180 schan->happened_cyclic++;
181
182 spin_unlock(&schan->lock);
183 }
184
185 /* Schedule tasklet */
186 tasklet_schedule(&sdma->tasklet);
187
188 return IRQ_HANDLED;
189}
190
191/* process completed descriptors */
192static void sirfsoc_dma_process_completed(struct sirfsoc_dma *sdma)
193{
194 dma_cookie_t last_cookie = 0;
195 struct sirfsoc_dma_chan *schan;
196 struct sirfsoc_dma_desc *sdesc;
197 struct dma_async_tx_descriptor *desc;
198 unsigned long flags;
199 unsigned long happened_cyclic;
200 LIST_HEAD(list);
201 int i;
202
203 for (i = 0; i < sdma->dma.chancnt; i++) {
204 schan = &sdma->channels[i];
205
206 /* Get all completed descriptors */
207 spin_lock_irqsave(&schan->lock, flags);
208 if (!list_empty(&schan->completed)) {
209 list_splice_tail_init(&schan->completed, &list);
210 spin_unlock_irqrestore(&schan->lock, flags);
211
212 /* Execute callbacks and run dependencies */
213 list_for_each_entry(sdesc, &list, node) {
214 desc = &sdesc->desc;
215
216 if (desc->callback)
217 desc->callback(desc->callback_param);
218
219 last_cookie = desc->cookie;
220 dma_run_dependencies(desc);
221 }
222
223 /* Free descriptors */
224 spin_lock_irqsave(&schan->lock, flags);
225 list_splice_tail_init(&list, &schan->free);
Russell King - ARM Linux4d4e58d2012-03-06 22:34:06 +0000226 schan->chan.completed_cookie = last_cookie;
Rongjun Yingca21a142011-10-27 19:22:39 -0700227 spin_unlock_irqrestore(&schan->lock, flags);
228 } else {
229 /* for cyclic channel, desc is always in active list */
230 sdesc = list_first_entry(&schan->active, struct sirfsoc_dma_desc,
231 node);
232
233 if (!sdesc || (sdesc && !sdesc->cyclic)) {
234 /* without active cyclic DMA */
235 spin_unlock_irqrestore(&schan->lock, flags);
236 continue;
237 }
238
239 /* cyclic DMA */
240 happened_cyclic = schan->happened_cyclic;
241 spin_unlock_irqrestore(&schan->lock, flags);
242
243 desc = &sdesc->desc;
244 while (happened_cyclic != schan->completed_cyclic) {
245 if (desc->callback)
246 desc->callback(desc->callback_param);
247 schan->completed_cyclic++;
248 }
249 }
250 }
251}
252
253/* DMA Tasklet */
254static void sirfsoc_dma_tasklet(unsigned long data)
255{
256 struct sirfsoc_dma *sdma = (void *)data;
257
258 sirfsoc_dma_process_completed(sdma);
259}
260
261/* Submit descriptor to hardware */
262static dma_cookie_t sirfsoc_dma_tx_submit(struct dma_async_tx_descriptor *txd)
263{
264 struct sirfsoc_dma_chan *schan = dma_chan_to_sirfsoc_dma_chan(txd->chan);
265 struct sirfsoc_dma_desc *sdesc;
266 unsigned long flags;
267 dma_cookie_t cookie;
268
269 sdesc = container_of(txd, struct sirfsoc_dma_desc, desc);
270
271 spin_lock_irqsave(&schan->lock, flags);
272
273 /* Move descriptor to queue */
274 list_move_tail(&sdesc->node, &schan->queued);
275
Russell King - ARM Linux884485e2012-03-06 22:34:46 +0000276 cookie = dma_cookie_assign(txd);
Rongjun Yingca21a142011-10-27 19:22:39 -0700277
278 spin_unlock_irqrestore(&schan->lock, flags);
279
280 return cookie;
281}
282
283static int sirfsoc_dma_slave_config(struct sirfsoc_dma_chan *schan,
284 struct dma_slave_config *config)
285{
286 unsigned long flags;
287
288 if ((config->src_addr_width != DMA_SLAVE_BUSWIDTH_4_BYTES) ||
289 (config->dst_addr_width != DMA_SLAVE_BUSWIDTH_4_BYTES))
290 return -EINVAL;
291
292 spin_lock_irqsave(&schan->lock, flags);
293 schan->mode = (config->src_maxburst == 4 ? 1 : 0);
294 spin_unlock_irqrestore(&schan->lock, flags);
295
296 return 0;
297}
298
299static int sirfsoc_dma_terminate_all(struct sirfsoc_dma_chan *schan)
300{
301 struct sirfsoc_dma *sdma = dma_chan_to_sirfsoc_dma(&schan->chan);
302 int cid = schan->chan.chan_id;
303 unsigned long flags;
304
Barry Song2b99c252012-12-14 11:06:58 +0000305 spin_lock_irqsave(&schan->lock, flags);
306
Barry Songf7d935d2012-11-01 22:54:43 +0800307 if (!sdma->is_marco) {
308 writel_relaxed(readl_relaxed(sdma->base + SIRFSOC_DMA_INT_EN) &
309 ~(1 << cid), sdma->base + SIRFSOC_DMA_INT_EN);
310 writel_relaxed(readl_relaxed(sdma->base + SIRFSOC_DMA_CH_LOOP_CTRL)
311 & ~((1 << cid) | 1 << (cid + 16)),
Rongjun Yingca21a142011-10-27 19:22:39 -0700312 sdma->base + SIRFSOC_DMA_CH_LOOP_CTRL);
Barry Songf7d935d2012-11-01 22:54:43 +0800313 } else {
314 writel_relaxed(1 << cid, sdma->base + SIRFSOC_DMA_INT_EN_CLR);
315 writel_relaxed((1 << cid) | 1 << (cid + 16),
316 sdma->base + SIRFSOC_DMA_CH_LOOP_CTRL_CLR);
317 }
318
Rongjun Yingca21a142011-10-27 19:22:39 -0700319 writel_relaxed(1 << cid, sdma->base + SIRFSOC_DMA_CH_VALID);
320
Rongjun Yingca21a142011-10-27 19:22:39 -0700321 list_splice_tail_init(&schan->active, &schan->free);
322 list_splice_tail_init(&schan->queued, &schan->free);
Barry Song2b99c252012-12-14 11:06:58 +0000323
Rongjun Yingca21a142011-10-27 19:22:39 -0700324 spin_unlock_irqrestore(&schan->lock, flags);
325
326 return 0;
327}
328
Barry Song2518d1d2012-12-14 10:59:22 +0000329static int sirfsoc_dma_pause_chan(struct sirfsoc_dma_chan *schan)
330{
331 struct sirfsoc_dma *sdma = dma_chan_to_sirfsoc_dma(&schan->chan);
332 int cid = schan->chan.chan_id;
333 unsigned long flags;
334
335 spin_lock_irqsave(&schan->lock, flags);
336
337 if (!sdma->is_marco)
338 writel_relaxed(readl_relaxed(sdma->base + SIRFSOC_DMA_CH_LOOP_CTRL)
339 & ~((1 << cid) | 1 << (cid + 16)),
340 sdma->base + SIRFSOC_DMA_CH_LOOP_CTRL);
341 else
342 writel_relaxed((1 << cid) | 1 << (cid + 16),
343 sdma->base + SIRFSOC_DMA_CH_LOOP_CTRL_CLR);
344
345 spin_unlock_irqrestore(&schan->lock, flags);
346
347 return 0;
348}
349
350static int sirfsoc_dma_resume_chan(struct sirfsoc_dma_chan *schan)
351{
352 struct sirfsoc_dma *sdma = dma_chan_to_sirfsoc_dma(&schan->chan);
353 int cid = schan->chan.chan_id;
354 unsigned long flags;
355
356 spin_lock_irqsave(&schan->lock, flags);
357
358 if (!sdma->is_marco)
359 writel_relaxed(readl_relaxed(sdma->base + SIRFSOC_DMA_CH_LOOP_CTRL)
360 | ((1 << cid) | 1 << (cid + 16)),
361 sdma->base + SIRFSOC_DMA_CH_LOOP_CTRL);
362 else
363 writel_relaxed((1 << cid) | 1 << (cid + 16),
364 sdma->base + SIRFSOC_DMA_CH_LOOP_CTRL);
365
Rongjun Yingca21a142011-10-27 19:22:39 -0700366 spin_unlock_irqrestore(&schan->lock, flags);
367
368 return 0;
369}
370
371static int sirfsoc_dma_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd,
372 unsigned long arg)
373{
374 struct dma_slave_config *config;
375 struct sirfsoc_dma_chan *schan = dma_chan_to_sirfsoc_dma_chan(chan);
376
377 switch (cmd) {
Barry Song2518d1d2012-12-14 10:59:22 +0000378 case DMA_PAUSE:
379 return sirfsoc_dma_pause_chan(schan);
380 case DMA_RESUME:
381 return sirfsoc_dma_resume_chan(schan);
Rongjun Yingca21a142011-10-27 19:22:39 -0700382 case DMA_TERMINATE_ALL:
383 return sirfsoc_dma_terminate_all(schan);
384 case DMA_SLAVE_CONFIG:
385 config = (struct dma_slave_config *)arg;
386 return sirfsoc_dma_slave_config(schan, config);
387
388 default:
389 break;
390 }
391
392 return -ENOSYS;
393}
394
395/* Alloc channel resources */
396static int sirfsoc_dma_alloc_chan_resources(struct dma_chan *chan)
397{
398 struct sirfsoc_dma *sdma = dma_chan_to_sirfsoc_dma(chan);
399 struct sirfsoc_dma_chan *schan = dma_chan_to_sirfsoc_dma_chan(chan);
400 struct sirfsoc_dma_desc *sdesc;
401 unsigned long flags;
402 LIST_HEAD(descs);
403 int i;
404
Barry Song2a766892013-07-30 17:44:34 +0800405 pm_runtime_get_sync(sdma->dma.dev);
406
Rongjun Yingca21a142011-10-27 19:22:39 -0700407 /* Alloc descriptors for this channel */
408 for (i = 0; i < SIRFSOC_DMA_DESCRIPTORS; i++) {
409 sdesc = kzalloc(sizeof(*sdesc), GFP_KERNEL);
410 if (!sdesc) {
411 dev_notice(sdma->dma.dev, "Memory allocation error. "
412 "Allocated only %u descriptors\n", i);
413 break;
414 }
415
416 dma_async_tx_descriptor_init(&sdesc->desc, chan);
417 sdesc->desc.flags = DMA_CTRL_ACK;
418 sdesc->desc.tx_submit = sirfsoc_dma_tx_submit;
419
420 list_add_tail(&sdesc->node, &descs);
421 }
422
423 /* Return error only if no descriptors were allocated */
424 if (i == 0)
425 return -ENOMEM;
426
427 spin_lock_irqsave(&schan->lock, flags);
428
429 list_splice_tail_init(&descs, &schan->free);
430 spin_unlock_irqrestore(&schan->lock, flags);
431
432 return i;
433}
434
435/* Free channel resources */
436static void sirfsoc_dma_free_chan_resources(struct dma_chan *chan)
437{
438 struct sirfsoc_dma_chan *schan = dma_chan_to_sirfsoc_dma_chan(chan);
Barry Song2a766892013-07-30 17:44:34 +0800439 struct sirfsoc_dma *sdma = dma_chan_to_sirfsoc_dma(chan);
Rongjun Yingca21a142011-10-27 19:22:39 -0700440 struct sirfsoc_dma_desc *sdesc, *tmp;
441 unsigned long flags;
442 LIST_HEAD(descs);
443
444 spin_lock_irqsave(&schan->lock, flags);
445
446 /* Channel must be idle */
447 BUG_ON(!list_empty(&schan->prepared));
448 BUG_ON(!list_empty(&schan->queued));
449 BUG_ON(!list_empty(&schan->active));
450 BUG_ON(!list_empty(&schan->completed));
451
452 /* Move data */
453 list_splice_tail_init(&schan->free, &descs);
454
455 spin_unlock_irqrestore(&schan->lock, flags);
456
457 /* Free descriptors */
458 list_for_each_entry_safe(sdesc, tmp, &descs, node)
459 kfree(sdesc);
Barry Song2a766892013-07-30 17:44:34 +0800460
461 pm_runtime_put(sdma->dma.dev);
Rongjun Yingca21a142011-10-27 19:22:39 -0700462}
463
464/* Send pending descriptor to hardware */
465static void sirfsoc_dma_issue_pending(struct dma_chan *chan)
466{
467 struct sirfsoc_dma_chan *schan = dma_chan_to_sirfsoc_dma_chan(chan);
468 unsigned long flags;
469
470 spin_lock_irqsave(&schan->lock, flags);
471
472 if (list_empty(&schan->active) && !list_empty(&schan->queued))
473 sirfsoc_dma_execute(schan);
474
475 spin_unlock_irqrestore(&schan->lock, flags);
476}
477
478/* Check request completion status */
479static enum dma_status
480sirfsoc_dma_tx_status(struct dma_chan *chan, dma_cookie_t cookie,
481 struct dma_tx_state *txstate)
482{
Rongjun Yingadd93b52013-05-14 23:03:20 +0800483 struct sirfsoc_dma *sdma = dma_chan_to_sirfsoc_dma(chan);
Rongjun Yingca21a142011-10-27 19:22:39 -0700484 struct sirfsoc_dma_chan *schan = dma_chan_to_sirfsoc_dma_chan(chan);
485 unsigned long flags;
Russell King - ARM Linux96a2af42012-03-06 22:35:27 +0000486 enum dma_status ret;
Rongjun Yingadd93b52013-05-14 23:03:20 +0800487 struct sirfsoc_dma_desc *sdesc;
488 int cid = schan->chan.chan_id;
489 unsigned long dma_pos;
490 unsigned long dma_request_bytes;
491 unsigned long residue;
Rongjun Yingca21a142011-10-27 19:22:39 -0700492
493 spin_lock_irqsave(&schan->lock, flags);
Rongjun Yingadd93b52013-05-14 23:03:20 +0800494
495 sdesc = list_first_entry(&schan->active, struct sirfsoc_dma_desc,
496 node);
497 dma_request_bytes = (sdesc->xlen + 1) * (sdesc->ylen + 1) *
498 (sdesc->width * SIRFSOC_DMA_WORD_LEN);
499
Russell King - ARM Linux96a2af42012-03-06 22:35:27 +0000500 ret = dma_cookie_status(chan, cookie, txstate);
Rongjun Yingadd93b52013-05-14 23:03:20 +0800501 dma_pos = readl_relaxed(sdma->base + cid * 0x10 + SIRFSOC_DMA_CH_ADDR)
502 << 2;
503 residue = dma_request_bytes - (dma_pos - sdesc->addr);
504 dma_set_residue(txstate, residue);
505
Rongjun Yingca21a142011-10-27 19:22:39 -0700506 spin_unlock_irqrestore(&schan->lock, flags);
507
Russell King - ARM Linux96a2af42012-03-06 22:35:27 +0000508 return ret;
Rongjun Yingca21a142011-10-27 19:22:39 -0700509}
510
511static struct dma_async_tx_descriptor *sirfsoc_dma_prep_interleaved(
512 struct dma_chan *chan, struct dma_interleaved_template *xt,
513 unsigned long flags)
514{
515 struct sirfsoc_dma *sdma = dma_chan_to_sirfsoc_dma(chan);
516 struct sirfsoc_dma_chan *schan = dma_chan_to_sirfsoc_dma_chan(chan);
517 struct sirfsoc_dma_desc *sdesc = NULL;
518 unsigned long iflags;
519 int ret;
520
Barry Song5997e082012-09-27 16:35:38 +0800521 if ((xt->dir != DMA_MEM_TO_DEV) && (xt->dir != DMA_DEV_TO_MEM)) {
Rongjun Yingca21a142011-10-27 19:22:39 -0700522 ret = -EINVAL;
523 goto err_dir;
524 }
525
526 /* Get free descriptor */
527 spin_lock_irqsave(&schan->lock, iflags);
528 if (!list_empty(&schan->free)) {
529 sdesc = list_first_entry(&schan->free, struct sirfsoc_dma_desc,
530 node);
531 list_del(&sdesc->node);
532 }
533 spin_unlock_irqrestore(&schan->lock, iflags);
534
535 if (!sdesc) {
536 /* try to free completed descriptors */
537 sirfsoc_dma_process_completed(sdma);
538 ret = 0;
539 goto no_desc;
540 }
541
542 /* Place descriptor in prepared list */
543 spin_lock_irqsave(&schan->lock, iflags);
544
545 /*
546 * Number of chunks in a frame can only be 1 for prima2
547 * and ylen (number of frame - 1) must be at least 0
548 */
549 if ((xt->frame_size == 1) && (xt->numf > 0)) {
550 sdesc->cyclic = 0;
551 sdesc->xlen = xt->sgl[0].size / SIRFSOC_DMA_WORD_LEN;
552 sdesc->width = (xt->sgl[0].size + xt->sgl[0].icg) /
553 SIRFSOC_DMA_WORD_LEN;
554 sdesc->ylen = xt->numf - 1;
555 if (xt->dir == DMA_MEM_TO_DEV) {
556 sdesc->addr = xt->src_start;
557 sdesc->dir = 1;
558 } else {
559 sdesc->addr = xt->dst_start;
560 sdesc->dir = 0;
561 }
562
563 list_add_tail(&sdesc->node, &schan->prepared);
564 } else {
565 pr_err("sirfsoc DMA Invalid xfer\n");
566 ret = -EINVAL;
567 goto err_xfer;
568 }
569 spin_unlock_irqrestore(&schan->lock, iflags);
570
571 return &sdesc->desc;
572err_xfer:
573 spin_unlock_irqrestore(&schan->lock, iflags);
574no_desc:
575err_dir:
576 return ERR_PTR(ret);
577}
578
579static struct dma_async_tx_descriptor *
580sirfsoc_dma_prep_cyclic(struct dma_chan *chan, dma_addr_t addr,
581 size_t buf_len, size_t period_len,
Peter Ujfalusiec8b5e42012-09-14 15:05:47 +0300582 enum dma_transfer_direction direction, unsigned long flags, void *context)
Rongjun Yingca21a142011-10-27 19:22:39 -0700583{
584 struct sirfsoc_dma_chan *schan = dma_chan_to_sirfsoc_dma_chan(chan);
585 struct sirfsoc_dma_desc *sdesc = NULL;
586 unsigned long iflags;
587
588 /*
589 * we only support cycle transfer with 2 period
590 * If the X-length is set to 0, it would be the loop mode.
591 * The DMA address keeps increasing until reaching the end of a loop
592 * area whose size is defined by (DMA_WIDTH x (Y_LENGTH + 1)). Then
593 * the DMA address goes back to the beginning of this area.
594 * In loop mode, the DMA data region is divided into two parts, BUFA
595 * and BUFB. DMA controller generates interrupts twice in each loop:
596 * when the DMA address reaches the end of BUFA or the end of the
597 * BUFB
598 */
599 if (buf_len != 2 * period_len)
600 return ERR_PTR(-EINVAL);
601
602 /* Get free descriptor */
603 spin_lock_irqsave(&schan->lock, iflags);
604 if (!list_empty(&schan->free)) {
605 sdesc = list_first_entry(&schan->free, struct sirfsoc_dma_desc,
606 node);
607 list_del(&sdesc->node);
608 }
609 spin_unlock_irqrestore(&schan->lock, iflags);
610
611 if (!sdesc)
Jingoo Han696b4ff2013-08-06 19:37:56 +0900612 return NULL;
Rongjun Yingca21a142011-10-27 19:22:39 -0700613
614 /* Place descriptor in prepared list */
615 spin_lock_irqsave(&schan->lock, iflags);
616 sdesc->addr = addr;
617 sdesc->cyclic = 1;
618 sdesc->xlen = 0;
619 sdesc->ylen = buf_len / SIRFSOC_DMA_WORD_LEN - 1;
620 sdesc->width = 1;
621 list_add_tail(&sdesc->node, &schan->prepared);
622 spin_unlock_irqrestore(&schan->lock, iflags);
623
624 return &sdesc->desc;
625}
626
627/*
628 * The DMA controller consists of 16 independent DMA channels.
629 * Each channel is allocated to a different function
630 */
631bool sirfsoc_dma_filter_id(struct dma_chan *chan, void *chan_id)
632{
633 unsigned int ch_nr = (unsigned int) chan_id;
634
635 if (ch_nr == chan->chan_id +
636 chan->device->dev_id * SIRFSOC_DMA_CHANNELS)
637 return true;
638
639 return false;
640}
641EXPORT_SYMBOL(sirfsoc_dma_filter_id);
642
Bill Pemberton463a1f82012-11-19 13:22:55 -0500643static int sirfsoc_dma_probe(struct platform_device *op)
Rongjun Yingca21a142011-10-27 19:22:39 -0700644{
645 struct device_node *dn = op->dev.of_node;
646 struct device *dev = &op->dev;
647 struct dma_device *dma;
648 struct sirfsoc_dma *sdma;
649 struct sirfsoc_dma_chan *schan;
650 struct resource res;
651 ulong regs_start, regs_size;
652 u32 id;
653 int ret, i;
654
655 sdma = devm_kzalloc(dev, sizeof(*sdma), GFP_KERNEL);
656 if (!sdma) {
657 dev_err(dev, "Memory exhausted!\n");
658 return -ENOMEM;
659 }
660
Barry Songf7d935d2012-11-01 22:54:43 +0800661 if (of_device_is_compatible(dn, "sirf,marco-dmac"))
662 sdma->is_marco = true;
663
Rongjun Yingca21a142011-10-27 19:22:39 -0700664 if (of_property_read_u32(dn, "cell-index", &id)) {
665 dev_err(dev, "Fail to get DMAC index\n");
Julia Lawall94d39012012-08-04 10:35:30 +0200666 return -ENODEV;
Rongjun Yingca21a142011-10-27 19:22:39 -0700667 }
668
669 sdma->irq = irq_of_parse_and_map(dn, 0);
670 if (sdma->irq == NO_IRQ) {
671 dev_err(dev, "Error mapping IRQ!\n");
Julia Lawall94d39012012-08-04 10:35:30 +0200672 return -EINVAL;
Rongjun Yingca21a142011-10-27 19:22:39 -0700673 }
674
Barry Songa7e34062013-03-18 16:33:43 +0800675 sdma->clk = devm_clk_get(dev, NULL);
676 if (IS_ERR(sdma->clk)) {
677 dev_err(dev, "failed to get a clock.\n");
678 return PTR_ERR(sdma->clk);
679 }
680
Rongjun Yingca21a142011-10-27 19:22:39 -0700681 ret = of_address_to_resource(dn, 0, &res);
682 if (ret) {
683 dev_err(dev, "Error parsing memory region!\n");
Julia Lawall94d39012012-08-04 10:35:30 +0200684 goto irq_dispose;
Rongjun Yingca21a142011-10-27 19:22:39 -0700685 }
686
687 regs_start = res.start;
688 regs_size = resource_size(&res);
689
690 sdma->base = devm_ioremap(dev, regs_start, regs_size);
691 if (!sdma->base) {
692 dev_err(dev, "Error mapping memory region!\n");
693 ret = -ENOMEM;
694 goto irq_dispose;
695 }
696
Julia Lawall94d39012012-08-04 10:35:30 +0200697 ret = request_irq(sdma->irq, &sirfsoc_dma_irq, 0, DRV_NAME, sdma);
Rongjun Yingca21a142011-10-27 19:22:39 -0700698 if (ret) {
699 dev_err(dev, "Error requesting IRQ!\n");
700 ret = -EINVAL;
Julia Lawall94d39012012-08-04 10:35:30 +0200701 goto irq_dispose;
Rongjun Yingca21a142011-10-27 19:22:39 -0700702 }
703
704 dma = &sdma->dma;
705 dma->dev = dev;
706 dma->chancnt = SIRFSOC_DMA_CHANNELS;
707
708 dma->device_alloc_chan_resources = sirfsoc_dma_alloc_chan_resources;
709 dma->device_free_chan_resources = sirfsoc_dma_free_chan_resources;
710 dma->device_issue_pending = sirfsoc_dma_issue_pending;
711 dma->device_control = sirfsoc_dma_control;
712 dma->device_tx_status = sirfsoc_dma_tx_status;
713 dma->device_prep_interleaved_dma = sirfsoc_dma_prep_interleaved;
714 dma->device_prep_dma_cyclic = sirfsoc_dma_prep_cyclic;
715
716 INIT_LIST_HEAD(&dma->channels);
717 dma_cap_set(DMA_SLAVE, dma->cap_mask);
718 dma_cap_set(DMA_CYCLIC, dma->cap_mask);
719 dma_cap_set(DMA_INTERLEAVE, dma->cap_mask);
720 dma_cap_set(DMA_PRIVATE, dma->cap_mask);
721
722 for (i = 0; i < dma->chancnt; i++) {
723 schan = &sdma->channels[i];
724
725 schan->chan.device = dma;
Russell King - ARM Linuxd3ee98cdc2012-03-06 22:35:47 +0000726 dma_cookie_init(&schan->chan);
Rongjun Yingca21a142011-10-27 19:22:39 -0700727
728 INIT_LIST_HEAD(&schan->free);
729 INIT_LIST_HEAD(&schan->prepared);
730 INIT_LIST_HEAD(&schan->queued);
731 INIT_LIST_HEAD(&schan->active);
732 INIT_LIST_HEAD(&schan->completed);
733
734 spin_lock_init(&schan->lock);
735 list_add_tail(&schan->chan.device_node, &dma->channels);
736 }
737
738 tasklet_init(&sdma->tasklet, sirfsoc_dma_tasklet, (unsigned long)sdma);
739
740 /* Register DMA engine */
741 dev_set_drvdata(dev, sdma);
Barry Song2a766892013-07-30 17:44:34 +0800742
Rongjun Yingca21a142011-10-27 19:22:39 -0700743 ret = dma_async_device_register(dma);
744 if (ret)
745 goto free_irq;
746
Barry Song2a766892013-07-30 17:44:34 +0800747 pm_runtime_enable(&op->dev);
Rongjun Yingca21a142011-10-27 19:22:39 -0700748 dev_info(dev, "initialized SIRFSOC DMAC driver\n");
749
750 return 0;
751
752free_irq:
Julia Lawall94d39012012-08-04 10:35:30 +0200753 free_irq(sdma->irq, sdma);
Rongjun Yingca21a142011-10-27 19:22:39 -0700754irq_dispose:
755 irq_dispose_mapping(sdma->irq);
Rongjun Yingca21a142011-10-27 19:22:39 -0700756 return ret;
757}
758
Greg Kroah-Hartman4bf27b82012-12-21 15:09:59 -0800759static int sirfsoc_dma_remove(struct platform_device *op)
Rongjun Yingca21a142011-10-27 19:22:39 -0700760{
761 struct device *dev = &op->dev;
762 struct sirfsoc_dma *sdma = dev_get_drvdata(dev);
763
764 dma_async_device_unregister(&sdma->dma);
Julia Lawall94d39012012-08-04 10:35:30 +0200765 free_irq(sdma->irq, sdma);
Rongjun Yingca21a142011-10-27 19:22:39 -0700766 irq_dispose_mapping(sdma->irq);
Barry Song2a766892013-07-30 17:44:34 +0800767 pm_runtime_disable(&op->dev);
768 if (!pm_runtime_status_suspended(&op->dev))
769 sirfsoc_dma_runtime_suspend(&op->dev);
770
Rongjun Yingca21a142011-10-27 19:22:39 -0700771 return 0;
772}
773
Barry Song2a766892013-07-30 17:44:34 +0800774static int sirfsoc_dma_runtime_suspend(struct device *dev)
775{
776 struct sirfsoc_dma *sdma = dev_get_drvdata(dev);
777
778 clk_disable_unprepare(sdma->clk);
779 return 0;
780}
781
782static int sirfsoc_dma_runtime_resume(struct device *dev)
783{
784 struct sirfsoc_dma *sdma = dev_get_drvdata(dev);
785 int ret;
786
787 ret = clk_prepare_enable(sdma->clk);
788 if (ret < 0) {
789 dev_err(dev, "clk_enable failed: %d\n", ret);
790 return ret;
791 }
792 return 0;
793}
794
795static int sirfsoc_dma_pm_suspend(struct device *dev)
796{
797 struct sirfsoc_dma *sdma = dev_get_drvdata(dev);
798 struct sirfsoc_dma_regs *save = &sdma->regs_save;
799 struct sirfsoc_dma_desc *sdesc;
800 struct sirfsoc_dma_chan *schan;
801 int ch;
802 int ret;
803
804 /*
805 * if we were runtime-suspended before, resume to enable clock
806 * before accessing register
807 */
808 if (pm_runtime_status_suspended(dev)) {
809 ret = sirfsoc_dma_runtime_resume(dev);
810 if (ret < 0)
811 return ret;
812 }
813
814 /*
815 * DMA controller will lose all registers while suspending
816 * so we need to save registers for active channels
817 */
818 for (ch = 0; ch < SIRFSOC_DMA_CHANNELS; ch++) {
819 schan = &sdma->channels[ch];
820 if (list_empty(&schan->active))
821 continue;
822 sdesc = list_first_entry(&schan->active,
823 struct sirfsoc_dma_desc,
824 node);
825 save->ctrl[ch] = readl_relaxed(sdma->base +
826 ch * 0x10 + SIRFSOC_DMA_CH_CTRL);
827 }
828 save->interrupt_en = readl_relaxed(sdma->base + SIRFSOC_DMA_INT_EN);
829
830 /* Disable clock */
831 sirfsoc_dma_runtime_suspend(dev);
832
833 return 0;
834}
835
836static int sirfsoc_dma_pm_resume(struct device *dev)
837{
838 struct sirfsoc_dma *sdma = dev_get_drvdata(dev);
839 struct sirfsoc_dma_regs *save = &sdma->regs_save;
840 struct sirfsoc_dma_desc *sdesc;
841 struct sirfsoc_dma_chan *schan;
842 int ch;
843 int ret;
844
845 /* Enable clock before accessing register */
846 ret = sirfsoc_dma_runtime_resume(dev);
847 if (ret < 0)
848 return ret;
849
850 writel_relaxed(save->interrupt_en, sdma->base + SIRFSOC_DMA_INT_EN);
851 for (ch = 0; ch < SIRFSOC_DMA_CHANNELS; ch++) {
852 schan = &sdma->channels[ch];
853 if (list_empty(&schan->active))
854 continue;
855 sdesc = list_first_entry(&schan->active,
856 struct sirfsoc_dma_desc,
857 node);
858 writel_relaxed(sdesc->width,
859 sdma->base + SIRFSOC_DMA_WIDTH_0 + ch * 4);
860 writel_relaxed(sdesc->xlen,
861 sdma->base + ch * 0x10 + SIRFSOC_DMA_CH_XLEN);
862 writel_relaxed(sdesc->ylen,
863 sdma->base + ch * 0x10 + SIRFSOC_DMA_CH_YLEN);
864 writel_relaxed(save->ctrl[ch],
865 sdma->base + ch * 0x10 + SIRFSOC_DMA_CH_CTRL);
866 writel_relaxed(sdesc->addr >> 2,
867 sdma->base + ch * 0x10 + SIRFSOC_DMA_CH_ADDR);
868 }
869
870 /* if we were runtime-suspended before, suspend again */
871 if (pm_runtime_status_suspended(dev))
872 sirfsoc_dma_runtime_suspend(dev);
873
874 return 0;
875}
876
877static const struct dev_pm_ops sirfsoc_dma_pm_ops = {
878 SET_RUNTIME_PM_OPS(sirfsoc_dma_runtime_suspend, sirfsoc_dma_runtime_resume, NULL)
879 SET_SYSTEM_SLEEP_PM_OPS(sirfsoc_dma_pm_suspend, sirfsoc_dma_pm_resume)
880};
881
Rongjun Yingca21a142011-10-27 19:22:39 -0700882static struct of_device_id sirfsoc_dma_match[] = {
883 { .compatible = "sirf,prima2-dmac", },
Barry Songf7d935d2012-11-01 22:54:43 +0800884 { .compatible = "sirf,marco-dmac", },
Rongjun Yingca21a142011-10-27 19:22:39 -0700885 {},
886};
887
888static struct platform_driver sirfsoc_dma_driver = {
889 .probe = sirfsoc_dma_probe,
Bill Pembertona7d6e3e2012-11-19 13:20:04 -0500890 .remove = sirfsoc_dma_remove,
Rongjun Yingca21a142011-10-27 19:22:39 -0700891 .driver = {
892 .name = DRV_NAME,
893 .owner = THIS_MODULE,
Barry Song2a766892013-07-30 17:44:34 +0800894 .pm = &sirfsoc_dma_pm_ops,
Rongjun Yingca21a142011-10-27 19:22:39 -0700895 .of_match_table = sirfsoc_dma_match,
896 },
897};
898
Barry Song42361f22013-04-11 14:09:28 +0800899static __init int sirfsoc_dma_init(void)
900{
901 return platform_driver_register(&sirfsoc_dma_driver);
902}
903
904static void __exit sirfsoc_dma_exit(void)
905{
906 platform_driver_unregister(&sirfsoc_dma_driver);
907}
908
909subsys_initcall(sirfsoc_dma_init);
910module_exit(sirfsoc_dma_exit);
Rongjun Yingca21a142011-10-27 19:22:39 -0700911
912MODULE_AUTHOR("Rongjun Ying <rongjun.ying@csr.com>, "
913 "Barry Song <baohua.song@csr.com>");
914MODULE_DESCRIPTION("SIRFSOC DMA control driver");
915MODULE_LICENSE("GPL v2");