blob: ab12fa5a129616a63b775a8a3ff014c5dac1fc04 [file] [log] [blame]
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +00001/*
2 * Renesas SuperH DMA Engine support
3 *
4 * base is drivers/dma/flsdma.c
5 *
6 * Copyright (C) 2009 Nobuhiro Iwamatsu <iwamatsu.nobuhiro@renesas.com>
7 * Copyright (C) 2009 Renesas Solutions, Inc. All rights reserved.
8 * Copyright (C) 2007 Freescale Semiconductor, Inc. All rights reserved.
9 *
10 * This is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * - DMA of SuperH does not have Hardware DMA chain mode.
16 * - MAX DMA size is 16MB.
17 *
18 */
19
20#include <linux/init.h>
21#include <linux/module.h>
22#include <linux/interrupt.h>
23#include <linux/dmaengine.h>
24#include <linux/delay.h>
25#include <linux/dma-mapping.h>
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +000026#include <linux/platform_device.h>
27#include <cpu/dma.h>
28#include <asm/dma-sh.h>
29#include "shdma.h"
30
31/* DMA descriptor control */
Guennadi Liakhovetski3542a112009-12-17 09:41:39 -070032enum sh_dmae_desc_status {
33 DESC_IDLE,
34 DESC_PREPARED,
35 DESC_SUBMITTED,
36 DESC_COMPLETED, /* completed, have to call callback */
37 DESC_WAITING, /* callback called, waiting for ack / re-submit */
38};
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +000039
40#define NR_DESCS_PER_CHANNEL 32
41/*
42 * Define the default configuration for dual address memory-memory transfer.
43 * The 0x400 value represents auto-request, external->external.
44 *
45 * And this driver set 4byte burst mode.
46 * If you want to change mode, you need to change RS_DEFAULT of value.
47 * (ex 1byte burst mode -> (RS_DUAL & ~TS_32)
48 */
49#define RS_DEFAULT (RS_DUAL)
50
Guennadi Liakhovetskicfefe992010-02-03 14:46:41 +000051/* A bitmask with bits enough for enum sh_dmae_slave_chan_id */
52static unsigned long sh_dmae_slave_used[BITS_TO_LONGS(SHDMA_SLAVE_NUMBER)];
53
Guennadi Liakhovetski3542a112009-12-17 09:41:39 -070054static void sh_dmae_chan_ld_cleanup(struct sh_dmae_chan *sh_chan, bool all);
55
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +000056static void sh_dmae_writel(struct sh_dmae_chan *sh_dc, u32 data, u32 reg)
57{
Guennadi Liakhovetski027811b2010-02-11 16:50:10 +000058 __raw_writel(data, sh_dc->base + reg / sizeof(u32));
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +000059}
60
61static u32 sh_dmae_readl(struct sh_dmae_chan *sh_dc, u32 reg)
62{
Guennadi Liakhovetski027811b2010-02-11 16:50:10 +000063 return __raw_readl(sh_dc->base + reg / sizeof(u32));
64}
65
66static u16 dmaor_read(struct sh_dmae_device *shdev)
67{
68 return __raw_readw(shdev->chan_reg + DMAOR / sizeof(u32));
69}
70
71static void dmaor_write(struct sh_dmae_device *shdev, u16 data)
72{
73 __raw_writew(data, shdev->chan_reg + DMAOR / sizeof(u32));
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +000074}
75
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +000076/*
77 * Reset DMA controller
78 *
79 * SH7780 has two DMAOR register
80 */
Guennadi Liakhovetski027811b2010-02-11 16:50:10 +000081static void sh_dmae_ctl_stop(struct sh_dmae_device *shdev)
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +000082{
Guennadi Liakhovetski027811b2010-02-11 16:50:10 +000083 unsigned short dmaor = dmaor_read(shdev);
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +000084
Guennadi Liakhovetski027811b2010-02-11 16:50:10 +000085 dmaor_write(shdev, dmaor & ~(DMAOR_NMIF | DMAOR_AE | DMAOR_DME));
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +000086}
87
Guennadi Liakhovetski027811b2010-02-11 16:50:10 +000088static int sh_dmae_rst(struct sh_dmae_device *shdev)
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +000089{
90 unsigned short dmaor;
91
Guennadi Liakhovetski027811b2010-02-11 16:50:10 +000092 sh_dmae_ctl_stop(shdev);
93 dmaor = dmaor_read(shdev) | DMAOR_INIT;
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +000094
Guennadi Liakhovetski027811b2010-02-11 16:50:10 +000095 dmaor_write(shdev, dmaor);
96 if (dmaor_read(shdev) & (DMAOR_AE | DMAOR_NMIF)) {
Guennadi Liakhovetski47a4dc22010-02-11 16:50:05 +000097 pr_warning("dma-sh: Can't initialize DMAOR.\n");
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +000098 return -EINVAL;
99 }
100 return 0;
101}
102
Guennadi Liakhovetskifc461852010-01-19 07:24:55 +0000103static bool dmae_is_busy(struct sh_dmae_chan *sh_chan)
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000104{
105 u32 chcr = sh_dmae_readl(sh_chan, CHCR);
Guennadi Liakhovetskifc461852010-01-19 07:24:55 +0000106
107 if ((chcr & (CHCR_DE | CHCR_TE)) == CHCR_DE)
108 return true; /* working */
109
110 return false; /* waiting */
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000111}
112
Guennadi Liakhovetski623b4ac2010-02-03 14:44:12 +0000113static unsigned int ts_shift[] = TS_SHIFT;
Guennadi Liakhovetskicfefe992010-02-03 14:46:41 +0000114static inline unsigned int calc_xmit_shift(u32 chcr)
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000115{
Guennadi Liakhovetski623b4ac2010-02-03 14:44:12 +0000116 int cnt = ((chcr & CHCR_TS_LOW_MASK) >> CHCR_TS_LOW_SHIFT) |
117 ((chcr & CHCR_TS_HIGH_MASK) >> CHCR_TS_HIGH_SHIFT);
118
119 return ts_shift[cnt];
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000120}
121
Guennadi Liakhovetski3542a112009-12-17 09:41:39 -0700122static void dmae_set_reg(struct sh_dmae_chan *sh_chan, struct sh_dmae_regs *hw)
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000123{
Guennadi Liakhovetski3542a112009-12-17 09:41:39 -0700124 sh_dmae_writel(sh_chan, hw->sar, SAR);
125 sh_dmae_writel(sh_chan, hw->dar, DAR);
Guennadi Liakhovetskicfefe992010-02-03 14:46:41 +0000126 sh_dmae_writel(sh_chan, hw->tcr >> sh_chan->xmit_shift, TCR);
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000127}
128
129static void dmae_start(struct sh_dmae_chan *sh_chan)
130{
131 u32 chcr = sh_dmae_readl(sh_chan, CHCR);
132
Guennadi Liakhovetski86d61b32009-12-10 18:35:07 +0100133 chcr |= CHCR_DE | CHCR_IE;
Guennadi Liakhovetskicfefe992010-02-03 14:46:41 +0000134 sh_dmae_writel(sh_chan, chcr & ~CHCR_TE, CHCR);
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000135}
136
137static void dmae_halt(struct sh_dmae_chan *sh_chan)
138{
139 u32 chcr = sh_dmae_readl(sh_chan, CHCR);
140
141 chcr &= ~(CHCR_DE | CHCR_TE | CHCR_IE);
142 sh_dmae_writel(sh_chan, chcr, CHCR);
143}
144
Guennadi Liakhovetskicfefe992010-02-03 14:46:41 +0000145static void dmae_init(struct sh_dmae_chan *sh_chan)
146{
147 u32 chcr = RS_DEFAULT; /* default is DUAL mode */
148 sh_chan->xmit_shift = calc_xmit_shift(chcr);
149 sh_dmae_writel(sh_chan, chcr, CHCR);
150}
151
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000152static int dmae_set_chcr(struct sh_dmae_chan *sh_chan, u32 val)
153{
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000154 /* When DMA was working, can not set data to CHCR */
Guennadi Liakhovetskifc461852010-01-19 07:24:55 +0000155 if (dmae_is_busy(sh_chan))
156 return -EBUSY;
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000157
Guennadi Liakhovetskicfefe992010-02-03 14:46:41 +0000158 sh_chan->xmit_shift = calc_xmit_shift(val);
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000159 sh_dmae_writel(sh_chan, val, CHCR);
Guennadi Liakhovetskicfefe992010-02-03 14:46:41 +0000160
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000161 return 0;
162}
163
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000164static int dmae_set_dmars(struct sh_dmae_chan *sh_chan, u16 val)
165{
Guennadi Liakhovetski027811b2010-02-11 16:50:10 +0000166 struct sh_dmae_device *shdev = container_of(sh_chan->common.device,
167 struct sh_dmae_device, common);
168 struct sh_dmae_pdata *pdata = shdev->pdata;
169 struct sh_dmae_channel *chan_pdata = &pdata->channel[sh_chan->id];
170 u16 __iomem *addr = shdev->dmars + chan_pdata->dmars / sizeof(u16);
171 int shift = chan_pdata->dmars_bit;
Guennadi Liakhovetskifc461852010-01-19 07:24:55 +0000172
173 if (dmae_is_busy(sh_chan))
174 return -EBUSY;
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000175
Guennadi Liakhovetski027811b2010-02-11 16:50:10 +0000176 __raw_writew((__raw_readw(addr) & (0xff00 >> shift)) | (val << shift),
177 addr);
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000178
179 return 0;
180}
181
182static dma_cookie_t sh_dmae_tx_submit(struct dma_async_tx_descriptor *tx)
183{
Guennadi Liakhovetski3542a112009-12-17 09:41:39 -0700184 struct sh_desc *desc = tx_to_sh_desc(tx), *chunk, *last = desc, *c;
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000185 struct sh_dmae_chan *sh_chan = to_sh_chan(tx->chan);
Guennadi Liakhovetski3542a112009-12-17 09:41:39 -0700186 dma_async_tx_callback callback = tx->callback;
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000187 dma_cookie_t cookie;
188
189 spin_lock_bh(&sh_chan->desc_lock);
190
191 cookie = sh_chan->common.cookie;
192 cookie++;
193 if (cookie < 0)
194 cookie = 1;
195
Guennadi Liakhovetski3542a112009-12-17 09:41:39 -0700196 sh_chan->common.cookie = cookie;
197 tx->cookie = cookie;
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000198
Guennadi Liakhovetski3542a112009-12-17 09:41:39 -0700199 /* Mark all chunks of this descriptor as submitted, move to the queue */
200 list_for_each_entry_safe(chunk, c, desc->node.prev, node) {
201 /*
202 * All chunks are on the global ld_free, so, we have to find
203 * the end of the chain ourselves
204 */
205 if (chunk != desc && (chunk->mark == DESC_IDLE ||
206 chunk->async_tx.cookie > 0 ||
207 chunk->async_tx.cookie == -EBUSY ||
208 &chunk->node == &sh_chan->ld_free))
209 break;
210 chunk->mark = DESC_SUBMITTED;
211 /* Callback goes to the last chunk */
212 chunk->async_tx.callback = NULL;
213 chunk->cookie = cookie;
214 list_move_tail(&chunk->node, &sh_chan->ld_queue);
215 last = chunk;
216 }
217
218 last->async_tx.callback = callback;
219 last->async_tx.callback_param = tx->callback_param;
220
221 dev_dbg(sh_chan->dev, "submit #%d@%p on %d: %x[%d] -> %x\n",
222 tx->cookie, &last->async_tx, sh_chan->id,
223 desc->hw.sar, desc->hw.tcr, desc->hw.dar);
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000224
225 spin_unlock_bh(&sh_chan->desc_lock);
226
227 return cookie;
228}
229
Guennadi Liakhovetski3542a112009-12-17 09:41:39 -0700230/* Called with desc_lock held */
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000231static struct sh_desc *sh_dmae_get_desc(struct sh_dmae_chan *sh_chan)
232{
Guennadi Liakhovetski3542a112009-12-17 09:41:39 -0700233 struct sh_desc *desc;
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000234
Guennadi Liakhovetski3542a112009-12-17 09:41:39 -0700235 list_for_each_entry(desc, &sh_chan->ld_free, node)
236 if (desc->mark != DESC_PREPARED) {
237 BUG_ON(desc->mark != DESC_IDLE);
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000238 list_del(&desc->node);
Guennadi Liakhovetski3542a112009-12-17 09:41:39 -0700239 return desc;
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000240 }
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000241
Guennadi Liakhovetski3542a112009-12-17 09:41:39 -0700242 return NULL;
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000243}
244
Guennadi Liakhovetskicfefe992010-02-03 14:46:41 +0000245static struct sh_dmae_slave_config *sh_dmae_find_slave(
246 struct sh_dmae_chan *sh_chan, enum sh_dmae_slave_chan_id slave_id)
247{
248 struct dma_device *dma_dev = sh_chan->common.device;
249 struct sh_dmae_device *shdev = container_of(dma_dev,
250 struct sh_dmae_device, common);
Guennadi Liakhovetski027811b2010-02-11 16:50:10 +0000251 struct sh_dmae_pdata *pdata = shdev->pdata;
Guennadi Liakhovetskicfefe992010-02-03 14:46:41 +0000252 int i;
253
254 if ((unsigned)slave_id >= SHDMA_SLAVE_NUMBER)
255 return NULL;
256
Guennadi Liakhovetski027811b2010-02-11 16:50:10 +0000257 for (i = 0; i < pdata->slave_num; i++)
258 if (pdata->slave[i].slave_id == slave_id)
259 return pdata->slave + i;
Guennadi Liakhovetskicfefe992010-02-03 14:46:41 +0000260
261 return NULL;
262}
263
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000264static int sh_dmae_alloc_chan_resources(struct dma_chan *chan)
265{
266 struct sh_dmae_chan *sh_chan = to_sh_chan(chan);
267 struct sh_desc *desc;
Guennadi Liakhovetskicfefe992010-02-03 14:46:41 +0000268 struct sh_dmae_slave *param = chan->private;
269
270 /*
271 * This relies on the guarantee from dmaengine that alloc_chan_resources
272 * never runs concurrently with itself or free_chan_resources.
273 */
274 if (param) {
275 struct sh_dmae_slave_config *cfg;
276
277 cfg = sh_dmae_find_slave(sh_chan, param->slave_id);
278 if (!cfg)
279 return -EINVAL;
280
281 if (test_and_set_bit(param->slave_id, sh_dmae_slave_used))
282 return -EBUSY;
283
284 param->config = cfg;
285
286 dmae_set_dmars(sh_chan, cfg->mid_rid);
287 dmae_set_chcr(sh_chan, cfg->chcr);
288 } else {
289 if ((sh_dmae_readl(sh_chan, CHCR) & 0x700) != 0x400)
290 dmae_set_chcr(sh_chan, RS_DEFAULT);
291 }
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000292
293 spin_lock_bh(&sh_chan->desc_lock);
294 while (sh_chan->descs_allocated < NR_DESCS_PER_CHANNEL) {
295 spin_unlock_bh(&sh_chan->desc_lock);
296 desc = kzalloc(sizeof(struct sh_desc), GFP_KERNEL);
297 if (!desc) {
298 spin_lock_bh(&sh_chan->desc_lock);
299 break;
300 }
301 dma_async_tx_descriptor_init(&desc->async_tx,
302 &sh_chan->common);
303 desc->async_tx.tx_submit = sh_dmae_tx_submit;
Guennadi Liakhovetski3542a112009-12-17 09:41:39 -0700304 desc->mark = DESC_IDLE;
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000305
306 spin_lock_bh(&sh_chan->desc_lock);
Guennadi Liakhovetski3542a112009-12-17 09:41:39 -0700307 list_add(&desc->node, &sh_chan->ld_free);
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000308 sh_chan->descs_allocated++;
309 }
310 spin_unlock_bh(&sh_chan->desc_lock);
311
312 return sh_chan->descs_allocated;
313}
314
315/*
316 * sh_dma_free_chan_resources - Free all resources of the channel.
317 */
318static void sh_dmae_free_chan_resources(struct dma_chan *chan)
319{
320 struct sh_dmae_chan *sh_chan = to_sh_chan(chan);
321 struct sh_desc *desc, *_desc;
322 LIST_HEAD(list);
323
Guennadi Liakhovetskicfefe992010-02-03 14:46:41 +0000324 dmae_halt(sh_chan);
325
Guennadi Liakhovetski3542a112009-12-17 09:41:39 -0700326 /* Prepared and not submitted descriptors can still be on the queue */
327 if (!list_empty(&sh_chan->ld_queue))
328 sh_dmae_chan_ld_cleanup(sh_chan, true);
329
Guennadi Liakhovetskicfefe992010-02-03 14:46:41 +0000330 if (chan->private) {
331 /* The caller is holding dma_list_mutex */
332 struct sh_dmae_slave *param = chan->private;
333 clear_bit(param->slave_id, sh_dmae_slave_used);
334 }
335
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000336 spin_lock_bh(&sh_chan->desc_lock);
337
338 list_splice_init(&sh_chan->ld_free, &list);
339 sh_chan->descs_allocated = 0;
340
341 spin_unlock_bh(&sh_chan->desc_lock);
342
343 list_for_each_entry_safe(desc, _desc, &list, node)
344 kfree(desc);
345}
346
Guennadi Liakhovetskicfefe992010-02-03 14:46:41 +0000347/**
Guennadi Liakhovetskifc461852010-01-19 07:24:55 +0000348 * sh_dmae_add_desc - get, set up and return one transfer descriptor
349 * @sh_chan: DMA channel
350 * @flags: DMA transfer flags
351 * @dest: destination DMA address, incremented when direction equals
352 * DMA_FROM_DEVICE or DMA_BIDIRECTIONAL
353 * @src: source DMA address, incremented when direction equals
354 * DMA_TO_DEVICE or DMA_BIDIRECTIONAL
355 * @len: DMA transfer length
356 * @first: if NULL, set to the current descriptor and cookie set to -EBUSY
357 * @direction: needed for slave DMA to decide which address to keep constant,
358 * equals DMA_BIDIRECTIONAL for MEMCPY
359 * Returns 0 or an error
360 * Locks: called with desc_lock held
361 */
362static struct sh_desc *sh_dmae_add_desc(struct sh_dmae_chan *sh_chan,
363 unsigned long flags, dma_addr_t *dest, dma_addr_t *src, size_t *len,
364 struct sh_desc **first, enum dma_data_direction direction)
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000365{
Guennadi Liakhovetskifc461852010-01-19 07:24:55 +0000366 struct sh_desc *new;
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000367 size_t copy_size;
Guennadi Liakhovetskifc461852010-01-19 07:24:55 +0000368
369 if (!*len)
370 return NULL;
371
372 /* Allocate the link descriptor from the free list */
373 new = sh_dmae_get_desc(sh_chan);
374 if (!new) {
375 dev_err(sh_chan->dev, "No free link descriptor available\n");
376 return NULL;
377 }
378
379 copy_size = min(*len, (size_t)SH_DMA_TCR_MAX + 1);
380
381 new->hw.sar = *src;
382 new->hw.dar = *dest;
383 new->hw.tcr = copy_size;
384
385 if (!*first) {
386 /* First desc */
387 new->async_tx.cookie = -EBUSY;
388 *first = new;
389 } else {
390 /* Other desc - invisible to the user */
391 new->async_tx.cookie = -EINVAL;
392 }
393
Guennadi Liakhovetskicfefe992010-02-03 14:46:41 +0000394 dev_dbg(sh_chan->dev,
395 "chaining (%u/%u)@%x -> %x with %p, cookie %d, shift %d\n",
Guennadi Liakhovetskifc461852010-01-19 07:24:55 +0000396 copy_size, *len, *src, *dest, &new->async_tx,
Guennadi Liakhovetskicfefe992010-02-03 14:46:41 +0000397 new->async_tx.cookie, sh_chan->xmit_shift);
Guennadi Liakhovetskifc461852010-01-19 07:24:55 +0000398
399 new->mark = DESC_PREPARED;
400 new->async_tx.flags = flags;
Guennadi Liakhovetskicfefe992010-02-03 14:46:41 +0000401 new->direction = direction;
Guennadi Liakhovetskifc461852010-01-19 07:24:55 +0000402
403 *len -= copy_size;
404 if (direction == DMA_BIDIRECTIONAL || direction == DMA_TO_DEVICE)
405 *src += copy_size;
406 if (direction == DMA_BIDIRECTIONAL || direction == DMA_FROM_DEVICE)
407 *dest += copy_size;
408
409 return new;
410}
411
412/*
413 * sh_dmae_prep_sg - prepare transfer descriptors from an SG list
414 *
415 * Common routine for public (MEMCPY) and slave DMA. The MEMCPY case is also
416 * converted to scatter-gather to guarantee consistent locking and a correct
417 * list manipulation. For slave DMA direction carries the usual meaning, and,
418 * logically, the SG list is RAM and the addr variable contains slave address,
419 * e.g., the FIFO I/O register. For MEMCPY direction equals DMA_BIDIRECTIONAL
420 * and the SG list contains only one element and points at the source buffer.
421 */
422static struct dma_async_tx_descriptor *sh_dmae_prep_sg(struct sh_dmae_chan *sh_chan,
423 struct scatterlist *sgl, unsigned int sg_len, dma_addr_t *addr,
424 enum dma_data_direction direction, unsigned long flags)
425{
426 struct scatterlist *sg;
427 struct sh_desc *first = NULL, *new = NULL /* compiler... */;
Guennadi Liakhovetski3542a112009-12-17 09:41:39 -0700428 LIST_HEAD(tx_list);
Guennadi Liakhovetskifc461852010-01-19 07:24:55 +0000429 int chunks = 0;
430 int i;
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000431
Guennadi Liakhovetskifc461852010-01-19 07:24:55 +0000432 if (!sg_len)
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000433 return NULL;
434
Guennadi Liakhovetskifc461852010-01-19 07:24:55 +0000435 for_each_sg(sgl, sg, sg_len, i)
436 chunks += (sg_dma_len(sg) + SH_DMA_TCR_MAX) /
437 (SH_DMA_TCR_MAX + 1);
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000438
Guennadi Liakhovetski3542a112009-12-17 09:41:39 -0700439 /* Have to lock the whole loop to protect against concurrent release */
440 spin_lock_bh(&sh_chan->desc_lock);
441
442 /*
443 * Chaining:
444 * first descriptor is what user is dealing with in all API calls, its
445 * cookie is at first set to -EBUSY, at tx-submit to a positive
446 * number
447 * if more than one chunk is needed further chunks have cookie = -EINVAL
448 * the last chunk, if not equal to the first, has cookie = -ENOSPC
449 * all chunks are linked onto the tx_list head with their .node heads
450 * only during this function, then they are immediately spliced
451 * back onto the free list in form of a chain
452 */
Guennadi Liakhovetskifc461852010-01-19 07:24:55 +0000453 for_each_sg(sgl, sg, sg_len, i) {
454 dma_addr_t sg_addr = sg_dma_address(sg);
455 size_t len = sg_dma_len(sg);
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000456
Guennadi Liakhovetskifc461852010-01-19 07:24:55 +0000457 if (!len)
458 goto err_get_desc;
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000459
Guennadi Liakhovetskifc461852010-01-19 07:24:55 +0000460 do {
461 dev_dbg(sh_chan->dev, "Add SG #%d@%p[%d], dma %llx\n",
462 i, sg, len, (unsigned long long)sg_addr);
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000463
Guennadi Liakhovetskifc461852010-01-19 07:24:55 +0000464 if (direction == DMA_FROM_DEVICE)
465 new = sh_dmae_add_desc(sh_chan, flags,
466 &sg_addr, addr, &len, &first,
467 direction);
468 else
469 new = sh_dmae_add_desc(sh_chan, flags,
470 addr, &sg_addr, &len, &first,
471 direction);
472 if (!new)
473 goto err_get_desc;
Guennadi Liakhovetski3542a112009-12-17 09:41:39 -0700474
Guennadi Liakhovetskifc461852010-01-19 07:24:55 +0000475 new->chunks = chunks--;
476 list_add_tail(&new->node, &tx_list);
477 } while (len);
478 }
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000479
Guennadi Liakhovetski3542a112009-12-17 09:41:39 -0700480 if (new != first)
481 new->async_tx.cookie = -ENOSPC;
482
483 /* Put them back on the free list, so, they don't get lost */
484 list_splice_tail(&tx_list, &sh_chan->ld_free);
485
486 spin_unlock_bh(&sh_chan->desc_lock);
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000487
488 return &first->async_tx;
Guennadi Liakhovetskifc461852010-01-19 07:24:55 +0000489
490err_get_desc:
491 list_for_each_entry(new, &tx_list, node)
492 new->mark = DESC_IDLE;
493 list_splice(&tx_list, &sh_chan->ld_free);
494
495 spin_unlock_bh(&sh_chan->desc_lock);
496
497 return NULL;
498}
499
500static struct dma_async_tx_descriptor *sh_dmae_prep_memcpy(
501 struct dma_chan *chan, dma_addr_t dma_dest, dma_addr_t dma_src,
502 size_t len, unsigned long flags)
503{
504 struct sh_dmae_chan *sh_chan;
505 struct scatterlist sg;
506
507 if (!chan || !len)
508 return NULL;
509
Guennadi Liakhovetskicfefe992010-02-03 14:46:41 +0000510 chan->private = NULL;
511
Guennadi Liakhovetskifc461852010-01-19 07:24:55 +0000512 sh_chan = to_sh_chan(chan);
513
514 sg_init_table(&sg, 1);
515 sg_set_page(&sg, pfn_to_page(PFN_DOWN(dma_src)), len,
516 offset_in_page(dma_src));
517 sg_dma_address(&sg) = dma_src;
518 sg_dma_len(&sg) = len;
519
520 return sh_dmae_prep_sg(sh_chan, &sg, 1, &dma_dest, DMA_BIDIRECTIONAL,
521 flags);
Guennadi Liakhovetski3542a112009-12-17 09:41:39 -0700522}
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000523
Guennadi Liakhovetskicfefe992010-02-03 14:46:41 +0000524static struct dma_async_tx_descriptor *sh_dmae_prep_slave_sg(
525 struct dma_chan *chan, struct scatterlist *sgl, unsigned int sg_len,
526 enum dma_data_direction direction, unsigned long flags)
527{
528 struct sh_dmae_slave *param;
529 struct sh_dmae_chan *sh_chan;
530
531 if (!chan)
532 return NULL;
533
534 sh_chan = to_sh_chan(chan);
535 param = chan->private;
536
537 /* Someone calling slave DMA on a public channel? */
538 if (!param || !sg_len) {
539 dev_warn(sh_chan->dev, "%s: bad parameter: %p, %d, %d\n",
540 __func__, param, sg_len, param ? param->slave_id : -1);
541 return NULL;
542 }
543
544 /*
545 * if (param != NULL), this is a successfully requested slave channel,
546 * therefore param->config != NULL too.
547 */
548 return sh_dmae_prep_sg(sh_chan, sgl, sg_len, &param->config->addr,
549 direction, flags);
550}
551
552static void sh_dmae_terminate_all(struct dma_chan *chan)
553{
554 struct sh_dmae_chan *sh_chan = to_sh_chan(chan);
555
556 if (!chan)
557 return;
558
559 sh_dmae_chan_ld_cleanup(sh_chan, true);
560}
561
Guennadi Liakhovetski3542a112009-12-17 09:41:39 -0700562static dma_async_tx_callback __ld_cleanup(struct sh_dmae_chan *sh_chan, bool all)
563{
564 struct sh_desc *desc, *_desc;
565 /* Is the "exposed" head of a chain acked? */
566 bool head_acked = false;
567 dma_cookie_t cookie = 0;
568 dma_async_tx_callback callback = NULL;
569 void *param = NULL;
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000570
Guennadi Liakhovetski3542a112009-12-17 09:41:39 -0700571 spin_lock_bh(&sh_chan->desc_lock);
572 list_for_each_entry_safe(desc, _desc, &sh_chan->ld_queue, node) {
573 struct dma_async_tx_descriptor *tx = &desc->async_tx;
574
575 BUG_ON(tx->cookie > 0 && tx->cookie != desc->cookie);
576 BUG_ON(desc->mark != DESC_SUBMITTED &&
577 desc->mark != DESC_COMPLETED &&
578 desc->mark != DESC_WAITING);
579
580 /*
581 * queue is ordered, and we use this loop to (1) clean up all
582 * completed descriptors, and to (2) update descriptor flags of
583 * any chunks in a (partially) completed chain
584 */
585 if (!all && desc->mark == DESC_SUBMITTED &&
586 desc->cookie != cookie)
587 break;
588
589 if (tx->cookie > 0)
590 cookie = tx->cookie;
591
592 if (desc->mark == DESC_COMPLETED && desc->chunks == 1) {
Guennadi Liakhovetskicfefe992010-02-03 14:46:41 +0000593 if (sh_chan->completed_cookie != desc->cookie - 1)
594 dev_dbg(sh_chan->dev,
595 "Completing cookie %d, expected %d\n",
596 desc->cookie,
597 sh_chan->completed_cookie + 1);
Guennadi Liakhovetski3542a112009-12-17 09:41:39 -0700598 sh_chan->completed_cookie = desc->cookie;
599 }
600
601 /* Call callback on the last chunk */
602 if (desc->mark == DESC_COMPLETED && tx->callback) {
603 desc->mark = DESC_WAITING;
604 callback = tx->callback;
605 param = tx->callback_param;
606 dev_dbg(sh_chan->dev, "descriptor #%d@%p on %d callback\n",
607 tx->cookie, tx, sh_chan->id);
608 BUG_ON(desc->chunks != 1);
609 break;
610 }
611
612 if (tx->cookie > 0 || tx->cookie == -EBUSY) {
613 if (desc->mark == DESC_COMPLETED) {
614 BUG_ON(tx->cookie < 0);
615 desc->mark = DESC_WAITING;
616 }
617 head_acked = async_tx_test_ack(tx);
618 } else {
619 switch (desc->mark) {
620 case DESC_COMPLETED:
621 desc->mark = DESC_WAITING;
622 /* Fall through */
623 case DESC_WAITING:
624 if (head_acked)
625 async_tx_ack(&desc->async_tx);
626 }
627 }
628
629 dev_dbg(sh_chan->dev, "descriptor %p #%d completed.\n",
630 tx, tx->cookie);
631
632 if (((desc->mark == DESC_COMPLETED ||
633 desc->mark == DESC_WAITING) &&
634 async_tx_test_ack(&desc->async_tx)) || all) {
635 /* Remove from ld_queue list */
636 desc->mark = DESC_IDLE;
637 list_move(&desc->node, &sh_chan->ld_free);
638 }
639 }
640 spin_unlock_bh(&sh_chan->desc_lock);
641
642 if (callback)
643 callback(param);
644
645 return callback;
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000646}
647
648/*
649 * sh_chan_ld_cleanup - Clean up link descriptors
650 *
Guennadi Liakhovetski3542a112009-12-17 09:41:39 -0700651 * This function cleans up the ld_queue of DMA channel.
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000652 */
Guennadi Liakhovetski3542a112009-12-17 09:41:39 -0700653static void sh_dmae_chan_ld_cleanup(struct sh_dmae_chan *sh_chan, bool all)
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000654{
Guennadi Liakhovetski3542a112009-12-17 09:41:39 -0700655 while (__ld_cleanup(sh_chan, all))
656 ;
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000657}
658
659static void sh_chan_xfer_ld_queue(struct sh_dmae_chan *sh_chan)
660{
Guennadi Liakhovetski47a4dc22010-02-11 16:50:05 +0000661 struct sh_desc *desc;
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000662
Guennadi Liakhovetski3542a112009-12-17 09:41:39 -0700663 spin_lock_bh(&sh_chan->desc_lock);
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000664 /* DMA work check */
Guennadi Liakhovetski3542a112009-12-17 09:41:39 -0700665 if (dmae_is_busy(sh_chan)) {
666 spin_unlock_bh(&sh_chan->desc_lock);
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000667 return;
Guennadi Liakhovetski3542a112009-12-17 09:41:39 -0700668 }
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000669
Guennadi Liakhovetskicfefe992010-02-03 14:46:41 +0000670 /* Find the first not transferred desciptor */
Guennadi Liakhovetski47a4dc22010-02-11 16:50:05 +0000671 list_for_each_entry(desc, &sh_chan->ld_queue, node)
672 if (desc->mark == DESC_SUBMITTED) {
Guennadi Liakhovetski3542a112009-12-17 09:41:39 -0700673 /* Get the ld start address from ld_queue */
Guennadi Liakhovetski47a4dc22010-02-11 16:50:05 +0000674 dmae_set_reg(sh_chan, &desc->hw);
Guennadi Liakhovetski3542a112009-12-17 09:41:39 -0700675 dmae_start(sh_chan);
676 break;
677 }
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000678
Guennadi Liakhovetski3542a112009-12-17 09:41:39 -0700679 spin_unlock_bh(&sh_chan->desc_lock);
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000680}
681
682static void sh_dmae_memcpy_issue_pending(struct dma_chan *chan)
683{
684 struct sh_dmae_chan *sh_chan = to_sh_chan(chan);
685 sh_chan_xfer_ld_queue(sh_chan);
686}
687
688static enum dma_status sh_dmae_is_complete(struct dma_chan *chan,
689 dma_cookie_t cookie,
690 dma_cookie_t *done,
691 dma_cookie_t *used)
692{
693 struct sh_dmae_chan *sh_chan = to_sh_chan(chan);
694 dma_cookie_t last_used;
695 dma_cookie_t last_complete;
Guennadi Liakhovetski47a4dc22010-02-11 16:50:05 +0000696 enum dma_status status;
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000697
Guennadi Liakhovetski3542a112009-12-17 09:41:39 -0700698 sh_dmae_chan_ld_cleanup(sh_chan, false);
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000699
700 last_used = chan->cookie;
701 last_complete = sh_chan->completed_cookie;
Guennadi Liakhovetski3542a112009-12-17 09:41:39 -0700702 BUG_ON(last_complete < 0);
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000703
704 if (done)
705 *done = last_complete;
706
707 if (used)
708 *used = last_used;
709
Guennadi Liakhovetski47a4dc22010-02-11 16:50:05 +0000710 spin_lock_bh(&sh_chan->desc_lock);
711
712 status = dma_async_is_complete(cookie, last_complete, last_used);
713
714 /*
715 * If we don't find cookie on the queue, it has been aborted and we have
716 * to report error
717 */
718 if (status != DMA_SUCCESS) {
719 struct sh_desc *desc;
720 status = DMA_ERROR;
721 list_for_each_entry(desc, &sh_chan->ld_queue, node)
722 if (desc->cookie == cookie) {
723 status = DMA_IN_PROGRESS;
724 break;
725 }
726 }
727
728 spin_unlock_bh(&sh_chan->desc_lock);
729
730 return status;
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000731}
732
733static irqreturn_t sh_dmae_interrupt(int irq, void *data)
734{
735 irqreturn_t ret = IRQ_NONE;
736 struct sh_dmae_chan *sh_chan = (struct sh_dmae_chan *)data;
737 u32 chcr = sh_dmae_readl(sh_chan, CHCR);
738
739 if (chcr & CHCR_TE) {
740 /* DMA stop */
741 dmae_halt(sh_chan);
742
743 ret = IRQ_HANDLED;
744 tasklet_schedule(&sh_chan->tasklet);
745 }
746
747 return ret;
748}
749
750#if defined(CONFIG_CPU_SH4)
751static irqreturn_t sh_dmae_err(int irq, void *data)
752{
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000753 struct sh_dmae_device *shdev = (struct sh_dmae_device *)data;
Guennadi Liakhovetski47a4dc22010-02-11 16:50:05 +0000754 int i;
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000755
Guennadi Liakhovetski47a4dc22010-02-11 16:50:05 +0000756 /* halt the dma controller */
Guennadi Liakhovetski027811b2010-02-11 16:50:10 +0000757 sh_dmae_ctl_stop(shdev);
Guennadi Liakhovetski47a4dc22010-02-11 16:50:05 +0000758
759 /* We cannot detect, which channel caused the error, have to reset all */
760 for (i = 0; i < MAX_DMA_CHANNELS; i++) {
761 struct sh_dmae_chan *sh_chan = shdev->chan[i];
762 if (sh_chan) {
763 struct sh_desc *desc;
764 /* Stop the channel */
765 dmae_halt(sh_chan);
766 /* Complete all */
767 list_for_each_entry(desc, &sh_chan->ld_queue, node) {
768 struct dma_async_tx_descriptor *tx = &desc->async_tx;
769 desc->mark = DESC_IDLE;
770 if (tx->callback)
771 tx->callback(tx->callback_param);
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000772 }
Guennadi Liakhovetski47a4dc22010-02-11 16:50:05 +0000773 list_splice_init(&sh_chan->ld_queue, &sh_chan->ld_free);
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000774 }
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000775 }
Guennadi Liakhovetski027811b2010-02-11 16:50:10 +0000776 sh_dmae_rst(shdev);
Guennadi Liakhovetski47a4dc22010-02-11 16:50:05 +0000777
778 return IRQ_HANDLED;
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000779}
780#endif
781
782static void dmae_do_tasklet(unsigned long data)
783{
784 struct sh_dmae_chan *sh_chan = (struct sh_dmae_chan *)data;
Guennadi Liakhovetski3542a112009-12-17 09:41:39 -0700785 struct sh_desc *desc;
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000786 u32 sar_buf = sh_dmae_readl(sh_chan, SAR);
Guennadi Liakhovetskicfefe992010-02-03 14:46:41 +0000787 u32 dar_buf = sh_dmae_readl(sh_chan, DAR);
Guennadi Liakhovetski86d61b32009-12-10 18:35:07 +0100788
Guennadi Liakhovetski3542a112009-12-17 09:41:39 -0700789 spin_lock(&sh_chan->desc_lock);
790 list_for_each_entry(desc, &sh_chan->ld_queue, node) {
Guennadi Liakhovetskicfefe992010-02-03 14:46:41 +0000791 if (desc->mark == DESC_SUBMITTED &&
792 ((desc->direction == DMA_FROM_DEVICE &&
793 (desc->hw.dar + desc->hw.tcr) == dar_buf) ||
794 (desc->hw.sar + desc->hw.tcr) == sar_buf)) {
Guennadi Liakhovetski3542a112009-12-17 09:41:39 -0700795 dev_dbg(sh_chan->dev, "done #%d@%p dst %u\n",
796 desc->async_tx.cookie, &desc->async_tx,
797 desc->hw.dar);
798 desc->mark = DESC_COMPLETED;
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000799 break;
800 }
801 }
Guennadi Liakhovetski3542a112009-12-17 09:41:39 -0700802 spin_unlock(&sh_chan->desc_lock);
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000803
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000804 /* Next desc */
805 sh_chan_xfer_ld_queue(sh_chan);
Guennadi Liakhovetski3542a112009-12-17 09:41:39 -0700806 sh_dmae_chan_ld_cleanup(sh_chan, false);
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000807}
808
Guennadi Liakhovetski027811b2010-02-11 16:50:10 +0000809static int __devinit sh_dmae_chan_probe(struct sh_dmae_device *shdev, int id,
810 int irq, unsigned long flags)
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000811{
812 int err;
Guennadi Liakhovetski027811b2010-02-11 16:50:10 +0000813 struct sh_dmae_channel *chan_pdata = &shdev->pdata->channel[id];
814 struct platform_device *pdev = to_platform_device(shdev->common.dev);
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000815 struct sh_dmae_chan *new_sh_chan;
816
817 /* alloc channel */
818 new_sh_chan = kzalloc(sizeof(struct sh_dmae_chan), GFP_KERNEL);
819 if (!new_sh_chan) {
Guennadi Liakhovetski86d61b32009-12-10 18:35:07 +0100820 dev_err(shdev->common.dev,
821 "No free memory for allocating dma channels!\n");
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000822 return -ENOMEM;
823 }
824
825 new_sh_chan->dev = shdev->common.dev;
826 new_sh_chan->id = id;
Guennadi Liakhovetski027811b2010-02-11 16:50:10 +0000827 new_sh_chan->irq = irq;
828 new_sh_chan->base = shdev->chan_reg + chan_pdata->offset / sizeof(u32);
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000829
830 /* Init DMA tasklet */
831 tasklet_init(&new_sh_chan->tasklet, dmae_do_tasklet,
832 (unsigned long)new_sh_chan);
833
834 /* Init the channel */
835 dmae_init(new_sh_chan);
836
837 spin_lock_init(&new_sh_chan->desc_lock);
838
839 /* Init descripter manage list */
840 INIT_LIST_HEAD(&new_sh_chan->ld_queue);
841 INIT_LIST_HEAD(&new_sh_chan->ld_free);
842
843 /* copy struct dma_device */
844 new_sh_chan->common.device = &shdev->common;
845
846 /* Add the channel to DMA device channel list */
847 list_add_tail(&new_sh_chan->common.device_node,
848 &shdev->common.channels);
849 shdev->common.chancnt++;
850
Guennadi Liakhovetski027811b2010-02-11 16:50:10 +0000851 if (pdev->id >= 0)
852 snprintf(new_sh_chan->dev_id, sizeof(new_sh_chan->dev_id),
853 "sh-dmae%d.%d", pdev->id, new_sh_chan->id);
854 else
855 snprintf(new_sh_chan->dev_id, sizeof(new_sh_chan->dev_id),
856 "sh-dma%d", new_sh_chan->id);
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000857
858 /* set up channel irq */
Guennadi Liakhovetski027811b2010-02-11 16:50:10 +0000859 err = request_irq(irq, &sh_dmae_interrupt, flags,
Guennadi Liakhovetski86d61b32009-12-10 18:35:07 +0100860 new_sh_chan->dev_id, new_sh_chan);
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000861 if (err) {
862 dev_err(shdev->common.dev, "DMA channel %d request_irq error "
863 "with return %d\n", id, err);
864 goto err_no_irq;
865 }
866
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000867 shdev->chan[id] = new_sh_chan;
868 return 0;
869
870err_no_irq:
871 /* remove from dmaengine device node */
872 list_del(&new_sh_chan->common.device_node);
873 kfree(new_sh_chan);
874 return err;
875}
876
877static void sh_dmae_chan_remove(struct sh_dmae_device *shdev)
878{
879 int i;
880
881 for (i = shdev->common.chancnt - 1 ; i >= 0 ; i--) {
882 if (shdev->chan[i]) {
Guennadi Liakhovetski027811b2010-02-11 16:50:10 +0000883 struct sh_dmae_chan *sh_chan = shdev->chan[i];
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000884
Guennadi Liakhovetski027811b2010-02-11 16:50:10 +0000885 free_irq(sh_chan->irq, sh_chan);
886
887 list_del(&sh_chan->common.device_node);
888 kfree(sh_chan);
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000889 shdev->chan[i] = NULL;
890 }
891 }
892 shdev->common.chancnt = 0;
893}
894
895static int __init sh_dmae_probe(struct platform_device *pdev)
896{
Guennadi Liakhovetski027811b2010-02-11 16:50:10 +0000897 struct sh_dmae_pdata *pdata = pdev->dev.platform_data;
898 unsigned long irqflags = IRQF_DISABLED,
899 chan_flag[MAX_DMA_CHANNELS] = {};
900 int errirq, chan_irq[MAX_DMA_CHANNELS];
901 int err, i, irq_cnt = 0, irqres = 0;
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000902 struct sh_dmae_device *shdev;
Guennadi Liakhovetski027811b2010-02-11 16:50:10 +0000903 struct resource *chan, *dmars, *errirq_res, *chanirq_res;
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000904
Dan Williams56adf7e2009-11-22 12:10:10 -0700905 /* get platform data */
Guennadi Liakhovetski027811b2010-02-11 16:50:10 +0000906 if (!pdata || !pdata->channel_num)
Dan Williams56adf7e2009-11-22 12:10:10 -0700907 return -ENODEV;
908
Guennadi Liakhovetski027811b2010-02-11 16:50:10 +0000909 chan = platform_get_resource(pdev, IORESOURCE_MEM, 0);
910 /* DMARS area is optional, if absent, this controller cannot do slave DMA */
911 dmars = platform_get_resource(pdev, IORESOURCE_MEM, 1);
912 /*
913 * IRQ resources:
914 * 1. there always must be at least one IRQ IO-resource. On SH4 it is
915 * the error IRQ, in which case it is the only IRQ in this resource:
916 * start == end. If it is the only IRQ resource, all channels also
917 * use the same IRQ.
918 * 2. DMA channel IRQ resources can be specified one per resource or in
919 * ranges (start != end)
920 * 3. iff all events (channels and, optionally, error) on this
921 * controller use the same IRQ, only one IRQ resource can be
922 * specified, otherwise there must be one IRQ per channel, even if
923 * some of them are equal
924 * 4. if all IRQs on this controller are equal or if some specific IRQs
925 * specify IORESOURCE_IRQ_SHAREABLE in their resources, they will be
926 * requested with the IRQF_SHARED flag
927 */
928 errirq_res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
929 if (!chan || !errirq_res)
930 return -ENODEV;
931
932 if (!request_mem_region(chan->start, resource_size(chan), pdev->name)) {
933 dev_err(&pdev->dev, "DMAC register region already claimed\n");
934 return -EBUSY;
935 }
936
937 if (dmars && !request_mem_region(dmars->start, resource_size(dmars), pdev->name)) {
938 dev_err(&pdev->dev, "DMAC DMARS region already claimed\n");
939 err = -EBUSY;
940 goto ermrdmars;
941 }
942
943 err = -ENOMEM;
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000944 shdev = kzalloc(sizeof(struct sh_dmae_device), GFP_KERNEL);
945 if (!shdev) {
Guennadi Liakhovetski027811b2010-02-11 16:50:10 +0000946 dev_err(&pdev->dev, "Not enough memory\n");
947 goto ealloc;
948 }
949
950 shdev->chan_reg = ioremap(chan->start, resource_size(chan));
951 if (!shdev->chan_reg)
952 goto emapchan;
953 if (dmars) {
954 shdev->dmars = ioremap(dmars->start, resource_size(dmars));
955 if (!shdev->dmars)
956 goto emapdmars;
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000957 }
958
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000959 /* platform data */
Guennadi Liakhovetski027811b2010-02-11 16:50:10 +0000960 shdev->pdata = pdata;
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000961
962 /* reset dma controller */
Guennadi Liakhovetski027811b2010-02-11 16:50:10 +0000963 err = sh_dmae_rst(shdev);
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000964 if (err)
965 goto rst_err;
966
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000967 INIT_LIST_HEAD(&shdev->common.channels);
968
969 dma_cap_set(DMA_MEMCPY, shdev->common.cap_mask);
Guennadi Liakhovetski027811b2010-02-11 16:50:10 +0000970 if (dmars)
971 dma_cap_set(DMA_SLAVE, shdev->common.cap_mask);
Guennadi Liakhovetskicfefe992010-02-03 14:46:41 +0000972
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000973 shdev->common.device_alloc_chan_resources
974 = sh_dmae_alloc_chan_resources;
975 shdev->common.device_free_chan_resources = sh_dmae_free_chan_resources;
976 shdev->common.device_prep_dma_memcpy = sh_dmae_prep_memcpy;
977 shdev->common.device_is_tx_complete = sh_dmae_is_complete;
978 shdev->common.device_issue_pending = sh_dmae_memcpy_issue_pending;
Guennadi Liakhovetskicfefe992010-02-03 14:46:41 +0000979
980 /* Compulsory for DMA_SLAVE fields */
981 shdev->common.device_prep_slave_sg = sh_dmae_prep_slave_sg;
982 shdev->common.device_terminate_all = sh_dmae_terminate_all;
983
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000984 shdev->common.dev = &pdev->dev;
Guennadi Liakhovetskiddb4f0f2009-12-04 19:44:41 +0100985 /* Default transfer size of 32 bytes requires 32-byte alignment */
986 shdev->common.copy_align = 5;
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000987
988#if defined(CONFIG_CPU_SH4)
Guennadi Liakhovetski027811b2010-02-11 16:50:10 +0000989 chanirq_res = platform_get_resource(pdev, IORESOURCE_IRQ, 1);
990
991 if (!chanirq_res)
992 chanirq_res = errirq_res;
993 else
994 irqres++;
995
996 if (chanirq_res == errirq_res ||
997 (errirq_res->flags & IORESOURCE_BITS) == IORESOURCE_IRQ_SHAREABLE)
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000998 irqflags = IRQF_SHARED;
Guennadi Liakhovetski027811b2010-02-11 16:50:10 +0000999
1000 errirq = errirq_res->start;
1001
1002 err = request_irq(errirq, sh_dmae_err, irqflags,
1003 "DMAC Address Error", shdev);
1004 if (err) {
1005 dev_err(&pdev->dev,
1006 "DMA failed requesting irq #%d, error %d\n",
1007 errirq, err);
1008 goto eirq_err;
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +00001009 }
1010
Guennadi Liakhovetski027811b2010-02-11 16:50:10 +00001011#else
1012 chanirq_res = errirq_res;
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +00001013#endif /* CONFIG_CPU_SH4 */
1014
Guennadi Liakhovetski027811b2010-02-11 16:50:10 +00001015 if (chanirq_res->start == chanirq_res->end &&
1016 !platform_get_resource(pdev, IORESOURCE_IRQ, 1)) {
1017 /* Special case - all multiplexed */
1018 for (; irq_cnt < pdata->channel_num; irq_cnt++) {
1019 chan_irq[irq_cnt] = chanirq_res->start;
1020 chan_flag[irq_cnt] = IRQF_SHARED;
1021 }
1022 } else {
1023 do {
1024 for (i = chanirq_res->start; i <= chanirq_res->end; i++) {
1025 if ((errirq_res->flags & IORESOURCE_BITS) ==
1026 IORESOURCE_IRQ_SHAREABLE)
1027 chan_flag[irq_cnt] = IRQF_SHARED;
1028 else
1029 chan_flag[irq_cnt] = IRQF_DISABLED;
1030 dev_dbg(&pdev->dev,
1031 "Found IRQ %d for channel %d\n",
1032 i, irq_cnt);
1033 chan_irq[irq_cnt++] = i;
1034 }
1035 chanirq_res = platform_get_resource(pdev,
1036 IORESOURCE_IRQ, ++irqres);
1037 } while (irq_cnt < pdata->channel_num && chanirq_res);
1038 }
1039
1040 if (irq_cnt < pdata->channel_num)
1041 goto eirqres;
1042
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +00001043 /* Create DMA Channel */
Guennadi Liakhovetski027811b2010-02-11 16:50:10 +00001044 for (i = 0; i < pdata->channel_num; i++) {
1045 err = sh_dmae_chan_probe(shdev, i, chan_irq[i], chan_flag[i]);
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +00001046 if (err)
1047 goto chan_probe_err;
1048 }
1049
1050 platform_set_drvdata(pdev, shdev);
1051 dma_async_device_register(&shdev->common);
1052
1053 return err;
1054
1055chan_probe_err:
1056 sh_dmae_chan_remove(shdev);
Guennadi Liakhovetski027811b2010-02-11 16:50:10 +00001057eirqres:
1058#if defined(CONFIG_CPU_SH4)
1059 free_irq(errirq, shdev);
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +00001060eirq_err:
Guennadi Liakhovetski027811b2010-02-11 16:50:10 +00001061#endif
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +00001062rst_err:
Guennadi Liakhovetski027811b2010-02-11 16:50:10 +00001063 if (dmars)
1064 iounmap(shdev->dmars);
1065emapdmars:
1066 iounmap(shdev->chan_reg);
1067emapchan:
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +00001068 kfree(shdev);
Guennadi Liakhovetski027811b2010-02-11 16:50:10 +00001069ealloc:
1070 if (dmars)
1071 release_mem_region(dmars->start, resource_size(dmars));
1072ermrdmars:
1073 release_mem_region(chan->start, resource_size(chan));
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +00001074
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +00001075 return err;
1076}
1077
1078static int __exit sh_dmae_remove(struct platform_device *pdev)
1079{
1080 struct sh_dmae_device *shdev = platform_get_drvdata(pdev);
Guennadi Liakhovetski027811b2010-02-11 16:50:10 +00001081 struct resource *res;
1082 int errirq = platform_get_irq(pdev, 0);
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +00001083
1084 dma_async_device_unregister(&shdev->common);
1085
Guennadi Liakhovetski027811b2010-02-11 16:50:10 +00001086 if (errirq > 0)
1087 free_irq(errirq, shdev);
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +00001088
1089 /* channel data remove */
1090 sh_dmae_chan_remove(shdev);
1091
Guennadi Liakhovetski027811b2010-02-11 16:50:10 +00001092 if (shdev->dmars)
1093 iounmap(shdev->dmars);
1094 iounmap(shdev->chan_reg);
1095
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +00001096 kfree(shdev);
1097
Guennadi Liakhovetski027811b2010-02-11 16:50:10 +00001098 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1099 if (res)
1100 release_mem_region(res->start, resource_size(res));
1101 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1102 if (res)
1103 release_mem_region(res->start, resource_size(res));
1104
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +00001105 return 0;
1106}
1107
1108static void sh_dmae_shutdown(struct platform_device *pdev)
1109{
1110 struct sh_dmae_device *shdev = platform_get_drvdata(pdev);
Guennadi Liakhovetski027811b2010-02-11 16:50:10 +00001111 sh_dmae_ctl_stop(shdev);
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +00001112}
1113
1114static struct platform_driver sh_dmae_driver = {
1115 .remove = __exit_p(sh_dmae_remove),
1116 .shutdown = sh_dmae_shutdown,
1117 .driver = {
1118 .name = "sh-dma-engine",
1119 },
1120};
1121
1122static int __init sh_dmae_init(void)
1123{
1124 return platform_driver_probe(&sh_dmae_driver, sh_dmae_probe);
1125}
1126module_init(sh_dmae_init);
1127
1128static void __exit sh_dmae_exit(void)
1129{
1130 platform_driver_unregister(&sh_dmae_driver);
1131}
1132module_exit(sh_dmae_exit);
1133
1134MODULE_AUTHOR("Nobuhiro Iwamatsu <iwamatsu.nobuhiro@renesas.com>");
1135MODULE_DESCRIPTION("Renesas SH DMA Engine driver");
1136MODULE_LICENSE("GPL");