aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorStefan Roese <sr@denx.de>2017-07-07 08:11:19 -0400
committerVinod Koul <vinod.koul@intel.com>2017-07-18 12:34:54 -0400
commita85c6f1b2921cbd2f54666a52804f407c4a064fe (patch)
tree8efaf3c6a14d80c596a75c2b7bd6d98bd8746a6f
parent5771a8c08880cdca3bfb4a3fc6d309d6bba20877 (diff)
dmaengine: Add driver for Altera / Intel mSGDMA IP core
This driver adds support for the Altera / Intel modular Scatter-Gather Direct Memory Access (mSGDMA) intellectual property (IP) to the Linux DMAengine subsystem. Currently it supports the following op modes: - DMA_MEMCPY - DMA_SG - DMA_SLAVE This implementation has been tested on an Altera Cyclone FPGA connected via PCIe, both on an ARM and an x86 platform. Signed-off-by: Stefan Roese <sr@denx.de> Cc: Vinod Koul <vinod.koul@intel.com> Signed-off-by: Vinod Koul <vinod.koul@intel.com>
-rw-r--r--drivers/dma/Kconfig6
-rw-r--r--drivers/dma/Makefile1
-rw-r--r--drivers/dma/altera-msgdma.c1031
3 files changed, 1038 insertions, 0 deletions
diff --git a/drivers/dma/Kconfig b/drivers/dma/Kconfig
index fa8f9c07ce73..fadc4d8783bd 100644
--- a/drivers/dma/Kconfig
+++ b/drivers/dma/Kconfig
@@ -56,6 +56,12 @@ config DMA_OF
56 select DMA_ENGINE 56 select DMA_ENGINE
57 57
58#devices 58#devices
59config ALTERA_MSGDMA
60 tristate "Altera / Intel mSGDMA Engine"
61 select DMA_ENGINE
62 help
63 Enable support for Altera / Intel mSGDMA controller.
64
59config AMBA_PL08X 65config AMBA_PL08X
60 bool "ARM PrimeCell PL080 or PL081 support" 66 bool "ARM PrimeCell PL080 or PL081 support"
61 depends on ARM_AMBA 67 depends on ARM_AMBA
diff --git a/drivers/dma/Makefile b/drivers/dma/Makefile
index d12ab2985ed1..f08f8de1b567 100644
--- a/drivers/dma/Makefile
+++ b/drivers/dma/Makefile
@@ -12,6 +12,7 @@ obj-$(CONFIG_DMA_OF) += of-dma.o
12obj-$(CONFIG_DMATEST) += dmatest.o 12obj-$(CONFIG_DMATEST) += dmatest.o
13 13
14#devices 14#devices
15obj-$(CONFIG_ALTERA_MSGDMA) += altera-msgdma.o
15obj-$(CONFIG_AMBA_PL08X) += amba-pl08x.o 16obj-$(CONFIG_AMBA_PL08X) += amba-pl08x.o
16obj-$(CONFIG_AMCC_PPC440SPE_ADMA) += ppc4xx/ 17obj-$(CONFIG_AMCC_PPC440SPE_ADMA) += ppc4xx/
17obj-$(CONFIG_AT_HDMAC) += at_hdmac.o 18obj-$(CONFIG_AT_HDMAC) += at_hdmac.o
diff --git a/drivers/dma/altera-msgdma.c b/drivers/dma/altera-msgdma.c
new file mode 100644
index 000000000000..33b87b413793
--- /dev/null
+++ b/drivers/dma/altera-msgdma.c
@@ -0,0 +1,1031 @@
1/*
2 * DMA driver for Altera mSGDMA IP core
3 *
4 * Copyright (C) 2017 Stefan Roese <sr@denx.de>
5 *
6 * Based on drivers/dma/xilinx/zynqmp_dma.c, which is:
7 * Copyright (C) 2016 Xilinx, Inc. All rights reserved.
8 *
9 * This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation, either version 2 of the License, or
12 * (at your option) any later version.
13 */
14
15#include <linux/bitops.h>
16#include <linux/delay.h>
17#include <linux/dma-mapping.h>
18#include <linux/dmapool.h>
19#include <linux/init.h>
20#include <linux/interrupt.h>
21#include <linux/io.h>
22#include <linux/iopoll.h>
23#include <linux/module.h>
24#include <linux/platform_device.h>
25#include <linux/slab.h>
26
27#include "dmaengine.h"
28
29#define MSGDMA_MAX_TRANS_LEN U32_MAX
30#define MSGDMA_DESC_NUM 1024
31
32/**
33 * struct msgdma_extended_desc - implements an extended descriptor
34 * @read_addr_lo: data buffer source address low bits
35 * @write_addr_lo: data buffer destination address low bits
36 * @len: the number of bytes to transfer per descriptor
37 * @burst_seq_num: bit 31:24 write burst
38 * bit 23:16 read burst
39 * bit 15:00 sequence number
40 * @stride: bit 31:16 write stride
41 * bit 15:00 read stride
42 * @read_addr_hi: data buffer source address high bits
43 * @write_addr_hi: data buffer destination address high bits
44 * @control: characteristics of the transfer
45 */
46struct msgdma_extended_desc {
47 u32 read_addr_lo;
48 u32 write_addr_lo;
49 u32 len;
50 u32 burst_seq_num;
51 u32 stride;
52 u32 read_addr_hi;
53 u32 write_addr_hi;
54 u32 control;
55};
56
57/* mSGDMA descriptor control field bit definitions */
58#define MSGDMA_DESC_CTL_SET_CH(x) ((x) & 0xff)
59#define MSGDMA_DESC_CTL_GEN_SOP BIT(8)
60#define MSGDMA_DESC_CTL_GEN_EOP BIT(9)
61#define MSGDMA_DESC_CTL_PARK_READS BIT(10)
62#define MSGDMA_DESC_CTL_PARK_WRITES BIT(11)
63#define MSGDMA_DESC_CTL_END_ON_EOP BIT(12)
64#define MSGDMA_DESC_CTL_END_ON_LEN BIT(13)
65#define MSGDMA_DESC_CTL_TR_COMP_IRQ BIT(14)
66#define MSGDMA_DESC_CTL_EARLY_IRQ BIT(15)
67#define MSGDMA_DESC_CTL_TR_ERR_IRQ GENMASK(23, 16)
68#define MSGDMA_DESC_CTL_EARLY_DONE BIT(24)
69
70/*
71 * Writing "1" the "go" bit commits the entire descriptor into the
72 * descriptor FIFO(s)
73 */
74#define MSGDMA_DESC_CTL_GO BIT(31)
75
76/* Tx buffer control flags */
77#define MSGDMA_DESC_CTL_TX_FIRST (MSGDMA_DESC_CTL_GEN_SOP | \
78 MSGDMA_DESC_CTL_TR_ERR_IRQ | \
79 MSGDMA_DESC_CTL_GO)
80
81#define MSGDMA_DESC_CTL_TX_MIDDLE (MSGDMA_DESC_CTL_TR_ERR_IRQ | \
82 MSGDMA_DESC_CTL_GO)
83
84#define MSGDMA_DESC_CTL_TX_LAST (MSGDMA_DESC_CTL_GEN_EOP | \
85 MSGDMA_DESC_CTL_TR_COMP_IRQ | \
86 MSGDMA_DESC_CTL_TR_ERR_IRQ | \
87 MSGDMA_DESC_CTL_GO)
88
89#define MSGDMA_DESC_CTL_TX_SINGLE (MSGDMA_DESC_CTL_GEN_SOP | \
90 MSGDMA_DESC_CTL_GEN_EOP | \
91 MSGDMA_DESC_CTL_TR_COMP_IRQ | \
92 MSGDMA_DESC_CTL_TR_ERR_IRQ | \
93 MSGDMA_DESC_CTL_GO)
94
95#define MSGDMA_DESC_CTL_RX_SINGLE (MSGDMA_DESC_CTL_END_ON_EOP | \
96 MSGDMA_DESC_CTL_END_ON_LEN | \
97 MSGDMA_DESC_CTL_TR_COMP_IRQ | \
98 MSGDMA_DESC_CTL_EARLY_IRQ | \
99 MSGDMA_DESC_CTL_TR_ERR_IRQ | \
100 MSGDMA_DESC_CTL_GO)
101
102/* mSGDMA extended descriptor stride definitions */
103#define MSGDMA_DESC_STRIDE_RD 0x00000001
104#define MSGDMA_DESC_STRIDE_WR 0x00010000
105#define MSGDMA_DESC_STRIDE_RW 0x00010001
106
107/**
108 * struct msgdma_csr - mSGDMA dispatcher control and status register map
109 * @status: Read/Clear
110 * @control: Read/Write
111 * @rw_fill_level: bit 31:16 - write fill level
112 * bit 15:00 - read fill level
113 * @resp_fill_level: bit 15:00 - response FIFO fill level
114 * @rw_seq_num: bit 31:16 - write sequence number
115 * bit 15:00 - read sequence number
116 * @pad: reserved
117 */
118struct msgdma_csr {
119 u32 status;
120 u32 control;
121 u32 rw_fill_level;
122 u32 resp_fill_level;
123 u32 rw_seq_num;
124 u32 pad[3];
125};
126
127/* mSGDMA CSR status register bit definitions */
128#define MSGDMA_CSR_STAT_BUSY BIT(0)
129#define MSGDMA_CSR_STAT_DESC_BUF_EMPTY BIT(1)
130#define MSGDMA_CSR_STAT_DESC_BUF_FULL BIT(2)
131#define MSGDMA_CSR_STAT_RESP_BUF_EMPTY BIT(3)
132#define MSGDMA_CSR_STAT_RESP_BUF_FULL BIT(4)
133#define MSGDMA_CSR_STAT_STOPPED BIT(5)
134#define MSGDMA_CSR_STAT_RESETTING BIT(6)
135#define MSGDMA_CSR_STAT_STOPPED_ON_ERR BIT(7)
136#define MSGDMA_CSR_STAT_STOPPED_ON_EARLY BIT(8)
137#define MSGDMA_CSR_STAT_IRQ BIT(9)
138#define MSGDMA_CSR_STAT_MASK GENMASK(9, 0)
139#define MSGDMA_CSR_STAT_MASK_WITHOUT_IRQ GENMASK(8, 0)
140
141#define DESC_EMPTY (MSGDMA_CSR_STAT_DESC_BUF_EMPTY | \
142 MSGDMA_CSR_STAT_RESP_BUF_EMPTY)
143
144/* mSGDMA CSR control register bit definitions */
145#define MSGDMA_CSR_CTL_STOP BIT(0)
146#define MSGDMA_CSR_CTL_RESET BIT(1)
147#define MSGDMA_CSR_CTL_STOP_ON_ERR BIT(2)
148#define MSGDMA_CSR_CTL_STOP_ON_EARLY BIT(3)
149#define MSGDMA_CSR_CTL_GLOBAL_INTR BIT(4)
150#define MSGDMA_CSR_CTL_STOP_DESCS BIT(5)
151
152/* mSGDMA CSR fill level bits */
153#define MSGDMA_CSR_WR_FILL_LEVEL_GET(v) (((v) & 0xffff0000) >> 16)
154#define MSGDMA_CSR_RD_FILL_LEVEL_GET(v) ((v) & 0x0000ffff)
155#define MSGDMA_CSR_RESP_FILL_LEVEL_GET(v) ((v) & 0x0000ffff)
156
157#define MSGDMA_CSR_SEQ_NUM_GET(v) (((v) & 0xffff0000) >> 16)
158
159/* mSGDMA response register map */
160struct msgdma_response {
161 u32 bytes_transferred;
162 u32 status;
163};
164
165/* mSGDMA response register bit definitions */
166#define MSGDMA_RESP_EARLY_TERM BIT(8)
167#define MSGDMA_RESP_ERR_MASK 0xff
168
169/**
170 * struct msgdma_sw_desc - implements a sw descriptor
171 * @async_tx: support for the async_tx api
172 * @hw_desc: assosiated HW descriptor
173 * @free_list: node of the free SW descriprots list
174 */
175struct msgdma_sw_desc {
176 struct dma_async_tx_descriptor async_tx;
177 struct msgdma_extended_desc hw_desc;
178 struct list_head node;
179 struct list_head tx_list;
180};
181
182/**
183 * struct msgdma_device - DMA device structure
184 */
185struct msgdma_device {
186 spinlock_t lock;
187 struct device *dev;
188 struct tasklet_struct irq_tasklet;
189 struct list_head pending_list;
190 struct list_head free_list;
191 struct list_head active_list;
192 struct list_head done_list;
193 u32 desc_free_cnt;
194 bool idle;
195
196 struct dma_device dmadev;
197 struct dma_chan dmachan;
198 dma_addr_t hw_desq;
199 struct msgdma_sw_desc *sw_desq;
200 unsigned int npendings;
201
202 struct dma_slave_config slave_cfg;
203
204 int irq;
205
206 /* mSGDMA controller */
207 struct msgdma_csr *csr;
208
209 /* mSGDMA descriptors */
210 struct msgdma_extended_desc *desc;
211
212 /* mSGDMA response */
213 struct msgdma_response *resp;
214};
215
216#define to_mdev(chan) container_of(chan, struct msgdma_device, dmachan)
217#define tx_to_desc(tx) container_of(tx, struct msgdma_sw_desc, async_tx)
218
219/**
220 * msgdma_get_descriptor - Get the sw descriptor from the pool
221 * @mdev: Pointer to the Altera mSGDMA device structure
222 *
223 * Return: The sw descriptor
224 */
225static struct msgdma_sw_desc *msgdma_get_descriptor(struct msgdma_device *mdev)
226{
227 struct msgdma_sw_desc *desc;
228
229 spin_lock_bh(&mdev->lock);
230 desc = list_first_entry(&mdev->free_list, struct msgdma_sw_desc, node);
231 list_del(&desc->node);
232 spin_unlock_bh(&mdev->lock);
233
234 INIT_LIST_HEAD(&desc->tx_list);
235
236 return desc;
237}
238
239/**
240 * msgdma_free_descriptor - Issue pending transactions
241 * @mdev: Pointer to the Altera mSGDMA device structure
242 * @desc: Transaction descriptor pointer
243 */
244static void msgdma_free_descriptor(struct msgdma_device *mdev,
245 struct msgdma_sw_desc *desc)
246{
247 struct msgdma_sw_desc *child, *next;
248
249 mdev->desc_free_cnt++;
250 list_add_tail(&desc->node, &mdev->free_list);
251 list_for_each_entry_safe(child, next, &desc->tx_list, node) {
252 mdev->desc_free_cnt++;
253 list_move_tail(&child->node, &mdev->free_list);
254 }
255}
256
257/**
258 * msgdma_free_desc_list - Free descriptors list
259 * @mdev: Pointer to the Altera mSGDMA device structure
260 * @list: List to parse and delete the descriptor
261 */
262static void msgdma_free_desc_list(struct msgdma_device *mdev,
263 struct list_head *list)
264{
265 struct msgdma_sw_desc *desc, *next;
266
267 list_for_each_entry_safe(desc, next, list, node)
268 msgdma_free_descriptor(mdev, desc);
269}
270
271/**
272 * msgdma_desc_config - Configure the descriptor
273 * @desc: Hw descriptor pointer
274 * @dst: Destination buffer address
275 * @src: Source buffer address
276 * @len: Transfer length
277 */
278static void msgdma_desc_config(struct msgdma_extended_desc *desc,
279 dma_addr_t dst, dma_addr_t src, size_t len,
280 u32 stride)
281{
282 /* Set lower 32bits of src & dst addresses in the descriptor */
283 desc->read_addr_lo = lower_32_bits(src);
284 desc->write_addr_lo = lower_32_bits(dst);
285
286 /* Set upper 32bits of src & dst addresses in the descriptor */
287 desc->read_addr_hi = upper_32_bits(src);
288 desc->write_addr_hi = upper_32_bits(dst);
289
290 desc->len = len;
291 desc->stride = stride;
292 desc->burst_seq_num = 0; /* 0 will result in max burst length */
293
294 /*
295 * Don't set interrupt on xfer end yet, this will be done later
296 * for the "last" descriptor
297 */
298 desc->control = MSGDMA_DESC_CTL_TR_ERR_IRQ | MSGDMA_DESC_CTL_GO |
299 MSGDMA_DESC_CTL_END_ON_LEN;
300}
301
302/**
303 * msgdma_desc_config_eod - Mark the descriptor as end descriptor
304 * @desc: Hw descriptor pointer
305 */
306static void msgdma_desc_config_eod(struct msgdma_extended_desc *desc)
307{
308 desc->control |= MSGDMA_DESC_CTL_TR_COMP_IRQ;
309}
310
311/**
312 * msgdma_tx_submit - Submit DMA transaction
313 * @tx: Async transaction descriptor pointer
314 *
315 * Return: cookie value
316 */
317static dma_cookie_t msgdma_tx_submit(struct dma_async_tx_descriptor *tx)
318{
319 struct msgdma_device *mdev = to_mdev(tx->chan);
320 struct msgdma_sw_desc *new;
321 dma_cookie_t cookie;
322
323 new = tx_to_desc(tx);
324 spin_lock_bh(&mdev->lock);
325 cookie = dma_cookie_assign(tx);
326
327 list_add_tail(&new->node, &mdev->pending_list);
328 spin_unlock_bh(&mdev->lock);
329
330 return cookie;
331}
332
333/**
334 * msgdma_prep_memcpy - prepare descriptors for memcpy transaction
335 * @dchan: DMA channel
336 * @dma_dst: Destination buffer address
337 * @dma_src: Source buffer address
338 * @len: Transfer length
339 * @flags: transfer ack flags
340 *
341 * Return: Async transaction descriptor on success and NULL on failure
342 */
343static struct dma_async_tx_descriptor *
344msgdma_prep_memcpy(struct dma_chan *dchan, dma_addr_t dma_dst,
345 dma_addr_t dma_src, size_t len, ulong flags)
346{
347 struct msgdma_device *mdev = to_mdev(dchan);
348 struct msgdma_sw_desc *new, *first = NULL;
349 struct msgdma_extended_desc *desc;
350 size_t copy;
351 u32 desc_cnt;
352
353 desc_cnt = DIV_ROUND_UP(len, MSGDMA_MAX_TRANS_LEN);
354
355 spin_lock_bh(&mdev->lock);
356 if (desc_cnt > mdev->desc_free_cnt) {
357 spin_unlock_bh(&mdev->lock);
358 dev_dbg(mdev->dev, "mdev %p descs are not available\n", mdev);
359 return NULL;
360 }
361 mdev->desc_free_cnt -= desc_cnt;
362 spin_unlock_bh(&mdev->lock);
363
364 do {
365 /* Allocate and populate the descriptor */
366 new = msgdma_get_descriptor(mdev);
367
368 copy = min_t(size_t, len, MSGDMA_MAX_TRANS_LEN);
369 desc = &new->hw_desc;
370 msgdma_desc_config(desc, dma_dst, dma_src, copy,
371 MSGDMA_DESC_STRIDE_RW);
372 len -= copy;
373 dma_src += copy;
374 dma_dst += copy;
375 if (!first)
376 first = new;
377 else
378 list_add_tail(&new->node, &first->tx_list);
379 } while (len);
380
381 msgdma_desc_config_eod(desc);
382 async_tx_ack(&first->async_tx);
383 first->async_tx.flags = flags;
384
385 return &first->async_tx;
386}
387
388/**
389 * msgdma_prep_sg - prepare descriptors for a memory sg transaction
390 * @dchan: DMA channel
391 * @dst_sg: Destination scatter list
392 * @dst_sg_len: Number of entries in destination scatter list
393 * @src_sg: Source scatter list
394 * @src_sg_len: Number of entries in source scatter list
395 * @flags: transfer ack flags
396 *
397 * Return: Async transaction descriptor on success and NULL on failure
398 */
399static struct dma_async_tx_descriptor *
400msgdma_prep_sg(struct dma_chan *dchan, struct scatterlist *dst_sg,
401 unsigned int dst_sg_len, struct scatterlist *src_sg,
402 unsigned int src_sg_len, unsigned long flags)
403{
404 struct msgdma_device *mdev = to_mdev(dchan);
405 struct msgdma_sw_desc *new, *first = NULL;
406 void *desc = NULL;
407 size_t len, dst_avail, src_avail;
408 dma_addr_t dma_dst, dma_src;
409 u32 desc_cnt = 0, i;
410 struct scatterlist *sg;
411
412 for_each_sg(src_sg, sg, src_sg_len, i)
413 desc_cnt += DIV_ROUND_UP(sg_dma_len(sg), MSGDMA_MAX_TRANS_LEN);
414
415 spin_lock_bh(&mdev->lock);
416 if (desc_cnt > mdev->desc_free_cnt) {
417 spin_unlock_bh(&mdev->lock);
418 dev_dbg(mdev->dev, "mdev %p descs are not available\n", mdev);
419 return NULL;
420 }
421 mdev->desc_free_cnt -= desc_cnt;
422 spin_unlock_bh(&mdev->lock);
423
424 dst_avail = sg_dma_len(dst_sg);
425 src_avail = sg_dma_len(src_sg);
426
427 /* Run until we are out of scatterlist entries */
428 while (true) {
429 /* Allocate and populate the descriptor */
430 new = msgdma_get_descriptor(mdev);
431
432 desc = &new->hw_desc;
433 len = min_t(size_t, src_avail, dst_avail);
434 len = min_t(size_t, len, MSGDMA_MAX_TRANS_LEN);
435 if (len == 0)
436 goto fetch;
437 dma_dst = sg_dma_address(dst_sg) + sg_dma_len(dst_sg) -
438 dst_avail;
439 dma_src = sg_dma_address(src_sg) + sg_dma_len(src_sg) -
440 src_avail;
441
442 msgdma_desc_config(desc, dma_dst, dma_src, len,
443 MSGDMA_DESC_STRIDE_RW);
444 dst_avail -= len;
445 src_avail -= len;
446
447 if (!first)
448 first = new;
449 else
450 list_add_tail(&new->node, &first->tx_list);
451fetch:
452 /* Fetch the next dst scatterlist entry */
453 if (dst_avail == 0) {
454 if (dst_sg_len == 0)
455 break;
456 dst_sg = sg_next(dst_sg);
457 if (dst_sg == NULL)
458 break;
459 dst_sg_len--;
460 dst_avail = sg_dma_len(dst_sg);
461 }
462 /* Fetch the next src scatterlist entry */
463 if (src_avail == 0) {
464 if (src_sg_len == 0)
465 break;
466 src_sg = sg_next(src_sg);
467 if (src_sg == NULL)
468 break;
469 src_sg_len--;
470 src_avail = sg_dma_len(src_sg);
471 }
472 }
473
474 msgdma_desc_config_eod(desc);
475 first->async_tx.flags = flags;
476
477 return &first->async_tx;
478}
479
480/**
481 * msgdma_prep_slave_sg - prepare descriptors for a slave sg transaction
482 *
483 * @dchan: DMA channel
484 * @sgl: Destination scatter list
485 * @sg_len: Number of entries in destination scatter list
486 * @dir: DMA transfer direction
487 * @flags: transfer ack flags
488 * @context: transfer context (unused)
489 */
490static struct dma_async_tx_descriptor *
491msgdma_prep_slave_sg(struct dma_chan *dchan, struct scatterlist *sgl,
492 unsigned int sg_len, enum dma_transfer_direction dir,
493 unsigned long flags, void *context)
494
495{
496 struct msgdma_device *mdev = to_mdev(dchan);
497 struct dma_slave_config *cfg = &mdev->slave_cfg;
498 struct msgdma_sw_desc *new, *first = NULL;
499 void *desc = NULL;
500 size_t len, avail;
501 dma_addr_t dma_dst, dma_src;
502 u32 desc_cnt = 0, i;
503 struct scatterlist *sg;
504 u32 stride;
505
506 for_each_sg(sgl, sg, sg_len, i)
507 desc_cnt += DIV_ROUND_UP(sg_dma_len(sg), MSGDMA_MAX_TRANS_LEN);
508
509 spin_lock_bh(&mdev->lock);
510 if (desc_cnt > mdev->desc_free_cnt) {
511 spin_unlock_bh(&mdev->lock);
512 dev_dbg(mdev->dev, "mdev %p descs are not available\n", mdev);
513 return NULL;
514 }
515 mdev->desc_free_cnt -= desc_cnt;
516 spin_unlock_bh(&mdev->lock);
517
518 avail = sg_dma_len(sgl);
519
520 /* Run until we are out of scatterlist entries */
521 while (true) {
522 /* Allocate and populate the descriptor */
523 new = msgdma_get_descriptor(mdev);
524
525 desc = &new->hw_desc;
526 len = min_t(size_t, avail, MSGDMA_MAX_TRANS_LEN);
527
528 if (dir == DMA_MEM_TO_DEV) {
529 dma_src = sg_dma_address(sgl) + sg_dma_len(sgl) - avail;
530 dma_dst = cfg->dst_addr;
531 stride = MSGDMA_DESC_STRIDE_RD;
532 } else {
533 dma_src = cfg->src_addr;
534 dma_dst = sg_dma_address(sgl) + sg_dma_len(sgl) - avail;
535 stride = MSGDMA_DESC_STRIDE_WR;
536 }
537 msgdma_desc_config(desc, dma_dst, dma_src, len, stride);
538 avail -= len;
539
540 if (!first)
541 first = new;
542 else
543 list_add_tail(&new->node, &first->tx_list);
544
545 /* Fetch the next scatterlist entry */
546 if (avail == 0) {
547 if (sg_len == 0)
548 break;
549 sgl = sg_next(sgl);
550 if (sgl == NULL)
551 break;
552 sg_len--;
553 avail = sg_dma_len(sgl);
554 }
555 }
556
557 msgdma_desc_config_eod(desc);
558 first->async_tx.flags = flags;
559
560 return &first->async_tx;
561}
562
563static int msgdma_dma_config(struct dma_chan *dchan,
564 struct dma_slave_config *config)
565{
566 struct msgdma_device *mdev = to_mdev(dchan);
567
568 memcpy(&mdev->slave_cfg, config, sizeof(*config));
569
570 return 0;
571}
572
573static void msgdma_reset(struct msgdma_device *mdev)
574{
575 u32 val;
576 int ret;
577
578 /* Reset mSGDMA */
579 iowrite32(MSGDMA_CSR_STAT_MASK, &mdev->csr->status);
580 iowrite32(MSGDMA_CSR_CTL_RESET, &mdev->csr->control);
581
582 ret = readl_poll_timeout(&mdev->csr->status, val,
583 (val & MSGDMA_CSR_STAT_RESETTING) == 0,
584 1, 10000);
585 if (ret)
586 dev_err(mdev->dev, "DMA channel did not reset\n");
587
588 /* Clear all status bits */
589 iowrite32(MSGDMA_CSR_STAT_MASK, &mdev->csr->status);
590
591 /* Enable the DMA controller including interrupts */
592 iowrite32(MSGDMA_CSR_CTL_STOP_ON_ERR | MSGDMA_CSR_CTL_STOP_ON_EARLY |
593 MSGDMA_CSR_CTL_GLOBAL_INTR, &mdev->csr->control);
594
595 mdev->idle = true;
596};
597
598static void msgdma_copy_one(struct msgdma_device *mdev,
599 struct msgdma_sw_desc *desc)
600{
601 struct msgdma_extended_desc *hw_desc = mdev->desc;
602
603 /*
604 * Check if the DESC FIFO it not full. If its full, we need to wait
605 * for at least one entry to become free again
606 */
607 while (ioread32(&mdev->csr->status) & MSGDMA_CSR_STAT_DESC_BUF_FULL)
608 mdelay(1);
609
610 /*
611 * The descriptor needs to get copied into the descriptor FIFO
612 * of the DMA controller. The descriptor will get flushed to the
613 * FIFO, once the last word (control word) is written. Since we
614 * are not 100% sure that memcpy() writes all word in the "correct"
615 * oder (address from low to high) on all architectures, we make
616 * sure this control word is written last by single coding it and
617 * adding some write-barriers here.
618 */
619 memcpy(hw_desc, &desc->hw_desc, sizeof(desc->hw_desc) - sizeof(u32));
620
621 /* Write control word last to flush this descriptor into the FIFO */
622 mdev->idle = false;
623 wmb();
624 iowrite32(desc->hw_desc.control, &hw_desc->control);
625 wmb();
626}
627
628/**
629 * msgdma_copy_desc_to_fifo - copy descriptor(s) into controller FIFO
630 * @mdev: Pointer to the Altera mSGDMA device structure
631 * @desc: Transaction descriptor pointer
632 */
633static void msgdma_copy_desc_to_fifo(struct msgdma_device *mdev,
634 struct msgdma_sw_desc *desc)
635{
636 struct msgdma_sw_desc *sdesc, *next;
637
638 msgdma_copy_one(mdev, desc);
639
640 list_for_each_entry_safe(sdesc, next, &desc->tx_list, node)
641 msgdma_copy_one(mdev, sdesc);
642}
643
644/**
645 * msgdma_start_transfer - Initiate the new transfer
646 * @mdev: Pointer to the Altera mSGDMA device structure
647 */
648static void msgdma_start_transfer(struct msgdma_device *mdev)
649{
650 struct msgdma_sw_desc *desc;
651
652 if (!mdev->idle)
653 return;
654
655 desc = list_first_entry_or_null(&mdev->pending_list,
656 struct msgdma_sw_desc, node);
657 if (!desc)
658 return;
659
660 list_splice_tail_init(&mdev->pending_list, &mdev->active_list);
661 msgdma_copy_desc_to_fifo(mdev, desc);
662}
663
664/**
665 * msgdma_issue_pending - Issue pending transactions
666 * @chan: DMA channel pointer
667 */
668static void msgdma_issue_pending(struct dma_chan *chan)
669{
670 struct msgdma_device *mdev = to_mdev(chan);
671
672 spin_lock_bh(&mdev->lock);
673 msgdma_start_transfer(mdev);
674 spin_unlock_bh(&mdev->lock);
675}
676
677/**
678 * msgdma_chan_desc_cleanup - Cleanup the completed descriptors
679 * @mdev: Pointer to the Altera mSGDMA device structure
680 */
681static void msgdma_chan_desc_cleanup(struct msgdma_device *mdev)
682{
683 struct msgdma_sw_desc *desc, *next;
684
685 list_for_each_entry_safe(desc, next, &mdev->done_list, node) {
686 dma_async_tx_callback callback;
687 void *callback_param;
688
689 list_del(&desc->node);
690
691 callback = desc->async_tx.callback;
692 callback_param = desc->async_tx.callback_param;
693 if (callback) {
694 spin_unlock(&mdev->lock);
695 callback(callback_param);
696 spin_lock(&mdev->lock);
697 }
698
699 /* Run any dependencies, then free the descriptor */
700 msgdma_free_descriptor(mdev, desc);
701 }
702}
703
704/**
705 * msgdma_complete_descriptor - Mark the active descriptor as complete
706 * @mdev: Pointer to the Altera mSGDMA device structure
707 */
708static void msgdma_complete_descriptor(struct msgdma_device *mdev)
709{
710 struct msgdma_sw_desc *desc;
711
712 desc = list_first_entry_or_null(&mdev->active_list,
713 struct msgdma_sw_desc, node);
714 if (!desc)
715 return;
716 list_del(&desc->node);
717 dma_cookie_complete(&desc->async_tx);
718 list_add_tail(&desc->node, &mdev->done_list);
719}
720
721/**
722 * msgdma_free_descriptors - Free channel descriptors
723 * @mdev: Pointer to the Altera mSGDMA device structure
724 */
725static void msgdma_free_descriptors(struct msgdma_device *mdev)
726{
727 msgdma_free_desc_list(mdev, &mdev->active_list);
728 msgdma_free_desc_list(mdev, &mdev->pending_list);
729 msgdma_free_desc_list(mdev, &mdev->done_list);
730}
731
732/**
733 * msgdma_free_chan_resources - Free channel resources
734 * @dchan: DMA channel pointer
735 */
736static void msgdma_free_chan_resources(struct dma_chan *dchan)
737{
738 struct msgdma_device *mdev = to_mdev(dchan);
739
740 spin_lock_bh(&mdev->lock);
741 msgdma_free_descriptors(mdev);
742 spin_unlock_bh(&mdev->lock);
743 kfree(mdev->sw_desq);
744}
745
746/**
747 * msgdma_alloc_chan_resources - Allocate channel resources
748 * @dchan: DMA channel
749 *
750 * Return: Number of descriptors on success and failure value on error
751 */
752static int msgdma_alloc_chan_resources(struct dma_chan *dchan)
753{
754 struct msgdma_device *mdev = to_mdev(dchan);
755 struct msgdma_sw_desc *desc;
756 int i;
757
758 mdev->sw_desq = kcalloc(MSGDMA_DESC_NUM, sizeof(*desc), GFP_NOWAIT);
759 if (!mdev->sw_desq)
760 return -ENOMEM;
761
762 mdev->idle = true;
763 mdev->desc_free_cnt = MSGDMA_DESC_NUM;
764
765 INIT_LIST_HEAD(&mdev->free_list);
766
767 for (i = 0; i < MSGDMA_DESC_NUM; i++) {
768 desc = mdev->sw_desq + i;
769 dma_async_tx_descriptor_init(&desc->async_tx, &mdev->dmachan);
770 desc->async_tx.tx_submit = msgdma_tx_submit;
771 list_add_tail(&desc->node, &mdev->free_list);
772 }
773
774 return MSGDMA_DESC_NUM;
775}
776
777/**
778 * msgdma_tasklet - Schedule completion tasklet
779 * @data: Pointer to the Altera sSGDMA channel structure
780 */
781static void msgdma_tasklet(unsigned long data)
782{
783 struct msgdma_device *mdev = (struct msgdma_device *)data;
784 u32 count;
785 u32 size;
786 u32 status;
787
788 spin_lock(&mdev->lock);
789
790 /* Read number of responses that are available */
791 count = ioread32(&mdev->csr->resp_fill_level);
792 dev_dbg(mdev->dev, "%s (%d): response count=%d\n",
793 __func__, __LINE__, count);
794
795 while (count--) {
796 /*
797 * Read both longwords to purge this response from the FIFO
798 * On Avalon-MM implementations, size and status do not
799 * have any real values, like transferred bytes or error
800 * bits. So we need to just drop these values.
801 */
802 size = ioread32(&mdev->resp->bytes_transferred);
803 status = ioread32(&mdev->resp->status);
804
805 msgdma_complete_descriptor(mdev);
806 msgdma_chan_desc_cleanup(mdev);
807 }
808
809 spin_unlock(&mdev->lock);
810}
811
812/**
813 * msgdma_irq_handler - Altera mSGDMA Interrupt handler
814 * @irq: IRQ number
815 * @data: Pointer to the Altera mSGDMA device structure
816 *
817 * Return: IRQ_HANDLED/IRQ_NONE
818 */
819static irqreturn_t msgdma_irq_handler(int irq, void *data)
820{
821 struct msgdma_device *mdev = data;
822 u32 status;
823
824 status = ioread32(&mdev->csr->status);
825 if ((status & MSGDMA_CSR_STAT_BUSY) == 0) {
826 /* Start next transfer if the DMA controller is idle */
827 spin_lock(&mdev->lock);
828 mdev->idle = true;
829 msgdma_start_transfer(mdev);
830 spin_unlock(&mdev->lock);
831 }
832
833 tasklet_schedule(&mdev->irq_tasklet);
834
835 /* Clear interrupt in mSGDMA controller */
836 iowrite32(MSGDMA_CSR_STAT_IRQ, &mdev->csr->status);
837
838 return IRQ_HANDLED;
839}
840
841/**
842 * msgdma_chan_remove - Channel remove function
843 * @mdev: Pointer to the Altera mSGDMA device structure
844 */
845static void msgdma_dev_remove(struct msgdma_device *mdev)
846{
847 if (!mdev)
848 return;
849
850 devm_free_irq(mdev->dev, mdev->irq, mdev);
851 tasklet_kill(&mdev->irq_tasklet);
852 list_del(&mdev->dmachan.device_node);
853}
854
855static int request_and_map(struct platform_device *pdev, const char *name,
856 struct resource **res, void __iomem **ptr)
857{
858 struct resource *region;
859 struct device *device = &pdev->dev;
860
861 *res = platform_get_resource_byname(pdev, IORESOURCE_MEM, name);
862 if (*res == NULL) {
863 dev_err(device, "resource %s not defined\n", name);
864 return -ENODEV;
865 }
866
867 region = devm_request_mem_region(device, (*res)->start,
868 resource_size(*res), dev_name(device));
869 if (region == NULL) {
870 dev_err(device, "unable to request %s\n", name);
871 return -EBUSY;
872 }
873
874 *ptr = devm_ioremap_nocache(device, region->start,
875 resource_size(region));
876 if (*ptr == NULL) {
877 dev_err(device, "ioremap_nocache of %s failed!", name);
878 return -ENOMEM;
879 }
880
881 return 0;
882}
883
884/**
885 * msgdma_probe - Driver probe function
886 * @pdev: Pointer to the platform_device structure
887 *
888 * Return: '0' on success and failure value on error
889 */
890static int msgdma_probe(struct platform_device *pdev)
891{
892 struct msgdma_device *mdev;
893 struct dma_device *dma_dev;
894 struct resource *dma_res;
895 int ret;
896
897 mdev = devm_kzalloc(&pdev->dev, sizeof(*mdev), GFP_NOWAIT);
898 if (!mdev)
899 return -ENOMEM;
900
901 mdev->dev = &pdev->dev;
902
903 /* Map CSR space */
904 ret = request_and_map(pdev, "csr", &dma_res, (void **)&mdev->csr);
905 if (ret)
906 return ret;
907
908 /* Map (extended) descriptor space */
909 ret = request_and_map(pdev, "desc", &dma_res, (void **)&mdev->desc);
910 if (ret)
911 return ret;
912
913 /* Map response space */
914 ret = request_and_map(pdev, "resp", &dma_res, (void **)&mdev->resp);
915 if (ret)
916 return ret;
917
918 platform_set_drvdata(pdev, mdev);
919
920 /* Get interrupt nr from platform data */
921 mdev->irq = platform_get_irq(pdev, 0);
922 if (mdev->irq < 0)
923 return -ENXIO;
924
925 ret = devm_request_irq(&pdev->dev, mdev->irq, msgdma_irq_handler,
926 0, dev_name(&pdev->dev), mdev);
927 if (ret)
928 return ret;
929
930 tasklet_init(&mdev->irq_tasklet, msgdma_tasklet, (unsigned long)mdev);
931
932 dma_cookie_init(&mdev->dmachan);
933
934 spin_lock_init(&mdev->lock);
935
936 INIT_LIST_HEAD(&mdev->active_list);
937 INIT_LIST_HEAD(&mdev->pending_list);
938 INIT_LIST_HEAD(&mdev->done_list);
939 INIT_LIST_HEAD(&mdev->free_list);
940
941 dma_dev = &mdev->dmadev;
942
943 /* Set DMA capabilities */
944 dma_cap_zero(dma_dev->cap_mask);
945 dma_cap_set(DMA_MEMCPY, dma_dev->cap_mask);
946 dma_cap_set(DMA_SG, dma_dev->cap_mask);
947 dma_cap_set(DMA_SLAVE, dma_dev->cap_mask);
948
949 dma_dev->src_addr_widths = BIT(DMA_SLAVE_BUSWIDTH_4_BYTES);
950 dma_dev->dst_addr_widths = BIT(DMA_SLAVE_BUSWIDTH_4_BYTES);
951 dma_dev->directions = BIT(DMA_MEM_TO_DEV) | BIT(DMA_DEV_TO_MEM) |
952 BIT(DMA_MEM_TO_MEM);
953 dma_dev->residue_granularity = DMA_RESIDUE_GRANULARITY_DESCRIPTOR;
954
955 /* Init DMA link list */
956 INIT_LIST_HEAD(&dma_dev->channels);
957
958 /* Set base routines */
959 dma_dev->device_tx_status = dma_cookie_status;
960 dma_dev->device_issue_pending = msgdma_issue_pending;
961 dma_dev->dev = &pdev->dev;
962
963 dma_dev->copy_align = DMAENGINE_ALIGN_4_BYTES;
964 dma_dev->device_prep_dma_memcpy = msgdma_prep_memcpy;
965 dma_dev->device_prep_dma_sg = msgdma_prep_sg;
966 dma_dev->device_prep_slave_sg = msgdma_prep_slave_sg;
967 dma_dev->device_config = msgdma_dma_config;
968
969 dma_dev->device_alloc_chan_resources = msgdma_alloc_chan_resources;
970 dma_dev->device_free_chan_resources = msgdma_free_chan_resources;
971
972 mdev->dmachan.device = dma_dev;
973 list_add_tail(&mdev->dmachan.device_node, &dma_dev->channels);
974
975 /* Set DMA mask to 64 bits */
976 ret = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
977 if (ret) {
978 dev_warn(&pdev->dev, "unable to set coherent mask to 64");
979 ret = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
980 if (ret)
981 goto fail;
982 }
983
984 msgdma_reset(mdev);
985
986 ret = dma_async_device_register(dma_dev);
987 if (ret)
988 goto fail;
989
990 dev_notice(&pdev->dev, "Altera mSGDMA driver probe success\n");
991
992 return 0;
993
994fail:
995 msgdma_dev_remove(mdev);
996
997 return ret;
998}
999
1000/**
1001 * msgdma_dma_remove - Driver remove function
1002 * @pdev: Pointer to the platform_device structure
1003 *
1004 * Return: Always '0'
1005 */
1006static int msgdma_remove(struct platform_device *pdev)
1007{
1008 struct msgdma_device *mdev = platform_get_drvdata(pdev);
1009
1010 dma_async_device_unregister(&mdev->dmadev);
1011 msgdma_dev_remove(mdev);
1012
1013 dev_notice(&pdev->dev, "Altera mSGDMA driver removed\n");
1014
1015 return 0;
1016}
1017
1018static struct platform_driver msgdma_driver = {
1019 .driver = {
1020 .name = "altera-msgdma",
1021 },
1022 .probe = msgdma_probe,
1023 .remove = msgdma_remove,
1024};
1025
1026module_platform_driver(msgdma_driver);
1027
1028MODULE_ALIAS("platform:altera-msgdma");
1029MODULE_DESCRIPTION("Altera mSGDMA driver");
1030MODULE_AUTHOR("Stefan Roese <sr@denx.de>");
1031MODULE_LICENSE("GPL");