xref: /openbsd/sys/arch/loongson/dev/kb3310.c (revision b4f91ba2)
1*b4f91ba2Smiod /*	$OpenBSD: kb3310.c,v 1.24 2024/01/21 07:17:06 miod Exp $	*/
2e1f5be9aSotto /*
3e1f5be9aSotto  * Copyright (c) 2010 Otto Moerbeek <otto@drijf.net>
4e1f5be9aSotto  *
5e1f5be9aSotto  * Permission to use, copy, modify, and distribute this software for any
6e1f5be9aSotto  * purpose with or without fee is hereby granted, provided that the above
7e1f5be9aSotto  * copyright notice and this permission notice appear in all copies.
8e1f5be9aSotto  *
9e1f5be9aSotto  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10e1f5be9aSotto  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11e1f5be9aSotto  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12e1f5be9aSotto  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13e1f5be9aSotto  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14e1f5be9aSotto  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15e1f5be9aSotto  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16e1f5be9aSotto  */
17e1f5be9aSotto 
18e1f5be9aSotto #include <sys/param.h>
19e1f5be9aSotto #include <sys/kernel.h>
20e1f5be9aSotto #include <sys/systm.h>
21e1f5be9aSotto #include <sys/device.h>
22e1f5be9aSotto #include <sys/sensors.h>
2399b7edb0Smiod #include <sys/timeout.h>
24e1f5be9aSotto 
2513bfa4beSotto #include <machine/apmvar.h>
26d57a735dSmiod #include <machine/autoconf.h>
27e1f5be9aSotto #include <machine/bus.h>
28e1f5be9aSotto #include <dev/isa/isavar.h>
29e1f5be9aSotto 
302581a76dSpirofti #include <dev/pci/glxreg.h>
312581a76dSpirofti 
32bec266e2Spirofti #include <loongson/dev/bonitoreg.h>
33bec266e2Spirofti #include <loongson/dev/kb3310var.h>
34bec266e2Spirofti 
3513bfa4beSotto #include "apm.h"
3699b7edb0Smiod #include "pckbd.h"
37f77164e9Smiod #include "hidkbd.h"
3899b7edb0Smiod 
39f77164e9Smiod #if NPCKBD > 0 || NHIDKBD > 0
4099b7edb0Smiod #include <dev/ic/pckbcvar.h>
4199b7edb0Smiod #include <dev/pckbc/pckbdvar.h>
428a83145eSjcs #include <dev/hid/hidkbdvar.h>
4399b7edb0Smiod #endif
4413bfa4beSotto 
45e1f5be9aSotto struct cfdriver ykbec_cd = {
46e1f5be9aSotto 	NULL, "ykbec", DV_DULL,
47e1f5be9aSotto };
48e1f5be9aSotto 
49bec266e2Spirofti #ifdef KB3310_DEBUG
50bec266e2Spirofti #define DPRINTF(x)	printf x
51bec266e2Spirofti #else
52bec266e2Spirofti #define DPRINTF(x)
53bec266e2Spirofti #endif
54bec266e2Spirofti 
55e1f5be9aSotto #define IO_YKBEC		0x381
56e1f5be9aSotto #define IO_YKBECSIZE		0x3
57e1f5be9aSotto 
5813bfa4beSotto static const struct {
5913bfa4beSotto 	const char *desc;
6013bfa4beSotto 	int type;
6113bfa4beSotto } ykbec_table[] = {
6213bfa4beSotto #define YKBEC_FAN	0
6313bfa4beSotto 	{ NULL,				SENSOR_FANRPM },
6413bfa4beSotto #define YKBEC_ITEMP	1
6513bfa4beSotto 	{ "Internal temperature",	SENSOR_TEMP },
66c4b88ac2Sotto #define YKBEC_FCAP	2
6713bfa4beSotto 	{ "Battery full charge capacity", SENSOR_AMPHOUR },
68c4b88ac2Sotto #define YKBEC_BCURRENT	3
6913bfa4beSotto 	{ "Battery current",		SENSOR_AMPS },
70c4b88ac2Sotto #define YKBEC_BVOLT	4
7113bfa4beSotto 	{ "Battery voltage",		SENSOR_VOLTS_DC },
72c4b88ac2Sotto #define YKBEC_BTEMP	5
7313bfa4beSotto 	{ "Battery temperature",	SENSOR_TEMP },
74c4b88ac2Sotto #define YKBEC_CAP	6
7513bfa4beSotto 	{ "Battery capacity",		SENSOR_PERCENT },
76c4b88ac2Sotto #define YKBEC_CHARGING	7
7713bfa4beSotto 	{ "Battery charging",		SENSOR_INDICATOR },
78c4b88ac2Sotto #define YKBEC_AC	8
79f60e0988Sfcambus 	{ "AC-Power",			SENSOR_INDICATOR },
80f60e0988Sfcambus #define YKBEC_LID	9
81f60e0988Sfcambus 	{ "Lid open",			SENSOR_INDICATOR }
82f60e0988Sfcambus #define YKBEC_NSENSORS	10
8313bfa4beSotto };
84e1f5be9aSotto 
85e1f5be9aSotto struct ykbec_softc {
86e1f5be9aSotto 	struct device		sc_dev;
87e1f5be9aSotto 	bus_space_tag_t		sc_iot;
88e1f5be9aSotto 	bus_space_handle_t	sc_ioh;
8913bfa4beSotto 	struct ksensor		sc_sensor[YKBEC_NSENSORS];
90e1f5be9aSotto 	struct ksensordev	sc_sensordev;
91f77164e9Smiod #if NPCKBD > 0 || NHIDKBD > 0
9299b7edb0Smiod 	struct timeout		sc_bell_tmo;
9399b7edb0Smiod #endif
94e1f5be9aSotto };
95e1f5be9aSotto 
96bec266e2Spirofti static struct ykbec_softc *ykbec_sc;
97bec266e2Spirofti static int ykbec_chip_config;
98bec266e2Spirofti 
99bec266e2Spirofti extern void loongson_set_isa_imr(uint);
100bec266e2Spirofti 
101e1f5be9aSotto int	ykbec_match(struct device *, void *, void *);
102e1f5be9aSotto void	ykbec_attach(struct device *, struct device *, void *);
103e1f5be9aSotto 
104e1f5be9aSotto const struct cfattach ykbec_ca = {
105e1f5be9aSotto 	sizeof(struct ykbec_softc), ykbec_match, ykbec_attach
106e1f5be9aSotto };
107e1f5be9aSotto 
10899b7edb0Smiod int	ykbec_apminfo(struct apm_power_info *);
10999b7edb0Smiod void	ykbec_bell(void *, u_int, u_int, u_int, int);
11099b7edb0Smiod void	ykbec_bell_stop(void *);
111c4b88ac2Sotto void	ykbec_print_bat_info(struct ykbec_softc *);
112e1f5be9aSotto u_int	ykbec_read(struct ykbec_softc *, u_int);
113e1f5be9aSotto u_int	ykbec_read16(struct ykbec_softc *, u_int);
11499b7edb0Smiod void	ykbec_refresh(void *arg);
11599b7edb0Smiod void	ykbec_write(struct ykbec_softc *, u_int, u_int);
116e1f5be9aSotto 
11713bfa4beSotto #if NAPM > 0
11813bfa4beSotto struct apm_power_info ykbec_apmdata;
11913bfa4beSotto const char *ykbec_batstate[] = {
12013bfa4beSotto 	"high",
12113bfa4beSotto 	"low",
12213bfa4beSotto 	"critical",
12313bfa4beSotto 	"charging",
12413bfa4beSotto 	"unknown"
12513bfa4beSotto };
12613bfa4beSotto #define BATTERY_STRING(x) ((x) < nitems(ykbec_batstate) ? \
12713bfa4beSotto 	ykbec_batstate[x] : ykbec_batstate[4])
12813bfa4beSotto #endif
12913bfa4beSotto 
130e1f5be9aSotto int
ykbec_match(struct device * parent,void * match,void * aux)131e1f5be9aSotto ykbec_match(struct device *parent, void *match, void *aux)
132e1f5be9aSotto {
133e1f5be9aSotto 	struct isa_attach_args *ia = aux;
134e1f5be9aSotto 	bus_space_handle_t ioh;
135e1f5be9aSotto 
136f75298a7Smiod 	/* XXX maybe allow LOONGSON_EBT700 ??? */
137d57a735dSmiod 	if (sys_platform->system_type != LOONGSON_YEELOONG)
138d57a735dSmiod 		return (0);
139d57a735dSmiod 
140e1f5be9aSotto 	if ((ia->ia_iobase != IOBASEUNK && ia->ia_iobase != IO_YKBEC) ||
141e1f5be9aSotto 	    /* (ia->ia_iosize != 0 && ia->ia_iosize != IO_YKBECSIZE) || XXX isa.c */
142e1f5be9aSotto 	    ia->ia_maddr != MADDRUNK || ia->ia_msize != 0 ||
143e1f5be9aSotto 	    ia->ia_irq != IRQUNK || ia->ia_drq != DRQUNK)
144e1f5be9aSotto 		return (0);
145e1f5be9aSotto 
146e1f5be9aSotto 	if (bus_space_map(ia->ia_iot, IO_YKBEC, IO_YKBECSIZE, 0, &ioh))
147e1f5be9aSotto 		return (0);
148e1f5be9aSotto 
149e1f5be9aSotto 	bus_space_unmap(ia->ia_iot, ioh, IO_YKBECSIZE);
150e1f5be9aSotto 
151e1f5be9aSotto 	ia->ia_iobase = IO_YKBEC;
152e1f5be9aSotto 	ia->ia_iosize = IO_YKBECSIZE;
153e1f5be9aSotto 
154e1f5be9aSotto 	return (1);
155e1f5be9aSotto }
156e1f5be9aSotto 
157e1f5be9aSotto void
ykbec_attach(struct device * parent,struct device * self,void * aux)158e1f5be9aSotto ykbec_attach(struct device *parent, struct device *self, void *aux)
159e1f5be9aSotto {
160e1f5be9aSotto 	struct isa_attach_args *ia = aux;
161e1f5be9aSotto 	struct ykbec_softc *sc = (struct ykbec_softc *)self;
16213bfa4beSotto 	int i;
163e1f5be9aSotto 
164e1f5be9aSotto 	sc->sc_iot = ia->ia_iot;
165e1f5be9aSotto 	if (bus_space_map(sc->sc_iot, ia->ia_iobase, ia->ia_iosize, 0,
166e1f5be9aSotto 	    &sc->sc_ioh)) {
167e1f5be9aSotto 		printf(": couldn't map I/O space");
168e1f5be9aSotto 		return;
169e1f5be9aSotto 	}
170e1f5be9aSotto 
171e1f5be9aSotto 	/* Initialize sensor data. */
172e1f5be9aSotto 	strlcpy(sc->sc_sensordev.xname, sc->sc_dev.dv_xname,
173e1f5be9aSotto 	    sizeof(sc->sc_sensordev.xname));
174e1f5be9aSotto 	if (sensor_task_register(sc, ykbec_refresh, 5) == NULL) {
175e1f5be9aSotto 		printf(", unable to register update task\n");
176e1f5be9aSotto 		return;
177e1f5be9aSotto 	}
178e1f5be9aSotto 
179*b4f91ba2Smiod #ifdef KB3310_DEBUG
180c4b88ac2Sotto 	ykbec_print_bat_info(sc);
181c4b88ac2Sotto #endif
18299b7edb0Smiod 	printf("\n");
18399b7edb0Smiod 
18413bfa4beSotto 	for (i = 0; i < YKBEC_NSENSORS; i++) {
18513bfa4beSotto 		sc->sc_sensor[i].type = ykbec_table[i].type;
18613bfa4beSotto 		if (ykbec_table[i].desc)
18713bfa4beSotto 			strlcpy(sc->sc_sensor[i].desc, ykbec_table[i].desc,
18813bfa4beSotto 			    sizeof(sc->sc_sensor[i].desc));
18913bfa4beSotto 		sensor_attach(&sc->sc_sensordev, &sc->sc_sensor[i]);
19013bfa4beSotto 	}
19115562d19Sotto 
192e1f5be9aSotto 	sensordev_install(&sc->sc_sensordev);
193e1f5be9aSotto 
19413bfa4beSotto #if NAPM > 0
19599b7edb0Smiod 	/* make sure we have the apm state initialized before apm attaches */
19699b7edb0Smiod 	ykbec_refresh(sc);
19713bfa4beSotto 	apm_setinfohook(ykbec_apminfo);
19813bfa4beSotto #endif
199f77164e9Smiod #if NPCKBD > 0 || NHIDKBD > 0
20099b7edb0Smiod 	timeout_set(&sc->sc_bell_tmo, ykbec_bell_stop, sc);
20199b7edb0Smiod #if NPCKBD > 0
20299b7edb0Smiod 	pckbd_hookup_bell(ykbec_bell, sc);
20399b7edb0Smiod #endif
204f77164e9Smiod #if NHIDKBD > 0
205f77164e9Smiod 	hidkbd_hookup_bell(ykbec_bell, sc);
20699b7edb0Smiod #endif
20799b7edb0Smiod #endif
208bec266e2Spirofti 	ykbec_sc = sc;
209e1f5be9aSotto }
210e1f5be9aSotto 
211e1f5be9aSotto void
ykbec_write(struct ykbec_softc * mcsc,u_int reg,u_int datum)212e1f5be9aSotto ykbec_write(struct ykbec_softc *mcsc, u_int reg, u_int datum)
213e1f5be9aSotto {
214e1f5be9aSotto 	struct ykbec_softc *sc = (struct ykbec_softc *)mcsc;
215e1f5be9aSotto 	bus_space_tag_t iot = sc->sc_iot;
216e1f5be9aSotto 	bus_space_handle_t ioh = sc->sc_ioh;
217e1f5be9aSotto 
218e1f5be9aSotto 	bus_space_write_1(iot, ioh, 0, (reg >> 8) & 0xff);
219e1f5be9aSotto 	bus_space_write_1(iot, ioh, 1, (reg >> 0) & 0xff);
220e1f5be9aSotto 	bus_space_write_1(iot, ioh, 2, datum);
221e1f5be9aSotto }
222e1f5be9aSotto 
223e1f5be9aSotto u_int
ykbec_read(struct ykbec_softc * mcsc,u_int reg)224e1f5be9aSotto ykbec_read(struct ykbec_softc *mcsc, u_int reg)
225e1f5be9aSotto {
226e1f5be9aSotto 	struct ykbec_softc *sc = (struct ykbec_softc *)mcsc;
227e1f5be9aSotto 	bus_space_tag_t iot = sc->sc_iot;
228e1f5be9aSotto 	bus_space_handle_t ioh = sc->sc_ioh;
229e1f5be9aSotto 
230e1f5be9aSotto 	bus_space_write_1(iot, ioh, 0, (reg >> 8) & 0xff);
231e1f5be9aSotto 	bus_space_write_1(iot, ioh, 1, (reg >> 0) & 0xff);
232e1f5be9aSotto 	return bus_space_read_1(iot, ioh, 2);
233e1f5be9aSotto }
234e1f5be9aSotto 
235e1f5be9aSotto u_int
ykbec_read16(struct ykbec_softc * mcsc,u_int reg)236e1f5be9aSotto ykbec_read16(struct ykbec_softc *mcsc, u_int reg)
237e1f5be9aSotto {
238e1f5be9aSotto 	u_int val;
239e1f5be9aSotto 
240e1f5be9aSotto 	val = ykbec_read(mcsc, reg);
241e1f5be9aSotto 	return (val << 8) | ykbec_read(mcsc, reg + 1);
242e1f5be9aSotto }
243e1f5be9aSotto 
244e1f5be9aSotto #define KB3310_FAN_SPEED_DIVIDER	480000
245e1f5be9aSotto 
246e1f5be9aSotto #define ECTEMP_CURRENT_REG		0xf458
247e1f5be9aSotto #define REG_FAN_SPEED_HIGH		0xfe22
248e1f5be9aSotto #define REG_FAN_SPEED_LOW		0xfe23
249e1f5be9aSotto 
250e1f5be9aSotto #define REG_DESIGN_CAP_HIGH		0xf77d
251e1f5be9aSotto #define REG_DESIGN_CAP_LOW		0xf77e
252e1f5be9aSotto #define REG_FULLCHG_CAP_HIGH		0xf780
253e1f5be9aSotto #define REG_FULLCHG_CAP_LOW		0xf781
254e1f5be9aSotto 
255e1f5be9aSotto #define REG_DESIGN_VOL_HIGH		0xf782
256e1f5be9aSotto #define REG_DESIGN_VOL_LOW		0xf783
257e1f5be9aSotto #define REG_CURRENT_HIGH		0xf784
258e1f5be9aSotto #define REG_CURRENT_LOW			0xf785
259e1f5be9aSotto #define REG_VOLTAGE_HIGH		0xf786
260e1f5be9aSotto #define REG_VOLTAGE_LOW			0xf787
261e1f5be9aSotto #define REG_TEMPERATURE_HIGH		0xf788
262e1f5be9aSotto #define REG_TEMPERATURE_LOW		0xf789
263e1f5be9aSotto #define REG_RELATIVE_CAT_HIGH		0xf492
264e1f5be9aSotto #define REG_RELATIVE_CAT_LOW		0xf493
265e1f5be9aSotto #define REG_BAT_VENDOR			0xf4c4
266e1f5be9aSotto #define REG_BAT_CELL_COUNT		0xf4c6
26715562d19Sotto 
268e1f5be9aSotto #define REG_BAT_CHARGE			0xf4a2
26915562d19Sotto #define BAT_CHARGE_AC			0x00
27015562d19Sotto #define BAT_CHARGE_DISCHARGE		0x01
27115562d19Sotto #define BAT_CHARGE_CHARGE		0x02
27215562d19Sotto 
273e1f5be9aSotto #define REG_POWER_FLAG			0xf440
27415562d19Sotto #define POWER_FLAG_ADAPTER_IN		(1<<0)
27515562d19Sotto #define POWER_FLAG_POWER_ON		(1<<1)
27615562d19Sotto #define POWER_FLAG_ENTER_SUS		(1<<2)
277e1f5be9aSotto 
278e1f5be9aSotto #define REG_BAT_STATUS			0xf4b0
279e1f5be9aSotto #define BAT_STATUS_BAT_EXISTS		(1<<0)
280e1f5be9aSotto #define BAT_STATUS_BAT_FULL		(1<<1)
281e1f5be9aSotto #define BAT_STATUS_BAT_DESTROY		(1<<2)
282e1f5be9aSotto #define BAT_STATUS_BAT_LOW		(1<<5)
283e1f5be9aSotto 
284e1f5be9aSotto #define REG_CHARGE_STATUS		0xf4b1
285e1f5be9aSotto #define CHARGE_STATUS_PRECHARGE		(1<<1)
286e1f5be9aSotto #define CHARGE_STATUS_OVERHEAT		(1<<2)
287e1f5be9aSotto 
288e1f5be9aSotto #define REG_BAT_STATE			0xf482
289e1f5be9aSotto #define BAT_STATE_DISCHARGING		(1<<0)
290e1f5be9aSotto #define BAT_STATE_CHARGING		(1<<1)
291e1f5be9aSotto 
29299b7edb0Smiod #define	REG_BEEP_CONTROL		0xf4d0
29399b7edb0Smiod #define	BEEP_ENABLE			(1<<0)
29499b7edb0Smiod 
295bec266e2Spirofti #define REG_PMUCFG			0xff0c
296bec266e2Spirofti #define PMUCFG_STOP_MODE		(1<<7)
297bec266e2Spirofti #define PMUCFG_IDLE_MODE		(1<<6)
298bec266e2Spirofti #define PMUCFG_LPC_WAKEUP		(1<<5)
299bec266e2Spirofti #define PMUCFG_RESET_8051		(1<<4)
300bec266e2Spirofti #define PMUCFG_SCI_WAKEUP		(1<<3)
301bec266e2Spirofti #define PMUCFG_WDT_WAKEUP		(1<<2)
302bec266e2Spirofti #define PMUCFG_GPWU_WAKEUP		(1<<1)
303bec266e2Spirofti #define PMUCFG_IRQ_IDLE			(1<<0)
304bec266e2Spirofti 
305bec266e2Spirofti #define REG_USB0			0xf461
306bec266e2Spirofti #define REG_USB1			0xf462
307bec266e2Spirofti #define REG_USB2			0xf463
308bec266e2Spirofti #define USB_FLAG_ON			1
309bec266e2Spirofti #define USB_FLAG_OFF			0
310bec266e2Spirofti 
311bec266e2Spirofti #define REG_FAN_CONTROL			0xf4d2
312bec266e2Spirofti #define	REG_FAN_ON			1
313bec266e2Spirofti #define REG_FAN_OFF			0
314bec266e2Spirofti 
315f60e0988Sfcambus #define REG_LID_STATE			0xf4bd
316f60e0988Sfcambus #define LID_OPEN			1
317f60e0988Sfcambus #define LID_CLOSED			0
318f60e0988Sfcambus 
319bec266e2Spirofti #define YKBEC_SCI_IRQ			0xa
320bec266e2Spirofti 
321*b4f91ba2Smiod #ifdef KB3310_DEBUG
322c4b88ac2Sotto void
ykbec_print_bat_info(struct ykbec_softc * sc)323c4b88ac2Sotto ykbec_print_bat_info(struct ykbec_softc *sc)
324c4b88ac2Sotto {
325c4b88ac2Sotto 	uint bat_status, count, dvolt, dcap;
326c4b88ac2Sotto 
327c4b88ac2Sotto 	printf(": battery ");
328c4b88ac2Sotto 	bat_status = ykbec_read(sc, REG_BAT_STATUS);
329c4b88ac2Sotto 	if (!ISSET(bat_status, BAT_STATUS_BAT_EXISTS)) {
330c4b88ac2Sotto 		printf("absent");
331c4b88ac2Sotto 		return;
332c4b88ac2Sotto 	}
333c4b88ac2Sotto 
334c4b88ac2Sotto 	count = ykbec_read(sc, REG_BAT_CELL_COUNT);
335c4b88ac2Sotto 	dvolt = ykbec_read16(sc, REG_DESIGN_VOL_HIGH);
336c4b88ac2Sotto 	dcap = ykbec_read16(sc, REG_DESIGN_CAP_HIGH);
337c4b88ac2Sotto 	printf("%d cells, design capacity %dmV %dmAh", count, dvolt, dcap);
338c4b88ac2Sotto }
339c4b88ac2Sotto #endif
340c4b88ac2Sotto 
341e1f5be9aSotto void
ykbec_refresh(void * arg)342e1f5be9aSotto ykbec_refresh(void *arg)
343e1f5be9aSotto {
344e1f5be9aSotto 	struct ykbec_softc *sc = (struct ykbec_softc *)arg;
34515562d19Sotto 	u_int val, bat_charge, bat_status, charge_status, bat_state, power_flag;
346f60e0988Sfcambus 	u_int lid_state, cap_pct, fullcap;
347e1f5be9aSotto 	int current;
34813bfa4beSotto #if NAPM > 0
34913bfa4beSotto 	struct apm_power_info old;
35013bfa4beSotto #endif
351e1f5be9aSotto 
3525175924aSotto 	val = ykbec_read16(sc, REG_FAN_SPEED_HIGH) & 0xfffff;
35313bfa4beSotto 	if (val != 0) {
354e1f5be9aSotto 		val = KB3310_FAN_SPEED_DIVIDER / val;
35513bfa4beSotto 		sc->sc_sensor[YKBEC_FAN].value = val;
356b525169dSotto 		CLR(sc->sc_sensor[YKBEC_FAN].flags, SENSOR_FINVALID);
35713bfa4beSotto 	} else
358b525169dSotto 		SET(sc->sc_sensor[YKBEC_FAN].flags, SENSOR_FINVALID);
359e1f5be9aSotto 
360e1f5be9aSotto 	val = ykbec_read(sc, ECTEMP_CURRENT_REG);
36113bfa4beSotto 	sc->sc_sensor[YKBEC_ITEMP].value = val * 1000000 + 273150000;
362e1f5be9aSotto 
36313bfa4beSotto 	fullcap = ykbec_read16(sc, REG_FULLCHG_CAP_HIGH);
36413bfa4beSotto 	sc->sc_sensor[YKBEC_FCAP].value = fullcap * 1000;
3655175924aSotto 
366e1f5be9aSotto 	current = ykbec_read16(sc, REG_CURRENT_HIGH);
3675175924aSotto 	/* sign extend short -> int, int -> int64 will be done next statement */
3685175924aSotto 	current |= -(current & 0x8000);
36913bfa4beSotto 	sc->sc_sensor[YKBEC_BCURRENT].value = -1000 * current;
3705175924aSotto 
37113bfa4beSotto 	sc->sc_sensor[YKBEC_BVOLT].value = ykbec_read16(sc, REG_VOLTAGE_HIGH) *
37213bfa4beSotto 	    1000;
373e1f5be9aSotto 
374e1f5be9aSotto 	val = ykbec_read16(sc, REG_TEMPERATURE_HIGH);
37513bfa4beSotto 	sc->sc_sensor[YKBEC_BTEMP].value = val * 1000000 + 273150000;
376e1f5be9aSotto 
37713bfa4beSotto 	cap_pct = ykbec_read16(sc, REG_RELATIVE_CAT_HIGH);
37813bfa4beSotto 	sc->sc_sensor[YKBEC_CAP].value = cap_pct * 1000;
379e1f5be9aSotto 
38015562d19Sotto 	bat_charge = ykbec_read(sc, REG_BAT_CHARGE);
38115562d19Sotto 	bat_status = ykbec_read(sc, REG_BAT_STATUS);
38215562d19Sotto 	charge_status = ykbec_read(sc, REG_CHARGE_STATUS);
38315562d19Sotto 	bat_state = ykbec_read(sc, REG_BAT_STATE);
38415562d19Sotto 	power_flag = ykbec_read(sc, REG_POWER_FLAG);
385f60e0988Sfcambus 	lid_state = ykbec_read(sc, REG_LID_STATE);
386e1f5be9aSotto 
387b525169dSotto 	sc->sc_sensor[YKBEC_CHARGING].value = !!ISSET(bat_state,
388b525169dSotto 	    BAT_STATE_CHARGING);
389b525169dSotto 	sc->sc_sensor[YKBEC_AC].value = !!ISSET(power_flag,
390b525169dSotto 	    POWER_FLAG_ADAPTER_IN);
391b525169dSotto 
392f60e0988Sfcambus 	sc->sc_sensor[YKBEC_LID].value = !!ISSET(lid_state, LID_OPEN);
393f60e0988Sfcambus 
394b525169dSotto 	sc->sc_sensor[YKBEC_CAP].status = ISSET(bat_status, BAT_STATUS_BAT_LOW) ?
395b525169dSotto 		SENSOR_S_CRIT : SENSOR_S_OK;
39613bfa4beSotto 
39713bfa4beSotto #if NAPM > 0
39813bfa4beSotto 	bcopy(&ykbec_apmdata, &old, sizeof(old));
39913bfa4beSotto 	ykbec_apmdata.battery_life = cap_pct;
400b525169dSotto 	ykbec_apmdata.ac_state = ISSET(power_flag, POWER_FLAG_ADAPTER_IN) ?
40113bfa4beSotto 	    APM_AC_ON : APM_AC_OFF;
402b525169dSotto 	if (!ISSET(bat_status, BAT_STATUS_BAT_EXISTS)) {
40313bfa4beSotto 		ykbec_apmdata.battery_state = APM_BATTERY_ABSENT;
40413bfa4beSotto 		ykbec_apmdata.minutes_left = 0;
40513bfa4beSotto 		ykbec_apmdata.battery_life = 0;
40613bfa4beSotto 	} else {
407b525169dSotto 		if (ISSET(bat_state, BAT_STATE_CHARGING))
40813bfa4beSotto 			ykbec_apmdata.battery_state = APM_BATT_CHARGING;
409b525169dSotto 		else if (ISSET(bat_status, BAT_STATUS_BAT_LOW))
410b525169dSotto 			ykbec_apmdata.battery_state = APM_BATT_CRITICAL;
411438e94d1Sfcambus 		else if (cap_pct > 50)
41213bfa4beSotto 			ykbec_apmdata.battery_state = APM_BATT_HIGH;
41313bfa4beSotto 		else
41413bfa4beSotto 			ykbec_apmdata.battery_state = APM_BATT_LOW;
41513bfa4beSotto 
416c50a8308Sotto 		/* if charging, current is positive */
417b525169dSotto 		if (ISSET(bat_state, BAT_STATE_CHARGING))
418c50a8308Sotto 			current = 0;
419c50a8308Sotto 		else
42013bfa4beSotto 			current = -current;
421c50a8308Sotto 		/* XXX Yeeloong draw is about 1A */
42213bfa4beSotto 		if (current <= 0)
42313bfa4beSotto 			current = 1000;
424c50a8308Sotto 		/* XXX at 5?%, the Yeeloong shuts down */
42513bfa4beSotto 		if (cap_pct <= 5)
42613bfa4beSotto 			cap_pct = 0;
42713bfa4beSotto 		else
42813bfa4beSotto 			cap_pct -= 5;
42913bfa4beSotto 		fullcap = cap_pct * 60 * fullcap / 100;
43013bfa4beSotto 		ykbec_apmdata.minutes_left = fullcap / current;
43113bfa4beSotto 
43213bfa4beSotto 	}
43313bfa4beSotto 	if (old.ac_state != ykbec_apmdata.ac_state)
43413bfa4beSotto 		apm_record_event(APM_POWER_CHANGE, "AC power",
43513bfa4beSotto 			ykbec_apmdata.ac_state ? "restored" : "lost");
43613bfa4beSotto 	if (old.battery_state != ykbec_apmdata.battery_state)
43713bfa4beSotto 		apm_record_event(APM_POWER_CHANGE, "battery",
43813bfa4beSotto 		    BATTERY_STRING(ykbec_apmdata.battery_state));
43913bfa4beSotto #endif
44013bfa4beSotto }
44113bfa4beSotto 
44213bfa4beSotto #if NAPM > 0
44313bfa4beSotto int
ykbec_apminfo(struct apm_power_info * info)44413bfa4beSotto ykbec_apminfo(struct apm_power_info *info)
44513bfa4beSotto {
44613bfa4beSotto 	bcopy(&ykbec_apmdata, info, sizeof(struct apm_power_info));
44713bfa4beSotto 	return 0;
44813bfa4beSotto }
449bec266e2Spirofti 
450bec266e2Spirofti int
ykbec_suspend()451bec266e2Spirofti ykbec_suspend()
452bec266e2Spirofti {
453bec266e2Spirofti 	struct ykbec_softc *sc = ykbec_sc;
454bec266e2Spirofti 	int ctrl;
455bec266e2Spirofti 
4567001b0e9Smiod 	/*
4577001b0e9Smiod 	 * Set up wakeup sources: currently only the internal keyboard.
4587001b0e9Smiod 	 */
4597001b0e9Smiod 	loongson_set_isa_imr(1 << 1);
460bec266e2Spirofti 
461bec266e2Spirofti 	/* USB */
462bec266e2Spirofti 	DPRINTF(("USB\n"));
463bec266e2Spirofti 	ykbec_write(sc, REG_USB0, USB_FLAG_OFF);
464bec266e2Spirofti 	ykbec_write(sc, REG_USB1, USB_FLAG_OFF);
465bec266e2Spirofti 	ykbec_write(sc, REG_USB2, USB_FLAG_OFF);
466bec266e2Spirofti 
467bec266e2Spirofti 	/* EC */
468bec266e2Spirofti 	DPRINTF(("REG_PMUCFG\n"));
469bec266e2Spirofti 	ctrl = PMUCFG_SCI_WAKEUP | PMUCFG_WDT_WAKEUP | PMUCFG_GPWU_WAKEUP |
470bec266e2Spirofti 	    PMUCFG_LPC_WAKEUP | PMUCFG_STOP_MODE | PMUCFG_RESET_8051;
471bec266e2Spirofti 	ykbec_write(sc, REG_PMUCFG, ctrl);
472bec266e2Spirofti 
473bec266e2Spirofti 	/* FAN */
474bec266e2Spirofti 	DPRINTF(("FAN\n"));
475bec266e2Spirofti 	ykbec_write(sc, REG_FAN_CONTROL, REG_FAN_OFF);
476bec266e2Spirofti 
477bec266e2Spirofti 	/* CPU */
478bec266e2Spirofti 	DPRINTF(("CPU\n"));
479bec266e2Spirofti 	ykbec_chip_config = REGVAL(LOONGSON_CHIP_CONFIG0);
4807001b0e9Smiod 	enableintr();
481bec266e2Spirofti 	REGVAL(LOONGSON_CHIP_CONFIG0) = ykbec_chip_config & ~0x7;
482bec266e2Spirofti 	(void)REGVAL(LOONGSON_CHIP_CONFIG0);
483bec266e2Spirofti 
4847001b0e9Smiod 	/*
4857001b0e9Smiod 	 * When a resume interrupt fires, we will enter the interrupt
4867001b0e9Smiod 	 * dispatcher, which will do nothing because we are at splhigh,
4877001b0e9Smiod 	 * and execution flow will return here and continue.
4887001b0e9Smiod 	 */
4897001b0e9Smiod 	(void)disableintr();
4907001b0e9Smiod 
491bec266e2Spirofti 	return 0;
492bec266e2Spirofti }
493bec266e2Spirofti 
494bec266e2Spirofti int
ykbec_resume()495bec266e2Spirofti ykbec_resume()
496bec266e2Spirofti {
497bec266e2Spirofti 	struct ykbec_softc *sc = ykbec_sc;
498bec266e2Spirofti 
499bec266e2Spirofti 	/* CPU */
500bec266e2Spirofti 	DPRINTF(("CPU\n"));
501bec266e2Spirofti 	REGVAL(LOONGSON_CHIP_CONFIG0) = ykbec_chip_config;
502bec266e2Spirofti 	(void)REGVAL(LOONGSON_CHIP_CONFIG0);
503bec266e2Spirofti 
504bec266e2Spirofti 	/* FAN */
505bec266e2Spirofti 	DPRINTF(("FAN\n"));
506bec266e2Spirofti 	ykbec_write(sc, REG_FAN_CONTROL, REG_FAN_ON);
507bec266e2Spirofti 
508bec266e2Spirofti 	/* USB */
509bec266e2Spirofti 	DPRINTF(("USB\n"));
510bec266e2Spirofti 	ykbec_write(sc, REG_USB0, USB_FLAG_ON);
511bec266e2Spirofti 	ykbec_write(sc, REG_USB1, USB_FLAG_ON);
512bec266e2Spirofti 	ykbec_write(sc, REG_USB2, USB_FLAG_ON);
513bec266e2Spirofti 
5147001b0e9Smiod 	ykbec_refresh(sc);
5157001b0e9Smiod 
516bec266e2Spirofti 	return 0;
517bec266e2Spirofti }
51899b7edb0Smiod #endif
51913bfa4beSotto 
520f77164e9Smiod #if NPCKBD > 0 || NHIDKBD > 0
52199b7edb0Smiod void
ykbec_bell(void * arg,u_int pitch,u_int period,u_int volume,int poll)52299b7edb0Smiod ykbec_bell(void *arg, u_int pitch, u_int period, u_int volume, int poll)
52399b7edb0Smiod {
52499b7edb0Smiod 	struct ykbec_softc *sc = (struct ykbec_softc *)arg;
52599b7edb0Smiod 	int bctrl;
52699b7edb0Smiod 	int s;
52799b7edb0Smiod 
52899b7edb0Smiod 	s = spltty();
52999b7edb0Smiod 	bctrl = ykbec_read(sc, REG_BEEP_CONTROL);
53052fe8a0eSdlg 	if (timeout_del(&sc->sc_bell_tmo) || volume == 0) {
53199b7edb0Smiod 		/* inline ykbec_bell_stop(arg); */
53299b7edb0Smiod 		ykbec_write(sc, REG_BEEP_CONTROL, bctrl & ~BEEP_ENABLE);
53399b7edb0Smiod 	}
53499b7edb0Smiod 
53599b7edb0Smiod 	if (volume != 0) {
53699b7edb0Smiod 		ykbec_write(sc, REG_BEEP_CONTROL, bctrl | BEEP_ENABLE);
53799b7edb0Smiod 		if (poll) {
53899b7edb0Smiod 			delay(period * 1000);
53999b7edb0Smiod 			ykbec_write(sc, REG_BEEP_CONTROL, bctrl & ~BEEP_ENABLE);
54099b7edb0Smiod 		} else {
54199b7edb0Smiod 			timeout_add_msec(&sc->sc_bell_tmo, period);
54299b7edb0Smiod 		}
54399b7edb0Smiod 	}
54499b7edb0Smiod 	splx(s);
54599b7edb0Smiod }
54699b7edb0Smiod 
54799b7edb0Smiod void
ykbec_bell_stop(void * arg)54899b7edb0Smiod ykbec_bell_stop(void *arg)
54999b7edb0Smiod {
55099b7edb0Smiod 	struct ykbec_softc *sc = (struct ykbec_softc *)arg;
55199b7edb0Smiod 	int s;
55299b7edb0Smiod 
55399b7edb0Smiod 	s = spltty();
55499b7edb0Smiod 	ykbec_write(sc, REG_BEEP_CONTROL,
55599b7edb0Smiod 	    ykbec_read(sc, REG_BEEP_CONTROL) & ~BEEP_ENABLE);
55699b7edb0Smiod 	splx(s);
55799b7edb0Smiod }
55813bfa4beSotto #endif
559