2 * Provide TDMA helper functions used by cipher and hash algorithm
5 * Author: Boris Brezillon <boris.brezillon@free-electrons.com>
6 * Author: Arnaud Ebalard <arno@natisbad.org>
8 * This work is based on an initial version written by
9 * Sebastian Andrzej Siewior < sebastian at breakpoint dot cc >
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License version 2 as published
13 * by the Free Software Foundation.
18 bool mv_cesa_req_dma_iter_next_transfer(struct mv_cesa_dma_iter
*iter
,
19 struct mv_cesa_sg_dma_iter
*sgiter
,
25 sgiter
->op_offset
+= len
;
26 sgiter
->offset
+= len
;
27 if (sgiter
->offset
== sg_dma_len(sgiter
->sg
)) {
28 if (sg_is_last(sgiter
->sg
))
31 sgiter
->sg
= sg_next(sgiter
->sg
);
34 if (sgiter
->op_offset
== iter
->op_len
)
40 void mv_cesa_dma_step(struct mv_cesa_tdma_req
*dreq
)
42 struct mv_cesa_engine
*engine
= dreq
->base
.engine
;
44 writel_relaxed(0, engine
->regs
+ CESA_SA_CFG
);
46 mv_cesa_set_int_mask(engine
, CESA_SA_INT_ACC0_IDMA_DONE
);
47 writel_relaxed(CESA_TDMA_DST_BURST_128B
| CESA_TDMA_SRC_BURST_128B
|
48 CESA_TDMA_NO_BYTE_SWAP
| CESA_TDMA_EN
,
49 engine
->regs
+ CESA_TDMA_CONTROL
);
51 writel_relaxed(CESA_SA_CFG_ACT_CH0_IDMA
| CESA_SA_CFG_MULTI_PKT
|
52 CESA_SA_CFG_CH0_W_IDMA
| CESA_SA_CFG_PARA_DIS
,
53 engine
->regs
+ CESA_SA_CFG
);
54 writel_relaxed(dreq
->chain
.first
->cur_dma
,
55 engine
->regs
+ CESA_TDMA_NEXT_ADDR
);
56 writel(CESA_SA_CMD_EN_CESA_SA_ACCL0
, engine
->regs
+ CESA_SA_CMD
);
59 void mv_cesa_dma_cleanup(struct mv_cesa_tdma_req
*dreq
)
61 struct mv_cesa_tdma_desc
*tdma
;
63 for (tdma
= dreq
->chain
.first
; tdma
;) {
64 struct mv_cesa_tdma_desc
*old_tdma
= tdma
;
66 if (tdma
->flags
& CESA_TDMA_OP
)
67 dma_pool_free(cesa_dev
->dma
->op_pool
, tdma
->op
,
68 le32_to_cpu(tdma
->src
));
71 dma_pool_free(cesa_dev
->dma
->tdma_desc_pool
, old_tdma
,
75 dreq
->chain
.first
= NULL
;
76 dreq
->chain
.last
= NULL
;
79 void mv_cesa_dma_prepare(struct mv_cesa_tdma_req
*dreq
,
80 struct mv_cesa_engine
*engine
)
82 struct mv_cesa_tdma_desc
*tdma
;
84 for (tdma
= dreq
->chain
.first
; tdma
; tdma
= tdma
->next
) {
85 if (tdma
->flags
& CESA_TDMA_DST_IN_SRAM
)
86 tdma
->dst
= cpu_to_le32(tdma
->dst
+ engine
->sram_dma
);
88 if (tdma
->flags
& CESA_TDMA_SRC_IN_SRAM
)
89 tdma
->src
= cpu_to_le32(tdma
->src
+ engine
->sram_dma
);
91 if (tdma
->flags
& CESA_TDMA_OP
)
92 mv_cesa_adjust_op(engine
, tdma
->op
);
96 static struct mv_cesa_tdma_desc
*
97 mv_cesa_dma_add_desc(struct mv_cesa_tdma_chain
*chain
, gfp_t flags
)
99 struct mv_cesa_tdma_desc
*new_tdma
= NULL
;
100 dma_addr_t dma_handle
;
102 new_tdma
= dma_pool_alloc(cesa_dev
->dma
->tdma_desc_pool
, flags
,
105 return ERR_PTR(-ENOMEM
);
107 memset(new_tdma
, 0, sizeof(*new_tdma
));
108 new_tdma
->cur_dma
= dma_handle
;
110 chain
->last
->next_dma
= cpu_to_le32(dma_handle
);
111 chain
->last
->next
= new_tdma
;
113 chain
->first
= new_tdma
;
116 chain
->last
= new_tdma
;
121 struct mv_cesa_op_ctx
*mv_cesa_dma_add_op(struct mv_cesa_tdma_chain
*chain
,
122 const struct mv_cesa_op_ctx
*op_templ
,
126 struct mv_cesa_tdma_desc
*tdma
;
127 struct mv_cesa_op_ctx
*op
;
128 dma_addr_t dma_handle
;
131 tdma
= mv_cesa_dma_add_desc(chain
, flags
);
133 return ERR_CAST(tdma
);
135 op
= dma_pool_alloc(cesa_dev
->dma
->op_pool
, flags
, &dma_handle
);
137 return ERR_PTR(-ENOMEM
);
141 size
= skip_ctx
? sizeof(op
->desc
) : sizeof(*op
);
145 tdma
->byte_cnt
= cpu_to_le32(size
| BIT(31));
146 tdma
->src
= cpu_to_le32(dma_handle
);
147 tdma
->flags
= CESA_TDMA_DST_IN_SRAM
| CESA_TDMA_OP
;
152 int mv_cesa_dma_add_data_transfer(struct mv_cesa_tdma_chain
*chain
,
153 dma_addr_t dst
, dma_addr_t src
, u32 size
,
154 u32 flags
, gfp_t gfp_flags
)
156 struct mv_cesa_tdma_desc
*tdma
;
158 tdma
= mv_cesa_dma_add_desc(chain
, gfp_flags
);
160 return PTR_ERR(tdma
);
162 tdma
->byte_cnt
= cpu_to_le32(size
| BIT(31));
166 flags
&= (CESA_TDMA_DST_IN_SRAM
| CESA_TDMA_SRC_IN_SRAM
);
167 tdma
->flags
= flags
| CESA_TDMA_DATA
;
172 int mv_cesa_dma_add_dummy_launch(struct mv_cesa_tdma_chain
*chain
, gfp_t flags
)
174 struct mv_cesa_tdma_desc
*tdma
;
176 tdma
= mv_cesa_dma_add_desc(chain
, flags
);
178 return PTR_ERR(tdma
);
183 int mv_cesa_dma_add_dummy_end(struct mv_cesa_tdma_chain
*chain
, gfp_t flags
)
185 struct mv_cesa_tdma_desc
*tdma
;
187 tdma
= mv_cesa_dma_add_desc(chain
, flags
);
189 return PTR_ERR(tdma
);
191 tdma
->byte_cnt
= cpu_to_le32(BIT(31));
196 int mv_cesa_dma_add_op_transfers(struct mv_cesa_tdma_chain
*chain
,
197 struct mv_cesa_dma_iter
*dma_iter
,
198 struct mv_cesa_sg_dma_iter
*sgiter
,
201 u32 flags
= sgiter
->dir
== DMA_TO_DEVICE
?
202 CESA_TDMA_DST_IN_SRAM
: CESA_TDMA_SRC_IN_SRAM
;
209 len
= mv_cesa_req_dma_iter_transfer_len(dma_iter
, sgiter
);
210 if (sgiter
->dir
== DMA_TO_DEVICE
) {
211 dst
= CESA_SA_DATA_SRAM_OFFSET
+ sgiter
->op_offset
;
212 src
= sg_dma_address(sgiter
->sg
) + sgiter
->offset
;
214 dst
= sg_dma_address(sgiter
->sg
) + sgiter
->offset
;
215 src
= CESA_SA_DATA_SRAM_OFFSET
+ sgiter
->op_offset
;
218 ret
= mv_cesa_dma_add_data_transfer(chain
, dst
, src
, len
,
223 } while (mv_cesa_req_dma_iter_next_transfer(dma_iter
, sgiter
, len
));