Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Driver for the Synopsys DesignWare DMA Controller (aka DMACA on |
| 3 | * AVR32 systems.) |
| 4 | * |
| 5 | * Copyright (C) 2007-2008 Atmel Corporation |
Viresh Kumar | aecb7b6 | 2011-05-24 14:04:09 +0530 | [diff] [blame] | 6 | * Copyright (C) 2010-2011 ST Microelectronics |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License version 2 as |
| 10 | * published by the Free Software Foundation. |
| 11 | */ |
Viresh Kumar | 327e697 | 2012-02-01 16:12:26 +0530 | [diff] [blame] | 12 | #include <linux/bitops.h> |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 13 | #include <linux/clk.h> |
| 14 | #include <linux/delay.h> |
| 15 | #include <linux/dmaengine.h> |
| 16 | #include <linux/dma-mapping.h> |
| 17 | #include <linux/init.h> |
| 18 | #include <linux/interrupt.h> |
| 19 | #include <linux/io.h> |
Viresh Kumar | d3f797d | 2012-04-20 20:15:34 +0530 | [diff] [blame] | 20 | #include <linux/of.h> |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 21 | #include <linux/mm.h> |
| 22 | #include <linux/module.h> |
| 23 | #include <linux/platform_device.h> |
| 24 | #include <linux/slab.h> |
| 25 | |
| 26 | #include "dw_dmac_regs.h" |
Russell King - ARM Linux | d2ebfb3 | 2012-03-06 22:34:26 +0000 | [diff] [blame] | 27 | #include "dmaengine.h" |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 28 | |
| 29 | /* |
| 30 | * This supports the Synopsys "DesignWare AHB Central DMA Controller", |
| 31 | * (DW_ahb_dmac) which is used with various AMBA 2.0 systems (not all |
| 32 | * of which use ARM any more). See the "Databook" from Synopsys for |
| 33 | * information beyond what licensees probably provide. |
| 34 | * |
| 35 | * The driver has currently been tested only with the Atmel AT32AP7000, |
| 36 | * which does not support descriptor writeback. |
| 37 | */ |
| 38 | |
Viresh Kumar | 327e697 | 2012-02-01 16:12:26 +0530 | [diff] [blame] | 39 | #define DWC_DEFAULT_CTLLO(_chan) ({ \ |
| 40 | struct dw_dma_slave *__slave = (_chan->private); \ |
| 41 | struct dw_dma_chan *_dwc = to_dw_dma_chan(_chan); \ |
| 42 | struct dma_slave_config *_sconfig = &_dwc->dma_sconfig; \ |
| 43 | int _dms = __slave ? __slave->dst_master : 0; \ |
| 44 | int _sms = __slave ? __slave->src_master : 1; \ |
| 45 | u8 _smsize = __slave ? _sconfig->src_maxburst : \ |
| 46 | DW_DMA_MSIZE_16; \ |
| 47 | u8 _dmsize = __slave ? _sconfig->dst_maxburst : \ |
| 48 | DW_DMA_MSIZE_16; \ |
Jamie Iles | f301c06 | 2011-01-21 14:11:53 +0000 | [diff] [blame] | 49 | \ |
Viresh Kumar | 327e697 | 2012-02-01 16:12:26 +0530 | [diff] [blame] | 50 | (DWC_CTLL_DST_MSIZE(_dmsize) \ |
| 51 | | DWC_CTLL_SRC_MSIZE(_smsize) \ |
Jamie Iles | f301c06 | 2011-01-21 14:11:53 +0000 | [diff] [blame] | 52 | | DWC_CTLL_LLP_D_EN \ |
| 53 | | DWC_CTLL_LLP_S_EN \ |
Viresh Kumar | 327e697 | 2012-02-01 16:12:26 +0530 | [diff] [blame] | 54 | | DWC_CTLL_DMS(_dms) \ |
| 55 | | DWC_CTLL_SMS(_sms)); \ |
Jamie Iles | f301c06 | 2011-01-21 14:11:53 +0000 | [diff] [blame] | 56 | }) |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 57 | |
| 58 | /* |
| 59 | * This is configuration-dependent and usually a funny size like 4095. |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 60 | * |
| 61 | * Note that this is a transfer count, i.e. if we transfer 32-bit |
Viresh Kumar | 418e740 | 2011-03-04 15:42:50 +0530 | [diff] [blame] | 62 | * words, we can do 16380 bytes per descriptor. |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 63 | * |
| 64 | * This parameter is also system-specific. |
| 65 | */ |
Viresh Kumar | 418e740 | 2011-03-04 15:42:50 +0530 | [diff] [blame] | 66 | #define DWC_MAX_COUNT 4095U |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 67 | |
| 68 | /* |
| 69 | * Number of descriptors to allocate for each channel. This should be |
| 70 | * made configurable somehow; preferably, the clients (at least the |
| 71 | * ones using slave transfers) should be able to give us a hint. |
| 72 | */ |
| 73 | #define NR_DESCS_PER_CHANNEL 64 |
| 74 | |
| 75 | /*----------------------------------------------------------------------*/ |
| 76 | |
| 77 | /* |
| 78 | * Because we're not relying on writeback from the controller (it may not |
| 79 | * even be configured into the core!) we don't need to use dma_pool. These |
| 80 | * descriptors -- and associated data -- are cacheable. We do need to make |
| 81 | * sure their dcache entries are written back before handing them off to |
| 82 | * the controller, though. |
| 83 | */ |
| 84 | |
Dan Williams | 41d5e59 | 2009-01-06 11:38:21 -0700 | [diff] [blame] | 85 | static struct device *chan2dev(struct dma_chan *chan) |
| 86 | { |
| 87 | return &chan->dev->device; |
| 88 | } |
| 89 | static struct device *chan2parent(struct dma_chan *chan) |
| 90 | { |
| 91 | return chan->dev->device.parent; |
| 92 | } |
| 93 | |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 94 | static struct dw_desc *dwc_first_active(struct dw_dma_chan *dwc) |
| 95 | { |
| 96 | return list_entry(dwc->active_list.next, struct dw_desc, desc_node); |
| 97 | } |
| 98 | |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 99 | static struct dw_desc *dwc_desc_get(struct dw_dma_chan *dwc) |
| 100 | { |
| 101 | struct dw_desc *desc, *_desc; |
| 102 | struct dw_desc *ret = NULL; |
| 103 | unsigned int i = 0; |
Viresh Kumar | 69cea5a | 2011-04-15 16:03:35 +0530 | [diff] [blame] | 104 | unsigned long flags; |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 105 | |
Viresh Kumar | 69cea5a | 2011-04-15 16:03:35 +0530 | [diff] [blame] | 106 | spin_lock_irqsave(&dwc->lock, flags); |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 107 | list_for_each_entry_safe(desc, _desc, &dwc->free_list, desc_node) { |
Andy Shevchenko | 2ab3727 | 2012-06-19 13:34:04 +0300 | [diff] [blame] | 108 | i++; |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 109 | if (async_tx_test_ack(&desc->txd)) { |
| 110 | list_del(&desc->desc_node); |
| 111 | ret = desc; |
| 112 | break; |
| 113 | } |
Dan Williams | 41d5e59 | 2009-01-06 11:38:21 -0700 | [diff] [blame] | 114 | dev_dbg(chan2dev(&dwc->chan), "desc %p not ACKed\n", desc); |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 115 | } |
Viresh Kumar | 69cea5a | 2011-04-15 16:03:35 +0530 | [diff] [blame] | 116 | spin_unlock_irqrestore(&dwc->lock, flags); |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 117 | |
Dan Williams | 41d5e59 | 2009-01-06 11:38:21 -0700 | [diff] [blame] | 118 | dev_vdbg(chan2dev(&dwc->chan), "scanned %u descriptors on freelist\n", i); |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 119 | |
| 120 | return ret; |
| 121 | } |
| 122 | |
| 123 | static void dwc_sync_desc_for_cpu(struct dw_dma_chan *dwc, struct dw_desc *desc) |
| 124 | { |
| 125 | struct dw_desc *child; |
| 126 | |
Dan Williams | e0bd0f8 | 2009-09-08 17:53:02 -0700 | [diff] [blame] | 127 | list_for_each_entry(child, &desc->tx_list, desc_node) |
Dan Williams | 41d5e59 | 2009-01-06 11:38:21 -0700 | [diff] [blame] | 128 | dma_sync_single_for_cpu(chan2parent(&dwc->chan), |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 129 | child->txd.phys, sizeof(child->lli), |
| 130 | DMA_TO_DEVICE); |
Dan Williams | 41d5e59 | 2009-01-06 11:38:21 -0700 | [diff] [blame] | 131 | dma_sync_single_for_cpu(chan2parent(&dwc->chan), |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 132 | desc->txd.phys, sizeof(desc->lli), |
| 133 | DMA_TO_DEVICE); |
| 134 | } |
| 135 | |
| 136 | /* |
| 137 | * Move a descriptor, including any children, to the free list. |
| 138 | * `desc' must not be on any lists. |
| 139 | */ |
| 140 | static void dwc_desc_put(struct dw_dma_chan *dwc, struct dw_desc *desc) |
| 141 | { |
Viresh Kumar | 69cea5a | 2011-04-15 16:03:35 +0530 | [diff] [blame] | 142 | unsigned long flags; |
| 143 | |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 144 | if (desc) { |
| 145 | struct dw_desc *child; |
| 146 | |
| 147 | dwc_sync_desc_for_cpu(dwc, desc); |
| 148 | |
Viresh Kumar | 69cea5a | 2011-04-15 16:03:35 +0530 | [diff] [blame] | 149 | spin_lock_irqsave(&dwc->lock, flags); |
Dan Williams | e0bd0f8 | 2009-09-08 17:53:02 -0700 | [diff] [blame] | 150 | list_for_each_entry(child, &desc->tx_list, desc_node) |
Dan Williams | 41d5e59 | 2009-01-06 11:38:21 -0700 | [diff] [blame] | 151 | dev_vdbg(chan2dev(&dwc->chan), |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 152 | "moving child desc %p to freelist\n", |
| 153 | child); |
Dan Williams | e0bd0f8 | 2009-09-08 17:53:02 -0700 | [diff] [blame] | 154 | list_splice_init(&desc->tx_list, &dwc->free_list); |
Dan Williams | 41d5e59 | 2009-01-06 11:38:21 -0700 | [diff] [blame] | 155 | dev_vdbg(chan2dev(&dwc->chan), "moving desc %p to freelist\n", desc); |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 156 | list_add(&desc->desc_node, &dwc->free_list); |
Viresh Kumar | 69cea5a | 2011-04-15 16:03:35 +0530 | [diff] [blame] | 157 | spin_unlock_irqrestore(&dwc->lock, flags); |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 158 | } |
| 159 | } |
| 160 | |
Viresh Kumar | 61e183f | 2011-11-17 16:01:29 +0530 | [diff] [blame] | 161 | static void dwc_initialize(struct dw_dma_chan *dwc) |
| 162 | { |
| 163 | struct dw_dma *dw = to_dw_dma(dwc->chan.device); |
| 164 | struct dw_dma_slave *dws = dwc->chan.private; |
| 165 | u32 cfghi = DWC_CFGH_FIFO_MODE; |
| 166 | u32 cfglo = DWC_CFGL_CH_PRIOR(dwc->priority); |
| 167 | |
| 168 | if (dwc->initialized == true) |
| 169 | return; |
| 170 | |
| 171 | if (dws) { |
| 172 | /* |
| 173 | * We need controller-specific data to set up slave |
| 174 | * transfers. |
| 175 | */ |
| 176 | BUG_ON(!dws->dma_dev || dws->dma_dev != dw->dma.dev); |
| 177 | |
| 178 | cfghi = dws->cfg_hi; |
| 179 | cfglo |= dws->cfg_lo & ~DWC_CFGL_CH_PRIOR_MASK; |
| 180 | } |
| 181 | |
| 182 | channel_writel(dwc, CFG_LO, cfglo); |
| 183 | channel_writel(dwc, CFG_HI, cfghi); |
| 184 | |
| 185 | /* Enable interrupts */ |
| 186 | channel_set_bit(dw, MASK.XFER, dwc->mask); |
Viresh Kumar | 61e183f | 2011-11-17 16:01:29 +0530 | [diff] [blame] | 187 | channel_set_bit(dw, MASK.ERROR, dwc->mask); |
| 188 | |
| 189 | dwc->initialized = true; |
| 190 | } |
| 191 | |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 192 | /*----------------------------------------------------------------------*/ |
| 193 | |
Andy Shevchenko | 1d45543 | 2012-06-19 13:34:03 +0300 | [diff] [blame] | 194 | static void dwc_dump_chan_regs(struct dw_dma_chan *dwc) |
| 195 | { |
| 196 | dev_err(chan2dev(&dwc->chan), |
| 197 | " SAR: 0x%x DAR: 0x%x LLP: 0x%x CTL: 0x%x:%08x\n", |
| 198 | channel_readl(dwc, SAR), |
| 199 | channel_readl(dwc, DAR), |
| 200 | channel_readl(dwc, LLP), |
| 201 | channel_readl(dwc, CTL_HI), |
| 202 | channel_readl(dwc, CTL_LO)); |
| 203 | } |
| 204 | |
| 205 | /*----------------------------------------------------------------------*/ |
| 206 | |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 207 | /* Called with dwc->lock held and bh disabled */ |
| 208 | static void dwc_dostart(struct dw_dma_chan *dwc, struct dw_desc *first) |
| 209 | { |
| 210 | struct dw_dma *dw = to_dw_dma(dwc->chan.device); |
| 211 | |
| 212 | /* ASSERT: channel is idle */ |
| 213 | if (dma_readl(dw, CH_EN) & dwc->mask) { |
Dan Williams | 41d5e59 | 2009-01-06 11:38:21 -0700 | [diff] [blame] | 214 | dev_err(chan2dev(&dwc->chan), |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 215 | "BUG: Attempted to start non-idle channel\n"); |
Andy Shevchenko | 1d45543 | 2012-06-19 13:34:03 +0300 | [diff] [blame] | 216 | dwc_dump_chan_regs(dwc); |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 217 | |
| 218 | /* The tasklet will hopefully advance the queue... */ |
| 219 | return; |
| 220 | } |
| 221 | |
Viresh Kumar | 61e183f | 2011-11-17 16:01:29 +0530 | [diff] [blame] | 222 | dwc_initialize(dwc); |
| 223 | |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 224 | channel_writel(dwc, LLP, first->txd.phys); |
| 225 | channel_writel(dwc, CTL_LO, |
| 226 | DWC_CTLL_LLP_D_EN | DWC_CTLL_LLP_S_EN); |
| 227 | channel_writel(dwc, CTL_HI, 0); |
| 228 | channel_set_bit(dw, CH_EN, dwc->mask); |
| 229 | } |
| 230 | |
| 231 | /*----------------------------------------------------------------------*/ |
| 232 | |
| 233 | static void |
Viresh Kumar | 5fedefb | 2011-04-15 16:03:35 +0530 | [diff] [blame] | 234 | dwc_descriptor_complete(struct dw_dma_chan *dwc, struct dw_desc *desc, |
| 235 | bool callback_required) |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 236 | { |
Viresh Kumar | 5fedefb | 2011-04-15 16:03:35 +0530 | [diff] [blame] | 237 | dma_async_tx_callback callback = NULL; |
| 238 | void *param = NULL; |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 239 | struct dma_async_tx_descriptor *txd = &desc->txd; |
Viresh Kumar | e518076 | 2011-03-03 15:47:20 +0530 | [diff] [blame] | 240 | struct dw_desc *child; |
Viresh Kumar | 69cea5a | 2011-04-15 16:03:35 +0530 | [diff] [blame] | 241 | unsigned long flags; |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 242 | |
Dan Williams | 41d5e59 | 2009-01-06 11:38:21 -0700 | [diff] [blame] | 243 | dev_vdbg(chan2dev(&dwc->chan), "descriptor %u complete\n", txd->cookie); |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 244 | |
Viresh Kumar | 69cea5a | 2011-04-15 16:03:35 +0530 | [diff] [blame] | 245 | spin_lock_irqsave(&dwc->lock, flags); |
Russell King - ARM Linux | f7fbce0 | 2012-03-06 22:35:07 +0000 | [diff] [blame] | 246 | dma_cookie_complete(txd); |
Viresh Kumar | 5fedefb | 2011-04-15 16:03:35 +0530 | [diff] [blame] | 247 | if (callback_required) { |
| 248 | callback = txd->callback; |
| 249 | param = txd->callback_param; |
| 250 | } |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 251 | |
| 252 | dwc_sync_desc_for_cpu(dwc, desc); |
Viresh Kumar | e518076 | 2011-03-03 15:47:20 +0530 | [diff] [blame] | 253 | |
| 254 | /* async_tx_ack */ |
| 255 | list_for_each_entry(child, &desc->tx_list, desc_node) |
| 256 | async_tx_ack(&child->txd); |
| 257 | async_tx_ack(&desc->txd); |
| 258 | |
Dan Williams | e0bd0f8 | 2009-09-08 17:53:02 -0700 | [diff] [blame] | 259 | list_splice_init(&desc->tx_list, &dwc->free_list); |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 260 | list_move(&desc->desc_node, &dwc->free_list); |
| 261 | |
Atsushi Nemoto | 657a77f | 2009-09-08 17:53:05 -0700 | [diff] [blame] | 262 | if (!dwc->chan.private) { |
| 263 | struct device *parent = chan2parent(&dwc->chan); |
| 264 | if (!(txd->flags & DMA_COMPL_SKIP_DEST_UNMAP)) { |
| 265 | if (txd->flags & DMA_COMPL_DEST_UNMAP_SINGLE) |
| 266 | dma_unmap_single(parent, desc->lli.dar, |
| 267 | desc->len, DMA_FROM_DEVICE); |
| 268 | else |
| 269 | dma_unmap_page(parent, desc->lli.dar, |
| 270 | desc->len, DMA_FROM_DEVICE); |
| 271 | } |
| 272 | if (!(txd->flags & DMA_COMPL_SKIP_SRC_UNMAP)) { |
| 273 | if (txd->flags & DMA_COMPL_SRC_UNMAP_SINGLE) |
| 274 | dma_unmap_single(parent, desc->lli.sar, |
| 275 | desc->len, DMA_TO_DEVICE); |
| 276 | else |
| 277 | dma_unmap_page(parent, desc->lli.sar, |
| 278 | desc->len, DMA_TO_DEVICE); |
| 279 | } |
| 280 | } |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 281 | |
Viresh Kumar | 69cea5a | 2011-04-15 16:03:35 +0530 | [diff] [blame] | 282 | spin_unlock_irqrestore(&dwc->lock, flags); |
| 283 | |
Viresh Kumar | 5fedefb | 2011-04-15 16:03:35 +0530 | [diff] [blame] | 284 | if (callback_required && callback) |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 285 | callback(param); |
| 286 | } |
| 287 | |
| 288 | static void dwc_complete_all(struct dw_dma *dw, struct dw_dma_chan *dwc) |
| 289 | { |
| 290 | struct dw_desc *desc, *_desc; |
| 291 | LIST_HEAD(list); |
Viresh Kumar | 69cea5a | 2011-04-15 16:03:35 +0530 | [diff] [blame] | 292 | unsigned long flags; |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 293 | |
Viresh Kumar | 69cea5a | 2011-04-15 16:03:35 +0530 | [diff] [blame] | 294 | spin_lock_irqsave(&dwc->lock, flags); |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 295 | if (dma_readl(dw, CH_EN) & dwc->mask) { |
Dan Williams | 41d5e59 | 2009-01-06 11:38:21 -0700 | [diff] [blame] | 296 | dev_err(chan2dev(&dwc->chan), |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 297 | "BUG: XFER bit set, but channel not idle!\n"); |
| 298 | |
| 299 | /* Try to continue after resetting the channel... */ |
| 300 | channel_clear_bit(dw, CH_EN, dwc->mask); |
| 301 | while (dma_readl(dw, CH_EN) & dwc->mask) |
| 302 | cpu_relax(); |
| 303 | } |
| 304 | |
| 305 | /* |
| 306 | * Submit queued descriptors ASAP, i.e. before we go through |
| 307 | * the completed ones. |
| 308 | */ |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 309 | list_splice_init(&dwc->active_list, &list); |
Viresh Kumar | f336e42 | 2011-03-03 15:47:16 +0530 | [diff] [blame] | 310 | if (!list_empty(&dwc->queue)) { |
| 311 | list_move(dwc->queue.next, &dwc->active_list); |
| 312 | dwc_dostart(dwc, dwc_first_active(dwc)); |
| 313 | } |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 314 | |
Viresh Kumar | 69cea5a | 2011-04-15 16:03:35 +0530 | [diff] [blame] | 315 | spin_unlock_irqrestore(&dwc->lock, flags); |
| 316 | |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 317 | list_for_each_entry_safe(desc, _desc, &list, desc_node) |
Viresh Kumar | 5fedefb | 2011-04-15 16:03:35 +0530 | [diff] [blame] | 318 | dwc_descriptor_complete(dwc, desc, true); |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | static void dwc_scan_descriptors(struct dw_dma *dw, struct dw_dma_chan *dwc) |
| 322 | { |
| 323 | dma_addr_t llp; |
| 324 | struct dw_desc *desc, *_desc; |
| 325 | struct dw_desc *child; |
| 326 | u32 status_xfer; |
Viresh Kumar | 69cea5a | 2011-04-15 16:03:35 +0530 | [diff] [blame] | 327 | unsigned long flags; |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 328 | |
Viresh Kumar | 69cea5a | 2011-04-15 16:03:35 +0530 | [diff] [blame] | 329 | spin_lock_irqsave(&dwc->lock, flags); |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 330 | llp = channel_readl(dwc, LLP); |
| 331 | status_xfer = dma_readl(dw, RAW.XFER); |
| 332 | |
| 333 | if (status_xfer & dwc->mask) { |
| 334 | /* Everything we've submitted is done */ |
| 335 | dma_writel(dw, CLEAR.XFER, dwc->mask); |
Viresh Kumar | 69cea5a | 2011-04-15 16:03:35 +0530 | [diff] [blame] | 336 | spin_unlock_irqrestore(&dwc->lock, flags); |
| 337 | |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 338 | dwc_complete_all(dw, dwc); |
| 339 | return; |
| 340 | } |
| 341 | |
Viresh Kumar | 69cea5a | 2011-04-15 16:03:35 +0530 | [diff] [blame] | 342 | if (list_empty(&dwc->active_list)) { |
| 343 | spin_unlock_irqrestore(&dwc->lock, flags); |
Jamie Iles | 087809f | 2011-01-21 14:11:52 +0000 | [diff] [blame] | 344 | return; |
Viresh Kumar | 69cea5a | 2011-04-15 16:03:35 +0530 | [diff] [blame] | 345 | } |
Jamie Iles | 087809f | 2011-01-21 14:11:52 +0000 | [diff] [blame] | 346 | |
Andy Shevchenko | 2e4c364 | 2012-06-19 13:34:05 +0300 | [diff] [blame^] | 347 | dev_vdbg(chan2dev(&dwc->chan), "%s: llp=0x%llx\n", __func__, |
Andy Shevchenko | 2f45d61 | 2012-06-19 13:34:02 +0300 | [diff] [blame] | 348 | (unsigned long long)llp); |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 349 | |
| 350 | list_for_each_entry_safe(desc, _desc, &dwc->active_list, desc_node) { |
Viresh Kumar | 84adccf | 2011-03-24 11:32:15 +0530 | [diff] [blame] | 351 | /* check first descriptors addr */ |
Viresh Kumar | 69cea5a | 2011-04-15 16:03:35 +0530 | [diff] [blame] | 352 | if (desc->txd.phys == llp) { |
| 353 | spin_unlock_irqrestore(&dwc->lock, flags); |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 354 | return; |
Viresh Kumar | 69cea5a | 2011-04-15 16:03:35 +0530 | [diff] [blame] | 355 | } |
Viresh Kumar | 84adccf | 2011-03-24 11:32:15 +0530 | [diff] [blame] | 356 | |
| 357 | /* check first descriptors llp */ |
Viresh Kumar | 69cea5a | 2011-04-15 16:03:35 +0530 | [diff] [blame] | 358 | if (desc->lli.llp == llp) { |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 359 | /* This one is currently in progress */ |
Viresh Kumar | 69cea5a | 2011-04-15 16:03:35 +0530 | [diff] [blame] | 360 | spin_unlock_irqrestore(&dwc->lock, flags); |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 361 | return; |
Viresh Kumar | 69cea5a | 2011-04-15 16:03:35 +0530 | [diff] [blame] | 362 | } |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 363 | |
Dan Williams | e0bd0f8 | 2009-09-08 17:53:02 -0700 | [diff] [blame] | 364 | list_for_each_entry(child, &desc->tx_list, desc_node) |
Viresh Kumar | 69cea5a | 2011-04-15 16:03:35 +0530 | [diff] [blame] | 365 | if (child->lli.llp == llp) { |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 366 | /* Currently in progress */ |
Viresh Kumar | 69cea5a | 2011-04-15 16:03:35 +0530 | [diff] [blame] | 367 | spin_unlock_irqrestore(&dwc->lock, flags); |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 368 | return; |
Viresh Kumar | 69cea5a | 2011-04-15 16:03:35 +0530 | [diff] [blame] | 369 | } |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 370 | |
| 371 | /* |
| 372 | * No descriptors so far seem to be in progress, i.e. |
| 373 | * this one must be done. |
| 374 | */ |
Viresh Kumar | 69cea5a | 2011-04-15 16:03:35 +0530 | [diff] [blame] | 375 | spin_unlock_irqrestore(&dwc->lock, flags); |
Viresh Kumar | 5fedefb | 2011-04-15 16:03:35 +0530 | [diff] [blame] | 376 | dwc_descriptor_complete(dwc, desc, true); |
Viresh Kumar | 69cea5a | 2011-04-15 16:03:35 +0530 | [diff] [blame] | 377 | spin_lock_irqsave(&dwc->lock, flags); |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 378 | } |
| 379 | |
Dan Williams | 41d5e59 | 2009-01-06 11:38:21 -0700 | [diff] [blame] | 380 | dev_err(chan2dev(&dwc->chan), |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 381 | "BUG: All descriptors done, but channel not idle!\n"); |
| 382 | |
| 383 | /* Try to continue after resetting the channel... */ |
| 384 | channel_clear_bit(dw, CH_EN, dwc->mask); |
| 385 | while (dma_readl(dw, CH_EN) & dwc->mask) |
| 386 | cpu_relax(); |
| 387 | |
| 388 | if (!list_empty(&dwc->queue)) { |
Viresh Kumar | f336e42 | 2011-03-03 15:47:16 +0530 | [diff] [blame] | 389 | list_move(dwc->queue.next, &dwc->active_list); |
| 390 | dwc_dostart(dwc, dwc_first_active(dwc)); |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 391 | } |
Viresh Kumar | 69cea5a | 2011-04-15 16:03:35 +0530 | [diff] [blame] | 392 | spin_unlock_irqrestore(&dwc->lock, flags); |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 393 | } |
| 394 | |
| 395 | static void dwc_dump_lli(struct dw_dma_chan *dwc, struct dw_lli *lli) |
| 396 | { |
Dan Williams | 41d5e59 | 2009-01-06 11:38:21 -0700 | [diff] [blame] | 397 | dev_printk(KERN_CRIT, chan2dev(&dwc->chan), |
Andy Shevchenko | 2f45d61 | 2012-06-19 13:34:02 +0300 | [diff] [blame] | 398 | " desc: s0x%llx d0x%llx l0x%llx c0x%x:%x\n", |
| 399 | (unsigned long long)lli->sar, |
| 400 | (unsigned long long)lli->dar, |
| 401 | (unsigned long long)lli->llp, |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 402 | lli->ctlhi, lli->ctllo); |
| 403 | } |
| 404 | |
| 405 | static void dwc_handle_error(struct dw_dma *dw, struct dw_dma_chan *dwc) |
| 406 | { |
| 407 | struct dw_desc *bad_desc; |
| 408 | struct dw_desc *child; |
Viresh Kumar | 69cea5a | 2011-04-15 16:03:35 +0530 | [diff] [blame] | 409 | unsigned long flags; |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 410 | |
| 411 | dwc_scan_descriptors(dw, dwc); |
| 412 | |
Viresh Kumar | 69cea5a | 2011-04-15 16:03:35 +0530 | [diff] [blame] | 413 | spin_lock_irqsave(&dwc->lock, flags); |
| 414 | |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 415 | /* |
| 416 | * The descriptor currently at the head of the active list is |
| 417 | * borked. Since we don't have any way to report errors, we'll |
| 418 | * just have to scream loudly and try to carry on. |
| 419 | */ |
| 420 | bad_desc = dwc_first_active(dwc); |
| 421 | list_del_init(&bad_desc->desc_node); |
Viresh Kumar | f336e42 | 2011-03-03 15:47:16 +0530 | [diff] [blame] | 422 | list_move(dwc->queue.next, dwc->active_list.prev); |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 423 | |
| 424 | /* Clear the error flag and try to restart the controller */ |
| 425 | dma_writel(dw, CLEAR.ERROR, dwc->mask); |
| 426 | if (!list_empty(&dwc->active_list)) |
| 427 | dwc_dostart(dwc, dwc_first_active(dwc)); |
| 428 | |
| 429 | /* |
| 430 | * KERN_CRITICAL may seem harsh, but since this only happens |
| 431 | * when someone submits a bad physical address in a |
| 432 | * descriptor, we should consider ourselves lucky that the |
| 433 | * controller flagged an error instead of scribbling over |
| 434 | * random memory locations. |
| 435 | */ |
Dan Williams | 41d5e59 | 2009-01-06 11:38:21 -0700 | [diff] [blame] | 436 | dev_printk(KERN_CRIT, chan2dev(&dwc->chan), |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 437 | "Bad descriptor submitted for DMA!\n"); |
Dan Williams | 41d5e59 | 2009-01-06 11:38:21 -0700 | [diff] [blame] | 438 | dev_printk(KERN_CRIT, chan2dev(&dwc->chan), |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 439 | " cookie: %d\n", bad_desc->txd.cookie); |
| 440 | dwc_dump_lli(dwc, &bad_desc->lli); |
Dan Williams | e0bd0f8 | 2009-09-08 17:53:02 -0700 | [diff] [blame] | 441 | list_for_each_entry(child, &bad_desc->tx_list, desc_node) |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 442 | dwc_dump_lli(dwc, &child->lli); |
| 443 | |
Viresh Kumar | 69cea5a | 2011-04-15 16:03:35 +0530 | [diff] [blame] | 444 | spin_unlock_irqrestore(&dwc->lock, flags); |
| 445 | |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 446 | /* Pretend the descriptor completed successfully */ |
Viresh Kumar | 5fedefb | 2011-04-15 16:03:35 +0530 | [diff] [blame] | 447 | dwc_descriptor_complete(dwc, bad_desc, true); |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 448 | } |
| 449 | |
Hans-Christian Egtvedt | d9de451 | 2009-04-01 15:47:02 +0200 | [diff] [blame] | 450 | /* --------------------- Cyclic DMA API extensions -------------------- */ |
| 451 | |
| 452 | inline dma_addr_t dw_dma_get_src_addr(struct dma_chan *chan) |
| 453 | { |
| 454 | struct dw_dma_chan *dwc = to_dw_dma_chan(chan); |
| 455 | return channel_readl(dwc, SAR); |
| 456 | } |
| 457 | EXPORT_SYMBOL(dw_dma_get_src_addr); |
| 458 | |
| 459 | inline dma_addr_t dw_dma_get_dst_addr(struct dma_chan *chan) |
| 460 | { |
| 461 | struct dw_dma_chan *dwc = to_dw_dma_chan(chan); |
| 462 | return channel_readl(dwc, DAR); |
| 463 | } |
| 464 | EXPORT_SYMBOL(dw_dma_get_dst_addr); |
| 465 | |
| 466 | /* called with dwc->lock held and all DMAC interrupts disabled */ |
| 467 | static void dwc_handle_cyclic(struct dw_dma *dw, struct dw_dma_chan *dwc, |
Viresh Kumar | ff7b05f | 2012-02-01 16:12:23 +0530 | [diff] [blame] | 468 | u32 status_err, u32 status_xfer) |
Hans-Christian Egtvedt | d9de451 | 2009-04-01 15:47:02 +0200 | [diff] [blame] | 469 | { |
Viresh Kumar | 69cea5a | 2011-04-15 16:03:35 +0530 | [diff] [blame] | 470 | unsigned long flags; |
| 471 | |
Viresh Kumar | ff7b05f | 2012-02-01 16:12:23 +0530 | [diff] [blame] | 472 | if (dwc->mask) { |
Hans-Christian Egtvedt | d9de451 | 2009-04-01 15:47:02 +0200 | [diff] [blame] | 473 | void (*callback)(void *param); |
| 474 | void *callback_param; |
| 475 | |
| 476 | dev_vdbg(chan2dev(&dwc->chan), "new cyclic period llp 0x%08x\n", |
| 477 | channel_readl(dwc, LLP)); |
Hans-Christian Egtvedt | d9de451 | 2009-04-01 15:47:02 +0200 | [diff] [blame] | 478 | |
| 479 | callback = dwc->cdesc->period_callback; |
| 480 | callback_param = dwc->cdesc->period_callback_param; |
Viresh Kumar | 69cea5a | 2011-04-15 16:03:35 +0530 | [diff] [blame] | 481 | |
| 482 | if (callback) |
Hans-Christian Egtvedt | d9de451 | 2009-04-01 15:47:02 +0200 | [diff] [blame] | 483 | callback(callback_param); |
Hans-Christian Egtvedt | d9de451 | 2009-04-01 15:47:02 +0200 | [diff] [blame] | 484 | } |
| 485 | |
| 486 | /* |
| 487 | * Error and transfer complete are highly unlikely, and will most |
| 488 | * likely be due to a configuration error by the user. |
| 489 | */ |
| 490 | if (unlikely(status_err & dwc->mask) || |
| 491 | unlikely(status_xfer & dwc->mask)) { |
| 492 | int i; |
| 493 | |
| 494 | dev_err(chan2dev(&dwc->chan), "cyclic DMA unexpected %s " |
| 495 | "interrupt, stopping DMA transfer\n", |
| 496 | status_xfer ? "xfer" : "error"); |
Viresh Kumar | 69cea5a | 2011-04-15 16:03:35 +0530 | [diff] [blame] | 497 | |
| 498 | spin_lock_irqsave(&dwc->lock, flags); |
| 499 | |
Andy Shevchenko | 1d45543 | 2012-06-19 13:34:03 +0300 | [diff] [blame] | 500 | dwc_dump_chan_regs(dwc); |
Hans-Christian Egtvedt | d9de451 | 2009-04-01 15:47:02 +0200 | [diff] [blame] | 501 | |
| 502 | channel_clear_bit(dw, CH_EN, dwc->mask); |
| 503 | while (dma_readl(dw, CH_EN) & dwc->mask) |
| 504 | cpu_relax(); |
| 505 | |
| 506 | /* make sure DMA does not restart by loading a new list */ |
| 507 | channel_writel(dwc, LLP, 0); |
| 508 | channel_writel(dwc, CTL_LO, 0); |
| 509 | channel_writel(dwc, CTL_HI, 0); |
| 510 | |
Hans-Christian Egtvedt | d9de451 | 2009-04-01 15:47:02 +0200 | [diff] [blame] | 511 | dma_writel(dw, CLEAR.ERROR, dwc->mask); |
| 512 | dma_writel(dw, CLEAR.XFER, dwc->mask); |
| 513 | |
| 514 | for (i = 0; i < dwc->cdesc->periods; i++) |
| 515 | dwc_dump_lli(dwc, &dwc->cdesc->desc[i]->lli); |
Viresh Kumar | 69cea5a | 2011-04-15 16:03:35 +0530 | [diff] [blame] | 516 | |
| 517 | spin_unlock_irqrestore(&dwc->lock, flags); |
Hans-Christian Egtvedt | d9de451 | 2009-04-01 15:47:02 +0200 | [diff] [blame] | 518 | } |
| 519 | } |
| 520 | |
| 521 | /* ------------------------------------------------------------------------- */ |
| 522 | |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 523 | static void dw_dma_tasklet(unsigned long data) |
| 524 | { |
| 525 | struct dw_dma *dw = (struct dw_dma *)data; |
| 526 | struct dw_dma_chan *dwc; |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 527 | u32 status_xfer; |
| 528 | u32 status_err; |
| 529 | int i; |
| 530 | |
Haavard Skinnemoen | 7fe7b2f | 2008-10-03 15:23:46 -0700 | [diff] [blame] | 531 | status_xfer = dma_readl(dw, RAW.XFER); |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 532 | status_err = dma_readl(dw, RAW.ERROR); |
| 533 | |
Andy Shevchenko | 2e4c364 | 2012-06-19 13:34:05 +0300 | [diff] [blame^] | 534 | dev_vdbg(dw->dma.dev, "%s: status_err=%x\n", __func__, status_err); |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 535 | |
| 536 | for (i = 0; i < dw->dma.chancnt; i++) { |
| 537 | dwc = &dw->chan[i]; |
Hans-Christian Egtvedt | d9de451 | 2009-04-01 15:47:02 +0200 | [diff] [blame] | 538 | if (test_bit(DW_DMA_IS_CYCLIC, &dwc->flags)) |
Viresh Kumar | ff7b05f | 2012-02-01 16:12:23 +0530 | [diff] [blame] | 539 | dwc_handle_cyclic(dw, dwc, status_err, status_xfer); |
Hans-Christian Egtvedt | d9de451 | 2009-04-01 15:47:02 +0200 | [diff] [blame] | 540 | else if (status_err & (1 << i)) |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 541 | dwc_handle_error(dw, dwc); |
Viresh Kumar | ff7b05f | 2012-02-01 16:12:23 +0530 | [diff] [blame] | 542 | else if (status_xfer & (1 << i)) |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 543 | dwc_scan_descriptors(dw, dwc); |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 544 | } |
| 545 | |
| 546 | /* |
Viresh Kumar | ff7b05f | 2012-02-01 16:12:23 +0530 | [diff] [blame] | 547 | * Re-enable interrupts. |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 548 | */ |
| 549 | channel_set_bit(dw, MASK.XFER, dw->all_chan_mask); |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 550 | channel_set_bit(dw, MASK.ERROR, dw->all_chan_mask); |
| 551 | } |
| 552 | |
| 553 | static irqreturn_t dw_dma_interrupt(int irq, void *dev_id) |
| 554 | { |
| 555 | struct dw_dma *dw = dev_id; |
| 556 | u32 status; |
| 557 | |
Andy Shevchenko | 2e4c364 | 2012-06-19 13:34:05 +0300 | [diff] [blame^] | 558 | dev_vdbg(dw->dma.dev, "%s: status=0x%x\n", __func__, |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 559 | dma_readl(dw, STATUS_INT)); |
| 560 | |
| 561 | /* |
| 562 | * Just disable the interrupts. We'll turn them back on in the |
| 563 | * softirq handler. |
| 564 | */ |
| 565 | channel_clear_bit(dw, MASK.XFER, dw->all_chan_mask); |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 566 | channel_clear_bit(dw, MASK.ERROR, dw->all_chan_mask); |
| 567 | |
| 568 | status = dma_readl(dw, STATUS_INT); |
| 569 | if (status) { |
| 570 | dev_err(dw->dma.dev, |
| 571 | "BUG: Unexpected interrupts pending: 0x%x\n", |
| 572 | status); |
| 573 | |
| 574 | /* Try to recover */ |
| 575 | channel_clear_bit(dw, MASK.XFER, (1 << 8) - 1); |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 576 | channel_clear_bit(dw, MASK.SRC_TRAN, (1 << 8) - 1); |
| 577 | channel_clear_bit(dw, MASK.DST_TRAN, (1 << 8) - 1); |
| 578 | channel_clear_bit(dw, MASK.ERROR, (1 << 8) - 1); |
| 579 | } |
| 580 | |
| 581 | tasklet_schedule(&dw->tasklet); |
| 582 | |
| 583 | return IRQ_HANDLED; |
| 584 | } |
| 585 | |
| 586 | /*----------------------------------------------------------------------*/ |
| 587 | |
| 588 | static dma_cookie_t dwc_tx_submit(struct dma_async_tx_descriptor *tx) |
| 589 | { |
| 590 | struct dw_desc *desc = txd_to_dw_desc(tx); |
| 591 | struct dw_dma_chan *dwc = to_dw_dma_chan(tx->chan); |
| 592 | dma_cookie_t cookie; |
Viresh Kumar | 69cea5a | 2011-04-15 16:03:35 +0530 | [diff] [blame] | 593 | unsigned long flags; |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 594 | |
Viresh Kumar | 69cea5a | 2011-04-15 16:03:35 +0530 | [diff] [blame] | 595 | spin_lock_irqsave(&dwc->lock, flags); |
Russell King - ARM Linux | 884485e | 2012-03-06 22:34:46 +0000 | [diff] [blame] | 596 | cookie = dma_cookie_assign(tx); |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 597 | |
| 598 | /* |
| 599 | * REVISIT: We should attempt to chain as many descriptors as |
| 600 | * possible, perhaps even appending to those already submitted |
| 601 | * for DMA. But this is hard to do in a race-free manner. |
| 602 | */ |
| 603 | if (list_empty(&dwc->active_list)) { |
Andy Shevchenko | 2e4c364 | 2012-06-19 13:34:05 +0300 | [diff] [blame^] | 604 | dev_vdbg(chan2dev(tx->chan), "%s: started %u\n", __func__, |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 605 | desc->txd.cookie); |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 606 | list_add_tail(&desc->desc_node, &dwc->active_list); |
Viresh Kumar | f336e42 | 2011-03-03 15:47:16 +0530 | [diff] [blame] | 607 | dwc_dostart(dwc, dwc_first_active(dwc)); |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 608 | } else { |
Andy Shevchenko | 2e4c364 | 2012-06-19 13:34:05 +0300 | [diff] [blame^] | 609 | dev_vdbg(chan2dev(tx->chan), "%s: queued %u\n", __func__, |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 610 | desc->txd.cookie); |
| 611 | |
| 612 | list_add_tail(&desc->desc_node, &dwc->queue); |
| 613 | } |
| 614 | |
Viresh Kumar | 69cea5a | 2011-04-15 16:03:35 +0530 | [diff] [blame] | 615 | spin_unlock_irqrestore(&dwc->lock, flags); |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 616 | |
| 617 | return cookie; |
| 618 | } |
| 619 | |
| 620 | static struct dma_async_tx_descriptor * |
| 621 | dwc_prep_dma_memcpy(struct dma_chan *chan, dma_addr_t dest, dma_addr_t src, |
| 622 | size_t len, unsigned long flags) |
| 623 | { |
| 624 | struct dw_dma_chan *dwc = to_dw_dma_chan(chan); |
| 625 | struct dw_desc *desc; |
| 626 | struct dw_desc *first; |
| 627 | struct dw_desc *prev; |
| 628 | size_t xfer_count; |
| 629 | size_t offset; |
| 630 | unsigned int src_width; |
| 631 | unsigned int dst_width; |
| 632 | u32 ctllo; |
| 633 | |
Andy Shevchenko | 2f45d61 | 2012-06-19 13:34:02 +0300 | [diff] [blame] | 634 | dev_vdbg(chan2dev(chan), |
Andy Shevchenko | 2e4c364 | 2012-06-19 13:34:05 +0300 | [diff] [blame^] | 635 | "%s: d0x%llx s0x%llx l0x%zx f0x%lx\n", __func__, |
Andy Shevchenko | 2f45d61 | 2012-06-19 13:34:02 +0300 | [diff] [blame] | 636 | (unsigned long long)dest, (unsigned long long)src, |
| 637 | len, flags); |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 638 | |
| 639 | if (unlikely(!len)) { |
Andy Shevchenko | 2e4c364 | 2012-06-19 13:34:05 +0300 | [diff] [blame^] | 640 | dev_dbg(chan2dev(chan), "%s: length is zero!\n", __func__); |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 641 | return NULL; |
| 642 | } |
| 643 | |
| 644 | /* |
| 645 | * We can be a lot more clever here, but this should take care |
| 646 | * of the most common optimization. |
| 647 | */ |
Viresh Kumar | a022745 | 2011-03-03 15:47:18 +0530 | [diff] [blame] | 648 | if (!((src | dest | len) & 7)) |
| 649 | src_width = dst_width = 3; |
| 650 | else if (!((src | dest | len) & 3)) |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 651 | src_width = dst_width = 2; |
| 652 | else if (!((src | dest | len) & 1)) |
| 653 | src_width = dst_width = 1; |
| 654 | else |
| 655 | src_width = dst_width = 0; |
| 656 | |
Viresh Kumar | 327e697 | 2012-02-01 16:12:26 +0530 | [diff] [blame] | 657 | ctllo = DWC_DEFAULT_CTLLO(chan) |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 658 | | DWC_CTLL_DST_WIDTH(dst_width) |
| 659 | | DWC_CTLL_SRC_WIDTH(src_width) |
| 660 | | DWC_CTLL_DST_INC |
| 661 | | DWC_CTLL_SRC_INC |
| 662 | | DWC_CTLL_FC_M2M; |
| 663 | prev = first = NULL; |
| 664 | |
| 665 | for (offset = 0; offset < len; offset += xfer_count << src_width) { |
| 666 | xfer_count = min_t(size_t, (len - offset) >> src_width, |
| 667 | DWC_MAX_COUNT); |
| 668 | |
| 669 | desc = dwc_desc_get(dwc); |
| 670 | if (!desc) |
| 671 | goto err_desc_get; |
| 672 | |
| 673 | desc->lli.sar = src + offset; |
| 674 | desc->lli.dar = dest + offset; |
| 675 | desc->lli.ctllo = ctllo; |
| 676 | desc->lli.ctlhi = xfer_count; |
| 677 | |
| 678 | if (!first) { |
| 679 | first = desc; |
| 680 | } else { |
| 681 | prev->lli.llp = desc->txd.phys; |
Dan Williams | 41d5e59 | 2009-01-06 11:38:21 -0700 | [diff] [blame] | 682 | dma_sync_single_for_device(chan2parent(chan), |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 683 | prev->txd.phys, sizeof(prev->lli), |
| 684 | DMA_TO_DEVICE); |
| 685 | list_add_tail(&desc->desc_node, |
Dan Williams | e0bd0f8 | 2009-09-08 17:53:02 -0700 | [diff] [blame] | 686 | &first->tx_list); |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 687 | } |
| 688 | prev = desc; |
| 689 | } |
| 690 | |
| 691 | |
| 692 | if (flags & DMA_PREP_INTERRUPT) |
| 693 | /* Trigger interrupt after last block */ |
| 694 | prev->lli.ctllo |= DWC_CTLL_INT_EN; |
| 695 | |
| 696 | prev->lli.llp = 0; |
Dan Williams | 41d5e59 | 2009-01-06 11:38:21 -0700 | [diff] [blame] | 697 | dma_sync_single_for_device(chan2parent(chan), |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 698 | prev->txd.phys, sizeof(prev->lli), |
| 699 | DMA_TO_DEVICE); |
| 700 | |
| 701 | first->txd.flags = flags; |
| 702 | first->len = len; |
| 703 | |
| 704 | return &first->txd; |
| 705 | |
| 706 | err_desc_get: |
| 707 | dwc_desc_put(dwc, first); |
| 708 | return NULL; |
| 709 | } |
| 710 | |
| 711 | static struct dma_async_tx_descriptor * |
| 712 | dwc_prep_slave_sg(struct dma_chan *chan, struct scatterlist *sgl, |
Vinod Koul | db8196d | 2011-10-13 22:34:23 +0530 | [diff] [blame] | 713 | unsigned int sg_len, enum dma_transfer_direction direction, |
Alexandre Bounine | 185ecb5 | 2012-03-08 15:35:13 -0500 | [diff] [blame] | 714 | unsigned long flags, void *context) |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 715 | { |
| 716 | struct dw_dma_chan *dwc = to_dw_dma_chan(chan); |
Dan Williams | 287d859 | 2009-02-18 14:48:26 -0800 | [diff] [blame] | 717 | struct dw_dma_slave *dws = chan->private; |
Viresh Kumar | 327e697 | 2012-02-01 16:12:26 +0530 | [diff] [blame] | 718 | struct dma_slave_config *sconfig = &dwc->dma_sconfig; |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 719 | struct dw_desc *prev; |
| 720 | struct dw_desc *first; |
| 721 | u32 ctllo; |
| 722 | dma_addr_t reg; |
| 723 | unsigned int reg_width; |
| 724 | unsigned int mem_width; |
| 725 | unsigned int i; |
| 726 | struct scatterlist *sg; |
| 727 | size_t total_len = 0; |
| 728 | |
Andy Shevchenko | 2e4c364 | 2012-06-19 13:34:05 +0300 | [diff] [blame^] | 729 | dev_vdbg(chan2dev(chan), "%s\n", __func__); |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 730 | |
| 731 | if (unlikely(!dws || !sg_len)) |
| 732 | return NULL; |
| 733 | |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 734 | prev = first = NULL; |
| 735 | |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 736 | switch (direction) { |
Vinod Koul | db8196d | 2011-10-13 22:34:23 +0530 | [diff] [blame] | 737 | case DMA_MEM_TO_DEV: |
Viresh Kumar | 327e697 | 2012-02-01 16:12:26 +0530 | [diff] [blame] | 738 | reg_width = __fls(sconfig->dst_addr_width); |
| 739 | reg = sconfig->dst_addr; |
| 740 | ctllo = (DWC_DEFAULT_CTLLO(chan) |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 741 | | DWC_CTLL_DST_WIDTH(reg_width) |
| 742 | | DWC_CTLL_DST_FIX |
Viresh Kumar | 327e697 | 2012-02-01 16:12:26 +0530 | [diff] [blame] | 743 | | DWC_CTLL_SRC_INC); |
| 744 | |
| 745 | ctllo |= sconfig->device_fc ? DWC_CTLL_FC(DW_DMA_FC_P_M2P) : |
| 746 | DWC_CTLL_FC(DW_DMA_FC_D_M2P); |
| 747 | |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 748 | for_each_sg(sgl, sg, sg_len, i) { |
| 749 | struct dw_desc *desc; |
Viresh Kumar | 69dc14b | 2011-04-18 14:54:56 +0530 | [diff] [blame] | 750 | u32 len, dlen, mem; |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 751 | |
Lars-Peter Clausen | cbb796c | 2012-04-25 20:50:51 +0200 | [diff] [blame] | 752 | mem = sg_dma_address(sg); |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 753 | len = sg_dma_len(sg); |
Viresh Kumar | 6bc711f | 2012-02-01 16:12:25 +0530 | [diff] [blame] | 754 | |
| 755 | if (!((mem | len) & 7)) |
| 756 | mem_width = 3; |
| 757 | else if (!((mem | len) & 3)) |
| 758 | mem_width = 2; |
| 759 | else if (!((mem | len) & 1)) |
| 760 | mem_width = 1; |
| 761 | else |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 762 | mem_width = 0; |
| 763 | |
Viresh Kumar | 69dc14b | 2011-04-18 14:54:56 +0530 | [diff] [blame] | 764 | slave_sg_todev_fill_desc: |
| 765 | desc = dwc_desc_get(dwc); |
| 766 | if (!desc) { |
| 767 | dev_err(chan2dev(chan), |
| 768 | "not enough descriptors available\n"); |
| 769 | goto err_desc_get; |
| 770 | } |
| 771 | |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 772 | desc->lli.sar = mem; |
| 773 | desc->lli.dar = reg; |
| 774 | desc->lli.ctllo = ctllo | DWC_CTLL_SRC_WIDTH(mem_width); |
Viresh Kumar | 69dc14b | 2011-04-18 14:54:56 +0530 | [diff] [blame] | 775 | if ((len >> mem_width) > DWC_MAX_COUNT) { |
| 776 | dlen = DWC_MAX_COUNT << mem_width; |
| 777 | mem += dlen; |
| 778 | len -= dlen; |
| 779 | } else { |
| 780 | dlen = len; |
| 781 | len = 0; |
| 782 | } |
| 783 | |
| 784 | desc->lli.ctlhi = dlen >> mem_width; |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 785 | |
| 786 | if (!first) { |
| 787 | first = desc; |
| 788 | } else { |
| 789 | prev->lli.llp = desc->txd.phys; |
Dan Williams | 41d5e59 | 2009-01-06 11:38:21 -0700 | [diff] [blame] | 790 | dma_sync_single_for_device(chan2parent(chan), |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 791 | prev->txd.phys, |
| 792 | sizeof(prev->lli), |
| 793 | DMA_TO_DEVICE); |
| 794 | list_add_tail(&desc->desc_node, |
Dan Williams | e0bd0f8 | 2009-09-08 17:53:02 -0700 | [diff] [blame] | 795 | &first->tx_list); |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 796 | } |
| 797 | prev = desc; |
Viresh Kumar | 69dc14b | 2011-04-18 14:54:56 +0530 | [diff] [blame] | 798 | total_len += dlen; |
| 799 | |
| 800 | if (len) |
| 801 | goto slave_sg_todev_fill_desc; |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 802 | } |
| 803 | break; |
Vinod Koul | db8196d | 2011-10-13 22:34:23 +0530 | [diff] [blame] | 804 | case DMA_DEV_TO_MEM: |
Viresh Kumar | 327e697 | 2012-02-01 16:12:26 +0530 | [diff] [blame] | 805 | reg_width = __fls(sconfig->src_addr_width); |
| 806 | reg = sconfig->src_addr; |
| 807 | ctllo = (DWC_DEFAULT_CTLLO(chan) |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 808 | | DWC_CTLL_SRC_WIDTH(reg_width) |
| 809 | | DWC_CTLL_DST_INC |
Viresh Kumar | 327e697 | 2012-02-01 16:12:26 +0530 | [diff] [blame] | 810 | | DWC_CTLL_SRC_FIX); |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 811 | |
Viresh Kumar | 327e697 | 2012-02-01 16:12:26 +0530 | [diff] [blame] | 812 | ctllo |= sconfig->device_fc ? DWC_CTLL_FC(DW_DMA_FC_P_P2M) : |
| 813 | DWC_CTLL_FC(DW_DMA_FC_D_P2M); |
| 814 | |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 815 | for_each_sg(sgl, sg, sg_len, i) { |
| 816 | struct dw_desc *desc; |
Viresh Kumar | 69dc14b | 2011-04-18 14:54:56 +0530 | [diff] [blame] | 817 | u32 len, dlen, mem; |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 818 | |
Lars-Peter Clausen | cbb796c | 2012-04-25 20:50:51 +0200 | [diff] [blame] | 819 | mem = sg_dma_address(sg); |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 820 | len = sg_dma_len(sg); |
Viresh Kumar | 6bc711f | 2012-02-01 16:12:25 +0530 | [diff] [blame] | 821 | |
| 822 | if (!((mem | len) & 7)) |
| 823 | mem_width = 3; |
| 824 | else if (!((mem | len) & 3)) |
| 825 | mem_width = 2; |
| 826 | else if (!((mem | len) & 1)) |
| 827 | mem_width = 1; |
| 828 | else |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 829 | mem_width = 0; |
| 830 | |
Viresh Kumar | 69dc14b | 2011-04-18 14:54:56 +0530 | [diff] [blame] | 831 | slave_sg_fromdev_fill_desc: |
| 832 | desc = dwc_desc_get(dwc); |
| 833 | if (!desc) { |
| 834 | dev_err(chan2dev(chan), |
| 835 | "not enough descriptors available\n"); |
| 836 | goto err_desc_get; |
| 837 | } |
| 838 | |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 839 | desc->lli.sar = reg; |
| 840 | desc->lli.dar = mem; |
| 841 | desc->lli.ctllo = ctllo | DWC_CTLL_DST_WIDTH(mem_width); |
Viresh Kumar | 69dc14b | 2011-04-18 14:54:56 +0530 | [diff] [blame] | 842 | if ((len >> reg_width) > DWC_MAX_COUNT) { |
| 843 | dlen = DWC_MAX_COUNT << reg_width; |
| 844 | mem += dlen; |
| 845 | len -= dlen; |
| 846 | } else { |
| 847 | dlen = len; |
| 848 | len = 0; |
| 849 | } |
| 850 | desc->lli.ctlhi = dlen >> reg_width; |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 851 | |
| 852 | if (!first) { |
| 853 | first = desc; |
| 854 | } else { |
| 855 | prev->lli.llp = desc->txd.phys; |
Dan Williams | 41d5e59 | 2009-01-06 11:38:21 -0700 | [diff] [blame] | 856 | dma_sync_single_for_device(chan2parent(chan), |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 857 | prev->txd.phys, |
| 858 | sizeof(prev->lli), |
| 859 | DMA_TO_DEVICE); |
| 860 | list_add_tail(&desc->desc_node, |
Dan Williams | e0bd0f8 | 2009-09-08 17:53:02 -0700 | [diff] [blame] | 861 | &first->tx_list); |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 862 | } |
| 863 | prev = desc; |
Viresh Kumar | 69dc14b | 2011-04-18 14:54:56 +0530 | [diff] [blame] | 864 | total_len += dlen; |
| 865 | |
| 866 | if (len) |
| 867 | goto slave_sg_fromdev_fill_desc; |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 868 | } |
| 869 | break; |
| 870 | default: |
| 871 | return NULL; |
| 872 | } |
| 873 | |
| 874 | if (flags & DMA_PREP_INTERRUPT) |
| 875 | /* Trigger interrupt after last block */ |
| 876 | prev->lli.ctllo |= DWC_CTLL_INT_EN; |
| 877 | |
| 878 | prev->lli.llp = 0; |
Dan Williams | 41d5e59 | 2009-01-06 11:38:21 -0700 | [diff] [blame] | 879 | dma_sync_single_for_device(chan2parent(chan), |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 880 | prev->txd.phys, sizeof(prev->lli), |
| 881 | DMA_TO_DEVICE); |
| 882 | |
| 883 | first->len = total_len; |
| 884 | |
| 885 | return &first->txd; |
| 886 | |
| 887 | err_desc_get: |
| 888 | dwc_desc_put(dwc, first); |
| 889 | return NULL; |
| 890 | } |
| 891 | |
Viresh Kumar | 327e697 | 2012-02-01 16:12:26 +0530 | [diff] [blame] | 892 | /* |
| 893 | * Fix sconfig's burst size according to dw_dmac. We need to convert them as: |
| 894 | * 1 -> 0, 4 -> 1, 8 -> 2, 16 -> 3. |
| 895 | * |
| 896 | * NOTE: burst size 2 is not supported by controller. |
| 897 | * |
| 898 | * This can be done by finding least significant bit set: n & (n - 1) |
| 899 | */ |
| 900 | static inline void convert_burst(u32 *maxburst) |
| 901 | { |
| 902 | if (*maxburst > 1) |
| 903 | *maxburst = fls(*maxburst) - 2; |
| 904 | else |
| 905 | *maxburst = 0; |
| 906 | } |
| 907 | |
| 908 | static int |
| 909 | set_runtime_config(struct dma_chan *chan, struct dma_slave_config *sconfig) |
| 910 | { |
| 911 | struct dw_dma_chan *dwc = to_dw_dma_chan(chan); |
| 912 | |
| 913 | /* Check if it is chan is configured for slave transfers */ |
| 914 | if (!chan->private) |
| 915 | return -EINVAL; |
| 916 | |
| 917 | memcpy(&dwc->dma_sconfig, sconfig, sizeof(*sconfig)); |
| 918 | |
| 919 | convert_burst(&dwc->dma_sconfig.src_maxburst); |
| 920 | convert_burst(&dwc->dma_sconfig.dst_maxburst); |
| 921 | |
| 922 | return 0; |
| 923 | } |
| 924 | |
Linus Walleij | 0582763 | 2010-05-17 16:30:42 -0700 | [diff] [blame] | 925 | static int dwc_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd, |
| 926 | unsigned long arg) |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 927 | { |
| 928 | struct dw_dma_chan *dwc = to_dw_dma_chan(chan); |
| 929 | struct dw_dma *dw = to_dw_dma(chan->device); |
| 930 | struct dw_desc *desc, *_desc; |
Viresh Kumar | 69cea5a | 2011-04-15 16:03:35 +0530 | [diff] [blame] | 931 | unsigned long flags; |
Linus Walleij | a7c57cf | 2011-04-19 08:31:32 +0800 | [diff] [blame] | 932 | u32 cfglo; |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 933 | LIST_HEAD(list); |
| 934 | |
Linus Walleij | a7c57cf | 2011-04-19 08:31:32 +0800 | [diff] [blame] | 935 | if (cmd == DMA_PAUSE) { |
| 936 | spin_lock_irqsave(&dwc->lock, flags); |
| 937 | |
| 938 | cfglo = channel_readl(dwc, CFG_LO); |
| 939 | channel_writel(dwc, CFG_LO, cfglo | DWC_CFGL_CH_SUSP); |
| 940 | while (!(channel_readl(dwc, CFG_LO) & DWC_CFGL_FIFO_EMPTY)) |
| 941 | cpu_relax(); |
| 942 | |
| 943 | dwc->paused = true; |
| 944 | spin_unlock_irqrestore(&dwc->lock, flags); |
| 945 | } else if (cmd == DMA_RESUME) { |
| 946 | if (!dwc->paused) |
| 947 | return 0; |
| 948 | |
| 949 | spin_lock_irqsave(&dwc->lock, flags); |
| 950 | |
| 951 | cfglo = channel_readl(dwc, CFG_LO); |
| 952 | channel_writel(dwc, CFG_LO, cfglo & ~DWC_CFGL_CH_SUSP); |
| 953 | dwc->paused = false; |
| 954 | |
| 955 | spin_unlock_irqrestore(&dwc->lock, flags); |
| 956 | } else if (cmd == DMA_TERMINATE_ALL) { |
| 957 | spin_lock_irqsave(&dwc->lock, flags); |
| 958 | |
| 959 | channel_clear_bit(dw, CH_EN, dwc->mask); |
| 960 | while (dma_readl(dw, CH_EN) & dwc->mask) |
| 961 | cpu_relax(); |
| 962 | |
| 963 | dwc->paused = false; |
| 964 | |
| 965 | /* active_list entries will end up before queued entries */ |
| 966 | list_splice_init(&dwc->queue, &list); |
| 967 | list_splice_init(&dwc->active_list, &list); |
| 968 | |
| 969 | spin_unlock_irqrestore(&dwc->lock, flags); |
| 970 | |
| 971 | /* Flush all pending and queued descriptors */ |
| 972 | list_for_each_entry_safe(desc, _desc, &list, desc_node) |
| 973 | dwc_descriptor_complete(dwc, desc, false); |
Viresh Kumar | 327e697 | 2012-02-01 16:12:26 +0530 | [diff] [blame] | 974 | } else if (cmd == DMA_SLAVE_CONFIG) { |
| 975 | return set_runtime_config(chan, (struct dma_slave_config *)arg); |
| 976 | } else { |
Linus Walleij | c3635c7 | 2010-03-26 16:44:01 -0700 | [diff] [blame] | 977 | return -ENXIO; |
Viresh Kumar | 327e697 | 2012-02-01 16:12:26 +0530 | [diff] [blame] | 978 | } |
Linus Walleij | c3635c7 | 2010-03-26 16:44:01 -0700 | [diff] [blame] | 979 | |
Linus Walleij | c3635c7 | 2010-03-26 16:44:01 -0700 | [diff] [blame] | 980 | return 0; |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 981 | } |
| 982 | |
| 983 | static enum dma_status |
Linus Walleij | 0793448 | 2010-03-26 16:50:49 -0700 | [diff] [blame] | 984 | dwc_tx_status(struct dma_chan *chan, |
| 985 | dma_cookie_t cookie, |
| 986 | struct dma_tx_state *txstate) |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 987 | { |
| 988 | struct dw_dma_chan *dwc = to_dw_dma_chan(chan); |
Russell King - ARM Linux | 96a2af4 | 2012-03-06 22:35:27 +0000 | [diff] [blame] | 989 | enum dma_status ret; |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 990 | |
Russell King - ARM Linux | 96a2af4 | 2012-03-06 22:35:27 +0000 | [diff] [blame] | 991 | ret = dma_cookie_status(chan, cookie, txstate); |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 992 | if (ret != DMA_SUCCESS) { |
| 993 | dwc_scan_descriptors(to_dw_dma(chan->device), dwc); |
| 994 | |
Russell King - ARM Linux | 96a2af4 | 2012-03-06 22:35:27 +0000 | [diff] [blame] | 995 | ret = dma_cookie_status(chan, cookie, txstate); |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 996 | } |
| 997 | |
Viresh Kumar | abf5390 | 2011-04-15 16:03:35 +0530 | [diff] [blame] | 998 | if (ret != DMA_SUCCESS) |
Russell King - ARM Linux | 96a2af4 | 2012-03-06 22:35:27 +0000 | [diff] [blame] | 999 | dma_set_residue(txstate, dwc_first_active(dwc)->len); |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 1000 | |
Linus Walleij | a7c57cf | 2011-04-19 08:31:32 +0800 | [diff] [blame] | 1001 | if (dwc->paused) |
| 1002 | return DMA_PAUSED; |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 1003 | |
| 1004 | return ret; |
| 1005 | } |
| 1006 | |
| 1007 | static void dwc_issue_pending(struct dma_chan *chan) |
| 1008 | { |
| 1009 | struct dw_dma_chan *dwc = to_dw_dma_chan(chan); |
| 1010 | |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 1011 | if (!list_empty(&dwc->queue)) |
| 1012 | dwc_scan_descriptors(to_dw_dma(chan->device), dwc); |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 1013 | } |
| 1014 | |
Dan Williams | aa1e6f1 | 2009-01-06 11:38:17 -0700 | [diff] [blame] | 1015 | static int dwc_alloc_chan_resources(struct dma_chan *chan) |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 1016 | { |
| 1017 | struct dw_dma_chan *dwc = to_dw_dma_chan(chan); |
| 1018 | struct dw_dma *dw = to_dw_dma(chan->device); |
| 1019 | struct dw_desc *desc; |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 1020 | int i; |
Viresh Kumar | 69cea5a | 2011-04-15 16:03:35 +0530 | [diff] [blame] | 1021 | unsigned long flags; |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 1022 | |
Andy Shevchenko | 2e4c364 | 2012-06-19 13:34:05 +0300 | [diff] [blame^] | 1023 | dev_vdbg(chan2dev(chan), "%s\n", __func__); |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 1024 | |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 1025 | /* ASSERT: channel is idle */ |
| 1026 | if (dma_readl(dw, CH_EN) & dwc->mask) { |
Dan Williams | 41d5e59 | 2009-01-06 11:38:21 -0700 | [diff] [blame] | 1027 | dev_dbg(chan2dev(chan), "DMA channel not idle?\n"); |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 1028 | return -EIO; |
| 1029 | } |
| 1030 | |
Russell King - ARM Linux | d3ee98cdc | 2012-03-06 22:35:47 +0000 | [diff] [blame] | 1031 | dma_cookie_init(chan); |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 1032 | |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 1033 | /* |
| 1034 | * NOTE: some controllers may have additional features that we |
| 1035 | * need to initialize here, like "scatter-gather" (which |
| 1036 | * doesn't mean what you think it means), and status writeback. |
| 1037 | */ |
| 1038 | |
Viresh Kumar | 69cea5a | 2011-04-15 16:03:35 +0530 | [diff] [blame] | 1039 | spin_lock_irqsave(&dwc->lock, flags); |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 1040 | i = dwc->descs_allocated; |
| 1041 | while (dwc->descs_allocated < NR_DESCS_PER_CHANNEL) { |
Viresh Kumar | 69cea5a | 2011-04-15 16:03:35 +0530 | [diff] [blame] | 1042 | spin_unlock_irqrestore(&dwc->lock, flags); |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 1043 | |
| 1044 | desc = kzalloc(sizeof(struct dw_desc), GFP_KERNEL); |
| 1045 | if (!desc) { |
Dan Williams | 41d5e59 | 2009-01-06 11:38:21 -0700 | [diff] [blame] | 1046 | dev_info(chan2dev(chan), |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 1047 | "only allocated %d descriptors\n", i); |
Viresh Kumar | 69cea5a | 2011-04-15 16:03:35 +0530 | [diff] [blame] | 1048 | spin_lock_irqsave(&dwc->lock, flags); |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 1049 | break; |
| 1050 | } |
| 1051 | |
Dan Williams | e0bd0f8 | 2009-09-08 17:53:02 -0700 | [diff] [blame] | 1052 | INIT_LIST_HEAD(&desc->tx_list); |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 1053 | dma_async_tx_descriptor_init(&desc->txd, chan); |
| 1054 | desc->txd.tx_submit = dwc_tx_submit; |
| 1055 | desc->txd.flags = DMA_CTRL_ACK; |
Dan Williams | 41d5e59 | 2009-01-06 11:38:21 -0700 | [diff] [blame] | 1056 | desc->txd.phys = dma_map_single(chan2parent(chan), &desc->lli, |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 1057 | sizeof(desc->lli), DMA_TO_DEVICE); |
| 1058 | dwc_desc_put(dwc, desc); |
| 1059 | |
Viresh Kumar | 69cea5a | 2011-04-15 16:03:35 +0530 | [diff] [blame] | 1060 | spin_lock_irqsave(&dwc->lock, flags); |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 1061 | i = ++dwc->descs_allocated; |
| 1062 | } |
| 1063 | |
Viresh Kumar | 69cea5a | 2011-04-15 16:03:35 +0530 | [diff] [blame] | 1064 | spin_unlock_irqrestore(&dwc->lock, flags); |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 1065 | |
Andy Shevchenko | 2e4c364 | 2012-06-19 13:34:05 +0300 | [diff] [blame^] | 1066 | dev_dbg(chan2dev(chan), "%s: allocated %d descriptors\n", __func__, i); |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 1067 | |
| 1068 | return i; |
| 1069 | } |
| 1070 | |
| 1071 | static void dwc_free_chan_resources(struct dma_chan *chan) |
| 1072 | { |
| 1073 | struct dw_dma_chan *dwc = to_dw_dma_chan(chan); |
| 1074 | struct dw_dma *dw = to_dw_dma(chan->device); |
| 1075 | struct dw_desc *desc, *_desc; |
Viresh Kumar | 69cea5a | 2011-04-15 16:03:35 +0530 | [diff] [blame] | 1076 | unsigned long flags; |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 1077 | LIST_HEAD(list); |
| 1078 | |
Andy Shevchenko | 2e4c364 | 2012-06-19 13:34:05 +0300 | [diff] [blame^] | 1079 | dev_dbg(chan2dev(chan), "%s: descs allocated=%u\n", __func__, |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 1080 | dwc->descs_allocated); |
| 1081 | |
| 1082 | /* ASSERT: channel is idle */ |
| 1083 | BUG_ON(!list_empty(&dwc->active_list)); |
| 1084 | BUG_ON(!list_empty(&dwc->queue)); |
| 1085 | BUG_ON(dma_readl(to_dw_dma(chan->device), CH_EN) & dwc->mask); |
| 1086 | |
Viresh Kumar | 69cea5a | 2011-04-15 16:03:35 +0530 | [diff] [blame] | 1087 | spin_lock_irqsave(&dwc->lock, flags); |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 1088 | list_splice_init(&dwc->free_list, &list); |
| 1089 | dwc->descs_allocated = 0; |
Viresh Kumar | 61e183f | 2011-11-17 16:01:29 +0530 | [diff] [blame] | 1090 | dwc->initialized = false; |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 1091 | |
| 1092 | /* Disable interrupts */ |
| 1093 | channel_clear_bit(dw, MASK.XFER, dwc->mask); |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 1094 | channel_clear_bit(dw, MASK.ERROR, dwc->mask); |
| 1095 | |
Viresh Kumar | 69cea5a | 2011-04-15 16:03:35 +0530 | [diff] [blame] | 1096 | spin_unlock_irqrestore(&dwc->lock, flags); |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 1097 | |
| 1098 | list_for_each_entry_safe(desc, _desc, &list, desc_node) { |
Dan Williams | 41d5e59 | 2009-01-06 11:38:21 -0700 | [diff] [blame] | 1099 | dev_vdbg(chan2dev(chan), " freeing descriptor %p\n", desc); |
| 1100 | dma_unmap_single(chan2parent(chan), desc->txd.phys, |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 1101 | sizeof(desc->lli), DMA_TO_DEVICE); |
| 1102 | kfree(desc); |
| 1103 | } |
| 1104 | |
Andy Shevchenko | 2e4c364 | 2012-06-19 13:34:05 +0300 | [diff] [blame^] | 1105 | dev_vdbg(chan2dev(chan), "%s: done\n", __func__); |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 1106 | } |
| 1107 | |
Hans-Christian Egtvedt | d9de451 | 2009-04-01 15:47:02 +0200 | [diff] [blame] | 1108 | /* --------------------- Cyclic DMA API extensions -------------------- */ |
| 1109 | |
| 1110 | /** |
| 1111 | * dw_dma_cyclic_start - start the cyclic DMA transfer |
| 1112 | * @chan: the DMA channel to start |
| 1113 | * |
| 1114 | * Must be called with soft interrupts disabled. Returns zero on success or |
| 1115 | * -errno on failure. |
| 1116 | */ |
| 1117 | int dw_dma_cyclic_start(struct dma_chan *chan) |
| 1118 | { |
| 1119 | struct dw_dma_chan *dwc = to_dw_dma_chan(chan); |
| 1120 | struct dw_dma *dw = to_dw_dma(dwc->chan.device); |
Viresh Kumar | 69cea5a | 2011-04-15 16:03:35 +0530 | [diff] [blame] | 1121 | unsigned long flags; |
Hans-Christian Egtvedt | d9de451 | 2009-04-01 15:47:02 +0200 | [diff] [blame] | 1122 | |
| 1123 | if (!test_bit(DW_DMA_IS_CYCLIC, &dwc->flags)) { |
| 1124 | dev_err(chan2dev(&dwc->chan), "missing prep for cyclic DMA\n"); |
| 1125 | return -ENODEV; |
| 1126 | } |
| 1127 | |
Viresh Kumar | 69cea5a | 2011-04-15 16:03:35 +0530 | [diff] [blame] | 1128 | spin_lock_irqsave(&dwc->lock, flags); |
Hans-Christian Egtvedt | d9de451 | 2009-04-01 15:47:02 +0200 | [diff] [blame] | 1129 | |
| 1130 | /* assert channel is idle */ |
| 1131 | if (dma_readl(dw, CH_EN) & dwc->mask) { |
| 1132 | dev_err(chan2dev(&dwc->chan), |
| 1133 | "BUG: Attempted to start non-idle channel\n"); |
Andy Shevchenko | 1d45543 | 2012-06-19 13:34:03 +0300 | [diff] [blame] | 1134 | dwc_dump_chan_regs(dwc); |
Viresh Kumar | 69cea5a | 2011-04-15 16:03:35 +0530 | [diff] [blame] | 1135 | spin_unlock_irqrestore(&dwc->lock, flags); |
Hans-Christian Egtvedt | d9de451 | 2009-04-01 15:47:02 +0200 | [diff] [blame] | 1136 | return -EBUSY; |
| 1137 | } |
| 1138 | |
Hans-Christian Egtvedt | d9de451 | 2009-04-01 15:47:02 +0200 | [diff] [blame] | 1139 | dma_writel(dw, CLEAR.ERROR, dwc->mask); |
| 1140 | dma_writel(dw, CLEAR.XFER, dwc->mask); |
| 1141 | |
| 1142 | /* setup DMAC channel registers */ |
| 1143 | channel_writel(dwc, LLP, dwc->cdesc->desc[0]->txd.phys); |
| 1144 | channel_writel(dwc, CTL_LO, DWC_CTLL_LLP_D_EN | DWC_CTLL_LLP_S_EN); |
| 1145 | channel_writel(dwc, CTL_HI, 0); |
| 1146 | |
| 1147 | channel_set_bit(dw, CH_EN, dwc->mask); |
| 1148 | |
Viresh Kumar | 69cea5a | 2011-04-15 16:03:35 +0530 | [diff] [blame] | 1149 | spin_unlock_irqrestore(&dwc->lock, flags); |
Hans-Christian Egtvedt | d9de451 | 2009-04-01 15:47:02 +0200 | [diff] [blame] | 1150 | |
| 1151 | return 0; |
| 1152 | } |
| 1153 | EXPORT_SYMBOL(dw_dma_cyclic_start); |
| 1154 | |
| 1155 | /** |
| 1156 | * dw_dma_cyclic_stop - stop the cyclic DMA transfer |
| 1157 | * @chan: the DMA channel to stop |
| 1158 | * |
| 1159 | * Must be called with soft interrupts disabled. |
| 1160 | */ |
| 1161 | void dw_dma_cyclic_stop(struct dma_chan *chan) |
| 1162 | { |
| 1163 | struct dw_dma_chan *dwc = to_dw_dma_chan(chan); |
| 1164 | struct dw_dma *dw = to_dw_dma(dwc->chan.device); |
Viresh Kumar | 69cea5a | 2011-04-15 16:03:35 +0530 | [diff] [blame] | 1165 | unsigned long flags; |
Hans-Christian Egtvedt | d9de451 | 2009-04-01 15:47:02 +0200 | [diff] [blame] | 1166 | |
Viresh Kumar | 69cea5a | 2011-04-15 16:03:35 +0530 | [diff] [blame] | 1167 | spin_lock_irqsave(&dwc->lock, flags); |
Hans-Christian Egtvedt | d9de451 | 2009-04-01 15:47:02 +0200 | [diff] [blame] | 1168 | |
| 1169 | channel_clear_bit(dw, CH_EN, dwc->mask); |
| 1170 | while (dma_readl(dw, CH_EN) & dwc->mask) |
| 1171 | cpu_relax(); |
| 1172 | |
Viresh Kumar | 69cea5a | 2011-04-15 16:03:35 +0530 | [diff] [blame] | 1173 | spin_unlock_irqrestore(&dwc->lock, flags); |
Hans-Christian Egtvedt | d9de451 | 2009-04-01 15:47:02 +0200 | [diff] [blame] | 1174 | } |
| 1175 | EXPORT_SYMBOL(dw_dma_cyclic_stop); |
| 1176 | |
| 1177 | /** |
| 1178 | * dw_dma_cyclic_prep - prepare the cyclic DMA transfer |
| 1179 | * @chan: the DMA channel to prepare |
| 1180 | * @buf_addr: physical DMA address where the buffer starts |
| 1181 | * @buf_len: total number of bytes for the entire buffer |
| 1182 | * @period_len: number of bytes for each period |
| 1183 | * @direction: transfer direction, to or from device |
| 1184 | * |
| 1185 | * Must be called before trying to start the transfer. Returns a valid struct |
| 1186 | * dw_cyclic_desc if successful or an ERR_PTR(-errno) if not successful. |
| 1187 | */ |
| 1188 | struct dw_cyclic_desc *dw_dma_cyclic_prep(struct dma_chan *chan, |
| 1189 | dma_addr_t buf_addr, size_t buf_len, size_t period_len, |
Vinod Koul | db8196d | 2011-10-13 22:34:23 +0530 | [diff] [blame] | 1190 | enum dma_transfer_direction direction) |
Hans-Christian Egtvedt | d9de451 | 2009-04-01 15:47:02 +0200 | [diff] [blame] | 1191 | { |
| 1192 | struct dw_dma_chan *dwc = to_dw_dma_chan(chan); |
Viresh Kumar | 327e697 | 2012-02-01 16:12:26 +0530 | [diff] [blame] | 1193 | struct dma_slave_config *sconfig = &dwc->dma_sconfig; |
Hans-Christian Egtvedt | d9de451 | 2009-04-01 15:47:02 +0200 | [diff] [blame] | 1194 | struct dw_cyclic_desc *cdesc; |
| 1195 | struct dw_cyclic_desc *retval = NULL; |
| 1196 | struct dw_desc *desc; |
| 1197 | struct dw_desc *last = NULL; |
Hans-Christian Egtvedt | d9de451 | 2009-04-01 15:47:02 +0200 | [diff] [blame] | 1198 | unsigned long was_cyclic; |
| 1199 | unsigned int reg_width; |
| 1200 | unsigned int periods; |
| 1201 | unsigned int i; |
Viresh Kumar | 69cea5a | 2011-04-15 16:03:35 +0530 | [diff] [blame] | 1202 | unsigned long flags; |
Hans-Christian Egtvedt | d9de451 | 2009-04-01 15:47:02 +0200 | [diff] [blame] | 1203 | |
Viresh Kumar | 69cea5a | 2011-04-15 16:03:35 +0530 | [diff] [blame] | 1204 | spin_lock_irqsave(&dwc->lock, flags); |
Hans-Christian Egtvedt | d9de451 | 2009-04-01 15:47:02 +0200 | [diff] [blame] | 1205 | if (!list_empty(&dwc->queue) || !list_empty(&dwc->active_list)) { |
Viresh Kumar | 69cea5a | 2011-04-15 16:03:35 +0530 | [diff] [blame] | 1206 | spin_unlock_irqrestore(&dwc->lock, flags); |
Hans-Christian Egtvedt | d9de451 | 2009-04-01 15:47:02 +0200 | [diff] [blame] | 1207 | dev_dbg(chan2dev(&dwc->chan), |
| 1208 | "queue and/or active list are not empty\n"); |
| 1209 | return ERR_PTR(-EBUSY); |
| 1210 | } |
| 1211 | |
| 1212 | was_cyclic = test_and_set_bit(DW_DMA_IS_CYCLIC, &dwc->flags); |
Viresh Kumar | 69cea5a | 2011-04-15 16:03:35 +0530 | [diff] [blame] | 1213 | spin_unlock_irqrestore(&dwc->lock, flags); |
Hans-Christian Egtvedt | d9de451 | 2009-04-01 15:47:02 +0200 | [diff] [blame] | 1214 | if (was_cyclic) { |
| 1215 | dev_dbg(chan2dev(&dwc->chan), |
| 1216 | "channel already prepared for cyclic DMA\n"); |
| 1217 | return ERR_PTR(-EBUSY); |
| 1218 | } |
| 1219 | |
| 1220 | retval = ERR_PTR(-EINVAL); |
Viresh Kumar | 327e697 | 2012-02-01 16:12:26 +0530 | [diff] [blame] | 1221 | |
| 1222 | if (direction == DMA_MEM_TO_DEV) |
| 1223 | reg_width = __ffs(sconfig->dst_addr_width); |
| 1224 | else |
| 1225 | reg_width = __ffs(sconfig->src_addr_width); |
| 1226 | |
Hans-Christian Egtvedt | d9de451 | 2009-04-01 15:47:02 +0200 | [diff] [blame] | 1227 | periods = buf_len / period_len; |
| 1228 | |
| 1229 | /* Check for too big/unaligned periods and unaligned DMA buffer. */ |
| 1230 | if (period_len > (DWC_MAX_COUNT << reg_width)) |
| 1231 | goto out_err; |
| 1232 | if (unlikely(period_len & ((1 << reg_width) - 1))) |
| 1233 | goto out_err; |
| 1234 | if (unlikely(buf_addr & ((1 << reg_width) - 1))) |
| 1235 | goto out_err; |
Vinod Koul | db8196d | 2011-10-13 22:34:23 +0530 | [diff] [blame] | 1236 | if (unlikely(!(direction & (DMA_MEM_TO_DEV | DMA_DEV_TO_MEM)))) |
Hans-Christian Egtvedt | d9de451 | 2009-04-01 15:47:02 +0200 | [diff] [blame] | 1237 | goto out_err; |
| 1238 | |
| 1239 | retval = ERR_PTR(-ENOMEM); |
| 1240 | |
| 1241 | if (periods > NR_DESCS_PER_CHANNEL) |
| 1242 | goto out_err; |
| 1243 | |
| 1244 | cdesc = kzalloc(sizeof(struct dw_cyclic_desc), GFP_KERNEL); |
| 1245 | if (!cdesc) |
| 1246 | goto out_err; |
| 1247 | |
| 1248 | cdesc->desc = kzalloc(sizeof(struct dw_desc *) * periods, GFP_KERNEL); |
| 1249 | if (!cdesc->desc) |
| 1250 | goto out_err_alloc; |
| 1251 | |
| 1252 | for (i = 0; i < periods; i++) { |
| 1253 | desc = dwc_desc_get(dwc); |
| 1254 | if (!desc) |
| 1255 | goto out_err_desc_get; |
| 1256 | |
| 1257 | switch (direction) { |
Vinod Koul | db8196d | 2011-10-13 22:34:23 +0530 | [diff] [blame] | 1258 | case DMA_MEM_TO_DEV: |
Viresh Kumar | 327e697 | 2012-02-01 16:12:26 +0530 | [diff] [blame] | 1259 | desc->lli.dar = sconfig->dst_addr; |
Hans-Christian Egtvedt | d9de451 | 2009-04-01 15:47:02 +0200 | [diff] [blame] | 1260 | desc->lli.sar = buf_addr + (period_len * i); |
Viresh Kumar | 327e697 | 2012-02-01 16:12:26 +0530 | [diff] [blame] | 1261 | desc->lli.ctllo = (DWC_DEFAULT_CTLLO(chan) |
Hans-Christian Egtvedt | d9de451 | 2009-04-01 15:47:02 +0200 | [diff] [blame] | 1262 | | DWC_CTLL_DST_WIDTH(reg_width) |
| 1263 | | DWC_CTLL_SRC_WIDTH(reg_width) |
| 1264 | | DWC_CTLL_DST_FIX |
| 1265 | | DWC_CTLL_SRC_INC |
Hans-Christian Egtvedt | d9de451 | 2009-04-01 15:47:02 +0200 | [diff] [blame] | 1266 | | DWC_CTLL_INT_EN); |
Viresh Kumar | 327e697 | 2012-02-01 16:12:26 +0530 | [diff] [blame] | 1267 | |
| 1268 | desc->lli.ctllo |= sconfig->device_fc ? |
| 1269 | DWC_CTLL_FC(DW_DMA_FC_P_M2P) : |
| 1270 | DWC_CTLL_FC(DW_DMA_FC_D_M2P); |
| 1271 | |
Hans-Christian Egtvedt | d9de451 | 2009-04-01 15:47:02 +0200 | [diff] [blame] | 1272 | break; |
Vinod Koul | db8196d | 2011-10-13 22:34:23 +0530 | [diff] [blame] | 1273 | case DMA_DEV_TO_MEM: |
Hans-Christian Egtvedt | d9de451 | 2009-04-01 15:47:02 +0200 | [diff] [blame] | 1274 | desc->lli.dar = buf_addr + (period_len * i); |
Viresh Kumar | 327e697 | 2012-02-01 16:12:26 +0530 | [diff] [blame] | 1275 | desc->lli.sar = sconfig->src_addr; |
| 1276 | desc->lli.ctllo = (DWC_DEFAULT_CTLLO(chan) |
Hans-Christian Egtvedt | d9de451 | 2009-04-01 15:47:02 +0200 | [diff] [blame] | 1277 | | DWC_CTLL_SRC_WIDTH(reg_width) |
| 1278 | | DWC_CTLL_DST_WIDTH(reg_width) |
| 1279 | | DWC_CTLL_DST_INC |
| 1280 | | DWC_CTLL_SRC_FIX |
Hans-Christian Egtvedt | d9de451 | 2009-04-01 15:47:02 +0200 | [diff] [blame] | 1281 | | DWC_CTLL_INT_EN); |
Viresh Kumar | 327e697 | 2012-02-01 16:12:26 +0530 | [diff] [blame] | 1282 | |
| 1283 | desc->lli.ctllo |= sconfig->device_fc ? |
| 1284 | DWC_CTLL_FC(DW_DMA_FC_P_P2M) : |
| 1285 | DWC_CTLL_FC(DW_DMA_FC_D_P2M); |
| 1286 | |
Hans-Christian Egtvedt | d9de451 | 2009-04-01 15:47:02 +0200 | [diff] [blame] | 1287 | break; |
| 1288 | default: |
| 1289 | break; |
| 1290 | } |
| 1291 | |
| 1292 | desc->lli.ctlhi = (period_len >> reg_width); |
| 1293 | cdesc->desc[i] = desc; |
| 1294 | |
| 1295 | if (last) { |
| 1296 | last->lli.llp = desc->txd.phys; |
| 1297 | dma_sync_single_for_device(chan2parent(chan), |
| 1298 | last->txd.phys, sizeof(last->lli), |
| 1299 | DMA_TO_DEVICE); |
| 1300 | } |
| 1301 | |
| 1302 | last = desc; |
| 1303 | } |
| 1304 | |
| 1305 | /* lets make a cyclic list */ |
| 1306 | last->lli.llp = cdesc->desc[0]->txd.phys; |
| 1307 | dma_sync_single_for_device(chan2parent(chan), last->txd.phys, |
| 1308 | sizeof(last->lli), DMA_TO_DEVICE); |
| 1309 | |
Andy Shevchenko | 2f45d61 | 2012-06-19 13:34:02 +0300 | [diff] [blame] | 1310 | dev_dbg(chan2dev(&dwc->chan), "cyclic prepared buf 0x%llx len %zu " |
| 1311 | "period %zu periods %d\n", (unsigned long long)buf_addr, |
| 1312 | buf_len, period_len, periods); |
Hans-Christian Egtvedt | d9de451 | 2009-04-01 15:47:02 +0200 | [diff] [blame] | 1313 | |
| 1314 | cdesc->periods = periods; |
| 1315 | dwc->cdesc = cdesc; |
| 1316 | |
| 1317 | return cdesc; |
| 1318 | |
| 1319 | out_err_desc_get: |
| 1320 | while (i--) |
| 1321 | dwc_desc_put(dwc, cdesc->desc[i]); |
| 1322 | out_err_alloc: |
| 1323 | kfree(cdesc); |
| 1324 | out_err: |
| 1325 | clear_bit(DW_DMA_IS_CYCLIC, &dwc->flags); |
| 1326 | return (struct dw_cyclic_desc *)retval; |
| 1327 | } |
| 1328 | EXPORT_SYMBOL(dw_dma_cyclic_prep); |
| 1329 | |
| 1330 | /** |
| 1331 | * dw_dma_cyclic_free - free a prepared cyclic DMA transfer |
| 1332 | * @chan: the DMA channel to free |
| 1333 | */ |
| 1334 | void dw_dma_cyclic_free(struct dma_chan *chan) |
| 1335 | { |
| 1336 | struct dw_dma_chan *dwc = to_dw_dma_chan(chan); |
| 1337 | struct dw_dma *dw = to_dw_dma(dwc->chan.device); |
| 1338 | struct dw_cyclic_desc *cdesc = dwc->cdesc; |
| 1339 | int i; |
Viresh Kumar | 69cea5a | 2011-04-15 16:03:35 +0530 | [diff] [blame] | 1340 | unsigned long flags; |
Hans-Christian Egtvedt | d9de451 | 2009-04-01 15:47:02 +0200 | [diff] [blame] | 1341 | |
Andy Shevchenko | 2e4c364 | 2012-06-19 13:34:05 +0300 | [diff] [blame^] | 1342 | dev_dbg(chan2dev(&dwc->chan), "%s\n", __func__); |
Hans-Christian Egtvedt | d9de451 | 2009-04-01 15:47:02 +0200 | [diff] [blame] | 1343 | |
| 1344 | if (!cdesc) |
| 1345 | return; |
| 1346 | |
Viresh Kumar | 69cea5a | 2011-04-15 16:03:35 +0530 | [diff] [blame] | 1347 | spin_lock_irqsave(&dwc->lock, flags); |
Hans-Christian Egtvedt | d9de451 | 2009-04-01 15:47:02 +0200 | [diff] [blame] | 1348 | |
| 1349 | channel_clear_bit(dw, CH_EN, dwc->mask); |
| 1350 | while (dma_readl(dw, CH_EN) & dwc->mask) |
| 1351 | cpu_relax(); |
| 1352 | |
Hans-Christian Egtvedt | d9de451 | 2009-04-01 15:47:02 +0200 | [diff] [blame] | 1353 | dma_writel(dw, CLEAR.ERROR, dwc->mask); |
| 1354 | dma_writel(dw, CLEAR.XFER, dwc->mask); |
| 1355 | |
Viresh Kumar | 69cea5a | 2011-04-15 16:03:35 +0530 | [diff] [blame] | 1356 | spin_unlock_irqrestore(&dwc->lock, flags); |
Hans-Christian Egtvedt | d9de451 | 2009-04-01 15:47:02 +0200 | [diff] [blame] | 1357 | |
| 1358 | for (i = 0; i < cdesc->periods; i++) |
| 1359 | dwc_desc_put(dwc, cdesc->desc[i]); |
| 1360 | |
| 1361 | kfree(cdesc->desc); |
| 1362 | kfree(cdesc); |
| 1363 | |
| 1364 | clear_bit(DW_DMA_IS_CYCLIC, &dwc->flags); |
| 1365 | } |
| 1366 | EXPORT_SYMBOL(dw_dma_cyclic_free); |
| 1367 | |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 1368 | /*----------------------------------------------------------------------*/ |
| 1369 | |
| 1370 | static void dw_dma_off(struct dw_dma *dw) |
| 1371 | { |
Viresh Kumar | 61e183f | 2011-11-17 16:01:29 +0530 | [diff] [blame] | 1372 | int i; |
| 1373 | |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 1374 | dma_writel(dw, CFG, 0); |
| 1375 | |
| 1376 | channel_clear_bit(dw, MASK.XFER, dw->all_chan_mask); |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 1377 | channel_clear_bit(dw, MASK.SRC_TRAN, dw->all_chan_mask); |
| 1378 | channel_clear_bit(dw, MASK.DST_TRAN, dw->all_chan_mask); |
| 1379 | channel_clear_bit(dw, MASK.ERROR, dw->all_chan_mask); |
| 1380 | |
| 1381 | while (dma_readl(dw, CFG) & DW_CFG_DMA_EN) |
| 1382 | cpu_relax(); |
Viresh Kumar | 61e183f | 2011-11-17 16:01:29 +0530 | [diff] [blame] | 1383 | |
| 1384 | for (i = 0; i < dw->dma.chancnt; i++) |
| 1385 | dw->chan[i].initialized = false; |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 1386 | } |
| 1387 | |
| 1388 | static int __init dw_probe(struct platform_device *pdev) |
| 1389 | { |
| 1390 | struct dw_dma_platform_data *pdata; |
| 1391 | struct resource *io; |
| 1392 | struct dw_dma *dw; |
| 1393 | size_t size; |
| 1394 | int irq; |
| 1395 | int err; |
| 1396 | int i; |
| 1397 | |
Viresh Kumar | 6c618c9 | 2012-02-01 16:12:22 +0530 | [diff] [blame] | 1398 | pdata = dev_get_platdata(&pdev->dev); |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 1399 | if (!pdata || pdata->nr_channels > DW_DMA_MAX_NR_CHANNELS) |
| 1400 | return -EINVAL; |
| 1401 | |
| 1402 | io = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 1403 | if (!io) |
| 1404 | return -EINVAL; |
| 1405 | |
| 1406 | irq = platform_get_irq(pdev, 0); |
| 1407 | if (irq < 0) |
| 1408 | return irq; |
| 1409 | |
| 1410 | size = sizeof(struct dw_dma); |
| 1411 | size += pdata->nr_channels * sizeof(struct dw_dma_chan); |
| 1412 | dw = kzalloc(size, GFP_KERNEL); |
| 1413 | if (!dw) |
| 1414 | return -ENOMEM; |
| 1415 | |
| 1416 | if (!request_mem_region(io->start, DW_REGLEN, pdev->dev.driver->name)) { |
| 1417 | err = -EBUSY; |
| 1418 | goto err_kfree; |
| 1419 | } |
| 1420 | |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 1421 | dw->regs = ioremap(io->start, DW_REGLEN); |
| 1422 | if (!dw->regs) { |
| 1423 | err = -ENOMEM; |
| 1424 | goto err_release_r; |
| 1425 | } |
| 1426 | |
| 1427 | dw->clk = clk_get(&pdev->dev, "hclk"); |
| 1428 | if (IS_ERR(dw->clk)) { |
| 1429 | err = PTR_ERR(dw->clk); |
| 1430 | goto err_clk; |
| 1431 | } |
Viresh Kumar | 3075528 | 2012-04-17 17:10:07 +0530 | [diff] [blame] | 1432 | clk_prepare_enable(dw->clk); |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 1433 | |
| 1434 | /* force dma off, just in case */ |
| 1435 | dw_dma_off(dw); |
| 1436 | |
| 1437 | err = request_irq(irq, dw_dma_interrupt, 0, "dw_dmac", dw); |
| 1438 | if (err) |
| 1439 | goto err_irq; |
| 1440 | |
| 1441 | platform_set_drvdata(pdev, dw); |
| 1442 | |
| 1443 | tasklet_init(&dw->tasklet, dw_dma_tasklet, (unsigned long)dw); |
| 1444 | |
| 1445 | dw->all_chan_mask = (1 << pdata->nr_channels) - 1; |
| 1446 | |
| 1447 | INIT_LIST_HEAD(&dw->dma.channels); |
Barry Song | 46389470 | 2011-09-15 03:06:30 -0700 | [diff] [blame] | 1448 | for (i = 0; i < pdata->nr_channels; i++) { |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 1449 | struct dw_dma_chan *dwc = &dw->chan[i]; |
| 1450 | |
| 1451 | dwc->chan.device = &dw->dma; |
Russell King - ARM Linux | d3ee98cdc | 2012-03-06 22:35:47 +0000 | [diff] [blame] | 1452 | dma_cookie_init(&dwc->chan); |
Viresh Kumar | b0c3130 | 2011-03-03 15:47:21 +0530 | [diff] [blame] | 1453 | if (pdata->chan_allocation_order == CHAN_ALLOCATION_ASCENDING) |
| 1454 | list_add_tail(&dwc->chan.device_node, |
| 1455 | &dw->dma.channels); |
| 1456 | else |
| 1457 | list_add(&dwc->chan.device_node, &dw->dma.channels); |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 1458 | |
Viresh Kumar | 93317e8 | 2011-03-03 15:47:22 +0530 | [diff] [blame] | 1459 | /* 7 is highest priority & 0 is lowest. */ |
| 1460 | if (pdata->chan_priority == CHAN_PRIORITY_ASCENDING) |
Viresh Kumar | e8d9f87 | 2012-02-01 16:12:21 +0530 | [diff] [blame] | 1461 | dwc->priority = pdata->nr_channels - i - 1; |
Viresh Kumar | 93317e8 | 2011-03-03 15:47:22 +0530 | [diff] [blame] | 1462 | else |
| 1463 | dwc->priority = i; |
| 1464 | |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 1465 | dwc->ch_regs = &__dw_regs(dw)->CHAN[i]; |
| 1466 | spin_lock_init(&dwc->lock); |
| 1467 | dwc->mask = 1 << i; |
| 1468 | |
| 1469 | INIT_LIST_HEAD(&dwc->active_list); |
| 1470 | INIT_LIST_HEAD(&dwc->queue); |
| 1471 | INIT_LIST_HEAD(&dwc->free_list); |
| 1472 | |
| 1473 | channel_clear_bit(dw, CH_EN, dwc->mask); |
| 1474 | } |
| 1475 | |
| 1476 | /* Clear/disable all interrupts on all channels. */ |
| 1477 | dma_writel(dw, CLEAR.XFER, dw->all_chan_mask); |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 1478 | dma_writel(dw, CLEAR.SRC_TRAN, dw->all_chan_mask); |
| 1479 | dma_writel(dw, CLEAR.DST_TRAN, dw->all_chan_mask); |
| 1480 | dma_writel(dw, CLEAR.ERROR, dw->all_chan_mask); |
| 1481 | |
| 1482 | channel_clear_bit(dw, MASK.XFER, dw->all_chan_mask); |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 1483 | channel_clear_bit(dw, MASK.SRC_TRAN, dw->all_chan_mask); |
| 1484 | channel_clear_bit(dw, MASK.DST_TRAN, dw->all_chan_mask); |
| 1485 | channel_clear_bit(dw, MASK.ERROR, dw->all_chan_mask); |
| 1486 | |
| 1487 | dma_cap_set(DMA_MEMCPY, dw->dma.cap_mask); |
| 1488 | dma_cap_set(DMA_SLAVE, dw->dma.cap_mask); |
Jamie Iles | 95ea759 | 2011-01-21 14:11:54 +0000 | [diff] [blame] | 1489 | if (pdata->is_private) |
| 1490 | dma_cap_set(DMA_PRIVATE, dw->dma.cap_mask); |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 1491 | dw->dma.dev = &pdev->dev; |
| 1492 | dw->dma.device_alloc_chan_resources = dwc_alloc_chan_resources; |
| 1493 | dw->dma.device_free_chan_resources = dwc_free_chan_resources; |
| 1494 | |
| 1495 | dw->dma.device_prep_dma_memcpy = dwc_prep_dma_memcpy; |
| 1496 | |
| 1497 | dw->dma.device_prep_slave_sg = dwc_prep_slave_sg; |
Linus Walleij | c3635c7 | 2010-03-26 16:44:01 -0700 | [diff] [blame] | 1498 | dw->dma.device_control = dwc_control; |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 1499 | |
Linus Walleij | 0793448 | 2010-03-26 16:50:49 -0700 | [diff] [blame] | 1500 | dw->dma.device_tx_status = dwc_tx_status; |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 1501 | dw->dma.device_issue_pending = dwc_issue_pending; |
| 1502 | |
| 1503 | dma_writel(dw, CFG, DW_CFG_DMA_EN); |
| 1504 | |
| 1505 | printk(KERN_INFO "%s: DesignWare DMA Controller, %d channels\n", |
Barry Song | 46389470 | 2011-09-15 03:06:30 -0700 | [diff] [blame] | 1506 | dev_name(&pdev->dev), pdata->nr_channels); |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 1507 | |
| 1508 | dma_async_device_register(&dw->dma); |
| 1509 | |
| 1510 | return 0; |
| 1511 | |
| 1512 | err_irq: |
Viresh Kumar | 3075528 | 2012-04-17 17:10:07 +0530 | [diff] [blame] | 1513 | clk_disable_unprepare(dw->clk); |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 1514 | clk_put(dw->clk); |
| 1515 | err_clk: |
| 1516 | iounmap(dw->regs); |
| 1517 | dw->regs = NULL; |
| 1518 | err_release_r: |
| 1519 | release_resource(io); |
| 1520 | err_kfree: |
| 1521 | kfree(dw); |
| 1522 | return err; |
| 1523 | } |
| 1524 | |
| 1525 | static int __exit dw_remove(struct platform_device *pdev) |
| 1526 | { |
| 1527 | struct dw_dma *dw = platform_get_drvdata(pdev); |
| 1528 | struct dw_dma_chan *dwc, *_dwc; |
| 1529 | struct resource *io; |
| 1530 | |
| 1531 | dw_dma_off(dw); |
| 1532 | dma_async_device_unregister(&dw->dma); |
| 1533 | |
| 1534 | free_irq(platform_get_irq(pdev, 0), dw); |
| 1535 | tasklet_kill(&dw->tasklet); |
| 1536 | |
| 1537 | list_for_each_entry_safe(dwc, _dwc, &dw->dma.channels, |
| 1538 | chan.device_node) { |
| 1539 | list_del(&dwc->chan.device_node); |
| 1540 | channel_clear_bit(dw, CH_EN, dwc->mask); |
| 1541 | } |
| 1542 | |
Viresh Kumar | 3075528 | 2012-04-17 17:10:07 +0530 | [diff] [blame] | 1543 | clk_disable_unprepare(dw->clk); |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 1544 | clk_put(dw->clk); |
| 1545 | |
| 1546 | iounmap(dw->regs); |
| 1547 | dw->regs = NULL; |
| 1548 | |
| 1549 | io = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 1550 | release_mem_region(io->start, DW_REGLEN); |
| 1551 | |
| 1552 | kfree(dw); |
| 1553 | |
| 1554 | return 0; |
| 1555 | } |
| 1556 | |
| 1557 | static void dw_shutdown(struct platform_device *pdev) |
| 1558 | { |
| 1559 | struct dw_dma *dw = platform_get_drvdata(pdev); |
| 1560 | |
| 1561 | dw_dma_off(platform_get_drvdata(pdev)); |
Viresh Kumar | 3075528 | 2012-04-17 17:10:07 +0530 | [diff] [blame] | 1562 | clk_disable_unprepare(dw->clk); |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 1563 | } |
| 1564 | |
Magnus Damm | 4a256b5 | 2009-07-08 13:22:18 +0200 | [diff] [blame] | 1565 | static int dw_suspend_noirq(struct device *dev) |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 1566 | { |
Magnus Damm | 4a256b5 | 2009-07-08 13:22:18 +0200 | [diff] [blame] | 1567 | struct platform_device *pdev = to_platform_device(dev); |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 1568 | struct dw_dma *dw = platform_get_drvdata(pdev); |
| 1569 | |
| 1570 | dw_dma_off(platform_get_drvdata(pdev)); |
Viresh Kumar | 3075528 | 2012-04-17 17:10:07 +0530 | [diff] [blame] | 1571 | clk_disable_unprepare(dw->clk); |
Viresh Kumar | 61e183f | 2011-11-17 16:01:29 +0530 | [diff] [blame] | 1572 | |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 1573 | return 0; |
| 1574 | } |
| 1575 | |
Magnus Damm | 4a256b5 | 2009-07-08 13:22:18 +0200 | [diff] [blame] | 1576 | static int dw_resume_noirq(struct device *dev) |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 1577 | { |
Magnus Damm | 4a256b5 | 2009-07-08 13:22:18 +0200 | [diff] [blame] | 1578 | struct platform_device *pdev = to_platform_device(dev); |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 1579 | struct dw_dma *dw = platform_get_drvdata(pdev); |
| 1580 | |
Viresh Kumar | 3075528 | 2012-04-17 17:10:07 +0530 | [diff] [blame] | 1581 | clk_prepare_enable(dw->clk); |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 1582 | dma_writel(dw, CFG, DW_CFG_DMA_EN); |
| 1583 | return 0; |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 1584 | } |
| 1585 | |
Alexey Dobriyan | 4714521 | 2009-12-14 18:00:08 -0800 | [diff] [blame] | 1586 | static const struct dev_pm_ops dw_dev_pm_ops = { |
Magnus Damm | 4a256b5 | 2009-07-08 13:22:18 +0200 | [diff] [blame] | 1587 | .suspend_noirq = dw_suspend_noirq, |
| 1588 | .resume_noirq = dw_resume_noirq, |
Rajeev KUMAR | 7414a1b | 2012-02-01 16:12:17 +0530 | [diff] [blame] | 1589 | .freeze_noirq = dw_suspend_noirq, |
| 1590 | .thaw_noirq = dw_resume_noirq, |
| 1591 | .restore_noirq = dw_resume_noirq, |
| 1592 | .poweroff_noirq = dw_suspend_noirq, |
Magnus Damm | 4a256b5 | 2009-07-08 13:22:18 +0200 | [diff] [blame] | 1593 | }; |
| 1594 | |
Viresh Kumar | d3f797d | 2012-04-20 20:15:34 +0530 | [diff] [blame] | 1595 | #ifdef CONFIG_OF |
| 1596 | static const struct of_device_id dw_dma_id_table[] = { |
| 1597 | { .compatible = "snps,dma-spear1340" }, |
| 1598 | {} |
| 1599 | }; |
| 1600 | MODULE_DEVICE_TABLE(of, dw_dma_id_table); |
| 1601 | #endif |
| 1602 | |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 1603 | static struct platform_driver dw_driver = { |
| 1604 | .remove = __exit_p(dw_remove), |
| 1605 | .shutdown = dw_shutdown, |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 1606 | .driver = { |
| 1607 | .name = "dw_dmac", |
Magnus Damm | 4a256b5 | 2009-07-08 13:22:18 +0200 | [diff] [blame] | 1608 | .pm = &dw_dev_pm_ops, |
Viresh Kumar | d3f797d | 2012-04-20 20:15:34 +0530 | [diff] [blame] | 1609 | .of_match_table = of_match_ptr(dw_dma_id_table), |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 1610 | }, |
| 1611 | }; |
| 1612 | |
| 1613 | static int __init dw_init(void) |
| 1614 | { |
| 1615 | return platform_driver_probe(&dw_driver, dw_probe); |
| 1616 | } |
Viresh Kumar | cb689a7 | 2011-03-03 15:47:15 +0530 | [diff] [blame] | 1617 | subsys_initcall(dw_init); |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 1618 | |
| 1619 | static void __exit dw_exit(void) |
| 1620 | { |
| 1621 | platform_driver_unregister(&dw_driver); |
| 1622 | } |
| 1623 | module_exit(dw_exit); |
| 1624 | |
| 1625 | MODULE_LICENSE("GPL v2"); |
| 1626 | MODULE_DESCRIPTION("Synopsys DesignWare DMA Controller driver"); |
Jean Delvare | e05503e | 2011-05-18 16:49:24 +0200 | [diff] [blame] | 1627 | MODULE_AUTHOR("Haavard Skinnemoen (Atmel)"); |
Viresh Kumar | aecb7b6 | 2011-05-24 14:04:09 +0530 | [diff] [blame] | 1628 | MODULE_AUTHOR("Viresh Kumar <viresh.kumar@st.com>"); |