rk_v1crypto.c revision 1.4
11.4Sthorpej/* $NetBSD: rk_v1crypto.c,v 1.4 2021/01/18 02:35:49 thorpej Exp $ */ 21.1Sriastrad 31.1Sriastrad/*- 41.1Sriastrad * Copyright (c) 2020 The NetBSD Foundation, Inc. 51.1Sriastrad * All rights reserved. 61.1Sriastrad * 71.1Sriastrad * This code is derived from software contributed to The NetBSD Foundation 81.1Sriastrad * by Taylor R. Campbell. 91.1Sriastrad * 101.1Sriastrad * Redistribution and use in source and binary forms, with or without 111.1Sriastrad * modification, are permitted provided that the following conditions 121.1Sriastrad * are met: 131.1Sriastrad * 1. Redistributions of source code must retain the above copyright 141.1Sriastrad * notice, this list of conditions and the following disclaimer. 151.1Sriastrad * 2. Redistributions in binary form must reproduce the above copyright 161.1Sriastrad * notice, this list of conditions and the following disclaimer in the 171.1Sriastrad * documentation and/or other materials provided with the distribution. 181.1Sriastrad * 191.1Sriastrad * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 201.1Sriastrad * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 211.1Sriastrad * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 221.1Sriastrad * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 231.1Sriastrad * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 241.1Sriastrad * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 251.1Sriastrad * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 261.1Sriastrad * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 271.1Sriastrad * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 281.1Sriastrad * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 291.1Sriastrad * POSSIBILITY OF SUCH DAMAGE. 301.1Sriastrad */ 311.1Sriastrad 321.1Sriastrad/* 331.1Sriastrad * rk_v1crypto -- Rockchip crypto v1 driver 341.1Sriastrad * 351.1Sriastrad * This is just the RNG for now. 361.1Sriastrad */ 371.1Sriastrad 381.1Sriastrad#include <sys/cdefs.h> 391.4Sthorpej__KERNEL_RCSID(1, "$NetBSD: rk_v1crypto.c,v 1.4 2021/01/18 02:35:49 thorpej Exp $"); 401.1Sriastrad 411.1Sriastrad#include <sys/types.h> 421.1Sriastrad 431.1Sriastrad#include <sys/bus.h> 441.1Sriastrad#include <sys/device.h> 451.1Sriastrad#include <sys/errno.h> 461.1Sriastrad#include <sys/mutex.h> 471.1Sriastrad#include <sys/rndsource.h> 481.1Sriastrad#include <sys/sysctl.h> 491.1Sriastrad 501.1Sriastrad#include <dev/fdt/fdtvar.h> 511.1Sriastrad 521.1Sriastrad#include <arm/rockchip/rk_v1crypto.h> 531.1Sriastrad 541.1Sriastradstruct rk_v1crypto_softc { 551.1Sriastrad device_t sc_dev; 561.1Sriastrad bus_space_tag_t sc_bst; 571.1Sriastrad bus_space_handle_t sc_bsh; 581.1Sriastrad kmutex_t sc_lock; 591.1Sriastrad struct krndsource sc_rndsource; 601.1Sriastrad struct rk_v1crypto_sysctl { 611.1Sriastrad struct sysctllog *cy_log; 621.1Sriastrad const struct sysctlnode *cy_root_node; 631.1Sriastrad } sc_sysctl; 641.1Sriastrad}; 651.1Sriastrad 661.1Sriastradstatic int rk_v1crypto_match(device_t, cfdata_t, void *); 671.1Sriastradstatic void rk_v1crypto_attach(device_t, device_t, void *); 681.1Sriastradstatic int rk_v1crypto_selftest(struct rk_v1crypto_softc *); 691.1Sriastradstatic void rk_v1crypto_rndsource_attach(struct rk_v1crypto_softc *); 701.1Sriastradstatic void rk_v1crypto_rng_get(size_t, void *); 711.1Sriastradstatic void rk_v1crypto_sysctl_attach(struct rk_v1crypto_softc *); 721.1Sriastradstatic int rk_v1crypto_sysctl_rng(SYSCTLFN_ARGS); 731.1Sriastradstatic int rk_v1crypto_rng(struct rk_v1crypto_softc *, 741.1Sriastrad uint32_t[static RK_V1CRYPTO_TRNG_NOUT]); 751.1Sriastrad 761.1Sriastradstatic uint32_t 771.1SriastradRKC_READ(struct rk_v1crypto_softc *sc, bus_addr_t reg) 781.1Sriastrad{ 791.1Sriastrad return bus_space_read_4(sc->sc_bst, sc->sc_bsh, reg); 801.1Sriastrad} 811.1Sriastrad 821.1Sriastradstatic void 831.1SriastradRKC_WRITE(struct rk_v1crypto_softc *sc, bus_addr_t reg, uint32_t v) 841.1Sriastrad{ 851.3Stnn bus_space_write_4(sc->sc_bst, sc->sc_bsh, reg, v); 861.1Sriastrad} 871.1Sriastrad 881.1Sriastradstatic inline void 891.1SriastradRKC_CTRL(struct rk_v1crypto_softc *sc, uint16_t m, uint16_t v) 901.1Sriastrad{ 911.1Sriastrad uint32_t c = 0; 921.1Sriastrad 931.1Sriastrad c |= __SHIFTIN(m, RK_V1CRYPTO_CTRL_MASK); 941.1Sriastrad c |= __SHIFTIN(v, m); 951.1Sriastrad RKC_WRITE(sc, RK_V1CRYPTO_CTRL, c); 961.1Sriastrad} 971.1Sriastrad 981.1SriastradCFATTACH_DECL_NEW(rk_v1crypto, sizeof(struct rk_v1crypto_softc), 991.1Sriastrad rk_v1crypto_match, rk_v1crypto_attach, NULL, NULL); 1001.1Sriastrad 1011.4Sthorpejstatic const struct device_compatible_entry compat_data[] = { 1021.4Sthorpej { .compat = "rockchip,rk3288-crypto" }, 1031.4Sthorpej 1041.4Sthorpej { 0 } 1051.1Sriastrad}; 1061.1Sriastrad 1071.1Sriastradstatic int 1081.1Sriastradrk_v1crypto_match(device_t parent, cfdata_t cf, void *aux) 1091.1Sriastrad{ 1101.1Sriastrad const struct fdt_attach_args *const faa = aux; 1111.1Sriastrad 1121.1Sriastrad return of_match_compat_data(faa->faa_phandle, compat_data); 1131.1Sriastrad} 1141.1Sriastrad 1151.1Sriastradstatic void 1161.1Sriastradrk_v1crypto_attach(device_t parent, device_t self, void *aux) 1171.1Sriastrad{ 1181.1Sriastrad static const char *const clks[] = {"aclk", "hclk", "sclk", "apb_pclk"}; 1191.1Sriastrad struct rk_v1crypto_softc *const sc = device_private(self); 1201.1Sriastrad const struct fdt_attach_args *const faa = aux; 1211.1Sriastrad bus_addr_t addr; 1221.1Sriastrad bus_size_t size; 1231.1Sriastrad const int phandle = faa->faa_phandle; 1241.1Sriastrad struct fdtbus_reset *rst; 1251.1Sriastrad unsigned i; 1261.1Sriastrad uint32_t ctrl; 1271.1Sriastrad 1281.1Sriastrad fdtbus_clock_assign(phandle); 1291.1Sriastrad 1301.1Sriastrad sc->sc_dev = self; 1311.1Sriastrad sc->sc_bst = faa->faa_bst; 1321.1Sriastrad mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_VM); 1331.1Sriastrad 1341.1Sriastrad /* Get and map device registers. */ 1351.1Sriastrad if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) { 1361.1Sriastrad aprint_error(": couldn't get registers\n"); 1371.1Sriastrad return; 1381.1Sriastrad } 1391.1Sriastrad if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) { 1401.1Sriastrad aprint_error(": couldn't map registers\n"); 1411.1Sriastrad return; 1421.1Sriastrad } 1431.1Sriastrad 1441.1Sriastrad /* Enable the clocks. */ 1451.1Sriastrad for (i = 0; i < __arraycount(clks); i++) { 1461.1Sriastrad if (fdtbus_clock_enable(phandle, clks[i], true) != 0) { 1471.1Sriastrad aprint_error(": couldn't enable %s clock\n", clks[i]); 1481.1Sriastrad return; 1491.1Sriastrad } 1501.1Sriastrad } 1511.1Sriastrad 1521.1Sriastrad /* Get a reset handle if we need and try to deassert it. */ 1531.1Sriastrad if ((rst = fdtbus_reset_get_index(phandle, 0)) != NULL) { 1541.1Sriastrad if (fdtbus_reset_deassert(rst) != 0) { 1551.1Sriastrad aprint_error(": couldn't de-assert reset\n"); 1561.1Sriastrad return; 1571.1Sriastrad } 1581.1Sriastrad } 1591.1Sriastrad 1601.1Sriastrad aprint_naive("\n"); 1611.1Sriastrad aprint_normal(": Crypto v1\n"); 1621.1Sriastrad 1631.1Sriastrad /* 1641.1Sriastrad * Enable ring oscillator to start gathering entropy, and set 1651.1Sriastrad * up the crypto clock to sample it once every 100 cycles. 1661.1Sriastrad * 1671.1Sriastrad * The ring oscillator can run even when the clock is gated or 1681.1Sriastrad * flush is asserted, and the longer we run it, the less it 1691.1Sriastrad * will be synchronized with the main clock owing to jitter 1701.1Sriastrad * ideally from unpredictable thermal noise. 1711.1Sriastrad */ 1721.1Sriastrad ctrl = RK_V1CRYPTO_TRNG_CTRL_OSC_ENABLE; 1731.1Sriastrad ctrl |= __SHIFTIN(100, RK_V1CRYPTO_TRNG_CTRL_CYCLES); 1741.1Sriastrad RKC_WRITE(sc, RK_V1CRYPTO_TRNG_CTRL, ctrl); 1751.1Sriastrad 1761.1Sriastrad if (rk_v1crypto_selftest(sc)) 1771.1Sriastrad return; 1781.1Sriastrad rk_v1crypto_rndsource_attach(sc); 1791.1Sriastrad rk_v1crypto_sysctl_attach(sc); 1801.1Sriastrad} 1811.1Sriastrad 1821.1Sriastradstatic int 1831.1Sriastradrk_v1crypto_selftest(struct rk_v1crypto_softc *sc) 1841.1Sriastrad{ 1851.1Sriastrad static const uint32_t key[4] = {0}; 1861.1Sriastrad static const uint32_t input[4] = {0}; 1871.1Sriastrad static const uint32_t expected[4] = { 1881.1Sriastrad 0x66e94bd4, 0xef8a2c3b, 0x884cfa59, 0xca342b2e, 1891.1Sriastrad }; 1901.1Sriastrad uint32_t output[4]; 1911.1Sriastrad uint32_t ctrl; 1921.1Sriastrad unsigned i, timo; 1931.1Sriastrad 1941.1Sriastrad /* Program the key and input block. */ 1951.1Sriastrad for (i = 0; i < 4; i++) 1961.1Sriastrad RKC_WRITE(sc, RK_V1CRYPTO_AES_DIN(i), key[i]); 1971.1Sriastrad for (i = 0; i < 4; i++) 1981.1Sriastrad RKC_WRITE(sc, RK_V1CRYPTO_AES_DIN(i), input[i]); 1991.1Sriastrad 2001.1Sriastrad /* 2011.1Sriastrad * Set up the AES unit to do AES-128 `ECB' (i.e., just the raw 2021.1Sriastrad * AES permutation) in the encryption direction. 2031.1Sriastrad */ 2041.1Sriastrad ctrl = 0; 2051.1Sriastrad ctrl |= RK_V1CRYPTO_AES_CTRL_KEYCHANGE; 2061.1Sriastrad ctrl |= __SHIFTIN(RK_V1CRYPTO_AES_CTRL_MODE_ECB, 2071.1Sriastrad RK_V1CRYPTO_AES_CTRL_MODE); 2081.1Sriastrad ctrl |= __SHIFTIN(RK_V1CRYPTO_AES_CTRL_KEYSIZE_128, 2091.1Sriastrad RK_V1CRYPTO_AES_CTRL_KEYSIZE); 2101.1Sriastrad ctrl |= __SHIFTIN(RK_V1CRYPTO_AES_CTRL_DIR_ENC, 2111.1Sriastrad RK_V1CRYPTO_AES_CTRL_DIR); 2121.1Sriastrad RKC_WRITE(sc, RK_V1CRYPTO_AES_CTRL, ctrl); 2131.1Sriastrad 2141.1Sriastrad /* Kick it off. */ 2151.1Sriastrad RKC_CTRL(sc, RK_V1CRYPTO_CTRL_AES_START, 1); 2161.1Sriastrad 2171.1Sriastrad /* Wait up to 1ms for it to complete. */ 2181.1Sriastrad timo = 1000; 2191.1Sriastrad while (RKC_READ(sc, RK_V1CRYPTO_CTRL) & RK_V1CRYPTO_CTRL_AES_START) { 2201.1Sriastrad if (--timo == 0) { 2211.1Sriastrad device_printf(sc->sc_dev, "AES self-test timed out\n"); 2221.1Sriastrad return -1; 2231.1Sriastrad } 2241.1Sriastrad DELAY(1); 2251.1Sriastrad } 2261.1Sriastrad 2271.1Sriastrad /* Read the output. */ 2281.1Sriastrad for (i = 0; i < 4; i++) 2291.1Sriastrad output[i] = RKC_READ(sc, RK_V1CRYPTO_AES_DOUT(i)); 2301.1Sriastrad 2311.1Sriastrad /* Verify the output. */ 2321.1Sriastrad for (i = 0; i < 4; i++) { 2331.1Sriastrad if (output[i] != expected[i]) { 2341.1Sriastrad device_printf(sc->sc_dev, "AES self-test failed\n"); 2351.1Sriastrad return -1; 2361.1Sriastrad } 2371.1Sriastrad } 2381.1Sriastrad 2391.1Sriastrad /* Success! */ 2401.1Sriastrad return 0; 2411.1Sriastrad} 2421.1Sriastrad 2431.1Sriastradstatic void 2441.1Sriastradrk_v1crypto_rndsource_attach(struct rk_v1crypto_softc *sc) 2451.1Sriastrad{ 2461.1Sriastrad device_t self = sc->sc_dev; 2471.1Sriastrad 2481.1Sriastrad rndsource_setcb(&sc->sc_rndsource, rk_v1crypto_rng_get, sc); 2491.1Sriastrad rnd_attach_source(&sc->sc_rndsource, device_xname(self), 2501.1Sriastrad RND_TYPE_RNG, RND_FLAG_DEFAULT|RND_FLAG_HASCB); 2511.1Sriastrad} 2521.1Sriastrad 2531.1Sriastradstatic void 2541.1Sriastradrk_v1crypto_rng_get(size_t nbytes, void *cookie) 2551.1Sriastrad{ 2561.1Sriastrad struct rk_v1crypto_softc *sc = cookie; 2571.1Sriastrad device_t self = sc->sc_dev; 2581.1Sriastrad uint32_t buf[RK_V1CRYPTO_TRNG_NOUT]; 2591.1Sriastrad uint32_t entropybits = NBBY*sizeof(buf)/2; /* be conservative */ 2601.1Sriastrad unsigned n = RK_V1CRYPTO_TRNG_NOUT; 2611.1Sriastrad int error; 2621.1Sriastrad size_t nbits = NBBY*nbytes; 2631.1Sriastrad 2641.1Sriastrad while (nbits) { 2651.1Sriastrad CTASSERT((RK_V1CRYPTO_TRNG_NOUT % 2) == 0); 2661.1Sriastrad 2671.1Sriastrad error = rk_v1crypto_rng(sc, buf); 2681.1Sriastrad if (error) { 2691.1Sriastrad device_printf(self, "timed out\n"); 2701.1Sriastrad break; 2711.1Sriastrad } 2721.1Sriastrad if (consttime_memequal(buf, buf + n/2, n/2)) { 2731.1Sriastrad device_printf(self, "failed repeated output test\n"); 2741.1Sriastrad break; 2751.1Sriastrad } 2761.2Sriastrad rnd_add_data_sync(&sc->sc_rndsource, buf, sizeof buf, 2771.2Sriastrad entropybits); 2781.1Sriastrad nbits -= MIN(nbits, MAX(1, entropybits)); 2791.1Sriastrad } 2801.1Sriastrad explicit_memset(buf, 0, sizeof buf); 2811.1Sriastrad} 2821.1Sriastrad 2831.1Sriastradstatic void 2841.1Sriastradrk_v1crypto_sysctl_attach(struct rk_v1crypto_softc *sc) 2851.1Sriastrad{ 2861.1Sriastrad device_t self = sc->sc_dev; 2871.1Sriastrad struct rk_v1crypto_sysctl *cy = &sc->sc_sysctl; 2881.1Sriastrad int error; 2891.1Sriastrad 2901.1Sriastrad /* hw.rkv1cryptoN (node) */ 2911.1Sriastrad error = sysctl_createv(&cy->cy_log, 0, NULL, &cy->cy_root_node, 2921.1Sriastrad CTLFLAG_PERMANENT, CTLTYPE_NODE, device_xname(self), 2931.1Sriastrad SYSCTL_DESCR("rk crypto v1 engine knobs"), 2941.1Sriastrad NULL, 0, NULL, 0, 2951.1Sriastrad CTL_HW, CTL_CREATE, CTL_EOL); 2961.1Sriastrad if (error) { 2971.1Sriastrad aprint_error_dev(self, 2981.1Sriastrad "failed to set up sysctl hw.%s: %d\n", 2991.1Sriastrad device_xname(self), error); 3001.1Sriastrad return; 3011.1Sriastrad } 3021.1Sriastrad 3031.1Sriastrad /* hw.rkv1cryptoN.rng (`struct', 32-byte array) */ 3041.1Sriastrad sysctl_createv(&cy->cy_log, 0, &cy->cy_root_node, NULL, 3051.1Sriastrad CTLFLAG_PERMANENT|CTLFLAG_READONLY|CTLFLAG_PRIVATE, CTLTYPE_STRUCT, 3061.1Sriastrad "rng", SYSCTL_DESCR("Read up to 32 bytes out of the TRNG"), 3071.1Sriastrad &rk_v1crypto_sysctl_rng, 0, sc, 0, CTL_CREATE, CTL_EOL); 3081.1Sriastrad if (error) { 3091.1Sriastrad aprint_error_dev(self, 3101.1Sriastrad "failed to set up sysctl hw.%s.rng: %d\n", 3111.1Sriastrad device_xname(self), error); 3121.1Sriastrad return; 3131.1Sriastrad } 3141.1Sriastrad} 3151.1Sriastrad 3161.1Sriastradstatic int 3171.1Sriastradrk_v1crypto_sysctl_rng(SYSCTLFN_ARGS) 3181.1Sriastrad{ 3191.1Sriastrad uint32_t buf[RK_V1CRYPTO_TRNG_NOUT]; 3201.1Sriastrad struct sysctlnode node = *rnode; 3211.1Sriastrad struct rk_v1crypto_softc *sc = node.sysctl_data; 3221.1Sriastrad size_t size; 3231.1Sriastrad int error; 3241.1Sriastrad 3251.1Sriastrad /* If oldp == NULL, the caller wants to learn the size. */ 3261.1Sriastrad if (oldp == NULL) { 3271.1Sriastrad *oldlenp = sizeof buf; 3281.1Sriastrad return 0; 3291.1Sriastrad } 3301.1Sriastrad 3311.1Sriastrad /* Verify the output buffer size is reasonable. */ 3321.1Sriastrad size = *oldlenp; 3331.1Sriastrad if (size > sizeof buf) /* size_t, so never negative */ 3341.1Sriastrad return E2BIG; 3351.1Sriastrad if (size == 0) 3361.1Sriastrad return 0; /* nothing to do */ 3371.1Sriastrad 3381.1Sriastrad /* Generate data. */ 3391.1Sriastrad error = rk_v1crypto_rng(sc, buf); 3401.1Sriastrad if (error) 3411.1Sriastrad return error; 3421.1Sriastrad 3431.1Sriastrad /* Copy out the data. */ 3441.1Sriastrad node.sysctl_data = buf; 3451.1Sriastrad node.sysctl_size = size; 3461.1Sriastrad error = sysctl_lookup(SYSCTLFN_CALL(&node)); 3471.1Sriastrad 3481.1Sriastrad /* Clear the buffer. */ 3491.1Sriastrad explicit_memset(buf, 0, sizeof buf); 3501.1Sriastrad 3511.1Sriastrad /* Return the sysctl_lookup error, if any. */ 3521.1Sriastrad return error; 3531.1Sriastrad} 3541.1Sriastrad 3551.1Sriastradstatic int 3561.1Sriastradrk_v1crypto_rng(struct rk_v1crypto_softc *sc, 3571.1Sriastrad uint32_t buf[static RK_V1CRYPTO_TRNG_NOUT]) 3581.1Sriastrad{ 3591.1Sriastrad unsigned i, timo; 3601.1Sriastrad int error; 3611.1Sriastrad 3621.1Sriastrad /* Acquire lock to serialize access to TRNG. */ 3631.1Sriastrad mutex_enter(&sc->sc_lock); 3641.1Sriastrad 3651.1Sriastrad /* 3661.1Sriastrad * Query TRNG and wait up to 1ms for it to post. Empirically, 3671.1Sriastrad * this takes around 120us. 3681.1Sriastrad */ 3691.1Sriastrad RKC_CTRL(sc, RK_V1CRYPTO_CTRL_TRNG_START, 1); 3701.1Sriastrad timo = 1000; 3711.1Sriastrad while (RKC_READ(sc, RK_V1CRYPTO_CTRL) & RK_V1CRYPTO_CTRL_TRNG_START) { 3721.1Sriastrad if (--timo == 0) { 3731.1Sriastrad error = ETIMEDOUT; 3741.1Sriastrad goto out; 3751.1Sriastrad } 3761.1Sriastrad DELAY(1); 3771.1Sriastrad } 3781.1Sriastrad 3791.1Sriastrad /* Read out the data. */ 3801.1Sriastrad for (i = 0; i < RK_V1CRYPTO_TRNG_NOUT; i++) 3811.1Sriastrad buf[i] = RKC_READ(sc, RK_V1CRYPTO_TRNG_DOUT(i)); 3821.1Sriastrad 3831.1Sriastrad /* Success! */ 3841.1Sriastrad error = 0; 3851.1Sriastradout: mutex_exit(&sc->sc_lock); 3861.1Sriastrad return error; 3871.1Sriastrad} 388