xref: /netbsd-src/sys/dev/ic/rng200.c (revision 8c13a1c7ef4620e09ff6a8140f557e096d0ce089)
1 /*	$NetBSD: rng200.c,v 1.4 2022/03/19 11:55:03 riastradh Exp $	*/
2 
3 /*
4  * Copyright (c) 2019 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Michael van Elst
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 /*
33  * Driver for the Broadcom iProc RNG200
34  */
35 
36 #include <sys/param.h>
37 #include <sys/bus.h>
38 #include <sys/mutex.h>
39 #include <sys/rndsource.h>
40 
41 #include <dev/ic/rng200var.h>
42 #include <dev/ic/rng200reg.h>
43 
44 #define READ4(sc, r) \
45 	bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (r))
46 
47 #define WRITE4(sc, r, v) \
48 	bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (r), (v))
49 
50 static void
rng200_reset(struct rng200_softc * sc)51 rng200_reset(struct rng200_softc *sc)
52 {
53 	uint32_t ctl, rng, rbg;
54 
55 	/* Disable RBG */
56 	ctl = READ4(sc, RNG200_CONTROL);
57 	ctl &= ~RNG200_RBG_MASK;
58 	WRITE4(sc, RNG200_CONTROL, ctl);
59 
60 	/* Clear interrupts */
61 	WRITE4(sc, RNG200_STATUS, 0xffffffff);
62 
63 	/* Reset RNG and RBG */
64 	rbg = READ4(sc, RNG200_RBG_RESET);
65 	rng = READ4(sc, RNG200_RNG_RESET);
66 	WRITE4(sc, RNG200_RBG_RESET, rbg | RBG_RESET);
67 	WRITE4(sc, RNG200_RNG_RESET, rng | RNG_RESET);
68 	WRITE4(sc, RNG200_RNG_RESET, rng);
69 	WRITE4(sc, RNG200_RBG_RESET, rbg);
70 
71 	/* Enable RBG */
72 	WRITE4(sc, RNG200_CONTROL, ctl | RNG200_RBG_ENABLE);
73 }
74 
75 static void
rng200_get(size_t bytes_wanted,void * priv)76 rng200_get(size_t bytes_wanted, void *priv)
77 {
78 	struct rng200_softc * const sc = priv;
79 	uint32_t w, data;
80 	unsigned count;
81 
82 	while (bytes_wanted) {
83 
84 		w = READ4(sc, RNG200_STATUS);
85 		if ((w & (RNG200_MASTER_FAIL | RNG200_NIST_FAIL)) != 0)
86 			rng200_reset(sc);
87 
88 		w = READ4(sc, RNG200_COUNT);
89 		count = __SHIFTOUT(w, RNG200_COUNT_MASK);
90 
91 		if (count == 0)
92 			break;
93 
94 		data = READ4(sc, RNG200_DATA);
95 		rnd_add_data_sync(&sc->sc_rndsource, &data,
96 		    sizeof(data), sizeof(data) * NBBY);
97 		bytes_wanted -= MIN(bytes_wanted, sizeof(data));
98 	}
99 	explicit_memset(&data, 0, sizeof(data));
100 }
101 
102 void
rng200_attach(struct rng200_softc * sc)103 rng200_attach(struct rng200_softc *sc)
104 {
105 
106 	rndsource_setcb(&sc->sc_rndsource, rng200_get, sc);
107 	rnd_attach_source(&sc->sc_rndsource, sc->sc_name,
108 		RND_TYPE_RNG, RND_FLAG_COLLECT_VALUE|RND_FLAG_HASCB);
109 }
110 
111 void
rng200_detach(struct rng200_softc * sc)112 rng200_detach(struct rng200_softc *sc)
113 {
114 
115 	rnd_detach_source(&sc->sc_rndsource);
116 }
117 
118