staging: most: core: add a match function for the bus
[linux/fpc-iii.git] / drivers / dma / st_fdma.c
blobbfb79bd0c6de5733205c22baeec582bc2fdf9ec7
1 /*
2 * DMA driver for STMicroelectronics STi FDMA controller
4 * Copyright (C) 2014 STMicroelectronics
6 * Author: Ludovic Barre <Ludovic.barre@st.com>
7 * Peter Griffin <peter.griffin@linaro.org>
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.
15 #include <linux/init.h>
16 #include <linux/module.h>
17 #include <linux/of_device.h>
18 #include <linux/of_dma.h>
19 #include <linux/platform_device.h>
20 #include <linux/interrupt.h>
21 #include <linux/remoteproc.h>
23 #include "st_fdma.h"
25 static inline struct st_fdma_chan *to_st_fdma_chan(struct dma_chan *c)
27 return container_of(c, struct st_fdma_chan, vchan.chan);
30 static struct st_fdma_desc *to_st_fdma_desc(struct virt_dma_desc *vd)
32 return container_of(vd, struct st_fdma_desc, vdesc);
35 static int st_fdma_dreq_get(struct st_fdma_chan *fchan)
37 struct st_fdma_dev *fdev = fchan->fdev;
38 u32 req_line_cfg = fchan->cfg.req_line;
39 u32 dreq_line;
40 int try = 0;
43 * dreq_mask is shared for n channels of fdma, so all accesses must be
44 * atomic. if the dreq_mask is changed between ffz and set_bit,
45 * we retry
47 do {
48 if (fdev->dreq_mask == ~0L) {
49 dev_err(fdev->dev, "No req lines available\n");
50 return -EINVAL;
53 if (try || req_line_cfg >= ST_FDMA_NR_DREQS) {
54 dev_err(fdev->dev, "Invalid or used req line\n");
55 return -EINVAL;
56 } else {
57 dreq_line = req_line_cfg;
60 try++;
61 } while (test_and_set_bit(dreq_line, &fdev->dreq_mask));
63 dev_dbg(fdev->dev, "get dreq_line:%d mask:%#lx\n",
64 dreq_line, fdev->dreq_mask);
66 return dreq_line;
69 static void st_fdma_dreq_put(struct st_fdma_chan *fchan)
71 struct st_fdma_dev *fdev = fchan->fdev;
73 dev_dbg(fdev->dev, "put dreq_line:%#x\n", fchan->dreq_line);
74 clear_bit(fchan->dreq_line, &fdev->dreq_mask);
77 static void st_fdma_xfer_desc(struct st_fdma_chan *fchan)
79 struct virt_dma_desc *vdesc;
80 unsigned long nbytes, ch_cmd, cmd;
82 vdesc = vchan_next_desc(&fchan->vchan);
83 if (!vdesc)
84 return;
86 fchan->fdesc = to_st_fdma_desc(vdesc);
87 nbytes = fchan->fdesc->node[0].desc->nbytes;
88 cmd = FDMA_CMD_START(fchan->vchan.chan.chan_id);
89 ch_cmd = fchan->fdesc->node[0].pdesc | FDMA_CH_CMD_STA_START;
91 /* start the channel for the descriptor */
92 fnode_write(fchan, nbytes, FDMA_CNTN_OFST);
93 fchan_write(fchan, ch_cmd, FDMA_CH_CMD_OFST);
94 writel(cmd,
95 fchan->fdev->slim_rproc->peri + FDMA_CMD_SET_OFST);
97 dev_dbg(fchan->fdev->dev, "start chan:%d\n", fchan->vchan.chan.chan_id);
100 static void st_fdma_ch_sta_update(struct st_fdma_chan *fchan,
101 unsigned long int_sta)
103 unsigned long ch_sta, ch_err;
104 int ch_id = fchan->vchan.chan.chan_id;
105 struct st_fdma_dev *fdev = fchan->fdev;
107 ch_sta = fchan_read(fchan, FDMA_CH_CMD_OFST);
108 ch_err = ch_sta & FDMA_CH_CMD_ERR_MASK;
109 ch_sta &= FDMA_CH_CMD_STA_MASK;
111 if (int_sta & FDMA_INT_STA_ERR) {
112 dev_warn(fdev->dev, "chan:%d, error:%ld\n", ch_id, ch_err);
113 fchan->status = DMA_ERROR;
114 return;
117 switch (ch_sta) {
118 case FDMA_CH_CMD_STA_PAUSED:
119 fchan->status = DMA_PAUSED;
120 break;
122 case FDMA_CH_CMD_STA_RUNNING:
123 fchan->status = DMA_IN_PROGRESS;
124 break;
128 static irqreturn_t st_fdma_irq_handler(int irq, void *dev_id)
130 struct st_fdma_dev *fdev = dev_id;
131 irqreturn_t ret = IRQ_NONE;
132 struct st_fdma_chan *fchan = &fdev->chans[0];
133 unsigned long int_sta, clr;
135 int_sta = fdma_read(fdev, FDMA_INT_STA_OFST);
136 clr = int_sta;
138 for (; int_sta != 0 ; int_sta >>= 2, fchan++) {
139 if (!(int_sta & (FDMA_INT_STA_CH | FDMA_INT_STA_ERR)))
140 continue;
142 spin_lock(&fchan->vchan.lock);
143 st_fdma_ch_sta_update(fchan, int_sta);
145 if (fchan->fdesc) {
146 if (!fchan->fdesc->iscyclic) {
147 list_del(&fchan->fdesc->vdesc.node);
148 vchan_cookie_complete(&fchan->fdesc->vdesc);
149 fchan->fdesc = NULL;
150 fchan->status = DMA_COMPLETE;
151 } else {
152 vchan_cyclic_callback(&fchan->fdesc->vdesc);
155 /* Start the next descriptor (if available) */
156 if (!fchan->fdesc)
157 st_fdma_xfer_desc(fchan);
160 spin_unlock(&fchan->vchan.lock);
161 ret = IRQ_HANDLED;
164 fdma_write(fdev, clr, FDMA_INT_CLR_OFST);
166 return ret;
169 static struct dma_chan *st_fdma_of_xlate(struct of_phandle_args *dma_spec,
170 struct of_dma *ofdma)
172 struct st_fdma_dev *fdev = ofdma->of_dma_data;
173 struct dma_chan *chan;
174 struct st_fdma_chan *fchan;
175 int ret;
177 if (dma_spec->args_count < 1)
178 return ERR_PTR(-EINVAL);
180 if (fdev->dma_device.dev->of_node != dma_spec->np)
181 return ERR_PTR(-EINVAL);
183 ret = rproc_boot(fdev->slim_rproc->rproc);
184 if (ret == -ENOENT)
185 return ERR_PTR(-EPROBE_DEFER);
186 else if (ret)
187 return ERR_PTR(ret);
189 chan = dma_get_any_slave_channel(&fdev->dma_device);
190 if (!chan)
191 goto err_chan;
193 fchan = to_st_fdma_chan(chan);
195 fchan->cfg.of_node = dma_spec->np;
196 fchan->cfg.req_line = dma_spec->args[0];
197 fchan->cfg.req_ctrl = 0;
198 fchan->cfg.type = ST_FDMA_TYPE_FREE_RUN;
200 if (dma_spec->args_count > 1)
201 fchan->cfg.req_ctrl = dma_spec->args[1]
202 & FDMA_REQ_CTRL_CFG_MASK;
204 if (dma_spec->args_count > 2)
205 fchan->cfg.type = dma_spec->args[2];
207 if (fchan->cfg.type == ST_FDMA_TYPE_FREE_RUN) {
208 fchan->dreq_line = 0;
209 } else {
210 fchan->dreq_line = st_fdma_dreq_get(fchan);
211 if (IS_ERR_VALUE(fchan->dreq_line)) {
212 chan = ERR_PTR(fchan->dreq_line);
213 goto err_chan;
217 dev_dbg(fdev->dev, "xlate req_line:%d type:%d req_ctrl:%#lx\n",
218 fchan->cfg.req_line, fchan->cfg.type, fchan->cfg.req_ctrl);
220 return chan;
222 err_chan:
223 rproc_shutdown(fdev->slim_rproc->rproc);
224 return chan;
228 static void st_fdma_free_desc(struct virt_dma_desc *vdesc)
230 struct st_fdma_desc *fdesc;
231 int i;
233 fdesc = to_st_fdma_desc(vdesc);
234 for (i = 0; i < fdesc->n_nodes; i++)
235 dma_pool_free(fdesc->fchan->node_pool, fdesc->node[i].desc,
236 fdesc->node[i].pdesc);
237 kfree(fdesc);
240 static struct st_fdma_desc *st_fdma_alloc_desc(struct st_fdma_chan *fchan,
241 int sg_len)
243 struct st_fdma_desc *fdesc;
244 int i;
246 fdesc = kzalloc(sizeof(*fdesc) +
247 sizeof(struct st_fdma_sw_node) * sg_len, GFP_NOWAIT);
248 if (!fdesc)
249 return NULL;
251 fdesc->fchan = fchan;
252 fdesc->n_nodes = sg_len;
253 for (i = 0; i < sg_len; i++) {
254 fdesc->node[i].desc = dma_pool_alloc(fchan->node_pool,
255 GFP_NOWAIT, &fdesc->node[i].pdesc);
256 if (!fdesc->node[i].desc)
257 goto err;
259 return fdesc;
261 err:
262 while (--i >= 0)
263 dma_pool_free(fchan->node_pool, fdesc->node[i].desc,
264 fdesc->node[i].pdesc);
265 kfree(fdesc);
266 return NULL;
269 static int st_fdma_alloc_chan_res(struct dma_chan *chan)
271 struct st_fdma_chan *fchan = to_st_fdma_chan(chan);
273 /* Create the dma pool for descriptor allocation */
274 fchan->node_pool = dma_pool_create(dev_name(&chan->dev->device),
275 fchan->fdev->dev,
276 sizeof(struct st_fdma_hw_node),
277 __alignof__(struct st_fdma_hw_node),
280 if (!fchan->node_pool) {
281 dev_err(fchan->fdev->dev, "unable to allocate desc pool\n");
282 return -ENOMEM;
285 dev_dbg(fchan->fdev->dev, "alloc ch_id:%d type:%d\n",
286 fchan->vchan.chan.chan_id, fchan->cfg.type);
288 return 0;
291 static void st_fdma_free_chan_res(struct dma_chan *chan)
293 struct st_fdma_chan *fchan = to_st_fdma_chan(chan);
294 struct rproc *rproc = fchan->fdev->slim_rproc->rproc;
295 unsigned long flags;
297 LIST_HEAD(head);
299 dev_dbg(fchan->fdev->dev, "%s: freeing chan:%d\n",
300 __func__, fchan->vchan.chan.chan_id);
302 if (fchan->cfg.type != ST_FDMA_TYPE_FREE_RUN)
303 st_fdma_dreq_put(fchan);
305 spin_lock_irqsave(&fchan->vchan.lock, flags);
306 fchan->fdesc = NULL;
307 spin_unlock_irqrestore(&fchan->vchan.lock, flags);
309 dma_pool_destroy(fchan->node_pool);
310 fchan->node_pool = NULL;
311 memset(&fchan->cfg, 0, sizeof(struct st_fdma_cfg));
313 rproc_shutdown(rproc);
316 static struct dma_async_tx_descriptor *st_fdma_prep_dma_memcpy(
317 struct dma_chan *chan, dma_addr_t dst, dma_addr_t src,
318 size_t len, unsigned long flags)
320 struct st_fdma_chan *fchan;
321 struct st_fdma_desc *fdesc;
322 struct st_fdma_hw_node *hw_node;
324 if (!len)
325 return NULL;
327 fchan = to_st_fdma_chan(chan);
329 /* We only require a single descriptor */
330 fdesc = st_fdma_alloc_desc(fchan, 1);
331 if (!fdesc) {
332 dev_err(fchan->fdev->dev, "no memory for desc\n");
333 return NULL;
336 hw_node = fdesc->node[0].desc;
337 hw_node->next = 0;
338 hw_node->control = FDMA_NODE_CTRL_REQ_MAP_FREE_RUN;
339 hw_node->control |= FDMA_NODE_CTRL_SRC_INCR;
340 hw_node->control |= FDMA_NODE_CTRL_DST_INCR;
341 hw_node->control |= FDMA_NODE_CTRL_INT_EON;
342 hw_node->nbytes = len;
343 hw_node->saddr = src;
344 hw_node->daddr = dst;
345 hw_node->generic.length = len;
346 hw_node->generic.sstride = 0;
347 hw_node->generic.dstride = 0;
349 return vchan_tx_prep(&fchan->vchan, &fdesc->vdesc, flags);
352 static int config_reqctrl(struct st_fdma_chan *fchan,
353 enum dma_transfer_direction direction)
355 u32 maxburst = 0, addr = 0;
356 enum dma_slave_buswidth width;
357 int ch_id = fchan->vchan.chan.chan_id;
358 struct st_fdma_dev *fdev = fchan->fdev;
360 switch (direction) {
362 case DMA_DEV_TO_MEM:
363 fchan->cfg.req_ctrl &= ~FDMA_REQ_CTRL_WNR;
364 maxburst = fchan->scfg.src_maxburst;
365 width = fchan->scfg.src_addr_width;
366 addr = fchan->scfg.src_addr;
367 break;
369 case DMA_MEM_TO_DEV:
370 fchan->cfg.req_ctrl |= FDMA_REQ_CTRL_WNR;
371 maxburst = fchan->scfg.dst_maxburst;
372 width = fchan->scfg.dst_addr_width;
373 addr = fchan->scfg.dst_addr;
374 break;
376 default:
377 return -EINVAL;
380 fchan->cfg.req_ctrl &= ~FDMA_REQ_CTRL_OPCODE_MASK;
382 switch (width) {
384 case DMA_SLAVE_BUSWIDTH_1_BYTE:
385 fchan->cfg.req_ctrl |= FDMA_REQ_CTRL_OPCODE_LD_ST1;
386 break;
388 case DMA_SLAVE_BUSWIDTH_2_BYTES:
389 fchan->cfg.req_ctrl |= FDMA_REQ_CTRL_OPCODE_LD_ST2;
390 break;
392 case DMA_SLAVE_BUSWIDTH_4_BYTES:
393 fchan->cfg.req_ctrl |= FDMA_REQ_CTRL_OPCODE_LD_ST4;
394 break;
396 case DMA_SLAVE_BUSWIDTH_8_BYTES:
397 fchan->cfg.req_ctrl |= FDMA_REQ_CTRL_OPCODE_LD_ST8;
398 break;
400 default:
401 return -EINVAL;
404 fchan->cfg.req_ctrl &= ~FDMA_REQ_CTRL_NUM_OPS_MASK;
405 fchan->cfg.req_ctrl |= FDMA_REQ_CTRL_NUM_OPS(maxburst-1);
406 dreq_write(fchan, fchan->cfg.req_ctrl, FDMA_REQ_CTRL_OFST);
408 fchan->cfg.dev_addr = addr;
409 fchan->cfg.dir = direction;
411 dev_dbg(fdev->dev, "chan:%d config_reqctrl:%#x req_ctrl:%#lx\n",
412 ch_id, addr, fchan->cfg.req_ctrl);
414 return 0;
417 static void fill_hw_node(struct st_fdma_hw_node *hw_node,
418 struct st_fdma_chan *fchan,
419 enum dma_transfer_direction direction)
421 if (direction == DMA_MEM_TO_DEV) {
422 hw_node->control |= FDMA_NODE_CTRL_SRC_INCR;
423 hw_node->control |= FDMA_NODE_CTRL_DST_STATIC;
424 hw_node->daddr = fchan->cfg.dev_addr;
425 } else {
426 hw_node->control |= FDMA_NODE_CTRL_SRC_STATIC;
427 hw_node->control |= FDMA_NODE_CTRL_DST_INCR;
428 hw_node->saddr = fchan->cfg.dev_addr;
431 hw_node->generic.sstride = 0;
432 hw_node->generic.dstride = 0;
435 static inline struct st_fdma_chan *st_fdma_prep_common(struct dma_chan *chan,
436 size_t len, enum dma_transfer_direction direction)
438 struct st_fdma_chan *fchan;
440 if (!chan || !len)
441 return NULL;
443 fchan = to_st_fdma_chan(chan);
445 if (!is_slave_direction(direction)) {
446 dev_err(fchan->fdev->dev, "bad direction?\n");
447 return NULL;
450 return fchan;
453 static struct dma_async_tx_descriptor *st_fdma_prep_dma_cyclic(
454 struct dma_chan *chan, dma_addr_t buf_addr, size_t len,
455 size_t period_len, enum dma_transfer_direction direction,
456 unsigned long flags)
458 struct st_fdma_chan *fchan;
459 struct st_fdma_desc *fdesc;
460 int sg_len, i;
462 fchan = st_fdma_prep_common(chan, len, direction);
463 if (!fchan)
464 return NULL;
466 if (!period_len)
467 return NULL;
469 if (config_reqctrl(fchan, direction)) {
470 dev_err(fchan->fdev->dev, "bad width or direction\n");
471 return NULL;
474 /* the buffer length must be a multiple of period_len */
475 if (len % period_len != 0) {
476 dev_err(fchan->fdev->dev, "len is not multiple of period\n");
477 return NULL;
480 sg_len = len / period_len;
481 fdesc = st_fdma_alloc_desc(fchan, sg_len);
482 if (!fdesc) {
483 dev_err(fchan->fdev->dev, "no memory for desc\n");
484 return NULL;
487 fdesc->iscyclic = true;
489 for (i = 0; i < sg_len; i++) {
490 struct st_fdma_hw_node *hw_node = fdesc->node[i].desc;
492 hw_node->next = fdesc->node[(i + 1) % sg_len].pdesc;
494 hw_node->control =
495 FDMA_NODE_CTRL_REQ_MAP_DREQ(fchan->dreq_line);
496 hw_node->control |= FDMA_NODE_CTRL_INT_EON;
498 fill_hw_node(hw_node, fchan, direction);
500 if (direction == DMA_MEM_TO_DEV)
501 hw_node->saddr = buf_addr + (i * period_len);
502 else
503 hw_node->daddr = buf_addr + (i * period_len);
505 hw_node->nbytes = period_len;
506 hw_node->generic.length = period_len;
509 return vchan_tx_prep(&fchan->vchan, &fdesc->vdesc, flags);
512 static struct dma_async_tx_descriptor *st_fdma_prep_slave_sg(
513 struct dma_chan *chan, struct scatterlist *sgl,
514 unsigned int sg_len, enum dma_transfer_direction direction,
515 unsigned long flags, void *context)
517 struct st_fdma_chan *fchan;
518 struct st_fdma_desc *fdesc;
519 struct st_fdma_hw_node *hw_node;
520 struct scatterlist *sg;
521 int i;
523 fchan = st_fdma_prep_common(chan, sg_len, direction);
524 if (!fchan)
525 return NULL;
527 if (!sgl)
528 return NULL;
530 fdesc = st_fdma_alloc_desc(fchan, sg_len);
531 if (!fdesc) {
532 dev_err(fchan->fdev->dev, "no memory for desc\n");
533 return NULL;
536 fdesc->iscyclic = false;
538 for_each_sg(sgl, sg, sg_len, i) {
539 hw_node = fdesc->node[i].desc;
541 hw_node->next = fdesc->node[(i + 1) % sg_len].pdesc;
542 hw_node->control = FDMA_NODE_CTRL_REQ_MAP_DREQ(fchan->dreq_line);
544 fill_hw_node(hw_node, fchan, direction);
546 if (direction == DMA_MEM_TO_DEV)
547 hw_node->saddr = sg_dma_address(sg);
548 else
549 hw_node->daddr = sg_dma_address(sg);
551 hw_node->nbytes = sg_dma_len(sg);
552 hw_node->generic.length = sg_dma_len(sg);
555 /* interrupt at end of last node */
556 hw_node->control |= FDMA_NODE_CTRL_INT_EON;
558 return vchan_tx_prep(&fchan->vchan, &fdesc->vdesc, flags);
561 static size_t st_fdma_desc_residue(struct st_fdma_chan *fchan,
562 struct virt_dma_desc *vdesc,
563 bool in_progress)
565 struct st_fdma_desc *fdesc = fchan->fdesc;
566 size_t residue = 0;
567 dma_addr_t cur_addr = 0;
568 int i;
570 if (in_progress) {
571 cur_addr = fchan_read(fchan, FDMA_CH_CMD_OFST);
572 cur_addr &= FDMA_CH_CMD_DATA_MASK;
575 for (i = fchan->fdesc->n_nodes - 1 ; i >= 0; i--) {
576 if (cur_addr == fdesc->node[i].pdesc) {
577 residue += fnode_read(fchan, FDMA_CNTN_OFST);
578 break;
580 residue += fdesc->node[i].desc->nbytes;
583 return residue;
586 static enum dma_status st_fdma_tx_status(struct dma_chan *chan,
587 dma_cookie_t cookie,
588 struct dma_tx_state *txstate)
590 struct st_fdma_chan *fchan = to_st_fdma_chan(chan);
591 struct virt_dma_desc *vd;
592 enum dma_status ret;
593 unsigned long flags;
595 ret = dma_cookie_status(chan, cookie, txstate);
596 if (ret == DMA_COMPLETE || !txstate)
597 return ret;
599 spin_lock_irqsave(&fchan->vchan.lock, flags);
600 vd = vchan_find_desc(&fchan->vchan, cookie);
601 if (fchan->fdesc && cookie == fchan->fdesc->vdesc.tx.cookie)
602 txstate->residue = st_fdma_desc_residue(fchan, vd, true);
603 else if (vd)
604 txstate->residue = st_fdma_desc_residue(fchan, vd, false);
605 else
606 txstate->residue = 0;
608 spin_unlock_irqrestore(&fchan->vchan.lock, flags);
610 return ret;
613 static void st_fdma_issue_pending(struct dma_chan *chan)
615 struct st_fdma_chan *fchan = to_st_fdma_chan(chan);
616 unsigned long flags;
618 spin_lock_irqsave(&fchan->vchan.lock, flags);
620 if (vchan_issue_pending(&fchan->vchan) && !fchan->fdesc)
621 st_fdma_xfer_desc(fchan);
623 spin_unlock_irqrestore(&fchan->vchan.lock, flags);
626 static int st_fdma_pause(struct dma_chan *chan)
628 unsigned long flags;
629 LIST_HEAD(head);
630 struct st_fdma_chan *fchan = to_st_fdma_chan(chan);
631 int ch_id = fchan->vchan.chan.chan_id;
632 unsigned long cmd = FDMA_CMD_PAUSE(ch_id);
634 dev_dbg(fchan->fdev->dev, "pause chan:%d\n", ch_id);
636 spin_lock_irqsave(&fchan->vchan.lock, flags);
637 if (fchan->fdesc)
638 fdma_write(fchan->fdev, cmd, FDMA_CMD_SET_OFST);
639 spin_unlock_irqrestore(&fchan->vchan.lock, flags);
641 return 0;
644 static int st_fdma_resume(struct dma_chan *chan)
646 unsigned long flags;
647 unsigned long val;
648 struct st_fdma_chan *fchan = to_st_fdma_chan(chan);
649 int ch_id = fchan->vchan.chan.chan_id;
651 dev_dbg(fchan->fdev->dev, "resume chan:%d\n", ch_id);
653 spin_lock_irqsave(&fchan->vchan.lock, flags);
654 if (fchan->fdesc) {
655 val = fchan_read(fchan, FDMA_CH_CMD_OFST);
656 val &= FDMA_CH_CMD_DATA_MASK;
657 fchan_write(fchan, val, FDMA_CH_CMD_OFST);
659 spin_unlock_irqrestore(&fchan->vchan.lock, flags);
661 return 0;
664 static int st_fdma_terminate_all(struct dma_chan *chan)
666 unsigned long flags;
667 LIST_HEAD(head);
668 struct st_fdma_chan *fchan = to_st_fdma_chan(chan);
669 int ch_id = fchan->vchan.chan.chan_id;
670 unsigned long cmd = FDMA_CMD_PAUSE(ch_id);
672 dev_dbg(fchan->fdev->dev, "terminate chan:%d\n", ch_id);
674 spin_lock_irqsave(&fchan->vchan.lock, flags);
675 fdma_write(fchan->fdev, cmd, FDMA_CMD_SET_OFST);
676 fchan->fdesc = NULL;
677 vchan_get_all_descriptors(&fchan->vchan, &head);
678 spin_unlock_irqrestore(&fchan->vchan.lock, flags);
679 vchan_dma_desc_free_list(&fchan->vchan, &head);
681 return 0;
684 static int st_fdma_slave_config(struct dma_chan *chan,
685 struct dma_slave_config *slave_cfg)
687 struct st_fdma_chan *fchan = to_st_fdma_chan(chan);
689 memcpy(&fchan->scfg, slave_cfg, sizeof(fchan->scfg));
690 return 0;
693 static const struct st_fdma_driverdata fdma_mpe31_stih407_11 = {
694 .name = "STiH407",
695 .id = 0,
698 static const struct st_fdma_driverdata fdma_mpe31_stih407_12 = {
699 .name = "STiH407",
700 .id = 1,
703 static const struct st_fdma_driverdata fdma_mpe31_stih407_13 = {
704 .name = "STiH407",
705 .id = 2,
708 static const struct of_device_id st_fdma_match[] = {
709 { .compatible = "st,stih407-fdma-mpe31-11"
710 , .data = &fdma_mpe31_stih407_11 },
711 { .compatible = "st,stih407-fdma-mpe31-12"
712 , .data = &fdma_mpe31_stih407_12 },
713 { .compatible = "st,stih407-fdma-mpe31-13"
714 , .data = &fdma_mpe31_stih407_13 },
717 MODULE_DEVICE_TABLE(of, st_fdma_match);
719 static int st_fdma_parse_dt(struct platform_device *pdev,
720 const struct st_fdma_driverdata *drvdata,
721 struct st_fdma_dev *fdev)
723 snprintf(fdev->fw_name, FW_NAME_SIZE, "fdma_%s_%d.elf",
724 drvdata->name, drvdata->id);
726 return of_property_read_u32(pdev->dev.of_node, "dma-channels",
727 &fdev->nr_channels);
729 #define FDMA_DMA_BUSWIDTHS (BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) | \
730 BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) | \
731 BIT(DMA_SLAVE_BUSWIDTH_3_BYTES) | \
732 BIT(DMA_SLAVE_BUSWIDTH_4_BYTES))
734 static void st_fdma_free(struct st_fdma_dev *fdev)
736 struct st_fdma_chan *fchan;
737 int i;
739 for (i = 0; i < fdev->nr_channels; i++) {
740 fchan = &fdev->chans[i];
741 list_del(&fchan->vchan.chan.device_node);
742 tasklet_kill(&fchan->vchan.task);
746 static int st_fdma_probe(struct platform_device *pdev)
748 struct st_fdma_dev *fdev;
749 const struct of_device_id *match;
750 struct device_node *np = pdev->dev.of_node;
751 const struct st_fdma_driverdata *drvdata;
752 int ret, i;
754 match = of_match_device((st_fdma_match), &pdev->dev);
755 if (!match || !match->data) {
756 dev_err(&pdev->dev, "No device match found\n");
757 return -ENODEV;
760 drvdata = match->data;
762 fdev = devm_kzalloc(&pdev->dev, sizeof(*fdev), GFP_KERNEL);
763 if (!fdev)
764 return -ENOMEM;
766 ret = st_fdma_parse_dt(pdev, drvdata, fdev);
767 if (ret) {
768 dev_err(&pdev->dev, "unable to find platform data\n");
769 goto err;
772 fdev->chans = devm_kcalloc(&pdev->dev, fdev->nr_channels,
773 sizeof(struct st_fdma_chan), GFP_KERNEL);
774 if (!fdev->chans)
775 return -ENOMEM;
777 fdev->dev = &pdev->dev;
778 fdev->drvdata = drvdata;
779 platform_set_drvdata(pdev, fdev);
781 fdev->irq = platform_get_irq(pdev, 0);
782 if (fdev->irq < 0) {
783 dev_err(&pdev->dev, "Failed to get irq resource\n");
784 return -EINVAL;
787 ret = devm_request_irq(&pdev->dev, fdev->irq, st_fdma_irq_handler, 0,
788 dev_name(&pdev->dev), fdev);
789 if (ret) {
790 dev_err(&pdev->dev, "Failed to request irq (%d)\n", ret);
791 goto err;
794 fdev->slim_rproc = st_slim_rproc_alloc(pdev, fdev->fw_name);
795 if (IS_ERR(fdev->slim_rproc)) {
796 ret = PTR_ERR(fdev->slim_rproc);
797 dev_err(&pdev->dev, "slim_rproc_alloc failed (%d)\n", ret);
798 goto err;
801 /* Initialise list of FDMA channels */
802 INIT_LIST_HEAD(&fdev->dma_device.channels);
803 for (i = 0; i < fdev->nr_channels; i++) {
804 struct st_fdma_chan *fchan = &fdev->chans[i];
806 fchan->fdev = fdev;
807 fchan->vchan.desc_free = st_fdma_free_desc;
808 vchan_init(&fchan->vchan, &fdev->dma_device);
811 /* Initialise the FDMA dreq (reserve 0 & 31 for FDMA use) */
812 fdev->dreq_mask = BIT(0) | BIT(31);
814 dma_cap_set(DMA_SLAVE, fdev->dma_device.cap_mask);
815 dma_cap_set(DMA_CYCLIC, fdev->dma_device.cap_mask);
816 dma_cap_set(DMA_MEMCPY, fdev->dma_device.cap_mask);
818 fdev->dma_device.dev = &pdev->dev;
819 fdev->dma_device.device_alloc_chan_resources = st_fdma_alloc_chan_res;
820 fdev->dma_device.device_free_chan_resources = st_fdma_free_chan_res;
821 fdev->dma_device.device_prep_dma_cyclic = st_fdma_prep_dma_cyclic;
822 fdev->dma_device.device_prep_slave_sg = st_fdma_prep_slave_sg;
823 fdev->dma_device.device_prep_dma_memcpy = st_fdma_prep_dma_memcpy;
824 fdev->dma_device.device_tx_status = st_fdma_tx_status;
825 fdev->dma_device.device_issue_pending = st_fdma_issue_pending;
826 fdev->dma_device.device_terminate_all = st_fdma_terminate_all;
827 fdev->dma_device.device_config = st_fdma_slave_config;
828 fdev->dma_device.device_pause = st_fdma_pause;
829 fdev->dma_device.device_resume = st_fdma_resume;
831 fdev->dma_device.src_addr_widths = FDMA_DMA_BUSWIDTHS;
832 fdev->dma_device.dst_addr_widths = FDMA_DMA_BUSWIDTHS;
833 fdev->dma_device.directions = BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV);
834 fdev->dma_device.residue_granularity = DMA_RESIDUE_GRANULARITY_BURST;
836 ret = dma_async_device_register(&fdev->dma_device);
837 if (ret) {
838 dev_err(&pdev->dev,
839 "Failed to register DMA device (%d)\n", ret);
840 goto err_rproc;
843 ret = of_dma_controller_register(np, st_fdma_of_xlate, fdev);
844 if (ret) {
845 dev_err(&pdev->dev,
846 "Failed to register controller (%d)\n", ret);
847 goto err_dma_dev;
850 dev_info(&pdev->dev, "ST FDMA engine driver, irq:%d\n", fdev->irq);
852 return 0;
854 err_dma_dev:
855 dma_async_device_unregister(&fdev->dma_device);
856 err_rproc:
857 st_fdma_free(fdev);
858 st_slim_rproc_put(fdev->slim_rproc);
859 err:
860 return ret;
863 static int st_fdma_remove(struct platform_device *pdev)
865 struct st_fdma_dev *fdev = platform_get_drvdata(pdev);
867 devm_free_irq(&pdev->dev, fdev->irq, fdev);
868 st_slim_rproc_put(fdev->slim_rproc);
869 of_dma_controller_free(pdev->dev.of_node);
870 dma_async_device_unregister(&fdev->dma_device);
872 return 0;
875 static struct platform_driver st_fdma_platform_driver = {
876 .driver = {
877 .name = DRIVER_NAME,
878 .of_match_table = st_fdma_match,
880 .probe = st_fdma_probe,
881 .remove = st_fdma_remove,
883 module_platform_driver(st_fdma_platform_driver);
885 MODULE_LICENSE("GPL v2");
886 MODULE_DESCRIPTION("STMicroelectronics FDMA engine driver");
887 MODULE_AUTHOR("Ludovic.barre <Ludovic.barre@st.com>");
888 MODULE_AUTHOR("Peter Griffin <peter.griffin@linaro.org>");
889 MODULE_ALIAS("platform: " DRIVER_NAME);