11492c8c0SAdrian Chadd /*-
2*4d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
31492c8c0SAdrian Chadd *
41492c8c0SAdrian Chadd * Copyright (c) 2021, Adrian Chadd <adrian@FreeBSD.org>
51492c8c0SAdrian Chadd *
61492c8c0SAdrian Chadd * Redistribution and use in source and binary forms, with or without
71492c8c0SAdrian Chadd * modification, are permitted provided that the following conditions
81492c8c0SAdrian Chadd * are met:
91492c8c0SAdrian Chadd * 1. Redistributions of source code must retain the above copyright
101492c8c0SAdrian Chadd * notice unmodified, this list of conditions, and the following
111492c8c0SAdrian Chadd * disclaimer.
121492c8c0SAdrian Chadd * 2. Redistributions in binary form must reproduce the above copyright
131492c8c0SAdrian Chadd * notice, this list of conditions and the following disclaimer in the
141492c8c0SAdrian Chadd * documentation and/or other materials provided with the distribution.
151492c8c0SAdrian Chadd *
161492c8c0SAdrian Chadd * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
171492c8c0SAdrian Chadd * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
181492c8c0SAdrian Chadd * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
191492c8c0SAdrian Chadd * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
201492c8c0SAdrian Chadd * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
211492c8c0SAdrian Chadd * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
221492c8c0SAdrian Chadd * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
231492c8c0SAdrian Chadd * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
241492c8c0SAdrian Chadd * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
251492c8c0SAdrian Chadd * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
261492c8c0SAdrian Chadd */
271492c8c0SAdrian Chadd
281492c8c0SAdrian Chadd /* Driver for Qualcomm MSM entropy device. */
291492c8c0SAdrian Chadd
301492c8c0SAdrian Chadd #include <sys/param.h>
311492c8c0SAdrian Chadd #include <sys/kernel.h>
321492c8c0SAdrian Chadd #include <sys/malloc.h>
331492c8c0SAdrian Chadd #include <sys/module.h>
341492c8c0SAdrian Chadd #include <sys/sglist.h>
351492c8c0SAdrian Chadd #include <sys/random.h>
361492c8c0SAdrian Chadd #include <sys/stdatomic.h>
371492c8c0SAdrian Chadd
381492c8c0SAdrian Chadd #include <machine/bus.h>
391492c8c0SAdrian Chadd #include <machine/resource.h>
401492c8c0SAdrian Chadd #include <sys/bus.h>
411492c8c0SAdrian Chadd
421492c8c0SAdrian Chadd #include <dev/fdt/fdt_common.h>
431492c8c0SAdrian Chadd #include <dev/ofw/ofw_bus.h>
441492c8c0SAdrian Chadd #include <dev/ofw/ofw_bus_subr.h>
451492c8c0SAdrian Chadd
461492c8c0SAdrian Chadd #include <dev/random/randomdev.h>
471492c8c0SAdrian Chadd #include <dev/random/random_harvestq.h>
481492c8c0SAdrian Chadd
491492c8c0SAdrian Chadd #include <dev/qcom_rnd/qcom_rnd_reg.h>
501492c8c0SAdrian Chadd
511492c8c0SAdrian Chadd struct qcom_rnd_softc {
521492c8c0SAdrian Chadd device_t dev;
531492c8c0SAdrian Chadd int reg_rid;
541492c8c0SAdrian Chadd struct resource *reg;
551492c8c0SAdrian Chadd };
561492c8c0SAdrian Chadd
571492c8c0SAdrian Chadd static int qcom_rnd_modevent(module_t, int, void *);
581492c8c0SAdrian Chadd
591492c8c0SAdrian Chadd static int qcom_rnd_probe(device_t);
601492c8c0SAdrian Chadd static int qcom_rnd_attach(device_t);
611492c8c0SAdrian Chadd static int qcom_rnd_detach(device_t);
621492c8c0SAdrian Chadd
631492c8c0SAdrian Chadd static int qcom_rnd_harvest(struct qcom_rnd_softc *, void *, size_t *);
641492c8c0SAdrian Chadd static unsigned qcom_rnd_read(void *, unsigned);
651492c8c0SAdrian Chadd
661492c8c0SAdrian Chadd static struct random_source random_qcom_rnd = {
671492c8c0SAdrian Chadd .rs_ident = "Qualcomm Entropy Adapter",
681492c8c0SAdrian Chadd .rs_source = RANDOM_PURE_QUALCOMM,
691492c8c0SAdrian Chadd .rs_read = qcom_rnd_read,
701492c8c0SAdrian Chadd };
711492c8c0SAdrian Chadd
721492c8c0SAdrian Chadd /* Kludge for API limitations of random(4). */
731492c8c0SAdrian Chadd static _Atomic(struct qcom_rnd_softc *) g_qcom_rnd_softc;
741492c8c0SAdrian Chadd
751492c8c0SAdrian Chadd static int
qcom_rnd_modevent(module_t mod,int type,void * unused)761492c8c0SAdrian Chadd qcom_rnd_modevent(module_t mod, int type, void *unused)
771492c8c0SAdrian Chadd {
781492c8c0SAdrian Chadd int error;
791492c8c0SAdrian Chadd
801492c8c0SAdrian Chadd switch (type) {
811492c8c0SAdrian Chadd case MOD_LOAD:
821492c8c0SAdrian Chadd case MOD_QUIESCE:
831492c8c0SAdrian Chadd case MOD_UNLOAD:
841492c8c0SAdrian Chadd case MOD_SHUTDOWN:
851492c8c0SAdrian Chadd error = 0;
861492c8c0SAdrian Chadd break;
871492c8c0SAdrian Chadd default:
881492c8c0SAdrian Chadd error = EOPNOTSUPP;
891492c8c0SAdrian Chadd break;
901492c8c0SAdrian Chadd }
911492c8c0SAdrian Chadd
921492c8c0SAdrian Chadd return (error);
931492c8c0SAdrian Chadd }
941492c8c0SAdrian Chadd
951492c8c0SAdrian Chadd static int
qcom_rnd_probe(device_t dev)961492c8c0SAdrian Chadd qcom_rnd_probe(device_t dev)
971492c8c0SAdrian Chadd {
981492c8c0SAdrian Chadd if (! ofw_bus_status_okay(dev)) {
991492c8c0SAdrian Chadd return (ENXIO);
1001492c8c0SAdrian Chadd }
1011492c8c0SAdrian Chadd
1021492c8c0SAdrian Chadd if (ofw_bus_is_compatible(dev, "qcom,prng") == 0) {
1031492c8c0SAdrian Chadd return (ENXIO);
1041492c8c0SAdrian Chadd }
1051492c8c0SAdrian Chadd
1061492c8c0SAdrian Chadd return (0);
1071492c8c0SAdrian Chadd }
1081492c8c0SAdrian Chadd
1091492c8c0SAdrian Chadd static int
qcom_rnd_attach(device_t dev)1101492c8c0SAdrian Chadd qcom_rnd_attach(device_t dev)
1111492c8c0SAdrian Chadd {
1121492c8c0SAdrian Chadd struct qcom_rnd_softc *sc, *exp;
1131492c8c0SAdrian Chadd uint32_t reg;
1141492c8c0SAdrian Chadd
1151492c8c0SAdrian Chadd sc = device_get_softc(dev);
1161492c8c0SAdrian Chadd
1171492c8c0SAdrian Chadd
1181492c8c0SAdrian Chadd /* Found a compatible device! */
1191492c8c0SAdrian Chadd
1201492c8c0SAdrian Chadd sc->dev = dev;
1211492c8c0SAdrian Chadd
1221492c8c0SAdrian Chadd exp = NULL;
1231492c8c0SAdrian Chadd if (!atomic_compare_exchange_strong_explicit(&g_qcom_rnd_softc, &exp,
1241492c8c0SAdrian Chadd sc, memory_order_release, memory_order_acquire)) {
1251492c8c0SAdrian Chadd return (ENXIO);
1261492c8c0SAdrian Chadd }
1271492c8c0SAdrian Chadd
1281492c8c0SAdrian Chadd sc->reg_rid = 0;
1291492c8c0SAdrian Chadd sc->reg = bus_alloc_resource_anywhere(dev, SYS_RES_MEMORY,
1301492c8c0SAdrian Chadd &sc->reg_rid, 0x140, RF_ACTIVE);
1311492c8c0SAdrian Chadd if (sc->reg == NULL) {
1321492c8c0SAdrian Chadd device_printf(dev, "Couldn't allocate memory resource!\n");
1331492c8c0SAdrian Chadd return (ENXIO);
1341492c8c0SAdrian Chadd }
1351492c8c0SAdrian Chadd
1361492c8c0SAdrian Chadd device_set_desc(dev, "Qualcomm PRNG");
1371492c8c0SAdrian Chadd
1381492c8c0SAdrian Chadd /*
1391492c8c0SAdrian Chadd * Check to see whether the PRNG has already been setup or not.
1401492c8c0SAdrian Chadd */
1411492c8c0SAdrian Chadd bus_barrier(sc->reg, 0, 0x120, BUS_SPACE_BARRIER_READ);
1421492c8c0SAdrian Chadd reg = bus_read_4(sc->reg, QCOM_RND_PRNG_CONFIG);
1431492c8c0SAdrian Chadd if (reg & QCOM_RND_PRNG_CONFIG_HW_ENABLE) {
1441492c8c0SAdrian Chadd device_printf(dev, "PRNG HW already enabled\n");
1451492c8c0SAdrian Chadd } else {
1461492c8c0SAdrian Chadd /*
1471492c8c0SAdrian Chadd * Do PRNG setup and then enable it.
1481492c8c0SAdrian Chadd */
1491492c8c0SAdrian Chadd reg = bus_read_4(sc->reg, QCOM_RND_PRNG_LFSR_CFG);
1501492c8c0SAdrian Chadd reg &= QCOM_RND_PRNG_LFSR_CFG_MASK;
1511492c8c0SAdrian Chadd reg |= QCOM_RND_PRNG_LFSR_CFG_CLOCKS;
1521492c8c0SAdrian Chadd bus_write_4(sc->reg, QCOM_RND_PRNG_LFSR_CFG, reg);
1531492c8c0SAdrian Chadd bus_barrier(sc->reg, 0, 0x120, BUS_SPACE_BARRIER_WRITE);
1541492c8c0SAdrian Chadd
1551492c8c0SAdrian Chadd reg = bus_read_4(sc->reg, QCOM_RND_PRNG_CONFIG);
1561492c8c0SAdrian Chadd reg |= QCOM_RND_PRNG_CONFIG_HW_ENABLE;
1571492c8c0SAdrian Chadd bus_write_4(sc->reg, QCOM_RND_PRNG_CONFIG, reg);
1581492c8c0SAdrian Chadd bus_barrier(sc->reg, 0, 0x120, BUS_SPACE_BARRIER_WRITE);
1591492c8c0SAdrian Chadd }
1601492c8c0SAdrian Chadd
1611492c8c0SAdrian Chadd random_source_register(&random_qcom_rnd);
1621492c8c0SAdrian Chadd return (0);
1631492c8c0SAdrian Chadd
1641492c8c0SAdrian Chadd }
1651492c8c0SAdrian Chadd
1661492c8c0SAdrian Chadd static int
qcom_rnd_detach(device_t dev)1671492c8c0SAdrian Chadd qcom_rnd_detach(device_t dev)
1681492c8c0SAdrian Chadd {
1691492c8c0SAdrian Chadd struct qcom_rnd_softc *sc;
1701492c8c0SAdrian Chadd
1711492c8c0SAdrian Chadd sc = device_get_softc(dev);
1721492c8c0SAdrian Chadd KASSERT(
1731492c8c0SAdrian Chadd atomic_load_explicit(&g_qcom_rnd_softc, memory_order_acquire) == sc,
1741492c8c0SAdrian Chadd ("only one global instance at a time"));
1751492c8c0SAdrian Chadd
1761492c8c0SAdrian Chadd random_source_deregister(&random_qcom_rnd);
1771492c8c0SAdrian Chadd if (sc->reg != NULL) {
1781492c8c0SAdrian Chadd bus_release_resource(sc->dev, SYS_RES_MEMORY, sc->reg_rid, sc->reg);
1791492c8c0SAdrian Chadd }
1801492c8c0SAdrian Chadd atomic_store_explicit(&g_qcom_rnd_softc, NULL, memory_order_release);
1811492c8c0SAdrian Chadd return (0);
1821492c8c0SAdrian Chadd }
1831492c8c0SAdrian Chadd
1841492c8c0SAdrian Chadd static int
qcom_rnd_harvest(struct qcom_rnd_softc * sc,void * buf,size_t * sz)1851492c8c0SAdrian Chadd qcom_rnd_harvest(struct qcom_rnd_softc *sc, void *buf, size_t *sz)
1861492c8c0SAdrian Chadd {
1871492c8c0SAdrian Chadd /*
1881492c8c0SAdrian Chadd * Add data to buf until we either run out of entropy or we
1891492c8c0SAdrian Chadd * fill the buffer.
1901492c8c0SAdrian Chadd *
1911492c8c0SAdrian Chadd * Note - be mindful of the provided buffer size; we're reading
1921492c8c0SAdrian Chadd * 4 bytes at a time but we only want to supply up to the max
1931492c8c0SAdrian Chadd * buffer size, so don't write past it!
1941492c8c0SAdrian Chadd */
1951492c8c0SAdrian Chadd size_t rz = 0;
1961492c8c0SAdrian Chadd uint32_t reg;
1971492c8c0SAdrian Chadd
1981492c8c0SAdrian Chadd while (rz < *sz) {
1991492c8c0SAdrian Chadd bus_barrier(sc->reg, 0, 0x120, BUS_SPACE_BARRIER_READ);
2001492c8c0SAdrian Chadd reg = bus_read_4(sc->reg, QCOM_RND_PRNG_STATUS);
2011492c8c0SAdrian Chadd if ((reg & QCOM_RND_PRNG_STATUS_DATA_AVAIL) == 0)
2021492c8c0SAdrian Chadd break;
2031492c8c0SAdrian Chadd reg = bus_read_4(sc->reg, QCOM_RND_PRNG_DATA_OUT);
2041492c8c0SAdrian Chadd memcpy(((char *) buf) + rz, ®, sizeof(uint32_t));
2051492c8c0SAdrian Chadd rz += sizeof(uint32_t);
2061492c8c0SAdrian Chadd }
2071492c8c0SAdrian Chadd
2081492c8c0SAdrian Chadd if (rz == 0)
2091492c8c0SAdrian Chadd return (EAGAIN);
2101492c8c0SAdrian Chadd *sz = rz;
2111492c8c0SAdrian Chadd return (0);
2121492c8c0SAdrian Chadd }
2131492c8c0SAdrian Chadd
2141492c8c0SAdrian Chadd static unsigned
qcom_rnd_read(void * buf,unsigned usz)2151492c8c0SAdrian Chadd qcom_rnd_read(void *buf, unsigned usz)
2161492c8c0SAdrian Chadd {
2171492c8c0SAdrian Chadd struct qcom_rnd_softc *sc;
2181492c8c0SAdrian Chadd size_t sz;
2191492c8c0SAdrian Chadd int error;
2201492c8c0SAdrian Chadd
2211492c8c0SAdrian Chadd sc = g_qcom_rnd_softc;
2221492c8c0SAdrian Chadd if (sc == NULL)
2231492c8c0SAdrian Chadd return (0);
2241492c8c0SAdrian Chadd
2251492c8c0SAdrian Chadd sz = usz;
2261492c8c0SAdrian Chadd error = qcom_rnd_harvest(sc, buf, &sz);
2271492c8c0SAdrian Chadd if (error != 0)
2281492c8c0SAdrian Chadd return (0);
2291492c8c0SAdrian Chadd
2301492c8c0SAdrian Chadd return (sz);
2311492c8c0SAdrian Chadd }
2321492c8c0SAdrian Chadd
2331492c8c0SAdrian Chadd static device_method_t qcom_rnd_methods[] = {
2341492c8c0SAdrian Chadd /* Device methods. */
2351492c8c0SAdrian Chadd DEVMETHOD(device_probe, qcom_rnd_probe),
2361492c8c0SAdrian Chadd DEVMETHOD(device_attach, qcom_rnd_attach),
2371492c8c0SAdrian Chadd DEVMETHOD(device_detach, qcom_rnd_detach),
2381492c8c0SAdrian Chadd
2391492c8c0SAdrian Chadd DEVMETHOD_END
2401492c8c0SAdrian Chadd };
2411492c8c0SAdrian Chadd
2421492c8c0SAdrian Chadd static driver_t qcom_rnd_driver = {
2431492c8c0SAdrian Chadd "qcom_rnd",
2441492c8c0SAdrian Chadd qcom_rnd_methods,
2451492c8c0SAdrian Chadd sizeof(struct qcom_rnd_softc)
2461492c8c0SAdrian Chadd };
2471492c8c0SAdrian Chadd
24853e1cbefSJohn Baldwin DRIVER_MODULE(qcom_rnd_random, simplebus, qcom_rnd_driver,
2491492c8c0SAdrian Chadd qcom_rnd_modevent, 0);
25053e1cbefSJohn Baldwin DRIVER_MODULE(qcom_rnd_random, ofwbus, qcom_rnd_driver,
2511492c8c0SAdrian Chadd qcom_rnd_modevent, 0);
2521492c8c0SAdrian Chadd MODULE_DEPEND(qcom_rnd_random, random_device, 1, 1, 1);
2531492c8c0SAdrian Chadd MODULE_VERSION(qcom_rnd_random, 1);
254