2005-01-06 23:35:40 +00:00
|
|
|
/*-
|
2002-04-04 23:39:10 +00:00
|
|
|
* Copyright (c) 1988 University of Utah.
|
|
|
|
* Copyright (c) 1982, 1990, 1993
|
2011-11-19 14:10:16 +00:00
|
|
|
* The Regents of the University of California.
|
|
|
|
* Copyright (c) 2011 The FreeBSD Foundation
|
|
|
|
* All rights reserved.
|
2002-04-04 23:39:10 +00:00
|
|
|
*
|
|
|
|
* This code is derived from software contributed to Berkeley by
|
|
|
|
* the Systems Programming Group of the University of Utah Computer
|
|
|
|
* Science Department.
|
|
|
|
*
|
2011-11-19 14:10:16 +00:00
|
|
|
* Portions of this software were developed by Julien Ridoux at the University
|
|
|
|
* of Melbourne under sponsorship from the FreeBSD Foundation.
|
|
|
|
*
|
2002-04-04 23:39:10 +00:00
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 4. Neither the name of the University nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* from: Utah $Hdr: clock.c 1.18 91/01/21$
|
|
|
|
* from: @(#)clock.c 8.2 (Berkeley) 1/12/94
|
|
|
|
* from: NetBSD: clock_subr.c,v 1.6 2001/07/07 17:04:02 thorpej Exp
|
|
|
|
* and
|
|
|
|
* from: src/sys/i386/isa/clock.c,v 1.176 2001/09/04
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Helpers for time-of-day clocks. This is useful for architectures that need
|
|
|
|
* support multiple models of such clocks, and generally serves to make the
|
|
|
|
* code more machine-independent.
|
|
|
|
* If the clock in question can also be used as a time counter, the driver
|
|
|
|
* needs to initiate this.
|
|
|
|
* This code is not yet used by all architectures.
|
|
|
|
*/
|
|
|
|
|
2003-06-11 00:56:59 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
2011-11-19 14:10:16 +00:00
|
|
|
#include "opt_ffclock.h"
|
|
|
|
|
2002-04-04 23:39:10 +00:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/systm.h>
|
|
|
|
#include <sys/kernel.h>
|
|
|
|
#include <sys/bus.h>
|
|
|
|
#include <sys/clock.h>
|
|
|
|
#include <sys/sysctl.h>
|
2011-11-19 14:10:16 +00:00
|
|
|
#ifdef FFCLOCK
|
|
|
|
#include <sys/timeffc.h>
|
|
|
|
#endif
|
2002-04-04 23:39:10 +00:00
|
|
|
#include <sys/timetc.h>
|
|
|
|
|
|
|
|
#include "clock_if.h"
|
|
|
|
|
|
|
|
static device_t clock_dev = NULL;
|
|
|
|
static long clock_res;
|
2010-08-12 17:17:05 +00:00
|
|
|
static struct timespec clock_adj;
|
2002-04-04 23:39:10 +00:00
|
|
|
|
Now that all platforms use genclock, shuffle things around slightly
for better structure.
Much of this is related to <sys/clock.h>, which should really have
been called <sys/calendar.h>, but unless and until we need the name,
the repocopy can wait.
In general the kernel does not know about minutes, hours, days,
timezones, daylight savings time, leap-years and such. All that
is theoretically a matter for userland only.
Parts of kernel code does however care: badly designed filesystems
store timestamps in local time and RTC chips almost universally
track time in a YY-MM-DD HH:MM:SS format, and sometimes in local
timezone instead of UTC. For this we have <sys/clock.h>
<sys/time.h> on the other hand, deals with time_t, timeval, timespec
and so on. These know only seconds and fractions thereof.
Move inittodr() and resettodr() prototypes to <sys/time.h>.
Retain the names as it is one of the few surviving PDP/VAX references.
Move startrtclock() to <machine/clock.h> on relevant platforms, it
is a MD call between machdep.c/clock.c. Remove references to it
elsewhere.
Remove a lot of unnecessary <sys/clock.h> includes.
Move the machdep.disable_rtc_set sysctl to subr_rtc.c where it belongs.
XXX: should be kern.disable_rtc_set really, it's not MD.
2008-04-22 19:38:30 +00:00
|
|
|
/* XXX: should be kern. now, it's no longer machdep. */
|
|
|
|
static int disable_rtc_set;
|
2010-08-12 16:13:24 +00:00
|
|
|
SYSCTL_INT(_machdep, OID_AUTO, disable_rtc_set, CTLFLAG_RW, &disable_rtc_set,
|
|
|
|
0, "Disallow adjusting time-of-day clock");
|
Now that all platforms use genclock, shuffle things around slightly
for better structure.
Much of this is related to <sys/clock.h>, which should really have
been called <sys/calendar.h>, but unless and until we need the name,
the repocopy can wait.
In general the kernel does not know about minutes, hours, days,
timezones, daylight savings time, leap-years and such. All that
is theoretically a matter for userland only.
Parts of kernel code does however care: badly designed filesystems
store timestamps in local time and RTC chips almost universally
track time in a YY-MM-DD HH:MM:SS format, and sometimes in local
timezone instead of UTC. For this we have <sys/clock.h>
<sys/time.h> on the other hand, deals with time_t, timeval, timespec
and so on. These know only seconds and fractions thereof.
Move inittodr() and resettodr() prototypes to <sys/time.h>.
Retain the names as it is one of the few surviving PDP/VAX references.
Move startrtclock() to <machine/clock.h> on relevant platforms, it
is a MD call between machdep.c/clock.c. Remove references to it
elsewhere.
Remove a lot of unnecessary <sys/clock.h> includes.
Move the machdep.disable_rtc_set sysctl to subr_rtc.c where it belongs.
XXX: should be kern.disable_rtc_set really, it's not MD.
2008-04-22 19:38:30 +00:00
|
|
|
|
2002-04-04 23:39:10 +00:00
|
|
|
void
|
2006-05-11 17:53:26 +00:00
|
|
|
clock_register(device_t dev, long res) /* res has units of microseconds */
|
2002-04-04 23:39:10 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
if (clock_dev != NULL) {
|
|
|
|
if (clock_res > res) {
|
2010-08-12 16:13:24 +00:00
|
|
|
if (bootverbose)
|
2002-04-04 23:39:10 +00:00
|
|
|
device_printf(dev, "not installed as "
|
|
|
|
"time-of-day clock: clock %s has higher "
|
|
|
|
"resolution\n", device_get_name(clock_dev));
|
|
|
|
return;
|
|
|
|
}
|
2010-08-12 16:13:24 +00:00
|
|
|
if (bootverbose)
|
|
|
|
device_printf(clock_dev, "removed as "
|
|
|
|
"time-of-day clock: clock %s has higher "
|
|
|
|
"resolution\n", device_get_name(dev));
|
2002-04-04 23:39:10 +00:00
|
|
|
}
|
|
|
|
clock_dev = dev;
|
|
|
|
clock_res = res;
|
2010-08-12 17:17:05 +00:00
|
|
|
clock_adj.tv_sec = res / 2 / 1000000;
|
|
|
|
clock_adj.tv_nsec = res / 2 % 1000000 * 1000;
|
2010-08-12 16:13:24 +00:00
|
|
|
if (bootverbose)
|
2002-04-04 23:39:10 +00:00
|
|
|
device_printf(dev, "registered as a time-of-day clock "
|
2010-08-12 17:17:05 +00:00
|
|
|
"(resolution %ldus, adjustment %jd.%09jds)\n", res,
|
|
|
|
(intmax_t)clock_adj.tv_sec, (intmax_t)clock_adj.tv_nsec);
|
2002-04-04 23:39:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* inittodr and settodr derived from the i386 versions written
|
|
|
|
* by Christoph Robitschko <chmr@edvz.tu-graz.ac.at>, reintroduced and
|
|
|
|
* updated by Chris Stenton <chris@gnome.co.uk> 8/10/94
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Initialize the time of day register, based on the time base which is, e.g.
|
|
|
|
* from a filesystem.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
inittodr(time_t base)
|
|
|
|
{
|
2010-08-12 16:13:24 +00:00
|
|
|
struct timespec ts;
|
2002-04-04 23:39:10 +00:00
|
|
|
int error;
|
|
|
|
|
|
|
|
if (clock_dev == NULL) {
|
|
|
|
printf("warning: no time-of-day clock registered, system time "
|
|
|
|
"will not be set accurately\n");
|
2009-03-23 21:16:21 +00:00
|
|
|
goto wrong_time;
|
2002-04-04 23:39:10 +00:00
|
|
|
}
|
Now that all platforms use genclock, shuffle things around slightly
for better structure.
Much of this is related to <sys/clock.h>, which should really have
been called <sys/calendar.h>, but unless and until we need the name,
the repocopy can wait.
In general the kernel does not know about minutes, hours, days,
timezones, daylight savings time, leap-years and such. All that
is theoretically a matter for userland only.
Parts of kernel code does however care: badly designed filesystems
store timestamps in local time and RTC chips almost universally
track time in a YY-MM-DD HH:MM:SS format, and sometimes in local
timezone instead of UTC. For this we have <sys/clock.h>
<sys/time.h> on the other hand, deals with time_t, timeval, timespec
and so on. These know only seconds and fractions thereof.
Move inittodr() and resettodr() prototypes to <sys/time.h>.
Retain the names as it is one of the few surviving PDP/VAX references.
Move startrtclock() to <machine/clock.h> on relevant platforms, it
is a MD call between machdep.c/clock.c. Remove references to it
elsewhere.
Remove a lot of unnecessary <sys/clock.h> includes.
Move the machdep.disable_rtc_set sysctl to subr_rtc.c where it belongs.
XXX: should be kern.disable_rtc_set really, it's not MD.
2008-04-22 19:38:30 +00:00
|
|
|
/* XXX: We should poll all registered RTCs in case of failure */
|
2002-04-04 23:39:10 +00:00
|
|
|
error = CLOCK_GETTIME(clock_dev, &ts);
|
|
|
|
if (error != 0 && error != EINVAL) {
|
|
|
|
printf("warning: clock_gettime failed (%d), the system time "
|
|
|
|
"will not be set accurately\n", error);
|
2009-03-23 21:16:21 +00:00
|
|
|
goto wrong_time;
|
2002-04-04 23:39:10 +00:00
|
|
|
}
|
|
|
|
if (error == EINVAL || ts.tv_sec < 0) {
|
2009-03-23 21:16:21 +00:00
|
|
|
printf("Invalid time in real time clock.\n"
|
|
|
|
"Check and reset the date immediately!\n");
|
|
|
|
goto wrong_time;
|
2002-04-04 23:39:10 +00:00
|
|
|
}
|
|
|
|
|
2006-10-02 18:23:37 +00:00
|
|
|
ts.tv_sec += utc_offset();
|
2010-08-12 17:17:05 +00:00
|
|
|
timespecadd(&ts, &clock_adj);
|
2009-03-23 21:16:21 +00:00
|
|
|
tc_setclock(&ts);
|
2011-11-19 14:10:16 +00:00
|
|
|
#ifdef FFCLOCK
|
|
|
|
ffclock_reset_clock(&ts);
|
|
|
|
#endif
|
2009-03-23 21:16:21 +00:00
|
|
|
return;
|
2002-04-04 23:39:10 +00:00
|
|
|
|
2009-03-23 21:16:21 +00:00
|
|
|
wrong_time:
|
|
|
|
if (base > 0) {
|
2010-08-12 16:13:24 +00:00
|
|
|
ts.tv_sec = base;
|
|
|
|
ts.tv_nsec = 0;
|
|
|
|
tc_setclock(&ts);
|
2002-04-04 23:39:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Write system time back to RTC
|
|
|
|
*/
|
|
|
|
void
|
2009-02-03 07:50:01 +00:00
|
|
|
resettodr(void)
|
2002-04-04 23:39:10 +00:00
|
|
|
{
|
|
|
|
struct timespec ts;
|
|
|
|
int error;
|
|
|
|
|
|
|
|
if (disable_rtc_set || clock_dev == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
getnanotime(&ts);
|
2010-08-12 17:17:05 +00:00
|
|
|
timespecadd(&ts, &clock_adj);
|
2006-10-02 18:23:37 +00:00
|
|
|
ts.tv_sec -= utc_offset();
|
Now that all platforms use genclock, shuffle things around slightly
for better structure.
Much of this is related to <sys/clock.h>, which should really have
been called <sys/calendar.h>, but unless and until we need the name,
the repocopy can wait.
In general the kernel does not know about minutes, hours, days,
timezones, daylight savings time, leap-years and such. All that
is theoretically a matter for userland only.
Parts of kernel code does however care: badly designed filesystems
store timestamps in local time and RTC chips almost universally
track time in a YY-MM-DD HH:MM:SS format, and sometimes in local
timezone instead of UTC. For this we have <sys/clock.h>
<sys/time.h> on the other hand, deals with time_t, timeval, timespec
and so on. These know only seconds and fractions thereof.
Move inittodr() and resettodr() prototypes to <sys/time.h>.
Retain the names as it is one of the few surviving PDP/VAX references.
Move startrtclock() to <machine/clock.h> on relevant platforms, it
is a MD call between machdep.c/clock.c. Remove references to it
elsewhere.
Remove a lot of unnecessary <sys/clock.h> includes.
Move the machdep.disable_rtc_set sysctl to subr_rtc.c where it belongs.
XXX: should be kern.disable_rtc_set really, it's not MD.
2008-04-22 19:38:30 +00:00
|
|
|
/* XXX: We should really set all registered RTCs */
|
2010-08-12 16:13:24 +00:00
|
|
|
if ((error = CLOCK_SETTIME(clock_dev, &ts)) != 0)
|
2002-04-04 23:39:10 +00:00
|
|
|
printf("warning: clock_settime failed (%d), time-of-day clock "
|
|
|
|
"not adjusted to system time\n", error);
|
|
|
|
}
|