subr_rtc.c revision 211228
1139804Simp/*- 293835Stmm * Copyright (c) 1988 University of Utah. 393835Stmm * Copyright (c) 1982, 1990, 1993 493835Stmm * The Regents of the University of California. All rights reserved. 593835Stmm * 693835Stmm * This code is derived from software contributed to Berkeley by 793835Stmm * the Systems Programming Group of the University of Utah Computer 893835Stmm * Science Department. 993835Stmm * 1093835Stmm * Redistribution and use in source and binary forms, with or without 1193835Stmm * modification, are permitted provided that the following conditions 1293835Stmm * are met: 1393835Stmm * 1. Redistributions of source code must retain the above copyright 1493835Stmm * notice, this list of conditions and the following disclaimer. 1593835Stmm * 2. Redistributions in binary form must reproduce the above copyright 1693835Stmm * notice, this list of conditions and the following disclaimer in the 1793835Stmm * documentation and/or other materials provided with the distribution. 1893835Stmm * 4. Neither the name of the University nor the names of its contributors 1993835Stmm * may be used to endorse or promote products derived from this software 2093835Stmm * without specific prior written permission. 2193835Stmm * 2293835Stmm * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 2393835Stmm * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2493835Stmm * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2593835Stmm * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 2693835Stmm * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2793835Stmm * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2893835Stmm * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2993835Stmm * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 3093835Stmm * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 3193835Stmm * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3293835Stmm * SUCH DAMAGE. 3393835Stmm * 3493835Stmm * from: Utah $Hdr: clock.c 1.18 91/01/21$ 3593835Stmm * from: @(#)clock.c 8.2 (Berkeley) 1/12/94 3693835Stmm * from: NetBSD: clock_subr.c,v 1.6 2001/07/07 17:04:02 thorpej Exp 3793835Stmm * and 3893835Stmm * from: src/sys/i386/isa/clock.c,v 1.176 2001/09/04 3993835Stmm */ 4093835Stmm 4193835Stmm/* 4293835Stmm * Helpers for time-of-day clocks. This is useful for architectures that need 4393835Stmm * support multiple models of such clocks, and generally serves to make the 4493835Stmm * code more machine-independent. 4593835Stmm * If the clock in question can also be used as a time counter, the driver 4693835Stmm * needs to initiate this. 4793835Stmm * This code is not yet used by all architectures. 4893835Stmm */ 4993835Stmm 50116182Sobrien#include <sys/cdefs.h> 51116182Sobrien__FBSDID("$FreeBSD: head/sys/kern/subr_rtc.c 211228 2010-08-12 16:13:24Z jkim $"); 52116182Sobrien 5393835Stmm#include <sys/param.h> 5493835Stmm#include <sys/systm.h> 5593835Stmm#include <sys/kernel.h> 5693835Stmm#include <sys/bus.h> 5793835Stmm#include <sys/clock.h> 5893835Stmm#include <sys/sysctl.h> 5993835Stmm#include <sys/timetc.h> 6093835Stmm 6193835Stmm#include "clock_if.h" 6293835Stmm 6393835Stmmstatic device_t clock_dev = NULL; 6493835Stmmstatic long clock_res; 6593835Stmm 66178429Sphk/* XXX: should be kern. now, it's no longer machdep. */ 67178429Sphkstatic int disable_rtc_set; 68211228SjkimSYSCTL_INT(_machdep, OID_AUTO, disable_rtc_set, CTLFLAG_RW, &disable_rtc_set, 69211228Sjkim 0, "Disallow adjusting time-of-day clock"); 70178429Sphk 7193835Stmmvoid 72158450Sphkclock_register(device_t dev, long res) /* res has units of microseconds */ 7393835Stmm{ 7493835Stmm 7593835Stmm if (clock_dev != NULL) { 7693835Stmm if (clock_res > res) { 77211228Sjkim if (bootverbose) 7893835Stmm device_printf(dev, "not installed as " 7993835Stmm "time-of-day clock: clock %s has higher " 8093835Stmm "resolution\n", device_get_name(clock_dev)); 8193835Stmm return; 8293835Stmm } 83211228Sjkim if (bootverbose) 84211228Sjkim device_printf(clock_dev, "removed as " 85211228Sjkim "time-of-day clock: clock %s has higher " 86211228Sjkim "resolution\n", device_get_name(dev)); 8793835Stmm } 8893835Stmm clock_dev = dev; 8993835Stmm clock_res = res; 90211228Sjkim if (bootverbose) 9193835Stmm device_printf(dev, "registered as a time-of-day clock " 9293835Stmm "(resolution %ldus)\n", res); 9393835Stmm} 9493835Stmm 9593835Stmm/* 9693835Stmm * inittodr and settodr derived from the i386 versions written 9793835Stmm * by Christoph Robitschko <chmr@edvz.tu-graz.ac.at>, reintroduced and 9893835Stmm * updated by Chris Stenton <chris@gnome.co.uk> 8/10/94 9993835Stmm */ 10093835Stmm 10193835Stmm/* 10293835Stmm * Initialize the time of day register, based on the time base which is, e.g. 10393835Stmm * from a filesystem. 10493835Stmm */ 10593835Stmmvoid 10693835Stmminittodr(time_t base) 10793835Stmm{ 108211228Sjkim struct timespec ts; 10993835Stmm int error; 11093835Stmm 11193835Stmm if (clock_dev == NULL) { 11293835Stmm printf("warning: no time-of-day clock registered, system time " 11393835Stmm "will not be set accurately\n"); 114190337Sjkim goto wrong_time; 11593835Stmm } 116178429Sphk /* XXX: We should poll all registered RTCs in case of failure */ 11793835Stmm error = CLOCK_GETTIME(clock_dev, &ts); 11893835Stmm if (error != 0 && error != EINVAL) { 11993835Stmm printf("warning: clock_gettime failed (%d), the system time " 12093835Stmm "will not be set accurately\n", error); 121190337Sjkim goto wrong_time; 12293835Stmm } 12393835Stmm if (error == EINVAL || ts.tv_sec < 0) { 124190337Sjkim printf("Invalid time in real time clock.\n" 125190337Sjkim "Check and reset the date immediately!\n"); 126190337Sjkim goto wrong_time; 12793835Stmm } 12893835Stmm 129162970Sphk ts.tv_sec += utc_offset(); 130190337Sjkim tc_setclock(&ts); 131190337Sjkim return; 13293835Stmm 133190337Sjkimwrong_time: 134190337Sjkim if (base > 0) { 135211228Sjkim ts.tv_sec = base; 136211228Sjkim ts.tv_nsec = 0; 137211228Sjkim tc_setclock(&ts); 13893835Stmm } 13993835Stmm} 14093835Stmm 14193835Stmm/* 14293835Stmm * Write system time back to RTC 14393835Stmm */ 14493835Stmmvoid 145188055Simpresettodr(void) 14693835Stmm{ 14793835Stmm struct timespec ts; 14893835Stmm int error; 14993835Stmm 15093835Stmm if (disable_rtc_set || clock_dev == NULL) 15193835Stmm return; 15293835Stmm 15393835Stmm getnanotime(&ts); 154162970Sphk ts.tv_sec -= utc_offset(); 155178429Sphk /* XXX: We should really set all registered RTCs */ 156211228Sjkim if ((error = CLOCK_SETTIME(clock_dev, &ts)) != 0) 15793835Stmm printf("warning: clock_settime failed (%d), time-of-day clock " 15893835Stmm "not adjusted to system time\n", error); 15993835Stmm} 160