]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/arm/mv/mv_ts.c
MFV 364468:
[FreeBSD/FreeBSD.git] / sys / arm / mv / mv_ts.c
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2012 Hiroki Sato <hrs@FreeBSD.org>
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 AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, 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  * Driver for on-die thermal sensor in 88F6282 and 88F6283.
29  */
30
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
33
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/module.h>
38 #include <sys/bus.h>
39 #include <sys/sysctl.h>
40
41 #include <dev/ofw/ofw_bus.h>
42 #include <dev/ofw/ofw_bus_subr.h>
43
44 #include <arm/mv/mvreg.h>
45 #include <arm/mv/mvvar.h>
46
47 static struct resource_spec mvts_res[] = {
48         { SYS_RES_MEMORY,       0,      RF_ACTIVE },
49         { -1, 0 }
50 };
51
52 struct mvts_softc {
53         device_t                sc_dev;
54         struct resource         *sc_res[sizeof(mvts_res)];
55 };
56
57 static int
58 ts_probe(device_t dev)
59 {
60         uint32_t d, r;
61
62         if (!ofw_bus_status_okay(dev))
63                 return (ENXIO);
64
65         if (!ofw_bus_is_compatible(dev, "mrvl,ts"))
66                 return (ENXIO);
67         soc_id(&d, &r);
68         switch (d) {
69         case MV_DEV_88F6282:
70                 break;
71         default:
72                 device_printf(dev, "unsupported SoC (ID: 0x%08X)!\n", d);
73                 return (ENXIO);
74         }
75         device_set_desc(dev, "Marvell Thermal Sensor");
76
77         return (0);
78 }
79
80 #define MV_TEMP_VALID_BIT       (1 << 9)
81 #define MV_TEMP_SENS_OFFS       10
82 #define MV_TEMP_SENS_MASK       0x1ff
83 #define MV_TEMP_SENS_READ_MAX   16
84 #define TZ_ZEROC                2731
85 #define MV_TEMP_CONVERT(x)      ((((322 - x) * 100000) / 13625) + TZ_ZEROC)
86
87 /*
88  * MSB                                 LSB
89  * 0000 0000 0000 0000 0000 0000 0000 0000
90  *                             ^- valid bit
91  *                  |---------|
92  *                         ^--- temperature (9 bits)
93  */
94
95 static int
96 ts_sysctl_handler(SYSCTL_HANDLER_ARGS)
97 {
98         struct mvts_softc *sc;
99         device_t dev;
100         uint32_t ret, ret0;
101         u_int val;
102         int i;
103
104         dev = (device_t)arg1;
105         sc = device_get_softc(dev);
106         val = TZ_ZEROC;
107
108         ret = bus_read_4(sc->sc_res[0], 0);
109         if ((ret & MV_TEMP_VALID_BIT) == 0) {
110                 device_printf(dev, "temperature sensor is broken.\n");
111                 goto ts_sysctl_handle_int;
112         }
113         ret0 = 0;
114         for (i = 0; i < MV_TEMP_SENS_READ_MAX; i++) {
115                 ret = bus_read_4(sc->sc_res[0], 0);
116                 ret = (ret >> MV_TEMP_SENS_OFFS) & MV_TEMP_SENS_MASK;
117
118                 /*
119                  * Successive reads should returns the same value except
120                  * for the LSB when the sensor is normal.
121                  */
122                 if (((ret0 ^ ret) & 0x1fe) == 0)
123                         break;
124                 else
125                         ret0 = ret;
126         }
127         if (i == MV_TEMP_SENS_READ_MAX) {
128                 device_printf(dev, "temperature sensor is unstable.\n");
129                 goto ts_sysctl_handle_int;
130         }
131         val = (u_int)MV_TEMP_CONVERT(ret);
132
133 ts_sysctl_handle_int:
134         return (sysctl_handle_int(oidp, &val, 0, req));
135 }
136
137 static int
138 ts_attach(device_t dev)
139 {
140         struct mvts_softc *sc;
141         struct sysctl_ctx_list *ctx;
142         int error;
143
144         sc = device_get_softc(dev);
145         sc->sc_dev = dev;
146         error = bus_alloc_resources(dev, mvts_res, sc->sc_res);
147         if (error) {
148                 device_printf(dev, "could not allocate resources\n");
149                 return (ENXIO);
150         }
151         ctx = device_get_sysctl_ctx(dev);
152         SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
153             OID_AUTO, "temperature",
154             CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_NEEDGIANT, dev,
155             0, ts_sysctl_handler, "IK", "Current Temperature");
156
157         return (0);
158 }
159
160 static int
161 ts_detach(device_t dev)
162 {
163
164         return (0);
165 }
166
167 static device_method_t ts_methods[] = {
168         DEVMETHOD(device_probe,         ts_probe),
169         DEVMETHOD(device_attach,        ts_attach),
170         DEVMETHOD(device_detach,        ts_detach),
171         {0, 0},
172 };
173
174 static driver_t ts_driver = {
175         "mvts",
176         ts_methods,
177         sizeof(struct mvts_softc),
178 };
179
180 static devclass_t ts_devclass;
181 DRIVER_MODULE(mvts, simplebus, ts_driver, ts_devclass, 0, 0);
182 MODULE_VERSION(mvts, 1);