| 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> | 
|  | 26 | #include <linux/dma-mapping.h> | 
| Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 27 | #include <linux/platform_device.h> | 
| Guennadi Liakhovetski | 20f2a3b | 2010-02-11 16:50:18 +0000 | [diff] [blame] | 28 | #include <linux/pm_runtime.h> | 
| Magnus Damm | b2623a6 | 2010-03-19 04:47:10 +0000 | [diff] [blame] | 29 | #include <linux/sh_dma.h> | 
| Paul Mundt | 03aa18f | 2010-12-17 19:16:10 +0900 | [diff] [blame] | 30 | #include <linux/notifier.h> | 
|  | 31 | #include <linux/kdebug.h> | 
|  | 32 | #include <linux/spinlock.h> | 
|  | 33 | #include <linux/rculist.h> | 
| Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 34 | #include "shdma.h" | 
|  | 35 |  | 
|  | 36 | /* DMA descriptor control */ | 
| Guennadi Liakhovetski | 3542a11 | 2009-12-17 09:41:39 -0700 | [diff] [blame] | 37 | enum sh_dmae_desc_status { | 
|  | 38 | DESC_IDLE, | 
|  | 39 | DESC_PREPARED, | 
|  | 40 | DESC_SUBMITTED, | 
|  | 41 | DESC_COMPLETED,	/* completed, have to call callback */ | 
|  | 42 | DESC_WAITING,	/* callback called, waiting for ack / re-submit */ | 
|  | 43 | }; | 
| Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 44 |  | 
|  | 45 | #define NR_DESCS_PER_CHANNEL 32 | 
| Guennadi Liakhovetski | 8b1935e | 2010-02-11 16:50:14 +0000 | [diff] [blame] | 46 | /* Default MEMCPY transfer size = 2^2 = 4 bytes */ | 
|  | 47 | #define LOG2_DEFAULT_XFER_SIZE	2 | 
| Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 48 |  | 
| Paul Mundt | 03aa18f | 2010-12-17 19:16:10 +0900 | [diff] [blame] | 49 | /* | 
|  | 50 | * Used for write-side mutual exclusion for the global device list, | 
| Guennadi Liakhovetski | 2dc6666 | 2011-04-29 17:09:21 +0000 | [diff] [blame] | 51 | * read-side synchronization by way of RCU, and per-controller data. | 
| Paul Mundt | 03aa18f | 2010-12-17 19:16:10 +0900 | [diff] [blame] | 52 | */ | 
|  | 53 | static DEFINE_SPINLOCK(sh_dmae_lock); | 
|  | 54 | static LIST_HEAD(sh_dmae_devices); | 
|  | 55 |  | 
| Guennadi Liakhovetski | cfefe99 | 2010-02-03 14:46:41 +0000 | [diff] [blame] | 56 | /* A bitmask with bits enough for enum sh_dmae_slave_chan_id */ | 
| Magnus Damm | 02ca508 | 2010-03-19 04:46:47 +0000 | [diff] [blame] | 57 | 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] | 58 |  | 
| Guennadi Liakhovetski | 3542a11 | 2009-12-17 09:41:39 -0700 | [diff] [blame] | 59 | static void sh_dmae_chan_ld_cleanup(struct sh_dmae_chan *sh_chan, bool all); | 
|  | 60 |  | 
| Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 61 | static void sh_dmae_writel(struct sh_dmae_chan *sh_dc, u32 data, u32 reg) | 
|  | 62 | { | 
| Guennadi Liakhovetski | 027811b | 2010-02-11 16:50:10 +0000 | [diff] [blame] | 63 | __raw_writel(data, sh_dc->base + reg / sizeof(u32)); | 
| Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 64 | } | 
|  | 65 |  | 
|  | 66 | static u32 sh_dmae_readl(struct sh_dmae_chan *sh_dc, u32 reg) | 
|  | 67 | { | 
| Guennadi Liakhovetski | 027811b | 2010-02-11 16:50:10 +0000 | [diff] [blame] | 68 | return __raw_readl(sh_dc->base + reg / sizeof(u32)); | 
|  | 69 | } | 
|  | 70 |  | 
|  | 71 | static u16 dmaor_read(struct sh_dmae_device *shdev) | 
|  | 72 | { | 
| Kuninori Morimoto | e76c3af | 2011-06-17 08:20:56 +0000 | [diff] [blame] | 73 | u32 __iomem *addr = shdev->chan_reg + DMAOR / sizeof(u32); | 
|  | 74 |  | 
|  | 75 | if (shdev->pdata->dmaor_is_32bit) | 
|  | 76 | return __raw_readl(addr); | 
|  | 77 | else | 
|  | 78 | return __raw_readw(addr); | 
| Guennadi Liakhovetski | 027811b | 2010-02-11 16:50:10 +0000 | [diff] [blame] | 79 | } | 
|  | 80 |  | 
|  | 81 | static void dmaor_write(struct sh_dmae_device *shdev, u16 data) | 
|  | 82 | { | 
| Kuninori Morimoto | e76c3af | 2011-06-17 08:20:56 +0000 | [diff] [blame] | 83 | u32 __iomem *addr = shdev->chan_reg + DMAOR / sizeof(u32); | 
|  | 84 |  | 
|  | 85 | if (shdev->pdata->dmaor_is_32bit) | 
|  | 86 | __raw_writel(data, addr); | 
|  | 87 | else | 
|  | 88 | __raw_writew(data, addr); | 
| Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 89 | } | 
|  | 90 |  | 
| Kuninori Morimoto | 5899a72 | 2011-06-17 08:20:40 +0000 | [diff] [blame] | 91 | static void chcr_write(struct sh_dmae_chan *sh_dc, u32 data) | 
|  | 92 | { | 
|  | 93 | struct sh_dmae_device *shdev = to_sh_dev(sh_dc); | 
|  | 94 |  | 
|  | 95 | __raw_writel(data, sh_dc->base + shdev->chcr_offset / sizeof(u32)); | 
|  | 96 | } | 
|  | 97 |  | 
|  | 98 | static u32 chcr_read(struct sh_dmae_chan *sh_dc) | 
|  | 99 | { | 
|  | 100 | struct sh_dmae_device *shdev = to_sh_dev(sh_dc); | 
|  | 101 |  | 
|  | 102 | return __raw_readl(sh_dc->base + shdev->chcr_offset / sizeof(u32)); | 
| Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 103 | } | 
|  | 104 |  | 
| Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 105 | /* | 
|  | 106 | * Reset DMA controller | 
|  | 107 | * | 
|  | 108 | * SH7780 has two DMAOR register | 
|  | 109 | */ | 
| Guennadi Liakhovetski | 027811b | 2010-02-11 16:50:10 +0000 | [diff] [blame] | 110 | static void sh_dmae_ctl_stop(struct sh_dmae_device *shdev) | 
| Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 111 | { | 
| Guennadi Liakhovetski | 2dc6666 | 2011-04-29 17:09:21 +0000 | [diff] [blame] | 112 | unsigned short dmaor; | 
|  | 113 | unsigned long flags; | 
| Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 114 |  | 
| Guennadi Liakhovetski | 2dc6666 | 2011-04-29 17:09:21 +0000 | [diff] [blame] | 115 | spin_lock_irqsave(&sh_dmae_lock, flags); | 
|  | 116 |  | 
|  | 117 | dmaor = dmaor_read(shdev); | 
| Guennadi Liakhovetski | 027811b | 2010-02-11 16:50:10 +0000 | [diff] [blame] | 118 | dmaor_write(shdev, dmaor & ~(DMAOR_NMIF | DMAOR_AE | DMAOR_DME)); | 
| Guennadi Liakhovetski | 2dc6666 | 2011-04-29 17:09:21 +0000 | [diff] [blame] | 119 |  | 
|  | 120 | spin_unlock_irqrestore(&sh_dmae_lock, flags); | 
| Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 121 | } | 
|  | 122 |  | 
| Guennadi Liakhovetski | 027811b | 2010-02-11 16:50:10 +0000 | [diff] [blame] | 123 | static int sh_dmae_rst(struct sh_dmae_device *shdev) | 
| Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 124 | { | 
|  | 125 | unsigned short dmaor; | 
| Guennadi Liakhovetski | 2dc6666 | 2011-04-29 17:09:21 +0000 | [diff] [blame] | 126 | unsigned long flags; | 
| Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 127 |  | 
| Guennadi Liakhovetski | 2dc6666 | 2011-04-29 17:09:21 +0000 | [diff] [blame] | 128 | spin_lock_irqsave(&sh_dmae_lock, flags); | 
| Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 129 |  | 
| Guennadi Liakhovetski | 2dc6666 | 2011-04-29 17:09:21 +0000 | [diff] [blame] | 130 | dmaor = dmaor_read(shdev) & ~(DMAOR_NMIF | DMAOR_AE | DMAOR_DME); | 
|  | 131 |  | 
|  | 132 | dmaor_write(shdev, dmaor | shdev->pdata->dmaor_init); | 
|  | 133 |  | 
|  | 134 | dmaor = dmaor_read(shdev); | 
|  | 135 |  | 
|  | 136 | spin_unlock_irqrestore(&sh_dmae_lock, flags); | 
|  | 137 |  | 
|  | 138 | if (dmaor & (DMAOR_AE | DMAOR_NMIF)) { | 
|  | 139 | dev_warn(shdev->common.dev, "Can't initialize DMAOR.\n"); | 
|  | 140 | return -EIO; | 
| Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 141 | } | 
|  | 142 | return 0; | 
|  | 143 | } | 
|  | 144 |  | 
| Guennadi Liakhovetski | fc46185 | 2010-01-19 07:24:55 +0000 | [diff] [blame] | 145 | static bool dmae_is_busy(struct sh_dmae_chan *sh_chan) | 
| Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 146 | { | 
| Kuninori Morimoto | 5899a72 | 2011-06-17 08:20:40 +0000 | [diff] [blame] | 147 | u32 chcr = chcr_read(sh_chan); | 
| Guennadi Liakhovetski | fc46185 | 2010-01-19 07:24:55 +0000 | [diff] [blame] | 148 |  | 
|  | 149 | if ((chcr & (CHCR_DE | CHCR_TE)) == CHCR_DE) | 
|  | 150 | return true; /* working */ | 
|  | 151 |  | 
|  | 152 | return false; /* waiting */ | 
| Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 153 | } | 
|  | 154 |  | 
| Guennadi Liakhovetski | 8b1935e | 2010-02-11 16:50:14 +0000 | [diff] [blame] | 155 | 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] | 156 | { | 
| Kuninori Morimoto | c4e0dd7 | 2011-06-16 05:08:09 +0000 | [diff] [blame] | 157 | struct sh_dmae_device *shdev = to_sh_dev(sh_chan); | 
| Guennadi Liakhovetski | 8b1935e | 2010-02-11 16:50:14 +0000 | [diff] [blame] | 158 | struct sh_dmae_pdata *pdata = shdev->pdata; | 
|  | 159 | int cnt = ((chcr & pdata->ts_low_mask) >> pdata->ts_low_shift) | | 
|  | 160 | ((chcr & pdata->ts_high_mask) >> pdata->ts_high_shift); | 
| Guennadi Liakhovetski | 623b4ac | 2010-02-03 14:44:12 +0000 | [diff] [blame] | 161 |  | 
| Guennadi Liakhovetski | 8b1935e | 2010-02-11 16:50:14 +0000 | [diff] [blame] | 162 | if (cnt >= pdata->ts_shift_num) | 
|  | 163 | cnt = 0; | 
|  | 164 |  | 
|  | 165 | return pdata->ts_shift[cnt]; | 
|  | 166 | } | 
|  | 167 |  | 
|  | 168 | static u32 log2size_to_chcr(struct sh_dmae_chan *sh_chan, int l2size) | 
|  | 169 | { | 
| Kuninori Morimoto | c4e0dd7 | 2011-06-16 05:08:09 +0000 | [diff] [blame] | 170 | struct sh_dmae_device *shdev = to_sh_dev(sh_chan); | 
| Guennadi Liakhovetski | 8b1935e | 2010-02-11 16:50:14 +0000 | [diff] [blame] | 171 | struct sh_dmae_pdata *pdata = shdev->pdata; | 
|  | 172 | int i; | 
|  | 173 |  | 
|  | 174 | for (i = 0; i < pdata->ts_shift_num; i++) | 
|  | 175 | if (pdata->ts_shift[i] == l2size) | 
|  | 176 | break; | 
|  | 177 |  | 
|  | 178 | if (i == pdata->ts_shift_num) | 
|  | 179 | i = 0; | 
|  | 180 |  | 
|  | 181 | return ((i << pdata->ts_low_shift) & pdata->ts_low_mask) | | 
|  | 182 | ((i << pdata->ts_high_shift) & pdata->ts_high_mask); | 
| Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 183 | } | 
|  | 184 |  | 
| Guennadi Liakhovetski | 3542a11 | 2009-12-17 09:41:39 -0700 | [diff] [blame] | 185 | 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] | 186 | { | 
| Guennadi Liakhovetski | 3542a11 | 2009-12-17 09:41:39 -0700 | [diff] [blame] | 187 | sh_dmae_writel(sh_chan, hw->sar, SAR); | 
|  | 188 | sh_dmae_writel(sh_chan, hw->dar, DAR); | 
| Guennadi Liakhovetski | cfefe99 | 2010-02-03 14:46:41 +0000 | [diff] [blame] | 189 | sh_dmae_writel(sh_chan, hw->tcr >> sh_chan->xmit_shift, TCR); | 
| Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 190 | } | 
|  | 191 |  | 
|  | 192 | static void dmae_start(struct sh_dmae_chan *sh_chan) | 
|  | 193 | { | 
| Kuninori Morimoto | 67c6269 | 2011-06-17 08:20:51 +0000 | [diff] [blame] | 194 | struct sh_dmae_device *shdev = to_sh_dev(sh_chan); | 
| Kuninori Morimoto | 5899a72 | 2011-06-17 08:20:40 +0000 | [diff] [blame] | 195 | u32 chcr = chcr_read(sh_chan); | 
| Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 196 |  | 
| Kuninori Morimoto | 260bf2c | 2011-06-17 08:21:05 +0000 | [diff] [blame] | 197 | if (shdev->pdata->needs_tend_set) | 
|  | 198 | sh_dmae_writel(sh_chan, 0xFFFFFFFF, TEND); | 
|  | 199 |  | 
| Kuninori Morimoto | 67c6269 | 2011-06-17 08:20:51 +0000 | [diff] [blame] | 200 | chcr |= CHCR_DE | shdev->chcr_ie_bit; | 
| Kuninori Morimoto | 5899a72 | 2011-06-17 08:20:40 +0000 | [diff] [blame] | 201 | chcr_write(sh_chan, chcr & ~CHCR_TE); | 
| Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 202 | } | 
|  | 203 |  | 
|  | 204 | static void dmae_halt(struct sh_dmae_chan *sh_chan) | 
|  | 205 | { | 
| Kuninori Morimoto | 67c6269 | 2011-06-17 08:20:51 +0000 | [diff] [blame] | 206 | struct sh_dmae_device *shdev = to_sh_dev(sh_chan); | 
| Kuninori Morimoto | 5899a72 | 2011-06-17 08:20:40 +0000 | [diff] [blame] | 207 | u32 chcr = chcr_read(sh_chan); | 
| Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 208 |  | 
| Kuninori Morimoto | 67c6269 | 2011-06-17 08:20:51 +0000 | [diff] [blame] | 209 | chcr &= ~(CHCR_DE | CHCR_TE | shdev->chcr_ie_bit); | 
| Kuninori Morimoto | 5899a72 | 2011-06-17 08:20:40 +0000 | [diff] [blame] | 210 | chcr_write(sh_chan, chcr); | 
| Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 211 | } | 
|  | 212 |  | 
| Guennadi Liakhovetski | cfefe99 | 2010-02-03 14:46:41 +0000 | [diff] [blame] | 213 | static void dmae_init(struct sh_dmae_chan *sh_chan) | 
|  | 214 | { | 
| Guennadi Liakhovetski | 8b1935e | 2010-02-11 16:50:14 +0000 | [diff] [blame] | 215 | /* | 
|  | 216 | * Default configuration for dual address memory-memory transfer. | 
|  | 217 | * 0x400 represents auto-request. | 
|  | 218 | */ | 
|  | 219 | u32 chcr = DM_INC | SM_INC | 0x400 | log2size_to_chcr(sh_chan, | 
|  | 220 | LOG2_DEFAULT_XFER_SIZE); | 
|  | 221 | sh_chan->xmit_shift = calc_xmit_shift(sh_chan, chcr); | 
| Kuninori Morimoto | 5899a72 | 2011-06-17 08:20:40 +0000 | [diff] [blame] | 222 | chcr_write(sh_chan, chcr); | 
| Guennadi Liakhovetski | cfefe99 | 2010-02-03 14:46:41 +0000 | [diff] [blame] | 223 | } | 
|  | 224 |  | 
| Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 225 | static int dmae_set_chcr(struct sh_dmae_chan *sh_chan, u32 val) | 
|  | 226 | { | 
| Guennadi Liakhovetski | 2dc6666 | 2011-04-29 17:09:21 +0000 | [diff] [blame] | 227 | /* If DMA is active, cannot set CHCR. TODO: remove this superfluous check */ | 
| Guennadi Liakhovetski | fc46185 | 2010-01-19 07:24:55 +0000 | [diff] [blame] | 228 | if (dmae_is_busy(sh_chan)) | 
|  | 229 | return -EBUSY; | 
| Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 230 |  | 
| Guennadi Liakhovetski | 8b1935e | 2010-02-11 16:50:14 +0000 | [diff] [blame] | 231 | sh_chan->xmit_shift = calc_xmit_shift(sh_chan, val); | 
| Kuninori Morimoto | 5899a72 | 2011-06-17 08:20:40 +0000 | [diff] [blame] | 232 | chcr_write(sh_chan, val); | 
| Guennadi Liakhovetski | cfefe99 | 2010-02-03 14:46:41 +0000 | [diff] [blame] | 233 |  | 
| Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 234 | return 0; | 
|  | 235 | } | 
|  | 236 |  | 
| Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 237 | static int dmae_set_dmars(struct sh_dmae_chan *sh_chan, u16 val) | 
|  | 238 | { | 
| Kuninori Morimoto | c4e0dd7 | 2011-06-16 05:08:09 +0000 | [diff] [blame] | 239 | struct sh_dmae_device *shdev = to_sh_dev(sh_chan); | 
| Guennadi Liakhovetski | 027811b | 2010-02-11 16:50:10 +0000 | [diff] [blame] | 240 | struct sh_dmae_pdata *pdata = shdev->pdata; | 
| Guennadi Liakhovetski | 5bac942 | 2010-04-21 15:36:49 +0000 | [diff] [blame] | 241 | const struct sh_dmae_channel *chan_pdata = &pdata->channel[sh_chan->id]; | 
| Magnus Damm | 26fc02a | 2011-05-24 10:31:12 +0000 | [diff] [blame] | 242 | u16 __iomem *addr = shdev->dmars; | 
| Kuninori Morimoto | 090b918 | 2011-06-16 05:08:28 +0000 | [diff] [blame] | 243 | unsigned int shift = chan_pdata->dmars_bit; | 
| Guennadi Liakhovetski | fc46185 | 2010-01-19 07:24:55 +0000 | [diff] [blame] | 244 |  | 
|  | 245 | if (dmae_is_busy(sh_chan)) | 
|  | 246 | return -EBUSY; | 
| Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 247 |  | 
| Kuninori Morimoto | 260bf2c | 2011-06-17 08:21:05 +0000 | [diff] [blame] | 248 | if (pdata->no_dmars) | 
|  | 249 | return 0; | 
|  | 250 |  | 
| Magnus Damm | 26fc02a | 2011-05-24 10:31:12 +0000 | [diff] [blame] | 251 | /* in the case of a missing DMARS resource use first memory window */ | 
|  | 252 | if (!addr) | 
|  | 253 | addr = (u16 __iomem *)shdev->chan_reg; | 
|  | 254 | addr += chan_pdata->dmars / sizeof(u16); | 
|  | 255 |  | 
| Guennadi Liakhovetski | 027811b | 2010-02-11 16:50:10 +0000 | [diff] [blame] | 256 | __raw_writew((__raw_readw(addr) & (0xff00 >> shift)) | (val << shift), | 
|  | 257 | addr); | 
| Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 258 |  | 
|  | 259 | return 0; | 
|  | 260 | } | 
|  | 261 |  | 
| Guennadi Liakhovetski | 7a1cd9a | 2011-08-18 16:55:27 +0200 | [diff] [blame] | 262 | static void sh_chan_xfer_ld_queue(struct sh_dmae_chan *sh_chan); | 
|  | 263 |  | 
| Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 264 | static dma_cookie_t sh_dmae_tx_submit(struct dma_async_tx_descriptor *tx) | 
|  | 265 | { | 
| Guennadi Liakhovetski | 3542a11 | 2009-12-17 09:41:39 -0700 | [diff] [blame] | 266 | 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] | 267 | struct sh_dmae_chan *sh_chan = to_sh_chan(tx->chan); | 
| Guennadi Liakhovetski | 7a1cd9a | 2011-08-18 16:55:27 +0200 | [diff] [blame] | 268 | struct sh_dmae_slave *param = tx->chan->private; | 
| Guennadi Liakhovetski | 3542a11 | 2009-12-17 09:41:39 -0700 | [diff] [blame] | 269 | dma_async_tx_callback callback = tx->callback; | 
| Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 270 | dma_cookie_t cookie; | 
| Guennadi Liakhovetski | 7a1cd9a | 2011-08-18 16:55:27 +0200 | [diff] [blame] | 271 | bool power_up; | 
| Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 272 |  | 
| Guennadi Liakhovetski | 7a1cd9a | 2011-08-18 16:55:27 +0200 | [diff] [blame] | 273 | spin_lock_irq(&sh_chan->desc_lock); | 
|  | 274 |  | 
|  | 275 | if (list_empty(&sh_chan->ld_queue)) | 
|  | 276 | power_up = true; | 
|  | 277 | else | 
|  | 278 | power_up = false; | 
| Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 279 |  | 
|  | 280 | cookie = sh_chan->common.cookie; | 
|  | 281 | cookie++; | 
|  | 282 | if (cookie < 0) | 
|  | 283 | cookie = 1; | 
|  | 284 |  | 
| Guennadi Liakhovetski | 3542a11 | 2009-12-17 09:41:39 -0700 | [diff] [blame] | 285 | sh_chan->common.cookie = cookie; | 
|  | 286 | tx->cookie = cookie; | 
| Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 287 |  | 
| Guennadi Liakhovetski | 3542a11 | 2009-12-17 09:41:39 -0700 | [diff] [blame] | 288 | /* Mark all chunks of this descriptor as submitted, move to the queue */ | 
|  | 289 | list_for_each_entry_safe(chunk, c, desc->node.prev, node) { | 
|  | 290 | /* | 
|  | 291 | * All chunks are on the global ld_free, so, we have to find | 
|  | 292 | * the end of the chain ourselves | 
|  | 293 | */ | 
|  | 294 | if (chunk != desc && (chunk->mark == DESC_IDLE || | 
|  | 295 | chunk->async_tx.cookie > 0 || | 
|  | 296 | chunk->async_tx.cookie == -EBUSY || | 
|  | 297 | &chunk->node == &sh_chan->ld_free)) | 
|  | 298 | break; | 
|  | 299 | chunk->mark = DESC_SUBMITTED; | 
|  | 300 | /* Callback goes to the last chunk */ | 
|  | 301 | chunk->async_tx.callback = NULL; | 
|  | 302 | chunk->cookie = cookie; | 
|  | 303 | list_move_tail(&chunk->node, &sh_chan->ld_queue); | 
|  | 304 | last = chunk; | 
|  | 305 | } | 
|  | 306 |  | 
|  | 307 | last->async_tx.callback = callback; | 
|  | 308 | last->async_tx.callback_param = tx->callback_param; | 
|  | 309 |  | 
|  | 310 | dev_dbg(sh_chan->dev, "submit #%d@%p on %d: %x[%d] -> %x\n", | 
|  | 311 | tx->cookie, &last->async_tx, sh_chan->id, | 
|  | 312 | desc->hw.sar, desc->hw.tcr, desc->hw.dar); | 
| Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 313 |  | 
| Guennadi Liakhovetski | 7a1cd9a | 2011-08-18 16:55:27 +0200 | [diff] [blame] | 314 | if (power_up) { | 
|  | 315 | sh_chan->pm_state = DMAE_PM_BUSY; | 
|  | 316 |  | 
|  | 317 | pm_runtime_get(sh_chan->dev); | 
|  | 318 |  | 
|  | 319 | spin_unlock_irq(&sh_chan->desc_lock); | 
|  | 320 |  | 
|  | 321 | pm_runtime_barrier(sh_chan->dev); | 
|  | 322 |  | 
|  | 323 | spin_lock_irq(&sh_chan->desc_lock); | 
|  | 324 |  | 
|  | 325 | /* Have we been reset, while waiting? */ | 
|  | 326 | if (sh_chan->pm_state != DMAE_PM_ESTABLISHED) { | 
|  | 327 | dev_dbg(sh_chan->dev, "Bring up channel %d\n", | 
|  | 328 | sh_chan->id); | 
|  | 329 | if (param) { | 
|  | 330 | const struct sh_dmae_slave_config *cfg = | 
|  | 331 | param->config; | 
|  | 332 |  | 
|  | 333 | dmae_set_dmars(sh_chan, cfg->mid_rid); | 
|  | 334 | dmae_set_chcr(sh_chan, cfg->chcr); | 
|  | 335 | } else { | 
|  | 336 | dmae_init(sh_chan); | 
|  | 337 | } | 
|  | 338 |  | 
|  | 339 | if (sh_chan->pm_state == DMAE_PM_PENDING) | 
|  | 340 | sh_chan_xfer_ld_queue(sh_chan); | 
|  | 341 | sh_chan->pm_state = DMAE_PM_ESTABLISHED; | 
|  | 342 | } | 
|  | 343 | } | 
|  | 344 |  | 
|  | 345 | spin_unlock_irq(&sh_chan->desc_lock); | 
| Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 346 |  | 
|  | 347 | return cookie; | 
|  | 348 | } | 
|  | 349 |  | 
| Guennadi Liakhovetski | 3542a11 | 2009-12-17 09:41:39 -0700 | [diff] [blame] | 350 | /* Called with desc_lock held */ | 
| Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 351 | static struct sh_desc *sh_dmae_get_desc(struct sh_dmae_chan *sh_chan) | 
|  | 352 | { | 
| Guennadi Liakhovetski | 3542a11 | 2009-12-17 09:41:39 -0700 | [diff] [blame] | 353 | struct sh_desc *desc; | 
| Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 354 |  | 
| Guennadi Liakhovetski | 3542a11 | 2009-12-17 09:41:39 -0700 | [diff] [blame] | 355 | list_for_each_entry(desc, &sh_chan->ld_free, node) | 
|  | 356 | if (desc->mark != DESC_PREPARED) { | 
|  | 357 | BUG_ON(desc->mark != DESC_IDLE); | 
| Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 358 | list_del(&desc->node); | 
| Guennadi Liakhovetski | 3542a11 | 2009-12-17 09:41:39 -0700 | [diff] [blame] | 359 | return desc; | 
| Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 360 | } | 
| Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 361 |  | 
| Guennadi Liakhovetski | 3542a11 | 2009-12-17 09:41:39 -0700 | [diff] [blame] | 362 | return NULL; | 
| Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 363 | } | 
|  | 364 |  | 
| Guennadi Liakhovetski | 5bac942 | 2010-04-21 15:36:49 +0000 | [diff] [blame] | 365 | static const struct sh_dmae_slave_config *sh_dmae_find_slave( | 
| Magnus Damm | 4bab9d4 | 2010-03-19 04:46:38 +0000 | [diff] [blame] | 366 | struct sh_dmae_chan *sh_chan, struct sh_dmae_slave *param) | 
| Guennadi Liakhovetski | cfefe99 | 2010-02-03 14:46:41 +0000 | [diff] [blame] | 367 | { | 
| Kuninori Morimoto | c4e0dd7 | 2011-06-16 05:08:09 +0000 | [diff] [blame] | 368 | struct sh_dmae_device *shdev = to_sh_dev(sh_chan); | 
| Guennadi Liakhovetski | 027811b | 2010-02-11 16:50:10 +0000 | [diff] [blame] | 369 | struct sh_dmae_pdata *pdata = shdev->pdata; | 
| Guennadi Liakhovetski | cfefe99 | 2010-02-03 14:46:41 +0000 | [diff] [blame] | 370 | int i; | 
|  | 371 |  | 
| Magnus Damm | 02ca508 | 2010-03-19 04:46:47 +0000 | [diff] [blame] | 372 | if (param->slave_id >= SH_DMA_SLAVE_NUMBER) | 
| Guennadi Liakhovetski | cfefe99 | 2010-02-03 14:46:41 +0000 | [diff] [blame] | 373 | return NULL; | 
|  | 374 |  | 
| Guennadi Liakhovetski | 027811b | 2010-02-11 16:50:10 +0000 | [diff] [blame] | 375 | for (i = 0; i < pdata->slave_num; i++) | 
| Magnus Damm | 4bab9d4 | 2010-03-19 04:46:38 +0000 | [diff] [blame] | 376 | if (pdata->slave[i].slave_id == param->slave_id) | 
| Guennadi Liakhovetski | 027811b | 2010-02-11 16:50:10 +0000 | [diff] [blame] | 377 | return pdata->slave + i; | 
| Guennadi Liakhovetski | cfefe99 | 2010-02-03 14:46:41 +0000 | [diff] [blame] | 378 |  | 
|  | 379 | return NULL; | 
|  | 380 | } | 
|  | 381 |  | 
| Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 382 | static int sh_dmae_alloc_chan_resources(struct dma_chan *chan) | 
|  | 383 | { | 
|  | 384 | struct sh_dmae_chan *sh_chan = to_sh_chan(chan); | 
|  | 385 | struct sh_desc *desc; | 
| Guennadi Liakhovetski | cfefe99 | 2010-02-03 14:46:41 +0000 | [diff] [blame] | 386 | struct sh_dmae_slave *param = chan->private; | 
| Guennadi Liakhovetski | 83515bc | 2010-04-19 08:39:39 +0000 | [diff] [blame] | 387 | int ret; | 
| Guennadi Liakhovetski | cfefe99 | 2010-02-03 14:46:41 +0000 | [diff] [blame] | 388 |  | 
|  | 389 | /* | 
|  | 390 | * This relies on the guarantee from dmaengine that alloc_chan_resources | 
|  | 391 | * never runs concurrently with itself or free_chan_resources. | 
|  | 392 | */ | 
|  | 393 | if (param) { | 
| Guennadi Liakhovetski | 5bac942 | 2010-04-21 15:36:49 +0000 | [diff] [blame] | 394 | const struct sh_dmae_slave_config *cfg; | 
| Guennadi Liakhovetski | cfefe99 | 2010-02-03 14:46:41 +0000 | [diff] [blame] | 395 |  | 
| Magnus Damm | 4bab9d4 | 2010-03-19 04:46:38 +0000 | [diff] [blame] | 396 | cfg = sh_dmae_find_slave(sh_chan, param); | 
| Guennadi Liakhovetski | 83515bc | 2010-04-19 08:39:39 +0000 | [diff] [blame] | 397 | if (!cfg) { | 
|  | 398 | ret = -EINVAL; | 
|  | 399 | goto efindslave; | 
|  | 400 | } | 
| Guennadi Liakhovetski | cfefe99 | 2010-02-03 14:46:41 +0000 | [diff] [blame] | 401 |  | 
| Guennadi Liakhovetski | 83515bc | 2010-04-19 08:39:39 +0000 | [diff] [blame] | 402 | if (test_and_set_bit(param->slave_id, sh_dmae_slave_used)) { | 
|  | 403 | ret = -EBUSY; | 
|  | 404 | goto etestused; | 
|  | 405 | } | 
| Guennadi Liakhovetski | cfefe99 | 2010-02-03 14:46:41 +0000 | [diff] [blame] | 406 |  | 
|  | 407 | param->config = cfg; | 
| Guennadi Liakhovetski | cfefe99 | 2010-02-03 14:46:41 +0000 | [diff] [blame] | 408 | } | 
| Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 409 |  | 
| Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 410 | while (sh_chan->descs_allocated < NR_DESCS_PER_CHANNEL) { | 
| Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 411 | desc = kzalloc(sizeof(struct sh_desc), GFP_KERNEL); | 
| Guennadi Liakhovetski | b4dae6e | 2011-09-25 16:12:18 +0200 | [diff] [blame] | 412 | if (!desc) | 
| Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 413 | break; | 
| Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 414 | dma_async_tx_descriptor_init(&desc->async_tx, | 
|  | 415 | &sh_chan->common); | 
|  | 416 | desc->async_tx.tx_submit = sh_dmae_tx_submit; | 
| Guennadi Liakhovetski | 3542a11 | 2009-12-17 09:41:39 -0700 | [diff] [blame] | 417 | desc->mark = DESC_IDLE; | 
| Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 418 |  | 
| Guennadi Liakhovetski | 3542a11 | 2009-12-17 09:41:39 -0700 | [diff] [blame] | 419 | list_add(&desc->node, &sh_chan->ld_free); | 
| Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 420 | sh_chan->descs_allocated++; | 
|  | 421 | } | 
| Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 422 |  | 
| Guennadi Liakhovetski | 83515bc | 2010-04-19 08:39:39 +0000 | [diff] [blame] | 423 | if (!sh_chan->descs_allocated) { | 
|  | 424 | ret = -ENOMEM; | 
|  | 425 | goto edescalloc; | 
|  | 426 | } | 
| Guennadi Liakhovetski | 20f2a3b | 2010-02-11 16:50:18 +0000 | [diff] [blame] | 427 |  | 
| Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 428 | return sh_chan->descs_allocated; | 
| Guennadi Liakhovetski | 83515bc | 2010-04-19 08:39:39 +0000 | [diff] [blame] | 429 |  | 
|  | 430 | edescalloc: | 
|  | 431 | if (param) | 
|  | 432 | clear_bit(param->slave_id, sh_dmae_slave_used); | 
|  | 433 | etestused: | 
|  | 434 | efindslave: | 
| Guennadi Liakhovetski | b4dae6e | 2011-09-25 16:12:18 +0200 | [diff] [blame] | 435 | chan->private = NULL; | 
| Guennadi Liakhovetski | 83515bc | 2010-04-19 08:39:39 +0000 | [diff] [blame] | 436 | return ret; | 
| Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 437 | } | 
|  | 438 |  | 
|  | 439 | /* | 
|  | 440 | * sh_dma_free_chan_resources - Free all resources of the channel. | 
|  | 441 | */ | 
|  | 442 | static void sh_dmae_free_chan_resources(struct dma_chan *chan) | 
|  | 443 | { | 
|  | 444 | struct sh_dmae_chan *sh_chan = to_sh_chan(chan); | 
|  | 445 | struct sh_desc *desc, *_desc; | 
|  | 446 | LIST_HEAD(list); | 
|  | 447 |  | 
| Guennadi Liakhovetski | 2dc6666 | 2011-04-29 17:09:21 +0000 | [diff] [blame] | 448 | /* Protect against ISR */ | 
|  | 449 | spin_lock_irq(&sh_chan->desc_lock); | 
| Guennadi Liakhovetski | cfefe99 | 2010-02-03 14:46:41 +0000 | [diff] [blame] | 450 | dmae_halt(sh_chan); | 
| Guennadi Liakhovetski | 2dc6666 | 2011-04-29 17:09:21 +0000 | [diff] [blame] | 451 | spin_unlock_irq(&sh_chan->desc_lock); | 
|  | 452 |  | 
|  | 453 | /* Now no new interrupts will occur */ | 
| Guennadi Liakhovetski | cfefe99 | 2010-02-03 14:46:41 +0000 | [diff] [blame] | 454 |  | 
| Guennadi Liakhovetski | 3542a11 | 2009-12-17 09:41:39 -0700 | [diff] [blame] | 455 | /* Prepared and not submitted descriptors can still be on the queue */ | 
|  | 456 | if (!list_empty(&sh_chan->ld_queue)) | 
|  | 457 | sh_dmae_chan_ld_cleanup(sh_chan, true); | 
|  | 458 |  | 
| Guennadi Liakhovetski | cfefe99 | 2010-02-03 14:46:41 +0000 | [diff] [blame] | 459 | if (chan->private) { | 
|  | 460 | /* The caller is holding dma_list_mutex */ | 
|  | 461 | struct sh_dmae_slave *param = chan->private; | 
|  | 462 | clear_bit(param->slave_id, sh_dmae_slave_used); | 
| Guennadi Liakhovetski | 2dc6666 | 2011-04-29 17:09:21 +0000 | [diff] [blame] | 463 | chan->private = NULL; | 
| Guennadi Liakhovetski | cfefe99 | 2010-02-03 14:46:41 +0000 | [diff] [blame] | 464 | } | 
|  | 465 |  | 
| Guennadi Liakhovetski | b4dae6e | 2011-09-25 16:12:18 +0200 | [diff] [blame] | 466 | spin_lock_irq(&sh_chan->desc_lock); | 
| Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 467 |  | 
|  | 468 | list_splice_init(&sh_chan->ld_free, &list); | 
|  | 469 | sh_chan->descs_allocated = 0; | 
|  | 470 |  | 
| Guennadi Liakhovetski | b4dae6e | 2011-09-25 16:12:18 +0200 | [diff] [blame] | 471 | spin_unlock_irq(&sh_chan->desc_lock); | 
| Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 472 |  | 
|  | 473 | list_for_each_entry_safe(desc, _desc, &list, node) | 
|  | 474 | kfree(desc); | 
|  | 475 | } | 
|  | 476 |  | 
| Guennadi Liakhovetski | cfefe99 | 2010-02-03 14:46:41 +0000 | [diff] [blame] | 477 | /** | 
| Guennadi Liakhovetski | fc46185 | 2010-01-19 07:24:55 +0000 | [diff] [blame] | 478 | * sh_dmae_add_desc - get, set up and return one transfer descriptor | 
|  | 479 | * @sh_chan:	DMA channel | 
|  | 480 | * @flags:	DMA transfer flags | 
|  | 481 | * @dest:	destination DMA address, incremented when direction equals | 
|  | 482 | *		DMA_FROM_DEVICE or DMA_BIDIRECTIONAL | 
|  | 483 | * @src:	source DMA address, incremented when direction equals | 
|  | 484 | *		DMA_TO_DEVICE or DMA_BIDIRECTIONAL | 
|  | 485 | * @len:	DMA transfer length | 
|  | 486 | * @first:	if NULL, set to the current descriptor and cookie set to -EBUSY | 
|  | 487 | * @direction:	needed for slave DMA to decide which address to keep constant, | 
|  | 488 | *		equals DMA_BIDIRECTIONAL for MEMCPY | 
|  | 489 | * Returns 0 or an error | 
|  | 490 | * Locks: called with desc_lock held | 
|  | 491 | */ | 
|  | 492 | static struct sh_desc *sh_dmae_add_desc(struct sh_dmae_chan *sh_chan, | 
|  | 493 | unsigned long flags, dma_addr_t *dest, dma_addr_t *src, size_t *len, | 
|  | 494 | struct sh_desc **first, enum dma_data_direction direction) | 
| Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 495 | { | 
| Guennadi Liakhovetski | fc46185 | 2010-01-19 07:24:55 +0000 | [diff] [blame] | 496 | struct sh_desc *new; | 
| Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 497 | size_t copy_size; | 
| Guennadi Liakhovetski | fc46185 | 2010-01-19 07:24:55 +0000 | [diff] [blame] | 498 |  | 
|  | 499 | if (!*len) | 
|  | 500 | return NULL; | 
|  | 501 |  | 
|  | 502 | /* Allocate the link descriptor from the free list */ | 
|  | 503 | new = sh_dmae_get_desc(sh_chan); | 
|  | 504 | if (!new) { | 
|  | 505 | dev_err(sh_chan->dev, "No free link descriptor available\n"); | 
|  | 506 | return NULL; | 
|  | 507 | } | 
|  | 508 |  | 
|  | 509 | copy_size = min(*len, (size_t)SH_DMA_TCR_MAX + 1); | 
|  | 510 |  | 
|  | 511 | new->hw.sar = *src; | 
|  | 512 | new->hw.dar = *dest; | 
|  | 513 | new->hw.tcr = copy_size; | 
|  | 514 |  | 
|  | 515 | if (!*first) { | 
|  | 516 | /* First desc */ | 
|  | 517 | new->async_tx.cookie = -EBUSY; | 
|  | 518 | *first = new; | 
|  | 519 | } else { | 
|  | 520 | /* Other desc - invisible to the user */ | 
|  | 521 | new->async_tx.cookie = -EINVAL; | 
|  | 522 | } | 
|  | 523 |  | 
| Guennadi Liakhovetski | cfefe99 | 2010-02-03 14:46:41 +0000 | [diff] [blame] | 524 | dev_dbg(sh_chan->dev, | 
|  | 525 | "chaining (%u/%u)@%x -> %x with %p, cookie %d, shift %d\n", | 
| Guennadi Liakhovetski | fc46185 | 2010-01-19 07:24:55 +0000 | [diff] [blame] | 526 | copy_size, *len, *src, *dest, &new->async_tx, | 
| Guennadi Liakhovetski | cfefe99 | 2010-02-03 14:46:41 +0000 | [diff] [blame] | 527 | new->async_tx.cookie, sh_chan->xmit_shift); | 
| Guennadi Liakhovetski | fc46185 | 2010-01-19 07:24:55 +0000 | [diff] [blame] | 528 |  | 
|  | 529 | new->mark = DESC_PREPARED; | 
|  | 530 | new->async_tx.flags = flags; | 
| Guennadi Liakhovetski | cfefe99 | 2010-02-03 14:46:41 +0000 | [diff] [blame] | 531 | new->direction = direction; | 
| Guennadi Liakhovetski | fc46185 | 2010-01-19 07:24:55 +0000 | [diff] [blame] | 532 |  | 
|  | 533 | *len -= copy_size; | 
|  | 534 | if (direction == DMA_BIDIRECTIONAL || direction == DMA_TO_DEVICE) | 
|  | 535 | *src += copy_size; | 
|  | 536 | if (direction == DMA_BIDIRECTIONAL || direction == DMA_FROM_DEVICE) | 
|  | 537 | *dest += copy_size; | 
|  | 538 |  | 
|  | 539 | return new; | 
|  | 540 | } | 
|  | 541 |  | 
|  | 542 | /* | 
|  | 543 | * sh_dmae_prep_sg - prepare transfer descriptors from an SG list | 
|  | 544 | * | 
|  | 545 | * Common routine for public (MEMCPY) and slave DMA. The MEMCPY case is also | 
|  | 546 | * converted to scatter-gather to guarantee consistent locking and a correct | 
|  | 547 | * list manipulation. For slave DMA direction carries the usual meaning, and, | 
|  | 548 | * logically, the SG list is RAM and the addr variable contains slave address, | 
|  | 549 | * e.g., the FIFO I/O register. For MEMCPY direction equals DMA_BIDIRECTIONAL | 
|  | 550 | * and the SG list contains only one element and points at the source buffer. | 
|  | 551 | */ | 
|  | 552 | static struct dma_async_tx_descriptor *sh_dmae_prep_sg(struct sh_dmae_chan *sh_chan, | 
|  | 553 | struct scatterlist *sgl, unsigned int sg_len, dma_addr_t *addr, | 
|  | 554 | enum dma_data_direction direction, unsigned long flags) | 
|  | 555 | { | 
|  | 556 | struct scatterlist *sg; | 
|  | 557 | struct sh_desc *first = NULL, *new = NULL /* compiler... */; | 
| Guennadi Liakhovetski | 3542a11 | 2009-12-17 09:41:39 -0700 | [diff] [blame] | 558 | LIST_HEAD(tx_list); | 
| Guennadi Liakhovetski | fc46185 | 2010-01-19 07:24:55 +0000 | [diff] [blame] | 559 | int chunks = 0; | 
| Guennadi Liakhovetski | b4dae6e | 2011-09-25 16:12:18 +0200 | [diff] [blame] | 560 | unsigned long irq_flags; | 
| Guennadi Liakhovetski | fc46185 | 2010-01-19 07:24:55 +0000 | [diff] [blame] | 561 | int i; | 
| Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 562 |  | 
| Guennadi Liakhovetski | fc46185 | 2010-01-19 07:24:55 +0000 | [diff] [blame] | 563 | if (!sg_len) | 
| Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 564 | return NULL; | 
|  | 565 |  | 
| Guennadi Liakhovetski | fc46185 | 2010-01-19 07:24:55 +0000 | [diff] [blame] | 566 | for_each_sg(sgl, sg, sg_len, i) | 
|  | 567 | chunks += (sg_dma_len(sg) + SH_DMA_TCR_MAX) / | 
|  | 568 | (SH_DMA_TCR_MAX + 1); | 
| Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 569 |  | 
| Guennadi Liakhovetski | 3542a11 | 2009-12-17 09:41:39 -0700 | [diff] [blame] | 570 | /* Have to lock the whole loop to protect against concurrent release */ | 
| Guennadi Liakhovetski | b4dae6e | 2011-09-25 16:12:18 +0200 | [diff] [blame] | 571 | spin_lock_irqsave(&sh_chan->desc_lock, irq_flags); | 
| Guennadi Liakhovetski | 3542a11 | 2009-12-17 09:41:39 -0700 | [diff] [blame] | 572 |  | 
|  | 573 | /* | 
|  | 574 | * Chaining: | 
|  | 575 | * first descriptor is what user is dealing with in all API calls, its | 
|  | 576 | *	cookie is at first set to -EBUSY, at tx-submit to a positive | 
|  | 577 | *	number | 
|  | 578 | * if more than one chunk is needed further chunks have cookie = -EINVAL | 
|  | 579 | * the last chunk, if not equal to the first, has cookie = -ENOSPC | 
|  | 580 | * all chunks are linked onto the tx_list head with their .node heads | 
|  | 581 | *	only during this function, then they are immediately spliced | 
|  | 582 | *	back onto the free list in form of a chain | 
|  | 583 | */ | 
| Guennadi Liakhovetski | fc46185 | 2010-01-19 07:24:55 +0000 | [diff] [blame] | 584 | for_each_sg(sgl, sg, sg_len, i) { | 
|  | 585 | dma_addr_t sg_addr = sg_dma_address(sg); | 
|  | 586 | size_t len = sg_dma_len(sg); | 
| Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 587 |  | 
| Guennadi Liakhovetski | fc46185 | 2010-01-19 07:24:55 +0000 | [diff] [blame] | 588 | if (!len) | 
|  | 589 | goto err_get_desc; | 
| Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 590 |  | 
| Guennadi Liakhovetski | fc46185 | 2010-01-19 07:24:55 +0000 | [diff] [blame] | 591 | do { | 
|  | 592 | dev_dbg(sh_chan->dev, "Add SG #%d@%p[%d], dma %llx\n", | 
|  | 593 | i, sg, len, (unsigned long long)sg_addr); | 
| Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 594 |  | 
| Guennadi Liakhovetski | fc46185 | 2010-01-19 07:24:55 +0000 | [diff] [blame] | 595 | if (direction == DMA_FROM_DEVICE) | 
|  | 596 | new = sh_dmae_add_desc(sh_chan, flags, | 
|  | 597 | &sg_addr, addr, &len, &first, | 
|  | 598 | direction); | 
|  | 599 | else | 
|  | 600 | new = sh_dmae_add_desc(sh_chan, flags, | 
|  | 601 | addr, &sg_addr, &len, &first, | 
|  | 602 | direction); | 
|  | 603 | if (!new) | 
|  | 604 | goto err_get_desc; | 
| Guennadi Liakhovetski | 3542a11 | 2009-12-17 09:41:39 -0700 | [diff] [blame] | 605 |  | 
| Guennadi Liakhovetski | fc46185 | 2010-01-19 07:24:55 +0000 | [diff] [blame] | 606 | new->chunks = chunks--; | 
|  | 607 | list_add_tail(&new->node, &tx_list); | 
|  | 608 | } while (len); | 
|  | 609 | } | 
| Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 610 |  | 
| Guennadi Liakhovetski | 3542a11 | 2009-12-17 09:41:39 -0700 | [diff] [blame] | 611 | if (new != first) | 
|  | 612 | new->async_tx.cookie = -ENOSPC; | 
|  | 613 |  | 
|  | 614 | /* Put them back on the free list, so, they don't get lost */ | 
|  | 615 | list_splice_tail(&tx_list, &sh_chan->ld_free); | 
|  | 616 |  | 
| Guennadi Liakhovetski | b4dae6e | 2011-09-25 16:12:18 +0200 | [diff] [blame] | 617 | spin_unlock_irqrestore(&sh_chan->desc_lock, irq_flags); | 
| Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 618 |  | 
|  | 619 | return &first->async_tx; | 
| Guennadi Liakhovetski | fc46185 | 2010-01-19 07:24:55 +0000 | [diff] [blame] | 620 |  | 
|  | 621 | err_get_desc: | 
|  | 622 | list_for_each_entry(new, &tx_list, node) | 
|  | 623 | new->mark = DESC_IDLE; | 
|  | 624 | list_splice(&tx_list, &sh_chan->ld_free); | 
|  | 625 |  | 
| Guennadi Liakhovetski | b4dae6e | 2011-09-25 16:12:18 +0200 | [diff] [blame] | 626 | spin_unlock_irqrestore(&sh_chan->desc_lock, irq_flags); | 
| Guennadi Liakhovetski | fc46185 | 2010-01-19 07:24:55 +0000 | [diff] [blame] | 627 |  | 
|  | 628 | return NULL; | 
|  | 629 | } | 
|  | 630 |  | 
|  | 631 | static struct dma_async_tx_descriptor *sh_dmae_prep_memcpy( | 
|  | 632 | struct dma_chan *chan, dma_addr_t dma_dest, dma_addr_t dma_src, | 
|  | 633 | size_t len, unsigned long flags) | 
|  | 634 | { | 
|  | 635 | struct sh_dmae_chan *sh_chan; | 
|  | 636 | struct scatterlist sg; | 
|  | 637 |  | 
|  | 638 | if (!chan || !len) | 
|  | 639 | return NULL; | 
|  | 640 |  | 
|  | 641 | sh_chan = to_sh_chan(chan); | 
|  | 642 |  | 
|  | 643 | sg_init_table(&sg, 1); | 
|  | 644 | sg_set_page(&sg, pfn_to_page(PFN_DOWN(dma_src)), len, | 
|  | 645 | offset_in_page(dma_src)); | 
|  | 646 | sg_dma_address(&sg) = dma_src; | 
|  | 647 | sg_dma_len(&sg) = len; | 
|  | 648 |  | 
|  | 649 | return sh_dmae_prep_sg(sh_chan, &sg, 1, &dma_dest, DMA_BIDIRECTIONAL, | 
|  | 650 | flags); | 
| Guennadi Liakhovetski | 3542a11 | 2009-12-17 09:41:39 -0700 | [diff] [blame] | 651 | } | 
| Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 652 |  | 
| Guennadi Liakhovetski | cfefe99 | 2010-02-03 14:46:41 +0000 | [diff] [blame] | 653 | static struct dma_async_tx_descriptor *sh_dmae_prep_slave_sg( | 
|  | 654 | struct dma_chan *chan, struct scatterlist *sgl, unsigned int sg_len, | 
|  | 655 | enum dma_data_direction direction, unsigned long flags) | 
|  | 656 | { | 
|  | 657 | struct sh_dmae_slave *param; | 
|  | 658 | struct sh_dmae_chan *sh_chan; | 
| Guennadi Liakhovetski | 5bac942 | 2010-04-21 15:36:49 +0000 | [diff] [blame] | 659 | dma_addr_t slave_addr; | 
| Guennadi Liakhovetski | cfefe99 | 2010-02-03 14:46:41 +0000 | [diff] [blame] | 660 |  | 
|  | 661 | if (!chan) | 
|  | 662 | return NULL; | 
|  | 663 |  | 
|  | 664 | sh_chan = to_sh_chan(chan); | 
|  | 665 | param = chan->private; | 
|  | 666 |  | 
|  | 667 | /* Someone calling slave DMA on a public channel? */ | 
|  | 668 | if (!param || !sg_len) { | 
|  | 669 | dev_warn(sh_chan->dev, "%s: bad parameter: %p, %d, %d\n", | 
|  | 670 | __func__, param, sg_len, param ? param->slave_id : -1); | 
|  | 671 | return NULL; | 
|  | 672 | } | 
|  | 673 |  | 
| Dan Carpenter | 9f9ff20 | 2010-08-14 11:01:45 +0200 | [diff] [blame] | 674 | slave_addr = param->config->addr; | 
|  | 675 |  | 
| Guennadi Liakhovetski | cfefe99 | 2010-02-03 14:46:41 +0000 | [diff] [blame] | 676 | /* | 
|  | 677 | * if (param != NULL), this is a successfully requested slave channel, | 
|  | 678 | * therefore param->config != NULL too. | 
|  | 679 | */ | 
| Guennadi Liakhovetski | 5bac942 | 2010-04-21 15:36:49 +0000 | [diff] [blame] | 680 | return sh_dmae_prep_sg(sh_chan, sgl, sg_len, &slave_addr, | 
| Guennadi Liakhovetski | cfefe99 | 2010-02-03 14:46:41 +0000 | [diff] [blame] | 681 | direction, flags); | 
|  | 682 | } | 
|  | 683 |  | 
| Linus Walleij | 0582763 | 2010-05-17 16:30:42 -0700 | [diff] [blame] | 684 | static int sh_dmae_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd, | 
|  | 685 | unsigned long arg) | 
| Guennadi Liakhovetski | cfefe99 | 2010-02-03 14:46:41 +0000 | [diff] [blame] | 686 | { | 
|  | 687 | struct sh_dmae_chan *sh_chan = to_sh_chan(chan); | 
| Guennadi Liakhovetski | b4dae6e | 2011-09-25 16:12:18 +0200 | [diff] [blame] | 688 | unsigned long flags; | 
| Guennadi Liakhovetski | cfefe99 | 2010-02-03 14:46:41 +0000 | [diff] [blame] | 689 |  | 
| Linus Walleij | c3635c7 | 2010-03-26 16:44:01 -0700 | [diff] [blame] | 690 | /* Only supports DMA_TERMINATE_ALL */ | 
|  | 691 | if (cmd != DMA_TERMINATE_ALL) | 
|  | 692 | return -ENXIO; | 
|  | 693 |  | 
| Guennadi Liakhovetski | cfefe99 | 2010-02-03 14:46:41 +0000 | [diff] [blame] | 694 | if (!chan) | 
| Linus Walleij | c3635c7 | 2010-03-26 16:44:01 -0700 | [diff] [blame] | 695 | return -EINVAL; | 
| Guennadi Liakhovetski | cfefe99 | 2010-02-03 14:46:41 +0000 | [diff] [blame] | 696 |  | 
| Guennadi Liakhovetski | b4dae6e | 2011-09-25 16:12:18 +0200 | [diff] [blame] | 697 | spin_lock_irqsave(&sh_chan->desc_lock, flags); | 
| Guennadi Liakhovetski | c014906 | 2010-02-18 16:30:02 +0000 | [diff] [blame] | 698 | dmae_halt(sh_chan); | 
|  | 699 |  | 
| Guennadi Liakhovetski | c014906 | 2010-02-18 16:30:02 +0000 | [diff] [blame] | 700 | if (!list_empty(&sh_chan->ld_queue)) { | 
|  | 701 | /* Record partial transfer */ | 
|  | 702 | struct sh_desc *desc = list_entry(sh_chan->ld_queue.next, | 
|  | 703 | struct sh_desc, node); | 
|  | 704 | desc->partial = (desc->hw.tcr - sh_dmae_readl(sh_chan, TCR)) << | 
|  | 705 | sh_chan->xmit_shift; | 
| Guennadi Liakhovetski | c014906 | 2010-02-18 16:30:02 +0000 | [diff] [blame] | 706 | } | 
| Guennadi Liakhovetski | b4dae6e | 2011-09-25 16:12:18 +0200 | [diff] [blame] | 707 | spin_unlock_irqrestore(&sh_chan->desc_lock, flags); | 
| Guennadi Liakhovetski | c014906 | 2010-02-18 16:30:02 +0000 | [diff] [blame] | 708 |  | 
| Guennadi Liakhovetski | cfefe99 | 2010-02-03 14:46:41 +0000 | [diff] [blame] | 709 | sh_dmae_chan_ld_cleanup(sh_chan, true); | 
| Linus Walleij | c3635c7 | 2010-03-26 16:44:01 -0700 | [diff] [blame] | 710 |  | 
|  | 711 | return 0; | 
| Guennadi Liakhovetski | cfefe99 | 2010-02-03 14:46:41 +0000 | [diff] [blame] | 712 | } | 
|  | 713 |  | 
| Guennadi Liakhovetski | 3542a11 | 2009-12-17 09:41:39 -0700 | [diff] [blame] | 714 | static dma_async_tx_callback __ld_cleanup(struct sh_dmae_chan *sh_chan, bool all) | 
|  | 715 | { | 
|  | 716 | struct sh_desc *desc, *_desc; | 
|  | 717 | /* Is the "exposed" head of a chain acked? */ | 
|  | 718 | bool head_acked = false; | 
|  | 719 | dma_cookie_t cookie = 0; | 
|  | 720 | dma_async_tx_callback callback = NULL; | 
|  | 721 | void *param = NULL; | 
| Guennadi Liakhovetski | b4dae6e | 2011-09-25 16:12:18 +0200 | [diff] [blame] | 722 | unsigned long flags; | 
| Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 723 |  | 
| Guennadi Liakhovetski | b4dae6e | 2011-09-25 16:12:18 +0200 | [diff] [blame] | 724 | spin_lock_irqsave(&sh_chan->desc_lock, flags); | 
| Guennadi Liakhovetski | 3542a11 | 2009-12-17 09:41:39 -0700 | [diff] [blame] | 725 | list_for_each_entry_safe(desc, _desc, &sh_chan->ld_queue, node) { | 
|  | 726 | struct dma_async_tx_descriptor *tx = &desc->async_tx; | 
|  | 727 |  | 
|  | 728 | BUG_ON(tx->cookie > 0 && tx->cookie != desc->cookie); | 
|  | 729 | BUG_ON(desc->mark != DESC_SUBMITTED && | 
|  | 730 | desc->mark != DESC_COMPLETED && | 
|  | 731 | desc->mark != DESC_WAITING); | 
|  | 732 |  | 
|  | 733 | /* | 
|  | 734 | * queue is ordered, and we use this loop to (1) clean up all | 
|  | 735 | * completed descriptors, and to (2) update descriptor flags of | 
|  | 736 | * any chunks in a (partially) completed chain | 
|  | 737 | */ | 
|  | 738 | if (!all && desc->mark == DESC_SUBMITTED && | 
|  | 739 | desc->cookie != cookie) | 
|  | 740 | break; | 
|  | 741 |  | 
|  | 742 | if (tx->cookie > 0) | 
|  | 743 | cookie = tx->cookie; | 
|  | 744 |  | 
|  | 745 | if (desc->mark == DESC_COMPLETED && desc->chunks == 1) { | 
| Guennadi Liakhovetski | cfefe99 | 2010-02-03 14:46:41 +0000 | [diff] [blame] | 746 | if (sh_chan->completed_cookie != desc->cookie - 1) | 
|  | 747 | dev_dbg(sh_chan->dev, | 
|  | 748 | "Completing cookie %d, expected %d\n", | 
|  | 749 | desc->cookie, | 
|  | 750 | sh_chan->completed_cookie + 1); | 
| Guennadi Liakhovetski | 3542a11 | 2009-12-17 09:41:39 -0700 | [diff] [blame] | 751 | sh_chan->completed_cookie = desc->cookie; | 
|  | 752 | } | 
|  | 753 |  | 
|  | 754 | /* Call callback on the last chunk */ | 
|  | 755 | if (desc->mark == DESC_COMPLETED && tx->callback) { | 
|  | 756 | desc->mark = DESC_WAITING; | 
|  | 757 | callback = tx->callback; | 
|  | 758 | param = tx->callback_param; | 
|  | 759 | dev_dbg(sh_chan->dev, "descriptor #%d@%p on %d callback\n", | 
|  | 760 | tx->cookie, tx, sh_chan->id); | 
|  | 761 | BUG_ON(desc->chunks != 1); | 
|  | 762 | break; | 
|  | 763 | } | 
|  | 764 |  | 
|  | 765 | if (tx->cookie > 0 || tx->cookie == -EBUSY) { | 
|  | 766 | if (desc->mark == DESC_COMPLETED) { | 
|  | 767 | BUG_ON(tx->cookie < 0); | 
|  | 768 | desc->mark = DESC_WAITING; | 
|  | 769 | } | 
|  | 770 | head_acked = async_tx_test_ack(tx); | 
|  | 771 | } else { | 
|  | 772 | switch (desc->mark) { | 
|  | 773 | case DESC_COMPLETED: | 
|  | 774 | desc->mark = DESC_WAITING; | 
|  | 775 | /* Fall through */ | 
|  | 776 | case DESC_WAITING: | 
|  | 777 | if (head_acked) | 
|  | 778 | async_tx_ack(&desc->async_tx); | 
|  | 779 | } | 
|  | 780 | } | 
|  | 781 |  | 
|  | 782 | dev_dbg(sh_chan->dev, "descriptor %p #%d completed.\n", | 
|  | 783 | tx, tx->cookie); | 
|  | 784 |  | 
|  | 785 | if (((desc->mark == DESC_COMPLETED || | 
|  | 786 | desc->mark == DESC_WAITING) && | 
|  | 787 | async_tx_test_ack(&desc->async_tx)) || all) { | 
|  | 788 | /* Remove from ld_queue list */ | 
|  | 789 | desc->mark = DESC_IDLE; | 
| Guennadi Liakhovetski | 7a1cd9a | 2011-08-18 16:55:27 +0200 | [diff] [blame] | 790 |  | 
| Guennadi Liakhovetski | 3542a11 | 2009-12-17 09:41:39 -0700 | [diff] [blame] | 791 | list_move(&desc->node, &sh_chan->ld_free); | 
| Guennadi Liakhovetski | 7a1cd9a | 2011-08-18 16:55:27 +0200 | [diff] [blame] | 792 |  | 
|  | 793 | if (list_empty(&sh_chan->ld_queue)) { | 
|  | 794 | dev_dbg(sh_chan->dev, "Bring down channel %d\n", sh_chan->id); | 
|  | 795 | pm_runtime_put(sh_chan->dev); | 
|  | 796 | } | 
| Guennadi Liakhovetski | 3542a11 | 2009-12-17 09:41:39 -0700 | [diff] [blame] | 797 | } | 
|  | 798 | } | 
| Guennadi Liakhovetski | 2dc6666 | 2011-04-29 17:09:21 +0000 | [diff] [blame] | 799 |  | 
|  | 800 | if (all && !callback) | 
|  | 801 | /* | 
|  | 802 | * Terminating and the loop completed normally: forgive | 
|  | 803 | * uncompleted cookies | 
|  | 804 | */ | 
|  | 805 | sh_chan->completed_cookie = sh_chan->common.cookie; | 
|  | 806 |  | 
| Guennadi Liakhovetski | b4dae6e | 2011-09-25 16:12:18 +0200 | [diff] [blame] | 807 | spin_unlock_irqrestore(&sh_chan->desc_lock, flags); | 
| Guennadi Liakhovetski | 3542a11 | 2009-12-17 09:41:39 -0700 | [diff] [blame] | 808 |  | 
|  | 809 | if (callback) | 
|  | 810 | callback(param); | 
|  | 811 |  | 
|  | 812 | return callback; | 
| Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 813 | } | 
|  | 814 |  | 
|  | 815 | /* | 
|  | 816 | * sh_chan_ld_cleanup - Clean up link descriptors | 
|  | 817 | * | 
| Guennadi Liakhovetski | 3542a11 | 2009-12-17 09:41:39 -0700 | [diff] [blame] | 818 | * This function cleans up the ld_queue of DMA channel. | 
| Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 819 | */ | 
| Guennadi Liakhovetski | 3542a11 | 2009-12-17 09:41:39 -0700 | [diff] [blame] | 820 | 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] | 821 | { | 
| Guennadi Liakhovetski | 3542a11 | 2009-12-17 09:41:39 -0700 | [diff] [blame] | 822 | while (__ld_cleanup(sh_chan, all)) | 
|  | 823 | ; | 
| Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 824 | } | 
|  | 825 |  | 
| Guennadi Liakhovetski | 7a1cd9a | 2011-08-18 16:55:27 +0200 | [diff] [blame] | 826 | /* Called under spin_lock_irq(&sh_chan->desc_lock) */ | 
| Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 827 | static void sh_chan_xfer_ld_queue(struct sh_dmae_chan *sh_chan) | 
|  | 828 | { | 
| Guennadi Liakhovetski | 47a4dc2 | 2010-02-11 16:50:05 +0000 | [diff] [blame] | 829 | struct sh_desc *desc; | 
| Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 830 |  | 
|  | 831 | /* DMA work check */ | 
| Guennadi Liakhovetski | 7a1cd9a | 2011-08-18 16:55:27 +0200 | [diff] [blame] | 832 | if (dmae_is_busy(sh_chan)) | 
| Guennadi Liakhovetski | b4dae6e | 2011-09-25 16:12:18 +0200 | [diff] [blame] | 833 | return; | 
| Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 834 |  | 
| Justin P. Mattock | 5a3a765 | 2011-01-19 15:36:38 +0100 | [diff] [blame] | 835 | /* Find the first not transferred descriptor */ | 
| Guennadi Liakhovetski | 47a4dc2 | 2010-02-11 16:50:05 +0000 | [diff] [blame] | 836 | list_for_each_entry(desc, &sh_chan->ld_queue, node) | 
|  | 837 | if (desc->mark == DESC_SUBMITTED) { | 
| Guennadi Liakhovetski | c014906 | 2010-02-18 16:30:02 +0000 | [diff] [blame] | 838 | dev_dbg(sh_chan->dev, "Queue #%d to %d: %u@%x -> %x\n", | 
|  | 839 | desc->async_tx.cookie, sh_chan->id, | 
|  | 840 | desc->hw.tcr, desc->hw.sar, desc->hw.dar); | 
| Guennadi Liakhovetski | 3542a11 | 2009-12-17 09:41:39 -0700 | [diff] [blame] | 841 | /* Get the ld start address from ld_queue */ | 
| Guennadi Liakhovetski | 47a4dc2 | 2010-02-11 16:50:05 +0000 | [diff] [blame] | 842 | dmae_set_reg(sh_chan, &desc->hw); | 
| Guennadi Liakhovetski | 3542a11 | 2009-12-17 09:41:39 -0700 | [diff] [blame] | 843 | dmae_start(sh_chan); | 
|  | 844 | break; | 
|  | 845 | } | 
| Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 846 | } | 
|  | 847 |  | 
|  | 848 | static void sh_dmae_memcpy_issue_pending(struct dma_chan *chan) | 
|  | 849 | { | 
|  | 850 | struct sh_dmae_chan *sh_chan = to_sh_chan(chan); | 
| Guennadi Liakhovetski | 7a1cd9a | 2011-08-18 16:55:27 +0200 | [diff] [blame] | 851 |  | 
|  | 852 | spin_lock_irq(&sh_chan->desc_lock); | 
|  | 853 | if (sh_chan->pm_state == DMAE_PM_ESTABLISHED) | 
|  | 854 | sh_chan_xfer_ld_queue(sh_chan); | 
|  | 855 | else | 
|  | 856 | sh_chan->pm_state = DMAE_PM_PENDING; | 
|  | 857 | spin_unlock_irq(&sh_chan->desc_lock); | 
| Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 858 | } | 
|  | 859 |  | 
| Linus Walleij | 0793448 | 2010-03-26 16:50:49 -0700 | [diff] [blame] | 860 | static enum dma_status sh_dmae_tx_status(struct dma_chan *chan, | 
| Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 861 | dma_cookie_t cookie, | 
| Linus Walleij | 0793448 | 2010-03-26 16:50:49 -0700 | [diff] [blame] | 862 | struct dma_tx_state *txstate) | 
| Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 863 | { | 
|  | 864 | struct sh_dmae_chan *sh_chan = to_sh_chan(chan); | 
|  | 865 | dma_cookie_t last_used; | 
|  | 866 | dma_cookie_t last_complete; | 
| Guennadi Liakhovetski | 47a4dc2 | 2010-02-11 16:50:05 +0000 | [diff] [blame] | 867 | enum dma_status status; | 
| Guennadi Liakhovetski | b4dae6e | 2011-09-25 16:12:18 +0200 | [diff] [blame] | 868 | unsigned long flags; | 
| Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 869 |  | 
| Guennadi Liakhovetski | 3542a11 | 2009-12-17 09:41:39 -0700 | [diff] [blame] | 870 | sh_dmae_chan_ld_cleanup(sh_chan, false); | 
| Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 871 |  | 
| Guennadi Liakhovetski | 2dc6666 | 2011-04-29 17:09:21 +0000 | [diff] [blame] | 872 | /* First read completed cookie to avoid a skew */ | 
| Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 873 | last_complete = sh_chan->completed_cookie; | 
| Guennadi Liakhovetski | 2dc6666 | 2011-04-29 17:09:21 +0000 | [diff] [blame] | 874 | rmb(); | 
|  | 875 | last_used = chan->cookie; | 
| Guennadi Liakhovetski | 3542a11 | 2009-12-17 09:41:39 -0700 | [diff] [blame] | 876 | BUG_ON(last_complete < 0); | 
| Dan Williams | bca3469 | 2010-03-26 16:52:10 -0700 | [diff] [blame] | 877 | dma_set_tx_state(txstate, last_complete, last_used, 0); | 
| Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 878 |  | 
| Guennadi Liakhovetski | b4dae6e | 2011-09-25 16:12:18 +0200 | [diff] [blame] | 879 | spin_lock_irqsave(&sh_chan->desc_lock, flags); | 
| Guennadi Liakhovetski | 47a4dc2 | 2010-02-11 16:50:05 +0000 | [diff] [blame] | 880 |  | 
|  | 881 | status = dma_async_is_complete(cookie, last_complete, last_used); | 
|  | 882 |  | 
|  | 883 | /* | 
|  | 884 | * If we don't find cookie on the queue, it has been aborted and we have | 
|  | 885 | * to report error | 
|  | 886 | */ | 
|  | 887 | if (status != DMA_SUCCESS) { | 
|  | 888 | struct sh_desc *desc; | 
|  | 889 | status = DMA_ERROR; | 
|  | 890 | list_for_each_entry(desc, &sh_chan->ld_queue, node) | 
|  | 891 | if (desc->cookie == cookie) { | 
|  | 892 | status = DMA_IN_PROGRESS; | 
|  | 893 | break; | 
|  | 894 | } | 
|  | 895 | } | 
|  | 896 |  | 
| Guennadi Liakhovetski | b4dae6e | 2011-09-25 16:12:18 +0200 | [diff] [blame] | 897 | spin_unlock_irqrestore(&sh_chan->desc_lock, flags); | 
| Guennadi Liakhovetski | 47a4dc2 | 2010-02-11 16:50:05 +0000 | [diff] [blame] | 898 |  | 
|  | 899 | return status; | 
| Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 900 | } | 
|  | 901 |  | 
|  | 902 | static irqreturn_t sh_dmae_interrupt(int irq, void *data) | 
|  | 903 | { | 
|  | 904 | irqreturn_t ret = IRQ_NONE; | 
| Guennadi Liakhovetski | 2dc6666 | 2011-04-29 17:09:21 +0000 | [diff] [blame] | 905 | struct sh_dmae_chan *sh_chan = data; | 
|  | 906 | u32 chcr; | 
|  | 907 |  | 
|  | 908 | spin_lock(&sh_chan->desc_lock); | 
|  | 909 |  | 
| Kuninori Morimoto | 5899a72 | 2011-06-17 08:20:40 +0000 | [diff] [blame] | 910 | chcr = chcr_read(sh_chan); | 
| Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 911 |  | 
|  | 912 | if (chcr & CHCR_TE) { | 
|  | 913 | /* DMA stop */ | 
|  | 914 | dmae_halt(sh_chan); | 
|  | 915 |  | 
|  | 916 | ret = IRQ_HANDLED; | 
|  | 917 | tasklet_schedule(&sh_chan->tasklet); | 
|  | 918 | } | 
|  | 919 |  | 
| Guennadi Liakhovetski | 2dc6666 | 2011-04-29 17:09:21 +0000 | [diff] [blame] | 920 | spin_unlock(&sh_chan->desc_lock); | 
|  | 921 |  | 
| Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 922 | return ret; | 
|  | 923 | } | 
|  | 924 |  | 
| Guennadi Liakhovetski | 2dc6666 | 2011-04-29 17:09:21 +0000 | [diff] [blame] | 925 | /* Called from error IRQ or NMI */ | 
|  | 926 | static bool sh_dmae_reset(struct sh_dmae_device *shdev) | 
| Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 927 | { | 
| Paul Mundt | 03aa18f | 2010-12-17 19:16:10 +0900 | [diff] [blame] | 928 | unsigned int handled = 0; | 
| Guennadi Liakhovetski | 47a4dc2 | 2010-02-11 16:50:05 +0000 | [diff] [blame] | 929 | int i; | 
| Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 930 |  | 
| Guennadi Liakhovetski | 47a4dc2 | 2010-02-11 16:50:05 +0000 | [diff] [blame] | 931 | /* halt the dma controller */ | 
| Guennadi Liakhovetski | 027811b | 2010-02-11 16:50:10 +0000 | [diff] [blame] | 932 | sh_dmae_ctl_stop(shdev); | 
| Guennadi Liakhovetski | 47a4dc2 | 2010-02-11 16:50:05 +0000 | [diff] [blame] | 933 |  | 
|  | 934 | /* We cannot detect, which channel caused the error, have to reset all */ | 
| Guennadi Liakhovetski | 8b1935e | 2010-02-11 16:50:14 +0000 | [diff] [blame] | 935 | for (i = 0; i < SH_DMAC_MAX_CHANNELS; i++) { | 
| Guennadi Liakhovetski | 47a4dc2 | 2010-02-11 16:50:05 +0000 | [diff] [blame] | 936 | struct sh_dmae_chan *sh_chan = shdev->chan[i]; | 
| Paul Mundt | 03aa18f | 2010-12-17 19:16:10 +0900 | [diff] [blame] | 937 | struct sh_desc *desc; | 
| Guennadi Liakhovetski | 2dc6666 | 2011-04-29 17:09:21 +0000 | [diff] [blame] | 938 | LIST_HEAD(dl); | 
| Paul Mundt | 03aa18f | 2010-12-17 19:16:10 +0900 | [diff] [blame] | 939 |  | 
|  | 940 | if (!sh_chan) | 
|  | 941 | continue; | 
|  | 942 |  | 
| Guennadi Liakhovetski | 2dc6666 | 2011-04-29 17:09:21 +0000 | [diff] [blame] | 943 | spin_lock(&sh_chan->desc_lock); | 
|  | 944 |  | 
| Paul Mundt | 03aa18f | 2010-12-17 19:16:10 +0900 | [diff] [blame] | 945 | /* Stop the channel */ | 
|  | 946 | dmae_halt(sh_chan); | 
|  | 947 |  | 
| Guennadi Liakhovetski | 2dc6666 | 2011-04-29 17:09:21 +0000 | [diff] [blame] | 948 | list_splice_init(&sh_chan->ld_queue, &dl); | 
|  | 949 |  | 
| Guennadi Liakhovetski | 7a1cd9a | 2011-08-18 16:55:27 +0200 | [diff] [blame] | 950 | if (!list_empty(&dl)) { | 
|  | 951 | dev_dbg(sh_chan->dev, "Bring down channel %d\n", sh_chan->id); | 
|  | 952 | pm_runtime_put(sh_chan->dev); | 
|  | 953 | } | 
|  | 954 | sh_chan->pm_state = DMAE_PM_ESTABLISHED; | 
|  | 955 |  | 
| Guennadi Liakhovetski | 2dc6666 | 2011-04-29 17:09:21 +0000 | [diff] [blame] | 956 | spin_unlock(&sh_chan->desc_lock); | 
|  | 957 |  | 
| Paul Mundt | 03aa18f | 2010-12-17 19:16:10 +0900 | [diff] [blame] | 958 | /* Complete all  */ | 
| Guennadi Liakhovetski | 2dc6666 | 2011-04-29 17:09:21 +0000 | [diff] [blame] | 959 | list_for_each_entry(desc, &dl, node) { | 
| Paul Mundt | 03aa18f | 2010-12-17 19:16:10 +0900 | [diff] [blame] | 960 | struct dma_async_tx_descriptor *tx = &desc->async_tx; | 
|  | 961 | desc->mark = DESC_IDLE; | 
|  | 962 | if (tx->callback) | 
|  | 963 | tx->callback(tx->callback_param); | 
| Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 964 | } | 
| Paul Mundt | 03aa18f | 2010-12-17 19:16:10 +0900 | [diff] [blame] | 965 |  | 
| Guennadi Liakhovetski | 2dc6666 | 2011-04-29 17:09:21 +0000 | [diff] [blame] | 966 | spin_lock(&sh_chan->desc_lock); | 
|  | 967 | list_splice(&dl, &sh_chan->ld_free); | 
|  | 968 | spin_unlock(&sh_chan->desc_lock); | 
|  | 969 |  | 
| Paul Mundt | 03aa18f | 2010-12-17 19:16:10 +0900 | [diff] [blame] | 970 | handled++; | 
| Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 971 | } | 
| Paul Mundt | 03aa18f | 2010-12-17 19:16:10 +0900 | [diff] [blame] | 972 |  | 
| Guennadi Liakhovetski | 027811b | 2010-02-11 16:50:10 +0000 | [diff] [blame] | 973 | sh_dmae_rst(shdev); | 
| Guennadi Liakhovetski | 47a4dc2 | 2010-02-11 16:50:05 +0000 | [diff] [blame] | 974 |  | 
| Paul Mundt | 03aa18f | 2010-12-17 19:16:10 +0900 | [diff] [blame] | 975 | return !!handled; | 
| Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 976 | } | 
| Paul Mundt | 03aa18f | 2010-12-17 19:16:10 +0900 | [diff] [blame] | 977 |  | 
|  | 978 | static irqreturn_t sh_dmae_err(int irq, void *data) | 
|  | 979 | { | 
| Yoshihiro Shimoda | ff7690b | 2011-02-09 07:46:47 +0000 | [diff] [blame] | 980 | struct sh_dmae_device *shdev = data; | 
|  | 981 |  | 
| Guennadi Liakhovetski | 2dc6666 | 2011-04-29 17:09:21 +0000 | [diff] [blame] | 982 | if (!(dmaor_read(shdev) & DMAOR_AE)) | 
| Yoshihiro Shimoda | ff7690b | 2011-02-09 07:46:47 +0000 | [diff] [blame] | 983 | return IRQ_NONE; | 
| Guennadi Liakhovetski | 2dc6666 | 2011-04-29 17:09:21 +0000 | [diff] [blame] | 984 |  | 
|  | 985 | sh_dmae_reset(data); | 
|  | 986 | return IRQ_HANDLED; | 
| Paul Mundt | 03aa18f | 2010-12-17 19:16:10 +0900 | [diff] [blame] | 987 | } | 
| Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 988 |  | 
|  | 989 | static void dmae_do_tasklet(unsigned long data) | 
|  | 990 | { | 
|  | 991 | struct sh_dmae_chan *sh_chan = (struct sh_dmae_chan *)data; | 
| Guennadi Liakhovetski | 3542a11 | 2009-12-17 09:41:39 -0700 | [diff] [blame] | 992 | struct sh_desc *desc; | 
| Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 993 | u32 sar_buf = sh_dmae_readl(sh_chan, SAR); | 
| Guennadi Liakhovetski | cfefe99 | 2010-02-03 14:46:41 +0000 | [diff] [blame] | 994 | u32 dar_buf = sh_dmae_readl(sh_chan, DAR); | 
| Guennadi Liakhovetski | 86d61b3 | 2009-12-10 18:35:07 +0100 | [diff] [blame] | 995 |  | 
| Guennadi Liakhovetski | b4dae6e | 2011-09-25 16:12:18 +0200 | [diff] [blame] | 996 | spin_lock_irq(&sh_chan->desc_lock); | 
| Guennadi Liakhovetski | 3542a11 | 2009-12-17 09:41:39 -0700 | [diff] [blame] | 997 | list_for_each_entry(desc, &sh_chan->ld_queue, node) { | 
| Guennadi Liakhovetski | cfefe99 | 2010-02-03 14:46:41 +0000 | [diff] [blame] | 998 | if (desc->mark == DESC_SUBMITTED && | 
|  | 999 | ((desc->direction == DMA_FROM_DEVICE && | 
|  | 1000 | (desc->hw.dar + desc->hw.tcr) == dar_buf) || | 
|  | 1001 | (desc->hw.sar + desc->hw.tcr) == sar_buf)) { | 
| Guennadi Liakhovetski | 3542a11 | 2009-12-17 09:41:39 -0700 | [diff] [blame] | 1002 | dev_dbg(sh_chan->dev, "done #%d@%p dst %u\n", | 
|  | 1003 | desc->async_tx.cookie, &desc->async_tx, | 
|  | 1004 | desc->hw.dar); | 
|  | 1005 | desc->mark = DESC_COMPLETED; | 
| Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 1006 | break; | 
|  | 1007 | } | 
|  | 1008 | } | 
| Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 1009 | /* Next desc */ | 
|  | 1010 | sh_chan_xfer_ld_queue(sh_chan); | 
| Guennadi Liakhovetski | 7a1cd9a | 2011-08-18 16:55:27 +0200 | [diff] [blame] | 1011 | spin_unlock_irq(&sh_chan->desc_lock); | 
|  | 1012 |  | 
| Guennadi Liakhovetski | 3542a11 | 2009-12-17 09:41:39 -0700 | [diff] [blame] | 1013 | sh_dmae_chan_ld_cleanup(sh_chan, false); | 
| Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 1014 | } | 
|  | 1015 |  | 
| Paul Mundt | 03aa18f | 2010-12-17 19:16:10 +0900 | [diff] [blame] | 1016 | static bool sh_dmae_nmi_notify(struct sh_dmae_device *shdev) | 
|  | 1017 | { | 
| Paul Mundt | 03aa18f | 2010-12-17 19:16:10 +0900 | [diff] [blame] | 1018 | /* Fast path out if NMIF is not asserted for this controller */ | 
|  | 1019 | if ((dmaor_read(shdev) & DMAOR_NMIF) == 0) | 
|  | 1020 | return false; | 
|  | 1021 |  | 
| Guennadi Liakhovetski | 2dc6666 | 2011-04-29 17:09:21 +0000 | [diff] [blame] | 1022 | return sh_dmae_reset(shdev); | 
| Paul Mundt | 03aa18f | 2010-12-17 19:16:10 +0900 | [diff] [blame] | 1023 | } | 
|  | 1024 |  | 
|  | 1025 | static int sh_dmae_nmi_handler(struct notifier_block *self, | 
|  | 1026 | unsigned long cmd, void *data) | 
|  | 1027 | { | 
|  | 1028 | struct sh_dmae_device *shdev; | 
|  | 1029 | int ret = NOTIFY_DONE; | 
|  | 1030 | bool triggered; | 
|  | 1031 |  | 
|  | 1032 | /* | 
|  | 1033 | * Only concern ourselves with NMI events. | 
|  | 1034 | * | 
|  | 1035 | * Normally we would check the die chain value, but as this needs | 
|  | 1036 | * to be architecture independent, check for NMI context instead. | 
|  | 1037 | */ | 
|  | 1038 | if (!in_nmi()) | 
|  | 1039 | return NOTIFY_DONE; | 
|  | 1040 |  | 
|  | 1041 | rcu_read_lock(); | 
|  | 1042 | list_for_each_entry_rcu(shdev, &sh_dmae_devices, node) { | 
|  | 1043 | /* | 
|  | 1044 | * Only stop if one of the controllers has NMIF asserted, | 
|  | 1045 | * we do not want to interfere with regular address error | 
|  | 1046 | * handling or NMI events that don't concern the DMACs. | 
|  | 1047 | */ | 
|  | 1048 | triggered = sh_dmae_nmi_notify(shdev); | 
|  | 1049 | if (triggered == true) | 
|  | 1050 | ret = NOTIFY_OK; | 
|  | 1051 | } | 
|  | 1052 | rcu_read_unlock(); | 
|  | 1053 |  | 
|  | 1054 | return ret; | 
|  | 1055 | } | 
|  | 1056 |  | 
|  | 1057 | static struct notifier_block sh_dmae_nmi_notifier __read_mostly = { | 
|  | 1058 | .notifier_call	= sh_dmae_nmi_handler, | 
|  | 1059 |  | 
|  | 1060 | /* Run before NMI debug handler and KGDB */ | 
|  | 1061 | .priority	= 1, | 
|  | 1062 | }; | 
|  | 1063 |  | 
| Guennadi Liakhovetski | 027811b | 2010-02-11 16:50:10 +0000 | [diff] [blame] | 1064 | static int __devinit sh_dmae_chan_probe(struct sh_dmae_device *shdev, int id, | 
|  | 1065 | int irq, unsigned long flags) | 
| Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 1066 | { | 
|  | 1067 | int err; | 
| Guennadi Liakhovetski | 5bac942 | 2010-04-21 15:36:49 +0000 | [diff] [blame] | 1068 | const struct sh_dmae_channel *chan_pdata = &shdev->pdata->channel[id]; | 
| Guennadi Liakhovetski | 027811b | 2010-02-11 16:50:10 +0000 | [diff] [blame] | 1069 | struct platform_device *pdev = to_platform_device(shdev->common.dev); | 
| Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 1070 | struct sh_dmae_chan *new_sh_chan; | 
|  | 1071 |  | 
|  | 1072 | /* alloc channel */ | 
|  | 1073 | new_sh_chan = kzalloc(sizeof(struct sh_dmae_chan), GFP_KERNEL); | 
|  | 1074 | if (!new_sh_chan) { | 
| Guennadi Liakhovetski | 86d61b3 | 2009-12-10 18:35:07 +0100 | [diff] [blame] | 1075 | dev_err(shdev->common.dev, | 
|  | 1076 | "No free memory for allocating dma channels!\n"); | 
| Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 1077 | return -ENOMEM; | 
|  | 1078 | } | 
|  | 1079 |  | 
| Guennadi Liakhovetski | 7a1cd9a | 2011-08-18 16:55:27 +0200 | [diff] [blame] | 1080 | new_sh_chan->pm_state = DMAE_PM_ESTABLISHED; | 
|  | 1081 |  | 
|  | 1082 | /* reference struct dma_device */ | 
| Guennadi Liakhovetski | 8b1935e | 2010-02-11 16:50:14 +0000 | [diff] [blame] | 1083 | new_sh_chan->common.device = &shdev->common; | 
|  | 1084 |  | 
| Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 1085 | new_sh_chan->dev = shdev->common.dev; | 
|  | 1086 | new_sh_chan->id = id; | 
| Guennadi Liakhovetski | 027811b | 2010-02-11 16:50:10 +0000 | [diff] [blame] | 1087 | new_sh_chan->irq = irq; | 
|  | 1088 | new_sh_chan->base = shdev->chan_reg + chan_pdata->offset / sizeof(u32); | 
| Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 1089 |  | 
|  | 1090 | /* Init DMA tasklet */ | 
|  | 1091 | tasklet_init(&new_sh_chan->tasklet, dmae_do_tasklet, | 
|  | 1092 | (unsigned long)new_sh_chan); | 
|  | 1093 |  | 
| Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 1094 | spin_lock_init(&new_sh_chan->desc_lock); | 
|  | 1095 |  | 
|  | 1096 | /* Init descripter manage list */ | 
|  | 1097 | INIT_LIST_HEAD(&new_sh_chan->ld_queue); | 
|  | 1098 | INIT_LIST_HEAD(&new_sh_chan->ld_free); | 
|  | 1099 |  | 
| Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 1100 | /* Add the channel to DMA device channel list */ | 
|  | 1101 | list_add_tail(&new_sh_chan->common.device_node, | 
|  | 1102 | &shdev->common.channels); | 
|  | 1103 | shdev->common.chancnt++; | 
|  | 1104 |  | 
| Guennadi Liakhovetski | 027811b | 2010-02-11 16:50:10 +0000 | [diff] [blame] | 1105 | if (pdev->id >= 0) | 
|  | 1106 | snprintf(new_sh_chan->dev_id, sizeof(new_sh_chan->dev_id), | 
|  | 1107 | "sh-dmae%d.%d", pdev->id, new_sh_chan->id); | 
|  | 1108 | else | 
|  | 1109 | snprintf(new_sh_chan->dev_id, sizeof(new_sh_chan->dev_id), | 
|  | 1110 | "sh-dma%d", new_sh_chan->id); | 
| Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 1111 |  | 
|  | 1112 | /* set up channel irq */ | 
| Guennadi Liakhovetski | 027811b | 2010-02-11 16:50:10 +0000 | [diff] [blame] | 1113 | err = request_irq(irq, &sh_dmae_interrupt, flags, | 
| Guennadi Liakhovetski | 86d61b3 | 2009-12-10 18:35:07 +0100 | [diff] [blame] | 1114 | new_sh_chan->dev_id, new_sh_chan); | 
| Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 1115 | if (err) { | 
|  | 1116 | dev_err(shdev->common.dev, "DMA channel %d request_irq error " | 
|  | 1117 | "with return %d\n", id, err); | 
|  | 1118 | goto err_no_irq; | 
|  | 1119 | } | 
|  | 1120 |  | 
| Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 1121 | shdev->chan[id] = new_sh_chan; | 
|  | 1122 | return 0; | 
|  | 1123 |  | 
|  | 1124 | err_no_irq: | 
|  | 1125 | /* remove from dmaengine device node */ | 
|  | 1126 | list_del(&new_sh_chan->common.device_node); | 
|  | 1127 | kfree(new_sh_chan); | 
|  | 1128 | return err; | 
|  | 1129 | } | 
|  | 1130 |  | 
|  | 1131 | static void sh_dmae_chan_remove(struct sh_dmae_device *shdev) | 
|  | 1132 | { | 
|  | 1133 | int i; | 
|  | 1134 |  | 
|  | 1135 | for (i = shdev->common.chancnt - 1 ; i >= 0 ; i--) { | 
|  | 1136 | if (shdev->chan[i]) { | 
| Guennadi Liakhovetski | 027811b | 2010-02-11 16:50:10 +0000 | [diff] [blame] | 1137 | struct sh_dmae_chan *sh_chan = shdev->chan[i]; | 
| Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 1138 |  | 
| Guennadi Liakhovetski | 027811b | 2010-02-11 16:50:10 +0000 | [diff] [blame] | 1139 | free_irq(sh_chan->irq, sh_chan); | 
|  | 1140 |  | 
|  | 1141 | list_del(&sh_chan->common.device_node); | 
|  | 1142 | kfree(sh_chan); | 
| Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 1143 | shdev->chan[i] = NULL; | 
|  | 1144 | } | 
|  | 1145 | } | 
|  | 1146 | shdev->common.chancnt = 0; | 
|  | 1147 | } | 
|  | 1148 |  | 
|  | 1149 | static int __init sh_dmae_probe(struct platform_device *pdev) | 
|  | 1150 | { | 
| Guennadi Liakhovetski | 027811b | 2010-02-11 16:50:10 +0000 | [diff] [blame] | 1151 | struct sh_dmae_pdata *pdata = pdev->dev.platform_data; | 
|  | 1152 | unsigned long irqflags = IRQF_DISABLED, | 
| Guennadi Liakhovetski | 8b1935e | 2010-02-11 16:50:14 +0000 | [diff] [blame] | 1153 | chan_flag[SH_DMAC_MAX_CHANNELS] = {}; | 
|  | 1154 | int errirq, chan_irq[SH_DMAC_MAX_CHANNELS]; | 
| Magnus Damm | 300e5f9 | 2011-05-24 10:31:20 +0000 | [diff] [blame] | 1155 | int err, i, irq_cnt = 0, irqres = 0, irq_cap = 0; | 
| Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 1156 | struct sh_dmae_device *shdev; | 
| Guennadi Liakhovetski | 027811b | 2010-02-11 16:50:10 +0000 | [diff] [blame] | 1157 | struct resource *chan, *dmars, *errirq_res, *chanirq_res; | 
| Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 1158 |  | 
| Dan Williams | 56adf7e | 2009-11-22 12:10:10 -0700 | [diff] [blame] | 1159 | /* get platform data */ | 
| Guennadi Liakhovetski | 027811b | 2010-02-11 16:50:10 +0000 | [diff] [blame] | 1160 | if (!pdata || !pdata->channel_num) | 
| Dan Williams | 56adf7e | 2009-11-22 12:10:10 -0700 | [diff] [blame] | 1161 | return -ENODEV; | 
|  | 1162 |  | 
| Guennadi Liakhovetski | 027811b | 2010-02-11 16:50:10 +0000 | [diff] [blame] | 1163 | chan = platform_get_resource(pdev, IORESOURCE_MEM, 0); | 
| Magnus Damm | 26fc02a | 2011-05-24 10:31:12 +0000 | [diff] [blame] | 1164 | /* DMARS area is optional */ | 
| Guennadi Liakhovetski | 027811b | 2010-02-11 16:50:10 +0000 | [diff] [blame] | 1165 | dmars = platform_get_resource(pdev, IORESOURCE_MEM, 1); | 
|  | 1166 | /* | 
|  | 1167 | * IRQ resources: | 
|  | 1168 | * 1. there always must be at least one IRQ IO-resource. On SH4 it is | 
|  | 1169 | *    the error IRQ, in which case it is the only IRQ in this resource: | 
|  | 1170 | *    start == end. If it is the only IRQ resource, all channels also | 
|  | 1171 | *    use the same IRQ. | 
|  | 1172 | * 2. DMA channel IRQ resources can be specified one per resource or in | 
|  | 1173 | *    ranges (start != end) | 
|  | 1174 | * 3. iff all events (channels and, optionally, error) on this | 
|  | 1175 | *    controller use the same IRQ, only one IRQ resource can be | 
|  | 1176 | *    specified, otherwise there must be one IRQ per channel, even if | 
|  | 1177 | *    some of them are equal | 
|  | 1178 | * 4. if all IRQs on this controller are equal or if some specific IRQs | 
|  | 1179 | *    specify IORESOURCE_IRQ_SHAREABLE in their resources, they will be | 
|  | 1180 | *    requested with the IRQF_SHARED flag | 
|  | 1181 | */ | 
|  | 1182 | errirq_res = platform_get_resource(pdev, IORESOURCE_IRQ, 0); | 
|  | 1183 | if (!chan || !errirq_res) | 
|  | 1184 | return -ENODEV; | 
|  | 1185 |  | 
|  | 1186 | if (!request_mem_region(chan->start, resource_size(chan), pdev->name)) { | 
|  | 1187 | dev_err(&pdev->dev, "DMAC register region already claimed\n"); | 
|  | 1188 | return -EBUSY; | 
|  | 1189 | } | 
|  | 1190 |  | 
|  | 1191 | if (dmars && !request_mem_region(dmars->start, resource_size(dmars), pdev->name)) { | 
|  | 1192 | dev_err(&pdev->dev, "DMAC DMARS region already claimed\n"); | 
|  | 1193 | err = -EBUSY; | 
|  | 1194 | goto ermrdmars; | 
|  | 1195 | } | 
|  | 1196 |  | 
|  | 1197 | err = -ENOMEM; | 
| Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 1198 | shdev = kzalloc(sizeof(struct sh_dmae_device), GFP_KERNEL); | 
|  | 1199 | if (!shdev) { | 
| Guennadi Liakhovetski | 027811b | 2010-02-11 16:50:10 +0000 | [diff] [blame] | 1200 | dev_err(&pdev->dev, "Not enough memory\n"); | 
|  | 1201 | goto ealloc; | 
|  | 1202 | } | 
|  | 1203 |  | 
|  | 1204 | shdev->chan_reg = ioremap(chan->start, resource_size(chan)); | 
|  | 1205 | if (!shdev->chan_reg) | 
|  | 1206 | goto emapchan; | 
|  | 1207 | if (dmars) { | 
|  | 1208 | shdev->dmars = ioremap(dmars->start, resource_size(dmars)); | 
|  | 1209 | if (!shdev->dmars) | 
|  | 1210 | goto emapdmars; | 
| Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 1211 | } | 
|  | 1212 |  | 
| Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 1213 | /* platform data */ | 
| Guennadi Liakhovetski | 027811b | 2010-02-11 16:50:10 +0000 | [diff] [blame] | 1214 | shdev->pdata = pdata; | 
| Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 1215 |  | 
| Kuninori Morimoto | 5899a72 | 2011-06-17 08:20:40 +0000 | [diff] [blame] | 1216 | if (pdata->chcr_offset) | 
|  | 1217 | shdev->chcr_offset = pdata->chcr_offset; | 
|  | 1218 | else | 
|  | 1219 | shdev->chcr_offset = CHCR; | 
|  | 1220 |  | 
| Kuninori Morimoto | 67c6269 | 2011-06-17 08:20:51 +0000 | [diff] [blame] | 1221 | if (pdata->chcr_ie_bit) | 
|  | 1222 | shdev->chcr_ie_bit = pdata->chcr_ie_bit; | 
|  | 1223 | else | 
|  | 1224 | shdev->chcr_ie_bit = CHCR_IE; | 
|  | 1225 |  | 
| Paul Mundt | 5c2de44 | 2011-05-31 15:53:03 +0900 | [diff] [blame] | 1226 | platform_set_drvdata(pdev, shdev); | 
|  | 1227 |  | 
| Guennadi Liakhovetski | 20f2a3b | 2010-02-11 16:50:18 +0000 | [diff] [blame] | 1228 | pm_runtime_enable(&pdev->dev); | 
|  | 1229 | pm_runtime_get_sync(&pdev->dev); | 
|  | 1230 |  | 
| Guennadi Liakhovetski | 31705e2 | 2011-05-02 07:59:02 +0000 | [diff] [blame] | 1231 | spin_lock_irq(&sh_dmae_lock); | 
| Paul Mundt | 03aa18f | 2010-12-17 19:16:10 +0900 | [diff] [blame] | 1232 | list_add_tail_rcu(&shdev->node, &sh_dmae_devices); | 
| Guennadi Liakhovetski | 31705e2 | 2011-05-02 07:59:02 +0000 | [diff] [blame] | 1233 | spin_unlock_irq(&sh_dmae_lock); | 
| Paul Mundt | 03aa18f | 2010-12-17 19:16:10 +0900 | [diff] [blame] | 1234 |  | 
| Guennadi Liakhovetski | 2dc6666 | 2011-04-29 17:09:21 +0000 | [diff] [blame] | 1235 | /* reset dma controller - only needed as a test */ | 
| Guennadi Liakhovetski | 027811b | 2010-02-11 16:50:10 +0000 | [diff] [blame] | 1236 | err = sh_dmae_rst(shdev); | 
| Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 1237 | if (err) | 
|  | 1238 | goto rst_err; | 
|  | 1239 |  | 
| Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 1240 | INIT_LIST_HEAD(&shdev->common.channels); | 
|  | 1241 |  | 
|  | 1242 | dma_cap_set(DMA_MEMCPY, shdev->common.cap_mask); | 
| Magnus Damm | 26fc02a | 2011-05-24 10:31:12 +0000 | [diff] [blame] | 1243 | if (pdata->slave && pdata->slave_num) | 
| Guennadi Liakhovetski | 027811b | 2010-02-11 16:50:10 +0000 | [diff] [blame] | 1244 | dma_cap_set(DMA_SLAVE, shdev->common.cap_mask); | 
| Guennadi Liakhovetski | cfefe99 | 2010-02-03 14:46:41 +0000 | [diff] [blame] | 1245 |  | 
| Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 1246 | shdev->common.device_alloc_chan_resources | 
|  | 1247 | = sh_dmae_alloc_chan_resources; | 
|  | 1248 | shdev->common.device_free_chan_resources = sh_dmae_free_chan_resources; | 
|  | 1249 | shdev->common.device_prep_dma_memcpy = sh_dmae_prep_memcpy; | 
| Linus Walleij | 0793448 | 2010-03-26 16:50:49 -0700 | [diff] [blame] | 1250 | shdev->common.device_tx_status = sh_dmae_tx_status; | 
| Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 1251 | shdev->common.device_issue_pending = sh_dmae_memcpy_issue_pending; | 
| Guennadi Liakhovetski | cfefe99 | 2010-02-03 14:46:41 +0000 | [diff] [blame] | 1252 |  | 
|  | 1253 | /* Compulsory for DMA_SLAVE fields */ | 
|  | 1254 | shdev->common.device_prep_slave_sg = sh_dmae_prep_slave_sg; | 
| Linus Walleij | c3635c7 | 2010-03-26 16:44:01 -0700 | [diff] [blame] | 1255 | shdev->common.device_control = sh_dmae_control; | 
| Guennadi Liakhovetski | cfefe99 | 2010-02-03 14:46:41 +0000 | [diff] [blame] | 1256 |  | 
| Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 1257 | shdev->common.dev = &pdev->dev; | 
| Guennadi Liakhovetski | ddb4f0f | 2009-12-04 19:44:41 +0100 | [diff] [blame] | 1258 | /* Default transfer size of 32 bytes requires 32-byte alignment */ | 
| Guennadi Liakhovetski | 8b1935e | 2010-02-11 16:50:14 +0000 | [diff] [blame] | 1259 | shdev->common.copy_align = LOG2_DEFAULT_XFER_SIZE; | 
| Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 1260 |  | 
| Magnus Damm | 927a7c9 | 2010-03-19 04:47:19 +0000 | [diff] [blame] | 1261 | #if defined(CONFIG_CPU_SH4) || defined(CONFIG_ARCH_SHMOBILE) | 
| Guennadi Liakhovetski | 027811b | 2010-02-11 16:50:10 +0000 | [diff] [blame] | 1262 | chanirq_res = platform_get_resource(pdev, IORESOURCE_IRQ, 1); | 
|  | 1263 |  | 
|  | 1264 | if (!chanirq_res) | 
|  | 1265 | chanirq_res = errirq_res; | 
|  | 1266 | else | 
|  | 1267 | irqres++; | 
|  | 1268 |  | 
|  | 1269 | if (chanirq_res == errirq_res || | 
|  | 1270 | (errirq_res->flags & IORESOURCE_BITS) == IORESOURCE_IRQ_SHAREABLE) | 
| Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 1271 | irqflags = IRQF_SHARED; | 
| Guennadi Liakhovetski | 027811b | 2010-02-11 16:50:10 +0000 | [diff] [blame] | 1272 |  | 
|  | 1273 | errirq = errirq_res->start; | 
|  | 1274 |  | 
|  | 1275 | err = request_irq(errirq, sh_dmae_err, irqflags, | 
|  | 1276 | "DMAC Address Error", shdev); | 
|  | 1277 | if (err) { | 
|  | 1278 | dev_err(&pdev->dev, | 
|  | 1279 | "DMA failed requesting irq #%d, error %d\n", | 
|  | 1280 | errirq, err); | 
|  | 1281 | goto eirq_err; | 
| Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 1282 | } | 
|  | 1283 |  | 
| Guennadi Liakhovetski | 027811b | 2010-02-11 16:50:10 +0000 | [diff] [blame] | 1284 | #else | 
|  | 1285 | chanirq_res = errirq_res; | 
| Magnus Damm | 927a7c9 | 2010-03-19 04:47:19 +0000 | [diff] [blame] | 1286 | #endif /* CONFIG_CPU_SH4 || CONFIG_ARCH_SHMOBILE */ | 
| Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 1287 |  | 
| Guennadi Liakhovetski | 027811b | 2010-02-11 16:50:10 +0000 | [diff] [blame] | 1288 | if (chanirq_res->start == chanirq_res->end && | 
|  | 1289 | !platform_get_resource(pdev, IORESOURCE_IRQ, 1)) { | 
|  | 1290 | /* Special case - all multiplexed */ | 
|  | 1291 | for (; irq_cnt < pdata->channel_num; irq_cnt++) { | 
| Magnus Damm | 300e5f9 | 2011-05-24 10:31:20 +0000 | [diff] [blame] | 1292 | if (irq_cnt < SH_DMAC_MAX_CHANNELS) { | 
|  | 1293 | chan_irq[irq_cnt] = chanirq_res->start; | 
|  | 1294 | chan_flag[irq_cnt] = IRQF_SHARED; | 
|  | 1295 | } else { | 
|  | 1296 | irq_cap = 1; | 
|  | 1297 | break; | 
|  | 1298 | } | 
| Guennadi Liakhovetski | 027811b | 2010-02-11 16:50:10 +0000 | [diff] [blame] | 1299 | } | 
|  | 1300 | } else { | 
|  | 1301 | do { | 
|  | 1302 | for (i = chanirq_res->start; i <= chanirq_res->end; i++) { | 
| Magnus Damm | dcee0bb | 2011-06-09 06:35:08 +0000 | [diff] [blame] | 1303 | if (irq_cnt >= SH_DMAC_MAX_CHANNELS) { | 
|  | 1304 | irq_cap = 1; | 
|  | 1305 | break; | 
|  | 1306 | } | 
|  | 1307 |  | 
| Guennadi Liakhovetski | 027811b | 2010-02-11 16:50:10 +0000 | [diff] [blame] | 1308 | if ((errirq_res->flags & IORESOURCE_BITS) == | 
|  | 1309 | IORESOURCE_IRQ_SHAREABLE) | 
|  | 1310 | chan_flag[irq_cnt] = IRQF_SHARED; | 
|  | 1311 | else | 
|  | 1312 | chan_flag[irq_cnt] = IRQF_DISABLED; | 
|  | 1313 | dev_dbg(&pdev->dev, | 
|  | 1314 | "Found IRQ %d for channel %d\n", | 
|  | 1315 | i, irq_cnt); | 
|  | 1316 | chan_irq[irq_cnt++] = i; | 
| Magnus Damm | 300e5f9 | 2011-05-24 10:31:20 +0000 | [diff] [blame] | 1317 | } | 
|  | 1318 |  | 
| Magnus Damm | dcee0bb | 2011-06-09 06:35:08 +0000 | [diff] [blame] | 1319 | if (irq_cnt >= SH_DMAC_MAX_CHANNELS) | 
| Magnus Damm | 300e5f9 | 2011-05-24 10:31:20 +0000 | [diff] [blame] | 1320 | break; | 
| Magnus Damm | dcee0bb | 2011-06-09 06:35:08 +0000 | [diff] [blame] | 1321 |  | 
| Guennadi Liakhovetski | 027811b | 2010-02-11 16:50:10 +0000 | [diff] [blame] | 1322 | chanirq_res = platform_get_resource(pdev, | 
|  | 1323 | IORESOURCE_IRQ, ++irqres); | 
|  | 1324 | } while (irq_cnt < pdata->channel_num && chanirq_res); | 
|  | 1325 | } | 
|  | 1326 |  | 
| Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 1327 | /* Create DMA Channel */ | 
| Magnus Damm | 300e5f9 | 2011-05-24 10:31:20 +0000 | [diff] [blame] | 1328 | for (i = 0; i < irq_cnt; i++) { | 
| Guennadi Liakhovetski | 027811b | 2010-02-11 16:50:10 +0000 | [diff] [blame] | 1329 | 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] | 1330 | if (err) | 
|  | 1331 | goto chan_probe_err; | 
|  | 1332 | } | 
|  | 1333 |  | 
| Magnus Damm | 300e5f9 | 2011-05-24 10:31:20 +0000 | [diff] [blame] | 1334 | if (irq_cap) | 
|  | 1335 | dev_notice(&pdev->dev, "Attempting to register %d DMA " | 
|  | 1336 | "channels when a maximum of %d are supported.\n", | 
|  | 1337 | pdata->channel_num, SH_DMAC_MAX_CHANNELS); | 
|  | 1338 |  | 
| Guennadi Liakhovetski | 20f2a3b | 2010-02-11 16:50:18 +0000 | [diff] [blame] | 1339 | pm_runtime_put(&pdev->dev); | 
|  | 1340 |  | 
| Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 1341 | dma_async_device_register(&shdev->common); | 
|  | 1342 |  | 
|  | 1343 | return err; | 
|  | 1344 |  | 
|  | 1345 | chan_probe_err: | 
|  | 1346 | sh_dmae_chan_remove(shdev); | 
| Magnus Damm | 300e5f9 | 2011-05-24 10:31:20 +0000 | [diff] [blame] | 1347 |  | 
| Magnus Damm | 927a7c9 | 2010-03-19 04:47:19 +0000 | [diff] [blame] | 1348 | #if defined(CONFIG_CPU_SH4) || defined(CONFIG_ARCH_SHMOBILE) | 
| Guennadi Liakhovetski | 027811b | 2010-02-11 16:50:10 +0000 | [diff] [blame] | 1349 | free_irq(errirq, shdev); | 
| Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 1350 | eirq_err: | 
| Guennadi Liakhovetski | 027811b | 2010-02-11 16:50:10 +0000 | [diff] [blame] | 1351 | #endif | 
| Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 1352 | rst_err: | 
| Guennadi Liakhovetski | 31705e2 | 2011-05-02 07:59:02 +0000 | [diff] [blame] | 1353 | spin_lock_irq(&sh_dmae_lock); | 
| Paul Mundt | 03aa18f | 2010-12-17 19:16:10 +0900 | [diff] [blame] | 1354 | list_del_rcu(&shdev->node); | 
| Guennadi Liakhovetski | 31705e2 | 2011-05-02 07:59:02 +0000 | [diff] [blame] | 1355 | spin_unlock_irq(&sh_dmae_lock); | 
| Paul Mundt | 03aa18f | 2010-12-17 19:16:10 +0900 | [diff] [blame] | 1356 |  | 
| Guennadi Liakhovetski | 20f2a3b | 2010-02-11 16:50:18 +0000 | [diff] [blame] | 1357 | pm_runtime_put(&pdev->dev); | 
| Guennadi Liakhovetski | 467017b | 2011-04-29 17:09:25 +0000 | [diff] [blame] | 1358 | pm_runtime_disable(&pdev->dev); | 
|  | 1359 |  | 
| Guennadi Liakhovetski | 027811b | 2010-02-11 16:50:10 +0000 | [diff] [blame] | 1360 | if (dmars) | 
|  | 1361 | iounmap(shdev->dmars); | 
| Paul Mundt | 5c2de44 | 2011-05-31 15:53:03 +0900 | [diff] [blame] | 1362 |  | 
|  | 1363 | platform_set_drvdata(pdev, NULL); | 
| Guennadi Liakhovetski | 027811b | 2010-02-11 16:50:10 +0000 | [diff] [blame] | 1364 | emapdmars: | 
|  | 1365 | iounmap(shdev->chan_reg); | 
| Guennadi Liakhovetski | 31705e2 | 2011-05-02 07:59:02 +0000 | [diff] [blame] | 1366 | synchronize_rcu(); | 
| Guennadi Liakhovetski | 027811b | 2010-02-11 16:50:10 +0000 | [diff] [blame] | 1367 | emapchan: | 
| Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 1368 | kfree(shdev); | 
| Guennadi Liakhovetski | 027811b | 2010-02-11 16:50:10 +0000 | [diff] [blame] | 1369 | ealloc: | 
|  | 1370 | if (dmars) | 
|  | 1371 | release_mem_region(dmars->start, resource_size(dmars)); | 
|  | 1372 | ermrdmars: | 
|  | 1373 | release_mem_region(chan->start, resource_size(chan)); | 
| Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 1374 |  | 
| Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 1375 | return err; | 
|  | 1376 | } | 
|  | 1377 |  | 
|  | 1378 | static int __exit sh_dmae_remove(struct platform_device *pdev) | 
|  | 1379 | { | 
|  | 1380 | struct sh_dmae_device *shdev = platform_get_drvdata(pdev); | 
| Guennadi Liakhovetski | 027811b | 2010-02-11 16:50:10 +0000 | [diff] [blame] | 1381 | struct resource *res; | 
|  | 1382 | int errirq = platform_get_irq(pdev, 0); | 
| Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 1383 |  | 
|  | 1384 | dma_async_device_unregister(&shdev->common); | 
|  | 1385 |  | 
| Guennadi Liakhovetski | 027811b | 2010-02-11 16:50:10 +0000 | [diff] [blame] | 1386 | if (errirq > 0) | 
|  | 1387 | free_irq(errirq, shdev); | 
| Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 1388 |  | 
| Guennadi Liakhovetski | 31705e2 | 2011-05-02 07:59:02 +0000 | [diff] [blame] | 1389 | spin_lock_irq(&sh_dmae_lock); | 
| Paul Mundt | 03aa18f | 2010-12-17 19:16:10 +0900 | [diff] [blame] | 1390 | list_del_rcu(&shdev->node); | 
| Guennadi Liakhovetski | 31705e2 | 2011-05-02 07:59:02 +0000 | [diff] [blame] | 1391 | spin_unlock_irq(&sh_dmae_lock); | 
| Paul Mundt | 03aa18f | 2010-12-17 19:16:10 +0900 | [diff] [blame] | 1392 |  | 
| Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 1393 | /* channel data remove */ | 
|  | 1394 | sh_dmae_chan_remove(shdev); | 
|  | 1395 |  | 
| Guennadi Liakhovetski | 20f2a3b | 2010-02-11 16:50:18 +0000 | [diff] [blame] | 1396 | pm_runtime_disable(&pdev->dev); | 
|  | 1397 |  | 
| Guennadi Liakhovetski | 027811b | 2010-02-11 16:50:10 +0000 | [diff] [blame] | 1398 | if (shdev->dmars) | 
|  | 1399 | iounmap(shdev->dmars); | 
|  | 1400 | iounmap(shdev->chan_reg); | 
|  | 1401 |  | 
| Paul Mundt | 5c2de44 | 2011-05-31 15:53:03 +0900 | [diff] [blame] | 1402 | platform_set_drvdata(pdev, NULL); | 
|  | 1403 |  | 
| Guennadi Liakhovetski | 31705e2 | 2011-05-02 07:59:02 +0000 | [diff] [blame] | 1404 | synchronize_rcu(); | 
| Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 1405 | kfree(shdev); | 
|  | 1406 |  | 
| Guennadi Liakhovetski | 027811b | 2010-02-11 16:50:10 +0000 | [diff] [blame] | 1407 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); | 
|  | 1408 | if (res) | 
|  | 1409 | release_mem_region(res->start, resource_size(res)); | 
|  | 1410 | res = platform_get_resource(pdev, IORESOURCE_MEM, 1); | 
|  | 1411 | if (res) | 
|  | 1412 | release_mem_region(res->start, resource_size(res)); | 
|  | 1413 |  | 
| Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 1414 | return 0; | 
|  | 1415 | } | 
|  | 1416 |  | 
|  | 1417 | static void sh_dmae_shutdown(struct platform_device *pdev) | 
|  | 1418 | { | 
|  | 1419 | struct sh_dmae_device *shdev = platform_get_drvdata(pdev); | 
| Guennadi Liakhovetski | 027811b | 2010-02-11 16:50:10 +0000 | [diff] [blame] | 1420 | sh_dmae_ctl_stop(shdev); | 
| Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 1421 | } | 
|  | 1422 |  | 
| Guennadi Liakhovetski | 467017b | 2011-04-29 17:09:25 +0000 | [diff] [blame] | 1423 | static int sh_dmae_runtime_suspend(struct device *dev) | 
|  | 1424 | { | 
|  | 1425 | return 0; | 
|  | 1426 | } | 
|  | 1427 |  | 
|  | 1428 | static int sh_dmae_runtime_resume(struct device *dev) | 
|  | 1429 | { | 
|  | 1430 | struct sh_dmae_device *shdev = dev_get_drvdata(dev); | 
|  | 1431 |  | 
|  | 1432 | return sh_dmae_rst(shdev); | 
|  | 1433 | } | 
|  | 1434 |  | 
|  | 1435 | #ifdef CONFIG_PM | 
|  | 1436 | static int sh_dmae_suspend(struct device *dev) | 
|  | 1437 | { | 
|  | 1438 | struct sh_dmae_device *shdev = dev_get_drvdata(dev); | 
|  | 1439 | int i; | 
|  | 1440 |  | 
|  | 1441 | for (i = 0; i < shdev->pdata->channel_num; i++) { | 
|  | 1442 | struct sh_dmae_chan *sh_chan = shdev->chan[i]; | 
|  | 1443 | if (sh_chan->descs_allocated) | 
|  | 1444 | sh_chan->pm_error = pm_runtime_put_sync(dev); | 
|  | 1445 | } | 
|  | 1446 |  | 
|  | 1447 | return 0; | 
|  | 1448 | } | 
|  | 1449 |  | 
|  | 1450 | static int sh_dmae_resume(struct device *dev) | 
|  | 1451 | { | 
|  | 1452 | struct sh_dmae_device *shdev = dev_get_drvdata(dev); | 
|  | 1453 | int i; | 
|  | 1454 |  | 
|  | 1455 | for (i = 0; i < shdev->pdata->channel_num; i++) { | 
|  | 1456 | struct sh_dmae_chan *sh_chan = shdev->chan[i]; | 
|  | 1457 | struct sh_dmae_slave *param = sh_chan->common.private; | 
|  | 1458 |  | 
|  | 1459 | if (!sh_chan->descs_allocated) | 
|  | 1460 | continue; | 
|  | 1461 |  | 
|  | 1462 | if (!sh_chan->pm_error) | 
|  | 1463 | pm_runtime_get_sync(dev); | 
|  | 1464 |  | 
|  | 1465 | if (param) { | 
|  | 1466 | const struct sh_dmae_slave_config *cfg = param->config; | 
|  | 1467 | dmae_set_dmars(sh_chan, cfg->mid_rid); | 
|  | 1468 | dmae_set_chcr(sh_chan, cfg->chcr); | 
|  | 1469 | } else { | 
|  | 1470 | dmae_init(sh_chan); | 
|  | 1471 | } | 
|  | 1472 | } | 
|  | 1473 |  | 
|  | 1474 | return 0; | 
|  | 1475 | } | 
|  | 1476 | #else | 
|  | 1477 | #define sh_dmae_suspend NULL | 
|  | 1478 | #define sh_dmae_resume NULL | 
|  | 1479 | #endif | 
|  | 1480 |  | 
|  | 1481 | const struct dev_pm_ops sh_dmae_pm = { | 
|  | 1482 | .suspend		= sh_dmae_suspend, | 
|  | 1483 | .resume			= sh_dmae_resume, | 
|  | 1484 | .runtime_suspend	= sh_dmae_runtime_suspend, | 
|  | 1485 | .runtime_resume		= sh_dmae_runtime_resume, | 
|  | 1486 | }; | 
|  | 1487 |  | 
| Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 1488 | static struct platform_driver sh_dmae_driver = { | 
|  | 1489 | .remove		= __exit_p(sh_dmae_remove), | 
|  | 1490 | .shutdown	= sh_dmae_shutdown, | 
|  | 1491 | .driver = { | 
| Guennadi Liakhovetski | 7a5c106 | 2010-05-21 15:28:51 +0000 | [diff] [blame] | 1492 | .owner	= THIS_MODULE, | 
| Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 1493 | .name	= "sh-dma-engine", | 
| Guennadi Liakhovetski | 467017b | 2011-04-29 17:09:25 +0000 | [diff] [blame] | 1494 | .pm	= &sh_dmae_pm, | 
| Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 1495 | }, | 
|  | 1496 | }; | 
|  | 1497 |  | 
|  | 1498 | static int __init sh_dmae_init(void) | 
|  | 1499 | { | 
| Guennadi Liakhovetski | 661382f | 2011-01-06 17:04:50 +0000 | [diff] [blame] | 1500 | /* Wire up NMI handling */ | 
|  | 1501 | int err = register_die_notifier(&sh_dmae_nmi_notifier); | 
|  | 1502 | if (err) | 
|  | 1503 | return err; | 
|  | 1504 |  | 
| Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 1505 | return platform_driver_probe(&sh_dmae_driver, sh_dmae_probe); | 
|  | 1506 | } | 
|  | 1507 | module_init(sh_dmae_init); | 
|  | 1508 |  | 
|  | 1509 | static void __exit sh_dmae_exit(void) | 
|  | 1510 | { | 
|  | 1511 | platform_driver_unregister(&sh_dmae_driver); | 
| Guennadi Liakhovetski | 661382f | 2011-01-06 17:04:50 +0000 | [diff] [blame] | 1512 |  | 
|  | 1513 | unregister_die_notifier(&sh_dmae_nmi_notifier); | 
| Nobuhiro Iwamatsu | d8902ad | 2009-09-07 03:26:23 +0000 | [diff] [blame] | 1514 | } | 
|  | 1515 | module_exit(sh_dmae_exit); | 
|  | 1516 |  | 
|  | 1517 | MODULE_AUTHOR("Nobuhiro Iwamatsu <iwamatsu.nobuhiro@renesas.com>"); | 
|  | 1518 | MODULE_DESCRIPTION("Renesas SH DMA Engine driver"); | 
|  | 1519 | MODULE_LICENSE("GPL"); | 
| Guennadi Liakhovetski | e584334 | 2010-11-24 09:48:10 +0000 | [diff] [blame] | 1520 | MODULE_ALIAS("platform:sh-dma-engine"); |