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