1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * DMA driver for STMicroelectronics STi FDMA controller
5 * Copyright (C) 2014 STMicroelectronics
7 * Author: Ludovic Barre <Ludovic.barre@st.com>
8 * Peter Griffin <peter.griffin@linaro.org>
11 #include <linux/init.h>
12 #include <linux/module.h>
14 #include <linux/of_dma.h>
15 #include <linux/platform_device.h>
16 #include <linux/property.h>
17 #include <linux/interrupt.h>
18 #include <linux/remoteproc.h>
19 #include <linux/slab.h>
23 static inline struct st_fdma_chan
*to_st_fdma_chan(struct dma_chan
*c
)
25 return container_of(c
, struct st_fdma_chan
, vchan
.chan
);
28 static struct st_fdma_desc
*to_st_fdma_desc(struct virt_dma_desc
*vd
)
30 return container_of(vd
, struct st_fdma_desc
, vdesc
);
33 static int st_fdma_dreq_get(struct st_fdma_chan
*fchan
)
35 struct st_fdma_dev
*fdev
= fchan
->fdev
;
36 u32 req_line_cfg
= fchan
->cfg
.req_line
;
41 * dreq_mask is shared for n channels of fdma, so all accesses must be
42 * atomic. if the dreq_mask is changed between ffz and set_bit,
46 if (fdev
->dreq_mask
== ~0L) {
47 dev_err(fdev
->dev
, "No req lines available\n");
51 if (try || req_line_cfg
>= ST_FDMA_NR_DREQS
) {
52 dev_err(fdev
->dev
, "Invalid or used req line\n");
55 dreq_line
= req_line_cfg
;
59 } while (test_and_set_bit(dreq_line
, &fdev
->dreq_mask
));
61 dev_dbg(fdev
->dev
, "get dreq_line:%d mask:%#lx\n",
62 dreq_line
, fdev
->dreq_mask
);
67 static void st_fdma_dreq_put(struct st_fdma_chan
*fchan
)
69 struct st_fdma_dev
*fdev
= fchan
->fdev
;
71 dev_dbg(fdev
->dev
, "put dreq_line:%#x\n", fchan
->dreq_line
);
72 clear_bit(fchan
->dreq_line
, &fdev
->dreq_mask
);
75 static void st_fdma_xfer_desc(struct st_fdma_chan
*fchan
)
77 struct virt_dma_desc
*vdesc
;
78 unsigned long nbytes
, ch_cmd
, cmd
;
80 vdesc
= vchan_next_desc(&fchan
->vchan
);
84 fchan
->fdesc
= to_st_fdma_desc(vdesc
);
85 nbytes
= fchan
->fdesc
->node
[0].desc
->nbytes
;
86 cmd
= FDMA_CMD_START(fchan
->vchan
.chan
.chan_id
);
87 ch_cmd
= fchan
->fdesc
->node
[0].pdesc
| FDMA_CH_CMD_STA_START
;
89 /* start the channel for the descriptor */
90 fnode_write(fchan
, nbytes
, FDMA_CNTN_OFST
);
91 fchan_write(fchan
, ch_cmd
, FDMA_CH_CMD_OFST
);
93 fchan
->fdev
->slim_rproc
->peri
+ FDMA_CMD_SET_OFST
);
95 dev_dbg(fchan
->fdev
->dev
, "start chan:%d\n", fchan
->vchan
.chan
.chan_id
);
98 static void st_fdma_ch_sta_update(struct st_fdma_chan
*fchan
,
99 unsigned long int_sta
)
101 unsigned long ch_sta
, ch_err
;
102 int ch_id
= fchan
->vchan
.chan
.chan_id
;
103 struct st_fdma_dev
*fdev
= fchan
->fdev
;
105 ch_sta
= fchan_read(fchan
, FDMA_CH_CMD_OFST
);
106 ch_err
= ch_sta
& FDMA_CH_CMD_ERR_MASK
;
107 ch_sta
&= FDMA_CH_CMD_STA_MASK
;
109 if (int_sta
& FDMA_INT_STA_ERR
) {
110 dev_warn(fdev
->dev
, "chan:%d, error:%ld\n", ch_id
, ch_err
);
111 fchan
->status
= DMA_ERROR
;
116 case FDMA_CH_CMD_STA_PAUSED
:
117 fchan
->status
= DMA_PAUSED
;
120 case FDMA_CH_CMD_STA_RUNNING
:
121 fchan
->status
= DMA_IN_PROGRESS
;
126 static irqreturn_t
st_fdma_irq_handler(int irq
, void *dev_id
)
128 struct st_fdma_dev
*fdev
= dev_id
;
129 irqreturn_t ret
= IRQ_NONE
;
130 struct st_fdma_chan
*fchan
= &fdev
->chans
[0];
131 unsigned long int_sta
, clr
;
133 int_sta
= fdma_read(fdev
, FDMA_INT_STA_OFST
);
136 for (; int_sta
!= 0 ; int_sta
>>= 2, fchan
++) {
137 if (!(int_sta
& (FDMA_INT_STA_CH
| FDMA_INT_STA_ERR
)))
140 spin_lock(&fchan
->vchan
.lock
);
141 st_fdma_ch_sta_update(fchan
, int_sta
);
144 if (!fchan
->fdesc
->iscyclic
) {
145 list_del(&fchan
->fdesc
->vdesc
.node
);
146 vchan_cookie_complete(&fchan
->fdesc
->vdesc
);
148 fchan
->status
= DMA_COMPLETE
;
150 vchan_cyclic_callback(&fchan
->fdesc
->vdesc
);
153 /* Start the next descriptor (if available) */
155 st_fdma_xfer_desc(fchan
);
158 spin_unlock(&fchan
->vchan
.lock
);
162 fdma_write(fdev
, clr
, FDMA_INT_CLR_OFST
);
167 static struct dma_chan
*st_fdma_of_xlate(struct of_phandle_args
*dma_spec
,
168 struct of_dma
*ofdma
)
170 struct st_fdma_dev
*fdev
= ofdma
->of_dma_data
;
171 struct dma_chan
*chan
;
172 struct st_fdma_chan
*fchan
;
175 if (dma_spec
->args_count
< 1)
176 return ERR_PTR(-EINVAL
);
178 if (fdev
->dma_device
.dev
->of_node
!= dma_spec
->np
)
179 return ERR_PTR(-EINVAL
);
181 ret
= rproc_boot(fdev
->slim_rproc
->rproc
);
183 return ERR_PTR(-EPROBE_DEFER
);
187 chan
= dma_get_any_slave_channel(&fdev
->dma_device
);
191 fchan
= to_st_fdma_chan(chan
);
193 fchan
->cfg
.of_node
= dma_spec
->np
;
194 fchan
->cfg
.req_line
= dma_spec
->args
[0];
195 fchan
->cfg
.req_ctrl
= 0;
196 fchan
->cfg
.type
= ST_FDMA_TYPE_FREE_RUN
;
198 if (dma_spec
->args_count
> 1)
199 fchan
->cfg
.req_ctrl
= dma_spec
->args
[1]
200 & FDMA_REQ_CTRL_CFG_MASK
;
202 if (dma_spec
->args_count
> 2)
203 fchan
->cfg
.type
= dma_spec
->args
[2];
205 if (fchan
->cfg
.type
== ST_FDMA_TYPE_FREE_RUN
) {
206 fchan
->dreq_line
= 0;
208 fchan
->dreq_line
= st_fdma_dreq_get(fchan
);
209 if (IS_ERR_VALUE(fchan
->dreq_line
)) {
210 chan
= ERR_PTR(fchan
->dreq_line
);
215 dev_dbg(fdev
->dev
, "xlate req_line:%d type:%d req_ctrl:%#lx\n",
216 fchan
->cfg
.req_line
, fchan
->cfg
.type
, fchan
->cfg
.req_ctrl
);
221 rproc_shutdown(fdev
->slim_rproc
->rproc
);
226 static void st_fdma_free_desc(struct virt_dma_desc
*vdesc
)
228 struct st_fdma_desc
*fdesc
;
231 fdesc
= to_st_fdma_desc(vdesc
);
232 for (i
= 0; i
< fdesc
->n_nodes
; i
++)
233 dma_pool_free(fdesc
->fchan
->node_pool
, fdesc
->node
[i
].desc
,
234 fdesc
->node
[i
].pdesc
);
238 static struct st_fdma_desc
*st_fdma_alloc_desc(struct st_fdma_chan
*fchan
,
241 struct st_fdma_desc
*fdesc
;
244 fdesc
= kzalloc(struct_size(fdesc
, node
, sg_len
), GFP_NOWAIT
);
248 fdesc
->fchan
= fchan
;
249 fdesc
->n_nodes
= sg_len
;
250 for (i
= 0; i
< sg_len
; i
++) {
251 fdesc
->node
[i
].desc
= dma_pool_alloc(fchan
->node_pool
,
252 GFP_NOWAIT
, &fdesc
->node
[i
].pdesc
);
253 if (!fdesc
->node
[i
].desc
)
260 dma_pool_free(fchan
->node_pool
, fdesc
->node
[i
].desc
,
261 fdesc
->node
[i
].pdesc
);
266 static int st_fdma_alloc_chan_res(struct dma_chan
*chan
)
268 struct st_fdma_chan
*fchan
= to_st_fdma_chan(chan
);
270 /* Create the dma pool for descriptor allocation */
271 fchan
->node_pool
= dma_pool_create(dev_name(&chan
->dev
->device
),
273 sizeof(struct st_fdma_hw_node
),
274 __alignof__(struct st_fdma_hw_node
),
277 if (!fchan
->node_pool
) {
278 dev_err(fchan
->fdev
->dev
, "unable to allocate desc pool\n");
282 dev_dbg(fchan
->fdev
->dev
, "alloc ch_id:%d type:%d\n",
283 fchan
->vchan
.chan
.chan_id
, fchan
->cfg
.type
);
288 static void st_fdma_free_chan_res(struct dma_chan
*chan
)
290 struct st_fdma_chan
*fchan
= to_st_fdma_chan(chan
);
291 struct rproc
*rproc
= fchan
->fdev
->slim_rproc
->rproc
;
294 dev_dbg(fchan
->fdev
->dev
, "%s: freeing chan:%d\n",
295 __func__
, fchan
->vchan
.chan
.chan_id
);
297 if (fchan
->cfg
.type
!= ST_FDMA_TYPE_FREE_RUN
)
298 st_fdma_dreq_put(fchan
);
300 spin_lock_irqsave(&fchan
->vchan
.lock
, flags
);
302 spin_unlock_irqrestore(&fchan
->vchan
.lock
, flags
);
304 dma_pool_destroy(fchan
->node_pool
);
305 fchan
->node_pool
= NULL
;
306 memset(&fchan
->cfg
, 0, sizeof(struct st_fdma_cfg
));
308 rproc_shutdown(rproc
);
311 static struct dma_async_tx_descriptor
*st_fdma_prep_dma_memcpy(
312 struct dma_chan
*chan
, dma_addr_t dst
, dma_addr_t src
,
313 size_t len
, unsigned long flags
)
315 struct st_fdma_chan
*fchan
;
316 struct st_fdma_desc
*fdesc
;
317 struct st_fdma_hw_node
*hw_node
;
322 fchan
= to_st_fdma_chan(chan
);
324 /* We only require a single descriptor */
325 fdesc
= st_fdma_alloc_desc(fchan
, 1);
327 dev_err(fchan
->fdev
->dev
, "no memory for desc\n");
331 hw_node
= fdesc
->node
[0].desc
;
333 hw_node
->control
= FDMA_NODE_CTRL_REQ_MAP_FREE_RUN
;
334 hw_node
->control
|= FDMA_NODE_CTRL_SRC_INCR
;
335 hw_node
->control
|= FDMA_NODE_CTRL_DST_INCR
;
336 hw_node
->control
|= FDMA_NODE_CTRL_INT_EON
;
337 hw_node
->nbytes
= len
;
338 hw_node
->saddr
= src
;
339 hw_node
->daddr
= dst
;
340 hw_node
->generic
.length
= len
;
341 hw_node
->generic
.sstride
= 0;
342 hw_node
->generic
.dstride
= 0;
344 return vchan_tx_prep(&fchan
->vchan
, &fdesc
->vdesc
, flags
);
347 static int config_reqctrl(struct st_fdma_chan
*fchan
,
348 enum dma_transfer_direction direction
)
350 u32 maxburst
= 0, addr
= 0;
351 enum dma_slave_buswidth width
;
352 int ch_id
= fchan
->vchan
.chan
.chan_id
;
353 struct st_fdma_dev
*fdev
= fchan
->fdev
;
358 fchan
->cfg
.req_ctrl
&= ~FDMA_REQ_CTRL_WNR
;
359 maxburst
= fchan
->scfg
.src_maxburst
;
360 width
= fchan
->scfg
.src_addr_width
;
361 addr
= fchan
->scfg
.src_addr
;
365 fchan
->cfg
.req_ctrl
|= FDMA_REQ_CTRL_WNR
;
366 maxburst
= fchan
->scfg
.dst_maxburst
;
367 width
= fchan
->scfg
.dst_addr_width
;
368 addr
= fchan
->scfg
.dst_addr
;
375 fchan
->cfg
.req_ctrl
&= ~FDMA_REQ_CTRL_OPCODE_MASK
;
379 case DMA_SLAVE_BUSWIDTH_1_BYTE
:
380 fchan
->cfg
.req_ctrl
|= FDMA_REQ_CTRL_OPCODE_LD_ST1
;
383 case DMA_SLAVE_BUSWIDTH_2_BYTES
:
384 fchan
->cfg
.req_ctrl
|= FDMA_REQ_CTRL_OPCODE_LD_ST2
;
387 case DMA_SLAVE_BUSWIDTH_4_BYTES
:
388 fchan
->cfg
.req_ctrl
|= FDMA_REQ_CTRL_OPCODE_LD_ST4
;
391 case DMA_SLAVE_BUSWIDTH_8_BYTES
:
392 fchan
->cfg
.req_ctrl
|= FDMA_REQ_CTRL_OPCODE_LD_ST8
;
399 fchan
->cfg
.req_ctrl
&= ~FDMA_REQ_CTRL_NUM_OPS_MASK
;
400 fchan
->cfg
.req_ctrl
|= FDMA_REQ_CTRL_NUM_OPS(maxburst
-1);
401 dreq_write(fchan
, fchan
->cfg
.req_ctrl
, FDMA_REQ_CTRL_OFST
);
403 fchan
->cfg
.dev_addr
= addr
;
404 fchan
->cfg
.dir
= direction
;
406 dev_dbg(fdev
->dev
, "chan:%d config_reqctrl:%#x req_ctrl:%#lx\n",
407 ch_id
, addr
, fchan
->cfg
.req_ctrl
);
412 static void fill_hw_node(struct st_fdma_hw_node
*hw_node
,
413 struct st_fdma_chan
*fchan
,
414 enum dma_transfer_direction direction
)
416 if (direction
== DMA_MEM_TO_DEV
) {
417 hw_node
->control
|= FDMA_NODE_CTRL_SRC_INCR
;
418 hw_node
->control
|= FDMA_NODE_CTRL_DST_STATIC
;
419 hw_node
->daddr
= fchan
->cfg
.dev_addr
;
421 hw_node
->control
|= FDMA_NODE_CTRL_SRC_STATIC
;
422 hw_node
->control
|= FDMA_NODE_CTRL_DST_INCR
;
423 hw_node
->saddr
= fchan
->cfg
.dev_addr
;
426 hw_node
->generic
.sstride
= 0;
427 hw_node
->generic
.dstride
= 0;
430 static inline struct st_fdma_chan
*st_fdma_prep_common(struct dma_chan
*chan
,
431 size_t len
, enum dma_transfer_direction direction
)
433 struct st_fdma_chan
*fchan
;
438 fchan
= to_st_fdma_chan(chan
);
440 if (!is_slave_direction(direction
)) {
441 dev_err(fchan
->fdev
->dev
, "bad direction?\n");
448 static struct dma_async_tx_descriptor
*st_fdma_prep_dma_cyclic(
449 struct dma_chan
*chan
, dma_addr_t buf_addr
, size_t len
,
450 size_t period_len
, enum dma_transfer_direction direction
,
453 struct st_fdma_chan
*fchan
;
454 struct st_fdma_desc
*fdesc
;
457 fchan
= st_fdma_prep_common(chan
, len
, direction
);
464 if (config_reqctrl(fchan
, direction
)) {
465 dev_err(fchan
->fdev
->dev
, "bad width or direction\n");
469 /* the buffer length must be a multiple of period_len */
470 if (len
% period_len
!= 0) {
471 dev_err(fchan
->fdev
->dev
, "len is not multiple of period\n");
475 sg_len
= len
/ period_len
;
476 fdesc
= st_fdma_alloc_desc(fchan
, sg_len
);
478 dev_err(fchan
->fdev
->dev
, "no memory for desc\n");
482 fdesc
->iscyclic
= true;
484 for (i
= 0; i
< sg_len
; i
++) {
485 struct st_fdma_hw_node
*hw_node
= fdesc
->node
[i
].desc
;
487 hw_node
->next
= fdesc
->node
[(i
+ 1) % sg_len
].pdesc
;
490 FDMA_NODE_CTRL_REQ_MAP_DREQ(fchan
->dreq_line
);
491 hw_node
->control
|= FDMA_NODE_CTRL_INT_EON
;
493 fill_hw_node(hw_node
, fchan
, direction
);
495 if (direction
== DMA_MEM_TO_DEV
)
496 hw_node
->saddr
= buf_addr
+ (i
* period_len
);
498 hw_node
->daddr
= buf_addr
+ (i
* period_len
);
500 hw_node
->nbytes
= period_len
;
501 hw_node
->generic
.length
= period_len
;
504 return vchan_tx_prep(&fchan
->vchan
, &fdesc
->vdesc
, flags
);
507 static struct dma_async_tx_descriptor
*st_fdma_prep_slave_sg(
508 struct dma_chan
*chan
, struct scatterlist
*sgl
,
509 unsigned int sg_len
, enum dma_transfer_direction direction
,
510 unsigned long flags
, void *context
)
512 struct st_fdma_chan
*fchan
;
513 struct st_fdma_desc
*fdesc
;
514 struct st_fdma_hw_node
*hw_node
;
515 struct scatterlist
*sg
;
518 fchan
= st_fdma_prep_common(chan
, sg_len
, direction
);
525 fdesc
= st_fdma_alloc_desc(fchan
, sg_len
);
527 dev_err(fchan
->fdev
->dev
, "no memory for desc\n");
531 fdesc
->iscyclic
= false;
533 for_each_sg(sgl
, sg
, sg_len
, i
) {
534 hw_node
= fdesc
->node
[i
].desc
;
536 hw_node
->next
= fdesc
->node
[(i
+ 1) % sg_len
].pdesc
;
537 hw_node
->control
= FDMA_NODE_CTRL_REQ_MAP_DREQ(fchan
->dreq_line
);
539 fill_hw_node(hw_node
, fchan
, direction
);
541 if (direction
== DMA_MEM_TO_DEV
)
542 hw_node
->saddr
= sg_dma_address(sg
);
544 hw_node
->daddr
= sg_dma_address(sg
);
546 hw_node
->nbytes
= sg_dma_len(sg
);
547 hw_node
->generic
.length
= sg_dma_len(sg
);
550 /* interrupt at end of last node */
551 hw_node
->control
|= FDMA_NODE_CTRL_INT_EON
;
553 return vchan_tx_prep(&fchan
->vchan
, &fdesc
->vdesc
, flags
);
556 static size_t st_fdma_desc_residue(struct st_fdma_chan
*fchan
,
557 struct virt_dma_desc
*vdesc
,
560 struct st_fdma_desc
*fdesc
= fchan
->fdesc
;
562 dma_addr_t cur_addr
= 0;
566 cur_addr
= fchan_read(fchan
, FDMA_CH_CMD_OFST
);
567 cur_addr
&= FDMA_CH_CMD_DATA_MASK
;
570 for (i
= fchan
->fdesc
->n_nodes
- 1 ; i
>= 0; i
--) {
571 if (cur_addr
== fdesc
->node
[i
].pdesc
) {
572 residue
+= fnode_read(fchan
, FDMA_CNTN_OFST
);
575 residue
+= fdesc
->node
[i
].desc
->nbytes
;
581 static enum dma_status
st_fdma_tx_status(struct dma_chan
*chan
,
583 struct dma_tx_state
*txstate
)
585 struct st_fdma_chan
*fchan
= to_st_fdma_chan(chan
);
586 struct virt_dma_desc
*vd
;
590 ret
= dma_cookie_status(chan
, cookie
, txstate
);
591 if (ret
== DMA_COMPLETE
|| !txstate
)
594 spin_lock_irqsave(&fchan
->vchan
.lock
, flags
);
595 vd
= vchan_find_desc(&fchan
->vchan
, cookie
);
596 if (fchan
->fdesc
&& cookie
== fchan
->fdesc
->vdesc
.tx
.cookie
)
597 txstate
->residue
= st_fdma_desc_residue(fchan
, vd
, true);
599 txstate
->residue
= st_fdma_desc_residue(fchan
, vd
, false);
601 txstate
->residue
= 0;
603 spin_unlock_irqrestore(&fchan
->vchan
.lock
, flags
);
608 static void st_fdma_issue_pending(struct dma_chan
*chan
)
610 struct st_fdma_chan
*fchan
= to_st_fdma_chan(chan
);
613 spin_lock_irqsave(&fchan
->vchan
.lock
, flags
);
615 if (vchan_issue_pending(&fchan
->vchan
) && !fchan
->fdesc
)
616 st_fdma_xfer_desc(fchan
);
618 spin_unlock_irqrestore(&fchan
->vchan
.lock
, flags
);
621 static int st_fdma_pause(struct dma_chan
*chan
)
624 struct st_fdma_chan
*fchan
= to_st_fdma_chan(chan
);
625 int ch_id
= fchan
->vchan
.chan
.chan_id
;
626 unsigned long cmd
= FDMA_CMD_PAUSE(ch_id
);
628 dev_dbg(fchan
->fdev
->dev
, "pause chan:%d\n", ch_id
);
630 spin_lock_irqsave(&fchan
->vchan
.lock
, flags
);
632 fdma_write(fchan
->fdev
, cmd
, FDMA_CMD_SET_OFST
);
633 spin_unlock_irqrestore(&fchan
->vchan
.lock
, flags
);
638 static int st_fdma_resume(struct dma_chan
*chan
)
642 struct st_fdma_chan
*fchan
= to_st_fdma_chan(chan
);
643 int ch_id
= fchan
->vchan
.chan
.chan_id
;
645 dev_dbg(fchan
->fdev
->dev
, "resume chan:%d\n", ch_id
);
647 spin_lock_irqsave(&fchan
->vchan
.lock
, flags
);
649 val
= fchan_read(fchan
, FDMA_CH_CMD_OFST
);
650 val
&= FDMA_CH_CMD_DATA_MASK
;
651 fchan_write(fchan
, val
, FDMA_CH_CMD_OFST
);
653 spin_unlock_irqrestore(&fchan
->vchan
.lock
, flags
);
658 static int st_fdma_terminate_all(struct dma_chan
*chan
)
662 struct st_fdma_chan
*fchan
= to_st_fdma_chan(chan
);
663 int ch_id
= fchan
->vchan
.chan
.chan_id
;
664 unsigned long cmd
= FDMA_CMD_PAUSE(ch_id
);
666 dev_dbg(fchan
->fdev
->dev
, "terminate chan:%d\n", ch_id
);
668 spin_lock_irqsave(&fchan
->vchan
.lock
, flags
);
669 fdma_write(fchan
->fdev
, cmd
, FDMA_CMD_SET_OFST
);
671 vchan_get_all_descriptors(&fchan
->vchan
, &head
);
672 spin_unlock_irqrestore(&fchan
->vchan
.lock
, flags
);
673 vchan_dma_desc_free_list(&fchan
->vchan
, &head
);
678 static int st_fdma_slave_config(struct dma_chan
*chan
,
679 struct dma_slave_config
*slave_cfg
)
681 struct st_fdma_chan
*fchan
= to_st_fdma_chan(chan
);
683 memcpy(&fchan
->scfg
, slave_cfg
, sizeof(fchan
->scfg
));
687 static const struct st_fdma_driverdata fdma_mpe31_stih407_11
= {
692 static const struct st_fdma_driverdata fdma_mpe31_stih407_12
= {
697 static const struct st_fdma_driverdata fdma_mpe31_stih407_13
= {
702 static const struct of_device_id st_fdma_match
[] = {
703 { .compatible
= "st,stih407-fdma-mpe31-11"
704 , .data
= &fdma_mpe31_stih407_11
},
705 { .compatible
= "st,stih407-fdma-mpe31-12"
706 , .data
= &fdma_mpe31_stih407_12
},
707 { .compatible
= "st,stih407-fdma-mpe31-13"
708 , .data
= &fdma_mpe31_stih407_13
},
711 MODULE_DEVICE_TABLE(of
, st_fdma_match
);
713 static int st_fdma_parse_dt(struct platform_device
*pdev
,
714 const struct st_fdma_driverdata
*drvdata
,
715 struct st_fdma_dev
*fdev
)
717 snprintf(fdev
->fw_name
, FW_NAME_SIZE
, "fdma_%s_%d.elf",
718 drvdata
->name
, drvdata
->id
);
720 return of_property_read_u32(pdev
->dev
.of_node
, "dma-channels",
723 #define FDMA_DMA_BUSWIDTHS (BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) | \
724 BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) | \
725 BIT(DMA_SLAVE_BUSWIDTH_3_BYTES) | \
726 BIT(DMA_SLAVE_BUSWIDTH_4_BYTES))
728 static void st_fdma_free(struct st_fdma_dev
*fdev
)
730 struct st_fdma_chan
*fchan
;
733 for (i
= 0; i
< fdev
->nr_channels
; i
++) {
734 fchan
= &fdev
->chans
[i
];
735 list_del(&fchan
->vchan
.chan
.device_node
);
736 tasklet_kill(&fchan
->vchan
.task
);
740 static int st_fdma_probe(struct platform_device
*pdev
)
742 struct st_fdma_dev
*fdev
;
743 struct device_node
*np
= pdev
->dev
.of_node
;
744 const struct st_fdma_driverdata
*drvdata
;
747 drvdata
= device_get_match_data(&pdev
->dev
);
749 fdev
= devm_kzalloc(&pdev
->dev
, sizeof(*fdev
), GFP_KERNEL
);
753 ret
= st_fdma_parse_dt(pdev
, drvdata
, fdev
);
755 dev_err(&pdev
->dev
, "unable to find platform data\n");
759 fdev
->chans
= devm_kcalloc(&pdev
->dev
, fdev
->nr_channels
,
760 sizeof(struct st_fdma_chan
), GFP_KERNEL
);
764 fdev
->dev
= &pdev
->dev
;
765 fdev
->drvdata
= drvdata
;
766 platform_set_drvdata(pdev
, fdev
);
768 fdev
->irq
= platform_get_irq(pdev
, 0);
772 ret
= devm_request_irq(&pdev
->dev
, fdev
->irq
, st_fdma_irq_handler
, 0,
773 dev_name(&pdev
->dev
), fdev
);
775 dev_err(&pdev
->dev
, "Failed to request irq (%d)\n", ret
);
779 fdev
->slim_rproc
= st_slim_rproc_alloc(pdev
, fdev
->fw_name
);
780 if (IS_ERR(fdev
->slim_rproc
)) {
781 ret
= PTR_ERR(fdev
->slim_rproc
);
782 dev_err(&pdev
->dev
, "slim_rproc_alloc failed (%d)\n", ret
);
786 /* Initialise list of FDMA channels */
787 INIT_LIST_HEAD(&fdev
->dma_device
.channels
);
788 for (i
= 0; i
< fdev
->nr_channels
; i
++) {
789 struct st_fdma_chan
*fchan
= &fdev
->chans
[i
];
792 fchan
->vchan
.desc_free
= st_fdma_free_desc
;
793 vchan_init(&fchan
->vchan
, &fdev
->dma_device
);
796 /* Initialise the FDMA dreq (reserve 0 & 31 for FDMA use) */
797 fdev
->dreq_mask
= BIT(0) | BIT(31);
799 dma_cap_set(DMA_SLAVE
, fdev
->dma_device
.cap_mask
);
800 dma_cap_set(DMA_CYCLIC
, fdev
->dma_device
.cap_mask
);
801 dma_cap_set(DMA_MEMCPY
, fdev
->dma_device
.cap_mask
);
803 fdev
->dma_device
.dev
= &pdev
->dev
;
804 fdev
->dma_device
.device_alloc_chan_resources
= st_fdma_alloc_chan_res
;
805 fdev
->dma_device
.device_free_chan_resources
= st_fdma_free_chan_res
;
806 fdev
->dma_device
.device_prep_dma_cyclic
= st_fdma_prep_dma_cyclic
;
807 fdev
->dma_device
.device_prep_slave_sg
= st_fdma_prep_slave_sg
;
808 fdev
->dma_device
.device_prep_dma_memcpy
= st_fdma_prep_dma_memcpy
;
809 fdev
->dma_device
.device_tx_status
= st_fdma_tx_status
;
810 fdev
->dma_device
.device_issue_pending
= st_fdma_issue_pending
;
811 fdev
->dma_device
.device_terminate_all
= st_fdma_terminate_all
;
812 fdev
->dma_device
.device_config
= st_fdma_slave_config
;
813 fdev
->dma_device
.device_pause
= st_fdma_pause
;
814 fdev
->dma_device
.device_resume
= st_fdma_resume
;
816 fdev
->dma_device
.src_addr_widths
= FDMA_DMA_BUSWIDTHS
;
817 fdev
->dma_device
.dst_addr_widths
= FDMA_DMA_BUSWIDTHS
;
818 fdev
->dma_device
.directions
= BIT(DMA_DEV_TO_MEM
) | BIT(DMA_MEM_TO_DEV
);
819 fdev
->dma_device
.residue_granularity
= DMA_RESIDUE_GRANULARITY_BURST
;
821 ret
= dmaenginem_async_device_register(&fdev
->dma_device
);
824 "Failed to register DMA device (%d)\n", ret
);
828 ret
= of_dma_controller_register(np
, st_fdma_of_xlate
, fdev
);
831 "Failed to register controller (%d)\n", ret
);
835 dev_info(&pdev
->dev
, "ST FDMA engine driver, irq:%d\n", fdev
->irq
);
841 st_slim_rproc_put(fdev
->slim_rproc
);
846 static void st_fdma_remove(struct platform_device
*pdev
)
848 struct st_fdma_dev
*fdev
= platform_get_drvdata(pdev
);
850 devm_free_irq(&pdev
->dev
, fdev
->irq
, fdev
);
851 st_slim_rproc_put(fdev
->slim_rproc
);
852 of_dma_controller_free(pdev
->dev
.of_node
);
855 static struct platform_driver st_fdma_platform_driver
= {
858 .of_match_table
= st_fdma_match
,
860 .probe
= st_fdma_probe
,
861 .remove
= st_fdma_remove
,
863 module_platform_driver(st_fdma_platform_driver
);
865 MODULE_LICENSE("GPL v2");
866 MODULE_DESCRIPTION("STMicroelectronics FDMA engine driver");
867 MODULE_AUTHOR("Ludovic.barre <Ludovic.barre@st.com>");
868 MODULE_AUTHOR("Peter Griffin <peter.griffin@linaro.org>");
869 MODULE_ALIAS("platform:" DRIVER_NAME
);