Stefan Wahren | 80c4445e5 | 2018-11-10 16:34:40 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Florian Meier | 96286b5 | 2014-01-06 20:18:24 +0100 | [diff] [blame] | 2 | /* |
| 3 | * BCM2835 DMA engine support |
| 4 | * |
| 5 | * This driver only supports cyclic DMA transfers |
| 6 | * as needed for the I2S module. |
| 7 | * |
| 8 | * Author: Florian Meier <florian.meier@koalo.de> |
| 9 | * Copyright 2013 |
| 10 | * |
| 11 | * Based on |
| 12 | * OMAP DMAengine support by Russell King |
| 13 | * |
| 14 | * BCM2708 DMA Driver |
| 15 | * Copyright (C) 2010 Broadcom |
| 16 | * |
| 17 | * Raspberry Pi PCM I2S ALSA Driver |
| 18 | * Copyright (c) by Phil Poole 2013 |
| 19 | * |
| 20 | * MARVELL MMP Peripheral DMA Driver |
| 21 | * Copyright 2012 Marvell International Ltd. |
Florian Meier | 96286b5 | 2014-01-06 20:18:24 +0100 | [diff] [blame] | 22 | */ |
| 23 | #include <linux/dmaengine.h> |
| 24 | #include <linux/dma-mapping.h> |
Peter Ujfalusi | 27bc944c | 2015-11-16 13:09:03 +0200 | [diff] [blame] | 25 | #include <linux/dmapool.h> |
Florian Meier | 96286b5 | 2014-01-06 20:18:24 +0100 | [diff] [blame] | 26 | #include <linux/err.h> |
| 27 | #include <linux/init.h> |
| 28 | #include <linux/interrupt.h> |
| 29 | #include <linux/list.h> |
| 30 | #include <linux/module.h> |
| 31 | #include <linux/platform_device.h> |
| 32 | #include <linux/slab.h> |
| 33 | #include <linux/io.h> |
| 34 | #include <linux/spinlock.h> |
| 35 | #include <linux/of.h> |
| 36 | #include <linux/of_dma.h> |
| 37 | |
| 38 | #include "virt-dma.h" |
| 39 | |
Martin Sperl | e2eca63 | 2016-04-11 13:29:08 +0000 | [diff] [blame] | 40 | #define BCM2835_DMA_MAX_DMA_CHAN_SUPPORTED 14 |
| 41 | #define BCM2835_DMA_CHAN_NAME_SIZE 8 |
| 42 | |
Florian Meier | 96286b5 | 2014-01-06 20:18:24 +0100 | [diff] [blame] | 43 | struct bcm2835_dmadev { |
| 44 | struct dma_device ddev; |
| 45 | spinlock_t lock; |
| 46 | void __iomem *base; |
| 47 | struct device_dma_parameters dma_parms; |
| 48 | }; |
| 49 | |
| 50 | struct bcm2835_dma_cb { |
| 51 | uint32_t info; |
| 52 | uint32_t src; |
| 53 | uint32_t dst; |
| 54 | uint32_t length; |
| 55 | uint32_t stride; |
| 56 | uint32_t next; |
| 57 | uint32_t pad[2]; |
| 58 | }; |
| 59 | |
Peter Ujfalusi | 27bc944c | 2015-11-16 13:09:03 +0200 | [diff] [blame] | 60 | struct bcm2835_cb_entry { |
| 61 | struct bcm2835_dma_cb *cb; |
| 62 | dma_addr_t paddr; |
| 63 | }; |
| 64 | |
Florian Meier | 96286b5 | 2014-01-06 20:18:24 +0100 | [diff] [blame] | 65 | struct bcm2835_chan { |
| 66 | struct virt_dma_chan vc; |
| 67 | struct list_head node; |
| 68 | |
| 69 | struct dma_slave_config cfg; |
Florian Meier | 96286b5 | 2014-01-06 20:18:24 +0100 | [diff] [blame] | 70 | unsigned int dreq; |
| 71 | |
| 72 | int ch; |
| 73 | struct bcm2835_desc *desc; |
Peter Ujfalusi | 27bc944c | 2015-11-16 13:09:03 +0200 | [diff] [blame] | 74 | struct dma_pool *cb_pool; |
Florian Meier | 96286b5 | 2014-01-06 20:18:24 +0100 | [diff] [blame] | 75 | |
| 76 | void __iomem *chan_base; |
| 77 | int irq_number; |
Martin Sperl | e2eca63 | 2016-04-11 13:29:08 +0000 | [diff] [blame] | 78 | unsigned int irq_flags; |
Martin Sperl | 4087412 | 2016-03-16 12:25:00 -0700 | [diff] [blame] | 79 | |
| 80 | bool is_lite_channel; |
Florian Meier | 96286b5 | 2014-01-06 20:18:24 +0100 | [diff] [blame] | 81 | }; |
| 82 | |
| 83 | struct bcm2835_desc { |
Peter Ujfalusi | 27bc944c | 2015-11-16 13:09:03 +0200 | [diff] [blame] | 84 | struct bcm2835_chan *c; |
Florian Meier | 96286b5 | 2014-01-06 20:18:24 +0100 | [diff] [blame] | 85 | struct virt_dma_desc vd; |
| 86 | enum dma_transfer_direction dir; |
| 87 | |
Florian Meier | 96286b5 | 2014-01-06 20:18:24 +0100 | [diff] [blame] | 88 | unsigned int frames; |
| 89 | size_t size; |
Martin Sperl | a4dcdd8 | 2016-03-16 12:24:58 -0700 | [diff] [blame] | 90 | |
| 91 | bool cyclic; |
Martin Sperl | 92153bb | 2016-03-16 12:24:59 -0700 | [diff] [blame] | 92 | |
| 93 | struct bcm2835_cb_entry cb_list[]; |
Florian Meier | 96286b5 | 2014-01-06 20:18:24 +0100 | [diff] [blame] | 94 | }; |
| 95 | |
| 96 | #define BCM2835_DMA_CS 0x00 |
| 97 | #define BCM2835_DMA_ADDR 0x04 |
Martin Sperl | e42685d | 2016-03-16 12:24:57 -0700 | [diff] [blame] | 98 | #define BCM2835_DMA_TI 0x08 |
Florian Meier | 96286b5 | 2014-01-06 20:18:24 +0100 | [diff] [blame] | 99 | #define BCM2835_DMA_SOURCE_AD 0x0c |
| 100 | #define BCM2835_DMA_DEST_AD 0x10 |
Martin Sperl | e42685d | 2016-03-16 12:24:57 -0700 | [diff] [blame] | 101 | #define BCM2835_DMA_LEN 0x14 |
| 102 | #define BCM2835_DMA_STRIDE 0x18 |
| 103 | #define BCM2835_DMA_NEXTCB 0x1c |
| 104 | #define BCM2835_DMA_DEBUG 0x20 |
Florian Meier | 96286b5 | 2014-01-06 20:18:24 +0100 | [diff] [blame] | 105 | |
| 106 | /* DMA CS Control and Status bits */ |
Martin Sperl | e42685d | 2016-03-16 12:24:57 -0700 | [diff] [blame] | 107 | #define BCM2835_DMA_ACTIVE BIT(0) /* activate the DMA */ |
| 108 | #define BCM2835_DMA_END BIT(1) /* current CB has ended */ |
| 109 | #define BCM2835_DMA_INT BIT(2) /* interrupt status */ |
| 110 | #define BCM2835_DMA_DREQ BIT(3) /* DREQ state */ |
Florian Meier | 96286b5 | 2014-01-06 20:18:24 +0100 | [diff] [blame] | 111 | #define BCM2835_DMA_ISPAUSED BIT(4) /* Pause requested or not active */ |
| 112 | #define BCM2835_DMA_ISHELD BIT(5) /* Is held by DREQ flow control */ |
Martin Sperl | e42685d | 2016-03-16 12:24:57 -0700 | [diff] [blame] | 113 | #define BCM2835_DMA_WAITING_FOR_WRITES BIT(6) /* waiting for last |
| 114 | * AXI-write to ack |
| 115 | */ |
| 116 | #define BCM2835_DMA_ERR BIT(8) |
| 117 | #define BCM2835_DMA_PRIORITY(x) ((x & 15) << 16) /* AXI priority */ |
| 118 | #define BCM2835_DMA_PANIC_PRIORITY(x) ((x & 15) << 20) /* panic priority */ |
| 119 | /* current value of TI.BCM2835_DMA_WAIT_RESP */ |
| 120 | #define BCM2835_DMA_WAIT_FOR_WRITES BIT(28) |
| 121 | #define BCM2835_DMA_DIS_DEBUG BIT(29) /* disable debug pause signal */ |
Florian Meier | 96286b5 | 2014-01-06 20:18:24 +0100 | [diff] [blame] | 122 | #define BCM2835_DMA_ABORT BIT(30) /* Stop current CB, go to next, WO */ |
| 123 | #define BCM2835_DMA_RESET BIT(31) /* WO, self clearing */ |
| 124 | |
Martin Sperl | e42685d | 2016-03-16 12:24:57 -0700 | [diff] [blame] | 125 | /* Transfer information bits - also bcm2835_cb.info field */ |
Florian Meier | 96286b5 | 2014-01-06 20:18:24 +0100 | [diff] [blame] | 126 | #define BCM2835_DMA_INT_EN BIT(0) |
Martin Sperl | e42685d | 2016-03-16 12:24:57 -0700 | [diff] [blame] | 127 | #define BCM2835_DMA_TDMODE BIT(1) /* 2D-Mode */ |
| 128 | #define BCM2835_DMA_WAIT_RESP BIT(3) /* wait for AXI-write to be acked */ |
Florian Meier | 96286b5 | 2014-01-06 20:18:24 +0100 | [diff] [blame] | 129 | #define BCM2835_DMA_D_INC BIT(4) |
Martin Sperl | e42685d | 2016-03-16 12:24:57 -0700 | [diff] [blame] | 130 | #define BCM2835_DMA_D_WIDTH BIT(5) /* 128bit writes if set */ |
| 131 | #define BCM2835_DMA_D_DREQ BIT(6) /* enable DREQ for destination */ |
| 132 | #define BCM2835_DMA_D_IGNORE BIT(7) /* ignore destination writes */ |
Florian Meier | 96286b5 | 2014-01-06 20:18:24 +0100 | [diff] [blame] | 133 | #define BCM2835_DMA_S_INC BIT(8) |
Martin Sperl | e42685d | 2016-03-16 12:24:57 -0700 | [diff] [blame] | 134 | #define BCM2835_DMA_S_WIDTH BIT(9) /* 128bit writes if set */ |
| 135 | #define BCM2835_DMA_S_DREQ BIT(10) /* enable SREQ for source */ |
| 136 | #define BCM2835_DMA_S_IGNORE BIT(11) /* ignore source reads - read 0 */ |
| 137 | #define BCM2835_DMA_BURST_LENGTH(x) ((x & 15) << 12) |
| 138 | #define BCM2835_DMA_PER_MAP(x) ((x & 31) << 16) /* REQ source */ |
| 139 | #define BCM2835_DMA_WAIT(x) ((x & 31) << 21) /* add DMA-wait cycles */ |
| 140 | #define BCM2835_DMA_NO_WIDE_BURSTS BIT(26) /* no 2 beat write bursts */ |
Florian Meier | 96286b5 | 2014-01-06 20:18:24 +0100 | [diff] [blame] | 141 | |
Martin Sperl | e42685d | 2016-03-16 12:24:57 -0700 | [diff] [blame] | 142 | /* debug register bits */ |
| 143 | #define BCM2835_DMA_DEBUG_LAST_NOT_SET_ERR BIT(0) |
| 144 | #define BCM2835_DMA_DEBUG_FIFO_ERR BIT(1) |
| 145 | #define BCM2835_DMA_DEBUG_READ_ERR BIT(2) |
| 146 | #define BCM2835_DMA_DEBUG_OUTSTANDING_WRITES_SHIFT 4 |
| 147 | #define BCM2835_DMA_DEBUG_OUTSTANDING_WRITES_BITS 4 |
| 148 | #define BCM2835_DMA_DEBUG_ID_SHIFT 16 |
| 149 | #define BCM2835_DMA_DEBUG_ID_BITS 9 |
| 150 | #define BCM2835_DMA_DEBUG_STATE_SHIFT 16 |
| 151 | #define BCM2835_DMA_DEBUG_STATE_BITS 9 |
| 152 | #define BCM2835_DMA_DEBUG_VERSION_SHIFT 25 |
| 153 | #define BCM2835_DMA_DEBUG_VERSION_BITS 3 |
| 154 | #define BCM2835_DMA_DEBUG_LITE BIT(28) |
| 155 | |
| 156 | /* shared registers for all dma channels */ |
| 157 | #define BCM2835_DMA_INT_STATUS 0xfe0 |
| 158 | #define BCM2835_DMA_ENABLE 0xff0 |
Florian Meier | 96286b5 | 2014-01-06 20:18:24 +0100 | [diff] [blame] | 159 | |
| 160 | #define BCM2835_DMA_DATA_TYPE_S8 1 |
| 161 | #define BCM2835_DMA_DATA_TYPE_S16 2 |
| 162 | #define BCM2835_DMA_DATA_TYPE_S32 4 |
| 163 | #define BCM2835_DMA_DATA_TYPE_S128 16 |
| 164 | |
Florian Meier | 96286b5 | 2014-01-06 20:18:24 +0100 | [diff] [blame] | 165 | /* Valid only for channels 0 - 14, 15 has its own base address */ |
| 166 | #define BCM2835_DMA_CHAN(n) ((n) << 8) /* Base address */ |
| 167 | #define BCM2835_DMA_CHANIO(base, n) ((base) + BCM2835_DMA_CHAN(n)) |
| 168 | |
Martin Sperl | 4087412 | 2016-03-16 12:25:00 -0700 | [diff] [blame] | 169 | /* the max dma length for different channels */ |
| 170 | #define MAX_DMA_LEN SZ_1G |
| 171 | #define MAX_LITE_DMA_LEN (SZ_64K - 4) |
| 172 | |
| 173 | static inline size_t bcm2835_dma_max_frame_length(struct bcm2835_chan *c) |
| 174 | { |
| 175 | /* lite and normal channels have different max frame length */ |
| 176 | return c->is_lite_channel ? MAX_LITE_DMA_LEN : MAX_DMA_LEN; |
| 177 | } |
| 178 | |
Martin Sperl | 92153bb | 2016-03-16 12:24:59 -0700 | [diff] [blame] | 179 | /* how many frames of max_len size do we need to transfer len bytes */ |
| 180 | static inline size_t bcm2835_dma_frames_for_length(size_t len, |
| 181 | size_t max_len) |
| 182 | { |
| 183 | return DIV_ROUND_UP(len, max_len); |
| 184 | } |
| 185 | |
Florian Meier | 96286b5 | 2014-01-06 20:18:24 +0100 | [diff] [blame] | 186 | static inline struct bcm2835_dmadev *to_bcm2835_dma_dev(struct dma_device *d) |
| 187 | { |
| 188 | return container_of(d, struct bcm2835_dmadev, ddev); |
| 189 | } |
| 190 | |
| 191 | static inline struct bcm2835_chan *to_bcm2835_dma_chan(struct dma_chan *c) |
| 192 | { |
| 193 | return container_of(c, struct bcm2835_chan, vc.chan); |
| 194 | } |
| 195 | |
| 196 | static inline struct bcm2835_desc *to_bcm2835_dma_desc( |
| 197 | struct dma_async_tx_descriptor *t) |
| 198 | { |
| 199 | return container_of(t, struct bcm2835_desc, vd.tx); |
| 200 | } |
| 201 | |
Martin Sperl | 92153bb | 2016-03-16 12:24:59 -0700 | [diff] [blame] | 202 | static void bcm2835_dma_free_cb_chain(struct bcm2835_desc *desc) |
Florian Meier | 96286b5 | 2014-01-06 20:18:24 +0100 | [diff] [blame] | 203 | { |
Martin Sperl | 92153bb | 2016-03-16 12:24:59 -0700 | [diff] [blame] | 204 | size_t i; |
Peter Ujfalusi | 27bc944c | 2015-11-16 13:09:03 +0200 | [diff] [blame] | 205 | |
| 206 | for (i = 0; i < desc->frames; i++) |
| 207 | dma_pool_free(desc->c->cb_pool, desc->cb_list[i].cb, |
| 208 | desc->cb_list[i].paddr); |
| 209 | |
Florian Meier | 96286b5 | 2014-01-06 20:18:24 +0100 | [diff] [blame] | 210 | kfree(desc); |
| 211 | } |
| 212 | |
Martin Sperl | 92153bb | 2016-03-16 12:24:59 -0700 | [diff] [blame] | 213 | static void bcm2835_dma_desc_free(struct virt_dma_desc *vd) |
| 214 | { |
| 215 | bcm2835_dma_free_cb_chain( |
| 216 | container_of(vd, struct bcm2835_desc, vd)); |
| 217 | } |
| 218 | |
| 219 | static void bcm2835_dma_create_cb_set_length( |
| 220 | struct bcm2835_chan *chan, |
| 221 | struct bcm2835_dma_cb *control_block, |
| 222 | size_t len, |
| 223 | size_t period_len, |
| 224 | size_t *total_len, |
| 225 | u32 finalextrainfo) |
| 226 | { |
Martin Sperl | 4087412 | 2016-03-16 12:25:00 -0700 | [diff] [blame] | 227 | size_t max_len = bcm2835_dma_max_frame_length(chan); |
| 228 | |
| 229 | /* set the length taking lite-channel limitations into account */ |
| 230 | control_block->length = min_t(u32, len, max_len); |
Martin Sperl | 92153bb | 2016-03-16 12:24:59 -0700 | [diff] [blame] | 231 | |
| 232 | /* finished if we have no period_length */ |
| 233 | if (!period_len) |
| 234 | return; |
| 235 | |
| 236 | /* |
| 237 | * period_len means: that we need to generate |
| 238 | * transfers that are terminating at every |
| 239 | * multiple of period_len - this is typically |
| 240 | * used to set the interrupt flag in info |
| 241 | * which is required during cyclic transfers |
| 242 | */ |
| 243 | |
| 244 | /* have we filled in period_length yet? */ |
Matthias Reichl | 2201ac6 | 2017-02-20 20:01:16 +0100 | [diff] [blame] | 245 | if (*total_len + control_block->length < period_len) { |
| 246 | /* update number of bytes in this period so far */ |
| 247 | *total_len += control_block->length; |
Martin Sperl | 92153bb | 2016-03-16 12:24:59 -0700 | [diff] [blame] | 248 | return; |
Matthias Reichl | 2201ac6 | 2017-02-20 20:01:16 +0100 | [diff] [blame] | 249 | } |
Martin Sperl | 92153bb | 2016-03-16 12:24:59 -0700 | [diff] [blame] | 250 | |
| 251 | /* calculate the length that remains to reach period_length */ |
| 252 | control_block->length = period_len - *total_len; |
| 253 | |
| 254 | /* reset total_length for next period */ |
| 255 | *total_len = 0; |
| 256 | |
| 257 | /* add extrainfo bits in info */ |
| 258 | control_block->info |= finalextrainfo; |
| 259 | } |
| 260 | |
Martin Sperl | 388cc7a | 2016-03-16 12:25:01 -0700 | [diff] [blame] | 261 | static inline size_t bcm2835_dma_count_frames_for_sg( |
| 262 | struct bcm2835_chan *c, |
| 263 | struct scatterlist *sgl, |
| 264 | unsigned int sg_len) |
| 265 | { |
| 266 | size_t frames = 0; |
| 267 | struct scatterlist *sgent; |
| 268 | unsigned int i; |
| 269 | size_t plength = bcm2835_dma_max_frame_length(c); |
| 270 | |
| 271 | for_each_sg(sgl, sgent, sg_len, i) |
| 272 | frames += bcm2835_dma_frames_for_length( |
| 273 | sg_dma_len(sgent), plength); |
| 274 | |
| 275 | return frames; |
| 276 | } |
| 277 | |
Martin Sperl | 92153bb | 2016-03-16 12:24:59 -0700 | [diff] [blame] | 278 | /** |
| 279 | * bcm2835_dma_create_cb_chain - create a control block and fills data in |
| 280 | * |
| 281 | * @chan: the @dma_chan for which we run this |
| 282 | * @direction: the direction in which we transfer |
| 283 | * @cyclic: it is a cyclic transfer |
| 284 | * @info: the default info bits to apply per controlblock |
| 285 | * @frames: number of controlblocks to allocate |
| 286 | * @src: the src address to assign (if the S_INC bit is set |
| 287 | * in @info, then it gets incremented) |
| 288 | * @dst: the dst address to assign (if the D_INC bit is set |
| 289 | * in @info, then it gets incremented) |
| 290 | * @buf_len: the full buffer length (may also be 0) |
| 291 | * @period_len: the period length when to apply @finalextrainfo |
| 292 | * in addition to the last transfer |
| 293 | * this will also break some control-blocks early |
| 294 | * @finalextrainfo: additional bits in last controlblock |
| 295 | * (or when period_len is reached in case of cyclic) |
| 296 | * @gfp: the GFP flag to use for allocation |
| 297 | */ |
| 298 | static struct bcm2835_desc *bcm2835_dma_create_cb_chain( |
| 299 | struct dma_chan *chan, enum dma_transfer_direction direction, |
| 300 | bool cyclic, u32 info, u32 finalextrainfo, size_t frames, |
| 301 | dma_addr_t src, dma_addr_t dst, size_t buf_len, |
| 302 | size_t period_len, gfp_t gfp) |
| 303 | { |
| 304 | struct bcm2835_chan *c = to_bcm2835_dma_chan(chan); |
| 305 | size_t len = buf_len, total_len; |
| 306 | size_t frame; |
| 307 | struct bcm2835_desc *d; |
| 308 | struct bcm2835_cb_entry *cb_entry; |
| 309 | struct bcm2835_dma_cb *control_block; |
| 310 | |
Martin Sperl | d9f094a | 2016-03-16 12:25:02 -0700 | [diff] [blame] | 311 | if (!frames) |
| 312 | return NULL; |
| 313 | |
Martin Sperl | 92153bb | 2016-03-16 12:24:59 -0700 | [diff] [blame] | 314 | /* allocate and setup the descriptor. */ |
| 315 | d = kzalloc(sizeof(*d) + frames * sizeof(struct bcm2835_cb_entry), |
| 316 | gfp); |
| 317 | if (!d) |
| 318 | return NULL; |
| 319 | |
| 320 | d->c = c; |
| 321 | d->dir = direction; |
| 322 | d->cyclic = cyclic; |
| 323 | |
| 324 | /* |
| 325 | * Iterate over all frames, create a control block |
| 326 | * for each frame and link them together. |
| 327 | */ |
| 328 | for (frame = 0, total_len = 0; frame < frames; d->frames++, frame++) { |
| 329 | cb_entry = &d->cb_list[frame]; |
| 330 | cb_entry->cb = dma_pool_alloc(c->cb_pool, gfp, |
| 331 | &cb_entry->paddr); |
| 332 | if (!cb_entry->cb) |
| 333 | goto error_cb; |
| 334 | |
| 335 | /* fill in the control block */ |
| 336 | control_block = cb_entry->cb; |
| 337 | control_block->info = info; |
| 338 | control_block->src = src; |
| 339 | control_block->dst = dst; |
| 340 | control_block->stride = 0; |
| 341 | control_block->next = 0; |
| 342 | /* set up length in control_block if requested */ |
| 343 | if (buf_len) { |
| 344 | /* calculate length honoring period_length */ |
| 345 | bcm2835_dma_create_cb_set_length( |
| 346 | c, control_block, |
| 347 | len, period_len, &total_len, |
| 348 | cyclic ? finalextrainfo : 0); |
| 349 | |
| 350 | /* calculate new remaining length */ |
| 351 | len -= control_block->length; |
| 352 | } |
| 353 | |
| 354 | /* link this the last controlblock */ |
| 355 | if (frame) |
| 356 | d->cb_list[frame - 1].cb->next = cb_entry->paddr; |
| 357 | |
| 358 | /* update src and dst and length */ |
| 359 | if (src && (info & BCM2835_DMA_S_INC)) |
| 360 | src += control_block->length; |
| 361 | if (dst && (info & BCM2835_DMA_D_INC)) |
| 362 | dst += control_block->length; |
| 363 | |
| 364 | /* Length of total transfer */ |
| 365 | d->size += control_block->length; |
| 366 | } |
| 367 | |
| 368 | /* the last frame requires extra flags */ |
| 369 | d->cb_list[d->frames - 1].cb->info |= finalextrainfo; |
| 370 | |
| 371 | /* detect a size missmatch */ |
| 372 | if (buf_len && (d->size != buf_len)) |
| 373 | goto error_cb; |
| 374 | |
| 375 | return d; |
| 376 | error_cb: |
| 377 | bcm2835_dma_free_cb_chain(d); |
| 378 | |
| 379 | return NULL; |
| 380 | } |
| 381 | |
Martin Sperl | 388cc7a | 2016-03-16 12:25:01 -0700 | [diff] [blame] | 382 | static void bcm2835_dma_fill_cb_chain_with_sg( |
| 383 | struct dma_chan *chan, |
| 384 | enum dma_transfer_direction direction, |
| 385 | struct bcm2835_cb_entry *cb, |
| 386 | struct scatterlist *sgl, |
| 387 | unsigned int sg_len) |
| 388 | { |
| 389 | struct bcm2835_chan *c = to_bcm2835_dma_chan(chan); |
Arnd Bergmann | 4aa819c | 2016-06-30 14:47:10 +0200 | [diff] [blame] | 390 | size_t len, max_len; |
| 391 | unsigned int i; |
Martin Sperl | 388cc7a | 2016-03-16 12:25:01 -0700 | [diff] [blame] | 392 | dma_addr_t addr; |
| 393 | struct scatterlist *sgent; |
| 394 | |
Arnd Bergmann | 4aa819c | 2016-06-30 14:47:10 +0200 | [diff] [blame] | 395 | max_len = bcm2835_dma_max_frame_length(c); |
Martin Sperl | 388cc7a | 2016-03-16 12:25:01 -0700 | [diff] [blame] | 396 | for_each_sg(sgl, sgent, sg_len, i) { |
| 397 | for (addr = sg_dma_address(sgent), len = sg_dma_len(sgent); |
| 398 | len > 0; |
| 399 | addr += cb->cb->length, len -= cb->cb->length, cb++) { |
| 400 | if (direction == DMA_DEV_TO_MEM) |
| 401 | cb->cb->dst = addr; |
| 402 | else |
| 403 | cb->cb->src = addr; |
| 404 | cb->cb->length = min(len, max_len); |
| 405 | } |
| 406 | } |
| 407 | } |
| 408 | |
Lukas Wunner | 3e05ada | 2019-01-23 09:26:00 +0100 | [diff] [blame] | 409 | static void bcm2835_dma_abort(struct bcm2835_chan *c) |
Florian Meier | 96286b5 | 2014-01-06 20:18:24 +0100 | [diff] [blame] | 410 | { |
Lukas Wunner | 9e528c7 | 2019-01-23 09:26:00 +0100 | [diff] [blame] | 411 | void __iomem *chan_base = c->chan_base; |
Florian Meier | 96286b5 | 2014-01-06 20:18:24 +0100 | [diff] [blame] | 412 | long int timeout = 10000; |
| 413 | |
Lukas Wunner | f7da7782 | 2019-01-23 09:26:00 +0100 | [diff] [blame] | 414 | /* |
| 415 | * A zero control block address means the channel is idle. |
| 416 | * (The ACTIVE flag in the CS register is not a reliable indicator.) |
| 417 | */ |
| 418 | if (!readl(chan_base + BCM2835_DMA_ADDR)) |
Lukas Wunner | 3e05ada | 2019-01-23 09:26:00 +0100 | [diff] [blame] | 419 | return; |
Florian Meier | 96286b5 | 2014-01-06 20:18:24 +0100 | [diff] [blame] | 420 | |
| 421 | /* Write 0 to the active bit - Pause the DMA */ |
| 422 | writel(0, chan_base + BCM2835_DMA_CS); |
| 423 | |
| 424 | /* Wait for any current AXI transfer to complete */ |
Lukas Wunner | 9e528c7 | 2019-01-23 09:26:00 +0100 | [diff] [blame] | 425 | while ((readl(chan_base + BCM2835_DMA_CS) & |
| 426 | BCM2835_DMA_WAITING_FOR_WRITES) && --timeout) |
Florian Meier | 96286b5 | 2014-01-06 20:18:24 +0100 | [diff] [blame] | 427 | cpu_relax(); |
Florian Meier | 96286b5 | 2014-01-06 20:18:24 +0100 | [diff] [blame] | 428 | |
Lukas Wunner | 9e528c7 | 2019-01-23 09:26:00 +0100 | [diff] [blame] | 429 | /* Peripheral might be stuck and fail to signal AXI write responses */ |
Florian Meier | 96286b5 | 2014-01-06 20:18:24 +0100 | [diff] [blame] | 430 | if (!timeout) |
Lukas Wunner | 9e528c7 | 2019-01-23 09:26:00 +0100 | [diff] [blame] | 431 | dev_err(c->vc.chan.device->dev, |
| 432 | "failed to complete outstanding writes\n"); |
Florian Meier | 96286b5 | 2014-01-06 20:18:24 +0100 | [diff] [blame] | 433 | |
Lukas Wunner | 9e528c7 | 2019-01-23 09:26:00 +0100 | [diff] [blame] | 434 | writel(BCM2835_DMA_RESET, chan_base + BCM2835_DMA_CS); |
Florian Meier | 96286b5 | 2014-01-06 20:18:24 +0100 | [diff] [blame] | 435 | } |
| 436 | |
| 437 | static void bcm2835_dma_start_desc(struct bcm2835_chan *c) |
| 438 | { |
| 439 | struct virt_dma_desc *vd = vchan_next_desc(&c->vc); |
| 440 | struct bcm2835_desc *d; |
| 441 | |
| 442 | if (!vd) { |
| 443 | c->desc = NULL; |
| 444 | return; |
| 445 | } |
| 446 | |
| 447 | list_del(&vd->node); |
| 448 | |
| 449 | c->desc = d = to_bcm2835_dma_desc(&vd->tx); |
| 450 | |
Peter Ujfalusi | 27bc944c | 2015-11-16 13:09:03 +0200 | [diff] [blame] | 451 | writel(d->cb_list[0].paddr, c->chan_base + BCM2835_DMA_ADDR); |
Florian Meier | 96286b5 | 2014-01-06 20:18:24 +0100 | [diff] [blame] | 452 | writel(BCM2835_DMA_ACTIVE, c->chan_base + BCM2835_DMA_CS); |
| 453 | } |
| 454 | |
| 455 | static irqreturn_t bcm2835_dma_callback(int irq, void *data) |
| 456 | { |
| 457 | struct bcm2835_chan *c = data; |
| 458 | struct bcm2835_desc *d; |
| 459 | unsigned long flags; |
| 460 | |
Martin Sperl | e2eca63 | 2016-04-11 13:29:08 +0000 | [diff] [blame] | 461 | /* check the shared interrupt */ |
| 462 | if (c->irq_flags & IRQF_SHARED) { |
| 463 | /* check if the interrupt is enabled */ |
| 464 | flags = readl(c->chan_base + BCM2835_DMA_CS); |
| 465 | /* if not set then we are not the reason for the irq */ |
| 466 | if (!(flags & BCM2835_DMA_INT)) |
| 467 | return IRQ_NONE; |
| 468 | } |
| 469 | |
Florian Meier | 96286b5 | 2014-01-06 20:18:24 +0100 | [diff] [blame] | 470 | spin_lock_irqsave(&c->vc.lock, flags); |
| 471 | |
Lukas Wunner | f7da7782 | 2019-01-23 09:26:00 +0100 | [diff] [blame] | 472 | /* |
| 473 | * Clear the INT flag to receive further interrupts. Keep the channel |
| 474 | * active in case the descriptor is cyclic or in case the client has |
| 475 | * already terminated the descriptor and issued a new one. (May happen |
| 476 | * if this IRQ handler is threaded.) If the channel is finished, it |
| 477 | * will remain idle despite the ACTIVE flag being set. |
| 478 | */ |
| 479 | writel(BCM2835_DMA_INT | BCM2835_DMA_ACTIVE, |
| 480 | c->chan_base + BCM2835_DMA_CS); |
Florian Meier | 96286b5 | 2014-01-06 20:18:24 +0100 | [diff] [blame] | 481 | |
| 482 | d = c->desc; |
| 483 | |
| 484 | if (d) { |
Martin Sperl | 388cc7a | 2016-03-16 12:25:01 -0700 | [diff] [blame] | 485 | if (d->cyclic) { |
| 486 | /* call the cyclic callback */ |
| 487 | vchan_cyclic_callback(&d->vd); |
Lukas Wunner | f7da7782 | 2019-01-23 09:26:00 +0100 | [diff] [blame] | 488 | } else if (!readl(c->chan_base + BCM2835_DMA_ADDR)) { |
Martin Sperl | 388cc7a | 2016-03-16 12:25:01 -0700 | [diff] [blame] | 489 | vchan_cookie_complete(&c->desc->vd); |
| 490 | bcm2835_dma_start_desc(c); |
| 491 | } |
| 492 | } |
Florian Meier | 96286b5 | 2014-01-06 20:18:24 +0100 | [diff] [blame] | 493 | |
| 494 | spin_unlock_irqrestore(&c->vc.lock, flags); |
| 495 | |
| 496 | return IRQ_HANDLED; |
| 497 | } |
| 498 | |
| 499 | static int bcm2835_dma_alloc_chan_resources(struct dma_chan *chan) |
| 500 | { |
| 501 | struct bcm2835_chan *c = to_bcm2835_dma_chan(chan); |
Peter Ujfalusi | 27bc944c | 2015-11-16 13:09:03 +0200 | [diff] [blame] | 502 | struct device *dev = c->vc.chan.device->dev; |
Florian Meier | 96286b5 | 2014-01-06 20:18:24 +0100 | [diff] [blame] | 503 | |
Peter Ujfalusi | 27bc944c | 2015-11-16 13:09:03 +0200 | [diff] [blame] | 504 | dev_dbg(dev, "Allocating DMA channel %d\n", c->ch); |
| 505 | |
Lukas Wunner | 603fe86 | 2019-01-23 09:26:00 +0100 | [diff] [blame^] | 506 | /* |
| 507 | * Control blocks are 256 bit in length and must start at a 256 bit |
| 508 | * (32 byte) aligned address (BCM2835 ARM Peripherals, sec. 4.2.1.1). |
| 509 | */ |
Peter Ujfalusi | 27bc944c | 2015-11-16 13:09:03 +0200 | [diff] [blame] | 510 | c->cb_pool = dma_pool_create(dev_name(dev), dev, |
Lukas Wunner | 603fe86 | 2019-01-23 09:26:00 +0100 | [diff] [blame^] | 511 | sizeof(struct bcm2835_dma_cb), 32, 0); |
Peter Ujfalusi | 27bc944c | 2015-11-16 13:09:03 +0200 | [diff] [blame] | 512 | if (!c->cb_pool) { |
| 513 | dev_err(dev, "unable to allocate descriptor pool\n"); |
| 514 | return -ENOMEM; |
| 515 | } |
Florian Meier | 96286b5 | 2014-01-06 20:18:24 +0100 | [diff] [blame] | 516 | |
Martin Sperl | e2eca63 | 2016-04-11 13:29:08 +0000 | [diff] [blame] | 517 | return request_irq(c->irq_number, bcm2835_dma_callback, |
| 518 | c->irq_flags, "DMA IRQ", c); |
Florian Meier | 96286b5 | 2014-01-06 20:18:24 +0100 | [diff] [blame] | 519 | } |
| 520 | |
| 521 | static void bcm2835_dma_free_chan_resources(struct dma_chan *chan) |
| 522 | { |
| 523 | struct bcm2835_chan *c = to_bcm2835_dma_chan(chan); |
| 524 | |
| 525 | vchan_free_chan_resources(&c->vc); |
| 526 | free_irq(c->irq_number, c); |
Peter Ujfalusi | 27bc944c | 2015-11-16 13:09:03 +0200 | [diff] [blame] | 527 | dma_pool_destroy(c->cb_pool); |
Florian Meier | 96286b5 | 2014-01-06 20:18:24 +0100 | [diff] [blame] | 528 | |
| 529 | dev_dbg(c->vc.chan.device->dev, "Freeing DMA channel %u\n", c->ch); |
| 530 | } |
| 531 | |
| 532 | static size_t bcm2835_dma_desc_size(struct bcm2835_desc *d) |
| 533 | { |
| 534 | return d->size; |
| 535 | } |
| 536 | |
| 537 | static size_t bcm2835_dma_desc_size_pos(struct bcm2835_desc *d, dma_addr_t addr) |
| 538 | { |
| 539 | unsigned int i; |
| 540 | size_t size; |
| 541 | |
| 542 | for (size = i = 0; i < d->frames; i++) { |
Peter Ujfalusi | 27bc944c | 2015-11-16 13:09:03 +0200 | [diff] [blame] | 543 | struct bcm2835_dma_cb *control_block = d->cb_list[i].cb; |
Florian Meier | 96286b5 | 2014-01-06 20:18:24 +0100 | [diff] [blame] | 544 | size_t this_size = control_block->length; |
| 545 | dma_addr_t dma; |
| 546 | |
| 547 | if (d->dir == DMA_DEV_TO_MEM) |
| 548 | dma = control_block->dst; |
| 549 | else |
| 550 | dma = control_block->src; |
| 551 | |
| 552 | if (size) |
| 553 | size += this_size; |
| 554 | else if (addr >= dma && addr < dma + this_size) |
| 555 | size += dma + this_size - addr; |
| 556 | } |
| 557 | |
| 558 | return size; |
| 559 | } |
| 560 | |
| 561 | static enum dma_status bcm2835_dma_tx_status(struct dma_chan *chan, |
| 562 | dma_cookie_t cookie, struct dma_tx_state *txstate) |
| 563 | { |
| 564 | struct bcm2835_chan *c = to_bcm2835_dma_chan(chan); |
| 565 | struct virt_dma_desc *vd; |
| 566 | enum dma_status ret; |
| 567 | unsigned long flags; |
| 568 | |
| 569 | ret = dma_cookie_status(chan, cookie, txstate); |
| 570 | if (ret == DMA_COMPLETE || !txstate) |
| 571 | return ret; |
| 572 | |
| 573 | spin_lock_irqsave(&c->vc.lock, flags); |
| 574 | vd = vchan_find_desc(&c->vc, cookie); |
| 575 | if (vd) { |
| 576 | txstate->residue = |
| 577 | bcm2835_dma_desc_size(to_bcm2835_dma_desc(&vd->tx)); |
| 578 | } else if (c->desc && c->desc->vd.tx.cookie == cookie) { |
| 579 | struct bcm2835_desc *d = c->desc; |
| 580 | dma_addr_t pos; |
| 581 | |
| 582 | if (d->dir == DMA_MEM_TO_DEV) |
| 583 | pos = readl(c->chan_base + BCM2835_DMA_SOURCE_AD); |
| 584 | else if (d->dir == DMA_DEV_TO_MEM) |
| 585 | pos = readl(c->chan_base + BCM2835_DMA_DEST_AD); |
| 586 | else |
| 587 | pos = 0; |
| 588 | |
| 589 | txstate->residue = bcm2835_dma_desc_size_pos(d, pos); |
| 590 | } else { |
| 591 | txstate->residue = 0; |
| 592 | } |
| 593 | |
| 594 | spin_unlock_irqrestore(&c->vc.lock, flags); |
| 595 | |
| 596 | return ret; |
| 597 | } |
| 598 | |
| 599 | static void bcm2835_dma_issue_pending(struct dma_chan *chan) |
| 600 | { |
| 601 | struct bcm2835_chan *c = to_bcm2835_dma_chan(chan); |
| 602 | unsigned long flags; |
| 603 | |
Florian Meier | 96286b5 | 2014-01-06 20:18:24 +0100 | [diff] [blame] | 604 | spin_lock_irqsave(&c->vc.lock, flags); |
| 605 | if (vchan_issue_pending(&c->vc) && !c->desc) |
| 606 | bcm2835_dma_start_desc(c); |
| 607 | |
| 608 | spin_unlock_irqrestore(&c->vc.lock, flags); |
| 609 | } |
| 610 | |
Ben Dooks | 6363722 | 2016-06-07 17:14:56 +0100 | [diff] [blame] | 611 | static struct dma_async_tx_descriptor *bcm2835_dma_prep_dma_memcpy( |
Martin Sperl | d9f094a | 2016-03-16 12:25:02 -0700 | [diff] [blame] | 612 | struct dma_chan *chan, dma_addr_t dst, dma_addr_t src, |
| 613 | size_t len, unsigned long flags) |
| 614 | { |
| 615 | struct bcm2835_chan *c = to_bcm2835_dma_chan(chan); |
| 616 | struct bcm2835_desc *d; |
| 617 | u32 info = BCM2835_DMA_D_INC | BCM2835_DMA_S_INC; |
| 618 | u32 extra = BCM2835_DMA_INT_EN | BCM2835_DMA_WAIT_RESP; |
| 619 | size_t max_len = bcm2835_dma_max_frame_length(c); |
| 620 | size_t frames; |
| 621 | |
| 622 | /* if src, dst or len is not given return with an error */ |
| 623 | if (!src || !dst || !len) |
| 624 | return NULL; |
| 625 | |
| 626 | /* calculate number of frames */ |
| 627 | frames = bcm2835_dma_frames_for_length(len, max_len); |
| 628 | |
| 629 | /* allocate the CB chain - this also fills in the pointers */ |
| 630 | d = bcm2835_dma_create_cb_chain(chan, DMA_MEM_TO_MEM, false, |
| 631 | info, extra, frames, |
| 632 | src, dst, len, 0, GFP_KERNEL); |
| 633 | if (!d) |
| 634 | return NULL; |
| 635 | |
| 636 | return vchan_tx_prep(&c->vc, &d->vd, flags); |
| 637 | } |
| 638 | |
Martin Sperl | 388cc7a | 2016-03-16 12:25:01 -0700 | [diff] [blame] | 639 | static struct dma_async_tx_descriptor *bcm2835_dma_prep_slave_sg( |
| 640 | struct dma_chan *chan, |
| 641 | struct scatterlist *sgl, unsigned int sg_len, |
| 642 | enum dma_transfer_direction direction, |
| 643 | unsigned long flags, void *context) |
| 644 | { |
| 645 | struct bcm2835_chan *c = to_bcm2835_dma_chan(chan); |
| 646 | struct bcm2835_desc *d; |
| 647 | dma_addr_t src = 0, dst = 0; |
| 648 | u32 info = BCM2835_DMA_WAIT_RESP; |
| 649 | u32 extra = BCM2835_DMA_INT_EN; |
| 650 | size_t frames; |
| 651 | |
| 652 | if (!is_slave_direction(direction)) { |
| 653 | dev_err(chan->device->dev, |
| 654 | "%s: bad direction?\n", __func__); |
| 655 | return NULL; |
| 656 | } |
| 657 | |
| 658 | if (c->dreq != 0) |
| 659 | info |= BCM2835_DMA_PER_MAP(c->dreq); |
| 660 | |
| 661 | if (direction == DMA_DEV_TO_MEM) { |
| 662 | if (c->cfg.src_addr_width != DMA_SLAVE_BUSWIDTH_4_BYTES) |
| 663 | return NULL; |
| 664 | src = c->cfg.src_addr; |
| 665 | info |= BCM2835_DMA_S_DREQ | BCM2835_DMA_D_INC; |
| 666 | } else { |
| 667 | if (c->cfg.dst_addr_width != DMA_SLAVE_BUSWIDTH_4_BYTES) |
| 668 | return NULL; |
| 669 | dst = c->cfg.dst_addr; |
| 670 | info |= BCM2835_DMA_D_DREQ | BCM2835_DMA_S_INC; |
| 671 | } |
| 672 | |
| 673 | /* count frames in sg list */ |
| 674 | frames = bcm2835_dma_count_frames_for_sg(c, sgl, sg_len); |
| 675 | |
| 676 | /* allocate the CB chain */ |
| 677 | d = bcm2835_dma_create_cb_chain(chan, direction, false, |
| 678 | info, extra, |
| 679 | frames, src, dst, 0, 0, |
| 680 | GFP_KERNEL); |
| 681 | if (!d) |
| 682 | return NULL; |
| 683 | |
| 684 | /* fill in frames with scatterlist pointers */ |
| 685 | bcm2835_dma_fill_cb_chain_with_sg(chan, direction, d->cb_list, |
| 686 | sgl, sg_len); |
| 687 | |
| 688 | return vchan_tx_prep(&c->vc, &d->vd, flags); |
| 689 | } |
| 690 | |
Florian Meier | 96286b5 | 2014-01-06 20:18:24 +0100 | [diff] [blame] | 691 | static struct dma_async_tx_descriptor *bcm2835_dma_prep_dma_cyclic( |
| 692 | struct dma_chan *chan, dma_addr_t buf_addr, size_t buf_len, |
| 693 | size_t period_len, enum dma_transfer_direction direction, |
Laurent Pinchart | 31c1e5a | 2014-08-01 12:20:10 +0200 | [diff] [blame] | 694 | unsigned long flags) |
Florian Meier | 96286b5 | 2014-01-06 20:18:24 +0100 | [diff] [blame] | 695 | { |
| 696 | struct bcm2835_chan *c = to_bcm2835_dma_chan(chan); |
Florian Meier | 96286b5 | 2014-01-06 20:18:24 +0100 | [diff] [blame] | 697 | struct bcm2835_desc *d; |
Martin Sperl | 92153bb | 2016-03-16 12:24:59 -0700 | [diff] [blame] | 698 | dma_addr_t src, dst; |
| 699 | u32 info = BCM2835_DMA_WAIT_RESP; |
| 700 | u32 extra = BCM2835_DMA_INT_EN; |
Martin Sperl | 4087412 | 2016-03-16 12:25:00 -0700 | [diff] [blame] | 701 | size_t max_len = bcm2835_dma_max_frame_length(c); |
Martin Sperl | 92153bb | 2016-03-16 12:24:59 -0700 | [diff] [blame] | 702 | size_t frames; |
Florian Meier | 96286b5 | 2014-01-06 20:18:24 +0100 | [diff] [blame] | 703 | |
| 704 | /* Grab configuration */ |
| 705 | if (!is_slave_direction(direction)) { |
| 706 | dev_err(chan->device->dev, "%s: bad direction?\n", __func__); |
| 707 | return NULL; |
| 708 | } |
| 709 | |
Martin Sperl | 92153bb | 2016-03-16 12:24:59 -0700 | [diff] [blame] | 710 | if (!buf_len) { |
| 711 | dev_err(chan->device->dev, |
| 712 | "%s: bad buffer length (= 0)\n", __func__); |
Florian Meier | 96286b5 | 2014-01-06 20:18:24 +0100 | [diff] [blame] | 713 | return NULL; |
| 714 | } |
| 715 | |
Florian Meier | 96286b5 | 2014-01-06 20:18:24 +0100 | [diff] [blame] | 716 | /* |
Martin Sperl | 92153bb | 2016-03-16 12:24:59 -0700 | [diff] [blame] | 717 | * warn if buf_len is not a multiple of period_len - this may leed |
| 718 | * to unexpected latencies for interrupts and thus audiable clicks |
Florian Meier | 96286b5 | 2014-01-06 20:18:24 +0100 | [diff] [blame] | 719 | */ |
Martin Sperl | 92153bb | 2016-03-16 12:24:59 -0700 | [diff] [blame] | 720 | if (buf_len % period_len) |
| 721 | dev_warn_once(chan->device->dev, |
| 722 | "%s: buffer_length (%zd) is not a multiple of period_len (%zd)\n", |
| 723 | __func__, buf_len, period_len); |
Florian Meier | 96286b5 | 2014-01-06 20:18:24 +0100 | [diff] [blame] | 724 | |
Martin Sperl | 92153bb | 2016-03-16 12:24:59 -0700 | [diff] [blame] | 725 | /* Setup DREQ channel */ |
| 726 | if (c->dreq != 0) |
| 727 | info |= BCM2835_DMA_PER_MAP(c->dreq); |
Florian Meier | 96286b5 | 2014-01-06 20:18:24 +0100 | [diff] [blame] | 728 | |
Martin Sperl | 92153bb | 2016-03-16 12:24:59 -0700 | [diff] [blame] | 729 | if (direction == DMA_DEV_TO_MEM) { |
| 730 | if (c->cfg.src_addr_width != DMA_SLAVE_BUSWIDTH_4_BYTES) |
| 731 | return NULL; |
| 732 | src = c->cfg.src_addr; |
| 733 | dst = buf_addr; |
| 734 | info |= BCM2835_DMA_S_DREQ | BCM2835_DMA_D_INC; |
| 735 | } else { |
| 736 | if (c->cfg.dst_addr_width != DMA_SLAVE_BUSWIDTH_4_BYTES) |
| 737 | return NULL; |
| 738 | dst = c->cfg.dst_addr; |
| 739 | src = buf_addr; |
| 740 | info |= BCM2835_DMA_D_DREQ | BCM2835_DMA_S_INC; |
Florian Meier | 96286b5 | 2014-01-06 20:18:24 +0100 | [diff] [blame] | 741 | } |
| 742 | |
Martin Sperl | 92153bb | 2016-03-16 12:24:59 -0700 | [diff] [blame] | 743 | /* calculate number of frames */ |
Martin Sperl | 4087412 | 2016-03-16 12:25:00 -0700 | [diff] [blame] | 744 | frames = /* number of periods */ |
| 745 | DIV_ROUND_UP(buf_len, period_len) * |
| 746 | /* number of frames per period */ |
| 747 | bcm2835_dma_frames_for_length(period_len, max_len); |
Martin Sperl | 92153bb | 2016-03-16 12:24:59 -0700 | [diff] [blame] | 748 | |
| 749 | /* |
| 750 | * allocate the CB chain |
| 751 | * note that we need to use GFP_NOWAIT, as the ALSA i2s dmaengine |
| 752 | * implementation calls prep_dma_cyclic with interrupts disabled. |
| 753 | */ |
| 754 | d = bcm2835_dma_create_cb_chain(chan, direction, true, |
| 755 | info, extra, |
| 756 | frames, src, dst, buf_len, |
| 757 | period_len, GFP_NOWAIT); |
| 758 | if (!d) |
| 759 | return NULL; |
| 760 | |
| 761 | /* wrap around into a loop */ |
| 762 | d->cb_list[d->frames - 1].cb->next = d->cb_list[0].paddr; |
| 763 | |
Florian Meier | 96286b5 | 2014-01-06 20:18:24 +0100 | [diff] [blame] | 764 | return vchan_tx_prep(&c->vc, &d->vd, flags); |
| 765 | } |
| 766 | |
Maxime Ripard | 39159be | 2014-11-17 14:42:08 +0100 | [diff] [blame] | 767 | static int bcm2835_dma_slave_config(struct dma_chan *chan, |
| 768 | struct dma_slave_config *cfg) |
Florian Meier | 96286b5 | 2014-01-06 20:18:24 +0100 | [diff] [blame] | 769 | { |
Maxime Ripard | 39159be | 2014-11-17 14:42:08 +0100 | [diff] [blame] | 770 | struct bcm2835_chan *c = to_bcm2835_dma_chan(chan); |
| 771 | |
Florian Meier | 96286b5 | 2014-01-06 20:18:24 +0100 | [diff] [blame] | 772 | c->cfg = *cfg; |
| 773 | |
| 774 | return 0; |
| 775 | } |
| 776 | |
Maxime Ripard | 39159be | 2014-11-17 14:42:08 +0100 | [diff] [blame] | 777 | static int bcm2835_dma_terminate_all(struct dma_chan *chan) |
Florian Meier | 96286b5 | 2014-01-06 20:18:24 +0100 | [diff] [blame] | 778 | { |
Maxime Ripard | 39159be | 2014-11-17 14:42:08 +0100 | [diff] [blame] | 779 | struct bcm2835_chan *c = to_bcm2835_dma_chan(chan); |
Florian Meier | 96286b5 | 2014-01-06 20:18:24 +0100 | [diff] [blame] | 780 | struct bcm2835_dmadev *d = to_bcm2835_dma_dev(c->vc.chan.device); |
| 781 | unsigned long flags; |
Florian Meier | 96286b5 | 2014-01-06 20:18:24 +0100 | [diff] [blame] | 782 | LIST_HEAD(head); |
| 783 | |
| 784 | spin_lock_irqsave(&c->vc.lock, flags); |
| 785 | |
| 786 | /* Prevent this channel being scheduled */ |
| 787 | spin_lock(&d->lock); |
| 788 | list_del_init(&c->node); |
| 789 | spin_unlock(&d->lock); |
| 790 | |
Lukas Wunner | f7da7782 | 2019-01-23 09:26:00 +0100 | [diff] [blame] | 791 | /* stop DMA activity */ |
Florian Meier | 96286b5 | 2014-01-06 20:18:24 +0100 | [diff] [blame] | 792 | if (c->desc) { |
Peter Ujfalusi | de92436 | 2017-11-14 16:32:07 +0200 | [diff] [blame] | 793 | vchan_terminate_vdesc(&c->desc->vd); |
Florian Meier | 96286b5 | 2014-01-06 20:18:24 +0100 | [diff] [blame] | 794 | c->desc = NULL; |
Lukas Wunner | 9e528c7 | 2019-01-23 09:26:00 +0100 | [diff] [blame] | 795 | bcm2835_dma_abort(c); |
Florian Meier | 96286b5 | 2014-01-06 20:18:24 +0100 | [diff] [blame] | 796 | } |
| 797 | |
| 798 | vchan_get_all_descriptors(&c->vc, &head); |
| 799 | spin_unlock_irqrestore(&c->vc.lock, flags); |
| 800 | vchan_dma_desc_free_list(&c->vc, &head); |
| 801 | |
| 802 | return 0; |
| 803 | } |
| 804 | |
Peter Ujfalusi | de92436 | 2017-11-14 16:32:07 +0200 | [diff] [blame] | 805 | static void bcm2835_dma_synchronize(struct dma_chan *chan) |
| 806 | { |
| 807 | struct bcm2835_chan *c = to_bcm2835_dma_chan(chan); |
| 808 | |
| 809 | vchan_synchronize(&c->vc); |
| 810 | } |
| 811 | |
Martin Sperl | e2eca63 | 2016-04-11 13:29:08 +0000 | [diff] [blame] | 812 | static int bcm2835_dma_chan_init(struct bcm2835_dmadev *d, int chan_id, |
| 813 | int irq, unsigned int irq_flags) |
Florian Meier | 96286b5 | 2014-01-06 20:18:24 +0100 | [diff] [blame] | 814 | { |
| 815 | struct bcm2835_chan *c; |
| 816 | |
| 817 | c = devm_kzalloc(d->ddev.dev, sizeof(*c), GFP_KERNEL); |
| 818 | if (!c) |
| 819 | return -ENOMEM; |
| 820 | |
| 821 | c->vc.desc_free = bcm2835_dma_desc_free; |
| 822 | vchan_init(&c->vc, &d->ddev); |
| 823 | INIT_LIST_HEAD(&c->node); |
| 824 | |
Florian Meier | 96286b5 | 2014-01-06 20:18:24 +0100 | [diff] [blame] | 825 | c->chan_base = BCM2835_DMA_CHANIO(d->base, chan_id); |
| 826 | c->ch = chan_id; |
| 827 | c->irq_number = irq; |
Martin Sperl | e2eca63 | 2016-04-11 13:29:08 +0000 | [diff] [blame] | 828 | c->irq_flags = irq_flags; |
Florian Meier | 96286b5 | 2014-01-06 20:18:24 +0100 | [diff] [blame] | 829 | |
Martin Sperl | 4087412 | 2016-03-16 12:25:00 -0700 | [diff] [blame] | 830 | /* check in DEBUG register if this is a LITE channel */ |
| 831 | if (readl(c->chan_base + BCM2835_DMA_DEBUG) & |
| 832 | BCM2835_DMA_DEBUG_LITE) |
| 833 | c->is_lite_channel = true; |
| 834 | |
Florian Meier | 96286b5 | 2014-01-06 20:18:24 +0100 | [diff] [blame] | 835 | return 0; |
| 836 | } |
| 837 | |
| 838 | static void bcm2835_dma_free(struct bcm2835_dmadev *od) |
| 839 | { |
| 840 | struct bcm2835_chan *c, *next; |
| 841 | |
| 842 | list_for_each_entry_safe(c, next, &od->ddev.channels, |
| 843 | vc.chan.device_node) { |
| 844 | list_del(&c->vc.chan.device_node); |
| 845 | tasklet_kill(&c->vc.task); |
| 846 | } |
| 847 | } |
| 848 | |
| 849 | static const struct of_device_id bcm2835_dma_of_match[] = { |
| 850 | { .compatible = "brcm,bcm2835-dma", }, |
| 851 | {}, |
| 852 | }; |
| 853 | MODULE_DEVICE_TABLE(of, bcm2835_dma_of_match); |
| 854 | |
| 855 | static struct dma_chan *bcm2835_dma_xlate(struct of_phandle_args *spec, |
| 856 | struct of_dma *ofdma) |
| 857 | { |
| 858 | struct bcm2835_dmadev *d = ofdma->of_dma_data; |
| 859 | struct dma_chan *chan; |
| 860 | |
| 861 | chan = dma_get_any_slave_channel(&d->ddev); |
| 862 | if (!chan) |
| 863 | return NULL; |
| 864 | |
| 865 | /* Set DREQ from param */ |
| 866 | to_bcm2835_dma_chan(chan)->dreq = spec->args[0]; |
| 867 | |
| 868 | return chan; |
| 869 | } |
| 870 | |
Florian Meier | 96286b5 | 2014-01-06 20:18:24 +0100 | [diff] [blame] | 871 | static int bcm2835_dma_probe(struct platform_device *pdev) |
| 872 | { |
| 873 | struct bcm2835_dmadev *od; |
| 874 | struct resource *res; |
| 875 | void __iomem *base; |
| 876 | int rc; |
Martin Sperl | e2eca63 | 2016-04-11 13:29:08 +0000 | [diff] [blame] | 877 | int i, j; |
| 878 | int irq[BCM2835_DMA_MAX_DMA_CHAN_SUPPORTED + 1]; |
| 879 | int irq_flags; |
Florian Meier | 96286b5 | 2014-01-06 20:18:24 +0100 | [diff] [blame] | 880 | uint32_t chans_available; |
Martin Sperl | e2eca63 | 2016-04-11 13:29:08 +0000 | [diff] [blame] | 881 | char chan_name[BCM2835_DMA_CHAN_NAME_SIZE]; |
Florian Meier | 96286b5 | 2014-01-06 20:18:24 +0100 | [diff] [blame] | 882 | |
| 883 | if (!pdev->dev.dma_mask) |
| 884 | pdev->dev.dma_mask = &pdev->dev.coherent_dma_mask; |
| 885 | |
| 886 | rc = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)); |
| 887 | if (rc) |
| 888 | return rc; |
| 889 | |
| 890 | od = devm_kzalloc(&pdev->dev, sizeof(*od), GFP_KERNEL); |
| 891 | if (!od) |
| 892 | return -ENOMEM; |
| 893 | |
| 894 | pdev->dev.dma_parms = &od->dma_parms; |
| 895 | dma_set_max_seg_size(&pdev->dev, 0x3FFFFFFF); |
| 896 | |
| 897 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 898 | base = devm_ioremap_resource(&pdev->dev, res); |
| 899 | if (IS_ERR(base)) |
| 900 | return PTR_ERR(base); |
| 901 | |
| 902 | od->base = base; |
| 903 | |
| 904 | dma_cap_set(DMA_SLAVE, od->ddev.cap_mask); |
Florian Meier | 7f5ae35 | 2014-01-17 18:06:29 +0100 | [diff] [blame] | 905 | dma_cap_set(DMA_PRIVATE, od->ddev.cap_mask); |
Florian Meier | 96286b5 | 2014-01-06 20:18:24 +0100 | [diff] [blame] | 906 | dma_cap_set(DMA_CYCLIC, od->ddev.cap_mask); |
Martin Sperl | 388cc7a | 2016-03-16 12:25:01 -0700 | [diff] [blame] | 907 | dma_cap_set(DMA_SLAVE, od->ddev.cap_mask); |
Martin Sperl | d9f094a | 2016-03-16 12:25:02 -0700 | [diff] [blame] | 908 | dma_cap_set(DMA_MEMCPY, od->ddev.cap_mask); |
Florian Meier | 96286b5 | 2014-01-06 20:18:24 +0100 | [diff] [blame] | 909 | od->ddev.device_alloc_chan_resources = bcm2835_dma_alloc_chan_resources; |
| 910 | od->ddev.device_free_chan_resources = bcm2835_dma_free_chan_resources; |
| 911 | od->ddev.device_tx_status = bcm2835_dma_tx_status; |
| 912 | od->ddev.device_issue_pending = bcm2835_dma_issue_pending; |
Florian Meier | 96286b5 | 2014-01-06 20:18:24 +0100 | [diff] [blame] | 913 | od->ddev.device_prep_dma_cyclic = bcm2835_dma_prep_dma_cyclic; |
Martin Sperl | 388cc7a | 2016-03-16 12:25:01 -0700 | [diff] [blame] | 914 | od->ddev.device_prep_slave_sg = bcm2835_dma_prep_slave_sg; |
Martin Sperl | d9f094a | 2016-03-16 12:25:02 -0700 | [diff] [blame] | 915 | od->ddev.device_prep_dma_memcpy = bcm2835_dma_prep_dma_memcpy; |
Maxime Ripard | 39159be | 2014-11-17 14:42:08 +0100 | [diff] [blame] | 916 | od->ddev.device_config = bcm2835_dma_slave_config; |
| 917 | od->ddev.device_terminate_all = bcm2835_dma_terminate_all; |
Peter Ujfalusi | de92436 | 2017-11-14 16:32:07 +0200 | [diff] [blame] | 918 | od->ddev.device_synchronize = bcm2835_dma_synchronize; |
Maxime Ripard | b574368 | 2014-11-17 14:42:45 +0100 | [diff] [blame] | 919 | od->ddev.src_addr_widths = BIT(DMA_SLAVE_BUSWIDTH_4_BYTES); |
| 920 | od->ddev.dst_addr_widths = BIT(DMA_SLAVE_BUSWIDTH_4_BYTES); |
Martin Sperl | d9f094a | 2016-03-16 12:25:02 -0700 | [diff] [blame] | 921 | od->ddev.directions = BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV) | |
| 922 | BIT(DMA_MEM_TO_MEM); |
Martin Sperl | 0fa5867 | 2016-03-16 12:24:55 -0700 | [diff] [blame] | 923 | od->ddev.residue_granularity = DMA_RESIDUE_GRANULARITY_BURST; |
Florian Meier | 96286b5 | 2014-01-06 20:18:24 +0100 | [diff] [blame] | 924 | od->ddev.dev = &pdev->dev; |
| 925 | INIT_LIST_HEAD(&od->ddev.channels); |
| 926 | spin_lock_init(&od->lock); |
| 927 | |
| 928 | platform_set_drvdata(pdev, od); |
| 929 | |
| 930 | /* Request DMA channel mask from device tree */ |
| 931 | if (of_property_read_u32(pdev->dev.of_node, |
| 932 | "brcm,dma-channel-mask", |
| 933 | &chans_available)) { |
| 934 | dev_err(&pdev->dev, "Failed to get channel mask\n"); |
| 935 | rc = -EINVAL; |
| 936 | goto err_no_dma; |
| 937 | } |
| 938 | |
Martin Sperl | e2eca63 | 2016-04-11 13:29:08 +0000 | [diff] [blame] | 939 | /* get irqs for each channel that we support */ |
| 940 | for (i = 0; i <= BCM2835_DMA_MAX_DMA_CHAN_SUPPORTED; i++) { |
| 941 | /* skip masked out channels */ |
| 942 | if (!(chans_available & (1 << i))) { |
| 943 | irq[i] = -1; |
| 944 | continue; |
Florian Meier | 96286b5 | 2014-01-06 20:18:24 +0100 | [diff] [blame] | 945 | } |
Martin Sperl | e2eca63 | 2016-04-11 13:29:08 +0000 | [diff] [blame] | 946 | |
| 947 | /* get the named irq */ |
| 948 | snprintf(chan_name, sizeof(chan_name), "dma%i", i); |
| 949 | irq[i] = platform_get_irq_byname(pdev, chan_name); |
| 950 | if (irq[i] >= 0) |
| 951 | continue; |
| 952 | |
| 953 | /* legacy device tree case handling */ |
| 954 | dev_warn_once(&pdev->dev, |
Martin Sperl | 0eef727 | 2016-04-22 07:12:48 +0000 | [diff] [blame] | 955 | "missing interrupt-names property in device tree - legacy interpretation is used\n"); |
Martin Sperl | e2eca63 | 2016-04-11 13:29:08 +0000 | [diff] [blame] | 956 | /* |
| 957 | * in case of channel >= 11 |
| 958 | * use the 11th interrupt and that is shared |
| 959 | */ |
| 960 | irq[i] = platform_get_irq(pdev, i < 11 ? i : 11); |
| 961 | } |
| 962 | |
| 963 | /* get irqs for each channel */ |
| 964 | for (i = 0; i <= BCM2835_DMA_MAX_DMA_CHAN_SUPPORTED; i++) { |
| 965 | /* skip channels without irq */ |
| 966 | if (irq[i] < 0) |
| 967 | continue; |
| 968 | |
| 969 | /* check if there are other channels that also use this irq */ |
| 970 | irq_flags = 0; |
| 971 | for (j = 0; j <= BCM2835_DMA_MAX_DMA_CHAN_SUPPORTED; j++) |
| 972 | if ((i != j) && (irq[j] == irq[i])) { |
| 973 | irq_flags = IRQF_SHARED; |
| 974 | break; |
| 975 | } |
| 976 | |
| 977 | /* initialize the channel */ |
| 978 | rc = bcm2835_dma_chan_init(od, i, irq[i], irq_flags); |
| 979 | if (rc) |
| 980 | goto err_no_dma; |
Florian Meier | 96286b5 | 2014-01-06 20:18:24 +0100 | [diff] [blame] | 981 | } |
| 982 | |
| 983 | dev_dbg(&pdev->dev, "Initialized %i DMA channels\n", i); |
| 984 | |
| 985 | /* Device-tree DMA controller registration */ |
| 986 | rc = of_dma_controller_register(pdev->dev.of_node, |
| 987 | bcm2835_dma_xlate, od); |
| 988 | if (rc) { |
| 989 | dev_err(&pdev->dev, "Failed to register DMA controller\n"); |
| 990 | goto err_no_dma; |
| 991 | } |
| 992 | |
| 993 | rc = dma_async_device_register(&od->ddev); |
| 994 | if (rc) { |
| 995 | dev_err(&pdev->dev, |
| 996 | "Failed to register slave DMA engine device: %d\n", rc); |
| 997 | goto err_no_dma; |
| 998 | } |
| 999 | |
| 1000 | dev_dbg(&pdev->dev, "Load BCM2835 DMA engine driver\n"); |
| 1001 | |
| 1002 | return 0; |
| 1003 | |
| 1004 | err_no_dma: |
| 1005 | bcm2835_dma_free(od); |
| 1006 | return rc; |
| 1007 | } |
| 1008 | |
| 1009 | static int bcm2835_dma_remove(struct platform_device *pdev) |
| 1010 | { |
| 1011 | struct bcm2835_dmadev *od = platform_get_drvdata(pdev); |
| 1012 | |
| 1013 | dma_async_device_unregister(&od->ddev); |
| 1014 | bcm2835_dma_free(od); |
| 1015 | |
| 1016 | return 0; |
| 1017 | } |
| 1018 | |
| 1019 | static struct platform_driver bcm2835_dma_driver = { |
| 1020 | .probe = bcm2835_dma_probe, |
| 1021 | .remove = bcm2835_dma_remove, |
| 1022 | .driver = { |
| 1023 | .name = "bcm2835-dma", |
Florian Meier | 96286b5 | 2014-01-06 20:18:24 +0100 | [diff] [blame] | 1024 | .of_match_table = of_match_ptr(bcm2835_dma_of_match), |
| 1025 | }, |
| 1026 | }; |
| 1027 | |
| 1028 | module_platform_driver(bcm2835_dma_driver); |
| 1029 | |
| 1030 | MODULE_ALIAS("platform:bcm2835-dma"); |
| 1031 | MODULE_DESCRIPTION("BCM2835 DMA engine driver"); |
| 1032 | MODULE_AUTHOR("Florian Meier <florian.meier@koalo.de>"); |
Stefan Wahren | ab39e147 | 2018-10-23 13:06:07 +0200 | [diff] [blame] | 1033 | MODULE_LICENSE("GPL"); |