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