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