xref: /linux/drivers/char/hw_random/cctrng.c (revision 5b0764a53235d3c77e1ed674e836e1b3fe1a4b1f)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (C) 2019-2020 ARM Limited or its affiliates. */
3 
4 #include <linux/kernel.h>
5 #include <linux/module.h>
6 #include <linux/clk.h>
7 #include <linux/hw_random.h>
8 #include <linux/io.h>
9 #include <linux/platform_device.h>
10 #include <linux/pm_runtime.h>
11 #include <linux/interrupt.h>
12 #include <linux/irqreturn.h>
13 #include <linux/workqueue.h>
14 #include <linux/circ_buf.h>
15 #include <linux/completion.h>
16 #include <linux/of.h>
17 #include <linux/bitfield.h>
18 #include <linux/fips.h>
19 
20 #include "cctrng.h"
21 
22 #define CC_REG_LOW(name)  (name ## _BIT_SHIFT)
23 #define CC_REG_HIGH(name) (CC_REG_LOW(name) + name ## _BIT_SIZE - 1)
24 #define CC_GENMASK(name)  GENMASK(CC_REG_HIGH(name), CC_REG_LOW(name))
25 
26 #define CC_REG_FLD_GET(reg_name, fld_name, reg_val)     \
27 	(FIELD_GET(CC_GENMASK(CC_ ## reg_name ## _ ## fld_name), reg_val))
28 
29 #define CC_HW_RESET_LOOP_COUNT 10
30 #define CC_TRNG_SUSPEND_TIMEOUT 3000
31 
32 /* data circular buffer in words must be:
33  *  - of a power-of-2 size (limitation of circ_buf.h macros)
34  *  - at least 6, the size generated in the EHR according to HW implementation
35  */
36 #define CCTRNG_DATA_BUF_WORDS 32
37 
38 /* The timeout for the TRNG operation should be calculated with the formula:
39  * Timeout = EHR_NUM * VN_COEFF * EHR_LENGTH * SAMPLE_CNT * SCALE_VALUE
40  * while:
41  *  - SAMPLE_CNT is input value from the characterisation process
42  *  - all the rest are constants
43  */
44 #define EHR_NUM 1
45 #define VN_COEFF 4
46 #define EHR_LENGTH CC_TRNG_EHR_IN_BITS
47 #define SCALE_VALUE 2
48 #define CCTRNG_TIMEOUT(smpl_cnt) \
49 	(EHR_NUM * VN_COEFF * EHR_LENGTH * smpl_cnt * SCALE_VALUE)
50 
51 struct cctrng_drvdata {
52 	struct platform_device *pdev;
53 	void __iomem *cc_base;
54 	struct clk *clk;
55 	struct hwrng rng;
56 	u32 active_rosc;
57 	/* Sampling interval for each ring oscillator:
58 	 * count of ring oscillator cycles between consecutive bits sampling.
59 	 * Value of 0 indicates non-valid rosc
60 	 */
61 	u32 smpl_ratio[CC_TRNG_NUM_OF_ROSCS];
62 
63 	u32 data_buf[CCTRNG_DATA_BUF_WORDS];
64 	struct circ_buf circ;
65 	struct work_struct compwork;
66 	struct work_struct startwork;
67 
68 	/* pending_hw - 1 when HW is pending, 0 when it is idle */
69 	atomic_t pending_hw;
70 
71 	/* protects against multiple concurrent consumers of data_buf */
72 	spinlock_t read_lock;
73 };
74 
75 
76 /* functions for write/read CC registers */
77 static inline void cc_iowrite(struct cctrng_drvdata *drvdata, u32 reg, u32 val)
78 {
79 	iowrite32(val, (drvdata->cc_base + reg));
80 }
81 static inline u32 cc_ioread(struct cctrng_drvdata *drvdata, u32 reg)
82 {
83 	return ioread32(drvdata->cc_base + reg);
84 }
85 
86 
87 static int cc_trng_pm_get(struct device *dev)
88 {
89 	return pm_runtime_resume_and_get(dev);
90 }
91 
92 static void cc_trng_pm_put_suspend(struct device *dev)
93 {
94 	int rc;
95 
96 	rc = pm_runtime_put_autosuspend(dev);
97 	if (rc)
98 		dev_err(dev, "pm_runtime_put_autosuspend returned %x\n", rc);
99 }
100 
101 static int cc_trng_pm_init(struct cctrng_drvdata *drvdata)
102 {
103 	struct device *dev = &(drvdata->pdev->dev);
104 
105 	/* must be before the enabling to avoid redundant suspending */
106 	pm_runtime_set_autosuspend_delay(dev, CC_TRNG_SUSPEND_TIMEOUT);
107 	pm_runtime_use_autosuspend(dev);
108 	/* set us as active - note we won't do PM ops until cc_trng_pm_go()! */
109 	return pm_runtime_set_active(dev);
110 }
111 
112 static void cc_trng_pm_go(struct cctrng_drvdata *drvdata)
113 {
114 	struct device *dev = &(drvdata->pdev->dev);
115 
116 	/* enable the PM module*/
117 	pm_runtime_enable(dev);
118 }
119 
120 static void cc_trng_pm_fini(struct cctrng_drvdata *drvdata)
121 {
122 	struct device *dev = &(drvdata->pdev->dev);
123 
124 	pm_runtime_disable(dev);
125 }
126 
127 
128 static inline int cc_trng_parse_sampling_ratio(struct cctrng_drvdata *drvdata)
129 {
130 	struct device *dev = &(drvdata->pdev->dev);
131 	struct device_node *np = drvdata->pdev->dev.of_node;
132 	int rc;
133 	int i;
134 	/* ret will be set to 0 if at least one rosc has (sampling ratio > 0) */
135 	int ret = -EINVAL;
136 
137 	rc = of_property_read_u32_array(np, "arm,rosc-ratio",
138 					drvdata->smpl_ratio,
139 					CC_TRNG_NUM_OF_ROSCS);
140 	if (rc) {
141 		/* arm,rosc-ratio was not found in device tree */
142 		return rc;
143 	}
144 
145 	/* verify that at least one rosc has (sampling ratio > 0) */
146 	for (i = 0; i < CC_TRNG_NUM_OF_ROSCS; ++i) {
147 		dev_dbg(dev, "rosc %d sampling ratio %u",
148 			i, drvdata->smpl_ratio[i]);
149 
150 		if (drvdata->smpl_ratio[i] > 0)
151 			ret = 0;
152 	}
153 
154 	return ret;
155 }
156 
157 static int cc_trng_change_rosc(struct cctrng_drvdata *drvdata)
158 {
159 	struct device *dev = &(drvdata->pdev->dev);
160 
161 	dev_dbg(dev, "cctrng change rosc (was %d)\n", drvdata->active_rosc);
162 	drvdata->active_rosc += 1;
163 
164 	while (drvdata->active_rosc < CC_TRNG_NUM_OF_ROSCS) {
165 		if (drvdata->smpl_ratio[drvdata->active_rosc] > 0)
166 			return 0;
167 
168 		drvdata->active_rosc += 1;
169 	}
170 	return -EINVAL;
171 }
172 
173 
174 static void cc_trng_enable_rnd_source(struct cctrng_drvdata *drvdata)
175 {
176 	u32 max_cycles;
177 
178 	/* Set watchdog threshold to maximal allowed time (in CPU cycles) */
179 	max_cycles = CCTRNG_TIMEOUT(drvdata->smpl_ratio[drvdata->active_rosc]);
180 	cc_iowrite(drvdata, CC_RNG_WATCHDOG_VAL_REG_OFFSET, max_cycles);
181 
182 	/* enable the RND source */
183 	cc_iowrite(drvdata, CC_RND_SOURCE_ENABLE_REG_OFFSET, 0x1);
184 
185 	/* unmask RNG interrupts */
186 	cc_iowrite(drvdata, CC_RNG_IMR_REG_OFFSET, (u32)~CC_RNG_INT_MASK);
187 }
188 
189 
190 /* increase circular data buffer index (head/tail) */
191 static inline void circ_idx_inc(int *idx, int bytes)
192 {
193 	*idx += (bytes + 3) >> 2;
194 	*idx &= (CCTRNG_DATA_BUF_WORDS - 1);
195 }
196 
197 static inline size_t circ_buf_space(struct cctrng_drvdata *drvdata)
198 {
199 	return CIRC_SPACE(drvdata->circ.head,
200 			  drvdata->circ.tail, CCTRNG_DATA_BUF_WORDS);
201 
202 }
203 
204 static int cctrng_read(struct hwrng *rng, void *data, size_t max, bool wait)
205 {
206 	/* current implementation ignores "wait" */
207 
208 	struct cctrng_drvdata *drvdata = (struct cctrng_drvdata *)rng->priv;
209 	struct device *dev = &(drvdata->pdev->dev);
210 	u32 *buf = (u32 *)drvdata->circ.buf;
211 	size_t copied;
212 	size_t cnt_w;
213 	size_t size;
214 	size_t left;
215 
216 	if (!spin_trylock(&drvdata->read_lock)) {
217 		/* concurrent consumers from data_buf cannot be served */
218 		dev_dbg_ratelimited(dev, "unable to hold lock\n");
219 		return 0;
220 	}
221 
222 	/* copy till end of data buffer (without wrap back) */
223 	cnt_w = CIRC_CNT_TO_END(drvdata->circ.head,
224 				drvdata->circ.tail, CCTRNG_DATA_BUF_WORDS);
225 	size = min((cnt_w<<2), max);
226 	memcpy(data, &(buf[drvdata->circ.tail]), size);
227 	copied = size;
228 	circ_idx_inc(&drvdata->circ.tail, size);
229 	/* copy rest of data in data buffer */
230 	left = max - copied;
231 	if (left > 0) {
232 		cnt_w = CIRC_CNT(drvdata->circ.head,
233 				 drvdata->circ.tail, CCTRNG_DATA_BUF_WORDS);
234 		size = min((cnt_w<<2), left);
235 		memcpy(data, &(buf[drvdata->circ.tail]), size);
236 		copied += size;
237 		circ_idx_inc(&drvdata->circ.tail, size);
238 	}
239 
240 	spin_unlock(&drvdata->read_lock);
241 
242 	if (circ_buf_space(drvdata) >= CC_TRNG_EHR_IN_WORDS) {
243 		if (atomic_cmpxchg(&drvdata->pending_hw, 0, 1) == 0) {
244 			/* re-check space in buffer to avoid potential race */
245 			if (circ_buf_space(drvdata) >= CC_TRNG_EHR_IN_WORDS) {
246 				/* increment device's usage counter */
247 				int rc = cc_trng_pm_get(dev);
248 
249 				if (rc) {
250 					dev_err(dev,
251 						"cc_trng_pm_get returned %x\n",
252 						rc);
253 					return rc;
254 				}
255 
256 				/* schedule execution of deferred work handler
257 				 * for filling of data buffer
258 				 */
259 				schedule_work(&drvdata->startwork);
260 			} else {
261 				atomic_set(&drvdata->pending_hw, 0);
262 			}
263 		}
264 	}
265 
266 	return copied;
267 }
268 
269 static void cc_trng_hw_trigger(struct cctrng_drvdata *drvdata)
270 {
271 	u32 tmp_smpl_cnt;
272 	struct device *dev = &(drvdata->pdev->dev);
273 
274 	dev_dbg(dev, "cctrng hw trigger.\n");
275 
276 	/* enable the HW RND clock */
277 	cc_iowrite(drvdata, CC_RNG_CLK_ENABLE_REG_OFFSET, 0x1);
278 
279 	/* do software reset */
280 	cc_iowrite(drvdata, CC_RNG_SW_RESET_REG_OFFSET, 0x1);
281 	/* in order to verify that the reset has completed,
282 	 * the sample count need to be verified
283 	 */
284 	do {
285 		/* enable the HW RND clock   */
286 		cc_iowrite(drvdata, CC_RNG_CLK_ENABLE_REG_OFFSET, 0x1);
287 
288 		/* set sampling ratio (rng_clocks) between consecutive bits */
289 		cc_iowrite(drvdata, CC_SAMPLE_CNT1_REG_OFFSET,
290 			   drvdata->smpl_ratio[drvdata->active_rosc]);
291 
292 		/* read the sampling ratio  */
293 		tmp_smpl_cnt = cc_ioread(drvdata, CC_SAMPLE_CNT1_REG_OFFSET);
294 
295 	} while (tmp_smpl_cnt != drvdata->smpl_ratio[drvdata->active_rosc]);
296 
297 	/* disable the RND source for setting new parameters in HW */
298 	cc_iowrite(drvdata, CC_RND_SOURCE_ENABLE_REG_OFFSET, 0);
299 
300 	cc_iowrite(drvdata, CC_RNG_ICR_REG_OFFSET, 0xFFFFFFFF);
301 
302 	cc_iowrite(drvdata, CC_TRNG_CONFIG_REG_OFFSET, drvdata->active_rosc);
303 
304 	/* Debug Control register: set to 0 - no bypasses */
305 	cc_iowrite(drvdata, CC_TRNG_DEBUG_CONTROL_REG_OFFSET, 0);
306 
307 	cc_trng_enable_rnd_source(drvdata);
308 }
309 
310 static void cc_trng_compwork_handler(struct work_struct *w)
311 {
312 	u32 isr;
313 	u32 ehr_valid;
314 	struct cctrng_drvdata *drvdata =
315 			container_of(w, struct cctrng_drvdata, compwork);
316 	struct device *dev = &(drvdata->pdev->dev);
317 	int i;
318 
319 	/* stop DMA and the RNG source */
320 	cc_iowrite(drvdata, CC_RNG_DMA_ENABLE_REG_OFFSET, 0);
321 	cc_iowrite(drvdata, CC_RND_SOURCE_ENABLE_REG_OFFSET, 0);
322 
323 	/* read RNG_ISR and check for errors */
324 	isr = cc_ioread(drvdata, CC_RNG_ISR_REG_OFFSET);
325 	ehr_valid = CC_REG_FLD_GET(RNG_ISR, EHR_VALID, isr);
326 	dev_dbg(dev, "Got RNG_ISR=0x%08X (EHR_VALID=%u)\n", isr, ehr_valid);
327 
328 	if (fips_enabled && CC_REG_FLD_GET(RNG_ISR, CRNGT_ERR, isr)) {
329 		fips_fail_notify();
330 		/* FIPS error is fatal */
331 		panic("Got HW CRNGT error while fips is enabled!\n");
332 	}
333 
334 	/* Clear all pending RNG interrupts */
335 	cc_iowrite(drvdata, CC_RNG_ICR_REG_OFFSET, isr);
336 
337 
338 	if (!ehr_valid) {
339 		/* in case of AUTOCORR/TIMEOUT error, try the next ROSC */
340 		if (CC_REG_FLD_GET(RNG_ISR, AUTOCORR_ERR, isr) ||
341 				CC_REG_FLD_GET(RNG_ISR, WATCHDOG, isr)) {
342 			dev_dbg(dev, "cctrng autocorr/timeout error.\n");
343 			goto next_rosc;
344 		}
345 
346 		/* in case of VN error, ignore it */
347 	}
348 
349 	/* read EHR data from registers */
350 	for (i = 0; i < CC_TRNG_EHR_IN_WORDS; i++) {
351 		/* calc word ptr in data_buf */
352 		u32 *buf = (u32 *)drvdata->circ.buf;
353 
354 		buf[drvdata->circ.head] = cc_ioread(drvdata,
355 				CC_EHR_DATA_0_REG_OFFSET + (i*sizeof(u32)));
356 
357 		/* EHR_DATA registers are cleared on read. In case 0 value was
358 		 * returned, restart the entropy collection.
359 		 */
360 		if (buf[drvdata->circ.head] == 0) {
361 			dev_dbg(dev, "Got 0 value in EHR. active_rosc %u\n",
362 				drvdata->active_rosc);
363 			goto next_rosc;
364 		}
365 
366 		circ_idx_inc(&drvdata->circ.head, 1<<2);
367 	}
368 
369 	atomic_set(&drvdata->pending_hw, 0);
370 
371 	/* continue to fill data buffer if needed */
372 	if (circ_buf_space(drvdata) >= CC_TRNG_EHR_IN_WORDS) {
373 		if (atomic_cmpxchg(&drvdata->pending_hw, 0, 1) == 0) {
374 			/* Re-enable rnd source */
375 			cc_trng_enable_rnd_source(drvdata);
376 			return;
377 		}
378 	}
379 
380 	cc_trng_pm_put_suspend(dev);
381 
382 	dev_dbg(dev, "compwork handler done\n");
383 	return;
384 
385 next_rosc:
386 	if ((circ_buf_space(drvdata) >= CC_TRNG_EHR_IN_WORDS) &&
387 			(cc_trng_change_rosc(drvdata) == 0)) {
388 		/* trigger trng hw with next rosc */
389 		cc_trng_hw_trigger(drvdata);
390 	} else {
391 		atomic_set(&drvdata->pending_hw, 0);
392 		cc_trng_pm_put_suspend(dev);
393 	}
394 }
395 
396 static irqreturn_t cc_isr(int irq, void *dev_id)
397 {
398 	struct cctrng_drvdata *drvdata = (struct cctrng_drvdata *)dev_id;
399 	struct device *dev = &(drvdata->pdev->dev);
400 	u32 irr;
401 
402 	/* if driver suspended return, probably shared interrupt */
403 	if (pm_runtime_suspended(dev))
404 		return IRQ_NONE;
405 
406 	/* read the interrupt status */
407 	irr = cc_ioread(drvdata, CC_HOST_RGF_IRR_REG_OFFSET);
408 	dev_dbg(dev, "Got IRR=0x%08X\n", irr);
409 
410 	if (irr == 0) /* Probably shared interrupt line */
411 		return IRQ_NONE;
412 
413 	/* clear interrupt - must be before processing events */
414 	cc_iowrite(drvdata, CC_HOST_RGF_ICR_REG_OFFSET, irr);
415 
416 	/* RNG interrupt - most probable */
417 	if (irr & CC_HOST_RNG_IRQ_MASK) {
418 		/* Mask RNG interrupts - will be unmasked in deferred work */
419 		cc_iowrite(drvdata, CC_RNG_IMR_REG_OFFSET, 0xFFFFFFFF);
420 
421 		/* We clear RNG interrupt here,
422 		 * to avoid it from firing as we'll unmask RNG interrupts.
423 		 */
424 		cc_iowrite(drvdata, CC_HOST_RGF_ICR_REG_OFFSET,
425 			   CC_HOST_RNG_IRQ_MASK);
426 
427 		irr &= ~CC_HOST_RNG_IRQ_MASK;
428 
429 		/* schedule execution of deferred work handler */
430 		schedule_work(&drvdata->compwork);
431 	}
432 
433 	if (irr) {
434 		dev_dbg_ratelimited(dev,
435 				"IRR includes unknown cause bits (0x%08X)\n",
436 				irr);
437 		/* Just warning */
438 	}
439 
440 	return IRQ_HANDLED;
441 }
442 
443 static void cc_trng_startwork_handler(struct work_struct *w)
444 {
445 	struct cctrng_drvdata *drvdata =
446 			container_of(w, struct cctrng_drvdata, startwork);
447 
448 	drvdata->active_rosc = 0;
449 	cc_trng_hw_trigger(drvdata);
450 }
451 
452 static int cctrng_probe(struct platform_device *pdev)
453 {
454 	struct cctrng_drvdata *drvdata;
455 	struct device *dev = &pdev->dev;
456 	int rc;
457 	u32 val;
458 	int irq;
459 
460 	/* Compile time assertion checks */
461 	BUILD_BUG_ON(CCTRNG_DATA_BUF_WORDS < 6);
462 	BUILD_BUG_ON((CCTRNG_DATA_BUF_WORDS & (CCTRNG_DATA_BUF_WORDS-1)) != 0);
463 
464 	drvdata = devm_kzalloc(dev, sizeof(*drvdata), GFP_KERNEL);
465 	if (!drvdata)
466 		return -ENOMEM;
467 
468 	drvdata->rng.name = devm_kstrdup(dev, dev_name(dev), GFP_KERNEL);
469 	if (!drvdata->rng.name)
470 		return -ENOMEM;
471 
472 	drvdata->rng.read = cctrng_read;
473 	drvdata->rng.priv = (unsigned long)drvdata;
474 	drvdata->rng.quality = CC_TRNG_QUALITY;
475 
476 	platform_set_drvdata(pdev, drvdata);
477 	drvdata->pdev = pdev;
478 
479 	drvdata->circ.buf = (char *)drvdata->data_buf;
480 
481 	drvdata->cc_base = devm_platform_ioremap_resource(pdev, 0);
482 	if (IS_ERR(drvdata->cc_base))
483 		return dev_err_probe(dev, PTR_ERR(drvdata->cc_base), "Failed to ioremap registers");
484 
485 	/* Then IRQ */
486 	irq = platform_get_irq(pdev, 0);
487 	if (irq < 0)
488 		return irq;
489 
490 	/* parse sampling rate from device tree */
491 	rc = cc_trng_parse_sampling_ratio(drvdata);
492 	if (rc)
493 		return dev_err_probe(dev, rc, "Failed to get legal sampling ratio for rosc\n");
494 
495 	drvdata->clk = devm_clk_get_optional_enabled(dev, NULL);
496 	if (IS_ERR(drvdata->clk))
497 		return dev_err_probe(dev, PTR_ERR(drvdata->clk),
498 				     "Failed to get or enable the clock\n");
499 
500 	INIT_WORK(&drvdata->compwork, cc_trng_compwork_handler);
501 	INIT_WORK(&drvdata->startwork, cc_trng_startwork_handler);
502 	spin_lock_init(&drvdata->read_lock);
503 
504 	/* register the driver isr function */
505 	rc = devm_request_irq(dev, irq, cc_isr, IRQF_SHARED, "cctrng", drvdata);
506 	if (rc)
507 		return rc;
508 	dev_dbg(dev, "Registered to IRQ: %d\n", irq);
509 
510 	/* Clear all pending interrupts */
511 	val = cc_ioread(drvdata, CC_HOST_RGF_IRR_REG_OFFSET);
512 	dev_dbg(dev, "IRR=0x%08X\n", val);
513 	cc_iowrite(drvdata, CC_HOST_RGF_ICR_REG_OFFSET, val);
514 
515 	/* unmask HOST RNG interrupt */
516 	cc_iowrite(drvdata, CC_HOST_RGF_IMR_REG_OFFSET,
517 		   cc_ioread(drvdata, CC_HOST_RGF_IMR_REG_OFFSET) &
518 		   ~CC_HOST_RNG_IRQ_MASK);
519 
520 	/* init PM */
521 	rc = cc_trng_pm_init(drvdata);
522 	if (rc)
523 		return dev_err_probe(dev, rc, "cc_trng_pm_init failed\n");
524 
525 	/* increment device's usage counter */
526 	rc = cc_trng_pm_get(dev);
527 	if (rc)
528 		return dev_err_probe(dev, rc, "cc_trng_pm_get returned %x\n", rc);
529 
530 	/* set pending_hw to verify that HW won't be triggered from read */
531 	atomic_set(&drvdata->pending_hw, 1);
532 
533 	/* registration of the hwrng device */
534 	rc = devm_hwrng_register(dev, &drvdata->rng);
535 	if (rc) {
536 		dev_err(dev, "Could not register hwrng device.\n");
537 		cc_trng_pm_fini(drvdata);
538 		return rc;
539 	}
540 
541 	/* trigger HW to start generate data */
542 	drvdata->active_rosc = 0;
543 	cc_trng_hw_trigger(drvdata);
544 
545 	/* All set, we can allow auto-suspend */
546 	cc_trng_pm_go(drvdata);
547 
548 	dev_info(dev, "ARM cctrng device initialized\n");
549 
550 	return 0;
551 }
552 
553 static void cctrng_remove(struct platform_device *pdev)
554 {
555 	struct cctrng_drvdata *drvdata = platform_get_drvdata(pdev);
556 	struct device *dev = &pdev->dev;
557 
558 	dev_dbg(dev, "Releasing cctrng resources...\n");
559 
560 	cc_trng_pm_fini(drvdata);
561 
562 	dev_info(dev, "ARM cctrng device terminated\n");
563 }
564 
565 static int __maybe_unused cctrng_suspend(struct device *dev)
566 {
567 	struct cctrng_drvdata *drvdata = dev_get_drvdata(dev);
568 
569 	dev_dbg(dev, "set HOST_POWER_DOWN_EN\n");
570 	cc_iowrite(drvdata, CC_HOST_POWER_DOWN_EN_REG_OFFSET,
571 			POWER_DOWN_ENABLE);
572 
573 	clk_disable_unprepare(drvdata->clk);
574 
575 	return 0;
576 }
577 
578 static bool cctrng_wait_for_reset_completion(struct cctrng_drvdata *drvdata)
579 {
580 	unsigned int val;
581 	unsigned int i;
582 
583 	for (i = 0; i < CC_HW_RESET_LOOP_COUNT; i++) {
584 		/* in cc7x3 NVM_IS_IDLE indicates that CC reset is
585 		 *  completed and device is fully functional
586 		 */
587 		val = cc_ioread(drvdata, CC_NVM_IS_IDLE_REG_OFFSET);
588 		if (val & BIT(CC_NVM_IS_IDLE_VALUE_BIT_SHIFT)) {
589 			/* hw indicate reset completed */
590 			return true;
591 		}
592 		/* allow scheduling other process on the processor */
593 		schedule();
594 	}
595 	/* reset not completed */
596 	return false;
597 }
598 
599 static int __maybe_unused cctrng_resume(struct device *dev)
600 {
601 	struct cctrng_drvdata *drvdata = dev_get_drvdata(dev);
602 	int rc;
603 
604 	dev_dbg(dev, "unset HOST_POWER_DOWN_EN\n");
605 	/* Enables the device source clk */
606 	rc = clk_prepare_enable(drvdata->clk);
607 	if (rc) {
608 		dev_err(dev, "failed getting clock back on. We're toast.\n");
609 		return rc;
610 	}
611 
612 	/* wait for Cryptocell reset completion */
613 	if (!cctrng_wait_for_reset_completion(drvdata)) {
614 		dev_err(dev, "Cryptocell reset not completed");
615 		clk_disable_unprepare(drvdata->clk);
616 		return -EBUSY;
617 	}
618 
619 	/* unmask HOST RNG interrupt */
620 	cc_iowrite(drvdata, CC_HOST_RGF_IMR_REG_OFFSET,
621 		   cc_ioread(drvdata, CC_HOST_RGF_IMR_REG_OFFSET) &
622 		   ~CC_HOST_RNG_IRQ_MASK);
623 
624 	cc_iowrite(drvdata, CC_HOST_POWER_DOWN_EN_REG_OFFSET,
625 		   POWER_DOWN_DISABLE);
626 
627 	return 0;
628 }
629 
630 static UNIVERSAL_DEV_PM_OPS(cctrng_pm, cctrng_suspend, cctrng_resume, NULL);
631 
632 static const struct of_device_id arm_cctrng_dt_match[] = {
633 	{ .compatible = "arm,cryptocell-713-trng", },
634 	{ .compatible = "arm,cryptocell-703-trng", },
635 	{},
636 };
637 MODULE_DEVICE_TABLE(of, arm_cctrng_dt_match);
638 
639 static struct platform_driver cctrng_driver = {
640 	.driver = {
641 		.name = "cctrng",
642 		.of_match_table = arm_cctrng_dt_match,
643 		.pm = &cctrng_pm,
644 	},
645 	.probe = cctrng_probe,
646 	.remove = cctrng_remove,
647 };
648 
649 module_platform_driver(cctrng_driver);
650 
651 /* Module description */
652 MODULE_DESCRIPTION("ARM CryptoCell TRNG Driver");
653 MODULE_AUTHOR("ARM");
654 MODULE_LICENSE("GPL v2");
655