1d7f7792eSThomas Moestl /* 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 * 3. All advertising materials mentioning features or use of this software 19d7f7792eSThomas Moestl * must display the following acknowledgement: 20d7f7792eSThomas Moestl * This product includes software developed by the University of 21d7f7792eSThomas Moestl * California, Berkeley and its contributors. 22d7f7792eSThomas Moestl * 4. Neither the name of the University nor the names of its contributors 23d7f7792eSThomas Moestl * may be used to endorse or promote products derived from this software 24d7f7792eSThomas Moestl * without specific prior written permission. 25d7f7792eSThomas Moestl * 26d7f7792eSThomas Moestl * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 27d7f7792eSThomas Moestl * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 28d7f7792eSThomas Moestl * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 29d7f7792eSThomas Moestl * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 30d7f7792eSThomas Moestl * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 31d7f7792eSThomas Moestl * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 32d7f7792eSThomas Moestl * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 33d7f7792eSThomas Moestl * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 34d7f7792eSThomas Moestl * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 35d7f7792eSThomas Moestl * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 36d7f7792eSThomas Moestl * SUCH DAMAGE. 37d7f7792eSThomas Moestl * 38d7f7792eSThomas Moestl * from: Utah $Hdr: clock.c 1.18 91/01/21$ 39d7f7792eSThomas Moestl * from: @(#)clock.c 8.2 (Berkeley) 1/12/94 40d7f7792eSThomas Moestl * from: NetBSD: clock_subr.c,v 1.6 2001/07/07 17:04:02 thorpej Exp 41d7f7792eSThomas Moestl * and 42d7f7792eSThomas Moestl * from: src/sys/i386/isa/clock.c,v 1.176 2001/09/04 43d7f7792eSThomas Moestl */ 44d7f7792eSThomas Moestl 45d7f7792eSThomas Moestl /* 46d7f7792eSThomas Moestl * Helpers for time-of-day clocks. This is useful for architectures that need 47d7f7792eSThomas Moestl * support multiple models of such clocks, and generally serves to make the 48d7f7792eSThomas Moestl * code more machine-independent. 49d7f7792eSThomas Moestl * If the clock in question can also be used as a time counter, the driver 50d7f7792eSThomas Moestl * needs to initiate this. 51d7f7792eSThomas Moestl * This code is not yet used by all architectures. 52d7f7792eSThomas Moestl */ 53d7f7792eSThomas Moestl 54d7f7792eSThomas Moestl /* 55d7f7792eSThomas Moestl * Generic routines to convert between a POSIX date 56d7f7792eSThomas Moestl * (seconds since 1/1/1970) and yr/mo/day/hr/min/sec 57d7f7792eSThomas Moestl * Derived from NetBSD arch/hp300/hp300/clock.c 58d7f7792eSThomas Moestl */ 59677b542eSDavid E. O'Brien 60677b542eSDavid E. O'Brien #include <sys/cdefs.h> 61677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$"); 62677b542eSDavid E. O'Brien 63d7f7792eSThomas Moestl #include <sys/param.h> 64d7f7792eSThomas Moestl #include <sys/systm.h> 65d7f7792eSThomas Moestl #include <sys/kernel.h> 66d7f7792eSThomas Moestl #include <sys/bus.h> 67d7f7792eSThomas Moestl #include <sys/clock.h> 68d7f7792eSThomas Moestl #include <sys/sysctl.h> 69d7f7792eSThomas Moestl #include <sys/timetc.h> 70d7f7792eSThomas Moestl 71d88d37d6SThomas Moestl /* XXX: for the CPU_* sysctl OID constants. */ 72d88d37d6SThomas Moestl #include <machine/cpu.h> 73d88d37d6SThomas Moestl 74d7f7792eSThomas Moestl #include "clock_if.h" 75d7f7792eSThomas Moestl 76d7f7792eSThomas Moestl static __inline int leapyear(int year); 77d7f7792eSThomas Moestl static int sysctl_machdep_adjkerntz(SYSCTL_HANDLER_ARGS); 78d7f7792eSThomas Moestl 79d7f7792eSThomas Moestl #define FEBRUARY 2 80d7f7792eSThomas Moestl #define days_in_year(y) (leapyear(y) ? 366 : 365) 81d7f7792eSThomas Moestl #define days_in_month(y, m) \ 82d7f7792eSThomas Moestl (month_days[(m) - 1] + (m == FEBRUARY ? leapyear(y) : 0)) 83d7f7792eSThomas Moestl /* Day of week. Days are counted from 1/1/1970, which was a Thursday */ 84d7f7792eSThomas Moestl #define day_of_week(days) (((days) + 4) % 7) 85d7f7792eSThomas Moestl 86d7f7792eSThomas Moestl static const int month_days[12] = { 87d7f7792eSThomas Moestl 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 88d7f7792eSThomas Moestl }; 89d7f7792eSThomas Moestl 90d7f7792eSThomas Moestl static device_t clock_dev = NULL; 91d7f7792eSThomas Moestl static long clock_res; 92d7f7792eSThomas Moestl 93d7f7792eSThomas Moestl int adjkerntz; /* local offset from GMT in seconds */ 94d7f7792eSThomas Moestl int disable_rtc_set; /* disable resettodr() if != 0 */ 95d7f7792eSThomas Moestl int wall_cmos_clock; /* wall CMOS clock assumed if != 0 */ 96d7f7792eSThomas Moestl 97d7f7792eSThomas Moestl /* 98d7f7792eSThomas Moestl * These have traditionally been in machdep, but should probably be moved to 99d7f7792eSThomas Moestl * kern. 100d7f7792eSThomas Moestl */ 101d88d37d6SThomas Moestl SYSCTL_PROC(_machdep, CPU_ADJKERNTZ, adjkerntz, CTLTYPE_INT|CTLFLAG_RW, 102d7f7792eSThomas Moestl &adjkerntz, 0, sysctl_machdep_adjkerntz, "I", ""); 103d7f7792eSThomas Moestl 104d88d37d6SThomas Moestl SYSCTL_INT(_machdep, CPU_DISRTCSET, disable_rtc_set, 105d7f7792eSThomas Moestl CTLFLAG_RW, &disable_rtc_set, 0, ""); 106d7f7792eSThomas Moestl 107d88d37d6SThomas Moestl SYSCTL_INT(_machdep, CPU_WALLCLOCK, wall_cmos_clock, 108d7f7792eSThomas Moestl CTLFLAG_RW, &wall_cmos_clock, 0, ""); 109d7f7792eSThomas Moestl 110d7f7792eSThomas Moestl static int 111d7f7792eSThomas Moestl sysctl_machdep_adjkerntz(SYSCTL_HANDLER_ARGS) 112d7f7792eSThomas Moestl { 113d7f7792eSThomas Moestl int error; 114d7f7792eSThomas Moestl error = sysctl_handle_int(oidp, oidp->oid_arg1, oidp->oid_arg2, 115d7f7792eSThomas Moestl req); 116d7f7792eSThomas Moestl if (!error && req->newptr) 117d7f7792eSThomas Moestl resettodr(); 118d7f7792eSThomas Moestl return (error); 119d7f7792eSThomas Moestl } 120d7f7792eSThomas Moestl 121d7f7792eSThomas Moestl /* 122d7f7792eSThomas Moestl * This inline avoids some unnecessary modulo operations 123d7f7792eSThomas Moestl * as compared with the usual macro: 124d7f7792eSThomas Moestl * ( ((year % 4) == 0 && 125d7f7792eSThomas Moestl * (year % 100) != 0) || 126d7f7792eSThomas Moestl * ((year % 400) == 0) ) 127d7f7792eSThomas Moestl * It is otherwise equivalent. 128d7f7792eSThomas Moestl */ 129d7f7792eSThomas Moestl static __inline int 130d7f7792eSThomas Moestl leapyear(int year) 131d7f7792eSThomas Moestl { 132d7f7792eSThomas Moestl int rv = 0; 133d7f7792eSThomas Moestl 134d7f7792eSThomas Moestl if ((year & 3) == 0) { 135d7f7792eSThomas Moestl rv = 1; 136d7f7792eSThomas Moestl if ((year % 100) == 0) { 137d7f7792eSThomas Moestl rv = 0; 138d7f7792eSThomas Moestl if ((year % 400) == 0) 139d7f7792eSThomas Moestl rv = 1; 140d7f7792eSThomas Moestl } 141d7f7792eSThomas Moestl } 142d7f7792eSThomas Moestl return (rv); 143d7f7792eSThomas Moestl } 144d7f7792eSThomas Moestl 145d7f7792eSThomas Moestl int 146d7f7792eSThomas Moestl clock_ct_to_ts(struct clocktime *ct, struct timespec *ts) 147d7f7792eSThomas Moestl { 148d7f7792eSThomas Moestl time_t secs; 149d7f7792eSThomas Moestl int i, year, days; 150d7f7792eSThomas Moestl 151d7f7792eSThomas Moestl year = ct->year; 152d7f7792eSThomas Moestl 153d7f7792eSThomas Moestl /* Sanity checks. */ 154d7f7792eSThomas Moestl if (ct->mon < 1 || ct->mon > 12 || ct->day < 1 || 155d7f7792eSThomas Moestl ct->day > days_in_month(year, ct->mon) || 156d7f7792eSThomas Moestl ct->hour > 23 || ct->min > 59 || ct->sec > 59 || 157d7f7792eSThomas Moestl ct->year > 2037) /* time_t overflow */ 158d7f7792eSThomas Moestl return (EINVAL); 159d7f7792eSThomas Moestl 160d7f7792eSThomas Moestl /* 161d7f7792eSThomas Moestl * Compute days since start of time 162d7f7792eSThomas Moestl * First from years, then from months. 163d7f7792eSThomas Moestl */ 164d7f7792eSThomas Moestl days = 0; 165d7f7792eSThomas Moestl for (i = POSIX_BASE_YEAR; i < year; i++) 166d7f7792eSThomas Moestl days += days_in_year(i); 167d7f7792eSThomas Moestl 168d7f7792eSThomas Moestl /* Months */ 169d7f7792eSThomas Moestl for (i = 1; i < ct->mon; i++) 170d7f7792eSThomas Moestl days += days_in_month(year, i); 171d7f7792eSThomas Moestl days += (ct->day - 1); 172d7f7792eSThomas Moestl 173d7f7792eSThomas Moestl /* Another sanity check. */ 174d7f7792eSThomas Moestl if (ct->dow != -1 && ct->dow != day_of_week(days)) 175d7f7792eSThomas Moestl return (EINVAL); 176d7f7792eSThomas Moestl 177d7f7792eSThomas Moestl /* Add hours, minutes, seconds. */ 178d7f7792eSThomas Moestl secs = ((days * 24 + ct->hour) * 60 + ct->min) * 60 + ct->sec; 179d7f7792eSThomas Moestl 180d7f7792eSThomas Moestl ts->tv_sec = secs; 181d7f7792eSThomas Moestl ts->tv_nsec = ct->nsec; 182d7f7792eSThomas Moestl return (0); 183d7f7792eSThomas Moestl } 184d7f7792eSThomas Moestl 185d7f7792eSThomas Moestl void 186d7f7792eSThomas Moestl clock_ts_to_ct(struct timespec *ts, struct clocktime *ct) 187d7f7792eSThomas Moestl { 188d7f7792eSThomas Moestl int i, year, days; 189d7f7792eSThomas Moestl time_t rsec; /* remainder seconds */ 190d7f7792eSThomas Moestl time_t secs; 191d7f7792eSThomas Moestl 192d7f7792eSThomas Moestl secs = ts->tv_sec; 193d7f7792eSThomas Moestl days = secs / SECDAY; 194d7f7792eSThomas Moestl rsec = secs % SECDAY; 195d7f7792eSThomas Moestl 196d7f7792eSThomas Moestl ct->dow = day_of_week(days); 197d7f7792eSThomas Moestl 198d7f7792eSThomas Moestl /* Subtract out whole years, counting them in i. */ 199d7f7792eSThomas Moestl for (year = POSIX_BASE_YEAR; days >= days_in_year(year); year++) 200d7f7792eSThomas Moestl days -= days_in_year(year); 201d7f7792eSThomas Moestl ct->year = year; 202d7f7792eSThomas Moestl 203d7f7792eSThomas Moestl /* Subtract out whole months, counting them in i. */ 204d7f7792eSThomas Moestl for (i = 1; days >= days_in_month(year, i); i++) 205d7f7792eSThomas Moestl days -= days_in_month(year, i); 206d7f7792eSThomas Moestl ct->mon = i; 207d7f7792eSThomas Moestl 208d7f7792eSThomas Moestl /* Days are what is left over (+1) from all that. */ 209d7f7792eSThomas Moestl ct->day = days + 1; 210d7f7792eSThomas Moestl 211d7f7792eSThomas Moestl /* Hours, minutes, seconds are easy */ 212d7f7792eSThomas Moestl ct->hour = rsec / 3600; 213d7f7792eSThomas Moestl rsec = rsec % 3600; 214d7f7792eSThomas Moestl ct->min = rsec / 60; 215d7f7792eSThomas Moestl rsec = rsec % 60; 216d7f7792eSThomas Moestl ct->sec = rsec; 217d7f7792eSThomas Moestl ct->nsec = ts->tv_nsec; 218d7f7792eSThomas Moestl } 219d7f7792eSThomas Moestl 220d7f7792eSThomas Moestl void 221d7f7792eSThomas Moestl clock_register(device_t dev, long res) 222d7f7792eSThomas Moestl { 223d7f7792eSThomas Moestl 224d7f7792eSThomas Moestl if (clock_dev != NULL) { 225d7f7792eSThomas Moestl if (clock_res > res) { 226d7f7792eSThomas Moestl if (bootverbose) { 227d7f7792eSThomas Moestl device_printf(dev, "not installed as " 228d7f7792eSThomas Moestl "time-of-day clock: clock %s has higher " 229d7f7792eSThomas Moestl "resolution\n", device_get_name(clock_dev)); 230d7f7792eSThomas Moestl } 231d7f7792eSThomas Moestl return; 232d7f7792eSThomas Moestl } else { 233d7f7792eSThomas Moestl if (bootverbose) { 234d7f7792eSThomas Moestl device_printf(clock_dev, "removed as " 235d7f7792eSThomas Moestl "time-of-day clock: clock %s has higher " 236d7f7792eSThomas Moestl "resolution\n", device_get_name(dev)); 237d7f7792eSThomas Moestl } 238d7f7792eSThomas Moestl } 239d7f7792eSThomas Moestl } 240d7f7792eSThomas Moestl clock_dev = dev; 241d7f7792eSThomas Moestl clock_res = res; 242d7f7792eSThomas Moestl if (bootverbose) { 243d7f7792eSThomas Moestl device_printf(dev, "registered as a time-of-day clock " 244d7f7792eSThomas Moestl "(resolution %ldus)\n", res); 245d7f7792eSThomas Moestl } 246d7f7792eSThomas Moestl } 247d7f7792eSThomas Moestl 248d7f7792eSThomas Moestl /* 249d7f7792eSThomas Moestl * inittodr and settodr derived from the i386 versions written 250d7f7792eSThomas Moestl * by Christoph Robitschko <chmr@edvz.tu-graz.ac.at>, reintroduced and 251d7f7792eSThomas Moestl * updated by Chris Stenton <chris@gnome.co.uk> 8/10/94 252d7f7792eSThomas Moestl */ 253d7f7792eSThomas Moestl 254d7f7792eSThomas Moestl /* 255d7f7792eSThomas Moestl * Initialize the time of day register, based on the time base which is, e.g. 256d7f7792eSThomas Moestl * from a filesystem. 257d7f7792eSThomas Moestl */ 258d7f7792eSThomas Moestl void 259d7f7792eSThomas Moestl inittodr(time_t base) 260d7f7792eSThomas Moestl { 261d7f7792eSThomas Moestl struct timespec diff, ref, ts; 262d7f7792eSThomas Moestl int error; 263d7f7792eSThomas Moestl 264d7f7792eSThomas Moestl if (base) { 265d7f7792eSThomas Moestl ref.tv_sec = base; 266d7f7792eSThomas Moestl ref.tv_nsec = 0; 267d7f7792eSThomas Moestl tc_setclock(&ref); 268d7f7792eSThomas Moestl } 269d7f7792eSThomas Moestl 270d7f7792eSThomas Moestl if (clock_dev == NULL) { 271d7f7792eSThomas Moestl printf("warning: no time-of-day clock registered, system time " 272d7f7792eSThomas Moestl "will not be set accurately\n"); 273d7f7792eSThomas Moestl return; 274d7f7792eSThomas Moestl } 275d7f7792eSThomas Moestl error = CLOCK_GETTIME(clock_dev, &ts); 276d7f7792eSThomas Moestl if (error != 0 && error != EINVAL) { 277d7f7792eSThomas Moestl printf("warning: clock_gettime failed (%d), the system time " 278d7f7792eSThomas Moestl "will not be set accurately\n", error); 279d7f7792eSThomas Moestl return; 280d7f7792eSThomas Moestl } 281d7f7792eSThomas Moestl if (error == EINVAL || ts.tv_sec < 0) { 282d7f7792eSThomas Moestl printf("Invalid time in real time clock.\n"); 283d7f7792eSThomas Moestl printf("Check and reset the date immediately!\n"); 284d7f7792eSThomas Moestl } 285d7f7792eSThomas Moestl 28691f1c2b3SPoul-Henning Kamp ts.tv_sec += tz_minuteswest * 60 + 287d7f7792eSThomas Moestl (wall_cmos_clock ? adjkerntz : 0); 288d7f7792eSThomas Moestl 289d7f7792eSThomas Moestl if (timespeccmp(&ref, &ts, >)) { 290d7f7792eSThomas Moestl diff = ref; 291d7f7792eSThomas Moestl timespecsub(&ref, &ts); 292d7f7792eSThomas Moestl } else { 293d7f7792eSThomas Moestl diff = ts; 294d7f7792eSThomas Moestl timespecsub(&diff, &ref); 295d7f7792eSThomas Moestl } 296d7f7792eSThomas Moestl if (ts.tv_sec >= 2) { 297d7f7792eSThomas Moestl /* badly off, adjust it */ 298d7f7792eSThomas Moestl tc_setclock(&ts); 299d7f7792eSThomas Moestl } 300d7f7792eSThomas Moestl } 301d7f7792eSThomas Moestl 302d7f7792eSThomas Moestl /* 303d7f7792eSThomas Moestl * Write system time back to RTC 304d7f7792eSThomas Moestl */ 305d7f7792eSThomas Moestl void 306d7f7792eSThomas Moestl resettodr() 307d7f7792eSThomas Moestl { 308d7f7792eSThomas Moestl struct timespec ts; 309d7f7792eSThomas Moestl int error; 310d7f7792eSThomas Moestl 311d7f7792eSThomas Moestl if (disable_rtc_set || clock_dev == NULL) 312d7f7792eSThomas Moestl return; 313d7f7792eSThomas Moestl 314d7f7792eSThomas Moestl getnanotime(&ts); 31591f1c2b3SPoul-Henning Kamp ts.tv_sec -= tz_minuteswest * 60 + (wall_cmos_clock ? adjkerntz : 0); 316d7f7792eSThomas Moestl if ((error = CLOCK_SETTIME(clock_dev, &ts)) != 0) { 317d7f7792eSThomas Moestl printf("warning: clock_settime failed (%d), time-of-day clock " 318d7f7792eSThomas Moestl "not adjusted to system time\n", error); 319d7f7792eSThomas Moestl return; 320d7f7792eSThomas Moestl } 321d7f7792eSThomas Moestl } 322