0014-Add-hwrng-hardware-random-number-generator-driver.patch 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. From 225e1250dfcec7b09493b6a86bdeaab9f2669221 Mon Sep 17 00:00:00 2001
  2. From: popcornmix <popcornmix@gmail.com>
  3. Date: Wed, 3 Jul 2013 00:51:55 +0100
  4. Subject: [PATCH 014/114] Add hwrng (hardware random number generator) driver
  5. ---
  6. drivers/char/hw_random/Kconfig | 11 ++++
  7. drivers/char/hw_random/Makefile | 1 +
  8. drivers/char/hw_random/bcm2708-rng.c | 118 +++++++++++++++++++++++++++++++++++
  9. 3 files changed, 130 insertions(+)
  10. create mode 100755 drivers/char/hw_random/bcm2708-rng.c
  11. --- a/drivers/char/hw_random/Kconfig
  12. +++ b/drivers/char/hw_random/Kconfig
  13. @@ -320,6 +320,17 @@ config HW_RANDOM_TPM
  14. If unsure, say Y.
  15. +config HW_RANDOM_BCM2708
  16. + tristate "BCM2708 generic true random number generator support"
  17. + depends on HW_RANDOM && ARCH_BCM2708
  18. + ---help---
  19. + This driver provides the kernel-side support for the BCM2708 hardware.
  20. +
  21. + To compile this driver as a module, choose M here: the
  22. + module will be called bcm2708-rng.
  23. +
  24. + If unsure, say N.
  25. +
  26. config HW_RANDOM_MSM
  27. tristate "Qualcomm SoCs Random Number Generator support"
  28. depends on HW_RANDOM && ARCH_QCOM
  29. --- a/drivers/char/hw_random/Makefile
  30. +++ b/drivers/char/hw_random/Makefile
  31. @@ -28,5 +28,6 @@ obj-$(CONFIG_HW_RANDOM_POWERNV) += power
  32. obj-$(CONFIG_HW_RANDOM_EXYNOS) += exynos-rng.o
  33. obj-$(CONFIG_HW_RANDOM_TPM) += tpm-rng.o
  34. obj-$(CONFIG_HW_RANDOM_BCM2835) += bcm2835-rng.o
  35. +obj-$(CONFIG_HW_RANDOM_BCM2708) += bcm2708-rng.o
  36. obj-$(CONFIG_HW_RANDOM_MSM) += msm-rng.o
  37. obj-$(CONFIG_HW_RANDOM_XGENE) += xgene-rng.o
  38. --- /dev/null
  39. +++ b/drivers/char/hw_random/bcm2708-rng.c
  40. @@ -0,0 +1,118 @@
  41. +/**
  42. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  43. + *
  44. + * Redistribution and use in source and binary forms, with or without
  45. + * modification, are permitted provided that the following conditions
  46. + * are met:
  47. + * 1. Redistributions of source code must retain the above copyright
  48. + * notice, this list of conditions, and the following disclaimer,
  49. + * without modification.
  50. + * 2. Redistributions in binary form must reproduce the above copyright
  51. + * notice, this list of conditions and the following disclaimer in the
  52. + * documentation and/or other materials provided with the distribution.
  53. + * 3. The names of the above-listed copyright holders may not be used
  54. + * to endorse or promote products derived from this software without
  55. + * specific prior written permission.
  56. + *
  57. + * ALTERNATIVELY, this software may be distributed under the terms of the
  58. + * GNU General Public License ("GPL") version 2, as published by the Free
  59. + * Software Foundation.
  60. + *
  61. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  62. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  63. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  64. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  65. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  66. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  67. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  68. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  69. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  70. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  71. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  72. + */
  73. +
  74. +#include <linux/kernel.h>
  75. +#include <linux/module.h>
  76. +#include <linux/init.h>
  77. +#include <linux/hw_random.h>
  78. +#include <linux/printk.h>
  79. +
  80. +#include <asm/io.h>
  81. +#include <mach/hardware.h>
  82. +#include <mach/platform.h>
  83. +
  84. +#define RNG_CTRL (0x0)
  85. +#define RNG_STATUS (0x4)
  86. +#define RNG_DATA (0x8)
  87. +#define RNG_FF_THRESHOLD (0xc)
  88. +
  89. +/* enable rng */
  90. +#define RNG_RBGEN 0x1
  91. +/* double speed, less random mode */
  92. +#define RNG_RBG2X 0x2
  93. +
  94. +/* the initial numbers generated are "less random" so will be discarded */
  95. +#define RNG_WARMUP_COUNT 0x40000
  96. +
  97. +static int bcm2708_rng_data_read(struct hwrng *rng, u32 *buffer)
  98. +{
  99. + void __iomem *rng_base = (void __iomem *)rng->priv;
  100. + unsigned words;
  101. + /* wait for a random number to be in fifo */
  102. + do {
  103. + words = __raw_readl(rng_base + RNG_STATUS)>>24;
  104. + }
  105. + while (words == 0);
  106. + /* read the random number */
  107. + *buffer = __raw_readl(rng_base + RNG_DATA);
  108. + return 4;
  109. +}
  110. +
  111. +static struct hwrng bcm2708_rng_ops = {
  112. + .name = "bcm2708",
  113. + .data_read = bcm2708_rng_data_read,
  114. +};
  115. +
  116. +static int __init bcm2708_rng_init(void)
  117. +{
  118. + void __iomem *rng_base;
  119. + int err;
  120. +
  121. + /* map peripheral */
  122. + rng_base = ioremap(RNG_BASE, 0x10);
  123. + pr_info("bcm2708_rng_init=%p\n", rng_base);
  124. + if (!rng_base) {
  125. + pr_err("bcm2708_rng_init failed to ioremap\n");
  126. + return -ENOMEM;
  127. + }
  128. + bcm2708_rng_ops.priv = (unsigned long)rng_base;
  129. +
  130. + /* set warm-up count & enable */
  131. + __raw_writel(RNG_WARMUP_COUNT, rng_base + RNG_STATUS);
  132. + __raw_writel(RNG_RBGEN, rng_base + RNG_CTRL);
  133. +
  134. + /* register driver */
  135. + err = hwrng_register(&bcm2708_rng_ops);
  136. + if (err) {
  137. + pr_err("bcm2708_rng_init hwrng_register()=%d\n", err);
  138. + iounmap(rng_base);
  139. + }
  140. + return err;
  141. +}
  142. +
  143. +static void __exit bcm2708_rng_exit(void)
  144. +{
  145. + void __iomem *rng_base = (void __iomem *)bcm2708_rng_ops.priv;
  146. + pr_info("bcm2708_rng_exit\n");
  147. + /* disable rng hardware */
  148. + __raw_writel(0, rng_base + RNG_CTRL);
  149. + /* unregister driver */
  150. + hwrng_unregister(&bcm2708_rng_ops);
  151. + iounmap(rng_base);
  152. +}
  153. +
  154. +module_init(bcm2708_rng_init);
  155. +module_exit(bcm2708_rng_exit);
  156. +
  157. +MODULE_DESCRIPTION("BCM2708 H/W Random Number Generator (RNG) driver");
  158. +MODULE_LICENSE("GPL and additional rights");