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/ofw/ofw_bus.h>
49#include <dev/ofw/ofw_bus_subr.h>
50
51
52struct aml8726_rng_softc {
53	device_t		dev;
54	struct resource		*res[1];
55	struct callout		co;
56	int			ticks;
57};
58
59static struct resource_spec aml8726_rng_spec[] = {
60	{ SYS_RES_MEMORY,	0,	RF_ACTIVE },
61	{ -1, 0 }
62};
63
64#define	AML_RNG_0_REG			0
65#define	AML_RNG_1_REG			4
66
67#define	CSR_READ_4(sc, reg)		bus_read_4((sc)->res[0], reg)
68
69static void
70aml8726_rng_harvest(void *arg)
71{
72	struct aml8726_rng_softc *sc = arg;
73	uint32_t rn[2];
74
75	rn[0] = CSR_READ_4(sc, AML_RNG_0_REG);
76	rn[1] = CSR_READ_4(sc, AML_RNG_1_REG);
77
78	random_harvest(rn, sizeof(rn), RANDOM_PURE_AML8726);
79
80	callout_reset(&sc->co, sc->ticks, aml8726_rng_harvest, sc);
81}
82
83static int
84aml8726_rng_probe(device_t dev)
85{
86
87	if (!ofw_bus_status_okay(dev))
88		return (ENXIO);
89
90	if (!ofw_bus_is_compatible(dev, "amlogic,aml8726-rng"))
91		return (ENXIO);
92
93	device_set_desc(dev, "Amlogic aml8726 RNG");
94
95	return (BUS_PROBE_DEFAULT);
96}
97
98static int
99aml8726_rng_attach(device_t dev)
100{
101	struct aml8726_rng_softc *sc = device_get_softc(dev);
102
103	sc->dev = dev;
104
105	if (bus_alloc_resources(dev, aml8726_rng_spec, sc->res)) {
106		device_printf(dev, "can not allocate resources for device\n");
107		return (ENXIO);
108	}
109
110	/* Install a periodic collector for the RNG */
111	if (hz > 100)
112		sc->ticks = hz / 100;
113	else
114		sc->ticks = 1;
115
116	callout_init(&sc->co, 1);
117	callout_reset(&sc->co, sc->ticks, aml8726_rng_harvest, sc);
118
119	return (0);
120}
121
122static int
123aml8726_rng_detach(device_t dev)
124{
125	struct aml8726_rng_softc *sc = device_get_softc(dev);
126
127	callout_drain(&sc->co);
128
129	bus_release_resources(dev, aml8726_rng_spec, sc->res);
130
131	return (0);
132}
133
134static 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),
139
140	DEVMETHOD_END
141};
142
143static driver_t aml8726_rng_driver = {
144	"rng",
145	aml8726_rng_methods,
146	sizeof(struct aml8726_rng_softc),
147};
148
149static devclass_t aml8726_rng_devclass;
150
151DRIVER_MODULE(aml8726_rng, simplebus, aml8726_rng_driver,
152    aml8726_rng_devclass, 0, 0);
153MODULE_DEPEND(aml8726_rng, random, 1, 1, 1);
154