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