i386 - Get completely rid of APIC_IO
[dragonfly.git] / sys / platform / pc32 / isa / pmtimer.c
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 /*
28  * Timer device driver for power management events.
29  * The code for suspend/resume is derived from APM device driver.
30  */
31
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/bus.h>
35 #include <sys/kernel.h>
36 #include <sys/syslog.h>
37 #include <sys/thread2.h>
38 #include <machine/clock.h>
39
40 #include <bus/isa/isavar.h>
41
42 static devclass_t pmtimer_devclass;
43
44 /* reject any PnP devices for now */
45 static struct isa_pnp_id pmtimer_ids[] = {
46         {0}
47 };
48
49 static int
50 pmtimer_probe(device_t dev)
51 {
52
53         if (ISA_PNP_PROBE(device_get_parent(dev), dev, pmtimer_ids) == ENXIO) {
54                 return (ENXIO);
55         }
56
57         /* only one instance always */
58         return (device_get_unit(dev));
59 }
60
61 static struct timeval suspend_time;
62 static struct timeval diff_time;
63
64 static int
65 pmtimer_suspend(device_t dev)
66 {
67         crit_enter();
68         microtime(&diff_time);
69         inittodr(0);
70         microtime(&suspend_time);
71         timevalsub(&diff_time, &suspend_time);
72         crit_exit();
73         return (0);
74 }
75
76 static int
77 pmtimer_resume(device_t dev)
78 {
79         u_int second, minute, hour;
80         struct timeval resume_time;
81
82         /* modified for adjkerntz */
83         crit_enter();
84         timer_restore();                /* restore the all timers */
85         inittodr(0);                    /* adjust time to RTC */
86         microtime(&resume_time);
87
88         crit_exit();
89         second = resume_time.tv_sec - suspend_time.tv_sec; 
90         hour = second / 3600;
91         second %= 3600;
92         minute = second / 60;
93         second %= 60;
94         log(LOG_NOTICE, "wakeup from sleeping state (slept %02d:%02d:%02d)\n",
95                 hour, minute, second);
96         return (0);
97 }
98
99 /*
100  * Because pmtimer is a static device that always exists under any attached
101  * isa device, and not scanned by the isa device, we need an identify
102  * function to install the device.
103  */
104 static device_method_t pmtimer_methods[] = {
105         /* Device interface */
106         DEVMETHOD(device_identify,      bus_generic_identify),
107         DEVMETHOD(device_probe,         pmtimer_probe),
108         DEVMETHOD(device_attach,        bus_generic_attach),
109         DEVMETHOD(device_suspend,       pmtimer_suspend),
110         DEVMETHOD(device_resume,        pmtimer_resume),
111         { 0, 0 }
112 };
113
114 static driver_t pmtimer_driver = {
115         "pmtimer",
116         pmtimer_methods,
117         1,              /* no softc */
118 };
119
120 DRIVER_MODULE(pmtimer, isa, pmtimer_driver, pmtimer_devclass, 0, 0);