stm32_crc32.c 8.1 KB

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