xgene-rng.c 12 KB

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