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