0001
0002
0003
0004
0005
0006
0007
0008
0009 #include "sl3516-ce.h"
0010 #include <linux/pm_runtime.h>
0011 #include <linux/hw_random.h>
0012
0013 static int sl3516_ce_rng_read(struct hwrng *rng, void *buf, size_t max, bool wait)
0014 {
0015 struct sl3516_ce_dev *ce;
0016 u32 *data = buf;
0017 size_t read = 0;
0018 int err;
0019
0020 ce = container_of(rng, struct sl3516_ce_dev, trng);
0021
0022 #ifdef CONFIG_CRYPTO_DEV_SL3516_DEBUG
0023 ce->hwrng_stat_req++;
0024 ce->hwrng_stat_bytes += max;
0025 #endif
0026
0027 err = pm_runtime_get_sync(ce->dev);
0028 if (err < 0) {
0029 pm_runtime_put_noidle(ce->dev);
0030 return err;
0031 }
0032
0033 while (read < max) {
0034 *data = readl(ce->base + IPSEC_RAND_NUM_REG);
0035 data++;
0036 read += 4;
0037 }
0038
0039 pm_runtime_put(ce->dev);
0040
0041 return read;
0042 }
0043
0044 int sl3516_ce_rng_register(struct sl3516_ce_dev *ce)
0045 {
0046 int ret;
0047
0048 ce->trng.name = "SL3516 Crypto Engine RNG";
0049 ce->trng.read = sl3516_ce_rng_read;
0050 ce->trng.quality = 700;
0051
0052 ret = hwrng_register(&ce->trng);
0053 if (ret)
0054 dev_err(ce->dev, "Fail to register the RNG\n");
0055 return ret;
0056 }
0057
0058 void sl3516_ce_rng_unregister(struct sl3516_ce_dev *ce)
0059 {
0060 hwrng_unregister(&ce->trng);
0061 }