1 /* $NetBSD: sunxi_sid.c,v 1.1 2017/10/03 23:42:17 jmcneill 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.1 2017/10/03 23:42:17 jmcneill 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 of_compat_data compat_data[] = { 58 { "allwinner,sun4i-a10-sid", (uintptr_t)&sun4i_a10_sid_config }, 59 { "allwinner,sun7i-a20-sid", (uintptr_t)&sun4i_a10_sid_config }, 60 { "allwinner,sun8i-h3-sid", (uintptr_t)&sun8i_h3_sid_config }, 61 { NULL } 62 }; 63 64 struct sunxi_sid_softc { 65 device_t sc_dev; 66 bus_space_tag_t sc_bst; 67 bus_space_handle_t sc_bsh; 68 const struct sunxi_sid_config *sc_conf; 69 }; 70 71 static struct sunxi_sid_softc *sid_softc = NULL; 72 73 #define EFUSE_READ(sc, reg) \ 74 bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (sc)->sc_conf->efuse_offset + (reg)) 75 #define EFUSE_WRITE(sc, reg, val) \ 76 bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (sc)->sc_conf->efuse_offset + (reg), (val)) 77 78 static int 79 sunxi_sid_match(device_t parent, cfdata_t cf, void *aux) 80 { 81 struct fdt_attach_args * const faa = aux; 82 83 return of_match_compat_data(faa->faa_phandle, compat_data); 84 } 85 86 static void 87 sunxi_sid_attach(device_t parent, device_t self, void *aux) 88 { 89 struct sunxi_sid_softc * const sc = device_private(self); 90 struct fdt_attach_args * const faa = aux; 91 const int phandle = faa->faa_phandle; 92 bus_addr_t addr; 93 bus_size_t size; 94 95 if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) { 96 aprint_error(": couldn't get registers\n"); 97 return; 98 } 99 100 sc->sc_dev = self; 101 sc->sc_bst = faa->faa_bst; 102 if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) { 103 aprint_error(": couldn't map registers\n"); 104 return; 105 } 106 sc->sc_conf = (void *)of_search_compatible(phandle, compat_data)->data; 107 108 aprint_naive("\n"); 109 aprint_normal(": Security ID EFUSE\n"); 110 111 KASSERT(sid_softc == NULL); 112 sid_softc = sc; 113 } 114 115 CFATTACH_DECL_NEW(sunxi_sid, sizeof(struct sunxi_sid_softc), 116 sunxi_sid_match, sunxi_sid_attach, NULL, NULL); 117 118 int 119 sunxi_sid_read_tscalib(uint32_t *calib) 120 { 121 if (sid_softc == NULL) 122 return ENXIO; 123 124 calib[0] = EFUSE_READ(sid_softc, EFUSE_THERMAL_CALIB0); 125 calib[1] = EFUSE_READ(sid_softc, EFUSE_THERMAL_CALIB1); 126 return 0; 127 } 128