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