subr_rtc.c revision 227723
1139804Simp/*- 293835Stmm * Copyright (c) 1988 University of Utah. 393835Stmm * Copyright (c) 1982, 1990, 1993 4227723Slstewart * The Regents of the University of California. 5227723Slstewart * Copyright (c) 2011 The FreeBSD Foundation 6227723Slstewart * All rights reserved. 793835Stmm * 893835Stmm * This code is derived from software contributed to Berkeley by 993835Stmm * the Systems Programming Group of the University of Utah Computer 1093835Stmm * Science Department. 1193835Stmm * 12227723Slstewart * Portions of this software were developed by Julien Ridoux at the University 13227723Slstewart * of Melbourne under sponsorship from the FreeBSD Foundation. 14227723Slstewart * 1593835Stmm * Redistribution and use in source and binary forms, with or without 1693835Stmm * modification, are permitted provided that the following conditions 1793835Stmm * are met: 1893835Stmm * 1. Redistributions of source code must retain the above copyright 1993835Stmm * notice, this list of conditions and the following disclaimer. 2093835Stmm * 2. Redistributions in binary form must reproduce the above copyright 2193835Stmm * notice, this list of conditions and the following disclaimer in the 2293835Stmm * documentation and/or other materials provided with the distribution. 2393835Stmm * 4. Neither the name of the University nor the names of its contributors 2493835Stmm * may be used to endorse or promote products derived from this software 2593835Stmm * without specific prior written permission. 2693835Stmm * 2793835Stmm * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 2893835Stmm * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2993835Stmm * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 3093835Stmm * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 3193835Stmm * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 3293835Stmm * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 3393835Stmm * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 3493835Stmm * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 3593835Stmm * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 3693835Stmm * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3793835Stmm * SUCH DAMAGE. 3893835Stmm * 3993835Stmm * from: Utah $Hdr: clock.c 1.18 91/01/21$ 4093835Stmm * from: @(#)clock.c 8.2 (Berkeley) 1/12/94 4193835Stmm * from: NetBSD: clock_subr.c,v 1.6 2001/07/07 17:04:02 thorpej Exp 4293835Stmm * and 4393835Stmm * from: src/sys/i386/isa/clock.c,v 1.176 2001/09/04 4493835Stmm */ 4593835Stmm 4693835Stmm/* 4793835Stmm * Helpers for time-of-day clocks. This is useful for architectures that need 4893835Stmm * support multiple models of such clocks, and generally serves to make the 4993835Stmm * code more machine-independent. 5093835Stmm * If the clock in question can also be used as a time counter, the driver 5193835Stmm * needs to initiate this. 5293835Stmm * This code is not yet used by all architectures. 5393835Stmm */ 5493835Stmm 55116182Sobrien#include <sys/cdefs.h> 56116182Sobrien__FBSDID("$FreeBSD: head/sys/kern/subr_rtc.c 227723 2011-11-19 14:10:16Z lstewart $"); 57116182Sobrien 58227723Slstewart#include "opt_ffclock.h" 59227723Slstewart 6093835Stmm#include <sys/param.h> 6193835Stmm#include <sys/systm.h> 6293835Stmm#include <sys/kernel.h> 6393835Stmm#include <sys/bus.h> 6493835Stmm#include <sys/clock.h> 6593835Stmm#include <sys/sysctl.h> 66227723Slstewart#ifdef FFCLOCK 67227723Slstewart#include <sys/timeffc.h> 68227723Slstewart#endif 6993835Stmm#include <sys/timetc.h> 7093835Stmm 7193835Stmm#include "clock_if.h" 7293835Stmm 7393835Stmmstatic device_t clock_dev = NULL; 7493835Stmmstatic long clock_res; 75211230Sjkimstatic struct timespec clock_adj; 7693835Stmm 77178429Sphk/* XXX: should be kern. now, it's no longer machdep. */ 78178429Sphkstatic int disable_rtc_set; 79211228SjkimSYSCTL_INT(_machdep, OID_AUTO, disable_rtc_set, CTLFLAG_RW, &disable_rtc_set, 80211228Sjkim 0, "Disallow adjusting time-of-day clock"); 81178429Sphk 8293835Stmmvoid 83158450Sphkclock_register(device_t dev, long res) /* res has units of microseconds */ 8493835Stmm{ 8593835Stmm 8693835Stmm if (clock_dev != NULL) { 8793835Stmm if (clock_res > res) { 88211228Sjkim if (bootverbose) 8993835Stmm device_printf(dev, "not installed as " 9093835Stmm "time-of-day clock: clock %s has higher " 9193835Stmm "resolution\n", device_get_name(clock_dev)); 9293835Stmm return; 9393835Stmm } 94211228Sjkim if (bootverbose) 95211228Sjkim device_printf(clock_dev, "removed as " 96211228Sjkim "time-of-day clock: clock %s has higher " 97211228Sjkim "resolution\n", device_get_name(dev)); 9893835Stmm } 9993835Stmm clock_dev = dev; 10093835Stmm clock_res = res; 101211230Sjkim clock_adj.tv_sec = res / 2 / 1000000; 102211230Sjkim clock_adj.tv_nsec = res / 2 % 1000000 * 1000; 103211228Sjkim if (bootverbose) 10493835Stmm device_printf(dev, "registered as a time-of-day clock " 105211230Sjkim "(resolution %ldus, adjustment %jd.%09jds)\n", res, 106211230Sjkim (intmax_t)clock_adj.tv_sec, (intmax_t)clock_adj.tv_nsec); 10793835Stmm} 10893835Stmm 10993835Stmm/* 11093835Stmm * inittodr and settodr derived from the i386 versions written 11193835Stmm * by Christoph Robitschko <chmr@edvz.tu-graz.ac.at>, reintroduced and 11293835Stmm * updated by Chris Stenton <chris@gnome.co.uk> 8/10/94 11393835Stmm */ 11493835Stmm 11593835Stmm/* 11693835Stmm * Initialize the time of day register, based on the time base which is, e.g. 11793835Stmm * from a filesystem. 11893835Stmm */ 11993835Stmmvoid 12093835Stmminittodr(time_t base) 12193835Stmm{ 122211228Sjkim struct timespec ts; 12393835Stmm int error; 12493835Stmm 12593835Stmm if (clock_dev == NULL) { 12693835Stmm printf("warning: no time-of-day clock registered, system time " 12793835Stmm "will not be set accurately\n"); 128190337Sjkim goto wrong_time; 12993835Stmm } 130178429Sphk /* XXX: We should poll all registered RTCs in case of failure */ 13193835Stmm error = CLOCK_GETTIME(clock_dev, &ts); 13293835Stmm if (error != 0 && error != EINVAL) { 13393835Stmm printf("warning: clock_gettime failed (%d), the system time " 13493835Stmm "will not be set accurately\n", error); 135190337Sjkim goto wrong_time; 13693835Stmm } 13793835Stmm if (error == EINVAL || ts.tv_sec < 0) { 138190337Sjkim printf("Invalid time in real time clock.\n" 139190337Sjkim "Check and reset the date immediately!\n"); 140190337Sjkim goto wrong_time; 14193835Stmm } 14293835Stmm 143162970Sphk ts.tv_sec += utc_offset(); 144211230Sjkim timespecadd(&ts, &clock_adj); 145190337Sjkim tc_setclock(&ts); 146227723Slstewart#ifdef FFCLOCK 147227723Slstewart ffclock_reset_clock(&ts); 148227723Slstewart#endif 149190337Sjkim return; 15093835Stmm 151190337Sjkimwrong_time: 152190337Sjkim if (base > 0) { 153211228Sjkim ts.tv_sec = base; 154211228Sjkim ts.tv_nsec = 0; 155211228Sjkim tc_setclock(&ts); 15693835Stmm } 15793835Stmm} 15893835Stmm 15993835Stmm/* 16093835Stmm * Write system time back to RTC 16193835Stmm */ 16293835Stmmvoid 163188055Simpresettodr(void) 16493835Stmm{ 16593835Stmm struct timespec ts; 16693835Stmm int error; 16793835Stmm 16893835Stmm if (disable_rtc_set || clock_dev == NULL) 16993835Stmm return; 17093835Stmm 17193835Stmm getnanotime(&ts); 172211230Sjkim timespecadd(&ts, &clock_adj); 173162970Sphk ts.tv_sec -= utc_offset(); 174178429Sphk /* XXX: We should really set all registered RTCs */ 175211228Sjkim if ((error = CLOCK_SETTIME(clock_dev, &ts)) != 0) 17693835Stmm printf("warning: clock_settime failed (%d), time-of-day clock " 17793835Stmm "not adjusted to system time\n", error); 17893835Stmm} 179