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