xref: /freebsd-11-stable/sys/i386/isa/pmtimer.c (revision 4ab2e064d7950be84256d671a7ae93f87cc6aa36)
1 /*-
2  * Copyright (c) 2000 Mitsuru IWASAKI <iwasaki@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 /*
31  * Timer device driver for power management events.
32  * The code for suspend/resume is derived from APM device driver.
33  */
34 
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/bus.h>
38 #include <sys/kernel.h>
39 #include <sys/module.h>
40 #include <sys/syslog.h>
41 #include <machine/clock.h>
42 
43 #include <isa/isavar.h>
44 
45 static devclass_t pmtimer_devclass;
46 
47 /* reject any PnP devices for now */
48 static struct isa_pnp_id pmtimer_ids[] = {
49 	{0}
50 };
51 
52 static void
pmtimer_identify(driver_t * driver,device_t parent)53 pmtimer_identify(driver_t *driver, device_t parent)
54 {
55 	device_t child;
56 
57 	/*
58 	 * Only add a child if one doesn't exist already.
59 	 */
60 	child = devclass_get_device(pmtimer_devclass, 0);
61 	if (child == NULL) {
62 		child = BUS_ADD_CHILD(parent, 0, "pmtimer", 0);
63 		if (child == NULL)
64 			panic("pmtimer_identify");
65 	}
66 }
67 
68 static int
pmtimer_probe(device_t dev)69 pmtimer_probe(device_t dev)
70 {
71 
72 	if (ISA_PNP_PROBE(device_get_parent(dev), dev, pmtimer_ids) == ENXIO)
73 		return (ENXIO);
74 
75 	/* only one instance always */
76 	return (device_get_unit(dev));
77 }
78 
79 static struct timeval suspend_time;
80 static struct timeval diff_time;
81 
82 static int
pmtimer_suspend(device_t dev)83 pmtimer_suspend(device_t dev)
84 {
85 
86 	microtime(&diff_time);
87 	inittodr(0);
88 	microtime(&suspend_time);
89 	timevalsub(&diff_time, &suspend_time);
90 	return (0);
91 }
92 
93 static int
pmtimer_resume(device_t dev)94 pmtimer_resume(device_t dev)
95 {
96 	u_int second, minute, hour;
97 	struct timeval resume_time, tmp_time;
98 
99 	/* modified for adjkerntz */
100 	timer_restore();		/* restore the all timers */
101 	inittodr(0);			/* adjust time to RTC */
102 	microtime(&resume_time);
103 	getmicrotime(&tmp_time);
104 	timevaladd(&tmp_time, &diff_time);
105 
106 #ifdef FIXME
107 	/* XXX THIS DOESN'T WORK!!! */
108 	time = tmp_time;
109 #endif
110 
111 #ifdef PMTIMER_FIXUP_CALLTODO
112 	/* Calculate the delta time suspended */
113 	timevalsub(&resume_time, &suspend_time);
114 	/* Fixup the calltodo list with the delta time. */
115 	adjust_timeout_calltodo(&resume_time);
116 	/*
117 	 * We've already calculated resume_time to be the delta between
118 	 * the suspend and the resume.
119 	 */
120 	second = resume_time.tv_sec;
121 #else /* !PMTIMER_FIXUP_CALLTODO */
122 	second = resume_time.tv_sec - suspend_time.tv_sec;
123 #endif /* PMTIMER_FIXUP_CALLTODO */
124 	hour = second / 3600;
125 	second %= 3600;
126 	minute = second / 60;
127 	second %= 60;
128 	log(LOG_NOTICE, "wakeup from sleeping state (slept %02d:%02d:%02d)\n",
129 		hour, minute, second);
130 	return (0);
131 }
132 
133 static device_method_t pmtimer_methods[] = {
134 	/* Device interface */
135 	DEVMETHOD(device_identify,	pmtimer_identify),
136 	DEVMETHOD(device_probe,		pmtimer_probe),
137 	DEVMETHOD(device_attach,	bus_generic_attach),
138 	DEVMETHOD(device_suspend,	pmtimer_suspend),
139 	DEVMETHOD(device_resume,	pmtimer_resume),
140 	{ 0, 0 }
141 };
142 
143 static driver_t pmtimer_driver = {
144 	"pmtimer",
145 	pmtimer_methods,
146 	1,		/* no softc */
147 };
148 
149 DRIVER_MODULE(pmtimer, isa, pmtimer_driver, pmtimer_devclass, 0, 0);
150