blob: 0253d5aecdb19f1a0871d9f643fbb97f1c666b09 [file] [log] [blame]
Piotr Ziecik0fb6f732010-02-05 03:42:52 +00001/*
2 * Copyright (C) Freescale Semicondutor, Inc. 2007, 2008.
3 * Copyright (C) Semihalf 2009
Ilya Yanokba2eea22010-10-27 01:52:57 +02004 * Copyright (C) Ilya Yanok, Emcraft Systems 2010
Piotr Ziecik0fb6f732010-02-05 03:42:52 +00005 *
6 * Written by Piotr Ziecik <kosmo@semihalf.com>. Hardware description
7 * (defines, structures and comments) was taken from MPC5121 DMA driver
8 * written by Hongjun Chen <hong-jun.chen@freescale.com>.
9 *
10 * Approved as OSADL project by a majority of OSADL members and funded
11 * by OSADL membership fees in 2009; for details see www.osadl.org.
12 *
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License as published by the Free
15 * Software Foundation; either version 2 of the License, or (at your option)
16 * any later version.
17 *
18 * This program is distributed in the hope that it will be useful, but WITHOUT
19 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
21 * more details.
22 *
23 * You should have received a copy of the GNU General Public License along with
24 * this program; if not, write to the Free Software Foundation, Inc., 59
25 * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
26 *
27 * The full GNU General Public License is included in this distribution in the
28 * file called COPYING.
29 */
30
31/*
32 * This is initial version of MPC5121 DMA driver. Only memory to memory
33 * transfers are supported (tested using dmatest module).
34 */
35
36#include <linux/module.h>
37#include <linux/dmaengine.h>
38#include <linux/dma-mapping.h>
39#include <linux/interrupt.h>
40#include <linux/io.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090041#include <linux/slab.h>
Piotr Ziecik0fb6f732010-02-05 03:42:52 +000042#include <linux/of_device.h>
43#include <linux/of_platform.h>
44
45#include <linux/random.h>
46
Russell King - ARM Linuxd2ebfb32012-03-06 22:34:26 +000047#include "dmaengine.h"
48
Piotr Ziecik0fb6f732010-02-05 03:42:52 +000049/* Number of DMA Transfer descriptors allocated per channel */
50#define MPC_DMA_DESCRIPTORS 64
51
52/* Macro definitions */
53#define MPC_DMA_CHANNELS 64
54#define MPC_DMA_TCD_OFFSET 0x1000
55
56/* Arbitration mode of group and channel */
57#define MPC_DMA_DMACR_EDCG (1 << 31)
58#define MPC_DMA_DMACR_ERGA (1 << 3)
59#define MPC_DMA_DMACR_ERCA (1 << 2)
60
61/* Error codes */
62#define MPC_DMA_DMAES_VLD (1 << 31)
63#define MPC_DMA_DMAES_GPE (1 << 15)
64#define MPC_DMA_DMAES_CPE (1 << 14)
65#define MPC_DMA_DMAES_ERRCHN(err) \
66 (((err) >> 8) & 0x3f)
67#define MPC_DMA_DMAES_SAE (1 << 7)
68#define MPC_DMA_DMAES_SOE (1 << 6)
69#define MPC_DMA_DMAES_DAE (1 << 5)
70#define MPC_DMA_DMAES_DOE (1 << 4)
71#define MPC_DMA_DMAES_NCE (1 << 3)
72#define MPC_DMA_DMAES_SGE (1 << 2)
73#define MPC_DMA_DMAES_SBE (1 << 1)
74#define MPC_DMA_DMAES_DBE (1 << 0)
75
Ilya Yanokba2eea22010-10-27 01:52:57 +020076#define MPC_DMA_DMAGPOR_SNOOP_ENABLE (1 << 6)
77
Piotr Ziecik0fb6f732010-02-05 03:42:52 +000078#define MPC_DMA_TSIZE_1 0x00
79#define MPC_DMA_TSIZE_2 0x01
80#define MPC_DMA_TSIZE_4 0x02
81#define MPC_DMA_TSIZE_16 0x04
82#define MPC_DMA_TSIZE_32 0x05
83
84/* MPC5121 DMA engine registers */
85struct __attribute__ ((__packed__)) mpc_dma_regs {
86 /* 0x00 */
87 u32 dmacr; /* DMA control register */
88 u32 dmaes; /* DMA error status */
89 /* 0x08 */
90 u32 dmaerqh; /* DMA enable request high(channels 63~32) */
91 u32 dmaerql; /* DMA enable request low(channels 31~0) */
92 u32 dmaeeih; /* DMA enable error interrupt high(ch63~32) */
93 u32 dmaeeil; /* DMA enable error interrupt low(ch31~0) */
94 /* 0x18 */
95 u8 dmaserq; /* DMA set enable request */
96 u8 dmacerq; /* DMA clear enable request */
97 u8 dmaseei; /* DMA set enable error interrupt */
98 u8 dmaceei; /* DMA clear enable error interrupt */
99 /* 0x1c */
100 u8 dmacint; /* DMA clear interrupt request */
101 u8 dmacerr; /* DMA clear error */
102 u8 dmassrt; /* DMA set start bit */
103 u8 dmacdne; /* DMA clear DONE status bit */
104 /* 0x20 */
105 u32 dmainth; /* DMA interrupt request high(ch63~32) */
106 u32 dmaintl; /* DMA interrupt request low(ch31~0) */
107 u32 dmaerrh; /* DMA error high(ch63~32) */
108 u32 dmaerrl; /* DMA error low(ch31~0) */
109 /* 0x30 */
110 u32 dmahrsh; /* DMA hw request status high(ch63~32) */
111 u32 dmahrsl; /* DMA hardware request status low(ch31~0) */
Ilya Yanokba2eea22010-10-27 01:52:57 +0200112 union {
113 u32 dmaihsa; /* DMA interrupt high select AXE(ch63~32) */
114 u32 dmagpor; /* (General purpose register on MPC8308) */
115 };
Piotr Ziecik0fb6f732010-02-05 03:42:52 +0000116 u32 dmailsa; /* DMA interrupt low select AXE(ch31~0) */
117 /* 0x40 ~ 0xff */
118 u32 reserve0[48]; /* Reserved */
119 /* 0x100 */
120 u8 dchpri[MPC_DMA_CHANNELS];
121 /* DMA channels(0~63) priority */
122};
123
124struct __attribute__ ((__packed__)) mpc_dma_tcd {
125 /* 0x00 */
126 u32 saddr; /* Source address */
127
128 u32 smod:5; /* Source address modulo */
129 u32 ssize:3; /* Source data transfer size */
130 u32 dmod:5; /* Destination address modulo */
131 u32 dsize:3; /* Destination data transfer size */
132 u32 soff:16; /* Signed source address offset */
133
134 /* 0x08 */
135 u32 nbytes; /* Inner "minor" byte count */
136 u32 slast; /* Last source address adjustment */
137 u32 daddr; /* Destination address */
138
139 /* 0x14 */
140 u32 citer_elink:1; /* Enable channel-to-channel linking on
141 * minor loop complete
142 */
143 u32 citer_linkch:6; /* Link channel for minor loop complete */
144 u32 citer:9; /* Current "major" iteration count */
145 u32 doff:16; /* Signed destination address offset */
146
147 /* 0x18 */
148 u32 dlast_sga; /* Last Destination address adjustment/scatter
149 * gather address
150 */
151
152 /* 0x1c */
153 u32 biter_elink:1; /* Enable channel-to-channel linking on major
154 * loop complete
155 */
156 u32 biter_linkch:6;
157 u32 biter:9; /* Beginning "major" iteration count */
158 u32 bwc:2; /* Bandwidth control */
159 u32 major_linkch:6; /* Link channel number */
160 u32 done:1; /* Channel done */
161 u32 active:1; /* Channel active */
162 u32 major_elink:1; /* Enable channel-to-channel linking on major
163 * loop complete
164 */
165 u32 e_sg:1; /* Enable scatter/gather processing */
166 u32 d_req:1; /* Disable request */
167 u32 int_half:1; /* Enable an interrupt when major counter is
168 * half complete
169 */
170 u32 int_maj:1; /* Enable an interrupt when major iteration
171 * count completes
172 */
173 u32 start:1; /* Channel start */
174};
175
176struct mpc_dma_desc {
177 struct dma_async_tx_descriptor desc;
178 struct mpc_dma_tcd *tcd;
179 dma_addr_t tcd_paddr;
180 int error;
181 struct list_head node;
182};
183
184struct mpc_dma_chan {
185 struct dma_chan chan;
186 struct list_head free;
187 struct list_head prepared;
188 struct list_head queued;
189 struct list_head active;
190 struct list_head completed;
191 struct mpc_dma_tcd *tcd;
192 dma_addr_t tcd_paddr;
Piotr Ziecik0fb6f732010-02-05 03:42:52 +0000193
194 /* Lock for this structure */
195 spinlock_t lock;
196};
197
198struct mpc_dma {
199 struct dma_device dma;
200 struct tasklet_struct tasklet;
201 struct mpc_dma_chan channels[MPC_DMA_CHANNELS];
202 struct mpc_dma_regs __iomem *regs;
203 struct mpc_dma_tcd __iomem *tcd;
204 int irq;
Ilya Yanokba2eea22010-10-27 01:52:57 +0200205 int irq2;
Piotr Ziecik0fb6f732010-02-05 03:42:52 +0000206 uint error_status;
Ilya Yanokba2eea22010-10-27 01:52:57 +0200207 int is_mpc8308;
Piotr Ziecik0fb6f732010-02-05 03:42:52 +0000208
209 /* Lock for error_status field in this structure */
210 spinlock_t error_status_lock;
211};
212
213#define DRV_NAME "mpc512x_dma"
214
215/* Convert struct dma_chan to struct mpc_dma_chan */
216static inline struct mpc_dma_chan *dma_chan_to_mpc_dma_chan(struct dma_chan *c)
217{
218 return container_of(c, struct mpc_dma_chan, chan);
219}
220
221/* Convert struct dma_chan to struct mpc_dma */
222static inline struct mpc_dma *dma_chan_to_mpc_dma(struct dma_chan *c)
223{
224 struct mpc_dma_chan *mchan = dma_chan_to_mpc_dma_chan(c);
225 return container_of(mchan, struct mpc_dma, channels[c->chan_id]);
226}
227
228/*
229 * Execute all queued DMA descriptors.
230 *
231 * Following requirements must be met while calling mpc_dma_execute():
232 * a) mchan->lock is acquired,
233 * b) mchan->active list is empty,
234 * c) mchan->queued list contains at least one entry.
235 */
236static void mpc_dma_execute(struct mpc_dma_chan *mchan)
237{
238 struct mpc_dma *mdma = dma_chan_to_mpc_dma(&mchan->chan);
239 struct mpc_dma_desc *first = NULL;
240 struct mpc_dma_desc *prev = NULL;
241 struct mpc_dma_desc *mdesc;
242 int cid = mchan->chan.chan_id;
243
244 /* Move all queued descriptors to active list */
245 list_splice_tail_init(&mchan->queued, &mchan->active);
246
247 /* Chain descriptors into one transaction */
248 list_for_each_entry(mdesc, &mchan->active, node) {
249 if (!first)
250 first = mdesc;
251
252 if (!prev) {
253 prev = mdesc;
254 continue;
255 }
256
257 prev->tcd->dlast_sga = mdesc->tcd_paddr;
258 prev->tcd->e_sg = 1;
259 mdesc->tcd->start = 1;
260
261 prev = mdesc;
262 }
263
Piotr Ziecik0fb6f732010-02-05 03:42:52 +0000264 prev->tcd->int_maj = 1;
265
266 /* Send first descriptor in chain into hardware */
267 memcpy_toio(&mdma->tcd[cid], first->tcd, sizeof(struct mpc_dma_tcd));
Ilya Yanok6504cf32010-10-27 01:52:55 +0200268
269 if (first != prev)
270 mdma->tcd[cid].e_sg = 1;
Piotr Ziecik0fb6f732010-02-05 03:42:52 +0000271 out_8(&mdma->regs->dmassrt, cid);
272}
273
274/* Handle interrupt on one half of DMA controller (32 channels) */
275static void mpc_dma_irq_process(struct mpc_dma *mdma, u32 is, u32 es, int off)
276{
277 struct mpc_dma_chan *mchan;
278 struct mpc_dma_desc *mdesc;
279 u32 status = is | es;
280 int ch;
281
282 while ((ch = fls(status) - 1) >= 0) {
283 status &= ~(1 << ch);
284 mchan = &mdma->channels[ch + off];
285
286 spin_lock(&mchan->lock);
287
Ilya Yanok28625592010-10-27 01:52:56 +0200288 out_8(&mdma->regs->dmacint, ch + off);
289 out_8(&mdma->regs->dmacerr, ch + off);
290
Piotr Ziecik0fb6f732010-02-05 03:42:52 +0000291 /* Check error status */
292 if (es & (1 << ch))
293 list_for_each_entry(mdesc, &mchan->active, node)
294 mdesc->error = -EIO;
295
296 /* Execute queued descriptors */
297 list_splice_tail_init(&mchan->active, &mchan->completed);
298 if (!list_empty(&mchan->queued))
299 mpc_dma_execute(mchan);
300
301 spin_unlock(&mchan->lock);
302 }
303}
304
305/* Interrupt handler */
306static irqreturn_t mpc_dma_irq(int irq, void *data)
307{
308 struct mpc_dma *mdma = data;
309 uint es;
310
311 /* Save error status register */
312 es = in_be32(&mdma->regs->dmaes);
313 spin_lock(&mdma->error_status_lock);
314 if ((es & MPC_DMA_DMAES_VLD) && mdma->error_status == 0)
315 mdma->error_status = es;
316 spin_unlock(&mdma->error_status_lock);
317
318 /* Handle interrupt on each channel */
Ilya Yanokba2eea22010-10-27 01:52:57 +0200319 if (mdma->dma.chancnt > 32) {
320 mpc_dma_irq_process(mdma, in_be32(&mdma->regs->dmainth),
Piotr Ziecik0fb6f732010-02-05 03:42:52 +0000321 in_be32(&mdma->regs->dmaerrh), 32);
Ilya Yanokba2eea22010-10-27 01:52:57 +0200322 }
Piotr Ziecik0fb6f732010-02-05 03:42:52 +0000323 mpc_dma_irq_process(mdma, in_be32(&mdma->regs->dmaintl),
324 in_be32(&mdma->regs->dmaerrl), 0);
325
Piotr Ziecik0fb6f732010-02-05 03:42:52 +0000326 /* Schedule tasklet */
327 tasklet_schedule(&mdma->tasklet);
328
329 return IRQ_HANDLED;
330}
331
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300332/* process completed descriptors */
Ilya Yanoka2769912010-10-27 01:52:58 +0200333static void mpc_dma_process_completed(struct mpc_dma *mdma)
Piotr Ziecik0fb6f732010-02-05 03:42:52 +0000334{
Piotr Ziecik0fb6f732010-02-05 03:42:52 +0000335 dma_cookie_t last_cookie = 0;
336 struct mpc_dma_chan *mchan;
337 struct mpc_dma_desc *mdesc;
338 struct dma_async_tx_descriptor *desc;
339 unsigned long flags;
340 LIST_HEAD(list);
Piotr Ziecik0fb6f732010-02-05 03:42:52 +0000341 int i;
342
Ilya Yanoka2769912010-10-27 01:52:58 +0200343 for (i = 0; i < mdma->dma.chancnt; i++) {
344 mchan = &mdma->channels[i];
345
346 /* Get all completed descriptors */
347 spin_lock_irqsave(&mchan->lock, flags);
348 if (!list_empty(&mchan->completed))
349 list_splice_tail_init(&mchan->completed, &list);
350 spin_unlock_irqrestore(&mchan->lock, flags);
351
352 if (list_empty(&list))
353 continue;
354
355 /* Execute callbacks and run dependencies */
356 list_for_each_entry(mdesc, &list, node) {
357 desc = &mdesc->desc;
358
359 if (desc->callback)
360 desc->callback(desc->callback_param);
361
362 last_cookie = desc->cookie;
363 dma_run_dependencies(desc);
364 }
365
366 /* Free descriptors */
367 spin_lock_irqsave(&mchan->lock, flags);
368 list_splice_tail_init(&list, &mchan->free);
Russell King - ARM Linux4d4e58d2012-03-06 22:34:06 +0000369 mchan->chan.completed_cookie = last_cookie;
Ilya Yanoka2769912010-10-27 01:52:58 +0200370 spin_unlock_irqrestore(&mchan->lock, flags);
371 }
372}
373
374/* DMA Tasklet */
375static void mpc_dma_tasklet(unsigned long data)
376{
377 struct mpc_dma *mdma = (void *)data;
378 unsigned long flags;
379 uint es;
380
Piotr Ziecik0fb6f732010-02-05 03:42:52 +0000381 spin_lock_irqsave(&mdma->error_status_lock, flags);
382 es = mdma->error_status;
383 mdma->error_status = 0;
384 spin_unlock_irqrestore(&mdma->error_status_lock, flags);
385
386 /* Print nice error report */
387 if (es) {
388 dev_err(mdma->dma.dev,
389 "Hardware reported following error(s) on channel %u:\n",
390 MPC_DMA_DMAES_ERRCHN(es));
391
392 if (es & MPC_DMA_DMAES_GPE)
393 dev_err(mdma->dma.dev, "- Group Priority Error\n");
394 if (es & MPC_DMA_DMAES_CPE)
395 dev_err(mdma->dma.dev, "- Channel Priority Error\n");
396 if (es & MPC_DMA_DMAES_SAE)
397 dev_err(mdma->dma.dev, "- Source Address Error\n");
398 if (es & MPC_DMA_DMAES_SOE)
399 dev_err(mdma->dma.dev, "- Source Offset"
400 " Configuration Error\n");
401 if (es & MPC_DMA_DMAES_DAE)
402 dev_err(mdma->dma.dev, "- Destination Address"
403 " Error\n");
404 if (es & MPC_DMA_DMAES_DOE)
405 dev_err(mdma->dma.dev, "- Destination Offset"
406 " Configuration Error\n");
407 if (es & MPC_DMA_DMAES_NCE)
408 dev_err(mdma->dma.dev, "- NBytes/Citter"
409 " Configuration Error\n");
410 if (es & MPC_DMA_DMAES_SGE)
411 dev_err(mdma->dma.dev, "- Scatter/Gather"
412 " Configuration Error\n");
413 if (es & MPC_DMA_DMAES_SBE)
414 dev_err(mdma->dma.dev, "- Source Bus Error\n");
415 if (es & MPC_DMA_DMAES_DBE)
416 dev_err(mdma->dma.dev, "- Destination Bus Error\n");
417 }
418
Ilya Yanoka2769912010-10-27 01:52:58 +0200419 mpc_dma_process_completed(mdma);
Piotr Ziecik0fb6f732010-02-05 03:42:52 +0000420}
421
422/* Submit descriptor to hardware */
423static dma_cookie_t mpc_dma_tx_submit(struct dma_async_tx_descriptor *txd)
424{
425 struct mpc_dma_chan *mchan = dma_chan_to_mpc_dma_chan(txd->chan);
426 struct mpc_dma_desc *mdesc;
427 unsigned long flags;
428 dma_cookie_t cookie;
429
430 mdesc = container_of(txd, struct mpc_dma_desc, desc);
431
432 spin_lock_irqsave(&mchan->lock, flags);
433
434 /* Move descriptor to queue */
435 list_move_tail(&mdesc->node, &mchan->queued);
436
437 /* If channel is idle, execute all queued descriptors */
438 if (list_empty(&mchan->active))
439 mpc_dma_execute(mchan);
440
441 /* Update cookie */
Russell King - ARM Linux884485e2012-03-06 22:34:46 +0000442 cookie = dma_cookie_assign(txd);
Piotr Ziecik0fb6f732010-02-05 03:42:52 +0000443 spin_unlock_irqrestore(&mchan->lock, flags);
444
445 return cookie;
446}
447
448/* Alloc channel resources */
449static int mpc_dma_alloc_chan_resources(struct dma_chan *chan)
450{
451 struct mpc_dma *mdma = dma_chan_to_mpc_dma(chan);
452 struct mpc_dma_chan *mchan = dma_chan_to_mpc_dma_chan(chan);
453 struct mpc_dma_desc *mdesc;
454 struct mpc_dma_tcd *tcd;
455 dma_addr_t tcd_paddr;
456 unsigned long flags;
457 LIST_HEAD(descs);
458 int i;
459
460 /* Alloc DMA memory for Transfer Control Descriptors */
461 tcd = dma_alloc_coherent(mdma->dma.dev,
462 MPC_DMA_DESCRIPTORS * sizeof(struct mpc_dma_tcd),
463 &tcd_paddr, GFP_KERNEL);
464 if (!tcd)
465 return -ENOMEM;
466
467 /* Alloc descriptors for this channel */
468 for (i = 0; i < MPC_DMA_DESCRIPTORS; i++) {
469 mdesc = kzalloc(sizeof(struct mpc_dma_desc), GFP_KERNEL);
470 if (!mdesc) {
471 dev_notice(mdma->dma.dev, "Memory allocation error. "
472 "Allocated only %u descriptors\n", i);
473 break;
474 }
475
476 dma_async_tx_descriptor_init(&mdesc->desc, chan);
477 mdesc->desc.flags = DMA_CTRL_ACK;
478 mdesc->desc.tx_submit = mpc_dma_tx_submit;
479
480 mdesc->tcd = &tcd[i];
481 mdesc->tcd_paddr = tcd_paddr + (i * sizeof(struct mpc_dma_tcd));
482
483 list_add_tail(&mdesc->node, &descs);
484 }
485
486 /* Return error only if no descriptors were allocated */
487 if (i == 0) {
488 dma_free_coherent(mdma->dma.dev,
489 MPC_DMA_DESCRIPTORS * sizeof(struct mpc_dma_tcd),
490 tcd, tcd_paddr);
491 return -ENOMEM;
492 }
493
494 spin_lock_irqsave(&mchan->lock, flags);
495 mchan->tcd = tcd;
496 mchan->tcd_paddr = tcd_paddr;
497 list_splice_tail_init(&descs, &mchan->free);
498 spin_unlock_irqrestore(&mchan->lock, flags);
499
500 /* Enable Error Interrupt */
501 out_8(&mdma->regs->dmaseei, chan->chan_id);
502
503 return 0;
504}
505
506/* Free channel resources */
507static void mpc_dma_free_chan_resources(struct dma_chan *chan)
508{
509 struct mpc_dma *mdma = dma_chan_to_mpc_dma(chan);
510 struct mpc_dma_chan *mchan = dma_chan_to_mpc_dma_chan(chan);
511 struct mpc_dma_desc *mdesc, *tmp;
512 struct mpc_dma_tcd *tcd;
513 dma_addr_t tcd_paddr;
514 unsigned long flags;
515 LIST_HEAD(descs);
516
517 spin_lock_irqsave(&mchan->lock, flags);
518
519 /* Channel must be idle */
520 BUG_ON(!list_empty(&mchan->prepared));
521 BUG_ON(!list_empty(&mchan->queued));
522 BUG_ON(!list_empty(&mchan->active));
523 BUG_ON(!list_empty(&mchan->completed));
524
525 /* Move data */
526 list_splice_tail_init(&mchan->free, &descs);
527 tcd = mchan->tcd;
528 tcd_paddr = mchan->tcd_paddr;
529
530 spin_unlock_irqrestore(&mchan->lock, flags);
531
532 /* Free DMA memory used by descriptors */
533 dma_free_coherent(mdma->dma.dev,
534 MPC_DMA_DESCRIPTORS * sizeof(struct mpc_dma_tcd),
535 tcd, tcd_paddr);
536
537 /* Free descriptors */
538 list_for_each_entry_safe(mdesc, tmp, &descs, node)
539 kfree(mdesc);
540
541 /* Disable Error Interrupt */
542 out_8(&mdma->regs->dmaceei, chan->chan_id);
543}
544
545/* Send all pending descriptor to hardware */
546static void mpc_dma_issue_pending(struct dma_chan *chan)
547{
548 /*
549 * We are posting descriptors to the hardware as soon as
550 * they are ready, so this function does nothing.
551 */
552}
553
554/* Check request completion status */
555static enum dma_status
Linus Walleij07934482010-03-26 16:50:49 -0700556mpc_dma_tx_status(struct dma_chan *chan, dma_cookie_t cookie,
557 struct dma_tx_state *txstate)
Piotr Ziecik0fb6f732010-02-05 03:42:52 +0000558{
559 struct mpc_dma_chan *mchan = dma_chan_to_mpc_dma_chan(chan);
560 unsigned long flags;
561 dma_cookie_t last_used;
562 dma_cookie_t last_complete;
563
564 spin_lock_irqsave(&mchan->lock, flags);
565 last_used = mchan->chan.cookie;
Russell King - ARM Linux4d4e58d2012-03-06 22:34:06 +0000566 last_complete = mchan->chan.completed_cookie;
Piotr Ziecik0fb6f732010-02-05 03:42:52 +0000567 spin_unlock_irqrestore(&mchan->lock, flags);
568
Dan Williamsbca34692010-03-26 16:52:10 -0700569 dma_set_tx_state(txstate, last_complete, last_used, 0);
Piotr Ziecik0fb6f732010-02-05 03:42:52 +0000570 return dma_async_is_complete(cookie, last_complete, last_used);
571}
572
573/* Prepare descriptor for memory to memory copy */
574static struct dma_async_tx_descriptor *
575mpc_dma_prep_memcpy(struct dma_chan *chan, dma_addr_t dst, dma_addr_t src,
576 size_t len, unsigned long flags)
577{
Ilya Yanokba2eea22010-10-27 01:52:57 +0200578 struct mpc_dma *mdma = dma_chan_to_mpc_dma(chan);
Piotr Ziecik0fb6f732010-02-05 03:42:52 +0000579 struct mpc_dma_chan *mchan = dma_chan_to_mpc_dma_chan(chan);
580 struct mpc_dma_desc *mdesc = NULL;
581 struct mpc_dma_tcd *tcd;
582 unsigned long iflags;
583
584 /* Get free descriptor */
585 spin_lock_irqsave(&mchan->lock, iflags);
586 if (!list_empty(&mchan->free)) {
587 mdesc = list_first_entry(&mchan->free, struct mpc_dma_desc,
588 node);
589 list_del(&mdesc->node);
590 }
591 spin_unlock_irqrestore(&mchan->lock, iflags);
592
Ilya Yanoka2769912010-10-27 01:52:58 +0200593 if (!mdesc) {
594 /* try to free completed descriptors */
595 mpc_dma_process_completed(mdma);
Piotr Ziecik0fb6f732010-02-05 03:42:52 +0000596 return NULL;
Ilya Yanoka2769912010-10-27 01:52:58 +0200597 }
Piotr Ziecik0fb6f732010-02-05 03:42:52 +0000598
599 mdesc->error = 0;
600 tcd = mdesc->tcd;
601
602 /* Prepare Transfer Control Descriptor for this transaction */
603 memset(tcd, 0, sizeof(struct mpc_dma_tcd));
604
605 if (IS_ALIGNED(src | dst | len, 32)) {
606 tcd->ssize = MPC_DMA_TSIZE_32;
607 tcd->dsize = MPC_DMA_TSIZE_32;
608 tcd->soff = 32;
609 tcd->doff = 32;
Ilya Yanokba2eea22010-10-27 01:52:57 +0200610 } else if (!mdma->is_mpc8308 && IS_ALIGNED(src | dst | len, 16)) {
611 /* MPC8308 doesn't support 16 byte transfers */
Piotr Ziecik0fb6f732010-02-05 03:42:52 +0000612 tcd->ssize = MPC_DMA_TSIZE_16;
613 tcd->dsize = MPC_DMA_TSIZE_16;
614 tcd->soff = 16;
615 tcd->doff = 16;
616 } else if (IS_ALIGNED(src | dst | len, 4)) {
617 tcd->ssize = MPC_DMA_TSIZE_4;
618 tcd->dsize = MPC_DMA_TSIZE_4;
619 tcd->soff = 4;
620 tcd->doff = 4;
621 } else if (IS_ALIGNED(src | dst | len, 2)) {
622 tcd->ssize = MPC_DMA_TSIZE_2;
623 tcd->dsize = MPC_DMA_TSIZE_2;
624 tcd->soff = 2;
625 tcd->doff = 2;
626 } else {
627 tcd->ssize = MPC_DMA_TSIZE_1;
628 tcd->dsize = MPC_DMA_TSIZE_1;
629 tcd->soff = 1;
630 tcd->doff = 1;
631 }
632
633 tcd->saddr = src;
634 tcd->daddr = dst;
635 tcd->nbytes = len;
636 tcd->biter = 1;
637 tcd->citer = 1;
638
639 /* Place descriptor in prepared list */
640 spin_lock_irqsave(&mchan->lock, iflags);
641 list_add_tail(&mdesc->node, &mchan->prepared);
642 spin_unlock_irqrestore(&mchan->lock, iflags);
643
644 return &mdesc->desc;
645}
646
Grant Likely00006122011-02-22 19:59:54 -0700647static int __devinit mpc_dma_probe(struct platform_device *op)
Piotr Ziecik0fb6f732010-02-05 03:42:52 +0000648{
Anatolij Gustschinb4a75c92010-05-31 18:39:13 +0200649 struct device_node *dn = op->dev.of_node;
Piotr Ziecik0fb6f732010-02-05 03:42:52 +0000650 struct device *dev = &op->dev;
651 struct dma_device *dma;
652 struct mpc_dma *mdma;
653 struct mpc_dma_chan *mchan;
654 struct resource res;
655 ulong regs_start, regs_size;
656 int retval, i;
657
658 mdma = devm_kzalloc(dev, sizeof(struct mpc_dma), GFP_KERNEL);
659 if (!mdma) {
660 dev_err(dev, "Memory exhausted!\n");
661 return -ENOMEM;
662 }
663
664 mdma->irq = irq_of_parse_and_map(dn, 0);
665 if (mdma->irq == NO_IRQ) {
666 dev_err(dev, "Error mapping IRQ!\n");
667 return -EINVAL;
668 }
669
Ilya Yanokba2eea22010-10-27 01:52:57 +0200670 if (of_device_is_compatible(dn, "fsl,mpc8308-dma")) {
671 mdma->is_mpc8308 = 1;
672 mdma->irq2 = irq_of_parse_and_map(dn, 1);
673 if (mdma->irq2 == NO_IRQ) {
674 dev_err(dev, "Error mapping IRQ!\n");
675 return -EINVAL;
676 }
677 }
678
Piotr Ziecik0fb6f732010-02-05 03:42:52 +0000679 retval = of_address_to_resource(dn, 0, &res);
680 if (retval) {
681 dev_err(dev, "Error parsing memory region!\n");
682 return retval;
683 }
684
685 regs_start = res.start;
Tobias Klauser8381fc32010-05-06 11:58:55 +0200686 regs_size = resource_size(&res);
Piotr Ziecik0fb6f732010-02-05 03:42:52 +0000687
688 if (!devm_request_mem_region(dev, regs_start, regs_size, DRV_NAME)) {
689 dev_err(dev, "Error requesting memory region!\n");
690 return -EBUSY;
691 }
692
693 mdma->regs = devm_ioremap(dev, regs_start, regs_size);
694 if (!mdma->regs) {
695 dev_err(dev, "Error mapping memory region!\n");
696 return -ENOMEM;
697 }
698
699 mdma->tcd = (struct mpc_dma_tcd *)((u8 *)(mdma->regs)
700 + MPC_DMA_TCD_OFFSET);
701
702 retval = devm_request_irq(dev, mdma->irq, &mpc_dma_irq, 0, DRV_NAME,
703 mdma);
704 if (retval) {
705 dev_err(dev, "Error requesting IRQ!\n");
706 return -EINVAL;
707 }
708
Ilya Yanokba2eea22010-10-27 01:52:57 +0200709 if (mdma->is_mpc8308) {
710 retval = devm_request_irq(dev, mdma->irq2, &mpc_dma_irq, 0,
711 DRV_NAME, mdma);
712 if (retval) {
713 dev_err(dev, "Error requesting IRQ2!\n");
714 return -EINVAL;
715 }
716 }
717
Piotr Ziecik0fb6f732010-02-05 03:42:52 +0000718 spin_lock_init(&mdma->error_status_lock);
719
720 dma = &mdma->dma;
721 dma->dev = dev;
Ilya Yanokba2eea22010-10-27 01:52:57 +0200722 if (!mdma->is_mpc8308)
723 dma->chancnt = MPC_DMA_CHANNELS;
724 else
725 dma->chancnt = 16; /* MPC8308 DMA has only 16 channels */
Piotr Ziecik0fb6f732010-02-05 03:42:52 +0000726 dma->device_alloc_chan_resources = mpc_dma_alloc_chan_resources;
727 dma->device_free_chan_resources = mpc_dma_free_chan_resources;
728 dma->device_issue_pending = mpc_dma_issue_pending;
Linus Walleij07934482010-03-26 16:50:49 -0700729 dma->device_tx_status = mpc_dma_tx_status;
Piotr Ziecik0fb6f732010-02-05 03:42:52 +0000730 dma->device_prep_dma_memcpy = mpc_dma_prep_memcpy;
731
732 INIT_LIST_HEAD(&dma->channels);
733 dma_cap_set(DMA_MEMCPY, dma->cap_mask);
734
735 for (i = 0; i < dma->chancnt; i++) {
736 mchan = &mdma->channels[i];
737
738 mchan->chan.device = dma;
Piotr Ziecik0fb6f732010-02-05 03:42:52 +0000739 mchan->chan.cookie = 1;
Russell King - ARM Linux4d4e58d2012-03-06 22:34:06 +0000740 mchan->chan.completed_cookie = mchan->chan.cookie;
Piotr Ziecik0fb6f732010-02-05 03:42:52 +0000741
742 INIT_LIST_HEAD(&mchan->free);
743 INIT_LIST_HEAD(&mchan->prepared);
744 INIT_LIST_HEAD(&mchan->queued);
745 INIT_LIST_HEAD(&mchan->active);
746 INIT_LIST_HEAD(&mchan->completed);
747
748 spin_lock_init(&mchan->lock);
749 list_add_tail(&mchan->chan.device_node, &dma->channels);
750 }
751
752 tasklet_init(&mdma->tasklet, mpc_dma_tasklet, (unsigned long)mdma);
753
754 /*
755 * Configure DMA Engine:
756 * - Dynamic clock,
757 * - Round-robin group arbitration,
758 * - Round-robin channel arbitration.
759 */
Ilya Yanokba2eea22010-10-27 01:52:57 +0200760 if (!mdma->is_mpc8308) {
761 out_be32(&mdma->regs->dmacr, MPC_DMA_DMACR_EDCG |
762 MPC_DMA_DMACR_ERGA | MPC_DMA_DMACR_ERCA);
Piotr Ziecik0fb6f732010-02-05 03:42:52 +0000763
Ilya Yanokba2eea22010-10-27 01:52:57 +0200764 /* Disable hardware DMA requests */
765 out_be32(&mdma->regs->dmaerqh, 0);
766 out_be32(&mdma->regs->dmaerql, 0);
Piotr Ziecik0fb6f732010-02-05 03:42:52 +0000767
Ilya Yanokba2eea22010-10-27 01:52:57 +0200768 /* Disable error interrupts */
769 out_be32(&mdma->regs->dmaeeih, 0);
770 out_be32(&mdma->regs->dmaeeil, 0);
Piotr Ziecik0fb6f732010-02-05 03:42:52 +0000771
Ilya Yanokba2eea22010-10-27 01:52:57 +0200772 /* Clear interrupts status */
773 out_be32(&mdma->regs->dmainth, 0xFFFFFFFF);
774 out_be32(&mdma->regs->dmaintl, 0xFFFFFFFF);
775 out_be32(&mdma->regs->dmaerrh, 0xFFFFFFFF);
776 out_be32(&mdma->regs->dmaerrl, 0xFFFFFFFF);
Piotr Ziecik0fb6f732010-02-05 03:42:52 +0000777
Ilya Yanokba2eea22010-10-27 01:52:57 +0200778 /* Route interrupts to IPIC */
779 out_be32(&mdma->regs->dmaihsa, 0);
780 out_be32(&mdma->regs->dmailsa, 0);
781 } else {
782 /* MPC8308 has 16 channels and lacks some registers */
783 out_be32(&mdma->regs->dmacr, MPC_DMA_DMACR_ERCA);
784
785 /* enable snooping */
786 out_be32(&mdma->regs->dmagpor, MPC_DMA_DMAGPOR_SNOOP_ENABLE);
787 /* Disable error interrupts */
788 out_be32(&mdma->regs->dmaeeil, 0);
789
790 /* Clear interrupts status */
791 out_be32(&mdma->regs->dmaintl, 0xFFFF);
792 out_be32(&mdma->regs->dmaerrl, 0xFFFF);
793 }
Piotr Ziecik0fb6f732010-02-05 03:42:52 +0000794
795 /* Register DMA engine */
796 dev_set_drvdata(dev, mdma);
797 retval = dma_async_device_register(dma);
798 if (retval) {
799 devm_free_irq(dev, mdma->irq, mdma);
800 irq_dispose_mapping(mdma->irq);
801 }
802
803 return retval;
804}
805
Grant Likely2dc11582010-08-06 09:25:50 -0600806static int __devexit mpc_dma_remove(struct platform_device *op)
Piotr Ziecik0fb6f732010-02-05 03:42:52 +0000807{
808 struct device *dev = &op->dev;
809 struct mpc_dma *mdma = dev_get_drvdata(dev);
810
811 dma_async_device_unregister(&mdma->dma);
812 devm_free_irq(dev, mdma->irq, mdma);
813 irq_dispose_mapping(mdma->irq);
814
815 return 0;
816}
817
818static struct of_device_id mpc_dma_match[] = {
819 { .compatible = "fsl,mpc5121-dma", },
820 {},
821};
822
Grant Likely00006122011-02-22 19:59:54 -0700823static struct platform_driver mpc_dma_driver = {
Piotr Ziecik0fb6f732010-02-05 03:42:52 +0000824 .probe = mpc_dma_probe,
825 .remove = __devexit_p(mpc_dma_remove),
Anatolij Gustschinb4a75c92010-05-31 18:39:13 +0200826 .driver = {
827 .name = DRV_NAME,
828 .owner = THIS_MODULE,
829 .of_match_table = mpc_dma_match,
Piotr Ziecik0fb6f732010-02-05 03:42:52 +0000830 },
831};
832
Axel Linc94e9102011-11-26 15:11:12 +0800833module_platform_driver(mpc_dma_driver);
Piotr Ziecik0fb6f732010-02-05 03:42:52 +0000834
835MODULE_LICENSE("GPL");
836MODULE_AUTHOR("Piotr Ziecik <kosmo@semihalf.com>");