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