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