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