From 3b48d9c0d4279ed4966d7521c956f131e18a83e8 Mon Sep 17 00:00:00 2001 From: phessler Date: Fri, 28 Apr 2023 05:13:37 +0000 Subject: [PATCH] Add a driver for the Qualcomm rng device found on the Thinkpad X13s, based on kettenis's amlrng driver. suggestions and OK patrick@ --- sys/arch/arm64/conf/GENERIC | 3 +- sys/arch/arm64/conf/RAMDISK | 3 +- sys/dev/fdt/files.fdt | 7 ++- sys/dev/fdt/qcrng.c | 98 +++++++++++++++++++++++++++++++++++++ 4 files changed, 108 insertions(+), 3 deletions(-) create mode 100644 sys/dev/fdt/qcrng.c diff --git a/sys/arch/arm64/conf/GENERIC b/sys/arch/arm64/conf/GENERIC index edb65a3ef41..0b13e8c4e4b 100644 --- a/sys/arch/arm64/conf/GENERIC +++ b/sys/arch/arm64/conf/GENERIC @@ -1,4 +1,4 @@ -# $OpenBSD: GENERIC,v 1.266 2023/04/20 19:28:30 jcs Exp $ +# $OpenBSD: GENERIC,v 1.267 2023/04/28 05:13:37 phessler Exp $ # # GENERIC machine description file # @@ -333,6 +333,7 @@ qcpmic* at qcspmi? qcpmicgpio* at qcpmic? qcpon* at qcpmic? qcpwm* at qcpmic? +qcrng* at fdt? qcrtc* at qcpmic? # Sunxi SoCs diff --git a/sys/arch/arm64/conf/RAMDISK b/sys/arch/arm64/conf/RAMDISK index d630c574a68..6d42fda91e5 100644 --- a/sys/arch/arm64/conf/RAMDISK +++ b/sys/arch/arm64/conf/RAMDISK @@ -1,4 +1,4 @@ -# $OpenBSD: RAMDISK,v 1.198 2023/04/20 19:28:30 jcs Exp $ +# $OpenBSD: RAMDISK,v 1.199 2023/04/28 05:13:37 phessler Exp $ machine arm64 maxusers 4 @@ -257,6 +257,7 @@ qcpmic* at qcspmi? qcpmicgpio* at qcpmic? qcpon* at qcpmic? qcpwm* at qcpmic? +qcrng* at fdt? qcrtc* at qcpmic? # Sunxi SoCs diff --git a/sys/dev/fdt/files.fdt b/sys/dev/fdt/files.fdt index 5df8d8572c8..5182538a391 100644 --- a/sys/dev/fdt/files.fdt +++ b/sys/dev/fdt/files.fdt @@ -1,4 +1,4 @@ -# $OpenBSD: files.fdt,v 1.186 2023/04/19 00:19:17 dlg Exp $ +# $OpenBSD: files.fdt,v 1.187 2023/04/28 05:13:37 phessler Exp $ # # Config file and device description for machine-independent FDT code. # Included by ports that need it. @@ -711,6 +711,11 @@ device qcpwm attach qcpwm at spmi file dev/fdt/qcpwm.c qcpwm +# Qualcomm RNG +device qcrng +attach qcrng at fdt +file dev/fdt/qcrng.c qcrng + # Qualcomm PMIC RTC device qcrtc attach qcrtc at spmi diff --git a/sys/dev/fdt/qcrng.c b/sys/dev/fdt/qcrng.c new file mode 100644 index 00000000000..c95c0a72573 --- /dev/null +++ b/sys/dev/fdt/qcrng.c @@ -0,0 +1,98 @@ +/* $OpenBSD: qcrng.c,v 1.1 2023/04/28 05:13:37 phessler Exp $ */ +/* + * Copyright (c) 2019 Mark Kettenis + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include +#include +#include +#include + +#include +#include + +#include +#include + +/* Registers */ +#define RNG_DATA 0x0000 +#define RNG_STATUS 0x0004 + +#define HREAD4(sc, reg) \ + (bus_space_read_4((sc)->sc_iot, (sc)->sc_ioh, (reg))) + +struct qcrng_softc { + struct device sc_dev; + bus_space_tag_t sc_iot; + bus_space_handle_t sc_ioh; + + struct timeout sc_to; +}; + +int qcrng_match(struct device *, void *, void *); +void qcrng_attach(struct device *, struct device *, void *); + +const struct cfattach qcrng_ca = { + sizeof (struct qcrng_softc), qcrng_match, qcrng_attach +}; + +struct cfdriver qcrng_cd = { + NULL, "qcrng", DV_DULL +}; + +void qcrng_rnd(void *); + +int +qcrng_match(struct device *parent, void *match, void *aux) +{ + struct fdt_attach_args *faa = aux; + + return OF_is_compatible(faa->fa_node, "qcom,prng-ee"); +} + +void +qcrng_attach(struct device *parent, struct device *self, void *aux) +{ + struct qcrng_softc *sc = (struct qcrng_softc *)self; + struct fdt_attach_args *faa = aux; + + if (faa->fa_nreg < 1) { + printf(": no registers\n"); + return; + } + + sc->sc_iot = faa->fa_iot; + if (bus_space_map(sc->sc_iot, faa->fa_reg[0].addr, + faa->fa_reg[0].size, 0, &sc->sc_ioh)) { + printf(": can't map registers\n"); + return; + } + + printf("\n"); + + timeout_set(&sc->sc_to, qcrng_rnd, sc); + qcrng_rnd(sc); +} + +void +qcrng_rnd(void *arg) +{ + struct qcrng_softc *sc = arg; + + if (HREAD4(sc, RNG_STATUS) & 0x1) + enqueue_randomness(HREAD4(sc, RNG_DATA)); + + timeout_add_sec(&sc->sc_to, 1); +} -- 2.20.1