aspeed-hace.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (c) 2021 Aspeed Technology Inc.
  4. */
  5. #include "aspeed-hace.h"
  6. #include <crypto/engine.h>
  7. #include <linux/clk.h>
  8. #include <linux/dma-mapping.h>
  9. #include <linux/err.h>
  10. #include <linux/interrupt.h>
  11. #include <linux/io.h>
  12. #include <linux/kernel.h>
  13. #include <linux/module.h>
  14. #include <linux/of.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/property.h>
  17. #ifdef CONFIG_CRYPTO_DEV_ASPEED_DEBUG
  18. #define HACE_DBG(d, fmt, ...) \
  19. dev_info((d)->dev, "%s() " fmt, __func__, ##__VA_ARGS__)
  20. #else
  21. #define HACE_DBG(d, fmt, ...) \
  22. dev_dbg((d)->dev, "%s() " fmt, __func__, ##__VA_ARGS__)
  23. #endif
  24. /* HACE interrupt service routine */
  25. static irqreturn_t aspeed_hace_irq(int irq, void *dev)
  26. {
  27. struct aspeed_hace_dev *hace_dev = (struct aspeed_hace_dev *)dev;
  28. struct aspeed_engine_crypto *crypto_engine = &hace_dev->crypto_engine;
  29. struct aspeed_engine_hash *hash_engine = &hace_dev->hash_engine;
  30. u32 sts;
  31. sts = ast_hace_read(hace_dev, ASPEED_HACE_STS);
  32. ast_hace_write(hace_dev, sts, ASPEED_HACE_STS);
  33. HACE_DBG(hace_dev, "irq status: 0x%x\n", sts);
  34. if (sts & HACE_HASH_ISR) {
  35. if (hash_engine->flags & CRYPTO_FLAGS_BUSY)
  36. tasklet_schedule(&hash_engine->done_task);
  37. else
  38. dev_warn(hace_dev->dev, "HASH no active requests.\n");
  39. }
  40. if (sts & HACE_CRYPTO_ISR) {
  41. if (crypto_engine->flags & CRYPTO_FLAGS_BUSY)
  42. tasklet_schedule(&crypto_engine->done_task);
  43. else
  44. dev_warn(hace_dev->dev, "CRYPTO no active requests.\n");
  45. }
  46. return IRQ_HANDLED;
  47. }
  48. static void aspeed_hace_crypto_done_task(unsigned long data)
  49. {
  50. struct aspeed_hace_dev *hace_dev = (struct aspeed_hace_dev *)data;
  51. struct aspeed_engine_crypto *crypto_engine = &hace_dev->crypto_engine;
  52. crypto_engine->resume(hace_dev);
  53. }
  54. static void aspeed_hace_hash_done_task(unsigned long data)
  55. {
  56. struct aspeed_hace_dev *hace_dev = (struct aspeed_hace_dev *)data;
  57. struct aspeed_engine_hash *hash_engine = &hace_dev->hash_engine;
  58. hash_engine->resume(hace_dev);
  59. }
  60. static void aspeed_hace_register(struct aspeed_hace_dev *hace_dev)
  61. {
  62. #ifdef CONFIG_CRYPTO_DEV_ASPEED_HACE_HASH
  63. aspeed_register_hace_hash_algs(hace_dev);
  64. #endif
  65. #ifdef CONFIG_CRYPTO_DEV_ASPEED_HACE_CRYPTO
  66. aspeed_register_hace_crypto_algs(hace_dev);
  67. #endif
  68. }
  69. static void aspeed_hace_unregister(struct aspeed_hace_dev *hace_dev)
  70. {
  71. #ifdef CONFIG_CRYPTO_DEV_ASPEED_HACE_HASH
  72. aspeed_unregister_hace_hash_algs(hace_dev);
  73. #endif
  74. #ifdef CONFIG_CRYPTO_DEV_ASPEED_HACE_CRYPTO
  75. aspeed_unregister_hace_crypto_algs(hace_dev);
  76. #endif
  77. }
  78. static const struct of_device_id aspeed_hace_of_matches[] = {
  79. { .compatible = "aspeed,ast2500-hace", .data = (void *)5, },
  80. { .compatible = "aspeed,ast2600-hace", .data = (void *)6, },
  81. {},
  82. };
  83. static int aspeed_hace_probe(struct platform_device *pdev)
  84. {
  85. struct aspeed_engine_crypto *crypto_engine;
  86. struct aspeed_engine_hash *hash_engine;
  87. struct aspeed_hace_dev *hace_dev;
  88. int rc;
  89. hace_dev = devm_kzalloc(&pdev->dev, sizeof(struct aspeed_hace_dev),
  90. GFP_KERNEL);
  91. if (!hace_dev)
  92. return -ENOMEM;
  93. hace_dev->version = (uintptr_t)device_get_match_data(&pdev->dev);
  94. if (!hace_dev->version) {
  95. dev_err(&pdev->dev, "Failed to match hace dev id\n");
  96. return -EINVAL;
  97. }
  98. hace_dev->dev = &pdev->dev;
  99. hash_engine = &hace_dev->hash_engine;
  100. crypto_engine = &hace_dev->crypto_engine;
  101. platform_set_drvdata(pdev, hace_dev);
  102. hace_dev->regs = devm_platform_get_and_ioremap_resource(pdev, 0, NULL);
  103. if (IS_ERR(hace_dev->regs))
  104. return PTR_ERR(hace_dev->regs);
  105. /* Get irq number and register it */
  106. hace_dev->irq = platform_get_irq(pdev, 0);
  107. if (hace_dev->irq < 0)
  108. return -ENXIO;
  109. rc = devm_request_irq(&pdev->dev, hace_dev->irq, aspeed_hace_irq, 0,
  110. dev_name(&pdev->dev), hace_dev);
  111. if (rc) {
  112. dev_err(&pdev->dev, "Failed to request interrupt\n");
  113. return rc;
  114. }
  115. /* Get clk and enable it */
  116. hace_dev->clk = devm_clk_get(&pdev->dev, NULL);
  117. if (IS_ERR(hace_dev->clk)) {
  118. dev_err(&pdev->dev, "Failed to get clk\n");
  119. return -ENODEV;
  120. }
  121. rc = clk_prepare_enable(hace_dev->clk);
  122. if (rc) {
  123. dev_err(&pdev->dev, "Failed to enable clock 0x%x\n", rc);
  124. return rc;
  125. }
  126. /* Initialize crypto hardware engine structure for hash */
  127. hace_dev->crypt_engine_hash = crypto_engine_alloc_init(hace_dev->dev,
  128. true);
  129. if (!hace_dev->crypt_engine_hash) {
  130. rc = -ENOMEM;
  131. goto clk_exit;
  132. }
  133. rc = crypto_engine_start(hace_dev->crypt_engine_hash);
  134. if (rc)
  135. goto err_engine_hash_start;
  136. tasklet_init(&hash_engine->done_task, aspeed_hace_hash_done_task,
  137. (unsigned long)hace_dev);
  138. /* Initialize crypto hardware engine structure for crypto */
  139. hace_dev->crypt_engine_crypto = crypto_engine_alloc_init(hace_dev->dev,
  140. true);
  141. if (!hace_dev->crypt_engine_crypto) {
  142. rc = -ENOMEM;
  143. goto err_engine_hash_start;
  144. }
  145. rc = crypto_engine_start(hace_dev->crypt_engine_crypto);
  146. if (rc)
  147. goto err_engine_crypto_start;
  148. tasklet_init(&crypto_engine->done_task, aspeed_hace_crypto_done_task,
  149. (unsigned long)hace_dev);
  150. /* Allocate DMA buffer for hash engine input used */
  151. hash_engine->ahash_src_addr =
  152. dmam_alloc_coherent(&pdev->dev,
  153. ASPEED_HASH_SRC_DMA_BUF_LEN,
  154. &hash_engine->ahash_src_dma_addr,
  155. GFP_KERNEL);
  156. if (!hash_engine->ahash_src_addr) {
  157. dev_err(&pdev->dev, "Failed to allocate dma buffer\n");
  158. rc = -ENOMEM;
  159. goto err_engine_crypto_start;
  160. }
  161. /* Allocate DMA buffer for crypto engine context used */
  162. crypto_engine->cipher_ctx =
  163. dmam_alloc_coherent(&pdev->dev,
  164. PAGE_SIZE,
  165. &crypto_engine->cipher_ctx_dma,
  166. GFP_KERNEL);
  167. if (!crypto_engine->cipher_ctx) {
  168. dev_err(&pdev->dev, "Failed to allocate cipher ctx dma\n");
  169. rc = -ENOMEM;
  170. goto err_engine_crypto_start;
  171. }
  172. /* Allocate DMA buffer for crypto engine input used */
  173. crypto_engine->cipher_addr =
  174. dmam_alloc_coherent(&pdev->dev,
  175. ASPEED_CRYPTO_SRC_DMA_BUF_LEN,
  176. &crypto_engine->cipher_dma_addr,
  177. GFP_KERNEL);
  178. if (!crypto_engine->cipher_addr) {
  179. dev_err(&pdev->dev, "Failed to allocate cipher addr dma\n");
  180. rc = -ENOMEM;
  181. goto err_engine_crypto_start;
  182. }
  183. /* Allocate DMA buffer for crypto engine output used */
  184. if (hace_dev->version == AST2600_VERSION) {
  185. crypto_engine->dst_sg_addr =
  186. dmam_alloc_coherent(&pdev->dev,
  187. ASPEED_CRYPTO_DST_DMA_BUF_LEN,
  188. &crypto_engine->dst_sg_dma_addr,
  189. GFP_KERNEL);
  190. if (!crypto_engine->dst_sg_addr) {
  191. dev_err(&pdev->dev, "Failed to allocate dst_sg dma\n");
  192. rc = -ENOMEM;
  193. goto err_engine_crypto_start;
  194. }
  195. }
  196. aspeed_hace_register(hace_dev);
  197. dev_info(&pdev->dev, "Aspeed Crypto Accelerator successfully registered\n");
  198. return 0;
  199. err_engine_crypto_start:
  200. crypto_engine_exit(hace_dev->crypt_engine_crypto);
  201. err_engine_hash_start:
  202. crypto_engine_exit(hace_dev->crypt_engine_hash);
  203. clk_exit:
  204. clk_disable_unprepare(hace_dev->clk);
  205. return rc;
  206. }
  207. static void aspeed_hace_remove(struct platform_device *pdev)
  208. {
  209. struct aspeed_hace_dev *hace_dev = platform_get_drvdata(pdev);
  210. struct aspeed_engine_crypto *crypto_engine = &hace_dev->crypto_engine;
  211. struct aspeed_engine_hash *hash_engine = &hace_dev->hash_engine;
  212. aspeed_hace_unregister(hace_dev);
  213. crypto_engine_exit(hace_dev->crypt_engine_hash);
  214. crypto_engine_exit(hace_dev->crypt_engine_crypto);
  215. tasklet_kill(&hash_engine->done_task);
  216. tasklet_kill(&crypto_engine->done_task);
  217. clk_disable_unprepare(hace_dev->clk);
  218. }
  219. MODULE_DEVICE_TABLE(of, aspeed_hace_of_matches);
  220. static struct platform_driver aspeed_hace_driver = {
  221. .probe = aspeed_hace_probe,
  222. .remove = aspeed_hace_remove,
  223. .driver = {
  224. .name = KBUILD_MODNAME,
  225. .of_match_table = aspeed_hace_of_matches,
  226. },
  227. };
  228. module_platform_driver(aspeed_hace_driver);
  229. MODULE_AUTHOR("Neal Liu <neal_liu@aspeedtech.com>");
  230. MODULE_DESCRIPTION("Aspeed HACE driver Crypto Accelerator");
  231. MODULE_LICENSE("GPL");