Re: [PATCH v4 3/3] hwrng: mxc-fsl - add support for Freescale RNGC

2017-07-20 Thread PrasannaKumar Muralidharan
Hi Martin,

Nice to see a quick turnaround. Have a minor suggestion below.

On 20 July 2017 at 15:57, Martin Kaiser  wrote:
> From: Steffen Trumtrar 
>
> The driver is ported from Freescales Linux git and can be
> found in the
>
> vendor/freescale/imx_2.6.35_maintain
>
> branch.
>
> According to that code, the RNGC is found on Freescales i.MX3/5 SoCs.
> The i.MX2x actually has an RNGB, which has no driver implementation
> in Freescales kernel. However as it turns out, the driver for the RNGC
> works fine on the (at least) i.MX25. So, they seem to be somewhat
> compatible.
>
> Signed-off-by: Steffen Trumtrar 
> Signed-off-by: Martin Kaiser 
> ---
> Changes in v4:
>   - use readl, writel instead of the __raw versions
>   - move the self test to a separate function
>   - remove the error checks before the self test,
> it'll fail when there are errors
>   - run the self test in the probe function, add a parameter to skip it
>   - read the error status register before we clear irq and error
>   - rewrite the irq handler to read and store error status
>   - helper functions for irq mask, unmask
>   - remove some more unused defines
>   - fix a bounds check in the read function
>   - read function: check status before the fifo level
>   - clean up the file header, add myself to the copyright notice
>
> Changes in v3:
>- use pdev->dev to request the irq, rngc->dev is not yet initialized
>- remove unused defines for registers and fields
>- use module_platform_driver_probe()
>- clean up the error handling in the probe function,
>  disable the clock if necessary
>- self-test must succeed in the first run
>- check for errors after seeding, exit for errors unrelated to statistics
>- set a timeout when waiting for a completion
>
> Changes in v2:
>   - remove irq variable from private struct
>   - move devm_request_irq from mxc_rngc_init to probe
>   - return irq in case of error
>   - handle irq 0 as error
>
>  drivers/char/hw_random/Kconfig|  13 ++
>  drivers/char/hw_random/Makefile   |   1 +
>  drivers/char/hw_random/mxc-rngc.c | 338 
> ++
>  3 files changed, 352 insertions(+)
>  create mode 100644 drivers/char/hw_random/mxc-rngc.c
>
> diff --git a/drivers/char/hw_random/Kconfig b/drivers/char/hw_random/Kconfig
> index 1b223c3..ef057b7 100644
> --- a/drivers/char/hw_random/Kconfig
> +++ b/drivers/char/hw_random/Kconfig
> @@ -255,6 +255,19 @@ config HW_RANDOM_MXC_RNGA
>
>   If unsure, say Y.
>
> +config HW_RANDOM_MXC_RNGC
> +   tristate "Freescale i.MX RNGC Random Number Generator"
> +   depends on ARCH_MXC
> +   default HW_RANDOM
> +   ---help---
> + This driver provides kernel-side support for the Random Number
> + Generator hardware found on some Freescale i.MX processors.
> +
> + To compile this driver as a module, choose M here: the
> + module will be called mxc-rngc.
> +
> + If unsure, say Y.
> +
>  config HW_RANDOM_NOMADIK
> tristate "ST-Ericsson Nomadik Random Number Generator support"
> depends on ARCH_NOMADIK
> diff --git a/drivers/char/hw_random/Makefile b/drivers/char/hw_random/Makefile
> index b085975..043b71d 100644
> --- a/drivers/char/hw_random/Makefile
> +++ b/drivers/char/hw_random/Makefile
> @@ -20,6 +20,7 @@ obj-$(CONFIG_HW_RANDOM_PASEMI) += pasemi-rng.o
>  obj-$(CONFIG_HW_RANDOM_VIRTIO) += virtio-rng.o
>  obj-$(CONFIG_HW_RANDOM_TX4939) += tx4939-rng.o
>  obj-$(CONFIG_HW_RANDOM_MXC_RNGA) += mxc-rnga.o
> +obj-$(CONFIG_HW_RANDOM_MXC_RNGC) += mxc-rngc.o
>  obj-$(CONFIG_HW_RANDOM_OCTEON) += octeon-rng.o
>  obj-$(CONFIG_HW_RANDOM_NOMADIK) += nomadik-rng.o
>  obj-$(CONFIG_HW_RANDOM_PSERIES) += pseries-rng.o
> diff --git a/drivers/char/hw_random/mxc-rngc.c 
> b/drivers/char/hw_random/mxc-rngc.c
> new file mode 100644
> index 000..5733a8e
> --- /dev/null
> +++ b/drivers/char/hw_random/mxc-rngc.c
> @@ -0,0 +1,338 @@
> +/*
> + * RNG driver for Freescale RNGC
> + *
> + * Copyright (C) 2008-2012 Freescale Semiconductor, Inc.
> + * Copyright (C) 2017 Martin Kaiser 
> + *
> + * The code contained herein is licensed under the GNU General Public
> + * License. You may obtain a copy of the GNU General Public License
> + * Version 2 or later at the following locations:
> + *
> + * http://www.opensource.org/licenses/gpl-license.html
> + * http://www.gnu.org/copyleft/gpl.html
> + */
> +
> +#include 
> +#include 
> +#include 
> +#include 
> +#include 
> +#include 
> +#include 
> +#include 
> +#include 
> +#include 
> +
> +#define RNGC_COMMAND   0x0004
> +#define RNGC_CONTROL   0x0008
> +#define RNGC_STATUS0x000C
> +#define RNGC_ERROR 0x0010
> +#define RNGC_FIFO  0x0014
> +
> +#define RNGC_CMD_CLR_ERR   0x0020
> +#define RNGC_CMD_CLR_INT   0x0010
> +#define RNGC_CMD_SEED  0x0002
> +#define RNGC_CMD_SELF_TEST   

