xref: /netbsd-src/sys/arch/sparc64/dev/psm.c (revision 7fa608457b817eca6e0977b37f758ae064f3c99c)
1 /* $NetBSD: psm.c,v 1.6 2007/10/17 19:57:29 garbled Exp $ */
2 /*
3  * Copyright (c) 2006 Itronix Inc.
4  * All rights reserved.
5  *
6  * Ported from Tadpole Solaris sources by Garrett D'Amore for Itronix Inc.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. The name of Itronix Inc. may not be used to endorse
17  *    or promote products derived from this software without specific
18  *    prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY ITRONIX INC. ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL ITRONIX INC. BE LIABLE FOR ANY
24  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
27  * ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 /*
33  * Tadpole-RDI Ultrabook IIi (huxley) power management.  Note that
34  * there is a lot of stuff still missing here, due in part to the confusion
35  * that exists with the NetBSD power management framework.  I'm not wasting
36  * time with APM at this point, and some of sysmon seems "lacking".
37  */
38 #include <sys/cdefs.h>
39 __KERNEL_RCSID(0, "$NetBSD: psm.c,v 1.6 2007/10/17 19:57:29 garbled Exp $");
40 
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/proc.h>
44 #include <sys/kernel.h>
45 #include <sys/kthread.h>
46 #include <sys/types.h>
47 #include <sys/device.h>
48 #include <sys/poll.h>
49 #include <sys/kauth.h>
50 
51 #include <machine/autoconf.h>
52 #include <machine/bus.h>
53 #include <machine/intr.h>
54 
55 #include <dev/ebus/ebusreg.h>
56 #include <dev/ebus/ebusvar.h>
57 
58 #include <dev/sysmon/sysmonvar.h>
59 
60 #include <sparc64/dev/psmreg.h>
61 
62 struct psm_softc {
63 	struct device		sc_dev;
64 	bus_space_tag_t		sc_memt;
65 	bus_space_handle_t	sc_memh;
66 
67 	int			sc_event;
68 	int			sc_flags;
69 	struct sysmon_pswitch	sc_sm_pbutton;
70 	struct sysmon_pswitch	sc_sm_lid;
71 	struct sysmon_pswitch	sc_sm_ac;
72 	struct evcnt		sc_intrcnt;
73 	lwp_t			*sc_thread;
74 };
75 
76 #define	PUT8(sc, r, v)		\
77 	bus_space_write_1(sc->sc_memt, sc->sc_memh, r, v)
78 #define	GET8(sc, r)		\
79 	bus_space_read_1(sc->sc_memt, sc->sc_memh, r)
80 
81 #define	WAIT_DELAY		1000
82 #define	WAIT_RETRIES		1000
83 
84 #define	RESET_DELAY		200
85 #define	CMD_DELAY		10
86 #define	CMD_RETRIES		5
87 
88 #ifdef	DEBUG
89 #define	STATIC
90 #else
91 #define	STATIC	static
92 #endif
93 
94 /* flags indicating state */
95 #define	PSM_FLAG_ACPWR		0x1
96 #define	PSM_FLAG_LIDCLOSED	0x2
97 #define	PSM_FLAG_DOCKED		0x4
98 
99 /* system events -- causes activity in the event thread */
100 #define	PSM_EV_PBUTTON		0x1
101 #define	PSM_EV_LID		0x2
102 #define	PSM_EV_ACPWR		0x4
103 #define	PSM_EV_BATT		0x8
104 #define	PSM_EV_TEMP		0x10
105 
106 STATIC void psm_sysmon_setup(struct psm_softc *);
107 STATIC void psm_event_thread(void *);
108 STATIC int psm_init(struct psm_softc *);
109 STATIC void psm_reset(struct psm_softc *);
110 STATIC void psm_poll_acpower(struct psm_softc *);
111 STATIC int psm_intr(void *);
112 STATIC int psm_misc_rd(struct psm_softc *, uint8_t, uint8_t *);
113 STATIC int psm_misc_wr(struct psm_softc *, uint8_t, uint8_t);
114 STATIC int psm_wait(struct psm_softc *, uint8_t);
115 #if 0
116 STATIC int psm_ecmd_rd16(struct psm_softc *, uint16_t *, uint8_t, uint8_t,
117     uint8_t);
118 #endif
119 STATIC int psm_ecmd_rd8(struct psm_softc *, uint8_t *, uint8_t, uint8_t,
120     uint8_t);
121 STATIC int psm_ecmd_wr8(struct psm_softc *, uint8_t, uint8_t, uint8_t,
122     uint8_t);
123 STATIC int psm_match(struct device *, struct cfdata *, void *);
124 STATIC void psm_attach(struct device *, struct device *, void *);
125 
126 CFATTACH_DECL(psm, sizeof(struct psm_softc),
127     psm_match, psm_attach, NULL, NULL);
128 
129 
130 int
131 psm_match(struct device *parent, struct cfdata *cf, void *aux)
132 {
133 	struct ebus_attach_args *ea = aux;
134 
135 	if (strcmp(ea->ea_name, "psm") != 0)
136 		return (0);
137 	return (1);
138 }
139 
140 void
141 psm_attach(struct device *parent, struct device *self, void *aux)
142 {
143 	struct psm_softc	*sc = (struct psm_softc *)self;
144 	struct ebus_attach_args	*ea = aux;
145 	bus_addr_t		devaddr;
146 	char			*xname;
147 
148 	xname = sc->sc_dev.dv_xname;
149 
150 	sc->sc_memt = ea->ea_bustag;
151 	devaddr = EBUS_ADDR_FROM_REG(&ea->ea_reg[0]);
152 
153 	if (bus_space_map(sc->sc_memt, devaddr, ea->ea_reg[0].size,
154 		0, &sc->sc_memh) != 0) {
155 		printf(": unable to map device registers\n");
156 		return;
157 	}
158 	if (psm_init(sc) != 0) {
159 		printf(": unable to initialize device\n");
160 		return;
161 	}
162 
163 	printf(": UltraBook IIi power control\n");
164 
165 	psm_sysmon_setup(sc);
166 
167 	if (kthread_create(PRI_NONE, 0, NULL, psm_event_thread, sc,
168 	    &sc->sc_thread, "%s", sc->sc_dev.dv_xname) != 0) {
169 		printf("%s: unable to create event kthread\n",
170 		    sc->sc_dev.dv_xname);
171 	}
172 
173 	/*
174 	 * Establish device interrupts
175 	 */
176 	(void) bus_intr_establish(sc->sc_memt, ea->ea_intr[0], IPL_HIGH,
177 	    psm_intr, sc);
178 	evcnt_attach_dynamic(&sc->sc_intrcnt, EVCNT_TYPE_INTR, NULL,
179 	    sc->sc_dev.dv_xname, "intr");
180 }
181 
182 /*
183  * Register sensors and events with sysmon.
184  */
185 void
186 psm_sysmon_setup(struct psm_softc *sc)
187 {
188 	const char	*xname	= sc->sc_dev.dv_xname;
189 
190 
191 	/*
192 	 * XXX: Register sysmon environment.
193 	 */
194 
195 	/*
196 	 * Register sysmon events
197 	 */
198 	sc->sc_sm_pbutton.smpsw_name = xname;
199 	sc->sc_sm_pbutton.smpsw_type = PSWITCH_TYPE_POWER;
200 	if (sysmon_pswitch_register(&sc->sc_sm_pbutton) != 0)
201 		printf("%s: unable to register power button\n", xname);
202 
203 	sc->sc_sm_lid.smpsw_name = xname;
204 	sc->sc_sm_lid.smpsw_type = PSWITCH_TYPE_LID;
205 	if (sysmon_pswitch_register(&sc->sc_sm_lid) != 0)
206 		printf("%s: unable to register lid switch\n", xname);
207 
208 	sc->sc_sm_ac.smpsw_name = xname;
209 	sc->sc_sm_ac.smpsw_type = PSWITCH_TYPE_ACADAPTER;
210 	if (sysmon_pswitch_register(&sc->sc_sm_ac) != 0)
211 		printf("%s: unable to register AC adapter\n", xname);
212 }
213 
214 void
215 psm_event_thread(void *arg)
216 {
217 	struct psm_softc *sc = arg;
218 	int	x;
219 	int	event;
220 	int	flags;
221 
222 	for (;;) {
223 		x = splhigh();
224 		/* check for AC power.  sets event if there is a change */
225 		psm_poll_acpower(sc);
226 
227 		/* read and clear events */
228 		event = sc->sc_event;
229 		flags = sc->sc_flags;
230 		sc->sc_event = 0;
231 		splx(x);
232 
233 		if (event & PSM_EV_PBUTTON) {
234 			sysmon_pswitch_event(&sc->sc_sm_pbutton,
235 			    PSWITCH_EVENT_PRESSED);
236 		}
237 
238 		if (event & PSM_EV_LID) {
239 			sysmon_pswitch_event(&sc->sc_sm_lid,
240 			    flags & PSM_FLAG_LIDCLOSED ?
241 			    PSWITCH_EVENT_PRESSED : PSWITCH_EVENT_RELEASED);
242 		}
243 
244 		if (event & PSM_EV_ACPWR) {
245 			sysmon_pswitch_event(&sc->sc_sm_ac,
246 			    flags & PSM_FLAG_ACPWR ?
247 			    PSWITCH_EVENT_PRESSED : PSWITCH_EVENT_RELEASED);
248 		}
249 
250 		/* XXX: handle PSM_EV_TEMP */
251 
252 		/* one second interval between probes of power */
253 		tsleep(&sc, PWAIT, "psm", hz);
254 	}
255 }
256 
257 int
258 psm_init(struct psm_softc *sc)
259 {
260 	int	x;
261 	uint8_t	batt = 0xff;	/* keep GCC 4.x happy */
262 
263 	/* clear interrupts */
264 	x = splhigh();
265 	PUT8(sc, PSM_ICR, 0xff);
266 	splx(x);
267 
268 	/* enable interrupts */
269 	if (psm_misc_wr(sc, PSM_MISC_IMR, PSM_IMR_ALL))
270 		return (-1);
271 
272 	/* make sure that UPS battery is reasonable */
273 	if (psm_misc_rd(sc, PSM_MISC_UPS, &batt) || (batt > PSM_MAX_BATTERIES))
274 		if (psm_misc_wr(sc, PSM_MISC_UPS, batt))
275 			printf("%s: cannot set UPS battery",
276 			    sc->sc_dev.dv_xname);
277 
278 	return (0);
279 }
280 
281 void
282 psm_reset(struct psm_softc *sc)
283 {
284 
285 	PUT8(sc, PSM_MCR, PSM_MCR_RST);
286 	delay(RESET_DELAY);
287 }
288 
289 void
290 psm_poll_acpower(struct psm_softc *sc)
291 {
292 	int	flags = sc->sc_flags;
293 
294 	if (GET8(sc, PSM_STAT) & PSM_STAT_AC) {
295 		sc->sc_flags |= PSM_FLAG_ACPWR;
296 	} else {
297 		sc->sc_flags &= ~PSM_FLAG_ACPWR;
298 	}
299 	if (flags != sc->sc_flags)
300 		sc->sc_event |= PSM_EV_ACPWR;
301 }
302 
303 int
304 psm_misc_rd(struct psm_softc *sc, uint8_t mreg, uint8_t *data)
305 {
306 
307 	return (psm_ecmd_rd8(sc, data, mreg, PSM_MODE_MISC, 0));
308 }
309 
310 int
311 psm_misc_wr(struct psm_softc *sc, uint8_t mreg, uint8_t data)
312 {
313 
314 	return (psm_ecmd_wr8(sc, data, mreg, PSM_MODE_MISC, 0));
315 }
316 
317 int
318 psm_wait(struct psm_softc *sc, uint8_t flag)
319 {
320     int retr = WAIT_RETRIES;
321 
322     while (GET8(sc, PSM_STAT) & flag) {
323 	    if (!(retr--)) {
324 		    return (-1);
325 	    }
326 	    delay(WAIT_DELAY);
327     }
328     return (0);
329 }
330 
331 #if 0
332 int
333 psm_ecmd_rd16(struct psm_softc *sc, uint16_t *data, uint8_t iar, uint8_t mode,
334     uint8_t addr)
335 {
336 	uint8_t	cmr = PSM_CMR_DATA(mode, PSM_L_16, PSM_D_RD, addr);
337 	int	x, rc, retr = CMD_RETRIES;
338 
339 	x = splhigh();
340 
341 	do {
342 		if ((rc = psm_wait(sc, PSM_STAT_RDA)) != 0) {
343 			psm_reset(sc);
344 			continue;
345 		}
346 
347 		PUT8(sc, PSM_IAR, iar);
348 		PUT8(sc, PSM_CMR, cmr);
349 
350 		delay(CMD_DELAY);
351 
352 		if ((rc = psm_wait(sc, PSM_STAT_RDA)) == 0) {
353 			*data = GET8(sc, PSM_PWDL) | (GET8(sc, PSM_PWDU) << 8);
354 			break;
355 		}
356 
357 		psm_reset(sc);
358 
359 	} while (--retr);
360 
361 	splx(x);
362 	return (rc);
363 }
364 #endif
365 
366 int
367 psm_ecmd_rd8(struct psm_softc *sc, uint8_t *data, uint8_t iar, uint8_t mode,
368     uint8_t addr)
369 {
370 	uint8_t	cmr = PSM_CMR_DATA(mode, PSM_L_8, PSM_D_RD, addr);
371 	int	x, rc, retr = CMD_RETRIES;
372 
373 	x = splhigh();
374 
375 	do {
376 		if ((rc = psm_wait(sc, PSM_STAT_RDA)) != 0) {
377 			psm_reset(sc);
378 			continue;
379 		}
380 
381 		PUT8(sc, PSM_IAR, iar);
382 		PUT8(sc, PSM_CMR, cmr);
383 
384 		delay(CMD_DELAY);
385 
386 		if ((rc = psm_wait(sc, PSM_STAT_RDA)) == 0) {
387 			(void) GET8(sc, PSM_PWDU);
388 			*data = GET8(sc, PSM_PWDL);
389 			break;
390 		}
391 
392 		psm_reset(sc);
393 
394 	} while (--retr);
395 
396 	splx(x);
397 	return (rc);
398 }
399 
400 int
401 psm_ecmd_wr8(struct psm_softc *sc, uint8_t data, uint8_t iar, uint8_t mode,
402     uint8_t addr)
403 {
404 	uint8_t	cmr = PSM_CMR_DATA(mode, PSM_L_8, PSM_D_WR, addr);
405 	int	x, rc, retr = CMD_RETRIES;
406 
407 	x = splhigh();
408 
409 	do {
410 		if ((rc = psm_wait(sc, PSM_STAT_WBF)) != 0) {
411 			psm_reset(sc);
412 			continue;
413 		}
414 
415 		PUT8(sc, PSM_PWDU, 0);
416 		PUT8(sc, PSM_PWDL, data);
417 		PUT8(sc, PSM_IAR, iar);
418 		PUT8(sc, PSM_CMR, cmr);
419 
420 		delay(CMD_DELAY);
421 
422 		if ((rc = psm_wait(sc, PSM_STAT_WBF)) == 0) {
423 			break;
424 		}
425 
426 		psm_reset(sc);
427 	} while (--retr);
428 
429 	splx(x);
430 
431 	return (rc);
432 }
433 
434 int
435 psm_intr(void *arg)
436 {
437 	struct psm_softc *sc = arg;
438 	uint8_t	isr;
439 
440 	isr = GET8(sc, PSM_ISR);
441 	if (isr & PSM_ISR_PO) {
442 		PUT8(sc, PSM_ICR, PSM_ISR_PO);
443 		sc->sc_event |= PSM_EV_PBUTTON;
444 	}
445 	if (isr & PSM_ISR_DK) {
446 		PUT8(sc, PSM_ICR, PSM_ISR_DK);
447 		sc->sc_flags |= PSM_FLAG_DOCKED;
448 	}
449 	if (isr & PSM_ISR_UDK) {
450 		PUT8(sc, PSM_ICR, PSM_ISR_UDK);
451 		sc->sc_flags &= ~PSM_FLAG_DOCKED;
452 	}
453 	if (isr & PSM_ISR_LIDC) {
454 		PUT8(sc, PSM_ICR, PSM_ISR_LIDC);
455 		sc->sc_flags |= PSM_FLAG_LIDCLOSED;
456 		sc->sc_event |= PSM_EV_LID;
457 	}
458 	if (isr & PSM_ISR_LIDO) {
459 		PUT8(sc, PSM_ICR, PSM_ISR_LIDO);
460 		sc->sc_flags &= ~PSM_FLAG_LIDCLOSED;
461 		sc->sc_event |= PSM_EV_LID;
462 	}
463 	if (isr & PSM_ISR_TMP) {
464 		/* Over temperature */
465 		PUT8(sc, PSM_ICR, PSM_ISR_TMP);
466 		sc->sc_event |= PSM_EV_TEMP;
467 	}
468 	if (isr & PSM_ISR_BCC) {
469 		/* battery config changed */
470 		PUT8(sc, PSM_ICR, PSM_ISR_BCC);
471 		sc->sc_event |= PSM_EV_BATT;
472 	}
473 	if (isr & PSM_ISR_RPD) {
474 		/* request to power down */
475 		sc->sc_event |= PSM_EV_PBUTTON;
476 	}
477 	if (sc->sc_event) {
478 		/* wake up the thread */
479 		wakeup(sc);
480 	}
481 	return (1);
482 }
483