2 * Support for Marvell's Cryptographic Engine and Security Accelerator (CESA)
3 * that can be found on the following platform: Orion, Kirkwood, Armada. This
4 * driver supports the TDMA engine on platforms on which it is available.
6 * Author: Boris Brezillon <boris.brezillon@free-electrons.com>
7 * Author: Arnaud Ebalard <arno@natisbad.org>
9 * This work is based on an initial version written by
10 * Sebastian Andrzej Siewior < sebastian at breakpoint dot cc >
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License version 2 as published
14 * by the Free Software Foundation.
17 #include <linux/delay.h>
18 #include <linux/genalloc.h>
19 #include <linux/interrupt.h>
21 #include <linux/kthread.h>
22 #include <linux/mbus.h>
23 #include <linux/platform_device.h>
24 #include <linux/scatterlist.h>
25 #include <linux/slab.h>
26 #include <linux/module.h>
27 #include <linux/clk.h>
29 #include <linux/of_platform.h>
30 #include <linux/of_irq.h>
34 struct mv_cesa_dev
*cesa_dev
;
36 static void mv_cesa_dequeue_req_unlocked(struct mv_cesa_engine
*engine
)
38 struct crypto_async_request
*req
, *backlog
;
39 struct mv_cesa_ctx
*ctx
;
41 spin_lock_bh(&cesa_dev
->lock
);
42 backlog
= crypto_get_backlog(&cesa_dev
->queue
);
43 req
= crypto_dequeue_request(&cesa_dev
->queue
);
45 spin_unlock_bh(&cesa_dev
->lock
);
51 backlog
->complete(backlog
, -EINPROGRESS
);
53 ctx
= crypto_tfm_ctx(req
->tfm
);
54 ctx
->ops
->prepare(req
, engine
);
58 static irqreturn_t
mv_cesa_int(int irq
, void *priv
)
60 struct mv_cesa_engine
*engine
= priv
;
61 struct crypto_async_request
*req
;
62 struct mv_cesa_ctx
*ctx
;
64 irqreturn_t ret
= IRQ_NONE
;
69 mask
= mv_cesa_get_int_mask(engine
);
70 status
= readl(engine
->regs
+ CESA_SA_INT_STATUS
);
76 * TODO: avoid clearing the FPGA_INT_STATUS if this not
77 * relevant on some platforms.
79 writel(~status
, engine
->regs
+ CESA_SA_FPGA_INT_STATUS
);
80 writel(~status
, engine
->regs
+ CESA_SA_INT_STATUS
);
83 spin_lock_bh(&engine
->lock
);
85 spin_unlock_bh(&engine
->lock
);
87 ctx
= crypto_tfm_ctx(req
->tfm
);
88 res
= ctx
->ops
->process(req
, status
& mask
);
89 if (res
!= -EINPROGRESS
) {
90 spin_lock_bh(&engine
->lock
);
92 mv_cesa_dequeue_req_unlocked(engine
);
93 spin_unlock_bh(&engine
->lock
);
94 ctx
->ops
->cleanup(req
);
96 req
->complete(req
, res
);
107 int mv_cesa_queue_req(struct crypto_async_request
*req
)
112 spin_lock_bh(&cesa_dev
->lock
);
113 ret
= crypto_enqueue_request(&cesa_dev
->queue
, req
);
114 spin_unlock_bh(&cesa_dev
->lock
);
116 if (ret
!= -EINPROGRESS
)
119 for (i
= 0; i
< cesa_dev
->caps
->nengines
; i
++) {
120 spin_lock_bh(&cesa_dev
->engines
[i
].lock
);
121 if (!cesa_dev
->engines
[i
].req
)
122 mv_cesa_dequeue_req_unlocked(&cesa_dev
->engines
[i
]);
123 spin_unlock_bh(&cesa_dev
->engines
[i
].lock
);
129 static int mv_cesa_add_algs(struct mv_cesa_dev
*cesa
)
134 for (i
= 0; i
< cesa
->caps
->ncipher_algs
; i
++) {
135 ret
= crypto_register_alg(cesa
->caps
->cipher_algs
[i
]);
137 goto err_unregister_crypto
;
140 for (i
= 0; i
< cesa
->caps
->nahash_algs
; i
++) {
141 ret
= crypto_register_ahash(cesa
->caps
->ahash_algs
[i
]);
143 goto err_unregister_ahash
;
148 err_unregister_ahash
:
149 for (j
= 0; j
< i
; j
++)
150 crypto_unregister_ahash(cesa
->caps
->ahash_algs
[j
]);
151 i
= cesa
->caps
->ncipher_algs
;
153 err_unregister_crypto
:
154 for (j
= 0; j
< i
; j
++)
155 crypto_unregister_alg(cesa
->caps
->cipher_algs
[j
]);
160 static void mv_cesa_remove_algs(struct mv_cesa_dev
*cesa
)
164 for (i
= 0; i
< cesa
->caps
->nahash_algs
; i
++)
165 crypto_unregister_ahash(cesa
->caps
->ahash_algs
[i
]);
167 for (i
= 0; i
< cesa
->caps
->ncipher_algs
; i
++)
168 crypto_unregister_alg(cesa
->caps
->cipher_algs
[i
]);
171 static struct crypto_alg
*armada_370_cipher_algs
[] = {
172 &mv_cesa_ecb_des_alg
,
173 &mv_cesa_cbc_des_alg
,
174 &mv_cesa_ecb_aes_alg
,
175 &mv_cesa_cbc_aes_alg
,
178 static struct ahash_alg
*armada_370_ahash_algs
[] = {
183 static const struct mv_cesa_caps armada_370_caps
= {
185 .cipher_algs
= armada_370_cipher_algs
,
186 .ncipher_algs
= ARRAY_SIZE(armada_370_cipher_algs
),
187 .ahash_algs
= armada_370_ahash_algs
,
188 .nahash_algs
= ARRAY_SIZE(armada_370_ahash_algs
),
192 static const struct of_device_id mv_cesa_of_match_table
[] = {
193 { .compatible
= "marvell,armada-370-crypto", .data
= &armada_370_caps
},
196 MODULE_DEVICE_TABLE(of
, mv_cesa_of_match_table
);
199 mv_cesa_conf_mbus_windows(struct mv_cesa_engine
*engine
,
200 const struct mbus_dram_target_info
*dram
)
202 void __iomem
*iobase
= engine
->regs
;
205 for (i
= 0; i
< 4; i
++) {
206 writel(0, iobase
+ CESA_TDMA_WINDOW_CTRL(i
));
207 writel(0, iobase
+ CESA_TDMA_WINDOW_BASE(i
));
210 for (i
= 0; i
< dram
->num_cs
; i
++) {
211 const struct mbus_dram_window
*cs
= dram
->cs
+ i
;
213 writel(((cs
->size
- 1) & 0xffff0000) |
214 (cs
->mbus_attr
<< 8) |
215 (dram
->mbus_dram_target_id
<< 4) | 1,
216 iobase
+ CESA_TDMA_WINDOW_CTRL(i
));
217 writel(cs
->base
, iobase
+ CESA_TDMA_WINDOW_BASE(i
));
221 static int mv_cesa_dev_dma_init(struct mv_cesa_dev
*cesa
)
223 struct device
*dev
= cesa
->dev
;
224 struct mv_cesa_dev_dma
*dma
;
226 if (!cesa
->caps
->has_tdma
)
229 dma
= devm_kzalloc(dev
, sizeof(*dma
), GFP_KERNEL
);
233 dma
->tdma_desc_pool
= dmam_pool_create("tdma_desc", dev
,
234 sizeof(struct mv_cesa_tdma_desc
),
236 if (!dma
->tdma_desc_pool
)
239 dma
->op_pool
= dmam_pool_create("cesa_op", dev
,
240 sizeof(struct mv_cesa_op_ctx
), 16, 0);
244 dma
->cache_pool
= dmam_pool_create("cesa_cache", dev
,
245 CESA_MAX_HASH_BLOCK_SIZE
, 1, 0);
246 if (!dma
->cache_pool
)
249 dma
->padding_pool
= dmam_pool_create("cesa_padding", dev
, 72, 1, 0);
250 if (!dma
->cache_pool
)
258 static int mv_cesa_get_sram(struct platform_device
*pdev
, int idx
)
260 struct mv_cesa_dev
*cesa
= platform_get_drvdata(pdev
);
261 struct mv_cesa_engine
*engine
= &cesa
->engines
[idx
];
262 const char *res_name
= "sram";
263 struct resource
*res
;
265 engine
->pool
= of_get_named_gen_pool(cesa
->dev
->of_node
,
266 "marvell,crypto-srams",
269 engine
->sram
= gen_pool_dma_alloc(engine
->pool
,
279 if (cesa
->caps
->nengines
> 1) {
286 res
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
,
288 if (!res
|| resource_size(res
) < cesa
->sram_size
)
291 engine
->sram
= devm_ioremap_resource(cesa
->dev
, res
);
292 if (IS_ERR(engine
->sram
))
293 return PTR_ERR(engine
->sram
);
295 engine
->sram_dma
= phys_to_dma(cesa
->dev
,
296 (phys_addr_t
)res
->start
);
301 static void mv_cesa_put_sram(struct platform_device
*pdev
, int idx
)
303 struct mv_cesa_dev
*cesa
= platform_get_drvdata(pdev
);
304 struct mv_cesa_engine
*engine
= &cesa
->engines
[idx
];
309 gen_pool_free(engine
->pool
, (unsigned long)engine
->sram
,
313 static int mv_cesa_probe(struct platform_device
*pdev
)
315 const struct mv_cesa_caps
*caps
= NULL
;
316 const struct mbus_dram_target_info
*dram
;
317 const struct of_device_id
*match
;
318 struct device
*dev
= &pdev
->dev
;
319 struct mv_cesa_dev
*cesa
;
320 struct mv_cesa_engine
*engines
;
321 struct resource
*res
;
326 dev_err(&pdev
->dev
, "Only one CESA device authorized\n");
333 match
= of_match_node(mv_cesa_of_match_table
, dev
->of_node
);
334 if (!match
|| !match
->data
)
339 cesa
= devm_kzalloc(dev
, sizeof(*cesa
), GFP_KERNEL
);
346 sram_size
= CESA_SA_DEFAULT_SRAM_SIZE
;
347 of_property_read_u32(cesa
->dev
->of_node
, "marvell,crypto-sram-size",
349 if (sram_size
< CESA_SA_MIN_SRAM_SIZE
)
350 sram_size
= CESA_SA_MIN_SRAM_SIZE
;
352 cesa
->sram_size
= sram_size
;
353 cesa
->engines
= devm_kzalloc(dev
, caps
->nengines
* sizeof(*engines
),
358 spin_lock_init(&cesa
->lock
);
359 crypto_init_queue(&cesa
->queue
, 50);
360 res
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
, "regs");
361 cesa
->regs
= devm_ioremap_resource(dev
, res
);
362 if (IS_ERR(cesa
->regs
))
365 ret
= mv_cesa_dev_dma_init(cesa
);
369 dram
= mv_mbus_dram_info_nooverlap();
371 platform_set_drvdata(pdev
, cesa
);
373 for (i
= 0; i
< caps
->nengines
; i
++) {
374 struct mv_cesa_engine
*engine
= &cesa
->engines
[i
];
378 spin_lock_init(&engine
->lock
);
380 ret
= mv_cesa_get_sram(pdev
, i
);
384 irq
= platform_get_irq(pdev
, i
);
391 * Not all platforms can gate the CESA clocks: do not complain
392 * if the clock does not exist.
394 snprintf(res_name
, sizeof(res_name
), "cesa%d", i
);
395 engine
->clk
= devm_clk_get(dev
, res_name
);
396 if (IS_ERR(engine
->clk
)) {
397 engine
->clk
= devm_clk_get(dev
, NULL
);
398 if (IS_ERR(engine
->clk
))
402 snprintf(res_name
, sizeof(res_name
), "cesaz%d", i
);
403 engine
->zclk
= devm_clk_get(dev
, res_name
);
404 if (IS_ERR(engine
->zclk
))
407 ret
= clk_prepare_enable(engine
->clk
);
411 ret
= clk_prepare_enable(engine
->zclk
);
415 engine
->regs
= cesa
->regs
+ CESA_ENGINE_OFF(i
);
417 if (dram
&& cesa
->caps
->has_tdma
)
418 mv_cesa_conf_mbus_windows(&cesa
->engines
[i
], dram
);
420 writel(0, cesa
->engines
[i
].regs
+ CESA_SA_INT_STATUS
);
421 writel(CESA_SA_CFG_STOP_DIG_ERR
,
422 cesa
->engines
[i
].regs
+ CESA_SA_CFG
);
423 writel(engine
->sram_dma
& CESA_SA_SRAM_MSK
,
424 cesa
->engines
[i
].regs
+ CESA_SA_DESC_P0
);
426 ret
= devm_request_threaded_irq(dev
, irq
, NULL
, mv_cesa_int
,
428 dev_name(&pdev
->dev
),
436 ret
= mv_cesa_add_algs(cesa
);
442 dev_info(dev
, "CESA device successfully registered\n");
447 for (i
= 0; i
< caps
->nengines
; i
++) {
448 clk_disable_unprepare(cesa
->engines
[i
].zclk
);
449 clk_disable_unprepare(cesa
->engines
[i
].clk
);
450 mv_cesa_put_sram(pdev
, i
);
456 static int mv_cesa_remove(struct platform_device
*pdev
)
458 struct mv_cesa_dev
*cesa
= platform_get_drvdata(pdev
);
461 mv_cesa_remove_algs(cesa
);
463 for (i
= 0; i
< cesa
->caps
->nengines
; i
++) {
464 clk_disable_unprepare(cesa
->engines
[i
].zclk
);
465 clk_disable_unprepare(cesa
->engines
[i
].clk
);
466 mv_cesa_put_sram(pdev
, i
);
472 static struct platform_driver marvell_cesa
= {
473 .probe
= mv_cesa_probe
,
474 .remove
= mv_cesa_remove
,
476 .owner
= THIS_MODULE
,
477 .name
= "marvell-cesa",
478 .of_match_table
= mv_cesa_of_match_table
,
481 module_platform_driver(marvell_cesa
);
483 MODULE_ALIAS("platform:mv_crypto");
484 MODULE_AUTHOR("Boris Brezillon <boris.brezillon@free-electrons.com>");
485 MODULE_AUTHOR("Arnaud Ebalard <arno@natisbad.org>");
486 MODULE_DESCRIPTION("Support for Marvell's cryptographic engine");
487 MODULE_LICENSE("GPL v2");