stm32_crc32.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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_REV_IN_WORD (BIT(6) | BIT(5))
  26. #define CRC_CR_REV_IN_BYTE BIT(5)
  27. #define CRC_CR_REV_OUT BIT(7)
  28. #define CRC32C_INIT_DEFAULT 0xFFFFFFFF
  29. #define CRC_AUTOSUSPEND_DELAY 50
  30. struct stm32_crc {
  31. struct list_head list;
  32. struct device *dev;
  33. void __iomem *regs;
  34. struct clk *clk;
  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. };
  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 = 0;
  55. mctx->poly = CRC32_POLY_LE;
  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 = CRC32C_INIT_DEFAULT;
  62. mctx->poly = CRC32C_POLY_LE;
  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 struct stm32_crc *stm32_crc_get_next_crc(void)
  77. {
  78. struct stm32_crc *crc;
  79. spin_lock_bh(&crc_list.lock);
  80. crc = list_first_entry(&crc_list.dev_list, struct stm32_crc, list);
  81. if (crc)
  82. list_move_tail(&crc->list, &crc_list.dev_list);
  83. spin_unlock_bh(&crc_list.lock);
  84. return crc;
  85. }
  86. static int stm32_crc_init(struct shash_desc *desc)
  87. {
  88. struct stm32_crc_desc_ctx *ctx = shash_desc_ctx(desc);
  89. struct stm32_crc_ctx *mctx = crypto_shash_ctx(desc->tfm);
  90. struct stm32_crc *crc;
  91. crc = stm32_crc_get_next_crc();
  92. if (!crc)
  93. return -ENODEV;
  94. pm_runtime_get_sync(crc->dev);
  95. /* Reset, set key, poly and configure in bit reverse mode */
  96. writel_relaxed(bitrev32(mctx->key), crc->regs + CRC_INIT);
  97. writel_relaxed(bitrev32(mctx->poly), crc->regs + CRC_POL);
  98. writel_relaxed(CRC_CR_RESET | CRC_CR_REV_IN_WORD | CRC_CR_REV_OUT,
  99. crc->regs + CRC_CR);
  100. /* Store partial result */
  101. ctx->partial = readl_relaxed(crc->regs + CRC_DR);
  102. pm_runtime_mark_last_busy(crc->dev);
  103. pm_runtime_put_autosuspend(crc->dev);
  104. return 0;
  105. }
  106. static int stm32_crc_update(struct shash_desc *desc, const u8 *d8,
  107. unsigned int length)
  108. {
  109. struct stm32_crc_desc_ctx *ctx = shash_desc_ctx(desc);
  110. struct stm32_crc_ctx *mctx = crypto_shash_ctx(desc->tfm);
  111. struct stm32_crc *crc;
  112. crc = stm32_crc_get_next_crc();
  113. if (!crc)
  114. return -ENODEV;
  115. pm_runtime_get_sync(crc->dev);
  116. /*
  117. * Restore previously calculated CRC for this context as init value
  118. * Restore polynomial configuration
  119. * Configure in register for word input data,
  120. * Configure out register in reversed bit mode data.
  121. */
  122. writel_relaxed(bitrev32(ctx->partial), crc->regs + CRC_INIT);
  123. writel_relaxed(bitrev32(mctx->poly), crc->regs + CRC_POL);
  124. writel_relaxed(CRC_CR_RESET | CRC_CR_REV_IN_WORD | CRC_CR_REV_OUT,
  125. crc->regs + CRC_CR);
  126. if (d8 != PTR_ALIGN(d8, sizeof(u32))) {
  127. /* Configure for byte data */
  128. writel_relaxed(CRC_CR_REV_IN_BYTE | CRC_CR_REV_OUT,
  129. crc->regs + CRC_CR);
  130. while (d8 != PTR_ALIGN(d8, sizeof(u32)) && length) {
  131. writeb_relaxed(*d8++, crc->regs + CRC_DR);
  132. length--;
  133. }
  134. /* Configure for word data */
  135. writel_relaxed(CRC_CR_REV_IN_WORD | CRC_CR_REV_OUT,
  136. crc->regs + CRC_CR);
  137. }
  138. for (; length >= sizeof(u32); d8 += sizeof(u32), length -= sizeof(u32))
  139. writel_relaxed(*((u32 *)d8), crc->regs + CRC_DR);
  140. if (length) {
  141. /* Configure for byte data */
  142. writel_relaxed(CRC_CR_REV_IN_BYTE | CRC_CR_REV_OUT,
  143. crc->regs + CRC_CR);
  144. while (length--)
  145. writeb_relaxed(*d8++, crc->regs + CRC_DR);
  146. }
  147. /* Store partial result */
  148. ctx->partial = readl_relaxed(crc->regs + CRC_DR);
  149. pm_runtime_mark_last_busy(crc->dev);
  150. pm_runtime_put_autosuspend(crc->dev);
  151. return 0;
  152. }
  153. static int stm32_crc_final(struct shash_desc *desc, u8 *out)
  154. {
  155. struct stm32_crc_desc_ctx *ctx = shash_desc_ctx(desc);
  156. struct stm32_crc_ctx *mctx = crypto_shash_ctx(desc->tfm);
  157. /* Send computed CRC */
  158. put_unaligned_le32(mctx->poly == CRC32C_POLY_LE ?
  159. ~ctx->partial : ctx->partial, out);
  160. return 0;
  161. }
  162. static int stm32_crc_finup(struct shash_desc *desc, const u8 *data,
  163. unsigned int length, u8 *out)
  164. {
  165. return stm32_crc_update(desc, data, length) ?:
  166. stm32_crc_final(desc, out);
  167. }
  168. static int stm32_crc_digest(struct shash_desc *desc, const u8 *data,
  169. unsigned int length, u8 *out)
  170. {
  171. return stm32_crc_init(desc) ?: stm32_crc_finup(desc, data, length, out);
  172. }
  173. static unsigned int refcnt;
  174. static DEFINE_MUTEX(refcnt_lock);
  175. static struct shash_alg algs[] = {
  176. /* CRC-32 */
  177. {
  178. .setkey = stm32_crc_setkey,
  179. .init = stm32_crc_init,
  180. .update = stm32_crc_update,
  181. .final = stm32_crc_final,
  182. .finup = stm32_crc_finup,
  183. .digest = stm32_crc_digest,
  184. .descsize = sizeof(struct stm32_crc_desc_ctx),
  185. .digestsize = CHKSUM_DIGEST_SIZE,
  186. .base = {
  187. .cra_name = "crc32",
  188. .cra_driver_name = DRIVER_NAME,
  189. .cra_priority = 200,
  190. .cra_flags = CRYPTO_ALG_OPTIONAL_KEY,
  191. .cra_blocksize = CHKSUM_BLOCK_SIZE,
  192. .cra_alignmask = 3,
  193. .cra_ctxsize = sizeof(struct stm32_crc_ctx),
  194. .cra_module = THIS_MODULE,
  195. .cra_init = stm32_crc32_cra_init,
  196. }
  197. },
  198. /* CRC-32Castagnoli */
  199. {
  200. .setkey = stm32_crc_setkey,
  201. .init = stm32_crc_init,
  202. .update = stm32_crc_update,
  203. .final = stm32_crc_final,
  204. .finup = stm32_crc_finup,
  205. .digest = stm32_crc_digest,
  206. .descsize = sizeof(struct stm32_crc_desc_ctx),
  207. .digestsize = CHKSUM_DIGEST_SIZE,
  208. .base = {
  209. .cra_name = "crc32c",
  210. .cra_driver_name = DRIVER_NAME,
  211. .cra_priority = 200,
  212. .cra_flags = CRYPTO_ALG_OPTIONAL_KEY,
  213. .cra_blocksize = CHKSUM_BLOCK_SIZE,
  214. .cra_alignmask = 3,
  215. .cra_ctxsize = sizeof(struct stm32_crc_ctx),
  216. .cra_module = THIS_MODULE,
  217. .cra_init = stm32_crc32c_cra_init,
  218. }
  219. }
  220. };
  221. static int stm32_crc_probe(struct platform_device *pdev)
  222. {
  223. struct device *dev = &pdev->dev;
  224. struct stm32_crc *crc;
  225. struct resource *res;
  226. int ret;
  227. crc = devm_kzalloc(dev, sizeof(*crc), GFP_KERNEL);
  228. if (!crc)
  229. return -ENOMEM;
  230. crc->dev = dev;
  231. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  232. crc->regs = devm_ioremap_resource(dev, res);
  233. if (IS_ERR(crc->regs)) {
  234. dev_err(dev, "Cannot map CRC IO\n");
  235. return PTR_ERR(crc->regs);
  236. }
  237. crc->clk = devm_clk_get(dev, NULL);
  238. if (IS_ERR(crc->clk)) {
  239. dev_err(dev, "Could not get clock\n");
  240. return PTR_ERR(crc->clk);
  241. }
  242. ret = clk_prepare_enable(crc->clk);
  243. if (ret) {
  244. dev_err(crc->dev, "Failed to enable clock\n");
  245. return ret;
  246. }
  247. pm_runtime_set_autosuspend_delay(dev, CRC_AUTOSUSPEND_DELAY);
  248. pm_runtime_use_autosuspend(dev);
  249. pm_runtime_get_noresume(dev);
  250. pm_runtime_set_active(dev);
  251. pm_runtime_enable(dev);
  252. platform_set_drvdata(pdev, crc);
  253. spin_lock(&crc_list.lock);
  254. list_add(&crc->list, &crc_list.dev_list);
  255. spin_unlock(&crc_list.lock);
  256. mutex_lock(&refcnt_lock);
  257. if (!refcnt) {
  258. ret = crypto_register_shashes(algs, ARRAY_SIZE(algs));
  259. if (ret) {
  260. mutex_unlock(&refcnt_lock);
  261. dev_err(dev, "Failed to register\n");
  262. clk_disable_unprepare(crc->clk);
  263. return ret;
  264. }
  265. }
  266. refcnt++;
  267. mutex_unlock(&refcnt_lock);
  268. dev_info(dev, "Initialized\n");
  269. pm_runtime_put_sync(dev);
  270. return 0;
  271. }
  272. static int stm32_crc_remove(struct platform_device *pdev)
  273. {
  274. struct stm32_crc *crc = platform_get_drvdata(pdev);
  275. int ret = pm_runtime_get_sync(crc->dev);
  276. if (ret < 0)
  277. return ret;
  278. spin_lock(&crc_list.lock);
  279. list_del(&crc->list);
  280. spin_unlock(&crc_list.lock);
  281. mutex_lock(&refcnt_lock);
  282. if (!--refcnt)
  283. crypto_unregister_shashes(algs, ARRAY_SIZE(algs));
  284. mutex_unlock(&refcnt_lock);
  285. pm_runtime_disable(crc->dev);
  286. pm_runtime_put_noidle(crc->dev);
  287. clk_disable_unprepare(crc->clk);
  288. return 0;
  289. }
  290. #ifdef CONFIG_PM
  291. static int stm32_crc_runtime_suspend(struct device *dev)
  292. {
  293. struct stm32_crc *crc = dev_get_drvdata(dev);
  294. clk_disable_unprepare(crc->clk);
  295. return 0;
  296. }
  297. static int stm32_crc_runtime_resume(struct device *dev)
  298. {
  299. struct stm32_crc *crc = dev_get_drvdata(dev);
  300. int ret;
  301. ret = clk_prepare_enable(crc->clk);
  302. if (ret) {
  303. dev_err(crc->dev, "Failed to prepare_enable clock\n");
  304. return ret;
  305. }
  306. return 0;
  307. }
  308. #endif
  309. static const struct dev_pm_ops stm32_crc_pm_ops = {
  310. SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
  311. pm_runtime_force_resume)
  312. SET_RUNTIME_PM_OPS(stm32_crc_runtime_suspend,
  313. stm32_crc_runtime_resume, NULL)
  314. };
  315. static const struct of_device_id stm32_dt_ids[] = {
  316. { .compatible = "st,stm32f7-crc", },
  317. {},
  318. };
  319. MODULE_DEVICE_TABLE(of, stm32_dt_ids);
  320. static struct platform_driver stm32_crc_driver = {
  321. .probe = stm32_crc_probe,
  322. .remove = stm32_crc_remove,
  323. .driver = {
  324. .name = DRIVER_NAME,
  325. .pm = &stm32_crc_pm_ops,
  326. .of_match_table = stm32_dt_ids,
  327. },
  328. };
  329. module_platform_driver(stm32_crc_driver);
  330. MODULE_AUTHOR("Fabien Dessenne <fabien.dessenne@st.com>");
  331. MODULE_DESCRIPTION("STMicrolectronics STM32 CRC32 hardware driver");
  332. MODULE_LICENSE("GPL");