Re: [PATCH] Added Getentropy() support to beagle BSP

2018-03-16 Thread Gedare Bloom
Hello Udit,

Now I think you should re-send this patch as a separate submission. In
this case, it would also be a good idea to tag it as "v2". You can use
for this the "-v 2" command line argument to git-format-patch, and
then git-send-email on the output file(s). Probably, you may want to
resend your ATSAM one too if it has undergone revisions.

Gedare

On Fri, Mar 16, 2018 at 6:10 AM, Udit agarwal  wrote:
> Patch for BBB:
>
> From a6b3b58a38a6925bf26b6573f36fc652708f9f32 Mon Sep 17 00:00:00 2001
> From: Udit agarwal 
> Date: Fri, 16 Mar 2018 15:37:05 +0530
> Subject: [PATCH] arm/beagle: add TRNG based getentropy implementation
>
> ---
>  bsps/arm/include/libcpu/am335x.h |  33 ++
>  c/src/lib/libbsp/arm/beagle/Makefile.am  |   4 +-
>  c/src/lib/libbsp/arm/beagle/dev/bbb_getentropy.c | 130
> +++
>  3 files changed, 166 insertions(+), 1 deletion(-)
>  create mode 100644 c/src/lib/libbsp/arm/beagle/dev/bbb_getentropy.c
>
> diff --git a/bsps/arm/include/libcpu/am335x.h
> b/bsps/arm/include/libcpu/am335x.h
> index 367e97c..a6fb8b8 100644
> --- a/bsps/arm/include/libcpu/am335x.h
> +++ b/bsps/arm/include/libcpu/am335x.h
> @@ -14,11 +14,17 @@
>   * Modified by Ben Gras  to add lots
>   * of beagleboard/beaglebone definitions, delete lpc32xx specific
>   * ones, and merge with some other header files.
> + *
> + * Modified by Udit agarwal  to add true random
> + * number generating module definitions and TRNG register structure.
>   */
>
>  #if !defined(_AM335X_H_)
>  #define _AM335X_H_
>
> +/* For TRNG register definition */
> +#include 
> +
>  /* Interrupt controller memory map */
>  #define OMAP3_DM37XX_INTR_BASE 0x4820 /* INTCPS physical address */
>
> @@ -701,4 +707,31 @@
>  #define AM335X_CM_PER_OCPWP_L3_CLKSTCTRL_CLKACTIVITY_OCPWP_L4_GCLK
> (0x0020u)
>  #define AM335X_I2C_INT_STOP_CONDITION AM335X_I2C_IRQSTATUS_BF
>
> +/* TRNG Registers */
> +/* TRNG base address */
> +#define TRNG_BASE 0x4831
> +/* Mask bits for trng clock status */
> +#define AM335X_CLK_TRNG_BIT_MASK (0x3)
> +/* Mask bits for output ready flag */
> +#define TRNG_STATUS_RDY (1u <<  0)
> +/* Mask bits for FRO related error */
> +#define TRNG_STATUS_ERR (1u <<  1)
> +/* Mask bits for clock status */
> +#define TRNG_STATUS_CLK (1u << 31)
> +/* enable module */
> +#define AM335X_TRNG_ENABLE (1 << 10)
> +
> +/* TRNG module clock register */
> +#define CM_PER_TRNG_CLKCTRL (AM335X_CM_PER_ADDR | (9 << 4))
>
> +
> +/* TRNG register structure */
> +typedef struct {
> +  uint64_t output; /* 00 */
> +  uint32_t status; /* 08 */
> +  uint32_t irq_en; /* 0c */
> +  uint32_t status_clr; /* 10 */
> +  uint32_t control;/* 14 */
> +  uint32_t config; /* 18 */
> +} am335x_trng_register;
> +
>  #endif
> diff --git a/c/src/lib/libbsp/arm/beagle/Makefile.am
> b/c/src/lib/libbsp/arm/beagle/Makefile.am
> index 8251660..c483dc4 100644
> --- a/c/src/lib/libbsp/arm/beagle/Makefile.am
> +++ b/c/src/lib/libbsp/arm/beagle/Makefile.am
> @@ -40,7 +40,6 @@ libbsp_a_LIBADD =
>
>  # Shared
>  libbsp_a_SOURCES += ../../shared/bootcard.c
> -libbsp_a_SOURCES += ../../shared/getentropy-cpucounter.c
>  libbsp_a_SOURCES += ../../shared/src/bsp-fdt.c
>  libbsp_a_SOURCES += ../../shared/bspclean.c
>  libbsp_a_SOURCES += ../../shared/bspgetworkarea.c
> @@ -88,6 +87,9 @@ libbsp_a_SOURCES += gpio/bbb-gpio.c
>  #pwm
>  libbsp_a_SOURCES += pwm/pwm.c
>
> +#getentropy
> +libbsp_a_SOURCES += dev/bbb_getentropy.c
> +
>  #RTC
>  libbsp_a_SOURCES += rtc.c
>  libbsp_a_SOURCES += ../../shared/tod.c
> diff --git a/c/src/lib/libbsp/arm/beagle/dev/bbb_getentropy.c
> b/c/src/lib/libbsp/arm/beagle/dev/bbb_getentropy.c
> new file mode 100644
> index 000..b3ea681
> --- /dev/null
> +++ b/c/src/lib/libbsp/arm/beagle/dev/bbb_getentropy.c
> @@ -0,0 +1,130 @@
>
> +/**
> +* @file
> +*
> +* @ingroup arm_beagle
> +*
> +* @brief Getentropy implementation on BeagleBone Black BSP
> +*/
> +
> +/*
> +* Copyright (c) 2018 Udit agarwal 
> +* All rights reserved.
> +*
> +* Redistribution and use in source and binary forms, with or without
> +* modification, are permitted provided that the following conditions
> +* are met:
> +* 1. Redistributions of source code must retain the above copyright
> +*notice, this list of conditions and the following disclaimer.
> +* 2. Redistributions in binary form must reproduce the above copyright
> +*notice, this list of conditions and the following disclaimer in the
> +*documentation and/or other materials provided with the distribution.
> +*
> +* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
> +* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
> +* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
> PURPOSE
> +* ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
> +* FOR ANY DIRECT, INDIRECT, INCIDENTAL, 

Re: [PATCH] Added Getentropy() support to beagle BSP

2018-03-16 Thread Udit agarwal
Patch for BBB:

>From a6b3b58a38a6925bf26b6573f36fc652708f9f32 Mon Sep 17 00:00:00 2001
From: Udit agarwal 
Date: Fri, 16 Mar 2018 15:37:05 +0530
Subject: [PATCH] arm/beagle: add TRNG based getentropy implementation

---
 bsps/arm/include/libcpu/am335x.h |  33 ++
 c/src/lib/libbsp/arm/beagle/Makefile.am  |   4 +-
 c/src/lib/libbsp/arm/beagle/dev/bbb_getentropy.c | 130
+++
 3 files changed, 166 insertions(+), 1 deletion(-)
 create mode 100644 c/src/lib/libbsp/arm/beagle/dev/bbb_getentropy.c

diff --git a/bsps/arm/include/libcpu/am335x.h
b/bsps/arm/include/libcpu/am335x.h
index 367e97c..a6fb8b8 100644
--- a/bsps/arm/include/libcpu/am335x.h
+++ b/bsps/arm/include/libcpu/am335x.h
@@ -14,11 +14,17 @@
  * Modified by Ben Gras  to add lots
  * of beagleboard/beaglebone definitions, delete lpc32xx specific
  * ones, and merge with some other header files.
+ *
+ * Modified by Udit agarwal  to add true random
+ * number generating module definitions and TRNG register structure.
  */

 #if !defined(_AM335X_H_)
 #define _AM335X_H_

+/* For TRNG register definition */
+#include 
+
 /* Interrupt controller memory map */
 #define OMAP3_DM37XX_INTR_BASE 0x4820 /* INTCPS physical address */

@@ -701,4 +707,31 @@
 #define AM335X_CM_PER_OCPWP_L3_CLKSTCTRL_CLKACTIVITY_OCPWP_L4_GCLK
(0x0020u)
 #define AM335X_I2C_INT_STOP_CONDITION AM335X_I2C_IRQSTATUS_BF

+/* TRNG Registers */
+/* TRNG base address */
+#define TRNG_BASE 0x4831
+/* Mask bits for trng clock status */
+#define AM335X_CLK_TRNG_BIT_MASK (0x3)
+/* Mask bits for output ready flag */
+#define TRNG_STATUS_RDY (1u <<  0)
+/* Mask bits for FRO related error */
+#define TRNG_STATUS_ERR (1u <<  1)
+/* Mask bits for clock status */
+#define TRNG_STATUS_CLK (1u << 31)
+/* enable module */
+#define AM335X_TRNG_ENABLE (1 << 10)
+
+/* TRNG module clock register */
+#define CM_PER_TRNG_CLKCTRL (AM335X_CM_PER_ADDR | (9 << 4))
+
+/* TRNG register structure */
+typedef struct {
+  uint64_t output; /* 00 */
+  uint32_t status; /* 08 */
+  uint32_t irq_en; /* 0c */
+  uint32_t status_clr; /* 10 */
+  uint32_t control;/* 14 */
+  uint32_t config; /* 18 */
+} am335x_trng_register;
+
 #endif
diff --git a/c/src/lib/libbsp/arm/beagle/Makefile.am
b/c/src/lib/libbsp/arm/beagle/Makefile.am
index 8251660..c483dc4 100644
--- a/c/src/lib/libbsp/arm/beagle/Makefile.am
+++ b/c/src/lib/libbsp/arm/beagle/Makefile.am
@@ -40,7 +40,6 @@ libbsp_a_LIBADD =

 # Shared
 libbsp_a_SOURCES += ../../shared/bootcard.c
-libbsp_a_SOURCES += ../../shared/getentropy-cpucounter.c
 libbsp_a_SOURCES += ../../shared/src/bsp-fdt.c
 libbsp_a_SOURCES += ../../shared/bspclean.c
 libbsp_a_SOURCES += ../../shared/bspgetworkarea.c
@@ -88,6 +87,9 @@ libbsp_a_SOURCES += gpio/bbb-gpio.c
 #pwm
 libbsp_a_SOURCES += pwm/pwm.c

+#getentropy
+libbsp_a_SOURCES += dev/bbb_getentropy.c
+
 #RTC
 libbsp_a_SOURCES += rtc.c
 libbsp_a_SOURCES += ../../shared/tod.c
