omap-rng.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  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 <asm/io.h>
  31. #define RNG_REG_STATUS_RDY (1 << 0)
  32. #define RNG_REG_INTACK_RDY_MASK (1 << 0)
  33. #define RNG_REG_INTACK_SHUTDOWN_OFLO_MASK (1 << 1)
  34. #define RNG_SHUTDOWN_OFLO_MASK (1 << 1)
  35. #define RNG_CONTROL_STARTUP_CYCLES_SHIFT 16
  36. #define RNG_CONTROL_STARTUP_CYCLES_MASK (0xffff << 16)
  37. #define RNG_CONTROL_ENABLE_TRNG_SHIFT 10
  38. #define RNG_CONTROL_ENABLE_TRNG_MASK (1 << 10)
  39. #define RNG_CONFIG_MAX_REFIL_CYCLES_SHIFT 16
  40. #define RNG_CONFIG_MAX_REFIL_CYCLES_MASK (0xffff << 16)
  41. #define RNG_CONFIG_MIN_REFIL_CYCLES_SHIFT 0
  42. #define RNG_CONFIG_MIN_REFIL_CYCLES_MASK (0xff << 0)
  43. #define RNG_CONTROL_STARTUP_CYCLES 0xff
  44. #define RNG_CONFIG_MIN_REFIL_CYCLES 0x21
  45. #define RNG_CONFIG_MAX_REFIL_CYCLES 0x22
  46. #define RNG_ALARMCNT_ALARM_TH_SHIFT 0x0
  47. #define RNG_ALARMCNT_ALARM_TH_MASK (0xff << 0)
  48. #define RNG_ALARMCNT_SHUTDOWN_TH_SHIFT 16
  49. #define RNG_ALARMCNT_SHUTDOWN_TH_MASK (0x1f << 16)
  50. #define RNG_ALARM_THRESHOLD 0xff
  51. #define RNG_SHUTDOWN_THRESHOLD 0x4
  52. #define RNG_REG_FROENABLE_MASK 0xffffff
  53. #define RNG_REG_FRODETUNE_MASK 0xffffff
  54. #define OMAP2_RNG_OUTPUT_SIZE 0x4
  55. #define OMAP4_RNG_OUTPUT_SIZE 0x8
  56. enum {
  57. RNG_OUTPUT_L_REG = 0,
  58. RNG_OUTPUT_H_REG,
  59. RNG_STATUS_REG,
  60. RNG_INTMASK_REG,
  61. RNG_INTACK_REG,
  62. RNG_CONTROL_REG,
  63. RNG_CONFIG_REG,
  64. RNG_ALARMCNT_REG,
  65. RNG_FROENABLE_REG,
  66. RNG_FRODETUNE_REG,
  67. RNG_ALARMMASK_REG,
  68. RNG_ALARMSTOP_REG,
  69. RNG_REV_REG,
  70. RNG_SYSCONFIG_REG,
  71. };
  72. static const u16 reg_map_omap2[] = {
  73. [RNG_OUTPUT_L_REG] = 0x0,
  74. [RNG_STATUS_REG] = 0x4,
  75. [RNG_CONFIG_REG] = 0x28,
  76. [RNG_REV_REG] = 0x3c,
  77. [RNG_SYSCONFIG_REG] = 0x40,
  78. };
  79. static const u16 reg_map_omap4[] = {
  80. [RNG_OUTPUT_L_REG] = 0x0,
  81. [RNG_OUTPUT_H_REG] = 0x4,
  82. [RNG_STATUS_REG] = 0x8,
  83. [RNG_INTMASK_REG] = 0xc,
  84. [RNG_INTACK_REG] = 0x10,
  85. [RNG_CONTROL_REG] = 0x14,
  86. [RNG_CONFIG_REG] = 0x18,
  87. [RNG_ALARMCNT_REG] = 0x1c,
  88. [RNG_FROENABLE_REG] = 0x20,
  89. [RNG_FRODETUNE_REG] = 0x24,
  90. [RNG_ALARMMASK_REG] = 0x28,
  91. [RNG_ALARMSTOP_REG] = 0x2c,
  92. [RNG_REV_REG] = 0x1FE0,
  93. [RNG_SYSCONFIG_REG] = 0x1FE4,
  94. };
  95. struct omap_rng_dev;
  96. /**
  97. * struct omap_rng_pdata - RNG IP block-specific data
  98. * @regs: Pointer to the register offsets structure.
  99. * @data_size: No. of bytes in RNG output.
  100. * @data_present: Callback to determine if data is available.
  101. * @init: Callback for IP specific initialization sequence.
  102. * @cleanup: Callback for IP specific cleanup sequence.
  103. */
  104. struct omap_rng_pdata {
  105. u16 *regs;
  106. u32 data_size;
  107. u32 (*data_present)(struct omap_rng_dev *priv);
  108. int (*init)(struct omap_rng_dev *priv);
  109. void (*cleanup)(struct omap_rng_dev *priv);
  110. };
  111. struct omap_rng_dev {
  112. void __iomem *base;
  113. struct device *dev;
  114. const struct omap_rng_pdata *pdata;
  115. };
  116. static inline u32 omap_rng_read(struct omap_rng_dev *priv, u16 reg)
  117. {
  118. return __raw_readl(priv->base + priv->pdata->regs[reg]);
  119. }
  120. static inline void omap_rng_write(struct omap_rng_dev *priv, u16 reg,
  121. u32 val)
  122. {
  123. __raw_writel(val, priv->base + priv->pdata->regs[reg]);
  124. }
  125. static int omap_rng_data_present(struct hwrng *rng, int wait)
  126. {
  127. struct omap_rng_dev *priv;
  128. int data, i;
  129. priv = (struct omap_rng_dev *)rng->priv;
  130. for (i = 0; i < 20; i++) {
  131. data = priv->pdata->data_present(priv);
  132. if (data || !wait)
  133. break;
  134. /* RNG produces data fast enough (2+ MBit/sec, even
  135. * during "rngtest" loads, that these delays don't
  136. * seem to trigger. We *could* use the RNG IRQ, but
  137. * that'd be higher overhead ... so why bother?
  138. */
  139. udelay(10);
  140. }
  141. return data;
  142. }
  143. static int omap_rng_data_read(struct hwrng *rng, u32 *data)
  144. {
  145. struct omap_rng_dev *priv;
  146. u32 data_size, i;
  147. priv = (struct omap_rng_dev *)rng->priv;
  148. data_size = priv->pdata->data_size;
  149. for (i = 0; i < data_size / sizeof(u32); i++)
  150. data[i] = omap_rng_read(priv, RNG_OUTPUT_L_REG + i);
  151. if (priv->pdata->regs[RNG_INTACK_REG])
  152. omap_rng_write(priv, RNG_INTACK_REG, RNG_REG_INTACK_RDY_MASK);
  153. return data_size;
  154. }
  155. static int omap_rng_init(struct hwrng *rng)
  156. {
  157. struct omap_rng_dev *priv;
  158. priv = (struct omap_rng_dev *)rng->priv;
  159. return priv->pdata->init(priv);
  160. }
  161. static void omap_rng_cleanup(struct hwrng *rng)
  162. {
  163. struct omap_rng_dev *priv;
  164. priv = (struct omap_rng_dev *)rng->priv;
  165. priv->pdata->cleanup(priv);
  166. }
  167. static struct hwrng omap_rng_ops = {
  168. .name = "omap",
  169. .data_present = omap_rng_data_present,
  170. .data_read = omap_rng_data_read,
  171. .init = omap_rng_init,
  172. .cleanup = omap_rng_cleanup,
  173. };
  174. static inline u32 omap2_rng_data_present(struct omap_rng_dev *priv)
  175. {
  176. return omap_rng_read(priv, RNG_STATUS_REG) ? 0 : 1;
  177. }
  178. static int omap2_rng_init(struct omap_rng_dev *priv)
  179. {
  180. omap_rng_write(priv, RNG_SYSCONFIG_REG, 0x1);
  181. return 0;
  182. }
  183. static void omap2_rng_cleanup(struct omap_rng_dev *priv)
  184. {
  185. omap_rng_write(priv, RNG_SYSCONFIG_REG, 0x0);
  186. }
  187. static struct omap_rng_pdata omap2_rng_pdata = {
  188. .regs = (u16 *)reg_map_omap2,
  189. .data_size = OMAP2_RNG_OUTPUT_SIZE,
  190. .data_present = omap2_rng_data_present,
  191. .init = omap2_rng_init,
  192. .cleanup = omap2_rng_cleanup,
  193. };
  194. #if defined(CONFIG_OF)
  195. static inline u32 omap4_rng_data_present(struct omap_rng_dev *priv)
  196. {
  197. return omap_rng_read(priv, RNG_STATUS_REG) & RNG_REG_STATUS_RDY;
  198. }
  199. static int omap4_rng_init(struct omap_rng_dev *priv)
  200. {
  201. u32 val;
  202. /* Return if RNG is already running. */
  203. if (omap_rng_read(priv, RNG_CONTROL_REG) & RNG_CONTROL_ENABLE_TRNG_MASK)
  204. return 0;
  205. val = RNG_CONFIG_MIN_REFIL_CYCLES << RNG_CONFIG_MIN_REFIL_CYCLES_SHIFT;
  206. val |= RNG_CONFIG_MAX_REFIL_CYCLES << RNG_CONFIG_MAX_REFIL_CYCLES_SHIFT;
  207. omap_rng_write(priv, RNG_CONFIG_REG, val);
  208. omap_rng_write(priv, RNG_FRODETUNE_REG, 0x0);
  209. omap_rng_write(priv, RNG_FROENABLE_REG, RNG_REG_FROENABLE_MASK);
  210. val = RNG_ALARM_THRESHOLD << RNG_ALARMCNT_ALARM_TH_SHIFT;
  211. val |= RNG_SHUTDOWN_THRESHOLD << RNG_ALARMCNT_SHUTDOWN_TH_SHIFT;
  212. omap_rng_write(priv, RNG_ALARMCNT_REG, val);
  213. val = RNG_CONTROL_STARTUP_CYCLES << RNG_CONTROL_STARTUP_CYCLES_SHIFT;
  214. val |= RNG_CONTROL_ENABLE_TRNG_MASK;
  215. omap_rng_write(priv, RNG_CONTROL_REG, val);
  216. return 0;
  217. }
  218. static void omap4_rng_cleanup(struct omap_rng_dev *priv)
  219. {
  220. int val;
  221. val = omap_rng_read(priv, RNG_CONTROL_REG);
  222. val &= ~RNG_CONTROL_ENABLE_TRNG_MASK;
  223. omap_rng_write(priv, RNG_CONTROL_REG, val);
  224. }
  225. static irqreturn_t omap4_rng_irq(int irq, void *dev_id)
  226. {
  227. struct omap_rng_dev *priv = dev_id;
  228. u32 fro_detune, fro_enable;
  229. /*
  230. * Interrupt raised by a fro shutdown threshold, do the following:
  231. * 1. Clear the alarm events.
  232. * 2. De tune the FROs which are shutdown.
  233. * 3. Re enable the shutdown FROs.
  234. */
  235. omap_rng_write(priv, RNG_ALARMMASK_REG, 0x0);
  236. omap_rng_write(priv, RNG_ALARMSTOP_REG, 0x0);
  237. fro_enable = omap_rng_read(priv, RNG_FROENABLE_REG);
  238. fro_detune = ~fro_enable & RNG_REG_FRODETUNE_MASK;
  239. fro_detune = fro_detune | omap_rng_read(priv, RNG_FRODETUNE_REG);
  240. fro_enable = RNG_REG_FROENABLE_MASK;
  241. omap_rng_write(priv, RNG_FRODETUNE_REG, fro_detune);
  242. omap_rng_write(priv, RNG_FROENABLE_REG, fro_enable);
  243. omap_rng_write(priv, RNG_INTACK_REG, RNG_REG_INTACK_SHUTDOWN_OFLO_MASK);
  244. return IRQ_HANDLED;
  245. }
  246. static struct omap_rng_pdata omap4_rng_pdata = {
  247. .regs = (u16 *)reg_map_omap4,
  248. .data_size = OMAP4_RNG_OUTPUT_SIZE,
  249. .data_present = omap4_rng_data_present,
  250. .init = omap4_rng_init,
  251. .cleanup = omap4_rng_cleanup,
  252. };
  253. static const struct of_device_id omap_rng_of_match[] = {
  254. {
  255. .compatible = "ti,omap2-rng",
  256. .data = &omap2_rng_pdata,
  257. },
  258. {
  259. .compatible = "ti,omap4-rng",
  260. .data = &omap4_rng_pdata,
  261. },
  262. {},
  263. };
  264. MODULE_DEVICE_TABLE(of, omap_rng_of_match);
  265. static int of_get_omap_rng_device_details(struct omap_rng_dev *priv,
  266. struct platform_device *pdev)
  267. {
  268. const struct of_device_id *match;
  269. struct device *dev = &pdev->dev;
  270. int irq, err;
  271. match = of_match_device(of_match_ptr(omap_rng_of_match), dev);
  272. if (!match) {
  273. dev_err(dev, "no compatible OF match\n");
  274. return -EINVAL;
  275. }
  276. priv->pdata = match->data;
  277. if (of_device_is_compatible(dev->of_node, "ti,omap4-rng")) {
  278. irq = platform_get_irq(pdev, 0);
  279. if (irq < 0) {
  280. dev_err(dev, "%s: error getting IRQ resource - %d\n",
  281. __func__, irq);
  282. return irq;
  283. }
  284. err = devm_request_irq(dev, irq, omap4_rng_irq,
  285. IRQF_TRIGGER_NONE, dev_name(dev), priv);
  286. if (err) {
  287. dev_err(dev, "unable to request irq %d, err = %d\n",
  288. irq, err);
  289. return err;
  290. }
  291. omap_rng_write(priv, RNG_INTMASK_REG, RNG_SHUTDOWN_OFLO_MASK);
  292. }
  293. return 0;
  294. }
  295. #else
  296. static int of_get_omap_rng_device_details(struct omap_rng_dev *omap_rng,
  297. struct platform_device *pdev)
  298. {
  299. return -EINVAL;
  300. }
  301. #endif
  302. static int get_omap_rng_device_details(struct omap_rng_dev *omap_rng)
  303. {
  304. /* Only OMAP2/3 can be non-DT */
  305. omap_rng->pdata = &omap2_rng_pdata;
  306. return 0;
  307. }
  308. static int omap_rng_probe(struct platform_device *pdev)
  309. {
  310. struct omap_rng_dev *priv;
  311. struct resource *res;
  312. struct device *dev = &pdev->dev;
  313. int ret;
  314. priv = devm_kzalloc(dev, sizeof(struct omap_rng_dev), GFP_KERNEL);
  315. if (!priv)
  316. return -ENOMEM;
  317. omap_rng_ops.priv = (unsigned long)priv;
  318. platform_set_drvdata(pdev, priv);
  319. priv->dev = dev;
  320. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  321. priv->base = devm_ioremap_resource(dev, res);
  322. if (IS_ERR(priv->base)) {
  323. ret = PTR_ERR(priv->base);
  324. goto err_ioremap;
  325. }
  326. pm_runtime_enable(&pdev->dev);
  327. pm_runtime_get_sync(&pdev->dev);
  328. ret = (dev->of_node) ? of_get_omap_rng_device_details(priv, pdev) :
  329. get_omap_rng_device_details(priv);
  330. if (ret)
  331. goto err_ioremap;
  332. ret = hwrng_register(&omap_rng_ops);
  333. if (ret)
  334. goto err_register;
  335. dev_info(&pdev->dev, "OMAP Random Number Generator ver. %02x\n",
  336. omap_rng_read(priv, RNG_REV_REG));
  337. return 0;
  338. err_register:
  339. priv->base = NULL;
  340. pm_runtime_disable(&pdev->dev);
  341. err_ioremap:
  342. dev_err(dev, "initialization failed.\n");
  343. return ret;
  344. }
  345. static int omap_rng_remove(struct platform_device *pdev)
  346. {
  347. struct omap_rng_dev *priv = platform_get_drvdata(pdev);
  348. hwrng_unregister(&omap_rng_ops);
  349. priv->pdata->cleanup(priv);
  350. pm_runtime_put_sync(&pdev->dev);
  351. pm_runtime_disable(&pdev->dev);
  352. return 0;
  353. }
  354. static int __maybe_unused omap_rng_suspend(struct device *dev)
  355. {
  356. struct omap_rng_dev *priv = dev_get_drvdata(dev);
  357. priv->pdata->cleanup(priv);
  358. pm_runtime_put_sync(dev);
  359. return 0;
  360. }
  361. static int __maybe_unused omap_rng_resume(struct device *dev)
  362. {
  363. struct omap_rng_dev *priv = dev_get_drvdata(dev);
  364. pm_runtime_get_sync(dev);
  365. priv->pdata->init(priv);
  366. return 0;
  367. }
  368. static SIMPLE_DEV_PM_OPS(omap_rng_pm, omap_rng_suspend, omap_rng_resume);
  369. static struct platform_driver omap_rng_driver = {
  370. .driver = {
  371. .name = "omap_rng",
  372. .pm = &omap_rng_pm,
  373. .of_match_table = of_match_ptr(omap_rng_of_match),
  374. },
  375. .probe = omap_rng_probe,
  376. .remove = omap_rng_remove,
  377. };
  378. module_platform_driver(omap_rng_driver);
  379. MODULE_ALIAS("platform:omap_rng");
  380. MODULE_AUTHOR("Deepak Saxena (and others)");
  381. MODULE_LICENSE("GPL");