1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) STMicroelectronics SA 2017
5 * Ux500 support taken from snippets in the old Ux500 cryp driver
8 #include <crypto/aes.h>
9 #include <crypto/engine.h>
10 #include <crypto/internal/aead.h>
11 #include <crypto/internal/des.h>
12 #include <crypto/internal/skcipher.h>
13 #include <crypto/scatterwalk.h>
14 #include <linux/bottom_half.h>
15 #include <linux/clk.h>
16 #include <linux/delay.h>
17 #include <linux/dma-mapping.h>
18 #include <linux/dmaengine.h>
19 #include <linux/err.h>
20 #include <linux/iopoll.h>
21 #include <linux/interrupt.h>
22 #include <linux/kernel.h>
23 #include <linux/module.h>
25 #include <linux/platform_device.h>
26 #include <linux/pm_runtime.h>
27 #include <linux/reset.h>
28 #include <linux/string.h>
30 #define DRIVER_NAME "stm32-cryp"
32 /* Bit [0] encrypt / decrypt */
33 #define FLG_ENCRYPT BIT(0)
34 /* Bit [8..1] algo & operation mode */
35 #define FLG_AES BIT(1)
36 #define FLG_DES BIT(2)
37 #define FLG_TDES BIT(3)
38 #define FLG_ECB BIT(4)
39 #define FLG_CBC BIT(5)
40 #define FLG_CTR BIT(6)
41 #define FLG_GCM BIT(7)
42 #define FLG_CCM BIT(8)
43 /* Mode mask = bits [15..0] */
44 #define FLG_MODE_MASK GENMASK(15, 0)
45 /* Bit [31..16] status */
46 #define FLG_IN_OUT_DMA BIT(16)
47 #define FLG_HEADER_DMA BIT(17)
50 #define CRYP_CR 0x00000000
51 #define CRYP_SR 0x00000004
52 #define CRYP_DIN 0x00000008
53 #define CRYP_DOUT 0x0000000C
54 #define CRYP_DMACR 0x00000010
55 #define CRYP_IMSCR 0x00000014
56 #define CRYP_RISR 0x00000018
57 #define CRYP_MISR 0x0000001C
58 #define CRYP_K0LR 0x00000020
59 #define CRYP_K0RR 0x00000024
60 #define CRYP_K1LR 0x00000028
61 #define CRYP_K1RR 0x0000002C
62 #define CRYP_K2LR 0x00000030
63 #define CRYP_K2RR 0x00000034
64 #define CRYP_K3LR 0x00000038
65 #define CRYP_K3RR 0x0000003C
66 #define CRYP_IV0LR 0x00000040
67 #define CRYP_IV0RR 0x00000044
68 #define CRYP_IV1LR 0x00000048
69 #define CRYP_IV1RR 0x0000004C
70 #define CRYP_CSGCMCCM0R 0x00000050
71 #define CRYP_CSGCM0R 0x00000070
73 #define UX500_CRYP_CR 0x00000000
74 #define UX500_CRYP_SR 0x00000004
75 #define UX500_CRYP_DIN 0x00000008
76 #define UX500_CRYP_DINSIZE 0x0000000C
77 #define UX500_CRYP_DOUT 0x00000010
78 #define UX500_CRYP_DOUSIZE 0x00000014
79 #define UX500_CRYP_DMACR 0x00000018
80 #define UX500_CRYP_IMSC 0x0000001C
81 #define UX500_CRYP_RIS 0x00000020
82 #define UX500_CRYP_MIS 0x00000024
83 #define UX500_CRYP_K1L 0x00000028
84 #define UX500_CRYP_K1R 0x0000002C
85 #define UX500_CRYP_K2L 0x00000030
86 #define UX500_CRYP_K2R 0x00000034
87 #define UX500_CRYP_K3L 0x00000038
88 #define UX500_CRYP_K3R 0x0000003C
89 #define UX500_CRYP_K4L 0x00000040
90 #define UX500_CRYP_K4R 0x00000044
91 #define UX500_CRYP_IV0L 0x00000048
92 #define UX500_CRYP_IV0R 0x0000004C
93 #define UX500_CRYP_IV1L 0x00000050
94 #define UX500_CRYP_IV1R 0x00000054
96 /* Registers values */
97 #define CR_DEC_NOT_ENC 0x00000004
98 #define CR_TDES_ECB 0x00000000
99 #define CR_TDES_CBC 0x00000008
100 #define CR_DES_ECB 0x00000010
101 #define CR_DES_CBC 0x00000018
102 #define CR_AES_ECB 0x00000020
103 #define CR_AES_CBC 0x00000028
104 #define CR_AES_CTR 0x00000030
105 #define CR_AES_KP 0x00000038 /* Not on Ux500 */
106 #define CR_AES_XTS 0x00000038 /* Only on Ux500 */
107 #define CR_AES_GCM 0x00080000
108 #define CR_AES_CCM 0x00080008
109 #define CR_AES_UNKNOWN 0xFFFFFFFF
110 #define CR_ALGO_MASK 0x00080038
111 #define CR_DATA32 0x00000000
112 #define CR_DATA16 0x00000040
113 #define CR_DATA8 0x00000080
114 #define CR_DATA1 0x000000C0
115 #define CR_KEY128 0x00000000
116 #define CR_KEY192 0x00000100
117 #define CR_KEY256 0x00000200
118 #define CR_KEYRDEN 0x00000400 /* Only on Ux500 */
119 #define CR_KSE 0x00000800 /* Only on Ux500 */
120 #define CR_FFLUSH 0x00004000
121 #define CR_CRYPEN 0x00008000
122 #define CR_PH_INIT 0x00000000
123 #define CR_PH_HEADER 0x00010000
124 #define CR_PH_PAYLOAD 0x00020000
125 #define CR_PH_FINAL 0x00030000
126 #define CR_PH_MASK 0x00030000
127 #define CR_NBPBL_SHIFT 20
129 #define SR_IFNF BIT(1)
130 #define SR_OFNE BIT(2)
131 #define SR_BUSY BIT(8)
133 #define DMACR_DIEN BIT(0)
134 #define DMACR_DOEN BIT(1)
136 #define IMSCR_IN BIT(0)
137 #define IMSCR_OUT BIT(1)
139 #define MISR_IN BIT(0)
140 #define MISR_OUT BIT(1)
143 #define AES_BLOCK_32 (AES_BLOCK_SIZE / sizeof(u32))
144 #define GCM_CTR_INIT 2
145 #define CRYP_AUTOSUSPEND_DELAY 50
147 #define CRYP_DMA_BURST_REG 4
149 enum stm32_dma_mode {
155 struct stm32_cryp_caps {
178 struct stm32_cryp_ctx {
179 struct stm32_cryp *cryp;
181 __be32 key[AES_KEYSIZE_256 / sizeof(u32)];
185 struct stm32_cryp_reqctx {
190 struct list_head list;
193 phys_addr_t phys_base;
197 const struct stm32_cryp_caps *caps;
198 struct stm32_cryp_ctx *ctx;
200 struct crypto_engine *engine;
202 struct skcipher_request *req;
203 struct aead_request *areq;
212 /* DMA process fields */
213 struct scatterlist *in_sg;
214 struct scatterlist *header_sg;
215 struct scatterlist *out_sg;
217 size_t header_sg_len;
219 struct completion dma_completion;
221 struct dma_chan *dma_lch_in;
222 struct dma_chan *dma_lch_out;
223 enum stm32_dma_mode dma_mode;
225 /* IT process fields */
226 struct scatter_walk in_walk;
227 struct scatter_walk out_walk;
233 struct stm32_cryp_list {
234 struct list_head dev_list;
235 spinlock_t lock; /* protect dev_list */
238 static struct stm32_cryp_list cryp_list = {
239 .dev_list = LIST_HEAD_INIT(cryp_list.dev_list),
240 .lock = __SPIN_LOCK_UNLOCKED(cryp_list.lock),
243 static inline bool is_aes(struct stm32_cryp *cryp)
245 return cryp->flags & FLG_AES;
248 static inline bool is_des(struct stm32_cryp *cryp)
250 return cryp->flags & FLG_DES;
253 static inline bool is_tdes(struct stm32_cryp *cryp)
255 return cryp->flags & FLG_TDES;
258 static inline bool is_ecb(struct stm32_cryp *cryp)
260 return cryp->flags & FLG_ECB;
263 static inline bool is_cbc(struct stm32_cryp *cryp)
265 return cryp->flags & FLG_CBC;
268 static inline bool is_ctr(struct stm32_cryp *cryp)
270 return cryp->flags & FLG_CTR;
273 static inline bool is_gcm(struct stm32_cryp *cryp)
275 return cryp->flags & FLG_GCM;
278 static inline bool is_ccm(struct stm32_cryp *cryp)
280 return cryp->flags & FLG_CCM;
283 static inline bool is_encrypt(struct stm32_cryp *cryp)
285 return cryp->flags & FLG_ENCRYPT;
288 static inline bool is_decrypt(struct stm32_cryp *cryp)
290 return !is_encrypt(cryp);
293 static inline u32 stm32_cryp_read(struct stm32_cryp *cryp, u32 ofst)
295 return readl_relaxed(cryp->regs + ofst);
298 static inline void stm32_cryp_write(struct stm32_cryp *cryp, u32 ofst, u32 val)
300 writel_relaxed(val, cryp->regs + ofst);
303 static inline int stm32_cryp_wait_busy(struct stm32_cryp *cryp)
307 return readl_relaxed_poll_timeout(cryp->regs + cryp->caps->sr, status,
308 !(status & SR_BUSY), 10, 100000);
311 static inline void stm32_cryp_enable(struct stm32_cryp *cryp)
313 writel_relaxed(readl_relaxed(cryp->regs + cryp->caps->cr) | CR_CRYPEN,
314 cryp->regs + cryp->caps->cr);
317 static inline int stm32_cryp_wait_enable(struct stm32_cryp *cryp)
321 return readl_relaxed_poll_timeout(cryp->regs + cryp->caps->cr, status,
322 !(status & CR_CRYPEN), 10, 100000);
325 static inline int stm32_cryp_wait_input(struct stm32_cryp *cryp)
329 return readl_relaxed_poll_timeout_atomic(cryp->regs + cryp->caps->sr, status,
330 status & SR_IFNF, 1, 10);
333 static inline int stm32_cryp_wait_output(struct stm32_cryp *cryp)
337 return readl_relaxed_poll_timeout_atomic(cryp->regs + cryp->caps->sr, status,
338 status & SR_OFNE, 1, 10);
341 static inline void stm32_cryp_key_read_enable(struct stm32_cryp *cryp)
343 writel_relaxed(readl_relaxed(cryp->regs + cryp->caps->cr) | CR_KEYRDEN,
344 cryp->regs + cryp->caps->cr);
347 static inline void stm32_cryp_key_read_disable(struct stm32_cryp *cryp)
349 writel_relaxed(readl_relaxed(cryp->regs + cryp->caps->cr) & ~CR_KEYRDEN,
350 cryp->regs + cryp->caps->cr);
353 static void stm32_cryp_irq_read_data(struct stm32_cryp *cryp);
354 static void stm32_cryp_irq_write_data(struct stm32_cryp *cryp);
355 static void stm32_cryp_irq_write_gcmccm_header(struct stm32_cryp *cryp);
356 static int stm32_cryp_read_auth_tag(struct stm32_cryp *cryp);
357 static void stm32_cryp_finish_req(struct stm32_cryp *cryp, int err);
358 static int stm32_cryp_dma_start(struct stm32_cryp *cryp);
359 static int stm32_cryp_it_start(struct stm32_cryp *cryp);
361 static struct stm32_cryp *stm32_cryp_find_dev(struct stm32_cryp_ctx *ctx)
363 struct stm32_cryp *tmp, *cryp = NULL;
365 spin_lock_bh(&cryp_list.lock);
367 list_for_each_entry(tmp, &cryp_list.dev_list, list) {
376 spin_unlock_bh(&cryp_list.lock);
381 static void stm32_cryp_hw_write_iv(struct stm32_cryp *cryp, __be32 *iv)
386 stm32_cryp_write(cryp, cryp->caps->iv0l, be32_to_cpu(*iv++));
387 stm32_cryp_write(cryp, cryp->caps->iv0r, be32_to_cpu(*iv++));
390 stm32_cryp_write(cryp, cryp->caps->iv1l, be32_to_cpu(*iv++));
391 stm32_cryp_write(cryp, cryp->caps->iv1r, be32_to_cpu(*iv++));
395 static void stm32_cryp_get_iv(struct stm32_cryp *cryp)
397 struct skcipher_request *req = cryp->req;
398 __be32 *tmp = (void *)req->iv;
403 if (cryp->caps->iv_protection)
404 stm32_cryp_key_read_enable(cryp);
406 *tmp++ = cpu_to_be32(stm32_cryp_read(cryp, cryp->caps->iv0l));
407 *tmp++ = cpu_to_be32(stm32_cryp_read(cryp, cryp->caps->iv0r));
410 *tmp++ = cpu_to_be32(stm32_cryp_read(cryp, cryp->caps->iv1l));
411 *tmp++ = cpu_to_be32(stm32_cryp_read(cryp, cryp->caps->iv1r));
414 if (cryp->caps->iv_protection)
415 stm32_cryp_key_read_disable(cryp);
419 * ux500_swap_bits_in_byte() - mirror the bits in a byte
420 * @b: the byte to be mirrored
422 * The bits are swapped the following way:
423 * Byte b include bits 0-7, nibble 1 (n1) include bits 0-3 and
424 * nibble 2 (n2) bits 4-7.
427 * (The "old" (moved) bit is replaced with a zero)
428 * 1. Move bit 6 and 7, 4 positions to the left.
429 * 2. Move bit 3 and 5, 2 positions to the left.
430 * 3. Move bit 1-4, 1 position to the left.
433 * 1. Move bit 0 and 1, 4 positions to the right.
434 * 2. Move bit 2 and 4, 2 positions to the right.
435 * 3. Move bit 3-6, 1 position to the right.
437 * Combine the two nibbles to a complete and swapped byte.
439 static inline u8 ux500_swap_bits_in_byte(u8 b)
441 #define R_SHIFT_4_MASK 0xc0 /* Bits 6 and 7, right shift 4 */
442 #define R_SHIFT_2_MASK 0x28 /* (After right shift 4) Bits 3 and 5,
444 #define R_SHIFT_1_MASK 0x1e /* (After right shift 2) Bits 1-4,
446 #define L_SHIFT_4_MASK 0x03 /* Bits 0 and 1, left shift 4 */
447 #define L_SHIFT_2_MASK 0x14 /* (After left shift 4) Bits 2 and 4,
449 #define L_SHIFT_1_MASK 0x78 /* (After left shift 1) Bits 3-6,
455 /* Swap most significant nibble */
456 /* Right shift 4, bits 6 and 7 */
457 n1 = ((b & R_SHIFT_4_MASK) >> 4) | (b & ~(R_SHIFT_4_MASK >> 4));
458 /* Right shift 2, bits 3 and 5 */
459 n1 = ((n1 & R_SHIFT_2_MASK) >> 2) | (n1 & ~(R_SHIFT_2_MASK >> 2));
460 /* Right shift 1, bits 1-4 */
461 n1 = (n1 & R_SHIFT_1_MASK) >> 1;
463 /* Swap least significant nibble */
464 /* Left shift 4, bits 0 and 1 */
465 n2 = ((b & L_SHIFT_4_MASK) << 4) | (b & ~(L_SHIFT_4_MASK << 4));
466 /* Left shift 2, bits 2 and 4 */
467 n2 = ((n2 & L_SHIFT_2_MASK) << 2) | (n2 & ~(L_SHIFT_2_MASK << 2));
468 /* Left shift 1, bits 3-6 */
469 n2 = (n2 & L_SHIFT_1_MASK) << 1;
475 * ux500_swizzle_key() - Shuffle around words and bits in the AES key
476 * @in: key to swizzle
478 * @len: length of key, in bytes
480 * This "key swizzling procedure" is described in the examples in the
481 * DB8500 design specification. There is no real description of why
482 * the bits have been arranged like this in the hardware.
484 static inline void ux500_swizzle_key(const u8 *in, u8 *out, u32 len)
487 int bpw = sizeof(u32);
493 for (i = 0; i < bpw; i++) {
494 index = len - j - bpw + i;
496 ux500_swap_bits_in_byte(in[index]);
502 static void stm32_cryp_hw_write_key(struct stm32_cryp *c)
508 stm32_cryp_write(c, c->caps->k1l, be32_to_cpu(c->ctx->key[0]));
509 stm32_cryp_write(c, c->caps->k1r, be32_to_cpu(c->ctx->key[1]));
514 * On the Ux500 the AES key is considered as a single bit sequence
515 * of 128, 192 or 256 bits length. It is written linearly into the
516 * registers from K1L and down, and need to be processed to become
517 * a proper big-endian bit sequence.
519 if (is_aes(c) && c->caps->linear_aes_key) {
522 ux500_swizzle_key((u8 *)c->ctx->key,
523 (u8 *)tmpkey, c->ctx->keylen);
526 for (i = 0; i < c->ctx->keylen / sizeof(u32); i++, r_id += 4)
527 stm32_cryp_write(c, r_id, tmpkey[i]);
533 for (i = c->ctx->keylen / sizeof(u32); i > 0; i--, r_id -= 4)
534 stm32_cryp_write(c, r_id, be32_to_cpu(c->ctx->key[i - 1]));
537 static u32 stm32_cryp_get_hw_mode(struct stm32_cryp *cryp)
539 if (is_aes(cryp) && is_ecb(cryp))
542 if (is_aes(cryp) && is_cbc(cryp))
545 if (is_aes(cryp) && is_ctr(cryp))
548 if (is_aes(cryp) && is_gcm(cryp))
551 if (is_aes(cryp) && is_ccm(cryp))
554 if (is_des(cryp) && is_ecb(cryp))
557 if (is_des(cryp) && is_cbc(cryp))
560 if (is_tdes(cryp) && is_ecb(cryp))
563 if (is_tdes(cryp) && is_cbc(cryp))
566 dev_err(cryp->dev, "Unknown mode\n");
567 return CR_AES_UNKNOWN;
570 static unsigned int stm32_cryp_get_input_text_len(struct stm32_cryp *cryp)
572 return is_encrypt(cryp) ? cryp->areq->cryptlen :
573 cryp->areq->cryptlen - cryp->authsize;
576 static int stm32_cryp_gcm_init(struct stm32_cryp *cryp, u32 cfg)
582 memcpy(iv, cryp->areq->iv, 12);
583 iv[3] = cpu_to_be32(GCM_CTR_INIT);
584 cryp->gcm_ctr = GCM_CTR_INIT;
585 stm32_cryp_hw_write_iv(cryp, iv);
587 stm32_cryp_write(cryp, cryp->caps->cr, cfg | CR_PH_INIT | CR_CRYPEN);
589 /* Wait for end of processing */
590 ret = stm32_cryp_wait_enable(cryp);
592 dev_err(cryp->dev, "Timeout (gcm init)\n");
596 /* Prepare next phase */
597 if (cryp->areq->assoclen) {
599 stm32_cryp_write(cryp, cryp->caps->cr, cfg);
600 } else if (stm32_cryp_get_input_text_len(cryp)) {
601 cfg |= CR_PH_PAYLOAD;
602 stm32_cryp_write(cryp, cryp->caps->cr, cfg);
608 static void stm32_crypt_gcmccm_end_header(struct stm32_cryp *cryp)
613 /* Check if whole header written */
614 if (!cryp->header_in) {
615 /* Wait for completion */
616 err = stm32_cryp_wait_busy(cryp);
618 dev_err(cryp->dev, "Timeout (gcm/ccm header)\n");
619 stm32_cryp_write(cryp, cryp->caps->imsc, 0);
620 stm32_cryp_finish_req(cryp, err);
624 if (stm32_cryp_get_input_text_len(cryp)) {
625 /* Phase 3 : payload */
626 cfg = stm32_cryp_read(cryp, cryp->caps->cr);
628 stm32_cryp_write(cryp, cryp->caps->cr, cfg);
631 cfg |= CR_PH_PAYLOAD | CR_CRYPEN;
632 stm32_cryp_write(cryp, cryp->caps->cr, cfg);
636 * Nothing to read, nothing to write, caller have to
643 static void stm32_cryp_write_ccm_first_header(struct stm32_cryp *cryp)
647 u32 alen = cryp->areq->assoclen;
648 u32 block[AES_BLOCK_32] = {0};
649 u8 *b8 = (u8 *)block;
652 /* Write first u32 of B1 */
653 b8[0] = (alen >> 8) & 0xFF;
657 /* Build the two first u32 of B1 */
660 b8[2] = (alen & 0xFF000000) >> 24;
661 b8[3] = (alen & 0x00FF0000) >> 16;
662 b8[4] = (alen & 0x0000FF00) >> 8;
663 b8[5] = alen & 0x000000FF;
667 written = min_t(size_t, AES_BLOCK_SIZE - len, alen);
669 scatterwalk_copychunks((char *)block + len, &cryp->in_walk, written, 0);
671 writesl(cryp->regs + cryp->caps->din, block, AES_BLOCK_32);
673 cryp->header_in -= written;
675 stm32_crypt_gcmccm_end_header(cryp);
678 static int stm32_cryp_ccm_init(struct stm32_cryp *cryp, u32 cfg)
681 u32 iv_32[AES_BLOCK_32], b0_32[AES_BLOCK_32];
682 u8 *iv = (u8 *)iv_32, *b0 = (u8 *)b0_32;
685 unsigned int i, textlen;
687 /* Phase 1 : init. Firstly set the CTR value to 1 (not 0) */
688 memcpy(iv, cryp->areq->iv, AES_BLOCK_SIZE);
689 memset(iv + AES_BLOCK_SIZE - 1 - iv[0], 0, iv[0] + 1);
690 iv[AES_BLOCK_SIZE - 1] = 1;
691 stm32_cryp_hw_write_iv(cryp, (__be32 *)iv);
694 memcpy(b0, iv, AES_BLOCK_SIZE);
696 b0[0] |= (8 * ((cryp->authsize - 2) / 2));
698 if (cryp->areq->assoclen)
701 textlen = stm32_cryp_get_input_text_len(cryp);
703 b0[AES_BLOCK_SIZE - 2] = textlen >> 8;
704 b0[AES_BLOCK_SIZE - 1] = textlen & 0xFF;
707 stm32_cryp_write(cryp, cryp->caps->cr, cfg | CR_PH_INIT | CR_CRYPEN);
713 for (i = 0; i < AES_BLOCK_32; i++) {
716 if (!cryp->caps->padding_wa)
717 xd = be32_to_cpu(bd[i]);
718 stm32_cryp_write(cryp, cryp->caps->din, xd);
721 /* Wait for end of processing */
722 ret = stm32_cryp_wait_enable(cryp);
724 dev_err(cryp->dev, "Timeout (ccm init)\n");
728 /* Prepare next phase */
729 if (cryp->areq->assoclen) {
730 cfg |= CR_PH_HEADER | CR_CRYPEN;
731 stm32_cryp_write(cryp, cryp->caps->cr, cfg);
733 /* Write first (special) block (may move to next phase [payload]) */
734 stm32_cryp_write_ccm_first_header(cryp);
735 } else if (stm32_cryp_get_input_text_len(cryp)) {
736 cfg |= CR_PH_PAYLOAD;
737 stm32_cryp_write(cryp, cryp->caps->cr, cfg);
743 static int stm32_cryp_hw_init(struct stm32_cryp *cryp)
748 pm_runtime_get_sync(cryp->dev);
750 /* Disable interrupt */
751 stm32_cryp_write(cryp, cryp->caps->imsc, 0);
753 /* Set configuration */
754 cfg = CR_DATA8 | CR_FFLUSH;
756 switch (cryp->ctx->keylen) {
757 case AES_KEYSIZE_128:
761 case AES_KEYSIZE_192:
766 case AES_KEYSIZE_256:
771 hw_mode = stm32_cryp_get_hw_mode(cryp);
772 if (hw_mode == CR_AES_UNKNOWN)
775 /* AES ECB/CBC decrypt: run key preparation first */
776 if (is_decrypt(cryp) &&
777 ((hw_mode == CR_AES_ECB) || (hw_mode == CR_AES_CBC))) {
778 /* Configure in key preparation mode */
779 if (cryp->caps->kp_mode)
780 stm32_cryp_write(cryp, cryp->caps->cr,
783 stm32_cryp_write(cryp,
784 cryp->caps->cr, cfg | CR_AES_ECB | CR_KSE);
786 /* Set key only after full configuration done */
787 stm32_cryp_hw_write_key(cryp);
789 /* Start prepare key */
790 stm32_cryp_enable(cryp);
791 /* Wait for end of processing */
792 ret = stm32_cryp_wait_busy(cryp);
794 dev_err(cryp->dev, "Timeout (key preparation)\n");
798 cfg |= hw_mode | CR_DEC_NOT_ENC;
800 /* Apply updated config (Decrypt + algo) and flush */
801 stm32_cryp_write(cryp, cryp->caps->cr, cfg);
804 if (is_decrypt(cryp))
805 cfg |= CR_DEC_NOT_ENC;
807 /* Apply config and flush */
808 stm32_cryp_write(cryp, cryp->caps->cr, cfg);
810 /* Set key only after configuration done */
811 stm32_cryp_hw_write_key(cryp);
818 if (hw_mode == CR_AES_CCM)
819 ret = stm32_cryp_ccm_init(cryp, cfg);
821 ret = stm32_cryp_gcm_init(cryp, cfg);
832 stm32_cryp_hw_write_iv(cryp, (__be32 *)cryp->req->iv);
840 stm32_cryp_enable(cryp);
845 static void stm32_cryp_finish_req(struct stm32_cryp *cryp, int err)
847 if (!err && (is_gcm(cryp) || is_ccm(cryp)))
848 /* Phase 4 : output tag */
849 err = stm32_cryp_read_auth_tag(cryp);
851 if (!err && (!(is_gcm(cryp) || is_ccm(cryp) || is_ecb(cryp))))
852 stm32_cryp_get_iv(cryp);
854 pm_runtime_mark_last_busy(cryp->dev);
855 pm_runtime_put_autosuspend(cryp->dev);
857 if (is_gcm(cryp) || is_ccm(cryp))
858 crypto_finalize_aead_request(cryp->engine, cryp->areq, err);
860 crypto_finalize_skcipher_request(cryp->engine, cryp->req, err);
863 static void stm32_cryp_header_dma_callback(void *param)
865 struct stm32_cryp *cryp = (struct stm32_cryp *)param;
869 dma_unmap_sg(cryp->dev, cryp->header_sg, cryp->header_sg_len, DMA_TO_DEVICE);
871 reg = stm32_cryp_read(cryp, cryp->caps->dmacr);
872 stm32_cryp_write(cryp, cryp->caps->dmacr, reg & ~(DMACR_DOEN | DMACR_DIEN));
874 kfree(cryp->header_sg);
876 reg = stm32_cryp_read(cryp, cryp->caps->cr);
878 if (cryp->header_in) {
879 stm32_cryp_write(cryp, cryp->caps->cr, reg | CR_CRYPEN);
881 ret = stm32_cryp_wait_input(cryp);
883 dev_err(cryp->dev, "input header ready timeout after dma\n");
884 stm32_cryp_finish_req(cryp, ret);
887 stm32_cryp_irq_write_gcmccm_header(cryp);
888 WARN_ON(cryp->header_in);
891 if (stm32_cryp_get_input_text_len(cryp)) {
892 /* Phase 3 : payload */
893 reg = stm32_cryp_read(cryp, cryp->caps->cr);
894 stm32_cryp_write(cryp, cryp->caps->cr, reg & ~CR_CRYPEN);
897 reg |= CR_PH_PAYLOAD | CR_CRYPEN;
898 stm32_cryp_write(cryp, cryp->caps->cr, reg);
900 if (cryp->flags & FLG_IN_OUT_DMA) {
901 ret = stm32_cryp_dma_start(cryp);
903 stm32_cryp_finish_req(cryp, ret);
905 stm32_cryp_it_start(cryp);
910 * Nothing to read, nothing to write => end request
912 stm32_cryp_finish_req(cryp, 0);
916 static void stm32_cryp_dma_callback(void *param)
918 struct stm32_cryp *cryp = (struct stm32_cryp *)param;
922 complete(&cryp->dma_completion); /* completion to indicate no timeout */
924 dma_sync_sg_for_device(cryp->dev, cryp->out_sg, cryp->out_sg_len, DMA_FROM_DEVICE);
926 if (cryp->in_sg != cryp->out_sg)
927 dma_unmap_sg(cryp->dev, cryp->in_sg, cryp->in_sg_len, DMA_TO_DEVICE);
929 dma_unmap_sg(cryp->dev, cryp->out_sg, cryp->out_sg_len, DMA_FROM_DEVICE);
931 reg = stm32_cryp_read(cryp, cryp->caps->dmacr);
932 stm32_cryp_write(cryp, cryp->caps->dmacr, reg & ~(DMACR_DOEN | DMACR_DIEN));
934 reg = stm32_cryp_read(cryp, cryp->caps->cr);
936 if (is_gcm(cryp) || is_ccm(cryp)) {
940 if (cryp->in_sg != cryp->req->src)
942 if (cryp->out_sg != cryp->req->dst)
946 if (cryp->payload_in) {
947 stm32_cryp_write(cryp, cryp->caps->cr, reg | CR_CRYPEN);
949 ret = stm32_cryp_wait_input(cryp);
951 dev_err(cryp->dev, "input ready timeout after dma\n");
952 stm32_cryp_finish_req(cryp, ret);
955 stm32_cryp_irq_write_data(cryp);
957 ret = stm32_cryp_wait_output(cryp);
959 dev_err(cryp->dev, "output ready timeout after dma\n");
960 stm32_cryp_finish_req(cryp, ret);
963 stm32_cryp_irq_read_data(cryp);
966 stm32_cryp_finish_req(cryp, 0);
969 static int stm32_cryp_header_dma_start(struct stm32_cryp *cryp)
972 struct dma_async_tx_descriptor *tx_in;
976 ret = dma_map_sg(cryp->dev, cryp->header_sg, cryp->header_sg_len, DMA_TO_DEVICE);
978 dev_err(cryp->dev, "dma_map_sg() error\n");
982 dma_sync_sg_for_device(cryp->dev, cryp->header_sg, cryp->header_sg_len, DMA_TO_DEVICE);
984 tx_in = dmaengine_prep_slave_sg(cryp->dma_lch_in, cryp->header_sg, cryp->header_sg_len,
985 DMA_MEM_TO_DEV, DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
987 dev_err(cryp->dev, "IN prep_slave_sg() failed\n");
991 tx_in->callback_param = cryp;
992 tx_in->callback = stm32_cryp_header_dma_callback;
994 /* Advance scatterwalk to not DMA'ed data */
995 align_size = ALIGN_DOWN(cryp->header_in, cryp->hw_blocksize);
996 scatterwalk_copychunks(NULL, &cryp->in_walk, align_size, 2);
997 cryp->header_in -= align_size;
999 ret = dma_submit_error(dmaengine_submit(tx_in));
1001 dev_err(cryp->dev, "DMA in submit failed\n");
1004 dma_async_issue_pending(cryp->dma_lch_in);
1006 reg = stm32_cryp_read(cryp, cryp->caps->dmacr);
1007 stm32_cryp_write(cryp, cryp->caps->dmacr, reg | DMACR_DIEN);
1012 static int stm32_cryp_dma_start(struct stm32_cryp *cryp)
1016 struct dma_async_tx_descriptor *tx_in, *tx_out;
1019 if (cryp->in_sg != cryp->out_sg) {
1020 ret = dma_map_sg(cryp->dev, cryp->in_sg, cryp->in_sg_len, DMA_TO_DEVICE);
1022 dev_err(cryp->dev, "dma_map_sg() error\n");
1027 ret = dma_map_sg(cryp->dev, cryp->out_sg, cryp->out_sg_len, DMA_FROM_DEVICE);
1029 dev_err(cryp->dev, "dma_map_sg() error\n");
1033 dma_sync_sg_for_device(cryp->dev, cryp->in_sg, cryp->in_sg_len, DMA_TO_DEVICE);
1035 tx_in = dmaengine_prep_slave_sg(cryp->dma_lch_in, cryp->in_sg, cryp->in_sg_len,
1036 DMA_MEM_TO_DEV, DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
1038 dev_err(cryp->dev, "IN prep_slave_sg() failed\n");
1042 /* No callback necessary */
1043 tx_in->callback_param = cryp;
1044 tx_in->callback = NULL;
1046 tx_out = dmaengine_prep_slave_sg(cryp->dma_lch_out, cryp->out_sg, cryp->out_sg_len,
1047 DMA_DEV_TO_MEM, DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
1049 dev_err(cryp->dev, "OUT prep_slave_sg() failed\n");
1053 reinit_completion(&cryp->dma_completion);
1054 tx_out->callback = stm32_cryp_dma_callback;
1055 tx_out->callback_param = cryp;
1057 /* Advance scatterwalk to not DMA'ed data */
1058 align_size = ALIGN_DOWN(cryp->payload_in, cryp->hw_blocksize);
1059 scatterwalk_copychunks(NULL, &cryp->in_walk, align_size, 2);
1060 cryp->payload_in -= align_size;
1062 ret = dma_submit_error(dmaengine_submit(tx_in));
1064 dev_err(cryp->dev, "DMA in submit failed\n");
1067 dma_async_issue_pending(cryp->dma_lch_in);
1069 /* Advance scatterwalk to not DMA'ed data */
1070 scatterwalk_copychunks(NULL, &cryp->out_walk, align_size, 2);
1071 cryp->payload_out -= align_size;
1072 ret = dma_submit_error(dmaengine_submit(tx_out));
1074 dev_err(cryp->dev, "DMA out submit failed\n");
1077 dma_async_issue_pending(cryp->dma_lch_out);
1079 reg = stm32_cryp_read(cryp, cryp->caps->dmacr);
1080 stm32_cryp_write(cryp, cryp->caps->dmacr, reg | DMACR_DOEN | DMACR_DIEN);
1082 if (!wait_for_completion_timeout(&cryp->dma_completion, msecs_to_jiffies(1000))) {
1083 dev_err(cryp->dev, "DMA out timed out\n");
1084 dmaengine_terminate_sync(cryp->dma_lch_out);
1091 static int stm32_cryp_it_start(struct stm32_cryp *cryp)
1093 /* Enable interrupt and let the IRQ handler do everything */
1094 stm32_cryp_write(cryp, cryp->caps->imsc, IMSCR_IN | IMSCR_OUT);
1099 static int stm32_cryp_cipher_one_req(struct crypto_engine *engine, void *areq);
1101 static int stm32_cryp_init_tfm(struct crypto_skcipher *tfm)
1103 crypto_skcipher_set_reqsize(tfm, sizeof(struct stm32_cryp_reqctx));
1108 static int stm32_cryp_aead_one_req(struct crypto_engine *engine, void *areq);
1110 static int stm32_cryp_aes_aead_init(struct crypto_aead *tfm)
1112 crypto_aead_set_reqsize(tfm, sizeof(struct stm32_cryp_reqctx));
1117 static int stm32_cryp_crypt(struct skcipher_request *req, unsigned long mode)
1119 struct stm32_cryp_ctx *ctx = crypto_skcipher_ctx(
1120 crypto_skcipher_reqtfm(req));
1121 struct stm32_cryp_reqctx *rctx = skcipher_request_ctx(req);
1122 struct stm32_cryp *cryp = stm32_cryp_find_dev(ctx);
1129 return crypto_transfer_skcipher_request_to_engine(cryp->engine, req);
1132 static int stm32_cryp_aead_crypt(struct aead_request *req, unsigned long mode)
1134 struct stm32_cryp_ctx *ctx = crypto_aead_ctx(crypto_aead_reqtfm(req));
1135 struct stm32_cryp_reqctx *rctx = aead_request_ctx(req);
1136 struct stm32_cryp *cryp = stm32_cryp_find_dev(ctx);
1143 return crypto_transfer_aead_request_to_engine(cryp->engine, req);
1146 static int stm32_cryp_setkey(struct crypto_skcipher *tfm, const u8 *key,
1147 unsigned int keylen)
1149 struct stm32_cryp_ctx *ctx = crypto_skcipher_ctx(tfm);
1151 memcpy(ctx->key, key, keylen);
1152 ctx->keylen = keylen;
1157 static int stm32_cryp_aes_setkey(struct crypto_skcipher *tfm, const u8 *key,
1158 unsigned int keylen)
1160 if (keylen != AES_KEYSIZE_128 && keylen != AES_KEYSIZE_192 &&
1161 keylen != AES_KEYSIZE_256)
1164 return stm32_cryp_setkey(tfm, key, keylen);
1167 static int stm32_cryp_des_setkey(struct crypto_skcipher *tfm, const u8 *key,
1168 unsigned int keylen)
1170 return verify_skcipher_des_key(tfm, key) ?:
1171 stm32_cryp_setkey(tfm, key, keylen);
1174 static int stm32_cryp_tdes_setkey(struct crypto_skcipher *tfm, const u8 *key,
1175 unsigned int keylen)
1177 return verify_skcipher_des3_key(tfm, key) ?:
1178 stm32_cryp_setkey(tfm, key, keylen);
1181 static int stm32_cryp_aes_aead_setkey(struct crypto_aead *tfm, const u8 *key,
1182 unsigned int keylen)
1184 struct stm32_cryp_ctx *ctx = crypto_aead_ctx(tfm);
1186 if (keylen != AES_KEYSIZE_128 && keylen != AES_KEYSIZE_192 &&
1187 keylen != AES_KEYSIZE_256)
1190 memcpy(ctx->key, key, keylen);
1191 ctx->keylen = keylen;
1196 static int stm32_cryp_aes_gcm_setauthsize(struct crypto_aead *tfm,
1197 unsigned int authsize)
1215 static int stm32_cryp_aes_ccm_setauthsize(struct crypto_aead *tfm,
1216 unsigned int authsize)
1234 static int stm32_cryp_aes_ecb_encrypt(struct skcipher_request *req)
1236 if (req->cryptlen % AES_BLOCK_SIZE)
1239 if (req->cryptlen == 0)
1242 return stm32_cryp_crypt(req, FLG_AES | FLG_ECB | FLG_ENCRYPT);
1245 static int stm32_cryp_aes_ecb_decrypt(struct skcipher_request *req)
1247 if (req->cryptlen % AES_BLOCK_SIZE)
1250 if (req->cryptlen == 0)
1253 return stm32_cryp_crypt(req, FLG_AES | FLG_ECB);
1256 static int stm32_cryp_aes_cbc_encrypt(struct skcipher_request *req)
1258 if (req->cryptlen % AES_BLOCK_SIZE)
1261 if (req->cryptlen == 0)
1264 return stm32_cryp_crypt(req, FLG_AES | FLG_CBC | FLG_ENCRYPT);
1267 static int stm32_cryp_aes_cbc_decrypt(struct skcipher_request *req)
1269 if (req->cryptlen % AES_BLOCK_SIZE)
1272 if (req->cryptlen == 0)
1275 return stm32_cryp_crypt(req, FLG_AES | FLG_CBC);
1278 static int stm32_cryp_aes_ctr_encrypt(struct skcipher_request *req)
1280 if (req->cryptlen == 0)
1283 return stm32_cryp_crypt(req, FLG_AES | FLG_CTR | FLG_ENCRYPT);
1286 static int stm32_cryp_aes_ctr_decrypt(struct skcipher_request *req)
1288 if (req->cryptlen == 0)
1291 return stm32_cryp_crypt(req, FLG_AES | FLG_CTR);
1294 static int stm32_cryp_aes_gcm_encrypt(struct aead_request *req)
1296 return stm32_cryp_aead_crypt(req, FLG_AES | FLG_GCM | FLG_ENCRYPT);
1299 static int stm32_cryp_aes_gcm_decrypt(struct aead_request *req)
1301 return stm32_cryp_aead_crypt(req, FLG_AES | FLG_GCM);
1304 static inline int crypto_ccm_check_iv(const u8 *iv)
1306 /* 2 <= L <= 8, so 1 <= L' <= 7. */
1307 if (iv[0] < 1 || iv[0] > 7)
1313 static int stm32_cryp_aes_ccm_encrypt(struct aead_request *req)
1317 err = crypto_ccm_check_iv(req->iv);
1321 return stm32_cryp_aead_crypt(req, FLG_AES | FLG_CCM | FLG_ENCRYPT);
1324 static int stm32_cryp_aes_ccm_decrypt(struct aead_request *req)
1328 err = crypto_ccm_check_iv(req->iv);
1332 return stm32_cryp_aead_crypt(req, FLG_AES | FLG_CCM);
1335 static int stm32_cryp_des_ecb_encrypt(struct skcipher_request *req)
1337 if (req->cryptlen % DES_BLOCK_SIZE)
1340 if (req->cryptlen == 0)
1343 return stm32_cryp_crypt(req, FLG_DES | FLG_ECB | FLG_ENCRYPT);
1346 static int stm32_cryp_des_ecb_decrypt(struct skcipher_request *req)
1348 if (req->cryptlen % DES_BLOCK_SIZE)
1351 if (req->cryptlen == 0)
1354 return stm32_cryp_crypt(req, FLG_DES | FLG_ECB);
1357 static int stm32_cryp_des_cbc_encrypt(struct skcipher_request *req)
1359 if (req->cryptlen % DES_BLOCK_SIZE)
1362 if (req->cryptlen == 0)
1365 return stm32_cryp_crypt(req, FLG_DES | FLG_CBC | FLG_ENCRYPT);
1368 static int stm32_cryp_des_cbc_decrypt(struct skcipher_request *req)
1370 if (req->cryptlen % DES_BLOCK_SIZE)
1373 if (req->cryptlen == 0)
1376 return stm32_cryp_crypt(req, FLG_DES | FLG_CBC);
1379 static int stm32_cryp_tdes_ecb_encrypt(struct skcipher_request *req)
1381 if (req->cryptlen % DES_BLOCK_SIZE)
1384 if (req->cryptlen == 0)
1387 return stm32_cryp_crypt(req, FLG_TDES | FLG_ECB | FLG_ENCRYPT);
1390 static int stm32_cryp_tdes_ecb_decrypt(struct skcipher_request *req)
1392 if (req->cryptlen % DES_BLOCK_SIZE)
1395 if (req->cryptlen == 0)
1398 return stm32_cryp_crypt(req, FLG_TDES | FLG_ECB);
1401 static int stm32_cryp_tdes_cbc_encrypt(struct skcipher_request *req)
1403 if (req->cryptlen % DES_BLOCK_SIZE)
1406 if (req->cryptlen == 0)
1409 return stm32_cryp_crypt(req, FLG_TDES | FLG_CBC | FLG_ENCRYPT);
1412 static int stm32_cryp_tdes_cbc_decrypt(struct skcipher_request *req)
1414 if (req->cryptlen % DES_BLOCK_SIZE)
1417 if (req->cryptlen == 0)
1420 return stm32_cryp_crypt(req, FLG_TDES | FLG_CBC);
1423 static enum stm32_dma_mode stm32_cryp_dma_check_sg(struct scatterlist *test_sg, size_t len,
1426 struct scatterlist *sg;
1430 return NO_DMA; /* Faster */
1432 for_each_sg(test_sg, sg, sg_nents(test_sg), i) {
1433 if (!IS_ALIGNED(sg->length, block_size) && !sg_is_last(sg))
1436 if (sg->offset % sizeof(u32))
1439 if (sg_is_last(sg) && !IS_ALIGNED(sg->length, AES_BLOCK_SIZE))
1440 return DMA_NEED_SG_TRUNC;
1443 return DMA_PLAIN_SG;
1446 static enum stm32_dma_mode stm32_cryp_dma_check(struct stm32_cryp *cryp, struct scatterlist *in_sg,
1447 struct scatterlist *out_sg)
1449 enum stm32_dma_mode ret = DMA_PLAIN_SG;
1454 if (!cryp->dma_lch_in || !cryp->dma_lch_out)
1457 ret = stm32_cryp_dma_check_sg(in_sg, cryp->payload_in, AES_BLOCK_SIZE);
1461 ret = stm32_cryp_dma_check_sg(out_sg, cryp->payload_out, AES_BLOCK_SIZE);
1465 /* Check CTR counter overflow */
1466 if (is_aes(cryp) && is_ctr(cryp)) {
1470 memcpy(&iv3, &cryp->req->iv[3 * sizeof(u32)], sizeof(iv3));
1471 c = be32_to_cpu(iv3);
1472 if ((c + cryp->payload_in) < cryp->payload_in)
1477 if (is_aes(cryp) && is_ctr(cryp) && ret == DMA_NEED_SG_TRUNC)
1483 static int stm32_cryp_truncate_sg(struct scatterlist **new_sg, size_t *new_sg_len,
1484 struct scatterlist *sg, off_t skip, size_t size)
1486 struct scatterlist *cur;
1496 alloc_sg_len = sg_nents_for_len(sg, skip + size);
1497 if (alloc_sg_len < 0)
1498 return alloc_sg_len;
1500 /* We allocate to much sg entry, but it is easier */
1501 *new_sg = kmalloc_array((size_t)alloc_sg_len, sizeof(struct scatterlist), GFP_KERNEL);
1505 sg_init_table(*new_sg, (unsigned int)alloc_sg_len);
1508 while (sg && size) {
1509 unsigned int len = sg->length;
1510 unsigned int offset = sg->offset;
1530 sg_set_page(cur, sg_page(sg), len, offset);
1542 static int stm32_cryp_cipher_prepare(struct stm32_cryp *cryp, struct scatterlist *in_sg,
1543 struct scatterlist *out_sg)
1548 cryp->dma_mode = stm32_cryp_dma_check(cryp, in_sg, out_sg);
1550 scatterwalk_start(&cryp->in_walk, in_sg);
1551 scatterwalk_start(&cryp->out_walk, out_sg);
1553 if (cryp->dma_mode == NO_DMA) {
1554 cryp->flags &= ~FLG_IN_OUT_DMA;
1557 memset(cryp->last_ctr, 0, sizeof(cryp->last_ctr));
1559 } else if (cryp->dma_mode == DMA_NEED_SG_TRUNC) {
1561 cryp->flags |= FLG_IN_OUT_DMA;
1563 align_size = ALIGN_DOWN(cryp->payload_in, cryp->hw_blocksize);
1564 ret = stm32_cryp_truncate_sg(&cryp->in_sg, &cryp->in_sg_len, in_sg, 0, align_size);
1568 ret = stm32_cryp_truncate_sg(&cryp->out_sg, &cryp->out_sg_len, out_sg, 0,
1575 cryp->flags |= FLG_IN_OUT_DMA;
1577 cryp->in_sg = in_sg;
1578 cryp->out_sg = out_sg;
1580 ret = sg_nents_for_len(cryp->in_sg, cryp->payload_in);
1583 cryp->in_sg_len = (size_t)ret;
1585 ret = sg_nents_for_len(out_sg, cryp->payload_out);
1588 cryp->out_sg_len = (size_t)ret;
1594 static int stm32_cryp_aead_prepare(struct stm32_cryp *cryp, struct scatterlist *in_sg,
1595 struct scatterlist *out_sg)
1601 cryp->header_sg = NULL;
1603 cryp->out_sg = NULL;
1605 if (!cryp->dma_lch_in || !cryp->dma_lch_out) {
1606 cryp->dma_mode = NO_DMA;
1607 cryp->flags &= ~(FLG_IN_OUT_DMA | FLG_HEADER_DMA);
1612 /* CCM hw_init may have advanced in header */
1613 skip = cryp->areq->assoclen - cryp->header_in;
1615 align_size = ALIGN_DOWN(cryp->header_in, cryp->hw_blocksize);
1616 ret = stm32_cryp_truncate_sg(&cryp->header_sg, &cryp->header_sg_len, in_sg, skip,
1621 ret = stm32_cryp_dma_check_sg(cryp->header_sg, align_size, AES_BLOCK_SIZE);
1622 if (ret == NO_DMA) {
1623 /* We cannot DMA the header */
1624 kfree(cryp->header_sg);
1625 cryp->header_sg = NULL;
1627 cryp->flags &= ~FLG_HEADER_DMA;
1629 cryp->flags |= FLG_HEADER_DMA;
1632 /* Now skip all header to be at payload start */
1633 skip = cryp->areq->assoclen;
1634 align_size = ALIGN_DOWN(cryp->payload_in, cryp->hw_blocksize);
1635 ret = stm32_cryp_truncate_sg(&cryp->in_sg, &cryp->in_sg_len, in_sg, skip, align_size);
1637 kfree(cryp->header_sg);
1641 /* For out buffer align_size is same as in buffer */
1642 ret = stm32_cryp_truncate_sg(&cryp->out_sg, &cryp->out_sg_len, out_sg, skip, align_size);
1644 kfree(cryp->header_sg);
1649 ret = stm32_cryp_dma_check_sg(cryp->in_sg, align_size, AES_BLOCK_SIZE);
1650 ret2 = stm32_cryp_dma_check_sg(cryp->out_sg, align_size, AES_BLOCK_SIZE);
1651 if (ret == NO_DMA || ret2 == NO_DMA) {
1655 kfree(cryp->out_sg);
1656 cryp->out_sg = NULL;
1658 cryp->flags &= ~FLG_IN_OUT_DMA;
1660 cryp->flags |= FLG_IN_OUT_DMA;
1666 static int stm32_cryp_prepare_req(struct skcipher_request *req,
1667 struct aead_request *areq)
1669 struct stm32_cryp_ctx *ctx;
1670 struct stm32_cryp *cryp;
1671 struct stm32_cryp_reqctx *rctx;
1672 struct scatterlist *in_sg, *out_sg;
1678 ctx = req ? crypto_skcipher_ctx(crypto_skcipher_reqtfm(req)) :
1679 crypto_aead_ctx(crypto_aead_reqtfm(areq));
1683 rctx = req ? skcipher_request_ctx(req) : aead_request_ctx(areq);
1684 rctx->mode &= FLG_MODE_MASK;
1686 cryp->flags = (cryp->flags & ~FLG_MODE_MASK) | rctx->mode;
1687 cryp->hw_blocksize = is_aes(cryp) ? AES_BLOCK_SIZE : DES_BLOCK_SIZE;
1693 cryp->header_in = 0;
1694 cryp->payload_in = req->cryptlen;
1695 cryp->payload_out = req->cryptlen;
1701 ret = stm32_cryp_cipher_prepare(cryp, in_sg, out_sg);
1705 ret = stm32_cryp_hw_init(cryp);
1708 * Length of input and output data:
1710 * INPUT = AssocData || PlainText
1711 * <- assoclen -> <- cryptlen ->
1713 * OUTPUT = AssocData || CipherText || AuthTag
1714 * <- assoclen -> <-- cryptlen --> <- authsize ->
1717 * INPUT = AssocData || CipherTex || AuthTag
1718 * <- assoclen ---> <---------- cryptlen ---------->
1720 * OUTPUT = AssocData || PlainText
1721 * <- assoclen -> <- cryptlen - authsize ->
1725 cryp->authsize = crypto_aead_authsize(crypto_aead_reqtfm(areq));
1726 if (is_encrypt(cryp)) {
1727 cryp->payload_in = areq->cryptlen;
1728 cryp->header_in = areq->assoclen;
1729 cryp->payload_out = areq->cryptlen;
1731 cryp->payload_in = areq->cryptlen - cryp->authsize;
1732 cryp->header_in = areq->assoclen;
1733 cryp->payload_out = cryp->payload_in;
1739 scatterwalk_start(&cryp->in_walk, in_sg);
1740 scatterwalk_start(&cryp->out_walk, out_sg);
1741 /* In output, jump after assoc data */
1742 scatterwalk_copychunks(NULL, &cryp->out_walk, cryp->areq->assoclen, 2);
1744 ret = stm32_cryp_hw_init(cryp);
1748 ret = stm32_cryp_aead_prepare(cryp, in_sg, out_sg);
1754 static int stm32_cryp_cipher_one_req(struct crypto_engine *engine, void *areq)
1756 struct skcipher_request *req = container_of(areq,
1757 struct skcipher_request,
1759 struct stm32_cryp_ctx *ctx = crypto_skcipher_ctx(
1760 crypto_skcipher_reqtfm(req));
1761 struct stm32_cryp *cryp = ctx->cryp;
1767 ret = stm32_cryp_prepare_req(req, NULL);
1771 if (cryp->flags & FLG_IN_OUT_DMA)
1772 ret = stm32_cryp_dma_start(cryp);
1774 ret = stm32_cryp_it_start(cryp);
1776 if (ret == -ETIMEDOUT)
1777 stm32_cryp_finish_req(cryp, ret);
1782 static int stm32_cryp_aead_one_req(struct crypto_engine *engine, void *areq)
1784 struct aead_request *req = container_of(areq, struct aead_request,
1786 struct stm32_cryp_ctx *ctx = crypto_aead_ctx(crypto_aead_reqtfm(req));
1787 struct stm32_cryp *cryp = ctx->cryp;
1793 err = stm32_cryp_prepare_req(NULL, req);
1797 if (!stm32_cryp_get_input_text_len(cryp) && !cryp->header_in &&
1798 !(cryp->flags & FLG_HEADER_DMA)) {
1799 /* No input data to process: get tag and finish */
1800 stm32_cryp_finish_req(cryp, 0);
1804 if (cryp->flags & FLG_HEADER_DMA)
1805 return stm32_cryp_header_dma_start(cryp);
1807 if (!cryp->header_in && cryp->flags & FLG_IN_OUT_DMA)
1808 return stm32_cryp_dma_start(cryp);
1810 return stm32_cryp_it_start(cryp);
1813 static int stm32_cryp_read_auth_tag(struct stm32_cryp *cryp)
1820 cfg = stm32_cryp_read(cryp, cryp->caps->cr);
1824 cfg &= ~CR_DEC_NOT_ENC;
1827 stm32_cryp_write(cryp, cryp->caps->cr, cfg);
1830 /* GCM: write aad and payload size (in bits) */
1831 size_bit = cryp->areq->assoclen * 8;
1832 if (cryp->caps->swap_final)
1833 size_bit = (__force u32)cpu_to_be32(size_bit);
1835 stm32_cryp_write(cryp, cryp->caps->din, 0);
1836 stm32_cryp_write(cryp, cryp->caps->din, size_bit);
1838 size_bit = is_encrypt(cryp) ? cryp->areq->cryptlen :
1839 cryp->areq->cryptlen - cryp->authsize;
1841 if (cryp->caps->swap_final)
1842 size_bit = (__force u32)cpu_to_be32(size_bit);
1844 stm32_cryp_write(cryp, cryp->caps->din, 0);
1845 stm32_cryp_write(cryp, cryp->caps->din, size_bit);
1847 /* CCM: write CTR0 */
1848 u32 iv32[AES_BLOCK_32];
1849 u8 *iv = (u8 *)iv32;
1850 __be32 *biv = (__be32 *)iv32;
1852 memcpy(iv, cryp->areq->iv, AES_BLOCK_SIZE);
1853 memset(iv + AES_BLOCK_SIZE - 1 - iv[0], 0, iv[0] + 1);
1855 for (i = 0; i < AES_BLOCK_32; i++) {
1858 if (!cryp->caps->padding_wa)
1859 xiv = be32_to_cpu(biv[i]);
1860 stm32_cryp_write(cryp, cryp->caps->din, xiv);
1864 /* Wait for output data */
1865 ret = stm32_cryp_wait_output(cryp);
1867 dev_err(cryp->dev, "Timeout (read tag)\n");
1871 if (is_encrypt(cryp)) {
1872 u32 out_tag[AES_BLOCK_32];
1874 /* Get and write tag */
1875 readsl(cryp->regs + cryp->caps->dout, out_tag, AES_BLOCK_32);
1876 scatterwalk_copychunks(out_tag, &cryp->out_walk, cryp->authsize, 1);
1878 /* Get and check tag */
1879 u32 in_tag[AES_BLOCK_32], out_tag[AES_BLOCK_32];
1881 scatterwalk_copychunks(in_tag, &cryp->in_walk, cryp->authsize, 0);
1882 readsl(cryp->regs + cryp->caps->dout, out_tag, AES_BLOCK_32);
1884 if (crypto_memneq(in_tag, out_tag, cryp->authsize))
1890 stm32_cryp_write(cryp, cryp->caps->cr, cfg);
1895 static void stm32_cryp_check_ctr_counter(struct stm32_cryp *cryp)
1899 if (unlikely(cryp->last_ctr[3] == cpu_to_be32(0xFFFFFFFF))) {
1901 * In this case, we need to increment manually the ctr counter,
1902 * as HW doesn't handle the U32 carry.
1904 crypto_inc((u8 *)cryp->last_ctr, sizeof(cryp->last_ctr));
1906 cr = stm32_cryp_read(cryp, cryp->caps->cr);
1907 stm32_cryp_write(cryp, cryp->caps->cr, cr & ~CR_CRYPEN);
1909 stm32_cryp_hw_write_iv(cryp, cryp->last_ctr);
1911 stm32_cryp_write(cryp, cryp->caps->cr, cr);
1914 /* The IV registers are BE */
1915 cryp->last_ctr[0] = cpu_to_be32(stm32_cryp_read(cryp, cryp->caps->iv0l));
1916 cryp->last_ctr[1] = cpu_to_be32(stm32_cryp_read(cryp, cryp->caps->iv0r));
1917 cryp->last_ctr[2] = cpu_to_be32(stm32_cryp_read(cryp, cryp->caps->iv1l));
1918 cryp->last_ctr[3] = cpu_to_be32(stm32_cryp_read(cryp, cryp->caps->iv1r));
1921 static void stm32_cryp_irq_read_data(struct stm32_cryp *cryp)
1923 u32 block[AES_BLOCK_32];
1925 readsl(cryp->regs + cryp->caps->dout, block, cryp->hw_blocksize / sizeof(u32));
1926 scatterwalk_copychunks(block, &cryp->out_walk, min_t(size_t, cryp->hw_blocksize,
1927 cryp->payload_out), 1);
1928 cryp->payload_out -= min_t(size_t, cryp->hw_blocksize,
1932 static void stm32_cryp_irq_write_block(struct stm32_cryp *cryp)
1934 u32 block[AES_BLOCK_32] = {0};
1936 scatterwalk_copychunks(block, &cryp->in_walk, min_t(size_t, cryp->hw_blocksize,
1937 cryp->payload_in), 0);
1938 writesl(cryp->regs + cryp->caps->din, block, cryp->hw_blocksize / sizeof(u32));
1939 cryp->payload_in -= min_t(size_t, cryp->hw_blocksize, cryp->payload_in);
1942 static void stm32_cryp_irq_write_gcm_padded_data(struct stm32_cryp *cryp)
1945 u32 cfg, block[AES_BLOCK_32] = {0};
1948 /* 'Special workaround' procedure described in the datasheet */
1951 stm32_cryp_write(cryp, cryp->caps->imsc, 0);
1952 cfg = stm32_cryp_read(cryp, cryp->caps->cr);
1954 stm32_cryp_write(cryp, cryp->caps->cr, cfg);
1956 /* b) Update IV1R */
1957 stm32_cryp_write(cryp, cryp->caps->iv1r, cryp->gcm_ctr - 2);
1959 /* c) change mode to CTR */
1960 cfg &= ~CR_ALGO_MASK;
1962 stm32_cryp_write(cryp, cryp->caps->cr, cfg);
1966 stm32_cryp_write(cryp, cryp->caps->cr, cfg);
1968 /* b) pad and write the last block */
1969 stm32_cryp_irq_write_block(cryp);
1970 /* wait end of process */
1971 err = stm32_cryp_wait_output(cryp);
1973 dev_err(cryp->dev, "Timeout (write gcm last data)\n");
1974 return stm32_cryp_finish_req(cryp, err);
1977 /* c) get and store encrypted data */
1979 * Same code as stm32_cryp_irq_read_data(), but we want to store
1982 readsl(cryp->regs + cryp->caps->dout, block, cryp->hw_blocksize / sizeof(u32));
1984 scatterwalk_copychunks(block, &cryp->out_walk, min_t(size_t, cryp->hw_blocksize,
1985 cryp->payload_out), 1);
1986 cryp->payload_out -= min_t(size_t, cryp->hw_blocksize,
1989 /* d) change mode back to AES GCM */
1990 cfg &= ~CR_ALGO_MASK;
1992 stm32_cryp_write(cryp, cryp->caps->cr, cfg);
1994 /* e) change phase to Final */
1997 stm32_cryp_write(cryp, cryp->caps->cr, cfg);
1999 /* f) write padded data */
2000 writesl(cryp->regs + cryp->caps->din, block, AES_BLOCK_32);
2002 /* g) Empty fifo out */
2003 err = stm32_cryp_wait_output(cryp);
2005 dev_err(cryp->dev, "Timeout (write gcm padded data)\n");
2006 return stm32_cryp_finish_req(cryp, err);
2009 for (i = 0; i < AES_BLOCK_32; i++)
2010 stm32_cryp_read(cryp, cryp->caps->dout);
2012 /* h) run the he normal Final phase */
2013 stm32_cryp_finish_req(cryp, 0);
2016 static void stm32_cryp_irq_set_npblb(struct stm32_cryp *cryp)
2020 /* disable ip, set NPBLB and reneable ip */
2021 cfg = stm32_cryp_read(cryp, cryp->caps->cr);
2023 stm32_cryp_write(cryp, cryp->caps->cr, cfg);
2025 cfg |= (cryp->hw_blocksize - cryp->payload_in) << CR_NBPBL_SHIFT;
2027 stm32_cryp_write(cryp, cryp->caps->cr, cfg);
2030 static void stm32_cryp_irq_write_ccm_padded_data(struct stm32_cryp *cryp)
2034 u32 cstmp1[AES_BLOCK_32], cstmp2[AES_BLOCK_32];
2035 u32 block[AES_BLOCK_32] = {0};
2038 /* 'Special workaround' procedure described in the datasheet */
2041 stm32_cryp_write(cryp, cryp->caps->imsc, 0);
2043 cfg = stm32_cryp_read(cryp, cryp->caps->cr);
2045 stm32_cryp_write(cryp, cryp->caps->cr, cfg);
2047 /* b) get IV1 from CRYP_CSGCMCCM7 */
2048 iv1tmp = stm32_cryp_read(cryp, CRYP_CSGCMCCM0R + 7 * 4);
2050 /* c) Load CRYP_CSGCMCCMxR */
2051 for (i = 0; i < ARRAY_SIZE(cstmp1); i++)
2052 cstmp1[i] = stm32_cryp_read(cryp, CRYP_CSGCMCCM0R + i * 4);
2055 stm32_cryp_write(cryp, cryp->caps->iv1r, iv1tmp);
2057 /* e) change mode to CTR */
2058 cfg &= ~CR_ALGO_MASK;
2060 stm32_cryp_write(cryp, cryp->caps->cr, cfg);
2064 stm32_cryp_write(cryp, cryp->caps->cr, cfg);
2066 /* b) pad and write the last block */
2067 stm32_cryp_irq_write_block(cryp);
2068 /* wait end of process */
2069 err = stm32_cryp_wait_output(cryp);
2071 dev_err(cryp->dev, "Timeout (write ccm padded data)\n");
2072 return stm32_cryp_finish_req(cryp, err);
2075 /* c) get and store decrypted data */
2077 * Same code as stm32_cryp_irq_read_data(), but we want to store
2080 readsl(cryp->regs + cryp->caps->dout, block, cryp->hw_blocksize / sizeof(u32));
2082 scatterwalk_copychunks(block, &cryp->out_walk, min_t(size_t, cryp->hw_blocksize,
2083 cryp->payload_out), 1);
2084 cryp->payload_out -= min_t(size_t, cryp->hw_blocksize, cryp->payload_out);
2086 /* d) Load again CRYP_CSGCMCCMxR */
2087 for (i = 0; i < ARRAY_SIZE(cstmp2); i++)
2088 cstmp2[i] = stm32_cryp_read(cryp, CRYP_CSGCMCCM0R + i * 4);
2090 /* e) change mode back to AES CCM */
2091 cfg &= ~CR_ALGO_MASK;
2093 stm32_cryp_write(cryp, cryp->caps->cr, cfg);
2095 /* f) change phase to header */
2097 cfg |= CR_PH_HEADER;
2098 stm32_cryp_write(cryp, cryp->caps->cr, cfg);
2100 /* g) XOR and write padded data */
2101 for (i = 0; i < ARRAY_SIZE(block); i++) {
2102 block[i] ^= cstmp1[i];
2103 block[i] ^= cstmp2[i];
2104 stm32_cryp_write(cryp, cryp->caps->din, block[i]);
2107 /* h) wait for completion */
2108 err = stm32_cryp_wait_busy(cryp);
2110 dev_err(cryp->dev, "Timeout (write ccm padded data)\n");
2112 /* i) run the he normal Final phase */
2113 stm32_cryp_finish_req(cryp, err);
2116 static void stm32_cryp_irq_write_data(struct stm32_cryp *cryp)
2118 if (unlikely(!cryp->payload_in)) {
2119 dev_warn(cryp->dev, "No more data to process\n");
2123 if (unlikely(cryp->payload_in < AES_BLOCK_SIZE &&
2124 (stm32_cryp_get_hw_mode(cryp) == CR_AES_GCM) &&
2125 is_encrypt(cryp))) {
2126 /* Padding for AES GCM encryption */
2127 if (cryp->caps->padding_wa) {
2128 /* Special case 1 */
2129 stm32_cryp_irq_write_gcm_padded_data(cryp);
2133 /* Setting padding bytes (NBBLB) */
2134 stm32_cryp_irq_set_npblb(cryp);
2137 if (unlikely((cryp->payload_in < AES_BLOCK_SIZE) &&
2138 (stm32_cryp_get_hw_mode(cryp) == CR_AES_CCM) &&
2139 is_decrypt(cryp))) {
2140 /* Padding for AES CCM decryption */
2141 if (cryp->caps->padding_wa) {
2142 /* Special case 2 */
2143 stm32_cryp_irq_write_ccm_padded_data(cryp);
2147 /* Setting padding bytes (NBBLB) */
2148 stm32_cryp_irq_set_npblb(cryp);
2151 if (is_aes(cryp) && is_ctr(cryp))
2152 stm32_cryp_check_ctr_counter(cryp);
2154 stm32_cryp_irq_write_block(cryp);
2157 static void stm32_cryp_irq_write_gcmccm_header(struct stm32_cryp *cryp)
2159 u32 block[AES_BLOCK_32] = {0};
2162 written = min_t(size_t, AES_BLOCK_SIZE, cryp->header_in);
2164 scatterwalk_copychunks(block, &cryp->in_walk, written, 0);
2166 writesl(cryp->regs + cryp->caps->din, block, AES_BLOCK_32);
2168 cryp->header_in -= written;
2170 stm32_crypt_gcmccm_end_header(cryp);
2173 static irqreturn_t stm32_cryp_irq_thread(int irq, void *arg)
2175 struct stm32_cryp *cryp = arg;
2177 u32 it_mask = stm32_cryp_read(cryp, cryp->caps->imsc);
2179 if (cryp->irq_status & MISR_OUT)
2180 /* Output FIFO IRQ: read data */
2181 stm32_cryp_irq_read_data(cryp);
2183 if (cryp->irq_status & MISR_IN) {
2184 if (is_gcm(cryp) || is_ccm(cryp)) {
2185 ph = stm32_cryp_read(cryp, cryp->caps->cr) & CR_PH_MASK;
2186 if (unlikely(ph == CR_PH_HEADER))
2188 stm32_cryp_irq_write_gcmccm_header(cryp);
2190 /* Input FIFO IRQ: write data */
2191 stm32_cryp_irq_write_data(cryp);
2195 /* Input FIFO IRQ: write data */
2196 stm32_cryp_irq_write_data(cryp);
2200 /* Mask useless interrupts */
2201 if (!cryp->payload_in && !cryp->header_in)
2202 it_mask &= ~IMSCR_IN;
2203 if (!cryp->payload_out)
2204 it_mask &= ~IMSCR_OUT;
2205 stm32_cryp_write(cryp, cryp->caps->imsc, it_mask);
2207 if (!cryp->payload_in && !cryp->header_in && !cryp->payload_out) {
2209 stm32_cryp_finish_req(cryp, 0);
2216 static irqreturn_t stm32_cryp_irq(int irq, void *arg)
2218 struct stm32_cryp *cryp = arg;
2220 cryp->irq_status = stm32_cryp_read(cryp, cryp->caps->mis);
2222 return IRQ_WAKE_THREAD;
2225 static int stm32_cryp_dma_init(struct stm32_cryp *cryp)
2227 struct dma_slave_config dma_conf;
2228 struct dma_chan *chan;
2231 memset(&dma_conf, 0, sizeof(dma_conf));
2233 dma_conf.direction = DMA_MEM_TO_DEV;
2234 dma_conf.dst_addr = cryp->phys_base + cryp->caps->din;
2235 dma_conf.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
2236 dma_conf.dst_maxburst = CRYP_DMA_BURST_REG;
2237 dma_conf.device_fc = false;
2239 chan = dma_request_chan(cryp->dev, "in");
2241 return PTR_ERR(chan);
2243 cryp->dma_lch_in = chan;
2244 ret = dmaengine_slave_config(cryp->dma_lch_in, &dma_conf);
2246 dma_release_channel(cryp->dma_lch_in);
2247 cryp->dma_lch_in = NULL;
2248 dev_err(cryp->dev, "Couldn't configure DMA in slave.\n");
2252 memset(&dma_conf, 0, sizeof(dma_conf));
2254 dma_conf.direction = DMA_DEV_TO_MEM;
2255 dma_conf.src_addr = cryp->phys_base + cryp->caps->dout;
2256 dma_conf.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
2257 dma_conf.src_maxburst = CRYP_DMA_BURST_REG;
2258 dma_conf.device_fc = false;
2260 chan = dma_request_chan(cryp->dev, "out");
2262 dma_release_channel(cryp->dma_lch_in);
2263 cryp->dma_lch_in = NULL;
2264 return PTR_ERR(chan);
2267 cryp->dma_lch_out = chan;
2269 ret = dmaengine_slave_config(cryp->dma_lch_out, &dma_conf);
2271 dma_release_channel(cryp->dma_lch_out);
2272 cryp->dma_lch_out = NULL;
2273 dev_err(cryp->dev, "Couldn't configure DMA out slave.\n");
2274 dma_release_channel(cryp->dma_lch_in);
2275 cryp->dma_lch_in = NULL;
2279 init_completion(&cryp->dma_completion);
2284 static struct skcipher_engine_alg crypto_algs[] = {
2287 .base.cra_name = "ecb(aes)",
2288 .base.cra_driver_name = "stm32-ecb-aes",
2289 .base.cra_priority = 300,
2290 .base.cra_flags = CRYPTO_ALG_ASYNC | CRYPTO_ALG_KERN_DRIVER_ONLY,
2291 .base.cra_blocksize = AES_BLOCK_SIZE,
2292 .base.cra_ctxsize = sizeof(struct stm32_cryp_ctx),
2293 .base.cra_alignmask = 0,
2294 .base.cra_module = THIS_MODULE,
2296 .init = stm32_cryp_init_tfm,
2297 .min_keysize = AES_MIN_KEY_SIZE,
2298 .max_keysize = AES_MAX_KEY_SIZE,
2299 .setkey = stm32_cryp_aes_setkey,
2300 .encrypt = stm32_cryp_aes_ecb_encrypt,
2301 .decrypt = stm32_cryp_aes_ecb_decrypt,
2304 .do_one_request = stm32_cryp_cipher_one_req,
2309 .base.cra_name = "cbc(aes)",
2310 .base.cra_driver_name = "stm32-cbc-aes",
2311 .base.cra_priority = 300,
2312 .base.cra_flags = CRYPTO_ALG_ASYNC | CRYPTO_ALG_KERN_DRIVER_ONLY,
2313 .base.cra_blocksize = AES_BLOCK_SIZE,
2314 .base.cra_ctxsize = sizeof(struct stm32_cryp_ctx),
2315 .base.cra_alignmask = 0,
2316 .base.cra_module = THIS_MODULE,
2318 .init = stm32_cryp_init_tfm,
2319 .min_keysize = AES_MIN_KEY_SIZE,
2320 .max_keysize = AES_MAX_KEY_SIZE,
2321 .ivsize = AES_BLOCK_SIZE,
2322 .setkey = stm32_cryp_aes_setkey,
2323 .encrypt = stm32_cryp_aes_cbc_encrypt,
2324 .decrypt = stm32_cryp_aes_cbc_decrypt,
2327 .do_one_request = stm32_cryp_cipher_one_req,
2332 .base.cra_name = "ctr(aes)",
2333 .base.cra_driver_name = "stm32-ctr-aes",
2334 .base.cra_priority = 300,
2335 .base.cra_flags = CRYPTO_ALG_ASYNC | CRYPTO_ALG_KERN_DRIVER_ONLY,
2336 .base.cra_blocksize = 1,
2337 .base.cra_ctxsize = sizeof(struct stm32_cryp_ctx),
2338 .base.cra_alignmask = 0,
2339 .base.cra_module = THIS_MODULE,
2341 .init = stm32_cryp_init_tfm,
2342 .min_keysize = AES_MIN_KEY_SIZE,
2343 .max_keysize = AES_MAX_KEY_SIZE,
2344 .ivsize = AES_BLOCK_SIZE,
2345 .setkey = stm32_cryp_aes_setkey,
2346 .encrypt = stm32_cryp_aes_ctr_encrypt,
2347 .decrypt = stm32_cryp_aes_ctr_decrypt,
2350 .do_one_request = stm32_cryp_cipher_one_req,
2355 .base.cra_name = "ecb(des)",
2356 .base.cra_driver_name = "stm32-ecb-des",
2357 .base.cra_priority = 300,
2358 .base.cra_flags = CRYPTO_ALG_ASYNC | CRYPTO_ALG_KERN_DRIVER_ONLY,
2359 .base.cra_blocksize = DES_BLOCK_SIZE,
2360 .base.cra_ctxsize = sizeof(struct stm32_cryp_ctx),
2361 .base.cra_alignmask = 0,
2362 .base.cra_module = THIS_MODULE,
2364 .init = stm32_cryp_init_tfm,
2365 .min_keysize = DES_BLOCK_SIZE,
2366 .max_keysize = DES_BLOCK_SIZE,
2367 .setkey = stm32_cryp_des_setkey,
2368 .encrypt = stm32_cryp_des_ecb_encrypt,
2369 .decrypt = stm32_cryp_des_ecb_decrypt,
2372 .do_one_request = stm32_cryp_cipher_one_req,
2377 .base.cra_name = "cbc(des)",
2378 .base.cra_driver_name = "stm32-cbc-des",
2379 .base.cra_priority = 300,
2380 .base.cra_flags = CRYPTO_ALG_ASYNC | CRYPTO_ALG_KERN_DRIVER_ONLY,
2381 .base.cra_blocksize = DES_BLOCK_SIZE,
2382 .base.cra_ctxsize = sizeof(struct stm32_cryp_ctx),
2383 .base.cra_alignmask = 0,
2384 .base.cra_module = THIS_MODULE,
2386 .init = stm32_cryp_init_tfm,
2387 .min_keysize = DES_BLOCK_SIZE,
2388 .max_keysize = DES_BLOCK_SIZE,
2389 .ivsize = DES_BLOCK_SIZE,
2390 .setkey = stm32_cryp_des_setkey,
2391 .encrypt = stm32_cryp_des_cbc_encrypt,
2392 .decrypt = stm32_cryp_des_cbc_decrypt,
2395 .do_one_request = stm32_cryp_cipher_one_req,
2400 .base.cra_name = "ecb(des3_ede)",
2401 .base.cra_driver_name = "stm32-ecb-des3",
2402 .base.cra_priority = 300,
2403 .base.cra_flags = CRYPTO_ALG_ASYNC | CRYPTO_ALG_KERN_DRIVER_ONLY,
2404 .base.cra_blocksize = DES_BLOCK_SIZE,
2405 .base.cra_ctxsize = sizeof(struct stm32_cryp_ctx),
2406 .base.cra_alignmask = 0,
2407 .base.cra_module = THIS_MODULE,
2409 .init = stm32_cryp_init_tfm,
2410 .min_keysize = 3 * DES_BLOCK_SIZE,
2411 .max_keysize = 3 * DES_BLOCK_SIZE,
2412 .setkey = stm32_cryp_tdes_setkey,
2413 .encrypt = stm32_cryp_tdes_ecb_encrypt,
2414 .decrypt = stm32_cryp_tdes_ecb_decrypt,
2417 .do_one_request = stm32_cryp_cipher_one_req,
2422 .base.cra_name = "cbc(des3_ede)",
2423 .base.cra_driver_name = "stm32-cbc-des3",
2424 .base.cra_priority = 300,
2425 .base.cra_flags = CRYPTO_ALG_ASYNC | CRYPTO_ALG_KERN_DRIVER_ONLY,
2426 .base.cra_blocksize = DES_BLOCK_SIZE,
2427 .base.cra_ctxsize = sizeof(struct stm32_cryp_ctx),
2428 .base.cra_alignmask = 0,
2429 .base.cra_module = THIS_MODULE,
2431 .init = stm32_cryp_init_tfm,
2432 .min_keysize = 3 * DES_BLOCK_SIZE,
2433 .max_keysize = 3 * DES_BLOCK_SIZE,
2434 .ivsize = DES_BLOCK_SIZE,
2435 .setkey = stm32_cryp_tdes_setkey,
2436 .encrypt = stm32_cryp_tdes_cbc_encrypt,
2437 .decrypt = stm32_cryp_tdes_cbc_decrypt,
2440 .do_one_request = stm32_cryp_cipher_one_req,
2445 static struct aead_engine_alg aead_algs[] = {
2447 .base.setkey = stm32_cryp_aes_aead_setkey,
2448 .base.setauthsize = stm32_cryp_aes_gcm_setauthsize,
2449 .base.encrypt = stm32_cryp_aes_gcm_encrypt,
2450 .base.decrypt = stm32_cryp_aes_gcm_decrypt,
2451 .base.init = stm32_cryp_aes_aead_init,
2453 .base.maxauthsize = AES_BLOCK_SIZE,
2456 .cra_name = "gcm(aes)",
2457 .cra_driver_name = "stm32-gcm-aes",
2458 .cra_priority = 300,
2459 .cra_flags = CRYPTO_ALG_ASYNC | CRYPTO_ALG_KERN_DRIVER_ONLY,
2461 .cra_ctxsize = sizeof(struct stm32_cryp_ctx),
2463 .cra_module = THIS_MODULE,
2466 .do_one_request = stm32_cryp_aead_one_req,
2470 .base.setkey = stm32_cryp_aes_aead_setkey,
2471 .base.setauthsize = stm32_cryp_aes_ccm_setauthsize,
2472 .base.encrypt = stm32_cryp_aes_ccm_encrypt,
2473 .base.decrypt = stm32_cryp_aes_ccm_decrypt,
2474 .base.init = stm32_cryp_aes_aead_init,
2475 .base.ivsize = AES_BLOCK_SIZE,
2476 .base.maxauthsize = AES_BLOCK_SIZE,
2479 .cra_name = "ccm(aes)",
2480 .cra_driver_name = "stm32-ccm-aes",
2481 .cra_priority = 300,
2482 .cra_flags = CRYPTO_ALG_ASYNC | CRYPTO_ALG_KERN_DRIVER_ONLY,
2484 .cra_ctxsize = sizeof(struct stm32_cryp_ctx),
2486 .cra_module = THIS_MODULE,
2489 .do_one_request = stm32_cryp_aead_one_req,
2494 static const struct stm32_cryp_caps ux500_data = {
2495 .aeads_support = false,
2496 .linear_aes_key = true,
2498 .iv_protection = true,
2501 .cr = UX500_CRYP_CR,
2502 .sr = UX500_CRYP_SR,
2503 .din = UX500_CRYP_DIN,
2504 .dout = UX500_CRYP_DOUT,
2505 .dmacr = UX500_CRYP_DMACR,
2506 .imsc = UX500_CRYP_IMSC,
2507 .mis = UX500_CRYP_MIS,
2508 .k1l = UX500_CRYP_K1L,
2509 .k1r = UX500_CRYP_K1R,
2510 .k3r = UX500_CRYP_K3R,
2511 .iv0l = UX500_CRYP_IV0L,
2512 .iv0r = UX500_CRYP_IV0R,
2513 .iv1l = UX500_CRYP_IV1L,
2514 .iv1r = UX500_CRYP_IV1R,
2517 static const struct stm32_cryp_caps f7_data = {
2518 .aeads_support = true,
2519 .linear_aes_key = false,
2521 .iv_protection = false,
2528 .dmacr = CRYP_DMACR,
2540 static const struct stm32_cryp_caps mp1_data = {
2541 .aeads_support = true,
2542 .linear_aes_key = false,
2544 .iv_protection = false,
2545 .swap_final = false,
2546 .padding_wa = false,
2551 .dmacr = CRYP_DMACR,
2563 static const struct of_device_id stm32_dt_ids[] = {
2564 { .compatible = "stericsson,ux500-cryp", .data = &ux500_data},
2565 { .compatible = "st,stm32f756-cryp", .data = &f7_data},
2566 { .compatible = "st,stm32mp1-cryp", .data = &mp1_data},
2569 MODULE_DEVICE_TABLE(of, stm32_dt_ids);
2571 static int stm32_cryp_probe(struct platform_device *pdev)
2573 struct device *dev = &pdev->dev;
2574 struct stm32_cryp *cryp;
2575 struct reset_control *rst;
2578 cryp = devm_kzalloc(dev, sizeof(*cryp), GFP_KERNEL);
2582 cryp->caps = of_device_get_match_data(dev);
2588 cryp->regs = devm_platform_ioremap_resource(pdev, 0);
2589 if (IS_ERR(cryp->regs))
2590 return PTR_ERR(cryp->regs);
2592 cryp->phys_base = platform_get_resource(pdev, IORESOURCE_MEM, 0)->start;
2594 irq = platform_get_irq(pdev, 0);
2598 ret = devm_request_threaded_irq(dev, irq, stm32_cryp_irq,
2599 stm32_cryp_irq_thread, IRQF_ONESHOT,
2600 dev_name(dev), cryp);
2602 dev_err(dev, "Cannot grab IRQ\n");
2606 cryp->clk = devm_clk_get(dev, NULL);
2607 if (IS_ERR(cryp->clk)) {
2608 dev_err_probe(dev, PTR_ERR(cryp->clk), "Could not get clock\n");
2610 return PTR_ERR(cryp->clk);
2613 ret = clk_prepare_enable(cryp->clk);
2615 dev_err(cryp->dev, "Failed to enable clock\n");
2619 pm_runtime_set_autosuspend_delay(dev, CRYP_AUTOSUSPEND_DELAY);
2620 pm_runtime_use_autosuspend(dev);
2622 pm_runtime_get_noresume(dev);
2623 pm_runtime_set_active(dev);
2624 pm_runtime_enable(dev);
2626 rst = devm_reset_control_get(dev, NULL);
2629 if (ret == -EPROBE_DEFER)
2632 reset_control_assert(rst);
2634 reset_control_deassert(rst);
2637 platform_set_drvdata(pdev, cryp);
2639 ret = stm32_cryp_dma_init(cryp);
2644 dev_dbg(dev, "DMA mode not available\n");
2650 spin_lock(&cryp_list.lock);
2651 list_add(&cryp->list, &cryp_list.dev_list);
2652 spin_unlock(&cryp_list.lock);
2654 /* Initialize crypto engine */
2655 cryp->engine = crypto_engine_alloc_init(dev, 1);
2656 if (!cryp->engine) {
2657 dev_err(dev, "Could not init crypto engine\n");
2662 ret = crypto_engine_start(cryp->engine);
2664 dev_err(dev, "Could not start crypto engine\n");
2668 ret = crypto_engine_register_skciphers(crypto_algs, ARRAY_SIZE(crypto_algs));
2670 dev_err(dev, "Could not register algs\n");
2674 if (cryp->caps->aeads_support) {
2675 ret = crypto_engine_register_aeads(aead_algs, ARRAY_SIZE(aead_algs));
2680 dev_info(dev, "Initialized\n");
2682 pm_runtime_put_sync(dev);
2687 crypto_engine_unregister_skciphers(crypto_algs, ARRAY_SIZE(crypto_algs));
2690 crypto_engine_exit(cryp->engine);
2692 spin_lock(&cryp_list.lock);
2693 list_del(&cryp->list);
2694 spin_unlock(&cryp_list.lock);
2696 if (cryp->dma_lch_in)
2697 dma_release_channel(cryp->dma_lch_in);
2698 if (cryp->dma_lch_out)
2699 dma_release_channel(cryp->dma_lch_out);
2702 pm_runtime_disable(dev);
2703 pm_runtime_put_noidle(dev);
2705 clk_disable_unprepare(cryp->clk);
2710 static void stm32_cryp_remove(struct platform_device *pdev)
2712 struct stm32_cryp *cryp = platform_get_drvdata(pdev);
2715 ret = pm_runtime_get_sync(cryp->dev);
2717 if (cryp->caps->aeads_support)
2718 crypto_engine_unregister_aeads(aead_algs, ARRAY_SIZE(aead_algs));
2719 crypto_engine_unregister_skciphers(crypto_algs, ARRAY_SIZE(crypto_algs));
2721 crypto_engine_exit(cryp->engine);
2723 spin_lock(&cryp_list.lock);
2724 list_del(&cryp->list);
2725 spin_unlock(&cryp_list.lock);
2727 if (cryp->dma_lch_in)
2728 dma_release_channel(cryp->dma_lch_in);
2730 if (cryp->dma_lch_out)
2731 dma_release_channel(cryp->dma_lch_out);
2733 pm_runtime_disable(cryp->dev);
2734 pm_runtime_put_noidle(cryp->dev);
2737 clk_disable_unprepare(cryp->clk);
2741 static int stm32_cryp_runtime_suspend(struct device *dev)
2743 struct stm32_cryp *cryp = dev_get_drvdata(dev);
2745 clk_disable_unprepare(cryp->clk);
2750 static int stm32_cryp_runtime_resume(struct device *dev)
2752 struct stm32_cryp *cryp = dev_get_drvdata(dev);
2755 ret = clk_prepare_enable(cryp->clk);
2757 dev_err(cryp->dev, "Failed to prepare_enable clock\n");
2765 static const struct dev_pm_ops stm32_cryp_pm_ops = {
2766 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
2767 pm_runtime_force_resume)
2768 SET_RUNTIME_PM_OPS(stm32_cryp_runtime_suspend,
2769 stm32_cryp_runtime_resume, NULL)
2772 static struct platform_driver stm32_cryp_driver = {
2773 .probe = stm32_cryp_probe,
2774 .remove = stm32_cryp_remove,
2776 .name = DRIVER_NAME,
2777 .pm = &stm32_cryp_pm_ops,
2778 .of_match_table = stm32_dt_ids,
2782 module_platform_driver(stm32_cryp_driver);
2785 MODULE_DESCRIPTION("STMicrolectronics STM32 CRYP hardware driver");
2786 MODULE_LICENSE("GPL");