blob: 658c153d3edb2590b0b470e3067da9aac76169ac [file] [log] [blame]
Robert Markofa5d33f2020-10-08 22:05:13 +02001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * PRNG driver for Qualcomm IPQ40xx
4 *
5 * Copyright (c) 2020 Sartura Ltd.
6 *
7 * Author: Robert Marko <robert.marko@sartura.hr>
8 *
9 * Based on Linux driver
10 */
11
Robert Markofa5d33f2020-10-08 22:05:13 +020012#include <clk.h>
Robert Markofa5d33f2020-10-08 22:05:13 +020013#include <dm.h>
Robert Markofa5d33f2020-10-08 22:05:13 +020014#include <rng.h>
Heinrich Schuchardt10a45cc2024-02-13 00:44:47 +010015#include <asm/io.h>
16#include <linux/bitops.h>
Robert Markofa5d33f2020-10-08 22:05:13 +020017
18/* Device specific register offsets */
19#define PRNG_DATA_OUT 0x0000
20#define PRNG_STATUS 0x0004
21#define PRNG_LFSR_CFG 0x0100
22#define PRNG_CONFIG 0x0104
23
24/* Device specific register masks and config values */
25#define PRNG_LFSR_CFG_MASK 0x0000ffff
26#define PRNG_LFSR_CFG_CLOCKS 0x0000dddd
27#define PRNG_CONFIG_HW_ENABLE BIT(1)
28#define PRNG_STATUS_DATA_AVAIL BIT(0)
29
30#define MAX_HW_FIFO_DEPTH 16
31#define MAX_HW_FIFO_SIZE (MAX_HW_FIFO_DEPTH * 4)
32#define WORD_SZ 4
33
34struct msm_rng_priv {
35 phys_addr_t base;
36 struct clk clk;
37};
38
39static int msm_rng_read(struct udevice *dev, void *data, size_t len)
40{
41 struct msm_rng_priv *priv = dev_get_priv(dev);
42 size_t currsize = 0;
43 u32 *retdata = data;
44 size_t maxsize;
45 u32 val;
46
47 /* calculate max size bytes to transfer back to caller */
48 maxsize = min_t(size_t, MAX_HW_FIFO_SIZE, len);
49
50 /* read random data from hardware */
51 do {
52 val = readl_relaxed(priv->base + PRNG_STATUS);
53 if (!(val & PRNG_STATUS_DATA_AVAIL))
54 break;
55
56 val = readl_relaxed(priv->base + PRNG_DATA_OUT);
57 if (!val)
58 break;
59
60 *retdata++ = val;
61 currsize += WORD_SZ;
62
63 /* make sure we stay on 32bit boundary */
64 if ((maxsize - currsize) < WORD_SZ)
65 break;
66 } while (currsize < maxsize);
67
68 return 0;
69}
70
71static int msm_rng_enable(struct msm_rng_priv *priv, int enable)
72{
73 u32 val;
74
75 if (enable) {
76 /* Enable PRNG only if it is not already enabled */
77 val = readl_relaxed(priv->base + PRNG_CONFIG);
78 if (val & PRNG_CONFIG_HW_ENABLE) {
79 val = readl_relaxed(priv->base + PRNG_LFSR_CFG);
80 val &= ~PRNG_LFSR_CFG_MASK;
81 val |= PRNG_LFSR_CFG_CLOCKS;
82 writel(val, priv->base + PRNG_LFSR_CFG);
83
84 val = readl_relaxed(priv->base + PRNG_CONFIG);
85 val |= PRNG_CONFIG_HW_ENABLE;
86 writel(val, priv->base + PRNG_CONFIG);
87 }
88 } else {
89 val = readl_relaxed(priv->base + PRNG_CONFIG);
90 val &= ~PRNG_CONFIG_HW_ENABLE;
91 writel(val, priv->base + PRNG_CONFIG);
92 }
93
94 return 0;
95}
96
97static int msm_rng_probe(struct udevice *dev)
98{
99 struct msm_rng_priv *priv = dev_get_priv(dev);
100
101 int ret;
102
103 priv->base = dev_read_addr(dev);
104 if (priv->base == FDT_ADDR_T_NONE)
105 return -EINVAL;
106
107 ret = clk_get_by_index(dev, 0, &priv->clk);
108 if (ret)
109 return ret;
110
111 ret = clk_enable(&priv->clk);
112 if (ret < 0)
113 return ret;
114
115 return msm_rng_enable(priv, 1);
116}
117
118static int msm_rng_remove(struct udevice *dev)
119{
120 struct msm_rng_priv *priv = dev_get_priv(dev);
121
122 return msm_rng_enable(priv, 0);
123}
124
125static const struct dm_rng_ops msm_rng_ops = {
126 .read = msm_rng_read,
127};
128
129static const struct udevice_id msm_rng_match[] = {
130 { .compatible = "qcom,prng", },
131 {},
132};
133
134U_BOOT_DRIVER(msm_rng) = {
135 .name = "msm-rng",
136 .id = UCLASS_RNG,
137 .of_match = msm_rng_match,
138 .ops = &msm_rng_ops,
139 .probe = msm_rng_probe,
140 .remove = msm_rng_remove,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700141 .priv_auto = sizeof(struct msm_rng_priv),
Robert Markofa5d33f2020-10-08 22:05:13 +0200142};