diff --git a/c/src/lib/libbsp/arm/beagle/dev/bbb_getentropy.c
b/c/src/lib/libbsp/arm/beagle/dev/bbb_getentropy.c
new file mode 100644
index 000..b3ea681
--- /dev/null
+++ b/c/src/lib/libbsp/arm/beagle/dev/bbb_getentropy.c
@@ -0,0 +1,130 @@
+/**
+* @file
+*
+* @ingroup arm_beagle
+*
+* @brief Getentropy implementation on BeagleBone Black BSP
+*/
+
+/*
+* Copyright (c) 2018 Udit agarwal 
+* All rights reserved.
+*
+* Redistribution and use in source and binary forms, with or without
+* modification, are permitted provided that the following conditions
+* are met:
+* 1. Redistributions of source code must retain the above copyright
+*notice, this list of conditions and the following disclaimer.
+* 2. Redistributions in binary form must reproduce the above copyright
+*notice, this list of conditions and the following disclaimer in the
+*documentation and/or other materials provided with the distribution.
+*
+* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE
+* ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL
+* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
STRICT
+* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+* SUCH DAMAGE.
+*/
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+/* max refill 34 * 256 cycles */
+#define AM335X_TRNG_MAX_REFILL (34 << 16)
+/* min refill 33 * 64 cycles */
+#define AM335X_TRNG_MIN_REFILL (33 << 0)
+/* startup 33 * 256 cycles */

Re: [PATCH] Added Getentropy() support to beagle BSP

2018-03-16 Thread Udit agarwal
Thanks for pointing that out, I have rectified the indentation errors.

Here's the patch for atsam:
>From 8e5e17525a76e68bc4d869d0af9700aaa5628483 Mon Sep 17 00:00:00 2001
From: Udit agarwal 
Date: Fri, 16 Mar 2018 14:54:53 +0530
Subject: [PATCH] arm/atsam: protect TRNG_GetRandData with mutex

---
 c/src/lib/libbsp/arm/atsam/startup/getentropy-trng.c | 7 ++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/c/src/lib/libbsp/arm/atsam/startup/getentropy-trng.c
b/c/src/lib/libbsp/arm/atsam/startup/getentropy-trng.c
index 11e24dc..54a1cef 100644
--- a/c/src/lib/libbsp/arm/atsam/startup/getentropy-trng.c
+++ b/c/src/lib/libbsp/arm/atsam/startup/getentropy-trng.c
@@ -17,6 +17,9 @@
 #include 
 #include 

