bcm2835_rng.c revision 1.3
1/*	$NetBSD: bcm2835_rng.c,v 1.3 2013/02/01 16:10:16 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.3 2013/02/01 16:10:16 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
42#include <arm/broadcom/bcm_amba.h>
43#include <arm/broadcom/bcm2835reg.h>
44#include <arm/broadcom/bcm2835_intr.h>
45
46#define RNG_CTRL		0x00
47#define  RNG_CTRL_EN		__BIT(0)
48#define RNG_STATUS		0x04
49#define  RNG_STATUS_CNT_MASK	__BITS(31,24)
50#define  RNG_STATUS_CNT_SHIFT	24
51#define RNG_DATA		0x08
52
53#define RNG_DATA_MAX		256
54
55struct bcm2835rng_softc {
56	device_t sc_dev;
57
58	bus_space_tag_t sc_iot;
59	bus_space_handle_t sc_ioh;
60
61	krndsource_t sc_rnd;
62	callout_t sc_tick;
63
64	uint32_t sc_data[RNG_DATA_MAX];
65};
66
67static int bcmrng_match(device_t, cfdata_t, void *);
68static void bcmrng_attach(device_t, device_t, void *);
69
70static void bcmrng_tick(void *);
71
72CFATTACH_DECL_NEW(bcmrng_amba, sizeof(struct bcm2835rng_softc),
73    bcmrng_match, bcmrng_attach, NULL, NULL);
74
75/* ARGSUSED */
76static int
77bcmrng_match(device_t parent, cfdata_t match, void *aux)
78{
79	struct amba_attach_args *aaa = aux;
80
81	if (strcmp(aaa->aaa_name, "bcmrng") != 0)
82		return 0;
83
84	return 1;
85}
86
87static void
88bcmrng_attach(device_t parent, device_t self, void *aux)
89{
90        struct bcm2835rng_softc *sc = device_private(self);
91 	struct amba_attach_args *aaa = aux;
92	uint32_t ctrl;
93
94	aprint_naive("\n");
95	aprint_normal(": RNG\n");
96
97	sc->sc_dev = self;
98	sc->sc_iot = aaa->aaa_iot;
99
100	if (bus_space_map(aaa->aaa_iot, aaa->aaa_addr, BCM2835_RNG_SIZE, 0,
101	    &sc->sc_ioh)) {
102		aprint_error_dev(sc->sc_dev, "unable to map device\n");
103		return;
104	}
105
106	rnd_attach_source(&sc->sc_rnd, device_xname(self), RND_TYPE_RNG,
107	    RND_FLAG_NO_ESTIMATE);
108
109	callout_init(&sc->sc_tick, CALLOUT_MPSAFE);
110	callout_setfunc(&sc->sc_tick, bcmrng_tick, sc);
111
112	/* discard initial numbers, broadcom says they are "less random" */
113	bus_space_write_4(sc->sc_iot, sc->sc_ioh, RNG_STATUS, 0x40000);
114
115	/* enable rng */
116	ctrl = bus_space_read_4(sc->sc_iot, sc->sc_ioh, RNG_CTRL);
117	ctrl |= RNG_CTRL_EN;
118	bus_space_write_4(sc->sc_iot, sc->sc_ioh, RNG_CTRL, ctrl);
119
120	/* start timer */
121	bcmrng_tick(sc);
122}
123
124static void
125bcmrng_tick(void *priv)
126{
127	struct bcm2835rng_softc *sc = priv;
128	uint32_t status;
129	int cnt;
130
131	status = bus_space_read_4(sc->sc_iot, sc->sc_ioh, RNG_STATUS);
132	cnt = (status & RNG_STATUS_CNT_MASK) >> RNG_STATUS_CNT_SHIFT;
133	if (cnt > 0) {
134		bus_space_read_multi_4(sc->sc_iot, sc->sc_ioh, RNG_DATA,
135		    sc->sc_data, cnt);
136		rnd_add_data(&sc->sc_rnd, sc->sc_data,
137		    cnt * 4, cnt * 4 * NBBY);
138	}
139
140	callout_schedule(&sc->sc_tick, 1);
141}
142