xgene-rng.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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 <rsahu@apm.com>
  7. * Shamal Winchurkar <swinchurkar@apm.com>
  8. * Feng Kan <fkan@apm.com>
  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/io.h>
  17. #include <linux/module.h>
  18. #include <linux/mod_devicetable.h>
  19. #include <linux/platform_device.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. };
  82. static void xgene_rng_expired_timer(struct timer_list *t)
  83. {
  84. struct xgene_rng_dev *ctx = from_timer(ctx, t, failure_timer);
  85. /* Clear failure counter as timer expired */
  86. disable_irq(ctx->irq);
  87. ctx->failure_cnt = 0;
  88. del_timer(&ctx->failure_timer);
  89. enable_irq(ctx->irq);
  90. }
  91. static void xgene_rng_start_timer(struct xgene_rng_dev *ctx)
  92. {
  93. ctx->failure_timer.expires = jiffies + 120 * HZ;
  94. add_timer(&ctx->failure_timer);
  95. }
  96. /*
  97. * Initialize or reinit free running oscillators (FROs)
  98. */
  99. static void xgene_rng_init_fro(struct xgene_rng_dev *ctx, u32 fro_val)
  100. {
  101. writel(fro_val, ctx->csr_base + RNG_FRODETUNE);
  102. writel(0x00000000, ctx->csr_base + RNG_ALARMMASK);
  103. writel(0x00000000, ctx->csr_base + RNG_ALARMSTOP);
  104. writel(0xFFFFFFFF, ctx->csr_base + RNG_FROENABLE);
  105. }
  106. static void xgene_rng_chk_overflow(struct xgene_rng_dev *ctx)
  107. {
  108. u32 val;
  109. val = readl(ctx->csr_base + RNG_INTR_STS_ACK);
  110. if (val & MONOBIT_FAIL_MASK)
  111. /*
  112. * LFSR detected an out-of-bounds number of 1s after
  113. * checking 20,000 bits (test T1 as specified in the
  114. * AIS-31 standard)
  115. */
  116. dev_err(ctx->dev, "test monobit failure error 0x%08X\n", val);
  117. if (val & POKER_FAIL_MASK)
  118. /*
  119. * LFSR detected an out-of-bounds value in at least one
  120. * of the 16 poker_count_X counters or an out of bounds sum
  121. * of squares value after checking 20,000 bits (test T2 as
  122. * specified in the AIS-31 standard)
  123. */
  124. dev_err(ctx->dev, "test poker failure error 0x%08X\n", val);
  125. if (val & LONG_RUN_FAIL_MASK)
  126. /*
  127. * LFSR detected a sequence of 34 identical bits
  128. * (test T4 as specified in the AIS-31 standard)
  129. */
  130. dev_err(ctx->dev, "test long run failure error 0x%08X\n", val);
  131. if (val & RUN_FAIL_MASK)
  132. /*
  133. * LFSR detected an outof-bounds value for at least one
  134. * of the running counters after checking 20,000 bits
  135. * (test T3 as specified in the AIS-31 standard)
  136. */
  137. dev_err(ctx->dev, "test run failure error 0x%08X\n", val);
  138. if (val & NOISE_FAIL_MASK)
  139. /* LFSR detected a sequence of 48 identical bits */
  140. dev_err(ctx->dev, "noise failure error 0x%08X\n", val);
  141. if (val & STUCK_OUT_MASK)
  142. /*
  143. * Detected output data registers generated same value twice
  144. * in a row
  145. */
  146. dev_err(ctx->dev, "stuck out failure error 0x%08X\n", val);
  147. if (val & SHUTDOWN_OFLO_MASK) {
  148. u32 frostopped;
  149. /* FROs shut down after a second error event. Try recover. */
  150. if (++ctx->failure_cnt == 1) {
  151. /* 1st time, just recover */
  152. ctx->failure_ts = jiffies;
  153. frostopped = readl(ctx->csr_base + RNG_ALARMSTOP);
  154. xgene_rng_init_fro(ctx, frostopped);
  155. /*
  156. * We must start a timer to clear out this error
  157. * in case the system timer wrap around
  158. */
  159. xgene_rng_start_timer(ctx);
  160. } else {
  161. /* 2nd time failure in lesser than 1 minute? */
  162. if (time_after(ctx->failure_ts + 60 * HZ, jiffies)) {
  163. dev_err(ctx->dev,
  164. "FRO shutdown failure error 0x%08X\n",
  165. val);
  166. } else {
  167. /* 2nd time failure after 1 minutes, recover */
  168. ctx->failure_ts = jiffies;
  169. ctx->failure_cnt = 1;
  170. /*
  171. * We must start a timer to clear out this
  172. * error in case the system timer wrap
  173. * around
  174. */
  175. xgene_rng_start_timer(ctx);
  176. }
  177. frostopped = readl(ctx->csr_base + RNG_ALARMSTOP);
  178. xgene_rng_init_fro(ctx, frostopped);
  179. }
  180. }
  181. /* Clear them all */
  182. writel(val, ctx->csr_base + RNG_INTR_STS_ACK);
  183. }
  184. static irqreturn_t xgene_rng_irq_handler(int irq, void *id)
  185. {
  186. struct xgene_rng_dev *ctx = id;
  187. /* RNG Alarm Counter overflow */
  188. xgene_rng_chk_overflow(ctx);
  189. return IRQ_HANDLED;
  190. }
  191. static int xgene_rng_data_present(struct hwrng *rng, int wait)
  192. {
  193. struct xgene_rng_dev *ctx = (struct xgene_rng_dev *) rng->priv;
  194. u32 i, val = 0;
  195. for (i = 0; i < XGENE_RNG_RETRY_COUNT; i++) {
  196. val = readl(ctx->csr_base + RNG_INTR_STS_ACK);
  197. if ((val & READY_MASK) || !wait)
  198. break;
  199. udelay(XGENE_RNG_RETRY_INTERVAL);
  200. }
  201. return (val & READY_MASK);
  202. }
  203. static int xgene_rng_data_read(struct hwrng *rng, u32 *data)
  204. {
  205. struct xgene_rng_dev *ctx = (struct xgene_rng_dev *) rng->priv;
  206. int i;
  207. for (i = 0; i < ctx->datum_size; i++)
  208. data[i] = readl(ctx->csr_base + RNG_INOUT_0 + i * 4);
  209. /* Clear ready bit to start next transaction */
  210. writel(READY_MASK, ctx->csr_base + RNG_INTR_STS_ACK);
  211. return ctx->datum_size << 2;
  212. }
  213. static void xgene_rng_init_internal(struct xgene_rng_dev *ctx)
  214. {
  215. u32 val;
  216. writel(0x00000000, ctx->csr_base + RNG_CONTROL);
  217. val = MAX_REFILL_CYCLES_SET(0, 10);
  218. val = MIN_REFILL_CYCLES_SET(val, 10);
  219. writel(val, ctx->csr_base + RNG_CONFIG);
  220. val = ALARM_THRESHOLD_SET(0, 0xFF);
  221. writel(val, ctx->csr_base + RNG_ALARMCNT);
  222. xgene_rng_init_fro(ctx, 0);
  223. writel(MONOBIT_FAIL_MASK |
  224. POKER_FAIL_MASK |
  225. LONG_RUN_FAIL_MASK |
  226. RUN_FAIL_MASK |
  227. NOISE_FAIL_MASK |
  228. STUCK_OUT_MASK |
  229. SHUTDOWN_OFLO_MASK |
  230. READY_MASK, ctx->csr_base + RNG_INTR_STS_ACK);
  231. val = ENABLE_RNG_SET(0, 1);
  232. val = MONOBIT_FAIL_MASK_SET(val, 1);
  233. val = POKER_FAIL_MASK_SET(val, 1);
  234. val = LONG_RUN_FAIL_MASK_SET(val, 1);
  235. val = RUN_FAIL_MASK_SET(val, 1);
  236. val = NOISE_FAIL_MASK_SET(val, 1);
  237. val = STUCK_OUT_MASK_SET(val, 1);
  238. val = SHUTDOWN_OFLO_MASK_SET(val, 1);
  239. writel(val, ctx->csr_base + RNG_CONTROL);
  240. }
  241. static int xgene_rng_init(struct hwrng *rng)
  242. {
  243. struct xgene_rng_dev *ctx = (struct xgene_rng_dev *) rng->priv;
  244. ctx->failure_cnt = 0;
  245. timer_setup(&ctx->failure_timer, xgene_rng_expired_timer, 0);
  246. ctx->revision = readl(ctx->csr_base + RNG_EIP_REV);
  247. dev_dbg(ctx->dev, "Rev %d.%d.%d\n",
  248. MAJOR_HW_REV_RD(ctx->revision),
  249. MINOR_HW_REV_RD(ctx->revision),
  250. HW_PATCH_LEVEL_RD(ctx->revision));
  251. dev_dbg(ctx->dev, "Options 0x%08X",
  252. readl(ctx->csr_base + RNG_OPTIONS));
  253. xgene_rng_init_internal(ctx);
  254. ctx->datum_size = RNG_MAX_DATUM;
  255. return 0;
  256. }
  257. #ifdef CONFIG_ACPI
  258. static const struct acpi_device_id xgene_rng_acpi_match[] = {
  259. { "APMC0D18", },
  260. { }
  261. };
  262. MODULE_DEVICE_TABLE(acpi, xgene_rng_acpi_match);
  263. #endif
  264. static struct hwrng xgene_rng_func = {
  265. .name = "xgene-rng",
  266. .init = xgene_rng_init,
  267. .data_present = xgene_rng_data_present,
  268. .data_read = xgene_rng_data_read,
  269. };
  270. static int xgene_rng_probe(struct platform_device *pdev)
  271. {
  272. struct xgene_rng_dev *ctx;
  273. struct clk *clk;
  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. ctx->csr_base = devm_platform_ioremap_resource(pdev, 0);
  280. if (IS_ERR(ctx->csr_base))
  281. return PTR_ERR(ctx->csr_base);
  282. rc = platform_get_irq(pdev, 0);
  283. if (rc < 0)
  284. return rc;
  285. ctx->irq = rc;
  286. dev_dbg(&pdev->dev, "APM X-Gene RNG BASE %p ALARM IRQ %d",
  287. ctx->csr_base, ctx->irq);
  288. rc = devm_request_irq(&pdev->dev, ctx->irq, xgene_rng_irq_handler, 0,
  289. dev_name(&pdev->dev), ctx);
  290. if (rc)
  291. return dev_err_probe(&pdev->dev, rc, "Could not request RNG alarm IRQ\n");
  292. /* Enable IP clock */
  293. clk = devm_clk_get_optional_enabled(&pdev->dev, NULL);
  294. if (IS_ERR(clk))
  295. return dev_err_probe(&pdev->dev, PTR_ERR(clk), "Couldn't get the clock for RNG\n");
  296. xgene_rng_func.priv = (unsigned long) ctx;
  297. rc = devm_hwrng_register(&pdev->dev, &xgene_rng_func);
  298. if (rc)
  299. return dev_err_probe(&pdev->dev, rc, "RNG registering failed\n");
  300. rc = device_init_wakeup(&pdev->dev, 1);
  301. if (rc)
  302. return dev_err_probe(&pdev->dev, rc, "RNG device_init_wakeup failed\n");
  303. return 0;
  304. }
  305. static void xgene_rng_remove(struct platform_device *pdev)
  306. {
  307. int rc;
  308. rc = device_init_wakeup(&pdev->dev, 0);
  309. if (rc)
  310. dev_err(&pdev->dev, "RNG init wakeup failed error %d\n", rc);
  311. }
  312. static const struct of_device_id xgene_rng_of_match[] = {
  313. { .compatible = "apm,xgene-rng" },
  314. { }
  315. };
  316. MODULE_DEVICE_TABLE(of, xgene_rng_of_match);
  317. static struct platform_driver xgene_rng_driver = {
  318. .probe = xgene_rng_probe,
  319. .remove_new = xgene_rng_remove,
  320. .driver = {
  321. .name = "xgene-rng",
  322. .of_match_table = xgene_rng_of_match,
  323. .acpi_match_table = ACPI_PTR(xgene_rng_acpi_match),
  324. },
  325. };
  326. module_platform_driver(xgene_rng_driver);
  327. MODULE_DESCRIPTION("APM X-Gene RNG driver");
  328. MODULE_LICENSE("GPL");