ks-sa-rng.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /*
  2. * Random Number Generator driver for the Keystone SOC
  3. *
  4. * Copyright (C) 2016 Texas Instruments Incorporated - http://www.ti.com
  5. *
  6. * Authors: Sandeep Nair
  7. * Vitaly Andrianov
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License
  11. * version 2 as published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. */
  18. #include <linux/hw_random.h>
  19. #include <linux/kernel.h>
  20. #include <linux/module.h>
  21. #include <linux/io.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/clk.h>
  24. #include <linux/pm_runtime.h>
  25. #include <linux/err.h>
  26. #include <linux/regmap.h>
  27. #include <linux/mfd/syscon.h>
  28. #include <linux/of.h>
  29. #include <linux/of_address.h>
  30. #include <linux/delay.h>
  31. #define SA_CMD_STATUS_OFS 0x8
  32. /* TRNG enable control in SA System module*/
  33. #define SA_CMD_STATUS_REG_TRNG_ENABLE BIT(3)
  34. /* TRNG start control in TRNG module */
  35. #define TRNG_CNTL_REG_TRNG_ENABLE BIT(10)
  36. /* Data ready indicator in STATUS register */
  37. #define TRNG_STATUS_REG_READY BIT(0)
  38. /* Data ready clear control in INTACK register */
  39. #define TRNG_INTACK_REG_READY BIT(0)
  40. /*
  41. * Number of samples taken to gather entropy during startup.
  42. * If value is 0, the number of samples is 2^24 else
  43. * equals value times 2^8.
  44. */
  45. #define TRNG_DEF_STARTUP_CYCLES 0
  46. #define TRNG_CNTL_REG_STARTUP_CYCLES_SHIFT 16
  47. /*
  48. * Minimum number of samples taken to regenerate entropy
  49. * If value is 0, the number of samples is 2^24 else
  50. * equals value times 2^6.
  51. */
  52. #define TRNG_DEF_MIN_REFILL_CYCLES 1
  53. #define TRNG_CFG_REG_MIN_REFILL_CYCLES_SHIFT 0
  54. /*
  55. * Maximum number of samples taken to regenerate entropy
  56. * If value is 0, the number of samples is 2^24 else
  57. * equals value times 2^8.
  58. */
  59. #define TRNG_DEF_MAX_REFILL_CYCLES 0
  60. #define TRNG_CFG_REG_MAX_REFILL_CYCLES_SHIFT 16
  61. /* Number of CLK input cycles between samples */
  62. #define TRNG_DEF_CLK_DIV_CYCLES 0
  63. #define TRNG_CFG_REG_SAMPLE_DIV_SHIFT 8
  64. /* Maximum retries to get rng data */
  65. #define SA_MAX_RNG_DATA_RETRIES 5
  66. /* Delay between retries (in usecs) */
  67. #define SA_RNG_DATA_RETRY_DELAY 5
  68. struct trng_regs {
  69. u32 output_l;
  70. u32 output_h;
  71. u32 status;
  72. u32 intmask;
  73. u32 intack;
  74. u32 control;
  75. u32 config;
  76. };
  77. struct ks_sa_rng {
  78. struct device *dev;
  79. struct hwrng rng;
  80. struct clk *clk;
  81. struct regmap *regmap_cfg;
  82. struct trng_regs *reg_rng;
  83. };
  84. static int ks_sa_rng_init(struct hwrng *rng)
  85. {
  86. u32 value;
  87. struct device *dev = (struct device *)rng->priv;
  88. struct ks_sa_rng *ks_sa_rng = dev_get_drvdata(dev);
  89. /* Enable RNG module */
  90. regmap_write_bits(ks_sa_rng->regmap_cfg, SA_CMD_STATUS_OFS,
  91. SA_CMD_STATUS_REG_TRNG_ENABLE,
  92. SA_CMD_STATUS_REG_TRNG_ENABLE);
  93. /* Configure RNG module */
  94. writel(0, &ks_sa_rng->reg_rng->control);
  95. value = TRNG_DEF_STARTUP_CYCLES << TRNG_CNTL_REG_STARTUP_CYCLES_SHIFT;
  96. writel(value, &ks_sa_rng->reg_rng->control);
  97. value = (TRNG_DEF_MIN_REFILL_CYCLES <<
  98. TRNG_CFG_REG_MIN_REFILL_CYCLES_SHIFT) |
  99. (TRNG_DEF_MAX_REFILL_CYCLES <<
  100. TRNG_CFG_REG_MAX_REFILL_CYCLES_SHIFT) |
  101. (TRNG_DEF_CLK_DIV_CYCLES <<
  102. TRNG_CFG_REG_SAMPLE_DIV_SHIFT);
  103. writel(value, &ks_sa_rng->reg_rng->config);
  104. /* Disable all interrupts from TRNG */
  105. writel(0, &ks_sa_rng->reg_rng->intmask);
  106. /* Enable RNG */
  107. value = readl(&ks_sa_rng->reg_rng->control);
  108. value |= TRNG_CNTL_REG_TRNG_ENABLE;
  109. writel(value, &ks_sa_rng->reg_rng->control);
  110. return 0;
  111. }
  112. static void ks_sa_rng_cleanup(struct hwrng *rng)
  113. {
  114. struct device *dev = (struct device *)rng->priv;
  115. struct ks_sa_rng *ks_sa_rng = dev_get_drvdata(dev);
  116. /* Disable RNG */
  117. writel(0, &ks_sa_rng->reg_rng->control);
  118. regmap_write_bits(ks_sa_rng->regmap_cfg, SA_CMD_STATUS_OFS,
  119. SA_CMD_STATUS_REG_TRNG_ENABLE, 0);
  120. }
  121. static int ks_sa_rng_data_read(struct hwrng *rng, u32 *data)
  122. {
  123. struct device *dev = (struct device *)rng->priv;
  124. struct ks_sa_rng *ks_sa_rng = dev_get_drvdata(dev);
  125. /* Read random data */
  126. data[0] = readl(&ks_sa_rng->reg_rng->output_l);
  127. data[1] = readl(&ks_sa_rng->reg_rng->output_h);
  128. writel(TRNG_INTACK_REG_READY, &ks_sa_rng->reg_rng->intack);
  129. return sizeof(u32) * 2;
  130. }
  131. static int ks_sa_rng_data_present(struct hwrng *rng, int wait)
  132. {
  133. struct device *dev = (struct device *)rng->priv;
  134. struct ks_sa_rng *ks_sa_rng = dev_get_drvdata(dev);
  135. u32 ready;
  136. int j;
  137. for (j = 0; j < SA_MAX_RNG_DATA_RETRIES; j++) {
  138. ready = readl(&ks_sa_rng->reg_rng->status);
  139. ready &= TRNG_STATUS_REG_READY;
  140. if (ready || !wait)
  141. break;
  142. udelay(SA_RNG_DATA_RETRY_DELAY);
  143. }
  144. return ready;
  145. }
  146. static int ks_sa_rng_probe(struct platform_device *pdev)
  147. {
  148. struct ks_sa_rng *ks_sa_rng;
  149. struct device *dev = &pdev->dev;
  150. int ret;
  151. struct resource *mem;
  152. ks_sa_rng = devm_kzalloc(dev, sizeof(*ks_sa_rng), GFP_KERNEL);
  153. if (!ks_sa_rng)
  154. return -ENOMEM;
  155. ks_sa_rng->dev = dev;
  156. ks_sa_rng->rng = (struct hwrng) {
  157. .name = "ks_sa_hwrng",
  158. .init = ks_sa_rng_init,
  159. .data_read = ks_sa_rng_data_read,
  160. .data_present = ks_sa_rng_data_present,
  161. .cleanup = ks_sa_rng_cleanup,
  162. };
  163. ks_sa_rng->rng.priv = (unsigned long)dev;
  164. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  165. ks_sa_rng->reg_rng = devm_ioremap_resource(dev, mem);
  166. if (IS_ERR(ks_sa_rng->reg_rng))
  167. return PTR_ERR(ks_sa_rng->reg_rng);
  168. ks_sa_rng->regmap_cfg =
  169. syscon_regmap_lookup_by_phandle(dev->of_node,
  170. "ti,syscon-sa-cfg");
  171. if (IS_ERR(ks_sa_rng->regmap_cfg)) {
  172. dev_err(dev, "syscon_node_to_regmap failed\n");
  173. return -EINVAL;
  174. }
  175. pm_runtime_enable(dev);
  176. ret = pm_runtime_get_sync(dev);
  177. if (ret < 0) {
  178. dev_err(dev, "Failed to enable SA power-domain\n");
  179. pm_runtime_disable(dev);
  180. return ret;
  181. }
  182. platform_set_drvdata(pdev, ks_sa_rng);
  183. return devm_hwrng_register(&pdev->dev, &ks_sa_rng->rng);
  184. }
  185. static int ks_sa_rng_remove(struct platform_device *pdev)
  186. {
  187. pm_runtime_put_sync(&pdev->dev);
  188. pm_runtime_disable(&pdev->dev);
  189. return 0;
  190. }
  191. static const struct of_device_id ks_sa_rng_dt_match[] = {
  192. {
  193. .compatible = "ti,keystone-rng",
  194. },
  195. { },
  196. };
  197. MODULE_DEVICE_TABLE(of, ks_sa_rng_dt_match);
  198. static struct platform_driver ks_sa_rng_driver = {
  199. .driver = {
  200. .name = "ks-sa-rng",
  201. .of_match_table = ks_sa_rng_dt_match,
  202. },
  203. .probe = ks_sa_rng_probe,
  204. .remove = ks_sa_rng_remove,
  205. };
  206. module_platform_driver(ks_sa_rng_driver);
  207. MODULE_DESCRIPTION("Keystone NETCP SA H/W Random Number Generator driver");
  208. MODULE_AUTHOR("Vitaly Andrianov <vitalya@ti.com>");
  209. MODULE_LICENSE("GPL");