stm32_crc32.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. /*
  2. * Copyright (C) STMicroelectronics SA 2017
  3. * Author: Fabien Dessenne <fabien.dessenne@st.com>
  4. * License terms: GNU General Public License (GPL), version 2
  5. */
  6. #include <linux/bitrev.h>
  7. #include <linux/clk.h>
  8. #include <linux/module.h>
  9. #include <linux/platform_device.h>
  10. #include <linux/pm_runtime.h>
  11. #include <crypto/internal/hash.h>
  12. #include <asm/unaligned.h>
  13. #define DRIVER_NAME "stm32-crc32"
  14. #define CHKSUM_DIGEST_SIZE 4
  15. #define CHKSUM_BLOCK_SIZE 1
  16. /* Registers */
  17. #define CRC_DR 0x00000000
  18. #define CRC_CR 0x00000008
  19. #define CRC_INIT 0x00000010
  20. #define CRC_POL 0x00000014
  21. /* Registers values */
  22. #define CRC_CR_RESET BIT(0)
  23. #define CRC_CR_REVERSE (BIT(7) | BIT(6) | BIT(5))
  24. #define CRC_INIT_DEFAULT 0xFFFFFFFF
  25. /* Polynomial reversed */
  26. #define POLY_CRC32 0xEDB88320
  27. #define POLY_CRC32C 0x82F63B78
  28. #define CRC_AUTOSUSPEND_DELAY 50
  29. struct stm32_crc {
  30. struct list_head list;
  31. struct device *dev;
  32. void __iomem *regs;
  33. struct clk *clk;
  34. u8 pending_data[sizeof(u32)];
  35. size_t nb_pending_bytes;
  36. };
  37. struct stm32_crc_list {
  38. struct list_head dev_list;
  39. spinlock_t lock; /* protect dev_list */
  40. };
  41. static struct stm32_crc_list crc_list = {
  42. .dev_list = LIST_HEAD_INIT(crc_list.dev_list),
  43. .lock = __SPIN_LOCK_UNLOCKED(crc_list.lock),
  44. };
  45. struct stm32_crc_ctx {
  46. u32 key;
  47. u32 poly;
  48. };
  49. struct stm32_crc_desc_ctx {
  50. u32 partial; /* crc32c: partial in first 4 bytes of that struct */
  51. struct stm32_crc *crc;
  52. };
  53. static int stm32_crc32_cra_init(struct crypto_tfm *tfm)
  54. {
  55. struct stm32_crc_ctx *mctx = crypto_tfm_ctx(tfm);
  56. mctx->key = CRC_INIT_DEFAULT;
  57. mctx->poly = POLY_CRC32;
  58. return 0;
  59. }
  60. static int stm32_crc32c_cra_init(struct crypto_tfm *tfm)
  61. {
  62. struct stm32_crc_ctx *mctx = crypto_tfm_ctx(tfm);
  63. mctx->key = CRC_INIT_DEFAULT;
  64. mctx->poly = POLY_CRC32C;
  65. return 0;
  66. }
  67. static int stm32_crc_setkey(struct crypto_shash *tfm, const u8 *key,
  68. unsigned int keylen)
  69. {
  70. struct stm32_crc_ctx *mctx = crypto_shash_ctx(tfm);
  71. if (keylen != sizeof(u32)) {
  72. crypto_shash_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
  73. return -EINVAL;
  74. }
  75. mctx->key = get_unaligned_le32(key);
  76. return 0;
  77. }
  78. static int stm32_crc_init(struct shash_desc *desc)
  79. {
  80. struct stm32_crc_desc_ctx *ctx = shash_desc_ctx(desc);
  81. struct stm32_crc_ctx *mctx = crypto_shash_ctx(desc->tfm);
  82. struct stm32_crc *crc;
  83. spin_lock_bh(&crc_list.lock);
  84. list_for_each_entry(crc, &crc_list.dev_list, list) {
  85. ctx->crc = crc;
  86. break;
  87. }
  88. spin_unlock_bh(&crc_list.lock);
  89. pm_runtime_get_sync(ctx->crc->dev);
  90. /* Reset, set key, poly and configure in bit reverse mode */
  91. writel_relaxed(bitrev32(mctx->key), ctx->crc->regs + CRC_INIT);
  92. writel_relaxed(bitrev32(mctx->poly), ctx->crc->regs + CRC_POL);
  93. writel_relaxed(CRC_CR_RESET | CRC_CR_REVERSE, ctx->crc->regs + CRC_CR);
  94. /* Store partial result */
  95. ctx->partial = readl_relaxed(ctx->crc->regs + CRC_DR);
  96. ctx->crc->nb_pending_bytes = 0;
  97. pm_runtime_mark_last_busy(ctx->crc->dev);
  98. pm_runtime_put_autosuspend(ctx->crc->dev);
  99. return 0;
  100. }
  101. static int stm32_crc_update(struct shash_desc *desc, const u8 *d8,
  102. unsigned int length)
  103. {
  104. struct stm32_crc_desc_ctx *ctx = shash_desc_ctx(desc);
  105. struct stm32_crc *crc = ctx->crc;
  106. u32 *d32;
  107. unsigned int i;
  108. pm_runtime_get_sync(crc->dev);
  109. if (unlikely(crc->nb_pending_bytes)) {
  110. while (crc->nb_pending_bytes != sizeof(u32) && length) {
  111. /* Fill in pending data */
  112. crc->pending_data[crc->nb_pending_bytes++] = *(d8++);
  113. length--;
  114. }
  115. if (crc->nb_pending_bytes == sizeof(u32)) {
  116. /* Process completed pending data */
  117. writel_relaxed(*(u32 *)crc->pending_data,
  118. crc->regs + CRC_DR);
  119. crc->nb_pending_bytes = 0;
  120. }
  121. }
  122. d32 = (u32 *)d8;
  123. for (i = 0; i < length >> 2; i++)
  124. /* Process 32 bits data */
  125. writel_relaxed(*(d32++), crc->regs + CRC_DR);
  126. /* Store partial result */
  127. ctx->partial = readl_relaxed(crc->regs + CRC_DR);
  128. pm_runtime_mark_last_busy(crc->dev);
  129. pm_runtime_put_autosuspend(crc->dev);
  130. /* Check for pending data (non 32 bits) */
  131. length &= 3;
  132. if (likely(!length))
  133. return 0;
  134. if ((crc->nb_pending_bytes + length) >= sizeof(u32)) {
  135. /* Shall not happen */
  136. dev_err(crc->dev, "Pending data overflow\n");
  137. return -EINVAL;
  138. }
  139. d8 = (const u8 *)d32;
  140. for (i = 0; i < length; i++)
  141. /* Store pending data */
  142. crc->pending_data[crc->nb_pending_bytes++] = *(d8++);
  143. return 0;
  144. }
  145. static int stm32_crc_final(struct shash_desc *desc, u8 *out)
  146. {
  147. struct stm32_crc_desc_ctx *ctx = shash_desc_ctx(desc);
  148. struct stm32_crc_ctx *mctx = crypto_shash_ctx(desc->tfm);
  149. /* Send computed CRC */
  150. put_unaligned_le32(mctx->poly == POLY_CRC32C ?
  151. ~ctx->partial : ctx->partial, out);
  152. return 0;
  153. }
  154. static int stm32_crc_finup(struct shash_desc *desc, const u8 *data,
  155. unsigned int length, u8 *out)
  156. {
  157. return stm32_crc_update(desc, data, length) ?:
  158. stm32_crc_final(desc, out);
  159. }
  160. static int stm32_crc_digest(struct shash_desc *desc, const u8 *data,
  161. unsigned int length, u8 *out)
  162. {
  163. return stm32_crc_init(desc) ?: stm32_crc_finup(desc, data, length, out);
  164. }
  165. static struct shash_alg algs[] = {
  166. /* CRC-32 */
  167. {
  168. .setkey = stm32_crc_setkey,
  169. .init = stm32_crc_init,
  170. .update = stm32_crc_update,
  171. .final = stm32_crc_final,
  172. .finup = stm32_crc_finup,
  173. .digest = stm32_crc_digest,
  174. .descsize = sizeof(struct stm32_crc_desc_ctx),
  175. .digestsize = CHKSUM_DIGEST_SIZE,
  176. .base = {
  177. .cra_name = "crc32",
  178. .cra_driver_name = DRIVER_NAME,
  179. .cra_priority = 200,
  180. .cra_flags = CRYPTO_ALG_OPTIONAL_KEY,
  181. .cra_blocksize = CHKSUM_BLOCK_SIZE,
  182. .cra_alignmask = 3,
  183. .cra_ctxsize = sizeof(struct stm32_crc_ctx),
  184. .cra_module = THIS_MODULE,
  185. .cra_init = stm32_crc32_cra_init,
  186. }
  187. },
  188. /* CRC-32Castagnoli */
  189. {
  190. .setkey = stm32_crc_setkey,
  191. .init = stm32_crc_init,
  192. .update = stm32_crc_update,
  193. .final = stm32_crc_final,
  194. .finup = stm32_crc_finup,
  195. .digest = stm32_crc_digest,
  196. .descsize = sizeof(struct stm32_crc_desc_ctx),
  197. .digestsize = CHKSUM_DIGEST_SIZE,
  198. .base = {
  199. .cra_name = "crc32c",
  200. .cra_driver_name = DRIVER_NAME,
  201. .cra_priority = 200,
  202. .cra_flags = CRYPTO_ALG_OPTIONAL_KEY,
  203. .cra_blocksize = CHKSUM_BLOCK_SIZE,
  204. .cra_alignmask = 3,
  205. .cra_ctxsize = sizeof(struct stm32_crc_ctx),
  206. .cra_module = THIS_MODULE,
  207. .cra_init = stm32_crc32c_cra_init,
  208. }
  209. }
  210. };
  211. static int stm32_crc_probe(struct platform_device *pdev)
  212. {
  213. struct device *dev = &pdev->dev;
  214. struct stm32_crc *crc;
  215. struct resource *res;
  216. int ret;
  217. crc = devm_kzalloc(dev, sizeof(*crc), GFP_KERNEL);
  218. if (!crc)
  219. return -ENOMEM;
  220. crc->dev = dev;
  221. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  222. crc->regs = devm_ioremap_resource(dev, res);
  223. if (IS_ERR(crc->regs)) {
  224. dev_err(dev, "Cannot map CRC IO\n");
  225. return PTR_ERR(crc->regs);
  226. }
  227. crc->clk = devm_clk_get(dev, NULL);
  228. if (IS_ERR(crc->clk)) {
  229. dev_err(dev, "Could not get clock\n");
  230. return PTR_ERR(crc->clk);
  231. }
  232. ret = clk_prepare_enable(crc->clk);
  233. if (ret) {
  234. dev_err(crc->dev, "Failed to enable clock\n");
  235. return ret;
  236. }
  237. pm_runtime_set_autosuspend_delay(dev, CRC_AUTOSUSPEND_DELAY);
  238. pm_runtime_use_autosuspend(dev);
  239. pm_runtime_get_noresume(dev);
  240. pm_runtime_set_active(dev);
  241. pm_runtime_enable(dev);
  242. platform_set_drvdata(pdev, crc);
  243. spin_lock(&crc_list.lock);
  244. list_add(&crc->list, &crc_list.dev_list);
  245. spin_unlock(&crc_list.lock);
  246. ret = crypto_register_shashes(algs, ARRAY_SIZE(algs));
  247. if (ret) {
  248. dev_err(dev, "Failed to register\n");
  249. clk_disable_unprepare(crc->clk);
  250. return ret;
  251. }
  252. dev_info(dev, "Initialized\n");
  253. pm_runtime_put_sync(dev);
  254. return 0;
  255. }
  256. static int stm32_crc_remove(struct platform_device *pdev)
  257. {
  258. struct stm32_crc *crc = platform_get_drvdata(pdev);
  259. int ret = pm_runtime_get_sync(crc->dev);
  260. if (ret < 0)
  261. return ret;
  262. spin_lock(&crc_list.lock);
  263. list_del(&crc->list);
  264. spin_unlock(&crc_list.lock);
  265. crypto_unregister_shashes(algs, ARRAY_SIZE(algs));
  266. pm_runtime_disable(crc->dev);
  267. pm_runtime_put_noidle(crc->dev);
  268. clk_disable_unprepare(crc->clk);
  269. return 0;
  270. }
  271. #ifdef CONFIG_PM
  272. static int stm32_crc_runtime_suspend(struct device *dev)
  273. {
  274. struct stm32_crc *crc = dev_get_drvdata(dev);
  275. clk_disable_unprepare(crc->clk);
  276. return 0;
  277. }
  278. static int stm32_crc_runtime_resume(struct device *dev)
  279. {
  280. struct stm32_crc *crc = dev_get_drvdata(dev);
  281. int ret;
  282. ret = clk_prepare_enable(crc->clk);
  283. if (ret) {
  284. dev_err(crc->dev, "Failed to prepare_enable clock\n");
  285. return ret;
  286. }
  287. return 0;
  288. }
  289. #endif
  290. static const struct dev_pm_ops stm32_crc_pm_ops = {
  291. SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
  292. pm_runtime_force_resume)
  293. SET_RUNTIME_PM_OPS(stm32_crc_runtime_suspend,
  294. stm32_crc_runtime_resume, NULL)
  295. };
  296. static const struct of_device_id stm32_dt_ids[] = {
  297. { .compatible = "st,stm32f7-crc", },
  298. {},
  299. };
  300. MODULE_DEVICE_TABLE(of, stm32_dt_ids);
  301. static struct platform_driver stm32_crc_driver = {
  302. .probe = stm32_crc_probe,
  303. .remove = stm32_crc_remove,
  304. .driver = {
  305. .name = DRIVER_NAME,
  306. .pm = &stm32_crc_pm_ops,
  307. .of_match_table = stm32_dt_ids,
  308. },
  309. };
  310. module_platform_driver(stm32_crc_driver);
  311. MODULE_AUTHOR("Fabien Dessenne <fabien.dessenne@st.com>");
  312. MODULE_DESCRIPTION("STMicrolectronics STM32 CRC32 hardware driver");
  313. MODULE_LICENSE("GPL");