<div dir="ltr"><div><div><div>Hi,<br></div>I have updated the code, please have a look.<br>From 74b8f4f5b9dd929b71ed5fb9dd0bc721a6f27a28 Mon Sep 17 00:00:00 2001<br>From: Udit agarwal <<a href="mailto:dev.madaari@gmail.com">dev.madaari@gmail.com</a>><br>Date: Wed, 7 Mar 2018 15:52:13 +0530<br>Subject: [PATCH] Added getentropy support to beagle BSP<br><br>---<br> bsps/arm/include/libcpu/am335x.h | 34 ++++++++-<br> c/src/lib/libbsp/arm/beagle/Makefile.am | 4 +<br> .../libbsp/arm/beagle/getentropy/bbb_getentropy.c | 88 ++++++++++++++++++++++<br> 3 files changed, 125 insertions(+), 1 deletion(-)<br> create mode 100644 c/src/lib/libbsp/arm/beagle/getentropy/bbb_getentropy.c<br><br>diff --git a/bsps/arm/include/libcpu/am335x.h b/bsps/arm/include/libcpu/am335x.h<br>index 367e97c..92af6dc 100644<br>--- a/bsps/arm/include/libcpu/am335x.h<br>+++ b/bsps/arm/include/libcpu/am335x.h<br>@@ -14,6 +14,9 @@<br> * Modified by Ben Gras <<a href="mailto:beng@shrike-systems.com">beng@shrike-systems.com</a>> to add lots<br> * of beagleboard/beaglebone definitions, delete lpc32xx specific<br> * ones, and merge with some other header files.<br>+ *<br>+ * Modified by Udit agarwal <<a href="mailto:dev.madaari@gmail.com">dev.madaari@gmail.com</a>> to add random<br>+ * number generating module definitions along with register structure.<br> */<br> <br> #if !defined(_AM335X_H_)<br>@@ -701,4 +704,33 @@<br> #define AM335X_CM_PER_OCPWP_L3_CLKSTCTRL_CLKACTIVITY_OCPWP_L4_GCLK (0x00000020u)<br> #define AM335X_I2C_INT_STOP_CONDITION AM335X_I2C_IRQSTATUS_BF<br> <br>-#endif<br>+/* TRNG Register */<br>+<br>+/* RNG base address */<br>+#define RNG_BASE 0x48310000<br>+/* RNG clock control */<br>+#define CM_PER_RNG_CLKCTRL (AM335X_CM_PER_ADDR | (9 << 4))<br>+/* rng module clock status bits */<br>+#define AM335X_CLK_RNG_BIT_MASK (0x30000)<br>+/* Offset from RNG base for output ready flag */<br>+#define RNG_STATUS_RDY (1u << 0) <br>+/* Offset from RNG base for FRO related error */<br>+#define RNG_STATUS_ERR (1u << 1) <br>+/* Offset from RNG base for clock status */<br>+#define RNG_STATUS_CLK (1u << 31) <br>+/* enable module */<br>+#define AM335X_RNG_ENABLE (1 << 10) <br>+<br>+/* TRNG register structure */<br>+struct bbb_trng_register <br>+{<br>+ uint64_t output; /*00*/ <br>+ uint32_t status; /*08*/ <br>+ uint32_t irq_en; /*0c*/ <br>+ uint32_t status_clr; /*10*/ <br>+ uint32_t control; /*14*/ <br>+ uint32_t config; /*18*/ <br>+};<br>+typedef struct bbb_trng_register bbb_trng_register;<br>+<br>+#endif<br>\ No newline at end of file<br>diff --git a/c/src/lib/libbsp/arm/beagle/Makefile.am b/c/src/lib/libbsp/arm/beagle/Makefile.am<br>index 8251660..bf92081 100644<br>--- a/c/src/lib/libbsp/arm/beagle/Makefile.am<br>+++ b/c/src/lib/libbsp/arm/beagle/Makefile.am<br>@@ -88,9 +88,13 @@ libbsp_a_SOURCES += gpio/bbb-gpio.c<br> #pwm<br> libbsp_a_SOURCES += pwm/pwm.c<br> <br>+#getentropy<br>+libbsp_a_SOURCES += getentropy/bbb_getentropy.c<br>+<br> #RTC<br> libbsp_a_SOURCES += rtc.c<br> libbsp_a_SOURCES += ../../shared/tod.c<br>+<br> # Clock<br> libbsp_a_SOURCES += clock.c<br> libbsp_a_SOURCES += ../../shared/clockdrv_shell.h<br>diff --git a/c/src/lib/libbsp/arm/beagle/getentropy/bbb_getentropy.c b/c/src/lib/libbsp/arm/beagle/getentropy/bbb_getentropy.c<br>new file mode 100644<br>index 0000000..5b7c5d0<br>--- /dev/null<br>+++ b/c/src/lib/libbsp/arm/beagle/getentropy/bbb_getentropy.c<br>@@ -0,0 +1,88 @@<br>+/**<br>+* @file<br>+*<br>+* @ingroup arm_beagle<br>+*<br>+* @brief Getentropy implementation on BeagleBone Black BSP<br>+*/<br>+<br>+/*<br>+* Copyright (c) 2018 Udit kumar agarwal <dev.madaari at <a href="http://gmail.com">gmail.com</a>><br>+*<br>+* The license and distribution terms for this file may be<br>+* found in the file LICENSE in this distribution or at<br>+* <a href="http://www.rtems.org/license/LICENSE">http://www.rtems.org/license/LICENSE</a>.<br>+*/<br>+<br>+#include <libcpu/am335x.h><br>+#include <unistd.h><br>+#include <string.h><br>+#include <rtems/sysinit.h><br>+#include <rtems/types.h><br>+<br>+/* max refill 34 * 256 cycles */<br>+#define AM335X_RNG_MAX_REFILL (34 << 16) <br>+/* min refill 33 * 64 cycles */<br>+#define AM335X_RNG_MIN_REFILL (33 << 0) <br>+/* startup 33 * 256 cycles */<br>+#define AM335X_RNG_STARTUP_CYCLES (33 << 16) <br>+<br>+/* maximun and minimum refill cycle sets the number of samples to be taken <br>+ from FRO to generate random number */<br>+static void am335x_rng_enable(bbb_trng_register *rng)<br>+{<br>+ rng->control = rng->config = 0;<br>+ rng->config |= AM335X_RNG_MIN_REFILL | AM335X_RNG_MAX_REFILL ;<br>+ rng->control |= AM335X_RNG_STARTUP_CYCLES | AM335X_RNG_ENABLE ;<br>+}<br>+<br>+static void am335x_rng_clock_enable(void)<br>+{<br>+ *(volatile uint8_t *) CM_PER_RNG_CLKCTRL = 2;<br>+ while( *(volatile uint32_t *) CM_PER_RNG_CLKCTRL & AM335X_CLK_RNG_BIT_MASK ) {}<br>+}<br>+<br>+static uint64_t trng_getranddata(bbb_trng_register *rng)<br>+{<br>+ uint64_t output = rng->output;<br>+ return output;<br>+}<br>+<br>+int getentropy(void *ptr, size_t n)<br>+{<br>+ volatile const bbb_trng_register *rng = (bbb_trng_register*) RNG_BASE;<br>+ am335x_rng_enable(rng);<br>+ while (n > 0) <br>+ {<br>+ uint64_t random;<br>+ size_t copy;<br>+<br>+ /* wait untill RNG becomes ready with next set of random data */<br>+ while( ( rng->status & RNG_STATUS_RDY ) == 0 ); <br>+<br>+ random = trng_getranddata(rng);<br>+<br>+ /* Checking for error by masking all bits other then error bit in status register */<br>+ if( ((rng->status & RNG_STATUS_ERR)>>1) == 0)<br>+ {<br>+ /* clear the status flag after reading to generate new random value*/<br>+ rng->status_clr = RNG_STATUS_RDY;<br>+ copy = sizeof(random);<br>+ if ( n < copy ) <br>+ {<br>+ copy = n;<br>+ }<br>+ memcpy(ptr, &random, copy);<br>+ n -= copy;<br>+ ptr = (char*)ptr + copy;<br>+ }<br>+ }<br>+<br>+ return 0;<br>+}<br>+<br>+RTEMS_SYSINIT_ITEM(<br>+ am335x_rng_clock_enable,<br>+ RTEMS_SYSINIT_DEVICE_DRIVERS,<br>+ RTEMS_SYSINIT_ORDER_LAST<br>+);<br>\ No newline at end of file<br>-- <br>1.9.1<br><br></div>Regards,<br></div>Udit agarwal<br></div><div class="gmail_extra"><br><div class="gmail_quote">On Tue, Mar 6, 2018 at 11:05 PM, Gedare Bloom <span dir="ltr"><<a href="mailto:gedare@rtems.org" target="_blank">gedare@rtems.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="HOEnZb"><div class="h5">On Tue, Mar 6, 2018 at 9:43 AM, Udit agarwal <<a href="mailto:dev.madaari@gmail.com">dev.madaari@gmail.com</a>> wrote:<br>
> Hi,<br>
> Here's the updated code(I have also attached the patch PFA):<br>
><br>
> From 96e6e1bfd8cffeef5d309eb0a07fe2<wbr>bfd086ef0a Mon Sep 17 00:00:00 2001<br>
> From: Udit agarwal <<a href="mailto:dev.madaari@gmail.com">dev.madaari@gmail.com</a>><br>
> Date: Tue, 6 Mar 2018 20:07:44 +0530<br>
> Subject: [PATCH] Added getentropy support to BBB BSP<br>
><br>
> ---<br>
> bsps/arm/beagle/<a href="http://headers.am" rel="noreferrer" target="_blank">headers.am</a> | 1 +<br>
> bsps/arm/beagle/include/bsp/<wbr>bbb_getentropy.h | 57 +++++++++++++++<br>
> bsps/arm/include/libcpu/<wbr>am335x.h | 16 +++++<br>
> c/src/lib/libbsp/arm/beagle/<wbr>Makefile.am | 4 ++<br>
> .../libbsp/arm/beagle/<wbr>getentropy/bbb_getentropy.c | 80<br>
> ++++++++++++++++++++++<br>
> 5 files changed, 158 insertions(+)<br>
> create mode 100644 bsps/arm/beagle/include/bsp/<wbr>bbb_getentropy.h<br>
> create mode 100644 c/src/lib/libbsp/arm/beagle/<wbr>getentropy/bbb_getentropy.c<br>
><br>
> diff --git a/bsps/arm/beagle/<a href="http://headers.am" rel="noreferrer" target="_blank">headers.am</a> b/bsps/arm/beagle/<a href="http://headers.am" rel="noreferrer" target="_blank">headers.am</a><br>
> index 6692d0b..ac4ff7c 100644<br>
> --- a/bsps/arm/beagle/<a href="http://headers.am" rel="noreferrer" target="_blank">headers.am</a><br>
> +++ b/bsps/arm/beagle/<a href="http://headers.am" rel="noreferrer" target="_blank">headers.am</a><br>
> @@ -12,3 +12,4 @@ include_bsp_HEADERS +=<br>
> ../../../../../../bsps/arm/<wbr>beagle/include/bsp/bbb-pwm.h<br>
> include_bsp_HEADERS +=<br>
> ../../../../../../bsps/arm/<wbr>beagle/include/bsp/<wbr>beagleboneblack.h<br>
> include_bsp_HEADERS += ../../../../../../bsps/arm/<wbr>beagle/include/bsp/i2c.h<br>
> include_bsp_HEADERS += ../../../../../../bsps/arm/<wbr>beagle/include/bsp/irq.h<br>
> +include_bsp_HEADERS +=<br>
> ../../../../../../bsps/arm/<wbr>beagle/include/bsp/bbb_<wbr>getentropy.h<br>
> diff --git a/bsps/arm/beagle/include/bsp/<wbr>bbb_getentropy.h<br>
> b/bsps/arm/beagle/include/bsp/<wbr>bbb_getentropy.h<br>
> new file mode 100644<br>
> index 0000000..5e46f89<br>
> --- /dev/null<br>
> +++ b/bsps/arm/beagle/include/bsp/<wbr>bbb_getentropy.h<br>
> @@ -0,0 +1,57 @@<br>
> +/**<br>
> + * @file<br>
> + *<br>
> + * @ingroup arm_beagle<br>
> + *<br>
> + * @brief TRNG Definations<br>
> + */<br>
> +<br>
> +/*<br>
> + * Copyright (c) 2018 Udit kumar agarwal <dev.madaari at <a href="http://gmail.com" rel="noreferrer" target="_blank">gmail.com</a>><br>
> + *<br>
> + * The license and distribution terms for this file may be<br>
> + * found in the file LICENSE in this distribution or at<br>
> + * <a href="http://www.rtems.org/license/LICENSE" rel="noreferrer" target="_blank">http://www.rtems.org/license/<wbr>LICENSE</a>.<br>
> + */<br>
> +<br>
> +<br>
> +#ifndef _TRNG_<br>
> +#define _TRNG_<br>
> +<br>
> +/*---------------------------<wbr>------------------------------<wbr>---------------------<br>
</div></div>Please look at similar files in RTEMS for how to add comments.<br>
We do not use this style /* --------------------<br>
And we don't put this kind of comment about the file sections.<br>
<span class=""><br>
> + * Headers<br>
> +<br>
> *-----------------------------<wbr>------------------------------<wbr>-----------------*/<br>
> +<br>
> +#include <libcpu/am335x.h><br>
> +<br>
> +<br>
> /*----------------------------<wbr>------------------------------<wbr>--------------------<br>
> + * Register structure<br>
> +<br>
> *-----------------------------<wbr>------------------------------<wbr>-----------------*/<br>
> +<br>
> +struct rng {<br>
> + uint64_t output; /*00*/<br>
> + uint32_t status; /*08*/<br>
> + uint32_t irq_en; /*0c*/<br>
> + uint32_t status_clr; /*10*/<br>
> + uint32_t control; /*14*/<br>
> + uint32_t config; /*18*/<br>
> +};<br>
</span>for an exported header, the struct namespace needs to be user<br>
friendly. See similar files at this layer for how to name things.<br>
Probably, this can be defined in the am335x.h header?<br>
<span class=""><br>
> +<br>
> +typedef volatile struct rng *rng_ref_t;<br>
</span>I'm not such a big fan of using the typedef to hide things like<br>
volatile or pointer notation. I don't see a need for this usually.<br>
<span class=""><br>
> +<br>
> +/*---------------------------<wbr>------------------------------<wbr>-------------------*/<br>
> +/* Exported functions<br>
> */<br>
> +/*---------------------------<wbr>------------------------------<wbr>-------------------*/<br>
> +<br>
> +/* configure and enable RNG module */<br>
> +static void am335x_rng_enable(rng_ref_t);<br>
> +/* enable module clock for random number generator */<br>
> +static void am335x_rng_clock_enable(void);<br>
> +/* outputs random data */<br>
> +static uint64_t trng_getranddata(rng_ref_t);<br>
<br>
</span>static keyword implies these are not exported.<br>
<span class=""><br>
> +/* Copy random data of a specified size to the pointer supplied */<br>
> +int getentropy(void *ptr, size_t);<br>
</span>I guess getentropy() is already exported by a different header file<br>
(unistd.h) in libc? This doesn't belong here.<br>
<br>
I suspect this header file is not actually necessary.<br>
<br>
> +<br>
> +<br>
> +<br>
Don't use multiple blank lines in a row. Only one empty line is ever<br>
needed by RTEMS style.<br>
<span class=""><br>
> +#endif /* #ifndef _TRNG_ */<br>
> \ No newline at end of file<br>
> diff --git a/bsps/arm/include/libcpu/<wbr>am335x.h<br>
> b/bsps/arm/include/libcpu/<wbr>am335x.h<br>
> index 367e97c..8b58f1a 100644<br>
> --- a/bsps/arm/include/libcpu/<wbr>am335x.h<br>
> +++ b/bsps/arm/include/libcpu/<wbr>am335x.h<br>
> @@ -14,6 +14,9 @@<br>
> * Modified by Ben Gras <<a href="mailto:beng@shrike-systems.com">beng@shrike-systems.com</a>> to add lots<br>
> * of beagleboard/beaglebone definitions, delete lpc32xx specific<br>
> * ones, and merge with some other header files.<br>
> + *<br>
> + * Modified by Udit agarwal <<a href="mailto:dev.madaari@gmail.com">dev.madaari@gmail.com</a>> to add random<br>
> + * number generating module definations<br>
</span>typo: definitions<br>
add a period (full stop).<br>
<span class=""><br>
> */<br>
><br>
> #if !defined(_AM335X_H_)<br>
> @@ -701,4 +704,17 @@<br>
> #define AM335X_CM_PER_OCPWP_L3_<wbr>CLKSTCTRL_CLKACTIVITY_OCPWP_<wbr>L4_GCLK<br>
> (0x00000020u)<br>
> #define AM335X_I2C_INT_STOP_CONDITION AM335X_I2C_IRQSTATUS_BF<br>
><br>
> +/* TRNG Register */<br>
> +<br>
> +/* RNG base address */<br>
> +#define RNG_BASE 0x48310000<br>
> +/* RNG clock control */<br>
> +#define CM_PER_RNG_CLKCTRL AM335X_CM_PER_ADDR | 9<<4<br>
</span>I'd put parentheses around this to avoid any problems with macro<br>
expansions and order of operations.<br>
<div><div class="h5"><br>
> +/* Offset from RNG base for output ready flag */<br>
> +#define RNG_STATUS_RDY (1u << 0)<br>
> +/* Offset from RNG base for FRO related error*/<br>
> +#define RNG_STATUS_ERR (1u << 1)<br>
> +/* Offset from RNG base for clock status */<br>
> +#define RNG_STATUS_CLK (1u << 31)<br>
> +<br>
> #endif<br>
> diff --git a/c/src/lib/libbsp/arm/beagle/<wbr>Makefile.am<br>
> b/c/src/lib/libbsp/arm/beagle/<wbr>Makefile.am<br>
> index 8251660..bf92081 100644<br>
> --- a/c/src/lib/libbsp/arm/beagle/<wbr>Makefile.am<br>
> +++ b/c/src/lib/libbsp/arm/beagle/<wbr>Makefile.am<br>
> @@ -88,9 +88,13 @@ libbsp_a_SOURCES += gpio/bbb-gpio.c<br>
> #pwm<br>
> libbsp_a_SOURCES += pwm/pwm.c<br>
><br>
> +#getentropy<br>
> +libbsp_a_SOURCES += getentropy/bbb_getentropy.c<br>
> +<br>
> #RTC<br>
> libbsp_a_SOURCES += rtc.c<br>
> libbsp_a_SOURCES += ../../shared/tod.c<br>
> +<br>
> # Clock<br>
> libbsp_a_SOURCES += clock.c<br>
> libbsp_a_SOURCES += ../../shared/clockdrv_shell.h<br>
> diff --git a/c/src/lib/libbsp/arm/beagle/<wbr>getentropy/bbb_getentropy.c<br>
> b/c/src/lib/libbsp/arm/beagle/<wbr>getentropy/bbb_getentropy.c<br>
> new file mode 100644<br>
> index 0000000..cab2102<br>
> --- /dev/null<br>
> +++ b/c/src/lib/libbsp/arm/beagle/<wbr>getentropy/bbb_getentropy.c<br>
> @@ -0,0 +1,80 @@<br>
> +/**<br>
> +* @file<br>
> +*<br>
> +* @ingroup arm_beagle<br>
> +*<br>
> +* @brief Getentropy implementation on BeagleBone Black BSP<br>
> +*/<br>
> +<br>
> +/*<br>
> +* Copyright (c) 2018 Udit kumar agarwal <dev.madaari at <a href="http://gmail.com" rel="noreferrer" target="_blank">gmail.com</a>><br>
> +*<br>
> +* The license and distribution terms for this file may be<br>
> +* found in the file LICENSE in this distribution or at<br>
> +* <a href="http://www.rtems.org/license/LICENSE" rel="noreferrer" target="_blank">http://www.rtems.org/license/<wbr>LICENSE</a>.<br>
> +*/<br>
> +<br>
> +#include <unistd.h><br>
> +#include <stdint.h><br>
> +#include <string.h><br>
> +#include <rtems/sysinit.h><br>
> +<br>
> +<br>
> +/* maximun and minimun refill cycle sets the number of samples to be taken<br>
</div></div>typo: minimum<br>
<span class="">> + from FRO to generate random number */<br>
> +static void am335x_rng_enable(rng_ref_t rng){<br>
</span>put { on a new line.<br>
<span class=""><br>
> + rng->config = 0<br>
> + | 34 << 16 /* max refill 34 * 256 cycles */<br>
</span>indent broken lines, but<br>
<span class="">> + | 33 << 0; /* min refill 33 * 64 cycles */<br>
</span>You can fit this on one line, remove the comments and put them above<br>
the line, e.g.<br>
rng->config = 34<<16 | 33;<br>
<br>
I would prefer not to have these hard-coded values though it is<br>
fragile. what means 34, 16, and 33?<br>
#define AM335X_RNG_MAX_REFILL (34 << 16)<br>
#define AM335X_RNG_MIN_REFILL (33)<br>
#define AM335X_RNG_CONFIG_REFILL (AM335X_RNG_MAX_REFILL | AM335X_RNG_MIN_REFILL)<br>
<br>
rng.config = rng.control = 0;<br>
rng.config |= AM335X_RNG_CONFIG_REFILL_TIME;<br>
<span class=""><br>
> + rng->control = 0<br>
> + | 33 << 16 /* startup 33 * 256 cycles */<br>
> + | 1 << 10; /* enable module */<br>
<br>
</span>rng.control |= AM335X_RNG_CONTROL_STARTUP;<br>
rng.control |= AM335X_RNG_CONTROL_ENABLE;<br>
<br>
for example is more readable. look around for other examples.<br>
<span class=""><br>
> +}<br>
> +<br>
> +static void am335x_rng_clock_enable(void)<br>
> +{<br>
> + *(volatile uint8_t *) CM_PER_RNG_CLKCTRL = 2;<br>
> + while( *(volatile uint32_t *) CM_PER_RNG_CLKCTRL & 0x30000 ) {}<br>
> +}<br>
> +<br>
> +static uint64_t trng_getranddata(rng_ref_t rng){<br>
> + uint64_t output = rng->output;<br>
> + return output;<br>
> +}<br>
> +<br>
> +int getentropy(void *ptr, size_t n)<br>
> +{<br>
> + rng_ref_t const rng = (rng_ref_t) RNG_BASE;<br>
> + am335x_rng_enable(rng);<br>
> + while (n > 0) {<br>
> + uint64_t random;<br>
</span>indent blocks of code.<br>
<span class=""><br>
> + size_t copy;<br>
> +<br>
> + /* wait untill RNG becomes ready with next set of random data */<br>
> + while( ( rng->status & RNG_STATUS_RDY ) == 0 );<br>
> +<br>
> + random = trng_getranddata(rng);<br>
> +<br>
> + /* Checking for error by masking all bits other then error bit in<br>
> status register */<br>
> + if( ((rng->status & RNG_STATUS_ERR)>>1) == 0){<br>
> +<br>
> + /* clear the status flag after reading to generate new random<br>
> value*/<br>
> + rng->status_clr = RNG_STATUS_RDY;<br>
> + copy = sizeof(random);<br>
> + if ( n < copy ) {<br>
> + copy = n;<br>
</span>ditto.<br>
<span class=""><br>
> + }<br>
> + memcpy(ptr, &random, copy);<br>
> + n -= copy;<br>
> + ptr += copy;<br>
</span>pointer arithmetic on void pointer is not legal. You can portably use<br>
ptr = (char*)ptr + copy;<br>
<div class="HOEnZb"><div class="h5"><br>
> + }<br>
> + }<br>
> +<br>
> + return 0;<br>
> +}<br>
> +<br>
> +RTEMS_SYSINIT_ITEM(<br>
> + am335x_rng_clock_enable,<br>
> + RTEMS_SYSINIT_DEVICE_DRIVERS,<br>
> + RTEMS_SYSINIT_ORDER_LAST<br>
> +);<br>
> \ No newline at end of file<br>
> --<br>
> 1.9.1<br>
><br>
> Regards,<br>
> Udit Agarwal<br>
><br>
> On Tue, Mar 6, 2018 at 12:50 AM, Christian Mauderer <<a href="mailto:list@c-mauderer.de">list@c-mauderer.de</a>><br>
> wrote:<br>
>><br>
>> Am 05.03.2018 um 14:51 schrieb Udit agarwal:<br>
>> > Hi,<br>
>> > I tried implementing getentropy on BBB, below is the patch. Please have<br>
>> > a look.<br>
>> > I followed these(1<br>
>> > <<a href="https://e2e.ti.com/support/arm/sitara_arm/f/791/t/355064" rel="noreferrer" target="_blank">https://e2e.ti.com/support/<wbr>arm/sitara_arm/f/791/t/355064</a>> & 2<br>
>> ><br>
>> > <<a href="http://ftp.netbsd.org/pub/NetBSD/NetBSD-current/src/sys/arch/arm/omap/am335x_trng.c" rel="noreferrer" target="_blank">http://ftp.netbsd.org/pub/<wbr>NetBSD/NetBSD-current/src/sys/<wbr>arch/arm/omap/am335x_trng.c</a>>)<br>
>> > links for code reference. and this<br>
>> ><br>
>> > <<a href="https://docs.google.com/spreadsheets/d/1VpghMlLtrWQIrcvCsRg3ueRZkX0eGSQkFnzjfsqrd8A/view#gid=0" rel="noreferrer" target="_blank">https://docs.google.com/<wbr>spreadsheets/d/<wbr>1VpghMlLtrWQIrcvCsRg3ueRZkX0eG<wbr>SQkFnzjfsqrd8A/view#gid=0</a>><br>
>> > for register reference. Moreover, what further configuration in RTEMS is<br>
>> > needed to execute and test this code along with getentropy() sample<br>
>> > testcode?<br>
>><br>
>> Hello Udit,<br>
>><br>
>> regarding the patch: Please use 'git format-patch' for generating your<br>
>> patches and send them unchanged to the mailing list (for example with<br>
>> 'git send-email'. If you follow this workflow, it's easier to apply and<br>
>> test the patches. The preferred workflow for RTEMS is described here:<br>
>> <a href="https://devel.rtems.org/wiki/Developer/Git/Users#CreatingaPatch" rel="noreferrer" target="_blank">https://devel.rtems.org/wiki/<wbr>Developer/Git/Users#<wbr>CreatingaPatch</a><br>
>><br>
>> Regarding documentation: It seems that there is really only few<br>
>> documentation regarding the crypto part available. The FreeBSD driver is<br>
>> most likely your best source.<br>
>><br>
>> I added further comments down in your code.<br>
>><br>
>> Best regards<br>
>><br>
>> Christian<br>
>><br>
>> ><br>
>> ><br>
>> > + /*<br>
>> > + * Copyright (c) 2018 embedded brains GmbH. All rights reserved.<br>
>> > + *<br>
>> > + * embedded brains GmbH<br>
>> > + * Dornierstr. 4<br>
>> > + * 82178 Puchheim<br>
>> > + * Germany<br>
>> > + * <<a href="mailto:rtems@embedded-brains.de">rtems@embedded-brains.de</a> <mailto:<a href="mailto:rtems@embedded-brains.de">rtems@embedded-brains.<wbr>de</a>>><br>
>> > + *<br>
>><br>
>> Also it's nice if you add an embedded brains header, I don't think that<br>
>> I've seen you when I have been at work today. So please add your own<br>
>> name to the copyright line instead. With that you can even point to that<br>
>> file and tell everyone that it's from you.<br>
>><br>
>> An address isn't really necessary except if you want it for some reason<br>
>> (like PR in case of a company).<br>
>><br>
>> > + * The license and distribution terms for this file may be<br>
>> > + * found in the file LICENSE in this distribution or at<br>
>> > + * <a href="http://www.rtems.org/license/LICENSE" rel="noreferrer" target="_blank">http://www.rtems.org/license/<wbr>LICENSE</a>.<br>
>> > + */<br>
>> > +<br>
>> > + #include <libcpu/am335x.h><br>
>> > + #include <unistd.h><br>
>> > + #include <string.h><br>
>> > + #include <rtems/sysinit.h><br>
>> > +<br>
>> > + #define RNG_BASE 0x48310000<br>
>> > + #define CM_PER_RNG_CLKCTRL 0x44e00090<br>
>> > + #define RNG_STATUS_RDY (1u << 0) // output ready for reading<br>
>> > + #define RNG_STATUS_ERR (1u << 1) // FRO shutdown alarm<br>
>> > + #define RNG_STATUS_CLK (1u << 31) // module functional clock<br>
>> > active (no irq)<br>
>><br>
>> Please add address definitions and the bit masks to the<br>
>> bsps/arm/include/libcpu/<wbr>am335x.h instead of defining them here locally.<br>
>> Note that for the CM_PER_RNG_CLKCTRL, there is already a<br>
>> AM335X_CM_PER_ADDR that should be used as a base.<br>
>><br>
>> > +<br>
>> > + typedef volatile struct rng *rng_ref_t;<br>
>> > +<br>
>> > + struct rng {<br>
>> > + /*00*/ uint64_t output; //r-<br>
>> > + /*08*/ uint32_t status; //r-<br>
>> > + /*0c*/ uint32_t irq_en; //rw<br>
>> > + /*10*/ uint32_t status_clr; //-c<br>
>> > + /*14*/ uint32_t control; //rw<br>
>> > + /*18*/ uint32_t config; //rw<br>
>> > + };<br>
>><br>
>> Same is true here: It's a structure describing the registers. So it<br>
>> should be in the header instead.<br>
>><br>
>> It seems that you copied that from the forum post. Please always check<br>
>> the license when you copy code. We have to avoid that we get any code<br>
>> into RTEMS that isn't compatible with RTEMS license.<br>
>><br>
>> In this case the code should be obvious enough that - if you just<br>
>> rewrite it in RTEMS style, there shouldn't be a problem. But just<br>
>> remember that licensing is always a difficult topic.<br>
>><br>
>> > +<br>
>> > + static void TRNG_Enable(rng_ref_t rng){<br>
>> > + rng->config = 0<br>
>> > + | 34 << 16 // max refill 34 * 256 cycles<br>
>> > + | 33 << 0; // min refill 33 * 64 cycles<br>
>> > + rng->control = 0<br>
>> > + | 33 << 16 // startup 33 * 256 cycles<br>
>> > + | 1 << 10; // enable module<br>
>> > + }<br>
>><br>
>> Please use C-Style comments /* */ in all pure C code (most of RTEMS).<br>
>><br>
>> > +<br>
>> > + static void beagle_trng_clock_enable(void)<br>
>> > + {<br>
>> > + // enable module clock<br>
>> > + *(volatile uint8_t *) CM_PER_RNG_CLKCTRL = 2;<br>
>> > + while( *(volatile uint32_t *) CM_PER_RNG_CLKCTRL & 0x30000 ) {}<br>
>> > + }<br>
>> > +<br>
>> > + static uint64_t TRNG_GetRandData(){<br>
>> > + uint64_t output = rng->output;<br>
>><br>
>> 'rng' isn't defined here.<br>
>><br>
>> > + return output;<br>
>> > + }<br>
>> > +<br>
>> > + int getentropy(void *ptr, size_t n)<br>
>> > + {<br>
>> > + rng_ref_t const rng = (rng_ref_t) RNG_BASE;<br>
>> > + TRNG_Enable(rng);<br>
>> > + while (n > 0) {<br>
>> > + uint64_t random;<br>
>> > + size_t copy;<br>
>> > +<br>
>> > + while( ( rng->status & RNG_STATUS_RDY ) == 0 ); //wait<br>
>><br>
>> You should check specifically for the TRNG_STATUS_READY flag here like<br>
>> done in FreeBSD. I'm not sure what the other flags are but it's quite<br>
>> possible that there is also some error flag or similar. In that case it<br>
>> would not be a good idea to use the random number.<br>
>><br>
>> > +<br>
>> > + random = TRNG_GetRandData(rng);<br>
>><br>
>> Note that you don't have to copy the function names from the atsam<br>
>> exactly. In the atsam BSP, the library has provided these names.<br>
>> Otherwise it's quite unusual to start functions with upper case in RTEMS.<br>
>><br>
>> > +<br>
>> > + /*<br>
>> > + * Read TRNG status one more time to avoid race condition.<br>
>> > + * Otherwise we can read (and clear) an old ready status but<br>
>> > get<br>
>> > + * a new value. The ready status for this value wouldn't be<br>
>> > + * reset.<br>
>> > + */<br>
>><br>
>> That comment seems like it doesn't fit here any longer. You acknowledge<br>
>> the status instead in your next line. Just remove the comment.<br>
>><br>
>> > + rng->status_clr = RNG_STATUS_RDY;<br>
>> > +<br>
>> > + copy = sizeof(random);<br>
>> > + if (n < copy ) {<br>
>> > + copy = n;<br>
>> > + }<br>
>> > + memcpy(ptr, &random, copy);<br>
>> > + n -= copy;<br>
>> > + ptr += copy;<br>
>> > + }<br>
>> > +<br>
>> > + return 0;<br>
>> > + }<br>
>> > +<br>
>> > + RTEMS_SYSINIT_ITEM(<br>
>> > + beagle_trng_clock_enable,<br>
>> > + RTEMS_SYSINIT_DEVICE_DRIVERS,<br>
>> > + RTEMS_SYSINIT_ORDER_LAST<br>
>> > + );<br>
>> ><br>
>> > Regards,<br>
>> > Udit agarwal<br>
>> ><br>
>> ><br>
>> > ______________________________<wbr>_________________<br>
>> > devel mailing list<br>
>> > <a href="mailto:devel@rtems.org">devel@rtems.org</a><br>
>> > <a href="http://lists.rtems.org/mailman/listinfo/devel" rel="noreferrer" target="_blank">http://lists.rtems.org/<wbr>mailman/listinfo/devel</a><br>
>> ><br>
><br>
><br>
><br>
> ______________________________<wbr>_________________<br>
> devel mailing list<br>
> <a href="mailto:devel@rtems.org">devel@rtems.org</a><br>
> <a href="http://lists.rtems.org/mailman/listinfo/devel" rel="noreferrer" target="_blank">http://lists.rtems.org/<wbr>mailman/listinfo/devel</a><br>
</div></div></blockquote></div><br></div>