xref: /freebsd/sys/kern/subr_rtc.c (revision b0fdc837133b8b1d9a033c4a4b45ccc6f436d396)
19454b2d8SWarner Losh /*-
2d7f7792eSThomas Moestl  * Copyright (c) 1988 University of Utah.
3d7f7792eSThomas Moestl  * Copyright (c) 1982, 1990, 1993
4*b0fdc837SLawrence Stewart  *	The Regents of the University of California.
5*b0fdc837SLawrence Stewart  * Copyright (c) 2011 The FreeBSD Foundation
6*b0fdc837SLawrence 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  *
12*b0fdc837SLawrence Stewart  * Portions of this software were developed by Julien Ridoux at the University
13*b0fdc837SLawrence Stewart  * of Melbourne under sponsorship from the FreeBSD Foundation.
14*b0fdc837SLawrence 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.
23d7f7792eSThomas Moestl  * 4. 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 
58*b0fdc837SLawrence Stewart #include "opt_ffclock.h"
59*b0fdc837SLawrence 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>
65d7f7792eSThomas Moestl #include <sys/sysctl.h>
66*b0fdc837SLawrence Stewart #ifdef FFCLOCK
67*b0fdc837SLawrence Stewart #include <sys/timeffc.h>
68*b0fdc837SLawrence Stewart #endif
69d7f7792eSThomas Moestl #include <sys/timetc.h>
70d7f7792eSThomas Moestl 
71d7f7792eSThomas Moestl #include "clock_if.h"
72d7f7792eSThomas Moestl 
73d7f7792eSThomas Moestl static device_t clock_dev = NULL;
74d7f7792eSThomas Moestl static long clock_res;
75116a77bdSJung-uk Kim static struct timespec clock_adj;
76d7f7792eSThomas Moestl 
779b4a8ab7SPoul-Henning Kamp /* XXX: should be kern. now, it's no longer machdep.  */
789b4a8ab7SPoul-Henning Kamp static int disable_rtc_set;
790674ea6fSJung-uk Kim SYSCTL_INT(_machdep, OID_AUTO, disable_rtc_set, CTLFLAG_RW, &disable_rtc_set,
800674ea6fSJung-uk Kim     0, "Disallow adjusting time-of-day clock");
819b4a8ab7SPoul-Henning Kamp 
82d7f7792eSThomas Moestl void
8399ab8292SPoul-Henning Kamp clock_register(device_t dev, long res)	/* res has units of microseconds */
84d7f7792eSThomas Moestl {
85d7f7792eSThomas Moestl 
86d7f7792eSThomas Moestl 	if (clock_dev != NULL) {
87d7f7792eSThomas Moestl 		if (clock_res > res) {
880674ea6fSJung-uk Kim 			if (bootverbose)
89d7f7792eSThomas Moestl 				device_printf(dev, "not installed as "
90d7f7792eSThomas Moestl 				    "time-of-day clock: clock %s has higher "
91d7f7792eSThomas Moestl 				    "resolution\n", device_get_name(clock_dev));
92d7f7792eSThomas Moestl 			return;
930674ea6fSJung-uk Kim 		}
940674ea6fSJung-uk Kim 		if (bootverbose)
95d7f7792eSThomas Moestl 			device_printf(clock_dev, "removed as "
96d7f7792eSThomas Moestl 			    "time-of-day clock: clock %s has higher "
97d7f7792eSThomas Moestl 			    "resolution\n", device_get_name(dev));
98d7f7792eSThomas Moestl 	}
99d7f7792eSThomas Moestl 	clock_dev = dev;
100d7f7792eSThomas Moestl 	clock_res = res;
101116a77bdSJung-uk Kim 	clock_adj.tv_sec = res / 2 / 1000000;
102116a77bdSJung-uk Kim 	clock_adj.tv_nsec = res / 2 % 1000000 * 1000;
1030674ea6fSJung-uk Kim 	if (bootverbose)
104d7f7792eSThomas Moestl 		device_printf(dev, "registered as a time-of-day clock "
105116a77bdSJung-uk Kim 		    "(resolution %ldus, adjustment %jd.%09jds)\n", res,
106116a77bdSJung-uk Kim 		    (intmax_t)clock_adj.tv_sec, (intmax_t)clock_adj.tv_nsec);
107d7f7792eSThomas Moestl }
108d7f7792eSThomas Moestl 
109d7f7792eSThomas Moestl /*
110d7f7792eSThomas Moestl  * inittodr and settodr derived from the i386 versions written
111d7f7792eSThomas Moestl  * by Christoph Robitschko <chmr@edvz.tu-graz.ac.at>,  reintroduced and
112d7f7792eSThomas Moestl  * updated by Chris Stenton <chris@gnome.co.uk> 8/10/94
113d7f7792eSThomas Moestl  */
114d7f7792eSThomas Moestl 
115d7f7792eSThomas Moestl /*
116d7f7792eSThomas Moestl  * Initialize the time of day register, based on the time base which is, e.g.
117d7f7792eSThomas Moestl  * from a filesystem.
118d7f7792eSThomas Moestl  */
119d7f7792eSThomas Moestl void
120d7f7792eSThomas Moestl inittodr(time_t base)
121d7f7792eSThomas Moestl {
1220674ea6fSJung-uk Kim 	struct timespec ts;
123d7f7792eSThomas Moestl 	int error;
124d7f7792eSThomas Moestl 
125d7f7792eSThomas Moestl 	if (clock_dev == NULL) {
126d7f7792eSThomas Moestl 		printf("warning: no time-of-day clock registered, system time "
127d7f7792eSThomas Moestl 		    "will not be set accurately\n");
128eae44ae0SJung-uk Kim 		goto wrong_time;
129d7f7792eSThomas Moestl 	}
1309b4a8ab7SPoul-Henning Kamp 	/* XXX: We should poll all registered RTCs in case of failure */
131d7f7792eSThomas Moestl 	error = CLOCK_GETTIME(clock_dev, &ts);
132d7f7792eSThomas Moestl 	if (error != 0 && error != EINVAL) {
133d7f7792eSThomas Moestl 		printf("warning: clock_gettime failed (%d), the system time "
134d7f7792eSThomas Moestl 		    "will not be set accurately\n", error);
135eae44ae0SJung-uk Kim 		goto wrong_time;
136d7f7792eSThomas Moestl 	}
137d7f7792eSThomas Moestl 	if (error == EINVAL || ts.tv_sec < 0) {
138eae44ae0SJung-uk Kim 		printf("Invalid time in real time clock.\n"
139eae44ae0SJung-uk Kim 		    "Check and reset the date immediately!\n");
140eae44ae0SJung-uk Kim 		goto wrong_time;
141d7f7792eSThomas Moestl 	}
142d7f7792eSThomas Moestl 
143e5037a18SPoul-Henning Kamp 	ts.tv_sec += utc_offset();
144116a77bdSJung-uk Kim 	timespecadd(&ts, &clock_adj);
145d7f7792eSThomas Moestl 	tc_setclock(&ts);
146*b0fdc837SLawrence Stewart #ifdef FFCLOCK
147*b0fdc837SLawrence Stewart 	ffclock_reset_clock(&ts);
148*b0fdc837SLawrence Stewart #endif
149eae44ae0SJung-uk Kim 	return;
150eae44ae0SJung-uk Kim 
151eae44ae0SJung-uk Kim wrong_time:
152eae44ae0SJung-uk Kim 	if (base > 0) {
1530674ea6fSJung-uk Kim 		ts.tv_sec = base;
1540674ea6fSJung-uk Kim 		ts.tv_nsec = 0;
1550674ea6fSJung-uk Kim 		tc_setclock(&ts);
156d7f7792eSThomas Moestl 	}
157d7f7792eSThomas Moestl }
158d7f7792eSThomas Moestl 
159d7f7792eSThomas Moestl /*
160d7f7792eSThomas Moestl  * Write system time back to RTC
161d7f7792eSThomas Moestl  */
162d7f7792eSThomas Moestl void
1639483543dSWarner Losh resettodr(void)
164d7f7792eSThomas Moestl {
165d7f7792eSThomas Moestl 	struct timespec ts;
166d7f7792eSThomas Moestl 	int error;
167d7f7792eSThomas Moestl 
168d7f7792eSThomas Moestl 	if (disable_rtc_set || clock_dev == NULL)
169d7f7792eSThomas Moestl 		return;
170d7f7792eSThomas Moestl 
171d7f7792eSThomas Moestl 	getnanotime(&ts);
172116a77bdSJung-uk Kim 	timespecadd(&ts, &clock_adj);
173e5037a18SPoul-Henning Kamp 	ts.tv_sec -= utc_offset();
1749b4a8ab7SPoul-Henning Kamp 	/* XXX: We should really set all registered RTCs */
1750674ea6fSJung-uk Kim 	if ((error = CLOCK_SETTIME(clock_dev, &ts)) != 0)
176d7f7792eSThomas Moestl 		printf("warning: clock_settime failed (%d), time-of-day clock "
177d7f7792eSThomas Moestl 		    "not adjusted to system time\n", error);
178d7f7792eSThomas Moestl }
179