[PATCH 2/2] crypto: ecdh: fix to allow multi segment scatterlists

2018-03-01 Thread James Bottomley
Apparently the ecdh use case was in bluetooth which always has single
element scatterlists, so the ecdh module was hard coded to expect
them.  Now we're using this in TPM, we need multi-element
scatterlists, so remove this limitation.

Signed-off-by: James Bottomley 
---
 crypto/ecdh.c | 23 +--
 1 file changed, 17 insertions(+), 6 deletions(-)

diff --git a/crypto/ecdh.c b/crypto/ecdh.c
index 3aca0933ec44..d2ec33f0e098 100644
--- a/crypto/ecdh.c
+++ b/crypto/ecdh.c
@@ -89,12 +89,19 @@ static int ecdh_compute_value(struct kpp_request *req)
if (!shared_secret)
goto free_pubkey;
 
-   copied = sg_copy_to_buffer(req->src, 1, public_key,
-  public_key_sz);
-   if (copied != public_key_sz) {
-   ret = -EINVAL;
+   /* from here on it's invalid parameters */
+   ret = -EINVAL;
+
+   /* must have exactly two points to be on the curve */
+   if (public_key_sz != req->src_len)
+   goto free_all;
+
+   copied = sg_copy_to_buffer(req->src,
+  sg_nents_for_len(req->src,
+   public_key_sz),
+  public_key, public_key_sz);
+   if (copied != public_key_sz)
goto free_all;
-   }
 
