aspeed-hace.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (c) 2021 Aspeed Technology Inc.
  4. */
  5. #include <linux/clk.h>
  6. #include <linux/module.h>
  7. #include <linux/of_address.h>
  8. #include <linux/of_device.h>
  9. #include <linux/of_irq.h>
  10. #include <linux/of.h>
  11. #include <linux/platform_device.h>
  12. #include "aspeed-hace.h"
  13. #ifdef CONFIG_CRYPTO_DEV_ASPEED_DEBUG
  14. #define HACE_DBG(d, fmt, ...) \
  15. dev_info((d)->dev, "%s() " fmt, __func__, ##__VA_ARGS__)
  16. #else
  17. #define HACE_DBG(d, fmt, ...) \
  18. dev_dbg((d)->dev, "%s() " fmt, __func__, ##__VA_ARGS__)
  19. #endif
  20. /* HACE interrupt service routine */
  21. static irqreturn_t aspeed_hace_irq(int irq, void *dev)
  22. {
  23. struct aspeed_hace_dev *hace_dev = (struct aspeed_hace_dev *)dev;
  24. struct aspeed_engine_crypto *crypto_engine = &hace_dev->crypto_engine;
  25. struct aspeed_engine_hash *hash_engine = &hace_dev->hash_engine;
  26. u32 sts;
  27. sts = ast_hace_read(hace_dev, ASPEED_HACE_STS);
  28. ast_hace_write(hace_dev, sts, ASPEED_HACE_STS);
  29. HACE_DBG(hace_dev, "irq status: 0x%x\n", sts);
  30. if (sts & HACE_HASH_ISR) {
  31. if (hash_engine->flags & CRYPTO_FLAGS_BUSY)
  32. tasklet_schedule(&hash_engine->done_task);
  33. else
  34. dev_warn(hace_dev->dev, "HASH no active requests.\n");
  35. }
  36. if (sts & HACE_CRYPTO_ISR) {
  37. if (crypto_engine->flags & CRYPTO_FLAGS_BUSY)
  38. tasklet_schedule(&crypto_engine->done_task);
  39. else
  40. dev_warn(hace_dev->dev, "CRYPTO no active requests.\n");
  41. }
  42. return IRQ_HANDLED;
  43. }
  44. static void aspeed_hace_crypto_done_task(unsigned long data)
  45. {
  46. struct aspeed_hace_dev *hace_dev = (struct aspeed_hace_dev *)data;
  47. struct aspeed_engine_crypto *crypto_engine = &hace_dev->crypto_engine;
  48. crypto_engine->resume(hace_dev);
  49. }
  50. static void aspeed_hace_hash_done_task(unsigned long data)
  51. {
  52. struct aspeed_hace_dev *hace_dev = (struct aspeed_hace_dev *)data;
  53. struct aspeed_engine_hash *hash_engine = &hace_dev->hash_engine;
  54. hash_engine->resume(hace_dev);
  55. }
  56. static void aspeed_hace_register(struct aspeed_hace_dev *hace_dev)
  57. {
  58. #ifdef CONFIG_CRYPTO_DEV_ASPEED_HACE_HASH
  59. aspeed_register_hace_hash_algs(hace_dev);
  60. #endif
  61. #ifdef CONFIG_CRYPTO_DEV_ASPEED_HACE_CRYPTO
  62. aspeed_register_hace_crypto_algs(hace_dev);
  63. #endif
  64. }
  65. static void aspeed_hace_unregister(struct aspeed_hace_dev *hace_dev)
  66. {
  67. #ifdef CONFIG_CRYPTO_DEV_ASPEED_HACE_HASH
  68. aspeed_unregister_hace_hash_algs(hace_dev);
  69. #endif
  70. #ifdef CONFIG_CRYPTO_DEV_ASPEED_HACE_CRYPTO
  71. aspeed_unregister_hace_crypto_algs(hace_dev);
  72. #endif
  73. }
  74. static const struct of_device_id aspeed_hace_of_matches[] = {
  75. { .compatible = "aspeed,ast2500-hace", .data = (void *)5, },
  76. { .compatible = "aspeed,ast2600-hace", .data = (void *)6, },
  77. {},
  78. };
  79. static int aspeed_hace_probe(struct platform_device *pdev)
  80. {
  81. struct aspeed_engine_crypto *crypto_engine;
  82. const struct of_device_id *hace_dev_id;
  83. struct aspeed_engine_hash *hash_engine;
  84. struct aspeed_hace_dev *hace_dev;
  85. struct resource *res;
  86. int rc;
  87. hace_dev = devm_kzalloc(&pdev->dev, sizeof(struct aspeed_hace_dev),
  88. GFP_KERNEL);
  89. if (!hace_dev)
  90. return -ENOMEM;
  91. hace_dev_id = of_match_device(aspeed_hace_of_matches, &pdev->dev);
  92. if (!hace_dev_id) {
  93. dev_err(&pdev->dev, "Failed to match hace dev id\n");
  94. return -EINVAL;
  95. }
  96. hace_dev->dev = &pdev->dev;
  97. hace_dev->version = (unsigned long)hace_dev_id->data;
  98. hash_engine = &hace_dev->hash_engine;
  99. crypto_engine = &hace_dev->crypto_engine;
  100. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  101. platform_set_drvdata(pdev, hace_dev);
  102. hace_dev->regs = devm_ioremap_resource(&pdev->dev, res);
  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 int 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. return 0;
  219. }
  220. MODULE_DEVICE_TABLE(of, aspeed_hace_of_matches);
  221. static struct platform_driver aspeed_hace_driver = {
  222. .probe = aspeed_hace_probe,
  223. .remove = aspeed_hace_remove,
  224. .driver = {
  225. .name = KBUILD_MODNAME,
  226. .of_match_table = aspeed_hace_of_matches,
  227. },
  228. };
  229. module_platform_driver(aspeed_hace_driver);
  230. MODULE_AUTHOR("Neal Liu <[email protected]>");
  231. MODULE_DESCRIPTION("Aspeed HACE driver Crypto Accelerator");
  232. MODULE_LICENSE("GPL");