Skip to content

Commit

Permalink
arch/risc-v/src/mpfs/mpfs_ddr.c: Add a simple prng for memory trainin…
Browse files Browse the repository at this point in the history
…g code

Implement the previously empty mpfs_ddr_rand with adapted "seiran128" code
from https://github.com/andanteyk/prng-seiran

This implements a non-secure prng, which is minimal in size. The DDR training
doesn't need cryptographically secure prng, and linking in the NuttX crypto
would increase the code size significantly for bootloaders.

Signed-off-by: Jukka Laitinen <[email protected]>
  • Loading branch information
jlaitine committed Aug 25, 2023
1 parent 296a33c commit ed46b7c
Showing 1 changed file with 24 additions and 6 deletions.
30 changes: 24 additions & 6 deletions arch/risc-v/src/mpfs/mpfs_ddr.c
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,14 @@ static const uint8_t refclk_offsets[][5] =

#endif

/* State of the seiran128 PRNG, with initial seed */

static uint64_t prng_state[2] =
{
0x6c64f673ed93b6cc,
0x97c703d5f6c9d72b
};

/****************************************************************************
* Private Functions
****************************************************************************/
Expand Down Expand Up @@ -2131,19 +2139,29 @@ static int mpfs_write_calibration_using_mtc(struct mpfs_ddr_priv_s *priv)
* Name: mpfs_ddr_rand
*
* Description:
* This should return a random value.
* This is adapted from seiran128
* (https://github.com/andanteyk/prng-seiran)
*
* Returned Value:
* Always zero at the moment.
*
* Assumptions/Limitations:
* This doesn't return random values at the moment.
* Non-cryptographically secure pseudo random number
*
****************************************************************************/

static inline uint64_t rotl(uint64_t x, int k)
{
return (x << k) | (x >> (-k & 0x3f));
}

static int mpfs_ddr_rand(void)
{
return 0;
uint64_t s0 = prng_state[0];
uint64_t s1 = prng_state[1];
uint64_t result = rotl((s0 + s1) * 9, 29) + s0;

prng_state[0] = s0 ^ rotl(s1, 29);
prng_state[1] = s0 ^ (s1 << 9);

return (int)result;
}

/****************************************************************************
Expand Down

0 comments on commit ed46b7c

Please sign in to comment.