+static rtems_mutex atsam_trng_mutex =
+RTEMS_MUTEX_INITIALIZER("atsam_trng");
+
 static void atsam_trng_enable(void)
 {
  PMC_EnablePeripheral(ID_TRNG);
@@ -25,6 +28,8 @@ static void atsam_trng_enable(void)

 int getentropy(void *ptr, size_t n)
 {
+  rtems_mutex_lock(_trng_mutex);
+
  while (n > 0) {
uint32_t random;
size_t copy;
@@ -50,7 +55,7 @@ int getentropy(void *ptr, size_t n)
n -= copy;
ptr += copy;
  }
-
+  rtems_mutex_unlock(_trng_mutex);
  return 0;
 }

-- 
1.9.1

On Thu, Mar 15, 2018 at 7:52 PM, Gedare Bloom  wrote:

> On Thu, Mar 15, 2018 at 7:00 AM, Udit agarwal 
> wrote:
> >
> >
> > On Thu, Mar 15, 2018 at 2:38 PM, Sebastian Huber
> >  wrote:
> >>
> >> On 15/03/18 09:20, Udit agarwal wrote:
> >>>
> >>> Also, will it be ok to keep atsam_trng_mutex even after user has
> finished
> >>> using getentropy() i.e when we don't use destroy(_trng_mutex) at
> all?
> >>
> >>
> >> Why do you want to destroy the mutex?
> >
> > Just trying to reduce redundancy, but then i don't think that's a good
> idea.
> > BTW, here's atsam patch, please have a look:
> >
> > From 35313002ea19715c5914c5983a24471b0763aa68 Mon Sep 17 00:00:00 2001
> > From: Udit agarwal 
> > Date: Thu, 15 Mar 2018 16:21:51 +0530
> > Subject: [PATCH] Added mutex across TRNG_GetRandData
>
> In the commit message, identify the rtems subsystem. I guess we don't
> have detailed advice on the wiki for this, but here I guess we usually
> would have something like "arm/atsam: protect TRNG_GetRandData with
> mutex"
>
> >
> > ---
> >  c/src/lib/libbsp/arm/atsam/startup/getentropy-trng.c | 7 +++
> >  1 file changed, 7 insertions(+)
> >
> > diff --git a/c/src/lib/libbsp/arm/atsam/startup/getentropy-trng.c
> > b/c/src/lib/libbsp/arm/atsam/startup/getentropy-trng.c
> > index 11e24dc..2789970 100644
> > --- a/c/src/lib/libbsp/arm/atsam/startup/getentropy-trng.c
> > +++ b/c/src/lib/libbsp/arm/atsam/startup/getentropy-trng.c
> > @@ -16,6 +16,9 @@
> >  #include 
> >  #include 
> >  #include 
> > +#include 
> > +
> > +static rtems_mutex atsam_trng_mutex =
> > RTEMS_MUTEX_INITIALIZER("atsam_trng");
> >
> The second line should be indented 2 more levels (4 spaces).
> https://devel.rtems.org/wiki/Developer/Coding/80_characters_per_line
>
> >  static void atsam_trng_enable(void)
> >  {
> > @@ -25,6 +28,9 @@ static void atsam_trng_enable(void)
> >
> >  int getentropy(void *ptr, size_t n)
> >  {
> > +
> > +rtems_mutex_lock(_trng_mutex);
> The number of indent spaces here look wrong to me. It should be 2.
>
> > +
> >  while (n > 0) {
> >  uint32_t random;
> >  size_t copy;
> > @@ -51,6 +57,7 @@ int getentropy(void *ptr, size_t n)
> >  ptr += copy;
> >  }
> >
> > +rtems_mutex_unlock(_trng_mutex);
> ditto.
>
> >  return 0;
> >  }
> >
> > --
> > 1.9.1
> >
> >
> >>
> >>
> >>
> >> --
> >> Sebastian Huber, embedded brains GmbH
> >>
> >> Address : Dornierstr. 4, D-82178 Puchheim, Germany
> >> Phone   : +49 89 189 47 41-16
> >> Fax : +49 89 189 47 41-09
> >> E-Mail  : sebastian.hu...@embedded-brains.de
> >> PGP : Public key available on request.
> >>
> >> Diese Nachricht ist keine geschäftliche Mitteilung im Sinne des EHUG.
> >>
> >
> >
> > ___
> > devel mailing list
> > devel@rtems.org
> > http://lists.rtems.org/mailman/listinfo/devel
>
___
devel mailing list
devel@rtems.org
http://lists.rtems.org/mailman/listinfo/devel

Re: [PATCH] Added Getentropy() support to beagle BSP

2018-03-15 Thread Gedare Bloom
On Thu, Mar 15, 2018 at 7:00 AM, Udit agarwal  wrote:
>
>
> On Thu, Mar 15, 2018 at 2:38 PM, Sebastian Huber
>  wrote:
>>
>> On 15/03/18 09:20, Udit agarwal wrote:
>>>
>>> Also, will it be ok to keep atsam_trng_mutex even after user has finished
>>> using getentropy() i.e when we don't use destroy(_trng_mutex) at all?
>>
>>
>> Why do you want to destroy the mutex?
>
> Just trying to reduce redundancy, but then i don't think that's a good idea.
> BTW, here's atsam patch, please have a look:
>
> From 35313002ea19715c5914c5983a24471b0763aa68 Mon Sep 17 00:00:00 2001
> From: Udit agarwal 
> Date: Thu, 15 Mar 2018 16:21:51 +0530
> Subject: [PATCH] Added mutex across TRNG_GetRandData

In the commit message, identify the rtems subsystem. I guess we don't
have detailed advice on the wiki for this, but here I guess we usually
would have something like "arm/atsam: protect TRNG_GetRandData with
mutex"

>
> ---
>  c/src/lib/libbsp/arm/atsam/startup/getentropy-trng.c | 7 +++
>  1 file changed, 7 insertions(+)
>
> diff --git a/c/src/lib/libbsp/arm/atsam/startup/getentropy-trng.c
> b/c/src/lib/libbsp/arm/atsam/startup/getentropy-trng.c
> index 11e24dc..2789970 100644
> --- a/c/src/lib/libbsp/arm/atsam/startup/getentropy-trng.c
> +++ b/c/src/lib/libbsp/arm/atsam/startup/getentropy-trng.c
> @@ -16,6 +16,9 @@
>  #include 
>  #include 
>  #include 
> +#include 
> +
> +static rtems_mutex atsam_trng_mutex =
> RTEMS_MUTEX_INITIALIZER("atsam_trng");
>
The second line should be indented 2 more levels (4 spaces).
https://devel.rtems.org/wiki/Developer/Coding/80_characters_per_line

>  static void atsam_trng_enable(void)
>  {
> @@ -25,6 +28,9 @@ static void atsam_trng_enable(void)
>
>  int getentropy(void *ptr, size_t n)
>  {
> +
> +rtems_mutex_lock(_trng_mutex);
The number of indent spaces here look wrong to me. It should be 2.

> +
>  while (n > 0) {
>  uint32_t random;
>  size_t copy;
> @@ -51,6 +57,7 @@ int getentropy(void *ptr, size_t n)
>  ptr += copy;
>  }
>
> +rtems_mutex_unlock(_trng_mutex);
ditto.

>  return 0;
>  }
>
> --
> 1.9.1
>
>
>>
>>
>>
>> --
>> Sebastian Huber, embedded brains GmbH
>>
>> Address : Dornierstr. 4, D-82178 Puchheim, Germany
>> Phone   : +49 89 189 47 41-16
>> Fax : +49 89 189 47 41-09
>> E-Mail  : sebastian.hu...@embedded-brains.de
>> PGP : Public key available on request.
>>
>> Diese Nachricht ist keine geschäftliche Mitteilung im Sinne des EHUG.
>>
>
>
> ___
> devel mailing list
> devel@rtems.org
> http://lists.rtems.org/mailman/listinfo/devel
___
devel mailing list
devel@rtems.org
http://lists.rtems.org/mailman/listinfo/devel

Re: [PATCH] Added Getentropy() support to beagle BSP

2018-03-15 Thread Udit agarwal
On Thu, Mar 15, 2018 at 2:38 PM, Sebastian Huber <
sebastian.hu...@embedded-brains.de> wrote:

> On 15/03/18 09:20, Udit agarwal wrote:
>
>> Also, will it be ok to keep atsam_trng_mutex even after user has finished
>> using getentropy() i.e when we don't use destroy(_trng_mutex) at all?
>>
>
> Why do you want to destroy the mutex?

Just trying to reduce redundancy, but then i don't think that's a good
idea. BTW, here's atsam patch, please have a look:

>From 35313002ea19715c5914c5983a24471b0763aa68 Mon Sep 17 00:00:00 2001
From: Udit agarwal 
Date: Thu, 15 Mar 2018 16:21:51 +0530
Subject: [PATCH] Added mutex across TRNG_GetRandData

---
 c/src/lib/libbsp/arm/atsam/startup/getentropy-trng.c | 7 +++
 1 file changed, 7 insertions(+)

diff --git a/c/src/lib/libbsp/arm/atsam/startup/getentropy-trng.c
b/c/src/lib/libbsp/arm/atsam/startup/getentropy-trng.c
index 11e24dc..2789970 100644
--- a/c/src/lib/libbsp/arm/atsam/startup/getentropy-trng.c
+++ b/c/src/lib/libbsp/arm/atsam/startup/getentropy-trng.c
@@ -16,6 +16,9 @@
 #include 
 #include 
 #include 
+#include 
+
+static rtems_mutex atsam_trng_mutex =
RTEMS_MUTEX_INITIALIZER("atsam_trng");

 static void atsam_trng_enable(void)
 {
@@ -25,6 +28,9 @@ static void atsam_trng_enable(void)

 int getentropy(void *ptr, size_t n)
 {
+
+rtems_mutex_lock(_trng_mutex);
+
 while (n > 0) {
 uint32_t random;
 size_t copy;
@@ -51,6 +57,7 @@ int getentropy(void *ptr, size_t n)
 ptr += copy;
 }

+rtems_mutex_unlock(_trng_mutex);
 return 0;
 }

-- 
1.9.1



>
>
> --
> Sebastian Huber, embedded brains GmbH
>
> Address : Dornierstr. 4, D-82178 Puchheim, G
> 
> ermany
> Phone   : +49 89 189 47 41-16
> Fax : +49 89 189 47 41-09
> E-Mail  : sebastian.hu...@embedded-brains.de
> PGP : Public key available on request.
>
> Diese Nachricht ist keine geschäftliche Mitteilung im Sinne des EHUG.
>
>
___
devel mailing list
devel@rtems.org
http://lists.rtems.org/mailman/listinfo/devel

Re: [PATCH] Added Getentropy() support to beagle BSP

2018-03-15 Thread Sebastian Huber

On 15/03/18 09:20, Udit agarwal wrote:
Also, will it be ok to keep atsam_trng_mutex even after user has 
finished using getentropy() i.e when we don't use 
destroy(_trng_mutex) at all?


Why do you want to destroy the mutex?

--
Sebastian Huber, embedded brains GmbH

Address : Dornierstr. 4, D-82178 Puchheim, Germany
Phone   : +49 89 189 47 41-16
Fax : +49 89 189 47 41-09
E-Mail  : sebastian.hu...@embedded-brains.de
PGP : Public key available on request.

Diese Nachricht ist keine geschäftliche Mitteilung im Sinne des EHUG.

___
devel mailing list
devel@rtems.org
http://lists.rtems.org/mailman/listinfo/devel

Re: [PATCH] Added Getentropy() support to beagle BSP

2018-03-15 Thread Udit agarwal
On Thu, Mar 15, 2018 at 1:19 PM, Sebastian Huber <
sebastian.hu...@embedded-brains.de> wrote:

> On 15/03/18 08:13, Udit agarwal wrote:
>
>> Oh yes, i think i misinterpreted your previous suggestion of surrounding
>> the while loop by mutex, it should be the outer while loop. some thing like
>> this?:
>> while (n > 0) {
>> uint32_t random;
>> size_t copy;
>>
>> rtems_mutex_lock(_trng_mutex);
>>
>
> Now you correctly use the mutex to produce 32-bits of random data.
> Currently the likely user of getrandom() is
>
> https://sourceware.org/git/gitweb.cgi?p=newlib-cygwin.git;a=
> blob;f=newlib/libc/stdlib/arc4random.c;h=33ed46a5e9e
> 756d4e216cb7686589eb60993;hb=HEAD#l89
>
> So, you need 12 mutex lock/unlock operations for this. I would protect the
> while (n > 0) loop with a single mutex lock/unlock.

Ok,Like this?
rtems_mutex_lock(_trng_mutex);
while (n > 0) {
   .
   .
}
rtems_mutex_unlock(_trng_mutex);

Also, will it be ok to keep atsam_trng_mutex even after user has finished
using getentropy() i.e when we don't use destroy(_trng_mutex) at all?

>
>
> --
> Sebastian Huber, embedded brains GmbH
>
> Address : Dornierstr. 4, D-82178 Puchheim, Germany
> Phone   : +49 89 189 47 41-16
> Fax : +49 89 189 47 41-09
> E-Mail  : sebastian.hu...@embedded-brains.de
> PGP : Public key available on request.
>
> Diese Nachricht ist keine geschäftliche Mitteilung im Sinne des EHUG.
>
>
___
devel mailing list
devel@rtems.org
http://lists.rtems.org/mailman/listinfo/devel

Re: [PATCH] Added Getentropy() support to beagle BSP

2018-03-15 Thread Sebastian Huber

On 15/03/18 08:13, Udit agarwal wrote:
Oh yes, i think i misinterpreted your previous suggestion of 
surrounding the while loop by mutex, it should be the outer while 
loop. some thing like this?:

while (n > 0) {
        uint32_t random;
        size_t copy;

        rtems_mutex_lock(_trng_mutex);


Now you correctly use the mutex to produce 32-bits of random data. 
Currently the likely user of getrandom() is


https://sourceware.org/git/gitweb.cgi?p=newlib-cygwin.git;a=blob;f=newlib/libc/stdlib/arc4random.c;h=33ed46a5e9e756d4e216cb7686589eb60993;hb=HEAD#l89

So, you need 12 mutex lock/unlock operations for this. I would protect 
the while (n > 0) loop with a single mutex lock/unlock.


--
Sebastian Huber, embedded brains GmbH

Address : Dornierstr. 4, D-82178 Puchheim, Germany
Phone   : +49 89 189 47 41-16
Fax : +49 89 189 47 41-09
E-Mail  : sebastian.hu...@embedded-brains.de
PGP : Public key available on request.

Diese Nachricht ist keine geschäftliche Mitteilung im Sinne des EHUG.

___
devel mailing list
devel@rtems.org
http://lists.rtems.org/mailman/listinfo/devel

Re: [PATCH] Added Getentropy() support to beagle BSP

2018-03-15 Thread Udit agarwal
On Thu, Mar 15, 2018 at 11:34 AM, Sebastian Huber <
sebastian.hu...@embedded-brains.de> wrote:

>
>
> On 15/03/18 05:56, Udit agarwal wrote:
>
>> +/* TRNG Register */
>> +
>> +/* RNG base address */
>>
>
> Is the module name TRNG or RNG?
>
It's TRNG. I'll correct this.

>
> +#define RNG_BASE 0x4831
>> +/* RNG clock control */
>> +#define CM_PER_RNG_CLKCTRL (AM335X_CM_PER_ADDR | (9 << 4))
>>
>
> This define is probably not for the TRNG register block. Should it move
> the corresponding register block?
>
This define is to enable clock for TRNG module by setting the bit in the
main clock register. Should i place this define with other CM definitions?

>
> +/* rng module clock status bits */
>> +#define AM335X_CLK_RNG_BIT_MASK (0x3)
>> +/* Offset from RNG base for output ready flag */
>> +#define RNG_STATUS_RDY (1u <<  0)
>> +/* Offset from RNG base for FRO related error */
>> +#define RNG_STATUS_ERR (1u <<  1)
>> +/* Offset from RNG base for clock status */
>> +#define RNG_STATUS_CLK (1u << 31)
>>
>
> Are these offsets or bits?
>
I should have probably marked them as mask bits.

>
> +/* enable module */
>> +#define AM335X_RNG_ENABLE (1 << 10)
>> +
>>
>
> --
> Sebastian Huber, embedded brains GmbH
>
> Address : Dornierstr. 4, D-82178 Puchheim, Germany
> Phone   : +49 89 189 47 41-16
> Fax : +49 89 189 47 41-09
> E-Mail  : sebastian.hu...@embedded-brains.de
> PGP : Public key available on request.
>
> Diese Nachricht ist keine geschäftliche Mitteilung im Sinne des EHUG.
>
>
___
devel mailing list
devel@rtems.org
http://lists.rtems.org/mailman/listinfo/devel

Re: [PATCH] Added Getentropy() support to beagle BSP

2018-03-15 Thread Udit agarwal
On Thu, Mar 15, 2018 at 11:29 AM, Sebastian Huber <
sebastian.hu...@embedded-brains.de> wrote:

>
>
> On 15/03/18 05:56, Udit agarwal wrote:
>
>>  c/src/lib/libbsp/arm/atsam/startup/getentropy-trng.c | 6 ++
>>  1 file changed, 6 insertions(+)
>>
>> diff --git a/c/src/lib/libbsp/arm/atsam/startup/getentropy-trng.c
>> b/c/src/lib/libbsp/arm/atsam/startup/getentropy-trng.c
>> index 11e24dc..b26b6a8 100644
>> --- a/c/src/lib/libbsp/arm/atsam/startup/getentropy-trng.c
>> +++ b/c/src/lib/libbsp/arm/atsam/startup/getentropy-trng.c
>> @@ -17,6 +17,8 @@
>>  #include 
>>  #include 
>>
>> +static rtems_mutex atsam_trng_reg = RTEMS_MUTEX_INITIALIZER("atsam
>> _trng_reg");
>>
>
> Is this a register or a mutex?
>

> +
>>  static void atsam_trng_enable(void)
>>  {
>>  PMC_EnablePeripheral(ID_TRNG);
>> @@ -29,9 +31,12 @@ int getentropy(void *ptr, size_t n)
>>  uint32_t random;
>>  size_t copy;
>>
>> +rtems_mutex_lock(_trng_reg);
>>  while ((TRNG_GetStatus() & TRNG_ISR_DATRDY) == 0) {
>>  /* wait */
>>  }
>> +
>> +rtems_mutex_unlock(_trng_reg);
>>
>
> Now the status register read is protected by a mutex.
>
> random = TRNG_GetRandData();
>>
>
> Can you ensure that this read is correct in a multi-threaded application
> (the status register has the right value during the read)?
>
> Oh yes, i think i misinterpreted your previous suggestion of surrounding
the while loop by mutex, it should be the outer while loop. some thing like
this?:
while (n > 0) {
uint32_t random;
size_t copy;

rtems_mutex_lock(_trng_mutex);

while ((TRNG_GetStatus() & TRNG_ISR_DATRDY) == 0) {
/* wait */
}

random = TRNG_GetRandData();

/*
 * Read TRNG status one more time to avoid race condition.
 * Otherwise we can read (and clear) an old ready status but get
 * a new value. The ready status for this value wouldn't be
 * reset.
 */
TRNG_GetStatus();

copy = sizeof(random);
if (n < copy ) {
copy = n;
}
memcpy(ptr, , copy);
n -= copy;
ptr += copy;

rtems_mutex_unlock(_trng_mutex);
}


>
>>  /*
>> @@ -51,6 +56,7 @@ int getentropy(void *ptr, size_t n)
>>  ptr += copy;
>>  }
>>
>> +rtems_mutex_destroy(_trng_reg);
>>
>
> Here  you destroy the mutex. What happens if you call getentropy() a
> second time?


> return 0;
>>  }
>>
>
> --
> Sebastian Huber, embedded brains GmbH
>
> Address : Dornierstr. 4, D-82178 Puchheim, Germany
> Phone   : +49 89 189 47 41-16
> Fax : +49 89 189 47 41-09
> E-Mail  : sebastian.hu...@embedded-brains.de
> PGP : Public key available on request.
>
> Diese Nachricht ist keine geschäftliche Mitteilung im Sinne des EHUG.
>
>
___
devel mailing list
devel@rtems.org
http://lists.rtems.org/mailman/listinfo/devel

Re: [PATCH] Added Getentropy() support to beagle BSP

2018-03-15 Thread Sebastian Huber



On 15/03/18 05:56, Udit agarwal wrote:

+/* TRNG Register */
+
+/* RNG base address */


Is the module name TRNG or RNG?


+#define RNG_BASE 0x4831
+/* RNG clock control */
+#define CM_PER_RNG_CLKCTRL (AM335X_CM_PER_ADDR | (9 << 4))


This define is probably not for the TRNG register block. Should it move 
the corresponding register block?



+/* rng module clock status bits */
+#define AM335X_CLK_RNG_BIT_MASK (0x3)
+/* Offset from RNG base for output ready flag */
+#define RNG_STATUS_RDY (1u <<  0)
+/* Offset from RNG base for FRO related error */
+#define RNG_STATUS_ERR (1u <<  1)
+/* Offset from RNG base for clock status */
+#define RNG_STATUS_CLK (1u << 31)


Are these offsets or bits?


+/* enable module */
+#define AM335X_RNG_ENABLE (1 << 10)
+


--
Sebastian Huber, embedded brains GmbH

Address : Dornierstr. 4, D-82178 Puchheim, Germany
Phone   : +49 89 189 47 41-16
Fax : +49 89 189 47 41-09
E-Mail  : sebastian.hu...@embedded-brains.de
PGP : Public key available on request.

Diese Nachricht ist keine geschäftliche Mitteilung im Sinne des EHUG.

___
devel mailing list
devel@rtems.org
http://lists.rtems.org/mailman/listinfo/devel

Re: [PATCH] Added Getentropy() support to beagle BSP

2018-03-15 Thread Sebastian Huber



On 15/03/18 05:56, Udit agarwal wrote:

 c/src/lib/libbsp/arm/atsam/startup/getentropy-trng.c | 6 ++
 1 file changed, 6 insertions(+)

diff --git a/c/src/lib/libbsp/arm/atsam/startup/getentropy-trng.c 
b/c/src/lib/libbsp/arm/atsam/startup/getentropy-trng.c

index 11e24dc..b26b6a8 100644
--- a/c/src/lib/libbsp/arm/atsam/startup/getentropy-trng.c
+++ b/c/src/lib/libbsp/arm/atsam/startup/getentropy-trng.c
@@ -17,6 +17,8 @@
 #include 
 #include 

+static rtems_mutex atsam_trng_reg = 
RTEMS_MUTEX_INITIALIZER("atsam_trng_reg");


Is this a register or a mutex?


+
 static void atsam_trng_enable(void)
 {
 PMC_EnablePeripheral(ID_TRNG);
@@ -29,9 +31,12 @@ int getentropy(void *ptr, size_t n)
     uint32_t random;
     size_t copy;

+        rtems_mutex_lock(_trng_reg);
     while ((TRNG_GetStatus() & TRNG_ISR_DATRDY) == 0) {
         /* wait */
     }
+
+        rtems_mutex_unlock(_trng_reg);


Now the status register read is protected by a mutex.


    random = TRNG_GetRandData();


Can you ensure that this read is correct in a multi-threaded application 
(the status register has the right value during the read)?




     /*
@@ -51,6 +56,7 @@ int getentropy(void *ptr, size_t n)
     ptr += copy;
 }

+    rtems_mutex_destroy(_trng_reg);


Here  you destroy the mutex. What happens if you call getentropy() a 
second time?



return 0;
 }


--
Sebastian Huber, embedded brains GmbH

Address : Dornierstr. 4, D-82178 Puchheim, Germany
Phone   : +49 89 189 47 41-16
Fax : +49 89 189 47 41-09
E-Mail  : sebastian.hu...@embedded-brains.de
PGP : Public key available on request.

Diese Nachricht ist keine geschäftliche Mitteilung im Sinne des EHUG.

___
devel mailing list
devel@rtems.org
http://lists.rtems.org/mailman/listinfo/devel

Re: [PATCH] Added Getentropy() support to beagle BSP

2018-03-14 Thread Udit agarwal
Hi,
Below is the updated patch along with the one for atsam. Please have a look.
Moreover, in error case(FRO unwanted shutdown), Error bit of status
register turns zero, indicating that FRO is no longer running.Untill now, i
haven't came across any situation where this bit turns 0. In the NETBSD
<http://ftp.netbsd.org/pub/NetBSD/NetBSD-current/src/sys/arch/arm/omap/am335x_trng.c>
and in linux kernel
<https://github.com/torvalds/linux/blob/68fed41e0ff6c0332520a0d70ac05be2a7d9130e/drivers/char/hw_random/omap-rng.c>
(line 241 ) implementation of am335x_trng, it seems they aren't checking
for this bit. Instead they are only using RNG_STATUS_RDY as an indication
of successful random data generation.

*Patch: atsam*

>From ae0c79ae4850503259c64387dea69346e7d7b1d4 Mon Sep 17 00:00:00 2001
From: Udit agarwal <dev.mada...@gmail.com>
Date: Thu, 15 Mar 2018 09:43:13 +0530
Subject: [PATCH] Fixed mutual exclusion of critical section

---
 c/src/lib/libbsp/arm/atsam/startup/getentropy-trng.c | 6 ++
 1 file changed, 6 insertions(+)

diff --git a/c/src/lib/libbsp/arm/atsam/startup/getentropy-trng.c
b/c/src/lib/libbsp/arm/atsam/startup/getentropy-trng.c
index 11e24dc..b26b6a8 100644
--- a/c/src/lib/libbsp/arm/atsam/startup/getentropy-trng.c
+++ b/c/src/lib/libbsp/arm/atsam/startup/getentropy-trng.c
@@ -17,6 +17,8 @@
 #include 
 #include 

+static rtems_mutex atsam_trng_reg =
RTEMS_MUTEX_INITIALIZER("atsam_trng_reg");
+
 static void atsam_trng_enable(void)
 {
 PMC_EnablePeripheral(ID_TRNG);
@@ -29,9 +31,12 @@ int getentropy(void *ptr, size_t n)
 uint32_t random;
 size_t copy;

+rtems_mutex_lock(_trng_reg);
 while ((TRNG_GetStatus() & TRNG_ISR_DATRDY) == 0) {
 /* wait */
 }
+
+rtems_mutex_unlock(_trng_reg);
 random = TRNG_GetRandData();

 /*
@@ -51,6 +56,7 @@ int getentropy(void *ptr, size_t n)
 ptr += copy;
 }

+rtems_mutex_destroy(_trng_reg);
 return 0;
 }

-- 
1.9.1

*Patch BBB*

>From ea885f9681c14f324917469b295e7489f56c10e7 Mon Sep 17 00:00:00 2001
From: Udit agarwal <dev.mada...@gmail.com>
Date: Thu, 15 Mar 2018 09:24:35 +0530
Subject: [PATCH] Added getentropy support to beagle BSP

---
 bsps/arm/include/libcpu/am335x.h |  33 ++
 c/src/lib/libbsp/arm/beagle/Makefile.am  |   4 +-
 c/src/lib/libbsp/arm/beagle/dev/bbb_getentropy.c | 126
+++
 3 files changed, 162 insertions(+), 1 deletion(-)
 create mode 100644 c/src/lib/libbsp/arm/beagle/dev/bbb_getentropy.c

diff --git a/bsps/arm/include/libcpu/am335x.h
b/bsps/arm/include/libcpu/am335x.h
index 367e97c..6170ef3 100644
--- a/bsps/arm/include/libcpu/am335x.h
+++ b/bsps/arm/include/libcpu/am335x.h
@@ -14,11 +14,17 @@
  * Modified by Ben Gras <b...@shrike-systems.com> to add lots
  * of beagleboard/beaglebone definitions, delete lpc32xx specific
  * ones, and merge with some other header files.
+ *
+ * Modified by Udit agarwal <dev.mada...@gmail.com> to add random
+ * number generating module definitions and TRNG register structure.
  */

 #if !defined(_AM335X_H_)
 #define _AM335X_H_

+/* For TRNG register definition */
+#include 
+
 /* Interrupt controller memory map */
 #define OMAP3_DM37XX_INTR_BASE 0x4820 /* INTCPS physical address */

@@ -701,4 +707,31 @@
 #define AM335X_CM_PER_OCPWP_L3_CLKSTCTRL_CLKACTIVITY_OCPWP_L4_GCLK
(0x0020u)
 #define AM335X_I2C_INT_STOP_CONDITION AM335X_I2C_IRQSTATUS_BF

+/* TRNG Register */
+
+/* RNG base address */
+#define RNG_BASE 0x4831
+/* RNG clock control */
+#define CM_PER_RNG_CLKCTRL (AM335X_CM_PER_ADDR | (9 << 4))
+/* rng module clock status bits */
+#define AM335X_CLK_RNG_BIT_MASK (0x3)
+/* Offset from RNG base for output ready flag */
+#define RNG_STATUS_RDY (1u <<  0)
+/* Offset from RNG base for FRO related error */
+#define RNG_STATUS_ERR (1u <<  1)
+/* Offset from RNG base for clock status */
+#define RNG_STATUS_CLK (1u << 31)
+/* enable module */
+#define AM335X_RNG_ENABLE (1 << 10)
+
+/* TRNG register structure */
+typedef struct {
+  uint64_t output; /* 00 */
+  uint32_t status; /* 08 */
+  uint32_t irq_en; /* 0c */
+  uint32_t status_clr; /* 10 */
+  uint32_t control;/* 14 */
+  uint32_t config; /* 18 */
+} am335x_trng_register;
+
 #endif
diff --git a/c/src/lib/libbsp/arm/beagle/Makefile.am
b/c/src/lib/libbsp/arm/beagle/Makefile.am
index 8251660..c483dc4 100644
--- a/c/src/lib/libbsp/arm/beagle/Makefile.am
+++ b/c/src/lib/libbsp/arm/beagle/Makefile.am
@@ -40,7 +40,6 @@ libbsp_a_LIBADD =

 # Shared
 libbsp_a_SOURCES += ../../shared/bootcard.c
-libbsp_a_SOURCES += ../../shared/getentropy-cpucounter.c
 libbsp_a_SOURCES += ../../shared/src/bsp-fdt.c
 libbsp_a_SOURCES += ../../shared/bspclean.c
 libbsp_a_SOURCES += ../../shared/bspgetworkarea.c
@@ -88,6 +87,9 @@ libbsp_a_SOURCES += gpio/bbb-gpio.c
 #pwm
 libbsp_a_SOURCES += pwm/pwm.c

+#getentropy
+libbsp_

Fwd: Re: [PATCH] Added Getentropy() support to beagle BSP

2018-03-14 Thread Christian Mauderer
Ups. Hit reply instead of reply all. This should go to the mailing list.

--- Ursprüngliche Nachricht ---
> 
> Am Mittwoch, 14. März 2018 schrieb Udit agarwal:
> > Hi,
> > @Joel This seems to be SoC specific.
> > 
> > 
> > So, here is the implementation with mutex, Do let me know if this is okay. 
> > I'll then do it for atsam also.
> 
> Atsam should be an extra patch.
> 
> > 
> > int getentropy(void *ptr, size_t n)
> > {
> > volatile am335x_trng_register *rng = (am335x_trng_register*) RNG_BASE;
> > rtems_mutex lock_trng_reg = RTEMS_MUTEX_INITIALIZER("lock_trng_reg");
> 
> If you create a local variable every thread will have it's own mutex. So they 
> still can access the critical section in parallel. Use a global one in this 
> case.
> 
> > 
> > while (n > 0)
> > {
> > uint64_t random;
> > size_t copy;
> > 
> > /* for mutual exclusion synchronization between multiple
> > access to TRNG register in different contexts */
> > rtems_mutex_lock(_trng_reg);
> > 
> > /* wait untill RNG becomes ready with next set of random data */
> > while( ( rng->status & RNG_STATUS_RDY ) == 0 )
> > {
> > /* wait */
> > }
> > 
> > random = trng_getranddata(rng);
> > /* clear the status flag after reading to generate new random
> > value */
> > rng->status_clr = RNG_STATUS_RDY;
> > rtems_mutex_unlock(_trng_reg);
> > 
> > /* checking for error by masking all bits other then error bit in
> > status register */
> > if( ((rng->status & RNG_STATUS_ERR)>>1) == 1)
> > {
> > copy = sizeof(random);
> > if ( n < copy )
> > {
> > copy = n;
> > }
> > memcpy(ptr, , copy);
> > n -= copy;
> > ptr = (char*)ptr + copy;
> > }
> 
> Just noted that one: What Value is returned in the error case? Can the error 
> even happen? I think that there is a special function that should be called 
> in that case.
> 
> > }
> > 
> > rtems_mutex_destroy(_trng_reg);
> > return 0;
> > }
> > 
> > 
> > 
> > 
> > On Wed, Mar 14, 2018 at 2:15 AM, Joel Sherrill <j...@rtems.org> wrote:
> > 
> > 
> > 
> > 
> > 
> > On Mar 13, 2018 1:31 AM, "Sebastian Huber" 
> > <sebastian.hu...@embedded-brains.de> wrote:
> > 
> > 
> > 
> > On 12/03/18 20:02, Udit agarwal wrote:
> > 
> > So, It looks like here's the final patch, do let me know if its ready to be 
> > pushed. Also, it would be really helpful if someone else also tests this 
> > patch before pushing(Although i have done that once).
> > 
> > Thanks,
> > Udit agarwal
> > 
> > 
> > From 454a8ff3e0ea3393818859874705a54b098c6081 Mon Sep 17 00:00:00 2001
> > 
> > From: Udit agarwal <dev.mada...@gmail.com <mailto:dev.mada...@gmail.com>>
> > 
> > Date: Tue, 13 Mar 2018 00:20:28 +0530
> > Subject: [PATCH] Added Getentropy() support to beagle BSP
> > 
> > ---
> > bsps/arm/include/libcpu/am335x.h | 37 ++-
> > c/src/lib/libbsp/arm/beagle/Makefile.am | 4 +-
> > .../libbsp/arm/beagle/getentropy/bbb_getentropy.c | 116 
> > +
> > 3 files changed, 155 insertions(+), 2 deletions(-)
> > create mode 100644 c/src/lib/libbsp/arm/beagle/getentropy/bbb_getentropy.c
> > 
> > diff --git a/bsps/arm/include/libcpu/am335x.h 
> > b/bsps/arm/include/libcpu/am335x.h
> > index 367e97c..cedd637 100644
> > --- a/bsps/arm/include/libcpu/am335x.h
> > +++ b/bsps/arm/include/libcpu/am335x.h
> > @@ -14,11 +14,17 @@
> > 
> > * Modified by Ben Gras <b...@shrike-systems.com 
> > <mailto:b...@shrike-systems.com>> to add lots
> > 
> > * of beagleboard/beaglebone definitions, delete lpc32xx specific
> > * ones, and merge with some other header files.
> > + *
> > 
> > + * Modified by Udit agarwal <dev.mada...@gmail.com 
> > <mailto:dev.mada...@gmail.com>> to add random
> > 
> > + * number generating module definitions and TRNG register structure.
> > */
> > 
> > #if !defined(_AM335X_H_)
> > #define _AM335X_H_
> > 
> > +/* For TRNG register definition */
> > +#include 
> > +
> > /* Interrupt controller memory map */
> > #define OMAP3_DM37XX_INTR_BASE 0x4820 /* INTCPS physical address */
> > 
> > @@ -701,4 +707,33 @@
> > #define AM335X_CM_PER_OCPWP_L3_CLKSTCTRL_CLKACTIVITY_OCPWP_L4_GCLK 
> > (0x0020u)
> > #define AM335X_I2C_INT_STOP_CONDITION AM335X_I2C_IRQSTATUS_B

Re: [PATCH] Added Getentropy() support to beagle BSP

2018-03-14 Thread Sebastian Huber

On 14/03/18 00:38, Udit agarwal wrote:

Hi,
@Joel This seems to be SoC specific.


Yes, it is an on-chip peripheral.



So, here is the implementation with mutex, Do let me know if this is 
okay. I'll then do it for atsam also.


int getentropy(void *ptr, size_t n)
{
    volatile am335x_trng_register  *rng = (am335x_trng_register*) 
RNG_BASE;

    rtems_mutex lock_trng_reg = RTEMS_MUTEX_INITIALIZER("lock_trng_reg");


In order to ensure mutual exclusion for a particular piece of code via a 
mutex object all threads must use the same mutex object. Here, you 
create a mutex object on the fly on the stack of the executing thread. 
So, each thread has its own mutex. This doesn't work. You must use a 
global mutex used by all threads. For example:


static rtems_mutex am335x_trng_mutex = RTEMS_MUTEX_INITIALIZER("AM335X 
TRNG");




    while (n > 0)
    {
        uint64_t random;
        size_t copy;

        /* for mutual exclusion synchronization between multiple
           access to TRNG register in different contexts */
        rtems_mutex_lock(_trng_reg);


I would do the lock/unlock around the while loop.



        /* wait untill RNG becomes ready with next set of random data */
        while( ( rng->status & RNG_STATUS_RDY ) == 0 )
        {
            /* wait */
        }

        random = trng_getranddata(rng);
        /* clear the status flag after reading to generate new random
           value */
        rng->status_clr = RNG_STATUS_RDY;
        rtems_mutex_unlock(_trng_reg);

        /* checking for error by masking all bits other then error bit in
           status register */
        if( ((rng->status & RNG_STATUS_ERR)>>1) == 1)
        {
            copy = sizeof(random);
            if ( n < copy )
            {
                copy = n;
            }
            memcpy(ptr, , copy);
            n -= copy;
            ptr = (char*)ptr + copy;
        }
    }

    rtems_mutex_destroy(_trng_reg);
    return 0;
}


On Wed, Mar 14, 2018 at 2:15 AM, Joel Sherrill <j...@rtems.org 
<mailto:j...@rtems.org>> wrote:




On Mar 13, 2018 1:31 AM, "Sebastian Huber"
<sebastian.hu...@embedded-brains.de
<mailto:sebastian.hu...@embedded-brains.de>> wrote:



On 12/03/18 20:02, Udit agarwal wrote:

So, It looks like here's the final patch, do let me know
if its ready to be pushed. Also, it would be really
helpful if someone else also tests this patch before
pushing(Although i have done that once).

Thanks,
Udit agarwal


From 454a8ff3e0ea3393818859874705a54b098c6081 Mon Sep 17
00:00:00 2001
From: Udit agarwal <dev.mada...@gmail.com
<mailto:dev.mada...@gmail.com>
<mailto:dev.mada...@gmail.com
<mailto:dev.mada...@gmail.com>>>

        Date: Tue, 13 Mar 2018 00:20:28 +0530
Subject: [PATCH] Added Getentropy() support to beagle BSP

---
 bsps/arm/include/libcpu/am335x.h |  37 ++-
 c/src/lib/libbsp/arm/beagle/Makefile.am
<http://akefile.am> |   4 +-
 .../libbsp/arm/beagle/getentropy/bbb_getentropy.c | 116
+
 3 files changed, 155 insertions(+), 2 deletions(-)
 create mode 100644
c/src/lib/libbsp/arm/beagle/getentropy/bbb_getentropy.c

diff --git a/bsps/arm/include/libcpu/am335x.h
b/bsps/arm/include/libcpu/am335x.h
index 367e97c..cedd637 100644
--- a/bsps/arm/include/libcpu/am335x.h
+++ b/bsps/arm/include/libcpu/am335x.h
@@ -14,11 +14,17 @@
  * Modified by Ben Gras <b...@shrike-systems.com
<mailto:b...@shrike-systems.com>
<mailto:b...@shrike-systems.com
<mailto:b...@shrike-systems.com>>> to add lots

  * of beagleboard/beaglebone definitions, delete lpc32xx
specific
  * ones, and merge with some other header files.
+ *
+ * Modified by Udit agarwal <dev.mada...@gmail.com
<mailto:dev.mada...@gmail.com>
<mailto:dev.mada...@gmail.com
<mailto:dev.mada...@gmail.com>>> to add random

+ * number generating module definitions and TRNG register
structure.
  */

 #if !defined(_AM335X_H_)
 #define _AM335X_H_

+/* For TRNG register definition */
+#include 
+
 /* Interrupt controller memory map */
 #define OMAP3_DM37XX_INTR_BASE 0x4820 /* INTCPS
physical address */

@@ -701,4 +707,33 @@
 #define
AM335X_CM_PER_OCPWP_L3_CLKSTCTRL_CLKACTIVITY_OCPWP_L4_GCLK
(0x0020u)
  

Re: [PATCH] Added Getentropy() support to beagle BSP

2018-03-13 Thread Udit agarwal
Hi,
@Joel This seems to be SoC specific.

So, here is the implementation with mutex, Do let me know if this is okay.
I'll then do it for atsam also.

int getentropy(void *ptr, size_t n)
{
volatile am335x_trng_register  *rng = (am335x_trng_register*) RNG_BASE;
rtems_mutex lock_trng_reg = RTEMS_MUTEX_INITIALIZER("lock_trng_reg");

while (n > 0)
{
uint64_t random;
size_t copy;

/* for mutual exclusion synchronization between multiple
   access to TRNG register in different contexts */
rtems_mutex_lock(_trng_reg);

/* wait untill RNG becomes ready with next set of random data */
while( ( rng->status & RNG_STATUS_RDY ) == 0 )
{
/* wait */
}

random = trng_getranddata(rng);
/* clear the status flag after reading to generate new random
   value */
rng->status_clr = RNG_STATUS_RDY;
rtems_mutex_unlock(_trng_reg);

/* checking for error by masking all bits other then error bit in
   status register */
if( ((rng->status & RNG_STATUS_ERR)>>1) == 1)
{
copy = sizeof(random);
if ( n < copy )
{
copy = n;
}
memcpy(ptr, , copy);
n -= copy;
ptr = (char*)ptr + copy;
}
}

rtems_mutex_destroy(_trng_reg);
return 0;
}


On Wed, Mar 14, 2018 at 2:15 AM, Joel Sherrill <j...@rtems.org> wrote:

>
>
> On Mar 13, 2018 1:31 AM, "Sebastian Huber" <sebastian.huber@embedded-
> brains.de> wrote:
>
>
>
> On 12/03/18 20:02, Udit agarwal wrote:
>
>> So, It looks like here's the final patch, do let me know if its ready to
>> be pushed. Also, it would be really helpful if someone else also tests this
>> patch before pushing(Although i have done that once).
>>
>> Thanks,
>> Udit agarwal
>>
>>
>> From 454a8ff3e0ea3393818859874705a54b098c6081 Mon Sep 17 00:00:00 2001
>> From: Udit agarwal <dev.mada...@gmail.com <mailto:dev.mada...@gmail.com>>
>>
>> Date: Tue, 13 Mar 2018 00:20:28 +0530
>> Subject: [PATCH] Added Getentropy() support to beagle BSP
>>
>> ---
>>  bsps/arm/include/libcpu/am335x.h   |  37 ++-
>>  c/src/lib/libbsp/arm/beagle/Makefile.am|   4 +-
>>  .../libbsp/arm/beagle/getentropy/bbb_getentropy.c  | 116
>> +
>>  3 files changed, 155 insertions(+), 2 deletions(-)
>>  create mode 100644 c/src/lib/libbsp/arm/beagle/ge
>> tentropy/bbb_getentropy.c
>>
>> diff --git a/bsps/arm/include/libcpu/am335x.h
>> b/bsps/arm/include/libcpu/am335x.h
>> index 367e97c..cedd637 100644
>> --- a/bsps/arm/include/libcpu/am335x.h
>> +++ b/bsps/arm/include/libcpu/am335x.h
>> @@ -14,11 +14,17 @@
>>   * Modified by Ben Gras <b...@shrike-systems.com > b...@shrike-systems.com>> to add lots
>>
>>   * of beagleboard/beaglebone definitions, delete lpc32xx specific
>>   * ones, and merge with some other header files.
>> + *
>> + * Modified by Udit agarwal <dev.mada...@gmail.com > dev.mada...@gmail.com>> to add random
>>
>> + * number generating module definitions and TRNG register structure.
>>   */
>>
>>  #if !defined(_AM335X_H_)
>>  #define _AM335X_H_
>>
>> +/* For TRNG register definition */
>> +#include 
>> +
>>  /* Interrupt controller memory map */
>>  #define OMAP3_DM37XX_INTR_BASE 0x4820 /* INTCPS physical address */
>>
>> @@ -701,4 +707,33 @@
>>  #define AM335X_CM_PER_OCPWP_L3_CLKSTCTRL_CLKACTIVITY_OCPWP_L4_GCLK
>> (0x0020u)
>>  #define AM335X_I2C_INT_STOP_CONDITION AM335X_I2C_IRQSTATUS_BF
>>
>> -#endif
>> +/* TRNG Register */
>> +
>> +/* RNG base address */
>> +#define RNG_BASE 0x4831
>> +/* RNG clock control */
>> +#define CM_PER_RNG_CLKCTRL (AM335X_CM_PER_ADDR | (9 << 4))
>> +/* rng module clock status bits */
>> +#define AM335X_CLK_RNG_BIT_MASK (0x3)
>> +/* Offset from RNG base for output ready flag */
>> +#define RNG_STATUS_RDY (1u <<  0)
>> +/* Offset from RNG base for FRO related error */
>> +#define RNG_STATUS_ERR (1u <<  1)
>> +/* Offset from RNG base for clock status */
>> +#define RNG_STATUS_CLK (1u << 31)
>> +/* enable module */
>> +#define AM335X_RNG_ENABLE (1 << 10)
>> +
>> +/* TRNG register structure */
>> +struct bbb_trng_register
>> +{
>> +uint64_t output; /* 00 */
>> +uint32_t status; /* 08 */
>> +uint32_t irq_en; /* 0c */
>> +uint32_t status_

Re: [PATCH] Added Getentropy() support to beagle BSP

2018-03-13 Thread Joel Sherrill
On Mar 13, 2018 1:31 AM, "Sebastian Huber" <
sebastian.hu...@embedded-brains.de> wrote:



On 12/03/18 20:02, Udit agarwal wrote:

> So, It looks like here's the final patch, do let me know if its ready to
> be pushed. Also, it would be really helpful if someone else also tests this
> patch before pushing(Although i have done that once).
>
> Thanks,
> Udit agarwal
>
>
> From 454a8ff3e0ea3393818859874705a54b098c6081 Mon Sep 17 00:00:00 2001
> From: Udit agarwal <dev.mada...@gmail.com <mailto:dev.mada...@gmail.com>>
>
> Date: Tue, 13 Mar 2018 00:20:28 +0530
> Subject: [PATCH] Added Getentropy() support to beagle BSP
>
> ---
>  bsps/arm/include/libcpu/am335x.h   |  37 ++-
>  c/src/lib/libbsp/arm/beagle/Makefile.am|   4 +-
>  .../libbsp/arm/beagle/getentropy/bbb_getentropy.c  | 116
> +
>  3 files changed, 155 insertions(+), 2 deletions(-)
>  create mode 100644 c/src/lib/libbsp/arm/beagle/ge
> tentropy/bbb_getentropy.c
>
> diff --git a/bsps/arm/include/libcpu/am335x.h
> b/bsps/arm/include/libcpu/am335x.h
> index 367e97c..cedd637 100644
> --- a/bsps/arm/include/libcpu/am335x.h
> +++ b/bsps/arm/include/libcpu/am335x.h
> @@ -14,11 +14,17 @@
>   * Modified by Ben Gras <b...@shrike-systems.com  b...@shrike-systems.com>> to add lots
>
>   * of beagleboard/beaglebone definitions, delete lpc32xx specific
>   * ones, and merge with some other header files.
> + *
> + * Modified by Udit agarwal <dev.mada...@gmail.com  dev.mada...@gmail.com>> to add random
>
> + * number generating module definitions and TRNG register structure.
>   */
>
>  #if !defined(_AM335X_H_)
>  #define _AM335X_H_
>
> +/* For TRNG register definition */
> +#include 
> +
>  /* Interrupt controller memory map */
>  #define OMAP3_DM37XX_INTR_BASE 0x4820 /* INTCPS physical address */
>
> @@ -701,4 +707,33 @@
>  #define AM335X_CM_PER_OCPWP_L3_CLKSTCTRL_CLKACTIVITY_OCPWP_L4_GCLK
> (0x0020u)
>  #define AM335X_I2C_INT_STOP_CONDITION AM335X_I2C_IRQSTATUS_BF
>
> -#endif
> +/* TRNG Register */
> +
> +/* RNG base address */
> +#define RNG_BASE 0x4831
> +/* RNG clock control */
> +#define CM_PER_RNG_CLKCTRL (AM335X_CM_PER_ADDR | (9 << 4))
> +/* rng module clock status bits */
> +#define AM335X_CLK_RNG_BIT_MASK (0x3)
> +/* Offset from RNG base for output ready flag */
> +#define RNG_STATUS_RDY (1u <<  0)
> +/* Offset from RNG base for FRO related error */
> +#define RNG_STATUS_ERR (1u <<  1)
> +/* Offset from RNG base for clock status */
> +#define RNG_STATUS_CLK (1u << 31)
> +/* enable module */
> +#define AM335X_RNG_ENABLE (1 << 10)
> +
> +/* TRNG register structure */
> +struct bbb_trng_register
> +{
> +uint64_t output; /* 00 */
> +uint32_t status; /* 08 */
> +uint32_t irq_en; /* 0c */
> +uint32_t status_clr; /* 10 */
> +uint32_t control;/* 14 */
> +uint32_t config; /* 18 */
> +};
> +typedef struct bbb_trng_register bbb_trng_register;
>

The bbb (Beagle Bone Black) is a particular board and the AM335X is a SoC
family. This should be something like this

typedef struct {
 ...
} am335x_trng;


+
> +#endif
> \ No newline at end of file
>

Git thinks that files should have a newline at the end of the file.


diff --git a/c/src/lib/libbsp/arm/beagle/Makefile.am
> b/c/src/lib/libbsp/arm/beagle/Makefile.am
> index 8251660..5d5ade3 100644
> --- a/c/src/lib/libbsp/arm/beagle/Makefile.am
> +++ b/c/src/lib/libbsp/arm/beagle/Makefile.am
> @@ -40,7 +40,6 @@ libbsp_a_LIBADD =
>
>  # Shared
>  libbsp_a_SOURCES += ../../shared/bootcard.c
> -libbsp_a_SOURCES += ../../shared/getentropy-cpucounter.c
>  libbsp_a_SOURCES += ../../shared/src/bsp-fdt.c
>  libbsp_a_SOURCES += ../../shared/bspclean.c
>  libbsp_a_SOURCES += ../../shared/bspgetworkarea.c
> @@ -88,6 +87,9 @@ libbsp_a_SOURCES += gpio/bbb-gpio.c
>  #pwm
>  libbsp_a_SOURCES += pwm/pwm.c
>
> +#getentropy
> +libbsp_a_SOURCES += getentropy/bbb_getentropy.c
> +
>

With the new BSP source structure

https://devel.rtems.org/ticket/3285

this new file could be also placed at

bsps/arm/beagle/dev/getentropy.c


Is this specific to the Beagle or the SoC? Or a combination? Earlier you
said the structure was SoC specific?


[...]
>
> +int getentropy(void *ptr, size_t n)
> +{
> +volatile bbb_trng_register  *rng = (bbb_trng_register*) RNG_BASE;
> +am335x_rng_enable(rng);
> +while (n > 0)
> +{
> +uint64_t random;
> +size_t copy;
> +
> +/* wait untill RNG becomes ready with next set of random data */
> +while( ( rng->status & RNG_STATUS_RDY ) == 0

Re: [PATCH] Added Getentropy() support to beagle BSP

2018-03-13 Thread Christian Mauderer
Am 13.03.2018 um 15:59 schrieb Udit agarwal:
> Hi,
> I have updated the patch, following were the changes:
> *Removed trialling white spaces and added new line at the end of each file
> *am335x_rng_enable() is now being called only once during initialization.
> *changed  directory to /arm/beagle/dev/bbb_getentropy.c
> *Renamed TRNG register structure as: am335x_trng_register 
> I have tested the patch after making above changes and it worked as before.
> 
> I need some suggestion regarding the solution to the problem raised
> during parallel execution of getentropy(),
> In that case, Two processes can actually fetch same random number. I was
> thinking to implement some sort of
> flag set/reset variable in shared memory, which can be used to implement
> a lock for TRNG register access something
> like a binary semaphore.

Hello Udit,

seems to be a small trick question from Sebastian: The processor family
only has single cores. But the problem can occur there too: If a high
prio task interrupts a lower prio task while it is waiting for the flag,
the two tasks might get the same value.

If you protect the critical section with a mutex, that should fix it.
One of the self contained mutexes should be usable here:
https://docs.rtems.org/branches/master/c-user/self_contained_objects.html#mutual-exclusion

By the way: That bug exists on the ATSAM too.

Best regards

Christian Mauderer

> 
> From b10e55c7f5cc8ca95b0d9b6a8cc7b8e37330544b Mon Sep 17 00:00:00 2001
> From: Udit agarwal <dev.mada...@gmail.com <mailto:dev.mada...@gmail.com>>
> Date: Tue, 13 Mar 2018 15:50:43 +0530
> Subject: [PATCH] Added getentropy support to Beagle BSP
> 
> ---
>  bsps/arm/include/libcpu/am335x.h |  33 +++
>  c/src/lib/libbsp/arm/beagle/Makefile.am  |   4 +-
>  c/src/lib/libbsp/arm/beagle/dev/bbb_getentropy.c | 117
> +++
>  3 files changed, 153 insertions(+), 1 deletion(-)
>  create mode 100644 c/src/lib/libbsp/arm/beagle/dev/bbb_getentropy.c
> 
> diff --git a/bsps/arm/include/libcpu/am335x.h
> b/bsps/arm/include/libcpu/am335x.h
> index 367e97c..6170ef3 100644
> --- a/bsps/arm/include/libcpu/am335x.h
> +++ b/bsps/arm/include/libcpu/am335x.h
> @@ -14,11 +14,17 @@
>   * Modified by Ben Gras <b...@shrike-systems.com
> <mailto:b...@shrike-systems.com>> to add lots
>   * of beagleboard/beaglebone definitions, delete lpc32xx specific
>   * ones, and merge with some other header files.
> + *
> + * Modified by Udit agarwal <dev.mada...@gmail.com
> <mailto:dev.mada...@gmail.com>> to add random
> + * number generating module definitions and TRNG register structure.
>   */
>  
>  #if !defined(_AM335X_H_)
>  #define _AM335X_H_
>  
> +/* For TRNG register definition */
> +#include 
> +
>  /* Interrupt controller memory map */
>  #define OMAP3_DM37XX_INTR_BASE 0x4820 /* INTCPS physical address */
>  
> @@ -701,4 +707,31 @@
>  #define AM335X_CM_PER_OCPWP_L3_CLKSTCTRL_CLKACTIVITY_OCPWP_L4_GCLK
> (0x0020u)
>  #define AM335X_I2C_INT_STOP_CONDITION AM335X_I2C_IRQSTATUS_BF
>  
> +/* TRNG Register */
> +
> +/* RNG base address */
> +#define RNG_BASE 0x4831
> +/* RNG clock control */
> +#define CM_PER_RNG_CLKCTRL (AM335X_CM_PER_ADDR | (9 << 4))
> +/* rng module clock status bits */
> +#define AM335X_CLK_RNG_BIT_MASK (0x3)
> +/* Offset from RNG base for output ready flag */
> +#define RNG_STATUS_RDY (1u <<  0)
> +/* Offset from RNG base for FRO related error */
> +#define RNG_STATUS_ERR (1u <<  1)
> +/* Offset from RNG base for clock status */
> +#define RNG_STATUS_CLK (1u << 31)
> +/* enable module */
> +#define AM335X_RNG_ENABLE (1 << 10)
> +
> +/* TRNG register structure */
> +typedef struct {
> +  uint64_t output; /* 00 */
> +  uint32_t status; /* 08 */
> +  uint32_t irq_en; /* 0c */
> +  uint32_t status_clr; /* 10 */
> +  uint32_t control;    /* 14 */
> +  uint32_t config; /* 18 */
> +} am335x_trng_register;
> +
>  #endif
> diff --git a/c/src/lib/libbsp/arm/beagle/Makefile.am
> b/c/src/lib/libbsp/arm/beagle/Makefile.am
> index 8251660..c483dc4 100644
> --- a/c/src/lib/libbsp/arm/beagle/Makefile.am
> +++ b/c/src/lib/libbsp/arm/beagle/Makefile.am
> @@ -40,7 +40,6 @@ libbsp_a_LIBADD =
>  
>  # Shared
>  libbsp_a_SOURCES += ../../shared/bootcard.c
> -libbsp_a_SOURCES += ../../shared/getentropy-cpucounter.c
>  libbsp_a_SOURCES += ../../shared/src/bsp-fdt.c
>  libbsp_a_SOURCES += ../../shared/bspclean.c
>  libbsp_a_SOURCES += ../../shared/bspgetworkarea.c
> @@ -88,6 +87,9 @@ libbsp_a_SOURCES += gpio/bbb-gpio.c
>  #pwm
>  libbsp_a_SOURCES += pwm/pwm.c
>  
> +#getentropy
> +libbsp_a_SOURCE

Re: [PATCH] Added Getentropy() support to beagle BSP

2018-03-13 Thread Udit agarwal
Hi,
I have updated the patch, following were the changes:
*Removed trialling white spaces and added new line at the end of each file
*am335x_rng_enable() is now being called only once during initialization.
*changed  directory to /arm/beagle/dev/bbb_getentropy.c
*Renamed TRNG register structure as: am335x_trng_register
I have tested the patch after making above changes and it worked as before.

I need some suggestion regarding the solution to the problem raised during
parallel execution of getentropy(),
In that case, Two processes can actually fetch same random number. I was
thinking to implement some sort of
flag set/reset variable in shared memory, which can be used to implement a
lock for TRNG register access something
like a binary semaphore.

>From b10e55c7f5cc8ca95b0d9b6a8cc7b8e37330544b Mon Sep 17 00:00:00 2001
From: Udit agarwal <dev.mada...@gmail.com>
Date: Tue, 13 Mar 2018 15:50:43 +0530
Subject: [PATCH] Added getentropy support to Beagle BSP

---
 bsps/arm/include/libcpu/am335x.h |  33 +++
 c/src/lib/libbsp/arm/beagle/Makefile.am  |   4 +-
 c/src/lib/libbsp/arm/beagle/dev/bbb_getentropy.c | 117
+++
 3 files changed, 153 insertions(+), 1 deletion(-)
 create mode 100644 c/src/lib/libbsp/arm/beagle/dev/bbb_getentropy.c

diff --git a/bsps/arm/include/libcpu/am335x.h
b/bsps/arm/include/libcpu/am335x.h
index 367e97c..6170ef3 100644
--- a/bsps/arm/include/libcpu/am335x.h
+++ b/bsps/arm/include/libcpu/am335x.h
@@ -14,11 +14,17 @@
  * Modified by Ben Gras <b...@shrike-systems.com> to add lots
  * of beagleboard/beaglebone definitions, delete lpc32xx specific
  * ones, and merge with some other header files.
+ *
+ * Modified by Udit agarwal <dev.mada...@gmail.com> to add random
+ * number generating module definitions and TRNG register structure.
  */

 #if !defined(_AM335X_H_)
 #define _AM335X_H_

+/* For TRNG register definition */
+#include 
+
 /* Interrupt controller memory map */
 #define OMAP3_DM37XX_INTR_BASE 0x4820 /* INTCPS physical address */

@@ -701,4 +707,31 @@
 #define AM335X_CM_PER_OCPWP_L3_CLKSTCTRL_CLKACTIVITY_OCPWP_L4_GCLK
(0x0020u)
 #define AM335X_I2C_INT_STOP_CONDITION AM335X_I2C_IRQSTATUS_BF

+/* TRNG Register */
+
+/* RNG base address */
+#define RNG_BASE 0x4831
+/* RNG clock control */
+#define CM_PER_RNG_CLKCTRL (AM335X_CM_PER_ADDR | (9 << 4))
+/* rng module clock status bits */
+#define AM335X_CLK_RNG_BIT_MASK (0x3)
+/* Offset from RNG base for output ready flag */
+#define RNG_STATUS_RDY (1u <<  0)
+/* Offset from RNG base for FRO related error */
+#define RNG_STATUS_ERR (1u <<  1)
+/* Offset from RNG base for clock status */
+#define RNG_STATUS_CLK (1u << 31)
+/* enable module */
+#define AM335X_RNG_ENABLE (1 << 10)
+
+/* TRNG register structure */
+typedef struct {
+  uint64_t output; /* 00 */
+  uint32_t status; /* 08 */
+  uint32_t irq_en; /* 0c */
+  uint32_t status_clr; /* 10 */
+  uint32_t control;/* 14 */
+  uint32_t config; /* 18 */
+} am335x_trng_register;
+
 #endif
diff --git a/c/src/lib/libbsp/arm/beagle/Makefile.am
b/c/src/lib/libbsp/arm/beagle/Makefile.am
index 8251660..c483dc4 100644
--- a/c/src/lib/libbsp/arm/beagle/Makefile.am
+++ b/c/src/lib/libbsp/arm/beagle/Makefile.am
@@ -40,7 +40,6 @@ libbsp_a_LIBADD =

 # Shared
 libbsp_a_SOURCES += ../../shared/bootcard.c
-libbsp_a_SOURCES += ../../shared/getentropy-cpucounter.c
 libbsp_a_SOURCES += ../../shared/src/bsp-fdt.c
 libbsp_a_SOURCES += ../../shared/bspclean.c
 libbsp_a_SOURCES += ../../shared/bspgetworkarea.c
@@ -88,6 +87,9 @@ libbsp_a_SOURCES += gpio/bbb-gpio.c
 #pwm
 libbsp_a_SOURCES += pwm/pwm.c

+#getentropy
+libbsp_a_SOURCES += dev/bbb_getentropy.c
+
 #RTC
 libbsp_a_SOURCES += rtc.c
 libbsp_a_SOURCES += ../../shared/tod.c
diff --git a/c/src/lib/libbsp/arm/beagle/dev/bbb_getentropy.c
b/c/src/lib/libbsp/arm/beagle/dev/bbb_getentropy.c
new file mode 100644
index 000..b2aea71
--- /dev/null
+++ b/c/src/lib/libbsp/arm/beagle/dev/bbb_getentropy.c
@@ -0,0 +1,117 @@
+/**
+* @file
+*
+* @ingroup arm_beagle
+*
+* @brief Getentropy implementation on BeagleBone Black BSP
+*/
+
+/*
+* Copyright (c) 2018 Udit agarwal 
+* All rights reserved.
+*
+* Redistribution and use in source and binary forms, with or without
+* modification, are permitted provided that the following conditions
+* are met:
+* 1. Redistributions of source code must retain the above copyright
+*notice, this list of conditions and the following disclaimer.
+* 2. Redistributions in binary form must reproduce the above copyright
+*notice, this list of conditions and the following disclaimer in the
+*documentation and/or other materials provided with the distribution.
+*
+* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE
+* ARE DISCLA

[PATCH] Added Getentropy() support to beagle BSP

2018-03-12 Thread Udit agarwal
So, It looks like here's the final patch, do let me know if its ready to be
pushed. Also, it would be really helpful if someone else also tests this
patch before pushing(Although i have done that once).

Thanks,
Udit agarwal


>From 454a8ff3e0ea3393818859874705a54b098c6081 Mon Sep 17 00:00:00 2001
From: Udit agarwal <dev.mada...@gmail.com>
Date: Tue, 13 Mar 2018 00:20:28 +0530
Subject: [PATCH] Added Getentropy() support to beagle BSP

---
 bsps/arm/include/libcpu/am335x.h   |  37 ++-
 c/src/lib/libbsp/arm/beagle/Makefile.am|   4 +-
 .../libbsp/arm/beagle/getentropy/bbb_getentropy.c  | 116
+
 3 files changed, 155 insertions(+), 2 deletions(-)
 create mode 100644 c/src/lib/libbsp/arm/beagle/getentropy/bbb_getentropy.c

diff --git a/bsps/arm/include/libcpu/am335x.h
b/bsps/arm/include/libcpu/am335x.h
index 367e97c..cedd637 100644
--- a/bsps/arm/include/libcpu/am335x.h
+++ b/bsps/arm/include/libcpu/am335x.h
@@ -14,11 +14,17 @@
  * Modified by Ben Gras <b...@shrike-systems.com> to add lots
  * of beagleboard/beaglebone definitions, delete lpc32xx specific
  * ones, and merge with some other header files.
+ *
+ * Modified by Udit agarwal <dev.mada...@gmail.com> to add random
+ * number generating module definitions and TRNG register structure.
  */

 #if !defined(_AM335X_H_)
 #define _AM335X_H_

+/* For TRNG register definition */
+#include 
+
 /* Interrupt controller memory map */
 #define OMAP3_DM37XX_INTR_BASE 0x4820 /* INTCPS physical address */

@@ -701,4 +707,33 @@
 #define AM335X_CM_PER_OCPWP_L3_CLKSTCTRL_CLKACTIVITY_OCPWP_L4_GCLK
(0x0020u)
 #define AM335X_I2C_INT_STOP_CONDITION AM335X_I2C_IRQSTATUS_BF

-#endif
+/* TRNG Register */
+
+/* RNG base address */
+#define RNG_BASE 0x4831
+/* RNG clock control */
+#define CM_PER_RNG_CLKCTRL (AM335X_CM_PER_ADDR | (9 << 4))
+/* rng module clock status bits */
+#define AM335X_CLK_RNG_BIT_MASK (0x3)
+/* Offset from RNG base for output ready flag */
+#define RNG_STATUS_RDY (1u <<  0)
+/* Offset from RNG base for FRO related error */
+#define RNG_STATUS_ERR (1u <<  1)
+/* Offset from RNG base for clock status */
+#define RNG_STATUS_CLK (1u << 31)
+/* enable module */
+#define AM335X_RNG_ENABLE (1 << 10)
+
+/* TRNG register structure */
+struct bbb_trng_register
+{
+uint64_t output; /* 00 */
+uint32_t status; /* 08 */
+uint32_t irq_en; /* 0c */
+uint32_t status_clr; /* 10 */
+uint32_t control;/* 14 */
+uint32_t config; /* 18 */
+};
+typedef struct bbb_trng_register bbb_trng_register;
+
+#endif
\ No newline at end of file
diff --git a/c/src/lib/libbsp/arm/beagle/Makefile.am
b/c/src/lib/libbsp/arm/beagle/Makefile.am
index 8251660..5d5ade3 100644
--- a/c/src/lib/libbsp/arm/beagle/Makefile.am
+++ b/c/src/lib/libbsp/arm/beagle/Makefile.am
@@ -40,7 +40,6 @@ libbsp_a_LIBADD =

 # Shared
 libbsp_a_SOURCES += ../../shared/bootcard.c
-libbsp_a_SOURCES += ../../shared/getentropy-cpucounter.c
 libbsp_a_SOURCES += ../../shared/src/bsp-fdt.c
 libbsp_a_SOURCES += ../../shared/bspclean.c
 libbsp_a_SOURCES += ../../shared/bspgetworkarea.c
@@ -88,6 +87,9 @@ libbsp_a_SOURCES += gpio/bbb-gpio.c
 #pwm
 libbsp_a_SOURCES += pwm/pwm.c

+#getentropy
+libbsp_a_SOURCES += getentropy/bbb_getentropy.c
+
 #RTC
 libbsp_a_SOURCES += rtc.c
 libbsp_a_SOURCES += ../../shared/tod.c
diff --git a/c/src/lib/libbsp/arm/beagle/getentropy/bbb_getentropy.c
b/c/src/lib/libbsp/arm/beagle/getentropy/bbb_getentropy.c
new file mode 100644
index 000..134bf3b
--- /dev/null
+++ b/c/src/lib/libbsp/arm/beagle/getentropy/bbb_getentropy.c
@@ -0,0 +1,116 @@
+/**
+* @file
+*
+* @ingroup arm_beagle
+*
+* @brief Getentropy implementation on BeagleBone Black BSP
+*/
+
+/*
+* Copyright (c) 2018 Udit agarwal 
+* All rights reserved.
+*
+* Redistribution and use in source and binary forms, with or without
+* modification, are permitted provided that the following conditions
+* are met:
+* 1. Redistributions of source code must retain the above copyright
+*notice, this list of conditions and the following disclaimer.
+* 2. Redistributions in binary form must reproduce the above copyright
+*notice, this list of conditions and the following disclaimer in the
+*documentation and/or other materials provided with the distribution.
+*
+* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE
+* ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL
+* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
STRICT
+* LIABILITY, OR TORT (INCLUDING