16 #include <linux/module.h>
18 #include <linux/errno.h>
19 #include <linux/kernel.h>
29 #include <crypto/aes.h>
35 #define _SBF(s, v) ((v) << (s))
36 #define _BIT(b) _SBF(b, 1)
39 #define SSS_REG_FCINTSTAT 0x0000
40 #define SSS_FCINTSTAT_BRDMAINT _BIT(3)
41 #define SSS_FCINTSTAT_BTDMAINT _BIT(2)
42 #define SSS_FCINTSTAT_HRDMAINT _BIT(1)
43 #define SSS_FCINTSTAT_PKDMAINT _BIT(0)
45 #define SSS_REG_FCINTENSET 0x0004
46 #define SSS_FCINTENSET_BRDMAINTENSET _BIT(3)
47 #define SSS_FCINTENSET_BTDMAINTENSET _BIT(2)
48 #define SSS_FCINTENSET_HRDMAINTENSET _BIT(1)
49 #define SSS_FCINTENSET_PKDMAINTENSET _BIT(0)
51 #define SSS_REG_FCINTENCLR 0x0008
52 #define SSS_FCINTENCLR_BRDMAINTENCLR _BIT(3)
53 #define SSS_FCINTENCLR_BTDMAINTENCLR _BIT(2)
54 #define SSS_FCINTENCLR_HRDMAINTENCLR _BIT(1)
55 #define SSS_FCINTENCLR_PKDMAINTENCLR _BIT(0)
57 #define SSS_REG_FCINTPEND 0x000C
58 #define SSS_FCINTPEND_BRDMAINTP _BIT(3)
59 #define SSS_FCINTPEND_BTDMAINTP _BIT(2)
60 #define SSS_FCINTPEND_HRDMAINTP _BIT(1)
61 #define SSS_FCINTPEND_PKDMAINTP _BIT(0)
63 #define SSS_REG_FCFIFOSTAT 0x0010
64 #define SSS_FCFIFOSTAT_BRFIFOFUL _BIT(7)
65 #define SSS_FCFIFOSTAT_BRFIFOEMP _BIT(6)
66 #define SSS_FCFIFOSTAT_BTFIFOFUL _BIT(5)
67 #define SSS_FCFIFOSTAT_BTFIFOEMP _BIT(4)
68 #define SSS_FCFIFOSTAT_HRFIFOFUL _BIT(3)
69 #define SSS_FCFIFOSTAT_HRFIFOEMP _BIT(2)
70 #define SSS_FCFIFOSTAT_PKFIFOFUL _BIT(1)
71 #define SSS_FCFIFOSTAT_PKFIFOEMP _BIT(0)
73 #define SSS_REG_FCFIFOCTRL 0x0014
74 #define SSS_FCFIFOCTRL_DESSEL _BIT(2)
75 #define SSS_HASHIN_INDEPENDENT _SBF(0, 0x00)
76 #define SSS_HASHIN_CIPHER_INPUT _SBF(0, 0x01)
77 #define SSS_HASHIN_CIPHER_OUTPUT _SBF(0, 0x02)
79 #define SSS_REG_FCBRDMAS 0x0020
80 #define SSS_REG_FCBRDMAL 0x0024
81 #define SSS_REG_FCBRDMAC 0x0028
82 #define SSS_FCBRDMAC_BYTESWAP _BIT(1)
83 #define SSS_FCBRDMAC_FLUSH _BIT(0)
85 #define SSS_REG_FCBTDMAS 0x0030
86 #define SSS_REG_FCBTDMAL 0x0034
87 #define SSS_REG_FCBTDMAC 0x0038
88 #define SSS_FCBTDMAC_BYTESWAP _BIT(1)
89 #define SSS_FCBTDMAC_FLUSH _BIT(0)
91 #define SSS_REG_FCHRDMAS 0x0040
92 #define SSS_REG_FCHRDMAL 0x0044
93 #define SSS_REG_FCHRDMAC 0x0048
94 #define SSS_FCHRDMAC_BYTESWAP _BIT(1)
95 #define SSS_FCHRDMAC_FLUSH _BIT(0)
97 #define SSS_REG_FCPKDMAS 0x0050
98 #define SSS_REG_FCPKDMAL 0x0054
99 #define SSS_REG_FCPKDMAC 0x0058
100 #define SSS_FCPKDMAC_BYTESWAP _BIT(3)
101 #define SSS_FCPKDMAC_DESCEND _BIT(2)
102 #define SSS_FCPKDMAC_TRANSMIT _BIT(1)
103 #define SSS_FCPKDMAC_FLUSH _BIT(0)
105 #define SSS_REG_FCPKDMAO 0x005C
108 #define SSS_REG_AES_CONTROL 0x4000
109 #define SSS_AES_BYTESWAP_DI _BIT(11)
110 #define SSS_AES_BYTESWAP_DO _BIT(10)
111 #define SSS_AES_BYTESWAP_IV _BIT(9)
112 #define SSS_AES_BYTESWAP_CNT _BIT(8)
113 #define SSS_AES_BYTESWAP_KEY _BIT(7)
114 #define SSS_AES_KEY_CHANGE_MODE _BIT(6)
115 #define SSS_AES_KEY_SIZE_128 _SBF(4, 0x00)
116 #define SSS_AES_KEY_SIZE_192 _SBF(4, 0x01)
117 #define SSS_AES_KEY_SIZE_256 _SBF(4, 0x02)
118 #define SSS_AES_FIFO_MODE _BIT(3)
119 #define SSS_AES_CHAIN_MODE_ECB _SBF(1, 0x00)
120 #define SSS_AES_CHAIN_MODE_CBC _SBF(1, 0x01)
121 #define SSS_AES_CHAIN_MODE_CTR _SBF(1, 0x02)
122 #define SSS_AES_MODE_DECRYPT _BIT(0)
124 #define SSS_REG_AES_STATUS 0x4004
125 #define SSS_AES_BUSY _BIT(2)
126 #define SSS_AES_INPUT_READY _BIT(1)
127 #define SSS_AES_OUTPUT_READY _BIT(0)
129 #define SSS_REG_AES_IN_DATA(s) (0x4010 + (s << 2))
130 #define SSS_REG_AES_OUT_DATA(s) (0x4020 + (s << 2))
131 #define SSS_REG_AES_IV_DATA(s) (0x4030 + (s << 2))
132 #define SSS_REG_AES_CNT_DATA(s) (0x4040 + (s << 2))
133 #define SSS_REG_AES_KEY_DATA(s) (0x4080 + (s << 2))
135 #define SSS_REG(dev, reg) ((dev)->ioaddr + (SSS_REG_##reg))
136 #define SSS_READ(dev, reg) __raw_readl(SSS_REG(dev, reg))
137 #define SSS_WRITE(dev, reg, val) __raw_writel((val), SSS_REG(dev, reg))
140 #define FLAGS_AES_DECRYPT _BIT(0)
141 #define FLAGS_AES_MODE_MASK _SBF(1, 0x03)
142 #define FLAGS_AES_CBC _SBF(1, 0x01)
143 #define FLAGS_AES_CTR _SBF(1, 0x02)
145 #define AES_KEY_LEN 16
146 #define CRYPTO_QUEUE_LEN 1
195 dev->
req->base.complete(&dev->
req->base, err);
199 static void s5p_unset_outdata(
struct s5p_aes_dev *dev)
204 static void s5p_unset_indata(
struct s5p_aes_dev *dev)
265 s5p_unset_outdata(dev);
270 s5p_aes_complete(dev, err);
274 s5p_set_dma_outdata(dev, dev->
sg_dst);
276 s5p_aes_complete(dev, err);
283 s5p_unset_indata(dev);
288 s5p_aes_complete(dev, err);
292 s5p_set_dma_indata(dev, dev->
sg_src);
299 struct s5p_aes_dev *dev = platform_get_drvdata(pdev);
315 spin_unlock_irqrestore(&dev->
lock, flags);
334 memcpy(keystart, key, keylen);
337 static void s5p_aes_crypt_start(
struct s5p_aes_dev *dev,
unsigned long mode)
374 err = s5p_set_indata(dev, req->
src);
378 err = s5p_set_outdata(dev, req->
dst);
382 SSS_WRITE(dev, AES_CONTROL, aes_control);
383 s5p_set_aes(dev, dev->
ctx->aes_key, req->
info, dev->
ctx->keylen);
385 s5p_set_dma_indata(dev, req->
src);
386 s5p_set_dma_outdata(dev, req->
dst);
391 spin_unlock_irqrestore(&dev->
lock, flags);
396 s5p_unset_indata(dev);
399 s5p_aes_complete(dev, err);
400 spin_unlock_irqrestore(&dev->
lock, flags);
403 static void s5p_tasklet_cb(
unsigned long data)
411 backlog = crypto_get_backlog(&dev->
queue);
413 spin_unlock_irqrestore(&dev->
lock, flags);
421 dev->
req = ablkcipher_request_cast(async_req);
422 dev->
ctx = crypto_tfm_ctx(dev->
req->base.tfm);
423 reqctx = ablkcipher_request_ctx(dev->
req);
425 s5p_aes_crypt_start(dev, reqctx->
mode);
428 static int s5p_aes_handle_req(
struct s5p_aes_dev *dev,
437 spin_unlock_irqrestore(&dev->
lock, flags);
442 err = ablkcipher_enqueue_request(&dev->
queue, req);
443 spin_unlock_irqrestore(&dev->
lock, flags);
445 tasklet_schedule(&dev->
tasklet);
459 pr_err(
"request size is not exact amount of AES blocks\n");
465 return s5p_aes_handle_req(dev, req);
469 const uint8_t *key,
unsigned int keylen)
471 struct crypto_tfm *tfm = crypto_ablkcipher_tfm(cipher);
487 return s5p_aes_crypt(req, 0);
492 return s5p_aes_crypt(req, FLAGS_AES_DECRYPT);
502 return s5p_aes_crypt(req, FLAGS_AES_DECRYPT |
FLAGS_AES_CBC);
505 static int s5p_aes_cra_init(
struct crypto_tfm *tfm)
517 .cra_name =
"ecb(aes)",
518 .cra_driver_name =
"ecb-aes-s5p",
525 .cra_alignmask = 0x0f,
528 .cra_init = s5p_aes_cra_init,
529 .cra_u.ablkcipher = {
532 .setkey = s5p_aes_setkey,
533 .encrypt = s5p_aes_ecb_encrypt,
534 .decrypt = s5p_aes_ecb_decrypt,
538 .cra_name =
"cbc(aes)",
539 .cra_driver_name =
"cbc-aes-s5p",
546 .cra_alignmask = 0x0f,
549 .cra_init = s5p_aes_cra_init,
550 .cra_u.ablkcipher = {
554 .setkey = s5p_aes_setkey,
555 .encrypt = s5p_aes_cbc_encrypt,
556 .decrypt = s5p_aes_cbc_decrypt,
580 resource_size(res), pdev->
name))
584 if (IS_ERR(pdata->
clk)) {
585 dev_err(dev,
"failed to find secss clock source\n");
598 dev_warn(dev,
"hash interrupt is not available.\n");
601 err = devm_request_irq(dev, pdata->
irq_hash, s5p_aes_interrupt,
604 dev_warn(dev,
"hash interrupt is not available.\n");
611 dev_warn(dev,
"feed control interrupt is not available.\n");
614 err = devm_request_irq(dev, pdata->
irq_fc, s5p_aes_interrupt,
617 dev_warn(dev,
"feed control interrupt is not available.\n");
622 platform_set_drvdata(pdev, pdata);
634 pr_info(
"s5p-sss driver registered\n");
639 dev_err(dev,
"can't register '%s': %d\n", algs[i].cra_name, err);
641 for (j = 0; j <
i; j++)
651 platform_set_drvdata(pdev,
NULL);
658 struct s5p_aes_dev *pdata = platform_get_drvdata(pdev);
673 platform_set_drvdata(pdev,
NULL);
679 .probe = s5p_aes_probe,
680 .remove = s5p_aes_remove,