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