Home | History | Annotate | Line # | Download | only in starfive
jh7110_trng.c revision 1.1
      1 /* $NetBSD: jh7110_trng.c,v 1.1 2025/02/08 16:12:20 skrll Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2025 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Nick Hudson
      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 #include <sys/cdefs.h>
     33 __KERNEL_RCSID(0, "$NetBSD: jh7110_trng.c,v 1.1 2025/02/08 16:12:20 skrll Exp $");
     34 
     35 #include <sys/param.h>
     36 
     37 #include <sys/bus.h>
     38 #include <sys/device.h>
     39 #include <sys/condvar.h>
     40 #include <sys/mutex.h>
     41 #include <sys/rndsource.h>
     42 
     43 #include <dev/fdt/fdtvar.h>
     44 
     45 
     46 struct jh7110_trng_softc {
     47 	device_t		sc_dev;
     48 	bus_space_tag_t		sc_bst;
     49 	bus_space_handle_t	sc_bsh;
     50 	int			sc_phandle;
     51 
     52 	kmutex_t		sc_lock;
     53 	kcondvar_t		sc_cv;
     54 	void *			sc_ih;
     55 	bool			sc_reseeddone;
     56 	size_t			sc_bytes_wanted;
     57 
     58 	krndsource_t		sc_rndsource;
     59 };
     60 
     61 
     62 #define RD4(sc, reg)							       \
     63 	bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
     64 #define WR4(sc, reg, val)						       \
     65 	bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
     66 
     67 
     68 /* Register definitions */
     69 // https://doc-en.rvspace.org/JH7110/TRM/JH7110_TRM/control_registers_trng.html?hl=trng
     70 #define JH7110_TRNG_CTRL		0x0000
     71 #define  JH7110_TRNG_CTRL_NOP			0x0
     72 #define  JH7110_TRNG_CTRL_RANDOMIZE		0x1
     73 #define  JH7110_TRNG_CTRL_RANDOM_RESEED		0x2
     74 #define  JH7110_TRNG_CTRL_NONCE_RESEED		0x3
     75 #define JH7110_TRNG_STAT		0x0004
     76 #define  JH7110_TRNG_STAT_SEEDED		__BIT(9)
     77 
     78 //XXXNH check
     79 #define JH7110_TRNG_MODE		0x0008
     80 #define  JH7110_TRNG_MODE_R256			__BIT(3)
     81 #define JH7110_TRNG_SMODE		0x000c
     82 #define JH7110_TRNG_IENABLE		0x0010
     83 #define  JH7110_TRNG_IENABLE_GLOBAL		__BIT(31)
     84 #define  JH7110_TRNG_IENABLE_LFSR_LOCKUP	__BIT(4)
     85 #define  JH7110_TRNG_IENABLE_RQST_LOCKUP	__BIT(3)
     86 #define  JH7110_TRNG_IENABLE_AGE_ALARM		__BIT(2)
     87 #define  JH7110_TRNG_IENABLE_SEED_DONE		__BIT(1)
     88 #define  JH7110_TRNG_IENABLE_RAND_RDY		__BIT(0)
     89 #define JH7110_TRNG_ISTATUS		0x0014
     90 #define  JH7110_TRNG_ISTATUS_LFSR_LOCKUP	__BIT(4)
     91 #define  JH7110_TRNG_ISTATUS_RQST_LOCKUP	__BIT(3)
     92 #define  JH7110_TRNG_ISTATUS_AGE_ALARM		__BIT(2)
     93 #define  JH7110_TRNG_ISTATUS_SEED_DONE		__BIT(1)
     94 #define  JH7110_TRNG_ISTATUS_RAND_RDY		__BIT(0)
     95 #define JH7110_TRNG_DATA0		0x0020
     96 #define JH7110_TRNG_DATA1		0x0024
     97 #define JH7110_TRNG_DATA2		0x0028
     98 #define JH7110_TRNG_DATA3		0x002c
     99 #define JH7110_TRNG_DATA4		0x0030
    100 #define JH7110_TRNG_DATA5		0x0034
    101 #define JH7110_TRNG_DATA6		0x0038
    102 #define JH7110_TRNG_DATA7		0x003c
    103 
    104 
    105 #define RD4(sc, reg)							       \
    106 	bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
    107 #define WR4(sc, reg, val)						       \
    108 	bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
    109 
    110 
    111 static void
    112 jh7110_trng_irqenable(struct jh7110_trng_softc *sc)
    113 {
    114 	WR4(sc, JH7110_TRNG_IENABLE,
    115 	    JH7110_TRNG_IENABLE_GLOBAL |
    116 	    JH7110_TRNG_IENABLE_SEED_DONE |
    117 	    JH7110_TRNG_IENABLE_RAND_RDY |
    118 	    JH7110_TRNG_IENABLE_LFSR_LOCKUP);
    119 }
    120 
    121 static void
    122 jh7110_trng_irqdisable(struct jh7110_trng_softc *sc)
    123 {
    124 	WR4(sc, JH7110_TRNG_IENABLE, 0);
    125 }
    126 
    127 static void
    128 jh7110_trng_probe(struct jh7110_trng_softc *sc, uint32_t istat)
    129 {
    130 	KASSERT(mutex_owned(&sc->sc_lock));
    131 
    132 	if (sc->sc_bytes_wanted != 0) {
    133 		uint32_t data[8];
    134 		const uint32_t stat = RD4(sc, JH7110_TRNG_STAT);
    135 
    136 		if (stat & JH7110_TRNG_STAT_SEEDED) {
    137 			if (istat & JH7110_TRNG_ISTATUS_RAND_RDY) {
    138 
    139 				WR4(sc, JH7110_TRNG_ISTATUS,
    140 				    JH7110_TRNG_ISTATUS_RAND_RDY);
    141 
    142 				data[0] = RD4(sc, JH7110_TRNG_DATA0);
    143 				data[1] = RD4(sc, JH7110_TRNG_DATA1);
    144 				data[2] = RD4(sc, JH7110_TRNG_DATA2);
    145 				data[3] = RD4(sc, JH7110_TRNG_DATA3);
    146 				data[4] = RD4(sc, JH7110_TRNG_DATA4);
    147 				data[5] = RD4(sc, JH7110_TRNG_DATA5);
    148 				data[6] = RD4(sc, JH7110_TRNG_DATA6);
    149 				data[7] = RD4(sc, JH7110_TRNG_DATA7);
    150 
    151 				rnd_add_data_sync(&sc->sc_rndsource, &data,
    152 				    sizeof(data), sizeof(data) * NBBY);
    153 
    154 				sc->sc_bytes_wanted -=
    155 				    MIN(sc->sc_bytes_wanted, sizeof(data));
    156 
    157 				if (sc->sc_bytes_wanted == 0)
    158 					jh7110_trng_irqdisable(sc);
    159 			}
    160 		} else {
    161 			WR4(sc, JH7110_TRNG_CTRL,
    162 			    JH7110_TRNG_CTRL_RANDOM_RESEED);
    163 		}
    164 		explicit_memset(data, 0, sizeof data);
    165 	}
    166 	if (sc->sc_bytes_wanted != 0) {
    167 		WR4(sc, JH7110_TRNG_CTRL,
    168 		    JH7110_TRNG_CTRL_RANDOMIZE);
    169 	}
    170 }
    171 
    172 static void
    173 jh7110_trng_get(size_t bytes_wanted, void *arg)
    174 {
    175 	struct jh7110_trng_softc * const sc = arg;
    176 
    177 	mutex_enter(&sc->sc_lock);
    178 	sc->sc_bytes_wanted += bytes_wanted;
    179 
    180 	jh7110_trng_irqenable(sc);
    181 
    182 	const uint32_t istat = RD4(sc, JH7110_TRNG_ISTATUS);
    183 	jh7110_trng_probe(sc, istat);
    184 
    185 	mutex_exit(&sc->sc_lock);
    186 }
    187 
    188 
    189 static int
    190 jh7110_trng_intr(void *priv)
    191 {
    192 	struct jh7110_trng_softc * const sc = priv;
    193 
    194 	mutex_enter(&sc->sc_lock);
    195 
    196 	const uint32_t istat = RD4(sc, JH7110_TRNG_ISTATUS);
    197 
    198 	if (istat & JH7110_TRNG_ISTATUS_RAND_RDY) {
    199 		KASSERT(RD4(sc, JH7110_TRNG_STAT) & JH7110_TRNG_STAT_SEEDED);
    200 		jh7110_trng_probe(sc, istat);
    201 		//sc->sc_randready = true;
    202 
    203 	}
    204 
    205 	if (istat & JH7110_TRNG_ISTATUS_SEED_DONE)
    206 		sc->sc_reseeddone = true;
    207 
    208 #if 0
    209 	if (istat & JH7110_TRNG_ISTATUS_LFSR_LOCKUP) {
    210 		sc->sc_reseeddone = false;
    211 	}
    212 #endif
    213 	WR4(sc, JH7110_TRNG_ISTATUS, istat);
    214 
    215 	if (sc->sc_reseeddone)
    216 		cv_broadcast(&sc->sc_cv);
    217 
    218 	mutex_exit(&sc->sc_lock);
    219 
    220 	return 1;
    221 }
    222 
    223 
    224 static void
    225 jh7110_trng_init(struct jh7110_trng_softc *sc)
    226 {
    227 	/* Mask and clear all interrupts. */
    228 	WR4(sc, JH7110_TRNG_IENABLE,  0U);
    229 	WR4(sc, JH7110_TRNG_ISTATUS, ~0U);
    230 
    231 	WR4(sc, JH7110_TRNG_MODE, JH7110_TRNG_MODE_R256);
    232 
    233 	mutex_enter(&sc->sc_lock);
    234 
    235 	jh7110_trng_irqenable(sc);
    236 
    237 	sc->sc_reseeddone = false;
    238 	WR4(sc, JH7110_TRNG_CTRL, JH7110_TRNG_CTRL_RANDOM_RESEED);
    239 
    240 	while (!sc->sc_reseeddone) {
    241 		const int error = cv_timedwait(&sc->sc_cv, &sc->sc_lock, 1);
    242 		if (error) {
    243 			printf("%s: timedout\n", __func__);
    244 			mutex_exit(&sc->sc_lock);
    245 			return;
    246 		}
    247 	}
    248 	mutex_exit(&sc->sc_lock);
    249 }
    250 
    251 static void
    252 jh7110_trng_attach_i(device_t self)
    253 {
    254 	struct jh7110_trng_softc * const sc = device_private(self);
    255 
    256 	jh7110_trng_init(sc);
    257 
    258 	/* set up an rndsource */
    259 	rndsource_setcb(&sc->sc_rndsource, &jh7110_trng_get, sc);
    260 	rnd_attach_source(&sc->sc_rndsource, device_xname(self), RND_TYPE_RNG,
    261 	    RND_FLAG_COLLECT_VALUE | RND_FLAG_HASCB);
    262 }
    263 
    264 /* Compat string(s) */
    265 static const struct device_compatible_entry compat_data[] = {
    266 	{ .compat = "starfive,jh7110-trng" },
    267 	DEVICE_COMPAT_EOL
    268 };
    269 
    270 
    271 static int
    272 jh7110_trng_match(device_t parent, cfdata_t cf, void *aux)
    273 {
    274 	struct fdt_attach_args * const faa = aux;
    275 
    276 	return of_compatible_match(faa->faa_phandle, compat_data);
    277 }
    278 
    279 static void
    280 jh7110_trng_attach(device_t parent, device_t self, void *aux)
    281 {
    282 	struct jh7110_trng_softc * const sc = device_private(self);
    283 	struct fdt_attach_args * const faa = aux;
    284 	const int phandle = faa->faa_phandle;
    285 	bus_space_tag_t bst = faa->faa_bst;
    286 	bus_addr_t addr;
    287 	bus_size_t size;
    288 	int error;
    289 
    290 	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
    291 		aprint_error(": couldn't get registers\n");
    292 		return;
    293 	}
    294 
    295 	error = bus_space_map(bst, addr, size, 0, &sc->sc_bsh);
    296 	if (error) {
    297 		aprint_error(": couldn't map %#" PRIxBUSADDR ": %d", addr,
    298 		    error);
    299 		return;
    300 	}
    301 
    302 	/* Enable the hclk clock.  */
    303 	error = fdtbus_clock_enable(phandle, "hclk", true);
    304 	if (error) {
    305 		aprint_error(": couldn't enable 'hclk' clock\n");
    306 		return;
    307 	}
    308 
    309 	/* Enable the hclk clock.  */
    310 	error = fdtbus_clock_enable(phandle, "ahb", true);
    311 	if (error) {
    312 		aprint_error(": couldn't enable 'ahb' clock\n");
    313 		return;
    314 	}
    315 
    316 	/* Get a reset handle if we need and try to deassert it.  */
    317 	struct fdtbus_reset * const rst = fdtbus_reset_get_index(phandle, 0);
    318 	if (rst != NULL) {
    319 		if (fdtbus_reset_deassert(rst) != 0) {
    320 			aprint_error(": couldn't de-assert reset\n");
    321 			return;
    322 		}
    323 	}
    324 
    325 	sc->sc_dev = self;
    326 	sc->sc_phandle = phandle;
    327 	sc->sc_bst = bst;
    328 
    329 	mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_VM);
    330 	cv_init(&sc->sc_cv, "jh7110trng");
    331 
    332 	aprint_naive("\n");
    333 	aprint_normal(": JH7110 TRNG\n");
    334 
    335 	char intrstr[128];
    336 	if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) {
    337 		aprint_error_dev(self, "failed to decode interrupt\n");
    338 		return;
    339 	}
    340 
    341 	sc->sc_ih = fdtbus_intr_establish_xname(phandle, 0, IPL_VM,
    342 	    FDT_INTR_MPSAFE, jh7110_trng_intr, sc, device_xname(self));
    343 	if (sc->sc_ih == NULL) {
    344 		aprint_error_dev(self, "failed to establish interrupt on %s\n",
    345 		    intrstr);
    346 		return;
    347 	}
    348 	aprint_normal_dev(self, "interrupting on %s\n", intrstr);
    349 
    350 	config_interrupts(self, jh7110_trng_attach_i);
    351 }
    352 
    353 CFATTACH_DECL_NEW(jh7110_trng, sizeof(struct jh7110_trng_softc),
    354 	jh7110_trng_match, jh7110_trng_attach, NULL, NULL);
    355