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: releng/11.0/sys/kern/subr_rtc.c 302251 2016-06-28 16:42:40Z kib $"); 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> 65302251Skib#include <sys/lock.h> 66302251Skib#include <sys/mutex.h> 6793835Stmm#include <sys/sysctl.h> 68227723Slstewart#ifdef FFCLOCK 69227723Slstewart#include <sys/timeffc.h> 70227723Slstewart#endif 7193835Stmm#include <sys/timetc.h> 7293835Stmm 7393835Stmm#include "clock_if.h" 7493835Stmm 7593835Stmmstatic device_t clock_dev = NULL; 7693835Stmmstatic long clock_res; 77211230Sjkimstatic struct timespec clock_adj; 78302251Skibstatic struct mtx resettodr_lock; 79302251SkibMTX_SYSINIT(resettodr_init, &resettodr_lock, "tod2rl", MTX_DEF); 8093835Stmm 81178429Sphk/* XXX: should be kern. now, it's no longer machdep. */ 82178429Sphkstatic int disable_rtc_set; 83211228SjkimSYSCTL_INT(_machdep, OID_AUTO, disable_rtc_set, CTLFLAG_RW, &disable_rtc_set, 84211228Sjkim 0, "Disallow adjusting time-of-day clock"); 85178429Sphk 8693835Stmmvoid 87158450Sphkclock_register(device_t dev, long res) /* res has units of microseconds */ 8893835Stmm{ 8993835Stmm 9093835Stmm if (clock_dev != NULL) { 91299064Sroyger if (clock_res <= res) { 92211228Sjkim if (bootverbose) 9393835Stmm device_printf(dev, "not installed as " 9493835Stmm "time-of-day clock: clock %s has higher " 9593835Stmm "resolution\n", device_get_name(clock_dev)); 9693835Stmm return; 9793835Stmm } 98211228Sjkim if (bootverbose) 99211228Sjkim device_printf(clock_dev, "removed as " 100211228Sjkim "time-of-day clock: clock %s has higher " 101211228Sjkim "resolution\n", device_get_name(dev)); 10293835Stmm } 10393835Stmm clock_dev = dev; 10493835Stmm clock_res = res; 105211230Sjkim clock_adj.tv_sec = res / 2 / 1000000; 106211230Sjkim clock_adj.tv_nsec = res / 2 % 1000000 * 1000; 107211228Sjkim if (bootverbose) 10893835Stmm device_printf(dev, "registered as a time-of-day clock " 109211230Sjkim "(resolution %ldus, adjustment %jd.%09jds)\n", res, 110211230Sjkim (intmax_t)clock_adj.tv_sec, (intmax_t)clock_adj.tv_nsec); 11193835Stmm} 11293835Stmm 11393835Stmm/* 11493835Stmm * inittodr and settodr derived from the i386 versions written 11593835Stmm * by Christoph Robitschko <chmr@edvz.tu-graz.ac.at>, reintroduced and 11693835Stmm * updated by Chris Stenton <chris@gnome.co.uk> 8/10/94 11793835Stmm */ 11893835Stmm 11993835Stmm/* 12093835Stmm * Initialize the time of day register, based on the time base which is, e.g. 12193835Stmm * from a filesystem. 12293835Stmm */ 12393835Stmmvoid 12493835Stmminittodr(time_t base) 12593835Stmm{ 126211228Sjkim struct timespec ts; 12793835Stmm int error; 12893835Stmm 12993835Stmm if (clock_dev == NULL) { 13093835Stmm printf("warning: no time-of-day clock registered, system time " 13193835Stmm "will not be set accurately\n"); 132190337Sjkim goto wrong_time; 13393835Stmm } 134178429Sphk /* XXX: We should poll all registered RTCs in case of failure */ 13593835Stmm error = CLOCK_GETTIME(clock_dev, &ts); 13693835Stmm if (error != 0 && error != EINVAL) { 13793835Stmm printf("warning: clock_gettime failed (%d), the system time " 13893835Stmm "will not be set accurately\n", error); 139190337Sjkim goto wrong_time; 14093835Stmm } 14193835Stmm if (error == EINVAL || ts.tv_sec < 0) { 142190337Sjkim printf("Invalid time in real time clock.\n" 143190337Sjkim "Check and reset the date immediately!\n"); 144190337Sjkim goto wrong_time; 14593835Stmm } 14693835Stmm 147162970Sphk ts.tv_sec += utc_offset(); 148211230Sjkim timespecadd(&ts, &clock_adj); 149190337Sjkim tc_setclock(&ts); 150227723Slstewart#ifdef FFCLOCK 151227723Slstewart ffclock_reset_clock(&ts); 152227723Slstewart#endif 153190337Sjkim return; 15493835Stmm 155190337Sjkimwrong_time: 156190337Sjkim if (base > 0) { 157211228Sjkim ts.tv_sec = base; 158211228Sjkim ts.tv_nsec = 0; 159211228Sjkim tc_setclock(&ts); 16093835Stmm } 16193835Stmm} 16293835Stmm 16393835Stmm/* 16493835Stmm * Write system time back to RTC 16593835Stmm */ 16693835Stmmvoid 167188055Simpresettodr(void) 16893835Stmm{ 16993835Stmm struct timespec ts; 17093835Stmm int error; 17193835Stmm 17293835Stmm if (disable_rtc_set || clock_dev == NULL) 17393835Stmm return; 17493835Stmm 175302251Skib mtx_lock(&resettodr_lock); 17693835Stmm getnanotime(&ts); 177211230Sjkim timespecadd(&ts, &clock_adj); 178162970Sphk ts.tv_sec -= utc_offset(); 179178429Sphk /* XXX: We should really set all registered RTCs */ 180302251Skib error = CLOCK_SETTIME(clock_dev, &ts); 181302251Skib mtx_unlock(&resettodr_lock); 182302251Skib if (error != 0) 18393835Stmm printf("warning: clock_settime failed (%d), time-of-day clock " 18493835Stmm "not adjusted to system time\n", error); 18593835Stmm} 186