4 * Support for OMAP AES HW acceleration.
6 * Copyright (c) 2010 Nokia Corporation
7 * Author: Dmitry Kasatkin <dmitry.kasatkin@nokia.com>
8 * Copyright (c) 2011 Texas Instruments Incorporated
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as published
12 * by the Free Software Foundation.
16 #define pr_fmt(fmt) "%20s: " fmt, __func__
17 #define prn(num) pr_debug(#num "=%d\n", num)
18 #define prx(num) pr_debug(#num "=%x\n", num)
20 #include <linux/err.h>
21 #include <linux/module.h>
22 #include <linux/init.h>
23 #include <linux/errno.h>
24 #include <linux/kernel.h>
25 #include <linux/platform_device.h>
26 #include <linux/scatterlist.h>
27 #include <linux/dma-mapping.h>
28 #include <linux/dmaengine.h>
29 #include <linux/omap-dma.h>
30 #include <linux/pm_runtime.h>
32 #include <linux/of_device.h>
33 #include <linux/of_address.h>
35 #include <linux/crypto.h>
36 #include <linux/interrupt.h>
37 #include <crypto/scatterwalk.h>
38 #include <crypto/aes.h>
40 #define DST_MAXBURST 4
41 #define DMA_MIN (DST_MAXBURST * sizeof(u32))
43 #define _calc_walked(inout) (dd->inout##_walk.offset - dd->inout##_sg->offset)
45 /* OMAP TRM gives bitfields as start:end, where start is the higher bit
46 number. For example 7:0 */
47 #define FLD_MASK(start, end) (((1 << ((start) - (end) + 1)) - 1) << (end))
48 #define FLD_VAL(val, start, end) (((val) << (end)) & FLD_MASK(start, end))
50 #define AES_REG_KEY(dd, x) ((dd)->pdata->key_ofs - \
52 #define AES_REG_IV(dd, x) ((dd)->pdata->iv_ofs + ((x) * 0x04))
54 #define AES_REG_CTRL(dd) ((dd)->pdata->ctrl_ofs)
55 #define AES_REG_CTRL_CTR_WIDTH_MASK (3 << 7)
56 #define AES_REG_CTRL_CTR_WIDTH_32 (0 << 7)
57 #define AES_REG_CTRL_CTR_WIDTH_64 (1 << 7)
58 #define AES_REG_CTRL_CTR_WIDTH_96 (2 << 7)
59 #define AES_REG_CTRL_CTR_WIDTH_128 (3 << 7)
60 #define AES_REG_CTRL_CTR (1 << 6)
61 #define AES_REG_CTRL_CBC (1 << 5)
62 #define AES_REG_CTRL_KEY_SIZE (3 << 3)
63 #define AES_REG_CTRL_DIRECTION (1 << 2)
64 #define AES_REG_CTRL_INPUT_READY (1 << 1)
65 #define AES_REG_CTRL_OUTPUT_READY (1 << 0)
67 #define AES_REG_DATA_N(dd, x) ((dd)->pdata->data_ofs + ((x) * 0x04))
69 #define AES_REG_REV(dd) ((dd)->pdata->rev_ofs)
71 #define AES_REG_MASK(dd) ((dd)->pdata->mask_ofs)
72 #define AES_REG_MASK_SIDLE (1 << 6)
73 #define AES_REG_MASK_START (1 << 5)
74 #define AES_REG_MASK_DMA_OUT_EN (1 << 3)
75 #define AES_REG_MASK_DMA_IN_EN (1 << 2)
76 #define AES_REG_MASK_SOFTRESET (1 << 1)
77 #define AES_REG_AUTOIDLE (1 << 0)
79 #define AES_REG_LENGTH_N(x) (0x54 + ((x) * 0x04))
81 #define AES_REG_IRQ_STATUS(dd) ((dd)->pdata->irq_status_ofs)
82 #define AES_REG_IRQ_ENABLE(dd) ((dd)->pdata->irq_enable_ofs)
83 #define AES_REG_IRQ_DATA_IN BIT(1)
84 #define AES_REG_IRQ_DATA_OUT BIT(2)
85 #define DEFAULT_TIMEOUT (5*HZ)
87 #define FLAGS_MODE_MASK 0x000f
88 #define FLAGS_ENCRYPT BIT(0)
89 #define FLAGS_CBC BIT(1)
90 #define FLAGS_GIV BIT(2)
91 #define FLAGS_CTR BIT(3)
93 #define FLAGS_INIT BIT(4)
94 #define FLAGS_FAST BIT(5)
95 #define FLAGS_BUSY BIT(6)
97 #define AES_BLOCK_WORDS (AES_BLOCK_SIZE >> 2)
100 struct omap_aes_dev *dd;
103 u32 key[AES_KEYSIZE_256 / sizeof(u32)];
107 struct omap_aes_reqctx {
111 #define OMAP_AES_QUEUE_LENGTH 1
112 #define OMAP_AES_CACHE_SIZE 0
114 struct omap_aes_algs_info {
115 struct crypto_alg *algs_list;
117 unsigned int registered;
120 struct omap_aes_pdata {
121 struct omap_aes_algs_info *algs_info;
122 unsigned int algs_info_size;
124 void (*trigger)(struct omap_aes_dev *dd, int length);
145 struct omap_aes_dev {
146 struct list_head list;
147 unsigned long phys_base;
148 void __iomem *io_base;
149 struct omap_aes_ctx *ctx;
155 struct crypto_queue queue;
157 struct tasklet_struct done_task;
158 struct tasklet_struct queue_task;
160 struct ablkcipher_request *req;
163 * total is used by PIO mode for book keeping so introduce
164 * variable total_save as need it to calc page_order
169 struct scatterlist *in_sg;
170 struct scatterlist *out_sg;
172 /* Buffers for copying for unaligned cases */
173 struct scatterlist in_sgl;
174 struct scatterlist out_sgl;
175 struct scatterlist *orig_out;
178 struct scatter_walk in_walk;
179 struct scatter_walk out_walk;
181 struct dma_chan *dma_lch_in;
183 struct dma_chan *dma_lch_out;
187 const struct omap_aes_pdata *pdata;
190 /* keep registered devices data here */
191 static LIST_HEAD(dev_list);
192 static DEFINE_SPINLOCK(list_lock);
195 #define omap_aes_read(dd, offset) \
198 _read_ret = __raw_readl(dd->io_base + offset); \
199 pr_debug("omap_aes_read(" #offset "=%#x)= %#x\n", \
200 offset, _read_ret); \
204 static inline u32 omap_aes_read(struct omap_aes_dev *dd, u32 offset)
206 return __raw_readl(dd->io_base + offset);
211 #define omap_aes_write(dd, offset, value) \
213 pr_debug("omap_aes_write(" #offset "=%#x) value=%#x\n", \
215 __raw_writel(value, dd->io_base + offset); \
218 static inline void omap_aes_write(struct omap_aes_dev *dd, u32 offset,
221 __raw_writel(value, dd->io_base + offset);
225 static inline void omap_aes_write_mask(struct omap_aes_dev *dd, u32 offset,
230 val = omap_aes_read(dd, offset);
233 omap_aes_write(dd, offset, val);
236 static void omap_aes_write_n(struct omap_aes_dev *dd, u32 offset,
237 u32 *value, int count)
239 for (; count--; value++, offset += 4)
240 omap_aes_write(dd, offset, *value);
243 static int omap_aes_hw_init(struct omap_aes_dev *dd)
245 if (!(dd->flags & FLAGS_INIT)) {
246 dd->flags |= FLAGS_INIT;
253 static int omap_aes_write_ctrl(struct omap_aes_dev *dd)
259 err = omap_aes_hw_init(dd);
263 key32 = dd->ctx->keylen / sizeof(u32);
265 /* it seems a key should always be set even if it has not changed */
266 for (i = 0; i < key32; i++) {
267 omap_aes_write(dd, AES_REG_KEY(dd, i),
268 __le32_to_cpu(dd->ctx->key[i]));
271 if ((dd->flags & (FLAGS_CBC | FLAGS_CTR)) && dd->req->info)
272 omap_aes_write_n(dd, AES_REG_IV(dd, 0), dd->req->info, 4);
274 val = FLD_VAL(((dd->ctx->keylen >> 3) - 1), 4, 3);
275 if (dd->flags & FLAGS_CBC)
276 val |= AES_REG_CTRL_CBC;
277 if (dd->flags & FLAGS_CTR) {
278 val |= AES_REG_CTRL_CTR | AES_REG_CTRL_CTR_WIDTH_32;
279 mask = AES_REG_CTRL_CTR | AES_REG_CTRL_CTR_WIDTH_MASK;
281 if (dd->flags & FLAGS_ENCRYPT)
282 val |= AES_REG_CTRL_DIRECTION;
284 mask |= AES_REG_CTRL_CBC | AES_REG_CTRL_DIRECTION |
285 AES_REG_CTRL_KEY_SIZE;
287 omap_aes_write_mask(dd, AES_REG_CTRL(dd), val, mask);
292 static void omap_aes_dma_trigger_omap2(struct omap_aes_dev *dd, int length)
296 val = dd->pdata->dma_start;
298 if (dd->dma_lch_out != NULL)
299 val |= dd->pdata->dma_enable_out;
300 if (dd->dma_lch_in != NULL)
301 val |= dd->pdata->dma_enable_in;
303 mask = dd->pdata->dma_enable_out | dd->pdata->dma_enable_in |
304 dd->pdata->dma_start;
306 omap_aes_write_mask(dd, AES_REG_MASK(dd), val, mask);
310 static void omap_aes_dma_trigger_omap4(struct omap_aes_dev *dd, int length)
312 omap_aes_write(dd, AES_REG_LENGTH_N(0), length);
313 omap_aes_write(dd, AES_REG_LENGTH_N(1), 0);
315 omap_aes_dma_trigger_omap2(dd, length);
318 static void omap_aes_dma_stop(struct omap_aes_dev *dd)
322 mask = dd->pdata->dma_enable_out | dd->pdata->dma_enable_in |
323 dd->pdata->dma_start;
325 omap_aes_write_mask(dd, AES_REG_MASK(dd), 0, mask);
328 static struct omap_aes_dev *omap_aes_find_dev(struct omap_aes_ctx *ctx)
330 struct omap_aes_dev *dd = NULL, *tmp;
332 spin_lock_bh(&list_lock);
334 list_for_each_entry(tmp, &dev_list, list) {
335 /* FIXME: take fist available aes core */
341 /* already found before */
344 spin_unlock_bh(&list_lock);
349 static void omap_aes_dma_out_callback(void *data)
351 struct omap_aes_dev *dd = data;
353 /* dma_lch_out - completed */
354 tasklet_schedule(&dd->done_task);
357 static int omap_aes_dma_init(struct omap_aes_dev *dd)
362 dd->dma_lch_out = NULL;
363 dd->dma_lch_in = NULL;
366 dma_cap_set(DMA_SLAVE, mask);
368 dd->dma_lch_in = dma_request_slave_channel_compat(mask,
372 if (!dd->dma_lch_in) {
373 dev_err(dd->dev, "Unable to request in DMA channel\n");
377 dd->dma_lch_out = dma_request_slave_channel_compat(mask,
381 if (!dd->dma_lch_out) {
382 dev_err(dd->dev, "Unable to request out DMA channel\n");
389 dma_release_channel(dd->dma_lch_in);
392 pr_err("error: %d\n", err);
396 static void omap_aes_dma_cleanup(struct omap_aes_dev *dd)
398 dma_release_channel(dd->dma_lch_out);
399 dma_release_channel(dd->dma_lch_in);
402 static void sg_copy_buf(void *buf, struct scatterlist *sg,
403 unsigned int start, unsigned int nbytes, int out)
405 struct scatter_walk walk;
410 scatterwalk_start(&walk, sg);
411 scatterwalk_advance(&walk, start);
412 scatterwalk_copychunks(buf, &walk, nbytes, out);
413 scatterwalk_done(&walk, out, 0);
416 static int omap_aes_crypt_dma(struct crypto_tfm *tfm,
417 struct scatterlist *in_sg, struct scatterlist *out_sg,
418 int in_sg_len, int out_sg_len)
420 struct omap_aes_ctx *ctx = crypto_tfm_ctx(tfm);
421 struct omap_aes_dev *dd = ctx->dd;
422 struct dma_async_tx_descriptor *tx_in, *tx_out;
423 struct dma_slave_config cfg;
427 scatterwalk_start(&dd->in_walk, dd->in_sg);
428 scatterwalk_start(&dd->out_walk, dd->out_sg);
430 /* Enable DATAIN interrupt and let it take
432 omap_aes_write(dd, AES_REG_IRQ_ENABLE(dd), 0x2);
436 dma_sync_sg_for_device(dd->dev, dd->in_sg, in_sg_len, DMA_TO_DEVICE);
438 memset(&cfg, 0, sizeof(cfg));
440 cfg.src_addr = dd->phys_base + AES_REG_DATA_N(dd, 0);
441 cfg.dst_addr = dd->phys_base + AES_REG_DATA_N(dd, 0);
442 cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
443 cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
444 cfg.src_maxburst = DST_MAXBURST;
445 cfg.dst_maxburst = DST_MAXBURST;
448 ret = dmaengine_slave_config(dd->dma_lch_in, &cfg);
450 dev_err(dd->dev, "can't configure IN dmaengine slave: %d\n",
455 tx_in = dmaengine_prep_slave_sg(dd->dma_lch_in, in_sg, in_sg_len,
457 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
459 dev_err(dd->dev, "IN prep_slave_sg() failed\n");
463 /* No callback necessary */
464 tx_in->callback_param = dd;
467 ret = dmaengine_slave_config(dd->dma_lch_out, &cfg);
469 dev_err(dd->dev, "can't configure OUT dmaengine slave: %d\n",
474 tx_out = dmaengine_prep_slave_sg(dd->dma_lch_out, out_sg, out_sg_len,
476 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
478 dev_err(dd->dev, "OUT prep_slave_sg() failed\n");
482 tx_out->callback = omap_aes_dma_out_callback;
483 tx_out->callback_param = dd;
485 dmaengine_submit(tx_in);
486 dmaengine_submit(tx_out);
488 dma_async_issue_pending(dd->dma_lch_in);
489 dma_async_issue_pending(dd->dma_lch_out);
492 dd->pdata->trigger(dd, dd->total);
497 static int omap_aes_crypt_dma_start(struct omap_aes_dev *dd)
499 struct crypto_tfm *tfm = crypto_ablkcipher_tfm(
500 crypto_ablkcipher_reqtfm(dd->req));
503 pr_debug("total: %d\n", dd->total);
506 err = dma_map_sg(dd->dev, dd->in_sg, dd->in_sg_len,
509 dev_err(dd->dev, "dma_map_sg() error\n");
513 err = dma_map_sg(dd->dev, dd->out_sg, dd->out_sg_len,
516 dev_err(dd->dev, "dma_map_sg() error\n");
521 err = omap_aes_crypt_dma(tfm, dd->in_sg, dd->out_sg, dd->in_sg_len,
523 if (err && !dd->pio_only) {
524 dma_unmap_sg(dd->dev, dd->in_sg, dd->in_sg_len, DMA_TO_DEVICE);
525 dma_unmap_sg(dd->dev, dd->out_sg, dd->out_sg_len,
532 static void omap_aes_finish_req(struct omap_aes_dev *dd, int err)
534 struct ablkcipher_request *req = dd->req;
536 pr_debug("err: %d\n", err);
538 dd->flags &= ~FLAGS_BUSY;
540 req->base.complete(&req->base, err);
543 static int omap_aes_crypt_dma_stop(struct omap_aes_dev *dd)
547 pr_debug("total: %d\n", dd->total);
549 omap_aes_dma_stop(dd);
551 dmaengine_terminate_all(dd->dma_lch_in);
552 dmaengine_terminate_all(dd->dma_lch_out);
557 int omap_aes_check_aligned(struct scatterlist *sg)
560 if (!IS_ALIGNED(sg->offset, 4))
562 if (!IS_ALIGNED(sg->length, AES_BLOCK_SIZE))
569 int omap_aes_copy_sgs(struct omap_aes_dev *dd)
571 void *buf_in, *buf_out;
574 pages = get_order(dd->total);
576 buf_in = (void *)__get_free_pages(GFP_ATOMIC, pages);
577 buf_out = (void *)__get_free_pages(GFP_ATOMIC, pages);
579 if (!buf_in || !buf_out) {
580 pr_err("Couldn't allocated pages for unaligned cases.\n");
584 dd->orig_out = dd->out_sg;
586 sg_copy_buf(buf_in, dd->in_sg, 0, dd->total, 0);
588 sg_init_table(&dd->in_sgl, 1);
589 sg_set_buf(&dd->in_sgl, buf_in, dd->total);
590 dd->in_sg = &dd->in_sgl;
592 sg_init_table(&dd->out_sgl, 1);
593 sg_set_buf(&dd->out_sgl, buf_out, dd->total);
594 dd->out_sg = &dd->out_sgl;
599 static int omap_aes_handle_queue(struct omap_aes_dev *dd,
600 struct ablkcipher_request *req)
602 struct crypto_async_request *async_req, *backlog;
603 struct omap_aes_ctx *ctx;
604 struct omap_aes_reqctx *rctx;
608 spin_lock_irqsave(&dd->lock, flags);
610 ret = ablkcipher_enqueue_request(&dd->queue, req);
611 if (dd->flags & FLAGS_BUSY) {
612 spin_unlock_irqrestore(&dd->lock, flags);
615 backlog = crypto_get_backlog(&dd->queue);
616 async_req = crypto_dequeue_request(&dd->queue);
618 dd->flags |= FLAGS_BUSY;
619 spin_unlock_irqrestore(&dd->lock, flags);
625 backlog->complete(backlog, -EINPROGRESS);
627 req = ablkcipher_request_cast(async_req);
629 /* assign new request to device */
631 dd->total = req->nbytes;
632 dd->total_save = req->nbytes;
633 dd->in_sg = req->src;
634 dd->out_sg = req->dst;
636 if (omap_aes_check_aligned(dd->in_sg) ||
637 omap_aes_check_aligned(dd->out_sg)) {
638 if (omap_aes_copy_sgs(dd))
639 pr_err("Failed to copy SGs for unaligned cases\n");
645 dd->in_sg_len = scatterwalk_bytes_sglen(dd->in_sg, dd->total);
646 dd->out_sg_len = scatterwalk_bytes_sglen(dd->out_sg, dd->total);
647 BUG_ON(dd->in_sg_len < 0 || dd->out_sg_len < 0);
649 rctx = ablkcipher_request_ctx(req);
650 ctx = crypto_ablkcipher_ctx(crypto_ablkcipher_reqtfm(req));
651 rctx->mode &= FLAGS_MODE_MASK;
652 dd->flags = (dd->flags & ~FLAGS_MODE_MASK) | rctx->mode;
657 err = omap_aes_write_ctrl(dd);
659 err = omap_aes_crypt_dma_start(dd);
661 /* aes_task will not finish it, so do it here */
662 omap_aes_finish_req(dd, err);
663 tasklet_schedule(&dd->queue_task);
666 return ret; /* return ret, which is enqueue return value */
669 static void omap_aes_done_task(unsigned long data)
671 struct omap_aes_dev *dd = (struct omap_aes_dev *)data;
672 void *buf_in, *buf_out;
675 pr_debug("enter done_task\n");
678 dma_sync_sg_for_device(dd->dev, dd->out_sg, dd->out_sg_len,
680 dma_unmap_sg(dd->dev, dd->in_sg, dd->in_sg_len, DMA_TO_DEVICE);
681 dma_unmap_sg(dd->dev, dd->out_sg, dd->out_sg_len,
683 omap_aes_crypt_dma_stop(dd);
686 if (dd->sgs_copied) {
687 buf_in = sg_virt(&dd->in_sgl);
688 buf_out = sg_virt(&dd->out_sgl);
690 sg_copy_buf(buf_out, dd->orig_out, 0, dd->total_save, 1);
692 pages = get_order(dd->total_save);
693 free_pages((unsigned long)buf_in, pages);
694 free_pages((unsigned long)buf_out, pages);
697 omap_aes_finish_req(dd, 0);
698 omap_aes_handle_queue(dd, NULL);
703 static void omap_aes_queue_task(unsigned long data)
705 struct omap_aes_dev *dd = (struct omap_aes_dev *)data;
707 omap_aes_handle_queue(dd, NULL);
710 static int omap_aes_crypt(struct ablkcipher_request *req, unsigned long mode)
712 struct omap_aes_ctx *ctx = crypto_ablkcipher_ctx(
713 crypto_ablkcipher_reqtfm(req));
714 struct omap_aes_reqctx *rctx = ablkcipher_request_ctx(req);
715 struct omap_aes_dev *dd;
717 pr_debug("nbytes: %d, enc: %d, cbc: %d\n", req->nbytes,
718 !!(mode & FLAGS_ENCRYPT),
719 !!(mode & FLAGS_CBC));
721 if (!IS_ALIGNED(req->nbytes, AES_BLOCK_SIZE)) {
722 pr_err("request size is not exact amount of AES blocks\n");
726 dd = omap_aes_find_dev(ctx);
732 return omap_aes_handle_queue(dd, req);
735 /* ********************** ALG API ************************************ */
737 static int omap_aes_setkey(struct crypto_ablkcipher *tfm, const u8 *key,
740 struct omap_aes_ctx *ctx = crypto_ablkcipher_ctx(tfm);
742 if (keylen != AES_KEYSIZE_128 && keylen != AES_KEYSIZE_192 &&
743 keylen != AES_KEYSIZE_256)
746 pr_debug("enter, keylen: %d\n", keylen);
748 memcpy(ctx->key, key, keylen);
749 ctx->keylen = keylen;
754 static int omap_aes_ecb_encrypt(struct ablkcipher_request *req)
756 return omap_aes_crypt(req, FLAGS_ENCRYPT);
759 static int omap_aes_ecb_decrypt(struct ablkcipher_request *req)
761 return omap_aes_crypt(req, 0);
764 static int omap_aes_cbc_encrypt(struct ablkcipher_request *req)
766 return omap_aes_crypt(req, FLAGS_ENCRYPT | FLAGS_CBC);
769 static int omap_aes_cbc_decrypt(struct ablkcipher_request *req)
771 return omap_aes_crypt(req, FLAGS_CBC);
774 static int omap_aes_ctr_encrypt(struct ablkcipher_request *req)
776 return omap_aes_crypt(req, FLAGS_ENCRYPT | FLAGS_CTR);
779 static int omap_aes_ctr_decrypt(struct ablkcipher_request *req)
781 return omap_aes_crypt(req, FLAGS_CTR);
784 static int omap_aes_cra_init(struct crypto_tfm *tfm)
786 struct omap_aes_dev *dd = NULL;
788 /* Find AES device, currently picks the first device */
789 spin_lock_bh(&list_lock);
790 list_for_each_entry(dd, &dev_list, list) {
793 spin_unlock_bh(&list_lock);
795 pm_runtime_get_sync(dd->dev);
796 tfm->crt_ablkcipher.reqsize = sizeof(struct omap_aes_reqctx);
801 static void omap_aes_cra_exit(struct crypto_tfm *tfm)
803 struct omap_aes_dev *dd = NULL;
805 /* Find AES device, currently picks the first device */
806 spin_lock_bh(&list_lock);
807 list_for_each_entry(dd, &dev_list, list) {
810 spin_unlock_bh(&list_lock);
812 pm_runtime_put_sync(dd->dev);
815 /* ********************** ALGS ************************************ */
817 static struct crypto_alg algs_ecb_cbc[] = {
819 .cra_name = "ecb(aes)",
820 .cra_driver_name = "ecb-aes-omap",
822 .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER |
823 CRYPTO_ALG_KERN_DRIVER_ONLY |
825 .cra_blocksize = AES_BLOCK_SIZE,
826 .cra_ctxsize = sizeof(struct omap_aes_ctx),
828 .cra_type = &crypto_ablkcipher_type,
829 .cra_module = THIS_MODULE,
830 .cra_init = omap_aes_cra_init,
831 .cra_exit = omap_aes_cra_exit,
832 .cra_u.ablkcipher = {
833 .min_keysize = AES_MIN_KEY_SIZE,
834 .max_keysize = AES_MAX_KEY_SIZE,
835 .setkey = omap_aes_setkey,
836 .encrypt = omap_aes_ecb_encrypt,
837 .decrypt = omap_aes_ecb_decrypt,
841 .cra_name = "cbc(aes)",
842 .cra_driver_name = "cbc-aes-omap",
844 .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER |
845 CRYPTO_ALG_KERN_DRIVER_ONLY |
847 .cra_blocksize = AES_BLOCK_SIZE,
848 .cra_ctxsize = sizeof(struct omap_aes_ctx),
850 .cra_type = &crypto_ablkcipher_type,
851 .cra_module = THIS_MODULE,
852 .cra_init = omap_aes_cra_init,
853 .cra_exit = omap_aes_cra_exit,
854 .cra_u.ablkcipher = {
855 .min_keysize = AES_MIN_KEY_SIZE,
856 .max_keysize = AES_MAX_KEY_SIZE,
857 .ivsize = AES_BLOCK_SIZE,
858 .setkey = omap_aes_setkey,
859 .encrypt = omap_aes_cbc_encrypt,
860 .decrypt = omap_aes_cbc_decrypt,
865 static struct crypto_alg algs_ctr[] = {
867 .cra_name = "ctr(aes)",
868 .cra_driver_name = "ctr-aes-omap",
870 .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER |
871 CRYPTO_ALG_KERN_DRIVER_ONLY |
873 .cra_blocksize = AES_BLOCK_SIZE,
874 .cra_ctxsize = sizeof(struct omap_aes_ctx),
876 .cra_type = &crypto_ablkcipher_type,
877 .cra_module = THIS_MODULE,
878 .cra_init = omap_aes_cra_init,
879 .cra_exit = omap_aes_cra_exit,
880 .cra_u.ablkcipher = {
881 .min_keysize = AES_MIN_KEY_SIZE,
882 .max_keysize = AES_MAX_KEY_SIZE,
884 .ivsize = AES_BLOCK_SIZE,
885 .setkey = omap_aes_setkey,
886 .encrypt = omap_aes_ctr_encrypt,
887 .decrypt = omap_aes_ctr_decrypt,
892 static struct omap_aes_algs_info omap_aes_algs_info_ecb_cbc[] = {
894 .algs_list = algs_ecb_cbc,
895 .size = ARRAY_SIZE(algs_ecb_cbc),
899 static const struct omap_aes_pdata omap_aes_pdata_omap2 = {
900 .algs_info = omap_aes_algs_info_ecb_cbc,
901 .algs_info_size = ARRAY_SIZE(omap_aes_algs_info_ecb_cbc),
902 .trigger = omap_aes_dma_trigger_omap2,
909 .dma_enable_in = BIT(2),
910 .dma_enable_out = BIT(3),
919 static struct omap_aes_algs_info omap_aes_algs_info_ecb_cbc_ctr[] = {
921 .algs_list = algs_ecb_cbc,
922 .size = ARRAY_SIZE(algs_ecb_cbc),
925 .algs_list = algs_ctr,
926 .size = ARRAY_SIZE(algs_ctr),
930 static const struct omap_aes_pdata omap_aes_pdata_omap3 = {
931 .algs_info = omap_aes_algs_info_ecb_cbc_ctr,
932 .algs_info_size = ARRAY_SIZE(omap_aes_algs_info_ecb_cbc_ctr),
933 .trigger = omap_aes_dma_trigger_omap2,
940 .dma_enable_in = BIT(2),
941 .dma_enable_out = BIT(3),
949 static const struct omap_aes_pdata omap_aes_pdata_omap4 = {
950 .algs_info = omap_aes_algs_info_ecb_cbc_ctr,
951 .algs_info_size = ARRAY_SIZE(omap_aes_algs_info_ecb_cbc_ctr),
952 .trigger = omap_aes_dma_trigger_omap4,
959 .irq_status_ofs = 0x8c,
960 .irq_enable_ofs = 0x90,
961 .dma_enable_in = BIT(5),
962 .dma_enable_out = BIT(6),
963 .major_mask = 0x0700,
965 .minor_mask = 0x003f,
969 static irqreturn_t omap_aes_irq(int irq, void *dev_id)
971 struct omap_aes_dev *dd = dev_id;
975 status = omap_aes_read(dd, AES_REG_IRQ_STATUS(dd));
976 if (status & AES_REG_IRQ_DATA_IN) {
977 omap_aes_write(dd, AES_REG_IRQ_ENABLE(dd), 0x0);
981 BUG_ON(_calc_walked(in) > dd->in_sg->length);
983 src = sg_virt(dd->in_sg) + _calc_walked(in);
985 for (i = 0; i < AES_BLOCK_WORDS; i++) {
986 omap_aes_write(dd, AES_REG_DATA_N(dd, i), *src);
988 scatterwalk_advance(&dd->in_walk, 4);
989 if (dd->in_sg->length == _calc_walked(in)) {
990 dd->in_sg = scatterwalk_sg_next(dd->in_sg);
992 scatterwalk_start(&dd->in_walk,
994 src = sg_virt(dd->in_sg) +
1002 /* Clear IRQ status */
1003 status &= ~AES_REG_IRQ_DATA_IN;
1004 omap_aes_write(dd, AES_REG_IRQ_STATUS(dd), status);
1006 /* Enable DATA_OUT interrupt */
1007 omap_aes_write(dd, AES_REG_IRQ_ENABLE(dd), 0x4);
1009 } else if (status & AES_REG_IRQ_DATA_OUT) {
1010 omap_aes_write(dd, AES_REG_IRQ_ENABLE(dd), 0x0);
1012 BUG_ON(!dd->out_sg);
1014 BUG_ON(_calc_walked(out) > dd->out_sg->length);
1016 dst = sg_virt(dd->out_sg) + _calc_walked(out);
1018 for (i = 0; i < AES_BLOCK_WORDS; i++) {
1019 *dst = omap_aes_read(dd, AES_REG_DATA_N(dd, i));
1020 scatterwalk_advance(&dd->out_walk, 4);
1021 if (dd->out_sg->length == _calc_walked(out)) {
1022 dd->out_sg = scatterwalk_sg_next(dd->out_sg);
1024 scatterwalk_start(&dd->out_walk,
1026 dst = sg_virt(dd->out_sg) +
1034 dd->total -= AES_BLOCK_SIZE;
1036 BUG_ON(dd->total < 0);
1038 /* Clear IRQ status */
1039 status &= ~AES_REG_IRQ_DATA_OUT;
1040 omap_aes_write(dd, AES_REG_IRQ_STATUS(dd), status);
1043 /* All bytes read! */
1044 tasklet_schedule(&dd->done_task);
1046 /* Enable DATA_IN interrupt for next block */
1047 omap_aes_write(dd, AES_REG_IRQ_ENABLE(dd), 0x2);
1053 static const struct of_device_id omap_aes_of_match[] = {
1055 .compatible = "ti,omap2-aes",
1056 .data = &omap_aes_pdata_omap2,
1059 .compatible = "ti,omap3-aes",
1060 .data = &omap_aes_pdata_omap3,
1063 .compatible = "ti,omap4-aes",
1064 .data = &omap_aes_pdata_omap4,
1068 MODULE_DEVICE_TABLE(of, omap_aes_of_match);
1070 static int omap_aes_get_res_of(struct omap_aes_dev *dd,
1071 struct device *dev, struct resource *res)
1073 struct device_node *node = dev->of_node;
1074 const struct of_device_id *match;
1077 match = of_match_device(of_match_ptr(omap_aes_of_match), dev);
1079 dev_err(dev, "no compatible OF match\n");
1084 err = of_address_to_resource(node, 0, res);
1086 dev_err(dev, "can't translate OF node address\n");
1091 dd->dma_out = -1; /* Dummy value that's unused */
1092 dd->dma_in = -1; /* Dummy value that's unused */
1094 dd->pdata = match->data;
1100 static const struct of_device_id omap_aes_of_match[] = {
1104 static int omap_aes_get_res_of(struct omap_aes_dev *dd,
1105 struct device *dev, struct resource *res)
1111 static int omap_aes_get_res_pdev(struct omap_aes_dev *dd,
1112 struct platform_device *pdev, struct resource *res)
1114 struct device *dev = &pdev->dev;
1118 /* Get the base address */
1119 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1121 dev_err(dev, "no MEM resource info\n");
1125 memcpy(res, r, sizeof(*res));
1127 /* Get the DMA out channel */
1128 r = platform_get_resource(pdev, IORESOURCE_DMA, 0);
1130 dev_err(dev, "no DMA out resource info\n");
1134 dd->dma_out = r->start;
1136 /* Get the DMA in channel */
1137 r = platform_get_resource(pdev, IORESOURCE_DMA, 1);
1139 dev_err(dev, "no DMA in resource info\n");
1143 dd->dma_in = r->start;
1145 /* Only OMAP2/3 can be non-DT */
1146 dd->pdata = &omap_aes_pdata_omap2;
1152 static int omap_aes_probe(struct platform_device *pdev)
1154 struct device *dev = &pdev->dev;
1155 struct omap_aes_dev *dd;
1156 struct crypto_alg *algp;
1157 struct resource res;
1158 int err = -ENOMEM, i, j, irq = -1;
1161 dd = devm_kzalloc(dev, sizeof(struct omap_aes_dev), GFP_KERNEL);
1163 dev_err(dev, "unable to alloc data struct.\n");
1167 platform_set_drvdata(pdev, dd);
1169 spin_lock_init(&dd->lock);
1170 crypto_init_queue(&dd->queue, OMAP_AES_QUEUE_LENGTH);
1172 err = (dev->of_node) ? omap_aes_get_res_of(dd, dev, &res) :
1173 omap_aes_get_res_pdev(dd, pdev, &res);
1177 dd->io_base = devm_ioremap_resource(dev, &res);
1178 if (IS_ERR(dd->io_base)) {
1179 err = PTR_ERR(dd->io_base);
1182 dd->phys_base = res.start;
1184 pm_runtime_enable(dev);
1185 pm_runtime_get_sync(dev);
1187 omap_aes_dma_stop(dd);
1189 reg = omap_aes_read(dd, AES_REG_REV(dd));
1191 pm_runtime_put_sync(dev);
1193 dev_info(dev, "OMAP AES hw accel rev: %u.%u\n",
1194 (reg & dd->pdata->major_mask) >> dd->pdata->major_shift,
1195 (reg & dd->pdata->minor_mask) >> dd->pdata->minor_shift);
1197 tasklet_init(&dd->done_task, omap_aes_done_task, (unsigned long)dd);
1198 tasklet_init(&dd->queue_task, omap_aes_queue_task, (unsigned long)dd);
1200 err = omap_aes_dma_init(dd);
1201 if (err && AES_REG_IRQ_STATUS(dd) && AES_REG_IRQ_ENABLE(dd)) {
1204 irq = platform_get_irq(pdev, 0);
1206 dev_err(dev, "can't get IRQ resource\n");
1210 err = devm_request_irq(dev, irq, omap_aes_irq, 0,
1213 dev_err(dev, "Unable to grab omap-aes IRQ\n");
1219 INIT_LIST_HEAD(&dd->list);
1220 spin_lock(&list_lock);
1221 list_add_tail(&dd->list, &dev_list);
1222 spin_unlock(&list_lock);
1224 for (i = 0; i < dd->pdata->algs_info_size; i++) {
1225 for (j = 0; j < dd->pdata->algs_info[i].size; j++) {
1226 algp = &dd->pdata->algs_info[i].algs_list[j];
1228 pr_debug("reg alg: %s\n", algp->cra_name);
1229 INIT_LIST_HEAD(&algp->cra_list);
1231 err = crypto_register_alg(algp);
1235 dd->pdata->algs_info[i].registered++;
1241 for (i = dd->pdata->algs_info_size - 1; i >= 0; i--)
1242 for (j = dd->pdata->algs_info[i].registered - 1; j >= 0; j--)
1243 crypto_unregister_alg(
1244 &dd->pdata->algs_info[i].algs_list[j]);
1246 omap_aes_dma_cleanup(dd);
1248 tasklet_kill(&dd->done_task);
1249 tasklet_kill(&dd->queue_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);
1266 spin_lock(&list_lock);
1267 list_del(&dd->list);
1268 spin_unlock(&list_lock);
1270 for (i = dd->pdata->algs_info_size - 1; i >= 0; i--)
1271 for (j = dd->pdata->algs_info[i].registered - 1; j >= 0; j--)
1272 crypto_unregister_alg(
1273 &dd->pdata->algs_info[i].algs_list[j]);
1275 tasklet_kill(&dd->done_task);
1276 tasklet_kill(&dd->queue_task);
1277 omap_aes_dma_cleanup(dd);
1278 pm_runtime_disable(dd->dev);
1284 #ifdef CONFIG_PM_SLEEP
1285 static int omap_aes_suspend(struct device *dev)
1287 pm_runtime_put_sync(dev);
1291 static int omap_aes_resume(struct device *dev)
1293 pm_runtime_get_sync(dev);
1298 static const struct dev_pm_ops omap_aes_pm_ops = {
1299 SET_SYSTEM_SLEEP_PM_OPS(omap_aes_suspend, omap_aes_resume)
1302 static struct platform_driver omap_aes_driver = {
1303 .probe = omap_aes_probe,
1304 .remove = omap_aes_remove,
1307 .owner = THIS_MODULE,
1308 .pm = &omap_aes_pm_ops,
1309 .of_match_table = omap_aes_of_match,
1313 module_platform_driver(omap_aes_driver);
1315 MODULE_DESCRIPTION("OMAP AES hw acceleration support.");
1316 MODULE_LICENSE("GPL v2");
1317 MODULE_AUTHOR("Dmitry Kasatkin");