xgene-rng.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * APM X-Gene SoC RNG Driver
  4. *
  5. * Copyright (c) 2014, Applied Micro Circuits Corporation
  6. * Author: Rameshwar Prasad Sahu <[email protected]>
  7. * Shamal Winchurkar <[email protected]>
  8. * Feng Kan <[email protected]>
  9. */
  10. #include <linux/acpi.h>
  11. #include <linux/clk.h>
  12. #include <linux/delay.h>
  13. #include <linux/hw_random.h>
  14. #include <linux/init.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/module.h>
  17. #include <linux/of_platform.h>
  18. #include <linux/of_irq.h>
  19. #include <linux/of_address.h>
  20. #include <linux/timer.h>
  21. #define RNG_MAX_DATUM 4
  22. #define MAX_TRY 100
  23. #define XGENE_RNG_RETRY_COUNT 20
  24. #define XGENE_RNG_RETRY_INTERVAL 10
  25. /* RNG Registers */
  26. #define RNG_INOUT_0 0x00
  27. #define RNG_INTR_STS_ACK 0x10
  28. #define RNG_CONTROL 0x14
  29. #define RNG_CONFIG 0x18
  30. #define RNG_ALARMCNT 0x1c
  31. #define RNG_FROENABLE 0x20
  32. #define RNG_FRODETUNE 0x24
  33. #define RNG_ALARMMASK 0x28
  34. #define RNG_ALARMSTOP 0x2c
  35. #define RNG_OPTIONS 0x78
  36. #define RNG_EIP_REV 0x7c
  37. #define MONOBIT_FAIL_MASK BIT(7)
  38. #define POKER_FAIL_MASK BIT(6)
  39. #define LONG_RUN_FAIL_MASK BIT(5)
  40. #define RUN_FAIL_MASK BIT(4)
  41. #define NOISE_FAIL_MASK BIT(3)
  42. #define STUCK_OUT_MASK BIT(2)
  43. #define SHUTDOWN_OFLO_MASK BIT(1)
  44. #define READY_MASK BIT(0)
  45. #define MAJOR_HW_REV_RD(src) (((src) & 0x0f000000) >> 24)
  46. #define MINOR_HW_REV_RD(src) (((src) & 0x00f00000) >> 20)
  47. #define HW_PATCH_LEVEL_RD(src) (((src) & 0x000f0000) >> 16)
  48. #define MAX_REFILL_CYCLES_SET(dst, src) \
  49. ((dst & ~0xffff0000) | (((u32)src << 16) & 0xffff0000))
  50. #define MIN_REFILL_CYCLES_SET(dst, src) \
  51. ((dst & ~0x000000ff) | (((u32)src) & 0x000000ff))
  52. #define ALARM_THRESHOLD_SET(dst, src) \
  53. ((dst & ~0x000000ff) | (((u32)src) & 0x000000ff))
  54. #define ENABLE_RNG_SET(dst, src) \
  55. ((dst & ~BIT(10)) | (((u32)src << 10) & BIT(10)))
  56. #define REGSPEC_TEST_MODE_SET(dst, src) \
  57. ((dst & ~BIT(8)) | (((u32)src << 8) & BIT(8)))
  58. #define MONOBIT_FAIL_MASK_SET(dst, src) \
  59. ((dst & ~BIT(7)) | (((u32)src << 7) & BIT(7)))
  60. #define POKER_FAIL_MASK_SET(dst, src) \
  61. ((dst & ~BIT(6)) | (((u32)src << 6) & BIT(6)))
  62. #define LONG_RUN_FAIL_MASK_SET(dst, src) \
  63. ((dst & ~BIT(5)) | (((u32)src << 5) & BIT(5)))
  64. #define RUN_FAIL_MASK_SET(dst, src) \
  65. ((dst & ~BIT(4)) | (((u32)src << 4) & BIT(4)))
  66. #define NOISE_FAIL_MASK_SET(dst, src) \
  67. ((dst & ~BIT(3)) | (((u32)src << 3) & BIT(3)))
  68. #define STUCK_OUT_MASK_SET(dst, src) \
  69. ((dst & ~BIT(2)) | (((u32)src << 2) & BIT(2)))
  70. #define SHUTDOWN_OFLO_MASK_SET(dst, src) \
  71. ((dst & ~BIT(1)) | (((u32)src << 1) & BIT(1)))
  72. struct xgene_rng_dev {
  73. u32 irq;
  74. void __iomem *csr_base;
  75. u32 revision;
  76. u32 datum_size;
  77. u32 failure_cnt; /* Failure count last minute */
  78. unsigned long failure_ts;/* First failure timestamp */
  79. struct timer_list failure_timer;
  80. struct device *dev;
  81. struct clk *clk;
  82. };
  83. static void xgene_rng_expired_timer(struct timer_list *t)
  84. {
  85. struct xgene_rng_dev *ctx = from_timer(ctx, t, failure_timer);
  86. /* Clear failure counter as timer expired */
  87. disable_irq(ctx->irq);
  88. ctx->failure_cnt = 0;
  89. del_timer(&ctx->failure_timer);
  90. enable_irq(ctx->irq);
  91. }
  92. static void xgene_rng_start_timer(struct xgene_rng_dev *ctx)
  93. {
  94. ctx->failure_timer.expires = jiffies + 120 * HZ;
  95. add_timer(&ctx->failure_timer);
  96. }
  97. /*
  98. * Initialize or reinit free running oscillators (FROs)
  99. */
  100. static void xgene_rng_init_fro(struct xgene_rng_dev *ctx, u32 fro_val)
  101. {
  102. writel(fro_val, ctx->csr_base + RNG_FRODETUNE);
  103. writel(0x00000000, ctx->csr_base + RNG_ALARMMASK);
  104. writel(0x00000000, ctx->csr_base + RNG_ALARMSTOP);
  105. writel(0xFFFFFFFF, ctx->csr_base + RNG_FROENABLE);
  106. }
  107. static void xgene_rng_chk_overflow(struct xgene_rng_dev *ctx)
  108. {
  109. u32 val;
  110. val = readl(ctx->csr_base + RNG_INTR_STS_ACK);
  111. if (val & MONOBIT_FAIL_MASK)
  112. /*
  113. * LFSR detected an out-of-bounds number of 1s after
  114. * checking 20,000 bits (test T1 as specified in the
  115. * AIS-31 standard)
  116. */
  117. dev_err(ctx->dev, "test monobit failure error 0x%08X\n", val);
  118. if (val & POKER_FAIL_MASK)
  119. /*
  120. * LFSR detected an out-of-bounds value in at least one
  121. * of the 16 poker_count_X counters or an out of bounds sum
  122. * of squares value after checking 20,000 bits (test T2 as
  123. * specified in the AIS-31 standard)
  124. */
  125. dev_err(ctx->dev, "test poker failure error 0x%08X\n", val);
  126. if (val & LONG_RUN_FAIL_MASK)
  127. /*
  128. * LFSR detected a sequence of 34 identical bits
  129. * (test T4 as specified in the AIS-31 standard)
  130. */
  131. dev_err(ctx->dev, "test long run failure error 0x%08X\n", val);
  132. if (val & RUN_FAIL_MASK)
  133. /*
  134. * LFSR detected an outof-bounds value for at least one
  135. * of the running counters after checking 20,000 bits
  136. * (test T3 as specified in the AIS-31 standard)
  137. */
  138. dev_err(ctx->dev, "test run failure error 0x%08X\n", val);
  139. if (val & NOISE_FAIL_MASK)
  140. /* LFSR detected a sequence of 48 identical bits */
  141. dev_err(ctx->dev, "noise failure error 0x%08X\n", val);
  142. if (val & STUCK_OUT_MASK)
  143. /*
  144. * Detected output data registers generated same value twice
  145. * in a row
  146. */
  147. dev_err(ctx->dev, "stuck out failure error 0x%08X\n", val);
  148. if (val & SHUTDOWN_OFLO_MASK) {
  149. u32 frostopped;
  150. /* FROs shut down after a second error event. Try recover. */
  151. if (++ctx->failure_cnt == 1) {
  152. /* 1st time, just recover */
  153. ctx->failure_ts = jiffies;
  154. frostopped = readl(ctx->csr_base + RNG_ALARMSTOP);
  155. xgene_rng_init_fro(ctx, frostopped);
  156. /*
  157. * We must start a timer to clear out this error
  158. * in case the system timer wrap around
  159. */
  160. xgene_rng_start_timer(ctx);
  161. } else {
  162. /* 2nd time failure in lesser than 1 minute? */
  163. if (time_after(ctx->failure_ts + 60 * HZ, jiffies)) {
  164. dev_err(ctx->dev,
  165. "FRO shutdown failure error 0x%08X\n",
  166. val);
  167. } else {
  168. /* 2nd time failure after 1 minutes, recover */
  169. ctx->failure_ts = jiffies;
  170. ctx->failure_cnt = 1;
  171. /*
  172. * We must start a timer to clear out this
  173. * error in case the system timer wrap
  174. * around
  175. */
  176. xgene_rng_start_timer(ctx);
  177. }
  178. frostopped = readl(ctx->csr_base + RNG_ALARMSTOP);
  179. xgene_rng_init_fro(ctx, frostopped);
  180. }
  181. }
  182. /* Clear them all */
  183. writel(val, ctx->csr_base + RNG_INTR_STS_ACK);
  184. }
  185. static irqreturn_t xgene_rng_irq_handler(int irq, void *id)
  186. {
  187. struct xgene_rng_dev *ctx = (struct xgene_rng_dev *) id;
  188. /* RNG Alarm Counter overflow */
  189. xgene_rng_chk_overflow(ctx);
  190. return IRQ_HANDLED;
  191. }
  192. static int xgene_rng_data_present(struct hwrng *rng, int wait)
  193. {
  194. struct xgene_rng_dev *ctx = (struct xgene_rng_dev *) rng->priv;
  195. u32 i, val = 0;
  196. for (i = 0; i < XGENE_RNG_RETRY_COUNT; i++) {
  197. val = readl(ctx->csr_base + RNG_INTR_STS_ACK);
  198. if ((val & READY_MASK) || !wait)
  199. break;
  200. udelay(XGENE_RNG_RETRY_INTERVAL);
  201. }
  202. return (val & READY_MASK);
  203. }
  204. static int xgene_rng_data_read(struct hwrng *rng, u32 *data)
  205. {
  206. struct xgene_rng_dev *ctx = (struct xgene_rng_dev *) rng->priv;
  207. int i;
  208. for (i = 0; i < ctx->datum_size; i++)
  209. data[i] = readl(ctx->csr_base + RNG_INOUT_0 + i * 4);
  210. /* Clear ready bit to start next transaction */
  211. writel(READY_MASK, ctx->csr_base + RNG_INTR_STS_ACK);
  212. return ctx->datum_size << 2;
  213. }
  214. static void xgene_rng_init_internal(struct xgene_rng_dev *ctx)
  215. {
  216. u32 val;
  217. writel(0x00000000, ctx->csr_base + RNG_CONTROL);
  218. val = MAX_REFILL_CYCLES_SET(0, 10);
  219. val = MIN_REFILL_CYCLES_SET(val, 10);
  220. writel(val, ctx->csr_base + RNG_CONFIG);
  221. val = ALARM_THRESHOLD_SET(0, 0xFF);
  222. writel(val, ctx->csr_base + RNG_ALARMCNT);
  223. xgene_rng_init_fro(ctx, 0);
  224. writel(MONOBIT_FAIL_MASK |
  225. POKER_FAIL_MASK |
  226. LONG_RUN_FAIL_MASK |
  227. RUN_FAIL_MASK |
  228. NOISE_FAIL_MASK |
  229. STUCK_OUT_MASK |
  230. SHUTDOWN_OFLO_MASK |
  231. READY_MASK, ctx->csr_base + RNG_INTR_STS_ACK);
  232. val = ENABLE_RNG_SET(0, 1);
  233. val = MONOBIT_FAIL_MASK_SET(val, 1);
  234. val = POKER_FAIL_MASK_SET(val, 1);
  235. val = LONG_RUN_FAIL_MASK_SET(val, 1);
  236. val = RUN_FAIL_MASK_SET(val, 1);
  237. val = NOISE_FAIL_MASK_SET(val, 1);
  238. val = STUCK_OUT_MASK_SET(val, 1);
  239. val = SHUTDOWN_OFLO_MASK_SET(val, 1);
  240. writel(val, ctx->csr_base + RNG_CONTROL);
  241. }
  242. static int xgene_rng_init(struct hwrng *rng)
  243. {
  244. struct xgene_rng_dev *ctx = (struct xgene_rng_dev *) rng->priv;
  245. ctx->failure_cnt = 0;
  246. timer_setup(&ctx->failure_timer, xgene_rng_expired_timer, 0);
  247. ctx->revision = readl(ctx->csr_base + RNG_EIP_REV);
  248. dev_dbg(ctx->dev, "Rev %d.%d.%d\n",
  249. MAJOR_HW_REV_RD(ctx->revision),
  250. MINOR_HW_REV_RD(ctx->revision),
  251. HW_PATCH_LEVEL_RD(ctx->revision));
  252. dev_dbg(ctx->dev, "Options 0x%08X",
  253. readl(ctx->csr_base + RNG_OPTIONS));
  254. xgene_rng_init_internal(ctx);
  255. ctx->datum_size = RNG_MAX_DATUM;
  256. return 0;
  257. }
  258. #ifdef CONFIG_ACPI
  259. static const struct acpi_device_id xgene_rng_acpi_match[] = {
  260. { "APMC0D18", },
  261. { }
  262. };
  263. MODULE_DEVICE_TABLE(acpi, xgene_rng_acpi_match);
  264. #endif
  265. static struct hwrng xgene_rng_func = {
  266. .name = "xgene-rng",
  267. .init = xgene_rng_init,
  268. .data_present = xgene_rng_data_present,
  269. .data_read = xgene_rng_data_read,
  270. };
  271. static int xgene_rng_probe(struct platform_device *pdev)
  272. {
  273. struct xgene_rng_dev *ctx;
  274. int rc = 0;
  275. ctx = devm_kzalloc(&pdev->dev, sizeof(*ctx), GFP_KERNEL);
  276. if (!ctx)
  277. return -ENOMEM;
  278. ctx->dev = &pdev->dev;
  279. platform_set_drvdata(pdev, ctx);
  280. ctx->csr_base = devm_platform_ioremap_resource(pdev, 0);
  281. if (IS_ERR(ctx->csr_base))
  282. return PTR_ERR(ctx->csr_base);
  283. rc = platform_get_irq(pdev, 0);
  284. if (rc < 0)
  285. return rc;
  286. ctx->irq = rc;
  287. dev_dbg(&pdev->dev, "APM X-Gene RNG BASE %p ALARM IRQ %d",
  288. ctx->csr_base, ctx->irq);
  289. rc = devm_request_irq(&pdev->dev, ctx->irq, xgene_rng_irq_handler, 0,
  290. dev_name(&pdev->dev), ctx);
  291. if (rc) {
  292. dev_err(&pdev->dev, "Could not request RNG alarm IRQ\n");
  293. return rc;
  294. }
  295. /* Enable IP clock */
  296. ctx->clk = devm_clk_get(&pdev->dev, NULL);
  297. if (IS_ERR(ctx->clk)) {
  298. dev_warn(&pdev->dev, "Couldn't get the clock for RNG\n");
  299. } else {
  300. rc = clk_prepare_enable(ctx->clk);
  301. if (rc) {
  302. dev_warn(&pdev->dev,
  303. "clock prepare enable failed for RNG");
  304. return rc;
  305. }
  306. }
  307. xgene_rng_func.priv = (unsigned long) ctx;
  308. rc = devm_hwrng_register(&pdev->dev, &xgene_rng_func);
  309. if (rc) {
  310. dev_err(&pdev->dev, "RNG registering failed error %d\n", rc);
  311. if (!IS_ERR(ctx->clk))
  312. clk_disable_unprepare(ctx->clk);
  313. return rc;
  314. }
  315. rc = device_init_wakeup(&pdev->dev, 1);
  316. if (rc) {
  317. dev_err(&pdev->dev, "RNG device_init_wakeup failed error %d\n",
  318. rc);
  319. if (!IS_ERR(ctx->clk))
  320. clk_disable_unprepare(ctx->clk);
  321. return rc;
  322. }
  323. return 0;
  324. }
  325. static int xgene_rng_remove(struct platform_device *pdev)
  326. {
  327. struct xgene_rng_dev *ctx = platform_get_drvdata(pdev);
  328. int rc;
  329. rc = device_init_wakeup(&pdev->dev, 0);
  330. if (rc)
  331. dev_err(&pdev->dev, "RNG init wakeup failed error %d\n", rc);
  332. if (!IS_ERR(ctx->clk))
  333. clk_disable_unprepare(ctx->clk);
  334. return rc;
  335. }
  336. static const struct of_device_id xgene_rng_of_match[] = {
  337. { .compatible = "apm,xgene-rng" },
  338. { }
  339. };
  340. MODULE_DEVICE_TABLE(of, xgene_rng_of_match);
  341. static struct platform_driver xgene_rng_driver = {
  342. .probe = xgene_rng_probe,
  343. .remove = xgene_rng_remove,
  344. .driver = {
  345. .name = "xgene-rng",
  346. .of_match_table = xgene_rng_of_match,
  347. .acpi_match_table = ACPI_PTR(xgene_rng_acpi_match),
  348. },
  349. };
  350. module_platform_driver(xgene_rng_driver);
  351. MODULE_DESCRIPTION("APM X-Gene RNG driver");
  352. MODULE_LICENSE("GPL");