bcm2835_rng.c revision 1.10
1/*	$NetBSD: bcm2835_rng.c,v 1.10 2014/08/10 16:44:33 tls Exp $ */
2
3/*-
4 * Copyright (c) 2013 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jared D. McNeill
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32#include <sys/cdefs.h>
33__KERNEL_RCSID(0, "$NetBSD: bcm2835_rng.c,v 1.10 2014/08/10 16:44:33 tls Exp $");
34
35#include <sys/param.h>
36#include <sys/systm.h>
37#include <sys/device.h>
38#include <sys/kernel.h>
39#include <sys/bus.h>
40#include <sys/rnd.h>
41#include <sys/atomic.h>
42#include <sys/intr.h>
43
44#include <arm/broadcom/bcm_amba.h>
45#include <arm/broadcom/bcm2835reg.h>
46#include <arm/broadcom/bcm2835_intr.h>
47
48#define RNG_CTRL		0x00
49#define  RNG_CTRL_EN		__BIT(0)
50#define RNG_STATUS		0x04
51#define  RNG_STATUS_CNT		__BITS(31,24)
52#define RNG_DATA		0x08
53
54#define RNG_DATA_MAX		256
55
56struct bcm2835rng_softc {
57	device_t		sc_dev;
58
59	bus_space_tag_t		sc_iot;
60	bus_space_handle_t	sc_ioh;
61
62	kmutex_t		sc_intr_lock;
63	unsigned int		sc_bytes_wanted;
64	void 			*sc_sih;
65
66	kmutex_t		sc_rnd_lock;
67	krndsource_t		sc_rndsource;
68};
69
70static int bcmrng_match(device_t, cfdata_t, void *);
71static void bcmrng_attach(device_t, device_t, void *);
72static void bcmrng_get(struct bcm2835rng_softc *);
73static void bcmrng_get_cb(size_t, void *);
74static void bcmrng_get_intr(void *);
75
76CFATTACH_DECL_NEW(bcmrng_amba, sizeof(struct bcm2835rng_softc),
77    bcmrng_match, bcmrng_attach, NULL, NULL);
78
79/* ARGSUSED */
80static int
81bcmrng_match(device_t parent, cfdata_t match, void *aux)
82{
83	struct amba_attach_args *aaa = aux;
84
85	if (strcmp(aaa->aaa_name, "bcmrng") != 0)
86		return 0;
87
88	return 1;
89}
90
91static void
92bcmrng_attach(device_t parent, device_t self, void *aux)
93{
94	struct bcm2835rng_softc *sc = device_private(self);
95 	struct amba_attach_args *aaa = aux;
96	uint32_t ctrl;
97
98	aprint_naive("\n");
99	aprint_normal(": RNG\n");
100
101	sc->sc_dev = self;
102	sc->sc_iot = aaa->aaa_iot;
103
104	if (bus_space_map(aaa->aaa_iot, aaa->aaa_addr, BCM2835_RNG_SIZE, 0,
105	    &sc->sc_ioh)) {
106		aprint_error_dev(sc->sc_dev, "unable to map device\n");
107		goto fail0;
108	}
109
110	/* discard initial numbers, broadcom says they are "less random" */
111	bus_space_write_4(sc->sc_iot, sc->sc_ioh, RNG_STATUS, 0x40000);
112
113	/* enable rng */
114	ctrl = bus_space_read_4(sc->sc_iot, sc->sc_ioh, RNG_CTRL);
115	ctrl |= RNG_CTRL_EN;
116	bus_space_write_4(sc->sc_iot, sc->sc_ioh, RNG_CTRL, ctrl);
117
118	/* set up a softint for adding data */
119	mutex_init(&sc->sc_intr_lock, MUTEX_DEFAULT, IPL_SERIAL);
120	sc->sc_bytes_wanted = 0;
121	sc->sc_sih = softint_establish(SOFTINT_SERIAL|SOFTINT_MPSAFE,
122	    &bcmrng_get_intr, sc);
123	if (sc->sc_sih == NULL) {
124		aprint_error_dev(sc->sc_dev, "unable to establish softint");
125		goto fail1;
126	}
127
128	/* set up an rndsource */
129	mutex_init(&sc->sc_rnd_lock, MUTEX_DEFAULT, IPL_SERIAL);
130	rndsource_setcb(&sc->sc_rndsource, &bcmrng_get_cb, sc);
131	rnd_attach_source(&sc->sc_rndsource, device_xname(self), RND_TYPE_RNG,
132	    RND_FLAG_COLLECT_VALUE|RND_FLAG_HASCB);
133
134	/* get some initial entropy ASAP */
135	bcmrng_get_cb(RND_POOLBITS / NBBY, sc);
136
137	/* Success!  */
138	return;
139
140fail1:	mutex_destroy(&sc->sc_intr_lock);
141	bus_space_unmap(aaa->aaa_iot, sc->sc_ioh, BCM2835_RNG_SIZE);
142fail0:	return;
143}
144
145static void
146bcmrng_get(struct bcm2835rng_softc *sc)
147{
148	uint32_t status, cnt;
149	uint32_t buf[RNG_DATA_MAX]; /* 1k on the stack */
150
151	mutex_spin_enter(&sc->sc_intr_lock);
152	while (sc->sc_bytes_wanted) {
153		status = bus_space_read_4(sc->sc_iot, sc->sc_ioh, RNG_STATUS);
154		cnt = __SHIFTOUT(status, RNG_STATUS_CNT);
155		KASSERT(cnt < RNG_DATA_MAX);
156		if (cnt == 0)
157			continue;	/* XXX Busy-waiting seems wrong...  */
158		bus_space_read_multi_4(sc->sc_iot, sc->sc_ioh, RNG_DATA, buf,
159		    cnt);
160
161		/*
162		 * This lock dance is necessary because rnd_add_data
163		 * may call bcmrng_get_cb which takes the intr lock.
164		 */
165		mutex_spin_exit(&sc->sc_intr_lock);
166		mutex_spin_enter(&sc->sc_rnd_lock);
167		rnd_add_data(&sc->sc_rndsource, buf, (cnt * 4),
168		    (cnt * 4 * NBBY));
169		mutex_spin_exit(&sc->sc_rnd_lock);
170		mutex_spin_enter(&sc->sc_intr_lock);
171		sc->sc_bytes_wanted -= MIN(sc->sc_bytes_wanted, (cnt * 4));
172	}
173	explicit_memset(buf, 0, sizeof(buf));
174	mutex_spin_exit(&sc->sc_intr_lock);
175}
176
177static void
178bcmrng_get_cb(size_t bytes_wanted, void *arg)
179{
180	struct bcm2835rng_softc *sc = arg;
181
182	/*
183	 * Deferring to a softint is necessary until the rnd(9) locking
184	 * is fixed.
185	 */
186	mutex_spin_enter(&sc->sc_intr_lock);
187	if (sc->sc_bytes_wanted == 0)
188		softint_schedule(sc->sc_sih);
189	if (bytes_wanted > (UINT_MAX - sc->sc_bytes_wanted))
190		sc->sc_bytes_wanted = UINT_MAX;
191	else
192		sc->sc_bytes_wanted += bytes_wanted;
193	mutex_spin_exit(&sc->sc_intr_lock);
194}
195
196static void
197bcmrng_get_intr(void *arg)
198{
199	struct bcm2835rng_softc *const sc = arg;
200
201	bcmrng_get(sc);
202}
203