xref: /netbsd-src/sys/arch/arm/amlogic/meson_rng.c (revision 53b02e147d4ed531c0d2a5ca9b3e8026ba3e99b5)
1 /* $NetBSD: meson_rng.c,v 1.4 2021/01/27 03:10:18 thorpej Exp $ */
2 
3 /*-
4  * Copyright (c) 2015-2019 Jared D. McNeill <jmcneill@invisible.ca>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: meson_rng.c,v 1.4 2021/01/27 03:10:18 thorpej Exp $");
31 
32 #include <sys/param.h>
33 #include <sys/bus.h>
34 #include <sys/device.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/mutex.h>
38 #include <sys/rndsource.h>
39 
40 #include <dev/fdt/fdtvar.h>
41 
42 static int	meson_rng_match(device_t, cfdata_t, void *);
43 static void	meson_rng_attach(device_t, device_t, void *);
44 
45 static void	meson_rng_get(size_t, void *);
46 
47 struct meson_rng_softc {
48 	device_t		sc_dev;
49 	bus_space_tag_t		sc_bst;
50 	bus_space_handle_t	sc_bsh;
51 
52 	kmutex_t		sc_lock;
53 	krndsource_t		sc_rndsource;
54 };
55 
56 static const struct device_compatible_entry compat_data[] = {
57 	{ .compat = "amlogic,meson-rng" },
58 	DEVICE_COMPAT_EOL
59 };
60 
61 CFATTACH_DECL_NEW(meson_rng, sizeof(struct meson_rng_softc),
62 	meson_rng_match, meson_rng_attach, NULL, NULL);
63 
64 static int
65 meson_rng_match(device_t parent, cfdata_t cf, void *aux)
66 {
67 	struct fdt_attach_args * const faa = aux;
68 
69 	return of_compatible_match(faa->faa_phandle, compat_data);
70 }
71 
72 static void
73 meson_rng_attach(device_t parent, device_t self, void *aux)
74 {
75 	struct meson_rng_softc * const sc = device_private(self);
76 	struct fdt_attach_args * const faa = aux;
77 	const int phandle = faa->faa_phandle;
78 	struct clk *clk;
79 	bus_addr_t addr;
80 	bus_size_t size;
81 
82 	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
83 		aprint_error(": couldn't get registers\n");
84 		return;
85 	}
86 
87 	sc->sc_dev = self;
88 	sc->sc_bst = faa->faa_bst;
89 	if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) {
90 		aprint_error(": couldn't map registers\n");
91 		return;
92 	}
93 
94 	mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_VM);
95 
96 	/* Core clock is optional */
97 	clk = fdtbus_clock_get(phandle, "core");
98 	if (clk != NULL && clk_enable(clk) != 0) {
99 		aprint_error(": couldn't enable core clock\n");
100 		return;
101 	}
102 
103 	aprint_naive("\n");
104 	aprint_normal(": Hardware RNG\n");
105 
106 	rndsource_setcb(&sc->sc_rndsource, meson_rng_get, sc);
107 	rnd_attach_source(&sc->sc_rndsource, device_xname(self), RND_TYPE_RNG,
108 	    RND_FLAG_COLLECT_VALUE|RND_FLAG_HASCB);
109 }
110 
111 static void
112 meson_rng_get(size_t bytes_wanted, void *priv)
113 {
114 	struct meson_rng_softc * const sc = priv;
115 	uint32_t data;
116 
117 	mutex_spin_enter(&sc->sc_lock);
118 	while (bytes_wanted) {
119 		data = bus_space_read_4(sc->sc_bst, sc->sc_bsh, 0);
120 		rnd_add_data_sync(&sc->sc_rndsource, &data, sizeof(data),
121 		    sizeof(data) * NBBY);
122 		bytes_wanted -= MIN(bytes_wanted, sizeof(data));
123 	}
124 	explicit_memset(&data, 0, sizeof(data));
125 	mutex_spin_exit(&sc->sc_lock);
126 }
127