xref: /netbsd-src/sys/arch/arm/sunxi/sunxi_sid.c (revision c7941324122a924b6e20c322a6b49a6a3b6137e7)
1 /* $NetBSD: sunxi_sid.c,v 1.8 2024/08/13 07:20:23 skrll Exp $ */
2 
3 /*-
4  * Copyright (c) 2017 Jared McNeill <jmcneill@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.8 2024/08/13 07:20:23 skrll 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 device_compatible_entry compat_data[] = {
58 	{ .compat = "allwinner,sun4i-a10-sid",	.data = &sun4i_a10_sid_config },
59 	{ .compat = "allwinner,sun7i-a20-sid",	.data = &sun4i_a10_sid_config },
60 	{ .compat = "allwinner,sun8i-h3-sid",	.data = &sun8i_h3_sid_config },
61 	{ .compat = "allwinner,sun8i-a83t-sid",	.data = &sun8i_h3_sid_config },
62 	{ .compat = "allwinner,sun20i-d1-sid",	.data = &sun8i_h3_sid_config },
63 	{ .compat = "allwinner,sun50i-a64-sid",	.data = &sun8i_h3_sid_config },
64 	DEVICE_COMPAT_EOL
65 };
66 
67 struct sunxi_sid_softc {
68 	device_t			sc_dev;
69 	bus_space_tag_t			sc_bst;
70 	bus_space_handle_t		sc_bsh;
71 	const struct sunxi_sid_config	*sc_conf;
72 };
73 
74 static struct sunxi_sid_softc *sid_softc = NULL;
75 
76 #define EFUSE_READ(sc, reg) \
77     bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (sc)->sc_conf->efuse_offset + (reg))
78 #define EFUSE_WRITE(sc, reg, val) \
79     bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (sc)->sc_conf->efuse_offset + (reg), (val))
80 
81 static int
82 sunxi_sid_match(device_t parent, cfdata_t cf, void *aux)
83 {
84 	struct fdt_attach_args * const faa = aux;
85 
86 	return of_compatible_match(faa->faa_phandle, compat_data);
87 }
88 
89 static void
90 sunxi_sid_attach(device_t parent, device_t self, void *aux)
91 {
92 	struct sunxi_sid_softc * const sc = device_private(self);
93 	struct fdt_attach_args * const faa = aux;
94 	const int phandle = faa->faa_phandle;
95 	bus_addr_t addr;
96 	bus_size_t size;
97 
98 	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
99 		aprint_error(": couldn't get registers\n");
100 		return;
101 	}
102 
103 	sc->sc_dev = self;
104 	sc->sc_bst = faa->faa_bst;
105 	if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) {
106 		aprint_error(": couldn't map registers\n");
107 		return;
108 	}
109 	sc->sc_conf = of_compatible_lookup(phandle, compat_data)->data;
110 
111 	aprint_naive("\n");
112 	aprint_normal(": Security ID EFUSE\n");
113 
114 	KASSERT(sid_softc == NULL);
115 	sid_softc = sc;
116 }
117 
118 CFATTACH_DECL_NEW(sunxi_sid, sizeof(struct sunxi_sid_softc),
119 	sunxi_sid_match, sunxi_sid_attach, NULL, NULL);
120 
121 int
122 sunxi_sid_read_tscalib(uint32_t *calib)
123 {
124 	if (sid_softc == NULL)
125 		return ENXIO;
126 
127 	calib[0] = EFUSE_READ(sid_softc, EFUSE_THERMAL_CALIB0);
128 	calib[1] = EFUSE_READ(sid_softc, EFUSE_THERMAL_CALIB1);
129 	return 0;
130 }
131