xref: /NextBSD/sys/arm/amlogic/aml8726/aml8726_rng.c (revision 287e3b14e9552995def1802ec9c5034f4adf28ec)
1 /*-
2  * Copyright 2014 John Wehle <john@feith.com>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 /*
28  * Amlogic aml8726 random number generator driver.
29  */
30 
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
33 
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/conf.h>
37 #include <sys/bus.h>
38 #include <sys/kernel.h>
39 #include <sys/module.h>
40 #include <sys/lock.h>
41 #include <sys/mutex.h>
42 #include <sys/resource.h>
43 #include <sys/rman.h>
44 #include <sys/random.h>
45 
46 #include <machine/bus.h>
47 
48 #include <dev/fdt/fdt_common.h>
49 #include <dev/ofw/ofw_bus.h>
50 #include <dev/ofw/ofw_bus_subr.h>
51 
52 
53 struct aml8726_rng_softc {
54 	device_t		dev;
55 	struct resource		*res[1];
56 	struct callout		co;
57 	int			ticks;
58 };
59 
60 static struct resource_spec aml8726_rng_spec[] = {
61 	{ SYS_RES_MEMORY,	0,	RF_ACTIVE },
62 	{ -1, 0 }
63 };
64 
65 #define	AML_RNG_0_REG			0
66 #define	AML_RNG_1_REG			4
67 
68 #define	CSR_READ_4(sc, reg)		bus_read_4((sc)->res[0], reg)
69 
70 static void
aml8726_rng_harvest(void * arg)71 aml8726_rng_harvest(void *arg)
72 {
73 	struct aml8726_rng_softc *sc = arg;
74 	uint32_t rn[2];
75 
76 	rn[0] = CSR_READ_4(sc, AML_RNG_0_REG);
77 	rn[1] = CSR_READ_4(sc, AML_RNG_1_REG);
78 
79 	random_harvest(rn, sizeof(rn), sizeof(rn) * NBBY / 2,
80 	    RANDOM_PURE_AML8726);
81 
82 	callout_reset(&sc->co, sc->ticks, aml8726_rng_harvest, sc);
83 }
84 
85 static int
aml8726_rng_probe(device_t dev)86 aml8726_rng_probe(device_t dev)
87 {
88 
89 	if (!ofw_bus_status_okay(dev))
90 		return (ENXIO);
91 
92 	if (!ofw_bus_is_compatible(dev, "amlogic,aml8726-rng"))
93 		return (ENXIO);
94 
95 	device_set_desc(dev, "Amlogic aml8726 RNG");
96 
97 	return (BUS_PROBE_DEFAULT);
98 }
99 
100 static int
aml8726_rng_attach(device_t dev)101 aml8726_rng_attach(device_t dev)
102 {
103 	struct aml8726_rng_softc *sc = device_get_softc(dev);
104 
105 	sc->dev = dev;
106 
107 	if (bus_alloc_resources(dev, aml8726_rng_spec, sc->res)) {
108 		device_printf(dev, "can not allocate resources for device\n");
109 		return (ENXIO);
110 	}
111 
112 	/* Install a periodic collector for the RNG */
113 	if (hz > 100)
114 		sc->ticks = hz / 100;
115 	else
116 		sc->ticks = 1;
117 
118 	callout_init(&sc->co, 1);
119 	callout_reset(&sc->co, sc->ticks, aml8726_rng_harvest, sc);
120 
121 	return (0);
122 }
123 
124 static int
aml8726_rng_detach(device_t dev)125 aml8726_rng_detach(device_t dev)
126 {
127 	struct aml8726_rng_softc *sc = device_get_softc(dev);
128 
129 	callout_drain(&sc->co);
130 
131 	bus_release_resources(dev, aml8726_rng_spec, sc->res);
132 
133 	return (0);
134 }
135 
136 static device_method_t aml8726_rng_methods[] = {
137 	/* Device interface */
138 	DEVMETHOD(device_probe,		aml8726_rng_probe),
139 	DEVMETHOD(device_attach,	aml8726_rng_attach),
140 	DEVMETHOD(device_detach,	aml8726_rng_detach),
141 
142 	DEVMETHOD_END
143 };
144 
145 static driver_t aml8726_rng_driver = {
146 	"rng",
147 	aml8726_rng_methods,
148 	sizeof(struct aml8726_rng_softc),
149 };
150 
151 static devclass_t aml8726_rng_devclass;
152 
153 DRIVER_MODULE(aml8726_rng, simplebus, aml8726_rng_driver,
154     aml8726_rng_devclass, 0, 0);
155 MODULE_DEPEND(aml8726_rng, random, 1, 1, 1);
156