2 * Copyright (C) STMicroelectronics SA 2017
3 * Author: Fabien Dessenne <fabien.dessenne@st.com>
4 * License terms: GNU General Public License (GPL), version 2
7 #include <linux/bitrev.h>
9 #include <linux/crc32poly.h>
10 #include <linux/module.h>
11 #include <linux/platform_device.h>
12 #include <linux/pm_runtime.h>
14 #include <crypto/internal/hash.h>
16 #include <asm/unaligned.h>
18 #define DRIVER_NAME "stm32-crc32"
19 #define CHKSUM_DIGEST_SIZE 4
20 #define CHKSUM_BLOCK_SIZE 1
23 #define CRC_DR 0x00000000
24 #define CRC_CR 0x00000008
25 #define CRC_INIT 0x00000010
26 #define CRC_POL 0x00000014
28 /* Registers values */
29 #define CRC_CR_RESET BIT(0)
30 #define CRC_CR_REVERSE (BIT(7) | BIT(6) | BIT(5))
31 #define CRC_INIT_DEFAULT 0xFFFFFFFF
33 #define CRC_AUTOSUSPEND_DELAY 50
36 struct list_head list;
40 u8 pending_data[sizeof(u32)];
41 size_t nb_pending_bytes;
44 struct stm32_crc_list {
45 struct list_head dev_list;
46 spinlock_t lock; /* protect dev_list */
49 static struct stm32_crc_list crc_list = {
50 .dev_list = LIST_HEAD_INIT(crc_list.dev_list),
51 .lock = __SPIN_LOCK_UNLOCKED(crc_list.lock),
54 struct stm32_crc_ctx {
59 struct stm32_crc_desc_ctx {
60 u32 partial; /* crc32c: partial in first 4 bytes of that struct */
61 struct stm32_crc *crc;
64 static int stm32_crc32_cra_init(struct crypto_tfm *tfm)
66 struct stm32_crc_ctx *mctx = crypto_tfm_ctx(tfm);
68 mctx->key = CRC_INIT_DEFAULT;
69 mctx->poly = CRC32_POLY_LE;
73 static int stm32_crc32c_cra_init(struct crypto_tfm *tfm)
75 struct stm32_crc_ctx *mctx = crypto_tfm_ctx(tfm);
77 mctx->key = CRC_INIT_DEFAULT;
78 mctx->poly = CRC32C_POLY_LE;
82 static int stm32_crc_setkey(struct crypto_shash *tfm, const u8 *key,
85 struct stm32_crc_ctx *mctx = crypto_shash_ctx(tfm);
87 if (keylen != sizeof(u32)) {
88 crypto_shash_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
92 mctx->key = get_unaligned_le32(key);
96 static int stm32_crc_init(struct shash_desc *desc)
98 struct stm32_crc_desc_ctx *ctx = shash_desc_ctx(desc);
99 struct stm32_crc_ctx *mctx = crypto_shash_ctx(desc->tfm);
100 struct stm32_crc *crc;
102 spin_lock_bh(&crc_list.lock);
103 list_for_each_entry(crc, &crc_list.dev_list, list) {
107 spin_unlock_bh(&crc_list.lock);
109 pm_runtime_get_sync(ctx->crc->dev);
111 /* Reset, set key, poly and configure in bit reverse mode */
112 writel_relaxed(bitrev32(mctx->key), ctx->crc->regs + CRC_INIT);
113 writel_relaxed(bitrev32(mctx->poly), ctx->crc->regs + CRC_POL);
114 writel_relaxed(CRC_CR_RESET | CRC_CR_REVERSE, ctx->crc->regs + CRC_CR);
116 /* Store partial result */
117 ctx->partial = readl_relaxed(ctx->crc->regs + CRC_DR);
118 ctx->crc->nb_pending_bytes = 0;
120 pm_runtime_mark_last_busy(ctx->crc->dev);
121 pm_runtime_put_autosuspend(ctx->crc->dev);
126 static int stm32_crc_update(struct shash_desc *desc, const u8 *d8,
129 struct stm32_crc_desc_ctx *ctx = shash_desc_ctx(desc);
130 struct stm32_crc *crc = ctx->crc;
134 pm_runtime_get_sync(crc->dev);
136 if (unlikely(crc->nb_pending_bytes)) {
137 while (crc->nb_pending_bytes != sizeof(u32) && length) {
138 /* Fill in pending data */
139 crc->pending_data[crc->nb_pending_bytes++] = *(d8++);
143 if (crc->nb_pending_bytes == sizeof(u32)) {
144 /* Process completed pending data */
145 writel_relaxed(*(u32 *)crc->pending_data,
147 crc->nb_pending_bytes = 0;
152 for (i = 0; i < length >> 2; i++)
153 /* Process 32 bits data */
154 writel_relaxed(*(d32++), crc->regs + CRC_DR);
156 /* Store partial result */
157 ctx->partial = readl_relaxed(crc->regs + CRC_DR);
159 pm_runtime_mark_last_busy(crc->dev);
160 pm_runtime_put_autosuspend(crc->dev);
162 /* Check for pending data (non 32 bits) */
167 if ((crc->nb_pending_bytes + length) >= sizeof(u32)) {
168 /* Shall not happen */
169 dev_err(crc->dev, "Pending data overflow\n");
173 d8 = (const u8 *)d32;
174 for (i = 0; i < length; i++)
175 /* Store pending data */
176 crc->pending_data[crc->nb_pending_bytes++] = *(d8++);
181 static int stm32_crc_final(struct shash_desc *desc, u8 *out)
183 struct stm32_crc_desc_ctx *ctx = shash_desc_ctx(desc);
184 struct stm32_crc_ctx *mctx = crypto_shash_ctx(desc->tfm);
186 /* Send computed CRC */
187 put_unaligned_le32(mctx->poly == CRC32C_POLY_LE ?
188 ~ctx->partial : ctx->partial, out);
193 static int stm32_crc_finup(struct shash_desc *desc, const u8 *data,
194 unsigned int length, u8 *out)
196 return stm32_crc_update(desc, data, length) ?:
197 stm32_crc_final(desc, out);
200 static int stm32_crc_digest(struct shash_desc *desc, const u8 *data,
201 unsigned int length, u8 *out)
203 return stm32_crc_init(desc) ?: stm32_crc_finup(desc, data, length, out);
206 static struct shash_alg algs[] = {
209 .setkey = stm32_crc_setkey,
210 .init = stm32_crc_init,
211 .update = stm32_crc_update,
212 .final = stm32_crc_final,
213 .finup = stm32_crc_finup,
214 .digest = stm32_crc_digest,
215 .descsize = sizeof(struct stm32_crc_desc_ctx),
216 .digestsize = CHKSUM_DIGEST_SIZE,
219 .cra_driver_name = DRIVER_NAME,
221 .cra_flags = CRYPTO_ALG_OPTIONAL_KEY,
222 .cra_blocksize = CHKSUM_BLOCK_SIZE,
224 .cra_ctxsize = sizeof(struct stm32_crc_ctx),
225 .cra_module = THIS_MODULE,
226 .cra_init = stm32_crc32_cra_init,
229 /* CRC-32Castagnoli */
231 .setkey = stm32_crc_setkey,
232 .init = stm32_crc_init,
233 .update = stm32_crc_update,
234 .final = stm32_crc_final,
235 .finup = stm32_crc_finup,
236 .digest = stm32_crc_digest,
237 .descsize = sizeof(struct stm32_crc_desc_ctx),
238 .digestsize = CHKSUM_DIGEST_SIZE,
240 .cra_name = "crc32c",
241 .cra_driver_name = DRIVER_NAME,
243 .cra_flags = CRYPTO_ALG_OPTIONAL_KEY,
244 .cra_blocksize = CHKSUM_BLOCK_SIZE,
246 .cra_ctxsize = sizeof(struct stm32_crc_ctx),
247 .cra_module = THIS_MODULE,
248 .cra_init = stm32_crc32c_cra_init,
253 static int stm32_crc_probe(struct platform_device *pdev)
255 struct device *dev = &pdev->dev;
256 struct stm32_crc *crc;
257 struct resource *res;
260 crc = devm_kzalloc(dev, sizeof(*crc), GFP_KERNEL);
266 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
267 crc->regs = devm_ioremap_resource(dev, res);
268 if (IS_ERR(crc->regs)) {
269 dev_err(dev, "Cannot map CRC IO\n");
270 return PTR_ERR(crc->regs);
273 crc->clk = devm_clk_get(dev, NULL);
274 if (IS_ERR(crc->clk)) {
275 dev_err(dev, "Could not get clock\n");
276 return PTR_ERR(crc->clk);
279 ret = clk_prepare_enable(crc->clk);
281 dev_err(crc->dev, "Failed to enable clock\n");
285 pm_runtime_set_autosuspend_delay(dev, CRC_AUTOSUSPEND_DELAY);
286 pm_runtime_use_autosuspend(dev);
288 pm_runtime_get_noresume(dev);
289 pm_runtime_set_active(dev);
290 pm_runtime_enable(dev);
292 platform_set_drvdata(pdev, crc);
294 spin_lock(&crc_list.lock);
295 list_add(&crc->list, &crc_list.dev_list);
296 spin_unlock(&crc_list.lock);
298 ret = crypto_register_shashes(algs, ARRAY_SIZE(algs));
300 dev_err(dev, "Failed to register\n");
301 clk_disable_unprepare(crc->clk);
305 dev_info(dev, "Initialized\n");
307 pm_runtime_put_sync(dev);
312 static int stm32_crc_remove(struct platform_device *pdev)
314 struct stm32_crc *crc = platform_get_drvdata(pdev);
315 int ret = pm_runtime_get_sync(crc->dev);
320 spin_lock(&crc_list.lock);
321 list_del(&crc->list);
322 spin_unlock(&crc_list.lock);
324 crypto_unregister_shashes(algs, ARRAY_SIZE(algs));
326 pm_runtime_disable(crc->dev);
327 pm_runtime_put_noidle(crc->dev);
329 clk_disable_unprepare(crc->clk);
335 static int stm32_crc_runtime_suspend(struct device *dev)
337 struct stm32_crc *crc = dev_get_drvdata(dev);
339 clk_disable_unprepare(crc->clk);
344 static int stm32_crc_runtime_resume(struct device *dev)
346 struct stm32_crc *crc = dev_get_drvdata(dev);
349 ret = clk_prepare_enable(crc->clk);
351 dev_err(crc->dev, "Failed to prepare_enable clock\n");
359 static const struct dev_pm_ops stm32_crc_pm_ops = {
360 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
361 pm_runtime_force_resume)
362 SET_RUNTIME_PM_OPS(stm32_crc_runtime_suspend,
363 stm32_crc_runtime_resume, NULL)
366 static const struct of_device_id stm32_dt_ids[] = {
367 { .compatible = "st,stm32f7-crc", },
370 MODULE_DEVICE_TABLE(of, stm32_dt_ids);
372 static struct platform_driver stm32_crc_driver = {
373 .probe = stm32_crc_probe,
374 .remove = stm32_crc_remove,
377 .pm = &stm32_crc_pm_ops,
378 .of_match_table = stm32_dt_ids,
382 module_platform_driver(stm32_crc_driver);
384 MODULE_AUTHOR("Fabien Dessenne <fabien.dessenne@st.com>");
385 MODULE_DESCRIPTION("STMicrolectronics STM32 CRC32 hardware driver");
386 MODULE_LICENSE("GPL");