2 * Copyright (c) 2016 Jared McNeill <jmcneill@invisible.ca>
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
18 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
20 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
21 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * Allwinner secure ID controller
30 #include <sys/cdefs.h>
31 #include <sys/endian.h>
32 #include <sys/param.h>
33 #include <sys/systm.h>
36 #include <sys/kernel.h>
38 #include <sys/mutex.h>
39 #include <sys/module.h>
40 #include <sys/sysctl.h>
41 #include <machine/bus.h>
43 #include <dev/ofw/ofw_bus.h>
44 #include <dev/ofw/ofw_bus_subr.h>
46 #include <arm/allwinner/aw_sid.h>
51 * Starting at least from sun8iw6 (A83T) EFUSE starts at 0x200
52 * There is 3 registers in the low area to read/write protected EFUSE.
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
63 #define EFUSE_OFFSET 0x200
64 #define EFUSE_NAME_SIZE 32
65 #define EFUSE_DESC_SIZE 64
68 char name[EFUSE_NAME_SIZE];
69 char desc[EFUSE_DESC_SIZE];
73 enum aw_sid_fuse_id id;
77 static struct aw_sid_efuse a10_efuses[] = {
80 .desc = "Root Key or ChipID",
83 .id = AW_SID_FUSE_ROOTKEY,
88 static struct aw_sid_efuse a64_efuses[] = {
91 .desc = "Root Key or ChipID",
95 .id = AW_SID_FUSE_ROOTKEY,
99 .name = "calibration",
100 .desc = "Thermal Sensor Calibration Data",
101 .base = EFUSE_OFFSET,
104 .id = AW_SID_FUSE_THSSENSOR,
109 static struct aw_sid_efuse a83t_efuses[] = {
112 .desc = "Root Key or ChipID",
113 .base = EFUSE_OFFSET,
116 .id = AW_SID_FUSE_ROOTKEY,
120 .name = "calibration",
121 .desc = "Thermal Sensor Calibration Data",
122 .base = EFUSE_OFFSET,
125 .id = AW_SID_FUSE_THSSENSOR,
130 static struct aw_sid_efuse h3_efuses[] = {
133 .desc = "Root Key or ChipID",
134 .base = EFUSE_OFFSET,
137 .id = AW_SID_FUSE_ROOTKEY,
141 .name = "calibration",
142 .desc = "Thermal Sensor Calibration Data",
143 .base = EFUSE_OFFSET,
146 .id = AW_SID_FUSE_THSSENSOR,
151 static struct aw_sid_efuse h5_efuses[] = {
154 .desc = "Root Key or ChipID",
155 .base = EFUSE_OFFSET,
158 .id = AW_SID_FUSE_ROOTKEY,
162 .name = "calibration",
163 .desc = "Thermal Sensor Calibration Data",
164 .base = EFUSE_OFFSET,
167 .id = AW_SID_FUSE_THSSENSOR,
173 struct aw_sid_efuse *efuses;
177 static const struct aw_sid_conf a10_conf = {
178 .efuses = a10_efuses,
179 .nfuses = nitems(a10_efuses),
182 static const struct aw_sid_conf a20_conf = {
183 .efuses = a10_efuses,
184 .nfuses = nitems(a10_efuses),
187 static const struct aw_sid_conf a64_conf = {
188 .efuses = a64_efuses,
189 .nfuses = nitems(a64_efuses),
192 static const struct aw_sid_conf a83t_conf = {
193 .efuses = a83t_efuses,
194 .nfuses = nitems(a83t_efuses),
197 static const struct aw_sid_conf h3_conf = {
199 .nfuses = nitems(h3_efuses),
202 static const struct aw_sid_conf h5_conf = {
204 .nfuses = nitems(h5_efuses),
207 static struct ofw_compat_data compat_data[] = {
208 { "allwinner,sun4i-a10-sid", (uintptr_t)&a10_conf},
209 { "allwinner,sun7i-a20-sid", (uintptr_t)&a20_conf},
210 { "allwinner,sun50i-a64-sid", (uintptr_t)&a64_conf},
211 { "allwinner,sun8i-a83t-sid", (uintptr_t)&a83t_conf},
212 { "allwinner,sun8i-h3-sid", (uintptr_t)&h3_conf},
213 { "allwinner,sun50i-h5-sid", (uintptr_t)&h5_conf},
217 struct aw_sid_softc {
219 struct resource *res;
220 struct aw_sid_conf *sid_conf;
221 struct mtx prctl_mtx;
224 static struct aw_sid_softc *aw_sid_sc;
226 static struct resource_spec aw_sid_spec[] = {
227 { SYS_RES_MEMORY, 0, RF_ACTIVE },
231 #define RD1(sc, reg) bus_read_1((sc)->res, (reg))
232 #define RD4(sc, reg) bus_read_4((sc)->res, (reg))
233 #define WR4(sc, reg, val) bus_write_4((sc)->res, (reg), (val))
235 static int aw_sid_sysctl(SYSCTL_HANDLER_ARGS);
238 aw_sid_probe(device_t dev)
240 if (!ofw_bus_status_okay(dev))
243 if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
246 device_set_desc(dev, "Allwinner Secure ID Controller");
247 return (BUS_PROBE_DEFAULT);
251 aw_sid_attach(device_t dev)
253 struct aw_sid_softc *sc;
257 node = ofw_bus_get_node(dev);
258 sc = device_get_softc(dev);
261 if (bus_alloc_resources(dev, aw_sid_spec, &sc->res) != 0) {
262 device_printf(dev, "cannot allocate resources for device\n");
266 mtx_init(&sc->prctl_mtx, device_get_nameunit(dev), NULL, MTX_DEF);
267 sc->sid_conf = (struct aw_sid_conf *)ofw_bus_search_compatible(dev, compat_data)->ocd_data;
270 /* Register ourself so device can resolve who we are */
271 OF_device_register_xref(OF_xref_from_node(node), dev);
273 for (i = 0; i < sc->sid_conf->nfuses ;i++) {\
274 SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
275 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
276 OID_AUTO, sc->sid_conf->efuses[i].name,
277 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_NEEDGIANT,
278 dev, sc->sid_conf->efuses[i].id, aw_sid_sysctl,
279 "A", sc->sid_conf->efuses[i].desc);
285 aw_sid_get_fuse(enum aw_sid_fuse_id id, uint8_t *out, uint32_t *size)
287 struct aw_sid_softc *sc;
295 for (i = 0; i < sc->sid_conf->nfuses; i++)
296 if (id == sc->sid_conf->efuses[i].id)
299 if (i == sc->sid_conf->nfuses)
302 if (*size != sc->sid_conf->efuses[i].size) {
303 *size = sc->sid_conf->efuses[i].size;
310 if (sc->sid_conf->efuses[i].public == false)
311 mtx_lock(&sc->prctl_mtx);
312 for (j = 0; j < sc->sid_conf->efuses[i].size; j += 4) {
313 if (sc->sid_conf->efuses[i].public == false) {
314 val = SID_PRCTL_OFFSET(sc->sid_conf->efuses[i].offset + j) |
317 WR4(sc, SID_PRCTL, val);
318 /* Read bit will be cleared once read has concluded */
319 while (RD4(sc, SID_PRCTL) & SID_PRCTL_READ)
321 val = RD4(sc, SID_RDKEY);
323 val = RD4(sc, sc->sid_conf->efuses[i].base +
324 sc->sid_conf->efuses[i].offset + j);
327 out[j + 1] = (val & 0xFF00) >> 8;
329 out[j + 2] = (val & 0xFF0000) >> 16;
331 out[j + 3] = (val & 0xFF000000) >> 24;
333 if (sc->sid_conf->efuses[i].public == false)
334 mtx_unlock(&sc->prctl_mtx);
340 aw_sid_read(device_t dev, uint32_t offset, uint32_t size, uint8_t *buffer)
342 struct aw_sid_softc *sc;
343 enum aw_sid_fuse_id fuse_id = 0;
346 sc = device_get_softc(dev);
348 for (i = 0; i < sc->sid_conf->nfuses; i++)
349 if (offset == sc->sid_conf->efuses[i].offset) {
350 fuse_id = sc->sid_conf->efuses[i].id;
357 return (aw_sid_get_fuse(fuse_id, buffer, &size));
361 aw_sid_sysctl(SYSCTL_HANDLER_ARGS)
364 enum aw_sid_fuse_id fuse = arg2;
370 /* Get the size of the efuse data */
372 aw_sid_get_fuse(fuse, NULL, &size);
373 /* We now have the real size */
374 ret = aw_sid_get_fuse(fuse, data, &size);
376 device_printf(dev, "Cannot get fuse id %d: %d\n", fuse, ret);
380 for (i = 0; i < size; i++)
381 snprintf(out + (i * 2), sizeof(out) - (i * 2),
384 return sysctl_handle_string(oidp, out, sizeof(out), req);
387 static device_method_t aw_sid_methods[] = {
388 /* Device interface */
389 DEVMETHOD(device_probe, aw_sid_probe),
390 DEVMETHOD(device_attach, aw_sid_attach),
392 /* NVMEM interface */
393 DEVMETHOD(nvmem_read, aw_sid_read),
397 static driver_t aw_sid_driver = {
400 sizeof(struct aw_sid_softc),
403 EARLY_DRIVER_MODULE(aw_sid, simplebus, aw_sid_driver, 0, 0,
404 BUS_PASS_SUPPORTDEV + BUS_PASS_ORDER_FIRST);
405 MODULE_VERSION(aw_sid, 1);
406 SIMPLEBUS_PNP_INFO(compat_data);