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