blob: a05b5cb3aea729806e485ccb8938931641671507 [file] [log] [blame]
Boris BREZILLONf63601f2015-06-18 15:46:20 +02001/*
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.
5 *
6 * Author: Boris Brezillon <boris.brezillon@free-electrons.com>
7 * Author: Arnaud Ebalard <arno@natisbad.org>
8 *
9 * This work is based on an initial version written by
10 * Sebastian Andrzej Siewior < sebastian at breakpoint dot cc >
11 *
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.
15 */
16
17#include <linux/delay.h>
18#include <linux/genalloc.h>
19#include <linux/interrupt.h>
20#include <linux/io.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>
28#include <linux/of.h>
29#include <linux/of_platform.h>
30#include <linux/of_irq.h>
31
32#include "cesa.h"
33
Boris BREZILLON64c55d42015-06-18 15:46:27 +020034static int allhwsupport = !IS_ENABLED(CONFIG_CRYPTO_DEV_MV_CESA);
35module_param_named(allhwsupport, allhwsupport, int, 0444);
36MODULE_PARM_DESC(allhwsupport, "Enable support for all hardware (even it if overlaps with the mv_cesa driver)");
37
Boris BREZILLONf63601f2015-06-18 15:46:20 +020038struct mv_cesa_dev *cesa_dev;
39
40static void mv_cesa_dequeue_req_unlocked(struct mv_cesa_engine *engine)
41{
42 struct crypto_async_request *req, *backlog;
43 struct mv_cesa_ctx *ctx;
44
45 spin_lock_bh(&cesa_dev->lock);
46 backlog = crypto_get_backlog(&cesa_dev->queue);
47 req = crypto_dequeue_request(&cesa_dev->queue);
48 engine->req = req;
49 spin_unlock_bh(&cesa_dev->lock);
50
51 if (!req)
52 return;
53
54 if (backlog)
55 backlog->complete(backlog, -EINPROGRESS);
56
57 ctx = crypto_tfm_ctx(req->tfm);
58 ctx->ops->prepare(req, engine);
59 ctx->ops->step(req);
60}
61
62static irqreturn_t mv_cesa_int(int irq, void *priv)
63{
64 struct mv_cesa_engine *engine = priv;
65 struct crypto_async_request *req;
66 struct mv_cesa_ctx *ctx;
67 u32 status, mask;
68 irqreturn_t ret = IRQ_NONE;
69
70 while (true) {
71 int res;
72
73 mask = mv_cesa_get_int_mask(engine);
74 status = readl(engine->regs + CESA_SA_INT_STATUS);
75
76 if (!(status & mask))
77 break;
78
79 /*
80 * TODO: avoid clearing the FPGA_INT_STATUS if this not
81 * relevant on some platforms.
82 */
83 writel(~status, engine->regs + CESA_SA_FPGA_INT_STATUS);
84 writel(~status, engine->regs + CESA_SA_INT_STATUS);
85
86 ret = IRQ_HANDLED;
87 spin_lock_bh(&engine->lock);
88 req = engine->req;
89 spin_unlock_bh(&engine->lock);
90 if (req) {
91 ctx = crypto_tfm_ctx(req->tfm);
92 res = ctx->ops->process(req, status & mask);
93 if (res != -EINPROGRESS) {
94 spin_lock_bh(&engine->lock);
95 engine->req = NULL;
96 mv_cesa_dequeue_req_unlocked(engine);
97 spin_unlock_bh(&engine->lock);
98 ctx->ops->cleanup(req);
99 local_bh_disable();
100 req->complete(req, res);
101 local_bh_enable();
102 } else {
103 ctx->ops->step(req);
104 }
105 }
106 }
107
108 return ret;
109}
110
111int mv_cesa_queue_req(struct crypto_async_request *req)
112{
113 int ret;
114 int i;
115
116 spin_lock_bh(&cesa_dev->lock);
117 ret = crypto_enqueue_request(&cesa_dev->queue, req);
118 spin_unlock_bh(&cesa_dev->lock);
119
120 if (ret != -EINPROGRESS)
121 return ret;
122
123 for (i = 0; i < cesa_dev->caps->nengines; i++) {
124 spin_lock_bh(&cesa_dev->engines[i].lock);
125 if (!cesa_dev->engines[i].req)
126 mv_cesa_dequeue_req_unlocked(&cesa_dev->engines[i]);
127 spin_unlock_bh(&cesa_dev->engines[i].lock);
128 }
129
130 return -EINPROGRESS;
131}
132
133static int mv_cesa_add_algs(struct mv_cesa_dev *cesa)
134{
135 int ret;
136 int i, j;
137
138 for (i = 0; i < cesa->caps->ncipher_algs; i++) {
139 ret = crypto_register_alg(cesa->caps->cipher_algs[i]);
140 if (ret)
141 goto err_unregister_crypto;
142 }
143
144 for (i = 0; i < cesa->caps->nahash_algs; i++) {
145 ret = crypto_register_ahash(cesa->caps->ahash_algs[i]);
146 if (ret)
147 goto err_unregister_ahash;
148 }
149
150 return 0;
151
152err_unregister_ahash:
153 for (j = 0; j < i; j++)
154 crypto_unregister_ahash(cesa->caps->ahash_algs[j]);
155 i = cesa->caps->ncipher_algs;
156
157err_unregister_crypto:
158 for (j = 0; j < i; j++)
159 crypto_unregister_alg(cesa->caps->cipher_algs[j]);
160
161 return ret;
162}
163
164static void mv_cesa_remove_algs(struct mv_cesa_dev *cesa)
165{
166 int i;
167
168 for (i = 0; i < cesa->caps->nahash_algs; i++)
169 crypto_unregister_ahash(cesa->caps->ahash_algs[i]);
170
171 for (i = 0; i < cesa->caps->ncipher_algs; i++)
172 crypto_unregister_alg(cesa->caps->cipher_algs[i]);
173}
174
175static struct crypto_alg *armada_370_cipher_algs[] = {
Boris BREZILLON7b3aaaa2015-06-18 15:46:22 +0200176 &mv_cesa_ecb_des_alg,
177 &mv_cesa_cbc_des_alg,
Arnaud Ebalard4ada4832015-06-18 15:46:23 +0200178 &mv_cesa_ecb_des3_ede_alg,
179 &mv_cesa_cbc_des3_ede_alg,
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200180 &mv_cesa_ecb_aes_alg,
181 &mv_cesa_cbc_aes_alg,
182};
183
184static struct ahash_alg *armada_370_ahash_algs[] = {
Arnaud Ebalard7aeef692015-06-18 15:46:24 +0200185 &mv_md5_alg,
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200186 &mv_sha1_alg,
Arnaud Ebalardf85a7622015-06-18 15:46:25 +0200187 &mv_sha256_alg,
Arnaud Ebalard7aeef692015-06-18 15:46:24 +0200188 &mv_ahmac_md5_alg,
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200189 &mv_ahmac_sha1_alg,
Arnaud Ebalardf85a7622015-06-18 15:46:25 +0200190 &mv_ahmac_sha256_alg,
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200191};
192
193static const struct mv_cesa_caps armada_370_caps = {
194 .nengines = 1,
195 .cipher_algs = armada_370_cipher_algs,
196 .ncipher_algs = ARRAY_SIZE(armada_370_cipher_algs),
197 .ahash_algs = armada_370_ahash_algs,
198 .nahash_algs = ARRAY_SIZE(armada_370_ahash_algs),
Boris BREZILLONdb509a42015-06-18 15:46:21 +0200199 .has_tdma = true,
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200200};
201
Boris BREZILLON898c9d52015-06-18 15:46:26 +0200202static const struct mv_cesa_caps armada_xp_caps = {
203 .nengines = 2,
204 .cipher_algs = armada_370_cipher_algs,
205 .ncipher_algs = ARRAY_SIZE(armada_370_cipher_algs),
206 .ahash_algs = armada_370_ahash_algs,
207 .nahash_algs = ARRAY_SIZE(armada_370_ahash_algs),
208 .has_tdma = true,
209};
210
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200211static const struct of_device_id mv_cesa_of_match_table[] = {
212 { .compatible = "marvell,armada-370-crypto", .data = &armada_370_caps },
Boris BREZILLON898c9d52015-06-18 15:46:26 +0200213 { .compatible = "marvell,armada-xp-crypto", .data = &armada_xp_caps },
214 { .compatible = "marvell,armada-375-crypto", .data = &armada_xp_caps },
215 { .compatible = "marvell,armada-38x-crypto", .data = &armada_xp_caps },
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200216 {}
217};
218MODULE_DEVICE_TABLE(of, mv_cesa_of_match_table);
219
Boris BREZILLONdb509a42015-06-18 15:46:21 +0200220static void
221mv_cesa_conf_mbus_windows(struct mv_cesa_engine *engine,
222 const struct mbus_dram_target_info *dram)
223{
224 void __iomem *iobase = engine->regs;
225 int i;
226
227 for (i = 0; i < 4; i++) {
228 writel(0, iobase + CESA_TDMA_WINDOW_CTRL(i));
229 writel(0, iobase + CESA_TDMA_WINDOW_BASE(i));
230 }
231
232 for (i = 0; i < dram->num_cs; i++) {
233 const struct mbus_dram_window *cs = dram->cs + i;
234
235 writel(((cs->size - 1) & 0xffff0000) |
236 (cs->mbus_attr << 8) |
237 (dram->mbus_dram_target_id << 4) | 1,
238 iobase + CESA_TDMA_WINDOW_CTRL(i));
239 writel(cs->base, iobase + CESA_TDMA_WINDOW_BASE(i));
240 }
241}
242
243static int mv_cesa_dev_dma_init(struct mv_cesa_dev *cesa)
244{
245 struct device *dev = cesa->dev;
246 struct mv_cesa_dev_dma *dma;
247
248 if (!cesa->caps->has_tdma)
249 return 0;
250
251 dma = devm_kzalloc(dev, sizeof(*dma), GFP_KERNEL);
252 if (!dma)
253 return -ENOMEM;
254
255 dma->tdma_desc_pool = dmam_pool_create("tdma_desc", dev,
256 sizeof(struct mv_cesa_tdma_desc),
257 16, 0);
258 if (!dma->tdma_desc_pool)
259 return -ENOMEM;
260
261 dma->op_pool = dmam_pool_create("cesa_op", dev,
262 sizeof(struct mv_cesa_op_ctx), 16, 0);
263 if (!dma->op_pool)
264 return -ENOMEM;
265
266 dma->cache_pool = dmam_pool_create("cesa_cache", dev,
267 CESA_MAX_HASH_BLOCK_SIZE, 1, 0);
268 if (!dma->cache_pool)
269 return -ENOMEM;
270
271 dma->padding_pool = dmam_pool_create("cesa_padding", dev, 72, 1, 0);
272 if (!dma->cache_pool)
273 return -ENOMEM;
274
275 cesa->dma = dma;
276
277 return 0;
278}
279
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200280static int mv_cesa_get_sram(struct platform_device *pdev, int idx)
281{
282 struct mv_cesa_dev *cesa = platform_get_drvdata(pdev);
283 struct mv_cesa_engine *engine = &cesa->engines[idx];
284 const char *res_name = "sram";
285 struct resource *res;
286
287 engine->pool = of_get_named_gen_pool(cesa->dev->of_node,
288 "marvell,crypto-srams",
289 idx);
290 if (engine->pool) {
291 engine->sram = gen_pool_dma_alloc(engine->pool,
292 cesa->sram_size,
293 &engine->sram_dma);
294 if (engine->sram)
295 return 0;
296
297 engine->pool = NULL;
298 return -ENOMEM;
299 }
300
301 if (cesa->caps->nengines > 1) {
302 if (!idx)
303 res_name = "sram0";
304 else
305 res_name = "sram1";
306 }
307
308 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
309 res_name);
310 if (!res || resource_size(res) < cesa->sram_size)
311 return -EINVAL;
312
313 engine->sram = devm_ioremap_resource(cesa->dev, res);
314 if (IS_ERR(engine->sram))
315 return PTR_ERR(engine->sram);
316
317 engine->sram_dma = phys_to_dma(cesa->dev,
318 (phys_addr_t)res->start);
319
320 return 0;
321}
322
323static void mv_cesa_put_sram(struct platform_device *pdev, int idx)
324{
325 struct mv_cesa_dev *cesa = platform_get_drvdata(pdev);
326 struct mv_cesa_engine *engine = &cesa->engines[idx];
327
328 if (!engine->pool)
329 return;
330
331 gen_pool_free(engine->pool, (unsigned long)engine->sram,
332 cesa->sram_size);
333}
334
335static int mv_cesa_probe(struct platform_device *pdev)
336{
337 const struct mv_cesa_caps *caps = NULL;
338 const struct mbus_dram_target_info *dram;
339 const struct of_device_id *match;
340 struct device *dev = &pdev->dev;
341 struct mv_cesa_dev *cesa;
342 struct mv_cesa_engine *engines;
343 struct resource *res;
344 int irq, ret, i;
345 u32 sram_size;
346
347 if (cesa_dev) {
348 dev_err(&pdev->dev, "Only one CESA device authorized\n");
349 return -EEXIST;
350 }
351
352 if (!dev->of_node)
353 return -ENOTSUPP;
354
355 match = of_match_node(mv_cesa_of_match_table, dev->of_node);
356 if (!match || !match->data)
357 return -ENOTSUPP;
358
359 caps = match->data;
360
361 cesa = devm_kzalloc(dev, sizeof(*cesa), GFP_KERNEL);
362 if (!cesa)
363 return -ENOMEM;
364
365 cesa->caps = caps;
366 cesa->dev = dev;
367
368 sram_size = CESA_SA_DEFAULT_SRAM_SIZE;
369 of_property_read_u32(cesa->dev->of_node, "marvell,crypto-sram-size",
370 &sram_size);
371 if (sram_size < CESA_SA_MIN_SRAM_SIZE)
372 sram_size = CESA_SA_MIN_SRAM_SIZE;
373
374 cesa->sram_size = sram_size;
375 cesa->engines = devm_kzalloc(dev, caps->nengines * sizeof(*engines),
376 GFP_KERNEL);
377 if (!cesa->engines)
378 return -ENOMEM;
379
380 spin_lock_init(&cesa->lock);
381 crypto_init_queue(&cesa->queue, 50);
382 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "regs");
383 cesa->regs = devm_ioremap_resource(dev, res);
384 if (IS_ERR(cesa->regs))
385 return -ENOMEM;
386
Boris BREZILLONdb509a42015-06-18 15:46:21 +0200387 ret = mv_cesa_dev_dma_init(cesa);
388 if (ret)
389 return ret;
390
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200391 dram = mv_mbus_dram_info_nooverlap();
392
393 platform_set_drvdata(pdev, cesa);
394
395 for (i = 0; i < caps->nengines; i++) {
396 struct mv_cesa_engine *engine = &cesa->engines[i];
397 char res_name[7];
398
399 engine->id = i;
400 spin_lock_init(&engine->lock);
401
402 ret = mv_cesa_get_sram(pdev, i);
403 if (ret)
404 goto err_cleanup;
405
406 irq = platform_get_irq(pdev, i);
407 if (irq < 0) {
408 ret = irq;
409 goto err_cleanup;
410 }
411
412 /*
413 * Not all platforms can gate the CESA clocks: do not complain
414 * if the clock does not exist.
415 */
416 snprintf(res_name, sizeof(res_name), "cesa%d", i);
417 engine->clk = devm_clk_get(dev, res_name);
418 if (IS_ERR(engine->clk)) {
419 engine->clk = devm_clk_get(dev, NULL);
420 if (IS_ERR(engine->clk))
421 engine->clk = NULL;
422 }
423
424 snprintf(res_name, sizeof(res_name), "cesaz%d", i);
425 engine->zclk = devm_clk_get(dev, res_name);
426 if (IS_ERR(engine->zclk))
427 engine->zclk = NULL;
428
429 ret = clk_prepare_enable(engine->clk);
430 if (ret)
431 goto err_cleanup;
432
433 ret = clk_prepare_enable(engine->zclk);
434 if (ret)
435 goto err_cleanup;
436
437 engine->regs = cesa->regs + CESA_ENGINE_OFF(i);
438
Boris BREZILLONdb509a42015-06-18 15:46:21 +0200439 if (dram && cesa->caps->has_tdma)
440 mv_cesa_conf_mbus_windows(&cesa->engines[i], dram);
441
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200442 writel(0, cesa->engines[i].regs + CESA_SA_INT_STATUS);
443 writel(CESA_SA_CFG_STOP_DIG_ERR,
444 cesa->engines[i].regs + CESA_SA_CFG);
445 writel(engine->sram_dma & CESA_SA_SRAM_MSK,
446 cesa->engines[i].regs + CESA_SA_DESC_P0);
447
448 ret = devm_request_threaded_irq(dev, irq, NULL, mv_cesa_int,
449 IRQF_ONESHOT,
450 dev_name(&pdev->dev),
451 &cesa->engines[i]);
452 if (ret)
453 goto err_cleanup;
454 }
455
456 cesa_dev = cesa;
457
458 ret = mv_cesa_add_algs(cesa);
459 if (ret) {
460 cesa_dev = NULL;
461 goto err_cleanup;
462 }
463
464 dev_info(dev, "CESA device successfully registered\n");
465
466 return 0;
467
468err_cleanup:
469 for (i = 0; i < caps->nengines; i++) {
470 clk_disable_unprepare(cesa->engines[i].zclk);
471 clk_disable_unprepare(cesa->engines[i].clk);
472 mv_cesa_put_sram(pdev, i);
473 }
474
475 return ret;
476}
477
478static int mv_cesa_remove(struct platform_device *pdev)
479{
480 struct mv_cesa_dev *cesa = platform_get_drvdata(pdev);
481 int i;
482
483 mv_cesa_remove_algs(cesa);
484
485 for (i = 0; i < cesa->caps->nengines; i++) {
486 clk_disable_unprepare(cesa->engines[i].zclk);
487 clk_disable_unprepare(cesa->engines[i].clk);
488 mv_cesa_put_sram(pdev, i);
489 }
490
491 return 0;
492}
493
494static struct platform_driver marvell_cesa = {
495 .probe = mv_cesa_probe,
496 .remove = mv_cesa_remove,
497 .driver = {
498 .owner = THIS_MODULE,
499 .name = "marvell-cesa",
500 .of_match_table = mv_cesa_of_match_table,
501 },
502};
503module_platform_driver(marvell_cesa);
504
505MODULE_ALIAS("platform:mv_crypto");
506MODULE_AUTHOR("Boris Brezillon <boris.brezillon@free-electrons.com>");
507MODULE_AUTHOR("Arnaud Ebalard <arno@natisbad.org>");
508MODULE_DESCRIPTION("Support for Marvell's cryptographic engine");
509MODULE_LICENSE("GPL v2");