1 // SPDX-License-Identifier: GPL-2.0-only
5 * Support for OMAP AES HW acceleration.
7 * Copyright (c) 2010 Nokia Corporation
8 * Author: Dmitry Kasatkin <dmitry.kasatkin@nokia.com>
9 * Copyright (c) 2011 Texas Instruments Incorporated
12 #define pr_fmt(fmt) "%20s: " fmt, __func__
13 #define prn(num) pr_debug(#num "=%d\n", num)
14 #define prx(num) pr_debug(#num "=%x\n", num)
16 #include <linux/err.h>
17 #include <linux/module.h>
18 #include <linux/init.h>
19 #include <linux/errno.h>
20 #include <linux/kernel.h>
21 #include <linux/platform_device.h>
22 #include <linux/scatterlist.h>
23 #include <linux/dma-mapping.h>
24 #include <linux/dmaengine.h>
25 #include <linux/pm_runtime.h>
27 #include <linux/of_device.h>
28 #include <linux/of_address.h>
30 #include <linux/crypto.h>
31 #include <linux/interrupt.h>
32 #include <crypto/scatterwalk.h>
33 #include <crypto/aes.h>
34 #include <crypto/gcm.h>
35 #include <crypto/engine.h>
36 #include <crypto/internal/skcipher.h>
37 #include <crypto/internal/aead.h>
39 #include "omap-crypto.h"
42 /* keep registered devices data here */
43 static LIST_HEAD(dev_list);
44 static DEFINE_SPINLOCK(list_lock);
46 static int aes_fallback_sz = 200;
49 #define omap_aes_read(dd, offset) \
52 _read_ret = __raw_readl(dd->io_base + offset); \
53 pr_debug("omap_aes_read(" #offset "=%#x)= %#x\n", \
58 inline u32 omap_aes_read(struct omap_aes_dev *dd, u32 offset)
60 return __raw_readl(dd->io_base + offset);
65 #define omap_aes_write(dd, offset, value) \
67 pr_debug("omap_aes_write(" #offset "=%#x) value=%#x\n", \
69 __raw_writel(value, dd->io_base + offset); \
72 inline void omap_aes_write(struct omap_aes_dev *dd, u32 offset,
75 __raw_writel(value, dd->io_base + offset);
79 static inline void omap_aes_write_mask(struct omap_aes_dev *dd, u32 offset,
84 val = omap_aes_read(dd, offset);
87 omap_aes_write(dd, offset, val);
90 static void omap_aes_write_n(struct omap_aes_dev *dd, u32 offset,
91 u32 *value, int count)
93 for (; count--; value++, offset += 4)
94 omap_aes_write(dd, offset, *value);
97 static int omap_aes_hw_init(struct omap_aes_dev *dd)
101 if (!(dd->flags & FLAGS_INIT)) {
102 dd->flags |= FLAGS_INIT;
106 err = pm_runtime_resume_and_get(dd->dev);
108 dev_err(dd->dev, "failed to get sync: %d\n", err);
115 void omap_aes_clear_copy_flags(struct omap_aes_dev *dd)
117 dd->flags &= ~(OMAP_CRYPTO_COPY_MASK << FLAGS_IN_DATA_ST_SHIFT);
118 dd->flags &= ~(OMAP_CRYPTO_COPY_MASK << FLAGS_OUT_DATA_ST_SHIFT);
119 dd->flags &= ~(OMAP_CRYPTO_COPY_MASK << FLAGS_ASSOC_DATA_ST_SHIFT);
122 int omap_aes_write_ctrl(struct omap_aes_dev *dd)
124 struct omap_aes_reqctx *rctx;
129 err = omap_aes_hw_init(dd);
133 key32 = dd->ctx->keylen / sizeof(u32);
135 /* RESET the key as previous HASH keys should not get affected*/
136 if (dd->flags & FLAGS_GCM)
137 for (i = 0; i < 0x40; i = i + 4)
138 omap_aes_write(dd, i, 0x0);
140 for (i = 0; i < key32; i++) {
141 omap_aes_write(dd, AES_REG_KEY(dd, i),
142 (__force u32)cpu_to_le32(dd->ctx->key[i]));
145 if ((dd->flags & (FLAGS_CBC | FLAGS_CTR)) && dd->req->iv)
146 omap_aes_write_n(dd, AES_REG_IV(dd, 0), (void *)dd->req->iv, 4);
148 if ((dd->flags & (FLAGS_GCM)) && dd->aead_req->iv) {
149 rctx = aead_request_ctx(dd->aead_req);
150 omap_aes_write_n(dd, AES_REG_IV(dd, 0), (u32 *)rctx->iv, 4);
153 val = FLD_VAL(((dd->ctx->keylen >> 3) - 1), 4, 3);
154 if (dd->flags & FLAGS_CBC)
155 val |= AES_REG_CTRL_CBC;
157 if (dd->flags & (FLAGS_CTR | FLAGS_GCM))
158 val |= AES_REG_CTRL_CTR | AES_REG_CTRL_CTR_WIDTH_128;
160 if (dd->flags & FLAGS_GCM)
161 val |= AES_REG_CTRL_GCM;
163 if (dd->flags & FLAGS_ENCRYPT)
164 val |= AES_REG_CTRL_DIRECTION;
166 omap_aes_write_mask(dd, AES_REG_CTRL(dd), val, AES_REG_CTRL_MASK);
171 static void omap_aes_dma_trigger_omap2(struct omap_aes_dev *dd, int length)
175 val = dd->pdata->dma_start;
177 if (dd->dma_lch_out != NULL)
178 val |= dd->pdata->dma_enable_out;
179 if (dd->dma_lch_in != NULL)
180 val |= dd->pdata->dma_enable_in;
182 mask = dd->pdata->dma_enable_out | dd->pdata->dma_enable_in |
183 dd->pdata->dma_start;
185 omap_aes_write_mask(dd, AES_REG_MASK(dd), val, mask);
189 static void omap_aes_dma_trigger_omap4(struct omap_aes_dev *dd, int length)
191 omap_aes_write(dd, AES_REG_LENGTH_N(0), length);
192 omap_aes_write(dd, AES_REG_LENGTH_N(1), 0);
193 if (dd->flags & FLAGS_GCM)
194 omap_aes_write(dd, AES_REG_A_LEN, dd->assoc_len);
196 omap_aes_dma_trigger_omap2(dd, length);
199 static void omap_aes_dma_stop(struct omap_aes_dev *dd)
203 mask = dd->pdata->dma_enable_out | dd->pdata->dma_enable_in |
204 dd->pdata->dma_start;
206 omap_aes_write_mask(dd, AES_REG_MASK(dd), 0, mask);
209 struct omap_aes_dev *omap_aes_find_dev(struct omap_aes_reqctx *rctx)
211 struct omap_aes_dev *dd;
213 spin_lock_bh(&list_lock);
214 dd = list_first_entry(&dev_list, struct omap_aes_dev, list);
215 list_move_tail(&dd->list, &dev_list);
217 spin_unlock_bh(&list_lock);
222 static void omap_aes_dma_out_callback(void *data)
224 struct omap_aes_dev *dd = data;
226 /* dma_lch_out - completed */
227 tasklet_schedule(&dd->done_task);
230 static int omap_aes_dma_init(struct omap_aes_dev *dd)
234 dd->dma_lch_out = NULL;
235 dd->dma_lch_in = NULL;
237 dd->dma_lch_in = dma_request_chan(dd->dev, "rx");
238 if (IS_ERR(dd->dma_lch_in)) {
239 dev_err(dd->dev, "Unable to request in DMA channel\n");
240 return PTR_ERR(dd->dma_lch_in);
243 dd->dma_lch_out = dma_request_chan(dd->dev, "tx");
244 if (IS_ERR(dd->dma_lch_out)) {
245 dev_err(dd->dev, "Unable to request out DMA channel\n");
246 err = PTR_ERR(dd->dma_lch_out);
253 dma_release_channel(dd->dma_lch_in);
258 static void omap_aes_dma_cleanup(struct omap_aes_dev *dd)
263 dma_release_channel(dd->dma_lch_out);
264 dma_release_channel(dd->dma_lch_in);
267 static int omap_aes_crypt_dma(struct omap_aes_dev *dd,
268 struct scatterlist *in_sg,
269 struct scatterlist *out_sg,
270 int in_sg_len, int out_sg_len)
272 struct dma_async_tx_descriptor *tx_in, *tx_out = NULL, *cb_desc;
273 struct dma_slave_config cfg;
277 scatterwalk_start(&dd->in_walk, dd->in_sg);
279 scatterwalk_start(&dd->out_walk, dd->out_sg);
281 /* Enable DATAIN interrupt and let it take
283 omap_aes_write(dd, AES_REG_IRQ_ENABLE(dd), 0x2);
287 dma_sync_sg_for_device(dd->dev, dd->in_sg, in_sg_len, DMA_TO_DEVICE);
289 memset(&cfg, 0, sizeof(cfg));
291 cfg.src_addr = dd->phys_base + AES_REG_DATA_N(dd, 0);
292 cfg.dst_addr = dd->phys_base + AES_REG_DATA_N(dd, 0);
293 cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
294 cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
295 cfg.src_maxburst = DST_MAXBURST;
296 cfg.dst_maxburst = DST_MAXBURST;
299 ret = dmaengine_slave_config(dd->dma_lch_in, &cfg);
301 dev_err(dd->dev, "can't configure IN dmaengine slave: %d\n",
306 tx_in = dmaengine_prep_slave_sg(dd->dma_lch_in, in_sg, in_sg_len,
308 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
310 dev_err(dd->dev, "IN prep_slave_sg() failed\n");
314 /* No callback necessary */
315 tx_in->callback_param = dd;
316 tx_in->callback = NULL;
320 ret = dmaengine_slave_config(dd->dma_lch_out, &cfg);
322 dev_err(dd->dev, "can't configure OUT dmaengine slave: %d\n",
327 tx_out = dmaengine_prep_slave_sg(dd->dma_lch_out, out_sg,
330 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
332 dev_err(dd->dev, "OUT prep_slave_sg() failed\n");
341 if (dd->flags & FLAGS_GCM)
342 cb_desc->callback = omap_aes_gcm_dma_out_callback;
344 cb_desc->callback = omap_aes_dma_out_callback;
345 cb_desc->callback_param = dd;
348 dmaengine_submit(tx_in);
350 dmaengine_submit(tx_out);
352 dma_async_issue_pending(dd->dma_lch_in);
354 dma_async_issue_pending(dd->dma_lch_out);
357 dd->pdata->trigger(dd, dd->total);
362 int omap_aes_crypt_dma_start(struct omap_aes_dev *dd)
366 pr_debug("total: %zu\n", dd->total);
369 err = dma_map_sg(dd->dev, dd->in_sg, dd->in_sg_len,
372 dev_err(dd->dev, "dma_map_sg() error\n");
376 if (dd->out_sg_len) {
377 err = dma_map_sg(dd->dev, dd->out_sg, dd->out_sg_len,
380 dev_err(dd->dev, "dma_map_sg() error\n");
386 err = omap_aes_crypt_dma(dd, dd->in_sg, dd->out_sg, dd->in_sg_len,
388 if (err && !dd->pio_only) {
389 dma_unmap_sg(dd->dev, dd->in_sg, dd->in_sg_len, DMA_TO_DEVICE);
391 dma_unmap_sg(dd->dev, dd->out_sg, dd->out_sg_len,
398 static void omap_aes_finish_req(struct omap_aes_dev *dd, int err)
400 struct skcipher_request *req = dd->req;
402 pr_debug("err: %d\n", err);
404 crypto_finalize_skcipher_request(dd->engine, req, err);
406 pm_runtime_mark_last_busy(dd->dev);
407 pm_runtime_put_autosuspend(dd->dev);
410 int omap_aes_crypt_dma_stop(struct omap_aes_dev *dd)
412 pr_debug("total: %zu\n", dd->total);
414 omap_aes_dma_stop(dd);
420 static int omap_aes_handle_queue(struct omap_aes_dev *dd,
421 struct skcipher_request *req)
424 return crypto_transfer_skcipher_request_to_engine(dd->engine, req);
429 static int omap_aes_prepare_req(struct crypto_engine *engine,
432 struct skcipher_request *req = container_of(areq, struct skcipher_request, base);
433 struct omap_aes_ctx *ctx = crypto_skcipher_ctx(
434 crypto_skcipher_reqtfm(req));
435 struct omap_aes_reqctx *rctx = skcipher_request_ctx(req);
436 struct omap_aes_dev *dd = rctx->dd;
443 /* assign new request to device */
445 dd->total = req->cryptlen;
446 dd->total_save = req->cryptlen;
447 dd->in_sg = req->src;
448 dd->out_sg = req->dst;
449 dd->orig_out = req->dst;
451 flags = OMAP_CRYPTO_COPY_DATA;
452 if (req->src == req->dst)
453 flags |= OMAP_CRYPTO_FORCE_COPY;
455 ret = omap_crypto_align_sg(&dd->in_sg, dd->total, AES_BLOCK_SIZE,
457 FLAGS_IN_DATA_ST_SHIFT, &dd->flags);
461 ret = omap_crypto_align_sg(&dd->out_sg, dd->total, AES_BLOCK_SIZE,
463 FLAGS_OUT_DATA_ST_SHIFT, &dd->flags);
467 dd->in_sg_len = sg_nents_for_len(dd->in_sg, dd->total);
468 if (dd->in_sg_len < 0)
469 return dd->in_sg_len;
471 dd->out_sg_len = sg_nents_for_len(dd->out_sg, dd->total);
472 if (dd->out_sg_len < 0)
473 return dd->out_sg_len;
475 rctx->mode &= FLAGS_MODE_MASK;
476 dd->flags = (dd->flags & ~FLAGS_MODE_MASK) | rctx->mode;
481 return omap_aes_write_ctrl(dd);
484 static int omap_aes_crypt_req(struct crypto_engine *engine,
487 struct skcipher_request *req = container_of(areq, struct skcipher_request, base);
488 struct omap_aes_reqctx *rctx = skcipher_request_ctx(req);
489 struct omap_aes_dev *dd = rctx->dd;
494 return omap_aes_crypt_dma_start(dd);
497 static void omap_aes_copy_ivout(struct omap_aes_dev *dd, u8 *ivbuf)
501 for (i = 0; i < 4; i++)
502 ((u32 *)ivbuf)[i] = omap_aes_read(dd, AES_REG_IV(dd, i));
505 static void omap_aes_done_task(unsigned long data)
507 struct omap_aes_dev *dd = (struct omap_aes_dev *)data;
509 pr_debug("enter done_task\n");
512 dma_sync_sg_for_device(dd->dev, dd->out_sg, dd->out_sg_len,
514 dma_unmap_sg(dd->dev, dd->in_sg, dd->in_sg_len, DMA_TO_DEVICE);
515 dma_unmap_sg(dd->dev, dd->out_sg, dd->out_sg_len,
517 omap_aes_crypt_dma_stop(dd);
520 omap_crypto_cleanup(dd->in_sg, NULL, 0, dd->total_save,
521 FLAGS_IN_DATA_ST_SHIFT, dd->flags);
523 omap_crypto_cleanup(dd->out_sg, dd->orig_out, 0, dd->total_save,
524 FLAGS_OUT_DATA_ST_SHIFT, dd->flags);
526 /* Update IV output */
527 if (dd->flags & (FLAGS_CBC | FLAGS_CTR))
528 omap_aes_copy_ivout(dd, dd->req->iv);
530 omap_aes_finish_req(dd, 0);
535 static int omap_aes_crypt(struct skcipher_request *req, unsigned long mode)
537 struct omap_aes_ctx *ctx = crypto_skcipher_ctx(
538 crypto_skcipher_reqtfm(req));
539 struct omap_aes_reqctx *rctx = skcipher_request_ctx(req);
540 struct omap_aes_dev *dd;
543 if ((req->cryptlen % AES_BLOCK_SIZE) && !(mode & FLAGS_CTR))
546 pr_debug("nbytes: %d, enc: %d, cbc: %d\n", req->cryptlen,
547 !!(mode & FLAGS_ENCRYPT),
548 !!(mode & FLAGS_CBC));
550 if (req->cryptlen < aes_fallback_sz) {
551 skcipher_request_set_tfm(&rctx->fallback_req, ctx->fallback);
552 skcipher_request_set_callback(&rctx->fallback_req,
556 skcipher_request_set_crypt(&rctx->fallback_req, req->src,
557 req->dst, req->cryptlen, req->iv);
559 if (mode & FLAGS_ENCRYPT)
560 ret = crypto_skcipher_encrypt(&rctx->fallback_req);
562 ret = crypto_skcipher_decrypt(&rctx->fallback_req);
565 dd = omap_aes_find_dev(rctx);
571 return omap_aes_handle_queue(dd, req);
574 /* ********************** ALG API ************************************ */
576 static int omap_aes_setkey(struct crypto_skcipher *tfm, const u8 *key,
579 struct omap_aes_ctx *ctx = crypto_skcipher_ctx(tfm);
582 if (keylen != AES_KEYSIZE_128 && keylen != AES_KEYSIZE_192 &&
583 keylen != AES_KEYSIZE_256)
586 pr_debug("enter, keylen: %d\n", keylen);
588 memcpy(ctx->key, key, keylen);
589 ctx->keylen = keylen;
591 crypto_skcipher_clear_flags(ctx->fallback, CRYPTO_TFM_REQ_MASK);
592 crypto_skcipher_set_flags(ctx->fallback, tfm->base.crt_flags &
593 CRYPTO_TFM_REQ_MASK);
595 ret = crypto_skcipher_setkey(ctx->fallback, key, keylen);
602 static int omap_aes_ecb_encrypt(struct skcipher_request *req)
604 return omap_aes_crypt(req, FLAGS_ENCRYPT);
607 static int omap_aes_ecb_decrypt(struct skcipher_request *req)
609 return omap_aes_crypt(req, 0);
612 static int omap_aes_cbc_encrypt(struct skcipher_request *req)
614 return omap_aes_crypt(req, FLAGS_ENCRYPT | FLAGS_CBC);
617 static int omap_aes_cbc_decrypt(struct skcipher_request *req)
619 return omap_aes_crypt(req, FLAGS_CBC);
622 static int omap_aes_ctr_encrypt(struct skcipher_request *req)
624 return omap_aes_crypt(req, FLAGS_ENCRYPT | FLAGS_CTR);
627 static int omap_aes_ctr_decrypt(struct skcipher_request *req)
629 return omap_aes_crypt(req, FLAGS_CTR);
632 static int omap_aes_prepare_req(struct crypto_engine *engine,
634 static int omap_aes_crypt_req(struct crypto_engine *engine,
637 static int omap_aes_init_tfm(struct crypto_skcipher *tfm)
639 const char *name = crypto_tfm_alg_name(&tfm->base);
640 struct omap_aes_ctx *ctx = crypto_skcipher_ctx(tfm);
641 struct crypto_skcipher *blk;
643 blk = crypto_alloc_skcipher(name, 0, CRYPTO_ALG_NEED_FALLBACK);
649 crypto_skcipher_set_reqsize(tfm, sizeof(struct omap_aes_reqctx) +
650 crypto_skcipher_reqsize(blk));
652 ctx->enginectx.op.prepare_request = omap_aes_prepare_req;
653 ctx->enginectx.op.unprepare_request = NULL;
654 ctx->enginectx.op.do_one_request = omap_aes_crypt_req;
659 static void omap_aes_exit_tfm(struct crypto_skcipher *tfm)
661 struct omap_aes_ctx *ctx = crypto_skcipher_ctx(tfm);
664 crypto_free_skcipher(ctx->fallback);
666 ctx->fallback = NULL;
669 /* ********************** ALGS ************************************ */
671 static struct skcipher_alg algs_ecb_cbc[] = {
673 .base.cra_name = "ecb(aes)",
674 .base.cra_driver_name = "ecb-aes-omap",
675 .base.cra_priority = 300,
676 .base.cra_flags = CRYPTO_ALG_KERN_DRIVER_ONLY |
678 CRYPTO_ALG_NEED_FALLBACK,
679 .base.cra_blocksize = AES_BLOCK_SIZE,
680 .base.cra_ctxsize = sizeof(struct omap_aes_ctx),
681 .base.cra_module = THIS_MODULE,
683 .min_keysize = AES_MIN_KEY_SIZE,
684 .max_keysize = AES_MAX_KEY_SIZE,
685 .setkey = omap_aes_setkey,
686 .encrypt = omap_aes_ecb_encrypt,
687 .decrypt = omap_aes_ecb_decrypt,
688 .init = omap_aes_init_tfm,
689 .exit = omap_aes_exit_tfm,
692 .base.cra_name = "cbc(aes)",
693 .base.cra_driver_name = "cbc-aes-omap",
694 .base.cra_priority = 300,
695 .base.cra_flags = CRYPTO_ALG_KERN_DRIVER_ONLY |
697 CRYPTO_ALG_NEED_FALLBACK,
698 .base.cra_blocksize = AES_BLOCK_SIZE,
699 .base.cra_ctxsize = sizeof(struct omap_aes_ctx),
700 .base.cra_module = THIS_MODULE,
702 .min_keysize = AES_MIN_KEY_SIZE,
703 .max_keysize = AES_MAX_KEY_SIZE,
704 .ivsize = AES_BLOCK_SIZE,
705 .setkey = omap_aes_setkey,
706 .encrypt = omap_aes_cbc_encrypt,
707 .decrypt = omap_aes_cbc_decrypt,
708 .init = omap_aes_init_tfm,
709 .exit = omap_aes_exit_tfm,
713 static struct skcipher_alg algs_ctr[] = {
715 .base.cra_name = "ctr(aes)",
716 .base.cra_driver_name = "ctr-aes-omap",
717 .base.cra_priority = 300,
718 .base.cra_flags = CRYPTO_ALG_KERN_DRIVER_ONLY |
720 CRYPTO_ALG_NEED_FALLBACK,
721 .base.cra_blocksize = 1,
722 .base.cra_ctxsize = sizeof(struct omap_aes_ctx),
723 .base.cra_module = THIS_MODULE,
725 .min_keysize = AES_MIN_KEY_SIZE,
726 .max_keysize = AES_MAX_KEY_SIZE,
727 .ivsize = AES_BLOCK_SIZE,
728 .setkey = omap_aes_setkey,
729 .encrypt = omap_aes_ctr_encrypt,
730 .decrypt = omap_aes_ctr_decrypt,
731 .init = omap_aes_init_tfm,
732 .exit = omap_aes_exit_tfm,
736 static struct omap_aes_algs_info omap_aes_algs_info_ecb_cbc[] = {
738 .algs_list = algs_ecb_cbc,
739 .size = ARRAY_SIZE(algs_ecb_cbc),
743 static struct aead_alg algs_aead_gcm[] = {
746 .cra_name = "gcm(aes)",
747 .cra_driver_name = "gcm-aes-omap",
749 .cra_flags = CRYPTO_ALG_ASYNC |
750 CRYPTO_ALG_KERN_DRIVER_ONLY,
752 .cra_ctxsize = sizeof(struct omap_aes_gcm_ctx),
753 .cra_alignmask = 0xf,
754 .cra_module = THIS_MODULE,
756 .init = omap_aes_gcm_cra_init,
757 .ivsize = GCM_AES_IV_SIZE,
758 .maxauthsize = AES_BLOCK_SIZE,
759 .setkey = omap_aes_gcm_setkey,
760 .setauthsize = omap_aes_gcm_setauthsize,
761 .encrypt = omap_aes_gcm_encrypt,
762 .decrypt = omap_aes_gcm_decrypt,
766 .cra_name = "rfc4106(gcm(aes))",
767 .cra_driver_name = "rfc4106-gcm-aes-omap",
769 .cra_flags = CRYPTO_ALG_ASYNC |
770 CRYPTO_ALG_KERN_DRIVER_ONLY,
772 .cra_ctxsize = sizeof(struct omap_aes_gcm_ctx),
773 .cra_alignmask = 0xf,
774 .cra_module = THIS_MODULE,
776 .init = omap_aes_gcm_cra_init,
777 .maxauthsize = AES_BLOCK_SIZE,
778 .ivsize = GCM_RFC4106_IV_SIZE,
779 .setkey = omap_aes_4106gcm_setkey,
780 .setauthsize = omap_aes_4106gcm_setauthsize,
781 .encrypt = omap_aes_4106gcm_encrypt,
782 .decrypt = omap_aes_4106gcm_decrypt,
786 static struct omap_aes_aead_algs omap_aes_aead_info = {
787 .algs_list = algs_aead_gcm,
788 .size = ARRAY_SIZE(algs_aead_gcm),
791 static const struct omap_aes_pdata omap_aes_pdata_omap2 = {
792 .algs_info = omap_aes_algs_info_ecb_cbc,
793 .algs_info_size = ARRAY_SIZE(omap_aes_algs_info_ecb_cbc),
794 .trigger = omap_aes_dma_trigger_omap2,
801 .dma_enable_in = BIT(2),
802 .dma_enable_out = BIT(3),
811 static struct omap_aes_algs_info omap_aes_algs_info_ecb_cbc_ctr[] = {
813 .algs_list = algs_ecb_cbc,
814 .size = ARRAY_SIZE(algs_ecb_cbc),
817 .algs_list = algs_ctr,
818 .size = ARRAY_SIZE(algs_ctr),
822 static const struct omap_aes_pdata omap_aes_pdata_omap3 = {
823 .algs_info = omap_aes_algs_info_ecb_cbc_ctr,
824 .algs_info_size = ARRAY_SIZE(omap_aes_algs_info_ecb_cbc_ctr),
825 .trigger = omap_aes_dma_trigger_omap2,
832 .dma_enable_in = BIT(2),
833 .dma_enable_out = BIT(3),
841 static const struct omap_aes_pdata omap_aes_pdata_omap4 = {
842 .algs_info = omap_aes_algs_info_ecb_cbc_ctr,
843 .algs_info_size = ARRAY_SIZE(omap_aes_algs_info_ecb_cbc_ctr),
844 .aead_algs_info = &omap_aes_aead_info,
845 .trigger = omap_aes_dma_trigger_omap4,
852 .irq_status_ofs = 0x8c,
853 .irq_enable_ofs = 0x90,
854 .dma_enable_in = BIT(5),
855 .dma_enable_out = BIT(6),
856 .major_mask = 0x0700,
858 .minor_mask = 0x003f,
862 static irqreturn_t omap_aes_irq(int irq, void *dev_id)
864 struct omap_aes_dev *dd = dev_id;
868 status = omap_aes_read(dd, AES_REG_IRQ_STATUS(dd));
869 if (status & AES_REG_IRQ_DATA_IN) {
870 omap_aes_write(dd, AES_REG_IRQ_ENABLE(dd), 0x0);
874 BUG_ON(_calc_walked(in) > dd->in_sg->length);
876 src = sg_virt(dd->in_sg) + _calc_walked(in);
878 for (i = 0; i < AES_BLOCK_WORDS; i++) {
879 omap_aes_write(dd, AES_REG_DATA_N(dd, i), *src);
881 scatterwalk_advance(&dd->in_walk, 4);
882 if (dd->in_sg->length == _calc_walked(in)) {
883 dd->in_sg = sg_next(dd->in_sg);
885 scatterwalk_start(&dd->in_walk,
887 src = sg_virt(dd->in_sg) +
895 /* Clear IRQ status */
896 status &= ~AES_REG_IRQ_DATA_IN;
897 omap_aes_write(dd, AES_REG_IRQ_STATUS(dd), status);
899 /* Enable DATA_OUT interrupt */
900 omap_aes_write(dd, AES_REG_IRQ_ENABLE(dd), 0x4);
902 } else if (status & AES_REG_IRQ_DATA_OUT) {
903 omap_aes_write(dd, AES_REG_IRQ_ENABLE(dd), 0x0);
907 BUG_ON(_calc_walked(out) > dd->out_sg->length);
909 dst = sg_virt(dd->out_sg) + _calc_walked(out);
911 for (i = 0; i < AES_BLOCK_WORDS; i++) {
912 *dst = omap_aes_read(dd, AES_REG_DATA_N(dd, i));
913 scatterwalk_advance(&dd->out_walk, 4);
914 if (dd->out_sg->length == _calc_walked(out)) {
915 dd->out_sg = sg_next(dd->out_sg);
917 scatterwalk_start(&dd->out_walk,
919 dst = sg_virt(dd->out_sg) +
927 dd->total -= min_t(size_t, AES_BLOCK_SIZE, dd->total);
929 /* Clear IRQ status */
930 status &= ~AES_REG_IRQ_DATA_OUT;
931 omap_aes_write(dd, AES_REG_IRQ_STATUS(dd), status);
934 /* All bytes read! */
935 tasklet_schedule(&dd->done_task);
937 /* Enable DATA_IN interrupt for next block */
938 omap_aes_write(dd, AES_REG_IRQ_ENABLE(dd), 0x2);
944 static const struct of_device_id omap_aes_of_match[] = {
946 .compatible = "ti,omap2-aes",
947 .data = &omap_aes_pdata_omap2,
950 .compatible = "ti,omap3-aes",
951 .data = &omap_aes_pdata_omap3,
954 .compatible = "ti,omap4-aes",
955 .data = &omap_aes_pdata_omap4,
959 MODULE_DEVICE_TABLE(of, omap_aes_of_match);
961 static int omap_aes_get_res_of(struct omap_aes_dev *dd,
962 struct device *dev, struct resource *res)
964 struct device_node *node = dev->of_node;
967 dd->pdata = of_device_get_match_data(dev);
969 dev_err(dev, "no compatible OF match\n");
974 err = of_address_to_resource(node, 0, res);
976 dev_err(dev, "can't translate OF node address\n");
985 static const struct of_device_id omap_aes_of_match[] = {
989 static int omap_aes_get_res_of(struct omap_aes_dev *dd,
990 struct device *dev, struct resource *res)
996 static int omap_aes_get_res_pdev(struct omap_aes_dev *dd,
997 struct platform_device *pdev, struct resource *res)
999 struct device *dev = &pdev->dev;
1003 /* Get the base address */
1004 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1006 dev_err(dev, "no MEM resource info\n");
1010 memcpy(res, r, sizeof(*res));
1012 /* Only OMAP2/3 can be non-DT */
1013 dd->pdata = &omap_aes_pdata_omap2;
1019 static ssize_t fallback_show(struct device *dev, struct device_attribute *attr,
1022 return sprintf(buf, "%d\n", aes_fallback_sz);
1025 static ssize_t fallback_store(struct device *dev, struct device_attribute *attr,
1026 const char *buf, size_t size)
1031 status = kstrtol(buf, 0, &value);
1035 /* HW accelerator only works with buffers > 9 */
1037 dev_err(dev, "minimum fallback size 9\n");
1041 aes_fallback_sz = value;
1046 static ssize_t queue_len_show(struct device *dev, struct device_attribute *attr,
1049 struct omap_aes_dev *dd = dev_get_drvdata(dev);
1051 return sprintf(buf, "%d\n", dd->engine->queue.max_qlen);
1054 static ssize_t queue_len_store(struct device *dev,
1055 struct device_attribute *attr, const char *buf,
1058 struct omap_aes_dev *dd;
1061 unsigned long flags;
1063 status = kstrtol(buf, 0, &value);
1071 * Changing the queue size in fly is safe, if size becomes smaller
1072 * than current size, it will just not accept new entries until
1073 * it has shrank enough.
1075 spin_lock_bh(&list_lock);
1076 list_for_each_entry(dd, &dev_list, list) {
1077 spin_lock_irqsave(&dd->lock, flags);
1078 dd->engine->queue.max_qlen = value;
1079 dd->aead_queue.base.max_qlen = value;
1080 spin_unlock_irqrestore(&dd->lock, flags);
1082 spin_unlock_bh(&list_lock);
1087 static DEVICE_ATTR_RW(queue_len);
1088 static DEVICE_ATTR_RW(fallback);
1090 static struct attribute *omap_aes_attrs[] = {
1091 &dev_attr_queue_len.attr,
1092 &dev_attr_fallback.attr,
1096 static struct attribute_group omap_aes_attr_group = {
1097 .attrs = omap_aes_attrs,
1100 static int omap_aes_probe(struct platform_device *pdev)
1102 struct device *dev = &pdev->dev;
1103 struct omap_aes_dev *dd;
1104 struct skcipher_alg *algp;
1105 struct aead_alg *aalg;
1106 struct resource res;
1107 int err = -ENOMEM, i, j, irq = -1;
1110 dd = devm_kzalloc(dev, sizeof(struct omap_aes_dev), GFP_KERNEL);
1112 dev_err(dev, "unable to alloc data struct.\n");
1116 platform_set_drvdata(pdev, dd);
1118 aead_init_queue(&dd->aead_queue, OMAP_AES_QUEUE_LENGTH);
1120 err = (dev->of_node) ? omap_aes_get_res_of(dd, dev, &res) :
1121 omap_aes_get_res_pdev(dd, pdev, &res);
1125 dd->io_base = devm_ioremap_resource(dev, &res);
1126 if (IS_ERR(dd->io_base)) {
1127 err = PTR_ERR(dd->io_base);
1130 dd->phys_base = res.start;
1132 pm_runtime_use_autosuspend(dev);
1133 pm_runtime_set_autosuspend_delay(dev, DEFAULT_AUTOSUSPEND_DELAY);
1135 pm_runtime_enable(dev);
1136 err = pm_runtime_resume_and_get(dev);
1138 dev_err(dev, "%s: failed to get_sync(%d)\n",
1140 goto err_pm_disable;
1143 omap_aes_dma_stop(dd);
1145 reg = omap_aes_read(dd, AES_REG_REV(dd));
1147 pm_runtime_put_sync(dev);
1149 dev_info(dev, "OMAP AES hw accel rev: %u.%u\n",
1150 (reg & dd->pdata->major_mask) >> dd->pdata->major_shift,
1151 (reg & dd->pdata->minor_mask) >> dd->pdata->minor_shift);
1153 tasklet_init(&dd->done_task, omap_aes_done_task, (unsigned long)dd);
1155 err = omap_aes_dma_init(dd);
1156 if (err == -EPROBE_DEFER) {
1158 } else if (err && AES_REG_IRQ_STATUS(dd) && AES_REG_IRQ_ENABLE(dd)) {
1161 irq = platform_get_irq(pdev, 0);
1167 err = devm_request_irq(dev, irq, omap_aes_irq, 0,
1170 dev_err(dev, "Unable to grab omap-aes IRQ\n");
1175 spin_lock_init(&dd->lock);
1177 INIT_LIST_HEAD(&dd->list);
1178 spin_lock_bh(&list_lock);
1179 list_add_tail(&dd->list, &dev_list);
1180 spin_unlock_bh(&list_lock);
1182 /* Initialize crypto engine */
1183 dd->engine = crypto_engine_alloc_init(dev, 1);
1189 err = crypto_engine_start(dd->engine);
1193 for (i = 0; i < dd->pdata->algs_info_size; i++) {
1194 if (!dd->pdata->algs_info[i].registered) {
1195 for (j = 0; j < dd->pdata->algs_info[i].size; j++) {
1196 algp = &dd->pdata->algs_info[i].algs_list[j];
1198 pr_debug("reg alg: %s\n", algp->base.cra_name);
1200 err = crypto_register_skcipher(algp);
1204 dd->pdata->algs_info[i].registered++;
1209 if (dd->pdata->aead_algs_info &&
1210 !dd->pdata->aead_algs_info->registered) {
1211 for (i = 0; i < dd->pdata->aead_algs_info->size; i++) {
1212 aalg = &dd->pdata->aead_algs_info->algs_list[i];
1214 pr_debug("reg alg: %s\n", aalg->base.cra_name);
1216 err = crypto_register_aead(aalg);
1220 dd->pdata->aead_algs_info->registered++;
1224 err = sysfs_create_group(&dev->kobj, &omap_aes_attr_group);
1226 dev_err(dev, "could not create sysfs device attrs\n");
1232 for (i = dd->pdata->aead_algs_info->registered - 1; i >= 0; i--) {
1233 aalg = &dd->pdata->aead_algs_info->algs_list[i];
1234 crypto_unregister_aead(aalg);
1237 for (i = dd->pdata->algs_info_size - 1; i >= 0; i--)
1238 for (j = dd->pdata->algs_info[i].registered - 1; j >= 0; j--)
1239 crypto_unregister_skcipher(
1240 &dd->pdata->algs_info[i].algs_list[j]);
1244 crypto_engine_exit(dd->engine);
1246 omap_aes_dma_cleanup(dd);
1248 tasklet_kill(&dd->done_task);
1250 pm_runtime_disable(dev);
1254 dev_err(dev, "initialization failed.\n");
1258 static int omap_aes_remove(struct platform_device *pdev)
1260 struct omap_aes_dev *dd = platform_get_drvdata(pdev);
1261 struct aead_alg *aalg;
1267 spin_lock_bh(&list_lock);
1268 list_del(&dd->list);
1269 spin_unlock_bh(&list_lock);
1271 for (i = dd->pdata->algs_info_size - 1; i >= 0; i--)
1272 for (j = dd->pdata->algs_info[i].registered - 1; j >= 0; j--) {
1273 crypto_unregister_skcipher(
1274 &dd->pdata->algs_info[i].algs_list[j]);
1275 dd->pdata->algs_info[i].registered--;
1278 for (i = dd->pdata->aead_algs_info->registered - 1; i >= 0; i--) {
1279 aalg = &dd->pdata->aead_algs_info->algs_list[i];
1280 crypto_unregister_aead(aalg);
1281 dd->pdata->aead_algs_info->registered--;
1285 crypto_engine_exit(dd->engine);
1287 tasklet_kill(&dd->done_task);
1288 omap_aes_dma_cleanup(dd);
1289 pm_runtime_disable(dd->dev);
1291 sysfs_remove_group(&dd->dev->kobj, &omap_aes_attr_group);
1296 #ifdef CONFIG_PM_SLEEP
1297 static int omap_aes_suspend(struct device *dev)
1299 pm_runtime_put_sync(dev);
1303 static int omap_aes_resume(struct device *dev)
1305 pm_runtime_resume_and_get(dev);
1310 static SIMPLE_DEV_PM_OPS(omap_aes_pm_ops, omap_aes_suspend, omap_aes_resume);
1312 static struct platform_driver omap_aes_driver = {
1313 .probe = omap_aes_probe,
1314 .remove = omap_aes_remove,
1317 .pm = &omap_aes_pm_ops,
1318 .of_match_table = omap_aes_of_match,
1322 module_platform_driver(omap_aes_driver);
1324 MODULE_DESCRIPTION("OMAP AES hw acceleration support.");
1325 MODULE_LICENSE("GPL v2");
1326 MODULE_AUTHOR("Dmitry Kasatkin");