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/module.h>
10 #include <linux/platform_device.h>
12 #include <crypto/internal/hash.h>
14 #include <asm/unaligned.h>
16 #define DRIVER_NAME "stm32-crc32"
17 #define CHKSUM_DIGEST_SIZE 4
18 #define CHKSUM_BLOCK_SIZE 1
21 #define CRC_DR 0x00000000
22 #define CRC_CR 0x00000008
23 #define CRC_INIT 0x00000010
24 #define CRC_POL 0x00000014
26 /* Registers values */
27 #define CRC_CR_RESET BIT(0)
28 #define CRC_CR_REVERSE (BIT(7) | BIT(6) | BIT(5))
29 #define CRC_INIT_DEFAULT 0xFFFFFFFF
31 /* Polynomial reversed */
32 #define POLY_CRC32 0xEDB88320
33 #define POLY_CRC32C 0x82F63B78
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 = POLY_CRC32;
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 = POLY_CRC32C;
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 /* Reset, set key, poly and configure in bit reverse mode */
110 writel_relaxed(bitrev32(mctx->key), ctx->crc->regs + CRC_INIT);
111 writel_relaxed(bitrev32(mctx->poly), ctx->crc->regs + CRC_POL);
112 writel_relaxed(CRC_CR_RESET | CRC_CR_REVERSE, ctx->crc->regs + CRC_CR);
114 /* Store partial result */
115 ctx->partial = readl_relaxed(ctx->crc->regs + CRC_DR);
116 ctx->crc->nb_pending_bytes = 0;
121 static int stm32_crc_update(struct shash_desc *desc, const u8 *d8,
124 struct stm32_crc_desc_ctx *ctx = shash_desc_ctx(desc);
125 struct stm32_crc *crc = ctx->crc;
129 if (unlikely(crc->nb_pending_bytes)) {
130 while (crc->nb_pending_bytes != sizeof(u32) && length) {
131 /* Fill in pending data */
132 crc->pending_data[crc->nb_pending_bytes++] = *(d8++);
136 if (crc->nb_pending_bytes == sizeof(u32)) {
137 /* Process completed pending data */
138 writel_relaxed(*(u32 *)crc->pending_data,
140 crc->nb_pending_bytes = 0;
145 for (i = 0; i < length >> 2; i++)
146 /* Process 32 bits data */
147 writel_relaxed(*(d32++), crc->regs + CRC_DR);
149 /* Store partial result */
150 ctx->partial = readl_relaxed(crc->regs + CRC_DR);
152 /* Check for pending data (non 32 bits) */
157 if ((crc->nb_pending_bytes + length) >= sizeof(u32)) {
158 /* Shall not happen */
159 dev_err(crc->dev, "Pending data overflow\n");
163 d8 = (const u8 *)d32;
164 for (i = 0; i < length; i++)
165 /* Store pending data */
166 crc->pending_data[crc->nb_pending_bytes++] = *(d8++);
171 static int stm32_crc_final(struct shash_desc *desc, u8 *out)
173 struct stm32_crc_desc_ctx *ctx = shash_desc_ctx(desc);
174 struct stm32_crc_ctx *mctx = crypto_shash_ctx(desc->tfm);
176 /* Send computed CRC */
177 put_unaligned_le32(mctx->poly == POLY_CRC32C ?
178 ~ctx->partial : ctx->partial, out);
183 static int stm32_crc_finup(struct shash_desc *desc, const u8 *data,
184 unsigned int length, u8 *out)
186 return stm32_crc_update(desc, data, length) ?:
187 stm32_crc_final(desc, out);
190 static int stm32_crc_digest(struct shash_desc *desc, const u8 *data,
191 unsigned int length, u8 *out)
193 return stm32_crc_init(desc) ?: stm32_crc_finup(desc, data, length, out);
196 static struct shash_alg algs[] = {
199 .setkey = stm32_crc_setkey,
200 .init = stm32_crc_init,
201 .update = stm32_crc_update,
202 .final = stm32_crc_final,
203 .finup = stm32_crc_finup,
204 .digest = stm32_crc_digest,
205 .descsize = sizeof(struct stm32_crc_desc_ctx),
206 .digestsize = CHKSUM_DIGEST_SIZE,
209 .cra_driver_name = DRIVER_NAME,
211 .cra_blocksize = CHKSUM_BLOCK_SIZE,
213 .cra_ctxsize = sizeof(struct stm32_crc_ctx),
214 .cra_module = THIS_MODULE,
215 .cra_init = stm32_crc32_cra_init,
218 /* CRC-32Castagnoli */
220 .setkey = stm32_crc_setkey,
221 .init = stm32_crc_init,
222 .update = stm32_crc_update,
223 .final = stm32_crc_final,
224 .finup = stm32_crc_finup,
225 .digest = stm32_crc_digest,
226 .descsize = sizeof(struct stm32_crc_desc_ctx),
227 .digestsize = CHKSUM_DIGEST_SIZE,
229 .cra_name = "crc32c",
230 .cra_driver_name = DRIVER_NAME,
232 .cra_blocksize = CHKSUM_BLOCK_SIZE,
234 .cra_ctxsize = sizeof(struct stm32_crc_ctx),
235 .cra_module = THIS_MODULE,
236 .cra_init = stm32_crc32c_cra_init,
241 static int stm32_crc_probe(struct platform_device *pdev)
243 struct device *dev = &pdev->dev;
244 struct stm32_crc *crc;
245 struct resource *res;
248 crc = devm_kzalloc(dev, sizeof(*crc), GFP_KERNEL);
254 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
255 crc->regs = devm_ioremap_resource(dev, res);
256 if (IS_ERR(crc->regs)) {
257 dev_err(dev, "Cannot map CRC IO\n");
258 return PTR_ERR(crc->regs);
261 crc->clk = devm_clk_get(dev, NULL);
262 if (IS_ERR(crc->clk)) {
263 dev_err(dev, "Could not get clock\n");
264 return PTR_ERR(crc->clk);
267 ret = clk_prepare_enable(crc->clk);
269 dev_err(crc->dev, "Failed to enable clock\n");
273 platform_set_drvdata(pdev, crc);
275 spin_lock(&crc_list.lock);
276 list_add(&crc->list, &crc_list.dev_list);
277 spin_unlock(&crc_list.lock);
279 ret = crypto_register_shashes(algs, ARRAY_SIZE(algs));
281 dev_err(dev, "Failed to register\n");
282 clk_disable_unprepare(crc->clk);
286 dev_info(dev, "Initialized\n");
291 static int stm32_crc_remove(struct platform_device *pdev)
293 struct stm32_crc *crc = platform_get_drvdata(pdev);
295 spin_lock(&crc_list.lock);
296 list_del(&crc->list);
297 spin_unlock(&crc_list.lock);
299 crypto_unregister_shashes(algs, ARRAY_SIZE(algs));
301 clk_disable_unprepare(crc->clk);
306 static const struct of_device_id stm32_dt_ids[] = {
307 { .compatible = "st,stm32f7-crc", },
310 MODULE_DEVICE_TABLE(of, stm32_dt_ids);
312 static struct platform_driver stm32_crc_driver = {
313 .probe = stm32_crc_probe,
314 .remove = stm32_crc_remove,
317 .of_match_table = stm32_dt_ids,
321 module_platform_driver(stm32_crc_driver);
323 MODULE_AUTHOR("Fabien Dessenne <fabien.dessenne@st.com>");
324 MODULE_DESCRIPTION("STMicrolectronics STM32 CRC32 hardware driver");
325 MODULE_LICENSE("GPL");