xgene-rng.c 12 KB

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