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