xref: /netbsd-src/sys/arch/mips/cavium/dev/octeon_rnm.c (revision e6c7e151de239c49d2e38720a061ed9d1fa99309)
1 /*	$NetBSD: octeon_rnm.c,v 1.2 2019/01/08 19:41:09 jdolecek Exp $	*/
2 
3 /*
4  * Copyright (c) 2007 Internet Initiative Japan, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: octeon_rnm.c,v 1.2 2019/01/08 19:41:09 jdolecek Exp $");
31 
32 #include <sys/param.h>
33 #include <sys/device.h>
34 #include <sys/systm.h>
35 #include <sys/sysctl.h>
36 #include <sys/kernel.h>
37 #include <sys/rndsource.h>
38 
39 #include <mips/locore.h>
40 #include <mips/cavium/include/iobusvar.h>
41 #include <mips/cavium/dev/octeon_rnmreg.h>
42 #include <mips/cavium/dev/octeon_corereg.h>
43 #include <mips/cavium/octeonvar.h>
44 
45 #include <sys/bus.h>
46 
47 #define RNG_DELAY_CLOCK 91
48 #define RNG_DEF_BURST_COUNT 10
49 
50 int octeon_rnm_burst_count = RNG_DEF_BURST_COUNT;
51 
52 struct octeon_rnm_softc {
53 	device_t sc_dev;
54 
55 	bus_space_tag_t		sc_bust;
56 	bus_space_handle_t	sc_regh;
57 
58 	krndsource_t		sc_rndsrc;	/* /dev/random source */
59 	struct callout		sc_rngto;	/* rng timeout */
60 	int			sc_rnghz;	/* rng poll time */
61 };
62 
63 static int octeon_rnm_match(device_t, struct cfdata *, void *);
64 static void octeon_rnm_attach(device_t, device_t, void *);
65 static void octeon_rnm_rng(void *);
66 static inline uint64_t octeon_rnm_load(struct octeon_rnm_softc *);
67 static inline int octeon_rnm_iobdma(struct octeon_rnm_softc *);
68 
69 CFATTACH_DECL_NEW(octeon_rnm, sizeof(struct octeon_rnm_softc),
70     octeon_rnm_match, octeon_rnm_attach, NULL, NULL);
71 
72 static int
73 octeon_rnm_match(device_t parent, struct cfdata *cf, void *aux)
74 {
75 	struct iobus_attach_args *aa = aux;
76 	int result = 0;
77 
78 	if (strcmp(cf->cf_name, aa->aa_name) != 0)
79 		goto out;
80 	if (cf->cf_unit != aa->aa_unitno)
81 		goto out;
82 	result = 1;
83 
84 out:
85 	return result;
86 }
87 
88 static void
89 octeon_rnm_attach(device_t parent, device_t self, void *aux)
90 {
91 	struct octeon_rnm_softc *sc = device_private(self);
92 	struct iobus_attach_args *aa = aux;
93 	int status;
94 
95 	aprint_normal("\n");
96 
97 	sc->sc_dev = self;
98 	sc->sc_bust = aa->aa_bust;
99 	status = bus_space_map(aa->aa_bust, aa->aa_unit->addr, RNM_SIZE,
100 	    0, &sc->sc_regh);
101 	if (status != 0)
102 		panic(": can't map i/o space");
103 
104 	bus_space_write_8(sc->sc_bust, sc->sc_regh, RNM_CTL_STATUS_OFFSET,
105 	    RNM_CTL_STATUS_RNG_EN | RNM_CTL_STATUS_ENT_EN);
106 
107 	if (hz >= 100)
108 		sc->sc_rnghz = hz / 100;
109 	else
110 		sc->sc_rnghz = 1;
111 
112 	rnd_attach_source(&sc->sc_rndsrc, device_xname(sc->sc_dev),
113 	    RND_TYPE_RNG, RND_FLAG_NO_ESTIMATE);
114 
115 	callout_init(&sc->sc_rngto, 0);
116 
117 	octeon_rnm_rng(sc);
118 
119 	aprint_normal("%s: random number generator enabled: %dhz\n",
120 	    device_xname(sc->sc_dev), sc->sc_rnghz);
121 }
122 
123 static void
124 octeon_rnm_rng(void *vsc)
125 {
126 	struct octeon_rnm_softc *sc = vsc;
127 	uint64_t rn;
128 	int i;
129 
130 	for (i = 0; i < octeon_rnm_burst_count; i++) {
131 		rn = octeon_rnm_load(sc);
132 		rnd_add_data(&sc->sc_rndsrc,
133 				&rn, sizeof(rn), sizeof(rn) * NBBY);
134 		/*
135 		 * XXX
136 		 * delay should be over RNG_DELAY_CLOCK cycles at least,
137 		 * we need nanodelay() or clkdelay().
138 		 */
139 		delay(1);
140 	}
141 
142 	callout_reset(&sc->sc_rngto, sc->sc_rnghz, octeon_rnm_rng, sc);
143 }
144 
145 static inline uint64_t
146 octeon_rnm_load(struct octeon_rnm_softc *sc)
147 {
148 	uint64_t addr =
149 	    RNM_OPERATION_BASE_IO_BIT |
150 	    __BITS64_SET(RNM_OPERATION_BASE_MAJOR_DID, 0x08) |
151 	    __BITS64_SET(RNM_OPERATION_BASE_SUB_DID, 0x00);
152 
153 	return octeon_xkphys_read_8(addr);
154 }
155 
156 static inline int
157 octeon_rnm_iobdma(struct octeon_rnm_softc *sc)
158 {
159 
160 	/* XXX */
161 	return 0;
162 }
163 
164 SYSCTL_SETUP(octeon_rnm_sysctl, "sysctl octeon_rnm subtree setup")
165 {
166 	int rc, root_num;
167 	const struct sysctlnode *node;
168 
169 	if ( (rc = sysctl_createv(clog, 0, NULL, NULL,
170 			CTLFLAG_PERMANENT, CTLTYPE_NODE, "hw", NULL,
171 			NULL, 0, NULL, 0, CTL_HW, CTL_EOL)) != 0) {
172 		goto err;
173 	}
174 
175 	if ( (rc = sysctl_createv(clog, 0, NULL, &node,
176 			CTLFLAG_PERMANENT, CTLTYPE_NODE, "octeon_rnm",
177 			SYSCTL_DESCR("octeon_rnm controls"),
178 			NULL, 0, NULL, 0, CTL_HW, CTL_CREATE, CTL_EOL)) != 0) {
179 		goto err;
180 	}
181 
182 	root_num = node->sysctl_num;
183 
184 	if ( (rc = sysctl_createv(clog, 0, NULL, NULL,
185 			CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
186 			CTLTYPE_INT, "burst_count",
187 			SYSCTL_DESCR("Burst read count per callout"),
188 			NULL, 0, &octeon_rnm_burst_count,
189 			0, CTL_HW, root_num, CTL_CREATE, CTL_EOL)) != 0) {
190 		goto err;
191 	}
192 
193 	return;
194 err:
195 	printf("%s: sysctl_createv failed (rc = %d)\n", __func__, rc);
196 }
197