tadpmu.c revision 1.2 1 /*/* $NetBSD: tadpmu.c,v 1.2 2018/10/13 19:53:43 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/proc.h>
37 #include <sys/bus.h>
38 #include <sys/intr.h>
39
40 #include <dev/sysmon/sysmonvar.h>
41 #include <dev/sysmon/sysmon_taskq.h>
42
43 #include <sparc64/dev/tadpmureg.h>
44 #include <sparc64/dev/tadpmuvar.h>
45
46 #ifdef TADPMU_DEBUG
47 #define DPRINTF printf
48 #else
49 #define DPRINTF while (0) printf
50 #endif
51
52 static bus_space_tag_t tadpmu_iot;
53 static bus_space_handle_t tadpmu_hcmd;
54 static bus_space_handle_t tadpmu_hdata;
55 static struct sysmon_envsys *tadpmu_sme;
56 static envsys_data_t tadpmu_sensors[5];
57 static uint8_t idata = 0xff;
58 static uint8_t ivalid = 0;
59 static wchan_t tadpmu;
60 static struct sysmon_pswitch tadpmu_pbutton;
61
62 static inline void
63 tadpmu_cmd(uint8_t d)
64 {
65 bus_space_write_1(tadpmu_iot, tadpmu_hcmd, 0, d);
66 }
67
68 static inline void
69 tadpmu_wdata(uint8_t d)
70 {
71 bus_space_write_1(tadpmu_iot, tadpmu_hdata, 0, d);
72 }
73
74 static inline uint8_t
75 tadpmu_status(void)
76 {
77 return bus_space_read_1(tadpmu_iot, tadpmu_hcmd, 0);
78 }
79
80 static inline uint8_t
81 tadpmu_data(void)
82 {
83 return bus_space_read_1(tadpmu_iot, tadpmu_hdata, 0);
84 }
85
86 static void
87 tadpmu_flush(void)
88 {
89 volatile uint8_t junk, d;
90 int bail = 0;
91
92 d = tadpmu_status();
93 while (d & STATUS_HAVE_DATA) {
94 junk = tadpmu_data();
95 __USE(junk);
96 delay(10);
97 bail++;
98 if (bail > 100) {
99 printf("%s: timeout waiting for data out to clear %2x\n",
100 __func__, d);
101 break;
102 }
103 d = tadpmu_status();
104 }
105 bail = 0;
106 d = tadpmu_status();
107 while (d & STATUS_SEND_DATA) {
108 bus_space_write_1(tadpmu_iot, tadpmu_hdata, 0, 0);
109 delay(10);
110 bail++;
111 if (bail > 100) {
112 printf("%s: timeout waiting for data in to clear %02x\n",
113 __func__, d);
114 break;
115 }
116 d = tadpmu_status();
117 }
118 }
119
120 static void
121 tadpmu_send_cmd(uint8_t cmd)
122 {
123 int bail = 0;
124 uint8_t d;
125
126 ivalid = 0;
127 tadpmu_cmd(cmd);
128
129 d = tadpmu_status();
130 while ((d & STATUS_CMD_IN_PROGRESS) == 0) {
131 delay(10);
132 bail++;
133 if (bail > 100) {
134 printf("%s: timeout waiting for command to start\n",
135 __func__);
136 break;
137 }
138 d = tadpmu_status();
139 }
140 }
141
142 static uint8_t
143 tadpmu_recv(void)
144 {
145 int bail = 0;
146 uint8_t d;
147
148 if (cold) {
149 d = tadpmu_status();
150 while ((d & STATUS_HAVE_DATA) == 0) {
151 delay(10);
152 bail++;
153 if (bail > 1000) {
154 printf("%s: timeout waiting for data %02x\n",
155 __func__, d);
156 break;
157 }
158 d = tadpmu_status();
159 }
160 return bus_space_read_1(tadpmu_iot, tadpmu_hdata, 0);
161 } else {
162 while (ivalid == 0)
163 tsleep(tadpmu, 0, "pmucmd", 1);
164 return idata;
165 }
166 }
167
168 static void
169 tadpmu_send(uint8_t v)
170 {
171 int bail = 0;
172 uint8_t d;
173
174 d = tadpmu_status();
175 while ((d & STATUS_SEND_DATA) == 0) {
176 delay(10);
177 bail++;
178 if (bail > 1000) {
179 printf("%s: timeout waiting for PMU ready %02x\n", __func__, d);
180 break;
181 }
182 d = tadpmu_status();
183 }
184
185 tadpmu_wdata(v);
186
187 while ((d & STATUS_SEND_DATA) != 0) {
188 delay(10);
189 bail++;
190 if (bail > 1000) {
191 printf("%s: timeout waiting for accept data %02x\n", __func__, d);
192 break;
193 }
194 d = tadpmu_status();
195 }
196 }
197
198 static void
199 tadpmu_sensors_refresh(struct sysmon_envsys *sme, envsys_data_t *edata)
200 {
201 int res;
202 if (edata->private > 0) {
203 tadpmu_flush();
204 tadpmu_send_cmd(edata->private);
205 res = tadpmu_recv();
206 if (edata->units == ENVSYS_STEMP) {
207 edata->value_cur = res * 1000000 + 273150000;
208 } else {
209 edata->value_cur = res;
210 }
211 edata->state = ENVSYS_SVALID;
212 } else {
213 edata->state = ENVSYS_SINVALID;
214 }
215 }
216
217 int
218 tadpmu_intr(void *cookie)
219 {
220 uint8_t s = tadpmu_status(), d;
221 if (s & STATUS_INTR) {
222 /* interrupt message */
223 d = tadpmu_data();
224 DPRINTF("status change %02x\n", d);
225 switch (d) {
226 case TADPMU_POWERBUTTON:
227 sysmon_pswitch_event(&tadpmu_pbutton,
228 PSWITCH_EVENT_PRESSED);
229 break;
230 case TADPMU_LID:
231 /* read genstat and report lid */
232 break;
233 }
234 }
235 s = tadpmu_status();
236 if (s & STATUS_HAVE_DATA) {
237 idata = tadpmu_data();
238 ivalid = 1;
239 wakeup(tadpmu);
240 DPRINTF("%s data %02x\n", __func__, idata);
241 }
242
243 return 1;
244 }
245
246 int
247 tadpmu_init(bus_space_tag_t t, bus_space_handle_t hcmd, bus_space_handle_t hdata)
248 {
249 int ver;
250
251 tadpmu_iot = t;
252 tadpmu_hcmd = hcmd;
253 tadpmu_hdata = hdata;
254
255 tadpmu_flush();
256 delay(1000);
257
258 tadpmu_send_cmd(CMD_READ_VERSION);
259 ver = tadpmu_recv();
260 printf("Tadpole PMU Version 1.%d\n", ver);
261
262 tadpmu_send_cmd(CMD_SET_OPMODE);
263 tadpmu_send(0x75);
264
265 tadpmu_send_cmd(CMD_READ_SYSTEMP);
266 ver = tadpmu_recv();
267 printf("Temperature %d\n", ver);
268
269 tadpmu_send_cmd(CMD_READ_VBATT);
270 ver = tadpmu_recv();
271 printf("Battery voltage %d\n", ver);
272
273 tadpmu_send_cmd(CMD_READ_GENSTAT);
274 ver = tadpmu_recv();
275 printf("status %02x\n", ver);
276
277 tadpmu_sme = sysmon_envsys_create();
278 tadpmu_sme->sme_name = "tadpmu";
279 tadpmu_sme->sme_cookie = NULL;
280 tadpmu_sme->sme_refresh = tadpmu_sensors_refresh;
281
282 tadpmu_sensors[0].units = ENVSYS_STEMP;
283 tadpmu_sensors[0].private = CMD_READ_SYSTEMP;
284 strcpy(tadpmu_sensors[0].desc, "systemp");
285 sysmon_envsys_sensor_attach(tadpmu_sme, &tadpmu_sensors[0]);
286 #ifdef TADPMU_DEBUG
287 tadpmu_sensors[1].units = ENVSYS_INTEGER;
288 tadpmu_sensors[1].private = 0x17;
289 strcpy(tadpmu_sensors[1].desc, "reg 17");
290 sysmon_envsys_sensor_attach(tadpmu_sme, &tadpmu_sensors[1]);
291 tadpmu_sensors[2].units = ENVSYS_INTEGER;
292 tadpmu_sensors[2].private = 0x18;
293 strcpy(tadpmu_sensors[2].desc, "reg 18");
294 sysmon_envsys_sensor_attach(tadpmu_sme, &tadpmu_sensors[2]);
295 tadpmu_sensors[3].units = ENVSYS_INTEGER;
296 tadpmu_sensors[3].private = CMD_READ_GENSTAT;
297 strcpy(tadpmu_sensors[3].desc, "genstat");
298 sysmon_envsys_sensor_attach(tadpmu_sme, &tadpmu_sensors[3]);
299 #if 0
300 tadpmu_sensors[4].units = ENVSYS_INTEGER;
301 tadpmu_sensors[4].private = CMD_READ_VBATT;
302 strcpy(tadpmu_sensors[4].desc, "Vbatt");
303 sysmon_envsys_sensor_attach(tadpmu_sme, &tadpmu_sensors[4]);
304 #endif
305 #endif
306 sysmon_envsys_register(tadpmu_sme);
307
308 sysmon_task_queue_init();
309 memset(&tadpmu_pbutton, 0, sizeof(struct sysmon_pswitch));
310 tadpmu_pbutton.smpsw_name = "tadpmu";
311 tadpmu_pbutton.smpsw_type = PSWITCH_TYPE_POWER;
312 if (sysmon_pswitch_register(&tadpmu_pbutton) != 0)
313 aprint_error(
314 "unable to register power button with sysmon\n");
315
316 return 0;
317 }
318
319