jh7110-cryp.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Cryptographic API.
  4. *
  5. * Support for StarFive hardware cryptographic engine.
  6. * Copyright (c) 2022 StarFive Technology
  7. *
  8. */
  9. #include <crypto/engine.h>
  10. #include "jh7110-cryp.h"
  11. #include <linux/clk.h>
  12. #include <linux/completion.h>
  13. #include <linux/err.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/iopoll.h>
  16. #include <linux/kernel.h>
  17. #include <linux/mod_devicetable.h>
  18. #include <linux/module.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/pm_runtime.h>
  21. #include <linux/reset.h>
  22. #include <linux/spinlock.h>
  23. #define DRIVER_NAME "jh7110-crypto"
  24. struct starfive_dev_list {
  25. struct list_head dev_list;
  26. spinlock_t lock; /* protect dev_list */
  27. };
  28. static struct starfive_dev_list dev_list = {
  29. .dev_list = LIST_HEAD_INIT(dev_list.dev_list),
  30. .lock = __SPIN_LOCK_UNLOCKED(dev_list.lock),
  31. };
  32. struct starfive_cryp_dev *starfive_cryp_find_dev(struct starfive_cryp_ctx *ctx)
  33. {
  34. struct starfive_cryp_dev *cryp = NULL, *tmp;
  35. spin_lock_bh(&dev_list.lock);
  36. if (!ctx->cryp) {
  37. list_for_each_entry(tmp, &dev_list.dev_list, list) {
  38. cryp = tmp;
  39. break;
  40. }
  41. ctx->cryp = cryp;
  42. } else {
  43. cryp = ctx->cryp;
  44. }
  45. spin_unlock_bh(&dev_list.lock);
  46. return cryp;
  47. }
  48. static u16 side_chan;
  49. module_param(side_chan, ushort, 0);
  50. MODULE_PARM_DESC(side_chan, "Enable side channel mitigation for AES module.\n"
  51. "Enabling this feature will reduce speed performance.\n"
  52. " 0 - Disabled\n"
  53. " other - Enabled");
  54. static int starfive_dma_init(struct starfive_cryp_dev *cryp)
  55. {
  56. dma_cap_mask_t mask;
  57. dma_cap_zero(mask);
  58. dma_cap_set(DMA_SLAVE, mask);
  59. cryp->tx = dma_request_chan(cryp->dev, "tx");
  60. if (IS_ERR(cryp->tx))
  61. return dev_err_probe(cryp->dev, PTR_ERR(cryp->tx),
  62. "Error requesting tx dma channel.\n");
  63. cryp->rx = dma_request_chan(cryp->dev, "rx");
  64. if (IS_ERR(cryp->rx)) {
  65. dma_release_channel(cryp->tx);
  66. return dev_err_probe(cryp->dev, PTR_ERR(cryp->rx),
  67. "Error requesting rx dma channel.\n");
  68. }
  69. return 0;
  70. }
  71. static void starfive_dma_cleanup(struct starfive_cryp_dev *cryp)
  72. {
  73. dma_release_channel(cryp->tx);
  74. dma_release_channel(cryp->rx);
  75. }
  76. static int starfive_cryp_probe(struct platform_device *pdev)
  77. {
  78. struct starfive_cryp_dev *cryp;
  79. struct resource *res;
  80. int ret;
  81. cryp = devm_kzalloc(&pdev->dev, sizeof(*cryp), GFP_KERNEL);
  82. if (!cryp)
  83. return -ENOMEM;
  84. platform_set_drvdata(pdev, cryp);
  85. cryp->dev = &pdev->dev;
  86. cryp->base = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
  87. if (IS_ERR(cryp->base))
  88. return dev_err_probe(&pdev->dev, PTR_ERR(cryp->base),
  89. "Error remapping memory for platform device\n");
  90. cryp->phys_base = res->start;
  91. cryp->dma_maxburst = 32;
  92. cryp->side_chan = side_chan;
  93. cryp->hclk = devm_clk_get(&pdev->dev, "hclk");
  94. if (IS_ERR(cryp->hclk))
  95. return dev_err_probe(&pdev->dev, PTR_ERR(cryp->hclk),
  96. "Error getting hardware reference clock\n");
  97. cryp->ahb = devm_clk_get(&pdev->dev, "ahb");
  98. if (IS_ERR(cryp->ahb))
  99. return dev_err_probe(&pdev->dev, PTR_ERR(cryp->ahb),
  100. "Error getting ahb reference clock\n");
  101. cryp->rst = devm_reset_control_get_shared(cryp->dev, NULL);
  102. if (IS_ERR(cryp->rst))
  103. return dev_err_probe(&pdev->dev, PTR_ERR(cryp->rst),
  104. "Error getting hardware reset line\n");
  105. clk_prepare_enable(cryp->hclk);
  106. clk_prepare_enable(cryp->ahb);
  107. reset_control_deassert(cryp->rst);
  108. spin_lock(&dev_list.lock);
  109. list_add(&cryp->list, &dev_list.dev_list);
  110. spin_unlock(&dev_list.lock);
  111. ret = starfive_dma_init(cryp);
  112. if (ret)
  113. goto err_dma_init;
  114. /* Initialize crypto engine */
  115. cryp->engine = crypto_engine_alloc_init(&pdev->dev, 1);
  116. if (!cryp->engine) {
  117. ret = -ENOMEM;
  118. goto err_engine;
  119. }
  120. ret = crypto_engine_start(cryp->engine);
  121. if (ret)
  122. goto err_engine_start;
  123. ret = starfive_aes_register_algs();
  124. if (ret)
  125. goto err_engine_start;
  126. ret = starfive_hash_register_algs();
  127. if (ret)
  128. goto err_algs_hash;
  129. ret = starfive_rsa_register_algs();
  130. if (ret)
  131. goto err_algs_rsa;
  132. return 0;
  133. err_algs_rsa:
  134. starfive_hash_unregister_algs();
  135. err_algs_hash:
  136. starfive_aes_unregister_algs();
  137. err_engine_start:
  138. crypto_engine_exit(cryp->engine);
  139. err_engine:
  140. starfive_dma_cleanup(cryp);
  141. err_dma_init:
  142. spin_lock(&dev_list.lock);
  143. list_del(&cryp->list);
  144. spin_unlock(&dev_list.lock);
  145. clk_disable_unprepare(cryp->hclk);
  146. clk_disable_unprepare(cryp->ahb);
  147. reset_control_assert(cryp->rst);
  148. return ret;
  149. }
  150. static void starfive_cryp_remove(struct platform_device *pdev)
  151. {
  152. struct starfive_cryp_dev *cryp = platform_get_drvdata(pdev);
  153. starfive_aes_unregister_algs();
  154. starfive_hash_unregister_algs();
  155. starfive_rsa_unregister_algs();
  156. crypto_engine_exit(cryp->engine);
  157. starfive_dma_cleanup(cryp);
  158. spin_lock(&dev_list.lock);
  159. list_del(&cryp->list);
  160. spin_unlock(&dev_list.lock);
  161. clk_disable_unprepare(cryp->hclk);
  162. clk_disable_unprepare(cryp->ahb);
  163. reset_control_assert(cryp->rst);
  164. }
  165. static const struct of_device_id starfive_dt_ids[] __maybe_unused = {
  166. { .compatible = "starfive,jh7110-crypto", .data = NULL},
  167. {},
  168. };
  169. MODULE_DEVICE_TABLE(of, starfive_dt_ids);
  170. static struct platform_driver starfive_cryp_driver = {
  171. .probe = starfive_cryp_probe,
  172. .remove = starfive_cryp_remove,
  173. .driver = {
  174. .name = DRIVER_NAME,
  175. .of_match_table = starfive_dt_ids,
  176. },
  177. };
  178. module_platform_driver(starfive_cryp_driver);
  179. MODULE_LICENSE("GPL");
  180. MODULE_DESCRIPTION("StarFive JH7110 Cryptographic Module");