xref: /freebsd/sys/arm/allwinner/aw_sid.c (revision 6dd381416b13b1c81139ce4168cedd0d46e28bf4)
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 #define	SID_THERMAL_CALIB0	(SID_SRAM + 0x34)
65 #define	SID_THERMAL_CALIB1	(SID_SRAM + 0x38)
66 
67 #define	ROOT_KEY_SIZE		4
68 
69 struct aw_sid_conf {
70 	bus_size_t	efuse_size;
71 	bus_size_t	rootkey_offset;
72 	bool		has_prctl;
73 	bool		has_thermal;
74 	bool		requires_prctl_read;
75 };
76 
77 static const struct aw_sid_conf a10_conf = {
78 	.efuse_size = 0x10,
79 	.rootkey_offset = 0,
80 };
81 
82 static const struct aw_sid_conf a20_conf = {
83 	.efuse_size = 0x10,
84 	.rootkey_offset = 0,
85 };
86 
87 static const struct aw_sid_conf a64_conf = {
88 	.efuse_size = 0x100,
89 	.rootkey_offset = SID_SRAM,
90 	.has_prctl = true,
91 	.has_thermal = true,
92 };
93 
94 static const struct aw_sid_conf a83t_conf = {
95 	.efuse_size = 0x100,
96 	.rootkey_offset = SID_SRAM,
97 	.has_prctl = true,
98 	.has_thermal = true,
99 };
100 
101 static const struct aw_sid_conf h3_conf = {
102 	.efuse_size = 0x100,
103 	.rootkey_offset = SID_SRAM,
104 	.has_prctl = true,
105 	.has_thermal = true,
106 	.requires_prctl_read = true,
107 };
108 
109 static struct ofw_compat_data compat_data[] = {
110 	{ "allwinner,sun4i-a10-sid",		(uintptr_t)&a10_conf},
111 	{ "allwinner,sun7i-a20-sid",		(uintptr_t)&a20_conf},
112 	{ "allwinner,sun50i-a64-sid",		(uintptr_t)&a64_conf},
113 	{ "allwinner,sun8i-a83t-sid",		(uintptr_t)&a83t_conf},
114 	{ "allwinner,sun8i-h3-sid",		(uintptr_t)&h3_conf},
115 	{ NULL,					0 }
116 };
117 
118 struct aw_sid_softc {
119 	struct resource		*res;
120 	struct aw_sid_conf	*sid_conf;
121 	struct mtx		prctl_mtx;
122 };
123 
124 static struct aw_sid_softc *aw_sid_sc;
125 
126 static struct resource_spec aw_sid_spec[] = {
127 	{ SYS_RES_MEMORY,	0,	RF_ACTIVE },
128 	{ -1, 0 }
129 };
130 
131 enum sid_keys {
132 	AW_SID_ROOT_KEY,
133 };
134 
135 #define	RD4(sc, reg)		bus_read_4((sc)->res, (reg))
136 #define	WR4(sc, reg, val)	bus_write_4((sc)->res, (reg), (val))
137 
138 static int aw_sid_sysctl(SYSCTL_HANDLER_ARGS);
139 static int aw_sid_prctl_read(device_t dev, bus_size_t offset, uint32_t *val);
140 
141 
142 /*
143  * offset here is offset into efuse space, rather than offset into sid register
144  * space. This form of read is only an option for newer SoC: A83t, H3, A64
145  */
146 static int
147 aw_sid_prctl_read(device_t dev, bus_size_t offset, uint32_t *val)
148 {
149 	struct aw_sid_softc *sc;
150 	uint32_t readval;
151 
152 	sc = device_get_softc(dev);
153 	if (!sc->sid_conf->has_prctl)
154 		return (1);
155 
156 	mtx_lock(&sc->prctl_mtx);
157 	readval = SID_PRCTL_OFFSET(offset) | SID_PRCTL_LOCK | SID_PRCTL_READ;
158 	WR4(sc, SID_PRCTL, readval);
159 	/* Read bit will be cleared once read has concluded */
160 	while (RD4(sc, SID_PRCTL) & SID_PRCTL_READ)
161 		continue;
162 	readval = RD4(sc, SID_RDKEY);
163 	mtx_unlock(&sc->prctl_mtx);
164 	*val = readval;
165 
166 	return (0);
167 }
168 
169 static int
170 aw_sid_probe(device_t dev)
171 {
172 	if (!ofw_bus_status_okay(dev))
173 		return (ENXIO);
174 
175 	if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
176 		return (ENXIO);
177 
178 	device_set_desc(dev, "Allwinner Secure ID Controller");
179 	return (BUS_PROBE_DEFAULT);
180 }
181 
182 static int
183 aw_sid_attach(device_t dev)
184 {
185 	struct aw_sid_softc *sc;
186 	bus_size_t i;
187 	uint32_t val;
188 
189 	sc = device_get_softc(dev);
190 
191 	if (bus_alloc_resources(dev, aw_sid_spec, &sc->res) != 0) {
192 		device_printf(dev, "cannot allocate resources for device\n");
193 		return (ENXIO);
194 	}
195 
196 	mtx_init(&sc->prctl_mtx, device_get_nameunit(dev), NULL, MTX_DEF);
197 	sc->sid_conf = (struct aw_sid_conf *)ofw_bus_search_compatible(dev, compat_data)->ocd_data;
198 	aw_sid_sc = sc;
199 
200 	/*
201 	 * This set of reads is solely for working around a silicon bug on some
202 	 * SoC that require a prctl read in order for direct register access to
203 	 * return a non-garbled value. Hence, the values we read are simply
204 	 * ignored.
205 	 */
206 	if (sc->sid_conf->requires_prctl_read)
207 		for (i = 0; i < sc->sid_conf->efuse_size; i += 4)
208 			if (aw_sid_prctl_read(dev, i, &val) != 0) {
209 				device_printf(dev, "failed prctl read\n");
210 				goto fail;
211 			}
212 
213 	SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
214 	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
215 	    OID_AUTO, "rootkey",
216 	    CTLTYPE_STRING | CTLFLAG_RD,
217 	    dev, AW_SID_ROOT_KEY, aw_sid_sysctl, "A", "Root Key");
218 
219 	return (0);
220 
221 fail:
222 	bus_release_resources(dev, aw_sid_spec, &sc->res);
223 	mtx_destroy(&sc->prctl_mtx);
224 	return (ENXIO);
225 }
226 
227 int
228 aw_sid_read_tscalib(uint32_t *calib0, uint32_t *calib1)
229 {
230 	struct aw_sid_softc *sc;
231 
232 	sc = aw_sid_sc;
233 	if (sc == NULL)
234 		return (ENXIO);
235 	if (!sc->sid_conf->has_thermal)
236 		return (ENXIO);
237 
238 	*calib0 = RD4(sc, SID_THERMAL_CALIB0);
239 	*calib1 = RD4(sc, SID_THERMAL_CALIB1);
240 
241 	return (0);
242 }
243 
244 int
245 aw_sid_get_rootkey(u_char *out)
246 {
247 	struct aw_sid_softc *sc;
248 	int i;
249 	bus_size_t root_key_off;
250 	u_int tmp;
251 
252 	sc = aw_sid_sc;
253 	if (sc == NULL)
254 		return (ENXIO);
255 	root_key_off = aw_sid_sc->sid_conf->rootkey_offset;
256 	for (i = 0; i < ROOT_KEY_SIZE ; i++) {
257 		tmp = RD4(aw_sid_sc, root_key_off + (i * 4));
258 		be32enc(&out[i * 4], tmp);
259 	}
260 
261 	return (0);
262 }
263 
264 static int
265 aw_sid_sysctl(SYSCTL_HANDLER_ARGS)
266 {
267 	enum sid_keys key = arg2;
268 	u_char rootkey[16];
269 	char out[33];
270 
271 	if (key != AW_SID_ROOT_KEY)
272 		return (ENOENT);
273 
274 	if (aw_sid_get_rootkey(rootkey) != 0)
275 		return (ENOENT);
276 	snprintf(out, sizeof(out),
277 	  "%16D", rootkey, "");
278 
279 	return sysctl_handle_string(oidp, out, sizeof(out), req);
280 }
281 
282 static device_method_t aw_sid_methods[] = {
283 	/* Device interface */
284 	DEVMETHOD(device_probe,		aw_sid_probe),
285 	DEVMETHOD(device_attach,	aw_sid_attach),
286 
287 	DEVMETHOD_END
288 };
289 
290 static driver_t aw_sid_driver = {
291 	"aw_sid",
292 	aw_sid_methods,
293 	sizeof(struct aw_sid_softc),
294 };
295 
296 static devclass_t aw_sid_devclass;
297 
298 EARLY_DRIVER_MODULE(aw_sid, simplebus, aw_sid_driver, aw_sid_devclass, 0, 0,
299     BUS_PASS_RESOURCE + BUS_PASS_ORDER_FIRST);
300 MODULE_VERSION(aw_sid, 1);
301