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/module.h> 43 #include <sys/sysctl.h> 44 #include <machine/bus.h> 45 46 #include <dev/ofw/ofw_bus.h> 47 #include <dev/ofw/ofw_bus_subr.h> 48 49 #include <arm/allwinner/aw_sid.h> 50 51 #define SID_SRAM 0x200 52 #define SID_THERMAL_CALIB0 (SID_SRAM + 0x34) 53 #define SID_THERMAL_CALIB1 (SID_SRAM + 0x38) 54 55 enum sid_type { 56 A10_SID = 1, 57 A20_SID, 58 A83T_SID, 59 }; 60 61 static struct ofw_compat_data compat_data[] = { 62 { "allwinner,sun4i-a10-sid", A10_SID}, 63 { "allwinner,sun7i-a20-sid", A20_SID}, 64 { "allwinner,sun8i-a83t-sid", A83T_SID}, 65 { NULL, 0 } 66 }; 67 68 struct aw_sid_softc { 69 struct resource *res; 70 int type; 71 }; 72 73 static struct aw_sid_softc *aw_sid_sc; 74 75 static struct resource_spec aw_sid_spec[] = { 76 { SYS_RES_MEMORY, 0, RF_ACTIVE }, 77 { -1, 0 } 78 }; 79 80 enum sid_keys { 81 AW_SID_ROOT_KEY, 82 }; 83 84 #define ROOT_KEY_OFF 0x0 85 #define ROOT_KEY_SIZE 4 86 87 #define RD4(sc, reg) bus_read_4((sc)->res, (reg)) 88 #define WR4(sc, reg, val) bus_write_4((sc)->res, (reg), (val)) 89 90 static int aw_sid_sysctl(SYSCTL_HANDLER_ARGS); 91 92 static int 93 aw_sid_probe(device_t dev) 94 { 95 if (!ofw_bus_status_okay(dev)) 96 return (ENXIO); 97 98 if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0) 99 return (ENXIO); 100 101 device_set_desc(dev, "Allwinner Secure ID Controller"); 102 return (BUS_PROBE_DEFAULT); 103 } 104 105 static int 106 aw_sid_attach(device_t dev) 107 { 108 struct aw_sid_softc *sc; 109 110 sc = device_get_softc(dev); 111 112 if (bus_alloc_resources(dev, aw_sid_spec, &sc->res) != 0) { 113 device_printf(dev, "cannot allocate resources for device\n"); 114 return (ENXIO); 115 } 116 117 aw_sid_sc = sc; 118 119 sc->type = ofw_bus_search_compatible(dev, compat_data)->ocd_data; 120 switch (sc->type) { 121 case A10_SID: 122 case A20_SID: 123 SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev), 124 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), 125 OID_AUTO, "rootkey", 126 CTLTYPE_STRING | CTLFLAG_RD, 127 dev, AW_SID_ROOT_KEY, aw_sid_sysctl, "A", "Root Key"); 128 break; 129 default: 130 break; 131 } 132 return (0); 133 } 134 135 int 136 aw_sid_read_tscalib(uint32_t *calib0, uint32_t *calib1) 137 { 138 struct aw_sid_softc *sc; 139 140 sc = aw_sid_sc; 141 if (sc == NULL) 142 return (ENXIO); 143 if (sc->type != A83T_SID) 144 return (ENXIO); 145 146 *calib0 = RD4(sc, SID_THERMAL_CALIB0); 147 *calib1 = RD4(sc, SID_THERMAL_CALIB1); 148 149 return (0); 150 } 151 152 int 153 aw_sid_get_rootkey(u_char *out) 154 { 155 struct aw_sid_softc *sc; 156 int i; 157 u_int tmp; 158 159 sc = aw_sid_sc; 160 if (sc == NULL) 161 return (ENXIO); 162 if (sc->type != A10_SID && sc->type != A20_SID) 163 return (ENXIO); 164 165 for (i = 0; i < ROOT_KEY_SIZE ; i++) { 166 tmp = RD4(aw_sid_sc, ROOT_KEY_OFF + (i * 4)); 167 be32enc(&out[i * 4], tmp); 168 } 169 170 return (0); 171 } 172 173 static int 174 aw_sid_sysctl(SYSCTL_HANDLER_ARGS) 175 { 176 enum sid_keys key = arg2; 177 u_char rootkey[16]; 178 char out[33]; 179 180 if (key != AW_SID_ROOT_KEY) 181 return (ENOENT); 182 183 if (aw_sid_get_rootkey(rootkey) != 0) 184 return (ENOENT); 185 snprintf(out, sizeof(out), 186 "%16D", rootkey, ""); 187 188 return sysctl_handle_string(oidp, out, sizeof(out), req); 189 } 190 191 static device_method_t aw_sid_methods[] = { 192 /* Device interface */ 193 DEVMETHOD(device_probe, aw_sid_probe), 194 DEVMETHOD(device_attach, aw_sid_attach), 195 196 DEVMETHOD_END 197 }; 198 199 static driver_t aw_sid_driver = { 200 "aw_sid", 201 aw_sid_methods, 202 sizeof(struct aw_sid_softc), 203 }; 204 205 static devclass_t aw_sid_devclass; 206 207 EARLY_DRIVER_MODULE(aw_sid, simplebus, aw_sid_driver, aw_sid_devclass, 0, 0, 208 BUS_PASS_RESOURCE + BUS_PASS_ORDER_FIRST); 209 MODULE_VERSION(aw_sid, 1); 210