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>
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
;
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,
48 if (fdev
->dreq_mask
== ~0L) {
49 dev_err(fdev
->dev
, "No req lines available\n");
53 if (try || req_line_cfg
>= ST_FDMA_NR_DREQS
) {
54 dev_err(fdev
->dev
, "Invalid or used req line\n");
57 dreq_line
= req_line_cfg
;
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
);
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
);
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
);
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
;
118 case FDMA_CH_CMD_STA_PAUSED
:
119 fchan
->status
= DMA_PAUSED
;
122 case FDMA_CH_CMD_STA_RUNNING
:
123 fchan
->status
= DMA_IN_PROGRESS
;
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
);
138 for (; int_sta
!= 0 ; int_sta
>>= 2, fchan
++) {
139 if (!(int_sta
& (FDMA_INT_STA_CH
| FDMA_INT_STA_ERR
)))
142 spin_lock(&fchan
->vchan
.lock
);
143 st_fdma_ch_sta_update(fchan
, int_sta
);
146 if (!fchan
->fdesc
->iscyclic
) {
147 list_del(&fchan
->fdesc
->vdesc
.node
);
148 vchan_cookie_complete(&fchan
->fdesc
->vdesc
);
150 fchan
->status
= DMA_COMPLETE
;
152 vchan_cyclic_callback(&fchan
->fdesc
->vdesc
);
155 /* Start the next descriptor (if available) */
157 st_fdma_xfer_desc(fchan
);
160 spin_unlock(&fchan
->vchan
.lock
);
164 fdma_write(fdev
, clr
, FDMA_INT_CLR_OFST
);
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
;
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
);
185 return ERR_PTR(-EPROBE_DEFER
);
189 chan
= dma_get_any_slave_channel(&fdev
->dma_device
);
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;
210 fchan
->dreq_line
= st_fdma_dreq_get(fchan
);
211 if (IS_ERR_VALUE(fchan
->dreq_line
)) {
212 chan
= ERR_PTR(fchan
->dreq_line
);
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
);
223 rproc_shutdown(fdev
->slim_rproc
->rproc
);
228 static void st_fdma_free_desc(struct virt_dma_desc
*vdesc
)
230 struct st_fdma_desc
*fdesc
;
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
);
240 static struct st_fdma_desc
*st_fdma_alloc_desc(struct st_fdma_chan
*fchan
,
243 struct st_fdma_desc
*fdesc
;
246 fdesc
= kzalloc(sizeof(*fdesc
) +
247 sizeof(struct st_fdma_sw_node
) * sg_len
, GFP_NOWAIT
);
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
)
263 dma_pool_free(fchan
->node_pool
, fdesc
->node
[i
].desc
,
264 fdesc
->node
[i
].pdesc
);
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
),
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");
285 dev_dbg(fchan
->fdev
->dev
, "alloc ch_id:%d type:%d\n",
286 fchan
->vchan
.chan
.chan_id
, fchan
->cfg
.type
);
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
;
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
);
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
;
327 fchan
= to_st_fdma_chan(chan
);
329 /* We only require a single descriptor */
330 fdesc
= st_fdma_alloc_desc(fchan
, 1);
332 dev_err(fchan
->fdev
->dev
, "no memory for desc\n");
336 hw_node
= fdesc
->node
[0].desc
;
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
;
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
;
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
;
380 fchan
->cfg
.req_ctrl
&= ~FDMA_REQ_CTRL_OPCODE_MASK
;
384 case DMA_SLAVE_BUSWIDTH_1_BYTE
:
385 fchan
->cfg
.req_ctrl
|= FDMA_REQ_CTRL_OPCODE_LD_ST1
;
388 case DMA_SLAVE_BUSWIDTH_2_BYTES
:
389 fchan
->cfg
.req_ctrl
|= FDMA_REQ_CTRL_OPCODE_LD_ST2
;
392 case DMA_SLAVE_BUSWIDTH_4_BYTES
:
393 fchan
->cfg
.req_ctrl
|= FDMA_REQ_CTRL_OPCODE_LD_ST4
;
396 case DMA_SLAVE_BUSWIDTH_8_BYTES
:
397 fchan
->cfg
.req_ctrl
|= FDMA_REQ_CTRL_OPCODE_LD_ST8
;
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
);
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
;
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
;
443 fchan
= to_st_fdma_chan(chan
);
445 if (!is_slave_direction(direction
)) {
446 dev_err(fchan
->fdev
->dev
, "bad direction?\n");
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
,
458 struct st_fdma_chan
*fchan
;
459 struct st_fdma_desc
*fdesc
;
462 fchan
= st_fdma_prep_common(chan
, len
, direction
);
469 if (config_reqctrl(fchan
, direction
)) {
470 dev_err(fchan
->fdev
->dev
, "bad width or direction\n");
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");
480 sg_len
= len
/ period_len
;
481 fdesc
= st_fdma_alloc_desc(fchan
, sg_len
);
483 dev_err(fchan
->fdev
->dev
, "no memory for desc\n");
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
;
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
);
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
;
523 fchan
= st_fdma_prep_common(chan
, sg_len
, direction
);
530 fdesc
= st_fdma_alloc_desc(fchan
, sg_len
);
532 dev_err(fchan
->fdev
->dev
, "no memory for desc\n");
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
);
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
,
565 struct st_fdma_desc
*fdesc
= fchan
->fdesc
;
567 dma_addr_t cur_addr
= 0;
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
);
580 residue
+= fdesc
->node
[i
].desc
->nbytes
;
586 static enum dma_status
st_fdma_tx_status(struct dma_chan
*chan
,
588 struct dma_tx_state
*txstate
)
590 struct st_fdma_chan
*fchan
= to_st_fdma_chan(chan
);
591 struct virt_dma_desc
*vd
;
595 ret
= dma_cookie_status(chan
, cookie
, txstate
);
596 if (ret
== DMA_COMPLETE
|| !txstate
)
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);
604 txstate
->residue
= st_fdma_desc_residue(fchan
, vd
, false);
606 txstate
->residue
= 0;
608 spin_unlock_irqrestore(&fchan
->vchan
.lock
, flags
);
613 static void st_fdma_issue_pending(struct dma_chan
*chan
)
615 struct st_fdma_chan
*fchan
= to_st_fdma_chan(chan
);
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
)
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
);
638 fdma_write(fchan
->fdev
, cmd
, FDMA_CMD_SET_OFST
);
639 spin_unlock_irqrestore(&fchan
->vchan
.lock
, flags
);
644 static int st_fdma_resume(struct dma_chan
*chan
)
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
);
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
);
664 static int st_fdma_terminate_all(struct dma_chan
*chan
)
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
);
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
);
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
));
693 static const struct st_fdma_driverdata fdma_mpe31_stih407_11
= {
698 static const struct st_fdma_driverdata fdma_mpe31_stih407_12
= {
703 static const struct st_fdma_driverdata fdma_mpe31_stih407_13
= {
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",
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
;
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
;
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");
760 drvdata
= match
->data
;
762 fdev
= devm_kzalloc(&pdev
->dev
, sizeof(*fdev
), GFP_KERNEL
);
766 ret
= st_fdma_parse_dt(pdev
, drvdata
, fdev
);
768 dev_err(&pdev
->dev
, "unable to find platform data\n");
772 fdev
->chans
= devm_kcalloc(&pdev
->dev
, fdev
->nr_channels
,
773 sizeof(struct st_fdma_chan
), GFP_KERNEL
);
777 fdev
->dev
= &pdev
->dev
;
778 fdev
->drvdata
= drvdata
;
779 platform_set_drvdata(pdev
, fdev
);
781 fdev
->irq
= platform_get_irq(pdev
, 0);
783 dev_err(&pdev
->dev
, "Failed to get irq resource\n");
787 ret
= devm_request_irq(&pdev
->dev
, fdev
->irq
, st_fdma_irq_handler
, 0,
788 dev_name(&pdev
->dev
), fdev
);
790 dev_err(&pdev
->dev
, "Failed to request irq (%d)\n", ret
);
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
);
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
];
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
);
839 "Failed to register DMA device (%d)\n", ret
);
843 ret
= of_dma_controller_register(np
, st_fdma_of_xlate
, fdev
);
846 "Failed to register controller (%d)\n", ret
);
850 dev_info(&pdev
->dev
, "ST FDMA engine driver, irq:%d\n", fdev
->irq
);
855 dma_async_device_unregister(&fdev
->dma_device
);
858 st_slim_rproc_put(fdev
->slim_rproc
);
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
);
875 static struct platform_driver st_fdma_platform_driver
= {
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
);