kb3310.c revision 1.1 1 1.1 bouyer /* $OpenBSD: kb3310.c,v 1.16 2010/10/14 21:23:04 pirofti Exp $ */
2 1.1 bouyer /*
3 1.1 bouyer * Copyright (c) 2010 Otto Moerbeek <otto (at) drijf.net>
4 1.1 bouyer *
5 1.1 bouyer * Permission to use, copy, modify, and distribute this software for any
6 1.1 bouyer * purpose with or without fee is hereby granted, provided that the above
7 1.1 bouyer * copyright notice and this permission notice appear in all copies.
8 1.1 bouyer *
9 1.1 bouyer * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 1.1 bouyer * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 1.1 bouyer * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 1.1 bouyer * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 1.1 bouyer * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 1.1 bouyer * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 1.1 bouyer * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 1.1 bouyer */
17 1.1 bouyer
18 1.1 bouyer #include <sys/param.h>
19 1.1 bouyer #include <sys/kernel.h>
20 1.1 bouyer #include <sys/systm.h>
21 1.1 bouyer #include <sys/device.h>
22 1.1 bouyer #include <sys/sensors.h>
23 1.1 bouyer #include <sys/timeout.h>
24 1.1 bouyer
25 1.1 bouyer #include <mips64/archtype.h>
26 1.1 bouyer #include <machine/apmvar.h>
27 1.1 bouyer #include <evbmips/loongson/autoconf.h>
28 1.1 bouyer #include <machine/bus.h>
29 1.1 bouyer #include <dev/isa/isavar.h>
30 1.1 bouyer
31 1.1 bouyer #include <dev/pci/glxreg.h>
32 1.1 bouyer
33 1.1 bouyer #include <loongson/dev/bonitoreg.h>
34 1.1 bouyer #include <loongson/dev/kb3310var.h>
35 1.1 bouyer
36 1.1 bouyer #include "apm.h"
37 1.1 bouyer #include "pckbd.h"
38 1.1 bouyer #include "hidkbd.h"
39 1.1 bouyer
40 1.1 bouyer #if NPCKBD > 0 || NHIDKBD > 0
41 1.1 bouyer #include <dev/ic/pckbcvar.h>
42 1.1 bouyer #include <dev/pckbc/pckbdvar.h>
43 1.1 bouyer #include <dev/usb/hidkbdvar.h>
44 1.1 bouyer #endif
45 1.1 bouyer
46 1.1 bouyer struct cfdriver ykbec_cd = {
47 1.1 bouyer NULL, "ykbec", DV_DULL,
48 1.1 bouyer };
49 1.1 bouyer
50 1.1 bouyer #ifdef KB3310_DEBUG
51 1.1 bouyer #define DPRINTF(x) printf x
52 1.1 bouyer #else
53 1.1 bouyer #define DPRINTF(x)
54 1.1 bouyer #endif
55 1.1 bouyer
56 1.1 bouyer #define IO_YKBEC 0x381
57 1.1 bouyer #define IO_YKBECSIZE 0x3
58 1.1 bouyer
59 1.1 bouyer static const struct {
60 1.1 bouyer const char *desc;
61 1.1 bouyer int type;
62 1.1 bouyer } ykbec_table[] = {
63 1.1 bouyer #define YKBEC_FAN 0
64 1.1 bouyer { NULL, SENSOR_FANRPM },
65 1.1 bouyer #define YKBEC_ITEMP 1
66 1.1 bouyer { "Internal temperature", SENSOR_TEMP },
67 1.1 bouyer #define YKBEC_FCAP 2
68 1.1 bouyer { "Battery full charge capacity", SENSOR_AMPHOUR },
69 1.1 bouyer #define YKBEC_BCURRENT 3
70 1.1 bouyer { "Battery current", SENSOR_AMPS },
71 1.1 bouyer #define YKBEC_BVOLT 4
72 1.1 bouyer { "Battery voltage", SENSOR_VOLTS_DC },
73 1.1 bouyer #define YKBEC_BTEMP 5
74 1.1 bouyer { "Battery temperature", SENSOR_TEMP },
75 1.1 bouyer #define YKBEC_CAP 6
76 1.1 bouyer { "Battery capacity", SENSOR_PERCENT },
77 1.1 bouyer #define YKBEC_CHARGING 7
78 1.1 bouyer { "Battery charging", SENSOR_INDICATOR },
79 1.1 bouyer #define YKBEC_AC 8
80 1.1 bouyer { "AC-Power", SENSOR_INDICATOR }
81 1.1 bouyer #define YKBEC_NSENSORS 9
82 1.1 bouyer };
83 1.1 bouyer
84 1.1 bouyer struct ykbec_softc {
85 1.1 bouyer struct device sc_dev;
86 1.1 bouyer bus_space_tag_t sc_iot;
87 1.1 bouyer bus_space_handle_t sc_ioh;
88 1.1 bouyer struct ksensor sc_sensor[YKBEC_NSENSORS];
89 1.1 bouyer struct ksensordev sc_sensordev;
90 1.1 bouyer #if NPCKBD > 0 || NHIDKBD > 0
91 1.1 bouyer struct timeout sc_bell_tmo;
92 1.1 bouyer #endif
93 1.1 bouyer };
94 1.1 bouyer
95 1.1 bouyer static struct ykbec_softc *ykbec_sc;
96 1.1 bouyer static int ykbec_chip_config;
97 1.1 bouyer
98 1.1 bouyer extern void loongson_set_isa_imr(uint);
99 1.1 bouyer
100 1.1 bouyer int ykbec_match(struct device *, void *, void *);
101 1.1 bouyer void ykbec_attach(struct device *, struct device *, void *);
102 1.1 bouyer
103 1.1 bouyer const struct cfattach ykbec_ca = {
104 1.1 bouyer sizeof(struct ykbec_softc), ykbec_match, ykbec_attach
105 1.1 bouyer };
106 1.1 bouyer
107 1.1 bouyer int ykbec_apminfo(struct apm_power_info *);
108 1.1 bouyer void ykbec_bell(void *, u_int, u_int, u_int, int);
109 1.1 bouyer void ykbec_bell_stop(void *);
110 1.1 bouyer void ykbec_print_bat_info(struct ykbec_softc *);
111 1.1 bouyer u_int ykbec_read(struct ykbec_softc *, u_int);
112 1.1 bouyer u_int ykbec_read16(struct ykbec_softc *, u_int);
113 1.1 bouyer void ykbec_refresh(void *arg);
114 1.1 bouyer void ykbec_write(struct ykbec_softc *, u_int, u_int);
115 1.1 bouyer
116 1.1 bouyer #if NAPM > 0
117 1.1 bouyer struct apm_power_info ykbec_apmdata;
118 1.1 bouyer const char *ykbec_batstate[] = {
119 1.1 bouyer "high",
120 1.1 bouyer "low",
121 1.1 bouyer "critical",
122 1.1 bouyer "charging",
123 1.1 bouyer "unknown"
124 1.1 bouyer };
125 1.1 bouyer #define BATTERY_STRING(x) ((x) < nitems(ykbec_batstate) ? \
126 1.1 bouyer ykbec_batstate[x] : ykbec_batstate[4])
127 1.1 bouyer #endif
128 1.1 bouyer
129 1.1 bouyer int
130 1.1 bouyer ykbec_match(struct device *parent, void *match, void *aux)
131 1.1 bouyer {
132 1.1 bouyer struct isa_attach_args *ia = aux;
133 1.1 bouyer bus_space_handle_t ioh;
134 1.1 bouyer
135 1.1 bouyer if (sys_platform->system_type != LOONGSON_YEELOONG)
136 1.1 bouyer return (0);
137 1.1 bouyer
138 1.1 bouyer if ((ia->ia_iobase != IOBASEUNK && ia->ia_iobase != IO_YKBEC) ||
139 1.1 bouyer /* (ia->ia_iosize != 0 && ia->ia_iosize != IO_YKBECSIZE) || XXX isa.c */
140 1.1 bouyer ia->ia_maddr != MADDRUNK || ia->ia_msize != 0 ||
141 1.1 bouyer ia->ia_irq != IRQUNK || ia->ia_drq != DRQUNK)
142 1.1 bouyer return (0);
143 1.1 bouyer
144 1.1 bouyer if (bus_space_map(ia->ia_iot, IO_YKBEC, IO_YKBECSIZE, 0, &ioh))
145 1.1 bouyer return (0);
146 1.1 bouyer
147 1.1 bouyer bus_space_unmap(ia->ia_iot, ioh, IO_YKBECSIZE);
148 1.1 bouyer
149 1.1 bouyer ia->ia_iobase = IO_YKBEC;
150 1.1 bouyer ia->ia_iosize = IO_YKBECSIZE;
151 1.1 bouyer
152 1.1 bouyer return (1);
153 1.1 bouyer }
154 1.1 bouyer
155 1.1 bouyer void
156 1.1 bouyer ykbec_attach(struct device *parent, struct device *self, void *aux)
157 1.1 bouyer {
158 1.1 bouyer struct isa_attach_args *ia = aux;
159 1.1 bouyer struct ykbec_softc *sc = (struct ykbec_softc *)self;
160 1.1 bouyer int i;
161 1.1 bouyer
162 1.1 bouyer sc->sc_iot = ia->ia_iot;
163 1.1 bouyer if (bus_space_map(sc->sc_iot, ia->ia_iobase, ia->ia_iosize, 0,
164 1.1 bouyer &sc->sc_ioh)) {
165 1.1 bouyer aprint_error(": couldn't map I/O space");
166 1.1 bouyer return;
167 1.1 bouyer }
168 1.1 bouyer
169 1.1 bouyer /* Initialize sensor data. */
170 1.1 bouyer strlcpy(sc->sc_sensordev.xname, sc->sc_dev.dv_xname,
171 1.1 bouyer sizeof(sc->sc_sensordev.xname));
172 1.1 bouyer if (sensor_task_register(sc, ykbec_refresh, 5) == NULL) {
173 1.1 bouyer aprint_error(", unable to register update task\n");
174 1.1 bouyer return;
175 1.1 bouyer }
176 1.1 bouyer
177 1.1 bouyer #ifdef DEBUG
178 1.1 bouyer ykbec_print_bat_info(sc);
179 1.1 bouyer #endif
180 1.1 bouyer aprint_normal("\n");
181 1.1 bouyer
182 1.1 bouyer for (i = 0; i < YKBEC_NSENSORS; i++) {
183 1.1 bouyer sc->sc_sensor[i].type = ykbec_table[i].type;
184 1.1 bouyer if (ykbec_table[i].desc)
185 1.1 bouyer strlcpy(sc->sc_sensor[i].desc, ykbec_table[i].desc,
186 1.1 bouyer sizeof(sc->sc_sensor[i].desc));
187 1.1 bouyer sensor_attach(&sc->sc_sensordev, &sc->sc_sensor[i]);
188 1.1 bouyer }
189 1.1 bouyer
190 1.1 bouyer sensordev_install(&sc->sc_sensordev);
191 1.1 bouyer
192 1.1 bouyer #if NAPM > 0
193 1.1 bouyer /* make sure we have the apm state initialized before apm attaches */
194 1.1 bouyer ykbec_refresh(sc);
195 1.1 bouyer apm_setinfohook(ykbec_apminfo);
196 1.1 bouyer #endif
197 1.1 bouyer #if NPCKBD > 0 || NHIDKBD > 0
198 1.1 bouyer timeout_set(&sc->sc_bell_tmo, ykbec_bell_stop, sc);
199 1.1 bouyer #if NPCKBD > 0
200 1.1 bouyer pckbd_hookup_bell(ykbec_bell, sc);
201 1.1 bouyer #endif
202 1.1 bouyer #if NHIDKBD > 0
203 1.1 bouyer hidkbd_hookup_bell(ykbec_bell, sc);
204 1.1 bouyer #endif
205 1.1 bouyer #endif
206 1.1 bouyer ykbec_sc = sc;
207 1.1 bouyer }
208 1.1 bouyer
209 1.1 bouyer void
210 1.1 bouyer ykbec_write(struct ykbec_softc *mcsc, u_int reg, u_int datum)
211 1.1 bouyer {
212 1.1 bouyer struct ykbec_softc *sc = (struct ykbec_softc *)mcsc;
213 1.1 bouyer bus_space_tag_t iot = sc->sc_iot;
214 1.1 bouyer bus_space_handle_t ioh = sc->sc_ioh;
215 1.1 bouyer
216 1.1 bouyer bus_space_write_1(iot, ioh, 0, (reg >> 8) & 0xff);
217 1.1 bouyer bus_space_write_1(iot, ioh, 1, (reg >> 0) & 0xff);
218 1.1 bouyer bus_space_write_1(iot, ioh, 2, datum);
219 1.1 bouyer }
220 1.1 bouyer
221 1.1 bouyer u_int
222 1.1 bouyer ykbec_read(struct ykbec_softc *mcsc, u_int reg)
223 1.1 bouyer {
224 1.1 bouyer struct ykbec_softc *sc = (struct ykbec_softc *)mcsc;
225 1.1 bouyer bus_space_tag_t iot = sc->sc_iot;
226 1.1 bouyer bus_space_handle_t ioh = sc->sc_ioh;
227 1.1 bouyer
228 1.1 bouyer bus_space_write_1(iot, ioh, 0, (reg >> 8) & 0xff);
229 1.1 bouyer bus_space_write_1(iot, ioh, 1, (reg >> 0) & 0xff);
230 1.1 bouyer return bus_space_read_1(iot, ioh, 2);
231 1.1 bouyer }
232 1.1 bouyer
233 1.1 bouyer u_int
234 1.1 bouyer ykbec_read16(struct ykbec_softc *mcsc, u_int reg)
235 1.1 bouyer {
236 1.1 bouyer u_int val;
237 1.1 bouyer
238 1.1 bouyer val = ykbec_read(mcsc, reg);
239 1.1 bouyer return (val << 8) | ykbec_read(mcsc, reg + 1);
240 1.1 bouyer }
241 1.1 bouyer
242 1.1 bouyer #define KB3310_FAN_SPEED_DIVIDER 480000
243 1.1 bouyer
244 1.1 bouyer #define ECTEMP_CURRENT_REG 0xf458
245 1.1 bouyer #define REG_FAN_SPEED_HIGH 0xfe22
246 1.1 bouyer #define REG_FAN_SPEED_LOW 0xfe23
247 1.1 bouyer
248 1.1 bouyer #define REG_DESIGN_CAP_HIGH 0xf77d
249 1.1 bouyer #define REG_DESIGN_CAP_LOW 0xf77e
250 1.1 bouyer #define REG_FULLCHG_CAP_HIGH 0xf780
251 1.1 bouyer #define REG_FULLCHG_CAP_LOW 0xf781
252 1.1 bouyer
253 1.1 bouyer #define REG_DESIGN_VOL_HIGH 0xf782
254 1.1 bouyer #define REG_DESIGN_VOL_LOW 0xf783
255 1.1 bouyer #define REG_CURRENT_HIGH 0xf784
256 1.1 bouyer #define REG_CURRENT_LOW 0xf785
257 1.1 bouyer #define REG_VOLTAGE_HIGH 0xf786
258 1.1 bouyer #define REG_VOLTAGE_LOW 0xf787
259 1.1 bouyer #define REG_TEMPERATURE_HIGH 0xf788
260 1.1 bouyer #define REG_TEMPERATURE_LOW 0xf789
261 1.1 bouyer #define REG_RELATIVE_CAT_HIGH 0xf492
262 1.1 bouyer #define REG_RELATIVE_CAT_LOW 0xf493
263 1.1 bouyer #define REG_BAT_VENDOR 0xf4c4
264 1.1 bouyer #define REG_BAT_CELL_COUNT 0xf4c6
265 1.1 bouyer
266 1.1 bouyer #define REG_BAT_CHARGE 0xf4a2
267 1.1 bouyer #define BAT_CHARGE_AC 0x00
268 1.1 bouyer #define BAT_CHARGE_DISCHARGE 0x01
269 1.1 bouyer #define BAT_CHARGE_CHARGE 0x02
270 1.1 bouyer
271 1.1 bouyer #define REG_POWER_FLAG 0xf440
272 1.1 bouyer #define POWER_FLAG_ADAPTER_IN (1<<0)
273 1.1 bouyer #define POWER_FLAG_POWER_ON (1<<1)
274 1.1 bouyer #define POWER_FLAG_ENTER_SUS (1<<2)
275 1.1 bouyer
276 1.1 bouyer #define REG_BAT_STATUS 0xf4b0
277 1.1 bouyer #define BAT_STATUS_BAT_EXISTS (1<<0)
278 1.1 bouyer #define BAT_STATUS_BAT_FULL (1<<1)
279 1.1 bouyer #define BAT_STATUS_BAT_DESTROY (1<<2)
280 1.1 bouyer #define BAT_STATUS_BAT_LOW (1<<5)
281 1.1 bouyer
282 1.1 bouyer #define REG_CHARGE_STATUS 0xf4b1
283 1.1 bouyer #define CHARGE_STATUS_PRECHARGE (1<<1)
284 1.1 bouyer #define CHARGE_STATUS_OVERHEAT (1<<2)
285 1.1 bouyer
286 1.1 bouyer #define REG_BAT_STATE 0xf482
287 1.1 bouyer #define BAT_STATE_DISCHARGING (1<<0)
288 1.1 bouyer #define BAT_STATE_CHARGING (1<<1)
289 1.1 bouyer
290 1.1 bouyer #define REG_BEEP_CONTROL 0xf4d0
291 1.1 bouyer #define BEEP_ENABLE (1<<0)
292 1.1 bouyer
293 1.1 bouyer #define REG_PMUCFG 0xff0c
294 1.1 bouyer #define PMUCFG_STOP_MODE (1<<7)
295 1.1 bouyer #define PMUCFG_IDLE_MODE (1<<6)
296 1.1 bouyer #define PMUCFG_LPC_WAKEUP (1<<5)
297 1.1 bouyer #define PMUCFG_RESET_8051 (1<<4)
298 1.1 bouyer #define PMUCFG_SCI_WAKEUP (1<<3)
299 1.1 bouyer #define PMUCFG_WDT_WAKEUP (1<<2)
300 1.1 bouyer #define PMUCFG_GPWU_WAKEUP (1<<1)
301 1.1 bouyer #define PMUCFG_IRQ_IDLE (1<<0)
302 1.1 bouyer
303 1.1 bouyer #define REG_USB0 0xf461
304 1.1 bouyer #define REG_USB1 0xf462
305 1.1 bouyer #define REG_USB2 0xf463
306 1.1 bouyer #define USB_FLAG_ON 1
307 1.1 bouyer #define USB_FLAG_OFF 0
308 1.1 bouyer
309 1.1 bouyer #define REG_FAN_CONTROL 0xf4d2
310 1.1 bouyer #define REG_FAN_ON 1
311 1.1 bouyer #define REG_FAN_OFF 0
312 1.1 bouyer
313 1.1 bouyer #define YKBEC_SCI_IRQ 0xa
314 1.1 bouyer
315 1.1 bouyer #ifdef DEBUG
316 1.1 bouyer void
317 1.1 bouyer ykbec_print_bat_info(struct ykbec_softc *sc)
318 1.1 bouyer {
319 1.1 bouyer uint bat_status, count, dvolt, dcap;
320 1.1 bouyer
321 1.1 bouyer printf(": battery ");
322 1.1 bouyer bat_status = ykbec_read(sc, REG_BAT_STATUS);
323 1.1 bouyer if (!ISSET(bat_status, BAT_STATUS_BAT_EXISTS)) {
324 1.1 bouyer printf("absent");
325 1.1 bouyer return;
326 1.1 bouyer }
327 1.1 bouyer
328 1.1 bouyer count = ykbec_read(sc, REG_BAT_CELL_COUNT);
329 1.1 bouyer dvolt = ykbec_read16(sc, REG_DESIGN_VOL_HIGH);
330 1.1 bouyer dcap = ykbec_read16(sc, REG_DESIGN_CAP_HIGH);
331 1.1 bouyer printf("%d cells, design capacity %dmV %dmAh", count, dvolt, dcap);
332 1.1 bouyer }
333 1.1 bouyer #endif
334 1.1 bouyer
335 1.1 bouyer void
336 1.1 bouyer ykbec_refresh(void *arg)
337 1.1 bouyer {
338 1.1 bouyer struct ykbec_softc *sc = (struct ykbec_softc *)arg;
339 1.1 bouyer u_int val, bat_charge, bat_status, charge_status, bat_state, power_flag;
340 1.1 bouyer u_int cap_pct, fullcap;
341 1.1 bouyer int current;
342 1.1 bouyer #if NAPM > 0
343 1.1 bouyer struct apm_power_info old;
344 1.1 bouyer #endif
345 1.1 bouyer
346 1.1 bouyer val = ykbec_read16(sc, REG_FAN_SPEED_HIGH) & 0xfffff;
347 1.1 bouyer if (val != 0) {
348 1.1 bouyer val = KB3310_FAN_SPEED_DIVIDER / val;
349 1.1 bouyer sc->sc_sensor[YKBEC_FAN].value = val;
350 1.1 bouyer CLR(sc->sc_sensor[YKBEC_FAN].flags, SENSOR_FINVALID);
351 1.1 bouyer } else
352 1.1 bouyer SET(sc->sc_sensor[YKBEC_FAN].flags, SENSOR_FINVALID);
353 1.1 bouyer
354 1.1 bouyer val = ykbec_read(sc, ECTEMP_CURRENT_REG);
355 1.1 bouyer sc->sc_sensor[YKBEC_ITEMP].value = val * 1000000 + 273150000;
356 1.1 bouyer
357 1.1 bouyer fullcap = ykbec_read16(sc, REG_FULLCHG_CAP_HIGH);
358 1.1 bouyer sc->sc_sensor[YKBEC_FCAP].value = fullcap * 1000;
359 1.1 bouyer
360 1.1 bouyer current = ykbec_read16(sc, REG_CURRENT_HIGH);
361 1.1 bouyer /* sign extend short -> int, int -> int64 will be done next statement */
362 1.1 bouyer current |= -(current & 0x8000);
363 1.1 bouyer sc->sc_sensor[YKBEC_BCURRENT].value = -1000 * current;
364 1.1 bouyer
365 1.1 bouyer sc->sc_sensor[YKBEC_BVOLT].value = ykbec_read16(sc, REG_VOLTAGE_HIGH) *
366 1.1 bouyer 1000;
367 1.1 bouyer
368 1.1 bouyer val = ykbec_read16(sc, REG_TEMPERATURE_HIGH);
369 1.1 bouyer sc->sc_sensor[YKBEC_BTEMP].value = val * 1000000 + 273150000;
370 1.1 bouyer
371 1.1 bouyer cap_pct = ykbec_read16(sc, REG_RELATIVE_CAT_HIGH);
372 1.1 bouyer sc->sc_sensor[YKBEC_CAP].value = cap_pct * 1000;
373 1.1 bouyer
374 1.1 bouyer bat_charge = ykbec_read(sc, REG_BAT_CHARGE);
375 1.1 bouyer bat_status = ykbec_read(sc, REG_BAT_STATUS);
376 1.1 bouyer charge_status = ykbec_read(sc, REG_CHARGE_STATUS);
377 1.1 bouyer bat_state = ykbec_read(sc, REG_BAT_STATE);
378 1.1 bouyer power_flag = ykbec_read(sc, REG_POWER_FLAG);
379 1.1 bouyer
380 1.1 bouyer sc->sc_sensor[YKBEC_CHARGING].value = !!ISSET(bat_state,
381 1.1 bouyer BAT_STATE_CHARGING);
382 1.1 bouyer sc->sc_sensor[YKBEC_AC].value = !!ISSET(power_flag,
383 1.1 bouyer POWER_FLAG_ADAPTER_IN);
384 1.1 bouyer
385 1.1 bouyer sc->sc_sensor[YKBEC_CAP].status = ISSET(bat_status, BAT_STATUS_BAT_LOW) ?
386 1.1 bouyer SENSOR_S_CRIT : SENSOR_S_OK;
387 1.1 bouyer
388 1.1 bouyer #if NAPM > 0
389 1.1 bouyer bcopy(&ykbec_apmdata, &old, sizeof(old));
390 1.1 bouyer ykbec_apmdata.battery_life = cap_pct;
391 1.1 bouyer ykbec_apmdata.ac_state = ISSET(power_flag, POWER_FLAG_ADAPTER_IN) ?
392 1.1 bouyer APM_AC_ON : APM_AC_OFF;
393 1.1 bouyer if (!ISSET(bat_status, BAT_STATUS_BAT_EXISTS)) {
394 1.1 bouyer ykbec_apmdata.battery_state = APM_BATTERY_ABSENT;
395 1.1 bouyer ykbec_apmdata.minutes_left = 0;
396 1.1 bouyer ykbec_apmdata.battery_life = 0;
397 1.1 bouyer } else {
398 1.1 bouyer if (ISSET(bat_state, BAT_STATE_CHARGING))
399 1.1 bouyer ykbec_apmdata.battery_state = APM_BATT_CHARGING;
400 1.1 bouyer else if (ISSET(bat_status, BAT_STATUS_BAT_LOW))
401 1.1 bouyer ykbec_apmdata.battery_state = APM_BATT_CRITICAL;
402 1.1 bouyer /* XXX arbitrary */
403 1.1 bouyer else if (cap_pct > 60)
404 1.1 bouyer ykbec_apmdata.battery_state = APM_BATT_HIGH;
405 1.1 bouyer else
406 1.1 bouyer ykbec_apmdata.battery_state = APM_BATT_LOW;
407 1.1 bouyer
408 1.1 bouyer /* if charging, current is positive */
409 1.1 bouyer if (ISSET(bat_state, BAT_STATE_CHARGING))
410 1.1 bouyer current = 0;
411 1.1 bouyer else
412 1.1 bouyer current = -current;
413 1.1 bouyer /* XXX Yeeloong draw is about 1A */
414 1.1 bouyer if (current <= 0)
415 1.1 bouyer current = 1000;
416 1.1 bouyer /* XXX at 5?%, the Yeeloong shuts down */
417 1.1 bouyer if (cap_pct <= 5)
418 1.1 bouyer cap_pct = 0;
419 1.1 bouyer else
420 1.1 bouyer cap_pct -= 5;
421 1.1 bouyer fullcap = cap_pct * 60 * fullcap / 100;
422 1.1 bouyer ykbec_apmdata.minutes_left = fullcap / current;
423 1.1 bouyer
424 1.1 bouyer }
425 1.1 bouyer if (old.ac_state != ykbec_apmdata.ac_state)
426 1.1 bouyer apm_record_event(APM_POWER_CHANGE, "AC power",
427 1.1 bouyer ykbec_apmdata.ac_state ? "restored" : "lost");
428 1.1 bouyer if (old.battery_state != ykbec_apmdata.battery_state)
429 1.1 bouyer apm_record_event(APM_POWER_CHANGE, "battery",
430 1.1 bouyer BATTERY_STRING(ykbec_apmdata.battery_state));
431 1.1 bouyer #endif
432 1.1 bouyer }
433 1.1 bouyer
434 1.1 bouyer
435 1.1 bouyer #if NAPM > 0
436 1.1 bouyer int
437 1.1 bouyer ykbec_apminfo(struct apm_power_info *info)
438 1.1 bouyer {
439 1.1 bouyer bcopy(&ykbec_apmdata, info, sizeof(struct apm_power_info));
440 1.1 bouyer return 0;
441 1.1 bouyer }
442 1.1 bouyer
443 1.1 bouyer int
444 1.1 bouyer ykbec_suspend()
445 1.1 bouyer {
446 1.1 bouyer struct ykbec_softc *sc = ykbec_sc;
447 1.1 bouyer int ctrl;
448 1.1 bouyer
449 1.1 bouyer /*
450 1.1 bouyer * Set up wakeup sources: currently only the internal keyboard.
451 1.1 bouyer */
452 1.1 bouyer loongson_set_isa_imr(1 << 1);
453 1.1 bouyer
454 1.1 bouyer /* USB */
455 1.1 bouyer DPRINTF(("USB\n"));
456 1.1 bouyer ykbec_write(sc, REG_USB0, USB_FLAG_OFF);
457 1.1 bouyer ykbec_write(sc, REG_USB1, USB_FLAG_OFF);
458 1.1 bouyer ykbec_write(sc, REG_USB2, USB_FLAG_OFF);
459 1.1 bouyer
460 1.1 bouyer /* EC */
461 1.1 bouyer DPRINTF(("REG_PMUCFG\n"));
462 1.1 bouyer ctrl = PMUCFG_SCI_WAKEUP | PMUCFG_WDT_WAKEUP | PMUCFG_GPWU_WAKEUP |
463 1.1 bouyer PMUCFG_LPC_WAKEUP | PMUCFG_STOP_MODE | PMUCFG_RESET_8051;
464 1.1 bouyer ykbec_write(sc, REG_PMUCFG, ctrl);
465 1.1 bouyer
466 1.1 bouyer /* FAN */
467 1.1 bouyer DPRINTF(("FAN\n"));
468 1.1 bouyer ykbec_write(sc, REG_FAN_CONTROL, REG_FAN_OFF);
469 1.1 bouyer
470 1.1 bouyer /* CPU */
471 1.1 bouyer DPRINTF(("CPU\n"));
472 1.1 bouyer ykbec_chip_config = REGVAL(LOONGSON_CHIP_CONFIG0);
473 1.1 bouyer enableintr();
474 1.1 bouyer REGVAL(LOONGSON_CHIP_CONFIG0) = ykbec_chip_config & ~0x7;
475 1.1 bouyer (void)REGVAL(LOONGSON_CHIP_CONFIG0);
476 1.1 bouyer
477 1.1 bouyer /*
478 1.1 bouyer * When a resume interrupt fires, we will enter the interrupt
479 1.1 bouyer * dispatcher, which will do nothing because we are at splhigh,
480 1.1 bouyer * and execution flow will return here and continue.
481 1.1 bouyer */
482 1.1 bouyer (void)disableintr();
483 1.1 bouyer
484 1.1 bouyer return 0;
485 1.1 bouyer }
486 1.1 bouyer
487 1.1 bouyer int
488 1.1 bouyer ykbec_resume()
489 1.1 bouyer {
490 1.1 bouyer struct ykbec_softc *sc = ykbec_sc;
491 1.1 bouyer
492 1.1 bouyer /* CPU */
493 1.1 bouyer DPRINTF(("CPU\n"));
494 1.1 bouyer REGVAL(LOONGSON_CHIP_CONFIG0) = ykbec_chip_config;
495 1.1 bouyer (void)REGVAL(LOONGSON_CHIP_CONFIG0);
496 1.1 bouyer
497 1.1 bouyer /* FAN */
498 1.1 bouyer DPRINTF(("FAN\n"));
499 1.1 bouyer ykbec_write(sc, REG_FAN_CONTROL, REG_FAN_ON);
500 1.1 bouyer
501 1.1 bouyer /* USB */
502 1.1 bouyer DPRINTF(("USB\n"));
503 1.1 bouyer ykbec_write(sc, REG_USB0, USB_FLAG_ON);
504 1.1 bouyer ykbec_write(sc, REG_USB1, USB_FLAG_ON);
505 1.1 bouyer ykbec_write(sc, REG_USB2, USB_FLAG_ON);
506 1.1 bouyer
507 1.1 bouyer ykbec_refresh(sc);
508 1.1 bouyer
509 1.1 bouyer return 0;
510 1.1 bouyer }
511 1.1 bouyer #endif
512 1.1 bouyer
513 1.1 bouyer #if NPCKBD > 0 || NHIDKBD > 0
514 1.1 bouyer void
515 1.1 bouyer ykbec_bell(void *arg, u_int pitch, u_int period, u_int volume, int poll)
516 1.1 bouyer {
517 1.1 bouyer struct ykbec_softc *sc = (struct ykbec_softc *)arg;
518 1.1 bouyer int bctrl;
519 1.1 bouyer int s;
520 1.1 bouyer
521 1.1 bouyer s = spltty();
522 1.1 bouyer bctrl = ykbec_read(sc, REG_BEEP_CONTROL);
523 1.1 bouyer if (volume == 0 || timeout_pending(&sc->sc_bell_tmo)) {
524 1.1 bouyer timeout_del(&sc->sc_bell_tmo);
525 1.1 bouyer /* inline ykbec_bell_stop(arg); */
526 1.1 bouyer ykbec_write(sc, REG_BEEP_CONTROL, bctrl & ~BEEP_ENABLE);
527 1.1 bouyer }
528 1.1 bouyer
529 1.1 bouyer if (volume != 0) {
530 1.1 bouyer ykbec_write(sc, REG_BEEP_CONTROL, bctrl | BEEP_ENABLE);
531 1.1 bouyer if (poll) {
532 1.1 bouyer delay(period * 1000);
533 1.1 bouyer ykbec_write(sc, REG_BEEP_CONTROL, bctrl & ~BEEP_ENABLE);
534 1.1 bouyer } else {
535 1.1 bouyer timeout_add_msec(&sc->sc_bell_tmo, period);
536 1.1 bouyer }
537 1.1 bouyer }
538 1.1 bouyer splx(s);
539 1.1 bouyer }
540 1.1 bouyer
541 1.1 bouyer void
542 1.1 bouyer ykbec_bell_stop(void *arg)
543 1.1 bouyer {
544 1.1 bouyer struct ykbec_softc *sc = (struct ykbec_softc *)arg;
545 1.1 bouyer int s;
546 1.1 bouyer
547 1.1 bouyer s = spltty();
548 1.1 bouyer ykbec_write(sc, REG_BEEP_CONTROL,
549 1.1 bouyer ykbec_read(sc, REG_BEEP_CONTROL) & ~BEEP_ENABLE);
550 1.1 bouyer splx(s);
551 1.1 bouyer }
552 1.1 bouyer #endif
553