1 1.3 thorpej /* $NetBSD: imxsnvs.c,v 1.3 2025/09/07 21:45:11 thorpej Exp $ */ 2 1.1 ryo 3 1.1 ryo /* 4 1.2 msaitoh * Copyright (c) 2014 Ryo Shimizu 5 1.1 ryo * All rights reserved. 6 1.1 ryo * 7 1.1 ryo * Redistribution and use in source and binary forms, with or without 8 1.1 ryo * modification, are permitted provided that the following conditions 9 1.1 ryo * are met: 10 1.1 ryo * 1. Redistributions of source code must retain the above copyright 11 1.1 ryo * notice, this list of conditions and the following disclaimer. 12 1.1 ryo * 2. Redistributions in binary form must reproduce the above copyright 13 1.1 ryo * notice, this list of conditions and the following disclaimer in the 14 1.1 ryo * documentation and/or other materials provided with the distribution. 15 1.1 ryo * 16 1.1 ryo * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17 1.1 ryo * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 1.1 ryo * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 1.1 ryo * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 20 1.1 ryo * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 1.1 ryo * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 22 1.1 ryo * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 1.1 ryo * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 24 1.1 ryo * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 25 1.1 ryo * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26 1.1 ryo * POSSIBILITY OF SUCH DAMAGE. 27 1.1 ryo */ 28 1.1 ryo 29 1.1 ryo /* 30 1.1 ryo * i.MX6,7 Secure Non-Volatile Storage 31 1.1 ryo */ 32 1.1 ryo #include <sys/cdefs.h> 33 1.3 thorpej __KERNEL_RCSID(0, "$NetBSD: imxsnvs.c,v 1.3 2025/09/07 21:45:11 thorpej Exp $"); 34 1.1 ryo 35 1.1 ryo #include "locators.h" 36 1.1 ryo #include <sys/bus.h> 37 1.1 ryo #include <sys/device.h> 38 1.1 ryo #include <sys/param.h> 39 1.1 ryo #include <dev/clock_subr.h> 40 1.1 ryo 41 1.1 ryo #include <arm/imx/imxsnvsreg.h> 42 1.1 ryo #include <arm/imx/imxsnvsvar.h> 43 1.1 ryo 44 1.1 ryo struct imxsnvs_softc { 45 1.1 ryo device_t sc_dev; 46 1.1 ryo bus_space_tag_t sc_iot; 47 1.1 ryo bus_space_handle_t sc_ioh; 48 1.1 ryo struct todr_chip_handle sc_todr; 49 1.1 ryo }; 50 1.1 ryo 51 1.1 ryo #define SNVS_READ(sc, reg) \ 52 1.1 ryo bus_space_read_4((sc)->sc_iot, (sc)->sc_ioh, reg) 53 1.1 ryo 54 1.1 ryo #define SNVS_WRITE(sc, reg, val) \ 55 1.1 ryo bus_space_write_4((sc)->sc_iot, (sc)->sc_ioh, reg, val) 56 1.1 ryo 57 1.1 ryo static int imxsnvs_rtc_enable(struct imxsnvs_softc *); 58 1.1 ryo static int imxsnvs_rtc_disable(struct imxsnvs_softc *); 59 1.1 ryo static int imxsnvs_gettime(todr_chip_handle_t, struct timeval *); 60 1.1 ryo static int imxsnvs_settime(todr_chip_handle_t, struct timeval *); 61 1.1 ryo 62 1.1 ryo 63 1.1 ryo CFATTACH_DECL_NEW(imxsnvs, sizeof(struct imxsnvs_softc), 64 1.1 ryo imxsnvs_match, imxsnvs_attach, NULL, NULL); 65 1.1 ryo 66 1.1 ryo /* ARGSUSED */ 67 1.1 ryo int 68 1.1 ryo imxsnvs_attach_common(device_t parent __unused, device_t self, 69 1.1 ryo bus_space_tag_t iot, paddr_t iobase, size_t size, 70 1.1 ryo int intr __unused, int flags __unused) 71 1.1 ryo { 72 1.1 ryo struct imxsnvs_softc *sc; 73 1.1 ryo uint32_t v1, v2; 74 1.1 ryo 75 1.1 ryo sc = device_private(self); 76 1.1 ryo sc->sc_dev = self; 77 1.1 ryo sc->sc_iot = iot; 78 1.1 ryo 79 1.1 ryo aprint_naive("\n"); 80 1.1 ryo aprint_normal(": Secure Non-Volatile Storage\n"); 81 1.1 ryo if (bus_space_map(sc->sc_iot, iobase, size, 0, 82 1.1 ryo &sc->sc_ioh)) { 83 1.1 ryo aprint_error_dev(self, "Cannot map registers\n"); 84 1.1 ryo return 1; 85 1.1 ryo } 86 1.1 ryo 87 1.1 ryo v1 = SNVS_READ(sc, SNVS_HPVIDR1); 88 1.1 ryo v2 = SNVS_READ(sc, SNVS_HPVIDR2); 89 1.1 ryo aprint_verbose_dev(self, "id=0x%llx, ver=%lld.%lld, ip_era=0x%llx, " 90 1.1 ryo "intg_opt=0x%llx, eco_rev=0x%llx, config_opt=0x%llx\n", 91 1.1 ryo __SHIFTOUT(v1, SNVS_HPVIDR1_IP_ID), 92 1.1 ryo __SHIFTOUT(v1, SNVS_HPVIDR1_MAJOR_REV), 93 1.1 ryo __SHIFTOUT(v1, SNVS_HPVIDR1_MINOR_REV), 94 1.1 ryo __SHIFTOUT(v2, SNVS_HPVIDR2_IP_ERA), 95 1.1 ryo __SHIFTOUT(v2, SNVS_HPVIDR2_INTG_OPT), 96 1.1 ryo __SHIFTOUT(v2, SNVS_HPVIDR2_ECO_REV), 97 1.1 ryo __SHIFTOUT(v2, SNVS_HPVIDR2_CONFIG_OPT)); 98 1.1 ryo 99 1.1 ryo if (imxsnvs_rtc_enable(sc) != 0) { 100 1.1 ryo aprint_error_dev(self, "cannot enable RTC\n"); 101 1.1 ryo return 1; 102 1.1 ryo } 103 1.1 ryo 104 1.1 ryo sc->sc_todr.todr_gettime = imxsnvs_gettime; 105 1.1 ryo sc->sc_todr.todr_settime = imxsnvs_settime; 106 1.3 thorpej sc->sc_todr.todr_dev = self; 107 1.1 ryo todr_attach(&sc->sc_todr); 108 1.1 ryo 109 1.1 ryo return 0; 110 1.1 ryo } 111 1.1 ryo 112 1.1 ryo static int 113 1.1 ryo imxsnvs_rtc_enable(struct imxsnvs_softc *sc) 114 1.1 ryo { 115 1.1 ryo uint32_t v; 116 1.1 ryo int timeout; 117 1.1 ryo 118 1.1 ryo /* enable SRTC */ 119 1.1 ryo v = SNVS_READ(sc, SNVS_LPCR); 120 1.1 ryo SNVS_WRITE(sc, SNVS_LPCR, v | SNVS_LPCR_SRTC_ENV); 121 1.1 ryo for (timeout = 10000; timeout > 0; timeout--) { 122 1.1 ryo if (SNVS_READ(sc, SNVS_LPCR) & SNVS_LPCR_SRTC_ENV) 123 1.1 ryo break; 124 1.1 ryo } 125 1.1 ryo if (timeout == 0) 126 1.1 ryo return ETIMEDOUT; 127 1.1 ryo 128 1.1 ryo return 0; 129 1.1 ryo } 130 1.1 ryo 131 1.1 ryo static int 132 1.1 ryo imxsnvs_rtc_disable(struct imxsnvs_softc *sc) 133 1.1 ryo { 134 1.1 ryo uint32_t v; 135 1.1 ryo int timeout; 136 1.1 ryo 137 1.1 ryo /* disable SRTC */ 138 1.1 ryo v = SNVS_READ(sc, SNVS_LPCR); 139 1.1 ryo SNVS_WRITE(sc, SNVS_LPCR, v & ~SNVS_LPCR_SRTC_ENV); 140 1.1 ryo for (timeout = 10000; timeout > 0; timeout--) { 141 1.1 ryo if (!(SNVS_READ(sc, SNVS_LPCR) & SNVS_LPCR_SRTC_ENV)) 142 1.1 ryo break; 143 1.1 ryo } 144 1.1 ryo if (timeout == 0) 145 1.1 ryo return ETIMEDOUT; 146 1.1 ryo 147 1.1 ryo return 0; 148 1.1 ryo } 149 1.1 ryo 150 1.1 ryo static int 151 1.1 ryo imxsnvs_gettime(todr_chip_handle_t tch, struct timeval *tvp) 152 1.1 ryo { 153 1.3 thorpej struct imxsnvs_softc *sc = device_private(tch->todr_dev); 154 1.1 ryo uint64_t c1, c2; 155 1.1 ryo 156 1.1 ryo c2 = ((uint64_t)SNVS_READ(sc, SNVS_LPSRTCMR) << 32) + 157 1.1 ryo SNVS_READ(sc, SNVS_LPSRTCLR); 158 1.1 ryo do { 159 1.1 ryo c1 = c2; 160 1.1 ryo c2 = ((uint64_t)SNVS_READ(sc, SNVS_LPSRTCMR) << 32) + 161 1.1 ryo SNVS_READ(sc, SNVS_LPSRTCLR); 162 1.1 ryo } while (c1 != c2); 163 1.1 ryo 164 1.1 ryo tvp->tv_sec = c1 >> SVNS_COUNTER_SHIFT; 165 1.1 ryo tvp->tv_usec = (c1 % SVNS_COUNTER_HZ) * 1000000 / SVNS_COUNTER_HZ; 166 1.1 ryo 167 1.1 ryo return 0; 168 1.1 ryo } 169 1.1 ryo 170 1.1 ryo static int 171 1.1 ryo imxsnvs_settime(todr_chip_handle_t tch, struct timeval *tvp) 172 1.1 ryo { 173 1.3 thorpej struct imxsnvs_softc *sc = device_private(tch->todr_dev); 174 1.1 ryo uint64_t c, h, l; 175 1.1 ryo int rv; 176 1.1 ryo 177 1.1 ryo c = (uint64_t)tvp->tv_sec * SVNS_COUNTER_HZ + 178 1.1 ryo (uint64_t)tvp->tv_usec * SVNS_COUNTER_HZ / 1000000; 179 1.1 ryo h = __SHIFTIN((c >> 32) & SNVS_LPSRTCMR_SRTC, SNVS_LPSRTCMR_SRTC); 180 1.1 ryo l = c & 0xffffffff; 181 1.1 ryo 182 1.1 ryo if ((rv = imxsnvs_rtc_disable(sc)) != 0) 183 1.1 ryo return rv; 184 1.1 ryo 185 1.1 ryo SNVS_WRITE(sc, SNVS_LPSRTCMR, h); 186 1.1 ryo SNVS_WRITE(sc, SNVS_LPSRTCLR, l); 187 1.1 ryo 188 1.1 ryo if ((rv = imxsnvs_rtc_enable(sc)) != 0) 189 1.1 ryo return rv; 190 1.1 ryo 191 1.1 ryo return 0; 192 1.1 ryo } 193