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), sizeof(rn) * NBBY / 2,
81 callout_reset(&sc->co, sc->ticks, aml8726_rng_harvest, sc);
85 aml8726_rng_probe(device_t dev)
88 if (!ofw_bus_status_okay(dev))
91 if (!ofw_bus_is_compatible(dev, "amlogic,aml8726-rng"))
94 device_set_desc(dev, "Amlogic aml8726 RNG");
96 return (BUS_PROBE_DEFAULT);
100 aml8726_rng_attach(device_t dev)
102 struct aml8726_rng_softc *sc = device_get_softc(dev);
106 if (bus_alloc_resources(dev, aml8726_rng_spec, sc->res)) {
107 device_printf(dev, "can not allocate resources for device\n");
111 /* Install a periodic collector for the RNG */
113 sc->ticks = hz / 100;
117 callout_init(&sc->co, 1);
118 callout_reset(&sc->co, sc->ticks, aml8726_rng_harvest, sc);
124 aml8726_rng_detach(device_t dev)
126 struct aml8726_rng_softc *sc = device_get_softc(dev);
128 callout_drain(&sc->co);
130 bus_release_resources(dev, aml8726_rng_spec, sc->res);
135 static device_method_t aml8726_rng_methods[] = {
136 /* Device interface */
137 DEVMETHOD(device_probe, aml8726_rng_probe),
138 DEVMETHOD(device_attach, aml8726_rng_attach),
139 DEVMETHOD(device_detach, aml8726_rng_detach),
144 static driver_t aml8726_rng_driver = {
147 sizeof(struct aml8726_rng_softc),
150 static devclass_t aml8726_rng_devclass;
152 DRIVER_MODULE(aml8726_rng, simplebus, aml8726_rng_driver,
153 aml8726_rng_devclass, 0, 0);
154 MODULE_DEPEND(aml8726_rng, random, 1, 1, 1);