ti_rng.c revision 1.5
1/* $NetBSD: ti_rng.c,v 1.5 2021/01/27 03:10:20 thorpej 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.5 2021/01/27 03:10:20 thorpej 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
44static const struct device_compatible_entry compat_data[] = {
45	{ .compat = "ti,omap4-rng" },
46	DEVICE_COMPAT_EOL
47};
48
49struct ti_rng_softc {
50	device_t sc_dev;
51	bus_space_tag_t sc_iot;
52	bus_space_handle_t sc_ioh;
53
54	kmutex_t sc_lock;
55	krndsource_t sc_rndsource;
56};
57
58static int	ti_rng_match(device_t, cfdata_t, void *);
59static void	ti_rng_attach(device_t, device_t, void *);
60static void	ti_rng_callback(size_t, void *);
61
62CFATTACH_DECL_NEW(ti_rng, sizeof(struct ti_rng_softc),
63    ti_rng_match, ti_rng_attach, NULL, NULL);
64
65#define RD4(sc, reg) \
66	bus_space_read_4((sc)->sc_iot, (sc)->sc_ioh, (reg))
67#define WR4(sc, reg, val) \
68	bus_space_write_4((sc)->sc_iot, (sc)->sc_ioh, (reg), (val))
69
70static int
71ti_rng_match(device_t parent, cfdata_t match, void *aux)
72{
73	struct fdt_attach_args * const faa = aux;
74
75	return of_compatible_match(faa->faa_phandle, compat_data);
76}
77
78static void
79ti_rng_attach(device_t parent, device_t self, void *aux)
80{
81	struct ti_rng_softc *sc = device_private(self);
82	struct fdt_attach_args * const faa = aux;
83	const int phandle = faa->faa_phandle;
84	bus_addr_t addr;
85	bus_size_t size;
86
87	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
88		aprint_error(": couldn't get registers\n");
89		return;
90	}
91
92	if (ti_prcm_enable_hwmod(phandle, 0) != 0) {
93		aprint_error(": couldn't enable module\n");
94		return;
95	}
96
97	sc->sc_dev = self;
98	sc->sc_iot = faa->faa_bst;
99	if (bus_space_map(sc->sc_iot, addr, size, 0, &sc->sc_ioh) != 0) {
100		aprint_error(": couldn't map registers\n");
101		return;
102	}
103
104	mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_VM);
105
106	if ((RD4(sc, TRNG_CONTROL_REG) & TRNG_CONTROL_ENABLE) == 0) {
107		WR4(sc, TRNG_CONFIG_REG,
108		    __SHIFTIN(0x21, TRNG_CONFIG_MIN_REFILL) |
109		    __SHIFTIN(0x22, TRNG_CONFIG_MAX_REFILL));
110		WR4(sc, TRNG_CONTROL_REG,
111		    __SHIFTIN(0x21, TRNG_CONTROL_STARTUP_CYCLES) |
112		    TRNG_CONTROL_ENABLE);
113	}
114
115	aprint_naive("\n");
116	aprint_normal(": RNG\n");
117
118	rndsource_setcb(&sc->sc_rndsource, ti_rng_callback, sc);
119	rnd_attach_source(&sc->sc_rndsource, device_xname(self), RND_TYPE_RNG,
120	    RND_FLAG_COLLECT_VALUE|RND_FLAG_HASCB);
121}
122
123static void
124ti_rng_callback(size_t bytes_wanted, void *priv)
125{
126	struct ti_rng_softc * const sc = priv;
127	uint32_t buf[2];
128	u_int retry;
129
130	mutex_enter(&sc->sc_lock);
131	while (bytes_wanted) {
132		for (retry = 10; retry > 0; retry--) {
133			if (RD4(sc, TRNG_STATUS_REG) & TRNG_STATUS_READY)
134				break;
135			delay(10);
136		}
137		if (retry == 0)
138			break;
139		buf[0] = RD4(sc, TRNG_OUTPUT_L_REG);
140		buf[1] = RD4(sc, TRNG_OUTPUT_H_REG);
141		WR4(sc, TRNG_INTACK_REG, TRNG_INTACK_READY);
142		rnd_add_data_sync(&sc->sc_rndsource, buf, sizeof(buf),
143		    sizeof(buf) * NBBY);
144		bytes_wanted -= MIN(bytes_wanted, sizeof(buf));
145	}
146	explicit_memset(buf, 0, sizeof(buf));
147	mutex_exit(&sc->sc_lock);
148}
149