omap-rng.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  1. /*
  2. * omap-rng.c - RNG driver for TI OMAP CPU family
  3. *
  4. * Author: Deepak Saxena <dsaxena@plexity.net>
  5. *
  6. * Copyright 2005 (c) MontaVista Software, Inc.
  7. *
  8. * Mostly based on original driver:
  9. *
  10. * Copyright (C) 2005 Nokia Corporation
  11. * Author: Juha Yrjölä <juha.yrjola@nokia.com>
  12. *
  13. * This file is licensed under the terms of the GNU General Public
  14. * License version 2. This program is licensed "as is" without any
  15. * warranty of any kind, whether express or implied.
  16. */
  17. #include <linux/module.h>
  18. #include <linux/init.h>
  19. #include <linux/random.h>
  20. #include <linux/err.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/hw_random.h>
  23. #include <linux/delay.h>
  24. #include <linux/slab.h>
  25. #include <linux/pm_runtime.h>
  26. #include <linux/of.h>
  27. #include <linux/of_device.h>
  28. #include <linux/of_address.h>
  29. #include <linux/interrupt.h>
  30. #include <linux/clk.h>
  31. #include <asm/io.h>
  32. #define RNG_REG_STATUS_RDY (1 << 0)
  33. #define RNG_REG_INTACK_RDY_MASK (1 << 0)
  34. #define RNG_REG_INTACK_SHUTDOWN_OFLO_MASK (1 << 1)
  35. #define RNG_SHUTDOWN_OFLO_MASK (1 << 1)
  36. #define RNG_CONTROL_STARTUP_CYCLES_SHIFT 16
  37. #define RNG_CONTROL_STARTUP_CYCLES_MASK (0xffff << 16)
  38. #define RNG_CONTROL_ENABLE_TRNG_SHIFT 10
  39. #define RNG_CONTROL_ENABLE_TRNG_MASK (1 << 10)
  40. #define RNG_CONFIG_MAX_REFIL_CYCLES_SHIFT 16
  41. #define RNG_CONFIG_MAX_REFIL_CYCLES_MASK (0xffff << 16)
  42. #define RNG_CONFIG_MIN_REFIL_CYCLES_SHIFT 0
  43. #define RNG_CONFIG_MIN_REFIL_CYCLES_MASK (0xff << 0)
  44. #define RNG_CONTROL_STARTUP_CYCLES 0xff
  45. #define RNG_CONFIG_MIN_REFIL_CYCLES 0x21
  46. #define RNG_CONFIG_MAX_REFIL_CYCLES 0x22
  47. #define RNG_ALARMCNT_ALARM_TH_SHIFT 0x0
  48. #define RNG_ALARMCNT_ALARM_TH_MASK (0xff << 0)
  49. #define RNG_ALARMCNT_SHUTDOWN_TH_SHIFT 16
  50. #define RNG_ALARMCNT_SHUTDOWN_TH_MASK (0x1f << 16)
  51. #define RNG_ALARM_THRESHOLD 0xff
  52. #define RNG_SHUTDOWN_THRESHOLD 0x4
  53. #define RNG_REG_FROENABLE_MASK 0xffffff
  54. #define RNG_REG_FRODETUNE_MASK 0xffffff
  55. #define OMAP2_RNG_OUTPUT_SIZE 0x4
  56. #define OMAP4_RNG_OUTPUT_SIZE 0x8
  57. #define EIP76_RNG_OUTPUT_SIZE 0x10
  58. enum {
  59. RNG_OUTPUT_0_REG = 0,
  60. RNG_OUTPUT_1_REG,
  61. RNG_OUTPUT_2_REG,
  62. RNG_OUTPUT_3_REG,
  63. RNG_STATUS_REG,
  64. RNG_INTMASK_REG,
  65. RNG_INTACK_REG,
  66. RNG_CONTROL_REG,
  67. RNG_CONFIG_REG,
  68. RNG_ALARMCNT_REG,
  69. RNG_FROENABLE_REG,
  70. RNG_FRODETUNE_REG,
  71. RNG_ALARMMASK_REG,
  72. RNG_ALARMSTOP_REG,
  73. RNG_REV_REG,
  74. RNG_SYSCONFIG_REG,
  75. };
  76. static const u16 reg_map_omap2[] = {
  77. [RNG_OUTPUT_0_REG] = 0x0,
  78. [RNG_STATUS_REG] = 0x4,
  79. [RNG_CONFIG_REG] = 0x28,
  80. [RNG_REV_REG] = 0x3c,
  81. [RNG_SYSCONFIG_REG] = 0x40,
  82. };
  83. static const u16 reg_map_omap4[] = {
  84. [RNG_OUTPUT_0_REG] = 0x0,
  85. [RNG_OUTPUT_1_REG] = 0x4,
  86. [RNG_STATUS_REG] = 0x8,
  87. [RNG_INTMASK_REG] = 0xc,
  88. [RNG_INTACK_REG] = 0x10,
  89. [RNG_CONTROL_REG] = 0x14,
  90. [RNG_CONFIG_REG] = 0x18,
  91. [RNG_ALARMCNT_REG] = 0x1c,
  92. [RNG_FROENABLE_REG] = 0x20,
  93. [RNG_FRODETUNE_REG] = 0x24,
  94. [RNG_ALARMMASK_REG] = 0x28,
  95. [RNG_ALARMSTOP_REG] = 0x2c,
  96. [RNG_REV_REG] = 0x1FE0,
  97. [RNG_SYSCONFIG_REG] = 0x1FE4,
  98. };
  99. static const u16 reg_map_eip76[] = {
  100. [RNG_OUTPUT_0_REG] = 0x0,
  101. [RNG_OUTPUT_1_REG] = 0x4,
  102. [RNG_OUTPUT_2_REG] = 0x8,
  103. [RNG_OUTPUT_3_REG] = 0xc,
  104. [RNG_STATUS_REG] = 0x10,
  105. [RNG_INTACK_REG] = 0x10,
  106. [RNG_CONTROL_REG] = 0x14,
  107. [RNG_CONFIG_REG] = 0x18,
  108. [RNG_ALARMCNT_REG] = 0x1c,
  109. [RNG_FROENABLE_REG] = 0x20,
  110. [RNG_FRODETUNE_REG] = 0x24,
  111. [RNG_ALARMMASK_REG] = 0x28,
  112. [RNG_ALARMSTOP_REG] = 0x2c,
  113. [RNG_REV_REG] = 0x7c,
  114. };
  115. struct omap_rng_dev;
  116. /**
  117. * struct omap_rng_pdata - RNG IP block-specific data
  118. * @regs: Pointer to the register offsets structure.
  119. * @data_size: No. of bytes in RNG output.
  120. * @data_present: Callback to determine if data is available.
  121. * @init: Callback for IP specific initialization sequence.
  122. * @cleanup: Callback for IP specific cleanup sequence.
  123. */
  124. struct omap_rng_pdata {
  125. u16 *regs;
  126. u32 data_size;
  127. u32 (*data_present)(struct omap_rng_dev *priv);
  128. int (*init)(struct omap_rng_dev *priv);
  129. void (*cleanup)(struct omap_rng_dev *priv);
  130. };
  131. struct omap_rng_dev {
  132. void __iomem *base;
  133. struct device *dev;
  134. const struct omap_rng_pdata *pdata;
  135. struct hwrng rng;
  136. struct clk *clk;
  137. };
  138. static inline u32 omap_rng_read(struct omap_rng_dev *priv, u16 reg)
  139. {
  140. return __raw_readl(priv->base + priv->pdata->regs[reg]);
  141. }
  142. static inline void omap_rng_write(struct omap_rng_dev *priv, u16 reg,
  143. u32 val)
  144. {
  145. __raw_writel(val, priv->base + priv->pdata->regs[reg]);
  146. }
  147. static int omap_rng_do_read(struct hwrng *rng, void *data, size_t max,
  148. bool wait)
  149. {
  150. struct omap_rng_dev *priv;
  151. int i, present;
  152. priv = (struct omap_rng_dev *)rng->priv;
  153. if (max < priv->pdata->data_size)
  154. return 0;
  155. for (i = 0; i < 20; i++) {
  156. present = priv->pdata->data_present(priv);
  157. if (present || !wait)
  158. break;
  159. udelay(10);
  160. }
  161. if (!present)
  162. return 0;
  163. memcpy_fromio(data, priv->base + priv->pdata->regs[RNG_OUTPUT_0_REG],
  164. priv->pdata->data_size);
  165. if (priv->pdata->regs[RNG_INTACK_REG])
  166. omap_rng_write(priv, RNG_INTACK_REG, RNG_REG_INTACK_RDY_MASK);
  167. return priv->pdata->data_size;
  168. }
  169. static int omap_rng_init(struct hwrng *rng)
  170. {
  171. struct omap_rng_dev *priv;
  172. priv = (struct omap_rng_dev *)rng->priv;
  173. return priv->pdata->init(priv);
  174. }
  175. static void omap_rng_cleanup(struct hwrng *rng)
  176. {
  177. struct omap_rng_dev *priv;
  178. priv = (struct omap_rng_dev *)rng->priv;
  179. priv->pdata->cleanup(priv);
  180. }
  181. static inline u32 omap2_rng_data_present(struct omap_rng_dev *priv)
  182. {
  183. return omap_rng_read(priv, RNG_STATUS_REG) ? 0 : 1;
  184. }
  185. static int omap2_rng_init(struct omap_rng_dev *priv)
  186. {
  187. omap_rng_write(priv, RNG_SYSCONFIG_REG, 0x1);
  188. return 0;
  189. }
  190. static void omap2_rng_cleanup(struct omap_rng_dev *priv)
  191. {
  192. omap_rng_write(priv, RNG_SYSCONFIG_REG, 0x0);
  193. }
  194. static struct omap_rng_pdata omap2_rng_pdata = {
  195. .regs = (u16 *)reg_map_omap2,
  196. .data_size = OMAP2_RNG_OUTPUT_SIZE,
  197. .data_present = omap2_rng_data_present,
  198. .init = omap2_rng_init,
  199. .cleanup = omap2_rng_cleanup,
  200. };
  201. #if defined(CONFIG_OF)
  202. static inline u32 omap4_rng_data_present(struct omap_rng_dev *priv)
  203. {
  204. return omap_rng_read(priv, RNG_STATUS_REG) & RNG_REG_STATUS_RDY;
  205. }
  206. static int eip76_rng_init(struct omap_rng_dev *priv)
  207. {
  208. u32 val;
  209. /* Return if RNG is already running. */
  210. if (omap_rng_read(priv, RNG_CONTROL_REG) & RNG_CONTROL_ENABLE_TRNG_MASK)
  211. return 0;
  212. /* Number of 512 bit blocks of raw Noise Source output data that must
  213. * be processed by either the Conditioning Function or the
  214. * SP 800-90 DRBG ‘BC_DF’ functionality to yield a ‘full entropy’
  215. * output value.
  216. */
  217. val = 0x5 << RNG_CONFIG_MIN_REFIL_CYCLES_SHIFT;
  218. /* Number of FRO samples that are XOR-ed together into one bit to be
  219. * shifted into the main shift register
  220. */
  221. val |= RNG_CONFIG_MAX_REFIL_CYCLES << RNG_CONFIG_MAX_REFIL_CYCLES_SHIFT;
  222. omap_rng_write(priv, RNG_CONFIG_REG, val);
  223. /* Enable all available FROs */
  224. omap_rng_write(priv, RNG_FRODETUNE_REG, 0x0);
  225. omap_rng_write(priv, RNG_FROENABLE_REG, RNG_REG_FROENABLE_MASK);
  226. /* Enable TRNG */
  227. val = RNG_CONTROL_ENABLE_TRNG_MASK;
  228. omap_rng_write(priv, RNG_CONTROL_REG, val);
  229. return 0;
  230. }
  231. static int omap4_rng_init(struct omap_rng_dev *priv)
  232. {
  233. u32 val;
  234. /* Return if RNG is already running. */
  235. if (omap_rng_read(priv, RNG_CONTROL_REG) & RNG_CONTROL_ENABLE_TRNG_MASK)
  236. return 0;
  237. val = RNG_CONFIG_MIN_REFIL_CYCLES << RNG_CONFIG_MIN_REFIL_CYCLES_SHIFT;
  238. val |= RNG_CONFIG_MAX_REFIL_CYCLES << RNG_CONFIG_MAX_REFIL_CYCLES_SHIFT;
  239. omap_rng_write(priv, RNG_CONFIG_REG, val);
  240. omap_rng_write(priv, RNG_FRODETUNE_REG, 0x0);
  241. omap_rng_write(priv, RNG_FROENABLE_REG, RNG_REG_FROENABLE_MASK);
  242. val = RNG_ALARM_THRESHOLD << RNG_ALARMCNT_ALARM_TH_SHIFT;
  243. val |= RNG_SHUTDOWN_THRESHOLD << RNG_ALARMCNT_SHUTDOWN_TH_SHIFT;
  244. omap_rng_write(priv, RNG_ALARMCNT_REG, val);
  245. val = RNG_CONTROL_STARTUP_CYCLES << RNG_CONTROL_STARTUP_CYCLES_SHIFT;
  246. val |= RNG_CONTROL_ENABLE_TRNG_MASK;
  247. omap_rng_write(priv, RNG_CONTROL_REG, val);
  248. return 0;
  249. }
  250. static void omap4_rng_cleanup(struct omap_rng_dev *priv)
  251. {
  252. int val;
  253. val = omap_rng_read(priv, RNG_CONTROL_REG);
  254. val &= ~RNG_CONTROL_ENABLE_TRNG_MASK;
  255. omap_rng_write(priv, RNG_CONTROL_REG, val);
  256. }
  257. static irqreturn_t omap4_rng_irq(int irq, void *dev_id)
  258. {
  259. struct omap_rng_dev *priv = dev_id;
  260. u32 fro_detune, fro_enable;
  261. /*
  262. * Interrupt raised by a fro shutdown threshold, do the following:
  263. * 1. Clear the alarm events.
  264. * 2. De tune the FROs which are shutdown.
  265. * 3. Re enable the shutdown FROs.
  266. */
  267. omap_rng_write(priv, RNG_ALARMMASK_REG, 0x0);
  268. omap_rng_write(priv, RNG_ALARMSTOP_REG, 0x0);
  269. fro_enable = omap_rng_read(priv, RNG_FROENABLE_REG);
  270. fro_detune = ~fro_enable & RNG_REG_FRODETUNE_MASK;
  271. fro_detune = fro_detune | omap_rng_read(priv, RNG_FRODETUNE_REG);
  272. fro_enable = RNG_REG_FROENABLE_MASK;
  273. omap_rng_write(priv, RNG_FRODETUNE_REG, fro_detune);
  274. omap_rng_write(priv, RNG_FROENABLE_REG, fro_enable);
  275. omap_rng_write(priv, RNG_INTACK_REG, RNG_REG_INTACK_SHUTDOWN_OFLO_MASK);
  276. return IRQ_HANDLED;
  277. }
  278. static struct omap_rng_pdata omap4_rng_pdata = {
  279. .regs = (u16 *)reg_map_omap4,
  280. .data_size = OMAP4_RNG_OUTPUT_SIZE,
  281. .data_present = omap4_rng_data_present,
  282. .init = omap4_rng_init,
  283. .cleanup = omap4_rng_cleanup,
  284. };
  285. static struct omap_rng_pdata eip76_rng_pdata = {
  286. .regs = (u16 *)reg_map_eip76,
  287. .data_size = EIP76_RNG_OUTPUT_SIZE,
  288. .data_present = omap4_rng_data_present,
  289. .init = eip76_rng_init,
  290. .cleanup = omap4_rng_cleanup,
  291. };
  292. static const struct of_device_id omap_rng_of_match[] = {
  293. {
  294. .compatible = "ti,omap2-rng",
  295. .data = &omap2_rng_pdata,
  296. },
  297. {
  298. .compatible = "ti,omap4-rng",
  299. .data = &omap4_rng_pdata,
  300. },
  301. {
  302. .compatible = "inside-secure,safexcel-eip76",
  303. .data = &eip76_rng_pdata,
  304. },
  305. {},
  306. };
  307. MODULE_DEVICE_TABLE(of, omap_rng_of_match);
  308. static int of_get_omap_rng_device_details(struct omap_rng_dev *priv,
  309. struct platform_device *pdev)
  310. {
  311. const struct of_device_id *match;
  312. struct device *dev = &pdev->dev;
  313. int irq, err;
  314. match = of_match_device(of_match_ptr(omap_rng_of_match), dev);
  315. if (!match) {
  316. dev_err(dev, "no compatible OF match\n");
  317. return -EINVAL;
  318. }
  319. priv->pdata = match->data;
  320. if (of_device_is_compatible(dev->of_node, "ti,omap4-rng") ||
  321. of_device_is_compatible(dev->of_node, "inside-secure,safexcel-eip76")) {
  322. irq = platform_get_irq(pdev, 0);
  323. if (irq < 0) {
  324. dev_err(dev, "%s: error getting IRQ resource - %d\n",
  325. __func__, irq);
  326. return irq;
  327. }
  328. err = devm_request_irq(dev, irq, omap4_rng_irq,
  329. IRQF_TRIGGER_NONE, dev_name(dev), priv);
  330. if (err) {
  331. dev_err(dev, "unable to request irq %d, err = %d\n",
  332. irq, err);
  333. return err;
  334. }
  335. omap_rng_write(priv, RNG_INTMASK_REG, RNG_SHUTDOWN_OFLO_MASK);
  336. priv->clk = of_clk_get(pdev->dev.of_node, 0);
  337. if (IS_ERR(priv->clk) && PTR_ERR(priv->clk) == -EPROBE_DEFER)
  338. return -EPROBE_DEFER;
  339. if (!IS_ERR(priv->clk)) {
  340. err = clk_prepare_enable(priv->clk);
  341. if (err)
  342. dev_err(&pdev->dev, "unable to enable the clk, "
  343. "err = %d\n", err);
  344. }
  345. }
  346. return 0;
  347. }
  348. #else
  349. static int of_get_omap_rng_device_details(struct omap_rng_dev *omap_rng,
  350. struct platform_device *pdev)
  351. {
  352. return -EINVAL;
  353. }
  354. #endif
  355. static int get_omap_rng_device_details(struct omap_rng_dev *omap_rng)
  356. {
  357. /* Only OMAP2/3 can be non-DT */
  358. omap_rng->pdata = &omap2_rng_pdata;
  359. return 0;
  360. }
  361. static int omap_rng_probe(struct platform_device *pdev)
  362. {
  363. struct omap_rng_dev *priv;
  364. struct resource *res;
  365. struct device *dev = &pdev->dev;
  366. int ret;
  367. priv = devm_kzalloc(dev, sizeof(struct omap_rng_dev), GFP_KERNEL);
  368. if (!priv)
  369. return -ENOMEM;
  370. priv->rng.read = omap_rng_do_read;
  371. priv->rng.init = omap_rng_init;
  372. priv->rng.cleanup = omap_rng_cleanup;
  373. priv->rng.priv = (unsigned long)priv;
  374. platform_set_drvdata(pdev, priv);
  375. priv->dev = dev;
  376. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  377. priv->base = devm_ioremap_resource(dev, res);
  378. if (IS_ERR(priv->base)) {
  379. ret = PTR_ERR(priv->base);
  380. goto err_ioremap;
  381. }
  382. priv->rng.name = devm_kstrdup(dev, dev_name(dev), GFP_KERNEL);
  383. if (!priv->rng.name) {
  384. ret = -ENOMEM;
  385. goto err_ioremap;
  386. }
  387. pm_runtime_enable(&pdev->dev);
  388. ret = pm_runtime_get_sync(&pdev->dev);
  389. if (ret < 0) {
  390. dev_err(&pdev->dev, "Failed to runtime_get device: %d\n", ret);
  391. pm_runtime_put_noidle(&pdev->dev);
  392. goto err_ioremap;
  393. }
  394. ret = (dev->of_node) ? of_get_omap_rng_device_details(priv, pdev) :
  395. get_omap_rng_device_details(priv);
  396. if (ret)
  397. goto err_register;
  398. ret = hwrng_register(&priv->rng);
  399. if (ret)
  400. goto err_register;
  401. dev_info(&pdev->dev, "Random Number Generator ver. %02x\n",
  402. omap_rng_read(priv, RNG_REV_REG));
  403. return 0;
  404. err_register:
  405. priv->base = NULL;
  406. pm_runtime_put_sync(&pdev->dev);
  407. pm_runtime_disable(&pdev->dev);
  408. if (!IS_ERR(priv->clk))
  409. clk_disable_unprepare(priv->clk);
  410. err_ioremap:
  411. dev_err(dev, "initialization failed.\n");
  412. return ret;
  413. }
  414. static int omap_rng_remove(struct platform_device *pdev)
  415. {
  416. struct omap_rng_dev *priv = platform_get_drvdata(pdev);
  417. hwrng_unregister(&priv->rng);
  418. priv->pdata->cleanup(priv);
  419. pm_runtime_put_sync(&pdev->dev);
  420. pm_runtime_disable(&pdev->dev);
  421. if (!IS_ERR(priv->clk))
  422. clk_disable_unprepare(priv->clk);
  423. return 0;
  424. }
  425. static int __maybe_unused omap_rng_suspend(struct device *dev)
  426. {
  427. struct omap_rng_dev *priv = dev_get_drvdata(dev);
  428. priv->pdata->cleanup(priv);
  429. pm_runtime_put_sync(dev);
  430. return 0;
  431. }
  432. static int __maybe_unused omap_rng_resume(struct device *dev)
  433. {
  434. struct omap_rng_dev *priv = dev_get_drvdata(dev);
  435. int ret;
  436. ret = pm_runtime_get_sync(dev);
  437. if (ret < 0) {
  438. dev_err(dev, "Failed to runtime_get device: %d\n", ret);
  439. pm_runtime_put_noidle(dev);
  440. return ret;
  441. }
  442. priv->pdata->init(priv);
  443. return 0;
  444. }
  445. static SIMPLE_DEV_PM_OPS(omap_rng_pm, omap_rng_suspend, omap_rng_resume);
  446. static struct platform_driver omap_rng_driver = {
  447. .driver = {
  448. .name = "omap_rng",
  449. .pm = &omap_rng_pm,
  450. .of_match_table = of_match_ptr(omap_rng_of_match),
  451. },
  452. .probe = omap_rng_probe,
  453. .remove = omap_rng_remove,
  454. };
  455. module_platform_driver(omap_rng_driver);
  456. MODULE_ALIAS("platform:omap_rng");
  457. MODULE_AUTHOR("Deepak Saxena (and others)");
  458. MODULE_LICENSE("GPL");