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