dmaengine: axi-dmac: Small code cleanup
[linux-2.6-block.git] / drivers / dma / dma-axi-dmac.c
CommitLineData
fda8d26e 1// SPDX-License-Identifier: GPL-2.0-only
0e3b67b3
LPC
2/*
3 * Driver for the Analog Devices AXI-DMAC core
4 *
f4a9fe97 5 * Copyright 2013-2019 Analog Devices Inc.
0e3b67b3 6 * Author: Lars-Peter Clausen <lars@metafoo.de>
0e3b67b3
LPC
7 */
8
78a2f92e 9#include <linux/bitfield.h>
0e3b67b3
LPC
10#include <linux/clk.h>
11#include <linux/device.h>
12#include <linux/dma-mapping.h>
13#include <linux/dmaengine.h>
14#include <linux/err.h>
15#include <linux/interrupt.h>
16#include <linux/io.h>
17#include <linux/kernel.h>
18#include <linux/module.h>
19#include <linux/of.h>
20#include <linux/of_dma.h>
9327c7e7 21#include <linux/of_address.h>
0e3b67b3 22#include <linux/platform_device.h>
fc15be39 23#include <linux/regmap.h>
0e3b67b3 24#include <linux/slab.h>
a5b20600 25#include <linux/fpga/adi-axi-common.h>
0e3b67b3
LPC
26
27#include <dt-bindings/dma/axi-dmac.h>
28
29#include "dmaengine.h"
30#include "virt-dma.h"
31
32/*
33 * The AXI-DMAC is a soft IP core that is used in FPGA designs. The core has
34 * various instantiation parameters which decided the exact feature set support
35 * by the core.
36 *
37 * Each channel of the core has a source interface and a destination interface.
38 * The number of channels and the type of the channel interfaces is selected at
39 * configuration time. A interface can either be a connected to a central memory
40 * interconnect, which allows access to system memory, or it can be connected to
41 * a dedicated bus which is directly connected to a data port on a peripheral.
42 * Given that those are configuration options of the core that are selected when
43 * it is instantiated this means that they can not be changed by software at
44 * runtime. By extension this means that each channel is uni-directional. It can
45 * either be device to memory or memory to device, but not both. Also since the
46 * device side is a dedicated data bus only connected to a single peripheral
47 * there is no address than can or needs to be configured for the device side.
48 */
49
78a2f92e
AA
50#define AXI_DMAC_REG_INTERFACE_DESC 0x10
51#define AXI_DMAC_DMA_SRC_TYPE_MSK GENMASK(13, 12)
52#define AXI_DMAC_DMA_SRC_TYPE_GET(x) FIELD_GET(AXI_DMAC_DMA_SRC_TYPE_MSK, x)
53#define AXI_DMAC_DMA_SRC_WIDTH_MSK GENMASK(11, 8)
54#define AXI_DMAC_DMA_SRC_WIDTH_GET(x) FIELD_GET(AXI_DMAC_DMA_SRC_WIDTH_MSK, x)
55#define AXI_DMAC_DMA_DST_TYPE_MSK GENMASK(5, 4)
56#define AXI_DMAC_DMA_DST_TYPE_GET(x) FIELD_GET(AXI_DMAC_DMA_DST_TYPE_MSK, x)
57#define AXI_DMAC_DMA_DST_WIDTH_MSK GENMASK(3, 0)
58#define AXI_DMAC_DMA_DST_WIDTH_GET(x) FIELD_GET(AXI_DMAC_DMA_DST_WIDTH_MSK, x)
9327c7e7
MT
59#define AXI_DMAC_REG_COHERENCY_DESC 0x14
60#define AXI_DMAC_DST_COHERENT_MSK BIT(0)
61#define AXI_DMAC_DST_COHERENT_GET(x) FIELD_GET(AXI_DMAC_DST_COHERENT_MSK, x)
78a2f92e 62
0e3b67b3
LPC
63#define AXI_DMAC_REG_IRQ_MASK 0x80
64#define AXI_DMAC_REG_IRQ_PENDING 0x84
65#define AXI_DMAC_REG_IRQ_SOURCE 0x88
66
67#define AXI_DMAC_REG_CTRL 0x400
68#define AXI_DMAC_REG_TRANSFER_ID 0x404
69#define AXI_DMAC_REG_START_TRANSFER 0x408
70#define AXI_DMAC_REG_FLAGS 0x40c
71#define AXI_DMAC_REG_DEST_ADDRESS 0x410
72#define AXI_DMAC_REG_SRC_ADDRESS 0x414
73#define AXI_DMAC_REG_X_LENGTH 0x418
74#define AXI_DMAC_REG_Y_LENGTH 0x41c
75#define AXI_DMAC_REG_DEST_STRIDE 0x420
76#define AXI_DMAC_REG_SRC_STRIDE 0x424
77#define AXI_DMAC_REG_TRANSFER_DONE 0x428
78#define AXI_DMAC_REG_ACTIVE_TRANSFER_ID 0x42c
79#define AXI_DMAC_REG_STATUS 0x430
80#define AXI_DMAC_REG_CURRENT_SRC_ADDR 0x434
81#define AXI_DMAC_REG_CURRENT_DEST_ADDR 0x438
e3923592
AA
82#define AXI_DMAC_REG_PARTIAL_XFER_LEN 0x44c
83#define AXI_DMAC_REG_PARTIAL_XFER_ID 0x450
0e3b67b3
LPC
84
85#define AXI_DMAC_CTRL_ENABLE BIT(0)
86#define AXI_DMAC_CTRL_PAUSE BIT(1)
87
88#define AXI_DMAC_IRQ_SOT BIT(0)
89#define AXI_DMAC_IRQ_EOT BIT(1)
90
91#define AXI_DMAC_FLAG_CYCLIC BIT(0)
a3ee0bf2 92#define AXI_DMAC_FLAG_LAST BIT(1)
e3923592
AA
93#define AXI_DMAC_FLAG_PARTIAL_REPORT BIT(2)
94
95#define AXI_DMAC_FLAG_PARTIAL_XFER_DONE BIT(31)
0e3b67b3 96
008913db
LPC
97/* The maximum ID allocated by the hardware is 31 */
98#define AXI_DMAC_SG_UNUSED 32U
99
0e3b67b3
LPC
100struct axi_dmac_sg {
101 dma_addr_t src_addr;
102 dma_addr_t dest_addr;
103 unsigned int x_len;
104 unsigned int y_len;
105 unsigned int dest_stride;
106 unsigned int src_stride;
107 unsigned int id;
e3923592 108 unsigned int partial_len;
008913db 109 bool schedule_when_free;
0e3b67b3
LPC
110};
111
112struct axi_dmac_desc {
113 struct virt_dma_desc vdesc;
114 bool cyclic;
e28d9155 115 bool have_partial_xfer;
0e3b67b3
LPC
116
117 unsigned int num_submitted;
118 unsigned int num_completed;
119 unsigned int num_sgs;
f1bc0d01 120 struct axi_dmac_sg sg[] __counted_by(num_sgs);
0e3b67b3
LPC
121};
122
123struct axi_dmac_chan {
124 struct virt_dma_chan vchan;
125
126 struct axi_dmac_desc *next_desc;
127 struct list_head active_descs;
128 enum dma_transfer_direction direction;
129
130 unsigned int src_width;
131 unsigned int dest_width;
132 unsigned int src_type;
133 unsigned int dest_type;
134
135 unsigned int max_length;
a5b20600
LPC
136 unsigned int address_align_mask;
137 unsigned int length_align_mask;
0e3b67b3 138
e3923592 139 bool hw_partial_xfer;
0e3b67b3
LPC
140 bool hw_cyclic;
141 bool hw_2d;
142};
143
144struct axi_dmac {
145 void __iomem *base;
146 int irq;
147
148 struct clk *clk;
149
150 struct dma_device dma_dev;
151 struct axi_dmac_chan chan;
0e3b67b3
LPC
152};
153
154static struct axi_dmac *chan_to_axi_dmac(struct axi_dmac_chan *chan)
155{
156 return container_of(chan->vchan.chan.device, struct axi_dmac,
157 dma_dev);
158}
159
160static struct axi_dmac_chan *to_axi_dmac_chan(struct dma_chan *c)
161{
162 return container_of(c, struct axi_dmac_chan, vchan.chan);
163}
164
165static struct axi_dmac_desc *to_axi_dmac_desc(struct virt_dma_desc *vdesc)
166{
167 return container_of(vdesc, struct axi_dmac_desc, vdesc);
168}
169
170static void axi_dmac_write(struct axi_dmac *axi_dmac, unsigned int reg,
171 unsigned int val)
172{
173 writel(val, axi_dmac->base + reg);
174}
175
176static int axi_dmac_read(struct axi_dmac *axi_dmac, unsigned int reg)
177{
178 return readl(axi_dmac->base + reg);
179}
180
181static int axi_dmac_src_is_mem(struct axi_dmac_chan *chan)
182{
183 return chan->src_type == AXI_DMAC_BUS_TYPE_AXI_MM;
184}
185
186static int axi_dmac_dest_is_mem(struct axi_dmac_chan *chan)
187{
188 return chan->dest_type == AXI_DMAC_BUS_TYPE_AXI_MM;
189}
190
191static bool axi_dmac_check_len(struct axi_dmac_chan *chan, unsigned int len)
192{
921234e0 193 if (len == 0)
0e3b67b3 194 return false;
a5b20600 195 if ((len & chan->length_align_mask) != 0) /* Not aligned */
0e3b67b3
LPC
196 return false;
197 return true;
198}
199
200static bool axi_dmac_check_addr(struct axi_dmac_chan *chan, dma_addr_t addr)
201{
a5b20600 202 if ((addr & chan->address_align_mask) != 0) /* Not aligned */
0e3b67b3
LPC
203 return false;
204 return true;
205}
206
207static void axi_dmac_start_transfer(struct axi_dmac_chan *chan)
208{
209 struct axi_dmac *dmac = chan_to_axi_dmac(chan);
210 struct virt_dma_desc *vdesc;
211 struct axi_dmac_desc *desc;
212 struct axi_dmac_sg *sg;
213 unsigned int flags = 0;
214 unsigned int val;
215
216 val = axi_dmac_read(dmac, AXI_DMAC_REG_START_TRANSFER);
217 if (val) /* Queue is full, wait for the next SOT IRQ */
218 return;
219
220 desc = chan->next_desc;
221
222 if (!desc) {
223 vdesc = vchan_next_desc(&chan->vchan);
224 if (!vdesc)
225 return;
226 list_move_tail(&vdesc->node, &chan->active_descs);
227 desc = to_axi_dmac_desc(vdesc);
228 }
229 sg = &desc->sg[desc->num_submitted];
230
008913db
LPC
231 /* Already queued in cyclic mode. Wait for it to finish */
232 if (sg->id != AXI_DMAC_SG_UNUSED) {
233 sg->schedule_when_free = true;
234 return;
235 }
236
0e3b67b3 237 desc->num_submitted++;
e28d9155
AA
238 if (desc->num_submitted == desc->num_sgs ||
239 desc->have_partial_xfer) {
008913db
LPC
240 if (desc->cyclic)
241 desc->num_submitted = 0; /* Start again */
242 else
243 chan->next_desc = NULL;
a3ee0bf2 244 flags |= AXI_DMAC_FLAG_LAST;
008913db 245 } else {
0e3b67b3 246 chan->next_desc = desc;
008913db 247 }
0e3b67b3
LPC
248
249 sg->id = axi_dmac_read(dmac, AXI_DMAC_REG_TRANSFER_ID);
250
251 if (axi_dmac_dest_is_mem(chan)) {
252 axi_dmac_write(dmac, AXI_DMAC_REG_DEST_ADDRESS, sg->dest_addr);
253 axi_dmac_write(dmac, AXI_DMAC_REG_DEST_STRIDE, sg->dest_stride);
254 }
255
256 if (axi_dmac_src_is_mem(chan)) {
257 axi_dmac_write(dmac, AXI_DMAC_REG_SRC_ADDRESS, sg->src_addr);
258 axi_dmac_write(dmac, AXI_DMAC_REG_SRC_STRIDE, sg->src_stride);
259 }
260
261 /*
262 * If the hardware supports cyclic transfers and there is no callback to
63ab76db
LPC
263 * call and only a single segment, enable hw cyclic mode to avoid
264 * unnecessary interrupts.
0e3b67b3 265 */
63ab76db
LPC
266 if (chan->hw_cyclic && desc->cyclic && !desc->vdesc.tx.callback &&
267 desc->num_sgs == 1)
0e3b67b3
LPC
268 flags |= AXI_DMAC_FLAG_CYCLIC;
269
e3923592
AA
270 if (chan->hw_partial_xfer)
271 flags |= AXI_DMAC_FLAG_PARTIAL_REPORT;
272
0e3b67b3
LPC
273 axi_dmac_write(dmac, AXI_DMAC_REG_X_LENGTH, sg->x_len - 1);
274 axi_dmac_write(dmac, AXI_DMAC_REG_Y_LENGTH, sg->y_len - 1);
275 axi_dmac_write(dmac, AXI_DMAC_REG_FLAGS, flags);
276 axi_dmac_write(dmac, AXI_DMAC_REG_START_TRANSFER, 1);
277}
278
279static struct axi_dmac_desc *axi_dmac_active_desc(struct axi_dmac_chan *chan)
280{
281 return list_first_entry_or_null(&chan->active_descs,
282 struct axi_dmac_desc, vdesc.node);
283}
284
e3923592
AA
285static inline unsigned int axi_dmac_total_sg_bytes(struct axi_dmac_chan *chan,
286 struct axi_dmac_sg *sg)
287{
288 if (chan->hw_2d)
289 return sg->x_len * sg->y_len;
290 else
291 return sg->x_len;
292}
293
294static void axi_dmac_dequeue_partial_xfers(struct axi_dmac_chan *chan)
295{
296 struct axi_dmac *dmac = chan_to_axi_dmac(chan);
297 struct axi_dmac_desc *desc;
298 struct axi_dmac_sg *sg;
299 u32 xfer_done, len, id, i;
300 bool found_sg;
301
302 do {
303 len = axi_dmac_read(dmac, AXI_DMAC_REG_PARTIAL_XFER_LEN);
304 id = axi_dmac_read(dmac, AXI_DMAC_REG_PARTIAL_XFER_ID);
305
306 found_sg = false;
307 list_for_each_entry(desc, &chan->active_descs, vdesc.node) {
308 for (i = 0; i < desc->num_sgs; i++) {
309 sg = &desc->sg[i];
310 if (sg->id == AXI_DMAC_SG_UNUSED)
311 continue;
312 if (sg->id == id) {
e28d9155 313 desc->have_partial_xfer = true;
e3923592
AA
314 sg->partial_len = len;
315 found_sg = true;
316 break;
317 }
318 }
319 if (found_sg)
320 break;
321 }
322
323 if (found_sg) {
324 dev_dbg(dmac->dma_dev.dev,
325 "Found partial segment id=%u, len=%u\n",
326 id, len);
327 } else {
328 dev_warn(dmac->dma_dev.dev,
329 "Not found partial segment id=%u, len=%u\n",
330 id, len);
331 }
332
333 /* Check if we have any more partial transfers */
334 xfer_done = axi_dmac_read(dmac, AXI_DMAC_REG_TRANSFER_DONE);
335 xfer_done = !(xfer_done & AXI_DMAC_FLAG_PARTIAL_XFER_DONE);
336
337 } while (!xfer_done);
338}
339
340static void axi_dmac_compute_residue(struct axi_dmac_chan *chan,
341 struct axi_dmac_desc *active)
342{
343 struct dmaengine_result *rslt = &active->vdesc.tx_result;
344 unsigned int start = active->num_completed - 1;
345 struct axi_dmac_sg *sg;
346 unsigned int i, total;
347
348 rslt->result = DMA_TRANS_NOERROR;
349 rslt->residue = 0;
350
351 /*
352 * We get here if the last completed segment is partial, which
353 * means we can compute the residue from that segment onwards
354 */
355 for (i = start; i < active->num_sgs; i++) {
356 sg = &active->sg[i];
357 total = axi_dmac_total_sg_bytes(chan, sg);
358 rslt->residue += (total - sg->partial_len);
359 }
360}
361
008913db 362static bool axi_dmac_transfer_done(struct axi_dmac_chan *chan,
0e3b67b3
LPC
363 unsigned int completed_transfers)
364{
365 struct axi_dmac_desc *active;
366 struct axi_dmac_sg *sg;
008913db 367 bool start_next = false;
0e3b67b3
LPC
368
369 active = axi_dmac_active_desc(chan);
370 if (!active)
008913db 371 return false;
0e3b67b3 372
e3923592
AA
373 if (chan->hw_partial_xfer &&
374 (completed_transfers & AXI_DMAC_FLAG_PARTIAL_XFER_DONE))
375 axi_dmac_dequeue_partial_xfers(chan);
376
008913db
LPC
377 do {
378 sg = &active->sg[active->num_completed];
379 if (sg->id == AXI_DMAC_SG_UNUSED) /* Not yet submitted */
380 break;
381 if (!(BIT(sg->id) & completed_transfers))
382 break;
383 active->num_completed++;
384 sg->id = AXI_DMAC_SG_UNUSED;
385 if (sg->schedule_when_free) {
386 sg->schedule_when_free = false;
387 start_next = true;
388 }
389
e3923592
AA
390 if (sg->partial_len)
391 axi_dmac_compute_residue(chan, active);
392
008913db
LPC
393 if (active->cyclic)
394 vchan_cyclic_callback(&active->vdesc);
395
e3923592
AA
396 if (active->num_completed == active->num_sgs ||
397 sg->partial_len) {
008913db
LPC
398 if (active->cyclic) {
399 active->num_completed = 0; /* wrap around */
400 } else {
0e3b67b3
LPC
401 list_del(&active->vdesc.node);
402 vchan_cookie_complete(&active->vdesc);
403 active = axi_dmac_active_desc(chan);
404 }
008913db
LPC
405 }
406 } while (active);
407
408 return start_next;
0e3b67b3
LPC
409}
410
411static irqreturn_t axi_dmac_interrupt_handler(int irq, void *devid)
412{
413 struct axi_dmac *dmac = devid;
414 unsigned int pending;
008913db 415 bool start_next = false;
0e3b67b3
LPC
416
417 pending = axi_dmac_read(dmac, AXI_DMAC_REG_IRQ_PENDING);
71831f65
LPC
418 if (!pending)
419 return IRQ_NONE;
420
0e3b67b3
LPC
421 axi_dmac_write(dmac, AXI_DMAC_REG_IRQ_PENDING, pending);
422
423 spin_lock(&dmac->chan.vchan.lock);
424 /* One or more transfers have finished */
425 if (pending & AXI_DMAC_IRQ_EOT) {
426 unsigned int completed;
427
428 completed = axi_dmac_read(dmac, AXI_DMAC_REG_TRANSFER_DONE);
008913db 429 start_next = axi_dmac_transfer_done(&dmac->chan, completed);
0e3b67b3
LPC
430 }
431 /* Space has become available in the descriptor queue */
008913db 432 if ((pending & AXI_DMAC_IRQ_SOT) || start_next)
0e3b67b3
LPC
433 axi_dmac_start_transfer(&dmac->chan);
434 spin_unlock(&dmac->chan.vchan.lock);
435
436 return IRQ_HANDLED;
437}
438
439static int axi_dmac_terminate_all(struct dma_chan *c)
440{
441 struct axi_dmac_chan *chan = to_axi_dmac_chan(c);
442 struct axi_dmac *dmac = chan_to_axi_dmac(chan);
443 unsigned long flags;
444 LIST_HEAD(head);
445
446 spin_lock_irqsave(&chan->vchan.lock, flags);
447 axi_dmac_write(dmac, AXI_DMAC_REG_CTRL, 0);
448 chan->next_desc = NULL;
449 vchan_get_all_descriptors(&chan->vchan, &head);
450 list_splice_tail_init(&chan->active_descs, &head);
451 spin_unlock_irqrestore(&chan->vchan.lock, flags);
452
453 vchan_dma_desc_free_list(&chan->vchan, &head);
454
455 return 0;
456}
457
860dd64c
LPC
458static void axi_dmac_synchronize(struct dma_chan *c)
459{
460 struct axi_dmac_chan *chan = to_axi_dmac_chan(c);
461
462 vchan_synchronize(&chan->vchan);
463}
464
0e3b67b3
LPC
465static void axi_dmac_issue_pending(struct dma_chan *c)
466{
467 struct axi_dmac_chan *chan = to_axi_dmac_chan(c);
468 struct axi_dmac *dmac = chan_to_axi_dmac(chan);
469 unsigned long flags;
470
471 axi_dmac_write(dmac, AXI_DMAC_REG_CTRL, AXI_DMAC_CTRL_ENABLE);
472
473 spin_lock_irqsave(&chan->vchan.lock, flags);
474 if (vchan_issue_pending(&chan->vchan))
475 axi_dmac_start_transfer(chan);
476 spin_unlock_irqrestore(&chan->vchan.lock, flags);
477}
478
479static struct axi_dmac_desc *axi_dmac_alloc_desc(unsigned int num_sgs)
480{
481 struct axi_dmac_desc *desc;
008913db 482 unsigned int i;
0e3b67b3 483
48b02a85 484 desc = kzalloc(struct_size(desc, sg, num_sgs), GFP_NOWAIT);
0e3b67b3
LPC
485 if (!desc)
486 return NULL;
f1bc0d01 487 desc->num_sgs = num_sgs;
0e3b67b3 488
008913db
LPC
489 for (i = 0; i < num_sgs; i++)
490 desc->sg[i].id = AXI_DMAC_SG_UNUSED;
491
0e3b67b3
LPC
492 return desc;
493}
494
921234e0
LPC
495static struct axi_dmac_sg *axi_dmac_fill_linear_sg(struct axi_dmac_chan *chan,
496 enum dma_transfer_direction direction, dma_addr_t addr,
497 unsigned int num_periods, unsigned int period_len,
498 struct axi_dmac_sg *sg)
499{
500 unsigned int num_segments, i;
501 unsigned int segment_size;
502 unsigned int len;
503
504 /* Split into multiple equally sized segments if necessary */
505 num_segments = DIV_ROUND_UP(period_len, chan->max_length);
506 segment_size = DIV_ROUND_UP(period_len, num_segments);
507 /* Take care of alignment */
a5b20600 508 segment_size = ((segment_size - 1) | chan->length_align_mask) + 1;
921234e0
LPC
509
510 for (i = 0; i < num_periods; i++) {
a2ab7045 511 for (len = period_len; len > segment_size; sg++) {
921234e0
LPC
512 if (direction == DMA_DEV_TO_MEM)
513 sg->dest_addr = addr;
514 else
515 sg->src_addr = addr;
516 sg->x_len = segment_size;
517 sg->y_len = 1;
921234e0
LPC
518 addr += segment_size;
519 len -= segment_size;
520 }
521
522 if (direction == DMA_DEV_TO_MEM)
523 sg->dest_addr = addr;
524 else
525 sg->src_addr = addr;
526 sg->x_len = len;
527 sg->y_len = 1;
528 sg++;
529 addr += len;
530 }
531
532 return sg;
533}
534
0e3b67b3
LPC
535static struct dma_async_tx_descriptor *axi_dmac_prep_slave_sg(
536 struct dma_chan *c, struct scatterlist *sgl,
537 unsigned int sg_len, enum dma_transfer_direction direction,
538 unsigned long flags, void *context)
539{
540 struct axi_dmac_chan *chan = to_axi_dmac_chan(c);
541 struct axi_dmac_desc *desc;
921234e0 542 struct axi_dmac_sg *dsg;
0e3b67b3 543 struct scatterlist *sg;
921234e0 544 unsigned int num_sgs;
0e3b67b3
LPC
545 unsigned int i;
546
547 if (direction != chan->direction)
548 return NULL;
549
921234e0
LPC
550 num_sgs = 0;
551 for_each_sg(sgl, sg, sg_len, i)
552 num_sgs += DIV_ROUND_UP(sg_dma_len(sg), chan->max_length);
553
554 desc = axi_dmac_alloc_desc(num_sgs);
0e3b67b3
LPC
555 if (!desc)
556 return NULL;
557
921234e0
LPC
558 dsg = desc->sg;
559
0e3b67b3
LPC
560 for_each_sg(sgl, sg, sg_len, i) {
561 if (!axi_dmac_check_addr(chan, sg_dma_address(sg)) ||
562 !axi_dmac_check_len(chan, sg_dma_len(sg))) {
563 kfree(desc);
564 return NULL;
565 }
566
921234e0
LPC
567 dsg = axi_dmac_fill_linear_sg(chan, direction, sg_dma_address(sg), 1,
568 sg_dma_len(sg), dsg);
0e3b67b3
LPC
569 }
570
571 desc->cyclic = false;
572
573 return vchan_tx_prep(&chan->vchan, &desc->vdesc, flags);
574}
575
576static struct dma_async_tx_descriptor *axi_dmac_prep_dma_cyclic(
577 struct dma_chan *c, dma_addr_t buf_addr, size_t buf_len,
578 size_t period_len, enum dma_transfer_direction direction,
579 unsigned long flags)
580{
581 struct axi_dmac_chan *chan = to_axi_dmac_chan(c);
582 struct axi_dmac_desc *desc;
921234e0 583 unsigned int num_periods, num_segments;
0e3b67b3
LPC
584
585 if (direction != chan->direction)
586 return NULL;
587
588 if (!axi_dmac_check_len(chan, buf_len) ||
589 !axi_dmac_check_addr(chan, buf_addr))
590 return NULL;
591
592 if (period_len == 0 || buf_len % period_len)
593 return NULL;
594
595 num_periods = buf_len / period_len;
921234e0 596 num_segments = DIV_ROUND_UP(period_len, chan->max_length);
0e3b67b3 597
921234e0 598 desc = axi_dmac_alloc_desc(num_periods * num_segments);
0e3b67b3
LPC
599 if (!desc)
600 return NULL;
601
921234e0
LPC
602 axi_dmac_fill_linear_sg(chan, direction, buf_addr, num_periods,
603 period_len, desc->sg);
0e3b67b3
LPC
604
605 desc->cyclic = true;
606
607 return vchan_tx_prep(&chan->vchan, &desc->vdesc, flags);
608}
609
610static struct dma_async_tx_descriptor *axi_dmac_prep_interleaved(
611 struct dma_chan *c, struct dma_interleaved_template *xt,
612 unsigned long flags)
613{
614 struct axi_dmac_chan *chan = to_axi_dmac_chan(c);
615 struct axi_dmac_desc *desc;
616 size_t dst_icg, src_icg;
617
618 if (xt->frame_size != 1)
619 return NULL;
620
621 if (xt->dir != chan->direction)
622 return NULL;
623
624 if (axi_dmac_src_is_mem(chan)) {
625 if (!xt->src_inc || !axi_dmac_check_addr(chan, xt->src_start))
626 return NULL;
627 }
628
629 if (axi_dmac_dest_is_mem(chan)) {
630 if (!xt->dst_inc || !axi_dmac_check_addr(chan, xt->dst_start))
631 return NULL;
632 }
633
634 dst_icg = dmaengine_get_dst_icg(xt, &xt->sgl[0]);
635 src_icg = dmaengine_get_src_icg(xt, &xt->sgl[0]);
636
637 if (chan->hw_2d) {
638 if (!axi_dmac_check_len(chan, xt->sgl[0].size) ||
648865a7 639 xt->numf == 0)
0e3b67b3
LPC
640 return NULL;
641 if (xt->sgl[0].size + dst_icg > chan->max_length ||
642 xt->sgl[0].size + src_icg > chan->max_length)
643 return NULL;
644 } else {
645 if (dst_icg != 0 || src_icg != 0)
646 return NULL;
647 if (chan->max_length / xt->sgl[0].size < xt->numf)
648 return NULL;
649 if (!axi_dmac_check_len(chan, xt->sgl[0].size * xt->numf))
650 return NULL;
651 }
652
653 desc = axi_dmac_alloc_desc(1);
654 if (!desc)
655 return NULL;
656
657 if (axi_dmac_src_is_mem(chan)) {
658 desc->sg[0].src_addr = xt->src_start;
659 desc->sg[0].src_stride = xt->sgl[0].size + src_icg;
660 }
661
662 if (axi_dmac_dest_is_mem(chan)) {
663 desc->sg[0].dest_addr = xt->dst_start;
664 desc->sg[0].dest_stride = xt->sgl[0].size + dst_icg;
665 }
666
667 if (chan->hw_2d) {
668 desc->sg[0].x_len = xt->sgl[0].size;
669 desc->sg[0].y_len = xt->numf;
670 } else {
671 desc->sg[0].x_len = xt->sgl[0].size * xt->numf;
672 desc->sg[0].y_len = 1;
673 }
674
8add6cce
DB
675 if (flags & DMA_CYCLIC)
676 desc->cyclic = true;
677
0e3b67b3
LPC
678 return vchan_tx_prep(&chan->vchan, &desc->vdesc, flags);
679}
680
681static void axi_dmac_free_chan_resources(struct dma_chan *c)
682{
683 vchan_free_chan_resources(to_virt_chan(c));
684}
685
686static void axi_dmac_desc_free(struct virt_dma_desc *vdesc)
687{
688 kfree(container_of(vdesc, struct axi_dmac_desc, vdesc));
689}
690
fc15be39
AA
691static bool axi_dmac_regmap_rdwr(struct device *dev, unsigned int reg)
692{
693 switch (reg) {
694 case AXI_DMAC_REG_IRQ_MASK:
695 case AXI_DMAC_REG_IRQ_SOURCE:
696 case AXI_DMAC_REG_IRQ_PENDING:
697 case AXI_DMAC_REG_CTRL:
698 case AXI_DMAC_REG_TRANSFER_ID:
699 case AXI_DMAC_REG_START_TRANSFER:
700 case AXI_DMAC_REG_FLAGS:
701 case AXI_DMAC_REG_DEST_ADDRESS:
702 case AXI_DMAC_REG_SRC_ADDRESS:
703 case AXI_DMAC_REG_X_LENGTH:
704 case AXI_DMAC_REG_Y_LENGTH:
705 case AXI_DMAC_REG_DEST_STRIDE:
706 case AXI_DMAC_REG_SRC_STRIDE:
707 case AXI_DMAC_REG_TRANSFER_DONE:
708 case AXI_DMAC_REG_ACTIVE_TRANSFER_ID:
709 case AXI_DMAC_REG_STATUS:
710 case AXI_DMAC_REG_CURRENT_SRC_ADDR:
711 case AXI_DMAC_REG_CURRENT_DEST_ADDR:
712 case AXI_DMAC_REG_PARTIAL_XFER_LEN:
713 case AXI_DMAC_REG_PARTIAL_XFER_ID:
714 return true;
715 default:
716 return false;
717 }
718}
719
720static const struct regmap_config axi_dmac_regmap_config = {
721 .reg_bits = 32,
722 .val_bits = 32,
723 .reg_stride = 4,
724 .max_register = AXI_DMAC_REG_PARTIAL_XFER_ID,
725 .readable_reg = axi_dmac_regmap_rdwr,
726 .writeable_reg = axi_dmac_regmap_rdwr,
727};
728
3061a65c
AA
729static void axi_dmac_adjust_chan_params(struct axi_dmac_chan *chan)
730{
731 chan->address_align_mask = max(chan->dest_width, chan->src_width) - 1;
732
733 if (axi_dmac_dest_is_mem(chan) && axi_dmac_src_is_mem(chan))
734 chan->direction = DMA_MEM_TO_MEM;
735 else if (!axi_dmac_dest_is_mem(chan) && axi_dmac_src_is_mem(chan))
736 chan->direction = DMA_MEM_TO_DEV;
737 else if (axi_dmac_dest_is_mem(chan) && !axi_dmac_src_is_mem(chan))
738 chan->direction = DMA_DEV_TO_MEM;
739 else
740 chan->direction = DMA_DEV_TO_DEV;
741}
742
0e3b67b3
LPC
743/*
744 * The configuration stored in the devicetree matches the configuration
745 * parameters of the peripheral instance and allows the driver to know which
746 * features are implemented and how it should behave.
747 */
748static int axi_dmac_parse_chan_dt(struct device_node *of_chan,
749 struct axi_dmac_chan *chan)
750{
751 u32 val;
752 int ret;
753
754 ret = of_property_read_u32(of_chan, "reg", &val);
755 if (ret)
756 return ret;
757
758 /* We only support 1 channel for now */
759 if (val != 0)
760 return -EINVAL;
761
762 ret = of_property_read_u32(of_chan, "adi,source-bus-type", &val);
763 if (ret)
764 return ret;
765 if (val > AXI_DMAC_BUS_TYPE_FIFO)
766 return -EINVAL;
767 chan->src_type = val;
768
769 ret = of_property_read_u32(of_chan, "adi,destination-bus-type", &val);
770 if (ret)
771 return ret;
772 if (val > AXI_DMAC_BUS_TYPE_FIFO)
773 return -EINVAL;
774 chan->dest_type = val;
775
776 ret = of_property_read_u32(of_chan, "adi,source-bus-width", &val);
777 if (ret)
778 return ret;
779 chan->src_width = val / 8;
780
781 ret = of_property_read_u32(of_chan, "adi,destination-bus-width", &val);
782 if (ret)
783 return ret;
784 chan->dest_width = val / 8;
785
3061a65c 786 axi_dmac_adjust_chan_params(chan);
0e3b67b3 787
0e3b67b3
LPC
788 return 0;
789}
790
06b6e88c
AA
791static int axi_dmac_parse_dt(struct device *dev, struct axi_dmac *dmac)
792{
793 struct device_node *of_channels, *of_chan;
794 int ret;
795
796 of_channels = of_get_child_by_name(dev->of_node, "adi,channels");
797 if (of_channels == NULL)
798 return -ENODEV;
799
800 for_each_child_of_node(of_channels, of_chan) {
801 ret = axi_dmac_parse_chan_dt(of_chan, &dmac->chan);
802 if (ret) {
803 of_node_put(of_chan);
804 of_node_put(of_channels);
805 return -EINVAL;
806 }
807 }
808 of_node_put(of_channels);
809
810 return 0;
811}
812
78a2f92e
AA
813static int axi_dmac_read_chan_config(struct device *dev, struct axi_dmac *dmac)
814{
815 struct axi_dmac_chan *chan = &dmac->chan;
816 unsigned int val, desc;
817
818 desc = axi_dmac_read(dmac, AXI_DMAC_REG_INTERFACE_DESC);
819 if (desc == 0) {
820 dev_err(dev, "DMA interface register reads zero\n");
821 return -EFAULT;
822 }
823
824 val = AXI_DMAC_DMA_SRC_TYPE_GET(desc);
825 if (val > AXI_DMAC_BUS_TYPE_FIFO) {
826 dev_err(dev, "Invalid source bus type read: %d\n", val);
827 return -EINVAL;
828 }
829 chan->src_type = val;
830
831 val = AXI_DMAC_DMA_DST_TYPE_GET(desc);
832 if (val > AXI_DMAC_BUS_TYPE_FIFO) {
833 dev_err(dev, "Invalid destination bus type read: %d\n", val);
834 return -EINVAL;
835 }
836 chan->dest_type = val;
837
838 val = AXI_DMAC_DMA_SRC_WIDTH_GET(desc);
839 if (val == 0) {
840 dev_err(dev, "Source bus width is zero\n");
841 return -EINVAL;
842 }
843 /* widths are stored in log2 */
844 chan->src_width = 1 << val;
845
846 val = AXI_DMAC_DMA_DST_WIDTH_GET(desc);
847 if (val == 0) {
848 dev_err(dev, "Destination bus width is zero\n");
849 return -EINVAL;
850 }
851 chan->dest_width = 1 << val;
852
853 axi_dmac_adjust_chan_params(chan);
854
855 return 0;
856}
857
b377e670 858static int axi_dmac_detect_caps(struct axi_dmac *dmac, unsigned int version)
56009f0d
LPC
859{
860 struct axi_dmac_chan *chan = &dmac->chan;
861
862 axi_dmac_write(dmac, AXI_DMAC_REG_FLAGS, AXI_DMAC_FLAG_CYCLIC);
863 if (axi_dmac_read(dmac, AXI_DMAC_REG_FLAGS) == AXI_DMAC_FLAG_CYCLIC)
864 chan->hw_cyclic = true;
865
866 axi_dmac_write(dmac, AXI_DMAC_REG_Y_LENGTH, 1);
867 if (axi_dmac_read(dmac, AXI_DMAC_REG_Y_LENGTH) == 1)
868 chan->hw_2d = true;
869
870 axi_dmac_write(dmac, AXI_DMAC_REG_X_LENGTH, 0xffffffff);
871 chan->max_length = axi_dmac_read(dmac, AXI_DMAC_REG_X_LENGTH);
872 if (chan->max_length != UINT_MAX)
873 chan->max_length++;
b5d89905
LPC
874
875 axi_dmac_write(dmac, AXI_DMAC_REG_DEST_ADDRESS, 0xffffffff);
876 if (axi_dmac_read(dmac, AXI_DMAC_REG_DEST_ADDRESS) == 0 &&
877 chan->dest_type == AXI_DMAC_BUS_TYPE_AXI_MM) {
878 dev_err(dmac->dma_dev.dev,
879 "Destination memory-mapped interface not supported.");
880 return -ENODEV;
881 }
882
883 axi_dmac_write(dmac, AXI_DMAC_REG_SRC_ADDRESS, 0xffffffff);
884 if (axi_dmac_read(dmac, AXI_DMAC_REG_SRC_ADDRESS) == 0 &&
885 chan->src_type == AXI_DMAC_BUS_TYPE_AXI_MM) {
886 dev_err(dmac->dma_dev.dev,
887 "Source memory-mapped interface not supported.");
888 return -ENODEV;
889 }
890
e3923592
AA
891 if (version >= ADI_AXI_PCORE_VER(4, 2, 'a'))
892 chan->hw_partial_xfer = true;
893
a5b20600
LPC
894 if (version >= ADI_AXI_PCORE_VER(4, 1, 'a')) {
895 axi_dmac_write(dmac, AXI_DMAC_REG_X_LENGTH, 0x00);
896 chan->length_align_mask =
897 axi_dmac_read(dmac, AXI_DMAC_REG_X_LENGTH);
898 } else {
899 chan->length_align_mask = chan->address_align_mask;
900 }
901
b5d89905 902 return 0;
56009f0d
LPC
903}
904
0e3b67b3
LPC
905static int axi_dmac_probe(struct platform_device *pdev)
906{
0e3b67b3
LPC
907 struct dma_device *dma_dev;
908 struct axi_dmac *dmac;
a5b982af 909 struct regmap *regmap;
b377e670 910 unsigned int version;
0e3b67b3
LPC
911 int ret;
912
913 dmac = devm_kzalloc(&pdev->dev, sizeof(*dmac), GFP_KERNEL);
914 if (!dmac)
915 return -ENOMEM;
916
917 dmac->irq = platform_get_irq(pdev, 0);
50dc60a2
LPC
918 if (dmac->irq < 0)
919 return dmac->irq;
920 if (dmac->irq == 0)
0e3b67b3
LPC
921 return -EINVAL;
922
4b23603a 923 dmac->base = devm_platform_ioremap_resource(pdev, 0);
0e3b67b3
LPC
924 if (IS_ERR(dmac->base))
925 return PTR_ERR(dmac->base);
926
927 dmac->clk = devm_clk_get(&pdev->dev, NULL);
928 if (IS_ERR(dmac->clk))
929 return PTR_ERR(dmac->clk);
930
08b36dba
AA
931 ret = clk_prepare_enable(dmac->clk);
932 if (ret < 0)
933 return ret;
934
78a2f92e
AA
935 version = axi_dmac_read(dmac, ADI_AXI_REG_VERSION);
936
937 if (version >= ADI_AXI_PCORE_VER(4, 3, 'a'))
938 ret = axi_dmac_read_chan_config(&pdev->dev, dmac);
939 else
940 ret = axi_dmac_parse_dt(&pdev->dev, dmac);
941
06b6e88c 942 if (ret < 0)
08b36dba 943 goto err_clk_disable;
0e3b67b3 944
a88fdece
AA
945 INIT_LIST_HEAD(&dmac->chan.active_descs);
946
921234e0 947 dma_set_max_seg_size(&pdev->dev, UINT_MAX);
0e3b67b3
LPC
948
949 dma_dev = &dmac->dma_dev;
950 dma_cap_set(DMA_SLAVE, dma_dev->cap_mask);
951 dma_cap_set(DMA_CYCLIC, dma_dev->cap_mask);
9a05045d 952 dma_cap_set(DMA_INTERLEAVE, dma_dev->cap_mask);
0e3b67b3
LPC
953 dma_dev->device_free_chan_resources = axi_dmac_free_chan_resources;
954 dma_dev->device_tx_status = dma_cookie_status;
955 dma_dev->device_issue_pending = axi_dmac_issue_pending;
956 dma_dev->device_prep_slave_sg = axi_dmac_prep_slave_sg;
957 dma_dev->device_prep_dma_cyclic = axi_dmac_prep_dma_cyclic;
958 dma_dev->device_prep_interleaved_dma = axi_dmac_prep_interleaved;
959 dma_dev->device_terminate_all = axi_dmac_terminate_all;
860dd64c 960 dma_dev->device_synchronize = axi_dmac_synchronize;
0e3b67b3 961 dma_dev->dev = &pdev->dev;
0e3b67b3
LPC
962 dma_dev->src_addr_widths = BIT(dmac->chan.src_width);
963 dma_dev->dst_addr_widths = BIT(dmac->chan.dest_width);
964 dma_dev->directions = BIT(dmac->chan.direction);
965 dma_dev->residue_granularity = DMA_RESIDUE_GRANULARITY_DESCRIPTOR;
966 INIT_LIST_HEAD(&dma_dev->channels);
967
968 dmac->chan.vchan.desc_free = axi_dmac_desc_free;
969 vchan_init(&dmac->chan.vchan, dma_dev);
970
b377e670 971 ret = axi_dmac_detect_caps(dmac, version);
b5d89905
LPC
972 if (ret)
973 goto err_clk_disable;
56009f0d 974
5b969bd1 975 dma_dev->copy_align = (dmac->chan.address_align_mask + 1);
56009f0d 976
0e3b67b3
LPC
977 axi_dmac_write(dmac, AXI_DMAC_REG_IRQ_MASK, 0x00);
978
9327c7e7
MT
979 if (of_dma_is_coherent(pdev->dev.of_node)) {
980 ret = axi_dmac_read(dmac, AXI_DMAC_REG_COHERENCY_DESC);
981
982 if (version < ADI_AXI_PCORE_VER(4, 4, 'a') ||
983 !AXI_DMAC_DST_COHERENT_GET(ret)) {
984 dev_err(dmac->dma_dev.dev,
985 "Coherent DMA not supported in hardware");
986 ret = -EINVAL;
987 goto err_clk_disable;
988 }
989 }
990
0e3b67b3
LPC
991 ret = dma_async_device_register(dma_dev);
992 if (ret)
993 goto err_clk_disable;
994
995 ret = of_dma_controller_register(pdev->dev.of_node,
996 of_dma_xlate_by_chan_id, dma_dev);
997 if (ret)
998 goto err_unregister_device;
999
9c87572e 1000 ret = request_irq(dmac->irq, axi_dmac_interrupt_handler, IRQF_SHARED,
0e3b67b3
LPC
1001 dev_name(&pdev->dev), dmac);
1002 if (ret)
1003 goto err_unregister_of;
1004
1005 platform_set_drvdata(pdev, dmac);
1006
a5b982af
CY
1007 regmap = devm_regmap_init_mmio(&pdev->dev, dmac->base,
1008 &axi_dmac_regmap_config);
1009 if (IS_ERR(regmap)) {
1010 ret = PTR_ERR(regmap);
1011 goto err_free_irq;
1012 }
fc15be39 1013
0e3b67b3
LPC
1014 return 0;
1015
a5b982af
CY
1016err_free_irq:
1017 free_irq(dmac->irq, dmac);
0e3b67b3
LPC
1018err_unregister_of:
1019 of_dma_controller_free(pdev->dev.of_node);
1020err_unregister_device:
1021 dma_async_device_unregister(&dmac->dma_dev);
1022err_clk_disable:
1023 clk_disable_unprepare(dmac->clk);
1024
1025 return ret;
1026}
1027
b5f095a7 1028static void axi_dmac_remove(struct platform_device *pdev)
0e3b67b3
LPC
1029{
1030 struct axi_dmac *dmac = platform_get_drvdata(pdev);
1031
1032 of_dma_controller_free(pdev->dev.of_node);
1033 free_irq(dmac->irq, dmac);
1034 tasklet_kill(&dmac->chan.vchan.task);
1035 dma_async_device_unregister(&dmac->dma_dev);
1036 clk_disable_unprepare(dmac->clk);
0e3b67b3
LPC
1037}
1038
1039static const struct of_device_id axi_dmac_of_match_table[] = {
1040 { .compatible = "adi,axi-dmac-1.00.a" },
1041 { },
1042};
9bcfe38f 1043MODULE_DEVICE_TABLE(of, axi_dmac_of_match_table);
0e3b67b3
LPC
1044
1045static struct platform_driver axi_dmac_driver = {
1046 .driver = {
1047 .name = "dma-axi-dmac",
1048 .of_match_table = axi_dmac_of_match_table,
1049 },
1050 .probe = axi_dmac_probe,
b5f095a7 1051 .remove_new = axi_dmac_remove,
0e3b67b3
LPC
1052};
1053module_platform_driver(axi_dmac_driver);
1054
1055MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
1056MODULE_DESCRIPTION("DMA controller driver for the AXI-DMAC controller");
1057MODULE_LICENSE("GPL v2");