17 #include <linux/kernel.h>
18 #include <linux/module.h>
19 #include <linux/slab.h>
26 #include <linux/device.h>
28 #include <linux/errno.h>
44 #define TDES_FLAGS_MODE_MASK 0x007f
45 #define TDES_FLAGS_ENCRYPT BIT(0)
46 #define TDES_FLAGS_CBC BIT(1)
47 #define TDES_FLAGS_CFB BIT(2)
48 #define TDES_FLAGS_CFB8 BIT(3)
49 #define TDES_FLAGS_CFB16 BIT(4)
50 #define TDES_FLAGS_CFB32 BIT(5)
51 #define TDES_FLAGS_OFB BIT(6)
53 #define TDES_FLAGS_INIT BIT(16)
54 #define TDES_FLAGS_FAST BIT(17)
55 #define TDES_FLAGS_BUSY BIT(18)
57 #define ATMEL_TDES_QUEUE_LENGTH 1
59 #define CFB8_BLOCK_SIZE 1
60 #define CFB16_BLOCK_SIZE 2
61 #define CFB32_BLOCK_SIZE 4
62 #define CFB64_BLOCK_SIZE 8
131 unsigned int count, off = 0;
133 while (buflen && total) {
134 count =
min((*sg)->length - *offset, total);
135 count =
min(count, buflen);
147 if (*offset == (*sg)->length) {
167 writel_relaxed(value, dd->
io_base + offset);
173 for (; count--; value++, offset += 4)
174 atmel_tdes_write(dd, offset, *value);
182 spin_lock_bh(&atmel_tdes.
lock);
192 spin_unlock_bh(&atmel_tdes.
lock);
199 clk_prepare_enable(dd->
iclk);
215 err = atmel_tdes_hw_init(dd);
251 atmel_tdes_write(dd,
TDES_CR, valcr);
252 atmel_tdes_write(dd,
TDES_MR, valmr);
255 dd->
ctx->keylen >> 2);
284 pr_err(
"not all data converted: %u\n", count);
332 pr_err(
"error: %d\n", err);
368 atmel_tdes_write(dd,
TDES_TPR, dma_addr_in);
369 atmel_tdes_write(dd,
TDES_TCR, len32);
370 atmel_tdes_write(dd,
TDES_RPR, dma_addr_out);
371 atmel_tdes_write(dd,
TDES_RCR, len32);
384 struct crypto_tfm *tfm = crypto_ablkcipher_tfm(
385 crypto_ablkcipher_reqtfm(dd->
req));
402 if (count != dd->
total) {
403 pr_err(
"request length != buffer length\n");
441 err = atmel_tdes_crypt_dma(tfm, addr_in, addr_out, count);
451 static void atmel_tdes_finish_req(
struct atmel_tdes_dev *dd,
int err)
455 clk_disable_unprepare(dd->
iclk);
459 req->
base.complete(&req->
base, err);
473 ret = ablkcipher_enqueue_request(&dd->
queue, req);
475 spin_unlock_irqrestore(&dd->
lock, flags);
478 backlog = crypto_get_backlog(&dd->
queue);
482 spin_unlock_irqrestore(&dd->
lock, flags);
490 req = ablkcipher_request_cast(async_req);
500 rctx = ablkcipher_request_ctx(req);
501 ctx = crypto_ablkcipher_ctx(crypto_ablkcipher_reqtfm(req));
507 err = atmel_tdes_write_ctrl(dd);
509 err = atmel_tdes_crypt_dma_start(dd);
512 atmel_tdes_finish_req(dd, err);
523 crypto_ablkcipher_reqtfm(req));
529 pr_err(
"request size is not exact amount of CFB8 blocks\n");
534 pr_err(
"request size is not exact amount of CFB16 blocks\n");
539 pr_err(
"request size is not exact amount of CFB32 blocks\n");
543 pr_err(
"request size is not exact amount of DES blocks\n");
547 dd = atmel_tdes_find_dev(ctx);
553 return atmel_tdes_handle_queue(dd, req);
561 struct crypto_tfm *ctfm = crypto_ablkcipher_tfm(tfm);
586 const char *alg_name;
588 alg_name = crypto_tfm_alg_name(crypto_ablkcipher_tfm(tfm));
614 return atmel_tdes_crypt(req, 0);
679 static int atmel_tdes_cra_init(
struct crypto_tfm *tfm)
686 static void atmel_tdes_cra_exit(
struct crypto_tfm *tfm)
692 .cra_name =
"ecb(des)",
693 .cra_driver_name =
"atmel-ecb-des",
701 .cra_init = atmel_tdes_cra_init,
702 .cra_exit = atmel_tdes_cra_exit,
703 .cra_u.ablkcipher = {
706 .setkey = atmel_des_setkey,
707 .encrypt = atmel_tdes_ecb_encrypt,
708 .decrypt = atmel_tdes_ecb_decrypt,
712 .cra_name =
"cbc(des)",
713 .cra_driver_name =
"atmel-cbc-des",
721 .cra_init = atmel_tdes_cra_init,
722 .cra_exit = atmel_tdes_cra_exit,
723 .cra_u.ablkcipher = {
727 .setkey = atmel_des_setkey,
728 .encrypt = atmel_tdes_cbc_encrypt,
729 .decrypt = atmel_tdes_cbc_decrypt,
733 .cra_name =
"cfb(des)",
734 .cra_driver_name =
"atmel-cfb-des",
742 .cra_init = atmel_tdes_cra_init,
743 .cra_exit = atmel_tdes_cra_exit,
744 .cra_u.ablkcipher = {
748 .setkey = atmel_des_setkey,
749 .encrypt = atmel_tdes_cfb_encrypt,
750 .decrypt = atmel_tdes_cfb_decrypt,
754 .cra_name =
"cfb8(des)",
755 .cra_driver_name =
"atmel-cfb8-des",
763 .cra_init = atmel_tdes_cra_init,
764 .cra_exit = atmel_tdes_cra_exit,
765 .cra_u.ablkcipher = {
769 .setkey = atmel_des_setkey,
770 .encrypt = atmel_tdes_cfb8_encrypt,
771 .decrypt = atmel_tdes_cfb8_decrypt,
775 .cra_name =
"cfb16(des)",
776 .cra_driver_name =
"atmel-cfb16-des",
784 .cra_init = atmel_tdes_cra_init,
785 .cra_exit = atmel_tdes_cra_exit,
786 .cra_u.ablkcipher = {
790 .setkey = atmel_des_setkey,
791 .encrypt = atmel_tdes_cfb16_encrypt,
792 .decrypt = atmel_tdes_cfb16_decrypt,
796 .cra_name =
"cfb32(des)",
797 .cra_driver_name =
"atmel-cfb32-des",
805 .cra_init = atmel_tdes_cra_init,
806 .cra_exit = atmel_tdes_cra_exit,
807 .cra_u.ablkcipher = {
811 .setkey = atmel_des_setkey,
812 .encrypt = atmel_tdes_cfb32_encrypt,
813 .decrypt = atmel_tdes_cfb32_decrypt,
817 .cra_name =
"ofb(des)",
818 .cra_driver_name =
"atmel-ofb-des",
826 .cra_init = atmel_tdes_cra_init,
827 .cra_exit = atmel_tdes_cra_exit,
828 .cra_u.ablkcipher = {
832 .setkey = atmel_des_setkey,
833 .encrypt = atmel_tdes_ofb_encrypt,
834 .decrypt = atmel_tdes_ofb_decrypt,
838 .cra_name =
"ecb(des3_ede)",
839 .cra_driver_name =
"atmel-ecb-tdes",
847 .cra_init = atmel_tdes_cra_init,
848 .cra_exit = atmel_tdes_cra_exit,
849 .cra_u.ablkcipher = {
852 .setkey = atmel_tdes_setkey,
853 .encrypt = atmel_tdes_ecb_encrypt,
854 .decrypt = atmel_tdes_ecb_decrypt,
858 .cra_name =
"cbc(des3_ede)",
859 .cra_driver_name =
"atmel-cbc-tdes",
867 .cra_init = atmel_tdes_cra_init,
868 .cra_exit = atmel_tdes_cra_exit,
869 .cra_u.ablkcipher = {
873 .setkey = atmel_tdes_setkey,
874 .encrypt = atmel_tdes_cbc_encrypt,
875 .decrypt = atmel_tdes_cbc_decrypt,
879 .cra_name =
"cfb(des3_ede)",
880 .cra_driver_name =
"atmel-cfb-tdes",
888 .cra_init = atmel_tdes_cra_init,
889 .cra_exit = atmel_tdes_cra_exit,
890 .cra_u.ablkcipher = {
894 .setkey = atmel_tdes_setkey,
895 .encrypt = atmel_tdes_cfb_encrypt,
896 .decrypt = atmel_tdes_cfb_decrypt,
900 .cra_name =
"cfb8(des3_ede)",
901 .cra_driver_name =
"atmel-cfb8-tdes",
909 .cra_init = atmel_tdes_cra_init,
910 .cra_exit = atmel_tdes_cra_exit,
911 .cra_u.ablkcipher = {
915 .setkey = atmel_tdes_setkey,
916 .encrypt = atmel_tdes_cfb8_encrypt,
917 .decrypt = atmel_tdes_cfb8_decrypt,
921 .cra_name =
"cfb16(des3_ede)",
922 .cra_driver_name =
"atmel-cfb16-tdes",
930 .cra_init = atmel_tdes_cra_init,
931 .cra_exit = atmel_tdes_cra_exit,
932 .cra_u.ablkcipher = {
936 .setkey = atmel_tdes_setkey,
937 .encrypt = atmel_tdes_cfb16_encrypt,
938 .decrypt = atmel_tdes_cfb16_decrypt,
942 .cra_name =
"cfb32(des3_ede)",
943 .cra_driver_name =
"atmel-cfb32-tdes",
951 .cra_init = atmel_tdes_cra_init,
952 .cra_exit = atmel_tdes_cra_exit,
953 .cra_u.ablkcipher = {
957 .setkey = atmel_tdes_setkey,
958 .encrypt = atmel_tdes_cfb32_encrypt,
959 .decrypt = atmel_tdes_cfb32_decrypt,
963 .cra_name =
"ofb(des3_ede)",
964 .cra_driver_name =
"atmel-ofb-tdes",
972 .cra_init = atmel_tdes_cra_init,
973 .cra_exit = atmel_tdes_cra_exit,
974 .cra_u.ablkcipher = {
978 .setkey = atmel_tdes_setkey,
979 .encrypt = atmel_tdes_ofb_encrypt,
980 .decrypt = atmel_tdes_ofb_decrypt,
985 static void atmel_tdes_queue_task(
unsigned long data)
989 atmel_tdes_handle_queue(dd,
NULL);
992 static void atmel_tdes_done_task(
unsigned long data)
997 err = atmel_tdes_crypt_dma_stop(dd);
1001 if (dd->
total && !err) {
1002 err = atmel_tdes_crypt_dma_start(dd);
1007 atmel_tdes_finish_req(dd, err);
1008 atmel_tdes_handle_queue(dd,
NULL);
1016 reg = atmel_tdes_read(tdes_dd,
TDES_ISR);
1017 if (reg & atmel_tdes_read(tdes_dd,
TDES_IMR)) {
1018 atmel_tdes_write(tdes_dd,
TDES_IDR, reg);
1022 dev_warn(tdes_dd->
dev,
"TDES interrupt when no active requests.\n");
1029 static void atmel_tdes_unregister_algs(
struct atmel_tdes_dev *dd)
1041 for (i = 0; i <
ARRAY_SIZE(tdes_algs); i++) {
1050 for (j = 0; j <
i; j++)
1061 unsigned long tdes_phys_size;
1065 if (tdes_dd ==
NULL) {
1066 dev_err(dev,
"unable to alloc data struct.\n");
1073 platform_set_drvdata(pdev, tdes_dd);
1075 INIT_LIST_HEAD(&tdes_dd->
list);
1078 (
unsigned long)tdes_dd);
1080 (
unsigned long)tdes_dd);
1089 dev_err(dev,
"no MEM resource info\n");
1094 tdes_phys_size = resource_size(tdes_res);
1098 if (tdes_dd->
irq < 0) {
1099 dev_err(dev,
"no IRQ resource info\n");
1105 "atmel-tdes", tdes_dd);
1107 dev_err(dev,
"unable to request tdes irq.\n");
1113 if (IS_ERR(tdes_dd->
iclk)) {
1114 dev_err(dev,
"clock intialization failed.\n");
1115 err = PTR_ERR(tdes_dd->
iclk);
1121 dev_err(dev,
"can't ioremap\n");
1126 err = atmel_tdes_dma_init(tdes_dd);
1130 spin_lock(&atmel_tdes.
lock);
1132 spin_unlock(&atmel_tdes.
lock);
1134 err = atmel_tdes_register_algs(tdes_dd);
1143 spin_lock(&atmel_tdes.
lock);
1145 spin_unlock(&atmel_tdes.
lock);
1146 atmel_tdes_dma_cleanup(tdes_dd);
1160 dev_err(dev,
"initialization failed.\n");
1169 tdes_dd = platform_get_drvdata(pdev);
1172 spin_lock(&atmel_tdes.
lock);
1174 spin_unlock(&atmel_tdes.
lock);
1176 atmel_tdes_unregister_algs(tdes_dd);
1181 atmel_tdes_dma_cleanup(tdes_dd);
1187 if (tdes_dd->
irq >= 0)
1197 .probe = atmel_tdes_probe,
1200 .name =
"atmel_tdes",