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_get_sync(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 __le32_to_cpu(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;
273 struct dma_slave_config cfg;
277 scatterwalk_start(&dd->in_walk, dd->in_sg);
278 scatterwalk_start(&dd->out_walk, dd->out_sg);
280 /* Enable DATAIN interrupt and let it take
282 omap_aes_write(dd, AES_REG_IRQ_ENABLE(dd), 0x2);
286 dma_sync_sg_for_device(dd->dev, dd->in_sg, in_sg_len, DMA_TO_DEVICE);
288 memset(&cfg, 0, sizeof(cfg));
290 cfg.src_addr = dd->phys_base + AES_REG_DATA_N(dd, 0);
291 cfg.dst_addr = dd->phys_base + AES_REG_DATA_N(dd, 0);
292 cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
293 cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
294 cfg.src_maxburst = DST_MAXBURST;
295 cfg.dst_maxburst = DST_MAXBURST;
298 ret = dmaengine_slave_config(dd->dma_lch_in, &cfg);
300 dev_err(dd->dev, "can't configure IN dmaengine slave: %d\n",
305 tx_in = dmaengine_prep_slave_sg(dd->dma_lch_in, in_sg, in_sg_len,
307 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
309 dev_err(dd->dev, "IN prep_slave_sg() failed\n");
313 /* No callback necessary */
314 tx_in->callback_param = dd;
317 ret = dmaengine_slave_config(dd->dma_lch_out, &cfg);
319 dev_err(dd->dev, "can't configure OUT dmaengine slave: %d\n",
324 tx_out = dmaengine_prep_slave_sg(dd->dma_lch_out, out_sg, out_sg_len,
326 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
328 dev_err(dd->dev, "OUT prep_slave_sg() failed\n");
332 if (dd->flags & FLAGS_GCM)
333 tx_out->callback = omap_aes_gcm_dma_out_callback;
335 tx_out->callback = omap_aes_dma_out_callback;
336 tx_out->callback_param = dd;
338 dmaengine_submit(tx_in);
339 dmaengine_submit(tx_out);
341 dma_async_issue_pending(dd->dma_lch_in);
342 dma_async_issue_pending(dd->dma_lch_out);
345 dd->pdata->trigger(dd, dd->total);
350 int omap_aes_crypt_dma_start(struct omap_aes_dev *dd)
354 pr_debug("total: %d\n", dd->total);
357 err = dma_map_sg(dd->dev, dd->in_sg, dd->in_sg_len,
360 dev_err(dd->dev, "dma_map_sg() error\n");
364 err = dma_map_sg(dd->dev, dd->out_sg, dd->out_sg_len,
367 dev_err(dd->dev, "dma_map_sg() error\n");
372 err = omap_aes_crypt_dma(dd, dd->in_sg, dd->out_sg, dd->in_sg_len,
374 if (err && !dd->pio_only) {
375 dma_unmap_sg(dd->dev, dd->in_sg, dd->in_sg_len, DMA_TO_DEVICE);
376 dma_unmap_sg(dd->dev, dd->out_sg, dd->out_sg_len,
383 static void omap_aes_finish_req(struct omap_aes_dev *dd, int err)
385 struct skcipher_request *req = dd->req;
387 pr_debug("err: %d\n", err);
389 crypto_finalize_skcipher_request(dd->engine, req, err);
391 pm_runtime_mark_last_busy(dd->dev);
392 pm_runtime_put_autosuspend(dd->dev);
395 int omap_aes_crypt_dma_stop(struct omap_aes_dev *dd)
397 pr_debug("total: %d\n", dd->total);
399 omap_aes_dma_stop(dd);
405 static int omap_aes_handle_queue(struct omap_aes_dev *dd,
406 struct skcipher_request *req)
409 return crypto_transfer_skcipher_request_to_engine(dd->engine, req);
414 static int omap_aes_prepare_req(struct crypto_engine *engine,
417 struct skcipher_request *req = container_of(areq, struct skcipher_request, base);
418 struct omap_aes_ctx *ctx = crypto_skcipher_ctx(
419 crypto_skcipher_reqtfm(req));
420 struct omap_aes_reqctx *rctx = skcipher_request_ctx(req);
421 struct omap_aes_dev *dd = rctx->dd;
428 /* assign new request to device */
430 dd->total = req->cryptlen;
431 dd->total_save = req->cryptlen;
432 dd->in_sg = req->src;
433 dd->out_sg = req->dst;
434 dd->orig_out = req->dst;
436 flags = OMAP_CRYPTO_COPY_DATA;
437 if (req->src == req->dst)
438 flags |= OMAP_CRYPTO_FORCE_COPY;
440 ret = omap_crypto_align_sg(&dd->in_sg, dd->total, AES_BLOCK_SIZE,
442 FLAGS_IN_DATA_ST_SHIFT, &dd->flags);
446 ret = omap_crypto_align_sg(&dd->out_sg, dd->total, AES_BLOCK_SIZE,
448 FLAGS_OUT_DATA_ST_SHIFT, &dd->flags);
452 dd->in_sg_len = sg_nents_for_len(dd->in_sg, dd->total);
453 if (dd->in_sg_len < 0)
454 return dd->in_sg_len;
456 dd->out_sg_len = sg_nents_for_len(dd->out_sg, dd->total);
457 if (dd->out_sg_len < 0)
458 return dd->out_sg_len;
460 rctx->mode &= FLAGS_MODE_MASK;
461 dd->flags = (dd->flags & ~FLAGS_MODE_MASK) | rctx->mode;
466 return omap_aes_write_ctrl(dd);
469 static int omap_aes_crypt_req(struct crypto_engine *engine,
472 struct skcipher_request *req = container_of(areq, struct skcipher_request, base);
473 struct omap_aes_reqctx *rctx = skcipher_request_ctx(req);
474 struct omap_aes_dev *dd = rctx->dd;
479 return omap_aes_crypt_dma_start(dd);
482 static void omap_aes_done_task(unsigned long data)
484 struct omap_aes_dev *dd = (struct omap_aes_dev *)data;
486 pr_debug("enter done_task\n");
489 dma_sync_sg_for_device(dd->dev, dd->out_sg, dd->out_sg_len,
491 dma_unmap_sg(dd->dev, dd->in_sg, dd->in_sg_len, DMA_TO_DEVICE);
492 dma_unmap_sg(dd->dev, dd->out_sg, dd->out_sg_len,
494 omap_aes_crypt_dma_stop(dd);
497 omap_crypto_cleanup(dd->in_sgl, NULL, 0, dd->total_save,
498 FLAGS_IN_DATA_ST_SHIFT, dd->flags);
500 omap_crypto_cleanup(&dd->out_sgl, dd->orig_out, 0, dd->total_save,
501 FLAGS_OUT_DATA_ST_SHIFT, dd->flags);
503 omap_aes_finish_req(dd, 0);
508 static int omap_aes_crypt(struct skcipher_request *req, unsigned long mode)
510 struct omap_aes_ctx *ctx = crypto_skcipher_ctx(
511 crypto_skcipher_reqtfm(req));
512 struct omap_aes_reqctx *rctx = skcipher_request_ctx(req);
513 struct omap_aes_dev *dd;
516 pr_debug("nbytes: %d, enc: %d, cbc: %d\n", req->cryptlen,
517 !!(mode & FLAGS_ENCRYPT),
518 !!(mode & FLAGS_CBC));
520 if (req->cryptlen < aes_fallback_sz) {
521 SYNC_SKCIPHER_REQUEST_ON_STACK(subreq, ctx->fallback);
523 skcipher_request_set_sync_tfm(subreq, ctx->fallback);
524 skcipher_request_set_callback(subreq, req->base.flags, NULL,
526 skcipher_request_set_crypt(subreq, req->src, req->dst,
527 req->cryptlen, req->iv);
529 if (mode & FLAGS_ENCRYPT)
530 ret = crypto_skcipher_encrypt(subreq);
532 ret = crypto_skcipher_decrypt(subreq);
534 skcipher_request_zero(subreq);
537 dd = omap_aes_find_dev(rctx);
543 return omap_aes_handle_queue(dd, req);
546 /* ********************** ALG API ************************************ */
548 static int omap_aes_setkey(struct crypto_skcipher *tfm, const u8 *key,
551 struct omap_aes_ctx *ctx = crypto_skcipher_ctx(tfm);
554 if (keylen != AES_KEYSIZE_128 && keylen != AES_KEYSIZE_192 &&
555 keylen != AES_KEYSIZE_256)
558 pr_debug("enter, keylen: %d\n", keylen);
560 memcpy(ctx->key, key, keylen);
561 ctx->keylen = keylen;
563 crypto_sync_skcipher_clear_flags(ctx->fallback, CRYPTO_TFM_REQ_MASK);
564 crypto_sync_skcipher_set_flags(ctx->fallback, tfm->base.crt_flags &
565 CRYPTO_TFM_REQ_MASK);
567 ret = crypto_sync_skcipher_setkey(ctx->fallback, key, keylen);
574 static int omap_aes_ecb_encrypt(struct skcipher_request *req)
576 return omap_aes_crypt(req, FLAGS_ENCRYPT);
579 static int omap_aes_ecb_decrypt(struct skcipher_request *req)
581 return omap_aes_crypt(req, 0);
584 static int omap_aes_cbc_encrypt(struct skcipher_request *req)
586 return omap_aes_crypt(req, FLAGS_ENCRYPT | FLAGS_CBC);
589 static int omap_aes_cbc_decrypt(struct skcipher_request *req)
591 return omap_aes_crypt(req, FLAGS_CBC);
594 static int omap_aes_ctr_encrypt(struct skcipher_request *req)
596 return omap_aes_crypt(req, FLAGS_ENCRYPT | FLAGS_CTR);
599 static int omap_aes_ctr_decrypt(struct skcipher_request *req)
601 return omap_aes_crypt(req, FLAGS_CTR);
604 static int omap_aes_prepare_req(struct crypto_engine *engine,
606 static int omap_aes_crypt_req(struct crypto_engine *engine,
609 static int omap_aes_init_tfm(struct crypto_skcipher *tfm)
611 const char *name = crypto_tfm_alg_name(&tfm->base);
612 struct omap_aes_ctx *ctx = crypto_skcipher_ctx(tfm);
613 struct crypto_sync_skcipher *blk;
615 blk = crypto_alloc_sync_skcipher(name, 0, CRYPTO_ALG_NEED_FALLBACK);
621 crypto_skcipher_set_reqsize(tfm, sizeof(struct omap_aes_reqctx));
623 ctx->enginectx.op.prepare_request = omap_aes_prepare_req;
624 ctx->enginectx.op.unprepare_request = NULL;
625 ctx->enginectx.op.do_one_request = omap_aes_crypt_req;
630 static int omap_aes_gcm_cra_init(struct crypto_aead *tfm)
632 struct omap_aes_dev *dd = NULL;
633 struct omap_aes_ctx *ctx = crypto_aead_ctx(tfm);
636 /* Find AES device, currently picks the first device */
637 spin_lock_bh(&list_lock);
638 list_for_each_entry(dd, &dev_list, list) {
641 spin_unlock_bh(&list_lock);
643 err = pm_runtime_get_sync(dd->dev);
645 dev_err(dd->dev, "%s: failed to get_sync(%d)\n",
650 tfm->reqsize = sizeof(struct omap_aes_reqctx);
651 ctx->ctr = crypto_alloc_skcipher("ecb(aes)", 0, 0);
652 if (IS_ERR(ctx->ctr)) {
653 pr_warn("could not load aes driver for encrypting IV\n");
654 return PTR_ERR(ctx->ctr);
660 static void omap_aes_exit_tfm(struct crypto_skcipher *tfm)
662 struct omap_aes_ctx *ctx = crypto_skcipher_ctx(tfm);
665 crypto_free_sync_skcipher(ctx->fallback);
667 ctx->fallback = NULL;
670 static void omap_aes_gcm_cra_exit(struct crypto_aead *tfm)
672 struct omap_aes_ctx *ctx = crypto_aead_ctx(tfm);
675 crypto_free_sync_skcipher(ctx->fallback);
677 ctx->fallback = NULL;
680 crypto_free_skcipher(ctx->ctr);
683 /* ********************** ALGS ************************************ */
685 static struct skcipher_alg algs_ecb_cbc[] = {
687 .base.cra_name = "ecb(aes)",
688 .base.cra_driver_name = "ecb-aes-omap",
689 .base.cra_priority = 300,
690 .base.cra_flags = CRYPTO_ALG_KERN_DRIVER_ONLY |
692 CRYPTO_ALG_NEED_FALLBACK,
693 .base.cra_blocksize = AES_BLOCK_SIZE,
694 .base.cra_ctxsize = sizeof(struct omap_aes_ctx),
695 .base.cra_module = THIS_MODULE,
697 .min_keysize = AES_MIN_KEY_SIZE,
698 .max_keysize = AES_MAX_KEY_SIZE,
699 .setkey = omap_aes_setkey,
700 .encrypt = omap_aes_ecb_encrypt,
701 .decrypt = omap_aes_ecb_decrypt,
702 .init = omap_aes_init_tfm,
703 .exit = omap_aes_exit_tfm,
706 .base.cra_name = "cbc(aes)",
707 .base.cra_driver_name = "cbc-aes-omap",
708 .base.cra_priority = 300,
709 .base.cra_flags = CRYPTO_ALG_KERN_DRIVER_ONLY |
711 CRYPTO_ALG_NEED_FALLBACK,
712 .base.cra_blocksize = AES_BLOCK_SIZE,
713 .base.cra_ctxsize = sizeof(struct omap_aes_ctx),
714 .base.cra_module = THIS_MODULE,
716 .min_keysize = AES_MIN_KEY_SIZE,
717 .max_keysize = AES_MAX_KEY_SIZE,
718 .ivsize = AES_BLOCK_SIZE,
719 .setkey = omap_aes_setkey,
720 .encrypt = omap_aes_cbc_encrypt,
721 .decrypt = omap_aes_cbc_decrypt,
722 .init = omap_aes_init_tfm,
723 .exit = omap_aes_exit_tfm,
727 static struct skcipher_alg algs_ctr[] = {
729 .base.cra_name = "ctr(aes)",
730 .base.cra_driver_name = "ctr-aes-omap",
731 .base.cra_priority = 300,
732 .base.cra_flags = CRYPTO_ALG_KERN_DRIVER_ONLY |
734 CRYPTO_ALG_NEED_FALLBACK,
735 .base.cra_blocksize = AES_BLOCK_SIZE,
736 .base.cra_ctxsize = sizeof(struct omap_aes_ctx),
737 .base.cra_module = THIS_MODULE,
739 .min_keysize = AES_MIN_KEY_SIZE,
740 .max_keysize = AES_MAX_KEY_SIZE,
741 .ivsize = AES_BLOCK_SIZE,
742 .setkey = omap_aes_setkey,
743 .encrypt = omap_aes_ctr_encrypt,
744 .decrypt = omap_aes_ctr_decrypt,
745 .init = omap_aes_init_tfm,
746 .exit = omap_aes_exit_tfm,
750 static struct omap_aes_algs_info omap_aes_algs_info_ecb_cbc[] = {
752 .algs_list = algs_ecb_cbc,
753 .size = ARRAY_SIZE(algs_ecb_cbc),
757 static struct aead_alg algs_aead_gcm[] = {
760 .cra_name = "gcm(aes)",
761 .cra_driver_name = "gcm-aes-omap",
763 .cra_flags = CRYPTO_ALG_ASYNC |
764 CRYPTO_ALG_KERN_DRIVER_ONLY,
766 .cra_ctxsize = sizeof(struct omap_aes_ctx),
767 .cra_alignmask = 0xf,
768 .cra_module = THIS_MODULE,
770 .init = omap_aes_gcm_cra_init,
771 .exit = omap_aes_gcm_cra_exit,
772 .ivsize = GCM_AES_IV_SIZE,
773 .maxauthsize = AES_BLOCK_SIZE,
774 .setkey = omap_aes_gcm_setkey,
775 .encrypt = omap_aes_gcm_encrypt,
776 .decrypt = omap_aes_gcm_decrypt,
780 .cra_name = "rfc4106(gcm(aes))",
781 .cra_driver_name = "rfc4106-gcm-aes-omap",
783 .cra_flags = CRYPTO_ALG_ASYNC |
784 CRYPTO_ALG_KERN_DRIVER_ONLY,
786 .cra_ctxsize = sizeof(struct omap_aes_ctx),
787 .cra_alignmask = 0xf,
788 .cra_module = THIS_MODULE,
790 .init = omap_aes_gcm_cra_init,
791 .exit = omap_aes_gcm_cra_exit,
792 .maxauthsize = AES_BLOCK_SIZE,
793 .ivsize = GCM_RFC4106_IV_SIZE,
794 .setkey = omap_aes_4106gcm_setkey,
795 .encrypt = omap_aes_4106gcm_encrypt,
796 .decrypt = omap_aes_4106gcm_decrypt,
800 static struct omap_aes_aead_algs omap_aes_aead_info = {
801 .algs_list = algs_aead_gcm,
802 .size = ARRAY_SIZE(algs_aead_gcm),
805 static const struct omap_aes_pdata omap_aes_pdata_omap2 = {
806 .algs_info = omap_aes_algs_info_ecb_cbc,
807 .algs_info_size = ARRAY_SIZE(omap_aes_algs_info_ecb_cbc),
808 .trigger = omap_aes_dma_trigger_omap2,
815 .dma_enable_in = BIT(2),
816 .dma_enable_out = BIT(3),
825 static struct omap_aes_algs_info omap_aes_algs_info_ecb_cbc_ctr[] = {
827 .algs_list = algs_ecb_cbc,
828 .size = ARRAY_SIZE(algs_ecb_cbc),
831 .algs_list = algs_ctr,
832 .size = ARRAY_SIZE(algs_ctr),
836 static const struct omap_aes_pdata omap_aes_pdata_omap3 = {
837 .algs_info = omap_aes_algs_info_ecb_cbc_ctr,
838 .algs_info_size = ARRAY_SIZE(omap_aes_algs_info_ecb_cbc_ctr),
839 .trigger = omap_aes_dma_trigger_omap2,
846 .dma_enable_in = BIT(2),
847 .dma_enable_out = BIT(3),
855 static const struct omap_aes_pdata omap_aes_pdata_omap4 = {
856 .algs_info = omap_aes_algs_info_ecb_cbc_ctr,
857 .algs_info_size = ARRAY_SIZE(omap_aes_algs_info_ecb_cbc_ctr),
858 .aead_algs_info = &omap_aes_aead_info,
859 .trigger = omap_aes_dma_trigger_omap4,
866 .irq_status_ofs = 0x8c,
867 .irq_enable_ofs = 0x90,
868 .dma_enable_in = BIT(5),
869 .dma_enable_out = BIT(6),
870 .major_mask = 0x0700,
872 .minor_mask = 0x003f,
876 static irqreturn_t omap_aes_irq(int irq, void *dev_id)
878 struct omap_aes_dev *dd = dev_id;
882 status = omap_aes_read(dd, AES_REG_IRQ_STATUS(dd));
883 if (status & AES_REG_IRQ_DATA_IN) {
884 omap_aes_write(dd, AES_REG_IRQ_ENABLE(dd), 0x0);
888 BUG_ON(_calc_walked(in) > dd->in_sg->length);
890 src = sg_virt(dd->in_sg) + _calc_walked(in);
892 for (i = 0; i < AES_BLOCK_WORDS; i++) {
893 omap_aes_write(dd, AES_REG_DATA_N(dd, i), *src);
895 scatterwalk_advance(&dd->in_walk, 4);
896 if (dd->in_sg->length == _calc_walked(in)) {
897 dd->in_sg = sg_next(dd->in_sg);
899 scatterwalk_start(&dd->in_walk,
901 src = sg_virt(dd->in_sg) +
909 /* Clear IRQ status */
910 status &= ~AES_REG_IRQ_DATA_IN;
911 omap_aes_write(dd, AES_REG_IRQ_STATUS(dd), status);
913 /* Enable DATA_OUT interrupt */
914 omap_aes_write(dd, AES_REG_IRQ_ENABLE(dd), 0x4);
916 } else if (status & AES_REG_IRQ_DATA_OUT) {
917 omap_aes_write(dd, AES_REG_IRQ_ENABLE(dd), 0x0);
921 BUG_ON(_calc_walked(out) > dd->out_sg->length);
923 dst = sg_virt(dd->out_sg) + _calc_walked(out);
925 for (i = 0; i < AES_BLOCK_WORDS; i++) {
926 *dst = omap_aes_read(dd, AES_REG_DATA_N(dd, i));
927 scatterwalk_advance(&dd->out_walk, 4);
928 if (dd->out_sg->length == _calc_walked(out)) {
929 dd->out_sg = sg_next(dd->out_sg);
931 scatterwalk_start(&dd->out_walk,
933 dst = sg_virt(dd->out_sg) +
941 dd->total -= min_t(size_t, AES_BLOCK_SIZE, dd->total);
943 /* Clear IRQ status */
944 status &= ~AES_REG_IRQ_DATA_OUT;
945 omap_aes_write(dd, AES_REG_IRQ_STATUS(dd), status);
948 /* All bytes read! */
949 tasklet_schedule(&dd->done_task);
951 /* Enable DATA_IN interrupt for next block */
952 omap_aes_write(dd, AES_REG_IRQ_ENABLE(dd), 0x2);
958 static const struct of_device_id omap_aes_of_match[] = {
960 .compatible = "ti,omap2-aes",
961 .data = &omap_aes_pdata_omap2,
964 .compatible = "ti,omap3-aes",
965 .data = &omap_aes_pdata_omap3,
968 .compatible = "ti,omap4-aes",
969 .data = &omap_aes_pdata_omap4,
973 MODULE_DEVICE_TABLE(of, omap_aes_of_match);
975 static int omap_aes_get_res_of(struct omap_aes_dev *dd,
976 struct device *dev, struct resource *res)
978 struct device_node *node = dev->of_node;
981 dd->pdata = of_device_get_match_data(dev);
983 dev_err(dev, "no compatible OF match\n");
988 err = of_address_to_resource(node, 0, res);
990 dev_err(dev, "can't translate OF node address\n");
999 static const struct of_device_id omap_aes_of_match[] = {
1003 static int omap_aes_get_res_of(struct omap_aes_dev *dd,
1004 struct device *dev, struct resource *res)
1010 static int omap_aes_get_res_pdev(struct omap_aes_dev *dd,
1011 struct platform_device *pdev, struct resource *res)
1013 struct device *dev = &pdev->dev;
1017 /* Get the base address */
1018 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1020 dev_err(dev, "no MEM resource info\n");
1024 memcpy(res, r, sizeof(*res));
1026 /* Only OMAP2/3 can be non-DT */
1027 dd->pdata = &omap_aes_pdata_omap2;
1033 static ssize_t fallback_show(struct device *dev, struct device_attribute *attr,
1036 return sprintf(buf, "%d\n", aes_fallback_sz);
1039 static ssize_t fallback_store(struct device *dev, struct device_attribute *attr,
1040 const char *buf, size_t size)
1045 status = kstrtol(buf, 0, &value);
1049 /* HW accelerator only works with buffers > 9 */
1051 dev_err(dev, "minimum fallback size 9\n");
1055 aes_fallback_sz = value;
1060 static ssize_t queue_len_show(struct device *dev, struct device_attribute *attr,
1063 struct omap_aes_dev *dd = dev_get_drvdata(dev);
1065 return sprintf(buf, "%d\n", dd->engine->queue.max_qlen);
1068 static ssize_t queue_len_store(struct device *dev,
1069 struct device_attribute *attr, const char *buf,
1072 struct omap_aes_dev *dd;
1075 unsigned long flags;
1077 status = kstrtol(buf, 0, &value);
1085 * Changing the queue size in fly is safe, if size becomes smaller
1086 * than current size, it will just not accept new entries until
1087 * it has shrank enough.
1089 spin_lock_bh(&list_lock);
1090 list_for_each_entry(dd, &dev_list, list) {
1091 spin_lock_irqsave(&dd->lock, flags);
1092 dd->engine->queue.max_qlen = value;
1093 dd->aead_queue.base.max_qlen = value;
1094 spin_unlock_irqrestore(&dd->lock, flags);
1096 spin_unlock_bh(&list_lock);
1101 static DEVICE_ATTR_RW(queue_len);
1102 static DEVICE_ATTR_RW(fallback);
1104 static struct attribute *omap_aes_attrs[] = {
1105 &dev_attr_queue_len.attr,
1106 &dev_attr_fallback.attr,
1110 static struct attribute_group omap_aes_attr_group = {
1111 .attrs = omap_aes_attrs,
1114 static int omap_aes_probe(struct platform_device *pdev)
1116 struct device *dev = &pdev->dev;
1117 struct omap_aes_dev *dd;
1118 struct skcipher_alg *algp;
1119 struct aead_alg *aalg;
1120 struct resource res;
1121 int err = -ENOMEM, i, j, irq = -1;
1124 dd = devm_kzalloc(dev, sizeof(struct omap_aes_dev), GFP_KERNEL);
1126 dev_err(dev, "unable to alloc data struct.\n");
1130 platform_set_drvdata(pdev, dd);
1132 aead_init_queue(&dd->aead_queue, OMAP_AES_QUEUE_LENGTH);
1134 err = (dev->of_node) ? omap_aes_get_res_of(dd, dev, &res) :
1135 omap_aes_get_res_pdev(dd, pdev, &res);
1139 dd->io_base = devm_ioremap_resource(dev, &res);
1140 if (IS_ERR(dd->io_base)) {
1141 err = PTR_ERR(dd->io_base);
1144 dd->phys_base = res.start;
1146 pm_runtime_use_autosuspend(dev);
1147 pm_runtime_set_autosuspend_delay(dev, DEFAULT_AUTOSUSPEND_DELAY);
1149 pm_runtime_enable(dev);
1150 err = pm_runtime_get_sync(dev);
1152 dev_err(dev, "%s: failed to get_sync(%d)\n",
1157 omap_aes_dma_stop(dd);
1159 reg = omap_aes_read(dd, AES_REG_REV(dd));
1161 pm_runtime_put_sync(dev);
1163 dev_info(dev, "OMAP AES hw accel rev: %u.%u\n",
1164 (reg & dd->pdata->major_mask) >> dd->pdata->major_shift,
1165 (reg & dd->pdata->minor_mask) >> dd->pdata->minor_shift);
1167 tasklet_init(&dd->done_task, omap_aes_done_task, (unsigned long)dd);
1169 err = omap_aes_dma_init(dd);
1170 if (err == -EPROBE_DEFER) {
1172 } else if (err && AES_REG_IRQ_STATUS(dd) && AES_REG_IRQ_ENABLE(dd)) {
1175 irq = platform_get_irq(pdev, 0);
1181 err = devm_request_irq(dev, irq, omap_aes_irq, 0,
1184 dev_err(dev, "Unable to grab omap-aes IRQ\n");
1189 spin_lock_init(&dd->lock);
1191 INIT_LIST_HEAD(&dd->list);
1192 spin_lock(&list_lock);
1193 list_add_tail(&dd->list, &dev_list);
1194 spin_unlock(&list_lock);
1196 /* Initialize crypto engine */
1197 dd->engine = crypto_engine_alloc_init(dev, 1);
1203 err = crypto_engine_start(dd->engine);
1207 for (i = 0; i < dd->pdata->algs_info_size; i++) {
1208 if (!dd->pdata->algs_info[i].registered) {
1209 for (j = 0; j < dd->pdata->algs_info[i].size; j++) {
1210 algp = &dd->pdata->algs_info[i].algs_list[j];
1212 pr_debug("reg alg: %s\n", algp->base.cra_name);
1214 err = crypto_register_skcipher(algp);
1218 dd->pdata->algs_info[i].registered++;
1223 if (dd->pdata->aead_algs_info &&
1224 !dd->pdata->aead_algs_info->registered) {
1225 for (i = 0; i < dd->pdata->aead_algs_info->size; i++) {
1226 aalg = &dd->pdata->aead_algs_info->algs_list[i];
1228 pr_debug("reg alg: %s\n", aalg->base.cra_name);
1230 err = crypto_register_aead(aalg);
1234 dd->pdata->aead_algs_info->registered++;
1238 err = sysfs_create_group(&dev->kobj, &omap_aes_attr_group);
1240 dev_err(dev, "could not create sysfs device attrs\n");
1246 for (i = dd->pdata->aead_algs_info->registered - 1; i >= 0; i--) {
1247 aalg = &dd->pdata->aead_algs_info->algs_list[i];
1248 crypto_unregister_aead(aalg);
1251 for (i = dd->pdata->algs_info_size - 1; i >= 0; i--)
1252 for (j = dd->pdata->algs_info[i].registered - 1; j >= 0; j--)
1253 crypto_unregister_skcipher(
1254 &dd->pdata->algs_info[i].algs_list[j]);
1258 crypto_engine_exit(dd->engine);
1260 omap_aes_dma_cleanup(dd);
1262 tasklet_kill(&dd->done_task);
1263 pm_runtime_disable(dev);
1267 dev_err(dev, "initialization failed.\n");
1271 static int omap_aes_remove(struct platform_device *pdev)
1273 struct omap_aes_dev *dd = platform_get_drvdata(pdev);
1274 struct aead_alg *aalg;
1280 spin_lock(&list_lock);
1281 list_del(&dd->list);
1282 spin_unlock(&list_lock);
1284 for (i = dd->pdata->algs_info_size - 1; i >= 0; i--)
1285 for (j = dd->pdata->algs_info[i].registered - 1; j >= 0; j--)
1286 crypto_unregister_skcipher(
1287 &dd->pdata->algs_info[i].algs_list[j]);
1289 for (i = dd->pdata->aead_algs_info->size - 1; i >= 0; i--) {
1290 aalg = &dd->pdata->aead_algs_info->algs_list[i];
1291 crypto_unregister_aead(aalg);
1294 crypto_engine_exit(dd->engine);
1296 tasklet_kill(&dd->done_task);
1297 omap_aes_dma_cleanup(dd);
1298 pm_runtime_disable(dd->dev);
1304 #ifdef CONFIG_PM_SLEEP
1305 static int omap_aes_suspend(struct device *dev)
1307 pm_runtime_put_sync(dev);
1311 static int omap_aes_resume(struct device *dev)
1313 pm_runtime_get_sync(dev);
1318 static SIMPLE_DEV_PM_OPS(omap_aes_pm_ops, omap_aes_suspend, omap_aes_resume);
1320 static struct platform_driver omap_aes_driver = {
1321 .probe = omap_aes_probe,
1322 .remove = omap_aes_remove,
1325 .pm = &omap_aes_pm_ops,
1326 .of_match_table = omap_aes_of_match,
1330 module_platform_driver(omap_aes_driver);
1332 MODULE_DESCRIPTION("OMAP AES hw acceleration support.");
1333 MODULE_LICENSE("GPL v2");
1334 MODULE_AUTHOR("Dmitry Kasatkin");