1*3b48d9c0Sphessler /* $OpenBSD: qcrng.c,v 1.1 2023/04/28 05:13:37 phessler Exp $ */
2*3b48d9c0Sphessler /*
3*3b48d9c0Sphessler * Copyright (c) 2019 Mark Kettenis <kettenis@openbsd.org>
4*3b48d9c0Sphessler *
5*3b48d9c0Sphessler * Permission to use, copy, modify, and distribute this software for any
6*3b48d9c0Sphessler * purpose with or without fee is hereby granted, provided that the above
7*3b48d9c0Sphessler * copyright notice and this permission notice appear in all copies.
8*3b48d9c0Sphessler *
9*3b48d9c0Sphessler * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10*3b48d9c0Sphessler * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11*3b48d9c0Sphessler * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12*3b48d9c0Sphessler * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13*3b48d9c0Sphessler * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14*3b48d9c0Sphessler * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15*3b48d9c0Sphessler * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16*3b48d9c0Sphessler */
17*3b48d9c0Sphessler
18*3b48d9c0Sphessler #include <sys/param.h>
19*3b48d9c0Sphessler #include <sys/systm.h>
20*3b48d9c0Sphessler #include <sys/device.h>
21*3b48d9c0Sphessler #include <sys/timeout.h>
22*3b48d9c0Sphessler
23*3b48d9c0Sphessler #include <machine/bus.h>
24*3b48d9c0Sphessler #include <machine/fdt.h>
25*3b48d9c0Sphessler
26*3b48d9c0Sphessler #include <dev/ofw/openfirm.h>
27*3b48d9c0Sphessler #include <dev/ofw/fdt.h>
28*3b48d9c0Sphessler
29*3b48d9c0Sphessler /* Registers */
30*3b48d9c0Sphessler #define RNG_DATA 0x0000
31*3b48d9c0Sphessler #define RNG_STATUS 0x0004
32*3b48d9c0Sphessler
33*3b48d9c0Sphessler #define HREAD4(sc, reg) \
34*3b48d9c0Sphessler (bus_space_read_4((sc)->sc_iot, (sc)->sc_ioh, (reg)))
35*3b48d9c0Sphessler
36*3b48d9c0Sphessler struct qcrng_softc {
37*3b48d9c0Sphessler struct device sc_dev;
38*3b48d9c0Sphessler bus_space_tag_t sc_iot;
39*3b48d9c0Sphessler bus_space_handle_t sc_ioh;
40*3b48d9c0Sphessler
41*3b48d9c0Sphessler struct timeout sc_to;
42*3b48d9c0Sphessler };
43*3b48d9c0Sphessler
44*3b48d9c0Sphessler int qcrng_match(struct device *, void *, void *);
45*3b48d9c0Sphessler void qcrng_attach(struct device *, struct device *, void *);
46*3b48d9c0Sphessler
47*3b48d9c0Sphessler const struct cfattach qcrng_ca = {
48*3b48d9c0Sphessler sizeof (struct qcrng_softc), qcrng_match, qcrng_attach
49*3b48d9c0Sphessler };
50*3b48d9c0Sphessler
51*3b48d9c0Sphessler struct cfdriver qcrng_cd = {
52*3b48d9c0Sphessler NULL, "qcrng", DV_DULL
53*3b48d9c0Sphessler };
54*3b48d9c0Sphessler
55*3b48d9c0Sphessler void qcrng_rnd(void *);
56*3b48d9c0Sphessler
57*3b48d9c0Sphessler int
qcrng_match(struct device * parent,void * match,void * aux)58*3b48d9c0Sphessler qcrng_match(struct device *parent, void *match, void *aux)
59*3b48d9c0Sphessler {
60*3b48d9c0Sphessler struct fdt_attach_args *faa = aux;
61*3b48d9c0Sphessler
62*3b48d9c0Sphessler return OF_is_compatible(faa->fa_node, "qcom,prng-ee");
63*3b48d9c0Sphessler }
64*3b48d9c0Sphessler
65*3b48d9c0Sphessler void
qcrng_attach(struct device * parent,struct device * self,void * aux)66*3b48d9c0Sphessler qcrng_attach(struct device *parent, struct device *self, void *aux)
67*3b48d9c0Sphessler {
68*3b48d9c0Sphessler struct qcrng_softc *sc = (struct qcrng_softc *)self;
69*3b48d9c0Sphessler struct fdt_attach_args *faa = aux;
70*3b48d9c0Sphessler
71*3b48d9c0Sphessler if (faa->fa_nreg < 1) {
72*3b48d9c0Sphessler printf(": no registers\n");
73*3b48d9c0Sphessler return;
74*3b48d9c0Sphessler }
75*3b48d9c0Sphessler
76*3b48d9c0Sphessler sc->sc_iot = faa->fa_iot;
77*3b48d9c0Sphessler if (bus_space_map(sc->sc_iot, faa->fa_reg[0].addr,
78*3b48d9c0Sphessler faa->fa_reg[0].size, 0, &sc->sc_ioh)) {
79*3b48d9c0Sphessler printf(": can't map registers\n");
80*3b48d9c0Sphessler return;
81*3b48d9c0Sphessler }
82*3b48d9c0Sphessler
83*3b48d9c0Sphessler printf("\n");
84*3b48d9c0Sphessler
85*3b48d9c0Sphessler timeout_set(&sc->sc_to, qcrng_rnd, sc);
86*3b48d9c0Sphessler qcrng_rnd(sc);
87*3b48d9c0Sphessler }
88*3b48d9c0Sphessler
89*3b48d9c0Sphessler void
qcrng_rnd(void * arg)90*3b48d9c0Sphessler qcrng_rnd(void *arg)
91*3b48d9c0Sphessler {
92*3b48d9c0Sphessler struct qcrng_softc *sc = arg;
93*3b48d9c0Sphessler
94*3b48d9c0Sphessler if (HREAD4(sc, RNG_STATUS) & 0x1)
95*3b48d9c0Sphessler enqueue_randomness(HREAD4(sc, RNG_DATA));
96*3b48d9c0Sphessler
97*3b48d9c0Sphessler timeout_add_sec(&sc->sc_to, 1);
98*3b48d9c0Sphessler }
99