xref: /openbsd-src/sys/arch/octeon/dev/octrng.c (revision 471aeecfc619bc9b69519928152daf993376c2a1)
1*471aeecfSnaddy /*	$OpenBSD: octrng.c,v 1.10 2022/04/06 18:59:27 naddy Exp $	*/
2188db5f4Spirofti /*
38855e28cSpirofti  * Copyright (c) 2013 Paul Irofti <paul@irofti.net>
4188db5f4Spirofti  *
5188db5f4Spirofti  * Permission to use, copy, modify, and/or distribute this software for any
6188db5f4Spirofti  * purpose with or without fee is hereby granted, provided that the above
7188db5f4Spirofti  * copyright notice and this permission notice appear in all copies.
8188db5f4Spirofti  *
9188db5f4Spirofti  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10188db5f4Spirofti  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11188db5f4Spirofti  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12188db5f4Spirofti  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13188db5f4Spirofti  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14188db5f4Spirofti  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15188db5f4Spirofti  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16188db5f4Spirofti  */
17188db5f4Spirofti 
18188db5f4Spirofti #include <sys/param.h>
19188db5f4Spirofti #include <sys/systm.h>
20188db5f4Spirofti #include <sys/device.h>
21188db5f4Spirofti #include <sys/timeout.h>
22188db5f4Spirofti 
23188db5f4Spirofti #include <machine/octeonvar.h>
24188db5f4Spirofti #include <machine/octeonreg.h>
25188db5f4Spirofti #include <octeon/dev/iobusvar.h>
26188db5f4Spirofti 
27188db5f4Spirofti int	octrng_match(struct device *, void *, void *);
28188db5f4Spirofti void	octrng_attach(struct device *, struct device *, void *);
29188db5f4Spirofti void	octrng_rnd(void *arg);
30188db5f4Spirofti 
31188db5f4Spirofti #ifdef OCTRNG_DEBUG
32188db5f4Spirofti #define DPRINTF(x)	printf x
33188db5f4Spirofti #else
34188db5f4Spirofti #define DPRINTF(x)
35188db5f4Spirofti #endif
36188db5f4Spirofti 
37188db5f4Spirofti #define OCTRNG_MAP_SIZE 8ULL
38188db5f4Spirofti 
39188db5f4Spirofti /* OCTRNG_ENTROPY_ADDR 0x8001400000000000ULL */
40188db5f4Spirofti #define OCTRNG_MAJORDID 8ULL
41188db5f4Spirofti #define OCTRNG_ENTROPY_ADDR (((uint64_t)1 << 48) | \
42188db5f4Spirofti 		((uint64_t)(OCTRNG_MAJORDID & 0x1f) << 43))
43188db5f4Spirofti #define OCTRNG_ENTROPY_REG	0
44188db5f4Spirofti 
45188db5f4Spirofti #define OCTRNG_CONTROL_ADDR 0x0001180040000000ULL
46188db5f4Spirofti 
47046f9a53Spirofti #define OCTRNG_RESET (1ULL << 3)
48046f9a53Spirofti #define OCTRNG_ENABLE_OUTPUT (1ULL << 1)
49046f9a53Spirofti #define OCTRNG_ENABLE_ENTROPY (1ULL << 0)
50188db5f4Spirofti 
51188db5f4Spirofti struct octrng_softc {
52188db5f4Spirofti 	struct device sc_dev;
53188db5f4Spirofti 	struct timeout sc_to;
54188db5f4Spirofti 	struct iobus_attach_args *sc_io;
55188db5f4Spirofti 
56188db5f4Spirofti 	bus_space_tag_t sc_iot;
57188db5f4Spirofti 	bus_space_handle_t sc_ioh;
58188db5f4Spirofti };
59188db5f4Spirofti 
60*471aeecfSnaddy const struct cfattach octrng_ca = {
61188db5f4Spirofti 	sizeof(struct octrng_softc), octrng_match, octrng_attach
62188db5f4Spirofti };
63188db5f4Spirofti 
64188db5f4Spirofti struct cfdriver octrng_cd = {
65188db5f4Spirofti 	NULL, "octrng", DV_DULL
66188db5f4Spirofti };
67188db5f4Spirofti 
68299b4781Spirofti 
69188db5f4Spirofti int
octrng_match(struct device * parent,void * match,void * aux)70188db5f4Spirofti octrng_match(struct device *parent, void *match, void *aux)
71188db5f4Spirofti {
72299b4781Spirofti 	struct iobus_attach_args *aa = aux;
73299b4781Spirofti 	struct cfdata *cf = match;
74299b4781Spirofti 
75188db5f4Spirofti 	/* XXX: check for board type */
76299b4781Spirofti 
7760d64acfSjasper 	if (strcmp(aa->aa_name, cf->cf_driver->cd_name) != 0)
78299b4781Spirofti 		return (0);
79299b4781Spirofti 
80188db5f4Spirofti 	return (1);
81188db5f4Spirofti }
82188db5f4Spirofti 
83188db5f4Spirofti void
octrng_attach(struct device * parent,struct device * self,void * aux)84188db5f4Spirofti octrng_attach(struct device *parent, struct device *self, void *aux)
85188db5f4Spirofti {
86188db5f4Spirofti 	struct octrng_softc *sc = (void *)self;
87188db5f4Spirofti 	sc->sc_io = aux;
88188db5f4Spirofti 
89188db5f4Spirofti 	uint64_t control_reg;
90188db5f4Spirofti 
91188db5f4Spirofti 	sc->sc_iot = sc->sc_io->aa_bust;
92188db5f4Spirofti 
93188db5f4Spirofti 	if (bus_space_map(sc->sc_iot, OCTEON_RNG_BASE, OCTRNG_MAP_SIZE, 0,
94188db5f4Spirofti 	    &sc->sc_ioh)) {
95188db5f4Spirofti 		printf(": can't map registers");
96188db5f4Spirofti 	}
97188db5f4Spirofti 
98188db5f4Spirofti 	control_reg = octeon_xkphys_read_8(OCTRNG_CONTROL_ADDR);
99188db5f4Spirofti 	control_reg |= (OCTRNG_ENABLE_OUTPUT | OCTRNG_ENABLE_ENTROPY);
100188db5f4Spirofti 	octeon_xkphys_write_8(OCTRNG_CONTROL_ADDR, control_reg);
101188db5f4Spirofti 
102188db5f4Spirofti 	timeout_set(&sc->sc_to, octrng_rnd, sc);
103188db5f4Spirofti 
104188db5f4Spirofti 	timeout_add_sec(&sc->sc_to, 5);
105188db5f4Spirofti 
106188db5f4Spirofti 	printf("\n");
107188db5f4Spirofti }
108188db5f4Spirofti 
109188db5f4Spirofti void
octrng_rnd(void * arg)110188db5f4Spirofti octrng_rnd(void *arg)
111188db5f4Spirofti {
112188db5f4Spirofti 	struct octrng_softc *sc = arg;
113188db5f4Spirofti 	uint64_t value;
114188db5f4Spirofti 
115188db5f4Spirofti 	value = bus_space_read_8(sc->sc_iot, sc->sc_ioh, OCTRNG_ENTROPY_REG);
116188db5f4Spirofti 
117188db5f4Spirofti 	DPRINTF(("%#llX ", value));	/* WARNING: very verbose */
118188db5f4Spirofti 
1199e9abf5bSjasper 	enqueue_randomness(value);
120188db5f4Spirofti 	timeout_add_msec(&sc->sc_to, 10);
121188db5f4Spirofti }
122