xref: /netbsd-src/sys/arch/mips/cavium/dev/octeon_rnm.c (revision bdc22b2e01993381dcefeff2bc9b56ca75a4235c)
1 /*	$NetBSD: octeon_rnm.c,v 1.1 2015/04/29 08:32:01 hikaru 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.1 2015/04/29 08:32:01 hikaru 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 #include <machine/param.h>
47 
48 #define RNG_DELAY_CLOCK 91
49 #define RNG_DEF_BURST_COUNT 10
50 
51 int octeon_rnm_burst_count = RNG_DEF_BURST_COUNT;
52 
53 struct octeon_rnm_softc {
54 	device_t sc_dev;
55 
56 	bus_space_tag_t		sc_bust;
57 	bus_space_handle_t	sc_regh;
58 
59 	krndsource_t		sc_rndsrc;	/* /dev/random source */
60 	struct callout		sc_rngto;	/* rng timeout */
61 	int			sc_rnghz;	/* rng poll time */
62 };
63 
64 static int octeon_rnm_match(device_t, struct cfdata *, void *);
65 static void octeon_rnm_attach(device_t, device_t, void *);
66 static void octeon_rnm_rng(void *);
67 static inline uint64_t octeon_rnm_load(struct octeon_rnm_softc *);
68 static inline int octeon_rnm_iobdma(struct octeon_rnm_softc *);
69 
70 CFATTACH_DECL_NEW(octeon_rnm, sizeof(struct octeon_rnm_softc),
71     octeon_rnm_match, octeon_rnm_attach, NULL, NULL);
72 
73 static int
74 octeon_rnm_match(device_t parent, struct cfdata *cf, void *aux)
75 {
76 	struct iobus_attach_args *aa = aux;
77 	int result = 0;
78 
79 	if (strcmp(cf->cf_name, aa->aa_name) != 0)
80 		goto out;
81 	if (cf->cf_unit != aa->aa_unitno)
82 		goto out;
83 	result = 1;
84 
85 out:
86 	return result;
87 }
88 
89 static void
90 octeon_rnm_attach(device_t parent, device_t self, void *aux)
91 {
92 	struct octeon_rnm_softc *sc = device_private(self);
93 	struct iobus_attach_args *aa = aux;
94 	int status;
95 
96 	aprint_normal("\n");
97 
98 	sc->sc_dev = self;
99 	sc->sc_bust = aa->aa_bust;
100 	status = bus_space_map(aa->aa_bust, aa->aa_unit->addr, RNM_SIZE,
101 	    0, &sc->sc_regh);
102 	if (status != 0)
103 		panic(": can't map i/o space");
104 
105 	bus_space_write_8(sc->sc_bust, sc->sc_regh, RNM_CTL_STATUS_OFFSET,
106 	    RNM_CTL_STATUS_RNG_EN | RNM_CTL_STATUS_ENT_EN);
107 
108 	if (hz >= 100)
109 		sc->sc_rnghz = hz / 100;
110 	else
111 		sc->sc_rnghz = 1;
112 
113 	rnd_attach_source(&sc->sc_rndsrc, device_xname(sc->sc_dev),
114 	    RND_TYPE_RNG, RND_FLAG_NO_ESTIMATE);
115 
116 	callout_init(&sc->sc_rngto, 0);
117 
118 	octeon_rnm_rng(sc);
119 
120 	aprint_normal("%s: random number generator enabled: %dhz\n",
121 	    device_xname(sc->sc_dev), sc->sc_rnghz);
122 }
123 
124 static void
125 octeon_rnm_rng(void *vsc)
126 {
127 	struct octeon_rnm_softc *sc = vsc;
128 	uint64_t rn;
129 	int i;
130 
131 	for (i = 0; i < octeon_rnm_burst_count; i++) {
132 		rn = octeon_rnm_load(sc);
133 		rnd_add_data(&sc->sc_rndsrc,
134 				&rn, sizeof(rn), sizeof(rn) * NBBY);
135 		/*
136 		 * XXX
137 		 * delay should be over RNG_DELAY_CLOCK cycles at least,
138 		 * we need nanodelay() or clkdelay().
139 		 */
140 		delay(1);
141 	}
142 
143 	callout_reset(&sc->sc_rngto, sc->sc_rnghz, octeon_rnm_rng, sc);
144 }
145 
146 static inline uint64_t
147 octeon_rnm_load(struct octeon_rnm_softc *sc)
148 {
149 	uint64_t addr =
150 	    RNM_OPERATION_BASE_IO_BIT |
151 	    __BITS64_SET(RNM_OPERATION_BASE_MAJOR_DID, 0x08) |
152 	    __BITS64_SET(RNM_OPERATION_BASE_SUB_DID, 0x00);
153 
154 	return octeon_xkphys_read_8(addr);
155 }
156 
157 static inline int
158 octeon_rnm_iobdma(struct octeon_rnm_softc *sc)
159 {
160 
161 	/* XXX */
162 	return 0;
163 }
164 
165 SYSCTL_SETUP(octeon_rnm_sysctl, "sysctl octeon_rnm subtree setup")
166 {
167 	int rc, root_num;
168 	const struct sysctlnode *node;
169 
170 	if ( (rc = sysctl_createv(clog, 0, NULL, NULL,
171 			CTLFLAG_PERMANENT, CTLTYPE_NODE, "hw", NULL,
172 			NULL, 0, NULL, 0, CTL_HW, CTL_EOL)) != 0) {
173 		goto err;
174 	}
175 
176 	if ( (rc = sysctl_createv(clog, 0, NULL, &node,
177 			CTLFLAG_PERMANENT, CTLTYPE_NODE, "octeon_rnm",
178 			SYSCTL_DESCR("octeon_rnm controls"),
179 			NULL, 0, NULL, 0, CTL_HW, CTL_CREATE, CTL_EOL)) != 0) {
180 		goto err;
181 	}
182 
183 	root_num = node->sysctl_num;
184 
185 	if ( (rc = sysctl_createv(clog, 0, NULL, NULL,
186 			CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
187 			CTLTYPE_INT, "burst_count",
188 			SYSCTL_DESCR("Burst read count per callout"),
189 			NULL, 0, &octeon_rnm_burst_count,
190 			0, CTL_HW, root_num, CTL_CREATE, CTL_EOL)) != 0) {
191 		goto err;
192 	}
193 
194 	return;
195 err:
196 	printf("%s: sysctl_createv failed (rc = %d)\n", __func__, rc);
197 }
198