sh_eth: fix EESIPR values for SH77{34|63}
[linux/fpc-iii.git] / drivers / crypto / omap-aes.c
blobfe32dd95ae4ff20008baeaaa9a78046b6b6c0ed0
1 /*
2 * Cryptographic API.
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/pm_runtime.h>
30 #include <linux/of.h>
31 #include <linux/of_device.h>
32 #include <linux/of_address.h>
33 #include <linux/io.h>
34 #include <linux/crypto.h>
35 #include <linux/interrupt.h>
36 #include <crypto/scatterwalk.h>
37 #include <crypto/aes.h>
38 #include <crypto/engine.h>
39 #include <crypto/internal/skcipher.h>
41 #define DST_MAXBURST 4
42 #define DMA_MIN (DST_MAXBURST * sizeof(u32))
44 #define _calc_walked(inout) (dd->inout##_walk.offset - dd->inout##_sg->offset)
46 /* OMAP TRM gives bitfields as start:end, where start is the higher bit
47 number. For example 7:0 */
48 #define FLD_MASK(start, end) (((1 << ((start) - (end) + 1)) - 1) << (end))
49 #define FLD_VAL(val, start, end) (((val) << (end)) & FLD_MASK(start, end))
51 #define AES_REG_KEY(dd, x) ((dd)->pdata->key_ofs - \
52 ((x ^ 0x01) * 0x04))
53 #define AES_REG_IV(dd, x) ((dd)->pdata->iv_ofs + ((x) * 0x04))
55 #define AES_REG_CTRL(dd) ((dd)->pdata->ctrl_ofs)
56 #define AES_REG_CTRL_CTR_WIDTH_MASK GENMASK(8, 7)
57 #define AES_REG_CTRL_CTR_WIDTH_32 0
58 #define AES_REG_CTRL_CTR_WIDTH_64 BIT(7)
59 #define AES_REG_CTRL_CTR_WIDTH_96 BIT(8)
60 #define AES_REG_CTRL_CTR_WIDTH_128 GENMASK(8, 7)
61 #define AES_REG_CTRL_CTR BIT(6)
62 #define AES_REG_CTRL_CBC BIT(5)
63 #define AES_REG_CTRL_KEY_SIZE GENMASK(4, 3)
64 #define AES_REG_CTRL_DIRECTION BIT(2)
65 #define AES_REG_CTRL_INPUT_READY BIT(1)
66 #define AES_REG_CTRL_OUTPUT_READY BIT(0)
67 #define AES_REG_CTRL_MASK GENMASK(24, 2)
69 #define AES_REG_DATA_N(dd, x) ((dd)->pdata->data_ofs + ((x) * 0x04))
71 #define AES_REG_REV(dd) ((dd)->pdata->rev_ofs)
73 #define AES_REG_MASK(dd) ((dd)->pdata->mask_ofs)
74 #define AES_REG_MASK_SIDLE BIT(6)
75 #define AES_REG_MASK_START BIT(5)
76 #define AES_REG_MASK_DMA_OUT_EN BIT(3)
77 #define AES_REG_MASK_DMA_IN_EN BIT(2)
78 #define AES_REG_MASK_SOFTRESET BIT(1)
79 #define AES_REG_AUTOIDLE BIT(0)
81 #define AES_REG_LENGTH_N(x) (0x54 + ((x) * 0x04))
83 #define AES_REG_IRQ_STATUS(dd) ((dd)->pdata->irq_status_ofs)
84 #define AES_REG_IRQ_ENABLE(dd) ((dd)->pdata->irq_enable_ofs)
85 #define AES_REG_IRQ_DATA_IN BIT(1)
86 #define AES_REG_IRQ_DATA_OUT BIT(2)
87 #define DEFAULT_TIMEOUT (5*HZ)
89 #define DEFAULT_AUTOSUSPEND_DELAY 1000
91 #define FLAGS_MODE_MASK 0x000f
92 #define FLAGS_ENCRYPT BIT(0)
93 #define FLAGS_CBC BIT(1)
94 #define FLAGS_GIV BIT(2)
95 #define FLAGS_CTR BIT(3)
97 #define FLAGS_INIT BIT(4)
98 #define FLAGS_FAST BIT(5)
99 #define FLAGS_BUSY BIT(6)
101 #define AES_BLOCK_WORDS (AES_BLOCK_SIZE >> 2)
103 struct omap_aes_ctx {
104 struct omap_aes_dev *dd;
106 int keylen;
107 u32 key[AES_KEYSIZE_256 / sizeof(u32)];
108 unsigned long flags;
109 struct crypto_skcipher *fallback;
112 struct omap_aes_reqctx {
113 unsigned long mode;
116 #define OMAP_AES_QUEUE_LENGTH 1
117 #define OMAP_AES_CACHE_SIZE 0
119 struct omap_aes_algs_info {
120 struct crypto_alg *algs_list;
121 unsigned int size;
122 unsigned int registered;
125 struct omap_aes_pdata {
126 struct omap_aes_algs_info *algs_info;
127 unsigned int algs_info_size;
129 void (*trigger)(struct omap_aes_dev *dd, int length);
131 u32 key_ofs;
132 u32 iv_ofs;
133 u32 ctrl_ofs;
134 u32 data_ofs;
135 u32 rev_ofs;
136 u32 mask_ofs;
137 u32 irq_enable_ofs;
138 u32 irq_status_ofs;
140 u32 dma_enable_in;
141 u32 dma_enable_out;
142 u32 dma_start;
144 u32 major_mask;
145 u32 major_shift;
146 u32 minor_mask;
147 u32 minor_shift;
150 struct omap_aes_dev {
151 struct list_head list;
152 unsigned long phys_base;
153 void __iomem *io_base;
154 struct omap_aes_ctx *ctx;
155 struct device *dev;
156 unsigned long flags;
157 int err;
159 struct tasklet_struct done_task;
161 struct ablkcipher_request *req;
162 struct crypto_engine *engine;
165 * total is used by PIO mode for book keeping so introduce
166 * variable total_save as need it to calc page_order
168 size_t total;
169 size_t total_save;
171 struct scatterlist *in_sg;
172 struct scatterlist *out_sg;
174 /* Buffers for copying for unaligned cases */
175 struct scatterlist in_sgl;
176 struct scatterlist out_sgl;
177 struct scatterlist *orig_out;
178 int sgs_copied;
180 struct scatter_walk in_walk;
181 struct scatter_walk out_walk;
182 struct dma_chan *dma_lch_in;
183 struct dma_chan *dma_lch_out;
184 int in_sg_len;
185 int out_sg_len;
186 int pio_only;
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);
194 #ifdef DEBUG
195 #define omap_aes_read(dd, offset) \
196 ({ \
197 int _read_ret; \
198 _read_ret = __raw_readl(dd->io_base + offset); \
199 pr_debug("omap_aes_read(" #offset "=%#x)= %#x\n", \
200 offset, _read_ret); \
201 _read_ret; \
203 #else
204 static inline u32 omap_aes_read(struct omap_aes_dev *dd, u32 offset)
206 return __raw_readl(dd->io_base + offset);
208 #endif
210 #ifdef DEBUG
211 #define omap_aes_write(dd, offset, value) \
212 do { \
213 pr_debug("omap_aes_write(" #offset "=%#x) value=%#x\n", \
214 offset, value); \
215 __raw_writel(value, dd->io_base + offset); \
216 } while (0)
217 #else
218 static inline void omap_aes_write(struct omap_aes_dev *dd, u32 offset,
219 u32 value)
221 __raw_writel(value, dd->io_base + offset);
223 #endif
225 static inline void omap_aes_write_mask(struct omap_aes_dev *dd, u32 offset,
226 u32 value, u32 mask)
228 u32 val;
230 val = omap_aes_read(dd, offset);
231 val &= ~mask;
232 val |= value;
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 int err;
247 if (!(dd->flags & FLAGS_INIT)) {
248 dd->flags |= FLAGS_INIT;
249 dd->err = 0;
252 err = pm_runtime_get_sync(dd->dev);
253 if (err < 0) {
254 dev_err(dd->dev, "failed to get sync: %d\n", err);
255 return err;
258 return 0;
261 static int omap_aes_write_ctrl(struct omap_aes_dev *dd)
263 unsigned int key32;
264 int i, err;
265 u32 val;
267 err = omap_aes_hw_init(dd);
268 if (err)
269 return err;
271 key32 = dd->ctx->keylen / sizeof(u32);
273 /* it seems a key should always be set even if it has not changed */
274 for (i = 0; i < key32; i++) {
275 omap_aes_write(dd, AES_REG_KEY(dd, i),
276 __le32_to_cpu(dd->ctx->key[i]));
279 if ((dd->flags & (FLAGS_CBC | FLAGS_CTR)) && dd->req->info)
280 omap_aes_write_n(dd, AES_REG_IV(dd, 0), dd->req->info, 4);
282 val = FLD_VAL(((dd->ctx->keylen >> 3) - 1), 4, 3);
283 if (dd->flags & FLAGS_CBC)
284 val |= AES_REG_CTRL_CBC;
285 if (dd->flags & FLAGS_CTR)
286 val |= AES_REG_CTRL_CTR | AES_REG_CTRL_CTR_WIDTH_128;
288 if (dd->flags & FLAGS_ENCRYPT)
289 val |= AES_REG_CTRL_DIRECTION;
291 omap_aes_write_mask(dd, AES_REG_CTRL(dd), val, AES_REG_CTRL_MASK);
293 return 0;
296 static void omap_aes_dma_trigger_omap2(struct omap_aes_dev *dd, int length)
298 u32 mask, val;
300 val = dd->pdata->dma_start;
302 if (dd->dma_lch_out != NULL)
303 val |= dd->pdata->dma_enable_out;
304 if (dd->dma_lch_in != NULL)
305 val |= dd->pdata->dma_enable_in;
307 mask = dd->pdata->dma_enable_out | dd->pdata->dma_enable_in |
308 dd->pdata->dma_start;
310 omap_aes_write_mask(dd, AES_REG_MASK(dd), val, mask);
314 static void omap_aes_dma_trigger_omap4(struct omap_aes_dev *dd, int length)
316 omap_aes_write(dd, AES_REG_LENGTH_N(0), length);
317 omap_aes_write(dd, AES_REG_LENGTH_N(1), 0);
319 omap_aes_dma_trigger_omap2(dd, length);
322 static void omap_aes_dma_stop(struct omap_aes_dev *dd)
324 u32 mask;
326 mask = dd->pdata->dma_enable_out | dd->pdata->dma_enable_in |
327 dd->pdata->dma_start;
329 omap_aes_write_mask(dd, AES_REG_MASK(dd), 0, mask);
332 static struct omap_aes_dev *omap_aes_find_dev(struct omap_aes_ctx *ctx)
334 struct omap_aes_dev *dd;
336 spin_lock_bh(&list_lock);
337 dd = list_first_entry(&dev_list, struct omap_aes_dev, list);
338 list_move_tail(&dd->list, &dev_list);
339 ctx->dd = dd;
340 spin_unlock_bh(&list_lock);
342 return dd;
345 static void omap_aes_dma_out_callback(void *data)
347 struct omap_aes_dev *dd = data;
349 /* dma_lch_out - completed */
350 tasklet_schedule(&dd->done_task);
353 static int omap_aes_dma_init(struct omap_aes_dev *dd)
355 int err;
357 dd->dma_lch_out = NULL;
358 dd->dma_lch_in = NULL;
360 dd->dma_lch_in = dma_request_chan(dd->dev, "rx");
361 if (IS_ERR(dd->dma_lch_in)) {
362 dev_err(dd->dev, "Unable to request in DMA channel\n");
363 return PTR_ERR(dd->dma_lch_in);
366 dd->dma_lch_out = dma_request_chan(dd->dev, "tx");
367 if (IS_ERR(dd->dma_lch_out)) {
368 dev_err(dd->dev, "Unable to request out DMA channel\n");
369 err = PTR_ERR(dd->dma_lch_out);
370 goto err_dma_out;
373 return 0;
375 err_dma_out:
376 dma_release_channel(dd->dma_lch_in);
378 return err;
381 static void omap_aes_dma_cleanup(struct omap_aes_dev *dd)
383 if (dd->pio_only)
384 return;
386 dma_release_channel(dd->dma_lch_out);
387 dma_release_channel(dd->dma_lch_in);
390 static void sg_copy_buf(void *buf, struct scatterlist *sg,
391 unsigned int start, unsigned int nbytes, int out)
393 struct scatter_walk walk;
395 if (!nbytes)
396 return;
398 scatterwalk_start(&walk, sg);
399 scatterwalk_advance(&walk, start);
400 scatterwalk_copychunks(buf, &walk, nbytes, out);
401 scatterwalk_done(&walk, out, 0);
404 static int omap_aes_crypt_dma(struct crypto_tfm *tfm,
405 struct scatterlist *in_sg, struct scatterlist *out_sg,
406 int in_sg_len, int out_sg_len)
408 struct omap_aes_ctx *ctx = crypto_tfm_ctx(tfm);
409 struct omap_aes_dev *dd = ctx->dd;
410 struct dma_async_tx_descriptor *tx_in, *tx_out;
411 struct dma_slave_config cfg;
412 int ret;
414 if (dd->pio_only) {
415 scatterwalk_start(&dd->in_walk, dd->in_sg);
416 scatterwalk_start(&dd->out_walk, dd->out_sg);
418 /* Enable DATAIN interrupt and let it take
419 care of the rest */
420 omap_aes_write(dd, AES_REG_IRQ_ENABLE(dd), 0x2);
421 return 0;
424 dma_sync_sg_for_device(dd->dev, dd->in_sg, in_sg_len, DMA_TO_DEVICE);
426 memset(&cfg, 0, sizeof(cfg));
428 cfg.src_addr = dd->phys_base + AES_REG_DATA_N(dd, 0);
429 cfg.dst_addr = dd->phys_base + AES_REG_DATA_N(dd, 0);
430 cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
431 cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
432 cfg.src_maxburst = DST_MAXBURST;
433 cfg.dst_maxburst = DST_MAXBURST;
435 /* IN */
436 ret = dmaengine_slave_config(dd->dma_lch_in, &cfg);
437 if (ret) {
438 dev_err(dd->dev, "can't configure IN dmaengine slave: %d\n",
439 ret);
440 return ret;
443 tx_in = dmaengine_prep_slave_sg(dd->dma_lch_in, in_sg, in_sg_len,
444 DMA_MEM_TO_DEV,
445 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
446 if (!tx_in) {
447 dev_err(dd->dev, "IN prep_slave_sg() failed\n");
448 return -EINVAL;
451 /* No callback necessary */
452 tx_in->callback_param = dd;
454 /* OUT */
455 ret = dmaengine_slave_config(dd->dma_lch_out, &cfg);
456 if (ret) {
457 dev_err(dd->dev, "can't configure OUT dmaengine slave: %d\n",
458 ret);
459 return ret;
462 tx_out = dmaengine_prep_slave_sg(dd->dma_lch_out, out_sg, out_sg_len,
463 DMA_DEV_TO_MEM,
464 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
465 if (!tx_out) {
466 dev_err(dd->dev, "OUT prep_slave_sg() failed\n");
467 return -EINVAL;
470 tx_out->callback = omap_aes_dma_out_callback;
471 tx_out->callback_param = dd;
473 dmaengine_submit(tx_in);
474 dmaengine_submit(tx_out);
476 dma_async_issue_pending(dd->dma_lch_in);
477 dma_async_issue_pending(dd->dma_lch_out);
479 /* start DMA */
480 dd->pdata->trigger(dd, dd->total);
482 return 0;
485 static int omap_aes_crypt_dma_start(struct omap_aes_dev *dd)
487 struct crypto_tfm *tfm = crypto_ablkcipher_tfm(
488 crypto_ablkcipher_reqtfm(dd->req));
489 int err;
491 pr_debug("total: %d\n", dd->total);
493 if (!dd->pio_only) {
494 err = dma_map_sg(dd->dev, dd->in_sg, dd->in_sg_len,
495 DMA_TO_DEVICE);
496 if (!err) {
497 dev_err(dd->dev, "dma_map_sg() error\n");
498 return -EINVAL;
501 err = dma_map_sg(dd->dev, dd->out_sg, dd->out_sg_len,
502 DMA_FROM_DEVICE);
503 if (!err) {
504 dev_err(dd->dev, "dma_map_sg() error\n");
505 return -EINVAL;
509 err = omap_aes_crypt_dma(tfm, dd->in_sg, dd->out_sg, dd->in_sg_len,
510 dd->out_sg_len);
511 if (err && !dd->pio_only) {
512 dma_unmap_sg(dd->dev, dd->in_sg, dd->in_sg_len, DMA_TO_DEVICE);
513 dma_unmap_sg(dd->dev, dd->out_sg, dd->out_sg_len,
514 DMA_FROM_DEVICE);
517 return err;
520 static void omap_aes_finish_req(struct omap_aes_dev *dd, int err)
522 struct ablkcipher_request *req = dd->req;
524 pr_debug("err: %d\n", err);
526 crypto_finalize_cipher_request(dd->engine, req, err);
528 pm_runtime_mark_last_busy(dd->dev);
529 pm_runtime_put_autosuspend(dd->dev);
532 static int omap_aes_crypt_dma_stop(struct omap_aes_dev *dd)
534 pr_debug("total: %d\n", dd->total);
536 omap_aes_dma_stop(dd);
539 return 0;
542 static int omap_aes_check_aligned(struct scatterlist *sg, int total)
544 int len = 0;
546 if (!IS_ALIGNED(total, AES_BLOCK_SIZE))
547 return -EINVAL;
549 while (sg) {
550 if (!IS_ALIGNED(sg->offset, 4))
551 return -1;
552 if (!IS_ALIGNED(sg->length, AES_BLOCK_SIZE))
553 return -1;
555 len += sg->length;
556 sg = sg_next(sg);
559 if (len != total)
560 return -1;
562 return 0;
565 static int omap_aes_copy_sgs(struct omap_aes_dev *dd)
567 void *buf_in, *buf_out;
568 int pages, total;
570 total = ALIGN(dd->total, AES_BLOCK_SIZE);
571 pages = get_order(total);
573 buf_in = (void *)__get_free_pages(GFP_ATOMIC, pages);
574 buf_out = (void *)__get_free_pages(GFP_ATOMIC, pages);
576 if (!buf_in || !buf_out) {
577 pr_err("Couldn't allocated pages for unaligned cases.\n");
578 return -1;
581 dd->orig_out = dd->out_sg;
583 sg_copy_buf(buf_in, dd->in_sg, 0, dd->total, 0);
585 sg_init_table(&dd->in_sgl, 1);
586 sg_set_buf(&dd->in_sgl, buf_in, total);
587 dd->in_sg = &dd->in_sgl;
588 dd->in_sg_len = 1;
590 sg_init_table(&dd->out_sgl, 1);
591 sg_set_buf(&dd->out_sgl, buf_out, total);
592 dd->out_sg = &dd->out_sgl;
593 dd->out_sg_len = 1;
595 return 0;
598 static int omap_aes_handle_queue(struct omap_aes_dev *dd,
599 struct ablkcipher_request *req)
601 if (req)
602 return crypto_transfer_cipher_request_to_engine(dd->engine, req);
604 return 0;
607 static int omap_aes_prepare_req(struct crypto_engine *engine,
608 struct ablkcipher_request *req)
610 struct omap_aes_ctx *ctx = crypto_ablkcipher_ctx(
611 crypto_ablkcipher_reqtfm(req));
612 struct omap_aes_dev *dd = ctx->dd;
613 struct omap_aes_reqctx *rctx;
615 if (!dd)
616 return -ENODEV;
618 /* assign new request to device */
619 dd->req = req;
620 dd->total = req->nbytes;
621 dd->total_save = req->nbytes;
622 dd->in_sg = req->src;
623 dd->out_sg = req->dst;
625 dd->in_sg_len = sg_nents_for_len(dd->in_sg, dd->total);
626 if (dd->in_sg_len < 0)
627 return dd->in_sg_len;
629 dd->out_sg_len = sg_nents_for_len(dd->out_sg, dd->total);
630 if (dd->out_sg_len < 0)
631 return dd->out_sg_len;
633 if (omap_aes_check_aligned(dd->in_sg, dd->total) ||
634 omap_aes_check_aligned(dd->out_sg, dd->total)) {
635 if (omap_aes_copy_sgs(dd))
636 pr_err("Failed to copy SGs for unaligned cases\n");
637 dd->sgs_copied = 1;
638 } else {
639 dd->sgs_copied = 0;
642 rctx = ablkcipher_request_ctx(req);
643 ctx = crypto_ablkcipher_ctx(crypto_ablkcipher_reqtfm(req));
644 rctx->mode &= FLAGS_MODE_MASK;
645 dd->flags = (dd->flags & ~FLAGS_MODE_MASK) | rctx->mode;
647 dd->ctx = ctx;
648 ctx->dd = dd;
650 return omap_aes_write_ctrl(dd);
653 static int omap_aes_crypt_req(struct crypto_engine *engine,
654 struct ablkcipher_request *req)
656 struct omap_aes_ctx *ctx = crypto_ablkcipher_ctx(
657 crypto_ablkcipher_reqtfm(req));
658 struct omap_aes_dev *dd = ctx->dd;
660 if (!dd)
661 return -ENODEV;
663 return omap_aes_crypt_dma_start(dd);
666 static void omap_aes_done_task(unsigned long data)
668 struct omap_aes_dev *dd = (struct omap_aes_dev *)data;
669 void *buf_in, *buf_out;
670 int pages, len;
672 pr_debug("enter done_task\n");
674 if (!dd->pio_only) {
675 dma_sync_sg_for_device(dd->dev, dd->out_sg, dd->out_sg_len,
676 DMA_FROM_DEVICE);
677 dma_unmap_sg(dd->dev, dd->in_sg, dd->in_sg_len, DMA_TO_DEVICE);
678 dma_unmap_sg(dd->dev, dd->out_sg, dd->out_sg_len,
679 DMA_FROM_DEVICE);
680 omap_aes_crypt_dma_stop(dd);
683 if (dd->sgs_copied) {
684 buf_in = sg_virt(&dd->in_sgl);
685 buf_out = sg_virt(&dd->out_sgl);
687 sg_copy_buf(buf_out, dd->orig_out, 0, dd->total_save, 1);
689 len = ALIGN(dd->total_save, AES_BLOCK_SIZE);
690 pages = get_order(len);
691 free_pages((unsigned long)buf_in, pages);
692 free_pages((unsigned long)buf_out, pages);
695 omap_aes_finish_req(dd, 0);
697 pr_debug("exit\n");
700 static int omap_aes_crypt(struct ablkcipher_request *req, unsigned long mode)
702 struct omap_aes_ctx *ctx = crypto_ablkcipher_ctx(
703 crypto_ablkcipher_reqtfm(req));
704 struct omap_aes_reqctx *rctx = ablkcipher_request_ctx(req);
705 struct omap_aes_dev *dd;
706 int ret;
708 pr_debug("nbytes: %d, enc: %d, cbc: %d\n", req->nbytes,
709 !!(mode & FLAGS_ENCRYPT),
710 !!(mode & FLAGS_CBC));
712 if (req->nbytes < 200) {
713 SKCIPHER_REQUEST_ON_STACK(subreq, ctx->fallback);
715 skcipher_request_set_tfm(subreq, ctx->fallback);
716 skcipher_request_set_callback(subreq, req->base.flags, NULL,
717 NULL);
718 skcipher_request_set_crypt(subreq, req->src, req->dst,
719 req->nbytes, req->info);
721 if (mode & FLAGS_ENCRYPT)
722 ret = crypto_skcipher_encrypt(subreq);
723 else
724 ret = crypto_skcipher_decrypt(subreq);
726 skcipher_request_zero(subreq);
727 return ret;
729 dd = omap_aes_find_dev(ctx);
730 if (!dd)
731 return -ENODEV;
733 rctx->mode = mode;
735 return omap_aes_handle_queue(dd, req);
738 /* ********************** ALG API ************************************ */
740 static int omap_aes_setkey(struct crypto_ablkcipher *tfm, const u8 *key,
741 unsigned int keylen)
743 struct omap_aes_ctx *ctx = crypto_ablkcipher_ctx(tfm);
744 int ret;
746 if (keylen != AES_KEYSIZE_128 && keylen != AES_KEYSIZE_192 &&
747 keylen != AES_KEYSIZE_256)
748 return -EINVAL;
750 pr_debug("enter, keylen: %d\n", keylen);
752 memcpy(ctx->key, key, keylen);
753 ctx->keylen = keylen;
755 crypto_skcipher_clear_flags(ctx->fallback, CRYPTO_TFM_REQ_MASK);
756 crypto_skcipher_set_flags(ctx->fallback, tfm->base.crt_flags &
757 CRYPTO_TFM_REQ_MASK);
759 ret = crypto_skcipher_setkey(ctx->fallback, key, keylen);
760 if (!ret)
761 return 0;
763 return 0;
766 static int omap_aes_ecb_encrypt(struct ablkcipher_request *req)
768 return omap_aes_crypt(req, FLAGS_ENCRYPT);
771 static int omap_aes_ecb_decrypt(struct ablkcipher_request *req)
773 return omap_aes_crypt(req, 0);
776 static int omap_aes_cbc_encrypt(struct ablkcipher_request *req)
778 return omap_aes_crypt(req, FLAGS_ENCRYPT | FLAGS_CBC);
781 static int omap_aes_cbc_decrypt(struct ablkcipher_request *req)
783 return omap_aes_crypt(req, FLAGS_CBC);
786 static int omap_aes_ctr_encrypt(struct ablkcipher_request *req)
788 return omap_aes_crypt(req, FLAGS_ENCRYPT | FLAGS_CTR);
791 static int omap_aes_ctr_decrypt(struct ablkcipher_request *req)
793 return omap_aes_crypt(req, FLAGS_CTR);
796 static int omap_aes_cra_init(struct crypto_tfm *tfm)
798 const char *name = crypto_tfm_alg_name(tfm);
799 const u32 flags = CRYPTO_ALG_ASYNC | CRYPTO_ALG_NEED_FALLBACK;
800 struct omap_aes_ctx *ctx = crypto_tfm_ctx(tfm);
801 struct crypto_skcipher *blk;
803 blk = crypto_alloc_skcipher(name, 0, flags);
804 if (IS_ERR(blk))
805 return PTR_ERR(blk);
807 ctx->fallback = blk;
809 tfm->crt_ablkcipher.reqsize = sizeof(struct omap_aes_reqctx);
811 return 0;
814 static void omap_aes_cra_exit(struct crypto_tfm *tfm)
816 struct omap_aes_ctx *ctx = crypto_tfm_ctx(tfm);
818 if (ctx->fallback)
819 crypto_free_skcipher(ctx->fallback);
821 ctx->fallback = NULL;
824 /* ********************** ALGS ************************************ */
826 static struct crypto_alg algs_ecb_cbc[] = {
828 .cra_name = "ecb(aes)",
829 .cra_driver_name = "ecb-aes-omap",
830 .cra_priority = 300,
831 .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER |
832 CRYPTO_ALG_KERN_DRIVER_ONLY |
833 CRYPTO_ALG_ASYNC | CRYPTO_ALG_NEED_FALLBACK,
834 .cra_blocksize = AES_BLOCK_SIZE,
835 .cra_ctxsize = sizeof(struct omap_aes_ctx),
836 .cra_alignmask = 0,
837 .cra_type = &crypto_ablkcipher_type,
838 .cra_module = THIS_MODULE,
839 .cra_init = omap_aes_cra_init,
840 .cra_exit = omap_aes_cra_exit,
841 .cra_u.ablkcipher = {
842 .min_keysize = AES_MIN_KEY_SIZE,
843 .max_keysize = AES_MAX_KEY_SIZE,
844 .setkey = omap_aes_setkey,
845 .encrypt = omap_aes_ecb_encrypt,
846 .decrypt = omap_aes_ecb_decrypt,
850 .cra_name = "cbc(aes)",
851 .cra_driver_name = "cbc-aes-omap",
852 .cra_priority = 300,
853 .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER |
854 CRYPTO_ALG_KERN_DRIVER_ONLY |
855 CRYPTO_ALG_ASYNC | CRYPTO_ALG_NEED_FALLBACK,
856 .cra_blocksize = AES_BLOCK_SIZE,
857 .cra_ctxsize = sizeof(struct omap_aes_ctx),
858 .cra_alignmask = 0,
859 .cra_type = &crypto_ablkcipher_type,
860 .cra_module = THIS_MODULE,
861 .cra_init = omap_aes_cra_init,
862 .cra_exit = omap_aes_cra_exit,
863 .cra_u.ablkcipher = {
864 .min_keysize = AES_MIN_KEY_SIZE,
865 .max_keysize = AES_MAX_KEY_SIZE,
866 .ivsize = AES_BLOCK_SIZE,
867 .setkey = omap_aes_setkey,
868 .encrypt = omap_aes_cbc_encrypt,
869 .decrypt = omap_aes_cbc_decrypt,
874 static struct crypto_alg algs_ctr[] = {
876 .cra_name = "ctr(aes)",
877 .cra_driver_name = "ctr-aes-omap",
878 .cra_priority = 300,
879 .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER |
880 CRYPTO_ALG_KERN_DRIVER_ONLY |
881 CRYPTO_ALG_ASYNC | CRYPTO_ALG_NEED_FALLBACK,
882 .cra_blocksize = AES_BLOCK_SIZE,
883 .cra_ctxsize = sizeof(struct omap_aes_ctx),
884 .cra_alignmask = 0,
885 .cra_type = &crypto_ablkcipher_type,
886 .cra_module = THIS_MODULE,
887 .cra_init = omap_aes_cra_init,
888 .cra_exit = omap_aes_cra_exit,
889 .cra_u.ablkcipher = {
890 .min_keysize = AES_MIN_KEY_SIZE,
891 .max_keysize = AES_MAX_KEY_SIZE,
892 .geniv = "eseqiv",
893 .ivsize = AES_BLOCK_SIZE,
894 .setkey = omap_aes_setkey,
895 .encrypt = omap_aes_ctr_encrypt,
896 .decrypt = omap_aes_ctr_decrypt,
901 static struct omap_aes_algs_info omap_aes_algs_info_ecb_cbc[] = {
903 .algs_list = algs_ecb_cbc,
904 .size = ARRAY_SIZE(algs_ecb_cbc),
908 static const struct omap_aes_pdata omap_aes_pdata_omap2 = {
909 .algs_info = omap_aes_algs_info_ecb_cbc,
910 .algs_info_size = ARRAY_SIZE(omap_aes_algs_info_ecb_cbc),
911 .trigger = omap_aes_dma_trigger_omap2,
912 .key_ofs = 0x1c,
913 .iv_ofs = 0x20,
914 .ctrl_ofs = 0x30,
915 .data_ofs = 0x34,
916 .rev_ofs = 0x44,
917 .mask_ofs = 0x48,
918 .dma_enable_in = BIT(2),
919 .dma_enable_out = BIT(3),
920 .dma_start = BIT(5),
921 .major_mask = 0xf0,
922 .major_shift = 4,
923 .minor_mask = 0x0f,
924 .minor_shift = 0,
927 #ifdef CONFIG_OF
928 static struct omap_aes_algs_info omap_aes_algs_info_ecb_cbc_ctr[] = {
930 .algs_list = algs_ecb_cbc,
931 .size = ARRAY_SIZE(algs_ecb_cbc),
934 .algs_list = algs_ctr,
935 .size = ARRAY_SIZE(algs_ctr),
939 static const struct omap_aes_pdata omap_aes_pdata_omap3 = {
940 .algs_info = omap_aes_algs_info_ecb_cbc_ctr,
941 .algs_info_size = ARRAY_SIZE(omap_aes_algs_info_ecb_cbc_ctr),
942 .trigger = omap_aes_dma_trigger_omap2,
943 .key_ofs = 0x1c,
944 .iv_ofs = 0x20,
945 .ctrl_ofs = 0x30,
946 .data_ofs = 0x34,
947 .rev_ofs = 0x44,
948 .mask_ofs = 0x48,
949 .dma_enable_in = BIT(2),
950 .dma_enable_out = BIT(3),
951 .dma_start = BIT(5),
952 .major_mask = 0xf0,
953 .major_shift = 4,
954 .minor_mask = 0x0f,
955 .minor_shift = 0,
958 static const struct omap_aes_pdata omap_aes_pdata_omap4 = {
959 .algs_info = omap_aes_algs_info_ecb_cbc_ctr,
960 .algs_info_size = ARRAY_SIZE(omap_aes_algs_info_ecb_cbc_ctr),
961 .trigger = omap_aes_dma_trigger_omap4,
962 .key_ofs = 0x3c,
963 .iv_ofs = 0x40,
964 .ctrl_ofs = 0x50,
965 .data_ofs = 0x60,
966 .rev_ofs = 0x80,
967 .mask_ofs = 0x84,
968 .irq_status_ofs = 0x8c,
969 .irq_enable_ofs = 0x90,
970 .dma_enable_in = BIT(5),
971 .dma_enable_out = BIT(6),
972 .major_mask = 0x0700,
973 .major_shift = 8,
974 .minor_mask = 0x003f,
975 .minor_shift = 0,
978 static irqreturn_t omap_aes_irq(int irq, void *dev_id)
980 struct omap_aes_dev *dd = dev_id;
981 u32 status, i;
982 u32 *src, *dst;
984 status = omap_aes_read(dd, AES_REG_IRQ_STATUS(dd));
985 if (status & AES_REG_IRQ_DATA_IN) {
986 omap_aes_write(dd, AES_REG_IRQ_ENABLE(dd), 0x0);
988 BUG_ON(!dd->in_sg);
990 BUG_ON(_calc_walked(in) > dd->in_sg->length);
992 src = sg_virt(dd->in_sg) + _calc_walked(in);
994 for (i = 0; i < AES_BLOCK_WORDS; i++) {
995 omap_aes_write(dd, AES_REG_DATA_N(dd, i), *src);
997 scatterwalk_advance(&dd->in_walk, 4);
998 if (dd->in_sg->length == _calc_walked(in)) {
999 dd->in_sg = sg_next(dd->in_sg);
1000 if (dd->in_sg) {
1001 scatterwalk_start(&dd->in_walk,
1002 dd->in_sg);
1003 src = sg_virt(dd->in_sg) +
1004 _calc_walked(in);
1006 } else {
1007 src++;
1011 /* Clear IRQ status */
1012 status &= ~AES_REG_IRQ_DATA_IN;
1013 omap_aes_write(dd, AES_REG_IRQ_STATUS(dd), status);
1015 /* Enable DATA_OUT interrupt */
1016 omap_aes_write(dd, AES_REG_IRQ_ENABLE(dd), 0x4);
1018 } else if (status & AES_REG_IRQ_DATA_OUT) {
1019 omap_aes_write(dd, AES_REG_IRQ_ENABLE(dd), 0x0);
1021 BUG_ON(!dd->out_sg);
1023 BUG_ON(_calc_walked(out) > dd->out_sg->length);
1025 dst = sg_virt(dd->out_sg) + _calc_walked(out);
1027 for (i = 0; i < AES_BLOCK_WORDS; i++) {
1028 *dst = omap_aes_read(dd, AES_REG_DATA_N(dd, i));
1029 scatterwalk_advance(&dd->out_walk, 4);
1030 if (dd->out_sg->length == _calc_walked(out)) {
1031 dd->out_sg = sg_next(dd->out_sg);
1032 if (dd->out_sg) {
1033 scatterwalk_start(&dd->out_walk,
1034 dd->out_sg);
1035 dst = sg_virt(dd->out_sg) +
1036 _calc_walked(out);
1038 } else {
1039 dst++;
1043 dd->total -= min_t(size_t, AES_BLOCK_SIZE, dd->total);
1045 /* Clear IRQ status */
1046 status &= ~AES_REG_IRQ_DATA_OUT;
1047 omap_aes_write(dd, AES_REG_IRQ_STATUS(dd), status);
1049 if (!dd->total)
1050 /* All bytes read! */
1051 tasklet_schedule(&dd->done_task);
1052 else
1053 /* Enable DATA_IN interrupt for next block */
1054 omap_aes_write(dd, AES_REG_IRQ_ENABLE(dd), 0x2);
1057 return IRQ_HANDLED;
1060 static const struct of_device_id omap_aes_of_match[] = {
1062 .compatible = "ti,omap2-aes",
1063 .data = &omap_aes_pdata_omap2,
1066 .compatible = "ti,omap3-aes",
1067 .data = &omap_aes_pdata_omap3,
1070 .compatible = "ti,omap4-aes",
1071 .data = &omap_aes_pdata_omap4,
1075 MODULE_DEVICE_TABLE(of, omap_aes_of_match);
1077 static int omap_aes_get_res_of(struct omap_aes_dev *dd,
1078 struct device *dev, struct resource *res)
1080 struct device_node *node = dev->of_node;
1081 const struct of_device_id *match;
1082 int err = 0;
1084 match = of_match_device(of_match_ptr(omap_aes_of_match), dev);
1085 if (!match) {
1086 dev_err(dev, "no compatible OF match\n");
1087 err = -EINVAL;
1088 goto err;
1091 err = of_address_to_resource(node, 0, res);
1092 if (err < 0) {
1093 dev_err(dev, "can't translate OF node address\n");
1094 err = -EINVAL;
1095 goto err;
1098 dd->pdata = match->data;
1100 err:
1101 return err;
1103 #else
1104 static const struct of_device_id omap_aes_of_match[] = {
1108 static int omap_aes_get_res_of(struct omap_aes_dev *dd,
1109 struct device *dev, struct resource *res)
1111 return -EINVAL;
1113 #endif
1115 static int omap_aes_get_res_pdev(struct omap_aes_dev *dd,
1116 struct platform_device *pdev, struct resource *res)
1118 struct device *dev = &pdev->dev;
1119 struct resource *r;
1120 int err = 0;
1122 /* Get the base address */
1123 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1124 if (!r) {
1125 dev_err(dev, "no MEM resource info\n");
1126 err = -ENODEV;
1127 goto err;
1129 memcpy(res, r, sizeof(*res));
1131 /* Only OMAP2/3 can be non-DT */
1132 dd->pdata = &omap_aes_pdata_omap2;
1134 err:
1135 return err;
1138 static int omap_aes_probe(struct platform_device *pdev)
1140 struct device *dev = &pdev->dev;
1141 struct omap_aes_dev *dd;
1142 struct crypto_alg *algp;
1143 struct resource res;
1144 int err = -ENOMEM, i, j, irq = -1;
1145 u32 reg;
1147 dd = devm_kzalloc(dev, sizeof(struct omap_aes_dev), GFP_KERNEL);
1148 if (dd == NULL) {
1149 dev_err(dev, "unable to alloc data struct.\n");
1150 goto err_data;
1152 dd->dev = dev;
1153 platform_set_drvdata(pdev, dd);
1155 err = (dev->of_node) ? omap_aes_get_res_of(dd, dev, &res) :
1156 omap_aes_get_res_pdev(dd, pdev, &res);
1157 if (err)
1158 goto err_res;
1160 dd->io_base = devm_ioremap_resource(dev, &res);
1161 if (IS_ERR(dd->io_base)) {
1162 err = PTR_ERR(dd->io_base);
1163 goto err_res;
1165 dd->phys_base = res.start;
1167 pm_runtime_use_autosuspend(dev);
1168 pm_runtime_set_autosuspend_delay(dev, DEFAULT_AUTOSUSPEND_DELAY);
1170 pm_runtime_enable(dev);
1171 err = pm_runtime_get_sync(dev);
1172 if (err < 0) {
1173 dev_err(dev, "%s: failed to get_sync(%d)\n",
1174 __func__, err);
1175 goto err_res;
1178 omap_aes_dma_stop(dd);
1180 reg = omap_aes_read(dd, AES_REG_REV(dd));
1182 pm_runtime_put_sync(dev);
1184 dev_info(dev, "OMAP AES hw accel rev: %u.%u\n",
1185 (reg & dd->pdata->major_mask) >> dd->pdata->major_shift,
1186 (reg & dd->pdata->minor_mask) >> dd->pdata->minor_shift);
1188 tasklet_init(&dd->done_task, omap_aes_done_task, (unsigned long)dd);
1190 err = omap_aes_dma_init(dd);
1191 if (err == -EPROBE_DEFER) {
1192 goto err_irq;
1193 } else if (err && AES_REG_IRQ_STATUS(dd) && AES_REG_IRQ_ENABLE(dd)) {
1194 dd->pio_only = 1;
1196 irq = platform_get_irq(pdev, 0);
1197 if (irq < 0) {
1198 dev_err(dev, "can't get IRQ resource\n");
1199 goto err_irq;
1202 err = devm_request_irq(dev, irq, omap_aes_irq, 0,
1203 dev_name(dev), dd);
1204 if (err) {
1205 dev_err(dev, "Unable to grab omap-aes IRQ\n");
1206 goto err_irq;
1211 INIT_LIST_HEAD(&dd->list);
1212 spin_lock(&list_lock);
1213 list_add_tail(&dd->list, &dev_list);
1214 spin_unlock(&list_lock);
1216 /* Initialize crypto engine */
1217 dd->engine = crypto_engine_alloc_init(dev, 1);
1218 if (!dd->engine) {
1219 err = -ENOMEM;
1220 goto err_engine;
1223 dd->engine->prepare_cipher_request = omap_aes_prepare_req;
1224 dd->engine->cipher_one_request = omap_aes_crypt_req;
1225 err = crypto_engine_start(dd->engine);
1226 if (err)
1227 goto err_engine;
1229 for (i = 0; i < dd->pdata->algs_info_size; i++) {
1230 if (!dd->pdata->algs_info[i].registered) {
1231 for (j = 0; j < dd->pdata->algs_info[i].size; j++) {
1232 algp = &dd->pdata->algs_info[i].algs_list[j];
1234 pr_debug("reg alg: %s\n", algp->cra_name);
1235 INIT_LIST_HEAD(&algp->cra_list);
1237 err = crypto_register_alg(algp);
1238 if (err)
1239 goto err_algs;
1241 dd->pdata->algs_info[i].registered++;
1246 return 0;
1247 err_algs:
1248 for (i = dd->pdata->algs_info_size - 1; i >= 0; i--)
1249 for (j = dd->pdata->algs_info[i].registered - 1; j >= 0; j--)
1250 crypto_unregister_alg(
1251 &dd->pdata->algs_info[i].algs_list[j]);
1253 err_engine:
1254 if (dd->engine)
1255 crypto_engine_exit(dd->engine);
1257 omap_aes_dma_cleanup(dd);
1258 err_irq:
1259 tasklet_kill(&dd->done_task);
1260 pm_runtime_disable(dev);
1261 err_res:
1262 dd = NULL;
1263 err_data:
1264 dev_err(dev, "initialization failed.\n");
1265 return err;
1268 static int omap_aes_remove(struct platform_device *pdev)
1270 struct omap_aes_dev *dd = platform_get_drvdata(pdev);
1271 int i, j;
1273 if (!dd)
1274 return -ENODEV;
1276 spin_lock(&list_lock);
1277 list_del(&dd->list);
1278 spin_unlock(&list_lock);
1280 for (i = dd->pdata->algs_info_size - 1; i >= 0; i--)
1281 for (j = dd->pdata->algs_info[i].registered - 1; j >= 0; j--)
1282 crypto_unregister_alg(
1283 &dd->pdata->algs_info[i].algs_list[j]);
1285 crypto_engine_exit(dd->engine);
1286 tasklet_kill(&dd->done_task);
1287 omap_aes_dma_cleanup(dd);
1288 pm_runtime_disable(dd->dev);
1289 dd = NULL;
1291 return 0;
1294 #ifdef CONFIG_PM_SLEEP
1295 static int omap_aes_suspend(struct device *dev)
1297 pm_runtime_put_sync(dev);
1298 return 0;
1301 static int omap_aes_resume(struct device *dev)
1303 pm_runtime_get_sync(dev);
1304 return 0;
1306 #endif
1308 static SIMPLE_DEV_PM_OPS(omap_aes_pm_ops, omap_aes_suspend, omap_aes_resume);
1310 static struct platform_driver omap_aes_driver = {
1311 .probe = omap_aes_probe,
1312 .remove = omap_aes_remove,
1313 .driver = {
1314 .name = "omap-aes",
1315 .pm = &omap_aes_pm_ops,
1316 .of_match_table = omap_aes_of_match,
1320 module_platform_driver(omap_aes_driver);
1322 MODULE_DESCRIPTION("OMAP AES hw acceleration support.");
1323 MODULE_LICENSE("GPL v2");
1324 MODULE_AUTHOR("Dmitry Kasatkin");