mtd: nand: make Open Firmware device id constant
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / char / hw_random / octeon-rng.c
blob54b0d9ba65cf4e74b7dfde417176cad5e7219f60
1 /*
2 * Hardware Random Number Generator support for Cavium Networks
3 * Octeon processor family.
5 * This file is subject to the terms and conditions of the GNU General Public
6 * License. See the file "COPYING" in the main directory of this archive
7 * for more details.
9 * Copyright (C) 2009 Cavium Networks
12 #include <linux/module.h>
13 #include <linux/init.h>
14 #include <linux/platform_device.h>
15 #include <linux/device.h>
16 #include <linux/hw_random.h>
17 #include <linux/io.h>
19 #include <asm/octeon/octeon.h>
20 #include <asm/octeon/cvmx-rnm-defs.h>
22 struct octeon_rng {
23 struct hwrng ops;
24 void __iomem *control_status;
25 void __iomem *result;
28 static int octeon_rng_init(struct hwrng *rng)
30 union cvmx_rnm_ctl_status ctl;
31 struct octeon_rng *p = container_of(rng, struct octeon_rng, ops);
33 ctl.u64 = 0;
34 ctl.s.ent_en = 1; /* Enable the entropy source. */
35 ctl.s.rng_en = 1; /* Enable the RNG hardware. */
36 cvmx_write_csr((u64)p->control_status, ctl.u64);
37 return 0;
40 static void octeon_rng_cleanup(struct hwrng *rng)
42 union cvmx_rnm_ctl_status ctl;
43 struct octeon_rng *p = container_of(rng, struct octeon_rng, ops);
45 ctl.u64 = 0;
46 /* Disable everything. */
47 cvmx_write_csr((u64)p->control_status, ctl.u64);
50 static int octeon_rng_data_read(struct hwrng *rng, u32 *data)
52 struct octeon_rng *p = container_of(rng, struct octeon_rng, ops);
54 *data = cvmx_read64_uint32((u64)p->result);
55 return sizeof(u32);
58 static int __devinit octeon_rng_probe(struct platform_device *pdev)
60 struct resource *res_ports;
61 struct resource *res_result;
62 struct octeon_rng *rng;
63 int ret;
64 struct hwrng ops = {
65 .name = "octeon",
66 .init = octeon_rng_init,
67 .cleanup = octeon_rng_cleanup,
68 .data_read = octeon_rng_data_read
71 rng = devm_kzalloc(&pdev->dev, sizeof(*rng), GFP_KERNEL);
72 if (!rng)
73 return -ENOMEM;
75 res_ports = platform_get_resource(pdev, IORESOURCE_MEM, 0);
76 if (!res_ports)
77 goto err_ports;
79 res_result = platform_get_resource(pdev, IORESOURCE_MEM, 1);
80 if (!res_result)
81 goto err_ports;
84 rng->control_status = devm_ioremap_nocache(&pdev->dev,
85 res_ports->start,
86 sizeof(u64));
87 if (!rng->control_status)
88 goto err_ports;
90 rng->result = devm_ioremap_nocache(&pdev->dev,
91 res_result->start,
92 sizeof(u64));
93 if (!rng->result)
94 goto err_r;
96 rng->ops = ops;
98 dev_set_drvdata(&pdev->dev, &rng->ops);
99 ret = hwrng_register(&rng->ops);
100 if (ret)
101 goto err;
103 dev_info(&pdev->dev, "Octeon Random Number Generator\n");
105 return 0;
106 err:
107 devm_iounmap(&pdev->dev, rng->control_status);
108 err_r:
109 devm_iounmap(&pdev->dev, rng->result);
110 err_ports:
111 devm_kfree(&pdev->dev, rng);
112 return -ENOENT;
115 static int __exit octeon_rng_remove(struct platform_device *pdev)
117 struct hwrng *rng = dev_get_drvdata(&pdev->dev);
119 hwrng_unregister(rng);
121 return 0;
124 static struct platform_driver octeon_rng_driver = {
125 .driver = {
126 .name = "octeon_rng",
127 .owner = THIS_MODULE,
129 .probe = octeon_rng_probe,
130 .remove = __exit_p(octeon_rng_remove),
133 static int __init octeon_rng_mod_init(void)
135 return platform_driver_register(&octeon_rng_driver);
138 static void __exit octeon_rng_mod_exit(void)
140 platform_driver_unregister(&octeon_rng_driver);
143 module_init(octeon_rng_mod_init);
144 module_exit(octeon_rng_mod_exit);
146 MODULE_AUTHOR("David Daney");
147 MODULE_LICENSE("GPL");