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