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