tadpmu.c revision 1.1 1 /*/* $NetBSD: tadpmu.c,v 1.1 2018/10/12 21:44:32 macallan Exp $ */
2
3 /*-
4 * Copyright (c) 2018 Michael Lorenz <macallan (at) netbsd.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 NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 /* a driver for the PMU found in Tadpole Wiper and possibly SPARCle laptops */
30
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/kernel.h>
34 #include <sys/device.h>
35 #include <sys/malloc.h>
36 #include <sys/bus.h>
37 #include <sys/intr.h>
38
39 #include <dev/sysmon/sysmonvar.h>
40
41 #include <sparc64/dev/tadpmureg.h>
42 #include <sparc64/dev/tadpmuvar.h>
43
44 static bus_space_tag_t tadpmu_iot;
45 static bus_space_handle_t tadpmu_hcmd;
46 static bus_space_handle_t tadpmu_hdata;
47 static struct sysmon_envsys *tadpmu_sme;
48 static envsys_data_t tadpmu_sensors[5];
49
50 static inline void
51 tadpmu_cmd(uint8_t d)
52 {
53 bus_space_write_1(tadpmu_iot, tadpmu_hcmd, 0, d);
54 }
55
56 static inline void
57 tadpmu_wdata(uint8_t d)
58 {
59 bus_space_write_1(tadpmu_iot, tadpmu_hdata, 0, d);
60 }
61
62 static inline uint8_t
63 tadpmu_status(void)
64 {
65 return bus_space_read_1(tadpmu_iot, tadpmu_hcmd, 0);
66 }
67
68 static inline uint8_t
69 tadpmu_data(void)
70 {
71 return bus_space_read_1(tadpmu_iot, tadpmu_hdata, 0);
72 }
73
74 static void
75 tadpmu_flush(void)
76 {
77 volatile uint8_t junk, d;
78 int bail = 0;
79
80 d = tadpmu_status();
81 while (d & STATUS_HAVE_DATA) {
82 junk = tadpmu_data();
83 __USE(junk);
84 delay(10);
85 bail++;
86 if (bail > 100) {
87 printf("%s: timeout waiting for data out to clear %2x\n",
88 __func__, d);
89 break;
90 }
91 d = tadpmu_status();
92 }
93 bail = 0;
94 d = tadpmu_status();
95 while (d & STATUS_SEND_DATA) {
96 bus_space_write_1(tadpmu_iot, tadpmu_hdata, 0, 0);
97 delay(10);
98 bail++;
99 if (bail > 100) {
100 printf("%s: timeout waiting for data in to clear %02x\n",
101 __func__, d);
102 break;
103 }
104 d = tadpmu_status();
105 }
106 }
107
108 static void
109 tadpmu_send_cmd(uint8_t cmd)
110 {
111 int bail = 0;
112 uint8_t d;
113
114 tadpmu_cmd(cmd);
115
116 d = tadpmu_status();
117 while ((d & STATUS_CMD_IN_PROGRESS) == 0) {
118 delay(10);
119 bail++;
120 if (bail > 100) {
121 printf("%s: timeout waiting for command to start\n",
122 __func__);
123 break;
124 }
125 d = tadpmu_status();
126 }
127 }
128
129 static uint8_t
130 tadpmu_recv(void)
131 {
132 int bail = 0;
133 uint8_t d;
134
135 d = tadpmu_status();
136 while ((d & STATUS_HAVE_DATA) == 0) {
137 delay(10);
138 bail++;
139 if (bail > 1000) {
140 printf("%s: timeout waiting for data %02x\n", __func__, d);
141 break;
142 }
143 d = tadpmu_status();
144 }
145 return bus_space_read_1(tadpmu_iot, tadpmu_hdata, 0);
146 }
147
148 static void
149 tadpmu_send(uint8_t v)
150 {
151 int bail = 0;
152 uint8_t d;
153
154 d = tadpmu_status();
155 while ((d & STATUS_SEND_DATA) == 0) {
156 delay(10);
157 bail++;
158 if (bail > 1000) {
159 printf("%s: timeout waiting for PMU ready %02x\n", __func__, d);
160 break;
161 }
162 d = tadpmu_status();
163 }
164
165 tadpmu_wdata(v);
166
167 while ((d & STATUS_SEND_DATA) != 0) {
168 delay(10);
169 bail++;
170 if (bail > 1000) {
171 printf("%s: timeout waiting for accept data %02x\n", __func__, d);
172 break;
173 }
174 d = tadpmu_status();
175 }
176 }
177
178 static void
179 tadpmu_sensors_refresh(struct sysmon_envsys *sme, envsys_data_t *edata)
180 {
181 int res;
182 if (edata->private > 0) {
183 tadpmu_flush();
184 tadpmu_send_cmd(edata->private);
185 res = tadpmu_recv();
186 if (edata->units == ENVSYS_STEMP) {
187 edata->value_cur = res * 1000000 + 273150000;
188 } else {
189 edata->value_cur = res;
190 }
191 edata->state = ENVSYS_SVALID;
192 } else {
193 edata->state = ENVSYS_SINVALID;
194 }
195 }
196
197 int
198 tadpmu_init(bus_space_tag_t t, bus_space_handle_t hcmd, bus_space_handle_t hdata)
199 {
200 int ver;
201
202 tadpmu_iot = t;
203 tadpmu_hcmd = hcmd;
204 tadpmu_hdata = hdata;
205
206 tadpmu_flush();
207 delay(1000);
208
209 tadpmu_send_cmd(CMD_READ_VERSION);
210 ver = tadpmu_recv();
211 printf("Tadpole PMU Version 1.%d\n", ver);
212
213 tadpmu_send_cmd(CMD_SET_OPMODE);
214 tadpmu_send(0x75);
215
216 tadpmu_send_cmd(CMD_READ_SYSTEMP);
217 ver = tadpmu_recv();
218 printf("Temperature %d\n", ver);
219
220 tadpmu_send_cmd(CMD_READ_VBATT);
221 ver = tadpmu_recv();
222 printf("Battery voltage %d\n", ver);
223
224 tadpmu_send_cmd(CMD_READ_GENSTAT);
225 ver = tadpmu_recv();
226 printf("status %02x\n", ver);
227
228 tadpmu_sme = sysmon_envsys_create();
229 tadpmu_sme->sme_name = "tadpmu";
230 tadpmu_sme->sme_cookie = NULL;
231 tadpmu_sme->sme_refresh = tadpmu_sensors_refresh;
232
233 tadpmu_sensors[0].units = ENVSYS_STEMP;
234 tadpmu_sensors[0].private = CMD_READ_SYSTEMP;
235 strcpy(tadpmu_sensors[0].desc, "systemp");
236 sysmon_envsys_sensor_attach(tadpmu_sme, &tadpmu_sensors[0]);
237 #ifdef TADPMU_DEBUG
238 tadpmu_sensors[1].units = ENVSYS_INTEGER;
239 tadpmu_sensors[1].private = 0x17;
240 strcpy(tadpmu_sensors[1].desc, "reg 17");
241 sysmon_envsys_sensor_attach(tadpmu_sme, &tadpmu_sensors[1]);
242 tadpmu_sensors[2].units = ENVSYS_INTEGER;
243 tadpmu_sensors[2].private = 0x18;
244 strcpy(tadpmu_sensors[2].desc, "reg 18");
245 sysmon_envsys_sensor_attach(tadpmu_sme, &tadpmu_sensors[2]);
246 tadpmu_sensors[3].units = ENVSYS_INTEGER;
247 tadpmu_sensors[3].private = CMD_READ_GENSTAT;
248 strcpy(tadpmu_sensors[3].desc, "genstat");
249 sysmon_envsys_sensor_attach(tadpmu_sme, &tadpmu_sensors[3]);
250 #if 0
251 tadpmu_sensors[4].units = ENVSYS_INTEGER;
252 tadpmu_sensors[4].private = CMD_READ_VBATT;
253 strcpy(tadpmu_sensors[4].desc, "Vbatt");
254 sysmon_envsys_sensor_attach(tadpmu_sme, &tadpmu_sensors[4]);
255 #endif
256 #endif
257 sysmon_envsys_register(tadpmu_sme);
258
259 return 0;
260 }
261
262