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>
31 #include <linux/of_device.h>
32 #include <linux/of_address.h>
34 #include <linux/crypto.h>
35 #include <linux/interrupt.h>
36 #include <crypto/scatterwalk.h>
37 #include <crypto/aes.h>
38 #include <crypto/gcm.h>
39 #include <crypto/engine.h>
40 #include <crypto/internal/skcipher.h>
41 #include <crypto/internal/aead.h>
43 #include "omap-crypto.h"
46 /* keep registered devices data here */
47 static LIST_HEAD(dev_list
);
48 static DEFINE_SPINLOCK(list_lock
);
50 static int aes_fallback_sz
= 200;
53 #define omap_aes_read(dd, offset) \
56 _read_ret = __raw_readl(dd->io_base + offset); \
57 pr_debug("omap_aes_read(" #offset "=%#x)= %#x\n", \
62 inline u32
omap_aes_read(struct omap_aes_dev
*dd
, u32 offset
)
64 return __raw_readl(dd
->io_base
+ offset
);
69 #define omap_aes_write(dd, offset, value) \
71 pr_debug("omap_aes_write(" #offset "=%#x) value=%#x\n", \
73 __raw_writel(value, dd->io_base + offset); \
76 inline void omap_aes_write(struct omap_aes_dev
*dd
, u32 offset
,
79 __raw_writel(value
, dd
->io_base
+ offset
);
83 static inline void omap_aes_write_mask(struct omap_aes_dev
*dd
, u32 offset
,
88 val
= omap_aes_read(dd
, offset
);
91 omap_aes_write(dd
, offset
, val
);
94 static void omap_aes_write_n(struct omap_aes_dev
*dd
, u32 offset
,
95 u32
*value
, int count
)
97 for (; count
--; value
++, offset
+= 4)
98 omap_aes_write(dd
, offset
, *value
);
101 static int omap_aes_hw_init(struct omap_aes_dev
*dd
)
105 if (!(dd
->flags
& FLAGS_INIT
)) {
106 dd
->flags
|= FLAGS_INIT
;
110 err
= pm_runtime_get_sync(dd
->dev
);
112 dev_err(dd
->dev
, "failed to get sync: %d\n", err
);
119 void omap_aes_clear_copy_flags(struct omap_aes_dev
*dd
)
121 dd
->flags
&= ~(OMAP_CRYPTO_COPY_MASK
<< FLAGS_IN_DATA_ST_SHIFT
);
122 dd
->flags
&= ~(OMAP_CRYPTO_COPY_MASK
<< FLAGS_OUT_DATA_ST_SHIFT
);
123 dd
->flags
&= ~(OMAP_CRYPTO_COPY_MASK
<< FLAGS_ASSOC_DATA_ST_SHIFT
);
126 int omap_aes_write_ctrl(struct omap_aes_dev
*dd
)
128 struct omap_aes_reqctx
*rctx
;
133 err
= omap_aes_hw_init(dd
);
137 key32
= dd
->ctx
->keylen
/ sizeof(u32
);
139 /* RESET the key as previous HASH keys should not get affected*/
140 if (dd
->flags
& FLAGS_GCM
)
141 for (i
= 0; i
< 0x40; i
= i
+ 4)
142 omap_aes_write(dd
, i
, 0x0);
144 for (i
= 0; i
< key32
; i
++) {
145 omap_aes_write(dd
, AES_REG_KEY(dd
, i
),
146 __le32_to_cpu(dd
->ctx
->key
[i
]));
149 if ((dd
->flags
& (FLAGS_CBC
| FLAGS_CTR
)) && dd
->req
->info
)
150 omap_aes_write_n(dd
, AES_REG_IV(dd
, 0), dd
->req
->info
, 4);
152 if ((dd
->flags
& (FLAGS_GCM
)) && dd
->aead_req
->iv
) {
153 rctx
= aead_request_ctx(dd
->aead_req
);
154 omap_aes_write_n(dd
, AES_REG_IV(dd
, 0), (u32
*)rctx
->iv
, 4);
157 val
= FLD_VAL(((dd
->ctx
->keylen
>> 3) - 1), 4, 3);
158 if (dd
->flags
& FLAGS_CBC
)
159 val
|= AES_REG_CTRL_CBC
;
161 if (dd
->flags
& (FLAGS_CTR
| FLAGS_GCM
))
162 val
|= AES_REG_CTRL_CTR
| AES_REG_CTRL_CTR_WIDTH_128
;
164 if (dd
->flags
& FLAGS_GCM
)
165 val
|= AES_REG_CTRL_GCM
;
167 if (dd
->flags
& FLAGS_ENCRYPT
)
168 val
|= AES_REG_CTRL_DIRECTION
;
170 omap_aes_write_mask(dd
, AES_REG_CTRL(dd
), val
, AES_REG_CTRL_MASK
);
175 static void omap_aes_dma_trigger_omap2(struct omap_aes_dev
*dd
, int length
)
179 val
= dd
->pdata
->dma_start
;
181 if (dd
->dma_lch_out
!= NULL
)
182 val
|= dd
->pdata
->dma_enable_out
;
183 if (dd
->dma_lch_in
!= NULL
)
184 val
|= dd
->pdata
->dma_enable_in
;
186 mask
= dd
->pdata
->dma_enable_out
| dd
->pdata
->dma_enable_in
|
187 dd
->pdata
->dma_start
;
189 omap_aes_write_mask(dd
, AES_REG_MASK(dd
), val
, mask
);
193 static void omap_aes_dma_trigger_omap4(struct omap_aes_dev
*dd
, int length
)
195 omap_aes_write(dd
, AES_REG_LENGTH_N(0), length
);
196 omap_aes_write(dd
, AES_REG_LENGTH_N(1), 0);
197 if (dd
->flags
& FLAGS_GCM
)
198 omap_aes_write(dd
, AES_REG_A_LEN
, dd
->assoc_len
);
200 omap_aes_dma_trigger_omap2(dd
, length
);
203 static void omap_aes_dma_stop(struct omap_aes_dev
*dd
)
207 mask
= dd
->pdata
->dma_enable_out
| dd
->pdata
->dma_enable_in
|
208 dd
->pdata
->dma_start
;
210 omap_aes_write_mask(dd
, AES_REG_MASK(dd
), 0, mask
);
213 struct omap_aes_dev
*omap_aes_find_dev(struct omap_aes_reqctx
*rctx
)
215 struct omap_aes_dev
*dd
;
217 spin_lock_bh(&list_lock
);
218 dd
= list_first_entry(&dev_list
, struct omap_aes_dev
, list
);
219 list_move_tail(&dd
->list
, &dev_list
);
221 spin_unlock_bh(&list_lock
);
226 static void omap_aes_dma_out_callback(void *data
)
228 struct omap_aes_dev
*dd
= data
;
230 /* dma_lch_out - completed */
231 tasklet_schedule(&dd
->done_task
);
234 static int omap_aes_dma_init(struct omap_aes_dev
*dd
)
238 dd
->dma_lch_out
= NULL
;
239 dd
->dma_lch_in
= NULL
;
241 dd
->dma_lch_in
= dma_request_chan(dd
->dev
, "rx");
242 if (IS_ERR(dd
->dma_lch_in
)) {
243 dev_err(dd
->dev
, "Unable to request in DMA channel\n");
244 return PTR_ERR(dd
->dma_lch_in
);
247 dd
->dma_lch_out
= dma_request_chan(dd
->dev
, "tx");
248 if (IS_ERR(dd
->dma_lch_out
)) {
249 dev_err(dd
->dev
, "Unable to request out DMA channel\n");
250 err
= PTR_ERR(dd
->dma_lch_out
);
257 dma_release_channel(dd
->dma_lch_in
);
262 static void omap_aes_dma_cleanup(struct omap_aes_dev
*dd
)
267 dma_release_channel(dd
->dma_lch_out
);
268 dma_release_channel(dd
->dma_lch_in
);
271 static int omap_aes_crypt_dma(struct omap_aes_dev
*dd
,
272 struct scatterlist
*in_sg
,
273 struct scatterlist
*out_sg
,
274 int in_sg_len
, int out_sg_len
)
276 struct dma_async_tx_descriptor
*tx_in
, *tx_out
;
277 struct dma_slave_config cfg
;
281 scatterwalk_start(&dd
->in_walk
, dd
->in_sg
);
282 scatterwalk_start(&dd
->out_walk
, dd
->out_sg
);
284 /* Enable DATAIN interrupt and let it take
286 omap_aes_write(dd
, AES_REG_IRQ_ENABLE(dd
), 0x2);
290 dma_sync_sg_for_device(dd
->dev
, dd
->in_sg
, in_sg_len
, DMA_TO_DEVICE
);
292 memset(&cfg
, 0, sizeof(cfg
));
294 cfg
.src_addr
= dd
->phys_base
+ AES_REG_DATA_N(dd
, 0);
295 cfg
.dst_addr
= dd
->phys_base
+ AES_REG_DATA_N(dd
, 0);
296 cfg
.src_addr_width
= DMA_SLAVE_BUSWIDTH_4_BYTES
;
297 cfg
.dst_addr_width
= DMA_SLAVE_BUSWIDTH_4_BYTES
;
298 cfg
.src_maxburst
= DST_MAXBURST
;
299 cfg
.dst_maxburst
= DST_MAXBURST
;
302 ret
= dmaengine_slave_config(dd
->dma_lch_in
, &cfg
);
304 dev_err(dd
->dev
, "can't configure IN dmaengine slave: %d\n",
309 tx_in
= dmaengine_prep_slave_sg(dd
->dma_lch_in
, in_sg
, in_sg_len
,
311 DMA_PREP_INTERRUPT
| DMA_CTRL_ACK
);
313 dev_err(dd
->dev
, "IN prep_slave_sg() failed\n");
317 /* No callback necessary */
318 tx_in
->callback_param
= dd
;
321 ret
= dmaengine_slave_config(dd
->dma_lch_out
, &cfg
);
323 dev_err(dd
->dev
, "can't configure OUT dmaengine slave: %d\n",
328 tx_out
= dmaengine_prep_slave_sg(dd
->dma_lch_out
, out_sg
, out_sg_len
,
330 DMA_PREP_INTERRUPT
| DMA_CTRL_ACK
);
332 dev_err(dd
->dev
, "OUT prep_slave_sg() failed\n");
336 if (dd
->flags
& FLAGS_GCM
)
337 tx_out
->callback
= omap_aes_gcm_dma_out_callback
;
339 tx_out
->callback
= omap_aes_dma_out_callback
;
340 tx_out
->callback_param
= dd
;
342 dmaengine_submit(tx_in
);
343 dmaengine_submit(tx_out
);
345 dma_async_issue_pending(dd
->dma_lch_in
);
346 dma_async_issue_pending(dd
->dma_lch_out
);
349 dd
->pdata
->trigger(dd
, dd
->total
);
354 int omap_aes_crypt_dma_start(struct omap_aes_dev
*dd
)
358 pr_debug("total: %d\n", dd
->total
);
361 err
= dma_map_sg(dd
->dev
, dd
->in_sg
, dd
->in_sg_len
,
364 dev_err(dd
->dev
, "dma_map_sg() error\n");
368 err
= dma_map_sg(dd
->dev
, dd
->out_sg
, dd
->out_sg_len
,
371 dev_err(dd
->dev
, "dma_map_sg() error\n");
376 err
= omap_aes_crypt_dma(dd
, dd
->in_sg
, dd
->out_sg
, dd
->in_sg_len
,
378 if (err
&& !dd
->pio_only
) {
379 dma_unmap_sg(dd
->dev
, dd
->in_sg
, dd
->in_sg_len
, DMA_TO_DEVICE
);
380 dma_unmap_sg(dd
->dev
, dd
->out_sg
, dd
->out_sg_len
,
387 static void omap_aes_finish_req(struct omap_aes_dev
*dd
, int err
)
389 struct ablkcipher_request
*req
= dd
->req
;
391 pr_debug("err: %d\n", err
);
393 crypto_finalize_ablkcipher_request(dd
->engine
, req
, err
);
395 pm_runtime_mark_last_busy(dd
->dev
);
396 pm_runtime_put_autosuspend(dd
->dev
);
399 int omap_aes_crypt_dma_stop(struct omap_aes_dev
*dd
)
401 pr_debug("total: %d\n", dd
->total
);
403 omap_aes_dma_stop(dd
);
409 static int omap_aes_handle_queue(struct omap_aes_dev
*dd
,
410 struct ablkcipher_request
*req
)
413 return crypto_transfer_ablkcipher_request_to_engine(dd
->engine
, req
);
418 static int omap_aes_prepare_req(struct crypto_engine
*engine
,
421 struct ablkcipher_request
*req
= container_of(areq
, struct ablkcipher_request
, base
);
422 struct omap_aes_ctx
*ctx
= crypto_ablkcipher_ctx(
423 crypto_ablkcipher_reqtfm(req
));
424 struct omap_aes_reqctx
*rctx
= ablkcipher_request_ctx(req
);
425 struct omap_aes_dev
*dd
= rctx
->dd
;
432 /* assign new request to device */
434 dd
->total
= req
->nbytes
;
435 dd
->total_save
= req
->nbytes
;
436 dd
->in_sg
= req
->src
;
437 dd
->out_sg
= req
->dst
;
438 dd
->orig_out
= req
->dst
;
440 flags
= OMAP_CRYPTO_COPY_DATA
;
441 if (req
->src
== req
->dst
)
442 flags
|= OMAP_CRYPTO_FORCE_COPY
;
444 ret
= omap_crypto_align_sg(&dd
->in_sg
, dd
->total
, AES_BLOCK_SIZE
,
446 FLAGS_IN_DATA_ST_SHIFT
, &dd
->flags
);
450 ret
= omap_crypto_align_sg(&dd
->out_sg
, dd
->total
, AES_BLOCK_SIZE
,
452 FLAGS_OUT_DATA_ST_SHIFT
, &dd
->flags
);
456 dd
->in_sg_len
= sg_nents_for_len(dd
->in_sg
, dd
->total
);
457 if (dd
->in_sg_len
< 0)
458 return dd
->in_sg_len
;
460 dd
->out_sg_len
= sg_nents_for_len(dd
->out_sg
, dd
->total
);
461 if (dd
->out_sg_len
< 0)
462 return dd
->out_sg_len
;
464 rctx
->mode
&= FLAGS_MODE_MASK
;
465 dd
->flags
= (dd
->flags
& ~FLAGS_MODE_MASK
) | rctx
->mode
;
470 return omap_aes_write_ctrl(dd
);
473 static int omap_aes_crypt_req(struct crypto_engine
*engine
,
476 struct ablkcipher_request
*req
= container_of(areq
, struct ablkcipher_request
, base
);
477 struct omap_aes_reqctx
*rctx
= ablkcipher_request_ctx(req
);
478 struct omap_aes_dev
*dd
= rctx
->dd
;
483 return omap_aes_crypt_dma_start(dd
);
486 static void omap_aes_done_task(unsigned long data
)
488 struct omap_aes_dev
*dd
= (struct omap_aes_dev
*)data
;
490 pr_debug("enter done_task\n");
493 dma_sync_sg_for_device(dd
->dev
, dd
->out_sg
, dd
->out_sg_len
,
495 dma_unmap_sg(dd
->dev
, dd
->in_sg
, dd
->in_sg_len
, DMA_TO_DEVICE
);
496 dma_unmap_sg(dd
->dev
, dd
->out_sg
, dd
->out_sg_len
,
498 omap_aes_crypt_dma_stop(dd
);
501 omap_crypto_cleanup(dd
->in_sgl
, NULL
, 0, dd
->total_save
,
502 FLAGS_IN_DATA_ST_SHIFT
, dd
->flags
);
504 omap_crypto_cleanup(&dd
->out_sgl
, dd
->orig_out
, 0, dd
->total_save
,
505 FLAGS_OUT_DATA_ST_SHIFT
, dd
->flags
);
507 omap_aes_finish_req(dd
, 0);
512 static int omap_aes_crypt(struct ablkcipher_request
*req
, unsigned long mode
)
514 struct omap_aes_ctx
*ctx
= crypto_ablkcipher_ctx(
515 crypto_ablkcipher_reqtfm(req
));
516 struct omap_aes_reqctx
*rctx
= ablkcipher_request_ctx(req
);
517 struct omap_aes_dev
*dd
;
520 pr_debug("nbytes: %d, enc: %d, cbc: %d\n", req
->nbytes
,
521 !!(mode
& FLAGS_ENCRYPT
),
522 !!(mode
& FLAGS_CBC
));
524 if (req
->nbytes
< aes_fallback_sz
) {
525 SKCIPHER_REQUEST_ON_STACK(subreq
, ctx
->fallback
);
527 skcipher_request_set_tfm(subreq
, ctx
->fallback
);
528 skcipher_request_set_callback(subreq
, req
->base
.flags
, NULL
,
530 skcipher_request_set_crypt(subreq
, req
->src
, req
->dst
,
531 req
->nbytes
, req
->info
);
533 if (mode
& FLAGS_ENCRYPT
)
534 ret
= crypto_skcipher_encrypt(subreq
);
536 ret
= crypto_skcipher_decrypt(subreq
);
538 skcipher_request_zero(subreq
);
541 dd
= omap_aes_find_dev(rctx
);
547 return omap_aes_handle_queue(dd
, req
);
550 /* ********************** ALG API ************************************ */
552 static int omap_aes_setkey(struct crypto_ablkcipher
*tfm
, const u8
*key
,
555 struct omap_aes_ctx
*ctx
= crypto_ablkcipher_ctx(tfm
);
558 if (keylen
!= AES_KEYSIZE_128
&& keylen
!= AES_KEYSIZE_192
&&
559 keylen
!= AES_KEYSIZE_256
)
562 pr_debug("enter, keylen: %d\n", keylen
);
564 memcpy(ctx
->key
, key
, keylen
);
565 ctx
->keylen
= keylen
;
567 crypto_skcipher_clear_flags(ctx
->fallback
, CRYPTO_TFM_REQ_MASK
);
568 crypto_skcipher_set_flags(ctx
->fallback
, tfm
->base
.crt_flags
&
569 CRYPTO_TFM_REQ_MASK
);
571 ret
= crypto_skcipher_setkey(ctx
->fallback
, key
, keylen
);
578 static int omap_aes_ecb_encrypt(struct ablkcipher_request
*req
)
580 return omap_aes_crypt(req
, FLAGS_ENCRYPT
);
583 static int omap_aes_ecb_decrypt(struct ablkcipher_request
*req
)
585 return omap_aes_crypt(req
, 0);
588 static int omap_aes_cbc_encrypt(struct ablkcipher_request
*req
)
590 return omap_aes_crypt(req
, FLAGS_ENCRYPT
| FLAGS_CBC
);
593 static int omap_aes_cbc_decrypt(struct ablkcipher_request
*req
)
595 return omap_aes_crypt(req
, FLAGS_CBC
);
598 static int omap_aes_ctr_encrypt(struct ablkcipher_request
*req
)
600 return omap_aes_crypt(req
, FLAGS_ENCRYPT
| FLAGS_CTR
);
603 static int omap_aes_ctr_decrypt(struct ablkcipher_request
*req
)
605 return omap_aes_crypt(req
, FLAGS_CTR
);
608 static int omap_aes_prepare_req(struct crypto_engine
*engine
,
610 static int omap_aes_crypt_req(struct crypto_engine
*engine
,
613 static int omap_aes_cra_init(struct crypto_tfm
*tfm
)
615 const char *name
= crypto_tfm_alg_name(tfm
);
616 const u32 flags
= CRYPTO_ALG_ASYNC
| CRYPTO_ALG_NEED_FALLBACK
;
617 struct omap_aes_ctx
*ctx
= crypto_tfm_ctx(tfm
);
618 struct crypto_skcipher
*blk
;
620 blk
= crypto_alloc_skcipher(name
, 0, flags
);
626 tfm
->crt_ablkcipher
.reqsize
= sizeof(struct omap_aes_reqctx
);
628 ctx
->enginectx
.op
.prepare_request
= omap_aes_prepare_req
;
629 ctx
->enginectx
.op
.unprepare_request
= NULL
;
630 ctx
->enginectx
.op
.do_one_request
= omap_aes_crypt_req
;
635 static int omap_aes_gcm_cra_init(struct crypto_aead
*tfm
)
637 struct omap_aes_dev
*dd
= NULL
;
638 struct omap_aes_ctx
*ctx
= crypto_aead_ctx(tfm
);
641 /* Find AES device, currently picks the first device */
642 spin_lock_bh(&list_lock
);
643 list_for_each_entry(dd
, &dev_list
, list
) {
646 spin_unlock_bh(&list_lock
);
648 err
= pm_runtime_get_sync(dd
->dev
);
650 dev_err(dd
->dev
, "%s: failed to get_sync(%d)\n",
655 tfm
->reqsize
= sizeof(struct omap_aes_reqctx
);
656 ctx
->ctr
= crypto_alloc_skcipher("ecb(aes)", 0, 0);
657 if (IS_ERR(ctx
->ctr
)) {
658 pr_warn("could not load aes driver for encrypting IV\n");
659 return PTR_ERR(ctx
->ctr
);
665 static void omap_aes_cra_exit(struct crypto_tfm
*tfm
)
667 struct omap_aes_ctx
*ctx
= crypto_tfm_ctx(tfm
);
670 crypto_free_skcipher(ctx
->fallback
);
672 ctx
->fallback
= NULL
;
675 static void omap_aes_gcm_cra_exit(struct crypto_aead
*tfm
)
677 struct omap_aes_ctx
*ctx
= crypto_aead_ctx(tfm
);
679 omap_aes_cra_exit(crypto_aead_tfm(tfm
));
682 crypto_free_skcipher(ctx
->ctr
);
685 /* ********************** ALGS ************************************ */
687 static struct crypto_alg algs_ecb_cbc
[] = {
689 .cra_name
= "ecb(aes)",
690 .cra_driver_name
= "ecb-aes-omap",
692 .cra_flags
= CRYPTO_ALG_TYPE_ABLKCIPHER
|
693 CRYPTO_ALG_KERN_DRIVER_ONLY
|
694 CRYPTO_ALG_ASYNC
| CRYPTO_ALG_NEED_FALLBACK
,
695 .cra_blocksize
= AES_BLOCK_SIZE
,
696 .cra_ctxsize
= sizeof(struct omap_aes_ctx
),
698 .cra_type
= &crypto_ablkcipher_type
,
699 .cra_module
= THIS_MODULE
,
700 .cra_init
= omap_aes_cra_init
,
701 .cra_exit
= omap_aes_cra_exit
,
702 .cra_u
.ablkcipher
= {
703 .min_keysize
= AES_MIN_KEY_SIZE
,
704 .max_keysize
= AES_MAX_KEY_SIZE
,
705 .setkey
= omap_aes_setkey
,
706 .encrypt
= omap_aes_ecb_encrypt
,
707 .decrypt
= omap_aes_ecb_decrypt
,
711 .cra_name
= "cbc(aes)",
712 .cra_driver_name
= "cbc-aes-omap",
714 .cra_flags
= CRYPTO_ALG_TYPE_ABLKCIPHER
|
715 CRYPTO_ALG_KERN_DRIVER_ONLY
|
716 CRYPTO_ALG_ASYNC
| CRYPTO_ALG_NEED_FALLBACK
,
717 .cra_blocksize
= AES_BLOCK_SIZE
,
718 .cra_ctxsize
= sizeof(struct omap_aes_ctx
),
720 .cra_type
= &crypto_ablkcipher_type
,
721 .cra_module
= THIS_MODULE
,
722 .cra_init
= omap_aes_cra_init
,
723 .cra_exit
= omap_aes_cra_exit
,
724 .cra_u
.ablkcipher
= {
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_cbc_encrypt
,
730 .decrypt
= omap_aes_cbc_decrypt
,
735 static struct crypto_alg algs_ctr
[] = {
737 .cra_name
= "ctr(aes)",
738 .cra_driver_name
= "ctr-aes-omap",
740 .cra_flags
= CRYPTO_ALG_TYPE_ABLKCIPHER
|
741 CRYPTO_ALG_KERN_DRIVER_ONLY
|
742 CRYPTO_ALG_ASYNC
| CRYPTO_ALG_NEED_FALLBACK
,
743 .cra_blocksize
= AES_BLOCK_SIZE
,
744 .cra_ctxsize
= sizeof(struct omap_aes_ctx
),
746 .cra_type
= &crypto_ablkcipher_type
,
747 .cra_module
= THIS_MODULE
,
748 .cra_init
= omap_aes_cra_init
,
749 .cra_exit
= omap_aes_cra_exit
,
750 .cra_u
.ablkcipher
= {
751 .min_keysize
= AES_MIN_KEY_SIZE
,
752 .max_keysize
= AES_MAX_KEY_SIZE
,
754 .ivsize
= AES_BLOCK_SIZE
,
755 .setkey
= omap_aes_setkey
,
756 .encrypt
= omap_aes_ctr_encrypt
,
757 .decrypt
= omap_aes_ctr_decrypt
,
762 static struct omap_aes_algs_info omap_aes_algs_info_ecb_cbc
[] = {
764 .algs_list
= algs_ecb_cbc
,
765 .size
= ARRAY_SIZE(algs_ecb_cbc
),
769 static struct aead_alg algs_aead_gcm
[] = {
772 .cra_name
= "gcm(aes)",
773 .cra_driver_name
= "gcm-aes-omap",
775 .cra_flags
= CRYPTO_ALG_ASYNC
|
776 CRYPTO_ALG_KERN_DRIVER_ONLY
,
778 .cra_ctxsize
= sizeof(struct omap_aes_ctx
),
779 .cra_alignmask
= 0xf,
780 .cra_module
= THIS_MODULE
,
782 .init
= omap_aes_gcm_cra_init
,
783 .exit
= omap_aes_gcm_cra_exit
,
784 .ivsize
= GCM_AES_IV_SIZE
,
785 .maxauthsize
= AES_BLOCK_SIZE
,
786 .setkey
= omap_aes_gcm_setkey
,
787 .encrypt
= omap_aes_gcm_encrypt
,
788 .decrypt
= omap_aes_gcm_decrypt
,
792 .cra_name
= "rfc4106(gcm(aes))",
793 .cra_driver_name
= "rfc4106-gcm-aes-omap",
795 .cra_flags
= CRYPTO_ALG_ASYNC
|
796 CRYPTO_ALG_KERN_DRIVER_ONLY
,
798 .cra_ctxsize
= sizeof(struct omap_aes_ctx
),
799 .cra_alignmask
= 0xf,
800 .cra_module
= THIS_MODULE
,
802 .init
= omap_aes_gcm_cra_init
,
803 .exit
= omap_aes_gcm_cra_exit
,
804 .maxauthsize
= AES_BLOCK_SIZE
,
805 .ivsize
= GCM_RFC4106_IV_SIZE
,
806 .setkey
= omap_aes_4106gcm_setkey
,
807 .encrypt
= omap_aes_4106gcm_encrypt
,
808 .decrypt
= omap_aes_4106gcm_decrypt
,
812 static struct omap_aes_aead_algs omap_aes_aead_info
= {
813 .algs_list
= algs_aead_gcm
,
814 .size
= ARRAY_SIZE(algs_aead_gcm
),
817 static const struct omap_aes_pdata omap_aes_pdata_omap2
= {
818 .algs_info
= omap_aes_algs_info_ecb_cbc
,
819 .algs_info_size
= ARRAY_SIZE(omap_aes_algs_info_ecb_cbc
),
820 .trigger
= omap_aes_dma_trigger_omap2
,
827 .dma_enable_in
= BIT(2),
828 .dma_enable_out
= BIT(3),
837 static struct omap_aes_algs_info omap_aes_algs_info_ecb_cbc_ctr
[] = {
839 .algs_list
= algs_ecb_cbc
,
840 .size
= ARRAY_SIZE(algs_ecb_cbc
),
843 .algs_list
= algs_ctr
,
844 .size
= ARRAY_SIZE(algs_ctr
),
848 static const struct omap_aes_pdata omap_aes_pdata_omap3
= {
849 .algs_info
= omap_aes_algs_info_ecb_cbc_ctr
,
850 .algs_info_size
= ARRAY_SIZE(omap_aes_algs_info_ecb_cbc_ctr
),
851 .trigger
= omap_aes_dma_trigger_omap2
,
858 .dma_enable_in
= BIT(2),
859 .dma_enable_out
= BIT(3),
867 static const struct omap_aes_pdata omap_aes_pdata_omap4
= {
868 .algs_info
= omap_aes_algs_info_ecb_cbc_ctr
,
869 .algs_info_size
= ARRAY_SIZE(omap_aes_algs_info_ecb_cbc_ctr
),
870 .aead_algs_info
= &omap_aes_aead_info
,
871 .trigger
= omap_aes_dma_trigger_omap4
,
878 .irq_status_ofs
= 0x8c,
879 .irq_enable_ofs
= 0x90,
880 .dma_enable_in
= BIT(5),
881 .dma_enable_out
= BIT(6),
882 .major_mask
= 0x0700,
884 .minor_mask
= 0x003f,
888 static irqreturn_t
omap_aes_irq(int irq
, void *dev_id
)
890 struct omap_aes_dev
*dd
= dev_id
;
894 status
= omap_aes_read(dd
, AES_REG_IRQ_STATUS(dd
));
895 if (status
& AES_REG_IRQ_DATA_IN
) {
896 omap_aes_write(dd
, AES_REG_IRQ_ENABLE(dd
), 0x0);
900 BUG_ON(_calc_walked(in
) > dd
->in_sg
->length
);
902 src
= sg_virt(dd
->in_sg
) + _calc_walked(in
);
904 for (i
= 0; i
< AES_BLOCK_WORDS
; i
++) {
905 omap_aes_write(dd
, AES_REG_DATA_N(dd
, i
), *src
);
907 scatterwalk_advance(&dd
->in_walk
, 4);
908 if (dd
->in_sg
->length
== _calc_walked(in
)) {
909 dd
->in_sg
= sg_next(dd
->in_sg
);
911 scatterwalk_start(&dd
->in_walk
,
913 src
= sg_virt(dd
->in_sg
) +
921 /* Clear IRQ status */
922 status
&= ~AES_REG_IRQ_DATA_IN
;
923 omap_aes_write(dd
, AES_REG_IRQ_STATUS(dd
), status
);
925 /* Enable DATA_OUT interrupt */
926 omap_aes_write(dd
, AES_REG_IRQ_ENABLE(dd
), 0x4);
928 } else if (status
& AES_REG_IRQ_DATA_OUT
) {
929 omap_aes_write(dd
, AES_REG_IRQ_ENABLE(dd
), 0x0);
933 BUG_ON(_calc_walked(out
) > dd
->out_sg
->length
);
935 dst
= sg_virt(dd
->out_sg
) + _calc_walked(out
);
937 for (i
= 0; i
< AES_BLOCK_WORDS
; i
++) {
938 *dst
= omap_aes_read(dd
, AES_REG_DATA_N(dd
, i
));
939 scatterwalk_advance(&dd
->out_walk
, 4);
940 if (dd
->out_sg
->length
== _calc_walked(out
)) {
941 dd
->out_sg
= sg_next(dd
->out_sg
);
943 scatterwalk_start(&dd
->out_walk
,
945 dst
= sg_virt(dd
->out_sg
) +
953 dd
->total
-= min_t(size_t, AES_BLOCK_SIZE
, dd
->total
);
955 /* Clear IRQ status */
956 status
&= ~AES_REG_IRQ_DATA_OUT
;
957 omap_aes_write(dd
, AES_REG_IRQ_STATUS(dd
), status
);
960 /* All bytes read! */
961 tasklet_schedule(&dd
->done_task
);
963 /* Enable DATA_IN interrupt for next block */
964 omap_aes_write(dd
, AES_REG_IRQ_ENABLE(dd
), 0x2);
970 static const struct of_device_id omap_aes_of_match
[] = {
972 .compatible
= "ti,omap2-aes",
973 .data
= &omap_aes_pdata_omap2
,
976 .compatible
= "ti,omap3-aes",
977 .data
= &omap_aes_pdata_omap3
,
980 .compatible
= "ti,omap4-aes",
981 .data
= &omap_aes_pdata_omap4
,
985 MODULE_DEVICE_TABLE(of
, omap_aes_of_match
);
987 static int omap_aes_get_res_of(struct omap_aes_dev
*dd
,
988 struct device
*dev
, struct resource
*res
)
990 struct device_node
*node
= dev
->of_node
;
993 dd
->pdata
= of_device_get_match_data(dev
);
995 dev_err(dev
, "no compatible OF match\n");
1000 err
= of_address_to_resource(node
, 0, res
);
1002 dev_err(dev
, "can't translate OF node address\n");
1011 static const struct of_device_id omap_aes_of_match
[] = {
1015 static int omap_aes_get_res_of(struct omap_aes_dev
*dd
,
1016 struct device
*dev
, struct resource
*res
)
1022 static int omap_aes_get_res_pdev(struct omap_aes_dev
*dd
,
1023 struct platform_device
*pdev
, struct resource
*res
)
1025 struct device
*dev
= &pdev
->dev
;
1029 /* Get the base address */
1030 r
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1032 dev_err(dev
, "no MEM resource info\n");
1036 memcpy(res
, r
, sizeof(*res
));
1038 /* Only OMAP2/3 can be non-DT */
1039 dd
->pdata
= &omap_aes_pdata_omap2
;
1045 static ssize_t
fallback_show(struct device
*dev
, struct device_attribute
*attr
,
1048 return sprintf(buf
, "%d\n", aes_fallback_sz
);
1051 static ssize_t
fallback_store(struct device
*dev
, struct device_attribute
*attr
,
1052 const char *buf
, size_t size
)
1057 status
= kstrtol(buf
, 0, &value
);
1061 /* HW accelerator only works with buffers > 9 */
1063 dev_err(dev
, "minimum fallback size 9\n");
1067 aes_fallback_sz
= value
;
1072 static ssize_t
queue_len_show(struct device
*dev
, struct device_attribute
*attr
,
1075 struct omap_aes_dev
*dd
= dev_get_drvdata(dev
);
1077 return sprintf(buf
, "%d\n", dd
->engine
->queue
.max_qlen
);
1080 static ssize_t
queue_len_store(struct device
*dev
,
1081 struct device_attribute
*attr
, const char *buf
,
1084 struct omap_aes_dev
*dd
;
1087 unsigned long flags
;
1089 status
= kstrtol(buf
, 0, &value
);
1097 * Changing the queue size in fly is safe, if size becomes smaller
1098 * than current size, it will just not accept new entries until
1099 * it has shrank enough.
1101 spin_lock_bh(&list_lock
);
1102 list_for_each_entry(dd
, &dev_list
, list
) {
1103 spin_lock_irqsave(&dd
->lock
, flags
);
1104 dd
->engine
->queue
.max_qlen
= value
;
1105 dd
->aead_queue
.base
.max_qlen
= value
;
1106 spin_unlock_irqrestore(&dd
->lock
, flags
);
1108 spin_unlock_bh(&list_lock
);
1113 static DEVICE_ATTR_RW(queue_len
);
1114 static DEVICE_ATTR_RW(fallback
);
1116 static struct attribute
*omap_aes_attrs
[] = {
1117 &dev_attr_queue_len
.attr
,
1118 &dev_attr_fallback
.attr
,
1122 static struct attribute_group omap_aes_attr_group
= {
1123 .attrs
= omap_aes_attrs
,
1126 static int omap_aes_probe(struct platform_device
*pdev
)
1128 struct device
*dev
= &pdev
->dev
;
1129 struct omap_aes_dev
*dd
;
1130 struct crypto_alg
*algp
;
1131 struct aead_alg
*aalg
;
1132 struct resource res
;
1133 int err
= -ENOMEM
, i
, j
, irq
= -1;
1136 dd
= devm_kzalloc(dev
, sizeof(struct omap_aes_dev
), GFP_KERNEL
);
1138 dev_err(dev
, "unable to alloc data struct.\n");
1142 platform_set_drvdata(pdev
, dd
);
1144 aead_init_queue(&dd
->aead_queue
, OMAP_AES_QUEUE_LENGTH
);
1146 err
= (dev
->of_node
) ? omap_aes_get_res_of(dd
, dev
, &res
) :
1147 omap_aes_get_res_pdev(dd
, pdev
, &res
);
1151 dd
->io_base
= devm_ioremap_resource(dev
, &res
);
1152 if (IS_ERR(dd
->io_base
)) {
1153 err
= PTR_ERR(dd
->io_base
);
1156 dd
->phys_base
= res
.start
;
1158 pm_runtime_use_autosuspend(dev
);
1159 pm_runtime_set_autosuspend_delay(dev
, DEFAULT_AUTOSUSPEND_DELAY
);
1161 pm_runtime_enable(dev
);
1162 err
= pm_runtime_get_sync(dev
);
1164 dev_err(dev
, "%s: failed to get_sync(%d)\n",
1169 omap_aes_dma_stop(dd
);
1171 reg
= omap_aes_read(dd
, AES_REG_REV(dd
));
1173 pm_runtime_put_sync(dev
);
1175 dev_info(dev
, "OMAP AES hw accel rev: %u.%u\n",
1176 (reg
& dd
->pdata
->major_mask
) >> dd
->pdata
->major_shift
,
1177 (reg
& dd
->pdata
->minor_mask
) >> dd
->pdata
->minor_shift
);
1179 tasklet_init(&dd
->done_task
, omap_aes_done_task
, (unsigned long)dd
);
1181 err
= omap_aes_dma_init(dd
);
1182 if (err
== -EPROBE_DEFER
) {
1184 } else if (err
&& AES_REG_IRQ_STATUS(dd
) && AES_REG_IRQ_ENABLE(dd
)) {
1187 irq
= platform_get_irq(pdev
, 0);
1189 dev_err(dev
, "can't get IRQ resource\n");
1194 err
= devm_request_irq(dev
, irq
, omap_aes_irq
, 0,
1197 dev_err(dev
, "Unable to grab omap-aes IRQ\n");
1202 spin_lock_init(&dd
->lock
);
1204 INIT_LIST_HEAD(&dd
->list
);
1205 spin_lock(&list_lock
);
1206 list_add_tail(&dd
->list
, &dev_list
);
1207 spin_unlock(&list_lock
);
1209 /* Initialize crypto engine */
1210 dd
->engine
= crypto_engine_alloc_init(dev
, 1);
1216 err
= crypto_engine_start(dd
->engine
);
1220 for (i
= 0; i
< dd
->pdata
->algs_info_size
; i
++) {
1221 if (!dd
->pdata
->algs_info
[i
].registered
) {
1222 for (j
= 0; j
< dd
->pdata
->algs_info
[i
].size
; j
++) {
1223 algp
= &dd
->pdata
->algs_info
[i
].algs_list
[j
];
1225 pr_debug("reg alg: %s\n", algp
->cra_name
);
1226 INIT_LIST_HEAD(&algp
->cra_list
);
1228 err
= crypto_register_alg(algp
);
1232 dd
->pdata
->algs_info
[i
].registered
++;
1237 if (dd
->pdata
->aead_algs_info
&&
1238 !dd
->pdata
->aead_algs_info
->registered
) {
1239 for (i
= 0; i
< dd
->pdata
->aead_algs_info
->size
; i
++) {
1240 aalg
= &dd
->pdata
->aead_algs_info
->algs_list
[i
];
1243 pr_debug("reg alg: %s\n", algp
->cra_name
);
1244 INIT_LIST_HEAD(&algp
->cra_list
);
1246 err
= crypto_register_aead(aalg
);
1250 dd
->pdata
->aead_algs_info
->registered
++;
1254 err
= sysfs_create_group(&dev
->kobj
, &omap_aes_attr_group
);
1256 dev_err(dev
, "could not create sysfs device attrs\n");
1262 for (i
= dd
->pdata
->aead_algs_info
->registered
- 1; i
>= 0; i
--) {
1263 aalg
= &dd
->pdata
->aead_algs_info
->algs_list
[i
];
1264 crypto_unregister_aead(aalg
);
1267 for (i
= dd
->pdata
->algs_info_size
- 1; i
>= 0; i
--)
1268 for (j
= dd
->pdata
->algs_info
[i
].registered
- 1; j
>= 0; j
--)
1269 crypto_unregister_alg(
1270 &dd
->pdata
->algs_info
[i
].algs_list
[j
]);
1274 crypto_engine_exit(dd
->engine
);
1276 omap_aes_dma_cleanup(dd
);
1278 tasklet_kill(&dd
->done_task
);
1279 pm_runtime_disable(dev
);
1283 dev_err(dev
, "initialization failed.\n");
1287 static int omap_aes_remove(struct platform_device
*pdev
)
1289 struct omap_aes_dev
*dd
= platform_get_drvdata(pdev
);
1290 struct aead_alg
*aalg
;
1296 spin_lock(&list_lock
);
1297 list_del(&dd
->list
);
1298 spin_unlock(&list_lock
);
1300 for (i
= dd
->pdata
->algs_info_size
- 1; i
>= 0; i
--)
1301 for (j
= dd
->pdata
->algs_info
[i
].registered
- 1; j
>= 0; j
--)
1302 crypto_unregister_alg(
1303 &dd
->pdata
->algs_info
[i
].algs_list
[j
]);
1305 for (i
= dd
->pdata
->aead_algs_info
->size
- 1; i
>= 0; i
--) {
1306 aalg
= &dd
->pdata
->aead_algs_info
->algs_list
[i
];
1307 crypto_unregister_aead(aalg
);
1310 crypto_engine_exit(dd
->engine
);
1312 tasklet_kill(&dd
->done_task
);
1313 omap_aes_dma_cleanup(dd
);
1314 pm_runtime_disable(dd
->dev
);
1320 #ifdef CONFIG_PM_SLEEP
1321 static int omap_aes_suspend(struct device
*dev
)
1323 pm_runtime_put_sync(dev
);
1327 static int omap_aes_resume(struct device
*dev
)
1329 pm_runtime_get_sync(dev
);
1334 static SIMPLE_DEV_PM_OPS(omap_aes_pm_ops
, omap_aes_suspend
, omap_aes_resume
);
1336 static struct platform_driver omap_aes_driver
= {
1337 .probe
= omap_aes_probe
,
1338 .remove
= omap_aes_remove
,
1341 .pm
= &omap_aes_pm_ops
,
1342 .of_match_table
= omap_aes_of_match
,
1346 module_platform_driver(omap_aes_driver
);
1348 MODULE_DESCRIPTION("OMAP AES hw acceleration support.");
1349 MODULE_LICENSE("GPL v2");
1350 MODULE_AUTHOR("Dmitry Kasatkin");