2 * Copyright 2014 John Wehle <john@feith.com>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
28 * Amlogic aml8726 random number generator driver.
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
34 #include <sys/param.h>
35 #include <sys/systm.h>
38 #include <sys/kernel.h>
39 #include <sys/module.h>
41 #include <sys/mutex.h>
42 #include <sys/resource.h>
44 #include <sys/random.h>
46 #include <machine/bus.h>
48 #include <dev/ofw/ofw_bus.h>
49 #include <dev/ofw/ofw_bus_subr.h>
52 struct aml8726_rng_softc {
54 struct resource *res[1];
59 static struct resource_spec aml8726_rng_spec[] = {
60 { SYS_RES_MEMORY, 0, RF_ACTIVE },
64 #define AML_RNG_0_REG 0
65 #define AML_RNG_1_REG 4
67 #define CSR_READ_4(sc, reg) bus_read_4((sc)->res[0], reg)
70 aml8726_rng_harvest(void *arg)
72 struct aml8726_rng_softc *sc = arg;
75 rn[0] = CSR_READ_4(sc, AML_RNG_0_REG);
76 rn[1] = CSR_READ_4(sc, AML_RNG_1_REG);
78 random_harvest(rn, sizeof(rn), RANDOM_PURE_AML8726);
80 callout_reset(&sc->co, sc->ticks, aml8726_rng_harvest, sc);
84 aml8726_rng_probe(device_t dev)
87 if (!ofw_bus_status_okay(dev))
90 if (!ofw_bus_is_compatible(dev, "amlogic,aml8726-rng"))
93 device_set_desc(dev, "Amlogic aml8726 RNG");
95 return (BUS_PROBE_DEFAULT);
99 aml8726_rng_attach(device_t dev)
101 struct aml8726_rng_softc *sc = device_get_softc(dev);
105 if (bus_alloc_resources(dev, aml8726_rng_spec, sc->res)) {
106 device_printf(dev, "can not allocate resources for device\n");
110 /* Install a periodic collector for the RNG */
112 sc->ticks = hz / 100;
116 callout_init(&sc->co, 1);
117 callout_reset(&sc->co, sc->ticks, aml8726_rng_harvest, sc);
123 aml8726_rng_detach(device_t dev)
125 struct aml8726_rng_softc *sc = device_get_softc(dev);
127 callout_drain(&sc->co);
129 bus_release_resources(dev, aml8726_rng_spec, sc->res);
134 static device_method_t aml8726_rng_methods[] = {
135 /* Device interface */
136 DEVMETHOD(device_probe, aml8726_rng_probe),
137 DEVMETHOD(device_attach, aml8726_rng_attach),
138 DEVMETHOD(device_detach, aml8726_rng_detach),
143 static driver_t aml8726_rng_driver = {
146 sizeof(struct aml8726_rng_softc),
149 static devclass_t aml8726_rng_devclass;
151 DRIVER_MODULE(aml8726_rng, simplebus, aml8726_rng_driver,
152 aml8726_rng_devclass, 0, 0);
153 MODULE_DEPEND(aml8726_rng, random, 1, 1, 1);