stm32_crc32.c 9.4 KB

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