xref: /netbsd/sys/arch/arm/ti/ti_rng.c (revision fa5a7cd4)
1 /* $NetBSD: ti_rng.c,v 1.7 2022/03/19 11:55:03 riastradh Exp $ */
2 
3 /*-
4  * Copyright (c) 2015 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. The name of the author may not be used to endorse or promote products
13  *    derived from this software without specific prior written permission.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
20  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
22  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 #include <sys/cdefs.h>
29 __KERNEL_RCSID(0, "$NetBSD: ti_rng.c,v 1.7 2022/03/19 11:55:03 riastradh Exp $");
30 
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/device.h>
34 #include <sys/conf.h>
35 #include <sys/mutex.h>
36 #include <sys/bus.h>
37 #include <sys/rndsource.h>
38 
39 #include <dev/fdt/fdtvar.h>
40 
41 #include <arm/ti/ti_prcm.h>
42 #include <arm/ti/ti_rngreg.h>
43 
44 static const struct device_compatible_entry compat_data[] = {
45 	{ .compat = "ti,omap4-rng" },
46 	DEVICE_COMPAT_EOL
47 };
48 
49 struct ti_rng_softc {
50 	device_t sc_dev;
51 	bus_space_tag_t sc_iot;
52 	bus_space_handle_t sc_ioh;
53 
54 	krndsource_t sc_rndsource;
55 };
56 
57 static int	ti_rng_match(device_t, cfdata_t, void *);
58 static void	ti_rng_attach(device_t, device_t, void *);
59 static void	ti_rng_callback(size_t, void *);
60 
61 CFATTACH_DECL_NEW(ti_rng, sizeof(struct ti_rng_softc),
62     ti_rng_match, ti_rng_attach, NULL, NULL);
63 
64 #define RD4(sc, reg) \
65 	bus_space_read_4((sc)->sc_iot, (sc)->sc_ioh, (reg))
66 #define WR4(sc, reg, val) \
67 	bus_space_write_4((sc)->sc_iot, (sc)->sc_ioh, (reg), (val))
68 
69 static int
ti_rng_match(device_t parent,cfdata_t match,void * aux)70 ti_rng_match(device_t parent, cfdata_t match, void *aux)
71 {
72 	struct fdt_attach_args * const faa = aux;
73 
74 	return of_compatible_match(faa->faa_phandle, compat_data);
75 }
76 
77 static void
ti_rng_attach(device_t parent,device_t self,void * aux)78 ti_rng_attach(device_t parent, device_t self, void *aux)
79 {
80 	struct ti_rng_softc *sc = device_private(self);
81 	struct fdt_attach_args * const faa = aux;
82 	const int phandle = faa->faa_phandle;
83 	bus_addr_t addr;
84 	bus_size_t size;
85 
86 	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
87 		aprint_error(": couldn't get registers\n");
88 		return;
89 	}
90 
91 	if (ti_prcm_enable_hwmod(phandle, 0) != 0) {
92 		aprint_error(": couldn't enable module\n");
93 		return;
94 	}
95 
96 	sc->sc_dev = self;
97 	sc->sc_iot = faa->faa_bst;
98 	if (bus_space_map(sc->sc_iot, addr, size, 0, &sc->sc_ioh) != 0) {
99 		aprint_error(": couldn't map registers\n");
100 		return;
101 	}
102 
103 	if ((RD4(sc, TRNG_CONTROL_REG) & TRNG_CONTROL_ENABLE) == 0) {
104 		WR4(sc, TRNG_CONFIG_REG,
105 		    __SHIFTIN(0x21, TRNG_CONFIG_MIN_REFILL) |
106 		    __SHIFTIN(0x22, TRNG_CONFIG_MAX_REFILL));
107 		WR4(sc, TRNG_CONTROL_REG,
108 		    __SHIFTIN(0x21, TRNG_CONTROL_STARTUP_CYCLES) |
109 		    TRNG_CONTROL_ENABLE);
110 	}
111 
112 	aprint_naive("\n");
113 	aprint_normal(": RNG\n");
114 
115 	rndsource_setcb(&sc->sc_rndsource, ti_rng_callback, sc);
116 	rnd_attach_source(&sc->sc_rndsource, device_xname(self), RND_TYPE_RNG,
117 	    RND_FLAG_COLLECT_VALUE|RND_FLAG_HASCB);
118 }
119 
120 static void
ti_rng_callback(size_t bytes_wanted,void * priv)121 ti_rng_callback(size_t bytes_wanted, void *priv)
122 {
123 	struct ti_rng_softc * const sc = priv;
124 	uint32_t buf[2];
125 	u_int retry;
126 
127 	while (bytes_wanted) {
128 		for (retry = 10; retry > 0; retry--) {
129 			if (RD4(sc, TRNG_STATUS_REG) & TRNG_STATUS_READY)
130 				break;
131 			delay(10);
132 		}
133 		if (retry == 0)
134 			break;
135 		buf[0] = RD4(sc, TRNG_OUTPUT_L_REG);
136 		buf[1] = RD4(sc, TRNG_OUTPUT_H_REG);
137 		WR4(sc, TRNG_INTACK_REG, TRNG_INTACK_READY);
138 		rnd_add_data_sync(&sc->sc_rndsource, buf, sizeof(buf),
139 		    sizeof(buf) * NBBY);
140 		bytes_wanted -= MIN(bytes_wanted, sizeof(buf));
141 	}
142 	explicit_memset(buf, 0, sizeof(buf));
143 }
144