1 /*- 2 * Copyright (c) 1988 University of Utah. 3 * Copyright (c) 1982, 1990, 1993 4 * The Regents of the University of California. 5 * Copyright (c) 2011 The FreeBSD Foundation 6 * All rights reserved. 7 * 8 * This code is derived from software contributed to Berkeley by 9 * the Systems Programming Group of the University of Utah Computer 10 * Science Department. 11 * 12 * Portions of this software were developed by Julien Ridoux at the University 13 * of Melbourne under sponsorship from the FreeBSD Foundation. 14 * 15 * Redistribution and use in source and binary forms, with or without 16 * modification, are permitted provided that the following conditions 17 * are met: 18 * 1. Redistributions of source code must retain the above copyright 19 * notice, this list of conditions and the following disclaimer. 20 * 2. Redistributions in binary form must reproduce the above copyright 21 * notice, this list of conditions and the following disclaimer in the 22 * documentation and/or other materials provided with the distribution. 23 * 4. Neither the name of the University nor the names of its contributors 24 * may be used to endorse or promote products derived from this software 25 * without specific prior written permission. 26 * 27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 37 * SUCH DAMAGE. 38 * 39 * from: Utah $Hdr: clock.c 1.18 91/01/21$ 40 * from: @(#)clock.c 8.2 (Berkeley) 1/12/94 41 * from: NetBSD: clock_subr.c,v 1.6 2001/07/07 17:04:02 thorpej Exp 42 * and 43 * from: src/sys/i386/isa/clock.c,v 1.176 2001/09/04 44 */ 45 46 /* 47 * Helpers for time-of-day clocks. This is useful for architectures that need 48 * support multiple models of such clocks, and generally serves to make the 49 * code more machine-independent. 50 * If the clock in question can also be used as a time counter, the driver 51 * needs to initiate this. 52 * This code is not yet used by all architectures. 53 */ 54 55 #include <sys/cdefs.h> 56 __FBSDID("$FreeBSD$"); 57 58 #include "opt_ffclock.h" 59 60 #include <sys/param.h> 61 #include <sys/systm.h> 62 #include <sys/kernel.h> 63 #include <sys/bus.h> 64 #include <sys/clock.h> 65 #include <sys/sysctl.h> 66 #ifdef FFCLOCK 67 #include <sys/timeffc.h> 68 #endif 69 #include <sys/timetc.h> 70 71 #include "clock_if.h" 72 73 static device_t clock_dev = NULL; 74 static long clock_res; 75 static struct timespec clock_adj; 76 77 /* XXX: should be kern. now, it's no longer machdep. */ 78 static int disable_rtc_set; 79 SYSCTL_INT(_machdep, OID_AUTO, disable_rtc_set, CTLFLAG_RW, &disable_rtc_set, 80 0, "Disallow adjusting time-of-day clock"); 81 82 void 83 clock_register(device_t dev, long res) /* res has units of microseconds */ 84 { 85 86 if (clock_dev != NULL) { 87 if (clock_res > res) { 88 if (bootverbose) 89 device_printf(dev, "not installed as " 90 "time-of-day clock: clock %s has higher " 91 "resolution\n", device_get_name(clock_dev)); 92 return; 93 } 94 if (bootverbose) 95 device_printf(clock_dev, "removed as " 96 "time-of-day clock: clock %s has higher " 97 "resolution\n", device_get_name(dev)); 98 } 99 clock_dev = dev; 100 clock_res = res; 101 clock_adj.tv_sec = res / 2 / 1000000; 102 clock_adj.tv_nsec = res / 2 % 1000000 * 1000; 103 if (bootverbose) 104 device_printf(dev, "registered as a time-of-day clock " 105 "(resolution %ldus, adjustment %jd.%09jds)\n", res, 106 (intmax_t)clock_adj.tv_sec, (intmax_t)clock_adj.tv_nsec); 107 } 108 109 /* 110 * inittodr and settodr derived from the i386 versions written 111 * by Christoph Robitschko <chmr@edvz.tu-graz.ac.at>, reintroduced and 112 * updated by Chris Stenton <chris@gnome.co.uk> 8/10/94 113 */ 114 115 /* 116 * Initialize the time of day register, based on the time base which is, e.g. 117 * from a filesystem. 118 */ 119 void 120 inittodr(time_t base) 121 { 122 struct timespec ts; 123 int error; 124 125 if (clock_dev == NULL) { 126 printf("warning: no time-of-day clock registered, system time " 127 "will not be set accurately\n"); 128 goto wrong_time; 129 } 130 /* XXX: We should poll all registered RTCs in case of failure */ 131 error = CLOCK_GETTIME(clock_dev, &ts); 132 if (error != 0 && error != EINVAL) { 133 printf("warning: clock_gettime failed (%d), the system time " 134 "will not be set accurately\n", error); 135 goto wrong_time; 136 } 137 if (error == EINVAL || ts.tv_sec < 0) { 138 printf("Invalid time in real time clock.\n" 139 "Check and reset the date immediately!\n"); 140 goto wrong_time; 141 } 142 143 ts.tv_sec += utc_offset(); 144 timespecadd(&ts, &clock_adj); 145 tc_setclock(&ts); 146 #ifdef FFCLOCK 147 ffclock_reset_clock(&ts); 148 #endif 149 return; 150 151 wrong_time: 152 if (base > 0) { 153 ts.tv_sec = base; 154 ts.tv_nsec = 0; 155 tc_setclock(&ts); 156 } 157 } 158 159 /* 160 * Write system time back to RTC 161 */ 162 void 163 resettodr(void) 164 { 165 struct timespec ts; 166 int error; 167 168 if (disable_rtc_set || clock_dev == NULL) 169 return; 170 171 getnanotime(&ts); 172 timespecadd(&ts, &clock_adj); 173 ts.tv_sec -= utc_offset(); 174 /* XXX: We should really set all registered RTCs */ 175 if ((error = CLOCK_SETTIME(clock_dev, &ts)) != 0) 176 printf("warning: clock_settime failed (%d), time-of-day clock " 177 "not adjusted to system time\n", error); 178 } 179