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