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