cctrng.c 18 KB

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