[PATCH v4 3/3] hwrng: mxc-fsl - add support for Freescale RNGC

2017-07-20 Thread Martin Kaiser
From: Steffen Trumtrar 

The driver is ported from Freescales Linux git and can be
found in the

vendor/freescale/imx_2.6.35_maintain

branch.

According to that code, the RNGC is found on Freescales i.MX3/5 SoCs.
The i.MX2x actually has an RNGB, which has no driver implementation
in Freescales kernel. However as it turns out, the driver for the RNGC
works fine on the (at least) i.MX25. So, they seem to be somewhat
compatible.

Signed-off-by: Steffen Trumtrar 
Signed-off-by: Martin Kaiser 
---
Changes in v4:
  - use readl, writel instead of the __raw versions
  - move the self test to a separate function
  - remove the error checks before the self test,
it'll fail when there are errors
  - run the self test in the probe function, add a parameter to skip it
  - read the error status register before we clear irq and error
  - rewrite the irq handler to read and store error status
  - helper functions for irq mask, unmask
  - remove some more unused defines
  - fix a bounds check in the read function
  - read function: check status before the fifo level
  - clean up the file header, add myself to the copyright notice

Changes in v3:
   - use pdev->dev to request the irq, rngc->dev is not yet initialized
   - remove unused defines for registers and fields
   - use module_platform_driver_probe()
   - clean up the error handling in the probe function,
 disable the clock if necessary
   - self-test must succeed in the first run
   - check for errors after seeding, exit for errors unrelated to statistics
   - set a timeout when waiting for a completion

Changes in v2:
  - remove irq variable from private struct
  - move devm_request_irq from mxc_rngc_init to probe
  - return irq in case of error
  - handle irq 0 as error

 drivers/char/hw_random/Kconfig|  13 ++
 drivers/char/hw_random/Makefile   |   1 +
 drivers/char/hw_random/mxc-rngc.c | 338 ++
 3 files changed, 352 insertions(+)
 create mode 100644 drivers/char/hw_random/mxc-rngc.c

