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