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