ret = crypto_ecdh_shared_secret(ctx->curve_id, ctx->ndigits,
ctx->private_key, public_key,
@@ -111,7 +118,11 @@ static int ecdh_compute_value(struct kpp_request *req)
if (ret < 0)
goto free_all;
 
-   copied = sg_copy_from_buffer(req->dst, 1, buf, nbytes);
+   /* might want less than we've got */
+   nbytes = min_t(size_t, nbytes, req->dst_len);
+   copied = sg_copy_from_buffer(req->dst, sg_nents_for_len(req->dst,
+   nbytes),
+buf, nbytes);
if (copied != nbytes)
ret = -EINVAL;
 
-- 
2.12.3


[PATCH 1/2] crypto: cfb: add support for Cipher FeedBack mode

2018-03-01 Thread James Bottomley
TPM security routines require encryption and decryption with AES in
CFB mode, so add it to the Linux Crypto schemes.  CFB is basically a
one time pad where the pad is generated initially from the encrypted
IV and then subsequently from the encrypted previous block of
ciphertext.  The pad is XOR'd into the plain text to get the final
ciphertext.

https://en.wikipedia.org/wiki/Block_cipher_mode_of_operation#CFB

Signed-off-by: James Bottomley 
---
 crypto/Kconfig  |   8 ++
 crypto/Makefile |   1 +
 crypto/cfb.c| 353 
 3 files changed, 362 insertions(+)
 create mode 100644 crypto/cfb.c

diff --git a/crypto/Kconfig b/crypto/Kconfig
index b75264b09a46..d43f2f677a10 100644
--- a/crypto/Kconfig
+++ b/crypto/Kconfig
@@ -324,6 +324,14 @@ config CRYPTO_CBC
  CBC: Cipher Block Chaining mode
  This block cipher algorithm is required for IPSec.
 
+config CRYPTO_CFB
+   tristate "CFB support"
+   select CRYPTO_BLKCIPHER
+   select CRYPTO_MANAGER
+   help
+ CFB: Cipher FeedBack mode
+ This block cipher algorithm is required for TPM2 Cryptography.
+
 config CRYPTO_CTR
tristate "CTR support"
select CRYPTO_BLKCIPHER
diff --git a/crypto/Makefile b/crypto/Makefile
index cdbc03b35510..0dcad117532e 100644
--- a/crypto/Makefile
+++ b/crypto/Makefile
@@ -78,6 +78,7 @@ obj-$(CONFIG_CRYPTO_TGR192) += tgr192.o
 obj-$(CONFIG_CRYPTO_GF128MUL) += gf128mul.o
 obj-$(CONFIG_CRYPTO_ECB) += ecb.o
 obj-$(CONFIG_CRYPTO_CBC) += cbc.o
+obj-$(CONFIG_CRYPTO_CFB) += cfb.o
 obj-$(CONFIG_CRYPTO_PCBC) += pcbc.o
 obj-$(CONFIG_CRYPTO_CTS) += cts.o
 obj-$(CONFIG_CRYPTO_LRW) += lrw.o
diff --git a/crypto/cfb.c b/crypto/cfb.c
new file mode 100644
index ..94ee39bed758
--- /dev/null
+++ b/crypto/cfb.c
@@ -0,0 +1,353 @@
+//SPDX-License-Identifier: GPL-2.0
+/*
+ * CFB: Cipher FeedBack mode
+ *
+ * Copyright (c) 2018 james.bottom...@hansenpartnership.com
+ *
+ * CFB is a stream cipher mode which is layered on to a block
+ * encryption scheme.  It works very much like a one time pad where
+ * the pad is generated initially from the encrypted IV and then
+ * subsequently from the encrypted previous block of ciphertext.  The
+ * pad is XOR'd into the plain text to get the final ciphertext.
+ *
+ * The scheme of CFB is best described by wikipedia:
+ *
+ * https://en.wikipedia.org/wiki/Block_cipher_mode_of_operation#CFB
+ *
+ * Note that since the pad for both encryption and decryption is
+ * generated by an encryption operation, CFB never uses the block
+ * decryption function.
+ */
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+struct crypto_cfb_ctx {
+   struct crypto_cipher *child;
+};
+
+static unsigned int crypto_cfb_bsize(struct crypto_skcipher *tfm)
+{
+   struct crypto_cfb_ctx *ctx = crypto_skcipher_ctx(tfm);
+   struct crypto_cipher *child = ctx->child;
+
+   return crypto_cipher_blocksize(child);
+}
+
+static void crypto_cfb_encrypt_one(struct crypto_skcipher *tfm,
+ const u8 *src, u8 *dst)
+{
+   struct crypto_cfb_ctx *ctx = crypto_skcipher_ctx(tfm);
+
+   crypto_cipher_encrypt_one(ctx->child, dst, src);
+}
+
+/* final encrypt and decrypt is the same */
+static void crypto_cfb_final(struct skcipher_walk *walk,
+struct crypto_skcipher *tfm)
+{
+   const unsigned int bsize = crypto_cfb_bsize(tfm);
+   const unsigned long alignmask = crypto_skcipher_alignmask(tfm);
+   u8 tmp[bsize + alignmask];
+   u8 *stream = PTR_ALIGN(tmp + 0, alignmask + 1);
+   u8 *src = walk->src.virt.addr;
+   u8 *dst = walk->dst.virt.addr;
+   u8 *iv = walk->iv;
+   unsigned int nbytes = walk->nbytes;
+
+   crypto_cfb_encrypt_one(tfm, iv, stream);
+   crypto_xor_cpy(dst, stream, src, nbytes);
+}
+
+static int crypto_cfb_encrypt_segment(struct skcipher_walk *walk,
+ struct crypto_skcipher *tfm)
+{
+   const unsigned int bsize = crypto_cfb_bsize(tfm);
+   unsigned int nbytes = walk->nbytes;
+   u8 *src = walk->src.virt.addr;
+   u8 *dst = walk->dst.virt.addr;
+   u8 *iv = walk->iv;
+
+   do {
+   crypto_cfb_encrypt_one(tfm, iv, dst);
+   crypto_xor(dst, src, bsize);
+   memcpy(iv, dst, bsize);
+
+   src += bsize;
+   dst += bsize;
+   } while ((nbytes -= bsize) >= bsize);
+
+   return nbytes;
+}
+
+static int crypto_cfb_encrypt_inplace(struct skcipher_walk *walk,
+ struct crypto_skcipher *tfm)
+{
+   const unsigned int bsize = crypto_cfb_bsize(tfm);
+   unsigned int nbytes = walk->nbytes;
+   u8 *src = walk->src.virt.addr;
+   u8 *iv = walk->iv;
+   u8 tmp[bsize];
+
+   do {
+   crypto_cfb_encrypt_one(tfm, iv, tmp);
+   crypto_xor(src, tmp, bsize);
+   

[PATCH 0/2] add crypto support for TPM communication

2018-03-01 Thread James Bottomley
To support cryptographic communication with the TPM, we need to add
Cipher FeedBack (CFB) mode for stream encryption because this is the
mandated encryption scheme for all encrypted parameters and responses.
 Additionally, we ran across a problem in the elliptic curve routines
in that the size of the scatterlist is hard coded to 1 which causes a
kernel BUG if you use a longer scatterlist.  Since all the current
kernel consumers use a single element scatterlist, this bug won't
manifest until we add the TPM routines to use crypto, so I didn't mark
it for stable.

James Bottomley (2):
  crypto: cfb: add support for Cipher FeedBack mode
  crypto: ecdh: fix to allow multi segment scatterlists

 crypto/Kconfig  |   8 ++
 crypto/Makefile |   1 +
 crypto/cfb.c| 353 
 crypto/ecdh.c   |  23 +++-
 4 files changed, 379 insertions(+), 6 deletions(-)
 create mode 100644 crypto/cfb.c

-- 
2.12.3


Re: [PATCH 2/2] hwrng: mxc-rnga - add driver support on boards with device tree

2018-03-01 Thread Vladimir Zapolskiy
On 02/27/2018 10:07 PM, Vladimir Zapolskiy wrote:
> On 02/27/2018 09:39 PM, Kim Phillips wrote:
>> On Tue, 27 Feb 2018 18:53:08 +0200
>> Vladimir Zapolskiy  wrote:
>>
>>> On 02/27/2018 05:49 PM, Kim Phillips wrote:
 On Mon, 26 Feb 2018 20:38:49 +0200
 Vladimir Zapolskiy  wrote:

> +#ifdef CONFIG_OF
> +static const struct of_device_id mxc_rnga_of_match[] = {
> + { .compatible = "fsl,imx31-rnga", },
> + { /* sentinel */ },
> +};
> +MODULE_DEVICE_TABLE(of, mxc_rnga_of_match);
> +#endif
> +
>  static struct platform_driver mxc_rnga_driver = {
>   .driver = {
> -.name = "mxc_rnga",
> -},
> + .name = "mxc_rnga",
> + .of_match_table = of_match_ptr(mxc_rnga_of_match),

 Does this build if CONFIG_OF is not set?
>>>
>>> Definitely it is expected to be built, you can verify it directly or
>>> check of_match_ptr() macro definition from include/linux/of.h
>>
>> Thanks, I verified it by removing the SOC_IMX31 dependency, and with
>> netwinder_defconfig as a base.  I also verified that the #ifdef
>> CONFIG_OF protecting the mxc_rnga_of_match definition is also not
>> needed.
> 
> That's a commonplace observation, but I have serious doubts, if it
> has become a common practice to remove CONFIG_OF and CONFIG_ACPI
> macro guards around device id lists. Still I would prefer to save
> compiled code size.
> 

I checked that all flavours of i.MX SoCs are under multiplatform build.
FWIW only 1 iMX31 board has DT support and 10 of them use platform
data, and I'd like to change the ratio. So it would be proper to
remove the set CONFIG_OF guard.

But what is significantly more important is that i.MX31 RNGA should
be defined as compatible with i.MX21 RNGA, and it obligates me to send
v2 with the corrected compatible name, I'll make both changes.

Thanks Kim for attracting my attention to possible improvements.

--
With best wishes,
Vladimir


[PATCH 2/4] crypto: omap-sham - Fix misleading indentation

2018-03-01 Thread Krzysztof Kozlowski
Commit 8043bb1ae03c ("crypto: omap-sham - convert driver logic to use
sgs for data xmit") removed the if() clause leaving the statement as is.
The intention was in that case to finish the request always so the goto
instruction seems sensible.

Remove the indentation to fix Smatch warning:
drivers/crypto/omap-sham.c:1761 omap_sham_done_task() warn: inconsistent 
indenting

Signed-off-by: Krzysztof Kozlowski 
---
 drivers/crypto/omap-sham.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/crypto/omap-sham.c b/drivers/crypto/omap-sham.c
index 7650b1b449bb..6cb6ab6f52c0 100644
--- a/drivers/crypto/omap-sham.c
+++ b/drivers/crypto/omap-sham.c
@@ -1758,7 +1758,7 @@ static void omap_sham_done_task(unsigned long data)
if (test_and_clear_bit(FLAGS_OUTPUT_READY, &dd->flags)) {
/* hash or semi-hash ready */
clear_bit(FLAGS_DMA_READY, &dd->flags);
-   goto finish;
+   goto finish;
}
}
 
-- 
2.7.4



[PATCH 3/4] crypto: s5p-sss: Remove useless check for non-null request

2018-03-01 Thread Krzysztof Kozlowski
ahash_request 'req' argument passed by the caller
s5p_hash_handle_queue() cannot be NULL here because it is obtained from
non-NULL pointer via container_of().

This fixes smatch warning:
drivers/crypto/s5p-sss.c:1213 s5p_hash_prepare_request() warn: variable 
dereferenced before check 'req' (see line 1208)

Signed-off-by: Krzysztof Kozlowski 
---
 drivers/crypto/s5p-sss.c | 3 ---
 1 file changed, 3 deletions(-)

diff --git a/drivers/crypto/s5p-sss.c b/drivers/crypto/s5p-sss.c
index 5d64c08b7f47..d7c8163e5068 100644
--- a/drivers/crypto/s5p-sss.c
+++ b/drivers/crypto/s5p-sss.c
@@ -1210,9 +1210,6 @@ static int s5p_hash_prepare_request(struct ahash_request 
*req, bool update)
int xmit_len, hash_later, nbytes;
int ret;
 
-   if (!req)
-   return 0;
-
if (update)
nbytes = req->nbytes;
else
-- 
2.7.4



[PATCH 4/4] crypto: s5p-sss - Constify pointed data (arguments and local variables)

2018-03-01 Thread Krzysztof Kozlowski
Improve the code (safety and readability) by indicating that data passed
through pointer is not modified.  This adds const keyword in many places,
most notably:
 - the driver data (pointer to struct samsung_aes_variant),
 - scatterlist addresses written as value to device registers,
 - key and IV arrays.

Signed-off-by: Krzysztof Kozlowski 
---
 drivers/crypto/s5p-sss.c | 31 +--
 1 file changed, 17 insertions(+), 14 deletions(-)

diff --git a/drivers/crypto/s5p-sss.c b/drivers/crypto/s5p-sss.c
index d7c8163e5068..bf7163042569 100644
--- a/drivers/crypto/s5p-sss.c
+++ b/drivers/crypto/s5p-sss.c
@@ -404,29 +404,31 @@ static const struct of_device_id s5p_sss_dt_match[] = {
 };
 MODULE_DEVICE_TABLE(of, s5p_sss_dt_match);
 
-static inline struct samsung_aes_variant *find_s5p_sss_version
-  (struct platform_device *pdev)
+static inline const struct samsung_aes_variant *find_s5p_sss_version
+  (const struct platform_device *pdev)
 {
if (IS_ENABLED(CONFIG_OF) && (pdev->dev.of_node)) {
const struct of_device_id *match;
 
match = of_match_node(s5p_sss_dt_match,
pdev->dev.of_node);
-   return (struct samsung_aes_variant *)match->data;
+   return (const struct samsung_aes_variant *)match->data;
}
-   return (struct samsung_aes_variant *)
+   return (const struct samsung_aes_variant *)
platform_get_device_id(pdev)->driver_data;
 }
 
 static struct s5p_aes_dev *s5p_dev;
 
-static void s5p_set_dma_indata(struct s5p_aes_dev *dev, struct scatterlist *sg)
+static void s5p_set_dma_indata(struct s5p_aes_dev *dev,
+  const struct scatterlist *sg)
 {
SSS_WRITE(dev, FCBRDMAS, sg_dma_address(sg));
SSS_WRITE(dev, FCBRDMAL, sg_dma_len(sg));
 }
 
-static void s5p_set_dma_outdata(struct s5p_aes_dev *dev, struct scatterlist 
*sg)
+static void s5p_set_dma_outdata(struct s5p_aes_dev *dev,
+   const struct scatterlist *sg)
 {
SSS_WRITE(dev, FCBTDMAS, sg_dma_address(sg));
SSS_WRITE(dev, FCBTDMAL, sg_dma_len(sg));
@@ -619,7 +621,7 @@ static inline void s5p_hash_write(struct s5p_aes_dev *dd,
  * @sg:scatterlist ready to DMA transmit
  */
 static void s5p_set_dma_hashdata(struct s5p_aes_dev *dev,
-struct scatterlist *sg)
+const struct scatterlist *sg)
 {
dev->hash_sg_cnt--;
SSS_WRITE(dev, FCHRDMAS, sg_dma_address(sg));
@@ -792,9 +794,9 @@ static void s5p_hash_read_msg(struct ahash_request *req)
  * @ctx:   request context
  */
 static void s5p_hash_write_ctx_iv(struct s5p_aes_dev *dd,
- struct s5p_hash_reqctx *ctx)
+ const struct s5p_hash_reqctx *ctx)
 {
-   u32 *hash = (u32 *)ctx->digest;
+   const u32 *hash = (const u32 *)ctx->digest;
unsigned int i;
 
for (i = 0; i < ctx->nregs; i++)
@@ -818,7 +820,7 @@ static void s5p_hash_write_iv(struct ahash_request *req)
  */
 static void s5p_hash_copy_result(struct ahash_request *req)
 {
-   struct s5p_hash_reqctx *ctx = ahash_request_ctx(req);
+   const struct s5p_hash_reqctx *ctx = ahash_request_ctx(req);
 
if (!req->result)
return;
@@ -1290,7 +1292,7 @@ static int s5p_hash_prepare_request(struct ahash_request 
*req, bool update)
  */
 static void s5p_hash_update_dma_stop(struct s5p_aes_dev *dd)
 {
-   struct s5p_hash_reqctx *ctx = ahash_request_ctx(dd->hash_req);
+   const struct s5p_hash_reqctx *ctx = ahash_request_ctx(dd->hash_req);
 
dma_unmap_sg(dd->dev, ctx->sg, ctx->sg_len, DMA_TO_DEVICE);
clear_bit(HASH_FLAGS_DMA_ACTIVE, &dd->hash_flags);
@@ -1717,7 +1719,7 @@ static void s5p_hash_cra_exit(struct crypto_tfm *tfm)
  */
 static int s5p_hash_export(struct ahash_request *req, void *out)
 {
-   struct s5p_hash_reqctx *ctx = ahash_request_ctx(req);
+   const struct s5p_hash_reqctx *ctx = ahash_request_ctx(req);
 
memcpy(out, ctx, sizeof(*ctx) + ctx->bufcnt);
 
@@ -1831,7 +1833,8 @@ static struct ahash_alg algs_sha1_md5_sha256[] = {
 };
 
 static void s5p_set_aes(struct s5p_aes_dev *dev,
-   uint8_t *key, uint8_t *iv, unsigned int keylen)
+   const uint8_t *key, const uint8_t *iv,
+   unsigned int keylen)
 {
void __iomem *keystart;
 
@@ -2150,7 +2153,7 @@ static int s5p_aes_probe(struct platform_device *pdev)
 {
struct device *dev = &pdev->dev;
int i, j, err = -ENODEV;
-   struct samsung_aes_variant *variant;
+   const struct samsung_aes_variant *variant;
struct s5p_aes_dev *pdata;
struct resource *res;
unsigned int hash_i;
-- 
2.7.4



[PATCH 1/4] crypto: omap-sham: Remove useless check for non-null request

2018-03-01 Thread Krzysztof Kozlowski
ahash_request 'req' argument passed by the caller
omap_sham_handle_queue() cannot be NULL here because it is obtained from
non-NULL pointer via container_of().

This fixes smatch warning:
drivers/crypto/omap-sham.c:812 omap_sham_prepare_request() warn: variable 
dereferenced before check 'req' (see line 805)

Signed-off-by: Krzysztof Kozlowski 
---
 drivers/crypto/omap-sham.c | 3 ---
 1 file changed, 3 deletions(-)

diff --git a/drivers/crypto/omap-sham.c b/drivers/crypto/omap-sham.c
index 86b89ace836f..7650b1b449bb 100644
--- a/drivers/crypto/omap-sham.c
+++ b/drivers/crypto/omap-sham.c
@@ -809,9 +809,6 @@ static int omap_sham_prepare_request(struct ahash_request 
*req, bool update)
bool final = rctx->flags & BIT(FLAGS_FINUP);
int xmit_len, hash_later;
 
-   if (!req)
-   return 0;
-
bs = get_block_size(rctx);
 
if (update)
-- 
2.7.4



[PATCH 0/2] Keystone2 HW random generator

2018-03-01 Thread Vitaly Andrianov
Keyston2 Security Accelerator has a hardware random generator sub-module.
This series adds the driver for the sub-module.

Vitaly Andrianov (2):
  Documentation: dt: rng: add bindings doc for Keystone SA HWRNG driver
  hw_random: keystone2: add hw_random driver

 .../devicetree/bindings/rng/ks-sa-rng.txt  |  21 ++
 drivers/char/hw_random/Kconfig |   7 +
 drivers/char/hw_random/Makefile|   1 +
 drivers/char/hw_random/ks-sa-rng.c | 257 +
 4 files changed, 286 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/rng/ks-sa-rng.txt
 create mode 100644 drivers/char/hw_random/ks-sa-rng.c

-- 
2.7.4



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

2018-03-01 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,
+   

[PATCH 1/2] Documentation: dt: rng: add bindings doc for Keystone SA HWRNG driver

2018-03-01 Thread Vitaly Andrianov
The Keystone SA module has a hardware random generator module.
This commit adds binding doc for the KS2 SA HWRNG driver.

Signed-off-by: Vitaly Andrianov 
Signed-off-by: Murali Karicheri 
---
 Documentation/devicetree/bindings/rng/ks-sa-rng.txt | 21 +
 1 file changed, 21 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/rng/ks-sa-rng.txt

diff --git a/Documentation/devicetree/bindings/rng/ks-sa-rng.txt 
b/Documentation/devicetree/bindings/rng/ks-sa-rng.txt
new file mode 100644
index 000..cc8ee44
--- /dev/null
+++ b/Documentation/devicetree/bindings/rng/ks-sa-rng.txt
@@ -0,0 +1,21 @@
+Keystone SoC Hardware Random Number Generator(HWRNG) Module
+
+On Keystone SoCs HWRNG module is a submodule of the Security Accelerator.
+
+- compatible: should be "ti,keystone-rng"
+- ti,syscon-sa-cfg: phandle to syscon node of the SA configuration registers.
+   This registers are shared between hwrng and crypto drivers.
+- clocks: phandle to the reference clocks for the subsystem
+- clock-names: functional clock name. Should be set to "fck"
+- reg: HWRNG module register space
+
+Example:
+/* K2HK */
+
+hwrng@0x24000 {
+   compatible = "ti,keystone-rng";
+   ti,syscon-sa-cfg = <&sa_config>;
+   clocks = <&clksa>;
+   clock-names = "fck";
+   reg = <0x24000 0x1000>;
+};
-- 
2.7.4