^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) #include "sun4i-ss.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) int sun4i_ss_prng_seed(struct crypto_rng *tfm, const u8 *seed,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) unsigned int slen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) struct sun4i_ss_alg_template *algt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) struct rng_alg *alg = crypto_rng_alg(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) algt = container_of(alg, struct sun4i_ss_alg_template, alg.rng);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) memcpy(algt->ss->seed, seed, slen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) int sun4i_ss_prng_generate(struct crypto_rng *tfm, const u8 *src,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) unsigned int slen, u8 *dst, unsigned int dlen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) struct sun4i_ss_alg_template *algt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) struct rng_alg *alg = crypto_rng_alg(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) int i, err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) u32 v;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) u32 *data = (u32 *)dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) const u32 mode = SS_OP_PRNG | SS_PRNG_CONTINUE | SS_ENABLED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) size_t len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) struct sun4i_ss_ctx *ss;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) unsigned int todo = (dlen / 4) * 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) algt = container_of(alg, struct sun4i_ss_alg_template, alg.rng);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) ss = algt->ss;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) err = pm_runtime_get_sync(ss->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) spin_lock_bh(&ss->slock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) writel(mode, ss->base + SS_CTL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) while (todo > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) /* write the seed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) for (i = 0; i < SS_SEED_LEN / BITS_PER_LONG; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) writel(ss->seed[i], ss->base + SS_KEY0 + i * 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) /* Read the random data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) len = min_t(size_t, SS_DATA_LEN / BITS_PER_BYTE, todo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) readsl(ss->base + SS_TXFIFO, data, len / 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) data += len / 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) todo -= len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) /* Update the seed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) for (i = 0; i < SS_SEED_LEN / BITS_PER_LONG; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) v = readl(ss->base + SS_KEY0 + i * 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) ss->seed[i] = v;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) writel(0, ss->base + SS_CTL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) spin_unlock_bh(&ss->slock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) pm_runtime_put(ss->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) }