17 #include <linux/errno.h>
22 #include <linux/module.h>
27 #include <crypto/aes.h>
36 #include <mach/hardware.h>
41 #define CRYP_MAX_KEY_SIZE 32
42 #define BYTES_PER_WORD 4
98 static inline u32 uint8p_to_uint32_be(
u8 *
in)
127 static inline u8 swap_bits_in_byte(
u8 b)
129 #define R_SHIFT_4_MASK 0xc0
130 #define R_SHIFT_2_MASK 0x28
132 #define R_SHIFT_1_MASK 0x1e
134 #define L_SHIFT_4_MASK 0x03
135 #define L_SHIFT_2_MASK 0x14
137 #define L_SHIFT_1_MASK 0x78
162 static inline void swap_words_in_key_and_bits_in_byte(
const u8 *
in,
172 index = len - j - BYTES_PER_WORD +
i;
174 swap_bits_in_byte(in[index]);
220 for (i = 0; i < ctx->
blocksize / 4; i++) {
222 &device_data->
base->dout);
235 for (i = 0 ; i < ctx->
blocksize / 4; i++) {
236 writel_relaxed(ctx->
indata,
237 &device_data->
base->din);
275 vector_value.init_value_left =
left;
276 vector_value.init_value_right =
right;
297 if (num_of_regs > 2) {
298 dev_err(device_data->
dev,
"[%s] Incorrect blocksize %d",
304 iv[i] = uint8p_to_uint32_be(ctx->
iv + i*4);
306 for (i = 0; i < num_of_regs; i++) {
307 status = cfg_iv(device_data, iv[i*2], iv[i*2+1],
325 key_value.key_value_left = left_key;
326 key_value.key_value_right = right_key;
333 "cryp_configure_key_values() failed!", __func__);
338 static int cfg_keys(
struct cryp_ctx *ctx)
341 int num_of_regs = ctx->
keylen / 8;
347 if (mode_is_aes(ctx->
config.algomode)) {
348 swap_words_in_key_and_bits_in_byte((
u8 *)ctx->
key,
352 for (i = 0; i < ctx->
keylen / 4; i++)
353 swapped_key[i] = uint8p_to_uint32_be(ctx->
key + i*4);
356 for (i = 0; i < num_of_regs; i++) {
357 cryp_error = set_key(ctx->
device,
358 *(((
u32 *)swapped_key)+i*2),
359 *(((
u32 *)swapped_key)+i*2+1),
362 if (cryp_error != 0) {
371 static int cryp_setup_context(
struct cryp_ctx *ctx,
391 if (cfg_keys(ctx) != 0) {
401 if (cfg_ivs(device_data, ctx) != 0)
408 }
else if (ctx->
updated == 1 &&
414 control_register = ctx->
dev_ctx.cr;
416 control_register = ctx->
dev_ctx.cr;
420 &device_data->
base->cr);
425 static int cryp_get_device_data(
struct cryp_ctx *ctx,
443 while (device_node) {
446 spin_lock(&local_device_data->
ctx_lock);
452 ctx->
device = local_device_data;
453 spin_unlock(&local_device_data->
ctx_lock);
456 spin_unlock(&local_device_data->
ctx_lock);
472 *device_data = local_device_data;
483 device_data->
dma.cfg_mem2cryp = mem_to_engine;
484 device_data->
dma.chan_mem2cryp =
487 device_data->
dma.cfg_mem2cryp);
489 device_data->
dma.cfg_cryp2mem = engine_to_mem;
490 device_data->
dma.chan_cryp2mem =
493 device_data->
dma.cfg_cryp2mem);
495 init_completion(&device_data->
dma.cryp_dma_complete);
498 static void cryp_dma_out_callback(
void *
data)
506 static int cryp_set_dma_transfer(
struct cryp_ctx *ctx,
519 "aligned! Addr: 0x%08x", __func__, (
u32)sg);
525 channel = ctx->
device->dma.chan_mem2cryp;
529 ctx->
device->dma.nents_src,
532 if (!ctx->
device->dma.sg_src_len) {
534 "[%s]: Could not map the sg list (TO_DEVICE)",
540 "(TO_DEVICE)", __func__);
542 desc = channel->
device->device_prep_slave_sg(channel,
544 ctx->
device->dma.sg_src_len,
549 channel = ctx->
device->dma.chan_cryp2mem;
553 ctx->
device->dma.nents_dst,
556 if (!ctx->
device->dma.sg_dst_len) {
558 "[%s]: Could not map the sg list (FROM_DEVICE)",
564 "(FROM_DEVICE)", __func__);
566 desc = channel->
device->device_prep_slave_sg(channel,
568 ctx->
device->dma.sg_dst_len,
573 desc->
callback = cryp_dma_out_callback;
584 dma_async_issue_pending(channel);
589 static void cryp_dma_done(
struct cryp_ctx *ctx)
595 chan = ctx->
device->dma.chan_mem2cryp;
600 chan = ctx->
device->dma.chan_cryp2mem;
633 static void cryp_polling_mode(
struct cryp_ctx *ctx,
637 int remaining_length = ctx->
datalen;
641 while (remaining_length > 0) {
647 readsl(&device_data->
base->dout, outdata, len);
653 static int cryp_disable_power(
struct device *
dev,
655 bool save_device_context)
659 dev_dbg(dev,
"[%s]", __func__);
666 if (save_device_context && device_data->
current_ctx) {
672 spin_unlock(&device_data->
ctx_lock);
678 "regulator_disable() failed!",
689 static int cryp_enable_power(
692 bool restore_device_context)
696 dev_dbg(dev,
"[%s]", __func__);
702 dev_err(dev,
"[%s]: regulator_enable() failed!",
709 dev_err(dev,
"[%s]: clk_enable() failed!",
719 if (restore_device_context && device_data->
current_ctx) {
724 spin_unlock(&device_data->
ctx_lock);
732 static int hw_crypt_noxts(
struct cryp_ctx *ctx,
748 "0x%08x", __func__, (
u32)indata);
752 ret = cryp_setup_context(ctx, device_data);
778 cryp_polling_mode(ctx, device_data);
804 sg = scatterwalk_sg_next(sg);
814 struct cryp_ctx *ctx = crypto_ablkcipher_ctx(cipher);
817 int bytes_written = 0;
826 ret = cryp_get_device_data(ctx, &device_data);
830 ret = cryp_setup_context(ctx, device_data);
841 bytes_written = cryp_dma_write(ctx, areq->
src, ctx->
datalen);
842 bytes_read = cryp_dma_read(ctx, areq->
dst, bytes_written);
854 spin_unlock(&device_data->
ctx_lock);
862 if (
unlikely(bytes_written != bytes_read))
872 struct cryp_ctx *ctx = crypto_ablkcipher_ctx(cipher);
874 unsigned long src_paddr;
875 unsigned long dst_paddr;
881 ret = cryp_get_device_data(ctx, &device_data);
885 ablkcipher_walk_init(&walk, areq->
dst, areq->
src, areq->
nbytes);
894 while ((nbytes = walk.nbytes) > 0) {
896 src_paddr = (
page_to_phys(walk.src.page) + walk.src.offset);
899 dst_paddr = (
page_to_phys(walk.dst.page) + walk.dst.offset);
904 ret = hw_crypt_noxts(ctx, device_data);
913 ablkcipher_walk_complete(&walk);
920 spin_unlock(&device_data->
ctx_lock);
932 const u8 *
key,
unsigned int keylen)
934 struct cryp_ctx *ctx = crypto_ablkcipher_ctx(cipher);
967 const u8 *key,
unsigned int keylen)
969 struct cryp_ctx *ctx = crypto_ablkcipher_ctx(cipher);
970 u32 *flags = &cipher->
base.crt_flags;
998 const u8 *key,
unsigned int keylen)
1000 struct cryp_ctx *ctx = crypto_ablkcipher_ctx(cipher);
1001 u32 *flags = &cipher->
base.crt_flags;
1002 const u32 *
K = (
const u32 *)key;
1015 if (
unlikely(!((K[0] ^ K[2]) | (K[1] ^ K[3])) ||
1016 !((K[2] ^ K[4]) | (K[3] ^ K[5]))) &&
1023 for (i = 0; i < 3; i++) {
1025 if (
unlikely(ret == 0) && (*flags & CRYPTO_TFM_REQ_WEAK_KEY)) {
1028 "CRYPTO_TFM_REQ_WEAK_KEY", __func__);
1043 struct cryp_ctx *ctx = crypto_ablkcipher_ctx(cipher);
1052 return ablk_dma_crypt(areq);
1055 return ablk_crypt(areq);
1061 struct cryp_ctx *ctx = crypto_ablkcipher_ctx(cipher);
1069 return ablk_dma_crypt(areq);
1072 return ablk_crypt(areq);
1080 static int cryp_cra_init(
struct crypto_tfm *tfm)
1082 struct cryp_ctx *ctx = crypto_tfm_ctx(tfm);
1089 ctx->
blocksize = crypto_tfm_alg_blocksize(tfm);
1099 .cra_driver_name =
"aes-ux500",
1100 .cra_priority = 300,
1104 .cra_ctxsize =
sizeof(
struct cryp_ctx),
1107 .cra_init = cryp_cra_init,
1113 .setkey = aes_ablkcipher_setkey,
1114 .encrypt = cryp_blk_encrypt,
1115 .decrypt = cryp_blk_decrypt
1123 .cra_name =
"ecb(aes)",
1124 .cra_driver_name =
"ecb-aes-ux500",
1125 .cra_priority = 300,
1129 .cra_ctxsize =
sizeof(
struct cryp_ctx),
1132 .cra_init = cryp_cra_init,
1138 .setkey = aes_ablkcipher_setkey,
1139 .encrypt = cryp_blk_encrypt,
1140 .decrypt = cryp_blk_decrypt,
1148 .cra_name =
"cbc(aes)",
1149 .cra_driver_name =
"cbc-aes-ux500",
1150 .cra_priority = 300,
1154 .cra_ctxsize =
sizeof(
struct cryp_ctx),
1157 .cra_init = cryp_cra_init,
1163 .setkey = aes_ablkcipher_setkey,
1164 .encrypt = cryp_blk_encrypt,
1165 .decrypt = cryp_blk_decrypt,
1174 .cra_name =
"ctr(aes)",
1175 .cra_driver_name =
"ctr-aes-ux500",
1176 .cra_priority = 300,
1180 .cra_ctxsize =
sizeof(
struct cryp_ctx),
1183 .cra_init = cryp_cra_init,
1189 .setkey = aes_ablkcipher_setkey,
1190 .encrypt = cryp_blk_encrypt,
1191 .decrypt = cryp_blk_decrypt,
1201 .cra_driver_name =
"des-ux500",
1202 .cra_priority = 300,
1206 .cra_ctxsize =
sizeof(
struct cryp_ctx),
1209 .cra_init = cryp_cra_init,
1215 .setkey = des_ablkcipher_setkey,
1216 .encrypt = cryp_blk_encrypt,
1217 .decrypt = cryp_blk_decrypt
1226 .cra_name =
"des3_ede",
1227 .cra_driver_name =
"des3_ede-ux500",
1228 .cra_priority = 300,
1232 .cra_ctxsize =
sizeof(
struct cryp_ctx),
1235 .cra_init = cryp_cra_init,
1241 .setkey = des_ablkcipher_setkey,
1242 .encrypt = cryp_blk_encrypt,
1243 .decrypt = cryp_blk_decrypt
1251 .cra_name =
"ecb(des)",
1252 .cra_driver_name =
"ecb-des-ux500",
1253 .cra_priority = 300,
1257 .cra_ctxsize =
sizeof(
struct cryp_ctx),
1260 .cra_init = cryp_cra_init,
1266 .setkey = des_ablkcipher_setkey,
1267 .encrypt = cryp_blk_encrypt,
1268 .decrypt = cryp_blk_decrypt,
1276 .cra_name =
"ecb(des3_ede)",
1277 .cra_driver_name =
"ecb-des3_ede-ux500",
1278 .cra_priority = 300,
1282 .cra_ctxsize =
sizeof(
struct cryp_ctx),
1285 .cra_init = cryp_cra_init,
1291 .setkey = des3_ablkcipher_setkey,
1292 .encrypt = cryp_blk_encrypt,
1293 .decrypt = cryp_blk_decrypt,
1301 .cra_name =
"cbc(des)",
1302 .cra_driver_name =
"cbc-des-ux500",
1303 .cra_priority = 300,
1307 .cra_ctxsize =
sizeof(
struct cryp_ctx),
1310 .cra_init = cryp_cra_init,
1316 .setkey = des_ablkcipher_setkey,
1317 .encrypt = cryp_blk_encrypt,
1318 .decrypt = cryp_blk_decrypt,
1326 .cra_name =
"cbc(des3_ede)",
1327 .cra_driver_name =
"cbc-des3_ede-ux500",
1328 .cra_priority = 300,
1332 .cra_ctxsize =
sizeof(
struct cryp_ctx),
1335 .cra_init = cryp_cra_init,
1341 .setkey = des3_ablkcipher_setkey,
1342 .encrypt = cryp_blk_encrypt,
1343 .decrypt = cryp_blk_decrypt,
1354 static int cryp_algs_register_all(
void)
1362 for (i = 0; i <
ARRAY_SIZE(cryp_algs); i++) {
1366 pr_err(
"[%s] alg registration failed",
1367 cryp_algs[i].crypto.cra_driver_name);
1373 for (i = 0; i <
count; i++)
1381 static void cryp_algs_unregister_all(
void)
1403 dev_dbg(dev,
"[%s]", __func__);
1406 dev_err(dev,
"[%s]: kzalloc() failed!", __func__);
1422 dev_err(dev,
"[%s]: platform_get_resource() failed",
1430 dev_err(dev,
"[%s]: request_mem_region() failed",
1437 if (!device_data->
base) {
1438 dev_err(dev,
"[%s]: ioremap failed!", __func__);
1449 dev_err(dev,
"[%s]: could not get cryp regulator", __func__);
1457 if (IS_ERR(device_data->
clk)) {
1458 dev_err(dev,
"[%s]: clk_get() failed!", __func__);
1459 ret = PTR_ERR(device_data->
clk);
1464 ret = cryp_enable_power(device_data->
dev, device_data,
false);
1466 dev_err(dev,
"[%s]: cryp_enable_power() failed!", __func__);
1471 if (cryp_error != 0) {
1472 dev_err(dev,
"[%s]: cryp_init() failed!", __func__);
1478 if (cryp_error != 0) {
1479 dev_err(dev,
"[%s]: cryp_configure_protection() failed!",
1487 dev_err(dev,
"[%s]: IORESOURCE_IRQ unavailable",
1494 cryp_interrupt_handler,
1499 dev_err(dev,
"[%s]: Unable to request IRQ", __func__);
1504 cryp_dma_setup_channel(device_data, dev);
1506 platform_set_drvdata(pdev, device_data);
1516 ret = cryp_algs_register_all();
1518 dev_err(dev,
"[%s]: cryp_algs_register_all() failed!",
1526 cryp_disable_power(device_data->
dev, device_data,
false);
1553 device_data = platform_get_drvdata(pdev);
1555 dev_err(&pdev->
dev,
"[%s]: platform_get_drvdata() failed!",
1569 spin_unlock(&device_data->
ctx_lock);
1575 spin_unlock(&device_data->
ctx_lock);
1583 cryp_algs_unregister_all();
1587 dev_err(&pdev->
dev,
"[%s]: IORESOURCE_IRQ, unavailable",
1594 if (cryp_disable_power(&pdev->
dev, device_data,
false))
1595 dev_err(&pdev->
dev,
"[%s]: cryp_disable_power() failed",
1619 device_data = platform_get_drvdata(pdev);
1621 dev_err(&pdev->
dev,
"[%s]: platform_get_drvdata() failed!",
1631 dev_dbg(&pdev->
dev,
"[%s]: Cryp still in use!"
1632 "Shutting down anyway...", __func__);
1640 spin_unlock(&device_data->
ctx_lock);
1648 cryp_algs_unregister_all();
1652 dev_err(&pdev->
dev,
"[%s]: IORESOURCE_IRQ, unavailable",
1659 if (cryp_disable_power(&pdev->
dev, device_data,
false))
1660 dev_err(&pdev->
dev,
"[%s]: cryp_disable_power() failed",
1665 static int ux500_cryp_suspend(
struct device *dev)
1673 dev_dbg(dev,
"[%s]", __func__);
1676 device_data = platform_get_drvdata(pdev);
1678 dev_err(dev,
"[%s]: platform_get_drvdata() failed!", __func__);
1684 dev_err(dev,
"[%s]: IORESOURCE_IRQ, unavailable", __func__);
1691 spin_unlock(&device_data->
ctx_lock);
1695 dev_dbg(dev,
"[%s]: down_interruptible() failed",
1697 ret = cryp_disable_power(dev, device_data,
false);
1700 ret = cryp_disable_power(dev, device_data,
true);
1703 dev_err(dev,
"[%s]: cryp_disable_power()", __func__);
1708 static int ux500_cryp_resume(
struct device *dev)
1716 dev_dbg(dev,
"[%s]", __func__);
1718 device_data = platform_get_drvdata(pdev);
1720 dev_err(dev,
"[%s]: platform_get_drvdata() failed!", __func__);
1727 spin_unlock(&device_data->
ctx_lock);
1733 ret = cryp_enable_power(dev, device_data,
true);
1736 dev_err(dev,
"[%s]: cryp_enable_power() failed!", __func__);
1749 .probe = ux500_cryp_probe,
1750 .remove = ux500_cryp_remove,
1751 .shutdown = ux500_cryp_shutdown,
1755 .pm = &ux500_cryp_pm,
1759 static int __init ux500_cryp_mod_init(
void)
1761 pr_debug(
"[%s] is called!", __func__);
1768 static void __exit ux500_cryp_mod_fini(
void)
1770 pr_debug(
"[%s] is called!", __func__);