[PATCH v2 2/2] hw_random: keystone2: add hw_random driver

2018-03-13 Thread Vitaly Andrianov
Keystone Security Accelerator module has a hardware random generator
sub-module. This commit adds the driver for this sub-module.

Signed-off-by: Vitaly Andrianov 
[t-kri...@ti.com: dropped one unnecessary dev_err message]
Signed-off-by: Tero Kristo 
Signed-off-by: Murali Karicheri 
---
 drivers/char/hw_random/Kconfig |   7 +
 drivers/char/hw_random/Makefile|   1 +
 drivers/char/hw_random/ks-sa-rng.c | 257 +
 3 files changed, 265 insertions(+)
 create mode 100644 drivers/char/hw_random/ks-sa-rng.c

diff --git a/drivers/char/hw_random/Kconfig b/drivers/char/hw_random/Kconfig
index 4d0f571..d53541e 100644
--- a/drivers/char/hw_random/Kconfig
+++ b/drivers/char/hw_random/Kconfig
@@ -452,3 +452,10 @@ config UML_RANDOM
  (check your distro, or download from
  http://sourceforge.net/projects/gkernel/).  rngd periodically reads
  /dev/hwrng and injects the entropy into /dev/random.
+
+config HW_RANDOM_KEYSTONE
+   depends on ARCH_KEYSTONE
+   default HW_RANDOM
+   tristate "TI Keystone NETCP SA Hardware random number generator"
+   help
+ This option enables Keystone's hardware random generator.
diff --git a/drivers/char/hw_random/Makefile b/drivers/char/hw_random/Makefile
index b780370..533e913 100644
--- a/drivers/char/hw_random/Makefile
+++ b/drivers/char/hw_random/Makefile
@@ -38,3 +38,4 @@ obj-$(CONFIG_HW_RANDOM_MESON) += meson-rng.o
 obj-$(CONFIG_HW_RANDOM_CAVIUM) += cavium-rng.o cavium-rng-vf.o
 obj-$(CONFIG_HW_RANDOM_MTK)+= mtk-rng.o
 obj-$(CONFIG_HW_RANDOM_S390) += s390-trng.o
+obj-$(CONFIG_HW_RANDOM_KEYSTONE) += ks-sa-rng.o
diff --git a/drivers/char/hw_random/ks-sa-rng.c 
b/drivers/char/hw_random/ks-sa-rng.c
new file mode 100644
index 000..62c6696
--- /dev/null
+++ b/drivers/char/hw_random/ks-sa-rng.c
@@ -0,0 +1,257 @@
+/*
+ * Random Number Generator driver for the Keystone SOC
+ *
+ * Copyright (C) 2016 Texas Instruments Incorporated - http://www.ti.com
+ *
+ * Authors:Sandeep Nair
+ * Vitaly Andrianov
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * version 2 as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ */
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+#define SA_CMD_STATUS_OFS  0x8
+
+/* TRNG enable control in SA System module*/
+#define SA_CMD_STATUS_REG_TRNG_ENABLE  BIT(3)
+
+/* TRNG start control in TRNG module */
+#define TRNG_CNTL_REG_TRNG_ENABLE  BIT(10)
+
+/* Data ready indicator in STATUS register */
+#define TRNG_STATUS_REG_READY  BIT(0)
+
+/* Data ready clear control in INTACK register */
+#define TRNG_INTACK_REG_READY  BIT(0)
+
+/*
+ * Number of samples taken to gather entropy during startup.
+ * If value is 0, the number of samples is 2^24 else
+ * equals value times 2^8.
+ */
+#define TRNG_DEF_STARTUP_CYCLES0
+#define TRNG_CNTL_REG_STARTUP_CYCLES_SHIFT 16
+
+/*
+ * Minimum number of samples taken to regenerate entropy
+ * If value is 0, the number of samples is 2^24 else
+ * equals value times 2^6.
+ */
+#define TRNG_DEF_MIN_REFILL_CYCLES 1
+#define TRNG_CFG_REG_MIN_REFILL_CYCLES_SHIFT   0
+
+/*
+ * Maximum number of samples taken to regenerate entropy
+ * If value is 0, the number of samples is 2^24 else
+ * equals value times 2^8.
+ */
+#define TRNG_DEF_MAX_REFILL_CYCLES 0
+#define TRNG_CFG_REG_MAX_REFILL_CYCLES_SHIFT   16
+
+/* Number of CLK input cycles between samples */
+#define TRNG_DEF_CLK_DIV_CYCLES0
+#define TRNG_CFG_REG_SAMPLE_DIV_SHIFT  8
+
+/* Maximum retries to get rng data */
+#define SA_MAX_RNG_DATA_RETRIES5
+/* Delay between retries (in usecs) */
+#define SA_RNG_DATA_RETRY_DELAY5
+
+struct trng_regs {
+   u32 output_l;
+   u32 output_h;
+   u32 status;
+   u32 intmask;
+   u32 intack;
+   u32 control;
+   u32 config;
+};
+
+struct ks_sa_rng {
+   struct device   *dev;
+   struct hwrngrng;
+   struct clk  *clk;
+   struct regmap   *regmap_cfg;
+   struct trng_regs *reg_rng;
+};
+
+static int ks_sa_rng_init(struct hwrng *rng)
+{
+   u32 value;
+   struct device *dev = (struct device *)rng->priv;
+   struct ks_sa_rng *ks_sa_rng = dev_get_drvdata(dev);
+
+   /* Enable RNG module */
+   regmap_write_bits(ks_sa_rng->regmap_cfg, SA_CMD_STATUS_OFS,
+  

[PATCH v2 2/2] hw_random: keystone2: add hw_random driver

2018-03-13 Thread Vitaly Andrianov
Keystone Security Accelerator module has a hardware random generator
sub-module. This commit adds the driver for this sub-module.

Signed-off-by: Vitaly Andrianov 
[t-kri...@ti.com: dropped one unnecessary dev_err message]
Signed-off-by: Tero Kristo 
Signed-off-by: Murali Karicheri 
---
 drivers/char/hw_random/Kconfig |   7 +
 drivers/char/hw_random/Makefile|   1 +
 drivers/char/hw_random/ks-sa-rng.c | 257 +
 3 files changed, 265 insertions(+)
 create mode 100644 drivers/char/hw_random/ks-sa-rng.c

diff --git a/drivers/char/hw_random/Kconfig b/drivers/char/hw_random/Kconfig
index 4d0f571..d53541e 100644
--- a/drivers/char/hw_random/Kconfig
+++ b/drivers/char/hw_random/Kconfig
@@ -452,3 +452,10 @@ config UML_RANDOM
  (check your distro, or download from
  http://sourceforge.net/projects/gkernel/).  rngd periodically reads
  /dev/hwrng and injects the entropy into /dev/random.
+
+config HW_RANDOM_KEYSTONE
+   depends on ARCH_KEYSTONE
+   default HW_RANDOM
+   tristate "TI Keystone NETCP SA Hardware random number generator"
+   help
+ This option enables Keystone's hardware random generator.
diff --git a/drivers/char/hw_random/Makefile b/drivers/char/hw_random/Makefile
index b780370..533e913 100644
--- a/drivers/char/hw_random/Makefile
+++ b/drivers/char/hw_random/Makefile
@@ -38,3 +38,4 @@ obj-$(CONFIG_HW_RANDOM_MESON) += meson-rng.o
 obj-$(CONFIG_HW_RANDOM_CAVIUM) += cavium-rng.o cavium-rng-vf.o
 obj-$(CONFIG_HW_RANDOM_MTK)+= mtk-rng.o
 obj-$(CONFIG_HW_RANDOM_S390) += s390-trng.o
+obj-$(CONFIG_HW_RANDOM_KEYSTONE) += ks-sa-rng.o
diff --git a/drivers/char/hw_random/ks-sa-rng.c 
b/drivers/char/hw_random/ks-sa-rng.c
new file mode 100644
index 000..62c6696
--- /dev/null
+++ b/drivers/char/hw_random/ks-sa-rng.c
@@ -0,0 +1,257 @@
+/*
+ * Random Number Generator driver for the Keystone SOC
+ *
+ * Copyright (C) 2016 Texas Instruments Incorporated - http://www.ti.com
+ *
+ * Authors:Sandeep Nair
+ * Vitaly Andrianov
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * version 2 as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ */
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+#define SA_CMD_STATUS_OFS  0x8
+
+/* TRNG enable control in SA System module*/
+#define SA_CMD_STATUS_REG_TRNG_ENABLE  BIT(3)
+
+/* TRNG start control in TRNG module */
+#define TRNG_CNTL_REG_TRNG_ENABLE  BIT(10)
+
+/* Data ready indicator in STATUS register */
+#define TRNG_STATUS_REG_READY  BIT(0)
+
+/* Data ready clear control in INTACK register */
+#define TRNG_INTACK_REG_READY  BIT(0)
+
+/*
+ * Number of samples taken to gather entropy during startup.
+ * If value is 0, the number of samples is 2^24 else
+ * equals value times 2^8.
+ */
+#define TRNG_DEF_STARTUP_CYCLES0
+#define TRNG_CNTL_REG_STARTUP_CYCLES_SHIFT 16
+
+/*
+ * Minimum number of samples taken to regenerate entropy
+ * If value is 0, the number of samples is 2^24 else
+ * equals value times 2^6.
+ */
+#define TRNG_DEF_MIN_REFILL_CYCLES 1
+#define TRNG_CFG_REG_MIN_REFILL_CYCLES_SHIFT   0
+
+/*
+ * Maximum number of samples taken to regenerate entropy
+ * If value is 0, the number of samples is 2^24 else
+ * equals value times 2^8.
+ */
+#define TRNG_DEF_MAX_REFILL_CYCLES 0
+#define TRNG_CFG_REG_MAX_REFILL_CYCLES_SHIFT   16
+
+/* Number of CLK input cycles between samples */
+#define TRNG_DEF_CLK_DIV_CYCLES0
+#define TRNG_CFG_REG_SAMPLE_DIV_SHIFT  8
+
+/* Maximum retries to get rng data */
+#define SA_MAX_RNG_DATA_RETRIES5
+/* Delay between retries (in usecs) */
+#define SA_RNG_DATA_RETRY_DELAY5
+
+struct trng_regs {
+   u32 output_l;
+   u32 output_h;
+   u32 status;
+   u32 intmask;
+   u32 intack;
+   u32 control;
+   u32 config;
+};
+
+struct ks_sa_rng {
+   struct device   *dev;
+   struct hwrngrng;
+   struct clk  *clk;
+   struct regmap   *regmap_cfg;
+   struct trng_regs *reg_rng;
+};
+
+static int ks_sa_rng_init(struct hwrng *rng)
+{
+   u32 value;
+   struct device *dev = (struct device *)rng->priv;
+   struct ks_sa_rng *ks_sa_rng = dev_get_drvdata(dev);
+
+   /* Enable RNG module */
+   regmap_write_bits(ks_sa_rng->regmap_cfg, SA_CMD_STATUS_OFS,
+ SA_CMD_STATUS_REG_TRNG_ENABLE,
+