xref: /dragonfly/sys/platform/pc64/isa/pmtimer.c (revision 67640b13)
1 /*-
2  * Copyright (c) 2000 Mitsuru IWASAKI <iwasaki@FreeBSD.org>
3  * Copyright (c) 2008 The DragonFly Project.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 /*
29  * Timer device driver for power management events.
30  * The code for suspend/resume is derived from APM device driver.
31  */
32 
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/bus.h>
36 #include <sys/kernel.h>
37 #include <sys/syslog.h>
38 #include <sys/thread2.h>
39 #include <machine/clock.h>
40 
41 #include <bus/isa/isavar.h>
42 
43 static devclass_t pmtimer_devclass;
44 
45 /* reject any PnP devices for now */
46 static struct isa_pnp_id pmtimer_ids[] = {
47 	{0}
48 };
49 
50 static int
51 pmtimer_probe(device_t dev)
52 {
53 
54 	if (ISA_PNP_PROBE(device_get_parent(dev), dev, pmtimer_ids) == ENXIO) {
55 		return (ENXIO);
56 	}
57 
58 	/* only one instance always */
59 	return (device_get_unit(dev));
60 }
61 
62 static struct timeval suspend_time;
63 static struct timeval diff_time;
64 
65 static int
66 pmtimer_suspend(device_t dev)
67 {
68 	crit_enter();
69 	microtime(&diff_time);
70 	inittodr(0);
71 	microtime(&suspend_time);
72 	timevalsub(&diff_time, &suspend_time);
73 	crit_exit();
74 	return (0);
75 }
76 
77 static int
78 pmtimer_resume(device_t dev)
79 {
80 	u_int second, minute, hour;
81 	struct timeval resume_time;
82 
83 	/* modified for adjkerntz */
84 	crit_enter();
85 	timer_restore();		/* restore the all timers */
86 	inittodr(0);			/* adjust time to RTC */
87 	microtime(&resume_time);
88 
89 	crit_exit();
90 	second = resume_time.tv_sec - suspend_time.tv_sec;
91 	hour = second / 3600;
92 	second %= 3600;
93 	minute = second / 60;
94 	second %= 60;
95 	log(LOG_NOTICE, "wakeup from sleeping state (slept %02d:%02d:%02d)\n",
96 		hour, minute, second);
97 	return (0);
98 }
99 
100 /*
101  * Because pmtimer is a static device that always exists under any attached
102  * isa device, and not scanned by the isa device, we need an identify
103  * function to install the device.
104  */
105 static device_method_t pmtimer_methods[] = {
106 	/* Device interface */
107 	DEVMETHOD(device_identify,	bus_generic_identify),
108 	DEVMETHOD(device_probe,		pmtimer_probe),
109 	DEVMETHOD(device_attach,	bus_generic_attach),
110 	DEVMETHOD(device_suspend,	pmtimer_suspend),
111 	DEVMETHOD(device_resume,	pmtimer_resume),
112 	DEVMETHOD_END
113 };
114 
115 static driver_t pmtimer_driver = {
116 	"pmtimer",
117 	pmtimer_methods,
118 	1,		/* no softc */
119 };
120 
121 DRIVER_MODULE(pmtimer, isa, pmtimer_driver, pmtimer_devclass, NULL, NULL);
122