blob: 03f7820fa333b89a445dc3831386ba371b93a1e1 [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>
Barry Song2e041c92014-03-27 15:49:31 +080021#include <linux/of_dma.h>
Rongjun Yingca21a142011-10-27 19:22:39 -070022#include <linux/sirfsoc_dma.h>
23
Vinod Koul949ff5b2012-03-13 11:58:12 +053024#include "dmaengine.h"
25
Rongjun Yingca21a142011-10-27 19:22:39 -070026#define SIRFSOC_DMA_DESCRIPTORS 16
27#define SIRFSOC_DMA_CHANNELS 16
28
29#define SIRFSOC_DMA_CH_ADDR 0x00
30#define SIRFSOC_DMA_CH_XLEN 0x04
31#define SIRFSOC_DMA_CH_YLEN 0x08
32#define SIRFSOC_DMA_CH_CTRL 0x0C
33
34#define SIRFSOC_DMA_WIDTH_0 0x100
35#define SIRFSOC_DMA_CH_VALID 0x140
36#define SIRFSOC_DMA_CH_INT 0x144
37#define SIRFSOC_DMA_INT_EN 0x148
Barry Songf7d935d2012-11-01 22:54:43 +080038#define SIRFSOC_DMA_INT_EN_CLR 0x14C
Rongjun Yingca21a142011-10-27 19:22:39 -070039#define SIRFSOC_DMA_CH_LOOP_CTRL 0x150
Barry Songf7d935d2012-11-01 22:54:43 +080040#define SIRFSOC_DMA_CH_LOOP_CTRL_CLR 0x15C
Rongjun Yingca21a142011-10-27 19:22:39 -070041
42#define SIRFSOC_DMA_MODE_CTRL_BIT 4
43#define SIRFSOC_DMA_DIR_CTRL_BIT 5
44
45/* xlen and dma_width register is in 4 bytes boundary */
46#define SIRFSOC_DMA_WORD_LEN 4
47
48struct sirfsoc_dma_desc {
49 struct dma_async_tx_descriptor desc;
50 struct list_head node;
51
52 /* SiRFprimaII 2D-DMA parameters */
53
54 int xlen; /* DMA xlen */
55 int ylen; /* DMA ylen */
56 int width; /* DMA width */
57 int dir;
58 bool cyclic; /* is loop DMA? */
59 u32 addr; /* DMA buffer address */
60};
61
62struct sirfsoc_dma_chan {
63 struct dma_chan chan;
64 struct list_head free;
65 struct list_head prepared;
66 struct list_head queued;
67 struct list_head active;
68 struct list_head completed;
Rongjun Yingca21a142011-10-27 19:22:39 -070069 unsigned long happened_cyclic;
70 unsigned long completed_cyclic;
71
72 /* Lock for this structure */
73 spinlock_t lock;
74
75 int mode;
76};
77
Barry Song2a766892013-07-30 17:44:34 +080078struct sirfsoc_dma_regs {
79 u32 ctrl[SIRFSOC_DMA_CHANNELS];
80 u32 interrupt_en;
81};
82
Rongjun Yingca21a142011-10-27 19:22:39 -070083struct sirfsoc_dma {
84 struct dma_device dma;
85 struct tasklet_struct tasklet;
86 struct sirfsoc_dma_chan channels[SIRFSOC_DMA_CHANNELS];
87 void __iomem *base;
88 int irq;
Barry Songa7e34062013-03-18 16:33:43 +080089 struct clk *clk;
Barry Songf7d935d2012-11-01 22:54:43 +080090 bool is_marco;
Barry Song2a766892013-07-30 17:44:34 +080091 struct sirfsoc_dma_regs regs_save;
Rongjun Yingca21a142011-10-27 19:22:39 -070092};
93
94#define DRV_NAME "sirfsoc_dma"
95
Barry Song2a766892013-07-30 17:44:34 +080096static int sirfsoc_dma_runtime_suspend(struct device *dev);
97
Rongjun Yingca21a142011-10-27 19:22:39 -070098/* Convert struct dma_chan to struct sirfsoc_dma_chan */
99static inline
100struct sirfsoc_dma_chan *dma_chan_to_sirfsoc_dma_chan(struct dma_chan *c)
101{
102 return container_of(c, struct sirfsoc_dma_chan, chan);
103}
104
105/* Convert struct dma_chan to struct sirfsoc_dma */
106static inline struct sirfsoc_dma *dma_chan_to_sirfsoc_dma(struct dma_chan *c)
107{
108 struct sirfsoc_dma_chan *schan = dma_chan_to_sirfsoc_dma_chan(c);
109 return container_of(schan, struct sirfsoc_dma, channels[c->chan_id]);
110}
111
112/* Execute all queued DMA descriptors */
113static void sirfsoc_dma_execute(struct sirfsoc_dma_chan *schan)
114{
115 struct sirfsoc_dma *sdma = dma_chan_to_sirfsoc_dma(&schan->chan);
116 int cid = schan->chan.chan_id;
117 struct sirfsoc_dma_desc *sdesc = NULL;
118
119 /*
120 * lock has been held by functions calling this, so we don't hold
121 * lock again
122 */
123
124 sdesc = list_first_entry(&schan->queued, struct sirfsoc_dma_desc,
125 node);
126 /* Move the first queued descriptor to active list */
Barry Song26fd1222012-09-27 16:36:10 +0800127 list_move_tail(&sdesc->node, &schan->active);
Rongjun Yingca21a142011-10-27 19:22:39 -0700128
129 /* Start the DMA transfer */
130 writel_relaxed(sdesc->width, sdma->base + SIRFSOC_DMA_WIDTH_0 +
131 cid * 4);
132 writel_relaxed(cid | (schan->mode << SIRFSOC_DMA_MODE_CTRL_BIT) |
133 (sdesc->dir << SIRFSOC_DMA_DIR_CTRL_BIT),
134 sdma->base + cid * 0x10 + SIRFSOC_DMA_CH_CTRL);
135 writel_relaxed(sdesc->xlen, sdma->base + cid * 0x10 +
136 SIRFSOC_DMA_CH_XLEN);
137 writel_relaxed(sdesc->ylen, sdma->base + cid * 0x10 +
138 SIRFSOC_DMA_CH_YLEN);
139 writel_relaxed(readl_relaxed(sdma->base + SIRFSOC_DMA_INT_EN) |
140 (1 << cid), sdma->base + SIRFSOC_DMA_INT_EN);
141
142 /*
143 * writel has an implict memory write barrier to make sure data is
144 * flushed into memory before starting DMA
145 */
146 writel(sdesc->addr >> 2, sdma->base + cid * 0x10 + SIRFSOC_DMA_CH_ADDR);
147
148 if (sdesc->cyclic) {
149 writel((1 << cid) | 1 << (cid + 16) |
150 readl_relaxed(sdma->base + SIRFSOC_DMA_CH_LOOP_CTRL),
151 sdma->base + SIRFSOC_DMA_CH_LOOP_CTRL);
152 schan->happened_cyclic = schan->completed_cyclic = 0;
153 }
154}
155
156/* Interrupt handler */
157static irqreturn_t sirfsoc_dma_irq(int irq, void *data)
158{
159 struct sirfsoc_dma *sdma = data;
160 struct sirfsoc_dma_chan *schan;
161 struct sirfsoc_dma_desc *sdesc = NULL;
162 u32 is;
163 int ch;
164
165 is = readl(sdma->base + SIRFSOC_DMA_CH_INT);
166 while ((ch = fls(is) - 1) >= 0) {
167 is &= ~(1 << ch);
168 writel_relaxed(1 << ch, sdma->base + SIRFSOC_DMA_CH_INT);
169 schan = &sdma->channels[ch];
170
171 spin_lock(&schan->lock);
172
173 sdesc = list_first_entry(&schan->active, struct sirfsoc_dma_desc,
174 node);
175 if (!sdesc->cyclic) {
176 /* Execute queued descriptors */
177 list_splice_tail_init(&schan->active, &schan->completed);
178 if (!list_empty(&schan->queued))
179 sirfsoc_dma_execute(schan);
180 } else
181 schan->happened_cyclic++;
182
183 spin_unlock(&schan->lock);
184 }
185
186 /* Schedule tasklet */
187 tasklet_schedule(&sdma->tasklet);
188
189 return IRQ_HANDLED;
190}
191
192/* process completed descriptors */
193static void sirfsoc_dma_process_completed(struct sirfsoc_dma *sdma)
194{
195 dma_cookie_t last_cookie = 0;
196 struct sirfsoc_dma_chan *schan;
197 struct sirfsoc_dma_desc *sdesc;
198 struct dma_async_tx_descriptor *desc;
199 unsigned long flags;
200 unsigned long happened_cyclic;
201 LIST_HEAD(list);
202 int i;
203
204 for (i = 0; i < sdma->dma.chancnt; i++) {
205 schan = &sdma->channels[i];
206
207 /* Get all completed descriptors */
208 spin_lock_irqsave(&schan->lock, flags);
209 if (!list_empty(&schan->completed)) {
210 list_splice_tail_init(&schan->completed, &list);
211 spin_unlock_irqrestore(&schan->lock, flags);
212
213 /* Execute callbacks and run dependencies */
214 list_for_each_entry(sdesc, &list, node) {
215 desc = &sdesc->desc;
216
217 if (desc->callback)
218 desc->callback(desc->callback_param);
219
220 last_cookie = desc->cookie;
221 dma_run_dependencies(desc);
222 }
223
224 /* Free descriptors */
225 spin_lock_irqsave(&schan->lock, flags);
226 list_splice_tail_init(&list, &schan->free);
Russell King - ARM Linux4d4e58d2012-03-06 22:34:06 +0000227 schan->chan.completed_cookie = last_cookie;
Rongjun Yingca21a142011-10-27 19:22:39 -0700228 spin_unlock_irqrestore(&schan->lock, flags);
229 } else {
230 /* for cyclic channel, desc is always in active list */
231 sdesc = list_first_entry(&schan->active, struct sirfsoc_dma_desc,
232 node);
233
234 if (!sdesc || (sdesc && !sdesc->cyclic)) {
235 /* without active cyclic DMA */
236 spin_unlock_irqrestore(&schan->lock, flags);
237 continue;
238 }
239
240 /* cyclic DMA */
241 happened_cyclic = schan->happened_cyclic;
242 spin_unlock_irqrestore(&schan->lock, flags);
243
244 desc = &sdesc->desc;
245 while (happened_cyclic != schan->completed_cyclic) {
246 if (desc->callback)
247 desc->callback(desc->callback_param);
248 schan->completed_cyclic++;
249 }
250 }
251 }
252}
253
254/* DMA Tasklet */
255static void sirfsoc_dma_tasklet(unsigned long data)
256{
257 struct sirfsoc_dma *sdma = (void *)data;
258
259 sirfsoc_dma_process_completed(sdma);
260}
261
262/* Submit descriptor to hardware */
263static dma_cookie_t sirfsoc_dma_tx_submit(struct dma_async_tx_descriptor *txd)
264{
265 struct sirfsoc_dma_chan *schan = dma_chan_to_sirfsoc_dma_chan(txd->chan);
266 struct sirfsoc_dma_desc *sdesc;
267 unsigned long flags;
268 dma_cookie_t cookie;
269
270 sdesc = container_of(txd, struct sirfsoc_dma_desc, desc);
271
272 spin_lock_irqsave(&schan->lock, flags);
273
274 /* Move descriptor to queue */
275 list_move_tail(&sdesc->node, &schan->queued);
276
Russell King - ARM Linux884485e2012-03-06 22:34:46 +0000277 cookie = dma_cookie_assign(txd);
Rongjun Yingca21a142011-10-27 19:22:39 -0700278
279 spin_unlock_irqrestore(&schan->lock, flags);
280
281 return cookie;
282}
283
284static int sirfsoc_dma_slave_config(struct sirfsoc_dma_chan *schan,
285 struct dma_slave_config *config)
286{
287 unsigned long flags;
288
289 if ((config->src_addr_width != DMA_SLAVE_BUSWIDTH_4_BYTES) ||
290 (config->dst_addr_width != DMA_SLAVE_BUSWIDTH_4_BYTES))
291 return -EINVAL;
292
293 spin_lock_irqsave(&schan->lock, flags);
294 schan->mode = (config->src_maxburst == 4 ? 1 : 0);
295 spin_unlock_irqrestore(&schan->lock, flags);
296
297 return 0;
298}
299
300static int sirfsoc_dma_terminate_all(struct sirfsoc_dma_chan *schan)
301{
302 struct sirfsoc_dma *sdma = dma_chan_to_sirfsoc_dma(&schan->chan);
303 int cid = schan->chan.chan_id;
304 unsigned long flags;
305
Barry Song2b99c252012-12-14 11:06:58 +0000306 spin_lock_irqsave(&schan->lock, flags);
307
Barry Songf7d935d2012-11-01 22:54:43 +0800308 if (!sdma->is_marco) {
309 writel_relaxed(readl_relaxed(sdma->base + SIRFSOC_DMA_INT_EN) &
310 ~(1 << cid), sdma->base + SIRFSOC_DMA_INT_EN);
311 writel_relaxed(readl_relaxed(sdma->base + SIRFSOC_DMA_CH_LOOP_CTRL)
312 & ~((1 << cid) | 1 << (cid + 16)),
Rongjun Yingca21a142011-10-27 19:22:39 -0700313 sdma->base + SIRFSOC_DMA_CH_LOOP_CTRL);
Barry Songf7d935d2012-11-01 22:54:43 +0800314 } else {
315 writel_relaxed(1 << cid, sdma->base + SIRFSOC_DMA_INT_EN_CLR);
316 writel_relaxed((1 << cid) | 1 << (cid + 16),
317 sdma->base + SIRFSOC_DMA_CH_LOOP_CTRL_CLR);
318 }
319
Rongjun Yingca21a142011-10-27 19:22:39 -0700320 writel_relaxed(1 << cid, sdma->base + SIRFSOC_DMA_CH_VALID);
321
Rongjun Yingca21a142011-10-27 19:22:39 -0700322 list_splice_tail_init(&schan->active, &schan->free);
323 list_splice_tail_init(&schan->queued, &schan->free);
Barry Song2b99c252012-12-14 11:06:58 +0000324
Rongjun Yingca21a142011-10-27 19:22:39 -0700325 spin_unlock_irqrestore(&schan->lock, flags);
326
327 return 0;
328}
329
Barry Song2518d1d2012-12-14 10:59:22 +0000330static int sirfsoc_dma_pause_chan(struct sirfsoc_dma_chan *schan)
331{
332 struct sirfsoc_dma *sdma = dma_chan_to_sirfsoc_dma(&schan->chan);
333 int cid = schan->chan.chan_id;
334 unsigned long flags;
335
336 spin_lock_irqsave(&schan->lock, flags);
337
338 if (!sdma->is_marco)
339 writel_relaxed(readl_relaxed(sdma->base + SIRFSOC_DMA_CH_LOOP_CTRL)
340 & ~((1 << cid) | 1 << (cid + 16)),
341 sdma->base + SIRFSOC_DMA_CH_LOOP_CTRL);
342 else
343 writel_relaxed((1 << cid) | 1 << (cid + 16),
344 sdma->base + SIRFSOC_DMA_CH_LOOP_CTRL_CLR);
345
346 spin_unlock_irqrestore(&schan->lock, flags);
347
348 return 0;
349}
350
351static int sirfsoc_dma_resume_chan(struct sirfsoc_dma_chan *schan)
352{
353 struct sirfsoc_dma *sdma = dma_chan_to_sirfsoc_dma(&schan->chan);
354 int cid = schan->chan.chan_id;
355 unsigned long flags;
356
357 spin_lock_irqsave(&schan->lock, flags);
358
359 if (!sdma->is_marco)
360 writel_relaxed(readl_relaxed(sdma->base + SIRFSOC_DMA_CH_LOOP_CTRL)
361 | ((1 << cid) | 1 << (cid + 16)),
362 sdma->base + SIRFSOC_DMA_CH_LOOP_CTRL);
363 else
364 writel_relaxed((1 << cid) | 1 << (cid + 16),
365 sdma->base + SIRFSOC_DMA_CH_LOOP_CTRL);
366
Rongjun Yingca21a142011-10-27 19:22:39 -0700367 spin_unlock_irqrestore(&schan->lock, flags);
368
369 return 0;
370}
371
372static int sirfsoc_dma_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd,
373 unsigned long arg)
374{
375 struct dma_slave_config *config;
376 struct sirfsoc_dma_chan *schan = dma_chan_to_sirfsoc_dma_chan(chan);
377
378 switch (cmd) {
Barry Song2518d1d2012-12-14 10:59:22 +0000379 case DMA_PAUSE:
380 return sirfsoc_dma_pause_chan(schan);
381 case DMA_RESUME:
382 return sirfsoc_dma_resume_chan(schan);
Rongjun Yingca21a142011-10-27 19:22:39 -0700383 case DMA_TERMINATE_ALL:
384 return sirfsoc_dma_terminate_all(schan);
385 case DMA_SLAVE_CONFIG:
386 config = (struct dma_slave_config *)arg;
387 return sirfsoc_dma_slave_config(schan, config);
388
389 default:
390 break;
391 }
392
393 return -ENOSYS;
394}
395
396/* Alloc channel resources */
397static int sirfsoc_dma_alloc_chan_resources(struct dma_chan *chan)
398{
399 struct sirfsoc_dma *sdma = dma_chan_to_sirfsoc_dma(chan);
400 struct sirfsoc_dma_chan *schan = dma_chan_to_sirfsoc_dma_chan(chan);
401 struct sirfsoc_dma_desc *sdesc;
402 unsigned long flags;
403 LIST_HEAD(descs);
404 int i;
405
Barry Song2a766892013-07-30 17:44:34 +0800406 pm_runtime_get_sync(sdma->dma.dev);
407
Rongjun Yingca21a142011-10-27 19:22:39 -0700408 /* Alloc descriptors for this channel */
409 for (i = 0; i < SIRFSOC_DMA_DESCRIPTORS; i++) {
410 sdesc = kzalloc(sizeof(*sdesc), GFP_KERNEL);
411 if (!sdesc) {
412 dev_notice(sdma->dma.dev, "Memory allocation error. "
413 "Allocated only %u descriptors\n", i);
414 break;
415 }
416
417 dma_async_tx_descriptor_init(&sdesc->desc, chan);
418 sdesc->desc.flags = DMA_CTRL_ACK;
419 sdesc->desc.tx_submit = sirfsoc_dma_tx_submit;
420
421 list_add_tail(&sdesc->node, &descs);
422 }
423
424 /* Return error only if no descriptors were allocated */
425 if (i == 0)
426 return -ENOMEM;
427
428 spin_lock_irqsave(&schan->lock, flags);
429
430 list_splice_tail_init(&descs, &schan->free);
431 spin_unlock_irqrestore(&schan->lock, flags);
432
433 return i;
434}
435
436/* Free channel resources */
437static void sirfsoc_dma_free_chan_resources(struct dma_chan *chan)
438{
439 struct sirfsoc_dma_chan *schan = dma_chan_to_sirfsoc_dma_chan(chan);
Barry Song2a766892013-07-30 17:44:34 +0800440 struct sirfsoc_dma *sdma = dma_chan_to_sirfsoc_dma(chan);
Rongjun Yingca21a142011-10-27 19:22:39 -0700441 struct sirfsoc_dma_desc *sdesc, *tmp;
442 unsigned long flags;
443 LIST_HEAD(descs);
444
445 spin_lock_irqsave(&schan->lock, flags);
446
447 /* Channel must be idle */
448 BUG_ON(!list_empty(&schan->prepared));
449 BUG_ON(!list_empty(&schan->queued));
450 BUG_ON(!list_empty(&schan->active));
451 BUG_ON(!list_empty(&schan->completed));
452
453 /* Move data */
454 list_splice_tail_init(&schan->free, &descs);
455
456 spin_unlock_irqrestore(&schan->lock, flags);
457
458 /* Free descriptors */
459 list_for_each_entry_safe(sdesc, tmp, &descs, node)
460 kfree(sdesc);
Barry Song2a766892013-07-30 17:44:34 +0800461
462 pm_runtime_put(sdma->dma.dev);
Rongjun Yingca21a142011-10-27 19:22:39 -0700463}
464
465/* Send pending descriptor to hardware */
466static void sirfsoc_dma_issue_pending(struct dma_chan *chan)
467{
468 struct sirfsoc_dma_chan *schan = dma_chan_to_sirfsoc_dma_chan(chan);
469 unsigned long flags;
470
471 spin_lock_irqsave(&schan->lock, flags);
472
473 if (list_empty(&schan->active) && !list_empty(&schan->queued))
474 sirfsoc_dma_execute(schan);
475
476 spin_unlock_irqrestore(&schan->lock, flags);
477}
478
479/* Check request completion status */
480static enum dma_status
481sirfsoc_dma_tx_status(struct dma_chan *chan, dma_cookie_t cookie,
482 struct dma_tx_state *txstate)
483{
Rongjun Yingadd93b52013-05-14 23:03:20 +0800484 struct sirfsoc_dma *sdma = dma_chan_to_sirfsoc_dma(chan);
Rongjun Yingca21a142011-10-27 19:22:39 -0700485 struct sirfsoc_dma_chan *schan = dma_chan_to_sirfsoc_dma_chan(chan);
486 unsigned long flags;
Russell King - ARM Linux96a2af42012-03-06 22:35:27 +0000487 enum dma_status ret;
Rongjun Yingadd93b52013-05-14 23:03:20 +0800488 struct sirfsoc_dma_desc *sdesc;
489 int cid = schan->chan.chan_id;
490 unsigned long dma_pos;
491 unsigned long dma_request_bytes;
492 unsigned long residue;
Rongjun Yingca21a142011-10-27 19:22:39 -0700493
494 spin_lock_irqsave(&schan->lock, flags);
Rongjun Yingadd93b52013-05-14 23:03:20 +0800495
496 sdesc = list_first_entry(&schan->active, struct sirfsoc_dma_desc,
497 node);
498 dma_request_bytes = (sdesc->xlen + 1) * (sdesc->ylen + 1) *
499 (sdesc->width * SIRFSOC_DMA_WORD_LEN);
500
Russell King - ARM Linux96a2af42012-03-06 22:35:27 +0000501 ret = dma_cookie_status(chan, cookie, txstate);
Rongjun Yingadd93b52013-05-14 23:03:20 +0800502 dma_pos = readl_relaxed(sdma->base + cid * 0x10 + SIRFSOC_DMA_CH_ADDR)
503 << 2;
504 residue = dma_request_bytes - (dma_pos - sdesc->addr);
505 dma_set_residue(txstate, residue);
506
Rongjun Yingca21a142011-10-27 19:22:39 -0700507 spin_unlock_irqrestore(&schan->lock, flags);
508
Russell King - ARM Linux96a2af42012-03-06 22:35:27 +0000509 return ret;
Rongjun Yingca21a142011-10-27 19:22:39 -0700510}
511
512static struct dma_async_tx_descriptor *sirfsoc_dma_prep_interleaved(
513 struct dma_chan *chan, struct dma_interleaved_template *xt,
514 unsigned long flags)
515{
516 struct sirfsoc_dma *sdma = dma_chan_to_sirfsoc_dma(chan);
517 struct sirfsoc_dma_chan *schan = dma_chan_to_sirfsoc_dma_chan(chan);
518 struct sirfsoc_dma_desc *sdesc = NULL;
519 unsigned long iflags;
520 int ret;
521
Barry Song5997e082012-09-27 16:35:38 +0800522 if ((xt->dir != DMA_MEM_TO_DEV) && (xt->dir != DMA_DEV_TO_MEM)) {
Rongjun Yingca21a142011-10-27 19:22:39 -0700523 ret = -EINVAL;
524 goto err_dir;
525 }
526
527 /* Get free descriptor */
528 spin_lock_irqsave(&schan->lock, iflags);
529 if (!list_empty(&schan->free)) {
530 sdesc = list_first_entry(&schan->free, struct sirfsoc_dma_desc,
531 node);
532 list_del(&sdesc->node);
533 }
534 spin_unlock_irqrestore(&schan->lock, iflags);
535
536 if (!sdesc) {
537 /* try to free completed descriptors */
538 sirfsoc_dma_process_completed(sdma);
539 ret = 0;
540 goto no_desc;
541 }
542
543 /* Place descriptor in prepared list */
544 spin_lock_irqsave(&schan->lock, iflags);
545
546 /*
547 * Number of chunks in a frame can only be 1 for prima2
548 * and ylen (number of frame - 1) must be at least 0
549 */
550 if ((xt->frame_size == 1) && (xt->numf > 0)) {
551 sdesc->cyclic = 0;
552 sdesc->xlen = xt->sgl[0].size / SIRFSOC_DMA_WORD_LEN;
553 sdesc->width = (xt->sgl[0].size + xt->sgl[0].icg) /
554 SIRFSOC_DMA_WORD_LEN;
555 sdesc->ylen = xt->numf - 1;
556 if (xt->dir == DMA_MEM_TO_DEV) {
557 sdesc->addr = xt->src_start;
558 sdesc->dir = 1;
559 } else {
560 sdesc->addr = xt->dst_start;
561 sdesc->dir = 0;
562 }
563
564 list_add_tail(&sdesc->node, &schan->prepared);
565 } else {
566 pr_err("sirfsoc DMA Invalid xfer\n");
567 ret = -EINVAL;
568 goto err_xfer;
569 }
570 spin_unlock_irqrestore(&schan->lock, iflags);
571
572 return &sdesc->desc;
573err_xfer:
574 spin_unlock_irqrestore(&schan->lock, iflags);
575no_desc:
576err_dir:
577 return ERR_PTR(ret);
578}
579
580static struct dma_async_tx_descriptor *
581sirfsoc_dma_prep_cyclic(struct dma_chan *chan, dma_addr_t addr,
582 size_t buf_len, size_t period_len,
Peter Ujfalusiec8b5e42012-09-14 15:05:47 +0300583 enum dma_transfer_direction direction, unsigned long flags, void *context)
Rongjun Yingca21a142011-10-27 19:22:39 -0700584{
585 struct sirfsoc_dma_chan *schan = dma_chan_to_sirfsoc_dma_chan(chan);
586 struct sirfsoc_dma_desc *sdesc = NULL;
587 unsigned long iflags;
588
589 /*
590 * we only support cycle transfer with 2 period
591 * If the X-length is set to 0, it would be the loop mode.
592 * The DMA address keeps increasing until reaching the end of a loop
593 * area whose size is defined by (DMA_WIDTH x (Y_LENGTH + 1)). Then
594 * the DMA address goes back to the beginning of this area.
595 * In loop mode, the DMA data region is divided into two parts, BUFA
596 * and BUFB. DMA controller generates interrupts twice in each loop:
597 * when the DMA address reaches the end of BUFA or the end of the
598 * BUFB
599 */
600 if (buf_len != 2 * period_len)
601 return ERR_PTR(-EINVAL);
602
603 /* Get free descriptor */
604 spin_lock_irqsave(&schan->lock, iflags);
605 if (!list_empty(&schan->free)) {
606 sdesc = list_first_entry(&schan->free, struct sirfsoc_dma_desc,
607 node);
608 list_del(&sdesc->node);
609 }
610 spin_unlock_irqrestore(&schan->lock, iflags);
611
612 if (!sdesc)
Jingoo Han696b4ff2013-08-06 19:37:56 +0900613 return NULL;
Rongjun Yingca21a142011-10-27 19:22:39 -0700614
615 /* Place descriptor in prepared list */
616 spin_lock_irqsave(&schan->lock, iflags);
617 sdesc->addr = addr;
618 sdesc->cyclic = 1;
619 sdesc->xlen = 0;
620 sdesc->ylen = buf_len / SIRFSOC_DMA_WORD_LEN - 1;
621 sdesc->width = 1;
622 list_add_tail(&sdesc->node, &schan->prepared);
623 spin_unlock_irqrestore(&schan->lock, iflags);
624
625 return &sdesc->desc;
626}
627
628/*
629 * The DMA controller consists of 16 independent DMA channels.
630 * Each channel is allocated to a different function
631 */
632bool sirfsoc_dma_filter_id(struct dma_chan *chan, void *chan_id)
633{
634 unsigned int ch_nr = (unsigned int) chan_id;
635
636 if (ch_nr == chan->chan_id +
637 chan->device->dev_id * SIRFSOC_DMA_CHANNELS)
638 return true;
639
640 return false;
641}
642EXPORT_SYMBOL(sirfsoc_dma_filter_id);
643
Rongjun Yingba07d812013-12-23 20:19:21 +0800644#define SIRFSOC_DMA_BUSWIDTHS \
645 (BIT(DMA_SLAVE_BUSWIDTH_UNDEFINED) | \
646 BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) | \
647 BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) | \
648 BIT(DMA_SLAVE_BUSWIDTH_4_BYTES) | \
649 BIT(DMA_SLAVE_BUSWIDTH_8_BYTES))
650
651static int sirfsoc_dma_device_slave_caps(struct dma_chan *dchan,
652 struct dma_slave_caps *caps)
653{
654 caps->src_addr_widths = SIRFSOC_DMA_BUSWIDTHS;
655 caps->dstn_addr_widths = SIRFSOC_DMA_BUSWIDTHS;
656 caps->directions = BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV);
657 caps->cmd_pause = true;
658 caps->cmd_terminate = true;
659
660 return 0;
661}
662
Barry Song2e041c92014-03-27 15:49:31 +0800663static struct dma_chan *of_dma_sirfsoc_xlate(struct of_phandle_args *dma_spec,
664 struct of_dma *ofdma)
665{
666 struct sirfsoc_dma *sdma = ofdma->of_dma_data;
667 unsigned int request = dma_spec->args[0];
668
Dan Carpenterf3817e72014-04-03 10:29:33 +0300669 if (request >= SIRFSOC_DMA_CHANNELS)
Barry Song2e041c92014-03-27 15:49:31 +0800670 return NULL;
671
672 return dma_get_slave_channel(&sdma->channels[request].chan);
673}
674
Bill Pemberton463a1f82012-11-19 13:22:55 -0500675static int sirfsoc_dma_probe(struct platform_device *op)
Rongjun Yingca21a142011-10-27 19:22:39 -0700676{
677 struct device_node *dn = op->dev.of_node;
678 struct device *dev = &op->dev;
679 struct dma_device *dma;
680 struct sirfsoc_dma *sdma;
681 struct sirfsoc_dma_chan *schan;
682 struct resource res;
683 ulong regs_start, regs_size;
684 u32 id;
685 int ret, i;
686
687 sdma = devm_kzalloc(dev, sizeof(*sdma), GFP_KERNEL);
688 if (!sdma) {
689 dev_err(dev, "Memory exhausted!\n");
690 return -ENOMEM;
691 }
692
Barry Songf7d935d2012-11-01 22:54:43 +0800693 if (of_device_is_compatible(dn, "sirf,marco-dmac"))
694 sdma->is_marco = true;
695
Rongjun Yingca21a142011-10-27 19:22:39 -0700696 if (of_property_read_u32(dn, "cell-index", &id)) {
697 dev_err(dev, "Fail to get DMAC index\n");
Julia Lawall94d39012012-08-04 10:35:30 +0200698 return -ENODEV;
Rongjun Yingca21a142011-10-27 19:22:39 -0700699 }
700
701 sdma->irq = irq_of_parse_and_map(dn, 0);
702 if (sdma->irq == NO_IRQ) {
703 dev_err(dev, "Error mapping IRQ!\n");
Julia Lawall94d39012012-08-04 10:35:30 +0200704 return -EINVAL;
Rongjun Yingca21a142011-10-27 19:22:39 -0700705 }
706
Barry Songa7e34062013-03-18 16:33:43 +0800707 sdma->clk = devm_clk_get(dev, NULL);
708 if (IS_ERR(sdma->clk)) {
709 dev_err(dev, "failed to get a clock.\n");
710 return PTR_ERR(sdma->clk);
711 }
712
Rongjun Yingca21a142011-10-27 19:22:39 -0700713 ret = of_address_to_resource(dn, 0, &res);
714 if (ret) {
715 dev_err(dev, "Error parsing memory region!\n");
Julia Lawall94d39012012-08-04 10:35:30 +0200716 goto irq_dispose;
Rongjun Yingca21a142011-10-27 19:22:39 -0700717 }
718
719 regs_start = res.start;
720 regs_size = resource_size(&res);
721
722 sdma->base = devm_ioremap(dev, regs_start, regs_size);
723 if (!sdma->base) {
724 dev_err(dev, "Error mapping memory region!\n");
725 ret = -ENOMEM;
726 goto irq_dispose;
727 }
728
Julia Lawall94d39012012-08-04 10:35:30 +0200729 ret = request_irq(sdma->irq, &sirfsoc_dma_irq, 0, DRV_NAME, sdma);
Rongjun Yingca21a142011-10-27 19:22:39 -0700730 if (ret) {
731 dev_err(dev, "Error requesting IRQ!\n");
732 ret = -EINVAL;
Julia Lawall94d39012012-08-04 10:35:30 +0200733 goto irq_dispose;
Rongjun Yingca21a142011-10-27 19:22:39 -0700734 }
735
736 dma = &sdma->dma;
737 dma->dev = dev;
738 dma->chancnt = SIRFSOC_DMA_CHANNELS;
739
740 dma->device_alloc_chan_resources = sirfsoc_dma_alloc_chan_resources;
741 dma->device_free_chan_resources = sirfsoc_dma_free_chan_resources;
742 dma->device_issue_pending = sirfsoc_dma_issue_pending;
743 dma->device_control = sirfsoc_dma_control;
744 dma->device_tx_status = sirfsoc_dma_tx_status;
745 dma->device_prep_interleaved_dma = sirfsoc_dma_prep_interleaved;
746 dma->device_prep_dma_cyclic = sirfsoc_dma_prep_cyclic;
Rongjun Yingba07d812013-12-23 20:19:21 +0800747 dma->device_slave_caps = sirfsoc_dma_device_slave_caps;
Rongjun Yingca21a142011-10-27 19:22:39 -0700748
749 INIT_LIST_HEAD(&dma->channels);
750 dma_cap_set(DMA_SLAVE, dma->cap_mask);
751 dma_cap_set(DMA_CYCLIC, dma->cap_mask);
752 dma_cap_set(DMA_INTERLEAVE, dma->cap_mask);
753 dma_cap_set(DMA_PRIVATE, dma->cap_mask);
754
755 for (i = 0; i < dma->chancnt; i++) {
756 schan = &sdma->channels[i];
757
758 schan->chan.device = dma;
Russell King - ARM Linuxd3ee98cdc2012-03-06 22:35:47 +0000759 dma_cookie_init(&schan->chan);
Rongjun Yingca21a142011-10-27 19:22:39 -0700760
761 INIT_LIST_HEAD(&schan->free);
762 INIT_LIST_HEAD(&schan->prepared);
763 INIT_LIST_HEAD(&schan->queued);
764 INIT_LIST_HEAD(&schan->active);
765 INIT_LIST_HEAD(&schan->completed);
766
767 spin_lock_init(&schan->lock);
768 list_add_tail(&schan->chan.device_node, &dma->channels);
769 }
770
771 tasklet_init(&sdma->tasklet, sirfsoc_dma_tasklet, (unsigned long)sdma);
772
773 /* Register DMA engine */
774 dev_set_drvdata(dev, sdma);
Barry Song2a766892013-07-30 17:44:34 +0800775
Rongjun Yingca21a142011-10-27 19:22:39 -0700776 ret = dma_async_device_register(dma);
777 if (ret)
778 goto free_irq;
779
Barry Song2e041c92014-03-27 15:49:31 +0800780 /* Device-tree DMA controller registration */
781 ret = of_dma_controller_register(dn, of_dma_sirfsoc_xlate, sdma);
782 if (ret) {
783 dev_err(dev, "failed to register DMA controller\n");
784 goto unreg_dma_dev;
785 }
786
Barry Song2a766892013-07-30 17:44:34 +0800787 pm_runtime_enable(&op->dev);
Rongjun Yingca21a142011-10-27 19:22:39 -0700788 dev_info(dev, "initialized SIRFSOC DMAC driver\n");
789
790 return 0;
791
Barry Song2e041c92014-03-27 15:49:31 +0800792unreg_dma_dev:
793 dma_async_device_unregister(dma);
Rongjun Yingca21a142011-10-27 19:22:39 -0700794free_irq:
Julia Lawall94d39012012-08-04 10:35:30 +0200795 free_irq(sdma->irq, sdma);
Rongjun Yingca21a142011-10-27 19:22:39 -0700796irq_dispose:
797 irq_dispose_mapping(sdma->irq);
Rongjun Yingca21a142011-10-27 19:22:39 -0700798 return ret;
799}
800
Greg Kroah-Hartman4bf27b82012-12-21 15:09:59 -0800801static int sirfsoc_dma_remove(struct platform_device *op)
Rongjun Yingca21a142011-10-27 19:22:39 -0700802{
803 struct device *dev = &op->dev;
804 struct sirfsoc_dma *sdma = dev_get_drvdata(dev);
805
Barry Song2e041c92014-03-27 15:49:31 +0800806 of_dma_controller_free(op->dev.of_node);
Rongjun Yingca21a142011-10-27 19:22:39 -0700807 dma_async_device_unregister(&sdma->dma);
Julia Lawall94d39012012-08-04 10:35:30 +0200808 free_irq(sdma->irq, sdma);
Rongjun Yingca21a142011-10-27 19:22:39 -0700809 irq_dispose_mapping(sdma->irq);
Barry Song2a766892013-07-30 17:44:34 +0800810 pm_runtime_disable(&op->dev);
811 if (!pm_runtime_status_suspended(&op->dev))
812 sirfsoc_dma_runtime_suspend(&op->dev);
813
Rongjun Yingca21a142011-10-27 19:22:39 -0700814 return 0;
815}
816
Barry Song2a766892013-07-30 17:44:34 +0800817static int sirfsoc_dma_runtime_suspend(struct device *dev)
818{
819 struct sirfsoc_dma *sdma = dev_get_drvdata(dev);
820
821 clk_disable_unprepare(sdma->clk);
822 return 0;
823}
824
825static int sirfsoc_dma_runtime_resume(struct device *dev)
826{
827 struct sirfsoc_dma *sdma = dev_get_drvdata(dev);
828 int ret;
829
830 ret = clk_prepare_enable(sdma->clk);
831 if (ret < 0) {
832 dev_err(dev, "clk_enable failed: %d\n", ret);
833 return ret;
834 }
835 return 0;
836}
837
838static int sirfsoc_dma_pm_suspend(struct device *dev)
839{
840 struct sirfsoc_dma *sdma = dev_get_drvdata(dev);
841 struct sirfsoc_dma_regs *save = &sdma->regs_save;
842 struct sirfsoc_dma_desc *sdesc;
843 struct sirfsoc_dma_chan *schan;
844 int ch;
845 int ret;
846
847 /*
848 * if we were runtime-suspended before, resume to enable clock
849 * before accessing register
850 */
851 if (pm_runtime_status_suspended(dev)) {
852 ret = sirfsoc_dma_runtime_resume(dev);
853 if (ret < 0)
854 return ret;
855 }
856
857 /*
858 * DMA controller will lose all registers while suspending
859 * so we need to save registers for active channels
860 */
861 for (ch = 0; ch < SIRFSOC_DMA_CHANNELS; ch++) {
862 schan = &sdma->channels[ch];
863 if (list_empty(&schan->active))
864 continue;
865 sdesc = list_first_entry(&schan->active,
866 struct sirfsoc_dma_desc,
867 node);
868 save->ctrl[ch] = readl_relaxed(sdma->base +
869 ch * 0x10 + SIRFSOC_DMA_CH_CTRL);
870 }
871 save->interrupt_en = readl_relaxed(sdma->base + SIRFSOC_DMA_INT_EN);
872
873 /* Disable clock */
874 sirfsoc_dma_runtime_suspend(dev);
875
876 return 0;
877}
878
879static int sirfsoc_dma_pm_resume(struct device *dev)
880{
881 struct sirfsoc_dma *sdma = dev_get_drvdata(dev);
882 struct sirfsoc_dma_regs *save = &sdma->regs_save;
883 struct sirfsoc_dma_desc *sdesc;
884 struct sirfsoc_dma_chan *schan;
885 int ch;
886 int ret;
887
888 /* Enable clock before accessing register */
889 ret = sirfsoc_dma_runtime_resume(dev);
890 if (ret < 0)
891 return ret;
892
893 writel_relaxed(save->interrupt_en, sdma->base + SIRFSOC_DMA_INT_EN);
894 for (ch = 0; ch < SIRFSOC_DMA_CHANNELS; ch++) {
895 schan = &sdma->channels[ch];
896 if (list_empty(&schan->active))
897 continue;
898 sdesc = list_first_entry(&schan->active,
899 struct sirfsoc_dma_desc,
900 node);
901 writel_relaxed(sdesc->width,
902 sdma->base + SIRFSOC_DMA_WIDTH_0 + ch * 4);
903 writel_relaxed(sdesc->xlen,
904 sdma->base + ch * 0x10 + SIRFSOC_DMA_CH_XLEN);
905 writel_relaxed(sdesc->ylen,
906 sdma->base + ch * 0x10 + SIRFSOC_DMA_CH_YLEN);
907 writel_relaxed(save->ctrl[ch],
908 sdma->base + ch * 0x10 + SIRFSOC_DMA_CH_CTRL);
909 writel_relaxed(sdesc->addr >> 2,
910 sdma->base + ch * 0x10 + SIRFSOC_DMA_CH_ADDR);
911 }
912
913 /* if we were runtime-suspended before, suspend again */
914 if (pm_runtime_status_suspended(dev))
915 sirfsoc_dma_runtime_suspend(dev);
916
917 return 0;
918}
919
920static const struct dev_pm_ops sirfsoc_dma_pm_ops = {
921 SET_RUNTIME_PM_OPS(sirfsoc_dma_runtime_suspend, sirfsoc_dma_runtime_resume, NULL)
922 SET_SYSTEM_SLEEP_PM_OPS(sirfsoc_dma_pm_suspend, sirfsoc_dma_pm_resume)
923};
924
Rongjun Yingca21a142011-10-27 19:22:39 -0700925static struct of_device_id sirfsoc_dma_match[] = {
926 { .compatible = "sirf,prima2-dmac", },
Barry Songf7d935d2012-11-01 22:54:43 +0800927 { .compatible = "sirf,marco-dmac", },
Rongjun Yingca21a142011-10-27 19:22:39 -0700928 {},
929};
930
931static struct platform_driver sirfsoc_dma_driver = {
932 .probe = sirfsoc_dma_probe,
Bill Pembertona7d6e3e2012-11-19 13:20:04 -0500933 .remove = sirfsoc_dma_remove,
Rongjun Yingca21a142011-10-27 19:22:39 -0700934 .driver = {
935 .name = DRV_NAME,
936 .owner = THIS_MODULE,
Barry Song2a766892013-07-30 17:44:34 +0800937 .pm = &sirfsoc_dma_pm_ops,
Rongjun Yingca21a142011-10-27 19:22:39 -0700938 .of_match_table = sirfsoc_dma_match,
939 },
940};
941
Barry Song42361f22013-04-11 14:09:28 +0800942static __init int sirfsoc_dma_init(void)
943{
944 return platform_driver_register(&sirfsoc_dma_driver);
945}
946
947static void __exit sirfsoc_dma_exit(void)
948{
949 platform_driver_unregister(&sirfsoc_dma_driver);
950}
951
952subsys_initcall(sirfsoc_dma_init);
953module_exit(sirfsoc_dma_exit);
Rongjun Yingca21a142011-10-27 19:22:39 -0700954
955MODULE_AUTHOR("Rongjun Ying <rongjun.ying@csr.com>, "
956 "Barry Song <baohua.song@csr.com>");
957MODULE_DESCRIPTION("SIRFSOC DMA control driver");
958MODULE_LICENSE("GPL v2");