diff --git a/drivers/char/hw_random/Kconfig b/drivers/char/hw_random/Kconfig
index 1b223c3..ef057b7 100644
--- a/drivers/char/hw_random/Kconfig
+++ b/drivers/char/hw_random/Kconfig
@@ -255,6 +255,19 @@ config HW_RANDOM_MXC_RNGA
 
  If unsure, say Y.
 
+config HW_RANDOM_MXC_RNGC
+   tristate "Freescale i.MX RNGC Random Number Generator"
+   depends on ARCH_MXC
+   default HW_RANDOM
+   ---help---
+ This driver provides kernel-side support for the Random Number
+ Generator hardware found on some Freescale i.MX processors.
+
+ To compile this driver as a module, choose M here: the
+ module will be called mxc-rngc.
+
+ If unsure, say Y.
+
 config HW_RANDOM_NOMADIK
tristate "ST-Ericsson Nomadik Random Number Generator support"
depends on ARCH_NOMADIK
diff --git a/drivers/char/hw_random/Makefile b/drivers/char/hw_random/Makefile
index b085975..043b71d 100644
--- a/drivers/char/hw_random/Makefile
+++ b/drivers/char/hw_random/Makefile
@@ -20,6 +20,7 @@ obj-$(CONFIG_HW_RANDOM_PASEMI) += pasemi-rng.o
 obj-$(CONFIG_HW_RANDOM_VIRTIO) += virtio-rng.o
 obj-$(CONFIG_HW_RANDOM_TX4939) += tx4939-rng.o
 obj-$(CONFIG_HW_RANDOM_MXC_RNGA) += mxc-rnga.o
+obj-$(CONFIG_HW_RANDOM_MXC_RNGC) += mxc-rngc.o
 obj-$(CONFIG_HW_RANDOM_OCTEON) += octeon-rng.o
 obj-$(CONFIG_HW_RANDOM_NOMADIK) += nomadik-rng.o
 obj-$(CONFIG_HW_RANDOM_PSERIES) += pseries-rng.o
diff --git a/drivers/char/hw_random/mxc-rngc.c 
b/drivers/char/hw_random/mxc-rngc.c
new file mode 100644
index 000..5733a8e
--- /dev/null
+++ b/drivers/char/hw_random/mxc-rngc.c
@@ -0,0 +1,338 @@
+/*
+ * RNG driver for Freescale RNGC
+ *
+ * Copyright (C) 2008-2012 Freescale Semiconductor, Inc.
+ * Copyright (C) 2017 Martin Kaiser 
+ *
+ * The code contained herein is licensed under the GNU General Public
+ * License. You may obtain a copy of the GNU General Public License
+ * Version 2 or later at the following locations:
+ *
+ * http://www.opensource.org/licenses/gpl-license.html
+ * http://www.gnu.org/copyleft/gpl.html
+ */
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+#define RNGC_COMMAND   0x0004
+#define RNGC_CONTROL   0x0008
+#define RNGC_STATUS0x000C
+#define RNGC_ERROR 0x0010
+#define RNGC_FIFO  0x0014
+
+#define RNGC_CMD_CLR_ERR   0x0020
+#define RNGC_CMD_CLR_INT   0x0010
+#define RNGC_CMD_SEED  0x0002
+#define RNGC_CMD_SELF_TEST 0x0001
+
+#define RNGC_CTRL_MASK_ERROR   0x0040
+#define RNGC_CTRL_MASK_DONE0x0020
+
+#define RNGC_STATUS_ERROR  0x0001
+#define RNGC_STATUS_FIFO_LEVEL_MASK0x0f00
+#define RNGC_STATUS_FIFO_LEVEL_SHIFT   8
+#define RNGC_STATUS_SEED_DONE  0x0020
+#define RNGC_STATUS_ST_DONE0x0010
+
+#defin