bcm2835_rng.c revision 1.5
1/*	$NetBSD: bcm2835_rng.c,v 1.5 2013/08/01 11:29:47 skrll 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.5 2013/08/01 11:29:47 skrll 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
43#include <arm/broadcom/bcm_amba.h>
44#include <arm/broadcom/bcm2835reg.h>
45#include <arm/broadcom/bcm2835_intr.h>
46
47#define RNG_CTRL		0x00
48#define  RNG_CTRL_EN		__BIT(0)
49#define RNG_STATUS		0x04
50#define  RNG_STATUS_CNT_MASK	__BITS(31,24)
51#define  RNG_STATUS_CNT_SHIFT	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	krndsource_t sc_rnd;
63
64	kmutex_t sc_mutex;
65
66	uint32_t sc_data[RNG_DATA_MAX];
67};
68
69static void bcmrng_get(size_t, void *);
70static int bcmrng_match(device_t, cfdata_t, void *);
71static void bcmrng_attach(device_t, device_t, void *);
72
73CFATTACH_DECL_NEW(bcmrng_amba, sizeof(struct bcm2835rng_softc),
74    bcmrng_match, bcmrng_attach, NULL, NULL);
75
76/* ARGSUSED */
77static int
78bcmrng_match(device_t parent, cfdata_t match, void *aux)
79{
80	struct amba_attach_args *aaa = aux;
81
82	if (strcmp(aaa->aaa_name, "bcmrng") != 0)
83		return 0;
84
85	return 1;
86}
87
88static void
89bcmrng_attach(device_t parent, device_t self, void *aux)
90{
91        struct bcm2835rng_softc *sc = device_private(self);
92 	struct amba_attach_args *aaa = aux;
93	uint32_t ctrl;
94
95	aprint_naive("\n");
96	aprint_normal(": RNG\n");
97
98	sc->sc_dev = self;
99	sc->sc_iot = aaa->aaa_iot;
100
101	if (bus_space_map(aaa->aaa_iot, aaa->aaa_addr, BCM2835_RNG_SIZE, 0,
102	    &sc->sc_ioh)) {
103		aprint_error_dev(sc->sc_dev, "unable to map device\n");
104		return;
105	}
106
107	mutex_init(&sc->sc_mutex, MUTEX_DEFAULT, IPL_VM);
108
109	rndsource_setcb(&sc->sc_rnd, bcmrng_get, sc);
110	rnd_attach_source(&sc->sc_rnd, device_xname(self), RND_TYPE_RNG,
111	    RND_FLAG_NO_ESTIMATE|RND_FLAG_HASCB);
112
113	/* discard initial numbers, broadcom says they are "less random" */
114	bus_space_write_4(sc->sc_iot, sc->sc_ioh, RNG_STATUS, 0x40000);
115
116	/* enable rng */
117	ctrl = bus_space_read_4(sc->sc_iot, sc->sc_ioh, RNG_CTRL);
118	ctrl |= RNG_CTRL_EN;
119	bus_space_write_4(sc->sc_iot, sc->sc_ioh, RNG_CTRL, ctrl);
120}
121
122static void
123bcmrng_get(size_t bytes, void *priv)
124{
125        struct bcm2835rng_softc *sc = priv;
126	uint32_t status;
127	int need = bytes, cnt;
128
129        mutex_spin_enter(&sc->sc_mutex);
130
131	if (__predict_false(need < 1)) {
132		return;
133	}
134
135	while (need > 0) {
136		status = bus_space_read_4(sc->sc_iot, sc->sc_ioh, RNG_STATUS);
137		cnt = (status & RNG_STATUS_CNT_MASK) >> RNG_STATUS_CNT_SHIFT;
138		if (cnt > 0) {
139			bus_space_read_multi_4(sc->sc_iot, sc->sc_ioh,
140					       RNG_DATA, sc->sc_data, cnt);
141			rnd_add_data(&sc->sc_rnd, sc->sc_data,
142		    		     cnt * 4, cnt * 4 * NBBY);
143		}
144
145		need -= cnt * 4;
146	}
147	mutex_spin_exit(&sc->sc_mutex);
148}
149