dmaengine: altera: remove DMA_SG
[linux-block.git] / drivers / dma / altera-msgdma.c
CommitLineData
a85c6f1b
SR
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
a85c6f1b
SR
388/**
389 * msgdma_prep_slave_sg - prepare descriptors for a slave sg transaction
390 *
391 * @dchan: DMA channel
392 * @sgl: Destination scatter list
393 * @sg_len: Number of entries in destination scatter list
394 * @dir: DMA transfer direction
395 * @flags: transfer ack flags
396 * @context: transfer context (unused)
397 */
398static struct dma_async_tx_descriptor *
399msgdma_prep_slave_sg(struct dma_chan *dchan, struct scatterlist *sgl,
400 unsigned int sg_len, enum dma_transfer_direction dir,
401 unsigned long flags, void *context)
402
403{
404 struct msgdma_device *mdev = to_mdev(dchan);
405 struct dma_slave_config *cfg = &mdev->slave_cfg;
406 struct msgdma_sw_desc *new, *first = NULL;
407 void *desc = NULL;
408 size_t len, avail;
409 dma_addr_t dma_dst, dma_src;
410 u32 desc_cnt = 0, i;
411 struct scatterlist *sg;
412 u32 stride;
413
414 for_each_sg(sgl, sg, sg_len, i)
415 desc_cnt += DIV_ROUND_UP(sg_dma_len(sg), MSGDMA_MAX_TRANS_LEN);
416
417 spin_lock_bh(&mdev->lock);
418 if (desc_cnt > mdev->desc_free_cnt) {
419 spin_unlock_bh(&mdev->lock);
420 dev_dbg(mdev->dev, "mdev %p descs are not available\n", mdev);
421 return NULL;
422 }
423 mdev->desc_free_cnt -= desc_cnt;
424 spin_unlock_bh(&mdev->lock);
425
426 avail = sg_dma_len(sgl);
427
428 /* Run until we are out of scatterlist entries */
429 while (true) {
430 /* Allocate and populate the descriptor */
431 new = msgdma_get_descriptor(mdev);
432
433 desc = &new->hw_desc;
434 len = min_t(size_t, avail, MSGDMA_MAX_TRANS_LEN);
435
436 if (dir == DMA_MEM_TO_DEV) {
437 dma_src = sg_dma_address(sgl) + sg_dma_len(sgl) - avail;
438 dma_dst = cfg->dst_addr;
439 stride = MSGDMA_DESC_STRIDE_RD;
440 } else {
441 dma_src = cfg->src_addr;
442 dma_dst = sg_dma_address(sgl) + sg_dma_len(sgl) - avail;
443 stride = MSGDMA_DESC_STRIDE_WR;
444 }
445 msgdma_desc_config(desc, dma_dst, dma_src, len, stride);
446 avail -= len;
447
448 if (!first)
449 first = new;
450 else
451 list_add_tail(&new->node, &first->tx_list);
452
453 /* Fetch the next scatterlist entry */
454 if (avail == 0) {
455 if (sg_len == 0)
456 break;
457 sgl = sg_next(sgl);
458 if (sgl == NULL)
459 break;
460 sg_len--;
461 avail = sg_dma_len(sgl);
462 }
463 }
464
465 msgdma_desc_config_eod(desc);
466 first->async_tx.flags = flags;
467
468 return &first->async_tx;
469}
470
471static int msgdma_dma_config(struct dma_chan *dchan,
472 struct dma_slave_config *config)
473{
474 struct msgdma_device *mdev = to_mdev(dchan);
475
476 memcpy(&mdev->slave_cfg, config, sizeof(*config));
477
478 return 0;
479}
480
481static void msgdma_reset(struct msgdma_device *mdev)
482{
483 u32 val;
484 int ret;
485
486 /* Reset mSGDMA */
487 iowrite32(MSGDMA_CSR_STAT_MASK, &mdev->csr->status);
488 iowrite32(MSGDMA_CSR_CTL_RESET, &mdev->csr->control);
489
490 ret = readl_poll_timeout(&mdev->csr->status, val,
491 (val & MSGDMA_CSR_STAT_RESETTING) == 0,
492 1, 10000);
493 if (ret)
494 dev_err(mdev->dev, "DMA channel did not reset\n");
495
496 /* Clear all status bits */
497 iowrite32(MSGDMA_CSR_STAT_MASK, &mdev->csr->status);
498
499 /* Enable the DMA controller including interrupts */
500 iowrite32(MSGDMA_CSR_CTL_STOP_ON_ERR | MSGDMA_CSR_CTL_STOP_ON_EARLY |
501 MSGDMA_CSR_CTL_GLOBAL_INTR, &mdev->csr->control);
502
503 mdev->idle = true;
504};
505
506static void msgdma_copy_one(struct msgdma_device *mdev,
507 struct msgdma_sw_desc *desc)
508{
509 struct msgdma_extended_desc *hw_desc = mdev->desc;
510
511 /*
512 * Check if the DESC FIFO it not full. If its full, we need to wait
513 * for at least one entry to become free again
514 */
515 while (ioread32(&mdev->csr->status) & MSGDMA_CSR_STAT_DESC_BUF_FULL)
516 mdelay(1);
517
518 /*
519 * The descriptor needs to get copied into the descriptor FIFO
520 * of the DMA controller. The descriptor will get flushed to the
521 * FIFO, once the last word (control word) is written. Since we
522 * are not 100% sure that memcpy() writes all word in the "correct"
523 * oder (address from low to high) on all architectures, we make
524 * sure this control word is written last by single coding it and
525 * adding some write-barriers here.
526 */
527 memcpy(hw_desc, &desc->hw_desc, sizeof(desc->hw_desc) - sizeof(u32));
528
529 /* Write control word last to flush this descriptor into the FIFO */
530 mdev->idle = false;
531 wmb();
532 iowrite32(desc->hw_desc.control, &hw_desc->control);
533 wmb();
534}
535
536/**
537 * msgdma_copy_desc_to_fifo - copy descriptor(s) into controller FIFO
538 * @mdev: Pointer to the Altera mSGDMA device structure
539 * @desc: Transaction descriptor pointer
540 */
541static void msgdma_copy_desc_to_fifo(struct msgdma_device *mdev,
542 struct msgdma_sw_desc *desc)
543{
544 struct msgdma_sw_desc *sdesc, *next;
545
546 msgdma_copy_one(mdev, desc);
547
548 list_for_each_entry_safe(sdesc, next, &desc->tx_list, node)
549 msgdma_copy_one(mdev, sdesc);
550}
551
552/**
553 * msgdma_start_transfer - Initiate the new transfer
554 * @mdev: Pointer to the Altera mSGDMA device structure
555 */
556static void msgdma_start_transfer(struct msgdma_device *mdev)
557{
558 struct msgdma_sw_desc *desc;
559
560 if (!mdev->idle)
561 return;
562
563 desc = list_first_entry_or_null(&mdev->pending_list,
564 struct msgdma_sw_desc, node);
565 if (!desc)
566 return;
567
568 list_splice_tail_init(&mdev->pending_list, &mdev->active_list);
569 msgdma_copy_desc_to_fifo(mdev, desc);
570}
571
572/**
573 * msgdma_issue_pending - Issue pending transactions
574 * @chan: DMA channel pointer
575 */
576static void msgdma_issue_pending(struct dma_chan *chan)
577{
578 struct msgdma_device *mdev = to_mdev(chan);
579
580 spin_lock_bh(&mdev->lock);
581 msgdma_start_transfer(mdev);
582 spin_unlock_bh(&mdev->lock);
583}
584
585/**
586 * msgdma_chan_desc_cleanup - Cleanup the completed descriptors
587 * @mdev: Pointer to the Altera mSGDMA device structure
588 */
589static void msgdma_chan_desc_cleanup(struct msgdma_device *mdev)
590{
591 struct msgdma_sw_desc *desc, *next;
592
593 list_for_each_entry_safe(desc, next, &mdev->done_list, node) {
594 dma_async_tx_callback callback;
595 void *callback_param;
596
597 list_del(&desc->node);
598
599 callback = desc->async_tx.callback;
600 callback_param = desc->async_tx.callback_param;
601 if (callback) {
602 spin_unlock(&mdev->lock);
603 callback(callback_param);
604 spin_lock(&mdev->lock);
605 }
606
607 /* Run any dependencies, then free the descriptor */
608 msgdma_free_descriptor(mdev, desc);
609 }
610}
611
612/**
613 * msgdma_complete_descriptor - Mark the active descriptor as complete
614 * @mdev: Pointer to the Altera mSGDMA device structure
615 */
616static void msgdma_complete_descriptor(struct msgdma_device *mdev)
617{
618 struct msgdma_sw_desc *desc;
619
620 desc = list_first_entry_or_null(&mdev->active_list,
621 struct msgdma_sw_desc, node);
622 if (!desc)
623 return;
624 list_del(&desc->node);
625 dma_cookie_complete(&desc->async_tx);
626 list_add_tail(&desc->node, &mdev->done_list);
627}
628
629/**
630 * msgdma_free_descriptors - Free channel descriptors
631 * @mdev: Pointer to the Altera mSGDMA device structure
632 */
633static void msgdma_free_descriptors(struct msgdma_device *mdev)
634{
635 msgdma_free_desc_list(mdev, &mdev->active_list);
636 msgdma_free_desc_list(mdev, &mdev->pending_list);
637 msgdma_free_desc_list(mdev, &mdev->done_list);
638}
639
640/**
641 * msgdma_free_chan_resources - Free channel resources
642 * @dchan: DMA channel pointer
643 */
644static void msgdma_free_chan_resources(struct dma_chan *dchan)
645{
646 struct msgdma_device *mdev = to_mdev(dchan);
647
648 spin_lock_bh(&mdev->lock);
649 msgdma_free_descriptors(mdev);
650 spin_unlock_bh(&mdev->lock);
651 kfree(mdev->sw_desq);
652}
653
654/**
655 * msgdma_alloc_chan_resources - Allocate channel resources
656 * @dchan: DMA channel
657 *
658 * Return: Number of descriptors on success and failure value on error
659 */
660static int msgdma_alloc_chan_resources(struct dma_chan *dchan)
661{
662 struct msgdma_device *mdev = to_mdev(dchan);
663 struct msgdma_sw_desc *desc;
664 int i;
665
666 mdev->sw_desq = kcalloc(MSGDMA_DESC_NUM, sizeof(*desc), GFP_NOWAIT);
667 if (!mdev->sw_desq)
668 return -ENOMEM;
669
670 mdev->idle = true;
671 mdev->desc_free_cnt = MSGDMA_DESC_NUM;
672
673 INIT_LIST_HEAD(&mdev->free_list);
674
675 for (i = 0; i < MSGDMA_DESC_NUM; i++) {
676 desc = mdev->sw_desq + i;
677 dma_async_tx_descriptor_init(&desc->async_tx, &mdev->dmachan);
678 desc->async_tx.tx_submit = msgdma_tx_submit;
679 list_add_tail(&desc->node, &mdev->free_list);
680 }
681
682 return MSGDMA_DESC_NUM;
683}
684
685/**
686 * msgdma_tasklet - Schedule completion tasklet
687 * @data: Pointer to the Altera sSGDMA channel structure
688 */
689static void msgdma_tasklet(unsigned long data)
690{
691 struct msgdma_device *mdev = (struct msgdma_device *)data;
692 u32 count;
693 u32 size;
694 u32 status;
695
696 spin_lock(&mdev->lock);
697
698 /* Read number of responses that are available */
699 count = ioread32(&mdev->csr->resp_fill_level);
700 dev_dbg(mdev->dev, "%s (%d): response count=%d\n",
701 __func__, __LINE__, count);
702
703 while (count--) {
704 /*
705 * Read both longwords to purge this response from the FIFO
706 * On Avalon-MM implementations, size and status do not
707 * have any real values, like transferred bytes or error
708 * bits. So we need to just drop these values.
709 */
710 size = ioread32(&mdev->resp->bytes_transferred);
711 status = ioread32(&mdev->resp->status);
712
713 msgdma_complete_descriptor(mdev);
714 msgdma_chan_desc_cleanup(mdev);
715 }
716
717 spin_unlock(&mdev->lock);
718}
719
720/**
721 * msgdma_irq_handler - Altera mSGDMA Interrupt handler
722 * @irq: IRQ number
723 * @data: Pointer to the Altera mSGDMA device structure
724 *
725 * Return: IRQ_HANDLED/IRQ_NONE
726 */
727static irqreturn_t msgdma_irq_handler(int irq, void *data)
728{
729 struct msgdma_device *mdev = data;
730 u32 status;
731
732 status = ioread32(&mdev->csr->status);
733 if ((status & MSGDMA_CSR_STAT_BUSY) == 0) {
734 /* Start next transfer if the DMA controller is idle */
735 spin_lock(&mdev->lock);
736 mdev->idle = true;
737 msgdma_start_transfer(mdev);
738 spin_unlock(&mdev->lock);
739 }
740
741 tasklet_schedule(&mdev->irq_tasklet);
742
743 /* Clear interrupt in mSGDMA controller */
744 iowrite32(MSGDMA_CSR_STAT_IRQ, &mdev->csr->status);
745
746 return IRQ_HANDLED;
747}
748
749/**
750 * msgdma_chan_remove - Channel remove function
751 * @mdev: Pointer to the Altera mSGDMA device structure
752 */
753static void msgdma_dev_remove(struct msgdma_device *mdev)
754{
755 if (!mdev)
756 return;
757
758 devm_free_irq(mdev->dev, mdev->irq, mdev);
759 tasklet_kill(&mdev->irq_tasklet);
760 list_del(&mdev->dmachan.device_node);
761}
762
763static int request_and_map(struct platform_device *pdev, const char *name,
764 struct resource **res, void __iomem **ptr)
765{
766 struct resource *region;
767 struct device *device = &pdev->dev;
768
769 *res = platform_get_resource_byname(pdev, IORESOURCE_MEM, name);
770 if (*res == NULL) {
771 dev_err(device, "resource %s not defined\n", name);
772 return -ENODEV;
773 }
774
775 region = devm_request_mem_region(device, (*res)->start,
776 resource_size(*res), dev_name(device));
777 if (region == NULL) {
778 dev_err(device, "unable to request %s\n", name);
779 return -EBUSY;
780 }
781
782 *ptr = devm_ioremap_nocache(device, region->start,
783 resource_size(region));
784 if (*ptr == NULL) {
785 dev_err(device, "ioremap_nocache of %s failed!", name);
786 return -ENOMEM;
787 }
788
789 return 0;
790}
791
792/**
793 * msgdma_probe - Driver probe function
794 * @pdev: Pointer to the platform_device structure
795 *
796 * Return: '0' on success and failure value on error
797 */
798static int msgdma_probe(struct platform_device *pdev)
799{
800 struct msgdma_device *mdev;
801 struct dma_device *dma_dev;
802 struct resource *dma_res;
803 int ret;
804
805 mdev = devm_kzalloc(&pdev->dev, sizeof(*mdev), GFP_NOWAIT);
806 if (!mdev)
807 return -ENOMEM;
808
809 mdev->dev = &pdev->dev;
810
811 /* Map CSR space */
812 ret = request_and_map(pdev, "csr", &dma_res, (void **)&mdev->csr);
813 if (ret)
814 return ret;
815
816 /* Map (extended) descriptor space */
817 ret = request_and_map(pdev, "desc", &dma_res, (void **)&mdev->desc);
818 if (ret)
819 return ret;
820
821 /* Map response space */
822 ret = request_and_map(pdev, "resp", &dma_res, (void **)&mdev->resp);
823 if (ret)
824 return ret;
825
826 platform_set_drvdata(pdev, mdev);
827
828 /* Get interrupt nr from platform data */
829 mdev->irq = platform_get_irq(pdev, 0);
830 if (mdev->irq < 0)
831 return -ENXIO;
832
833 ret = devm_request_irq(&pdev->dev, mdev->irq, msgdma_irq_handler,
834 0, dev_name(&pdev->dev), mdev);
835 if (ret)
836 return ret;
837
838 tasklet_init(&mdev->irq_tasklet, msgdma_tasklet, (unsigned long)mdev);
839
840 dma_cookie_init(&mdev->dmachan);
841
842 spin_lock_init(&mdev->lock);
843
844 INIT_LIST_HEAD(&mdev->active_list);
845 INIT_LIST_HEAD(&mdev->pending_list);
846 INIT_LIST_HEAD(&mdev->done_list);
847 INIT_LIST_HEAD(&mdev->free_list);
848
849 dma_dev = &mdev->dmadev;
850
851 /* Set DMA capabilities */
852 dma_cap_zero(dma_dev->cap_mask);
853 dma_cap_set(DMA_MEMCPY, dma_dev->cap_mask);
a85c6f1b
SR
854 dma_cap_set(DMA_SLAVE, dma_dev->cap_mask);
855
856 dma_dev->src_addr_widths = BIT(DMA_SLAVE_BUSWIDTH_4_BYTES);
857 dma_dev->dst_addr_widths = BIT(DMA_SLAVE_BUSWIDTH_4_BYTES);
858 dma_dev->directions = BIT(DMA_MEM_TO_DEV) | BIT(DMA_DEV_TO_MEM) |
859 BIT(DMA_MEM_TO_MEM);
860 dma_dev->residue_granularity = DMA_RESIDUE_GRANULARITY_DESCRIPTOR;
861
862 /* Init DMA link list */
863 INIT_LIST_HEAD(&dma_dev->channels);
864
865 /* Set base routines */
866 dma_dev->device_tx_status = dma_cookie_status;
867 dma_dev->device_issue_pending = msgdma_issue_pending;
868 dma_dev->dev = &pdev->dev;
869
870 dma_dev->copy_align = DMAENGINE_ALIGN_4_BYTES;
871 dma_dev->device_prep_dma_memcpy = msgdma_prep_memcpy;
a85c6f1b
SR
872 dma_dev->device_prep_slave_sg = msgdma_prep_slave_sg;
873 dma_dev->device_config = msgdma_dma_config;
874
875 dma_dev->device_alloc_chan_resources = msgdma_alloc_chan_resources;
876 dma_dev->device_free_chan_resources = msgdma_free_chan_resources;
877
878 mdev->dmachan.device = dma_dev;
879 list_add_tail(&mdev->dmachan.device_node, &dma_dev->channels);
880
881 /* Set DMA mask to 64 bits */
882 ret = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
883 if (ret) {
884 dev_warn(&pdev->dev, "unable to set coherent mask to 64");
885 ret = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
886 if (ret)
887 goto fail;
888 }
889
890 msgdma_reset(mdev);
891
892 ret = dma_async_device_register(dma_dev);
893 if (ret)
894 goto fail;
895
896 dev_notice(&pdev->dev, "Altera mSGDMA driver probe success\n");
897
898 return 0;
899
900fail:
901 msgdma_dev_remove(mdev);
902
903 return ret;
904}
905
906/**
907 * msgdma_dma_remove - Driver remove function
908 * @pdev: Pointer to the platform_device structure
909 *
910 * Return: Always '0'
911 */
912static int msgdma_remove(struct platform_device *pdev)
913{
914 struct msgdma_device *mdev = platform_get_drvdata(pdev);
915
916 dma_async_device_unregister(&mdev->dmadev);
917 msgdma_dev_remove(mdev);
918
919 dev_notice(&pdev->dev, "Altera mSGDMA driver removed\n");
920
921 return 0;
922}
923
924static struct platform_driver msgdma_driver = {
925 .driver = {
926 .name = "altera-msgdma",
927 },
928 .probe = msgdma_probe,
929 .remove = msgdma_remove,
930};
931
932module_platform_driver(msgdma_driver);
933
934MODULE_ALIAS("platform:altera-msgdma");
935MODULE_DESCRIPTION("Altera mSGDMA driver");
936MODULE_AUTHOR("Stefan Roese <sr@denx.de>");
937MODULE_LICENSE("GPL");