2929#define RNG_CTRL_RNG_RBGEN_MASK 0x00001FFF
3030#define RNG_CTRL_RNG_RBGEN_ENABLE 0x00000001
3131#define RNG_CTRL_RNG_RBGEN_DISABLE 0x00000000
32+ #define RNG_CTRL_RNG_DIV_CTRL_SHIFT 13
3233
3334#define RNG_SOFT_RESET_OFFSET 0x04
3435#define RNG_SOFT_RESET 0x00000001
3536
3637#define RBG_SOFT_RESET_OFFSET 0x08
3738#define RBG_SOFT_RESET 0x00000001
3839
40+ #define RNG_TOTAL_BIT_COUNT_OFFSET 0x0C
41+
42+ #define RNG_TOTAL_BIT_COUNT_THRESHOLD_OFFSET 0x10
43+
3944#define RNG_INT_STATUS_OFFSET 0x18
4045#define RNG_INT_STATUS_MASTER_FAIL_LOCKOUT_IRQ_MASK 0x80000000
4146#define RNG_INT_STATUS_STARTUP_TRANSITIONS_MET_IRQ_MASK 0x00020000
4247#define RNG_INT_STATUS_NIST_FAIL_IRQ_MASK 0x00000020
4348#define RNG_INT_STATUS_TOTAL_BITS_COUNT_IRQ_MASK 0x00000001
4449
50+ #define RNG_INT_ENABLE_OFFSET 0x1C
51+
4552#define RNG_FIFO_DATA_OFFSET 0x20
4653
4754#define RNG_FIFO_COUNT_OFFSET 0x24
4855#define RNG_FIFO_COUNT_RNG_FIFO_COUNT_MASK 0x000000FF
56+ #define RNG_FIFO_COUNT_RNG_FIFO_THRESHOLD_SHIFT 8
4957
5058struct iproc_rng200_dev {
5159 struct hwrng rng ;
@@ -166,6 +174,64 @@ static int iproc_rng200_init(struct hwrng *rng)
166174 return 0 ;
167175}
168176
177+ static int bcm2838_rng200_read (struct hwrng * rng , void * buf , size_t max ,
178+ bool wait )
179+ {
180+ struct iproc_rng200_dev * priv = to_rng_priv (rng );
181+ u32 max_words = max / sizeof (u32 );
182+ u32 num_words , count , val ;
183+
184+ /* ensure warm up period has elapsed */
185+ while (1 ) {
186+ val = ioread32 (priv -> base + RNG_TOTAL_BIT_COUNT_OFFSET );
187+ if (val > 16 )
188+ break ;
189+ cpu_relax ();
190+ }
191+
192+ /* ensure fifo is not empty */
193+ while (1 ) {
194+ num_words = ioread32 (priv -> base + RNG_FIFO_COUNT_OFFSET ) &
195+ RNG_FIFO_COUNT_RNG_FIFO_COUNT_MASK ;
196+ if (num_words )
197+ break ;
198+ if (!wait )
199+ return 0 ;
200+ cpu_relax ();
201+ }
202+
203+ if (num_words > max_words )
204+ num_words = max_words ;
205+
206+ for (count = 0 ; count < num_words ; count ++ ) {
207+ ((u32 * )buf )[count ] = ioread32 (priv -> base +
208+ RNG_FIFO_DATA_OFFSET );
209+ }
210+
211+ return num_words * sizeof (u32 );
212+ }
213+
214+ static int bcm2838_rng200_init (struct hwrng * rng )
215+ {
216+ struct iproc_rng200_dev * priv = to_rng_priv (rng );
217+ uint32_t val ;
218+
219+ if (ioread32 (priv -> base + RNG_CTRL_OFFSET ) & RNG_CTRL_RNG_RBGEN_MASK )
220+ return 0 ;
221+
222+ /* initial numbers generated are "less random" so will be discarded */
223+ val = 0x40000 ;
224+ iowrite32 (val , priv -> base + RNG_TOTAL_BIT_COUNT_THRESHOLD_OFFSET );
225+ /* min fifo count to generate full interrupt */
226+ val = 2 << RNG_FIFO_COUNT_RNG_FIFO_THRESHOLD_SHIFT ;
227+ iowrite32 (val , priv -> base + RNG_FIFO_COUNT_OFFSET );
228+ /* enable the rng - 1Mhz sample rate */
229+ val = (0x3 << RNG_CTRL_RNG_DIV_CTRL_SHIFT ) | RNG_CTRL_RNG_RBGEN_MASK ;
230+ iowrite32 (val , priv -> base + RNG_CTRL_OFFSET );
231+
232+ return 0 ;
233+ }
234+
169235static void iproc_rng200_cleanup (struct hwrng * rng )
170236{
171237 struct iproc_rng200_dev * priv = to_rng_priv (rng );
@@ -202,10 +268,16 @@ static int iproc_rng200_probe(struct platform_device *pdev)
202268 return PTR_ERR (priv -> base );
203269 }
204270
205- priv -> rng .name = "iproc-rng200" ,
206- priv -> rng .read = iproc_rng200_read ,
207- priv -> rng .init = iproc_rng200_init ,
208- priv -> rng .cleanup = iproc_rng200_cleanup ,
271+ priv -> rng .name = pdev -> name ;
272+ priv -> rng .cleanup = iproc_rng200_cleanup ;
273+
274+ if (of_device_is_compatible (dev -> of_node , "brcm,bcm2838-rng200" )) {
275+ priv -> rng .init = bcm2838_rng200_init ;
276+ priv -> rng .read = bcm2838_rng200_read ;
277+ } else {
278+ priv -> rng .init = iproc_rng200_init ;
279+ priv -> rng .read = iproc_rng200_read ;
280+ }
209281
210282 /* Register driver */
211283 ret = devm_hwrng_register (dev , & priv -> rng );
@@ -222,6 +294,7 @@ static int iproc_rng200_probe(struct platform_device *pdev)
222294static const struct of_device_id iproc_rng200_of_match [] = {
223295 { .compatible = "brcm,bcm7278-rng200" , },
224296 { .compatible = "brcm,iproc-rng200" , },
297+ { .compatible = "brcm,bcm2838-rng200" },
225298 {},
226299};
227300MODULE_DEVICE_TABLE (of , iproc_rng200_of_match );
0 commit comments