Nicolas Ferre | dc78baa | 2009-07-03 19:24:33 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Driver for the Atmel AHB DMA Controller (aka HDMA or DMAC on AT91 systems) |
| 3 | * |
| 4 | * Copyright (C) 2008 Atmel Corporation |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * |
Nicolas Ferre | 9102d87 | 2012-06-12 10:44:55 +0200 | [diff] [blame] | 12 | * This supports the Atmel AHB DMA Controller found in several Atmel SoCs. |
| 13 | * The only Atmel DMA Controller that is not covered by this driver is the one |
| 14 | * found on AT91SAM9263. |
Nicolas Ferre | dc78baa | 2009-07-03 19:24:33 +0200 | [diff] [blame] | 15 | */ |
| 16 | |
Ludovic Desroches | 62971b2 | 2013-06-13 10:39:39 +0200 | [diff] [blame^] | 17 | #include <dt-bindings/dma/at91.h> |
Nicolas Ferre | dc78baa | 2009-07-03 19:24:33 +0200 | [diff] [blame] | 18 | #include <linux/clk.h> |
| 19 | #include <linux/dmaengine.h> |
| 20 | #include <linux/dma-mapping.h> |
| 21 | #include <linux/dmapool.h> |
| 22 | #include <linux/interrupt.h> |
| 23 | #include <linux/module.h> |
| 24 | #include <linux/platform_device.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 25 | #include <linux/slab.h> |
Nicolas Ferre | c511595 | 2011-10-17 14:56:41 +0200 | [diff] [blame] | 26 | #include <linux/of.h> |
| 27 | #include <linux/of_device.h> |
Ludovic Desroches | bbe89c8 | 2013-04-19 09:11:18 +0000 | [diff] [blame] | 28 | #include <linux/of_dma.h> |
Nicolas Ferre | dc78baa | 2009-07-03 19:24:33 +0200 | [diff] [blame] | 29 | |
| 30 | #include "at_hdmac_regs.h" |
Russell King - ARM Linux | d2ebfb3 | 2012-03-06 22:34:26 +0000 | [diff] [blame] | 31 | #include "dmaengine.h" |
Nicolas Ferre | dc78baa | 2009-07-03 19:24:33 +0200 | [diff] [blame] | 32 | |
| 33 | /* |
| 34 | * Glossary |
| 35 | * -------- |
| 36 | * |
| 37 | * at_hdmac : Name of the ATmel AHB DMA Controller |
| 38 | * at_dma_ / atdma : ATmel DMA controller entity related |
| 39 | * atc_ / atchan : ATmel DMA Channel entity related |
| 40 | */ |
| 41 | |
| 42 | #define ATC_DEFAULT_CFG (ATC_FIFOCFG_HALFFIFO) |
Nicolas Ferre | ae14d4b | 2011-04-30 16:57:49 +0200 | [diff] [blame] | 43 | #define ATC_DEFAULT_CTRLB (ATC_SIF(AT_DMA_MEM_IF) \ |
| 44 | |ATC_DIF(AT_DMA_MEM_IF)) |
Nicolas Ferre | dc78baa | 2009-07-03 19:24:33 +0200 | [diff] [blame] | 45 | |
| 46 | /* |
| 47 | * Initial number of descriptors to allocate for each channel. This could |
| 48 | * be increased during dma usage. |
| 49 | */ |
| 50 | static unsigned int init_nr_desc_per_channel = 64; |
| 51 | module_param(init_nr_desc_per_channel, uint, 0644); |
| 52 | MODULE_PARM_DESC(init_nr_desc_per_channel, |
| 53 | "initial descriptors per channel (default: 64)"); |
| 54 | |
| 55 | |
| 56 | /* prototypes */ |
| 57 | static dma_cookie_t atc_tx_submit(struct dma_async_tx_descriptor *tx); |
Elen Song | d48de6f | 2013-05-10 11:01:46 +0800 | [diff] [blame] | 58 | static void atc_issue_pending(struct dma_chan *chan); |
Nicolas Ferre | dc78baa | 2009-07-03 19:24:33 +0200 | [diff] [blame] | 59 | |
| 60 | |
| 61 | /*----------------------------------------------------------------------*/ |
| 62 | |
| 63 | static struct at_desc *atc_first_active(struct at_dma_chan *atchan) |
| 64 | { |
| 65 | return list_first_entry(&atchan->active_list, |
| 66 | struct at_desc, desc_node); |
| 67 | } |
| 68 | |
| 69 | static struct at_desc *atc_first_queued(struct at_dma_chan *atchan) |
| 70 | { |
| 71 | return list_first_entry(&atchan->queue, |
| 72 | struct at_desc, desc_node); |
| 73 | } |
| 74 | |
| 75 | /** |
Uwe Kleine-König | 421f91d | 2010-06-11 12:17:00 +0200 | [diff] [blame] | 76 | * atc_alloc_descriptor - allocate and return an initialized descriptor |
Nicolas Ferre | dc78baa | 2009-07-03 19:24:33 +0200 | [diff] [blame] | 77 | * @chan: the channel to allocate descriptors for |
| 78 | * @gfp_flags: GFP allocation flags |
| 79 | * |
| 80 | * Note: The ack-bit is positioned in the descriptor flag at creation time |
| 81 | * to make initial allocation more convenient. This bit will be cleared |
| 82 | * and control will be given to client at usage time (during |
| 83 | * preparation functions). |
| 84 | */ |
| 85 | static struct at_desc *atc_alloc_descriptor(struct dma_chan *chan, |
| 86 | gfp_t gfp_flags) |
| 87 | { |
| 88 | struct at_desc *desc = NULL; |
| 89 | struct at_dma *atdma = to_at_dma(chan->device); |
| 90 | dma_addr_t phys; |
| 91 | |
| 92 | desc = dma_pool_alloc(atdma->dma_desc_pool, gfp_flags, &phys); |
| 93 | if (desc) { |
| 94 | memset(desc, 0, sizeof(struct at_desc)); |
Dan Williams | 285a3c7 | 2009-09-08 17:53:03 -0700 | [diff] [blame] | 95 | INIT_LIST_HEAD(&desc->tx_list); |
Nicolas Ferre | dc78baa | 2009-07-03 19:24:33 +0200 | [diff] [blame] | 96 | dma_async_tx_descriptor_init(&desc->txd, chan); |
| 97 | /* txd.flags will be overwritten in prep functions */ |
| 98 | desc->txd.flags = DMA_CTRL_ACK; |
| 99 | desc->txd.tx_submit = atc_tx_submit; |
| 100 | desc->txd.phys = phys; |
| 101 | } |
| 102 | |
| 103 | return desc; |
| 104 | } |
| 105 | |
| 106 | /** |
André Goddard Rosa | af901ca | 2009-11-14 13:09:05 -0200 | [diff] [blame] | 107 | * atc_desc_get - get an unused descriptor from free_list |
Nicolas Ferre | dc78baa | 2009-07-03 19:24:33 +0200 | [diff] [blame] | 108 | * @atchan: channel we want a new descriptor for |
| 109 | */ |
| 110 | static struct at_desc *atc_desc_get(struct at_dma_chan *atchan) |
| 111 | { |
| 112 | struct at_desc *desc, *_desc; |
| 113 | struct at_desc *ret = NULL; |
Nicolas Ferre | d8cb04b | 2011-07-27 12:21:28 +0000 | [diff] [blame] | 114 | unsigned long flags; |
Nicolas Ferre | dc78baa | 2009-07-03 19:24:33 +0200 | [diff] [blame] | 115 | unsigned int i = 0; |
| 116 | LIST_HEAD(tmp_list); |
| 117 | |
Nicolas Ferre | d8cb04b | 2011-07-27 12:21:28 +0000 | [diff] [blame] | 118 | spin_lock_irqsave(&atchan->lock, flags); |
Nicolas Ferre | dc78baa | 2009-07-03 19:24:33 +0200 | [diff] [blame] | 119 | list_for_each_entry_safe(desc, _desc, &atchan->free_list, desc_node) { |
| 120 | i++; |
| 121 | if (async_tx_test_ack(&desc->txd)) { |
| 122 | list_del(&desc->desc_node); |
| 123 | ret = desc; |
| 124 | break; |
| 125 | } |
| 126 | dev_dbg(chan2dev(&atchan->chan_common), |
| 127 | "desc %p not ACKed\n", desc); |
| 128 | } |
Nicolas Ferre | d8cb04b | 2011-07-27 12:21:28 +0000 | [diff] [blame] | 129 | spin_unlock_irqrestore(&atchan->lock, flags); |
Nicolas Ferre | dc78baa | 2009-07-03 19:24:33 +0200 | [diff] [blame] | 130 | dev_vdbg(chan2dev(&atchan->chan_common), |
| 131 | "scanned %u descriptors on freelist\n", i); |
| 132 | |
| 133 | /* no more descriptor available in initial pool: create one more */ |
| 134 | if (!ret) { |
| 135 | ret = atc_alloc_descriptor(&atchan->chan_common, GFP_ATOMIC); |
| 136 | if (ret) { |
Nicolas Ferre | d8cb04b | 2011-07-27 12:21:28 +0000 | [diff] [blame] | 137 | spin_lock_irqsave(&atchan->lock, flags); |
Nicolas Ferre | dc78baa | 2009-07-03 19:24:33 +0200 | [diff] [blame] | 138 | atchan->descs_allocated++; |
Nicolas Ferre | d8cb04b | 2011-07-27 12:21:28 +0000 | [diff] [blame] | 139 | spin_unlock_irqrestore(&atchan->lock, flags); |
Nicolas Ferre | dc78baa | 2009-07-03 19:24:33 +0200 | [diff] [blame] | 140 | } else { |
| 141 | dev_err(chan2dev(&atchan->chan_common), |
| 142 | "not enough descriptors available\n"); |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | return ret; |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * atc_desc_put - move a descriptor, including any children, to the free list |
| 151 | * @atchan: channel we work on |
| 152 | * @desc: descriptor, at the head of a chain, to move to free list |
| 153 | */ |
| 154 | static void atc_desc_put(struct at_dma_chan *atchan, struct at_desc *desc) |
| 155 | { |
| 156 | if (desc) { |
| 157 | struct at_desc *child; |
Nicolas Ferre | d8cb04b | 2011-07-27 12:21:28 +0000 | [diff] [blame] | 158 | unsigned long flags; |
Nicolas Ferre | dc78baa | 2009-07-03 19:24:33 +0200 | [diff] [blame] | 159 | |
Nicolas Ferre | d8cb04b | 2011-07-27 12:21:28 +0000 | [diff] [blame] | 160 | spin_lock_irqsave(&atchan->lock, flags); |
Dan Williams | 285a3c7 | 2009-09-08 17:53:03 -0700 | [diff] [blame] | 161 | list_for_each_entry(child, &desc->tx_list, desc_node) |
Nicolas Ferre | dc78baa | 2009-07-03 19:24:33 +0200 | [diff] [blame] | 162 | dev_vdbg(chan2dev(&atchan->chan_common), |
| 163 | "moving child desc %p to freelist\n", |
| 164 | child); |
Dan Williams | 285a3c7 | 2009-09-08 17:53:03 -0700 | [diff] [blame] | 165 | list_splice_init(&desc->tx_list, &atchan->free_list); |
Nicolas Ferre | dc78baa | 2009-07-03 19:24:33 +0200 | [diff] [blame] | 166 | dev_vdbg(chan2dev(&atchan->chan_common), |
| 167 | "moving desc %p to freelist\n", desc); |
| 168 | list_add(&desc->desc_node, &atchan->free_list); |
Nicolas Ferre | d8cb04b | 2011-07-27 12:21:28 +0000 | [diff] [blame] | 169 | spin_unlock_irqrestore(&atchan->lock, flags); |
Nicolas Ferre | dc78baa | 2009-07-03 19:24:33 +0200 | [diff] [blame] | 170 | } |
| 171 | } |
| 172 | |
| 173 | /** |
Masanari Iida | d73111c | 2012-08-04 23:37:53 +0900 | [diff] [blame] | 174 | * atc_desc_chain - build chain adding a descriptor |
| 175 | * @first: address of first descriptor of the chain |
| 176 | * @prev: address of previous descriptor of the chain |
Nicolas Ferre | 53830cc | 2011-04-30 16:57:46 +0200 | [diff] [blame] | 177 | * @desc: descriptor to queue |
| 178 | * |
| 179 | * Called from prep_* functions |
| 180 | */ |
| 181 | static void atc_desc_chain(struct at_desc **first, struct at_desc **prev, |
| 182 | struct at_desc *desc) |
| 183 | { |
| 184 | if (!(*first)) { |
| 185 | *first = desc; |
| 186 | } else { |
| 187 | /* inform the HW lli about chaining */ |
| 188 | (*prev)->lli.dscr = desc->txd.phys; |
| 189 | /* insert the link descriptor to the LD ring */ |
| 190 | list_add_tail(&desc->desc_node, |
| 191 | &(*first)->tx_list); |
| 192 | } |
| 193 | *prev = desc; |
| 194 | } |
| 195 | |
| 196 | /** |
Nicolas Ferre | dc78baa | 2009-07-03 19:24:33 +0200 | [diff] [blame] | 197 | * atc_dostart - starts the DMA engine for real |
| 198 | * @atchan: the channel we want to start |
| 199 | * @first: first descriptor in the list we want to begin with |
| 200 | * |
| 201 | * Called with atchan->lock held and bh disabled |
| 202 | */ |
| 203 | static void atc_dostart(struct at_dma_chan *atchan, struct at_desc *first) |
| 204 | { |
| 205 | struct at_dma *atdma = to_at_dma(atchan->chan_common.device); |
| 206 | |
| 207 | /* ASSERT: channel is idle */ |
| 208 | if (atc_chan_is_enabled(atchan)) { |
| 209 | dev_err(chan2dev(&atchan->chan_common), |
| 210 | "BUG: Attempted to start non-idle channel\n"); |
| 211 | dev_err(chan2dev(&atchan->chan_common), |
| 212 | " channel: s0x%x d0x%x ctrl0x%x:0x%x l0x%x\n", |
| 213 | channel_readl(atchan, SADDR), |
| 214 | channel_readl(atchan, DADDR), |
| 215 | channel_readl(atchan, CTRLA), |
| 216 | channel_readl(atchan, CTRLB), |
| 217 | channel_readl(atchan, DSCR)); |
| 218 | |
| 219 | /* The tasklet will hopefully advance the queue... */ |
| 220 | return; |
| 221 | } |
| 222 | |
| 223 | vdbg_dump_regs(atchan); |
| 224 | |
Nicolas Ferre | dc78baa | 2009-07-03 19:24:33 +0200 | [diff] [blame] | 225 | channel_writel(atchan, SADDR, 0); |
| 226 | channel_writel(atchan, DADDR, 0); |
| 227 | channel_writel(atchan, CTRLA, 0); |
| 228 | channel_writel(atchan, CTRLB, 0); |
| 229 | channel_writel(atchan, DSCR, first->txd.phys); |
| 230 | dma_writel(atdma, CHER, atchan->mask); |
| 231 | |
| 232 | vdbg_dump_regs(atchan); |
| 233 | } |
| 234 | |
Elen Song | d48de6f | 2013-05-10 11:01:46 +0800 | [diff] [blame] | 235 | /* |
| 236 | * atc_get_current_descriptors - |
| 237 | * locate the descriptor which equal to physical address in DSCR |
| 238 | * @atchan: the channel we want to start |
| 239 | * @dscr_addr: physical descriptor address in DSCR |
| 240 | */ |
| 241 | static struct at_desc *atc_get_current_descriptors(struct at_dma_chan *atchan, |
| 242 | u32 dscr_addr) |
| 243 | { |
| 244 | struct at_desc *desc, *_desc, *child, *desc_cur = NULL; |
| 245 | |
| 246 | list_for_each_entry_safe(desc, _desc, &atchan->active_list, desc_node) { |
| 247 | if (desc->lli.dscr == dscr_addr) { |
| 248 | desc_cur = desc; |
| 249 | break; |
| 250 | } |
| 251 | |
| 252 | list_for_each_entry(child, &desc->tx_list, desc_node) { |
| 253 | if (child->lli.dscr == dscr_addr) { |
| 254 | desc_cur = child; |
| 255 | break; |
| 256 | } |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | return desc_cur; |
| 261 | } |
| 262 | |
| 263 | /* |
| 264 | * atc_get_bytes_left - |
| 265 | * Get the number of bytes residue in dma buffer, |
| 266 | * @chan: the channel we want to start |
| 267 | */ |
| 268 | static int atc_get_bytes_left(struct dma_chan *chan) |
| 269 | { |
| 270 | struct at_dma_chan *atchan = to_at_dma_chan(chan); |
| 271 | struct at_dma *atdma = to_at_dma(chan->device); |
| 272 | int chan_id = atchan->chan_common.chan_id; |
| 273 | struct at_desc *desc_first = atc_first_active(atchan); |
| 274 | struct at_desc *desc_cur; |
| 275 | int ret = 0, count = 0; |
| 276 | |
| 277 | /* |
| 278 | * Initialize necessary values in the first time. |
| 279 | * remain_desc record remain desc length. |
| 280 | */ |
| 281 | if (atchan->remain_desc == 0) |
| 282 | /* First descriptor embedds the transaction length */ |
| 283 | atchan->remain_desc = desc_first->len; |
| 284 | |
| 285 | /* |
| 286 | * This happens when current descriptor transfer complete. |
| 287 | * The residual buffer size should reduce current descriptor length. |
| 288 | */ |
| 289 | if (unlikely(test_bit(ATC_IS_BTC, &atchan->status))) { |
| 290 | clear_bit(ATC_IS_BTC, &atchan->status); |
| 291 | desc_cur = atc_get_current_descriptors(atchan, |
| 292 | channel_readl(atchan, DSCR)); |
| 293 | if (!desc_cur) { |
| 294 | ret = -EINVAL; |
| 295 | goto out; |
| 296 | } |
| 297 | atchan->remain_desc -= (desc_cur->lli.ctrla & ATC_BTSIZE_MAX) |
| 298 | << (desc_first->tx_width); |
| 299 | if (atchan->remain_desc < 0) { |
| 300 | ret = -EINVAL; |
| 301 | goto out; |
| 302 | } else |
| 303 | ret = atchan->remain_desc; |
| 304 | } else { |
| 305 | /* |
| 306 | * Get residual bytes when current |
| 307 | * descriptor transfer in progress. |
| 308 | */ |
| 309 | count = (channel_readl(atchan, CTRLA) & ATC_BTSIZE_MAX) |
| 310 | << (desc_first->tx_width); |
| 311 | ret = atchan->remain_desc - count; |
| 312 | } |
| 313 | /* |
| 314 | * Check fifo empty. |
| 315 | */ |
| 316 | if (!(dma_readl(atdma, CHSR) & AT_DMA_EMPT(chan_id))) |
| 317 | atc_issue_pending(chan); |
| 318 | |
| 319 | out: |
| 320 | return ret; |
| 321 | } |
| 322 | |
Nicolas Ferre | dc78baa | 2009-07-03 19:24:33 +0200 | [diff] [blame] | 323 | /** |
| 324 | * atc_chain_complete - finish work for one transaction chain |
| 325 | * @atchan: channel we work on |
| 326 | * @desc: descriptor at the head of the chain we want do complete |
| 327 | * |
| 328 | * Called with atchan->lock held and bh disabled */ |
| 329 | static void |
| 330 | atc_chain_complete(struct at_dma_chan *atchan, struct at_desc *desc) |
| 331 | { |
Nicolas Ferre | dc78baa | 2009-07-03 19:24:33 +0200 | [diff] [blame] | 332 | struct dma_async_tx_descriptor *txd = &desc->txd; |
| 333 | |
| 334 | dev_vdbg(chan2dev(&atchan->chan_common), |
| 335 | "descriptor %u complete\n", txd->cookie); |
| 336 | |
Vinod Koul | d411605 | 2012-05-11 11:48:21 +0530 | [diff] [blame] | 337 | /* mark the descriptor as complete for non cyclic cases only */ |
| 338 | if (!atc_chan_is_cyclic(atchan)) |
| 339 | dma_cookie_complete(txd); |
Nicolas Ferre | dc78baa | 2009-07-03 19:24:33 +0200 | [diff] [blame] | 340 | |
| 341 | /* move children to free_list */ |
Dan Williams | 285a3c7 | 2009-09-08 17:53:03 -0700 | [diff] [blame] | 342 | list_splice_init(&desc->tx_list, &atchan->free_list); |
Nicolas Ferre | dc78baa | 2009-07-03 19:24:33 +0200 | [diff] [blame] | 343 | /* move myself to free_list */ |
| 344 | list_move(&desc->desc_node, &atchan->free_list); |
| 345 | |
Nicolas Ferre | ebcf9b8 | 2011-01-12 15:39:06 +0100 | [diff] [blame] | 346 | /* unmap dma addresses (not on slave channels) */ |
Atsushi Nemoto | 657a77f | 2009-09-08 17:53:05 -0700 | [diff] [blame] | 347 | if (!atchan->chan_common.private) { |
| 348 | struct device *parent = chan2parent(&atchan->chan_common); |
| 349 | if (!(txd->flags & DMA_COMPL_SKIP_DEST_UNMAP)) { |
| 350 | if (txd->flags & DMA_COMPL_DEST_UNMAP_SINGLE) |
| 351 | dma_unmap_single(parent, |
| 352 | desc->lli.daddr, |
| 353 | desc->len, DMA_FROM_DEVICE); |
| 354 | else |
| 355 | dma_unmap_page(parent, |
| 356 | desc->lli.daddr, |
| 357 | desc->len, DMA_FROM_DEVICE); |
| 358 | } |
| 359 | if (!(txd->flags & DMA_COMPL_SKIP_SRC_UNMAP)) { |
| 360 | if (txd->flags & DMA_COMPL_SRC_UNMAP_SINGLE) |
| 361 | dma_unmap_single(parent, |
| 362 | desc->lli.saddr, |
| 363 | desc->len, DMA_TO_DEVICE); |
| 364 | else |
| 365 | dma_unmap_page(parent, |
| 366 | desc->lli.saddr, |
| 367 | desc->len, DMA_TO_DEVICE); |
| 368 | } |
Nicolas Ferre | dc78baa | 2009-07-03 19:24:33 +0200 | [diff] [blame] | 369 | } |
| 370 | |
Nicolas Ferre | 53830cc | 2011-04-30 16:57:46 +0200 | [diff] [blame] | 371 | /* for cyclic transfers, |
| 372 | * no need to replay callback function while stopping */ |
Nicolas Ferre | 3c47748 | 2011-07-25 21:09:23 +0000 | [diff] [blame] | 373 | if (!atc_chan_is_cyclic(atchan)) { |
Nicolas Ferre | 53830cc | 2011-04-30 16:57:46 +0200 | [diff] [blame] | 374 | dma_async_tx_callback callback = txd->callback; |
| 375 | void *param = txd->callback_param; |
| 376 | |
| 377 | /* |
| 378 | * The API requires that no submissions are done from a |
| 379 | * callback, so we don't need to drop the lock here |
| 380 | */ |
| 381 | if (callback) |
| 382 | callback(param); |
| 383 | } |
Nicolas Ferre | dc78baa | 2009-07-03 19:24:33 +0200 | [diff] [blame] | 384 | |
| 385 | dma_run_dependencies(txd); |
| 386 | } |
| 387 | |
| 388 | /** |
| 389 | * atc_complete_all - finish work for all transactions |
| 390 | * @atchan: channel to complete transactions for |
| 391 | * |
| 392 | * Eventually submit queued descriptors if any |
| 393 | * |
| 394 | * Assume channel is idle while calling this function |
| 395 | * Called with atchan->lock held and bh disabled |
| 396 | */ |
| 397 | static void atc_complete_all(struct at_dma_chan *atchan) |
| 398 | { |
| 399 | struct at_desc *desc, *_desc; |
| 400 | LIST_HEAD(list); |
| 401 | |
| 402 | dev_vdbg(chan2dev(&atchan->chan_common), "complete all\n"); |
| 403 | |
Nicolas Ferre | dc78baa | 2009-07-03 19:24:33 +0200 | [diff] [blame] | 404 | /* |
| 405 | * Submit queued descriptors ASAP, i.e. before we go through |
| 406 | * the completed ones. |
| 407 | */ |
| 408 | if (!list_empty(&atchan->queue)) |
| 409 | atc_dostart(atchan, atc_first_queued(atchan)); |
| 410 | /* empty active_list now it is completed */ |
| 411 | list_splice_init(&atchan->active_list, &list); |
| 412 | /* empty queue list by moving descriptors (if any) to active_list */ |
| 413 | list_splice_init(&atchan->queue, &atchan->active_list); |
| 414 | |
| 415 | list_for_each_entry_safe(desc, _desc, &list, desc_node) |
| 416 | atc_chain_complete(atchan, desc); |
| 417 | } |
| 418 | |
| 419 | /** |
| 420 | * atc_cleanup_descriptors - cleanup up finished descriptors in active_list |
| 421 | * @atchan: channel to be cleaned up |
| 422 | * |
| 423 | * Called with atchan->lock held and bh disabled |
| 424 | */ |
| 425 | static void atc_cleanup_descriptors(struct at_dma_chan *atchan) |
| 426 | { |
| 427 | struct at_desc *desc, *_desc; |
| 428 | struct at_desc *child; |
| 429 | |
| 430 | dev_vdbg(chan2dev(&atchan->chan_common), "cleanup descriptors\n"); |
| 431 | |
| 432 | list_for_each_entry_safe(desc, _desc, &atchan->active_list, desc_node) { |
| 433 | if (!(desc->lli.ctrla & ATC_DONE)) |
| 434 | /* This one is currently in progress */ |
| 435 | return; |
| 436 | |
Dan Williams | 285a3c7 | 2009-09-08 17:53:03 -0700 | [diff] [blame] | 437 | list_for_each_entry(child, &desc->tx_list, desc_node) |
Nicolas Ferre | dc78baa | 2009-07-03 19:24:33 +0200 | [diff] [blame] | 438 | if (!(child->lli.ctrla & ATC_DONE)) |
| 439 | /* Currently in progress */ |
| 440 | return; |
| 441 | |
| 442 | /* |
| 443 | * No descriptors so far seem to be in progress, i.e. |
| 444 | * this chain must be done. |
| 445 | */ |
| 446 | atc_chain_complete(atchan, desc); |
| 447 | } |
| 448 | } |
| 449 | |
| 450 | /** |
| 451 | * atc_advance_work - at the end of a transaction, move forward |
| 452 | * @atchan: channel where the transaction ended |
| 453 | * |
| 454 | * Called with atchan->lock held and bh disabled |
| 455 | */ |
| 456 | static void atc_advance_work(struct at_dma_chan *atchan) |
| 457 | { |
| 458 | dev_vdbg(chan2dev(&atchan->chan_common), "advance_work\n"); |
| 459 | |
Ludovic Desroches | d202f05 | 2013-04-18 09:52:59 +0200 | [diff] [blame] | 460 | if (atc_chan_is_enabled(atchan)) |
| 461 | return; |
| 462 | |
Nicolas Ferre | dc78baa | 2009-07-03 19:24:33 +0200 | [diff] [blame] | 463 | if (list_empty(&atchan->active_list) || |
| 464 | list_is_singular(&atchan->active_list)) { |
| 465 | atc_complete_all(atchan); |
| 466 | } else { |
| 467 | atc_chain_complete(atchan, atc_first_active(atchan)); |
| 468 | /* advance work */ |
| 469 | atc_dostart(atchan, atc_first_active(atchan)); |
| 470 | } |
| 471 | } |
| 472 | |
| 473 | |
| 474 | /** |
| 475 | * atc_handle_error - handle errors reported by DMA controller |
| 476 | * @atchan: channel where error occurs |
| 477 | * |
| 478 | * Called with atchan->lock held and bh disabled |
| 479 | */ |
| 480 | static void atc_handle_error(struct at_dma_chan *atchan) |
| 481 | { |
| 482 | struct at_desc *bad_desc; |
| 483 | struct at_desc *child; |
| 484 | |
| 485 | /* |
| 486 | * The descriptor currently at the head of the active list is |
| 487 | * broked. Since we don't have any way to report errors, we'll |
| 488 | * just have to scream loudly and try to carry on. |
| 489 | */ |
| 490 | bad_desc = atc_first_active(atchan); |
| 491 | list_del_init(&bad_desc->desc_node); |
| 492 | |
| 493 | /* As we are stopped, take advantage to push queued descriptors |
| 494 | * in active_list */ |
| 495 | list_splice_init(&atchan->queue, atchan->active_list.prev); |
| 496 | |
| 497 | /* Try to restart the controller */ |
| 498 | if (!list_empty(&atchan->active_list)) |
| 499 | atc_dostart(atchan, atc_first_active(atchan)); |
| 500 | |
| 501 | /* |
| 502 | * KERN_CRITICAL may seem harsh, but since this only happens |
| 503 | * when someone submits a bad physical address in a |
| 504 | * descriptor, we should consider ourselves lucky that the |
| 505 | * controller flagged an error instead of scribbling over |
| 506 | * random memory locations. |
| 507 | */ |
| 508 | dev_crit(chan2dev(&atchan->chan_common), |
| 509 | "Bad descriptor submitted for DMA!\n"); |
| 510 | dev_crit(chan2dev(&atchan->chan_common), |
| 511 | " cookie: %d\n", bad_desc->txd.cookie); |
| 512 | atc_dump_lli(atchan, &bad_desc->lli); |
Dan Williams | 285a3c7 | 2009-09-08 17:53:03 -0700 | [diff] [blame] | 513 | list_for_each_entry(child, &bad_desc->tx_list, desc_node) |
Nicolas Ferre | dc78baa | 2009-07-03 19:24:33 +0200 | [diff] [blame] | 514 | atc_dump_lli(atchan, &child->lli); |
| 515 | |
| 516 | /* Pretend the descriptor completed successfully */ |
| 517 | atc_chain_complete(atchan, bad_desc); |
| 518 | } |
| 519 | |
Nicolas Ferre | 53830cc | 2011-04-30 16:57:46 +0200 | [diff] [blame] | 520 | /** |
| 521 | * atc_handle_cyclic - at the end of a period, run callback function |
| 522 | * @atchan: channel used for cyclic operations |
| 523 | * |
| 524 | * Called with atchan->lock held and bh disabled |
| 525 | */ |
| 526 | static void atc_handle_cyclic(struct at_dma_chan *atchan) |
| 527 | { |
| 528 | struct at_desc *first = atc_first_active(atchan); |
| 529 | struct dma_async_tx_descriptor *txd = &first->txd; |
| 530 | dma_async_tx_callback callback = txd->callback; |
| 531 | void *param = txd->callback_param; |
| 532 | |
| 533 | dev_vdbg(chan2dev(&atchan->chan_common), |
| 534 | "new cyclic period llp 0x%08x\n", |
| 535 | channel_readl(atchan, DSCR)); |
| 536 | |
| 537 | if (callback) |
| 538 | callback(param); |
| 539 | } |
Nicolas Ferre | dc78baa | 2009-07-03 19:24:33 +0200 | [diff] [blame] | 540 | |
| 541 | /*-- IRQ & Tasklet ---------------------------------------------------*/ |
| 542 | |
| 543 | static void atc_tasklet(unsigned long data) |
| 544 | { |
| 545 | struct at_dma_chan *atchan = (struct at_dma_chan *)data; |
Nicolas Ferre | d8cb04b | 2011-07-27 12:21:28 +0000 | [diff] [blame] | 546 | unsigned long flags; |
Nicolas Ferre | dc78baa | 2009-07-03 19:24:33 +0200 | [diff] [blame] | 547 | |
Nicolas Ferre | d8cb04b | 2011-07-27 12:21:28 +0000 | [diff] [blame] | 548 | spin_lock_irqsave(&atchan->lock, flags); |
Nicolas Ferre | 53830cc | 2011-04-30 16:57:46 +0200 | [diff] [blame] | 549 | if (test_and_clear_bit(ATC_IS_ERROR, &atchan->status)) |
Nicolas Ferre | dc78baa | 2009-07-03 19:24:33 +0200 | [diff] [blame] | 550 | atc_handle_error(atchan); |
Nicolas Ferre | 3c47748 | 2011-07-25 21:09:23 +0000 | [diff] [blame] | 551 | else if (atc_chan_is_cyclic(atchan)) |
Nicolas Ferre | 53830cc | 2011-04-30 16:57:46 +0200 | [diff] [blame] | 552 | atc_handle_cyclic(atchan); |
Nicolas Ferre | dc78baa | 2009-07-03 19:24:33 +0200 | [diff] [blame] | 553 | else |
| 554 | atc_advance_work(atchan); |
| 555 | |
Nicolas Ferre | d8cb04b | 2011-07-27 12:21:28 +0000 | [diff] [blame] | 556 | spin_unlock_irqrestore(&atchan->lock, flags); |
Nicolas Ferre | dc78baa | 2009-07-03 19:24:33 +0200 | [diff] [blame] | 557 | } |
| 558 | |
| 559 | static irqreturn_t at_dma_interrupt(int irq, void *dev_id) |
| 560 | { |
| 561 | struct at_dma *atdma = (struct at_dma *)dev_id; |
| 562 | struct at_dma_chan *atchan; |
| 563 | int i; |
| 564 | u32 status, pending, imr; |
| 565 | int ret = IRQ_NONE; |
| 566 | |
| 567 | do { |
| 568 | imr = dma_readl(atdma, EBCIMR); |
| 569 | status = dma_readl(atdma, EBCISR); |
| 570 | pending = status & imr; |
| 571 | |
| 572 | if (!pending) |
| 573 | break; |
| 574 | |
| 575 | dev_vdbg(atdma->dma_common.dev, |
| 576 | "interrupt: status = 0x%08x, 0x%08x, 0x%08x\n", |
| 577 | status, imr, pending); |
| 578 | |
| 579 | for (i = 0; i < atdma->dma_common.chancnt; i++) { |
| 580 | atchan = &atdma->chan[i]; |
Nicolas Ferre | 9b3aa58 | 2011-04-30 16:57:45 +0200 | [diff] [blame] | 581 | if (pending & (AT_DMA_BTC(i) | AT_DMA_ERR(i))) { |
Nicolas Ferre | dc78baa | 2009-07-03 19:24:33 +0200 | [diff] [blame] | 582 | if (pending & AT_DMA_ERR(i)) { |
| 583 | /* Disable channel on AHB error */ |
Nicolas Ferre | 23b5e3a | 2011-05-06 19:56:52 +0200 | [diff] [blame] | 584 | dma_writel(atdma, CHDR, |
| 585 | AT_DMA_RES(i) | atchan->mask); |
Nicolas Ferre | dc78baa | 2009-07-03 19:24:33 +0200 | [diff] [blame] | 586 | /* Give information to tasklet */ |
Nicolas Ferre | 53830cc | 2011-04-30 16:57:46 +0200 | [diff] [blame] | 587 | set_bit(ATC_IS_ERROR, &atchan->status); |
Nicolas Ferre | dc78baa | 2009-07-03 19:24:33 +0200 | [diff] [blame] | 588 | } |
Elen Song | d48de6f | 2013-05-10 11:01:46 +0800 | [diff] [blame] | 589 | if (pending & AT_DMA_BTC(i)) |
| 590 | set_bit(ATC_IS_BTC, &atchan->status); |
Nicolas Ferre | dc78baa | 2009-07-03 19:24:33 +0200 | [diff] [blame] | 591 | tasklet_schedule(&atchan->tasklet); |
| 592 | ret = IRQ_HANDLED; |
| 593 | } |
| 594 | } |
| 595 | |
| 596 | } while (pending); |
| 597 | |
| 598 | return ret; |
| 599 | } |
| 600 | |
| 601 | |
| 602 | /*-- DMA Engine API --------------------------------------------------*/ |
| 603 | |
| 604 | /** |
| 605 | * atc_tx_submit - set the prepared descriptor(s) to be executed by the engine |
| 606 | * @desc: descriptor at the head of the transaction chain |
| 607 | * |
| 608 | * Queue chain if DMA engine is working already |
| 609 | * |
| 610 | * Cookie increment and adding to active_list or queue must be atomic |
| 611 | */ |
| 612 | static dma_cookie_t atc_tx_submit(struct dma_async_tx_descriptor *tx) |
| 613 | { |
| 614 | struct at_desc *desc = txd_to_at_desc(tx); |
| 615 | struct at_dma_chan *atchan = to_at_dma_chan(tx->chan); |
| 616 | dma_cookie_t cookie; |
Nicolas Ferre | d8cb04b | 2011-07-27 12:21:28 +0000 | [diff] [blame] | 617 | unsigned long flags; |
Nicolas Ferre | dc78baa | 2009-07-03 19:24:33 +0200 | [diff] [blame] | 618 | |
Nicolas Ferre | d8cb04b | 2011-07-27 12:21:28 +0000 | [diff] [blame] | 619 | spin_lock_irqsave(&atchan->lock, flags); |
Russell King - ARM Linux | 884485e | 2012-03-06 22:34:46 +0000 | [diff] [blame] | 620 | cookie = dma_cookie_assign(tx); |
Nicolas Ferre | dc78baa | 2009-07-03 19:24:33 +0200 | [diff] [blame] | 621 | |
| 622 | if (list_empty(&atchan->active_list)) { |
| 623 | dev_vdbg(chan2dev(tx->chan), "tx_submit: started %u\n", |
| 624 | desc->txd.cookie); |
| 625 | atc_dostart(atchan, desc); |
| 626 | list_add_tail(&desc->desc_node, &atchan->active_list); |
| 627 | } else { |
| 628 | dev_vdbg(chan2dev(tx->chan), "tx_submit: queued %u\n", |
| 629 | desc->txd.cookie); |
| 630 | list_add_tail(&desc->desc_node, &atchan->queue); |
| 631 | } |
| 632 | |
Nicolas Ferre | d8cb04b | 2011-07-27 12:21:28 +0000 | [diff] [blame] | 633 | spin_unlock_irqrestore(&atchan->lock, flags); |
Nicolas Ferre | dc78baa | 2009-07-03 19:24:33 +0200 | [diff] [blame] | 634 | |
| 635 | return cookie; |
| 636 | } |
| 637 | |
| 638 | /** |
| 639 | * atc_prep_dma_memcpy - prepare a memcpy operation |
| 640 | * @chan: the channel to prepare operation on |
| 641 | * @dest: operation virtual destination address |
| 642 | * @src: operation virtual source address |
| 643 | * @len: operation length |
| 644 | * @flags: tx descriptor status flags |
| 645 | */ |
| 646 | static struct dma_async_tx_descriptor * |
| 647 | atc_prep_dma_memcpy(struct dma_chan *chan, dma_addr_t dest, dma_addr_t src, |
| 648 | size_t len, unsigned long flags) |
| 649 | { |
| 650 | struct at_dma_chan *atchan = to_at_dma_chan(chan); |
| 651 | struct at_desc *desc = NULL; |
| 652 | struct at_desc *first = NULL; |
| 653 | struct at_desc *prev = NULL; |
| 654 | size_t xfer_count; |
| 655 | size_t offset; |
| 656 | unsigned int src_width; |
| 657 | unsigned int dst_width; |
| 658 | u32 ctrla; |
| 659 | u32 ctrlb; |
| 660 | |
| 661 | dev_vdbg(chan2dev(chan), "prep_dma_memcpy: d0x%x s0x%x l0x%zx f0x%lx\n", |
| 662 | dest, src, len, flags); |
| 663 | |
| 664 | if (unlikely(!len)) { |
| 665 | dev_dbg(chan2dev(chan), "prep_dma_memcpy: length is zero!\n"); |
| 666 | return NULL; |
| 667 | } |
| 668 | |
Nicolas Ferre | 9b3aa58 | 2011-04-30 16:57:45 +0200 | [diff] [blame] | 669 | ctrlb = ATC_DEFAULT_CTRLB | ATC_IEN |
Nicolas Ferre | dc78baa | 2009-07-03 19:24:33 +0200 | [diff] [blame] | 670 | | ATC_SRC_ADDR_MODE_INCR |
| 671 | | ATC_DST_ADDR_MODE_INCR |
| 672 | | ATC_FC_MEM2MEM; |
| 673 | |
| 674 | /* |
| 675 | * We can be a lot more clever here, but this should take care |
| 676 | * of the most common optimization. |
| 677 | */ |
| 678 | if (!((src | dest | len) & 3)) { |
Nicolas Ferre | b409ebf | 2012-05-10 12:17:40 +0200 | [diff] [blame] | 679 | ctrla = ATC_SRC_WIDTH_WORD | ATC_DST_WIDTH_WORD; |
Nicolas Ferre | dc78baa | 2009-07-03 19:24:33 +0200 | [diff] [blame] | 680 | src_width = dst_width = 2; |
| 681 | } else if (!((src | dest | len) & 1)) { |
Nicolas Ferre | b409ebf | 2012-05-10 12:17:40 +0200 | [diff] [blame] | 682 | ctrla = ATC_SRC_WIDTH_HALFWORD | ATC_DST_WIDTH_HALFWORD; |
Nicolas Ferre | dc78baa | 2009-07-03 19:24:33 +0200 | [diff] [blame] | 683 | src_width = dst_width = 1; |
| 684 | } else { |
Nicolas Ferre | b409ebf | 2012-05-10 12:17:40 +0200 | [diff] [blame] | 685 | ctrla = ATC_SRC_WIDTH_BYTE | ATC_DST_WIDTH_BYTE; |
Nicolas Ferre | dc78baa | 2009-07-03 19:24:33 +0200 | [diff] [blame] | 686 | src_width = dst_width = 0; |
| 687 | } |
| 688 | |
| 689 | for (offset = 0; offset < len; offset += xfer_count << src_width) { |
| 690 | xfer_count = min_t(size_t, (len - offset) >> src_width, |
| 691 | ATC_BTSIZE_MAX); |
| 692 | |
| 693 | desc = atc_desc_get(atchan); |
| 694 | if (!desc) |
| 695 | goto err_desc_get; |
| 696 | |
| 697 | desc->lli.saddr = src + offset; |
| 698 | desc->lli.daddr = dest + offset; |
| 699 | desc->lli.ctrla = ctrla | xfer_count; |
| 700 | desc->lli.ctrlb = ctrlb; |
| 701 | |
| 702 | desc->txd.cookie = 0; |
Nicolas Ferre | dc78baa | 2009-07-03 19:24:33 +0200 | [diff] [blame] | 703 | |
Nicolas Ferre | e257e15 | 2011-05-06 19:56:53 +0200 | [diff] [blame] | 704 | atc_desc_chain(&first, &prev, desc); |
Nicolas Ferre | dc78baa | 2009-07-03 19:24:33 +0200 | [diff] [blame] | 705 | } |
| 706 | |
| 707 | /* First descriptor of the chain embedds additional information */ |
| 708 | first->txd.cookie = -EBUSY; |
| 709 | first->len = len; |
Elen Song | d088c33 | 2013-05-10 11:00:50 +0800 | [diff] [blame] | 710 | first->tx_width = src_width; |
Nicolas Ferre | dc78baa | 2009-07-03 19:24:33 +0200 | [diff] [blame] | 711 | |
| 712 | /* set end-of-link to the last link descriptor of list*/ |
| 713 | set_desc_eol(desc); |
| 714 | |
Nicolas Ferre | 568f7f0 | 2011-01-12 15:39:09 +0100 | [diff] [blame] | 715 | first->txd.flags = flags; /* client is in control of this ack */ |
Nicolas Ferre | dc78baa | 2009-07-03 19:24:33 +0200 | [diff] [blame] | 716 | |
| 717 | return &first->txd; |
| 718 | |
| 719 | err_desc_get: |
| 720 | atc_desc_put(atchan, first); |
| 721 | return NULL; |
| 722 | } |
| 723 | |
Nicolas Ferre | 808347f | 2009-07-22 20:04:45 +0200 | [diff] [blame] | 724 | |
| 725 | /** |
| 726 | * atc_prep_slave_sg - prepare descriptors for a DMA_SLAVE transaction |
| 727 | * @chan: DMA channel |
| 728 | * @sgl: scatterlist to transfer to/from |
| 729 | * @sg_len: number of entries in @scatterlist |
| 730 | * @direction: DMA direction |
| 731 | * @flags: tx descriptor status flags |
Alexandre Bounine | 185ecb5 | 2012-03-08 15:35:13 -0500 | [diff] [blame] | 732 | * @context: transaction context (ignored) |
Nicolas Ferre | 808347f | 2009-07-22 20:04:45 +0200 | [diff] [blame] | 733 | */ |
| 734 | static struct dma_async_tx_descriptor * |
| 735 | atc_prep_slave_sg(struct dma_chan *chan, struct scatterlist *sgl, |
Vinod Koul | db8196d | 2011-10-13 22:34:23 +0530 | [diff] [blame] | 736 | unsigned int sg_len, enum dma_transfer_direction direction, |
Alexandre Bounine | 185ecb5 | 2012-03-08 15:35:13 -0500 | [diff] [blame] | 737 | unsigned long flags, void *context) |
Nicolas Ferre | 808347f | 2009-07-22 20:04:45 +0200 | [diff] [blame] | 738 | { |
| 739 | struct at_dma_chan *atchan = to_at_dma_chan(chan); |
| 740 | struct at_dma_slave *atslave = chan->private; |
Nicolas Ferre | beeaa10 | 2012-03-14 12:41:43 +0100 | [diff] [blame] | 741 | struct dma_slave_config *sconfig = &atchan->dma_sconfig; |
Nicolas Ferre | 808347f | 2009-07-22 20:04:45 +0200 | [diff] [blame] | 742 | struct at_desc *first = NULL; |
| 743 | struct at_desc *prev = NULL; |
| 744 | u32 ctrla; |
| 745 | u32 ctrlb; |
| 746 | dma_addr_t reg; |
| 747 | unsigned int reg_width; |
| 748 | unsigned int mem_width; |
| 749 | unsigned int i; |
| 750 | struct scatterlist *sg; |
| 751 | size_t total_len = 0; |
| 752 | |
Nicolas Ferre | cc52a10 | 2011-04-30 16:57:47 +0200 | [diff] [blame] | 753 | dev_vdbg(chan2dev(chan), "prep_slave_sg (%d): %s f0x%lx\n", |
| 754 | sg_len, |
Vinod Koul | db8196d | 2011-10-13 22:34:23 +0530 | [diff] [blame] | 755 | direction == DMA_MEM_TO_DEV ? "TO DEVICE" : "FROM DEVICE", |
Nicolas Ferre | 808347f | 2009-07-22 20:04:45 +0200 | [diff] [blame] | 756 | flags); |
| 757 | |
| 758 | if (unlikely(!atslave || !sg_len)) { |
Nicolas Ferre | c618a9b | 2012-09-11 17:21:44 +0200 | [diff] [blame] | 759 | dev_dbg(chan2dev(chan), "prep_slave_sg: sg length is zero!\n"); |
Nicolas Ferre | 808347f | 2009-07-22 20:04:45 +0200 | [diff] [blame] | 760 | return NULL; |
| 761 | } |
| 762 | |
Nicolas Ferre | 1dd1ea8 | 2012-05-10 12:17:41 +0200 | [diff] [blame] | 763 | ctrla = ATC_SCSIZE(sconfig->src_maxburst) |
| 764 | | ATC_DCSIZE(sconfig->dst_maxburst); |
Nicolas Ferre | ae14d4b | 2011-04-30 16:57:49 +0200 | [diff] [blame] | 765 | ctrlb = ATC_IEN; |
Nicolas Ferre | 808347f | 2009-07-22 20:04:45 +0200 | [diff] [blame] | 766 | |
| 767 | switch (direction) { |
Vinod Koul | db8196d | 2011-10-13 22:34:23 +0530 | [diff] [blame] | 768 | case DMA_MEM_TO_DEV: |
Nicolas Ferre | beeaa10 | 2012-03-14 12:41:43 +0100 | [diff] [blame] | 769 | reg_width = convert_buswidth(sconfig->dst_addr_width); |
Nicolas Ferre | 808347f | 2009-07-22 20:04:45 +0200 | [diff] [blame] | 770 | ctrla |= ATC_DST_WIDTH(reg_width); |
| 771 | ctrlb |= ATC_DST_ADDR_MODE_FIXED |
| 772 | | ATC_SRC_ADDR_MODE_INCR |
Nicolas Ferre | ae14d4b | 2011-04-30 16:57:49 +0200 | [diff] [blame] | 773 | | ATC_FC_MEM2PER |
Ludovic Desroches | bbe89c8 | 2013-04-19 09:11:18 +0000 | [diff] [blame] | 774 | | ATC_SIF(atchan->mem_if) | ATC_DIF(atchan->per_if); |
Nicolas Ferre | beeaa10 | 2012-03-14 12:41:43 +0100 | [diff] [blame] | 775 | reg = sconfig->dst_addr; |
Nicolas Ferre | 808347f | 2009-07-22 20:04:45 +0200 | [diff] [blame] | 776 | for_each_sg(sgl, sg, sg_len, i) { |
| 777 | struct at_desc *desc; |
| 778 | u32 len; |
| 779 | u32 mem; |
| 780 | |
| 781 | desc = atc_desc_get(atchan); |
| 782 | if (!desc) |
| 783 | goto err_desc_get; |
| 784 | |
Nicolas Ferre | 0f70e8c | 2010-12-15 18:50:16 +0100 | [diff] [blame] | 785 | mem = sg_dma_address(sg); |
Nicolas Ferre | 808347f | 2009-07-22 20:04:45 +0200 | [diff] [blame] | 786 | len = sg_dma_len(sg); |
Nicolas Ferre | c456797 | 2012-09-11 17:21:45 +0200 | [diff] [blame] | 787 | if (unlikely(!len)) { |
| 788 | dev_dbg(chan2dev(chan), |
| 789 | "prep_slave_sg: sg(%d) data length is zero\n", i); |
| 790 | goto err; |
| 791 | } |
Nicolas Ferre | 808347f | 2009-07-22 20:04:45 +0200 | [diff] [blame] | 792 | mem_width = 2; |
| 793 | if (unlikely(mem & 3 || len & 3)) |
| 794 | mem_width = 0; |
| 795 | |
| 796 | desc->lli.saddr = mem; |
| 797 | desc->lli.daddr = reg; |
| 798 | desc->lli.ctrla = ctrla |
| 799 | | ATC_SRC_WIDTH(mem_width) |
| 800 | | len >> mem_width; |
| 801 | desc->lli.ctrlb = ctrlb; |
| 802 | |
Nicolas Ferre | e257e15 | 2011-05-06 19:56:53 +0200 | [diff] [blame] | 803 | atc_desc_chain(&first, &prev, desc); |
Nicolas Ferre | 808347f | 2009-07-22 20:04:45 +0200 | [diff] [blame] | 804 | total_len += len; |
| 805 | } |
| 806 | break; |
Vinod Koul | db8196d | 2011-10-13 22:34:23 +0530 | [diff] [blame] | 807 | case DMA_DEV_TO_MEM: |
Nicolas Ferre | beeaa10 | 2012-03-14 12:41:43 +0100 | [diff] [blame] | 808 | reg_width = convert_buswidth(sconfig->src_addr_width); |
Nicolas Ferre | 808347f | 2009-07-22 20:04:45 +0200 | [diff] [blame] | 809 | ctrla |= ATC_SRC_WIDTH(reg_width); |
| 810 | ctrlb |= ATC_DST_ADDR_MODE_INCR |
| 811 | | ATC_SRC_ADDR_MODE_FIXED |
Nicolas Ferre | ae14d4b | 2011-04-30 16:57:49 +0200 | [diff] [blame] | 812 | | ATC_FC_PER2MEM |
Ludovic Desroches | bbe89c8 | 2013-04-19 09:11:18 +0000 | [diff] [blame] | 813 | | ATC_SIF(atchan->per_if) | ATC_DIF(atchan->mem_if); |
Nicolas Ferre | 808347f | 2009-07-22 20:04:45 +0200 | [diff] [blame] | 814 | |
Nicolas Ferre | beeaa10 | 2012-03-14 12:41:43 +0100 | [diff] [blame] | 815 | reg = sconfig->src_addr; |
Nicolas Ferre | 808347f | 2009-07-22 20:04:45 +0200 | [diff] [blame] | 816 | for_each_sg(sgl, sg, sg_len, i) { |
| 817 | struct at_desc *desc; |
| 818 | u32 len; |
| 819 | u32 mem; |
| 820 | |
| 821 | desc = atc_desc_get(atchan); |
| 822 | if (!desc) |
| 823 | goto err_desc_get; |
| 824 | |
Nicolas Ferre | 0f70e8c | 2010-12-15 18:50:16 +0100 | [diff] [blame] | 825 | mem = sg_dma_address(sg); |
Nicolas Ferre | 808347f | 2009-07-22 20:04:45 +0200 | [diff] [blame] | 826 | len = sg_dma_len(sg); |
Nicolas Ferre | c456797 | 2012-09-11 17:21:45 +0200 | [diff] [blame] | 827 | if (unlikely(!len)) { |
| 828 | dev_dbg(chan2dev(chan), |
| 829 | "prep_slave_sg: sg(%d) data length is zero\n", i); |
| 830 | goto err; |
| 831 | } |
Nicolas Ferre | 808347f | 2009-07-22 20:04:45 +0200 | [diff] [blame] | 832 | mem_width = 2; |
| 833 | if (unlikely(mem & 3 || len & 3)) |
| 834 | mem_width = 0; |
| 835 | |
| 836 | desc->lli.saddr = reg; |
| 837 | desc->lli.daddr = mem; |
| 838 | desc->lli.ctrla = ctrla |
| 839 | | ATC_DST_WIDTH(mem_width) |
Nicolas Ferre | 59a609d | 2010-12-13 13:48:41 +0100 | [diff] [blame] | 840 | | len >> reg_width; |
Nicolas Ferre | 808347f | 2009-07-22 20:04:45 +0200 | [diff] [blame] | 841 | desc->lli.ctrlb = ctrlb; |
| 842 | |
Nicolas Ferre | e257e15 | 2011-05-06 19:56:53 +0200 | [diff] [blame] | 843 | atc_desc_chain(&first, &prev, desc); |
Nicolas Ferre | 808347f | 2009-07-22 20:04:45 +0200 | [diff] [blame] | 844 | total_len += len; |
| 845 | } |
| 846 | break; |
| 847 | default: |
| 848 | return NULL; |
| 849 | } |
| 850 | |
| 851 | /* set end-of-link to the last link descriptor of list*/ |
| 852 | set_desc_eol(prev); |
| 853 | |
| 854 | /* First descriptor of the chain embedds additional information */ |
| 855 | first->txd.cookie = -EBUSY; |
| 856 | first->len = total_len; |
Elen Song | d088c33 | 2013-05-10 11:00:50 +0800 | [diff] [blame] | 857 | first->tx_width = reg_width; |
Nicolas Ferre | 808347f | 2009-07-22 20:04:45 +0200 | [diff] [blame] | 858 | |
Nicolas Ferre | 568f7f0 | 2011-01-12 15:39:09 +0100 | [diff] [blame] | 859 | /* first link descriptor of list is responsible of flags */ |
| 860 | first->txd.flags = flags; /* client is in control of this ack */ |
Nicolas Ferre | 808347f | 2009-07-22 20:04:45 +0200 | [diff] [blame] | 861 | |
| 862 | return &first->txd; |
| 863 | |
| 864 | err_desc_get: |
| 865 | dev_err(chan2dev(chan), "not enough descriptors available\n"); |
Nicolas Ferre | c456797 | 2012-09-11 17:21:45 +0200 | [diff] [blame] | 866 | err: |
Nicolas Ferre | 808347f | 2009-07-22 20:04:45 +0200 | [diff] [blame] | 867 | atc_desc_put(atchan, first); |
| 868 | return NULL; |
| 869 | } |
| 870 | |
Nicolas Ferre | 53830cc | 2011-04-30 16:57:46 +0200 | [diff] [blame] | 871 | /** |
| 872 | * atc_dma_cyclic_check_values |
| 873 | * Check for too big/unaligned periods and unaligned DMA buffer |
| 874 | */ |
| 875 | static int |
| 876 | atc_dma_cyclic_check_values(unsigned int reg_width, dma_addr_t buf_addr, |
Andy Shevchenko | 0e7264c | 2013-01-10 10:52:57 +0200 | [diff] [blame] | 877 | size_t period_len) |
Nicolas Ferre | 53830cc | 2011-04-30 16:57:46 +0200 | [diff] [blame] | 878 | { |
| 879 | if (period_len > (ATC_BTSIZE_MAX << reg_width)) |
| 880 | goto err_out; |
| 881 | if (unlikely(period_len & ((1 << reg_width) - 1))) |
| 882 | goto err_out; |
| 883 | if (unlikely(buf_addr & ((1 << reg_width) - 1))) |
| 884 | goto err_out; |
Nicolas Ferre | 53830cc | 2011-04-30 16:57:46 +0200 | [diff] [blame] | 885 | |
| 886 | return 0; |
| 887 | |
| 888 | err_out: |
| 889 | return -EINVAL; |
| 890 | } |
| 891 | |
| 892 | /** |
Masanari Iida | d73111c | 2012-08-04 23:37:53 +0900 | [diff] [blame] | 893 | * atc_dma_cyclic_fill_desc - Fill one period descriptor |
Nicolas Ferre | 53830cc | 2011-04-30 16:57:46 +0200 | [diff] [blame] | 894 | */ |
| 895 | static int |
Nicolas Ferre | beeaa10 | 2012-03-14 12:41:43 +0100 | [diff] [blame] | 896 | atc_dma_cyclic_fill_desc(struct dma_chan *chan, struct at_desc *desc, |
Nicolas Ferre | 53830cc | 2011-04-30 16:57:46 +0200 | [diff] [blame] | 897 | unsigned int period_index, dma_addr_t buf_addr, |
Nicolas Ferre | beeaa10 | 2012-03-14 12:41:43 +0100 | [diff] [blame] | 898 | unsigned int reg_width, size_t period_len, |
| 899 | enum dma_transfer_direction direction) |
Nicolas Ferre | 53830cc | 2011-04-30 16:57:46 +0200 | [diff] [blame] | 900 | { |
Nicolas Ferre | beeaa10 | 2012-03-14 12:41:43 +0100 | [diff] [blame] | 901 | struct at_dma_chan *atchan = to_at_dma_chan(chan); |
Nicolas Ferre | beeaa10 | 2012-03-14 12:41:43 +0100 | [diff] [blame] | 902 | struct dma_slave_config *sconfig = &atchan->dma_sconfig; |
| 903 | u32 ctrla; |
Nicolas Ferre | 53830cc | 2011-04-30 16:57:46 +0200 | [diff] [blame] | 904 | |
| 905 | /* prepare common CRTLA value */ |
Nicolas Ferre | 1dd1ea8 | 2012-05-10 12:17:41 +0200 | [diff] [blame] | 906 | ctrla = ATC_SCSIZE(sconfig->src_maxburst) |
| 907 | | ATC_DCSIZE(sconfig->dst_maxburst) |
Nicolas Ferre | 53830cc | 2011-04-30 16:57:46 +0200 | [diff] [blame] | 908 | | ATC_DST_WIDTH(reg_width) |
| 909 | | ATC_SRC_WIDTH(reg_width) |
| 910 | | period_len >> reg_width; |
| 911 | |
| 912 | switch (direction) { |
Vinod Koul | db8196d | 2011-10-13 22:34:23 +0530 | [diff] [blame] | 913 | case DMA_MEM_TO_DEV: |
Nicolas Ferre | 53830cc | 2011-04-30 16:57:46 +0200 | [diff] [blame] | 914 | desc->lli.saddr = buf_addr + (period_len * period_index); |
Nicolas Ferre | beeaa10 | 2012-03-14 12:41:43 +0100 | [diff] [blame] | 915 | desc->lli.daddr = sconfig->dst_addr; |
Nicolas Ferre | 53830cc | 2011-04-30 16:57:46 +0200 | [diff] [blame] | 916 | desc->lli.ctrla = ctrla; |
Nicolas Ferre | ae14d4b | 2011-04-30 16:57:49 +0200 | [diff] [blame] | 917 | desc->lli.ctrlb = ATC_DST_ADDR_MODE_FIXED |
Nicolas Ferre | 53830cc | 2011-04-30 16:57:46 +0200 | [diff] [blame] | 918 | | ATC_SRC_ADDR_MODE_INCR |
Nicolas Ferre | ae14d4b | 2011-04-30 16:57:49 +0200 | [diff] [blame] | 919 | | ATC_FC_MEM2PER |
Ludovic Desroches | bbe89c8 | 2013-04-19 09:11:18 +0000 | [diff] [blame] | 920 | | ATC_SIF(atchan->mem_if) |
| 921 | | ATC_DIF(atchan->per_if); |
Nicolas Ferre | 53830cc | 2011-04-30 16:57:46 +0200 | [diff] [blame] | 922 | break; |
| 923 | |
Vinod Koul | db8196d | 2011-10-13 22:34:23 +0530 | [diff] [blame] | 924 | case DMA_DEV_TO_MEM: |
Nicolas Ferre | beeaa10 | 2012-03-14 12:41:43 +0100 | [diff] [blame] | 925 | desc->lli.saddr = sconfig->src_addr; |
Nicolas Ferre | 53830cc | 2011-04-30 16:57:46 +0200 | [diff] [blame] | 926 | desc->lli.daddr = buf_addr + (period_len * period_index); |
| 927 | desc->lli.ctrla = ctrla; |
Nicolas Ferre | ae14d4b | 2011-04-30 16:57:49 +0200 | [diff] [blame] | 928 | desc->lli.ctrlb = ATC_DST_ADDR_MODE_INCR |
Nicolas Ferre | 53830cc | 2011-04-30 16:57:46 +0200 | [diff] [blame] | 929 | | ATC_SRC_ADDR_MODE_FIXED |
Nicolas Ferre | ae14d4b | 2011-04-30 16:57:49 +0200 | [diff] [blame] | 930 | | ATC_FC_PER2MEM |
Ludovic Desroches | bbe89c8 | 2013-04-19 09:11:18 +0000 | [diff] [blame] | 931 | | ATC_SIF(atchan->per_if) |
| 932 | | ATC_DIF(atchan->mem_if); |
Nicolas Ferre | 53830cc | 2011-04-30 16:57:46 +0200 | [diff] [blame] | 933 | break; |
| 934 | |
| 935 | default: |
| 936 | return -EINVAL; |
| 937 | } |
| 938 | |
| 939 | return 0; |
| 940 | } |
| 941 | |
| 942 | /** |
| 943 | * atc_prep_dma_cyclic - prepare the cyclic DMA transfer |
| 944 | * @chan: the DMA channel to prepare |
| 945 | * @buf_addr: physical DMA address where the buffer starts |
| 946 | * @buf_len: total number of bytes for the entire buffer |
| 947 | * @period_len: number of bytes for each period |
| 948 | * @direction: transfer direction, to or from device |
Peter Ujfalusi | ec8b5e4 | 2012-09-14 15:05:47 +0300 | [diff] [blame] | 949 | * @flags: tx descriptor status flags |
Alexandre Bounine | 185ecb5 | 2012-03-08 15:35:13 -0500 | [diff] [blame] | 950 | * @context: transfer context (ignored) |
Nicolas Ferre | 53830cc | 2011-04-30 16:57:46 +0200 | [diff] [blame] | 951 | */ |
| 952 | static struct dma_async_tx_descriptor * |
| 953 | atc_prep_dma_cyclic(struct dma_chan *chan, dma_addr_t buf_addr, size_t buf_len, |
Alexandre Bounine | 185ecb5 | 2012-03-08 15:35:13 -0500 | [diff] [blame] | 954 | size_t period_len, enum dma_transfer_direction direction, |
Peter Ujfalusi | ec8b5e4 | 2012-09-14 15:05:47 +0300 | [diff] [blame] | 955 | unsigned long flags, void *context) |
Nicolas Ferre | 53830cc | 2011-04-30 16:57:46 +0200 | [diff] [blame] | 956 | { |
| 957 | struct at_dma_chan *atchan = to_at_dma_chan(chan); |
| 958 | struct at_dma_slave *atslave = chan->private; |
Nicolas Ferre | beeaa10 | 2012-03-14 12:41:43 +0100 | [diff] [blame] | 959 | struct dma_slave_config *sconfig = &atchan->dma_sconfig; |
Nicolas Ferre | 53830cc | 2011-04-30 16:57:46 +0200 | [diff] [blame] | 960 | struct at_desc *first = NULL; |
| 961 | struct at_desc *prev = NULL; |
| 962 | unsigned long was_cyclic; |
Nicolas Ferre | beeaa10 | 2012-03-14 12:41:43 +0100 | [diff] [blame] | 963 | unsigned int reg_width; |
Nicolas Ferre | 53830cc | 2011-04-30 16:57:46 +0200 | [diff] [blame] | 964 | unsigned int periods = buf_len / period_len; |
| 965 | unsigned int i; |
| 966 | |
| 967 | dev_vdbg(chan2dev(chan), "prep_dma_cyclic: %s buf@0x%08x - %d (%d/%d)\n", |
Vinod Koul | db8196d | 2011-10-13 22:34:23 +0530 | [diff] [blame] | 968 | direction == DMA_MEM_TO_DEV ? "TO DEVICE" : "FROM DEVICE", |
Nicolas Ferre | 53830cc | 2011-04-30 16:57:46 +0200 | [diff] [blame] | 969 | buf_addr, |
| 970 | periods, buf_len, period_len); |
| 971 | |
| 972 | if (unlikely(!atslave || !buf_len || !period_len)) { |
| 973 | dev_dbg(chan2dev(chan), "prep_dma_cyclic: length is zero!\n"); |
| 974 | return NULL; |
| 975 | } |
| 976 | |
| 977 | was_cyclic = test_and_set_bit(ATC_IS_CYCLIC, &atchan->status); |
| 978 | if (was_cyclic) { |
| 979 | dev_dbg(chan2dev(chan), "prep_dma_cyclic: channel in use!\n"); |
| 980 | return NULL; |
| 981 | } |
| 982 | |
Andy Shevchenko | 0e7264c | 2013-01-10 10:52:57 +0200 | [diff] [blame] | 983 | if (unlikely(!is_slave_direction(direction))) |
| 984 | goto err_out; |
| 985 | |
Nicolas Ferre | beeaa10 | 2012-03-14 12:41:43 +0100 | [diff] [blame] | 986 | if (sconfig->direction == DMA_MEM_TO_DEV) |
| 987 | reg_width = convert_buswidth(sconfig->dst_addr_width); |
| 988 | else |
| 989 | reg_width = convert_buswidth(sconfig->src_addr_width); |
| 990 | |
Nicolas Ferre | 53830cc | 2011-04-30 16:57:46 +0200 | [diff] [blame] | 991 | /* Check for too big/unaligned periods and unaligned DMA buffer */ |
Andy Shevchenko | 0e7264c | 2013-01-10 10:52:57 +0200 | [diff] [blame] | 992 | if (atc_dma_cyclic_check_values(reg_width, buf_addr, period_len)) |
Nicolas Ferre | 53830cc | 2011-04-30 16:57:46 +0200 | [diff] [blame] | 993 | goto err_out; |
| 994 | |
| 995 | /* build cyclic linked list */ |
| 996 | for (i = 0; i < periods; i++) { |
| 997 | struct at_desc *desc; |
| 998 | |
| 999 | desc = atc_desc_get(atchan); |
| 1000 | if (!desc) |
| 1001 | goto err_desc_get; |
| 1002 | |
Nicolas Ferre | beeaa10 | 2012-03-14 12:41:43 +0100 | [diff] [blame] | 1003 | if (atc_dma_cyclic_fill_desc(chan, desc, i, buf_addr, |
| 1004 | reg_width, period_len, direction)) |
Nicolas Ferre | 53830cc | 2011-04-30 16:57:46 +0200 | [diff] [blame] | 1005 | goto err_desc_get; |
| 1006 | |
| 1007 | atc_desc_chain(&first, &prev, desc); |
| 1008 | } |
| 1009 | |
| 1010 | /* lets make a cyclic list */ |
| 1011 | prev->lli.dscr = first->txd.phys; |
| 1012 | |
| 1013 | /* First descriptor of the chain embedds additional information */ |
| 1014 | first->txd.cookie = -EBUSY; |
| 1015 | first->len = buf_len; |
Elen Song | d088c33 | 2013-05-10 11:00:50 +0800 | [diff] [blame] | 1016 | first->tx_width = reg_width; |
Nicolas Ferre | 53830cc | 2011-04-30 16:57:46 +0200 | [diff] [blame] | 1017 | |
| 1018 | return &first->txd; |
| 1019 | |
| 1020 | err_desc_get: |
| 1021 | dev_err(chan2dev(chan), "not enough descriptors available\n"); |
| 1022 | atc_desc_put(atchan, first); |
| 1023 | err_out: |
| 1024 | clear_bit(ATC_IS_CYCLIC, &atchan->status); |
| 1025 | return NULL; |
| 1026 | } |
| 1027 | |
Nicolas Ferre | beeaa10 | 2012-03-14 12:41:43 +0100 | [diff] [blame] | 1028 | static int set_runtime_config(struct dma_chan *chan, |
| 1029 | struct dma_slave_config *sconfig) |
| 1030 | { |
| 1031 | struct at_dma_chan *atchan = to_at_dma_chan(chan); |
| 1032 | |
| 1033 | /* Check if it is chan is configured for slave transfers */ |
| 1034 | if (!chan->private) |
| 1035 | return -EINVAL; |
| 1036 | |
| 1037 | memcpy(&atchan->dma_sconfig, sconfig, sizeof(*sconfig)); |
| 1038 | |
| 1039 | convert_burst(&atchan->dma_sconfig.src_maxburst); |
| 1040 | convert_burst(&atchan->dma_sconfig.dst_maxburst); |
| 1041 | |
| 1042 | return 0; |
| 1043 | } |
| 1044 | |
Nicolas Ferre | 53830cc | 2011-04-30 16:57:46 +0200 | [diff] [blame] | 1045 | |
Linus Walleij | 0582763 | 2010-05-17 16:30:42 -0700 | [diff] [blame] | 1046 | static int atc_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd, |
| 1047 | unsigned long arg) |
Nicolas Ferre | 808347f | 2009-07-22 20:04:45 +0200 | [diff] [blame] | 1048 | { |
| 1049 | struct at_dma_chan *atchan = to_at_dma_chan(chan); |
| 1050 | struct at_dma *atdma = to_at_dma(chan->device); |
Nicolas Ferre | 23b5e3a | 2011-05-06 19:56:52 +0200 | [diff] [blame] | 1051 | int chan_id = atchan->chan_common.chan_id; |
Nicolas Ferre | d8cb04b | 2011-07-27 12:21:28 +0000 | [diff] [blame] | 1052 | unsigned long flags; |
Nicolas Ferre | 23b5e3a | 2011-05-06 19:56:52 +0200 | [diff] [blame] | 1053 | |
Nicolas Ferre | 808347f | 2009-07-22 20:04:45 +0200 | [diff] [blame] | 1054 | LIST_HEAD(list); |
| 1055 | |
Nicolas Ferre | 23b5e3a | 2011-05-06 19:56:52 +0200 | [diff] [blame] | 1056 | dev_vdbg(chan2dev(chan), "atc_control (%d)\n", cmd); |
| 1057 | |
| 1058 | if (cmd == DMA_PAUSE) { |
Nicolas Ferre | d8cb04b | 2011-07-27 12:21:28 +0000 | [diff] [blame] | 1059 | spin_lock_irqsave(&atchan->lock, flags); |
Nicolas Ferre | 23b5e3a | 2011-05-06 19:56:52 +0200 | [diff] [blame] | 1060 | |
| 1061 | dma_writel(atdma, CHER, AT_DMA_SUSP(chan_id)); |
Nicolas Ferre | 23b5e3a | 2011-05-06 19:56:52 +0200 | [diff] [blame] | 1062 | set_bit(ATC_IS_PAUSED, &atchan->status); |
| 1063 | |
Nicolas Ferre | d8cb04b | 2011-07-27 12:21:28 +0000 | [diff] [blame] | 1064 | spin_unlock_irqrestore(&atchan->lock, flags); |
Nicolas Ferre | 23b5e3a | 2011-05-06 19:56:52 +0200 | [diff] [blame] | 1065 | } else if (cmd == DMA_RESUME) { |
Nicolas Ferre | 3c47748 | 2011-07-25 21:09:23 +0000 | [diff] [blame] | 1066 | if (!atc_chan_is_paused(atchan)) |
Nicolas Ferre | 23b5e3a | 2011-05-06 19:56:52 +0200 | [diff] [blame] | 1067 | return 0; |
| 1068 | |
Nicolas Ferre | d8cb04b | 2011-07-27 12:21:28 +0000 | [diff] [blame] | 1069 | spin_lock_irqsave(&atchan->lock, flags); |
Nicolas Ferre | 23b5e3a | 2011-05-06 19:56:52 +0200 | [diff] [blame] | 1070 | |
| 1071 | dma_writel(atdma, CHDR, AT_DMA_RES(chan_id)); |
| 1072 | clear_bit(ATC_IS_PAUSED, &atchan->status); |
| 1073 | |
Nicolas Ferre | d8cb04b | 2011-07-27 12:21:28 +0000 | [diff] [blame] | 1074 | spin_unlock_irqrestore(&atchan->lock, flags); |
Nicolas Ferre | 23b5e3a | 2011-05-06 19:56:52 +0200 | [diff] [blame] | 1075 | } else if (cmd == DMA_TERMINATE_ALL) { |
| 1076 | struct at_desc *desc, *_desc; |
| 1077 | /* |
| 1078 | * This is only called when something went wrong elsewhere, so |
| 1079 | * we don't really care about the data. Just disable the |
| 1080 | * channel. We still have to poll the channel enable bit due |
| 1081 | * to AHB/HSB limitations. |
| 1082 | */ |
Nicolas Ferre | d8cb04b | 2011-07-27 12:21:28 +0000 | [diff] [blame] | 1083 | spin_lock_irqsave(&atchan->lock, flags); |
Nicolas Ferre | 23b5e3a | 2011-05-06 19:56:52 +0200 | [diff] [blame] | 1084 | |
| 1085 | /* disabling channel: must also remove suspend state */ |
| 1086 | dma_writel(atdma, CHDR, AT_DMA_RES(chan_id) | atchan->mask); |
| 1087 | |
| 1088 | /* confirm that this channel is disabled */ |
| 1089 | while (dma_readl(atdma, CHSR) & atchan->mask) |
| 1090 | cpu_relax(); |
| 1091 | |
| 1092 | /* active_list entries will end up before queued entries */ |
| 1093 | list_splice_init(&atchan->queue, &list); |
| 1094 | list_splice_init(&atchan->active_list, &list); |
| 1095 | |
| 1096 | /* Flush all pending and queued descriptors */ |
| 1097 | list_for_each_entry_safe(desc, _desc, &list, desc_node) |
| 1098 | atc_chain_complete(atchan, desc); |
| 1099 | |
| 1100 | clear_bit(ATC_IS_PAUSED, &atchan->status); |
| 1101 | /* if channel dedicated to cyclic operations, free it */ |
| 1102 | clear_bit(ATC_IS_CYCLIC, &atchan->status); |
| 1103 | |
Nicolas Ferre | d8cb04b | 2011-07-27 12:21:28 +0000 | [diff] [blame] | 1104 | spin_unlock_irqrestore(&atchan->lock, flags); |
Nicolas Ferre | beeaa10 | 2012-03-14 12:41:43 +0100 | [diff] [blame] | 1105 | } else if (cmd == DMA_SLAVE_CONFIG) { |
| 1106 | return set_runtime_config(chan, (struct dma_slave_config *)arg); |
Nicolas Ferre | 23b5e3a | 2011-05-06 19:56:52 +0200 | [diff] [blame] | 1107 | } else { |
Linus Walleij | c3635c7 | 2010-03-26 16:44:01 -0700 | [diff] [blame] | 1108 | return -ENXIO; |
Nicolas Ferre | 23b5e3a | 2011-05-06 19:56:52 +0200 | [diff] [blame] | 1109 | } |
Yong Wang | b0ebeb9 | 2010-08-05 10:40:08 +0800 | [diff] [blame] | 1110 | |
Linus Walleij | c3635c7 | 2010-03-26 16:44:01 -0700 | [diff] [blame] | 1111 | return 0; |
Nicolas Ferre | 808347f | 2009-07-22 20:04:45 +0200 | [diff] [blame] | 1112 | } |
| 1113 | |
Nicolas Ferre | dc78baa | 2009-07-03 19:24:33 +0200 | [diff] [blame] | 1114 | /** |
Linus Walleij | 0793448 | 2010-03-26 16:50:49 -0700 | [diff] [blame] | 1115 | * atc_tx_status - poll for transaction completion |
Nicolas Ferre | dc78baa | 2009-07-03 19:24:33 +0200 | [diff] [blame] | 1116 | * @chan: DMA channel |
| 1117 | * @cookie: transaction identifier to check status of |
Linus Walleij | 0793448 | 2010-03-26 16:50:49 -0700 | [diff] [blame] | 1118 | * @txstate: if not %NULL updated with transaction state |
Nicolas Ferre | dc78baa | 2009-07-03 19:24:33 +0200 | [diff] [blame] | 1119 | * |
Linus Walleij | 0793448 | 2010-03-26 16:50:49 -0700 | [diff] [blame] | 1120 | * If @txstate is passed in, upon return it reflect the driver |
Nicolas Ferre | dc78baa | 2009-07-03 19:24:33 +0200 | [diff] [blame] | 1121 | * internal state and can be used with dma_async_is_complete() to check |
| 1122 | * the status of multiple cookies without re-checking hardware state. |
| 1123 | */ |
| 1124 | static enum dma_status |
Linus Walleij | 0793448 | 2010-03-26 16:50:49 -0700 | [diff] [blame] | 1125 | atc_tx_status(struct dma_chan *chan, |
Nicolas Ferre | dc78baa | 2009-07-03 19:24:33 +0200 | [diff] [blame] | 1126 | dma_cookie_t cookie, |
Linus Walleij | 0793448 | 2010-03-26 16:50:49 -0700 | [diff] [blame] | 1127 | struct dma_tx_state *txstate) |
Nicolas Ferre | dc78baa | 2009-07-03 19:24:33 +0200 | [diff] [blame] | 1128 | { |
| 1129 | struct at_dma_chan *atchan = to_at_dma_chan(chan); |
Nicolas Ferre | d8cb04b | 2011-07-27 12:21:28 +0000 | [diff] [blame] | 1130 | unsigned long flags; |
Nicolas Ferre | dc78baa | 2009-07-03 19:24:33 +0200 | [diff] [blame] | 1131 | enum dma_status ret; |
Elen Song | d48de6f | 2013-05-10 11:01:46 +0800 | [diff] [blame] | 1132 | int bytes = 0; |
| 1133 | |
| 1134 | ret = dma_cookie_status(chan, cookie, txstate); |
| 1135 | if (ret == DMA_SUCCESS) |
| 1136 | return ret; |
| 1137 | /* |
| 1138 | * There's no point calculating the residue if there's |
| 1139 | * no txstate to store the value. |
| 1140 | */ |
| 1141 | if (!txstate) |
| 1142 | return DMA_ERROR; |
Nicolas Ferre | dc78baa | 2009-07-03 19:24:33 +0200 | [diff] [blame] | 1143 | |
Nicolas Ferre | d8cb04b | 2011-07-27 12:21:28 +0000 | [diff] [blame] | 1144 | spin_lock_irqsave(&atchan->lock, flags); |
Nicolas Ferre | dc78baa | 2009-07-03 19:24:33 +0200 | [diff] [blame] | 1145 | |
Elen Song | d48de6f | 2013-05-10 11:01:46 +0800 | [diff] [blame] | 1146 | /* Get number of bytes left in the active transactions */ |
| 1147 | bytes = atc_get_bytes_left(chan); |
Russell King - ARM Linux | 96a2af4 | 2012-03-06 22:35:27 +0000 | [diff] [blame] | 1148 | |
Nicolas Ferre | d8cb04b | 2011-07-27 12:21:28 +0000 | [diff] [blame] | 1149 | spin_unlock_irqrestore(&atchan->lock, flags); |
Nicolas Ferre | dc78baa | 2009-07-03 19:24:33 +0200 | [diff] [blame] | 1150 | |
Elen Song | d48de6f | 2013-05-10 11:01:46 +0800 | [diff] [blame] | 1151 | if (unlikely(bytes < 0)) { |
| 1152 | dev_vdbg(chan2dev(chan), "get residual bytes error\n"); |
| 1153 | return DMA_ERROR; |
| 1154 | } else |
| 1155 | dma_set_residue(txstate, bytes); |
Nicolas Ferre | 543aabc | 2011-05-06 19:56:51 +0200 | [diff] [blame] | 1156 | |
Elen Song | d48de6f | 2013-05-10 11:01:46 +0800 | [diff] [blame] | 1157 | dev_vdbg(chan2dev(chan), "tx_status %d: cookie = %d residue = %d\n", |
| 1158 | ret, cookie, bytes); |
Nicolas Ferre | dc78baa | 2009-07-03 19:24:33 +0200 | [diff] [blame] | 1159 | |
| 1160 | return ret; |
| 1161 | } |
| 1162 | |
| 1163 | /** |
| 1164 | * atc_issue_pending - try to finish work |
| 1165 | * @chan: target DMA channel |
| 1166 | */ |
| 1167 | static void atc_issue_pending(struct dma_chan *chan) |
| 1168 | { |
| 1169 | struct at_dma_chan *atchan = to_at_dma_chan(chan); |
Nicolas Ferre | d8cb04b | 2011-07-27 12:21:28 +0000 | [diff] [blame] | 1170 | unsigned long flags; |
Nicolas Ferre | dc78baa | 2009-07-03 19:24:33 +0200 | [diff] [blame] | 1171 | |
| 1172 | dev_vdbg(chan2dev(chan), "issue_pending\n"); |
| 1173 | |
Nicolas Ferre | 53830cc | 2011-04-30 16:57:46 +0200 | [diff] [blame] | 1174 | /* Not needed for cyclic transfers */ |
Nicolas Ferre | 3c47748 | 2011-07-25 21:09:23 +0000 | [diff] [blame] | 1175 | if (atc_chan_is_cyclic(atchan)) |
Nicolas Ferre | 53830cc | 2011-04-30 16:57:46 +0200 | [diff] [blame] | 1176 | return; |
| 1177 | |
Nicolas Ferre | d8cb04b | 2011-07-27 12:21:28 +0000 | [diff] [blame] | 1178 | spin_lock_irqsave(&atchan->lock, flags); |
Ludovic Desroches | d202f05 | 2013-04-18 09:52:59 +0200 | [diff] [blame] | 1179 | atc_advance_work(atchan); |
Nicolas Ferre | d8cb04b | 2011-07-27 12:21:28 +0000 | [diff] [blame] | 1180 | spin_unlock_irqrestore(&atchan->lock, flags); |
Nicolas Ferre | dc78baa | 2009-07-03 19:24:33 +0200 | [diff] [blame] | 1181 | } |
| 1182 | |
| 1183 | /** |
| 1184 | * atc_alloc_chan_resources - allocate resources for DMA channel |
| 1185 | * @chan: allocate descriptor resources for this channel |
| 1186 | * @client: current client requesting the channel be ready for requests |
| 1187 | * |
| 1188 | * return - the number of allocated descriptors |
| 1189 | */ |
| 1190 | static int atc_alloc_chan_resources(struct dma_chan *chan) |
| 1191 | { |
| 1192 | struct at_dma_chan *atchan = to_at_dma_chan(chan); |
| 1193 | struct at_dma *atdma = to_at_dma(chan->device); |
| 1194 | struct at_desc *desc; |
Nicolas Ferre | 808347f | 2009-07-22 20:04:45 +0200 | [diff] [blame] | 1195 | struct at_dma_slave *atslave; |
Nicolas Ferre | d8cb04b | 2011-07-27 12:21:28 +0000 | [diff] [blame] | 1196 | unsigned long flags; |
Nicolas Ferre | dc78baa | 2009-07-03 19:24:33 +0200 | [diff] [blame] | 1197 | int i; |
Nicolas Ferre | 808347f | 2009-07-22 20:04:45 +0200 | [diff] [blame] | 1198 | u32 cfg; |
Nicolas Ferre | dc78baa | 2009-07-03 19:24:33 +0200 | [diff] [blame] | 1199 | LIST_HEAD(tmp_list); |
| 1200 | |
| 1201 | dev_vdbg(chan2dev(chan), "alloc_chan_resources\n"); |
| 1202 | |
| 1203 | /* ASSERT: channel is idle */ |
| 1204 | if (atc_chan_is_enabled(atchan)) { |
| 1205 | dev_dbg(chan2dev(chan), "DMA channel not idle ?\n"); |
| 1206 | return -EIO; |
| 1207 | } |
| 1208 | |
Nicolas Ferre | 808347f | 2009-07-22 20:04:45 +0200 | [diff] [blame] | 1209 | cfg = ATC_DEFAULT_CFG; |
| 1210 | |
| 1211 | atslave = chan->private; |
| 1212 | if (atslave) { |
| 1213 | /* |
| 1214 | * We need controller-specific data to set up slave |
| 1215 | * transfers. |
| 1216 | */ |
| 1217 | BUG_ON(!atslave->dma_dev || atslave->dma_dev != atdma->dma_common.dev); |
| 1218 | |
Nicolas Ferre | ea7e790 | 2013-05-10 15:19:13 +0200 | [diff] [blame] | 1219 | /* if cfg configuration specified take it instead of default */ |
Nicolas Ferre | 808347f | 2009-07-22 20:04:45 +0200 | [diff] [blame] | 1220 | if (atslave->cfg) |
| 1221 | cfg = atslave->cfg; |
| 1222 | } |
| 1223 | |
| 1224 | /* have we already been set up? |
| 1225 | * reconfigure channel but no need to reallocate descriptors */ |
Nicolas Ferre | dc78baa | 2009-07-03 19:24:33 +0200 | [diff] [blame] | 1226 | if (!list_empty(&atchan->free_list)) |
| 1227 | return atchan->descs_allocated; |
| 1228 | |
| 1229 | /* Allocate initial pool of descriptors */ |
| 1230 | for (i = 0; i < init_nr_desc_per_channel; i++) { |
| 1231 | desc = atc_alloc_descriptor(chan, GFP_KERNEL); |
| 1232 | if (!desc) { |
| 1233 | dev_err(atdma->dma_common.dev, |
| 1234 | "Only %d initial descriptors\n", i); |
| 1235 | break; |
| 1236 | } |
| 1237 | list_add_tail(&desc->desc_node, &tmp_list); |
| 1238 | } |
| 1239 | |
Nicolas Ferre | d8cb04b | 2011-07-27 12:21:28 +0000 | [diff] [blame] | 1240 | spin_lock_irqsave(&atchan->lock, flags); |
Nicolas Ferre | dc78baa | 2009-07-03 19:24:33 +0200 | [diff] [blame] | 1241 | atchan->descs_allocated = i; |
Elen Song | d48de6f | 2013-05-10 11:01:46 +0800 | [diff] [blame] | 1242 | atchan->remain_desc = 0; |
Nicolas Ferre | dc78baa | 2009-07-03 19:24:33 +0200 | [diff] [blame] | 1243 | list_splice(&tmp_list, &atchan->free_list); |
Russell King - ARM Linux | d3ee98cdc | 2012-03-06 22:35:47 +0000 | [diff] [blame] | 1244 | dma_cookie_init(chan); |
Nicolas Ferre | d8cb04b | 2011-07-27 12:21:28 +0000 | [diff] [blame] | 1245 | spin_unlock_irqrestore(&atchan->lock, flags); |
Nicolas Ferre | dc78baa | 2009-07-03 19:24:33 +0200 | [diff] [blame] | 1246 | |
| 1247 | /* channel parameters */ |
Nicolas Ferre | 808347f | 2009-07-22 20:04:45 +0200 | [diff] [blame] | 1248 | channel_writel(atchan, CFG, cfg); |
Nicolas Ferre | dc78baa | 2009-07-03 19:24:33 +0200 | [diff] [blame] | 1249 | |
| 1250 | dev_dbg(chan2dev(chan), |
| 1251 | "alloc_chan_resources: allocated %d descriptors\n", |
| 1252 | atchan->descs_allocated); |
| 1253 | |
| 1254 | return atchan->descs_allocated; |
| 1255 | } |
| 1256 | |
| 1257 | /** |
| 1258 | * atc_free_chan_resources - free all channel resources |
| 1259 | * @chan: DMA channel |
| 1260 | */ |
| 1261 | static void atc_free_chan_resources(struct dma_chan *chan) |
| 1262 | { |
| 1263 | struct at_dma_chan *atchan = to_at_dma_chan(chan); |
| 1264 | struct at_dma *atdma = to_at_dma(chan->device); |
| 1265 | struct at_desc *desc, *_desc; |
| 1266 | LIST_HEAD(list); |
| 1267 | |
| 1268 | dev_dbg(chan2dev(chan), "free_chan_resources: (descs allocated=%u)\n", |
| 1269 | atchan->descs_allocated); |
| 1270 | |
| 1271 | /* ASSERT: channel is idle */ |
| 1272 | BUG_ON(!list_empty(&atchan->active_list)); |
| 1273 | BUG_ON(!list_empty(&atchan->queue)); |
| 1274 | BUG_ON(atc_chan_is_enabled(atchan)); |
| 1275 | |
| 1276 | list_for_each_entry_safe(desc, _desc, &atchan->free_list, desc_node) { |
| 1277 | dev_vdbg(chan2dev(chan), " freeing descriptor %p\n", desc); |
| 1278 | list_del(&desc->desc_node); |
| 1279 | /* free link descriptor */ |
| 1280 | dma_pool_free(atdma->dma_desc_pool, desc, desc->txd.phys); |
| 1281 | } |
| 1282 | list_splice_init(&atchan->free_list, &list); |
| 1283 | atchan->descs_allocated = 0; |
Nicolas Ferre | 53830cc | 2011-04-30 16:57:46 +0200 | [diff] [blame] | 1284 | atchan->status = 0; |
Elen Song | d48de6f | 2013-05-10 11:01:46 +0800 | [diff] [blame] | 1285 | atchan->remain_desc = 0; |
Nicolas Ferre | dc78baa | 2009-07-03 19:24:33 +0200 | [diff] [blame] | 1286 | |
| 1287 | dev_vdbg(chan2dev(chan), "free_chan_resources: done\n"); |
| 1288 | } |
| 1289 | |
Ludovic Desroches | bbe89c8 | 2013-04-19 09:11:18 +0000 | [diff] [blame] | 1290 | #ifdef CONFIG_OF |
| 1291 | static bool at_dma_filter(struct dma_chan *chan, void *slave) |
| 1292 | { |
| 1293 | struct at_dma_slave *atslave = slave; |
| 1294 | |
| 1295 | if (atslave->dma_dev == chan->device->dev) { |
| 1296 | chan->private = atslave; |
| 1297 | return true; |
| 1298 | } else { |
| 1299 | return false; |
| 1300 | } |
| 1301 | } |
| 1302 | |
| 1303 | static struct dma_chan *at_dma_xlate(struct of_phandle_args *dma_spec, |
| 1304 | struct of_dma *of_dma) |
| 1305 | { |
| 1306 | struct dma_chan *chan; |
| 1307 | struct at_dma_chan *atchan; |
| 1308 | struct at_dma_slave *atslave; |
| 1309 | dma_cap_mask_t mask; |
| 1310 | unsigned int per_id; |
| 1311 | struct platform_device *dmac_pdev; |
| 1312 | |
| 1313 | if (dma_spec->args_count != 2) |
| 1314 | return NULL; |
| 1315 | |
| 1316 | dmac_pdev = of_find_device_by_node(dma_spec->np); |
| 1317 | |
| 1318 | dma_cap_zero(mask); |
| 1319 | dma_cap_set(DMA_SLAVE, mask); |
| 1320 | |
| 1321 | atslave = devm_kzalloc(&dmac_pdev->dev, sizeof(*atslave), GFP_KERNEL); |
| 1322 | if (!atslave) |
| 1323 | return NULL; |
Ludovic Desroches | 62971b2 | 2013-06-13 10:39:39 +0200 | [diff] [blame^] | 1324 | |
| 1325 | atslave->cfg = ATC_DST_H2SEL_HW | ATC_SRC_H2SEL_HW; |
Ludovic Desroches | bbe89c8 | 2013-04-19 09:11:18 +0000 | [diff] [blame] | 1326 | /* |
| 1327 | * We can fill both SRC_PER and DST_PER, one of these fields will be |
| 1328 | * ignored depending on DMA transfer direction. |
| 1329 | */ |
Ludovic Desroches | 62971b2 | 2013-06-13 10:39:39 +0200 | [diff] [blame^] | 1330 | per_id = dma_spec->args[1] & AT91_DMA_CFG_PER_ID_MASK; |
| 1331 | atslave->cfg |= ATC_DST_PER_MSB(per_id) | ATC_DST_PER(per_id) |
Nicolas Ferre | 6c22770 | 2013-05-10 15:19:15 +0200 | [diff] [blame] | 1332 | | ATC_SRC_PER_MSB(per_id) | ATC_SRC_PER(per_id); |
Ludovic Desroches | 62971b2 | 2013-06-13 10:39:39 +0200 | [diff] [blame^] | 1333 | /* |
| 1334 | * We have to translate the value we get from the device tree since |
| 1335 | * the half FIFO configuration value had to be 0 to keep backward |
| 1336 | * compatibility. |
| 1337 | */ |
| 1338 | switch (dma_spec->args[1] & AT91_DMA_CFG_FIFOCFG_MASK) { |
| 1339 | case AT91_DMA_CFG_FIFOCFG_ALAP: |
| 1340 | atslave->cfg |= ATC_FIFOCFG_LARGESTBURST; |
| 1341 | break; |
| 1342 | case AT91_DMA_CFG_FIFOCFG_ASAP: |
| 1343 | atslave->cfg |= ATC_FIFOCFG_ENOUGHSPACE; |
| 1344 | break; |
| 1345 | case AT91_DMA_CFG_FIFOCFG_HALF: |
| 1346 | default: |
| 1347 | atslave->cfg |= ATC_FIFOCFG_HALFFIFO; |
| 1348 | } |
Ludovic Desroches | bbe89c8 | 2013-04-19 09:11:18 +0000 | [diff] [blame] | 1349 | atslave->dma_dev = &dmac_pdev->dev; |
| 1350 | |
| 1351 | chan = dma_request_channel(mask, at_dma_filter, atslave); |
| 1352 | if (!chan) |
| 1353 | return NULL; |
| 1354 | |
| 1355 | atchan = to_at_dma_chan(chan); |
| 1356 | atchan->per_if = dma_spec->args[0] & 0xff; |
| 1357 | atchan->mem_if = (dma_spec->args[0] >> 16) & 0xff; |
| 1358 | |
| 1359 | return chan; |
| 1360 | } |
| 1361 | #else |
| 1362 | static struct dma_chan *at_dma_xlate(struct of_phandle_args *dma_spec, |
| 1363 | struct of_dma *of_dma) |
| 1364 | { |
| 1365 | return NULL; |
| 1366 | } |
| 1367 | #endif |
Nicolas Ferre | dc78baa | 2009-07-03 19:24:33 +0200 | [diff] [blame] | 1368 | |
| 1369 | /*-- Module Management -----------------------------------------------*/ |
| 1370 | |
Nicolas Ferre | 02f88be | 2011-11-22 11:55:54 +0100 | [diff] [blame] | 1371 | /* cap_mask is a multi-u32 bitfield, fill it with proper C code. */ |
| 1372 | static struct at_dma_platform_data at91sam9rl_config = { |
| 1373 | .nr_channels = 2, |
| 1374 | }; |
| 1375 | static struct at_dma_platform_data at91sam9g45_config = { |
| 1376 | .nr_channels = 8, |
| 1377 | }; |
| 1378 | |
Nicolas Ferre | c511595 | 2011-10-17 14:56:41 +0200 | [diff] [blame] | 1379 | #if defined(CONFIG_OF) |
| 1380 | static const struct of_device_id atmel_dma_dt_ids[] = { |
| 1381 | { |
| 1382 | .compatible = "atmel,at91sam9rl-dma", |
Nicolas Ferre | 02f88be | 2011-11-22 11:55:54 +0100 | [diff] [blame] | 1383 | .data = &at91sam9rl_config, |
Nicolas Ferre | c511595 | 2011-10-17 14:56:41 +0200 | [diff] [blame] | 1384 | }, { |
| 1385 | .compatible = "atmel,at91sam9g45-dma", |
Nicolas Ferre | 02f88be | 2011-11-22 11:55:54 +0100 | [diff] [blame] | 1386 | .data = &at91sam9g45_config, |
Nicolas Ferre | dcc8173 | 2011-11-22 11:55:53 +0100 | [diff] [blame] | 1387 | }, { |
| 1388 | /* sentinel */ |
| 1389 | } |
Nicolas Ferre | c511595 | 2011-10-17 14:56:41 +0200 | [diff] [blame] | 1390 | }; |
| 1391 | |
| 1392 | MODULE_DEVICE_TABLE(of, atmel_dma_dt_ids); |
| 1393 | #endif |
| 1394 | |
Nicolas Ferre | 0ab88a0 | 2011-11-22 11:55:52 +0100 | [diff] [blame] | 1395 | static const struct platform_device_id atdma_devtypes[] = { |
Nicolas Ferre | 6734845 | 2011-10-17 14:56:40 +0200 | [diff] [blame] | 1396 | { |
| 1397 | .name = "at91sam9rl_dma", |
Nicolas Ferre | 02f88be | 2011-11-22 11:55:54 +0100 | [diff] [blame] | 1398 | .driver_data = (unsigned long) &at91sam9rl_config, |
Nicolas Ferre | 6734845 | 2011-10-17 14:56:40 +0200 | [diff] [blame] | 1399 | }, { |
| 1400 | .name = "at91sam9g45_dma", |
Nicolas Ferre | 02f88be | 2011-11-22 11:55:54 +0100 | [diff] [blame] | 1401 | .driver_data = (unsigned long) &at91sam9g45_config, |
Nicolas Ferre | 6734845 | 2011-10-17 14:56:40 +0200 | [diff] [blame] | 1402 | }, { |
| 1403 | /* sentinel */ |
| 1404 | } |
| 1405 | }; |
| 1406 | |
Uwe Kleine-König | 7fd63cc | 2012-07-13 14:32:10 +0200 | [diff] [blame] | 1407 | static inline const struct at_dma_platform_data * __init at_dma_get_driver_data( |
Nicolas Ferre | 02f88be | 2011-11-22 11:55:54 +0100 | [diff] [blame] | 1408 | struct platform_device *pdev) |
Nicolas Ferre | c511595 | 2011-10-17 14:56:41 +0200 | [diff] [blame] | 1409 | { |
| 1410 | if (pdev->dev.of_node) { |
| 1411 | const struct of_device_id *match; |
| 1412 | match = of_match_node(atmel_dma_dt_ids, pdev->dev.of_node); |
| 1413 | if (match == NULL) |
Nicolas Ferre | 02f88be | 2011-11-22 11:55:54 +0100 | [diff] [blame] | 1414 | return NULL; |
| 1415 | return match->data; |
Nicolas Ferre | c511595 | 2011-10-17 14:56:41 +0200 | [diff] [blame] | 1416 | } |
Nicolas Ferre | 02f88be | 2011-11-22 11:55:54 +0100 | [diff] [blame] | 1417 | return (struct at_dma_platform_data *) |
| 1418 | platform_get_device_id(pdev)->driver_data; |
Nicolas Ferre | c511595 | 2011-10-17 14:56:41 +0200 | [diff] [blame] | 1419 | } |
| 1420 | |
Nicolas Ferre | dc78baa | 2009-07-03 19:24:33 +0200 | [diff] [blame] | 1421 | /** |
| 1422 | * at_dma_off - disable DMA controller |
| 1423 | * @atdma: the Atmel HDAMC device |
| 1424 | */ |
| 1425 | static void at_dma_off(struct at_dma *atdma) |
| 1426 | { |
| 1427 | dma_writel(atdma, EN, 0); |
| 1428 | |
| 1429 | /* disable all interrupts */ |
| 1430 | dma_writel(atdma, EBCIDR, -1L); |
| 1431 | |
| 1432 | /* confirm that all channels are disabled */ |
| 1433 | while (dma_readl(atdma, CHSR) & atdma->all_chan_mask) |
| 1434 | cpu_relax(); |
| 1435 | } |
| 1436 | |
| 1437 | static int __init at_dma_probe(struct platform_device *pdev) |
| 1438 | { |
Nicolas Ferre | dc78baa | 2009-07-03 19:24:33 +0200 | [diff] [blame] | 1439 | struct resource *io; |
| 1440 | struct at_dma *atdma; |
| 1441 | size_t size; |
| 1442 | int irq; |
| 1443 | int err; |
| 1444 | int i; |
Uwe Kleine-König | 7fd63cc | 2012-07-13 14:32:10 +0200 | [diff] [blame] | 1445 | const struct at_dma_platform_data *plat_dat; |
Nicolas Ferre | dc78baa | 2009-07-03 19:24:33 +0200 | [diff] [blame] | 1446 | |
Nicolas Ferre | 02f88be | 2011-11-22 11:55:54 +0100 | [diff] [blame] | 1447 | /* setup platform data for each SoC */ |
| 1448 | dma_cap_set(DMA_MEMCPY, at91sam9rl_config.cap_mask); |
| 1449 | dma_cap_set(DMA_MEMCPY, at91sam9g45_config.cap_mask); |
| 1450 | dma_cap_set(DMA_SLAVE, at91sam9g45_config.cap_mask); |
Nicolas Ferre | 6734845 | 2011-10-17 14:56:40 +0200 | [diff] [blame] | 1451 | |
| 1452 | /* get DMA parameters from controller type */ |
Nicolas Ferre | 02f88be | 2011-11-22 11:55:54 +0100 | [diff] [blame] | 1453 | plat_dat = at_dma_get_driver_data(pdev); |
| 1454 | if (!plat_dat) |
| 1455 | return -ENODEV; |
Nicolas Ferre | dc78baa | 2009-07-03 19:24:33 +0200 | [diff] [blame] | 1456 | |
| 1457 | io = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 1458 | if (!io) |
| 1459 | return -EINVAL; |
| 1460 | |
| 1461 | irq = platform_get_irq(pdev, 0); |
| 1462 | if (irq < 0) |
| 1463 | return irq; |
| 1464 | |
| 1465 | size = sizeof(struct at_dma); |
Nicolas Ferre | 02f88be | 2011-11-22 11:55:54 +0100 | [diff] [blame] | 1466 | size += plat_dat->nr_channels * sizeof(struct at_dma_chan); |
Nicolas Ferre | dc78baa | 2009-07-03 19:24:33 +0200 | [diff] [blame] | 1467 | atdma = kzalloc(size, GFP_KERNEL); |
| 1468 | if (!atdma) |
| 1469 | return -ENOMEM; |
| 1470 | |
Nicolas Ferre | 6734845 | 2011-10-17 14:56:40 +0200 | [diff] [blame] | 1471 | /* discover transaction capabilities */ |
Nicolas Ferre | 02f88be | 2011-11-22 11:55:54 +0100 | [diff] [blame] | 1472 | atdma->dma_common.cap_mask = plat_dat->cap_mask; |
| 1473 | atdma->all_chan_mask = (1 << plat_dat->nr_channels) - 1; |
Nicolas Ferre | dc78baa | 2009-07-03 19:24:33 +0200 | [diff] [blame] | 1474 | |
H Hartley Sweeten | 114df7d | 2011-06-01 15:16:09 -0700 | [diff] [blame] | 1475 | size = resource_size(io); |
Nicolas Ferre | dc78baa | 2009-07-03 19:24:33 +0200 | [diff] [blame] | 1476 | if (!request_mem_region(io->start, size, pdev->dev.driver->name)) { |
| 1477 | err = -EBUSY; |
| 1478 | goto err_kfree; |
| 1479 | } |
| 1480 | |
| 1481 | atdma->regs = ioremap(io->start, size); |
| 1482 | if (!atdma->regs) { |
| 1483 | err = -ENOMEM; |
| 1484 | goto err_release_r; |
| 1485 | } |
| 1486 | |
| 1487 | atdma->clk = clk_get(&pdev->dev, "dma_clk"); |
| 1488 | if (IS_ERR(atdma->clk)) { |
| 1489 | err = PTR_ERR(atdma->clk); |
| 1490 | goto err_clk; |
| 1491 | } |
| 1492 | clk_enable(atdma->clk); |
| 1493 | |
| 1494 | /* force dma off, just in case */ |
| 1495 | at_dma_off(atdma); |
| 1496 | |
| 1497 | err = request_irq(irq, at_dma_interrupt, 0, "at_hdmac", atdma); |
| 1498 | if (err) |
| 1499 | goto err_irq; |
| 1500 | |
| 1501 | platform_set_drvdata(pdev, atdma); |
| 1502 | |
| 1503 | /* create a pool of consistent memory blocks for hardware descriptors */ |
| 1504 | atdma->dma_desc_pool = dma_pool_create("at_hdmac_desc_pool", |
| 1505 | &pdev->dev, sizeof(struct at_desc), |
| 1506 | 4 /* word alignment */, 0); |
| 1507 | if (!atdma->dma_desc_pool) { |
| 1508 | dev_err(&pdev->dev, "No memory for descriptors dma pool\n"); |
| 1509 | err = -ENOMEM; |
| 1510 | goto err_pool_create; |
| 1511 | } |
| 1512 | |
| 1513 | /* clear any pending interrupt */ |
| 1514 | while (dma_readl(atdma, EBCISR)) |
| 1515 | cpu_relax(); |
| 1516 | |
| 1517 | /* initialize channels related values */ |
| 1518 | INIT_LIST_HEAD(&atdma->dma_common.channels); |
Nicolas Ferre | 02f88be | 2011-11-22 11:55:54 +0100 | [diff] [blame] | 1519 | for (i = 0; i < plat_dat->nr_channels; i++) { |
Nicolas Ferre | dc78baa | 2009-07-03 19:24:33 +0200 | [diff] [blame] | 1520 | struct at_dma_chan *atchan = &atdma->chan[i]; |
| 1521 | |
Ludovic Desroches | bbe89c8 | 2013-04-19 09:11:18 +0000 | [diff] [blame] | 1522 | atchan->mem_if = AT_DMA_MEM_IF; |
| 1523 | atchan->per_if = AT_DMA_PER_IF; |
Nicolas Ferre | dc78baa | 2009-07-03 19:24:33 +0200 | [diff] [blame] | 1524 | atchan->chan_common.device = &atdma->dma_common; |
Russell King - ARM Linux | d3ee98cdc | 2012-03-06 22:35:47 +0000 | [diff] [blame] | 1525 | dma_cookie_init(&atchan->chan_common); |
Nicolas Ferre | dc78baa | 2009-07-03 19:24:33 +0200 | [diff] [blame] | 1526 | list_add_tail(&atchan->chan_common.device_node, |
| 1527 | &atdma->dma_common.channels); |
| 1528 | |
| 1529 | atchan->ch_regs = atdma->regs + ch_regs(i); |
| 1530 | spin_lock_init(&atchan->lock); |
| 1531 | atchan->mask = 1 << i; |
| 1532 | |
| 1533 | INIT_LIST_HEAD(&atchan->active_list); |
| 1534 | INIT_LIST_HEAD(&atchan->queue); |
| 1535 | INIT_LIST_HEAD(&atchan->free_list); |
| 1536 | |
| 1537 | tasklet_init(&atchan->tasklet, atc_tasklet, |
| 1538 | (unsigned long)atchan); |
Nikolaus Voss | bda3a47 | 2012-01-17 10:28:33 +0100 | [diff] [blame] | 1539 | atc_enable_chan_irq(atdma, i); |
Nicolas Ferre | dc78baa | 2009-07-03 19:24:33 +0200 | [diff] [blame] | 1540 | } |
| 1541 | |
| 1542 | /* set base routines */ |
| 1543 | atdma->dma_common.device_alloc_chan_resources = atc_alloc_chan_resources; |
| 1544 | atdma->dma_common.device_free_chan_resources = atc_free_chan_resources; |
Linus Walleij | 0793448 | 2010-03-26 16:50:49 -0700 | [diff] [blame] | 1545 | atdma->dma_common.device_tx_status = atc_tx_status; |
Nicolas Ferre | dc78baa | 2009-07-03 19:24:33 +0200 | [diff] [blame] | 1546 | atdma->dma_common.device_issue_pending = atc_issue_pending; |
| 1547 | atdma->dma_common.dev = &pdev->dev; |
| 1548 | |
| 1549 | /* set prep routines based on capability */ |
| 1550 | if (dma_has_cap(DMA_MEMCPY, atdma->dma_common.cap_mask)) |
| 1551 | atdma->dma_common.device_prep_dma_memcpy = atc_prep_dma_memcpy; |
| 1552 | |
Nicolas Ferre | d7db808 | 2011-08-05 11:43:44 +0000 | [diff] [blame] | 1553 | if (dma_has_cap(DMA_SLAVE, atdma->dma_common.cap_mask)) { |
Nicolas Ferre | 808347f | 2009-07-22 20:04:45 +0200 | [diff] [blame] | 1554 | atdma->dma_common.device_prep_slave_sg = atc_prep_slave_sg; |
Nicolas Ferre | d7db808 | 2011-08-05 11:43:44 +0000 | [diff] [blame] | 1555 | /* controller can do slave DMA: can trigger cyclic transfers */ |
| 1556 | dma_cap_set(DMA_CYCLIC, atdma->dma_common.cap_mask); |
Nicolas Ferre | 53830cc | 2011-04-30 16:57:46 +0200 | [diff] [blame] | 1557 | atdma->dma_common.device_prep_dma_cyclic = atc_prep_dma_cyclic; |
Linus Walleij | c3635c7 | 2010-03-26 16:44:01 -0700 | [diff] [blame] | 1558 | atdma->dma_common.device_control = atc_control; |
Nicolas Ferre | d7db808 | 2011-08-05 11:43:44 +0000 | [diff] [blame] | 1559 | } |
Nicolas Ferre | 808347f | 2009-07-22 20:04:45 +0200 | [diff] [blame] | 1560 | |
Nicolas Ferre | dc78baa | 2009-07-03 19:24:33 +0200 | [diff] [blame] | 1561 | dma_writel(atdma, EN, AT_DMA_ENABLE); |
| 1562 | |
| 1563 | dev_info(&pdev->dev, "Atmel AHB DMA Controller ( %s%s), %d channels\n", |
| 1564 | dma_has_cap(DMA_MEMCPY, atdma->dma_common.cap_mask) ? "cpy " : "", |
| 1565 | dma_has_cap(DMA_SLAVE, atdma->dma_common.cap_mask) ? "slave " : "", |
Nicolas Ferre | 02f88be | 2011-11-22 11:55:54 +0100 | [diff] [blame] | 1566 | plat_dat->nr_channels); |
Nicolas Ferre | dc78baa | 2009-07-03 19:24:33 +0200 | [diff] [blame] | 1567 | |
| 1568 | dma_async_device_register(&atdma->dma_common); |
| 1569 | |
Ludovic Desroches | bbe89c8 | 2013-04-19 09:11:18 +0000 | [diff] [blame] | 1570 | /* |
| 1571 | * Do not return an error if the dmac node is not present in order to |
| 1572 | * not break the existing way of requesting channel with |
| 1573 | * dma_request_channel(). |
| 1574 | */ |
| 1575 | if (pdev->dev.of_node) { |
| 1576 | err = of_dma_controller_register(pdev->dev.of_node, |
| 1577 | at_dma_xlate, atdma); |
| 1578 | if (err) { |
| 1579 | dev_err(&pdev->dev, "could not register of_dma_controller\n"); |
| 1580 | goto err_of_dma_controller_register; |
| 1581 | } |
| 1582 | } |
| 1583 | |
Nicolas Ferre | dc78baa | 2009-07-03 19:24:33 +0200 | [diff] [blame] | 1584 | return 0; |
| 1585 | |
Ludovic Desroches | bbe89c8 | 2013-04-19 09:11:18 +0000 | [diff] [blame] | 1586 | err_of_dma_controller_register: |
| 1587 | dma_async_device_unregister(&atdma->dma_common); |
| 1588 | dma_pool_destroy(atdma->dma_desc_pool); |
Nicolas Ferre | dc78baa | 2009-07-03 19:24:33 +0200 | [diff] [blame] | 1589 | err_pool_create: |
Nicolas Ferre | dc78baa | 2009-07-03 19:24:33 +0200 | [diff] [blame] | 1590 | free_irq(platform_get_irq(pdev, 0), atdma); |
| 1591 | err_irq: |
| 1592 | clk_disable(atdma->clk); |
| 1593 | clk_put(atdma->clk); |
| 1594 | err_clk: |
| 1595 | iounmap(atdma->regs); |
| 1596 | atdma->regs = NULL; |
| 1597 | err_release_r: |
| 1598 | release_mem_region(io->start, size); |
| 1599 | err_kfree: |
| 1600 | kfree(atdma); |
| 1601 | return err; |
| 1602 | } |
| 1603 | |
Maxin B. John | 1d1bbd3 | 2013-02-20 02:07:04 +0200 | [diff] [blame] | 1604 | static int at_dma_remove(struct platform_device *pdev) |
Nicolas Ferre | dc78baa | 2009-07-03 19:24:33 +0200 | [diff] [blame] | 1605 | { |
| 1606 | struct at_dma *atdma = platform_get_drvdata(pdev); |
| 1607 | struct dma_chan *chan, *_chan; |
| 1608 | struct resource *io; |
| 1609 | |
| 1610 | at_dma_off(atdma); |
| 1611 | dma_async_device_unregister(&atdma->dma_common); |
| 1612 | |
| 1613 | dma_pool_destroy(atdma->dma_desc_pool); |
Nicolas Ferre | dc78baa | 2009-07-03 19:24:33 +0200 | [diff] [blame] | 1614 | free_irq(platform_get_irq(pdev, 0), atdma); |
| 1615 | |
| 1616 | list_for_each_entry_safe(chan, _chan, &atdma->dma_common.channels, |
| 1617 | device_node) { |
| 1618 | struct at_dma_chan *atchan = to_at_dma_chan(chan); |
| 1619 | |
| 1620 | /* Disable interrupts */ |
Nikolaus Voss | bda3a47 | 2012-01-17 10:28:33 +0100 | [diff] [blame] | 1621 | atc_disable_chan_irq(atdma, chan->chan_id); |
Nicolas Ferre | dc78baa | 2009-07-03 19:24:33 +0200 | [diff] [blame] | 1622 | tasklet_disable(&atchan->tasklet); |
| 1623 | |
| 1624 | tasklet_kill(&atchan->tasklet); |
| 1625 | list_del(&chan->device_node); |
| 1626 | } |
| 1627 | |
| 1628 | clk_disable(atdma->clk); |
| 1629 | clk_put(atdma->clk); |
| 1630 | |
| 1631 | iounmap(atdma->regs); |
| 1632 | atdma->regs = NULL; |
| 1633 | |
| 1634 | io = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
H Hartley Sweeten | 114df7d | 2011-06-01 15:16:09 -0700 | [diff] [blame] | 1635 | release_mem_region(io->start, resource_size(io)); |
Nicolas Ferre | dc78baa | 2009-07-03 19:24:33 +0200 | [diff] [blame] | 1636 | |
| 1637 | kfree(atdma); |
| 1638 | |
| 1639 | return 0; |
| 1640 | } |
| 1641 | |
| 1642 | static void at_dma_shutdown(struct platform_device *pdev) |
| 1643 | { |
| 1644 | struct at_dma *atdma = platform_get_drvdata(pdev); |
| 1645 | |
| 1646 | at_dma_off(platform_get_drvdata(pdev)); |
| 1647 | clk_disable(atdma->clk); |
| 1648 | } |
| 1649 | |
Nicolas Ferre | c0ba594 | 2011-07-27 12:21:29 +0000 | [diff] [blame] | 1650 | static int at_dma_prepare(struct device *dev) |
| 1651 | { |
| 1652 | struct platform_device *pdev = to_platform_device(dev); |
| 1653 | struct at_dma *atdma = platform_get_drvdata(pdev); |
| 1654 | struct dma_chan *chan, *_chan; |
| 1655 | |
| 1656 | list_for_each_entry_safe(chan, _chan, &atdma->dma_common.channels, |
| 1657 | device_node) { |
| 1658 | struct at_dma_chan *atchan = to_at_dma_chan(chan); |
| 1659 | /* wait for transaction completion (except in cyclic case) */ |
Nicolas Ferre | 3c47748 | 2011-07-25 21:09:23 +0000 | [diff] [blame] | 1660 | if (atc_chan_is_enabled(atchan) && !atc_chan_is_cyclic(atchan)) |
Nicolas Ferre | c0ba594 | 2011-07-27 12:21:29 +0000 | [diff] [blame] | 1661 | return -EAGAIN; |
| 1662 | } |
| 1663 | return 0; |
| 1664 | } |
| 1665 | |
| 1666 | static void atc_suspend_cyclic(struct at_dma_chan *atchan) |
| 1667 | { |
| 1668 | struct dma_chan *chan = &atchan->chan_common; |
| 1669 | |
| 1670 | /* Channel should be paused by user |
| 1671 | * do it anyway even if it is not done already */ |
Nicolas Ferre | 3c47748 | 2011-07-25 21:09:23 +0000 | [diff] [blame] | 1672 | if (!atc_chan_is_paused(atchan)) { |
Nicolas Ferre | c0ba594 | 2011-07-27 12:21:29 +0000 | [diff] [blame] | 1673 | dev_warn(chan2dev(chan), |
| 1674 | "cyclic channel not paused, should be done by channel user\n"); |
| 1675 | atc_control(chan, DMA_PAUSE, 0); |
| 1676 | } |
| 1677 | |
| 1678 | /* now preserve additional data for cyclic operations */ |
| 1679 | /* next descriptor address in the cyclic list */ |
| 1680 | atchan->save_dscr = channel_readl(atchan, DSCR); |
| 1681 | |
| 1682 | vdbg_dump_regs(atchan); |
| 1683 | } |
| 1684 | |
Dan Williams | 33f82d1 | 2009-09-10 00:06:44 +0200 | [diff] [blame] | 1685 | static int at_dma_suspend_noirq(struct device *dev) |
Nicolas Ferre | dc78baa | 2009-07-03 19:24:33 +0200 | [diff] [blame] | 1686 | { |
Dan Williams | 33f82d1 | 2009-09-10 00:06:44 +0200 | [diff] [blame] | 1687 | struct platform_device *pdev = to_platform_device(dev); |
| 1688 | struct at_dma *atdma = platform_get_drvdata(pdev); |
Nicolas Ferre | c0ba594 | 2011-07-27 12:21:29 +0000 | [diff] [blame] | 1689 | struct dma_chan *chan, *_chan; |
Nicolas Ferre | dc78baa | 2009-07-03 19:24:33 +0200 | [diff] [blame] | 1690 | |
Nicolas Ferre | c0ba594 | 2011-07-27 12:21:29 +0000 | [diff] [blame] | 1691 | /* preserve data */ |
| 1692 | list_for_each_entry_safe(chan, _chan, &atdma->dma_common.channels, |
| 1693 | device_node) { |
| 1694 | struct at_dma_chan *atchan = to_at_dma_chan(chan); |
| 1695 | |
Nicolas Ferre | 3c47748 | 2011-07-25 21:09:23 +0000 | [diff] [blame] | 1696 | if (atc_chan_is_cyclic(atchan)) |
Nicolas Ferre | c0ba594 | 2011-07-27 12:21:29 +0000 | [diff] [blame] | 1697 | atc_suspend_cyclic(atchan); |
| 1698 | atchan->save_cfg = channel_readl(atchan, CFG); |
| 1699 | } |
| 1700 | atdma->save_imr = dma_readl(atdma, EBCIMR); |
| 1701 | |
| 1702 | /* disable DMA controller */ |
| 1703 | at_dma_off(atdma); |
Nicolas Ferre | dc78baa | 2009-07-03 19:24:33 +0200 | [diff] [blame] | 1704 | clk_disable(atdma->clk); |
| 1705 | return 0; |
| 1706 | } |
| 1707 | |
Nicolas Ferre | c0ba594 | 2011-07-27 12:21:29 +0000 | [diff] [blame] | 1708 | static void atc_resume_cyclic(struct at_dma_chan *atchan) |
| 1709 | { |
| 1710 | struct at_dma *atdma = to_at_dma(atchan->chan_common.device); |
| 1711 | |
| 1712 | /* restore channel status for cyclic descriptors list: |
| 1713 | * next descriptor in the cyclic list at the time of suspend */ |
| 1714 | channel_writel(atchan, SADDR, 0); |
| 1715 | channel_writel(atchan, DADDR, 0); |
| 1716 | channel_writel(atchan, CTRLA, 0); |
| 1717 | channel_writel(atchan, CTRLB, 0); |
| 1718 | channel_writel(atchan, DSCR, atchan->save_dscr); |
| 1719 | dma_writel(atdma, CHER, atchan->mask); |
| 1720 | |
| 1721 | /* channel pause status should be removed by channel user |
| 1722 | * We cannot take the initiative to do it here */ |
| 1723 | |
| 1724 | vdbg_dump_regs(atchan); |
| 1725 | } |
| 1726 | |
Dan Williams | 33f82d1 | 2009-09-10 00:06:44 +0200 | [diff] [blame] | 1727 | static int at_dma_resume_noirq(struct device *dev) |
Nicolas Ferre | dc78baa | 2009-07-03 19:24:33 +0200 | [diff] [blame] | 1728 | { |
Dan Williams | 33f82d1 | 2009-09-10 00:06:44 +0200 | [diff] [blame] | 1729 | struct platform_device *pdev = to_platform_device(dev); |
| 1730 | struct at_dma *atdma = platform_get_drvdata(pdev); |
Nicolas Ferre | c0ba594 | 2011-07-27 12:21:29 +0000 | [diff] [blame] | 1731 | struct dma_chan *chan, *_chan; |
Nicolas Ferre | dc78baa | 2009-07-03 19:24:33 +0200 | [diff] [blame] | 1732 | |
Nicolas Ferre | c0ba594 | 2011-07-27 12:21:29 +0000 | [diff] [blame] | 1733 | /* bring back DMA controller */ |
Nicolas Ferre | dc78baa | 2009-07-03 19:24:33 +0200 | [diff] [blame] | 1734 | clk_enable(atdma->clk); |
| 1735 | dma_writel(atdma, EN, AT_DMA_ENABLE); |
Nicolas Ferre | c0ba594 | 2011-07-27 12:21:29 +0000 | [diff] [blame] | 1736 | |
| 1737 | /* clear any pending interrupt */ |
| 1738 | while (dma_readl(atdma, EBCISR)) |
| 1739 | cpu_relax(); |
| 1740 | |
| 1741 | /* restore saved data */ |
| 1742 | dma_writel(atdma, EBCIER, atdma->save_imr); |
| 1743 | list_for_each_entry_safe(chan, _chan, &atdma->dma_common.channels, |
| 1744 | device_node) { |
| 1745 | struct at_dma_chan *atchan = to_at_dma_chan(chan); |
| 1746 | |
| 1747 | channel_writel(atchan, CFG, atchan->save_cfg); |
Nicolas Ferre | 3c47748 | 2011-07-25 21:09:23 +0000 | [diff] [blame] | 1748 | if (atc_chan_is_cyclic(atchan)) |
Nicolas Ferre | c0ba594 | 2011-07-27 12:21:29 +0000 | [diff] [blame] | 1749 | atc_resume_cyclic(atchan); |
| 1750 | } |
Nicolas Ferre | dc78baa | 2009-07-03 19:24:33 +0200 | [diff] [blame] | 1751 | return 0; |
Nicolas Ferre | dc78baa | 2009-07-03 19:24:33 +0200 | [diff] [blame] | 1752 | } |
| 1753 | |
Alexey Dobriyan | 4714521 | 2009-12-14 18:00:08 -0800 | [diff] [blame] | 1754 | static const struct dev_pm_ops at_dma_dev_pm_ops = { |
Nicolas Ferre | c0ba594 | 2011-07-27 12:21:29 +0000 | [diff] [blame] | 1755 | .prepare = at_dma_prepare, |
Dan Williams | 33f82d1 | 2009-09-10 00:06:44 +0200 | [diff] [blame] | 1756 | .suspend_noirq = at_dma_suspend_noirq, |
| 1757 | .resume_noirq = at_dma_resume_noirq, |
| 1758 | }; |
| 1759 | |
Nicolas Ferre | dc78baa | 2009-07-03 19:24:33 +0200 | [diff] [blame] | 1760 | static struct platform_driver at_dma_driver = { |
Maxin B. John | 1d1bbd3 | 2013-02-20 02:07:04 +0200 | [diff] [blame] | 1761 | .remove = at_dma_remove, |
Nicolas Ferre | dc78baa | 2009-07-03 19:24:33 +0200 | [diff] [blame] | 1762 | .shutdown = at_dma_shutdown, |
Nicolas Ferre | 6734845 | 2011-10-17 14:56:40 +0200 | [diff] [blame] | 1763 | .id_table = atdma_devtypes, |
Nicolas Ferre | dc78baa | 2009-07-03 19:24:33 +0200 | [diff] [blame] | 1764 | .driver = { |
| 1765 | .name = "at_hdmac", |
Dan Williams | 33f82d1 | 2009-09-10 00:06:44 +0200 | [diff] [blame] | 1766 | .pm = &at_dma_dev_pm_ops, |
Nicolas Ferre | c511595 | 2011-10-17 14:56:41 +0200 | [diff] [blame] | 1767 | .of_match_table = of_match_ptr(atmel_dma_dt_ids), |
Nicolas Ferre | dc78baa | 2009-07-03 19:24:33 +0200 | [diff] [blame] | 1768 | }, |
| 1769 | }; |
| 1770 | |
| 1771 | static int __init at_dma_init(void) |
| 1772 | { |
| 1773 | return platform_driver_probe(&at_dma_driver, at_dma_probe); |
| 1774 | } |
Eric Xu | 93d0bec | 2011-01-12 15:39:08 +0100 | [diff] [blame] | 1775 | subsys_initcall(at_dma_init); |
Nicolas Ferre | dc78baa | 2009-07-03 19:24:33 +0200 | [diff] [blame] | 1776 | |
| 1777 | static void __exit at_dma_exit(void) |
| 1778 | { |
| 1779 | platform_driver_unregister(&at_dma_driver); |
| 1780 | } |
| 1781 | module_exit(at_dma_exit); |
| 1782 | |
| 1783 | MODULE_DESCRIPTION("Atmel AHB DMA Controller driver"); |
| 1784 | MODULE_AUTHOR("Nicolas Ferre <nicolas.ferre@atmel.com>"); |
| 1785 | MODULE_LICENSE("GPL"); |
| 1786 | MODULE_ALIAS("platform:at_hdmac"); |