xref: /openbsd-src/sys/arch/octeon/dev/octrng.c (revision 471aeecfc619bc9b69519928152daf993376c2a1)
1 /*	$OpenBSD: octrng.c,v 1.10 2022/04/06 18:59:27 naddy Exp $	*/
2 /*
3  * Copyright (c) 2013 Paul Irofti <paul@irofti.net>
4  *
5  * Permission to use, copy, modify, and/or 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/octeonvar.h>
24 #include <machine/octeonreg.h>
25 #include <octeon/dev/iobusvar.h>
26 
27 int	octrng_match(struct device *, void *, void *);
28 void	octrng_attach(struct device *, struct device *, void *);
29 void	octrng_rnd(void *arg);
30 
31 #ifdef OCTRNG_DEBUG
32 #define DPRINTF(x)	printf x
33 #else
34 #define DPRINTF(x)
35 #endif
36 
37 #define OCTRNG_MAP_SIZE 8ULL
38 
39 /* OCTRNG_ENTROPY_ADDR 0x8001400000000000ULL */
40 #define OCTRNG_MAJORDID 8ULL
41 #define OCTRNG_ENTROPY_ADDR (((uint64_t)1 << 48) | \
42 		((uint64_t)(OCTRNG_MAJORDID & 0x1f) << 43))
43 #define OCTRNG_ENTROPY_REG	0
44 
45 #define OCTRNG_CONTROL_ADDR 0x0001180040000000ULL
46 
47 #define OCTRNG_RESET (1ULL << 3)
48 #define OCTRNG_ENABLE_OUTPUT (1ULL << 1)
49 #define OCTRNG_ENABLE_ENTROPY (1ULL << 0)
50 
51 struct octrng_softc {
52 	struct device sc_dev;
53 	struct timeout sc_to;
54 	struct iobus_attach_args *sc_io;
55 
56 	bus_space_tag_t sc_iot;
57 	bus_space_handle_t sc_ioh;
58 };
59 
60 const struct cfattach octrng_ca = {
61 	sizeof(struct octrng_softc), octrng_match, octrng_attach
62 };
63 
64 struct cfdriver octrng_cd = {
65 	NULL, "octrng", DV_DULL
66 };
67 
68 
69 int
octrng_match(struct device * parent,void * match,void * aux)70 octrng_match(struct device *parent, void *match, void *aux)
71 {
72 	struct iobus_attach_args *aa = aux;
73 	struct cfdata *cf = match;
74 
75 	/* XXX: check for board type */
76 
77 	if (strcmp(aa->aa_name, cf->cf_driver->cd_name) != 0)
78 		return (0);
79 
80 	return (1);
81 }
82 
83 void
octrng_attach(struct device * parent,struct device * self,void * aux)84 octrng_attach(struct device *parent, struct device *self, void *aux)
85 {
86 	struct octrng_softc *sc = (void *)self;
87 	sc->sc_io = aux;
88 
89 	uint64_t control_reg;
90 
91 	sc->sc_iot = sc->sc_io->aa_bust;
92 
93 	if (bus_space_map(sc->sc_iot, OCTEON_RNG_BASE, OCTRNG_MAP_SIZE, 0,
94 	    &sc->sc_ioh)) {
95 		printf(": can't map registers");
96 	}
97 
98 	control_reg = octeon_xkphys_read_8(OCTRNG_CONTROL_ADDR);
99 	control_reg |= (OCTRNG_ENABLE_OUTPUT | OCTRNG_ENABLE_ENTROPY);
100 	octeon_xkphys_write_8(OCTRNG_CONTROL_ADDR, control_reg);
101 
102 	timeout_set(&sc->sc_to, octrng_rnd, sc);
103 
104 	timeout_add_sec(&sc->sc_to, 5);
105 
106 	printf("\n");
107 }
108 
109 void
octrng_rnd(void * arg)110 octrng_rnd(void *arg)
111 {
112 	struct octrng_softc *sc = arg;
113 	uint64_t value;
114 
115 	value = bus_space_read_8(sc->sc_iot, sc->sc_ioh, OCTRNG_ENTROPY_REG);
116 
117 	DPRINTF(("%#llX ", value));	/* WARNING: very verbose */
118 
119 	enqueue_randomness(value);
120 	timeout_add_msec(&sc->sc_to, 10);
121 }
122