blob: 812fd76e9c18e4b2b210f20125324560a873ddeb [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090022#include <linux/slab.h>
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +000023#include <linux/interrupt.h>
24#include <linux/dmaengine.h>
25#include <linux/delay.h>
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +000026#include <linux/platform_device.h>
Guennadi Liakhovetski20f2a3b2010-02-11 16:50:18 +000027#include <linux/pm_runtime.h>
Magnus Dammb2623a62010-03-19 04:47:10 +000028#include <linux/sh_dma.h>
Paul Mundt03aa18f2010-12-17 19:16:10 +090029#include <linux/notifier.h>
30#include <linux/kdebug.h>
31#include <linux/spinlock.h>
32#include <linux/rculist.h>
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +000033#include "shdma.h"
34
35/* DMA descriptor control */
Guennadi Liakhovetski3542a112009-12-17 09:41:39 -070036enum sh_dmae_desc_status {
37 DESC_IDLE,
38 DESC_PREPARED,
39 DESC_SUBMITTED,
40 DESC_COMPLETED, /* completed, have to call callback */
41 DESC_WAITING, /* callback called, waiting for ack / re-submit */
42};
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +000043
44#define NR_DESCS_PER_CHANNEL 32
Guennadi Liakhovetski8b1935e2010-02-11 16:50:14 +000045/* Default MEMCPY transfer size = 2^2 = 4 bytes */
46#define LOG2_DEFAULT_XFER_SIZE 2
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +000047
Paul Mundt03aa18f2010-12-17 19:16:10 +090048/*
49 * Used for write-side mutual exclusion for the global device list,
Guennadi Liakhovetski2dc66662011-04-29 17:09:21 +000050 * read-side synchronization by way of RCU, and per-controller data.
Paul Mundt03aa18f2010-12-17 19:16:10 +090051 */
52static DEFINE_SPINLOCK(sh_dmae_lock);
53static LIST_HEAD(sh_dmae_devices);
54
Guennadi Liakhovetskicfefe992010-02-03 14:46:41 +000055/* A bitmask with bits enough for enum sh_dmae_slave_chan_id */
Magnus Damm02ca5082010-03-19 04:46:47 +000056static unsigned long sh_dmae_slave_used[BITS_TO_LONGS(SH_DMA_SLAVE_NUMBER)];
Guennadi Liakhovetskicfefe992010-02-03 14:46:41 +000057
Guennadi Liakhovetski3542a112009-12-17 09:41:39 -070058static void sh_dmae_chan_ld_cleanup(struct sh_dmae_chan *sh_chan, bool all);
Guennadi Liakhovetskic11b46c2012-01-04 15:34:17 +010059static void sh_chan_xfer_ld_queue(struct sh_dmae_chan *sh_chan);
60
61static void chclr_write(struct sh_dmae_chan *sh_dc, u32 data)
62{
63 struct sh_dmae_device *shdev = to_sh_dev(sh_dc);
64
65 __raw_writel(data, shdev->chan_reg +
66 shdev->pdata->channel[sh_dc->id].chclr_offset);
67}
Guennadi Liakhovetski3542a112009-12-17 09:41:39 -070068
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +000069static void sh_dmae_writel(struct sh_dmae_chan *sh_dc, u32 data, u32 reg)
70{
Guennadi Liakhovetski027811b2010-02-11 16:50:10 +000071 __raw_writel(data, sh_dc->base + reg / sizeof(u32));
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +000072}
73
74static u32 sh_dmae_readl(struct sh_dmae_chan *sh_dc, u32 reg)
75{
Guennadi Liakhovetski027811b2010-02-11 16:50:10 +000076 return __raw_readl(sh_dc->base + reg / sizeof(u32));
77}
78
79static u16 dmaor_read(struct sh_dmae_device *shdev)
80{
Kuninori Morimotoe76c3af2011-06-17 08:20:56 +000081 u32 __iomem *addr = shdev->chan_reg + DMAOR / sizeof(u32);
82
83 if (shdev->pdata->dmaor_is_32bit)
84 return __raw_readl(addr);
85 else
86 return __raw_readw(addr);
Guennadi Liakhovetski027811b2010-02-11 16:50:10 +000087}
88
89static void dmaor_write(struct sh_dmae_device *shdev, u16 data)
90{
Kuninori Morimotoe76c3af2011-06-17 08:20:56 +000091 u32 __iomem *addr = shdev->chan_reg + DMAOR / sizeof(u32);
92
93 if (shdev->pdata->dmaor_is_32bit)
94 __raw_writel(data, addr);
95 else
96 __raw_writew(data, addr);
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +000097}
98
Kuninori Morimoto5899a722011-06-17 08:20:40 +000099static void chcr_write(struct sh_dmae_chan *sh_dc, u32 data)
100{
101 struct sh_dmae_device *shdev = to_sh_dev(sh_dc);
102
103 __raw_writel(data, sh_dc->base + shdev->chcr_offset / sizeof(u32));
104}
105
106static u32 chcr_read(struct sh_dmae_chan *sh_dc)
107{
108 struct sh_dmae_device *shdev = to_sh_dev(sh_dc);
109
110 return __raw_readl(sh_dc->base + shdev->chcr_offset / sizeof(u32));
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000111}
112
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000113/*
114 * Reset DMA controller
115 *
116 * SH7780 has two DMAOR register
117 */
Guennadi Liakhovetski027811b2010-02-11 16:50:10 +0000118static void sh_dmae_ctl_stop(struct sh_dmae_device *shdev)
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000119{
Guennadi Liakhovetski2dc66662011-04-29 17:09:21 +0000120 unsigned short dmaor;
121 unsigned long flags;
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000122
Guennadi Liakhovetski2dc66662011-04-29 17:09:21 +0000123 spin_lock_irqsave(&sh_dmae_lock, flags);
124
125 dmaor = dmaor_read(shdev);
Guennadi Liakhovetski027811b2010-02-11 16:50:10 +0000126 dmaor_write(shdev, dmaor & ~(DMAOR_NMIF | DMAOR_AE | DMAOR_DME));
Guennadi Liakhovetski2dc66662011-04-29 17:09:21 +0000127
128 spin_unlock_irqrestore(&sh_dmae_lock, flags);
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000129}
130
Guennadi Liakhovetski027811b2010-02-11 16:50:10 +0000131static int sh_dmae_rst(struct sh_dmae_device *shdev)
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000132{
133 unsigned short dmaor;
Guennadi Liakhovetski2dc66662011-04-29 17:09:21 +0000134 unsigned long flags;
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000135
Guennadi Liakhovetski2dc66662011-04-29 17:09:21 +0000136 spin_lock_irqsave(&sh_dmae_lock, flags);
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000137
Guennadi Liakhovetski2dc66662011-04-29 17:09:21 +0000138 dmaor = dmaor_read(shdev) & ~(DMAOR_NMIF | DMAOR_AE | DMAOR_DME);
139
Guennadi Liakhovetskic11b46c2012-01-04 15:34:17 +0100140 if (shdev->pdata->chclr_present) {
141 int i;
142 for (i = 0; i < shdev->pdata->channel_num; i++) {
143 struct sh_dmae_chan *sh_chan = shdev->chan[i];
144 if (sh_chan)
145 chclr_write(sh_chan, 0);
146 }
147 }
148
Guennadi Liakhovetski2dc66662011-04-29 17:09:21 +0000149 dmaor_write(shdev, dmaor | shdev->pdata->dmaor_init);
150
151 dmaor = dmaor_read(shdev);
152
153 spin_unlock_irqrestore(&sh_dmae_lock, flags);
154
155 if (dmaor & (DMAOR_AE | DMAOR_NMIF)) {
156 dev_warn(shdev->common.dev, "Can't initialize DMAOR.\n");
157 return -EIO;
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000158 }
Guennadi Liakhovetskic11b46c2012-01-04 15:34:17 +0100159 if (shdev->pdata->dmaor_init & ~dmaor)
160 dev_warn(shdev->common.dev,
161 "DMAOR=0x%x hasn't latched the initial value 0x%x.\n",
162 dmaor, shdev->pdata->dmaor_init);
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000163 return 0;
164}
165
Guennadi Liakhovetskifc461852010-01-19 07:24:55 +0000166static bool dmae_is_busy(struct sh_dmae_chan *sh_chan)
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000167{
Kuninori Morimoto5899a722011-06-17 08:20:40 +0000168 u32 chcr = chcr_read(sh_chan);
Guennadi Liakhovetskifc461852010-01-19 07:24:55 +0000169
170 if ((chcr & (CHCR_DE | CHCR_TE)) == CHCR_DE)
171 return true; /* working */
172
173 return false; /* waiting */
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000174}
175
Guennadi Liakhovetski8b1935e2010-02-11 16:50:14 +0000176static unsigned int calc_xmit_shift(struct sh_dmae_chan *sh_chan, u32 chcr)
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000177{
Kuninori Morimotoc4e0dd72011-06-16 05:08:09 +0000178 struct sh_dmae_device *shdev = to_sh_dev(sh_chan);
Guennadi Liakhovetski8b1935e2010-02-11 16:50:14 +0000179 struct sh_dmae_pdata *pdata = shdev->pdata;
180 int cnt = ((chcr & pdata->ts_low_mask) >> pdata->ts_low_shift) |
181 ((chcr & pdata->ts_high_mask) >> pdata->ts_high_shift);
Guennadi Liakhovetski623b4ac2010-02-03 14:44:12 +0000182
Guennadi Liakhovetski8b1935e2010-02-11 16:50:14 +0000183 if (cnt >= pdata->ts_shift_num)
184 cnt = 0;
185
186 return pdata->ts_shift[cnt];
187}
188
189static u32 log2size_to_chcr(struct sh_dmae_chan *sh_chan, int l2size)
190{
Kuninori Morimotoc4e0dd72011-06-16 05:08:09 +0000191 struct sh_dmae_device *shdev = to_sh_dev(sh_chan);
Guennadi Liakhovetski8b1935e2010-02-11 16:50:14 +0000192 struct sh_dmae_pdata *pdata = shdev->pdata;
193 int i;
194
195 for (i = 0; i < pdata->ts_shift_num; i++)
196 if (pdata->ts_shift[i] == l2size)
197 break;
198
199 if (i == pdata->ts_shift_num)
200 i = 0;
201
202 return ((i << pdata->ts_low_shift) & pdata->ts_low_mask) |
203 ((i << pdata->ts_high_shift) & pdata->ts_high_mask);
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000204}
205
Guennadi Liakhovetski3542a112009-12-17 09:41:39 -0700206static void dmae_set_reg(struct sh_dmae_chan *sh_chan, struct sh_dmae_regs *hw)
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000207{
Guennadi Liakhovetski3542a112009-12-17 09:41:39 -0700208 sh_dmae_writel(sh_chan, hw->sar, SAR);
209 sh_dmae_writel(sh_chan, hw->dar, DAR);
Guennadi Liakhovetskicfefe992010-02-03 14:46:41 +0000210 sh_dmae_writel(sh_chan, hw->tcr >> sh_chan->xmit_shift, TCR);
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000211}
212
213static void dmae_start(struct sh_dmae_chan *sh_chan)
214{
Kuninori Morimoto67c62692011-06-17 08:20:51 +0000215 struct sh_dmae_device *shdev = to_sh_dev(sh_chan);
Kuninori Morimoto5899a722011-06-17 08:20:40 +0000216 u32 chcr = chcr_read(sh_chan);
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000217
Kuninori Morimoto260bf2c2011-06-17 08:21:05 +0000218 if (shdev->pdata->needs_tend_set)
219 sh_dmae_writel(sh_chan, 0xFFFFFFFF, TEND);
220
Kuninori Morimoto67c62692011-06-17 08:20:51 +0000221 chcr |= CHCR_DE | shdev->chcr_ie_bit;
Kuninori Morimoto5899a722011-06-17 08:20:40 +0000222 chcr_write(sh_chan, chcr & ~CHCR_TE);
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000223}
224
225static void dmae_halt(struct sh_dmae_chan *sh_chan)
226{
Kuninori Morimoto67c62692011-06-17 08:20:51 +0000227 struct sh_dmae_device *shdev = to_sh_dev(sh_chan);
Kuninori Morimoto5899a722011-06-17 08:20:40 +0000228 u32 chcr = chcr_read(sh_chan);
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000229
Kuninori Morimoto67c62692011-06-17 08:20:51 +0000230 chcr &= ~(CHCR_DE | CHCR_TE | shdev->chcr_ie_bit);
Kuninori Morimoto5899a722011-06-17 08:20:40 +0000231 chcr_write(sh_chan, chcr);
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000232}
233
Guennadi Liakhovetskicfefe992010-02-03 14:46:41 +0000234static void dmae_init(struct sh_dmae_chan *sh_chan)
235{
Guennadi Liakhovetski8b1935e2010-02-11 16:50:14 +0000236 /*
237 * Default configuration for dual address memory-memory transfer.
238 * 0x400 represents auto-request.
239 */
240 u32 chcr = DM_INC | SM_INC | 0x400 | log2size_to_chcr(sh_chan,
241 LOG2_DEFAULT_XFER_SIZE);
242 sh_chan->xmit_shift = calc_xmit_shift(sh_chan, chcr);
Kuninori Morimoto5899a722011-06-17 08:20:40 +0000243 chcr_write(sh_chan, chcr);
Guennadi Liakhovetskicfefe992010-02-03 14:46:41 +0000244}
245
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000246static int dmae_set_chcr(struct sh_dmae_chan *sh_chan, u32 val)
247{
Guennadi Liakhovetski2dc66662011-04-29 17:09:21 +0000248 /* If DMA is active, cannot set CHCR. TODO: remove this superfluous check */
Guennadi Liakhovetskifc461852010-01-19 07:24:55 +0000249 if (dmae_is_busy(sh_chan))
250 return -EBUSY;
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000251
Guennadi Liakhovetski8b1935e2010-02-11 16:50:14 +0000252 sh_chan->xmit_shift = calc_xmit_shift(sh_chan, val);
Kuninori Morimoto5899a722011-06-17 08:20:40 +0000253 chcr_write(sh_chan, val);
Guennadi Liakhovetskicfefe992010-02-03 14:46:41 +0000254
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000255 return 0;
256}
257
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000258static int dmae_set_dmars(struct sh_dmae_chan *sh_chan, u16 val)
259{
Kuninori Morimotoc4e0dd72011-06-16 05:08:09 +0000260 struct sh_dmae_device *shdev = to_sh_dev(sh_chan);
Guennadi Liakhovetski027811b2010-02-11 16:50:10 +0000261 struct sh_dmae_pdata *pdata = shdev->pdata;
Guennadi Liakhovetski5bac9422010-04-21 15:36:49 +0000262 const struct sh_dmae_channel *chan_pdata = &pdata->channel[sh_chan->id];
Magnus Damm26fc02a2011-05-24 10:31:12 +0000263 u16 __iomem *addr = shdev->dmars;
Kuninori Morimoto090b9182011-06-16 05:08:28 +0000264 unsigned int shift = chan_pdata->dmars_bit;
Guennadi Liakhovetskifc461852010-01-19 07:24:55 +0000265
266 if (dmae_is_busy(sh_chan))
267 return -EBUSY;
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000268
Kuninori Morimoto260bf2c2011-06-17 08:21:05 +0000269 if (pdata->no_dmars)
270 return 0;
271
Magnus Damm26fc02a2011-05-24 10:31:12 +0000272 /* in the case of a missing DMARS resource use first memory window */
273 if (!addr)
274 addr = (u16 __iomem *)shdev->chan_reg;
275 addr += chan_pdata->dmars / sizeof(u16);
276
Guennadi Liakhovetski027811b2010-02-11 16:50:10 +0000277 __raw_writew((__raw_readw(addr) & (0xff00 >> shift)) | (val << shift),
278 addr);
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000279
280 return 0;
281}
282
283static dma_cookie_t sh_dmae_tx_submit(struct dma_async_tx_descriptor *tx)
284{
Guennadi Liakhovetski3542a112009-12-17 09:41:39 -0700285 struct sh_desc *desc = tx_to_sh_desc(tx), *chunk, *last = desc, *c;
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000286 struct sh_dmae_chan *sh_chan = to_sh_chan(tx->chan);
Guennadi Liakhovetski7a1cd9a2011-08-18 16:55:27 +0200287 struct sh_dmae_slave *param = tx->chan->private;
Guennadi Liakhovetski3542a112009-12-17 09:41:39 -0700288 dma_async_tx_callback callback = tx->callback;
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000289 dma_cookie_t cookie;
Guennadi Liakhovetski7a1cd9a2011-08-18 16:55:27 +0200290 bool power_up;
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000291
Guennadi Liakhovetski7a1cd9a2011-08-18 16:55:27 +0200292 spin_lock_irq(&sh_chan->desc_lock);
293
294 if (list_empty(&sh_chan->ld_queue))
295 power_up = true;
296 else
297 power_up = false;
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000298
299 cookie = sh_chan->common.cookie;
300 cookie++;
301 if (cookie < 0)
302 cookie = 1;
303
Guennadi Liakhovetski3542a112009-12-17 09:41:39 -0700304 sh_chan->common.cookie = cookie;
305 tx->cookie = cookie;
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000306
Guennadi Liakhovetski3542a112009-12-17 09:41:39 -0700307 /* Mark all chunks of this descriptor as submitted, move to the queue */
308 list_for_each_entry_safe(chunk, c, desc->node.prev, node) {
309 /*
310 * All chunks are on the global ld_free, so, we have to find
311 * the end of the chain ourselves
312 */
313 if (chunk != desc && (chunk->mark == DESC_IDLE ||
314 chunk->async_tx.cookie > 0 ||
315 chunk->async_tx.cookie == -EBUSY ||
316 &chunk->node == &sh_chan->ld_free))
317 break;
318 chunk->mark = DESC_SUBMITTED;
319 /* Callback goes to the last chunk */
320 chunk->async_tx.callback = NULL;
321 chunk->cookie = cookie;
322 list_move_tail(&chunk->node, &sh_chan->ld_queue);
323 last = chunk;
324 }
325
326 last->async_tx.callback = callback;
327 last->async_tx.callback_param = tx->callback_param;
328
329 dev_dbg(sh_chan->dev, "submit #%d@%p on %d: %x[%d] -> %x\n",
330 tx->cookie, &last->async_tx, sh_chan->id,
331 desc->hw.sar, desc->hw.tcr, desc->hw.dar);
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000332
Guennadi Liakhovetski7a1cd9a2011-08-18 16:55:27 +0200333 if (power_up) {
334 sh_chan->pm_state = DMAE_PM_BUSY;
335
336 pm_runtime_get(sh_chan->dev);
337
338 spin_unlock_irq(&sh_chan->desc_lock);
339
340 pm_runtime_barrier(sh_chan->dev);
341
342 spin_lock_irq(&sh_chan->desc_lock);
343
344 /* Have we been reset, while waiting? */
345 if (sh_chan->pm_state != DMAE_PM_ESTABLISHED) {
346 dev_dbg(sh_chan->dev, "Bring up channel %d\n",
347 sh_chan->id);
348 if (param) {
349 const struct sh_dmae_slave_config *cfg =
350 param->config;
351
352 dmae_set_dmars(sh_chan, cfg->mid_rid);
353 dmae_set_chcr(sh_chan, cfg->chcr);
354 } else {
355 dmae_init(sh_chan);
356 }
357
358 if (sh_chan->pm_state == DMAE_PM_PENDING)
359 sh_chan_xfer_ld_queue(sh_chan);
360 sh_chan->pm_state = DMAE_PM_ESTABLISHED;
361 }
Guennadi Liakhovetskic11b46c2012-01-04 15:34:17 +0100362 } else {
363 sh_chan->pm_state = DMAE_PM_PENDING;
Guennadi Liakhovetski7a1cd9a2011-08-18 16:55:27 +0200364 }
365
366 spin_unlock_irq(&sh_chan->desc_lock);
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000367
368 return cookie;
369}
370
Guennadi Liakhovetski3542a112009-12-17 09:41:39 -0700371/* Called with desc_lock held */
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000372static struct sh_desc *sh_dmae_get_desc(struct sh_dmae_chan *sh_chan)
373{
Guennadi Liakhovetski3542a112009-12-17 09:41:39 -0700374 struct sh_desc *desc;
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000375
Guennadi Liakhovetski3542a112009-12-17 09:41:39 -0700376 list_for_each_entry(desc, &sh_chan->ld_free, node)
377 if (desc->mark != DESC_PREPARED) {
378 BUG_ON(desc->mark != DESC_IDLE);
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000379 list_del(&desc->node);
Guennadi Liakhovetski3542a112009-12-17 09:41:39 -0700380 return desc;
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000381 }
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000382
Guennadi Liakhovetski3542a112009-12-17 09:41:39 -0700383 return NULL;
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000384}
385
Guennadi Liakhovetski5bac9422010-04-21 15:36:49 +0000386static const struct sh_dmae_slave_config *sh_dmae_find_slave(
Magnus Damm4bab9d42010-03-19 04:46:38 +0000387 struct sh_dmae_chan *sh_chan, struct sh_dmae_slave *param)
Guennadi Liakhovetskicfefe992010-02-03 14:46:41 +0000388{
Kuninori Morimotoc4e0dd72011-06-16 05:08:09 +0000389 struct sh_dmae_device *shdev = to_sh_dev(sh_chan);
Guennadi Liakhovetski027811b2010-02-11 16:50:10 +0000390 struct sh_dmae_pdata *pdata = shdev->pdata;
Guennadi Liakhovetskicfefe992010-02-03 14:46:41 +0000391 int i;
392
Magnus Damm02ca5082010-03-19 04:46:47 +0000393 if (param->slave_id >= SH_DMA_SLAVE_NUMBER)
Guennadi Liakhovetskicfefe992010-02-03 14:46:41 +0000394 return NULL;
395
Guennadi Liakhovetski027811b2010-02-11 16:50:10 +0000396 for (i = 0; i < pdata->slave_num; i++)
Magnus Damm4bab9d42010-03-19 04:46:38 +0000397 if (pdata->slave[i].slave_id == param->slave_id)
Guennadi Liakhovetski027811b2010-02-11 16:50:10 +0000398 return pdata->slave + i;
Guennadi Liakhovetskicfefe992010-02-03 14:46:41 +0000399
400 return NULL;
401}
402
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000403static int sh_dmae_alloc_chan_resources(struct dma_chan *chan)
404{
405 struct sh_dmae_chan *sh_chan = to_sh_chan(chan);
406 struct sh_desc *desc;
Guennadi Liakhovetskicfefe992010-02-03 14:46:41 +0000407 struct sh_dmae_slave *param = chan->private;
Guennadi Liakhovetski83515bc2010-04-19 08:39:39 +0000408 int ret;
Guennadi Liakhovetskicfefe992010-02-03 14:46:41 +0000409
410 /*
411 * This relies on the guarantee from dmaengine that alloc_chan_resources
412 * never runs concurrently with itself or free_chan_resources.
413 */
414 if (param) {
Guennadi Liakhovetski5bac9422010-04-21 15:36:49 +0000415 const struct sh_dmae_slave_config *cfg;
Guennadi Liakhovetskicfefe992010-02-03 14:46:41 +0000416
Magnus Damm4bab9d42010-03-19 04:46:38 +0000417 cfg = sh_dmae_find_slave(sh_chan, param);
Guennadi Liakhovetski83515bc2010-04-19 08:39:39 +0000418 if (!cfg) {
419 ret = -EINVAL;
420 goto efindslave;
421 }
Guennadi Liakhovetskicfefe992010-02-03 14:46:41 +0000422
Guennadi Liakhovetski83515bc2010-04-19 08:39:39 +0000423 if (test_and_set_bit(param->slave_id, sh_dmae_slave_used)) {
424 ret = -EBUSY;
425 goto etestused;
426 }
Guennadi Liakhovetskicfefe992010-02-03 14:46:41 +0000427
428 param->config = cfg;
Guennadi Liakhovetskicfefe992010-02-03 14:46:41 +0000429 }
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000430
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000431 while (sh_chan->descs_allocated < NR_DESCS_PER_CHANNEL) {
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000432 desc = kzalloc(sizeof(struct sh_desc), GFP_KERNEL);
Guennadi Liakhovetskib4dae6e2011-09-25 16:12:18 +0200433 if (!desc)
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000434 break;
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000435 dma_async_tx_descriptor_init(&desc->async_tx,
436 &sh_chan->common);
437 desc->async_tx.tx_submit = sh_dmae_tx_submit;
Guennadi Liakhovetski3542a112009-12-17 09:41:39 -0700438 desc->mark = DESC_IDLE;
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000439
Guennadi Liakhovetski3542a112009-12-17 09:41:39 -0700440 list_add(&desc->node, &sh_chan->ld_free);
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000441 sh_chan->descs_allocated++;
442 }
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000443
Guennadi Liakhovetski83515bc2010-04-19 08:39:39 +0000444 if (!sh_chan->descs_allocated) {
445 ret = -ENOMEM;
446 goto edescalloc;
447 }
Guennadi Liakhovetski20f2a3b2010-02-11 16:50:18 +0000448
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000449 return sh_chan->descs_allocated;
Guennadi Liakhovetski83515bc2010-04-19 08:39:39 +0000450
451edescalloc:
452 if (param)
453 clear_bit(param->slave_id, sh_dmae_slave_used);
454etestused:
455efindslave:
Guennadi Liakhovetskib4dae6e2011-09-25 16:12:18 +0200456 chan->private = NULL;
Guennadi Liakhovetski83515bc2010-04-19 08:39:39 +0000457 return ret;
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000458}
459
460/*
461 * sh_dma_free_chan_resources - Free all resources of the channel.
462 */
463static void sh_dmae_free_chan_resources(struct dma_chan *chan)
464{
465 struct sh_dmae_chan *sh_chan = to_sh_chan(chan);
466 struct sh_desc *desc, *_desc;
467 LIST_HEAD(list);
468
Guennadi Liakhovetski2dc66662011-04-29 17:09:21 +0000469 /* Protect against ISR */
470 spin_lock_irq(&sh_chan->desc_lock);
Guennadi Liakhovetskicfefe992010-02-03 14:46:41 +0000471 dmae_halt(sh_chan);
Guennadi Liakhovetski2dc66662011-04-29 17:09:21 +0000472 spin_unlock_irq(&sh_chan->desc_lock);
473
474 /* Now no new interrupts will occur */
Guennadi Liakhovetskicfefe992010-02-03 14:46:41 +0000475
Guennadi Liakhovetski3542a112009-12-17 09:41:39 -0700476 /* Prepared and not submitted descriptors can still be on the queue */
477 if (!list_empty(&sh_chan->ld_queue))
478 sh_dmae_chan_ld_cleanup(sh_chan, true);
479
Guennadi Liakhovetskicfefe992010-02-03 14:46:41 +0000480 if (chan->private) {
481 /* The caller is holding dma_list_mutex */
482 struct sh_dmae_slave *param = chan->private;
483 clear_bit(param->slave_id, sh_dmae_slave_used);
Guennadi Liakhovetski2dc66662011-04-29 17:09:21 +0000484 chan->private = NULL;
Guennadi Liakhovetskicfefe992010-02-03 14:46:41 +0000485 }
486
Guennadi Liakhovetskib4dae6e2011-09-25 16:12:18 +0200487 spin_lock_irq(&sh_chan->desc_lock);
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000488
489 list_splice_init(&sh_chan->ld_free, &list);
490 sh_chan->descs_allocated = 0;
491
Guennadi Liakhovetskib4dae6e2011-09-25 16:12:18 +0200492 spin_unlock_irq(&sh_chan->desc_lock);
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000493
494 list_for_each_entry_safe(desc, _desc, &list, node)
495 kfree(desc);
496}
497
Guennadi Liakhovetskicfefe992010-02-03 14:46:41 +0000498/**
Guennadi Liakhovetskifc461852010-01-19 07:24:55 +0000499 * sh_dmae_add_desc - get, set up and return one transfer descriptor
500 * @sh_chan: DMA channel
501 * @flags: DMA transfer flags
502 * @dest: destination DMA address, incremented when direction equals
Vinod Kouldb8196d2011-10-13 22:34:23 +0530503 * DMA_DEV_TO_MEM
Guennadi Liakhovetskifc461852010-01-19 07:24:55 +0000504 * @src: source DMA address, incremented when direction equals
Vinod Kouldb8196d2011-10-13 22:34:23 +0530505 * DMA_MEM_TO_DEV
Guennadi Liakhovetskifc461852010-01-19 07:24:55 +0000506 * @len: DMA transfer length
507 * @first: if NULL, set to the current descriptor and cookie set to -EBUSY
508 * @direction: needed for slave DMA to decide which address to keep constant,
Vinod Kouldb8196d2011-10-13 22:34:23 +0530509 * equals DMA_MEM_TO_MEM for MEMCPY
Guennadi Liakhovetskifc461852010-01-19 07:24:55 +0000510 * Returns 0 or an error
511 * Locks: called with desc_lock held
512 */
513static struct sh_desc *sh_dmae_add_desc(struct sh_dmae_chan *sh_chan,
514 unsigned long flags, dma_addr_t *dest, dma_addr_t *src, size_t *len,
Vinod Kouldb8196d2011-10-13 22:34:23 +0530515 struct sh_desc **first, enum dma_transfer_direction direction)
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000516{
Guennadi Liakhovetskifc461852010-01-19 07:24:55 +0000517 struct sh_desc *new;
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000518 size_t copy_size;
Guennadi Liakhovetskifc461852010-01-19 07:24:55 +0000519
520 if (!*len)
521 return NULL;
522
523 /* Allocate the link descriptor from the free list */
524 new = sh_dmae_get_desc(sh_chan);
525 if (!new) {
526 dev_err(sh_chan->dev, "No free link descriptor available\n");
527 return NULL;
528 }
529
530 copy_size = min(*len, (size_t)SH_DMA_TCR_MAX + 1);
531
532 new->hw.sar = *src;
533 new->hw.dar = *dest;
534 new->hw.tcr = copy_size;
535
536 if (!*first) {
537 /* First desc */
538 new->async_tx.cookie = -EBUSY;
539 *first = new;
540 } else {
541 /* Other desc - invisible to the user */
542 new->async_tx.cookie = -EINVAL;
543 }
544
Guennadi Liakhovetskicfefe992010-02-03 14:46:41 +0000545 dev_dbg(sh_chan->dev,
546 "chaining (%u/%u)@%x -> %x with %p, cookie %d, shift %d\n",
Guennadi Liakhovetskifc461852010-01-19 07:24:55 +0000547 copy_size, *len, *src, *dest, &new->async_tx,
Guennadi Liakhovetskicfefe992010-02-03 14:46:41 +0000548 new->async_tx.cookie, sh_chan->xmit_shift);
Guennadi Liakhovetskifc461852010-01-19 07:24:55 +0000549
550 new->mark = DESC_PREPARED;
551 new->async_tx.flags = flags;
Guennadi Liakhovetskicfefe992010-02-03 14:46:41 +0000552 new->direction = direction;
Guennadi Liakhovetskifc461852010-01-19 07:24:55 +0000553
554 *len -= copy_size;
Vinod Kouldb8196d2011-10-13 22:34:23 +0530555 if (direction == DMA_MEM_TO_MEM || direction == DMA_MEM_TO_DEV)
Guennadi Liakhovetskifc461852010-01-19 07:24:55 +0000556 *src += copy_size;
Vinod Kouldb8196d2011-10-13 22:34:23 +0530557 if (direction == DMA_MEM_TO_MEM || direction == DMA_DEV_TO_MEM)
Guennadi Liakhovetskifc461852010-01-19 07:24:55 +0000558 *dest += copy_size;
559
560 return new;
561}
562
563/*
564 * sh_dmae_prep_sg - prepare transfer descriptors from an SG list
565 *
566 * Common routine for public (MEMCPY) and slave DMA. The MEMCPY case is also
567 * converted to scatter-gather to guarantee consistent locking and a correct
568 * list manipulation. For slave DMA direction carries the usual meaning, and,
569 * logically, the SG list is RAM and the addr variable contains slave address,
Vinod Kouldb8196d2011-10-13 22:34:23 +0530570 * e.g., the FIFO I/O register. For MEMCPY direction equals DMA_MEM_TO_MEM
Guennadi Liakhovetskifc461852010-01-19 07:24:55 +0000571 * and the SG list contains only one element and points at the source buffer.
572 */
573static struct dma_async_tx_descriptor *sh_dmae_prep_sg(struct sh_dmae_chan *sh_chan,
574 struct scatterlist *sgl, unsigned int sg_len, dma_addr_t *addr,
Vinod Kouldb8196d2011-10-13 22:34:23 +0530575 enum dma_transfer_direction direction, unsigned long flags)
Guennadi Liakhovetskifc461852010-01-19 07:24:55 +0000576{
577 struct scatterlist *sg;
578 struct sh_desc *first = NULL, *new = NULL /* compiler... */;
Guennadi Liakhovetski3542a112009-12-17 09:41:39 -0700579 LIST_HEAD(tx_list);
Guennadi Liakhovetskifc461852010-01-19 07:24:55 +0000580 int chunks = 0;
Guennadi Liakhovetskib4dae6e2011-09-25 16:12:18 +0200581 unsigned long irq_flags;
Guennadi Liakhovetskifc461852010-01-19 07:24:55 +0000582 int i;
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000583
Guennadi Liakhovetskifc461852010-01-19 07:24:55 +0000584 if (!sg_len)
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000585 return NULL;
586
Guennadi Liakhovetskifc461852010-01-19 07:24:55 +0000587 for_each_sg(sgl, sg, sg_len, i)
588 chunks += (sg_dma_len(sg) + SH_DMA_TCR_MAX) /
589 (SH_DMA_TCR_MAX + 1);
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000590
Guennadi Liakhovetski3542a112009-12-17 09:41:39 -0700591 /* Have to lock the whole loop to protect against concurrent release */
Guennadi Liakhovetskib4dae6e2011-09-25 16:12:18 +0200592 spin_lock_irqsave(&sh_chan->desc_lock, irq_flags);
Guennadi Liakhovetski3542a112009-12-17 09:41:39 -0700593
594 /*
595 * Chaining:
596 * first descriptor is what user is dealing with in all API calls, its
597 * cookie is at first set to -EBUSY, at tx-submit to a positive
598 * number
599 * if more than one chunk is needed further chunks have cookie = -EINVAL
600 * the last chunk, if not equal to the first, has cookie = -ENOSPC
601 * all chunks are linked onto the tx_list head with their .node heads
602 * only during this function, then they are immediately spliced
603 * back onto the free list in form of a chain
604 */
Guennadi Liakhovetskifc461852010-01-19 07:24:55 +0000605 for_each_sg(sgl, sg, sg_len, i) {
606 dma_addr_t sg_addr = sg_dma_address(sg);
607 size_t len = sg_dma_len(sg);
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000608
Guennadi Liakhovetskifc461852010-01-19 07:24:55 +0000609 if (!len)
610 goto err_get_desc;
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000611
Guennadi Liakhovetskifc461852010-01-19 07:24:55 +0000612 do {
613 dev_dbg(sh_chan->dev, "Add SG #%d@%p[%d], dma %llx\n",
614 i, sg, len, (unsigned long long)sg_addr);
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000615
Vinod Kouldb8196d2011-10-13 22:34:23 +0530616 if (direction == DMA_DEV_TO_MEM)
Guennadi Liakhovetskifc461852010-01-19 07:24:55 +0000617 new = sh_dmae_add_desc(sh_chan, flags,
618 &sg_addr, addr, &len, &first,
619 direction);
620 else
621 new = sh_dmae_add_desc(sh_chan, flags,
622 addr, &sg_addr, &len, &first,
623 direction);
624 if (!new)
625 goto err_get_desc;
Guennadi Liakhovetski3542a112009-12-17 09:41:39 -0700626
Guennadi Liakhovetskifc461852010-01-19 07:24:55 +0000627 new->chunks = chunks--;
628 list_add_tail(&new->node, &tx_list);
629 } while (len);
630 }
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000631
Guennadi Liakhovetski3542a112009-12-17 09:41:39 -0700632 if (new != first)
633 new->async_tx.cookie = -ENOSPC;
634
635 /* Put them back on the free list, so, they don't get lost */
636 list_splice_tail(&tx_list, &sh_chan->ld_free);
637
Guennadi Liakhovetskib4dae6e2011-09-25 16:12:18 +0200638 spin_unlock_irqrestore(&sh_chan->desc_lock, irq_flags);
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000639
640 return &first->async_tx;
Guennadi Liakhovetskifc461852010-01-19 07:24:55 +0000641
642err_get_desc:
643 list_for_each_entry(new, &tx_list, node)
644 new->mark = DESC_IDLE;
645 list_splice(&tx_list, &sh_chan->ld_free);
646
Guennadi Liakhovetskib4dae6e2011-09-25 16:12:18 +0200647 spin_unlock_irqrestore(&sh_chan->desc_lock, irq_flags);
Guennadi Liakhovetskifc461852010-01-19 07:24:55 +0000648
649 return NULL;
650}
651
652static struct dma_async_tx_descriptor *sh_dmae_prep_memcpy(
653 struct dma_chan *chan, dma_addr_t dma_dest, dma_addr_t dma_src,
654 size_t len, unsigned long flags)
655{
656 struct sh_dmae_chan *sh_chan;
657 struct scatterlist sg;
658
659 if (!chan || !len)
660 return NULL;
661
662 sh_chan = to_sh_chan(chan);
663
664 sg_init_table(&sg, 1);
665 sg_set_page(&sg, pfn_to_page(PFN_DOWN(dma_src)), len,
666 offset_in_page(dma_src));
667 sg_dma_address(&sg) = dma_src;
668 sg_dma_len(&sg) = len;
669
Vinod Kouldb8196d2011-10-13 22:34:23 +0530670 return sh_dmae_prep_sg(sh_chan, &sg, 1, &dma_dest, DMA_MEM_TO_MEM,
Guennadi Liakhovetskifc461852010-01-19 07:24:55 +0000671 flags);
Guennadi Liakhovetski3542a112009-12-17 09:41:39 -0700672}
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000673
Guennadi Liakhovetskicfefe992010-02-03 14:46:41 +0000674static struct dma_async_tx_descriptor *sh_dmae_prep_slave_sg(
675 struct dma_chan *chan, struct scatterlist *sgl, unsigned int sg_len,
Vinod Kouldb8196d2011-10-13 22:34:23 +0530676 enum dma_transfer_direction direction, unsigned long flags)
Guennadi Liakhovetskicfefe992010-02-03 14:46:41 +0000677{
678 struct sh_dmae_slave *param;
679 struct sh_dmae_chan *sh_chan;
Guennadi Liakhovetski5bac9422010-04-21 15:36:49 +0000680 dma_addr_t slave_addr;
Guennadi Liakhovetskicfefe992010-02-03 14:46:41 +0000681
682 if (!chan)
683 return NULL;
684
685 sh_chan = to_sh_chan(chan);
686 param = chan->private;
687
688 /* Someone calling slave DMA on a public channel? */
689 if (!param || !sg_len) {
690 dev_warn(sh_chan->dev, "%s: bad parameter: %p, %d, %d\n",
691 __func__, param, sg_len, param ? param->slave_id : -1);
692 return NULL;
693 }
694
Dan Carpenter9f9ff202010-08-14 11:01:45 +0200695 slave_addr = param->config->addr;
696
Guennadi Liakhovetskicfefe992010-02-03 14:46:41 +0000697 /*
698 * if (param != NULL), this is a successfully requested slave channel,
699 * therefore param->config != NULL too.
700 */
Guennadi Liakhovetski5bac9422010-04-21 15:36:49 +0000701 return sh_dmae_prep_sg(sh_chan, sgl, sg_len, &slave_addr,
Guennadi Liakhovetskicfefe992010-02-03 14:46:41 +0000702 direction, flags);
703}
704
Linus Walleij05827632010-05-17 16:30:42 -0700705static int sh_dmae_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd,
706 unsigned long arg)
Guennadi Liakhovetskicfefe992010-02-03 14:46:41 +0000707{
708 struct sh_dmae_chan *sh_chan = to_sh_chan(chan);
Guennadi Liakhovetskib4dae6e2011-09-25 16:12:18 +0200709 unsigned long flags;
Guennadi Liakhovetskicfefe992010-02-03 14:46:41 +0000710
Linus Walleijc3635c72010-03-26 16:44:01 -0700711 /* Only supports DMA_TERMINATE_ALL */
712 if (cmd != DMA_TERMINATE_ALL)
713 return -ENXIO;
714
Guennadi Liakhovetskicfefe992010-02-03 14:46:41 +0000715 if (!chan)
Linus Walleijc3635c72010-03-26 16:44:01 -0700716 return -EINVAL;
Guennadi Liakhovetskicfefe992010-02-03 14:46:41 +0000717
Guennadi Liakhovetskib4dae6e2011-09-25 16:12:18 +0200718 spin_lock_irqsave(&sh_chan->desc_lock, flags);
Guennadi Liakhovetskic0149062010-02-18 16:30:02 +0000719 dmae_halt(sh_chan);
720
Guennadi Liakhovetskic0149062010-02-18 16:30:02 +0000721 if (!list_empty(&sh_chan->ld_queue)) {
722 /* Record partial transfer */
723 struct sh_desc *desc = list_entry(sh_chan->ld_queue.next,
724 struct sh_desc, node);
725 desc->partial = (desc->hw.tcr - sh_dmae_readl(sh_chan, TCR)) <<
726 sh_chan->xmit_shift;
Guennadi Liakhovetskic0149062010-02-18 16:30:02 +0000727 }
Guennadi Liakhovetskib4dae6e2011-09-25 16:12:18 +0200728 spin_unlock_irqrestore(&sh_chan->desc_lock, flags);
Guennadi Liakhovetskic0149062010-02-18 16:30:02 +0000729
Guennadi Liakhovetskicfefe992010-02-03 14:46:41 +0000730 sh_dmae_chan_ld_cleanup(sh_chan, true);
Linus Walleijc3635c72010-03-26 16:44:01 -0700731
732 return 0;
Guennadi Liakhovetskicfefe992010-02-03 14:46:41 +0000733}
734
Guennadi Liakhovetski3542a112009-12-17 09:41:39 -0700735static dma_async_tx_callback __ld_cleanup(struct sh_dmae_chan *sh_chan, bool all)
736{
737 struct sh_desc *desc, *_desc;
738 /* Is the "exposed" head of a chain acked? */
739 bool head_acked = false;
740 dma_cookie_t cookie = 0;
741 dma_async_tx_callback callback = NULL;
742 void *param = NULL;
Guennadi Liakhovetskib4dae6e2011-09-25 16:12:18 +0200743 unsigned long flags;
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000744
Guennadi Liakhovetskib4dae6e2011-09-25 16:12:18 +0200745 spin_lock_irqsave(&sh_chan->desc_lock, flags);
Guennadi Liakhovetski3542a112009-12-17 09:41:39 -0700746 list_for_each_entry_safe(desc, _desc, &sh_chan->ld_queue, node) {
747 struct dma_async_tx_descriptor *tx = &desc->async_tx;
748
749 BUG_ON(tx->cookie > 0 && tx->cookie != desc->cookie);
750 BUG_ON(desc->mark != DESC_SUBMITTED &&
751 desc->mark != DESC_COMPLETED &&
752 desc->mark != DESC_WAITING);
753
754 /*
755 * queue is ordered, and we use this loop to (1) clean up all
756 * completed descriptors, and to (2) update descriptor flags of
757 * any chunks in a (partially) completed chain
758 */
759 if (!all && desc->mark == DESC_SUBMITTED &&
760 desc->cookie != cookie)
761 break;
762
763 if (tx->cookie > 0)
764 cookie = tx->cookie;
765
766 if (desc->mark == DESC_COMPLETED && desc->chunks == 1) {
Guennadi Liakhovetskicfefe992010-02-03 14:46:41 +0000767 if (sh_chan->completed_cookie != desc->cookie - 1)
768 dev_dbg(sh_chan->dev,
769 "Completing cookie %d, expected %d\n",
770 desc->cookie,
771 sh_chan->completed_cookie + 1);
Guennadi Liakhovetski3542a112009-12-17 09:41:39 -0700772 sh_chan->completed_cookie = desc->cookie;
773 }
774
775 /* Call callback on the last chunk */
776 if (desc->mark == DESC_COMPLETED && tx->callback) {
777 desc->mark = DESC_WAITING;
778 callback = tx->callback;
779 param = tx->callback_param;
780 dev_dbg(sh_chan->dev, "descriptor #%d@%p on %d callback\n",
781 tx->cookie, tx, sh_chan->id);
782 BUG_ON(desc->chunks != 1);
783 break;
784 }
785
786 if (tx->cookie > 0 || tx->cookie == -EBUSY) {
787 if (desc->mark == DESC_COMPLETED) {
788 BUG_ON(tx->cookie < 0);
789 desc->mark = DESC_WAITING;
790 }
791 head_acked = async_tx_test_ack(tx);
792 } else {
793 switch (desc->mark) {
794 case DESC_COMPLETED:
795 desc->mark = DESC_WAITING;
796 /* Fall through */
797 case DESC_WAITING:
798 if (head_acked)
799 async_tx_ack(&desc->async_tx);
800 }
801 }
802
803 dev_dbg(sh_chan->dev, "descriptor %p #%d completed.\n",
804 tx, tx->cookie);
805
806 if (((desc->mark == DESC_COMPLETED ||
807 desc->mark == DESC_WAITING) &&
808 async_tx_test_ack(&desc->async_tx)) || all) {
809 /* Remove from ld_queue list */
810 desc->mark = DESC_IDLE;
Guennadi Liakhovetski7a1cd9a2011-08-18 16:55:27 +0200811
Guennadi Liakhovetski3542a112009-12-17 09:41:39 -0700812 list_move(&desc->node, &sh_chan->ld_free);
Guennadi Liakhovetski7a1cd9a2011-08-18 16:55:27 +0200813
814 if (list_empty(&sh_chan->ld_queue)) {
815 dev_dbg(sh_chan->dev, "Bring down channel %d\n", sh_chan->id);
816 pm_runtime_put(sh_chan->dev);
817 }
Guennadi Liakhovetski3542a112009-12-17 09:41:39 -0700818 }
819 }
Guennadi Liakhovetski2dc66662011-04-29 17:09:21 +0000820
821 if (all && !callback)
822 /*
823 * Terminating and the loop completed normally: forgive
824 * uncompleted cookies
825 */
826 sh_chan->completed_cookie = sh_chan->common.cookie;
827
Guennadi Liakhovetskib4dae6e2011-09-25 16:12:18 +0200828 spin_unlock_irqrestore(&sh_chan->desc_lock, flags);
Guennadi Liakhovetski3542a112009-12-17 09:41:39 -0700829
830 if (callback)
831 callback(param);
832
833 return callback;
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000834}
835
836/*
837 * sh_chan_ld_cleanup - Clean up link descriptors
838 *
Guennadi Liakhovetski3542a112009-12-17 09:41:39 -0700839 * This function cleans up the ld_queue of DMA channel.
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000840 */
Guennadi Liakhovetski3542a112009-12-17 09:41:39 -0700841static void sh_dmae_chan_ld_cleanup(struct sh_dmae_chan *sh_chan, bool all)
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000842{
Guennadi Liakhovetski3542a112009-12-17 09:41:39 -0700843 while (__ld_cleanup(sh_chan, all))
844 ;
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000845}
846
Guennadi Liakhovetski7a1cd9a2011-08-18 16:55:27 +0200847/* Called under spin_lock_irq(&sh_chan->desc_lock) */
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000848static void sh_chan_xfer_ld_queue(struct sh_dmae_chan *sh_chan)
849{
Guennadi Liakhovetski47a4dc22010-02-11 16:50:05 +0000850 struct sh_desc *desc;
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000851
852 /* DMA work check */
Guennadi Liakhovetski7a1cd9a2011-08-18 16:55:27 +0200853 if (dmae_is_busy(sh_chan))
Guennadi Liakhovetskib4dae6e2011-09-25 16:12:18 +0200854 return;
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000855
Justin P. Mattock5a3a7652011-01-19 15:36:38 +0100856 /* Find the first not transferred descriptor */
Guennadi Liakhovetski47a4dc22010-02-11 16:50:05 +0000857 list_for_each_entry(desc, &sh_chan->ld_queue, node)
858 if (desc->mark == DESC_SUBMITTED) {
Guennadi Liakhovetskic0149062010-02-18 16:30:02 +0000859 dev_dbg(sh_chan->dev, "Queue #%d to %d: %u@%x -> %x\n",
860 desc->async_tx.cookie, sh_chan->id,
861 desc->hw.tcr, desc->hw.sar, desc->hw.dar);
Guennadi Liakhovetski3542a112009-12-17 09:41:39 -0700862 /* Get the ld start address from ld_queue */
Guennadi Liakhovetski47a4dc22010-02-11 16:50:05 +0000863 dmae_set_reg(sh_chan, &desc->hw);
Guennadi Liakhovetski3542a112009-12-17 09:41:39 -0700864 dmae_start(sh_chan);
865 break;
866 }
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000867}
868
869static void sh_dmae_memcpy_issue_pending(struct dma_chan *chan)
870{
871 struct sh_dmae_chan *sh_chan = to_sh_chan(chan);
Guennadi Liakhovetski7a1cd9a2011-08-18 16:55:27 +0200872
873 spin_lock_irq(&sh_chan->desc_lock);
874 if (sh_chan->pm_state == DMAE_PM_ESTABLISHED)
875 sh_chan_xfer_ld_queue(sh_chan);
876 else
877 sh_chan->pm_state = DMAE_PM_PENDING;
878 spin_unlock_irq(&sh_chan->desc_lock);
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000879}
880
Linus Walleij07934482010-03-26 16:50:49 -0700881static enum dma_status sh_dmae_tx_status(struct dma_chan *chan,
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000882 dma_cookie_t cookie,
Linus Walleij07934482010-03-26 16:50:49 -0700883 struct dma_tx_state *txstate)
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000884{
885 struct sh_dmae_chan *sh_chan = to_sh_chan(chan);
886 dma_cookie_t last_used;
887 dma_cookie_t last_complete;
Guennadi Liakhovetski47a4dc22010-02-11 16:50:05 +0000888 enum dma_status status;
Guennadi Liakhovetskib4dae6e2011-09-25 16:12:18 +0200889 unsigned long flags;
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000890
Guennadi Liakhovetski3542a112009-12-17 09:41:39 -0700891 sh_dmae_chan_ld_cleanup(sh_chan, false);
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000892
Guennadi Liakhovetski2dc66662011-04-29 17:09:21 +0000893 /* First read completed cookie to avoid a skew */
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000894 last_complete = sh_chan->completed_cookie;
Guennadi Liakhovetski2dc66662011-04-29 17:09:21 +0000895 rmb();
896 last_used = chan->cookie;
Guennadi Liakhovetski3542a112009-12-17 09:41:39 -0700897 BUG_ON(last_complete < 0);
Dan Williamsbca34692010-03-26 16:52:10 -0700898 dma_set_tx_state(txstate, last_complete, last_used, 0);
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000899
Guennadi Liakhovetskib4dae6e2011-09-25 16:12:18 +0200900 spin_lock_irqsave(&sh_chan->desc_lock, flags);
Guennadi Liakhovetski47a4dc22010-02-11 16:50:05 +0000901
902 status = dma_async_is_complete(cookie, last_complete, last_used);
903
904 /*
905 * If we don't find cookie on the queue, it has been aborted and we have
906 * to report error
907 */
908 if (status != DMA_SUCCESS) {
909 struct sh_desc *desc;
910 status = DMA_ERROR;
911 list_for_each_entry(desc, &sh_chan->ld_queue, node)
912 if (desc->cookie == cookie) {
913 status = DMA_IN_PROGRESS;
914 break;
915 }
916 }
917
Guennadi Liakhovetskib4dae6e2011-09-25 16:12:18 +0200918 spin_unlock_irqrestore(&sh_chan->desc_lock, flags);
Guennadi Liakhovetski47a4dc22010-02-11 16:50:05 +0000919
920 return status;
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000921}
922
923static irqreturn_t sh_dmae_interrupt(int irq, void *data)
924{
925 irqreturn_t ret = IRQ_NONE;
Guennadi Liakhovetski2dc66662011-04-29 17:09:21 +0000926 struct sh_dmae_chan *sh_chan = data;
927 u32 chcr;
928
929 spin_lock(&sh_chan->desc_lock);
930
Kuninori Morimoto5899a722011-06-17 08:20:40 +0000931 chcr = chcr_read(sh_chan);
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000932
933 if (chcr & CHCR_TE) {
934 /* DMA stop */
935 dmae_halt(sh_chan);
936
937 ret = IRQ_HANDLED;
938 tasklet_schedule(&sh_chan->tasklet);
939 }
940
Guennadi Liakhovetski2dc66662011-04-29 17:09:21 +0000941 spin_unlock(&sh_chan->desc_lock);
942
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000943 return ret;
944}
945
Guennadi Liakhovetski2dc66662011-04-29 17:09:21 +0000946/* Called from error IRQ or NMI */
947static bool sh_dmae_reset(struct sh_dmae_device *shdev)
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000948{
Paul Mundt03aa18f2010-12-17 19:16:10 +0900949 unsigned int handled = 0;
Guennadi Liakhovetski47a4dc22010-02-11 16:50:05 +0000950 int i;
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000951
Guennadi Liakhovetski47a4dc22010-02-11 16:50:05 +0000952 /* halt the dma controller */
Guennadi Liakhovetski027811b2010-02-11 16:50:10 +0000953 sh_dmae_ctl_stop(shdev);
Guennadi Liakhovetski47a4dc22010-02-11 16:50:05 +0000954
955 /* We cannot detect, which channel caused the error, have to reset all */
Guennadi Liakhovetski8b1935e2010-02-11 16:50:14 +0000956 for (i = 0; i < SH_DMAC_MAX_CHANNELS; i++) {
Guennadi Liakhovetski47a4dc22010-02-11 16:50:05 +0000957 struct sh_dmae_chan *sh_chan = shdev->chan[i];
Paul Mundt03aa18f2010-12-17 19:16:10 +0900958 struct sh_desc *desc;
Guennadi Liakhovetski2dc66662011-04-29 17:09:21 +0000959 LIST_HEAD(dl);
Paul Mundt03aa18f2010-12-17 19:16:10 +0900960
961 if (!sh_chan)
962 continue;
963
Guennadi Liakhovetski2dc66662011-04-29 17:09:21 +0000964 spin_lock(&sh_chan->desc_lock);
965
Paul Mundt03aa18f2010-12-17 19:16:10 +0900966 /* Stop the channel */
967 dmae_halt(sh_chan);
968
Guennadi Liakhovetski2dc66662011-04-29 17:09:21 +0000969 list_splice_init(&sh_chan->ld_queue, &dl);
970
Guennadi Liakhovetski7a1cd9a2011-08-18 16:55:27 +0200971 if (!list_empty(&dl)) {
972 dev_dbg(sh_chan->dev, "Bring down channel %d\n", sh_chan->id);
973 pm_runtime_put(sh_chan->dev);
974 }
975 sh_chan->pm_state = DMAE_PM_ESTABLISHED;
976
Guennadi Liakhovetski2dc66662011-04-29 17:09:21 +0000977 spin_unlock(&sh_chan->desc_lock);
978
Paul Mundt03aa18f2010-12-17 19:16:10 +0900979 /* Complete all */
Guennadi Liakhovetski2dc66662011-04-29 17:09:21 +0000980 list_for_each_entry(desc, &dl, node) {
Paul Mundt03aa18f2010-12-17 19:16:10 +0900981 struct dma_async_tx_descriptor *tx = &desc->async_tx;
982 desc->mark = DESC_IDLE;
983 if (tx->callback)
984 tx->callback(tx->callback_param);
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000985 }
Paul Mundt03aa18f2010-12-17 19:16:10 +0900986
Guennadi Liakhovetski2dc66662011-04-29 17:09:21 +0000987 spin_lock(&sh_chan->desc_lock);
988 list_splice(&dl, &sh_chan->ld_free);
989 spin_unlock(&sh_chan->desc_lock);
990
Paul Mundt03aa18f2010-12-17 19:16:10 +0900991 handled++;
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000992 }
Paul Mundt03aa18f2010-12-17 19:16:10 +0900993
Guennadi Liakhovetski027811b2010-02-11 16:50:10 +0000994 sh_dmae_rst(shdev);
Guennadi Liakhovetski47a4dc22010-02-11 16:50:05 +0000995
Paul Mundt03aa18f2010-12-17 19:16:10 +0900996 return !!handled;
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +0000997}
Paul Mundt03aa18f2010-12-17 19:16:10 +0900998
999static irqreturn_t sh_dmae_err(int irq, void *data)
1000{
Yoshihiro Shimodaff7690b2011-02-09 07:46:47 +00001001 struct sh_dmae_device *shdev = data;
1002
Guennadi Liakhovetski2dc66662011-04-29 17:09:21 +00001003 if (!(dmaor_read(shdev) & DMAOR_AE))
Yoshihiro Shimodaff7690b2011-02-09 07:46:47 +00001004 return IRQ_NONE;
Guennadi Liakhovetski2dc66662011-04-29 17:09:21 +00001005
1006 sh_dmae_reset(data);
1007 return IRQ_HANDLED;
Paul Mundt03aa18f2010-12-17 19:16:10 +09001008}
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +00001009
1010static void dmae_do_tasklet(unsigned long data)
1011{
1012 struct sh_dmae_chan *sh_chan = (struct sh_dmae_chan *)data;
Guennadi Liakhovetski3542a112009-12-17 09:41:39 -07001013 struct sh_desc *desc;
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +00001014 u32 sar_buf = sh_dmae_readl(sh_chan, SAR);
Guennadi Liakhovetskicfefe992010-02-03 14:46:41 +00001015 u32 dar_buf = sh_dmae_readl(sh_chan, DAR);
Guennadi Liakhovetski86d61b32009-12-10 18:35:07 +01001016
Guennadi Liakhovetskib4dae6e2011-09-25 16:12:18 +02001017 spin_lock_irq(&sh_chan->desc_lock);
Guennadi Liakhovetski3542a112009-12-17 09:41:39 -07001018 list_for_each_entry(desc, &sh_chan->ld_queue, node) {
Guennadi Liakhovetskicfefe992010-02-03 14:46:41 +00001019 if (desc->mark == DESC_SUBMITTED &&
Vinod Kouldb8196d2011-10-13 22:34:23 +05301020 ((desc->direction == DMA_DEV_TO_MEM &&
Guennadi Liakhovetskicfefe992010-02-03 14:46:41 +00001021 (desc->hw.dar + desc->hw.tcr) == dar_buf) ||
1022 (desc->hw.sar + desc->hw.tcr) == sar_buf)) {
Guennadi Liakhovetski3542a112009-12-17 09:41:39 -07001023 dev_dbg(sh_chan->dev, "done #%d@%p dst %u\n",
1024 desc->async_tx.cookie, &desc->async_tx,
1025 desc->hw.dar);
1026 desc->mark = DESC_COMPLETED;
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +00001027 break;
1028 }
1029 }
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +00001030 /* Next desc */
1031 sh_chan_xfer_ld_queue(sh_chan);
Guennadi Liakhovetski7a1cd9a2011-08-18 16:55:27 +02001032 spin_unlock_irq(&sh_chan->desc_lock);
1033
Guennadi Liakhovetski3542a112009-12-17 09:41:39 -07001034 sh_dmae_chan_ld_cleanup(sh_chan, false);
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +00001035}
1036
Paul Mundt03aa18f2010-12-17 19:16:10 +09001037static bool sh_dmae_nmi_notify(struct sh_dmae_device *shdev)
1038{
Paul Mundt03aa18f2010-12-17 19:16:10 +09001039 /* Fast path out if NMIF is not asserted for this controller */
1040 if ((dmaor_read(shdev) & DMAOR_NMIF) == 0)
1041 return false;
1042
Guennadi Liakhovetski2dc66662011-04-29 17:09:21 +00001043 return sh_dmae_reset(shdev);
Paul Mundt03aa18f2010-12-17 19:16:10 +09001044}
1045
1046static int sh_dmae_nmi_handler(struct notifier_block *self,
1047 unsigned long cmd, void *data)
1048{
1049 struct sh_dmae_device *shdev;
1050 int ret = NOTIFY_DONE;
1051 bool triggered;
1052
1053 /*
1054 * Only concern ourselves with NMI events.
1055 *
1056 * Normally we would check the die chain value, but as this needs
1057 * to be architecture independent, check for NMI context instead.
1058 */
1059 if (!in_nmi())
1060 return NOTIFY_DONE;
1061
1062 rcu_read_lock();
1063 list_for_each_entry_rcu(shdev, &sh_dmae_devices, node) {
1064 /*
1065 * Only stop if one of the controllers has NMIF asserted,
1066 * we do not want to interfere with regular address error
1067 * handling or NMI events that don't concern the DMACs.
1068 */
1069 triggered = sh_dmae_nmi_notify(shdev);
1070 if (triggered == true)
1071 ret = NOTIFY_OK;
1072 }
1073 rcu_read_unlock();
1074
1075 return ret;
1076}
1077
1078static struct notifier_block sh_dmae_nmi_notifier __read_mostly = {
1079 .notifier_call = sh_dmae_nmi_handler,
1080
1081 /* Run before NMI debug handler and KGDB */
1082 .priority = 1,
1083};
1084
Guennadi Liakhovetski027811b2010-02-11 16:50:10 +00001085static int __devinit sh_dmae_chan_probe(struct sh_dmae_device *shdev, int id,
1086 int irq, unsigned long flags)
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +00001087{
1088 int err;
Guennadi Liakhovetski5bac9422010-04-21 15:36:49 +00001089 const struct sh_dmae_channel *chan_pdata = &shdev->pdata->channel[id];
Guennadi Liakhovetski027811b2010-02-11 16:50:10 +00001090 struct platform_device *pdev = to_platform_device(shdev->common.dev);
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +00001091 struct sh_dmae_chan *new_sh_chan;
1092
1093 /* alloc channel */
1094 new_sh_chan = kzalloc(sizeof(struct sh_dmae_chan), GFP_KERNEL);
1095 if (!new_sh_chan) {
Guennadi Liakhovetski86d61b32009-12-10 18:35:07 +01001096 dev_err(shdev->common.dev,
1097 "No free memory for allocating dma channels!\n");
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +00001098 return -ENOMEM;
1099 }
1100
Guennadi Liakhovetski7a1cd9a2011-08-18 16:55:27 +02001101 new_sh_chan->pm_state = DMAE_PM_ESTABLISHED;
1102
1103 /* reference struct dma_device */
Guennadi Liakhovetski8b1935e2010-02-11 16:50:14 +00001104 new_sh_chan->common.device = &shdev->common;
1105
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +00001106 new_sh_chan->dev = shdev->common.dev;
1107 new_sh_chan->id = id;
Guennadi Liakhovetski027811b2010-02-11 16:50:10 +00001108 new_sh_chan->irq = irq;
1109 new_sh_chan->base = shdev->chan_reg + chan_pdata->offset / sizeof(u32);
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +00001110
1111 /* Init DMA tasklet */
1112 tasklet_init(&new_sh_chan->tasklet, dmae_do_tasklet,
1113 (unsigned long)new_sh_chan);
1114
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +00001115 spin_lock_init(&new_sh_chan->desc_lock);
1116
1117 /* Init descripter manage list */
1118 INIT_LIST_HEAD(&new_sh_chan->ld_queue);
1119 INIT_LIST_HEAD(&new_sh_chan->ld_free);
1120
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +00001121 /* Add the channel to DMA device channel list */
1122 list_add_tail(&new_sh_chan->common.device_node,
1123 &shdev->common.channels);
1124 shdev->common.chancnt++;
1125
Guennadi Liakhovetski027811b2010-02-11 16:50:10 +00001126 if (pdev->id >= 0)
1127 snprintf(new_sh_chan->dev_id, sizeof(new_sh_chan->dev_id),
1128 "sh-dmae%d.%d", pdev->id, new_sh_chan->id);
1129 else
1130 snprintf(new_sh_chan->dev_id, sizeof(new_sh_chan->dev_id),
1131 "sh-dma%d", new_sh_chan->id);
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +00001132
1133 /* set up channel irq */
Guennadi Liakhovetski027811b2010-02-11 16:50:10 +00001134 err = request_irq(irq, &sh_dmae_interrupt, flags,
Guennadi Liakhovetski86d61b32009-12-10 18:35:07 +01001135 new_sh_chan->dev_id, new_sh_chan);
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +00001136 if (err) {
1137 dev_err(shdev->common.dev, "DMA channel %d request_irq error "
1138 "with return %d\n", id, err);
1139 goto err_no_irq;
1140 }
1141
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +00001142 shdev->chan[id] = new_sh_chan;
1143 return 0;
1144
1145err_no_irq:
1146 /* remove from dmaengine device node */
1147 list_del(&new_sh_chan->common.device_node);
1148 kfree(new_sh_chan);
1149 return err;
1150}
1151
1152static void sh_dmae_chan_remove(struct sh_dmae_device *shdev)
1153{
1154 int i;
1155
1156 for (i = shdev->common.chancnt - 1 ; i >= 0 ; i--) {
1157 if (shdev->chan[i]) {
Guennadi Liakhovetski027811b2010-02-11 16:50:10 +00001158 struct sh_dmae_chan *sh_chan = shdev->chan[i];
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +00001159
Guennadi Liakhovetski027811b2010-02-11 16:50:10 +00001160 free_irq(sh_chan->irq, sh_chan);
1161
1162 list_del(&sh_chan->common.device_node);
1163 kfree(sh_chan);
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +00001164 shdev->chan[i] = NULL;
1165 }
1166 }
1167 shdev->common.chancnt = 0;
1168}
1169
1170static int __init sh_dmae_probe(struct platform_device *pdev)
1171{
Guennadi Liakhovetski027811b2010-02-11 16:50:10 +00001172 struct sh_dmae_pdata *pdata = pdev->dev.platform_data;
1173 unsigned long irqflags = IRQF_DISABLED,
Guennadi Liakhovetski8b1935e2010-02-11 16:50:14 +00001174 chan_flag[SH_DMAC_MAX_CHANNELS] = {};
1175 int errirq, chan_irq[SH_DMAC_MAX_CHANNELS];
Magnus Damm300e5f92011-05-24 10:31:20 +00001176 int err, i, irq_cnt = 0, irqres = 0, irq_cap = 0;
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +00001177 struct sh_dmae_device *shdev;
Guennadi Liakhovetski027811b2010-02-11 16:50:10 +00001178 struct resource *chan, *dmars, *errirq_res, *chanirq_res;
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +00001179
Dan Williams56adf7e2009-11-22 12:10:10 -07001180 /* get platform data */
Guennadi Liakhovetski027811b2010-02-11 16:50:10 +00001181 if (!pdata || !pdata->channel_num)
Dan Williams56adf7e2009-11-22 12:10:10 -07001182 return -ENODEV;
1183
Guennadi Liakhovetski027811b2010-02-11 16:50:10 +00001184 chan = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Magnus Damm26fc02a2011-05-24 10:31:12 +00001185 /* DMARS area is optional */
Guennadi Liakhovetski027811b2010-02-11 16:50:10 +00001186 dmars = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1187 /*
1188 * IRQ resources:
1189 * 1. there always must be at least one IRQ IO-resource. On SH4 it is
1190 * the error IRQ, in which case it is the only IRQ in this resource:
1191 * start == end. If it is the only IRQ resource, all channels also
1192 * use the same IRQ.
1193 * 2. DMA channel IRQ resources can be specified one per resource or in
1194 * ranges (start != end)
1195 * 3. iff all events (channels and, optionally, error) on this
1196 * controller use the same IRQ, only one IRQ resource can be
1197 * specified, otherwise there must be one IRQ per channel, even if
1198 * some of them are equal
1199 * 4. if all IRQs on this controller are equal or if some specific IRQs
1200 * specify IORESOURCE_IRQ_SHAREABLE in their resources, they will be
1201 * requested with the IRQF_SHARED flag
1202 */
1203 errirq_res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1204 if (!chan || !errirq_res)
1205 return -ENODEV;
1206
1207 if (!request_mem_region(chan->start, resource_size(chan), pdev->name)) {
1208 dev_err(&pdev->dev, "DMAC register region already claimed\n");
1209 return -EBUSY;
1210 }
1211
1212 if (dmars && !request_mem_region(dmars->start, resource_size(dmars), pdev->name)) {
1213 dev_err(&pdev->dev, "DMAC DMARS region already claimed\n");
1214 err = -EBUSY;
1215 goto ermrdmars;
1216 }
1217
1218 err = -ENOMEM;
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +00001219 shdev = kzalloc(sizeof(struct sh_dmae_device), GFP_KERNEL);
1220 if (!shdev) {
Guennadi Liakhovetski027811b2010-02-11 16:50:10 +00001221 dev_err(&pdev->dev, "Not enough memory\n");
1222 goto ealloc;
1223 }
1224
1225 shdev->chan_reg = ioremap(chan->start, resource_size(chan));
1226 if (!shdev->chan_reg)
1227 goto emapchan;
1228 if (dmars) {
1229 shdev->dmars = ioremap(dmars->start, resource_size(dmars));
1230 if (!shdev->dmars)
1231 goto emapdmars;
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +00001232 }
1233
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +00001234 /* platform data */
Guennadi Liakhovetski027811b2010-02-11 16:50:10 +00001235 shdev->pdata = pdata;
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +00001236
Kuninori Morimoto5899a722011-06-17 08:20:40 +00001237 if (pdata->chcr_offset)
1238 shdev->chcr_offset = pdata->chcr_offset;
1239 else
1240 shdev->chcr_offset = CHCR;
1241
Kuninori Morimoto67c62692011-06-17 08:20:51 +00001242 if (pdata->chcr_ie_bit)
1243 shdev->chcr_ie_bit = pdata->chcr_ie_bit;
1244 else
1245 shdev->chcr_ie_bit = CHCR_IE;
1246
Paul Mundt5c2de442011-05-31 15:53:03 +09001247 platform_set_drvdata(pdev, shdev);
1248
Guennadi Liakhovetskic11b46c2012-01-04 15:34:17 +01001249 shdev->common.dev = &pdev->dev;
1250
Guennadi Liakhovetski20f2a3b2010-02-11 16:50:18 +00001251 pm_runtime_enable(&pdev->dev);
1252 pm_runtime_get_sync(&pdev->dev);
1253
Guennadi Liakhovetski31705e22011-05-02 07:59:02 +00001254 spin_lock_irq(&sh_dmae_lock);
Paul Mundt03aa18f2010-12-17 19:16:10 +09001255 list_add_tail_rcu(&shdev->node, &sh_dmae_devices);
Guennadi Liakhovetski31705e22011-05-02 07:59:02 +00001256 spin_unlock_irq(&sh_dmae_lock);
Paul Mundt03aa18f2010-12-17 19:16:10 +09001257
Guennadi Liakhovetski2dc66662011-04-29 17:09:21 +00001258 /* reset dma controller - only needed as a test */
Guennadi Liakhovetski027811b2010-02-11 16:50:10 +00001259 err = sh_dmae_rst(shdev);
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +00001260 if (err)
1261 goto rst_err;
1262
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +00001263 INIT_LIST_HEAD(&shdev->common.channels);
1264
Guennadi Liakhovetskie9c8d7a2012-01-18 10:14:25 +01001265 if (!pdata->slave_only)
1266 dma_cap_set(DMA_MEMCPY, shdev->common.cap_mask);
Magnus Damm26fc02a2011-05-24 10:31:12 +00001267 if (pdata->slave && pdata->slave_num)
Guennadi Liakhovetski027811b2010-02-11 16:50:10 +00001268 dma_cap_set(DMA_SLAVE, shdev->common.cap_mask);
Guennadi Liakhovetskicfefe992010-02-03 14:46:41 +00001269
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +00001270 shdev->common.device_alloc_chan_resources
1271 = sh_dmae_alloc_chan_resources;
1272 shdev->common.device_free_chan_resources = sh_dmae_free_chan_resources;
1273 shdev->common.device_prep_dma_memcpy = sh_dmae_prep_memcpy;
Linus Walleij07934482010-03-26 16:50:49 -07001274 shdev->common.device_tx_status = sh_dmae_tx_status;
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +00001275 shdev->common.device_issue_pending = sh_dmae_memcpy_issue_pending;
Guennadi Liakhovetskicfefe992010-02-03 14:46:41 +00001276
1277 /* Compulsory for DMA_SLAVE fields */
1278 shdev->common.device_prep_slave_sg = sh_dmae_prep_slave_sg;
Linus Walleijc3635c72010-03-26 16:44:01 -07001279 shdev->common.device_control = sh_dmae_control;
Guennadi Liakhovetskicfefe992010-02-03 14:46:41 +00001280
Guennadi Liakhovetskiddb4f0f2009-12-04 19:44:41 +01001281 /* Default transfer size of 32 bytes requires 32-byte alignment */
Guennadi Liakhovetski8b1935e2010-02-11 16:50:14 +00001282 shdev->common.copy_align = LOG2_DEFAULT_XFER_SIZE;
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +00001283
Magnus Damm927a7c92010-03-19 04:47:19 +00001284#if defined(CONFIG_CPU_SH4) || defined(CONFIG_ARCH_SHMOBILE)
Guennadi Liakhovetski027811b2010-02-11 16:50:10 +00001285 chanirq_res = platform_get_resource(pdev, IORESOURCE_IRQ, 1);
1286
1287 if (!chanirq_res)
1288 chanirq_res = errirq_res;
1289 else
1290 irqres++;
1291
1292 if (chanirq_res == errirq_res ||
1293 (errirq_res->flags & IORESOURCE_BITS) == IORESOURCE_IRQ_SHAREABLE)
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +00001294 irqflags = IRQF_SHARED;
Guennadi Liakhovetski027811b2010-02-11 16:50:10 +00001295
1296 errirq = errirq_res->start;
1297
1298 err = request_irq(errirq, sh_dmae_err, irqflags,
1299 "DMAC Address Error", shdev);
1300 if (err) {
1301 dev_err(&pdev->dev,
1302 "DMA failed requesting irq #%d, error %d\n",
1303 errirq, err);
1304 goto eirq_err;
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +00001305 }
1306
Guennadi Liakhovetski027811b2010-02-11 16:50:10 +00001307#else
1308 chanirq_res = errirq_res;
Magnus Damm927a7c92010-03-19 04:47:19 +00001309#endif /* CONFIG_CPU_SH4 || CONFIG_ARCH_SHMOBILE */
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +00001310
Guennadi Liakhovetski027811b2010-02-11 16:50:10 +00001311 if (chanirq_res->start == chanirq_res->end &&
1312 !platform_get_resource(pdev, IORESOURCE_IRQ, 1)) {
1313 /* Special case - all multiplexed */
1314 for (; irq_cnt < pdata->channel_num; irq_cnt++) {
Magnus Damm300e5f92011-05-24 10:31:20 +00001315 if (irq_cnt < SH_DMAC_MAX_CHANNELS) {
1316 chan_irq[irq_cnt] = chanirq_res->start;
1317 chan_flag[irq_cnt] = IRQF_SHARED;
1318 } else {
1319 irq_cap = 1;
1320 break;
1321 }
Guennadi Liakhovetski027811b2010-02-11 16:50:10 +00001322 }
1323 } else {
1324 do {
1325 for (i = chanirq_res->start; i <= chanirq_res->end; i++) {
Magnus Dammdcee0bb2011-06-09 06:35:08 +00001326 if (irq_cnt >= SH_DMAC_MAX_CHANNELS) {
1327 irq_cap = 1;
1328 break;
1329 }
1330
Guennadi Liakhovetski027811b2010-02-11 16:50:10 +00001331 if ((errirq_res->flags & IORESOURCE_BITS) ==
1332 IORESOURCE_IRQ_SHAREABLE)
1333 chan_flag[irq_cnt] = IRQF_SHARED;
1334 else
1335 chan_flag[irq_cnt] = IRQF_DISABLED;
1336 dev_dbg(&pdev->dev,
1337 "Found IRQ %d for channel %d\n",
1338 i, irq_cnt);
1339 chan_irq[irq_cnt++] = i;
Magnus Damm300e5f92011-05-24 10:31:20 +00001340 }
1341
Magnus Dammdcee0bb2011-06-09 06:35:08 +00001342 if (irq_cnt >= SH_DMAC_MAX_CHANNELS)
Magnus Damm300e5f92011-05-24 10:31:20 +00001343 break;
Magnus Dammdcee0bb2011-06-09 06:35:08 +00001344
Guennadi Liakhovetski027811b2010-02-11 16:50:10 +00001345 chanirq_res = platform_get_resource(pdev,
1346 IORESOURCE_IRQ, ++irqres);
1347 } while (irq_cnt < pdata->channel_num && chanirq_res);
1348 }
1349
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +00001350 /* Create DMA Channel */
Magnus Damm300e5f92011-05-24 10:31:20 +00001351 for (i = 0; i < irq_cnt; i++) {
Guennadi Liakhovetski027811b2010-02-11 16:50:10 +00001352 err = sh_dmae_chan_probe(shdev, i, chan_irq[i], chan_flag[i]);
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +00001353 if (err)
1354 goto chan_probe_err;
1355 }
1356
Magnus Damm300e5f92011-05-24 10:31:20 +00001357 if (irq_cap)
1358 dev_notice(&pdev->dev, "Attempting to register %d DMA "
1359 "channels when a maximum of %d are supported.\n",
1360 pdata->channel_num, SH_DMAC_MAX_CHANNELS);
1361
Guennadi Liakhovetski20f2a3b2010-02-11 16:50:18 +00001362 pm_runtime_put(&pdev->dev);
1363
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +00001364 dma_async_device_register(&shdev->common);
1365
1366 return err;
1367
1368chan_probe_err:
1369 sh_dmae_chan_remove(shdev);
Magnus Damm300e5f92011-05-24 10:31:20 +00001370
Magnus Damm927a7c92010-03-19 04:47:19 +00001371#if defined(CONFIG_CPU_SH4) || defined(CONFIG_ARCH_SHMOBILE)
Guennadi Liakhovetski027811b2010-02-11 16:50:10 +00001372 free_irq(errirq, shdev);
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +00001373eirq_err:
Guennadi Liakhovetski027811b2010-02-11 16:50:10 +00001374#endif
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +00001375rst_err:
Guennadi Liakhovetski31705e22011-05-02 07:59:02 +00001376 spin_lock_irq(&sh_dmae_lock);
Paul Mundt03aa18f2010-12-17 19:16:10 +09001377 list_del_rcu(&shdev->node);
Guennadi Liakhovetski31705e22011-05-02 07:59:02 +00001378 spin_unlock_irq(&sh_dmae_lock);
Paul Mundt03aa18f2010-12-17 19:16:10 +09001379
Guennadi Liakhovetski20f2a3b2010-02-11 16:50:18 +00001380 pm_runtime_put(&pdev->dev);
Guennadi Liakhovetski467017b2011-04-29 17:09:25 +00001381 pm_runtime_disable(&pdev->dev);
1382
Guennadi Liakhovetski027811b2010-02-11 16:50:10 +00001383 if (dmars)
1384 iounmap(shdev->dmars);
Paul Mundt5c2de442011-05-31 15:53:03 +09001385
1386 platform_set_drvdata(pdev, NULL);
Guennadi Liakhovetski027811b2010-02-11 16:50:10 +00001387emapdmars:
1388 iounmap(shdev->chan_reg);
Guennadi Liakhovetski31705e22011-05-02 07:59:02 +00001389 synchronize_rcu();
Guennadi Liakhovetski027811b2010-02-11 16:50:10 +00001390emapchan:
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +00001391 kfree(shdev);
Guennadi Liakhovetski027811b2010-02-11 16:50:10 +00001392ealloc:
1393 if (dmars)
1394 release_mem_region(dmars->start, resource_size(dmars));
1395ermrdmars:
1396 release_mem_region(chan->start, resource_size(chan));
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +00001397
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +00001398 return err;
1399}
1400
1401static int __exit sh_dmae_remove(struct platform_device *pdev)
1402{
1403 struct sh_dmae_device *shdev = platform_get_drvdata(pdev);
Guennadi Liakhovetski027811b2010-02-11 16:50:10 +00001404 struct resource *res;
1405 int errirq = platform_get_irq(pdev, 0);
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +00001406
1407 dma_async_device_unregister(&shdev->common);
1408
Guennadi Liakhovetski027811b2010-02-11 16:50:10 +00001409 if (errirq > 0)
1410 free_irq(errirq, shdev);
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +00001411
Guennadi Liakhovetski31705e22011-05-02 07:59:02 +00001412 spin_lock_irq(&sh_dmae_lock);
Paul Mundt03aa18f2010-12-17 19:16:10 +09001413 list_del_rcu(&shdev->node);
Guennadi Liakhovetski31705e22011-05-02 07:59:02 +00001414 spin_unlock_irq(&sh_dmae_lock);
Paul Mundt03aa18f2010-12-17 19:16:10 +09001415
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +00001416 /* channel data remove */
1417 sh_dmae_chan_remove(shdev);
1418
Guennadi Liakhovetski20f2a3b2010-02-11 16:50:18 +00001419 pm_runtime_disable(&pdev->dev);
1420
Guennadi Liakhovetski027811b2010-02-11 16:50:10 +00001421 if (shdev->dmars)
1422 iounmap(shdev->dmars);
1423 iounmap(shdev->chan_reg);
1424
Paul Mundt5c2de442011-05-31 15:53:03 +09001425 platform_set_drvdata(pdev, NULL);
1426
Guennadi Liakhovetski31705e22011-05-02 07:59:02 +00001427 synchronize_rcu();
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +00001428 kfree(shdev);
1429
Guennadi Liakhovetski027811b2010-02-11 16:50:10 +00001430 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1431 if (res)
1432 release_mem_region(res->start, resource_size(res));
1433 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1434 if (res)
1435 release_mem_region(res->start, resource_size(res));
1436
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +00001437 return 0;
1438}
1439
1440static void sh_dmae_shutdown(struct platform_device *pdev)
1441{
1442 struct sh_dmae_device *shdev = platform_get_drvdata(pdev);
Guennadi Liakhovetski027811b2010-02-11 16:50:10 +00001443 sh_dmae_ctl_stop(shdev);
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +00001444}
1445
Guennadi Liakhovetski467017b2011-04-29 17:09:25 +00001446static int sh_dmae_runtime_suspend(struct device *dev)
1447{
1448 return 0;
1449}
1450
1451static int sh_dmae_runtime_resume(struct device *dev)
1452{
1453 struct sh_dmae_device *shdev = dev_get_drvdata(dev);
1454
1455 return sh_dmae_rst(shdev);
1456}
1457
1458#ifdef CONFIG_PM
1459static int sh_dmae_suspend(struct device *dev)
1460{
Guennadi Liakhovetski467017b2011-04-29 17:09:25 +00001461 return 0;
1462}
1463
1464static int sh_dmae_resume(struct device *dev)
1465{
1466 struct sh_dmae_device *shdev = dev_get_drvdata(dev);
Guennadi Liakhovetskic11b46c2012-01-04 15:34:17 +01001467 int i, ret;
1468
1469 ret = sh_dmae_rst(shdev);
1470 if (ret < 0)
1471 dev_err(dev, "Failed to reset!\n");
Guennadi Liakhovetski467017b2011-04-29 17:09:25 +00001472
1473 for (i = 0; i < shdev->pdata->channel_num; i++) {
1474 struct sh_dmae_chan *sh_chan = shdev->chan[i];
1475 struct sh_dmae_slave *param = sh_chan->common.private;
1476
1477 if (!sh_chan->descs_allocated)
1478 continue;
1479
Guennadi Liakhovetski467017b2011-04-29 17:09:25 +00001480 if (param) {
1481 const struct sh_dmae_slave_config *cfg = param->config;
1482 dmae_set_dmars(sh_chan, cfg->mid_rid);
1483 dmae_set_chcr(sh_chan, cfg->chcr);
1484 } else {
1485 dmae_init(sh_chan);
1486 }
1487 }
1488
1489 return 0;
1490}
1491#else
1492#define sh_dmae_suspend NULL
1493#define sh_dmae_resume NULL
1494#endif
1495
1496const struct dev_pm_ops sh_dmae_pm = {
1497 .suspend = sh_dmae_suspend,
1498 .resume = sh_dmae_resume,
1499 .runtime_suspend = sh_dmae_runtime_suspend,
1500 .runtime_resume = sh_dmae_runtime_resume,
1501};
1502
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +00001503static struct platform_driver sh_dmae_driver = {
1504 .remove = __exit_p(sh_dmae_remove),
1505 .shutdown = sh_dmae_shutdown,
1506 .driver = {
Guennadi Liakhovetski7a5c1062010-05-21 15:28:51 +00001507 .owner = THIS_MODULE,
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +00001508 .name = "sh-dma-engine",
Guennadi Liakhovetski467017b2011-04-29 17:09:25 +00001509 .pm = &sh_dmae_pm,
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +00001510 },
1511};
1512
1513static int __init sh_dmae_init(void)
1514{
Guennadi Liakhovetski661382f2011-01-06 17:04:50 +00001515 /* Wire up NMI handling */
1516 int err = register_die_notifier(&sh_dmae_nmi_notifier);
1517 if (err)
1518 return err;
1519
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +00001520 return platform_driver_probe(&sh_dmae_driver, sh_dmae_probe);
1521}
1522module_init(sh_dmae_init);
1523
1524static void __exit sh_dmae_exit(void)
1525{
1526 platform_driver_unregister(&sh_dmae_driver);
Guennadi Liakhovetski661382f2011-01-06 17:04:50 +00001527
1528 unregister_die_notifier(&sh_dmae_nmi_notifier);
Nobuhiro Iwamatsud8902ad2009-09-07 03:26:23 +00001529}
1530module_exit(sh_dmae_exit);
1531
1532MODULE_AUTHOR("Nobuhiro Iwamatsu <iwamatsu.nobuhiro@renesas.com>");
1533MODULE_DESCRIPTION("Renesas SH DMA Engine driver");
1534MODULE_LICENSE("GPL");
Guennadi Liakhovetskie5843342010-11-24 09:48:10 +00001535MODULE_ALIAS("platform:sh-dma-engine");