xref: /freebsd/sys/kern/subr_rtc.c (revision 9b4a8ab7bad97bcb374ab6498fe8a3ff50f260d4)
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;
65d7f7792eSThomas Moestl 
669b4a8ab7SPoul-Henning Kamp /* XXX: should be kern. now, it's no longer machdep.  */
679b4a8ab7SPoul-Henning Kamp static int disable_rtc_set;
689b4a8ab7SPoul-Henning Kamp SYSCTL_INT(_machdep, OID_AUTO, disable_rtc_set,
699b4a8ab7SPoul-Henning Kamp 	CTLFLAG_RW, &disable_rtc_set, 0, "");
709b4a8ab7SPoul-Henning Kamp 
71d7f7792eSThomas Moestl void
7299ab8292SPoul-Henning Kamp clock_register(device_t dev, long res)	/* res has units of microseconds */
73d7f7792eSThomas Moestl {
74d7f7792eSThomas Moestl 
75d7f7792eSThomas Moestl 	if (clock_dev != NULL) {
76d7f7792eSThomas Moestl 		if (clock_res > res) {
77d7f7792eSThomas Moestl 			if (bootverbose) {
78d7f7792eSThomas Moestl 				device_printf(dev, "not installed as "
79d7f7792eSThomas Moestl 				    "time-of-day clock: clock %s has higher "
80d7f7792eSThomas Moestl 				    "resolution\n", device_get_name(clock_dev));
81d7f7792eSThomas Moestl 			}
82d7f7792eSThomas Moestl 			return;
83d7f7792eSThomas Moestl 		} else {
84d7f7792eSThomas Moestl 			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 		}
90d7f7792eSThomas Moestl 	}
91d7f7792eSThomas Moestl 	clock_dev = dev;
92d7f7792eSThomas Moestl 	clock_res = res;
93d7f7792eSThomas Moestl 	if (bootverbose) {
94d7f7792eSThomas Moestl 		device_printf(dev, "registered as a time-of-day clock "
95d7f7792eSThomas Moestl 		    "(resolution %ldus)\n", res);
96d7f7792eSThomas Moestl 	}
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 {
112d7f7792eSThomas Moestl 	struct timespec diff, ref, ts;
113d7f7792eSThomas Moestl 	int error;
114d7f7792eSThomas Moestl 
115d7f7792eSThomas Moestl 	if (base) {
116d7f7792eSThomas Moestl 		ref.tv_sec = base;
117d7f7792eSThomas Moestl 		ref.tv_nsec = 0;
118d7f7792eSThomas Moestl 		tc_setclock(&ref);
119d7f7792eSThomas Moestl 	}
120d7f7792eSThomas Moestl 
121d7f7792eSThomas Moestl 	if (clock_dev == NULL) {
122d7f7792eSThomas Moestl 		printf("warning: no time-of-day clock registered, system time "
123d7f7792eSThomas Moestl 		    "will not be set accurately\n");
124d7f7792eSThomas Moestl 		return;
125d7f7792eSThomas Moestl 	}
1269b4a8ab7SPoul-Henning Kamp 	/* XXX: We should poll all registered RTCs in case of failure */
127d7f7792eSThomas Moestl 	error = CLOCK_GETTIME(clock_dev, &ts);
128d7f7792eSThomas Moestl 	if (error != 0 && error != EINVAL) {
129d7f7792eSThomas Moestl 		printf("warning: clock_gettime failed (%d), the system time "
130d7f7792eSThomas Moestl 		    "will not be set accurately\n", error);
131d7f7792eSThomas Moestl 		return;
132d7f7792eSThomas Moestl 	}
133d7f7792eSThomas Moestl 	if (error == EINVAL || ts.tv_sec < 0) {
134d7f7792eSThomas Moestl 		printf("Invalid time in real time clock.\n");
135d7f7792eSThomas Moestl 		printf("Check and reset the date immediately!\n");
136d7f7792eSThomas Moestl 	}
137d7f7792eSThomas Moestl 
138e5037a18SPoul-Henning Kamp 	ts.tv_sec += utc_offset();
139d7f7792eSThomas Moestl 
140d7f7792eSThomas Moestl 	if (timespeccmp(&ref, &ts, >)) {
141d7f7792eSThomas Moestl 		diff = ref;
142d7f7792eSThomas Moestl 		timespecsub(&ref, &ts);
143d7f7792eSThomas Moestl 	} else {
144d7f7792eSThomas Moestl 		diff = ts;
145d7f7792eSThomas Moestl 		timespecsub(&diff, &ref);
146d7f7792eSThomas Moestl 	}
147d7f7792eSThomas Moestl 	if (ts.tv_sec >= 2) {
148d7f7792eSThomas Moestl 		/* badly off, adjust it */
149d7f7792eSThomas Moestl 		tc_setclock(&ts);
150d7f7792eSThomas Moestl 	}
151d7f7792eSThomas Moestl }
152d7f7792eSThomas Moestl 
153d7f7792eSThomas Moestl /*
154d7f7792eSThomas Moestl  * Write system time back to RTC
155d7f7792eSThomas Moestl  */
156d7f7792eSThomas Moestl void
157d7f7792eSThomas Moestl resettodr()
158d7f7792eSThomas Moestl {
159d7f7792eSThomas Moestl 	struct timespec ts;
160d7f7792eSThomas Moestl 	int error;
161d7f7792eSThomas Moestl 
162d7f7792eSThomas Moestl 	if (disable_rtc_set || clock_dev == NULL)
163d7f7792eSThomas Moestl 		return;
164d7f7792eSThomas Moestl 
165d7f7792eSThomas Moestl 	getnanotime(&ts);
166e5037a18SPoul-Henning Kamp 	ts.tv_sec -= utc_offset();
1679b4a8ab7SPoul-Henning Kamp 	/* XXX: We should really set all registered RTCs */
168d7f7792eSThomas Moestl 	if ((error = CLOCK_SETTIME(clock_dev, &ts)) != 0) {
169d7f7792eSThomas Moestl 		printf("warning: clock_settime failed (%d), time-of-day clock "
170d7f7792eSThomas Moestl 		    "not adjusted to system time\n", error);
171d7f7792eSThomas Moestl 		return;
172d7f7792eSThomas Moestl 	}
173d7f7792eSThomas Moestl }
174