1 /* $NetBSD: sunxi_sid.c,v 1.7 2021/01/27 03:10:20 thorpej Exp $ */ 2 3 /*- 4 * Copyright (c) 2017 Jared McNeill <jmcneill@invisible.ca> 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 29 #include <sys/cdefs.h> 30 __KERNEL_RCSID(0, "$NetBSD: sunxi_sid.c,v 1.7 2021/01/27 03:10:20 thorpej Exp $"); 31 32 #include <sys/param.h> 33 #include <sys/bus.h> 34 #include <sys/device.h> 35 #include <sys/intr.h> 36 #include <sys/systm.h> 37 38 #include <dev/fdt/fdtvar.h> 39 40 #include <arm/sunxi/sunxi_sid.h> 41 42 #define EFUSE_THERMAL_CALIB0 0x34 43 #define EFUSE_THERMAL_CALIB1 0x38 44 45 struct sunxi_sid_config { 46 bus_size_t efuse_offset; 47 }; 48 49 static const struct sunxi_sid_config sun4i_a10_sid_config = { 50 .efuse_offset = 0, 51 }; 52 53 static const struct sunxi_sid_config sun8i_h3_sid_config = { 54 .efuse_offset = 0x200, 55 }; 56 57 static const struct device_compatible_entry compat_data[] = { 58 { .compat = "allwinner,sun4i-a10-sid", .data = &sun4i_a10_sid_config }, 59 { .compat = "allwinner,sun7i-a20-sid", .data = &sun4i_a10_sid_config }, 60 { .compat = "allwinner,sun8i-h3-sid", .data = &sun8i_h3_sid_config }, 61 { .compat = "allwinner,sun8i-a83t-sid", .data = &sun8i_h3_sid_config }, 62 { .compat = "allwinner,sun50i-a64-sid", .data = &sun8i_h3_sid_config }, 63 DEVICE_COMPAT_EOL 64 }; 65 66 struct sunxi_sid_softc { 67 device_t sc_dev; 68 bus_space_tag_t sc_bst; 69 bus_space_handle_t sc_bsh; 70 const struct sunxi_sid_config *sc_conf; 71 }; 72 73 static struct sunxi_sid_softc *sid_softc = NULL; 74 75 #define EFUSE_READ(sc, reg) \ 76 bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (sc)->sc_conf->efuse_offset + (reg)) 77 #define EFUSE_WRITE(sc, reg, val) \ 78 bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (sc)->sc_conf->efuse_offset + (reg), (val)) 79 80 static int 81 sunxi_sid_match(device_t parent, cfdata_t cf, void *aux) 82 { 83 struct fdt_attach_args * const faa = aux; 84 85 return of_compatible_match(faa->faa_phandle, compat_data); 86 } 87 88 static void 89 sunxi_sid_attach(device_t parent, device_t self, void *aux) 90 { 91 struct sunxi_sid_softc * const sc = device_private(self); 92 struct fdt_attach_args * const faa = aux; 93 const int phandle = faa->faa_phandle; 94 bus_addr_t addr; 95 bus_size_t size; 96 97 if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) { 98 aprint_error(": couldn't get registers\n"); 99 return; 100 } 101 102 sc->sc_dev = self; 103 sc->sc_bst = faa->faa_bst; 104 if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) { 105 aprint_error(": couldn't map registers\n"); 106 return; 107 } 108 sc->sc_conf = of_compatible_lookup(phandle, compat_data)->data; 109 110 aprint_naive("\n"); 111 aprint_normal(": Security ID EFUSE\n"); 112 113 KASSERT(sid_softc == NULL); 114 sid_softc = sc; 115 } 116 117 CFATTACH_DECL_NEW(sunxi_sid, sizeof(struct sunxi_sid_softc), 118 sunxi_sid_match, sunxi_sid_attach, NULL, NULL); 119 120 int 121 sunxi_sid_read_tscalib(uint32_t *calib) 122 { 123 if (sid_softc == NULL) 124 return ENXIO; 125 126 calib[0] = EFUSE_READ(sid_softc, EFUSE_THERMAL_CALIB0); 127 calib[1] = EFUSE_READ(sid_softc, EFUSE_THERMAL_CALIB1); 128 return 0; 129 } 130