1 /*
2  * Hardware Random Number Generator support for Cavium Networks
3  * Octeon processor family.
4  *
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.
8  *
9  * Copyright (C) 2009 Cavium Networks
10  */
11 
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>
18 #include <linux/gfp.h>
19 
20 #include <asm/octeon/octeon.h>
21 #include <asm/octeon/cvmx-rnm-defs.h>
22 
23 struct octeon_rng {
24 	struct hwrng ops;
25 	void __iomem *control_status;
26 	void __iomem *result;
27 };
28 
octeon_rng_init(struct hwrng * rng)29 static int octeon_rng_init(struct hwrng *rng)
30 {
31 	union cvmx_rnm_ctl_status ctl;
32 	struct octeon_rng *p = container_of(rng, struct octeon_rng, ops);
33 
34 	ctl.u64 = 0;
35 	ctl.s.ent_en = 1; /* Enable the entropy source.  */
36 	ctl.s.rng_en = 1; /* Enable the RNG hardware.  */
37 	cvmx_write_csr((u64)p->control_status, ctl.u64);
38 	return 0;
39 }
40 
octeon_rng_cleanup(struct hwrng * rng)41 static void octeon_rng_cleanup(struct hwrng *rng)
42 {
43 	union cvmx_rnm_ctl_status ctl;
44 	struct octeon_rng *p = container_of(rng, struct octeon_rng, ops);
45 
46 	ctl.u64 = 0;
47 	/* Disable everything.  */
48 	cvmx_write_csr((u64)p->control_status, ctl.u64);
49 }
50 
octeon_rng_data_read(struct hwrng * rng,u32 * data)51 static int octeon_rng_data_read(struct hwrng *rng, u32 *data)
52 {
53 	struct octeon_rng *p = container_of(rng, struct octeon_rng, ops);
54 
55 	*data = cvmx_read64_uint32((u64)p->result);
56 	return sizeof(u32);
57 }
58 
octeon_rng_probe(struct platform_device * pdev)59 static int __devinit octeon_rng_probe(struct platform_device *pdev)
60 {
61 	struct resource *res_ports;
62 	struct resource *res_result;
63 	struct octeon_rng *rng;
64 	int ret;
65 	struct hwrng ops = {
66 		.name = "octeon",
67 		.init = octeon_rng_init,
68 		.cleanup = octeon_rng_cleanup,
69 		.data_read = octeon_rng_data_read
70 	};
71 
72 	rng = devm_kzalloc(&pdev->dev, sizeof(*rng), GFP_KERNEL);
73 	if (!rng)
74 		return -ENOMEM;
75 
76 	res_ports = platform_get_resource(pdev, IORESOURCE_MEM, 0);
77 	if (!res_ports)
78 		goto err_ports;
79 
80 	res_result = platform_get_resource(pdev, IORESOURCE_MEM, 1);
81 	if (!res_result)
82 		goto err_ports;
83 
84 
85 	rng->control_status = devm_ioremap_nocache(&pdev->dev,
86 						   res_ports->start,
87 						   sizeof(u64));
88 	if (!rng->control_status)
89 		goto err_ports;
90 
91 	rng->result = devm_ioremap_nocache(&pdev->dev,
92 					   res_result->start,
93 					   sizeof(u64));
94 	if (!rng->result)
95 		goto err_r;
96 
97 	rng->ops = ops;
98 
99 	dev_set_drvdata(&pdev->dev, &rng->ops);
100 	ret = hwrng_register(&rng->ops);
101 	if (ret)
102 		goto err;
103 
104 	dev_info(&pdev->dev, "Octeon Random Number Generator\n");
105 
106 	return 0;
107 err:
108 	devm_iounmap(&pdev->dev, rng->control_status);
109 err_r:
110 	devm_iounmap(&pdev->dev, rng->result);
111 err_ports:
112 	devm_kfree(&pdev->dev, rng);
113 	return -ENOENT;
114 }
115 
octeon_rng_remove(struct platform_device * pdev)116 static int __exit octeon_rng_remove(struct platform_device *pdev)
117 {
118 	struct hwrng *rng = dev_get_drvdata(&pdev->dev);
119 
120 	hwrng_unregister(rng);
121 
122 	return 0;
123 }
124 
125 static struct platform_driver octeon_rng_driver = {
126 	.driver = {
127 		.name		= "octeon_rng",
128 		.owner		= THIS_MODULE,
129 	},
130 	.probe		= octeon_rng_probe,
131 	.remove		= __exit_p(octeon_rng_remove),
132 };
133 
octeon_rng_mod_init(void)134 static int __init octeon_rng_mod_init(void)
135 {
136 	return platform_driver_register(&octeon_rng_driver);
137 }
138 
octeon_rng_mod_exit(void)139 static void __exit octeon_rng_mod_exit(void)
140 {
141 	platform_driver_unregister(&octeon_rng_driver);
142 }
143 
144 module_init(octeon_rng_mod_init);
145 module_exit(octeon_rng_mod_exit);
146 
147 MODULE_AUTHOR("David Daney");
148 MODULE_LICENSE("GPL");
149