Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 1 | /* |
| 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 Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 22 | #include <linux/slab.h> |
Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 23 | #include <linux/interrupt.h> |
| 24 | #include <linux/dmaengine.h> |
| 25 | #include <linux/delay.h> |
Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 26 | #include <linux/platform_device.h> |
Guennadi Liakhovetski | 20f2a3b | 2010-02-11 16:50:18 +0000 | [diff] [blame] | 27 | #include <linux/pm_runtime.h> |
Magnus Damm | b2623a6 | 2010-03-19 04:47:10 +0000 | [diff] [blame] | 28 | #include <linux/sh_dma.h> |
Paul Mundt | 03aa18f | 2010-12-17 19:16:10 +0900 | [diff] [blame] | 29 | #include <linux/notifier.h> |
| 30 | #include <linux/kdebug.h> |
| 31 | #include <linux/spinlock.h> |
| 32 | #include <linux/rculist.h> |
Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 33 | #include "shdma.h" |
| 34 | |
| 35 | /* DMA descriptor control */ |
Guennadi Liakhovetski | 3542a11 | 2009-12-17 09:41:39 -0700 | [diff] [blame] | 36 | enum 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 Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 43 | |
| 44 | #define NR_DESCS_PER_CHANNEL 32 |
Guennadi Liakhovetski | 8b1935e | 2010-02-11 16:50:14 +0000 | [diff] [blame] | 45 | /* Default MEMCPY transfer size = 2^2 = 4 bytes */ |
| 46 | #define LOG2_DEFAULT_XFER_SIZE 2 |
Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 47 | |
Paul Mundt | 03aa18f | 2010-12-17 19:16:10 +0900 | [diff] [blame] | 48 | /* |
| 49 | * Used for write-side mutual exclusion for the global device list, |
Guennadi Liakhovetski | 2dc6666 | 2011-04-29 17:09:21 +0000 | [diff] [blame] | 50 | * read-side synchronization by way of RCU, and per-controller data. |
Paul Mundt | 03aa18f | 2010-12-17 19:16:10 +0900 | [diff] [blame] | 51 | */ |
| 52 | static DEFINE_SPINLOCK(sh_dmae_lock); |
| 53 | static LIST_HEAD(sh_dmae_devices); |
| 54 | |
Guennadi Liakhovetski | cfefe99 | 2010-02-03 14:46:41 +0000 | [diff] [blame] | 55 | /* A bitmask with bits enough for enum sh_dmae_slave_chan_id */ |
Magnus Damm | 02ca508 | 2010-03-19 04:46:47 +0000 | [diff] [blame] | 56 | static unsigned long sh_dmae_slave_used[BITS_TO_LONGS(SH_DMA_SLAVE_NUMBER)]; |
Guennadi Liakhovetski | cfefe99 | 2010-02-03 14:46:41 +0000 | [diff] [blame] | 57 | |
Guennadi Liakhovetski | 3542a11 | 2009-12-17 09:41:39 -0700 | [diff] [blame] | 58 | static void sh_dmae_chan_ld_cleanup(struct sh_dmae_chan *sh_chan, bool all); |
Guennadi Liakhovetski | c11b46c | 2012-01-04 15:34:17 +0100 | [diff] [blame] | 59 | static void sh_chan_xfer_ld_queue(struct sh_dmae_chan *sh_chan); |
| 60 | |
| 61 | static 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 Liakhovetski | 3542a11 | 2009-12-17 09:41:39 -0700 | [diff] [blame] | 68 | |
Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 69 | static void sh_dmae_writel(struct sh_dmae_chan *sh_dc, u32 data, u32 reg) |
| 70 | { |
Guennadi Liakhovetski | 027811b | 2010-02-11 16:50:10 +0000 | [diff] [blame] | 71 | __raw_writel(data, sh_dc->base + reg / sizeof(u32)); |
Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | static u32 sh_dmae_readl(struct sh_dmae_chan *sh_dc, u32 reg) |
| 75 | { |
Guennadi Liakhovetski | 027811b | 2010-02-11 16:50:10 +0000 | [diff] [blame] | 76 | return __raw_readl(sh_dc->base + reg / sizeof(u32)); |
| 77 | } |
| 78 | |
| 79 | static u16 dmaor_read(struct sh_dmae_device *shdev) |
| 80 | { |
Kuninori Morimoto | e76c3af | 2011-06-17 08:20:56 +0000 | [diff] [blame] | 81 | 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 Liakhovetski | 027811b | 2010-02-11 16:50:10 +0000 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | static void dmaor_write(struct sh_dmae_device *shdev, u16 data) |
| 90 | { |
Kuninori Morimoto | e76c3af | 2011-06-17 08:20:56 +0000 | [diff] [blame] | 91 | 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 Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 97 | } |
| 98 | |
Kuninori Morimoto | 5899a72 | 2011-06-17 08:20:40 +0000 | [diff] [blame] | 99 | static 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 | |
| 106 | static 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 Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 111 | } |
| 112 | |
Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 113 | /* |
| 114 | * Reset DMA controller |
| 115 | * |
| 116 | * SH7780 has two DMAOR register |
| 117 | */ |
Guennadi Liakhovetski | 027811b | 2010-02-11 16:50:10 +0000 | [diff] [blame] | 118 | static void sh_dmae_ctl_stop(struct sh_dmae_device *shdev) |
Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 119 | { |
Guennadi Liakhovetski | 2dc6666 | 2011-04-29 17:09:21 +0000 | [diff] [blame] | 120 | unsigned short dmaor; |
| 121 | unsigned long flags; |
Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 122 | |
Guennadi Liakhovetski | 2dc6666 | 2011-04-29 17:09:21 +0000 | [diff] [blame] | 123 | spin_lock_irqsave(&sh_dmae_lock, flags); |
| 124 | |
| 125 | dmaor = dmaor_read(shdev); |
Guennadi Liakhovetski | 027811b | 2010-02-11 16:50:10 +0000 | [diff] [blame] | 126 | dmaor_write(shdev, dmaor & ~(DMAOR_NMIF | DMAOR_AE | DMAOR_DME)); |
Guennadi Liakhovetski | 2dc6666 | 2011-04-29 17:09:21 +0000 | [diff] [blame] | 127 | |
| 128 | spin_unlock_irqrestore(&sh_dmae_lock, flags); |
Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 129 | } |
| 130 | |
Guennadi Liakhovetski | 027811b | 2010-02-11 16:50:10 +0000 | [diff] [blame] | 131 | static int sh_dmae_rst(struct sh_dmae_device *shdev) |
Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 132 | { |
| 133 | unsigned short dmaor; |
Guennadi Liakhovetski | 2dc6666 | 2011-04-29 17:09:21 +0000 | [diff] [blame] | 134 | unsigned long flags; |
Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 135 | |
Guennadi Liakhovetski | 2dc6666 | 2011-04-29 17:09:21 +0000 | [diff] [blame] | 136 | spin_lock_irqsave(&sh_dmae_lock, flags); |
Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 137 | |
Guennadi Liakhovetski | 2dc6666 | 2011-04-29 17:09:21 +0000 | [diff] [blame] | 138 | dmaor = dmaor_read(shdev) & ~(DMAOR_NMIF | DMAOR_AE | DMAOR_DME); |
| 139 | |
Guennadi Liakhovetski | c11b46c | 2012-01-04 15:34:17 +0100 | [diff] [blame] | 140 | 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 Liakhovetski | 2dc6666 | 2011-04-29 17:09:21 +0000 | [diff] [blame] | 149 | 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 Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 158 | } |
Guennadi Liakhovetski | c11b46c | 2012-01-04 15:34:17 +0100 | [diff] [blame] | 159 | 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 Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 163 | return 0; |
| 164 | } |
| 165 | |
Guennadi Liakhovetski | fc46185 | 2010-01-19 07:24:55 +0000 | [diff] [blame] | 166 | static bool dmae_is_busy(struct sh_dmae_chan *sh_chan) |
Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 167 | { |
Kuninori Morimoto | 5899a72 | 2011-06-17 08:20:40 +0000 | [diff] [blame] | 168 | u32 chcr = chcr_read(sh_chan); |
Guennadi Liakhovetski | fc46185 | 2010-01-19 07:24:55 +0000 | [diff] [blame] | 169 | |
| 170 | if ((chcr & (CHCR_DE | CHCR_TE)) == CHCR_DE) |
| 171 | return true; /* working */ |
| 172 | |
| 173 | return false; /* waiting */ |
Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 174 | } |
| 175 | |
Guennadi Liakhovetski | 8b1935e | 2010-02-11 16:50:14 +0000 | [diff] [blame] | 176 | static unsigned int calc_xmit_shift(struct sh_dmae_chan *sh_chan, u32 chcr) |
Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 177 | { |
Kuninori Morimoto | c4e0dd7 | 2011-06-16 05:08:09 +0000 | [diff] [blame] | 178 | struct sh_dmae_device *shdev = to_sh_dev(sh_chan); |
Guennadi Liakhovetski | 8b1935e | 2010-02-11 16:50:14 +0000 | [diff] [blame] | 179 | 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 Liakhovetski | 623b4ac | 2010-02-03 14:44:12 +0000 | [diff] [blame] | 182 | |
Guennadi Liakhovetski | 8b1935e | 2010-02-11 16:50:14 +0000 | [diff] [blame] | 183 | if (cnt >= pdata->ts_shift_num) |
| 184 | cnt = 0; |
| 185 | |
| 186 | return pdata->ts_shift[cnt]; |
| 187 | } |
| 188 | |
| 189 | static u32 log2size_to_chcr(struct sh_dmae_chan *sh_chan, int l2size) |
| 190 | { |
Kuninori Morimoto | c4e0dd7 | 2011-06-16 05:08:09 +0000 | [diff] [blame] | 191 | struct sh_dmae_device *shdev = to_sh_dev(sh_chan); |
Guennadi Liakhovetski | 8b1935e | 2010-02-11 16:50:14 +0000 | [diff] [blame] | 192 | 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 Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 204 | } |
| 205 | |
Guennadi Liakhovetski | 3542a11 | 2009-12-17 09:41:39 -0700 | [diff] [blame] | 206 | static void dmae_set_reg(struct sh_dmae_chan *sh_chan, struct sh_dmae_regs *hw) |
Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 207 | { |
Guennadi Liakhovetski | 3542a11 | 2009-12-17 09:41:39 -0700 | [diff] [blame] | 208 | sh_dmae_writel(sh_chan, hw->sar, SAR); |
| 209 | sh_dmae_writel(sh_chan, hw->dar, DAR); |
Guennadi Liakhovetski | cfefe99 | 2010-02-03 14:46:41 +0000 | [diff] [blame] | 210 | sh_dmae_writel(sh_chan, hw->tcr >> sh_chan->xmit_shift, TCR); |
Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | static void dmae_start(struct sh_dmae_chan *sh_chan) |
| 214 | { |
Kuninori Morimoto | 67c6269 | 2011-06-17 08:20:51 +0000 | [diff] [blame] | 215 | struct sh_dmae_device *shdev = to_sh_dev(sh_chan); |
Kuninori Morimoto | 5899a72 | 2011-06-17 08:20:40 +0000 | [diff] [blame] | 216 | u32 chcr = chcr_read(sh_chan); |
Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 217 | |
Kuninori Morimoto | 260bf2c | 2011-06-17 08:21:05 +0000 | [diff] [blame] | 218 | if (shdev->pdata->needs_tend_set) |
| 219 | sh_dmae_writel(sh_chan, 0xFFFFFFFF, TEND); |
| 220 | |
Kuninori Morimoto | 67c6269 | 2011-06-17 08:20:51 +0000 | [diff] [blame] | 221 | chcr |= CHCR_DE | shdev->chcr_ie_bit; |
Kuninori Morimoto | 5899a72 | 2011-06-17 08:20:40 +0000 | [diff] [blame] | 222 | chcr_write(sh_chan, chcr & ~CHCR_TE); |
Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | static void dmae_halt(struct sh_dmae_chan *sh_chan) |
| 226 | { |
Kuninori Morimoto | 67c6269 | 2011-06-17 08:20:51 +0000 | [diff] [blame] | 227 | struct sh_dmae_device *shdev = to_sh_dev(sh_chan); |
Kuninori Morimoto | 5899a72 | 2011-06-17 08:20:40 +0000 | [diff] [blame] | 228 | u32 chcr = chcr_read(sh_chan); |
Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 229 | |
Kuninori Morimoto | 67c6269 | 2011-06-17 08:20:51 +0000 | [diff] [blame] | 230 | chcr &= ~(CHCR_DE | CHCR_TE | shdev->chcr_ie_bit); |
Kuninori Morimoto | 5899a72 | 2011-06-17 08:20:40 +0000 | [diff] [blame] | 231 | chcr_write(sh_chan, chcr); |
Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 232 | } |
| 233 | |
Guennadi Liakhovetski | cfefe99 | 2010-02-03 14:46:41 +0000 | [diff] [blame] | 234 | static void dmae_init(struct sh_dmae_chan *sh_chan) |
| 235 | { |
Guennadi Liakhovetski | 8b1935e | 2010-02-11 16:50:14 +0000 | [diff] [blame] | 236 | /* |
| 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 Morimoto | 5899a72 | 2011-06-17 08:20:40 +0000 | [diff] [blame] | 243 | chcr_write(sh_chan, chcr); |
Guennadi Liakhovetski | cfefe99 | 2010-02-03 14:46:41 +0000 | [diff] [blame] | 244 | } |
| 245 | |
Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 246 | static int dmae_set_chcr(struct sh_dmae_chan *sh_chan, u32 val) |
| 247 | { |
Guennadi Liakhovetski | 2dc6666 | 2011-04-29 17:09:21 +0000 | [diff] [blame] | 248 | /* If DMA is active, cannot set CHCR. TODO: remove this superfluous check */ |
Guennadi Liakhovetski | fc46185 | 2010-01-19 07:24:55 +0000 | [diff] [blame] | 249 | if (dmae_is_busy(sh_chan)) |
| 250 | return -EBUSY; |
Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 251 | |
Guennadi Liakhovetski | 8b1935e | 2010-02-11 16:50:14 +0000 | [diff] [blame] | 252 | sh_chan->xmit_shift = calc_xmit_shift(sh_chan, val); |
Kuninori Morimoto | 5899a72 | 2011-06-17 08:20:40 +0000 | [diff] [blame] | 253 | chcr_write(sh_chan, val); |
Guennadi Liakhovetski | cfefe99 | 2010-02-03 14:46:41 +0000 | [diff] [blame] | 254 | |
Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 255 | return 0; |
| 256 | } |
| 257 | |
Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 258 | static int dmae_set_dmars(struct sh_dmae_chan *sh_chan, u16 val) |
| 259 | { |
Kuninori Morimoto | c4e0dd7 | 2011-06-16 05:08:09 +0000 | [diff] [blame] | 260 | struct sh_dmae_device *shdev = to_sh_dev(sh_chan); |
Guennadi Liakhovetski | 027811b | 2010-02-11 16:50:10 +0000 | [diff] [blame] | 261 | struct sh_dmae_pdata *pdata = shdev->pdata; |
Guennadi Liakhovetski | 5bac942 | 2010-04-21 15:36:49 +0000 | [diff] [blame] | 262 | const struct sh_dmae_channel *chan_pdata = &pdata->channel[sh_chan->id]; |
Magnus Damm | 26fc02a | 2011-05-24 10:31:12 +0000 | [diff] [blame] | 263 | u16 __iomem *addr = shdev->dmars; |
Kuninori Morimoto | 090b918 | 2011-06-16 05:08:28 +0000 | [diff] [blame] | 264 | unsigned int shift = chan_pdata->dmars_bit; |
Guennadi Liakhovetski | fc46185 | 2010-01-19 07:24:55 +0000 | [diff] [blame] | 265 | |
| 266 | if (dmae_is_busy(sh_chan)) |
| 267 | return -EBUSY; |
Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 268 | |
Kuninori Morimoto | 260bf2c | 2011-06-17 08:21:05 +0000 | [diff] [blame] | 269 | if (pdata->no_dmars) |
| 270 | return 0; |
| 271 | |
Magnus Damm | 26fc02a | 2011-05-24 10:31:12 +0000 | [diff] [blame] | 272 | /* 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 Liakhovetski | 027811b | 2010-02-11 16:50:10 +0000 | [diff] [blame] | 277 | __raw_writew((__raw_readw(addr) & (0xff00 >> shift)) | (val << shift), |
| 278 | addr); |
Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 279 | |
| 280 | return 0; |
| 281 | } |
| 282 | |
| 283 | static dma_cookie_t sh_dmae_tx_submit(struct dma_async_tx_descriptor *tx) |
| 284 | { |
Guennadi Liakhovetski | 3542a11 | 2009-12-17 09:41:39 -0700 | [diff] [blame] | 285 | struct sh_desc *desc = tx_to_sh_desc(tx), *chunk, *last = desc, *c; |
Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 286 | struct sh_dmae_chan *sh_chan = to_sh_chan(tx->chan); |
Guennadi Liakhovetski | 7a1cd9a | 2011-08-18 16:55:27 +0200 | [diff] [blame] | 287 | struct sh_dmae_slave *param = tx->chan->private; |
Guennadi Liakhovetski | 3542a11 | 2009-12-17 09:41:39 -0700 | [diff] [blame] | 288 | dma_async_tx_callback callback = tx->callback; |
Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 289 | dma_cookie_t cookie; |
Guennadi Liakhovetski | 7a1cd9a | 2011-08-18 16:55:27 +0200 | [diff] [blame] | 290 | bool power_up; |
Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 291 | |
Guennadi Liakhovetski | 7a1cd9a | 2011-08-18 16:55:27 +0200 | [diff] [blame] | 292 | 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 Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 298 | |
| 299 | cookie = sh_chan->common.cookie; |
| 300 | cookie++; |
| 301 | if (cookie < 0) |
| 302 | cookie = 1; |
| 303 | |
Guennadi Liakhovetski | 3542a11 | 2009-12-17 09:41:39 -0700 | [diff] [blame] | 304 | sh_chan->common.cookie = cookie; |
| 305 | tx->cookie = cookie; |
Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 306 | |
Guennadi Liakhovetski | 3542a11 | 2009-12-17 09:41:39 -0700 | [diff] [blame] | 307 | /* 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 Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 332 | |
Guennadi Liakhovetski | 7a1cd9a | 2011-08-18 16:55:27 +0200 | [diff] [blame] | 333 | 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 Liakhovetski | c11b46c | 2012-01-04 15:34:17 +0100 | [diff] [blame] | 362 | } else { |
| 363 | sh_chan->pm_state = DMAE_PM_PENDING; |
Guennadi Liakhovetski | 7a1cd9a | 2011-08-18 16:55:27 +0200 | [diff] [blame] | 364 | } |
| 365 | |
| 366 | spin_unlock_irq(&sh_chan->desc_lock); |
Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 367 | |
| 368 | return cookie; |
| 369 | } |
| 370 | |
Guennadi Liakhovetski | 3542a11 | 2009-12-17 09:41:39 -0700 | [diff] [blame] | 371 | /* Called with desc_lock held */ |
Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 372 | static struct sh_desc *sh_dmae_get_desc(struct sh_dmae_chan *sh_chan) |
| 373 | { |
Guennadi Liakhovetski | 3542a11 | 2009-12-17 09:41:39 -0700 | [diff] [blame] | 374 | struct sh_desc *desc; |
Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 375 | |
Guennadi Liakhovetski | 3542a11 | 2009-12-17 09:41:39 -0700 | [diff] [blame] | 376 | list_for_each_entry(desc, &sh_chan->ld_free, node) |
| 377 | if (desc->mark != DESC_PREPARED) { |
| 378 | BUG_ON(desc->mark != DESC_IDLE); |
Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 379 | list_del(&desc->node); |
Guennadi Liakhovetski | 3542a11 | 2009-12-17 09:41:39 -0700 | [diff] [blame] | 380 | return desc; |
Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 381 | } |
Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 382 | |
Guennadi Liakhovetski | 3542a11 | 2009-12-17 09:41:39 -0700 | [diff] [blame] | 383 | return NULL; |
Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 384 | } |
| 385 | |
Guennadi Liakhovetski | 5bac942 | 2010-04-21 15:36:49 +0000 | [diff] [blame] | 386 | static const struct sh_dmae_slave_config *sh_dmae_find_slave( |
Magnus Damm | 4bab9d4 | 2010-03-19 04:46:38 +0000 | [diff] [blame] | 387 | struct sh_dmae_chan *sh_chan, struct sh_dmae_slave *param) |
Guennadi Liakhovetski | cfefe99 | 2010-02-03 14:46:41 +0000 | [diff] [blame] | 388 | { |
Kuninori Morimoto | c4e0dd7 | 2011-06-16 05:08:09 +0000 | [diff] [blame] | 389 | struct sh_dmae_device *shdev = to_sh_dev(sh_chan); |
Guennadi Liakhovetski | 027811b | 2010-02-11 16:50:10 +0000 | [diff] [blame] | 390 | struct sh_dmae_pdata *pdata = shdev->pdata; |
Guennadi Liakhovetski | cfefe99 | 2010-02-03 14:46:41 +0000 | [diff] [blame] | 391 | int i; |
| 392 | |
Magnus Damm | 02ca508 | 2010-03-19 04:46:47 +0000 | [diff] [blame] | 393 | if (param->slave_id >= SH_DMA_SLAVE_NUMBER) |
Guennadi Liakhovetski | cfefe99 | 2010-02-03 14:46:41 +0000 | [diff] [blame] | 394 | return NULL; |
| 395 | |
Guennadi Liakhovetski | 027811b | 2010-02-11 16:50:10 +0000 | [diff] [blame] | 396 | for (i = 0; i < pdata->slave_num; i++) |
Magnus Damm | 4bab9d4 | 2010-03-19 04:46:38 +0000 | [diff] [blame] | 397 | if (pdata->slave[i].slave_id == param->slave_id) |
Guennadi Liakhovetski | 027811b | 2010-02-11 16:50:10 +0000 | [diff] [blame] | 398 | return pdata->slave + i; |
Guennadi Liakhovetski | cfefe99 | 2010-02-03 14:46:41 +0000 | [diff] [blame] | 399 | |
| 400 | return NULL; |
| 401 | } |
| 402 | |
Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 403 | static 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 Liakhovetski | cfefe99 | 2010-02-03 14:46:41 +0000 | [diff] [blame] | 407 | struct sh_dmae_slave *param = chan->private; |
Guennadi Liakhovetski | 83515bc | 2010-04-19 08:39:39 +0000 | [diff] [blame] | 408 | int ret; |
Guennadi Liakhovetski | cfefe99 | 2010-02-03 14:46:41 +0000 | [diff] [blame] | 409 | |
| 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 Liakhovetski | 5bac942 | 2010-04-21 15:36:49 +0000 | [diff] [blame] | 415 | const struct sh_dmae_slave_config *cfg; |
Guennadi Liakhovetski | cfefe99 | 2010-02-03 14:46:41 +0000 | [diff] [blame] | 416 | |
Magnus Damm | 4bab9d4 | 2010-03-19 04:46:38 +0000 | [diff] [blame] | 417 | cfg = sh_dmae_find_slave(sh_chan, param); |
Guennadi Liakhovetski | 83515bc | 2010-04-19 08:39:39 +0000 | [diff] [blame] | 418 | if (!cfg) { |
| 419 | ret = -EINVAL; |
| 420 | goto efindslave; |
| 421 | } |
Guennadi Liakhovetski | cfefe99 | 2010-02-03 14:46:41 +0000 | [diff] [blame] | 422 | |
Guennadi Liakhovetski | 83515bc | 2010-04-19 08:39:39 +0000 | [diff] [blame] | 423 | if (test_and_set_bit(param->slave_id, sh_dmae_slave_used)) { |
| 424 | ret = -EBUSY; |
| 425 | goto etestused; |
| 426 | } |
Guennadi Liakhovetski | cfefe99 | 2010-02-03 14:46:41 +0000 | [diff] [blame] | 427 | |
| 428 | param->config = cfg; |
Guennadi Liakhovetski | cfefe99 | 2010-02-03 14:46:41 +0000 | [diff] [blame] | 429 | } |
Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 430 | |
Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 431 | while (sh_chan->descs_allocated < NR_DESCS_PER_CHANNEL) { |
Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 432 | desc = kzalloc(sizeof(struct sh_desc), GFP_KERNEL); |
Guennadi Liakhovetski | b4dae6e | 2011-09-25 16:12:18 +0200 | [diff] [blame] | 433 | if (!desc) |
Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 434 | break; |
Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 435 | dma_async_tx_descriptor_init(&desc->async_tx, |
| 436 | &sh_chan->common); |
| 437 | desc->async_tx.tx_submit = sh_dmae_tx_submit; |
Guennadi Liakhovetski | 3542a11 | 2009-12-17 09:41:39 -0700 | [diff] [blame] | 438 | desc->mark = DESC_IDLE; |
Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 439 | |
Guennadi Liakhovetski | 3542a11 | 2009-12-17 09:41:39 -0700 | [diff] [blame] | 440 | list_add(&desc->node, &sh_chan->ld_free); |
Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 441 | sh_chan->descs_allocated++; |
| 442 | } |
Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 443 | |
Guennadi Liakhovetski | 83515bc | 2010-04-19 08:39:39 +0000 | [diff] [blame] | 444 | if (!sh_chan->descs_allocated) { |
| 445 | ret = -ENOMEM; |
| 446 | goto edescalloc; |
| 447 | } |
Guennadi Liakhovetski | 20f2a3b | 2010-02-11 16:50:18 +0000 | [diff] [blame] | 448 | |
Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 449 | return sh_chan->descs_allocated; |
Guennadi Liakhovetski | 83515bc | 2010-04-19 08:39:39 +0000 | [diff] [blame] | 450 | |
| 451 | edescalloc: |
| 452 | if (param) |
| 453 | clear_bit(param->slave_id, sh_dmae_slave_used); |
| 454 | etestused: |
| 455 | efindslave: |
Guennadi Liakhovetski | b4dae6e | 2011-09-25 16:12:18 +0200 | [diff] [blame] | 456 | chan->private = NULL; |
Guennadi Liakhovetski | 83515bc | 2010-04-19 08:39:39 +0000 | [diff] [blame] | 457 | return ret; |
Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 458 | } |
| 459 | |
| 460 | /* |
| 461 | * sh_dma_free_chan_resources - Free all resources of the channel. |
| 462 | */ |
| 463 | static 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 Liakhovetski | 2dc6666 | 2011-04-29 17:09:21 +0000 | [diff] [blame] | 469 | /* Protect against ISR */ |
| 470 | spin_lock_irq(&sh_chan->desc_lock); |
Guennadi Liakhovetski | cfefe99 | 2010-02-03 14:46:41 +0000 | [diff] [blame] | 471 | dmae_halt(sh_chan); |
Guennadi Liakhovetski | 2dc6666 | 2011-04-29 17:09:21 +0000 | [diff] [blame] | 472 | spin_unlock_irq(&sh_chan->desc_lock); |
| 473 | |
| 474 | /* Now no new interrupts will occur */ |
Guennadi Liakhovetski | cfefe99 | 2010-02-03 14:46:41 +0000 | [diff] [blame] | 475 | |
Guennadi Liakhovetski | 3542a11 | 2009-12-17 09:41:39 -0700 | [diff] [blame] | 476 | /* 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 Liakhovetski | cfefe99 | 2010-02-03 14:46:41 +0000 | [diff] [blame] | 480 | 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 Liakhovetski | 2dc6666 | 2011-04-29 17:09:21 +0000 | [diff] [blame] | 484 | chan->private = NULL; |
Guennadi Liakhovetski | cfefe99 | 2010-02-03 14:46:41 +0000 | [diff] [blame] | 485 | } |
| 486 | |
Guennadi Liakhovetski | b4dae6e | 2011-09-25 16:12:18 +0200 | [diff] [blame] | 487 | spin_lock_irq(&sh_chan->desc_lock); |
Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 488 | |
| 489 | list_splice_init(&sh_chan->ld_free, &list); |
| 490 | sh_chan->descs_allocated = 0; |
| 491 | |
Guennadi Liakhovetski | b4dae6e | 2011-09-25 16:12:18 +0200 | [diff] [blame] | 492 | spin_unlock_irq(&sh_chan->desc_lock); |
Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 493 | |
| 494 | list_for_each_entry_safe(desc, _desc, &list, node) |
| 495 | kfree(desc); |
| 496 | } |
| 497 | |
Guennadi Liakhovetski | cfefe99 | 2010-02-03 14:46:41 +0000 | [diff] [blame] | 498 | /** |
Guennadi Liakhovetski | fc46185 | 2010-01-19 07:24:55 +0000 | [diff] [blame] | 499 | * 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 Koul | db8196d | 2011-10-13 22:34:23 +0530 | [diff] [blame] | 503 | * DMA_DEV_TO_MEM |
Guennadi Liakhovetski | fc46185 | 2010-01-19 07:24:55 +0000 | [diff] [blame] | 504 | * @src: source DMA address, incremented when direction equals |
Vinod Koul | db8196d | 2011-10-13 22:34:23 +0530 | [diff] [blame] | 505 | * DMA_MEM_TO_DEV |
Guennadi Liakhovetski | fc46185 | 2010-01-19 07:24:55 +0000 | [diff] [blame] | 506 | * @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 Koul | db8196d | 2011-10-13 22:34:23 +0530 | [diff] [blame] | 509 | * equals DMA_MEM_TO_MEM for MEMCPY |
Guennadi Liakhovetski | fc46185 | 2010-01-19 07:24:55 +0000 | [diff] [blame] | 510 | * Returns 0 or an error |
| 511 | * Locks: called with desc_lock held |
| 512 | */ |
| 513 | static 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 Koul | db8196d | 2011-10-13 22:34:23 +0530 | [diff] [blame] | 515 | struct sh_desc **first, enum dma_transfer_direction direction) |
Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 516 | { |
Guennadi Liakhovetski | fc46185 | 2010-01-19 07:24:55 +0000 | [diff] [blame] | 517 | struct sh_desc *new; |
Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 518 | size_t copy_size; |
Guennadi Liakhovetski | fc46185 | 2010-01-19 07:24:55 +0000 | [diff] [blame] | 519 | |
| 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 Liakhovetski | cfefe99 | 2010-02-03 14:46:41 +0000 | [diff] [blame] | 545 | dev_dbg(sh_chan->dev, |
| 546 | "chaining (%u/%u)@%x -> %x with %p, cookie %d, shift %d\n", |
Guennadi Liakhovetski | fc46185 | 2010-01-19 07:24:55 +0000 | [diff] [blame] | 547 | copy_size, *len, *src, *dest, &new->async_tx, |
Guennadi Liakhovetski | cfefe99 | 2010-02-03 14:46:41 +0000 | [diff] [blame] | 548 | new->async_tx.cookie, sh_chan->xmit_shift); |
Guennadi Liakhovetski | fc46185 | 2010-01-19 07:24:55 +0000 | [diff] [blame] | 549 | |
| 550 | new->mark = DESC_PREPARED; |
| 551 | new->async_tx.flags = flags; |
Guennadi Liakhovetski | cfefe99 | 2010-02-03 14:46:41 +0000 | [diff] [blame] | 552 | new->direction = direction; |
Guennadi Liakhovetski | fc46185 | 2010-01-19 07:24:55 +0000 | [diff] [blame] | 553 | |
| 554 | *len -= copy_size; |
Vinod Koul | db8196d | 2011-10-13 22:34:23 +0530 | [diff] [blame] | 555 | if (direction == DMA_MEM_TO_MEM || direction == DMA_MEM_TO_DEV) |
Guennadi Liakhovetski | fc46185 | 2010-01-19 07:24:55 +0000 | [diff] [blame] | 556 | *src += copy_size; |
Vinod Koul | db8196d | 2011-10-13 22:34:23 +0530 | [diff] [blame] | 557 | if (direction == DMA_MEM_TO_MEM || direction == DMA_DEV_TO_MEM) |
Guennadi Liakhovetski | fc46185 | 2010-01-19 07:24:55 +0000 | [diff] [blame] | 558 | *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 Koul | db8196d | 2011-10-13 22:34:23 +0530 | [diff] [blame] | 570 | * e.g., the FIFO I/O register. For MEMCPY direction equals DMA_MEM_TO_MEM |
Guennadi Liakhovetski | fc46185 | 2010-01-19 07:24:55 +0000 | [diff] [blame] | 571 | * and the SG list contains only one element and points at the source buffer. |
| 572 | */ |
| 573 | static 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 Koul | db8196d | 2011-10-13 22:34:23 +0530 | [diff] [blame] | 575 | enum dma_transfer_direction direction, unsigned long flags) |
Guennadi Liakhovetski | fc46185 | 2010-01-19 07:24:55 +0000 | [diff] [blame] | 576 | { |
| 577 | struct scatterlist *sg; |
| 578 | struct sh_desc *first = NULL, *new = NULL /* compiler... */; |
Guennadi Liakhovetski | 3542a11 | 2009-12-17 09:41:39 -0700 | [diff] [blame] | 579 | LIST_HEAD(tx_list); |
Guennadi Liakhovetski | fc46185 | 2010-01-19 07:24:55 +0000 | [diff] [blame] | 580 | int chunks = 0; |
Guennadi Liakhovetski | b4dae6e | 2011-09-25 16:12:18 +0200 | [diff] [blame] | 581 | unsigned long irq_flags; |
Guennadi Liakhovetski | fc46185 | 2010-01-19 07:24:55 +0000 | [diff] [blame] | 582 | int i; |
Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 583 | |
Guennadi Liakhovetski | fc46185 | 2010-01-19 07:24:55 +0000 | [diff] [blame] | 584 | if (!sg_len) |
Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 585 | return NULL; |
| 586 | |
Guennadi Liakhovetski | fc46185 | 2010-01-19 07:24:55 +0000 | [diff] [blame] | 587 | 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 Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 590 | |
Guennadi Liakhovetski | 3542a11 | 2009-12-17 09:41:39 -0700 | [diff] [blame] | 591 | /* Have to lock the whole loop to protect against concurrent release */ |
Guennadi Liakhovetski | b4dae6e | 2011-09-25 16:12:18 +0200 | [diff] [blame] | 592 | spin_lock_irqsave(&sh_chan->desc_lock, irq_flags); |
Guennadi Liakhovetski | 3542a11 | 2009-12-17 09:41:39 -0700 | [diff] [blame] | 593 | |
| 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 Liakhovetski | fc46185 | 2010-01-19 07:24:55 +0000 | [diff] [blame] | 605 | 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 Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 608 | |
Guennadi Liakhovetski | fc46185 | 2010-01-19 07:24:55 +0000 | [diff] [blame] | 609 | if (!len) |
| 610 | goto err_get_desc; |
Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 611 | |
Guennadi Liakhovetski | fc46185 | 2010-01-19 07:24:55 +0000 | [diff] [blame] | 612 | 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 Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 615 | |
Vinod Koul | db8196d | 2011-10-13 22:34:23 +0530 | [diff] [blame] | 616 | if (direction == DMA_DEV_TO_MEM) |
Guennadi Liakhovetski | fc46185 | 2010-01-19 07:24:55 +0000 | [diff] [blame] | 617 | 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 Liakhovetski | 3542a11 | 2009-12-17 09:41:39 -0700 | [diff] [blame] | 626 | |
Guennadi Liakhovetski | fc46185 | 2010-01-19 07:24:55 +0000 | [diff] [blame] | 627 | new->chunks = chunks--; |
| 628 | list_add_tail(&new->node, &tx_list); |
| 629 | } while (len); |
| 630 | } |
Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 631 | |
Guennadi Liakhovetski | 3542a11 | 2009-12-17 09:41:39 -0700 | [diff] [blame] | 632 | 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 Liakhovetski | b4dae6e | 2011-09-25 16:12:18 +0200 | [diff] [blame] | 638 | spin_unlock_irqrestore(&sh_chan->desc_lock, irq_flags); |
Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 639 | |
| 640 | return &first->async_tx; |
Guennadi Liakhovetski | fc46185 | 2010-01-19 07:24:55 +0000 | [diff] [blame] | 641 | |
| 642 | err_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 Liakhovetski | b4dae6e | 2011-09-25 16:12:18 +0200 | [diff] [blame] | 647 | spin_unlock_irqrestore(&sh_chan->desc_lock, irq_flags); |
Guennadi Liakhovetski | fc46185 | 2010-01-19 07:24:55 +0000 | [diff] [blame] | 648 | |
| 649 | return NULL; |
| 650 | } |
| 651 | |
| 652 | static 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 Koul | db8196d | 2011-10-13 22:34:23 +0530 | [diff] [blame] | 670 | return sh_dmae_prep_sg(sh_chan, &sg, 1, &dma_dest, DMA_MEM_TO_MEM, |
Guennadi Liakhovetski | fc46185 | 2010-01-19 07:24:55 +0000 | [diff] [blame] | 671 | flags); |
Guennadi Liakhovetski | 3542a11 | 2009-12-17 09:41:39 -0700 | [diff] [blame] | 672 | } |
Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 673 | |
Guennadi Liakhovetski | cfefe99 | 2010-02-03 14:46:41 +0000 | [diff] [blame] | 674 | static struct dma_async_tx_descriptor *sh_dmae_prep_slave_sg( |
| 675 | struct dma_chan *chan, struct scatterlist *sgl, unsigned int sg_len, |
Vinod Koul | db8196d | 2011-10-13 22:34:23 +0530 | [diff] [blame] | 676 | enum dma_transfer_direction direction, unsigned long flags) |
Guennadi Liakhovetski | cfefe99 | 2010-02-03 14:46:41 +0000 | [diff] [blame] | 677 | { |
| 678 | struct sh_dmae_slave *param; |
| 679 | struct sh_dmae_chan *sh_chan; |
Guennadi Liakhovetski | 5bac942 | 2010-04-21 15:36:49 +0000 | [diff] [blame] | 680 | dma_addr_t slave_addr; |
Guennadi Liakhovetski | cfefe99 | 2010-02-03 14:46:41 +0000 | [diff] [blame] | 681 | |
| 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 Carpenter | 9f9ff20 | 2010-08-14 11:01:45 +0200 | [diff] [blame] | 695 | slave_addr = param->config->addr; |
| 696 | |
Guennadi Liakhovetski | cfefe99 | 2010-02-03 14:46:41 +0000 | [diff] [blame] | 697 | /* |
| 698 | * if (param != NULL), this is a successfully requested slave channel, |
| 699 | * therefore param->config != NULL too. |
| 700 | */ |
Guennadi Liakhovetski | 5bac942 | 2010-04-21 15:36:49 +0000 | [diff] [blame] | 701 | return sh_dmae_prep_sg(sh_chan, sgl, sg_len, &slave_addr, |
Guennadi Liakhovetski | cfefe99 | 2010-02-03 14:46:41 +0000 | [diff] [blame] | 702 | direction, flags); |
| 703 | } |
| 704 | |
Linus Walleij | 0582763 | 2010-05-17 16:30:42 -0700 | [diff] [blame] | 705 | static int sh_dmae_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd, |
| 706 | unsigned long arg) |
Guennadi Liakhovetski | cfefe99 | 2010-02-03 14:46:41 +0000 | [diff] [blame] | 707 | { |
| 708 | struct sh_dmae_chan *sh_chan = to_sh_chan(chan); |
Guennadi Liakhovetski | b4dae6e | 2011-09-25 16:12:18 +0200 | [diff] [blame] | 709 | unsigned long flags; |
Guennadi Liakhovetski | cfefe99 | 2010-02-03 14:46:41 +0000 | [diff] [blame] | 710 | |
Linus Walleij | c3635c7 | 2010-03-26 16:44:01 -0700 | [diff] [blame] | 711 | /* Only supports DMA_TERMINATE_ALL */ |
| 712 | if (cmd != DMA_TERMINATE_ALL) |
| 713 | return -ENXIO; |
| 714 | |
Guennadi Liakhovetski | cfefe99 | 2010-02-03 14:46:41 +0000 | [diff] [blame] | 715 | if (!chan) |
Linus Walleij | c3635c7 | 2010-03-26 16:44:01 -0700 | [diff] [blame] | 716 | return -EINVAL; |
Guennadi Liakhovetski | cfefe99 | 2010-02-03 14:46:41 +0000 | [diff] [blame] | 717 | |
Guennadi Liakhovetski | b4dae6e | 2011-09-25 16:12:18 +0200 | [diff] [blame] | 718 | spin_lock_irqsave(&sh_chan->desc_lock, flags); |
Guennadi Liakhovetski | c014906 | 2010-02-18 16:30:02 +0000 | [diff] [blame] | 719 | dmae_halt(sh_chan); |
| 720 | |
Guennadi Liakhovetski | c014906 | 2010-02-18 16:30:02 +0000 | [diff] [blame] | 721 | 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 Liakhovetski | c014906 | 2010-02-18 16:30:02 +0000 | [diff] [blame] | 727 | } |
Guennadi Liakhovetski | b4dae6e | 2011-09-25 16:12:18 +0200 | [diff] [blame] | 728 | spin_unlock_irqrestore(&sh_chan->desc_lock, flags); |
Guennadi Liakhovetski | c014906 | 2010-02-18 16:30:02 +0000 | [diff] [blame] | 729 | |
Guennadi Liakhovetski | cfefe99 | 2010-02-03 14:46:41 +0000 | [diff] [blame] | 730 | sh_dmae_chan_ld_cleanup(sh_chan, true); |
Linus Walleij | c3635c7 | 2010-03-26 16:44:01 -0700 | [diff] [blame] | 731 | |
| 732 | return 0; |
Guennadi Liakhovetski | cfefe99 | 2010-02-03 14:46:41 +0000 | [diff] [blame] | 733 | } |
| 734 | |
Guennadi Liakhovetski | 3542a11 | 2009-12-17 09:41:39 -0700 | [diff] [blame] | 735 | static 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 Liakhovetski | b4dae6e | 2011-09-25 16:12:18 +0200 | [diff] [blame] | 743 | unsigned long flags; |
Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 744 | |
Guennadi Liakhovetski | b4dae6e | 2011-09-25 16:12:18 +0200 | [diff] [blame] | 745 | spin_lock_irqsave(&sh_chan->desc_lock, flags); |
Guennadi Liakhovetski | 3542a11 | 2009-12-17 09:41:39 -0700 | [diff] [blame] | 746 | 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 Liakhovetski | cfefe99 | 2010-02-03 14:46:41 +0000 | [diff] [blame] | 767 | 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 Liakhovetski | 3542a11 | 2009-12-17 09:41:39 -0700 | [diff] [blame] | 772 | 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 Liakhovetski | 7a1cd9a | 2011-08-18 16:55:27 +0200 | [diff] [blame] | 811 | |
Guennadi Liakhovetski | 3542a11 | 2009-12-17 09:41:39 -0700 | [diff] [blame] | 812 | list_move(&desc->node, &sh_chan->ld_free); |
Guennadi Liakhovetski | 7a1cd9a | 2011-08-18 16:55:27 +0200 | [diff] [blame] | 813 | |
| 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 Liakhovetski | 3542a11 | 2009-12-17 09:41:39 -0700 | [diff] [blame] | 818 | } |
| 819 | } |
Guennadi Liakhovetski | 2dc6666 | 2011-04-29 17:09:21 +0000 | [diff] [blame] | 820 | |
| 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 Liakhovetski | b4dae6e | 2011-09-25 16:12:18 +0200 | [diff] [blame] | 828 | spin_unlock_irqrestore(&sh_chan->desc_lock, flags); |
Guennadi Liakhovetski | 3542a11 | 2009-12-17 09:41:39 -0700 | [diff] [blame] | 829 | |
| 830 | if (callback) |
| 831 | callback(param); |
| 832 | |
| 833 | return callback; |
Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 834 | } |
| 835 | |
| 836 | /* |
| 837 | * sh_chan_ld_cleanup - Clean up link descriptors |
| 838 | * |
Guennadi Liakhovetski | 3542a11 | 2009-12-17 09:41:39 -0700 | [diff] [blame] | 839 | * This function cleans up the ld_queue of DMA channel. |
Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 840 | */ |
Guennadi Liakhovetski | 3542a11 | 2009-12-17 09:41:39 -0700 | [diff] [blame] | 841 | static void sh_dmae_chan_ld_cleanup(struct sh_dmae_chan *sh_chan, bool all) |
Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 842 | { |
Guennadi Liakhovetski | 3542a11 | 2009-12-17 09:41:39 -0700 | [diff] [blame] | 843 | while (__ld_cleanup(sh_chan, all)) |
| 844 | ; |
Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 845 | } |
| 846 | |
Guennadi Liakhovetski | 7a1cd9a | 2011-08-18 16:55:27 +0200 | [diff] [blame] | 847 | /* Called under spin_lock_irq(&sh_chan->desc_lock) */ |
Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 848 | static void sh_chan_xfer_ld_queue(struct sh_dmae_chan *sh_chan) |
| 849 | { |
Guennadi Liakhovetski | 47a4dc2 | 2010-02-11 16:50:05 +0000 | [diff] [blame] | 850 | struct sh_desc *desc; |
Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 851 | |
| 852 | /* DMA work check */ |
Guennadi Liakhovetski | 7a1cd9a | 2011-08-18 16:55:27 +0200 | [diff] [blame] | 853 | if (dmae_is_busy(sh_chan)) |
Guennadi Liakhovetski | b4dae6e | 2011-09-25 16:12:18 +0200 | [diff] [blame] | 854 | return; |
Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 855 | |
Justin P. Mattock | 5a3a765 | 2011-01-19 15:36:38 +0100 | [diff] [blame] | 856 | /* Find the first not transferred descriptor */ |
Guennadi Liakhovetski | 47a4dc2 | 2010-02-11 16:50:05 +0000 | [diff] [blame] | 857 | list_for_each_entry(desc, &sh_chan->ld_queue, node) |
| 858 | if (desc->mark == DESC_SUBMITTED) { |
Guennadi Liakhovetski | c014906 | 2010-02-18 16:30:02 +0000 | [diff] [blame] | 859 | 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 Liakhovetski | 3542a11 | 2009-12-17 09:41:39 -0700 | [diff] [blame] | 862 | /* Get the ld start address from ld_queue */ |
Guennadi Liakhovetski | 47a4dc2 | 2010-02-11 16:50:05 +0000 | [diff] [blame] | 863 | dmae_set_reg(sh_chan, &desc->hw); |
Guennadi Liakhovetski | 3542a11 | 2009-12-17 09:41:39 -0700 | [diff] [blame] | 864 | dmae_start(sh_chan); |
| 865 | break; |
| 866 | } |
Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 867 | } |
| 868 | |
| 869 | static void sh_dmae_memcpy_issue_pending(struct dma_chan *chan) |
| 870 | { |
| 871 | struct sh_dmae_chan *sh_chan = to_sh_chan(chan); |
Guennadi Liakhovetski | 7a1cd9a | 2011-08-18 16:55:27 +0200 | [diff] [blame] | 872 | |
| 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 Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 879 | } |
| 880 | |
Linus Walleij | 0793448 | 2010-03-26 16:50:49 -0700 | [diff] [blame] | 881 | static enum dma_status sh_dmae_tx_status(struct dma_chan *chan, |
Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 882 | dma_cookie_t cookie, |
Linus Walleij | 0793448 | 2010-03-26 16:50:49 -0700 | [diff] [blame] | 883 | struct dma_tx_state *txstate) |
Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 884 | { |
| 885 | struct sh_dmae_chan *sh_chan = to_sh_chan(chan); |
| 886 | dma_cookie_t last_used; |
| 887 | dma_cookie_t last_complete; |
Guennadi Liakhovetski | 47a4dc2 | 2010-02-11 16:50:05 +0000 | [diff] [blame] | 888 | enum dma_status status; |
Guennadi Liakhovetski | b4dae6e | 2011-09-25 16:12:18 +0200 | [diff] [blame] | 889 | unsigned long flags; |
Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 890 | |
Guennadi Liakhovetski | 3542a11 | 2009-12-17 09:41:39 -0700 | [diff] [blame] | 891 | sh_dmae_chan_ld_cleanup(sh_chan, false); |
Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 892 | |
Guennadi Liakhovetski | 2dc6666 | 2011-04-29 17:09:21 +0000 | [diff] [blame] | 893 | /* First read completed cookie to avoid a skew */ |
Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 894 | last_complete = sh_chan->completed_cookie; |
Guennadi Liakhovetski | 2dc6666 | 2011-04-29 17:09:21 +0000 | [diff] [blame] | 895 | rmb(); |
| 896 | last_used = chan->cookie; |
Guennadi Liakhovetski | 3542a11 | 2009-12-17 09:41:39 -0700 | [diff] [blame] | 897 | BUG_ON(last_complete < 0); |
Dan Williams | bca3469 | 2010-03-26 16:52:10 -0700 | [diff] [blame] | 898 | dma_set_tx_state(txstate, last_complete, last_used, 0); |
Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 899 | |
Guennadi Liakhovetski | b4dae6e | 2011-09-25 16:12:18 +0200 | [diff] [blame] | 900 | spin_lock_irqsave(&sh_chan->desc_lock, flags); |
Guennadi Liakhovetski | 47a4dc2 | 2010-02-11 16:50:05 +0000 | [diff] [blame] | 901 | |
| 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 Liakhovetski | b4dae6e | 2011-09-25 16:12:18 +0200 | [diff] [blame] | 918 | spin_unlock_irqrestore(&sh_chan->desc_lock, flags); |
Guennadi Liakhovetski | 47a4dc2 | 2010-02-11 16:50:05 +0000 | [diff] [blame] | 919 | |
| 920 | return status; |
Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 921 | } |
| 922 | |
| 923 | static irqreturn_t sh_dmae_interrupt(int irq, void *data) |
| 924 | { |
| 925 | irqreturn_t ret = IRQ_NONE; |
Guennadi Liakhovetski | 2dc6666 | 2011-04-29 17:09:21 +0000 | [diff] [blame] | 926 | struct sh_dmae_chan *sh_chan = data; |
| 927 | u32 chcr; |
| 928 | |
| 929 | spin_lock(&sh_chan->desc_lock); |
| 930 | |
Kuninori Morimoto | 5899a72 | 2011-06-17 08:20:40 +0000 | [diff] [blame] | 931 | chcr = chcr_read(sh_chan); |
Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 932 | |
| 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 Liakhovetski | 2dc6666 | 2011-04-29 17:09:21 +0000 | [diff] [blame] | 941 | spin_unlock(&sh_chan->desc_lock); |
| 942 | |
Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 943 | return ret; |
| 944 | } |
| 945 | |
Guennadi Liakhovetski | 2dc6666 | 2011-04-29 17:09:21 +0000 | [diff] [blame] | 946 | /* Called from error IRQ or NMI */ |
| 947 | static bool sh_dmae_reset(struct sh_dmae_device *shdev) |
Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 948 | { |
Paul Mundt | 03aa18f | 2010-12-17 19:16:10 +0900 | [diff] [blame] | 949 | unsigned int handled = 0; |
Guennadi Liakhovetski | 47a4dc2 | 2010-02-11 16:50:05 +0000 | [diff] [blame] | 950 | int i; |
Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 951 | |
Guennadi Liakhovetski | 47a4dc2 | 2010-02-11 16:50:05 +0000 | [diff] [blame] | 952 | /* halt the dma controller */ |
Guennadi Liakhovetski | 027811b | 2010-02-11 16:50:10 +0000 | [diff] [blame] | 953 | sh_dmae_ctl_stop(shdev); |
Guennadi Liakhovetski | 47a4dc2 | 2010-02-11 16:50:05 +0000 | [diff] [blame] | 954 | |
| 955 | /* We cannot detect, which channel caused the error, have to reset all */ |
Guennadi Liakhovetski | 8b1935e | 2010-02-11 16:50:14 +0000 | [diff] [blame] | 956 | for (i = 0; i < SH_DMAC_MAX_CHANNELS; i++) { |
Guennadi Liakhovetski | 47a4dc2 | 2010-02-11 16:50:05 +0000 | [diff] [blame] | 957 | struct sh_dmae_chan *sh_chan = shdev->chan[i]; |
Paul Mundt | 03aa18f | 2010-12-17 19:16:10 +0900 | [diff] [blame] | 958 | struct sh_desc *desc; |
Guennadi Liakhovetski | 2dc6666 | 2011-04-29 17:09:21 +0000 | [diff] [blame] | 959 | LIST_HEAD(dl); |
Paul Mundt | 03aa18f | 2010-12-17 19:16:10 +0900 | [diff] [blame] | 960 | |
| 961 | if (!sh_chan) |
| 962 | continue; |
| 963 | |
Guennadi Liakhovetski | 2dc6666 | 2011-04-29 17:09:21 +0000 | [diff] [blame] | 964 | spin_lock(&sh_chan->desc_lock); |
| 965 | |
Paul Mundt | 03aa18f | 2010-12-17 19:16:10 +0900 | [diff] [blame] | 966 | /* Stop the channel */ |
| 967 | dmae_halt(sh_chan); |
| 968 | |
Guennadi Liakhovetski | 2dc6666 | 2011-04-29 17:09:21 +0000 | [diff] [blame] | 969 | list_splice_init(&sh_chan->ld_queue, &dl); |
| 970 | |
Guennadi Liakhovetski | 7a1cd9a | 2011-08-18 16:55:27 +0200 | [diff] [blame] | 971 | 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 Liakhovetski | 2dc6666 | 2011-04-29 17:09:21 +0000 | [diff] [blame] | 977 | spin_unlock(&sh_chan->desc_lock); |
| 978 | |
Paul Mundt | 03aa18f | 2010-12-17 19:16:10 +0900 | [diff] [blame] | 979 | /* Complete all */ |
Guennadi Liakhovetski | 2dc6666 | 2011-04-29 17:09:21 +0000 | [diff] [blame] | 980 | list_for_each_entry(desc, &dl, node) { |
Paul Mundt | 03aa18f | 2010-12-17 19:16:10 +0900 | [diff] [blame] | 981 | 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 Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 985 | } |
Paul Mundt | 03aa18f | 2010-12-17 19:16:10 +0900 | [diff] [blame] | 986 | |
Guennadi Liakhovetski | 2dc6666 | 2011-04-29 17:09:21 +0000 | [diff] [blame] | 987 | spin_lock(&sh_chan->desc_lock); |
| 988 | list_splice(&dl, &sh_chan->ld_free); |
| 989 | spin_unlock(&sh_chan->desc_lock); |
| 990 | |
Paul Mundt | 03aa18f | 2010-12-17 19:16:10 +0900 | [diff] [blame] | 991 | handled++; |
Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 992 | } |
Paul Mundt | 03aa18f | 2010-12-17 19:16:10 +0900 | [diff] [blame] | 993 | |
Guennadi Liakhovetski | 027811b | 2010-02-11 16:50:10 +0000 | [diff] [blame] | 994 | sh_dmae_rst(shdev); |
Guennadi Liakhovetski | 47a4dc2 | 2010-02-11 16:50:05 +0000 | [diff] [blame] | 995 | |
Paul Mundt | 03aa18f | 2010-12-17 19:16:10 +0900 | [diff] [blame] | 996 | return !!handled; |
Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 997 | } |
Paul Mundt | 03aa18f | 2010-12-17 19:16:10 +0900 | [diff] [blame] | 998 | |
| 999 | static irqreturn_t sh_dmae_err(int irq, void *data) |
| 1000 | { |
Yoshihiro Shimoda | ff7690b | 2011-02-09 07:46:47 +0000 | [diff] [blame] | 1001 | struct sh_dmae_device *shdev = data; |
| 1002 | |
Guennadi Liakhovetski | 2dc6666 | 2011-04-29 17:09:21 +0000 | [diff] [blame] | 1003 | if (!(dmaor_read(shdev) & DMAOR_AE)) |
Yoshihiro Shimoda | ff7690b | 2011-02-09 07:46:47 +0000 | [diff] [blame] | 1004 | return IRQ_NONE; |
Guennadi Liakhovetski | 2dc6666 | 2011-04-29 17:09:21 +0000 | [diff] [blame] | 1005 | |
| 1006 | sh_dmae_reset(data); |
| 1007 | return IRQ_HANDLED; |
Paul Mundt | 03aa18f | 2010-12-17 19:16:10 +0900 | [diff] [blame] | 1008 | } |
Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 1009 | |
| 1010 | static void dmae_do_tasklet(unsigned long data) |
| 1011 | { |
| 1012 | struct sh_dmae_chan *sh_chan = (struct sh_dmae_chan *)data; |
Guennadi Liakhovetski | 3542a11 | 2009-12-17 09:41:39 -0700 | [diff] [blame] | 1013 | struct sh_desc *desc; |
Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 1014 | u32 sar_buf = sh_dmae_readl(sh_chan, SAR); |
Guennadi Liakhovetski | cfefe99 | 2010-02-03 14:46:41 +0000 | [diff] [blame] | 1015 | u32 dar_buf = sh_dmae_readl(sh_chan, DAR); |
Guennadi Liakhovetski | 86d61b3 | 2009-12-10 18:35:07 +0100 | [diff] [blame] | 1016 | |
Guennadi Liakhovetski | b4dae6e | 2011-09-25 16:12:18 +0200 | [diff] [blame] | 1017 | spin_lock_irq(&sh_chan->desc_lock); |
Guennadi Liakhovetski | 3542a11 | 2009-12-17 09:41:39 -0700 | [diff] [blame] | 1018 | list_for_each_entry(desc, &sh_chan->ld_queue, node) { |
Guennadi Liakhovetski | cfefe99 | 2010-02-03 14:46:41 +0000 | [diff] [blame] | 1019 | if (desc->mark == DESC_SUBMITTED && |
Vinod Koul | db8196d | 2011-10-13 22:34:23 +0530 | [diff] [blame] | 1020 | ((desc->direction == DMA_DEV_TO_MEM && |
Guennadi Liakhovetski | cfefe99 | 2010-02-03 14:46:41 +0000 | [diff] [blame] | 1021 | (desc->hw.dar + desc->hw.tcr) == dar_buf) || |
| 1022 | (desc->hw.sar + desc->hw.tcr) == sar_buf)) { |
Guennadi Liakhovetski | 3542a11 | 2009-12-17 09:41:39 -0700 | [diff] [blame] | 1023 | 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 Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 1027 | break; |
| 1028 | } |
| 1029 | } |
Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 1030 | /* Next desc */ |
| 1031 | sh_chan_xfer_ld_queue(sh_chan); |
Guennadi Liakhovetski | 7a1cd9a | 2011-08-18 16:55:27 +0200 | [diff] [blame] | 1032 | spin_unlock_irq(&sh_chan->desc_lock); |
| 1033 | |
Guennadi Liakhovetski | 3542a11 | 2009-12-17 09:41:39 -0700 | [diff] [blame] | 1034 | sh_dmae_chan_ld_cleanup(sh_chan, false); |
Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 1035 | } |
| 1036 | |
Paul Mundt | 03aa18f | 2010-12-17 19:16:10 +0900 | [diff] [blame] | 1037 | static bool sh_dmae_nmi_notify(struct sh_dmae_device *shdev) |
| 1038 | { |
Paul Mundt | 03aa18f | 2010-12-17 19:16:10 +0900 | [diff] [blame] | 1039 | /* Fast path out if NMIF is not asserted for this controller */ |
| 1040 | if ((dmaor_read(shdev) & DMAOR_NMIF) == 0) |
| 1041 | return false; |
| 1042 | |
Guennadi Liakhovetski | 2dc6666 | 2011-04-29 17:09:21 +0000 | [diff] [blame] | 1043 | return sh_dmae_reset(shdev); |
Paul Mundt | 03aa18f | 2010-12-17 19:16:10 +0900 | [diff] [blame] | 1044 | } |
| 1045 | |
| 1046 | static 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 | |
| 1078 | static 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 Liakhovetski | 027811b | 2010-02-11 16:50:10 +0000 | [diff] [blame] | 1085 | static int __devinit sh_dmae_chan_probe(struct sh_dmae_device *shdev, int id, |
| 1086 | int irq, unsigned long flags) |
Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 1087 | { |
| 1088 | int err; |
Guennadi Liakhovetski | 5bac942 | 2010-04-21 15:36:49 +0000 | [diff] [blame] | 1089 | const struct sh_dmae_channel *chan_pdata = &shdev->pdata->channel[id]; |
Guennadi Liakhovetski | 027811b | 2010-02-11 16:50:10 +0000 | [diff] [blame] | 1090 | struct platform_device *pdev = to_platform_device(shdev->common.dev); |
Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 1091 | 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 Liakhovetski | 86d61b3 | 2009-12-10 18:35:07 +0100 | [diff] [blame] | 1096 | dev_err(shdev->common.dev, |
| 1097 | "No free memory for allocating dma channels!\n"); |
Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 1098 | return -ENOMEM; |
| 1099 | } |
| 1100 | |
Guennadi Liakhovetski | 7a1cd9a | 2011-08-18 16:55:27 +0200 | [diff] [blame] | 1101 | new_sh_chan->pm_state = DMAE_PM_ESTABLISHED; |
| 1102 | |
| 1103 | /* reference struct dma_device */ |
Guennadi Liakhovetski | 8b1935e | 2010-02-11 16:50:14 +0000 | [diff] [blame] | 1104 | new_sh_chan->common.device = &shdev->common; |
| 1105 | |
Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 1106 | new_sh_chan->dev = shdev->common.dev; |
| 1107 | new_sh_chan->id = id; |
Guennadi Liakhovetski | 027811b | 2010-02-11 16:50:10 +0000 | [diff] [blame] | 1108 | new_sh_chan->irq = irq; |
| 1109 | new_sh_chan->base = shdev->chan_reg + chan_pdata->offset / sizeof(u32); |
Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 1110 | |
| 1111 | /* Init DMA tasklet */ |
| 1112 | tasklet_init(&new_sh_chan->tasklet, dmae_do_tasklet, |
| 1113 | (unsigned long)new_sh_chan); |
| 1114 | |
Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 1115 | 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 Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 1121 | /* 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 Liakhovetski | 027811b | 2010-02-11 16:50:10 +0000 | [diff] [blame] | 1126 | 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 Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 1132 | |
| 1133 | /* set up channel irq */ |
Guennadi Liakhovetski | 027811b | 2010-02-11 16:50:10 +0000 | [diff] [blame] | 1134 | err = request_irq(irq, &sh_dmae_interrupt, flags, |
Guennadi Liakhovetski | 86d61b3 | 2009-12-10 18:35:07 +0100 | [diff] [blame] | 1135 | new_sh_chan->dev_id, new_sh_chan); |
Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 1136 | 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 Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 1142 | shdev->chan[id] = new_sh_chan; |
| 1143 | return 0; |
| 1144 | |
| 1145 | err_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 | |
| 1152 | static 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 Liakhovetski | 027811b | 2010-02-11 16:50:10 +0000 | [diff] [blame] | 1158 | struct sh_dmae_chan *sh_chan = shdev->chan[i]; |
Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 1159 | |
Guennadi Liakhovetski | 027811b | 2010-02-11 16:50:10 +0000 | [diff] [blame] | 1160 | free_irq(sh_chan->irq, sh_chan); |
| 1161 | |
| 1162 | list_del(&sh_chan->common.device_node); |
| 1163 | kfree(sh_chan); |
Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 1164 | shdev->chan[i] = NULL; |
| 1165 | } |
| 1166 | } |
| 1167 | shdev->common.chancnt = 0; |
| 1168 | } |
| 1169 | |
| 1170 | static int __init sh_dmae_probe(struct platform_device *pdev) |
| 1171 | { |
Guennadi Liakhovetski | 027811b | 2010-02-11 16:50:10 +0000 | [diff] [blame] | 1172 | struct sh_dmae_pdata *pdata = pdev->dev.platform_data; |
| 1173 | unsigned long irqflags = IRQF_DISABLED, |
Guennadi Liakhovetski | 8b1935e | 2010-02-11 16:50:14 +0000 | [diff] [blame] | 1174 | chan_flag[SH_DMAC_MAX_CHANNELS] = {}; |
| 1175 | int errirq, chan_irq[SH_DMAC_MAX_CHANNELS]; |
Magnus Damm | 300e5f9 | 2011-05-24 10:31:20 +0000 | [diff] [blame] | 1176 | int err, i, irq_cnt = 0, irqres = 0, irq_cap = 0; |
Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 1177 | struct sh_dmae_device *shdev; |
Guennadi Liakhovetski | 027811b | 2010-02-11 16:50:10 +0000 | [diff] [blame] | 1178 | struct resource *chan, *dmars, *errirq_res, *chanirq_res; |
Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 1179 | |
Dan Williams | 56adf7e | 2009-11-22 12:10:10 -0700 | [diff] [blame] | 1180 | /* get platform data */ |
Guennadi Liakhovetski | 027811b | 2010-02-11 16:50:10 +0000 | [diff] [blame] | 1181 | if (!pdata || !pdata->channel_num) |
Dan Williams | 56adf7e | 2009-11-22 12:10:10 -0700 | [diff] [blame] | 1182 | return -ENODEV; |
| 1183 | |
Guennadi Liakhovetski | 027811b | 2010-02-11 16:50:10 +0000 | [diff] [blame] | 1184 | chan = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
Magnus Damm | 26fc02a | 2011-05-24 10:31:12 +0000 | [diff] [blame] | 1185 | /* DMARS area is optional */ |
Guennadi Liakhovetski | 027811b | 2010-02-11 16:50:10 +0000 | [diff] [blame] | 1186 | 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 Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 1219 | shdev = kzalloc(sizeof(struct sh_dmae_device), GFP_KERNEL); |
| 1220 | if (!shdev) { |
Guennadi Liakhovetski | 027811b | 2010-02-11 16:50:10 +0000 | [diff] [blame] | 1221 | 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 Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 1232 | } |
| 1233 | |
Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 1234 | /* platform data */ |
Guennadi Liakhovetski | 027811b | 2010-02-11 16:50:10 +0000 | [diff] [blame] | 1235 | shdev->pdata = pdata; |
Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 1236 | |
Kuninori Morimoto | 5899a72 | 2011-06-17 08:20:40 +0000 | [diff] [blame] | 1237 | if (pdata->chcr_offset) |
| 1238 | shdev->chcr_offset = pdata->chcr_offset; |
| 1239 | else |
| 1240 | shdev->chcr_offset = CHCR; |
| 1241 | |
Kuninori Morimoto | 67c6269 | 2011-06-17 08:20:51 +0000 | [diff] [blame] | 1242 | 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 Mundt | 5c2de44 | 2011-05-31 15:53:03 +0900 | [diff] [blame] | 1247 | platform_set_drvdata(pdev, shdev); |
| 1248 | |
Guennadi Liakhovetski | c11b46c | 2012-01-04 15:34:17 +0100 | [diff] [blame] | 1249 | shdev->common.dev = &pdev->dev; |
| 1250 | |
Guennadi Liakhovetski | 20f2a3b | 2010-02-11 16:50:18 +0000 | [diff] [blame] | 1251 | pm_runtime_enable(&pdev->dev); |
| 1252 | pm_runtime_get_sync(&pdev->dev); |
| 1253 | |
Guennadi Liakhovetski | 31705e2 | 2011-05-02 07:59:02 +0000 | [diff] [blame] | 1254 | spin_lock_irq(&sh_dmae_lock); |
Paul Mundt | 03aa18f | 2010-12-17 19:16:10 +0900 | [diff] [blame] | 1255 | list_add_tail_rcu(&shdev->node, &sh_dmae_devices); |
Guennadi Liakhovetski | 31705e2 | 2011-05-02 07:59:02 +0000 | [diff] [blame] | 1256 | spin_unlock_irq(&sh_dmae_lock); |
Paul Mundt | 03aa18f | 2010-12-17 19:16:10 +0900 | [diff] [blame] | 1257 | |
Guennadi Liakhovetski | 2dc6666 | 2011-04-29 17:09:21 +0000 | [diff] [blame] | 1258 | /* reset dma controller - only needed as a test */ |
Guennadi Liakhovetski | 027811b | 2010-02-11 16:50:10 +0000 | [diff] [blame] | 1259 | err = sh_dmae_rst(shdev); |
Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 1260 | if (err) |
| 1261 | goto rst_err; |
| 1262 | |
Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 1263 | INIT_LIST_HEAD(&shdev->common.channels); |
| 1264 | |
Guennadi Liakhovetski | e9c8d7a | 2012-01-18 10:14:25 +0100 | [diff] [blame] | 1265 | if (!pdata->slave_only) |
| 1266 | dma_cap_set(DMA_MEMCPY, shdev->common.cap_mask); |
Magnus Damm | 26fc02a | 2011-05-24 10:31:12 +0000 | [diff] [blame] | 1267 | if (pdata->slave && pdata->slave_num) |
Guennadi Liakhovetski | 027811b | 2010-02-11 16:50:10 +0000 | [diff] [blame] | 1268 | dma_cap_set(DMA_SLAVE, shdev->common.cap_mask); |
Guennadi Liakhovetski | cfefe99 | 2010-02-03 14:46:41 +0000 | [diff] [blame] | 1269 | |
Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 1270 | 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 Walleij | 0793448 | 2010-03-26 16:50:49 -0700 | [diff] [blame] | 1274 | shdev->common.device_tx_status = sh_dmae_tx_status; |
Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 1275 | shdev->common.device_issue_pending = sh_dmae_memcpy_issue_pending; |
Guennadi Liakhovetski | cfefe99 | 2010-02-03 14:46:41 +0000 | [diff] [blame] | 1276 | |
| 1277 | /* Compulsory for DMA_SLAVE fields */ |
| 1278 | shdev->common.device_prep_slave_sg = sh_dmae_prep_slave_sg; |
Linus Walleij | c3635c7 | 2010-03-26 16:44:01 -0700 | [diff] [blame] | 1279 | shdev->common.device_control = sh_dmae_control; |
Guennadi Liakhovetski | cfefe99 | 2010-02-03 14:46:41 +0000 | [diff] [blame] | 1280 | |
Guennadi Liakhovetski | ddb4f0f | 2009-12-04 19:44:41 +0100 | [diff] [blame] | 1281 | /* Default transfer size of 32 bytes requires 32-byte alignment */ |
Guennadi Liakhovetski | 8b1935e | 2010-02-11 16:50:14 +0000 | [diff] [blame] | 1282 | shdev->common.copy_align = LOG2_DEFAULT_XFER_SIZE; |
Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 1283 | |
Magnus Damm | 927a7c9 | 2010-03-19 04:47:19 +0000 | [diff] [blame] | 1284 | #if defined(CONFIG_CPU_SH4) || defined(CONFIG_ARCH_SHMOBILE) |
Guennadi Liakhovetski | 027811b | 2010-02-11 16:50:10 +0000 | [diff] [blame] | 1285 | 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 Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 1294 | irqflags = IRQF_SHARED; |
Guennadi Liakhovetski | 027811b | 2010-02-11 16:50:10 +0000 | [diff] [blame] | 1295 | |
| 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 Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 1305 | } |
| 1306 | |
Guennadi Liakhovetski | 027811b | 2010-02-11 16:50:10 +0000 | [diff] [blame] | 1307 | #else |
| 1308 | chanirq_res = errirq_res; |
Magnus Damm | 927a7c9 | 2010-03-19 04:47:19 +0000 | [diff] [blame] | 1309 | #endif /* CONFIG_CPU_SH4 || CONFIG_ARCH_SHMOBILE */ |
Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 1310 | |
Guennadi Liakhovetski | 027811b | 2010-02-11 16:50:10 +0000 | [diff] [blame] | 1311 | 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 Damm | 300e5f9 | 2011-05-24 10:31:20 +0000 | [diff] [blame] | 1315 | 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 Liakhovetski | 027811b | 2010-02-11 16:50:10 +0000 | [diff] [blame] | 1322 | } |
| 1323 | } else { |
| 1324 | do { |
| 1325 | for (i = chanirq_res->start; i <= chanirq_res->end; i++) { |
Magnus Damm | dcee0bb | 2011-06-09 06:35:08 +0000 | [diff] [blame] | 1326 | if (irq_cnt >= SH_DMAC_MAX_CHANNELS) { |
| 1327 | irq_cap = 1; |
| 1328 | break; |
| 1329 | } |
| 1330 | |
Guennadi Liakhovetski | 027811b | 2010-02-11 16:50:10 +0000 | [diff] [blame] | 1331 | 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 Damm | 300e5f9 | 2011-05-24 10:31:20 +0000 | [diff] [blame] | 1340 | } |
| 1341 | |
Magnus Damm | dcee0bb | 2011-06-09 06:35:08 +0000 | [diff] [blame] | 1342 | if (irq_cnt >= SH_DMAC_MAX_CHANNELS) |
Magnus Damm | 300e5f9 | 2011-05-24 10:31:20 +0000 | [diff] [blame] | 1343 | break; |
Magnus Damm | dcee0bb | 2011-06-09 06:35:08 +0000 | [diff] [blame] | 1344 | |
Guennadi Liakhovetski | 027811b | 2010-02-11 16:50:10 +0000 | [diff] [blame] | 1345 | chanirq_res = platform_get_resource(pdev, |
| 1346 | IORESOURCE_IRQ, ++irqres); |
| 1347 | } while (irq_cnt < pdata->channel_num && chanirq_res); |
| 1348 | } |
| 1349 | |
Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 1350 | /* Create DMA Channel */ |
Magnus Damm | 300e5f9 | 2011-05-24 10:31:20 +0000 | [diff] [blame] | 1351 | for (i = 0; i < irq_cnt; i++) { |
Guennadi Liakhovetski | 027811b | 2010-02-11 16:50:10 +0000 | [diff] [blame] | 1352 | err = sh_dmae_chan_probe(shdev, i, chan_irq[i], chan_flag[i]); |
Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 1353 | if (err) |
| 1354 | goto chan_probe_err; |
| 1355 | } |
| 1356 | |
Magnus Damm | 300e5f9 | 2011-05-24 10:31:20 +0000 | [diff] [blame] | 1357 | 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 Liakhovetski | 20f2a3b | 2010-02-11 16:50:18 +0000 | [diff] [blame] | 1362 | pm_runtime_put(&pdev->dev); |
| 1363 | |
Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 1364 | dma_async_device_register(&shdev->common); |
| 1365 | |
| 1366 | return err; |
| 1367 | |
| 1368 | chan_probe_err: |
| 1369 | sh_dmae_chan_remove(shdev); |
Magnus Damm | 300e5f9 | 2011-05-24 10:31:20 +0000 | [diff] [blame] | 1370 | |
Magnus Damm | 927a7c9 | 2010-03-19 04:47:19 +0000 | [diff] [blame] | 1371 | #if defined(CONFIG_CPU_SH4) || defined(CONFIG_ARCH_SHMOBILE) |
Guennadi Liakhovetski | 027811b | 2010-02-11 16:50:10 +0000 | [diff] [blame] | 1372 | free_irq(errirq, shdev); |
Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 1373 | eirq_err: |
Guennadi Liakhovetski | 027811b | 2010-02-11 16:50:10 +0000 | [diff] [blame] | 1374 | #endif |
Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 1375 | rst_err: |
Guennadi Liakhovetski | 31705e2 | 2011-05-02 07:59:02 +0000 | [diff] [blame] | 1376 | spin_lock_irq(&sh_dmae_lock); |
Paul Mundt | 03aa18f | 2010-12-17 19:16:10 +0900 | [diff] [blame] | 1377 | list_del_rcu(&shdev->node); |
Guennadi Liakhovetski | 31705e2 | 2011-05-02 07:59:02 +0000 | [diff] [blame] | 1378 | spin_unlock_irq(&sh_dmae_lock); |
Paul Mundt | 03aa18f | 2010-12-17 19:16:10 +0900 | [diff] [blame] | 1379 | |
Guennadi Liakhovetski | 20f2a3b | 2010-02-11 16:50:18 +0000 | [diff] [blame] | 1380 | pm_runtime_put(&pdev->dev); |
Guennadi Liakhovetski | 467017b | 2011-04-29 17:09:25 +0000 | [diff] [blame] | 1381 | pm_runtime_disable(&pdev->dev); |
| 1382 | |
Guennadi Liakhovetski | 027811b | 2010-02-11 16:50:10 +0000 | [diff] [blame] | 1383 | if (dmars) |
| 1384 | iounmap(shdev->dmars); |
Paul Mundt | 5c2de44 | 2011-05-31 15:53:03 +0900 | [diff] [blame] | 1385 | |
| 1386 | platform_set_drvdata(pdev, NULL); |
Guennadi Liakhovetski | 027811b | 2010-02-11 16:50:10 +0000 | [diff] [blame] | 1387 | emapdmars: |
| 1388 | iounmap(shdev->chan_reg); |
Guennadi Liakhovetski | 31705e2 | 2011-05-02 07:59:02 +0000 | [diff] [blame] | 1389 | synchronize_rcu(); |
Guennadi Liakhovetski | 027811b | 2010-02-11 16:50:10 +0000 | [diff] [blame] | 1390 | emapchan: |
Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 1391 | kfree(shdev); |
Guennadi Liakhovetski | 027811b | 2010-02-11 16:50:10 +0000 | [diff] [blame] | 1392 | ealloc: |
| 1393 | if (dmars) |
| 1394 | release_mem_region(dmars->start, resource_size(dmars)); |
| 1395 | ermrdmars: |
| 1396 | release_mem_region(chan->start, resource_size(chan)); |
Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 1397 | |
Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 1398 | return err; |
| 1399 | } |
| 1400 | |
| 1401 | static int __exit sh_dmae_remove(struct platform_device *pdev) |
| 1402 | { |
| 1403 | struct sh_dmae_device *shdev = platform_get_drvdata(pdev); |
Guennadi Liakhovetski | 027811b | 2010-02-11 16:50:10 +0000 | [diff] [blame] | 1404 | struct resource *res; |
| 1405 | int errirq = platform_get_irq(pdev, 0); |
Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 1406 | |
| 1407 | dma_async_device_unregister(&shdev->common); |
| 1408 | |
Guennadi Liakhovetski | 027811b | 2010-02-11 16:50:10 +0000 | [diff] [blame] | 1409 | if (errirq > 0) |
| 1410 | free_irq(errirq, shdev); |
Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 1411 | |
Guennadi Liakhovetski | 31705e2 | 2011-05-02 07:59:02 +0000 | [diff] [blame] | 1412 | spin_lock_irq(&sh_dmae_lock); |
Paul Mundt | 03aa18f | 2010-12-17 19:16:10 +0900 | [diff] [blame] | 1413 | list_del_rcu(&shdev->node); |
Guennadi Liakhovetski | 31705e2 | 2011-05-02 07:59:02 +0000 | [diff] [blame] | 1414 | spin_unlock_irq(&sh_dmae_lock); |
Paul Mundt | 03aa18f | 2010-12-17 19:16:10 +0900 | [diff] [blame] | 1415 | |
Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 1416 | /* channel data remove */ |
| 1417 | sh_dmae_chan_remove(shdev); |
| 1418 | |
Guennadi Liakhovetski | 20f2a3b | 2010-02-11 16:50:18 +0000 | [diff] [blame] | 1419 | pm_runtime_disable(&pdev->dev); |
| 1420 | |
Guennadi Liakhovetski | 027811b | 2010-02-11 16:50:10 +0000 | [diff] [blame] | 1421 | if (shdev->dmars) |
| 1422 | iounmap(shdev->dmars); |
| 1423 | iounmap(shdev->chan_reg); |
| 1424 | |
Paul Mundt | 5c2de44 | 2011-05-31 15:53:03 +0900 | [diff] [blame] | 1425 | platform_set_drvdata(pdev, NULL); |
| 1426 | |
Guennadi Liakhovetski | 31705e2 | 2011-05-02 07:59:02 +0000 | [diff] [blame] | 1427 | synchronize_rcu(); |
Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 1428 | kfree(shdev); |
| 1429 | |
Guennadi Liakhovetski | 027811b | 2010-02-11 16:50:10 +0000 | [diff] [blame] | 1430 | 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 Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 1437 | return 0; |
| 1438 | } |
| 1439 | |
| 1440 | static void sh_dmae_shutdown(struct platform_device *pdev) |
| 1441 | { |
| 1442 | struct sh_dmae_device *shdev = platform_get_drvdata(pdev); |
Guennadi Liakhovetski | 027811b | 2010-02-11 16:50:10 +0000 | [diff] [blame] | 1443 | sh_dmae_ctl_stop(shdev); |
Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 1444 | } |
| 1445 | |
Guennadi Liakhovetski | 467017b | 2011-04-29 17:09:25 +0000 | [diff] [blame] | 1446 | static int sh_dmae_runtime_suspend(struct device *dev) |
| 1447 | { |
| 1448 | return 0; |
| 1449 | } |
| 1450 | |
| 1451 | static 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 |
| 1459 | static int sh_dmae_suspend(struct device *dev) |
| 1460 | { |
Guennadi Liakhovetski | 467017b | 2011-04-29 17:09:25 +0000 | [diff] [blame] | 1461 | return 0; |
| 1462 | } |
| 1463 | |
| 1464 | static int sh_dmae_resume(struct device *dev) |
| 1465 | { |
| 1466 | struct sh_dmae_device *shdev = dev_get_drvdata(dev); |
Guennadi Liakhovetski | c11b46c | 2012-01-04 15:34:17 +0100 | [diff] [blame] | 1467 | int i, ret; |
| 1468 | |
| 1469 | ret = sh_dmae_rst(shdev); |
| 1470 | if (ret < 0) |
| 1471 | dev_err(dev, "Failed to reset!\n"); |
Guennadi Liakhovetski | 467017b | 2011-04-29 17:09:25 +0000 | [diff] [blame] | 1472 | |
| 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 Liakhovetski | 467017b | 2011-04-29 17:09:25 +0000 | [diff] [blame] | 1480 | 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 | |
| 1496 | const 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 Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 1503 | static struct platform_driver sh_dmae_driver = { |
| 1504 | .remove = __exit_p(sh_dmae_remove), |
| 1505 | .shutdown = sh_dmae_shutdown, |
| 1506 | .driver = { |
Guennadi Liakhovetski | 7a5c106 | 2010-05-21 15:28:51 +0000 | [diff] [blame] | 1507 | .owner = THIS_MODULE, |
Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 1508 | .name = "sh-dma-engine", |
Guennadi Liakhovetski | 467017b | 2011-04-29 17:09:25 +0000 | [diff] [blame] | 1509 | .pm = &sh_dmae_pm, |
Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 1510 | }, |
| 1511 | }; |
| 1512 | |
| 1513 | static int __init sh_dmae_init(void) |
| 1514 | { |
Guennadi Liakhovetski | 661382f | 2011-01-06 17:04:50 +0000 | [diff] [blame] | 1515 | /* Wire up NMI handling */ |
| 1516 | int err = register_die_notifier(&sh_dmae_nmi_notifier); |
| 1517 | if (err) |
| 1518 | return err; |
| 1519 | |
Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 1520 | return platform_driver_probe(&sh_dmae_driver, sh_dmae_probe); |
| 1521 | } |
| 1522 | module_init(sh_dmae_init); |
| 1523 | |
| 1524 | static void __exit sh_dmae_exit(void) |
| 1525 | { |
| 1526 | platform_driver_unregister(&sh_dmae_driver); |
Guennadi Liakhovetski | 661382f | 2011-01-06 17:04:50 +0000 | [diff] [blame] | 1527 | |
| 1528 | unregister_die_notifier(&sh_dmae_nmi_notifier); |
Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 1529 | } |
| 1530 | module_exit(sh_dmae_exit); |
| 1531 | |
| 1532 | MODULE_AUTHOR("Nobuhiro Iwamatsu <iwamatsu.nobuhiro@renesas.com>"); |
| 1533 | MODULE_DESCRIPTION("Renesas SH DMA Engine driver"); |
| 1534 | MODULE_LICENSE("GPL"); |
Guennadi Liakhovetski | e584334 | 2010-11-24 09:48:10 +0000 | [diff] [blame] | 1535 | MODULE_ALIAS("platform:sh-dma-engine"); |