2 * Copyright (c) 2005 Marcel Moolenaar
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
31 #include <sys/kernel.h>
32 #include <sys/clock.h>
33 #include <sys/queue.h>
34 #include <sys/sysctl.h>
35 #include <sys/systm.h>
37 #include <sys/clock.h>
38 #include <sys/timetc.h>
41 #include <machine/clock.h>
42 #include <machine/cpu.h>
43 #include <machine/efi.h>
45 uint64_t ia64_clock_reload;
47 static int clock_initialized = 0;
50 static timecounter_get_t ia64_get_timecount;
52 static struct timecounter ia64_timecounter = {
53 ia64_get_timecount, /* get_timecount */
55 ~0u, /* counter_mask */
61 ia64_get_timecount(struct timecounter* tc)
63 return ia64_get_itc();
71 PCPU_SET(clockadj, 0);
72 PCPU_SET(clock, ia64_get_itc());
73 ia64_set_itm(PCPU_GET(clock) + ia64_clock_reload);
74 ia64_set_itv(CLOCK_VECTOR); /* highest priority class */
79 * Start the real-time and statistics clocks. We use cr.itc and cr.itm
80 * to implement a 1000hz clock.
86 if (itc_frequency == 0)
87 panic("Unknown clock frequency");
90 ia64_clock_reload = (itc_frequency + hz/2) / hz;
93 ia64_timecounter.tc_frequency = itc_frequency;
94 tc_init(&ia64_timecounter);
101 cpu_startprofclock(void)
108 cpu_stopprofclock(void)
115 inittodr(time_t base)
125 * This code was written in 2005, so logically EFI cannot return
126 * a year smaller than that. Assume the EFI clock is out of whack
127 * in that case and reset the EFI clock.
129 if (tm.tm_year < 2005) {
130 printf("WARNING: CHECK AND RESET THE DATE!\n");
131 memset(&tm, 0, sizeof(tm));
133 tm.tm_mon = tm.tm_mday = 1;
134 if (efi_set_time(&tm))
135 printf("ERROR: COULD NOT RESET EFI CLOCK!\n");
138 ct.nsec = tm.tm_nsec;
141 ct.hour = tm.tm_hour;
144 ct.year = tm.tm_year;
146 if (clock_ct_to_ts(&ct, &ts))
147 printf("Invalid time in clock: check and reset the date!\n");
148 ts.tv_sec += utc_offset();
151 * The EFI clock is supposed to be a real-time clock, whereas the
152 * base argument is coming from a saved (as on disk) time. It's
153 * impossible for a saved time to represent a time in the future,
154 * so we expect the EFI clock to be larger. If not, the EFI clock
155 * may not be reliable and we trust the base.
156 * Warn if the EFI clock was off by 2 or more days.
158 if (ts.tv_sec < base) {
159 days = (base - ts.tv_sec) / (60L * 60L * 24L);
161 printf("WARNING: EFI clock lost %ld days!\n", days);
167 clock_initialized = 1;
171 * Reset the TODR based on the time value; used when the TODR has a
172 * preposterous value and also when the time is reset by the stime
173 * system call. Also called when the TODR goes past
174 * TODRZERO + 100*(SECYEAR+2*SECDAY) (e.g. on Jan 2 just after midnight)
175 * to wrap the TODR around.
184 if (!clock_initialized || disable_rtc_set)
189 ts.tv_sec -= utc_offset();
190 clock_ts_to_ct(&ts, &ct);
192 tm.tm_nsec = ts.tv_nsec;
195 tm.tm_hour = ct.hour;
197 tm.tm_year = ct.year;
200 if (efi_set_time(&tm))
201 printf("ERROR: COULD NOT RESET EFI CLOCK!\n");