xgene-rng.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  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(unsigned long arg)
  97. {
  98. struct xgene_rng_dev *ctx = (struct xgene_rng_dev *) arg;
  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.data = (unsigned long) ctx;
  108. ctx->failure_timer.function = xgene_rng_expired_timer;
  109. ctx->failure_timer.expires = jiffies + 120 * HZ;
  110. add_timer(&ctx->failure_timer);
  111. }
  112. /*
  113. * Initialize or reinit free running oscillators (FROs)
  114. */
  115. static void xgene_rng_init_fro(struct xgene_rng_dev *ctx, u32 fro_val)
  116. {
  117. writel(fro_val, ctx->csr_base + RNG_FRODETUNE);
  118. writel(0x00000000, ctx->csr_base + RNG_ALARMMASK);
  119. writel(0x00000000, ctx->csr_base + RNG_ALARMSTOP);
  120. writel(0xFFFFFFFF, ctx->csr_base + RNG_FROENABLE);
  121. }
  122. static void xgene_rng_chk_overflow(struct xgene_rng_dev *ctx)
  123. {
  124. u32 val;
  125. val = readl(ctx->csr_base + RNG_INTR_STS_ACK);
  126. if (val & MONOBIT_FAIL_MASK)
  127. /*
  128. * LFSR detected an out-of-bounds number of 1s after
  129. * checking 20,000 bits (test T1 as specified in the
  130. * AIS-31 standard)
  131. */
  132. dev_err(ctx->dev, "test monobit failure error 0x%08X\n", val);
  133. if (val & POKER_FAIL_MASK)
  134. /*
  135. * LFSR detected an out-of-bounds value in at least one
  136. * of the 16 poker_count_X counters or an out of bounds sum
  137. * of squares value after checking 20,000 bits (test T2 as
  138. * specified in the AIS-31 standard)
  139. */
  140. dev_err(ctx->dev, "test poker failure error 0x%08X\n", val);
  141. if (val & LONG_RUN_FAIL_MASK)
  142. /*
  143. * LFSR detected a sequence of 34 identical bits
  144. * (test T4 as specified in the AIS-31 standard)
  145. */
  146. dev_err(ctx->dev, "test long run failure error 0x%08X\n", val);
  147. if (val & RUN_FAIL_MASK)
  148. /*
  149. * LFSR detected an outof-bounds value for at least one
  150. * of the running counters after checking 20,000 bits
  151. * (test T3 as specified in the AIS-31 standard)
  152. */
  153. dev_err(ctx->dev, "test run failure error 0x%08X\n", val);
  154. if (val & NOISE_FAIL_MASK)
  155. /* LFSR detected a sequence of 48 identical bits */
  156. dev_err(ctx->dev, "noise failure error 0x%08X\n", val);
  157. if (val & STUCK_OUT_MASK)
  158. /*
  159. * Detected output data registers generated same value twice
  160. * in a row
  161. */
  162. dev_err(ctx->dev, "stuck out failure error 0x%08X\n", val);
  163. if (val & SHUTDOWN_OFLO_MASK) {
  164. u32 frostopped;
  165. /* FROs shut down after a second error event. Try recover. */
  166. if (++ctx->failure_cnt == 1) {
  167. /* 1st time, just recover */
  168. ctx->failure_ts = jiffies;
  169. frostopped = readl(ctx->csr_base + RNG_ALARMSTOP);
  170. xgene_rng_init_fro(ctx, frostopped);
  171. /*
  172. * We must start a timer to clear out this error
  173. * in case the system timer wrap around
  174. */
  175. xgene_rng_start_timer(ctx);
  176. } else {
  177. /* 2nd time failure in lesser than 1 minute? */
  178. if (time_after(ctx->failure_ts + 60 * HZ, jiffies)) {
  179. dev_err(ctx->dev,
  180. "FRO shutdown failure error 0x%08X\n",
  181. val);
  182. } else {
  183. /* 2nd time failure after 1 minutes, recover */
  184. ctx->failure_ts = jiffies;
  185. ctx->failure_cnt = 1;
  186. /*
  187. * We must start a timer to clear out this
  188. * error in case the system timer wrap
  189. * around
  190. */
  191. xgene_rng_start_timer(ctx);
  192. }
  193. frostopped = readl(ctx->csr_base + RNG_ALARMSTOP);
  194. xgene_rng_init_fro(ctx, frostopped);
  195. }
  196. }
  197. /* Clear them all */
  198. writel(val, ctx->csr_base + RNG_INTR_STS_ACK);
  199. }
  200. static irqreturn_t xgene_rng_irq_handler(int irq, void *id)
  201. {
  202. struct xgene_rng_dev *ctx = (struct xgene_rng_dev *) id;
  203. /* RNG Alarm Counter overflow */
  204. xgene_rng_chk_overflow(ctx);
  205. return IRQ_HANDLED;
  206. }
  207. static int xgene_rng_data_present(struct hwrng *rng, int wait)
  208. {
  209. struct xgene_rng_dev *ctx = (struct xgene_rng_dev *) rng->priv;
  210. u32 i, val = 0;
  211. for (i = 0; i < XGENE_RNG_RETRY_COUNT; i++) {
  212. val = readl(ctx->csr_base + RNG_INTR_STS_ACK);
  213. if ((val & READY_MASK) || !wait)
  214. break;
  215. udelay(XGENE_RNG_RETRY_INTERVAL);
  216. }
  217. return (val & READY_MASK);
  218. }
  219. static int xgene_rng_data_read(struct hwrng *rng, u32 *data)
  220. {
  221. struct xgene_rng_dev *ctx = (struct xgene_rng_dev *) rng->priv;
  222. int i;
  223. for (i = 0; i < ctx->datum_size; i++)
  224. data[i] = readl(ctx->csr_base + RNG_INOUT_0 + i * 4);
  225. /* Clear ready bit to start next transaction */
  226. writel(READY_MASK, ctx->csr_base + RNG_INTR_STS_ACK);
  227. return ctx->datum_size << 2;
  228. }
  229. static void xgene_rng_init_internal(struct xgene_rng_dev *ctx)
  230. {
  231. u32 val;
  232. writel(0x00000000, ctx->csr_base + RNG_CONTROL);
  233. val = MAX_REFILL_CYCLES_SET(0, 10);
  234. val = MIN_REFILL_CYCLES_SET(val, 10);
  235. writel(val, ctx->csr_base + RNG_CONFIG);
  236. val = ALARM_THRESHOLD_SET(0, 0xFF);
  237. writel(val, ctx->csr_base + RNG_ALARMCNT);
  238. xgene_rng_init_fro(ctx, 0);
  239. writel(MONOBIT_FAIL_MASK |
  240. POKER_FAIL_MASK |
  241. LONG_RUN_FAIL_MASK |
  242. RUN_FAIL_MASK |
  243. NOISE_FAIL_MASK |
  244. STUCK_OUT_MASK |
  245. SHUTDOWN_OFLO_MASK |
  246. READY_MASK, ctx->csr_base + RNG_INTR_STS_ACK);
  247. val = ENABLE_RNG_SET(0, 1);
  248. val = MONOBIT_FAIL_MASK_SET(val, 1);
  249. val = POKER_FAIL_MASK_SET(val, 1);
  250. val = LONG_RUN_FAIL_MASK_SET(val, 1);
  251. val = RUN_FAIL_MASK_SET(val, 1);
  252. val = NOISE_FAIL_MASK_SET(val, 1);
  253. val = STUCK_OUT_MASK_SET(val, 1);
  254. val = SHUTDOWN_OFLO_MASK_SET(val, 1);
  255. writel(val, ctx->csr_base + RNG_CONTROL);
  256. }
  257. static int xgene_rng_init(struct hwrng *rng)
  258. {
  259. struct xgene_rng_dev *ctx = (struct xgene_rng_dev *) rng->priv;
  260. ctx->failure_cnt = 0;
  261. init_timer(&ctx->failure_timer);
  262. ctx->revision = readl(ctx->csr_base + RNG_EIP_REV);
  263. dev_dbg(ctx->dev, "Rev %d.%d.%d\n",
  264. MAJOR_HW_REV_RD(ctx->revision),
  265. MINOR_HW_REV_RD(ctx->revision),
  266. HW_PATCH_LEVEL_RD(ctx->revision));
  267. dev_dbg(ctx->dev, "Options 0x%08X",
  268. readl(ctx->csr_base + RNG_OPTIONS));
  269. xgene_rng_init_internal(ctx);
  270. ctx->datum_size = RNG_MAX_DATUM;
  271. return 0;
  272. }
  273. #ifdef CONFIG_ACPI
  274. static const struct acpi_device_id xgene_rng_acpi_match[] = {
  275. { "APMC0D18", },
  276. { }
  277. };
  278. MODULE_DEVICE_TABLE(acpi, xgene_rng_acpi_match);
  279. #endif
  280. static struct hwrng xgene_rng_func = {
  281. .name = "xgene-rng",
  282. .init = xgene_rng_init,
  283. .data_present = xgene_rng_data_present,
  284. .data_read = xgene_rng_data_read,
  285. };
  286. static int xgene_rng_probe(struct platform_device *pdev)
  287. {
  288. struct resource *res;
  289. struct xgene_rng_dev *ctx;
  290. int rc = 0;
  291. ctx = devm_kzalloc(&pdev->dev, sizeof(*ctx), GFP_KERNEL);
  292. if (!ctx)
  293. return -ENOMEM;
  294. ctx->dev = &pdev->dev;
  295. platform_set_drvdata(pdev, ctx);
  296. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  297. ctx->csr_base = devm_ioremap_resource(&pdev->dev, res);
  298. if (IS_ERR(ctx->csr_base))
  299. return PTR_ERR(ctx->csr_base);
  300. rc = platform_get_irq(pdev, 0);
  301. if (rc < 0) {
  302. dev_err(&pdev->dev, "No IRQ resource\n");
  303. return rc;
  304. }
  305. ctx->irq = rc;
  306. dev_dbg(&pdev->dev, "APM X-Gene RNG BASE %p ALARM IRQ %d",
  307. ctx->csr_base, ctx->irq);
  308. rc = devm_request_irq(&pdev->dev, ctx->irq, xgene_rng_irq_handler, 0,
  309. dev_name(&pdev->dev), ctx);
  310. if (rc) {
  311. dev_err(&pdev->dev, "Could not request RNG alarm IRQ\n");
  312. return rc;
  313. }
  314. /* Enable IP clock */
  315. ctx->clk = devm_clk_get(&pdev->dev, NULL);
  316. if (IS_ERR(ctx->clk)) {
  317. dev_warn(&pdev->dev, "Couldn't get the clock for RNG\n");
  318. } else {
  319. rc = clk_prepare_enable(ctx->clk);
  320. if (rc) {
  321. dev_warn(&pdev->dev,
  322. "clock prepare enable failed for RNG");
  323. return rc;
  324. }
  325. }
  326. xgene_rng_func.priv = (unsigned long) ctx;
  327. rc = hwrng_register(&xgene_rng_func);
  328. if (rc) {
  329. dev_err(&pdev->dev, "RNG registering failed error %d\n", rc);
  330. if (!IS_ERR(ctx->clk))
  331. clk_disable_unprepare(ctx->clk);
  332. return rc;
  333. }
  334. rc = device_init_wakeup(&pdev->dev, 1);
  335. if (rc) {
  336. dev_err(&pdev->dev, "RNG device_init_wakeup failed error %d\n",
  337. rc);
  338. if (!IS_ERR(ctx->clk))
  339. clk_disable_unprepare(ctx->clk);
  340. hwrng_unregister(&xgene_rng_func);
  341. return rc;
  342. }
  343. return 0;
  344. }
  345. static int xgene_rng_remove(struct platform_device *pdev)
  346. {
  347. struct xgene_rng_dev *ctx = platform_get_drvdata(pdev);
  348. int rc;
  349. rc = device_init_wakeup(&pdev->dev, 0);
  350. if (rc)
  351. dev_err(&pdev->dev, "RNG init wakeup failed error %d\n", rc);
  352. if (!IS_ERR(ctx->clk))
  353. clk_disable_unprepare(ctx->clk);
  354. hwrng_unregister(&xgene_rng_func);
  355. return rc;
  356. }
  357. static const struct of_device_id xgene_rng_of_match[] = {
  358. { .compatible = "apm,xgene-rng" },
  359. { }
  360. };
  361. MODULE_DEVICE_TABLE(of, xgene_rng_of_match);
  362. static struct platform_driver xgene_rng_driver = {
  363. .probe = xgene_rng_probe,
  364. .remove = xgene_rng_remove,
  365. .driver = {
  366. .name = "xgene-rng",
  367. .of_match_table = xgene_rng_of_match,
  368. .acpi_match_table = ACPI_PTR(xgene_rng_acpi_match),
  369. },
  370. };
  371. module_platform_driver(xgene_rng_driver);
  372. MODULE_DESCRIPTION("APM X-Gene RNG driver");
  373. MODULE_LICENSE("GPL");