bcm53xx_rng.c revision 1.4 1 /*-
2 * Copyright (c) 2012 The NetBSD Foundation, Inc.
3 * All rights reserved.
4 *
5 * This code is derived from software contributed to The NetBSD Foundation
6 * by Matt Thomas of 3am Software Foundry.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
21 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 * POSSIBILITY OF SUCH DAMAGE.
28 */
29
30 #define RNG_PRIVATE
31
32 #include "locators.h"
33
34 #include <sys/cdefs.h>
35
36 __KERNEL_RCSID(1, "$NetBSD: bcm53xx_rng.c,v 1.4 2013/02/01 19:27:47 matt Exp $");
37
38 #include <sys/bus.h>
39 #include <sys/callout.h>
40 #include <sys/device.h>
41 #include <sys/intr.h>
42 #include <sys/rnd.h>
43 #include <sys/systm.h>
44
45 #include <arm/broadcom/bcm53xx_reg.h>
46 #include <arm/broadcom/bcm53xx_var.h>
47
48 static int bcmrng_ccb_match(device_t, cfdata_t, void *);
49 static void bcmrng_ccb_attach(device_t, device_t, void *);
50
51 struct bcmrng_softc {
52 device_t sc_dev;
53 bus_space_tag_t sc_bst;
54 bus_space_handle_t sc_bsh;
55 krndsource_t sc_rnd_source;
56 struct callout sc_rnd_callout;
57 kmutex_t *sc_lock;
58 #ifdef RNG_USE_INTR
59 size_t sc_intr_amount;
60 void *sc_ih;
61 #endif
62 };
63
64 static void bcmrng_callout(void *arg);
65 static size_t bcmrng_empty(struct bcmrng_softc *);
66 #ifdef RNG_USE_INTR
67 static int bcmrng_intr(void *);
68 #endif
69
70 CFATTACH_DECL_NEW(bcmrng_ccb, sizeof(struct bcmrng_softc),
71 bcmrng_ccb_match, bcmrng_ccb_attach, NULL, NULL);
72
73 static int
74 bcmrng_ccb_match(device_t parent, cfdata_t cf, void *aux)
75 {
76 struct bcmccb_attach_args * const ccbaa = aux;
77 const struct bcm_locators * const loc = &ccbaa->ccbaa_loc;
78
79 if (strcmp(cf->cf_name, loc->loc_name))
80 return 0;
81
82 KASSERT(cf->cf_loc[BCMCCBCF_PORT] == BCMCCBCF_PORT_DEFAULT);
83
84 return 1;
85 }
86
87 static inline uint32_t
88 bcmrng_read_4(struct bcmrng_softc *sc, bus_size_t o)
89 {
90 return bus_space_read_4(sc->sc_bst, sc->sc_bsh, o);
91 }
92
93 static inline void
94 bcmrng_read_multi_4(struct bcmrng_softc *sc, bus_size_t o, uint32_t *p,
95 size_t c)
96 {
97 return bus_space_read_multi_4(sc->sc_bst, sc->sc_bsh, o, p, c);
98 }
99
100 static inline void
101 bcmrng_write_4(struct bcmrng_softc *sc, bus_size_t o, uint32_t v)
102 {
103 bus_space_write_4(sc->sc_bst, sc->sc_bsh, o, v);
104 }
105
106 /*
107 * To be called from initarm for an early start.
108 */
109 void
110 bcm53xx_rng_start(bus_space_tag_t bst, bus_space_handle_t bsh)
111 {
112 /*
113 * Disable the interrupt and enable the RNG.
114 */
115 bus_space_write_4(bst, bsh, CCB_BASE + RNG_BASE + RNG_INT_MASK, RNG_INT_OFF);
116 bus_space_write_4(bst, bsh, CCB_BASE + RNG_BASE + RNG_CTRL,
117 bus_space_read_4(bst, bsh, CCB_BASE + RNG_BASE + RNG_CTRL) | RNG_RBGEN);
118 }
119
120 static void
121 bcmrng_ccb_attach(device_t parent, device_t self, void *aux)
122 {
123 struct bcmrng_softc * const sc = device_private(self);
124 struct bcmccb_attach_args * const ccbaa = aux;
125 const struct bcm_locators * const loc = &ccbaa->ccbaa_loc;
126
127 sc->sc_dev = self;
128
129 /* make sure it's running */
130 bcm53xx_rng_start(ccbaa->ccbaa_ccb_bst, ccbaa->ccbaa_ccb_bsh);
131
132 sc->sc_bst = ccbaa->ccbaa_ccb_bst;
133 bus_space_subregion(sc->sc_bst, ccbaa->ccbaa_ccb_bsh,
134 loc->loc_offset, loc->loc_size, &sc->sc_bsh);
135
136 aprint_naive("\n");
137 aprint_normal(": Random Number Generator (%u bit FIFO)\n",
138 32 * bcmrng_read_4(sc, RNG_FF_THRESHOLD));
139
140 sc->sc_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_SOFTCLOCK);
141
142 callout_init(&sc->sc_rnd_callout, CALLOUT_MPSAFE);
143
144 rnd_attach_source(&sc->sc_rnd_source, device_xname(self), RND_TYPE_RNG,
145 RND_FLAG_NO_ESTIMATE);
146
147 #ifdef RNG_USE_INTR
148 sc->sc_ih = intr_establish(loc->loc_intrs[0], IPL_VM, IST_LEVEL,
149 bcmrng_intr, sc);
150 if (sc->sc_ih == NULL) {
151 aprint_error_dev(self, "failed to establish interrupt %d\n",
152 loc->loc_intrs[0]);
153 } else {
154 aprint_normal_dev(self, "interrupting on irq %d\n", loc->loc_intrs[0]);
155 }
156 #else
157 bcmrng_callout(sc);
158 #endif
159 }
160
161 size_t
162 bcmrng_empty(struct bcmrng_softc *sc)
163 {
164 mutex_enter(sc->sc_lock);
165 size_t nwords = __SHIFTOUT(bcmrng_read_4(sc, RNG_STATUS), RNG_VAL);
166 if (nwords == 0) {
167 mutex_exit(sc->sc_lock);
168 return 0;
169 }
170
171 uint32_t data[nwords];
172
173 bcmrng_read_multi_4(sc, RNG_DATA, data, nwords);
174
175 rnd_add_data(&sc->sc_rnd_source, data, sizeof(data), sizeof(data) * 8);
176 mutex_exit(sc->sc_lock);
177 //aprint_debug_dev(sc->sc_dev, "added %zu words\n", nwords);
178 return nwords;
179 }
180
181 void
182 bcmrng_callout(void *arg)
183 {
184 struct bcmrng_softc * const sc = arg;
185
186 bcmrng_empty(sc);
187
188 callout_reset(&sc->sc_rnd_callout, 1, bcmrng_callout, sc);
189 }
190
191 #ifdef RNG_USE_INTR
192 int
193 bcmrng_intr(void *arg)
194 {
195 struct bcmrng_softc * const sc = arg;
196
197 sc->sc_intr_amount += bcmrng_empty(sc);
198 if (sc->sc_intr_amount >= 0x10000) {
199 bcmrng_callout(sc);
200 bcmrng_write_4(sc, RNG_INT_MASK, RNG_INT_OFF);
201 }
202 return 1;
203 }
204 #endif
205