ti_rng.c revision 1.1
11.1Sjmcneill/* $NetBSD: ti_rng.c,v 1.1 2019/10/28 23:57:59 jmcneill Exp $ */
21.1Sjmcneill
31.1Sjmcneill/*-
41.1Sjmcneill * Copyright (c) 2015 Jared D. McNeill <jmcneill@invisible.ca>
51.1Sjmcneill * All rights reserved.
61.1Sjmcneill *
71.1Sjmcneill * Redistribution and use in source and binary forms, with or without
81.1Sjmcneill * modification, are permitted provided that the following conditions
91.1Sjmcneill * are met:
101.1Sjmcneill * 1. Redistributions of source code must retain the above copyright
111.1Sjmcneill *    notice, this list of conditions and the following disclaimer.
121.1Sjmcneill * 2. The name of the author may not be used to endorse or promote products
131.1Sjmcneill *    derived from this software without specific prior written permission.
141.1Sjmcneill *
151.1Sjmcneill * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
161.1Sjmcneill * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
171.1Sjmcneill * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
181.1Sjmcneill * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
191.1Sjmcneill * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
201.1Sjmcneill * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
211.1Sjmcneill * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
221.1Sjmcneill * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
231.1Sjmcneill * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
241.1Sjmcneill * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
251.1Sjmcneill * SUCH DAMAGE.
261.1Sjmcneill */
271.1Sjmcneill
281.1Sjmcneill#include <sys/cdefs.h>
291.1Sjmcneill__KERNEL_RCSID(0, "$NetBSD: ti_rng.c,v 1.1 2019/10/28 23:57:59 jmcneill Exp $");
301.1Sjmcneill
311.1Sjmcneill#include <sys/param.h>
321.1Sjmcneill#include <sys/systm.h>
331.1Sjmcneill#include <sys/device.h>
341.1Sjmcneill#include <sys/conf.h>
351.1Sjmcneill#include <sys/mutex.h>
361.1Sjmcneill#include <sys/bus.h>
371.1Sjmcneill#include <sys/rndpool.h>
381.1Sjmcneill#include <sys/rndsource.h>
391.1Sjmcneill
401.1Sjmcneill#include <dev/fdt/fdtvar.h>
411.1Sjmcneill
421.1Sjmcneill#include <arm/ti/ti_prcm.h>
431.1Sjmcneill#include <arm/ti/ti_rngreg.h>
441.1Sjmcneill
451.1Sjmcneillstatic const char * const compatible[] = {
461.1Sjmcneill	"ti,omap4-rng",
471.1Sjmcneill	NULL
481.1Sjmcneill};
491.1Sjmcneill
501.1Sjmcneillstruct ti_rng_softc {
511.1Sjmcneill	device_t sc_dev;
521.1Sjmcneill	bus_space_tag_t sc_iot;
531.1Sjmcneill	bus_space_handle_t sc_ioh;
541.1Sjmcneill
551.1Sjmcneill	kmutex_t sc_lock;
561.1Sjmcneill	krndsource_t sc_rndsource;
571.1Sjmcneill};
581.1Sjmcneill
591.1Sjmcneillstatic int	ti_rng_match(device_t, cfdata_t, void *);
601.1Sjmcneillstatic void	ti_rng_attach(device_t, device_t, void *);
611.1Sjmcneillstatic void	ti_rng_callback(size_t, void *);
621.1Sjmcneill
631.1SjmcneillCFATTACH_DECL_NEW(ti_rng, sizeof(struct ti_rng_softc),
641.1Sjmcneill    ti_rng_match, ti_rng_attach, NULL, NULL);
651.1Sjmcneill
661.1Sjmcneill#define RD4(sc, reg) \
671.1Sjmcneill	bus_space_read_4((sc)->sc_iot, (sc)->sc_ioh, (reg))
681.1Sjmcneill#define WR4(sc, reg, val) \
691.1Sjmcneill	bus_space_write_4((sc)->sc_iot, (sc)->sc_ioh, (reg), (val))
701.1Sjmcneill
711.1Sjmcneillstatic int
721.1Sjmcneillti_rng_match(device_t parent, cfdata_t match, void *aux)
731.1Sjmcneill{
741.1Sjmcneill	struct fdt_attach_args * const faa = aux;
751.1Sjmcneill
761.1Sjmcneill	return of_match_compatible(faa->faa_phandle, compatible);
771.1Sjmcneill}
781.1Sjmcneill
791.1Sjmcneillstatic void
801.1Sjmcneillti_rng_attach(device_t parent, device_t self, void *aux)
811.1Sjmcneill{
821.1Sjmcneill	struct ti_rng_softc *sc = device_private(self);
831.1Sjmcneill	struct fdt_attach_args * const faa = aux;
841.1Sjmcneill	const int phandle = faa->faa_phandle;
851.1Sjmcneill	bus_addr_t addr;
861.1Sjmcneill	bus_size_t size;
871.1Sjmcneill
881.1Sjmcneill	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
891.1Sjmcneill		aprint_error(": couldn't get registers\n");
901.1Sjmcneill		return;
911.1Sjmcneill	}
921.1Sjmcneill
931.1Sjmcneill	if (ti_prcm_enable_hwmod(OF_parent(phandle), 0) != 0) {
941.1Sjmcneill		aprint_error(": couldn't enable module\n");
951.1Sjmcneill		return;
961.1Sjmcneill	}
971.1Sjmcneill
981.1Sjmcneill	sc->sc_dev = self;
991.1Sjmcneill	sc->sc_iot = faa->faa_bst;
1001.1Sjmcneill	if (bus_space_map(sc->sc_iot, addr, size, 0, &sc->sc_ioh) != 0) {
1011.1Sjmcneill		aprint_error(": couldn't map registers\n");
1021.1Sjmcneill		return;
1031.1Sjmcneill	}
1041.1Sjmcneill
1051.1Sjmcneill	mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_VM);
1061.1Sjmcneill
1071.1Sjmcneill	if ((RD4(sc, TRNG_CONTROL_REG) & TRNG_CONTROL_ENABLE) == 0) {
1081.1Sjmcneill		WR4(sc, TRNG_CONFIG_REG,
1091.1Sjmcneill		    __SHIFTIN(0x21, TRNG_CONFIG_MIN_REFILL) |
1101.1Sjmcneill		    __SHIFTIN(0x22, TRNG_CONFIG_MAX_REFILL));
1111.1Sjmcneill		WR4(sc, TRNG_CONTROL_REG,
1121.1Sjmcneill		    __SHIFTIN(0x21, TRNG_CONTROL_STARTUP_CYCLES) |
1131.1Sjmcneill		    TRNG_CONTROL_ENABLE);
1141.1Sjmcneill	}
1151.1Sjmcneill
1161.1Sjmcneill	rndsource_setcb(&sc->sc_rndsource, ti_rng_callback, sc);
1171.1Sjmcneill	rnd_attach_source(&sc->sc_rndsource, device_xname(self), RND_TYPE_RNG,
1181.1Sjmcneill	    RND_FLAG_COLLECT_VALUE|RND_FLAG_HASCB);
1191.1Sjmcneill
1201.1Sjmcneill	aprint_naive("\n");
1211.1Sjmcneill	aprint_normal(": RNG\n");
1221.1Sjmcneill
1231.1Sjmcneill	ti_rng_callback(RND_POOLBITS / NBBY, sc);
1241.1Sjmcneill}
1251.1Sjmcneill
1261.1Sjmcneillstatic void
1271.1Sjmcneillti_rng_callback(size_t bytes_wanted, void *priv)
1281.1Sjmcneill{
1291.1Sjmcneill	struct ti_rng_softc * const sc = priv;
1301.1Sjmcneill	uint32_t buf[2];
1311.1Sjmcneill	u_int retry;
1321.1Sjmcneill
1331.1Sjmcneill	mutex_enter(&sc->sc_lock);
1341.1Sjmcneill	while (bytes_wanted) {
1351.1Sjmcneill		for (retry = 10; retry > 0; retry--) {
1361.1Sjmcneill			if (RD4(sc, TRNG_STATUS_REG) & TRNG_STATUS_READY)
1371.1Sjmcneill				break;
1381.1Sjmcneill			delay(10);
1391.1Sjmcneill		}
1401.1Sjmcneill		if (retry == 0)
1411.1Sjmcneill			break;
1421.1Sjmcneill		buf[0] = RD4(sc, TRNG_OUTPUT_L_REG);
1431.1Sjmcneill		buf[1] = RD4(sc, TRNG_OUTPUT_H_REG);
1441.1Sjmcneill		WR4(sc, TRNG_INTACK_REG, TRNG_INTACK_READY);
1451.1Sjmcneill		rnd_add_data_sync(&sc->sc_rndsource, buf, sizeof(buf),
1461.1Sjmcneill		    sizeof(buf) * NBBY);
1471.1Sjmcneill		bytes_wanted -= MIN(bytes_wanted, sizeof(buf));
1481.1Sjmcneill	}
1491.1Sjmcneill	explicit_memset(buf, 0, sizeof(buf));
1501.1Sjmcneill	mutex_exit(&sc->sc_lock);
1511.1Sjmcneill}
152