xref: /openbsd-src/sys/dev/fdt/bcm2835_rng.c (revision 99fd087599a8791921855f21bd7e36130f39aadc)
1 /*	$OpenBSD: bcm2835_rng.c,v 1.2 2018/04/28 15:44:59 jasper Exp $	*/
2 /*
3  * Copyright (c) 2018 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/rndvar.h>
27 #include <dev/ofw/openfirm.h>
28 #include <dev/ofw/fdt.h>
29 
30 /* Registers */
31 #define RNG_CTRL		0x0000
32 #define  RNG_CTRL_EN		(1 << 0)
33 #define RNG_STATUS		0x0004
34 #define  RNG_STATUS_COUNT(x)	((x) >> 24)
35 #define RNG_DATA		0x0008
36 
37 #define HREAD4(sc, reg)							\
38 	(bus_space_read_4((sc)->sc_iot, (sc)->sc_ioh, (reg)))
39 #define HWRITE4(sc, reg, val)						\
40 	bus_space_write_4((sc)->sc_iot, (sc)->sc_ioh, (reg), (val))
41 
42 struct bcmrng_softc {
43 	struct device		sc_dev;
44 	bus_space_tag_t		sc_iot;
45 	bus_space_handle_t	sc_ioh;
46 
47 	struct timeout		sc_to;
48 };
49 
50 int	bcmrng_match(struct device *, void *, void *);
51 void	bcmrng_attach(struct device *, struct device *, void *);
52 
53 struct cfattach	bcmrng_ca = {
54 	sizeof (struct bcmrng_softc), bcmrng_match, bcmrng_attach
55 };
56 
57 struct cfdriver bcmrng_cd = {
58 	NULL, "bcmrng", DV_DULL
59 };
60 
61 void	bcmrng_rnd(void *);
62 
63 int
64 bcmrng_match(struct device *parent, void *match, void *aux)
65 {
66 	struct fdt_attach_args *faa = aux;
67 
68 	return OF_is_compatible(faa->fa_node, "brcm,bcm2835-rng");
69 }
70 
71 void
72 bcmrng_attach(struct device *parent, struct device *self, void *aux)
73 {
74 	struct bcmrng_softc *sc = (struct bcmrng_softc *)self;
75 	struct fdt_attach_args *faa = aux;
76 
77 	if (faa->fa_nreg < 1) {
78 		printf(": no registers\n");
79 		return;
80 	}
81 
82 	sc->sc_iot = faa->fa_iot;
83 	if (bus_space_map(sc->sc_iot, faa->fa_reg[0].addr,
84 	    faa->fa_reg[0].size, 0, &sc->sc_ioh)) {
85 		printf(": can't map registers\n");
86 		return;
87 	}
88 
89 	printf("\n");
90 
91 	/* Discard initial numbers which may be "less" random. */
92 	HWRITE4(sc, RNG_STATUS, 250000);
93 	HWRITE4(sc, RNG_CTRL, RNG_CTRL_EN);
94 
95 	timeout_set(&sc->sc_to, bcmrng_rnd, sc);
96 	bcmrng_rnd(sc);
97 }
98 
99 void
100 bcmrng_rnd(void *arg)
101 {
102 	struct bcmrng_softc *sc = arg;
103 	uint32_t status, data;
104 	int i, count;
105 
106 	status = HREAD4(sc, RNG_STATUS);
107 	count = MIN(4, RNG_STATUS_COUNT(status));
108 	for (i = 0; i < count; i++) {
109 		data = HREAD4(sc, RNG_DATA);
110 		enqueue_randomness(data);
111 	}
112 
113 	timeout_add_sec(&sc->sc_to, 1);
114 }
115