blob: e68b856d03b6e43afce24b2adcec545665e3d7a7 [file] [log] [blame]
David Brazdil0f672f62019-12-10 10:32:29 +00001// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * Copyright (C) STMicroelectronics SA 2017
4 * Author: Fabien Dessenne <fabien.dessenne@st.com>
5 */
6
7#include <linux/bitrev.h>
8#include <linux/clk.h>
9#include <linux/crc32poly.h>
10#include <linux/module.h>
11#include <linux/mod_devicetable.h>
12#include <linux/platform_device.h>
13#include <linux/pm_runtime.h>
14
15#include <crypto/internal/hash.h>
16
17#include <asm/unaligned.h>
18
19#define DRIVER_NAME "stm32-crc32"
20#define CHKSUM_DIGEST_SIZE 4
21#define CHKSUM_BLOCK_SIZE 1
22
23/* Registers */
24#define CRC_DR 0x00000000
25#define CRC_CR 0x00000008
26#define CRC_INIT 0x00000010
27#define CRC_POL 0x00000014
28
29/* Registers values */
30#define CRC_CR_RESET BIT(0)
Olivier Deprez0e641232021-09-23 10:07:05 +020031#define CRC_CR_REV_IN_WORD (BIT(6) | BIT(5))
32#define CRC_CR_REV_IN_BYTE BIT(5)
33#define CRC_CR_REV_OUT BIT(7)
34#define CRC32C_INIT_DEFAULT 0xFFFFFFFF
David Brazdil0f672f62019-12-10 10:32:29 +000035
36#define CRC_AUTOSUSPEND_DELAY 50
37
38struct stm32_crc {
39 struct list_head list;
40 struct device *dev;
41 void __iomem *regs;
42 struct clk *clk;
David Brazdil0f672f62019-12-10 10:32:29 +000043};
44
45struct stm32_crc_list {
46 struct list_head dev_list;
47 spinlock_t lock; /* protect dev_list */
48};
49
50static struct stm32_crc_list crc_list = {
51 .dev_list = LIST_HEAD_INIT(crc_list.dev_list),
52 .lock = __SPIN_LOCK_UNLOCKED(crc_list.lock),
53};
54
55struct stm32_crc_ctx {
56 u32 key;
57 u32 poly;
58};
59
60struct stm32_crc_desc_ctx {
61 u32 partial; /* crc32c: partial in first 4 bytes of that struct */
David Brazdil0f672f62019-12-10 10:32:29 +000062};
63
64static int stm32_crc32_cra_init(struct crypto_tfm *tfm)
65{
66 struct stm32_crc_ctx *mctx = crypto_tfm_ctx(tfm);
67
Olivier Deprez0e641232021-09-23 10:07:05 +020068 mctx->key = 0;
David Brazdil0f672f62019-12-10 10:32:29 +000069 mctx->poly = CRC32_POLY_LE;
70 return 0;
71}
72
73static int stm32_crc32c_cra_init(struct crypto_tfm *tfm)
74{
75 struct stm32_crc_ctx *mctx = crypto_tfm_ctx(tfm);
76
Olivier Deprez0e641232021-09-23 10:07:05 +020077 mctx->key = CRC32C_INIT_DEFAULT;
David Brazdil0f672f62019-12-10 10:32:29 +000078 mctx->poly = CRC32C_POLY_LE;
79 return 0;
80}
81
82static int stm32_crc_setkey(struct crypto_shash *tfm, const u8 *key,
83 unsigned int keylen)
84{
85 struct stm32_crc_ctx *mctx = crypto_shash_ctx(tfm);
86
87 if (keylen != sizeof(u32)) {
88 crypto_shash_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
89 return -EINVAL;
90 }
91
92 mctx->key = get_unaligned_le32(key);
93 return 0;
94}
95
Olivier Deprez0e641232021-09-23 10:07:05 +020096static struct stm32_crc *stm32_crc_get_next_crc(void)
97{
98 struct stm32_crc *crc;
99
100 spin_lock_bh(&crc_list.lock);
101 crc = list_first_entry(&crc_list.dev_list, struct stm32_crc, list);
102 if (crc)
103 list_move_tail(&crc->list, &crc_list.dev_list);
104 spin_unlock_bh(&crc_list.lock);
105
106 return crc;
107}
108
David Brazdil0f672f62019-12-10 10:32:29 +0000109static int stm32_crc_init(struct shash_desc *desc)
110{
111 struct stm32_crc_desc_ctx *ctx = shash_desc_ctx(desc);
112 struct stm32_crc_ctx *mctx = crypto_shash_ctx(desc->tfm);
113 struct stm32_crc *crc;
114
Olivier Deprez0e641232021-09-23 10:07:05 +0200115 crc = stm32_crc_get_next_crc();
116 if (!crc)
117 return -ENODEV;
David Brazdil0f672f62019-12-10 10:32:29 +0000118
Olivier Deprez0e641232021-09-23 10:07:05 +0200119 pm_runtime_get_sync(crc->dev);
David Brazdil0f672f62019-12-10 10:32:29 +0000120
121 /* Reset, set key, poly and configure in bit reverse mode */
Olivier Deprez0e641232021-09-23 10:07:05 +0200122 writel_relaxed(bitrev32(mctx->key), 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);
David Brazdil0f672f62019-12-10 10:32:29 +0000126
127 /* Store partial result */
Olivier Deprez0e641232021-09-23 10:07:05 +0200128 ctx->partial = readl_relaxed(crc->regs + CRC_DR);
David Brazdil0f672f62019-12-10 10:32:29 +0000129
Olivier Deprez0e641232021-09-23 10:07:05 +0200130 pm_runtime_mark_last_busy(crc->dev);
131 pm_runtime_put_autosuspend(crc->dev);
David Brazdil0f672f62019-12-10 10:32:29 +0000132
133 return 0;
134}
135
136static int stm32_crc_update(struct shash_desc *desc, const u8 *d8,
137 unsigned int length)
138{
139 struct stm32_crc_desc_ctx *ctx = shash_desc_ctx(desc);
Olivier Deprez0e641232021-09-23 10:07:05 +0200140 struct stm32_crc_ctx *mctx = crypto_shash_ctx(desc->tfm);
141 struct stm32_crc *crc;
142
143 crc = stm32_crc_get_next_crc();
144 if (!crc)
145 return -ENODEV;
David Brazdil0f672f62019-12-10 10:32:29 +0000146
147 pm_runtime_get_sync(crc->dev);
148
Olivier Deprez0e641232021-09-23 10:07:05 +0200149 /*
150 * Restore previously calculated CRC for this context as init value
151 * Restore polynomial configuration
152 * Configure in register for word input data,
153 * Configure out register in reversed bit mode data.
154 */
155 writel_relaxed(bitrev32(ctx->partial), crc->regs + CRC_INIT);
156 writel_relaxed(bitrev32(mctx->poly), crc->regs + CRC_POL);
157 writel_relaxed(CRC_CR_RESET | CRC_CR_REV_IN_WORD | CRC_CR_REV_OUT,
158 crc->regs + CRC_CR);
159
160 if (d8 != PTR_ALIGN(d8, sizeof(u32))) {
161 /* Configure for byte data */
162 writel_relaxed(CRC_CR_REV_IN_BYTE | CRC_CR_REV_OUT,
163 crc->regs + CRC_CR);
164 while (d8 != PTR_ALIGN(d8, sizeof(u32)) && length) {
165 writeb_relaxed(*d8++, crc->regs + CRC_DR);
David Brazdil0f672f62019-12-10 10:32:29 +0000166 length--;
167 }
Olivier Deprez0e641232021-09-23 10:07:05 +0200168 /* Configure for word data */
169 writel_relaxed(CRC_CR_REV_IN_WORD | CRC_CR_REV_OUT,
170 crc->regs + CRC_CR);
David Brazdil0f672f62019-12-10 10:32:29 +0000171 }
172
Olivier Deprez0e641232021-09-23 10:07:05 +0200173 for (; length >= sizeof(u32); d8 += sizeof(u32), length -= sizeof(u32))
174 writel_relaxed(*((u32 *)d8), crc->regs + CRC_DR);
175
176 if (length) {
177 /* Configure for byte data */
178 writel_relaxed(CRC_CR_REV_IN_BYTE | CRC_CR_REV_OUT,
179 crc->regs + CRC_CR);
180 while (length--)
181 writeb_relaxed(*d8++, crc->regs + CRC_DR);
182 }
David Brazdil0f672f62019-12-10 10:32:29 +0000183
184 /* Store partial result */
185 ctx->partial = readl_relaxed(crc->regs + CRC_DR);
186
187 pm_runtime_mark_last_busy(crc->dev);
188 pm_runtime_put_autosuspend(crc->dev);
189
David Brazdil0f672f62019-12-10 10:32:29 +0000190 return 0;
191}
192
193static int stm32_crc_final(struct shash_desc *desc, u8 *out)
194{
195 struct stm32_crc_desc_ctx *ctx = shash_desc_ctx(desc);
196 struct stm32_crc_ctx *mctx = crypto_shash_ctx(desc->tfm);
197
198 /* Send computed CRC */
199 put_unaligned_le32(mctx->poly == CRC32C_POLY_LE ?
200 ~ctx->partial : ctx->partial, out);
201
202 return 0;
203}
204
205static int stm32_crc_finup(struct shash_desc *desc, const u8 *data,
206 unsigned int length, u8 *out)
207{
208 return stm32_crc_update(desc, data, length) ?:
209 stm32_crc_final(desc, out);
210}
211
212static int stm32_crc_digest(struct shash_desc *desc, const u8 *data,
213 unsigned int length, u8 *out)
214{
215 return stm32_crc_init(desc) ?: stm32_crc_finup(desc, data, length, out);
216}
217
Olivier Deprez0e641232021-09-23 10:07:05 +0200218static unsigned int refcnt;
219static DEFINE_MUTEX(refcnt_lock);
David Brazdil0f672f62019-12-10 10:32:29 +0000220static struct shash_alg algs[] = {
221 /* CRC-32 */
222 {
223 .setkey = stm32_crc_setkey,
224 .init = stm32_crc_init,
225 .update = stm32_crc_update,
226 .final = stm32_crc_final,
227 .finup = stm32_crc_finup,
228 .digest = stm32_crc_digest,
229 .descsize = sizeof(struct stm32_crc_desc_ctx),
230 .digestsize = CHKSUM_DIGEST_SIZE,
231 .base = {
232 .cra_name = "crc32",
233 .cra_driver_name = DRIVER_NAME,
234 .cra_priority = 200,
235 .cra_flags = CRYPTO_ALG_OPTIONAL_KEY,
236 .cra_blocksize = CHKSUM_BLOCK_SIZE,
237 .cra_alignmask = 3,
238 .cra_ctxsize = sizeof(struct stm32_crc_ctx),
239 .cra_module = THIS_MODULE,
240 .cra_init = stm32_crc32_cra_init,
241 }
242 },
243 /* CRC-32Castagnoli */
244 {
245 .setkey = stm32_crc_setkey,
246 .init = stm32_crc_init,
247 .update = stm32_crc_update,
248 .final = stm32_crc_final,
249 .finup = stm32_crc_finup,
250 .digest = stm32_crc_digest,
251 .descsize = sizeof(struct stm32_crc_desc_ctx),
252 .digestsize = CHKSUM_DIGEST_SIZE,
253 .base = {
254 .cra_name = "crc32c",
255 .cra_driver_name = DRIVER_NAME,
256 .cra_priority = 200,
257 .cra_flags = CRYPTO_ALG_OPTIONAL_KEY,
258 .cra_blocksize = CHKSUM_BLOCK_SIZE,
259 .cra_alignmask = 3,
260 .cra_ctxsize = sizeof(struct stm32_crc_ctx),
261 .cra_module = THIS_MODULE,
262 .cra_init = stm32_crc32c_cra_init,
263 }
264 }
265};
266
267static int stm32_crc_probe(struct platform_device *pdev)
268{
269 struct device *dev = &pdev->dev;
270 struct stm32_crc *crc;
271 int ret;
272
273 crc = devm_kzalloc(dev, sizeof(*crc), GFP_KERNEL);
274 if (!crc)
275 return -ENOMEM;
276
277 crc->dev = dev;
278
279 crc->regs = devm_platform_ioremap_resource(pdev, 0);
280 if (IS_ERR(crc->regs)) {
281 dev_err(dev, "Cannot map CRC IO\n");
282 return PTR_ERR(crc->regs);
283 }
284
285 crc->clk = devm_clk_get(dev, NULL);
286 if (IS_ERR(crc->clk)) {
287 dev_err(dev, "Could not get clock\n");
288 return PTR_ERR(crc->clk);
289 }
290
291 ret = clk_prepare_enable(crc->clk);
292 if (ret) {
293 dev_err(crc->dev, "Failed to enable clock\n");
294 return ret;
295 }
296
297 pm_runtime_set_autosuspend_delay(dev, CRC_AUTOSUSPEND_DELAY);
298 pm_runtime_use_autosuspend(dev);
299
300 pm_runtime_get_noresume(dev);
301 pm_runtime_set_active(dev);
302 pm_runtime_enable(dev);
303
304 platform_set_drvdata(pdev, crc);
305
306 spin_lock(&crc_list.lock);
307 list_add(&crc->list, &crc_list.dev_list);
308 spin_unlock(&crc_list.lock);
309
Olivier Deprez0e641232021-09-23 10:07:05 +0200310 mutex_lock(&refcnt_lock);
311 if (!refcnt) {
312 ret = crypto_register_shashes(algs, ARRAY_SIZE(algs));
313 if (ret) {
314 mutex_unlock(&refcnt_lock);
315 dev_err(dev, "Failed to register\n");
316 clk_disable_unprepare(crc->clk);
317 return ret;
318 }
David Brazdil0f672f62019-12-10 10:32:29 +0000319 }
Olivier Deprez0e641232021-09-23 10:07:05 +0200320 refcnt++;
321 mutex_unlock(&refcnt_lock);
David Brazdil0f672f62019-12-10 10:32:29 +0000322
323 dev_info(dev, "Initialized\n");
324
325 pm_runtime_put_sync(dev);
326
327 return 0;
328}
329
330static int stm32_crc_remove(struct platform_device *pdev)
331{
332 struct stm32_crc *crc = platform_get_drvdata(pdev);
333 int ret = pm_runtime_get_sync(crc->dev);
334
335 if (ret < 0)
336 return ret;
337
338 spin_lock(&crc_list.lock);
339 list_del(&crc->list);
340 spin_unlock(&crc_list.lock);
341
Olivier Deprez0e641232021-09-23 10:07:05 +0200342 mutex_lock(&refcnt_lock);
343 if (!--refcnt)
344 crypto_unregister_shashes(algs, ARRAY_SIZE(algs));
345 mutex_unlock(&refcnt_lock);
David Brazdil0f672f62019-12-10 10:32:29 +0000346
347 pm_runtime_disable(crc->dev);
348 pm_runtime_put_noidle(crc->dev);
349
350 clk_disable_unprepare(crc->clk);
351
352 return 0;
353}
354
355#ifdef CONFIG_PM
356static int stm32_crc_runtime_suspend(struct device *dev)
357{
358 struct stm32_crc *crc = dev_get_drvdata(dev);
359
360 clk_disable_unprepare(crc->clk);
361
362 return 0;
363}
364
365static int stm32_crc_runtime_resume(struct device *dev)
366{
367 struct stm32_crc *crc = dev_get_drvdata(dev);
368 int ret;
369
370 ret = clk_prepare_enable(crc->clk);
371 if (ret) {
372 dev_err(crc->dev, "Failed to prepare_enable clock\n");
373 return ret;
374 }
375
376 return 0;
377}
378#endif
379
380static const struct dev_pm_ops stm32_crc_pm_ops = {
381 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
382 pm_runtime_force_resume)
383 SET_RUNTIME_PM_OPS(stm32_crc_runtime_suspend,
384 stm32_crc_runtime_resume, NULL)
385};
386
387static const struct of_device_id stm32_dt_ids[] = {
388 { .compatible = "st,stm32f7-crc", },
389 {},
390};
391MODULE_DEVICE_TABLE(of, stm32_dt_ids);
392
393static struct platform_driver stm32_crc_driver = {
394 .probe = stm32_crc_probe,
395 .remove = stm32_crc_remove,
396 .driver = {
397 .name = DRIVER_NAME,
398 .pm = &stm32_crc_pm_ops,
399 .of_match_table = stm32_dt_ids,
400 },
401};
402
403module_platform_driver(stm32_crc_driver);
404
405MODULE_AUTHOR("Fabien Dessenne <fabien.dessenne@st.com>");
406MODULE_DESCRIPTION("STMicrolectronics STM32 CRC32 hardware driver");
407MODULE_LICENSE("GPL");