1/*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2021, Adrian Chadd <adrian@FreeBSD.org>
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice unmodified, this list of conditions, and the following
11 *    disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28/* Driver for Qualcomm MSM entropy device. */
29
30#include <sys/param.h>
31#include <sys/kernel.h>
32#include <sys/malloc.h>
33#include <sys/module.h>
34#include <sys/sglist.h>
35#include <sys/random.h>
36#include <sys/stdatomic.h>
37
38#include <machine/bus.h>
39#include <machine/resource.h>
40#include <sys/bus.h>
41
42#include <dev/fdt/fdt_common.h>
43#include <dev/ofw/ofw_bus.h>
44#include <dev/ofw/ofw_bus_subr.h>
45
46#include <dev/random/randomdev.h>
47#include <dev/random/random_harvestq.h>
48
49#include <dev/qcom_rnd/qcom_rnd_reg.h>
50
51struct qcom_rnd_softc {
52	device_t	 dev;
53	int		reg_rid;
54	struct resource	*reg;
55};
56
57static int	qcom_rnd_modevent(module_t, int, void *);
58
59static int	qcom_rnd_probe(device_t);
60static int	qcom_rnd_attach(device_t);
61static int	qcom_rnd_detach(device_t);
62
63static int	qcom_rnd_harvest(struct qcom_rnd_softc *, void *, size_t *);
64static unsigned	qcom_rnd_read(void *, unsigned);
65
66static struct random_source random_qcom_rnd = {
67	.rs_ident = "Qualcomm Entropy Adapter",
68	.rs_source = RANDOM_PURE_QUALCOMM,
69	.rs_read = qcom_rnd_read,
70};
71
72/* Kludge for API limitations of random(4). */
73static _Atomic(struct qcom_rnd_softc *) g_qcom_rnd_softc;
74
75static int
76qcom_rnd_modevent(module_t mod, int type, void *unused)
77{
78	int error;
79
80	switch (type) {
81	case MOD_LOAD:
82	case MOD_QUIESCE:
83	case MOD_UNLOAD:
84	case MOD_SHUTDOWN:
85		error = 0;
86		break;
87	default:
88		error = EOPNOTSUPP;
89		break;
90	}
91
92	return (error);
93}
94
95static int
96qcom_rnd_probe(device_t dev)
97{
98	if (! ofw_bus_status_okay(dev)) {
99		return (ENXIO);
100	}
101
102	if (ofw_bus_is_compatible(dev, "qcom,prng") == 0) {
103		return (ENXIO);
104	}
105
106	return (0);
107}
108
109static int
110qcom_rnd_attach(device_t dev)
111{
112	struct qcom_rnd_softc *sc, *exp;
113	uint32_t reg;
114
115	sc = device_get_softc(dev);
116
117
118	/* Found a compatible device! */
119
120	sc->dev = dev;
121
122	exp = NULL;
123	if (!atomic_compare_exchange_strong_explicit(&g_qcom_rnd_softc, &exp,
124	    sc, memory_order_release, memory_order_acquire)) {
125		return (ENXIO);
126	}
127
128	sc->reg_rid = 0;
129	sc->reg = bus_alloc_resource_anywhere(dev, SYS_RES_MEMORY,
130	    &sc->reg_rid, 0x140, RF_ACTIVE);
131	if (sc->reg == NULL) {
132		device_printf(dev, "Couldn't allocate memory resource!\n");
133		return (ENXIO);
134	}
135
136	device_set_desc(dev, "Qualcomm PRNG");
137
138	/*
139	 * Check to see whether the PRNG has already been setup or not.
140	 */
141	bus_barrier(sc->reg, 0, 0x120, BUS_SPACE_BARRIER_READ);
142	reg = bus_read_4(sc->reg, QCOM_RND_PRNG_CONFIG);
143	if (reg & QCOM_RND_PRNG_CONFIG_HW_ENABLE) {
144		device_printf(dev, "PRNG HW already enabled\n");
145	} else {
146		/*
147		 * Do PRNG setup and then enable it.
148		 */
149		reg = bus_read_4(sc->reg, QCOM_RND_PRNG_LFSR_CFG);
150		reg &= QCOM_RND_PRNG_LFSR_CFG_MASK;
151		reg |= QCOM_RND_PRNG_LFSR_CFG_CLOCKS;
152		bus_write_4(sc->reg, QCOM_RND_PRNG_LFSR_CFG, reg);
153		bus_barrier(sc->reg, 0, 0x120, BUS_SPACE_BARRIER_WRITE);
154
155		reg = bus_read_4(sc->reg, QCOM_RND_PRNG_CONFIG);
156		reg |= QCOM_RND_PRNG_CONFIG_HW_ENABLE;
157		bus_write_4(sc->reg, QCOM_RND_PRNG_CONFIG, reg);
158		bus_barrier(sc->reg, 0, 0x120, BUS_SPACE_BARRIER_WRITE);
159	}
160
161	random_source_register(&random_qcom_rnd);
162	return (0);
163
164}
165
166static int
167qcom_rnd_detach(device_t dev)
168{
169	struct qcom_rnd_softc *sc;
170
171	sc = device_get_softc(dev);
172	KASSERT(
173	    atomic_load_explicit(&g_qcom_rnd_softc, memory_order_acquire) == sc,
174	    ("only one global instance at a time"));
175
176	random_source_deregister(&random_qcom_rnd);
177	if (sc->reg != NULL) {
178		bus_release_resource(sc->dev, SYS_RES_MEMORY, sc->reg_rid, sc->reg);
179	}
180	atomic_store_explicit(&g_qcom_rnd_softc, NULL, memory_order_release);
181	return (0);
182}
183
184static int
185qcom_rnd_harvest(struct qcom_rnd_softc *sc, void *buf, size_t *sz)
186{
187	/*
188	 * Add data to buf until we either run out of entropy or we
189	 * fill the buffer.
190	 *
191	 * Note - be mindful of the provided buffer size; we're reading
192	 * 4 bytes at a time but we only want to supply up to the max
193	 * buffer size, so don't write past it!
194	 */
195	size_t rz = 0;
196	uint32_t reg;
197
198	while (rz < *sz) {
199		bus_barrier(sc->reg, 0, 0x120, BUS_SPACE_BARRIER_READ);
200		reg = bus_read_4(sc->reg, QCOM_RND_PRNG_STATUS);
201		if ((reg & QCOM_RND_PRNG_STATUS_DATA_AVAIL) == 0)
202			break;
203		reg = bus_read_4(sc->reg, QCOM_RND_PRNG_DATA_OUT);
204		memcpy(((char *) buf) + rz, &reg, sizeof(uint32_t));
205		rz += sizeof(uint32_t);
206	}
207
208	if (rz == 0)
209		return (EAGAIN);
210	*sz = rz;
211	return (0);
212}
213
214static unsigned
215qcom_rnd_read(void *buf, unsigned usz)
216{
217	struct qcom_rnd_softc *sc;
218	size_t sz;
219	int error;
220
221	sc = g_qcom_rnd_softc;
222	if (sc == NULL)
223		return (0);
224
225	sz = usz;
226	error = qcom_rnd_harvest(sc, buf, &sz);
227	if (error != 0)
228		return (0);
229
230	return (sz);
231}
232
233static device_method_t qcom_rnd_methods[] = {
234	/* Device methods. */
235	DEVMETHOD(device_probe,		qcom_rnd_probe),
236	DEVMETHOD(device_attach,	qcom_rnd_attach),
237	DEVMETHOD(device_detach,	qcom_rnd_detach),
238
239	DEVMETHOD_END
240};
241
242static driver_t qcom_rnd_driver = {
243	"qcom_rnd",
244	qcom_rnd_methods,
245	sizeof(struct qcom_rnd_softc)
246};
247
248DRIVER_MODULE(qcom_rnd_random, simplebus, qcom_rnd_driver,
249    qcom_rnd_modevent, 0);
250DRIVER_MODULE(qcom_rnd_random, ofwbus, qcom_rnd_driver,
251    qcom_rnd_modevent, 0);
252MODULE_DEPEND(qcom_rnd_random, random_device, 1, 1, 1);
253MODULE_VERSION(qcom_rnd_random, 1);
254