1/*****************************************************************************
2 *                                                                           *
3 * Copyright (c) David L. Mills 1993                                         *
4 *                                                                           *
5 * Permission to use, copy, modify, and distribute this software and its     *
6 * documentation for any purpose and without fee is hereby granted, provided *
7 * that the above copyright notice appears in all copies and that both the   *
8 * copyright notice and this permission notice appear in supporting          *
9 * documentation, and that the name University of Delaware not be used in    *
10 * advertising or publicity pertaining to distribution of the software       *
11 * without specific, written prior permission.  The University of Delaware   *
12 * makes no representations about the suitability this software for any      *
13 * purpose.  It is provided "as is" without express or implied warranty.     *
14 *                                                                           *
15 *****************************************************************************/
16
17/*
18 * Modification history timex.h
19 *
20 * 29 Dec 97	Russell King
21 *	Moved CLOCK_TICK_RATE, CLOCK_TICK_FACTOR and FINETUNE to asm/timex.h
22 *	for ARM machines
23 *
24 *  9 Jan 97    Adrian Sun
25 *      Shifted LATCH define to allow access to alpha machines.
26 *
27 * 26 Sep 94	David L. Mills
28 *	Added defines for hybrid phase/frequency-lock loop.
29 *
30 * 19 Mar 94	David L. Mills
31 *	Moved defines from kernel routines to header file and added new
32 *	defines for PPS phase-lock loop.
33 *
34 * 20 Feb 94	David L. Mills
35 *	Revised status codes and structures for external clock and PPS
36 *	signal discipline.
37 *
38 * 28 Nov 93	David L. Mills
39 *	Adjusted parameters to improve stability and increase poll
40 *	interval.
41 *
42 * 17 Sep 93    David L. Mills
43 *      Created file $NTP/include/sys/timex.h
44 * 07 Oct 93    Torsten Duwe
45 *      Derived linux/timex.h
46 * 1995-08-13    Torsten Duwe
47 *      kernel PLL updated to 1994-12-13 specs (rfc-1589)
48 * 1997-08-30    Ulrich Windl
49 *      Added new constant NTP_PHASE_LIMIT
50 * 2004-08-12    Christoph Lameter
51 *      Reworked time interpolation logic
52 */
53#ifndef _LINUX_TIMEX_H
54#define _LINUX_TIMEX_H
55
56#include <linux/compiler.h>
57#include <linux/time.h>
58
59#include <asm/param.h>
60
61/*
62 * SHIFT_KG and SHIFT_KF establish the damping of the PLL and are chosen
63 * for a slightly underdamped convergence characteristic. SHIFT_KH
64 * establishes the damping of the FLL and is chosen by wisdom and black
65 * art.
66 *
67 * MAXTC establishes the maximum time constant of the PLL. With the
68 * SHIFT_KG and SHIFT_KF values given and a time constant range from
69 * zero to MAXTC, the PLL will converge in 15 minutes to 16 hours,
70 * respectively.
71 */
72#define SHIFT_KG 6		/* phase factor (shift) */
73#define SHIFT_KF 16		/* PLL frequency factor (shift) */
74#define SHIFT_KH 2		/* FLL frequency factor (shift) */
75#define MAXTC 6			/* maximum time constant (shift) */
76
77/*
78 * The SHIFT_SCALE define establishes the decimal point of the time_phase
79 * variable which serves as an extension to the low-order bits of the
80 * system clock variable. The SHIFT_UPDATE define establishes the decimal
81 * point of the time_offset variable which represents the current offset
82 * with respect to standard time. The FINENSEC define represents 1 nsec in
83 * scaled units.
84 *
85 * SHIFT_USEC defines the scaling (shift) of the time_freq and
86 * time_tolerance variables, which represent the current frequency
87 * offset and maximum frequency tolerance.
88 *
89 * FINENSEC is 1 ns in SHIFT_UPDATE units of the time_phase variable.
90 */
91#define SHIFT_SCALE 22		/* phase scale (shift) */
92#define SHIFT_UPDATE (SHIFT_KG + MAXTC) /* time offset scale (shift) */
93#define SHIFT_USEC 16		/* frequency offset scale (shift) */
94#define FINENSEC (1L << (SHIFT_SCALE - 10)) /* ~1 ns in phase units */
95
96#define MAXPHASE 512000L        /* max phase error (us) */
97#define MAXFREQ (512L << SHIFT_USEC)  /* max frequency error (ppm) */
98#define MINSEC 16L              /* min interval between updates (s) */
99#define MAXSEC 1200L            /* max interval between updates (s) */
100#define	NTP_PHASE_LIMIT	(MAXPHASE << 5)	/* beyond max. dispersion */
101
102/*
103 * syscall interface - used (mainly by NTP daemon)
104 * to discipline kernel clock oscillator
105 */
106struct timex {
107	unsigned int modes;	/* mode selector */
108	long offset;		/* time offset (usec) */
109	long freq;		/* frequency offset (scaled ppm) */
110	long maxerror;		/* maximum error (usec) */
111	long esterror;		/* estimated error (usec) */
112	int status;		/* clock command/status */
113	long constant;		/* pll time constant */
114	long precision;		/* clock precision (usec) (read only) */
115	long tolerance;		/* clock frequency tolerance (ppm)
116				 * (read only)
117				 */
118	struct timeval time;	/* (read only) */
119	long tick;		/* (modified) usecs between clock ticks */
120
121	long ppsfreq;           /* pps frequency (scaled ppm) (ro) */
122	long jitter;            /* pps jitter (us) (ro) */
123	int shift;              /* interval duration (s) (shift) (ro) */
124	long stabil;            /* pps stability (scaled ppm) (ro) */
125	long jitcnt;            /* jitter limit exceeded (ro) */
126	long calcnt;            /* calibration intervals (ro) */
127	long errcnt;            /* calibration errors (ro) */
128	long stbcnt;            /* stability limit exceeded (ro) */
129
130	int  :32; int  :32; int  :32; int  :32;
131	int  :32; int  :32; int  :32; int  :32;
132	int  :32; int  :32; int  :32; int  :32;
133};
134
135/*
136 * Mode codes (timex.mode)
137 */
138#define ADJ_OFFSET		0x0001	/* time offset */
139#define ADJ_FREQUENCY		0x0002	/* frequency offset */
140#define ADJ_MAXERROR		0x0004	/* maximum time error */
141#define ADJ_ESTERROR		0x0008	/* estimated time error */
142#define ADJ_STATUS		0x0010	/* clock status */
143#define ADJ_TIMECONST		0x0020	/* pll time constant */
144#define ADJ_TICK		0x4000	/* tick value */
145#define ADJ_OFFSET_SINGLESHOT	0x8001	/* old-fashioned adjtime */
146
147/* xntp 3.4 compatibility names */
148#define MOD_OFFSET	ADJ_OFFSET
149#define MOD_FREQUENCY	ADJ_FREQUENCY
150#define MOD_MAXERROR	ADJ_MAXERROR
151#define MOD_ESTERROR	ADJ_ESTERROR
152#define MOD_STATUS	ADJ_STATUS
153#define MOD_TIMECONST	ADJ_TIMECONST
154#define MOD_CLKB	ADJ_TICK
155#define MOD_CLKA	ADJ_OFFSET_SINGLESHOT /* 0x8000 in original */
156
157
158/*
159 * Status codes (timex.status)
160 */
161#define STA_PLL		0x0001	/* enable PLL updates (rw) */
162#define STA_PPSFREQ	0x0002	/* enable PPS freq discipline (rw) */
163#define STA_PPSTIME	0x0004	/* enable PPS time discipline (rw) */
164#define STA_FLL		0x0008	/* select frequency-lock mode (rw) */
165
166#define STA_INS		0x0010	/* insert leap (rw) */
167#define STA_DEL		0x0020	/* delete leap (rw) */
168#define STA_UNSYNC	0x0040	/* clock unsynchronized (rw) */
169#define STA_FREQHOLD	0x0080	/* hold frequency (rw) */
170
171#define STA_PPSSIGNAL	0x0100	/* PPS signal present (ro) */
172#define STA_PPSJITTER	0x0200	/* PPS signal jitter exceeded (ro) */
173#define STA_PPSWANDER	0x0400	/* PPS signal wander exceeded (ro) */
174#define STA_PPSERROR	0x0800	/* PPS signal calibration error (ro) */
175
176#define STA_CLOCKERR	0x1000	/* clock hardware fault (ro) */
177
178#define STA_RONLY (STA_PPSSIGNAL | STA_PPSJITTER | STA_PPSWANDER | \
179    STA_PPSERROR | STA_CLOCKERR) /* read-only bits */
180
181/*
182 * Clock states (time_state)
183 */
184#define TIME_OK		0	/* clock synchronized, no leap second */
185#define TIME_INS	1	/* insert leap second */
186#define TIME_DEL	2	/* delete leap second */
187#define TIME_OOP	3	/* leap second in progress */
188#define TIME_WAIT	4	/* leap second has occurred */
189#define TIME_ERROR	5	/* clock not synchronized */
190#define TIME_BAD	TIME_ERROR /* bw compat */
191
192#ifdef __KERNEL__
193#include <asm/timex.h>
194
195/*
196 * kernel variables
197 * Note: maximum error = NTP synch distance = dispersion + delay / 2;
198 * estimated error = NTP dispersion.
199 */
200extern unsigned long tick_usec;		/* USER_HZ period (usec) */
201extern unsigned long tick_nsec;		/* ACTHZ          period (nsec) */
202extern int tickadj;			/* amount of adjustment per tick */
203
204/*
205 * phase-lock loop variables
206 */
207extern int time_state;		/* clock status */
208extern int time_status;		/* clock synchronization status bits */
209extern long time_offset;	/* time adjustment (us) */
210extern long time_constant;	/* pll time constant */
211extern long time_tolerance;	/* frequency tolerance (ppm) */
212extern long time_precision;	/* clock precision (us) */
213extern long time_maxerror;	/* maximum error */
214extern long time_esterror;	/* estimated error */
215
216extern long time_freq;		/* frequency offset (scaled ppm) */
217extern long time_reftime;	/* time at last adjustment (s) */
218
219extern long time_adjust;	/* The amount of adjtime left */
220extern long time_next_adjust;	/* Value for time_adjust at next tick */
221
222/**
223 * ntp_clear - Clears the NTP state variables
224 *
225 * Must be called while holding a write on the xtime_lock
226 */
227static inline void ntp_clear(void)
228{
229	time_adjust = 0;		/* stop active adjtime() */
230	time_status |= STA_UNSYNC;
231	time_maxerror = NTP_PHASE_LIMIT;
232	time_esterror = NTP_PHASE_LIMIT;
233}
234
235/**
236 * ntp_synced - Returns 1 if the NTP status is not UNSYNC
237 *
238 */
239static inline int ntp_synced(void)
240{
241	return !(time_status & STA_UNSYNC);
242}
243
244/* Required to safely shift negative values */
245#define shift_right(x, s) ({	\
246	__typeof__(x) __x = (x);	\
247	__typeof__(s) __s = (s);	\
248	__x < 0 ? -(-__x >> __s) : __x >> __s;	\
249})
250
251
252#ifdef CONFIG_TIME_INTERPOLATION
253
254#define TIME_SOURCE_CPU 0
255#define TIME_SOURCE_MMIO64 1
256#define TIME_SOURCE_MMIO32 2
257#define TIME_SOURCE_FUNCTION 3
258
259/* For proper operations time_interpolator clocks must run slightly slower
260 * than the standard clock since the interpolator may only correct by having
261 * time jump forward during a tick. A slower clock is usually a side effect
262 * of the integer divide of the nanoseconds in a second by the frequency.
263 * The accuracy of the division can be increased by specifying a shift.
264 * However, this may cause the clock not to be slow enough.
265 * The interpolator will self-tune the clock by slowing down if no
266 * resets occur or speeding up if the time jumps per analysis cycle
267 * become too high.
268 *
269 * Setting jitter compensates for a fluctuating timesource by comparing
270 * to the last value read from the timesource to insure that an earlier value
271 * is not returned by a later call. The price to pay
272 * for the compensation is that the timer routines are not as scalable anymore.
273 */
274
275struct time_interpolator {
276	u16 source;			/* time source flags */
277	u8 shift;			/* increases accuracy of multiply by shifting. */
278				/* Note that bits may be lost if shift is set too high */
279	u8 jitter;			/* if set compensate for fluctuations */
280	u32 nsec_per_cyc;		/* set by register_time_interpolator() */
281	void *addr;			/* address of counter or function */
282	u64 mask;			/* mask the valid bits of the counter */
283	unsigned long offset;		/* nsec offset at last update of interpolator */
284	u64 last_counter;		/* counter value in units of the counter at last update */
285	u64 last_cycle;			/* Last timer value if TIME_SOURCE_JITTER is set */
286	u64 frequency;			/* frequency in counts/second */
287	long drift;			/* drift in parts-per-million (or -1) */
288	unsigned long skips;		/* skips forward */
289	unsigned long ns_skipped;	/* nanoseconds skipped */
290	struct time_interpolator *next;
291};
292
293extern void register_time_interpolator(struct time_interpolator *);
294extern void unregister_time_interpolator(struct time_interpolator *);
295extern void time_interpolator_reset(void);
296extern unsigned long time_interpolator_get_offset(void);
297
298#else /* !CONFIG_TIME_INTERPOLATION */
299
300static inline void
301time_interpolator_reset(void)
302{
303}
304
305#endif /* !CONFIG_TIME_INTERPOLATION */
306
307#define TICK_LENGTH_SHIFT	32
308
309/* Returns how long ticks are at present, in ns / 2^(SHIFT_SCALE-10). */
310extern u64 current_tick_length(void);
311
312extern int do_adjtimex(struct timex *);
313
314#endif /* KERNEL */
315
316#endif /* LINUX_TIMEX_H */
317