octeon_rnm.c revision 1.3 1 /* $NetBSD: octeon_rnm.c,v 1.3 2020/05/12 10:37:10 simonb 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.3 2020/05/12 10:37:10 simonb 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 uint64_t bist_status;
94
95 aprint_normal("\n");
96
97 sc->sc_dev = self;
98 sc->sc_bust = aa->aa_bust;
99 if (bus_space_map(aa->aa_bust, aa->aa_unit->addr, RNM_SIZE,
100 0, &sc->sc_regh) != 0) {
101 aprint_error_dev(self, "unable to map device\n");
102 return;
103 }
104
105 bist_status = bus_space_read_8(sc->sc_bust, sc->sc_regh,
106 RNM_BIST_STATUS_OFFSET);
107 if (bist_status) {
108 aprint_error_dev(self, "RNG built in self test failed: %#lx\n",
109 bist_status);
110 return;
111 }
112
113 bus_space_write_8(sc->sc_bust, sc->sc_regh, RNM_CTL_STATUS_OFFSET,
114 RNM_CTL_STATUS_RNG_EN | RNM_CTL_STATUS_ENT_EN);
115
116 if (hz >= 100)
117 sc->sc_rnghz = hz / 100;
118 else
119 sc->sc_rnghz = 1;
120
121 rnd_attach_source(&sc->sc_rndsrc, device_xname(sc->sc_dev),
122 RND_TYPE_RNG, RND_FLAG_NO_ESTIMATE);
123
124 callout_init(&sc->sc_rngto, 0);
125
126 octeon_rnm_rng(sc);
127
128 aprint_normal("%s: random number generator enabled: %dhz\n",
129 device_xname(sc->sc_dev), sc->sc_rnghz);
130 }
131
132 static void
133 octeon_rnm_rng(void *vsc)
134 {
135 struct octeon_rnm_softc *sc = vsc;
136 uint64_t rn;
137 int i;
138
139 for (i = 0; i < octeon_rnm_burst_count; i++) {
140 rn = octeon_rnm_load(sc);
141 rnd_add_data(&sc->sc_rndsrc,
142 &rn, sizeof(rn), sizeof(rn) * NBBY);
143 /*
144 * XXX
145 * delay should be over RNG_DELAY_CLOCK cycles at least,
146 * we need nanodelay() or clkdelay().
147 */
148 delay(1);
149 }
150
151 callout_reset(&sc->sc_rngto, sc->sc_rnghz, octeon_rnm_rng, sc);
152 }
153
154 static inline uint64_t
155 octeon_rnm_load(struct octeon_rnm_softc *sc)
156 {
157 uint64_t addr =
158 RNM_OPERATION_BASE_IO_BIT |
159 __BITS64_SET(RNM_OPERATION_BASE_MAJOR_DID, 0x08) |
160 __BITS64_SET(RNM_OPERATION_BASE_SUB_DID, 0x00);
161
162 return octeon_xkphys_read_8(addr);
163 }
164
165 static inline int
166 octeon_rnm_iobdma(struct octeon_rnm_softc *sc)
167 {
168
169 /* XXX */
170 return 0;
171 }
172
173 SYSCTL_SETUP(octeon_rnm_sysctl, "sysctl octeon_rnm subtree setup")
174 {
175 int rc, root_num;
176 const struct sysctlnode *node;
177
178 if ( (rc = sysctl_createv(clog, 0, NULL, NULL,
179 CTLFLAG_PERMANENT, CTLTYPE_NODE, "hw", NULL,
180 NULL, 0, NULL, 0, CTL_HW, CTL_EOL)) != 0) {
181 goto err;
182 }
183
184 if ( (rc = sysctl_createv(clog, 0, NULL, &node,
185 CTLFLAG_PERMANENT, CTLTYPE_NODE, "octeon_rnm",
186 SYSCTL_DESCR("octeon_rnm controls"),
187 NULL, 0, NULL, 0, CTL_HW, CTL_CREATE, CTL_EOL)) != 0) {
188 goto err;
189 }
190
191 root_num = node->sysctl_num;
192
193 if ( (rc = sysctl_createv(clog, 0, NULL, NULL,
194 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
195 CTLTYPE_INT, "burst_count",
196 SYSCTL_DESCR("Burst read count per callout"),
197 NULL, 0, &octeon_rnm_burst_count,
198 0, CTL_HW, root_num, CTL_CREATE, CTL_EOL)) != 0) {
199 goto err;
200 }
201
202 return;
203 err:
204 printf("%s: sysctl_createv failed (rc = %d)\n", __func__, rc);
205 }
206