1 /*- 2 * Copyright (c) 2016 Jared McNeill <jmcneill@invisible.ca> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 19 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 20 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 21 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 22 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 * 26 * $FreeBSD$ 27 */ 28 29 /* 30 * Allwinner secure ID controller 31 */ 32 33 #include <sys/cdefs.h> 34 __FBSDID("$FreeBSD$"); 35 36 #include <sys/endian.h> 37 #include <sys/param.h> 38 #include <sys/systm.h> 39 #include <sys/bus.h> 40 #include <sys/rman.h> 41 #include <sys/kernel.h> 42 #include <sys/lock.h> 43 #include <sys/mutex.h> 44 #include <sys/module.h> 45 #include <sys/sysctl.h> 46 #include <machine/bus.h> 47 48 #include <dev/ofw/ofw_bus.h> 49 #include <dev/ofw/ofw_bus_subr.h> 50 51 #include <arm/allwinner/aw_sid.h> 52 53 /* efuse registers */ 54 #define SID_PRCTL 0x40 55 #define SID_PRCTL_OFFSET_MASK 0xff 56 #define SID_PRCTL_OFFSET(n) (((n) & SID_PRCTL_OFFSET_MASK) << 16) 57 #define SID_PRCTL_LOCK (0xac << 8) 58 #define SID_PRCTL_READ (0x01 << 1) 59 #define SID_PRCTL_WRITE (0x01 << 0) 60 #define SID_PRKEY 0x50 61 #define SID_RDKEY 0x60 62 63 #define SID_SRAM 0x200 64 /* Offsets into efuse space, for convenience */ 65 #define SID_THERMAL_CALIB0_OFF (0x34) 66 #define SID_THERMAL_CALIB1_OFF (0x38) 67 #define SID_THERMAL_CALIB0 (SID_SRAM + SID_THERMAL_CALIB0_OFF) 68 #define SID_THERMAL_CALIB1 (SID_SRAM + SID_THERMAL_CALIB1_OFF) 69 70 #define ROOT_KEY_SIZE 4 71 72 struct aw_sid_conf { 73 bus_size_t efuse_size; 74 bus_size_t rootkey_offset; 75 bool has_prctl; 76 bool has_thermal; 77 bool requires_prctl_read; 78 }; 79 80 static const struct aw_sid_conf a10_conf = { 81 .efuse_size = 0x10, 82 .rootkey_offset = 0, 83 }; 84 85 static const struct aw_sid_conf a20_conf = { 86 .efuse_size = 0x10, 87 .rootkey_offset = 0, 88 }; 89 90 static const struct aw_sid_conf a64_conf = { 91 .efuse_size = 0x100, 92 .rootkey_offset = SID_SRAM, 93 .has_prctl = true, 94 .has_thermal = true, 95 }; 96 97 static const struct aw_sid_conf a83t_conf = { 98 .efuse_size = 0x100, 99 .rootkey_offset = SID_SRAM, 100 .has_prctl = true, 101 .has_thermal = true, 102 }; 103 104 static const struct aw_sid_conf h3_conf = { 105 .efuse_size = 0x100, 106 .rootkey_offset = SID_SRAM, 107 .has_prctl = true, 108 .has_thermal = true, 109 .requires_prctl_read = true, 110 }; 111 112 static struct ofw_compat_data compat_data[] = { 113 { "allwinner,sun4i-a10-sid", (uintptr_t)&a10_conf}, 114 { "allwinner,sun7i-a20-sid", (uintptr_t)&a20_conf}, 115 { "allwinner,sun50i-a64-sid", (uintptr_t)&a64_conf}, 116 { "allwinner,sun8i-a83t-sid", (uintptr_t)&a83t_conf}, 117 { "allwinner,sun8i-h3-sid", (uintptr_t)&h3_conf}, 118 { NULL, 0 } 119 }; 120 121 struct aw_sid_softc { 122 device_t sid_dev; 123 struct resource *res; 124 struct aw_sid_conf *sid_conf; 125 struct mtx prctl_mtx; 126 }; 127 128 static struct aw_sid_softc *aw_sid_sc; 129 130 static struct resource_spec aw_sid_spec[] = { 131 { SYS_RES_MEMORY, 0, RF_ACTIVE }, 132 { -1, 0 } 133 }; 134 135 enum sid_keys { 136 AW_SID_ROOT_KEY, 137 }; 138 139 #define RD4(sc, reg) bus_read_4((sc)->res, (reg)) 140 #define WR4(sc, reg, val) bus_write_4((sc)->res, (reg), (val)) 141 142 #define PRCTL_RD4(sc, reg, val) aw_sid_prctl_read((sc)->sid_dev, (reg), (val)) 143 144 static int aw_sid_sysctl(SYSCTL_HANDLER_ARGS); 145 static int aw_sid_prctl_read(device_t dev, bus_size_t offset, uint32_t *val); 146 147 148 /* 149 * offset here is offset into efuse space, rather than offset into sid register 150 * space. This form of read is only an option for newer SoC: A83t, H3, A64 151 */ 152 static int 153 aw_sid_prctl_read(device_t dev, bus_size_t offset, uint32_t *val) 154 { 155 struct aw_sid_softc *sc; 156 uint32_t readval; 157 158 sc = device_get_softc(dev); 159 if (!sc->sid_conf->has_prctl) 160 return (1); 161 162 mtx_lock(&sc->prctl_mtx); 163 readval = SID_PRCTL_OFFSET(offset) | SID_PRCTL_LOCK | SID_PRCTL_READ; 164 WR4(sc, SID_PRCTL, readval); 165 /* Read bit will be cleared once read has concluded */ 166 while (RD4(sc, SID_PRCTL) & SID_PRCTL_READ) 167 continue; 168 readval = RD4(sc, SID_RDKEY); 169 mtx_unlock(&sc->prctl_mtx); 170 *val = readval; 171 172 return (0); 173 } 174 175 static int 176 aw_sid_probe(device_t dev) 177 { 178 if (!ofw_bus_status_okay(dev)) 179 return (ENXIO); 180 181 if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0) 182 return (ENXIO); 183 184 device_set_desc(dev, "Allwinner Secure ID Controller"); 185 return (BUS_PROBE_DEFAULT); 186 } 187 188 static int 189 aw_sid_attach(device_t dev) 190 { 191 struct aw_sid_softc *sc; 192 193 sc = device_get_softc(dev); 194 sc->sid_dev = dev; 195 196 if (bus_alloc_resources(dev, aw_sid_spec, &sc->res) != 0) { 197 device_printf(dev, "cannot allocate resources for device\n"); 198 return (ENXIO); 199 } 200 201 mtx_init(&sc->prctl_mtx, device_get_nameunit(dev), NULL, MTX_DEF); 202 sc->sid_conf = (struct aw_sid_conf *)ofw_bus_search_compatible(dev, compat_data)->ocd_data; 203 aw_sid_sc = sc; 204 205 SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev), 206 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), 207 OID_AUTO, "rootkey", 208 CTLTYPE_STRING | CTLFLAG_RD, 209 dev, AW_SID_ROOT_KEY, aw_sid_sysctl, "A", "Root Key"); 210 211 return (0); 212 } 213 214 int 215 aw_sid_read_tscalib(uint32_t *calib0, uint32_t *calib1) 216 { 217 struct aw_sid_softc *sc; 218 219 sc = aw_sid_sc; 220 if (sc == NULL) 221 return (ENXIO); 222 if (!sc->sid_conf->has_thermal) 223 return (ENXIO); 224 225 if (sc->sid_conf->requires_prctl_read) { 226 PRCTL_RD4(sc, SID_THERMAL_CALIB0_OFF, calib0); 227 PRCTL_RD4(sc, SID_THERMAL_CALIB1_OFF, calib1); 228 } else { 229 *calib0 = RD4(sc, SID_THERMAL_CALIB0); 230 *calib1 = RD4(sc, SID_THERMAL_CALIB1); 231 } 232 233 return (0); 234 } 235 236 int 237 aw_sid_get_rootkey(u_char *out) 238 { 239 struct aw_sid_softc *sc; 240 int i; 241 bus_size_t root_key_off; 242 u_int tmp; 243 244 sc = aw_sid_sc; 245 if (sc == NULL) 246 return (ENXIO); 247 root_key_off = aw_sid_sc->sid_conf->rootkey_offset; 248 for (i = 0; i < ROOT_KEY_SIZE ; i++) { 249 if (sc->sid_conf->requires_prctl_read) 250 PRCTL_RD4(sc, (i * 4), &tmp); 251 else 252 tmp = RD4(aw_sid_sc, root_key_off + (i * 4)); 253 be32enc(&out[i * 4], tmp); 254 } 255 256 return (0); 257 } 258 259 static int 260 aw_sid_sysctl(SYSCTL_HANDLER_ARGS) 261 { 262 enum sid_keys key = arg2; 263 u_char rootkey[16]; 264 char out[33]; 265 266 if (key != AW_SID_ROOT_KEY) 267 return (ENOENT); 268 269 if (aw_sid_get_rootkey(rootkey) != 0) 270 return (ENOENT); 271 snprintf(out, sizeof(out), 272 "%16D", rootkey, ""); 273 274 return sysctl_handle_string(oidp, out, sizeof(out), req); 275 } 276 277 static device_method_t aw_sid_methods[] = { 278 /* Device interface */ 279 DEVMETHOD(device_probe, aw_sid_probe), 280 DEVMETHOD(device_attach, aw_sid_attach), 281 282 DEVMETHOD_END 283 }; 284 285 static driver_t aw_sid_driver = { 286 "aw_sid", 287 aw_sid_methods, 288 sizeof(struct aw_sid_softc), 289 }; 290 291 static devclass_t aw_sid_devclass; 292 293 EARLY_DRIVER_MODULE(aw_sid, simplebus, aw_sid_driver, aw_sid_devclass, 0, 0, 294 BUS_PASS_RESOURCE + BUS_PASS_ORDER_FIRST); 295 MODULE_VERSION(aw_sid, 1); 296