0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014 #include <linux/clk.h>
0015 #include <linux/delay.h>
0016 #include <linux/hw_random.h>
0017 #include <linux/io.h>
0018 #include <linux/module.h>
0019 #include <linux/of.h>
0020 #include <linux/platform_device.h>
0021
0022
0023 #define RNGA_CONTROL 0x00
0024 #define RNGA_STATUS 0x04
0025 #define RNGA_ENTROPY 0x08
0026 #define RNGA_OUTPUT_FIFO 0x0c
0027 #define RNGA_MODE 0x10
0028 #define RNGA_VERIFICATION_CONTROL 0x14
0029 #define RNGA_OSC_CONTROL_COUNTER 0x18
0030 #define RNGA_OSC1_COUNTER 0x1c
0031 #define RNGA_OSC2_COUNTER 0x20
0032 #define RNGA_OSC_COUNTER_STATUS 0x24
0033
0034
0035 #define RNG_ADDR_RANGE 0x28
0036
0037
0038 #define RNGA_CONTROL_SLEEP 0x00000010
0039 #define RNGA_CONTROL_CLEAR_INT 0x00000008
0040 #define RNGA_CONTROL_MASK_INTS 0x00000004
0041 #define RNGA_CONTROL_HIGH_ASSURANCE 0x00000002
0042 #define RNGA_CONTROL_GO 0x00000001
0043
0044 #define RNGA_STATUS_LEVEL_MASK 0x0000ff00
0045
0046
0047 #define RNGA_STATUS_OSC_DEAD 0x80000000
0048 #define RNGA_STATUS_SLEEP 0x00000010
0049 #define RNGA_STATUS_ERROR_INT 0x00000008
0050 #define RNGA_STATUS_FIFO_UNDERFLOW 0x00000004
0051 #define RNGA_STATUS_LAST_READ_STATUS 0x00000002
0052 #define RNGA_STATUS_SECURITY_VIOLATION 0x00000001
0053
0054 struct mxc_rng {
0055 struct device *dev;
0056 struct hwrng rng;
0057 void __iomem *mem;
0058 struct clk *clk;
0059 };
0060
0061 static int mxc_rnga_data_present(struct hwrng *rng, int wait)
0062 {
0063 int i;
0064 struct mxc_rng *mxc_rng = container_of(rng, struct mxc_rng, rng);
0065
0066 for (i = 0; i < 20; i++) {
0067
0068 int level = (__raw_readl(mxc_rng->mem + RNGA_STATUS) &
0069 RNGA_STATUS_LEVEL_MASK) >> 8;
0070 if (level || !wait)
0071 return !!level;
0072 udelay(10);
0073 }
0074 return 0;
0075 }
0076
0077 static int mxc_rnga_data_read(struct hwrng *rng, u32 * data)
0078 {
0079 int err;
0080 u32 ctrl;
0081 struct mxc_rng *mxc_rng = container_of(rng, struct mxc_rng, rng);
0082
0083
0084 *data = __raw_readl(mxc_rng->mem + RNGA_OUTPUT_FIFO);
0085
0086
0087 err = __raw_readl(mxc_rng->mem + RNGA_STATUS) & RNGA_STATUS_ERROR_INT;
0088
0089
0090 if (err) {
0091 dev_dbg(mxc_rng->dev, "Error while reading random number!\n");
0092 ctrl = __raw_readl(mxc_rng->mem + RNGA_CONTROL);
0093 __raw_writel(ctrl | RNGA_CONTROL_CLEAR_INT,
0094 mxc_rng->mem + RNGA_CONTROL);
0095 return 0;
0096 } else
0097 return 4;
0098 }
0099
0100 static int mxc_rnga_init(struct hwrng *rng)
0101 {
0102 u32 ctrl, osc;
0103 struct mxc_rng *mxc_rng = container_of(rng, struct mxc_rng, rng);
0104
0105
0106 ctrl = __raw_readl(mxc_rng->mem + RNGA_CONTROL);
0107 __raw_writel(ctrl & ~RNGA_CONTROL_SLEEP, mxc_rng->mem + RNGA_CONTROL);
0108
0109
0110 osc = __raw_readl(mxc_rng->mem + RNGA_STATUS);
0111 if (osc & RNGA_STATUS_OSC_DEAD) {
0112 dev_err(mxc_rng->dev, "RNGA Oscillator is dead!\n");
0113 return -ENODEV;
0114 }
0115
0116
0117 ctrl = __raw_readl(mxc_rng->mem + RNGA_CONTROL);
0118 __raw_writel(ctrl | RNGA_CONTROL_GO, mxc_rng->mem + RNGA_CONTROL);
0119
0120 return 0;
0121 }
0122
0123 static void mxc_rnga_cleanup(struct hwrng *rng)
0124 {
0125 u32 ctrl;
0126 struct mxc_rng *mxc_rng = container_of(rng, struct mxc_rng, rng);
0127
0128 ctrl = __raw_readl(mxc_rng->mem + RNGA_CONTROL);
0129
0130
0131 __raw_writel(ctrl & ~RNGA_CONTROL_GO, mxc_rng->mem + RNGA_CONTROL);
0132 }
0133
0134 static int __init mxc_rnga_probe(struct platform_device *pdev)
0135 {
0136 int err;
0137 struct mxc_rng *mxc_rng;
0138
0139 mxc_rng = devm_kzalloc(&pdev->dev, sizeof(*mxc_rng), GFP_KERNEL);
0140 if (!mxc_rng)
0141 return -ENOMEM;
0142
0143 mxc_rng->dev = &pdev->dev;
0144 mxc_rng->rng.name = "mxc-rnga";
0145 mxc_rng->rng.init = mxc_rnga_init;
0146 mxc_rng->rng.cleanup = mxc_rnga_cleanup;
0147 mxc_rng->rng.data_present = mxc_rnga_data_present;
0148 mxc_rng->rng.data_read = mxc_rnga_data_read;
0149
0150 mxc_rng->clk = devm_clk_get(&pdev->dev, NULL);
0151 if (IS_ERR(mxc_rng->clk)) {
0152 dev_err(&pdev->dev, "Could not get rng_clk!\n");
0153 return PTR_ERR(mxc_rng->clk);
0154 }
0155
0156 err = clk_prepare_enable(mxc_rng->clk);
0157 if (err)
0158 return err;
0159
0160 mxc_rng->mem = devm_platform_ioremap_resource(pdev, 0);
0161 if (IS_ERR(mxc_rng->mem)) {
0162 err = PTR_ERR(mxc_rng->mem);
0163 goto err_ioremap;
0164 }
0165
0166 err = hwrng_register(&mxc_rng->rng);
0167 if (err) {
0168 dev_err(&pdev->dev, "MXC RNGA registering failed (%d)\n", err);
0169 goto err_ioremap;
0170 }
0171
0172 return 0;
0173
0174 err_ioremap:
0175 clk_disable_unprepare(mxc_rng->clk);
0176 return err;
0177 }
0178
0179 static int __exit mxc_rnga_remove(struct platform_device *pdev)
0180 {
0181 struct mxc_rng *mxc_rng = platform_get_drvdata(pdev);
0182
0183 hwrng_unregister(&mxc_rng->rng);
0184
0185 clk_disable_unprepare(mxc_rng->clk);
0186
0187 return 0;
0188 }
0189
0190 static const struct of_device_id mxc_rnga_of_match[] = {
0191 { .compatible = "fsl,imx21-rnga", },
0192 { .compatible = "fsl,imx31-rnga", },
0193 { },
0194 };
0195 MODULE_DEVICE_TABLE(of, mxc_rnga_of_match);
0196
0197 static struct platform_driver mxc_rnga_driver = {
0198 .driver = {
0199 .name = "mxc_rnga",
0200 .of_match_table = mxc_rnga_of_match,
0201 },
0202 .remove = __exit_p(mxc_rnga_remove),
0203 };
0204
0205 module_platform_driver_probe(mxc_rnga_driver, mxc_rnga_probe);
0206
0207 MODULE_AUTHOR("Freescale Semiconductor, Inc.");
0208 MODULE_DESCRIPTION("H/W RNGA driver for i.MX");
0209 MODULE_LICENSE("GPL");