]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/dma/fsldma.c
DMAENGINE: extend the control command to include an arg
[net-next-2.6.git] / drivers / dma / fsldma.c
CommitLineData
173acc7c
ZW
1/*
2 * Freescale MPC85xx, MPC83xx DMA Engine support
3 *
4 * Copyright (C) 2007 Freescale Semiconductor, Inc. All rights reserved.
5 *
6 * Author:
7 * Zhang Wei <wei.zhang@freescale.com>, Jul 2007
8 * Ebony Zhu <ebony.zhu@freescale.com>, May 2007
9 *
10 * Description:
11 * DMA engine driver for Freescale MPC8540 DMA controller, which is
12 * also fit for MPC8560, MPC8555, MPC8548, MPC8641, and etc.
13 * The support for MPC8349 DMA contorller is also added.
14 *
a7aea373
IS
15 * This driver instructs the DMA controller to issue the PCI Read Multiple
16 * command for PCI read operations, instead of using the default PCI Read Line
17 * command. Please be aware that this setting may result in read pre-fetching
18 * on some platforms.
19 *
173acc7c
ZW
20 * This is free software; you can redistribute it and/or modify
21 * it under the terms of the GNU General Public License as published by
22 * the Free Software Foundation; either version 2 of the License, or
23 * (at your option) any later version.
24 *
25 */
26
27#include <linux/init.h>
28#include <linux/module.h>
29#include <linux/pci.h>
30#include <linux/interrupt.h>
31#include <linux/dmaengine.h>
32#include <linux/delay.h>
33#include <linux/dma-mapping.h>
34#include <linux/dmapool.h>
35#include <linux/of_platform.h>
36
bbea0b6e 37#include <asm/fsldma.h>
173acc7c
ZW
38#include "fsldma.h"
39
a1c03319 40static void dma_init(struct fsldma_chan *chan)
173acc7c
ZW
41{
42 /* Reset the channel */
a1c03319 43 DMA_OUT(chan, &chan->regs->mr, 0, 32);
173acc7c 44
a1c03319 45 switch (chan->feature & FSL_DMA_IP_MASK) {
173acc7c
ZW
46 case FSL_DMA_IP_85XX:
47 /* Set the channel to below modes:
48 * EIE - Error interrupt enable
49 * EOSIE - End of segments interrupt enable (basic mode)
50 * EOLNIE - End of links interrupt enable
51 */
a1c03319 52 DMA_OUT(chan, &chan->regs->mr, FSL_DMA_MR_EIE
173acc7c
ZW
53 | FSL_DMA_MR_EOLNIE | FSL_DMA_MR_EOSIE, 32);
54 break;
55 case FSL_DMA_IP_83XX:
56 /* Set the channel to below modes:
57 * EOTIE - End-of-transfer interrupt enable
a7aea373 58 * PRC_RM - PCI read multiple
173acc7c 59 */
a1c03319 60 DMA_OUT(chan, &chan->regs->mr, FSL_DMA_MR_EOTIE
a7aea373 61 | FSL_DMA_MR_PRC_RM, 32);
173acc7c
ZW
62 break;
63 }
173acc7c
ZW
64}
65
a1c03319 66static void set_sr(struct fsldma_chan *chan, u32 val)
173acc7c 67{
a1c03319 68 DMA_OUT(chan, &chan->regs->sr, val, 32);
173acc7c
ZW
69}
70
a1c03319 71static u32 get_sr(struct fsldma_chan *chan)
173acc7c 72{
a1c03319 73 return DMA_IN(chan, &chan->regs->sr, 32);
173acc7c
ZW
74}
75
a1c03319 76static void set_desc_cnt(struct fsldma_chan *chan,
173acc7c
ZW
77 struct fsl_dma_ld_hw *hw, u32 count)
78{
a1c03319 79 hw->count = CPU_TO_DMA(chan, count, 32);
173acc7c
ZW
80}
81
a1c03319 82static void set_desc_src(struct fsldma_chan *chan,
173acc7c
ZW
83 struct fsl_dma_ld_hw *hw, dma_addr_t src)
84{
85 u64 snoop_bits;
86
a1c03319 87 snoop_bits = ((chan->feature & FSL_DMA_IP_MASK) == FSL_DMA_IP_85XX)
173acc7c 88 ? ((u64)FSL_DMA_SATR_SREADTYPE_SNOOP_READ << 32) : 0;
a1c03319 89 hw->src_addr = CPU_TO_DMA(chan, snoop_bits | src, 64);
173acc7c
ZW
90}
91
a1c03319 92static void set_desc_dst(struct fsldma_chan *chan,
738f5f7e 93 struct fsl_dma_ld_hw *hw, dma_addr_t dst)
173acc7c
ZW
94{
95 u64 snoop_bits;
96
a1c03319 97 snoop_bits = ((chan->feature & FSL_DMA_IP_MASK) == FSL_DMA_IP_85XX)
173acc7c 98 ? ((u64)FSL_DMA_DATR_DWRITETYPE_SNOOP_WRITE << 32) : 0;
a1c03319 99 hw->dst_addr = CPU_TO_DMA(chan, snoop_bits | dst, 64);
173acc7c
ZW
100}
101
a1c03319 102static void set_desc_next(struct fsldma_chan *chan,
173acc7c
ZW
103 struct fsl_dma_ld_hw *hw, dma_addr_t next)
104{
105 u64 snoop_bits;
106
a1c03319 107 snoop_bits = ((chan->feature & FSL_DMA_IP_MASK) == FSL_DMA_IP_83XX)
173acc7c 108 ? FSL_DMA_SNEN : 0;
a1c03319 109 hw->next_ln_addr = CPU_TO_DMA(chan, snoop_bits | next, 64);
173acc7c
ZW
110}
111
a1c03319 112static void set_cdar(struct fsldma_chan *chan, dma_addr_t addr)
173acc7c 113{
a1c03319 114 DMA_OUT(chan, &chan->regs->cdar, addr | FSL_DMA_SNEN, 64);
173acc7c
ZW
115}
116
a1c03319 117static dma_addr_t get_cdar(struct fsldma_chan *chan)
173acc7c 118{
a1c03319 119 return DMA_IN(chan, &chan->regs->cdar, 64) & ~FSL_DMA_SNEN;
173acc7c
ZW
120}
121
a1c03319 122static dma_addr_t get_ndar(struct fsldma_chan *chan)
173acc7c 123{
a1c03319 124 return DMA_IN(chan, &chan->regs->ndar, 64);
173acc7c
ZW
125}
126
a1c03319 127static u32 get_bcr(struct fsldma_chan *chan)
f79abb62 128{
a1c03319 129 return DMA_IN(chan, &chan->regs->bcr, 32);
f79abb62
ZW
130}
131
a1c03319 132static int dma_is_idle(struct fsldma_chan *chan)
173acc7c 133{
a1c03319 134 u32 sr = get_sr(chan);
173acc7c
ZW
135 return (!(sr & FSL_DMA_SR_CB)) || (sr & FSL_DMA_SR_CH);
136}
137
a1c03319 138static void dma_start(struct fsldma_chan *chan)
173acc7c 139{
272ca655
IS
140 u32 mode;
141
a1c03319 142 mode = DMA_IN(chan, &chan->regs->mr, 32);
272ca655 143
a1c03319
IS
144 if ((chan->feature & FSL_DMA_IP_MASK) == FSL_DMA_IP_85XX) {
145 if (chan->feature & FSL_DMA_CHAN_PAUSE_EXT) {
146 DMA_OUT(chan, &chan->regs->bcr, 0, 32);
272ca655
IS
147 mode |= FSL_DMA_MR_EMP_EN;
148 } else {
149 mode &= ~FSL_DMA_MR_EMP_EN;
150 }
43a1a3ed 151 }
173acc7c 152
a1c03319 153 if (chan->feature & FSL_DMA_CHAN_START_EXT)
272ca655 154 mode |= FSL_DMA_MR_EMS_EN;
173acc7c 155 else
272ca655 156 mode |= FSL_DMA_MR_CS;
173acc7c 157
a1c03319 158 DMA_OUT(chan, &chan->regs->mr, mode, 32);
173acc7c
ZW
159}
160
a1c03319 161static void dma_halt(struct fsldma_chan *chan)
173acc7c 162{
272ca655 163 u32 mode;
900325a6
DW
164 int i;
165
a1c03319 166 mode = DMA_IN(chan, &chan->regs->mr, 32);
272ca655 167 mode |= FSL_DMA_MR_CA;
a1c03319 168 DMA_OUT(chan, &chan->regs->mr, mode, 32);
272ca655
IS
169
170 mode &= ~(FSL_DMA_MR_CS | FSL_DMA_MR_EMS_EN | FSL_DMA_MR_CA);
a1c03319 171 DMA_OUT(chan, &chan->regs->mr, mode, 32);
173acc7c 172
900325a6 173 for (i = 0; i < 100; i++) {
a1c03319 174 if (dma_is_idle(chan))
9c3a50b7
IS
175 return;
176
173acc7c 177 udelay(10);
900325a6 178 }
272ca655 179
9c3a50b7 180 if (!dma_is_idle(chan))
a1c03319 181 dev_err(chan->dev, "DMA halt timeout!\n");
173acc7c
ZW
182}
183
a1c03319 184static void set_ld_eol(struct fsldma_chan *chan,
173acc7c
ZW
185 struct fsl_desc_sw *desc)
186{
776c8943
IS
187 u64 snoop_bits;
188
a1c03319 189 snoop_bits = ((chan->feature & FSL_DMA_IP_MASK) == FSL_DMA_IP_83XX)
776c8943
IS
190 ? FSL_DMA_SNEN : 0;
191
a1c03319
IS
192 desc->hw.next_ln_addr = CPU_TO_DMA(chan,
193 DMA_TO_CPU(chan, desc->hw.next_ln_addr, 64) | FSL_DMA_EOL
776c8943 194 | snoop_bits, 64);
173acc7c
ZW
195}
196
173acc7c
ZW
197/**
198 * fsl_chan_set_src_loop_size - Set source address hold transfer size
a1c03319 199 * @chan : Freescale DMA channel
173acc7c
ZW
200 * @size : Address loop size, 0 for disable loop
201 *
202 * The set source address hold transfer size. The source
203 * address hold or loop transfer size is when the DMA transfer
204 * data from source address (SA), if the loop size is 4, the DMA will
205 * read data from SA, SA + 1, SA + 2, SA + 3, then loop back to SA,
206 * SA + 1 ... and so on.
207 */
a1c03319 208static void fsl_chan_set_src_loop_size(struct fsldma_chan *chan, int size)
173acc7c 209{
272ca655
IS
210 u32 mode;
211
a1c03319 212 mode = DMA_IN(chan, &chan->regs->mr, 32);
272ca655 213
173acc7c
ZW
214 switch (size) {
215 case 0:
272ca655 216 mode &= ~FSL_DMA_MR_SAHE;
173acc7c
ZW
217 break;
218 case 1:
219 case 2:
220 case 4:
221 case 8:
272ca655 222 mode |= FSL_DMA_MR_SAHE | (__ilog2(size) << 14);
173acc7c
ZW
223 break;
224 }
272ca655 225
a1c03319 226 DMA_OUT(chan, &chan->regs->mr, mode, 32);
173acc7c
ZW
227}
228
229/**
738f5f7e 230 * fsl_chan_set_dst_loop_size - Set destination address hold transfer size
a1c03319 231 * @chan : Freescale DMA channel
173acc7c
ZW
232 * @size : Address loop size, 0 for disable loop
233 *
234 * The set destination address hold transfer size. The destination
235 * address hold or loop transfer size is when the DMA transfer
236 * data to destination address (TA), if the loop size is 4, the DMA will
237 * write data to TA, TA + 1, TA + 2, TA + 3, then loop back to TA,
238 * TA + 1 ... and so on.
239 */
a1c03319 240static void fsl_chan_set_dst_loop_size(struct fsldma_chan *chan, int size)
173acc7c 241{
272ca655
IS
242 u32 mode;
243
a1c03319 244 mode = DMA_IN(chan, &chan->regs->mr, 32);
272ca655 245
173acc7c
ZW
246 switch (size) {
247 case 0:
272ca655 248 mode &= ~FSL_DMA_MR_DAHE;
173acc7c
ZW
249 break;
250 case 1:
251 case 2:
252 case 4:
253 case 8:
272ca655 254 mode |= FSL_DMA_MR_DAHE | (__ilog2(size) << 16);
173acc7c
ZW
255 break;
256 }
272ca655 257
a1c03319 258 DMA_OUT(chan, &chan->regs->mr, mode, 32);
173acc7c
ZW
259}
260
261/**
e6c7ecb6 262 * fsl_chan_set_request_count - Set DMA Request Count for external control
a1c03319 263 * @chan : Freescale DMA channel
e6c7ecb6
IS
264 * @size : Number of bytes to transfer in a single request
265 *
266 * The Freescale DMA channel can be controlled by the external signal DREQ#.
267 * The DMA request count is how many bytes are allowed to transfer before
268 * pausing the channel, after which a new assertion of DREQ# resumes channel
269 * operation.
173acc7c 270 *
e6c7ecb6 271 * A size of 0 disables external pause control. The maximum size is 1024.
173acc7c 272 */
a1c03319 273static void fsl_chan_set_request_count(struct fsldma_chan *chan, int size)
173acc7c 274{
272ca655
IS
275 u32 mode;
276
e6c7ecb6 277 BUG_ON(size > 1024);
272ca655 278
a1c03319 279 mode = DMA_IN(chan, &chan->regs->mr, 32);
272ca655
IS
280 mode |= (__ilog2(size) << 24) & 0x0f000000;
281
a1c03319 282 DMA_OUT(chan, &chan->regs->mr, mode, 32);
e6c7ecb6 283}
173acc7c 284
e6c7ecb6
IS
285/**
286 * fsl_chan_toggle_ext_pause - Toggle channel external pause status
a1c03319 287 * @chan : Freescale DMA channel
e6c7ecb6
IS
288 * @enable : 0 is disabled, 1 is enabled.
289 *
290 * The Freescale DMA channel can be controlled by the external signal DREQ#.
291 * The DMA Request Count feature should be used in addition to this feature
292 * to set the number of bytes to transfer before pausing the channel.
293 */
a1c03319 294static void fsl_chan_toggle_ext_pause(struct fsldma_chan *chan, int enable)
e6c7ecb6
IS
295{
296 if (enable)
a1c03319 297 chan->feature |= FSL_DMA_CHAN_PAUSE_EXT;
e6c7ecb6 298 else
a1c03319 299 chan->feature &= ~FSL_DMA_CHAN_PAUSE_EXT;
173acc7c
ZW
300}
301
302/**
303 * fsl_chan_toggle_ext_start - Toggle channel external start status
a1c03319 304 * @chan : Freescale DMA channel
173acc7c
ZW
305 * @enable : 0 is disabled, 1 is enabled.
306 *
307 * If enable the external start, the channel can be started by an
308 * external DMA start pin. So the dma_start() does not start the
309 * transfer immediately. The DMA channel will wait for the
310 * control pin asserted.
311 */
a1c03319 312static void fsl_chan_toggle_ext_start(struct fsldma_chan *chan, int enable)
173acc7c
ZW
313{
314 if (enable)
a1c03319 315 chan->feature |= FSL_DMA_CHAN_START_EXT;
173acc7c 316 else
a1c03319 317 chan->feature &= ~FSL_DMA_CHAN_START_EXT;
173acc7c
ZW
318}
319
9c3a50b7
IS
320static void append_ld_queue(struct fsldma_chan *chan,
321 struct fsl_desc_sw *desc)
322{
323 struct fsl_desc_sw *tail = to_fsl_desc(chan->ld_pending.prev);
324
325 if (list_empty(&chan->ld_pending))
326 goto out_splice;
327
328 /*
329 * Add the hardware descriptor to the chain of hardware descriptors
330 * that already exists in memory.
331 *
332 * This will un-set the EOL bit of the existing transaction, and the
333 * last link in this transaction will become the EOL descriptor.
334 */
335 set_desc_next(chan, &tail->hw, desc->async_tx.phys);
336
337 /*
338 * Add the software descriptor and all children to the list
339 * of pending transactions
340 */
341out_splice:
342 list_splice_tail_init(&desc->tx_list, &chan->ld_pending);
343}
344
173acc7c
ZW
345static dma_cookie_t fsl_dma_tx_submit(struct dma_async_tx_descriptor *tx)
346{
a1c03319 347 struct fsldma_chan *chan = to_fsl_chan(tx->chan);
eda34234
DW
348 struct fsl_desc_sw *desc = tx_to_fsl_desc(tx);
349 struct fsl_desc_sw *child;
173acc7c
ZW
350 unsigned long flags;
351 dma_cookie_t cookie;
352
a1c03319 353 spin_lock_irqsave(&chan->desc_lock, flags);
173acc7c 354
9c3a50b7
IS
355 /*
356 * assign cookies to all of the software descriptors
357 * that make up this transaction
358 */
a1c03319 359 cookie = chan->common.cookie;
eda34234 360 list_for_each_entry(child, &desc->tx_list, node) {
bcfb7465
IS
361 cookie++;
362 if (cookie < 0)
363 cookie = 1;
364
6ca3a7a9 365 child->async_tx.cookie = cookie;
bcfb7465
IS
366 }
367
a1c03319 368 chan->common.cookie = cookie;
9c3a50b7
IS
369
370 /* put this transaction onto the tail of the pending queue */
a1c03319 371 append_ld_queue(chan, desc);
173acc7c 372
a1c03319 373 spin_unlock_irqrestore(&chan->desc_lock, flags);
173acc7c
ZW
374
375 return cookie;
376}
377
378/**
379 * fsl_dma_alloc_descriptor - Allocate descriptor from channel's DMA pool.
a1c03319 380 * @chan : Freescale DMA channel
173acc7c
ZW
381 *
382 * Return - The descriptor allocated. NULL for failed.
383 */
384static struct fsl_desc_sw *fsl_dma_alloc_descriptor(
a1c03319 385 struct fsldma_chan *chan)
173acc7c 386{
9c3a50b7 387 struct fsl_desc_sw *desc;
173acc7c 388 dma_addr_t pdesc;
9c3a50b7
IS
389
390 desc = dma_pool_alloc(chan->desc_pool, GFP_ATOMIC, &pdesc);
391 if (!desc) {
392 dev_dbg(chan->dev, "out of memory for link desc\n");
393 return NULL;
173acc7c
ZW
394 }
395
9c3a50b7
IS
396 memset(desc, 0, sizeof(*desc));
397 INIT_LIST_HEAD(&desc->tx_list);
398 dma_async_tx_descriptor_init(&desc->async_tx, &chan->common);
399 desc->async_tx.tx_submit = fsl_dma_tx_submit;
400 desc->async_tx.phys = pdesc;
401
402 return desc;
173acc7c
ZW
403}
404
405
406/**
407 * fsl_dma_alloc_chan_resources - Allocate resources for DMA channel.
a1c03319 408 * @chan : Freescale DMA channel
173acc7c
ZW
409 *
410 * This function will create a dma pool for descriptor allocation.
411 *
412 * Return - The number of descriptors allocated.
413 */
a1c03319 414static int fsl_dma_alloc_chan_resources(struct dma_chan *dchan)
173acc7c 415{
a1c03319 416 struct fsldma_chan *chan = to_fsl_chan(dchan);
77cd62e8
TT
417
418 /* Has this channel already been allocated? */
a1c03319 419 if (chan->desc_pool)
77cd62e8 420 return 1;
173acc7c 421
9c3a50b7
IS
422 /*
423 * We need the descriptor to be aligned to 32bytes
173acc7c
ZW
424 * for meeting FSL DMA specification requirement.
425 */
a1c03319 426 chan->desc_pool = dma_pool_create("fsl_dma_engine_desc_pool",
9c3a50b7
IS
427 chan->dev,
428 sizeof(struct fsl_desc_sw),
429 __alignof__(struct fsl_desc_sw), 0);
a1c03319 430 if (!chan->desc_pool) {
9c3a50b7
IS
431 dev_err(chan->dev, "unable to allocate channel %d "
432 "descriptor pool\n", chan->id);
433 return -ENOMEM;
173acc7c
ZW
434 }
435
9c3a50b7 436 /* there is at least one descriptor free to be allocated */
173acc7c
ZW
437 return 1;
438}
439
9c3a50b7
IS
440/**
441 * fsldma_free_desc_list - Free all descriptors in a queue
442 * @chan: Freescae DMA channel
443 * @list: the list to free
444 *
445 * LOCKING: must hold chan->desc_lock
446 */
447static void fsldma_free_desc_list(struct fsldma_chan *chan,
448 struct list_head *list)
449{
450 struct fsl_desc_sw *desc, *_desc;
451
452 list_for_each_entry_safe(desc, _desc, list, node) {
453 list_del(&desc->node);
454 dma_pool_free(chan->desc_pool, desc, desc->async_tx.phys);
455 }
456}
457
458static void fsldma_free_desc_list_reverse(struct fsldma_chan *chan,
459 struct list_head *list)
460{
461 struct fsl_desc_sw *desc, *_desc;
462
463 list_for_each_entry_safe_reverse(desc, _desc, list, node) {
464 list_del(&desc->node);
465 dma_pool_free(chan->desc_pool, desc, desc->async_tx.phys);
466 }
467}
468
173acc7c
ZW
469/**
470 * fsl_dma_free_chan_resources - Free all resources of the channel.
a1c03319 471 * @chan : Freescale DMA channel
173acc7c 472 */
a1c03319 473static void fsl_dma_free_chan_resources(struct dma_chan *dchan)
173acc7c 474{
a1c03319 475 struct fsldma_chan *chan = to_fsl_chan(dchan);
173acc7c
ZW
476 unsigned long flags;
477
a1c03319
IS
478 dev_dbg(chan->dev, "Free all channel resources.\n");
479 spin_lock_irqsave(&chan->desc_lock, flags);
9c3a50b7
IS
480 fsldma_free_desc_list(chan, &chan->ld_pending);
481 fsldma_free_desc_list(chan, &chan->ld_running);
a1c03319 482 spin_unlock_irqrestore(&chan->desc_lock, flags);
77cd62e8 483
9c3a50b7 484 dma_pool_destroy(chan->desc_pool);
a1c03319 485 chan->desc_pool = NULL;
173acc7c
ZW
486}
487
2187c269 488static struct dma_async_tx_descriptor *
a1c03319 489fsl_dma_prep_interrupt(struct dma_chan *dchan, unsigned long flags)
2187c269 490{
a1c03319 491 struct fsldma_chan *chan;
2187c269
ZW
492 struct fsl_desc_sw *new;
493
a1c03319 494 if (!dchan)
2187c269
ZW
495 return NULL;
496
a1c03319 497 chan = to_fsl_chan(dchan);
2187c269 498
a1c03319 499 new = fsl_dma_alloc_descriptor(chan);
2187c269 500 if (!new) {
a1c03319 501 dev_err(chan->dev, "No free memory for link descriptor\n");
2187c269
ZW
502 return NULL;
503 }
504
505 new->async_tx.cookie = -EBUSY;
636bdeaa 506 new->async_tx.flags = flags;
2187c269 507
f79abb62 508 /* Insert the link descriptor to the LD ring */
eda34234 509 list_add_tail(&new->node, &new->tx_list);
f79abb62 510
2187c269 511 /* Set End-of-link to the last link descriptor of new list*/
a1c03319 512 set_ld_eol(chan, new);
2187c269
ZW
513
514 return &new->async_tx;
515}
516
173acc7c 517static struct dma_async_tx_descriptor *fsl_dma_prep_memcpy(
a1c03319 518 struct dma_chan *dchan, dma_addr_t dma_dst, dma_addr_t dma_src,
173acc7c
ZW
519 size_t len, unsigned long flags)
520{
a1c03319 521 struct fsldma_chan *chan;
173acc7c
ZW
522 struct fsl_desc_sw *first = NULL, *prev = NULL, *new;
523 size_t copy;
173acc7c 524
a1c03319 525 if (!dchan)
173acc7c
ZW
526 return NULL;
527
528 if (!len)
529 return NULL;
530
a1c03319 531 chan = to_fsl_chan(dchan);
173acc7c
ZW
532
533 do {
534
535 /* Allocate the link descriptor from DMA pool */
a1c03319 536 new = fsl_dma_alloc_descriptor(chan);
173acc7c 537 if (!new) {
a1c03319 538 dev_err(chan->dev,
173acc7c 539 "No free memory for link descriptor\n");
2e077f8e 540 goto fail;
173acc7c
ZW
541 }
542#ifdef FSL_DMA_LD_DEBUG
a1c03319 543 dev_dbg(chan->dev, "new link desc alloc %p\n", new);
173acc7c
ZW
544#endif
545
56822843 546 copy = min(len, (size_t)FSL_DMA_BCR_MAX_CNT);
173acc7c 547
a1c03319
IS
548 set_desc_cnt(chan, &new->hw, copy);
549 set_desc_src(chan, &new->hw, dma_src);
550 set_desc_dst(chan, &new->hw, dma_dst);
173acc7c
ZW
551
552 if (!first)
553 first = new;
554 else
a1c03319 555 set_desc_next(chan, &prev->hw, new->async_tx.phys);
173acc7c
ZW
556
557 new->async_tx.cookie = 0;
636bdeaa 558 async_tx_ack(&new->async_tx);
173acc7c
ZW
559
560 prev = new;
561 len -= copy;
562 dma_src += copy;
738f5f7e 563 dma_dst += copy;
173acc7c
ZW
564
565 /* Insert the link descriptor to the LD ring */
eda34234 566 list_add_tail(&new->node, &first->tx_list);
173acc7c
ZW
567 } while (len);
568
636bdeaa 569 new->async_tx.flags = flags; /* client is in control of this ack */
173acc7c
ZW
570 new->async_tx.cookie = -EBUSY;
571
572 /* Set End-of-link to the last link descriptor of new list*/
a1c03319 573 set_ld_eol(chan, new);
173acc7c 574
2e077f8e
IS
575 return &first->async_tx;
576
577fail:
578 if (!first)
579 return NULL;
580
9c3a50b7 581 fsldma_free_desc_list_reverse(chan, &first->tx_list);
2e077f8e 582 return NULL;
173acc7c
ZW
583}
584
bbea0b6e
IS
585/**
586 * fsl_dma_prep_slave_sg - prepare descriptors for a DMA_SLAVE transaction
587 * @chan: DMA channel
588 * @sgl: scatterlist to transfer to/from
589 * @sg_len: number of entries in @scatterlist
590 * @direction: DMA direction
591 * @flags: DMAEngine flags
592 *
593 * Prepare a set of descriptors for a DMA_SLAVE transaction. Following the
594 * DMA_SLAVE API, this gets the device-specific information from the
595 * chan->private variable.
596 */
597static struct dma_async_tx_descriptor *fsl_dma_prep_slave_sg(
a1c03319 598 struct dma_chan *dchan, struct scatterlist *sgl, unsigned int sg_len,
bbea0b6e
IS
599 enum dma_data_direction direction, unsigned long flags)
600{
a1c03319 601 struct fsldma_chan *chan;
bbea0b6e
IS
602 struct fsl_desc_sw *first = NULL, *prev = NULL, *new = NULL;
603 struct fsl_dma_slave *slave;
bbea0b6e
IS
604 size_t copy;
605
606 int i;
607 struct scatterlist *sg;
608 size_t sg_used;
609 size_t hw_used;
610 struct fsl_dma_hw_addr *hw;
611 dma_addr_t dma_dst, dma_src;
612
a1c03319 613 if (!dchan)
bbea0b6e
IS
614 return NULL;
615
a1c03319 616 if (!dchan->private)
bbea0b6e
IS
617 return NULL;
618
a1c03319
IS
619 chan = to_fsl_chan(dchan);
620 slave = dchan->private;
bbea0b6e
IS
621
622 if (list_empty(&slave->addresses))
623 return NULL;
624
625 hw = list_first_entry(&slave->addresses, struct fsl_dma_hw_addr, entry);
626 hw_used = 0;
627
628 /*
629 * Build the hardware transaction to copy from the scatterlist to
630 * the hardware, or from the hardware to the scatterlist
631 *
632 * If you are copying from the hardware to the scatterlist and it
633 * takes two hardware entries to fill an entire page, then both
634 * hardware entries will be coalesced into the same page
635 *
636 * If you are copying from the scatterlist to the hardware and a
637 * single page can fill two hardware entries, then the data will
638 * be read out of the page into the first hardware entry, and so on
639 */
640 for_each_sg(sgl, sg, sg_len, i) {
641 sg_used = 0;
642
643 /* Loop until the entire scatterlist entry is used */
644 while (sg_used < sg_dma_len(sg)) {
645
646 /*
647 * If we've used up the current hardware address/length
648 * pair, we need to load a new one
649 *
650 * This is done in a while loop so that descriptors with
651 * length == 0 will be skipped
652 */
653 while (hw_used >= hw->length) {
654
655 /*
656 * If the current hardware entry is the last
657 * entry in the list, we're finished
658 */
659 if (list_is_last(&hw->entry, &slave->addresses))
660 goto finished;
661
662 /* Get the next hardware address/length pair */
663 hw = list_entry(hw->entry.next,
664 struct fsl_dma_hw_addr, entry);
665 hw_used = 0;
666 }
667
668 /* Allocate the link descriptor from DMA pool */
a1c03319 669 new = fsl_dma_alloc_descriptor(chan);
bbea0b6e 670 if (!new) {
a1c03319 671 dev_err(chan->dev, "No free memory for "
bbea0b6e
IS
672 "link descriptor\n");
673 goto fail;
674 }
675#ifdef FSL_DMA_LD_DEBUG
a1c03319 676 dev_dbg(chan->dev, "new link desc alloc %p\n", new);
bbea0b6e
IS
677#endif
678
679 /*
680 * Calculate the maximum number of bytes to transfer,
681 * making sure it is less than the DMA controller limit
682 */
683 copy = min_t(size_t, sg_dma_len(sg) - sg_used,
684 hw->length - hw_used);
685 copy = min_t(size_t, copy, FSL_DMA_BCR_MAX_CNT);
686
687 /*
688 * DMA_FROM_DEVICE
689 * from the hardware to the scatterlist
690 *
691 * DMA_TO_DEVICE
692 * from the scatterlist to the hardware
693 */
694 if (direction == DMA_FROM_DEVICE) {
695 dma_src = hw->address + hw_used;
696 dma_dst = sg_dma_address(sg) + sg_used;
697 } else {
698 dma_src = sg_dma_address(sg) + sg_used;
699 dma_dst = hw->address + hw_used;
700 }
701
702 /* Fill in the descriptor */
a1c03319
IS
703 set_desc_cnt(chan, &new->hw, copy);
704 set_desc_src(chan, &new->hw, dma_src);
705 set_desc_dst(chan, &new->hw, dma_dst);
bbea0b6e
IS
706
707 /*
708 * If this is not the first descriptor, chain the
709 * current descriptor after the previous descriptor
710 */
711 if (!first) {
712 first = new;
713 } else {
a1c03319 714 set_desc_next(chan, &prev->hw,
bbea0b6e
IS
715 new->async_tx.phys);
716 }
717
718 new->async_tx.cookie = 0;
719 async_tx_ack(&new->async_tx);
720
721 prev = new;
722 sg_used += copy;
723 hw_used += copy;
724
725 /* Insert the link descriptor into the LD ring */
726 list_add_tail(&new->node, &first->tx_list);
727 }
728 }
729
730finished:
731
732 /* All of the hardware address/length pairs had length == 0 */
733 if (!first || !new)
734 return NULL;
735
736 new->async_tx.flags = flags;
737 new->async_tx.cookie = -EBUSY;
738
739 /* Set End-of-link to the last link descriptor of new list */
a1c03319 740 set_ld_eol(chan, new);
bbea0b6e
IS
741
742 /* Enable extra controller features */
a1c03319
IS
743 if (chan->set_src_loop_size)
744 chan->set_src_loop_size(chan, slave->src_loop_size);
bbea0b6e 745
a1c03319
IS
746 if (chan->set_dst_loop_size)
747 chan->set_dst_loop_size(chan, slave->dst_loop_size);
bbea0b6e 748
a1c03319
IS
749 if (chan->toggle_ext_start)
750 chan->toggle_ext_start(chan, slave->external_start);
bbea0b6e 751
a1c03319
IS
752 if (chan->toggle_ext_pause)
753 chan->toggle_ext_pause(chan, slave->external_pause);
bbea0b6e 754
a1c03319
IS
755 if (chan->set_request_count)
756 chan->set_request_count(chan, slave->request_count);
bbea0b6e
IS
757
758 return &first->async_tx;
759
760fail:
761 /* If first was not set, then we failed to allocate the very first
762 * descriptor, and we're done */
763 if (!first)
764 return NULL;
765
766 /*
767 * First is set, so all of the descriptors we allocated have been added
768 * to first->tx_list, INCLUDING "first" itself. Therefore we
769 * must traverse the list backwards freeing each descriptor in turn
770 *
771 * We're re-using variables for the loop, oh well
772 */
9c3a50b7 773 fsldma_free_desc_list_reverse(chan, &first->tx_list);
bbea0b6e
IS
774 return NULL;
775}
776
c3635c78 777static int fsl_dma_device_control(struct dma_chan *dchan,
05827630 778 enum dma_ctrl_cmd cmd, unsigned long arg)
bbea0b6e 779{
a1c03319 780 struct fsldma_chan *chan;
bbea0b6e
IS
781 unsigned long flags;
782
c3635c78
LW
783 /* Only supports DMA_TERMINATE_ALL */
784 if (cmd != DMA_TERMINATE_ALL)
785 return -ENXIO;
786
a1c03319 787 if (!dchan)
c3635c78 788 return -EINVAL;
bbea0b6e 789
a1c03319 790 chan = to_fsl_chan(dchan);
bbea0b6e
IS
791
792 /* Halt the DMA engine */
a1c03319 793 dma_halt(chan);
bbea0b6e 794
a1c03319 795 spin_lock_irqsave(&chan->desc_lock, flags);
bbea0b6e
IS
796
797 /* Remove and free all of the descriptors in the LD queue */
9c3a50b7
IS
798 fsldma_free_desc_list(chan, &chan->ld_pending);
799 fsldma_free_desc_list(chan, &chan->ld_running);
bbea0b6e 800
a1c03319 801 spin_unlock_irqrestore(&chan->desc_lock, flags);
c3635c78
LW
802
803 return 0;
bbea0b6e
IS
804}
805
173acc7c
ZW
806/**
807 * fsl_dma_update_completed_cookie - Update the completed cookie.
a1c03319 808 * @chan : Freescale DMA channel
9c3a50b7
IS
809 *
810 * CONTEXT: hardirq
173acc7c 811 */
a1c03319 812static void fsl_dma_update_completed_cookie(struct fsldma_chan *chan)
173acc7c 813{
9c3a50b7
IS
814 struct fsl_desc_sw *desc;
815 unsigned long flags;
816 dma_cookie_t cookie;
173acc7c 817
9c3a50b7 818 spin_lock_irqsave(&chan->desc_lock, flags);
173acc7c 819
9c3a50b7
IS
820 if (list_empty(&chan->ld_running)) {
821 dev_dbg(chan->dev, "no running descriptors\n");
822 goto out_unlock;
173acc7c 823 }
9c3a50b7
IS
824
825 /* Get the last descriptor, update the cookie to that */
826 desc = to_fsl_desc(chan->ld_running.prev);
827 if (dma_is_idle(chan))
828 cookie = desc->async_tx.cookie;
76bd061f 829 else {
9c3a50b7 830 cookie = desc->async_tx.cookie - 1;
76bd061f
SM
831 if (unlikely(cookie < DMA_MIN_COOKIE))
832 cookie = DMA_MAX_COOKIE;
833 }
9c3a50b7
IS
834
835 chan->completed_cookie = cookie;
836
837out_unlock:
838 spin_unlock_irqrestore(&chan->desc_lock, flags);
839}
840
841/**
842 * fsldma_desc_status - Check the status of a descriptor
843 * @chan: Freescale DMA channel
844 * @desc: DMA SW descriptor
845 *
846 * This function will return the status of the given descriptor
847 */
848static enum dma_status fsldma_desc_status(struct fsldma_chan *chan,
849 struct fsl_desc_sw *desc)
850{
851 return dma_async_is_complete(desc->async_tx.cookie,
852 chan->completed_cookie,
853 chan->common.cookie);
173acc7c
ZW
854}
855
856/**
857 * fsl_chan_ld_cleanup - Clean up link descriptors
a1c03319 858 * @chan : Freescale DMA channel
173acc7c
ZW
859 *
860 * This function clean up the ld_queue of DMA channel.
173acc7c 861 */
a1c03319 862static void fsl_chan_ld_cleanup(struct fsldma_chan *chan)
173acc7c
ZW
863{
864 struct fsl_desc_sw *desc, *_desc;
865 unsigned long flags;
866
a1c03319 867 spin_lock_irqsave(&chan->desc_lock, flags);
173acc7c 868
9c3a50b7
IS
869 dev_dbg(chan->dev, "chan completed_cookie = %d\n", chan->completed_cookie);
870 list_for_each_entry_safe(desc, _desc, &chan->ld_running, node) {
173acc7c
ZW
871 dma_async_tx_callback callback;
872 void *callback_param;
873
9c3a50b7 874 if (fsldma_desc_status(chan, desc) == DMA_IN_PROGRESS)
173acc7c
ZW
875 break;
876
9c3a50b7 877 /* Remove from the list of running transactions */
173acc7c
ZW
878 list_del(&desc->node);
879
173acc7c 880 /* Run the link descriptor callback function */
9c3a50b7
IS
881 callback = desc->async_tx.callback;
882 callback_param = desc->async_tx.callback_param;
173acc7c 883 if (callback) {
a1c03319 884 spin_unlock_irqrestore(&chan->desc_lock, flags);
9c3a50b7 885 dev_dbg(chan->dev, "LD %p callback\n", desc);
173acc7c 886 callback(callback_param);
a1c03319 887 spin_lock_irqsave(&chan->desc_lock, flags);
173acc7c 888 }
9c3a50b7
IS
889
890 /* Run any dependencies, then free the descriptor */
891 dma_run_dependencies(&desc->async_tx);
892 dma_pool_free(chan->desc_pool, desc, desc->async_tx.phys);
173acc7c 893 }
9c3a50b7 894
a1c03319 895 spin_unlock_irqrestore(&chan->desc_lock, flags);
173acc7c
ZW
896}
897
898/**
9c3a50b7 899 * fsl_chan_xfer_ld_queue - transfer any pending transactions
a1c03319 900 * @chan : Freescale DMA channel
9c3a50b7
IS
901 *
902 * This will make sure that any pending transactions will be run.
903 * If the DMA controller is idle, it will be started. Otherwise,
904 * the DMA controller's interrupt handler will start any pending
905 * transactions when it becomes idle.
173acc7c 906 */
a1c03319 907static void fsl_chan_xfer_ld_queue(struct fsldma_chan *chan)
173acc7c 908{
9c3a50b7 909 struct fsl_desc_sw *desc;
173acc7c
ZW
910 unsigned long flags;
911
a1c03319 912 spin_lock_irqsave(&chan->desc_lock, flags);
138ef018 913
9c3a50b7
IS
914 /*
915 * If the list of pending descriptors is empty, then we
916 * don't need to do any work at all
917 */
918 if (list_empty(&chan->ld_pending)) {
919 dev_dbg(chan->dev, "no pending LDs\n");
138ef018 920 goto out_unlock;
9c3a50b7 921 }
173acc7c 922
9c3a50b7
IS
923 /*
924 * The DMA controller is not idle, which means the interrupt
925 * handler will start any queued transactions when it runs
926 * at the end of the current transaction
927 */
928 if (!dma_is_idle(chan)) {
929 dev_dbg(chan->dev, "DMA controller still busy\n");
930 goto out_unlock;
931 }
932
933 /*
934 * TODO:
935 * make sure the dma_halt() function really un-wedges the
936 * controller as much as possible
937 */
a1c03319 938 dma_halt(chan);
173acc7c 939
9c3a50b7
IS
940 /*
941 * If there are some link descriptors which have not been
942 * transferred, we need to start the controller
173acc7c 943 */
173acc7c 944
9c3a50b7
IS
945 /*
946 * Move all elements from the queue of pending transactions
947 * onto the list of running transactions
948 */
949 desc = list_first_entry(&chan->ld_pending, struct fsl_desc_sw, node);
950 list_splice_tail_init(&chan->ld_pending, &chan->ld_running);
951
952 /*
953 * Program the descriptor's address into the DMA controller,
954 * then start the DMA transaction
955 */
956 set_cdar(chan, desc->async_tx.phys);
957 dma_start(chan);
138ef018
IS
958
959out_unlock:
a1c03319 960 spin_unlock_irqrestore(&chan->desc_lock, flags);
173acc7c
ZW
961}
962
963/**
964 * fsl_dma_memcpy_issue_pending - Issue the DMA start command
a1c03319 965 * @chan : Freescale DMA channel
173acc7c 966 */
a1c03319 967static void fsl_dma_memcpy_issue_pending(struct dma_chan *dchan)
173acc7c 968{
a1c03319 969 struct fsldma_chan *chan = to_fsl_chan(dchan);
a1c03319 970 fsl_chan_xfer_ld_queue(chan);
173acc7c
ZW
971}
972
173acc7c 973/**
07934481 974 * fsl_tx_status - Determine the DMA status
a1c03319 975 * @chan : Freescale DMA channel
173acc7c 976 */
07934481 977static enum dma_status fsl_tx_status(struct dma_chan *dchan,
173acc7c 978 dma_cookie_t cookie,
07934481 979 struct dma_tx_state *txstate)
173acc7c 980{
a1c03319 981 struct fsldma_chan *chan = to_fsl_chan(dchan);
173acc7c
ZW
982 dma_cookie_t last_used;
983 dma_cookie_t last_complete;
984
a1c03319 985 fsl_chan_ld_cleanup(chan);
173acc7c 986
a1c03319
IS
987 last_used = dchan->cookie;
988 last_complete = chan->completed_cookie;
173acc7c 989
bca34692 990 dma_set_tx_state(txstate, last_complete, last_used, 0);
173acc7c
ZW
991
992 return dma_async_is_complete(cookie, last_complete, last_used);
993}
994
d3f620b2
IS
995/*----------------------------------------------------------------------------*/
996/* Interrupt Handling */
997/*----------------------------------------------------------------------------*/
998
e7a29151 999static irqreturn_t fsldma_chan_irq(int irq, void *data)
173acc7c 1000{
a1c03319 1001 struct fsldma_chan *chan = data;
1c62979e
ZW
1002 int update_cookie = 0;
1003 int xfer_ld_q = 0;
a1c03319 1004 u32 stat;
173acc7c 1005
9c3a50b7 1006 /* save and clear the status register */
a1c03319 1007 stat = get_sr(chan);
9c3a50b7
IS
1008 set_sr(chan, stat);
1009 dev_dbg(chan->dev, "irq: channel %d, stat = 0x%x\n", chan->id, stat);
173acc7c
ZW
1010
1011 stat &= ~(FSL_DMA_SR_CB | FSL_DMA_SR_CH);
1012 if (!stat)
1013 return IRQ_NONE;
1014
1015 if (stat & FSL_DMA_SR_TE)
a1c03319 1016 dev_err(chan->dev, "Transfer Error!\n");
173acc7c 1017
9c3a50b7
IS
1018 /*
1019 * Programming Error
f79abb62
ZW
1020 * The DMA_INTERRUPT async_tx is a NULL transfer, which will
1021 * triger a PE interrupt.
1022 */
1023 if (stat & FSL_DMA_SR_PE) {
9c3a50b7 1024 dev_dbg(chan->dev, "irq: Programming Error INT\n");
a1c03319 1025 if (get_bcr(chan) == 0) {
f79abb62
ZW
1026 /* BCR register is 0, this is a DMA_INTERRUPT async_tx.
1027 * Now, update the completed cookie, and continue the
1028 * next uncompleted transfer.
1029 */
1c62979e
ZW
1030 update_cookie = 1;
1031 xfer_ld_q = 1;
f79abb62
ZW
1032 }
1033 stat &= ~FSL_DMA_SR_PE;
1034 }
1035
9c3a50b7
IS
1036 /*
1037 * If the link descriptor segment transfer finishes,
173acc7c
ZW
1038 * we will recycle the used descriptor.
1039 */
1040 if (stat & FSL_DMA_SR_EOSI) {
9c3a50b7
IS
1041 dev_dbg(chan->dev, "irq: End-of-segments INT\n");
1042 dev_dbg(chan->dev, "irq: clndar 0x%llx, nlndar 0x%llx\n",
a1c03319
IS
1043 (unsigned long long)get_cdar(chan),
1044 (unsigned long long)get_ndar(chan));
173acc7c 1045 stat &= ~FSL_DMA_SR_EOSI;
1c62979e
ZW
1046 update_cookie = 1;
1047 }
1048
9c3a50b7
IS
1049 /*
1050 * For MPC8349, EOCDI event need to update cookie
1c62979e
ZW
1051 * and start the next transfer if it exist.
1052 */
1053 if (stat & FSL_DMA_SR_EOCDI) {
9c3a50b7 1054 dev_dbg(chan->dev, "irq: End-of-Chain link INT\n");
1c62979e
ZW
1055 stat &= ~FSL_DMA_SR_EOCDI;
1056 update_cookie = 1;
1057 xfer_ld_q = 1;
173acc7c
ZW
1058 }
1059
9c3a50b7
IS
1060 /*
1061 * If it current transfer is the end-of-transfer,
173acc7c
ZW
1062 * we should clear the Channel Start bit for
1063 * prepare next transfer.
1064 */
1c62979e 1065 if (stat & FSL_DMA_SR_EOLNI) {
9c3a50b7 1066 dev_dbg(chan->dev, "irq: End-of-link INT\n");
173acc7c 1067 stat &= ~FSL_DMA_SR_EOLNI;
1c62979e 1068 xfer_ld_q = 1;
173acc7c
ZW
1069 }
1070
1c62979e 1071 if (update_cookie)
a1c03319 1072 fsl_dma_update_completed_cookie(chan);
1c62979e 1073 if (xfer_ld_q)
a1c03319 1074 fsl_chan_xfer_ld_queue(chan);
173acc7c 1075 if (stat)
9c3a50b7 1076 dev_dbg(chan->dev, "irq: unhandled sr 0x%02x\n", stat);
173acc7c 1077
9c3a50b7 1078 dev_dbg(chan->dev, "irq: Exit\n");
a1c03319 1079 tasklet_schedule(&chan->tasklet);
173acc7c
ZW
1080 return IRQ_HANDLED;
1081}
1082
d3f620b2
IS
1083static void dma_do_tasklet(unsigned long data)
1084{
a1c03319
IS
1085 struct fsldma_chan *chan = (struct fsldma_chan *)data;
1086 fsl_chan_ld_cleanup(chan);
d3f620b2
IS
1087}
1088
1089static irqreturn_t fsldma_ctrl_irq(int irq, void *data)
173acc7c 1090{
a4f56d4b 1091 struct fsldma_device *fdev = data;
d3f620b2
IS
1092 struct fsldma_chan *chan;
1093 unsigned int handled = 0;
1094 u32 gsr, mask;
1095 int i;
173acc7c 1096
e7a29151 1097 gsr = (fdev->feature & FSL_DMA_BIG_ENDIAN) ? in_be32(fdev->regs)
d3f620b2
IS
1098 : in_le32(fdev->regs);
1099 mask = 0xff000000;
1100 dev_dbg(fdev->dev, "IRQ: gsr 0x%.8x\n", gsr);
173acc7c 1101
d3f620b2
IS
1102 for (i = 0; i < FSL_DMA_MAX_CHANS_PER_DEVICE; i++) {
1103 chan = fdev->chan[i];
1104 if (!chan)
1105 continue;
1106
1107 if (gsr & mask) {
1108 dev_dbg(fdev->dev, "IRQ: chan %d\n", chan->id);
1109 fsldma_chan_irq(irq, chan);
1110 handled++;
1111 }
1112
1113 gsr &= ~mask;
1114 mask >>= 8;
1115 }
1116
1117 return IRQ_RETVAL(handled);
173acc7c
ZW
1118}
1119
d3f620b2 1120static void fsldma_free_irqs(struct fsldma_device *fdev)
173acc7c 1121{
d3f620b2
IS
1122 struct fsldma_chan *chan;
1123 int i;
1124
1125 if (fdev->irq != NO_IRQ) {
1126 dev_dbg(fdev->dev, "free per-controller IRQ\n");
1127 free_irq(fdev->irq, fdev);
1128 return;
1129 }
1130
1131 for (i = 0; i < FSL_DMA_MAX_CHANS_PER_DEVICE; i++) {
1132 chan = fdev->chan[i];
1133 if (chan && chan->irq != NO_IRQ) {
1134 dev_dbg(fdev->dev, "free channel %d IRQ\n", chan->id);
1135 free_irq(chan->irq, chan);
1136 }
1137 }
1138}
1139
1140static int fsldma_request_irqs(struct fsldma_device *fdev)
1141{
1142 struct fsldma_chan *chan;
1143 int ret;
1144 int i;
1145
1146 /* if we have a per-controller IRQ, use that */
1147 if (fdev->irq != NO_IRQ) {
1148 dev_dbg(fdev->dev, "request per-controller IRQ\n");
1149 ret = request_irq(fdev->irq, fsldma_ctrl_irq, IRQF_SHARED,
1150 "fsldma-controller", fdev);
1151 return ret;
1152 }
1153
1154 /* no per-controller IRQ, use the per-channel IRQs */
1155 for (i = 0; i < FSL_DMA_MAX_CHANS_PER_DEVICE; i++) {
1156 chan = fdev->chan[i];
1157 if (!chan)
1158 continue;
1159
1160 if (chan->irq == NO_IRQ) {
1161 dev_err(fdev->dev, "no interrupts property defined for "
1162 "DMA channel %d. Please fix your "
1163 "device tree\n", chan->id);
1164 ret = -ENODEV;
1165 goto out_unwind;
1166 }
1167
1168 dev_dbg(fdev->dev, "request channel %d IRQ\n", chan->id);
1169 ret = request_irq(chan->irq, fsldma_chan_irq, IRQF_SHARED,
1170 "fsldma-chan", chan);
1171 if (ret) {
1172 dev_err(fdev->dev, "unable to request IRQ for DMA "
1173 "channel %d\n", chan->id);
1174 goto out_unwind;
1175 }
1176 }
1177
1178 return 0;
1179
1180out_unwind:
1181 for (/* none */; i >= 0; i--) {
1182 chan = fdev->chan[i];
1183 if (!chan)
1184 continue;
1185
1186 if (chan->irq == NO_IRQ)
1187 continue;
1188
1189 free_irq(chan->irq, chan);
1190 }
1191
1192 return ret;
173acc7c
ZW
1193}
1194
a4f56d4b
IS
1195/*----------------------------------------------------------------------------*/
1196/* OpenFirmware Subsystem */
1197/*----------------------------------------------------------------------------*/
1198
1199static int __devinit fsl_dma_chan_probe(struct fsldma_device *fdev,
77cd62e8 1200 struct device_node *node, u32 feature, const char *compatible)
173acc7c 1201{
a1c03319 1202 struct fsldma_chan *chan;
4ce0e953 1203 struct resource res;
173acc7c
ZW
1204 int err;
1205
173acc7c 1206 /* alloc channel */
a1c03319
IS
1207 chan = kzalloc(sizeof(*chan), GFP_KERNEL);
1208 if (!chan) {
e7a29151
IS
1209 dev_err(fdev->dev, "no free memory for DMA channels!\n");
1210 err = -ENOMEM;
1211 goto out_return;
1212 }
1213
1214 /* ioremap registers for use */
a1c03319
IS
1215 chan->regs = of_iomap(node, 0);
1216 if (!chan->regs) {
e7a29151
IS
1217 dev_err(fdev->dev, "unable to ioremap registers\n");
1218 err = -ENOMEM;
a1c03319 1219 goto out_free_chan;
173acc7c
ZW
1220 }
1221
4ce0e953 1222 err = of_address_to_resource(node, 0, &res);
173acc7c 1223 if (err) {
e7a29151
IS
1224 dev_err(fdev->dev, "unable to find 'reg' property\n");
1225 goto out_iounmap_regs;
173acc7c
ZW
1226 }
1227
a1c03319 1228 chan->feature = feature;
173acc7c 1229 if (!fdev->feature)
a1c03319 1230 fdev->feature = chan->feature;
173acc7c 1231
e7a29151
IS
1232 /*
1233 * If the DMA device's feature is different than the feature
1234 * of its channels, report the bug
173acc7c 1235 */
a1c03319 1236 WARN_ON(fdev->feature != chan->feature);
e7a29151 1237
a1c03319
IS
1238 chan->dev = fdev->dev;
1239 chan->id = ((res.start - 0x100) & 0xfff) >> 7;
1240 if (chan->id >= FSL_DMA_MAX_CHANS_PER_DEVICE) {
e7a29151 1241 dev_err(fdev->dev, "too many channels for device\n");
173acc7c 1242 err = -EINVAL;
e7a29151 1243 goto out_iounmap_regs;
173acc7c 1244 }
173acc7c 1245
a1c03319
IS
1246 fdev->chan[chan->id] = chan;
1247 tasklet_init(&chan->tasklet, dma_do_tasklet, (unsigned long)chan);
e7a29151
IS
1248
1249 /* Initialize the channel */
a1c03319 1250 dma_init(chan);
173acc7c
ZW
1251
1252 /* Clear cdar registers */
a1c03319 1253 set_cdar(chan, 0);
173acc7c 1254
a1c03319 1255 switch (chan->feature & FSL_DMA_IP_MASK) {
173acc7c 1256 case FSL_DMA_IP_85XX:
a1c03319 1257 chan->toggle_ext_pause = fsl_chan_toggle_ext_pause;
173acc7c 1258 case FSL_DMA_IP_83XX:
a1c03319
IS
1259 chan->toggle_ext_start = fsl_chan_toggle_ext_start;
1260 chan->set_src_loop_size = fsl_chan_set_src_loop_size;
1261 chan->set_dst_loop_size = fsl_chan_set_dst_loop_size;
1262 chan->set_request_count = fsl_chan_set_request_count;
173acc7c
ZW
1263 }
1264
a1c03319 1265 spin_lock_init(&chan->desc_lock);
9c3a50b7
IS
1266 INIT_LIST_HEAD(&chan->ld_pending);
1267 INIT_LIST_HEAD(&chan->ld_running);
173acc7c 1268
a1c03319 1269 chan->common.device = &fdev->common;
173acc7c 1270
d3f620b2 1271 /* find the IRQ line, if it exists in the device tree */
a1c03319 1272 chan->irq = irq_of_parse_and_map(node, 0);
d3f620b2 1273
173acc7c 1274 /* Add the channel to DMA device channel list */
a1c03319 1275 list_add_tail(&chan->common.device_node, &fdev->common.channels);
173acc7c
ZW
1276 fdev->common.chancnt++;
1277
a1c03319
IS
1278 dev_info(fdev->dev, "#%d (%s), irq %d\n", chan->id, compatible,
1279 chan->irq != NO_IRQ ? chan->irq : fdev->irq);
173acc7c
ZW
1280
1281 return 0;
51ee87f2 1282
e7a29151 1283out_iounmap_regs:
a1c03319
IS
1284 iounmap(chan->regs);
1285out_free_chan:
1286 kfree(chan);
e7a29151 1287out_return:
173acc7c
ZW
1288 return err;
1289}
1290
a1c03319 1291static void fsl_dma_chan_remove(struct fsldma_chan *chan)
173acc7c 1292{
a1c03319
IS
1293 irq_dispose_mapping(chan->irq);
1294 list_del(&chan->common.device_node);
1295 iounmap(chan->regs);
1296 kfree(chan);
173acc7c
ZW
1297}
1298
e7a29151 1299static int __devinit fsldma_of_probe(struct of_device *op,
173acc7c
ZW
1300 const struct of_device_id *match)
1301{
a4f56d4b 1302 struct fsldma_device *fdev;
77cd62e8 1303 struct device_node *child;
e7a29151 1304 int err;
173acc7c 1305
a4f56d4b 1306 fdev = kzalloc(sizeof(*fdev), GFP_KERNEL);
173acc7c 1307 if (!fdev) {
e7a29151
IS
1308 dev_err(&op->dev, "No enough memory for 'priv'\n");
1309 err = -ENOMEM;
1310 goto out_return;
173acc7c 1311 }
e7a29151
IS
1312
1313 fdev->dev = &op->dev;
173acc7c
ZW
1314 INIT_LIST_HEAD(&fdev->common.channels);
1315
e7a29151
IS
1316 /* ioremap the registers for use */
1317 fdev->regs = of_iomap(op->node, 0);
1318 if (!fdev->regs) {
1319 dev_err(&op->dev, "unable to ioremap registers\n");
1320 err = -ENOMEM;
1321 goto out_free_fdev;
173acc7c
ZW
1322 }
1323
d3f620b2
IS
1324 /* map the channel IRQ if it exists, but don't hookup the handler yet */
1325 fdev->irq = irq_of_parse_and_map(op->node, 0);
1326
173acc7c
ZW
1327 dma_cap_set(DMA_MEMCPY, fdev->common.cap_mask);
1328 dma_cap_set(DMA_INTERRUPT, fdev->common.cap_mask);
bbea0b6e 1329 dma_cap_set(DMA_SLAVE, fdev->common.cap_mask);
173acc7c
ZW
1330 fdev->common.device_alloc_chan_resources = fsl_dma_alloc_chan_resources;
1331 fdev->common.device_free_chan_resources = fsl_dma_free_chan_resources;
2187c269 1332 fdev->common.device_prep_dma_interrupt = fsl_dma_prep_interrupt;
173acc7c 1333 fdev->common.device_prep_dma_memcpy = fsl_dma_prep_memcpy;
07934481 1334 fdev->common.device_tx_status = fsl_tx_status;
173acc7c 1335 fdev->common.device_issue_pending = fsl_dma_memcpy_issue_pending;
bbea0b6e 1336 fdev->common.device_prep_slave_sg = fsl_dma_prep_slave_sg;
c3635c78 1337 fdev->common.device_control = fsl_dma_device_control;
e7a29151 1338 fdev->common.dev = &op->dev;
173acc7c 1339
e7a29151 1340 dev_set_drvdata(&op->dev, fdev);
77cd62e8 1341
e7a29151
IS
1342 /*
1343 * We cannot use of_platform_bus_probe() because there is no
1344 * of_platform_bus_remove(). Instead, we manually instantiate every DMA
77cd62e8
TT
1345 * channel object.
1346 */
e7a29151
IS
1347 for_each_child_of_node(op->node, child) {
1348 if (of_device_is_compatible(child, "fsl,eloplus-dma-channel")) {
77cd62e8
TT
1349 fsl_dma_chan_probe(fdev, child,
1350 FSL_DMA_IP_85XX | FSL_DMA_BIG_ENDIAN,
1351 "fsl,eloplus-dma-channel");
e7a29151
IS
1352 }
1353
1354 if (of_device_is_compatible(child, "fsl,elo-dma-channel")) {
77cd62e8
TT
1355 fsl_dma_chan_probe(fdev, child,
1356 FSL_DMA_IP_83XX | FSL_DMA_LITTLE_ENDIAN,
1357 "fsl,elo-dma-channel");
e7a29151 1358 }
77cd62e8 1359 }
173acc7c 1360
d3f620b2
IS
1361 /*
1362 * Hookup the IRQ handler(s)
1363 *
1364 * If we have a per-controller interrupt, we prefer that to the
1365 * per-channel interrupts to reduce the number of shared interrupt
1366 * handlers on the same IRQ line
1367 */
1368 err = fsldma_request_irqs(fdev);
1369 if (err) {
1370 dev_err(fdev->dev, "unable to request IRQs\n");
1371 goto out_free_fdev;
1372 }
1373
173acc7c
ZW
1374 dma_async_device_register(&fdev->common);
1375 return 0;
1376
e7a29151 1377out_free_fdev:
d3f620b2 1378 irq_dispose_mapping(fdev->irq);
173acc7c 1379 kfree(fdev);
e7a29151 1380out_return:
173acc7c
ZW
1381 return err;
1382}
1383
e7a29151 1384static int fsldma_of_remove(struct of_device *op)
77cd62e8 1385{
a4f56d4b 1386 struct fsldma_device *fdev;
77cd62e8
TT
1387 unsigned int i;
1388
e7a29151 1389 fdev = dev_get_drvdata(&op->dev);
77cd62e8
TT
1390 dma_async_device_unregister(&fdev->common);
1391
d3f620b2
IS
1392 fsldma_free_irqs(fdev);
1393
e7a29151 1394 for (i = 0; i < FSL_DMA_MAX_CHANS_PER_DEVICE; i++) {
77cd62e8
TT
1395 if (fdev->chan[i])
1396 fsl_dma_chan_remove(fdev->chan[i]);
e7a29151 1397 }
77cd62e8 1398
e7a29151
IS
1399 iounmap(fdev->regs);
1400 dev_set_drvdata(&op->dev, NULL);
77cd62e8 1401 kfree(fdev);
77cd62e8
TT
1402
1403 return 0;
1404}
1405
4b1cf1fa 1406static const struct of_device_id fsldma_of_ids[] = {
049c9d45
KG
1407 { .compatible = "fsl,eloplus-dma", },
1408 { .compatible = "fsl,elo-dma", },
173acc7c
ZW
1409 {}
1410};
1411
a4f56d4b
IS
1412static struct of_platform_driver fsldma_of_driver = {
1413 .name = "fsl-elo-dma",
1414 .match_table = fsldma_of_ids,
1415 .probe = fsldma_of_probe,
1416 .remove = fsldma_of_remove,
173acc7c
ZW
1417};
1418
a4f56d4b
IS
1419/*----------------------------------------------------------------------------*/
1420/* Module Init / Exit */
1421/*----------------------------------------------------------------------------*/
1422
1423static __init int fsldma_init(void)
173acc7c 1424{
77cd62e8
TT
1425 int ret;
1426
1427 pr_info("Freescale Elo / Elo Plus DMA driver\n");
1428
a4f56d4b 1429 ret = of_register_platform_driver(&fsldma_of_driver);
77cd62e8
TT
1430 if (ret)
1431 pr_err("fsldma: failed to register platform driver\n");
1432
1433 return ret;
1434}
1435
a4f56d4b 1436static void __exit fsldma_exit(void)
77cd62e8 1437{
a4f56d4b 1438 of_unregister_platform_driver(&fsldma_of_driver);
173acc7c
ZW
1439}
1440
a4f56d4b
IS
1441subsys_initcall(fsldma_init);
1442module_exit(fsldma_exit);
77cd62e8
TT
1443
1444MODULE_DESCRIPTION("Freescale Elo / Elo Plus DMA driver");
1445MODULE_LICENSE("GPL");