2019-05-28 19:57:21 +03:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
2017-03-21 18:13:28 +03:00
|
|
|
/*
|
|
|
|
* Copyright (C) STMicroelectronics SA 2017
|
|
|
|
* Author: Fabien Dessenne <fabien.dessenne@st.com>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/bitrev.h>
|
|
|
|
#include <linux/clk.h>
|
2020-09-16 09:33:44 +03:00
|
|
|
#include <linux/crc32.h>
|
2018-07-17 19:05:38 +03:00
|
|
|
#include <linux/crc32poly.h>
|
2020-08-21 15:42:10 +03:00
|
|
|
#include <linux/io.h>
|
|
|
|
#include <linux/kernel.h>
|
2017-03-21 18:13:28 +03:00
|
|
|
#include <linux/module.h>
|
2018-06-20 08:47:28 +03:00
|
|
|
#include <linux/mod_devicetable.h>
|
2017-03-21 18:13:28 +03:00
|
|
|
#include <linux/platform_device.h>
|
2018-06-26 15:52:46 +03:00
|
|
|
#include <linux/pm_runtime.h>
|
2017-03-21 18:13:28 +03:00
|
|
|
|
|
|
|
#include <crypto/internal/hash.h>
|
|
|
|
|
|
|
|
#include <asm/unaligned.h>
|
|
|
|
|
|
|
|
#define DRIVER_NAME "stm32-crc32"
|
|
|
|
#define CHKSUM_DIGEST_SIZE 4
|
|
|
|
#define CHKSUM_BLOCK_SIZE 1
|
|
|
|
|
|
|
|
/* Registers */
|
|
|
|
#define CRC_DR 0x00000000
|
|
|
|
#define CRC_CR 0x00000008
|
|
|
|
#define CRC_INIT 0x00000010
|
|
|
|
#define CRC_POL 0x00000014
|
|
|
|
|
|
|
|
/* Registers values */
|
|
|
|
#define CRC_CR_RESET BIT(0)
|
2020-05-12 17:11:09 +03:00
|
|
|
#define CRC_CR_REV_IN_WORD (BIT(6) | BIT(5))
|
|
|
|
#define CRC_CR_REV_IN_BYTE BIT(5)
|
|
|
|
#define CRC_CR_REV_OUT BIT(7)
|
2020-05-12 17:11:10 +03:00
|
|
|
#define CRC32C_INIT_DEFAULT 0xFFFFFFFF
|
2017-03-21 18:13:28 +03:00
|
|
|
|
2018-06-26 15:52:46 +03:00
|
|
|
#define CRC_AUTOSUSPEND_DELAY 50
|
|
|
|
|
2020-05-12 17:11:13 +03:00
|
|
|
static unsigned int burst_size;
|
|
|
|
module_param(burst_size, uint, 0644);
|
|
|
|
MODULE_PARM_DESC(burst_size, "Select burst byte size (0 unlimited)");
|
|
|
|
|
2017-03-21 18:13:28 +03:00
|
|
|
struct stm32_crc {
|
|
|
|
struct list_head list;
|
|
|
|
struct device *dev;
|
|
|
|
void __iomem *regs;
|
|
|
|
struct clk *clk;
|
2020-05-12 17:11:13 +03:00
|
|
|
spinlock_t lock;
|
2017-03-21 18:13:28 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
struct stm32_crc_list {
|
|
|
|
struct list_head dev_list;
|
|
|
|
spinlock_t lock; /* protect dev_list */
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct stm32_crc_list crc_list = {
|
|
|
|
.dev_list = LIST_HEAD_INIT(crc_list.dev_list),
|
|
|
|
.lock = __SPIN_LOCK_UNLOCKED(crc_list.lock),
|
|
|
|
};
|
|
|
|
|
|
|
|
struct stm32_crc_ctx {
|
|
|
|
u32 key;
|
|
|
|
u32 poly;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct stm32_crc_desc_ctx {
|
|
|
|
u32 partial; /* crc32c: partial in first 4 bytes of that struct */
|
|
|
|
};
|
|
|
|
|
|
|
|
static int stm32_crc32_cra_init(struct crypto_tfm *tfm)
|
|
|
|
{
|
|
|
|
struct stm32_crc_ctx *mctx = crypto_tfm_ctx(tfm);
|
|
|
|
|
2020-05-12 17:11:10 +03:00
|
|
|
mctx->key = 0;
|
2018-07-17 19:05:38 +03:00
|
|
|
mctx->poly = CRC32_POLY_LE;
|
2017-03-21 18:13:28 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int stm32_crc32c_cra_init(struct crypto_tfm *tfm)
|
|
|
|
{
|
|
|
|
struct stm32_crc_ctx *mctx = crypto_tfm_ctx(tfm);
|
|
|
|
|
2020-05-12 17:11:10 +03:00
|
|
|
mctx->key = CRC32C_INIT_DEFAULT;
|
2018-07-17 19:05:38 +03:00
|
|
|
mctx->poly = CRC32C_POLY_LE;
|
2017-03-21 18:13:28 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int stm32_crc_setkey(struct crypto_shash *tfm, const u8 *key,
|
|
|
|
unsigned int keylen)
|
|
|
|
{
|
|
|
|
struct stm32_crc_ctx *mctx = crypto_shash_ctx(tfm);
|
|
|
|
|
crypto: remove CRYPTO_TFM_RES_BAD_KEY_LEN
The CRYPTO_TFM_RES_BAD_KEY_LEN flag was apparently meant as a way to
make the ->setkey() functions provide more information about errors.
However, no one actually checks for this flag, which makes it pointless.
Also, many algorithms fail to set this flag when given a bad length key.
Reviewing just the generic implementations, this is the case for
aes-fixed-time, cbcmac, echainiv, nhpoly1305, pcrypt, rfc3686, rfc4309,
rfc7539, rfc7539esp, salsa20, seqiv, and xcbc. But there are probably
many more in arch/*/crypto/ and drivers/crypto/.
Some algorithms can even set this flag when the key is the correct
length. For example, authenc and authencesn set it when the key payload
is malformed in any way (not just a bad length), the atmel-sha and ccree
drivers can set it if a memory allocation fails, and the chelsio driver
sets it for bad auth tag lengths, not just bad key lengths.
So even if someone actually wanted to start checking this flag (which
seems unlikely, since it's been unused for a long time), there would be
a lot of work needed to get it working correctly. But it would probably
be much better to go back to the drawing board and just define different
return values, like -EINVAL if the key is invalid for the algorithm vs.
-EKEYREJECTED if the key was rejected by a policy like "no weak keys".
That would be much simpler, less error-prone, and easier to test.
So just remove this flag.
Signed-off-by: Eric Biggers <ebiggers@google.com>
Reviewed-by: Horia Geantă <horia.geanta@nxp.com>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
2019-12-31 06:19:36 +03:00
|
|
|
if (keylen != sizeof(u32))
|
2017-03-21 18:13:28 +03:00
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
mctx->key = get_unaligned_le32(key);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-05-12 17:11:11 +03:00
|
|
|
static struct stm32_crc *stm32_crc_get_next_crc(void)
|
2017-03-21 18:13:28 +03:00
|
|
|
{
|
|
|
|
struct stm32_crc *crc;
|
|
|
|
|
|
|
|
spin_lock_bh(&crc_list.lock);
|
2023-12-15 14:17:24 +03:00
|
|
|
crc = list_first_entry_or_null(&crc_list.dev_list, struct stm32_crc, list);
|
2020-05-12 17:11:11 +03:00
|
|
|
if (crc)
|
|
|
|
list_move_tail(&crc->list, &crc_list.dev_list);
|
2017-03-21 18:13:28 +03:00
|
|
|
spin_unlock_bh(&crc_list.lock);
|
|
|
|
|
2020-05-12 17:11:11 +03:00
|
|
|
return crc;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int stm32_crc_init(struct shash_desc *desc)
|
|
|
|
{
|
|
|
|
struct stm32_crc_desc_ctx *ctx = shash_desc_ctx(desc);
|
|
|
|
struct stm32_crc_ctx *mctx = crypto_shash_ctx(desc->tfm);
|
|
|
|
struct stm32_crc *crc;
|
2020-05-12 17:11:13 +03:00
|
|
|
unsigned long flags;
|
2020-05-12 17:11:11 +03:00
|
|
|
|
|
|
|
crc = stm32_crc_get_next_crc();
|
|
|
|
if (!crc)
|
|
|
|
return -ENODEV;
|
|
|
|
|
2020-05-12 17:11:09 +03:00
|
|
|
pm_runtime_get_sync(crc->dev);
|
2018-06-26 15:52:46 +03:00
|
|
|
|
2020-05-12 17:11:13 +03:00
|
|
|
spin_lock_irqsave(&crc->lock, flags);
|
|
|
|
|
2017-03-21 18:13:28 +03:00
|
|
|
/* Reset, set key, poly and configure in bit reverse mode */
|
2020-05-12 17:11:09 +03:00
|
|
|
writel_relaxed(bitrev32(mctx->key), crc->regs + CRC_INIT);
|
|
|
|
writel_relaxed(bitrev32(mctx->poly), crc->regs + CRC_POL);
|
|
|
|
writel_relaxed(CRC_CR_RESET | CRC_CR_REV_IN_WORD | CRC_CR_REV_OUT,
|
|
|
|
crc->regs + CRC_CR);
|
2017-03-21 18:13:28 +03:00
|
|
|
|
|
|
|
/* Store partial result */
|
2020-05-12 17:11:09 +03:00
|
|
|
ctx->partial = readl_relaxed(crc->regs + CRC_DR);
|
2017-03-21 18:13:28 +03:00
|
|
|
|
2020-05-12 17:11:13 +03:00
|
|
|
spin_unlock_irqrestore(&crc->lock, flags);
|
|
|
|
|
2020-05-12 17:11:09 +03:00
|
|
|
pm_runtime_mark_last_busy(crc->dev);
|
|
|
|
pm_runtime_put_autosuspend(crc->dev);
|
2018-06-26 15:52:46 +03:00
|
|
|
|
2017-03-21 18:13:28 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-05-12 17:11:13 +03:00
|
|
|
static int burst_update(struct shash_desc *desc, const u8 *d8,
|
|
|
|
size_t length)
|
2017-03-21 18:13:28 +03:00
|
|
|
{
|
|
|
|
struct stm32_crc_desc_ctx *ctx = shash_desc_ctx(desc);
|
2020-05-12 17:11:09 +03:00
|
|
|
struct stm32_crc_ctx *mctx = crypto_shash_ctx(desc->tfm);
|
|
|
|
struct stm32_crc *crc;
|
|
|
|
|
2020-05-12 17:11:11 +03:00
|
|
|
crc = stm32_crc_get_next_crc();
|
|
|
|
if (!crc)
|
|
|
|
return -ENODEV;
|
2017-03-21 18:13:28 +03:00
|
|
|
|
2018-06-26 15:52:46 +03:00
|
|
|
pm_runtime_get_sync(crc->dev);
|
|
|
|
|
2020-09-16 09:33:44 +03:00
|
|
|
if (!spin_trylock(&crc->lock)) {
|
|
|
|
/* Hardware is busy, calculate crc32 by software */
|
|
|
|
if (mctx->poly == CRC32_POLY_LE)
|
|
|
|
ctx->partial = crc32_le(ctx->partial, d8, length);
|
|
|
|
else
|
|
|
|
ctx->partial = __crc32c_le(ctx->partial, d8, length);
|
|
|
|
|
|
|
|
goto pm_out;
|
|
|
|
}
|
2020-05-12 17:11:13 +03:00
|
|
|
|
2020-05-12 17:11:09 +03:00
|
|
|
/*
|
|
|
|
* Restore previously calculated CRC for this context as init value
|
|
|
|
* Restore polynomial configuration
|
|
|
|
* Configure in register for word input data,
|
|
|
|
* Configure out register in reversed bit mode data.
|
|
|
|
*/
|
|
|
|
writel_relaxed(bitrev32(ctx->partial), crc->regs + CRC_INIT);
|
|
|
|
writel_relaxed(bitrev32(mctx->poly), crc->regs + CRC_POL);
|
|
|
|
writel_relaxed(CRC_CR_RESET | CRC_CR_REV_IN_WORD | CRC_CR_REV_OUT,
|
|
|
|
crc->regs + CRC_CR);
|
|
|
|
|
|
|
|
if (d8 != PTR_ALIGN(d8, sizeof(u32))) {
|
|
|
|
/* Configure for byte data */
|
|
|
|
writel_relaxed(CRC_CR_REV_IN_BYTE | CRC_CR_REV_OUT,
|
|
|
|
crc->regs + CRC_CR);
|
|
|
|
while (d8 != PTR_ALIGN(d8, sizeof(u32)) && length) {
|
|
|
|
writeb_relaxed(*d8++, crc->regs + CRC_DR);
|
2017-03-21 18:13:28 +03:00
|
|
|
length--;
|
|
|
|
}
|
2020-05-12 17:11:09 +03:00
|
|
|
/* Configure for word data */
|
|
|
|
writel_relaxed(CRC_CR_REV_IN_WORD | CRC_CR_REV_OUT,
|
|
|
|
crc->regs + CRC_CR);
|
2017-03-21 18:13:28 +03:00
|
|
|
}
|
|
|
|
|
2020-05-12 17:11:09 +03:00
|
|
|
for (; length >= sizeof(u32); d8 += sizeof(u32), length -= sizeof(u32))
|
|
|
|
writel_relaxed(*((u32 *)d8), crc->regs + CRC_DR);
|
|
|
|
|
|
|
|
if (length) {
|
|
|
|
/* Configure for byte data */
|
|
|
|
writel_relaxed(CRC_CR_REV_IN_BYTE | CRC_CR_REV_OUT,
|
|
|
|
crc->regs + CRC_CR);
|
|
|
|
while (length--)
|
|
|
|
writeb_relaxed(*d8++, crc->regs + CRC_DR);
|
|
|
|
}
|
2017-03-21 18:13:28 +03:00
|
|
|
|
|
|
|
/* Store partial result */
|
2017-07-13 16:06:31 +03:00
|
|
|
ctx->partial = readl_relaxed(crc->regs + CRC_DR);
|
2017-03-21 18:13:28 +03:00
|
|
|
|
2020-09-16 09:33:44 +03:00
|
|
|
spin_unlock(&crc->lock);
|
2020-05-12 17:11:13 +03:00
|
|
|
|
2020-09-16 09:33:44 +03:00
|
|
|
pm_out:
|
2018-06-26 15:52:46 +03:00
|
|
|
pm_runtime_mark_last_busy(crc->dev);
|
|
|
|
pm_runtime_put_autosuspend(crc->dev);
|
|
|
|
|
2017-03-21 18:13:28 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-05-12 17:11:13 +03:00
|
|
|
static int stm32_crc_update(struct shash_desc *desc, const u8 *d8,
|
|
|
|
unsigned int length)
|
|
|
|
{
|
|
|
|
const unsigned int burst_sz = burst_size;
|
|
|
|
unsigned int rem_sz;
|
|
|
|
const u8 *cur;
|
|
|
|
size_t size;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (!burst_sz)
|
|
|
|
return burst_update(desc, d8, length);
|
|
|
|
|
|
|
|
/* Digest first bytes not 32bit aligned at first pass in the loop */
|
2020-08-21 16:59:12 +03:00
|
|
|
size = min_t(size_t, length, burst_sz + (size_t)d8 -
|
|
|
|
ALIGN_DOWN((size_t)d8, sizeof(u32)));
|
2020-05-12 17:11:13 +03:00
|
|
|
for (rem_sz = length, cur = d8; rem_sz;
|
|
|
|
rem_sz -= size, cur += size, size = min(rem_sz, burst_sz)) {
|
|
|
|
ret = burst_update(desc, cur, size);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-03-21 18:13:28 +03:00
|
|
|
static int stm32_crc_final(struct shash_desc *desc, u8 *out)
|
|
|
|
{
|
|
|
|
struct stm32_crc_desc_ctx *ctx = shash_desc_ctx(desc);
|
|
|
|
struct stm32_crc_ctx *mctx = crypto_shash_ctx(desc->tfm);
|
|
|
|
|
|
|
|
/* Send computed CRC */
|
2018-07-17 19:05:38 +03:00
|
|
|
put_unaligned_le32(mctx->poly == CRC32C_POLY_LE ?
|
2017-03-21 18:13:28 +03:00
|
|
|
~ctx->partial : ctx->partial, out);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int stm32_crc_finup(struct shash_desc *desc, const u8 *data,
|
|
|
|
unsigned int length, u8 *out)
|
|
|
|
{
|
|
|
|
return stm32_crc_update(desc, data, length) ?:
|
|
|
|
stm32_crc_final(desc, out);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int stm32_crc_digest(struct shash_desc *desc, const u8 *data,
|
|
|
|
unsigned int length, u8 *out)
|
|
|
|
{
|
|
|
|
return stm32_crc_init(desc) ?: stm32_crc_finup(desc, data, length, out);
|
|
|
|
}
|
|
|
|
|
2020-05-12 17:11:11 +03:00
|
|
|
static unsigned int refcnt;
|
|
|
|
static DEFINE_MUTEX(refcnt_lock);
|
2017-03-21 18:13:28 +03:00
|
|
|
static struct shash_alg algs[] = {
|
|
|
|
/* CRC-32 */
|
|
|
|
{
|
|
|
|
.setkey = stm32_crc_setkey,
|
|
|
|
.init = stm32_crc_init,
|
|
|
|
.update = stm32_crc_update,
|
|
|
|
.final = stm32_crc_final,
|
|
|
|
.finup = stm32_crc_finup,
|
|
|
|
.digest = stm32_crc_digest,
|
|
|
|
.descsize = sizeof(struct stm32_crc_desc_ctx),
|
|
|
|
.digestsize = CHKSUM_DIGEST_SIZE,
|
|
|
|
.base = {
|
|
|
|
.cra_name = "crc32",
|
crypto: stm32/crc32 - Fix kernel BUG triggered in probe()
commit 29009604ad4e3ef784fd9b9fef6f23610ddf633d upstream.
The include/linux/crypto.h struct crypto_alg field cra_driver_name description
states "Unique name of the transformation provider. " ... " this contains the
name of the chip or provider and the name of the transformation algorithm."
In case of the stm32-crc driver, field cra_driver_name is identical for all
registered transformation providers and set to the name of the driver itself,
which is incorrect. This patch fixes it by assigning a unique cra_driver_name
to each registered transformation provider.
The kernel crash is triggered when the driver calls crypto_register_shashes()
which calls crypto_register_shash(), which calls crypto_register_alg(), which
calls __crypto_register_alg(), which returns -EEXIST, which is propagated
back through this call chain. Upon -EEXIST from crypto_register_shash(), the
crypto_register_shashes() starts unregistering the providers back, and calls
crypto_unregister_shash(), which calls crypto_unregister_alg(), and this is
where the BUG() triggers due to incorrect cra_refcnt.
Fixes: b51dbe90912a ("crypto: stm32 - Support for STM32 CRC32 crypto module")
Signed-off-by: Marek Vasut <marex@denx.de>
Cc: <stable@vger.kernel.org> # 4.12+
Cc: Alexandre Torgue <alexandre.torgue@foss.st.com>
Cc: Fabien Dessenne <fabien.dessenne@st.com>
Cc: Herbert Xu <herbert@gondor.apana.org.au>
Cc: Lionel Debieve <lionel.debieve@st.com>
Cc: Nicolas Toromanoff <nicolas.toromanoff@st.com>
Cc: linux-arm-kernel@lists.infradead.org
Cc: linux-stm32@st-md-mailman.stormreply.com
To: linux-crypto@vger.kernel.org
Acked-by: Nicolas Toromanoff <nicolas.toromanoff@foss.st.com>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2021-12-20 22:50:22 +03:00
|
|
|
.cra_driver_name = "stm32-crc32-crc32",
|
2017-03-21 18:13:28 +03:00
|
|
|
.cra_priority = 200,
|
2018-01-03 22:16:26 +03:00
|
|
|
.cra_flags = CRYPTO_ALG_OPTIONAL_KEY,
|
2017-03-21 18:13:28 +03:00
|
|
|
.cra_blocksize = CHKSUM_BLOCK_SIZE,
|
|
|
|
.cra_alignmask = 3,
|
|
|
|
.cra_ctxsize = sizeof(struct stm32_crc_ctx),
|
|
|
|
.cra_module = THIS_MODULE,
|
|
|
|
.cra_init = stm32_crc32_cra_init,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
/* CRC-32Castagnoli */
|
|
|
|
{
|
|
|
|
.setkey = stm32_crc_setkey,
|
|
|
|
.init = stm32_crc_init,
|
|
|
|
.update = stm32_crc_update,
|
|
|
|
.final = stm32_crc_final,
|
|
|
|
.finup = stm32_crc_finup,
|
|
|
|
.digest = stm32_crc_digest,
|
|
|
|
.descsize = sizeof(struct stm32_crc_desc_ctx),
|
|
|
|
.digestsize = CHKSUM_DIGEST_SIZE,
|
|
|
|
.base = {
|
|
|
|
.cra_name = "crc32c",
|
crypto: stm32/crc32 - Fix kernel BUG triggered in probe()
commit 29009604ad4e3ef784fd9b9fef6f23610ddf633d upstream.
The include/linux/crypto.h struct crypto_alg field cra_driver_name description
states "Unique name of the transformation provider. " ... " this contains the
name of the chip or provider and the name of the transformation algorithm."
In case of the stm32-crc driver, field cra_driver_name is identical for all
registered transformation providers and set to the name of the driver itself,
which is incorrect. This patch fixes it by assigning a unique cra_driver_name
to each registered transformation provider.
The kernel crash is triggered when the driver calls crypto_register_shashes()
which calls crypto_register_shash(), which calls crypto_register_alg(), which
calls __crypto_register_alg(), which returns -EEXIST, which is propagated
back through this call chain. Upon -EEXIST from crypto_register_shash(), the
crypto_register_shashes() starts unregistering the providers back, and calls
crypto_unregister_shash(), which calls crypto_unregister_alg(), and this is
where the BUG() triggers due to incorrect cra_refcnt.
Fixes: b51dbe90912a ("crypto: stm32 - Support for STM32 CRC32 crypto module")
Signed-off-by: Marek Vasut <marex@denx.de>
Cc: <stable@vger.kernel.org> # 4.12+
Cc: Alexandre Torgue <alexandre.torgue@foss.st.com>
Cc: Fabien Dessenne <fabien.dessenne@st.com>
Cc: Herbert Xu <herbert@gondor.apana.org.au>
Cc: Lionel Debieve <lionel.debieve@st.com>
Cc: Nicolas Toromanoff <nicolas.toromanoff@st.com>
Cc: linux-arm-kernel@lists.infradead.org
Cc: linux-stm32@st-md-mailman.stormreply.com
To: linux-crypto@vger.kernel.org
Acked-by: Nicolas Toromanoff <nicolas.toromanoff@foss.st.com>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2021-12-20 22:50:22 +03:00
|
|
|
.cra_driver_name = "stm32-crc32-crc32c",
|
2017-03-21 18:13:28 +03:00
|
|
|
.cra_priority = 200,
|
2018-01-03 22:16:26 +03:00
|
|
|
.cra_flags = CRYPTO_ALG_OPTIONAL_KEY,
|
2017-03-21 18:13:28 +03:00
|
|
|
.cra_blocksize = CHKSUM_BLOCK_SIZE,
|
|
|
|
.cra_alignmask = 3,
|
|
|
|
.cra_ctxsize = sizeof(struct stm32_crc_ctx),
|
|
|
|
.cra_module = THIS_MODULE,
|
|
|
|
.cra_init = stm32_crc32c_cra_init,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
static int stm32_crc_probe(struct platform_device *pdev)
|
|
|
|
{
|
|
|
|
struct device *dev = &pdev->dev;
|
|
|
|
struct stm32_crc *crc;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
crc = devm_kzalloc(dev, sizeof(*crc), GFP_KERNEL);
|
|
|
|
if (!crc)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
crc->dev = dev;
|
|
|
|
|
2019-08-02 16:28:07 +03:00
|
|
|
crc->regs = devm_platform_ioremap_resource(pdev, 0);
|
2017-03-21 18:13:28 +03:00
|
|
|
if (IS_ERR(crc->regs)) {
|
|
|
|
dev_err(dev, "Cannot map CRC IO\n");
|
|
|
|
return PTR_ERR(crc->regs);
|
|
|
|
}
|
|
|
|
|
|
|
|
crc->clk = devm_clk_get(dev, NULL);
|
|
|
|
if (IS_ERR(crc->clk)) {
|
|
|
|
dev_err(dev, "Could not get clock\n");
|
|
|
|
return PTR_ERR(crc->clk);
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = clk_prepare_enable(crc->clk);
|
|
|
|
if (ret) {
|
|
|
|
dev_err(crc->dev, "Failed to enable clock\n");
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-06-26 15:52:46 +03:00
|
|
|
pm_runtime_set_autosuspend_delay(dev, CRC_AUTOSUSPEND_DELAY);
|
|
|
|
pm_runtime_use_autosuspend(dev);
|
|
|
|
|
|
|
|
pm_runtime_get_noresume(dev);
|
|
|
|
pm_runtime_set_active(dev);
|
2020-05-12 17:11:12 +03:00
|
|
|
pm_runtime_irq_safe(dev);
|
2018-06-26 15:52:46 +03:00
|
|
|
pm_runtime_enable(dev);
|
|
|
|
|
2020-05-12 17:11:13 +03:00
|
|
|
spin_lock_init(&crc->lock);
|
|
|
|
|
2017-03-21 18:13:28 +03:00
|
|
|
platform_set_drvdata(pdev, crc);
|
|
|
|
|
|
|
|
spin_lock(&crc_list.lock);
|
|
|
|
list_add(&crc->list, &crc_list.dev_list);
|
|
|
|
spin_unlock(&crc_list.lock);
|
|
|
|
|
2020-05-12 17:11:11 +03:00
|
|
|
mutex_lock(&refcnt_lock);
|
|
|
|
if (!refcnt) {
|
|
|
|
ret = crypto_register_shashes(algs, ARRAY_SIZE(algs));
|
|
|
|
if (ret) {
|
|
|
|
mutex_unlock(&refcnt_lock);
|
|
|
|
dev_err(dev, "Failed to register\n");
|
|
|
|
clk_disable_unprepare(crc->clk);
|
|
|
|
return ret;
|
|
|
|
}
|
2017-03-21 18:13:28 +03:00
|
|
|
}
|
2020-05-12 17:11:11 +03:00
|
|
|
refcnt++;
|
|
|
|
mutex_unlock(&refcnt_lock);
|
2017-03-21 18:13:28 +03:00
|
|
|
|
|
|
|
dev_info(dev, "Initialized\n");
|
|
|
|
|
2018-06-26 15:52:46 +03:00
|
|
|
pm_runtime_put_sync(dev);
|
|
|
|
|
2017-03-21 18:13:28 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int stm32_crc_remove(struct platform_device *pdev)
|
|
|
|
{
|
|
|
|
struct stm32_crc *crc = platform_get_drvdata(pdev);
|
2018-06-26 15:52:46 +03:00
|
|
|
int ret = pm_runtime_get_sync(crc->dev);
|
|
|
|
|
2022-03-17 16:16:13 +03:00
|
|
|
if (ret < 0) {
|
|
|
|
pm_runtime_put_noidle(crc->dev);
|
2018-06-26 15:52:46 +03:00
|
|
|
return ret;
|
2022-03-17 16:16:13 +03:00
|
|
|
}
|
2017-03-21 18:13:28 +03:00
|
|
|
|
|
|
|
spin_lock(&crc_list.lock);
|
|
|
|
list_del(&crc->list);
|
|
|
|
spin_unlock(&crc_list.lock);
|
|
|
|
|
2020-05-12 17:11:11 +03:00
|
|
|
mutex_lock(&refcnt_lock);
|
|
|
|
if (!--refcnt)
|
|
|
|
crypto_unregister_shashes(algs, ARRAY_SIZE(algs));
|
|
|
|
mutex_unlock(&refcnt_lock);
|
2017-03-21 18:13:28 +03:00
|
|
|
|
2018-06-26 15:52:46 +03:00
|
|
|
pm_runtime_disable(crc->dev);
|
|
|
|
pm_runtime_put_noidle(crc->dev);
|
|
|
|
|
2017-03-21 18:13:28 +03:00
|
|
|
clk_disable_unprepare(crc->clk);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-05-12 17:11:12 +03:00
|
|
|
static int __maybe_unused stm32_crc_suspend(struct device *dev)
|
2018-06-26 15:52:46 +03:00
|
|
|
{
|
|
|
|
struct stm32_crc *crc = dev_get_drvdata(dev);
|
2020-05-12 17:11:12 +03:00
|
|
|
int ret;
|
2018-06-26 15:52:46 +03:00
|
|
|
|
2020-05-12 17:11:12 +03:00
|
|
|
ret = pm_runtime_force_suspend(dev);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
clk_unprepare(crc->clk);
|
2018-06-26 15:52:46 +03:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-05-12 17:11:12 +03:00
|
|
|
static int __maybe_unused stm32_crc_resume(struct device *dev)
|
2018-06-26 15:52:46 +03:00
|
|
|
{
|
|
|
|
struct stm32_crc *crc = dev_get_drvdata(dev);
|
|
|
|
int ret;
|
|
|
|
|
2020-05-12 17:11:12 +03:00
|
|
|
ret = clk_prepare(crc->clk);
|
2018-06-26 15:52:46 +03:00
|
|
|
if (ret) {
|
2020-05-12 17:11:12 +03:00
|
|
|
dev_err(crc->dev, "Failed to prepare clock\n");
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return pm_runtime_force_resume(dev);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int __maybe_unused stm32_crc_runtime_suspend(struct device *dev)
|
|
|
|
{
|
|
|
|
struct stm32_crc *crc = dev_get_drvdata(dev);
|
|
|
|
|
|
|
|
clk_disable(crc->clk);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int __maybe_unused stm32_crc_runtime_resume(struct device *dev)
|
|
|
|
{
|
|
|
|
struct stm32_crc *crc = dev_get_drvdata(dev);
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = clk_enable(crc->clk);
|
|
|
|
if (ret) {
|
|
|
|
dev_err(crc->dev, "Failed to enable clock\n");
|
2018-06-26 15:52:46 +03:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct dev_pm_ops stm32_crc_pm_ops = {
|
2020-05-12 17:11:12 +03:00
|
|
|
SET_SYSTEM_SLEEP_PM_OPS(stm32_crc_suspend,
|
|
|
|
stm32_crc_resume)
|
2018-06-26 15:52:46 +03:00
|
|
|
SET_RUNTIME_PM_OPS(stm32_crc_runtime_suspend,
|
|
|
|
stm32_crc_runtime_resume, NULL)
|
|
|
|
};
|
|
|
|
|
2017-03-21 18:13:28 +03:00
|
|
|
static const struct of_device_id stm32_dt_ids[] = {
|
|
|
|
{ .compatible = "st,stm32f7-crc", },
|
|
|
|
{},
|
|
|
|
};
|
2017-04-25 17:35:42 +03:00
|
|
|
MODULE_DEVICE_TABLE(of, stm32_dt_ids);
|
2017-03-21 18:13:28 +03:00
|
|
|
|
|
|
|
static struct platform_driver stm32_crc_driver = {
|
|
|
|
.probe = stm32_crc_probe,
|
|
|
|
.remove = stm32_crc_remove,
|
|
|
|
.driver = {
|
|
|
|
.name = DRIVER_NAME,
|
2018-06-26 15:52:46 +03:00
|
|
|
.pm = &stm32_crc_pm_ops,
|
2017-03-21 18:13:28 +03:00
|
|
|
.of_match_table = stm32_dt_ids,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
module_platform_driver(stm32_crc_driver);
|
|
|
|
|
|
|
|
MODULE_AUTHOR("Fabien Dessenne <fabien.dessenne@st.com>");
|
|
|
|
MODULE_DESCRIPTION("STMicrolectronics STM32 CRC32 hardware driver");
|
|
|
|
MODULE_LICENSE("GPL");
|