"Fossies" - the Fresh Open Source Software Archive

Member "PPSkit-2.1.7/patch-2.4.33.2" (26 Aug 2006, 305708 Bytes) of package /linux/misc/old/PPSkit-2.1.7.tar.gz:


As a special service "Fossies" has tried to format the requested text file into HTML format (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file.

    1 : Differences between tags 'r2_4_33_2' and 'PPS_2_1_7'
    2 : Using module 'Kernel' in CVS repository '/usr/src/repositories/LinuxCVS'
    3 : This patch was created automatically at Sat Aug 26 22:15:55 CEST 2006
    4 : ($Id: patch-2.4.33.2,v 1.1.2.1 2006/08/26 20:24:35 windl Exp $)
    5 Index: linux/CREDITS
    6 diff -u linux/CREDITS:1.1.1.22 linux/CREDITS:1.1.1.22.2.1
    7 --- linux/CREDITS:1.1.1.22	Sat Aug 26 21:34:22 2006
    8 +++ linux/CREDITS	Sat Aug 26 21:48:04 2006
    9 @@ -3311,7 +3311,10 @@
   10  N: Ulrich Windl
   11  E: Ulrich.Windl@rz.uni-regensburg.de
   12  P: 1024/E843660D CF D7 43 A1 5A 49 14 25  7C 04 A0 6E 4C 3A AC 6D
   13 -D: Supports NTP on Linux.  Added PPS code.  Fixed bugs in adjtimex().
   14 +D: Supports NTP on Linux.  Implemented PPS API (RFC 2783).
   15 +D: Extended and updated kernel time routines to match the demands of NTP.
   16 +D: Added support for nanosecond time resolution (time "mega patch").
   17 +D: Several fixes for adjtimex().  Added /proc/sys/kernel/time directory.
   18  S: Alte Regensburger Str. 11a
   19  S: 93149 Nittenau
   20  S: Germany
   21 Index: linux/Makefile
   22 diff -u linux/Makefile:1.1.1.23 linux/Makefile:1.1.1.23.2.1
   23 --- linux/Makefile:1.1.1.23	Sat Aug 26 21:34:22 2006
   24 +++ linux/Makefile	Sat Aug 26 21:48:04 2006
   25 @@ -1,7 +1,7 @@
   26  VERSION = 2
   27  PATCHLEVEL = 4
   28  SUBLEVEL = 33
   29 -EXTRAVERSION = .2
   30 +EXTRAVERSION = .2-NANO
   31  
   32  KERNELRELEASE=$(VERSION).$(PATCHLEVEL).$(SUBLEVEL)$(EXTRAVERSION)
   33  
   34 Index: linux/Documentation/Configure.help
   35 diff -u linux/Documentation/Configure.help:1.1.1.21 linux/Documentation/Configure.help:1.1.1.21.2.1
   36 --- linux/Documentation/Configure.help:1.1.1.21	Sat Aug 26 21:35:38 2006
   37 +++ linux/Documentation/Configure.help	Sat Aug 26 21:48:23 2006
   38 @@ -4859,6 +4859,28 @@
   39  
   40    If unsure, say N.
   41  
   42 +NTP kernel support
   43 +CONFIG_NTP
   44 +  Say N here if you are sure you do not use the NTP kernel code and you
   45 +  want to minimize the kernel size or system overhead.  Otherwise say Y.
   46 +
   47 +NTP PPS support
   48 +CONFIG_NTP_PPS
   49 +  Saying Y enables extra code that can calibrate your system clock relative
   50 +  to some external precision pulse-per-second (PPS).  This simply provides
   51 +  an additional kernel routine that still needs to be used by some driver
   52 +  (e.g. the serial driver can take time stamps for pulses on carrier detect).
   53 +  Normally you would say N here.
   54 +
   55 +NTP PPS support on serial port
   56 +CONFIG_NTP_PPS_SERIAL
   57 +  Saying Y here will provide the PPS API (RFC 2783) for the serial driver.
   58 +  The PPS API enables application programs to capture precision time stamps,
   59 +  optionally calibrating the software clock.  This feature is optional, and
   60 +  it keeps the port available for normal operation.  Usually you don't need
   61 +  this.  See http://www.ntp.org/ntpfaq/NTP-s-config.htm#S-CONFIG-ADV-PPS for
   62 +  details.
   63 +
   64  VGA text console
   65  CONFIG_VGA_CONSOLE
   66    Saying Y here will allow you to use Linux in text mode through a
   67 Index: linux/Documentation/ioctl-number.txt
   68 diff -u linux/Documentation/ioctl-number.txt:1.1.1.10 linux/Documentation/ioctl-number.txt:1.1.1.10.2.1
   69 --- linux/Documentation/ioctl-number.txt:1.1.1.10	Sat Aug 26 21:35:37 2006
   70 +++ linux/Documentation/ioctl-number.txt	Sat Aug 26 21:48:24 2006
   71 @@ -72,7 +72,7 @@
   72  		linux/blkpg.h
   73  0x20	all	drivers/cdrom/cm206.h
   74  0x22	all	scsi/sg.h
   75 -'1'	00-1F	<linux/timepps.h>	PPS kit from Ulrich Windl
   76 +'1'	00-1F	linux/timepps.h		PPSkit (PPS API) from Ulrich Windl
   77  					<ftp://ftp.de.kernel.org/pub/linux/daemons/ntp/PPS/>
   78  '6'	00-10	<asm-i386/processor.h>	Intel IA32 microcode update driver
   79  					<mailto:tigran@veritas.com>
   80 Index: linux/Documentation/kernel-time.txt
   81 diff -u /dev/null linux/Documentation/kernel-time.txt:1.2.12.2
   82 --- /dev/null	Sat Aug 26 22:15:56 2006
   83 +++ linux/Documentation/kernel-time.txt	Sat Aug 26 21:53:19 2006
   84 @@ -0,0 +1,359 @@
   85 +Kernel Time Overview
   86 +====================
   87 +
   88 +This document gives a short overview about the code that handles time
   89 +in the Linux kernel in order to make it understandable what all the
   90 +NTP stuff is about. The description concentrates on the user's view of
   91 +kernel time.
   92 +
   93 +1. Hardware
   94 +===========
   95 +
   96 +The kernel uses the timer (programmable periodic interrupt at rate
   97 +``HZ'') to increment the kernel clock. The kernel clock consists of
   98 +two counters.  The first one (``tv_sec'') counts the seconds since
   99 +1970-01-01, while the second one (``tv_nsec'') manages the fraction of
  100 +the current second in nanoseconds.  During boot the first value is
  101 +derived from the time found in the CMOS clock (also known as ``real
  102 +time clock'') which only has a resolution of one second.
  103 +
  104 +During each interrupt a specific amount (the "time_tick",
  105 +1000000000/HZ) of nanoseconds is added to the fractional second.  If
  106 +a second overflow occurs, it is handled accordingly.  Normally the new
  107 +kernel time is not written back to the CMOS clock, but it can be done.
  108 +
  109 +If an application program asks for the current time, it is possible to
  110 +get an estimate of the elapsed time since the last timer interrupt, by
  111 +extrapolating the time.  This is what  ``do_nanotime()'' is for.
  112 +
  113 +On the Intel 386 architecture the registers in the timer chip can be
  114 +read to get a resolution of less than a microsecond.
  115 +
  116 +On a Pentium the CPU's cycle counter can be used to estimate the time
  117 +since the last timer interrupt by scaling the cycles elapsed during
  118 +two successive timer interrupts. The resolution for fast CPUs is
  119 +significantly higher than for the timer's counter register (close to
  120 +or even below one microsecond).  Furthermore it can be read much
  121 +faster than the hardware registers of the timer chip.  Unfortunately
  122 +modern motherboards may vary the CPU frequency to avoid overheating
  123 +the processor or to save power (APM features).
  124 +
  125 +Other architectures have similar fast timing facilities, more or less
  126 +reliable.
  127 +
  128 +These are the components that determine the "resolution" of the kernel
  129 +clock.  Also the "stability" (reliability or predictability) and
  130 +precision (the error accumulated during reading the time) is
  131 +determined by the hardware.
  132 +
  133 +2. Software
  134 +===========
  135 +
  136 +While the hardware is responsible for the resolution and precision of
  137 +the clock, the software is responsible for calibrating the clock and
  138 +programming the hardware. Linux uses an extended algorithm to manage
  139 +system time.  That algorithm is basically the NTP kernel clock model
  140 +described in RFC 1589, RFC 1305 and related papers.  The current
  141 +implementation is referred to as ``nanokernel'', because it keeps time
  142 +in nanoseconds.
  143 +
  144 +As most clocks are not very precise or stable, it is necessary to
  145 +correct the kernel clock from time to time. Traditionally it is
  146 +possible to overwrite the current kernel counters to make the time
  147 +"step" (stime, settimeofday, clock_settime). Also it's possible to
  148 +initiate a more gradual change in time by specifying the amount of
  149 +time that is to be changed. This is called "slewing" the clock
  150 +(adjtime).
  151 +
  152 +When slewing the clock, the value of "time_tick" is modified by a
  153 +small amount, called "tickadj" (50000/HZ, Ás per interrupt). That
  154 +way the clock seems to be running faster or slower for some time
  155 +(until the desired correction has been applied), and then continues to
  156 +run at nominal speed. Obviously "tickadj" does not only affect the
  157 +speed in which an adjustment is made, but also the resolution of the
  158 +adjustment.  Usually there is no adjustment below "tickadj". One
  159 +reason against using the smallest possible value for tickadj is that
  160 +the correction must be more than the clock's intrinsic error (Besides
  161 +that fact that a correction can take very long). Otherwise you'll
  162 +never be able to keep up with the error.
  163 +
  164 +2.1 NTP extensions
  165 +==================
  166 +
  167 +Independent on which of the above two methods is used, the process has
  168 +to be repeated again and again to keep the deviation of the software
  169 +clock within a small interval. When you know the percentage of your
  170 +clock error (e.g. 15 seconds to fast within 24 hours), you can also
  171 +pass a correction value to the kernel, asking for automatic
  172 +correction of the frequency error.
  173 +
  174 +Such a correction value can be expressed in PPM (parts per million,
  175 +0.0001%) of the clock's "frequency". Additionally the enhanced clock
  176 +model can be told to learn from periodic adjustments to the time
  177 +offset, such that the virtual frequency of the kernel clock is
  178 +modified (you can't tune the hardware).  Alternatively the frequency
  179 +can be set directly. As these corrections are limited by ``tickadj''
  180 +(max <= HZ*tickadj), it's sometimes necessary to change the value of
  181 +``time_tick'' (if you want to keep tickadj small).
  182 +
  183 +Unfortunately specifying the frequency alone is not sufficient to make
  184 +a stable clock, because the frequency varies over time. These
  185 +variations in frequency are usually influenced by the environmental
  186 +temperature and are commonly named "drift". When the clock algorithm
  187 +learns from periodic adjustments, it also gets an estimate for the
  188 +drift. Thus at each timer interrupt a small "tolerance" (uncertainty)
  189 +is added to the clock's error. If the initial error is set when
  190 +synchronizing, the kernel maintains an estimate of the current maximum
  191 +error.
  192 +
  193 +The idea is to "synchronize" the kernel clock to a reference clock and
  194 +let it run free for a while, until the estimated error is too big to
  195 +be tolerated (Here a few milliseconds is considered "big" already, and
  196 +typical update intervals are between one minute and one hour).
  197 +
  198 +To make the automatic correction work, the basic algorithm is modified
  199 +in a way that can deal with fractions of nanoseconds, and time_tick is
  200 +temporarily modified to yield a value that results in an error of less
  201 +than one nanosecond per timer interrupt.
  202 +
  203 +Besides that regulation and error propagating machinery, the kernel
  204 +clock also handles insertion and deletion of leap seconds (if it is
  205 +told to do so).
  206 +
  207 +Managing a precise and stable kernel time requires periodic updates to
  208 +the clock offset and estimated error (tolerance) of the kernel clock.
  209 +Depending on the clock's stability there is an optimal update
  210 +frequency, derived from the so-called Allan variance.  The xntpd
  211 +daemon is a program to do that (see http://www.eecis.udel.edu/~ntp),
  212 +but there's also a way to calibrate the kernel clock by external
  213 +hardware.  One of these methods is called the PPS (Pulse Per Second)
  214 +code.
  215 +
  216 +2.1.1 PPS calibration
  217 +=====================
  218 +
  219 +PPS calibration works very similar to time calibration, but only on a
  220 +smaller scale.  The code assumes the clock initially is closer than
  221 +0.5 seconds to some reference time.  As the PPS can only calibrate the
  222 +frequency and the offset relative to the second, the absolute time
  223 +(epoch) must be set by other means.
  224 +
  225 +Basically the code automatically compares the current clock offset for
  226 +each second with a high precision external pulse (e.g. derived from a
  227 +GPS receiver, allowing a maximum relative error of 500PPM) and learns
  228 +whether the clock frequency is too slow or fast. The code can be told
  229 +to adjust only the time offset, or to adjust the frequency of the
  230 +kernel.  The precision achievable with PPS synchronization is
  231 +significantly higher than with usual adjustments.  With a Celeron-500
  232 +system with little load it is possible to get the clock as close as a
  233 +few hundred nanoseconds relative to the PPS on average.
  234 +
  235 +PPS API
  236 +-------
  237 +
  238 +There's a new RFC (2783) on how to implement a common programming
  239 +interface for the PPS stuff.  Linux includes an example implementation
  240 +for the serial port.  You can capture changes of the carrier detect
  241 +status line.  These events can be used to calibrate the system clock
  242 +if the pulses are stable enough.  See <linux/timepps.h>.
  243 +
  244 +2.2 The new Linux implementation
  245 +================================
  246 +
  247 +In the Linux implementation the CMOS clock is updated periodically
  248 +(every 11 minutes) from the kernel time when the clock is synchronized
  249 +(flag STA_UNSYNC is cleared). The flag STA_UNSYNC will automatically
  250 +be set, once the maximum error is above a fixed limit
  251 +(NTP_PHASE_LIMIT, currently about 2s).  Unfortunately the maximum
  252 +error increases with a worst-case estimate (in reality the increase of
  253 +the error depends mostly on the frequency change of the system
  254 +clock(s).  A busy system board gets warmer, and the frequency of the
  255 +clock decreases).
  256 +
  257 +Linux uses one function, adjtimex, to implement the functions for
  258 +classical adjtime() and the new ntp_gettime() and ntp_adjtime()
  259 +functions.  In order to achieve that, the function uses one rather big
  260 +"struct kernel_timex" (Currently just named ``timex'' to confuse
  261 +people) that contains a "modes" field and several state variables.  By
  262 +setting individual bits in the "modes" field the specific function of
  263 +the adjtimex system call is selected while the remaining fields of the
  264 +structure are written or read as needed.
  265 +
  266 +struct kernel_timex {
  267 +	unsigned int modes;	/* clock mode bits (wo) */
  268 +	long	offset;		/* time offset (ns/us) (rw) */
  269 +	long	freq;		/* frequency offset (scaled PPM) (rw) */
  270 +	long	maxerror;	/* maximum error (us) (rw) */
  271 +	long	esterror;	/* estimated error (us) (rw) */
  272 +	int	status;		/* clock status bits (rw) */
  273 +	long	constant;	/* poll interval (log2 s) (rw) */
  274 +	long	precision;	/* clock precision (ns/us) (ro) */
  275 +	long	tolerance;	/* clock frequency tolerance (scaled
  276 +				 * PPM) (ro) */
  277 +	/*----BEGIN Linux extensions (part 1) */
  278 +	/* Depending on whether ``NANO'' is defined when including
  279 +	 * this file, the same memory location can be accessed under
  280 +	 * two different names.  ``STA_NANO'' at runtime should
  281 +	 * correspond with ``NANO'' at compilation time.
  282 +	 */
  283 +#ifdef NANO
  284 +	struct timespec time;	/* current time (ns) (ro) */
  285 +#else
  286 +	struct timeval time;	/* current time (us) (ro) */
  287 +#endif /* NANO */
  288 +	long	time_tick;	/* time increment per tick (?) (rw) */
  289 +	/*----END Linux extensions (part 1) */
  290 +	/*
  291 +	 * The following read-only structure members are implemented
  292 +	 * only if the PPS signal discipline is configured in the
  293 +	 * kernel. They are included in all configurations to insure
  294 +	 * portability.
  295 +	 */
  296 +	long	ppsfreq;	/* PPS frequency (scaled PPM) (ro) */
  297 +	long	jitter;		/* PPS jitter (ns/us) (ro) */
  298 +	int	shift;		/* interval duration (s) (shift) (ro) */
  299 +	long	stabil;		/* PPS stability (scaled PPM) (ro) */
  300 +	long	jitcnt;		/* jitter limit exceeded (ro) */
  301 +	long	calcnt;		/* calibration intervals (ro) */
  302 +	long	errcnt;		/* calibration errors (ro) */
  303 +	long	stbcnt;		/* stability limit exceeded (ro) */
  304 +	/*----BEGIN Linux extensions (part 2) */
  305 +	/* first we reserve some space, then we add our new stuff */
  306 +	/* Yes, it's ugly, but it's binary compatible */
  307 +	int  :32; int  :32; int  :32; int  :32;
  308 +	int tickadj;		/* tickadj (us) (rw) -- extension by UW */
  309 +	int  :32; int  :32; int  :32;
  310 +	int  :32; int  :32; int  :32; int  :32;
  311 +	/*----END Linux extensions (part 2) */
  312 +};
  313 +
  314 +The official ntp_gettime() function only returns these data:
  315 +
  316 +struct ntptimeval {
  317 +	struct timeval	time;	/* current time (ro) */
  318 +	long maxerror;		/* maximum error (us) (ro) */
  319 +	long esterror;		/* estimated error (us) (ro) */
  320 +};
  321 +
  322 +The official ntp_adjtime() function contains these data (note the lack
  323 +of `time'!):
  324 +
  325 +struct timex {
  326 +	unsigned int modes;	/* mode selector */
  327 +	long offset;		/* time offset (usec) */
  328 +	long freq;		/* frequency offset (scaled ppm) */
  329 +	long maxerror;		/* maximum error (usec) */
  330 +	long esterror;		/* estimated error (usec) */
  331 +	int status;		/* clock command/status */
  332 +	long constant;		/* pll time constant */
  333 +	long precision;		/* clock precision (usec) (read only) */
  334 +	long tolerance;		/* clock frequency tolerance (ppm)
  335 +				 * (read only)
  336 +				 */
  337 +
  338 +	long ppsfreq;           /* pps frequency (scaled ppm) (ro) */
  339 +	long jitter;            /* pps jitter (us) (ro) */
  340 +	int shift;              /* interval duration (s) (shift) (ro) */
  341 +	long stabil;            /* pps stability (scaled ppm) (ro) */
  342 +	long jitcnt;            /* jitter limit exceeded (ro) */
  343 +	long calcnt;            /* calibration intervals (ro) */
  344 +	long errcnt;            /* calibration errors (ro) */
  345 +	long stbcnt;            /* stability limit exceeded (ro) */
  346 +};
  347 +
  348 +The symbolic names of the bits used to select the NTP compatible
  349 +operating modes all start with "MOD_"; extensions start with "ADJ_".
  350 +The specific status of the kernel clock (`status') is also expressed
  351 +by a set of bits, all starting with "STA_".
  352 +
  353 +Thus the classical function "adjtime" can be implemented as follows
  354 +(please note that "struct timeval" is reduced to a single "long",
  355 +causing severe restrictions):
  356 +
  357 +/* This is a simple-minded implementation! */
  358 +inline	int adjtime (const struct timeval *delta, struct timeval *olddelta)
  359 +{
  360 +	struct timex	tntx;
  361 +	int		result;
  362 +	if (delta) {
  363 +		struct timeval tmp;
  364 +
  365 +		/* We enforce some arbitrary (2000s) limit here. */
  366 +		tmp.tv_sec = delta->tv_sec + delta->tv_usec / 1000000L;
  367 +		tmp.tv_usec = delta->tv_usec % 1000000L;
  368 +		if (tmp.tv_sec > 2000 || tmp.tv_sec < -2000) {
  369 +			return -1;
  370 +		}
  371 +		tntx.offset = tmp.tv_usec + tmp.tv_sec * 1000000L;
  372 +		tntx.modes = ADJ_ADJTIME;
  373 +	}
  374 +	else
  375 +		tntx.modes = 0;
  376 +	result = __adjtimex (&tntx);
  377 +	if (result >= 0 && olddelta) {
  378 +		if (tntx.offset < 0) {
  379 +			olddelta->tv_usec = -(-tntx.offset % 1000000);
  380 +			olddelta->tv_sec  = -(-tntx.offset / 1000000);
  381 +		} else {
  382 +			olddelta->tv_usec = tntx.offset % 1000000;
  383 +			olddelta->tv_sec  = tntx.offset / 1000000;
  384 +		}
  385 +	}
  386 +	return result;
  387 +}
  388 +
  389 +The "ntp_gettime" and "ntp_adjtime" functions can be implemented as
  390 +follows:
  391 +
  392 +int ntp_gettime(struct ntptimeval *tptr)
  393 +{
  394 +	struct kernel_timex tx;
  395 +	int result;
  396 +
  397 +	tx.modes = 0;
  398 +	result = adjtimex(&tx);
  399 +	tptr->time = tx.time;
  400 +	tptr->maxerror = tx.maxerror;
  401 +	tptr->esterror = tx.esterror;
  402 +	return(result);
  403 +}
  404 +
  405 +inline	int adjtime (const struct timeval *delta, struct timeval *olddelta)
  406 +{
  407 +	struct timex	tntx;
  408 +	int		result;
  409 +	if (delta) {
  410 +		struct timeval tmp;
  411 +
  412 +		/* We enforce some arbitrary (2000s) limit here. */
  413 +		tmp.tv_sec = delta->tv_sec + delta->tv_usec / 1000000L;
  414 +		tmp.tv_usec = delta->tv_usec % 1000000L;
  415 +		if (tmp.tv_sec > 2000 || tmp.tv_sec < -2000) {
  416 +			return -1;
  417 +		}
  418 +		tntx.offset = tmp.tv_usec + tmp.tv_sec * 1000000L;
  419 +		tntx.modes = ADJ_ADJTIME;
  420 +	}
  421 +	else
  422 +		tntx.modes = 0;
  423 +	result = __adjtimex (&tntx);
  424 +	if (result >= 0 && olddelta) {
  425 +		if (tntx.offset < 0) {
  426 +			olddelta->tv_usec = -(-tntx.offset % 1000000);
  427 +			olddelta->tv_sec  = -(-tntx.offset / 1000000);
  428 +		} else {
  429 +			olddelta->tv_usec = tntx.offset % 1000000;
  430 +			olddelta->tv_sec  = tntx.offset / 1000000;
  431 +		}
  432 +	}
  433 +	return result;
  434 +}
  435 +
  436 +2.3 Problems
  437 +============
  438 +
  439 +The kernel's ``struct timex'' is an equivalent superset of NTP's timex
  440 +structure.
  441 +
  442 +Ulrich Windl
  443 +2003-03-21
  444 Index: linux/Documentation/magic-number.txt
  445 diff -u linux/Documentation/magic-number.txt:1.1.1.4 linux/Documentation/magic-number.txt:1.1.1.4.2.1
  446 --- linux/Documentation/magic-number.txt:1.1.1.4	Sat Aug 26 21:35:38 2006
  447 +++ linux/Documentation/magic-number.txt	Sat Aug 26 21:48:24 2006
  448 @@ -54,6 +54,7 @@
  449  PTY_MAGIC	      0x5001	  (none at the moment)
  450  					            drivers/char/pty.c
  451  PPP_MAGIC             0x5002      ppp               include/linux/if_ppp.h
  452 +PPSCLOCK_MAGIC	      0x5003	  pps		    include/linux/timepps.h
  453  SERIAL_MAGIC          0x5301      async_struct      include/linux/serial.h
  454  SSTATE_MAGIC          0x5302      serial_state      include/linux/serial.h
  455  SLIP_MAGIC            0x5302      slip              drivers/net/slip.h
  456 Index: linux/Documentation/sysctl/kernel.txt
  457 diff -u linux/Documentation/sysctl/kernel.txt:1.1.1.5 linux/Documentation/sysctl/kernel.txt:1.1.1.5.2.1
  458 --- linux/Documentation/sysctl/kernel.txt:1.1.1.5	Sat Aug 26 21:35:39 2006
  459 +++ linux/Documentation/sysctl/kernel.txt	Sat Aug 26 21:48:27 2006
  460 @@ -40,6 +40,7 @@
  461  - sg-big-buff                 [ generic SCSI device (sg) ]
  462  - shmmax                      [ sysv ipc ]
  463  - tainted
  464 +- time			      [ directory ]
  465  - version
  466  - zero-paged                  [ PPC only ]
  467  
  468 @@ -221,6 +222,37 @@
  469  
  470  ==============================================================
  471  
  472 +time:
  473 +
  474 +time/Hz (r/o):
  475 +	Intended to reveal the value of HZ to user programs.
  476 +
  477 +time/pps (r/o):
  478 +	(experimental) Used to reveal the internals of ``struct pps''
  479 +
  480 +time/rtc_runs_localtime (r/w):
  481 +	Set to ``1'' if RTC is set to local time
  482 +
  483 +time/rtc_update (r/w):
  484 +	Amount of seconds after which the RTC is updated from system time
  485 +	if ``STA_UNSYNC'' in ``time_status'' is cleared.  When setting the
  486 +	system time directly, it will be updated the next second.
  487 +	Setting ``rtc_update'' to a value <= 0, the RTC is not updated.
  488 +
  489 +time/tickadj (r/w):
  490 +	Amount of nanoseconds that will be used to adjust a wrong clock
  491 +	gradually (using ``adjtime()'').
  492 +
  493 +time/time_tick (r/w):
  494 +	Amount of nanoseconds to add every timer interrupt.
  495 +
  496 +time/timezone (r/w):
  497 +	Two values describing the time zone offset west of GMT in minutes,
  498 +	and whether daylight saving time is active (required to convert
  499 +	kernel time to local time).
  500 +
  501 +==============================================================
  502 +
  503  tainted: 
  504  
  505  Non-zero if the kernel has been tainted.  Numeric values, which
  506 Index: linux/arch/alpha/config.in
  507 diff -u linux/arch/alpha/config.in:1.1.1.12 linux/arch/alpha/config.in:1.1.1.12.6.1
  508 --- linux/arch/alpha/config.in:1.1.1.12	Sat Jan 22 21:52:43 2005
  509 +++ linux/arch/alpha/config.in	Sat Aug 26 21:48:30 2006
  510 @@ -221,6 +221,13 @@
  511  	define_bool CONFIG_ALPHA_EV6 y
  512  	define_bool CONFIG_ALPHA_EV67 y
  513  fi
  514 +bool 'NTP kernel support' CONFIG_NTP
  515 +if [ "$CONFIG_NTP" != "n" ]; then
  516 +  bool '   NTP PPS support' CONFIG_NTP_PPS
  517 +  if [ "$CONFIG_NTP_PPS" != "n" ]; then
  518 +    bool '   Debug NTP PPS support' CONFIG_NTP_PPS_DEBUG
  519 +  fi
  520 +fi
  521  
  522  if [ "$CONFIG_ALPHA_JENSEN" = "y" -o "$CONFIG_ALPHA_MIKASA" = "y" \
  523  	-o "$CONFIG_ALPHA_SABLE" = "y" -o "$CONFIG_ALPHA_NORITAKE" = "y" \
  524 Index: linux/arch/alpha/defconfig
  525 diff -u linux/arch/alpha/defconfig:1.1.1.9 linux/arch/alpha/defconfig:1.1.1.9.2.1
  526 --- linux/arch/alpha/defconfig:1.1.1.9	Sat Aug 26 21:34:46 2006
  527 +++ linux/arch/alpha/defconfig	Sat Aug 26 21:48:30 2006
  528 @@ -74,6 +74,10 @@
  529  CONFIG_BINFMT_ELF=y
  530  # CONFIG_BINFMT_MISC is not set
  531  # CONFIG_BINFMT_EM86 is not set
  532 +CONFIG_NTP=y
  533 +# CONFIG_NTP_PPS is not set
  534 +# CONFIG_NTP_PPS_DEBUG is not set
  535 +# CONFIG_NTP_PPS_SERIAL is not set
  536  
  537  #
  538  # Parallel port support
  539 Index: linux/arch/alpha/kernel/osf_sys.c
  540 diff -u linux/arch/alpha/kernel/osf_sys.c:1.1.1.12 linux/arch/alpha/kernel/osf_sys.c:1.1.1.12.2.1
  541 --- linux/arch/alpha/kernel/osf_sys.c:1.1.1.12	Sat Aug 26 21:34:47 2006
  542 +++ linux/arch/alpha/kernel/osf_sys.c	Sat Aug 26 21:48:33 2006
  543 @@ -1293,17 +1293,20 @@
  544  
  545  	/* copy relevant bits of struct timex. */
  546  	if (copy_from_user(&txc, txc_p, offsetof(struct timex32, time)) ||
  547 -	    copy_from_user(&txc.tick, &txc_p->tick, sizeof(struct timex32) - 
  548 +	    copy_from_user(&txc.time_tick, &txc_p->tick, sizeof(struct timex32) - 
  549  			   offsetof(struct timex32, time)))
  550  	  return -EFAULT;
  551  
  552 +	txc.time_tick *= 1000;
  553 +	/* NOTE[UW]: time_tick is nanoseconds */
  554  	ret = do_adjtimex(&txc);	
  555  	if (ret < 0)
  556  	  return ret;
  557 +	txc.time_tick /= 1000;
  558  	
  559  	/* copy back to timex32 */
  560  	if (copy_to_user(txc_p, &txc, offsetof(struct timex32, time)) ||
  561 -	    (copy_to_user(&txc_p->tick, &txc.tick, sizeof(struct timex32) - 
  562 +	    (copy_to_user(&txc_p->tick, &txc.time_tick, sizeof(struct timex32) - 
  563  			  offsetof(struct timex32, tick))) ||
  564  	    (put_tv32(&txc_p->time, &txc.time)))
  565  	  return -EFAULT;
  566 Index: linux/arch/alpha/kernel/time.c
  567 diff -u linux/arch/alpha/kernel/time.c:1.1.1.10 linux/arch/alpha/kernel/time.c:1.1.1.10.2.1
  568 --- linux/arch/alpha/kernel/time.c:1.1.1.10	Sat Aug 26 21:34:47 2006
  569 +++ linux/arch/alpha/kernel/time.c	Sat Aug 26 21:48:33 2006
  570 @@ -18,16 +18,35 @@
  571   *      fixed tick loss calculation in timer_interrupt
  572   *      (round system clock to nearest tick instead of truncating)
  573   *      fixed algorithm in time_init for getting time from CMOS clock
  574 + * 1999-03-10	Ulrich Windl
  575 + *	Updated for nanosecond resolution and general cleanup
  576   * 1999-04-16	Thorsten Kranzkowski (dl8bcu@gmx.net)
  577   *	fixed algorithm in do_gettimeofday() for calculating the precise time
  578   *	from processor cycle counter (now taking lost_ticks into account)
  579 + * 1999-04-29	Ulrich Windl
  580 + *	Merged recent changes.
  581 + * 1999-12-10	Ulrich Windl
  582 + *	Implement new logic with ``rtc_update_slave''.
  583 + *	Use new ``update_rtc()'' (untested).
  584 + * 2000-03-16	Ulrich Windl, John Sager
  585 + *	Fixed compilation.
  586 + * 2000-04-16	Ulrich Windl
  587 + *	Don't take ``lost_ticks'' into account here. It's done in
  588 + *	``kernel/time.c'' now..
  589   * 2000-08-13	Jan-Benedict Glaw <jbglaw@lug-owl.de>
  590   * 	Fixed time_init to be aware of epoches != 1900. This prevents
  591   * 	booting up in 2048 for me;) Code is stolen from rtc.c.
  592 + * 2000-12-03	Ulrich Windl
  593 + *	Re-merged code from 2.2.17.
  594 + * 2000-12-09	Ulrich Windl
  595 + *	Remove traditional code for microseconds as the nanoseconds seem
  596 + *	stable.
  597 + * 2001-03-28	Ulrich Windl
  598 + *	Another cross-merge to fix compilation problems.
  599   */
  600  #include <linux/config.h>
  601  #include <linux/errno.h>
  602 -#include <linux/sched.h>
  603 +#include <linux/time.h>
  604  #include <linux/kernel.h>
  605  #include <linux/param.h>
  606  #include <linux/string.h>
  607 @@ -49,12 +68,11 @@
  608  #include "irq_impl.h"
  609  
  610  extern rwlock_t xtime_lock;
  611 -extern unsigned long wall_jiffies;	/* kernel/timer.c */
  612 -
  613 -static int set_rtc_mmss(unsigned long);
  614  
  615  spinlock_t rtc_lock = SPIN_LOCK_UNLOCKED;
  616  
  617 +static void update_rtc(void);
  618 +
  619  /*
  620   * Shift amount by which scaled_ticks_per_cycle is scaled.  Shifting
  621   * by 48 gives us 16 bits for HZ while keeping the accuracy good even
  622 @@ -86,6 +104,30 @@
  623  
  624  
  625  /*
  626 + * Use the cycle counter to estimate an displacement from the last time
  627 + * tick.  Unfortunately the Alpha designers made only the low 32-bits of
  628 + * the cycle counter active, so we overflow on 8.2 seconds on a 500MHz
  629 + * part.  So we can't do the "find absolute time in terms of cycles" thing
  630 + * that the other ports do.
  631 + */
  632 +/* nanoseconds since last timer interrupt */
  633 +static inline unsigned long do_exact_nanotime(void)
  634 +{
  635 +	unsigned long delta_usec;
  636 +
  637 +#ifdef CONFIG_SMP
  638 +	delta_usec = (jiffies - wall_jiffies) * (1000000 / HZ);
  639 +#else
  640 +	delta_usec = rpcc() - state.last_time;
  641 +	delta_usec = (delta_usec * state.scaled_ticks_per_cycle 
  642 +		      + state.partial_tick
  643 +		      + ((jiffies - wall_jiffies) << FIX_SHIFT)) * 15625;
  644 +	delta_usec = ((delta_usec / ((1UL << (FIX_SHIFT-6-1)) * HZ)) + 1) / 2;
  645 +#endif
  646 +	return delta_usec * 1000;
  647 +}
  648 +
  649 +/*
  650   * timer_interrupt() needs to keep up the real-time clock,
  651   * as well as call the "do_timer()" routine every clocktick
  652   */
  653 @@ -121,18 +163,18 @@
  654  	}
  655  
  656  	/*
  657 -	 * If we have an externally synchronized Linux clock, then update
  658 -	 * CMOS clock accordingly every ~11 minutes. Set_rtc_mmss() has to be
  659 -	 * called as close as possible to 500 ms before the new second starts.
  660 +	 * If we have a change of the software clock to be committed to the
  661 +	 * CMOS clock, then do it. Update_rtc() has to be called as close as
  662 +	 * possible to 500 ms before the new second starts.
  663  	 */
  664 -	if ((time_status & STA_UNSYNC) == 0
  665 -	    && xtime.tv_sec > state.last_rtc_update + 660
  666 -	    && xtime.tv_usec >= 500000 - ((unsigned) tick) / 2
  667 -	    && xtime.tv_usec <= 500000 + ((unsigned) tick) / 2) {
  668 -		int tmp = set_rtc_mmss(xtime.tv_sec);
  669 -		state.last_rtc_update = xtime.tv_sec - (tmp ? 600 : 0);
  670 +	if (rtc_update_slave &&
  671 +	    xtime.tv_sec >= state.last_rtc_update + rtc_update_slave &&
  672 +	    xtime.tv_nsec >= 500000000 - ((unsigned long) time_tick) / 2 &&
  673 +	    xtime.tv_nsec <= 500000000 + ((unsigned long) time_tick) / 2) {
  674 +		update_rtc();
  675 +		state.last_rtc_update = xtime.tv_sec;
  676 +		rtc_update_slave = 0;
  677  	}
  678 -
  679  	write_unlock(&xtime_lock);
  680  }
  681  
  682 @@ -141,6 +183,8 @@
  683  {
  684  	unsigned char x;
  685  
  686 +	do_nanotime = do_exact_nanotime;
  687 +
  688  	/* Reset periodic interrupt frequency.  */
  689  	x = CMOS_READ(RTC_FREQ_SELECT) & 0x3f;
  690  	/* Test includes known working values on various platforms
  691 @@ -365,7 +409,7 @@
  692  		year += 100;
  693  
  694  	xtime.tv_sec = mktime(year, mon, day, hour, min, sec);
  695 -	xtime.tv_usec = 0;
  696 +	xtime.tv_nsec = 0;
  697  
  698  	if (HZ > (1<<16)) {
  699  		extern void __you_loose (void);
  700 @@ -378,110 +422,14 @@
  701  	state.last_rtc_update = 0;
  702  	state.partial_tick = 0L;
  703  
  704 +	timevar_init();
  705 +
  706  	/* Startup the timer source. */
  707  	alpha_mv.init_rtc();
  708  }
  709  
  710  /*
  711 - * Use the cycle counter to estimate an displacement from the last time
  712 - * tick.  Unfortunately the Alpha designers made only the low 32-bits of
  713 - * the cycle counter active, so we overflow on 8.2 seconds on a 500MHz
  714 - * part.  So we can't do the "find absolute time in terms of cycles" thing
  715 - * that the other ports do.
  716 - */
  717 -void
  718 -do_gettimeofday(struct timeval *tv)
  719 -{
  720 -	unsigned long sec, usec, lost, flags;
  721 -	unsigned long delta_cycles, delta_usec, partial_tick;
  722 -
  723 -	read_lock_irqsave(&xtime_lock, flags);
  724 -
  725 -	delta_cycles = rpcc() - state.last_time;
  726 -	sec = xtime.tv_sec;
  727 -	usec = xtime.tv_usec;
  728 -	partial_tick = state.partial_tick;
  729 -	lost = jiffies - wall_jiffies;
  730 -
  731 -	read_unlock_irqrestore(&xtime_lock, flags);
  732 -
  733 -#ifdef CONFIG_SMP
  734 -	/* Until and unless we figure out how to get cpu cycle counters
  735 -	   in sync and keep them there, we can't use the rpcc tricks.  */
  736 -	delta_usec = lost * (1000000 / HZ);
  737 -#else
  738 -	/*
  739 -	 * usec = cycles * ticks_per_cycle * 2**48 * 1e6 / (2**48 * ticks)
  740 -	 *	= cycles * (s_t_p_c) * 1e6 / (2**48 * ticks)
  741 -	 *	= cycles * (s_t_p_c) * 15625 / (2**42 * ticks)
  742 -	 *
  743 -	 * which, given a 600MHz cycle and a 1024Hz tick, has a
  744 -	 * dynamic range of about 1.7e17, which is less than the
  745 -	 * 1.8e19 in an unsigned long, so we are safe from overflow.
  746 -	 *
  747 -	 * Round, but with .5 up always, since .5 to even is harder
  748 -	 * with no clear gain.
  749 -	 */
  750 -
  751 -	delta_usec = (delta_cycles * state.scaled_ticks_per_cycle 
  752 -		      + partial_tick
  753 -		      + (lost << FIX_SHIFT)) * 15625;
  754 -	delta_usec = ((delta_usec / ((1UL << (FIX_SHIFT-6-1)) * HZ)) + 1) / 2;
  755 -#endif
  756 -
  757 -	usec += delta_usec;
  758 -	if (usec >= 1000000) {
  759 -		sec += 1;
  760 -		usec -= 1000000;
  761 -	}
  762 -
  763 -	tv->tv_sec = sec;
  764 -	tv->tv_usec = usec;
  765 -}
  766 -
  767 -void
  768 -do_settimeofday(struct timeval *tv)
  769 -{
  770 -	unsigned long delta_usec;
  771 -	long sec, usec;
  772 -	
  773 -	write_lock_irq(&xtime_lock);
  774 -
  775 -	/* The offset that is added into time in do_gettimeofday above
  776 -	   must be subtracted out here to keep a coherent view of the
  777 -	   time.  Without this, a full-tick error is possible.  */
  778 -
  779 -#ifdef CONFIG_SMP
  780 -	delta_usec = (jiffies - wall_jiffies) * (1000000 / HZ);
  781 -#else
  782 -	delta_usec = rpcc() - state.last_time;
  783 -	delta_usec = (delta_usec * state.scaled_ticks_per_cycle 
  784 -		      + state.partial_tick
  785 -		      + ((jiffies - wall_jiffies) << FIX_SHIFT)) * 15625;
  786 -	delta_usec = ((delta_usec / ((1UL << (FIX_SHIFT-6-1)) * HZ)) + 1) / 2;
  787 -#endif
  788 -
  789 -	sec = tv->tv_sec;
  790 -	usec = tv->tv_usec;
  791 -	usec -= delta_usec;
  792 -	if (usec < 0) {
  793 -		usec += 1000000;
  794 -		sec -= 1;
  795 -	}
  796 -
  797 -	xtime.tv_sec = sec;
  798 -	xtime.tv_usec = usec;
  799 -	time_adjust = 0;		/* stop active adjtime() */
  800 -	time_status |= STA_UNSYNC;
  801 -	time_maxerror = NTP_PHASE_LIMIT;
  802 -	time_esterror = NTP_PHASE_LIMIT;
  803 -
  804 -	write_unlock_irq(&xtime_lock);
  805 -}
  806 -
  807 -
  808 -/*
  809 - * In order to set the CMOS clock precisely, set_rtc_mmss has to be
  810 + * In order to set the CMOS clock precisely, update_rtc has to be
  811   * called 500 ms after the second nowtime has started, because when
  812   * nowtime is written into the registers of the CMOS clock, it will
  813   * jump to the next second precisely 500 ms later. Check the Motorola
  814 @@ -490,15 +438,11 @@
  815   * BUG: This routine does not handle hour overflow properly; it just
  816   *      sets the minutes. Usually you won't notice until after reboot!
  817   */
  818 -
  819 -extern int abs(int);
  820 -
  821 -static int
  822 -set_rtc_mmss(unsigned long nowtime)
  823 +static void
  824 +update_rtc(void)
  825  {
  826 -	int retval = 0;
  827 -	int real_seconds, real_minutes, cmos_minutes;
  828  	unsigned char save_control, save_freq_select;
  829 +	int ss, mi, hh, dd, mo, yy;
  830  
  831  	/* irq are locally disabled here */
  832  	spin_lock(&rtc_lock);
  833 @@ -510,37 +454,29 @@
  834  	save_freq_select = CMOS_READ(RTC_FREQ_SELECT);
  835  	CMOS_WRITE((save_freq_select|RTC_DIV_RESET2), RTC_FREQ_SELECT);
  836  
  837 -	cmos_minutes = CMOS_READ(RTC_MINUTES);
  838 -	if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
  839 -		BCD_TO_BIN(cmos_minutes);
  840 -
  841 -	/*
  842 -	 * since we're only adjusting minutes and seconds,
  843 -	 * don't interfere with hour overflow. This avoids
  844 -	 * messing with unknown time zones but requires your
  845 -	 * RTC not to be off by more than 15 minutes
  846 -	 */
  847 -	real_seconds = nowtime % 60;
  848 -	real_minutes = nowtime / 60;
  849 -	if (((abs(real_minutes - cmos_minutes) + 15)/30) & 1) {
  850 -		/* correct for half hour time zone */
  851 -		real_minutes += 30;
  852 -	}
  853 -	real_minutes %= 60;
  854 -
  855 -	if (abs(real_minutes - cmos_minutes) < 30) {
  856 -		if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
  857 -			BIN_TO_BCD(real_seconds);
  858 -			BIN_TO_BCD(real_minutes);
  859 -		}
  860 -		CMOS_WRITE(real_seconds,RTC_SECONDS);
  861 -		CMOS_WRITE(real_minutes,RTC_MINUTES);
  862 -	} else {
  863 -		printk(KERN_WARNING
  864 -		       "set_rtc_mmss: can't update from %d to %d\n",
  865 -		       cmos_minutes, real_minutes);
  866 - 		retval = -1;
  867 -	}
  868 +	ktime_to_rtc(xtime.tv_sec, &ss, &dd);
  869 +	hh = ss >> 16;
  870 +	mi = (ss >> 8) & 0xff;
  871 +	ss &= 0xff;
  872 +	yy = (dd >> 16) % 100;
  873 +	mo = (dd >> 8) & 0xff;
  874 +	dd &= 0xff;
  875 +	printk(KERN_DEBUG "update_rtc: %02d-%02d-%02d %02d:%02d:%02d\n",
  876 +	       yy, mo, dd, hh, mi, ss);
  877 +	if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
  878 +		BIN_TO_BCD(ss);
  879 +		BIN_TO_BCD(mi);
  880 +		BIN_TO_BCD(hh);
  881 +		BIN_TO_BCD(dd);
  882 +		BIN_TO_BCD(mo);
  883 +		BIN_TO_BCD(yy);
  884 +	}
  885 +	CMOS_WRITE(ss, RTC_SECONDS);
  886 +	CMOS_WRITE(mi, RTC_MINUTES);
  887 +	CMOS_WRITE(hh, RTC_HOURS);
  888 +	CMOS_WRITE(dd, RTC_DAY_OF_MONTH);
  889 +	CMOS_WRITE(mo, RTC_MONTH);
  890 +	CMOS_WRITE(yy, RTC_YEAR);
  891  
  892  	/* The following flags have to be released exactly in this order,
  893  	 * otherwise the DS12887 (popular MC146818A clone with integrated
  894 @@ -552,6 +488,12 @@
  895  	CMOS_WRITE(save_control, RTC_CONTROL);
  896  	CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
  897  	spin_unlock(&rtc_lock);
  898 +}
  899  
  900 -	return retval;
  901 +/* Fine-tune the tick interpolation for a total frequency error
  902 + * `freq' / 1000 PPM.
  903 + */
  904 +void update_nanoscale(long freq)
  905 +{
  906 +	/* not implemented -- this is just a prototype [UW] */
  907  }
  908 Index: linux/arch/arm/config.in
  909 diff -u linux/arch/arm/config.in:1.1.1.10 linux/arch/arm/config.in:1.1.1.10.6.1
  910 --- linux/arch/arm/config.in:1.1.1.10	Sat Jan 22 21:52:45 2005
  911 +++ linux/arch/arm/config.in	Sat Aug 26 21:48:36 2006
  912 @@ -178,6 +178,13 @@
  913  if [ "$CONFIG_ARCH_GUIDEA07" = "y" ]; then
  914     define_bool CONFIG_ARCH_CDB89712 y
  915  fi
  916 +bool 'NTP kernel support' CONFIG_NTP
  917 +if [ "$CONFIG_NTP" != "n" ]; then
  918 +  bool '   NTP PPS support' CONFIG_NTP_PPS
  919 +  if [ "$CONFIG_NTP_PPS" != "n" ]; then
  920 +    bool '   Debug NTP PPS support' CONFIG_NTP_PPS_DEBUG
  921 +  fi
  922 +fi
  923  
  924  endmenu
  925  
  926 Index: linux/arch/arm/defconfig
  927 diff -u linux/arch/arm/defconfig:1.1.1.3 linux/arch/arm/defconfig:1.1.1.3.6.1
  928 --- linux/arch/arm/defconfig:1.1.1.3	Sat Jan 22 21:52:45 2005
  929 +++ linux/arch/arm/defconfig	Sat Aug 26 21:48:36 2006
  930 @@ -88,6 +88,10 @@
  931  # CONFIG_BINFMT_MISC is not set
  932  # CONFIG_PM is not set
  933  # CONFIG_ARTHUR is not set
  934 +CONFIG_NTP=y
  935 +# CONFIG_NTP_PPS is not set
  936 +# CONFIG_NTP_PPS_DEBUG is not set
  937 +# CONFIG_NTP_PPS_SERIAL is not set
  938  CONFIG_CMDLINE="root=1f04 mem=32M"
  939  CONFIG_LEDS=y
  940  CONFIG_LEDS_TIMER=y
  941 Index: linux/arch/arm/kernel/time.c
  942 diff -u linux/arch/arm/kernel/time.c:1.1.1.8 linux/arch/arm/kernel/time.c:1.1.1.8.2.1
  943 --- linux/arch/arm/kernel/time.c:1.1.1.8	Sat Aug 26 21:34:34 2006
  944 +++ linux/arch/arm/kernel/time.c	Sat Aug 26 21:48:39 2006
  945 @@ -15,6 +15,11 @@
  946   *              fixed set_rtc_mmss, fixed time.year for >= 2000, new mktime
  947   *  1998-12-20  Updated NTP code according to technical memorandum Jan '96
  948   *              "A Kernel Model for Precision Timekeeping" by Dave Mills
  949 + * 1999-03-10	Ulrich Windl
  950 + *	Updated for nanosecond resolution (untested)
  951 + * 2000-12-09	Ulrich Windl
  952 + *	Remove traditional code for microseconds as the nanoseconds seem
  953 + *	stable.
  954   */
  955  #include <linux/config.h>
  956  #include <linux/module.h>
  957 @@ -103,12 +108,14 @@
  958  	if (time_status & STA_UNSYNC || set_rtc == NULL)
  959  		return;
  960  
  961 +	/* [UW] the new update logic should be merged from
  962 +	   arch/i386/kernel/time.c */
  963  	if (next_rtc_update &&
  964  	    time_before(xtime.tv_sec, next_rtc_update))
  965  		return;
  966  
  967 -	if (xtime.tv_usec < 50000 - (tick >> 1) &&
  968 -	    xtime.tv_usec >= 50000 + (tick >> 1))
  969 +	if (xtime.tv_nsec < 50000000 - (time_tick >> 1) &&
  970 +	    xtime.tv_nsec >= 50000000 + (time_tick >> 1))
  971  		return;
  972  
  973  	if (set_rtc())
  974 @@ -149,58 +156,6 @@
  975  #define do_leds()
  976  #endif
  977  
  978 -void do_gettimeofday(struct timeval *tv)
  979 -{
  980 -	unsigned long flags;
  981 -	unsigned long usec, sec;
  982 -
  983 -	read_lock_irqsave(&xtime_lock, flags);
  984 -	usec = gettimeoffset();
  985 -	{
  986 -		unsigned long lost = jiffies - wall_jiffies;
  987 -
  988 -		if (lost)
  989 -			usec += lost * USECS_PER_JIFFY;
  990 -	}
  991 -	sec = xtime.tv_sec;
  992 -	usec += xtime.tv_usec;
  993 -	read_unlock_irqrestore(&xtime_lock, flags);
  994 -
  995 -	/* usec may have gone up a lot: be safe */
  996 -	while (usec >= 1000000) {
  997 -		usec -= 1000000;
  998 -		sec++;
  999 -	}
 1000 -
 1001 -	tv->tv_sec = sec;
 1002 -	tv->tv_usec = usec;
 1003 -}
 1004 -
 1005 -void do_settimeofday(struct timeval *tv)
 1006 -{
 1007 -	write_lock_irq(&xtime_lock);
 1008 -	/* This is revolting. We need to set the xtime.tv_usec
 1009 -	 * correctly. However, the value in this location is
 1010 -	 * is value at the last tick.
 1011 -	 * Discover what correction gettimeofday
 1012 -	 * would have done, and then undo it!
 1013 -	 */
 1014 -	tv->tv_usec -= gettimeoffset();
 1015 -	tv->tv_usec -= (jiffies - wall_jiffies) * USECS_PER_JIFFY;
 1016 -
 1017 -	while (tv->tv_usec < 0) {
 1018 -		tv->tv_usec += 1000000;
 1019 -		tv->tv_sec--;
 1020 -	}
 1021 -
 1022 -	xtime = *tv;
 1023 -	time_adjust = 0;		/* stop active adjtime() */
 1024 -	time_status |= STA_UNSYNC;
 1025 -	time_maxerror = NTP_PHASE_LIMIT;
 1026 -	time_esterror = NTP_PHASE_LIMIT;
 1027 -	write_unlock_irq(&xtime_lock);
 1028 -}
 1029 -
 1030  static struct irqaction timer_irq = {
 1031  	.name	= "timer",
 1032  	.flags	= SA_INTERRUPT,
 1033 @@ -211,6 +166,17 @@
 1034   */
 1035  #include <asm/arch/time.h>
 1036  
 1037 +/* nanoseconds since last timer interrupt */
 1038 +static inline unsigned long do_exact_nanotime(void)
 1039 +{
 1040 +	return gettimeoffset() * 1000;
 1041 +}
 1042 +
 1043 +static unsigned long do_gettimeoffset(void)
 1044 +{
 1045 +	return gettimeoffset ();
 1046 +}
 1047 +
 1048  /*
 1049   * This must cause the timer to start ticking.
 1050   * It doesn't have to set the current time though
 1051 @@ -219,8 +185,18 @@
 1052   */
 1053  void __init time_init(void)
 1054  {
 1055 -	xtime.tv_usec = 0;
 1056 +	do_nanotime = do_exact_nanotime;
 1057 +	xtime.tv_nsec = 0;
 1058  	xtime.tv_sec  = 0;
 1059  
 1060 +	timevar_init();		/* architecture independent initialization */
 1061  	setup_timer();
 1062  }
 1063 +
 1064 +/* Fine-tune the tick interpolation for a total frequency error
 1065 + * `freq' / 1000 PPM.
 1066 + */
 1067 +void update_nanoscale(long freq)
 1068 +{
 1069 +	/* not implemented -- this is just a prototype [UW] */
 1070 +}
 1071 Index: linux/arch/cris/kernel/time.c
 1072 diff -u linux/arch/cris/kernel/time.c:1.1.1.11 linux/arch/cris/kernel/time.c:1.1.1.11.2.1
 1073 --- linux/arch/cris/kernel/time.c:1.1.1.11	Sat Aug 26 21:34:37 2006
 1074 +++ linux/arch/cris/kernel/time.c	Sat Aug 26 21:48:42 2006
 1075 @@ -658,3 +658,11 @@
 1076  		IO_STATE(R_VECT_MASK_SET, nmi, set);
 1077  #endif
 1078  }
 1079 +
 1080 +/* Fine-tune the tick interpolation for a total frequency error
 1081 + * `freq' / 1000 PPM.
 1082 + */
 1083 +void update_nanoscale(long freq)
 1084 +{
 1085 +	/* not implemented -- this is just a prototype [UW] */
 1086 +}
 1087 Index: linux/arch/i386/config.in
 1088 diff -u linux/arch/i386/config.in:1.1.1.16 linux/arch/i386/config.in:1.1.1.16.2.1
 1089 --- linux/arch/i386/config.in:1.1.1.16	Sat Aug 26 21:34:36 2006
 1090 +++ linux/arch/i386/config.in	Sat Aug 26 21:48:44 2006
 1091 @@ -223,6 +223,13 @@
 1092  if [ "$CONFIG_HIGHMEM" = "y" ]; then
 1093     bool 'HIGHMEM I/O support' CONFIG_HIGHIO
 1094  fi
 1095 +bool 'NTP kernel support' CONFIG_NTP
 1096 +if [ "$CONFIG_NTP" != "n" ]; then
 1097 +  bool '   NTP PPS support' CONFIG_NTP_PPS
 1098 +  if [ "$CONFIG_NTP_PPS" != "n" ]; then
 1099 +    bool '   Debug NTP PPS support' CONFIG_NTP_PPS_DEBUG
 1100 +  fi
 1101 +fi
 1102  
 1103  bool 'Math emulation' CONFIG_MATH_EMULATION
 1104  bool 'MTRR (Memory Type Range Register) support' CONFIG_MTRR
 1105 Index: linux/arch/i386/defconfig
 1106 diff -u linux/arch/i386/defconfig:1.1.1.20 linux/arch/i386/defconfig:1.1.1.20.2.1
 1107 --- linux/arch/i386/defconfig:1.1.1.20	Sat Aug 26 21:34:36 2006
 1108 +++ linux/arch/i386/defconfig	Sat Aug 26 21:48:44 2006
 1109 @@ -119,6 +119,10 @@
 1110  CONFIG_BINFMT_ELF=y
 1111  CONFIG_BINFMT_MISC=y
 1112  # CONFIG_OOM_KILLER is not set
 1113 +CONFIG_NTP=y
 1114 +# CONFIG_NTP_PPS is not set
 1115 +# CONFIG_NTP_PPS_DEBUG is not set
 1116 +# CONFIG_NTP_PPS_SERIAL is not set
 1117  CONFIG_PM=y
 1118  # CONFIG_APM is not set
 1119  
 1120 Index: linux/arch/i386/kernel/apm.c
 1121 diff -u linux/arch/i386/kernel/apm.c:1.1.1.17 linux/arch/i386/kernel/apm.c:1.1.1.17.2.1
 1122 --- linux/arch/i386/kernel/apm.c:1.1.1.17	Sat Aug 26 21:34:37 2006
 1123 +++ linux/arch/i386/kernel/apm.c	Sat Aug 26 21:48:47 2006
 1124 @@ -308,9 +308,9 @@
 1125  #undef INIT_TIMER_AFTER_SUSPEND
 1126  
 1127  #ifdef INIT_TIMER_AFTER_SUSPEND
 1128 -#include <linux/timex.h>
 1129 -#include <asm/io.h>
 1130 +#include <linux/time.h>
 1131  #include <linux/delay.h>
 1132 +#include <asm/io.h>
 1133  #endif
 1134  
 1135  /*
 1136 @@ -386,13 +386,6 @@
 1137  static int			ignore_normal_resume;
 1138  static int			bounce_interval = DEFAULT_BOUNCE_INTERVAL;
 1139  
 1140 -#ifdef CONFIG_APM_RTC_IS_GMT
 1141 -#	define	clock_cmos_diff	0
 1142 -#	define	got_clock_diff	1
 1143 -#else
 1144 -static long			clock_cmos_diff;
 1145 -static int			got_clock_diff;
 1146 -#endif
 1147  static int			debug;
 1148  static int			smp = 0;
 1149  static int			apm_disabled = -1;
 1150 @@ -1188,29 +1181,10 @@
 1151  {
 1152  	unsigned long	flags;
 1153  
 1154 -	if (got_clock_diff) {	/* Must know time zone in order to set clock */
 1155 -		save_flags(flags);
 1156 -		cli();
 1157 -		CURRENT_TIME = get_cmos_time() + clock_cmos_diff;
 1158 -		restore_flags(flags);
 1159 -	}
 1160 -}
 1161 -
 1162 -static void get_time_diff(void)
 1163 -{
 1164 -#ifndef CONFIG_APM_RTC_IS_GMT
 1165 -	unsigned long	flags;
 1166 -
 1167 -	/*
 1168 -	 * Estimate time zone so that set_time can update the clock
 1169 -	 */
 1170  	save_flags(flags);
 1171 -	clock_cmos_diff = -get_cmos_time();
 1172  	cli();
 1173 -	clock_cmos_diff += CURRENT_TIME;
 1174 -	got_clock_diff = 1;
 1175 +	CURRENT_TIME = get_cmos_time();
 1176  	restore_flags(flags);
 1177 -#endif
 1178  }
 1179  
 1180  static void reinit_timer(void)
 1181 @@ -1248,7 +1222,11 @@
 1182  		}
 1183  		printk(KERN_CRIT "apm: suspend was vetoed, but suspending anyway.\n");
 1184  	}
 1185 +#ifndef CONFIG_NTP
 1186  	get_time_diff();
 1187 +#else
 1188 +	printk(KERN_WARNING "apm: get_time_diff() not called\n");	
 1189 +#endif
 1190  	__cli();
 1191  	err = set_system_power_state(APM_STATE_SUSPEND);
 1192  	reinit_timer();
 1193 @@ -1276,7 +1254,11 @@
 1194  	int	err;
 1195  
 1196  	/* If needed, notify drivers here */
 1197 +#ifndef CONFIG_NTP
 1198  	get_time_diff();
 1199 +#else
 1200 +	printk(KERN_WARNING "apm: get_time_diff() not called\n");	
 1201 +#endif
 1202  	err = set_system_power_state(APM_STATE_STANDBY);
 1203  	if ((err != APM_SUCCESS) && (err != APM_NO_ERROR))
 1204  		apm_error("standby", err);
 1205 Index: linux/arch/i386/kernel/i386_ksyms.c
 1206 diff -u linux/arch/i386/kernel/i386_ksyms.c:1.1.1.17 linux/arch/i386/kernel/i386_ksyms.c:1.1.1.17.2.1
 1207 --- linux/arch/i386/kernel/i386_ksyms.c:1.1.1.17	Sat Aug 26 21:34:37 2006
 1208 +++ linux/arch/i386/kernel/i386_ksyms.c	Sat Aug 26 21:48:47 2006
 1209 @@ -32,6 +32,7 @@
 1210  
 1211  extern void dump_thread(struct pt_regs *, struct user *);
 1212  extern spinlock_t rtc_lock;
 1213 +extern spinlock_t i8253_lock;
 1214  
 1215  #if defined(CONFIG_APM) || defined(CONFIG_APM_MODULE)
 1216  extern void machine_real_restart(unsigned char *, int);
 1217 @@ -167,6 +168,7 @@
 1218  EXPORT_SYMBOL(get_wchan);
 1219  
 1220  EXPORT_SYMBOL(rtc_lock);
 1221 +EXPORT_SYMBOL(i8253_lock);
 1222  
 1223  #undef memcpy
 1224  #undef memset
 1225 Index: linux/arch/i386/kernel/i8259.c
 1226 diff -u linux/arch/i386/kernel/i8259.c:1.1.1.7 linux/arch/i386/kernel/i8259.c:1.1.1.7.2.1
 1227 --- linux/arch/i386/kernel/i8259.c:1.1.1.7	Sat Aug 26 21:34:37 2006
 1228 +++ linux/arch/i386/kernel/i8259.c	Sat Aug 26 21:48:47 2006
 1229 @@ -5,7 +5,7 @@
 1230  #include <linux/sched.h>
 1231  #include <linux/ioport.h>
 1232  #include <linux/interrupt.h>
 1233 -#include <linux/timex.h>
 1234 +#include <linux/time.h>
 1235  #include <linux/slab.h>
 1236  #include <linux/random.h>
 1237  #include <linux/smp_lock.h>
 1238 Index: linux/arch/i386/kernel/irq.c
 1239 diff -u linux/arch/i386/kernel/irq.c:1.1.1.11 linux/arch/i386/kernel/irq.c:1.1.1.11.2.1
 1240 --- linux/arch/i386/kernel/irq.c:1.1.1.11	Sat Aug 26 21:34:37 2006
 1241 +++ linux/arch/i386/kernel/irq.c	Sat Aug 26 21:48:47 2006
 1242 @@ -24,7 +24,7 @@
 1243  #include <linux/sched.h>
 1244  #include <linux/ioport.h>
 1245  #include <linux/interrupt.h>
 1246 -#include <linux/timex.h>
 1247 +#include <linux/time.h>
 1248  #include <linux/slab.h>
 1249  #include <linux/random.h>
 1250  #include <linux/smp_lock.h>
 1251 Index: linux/arch/i386/kernel/setup.c
 1252 diff -u linux/arch/i386/kernel/setup.c:1.1.1.21 linux/arch/i386/kernel/setup.c:1.1.1.21.2.1
 1253 --- linux/arch/i386/kernel/setup.c:1.1.1.21	Sat Aug 26 21:34:37 2006
 1254 +++ linux/arch/i386/kernel/setup.c	Sat Aug 26 21:48:47 2006
 1255 @@ -111,6 +111,7 @@
 1256  #include <asm/mtrr.h>
 1257  #include <asm/uaccess.h>
 1258  #include <asm/system.h>
 1259 +#include <asm/timex.h>	/* cpu_khz */
 1260  #include <asm/io.h>
 1261  #include <asm/smp.h>
 1262  #include <asm/cobalt.h>
 1263 Index: linux/arch/i386/kernel/smpboot.c
 1264 diff -u linux/arch/i386/kernel/smpboot.c:1.1.1.16 linux/arch/i386/kernel/smpboot.c:1.1.1.16.2.1
 1265 --- linux/arch/i386/kernel/smpboot.c:1.1.1.16	Sat Aug 26 21:34:37 2006
 1266 +++ linux/arch/i386/kernel/smpboot.c	Sat Aug 26 21:48:47 2006
 1267 @@ -43,6 +43,7 @@
 1268  
 1269  #include <linux/delay.h>
 1270  #include <linux/mc146818rtc.h>
 1271 +#include <asm/timex.h>	/* cpu_khz */
 1272  #include <asm/mtrr.h>
 1273  #include <asm/pgalloc.h>
 1274  #include <asm/smpboot.h>
 1275 @@ -126,9 +127,11 @@
 1276   * The bootstrap kernel entry code has set these up. Save them for
 1277   * a given CPU
 1278   */
 1279 -
 1280  void __init smp_store_cpu_info(int id)
 1281  {
 1282 +#ifdef CONFIG_X86_TSC
 1283 +	extern unsigned long calibrate_tsc(void);
 1284 +#endif
 1285  	struct cpuinfo_x86 *c = cpu_data + id;
 1286  
 1287  	*c = boot_cpu_data;
 1288 @@ -148,6 +151,28 @@
 1289  		 * Remember we have B step Pentia with bugs
 1290  		 */
 1291  		smp_b_stepping = 1;
 1292 +
 1293 +#ifdef CONFIG_X86_TSC
 1294 +	/*
 1295 +	 * Calibrate all additional CPUs (id != 0) and report CPU clock
 1296 +	 * in kHz (just like in time_init()).
 1297 +	 */
 1298 +	if (id != 0) {
 1299 +		exact_nanotime_quotient = calibrate_tsc();
 1300 +
 1301 +		if (exact_nanotime_quotient != 0) {
 1302 +			unsigned long eax, edx, kHz;
 1303 +
 1304 +			edx = 1000000 >> 4; eax = 0;
 1305 +			__asm__("divl %2"
 1306 +				:"=a" (kHz), "=d" (edx)
 1307 +				:"r" (exact_nanotime_quotient),
 1308 +				"0" (eax), "1" (edx));
 1309 +			printk("CPU#%d: Detected %ld kHz processor.\n",
 1310 +			       id, kHz);
 1311 +		}
 1312 +	}
 1313 +#endif
 1314  }
 1315  
 1316  /*
 1317 @@ -187,8 +212,6 @@
 1318  
 1319  #define NR_LOOPS 5
 1320  
 1321 -extern unsigned long fast_gettimeoffset_quotient;
 1322 -
 1323  /*
 1324   * accurate 64-bit/32-bit division, expanded to 32-bit divisions and 64-bit
 1325   * multiplication. Not terribly optimized but we need it at boot time only
 1326 @@ -223,12 +246,19 @@
 1327  	unsigned long long t0;
 1328  	unsigned long long sum, avg;
 1329  	long long delta;
 1330 -	unsigned long one_usec;
 1331 +	unsigned long quarter_usec, edx;
 1332  	int buggy = 0;
 1333  
 1334  	printk("checking TSC synchronization across CPUs: ");
 1335  
 1336 -	one_usec = ((1<<30)/fast_gettimeoffset_quotient)*(1<<2);
 1337 +	/* convert 250ns to CPU cycles:
 1338 +	 * cycles = (250 * 2^28) / exact_nanotime_quotient
 1339 +	 * cycles = (15*2^32 + 2684354560) / exact_nanotime_quotient
 1340 +	 */
 1341 +	__asm__("divl %2"
 1342 +		:"=a" (quarter_usec), "=d" (edx)
 1343 +		:"r" (exact_nanotime_quotient),
 1344 +		 "0" (2684354560U), "1" (15));
 1345  
 1346  	atomic_set(&tsc_start_flag, 1);
 1347  	wmb();
 1348 @@ -284,22 +314,23 @@
 1349  		if (delta < 0)
 1350  			delta = -delta;
 1351  		/*
 1352 -		 * We report bigger than 2 microseconds clock differences.
 1353 +		 * We report bigger than 0.5 microseconds clock differences.
 1354  		 */
 1355 -		if (delta > 2*one_usec) {
 1356 +		if (delta > 2 * quarter_usec) {
 1357  			long realdelta;
 1358  			if (!buggy) {
 1359  				buggy = 1;
 1360  				printk("\n");
 1361  			}
 1362 -			realdelta = div64(delta, one_usec);
 1363 +			realdelta = div64(delta, quarter_usec);
 1364  			if (tsc_values[i] < avg)
 1365  				realdelta = -realdelta;
 1366  
 1367 -			printk("BIOS BUG: CPU#%d improperly initialized, has %ld usecs TSC skew! FIXED.\n",
 1368 -				i, realdelta);
 1369 +			printk(KERN_WARNING
 1370 +			       "BIOS BUG: CPU#%d improperly initialized\n", i);
 1371 +			printk(KERN_INFO "TSC skew is %ld ns! FIXED.\n",
 1372 +			       realdelta * 250);
 1373  		}
 1374 -
 1375  		sum += delta;
 1376  	}
 1377  	if (!buggy)
 1378 @@ -332,7 +363,6 @@
 1379  #undef NR_LOOPS
 1380  
 1381  extern void calibrate_delay(void);
 1382 -
 1383  static atomic_t init_deasserted;
 1384  
 1385  void __init smp_callin(void)
 1386 Index: linux/arch/i386/kernel/time.c
 1387 diff -u linux/arch/i386/kernel/time.c:1.1.1.12 linux/arch/i386/kernel/time.c:1.1.1.12.2.1
 1388 --- linux/arch/i386/kernel/time.c:1.1.1.12	Sat Aug 26 21:34:37 2006
 1389 +++ linux/arch/i386/kernel/time.c	Sat Aug 26 21:48:47 2006
 1390 @@ -11,13 +11,13 @@
 1391   *      fixed 500 ms bug at call to set_rtc_mmss, fixed DS12887
 1392   *      precision CMOS clock update
 1393   * 1996-05-03    Ingo Molnar
 1394 - *      fixed time warps in do_[slow|fast]_gettimeoffset()
 1395 + *      fixed time warps in do_[poor|exact]_microtime()
 1396   * 1997-09-10	Updated NTP code according to technical memorandum Jan '96
 1397   *		"A Kernel Model for Precision Timekeeping" by Dave Mills
 1398   * 1998-09-05    (Various)
 1399 - *	More robust do_fast_gettimeoffset() algorithm implemented
 1400 + *	More robust do_exact_microtime() algorithm implemented
 1401   *	(works with APM, Cyrix 6x86MX and Centaur C6),
 1402 - *	monotonic gettimeofday() with fast_get_timeoffset(),
 1403 + *	monotonic gettimeofday() with exact_microtime(),
 1404   *	drift-proof precision TSC calibration on boot
 1405   *	(C. Scott Ananian <cananian@alumni.princeton.edu>, Andrew D.
 1406   *	Balsa <andrebalsa@altern.org>, Philip Gladstone <philip@raptor.com>;
 1407 @@ -28,6 +28,54 @@
 1408   * 1998-12-24 Copyright (C) 1998  Andrea Arcangeli
 1409   *	Fixed a xtime SMP race (we need the xtime_lock rw spinlock to
 1410   *	serialize accesses to xtime/lost_ticks).
 1411 + * 1999-04-24	Ulrich Windl
 1412 + *	Modifications for nanosecond resolution. Several cleanups, including
 1413 + *	renaming.  Fixed bug for ``CONFIG_X86_TSC'' with bad CPU
 1414 + *	implementation.  ``calibrate_tsc()'' calibrates for nanoseconds now.
 1415 + *	Made ``mktime()'' architecture independent.  Fixed time offset
 1416 + *	computations (estimations).
 1417 + * 1999-05-22	Ulrich Windl
 1418 + *	Bug fix: The value of ``lost_ticks'' may change while the time offset
 1419 + *	is computed, causing time jumps in both directions (Seen on a i386).
 1420 + * 1999-10-14	Ulrich Windl
 1421 + *	Bug fix: The code using the timer chip could return bad time due to
 1422 + *	overflows.  Reduce resolution even more.
 1423 + * 1999-12-10	Ulrich Windl
 1424 + *	Implement new logic with ``rtc_update_slave''.  Do a complete update
 1425 + *	of the CMOS date and time registers (``update_rtc'' replaces
 1426 + *	``set_rtc_mmss'').  Use new ``rtc_to_ktime()'' and ``ktime_to_rtc()''
 1427 + *	to get the timezone right.
 1428 + * 2000-03-31	Ulrich Windl
 1429 + *	Print time read from CMOS clock.  Require ``HZ == 100''.  Improve
 1430 + *	speed and precision when converting timer count to nanoseconds.
 1431 + * 2000-04-16	Ulrich Windl
 1432 + *	``do_nanotime'' does not include ``lost_ticks''.
 1433 + * 2000-09-04	Ulrich Windl
 1434 + *	Improved accuracy and valid range of ``nanodelay_at_last_interrupt''
 1435 + *	as well as ``do_poor_nanotime()''.  Fixed conversion from TSC to
 1436 + *	nanoseconds.
 1437 + * 2000-12-09	Ulrich Windl
 1438 + *	Remove traditional code for microseconds as the nanoseconds seem
 1439 + *	stable.
 1440 + * 2001-01-09	Ulrich Windl
 1441 + *	Improved accuracy and arithmetic of ``cpu_khz''. Improved accuracy of
 1442 + *	``nanodelay_at_last_interrupt'', ``do_poor_nanotime()'' (fixed stupid
 1443 + *	bug, too).
 1444 + * 2001-03-15	Ulrich Windl
 1445 + *	Fix for SMP: ``exact_nanotime_quotient'' is no longer static.
 1446 + * 2001-04-16	Ulrich Windl
 1447 + *	Removed unneeded code from ``update_rtc''.
 1448 + * 2001-05-10	Ulrich Windl
 1449 + *	Added scaled tick interpolation for TSC and i8259 to avoid jumps near
 1450 + *	the end of a tick (``scaled_nano_quotient'', ``update_nanoscale()'').
 1451 + * 2002-09-25	Ulrich Windl
 1452 + *	Fixed some compilation problems when ``CONFIG_X86_TSC'' is not
 1453 + *	defined.
 1454 + * 2003-04-06	Ulrich Windl
 1455 + *	Fixed assembler constraints that allowed illegal register allocation.
 1456 + *	Use macros calls instead of writing explicit inline assember.
 1457 + * 2003-08-14	Ulrich Windl
 1458 + *	Merged nanosecond timing into new code base.
 1459   */
 1460  
 1461  #include <linux/errno.h>
 1462 @@ -43,6 +91,7 @@
 1463  #include <linux/init.h>
 1464  #include <linux/smp.h>
 1465  
 1466 +#include <asm/timex.h>
 1467  #include <asm/io.h>
 1468  #include <asm/smp.h>
 1469  #include <asm/irq.h>
 1470 @@ -53,7 +102,6 @@
 1471  #include <asm/processor.h>
 1472  
 1473  #include <linux/mc146818rtc.h>
 1474 -#include <linux/timex.h>
 1475  #include <linux/config.h>
 1476  
 1477  #include <asm/fixmap.h>
 1478 @@ -67,26 +115,64 @@
 1479  
 1480  unsigned long cpu_khz;	/* Detected as we calibrate the TSC */
 1481  
 1482 -/* Number of usecs that the last interrupt was delayed */
 1483 -static int delay_at_last_interrupt;
 1484 +#ifndef NANOSECOND
 1485 +#define NANOSECOND 1000000000
 1486 +#endif
 1487 +
 1488 +/* multiply (a) * (rm1), and divide 64 bit result by (rm2).
 1489 + * quotient will be in (a), remainder in (d).
 1490 +*/
 1491 +#define	mul_div(a, d, rm1, rm2) \
 1492 +	__asm__("mull %3\n\t" \
 1493 +		"divl %4" \
 1494 +		:"=&a" (a), "=&d" (d) \
 1495 +		:"%0" (a), "rm" (rm1), "rm" (rm2) \
 1496 +		:"cc")
 1497 +
 1498 +/* multiply (a) * (rm) with 64 bit result in (d),(a) */
 1499 +#define	mul64(a, d, rm) \
 1500 +	__asm__("mull %2" \
 1501 +		:"+a" (a), "=d" (d) \
 1502 +		:"rm" (rm) \
 1503 +		:"cc")
 1504 +
 1505 +#if HZ == 100
 1506 +/* One count of the timer register is worth 1/CLOCK_TICK_RATE
 1507 + * (1000000000/1193180ns == 838.09651519ns). The register is reloaded
 1508 + * with LATCH (11932). This accumulates within one tick to
 1509 + * 10000168ns, giving an error of about 167.6ns per tick.  Using a factor
 1510 + * of 838ns per timer count would result in 984ns error per tick.
 1511 + * Scaling the exact factor by 2^(32-10) gives 3515231566,
 1512 + * resulting in an overall error of 167.6ns per tick. That is less than
 1513 + * the intrinsic error, so it must be OK.
 1514 + */
 1515 +#define	EXACT_i8253_FACTOR	3515231566UL
 1516 +#else
 1517 +#error HZ != 100 not implemented
 1518 +#endif
 1519 +unsigned long scaled_i8253_factor = EXACT_i8253_FACTOR;
 1520 +
 1521 +#ifdef CONFIG_X86_TSC
 1522 +static int use_tsc = 0;
 1523 +
 1524 +/* Number of nanoseconds that the last interrupt was delayed */
 1525 +static long nanodelay_at_last_interrupt;
 1526  
 1527  static unsigned long last_tsc_low; /* lsb 32 bits of Time Stamp Counter */
 1528  
 1529 -/* Cached *multiplier* to convert TSC counts to microseconds.
 1530 +/* Cached *multiplier* to convert TSC counts to nanoseconds.
 1531   * (see the equation below).
 1532 - * Equal to 2^32 * (1 / (clocks per usec) ).
 1533 + * Equal to 2^28 * (1 / (clocks per nsec) ).
 1534   * Initialized in time_init.
 1535   */
 1536 -unsigned long fast_gettimeoffset_quotient;
 1537 +unsigned long exact_nanotime_quotient = 0;
 1538 +unsigned long scaled_nano_quotient = 0;
 1539  
 1540 -extern rwlock_t xtime_lock;
 1541 -extern unsigned long wall_jiffies;
 1542 -
 1543 -spinlock_t rtc_lock = SPIN_LOCK_UNLOCKED;
 1544 -
 1545 -static inline unsigned long do_fast_gettimeoffset(void)
 1546 +/* nanoseconds since last timer interrupt (using the CPU cycle-counter) */
 1547 +static inline unsigned long do_exact_nanotime(void)
 1548  {
 1549 -	register unsigned long eax, edx;
 1550 +	register unsigned long eax asm("ax");
 1551 +	register unsigned long edx asm("dx");
 1552  
 1553  	/* Read the Time Stamp Counter */
 1554  
 1555 @@ -96,9 +182,10 @@
 1556  	eax -= last_tsc_low;	/* tsc_low delta */
 1557  
 1558  	/*
 1559 -         * Time offset = (tsc_low delta) * fast_gettimeoffset_quotient
 1560 -         *             = (tsc_low delta) * (usecs_per_clock)
 1561 -         *             = (tsc_low delta) * (usecs_per_jiffy / clocks_per_jiffy)
 1562 +	 * Time offset = (tsc_low delta << 4) * scaled_nano_quotient
 1563 +	 *             = (tsc_low delta << 4) * (nsecs_per_clock)
 1564 +	 *             = (tsc_low delta << 4) * (nsecs_per_jiffy /
 1565 +	 *				    clocks_per_jiffy)
 1566  	 *
 1567  	 * Using a mull instead of a divl saves up to 31 clock cycles
 1568  	 * in the critical path.
 1569 @@ -106,58 +193,31 @@
 1570  
 1571  	__asm__("mull %2"
 1572  		:"=a" (eax), "=d" (edx)
 1573 -		:"rm" (fast_gettimeoffset_quotient),
 1574 -		 "0" (eax));
 1575 +		:"%rm" (scaled_nano_quotient), "0" (eax << 4)
 1576 +		:"cc");
 1577  
 1578 -	/* our adjusted time offset in microseconds */
 1579 -	return delay_at_last_interrupt + edx;
 1580 +	/* our adjusted time offset in nanoseconds */
 1581 +	return nanodelay_at_last_interrupt + edx;
 1582  }
 1583 +#endif
 1584  
 1585 -#define TICK_SIZE tick
 1586 +extern rwlock_t xtime_lock;
 1587  
 1588 -spinlock_t i8253_lock = SPIN_LOCK_UNLOCKED;
 1589 +spinlock_t rtc_lock = SPIN_LOCK_UNLOCKED;
 1590  
 1591 -EXPORT_SYMBOL(i8253_lock);
 1592 +spinlock_t i8253_lock = SPIN_LOCK_UNLOCKED;
 1593  
 1594  extern spinlock_t i8259A_lock;
 1595  
 1596 -#ifndef CONFIG_X86_TSC
 1597 -
 1598 +/* return nanoseconds since last timer interrupt (using the timer
 1599 + * chip's registers plus some helper variable to detect overflows)
 1600 + */
 1601  /* This function must be called with interrupts disabled 
 1602   * It was inspired by Steve McCanne's microtime-i386 for BSD.  -- jrs
 1603 - * 
 1604 - * However, the pc-audio speaker driver changes the divisor so that
 1605 - * it gets interrupted rather more often - it loads 64 into the
 1606 - * counter rather than 11932! This has an adverse impact on
 1607 - * do_gettimeoffset() -- it stops working! What is also not
 1608 - * good is that the interval that our timer function gets called
 1609 - * is no longer 10.0002 ms, but 9.9767 ms. To get around this
 1610 - * would require using a different timing source. Maybe someone
 1611 - * could use the RTC - I know that this can interrupt at frequencies
 1612 - * ranging from 8192Hz to 2Hz. If I had the energy, I'd somehow fix
 1613 - * it so that at startup, the timer code in sched.c would select
 1614 - * using either the RTC or the 8253 timer. The decision would be
 1615 - * based on whether there was any other device around that needed
 1616 - * to trample on the 8253. I'd set up the RTC to interrupt at 1024 Hz,
 1617 - * and then do some jiggery to have a version of do_timer that 
 1618 - * advanced the clock by 1/1024 s. Every time that reached over 1/100
 1619 - * of a second, then do all the old code. If the time was kept correct
 1620 - * then do_gettimeoffset could just return 0 - there is no low order
 1621 - * divider that can be accessed.
 1622 - *
 1623 - * Ideally, you would be able to use the RTC for the speaker driver,
 1624 - * but it appears that the speaker driver really needs interrupt more
 1625 - * often than every 120 us or so.
 1626 - *
 1627 - * Anyway, this needs more thought....		pjsg (1993-08-28)
 1628 - * 
 1629 - * If you are really that interested, you should be reading
 1630 - * comp.protocols.time.ntp!
 1631   */
 1632 -
 1633 -static unsigned long do_slow_gettimeoffset(void)
 1634 +static unsigned long do_poor_nanotime(void)
 1635  {
 1636 -	int count;
 1637 +	unsigned long count;
 1638  
 1639  	static int count_p = LATCH;    /* for the first call after boot */
 1640  	static unsigned long jiffies_p = 0;
 1641 @@ -173,13 +233,11 @@
 1642  	outb_p(0x00, 0x43);	/* latch the count ASAP */
 1643  
 1644  	count = inb_p(0x40);	/* read the latched count */
 1645 -
 1646  	/*
 1647  	 * We do this guaranteed double memory access instead of a _p 
 1648  	 * postfix in the previous port access. Wheee, hackady hack
 1649  	 */
 1650   	jiffies_t = jiffies;
 1651 -
 1652  	count |= inb_p(0x40) << 8;
 1653  	
 1654          /* VIA686a test code... reset the latch if count > max + 1 */
 1655 @@ -202,7 +260,6 @@
 1656  	 */
 1657  
 1658  /* you can safely undefine this if you don't have the Neptune chipset */
 1659 -
 1660  #define BUGGY_NEPTUN_TIMER
 1661  
 1662  	if( jiffies_t == jiffies_p ) {
 1663 @@ -226,7 +283,10 @@
 1664  				 * well, that's why we call them lost, don't we? :)
 1665  				 * [hmm, on the Pentium and Alpha we can ... sort of]
 1666  				 */
 1667 +#if 0 /* Should it be treated? [UW] */
 1668  				count -= LATCH;
 1669 +				/* "count -= LATCH" would overflow below */
 1670 +#endif
 1671  			} else {
 1672  #ifdef BUGGY_NEPTUN_TIMER
 1673  				/*
 1674 @@ -238,25 +298,32 @@
 1675  				 */
 1676  
 1677  				count -= 256;
 1678 +				/* OK, won't overflow nanosecond computing */
 1679  #else
 1680 -				printk("do_slow_gettimeoffset(): hardware timer problem?\n");
 1681 +				printk("do_poor_nanotime(): hardware timer problem?\n");
 1682  #endif
 1683  			}
 1684  		}
 1685 -	} else
 1686 -		jiffies_p = jiffies_t;
 1687 -
 1688 +	}
 1689 +	jiffies_p = jiffies_t;
 1690  	count_p = count;
 1691  
 1692 -	count = ((LATCH-1) - count) * TICK_SIZE;
 1693 -	count = (count + LATCH/2) / LATCH;
 1694 +	/* The timer register moves from `LATCH' to zero while one
 1695 +	 * `time_tick' elapses.  We define zero as the maximum offset.
 1696 +	 * If servicing the interrupt is delayed for too long, the
 1697 +	 * cycle starts again.  That's why we need an indication for
 1698 +	 * `lost' (delayed) interrupt servicing.
 1699 +	 */
 1700  
 1701 -	return count;
 1702 +	count = (LATCH-1) - count;
 1703 +	/* variable is being misused */
 1704 +	mul64(count, jiffies_t, scaled_i8253_factor);
 1705 +	/* As we take the higher 32 bits of the result, the result is
 1706 +	 * too large by a factor of 2^22.
 1707 +	 */
 1708 +	return (jiffies_t << 10) + (count >> 22);
 1709  }
 1710  
 1711 -static unsigned long (*do_gettimeoffset)(void) = do_slow_gettimeoffset;
 1712 -
 1713 -
 1714  /* IBM Summit (EXA) Cyclone Timer code*/
 1715  #ifdef CONFIG_X86_SUMMIT
 1716  
 1717 @@ -303,6 +370,7 @@
 1718                 
 1719  	count = ((LATCH-1) - count) * TICK_SIZE;
 1720  	delay_at_last_interrupt = (count + LATCH/2) / LATCH;
 1721 +	delay_at_last_interrupt *= 1000;	/* quick-and-dirty fix by UW */
 1722  }
 1723  
 1724  static unsigned long do_gettimeoffset_cyclone(void)
 1725 @@ -320,7 +388,7 @@
 1726  
 1727  	/* convert cyclone ticks to microseconds */	
 1728  	/* XXX slow, can we speed this up? */
 1729 -	offset = offset/(CYCLONE_TIMER_FREQ/1000000);
 1730 +	offset = offset/(CYCLONE_TIMER_FREQ/1000);	/* fix by UW */
 1731  
 1732  	/* our adjusted time offset in microseconds */
 1733  	return delay_at_last_interrupt + offset;
 1734 @@ -416,12 +484,6 @@
 1735  }
 1736  #endif /* CONFIG_X86_SUMMIT */
 1737  
 1738 -#else
 1739 -
 1740 -#define do_gettimeoffset()	do_fast_gettimeoffset()
 1741 -
 1742 -#endif
 1743 -
 1744  /* No-cyclone stubs */
 1745  #ifndef CONFIG_X86_SUMMIT
 1746  int __init cyclone_setup(char *str) 
 1747 @@ -436,74 +498,54 @@
 1748  void __cyclone_delay(unsigned long loops) {}
 1749  #endif /* CONFIG_X86_SUMMIT */
 1750  
 1751 -/*
 1752 - * This version of gettimeofday has microsecond resolution
 1753 - * and better than microsecond precision on fast x86 machines with TSC.
 1754 - */
 1755 -void do_gettimeofday(struct timeval *tv)
 1756 +/* update ``scaled_nano_quotient'' and ``scaled_i8253_factor'' */
 1757 +void update_nanoscale(long freq)
 1758  {
 1759 -	unsigned long flags;
 1760 -	unsigned long usec, sec;
 1761 -
 1762 -	read_lock_irqsave(&xtime_lock, flags);
 1763 -	usec = do_gettimeoffset();
 1764 -	{
 1765 -		unsigned long lost = jiffies - wall_jiffies;
 1766 -		if (lost)
 1767 -			usec += lost * (1000000 / HZ);
 1768 -	}
 1769 -	sec = xtime.tv_sec;
 1770 -	usec += xtime.tv_usec;
 1771 -	read_unlock_irqrestore(&xtime_lock, flags);
 1772 -
 1773 -	while (usec >= 1000000) {
 1774 -		usec -= 1000000;
 1775 -		sec++;
 1776 -	}
 1777 -
 1778 -	tv->tv_sec = sec;
 1779 -	tv->tv_usec = usec;
 1780 -}
 1781 -
 1782 -void do_settimeofday(struct timeval *tv)
 1783 -{
 1784 -	write_lock_irq(&xtime_lock);
 1785 -	/*
 1786 -	 * This is revolting. We need to set "xtime" correctly. However, the
 1787 -	 * value in this location is the value at the most recent update of
 1788 -	 * wall time.  Discover what correction gettimeofday() would have
 1789 -	 * made, and then undo it!
 1790 -	 */
 1791 -	tv->tv_usec -= do_gettimeoffset();
 1792 -	tv->tv_usec -= (jiffies - wall_jiffies) * (1000000 / HZ);
 1793 -
 1794 -	while (tv->tv_usec < 0) {
 1795 -		tv->tv_usec += 1000000;
 1796 -		tv->tv_sec--;
 1797 -	}
 1798 -
 1799 -	xtime = *tv;
 1800 -	time_adjust = 0;		/* stop active adjtime() */
 1801 -	time_status |= STA_UNSYNC;
 1802 -	time_maxerror = NTP_PHASE_LIMIT;
 1803 -	time_esterror = NTP_PHASE_LIMIT;
 1804 -	write_unlock_irq(&xtime_lock);
 1805 +	register unsigned long	eax, edx;
 1806 +#if 0	/* XXX debugging leftover */
 1807 +	printk(KERN_DEBUG "update_nanoscale(): freq = %lu\n", freq);
 1808 +#endif
 1809 +	if ( freq < -2000000 || freq > 2000000 )
 1810 +		return;	/* enforce limit to avoid overflow in division */
 1811 +	/* `freq' is 1000*PPM, so the multiplier is ``freq / 1000000000'' */
 1812 +#ifdef CONFIG_X86_TSC
 1813 +	eax = freq >= 0 ? freq : -freq;
 1814 +	mul_div(eax, edx, exact_nanotime_quotient, NANOSECOND);
 1815 +	if ( freq >= 0 )
 1816 +		scaled_nano_quotient = (long) exact_nanotime_quotient + eax;
 1817 +	else
 1818 +		scaled_nano_quotient = (long) exact_nanotime_quotient - eax;
 1819 +#if 0	/* XXX debugging leftover */
 1820 +	printk(KERN_DEBUG "(TSC CPU#%d): q(%lu)%+ldPPM = s(%lu)\n",
 1821 +	       smp_processor_id(), exact_nanotime_quotient, freq,
 1822 +	       scaled_nano_quotient);
 1823 +#endif
 1824 +#endif /* CONFIG_X86_TSC */
 1825 +	eax = freq >= 0 ? freq : -freq;
 1826 +	mul_div(eax, edx, EXACT_i8253_FACTOR, NANOSECOND);
 1827 +	if ( freq >= 0 )
 1828 +		scaled_i8253_factor = (long) EXACT_i8253_FACTOR + eax;
 1829 +	else
 1830 +		scaled_i8253_factor = (long) EXACT_i8253_FACTOR - eax;
 1831 +#if 0	/* XXX debugging leftover */
 1832 +	printk(KERN_DEBUG "(i8253): q(%lu)%+ldPPM = s(%lu)\n",
 1833 +	       EXACT_i8253_FACTOR, freq, scaled_i8253_factor);
 1834 +#endif
 1835  }
 1836  
 1837  /*
 1838 - * In order to set the CMOS clock precisely, set_rtc_mmss has to be
 1839 - * called 500 ms after the second nowtime has started, because when
 1840 - * nowtime is written into the registers of the CMOS clock, it will
 1841 + * In order to set the CMOS clock precisely, update_rtc has to be
 1842 + * called 500 ms after the current second has started, because when
 1843 + * the time is written into the registers of the CMOS clock, it will
 1844   * jump to the next second precisely 500 ms later. Check the Motorola
 1845   * MC146818A or Dallas DS12887 data sheet for details.
 1846   *
 1847   * BUG: This routine does not handle hour overflow properly; it just
 1848   *      sets the minutes. Usually you'll only notice that after reboot!
 1849   */
 1850 -static int set_rtc_mmss(unsigned long nowtime)
 1851 +static void update_rtc(void)
 1852  {
 1853 -	int retval = 0;
 1854 -	int real_seconds, real_minutes, cmos_minutes;
 1855 +	int ss, mi, hh, dd, mo, yy;
 1856  	unsigned char save_control, save_freq_select;
 1857  
 1858  	/* gets recalled with irq locally disabled */
 1859 @@ -514,35 +556,30 @@
 1860  	save_freq_select = CMOS_READ(RTC_FREQ_SELECT); /* stop and reset prescaler */
 1861  	CMOS_WRITE((save_freq_select|RTC_DIV_RESET2), RTC_FREQ_SELECT);
 1862  
 1863 -	cmos_minutes = CMOS_READ(RTC_MINUTES);
 1864 -	if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
 1865 -		BCD_TO_BIN(cmos_minutes);
 1866 -
 1867 -	/*
 1868 -	 * since we're only adjusting minutes and seconds,
 1869 -	 * don't interfere with hour overflow. This avoids
 1870 -	 * messing with unknown time zones but requires your
 1871 -	 * RTC not to be off by more than 15 minutes
 1872 -	 */
 1873 -	real_seconds = nowtime % 60;
 1874 -	real_minutes = nowtime / 60;
 1875 -	if (((abs(real_minutes - cmos_minutes) + 15)/30) & 1)
 1876 -		real_minutes += 30;		/* correct for half hour time zone */
 1877 -	real_minutes %= 60;
 1878 -
 1879 -	if (abs(real_minutes - cmos_minutes) < 30) {
 1880 -		if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
 1881 -			BIN_TO_BCD(real_seconds);
 1882 -			BIN_TO_BCD(real_minutes);
 1883 -		}
 1884 -		CMOS_WRITE(real_seconds,RTC_SECONDS);
 1885 -		CMOS_WRITE(real_minutes,RTC_MINUTES);
 1886 -	} else {
 1887 -		printk(KERN_WARNING
 1888 -		       "set_rtc_mmss: can't update from %d to %d\n",
 1889 -		       cmos_minutes, real_minutes);
 1890 -		retval = -1;
 1891 -	}
 1892 +	ktime_to_rtc(xtime.tv_sec, &ss, &dd);
 1893 +	hh = ss >> 16;
 1894 +	mi = (ss >> 8) & 0xff;
 1895 +	ss &= 0xff;
 1896 +	yy = (dd >> 16) % 100;
 1897 +	mo = (dd >> 8) & 0xff;
 1898 +	dd &= 0xff;
 1899 +	printk(KERN_DEBUG "update_rtc: %02d-%02d-%02d %02d:%02d:%02d\n",
 1900 +	       yy, mo, dd, hh, mi, ss);
 1901 +
 1902 +	if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
 1903 +		BIN_TO_BCD(ss);
 1904 +		BIN_TO_BCD(mi);
 1905 +		BIN_TO_BCD(hh);
 1906 +		BIN_TO_BCD(dd);
 1907 +		BIN_TO_BCD(mo);
 1908 +		BIN_TO_BCD(yy);
 1909 +  	}
 1910 +	CMOS_WRITE(ss, RTC_SECONDS);
 1911 +	CMOS_WRITE(mi, RTC_MINUTES);
 1912 +	CMOS_WRITE(hh, RTC_HOURS);
 1913 +	CMOS_WRITE(dd, RTC_DAY_OF_MONTH);
 1914 +	CMOS_WRITE(mo, RTC_MONTH);
 1915 +	CMOS_WRITE(yy, RTC_YEAR);
 1916  
 1917  	/* The following flags have to be released exactly in this order,
 1918  	 * otherwise the DS12887 (popular MC146818A clone with integrated
 1919 @@ -554,8 +591,6 @@
 1920  	CMOS_WRITE(save_control, RTC_CONTROL);
 1921  	CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
 1922  	spin_unlock(&rtc_lock);
 1923 -
 1924 -	return retval;
 1925  }
 1926  
 1927  /* last time the cmos clock got updated */
 1928 @@ -604,18 +639,17 @@
 1929  #endif
 1930  
 1931  	/*
 1932 -	 * If we have an externally synchronized Linux clock, then update
 1933 -	 * CMOS clock accordingly every ~11 minutes. Set_rtc_mmss() has to be
 1934 -	 * called as close as possible to 500 ms before the new second starts.
 1935 -	 */
 1936 -	if ((time_status & STA_UNSYNC) == 0 &&
 1937 -	    xtime.tv_sec > last_rtc_update + 660 &&
 1938 -	    xtime.tv_usec >= 500000 - ((unsigned) tick) / 2 &&
 1939 -	    xtime.tv_usec <= 500000 + ((unsigned) tick) / 2) {
 1940 -		if (set_rtc_mmss(xtime.tv_sec) == 0)
 1941 -			last_rtc_update = xtime.tv_sec;
 1942 -		else
 1943 -			last_rtc_update = xtime.tv_sec - 600; /* do it again in 60 s */
 1944 +	 * If we have a change of the software clock to be committed to the
 1945 +	 * CMOS clock, then do it. Update_rtc() has to be called as close as
 1946 +	 * possible to 500 ms before the new second starts.
 1947 +	 */
 1948 +	if (rtc_update_slave &&
 1949 +	    xtime.tv_sec >= last_rtc_update + rtc_update_slave &&
 1950 +	    xtime.tv_nsec >= NANOSECOND / 2 - ((unsigned long) time_tick) / 2 &&
 1951 +	    xtime.tv_nsec <= NANOSECOND / 2 + ((unsigned long) time_tick) / 2) {
 1952 +		update_rtc();
 1953 +		last_rtc_update = xtime.tv_sec;
 1954 +		rtc_update_slave = 0;
 1955  	}
 1956  	    
 1957  #ifdef CONFIG_MCA
 1958 @@ -635,8 +669,6 @@
 1959  #endif
 1960  }
 1961  
 1962 -static int use_tsc;
 1963 -
 1964  /*
 1965   * This is the same as the above, except we _also_ save the current
 1966   * Time Stamp Counter value at the time of the timer interrupt, so that
 1967 @@ -644,8 +676,6 @@
 1968   */
 1969  static void timer_interrupt(int irq, void *dev_id, struct pt_regs *regs)
 1970  {
 1971 -	int count;
 1972 -
 1973  	/*
 1974  	 * Here we are in the timer irq handler. We just have irqs locally
 1975  	 * disabled but we don't know if the timer_bh is running on the other
 1976 @@ -657,7 +687,14 @@
 1977  
 1978  	if(use_cyclone)
 1979  		mark_timeoffset_cyclone();
 1980 +#ifdef CONFIG_X86_TSC
 1981  	else if (use_tsc) {
 1982 +		long count;
 1983 +		unsigned long eax, edx;
 1984 +#ifdef CONFIG_SMP
 1985 +		unsigned long delta_nsec_high, delta_nsec_low;
 1986 +		int cpu;
 1987 +#endif
 1988  		/*
 1989  		 * It is important that these two operations happen almost at
 1990  		 * the same time. We do the RDTSC stuff first, since it's
 1991 @@ -671,13 +708,12 @@
 1992  		 */
 1993  	
 1994  		/* read Pentium cycle counter */
 1995 -
 1996  		rdtscl(last_tsc_low);
 1997  
 1998  		spin_lock(&i8253_lock);
 1999  		outb_p(0x00, 0x43);     /* latch the count ASAP */
 2000  
 2001 -		count = inb_p(0x40);    /* read the latched count */
 2002 +		count = inb_p(0x40);	/* read the latched count */
 2003  		count |= inb(0x40) << 8;
 2004  
 2005  		/* Any unpaired read will cause the above to swap MSB/LSB
 2006 @@ -702,11 +738,16 @@
 2007  		if (count == LATCH) {
 2008  			count--;
 2009  		}
 2010 +		eax = (LATCH-1) - count;
 2011 +		mul64(eax, edx, scaled_i8253_factor);
 2012  
 2013 -		count = ((LATCH-1) - count) * TICK_SIZE;
 2014 -		delay_at_last_interrupt = (count + LATCH/2) / LATCH;
 2015 +		/* As we take the higher 32 bits of the result, the
 2016 +		 * result is too large by a factor of 2^22.
 2017 +		 */
 2018 +		nanodelay_at_last_interrupt = (edx << 10) + (eax >> 22);
 2019  	}
 2020  
 2021 +#endif /* CONFIG_X86_TSC */
 2022  	do_timer_interrupt(irq, NULL, regs);
 2023  
 2024  	write_unlock(&xtime_lock);
 2025 @@ -740,25 +781,29 @@
 2026  		mon = CMOS_READ(RTC_MONTH);
 2027  		year = CMOS_READ(RTC_YEAR);
 2028  	} while (sec != CMOS_READ(RTC_SECONDS));
 2029 -	if (!(CMOS_READ(RTC_CONTROL) & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
 2030 -	  {
 2031 -	    BCD_TO_BIN(sec);
 2032 -	    BCD_TO_BIN(min);
 2033 -	    BCD_TO_BIN(hour);
 2034 -	    BCD_TO_BIN(day);
 2035 -	    BCD_TO_BIN(mon);
 2036 -	    BCD_TO_BIN(year);
 2037 -	  }
 2038  	spin_unlock(&rtc_lock);
 2039 -	if ((year += 1900) < 1970)
 2040 -		year += 100;
 2041 -	return mktime(year, mon, day, hour, min, sec);
 2042 +  	if (!(CMOS_READ(RTC_CONTROL) & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
 2043 +	{
 2044 +		BCD_TO_BIN(sec);
 2045 +		BCD_TO_BIN(min);
 2046 +		BCD_TO_BIN(hour);
 2047 +		BCD_TO_BIN(day);
 2048 +		BCD_TO_BIN(mon);
 2049 +		BCD_TO_BIN(year);
 2050 +	}
 2051 +	if ((year += 1900) < 1970)	/* Fix for year 2000 rollover */
 2052 +		year += 100;		/* Doesn't the CMOS have a century? */
 2053 +	printk(KERN_INFO "get_cmos_time: %d-%02d-%02d %02d:%02d:%02d\n",
 2054 +	       year, mon, day, hour, min, sec);
 2055 +	return rtc_to_ktime(year, mon, day, hour, min, sec);
 2056  }
 2057  
 2058  static struct irqaction irq0  = { timer_interrupt, SA_INTERRUPT, 0, "timer", NULL, NULL};
 2059  
 2060 +#ifdef CONFIG_X86_TSC
 2061  /* ------ Calibrate the TSC ------- 
 2062 - * Return 2^32 * (1 / (TSC clocks per usec)) for do_fast_gettimeoffset().
 2063 + * Return 2^28 * (1 / (TSC clocks per nsec)) for exact time differences using
 2064 + * ``rdtsc''.
 2065   * Too much 64-bit arithmetic here to do this cleanly in C, and for
 2066   * accuracy's sake we want to keep the overhead on the CTC speaker (channel 2)
 2067   * output busy loop as low as possible. We avoid reading the CTC registers
 2068 @@ -767,11 +812,16 @@
 2069   */
 2070  
 2071  #define CALIBRATE_LATCH	(5 * LATCH)
 2072 -#define CALIBRATE_TIME	(5 * 1000020/HZ)
 2073 +#define CALIBRATE_TIME	((5 * 1000020/HZ * 1000) >> 4)
 2074 +/* allow 1GHz / 16 = 62.5MHz CPUs */
 2075  
 2076 +#ifdef CONFIG_SMP
 2077 +unsigned long __init calibrate_tsc(void)
 2078 +#else
 2079  static unsigned long __init calibrate_tsc(void)
 2080 +#endif
 2081  {
 2082 -       /* Set the Gate high, disable speaker */
 2083 +	/* Set the Gate high, disable speaker */
 2084  	outb((inb(0x61) & ~0x02) | 0x01, 0x61);
 2085  
 2086  	/*
 2087 @@ -810,6 +860,8 @@
 2088  			:"g" (startlow), "g" (starthigh),
 2089  			 "0" (endlow), "1" (endhigh));
 2090  
 2091 +		printk(KERN_DEBUG "calibrate_tsc(): %dns = %lu:%lu\n",
 2092 +		       CALIBRATE_TIME << 4, endhigh, endlow);
 2093  		/* Error: ECPUTOOFAST */
 2094  		if (endhigh)
 2095  			goto bad_ctc;
 2096 @@ -818,10 +870,14 @@
 2097  		if (endlow <= CALIBRATE_TIME)
 2098  			goto bad_ctc;
 2099  
 2100 +		/* CALIBRATE_TIME * 2^32 / endlow */
 2101  		__asm__("divl %2"
 2102  			:"=a" (endlow), "=d" (endhigh)
 2103 -			:"r" (endlow), "0" (0), "1" (CALIBRATE_TIME));
 2104 +			:"rm" (endlow), "0" (0), "1" (CALIBRATE_TIME)
 2105 +			:"cc");
 2106  
 2107 +		printk(KERN_DEBUG "calibrate_tsc(): 16 * (%lu:%lu)\n",
 2108 +		       endhigh, endlow);
 2109  		return endlow;
 2110  	}
 2111  
 2112 @@ -833,13 +889,15 @@
 2113  bad_ctc:
 2114  	return 0;
 2115  }
 2116 +#endif
 2117  
 2118  void __init time_init(void)
 2119  {
 2120  	extern int x86_udelay_tsc;
 2121  	
 2122 +	do_nanotime = do_poor_nanotime;
 2123  	xtime.tv_sec = get_cmos_time();
 2124 -	xtime.tv_usec = 0;
 2125 +	xtime.tv_nsec = 0;
 2126  
 2127  /*
 2128   * If we have APM enabled or the CPU clock speed is variable
 2129 @@ -855,6 +913,8 @@
 2130   * to disk; this won't break the kernel, though, 'cuz we're
 2131   * smart.  See arch/i386/kernel/apm.c.
 2132   */
 2133 +
 2134 +#ifdef CONFIG_X86_TSC
 2135   	/*
 2136   	 *	Firstly we have to do a CPU check for chips with
 2137   	 * 	a potentially buggy TSC. At this point we haven't run
 2138 @@ -872,9 +932,13 @@
 2139  		init_cyclone_clock();
 2140  		
 2141  	if (cpu_has_tsc) {
 2142 -		unsigned long tsc_quotient = calibrate_tsc();
 2143 -		if (tsc_quotient) {
 2144 -			fast_gettimeoffset_quotient = tsc_quotient;
 2145 +		exact_nanotime_quotient = calibrate_tsc();
 2146 +		scaled_nano_quotient = exact_nanotime_quotient;
 2147 +		/* exact_nanotime_quotient is 2^28 * number of ns/TSC */
 2148 +		if (exact_nanotime_quotient != 0)
 2149 +		{
 2150 +			unsigned long eax, edx;
 2151 +
 2152  			/* XXX: This is messy
 2153  			 * However, we want to allow for the cyclone timer 
 2154  			 * to work w/ or w/o the TSCs being avaliable
 2155 @@ -886,26 +950,26 @@
 2156  				 *	and just enable this for the next intel chips ?
 2157  			 	 */
 2158  				use_tsc = 1;
 2159 +				/* Now it's safe to use these routines */
 2160 +				do_nanotime = do_exact_nanotime;
 2161  				x86_udelay_tsc = 1;
 2162 -#ifndef do_gettimeoffset
 2163 -				do_gettimeoffset = do_fast_gettimeoffset;
 2164 -#endif
 2165  			}
 2166 -			/* report CPU clock rate in Hz.
 2167 -			 * The formula is (10^6 * 2^32) / (2^32 * 1 / (clocks/us)) =
 2168 -			 * clock/second. Our precision is about 100 ppm.
 2169 +			/* report CPU clock rate in kHz. The formula is
 2170 +			 * ((10^6 >> 4) * 2^32) / (2^32 * 1 / (clocks/ns)) =
 2171 +			 * clock/millisecond. Our precision is about 100 ppm.
 2172  			 */
 2173 -			{	unsigned long eax=0, edx=1000;
 2174 -				__asm__("divl %2"
 2175 -		       		:"=a" (cpu_khz), "=d" (edx)
 2176 -        	       		:"r" (tsc_quotient),
 2177 -	                	"0" (eax), "1" (edx));
 2178 -				printk("Detected %lu.%03lu MHz processor.\n", cpu_khz / 1000, cpu_khz % 1000);
 2179 -			}
 2180 +			edx = 1000000 >> 4, eax = 0;
 2181 +			__asm__("divl %2"
 2182 +				:"=a" (cpu_khz), "=d" (edx)
 2183 +				:"rm" (exact_nanotime_quotient),
 2184 +				"0" (eax), "1" (edx)
 2185 +				:"cc");
 2186 +			printk("Detected %ld kHz processor.\n", cpu_khz);
 2187  		}
 2188  	}
 2189  
 2190 -
 2191 +#endif /* CONFIG_X86_TSC */
 2192 +	timevar_init();		/* architecture independent initialization */
 2193  #ifdef CONFIG_VISWS
 2194  	printk("Starting Cobalt Timer system clock\n");
 2195  
 2196 Index: linux/arch/i386/kernel/visws_apic.c
 2197 diff -u linux/arch/i386/kernel/visws_apic.c:1.1.1.2 linux/arch/i386/kernel/visws_apic.c:1.1.1.2.22.1
 2198 --- linux/arch/i386/kernel/visws_apic.c:1.1.1.2	Sun Mar 11 14:51:22 2001
 2199 +++ linux/arch/i386/kernel/visws_apic.c	Sat Aug 26 21:48:47 2006
 2200 @@ -16,10 +16,9 @@
 2201  #include <linux/errno.h>
 2202  #include <linux/kernel_stat.h>
 2203  #include <linux/signal.h>
 2204 -#include <linux/sched.h>
 2205  #include <linux/ioport.h>
 2206  #include <linux/interrupt.h>
 2207 -#include <linux/timex.h>
 2208 +#include <linux/time.h>
 2209  #include <linux/slab.h>
 2210  #include <linux/random.h>
 2211  #include <linux/smp.h>
 2212 Index: linux/arch/ia64/dig/setup.c
 2213 diff -u linux/arch/ia64/dig/setup.c:1.1.1.8 linux/arch/ia64/dig/setup.c:1.1.1.8.2.1
 2214 --- linux/arch/ia64/dig/setup.c:1.1.1.8	Sat Aug 26 21:34:41 2006
 2215 +++ linux/arch/ia64/dig/setup.c	Sat Aug 26 21:48:49 2006
 2216 @@ -17,7 +17,7 @@
 2217  #include <linux/string.h>
 2218  #include <linux/tty.h>
 2219  #include <linux/console.h>
 2220 -#include <linux/timex.h>
 2221 +#include <linux/time.h>
 2222  #include <linux/sched.h>
 2223  
 2224  #include <asm/io.h>
 2225 Index: linux/arch/ia64/ia32/sys_ia32.c
 2226 diff -u linux/arch/ia64/ia32/sys_ia32.c:1.1.1.17 linux/arch/ia64/ia32/sys_ia32.c:1.1.1.17.2.1
 2227 --- linux/arch/ia64/ia32/sys_ia32.c:1.1.1.17	Sat Aug 26 21:34:41 2006
 2228 +++ linux/arch/ia64/ia32/sys_ia32.c	Sat Aug 26 21:48:52 2006
 2229 @@ -751,6 +751,7 @@
 2230  struct timeval32
 2231  {
 2232      int tv_sec, tv_usec;
 2233 +	/*[UW] Attention: Actually ``tv_usec'' is nanoseconds sometimes! */
 2234  };
 2235  
 2236  struct itimerval32
 2237 @@ -4548,12 +4549,15 @@
 2238  	u32 modes;
 2239  	s32 offset, freq, maxerror, esterror;
 2240  	s32 status, constant, precision, tolerance;
 2241 -	struct timeval32 time;
 2242 -	s32 tick;
 2243 +	struct timespec32 time;		/* ``NTP_NANO'' is defined */
 2244 +	s32 time_tick;
 2245  	s32 ppsfreq, jitter, shift, stabil;
 2246  	s32 jitcnt, calcnt, errcnt, stbcnt;
 2247 -	s32  :32; s32  :32; s32  :32; s32  :32;
 2248 -	s32  :32; s32  :32; s32  :32; s32  :32;
 2249 +	/*[UW]Sorry , the layout is a mess, but it's for historical reasons */
 2250 +	s32 tai;
 2251 +	s32  :32; s32  :32; s32  :32;
 2252 +	int	tickadj;
 2253 +	s32  :32; s32  :32; s32  :32;
 2254  	s32  :32; s32  :32; s32  :32; s32  :32;
 2255  };
 2256  
 2257 @@ -4577,8 +4581,8 @@
 2258  	   __get_user(txc.precision, &utp->precision) ||
 2259  	   __get_user(txc.tolerance, &utp->tolerance) ||
 2260  	   __get_user(txc.time.tv_sec, &utp->time.tv_sec) ||
 2261 -	   __get_user(txc.time.tv_usec, &utp->time.tv_usec) ||
 2262 -	   __get_user(txc.tick, &utp->tick) ||
 2263 +	   __get_user(txc.time.tv_nsec, &utp->time.tv_nsec) ||
 2264 +	   __get_user(txc.time_tick, &utp->time_tick) ||
 2265  	   __get_user(txc.ppsfreq, &utp->ppsfreq) ||
 2266  	   __get_user(txc.jitter, &utp->jitter) ||
 2267  	   __get_user(txc.shift, &utp->shift) ||
 2268 @@ -4586,7 +4590,9 @@
 2269  	   __get_user(txc.jitcnt, &utp->jitcnt) ||
 2270  	   __get_user(txc.calcnt, &utp->calcnt) ||
 2271  	   __get_user(txc.errcnt, &utp->errcnt) ||
 2272 -	   __get_user(txc.stbcnt, &utp->stbcnt))
 2273 +	   __get_user(txc.stbcnt, &utp->stbcnt) ||
 2274 +	   __get_user(txc.stbcnt, &utp->tai) ||
 2275 +	   __get_user(txc.stbcnt, &utp->tickadj))
 2276  		return -EFAULT;
 2277  
 2278  	ret = do_adjtimex(&txc);
 2279 @@ -4601,8 +4607,8 @@
 2280  	   __put_user(txc.precision, &utp->precision) ||
 2281  	   __put_user(txc.tolerance, &utp->tolerance) ||
 2282  	   __put_user(txc.time.tv_sec, &utp->time.tv_sec) ||
 2283 -	   __put_user(txc.time.tv_usec, &utp->time.tv_usec) ||
 2284 -	   __put_user(txc.tick, &utp->tick) ||
 2285 +	   __put_user(txc.time.tv_nsec, &utp->time.tv_nsec) ||
 2286 +	   __put_user(txc.tick, &utp->time_tick) ||
 2287  	   __put_user(txc.ppsfreq, &utp->ppsfreq) ||
 2288  	   __put_user(txc.jitter, &utp->jitter) ||
 2289  	   __put_user(txc.shift, &utp->shift) ||
 2290 @@ -4610,7 +4616,9 @@
 2291  	   __put_user(txc.jitcnt, &utp->jitcnt) ||
 2292  	   __put_user(txc.calcnt, &utp->calcnt) ||
 2293  	   __put_user(txc.errcnt, &utp->errcnt) ||
 2294 -	   __put_user(txc.stbcnt, &utp->stbcnt))
 2295 +	   __put_user(txc.stbcnt, &utp->stbcnt) ||
 2296 +	   __put_user(txc.stbcnt, &utp->tai) ||
 2297 +	   __put_user(txc.stbcnt, &utp->tickadj))
 2298  		ret = -EFAULT;
 2299  
 2300  	return ret;
 2301 Index: linux/arch/ia64/kernel/irq.c
 2302 diff -u linux/arch/ia64/kernel/irq.c:1.1.1.14 linux/arch/ia64/kernel/irq.c:1.1.1.14.2.1
 2303 --- linux/arch/ia64/kernel/irq.c:1.1.1.14	Sat Aug 26 21:34:41 2006
 2304 +++ linux/arch/ia64/kernel/irq.c	Sat Aug 26 21:48:54 2006
 2305 @@ -24,7 +24,7 @@
 2306  #include <linux/sched.h>
 2307  #include <linux/ioport.h>
 2308  #include <linux/interrupt.h>
 2309 -#include <linux/timex.h>
 2310 +#include <linux/time.h>
 2311  #include <linux/slab.h>
 2312  #include <linux/random.h>
 2313  #include <linux/smp_lock.h>
 2314 Index: linux/arch/ia64/kernel/time.c
 2315 diff -u linux/arch/ia64/kernel/time.c:1.1.1.12 linux/arch/ia64/kernel/time.c:1.1.1.12.2.1
 2316 --- linux/arch/ia64/kernel/time.c:1.1.1.12	Sat Aug 26 21:34:42 2006
 2317 +++ linux/arch/ia64/kernel/time.c	Sat Aug 26 21:48:54 2006
 2318 @@ -294,3 +294,11 @@
 2319  	efi_gettimeofday((struct timeval *) &xtime);
 2320  	ia64_init_itm();
 2321  }
 2322 +
 2323 +/* Fine-tune the tick interpolation for a total frequency error
 2324 + * `freq' / 1000 PPM.
 2325 + */
 2326 +void update_nanoscale(long freq)
 2327 +{
 2328 +	/* not implemented -- this is just a prototype [UW] */
 2329 +}
 2330 Index: linux/arch/m68k/config.in
 2331 diff -u linux/arch/m68k/config.in:1.1.1.8 linux/arch/m68k/config.in:1.1.1.8.6.1
 2332 --- linux/arch/m68k/config.in:1.1.1.8	Sat Jan 22 21:53:07 2005
 2333 +++ linux/arch/m68k/config.in	Sat Aug 26 21:48:57 2006
 2334 @@ -146,6 +146,13 @@
 2335        bool '    IEEE 1284 transfer modes' CONFIG_PARPORT_1284
 2336     fi
 2337  fi
 2338 +bool 'NTP kernel support' CONFIG_NTP
 2339 +if [ "$CONFIG_NTP" != "n" ]; then
 2340 +  bool '   NTP PPS support' CONFIG_NTP_PPS
 2341 +  if [ "$CONFIG_NTP_PPS" != "n" ]; then
 2342 +    bool '   Debug NTP PPS support' CONFIG_NTP_PPS_DEBUG
 2343 +  fi
 2344 +fi
 2345  
 2346  if [ "$CONFIG_Q40" = "y" -o "$CONFIG_AMIGA_PCMCIA" = "y" \
 2347        -o "$CONFIG_GG2" = "y" ]; then
 2348 Index: linux/arch/m68k/defconfig
 2349 diff -u linux/arch/m68k/defconfig:1.1.1.5 linux/arch/m68k/defconfig:1.1.1.5.2.1
 2350 --- linux/arch/m68k/defconfig:1.1.1.5	Sat Aug 26 21:34:42 2006
 2351 +++ linux/arch/m68k/defconfig	Sat Aug 26 21:48:57 2006
 2352 @@ -47,6 +47,10 @@
 2353  CONFIG_BINFMT_AOUT=y
 2354  CONFIG_BINFMT_ELF=y
 2355  # CONFIG_BINFMT_MISC is not set
 2356 +CONFIG_NTP=y
 2357 +# CONFIG_NTP_PPS is not set
 2358 +# CONFIG_NTP_PPS_DEBUG is not set
 2359 +# CONFIG_NTP_PPS_SERIAL is not set
 2360  CONFIG_ZORRO=y
 2361  # CONFIG_AMIGA_PCMCIA is not set
 2362  # CONFIG_HEARTBEAT is not set
 2363 Index: linux/arch/m68k/kernel/time.c
 2364 diff -u linux/arch/m68k/kernel/time.c:1.1.1.5 linux/arch/m68k/kernel/time.c:1.1.1.5.2.1
 2365 --- linux/arch/m68k/kernel/time.c:1.1.1.5	Sat Aug 26 21:34:42 2006
 2366 +++ linux/arch/m68k/kernel/time.c	Sat Aug 26 21:48:59 2006
 2367 @@ -6,13 +6,20 @@
 2368   * This file contains the m68k-specific time handling details.
 2369   * Most of the stuff is located in the machine specific files.
 2370   *
 2371 - * 1997-09-10	Updated NTP code according to technical memorandum Jan '96
 2372 - *		"A Kernel Model for Precision Timekeeping" by Dave Mills
 2373 + * 1999-05-22	Ulrich Windl
 2374 + *	Updated for nanosecond resolution (untested)
 2375 + * 1999-12-10	Ulrich Windl
 2376 + *	Implement new logic with ``rtc_update_slave''.  Use ``rtc_update()'',
 2377 + *	replacing ``set_rtc_mmss()'' (untested).
 2378 + * 2000-12-09	Ulrich Windl
 2379 + *	Remove traditional code for microseconds as the nanoseconds seem
 2380 + *	stable.
 2381   */
 2382  
 2383  #include <linux/config.h> /* CONFIG_HEARTBEAT */
 2384  #include <linux/errno.h>
 2385 -#include <linux/sched.h>
 2386 +#include <linux/time.h>
 2387 +#include <linux/timex.h>
 2388  #include <linux/kernel.h>
 2389  #include <linux/param.h>
 2390  #include <linux/string.h>
 2391 @@ -21,14 +28,11 @@
 2392  #include <asm/machdep.h>
 2393  #include <asm/io.h>
 2394  
 2395 -#include <linux/timex.h>
 2396 -
 2397 -
 2398 -static inline int set_rtc_mmss(unsigned long nowtime)
 2399 +static inline void update_rtc(void)
 2400  {
 2401 -  if (mach_set_clock_mmss)
 2402 -    return mach_set_clock_mmss (nowtime);
 2403 -  return -1;
 2404 +	if (mach_set_clock_mmss)
 2405 +		return mach_set_clock_mmss (xtime.tv_sec);
 2406 +	return -1;
 2407  }
 2408  
 2409  static inline void do_profile (unsigned long pc)
 2410 @@ -85,6 +89,12 @@
 2411  #endif /* CONFIG_HEARTBEAT */
 2412  }
 2413  
 2414 +/* nanoseconds since last timer interrupt */
 2415 +static inline unsigned long do_exact_nanotime(void)
 2416 +{
 2417 +	return do_exact_microtime() * 1000;
 2418 +}
 2419 +
 2420  void time_init(void)
 2421  {
 2422  	unsigned int year, mon, day, hour, min, sec;
 2423 @@ -92,65 +102,21 @@
 2424  	extern void arch_gettod(int *year, int *mon, int *day, int *hour,
 2425  				int *min, int *sec);
 2426  
 2427 +	do_nanotime = do_exact_nanotime;
 2428  	arch_gettod (&year, &mon, &day, &hour, &min, &sec);
 2429  
 2430  	if ((year += 1900) < 1970)
 2431  		year += 100;
 2432  	xtime.tv_sec = mktime(year, mon, day, hour, min, sec);
 2433 -	xtime.tv_usec = 0;
 2434 +	xtime.tv_nsec = 0;
 2435  
 2436  	mach_sched_init(timer_interrupt);
 2437  }
 2438  
 2439 -extern rwlock_t xtime_lock;
 2440 -
 2441 -/*
 2442 - * This version of gettimeofday has near microsecond resolution.
 2443 +/* Fine-tune the tick interpolation for a total frequency error
 2444 + * `freq' / 1000 PPM.
 2445   */
 2446 -void do_gettimeofday(struct timeval *tv)
 2447 -{
 2448 -	extern unsigned long wall_jiffies;
 2449 -	unsigned long flags;
 2450 -	unsigned long usec, sec, lost;
 2451 -
 2452 -	read_lock_irqsave(&xtime_lock, flags);
 2453 -	usec = mach_gettimeoffset();
 2454 -	lost = jiffies - wall_jiffies;
 2455 -	if (lost)
 2456 -		usec += lost * (1000000/HZ);
 2457 -	sec = xtime.tv_sec;
 2458 -	usec += xtime.tv_usec;
 2459 -	read_unlock_irqrestore(&xtime_lock, flags);
 2460 -
 2461 -	while (usec >= 1000000) {
 2462 -		usec -= 1000000;
 2463 -		sec++;
 2464 -	}
 2465 -
 2466 -	tv->tv_sec = sec;
 2467 -	tv->tv_usec = usec;
 2468 -}
 2469 -
 2470 -void do_settimeofday(struct timeval *tv)
 2471 +void update_nanoscale(long freq)
 2472  {
 2473 -	write_lock_irq(&xtime_lock);
 2474 -	/* This is revolting. We need to set the xtime.tv_usec
 2475 -	 * correctly. However, the value in this location is
 2476 -	 * is value at the last tick.
 2477 -	 * Discover what correction gettimeofday
 2478 -	 * would have done, and then undo it!
 2479 -	 */
 2480 -	tv->tv_usec -= mach_gettimeoffset();
 2481 -
 2482 -	while (tv->tv_usec < 0) {
 2483 -		tv->tv_usec += 1000000;
 2484 -		tv->tv_sec--;
 2485 -	}
 2486 -
 2487 -	xtime = *tv;
 2488 -	time_adjust = 0;		/* stop active adjtime() */
 2489 -	time_status |= STA_UNSYNC;
 2490 -	time_maxerror = NTP_PHASE_LIMIT;
 2491 -	time_esterror = NTP_PHASE_LIMIT;
 2492 -	write_unlock_irq(&xtime_lock);
 2493 +	/* not implemented -- this is just a prototype [UW] */
 2494  }
 2495 Index: linux/arch/mips/config-shared.in
 2496 diff -u linux/arch/mips/config-shared.in:1.1.1.5 linux/arch/mips/config-shared.in:1.1.1.5.6.1
 2497 --- linux/arch/mips/config-shared.in:1.1.1.5	Sat Jan 22 22:02:25 2005
 2498 +++ linux/arch/mips/config-shared.in	Sat Aug 26 21:49:02 2006
 2499 @@ -971,6 +971,14 @@
 2500  
 2501  #source drivers/misc/Config.in
 2502  
 2503 +bool 'NTP kernel support' CONFIG_NTP
 2504 +if [ "$CONFIG_NTP" != "n" ]; then
 2505 +  bool '   NTP PPS support' CONFIG_NTP_PPS
 2506 +  if [ "$CONFIG_NTP_PPS" != "n" ]; then
 2507 +    bool '   Debug NTP PPS support' CONFIG_NTP_PPS_DEBUG
 2508 +  fi
 2509 +fi
 2510 +
 2511  source fs/Config.in
 2512  
 2513  source drivers/media/Config.in
 2514 Index: linux/arch/mips/defconfig
 2515 diff -u linux/arch/mips/defconfig:1.1.1.12 linux/arch/mips/defconfig:1.1.1.12.2.1
 2516 --- linux/arch/mips/defconfig:1.1.1.12	Sat Aug 26 21:34:43 2006
 2517 +++ linux/arch/mips/defconfig	Sat Aug 26 21:49:02 2006
 2518 @@ -150,6 +150,10 @@
 2519  # CONFIG_BINFMT_MISC is not set
 2520  # CONFIG_OOM_KILLER is not set
 2521  # CONFIG_CMDLINE_BOOL is not set
 2522 +CONFIG_NTP=y
 2523 +# CONFIG_NTP_PPS is not set
 2524 +# CONFIG_NTP_PPS_DEBUG is not set
 2525 +# CONFIG_NTP_PPS_SERIAL is not set
 2526  
 2527  #
 2528  # Memory Technology Devices (MTD)
 2529 Index: linux/arch/mips/baget/irq.c
 2530 diff -u linux/arch/mips/baget/irq.c:1.1.1.6 linux/arch/mips/baget/irq.c:1.1.1.6.6.1
 2531 --- linux/arch/mips/baget/irq.c:1.1.1.6	Sat Jan 22 21:40:27 2005
 2532 +++ linux/arch/mips/baget/irq.c	Sat Aug 26 21:49:04 2006
 2533 @@ -13,7 +13,7 @@
 2534  #include <linux/types.h>
 2535  #include <linux/interrupt.h>
 2536  #include <linux/ioport.h>
 2537 -#include <linux/timex.h>
 2538 +#include <linux/time.h>
 2539  #include <linux/slab.h>
 2540  #include <linux/random.h>
 2541  #include <linux/delay.h>
 2542 Index: linux/arch/mips/baget/time.c
 2543 diff -u linux/arch/mips/baget/time.c:1.1.1.3 linux/arch/mips/baget/time.c:1.1.1.3.6.1
 2544 --- linux/arch/mips/baget/time.c:1.1.1.3	Sat Jan 22 21:40:27 2005
 2545 +++ linux/arch/mips/baget/time.c	Sat Aug 26 21:49:04 2006
 2546 @@ -12,7 +12,7 @@
 2547  #include <linux/string.h>
 2548  #include <linux/mm.h>
 2549  #include <linux/interrupt.h>
 2550 -#include <linux/timex.h>
 2551 +#include <linux/time.h>
 2552  #include <linux/spinlock.h>
 2553  
 2554  #include <asm/bootinfo.h>
 2555 Index: linux/arch/mips/kernel/setup.c
 2556 diff -u linux/arch/mips/kernel/setup.c:1.1.1.12 linux/arch/mips/kernel/setup.c:1.1.1.12.2.1
 2557 --- linux/arch/mips/kernel/setup.c:1.1.1.12	Sat Aug 26 21:34:46 2006
 2558 +++ linux/arch/mips/kernel/setup.c	Sat Aug 26 21:49:07 2006
 2559 @@ -29,7 +29,6 @@
 2560  #include <linux/bootmem.h>
 2561  #include <linux/blk.h>
 2562  #include <linux/ide.h>
 2563 -#include <linux/timex.h>
 2564  
 2565  #include <asm/asm.h>
 2566  #include <asm/bootinfo.h>
 2567 Index: linux/arch/mips/kernel/sysirix.c
 2568 diff -u linux/arch/mips/kernel/sysirix.c:1.1.1.12 linux/arch/mips/kernel/sysirix.c:1.1.1.12.2.1
 2569 --- linux/arch/mips/kernel/sysirix.c:1.1.1.12	Sat Aug 26 21:34:46 2006
 2570 +++ linux/arch/mips/kernel/sysirix.c	Sat Aug 26 21:49:07 2006
 2571 @@ -13,7 +13,7 @@
 2572  #include <linux/slab.h>
 2573  #include <linux/swap.h>
 2574  #include <linux/errno.h>
 2575 -#include <linux/timex.h>
 2576 +#include <linux/time.h>
 2577  #include <linux/times.h>
 2578  #include <linux/elf.h>
 2579  #include <linux/msg.h>
 2580 @@ -618,19 +618,7 @@
 2581  
 2582  asmlinkage int irix_stime(int value)
 2583  {
 2584 -	if (!capable(CAP_SYS_TIME))
 2585 -		return -EPERM;
 2586 -
 2587 -	write_lock_irq(&xtime_lock);
 2588 -	xtime.tv_sec = value;
 2589 -	xtime.tv_usec = 0;
 2590 -	time_adjust = 0;			/* stop active adjtime() */
 2591 -	time_status |= STA_UNSYNC;
 2592 -	time_maxerror = NTP_PHASE_LIMIT;
 2593 -	time_esterror = NTP_PHASE_LIMIT;
 2594 -	write_unlock_irq(&xtime_lock);
 2595 -
 2596 -	return 0;
 2597 +	return sys_stime(&value);
 2598  }
 2599  
 2600  extern int do_setitimer(int which, struct itimerval *value,
 2601 @@ -1050,10 +1038,7 @@
 2602  
 2603  asmlinkage int irix_gettimeofday(struct timeval *tv)
 2604  {
 2605 -	int retval;
 2606 -
 2607 -	retval = copy_to_user(tv, &xtime, sizeof(*tv)) ? -EFAULT : 0;
 2608 -	return retval;
 2609 +	return sys_gettimeofday(tv, NULL);
 2610  }
 2611  
 2612  #define IRIX_MAP_AUTOGROW 0x40
 2613 Index: linux/arch/mips/kernel/time.c
 2614 diff -u linux/arch/mips/kernel/time.c:1.1.1.11 linux/arch/mips/kernel/time.c:1.1.1.11.2.1
 2615 --- linux/arch/mips/kernel/time.c:1.1.1.11	Sat Aug 26 21:34:46 2006
 2616 +++ linux/arch/mips/kernel/time.c	Sat Aug 26 21:49:07 2006
 2617 @@ -10,12 +10,19 @@
 2618   * under  the terms of  the GNU General  Public License as published by the
 2619   * Free Software Foundation;  either version 2 of the  License, or (at your
 2620   * option) any later version.
 2621 + * 1999-04-07	Ulrich Windl
 2622 + *	Updated for nanosecond resolution (untested)
 2623 + * 1999-12-10	Ulrich Windl
 2624 + *	Implement new logic with ``rtc_update_slave''.
 2625 + *	Use new ``update_rtc'' (untested).
 2626 + * 2000-12-09	Ulrich Windl
 2627 + *	Remove traditional code for microseconds as the nanoseconds seem
 2628 + *	stable.
 2629   */
 2630  #include <linux/config.h>
 2631  #include <linux/types.h>
 2632  #include <linux/kernel.h>
 2633  #include <linux/init.h>
 2634 -#include <linux/sched.h>
 2635  #include <linux/param.h>
 2636  #include <linux/time.h>
 2637  #include <linux/timex.h>
 2638 @@ -45,7 +52,6 @@
 2639   * forward reference
 2640   */
 2641  extern rwlock_t xtime_lock;
 2642 -extern volatile unsigned long wall_jiffies;
 2643  
 2644  spinlock_t rtc_lock = SPIN_LOCK_UNLOCKED;
 2645  
 2646 @@ -151,62 +157,6 @@
 2647  unsigned int (*mips_hpt_read)(void);
 2648  void (*mips_hpt_init)(unsigned int);
 2649  
 2650 -
 2651 -/*
 2652 - * timeofday services, for syscalls.
 2653 - */
 2654 -void do_gettimeofday(struct timeval *tv)
 2655 -{
 2656 -	unsigned long flags, lost;
 2657 -
 2658 -	read_lock_irqsave(&xtime_lock, flags);
 2659 -
 2660 -	*tv = xtime;
 2661 -	tv->tv_usec += do_gettimeoffset();
 2662 -	/*
 2663 -	 * xtime is atomically updated in timer_bh.  jiffies - wall_jiffies
 2664 -	 * is nonzero if the timer bottom half hasn't executed yet.
 2665 -	 */
 2666 -	lost = jiffies - wall_jiffies;
 2667 -	if (lost)
 2668 -		tv->tv_usec += lost * USECS_PER_JIFFY;
 2669 -
 2670 -	read_unlock_irqrestore(&xtime_lock, flags);
 2671 -
 2672 -	while (tv->tv_usec >= 1000000) {
 2673 -		tv->tv_usec -= 1000000;
 2674 -		tv->tv_sec++;
 2675 -	}
 2676 -}
 2677 -
 2678 -void do_settimeofday(struct timeval *tv)
 2679 -{
 2680 -	write_lock_irq(&xtime_lock);
 2681 -
 2682 -	/*
 2683 -	 * This is revolting.  We need to set "xtime" correctly.  However,
 2684 -	 * the value in this location is the value at the most recent update
 2685 -	 * of wall time.  Discover what correction gettimeofday() would have
 2686 -	 * made, and then undo it!
 2687 -	 */
 2688 -	tv->tv_usec -= do_gettimeoffset();
 2689 -	tv->tv_usec -= (jiffies - wall_jiffies) * USECS_PER_JIFFY;
 2690 -
 2691 -	while (tv->tv_usec < 0) {
 2692 -		tv->tv_usec += 1000000;
 2693 -		tv->tv_sec--;
 2694 -	}
 2695 -
 2696 -	xtime = *tv;
 2697 -	time_adjust = 0;			/* stop active adjtime() */
 2698 -	time_status |= STA_UNSYNC;
 2699 -	time_maxerror = NTP_PHASE_LIMIT;
 2700 -	time_esterror = NTP_PHASE_LIMIT;
 2701 -
 2702 -	write_unlock_irq(&xtime_lock);
 2703 -}
 2704 -
 2705 -
 2706  /*
 2707   * Gettimeoffset routines.  These routines returns the time duration
 2708   * since last timer interrupt in usecs.
 2709 @@ -356,6 +306,7 @@
 2710  		: "r" (count), "r" (quotient)
 2711  		: "lo", GCC_REG_ACCUM);
 2712  
 2713 +#ifdef CONFIG_NTP
 2714  	/*
 2715  	 * Due to possible jiffies inconsistencies, we need to check
 2716  	 * the result so that we'll get a timer that is monotonic.
 2717 Index: linux/arch/mips/sni/setup.c
 2718 diff -u linux/arch/mips/sni/setup.c:1.1.1.7 linux/arch/mips/sni/setup.c:1.1.1.7.2.1
 2719 --- linux/arch/mips/sni/setup.c:1.1.1.7	Sat Aug 26 21:34:44 2006
 2720 +++ linux/arch/mips/sni/setup.c	Sat Aug 26 21:49:09 2006
 2721 @@ -14,7 +14,7 @@
 2722  #include <linux/sched.h>
 2723  #include <linux/init.h>
 2724  #include <linux/interrupt.h>
 2725 -#include <linux/timex.h>
 2726 +#include <linux/time.h>
 2727  #include <linux/pci.h>
 2728  #include <linux/mc146818rtc.h>
 2729  #include <linux/console.h>
 2730 Index: linux/arch/mips64/kernel/smp.c
 2731 diff -u linux/arch/mips64/kernel/smp.c:1.1.1.10 linux/arch/mips64/kernel/smp.c:1.1.1.10.6.1
 2732 --- linux/arch/mips64/kernel/smp.c:1.1.1.10	Sat Jan 22 22:02:43 2005
 2733 +++ linux/arch/mips64/kernel/smp.c	Sat Aug 26 21:49:13 2006
 2734 @@ -27,7 +27,6 @@
 2735  #include <linux/threads.h>
 2736  #include <linux/module.h>
 2737  #include <linux/time.h>
 2738 -#include <linux/timex.h>
 2739  #include <linux/sched.h>
 2740  
 2741  #include <asm/atomic.h>
 2742 Index: linux/arch/ppc/config.in
 2743 diff -u linux/arch/ppc/config.in:1.1.1.13 linux/arch/ppc/config.in:1.1.1.13.6.1
 2744 --- linux/arch/ppc/config.in:1.1.1.13	Sat Jan 22 21:53:37 2005
 2745 +++ linux/arch/ppc/config.in	Sat Aug 26 21:49:15 2006
 2746 @@ -121,6 +121,13 @@
 2747   	define_bool CONFIG_TQM8xxL y
 2748    fi
 2749  fi
 2750 +bool 'NTP kernel support' CONFIG_NTP
 2751 +if [ "$CONFIG_NTP" != "n" ]; then
 2752 +  bool '   NTP PPS support' CONFIG_NTP_PPS
 2753 +  if [ "$CONFIG_NTP_PPS" != "n" ]; then
 2754 +    bool '   Debug NTP PPS support' CONFIG_NTP_PPS_DEBUG
 2755 +  fi
 2756 +fi
 2757  
 2758  if [ "$CONFIG_6xx" = "y" -a "$CONFIG_8260" = "n" ]; then
 2759    choice 'Machine Type'		\
 2760 Index: linux/arch/ppc/defconfig
 2761 diff -u linux/arch/ppc/defconfig:1.1.1.12 linux/arch/ppc/defconfig:1.1.1.12.6.1
 2762 --- linux/arch/ppc/defconfig:1.1.1.12	Sat Jan 22 22:02:48 2005
 2763 +++ linux/arch/ppc/defconfig	Sat Aug 26 21:49:15 2006
 2764 @@ -655,6 +655,10 @@
 2765  CONFIG_SERIAL=m
 2766  # CONFIG_SERIAL_EXTENDED is not set
 2767  # CONFIG_SERIAL_NONSTANDARD is not set
 2768 +CONFIG_NTP=y
 2769 +# CONFIG_NTP_PPS is not set
 2770 +# CONFIG_NTP_PPS_DEBUG is not set
 2771 +# CONFIG_NTP_PPS_SERIAL is not set
 2772  CONFIG_UNIX98_PTYS=y
 2773  CONFIG_UNIX98_PTY_COUNT=256
 2774  
 2775 Index: linux/arch/ppc/amiga/time.c
 2776 diff -u linux/arch/ppc/amiga/time.c:1.1.1.6 linux/arch/ppc/amiga/time.c:1.1.1.6.2.1
 2777 --- linux/arch/ppc/amiga/time.c:1.1.1.6	Sat Aug 26 21:34:35 2006
 2778 +++ linux/arch/ppc/amiga/time.c	Sat Aug 26 21:49:18 2006
 2779 @@ -8,8 +8,7 @@
 2780  
 2781  #include <asm/machdep.h>
 2782  #include <asm/io.h>
 2783 -
 2784 -#include <linux/timex.h>
 2785 +#include <asm/timex.h>
 2786  
 2787  unsigned long m68k_get_rtc_time(void)
 2788  {
 2789 Index: linux/arch/ppc/kernel/irq.c
 2790 diff -u linux/arch/ppc/kernel/irq.c:1.1.1.11 linux/arch/ppc/kernel/irq.c:1.1.1.11.6.1
 2791 --- linux/arch/ppc/kernel/irq.c:1.1.1.11	Sat Jan 22 21:40:50 2005
 2792 +++ linux/arch/ppc/kernel/irq.c	Sat Aug 26 21:49:20 2006
 2793 @@ -35,7 +35,7 @@
 2794  #include <linux/sched.h>
 2795  #include <linux/ioport.h>
 2796  #include <linux/interrupt.h>
 2797 -#include <linux/timex.h>
 2798 +#include <linux/time.h>
 2799  #include <linux/config.h>
 2800  #include <linux/init.h>
 2801  #include <linux/slab.h>
 2802 Index: linux/arch/ppc/kernel/time.c
 2803 diff -u linux/arch/ppc/kernel/time.c:1.1.1.14 linux/arch/ppc/kernel/time.c:1.1.1.14.2.1
 2804 --- linux/arch/ppc/kernel/time.c:1.1.1.14	Sat Aug 26 21:34:36 2006
 2805 +++ linux/arch/ppc/kernel/time.c	Sat Aug 26 21:49:20 2006
 2806 @@ -38,20 +38,23 @@
 2807   * Since it is not possible to get a nice 100 Hz clock out of this, without
 2808   * creating a software PLL, I have set HZ to 128.  -- Dan
 2809   *
 2810 - * 1997-09-10  Updated NTP code according to technical memorandum Jan '96
 2811 - *             "A Kernel Model for Precision Timekeeping" by Dave Mills
 2812 + * 1999-05-22	Ulrich Windl
 2813 + *	Updated for nanoseconds and general cleanup (untested)
 2814 + * 1999-12-10	Ulrich Windl
 2815 + *	Implement new logic with ``rtc_update_slave''.
 2816 + * 2000-12-09	Ulrich Windl
 2817 + *	Remove traditional code for microseconds as the nanoseconds seem
 2818 + *	stable.
 2819   */
 2820  
 2821  #include <linux/config.h>
 2822  #include <linux/errno.h>
 2823 -#include <linux/sched.h>
 2824  #include <linux/kernel.h>
 2825  #include <linux/param.h>
 2826  #include <linux/string.h>
 2827  #include <linux/mm.h>
 2828  #include <linux/module.h>
 2829  #include <linux/interrupt.h>
 2830 -#include <linux/timex.h>
 2831  #include <linux/kernel_stat.h>
 2832  #include <linux/mc146818rtc.h>
 2833  #include <linux/time.h>
 2834 @@ -183,15 +186,15 @@
 2835  		 * We should have an rtc call that only sets the minutes and
 2836  		 * seconds like on Intel to avoid problems with non UTC clocks.
 2837  		 */
 2838 -		if ( ppc_md.set_rtc_time && (time_status & STA_UNSYNC) == 0 &&
 2839 -		     xtime.tv_sec - last_rtc_update >= 659 &&
 2840 -		     abs(xtime.tv_usec - (1000000-1000000/HZ)) < 500000/HZ &&
 2841 -		     jiffies - wall_jiffies == 1) {
 2842 -		  	if (ppc_md.set_rtc_time(xtime.tv_sec+1 + timezone_offset) == 0)
 2843 -				last_rtc_update = xtime.tv_sec+1;
 2844 -			else
 2845 -				/* Try again one minute later */
 2846 -				last_rtc_update += 60;
 2847 +		if (rtc_update_slave && ppc_md.set_rtc_time &&
 2848 +		    xtime.tv_sec >= last_rtc_update + rtc_update_slave)
 2849 +		{
 2850 +			if (ppc_md.set_rtc_time(xtime.tv_sec) == 0) {
 2851 +				last_rtc_update = xtime.tv_sec;
 2852 +				rtc_update_slave = 0;
 2853 +			}
 2854 +			else	/* pause for 1 minute */
 2855 +				rtc_update_slave += 60;
 2856  		}
 2857  		write_unlock(&xtime_lock);
 2858  	}
 2859 @@ -211,83 +214,13 @@
 2860  	return 1; /* lets ret_from_int know we can do checks */
 2861  }
 2862  
 2863 -/*
 2864 - * This version of gettimeofday has microsecond resolution.
 2865 - */
 2866 -void do_gettimeofday(struct timeval *tv)
 2867 -{
 2868 -	unsigned long flags;
 2869 -	unsigned delta, lost_ticks, usec, sec;
 2870 -
 2871 -	read_lock_irqsave(&xtime_lock, flags);
 2872 -	sec = xtime.tv_sec;
 2873 -	usec = xtime.tv_usec;
 2874 -	delta = tb_ticks_since(tb_last_stamp);
 2875 -#ifdef CONFIG_SMP
 2876 -	/* As long as timebases are not in sync, gettimeofday can only
 2877 -	 * have jiffy resolution on SMP.
 2878 -	 */
 2879 -	if (!smp_tb_synchronized)
 2880 -		delta = 0;
 2881 -#endif /* CONFIG_SMP */
 2882 -	lost_ticks = jiffies - wall_jiffies;
 2883 -	read_unlock_irqrestore(&xtime_lock, flags);
 2884 -
 2885 -	usec += mulhwu(tb_to_us, tb_ticks_per_jiffy * lost_ticks + delta);
 2886 -	while (usec >= 1000000) {
 2887 -	  	sec++;
 2888 -		usec -= 1000000;
 2889 -	}
 2890 -	tv->tv_sec = sec;
 2891 -	tv->tv_usec = usec;
 2892 -}
 2893 -
 2894 -void do_settimeofday(struct timeval *tv)
 2895 +/* nanoseconds since last timer interrupt */
 2896 +static inline unsigned long do_exact_nanotime(void)
 2897  {
 2898 -	unsigned long flags;
 2899 -	int tb_delta, new_usec, new_sec;
 2900 -
 2901 -	write_lock_irqsave(&xtime_lock, flags);
 2902 -	/* Updating the RTC is not the job of this code. If the time is
 2903 -	 * stepped under NTP, the RTC will be update after STA_UNSYNC
 2904 -	 * is cleared. Tool like clock/hwclock either copy the RTC
 2905 -	 * to the system time, in which case there is no point in writing
 2906 -	 * to the RTC again, or write to the RTC but then they don't call
 2907 -	 * settimeofday to perform this operation. Note also that
 2908 -	 * we don't touch the decrementer since:
 2909 -	 * a) it would lose timer interrupt synchronization on SMP
 2910 -	 * (if it is working one day)
 2911 -	 * b) it could make one jiffy spuriously shorter or longer
 2912 -	 * which would introduce another source of uncertainty potentially
 2913 -	 * harmful to relatively short timers.
 2914 -	 */
 2915 -
 2916 -	/* This works perfectly on SMP only if the tb are in sync but
 2917 -	 * guarantees an error < 1 jiffy even if they are off by eons,
 2918 -	 * still reasonable when gettimeofday resolution is 1 jiffy.
 2919 -	 */
 2920 -	tb_delta = tb_ticks_since(last_jiffy_stamp(smp_processor_id()));
 2921 -	tb_delta += (jiffies - wall_jiffies) * tb_ticks_per_jiffy;
 2922 -	new_sec = tv->tv_sec;
 2923 -	new_usec = tv->tv_usec - mulhwu(tb_to_us, tb_delta);
 2924 -	while (new_usec <0) {
 2925 -		new_sec--;
 2926 -		new_usec += 1000000;
 2927 -	}
 2928 -	xtime.tv_usec = new_usec;
 2929 -	xtime.tv_sec = new_sec;
 2930 -
 2931 -	/* In case of a large backwards jump in time with NTP, we want the
 2932 -	 * clock to be updated as soon as the PLL is again in lock.
 2933 -	 */
 2934 -	last_rtc_update = new_sec - 658;
 2935 -
 2936 -	time_adjust = 0;                /* stop active adjtime() */
 2937 -	time_status |= STA_UNSYNC;
 2938 -	time_state = TIME_ERROR;        /* p. 24, (a) */
 2939 -	time_maxerror = NTP_PHASE_LIMIT;
 2940 -	time_esterror = NTP_PHASE_LIMIT;
 2941 -	write_unlock_irqrestore(&xtime_lock, flags);
 2942 +	return (decrementer_count - get_dec())
 2943 +	    * count_period_num / count_period_den * 1000;
 2944 +	/* please verify! - UW */
 2945 +	/* set_dec(frac_tick * count_period_den / count_period_num); */
 2946  }
 2947  
 2948  /* This function is only called on the boot processor */
 2949 @@ -296,6 +229,7 @@
 2950  	time_t sec, old_sec;
 2951  	unsigned old_stamp, stamp, elapsed;
 2952  
 2953 +	do_nanotime = do_exact_nanotime;
 2954          if (ppc_md.time_init != NULL)
 2955                  timezone_offset = ppc_md.time_init();
 2956  
 2957 @@ -331,7 +265,8 @@
 2958  		if (sec == old_sec)
 2959  			printk("Warning: real time clock seems stuck!\n");
 2960  		xtime.tv_sec = sec;
 2961 -		xtime.tv_usec = 0;
 2962 +		xtime.tv_nsec = 0;
 2963 +		timevar_init();
 2964  		/* No update now, we just read the time from the RTC ! */
 2965  		last_rtc_update = xtime.tv_sec;
 2966  	}
 2967 @@ -426,3 +361,10 @@
 2968  	return mlt;
 2969  }
 2970  
 2971 +/* Fine-tune the tick interpolation for a total frequency error
 2972 + * `freq' / 1000 PPM.
 2973 + */
 2974 +void update_nanoscale(long freq)
 2975 +{
 2976 +	/* not implemented -- this is just a prototype [UW] */
 2977 +}
 2978 Index: linux/arch/s390/config.in
 2979 diff -u linux/arch/s390/config.in:1.1.1.10 linux/arch/s390/config.in:1.1.1.10.6.1
 2980 --- linux/arch/s390/config.in:1.1.1.10	Sat Jan 22 21:53:50 2005
 2981 +++ linux/arch/s390/config.in	Sat Aug 26 21:49:22 2006
 2982 @@ -64,6 +64,17 @@
 2983  bool 'VM shared kernel support' CONFIG_SHARED_KERNEL
 2984  endmenu
 2985  
 2986 +mainmenu_option next_comment
 2987 +comment 'Time configuration'
 2988 +bool 'NTP kernel support' CONFIG_NTP
 2989 +if [ "$CONFIG_NTP" != "n" ]; then
 2990 +  bool '   NTP PPS support' CONFIG_NTP_PPS
 2991 +  if [ "$CONFIG_NTP_PPS" != "n" ]; then
 2992 +    bool '   Debug NTP PPS support' CONFIG_NTP_PPS_DEBUG
 2993 +  fi
 2994 +fi
 2995 +endmenu
 2996 +
 2997  source drivers/s390/Config.in
 2998  
 2999  if [ "$CONFIG_NET" = "y" ]; then
 3000 Index: linux/arch/s390/defconfig
 3001 diff -u linux/arch/s390/defconfig:1.1.1.14 linux/arch/s390/defconfig:1.1.1.14.2.1
 3002 --- linux/arch/s390/defconfig:1.1.1.14	Sat Aug 26 21:34:46 2006
 3003 +++ linux/arch/s390/defconfig	Sat Aug 26 21:49:22 2006
 3004 @@ -52,6 +52,14 @@
 3005  # CONFIG_SHARED_KERNEL is not set
 3006  
 3007  #
 3008 +# Time configuration
 3009 +#
 3010 +CONFIG_NTP=y
 3011 +# CONFIG_NTP_PPS is not set
 3012 +# CONFIG_NTP_PPS_DEBUG is not set
 3013 +# CONFIG_NTP_PPS_SERIAL is not set
 3014 +
 3015 +#
 3016  # Block device drivers
 3017  #
 3018  CONFIG_BLK_DEV_LOOP=y
 3019 Index: linux/arch/s390/kernel/time.c
 3020 diff -u linux/arch/s390/kernel/time.c:1.1.1.12 linux/arch/s390/kernel/time.c:1.1.1.12.2.1
 3021 --- linux/arch/s390/kernel/time.c:1.1.1.12	Sat Aug 26 21:34:46 2006
 3022 +++ linux/arch/s390/kernel/time.c	Sat Aug 26 21:49:25 2006
 3023 @@ -9,10 +9,14 @@
 3024   *
 3025   *  Derived from "arch/i386/kernel/time.c"
 3026   *    Copyright (C) 1991, 1992, 1995  Linus Torvalds
 3027 + * 2000-01-23	Ulrich Windl
 3028 + *	Merged changes for PPSkit-0.9.1 from i386 architecture [untested].
 3029 + * 2000-12-09	Ulrich Windl
 3030 + *	Remove traditional code for microseconds as the nanoseconds seem
 3031 + *	stable.
 3032   */
 3033  
 3034  #include <linux/errno.h>
 3035 -#include <linux/sched.h>
 3036  #include <linux/kernel.h>
 3037  #include <linux/param.h>
 3038  #include <linux/string.h>
 3039 @@ -28,7 +32,6 @@
 3040  #include <asm/delay.h>
 3041  #include <asm/s390_ext.h>
 3042  
 3043 -#include <linux/timex.h>
 3044  #include <linux/config.h>
 3045  
 3046  #include <asm/irq.h>
 3047 @@ -49,10 +52,11 @@
 3048  static u64 init_timer_cc;
 3049  static u64 xtime_cc;
 3050  
 3051 -extern rwlock_t xtime_lock;
 3052 -extern unsigned long wall_jiffies;
 3053 -
 3054 -void tod_to_timeval(__u64 todval, struct timeval *xtime)
 3055 +#ifndef NANOSECOND
 3056 +#define NANOSECOND 1000000000
 3057 +#endif
 3058 +  
 3059 +void tod_to_timespec(__u64 todval, struct timespec *xtime)
 3060  {
 3061          const int high_bit = 0x80000000L;
 3062          const int c_f4240 = 0xf4240L;
 3063 @@ -84,9 +88,11 @@
 3064  		      : "=m" (*xtime) : "m" (todval),
 3065  		        "m" (c_7a120), "m" (high_bit), "m" (c_f4240)
 3066  		      : "cc", "memory", "2", "3", "4" );
 3067 +	xtime->tv_nsec *= 1000;	/* UW: sorry, I can't do S390 assembler */
 3068  }
 3069  
 3070 -static inline unsigned long do_gettimeoffset(void) 
 3071 +/* nanoseconds since last timer interrupt */
 3072 +unsigned long do_nanotime(void) 
 3073  {
 3074  	__u64 now;
 3075  
 3076 @@ -94,54 +100,7 @@
 3077          now = (now - init_timer_cc) >> 12;
 3078  	/* We require the offset from the latest update of xtime */
 3079  	now -= (__u64) wall_jiffies*USECS_PER_JIFFY;
 3080 -	return (unsigned long) now;
 3081 -}
 3082 -
 3083 -/*
 3084 - * This version of gettimeofday has microsecond resolution.
 3085 - */
 3086 -void do_gettimeofday(struct timeval *tv)
 3087 -{
 3088 -	unsigned long flags;
 3089 -	unsigned long usec, sec;
 3090 -
 3091 -	read_lock_irqsave(&xtime_lock, flags);
 3092 -	sec = xtime.tv_sec;
 3093 -	usec = xtime.tv_usec + do_gettimeoffset();
 3094 -	read_unlock_irqrestore(&xtime_lock, flags);
 3095 -
 3096 -	while (usec >= 1000000) {
 3097 -		usec -= 1000000;
 3098 -		sec++;
 3099 -	}
 3100 -
 3101 -	tv->tv_sec = sec;
 3102 -	tv->tv_usec = usec;
 3103 -}
 3104 -
 3105 -void do_settimeofday(struct timeval *tv)
 3106 -{
 3107 -
 3108 -	write_lock_irq(&xtime_lock);
 3109 -	/* This is revolting. We need to set the xtime.tv_usec
 3110 -	 * correctly. However, the value in this location is
 3111 -	 * is value at the last tick.
 3112 -	 * Discover what correction gettimeofday
 3113 -	 * would have done, and then undo it!
 3114 -	 */
 3115 -	tv->tv_usec -= do_gettimeoffset();
 3116 -
 3117 -	while (tv->tv_usec < 0) {
 3118 -		tv->tv_usec += 1000000;
 3119 -		tv->tv_sec--;
 3120 -	}
 3121 -
 3122 -	xtime = *tv;
 3123 -	time_adjust = 0;		/* stop active adjtime() */
 3124 -	time_status |= STA_UNSYNC;
 3125 -	time_maxerror = NTP_PHASE_LIMIT;
 3126 -	time_esterror = NTP_PHASE_LIMIT;
 3127 -	write_unlock_irq(&xtime_lock);
 3128 +	return (unsigned long) now * 1000;
 3129  }
 3130  
 3131  static inline __u32 div64_32(__u64 dividend, __u32 divisor)
 3132 @@ -263,7 +222,8 @@
 3133  	xtime_cc = init_timer_cc + CLK_TICKS_PER_JIFFY;
 3134          set_time_cc = init_timer_cc - 0x8126d60e46000000LL +
 3135                        (0x3c26700LL*1000000*4096);
 3136 -        tod_to_timeval(set_time_cc, &xtime);
 3137 +        tod_to_timespec(set_time_cc, &xtime);
 3138 +	timevar_init();
 3139  
 3140          /* request the 0x1004 external interrupt */
 3141          if (register_early_external_interrupt(0x1004, NULL,
 3142 @@ -273,3 +233,11 @@
 3143          /* init CPU timer */
 3144          init_cpu_timer();
 3145  }
 3146 +
 3147 +/* Fine-tune the tick interpolation for a total frequency error
 3148 + * `freq' / 1000 PPM.
 3149 + */
 3150 +void update_nanoscale(long freq)
 3151 +{
 3152 +	/* not implemented -- this is just a prototype [UW] */
 3153 +}
 3154 Index: linux/arch/s390x/kernel/time.c
 3155 diff -u linux/arch/s390x/kernel/time.c:1.1.1.8 linux/arch/s390x/kernel/time.c:1.1.1.8.6.1
 3156 --- linux/arch/s390x/kernel/time.c:1.1.1.8	Sat Jan 22 21:53:52 2005
 3157 +++ linux/arch/s390x/kernel/time.c	Sat Aug 26 21:49:29 2006
 3158 @@ -238,3 +238,11 @@
 3159          /* init CPU timer */
 3160          init_cpu_timer();
 3161  }
 3162 +
 3163 +/* Fine-tune the tick interpolation for a total frequency error
 3164 + * `freq' / 1000 PPM.
 3165 + */
 3166 +void update_nanoscale(long freq)
 3167 +{
 3168 +	/* not implemented -- this is just a prototype [UW] */
 3169 +}
 3170 Index: linux/arch/sh/kernel/irq.c
 3171 diff -u linux/arch/sh/kernel/irq.c:1.1.1.9 linux/arch/sh/kernel/irq.c:1.1.1.9.2.1
 3172 --- linux/arch/sh/kernel/irq.c:1.1.1.9	Sat Aug 26 21:34:33 2006
 3173 +++ linux/arch/sh/kernel/irq.c	Sat Aug 26 21:49:31 2006
 3174 @@ -21,7 +21,7 @@
 3175  #include <linux/sched.h>
 3176  #include <linux/ioport.h>
 3177  #include <linux/interrupt.h>
 3178 -#include <linux/timex.h>
 3179 +#include <linux/time.h>
 3180  #include <linux/slab.h>
 3181  #include <linux/random.h>
 3182  #include <linux/smp.h>
 3183 Index: linux/arch/sh/kernel/time.c
 3184 diff -u linux/arch/sh/kernel/time.c:1.1.1.8 linux/arch/sh/kernel/time.c:1.1.1.8.2.1
 3185 --- linux/arch/sh/kernel/time.c:1.1.1.8	Sat Aug 26 21:34:33 2006
 3186 +++ linux/arch/sh/kernel/time.c	Sat Aug 26 21:49:31 2006
 3187 @@ -34,7 +34,7 @@
 3188  #include <asm/kgdb.h>
 3189  #endif
 3190  
 3191 -#include <linux/timex.h>
 3192 +#include <linux/time.h>
 3193  #include <linux/irq.h>
 3194  
 3195  #define TMU_TOCR_INIT	0x00	/* Don't output RTC clock */
 3196 @@ -154,52 +154,12 @@
 3197  
 3198  void do_gettimeofday(struct timeval *tv)
 3199  {
 3200 -	unsigned long flags;
 3201 -	unsigned long usec, sec;
 3202 -
 3203 -	read_lock_irqsave(&xtime_lock, flags);
 3204 -	usec = do_gettimeoffset();
 3205 -	{
 3206 -		unsigned long lost = jiffies - wall_jiffies;
 3207 -		if (lost)
 3208 -			usec += lost * (1000000 / HZ);
 3209 -	}
 3210 -	sec = xtime.tv_sec;
 3211 -	usec += xtime.tv_usec;
 3212 -	read_unlock_irqrestore(&xtime_lock, flags);
 3213 -
 3214 -	while (usec >= 1000000) {
 3215 -		usec -= 1000000;
 3216 -		sec++;
 3217 -	}
 3218 -
 3219 -	tv->tv_sec = sec;
 3220 -	tv->tv_usec = usec;
 3221 +	sys_gettimeofday(tv, NULL);
 3222  }
 3223  
 3224  void do_settimeofday(struct timeval *tv)
 3225  {
 3226 -	write_lock_irq(&xtime_lock);
 3227 -	/*
 3228 -	 * This is revolting. We need to set "xtime" correctly. However, the
 3229 -	 * value in this location is the value at the most recent update of
 3230 -	 * wall time.  Discover what correction gettimeofday() would have
 3231 -	 * made, and then undo it!
 3232 -	 */
 3233 -	tv->tv_usec -= do_gettimeoffset();
 3234 -	tv->tv_usec -= (jiffies - wall_jiffies) * (1000000 / HZ);
 3235 -
 3236 -	while (tv->tv_usec < 0) {
 3237 -		tv->tv_usec += 1000000;
 3238 -		tv->tv_sec--;
 3239 -	}
 3240 -
 3241 -	xtime = *tv;
 3242 -	time_adjust = 0;		/* stop active adjtime() */
 3243 -	time_status |= STA_UNSYNC;
 3244 -	time_maxerror = NTP_PHASE_LIMIT;
 3245 -	time_esterror = NTP_PHASE_LIMIT;
 3246 -	write_unlock_irq(&xtime_lock);
 3247 +	sys_settimeofday(tv, NULL);
 3248  }
 3249  
 3250  /* last time the RTC clock got updated */
 3251 @@ -241,20 +201,18 @@
 3252  	if (sh_mv.mv_heartbeat != NULL) 
 3253  		sh_mv.mv_heartbeat();
 3254  #endif
 3255 -
 3256  	/*
 3257 -	 * If we have an externally synchronized Linux clock, then update
 3258 -	 * RTC clock accordingly every ~11 minutes. Set_rtc_mmss() has to be
 3259 -	 * called as close as possible to 500 ms before the new second starts.
 3260 +	 * If we have a change of the software clock to be committed to the
 3261 +	 * CMOS clock, then do it. Update_rtc() has to be called as close as
 3262 +	 * possible to 500 ms before the new second starts.
 3263  	 */
 3264 -	if ((time_status & STA_UNSYNC) == 0 &&
 3265 -	    xtime.tv_sec > last_rtc_update + 660 &&
 3266 -	    xtime.tv_usec >= 500000 - ((unsigned) tick) / 2 &&
 3267 -	    xtime.tv_usec <= 500000 + ((unsigned) tick) / 2) {
 3268 -		if (sh_mv.mv_rtc_settimeofday(&xtime) == 0)
 3269 -			last_rtc_update = xtime.tv_sec;
 3270 -		else
 3271 -			last_rtc_update = xtime.tv_sec - 600; /* do it again in 60 s */
 3272 +	if (rtc_update_slave &&
 3273 +	    xtime.tv_sec >= last_rtc_update + rtc_update_slave &&
 3274 +	    xtime.tv_nsec >= NANOSECOND / 2 - ((unsigned long) time_tick) / 2 &&
 3275 +	    xtime.tv_nsec <= NANOSECOND / 2 + ((unsigned long) time_tick) / 2) {
 3276 +		update_rtc();
 3277 +		last_rtc_update = xtime.tv_sec;
 3278 +		rtc_update_slave = 0;
 3279  	}
 3280  }
 3281  
 3282 @@ -573,3 +531,11 @@
 3283  #endif
 3284  
 3285  }
 3286 +
 3287 +/* Fine-tune the tick interpolation for a total frequency error
 3288 + * `freq' / 1000 PPM.
 3289 + */
 3290 +void update_nanoscale(long freq)
 3291 +{
 3292 +	/* not implemented -- this is just a prototype [UW] */
 3293 +}
 3294 Index: linux/arch/sparc/config.in
 3295 diff -u linux/arch/sparc/config.in:1.1.1.10 linux/arch/sparc/config.in:1.1.1.10.6.1
 3296 --- linux/arch/sparc/config.in:1.1.1.10	Sat Jan 22 21:53:56 2005
 3297 +++ linux/arch/sparc/config.in	Sat Aug 26 21:49:34 2006
 3298 @@ -93,6 +93,13 @@
 3299     source drivers/sbus/char/Config.in
 3300     source drivers/sbus/audio/Config.in
 3301  fi
 3302 +bool 'NTP kernel support' CONFIG_NTP
 3303 +if [ "$CONFIG_NTP" != "n" ]; then
 3304 +  bool '   NTP PPS support' CONFIG_NTP_PPS
 3305 +  if [ "$CONFIG_NTP_PPS" != "n" ]; then
 3306 +    bool '   Debug NTP PPS support' CONFIG_NTP_PPS_DEBUG
 3307 +  fi
 3308 +fi
 3309  
 3310  mainmenu_option next_comment
 3311  comment 'Block devices'
 3312 Index: linux/arch/sparc/defconfig
 3313 diff -u linux/arch/sparc/defconfig:1.1.1.12 linux/arch/sparc/defconfig:1.1.1.12.2.1
 3314 --- linux/arch/sparc/defconfig:1.1.1.12	Sat Aug 26 21:34:51 2006
 3315 +++ linux/arch/sparc/defconfig	Sat Aug 26 21:49:34 2006
 3316 @@ -54,6 +54,10 @@
 3317  CONFIG_BINFMT_MISC=m
 3318  CONFIG_SUNOS_EMUL=y
 3319  # CONFIG_OOM_KILLER is not set
 3320 +CONFIG_NTP=y
 3321 +# CONFIG_NTP_PPS is not set
 3322 +# CONFIG_NTP_PPS_DEBUG is not set
 3323 +# CONFIG_NTP_PPS_SERIAL is not set
 3324  
 3325  #
 3326  # Parallel port support
 3327 Index: linux/arch/sparc/kernel/pcic.c
 3328 diff -u linux/arch/sparc/kernel/pcic.c:1.1.1.9 linux/arch/sparc/kernel/pcic.c:1.1.1.9.2.1
 3329 --- linux/arch/sparc/kernel/pcic.c:1.1.1.9	Sat Aug 26 21:34:51 2006
 3330 +++ linux/arch/sparc/kernel/pcic.c	Sat Aug 26 21:49:36 2006
 3331 @@ -24,7 +24,7 @@
 3332  
 3333  #include <linux/ctype.h>
 3334  #include <linux/pci.h>
 3335 -#include <linux/timex.h>
 3336 +#include <linux/time.h>
 3337  #include <linux/interrupt.h>
 3338  
 3339  #include <asm/irq.h>
 3340 @@ -828,20 +828,10 @@
 3341  	}       
 3342  }
 3343  
 3344 +/* is this needed at all? */
 3345  static void pci_do_settimeofday(struct timeval *tv)
 3346  {
 3347 -	cli();
 3348 -	tv->tv_usec -= do_gettimeoffset();
 3349 -	if(tv->tv_usec < 0) {
 3350 -		tv->tv_usec += 1000000;
 3351 -		tv->tv_sec--;
 3352 -	}
 3353 -	xtime = *tv;
 3354 -	time_adjust = 0;		/* stop active adjtime() */
 3355 -	time_status |= STA_UNSYNC;
 3356 -	time_maxerror = NTP_PHASE_LIMIT;
 3357 -	time_esterror = NTP_PHASE_LIMIT;
 3358 -	sti();
 3359 +	do_settimeofday(tv);
 3360  }
 3361  
 3362  #if 0
 3363 Index: linux/arch/sparc/kernel/tick14.c
 3364 diff -u linux/arch/sparc/kernel/tick14.c:1.1.1.3 linux/arch/sparc/kernel/tick14.c:1.1.1.3.2.1
 3365 --- linux/arch/sparc/kernel/tick14.c:1.1.1.3	Sat Aug 26 21:34:51 2006
 3366 +++ linux/arch/sparc/kernel/tick14.c	Sat Aug 26 21:49:36 2006
 3367 @@ -15,11 +15,12 @@
 3368  #include <linux/param.h>
 3369  #include <linux/string.h>
 3370  #include <linux/mm.h>
 3371 -#include <linux/timex.h>
 3372 +#include <linux/time.h>
 3373  
 3374  #include <asm/oplib.h>
 3375  #include <asm/segment.h>
 3376  #include <asm/timer.h>
 3377 +#include <asm/timex.h>
 3378  #include <asm/mostek.h>
 3379  #include <asm/system.h>
 3380  #include <asm/irq.h>
 3381 Index: linux/arch/sparc/kernel/time.c
 3382 diff -u linux/arch/sparc/kernel/time.c:1.1.1.8 linux/arch/sparc/kernel/time.c:1.1.1.8.2.1
 3383 --- linux/arch/sparc/kernel/time.c:1.1.1.8	Sat Aug 26 21:34:51 2006
 3384 +++ linux/arch/sparc/kernel/time.c	Sat Aug 26 21:49:36 2006
 3385 @@ -12,18 +12,20 @@
 3386   *
 3387   * This file handles the Sparc specific time handling details.
 3388   *
 3389 - * 1997-09-10	Updated NTP code according to technical memorandum Jan '96
 3390 - *		"A Kernel Model for Precision Timekeeping" by Dave Mills
 3391 + * 1999-03-10	Ulrich Windl
 3392 + *	Updated for nanokernel resolution (untested)
 3393 + * 1999-12-10	Ulrich Windl
 3394 + *	Implement new logic with ``rtc_update_slave''.
 3395 + *	Use new ``update_rtc()'' (untested).
 3396   */
 3397  #include <linux/config.h>
 3398  #include <linux/errno.h>
 3399 -#include <linux/sched.h>
 3400 +#include <linux/time.h>
 3401  #include <linux/kernel.h>
 3402  #include <linux/param.h>
 3403  #include <linux/string.h>
 3404  #include <linux/mm.h>
 3405  #include <linux/interrupt.h>
 3406 -#include <linux/timex.h>
 3407  #include <linux/init.h>
 3408  #include <linux/pci.h>
 3409  #include <linux/ioport.h>
 3410 @@ -41,13 +43,11 @@
 3411  #include <asm/page.h>
 3412  #include <asm/pcic.h>
 3413  
 3414 -extern rwlock_t xtime_lock;
 3415 -
 3416  enum sparc_clock_type sp_clock_typ;
 3417  spinlock_t mostek_lock = SPIN_LOCK_UNLOCKED;
 3418  unsigned long mstk48t02_regs = 0UL;
 3419  static struct mostek48t08 *mstk48t08_regs = 0;
 3420 -static int set_rtc_mmss(unsigned long);
 3421 +static void update_rtc(void);
 3422  static void sbus_do_settimeofday(struct timeval *tv);
 3423  
 3424  #ifdef CONFIG_SUN4
 3425 @@ -138,14 +138,13 @@
 3426  	do_timer(regs);
 3427  
 3428  	/* Determine when to update the Mostek clock. */
 3429 -	if ((time_status & STA_UNSYNC) == 0 &&
 3430 -	    xtime.tv_sec > last_rtc_update + 660 &&
 3431 -	    xtime.tv_usec >= 500000 - ((unsigned) tick) / 2 &&
 3432 -	    xtime.tv_usec <= 500000 + ((unsigned) tick) / 2) {
 3433 -	  if (set_rtc_mmss(xtime.tv_sec) == 0)
 3434 -	    last_rtc_update = xtime.tv_sec;
 3435 -	  else
 3436 -	    last_rtc_update = xtime.tv_sec - 600; /* do it again in 60 s */
 3437 +	if (rtc_update_slave &&
 3438 +	    xtime.tv_sec >= last_rtc_update + rtc_update_slave &&
 3439 +	    xtime.tv_nsec >= 500000000 - ((unsigned long) time_tick) / 2 &&
 3440 +	    xtime.tv_nsec <= 500000000 + ((unsigned long) time_tick) / 2) {
 3441 +		update_rtc();
 3442 +		last_rtc_update = xtime.tv_sec;
 3443 +		rtc_update_slave = 0;
 3444  	}
 3445  	write_unlock(&xtime_lock);
 3446  }
 3447 @@ -371,6 +370,9 @@
 3448  	struct intersil *iregs;
 3449  #endif
 3450  
 3451 +	do_microtime = do_exact_microtime;
 3452 +	do_nanotime = do_exact_nanotime;
 3453 +	timevar_init();		/* architecture independent initialization */
 3454  	BTFIXUPSET_CALL(bus_do_settimeofday, sbus_do_settimeofday, BTFIXUPCALL_NORM);
 3455  	btfixup();
 3456  
 3457 @@ -398,7 +400,7 @@
 3458  	mon = MSTK_REG_MONTH(mregs);
 3459  	year = MSTK_CVT_YEAR( MSTK_REG_YEAR(mregs) );
 3460  	xtime.tv_sec = mktime(year, mon, day, hour, min, sec);
 3461 -	xtime.tv_usec = 0;
 3462 +	xtime.tv_nsec = 0;
 3463  	mregs->creg &= ~MSTK_CREG_READ;
 3464  	spin_unlock_irq(&mostek_lock);
 3465  #ifdef CONFIG_SUN4
 3466 @@ -429,7 +431,7 @@
 3467  		intersil_start(iregs);
 3468  
 3469  		xtime.tv_sec = mktime(year, mon, day, hour, min, sec);
 3470 -		xtime.tv_usec = 0;
 3471 +		xtime.tv_nsec = 0;
 3472  		printk("%u/%u/%u %u:%u:%u\n",day,mon,year,hour,min,sec);
 3473  	}
 3474  #endif
 3475 @@ -465,78 +467,25 @@
 3476  	return offset + count;
 3477  }
 3478  
 3479 -/* This need not obtain the xtime_lock as it is coded in
 3480 - * an implicitly SMP safe way already.
 3481 - */
 3482 -void do_gettimeofday(struct timeval *tv)
 3483 +/* microseconds since last timer interrupt */
 3484 +static inline unsigned long do_exact_microtime(void)
 3485  {
 3486 -	/* Load doubles must be used on xtime so that what we get
 3487 -	 * is guarenteed to be atomic, this is why we can run this
 3488 -	 * with interrupts on full blast.  Don't touch this... -DaveM
 3489 -	 */
 3490 -	__asm__ __volatile__(
 3491 -	"sethi	%hi(master_l10_counter), %o1\n\t"
 3492 -	"ld	[%o1 + %lo(master_l10_counter)], %g3\n\t"
 3493 -	"sethi	%hi(xtime), %g2\n"
 3494 -	"1:\n\t"
 3495 -	"ldd	[%g2 + %lo(xtime)], %o4\n\t"
 3496 -	"ld	[%g3], %o1\n\t"
 3497 -	"ldd	[%g2 + %lo(xtime)], %o2\n\t"
 3498 -	"xor	%o4, %o2, %o2\n\t"
 3499 -	"xor	%o5, %o3, %o3\n\t"
 3500 -	"orcc	%o2, %o3, %g0\n\t"
 3501 -	"bne	1b\n\t"
 3502 -	" cmp	%o1, 0\n\t"
 3503 -	"bge	1f\n\t"
 3504 -	" srl	%o1, 0xa, %o1\n\t"
 3505 -	"sethi	%hi(tick), %o3\n\t"
 3506 -	"ld	[%o3 + %lo(tick)], %o3\n\t"
 3507 -	"sethi	%hi(0x1fffff), %o2\n\t"
 3508 -	"or	%o2, %lo(0x1fffff), %o2\n\t"
 3509 -	"add	%o5, %o3, %o5\n\t"
 3510 -	"and	%o1, %o2, %o1\n"
 3511 -	"1:\n\t"
 3512 -	"add	%o5, %o1, %o5\n\t"
 3513 -	"sethi	%hi(1000000), %o2\n\t"
 3514 -	"or	%o2, %lo(1000000), %o2\n\t"
 3515 -	"cmp	%o5, %o2\n\t"
 3516 -	"bl,a	1f\n\t"
 3517 -	" st	%o4, [%o0 + 0x0]\n\t"
 3518 -	"add	%o4, 0x1, %o4\n\t"
 3519 -	"sub	%o5, %o2, %o5\n\t"
 3520 -	"st	%o4, [%o0 + 0x0]\n"
 3521 -	"1:\n\t"
 3522 -	"st	%o5, [%o0 + 0x4]\n");
 3523 -}
 3524 -
 3525 -void do_settimeofday(struct timeval *tv)
 3526 -{
 3527 -	write_lock_irq(&xtime_lock);
 3528 -	bus_do_settimeofday(tv);
 3529 -	write_unlock_irq(&xtime_lock);
 3530 -}
 3531 -
 3532 -static void sbus_do_settimeofday(struct timeval *tv)
 3533 -{
 3534 -	tv->tv_usec -= do_gettimeoffset();
 3535 -	if(tv->tv_usec < 0) {
 3536 -		tv->tv_usec += 1000000;
 3537 -		tv->tv_sec--;
 3538 -	}
 3539 -	xtime = *tv;
 3540 -	time_adjust = 0;		/* stop active adjtime() */
 3541 -	time_status |= STA_UNSYNC;
 3542 -	time_maxerror = NTP_PHASE_LIMIT;
 3543 -	time_esterror = NTP_PHASE_LIMIT;
 3544 +	return do_gettimeoffset();
 3545 +}
 3546 +
 3547 +/* nanoseconds since last timer interrupt */
 3548 +static inline unsigned long do_exact_nanotime(void)
 3549 +{
 3550 +	return do_exact_microtime() * 1000;
 3551  }
 3552  
 3553  /*
 3554 - * BUG: This routine does not handle hour overflow properly; it just
 3555 - *      sets the minutes. Usually you won't notice until after reboot!
 3556 + * Update the RTC from kernel time
 3557   */
 3558 -static int set_rtc_mmss(unsigned long nowtime)
 3559 +static void update_rtc(void)
 3560  {
 3561 -	int real_seconds, real_minutes, mostek_minutes;
 3562 +	int ss, mi, hh, dd, mo, yy;
 3563 +	unsigned char save_control, save_freq_select;
 3564  	struct mostek48t02 *regs = (struct mostek48t02 *)mstk48t02_regs;
 3565  	unsigned long flags;
 3566  #ifdef CONFIG_SUN4
 3567 @@ -547,32 +496,30 @@
 3568  	/* Not having a register set can lead to trouble. */
 3569  	if (!regs) {
 3570  #ifdef CONFIG_SUN4
 3571 -		if(!iregs)
 3572 -		return -1;
 3573 +		if (!iregs)
 3574 +			return -1;
 3575  	 	else {
 3576  			temp = iregs->clk.int_csec;
 3577  
 3578 -			mostek_minutes = iregs->clk.int_min;
 3579 -
 3580 -			real_seconds = nowtime % 60;
 3581 -			real_minutes = nowtime / 60;
 3582 -			if (((abs(real_minutes - mostek_minutes) + 15)/30) & 1)
 3583 -				real_minutes += 30;	/* correct for half hour time zone */
 3584 -			real_minutes %= 60;
 3585 -
 3586 -			if (abs(real_minutes - mostek_minutes) < 30) {
 3587 -				intersil_stop(iregs);
 3588 -				iregs->clk.int_sec=real_seconds;
 3589 -				iregs->clk.int_min=real_minutes;
 3590 -				intersil_start(iregs);
 3591 -			} else {
 3592 -				printk(KERN_WARNING
 3593 -			       "set_rtc_mmss: can't update from %d to %d\n",
 3594 -				       mostek_minutes, real_minutes);
 3595 -				return -1;
 3596 -			}
 3597 -			
 3598 -			return 0;
 3599 +			ktime_to_rtc(xtime.tv_sec, &ss, &dd);
 3600 +			hh = ss >> 16;
 3601 +			mi = (ss >> 8) & 0xff;
 3602 +			ss &= 0xff;
 3603 +			yy = (dd >> 16) % 100;
 3604 +			mo = (dd >> 8) & 0xff;
 3605 +			dd &= 0xff;
 3606 +			printk(KERN_DEBUG
 3607 +			       "update_rtc: %d-%02d-%02d %02d:%02d:%02d\n",
 3608 +			       yy, mo, dd, hh, mi, ss);
 3609 +
 3610 +			intersil_stop(iregs);
 3611 +			iregs->clk.int_sec = ss;
 3612 +			iregs->clk.int_min = mi;
 3613 +			iregs->clk.int_hour = hh;
 3614 +			iregs->clk.int_dom = dd;
 3615 +			iregs->clk.int_month = mo;
 3616 +			iregs->clk.int_year = yy;
 3617 +			intersil_start(iregs);
 3618  		}
 3619  #endif
 3620  	}
 3621 @@ -607,3 +554,11 @@
 3622  		return -1;
 3623  	}
 3624  }
 3625 +
 3626 +/* Fine-tune the tick interpolation for a total frequency error
 3627 + * `freq' / 1000 PPM.
 3628 + */
 3629 +void update_nanoscale(long freq)
 3630 +{
 3631 +	/* not implemented -- this is just a prototype [UW] */
 3632 +}
 3633 Index: linux/arch/sparc64/config.in
 3634 diff -u linux/arch/sparc64/config.in:1.1.1.13 linux/arch/sparc64/config.in:1.1.1.13.6.1
 3635 --- linux/arch/sparc64/config.in:1.1.1.13	Sat Jan 22 21:53:58 2005
 3636 +++ linux/arch/sparc64/config.in	Sat Aug 26 21:49:39 2006
 3637 @@ -125,6 +125,13 @@
 3638  if [ "$CONFIG_NET" = "y" ]; then
 3639     source net/Config.in
 3640  fi
 3641 +bool 'NTP kernel support' CONFIG_NTP
 3642 +if [ "$CONFIG_NTP" != "n" ]; then
 3643 +  bool '   NTP PPS support' CONFIG_NTP_PPS
 3644 +  if [ "$CONFIG_NTP_PPS" != "n" ]; then
 3645 +    bool '   Debug NTP PPS support' CONFIG_NTP_PPS_DEBUG
 3646 +  fi
 3647 +fi
 3648  
 3649  mainmenu_option next_comment
 3650  comment 'ATA/IDE/MFM/RLL support'
 3651 Index: linux/arch/sparc64/defconfig
 3652 diff -u linux/arch/sparc64/defconfig:1.1.1.19 linux/arch/sparc64/defconfig:1.1.1.19.2.1
 3653 --- linux/arch/sparc64/defconfig:1.1.1.19	Sat Aug 26 21:34:51 2006
 3654 +++ linux/arch/sparc64/defconfig	Sat Aug 26 21:49:39 2006
 3655 @@ -74,6 +74,10 @@
 3656  # CONFIG_SUNOS_EMUL is not set
 3657  CONFIG_SOLARIS_EMUL=m
 3658  # CONFIG_OOM_KILLER is not set
 3659 +CONFIG_NTP=y
 3660 +# CONFIG_NTP_PPS is not set
 3661 +# CONFIG_NTP_PPS_DEBUG is not set
 3662 +# CONFIG_NTP_PPS_SERIAL is not set
 3663  
 3664  #
 3665  # Parallel port support
 3666 Index: linux/arch/sparc64/kernel/sys_sparc32.c
 3667 diff -u linux/arch/sparc64/kernel/sys_sparc32.c:1.1.1.19 linux/arch/sparc64/kernel/sys_sparc32.c:1.1.1.19.2.1
 3668 --- linux/arch/sparc64/kernel/sys_sparc32.c:1.1.1.19	Sat Aug 26 21:34:52 2006
 3669 +++ linux/arch/sparc64/kernel/sys_sparc32.c	Sat Aug 26 21:49:42 2006
 3670 @@ -19,7 +19,7 @@
 3671  #include <linux/resource.h>
 3672  #include <linux/times.h>
 3673  #include <linux/utsname.h>
 3674 -#include <linux/timex.h>
 3675 +#include <linux/time.h>
 3676  #include <linux/smp.h>
 3677  #include <linux/smp_lock.h>
 3678  #include <linux/sem.h>
 3679 Index: linux/arch/sparc64/kernel/time.c
 3680 diff -u linux/arch/sparc64/kernel/time.c:1.1.1.16 linux/arch/sparc64/kernel/time.c:1.1.1.16.2.1
 3681 --- linux/arch/sparc64/kernel/time.c:1.1.1.16	Sat Aug 26 21:34:52 2006
 3682 +++ linux/arch/sparc64/kernel/time.c	Sat Aug 26 21:49:42 2006
 3683 @@ -7,17 +7,24 @@
 3684   * Based largely on code which is:
 3685   *
 3686   * Copyright (C) 1996 Thomas K. Dyas (tdyas@eden.rutgers.edu)
 3687 + * 1999-03-10	Ulrich Windl
 3688 + *	Updated for nanokernel resolution (untested)
 3689 + * 1999-12-10	Ulrich Windl
 3690 + *	Implement new logic with ``rtc_update_slave''.
 3691 + *	Use new ``rtc_update()'' (untested).
 3692 + * 2000-12-09	Ulrich Windl
 3693 + *	Remove traditional code for microseconds as the nanoseconds seem
 3694 + *	stable.
 3695   */
 3696  
 3697  #include <linux/config.h>
 3698  #include <linux/errno.h>
 3699 -#include <linux/sched.h>
 3700 +#include <linux/time.h>
 3701  #include <linux/kernel.h>
 3702  #include <linux/param.h>
 3703  #include <linux/string.h>
 3704  #include <linux/mm.h>
 3705  #include <linux/interrupt.h>
 3706 -#include <linux/timex.h>
 3707  #include <linux/init.h>
 3708  #include <linux/ioport.h>
 3709  #include <linux/mc146818rtc.h>
 3710 @@ -50,7 +57,7 @@
 3711  static unsigned long mstk48t08_regs = 0UL;
 3712  static unsigned long mstk48t59_regs = 0UL;
 3713  
 3714 -static int set_rtc_mmss(unsigned long);
 3715 +static void update_rtc(void);
 3716  
 3717  struct sparc64_tick_ops *tick_ops;
 3718  
 3719 @@ -414,15 +421,13 @@
 3720  	static long last_rtc_update;
 3721  
 3722  	/* Determine when to update the Mostek clock. */
 3723 -	if ((time_status & STA_UNSYNC) == 0 &&
 3724 -	    xtime.tv_sec > last_rtc_update + 660 &&
 3725 -	    xtime.tv_usec >= 500000 - ((unsigned) tick) / 2 &&
 3726 -	    xtime.tv_usec <= 500000 + ((unsigned) tick) / 2) {
 3727 -		if (set_rtc_mmss(xtime.tv_sec) == 0)
 3728 -			last_rtc_update = xtime.tv_sec;
 3729 -		else
 3730 -			last_rtc_update = xtime.tv_sec - 600;
 3731 -			/* do it again in 60 s */
 3732 +	if (rtc_update_slave &&
 3733 +	    xtime.tv_sec >= last_rtc_update + rtc_update_slave &&
 3734 +	    xtime.tv_nsec >= 500000000 - ((unsigned long) tick) / 2 &&
 3735 +	    xtime.tv_nsec <= 500000000 + ((unsigned long) tick) / 2) {
 3736 +		update_rtc();
 3737 +		last_rtc_update = xtime.tv_sec;
 3738 +		rtc_update_slave = 0;
 3739  	}
 3740  }
 3741  
 3742 @@ -684,7 +689,7 @@
 3743  	}
 3744  
 3745  	xtime.tv_sec = mktime(year, mon, day, hour, min, sec);
 3746 -	xtime.tv_usec = 0;
 3747 +	xtime.tv_nsec = 0;
 3748  
 3749  	if (mregs) {
 3750  		tmp = mostek_read(mregs + MOSTEK_CREG);
 3751 @@ -979,11 +984,19 @@
 3752  	return clock;
 3753  }
 3754  
 3755 +/* nanoseconds since last timer interrupt */
 3756 +static inline unsigned long do_exact_nanotime(void)
 3757 +{
 3758 +	return do_gettimeoffset() * 1000;	/* please fix it! -- UW */
 3759 +}
 3760 +
 3761  /* The quotient formula is taken from the IA64 port. */
 3762  void __init time_init(void)
 3763  {
 3764  	unsigned long clock = sparc64_init_timers(timer_interrupt);
 3765  
 3766 +	do_nanotime = do_exact_nanotime;
 3767 +	timevar_init();		/* architecture independent initialization */
 3768  	timer_ticks_per_usec_quotient =
 3769  		(((1000000UL << 30) +
 3770  		  (clock / 2)) / clock);
 3771 @@ -999,29 +1012,6 @@
 3772  	return (ticks * timer_ticks_per_usec_quotient) >> 30UL;
 3773  }
 3774  
 3775 -void do_settimeofday(struct timeval *tv)
 3776 -{
 3777 -	if (this_is_starfire)
 3778 -		return;
 3779 -
 3780 -	write_lock_irq(&xtime_lock);
 3781 -
 3782 -	tv->tv_usec -= do_gettimeoffset();
 3783 -	tv->tv_usec -= (jiffies - wall_jiffies) * (1000000 / HZ);
 3784 -
 3785 -	while (tv->tv_usec < 0) {
 3786 -		tv->tv_usec += 1000000;
 3787 -		tv->tv_sec--;
 3788 -	}
 3789 -
 3790 -	xtime = *tv;
 3791 -	time_adjust = 0;		/* stop active adjtime() */
 3792 -	time_status |= STA_UNSYNC;
 3793 -	time_maxerror = NTP_PHASE_LIMIT;
 3794 -	time_esterror = NTP_PHASE_LIMIT;
 3795 -
 3796 -	write_unlock_irq(&xtime_lock);
 3797 -}
 3798  
 3799  void do_gettimeofday(struct timeval *tv)
 3800  {
 3801 @@ -1048,9 +1038,9 @@
 3802  	tv->tv_usec = usec;
 3803  }
 3804  
 3805 -static int set_rtc_mmss(unsigned long nowtime)
 3806 +static void update_rtc(void)
 3807  {
 3808 -	int real_seconds, real_minutes, chip_minutes;
 3809 +	int ss, mi, hh, dd, mo, yy;
 3810  	unsigned long mregs = mstk48t02_regs;
 3811  #ifdef CONFIG_PCI
 3812  	unsigned long dregs = ds1287_regs;
 3813 @@ -1065,95 +1055,35 @@
 3814  	 * Also starfire doesn't have a tod clock.
 3815  	 */
 3816  	if (!mregs && !dregs) 
 3817 -		return -1;
 3818 +		return;
 3819  
 3820 +	ktime_to_rtc(xtime.tv_sec, &ss, &dd);
 3821 +	hh = ss >> 16;
 3822 +	mi = (ss >> 8) & 0xff;
 3823 +	ss &= 0xff;
 3824 +	yy = (dd >> 16) % 100;
 3825 +	mo = (dd >> 8) & 0xff;
 3826 +	dd &= 0xff;
 3827 +	printk(KERN_DEBUG "update_rtc: %d-%02d-%02d %02d:%02d:%02d\n",
 3828 +	       yy, mo, dd, hh, mi, ss);
 3829  	if (mregs) {
 3830  		spin_lock_irqsave(&mostek_lock, flags);
 3831 -
 3832 -		/* Read the current RTC minutes. */
 3833 -		tmp = mostek_read(mregs + MOSTEK_CREG);
 3834 -		tmp |= MSTK_CREG_READ;
 3835 -		mostek_write(mregs + MOSTEK_CREG, tmp);
 3836 -
 3837 -		chip_minutes = MSTK_REG_MIN(mregs);
 3838 -
 3839 -		tmp = mostek_read(mregs + MOSTEK_CREG);
 3840 -		tmp &= ~MSTK_CREG_READ;
 3841 -		mostek_write(mregs + MOSTEK_CREG, tmp);
 3842 -
 3843 -		/*
 3844 -		 * since we're only adjusting minutes and seconds,
 3845 -		 * don't interfere with hour overflow. This avoids
 3846 -		 * messing with unknown time zones but requires your
 3847 -		 * RTC not to be off by more than 15 minutes
 3848 -		 */
 3849 -		real_seconds = nowtime % 60;
 3850 -		real_minutes = nowtime / 60;
 3851 -		if (((abs(real_minutes - chip_minutes) + 15)/30) & 1)
 3852 -			real_minutes += 30;	/* correct for half hour time zone */
 3853 -		real_minutes %= 60;
 3854 -
 3855 -		if (abs(real_minutes - chip_minutes) < 30) {
 3856 -			tmp = mostek_read(mregs + MOSTEK_CREG);
 3857 -			tmp |= MSTK_CREG_WRITE;
 3858 -			mostek_write(mregs + MOSTEK_CREG, tmp);
 3859 -
 3860 -			MSTK_SET_REG_SEC(mregs,real_seconds);
 3861 -			MSTK_SET_REG_MIN(mregs,real_minutes);
 3862 -
 3863 -			tmp = mostek_read(mregs + MOSTEK_CREG);
 3864 -			tmp &= ~MSTK_CREG_WRITE;
 3865 -			mostek_write(mregs + MOSTEK_CREG, tmp);
 3866 -
 3867 -			spin_unlock_irqrestore(&mostek_lock, flags);
 3868 -
 3869 -			return 0;
 3870 -		} else {
 3871 -			spin_unlock_irqrestore(&mostek_lock, flags);
 3872 -
 3873 -			return -1;
 3874 -		}
 3875 -	} else {
 3876 -		int retval = 0;
 3877 -		unsigned char save_control, save_freq_select;
 3878 -
 3879 -		/* Stolen from arch/i386/kernel/time.c, see there for
 3880 -		 * credits and descriptive comments.
 3881 -		 */
 3882 -		spin_lock_irqsave(&rtc_lock, flags);
 3883 -		save_control = CMOS_READ(RTC_CONTROL); /* tell the clock it's being set */
 3884 -		CMOS_WRITE((save_control|RTC_SET), RTC_CONTROL);
 3885 -
 3886 -		save_freq_select = CMOS_READ(RTC_FREQ_SELECT); /* stop and reset prescaler */
 3887 -		CMOS_WRITE((save_freq_select|RTC_DIV_RESET2), RTC_FREQ_SELECT);
 3888 -
 3889 -		chip_minutes = CMOS_READ(RTC_MINUTES);
 3890 -		if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
 3891 -			BCD_TO_BIN(chip_minutes);
 3892 -		real_seconds = nowtime % 60;
 3893 -		real_minutes = nowtime / 60;
 3894 -		if (((abs(real_minutes - chip_minutes) + 15)/30) & 1)
 3895 -			real_minutes += 30;
 3896 -		real_minutes %= 60;
 3897 -
 3898 -		if (abs(real_minutes - chip_minutes) < 30) {
 3899 -			if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
 3900 -				BIN_TO_BCD(real_seconds);
 3901 -				BIN_TO_BCD(real_minutes);
 3902 -			}
 3903 -			CMOS_WRITE(real_seconds,RTC_SECONDS);
 3904 -			CMOS_WRITE(real_minutes,RTC_MINUTES);
 3905 -		} else {
 3906 -			printk(KERN_WARNING
 3907 -			       "set_rtc_mmss: can't update from %d to %d\n",
 3908 -			       chip_minutes, real_minutes);
 3909 -			retval = -1;
 3910 -		}
 3911 -
 3912 -		CMOS_WRITE(save_control, RTC_CONTROL);
 3913 -		CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
 3914 -		spin_unlock_irqrestore(&rtc_lock, flags);
 3915 -
 3916 -		return retval;
 3917 +		regs->creg |= MSTK_CREG_WRITE;
 3918 +		MSTK_SET_REG_SEC(regs, ss);
 3919 +		MSTK_SET_REG_MIN(regs, mi);
 3920 +		MSTK_SET_REG_HOUR(regs, hh);
 3921 +		MSTK_SET_REG_DOM(regs, dd);
 3922 +		MSTK_SET_REG_MONTH(regs, mo);
 3923 +		MSTK_SET_REG_YEAR(regs, yy);
 3924 +		regs->creg &= ~MSTK_CREG_WRITE;
 3925 +		spin_unlock_irqrestore(&mostek_lock, flags);
 3926  	}
 3927  }
 3928 +
 3929 +/* Fine-tune the tick interpolation for a total frequency error
 3930 + * `freq' / 1000 PPM.
 3931 + */
 3932 +void update_nanoscale(long freq)
 3933 +{
 3934 +	/* not implemented -- this is just a prototype [UW] */
 3935 +}
 3936 Index: linux/arch/sparc64/solaris/misc.c
 3937 diff -u linux/arch/sparc64/solaris/misc.c:1.1.1.9 linux/arch/sparc64/solaris/misc.c:1.1.1.9.2.1
 3938 --- linux/arch/sparc64/solaris/misc.c:1.1.1.9	Sat Aug 26 21:34:52 2006
 3939 +++ linux/arch/sparc64/solaris/misc.c	Sat Aug 26 21:49:45 2006
 3940 @@ -14,7 +14,7 @@
 3941  #include <linux/smp.h>
 3942  #include <linux/mman.h>
 3943  #include <linux/file.h>
 3944 -#include <linux/timex.h>
 3945 +#include <linux/time.h>
 3946  
 3947  #include <asm/uaccess.h>
 3948  #include <asm/string.h>
 3949 Index: linux/drivers/acorn/char/i2c.c
 3950 diff -u linux/drivers/acorn/char/i2c.c:1.1.1.7 linux/drivers/acorn/char/i2c.c:1.1.1.7.2.1
 3951 --- linux/drivers/acorn/char/i2c.c:1.1.1.7	Sat Aug 26 21:35:23 2006
 3952 +++ linux/drivers/acorn/char/i2c.c	Sat Aug 26 21:49:47 2006
 3953 @@ -132,7 +132,7 @@
 3954  	if (rtc_command(RTC_GETDATETIME, &old_rtctm))
 3955  		return 0;
 3956  
 3957 -	new_rtctm.cs    = xtime.tv_usec / 10000;
 3958 +	new_rtctm.cs    = xtime.tv_nsec / 10000000;
 3959  	new_rtctm.secs  = nowtime % 60;	nowtime /= 60;
 3960  	new_rtctm.mins  = nowtime % 60;	nowtime /= 60;
 3961  	new_rtctm.hours = nowtime % 24;
 3962 @@ -284,7 +284,7 @@
 3963  		rtc_client = client;
 3964  		get_rtc_time(&rtctm, &year);
 3965  
 3966 -		xtime.tv_usec = rtctm.cs * 10000;
 3967 +		xtime.tv_nsec = rtctm.cs * 10000000;
 3968  		xtime.tv_sec  = mktime(year, rtctm.mon, rtctm.mday,
 3969  				       rtctm.hours, rtctm.mins, rtctm.secs);
 3970  		set_rtc = k_set_rtc_time;
 3971 Index: linux/drivers/atm/ambassador.c
 3972 diff -u linux/drivers/atm/ambassador.c:1.1.1.8 linux/drivers/atm/ambassador.c:1.1.1.8.2.1
 3973 --- linux/drivers/atm/ambassador.c:1.1.1.8	Sat Aug 26 21:34:53 2006
 3974 +++ linux/drivers/atm/ambassador.c	Sat Aug 26 21:49:50 2006
 3975 @@ -25,6 +25,7 @@
 3976  #include <linux/module.h>
 3977  #include <linux/types.h>
 3978  #include <linux/pci.h>
 3979 +#include <linux/time.h>
 3980  #include <linux/kernel.h>
 3981  #include <linux/init.h>
 3982  #include <linux/ioport.h>
 3983 @@ -515,7 +516,7 @@
 3984  	  
 3985  	  // VC layer stats
 3986  	  atomic_inc(&atm_vcc->stats->rx);
 3987 -	  skb->stamp = xtime;
 3988 +	  do_gettimeofday(&skb->stamp);
 3989  	  // end of our responsability
 3990  	  atm_vcc->push (atm_vcc, skb);
 3991  	  return;
 3992 Index: linux/drivers/atm/atmtcp.c
 3993 diff -u linux/drivers/atm/atmtcp.c:1.1.1.8 linux/drivers/atm/atmtcp.c:1.1.1.8.2.1
 3994 --- linux/drivers/atm/atmtcp.c:1.1.1.8	Sat Aug 26 21:34:53 2006
 3995 +++ linux/drivers/atm/atmtcp.c	Sat Aug 26 21:49:50 2006
 3996 @@ -8,6 +8,7 @@
 3997  #include <linux/atmdev.h>
 3998  #include <linux/atm_tcp.h>
 3999  #include <linux/bitops.h>
 4000 +#include <linux/time.h>
 4001  #include <asm/uaccess.h>
 4002  #include <asm/atomic.h>
 4003  
 4004 @@ -299,7 +300,7 @@
 4005  		result = -ENOBUFS;
 4006  		goto done;
 4007  	}
 4008 -	new_skb->stamp = xtime;
 4009 +	do_gettimeofday(&new_skb->stamp);
 4010  	memcpy(skb_put(new_skb,skb->len),skb->data,skb->len);
 4011  	out_vcc->push(out_vcc,new_skb);
 4012  	atomic_inc(&vcc->stats->tx);
 4013 Index: linux/drivers/atm/eni.c
 4014 diff -u linux/drivers/atm/eni.c:1.1.1.9 linux/drivers/atm/eni.c:1.1.1.9.2.1
 4015 --- linux/drivers/atm/eni.c:1.1.1.9	Sat Aug 26 21:34:53 2006
 4016 +++ linux/drivers/atm/eni.c	Sat Aug 26 21:49:50 2006
 4017 @@ -14,7 +14,6 @@
 4018  #include <linux/sonet.h>
 4019  #include <linux/skbuff.h>
 4020  #include <linux/time.h>
 4021 -#include <linux/sched.h> /* for xtime */
 4022  #include <linux/delay.h>
 4023  #include <linux/uio.h>
 4024  #include <linux/init.h>
 4025 @@ -703,7 +702,7 @@
 4026  			DPRINTK("Grr, servicing VCC %ld twice\n",vci);
 4027  			continue;
 4028  		}
 4029 -		ENI_VCC(vcc)->timestamp = xtime;
 4030 +		do_gettimeofday(&ENI_VCC(vcc)->timestamp);
 4031  		ENI_VCC(vcc)->next = NULL;
 4032  		if (vcc->qos.rxtp.traffic_class == ATM_CBR) {
 4033  			if (eni_dev->fast)
 4034 Index: linux/drivers/atm/fore200e.c
 4035 diff -u linux/drivers/atm/fore200e.c:1.1.1.12 linux/drivers/atm/fore200e.c:1.1.1.12.2.1
 4036 --- linux/drivers/atm/fore200e.c:1.1.1.12	Sat Aug 26 21:34:54 2006
 4037 +++ linux/drivers/atm/fore200e.c	Sat Aug 26 21:49:50 2006
 4038 @@ -1206,7 +1206,7 @@
 4039  	return -ENOMEM;
 4040      } 
 4041  
 4042 -    skb->stamp = xtime;
 4043 +    do_gettimeofday(&skb->stamp);
 4044      
 4045  #ifdef FORE200E_52BYTE_AAL0_SDU
 4046      if (cell_header) {
 4047 Index: linux/drivers/atm/horizon.c
 4048 diff -u linux/drivers/atm/horizon.c:1.1.1.8 linux/drivers/atm/horizon.c:1.1.1.8.2.1
 4049 --- linux/drivers/atm/horizon.c:1.1.1.8	Sat Aug 26 21:34:54 2006
 4050 +++ linux/drivers/atm/horizon.c	Sat Aug 26 21:49:50 2006
 4051 @@ -1049,7 +1049,7 @@
 4052  	  struct atm_vcc * vcc = ATM_SKB(skb)->vcc;
 4053  	  // VC layer stats
 4054  	  atomic_inc(&vcc->stats->rx);
 4055 -	  skb->stamp = xtime;
 4056 +	  do_gettimeofday(&skb->stamp);
 4057  	  // end of our responsability
 4058  	  vcc->push (vcc, skb);
 4059  	}
 4060 Index: linux/drivers/atm/iphase.c
 4061 diff -u linux/drivers/atm/iphase.c:1.1.1.12 linux/drivers/atm/iphase.c:1.1.1.12.2.1
 4062 --- linux/drivers/atm/iphase.c:1.1.1.12	Sat Aug 26 21:34:53 2006
 4063 +++ linux/drivers/atm/iphase.c	Sat Aug 26 21:49:50 2006
 4064 @@ -54,7 +54,6 @@
 4065  #include <linux/sonet.h>  
 4066  #include <linux/skbuff.h>  
 4067  #include <linux/time.h>  
 4068 -#include <linux/sched.h> /* for xtime */  
 4069  #include <linux/delay.h>  
 4070  #include <linux/uio.h>  
 4071  #include <linux/init.h>  
 4072 Index: linux/drivers/atm/nicstar.c
 4073 diff -u linux/drivers/atm/nicstar.c:1.1.1.12 linux/drivers/atm/nicstar.c:1.1.1.12.2.1
 4074 --- linux/drivers/atm/nicstar.c:1.1.1.12	Sat Aug 26 21:34:54 2006
 4075 +++ linux/drivers/atm/nicstar.c	Sat Aug 26 21:49:50 2006
 4076 @@ -2191,7 +2191,7 @@
 4077           memcpy(sb->tail, cell, ATM_CELL_PAYLOAD);
 4078           skb_put(sb, ATM_CELL_PAYLOAD);
 4079           ATM_SKB(sb)->vcc = vcc;
 4080 -         sb->stamp = xtime;
 4081 +         do_gettimeofday(&sb->stamp);
 4082           vcc->push(vcc, sb);
 4083           atomic_inc(&vcc->stats->rx);
 4084           cell += ATM_CELL_PAYLOAD;
 4085 @@ -2323,7 +2323,7 @@
 4086              skb->destructor = ns_sb_destructor;
 4087  #endif /* NS_USE_DESTRUCTORS */
 4088              ATM_SKB(skb)->vcc = vcc;
 4089 -            skb->stamp = xtime;
 4090 +            do_gettimeofday(&skb->stamp);
 4091              vcc->push(vcc, skb);
 4092              atomic_inc(&vcc->stats->rx);
 4093           }
 4094 @@ -2351,7 +2351,7 @@
 4095                 sb->destructor = ns_sb_destructor;
 4096  #endif /* NS_USE_DESTRUCTORS */
 4097                 ATM_SKB(sb)->vcc = vcc;
 4098 -               sb->stamp = xtime;
 4099 +               do_gettimeofday(&sb->stamp);
 4100                 vcc->push(vcc, sb);
 4101                 atomic_inc(&vcc->stats->rx);
 4102              }
 4103 @@ -2378,7 +2378,7 @@
 4104                 memcpy(skb->data, sb->data, NS_SMBUFSIZE);
 4105                 skb_put(skb, len - NS_SMBUFSIZE);
 4106                 ATM_SKB(skb)->vcc = vcc;
 4107 -               skb->stamp = xtime;
 4108 +               do_gettimeofday(&skb->stamp);
 4109                 vcc->push(vcc, skb);
 4110                 atomic_inc(&vcc->stats->rx);
 4111              }
 4112 @@ -2485,7 +2485,7 @@
 4113  #ifdef NS_USE_DESTRUCTORS
 4114              hb->destructor = ns_hb_destructor;
 4115  #endif /* NS_USE_DESTRUCTORS */
 4116 -            hb->stamp = xtime;
 4117 +            do_gettimeofday(&hb->stamp);
 4118              vcc->push(vcc, hb);
 4119              atomic_inc(&vcc->stats->rx);
 4120           }
 4121 Index: linux/drivers/atm/zatm.c
 4122 diff -u linux/drivers/atm/zatm.c:1.1.1.8 linux/drivers/atm/zatm.c:1.1.1.8.2.1
 4123 --- linux/drivers/atm/zatm.c:1.1.1.8	Sat Aug 26 21:34:54 2006
 4124 +++ linux/drivers/atm/zatm.c	Sat Aug 26 21:49:50 2006
 4125 @@ -584,7 +584,7 @@
 4126  #ifdef CONFIG_ATM_ZATM_EXACT_TS
 4127  		skb->stamp = exact_time(zatm_dev,here[1]);
 4128  #else
 4129 -		skb->stamp = xtime;
 4130 +		do_gettimeofday(&skb->stamp);
 4131  #endif
 4132  #if 0
 4133  printk("[-3..0] 0x%08lx 0x%08lx 0x%08lx 0x%08lx\n",((unsigned *) skb->data)[-3],
 4134 Index: linux/drivers/char/Config.in
 4135 diff -u linux/drivers/char/Config.in:1.1.1.15 linux/drivers/char/Config.in:1.1.1.15.6.1
 4136 --- linux/drivers/char/Config.in:1.1.1.15	Sat Jan 22 21:54:39 2005
 4137 +++ linux/drivers/char/Config.in	Sat Aug 26 21:49:53 2006
 4138 @@ -25,6 +25,13 @@
 4139        tristate '   Dual serial port support' CONFIG_DUALSP_SERIAL
 4140     fi
 4141  fi
 4142 +if [ "$CONFIG_SERIAL" != "n" ]; then
 4143 +   if [ "$CONFIG_NTP" = "y" -a "$CONFIG_NTP_PPS" = "y" ]; then
 4144 +      if [ "$CONFIG_EXPERIMENTAL" = "y" ]; then
 4145 +	 bool '   NTP PPS support on serial port' CONFIG_NTP_PPS_SERIAL
 4146 +      fi
 4147 +   fi
 4148 +fi
 4149  dep_mbool 'Extended dumb serial driver options' CONFIG_SERIAL_EXTENDED $CONFIG_SERIAL
 4150  if [ "$CONFIG_SERIAL_EXTENDED" = "y" ]; then
 4151     bool '  Support more than 4 serial ports' CONFIG_SERIAL_MANY_PORTS
 4152 Index: linux/drivers/char/n_tty.c
 4153 diff -u linux/drivers/char/n_tty.c:1.1.1.10 linux/drivers/char/n_tty.c:1.1.1.10.2.1
 4154 --- linux/drivers/char/n_tty.c:1.1.1.10	Sat Aug 26 21:35:12 2006
 4155 +++ linux/drivers/char/n_tty.c	Sat Aug 26 21:49:53 2006
 4156 @@ -1498,3 +1498,28 @@
 4157  	n_tty_write_wakeup	/* write_wakeup */
 4158  };
 4159  
 4160 +#ifdef	CONFIG_NTP_PPS
 4161 +	/* OK, I found this too late: That hack is most likely wrong, but
 4162 +	 * it should be better than the previous versions. XXX FIXME!
 4163 +	 * (Any help appreciated -- U. Windl)
 4164 +	 */
 4165 +struct tty_ldisc tty_ldisc_N_PPSCLOCK = {
 4166 +	TTY_LDISC_MAGIC,	/* magic */
 4167 +	"n_PPSclock",		/* name */
 4168 +	0,			/* num */
 4169 +	0,			/* flags */
 4170 +	n_tty_open,		/* open */
 4171 +	n_tty_close,		/* close */
 4172 +	n_tty_flush_buffer,	/* flush_buffer */
 4173 +	n_tty_chars_in_buffer,	/* chars_in_buffer */
 4174 +	read_chan,		/* read */
 4175 +	write_chan,		/* write */
 4176 +	n_tty_ioctl,		/* ioctl */
 4177 +	n_tty_set_termios,	/* set_termios */
 4178 +	normal_poll,		/* poll */
 4179 +	NULL,			/* hangup */
 4180 +	n_tty_receive_buf,	/* receive_buf */
 4181 +	n_tty_receive_room,	/* receive_room */
 4182 +	n_tty_write_wakeup	/* write_wakeup */
 4183 +};
 4184 +#endif
 4185 Index: linux/drivers/char/serial.c
 4186 diff -u linux/drivers/char/serial.c:1.1.1.18 linux/drivers/char/serial.c:1.1.1.18.4.1
 4187 --- linux/drivers/char/serial.c:1.1.1.18	Thu Jan 19 20:19:23 2006
 4188 +++ linux/drivers/char/serial.c	Sat Aug 26 21:49:53 2006
 4189 @@ -39,6 +39,9 @@
 4190   *
 4191   *  8/99: Generalized PCI support added.  Theodore Ts'o
 4192   * 
 4193 + *  12/99: Additions for NTP PPS pulse on CD by Ulrich Windl (with
 4194 + *	  contributions by H. P. Anvin).  Revised PPS API (1999-08-17)
 4195 + *
 4196   *  3/00: Rid circular buffer of redundant xmit_cnt.  Fix a
 4197   *	  few races on freeing buffers too.
 4198   *	  Alan Modra <alan@linuxcare.com>
 4199 @@ -57,6 +60,11 @@
 4200   * 10/00: add in optional software flow control for serial console.
 4201   *	  Kanoj Sarcar <kanoj@sgi.com>  (Modified by Theodore Ts'o)
 4202   *
 4203 + * 12/00: Updated PPS API for Linux 2.4.0
 4204 + *	  Ulrich Windl
 4205 + * 04/01: Updated to support different devices for PPS API's ``kernel
 4206 + *	  consumer''
 4207 + *	  Ulrich Windl
 4208   * 02/02: Fix for AMD Elan bug in transmit irq routine, by
 4209   *        Christer Weinigel <wingel@hog.ctrl-c.liu.se>,
 4210   *        Robert Schwebel <robert@schwebel.de>,
 4211 @@ -64,7 +72,7 @@
 4212   *        Theodore Ts'o <tytso@mit.edu>
 4213   */
 4214  
 4215 -static char *serial_version = "5.05c";
 4216 +static char *serial_version = "5.05c+";
 4217  static char *serial_revdate = "2001-07-08";
 4218  
 4219  /*
 4220 @@ -141,6 +149,7 @@
 4221  #undef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
 4222  #undef SERIAL_DEBUG_PCI
 4223  #undef SERIAL_DEBUG_AUTOCONF
 4224 +#undef SERIAL_DEBUG_NTP_PPS
 4225  
 4226  /* Sanity checks */
 4227  
 4228 @@ -236,6 +245,13 @@
 4229  #include <asm/irq.h>
 4230  #include <asm/bitops.h>
 4231  
 4232 +#ifdef	CONFIG_NTP_PPS_SERIAL
 4233 +#include <linux/time.h> 	/* struct timeval, do_gettimeofday(),
 4234 +				   hardpps() */
 4235 +#include <linux/timepps.h>	/* PPS API */
 4236 +#include <linux/slab.h>		/* kmalloc(), kfree() */
 4237 +#endif
 4238 +
 4239  #if defined(CONFIG_MAC_SERIAL)
 4240  #define SERIAL_DEV_OFFSET	((_machine == _MACH_prep || _machine == _MACH_chrp) ? 0 : 2)
 4241  #else
 4242 @@ -735,6 +751,98 @@
 4243  	}
 4244  }
 4245  
 4246 +#ifdef	CONFIG_NTP_PPS_SERIAL
 4247 +#define NANOSECOND	1000000000
 4248 +
 4249 +/* update PPS info from the event time stamp stored in etime and ecount. */
 4250 +static inline void pps_update_event(struct async_struct *info,
 4251 +				    int modem_status)
 4252 +{
 4253 +	struct tty_struct const	*const tty	= info->tty;
 4254 +	struct pps	*pp;
 4255 +	int mode;
 4256 +
 4257 +	if ((pp = (struct pps *) tty->disc_data) == NULL
 4258 +	    || pp->magic != PPSCLOCK_MAGIC) {
 4259 +#ifdef SERIAL_DEBUG_NTP_PPS
 4260 +		printk(KERN_ERR
 4261 +		       "pps_update_event(): illegal disc_data for info %p\n",
 4262 +		       info);
 4263 +#endif /* SERIAL_DEBUG_NTP_PPS */
 4264 +		return;
 4265 +	}
 4266 +	mode = pp->state.parm.mode;
 4267 +	if ((mode & (PPS_ECHOASSERT|PPS_ECHOCLEAR)) != 0) {
 4268 +		/* echo event via cleared RTS bit */
 4269 +		info->MCR |= UART_MCR_RTS;		/* signal no event */
 4270 +		if (((modem_status & UART_MSR_DCD) != 0) ==
 4271 +		    ((mode & PPS_ECHOASSERT) != 0))
 4272 +			info->MCR &= ~UART_MCR_RTS;	/* signal event */
 4273 +		serial_out(info, UART_MCR, info->MCR);
 4274 +	}
 4275 +	if ((modem_status & UART_MSR_DCD) != 0)	{
 4276 +		struct timespec ts;
 4277 +		ts = pp->state.etime;
 4278 +		pp->state.info.current_mode = mode;
 4279 +		if ((mode & PPS_OFFSETASSERT) != 0) {
 4280 +			ts.tv_nsec += pp->state.parm.assert_offset.tv_nsec;
 4281 +			if (ts.tv_nsec >= NANOSECOND) {
 4282 +				ts.tv_nsec -= NANOSECOND;
 4283 +				++ts.tv_sec;
 4284 +			} else if (ts.tv_nsec < 0) {
 4285 +				ts.tv_nsec += NANOSECOND;
 4286 +				--ts.tv_sec;
 4287 +			}
 4288 +		}
 4289 +		if ((pps_kc_hardpps_mode & PPS_CAPTUREASSERT) != 0 &&
 4290 +		    pps_kc_hardpps_dev == (void *) tty)
 4291 +			hardpps(&ts, pp->state.ecount);
 4292 +		if ((mode & PPS_CAPTUREASSERT) != 0) {
 4293 +			pp->state.info.assert_timestamp = ts;
 4294 +			++pp->state.info.assert_sequence;
 4295 +			if (waitqueue_active(&pp->state.ewait))
 4296 +				wake_up_interruptible(&pp->state.ewait);
 4297 +		}
 4298 +#ifdef SERIAL_DEBUG_NTP_PPS
 4299 +		printk(KERN_DEBUG
 4300 +		       "ASSERT event #%lu for %p at %lu.%09ld (%9ld)\n",
 4301 +		       pp->state.info.assert_sequence, pp, ts.tv_sec,
 4302 +		       ts.tv_nsec, pp->state.ecount);
 4303 +#endif /* SERIAL_DEBUG_NTP_PPS */
 4304 +	} else {
 4305 +		struct timespec ts;
 4306 +		ts = pp->state.etime;
 4307 +		pp->state.info.current_mode = mode;
 4308 +		if ((mode & PPS_OFFSETCLEAR) != 0) {
 4309 +			ts.tv_nsec += pp->state.parm.clear_offset.tv_nsec;
 4310 +			if (ts.tv_nsec >= NANOSECOND) {
 4311 +				ts.tv_nsec -= NANOSECOND;
 4312 +				++ts.tv_sec;
 4313 +			} else if (ts.tv_nsec < 0) {
 4314 +				ts.tv_nsec += NANOSECOND;
 4315 +				--ts.tv_sec;
 4316 +			}
 4317 +		}
 4318 +		if ((pps_kc_hardpps_mode & PPS_CAPTURECLEAR) != 0 &&
 4319 +		    pps_kc_hardpps_dev == (void *) tty)
 4320 +			hardpps(&ts, pp->state.ecount);
 4321 +		if ((mode & PPS_CAPTURECLEAR) != 0) {
 4322 +			pp->state.info.clear_timestamp = ts;
 4323 +			++pp->state.info.clear_sequence;
 4324 +			if (waitqueue_active(&pp->state.ewait))
 4325 +				wake_up_interruptible(&pp->state.ewait);
 4326 +		}
 4327 +#ifdef SERIAL_DEBUG_NTP_PPS
 4328 +		printk(KERN_DEBUG
 4329 +		       "CLEAR event #%lu for %p at %lu.%09ld (%9ld)\n",
 4330 +		       pp->state.info.clear_sequence, pp, ts.tv_sec,
 4331 +		       ts.tv_nsec, pp->state.ecount);
 4332 +#endif /* SERIAL_DEBUG_NTP_PPS */
 4333 +	}
 4334 +#undef NANOSECOND
 4335 +}
 4336 +#endif
 4337 +
 4338  static _INLINE_ void check_modem_status(struct async_struct *info)
 4339  {
 4340  	int	status;
 4341 @@ -750,12 +858,14 @@
 4342  		if (status & UART_MSR_DDSR)
 4343  			icount->dsr++;
 4344  		if (status & UART_MSR_DDCD) {
 4345 +#ifdef CONFIG_NTP_PPS_SERIAL
 4346 +			struct pps *pp = (struct pps *) info->tty->disc_data;
 4347 +				
 4348 +			if (pp != NULL && pp->magic == PPSCLOCK_MAGIC) {
 4349 +				pps_update_event(info, status);
 4350 +			}
 4351 +#endif /* CONFIG_NTP_PPS_SERIAL */
 4352  			icount->dcd++;
 4353 -#ifdef CONFIG_HARD_PPS
 4354 -			if ((info->flags & ASYNC_HARDPPS_CD) &&
 4355 -			    (status & UART_MSR_DCD))
 4356 -				hardpps();
 4357 -#endif
 4358  		}
 4359  		if (status & UART_MSR_DCTS)
 4360  			icount->cts++;
 4361 @@ -842,6 +952,24 @@
 4362  #ifdef SERIAL_DEBUG_INTR
 4363  		printk("IIR = %x...", serial_in(info, UART_IIR));
 4364  #endif
 4365 +#ifdef CONFIG_NTP_PPS_SERIAL
 4366 +		{	/* store timestamp in case it is wanted later */
 4367 +			struct pps *pp = (struct pps *) info->tty->disc_data;
 4368 +				
 4369 +			if (pp != NULL && pp->magic == PPSCLOCK_MAGIC) {
 4370 +				pp->state.ecount =
 4371 +				do_clock_gettime(CLOCK_REALTIME,
 4372 +						 &pp->state.etime);
 4373 +#ifdef SERIAL_DEBUG_NTP_PPS
 4374 +				printk(KERN_DEBUG
 4375 +				       "rs_interrupt: time %ld:%ld(%ld)\n",
 4376 +				       pp->state.etime.tv_sec,
 4377 +				       pp->state.etime.tv_nsec,
 4378 +				       pp->state.ecount);
 4379 +#endif
 4380 +			}
 4381 +		}
 4382 +#endif /* CONFIG_NTP_PPS_SERIAL */
 4383  		end_mark = 0;
 4384  
 4385  		info->last_active = jiffies;
 4386 @@ -910,6 +1038,23 @@
 4387  	if (!info || !info->tty)
 4388  		return;
 4389  
 4390 +#ifdef CONFIG_NTP_PPS_SERIAL
 4391 +	{	/* store timestamp in case it is wanted later */
 4392 +		struct pps *pp = (struct pps *) info->tty->disc_data;
 4393 +
 4394 +		if (pp != NULL && pp->magic == PPSCLOCK_MAGIC) {
 4395 +			pp->state.ecount = do_clock_gettime(CLOCK_REALTIME,
 4396 +							    &pp->state.etime);
 4397 +#ifdef SERIAL_DEBUG_NTP_PPS
 4398 +				printk(KERN_DEBUG
 4399 +				       "rs_interrupt_single: time %ld:%ld(%ld)\n",
 4400 +				       pp->state.etime.tv_sec,
 4401 +				       pp->state.etime.tv_nsec,
 4402 +				       pp->state.ecount);
 4403 +#endif
 4404 +		}
 4405 +	}
 4406 +#endif /* CONFIG_NTP_PPS_SERIAL */
 4407  #ifdef CONFIG_SERIAL_MULTIPORT	
 4408  	multi = &rs_multiport[irq];
 4409  	if (multi->port_monitor)
 4410 @@ -989,6 +1134,24 @@
 4411  		if (!info->tty ||
 4412  		    (serial_in(info, UART_IIR) & UART_IIR_NO_INT))
 4413  			goto next;
 4414 +#ifdef CONFIG_NTP_PPS_SERIAL
 4415 +		{	/* store timestamp in case it is wanted later */
 4416 +			struct pps *pp = (struct pps *) info->tty->disc_data;
 4417 +				
 4418 +			if (pp != NULL && pp->magic == PPSCLOCK_MAGIC) {
 4419 +				pp->state.ecount =
 4420 +					do_clock_gettime(CLOCK_REALTIME,
 4421 +							 &pp->state.etime);
 4422 +#ifdef SERIAL_DEBUG_NTP_PPS
 4423 +				printk(KERN_DEBUG
 4424 +				       "rs_interrupt_multi: time %ld:%ld(%ld)\n",
 4425 +				       pp->state.etime.tv_sec,
 4426 +				       pp->state.etime.tv_nsec,
 4427 +				       pp->state.ecount);
 4428 +#endif
 4429 +			}
 4430 +		}
 4431 +#endif /* CONFIG_NTP_PPS_SERIAL */
 4432  
 4433  		info->last_active = jiffies;
 4434  
 4435 @@ -1642,6 +1805,9 @@
 4436  	unsigned cflag, cval, fcr = 0;
 4437  	int	bits;
 4438  	unsigned long	flags;
 4439 +#ifdef CONFIG_NTP_PPS_SERIAL
 4440 +	struct pps	*pps;
 4441 +#endif
 4442  
 4443  	if (!info->tty || !info->tty->termios)
 4444  		return;
 4445 @@ -1756,8 +1922,6 @@
 4446  	
 4447  	/* CTS flow control flag and modem status interrupts */
 4448  	info->IER &= ~UART_IER_MSI;
 4449 -	if (info->flags & ASYNC_HARDPPS_CD)
 4450 -		info->IER |= UART_IER_MSI;
 4451  	if (cflag & CRTSCTS) {
 4452  		info->flags |= ASYNC_CTS_FLOW;
 4453  		info->IER |= UART_IER_MSI;
 4454 @@ -1769,6 +1933,12 @@
 4455  		info->flags |= ASYNC_CHECK_CD;
 4456  		info->IER |= UART_IER_MSI;
 4457  	}
 4458 +#ifdef CONFIG_NTP_PPS_SERIAL
 4459 +	if ((pps = info->tty->disc_data) != 0 &&
 4460 +	    pps->magic == PPSCLOCK_MAGIC &&
 4461 +	    (pps->state.parm.mode & ~PPS_TSFMT_TSPEC) != 0)
 4462 +		info->IER |= UART_IER_MSI;	/* enable interrupts */
 4463 +#endif
 4464  	serial_out(info, UART_IER, info->IER);
 4465  
 4466  	/*
 4467 @@ -2558,6 +2728,34 @@
 4468  }
 4469  #endif
 4470  
 4471 +#ifdef	CONFIG_NTP_PPS_SERIAL
 4472 +/* Return allowed mode bits for given pps struct, file's mode, and user.
 4473 + * Bits set in `*obligatory' must be set.  Returned bits may be set.
 4474 + */
 4475 +static int pps_allowed_mode(const struct tty_struct *tty, mode_t fmode,
 4476 +			    int *obligatory)
 4477 +{
 4478 +	const struct pps	*pps = (const struct pps *) tty->disc_data;
 4479 +	int 			cap = pps->state.cap;
 4480 +
 4481 +	cap &= ~PPS_CANWAIT;				/* always RO */
 4482 +	*obligatory = PPS_TSFMT_TSPEC;		/* only supported format */
 4483 +	if ((fmode & FMODE_WRITE) == 0) {	/* read-only mode */
 4484 +		cap = *obligatory = pps->state.parm.mode;
 4485 +	} else if (!capable(CAP_SYS_TIME)) {	/* may not manipulate time */
 4486 +		int	fixed_bits;
 4487 +		int	active_flags = pps->state.parm.mode;
 4488 +
 4489 +		if (pps_kc_hardpps_dev == tty) {
 4490 +			fixed_bits = PPS_OFFSETASSERT|PPS_OFFSETCLEAR;
 4491 +			fixed_bits &= active_flags;
 4492 +			*obligatory |= fixed_bits;
 4493 +		}
 4494 +	}
 4495 +	return cap;
 4496 +}
 4497 +#endif
 4498 +
 4499  static int rs_ioctl(struct tty_struct *tty, struct file * file,
 4500  		    unsigned int cmd, unsigned long arg)
 4501  {
 4502 @@ -2565,6 +2763,9 @@
 4503  	struct async_icount cprev, cnow;	/* kernel counter temps */
 4504  	struct serial_icounter_struct icount;
 4505  	unsigned long flags;
 4506 +#ifdef	CONFIG_NTP_PPS_SERIAL
 4507 +	int error = 0;
 4508 +#endif
 4509  #if (LINUX_VERSION_CODE < 131394) /* Linux 2.1.66 */
 4510  	int retval, tmp;
 4511  #endif
 4512 @@ -2717,6 +2918,379 @@
 4513  			/* "setserial -W" is called in Debian boot */
 4514  			printk ("TIOCSER?WILD ioctl obsolete, ignored.\n");
 4515  			return 0;
 4516 +#ifdef	CONFIG_NTP_PPS_SERIAL
 4517 +#ifdef SERIAL_DEBUG_NTP_PPS
 4518 +#define RESTORE_FLAGS_AND_RETURN	\
 4519 +	do { \
 4520 +		printk(KERN_DEBUG "ioctl() returns %d\n", error);\
 4521 +		restore_flags(flags); return error;\
 4522 +	} while (0);
 4523 +#else
 4524 +#define RESTORE_FLAGS_AND_RETURN	\
 4525 +	do { restore_flags(flags); return error; } while (0);
 4526 +#endif
 4527 +
 4528 +		case PPS_IOC_CREATE:
 4529 +		{
 4530 +			/* initialize the tty data struct */
 4531 +			struct pps *pps;
 4532 +
 4533 +			save_flags(flags); cli();
 4534 +			pps = (struct pps *) tty->disc_data;
 4535 +#ifdef SERIAL_DEBUG_NTP_PPS
 4536 +			printk(KERN_DEBUG
 4537 +			       "PPS_IOC_CREATE: tty/count/data = %p/%d/%p\n",
 4538 +			       tty, tty->count, pps);
 4539 +#endif /* SERIAL_DEBUG_NTP_PPS */
 4540 +			if (pps != NULL) {
 4541 +#ifdef SERIAL_DEBUG_NTP_PPS
 4542 +				printk(KERN_DEBUG
 4543 +				       "PPS_IOC_CREATE: magic = 0x%x\n",
 4544 +				       pps->magic);
 4545 +#endif /* SERIAL_DEBUG_NTP_PPS */
 4546 +				/* share the handle if valid, otherwise fail */
 4547 +				if (pps->magic != PPSCLOCK_MAGIC)
 4548 +					error = -EBADF;
 4549 +				RESTORE_FLAGS_AND_RETURN;
 4550 +			}
 4551 +			if ((file->f_mode & FMODE_WRITE) == 0) {
 4552 +				error = -EBADF;
 4553 +				RESTORE_FLAGS_AND_RETURN;
 4554 +			}
 4555 +			if ((pps = (struct pps *) kmalloc(sizeof(struct pps),
 4556 +							  GFP_KERNEL)) == NULL)
 4557 +			{
 4558 +				printk(KERN_ERR
 4559 +				       "PPS_IOC_CREATE: kmalloc failed\n");
 4560 +				error = -ENOMEM;
 4561 +				RESTORE_FLAGS_AND_RETURN;
 4562 +			}
 4563 +			/* clear all parameters */
 4564 +			memset(pps, 0, sizeof(struct pps));
 4565 +			pps->magic = PPSCLOCK_MAGIC;
 4566 +			pps->state.parm.api_version = PPS_API_VERS_1;
 4567 +			pps->state.parm.mode = PPS_TSFMT_TSPEC;
 4568 +			pps->state.cap = (PPS_CAPTUREASSERT|
 4569 +					  PPS_CAPTURECLEAR|
 4570 +					  PPS_OFFSETASSERT|
 4571 +					  PPS_OFFSETCLEAR|
 4572 +					  PPS_ECHOASSERT|
 4573 +					  PPS_ECHOCLEAR|
 4574 +					  PPS_CANWAIT|
 4575 +					  PPS_TSFMT_TSPEC);
 4576 +			init_waitqueue_head(&pps->state.ewait);
 4577 +			tty->disc_data = (char *) pps;
 4578 +			tty->termios->c_line = N_PPSCLOCK;
 4579 +#ifdef SERIAL_DEBUG_NTP_PPS
 4580 +			printk(KERN_DEBUG
 4581 +			       "PPS_IOC_CREATE: new pps at %p, magic 0x%0x\n",
 4582 +			       pps, pps->magic);
 4583 +#endif /* SERIAL_DEBUG_NTP_PPS */
 4584 +			error = 0;
 4585 +			RESTORE_FLAGS_AND_RETURN;
 4586 +		}
 4587 +
 4588 +		case PPS_IOC_DESTROY:
 4589 +		{
 4590 +			/* draft 03 says the settings are unaffected. */
 4591 +			struct pps *pps;
 4592 +
 4593 +			save_flags(flags); cli();
 4594 +			if ((file->f_mode & FMODE_WRITE) == 0) {
 4595 +				error = -EBADF;
 4596 +				RESTORE_FLAGS_AND_RETURN;
 4597 +			}
 4598 +			pps = (struct pps *) tty->disc_data;
 4599 +#ifdef SERIAL_DEBUG_NTP_PPS
 4600 +			printk(KERN_DEBUG
 4601 +			       "PPS_IOC_DESTROY: tty/count/data = %p/%d/%p\n",
 4602 +			       tty, tty->count, pps);
 4603 +#endif /* SERIAL_DEBUG_NTP_PPS */
 4604 +			if (pps == NULL || pps->magic != PPSCLOCK_MAGIC) {
 4605 +				error = -EOPNOTSUPP;
 4606 +				RESTORE_FLAGS_AND_RETURN;
 4607 +			}
 4608 +			if (waitqueue_active(&pps->state.ewait)) {
 4609 +#ifdef SERIAL_DEBUG_NTP_PPS
 4610 +				printk(KERN_DEBUG
 4611 +				       "PPS_IOC_DESTROY: wait queue busy\n");
 4612 +#endif /* SERIAL_DEBUG_NTP_PPS */
 4613 +				error = -EBUSY;
 4614 +				RESTORE_FLAGS_AND_RETURN;
 4615 +			}
 4616 +			error = 0;
 4617 +			RESTORE_FLAGS_AND_RETURN;
 4618 +		}
 4619 +
 4620 +		case PPS_IOC_FETCH:
 4621 +		{
 4622 +			struct pps *pps = (struct pps *) tty->disc_data;
 4623 +			struct pps_fetch_args parms;
 4624 +			long timeout;
 4625 +
 4626 +			save_flags(flags); cli();
 4627 +#ifdef SERIAL_DEBUG_NTP_PPS
 4628 +			printk(KERN_DEBUG
 4629 +			       "PPS_IOC_FETCH: tty/count/pps = %p/%d/%p\n",
 4630 +			       tty, tty->count, pps);
 4631 +#endif /* SERIAL_DEBUG_NTP_PPS */
 4632 +			if (pps == NULL || pps->magic != PPSCLOCK_MAGIC) {
 4633 +				error = -EOPNOTSUPP;
 4634 +				RESTORE_FLAGS_AND_RETURN;
 4635 +			}
 4636 +			if (copy_from_user(&parms,
 4637 +					   (struct pps_fetch_args *) arg,
 4638 +					   sizeof(struct pps_fetch_args))
 4639 +			    != 0) {
 4640 +				error = -EFAULT;
 4641 +				RESTORE_FLAGS_AND_RETURN;
 4642 +			}
 4643 +			if (parms.tsformat != PPS_TSFMT_TSPEC) {
 4644 +				error = -EINVAL;
 4645 +				RESTORE_FLAGS_AND_RETURN;
 4646 +			}
 4647 +			timeout = HZ * parms.timeout.tv_sec;
 4648 +			timeout += parms.timeout.tv_nsec / (1000000000 / HZ);
 4649 +			if (timeout != 0) {
 4650 +				restore_flags(flags);
 4651 +				if (parms.timeout.tv_sec == -1)
 4652 +					interruptible_sleep_on(&pps->state.ewait);
 4653 +				else {
 4654 +					timeout = interruptible_sleep_on_timeout(
 4655 +						&pps->state.ewait,
 4656 +						timeout);
 4657 +					if (timeout <= 0) {
 4658 +						error = -ETIMEDOUT;
 4659 +						return error;
 4660 +						/* flags already restored */
 4661 +					}
 4662 +				}
 4663 +				save_flags(flags); cli();
 4664 +				pps = (struct pps *) tty->disc_data;
 4665 +				if (pps == NULL || pps->magic != PPSCLOCK_MAGIC)
 4666 +				{
 4667 +#ifdef SERIAL_DEBUG_NTP_PPS
 4668 +					printk(KERN_DEBUG "PPS_IOC_FETCH: "
 4669 +					       "tty %p lacks pps\n",
 4670 +					       tty);
 4671 +#endif /* SERIAL_DEBUG_NTP_PPS */
 4672 +					error = -EOPNOTSUPP;
 4673 +					RESTORE_FLAGS_AND_RETURN;
 4674 +				}
 4675 +				if (signal_pending(current)) {
 4676 +					error = -EINTR;
 4677 +					RESTORE_FLAGS_AND_RETURN;
 4678 +				}
 4679 +			}
 4680 +			parms.pps_info_buf = pps->state.info;
 4681 +			if (copy_to_user((struct pps_fetch_args *) arg,
 4682 +					 &parms,
 4683 +					 sizeof(struct pps_fetch_args)) != 0)
 4684 +				error = -EFAULT;
 4685 +			RESTORE_FLAGS_AND_RETURN;
 4686 +		}
 4687 +
 4688 +		case PPS_IOC_SETPARMS:
 4689 +		{
 4690 +			struct pps *pps = (struct pps *) tty->disc_data;
 4691 +			struct pps_params parms;
 4692 +			int may_bits, must_bits;
 4693 +
 4694 +			save_flags(flags); cli();
 4695 +			if ((file->f_mode & FMODE_WRITE) == 0) {
 4696 +				error = -EBADF;
 4697 +				RESTORE_FLAGS_AND_RETURN;
 4698 +			}
 4699 +#ifdef SERIAL_DEBUG_NTP_PPS
 4700 +			printk(KERN_DEBUG
 4701 +			       "PPS_IOC_SETPARAMS: tty/pps = %p/%p\n",
 4702 +			       tty, pps);
 4703 +#endif /* SERIAL_DEBUG_NTP_PPS */
 4704 +			if (pps == NULL || pps->magic != PPSCLOCK_MAGIC) {
 4705 +				error = -EOPNOTSUPP;
 4706 +				RESTORE_FLAGS_AND_RETURN;
 4707 +			}
 4708 +			if (copy_from_user(&parms,
 4709 +					   (struct pps_params *) arg,
 4710 +					   sizeof(struct pps_params)) != 0) {
 4711 +				error = -EFAULT;
 4712 +				RESTORE_FLAGS_AND_RETURN;
 4713 +			}
 4714 +#ifdef SERIAL_DEBUG_NTP_PPS
 4715 +			printk(KERN_DEBUG "PPS_IOC_SETPARAMS: "
 4716 +			       "vers/mode(cap) = %#x/%#x(%#x)\n",
 4717 +			       parms.api_version, parms.mode, pps->state.cap);
 4718 +#endif /* SERIAL_DEBUG_NTP_PPS */
 4719 +			if (parms.api_version != PPS_API_VERS_1) {
 4720 +				error = -EINVAL;
 4721 +				RESTORE_FLAGS_AND_RETURN;
 4722 +			}
 4723 +			if ((parms.mode & ~pps->state.cap) != 0 ) {
 4724 +				error = -EINVAL;
 4725 +				RESTORE_FLAGS_AND_RETURN;
 4726 +			}
 4727 +			if ((parms.mode &
 4728 +			     (PPS_TSFMT_TSPEC|PPS_TSFMT_NTPFP)) == 0 ) {
 4729 +				/* section 3.3 of RFC 2783 interpreted */
 4730 +				parms.mode |= PPS_TSFMT_TSPEC;
 4731 +			}
 4732 +			may_bits = pps_allowed_mode(tty, file->f_mode,
 4733 +						    &must_bits);
 4734 +			if ((parms.mode & must_bits) != must_bits ||
 4735 +			    (parms.mode & ~may_bits) != 0) {
 4736 +				error = -EPERM;
 4737 +				RESTORE_FLAGS_AND_RETURN;
 4738 +			}
 4739 +			if (capable(CAP_SYS_TIME)) {
 4740 +				/* allow setting offsets */
 4741 +				pps->state.parm = parms;
 4742 +			} else {
 4743 +				pps_params_t	*ppspp = &pps->state.parm;
 4744 +
 4745 +				ppspp->api_version = parms.api_version;
 4746 +				ppspp->mode = parms.mode;
 4747 +				/* not offsets! */
 4748 +			}
 4749 +			if (parms.mode & (PPS_CAPTUREASSERT|PPS_CAPTURECLEAR)) {
 4750 +				/* enable interrupts */
 4751 +				info->IER |= UART_IER_MSI;
 4752 +				info->flags |= ASYNC_LOW_LATENCY;
 4753 +				if (info->flags & ASYNC_INITIALIZED) {
 4754 +					serial_out(info, UART_IER, info->IER);
 4755 +#ifdef SERIAL_DEBUG_NTP_PPS
 4756 +					printk(KERN_DEBUG
 4757 +					       "PPS_IOC_SETPARAMS: IER:%02x\n",
 4758 +					       info->IER);
 4759 +#endif /* SERIAL_DEBUG_NTP_PPS */
 4760 +				}
 4761 +			}
 4762 +			RESTORE_FLAGS_AND_RETURN;
 4763 +		}
 4764 +
 4765 +		case PPS_IOC_GETPARMS:
 4766 +		{
 4767 +			const struct pps *pps = (struct pps *) tty->disc_data;
 4768 +
 4769 +			save_flags(flags); cli();
 4770 +#ifdef SERIAL_DEBUG_NTP_PPS
 4771 +			printk(KERN_DEBUG
 4772 +			       "PPS_IOC_GETPARAMS: tty/pps = %p/%p\n",
 4773 +			       tty, pps);
 4774 +#endif /* SERIAL_DEBUG_NTP_PPS */
 4775 +			if (pps == NULL || pps->magic != PPSCLOCK_MAGIC) {
 4776 +				error = -EOPNOTSUPP;
 4777 +				RESTORE_FLAGS_AND_RETURN;
 4778 +			}
 4779 +			if (copy_to_user((pps_params_t *) arg,
 4780 +					 &(pps->state.parm),
 4781 +					 sizeof(struct pps_params)) != 0) {
 4782 +				error = -EFAULT;
 4783 +				RESTORE_FLAGS_AND_RETURN;
 4784 +			}
 4785 +		}
 4786 +
 4787 +		case PPS_IOC_GETCAP:
 4788 +		{
 4789 +			const struct pps *pps = (struct pps *) tty->disc_data;
 4790 +			int may_bits, must_bits;
 4791 +
 4792 +			save_flags(flags); cli();
 4793 +#ifdef SERIAL_DEBUG_NTP_PPS
 4794 +			printk(KERN_DEBUG
 4795 +			       "PPS_IOC_GETCAP: tty/pps = %p/%p\n", tty, pps);
 4796 +#endif /* SERIAL_DEBUG_NTP_PPS */
 4797 +			if (pps == NULL || pps->magic != PPSCLOCK_MAGIC) {
 4798 +				error = -EOPNOTSUPP;
 4799 +				RESTORE_FLAGS_AND_RETURN;
 4800 +			}
 4801 +			may_bits = pps_allowed_mode(tty, file->f_mode,
 4802 +						    &must_bits);
 4803 +			if (copy_to_user((int *) arg, &may_bits,
 4804 +					 sizeof(int)) != 0)
 4805 +				error = -EFAULT;
 4806 +			RESTORE_FLAGS_AND_RETURN;
 4807 +		}
 4808 +
 4809 +		case PPS_IOC_KC_BIND:
 4810 +		{
 4811 +			struct pps *pps = (struct pps *) tty->disc_data;
 4812 +			struct pps_bind_args parms;
 4813 +
 4814 +			save_flags(flags); cli();
 4815 +			if ((file->f_mode & FMODE_WRITE) == 0) {
 4816 +				error = -EBADF;
 4817 +				RESTORE_FLAGS_AND_RETURN;
 4818 +			}
 4819 +#ifdef SERIAL_DEBUG_NTP_PPS
 4820 +			printk(KERN_DEBUG
 4821 +			       "PPS_IOC_KC_BIND: tty/pps = %p/%p\n", tty, pps);
 4822 +			printk(KERN_DEBUG
 4823 +			       "PPS_IOC_KC_BIND: current dev/mode = %p/0x%x\n",
 4824 +			       pps_kc_hardpps_dev, pps_kc_hardpps_mode);
 4825 +			
 4826 +#endif /* SERIAL_DEBUG_NTP_PPS */
 4827 +			if (pps == NULL || pps->magic != PPSCLOCK_MAGIC)
 4828 +			{
 4829 +				error = -EOPNOTSUPP;
 4830 +				RESTORE_FLAGS_AND_RETURN;
 4831 +			}
 4832 +			if (copy_from_user(&parms,
 4833 +					   (struct pps_bind_args *) arg,
 4834 +					   sizeof(struct pps_bind_args)) != 0)
 4835 +			{
 4836 +				error = -EFAULT;
 4837 +				RESTORE_FLAGS_AND_RETURN;
 4838 +			}
 4839 +			/* generic parameter validation */
 4840 +			if (parms.tsformat != PPS_TSFMT_TSPEC ||
 4841 +			    (parms.edge & ~PPS_CAPTUREBOTH) != 0 ||
 4842 +#if 0
 4843 +			    parms.consumer < PPS_KC_HARDPPS ||
 4844 +			    parms.consumer > PPS_KC_HARDPPS_FLL ||
 4845 +#endif
 4846 +			    parms.consumer != PPS_KC_HARDPPS) {
 4847 +				error = -EINVAL;
 4848 +				RESTORE_FLAGS_AND_RETURN;
 4849 +			}
 4850 +			/* permission check */
 4851 +			if (!capable(CAP_SYS_TIME)) {
 4852 +				error = -EPERM;
 4853 +				RESTORE_FLAGS_AND_RETURN;
 4854 +			}
 4855 +			/* detailed parameter check */
 4856 +			if (parms.edge == 0) {
 4857 +				if (pps_kc_hardpps_dev == NULL ||
 4858 +				    pps_kc_hardpps_dev == (void *) tty) {
 4859 +					pps_kc_hardpps_mode = parms.edge;
 4860 +					pps_kc_hardpps_dev = NULL;
 4861 +#ifdef SERIAL_DEBUG_NTP_PPS
 4862 +					printk(KERN_DEBUG "PPS_IOC_KC_BIND: "
 4863 +					       "unbound kernel consumer\n");
 4864 +#endif /* SERIAL_DEBUG_NTP_PPS */
 4865 +				} else {	/* another consumer bound */
 4866 +					error = -EINVAL;
 4867 +					RESTORE_FLAGS_AND_RETURN;
 4868 +				}
 4869 +			} else {
 4870 +				if (pps_kc_hardpps_dev == (void *) tty ||
 4871 +				    pps_kc_hardpps_dev == NULL) {
 4872 +					pps_kc_hardpps_mode = parms.edge;
 4873 +					pps_kc_hardpps_dev = tty;
 4874 +#ifdef SERIAL_DEBUG_NTP_PPS
 4875 +					printk(KERN_DEBUG "PPS_IOC_KC_BIND: "
 4876 +					       "new kernel consumer: dev=%p, "
 4877 +					       "edge=0x%x\n",
 4878 +					       pps_kc_hardpps_dev,
 4879 +					       pps_kc_hardpps_mode);
 4880 +#endif /* SERIAL_DEBUG_NTP_PPS */
 4881 +				} else {
 4882 +					error = -EINVAL;
 4883 +					RESTORE_FLAGS_AND_RETURN;
 4884 +				}
 4885 +			}
 4886 +			RESTORE_FLAGS_AND_RETURN;
 4887 +		}
 4888 +#endif /* CONFIG_NTP_PPS_SERIAL */
 4889  
 4890  		default:
 4891  			return -ENOIOCTLCMD;
 4892 @@ -2869,6 +3443,32 @@
 4893  		 */
 4894  		rs_wait_until_sent(tty, info->timeout);
 4895  	}
 4896 +#ifdef CONFIG_NTP_PPS_SERIAL
 4897 +	if (info->tty->disc_data != NULL
 4898 +	    && ((struct pps *)(info->tty->disc_data))->magic == PPSCLOCK_MAGIC)
 4899 +	{
 4900 +		struct pps *tmp = (struct pps *) info->tty->disc_data;
 4901 +		if (waitqueue_active(&tmp->state.ewait))
 4902 +			wake_up_interruptible(&tmp->state.ewait);
 4903 +		info->tty->termios->c_line = N_TTY;
 4904 +		info->tty->disc_data = NULL;
 4905 +		tmp->magic = 0;
 4906 +		kfree(tmp);
 4907 +		if (pps_kc_hardpps_dev == (void *) info->tty) {
 4908 +			pps_kc_hardpps_mode = 0;
 4909 +			pps_kc_hardpps_dev = NULL;
 4910 +#ifdef SERIAL_DEBUG_NTP_PPS
 4911 +			printk(KERN_DEBUG
 4912 +			       "rs_close(): unbound kernel consumer dev %p\n",
 4913 +			       info->tty);
 4914 +#endif /* SERIAL_DEBUG_NTP_PPS */
 4915 +		}
 4916 +#ifdef SERIAL_DEBUG_NTP_PPS
 4917 +		printk(KERN_DEBUG "rs_close(): removed pps %p from tty %p\n",
 4918 +		       tmp, tty);
 4919 +#endif /* SERIAL_DEBUG_NTP_PPS */
 4920 +	}
 4921 +#endif /* CONFIG_NTP_PPS_SERIAL */
 4922  	shutdown(info);
 4923  	if (tty->driver.flush_buffer)
 4924  		tty->driver.flush_buffer(tty);
 4925 @@ -3273,6 +3873,9 @@
 4926  {
 4927  	struct async_struct *info = state->info, scr_info;
 4928  	char	stat_buf[30], control, status;
 4929 +#ifdef SERIAL_DEBUG_NTP_PPS
 4930 +	char	ier;
 4931 +#endif
 4932  	int	ret;
 4933  	unsigned long flags;
 4934  
 4935 @@ -3307,6 +3910,9 @@
 4936  	save_flags(flags); cli();
 4937  	status = serial_in(info, UART_MSR);
 4938  	control = info != &scr_info ? info->MCR : serial_in(info, UART_MCR);
 4939 +#ifdef SERIAL_DEBUG_NTP_PPS
 4940 +	ier = serial_in(info, UART_IER);
 4941 +#endif
 4942  	restore_flags(flags); 
 4943  
 4944  	stat_buf[0] = 0;
 4945 @@ -3344,6 +3950,9 @@
 4946  	if (state->icount.overrun)
 4947  		ret += sprintf(buf+ret, " oe:%d", state->icount.overrun);
 4948  
 4949 +#ifdef SERIAL_DEBUG_NTP_PPS
 4950 +	ret += sprintf(buf+ret, " IER:%02x", (unsigned) ier);
 4951 +#endif
 4952  	/*
 4953  	 * Last thing is the RS-232 status lines
 4954  	 */
 4955 @@ -3423,6 +4032,10 @@
 4956         " SERIAL_ACPI"
 4957  #define SERIAL_OPT
 4958  #endif
 4959 +#ifdef CONFIG_NTP_PPS_SERIAL
 4960 +	       " PPS_API(RFC-2783)"
 4961 +#define SERIAL_OPT
 4962 +#endif
 4963  #ifdef SERIAL_OPT
 4964         " enabled\n";
 4965  #else
 4966 @@ -5670,7 +6283,8 @@
 4967  	state = &rs_table[i];
 4968  	if (rs_table[i].count) {
 4969  		restore_flags(flags);
 4970 -		printk("Couldn't configure serial #%d (port=%ld,irq=%d): "
 4971 +		printk(KERN_ERR
 4972 +		       "Couldn't configure serial #%d (port=%ld,irq=%d): "
 4973  		       "device already open\n", i, port, req->irq);
 4974  		return -1;
 4975  	}
 4976 @@ -5692,7 +6306,7 @@
 4977  	autoconfig(state);
 4978  	if (state->type == PORT_UNKNOWN) {
 4979  		restore_flags(flags);
 4980 -		printk("register_serial(): autoconfig failed\n");
 4981 +		printk(KERN_ERR "register_serial(): autoconfig failed\n");
 4982  		return -1;
 4983  	}
 4984  	restore_flags(flags);
 4985 @@ -5753,10 +6367,12 @@
 4986  	save_flags(flags); cli();
 4987          remove_bh(SERIAL_BH);
 4988  	if ((e1 = tty_unregister_driver(&serial_driver)))
 4989 -		printk("serial: failed to unregister serial driver (%d)\n",
 4990 +		printk(KERN_WARNING
 4991 +		       "serial: failed to unregister serial driver (%d)\n",
 4992  		       e1);
 4993  	if ((e2 = tty_unregister_driver(&callout_driver)))
 4994 -		printk("serial: failed to unregister callout driver (%d)\n", 
 4995 +		printk(KERN_WARNING
 4996 +		       "serial: failed to unregister callout driver (%d)\n", 
 4997  		       e2);
 4998  	restore_flags(flags);
 4999  
 5000 Index: linux/drivers/char/tty_io.c
 5001 diff -u linux/drivers/char/tty_io.c:1.1.1.20 linux/drivers/char/tty_io.c:1.1.1.20.2.1
 5002 --- linux/drivers/char/tty_io.c:1.1.1.20	Sat Aug 26 21:35:12 2006
 5003 +++ linux/drivers/char/tty_io.c	Sat Aug 26 21:49:53 2006
 5004 @@ -2626,7 +2626,6 @@
 5005  	return 0;
 5006  }
 5007  
 5008 -
 5009  /*
 5010   * Initialize the console device. This is called *early*, so
 5011   * we can't necessarily depend on lots of kernel help here.
 5012 @@ -2638,6 +2637,9 @@
 5013  	/* Setup the default TTY line discipline. */
 5014  	memset(tty_ldiscs, 0, NR_LDISCS*sizeof(struct tty_ldisc));
 5015  	(void) tty_register_ldisc(N_TTY, &tty_ldisc_N_TTY);
 5016 +#ifdef	CONFIG_NTP_PPS
 5017 +	(void) tty_register_ldisc(N_PPSCLOCK, &tty_ldisc_N_PPSCLOCK);
 5018 +#endif
 5019  
 5020  	/*
 5021  	 * Set up the standard termios.  Individual tty drivers may 
 5022 Index: linux/drivers/char/ftape/lowlevel/ftape-calibr.c
 5023 diff -u linux/drivers/char/ftape/lowlevel/ftape-calibr.c:1.1.1.5 linux/drivers/char/ftape/lowlevel/ftape-calibr.c:1.1.1.5.2.1
 5024 --- linux/drivers/char/ftape/lowlevel/ftape-calibr.c:1.1.1.5	Sat Aug 26 21:35:14 2006
 5025 +++ linux/drivers/char/ftape/lowlevel/ftape-calibr.c	Sat Aug 26 21:49:56 2006
 5026 @@ -26,7 +26,7 @@
 5027  
 5028  #include <linux/config.h>
 5029  #include <linux/errno.h>
 5030 -#include <linux/sched.h>
 5031 +#include <linux/time.h>
 5032  #include <asm/system.h>
 5033  #include <asm/io.h>
 5034  #if defined(__alpha__)
 5035 @@ -35,7 +35,7 @@
 5036  # include <asm/msr.h>
 5037  # include <asm/timex.h>
 5038  #elif defined(__i386__) 
 5039 -# include <linux/timex.h>
 5040 +# include <asm/timex.h>
 5041  #endif
 5042  #include <linux/ftape.h>
 5043  #include "../lowlevel/ftape-tracing.h"
 5044 Index: linux/drivers/i2c/i2c-core.c
 5045 diff -u linux/drivers/i2c/i2c-core.c:1.1.1.14 linux/drivers/i2c/i2c-core.c:1.1.1.14.2.1
 5046 --- linux/drivers/i2c/i2c-core.c:1.1.1.14	Sat Aug 26 21:34:54 2006
 5047 +++ linux/drivers/i2c/i2c-core.c	Sat Aug 26 21:49:58 2006
 5048 @@ -750,7 +750,7 @@
 5049  		msg.addr   = client->addr;
 5050  		msg.flags = client->flags & I2C_M_TEN;
 5051  		msg.len = count;
 5052 -		(const char *)msg.buf = buf;
 5053 +		msg.buf = buf;
 5054  	
 5055  		DEB2(printk(KERN_DEBUG "i2c-core.o: master_send: writing %d bytes on %s.\n",
 5056  			count,client->adapter->name));
 5057 Index: linux/drivers/i2c/i2c-proc.c
 5058 diff -u linux/drivers/i2c/i2c-proc.c:1.1.1.5 linux/drivers/i2c/i2c-proc.c:1.1.1.5.6.1
 5059 --- linux/drivers/i2c/i2c-proc.c:1.1.1.5	Sat Jan 22 22:04:10 2005
 5060 +++ linux/drivers/i2c/i2c-proc.c	Sat Aug 26 21:49:58 2006
 5061 @@ -43,9 +43,9 @@
 5062  
 5063  static int i2c_create_name(char **name, const char *prefix,
 5064  			       struct i2c_adapter *adapter, int addr);
 5065 -static int i2c_parse_reals(int *nrels, void *buffer, int bufsize,
 5066 +static int i2c_parse_reals(int *nrels, char *buffer, int bufsize,
 5067  			       long *results, int magnitude);
 5068 -static int i2c_write_reals(int nrels, void *buffer, int *bufsize,
 5069 +static int i2c_write_reals(int nrels, char *buffer, int *bufsize,
 5070  			       long *results, int magnitude);
 5071  static int i2c_proc_chips(ctl_table * ctl, int write,
 5072  			      struct file *filp, void *buffer,
 5073 @@ -205,7 +205,7 @@
 5074  		table = i2c_entries[id]->ctl_table;
 5075  		unregister_sysctl_table(i2c_entries[id]);
 5076  		/* 2-step kfree needed to keep gcc happy about const points */
 5077 -		(const char *) temp = table[4].procname;
 5078 +		temp = table[4].procname;
 5079  		kfree(temp);
 5080  		kfree(table);
 5081  		i2c_entries[id] = NULL;
 5082 @@ -287,7 +287,7 @@
 5083  			if(copy_to_user(buffer, BUF, buflen))
 5084  				return -EFAULT;
 5085  			curbufsize += buflen;
 5086 -			(char *) buffer += buflen;
 5087 +			buffer += buflen;
 5088  		}
 5089  	*lenp = curbufsize;
 5090  	filp->f_pos += curbufsize;
 5091 @@ -318,8 +318,7 @@
 5092  					     sizeof(struct
 5093  						    i2c_chips_data)))
 5094  					return -EFAULT;
 5095 -				(char *) oldval +=
 5096 -				    sizeof(struct i2c_chips_data);
 5097 +				oldval += sizeof(struct i2c_chips_data);
 5098  				nrels++;
 5099  			}
 5100  		oldlen = nrels * sizeof(struct i2c_chips_data);
 5101 @@ -456,7 +455,7 @@
 5102     WARNING! This is tricky code. I have tested it, but there may still be
 5103              hidden bugs in it, even leading to crashes and things!
 5104  */
 5105 -int i2c_parse_reals(int *nrels, void *buffer, int bufsize,
 5106 +int i2c_parse_reals(int *nrels, char *buffer, int bufsize,
 5107  			 long *results, int magnitude)
 5108  {
 5109  	int maxels, min, mag;
 5110 @@ -470,10 +469,10 @@
 5111  
 5112  		/* Skip spaces at the start */
 5113  		while (bufsize && 
 5114 -		       !((ret=get_user(nextchar, (char *) buffer))) &&
 5115 +		       !((ret=get_user(nextchar, buffer))) &&
 5116  		       isspace((int) nextchar)) {
 5117  			bufsize--;
 5118 -			((char *) buffer)++;
 5119 +			buffer++;
 5120  		}
 5121  
 5122  		if (ret)
 5123 @@ -488,22 +487,22 @@
 5124  		mag = magnitude;
 5125  
 5126  		/* Check for a minus */
 5127 -		if (!((ret=get_user(nextchar, (char *) buffer)))
 5128 +		if (!((ret=get_user(nextchar, buffer)))
 5129  		    && (nextchar == '-')) {
 5130  			min = 1;
 5131  			bufsize--;
 5132 -			((char *) buffer)++;
 5133 +			buffer++;
 5134  		}
 5135  		if (ret)
 5136  			return -EFAULT;
 5137  
 5138  		/* Digits before a decimal dot */
 5139  		while (bufsize && 
 5140 -		       !((ret=get_user(nextchar, (char *) buffer))) &&
 5141 +		       !((ret=get_user(nextchar, buffer))) &&
 5142  		       isdigit((int) nextchar)) {
 5143  			res = res * 10 + nextchar - '0';
 5144  			bufsize--;
 5145 -			((char *) buffer)++;
 5146 +			buffer++;
 5147  		}
 5148  		if (ret)
 5149  			return -EFAULT;
 5150 @@ -517,16 +516,16 @@
 5151  		if (bufsize && (nextchar == '.')) {
 5152  			/* Skip the dot */
 5153  			bufsize--;
 5154 -			((char *) buffer)++;
 5155 +			buffer++;
 5156  
 5157  			/* Read digits while they are significant */
 5158  			while (bufsize && (mag > 0) &&
 5159 -			       !((ret=get_user(nextchar, (char *) buffer))) &&
 5160 +			       !((ret=get_user(nextchar, buffer))) &&
 5161  			       isdigit((int) nextchar)) {
 5162  				res = res * 10 + nextchar - '0';
 5163  				mag--;
 5164  				bufsize--;
 5165 -				((char *) buffer)++;
 5166 +				buffer++;
 5167  			}
 5168  			if (ret)
 5169  				return -EFAULT;
 5170 @@ -539,10 +538,10 @@
 5171  
 5172  		/* Skip everything until we hit whitespace */
 5173  		while (bufsize && 
 5174 -		       !((ret=get_user(nextchar, (char *) buffer))) &&
 5175 +		       !((ret=get_user(nextchar, buffer))) &&
 5176  		       !isspace((int) nextchar)) {
 5177  			bufsize--;
 5178 -			((char *) buffer)++;
 5179 +			buffer++;
 5180  		}
 5181  		if (ret)
 5182  			return -EFAULT;
 5183 @@ -557,7 +556,7 @@
 5184  	return 0;
 5185  }
 5186  
 5187 -int i2c_write_reals(int nrels, void *buffer, int *bufsize,
 5188 +int i2c_write_reals(int nrels, char *buffer, int *bufsize,
 5189  			 long *results, int magnitude)
 5190  {
 5191  #define BUFLEN 20
 5192 @@ -571,10 +570,10 @@
 5193  		mag = magnitude;
 5194  
 5195  		if (nr != 0) {
 5196 -			if(put_user(' ', (char *) buffer))
 5197 +			if(put_user(' ', buffer))
 5198  				return -EFAULT;
 5199  			curbufsize++;
 5200 -			((char *) buffer)++;
 5201 +			buffer++;
 5202  		}
 5203  
 5204  		/* Fill BUF with the representation of the next string */
 5205 @@ -612,15 +611,15 @@
 5206  		/* Now copy it to the user-space buffer */
 5207  		if (buflen + curbufsize > *bufsize)
 5208  			buflen = *bufsize - curbufsize;
 5209 -		if(copy_to_user(buffer, BUF, buflen))
 5210 +		if (copy_to_user(buffer, BUF, buflen))
 5211  			return -EFAULT;
 5212  		curbufsize += buflen;
 5213 -		(char *) buffer += buflen;
 5214 +		buffer += buflen;
 5215  
 5216  		nr++;
 5217  	}
 5218  	if (curbufsize < *bufsize) {
 5219 -		if(put_user('\n', (char *) buffer))
 5220 +		if(put_user('\n', buffer))
 5221  			return -EFAULT;
 5222  		curbufsize++;
 5223  	}
 5224 Index: linux/drivers/sbus/char/sunkbd.c
 5225 diff -u linux/drivers/sbus/char/sunkbd.c:1.1.1.5 linux/drivers/sbus/char/sunkbd.c:1.1.1.5.12.1
 5226 --- linux/drivers/sbus/char/sunkbd.c:1.1.1.5	Sun Aug 18 22:10:34 2002
 5227 +++ linux/drivers/sbus/char/sunkbd.c	Sat Aug 26 21:50:02 2006
 5228 @@ -1296,7 +1296,7 @@
 5229  	if (next != kbd_tail){
 5230  		kbd_queue [kbd_head].id = scan & KBD_KEYMASK;
 5231  		kbd_queue [kbd_head].value=scan & KBD_UP ? VKEY_UP : VKEY_DOWN;
 5232 -		kbd_queue [kbd_head].time = xtime;
 5233 +		do_gettimeofday(&kbd_queue [kbd_head].time);
 5234  		kbd_head = next;
 5235  	}
 5236  	spin_unlock_irqrestore(&kbd_queue_lock, flags);
 5237 Index: linux/drivers/sbus/char/sunmouse.c
 5238 diff -u linux/drivers/sbus/char/sunmouse.c:1.1.1.2 linux/drivers/sbus/char/sunmouse.c:1.1.1.2.24.1
 5239 --- linux/drivers/sbus/char/sunmouse.c:1.1.1.2	Fri Feb  2 20:10:41 2001
 5240 +++ linux/drivers/sbus/char/sunmouse.c	Sat Aug 26 21:50:02 2006
 5241 @@ -360,20 +360,20 @@
 5242  			ev.id = MS_MIDDLE;
 5243  			ev.value = bstate & BUTTON_MIDDLE;
 5244  		}
 5245 -		ev.time = xtime;
 5246 +		do_gettimeofday(&ev.time);
 5247  		ev.value = ev.value ? VKEY_DOWN : VKEY_UP;
 5248  		pushed += push_event (&ev);
 5249  	}
 5250  	if (sunmouse.delta_x) {
 5251  		ev.id = LOC_X_DELTA;
 5252 -		ev.time = xtime;
 5253 +		do_gettimeofday(&ev.time);
 5254  		ev.value = sunmouse.delta_x;
 5255  		pushed += push_event (&ev);
 5256  		sunmouse.delta_x = 0;
 5257  	}
 5258  	if (sunmouse.delta_y) {
 5259  		ev.id = LOC_Y_DELTA;
 5260 -		ev.time = xtime;
 5261 +		do_gettimeofday(&ev.time);
 5262  		ev.value = sunmouse.delta_y;
 5263  		pushed += push_event (&ev);
 5264  	}
 5265 Index: linux/drivers/scsi/aic7xxx/Makefile
 5266 diff -u linux/drivers/scsi/aic7xxx/Makefile:1.1.1.8 linux/drivers/scsi/aic7xxx/Makefile:1.1.1.8.2.1
 5267 --- linux/drivers/scsi/aic7xxx/Makefile:1.1.1.8	Sat Aug 26 21:35:23 2006
 5268 +++ linux/drivers/scsi/aic7xxx/Makefile	Sat Aug 26 21:50:05 2006
 5269 @@ -13,7 +13,7 @@
 5270  obj-$(CONFIG_SCSI_AIC79XX)	+= aic79xx.o
 5271  endif
 5272  
 5273 -EXTRA_CFLAGS += -I$(TOPDIR)/drivers/scsi -Werror
 5274 +EXTRA_CFLAGS += -I$(TOPDIR)/drivers/scsi #-Werror
 5275  #EXTRA_CFLAGS += -g
 5276  
 5277  # Platform Specific Files
 5278 Index: linux/drivers/sound/sound_firmware.c
 5279 diff -u linux/drivers/sound/sound_firmware.c:1.1.1.2 linux/drivers/sound/sound_firmware.c:1.1.1.2.22.1
 5280 --- linux/drivers/sound/sound_firmware.c:1.1.1.2	Sun Mar 11 14:55:33 2001
 5281 +++ linux/drivers/sound/sound_firmware.c	Sat Aug 26 21:50:07 2006
 5282 @@ -7,7 +7,6 @@
 5283  #include <linux/unistd.h>
 5284  #include <asm/uaccess.h>
 5285  
 5286 -static int errno;
 5287  static int do_mod_firmware_load(const char *fn, char **fp)
 5288  {
 5289  	int fd;
 5290 Index: linux/drivers/usb/inode.c
 5291 diff -u linux/drivers/usb/inode.c:1.1.1.8 linux/drivers/usb/inode.c:1.1.1.8.6.1
 5292 --- linux/drivers/usb/inode.c:1.1.1.8	Sat Jan 22 21:56:55 2005
 5293 +++ linux/drivers/usb/inode.c	Sat Aug 26 21:50:10 2006
 5294 @@ -310,7 +310,7 @@
 5295  	return 1;
 5296  }
 5297  
 5298 -static struct dentry_operations usbdevfs_dentry_operations = {
 5299 +/*static*/ struct dentry_operations usbdevfs_dentry_operations = {
 5300  	d_revalidate:	usbdevfs_revalidate,
 5301  };
 5302  
 5303 @@ -495,11 +495,11 @@
 5304  	lookup: usbdevfs_root_lookup,
 5305  };
 5306  
 5307 -static struct file_operations usbdevfs_bus_file_operations = {
 5308 +/*static*/ struct file_operations usbdevfs_bus_file_operations = {
 5309  	readdir: usbdevfs_bus_readdir,
 5310  };
 5311  
 5312 -static struct inode_operations usbdevfs_bus_inode_operations = {
 5313 +/*static*/ struct inode_operations usbdevfs_bus_inode_operations = {
 5314  	lookup: usbdevfs_bus_lookup,
 5315  };
 5316  
 5317 Index: linux/fs/affs/file.c
 5318 diff -u linux/fs/affs/file.c:1.1.1.11 linux/fs/affs/file.c:1.1.1.11.2.1
 5319 --- linux/fs/affs/file.c:1.1.1.11	Sat Aug 26 21:34:25 2006
 5320 +++ linux/fs/affs/file.c	Sat Aug 26 21:50:12 2006
 5321 @@ -15,7 +15,7 @@
 5322  #include <asm/div64.h>
 5323  #include <asm/uaccess.h>
 5324  #include <asm/system.h>
 5325 -#include <linux/sched.h>
 5326 +#include <linux/time.h>
 5327  #include <linux/affs_fs.h>
 5328  #include <linux/fcntl.h>
 5329  #include <linux/kernel.h>
 5330 Index: linux/fs/isofs/util.c
 5331 diff -u linux/fs/isofs/util.c:1.1.1.6 linux/fs/isofs/util.c:1.1.1.6.2.1
 5332 --- linux/fs/isofs/util.c:1.1.1.6	Sat Aug 26 21:34:29 2006
 5333 +++ linux/fs/isofs/util.c	Sat Aug 26 21:50:14 2006
 5334 @@ -2,7 +2,6 @@
 5335   *  linux/fs/isofs/util.c
 5336   */
 5337  
 5338 -#include <linux/time.h>
 5339  #include <linux/iso_fs.h>
 5340  
 5341  /* 
 5342 Index: linux/fs/udf/udfdecl.h
 5343 diff -u linux/fs/udf/udfdecl.h:1.1.1.8 linux/fs/udf/udfdecl.h:1.1.1.8.2.1
 5344 --- linux/fs/udf/udfdecl.h:1.1.1.8	Sat Aug 26 21:34:24 2006
 5345 +++ linux/fs/udf/udfdecl.h	Sat Aug 26 21:50:16 2006
 5346 @@ -31,7 +31,7 @@
 5347  #define UDF_NAME_LEN		255
 5348  #define UDF_PATH_LEN		1023
 5349  
 5350 -#define CURRENT_UTIME	(xtime.tv_usec)
 5351 +#define CURRENT_UTIME	(xtime.tv_nsec / 1000)
 5352  
 5353  #define udf_file_entry_alloc_offset(inode)\
 5354  	((UDF_I_EXTENDED_FE(inode) ?\
 5355 Index: linux/include/asm-alpha/termios.h
 5356 diff -u linux/include/asm-alpha/termios.h:1.1.1.3 linux/include/asm-alpha/termios.h:1.1.1.3.22.1
 5357 --- linux/include/asm-alpha/termios.h:1.1.1.3	Sat Jul 28 22:52:25 2001
 5358 +++ linux/include/asm-alpha/termios.h	Sat Aug 26 21:50:19 2006
 5359 @@ -83,6 +83,7 @@
 5360  #define N_HDLC		13	/* synchronous HDLC */
 5361  #define N_SYNC_PPP	14
 5362  #define N_HCI		15  /* Bluetooth HCI UART */
 5363 +#define	N_PPSCLOCK	16	/* pulse-per-second detection */
 5364  
 5365  #ifdef __KERNEL__
 5366  /*	eof=^D		eol=\0		eol2=\0		erase=del
 5367 Index: linux/include/asm-alpha/timex.h
 5368 diff -u linux/include/asm-alpha/timex.h:1.1.1.5 linux/include/asm-alpha/timex.h:1.1.1.5.2.1
 5369 --- linux/include/asm-alpha/timex.h:1.1.1.5	Sat Aug 26 21:35:52 2006
 5370 +++ linux/include/asm-alpha/timex.h	Sat Aug 26 21:50:19 2006
 5371 @@ -8,6 +8,9 @@
 5372  
 5373  #define CLOCK_TICK_RATE	1193180 /* Underlying HZ */
 5374  
 5375 +/* LATCH is used in the interval timer and ftape setup. */
 5376 +#define LATCH  ((CLOCK_TICK_RATE + HZ/2) / HZ)	/* For divider */
 5377 +
 5378  /*
 5379   * Standard way to access the cycle counter.
 5380   * Currently only used on SMP for scheduling.
 5381 Index: linux/include/asm-arm/termios.h
 5382 diff -u linux/include/asm-arm/termios.h:1.1.1.3 linux/include/asm-arm/termios.h:1.1.1.3.22.1
 5383 --- linux/include/asm-arm/termios.h:1.1.1.3	Sat Jul 28 22:52:27 2001
 5384 +++ linux/include/asm-arm/termios.h	Sat Aug 26 21:50:21 2006
 5385 @@ -66,6 +66,7 @@
 5386  #define N_HDLC		13	/* synchronous HDLC */
 5387  #define N_SYNC_PPP	14
 5388  #define N_HCI		15  /* Bluetooth HCI UART */
 5389 +#define	N_PPSCLOCK	16	/* pulse-per-second detection */
 5390  
 5391  #ifdef __KERNEL__
 5392  
 5393 Index: linux/include/asm-i386/processor.h
 5394 diff -u linux/include/asm-i386/processor.h:1.1.1.15 linux/include/asm-i386/processor.h:1.1.1.15.2.1
 5395 --- linux/include/asm-i386/processor.h:1.1.1.15	Sat Aug 26 21:35:53 2006
 5396 +++ linux/include/asm-i386/processor.h	Sat Aug 26 21:50:24 2006
 5397 @@ -72,7 +72,6 @@
 5398   */
 5399  
 5400  extern struct cpuinfo_x86 boot_cpu_data;
 5401 -extern struct tss_struct init_tss[NR_CPUS];
 5402  
 5403  #ifdef CONFIG_SMP
 5404  extern struct cpuinfo_x86 cpu_data[];
 5405 @@ -357,6 +356,8 @@
 5406  	unsigned long __cacheline_filler[5];
 5407  };
 5408  
 5409 +extern struct tss_struct init_tss[NR_CPUS];
 5410 +
 5411  struct thread_struct {
 5412  	unsigned long	esp0;
 5413  	unsigned long	eip;
 5414 Index: linux/include/asm-i386/termios.h
 5415 diff -u linux/include/asm-i386/termios.h:1.1.1.6 linux/include/asm-i386/termios.h:1.1.1.6.2.1
 5416 --- linux/include/asm-i386/termios.h:1.1.1.6	Sat Aug 26 21:35:53 2006
 5417 +++ linux/include/asm-i386/termios.h	Sat Aug 26 21:50:24 2006
 5418 @@ -58,6 +58,7 @@
 5419  #define N_HDLC		13	/* synchronous HDLC */
 5420  #define N_SYNC_PPP	14	/* synchronous PPP */
 5421  #define N_HCI		15  /* Bluetooth HCI UART */
 5422 +#define	N_PPSCLOCK	16	/* pulse-per-second detection */
 5423  
 5424  #ifdef __KERNEL__
 5425  
 5426 Index: linux/include/asm-i386/timex.h
 5427 diff -u linux/include/asm-i386/timex.h:1.1.1.6 linux/include/asm-i386/timex.h:1.1.1.6.2.1
 5428 --- linux/include/asm-i386/timex.h:1.1.1.6	Sat Aug 26 21:35:53 2006
 5429 +++ linux/include/asm-i386/timex.h	Sat Aug 26 21:50:24 2006
 5430 @@ -16,6 +16,10 @@
 5431  #endif
 5432  
 5433  #define CLOCK_TICK_FACTOR	20	/* Factor of both 1000000 and CLOCK_TICK_RATE */
 5434 +
 5435 +/* LATCH is used in the interval timer and ftape setup. */
 5436 +#define LATCH  ((CLOCK_TICK_RATE + HZ/2) / HZ)	/* For divider */
 5437 +
 5438  #define FINETUNE ((((((long)LATCH * HZ - CLOCK_TICK_RATE) << SHIFT_HZ) * \
 5439  	(1000000/CLOCK_TICK_FACTOR) / (CLOCK_TICK_RATE/CLOCK_TICK_FACTOR)) \
 5440  		<< (SHIFT_SCALE-SHIFT_HZ)) / HZ)
 5441 @@ -50,6 +54,11 @@
 5442  #endif
 5443  }
 5444  
 5445 +/* The following two items are set if TSC is configured and operational: */
 5446 +extern	unsigned long cpu_khz;		/* CPU frequency in kHz */
 5447 +extern	unsigned long exact_nanotime_quotient;
 5448 +					/* 2^28 * (1 / (clocks per nsec) */
 5449 +
 5450  extern unsigned long cpu_khz;
 5451  
 5452  #define vxtime_lock()		do {} while (0)
 5453 Index: linux/include/asm-m68k/machdep.h
 5454 diff -u linux/include/asm-m68k/machdep.h:1.1.1.7 linux/include/asm-m68k/machdep.h:1.1.1.7.2.1
 5455 --- linux/include/asm-m68k/machdep.h:1.1.1.7	Sat Aug 26 21:35:54 2006
 5456 +++ linux/include/asm-m68k/machdep.h	Sat Aug 26 21:50:26 2006
 5457 @@ -3,7 +3,7 @@
 5458  
 5459  struct pt_regs;
 5460  struct kbd_repeat;
 5461 -struct mktime;
 5462 +/*struct mktime; -- conflicts with function of same name --UW */
 5463  struct rtc_time;
 5464  struct rtc_pll_info;
 5465  struct gendisk;
 5466 Index: linux/include/asm-m68k/termios.h
 5467 diff -u linux/include/asm-m68k/termios.h:1.1.1.6 linux/include/asm-m68k/termios.h:1.1.1.6.2.1
 5468 --- linux/include/asm-m68k/termios.h:1.1.1.6	Sat Aug 26 21:35:54 2006
 5469 +++ linux/include/asm-m68k/termios.h	Sat Aug 26 21:50:26 2006
 5470 @@ -68,6 +68,7 @@
 5471  #define N_HDLC		13	/* synchronous HDLC */
 5472  #define N_SYNC_PPP	14
 5473  #define N_HCI		15  /* Bluetooth HCI UART */
 5474 +#define	N_PPSCLOCK	16	/* pulse-per-second detection */
 5475  
 5476  #ifdef __KERNEL__
 5477  
 5478 Index: linux/include/asm-m68k/timex.h
 5479 diff -u linux/include/asm-m68k/timex.h:1.1.1.4 linux/include/asm-m68k/timex.h:1.1.1.4.2.1
 5480 --- linux/include/asm-m68k/timex.h:1.1.1.4	Sat Aug 26 21:35:54 2006
 5481 +++ linux/include/asm-m68k/timex.h	Sat Aug 26 21:50:26 2006
 5482 @@ -12,6 +12,9 @@
 5483  	(1000000/CLOCK_TICK_FACTOR) / (CLOCK_TICK_RATE/CLOCK_TICK_FACTOR)) \
 5484  		<< (SHIFT_SCALE-SHIFT_HZ)) / HZ)
 5485  
 5486 +/* LATCH is used in the interval timer and ftape setup. */
 5487 +#define LATCH  ((CLOCK_TICK_RATE + HZ/2) / HZ)	/* For divider */
 5488 +
 5489  typedef unsigned long cycles_t;
 5490  
 5491  static inline cycles_t get_cycles(void)
 5492 Index: linux/include/asm-mips/termios.h
 5493 diff -u linux/include/asm-mips/termios.h:1.1.1.7 linux/include/asm-mips/termios.h:1.1.1.7.2.1
 5494 --- linux/include/asm-mips/termios.h:1.1.1.7	Sat Aug 26 21:35:56 2006
 5495 +++ linux/include/asm-mips/termios.h	Sat Aug 26 21:50:29 2006
 5496 @@ -103,6 +103,7 @@
 5497  #define N_HDLC		13	/* synchronous HDLC */
 5498  #define N_SYNC_PPP	14	/* synchronous PPP */
 5499  #define N_HCI		15	/* Bluetooth HCI UART */
 5500 +#define	N_PPSCLOCK	16	/* pulse-per-second detection */
 5501  
 5502  #ifdef __KERNEL__
 5503  
 5504 Index: linux/include/asm-mips/timex.h
 5505 diff -u linux/include/asm-mips/timex.h:1.1.1.6 linux/include/asm-mips/timex.h:1.1.1.6.2.1
 5506 --- linux/include/asm-mips/timex.h:1.1.1.6	Sat Aug 26 21:35:55 2006
 5507 +++ linux/include/asm-mips/timex.h	Sat Aug 26 21:50:29 2006
 5508 @@ -12,6 +12,9 @@
 5509  
 5510  #include <asm/mipsregs.h>
 5511  
 5512 +/* LATCH is used in the interval timer and ftape setup. */
 5513 +#define LATCH  ((CLOCK_TICK_RATE + HZ/2) / HZ)	/* For divider */
 5514 +
 5515  /*
 5516   * Standard way to access the cycle counter.
 5517   * Currently only used on SMP for scheduling.
 5518 Index: linux/include/asm-mips64/timex.h
 5519 diff -u linux/include/asm-mips64/timex.h:1.1.1.4 linux/include/asm-mips64/timex.h:1.1.1.4.6.1
 5520 --- linux/include/asm-mips64/timex.h:1.1.1.4	Sat Jan 22 21:45:02 2005
 5521 +++ linux/include/asm-mips64/timex.h	Sat Aug 26 21:50:32 2006
 5522 @@ -12,6 +12,9 @@
 5523  
 5524  #include <asm/mipsregs.h>
 5525  
 5526 +/* LATCH is used in the interval timer and ftape setup. */
 5527 +#define LATCH  ((CLOCK_TICK_RATE + HZ/2) / HZ)	/* For divider */
 5528 +
 5529  /*
 5530   * Standard way to access the cycle counter.
 5531   * Currently only used on SMP for scheduling.
 5532 Index: linux/include/asm-ppc/termios.h
 5533 diff -u linux/include/asm-ppc/termios.h:1.1.1.7 linux/include/asm-ppc/termios.h:1.1.1.7.6.1
 5534 --- linux/include/asm-ppc/termios.h:1.1.1.7	Sat Jan 22 21:45:09 2005
 5535 +++ linux/include/asm-ppc/termios.h	Sat Aug 26 21:50:34 2006
 5536 @@ -192,6 +192,7 @@
 5537  #define N_HDLC		13	/* synchronous HDLC */
 5538  #define N_SYNC_PPP	14
 5539  #define N_HCI		15  /* Bluetooth HCI UART */
 5540 +#define	N_PPSCLOCK	16	/* pulse-per-second detection */
 5541  
 5542  #ifdef __KERNEL__
 5543  
 5544 Index: linux/include/asm-ppc/timex.h
 5545 diff -u linux/include/asm-ppc/timex.h:1.1.1.5 linux/include/asm-ppc/timex.h:1.1.1.5.8.1
 5546 --- linux/include/asm-ppc/timex.h:1.1.1.5	Fri Aug 15 22:29:02 2003
 5547 +++ linux/include/asm-ppc/timex.h	Sat Aug 26 21:50:34 2006
 5548 @@ -16,6 +16,9 @@
 5549  	(1000000/CLOCK_TICK_FACTOR) / (CLOCK_TICK_RATE/CLOCK_TICK_FACTOR)) \
 5550  		<< (SHIFT_SCALE-SHIFT_HZ)) / HZ)
 5551  
 5552 +/* LATCH is used in the interval timer and ftape setup. */
 5553 +#define LATCH  ((CLOCK_TICK_RATE + HZ/2) / HZ)	/* For divider */
 5554 +
 5555  typedef unsigned long cycles_t;
 5556  
 5557  /*
 5558 Index: linux/include/asm-s390/timex.h
 5559 diff -u linux/include/asm-s390/timex.h:1.1.1.6 linux/include/asm-s390/timex.h:1.1.1.6.2.1
 5560 --- linux/include/asm-s390/timex.h:1.1.1.6	Sat Aug 26 21:35:58 2006
 5561 +++ linux/include/asm-s390/timex.h	Sat Aug 26 21:50:36 2006
 5562 @@ -17,6 +17,9 @@
 5563  	(1000000/CLOCK_TICK_FACTOR) / (CLOCK_TICK_RATE/CLOCK_TICK_FACTOR)) \
 5564  		<< (SHIFT_SCALE-SHIFT_HZ)) / HZ)
 5565  
 5566 +/* LATCH is used in the interval timer and ftape setup. */
 5567 +#define LATCH  ((CLOCK_TICK_RATE + HZ/2) / HZ)	/* For divider */
 5568 +
 5569  typedef unsigned long long cycles_t;
 5570  
 5571  extern cycles_t cacheflush_time;
 5572 Index: linux/include/asm-sparc/termios.h
 5573 diff -u linux/include/asm-sparc/termios.h:1.1.1.5 linux/include/asm-sparc/termios.h:1.1.1.5.2.1
 5574 --- linux/include/asm-sparc/termios.h:1.1.1.5	Sat Aug 26 21:35:53 2006
 5575 +++ linux/include/asm-sparc/termios.h	Sat Aug 26 21:50:38 2006
 5576 @@ -71,6 +71,7 @@
 5577  #define N_HDLC		13	/* synchronous HDLC */
 5578  #define N_SYNC_PPP	14	/* synchronous PPP */
 5579  #define N_HCI		15  /* Bluetooth HCI UART */
 5580 +#define	N_PPSCLOCK	16	/* pulse-per-second detection */
 5581  
 5582  #ifdef __KERNEL__
 5583  
 5584 Index: linux/include/asm-sparc/timex.h
 5585 diff -u linux/include/asm-sparc/timex.h:1.1.1.4 linux/include/asm-sparc/timex.h:1.1.1.4.2.1
 5586 --- linux/include/asm-sparc/timex.h:1.1.1.4	Sat Aug 26 21:35:53 2006
 5587 +++ linux/include/asm-sparc/timex.h	Sat Aug 26 21:50:38 2006
 5588 @@ -12,6 +12,9 @@
 5589  	(1000000/CLOCK_TICK_FACTOR) / (CLOCK_TICK_RATE/CLOCK_TICK_FACTOR)) \
 5590  		<< (SHIFT_SCALE-SHIFT_HZ)) / HZ)
 5591  
 5592 +/* LATCH is used in the interval timer and ftape setup. */
 5593 +#define LATCH  ((CLOCK_TICK_RATE + HZ/2) / HZ)	/* For divider */
 5594 +
 5595  /* XXX Maybe do something better at some point... -DaveM */
 5596  typedef unsigned long cycles_t;
 5597  extern cycles_t cacheflush_time;
 5598 Index: linux/include/asm-sparc64/termios.h
 5599 diff -u linux/include/asm-sparc64/termios.h:1.1.1.5 linux/include/asm-sparc64/termios.h:1.1.1.5.2.1
 5600 --- linux/include/asm-sparc64/termios.h:1.1.1.5	Sat Aug 26 21:36:02 2006
 5601 +++ linux/include/asm-sparc64/termios.h	Sat Aug 26 21:50:40 2006
 5602 @@ -71,6 +71,7 @@
 5603  #define N_HDLC		13	/* synchronous HDLC */
 5604  #define N_SYNC_PPP	14	/* synchronous PPP */
 5605  #define N_HCI		15  /* Bluetooth HCI UART */
 5606 +#define	N_PPSCLOCK	16	/* pulse-per-second detection */
 5607  
 5608  #ifdef __KERNEL__
 5609  
 5610 Index: linux/include/asm-sparc64/timex.h
 5611 diff -u linux/include/asm-sparc64/timex.h:1.1.1.8 linux/include/asm-sparc64/timex.h:1.1.1.8.2.1
 5612 --- linux/include/asm-sparc64/timex.h:1.1.1.8	Sat Aug 26 21:36:01 2006
 5613 +++ linux/include/asm-sparc64/timex.h	Sat Aug 26 21:50:40 2006
 5614 @@ -14,6 +14,9 @@
 5615  	(1000000/CLOCK_TICK_FACTOR) / (CLOCK_TICK_RATE/CLOCK_TICK_FACTOR)) \
 5616  		<< (SHIFT_SCALE-SHIFT_HZ)) / HZ)
 5617  
 5618 +/* LATCH is used in the interval timer and ftape setup. */
 5619 +#define LATCH  ((CLOCK_TICK_RATE + HZ/2) / HZ)	/* For divider */
 5620 +
 5621  /* Getting on the cycle counter on sparc64. */
 5622  typedef unsigned long cycles_t;
 5623  #define get_cycles()	tick_ops->get_tick()
 5624 Index: linux/include/linux/fs.h
 5625 diff -u linux/include/linux/fs.h:1.1.1.22 linux/include/linux/fs.h:1.1.1.22.2.1
 5626 --- linux/include/linux/fs.h:1.1.1.22	Sat Aug 26 21:35:41 2006
 5627 +++ linux/include/linux/fs.h	Sat Aug 26 21:50:43 2006
 5628 @@ -1559,7 +1559,7 @@
 5629  unsigned long generate_cluster(kdev_t, int b[], int);
 5630  unsigned long generate_cluster_swab32(kdev_t, int b[], int);
 5631  extern kdev_t ROOT_DEV;
 5632 -extern char root_device_name[];
 5633 +/* extern char root_device_name[]; */
 5634  
 5635  
 5636  extern void show_buffers(void);
 5637 Index: linux/include/linux/i2c.h
 5638 diff -u linux/include/linux/i2c.h:1.1.1.11 linux/include/linux/i2c.h:1.1.1.11.2.1
 5639 --- linux/include/linux/i2c.h:1.1.1.11	Sat Aug 26 21:35:41 2006
 5640 +++ linux/include/linux/i2c.h	Sat Aug 26 21:50:43 2006
 5641 @@ -34,9 +34,19 @@
 5642  #include <linux/i2c-id.h>	/* id values of adapters et. al. 	*/
 5643  #include <linux/types.h>
 5644  
 5645 -
 5646 -struct i2c_msg;
 5647 -
 5648 +/*
 5649 + * I2C Message - used for pure i2c transaction, also from /dev interface
 5650 + */
 5651 +struct i2c_msg {
 5652 +	__u16 addr;	/* slave address			*/
 5653 +	__u16 flags;		
 5654 +#define I2C_M_TEN	0x10	/* we have a ten bit chip address	*/
 5655 +#define I2C_M_RD	0x01
 5656 +#define I2C_M_NOSTART	0x4000
 5657 +#define I2C_M_REV_DIR_ADDR	0x2000
 5658 +	__u16 len;		/* msg length				*/
 5659 +	__u8 *buf;		/* pointer to msg data			*/
 5660 +};
 5661  
 5662  #ifdef __KERNEL__
 5663  
 5664 @@ -358,20 +368,6 @@
 5665  
 5666  #endif /* __KERNEL__ */
 5667  
 5668 -/*
 5669 - * I2C Message - used for pure i2c transaction, also from /dev interface
 5670 - */
 5671 -struct i2c_msg {
 5672 -	__u16 addr;	/* slave address			*/
 5673 -	__u16 flags;		
 5674 -#define I2C_M_TEN	0x10	/* we have a ten bit chip address	*/
 5675 -#define I2C_M_RD	0x01
 5676 -#define I2C_M_NOSTART	0x4000
 5677 -#define I2C_M_REV_DIR_ADDR	0x2000
 5678 -	__u16 len;		/* msg length				*/
 5679 -	__u8 *buf;		/* pointer to msg data			*/
 5680 -};
 5681 -
 5682  /* To determine what functionality is present */
 5683  
 5684  #define I2C_FUNC_I2C			0x00000001
 5685 Index: linux/include/linux/sched.h
 5686 diff -u linux/include/linux/sched.h:1.1.1.20 linux/include/linux/sched.h:1.1.1.20.2.1
 5687 --- linux/include/linux/sched.h:1.1.1.20	Sat Aug 26 21:35:43 2006
 5688 +++ linux/include/linux/sched.h	Sat Aug 26 21:50:43 2006
 5689 @@ -1,6 +1,10 @@
 5690  #ifndef _LINUX_SCHED_H
 5691  #define _LINUX_SCHED_H
 5692  
 5693 +/* 1999-02-02	Ulrich Windl
 5694 + *	Moved time related stuff to <linux/time.h>
 5695 + */
 5696 +
 5697  #include <asm/param.h>	/* for HZ */
 5698  
 5699  extern unsigned long event;
 5700 @@ -11,7 +15,6 @@
 5701  #include <linux/kernel.h>
 5702  #include <linux/types.h>
 5703  #include <linux/times.h>
 5704 -#include <linux/timex.h>
 5705  #include <linux/rbtree.h>
 5706  
 5707  #include <asm/system.h>
 5708 @@ -77,7 +80,6 @@
 5709  extern int last_pid;
 5710  
 5711  #include <linux/fs.h>
 5712 -#include <linux/time.h>
 5713  #include <linux/param.h>
 5714  #include <linux/resource.h>
 5715  #ifdef __KERNEL__
 5716 @@ -583,15 +585,12 @@
 5717  extern unsigned long volatile jiffies;
 5718  extern unsigned long itimer_ticks;
 5719  extern unsigned long itimer_next;
 5720 -extern struct timeval xtime;
 5721  extern void do_timer(struct pt_regs *);
 5722  
 5723  extern unsigned int * prof_buffer;
 5724  extern unsigned long prof_len;
 5725  extern unsigned long prof_shift;
 5726  
 5727 -#define CURRENT_TIME (xtime.tv_sec)
 5728 -
 5729  extern void FASTCALL(__wake_up(wait_queue_head_t *q, unsigned int mode, int nr));
 5730  extern void FASTCALL(__wake_up_sync(wait_queue_head_t *q, unsigned int mode, int nr));
 5731  extern void FASTCALL(sleep_on(wait_queue_head_t *q));
 5732 Index: linux/include/linux/serial.h
 5733 diff -u linux/include/linux/serial.h:1.1.1.8 linux/include/linux/serial.h:1.1.1.8.2.1
 5734 --- linux/include/linux/serial.h:1.1.1.8	Sat Aug 26 21:35:47 2006
 5735 +++ linux/include/linux/serial.h	Sat Aug 26 21:50:43 2006
 5736 @@ -112,8 +112,6 @@
 5737  #define ASYNC_PGRP_LOCKOUT    0x0200 /* Lock out cua opens based on pgrp */
 5738  #define ASYNC_CALLOUT_NOHUP   0x0400 /* Don't do hangups for cua device */
 5739  
 5740 -#define ASYNC_HARDPPS_CD	0x0800	/* Call hardpps when CD goes high  */
 5741 -
 5742  #define ASYNC_SPD_SHI	0x1000	/* Use 230400 instead of 38400 bps */
 5743  #define ASYNC_SPD_WARP	0x1010	/* Use 460800 instead of 38400 bps */
 5744  
 5745 @@ -124,7 +122,7 @@
 5746  
 5747  #define ASYNC_AUTOPROBE	 0x8000 /* Port was autoprobed by PCI or PNP code */
 5748  
 5749 -#define ASYNC_FLAGS	0x7FFF	/* Possible legal async flags */
 5750 +#define ASYNC_FLAGS	0x77FF	/* Possible legal async flags */
 5751  #define ASYNC_USR_MASK	0x3430	/* Legal flags that non-privileged
 5752  				 * users can set or reset */
 5753  
 5754 Index: linux/include/linux/sysctl.h
 5755 diff -u linux/include/linux/sysctl.h:1.1.1.16 linux/include/linux/sysctl.h:1.1.1.16.2.1
 5756 --- linux/include/linux/sysctl.h:1.1.1.16	Sat Aug 26 21:35:47 2006
 5757 +++ linux/include/linux/sysctl.h	Sat Aug 26 21:50:43 2006
 5758 @@ -125,6 +125,7 @@
 5759  	KERN_CORE_USES_PID=52,		/* int: use core or core.%pid */
 5760  	KERN_TAINTED=53,	/* int: various kernel tainted flags */
 5761  	KERN_CADPID=54,		/* int: PID of the process to notify on CAD */
 5762 +	KERN_TIME=55,		/* directory: time */
 5763   	KERN_CORE_PATTERN=56,	/* string: pattern for core-files */
 5764  	KERN_PPC_L3CR=57,       /* l3cr register on PPC */
 5765  	KERN_EXCEPTION_TRACE=58, /* boolean: exception trace */
 5766 @@ -132,6 +133,17 @@
 5767  	KERN_SPARC_SCONS_PWROFF=64, /* int: serial console power-off halt */
 5768  };
 5769  
 5770 +/* KERN_TIME names: */
 5771 +enum
 5772 +{
 5773 +	KERN_TIME_TIMEZONE=1,		/* struct: timezone */
 5774 +	KERN_TIME_RTC_UPDATE=2,		/* int: rtc_update */
 5775 +	KERN_TIME_RTC_RUNS_LOCALTIME=3,	/* int: rtc_runs_localtime */
 5776 +	KERN_TIME_TIME_TICK=4,		/* int: time_tick */
 5777 +	KERN_TIME_TICKADJ=5,		/* int: tickadj */
 5778 +	KERN_TIME_HZ=6,			/* int: HZ */
 5779 +	KERN_TIME_PPS_VAR=99,		/* struct pps_var: pps */
 5780 +};
 5781  
 5782  /* CTL_VM names: */
 5783  enum
 5784 Index: linux/include/linux/time.h
 5785 diff -u linux/include/linux/time.h:1.1.1.5 linux/include/linux/time.h:1.1.1.5.2.1
 5786 --- linux/include/linux/time.h:1.1.1.5	Sat Aug 26 21:35:47 2006
 5787 +++ linux/include/linux/time.h	Sat Aug 26 21:50:43 2006
 5788 @@ -1,8 +1,26 @@
 5789  #ifndef _LINUX_TIME_H
 5790  #define _LINUX_TIME_H
 5791  
 5792 +/* 1999-02-02	Ulrich Windl
 5793 + *	Added time related stuff from <linux/sched.h>
 5794 + * 1999-03-10	Ulrich Windl
 5795 + *	Complete rewrite for nanosecond resolution:  Add public
 5796 + *	``do_nanotime'' and ``do_microtime'' function to get time offset.
 5797 + *	Add functions from POSIX.4 (clock_{gettime,settime,getres}).
 5798 + *	Added ``mktime()''.
 5799 + * 1999-12-04	Ulrich Windl
 5800 + *	Added declaration for ``rtc_update'', ``ktime_to_rtc'', and
 5801 + *	``rtc_to_ktime''.
 5802 + * 2000-03-31	Ulrich Windl
 5803 + *	Make ``do_clock_gettime()'' return the offset since last update of
 5804 + *	``xtime''.
 5805 + * 2000-12-09	Ulrich Windl
 5806 + *	Remove ``(*do_microtime)(void)'' as the nanoseconds seem stable.
 5807 + */
 5808 +#include <asm/timex.h>
 5809  #include <asm/param.h>
 5810  #include <linux/types.h>
 5811 +#include <linux/spinlock.h>
 5812  
 5813  #ifndef _STRUCT_TIMESPEC
 5814  #define _STRUCT_TIMESPEC
 5815 @@ -10,10 +28,32 @@
 5816  	time_t	tv_sec;		/* seconds */
 5817  	long	tv_nsec;	/* nanoseconds */
 5818  };
 5819 +#define	CLOCK_REALTIME	0	/* currently a dummy */
 5820 +typedef int	clockid_t;	/* POSIX.4, currently dummy */
 5821  #endif /* _STRUCT_TIMESPEC */
 5822  
 5823  #ifdef __KERNEL__
 5824  
 5825 +/* elapsed timer interrupts since last processing */
 5826 +extern volatile unsigned long wall_jiffies;
 5827 +
 5828 +/* Warning: SHIFT_HZ should no longer be used; it's obsolete now! [UW] */
 5829 +#if HZ >= 24 && HZ < 48
 5830 +# define SHIFT_HZ	5
 5831 +#elif HZ >= 48 && HZ < 96
 5832 +# define SHIFT_HZ	6
 5833 +#elif HZ >= 96 && HZ < 192
 5834 +# define SHIFT_HZ	7
 5835 +#elif HZ >= 192 && HZ < 384
 5836 +# define SHIFT_HZ	8
 5837 +#elif HZ >= 384 && HZ < 768
 5838 +# define SHIFT_HZ	9
 5839 +#elif HZ >= 768 && HZ < 1536
 5840 +# define SHIFT_HZ	10
 5841 +#else
 5842 +# error You lose.
 5843 +#endif
 5844 +
 5845  /*
 5846   * Change timeval to jiffies, trying to avoid the
 5847   * most obvious overflows..
 5848 @@ -48,40 +88,6 @@
 5849  	value->tv_sec = jiffies / HZ;
 5850  }
 5851  
 5852 -
 5853 -/* Converts Gregorian date to seconds since 1970-01-01 00:00:00.
 5854 - * Assumes input in normal date format, i.e. 1980-12-31 23:59:59
 5855 - * => year=1980, mon=12, day=31, hour=23, min=59, sec=59.
 5856 - *
 5857 - * [For the Julian calendar (which was used in Russia before 1917,
 5858 - * Britain & colonies before 1752, anywhere else before 1582,
 5859 - * and is still in use by some communities) leave out the
 5860 - * -year/100+year/400 terms, and add 10.]
 5861 - *
 5862 - * This algorithm was first published by Gauss (I think).
 5863 - *
 5864 - * WARNING: this function will overflow on 2106-02-07 06:28:16 on
 5865 - * machines were long is 32-bit! (However, as time_t is signed, we
 5866 - * will already get problems at other places on 2038-01-19 03:14:08)
 5867 - */
 5868 -static inline unsigned long
 5869 -mktime (unsigned int year, unsigned int mon,
 5870 -	unsigned int day, unsigned int hour,
 5871 -	unsigned int min, unsigned int sec)
 5872 -{
 5873 -	if (0 >= (int) (mon -= 2)) {	/* 1..12 -> 11,12,1..10 */
 5874 -		mon += 12;		/* Puts Feb last since it has leap day */
 5875 -		year -= 1;
 5876 -	}
 5877 -
 5878 -	return (((
 5879 -		(unsigned long) (year/4 - year/100 + year/400 + 367*mon/12 + day) +
 5880 -			year*365 - 719499
 5881 -	    )*24 + hour /* now have hours */
 5882 -	  )*60 + min /* now have minutes */
 5883 -	)*60 + sec; /* finally seconds */
 5884 -}
 5885 -
 5886  #endif /* __KERNEL__ */
 5887  
 5888  
 5889 @@ -98,8 +104,46 @@
 5890  #define NFDBITS			__NFDBITS
 5891  
 5892  #ifdef __KERNEL__
 5893 -extern void do_gettimeofday(struct timeval *tv);
 5894 -extern void do_settimeofday(struct timeval *tv);
 5895 +/* The current time */
 5896 +extern	volatile struct timespec xtime;
 5897 +#define CURRENT_TIME (xtime.tv_sec)
 5898 +
 5899 +/*
 5900 + * This spinlock protect us from races in SMP while playing with xtime. -arca
 5901 + */
 5902 +extern rwlock_t xtime_lock;
 5903 +
 5904 +/* kernel time variables */
 5905 +extern long time_tick;				/* time increment (ns) */
 5906 +extern int rtc_update_slave;			/* see kernel/time.c */
 5907 +
 5908 +/* Gregorian date & time as UNIX time */
 5909 +extern unsigned long mktime(unsigned int year, unsigned int mon,
 5910 +			    unsigned int day, unsigned int hour,
 5911 +			    unsigned int min, unsigned int sec);
 5912 +/* kernel time as Gregorian components to update RTC, see kernel/time.c */
 5913 +extern void ktime_to_rtc(time_t t, int *hhmmss, int *yyyymmdd);
 5914 +/* RTC time as kernel time (uses mktime). */
 5915 +time_t rtc_to_ktime(unsigned int year, unsigned int mon,
 5916 +		    unsigned int day, unsigned int hour,
 5917 +		    unsigned int min, unsigned int sec);
 5918 +
 5919 +extern void update_wall_time(unsigned long ticks);
 5920 +/* nanoseconds since last timer interrupt (hook) */
 5921 +extern unsigned long (*do_nanotime)(void);
 5922 +
 5923 +/* compensate a systematic clockerror of `freq' / 1000 PPM */
 5924 +extern	void update_nanoscale(long freq);
 5925 +
 5926 +extern void do_gettimeofday(struct timeval *tvp);
 5927 +extern void do_settimeofday(const struct timeval *tvp);
 5928 +extern unsigned long do_clock_gettime(clockid_t sysclock, struct timespec *tsp);
 5929 +extern void do_clock_settime(clockid_t sysclock, const struct timespec *tsp);
 5930 +extern void do_clock_getres(clockid_t sysclock, struct timespec *tsp);
 5931 +#ifdef CONFIG_NTP_PPS	/* handle PPS pulse */
 5932 +extern void hardpps(const struct timespec *p_ts, long nsec);
 5933 +#endif
 5934 +extern void timevar_init(void);		/* initialize kernel time variables */
 5935  #endif
 5936  
 5937  #define FD_SETSIZE		__FD_SETSIZE
 5938 Index: linux/include/linux/timepps.h
 5939 diff -u /dev/null linux/include/linux/timepps.h:1.2.16.2
 5940 --- /dev/null	Sat Aug 26 22:16:05 2006
 5941 +++ linux/include/linux/timepps.h	Sat Aug 26 21:53:24 2006
 5942 @@ -0,0 +1,230 @@
 5943 +/* Interface to the PPS API described in RFC 2783 (March 2000)
 5944 + *
 5945 + * Copyright (c) 1999, 2001 by Ulrich Windl,
 5946 + * 	based on code by Reg Clemens <reg@dwf.com>
 5947 + *		based on code by Poul-Henning Kamp <phk@FreeBSD.org>
 5948 + * ----------------------------------------------------------------------------
 5949 + * "THE BEER-WARE LICENSE" (Revision 42):
 5950 + * <phk@FreeBSD.org> wrote this file.  As long as you retain this notice you
 5951 + * can do whatever you want with this stuff. If we meet some day, and you think
 5952 + * this stuff is worth it, you can buy me a beer in return.   Poul-Henning Kamp
 5953 + * ---------------------------------------------------------------------------- */
 5954 +
 5955 +#ifndef _SYS_TIMEPPS_H_
 5956 +#define _SYS_TIMEPPS_H_
 5957 +
 5958 +/* Implementation note: the logical states ``assert'' and ``clear''
 5959 + * are implemented in terms of the UART register, i.e. ``assert''
 5960 + * means the bit is set.
 5961 + */
 5962 +
 5963 +/*---- 3.2 New data structures ----*/
 5964 +#define PPS_API_VERS_1	1		/* draft-05, dated 1999-08 */
 5965 +
 5966 +typedef int pps_handle_t;	 	/* represents a PPS source */
 5967 +
 5968 +typedef unsigned long pps_seq_t;	/* sequence number */
 5969 +
 5970 +typedef struct ntp_fp {
 5971 +	unsigned int	integral;
 5972 +	unsigned int	fractional;
 5973 +} ntp_fp_t;				/* NTP-compatible time stamp */
 5974 +
 5975 +typedef union pps_timeu {
 5976 +	struct timespec	tspec;
 5977 +	ntp_fp_t	ntpfp;
 5978 +	unsigned long	longpad[3];
 5979 +} pps_timeu_t;		/* generic data type to represent time stamps */
 5980 +
 5981 +typedef struct pps_info {
 5982 +	pps_seq_t	assert_sequence;	/* seq. num. of assert event */
 5983 +	pps_seq_t	clear_sequence;		/* seq. num. of clear event */
 5984 +	pps_timeu_t	assert_tu;		/* time of assert event */
 5985 +	pps_timeu_t	clear_tu;		/* time of clear event */
 5986 +	int		current_mode;		/* current mode bits */
 5987 +} pps_info_t;
 5988 +
 5989 +#define assert_timestamp        assert_tu.tspec
 5990 +#define clear_timestamp         clear_tu.tspec
 5991 +
 5992 +#define assert_timestamp_ntpfp  assert_tu.ntpfp
 5993 +#define clear_timestamp_ntpfp   clear_tu.ntpfp
 5994 +
 5995 +typedef struct pps_params {
 5996 +	int		api_version;	/* API version # */
 5997 +	int		mode;		/* mode bits */
 5998 +	pps_timeu_t assert_off_tu;	/* offset compensation for assert */
 5999 +	pps_timeu_t clear_off_tu;	/* offset compensation for clear */
 6000 +} pps_params_t;
 6001 +
 6002 +#define assert_offset   assert_off_tu.tspec
 6003 +#define clear_offset    clear_off_tu.tspec
 6004 +
 6005 +#define assert_offset_ntpfp     assert_off_tu.ntpfp
 6006 +#define clear_offset_ntpfp      clear_off_tu.ntpfp
 6007 +
 6008 +/*---- 3.3 Mode bit definitions ----*/
 6009 +/*-- Device/implementation parameters --*/
 6010 +#define PPS_CAPTUREASSERT	0x01	/* capture assert events */
 6011 +#define PPS_CAPTURECLEAR	0x02	/* capture clear events */
 6012 +#define PPS_CAPTUREBOTH		0x03	/* capture assert and clear events */
 6013 +
 6014 +#define PPS_OFFSETASSERT	0x10	/* apply compensation for assert ev. */
 6015 +#define PPS_OFFSETCLEAR		0x20	/* apply compensation for clear ev. */
 6016 +
 6017 +#define PPS_CANWAIT		0x100	/* Can we wait for an event? */
 6018 +#define PPS_CANPOLL		0x200	/* "This bit is reserved for
 6019 +                                           future use." */
 6020 +
 6021 +/*-- Kernel actions --*/
 6022 +#define PPS_ECHOASSERT		0x40	/* feed back assert event to output */
 6023 +#define PPS_ECHOCLEAR		0x80	/* feed back clear event to output */
 6024 +
 6025 +/*-- Timestamp formats --*/
 6026 +#define PPS_TSFMT_TSPEC		0x1000	/* select timespec format */
 6027 +#define PPS_TSFMT_NTPFP		0x2000	/* select NTP format */
 6028 +
 6029 +/*---- 3.4.4 New functions: disciplining the kernel timebase ----*/
 6030 +/*-- Kernel consumers --*/
 6031 +#define PPS_KC_HARDPPS		0	/* hardpps() (or equivalent) */
 6032 +#define PPS_KC_HARDPPS_PLL	1	/* hardpps() constrained to
 6033 +					   use a phase-locked loop */
 6034 +#define PPS_KC_HARDPPS_FLL	2	/* hardpps() constrained to
 6035 +					   use a frequency-locked loop */
 6036 +
 6037 +/*------ Here begins the implementation-specific part! ------*/
 6038 +struct pps_fetch_args {
 6039 +	int		tsformat;
 6040 +	struct timespec	timeout;
 6041 +	pps_info_t	pps_info_buf;
 6042 +};
 6043 +
 6044 +struct pps_bind_args {
 6045 +	int		tsformat;	/* format of time stamps */
 6046 +	int		edge;		/* selected event type */
 6047 +	int		consumer;	/* selected kernel consumer */
 6048 +};
 6049 +
 6050 +/* check Documentation/ioctl-number.txt! */
 6051 +#define PPS_IOC_CREATE		_IO('1', 1)
 6052 +#define PPS_IOC_DESTROY		_IO('1', 2)
 6053 +#define PPS_IOC_SETPARMS	_IOW('1', 3, pps_params_t)
 6054 +#define PPS_IOC_GETPARMS	_IOR('1', 4, pps_params_t)
 6055 +#define PPS_IOC_GETCAP		_IOR('1', 5, int)
 6056 +#define PPS_IOC_FETCH		_IOWR('1', 6, struct pps_fetch_args)
 6057 +#define PPS_IOC_KC_BIND		_IOW('1', 7, struct pps_bind_args)
 6058 +
 6059 +#ifdef __KERNEL__
 6060 +
 6061 +#include <linux/ioctl.h>
 6062 +
 6063 +#define PPSCLOCK_MAGIC 0x5003	/* check Documentation/magic-number.txt! */
 6064 +
 6065 +struct pps_state {
 6066 +	pps_params_t	parm;		/* PPS parameters */
 6067 +	pps_info_t	info;		/* PPS information */
 6068 +	int		cap;		/* PPS capabilities */
 6069 +	long		ecount;		/* interpolation offset of event */
 6070 +	struct timespec	etime;		/* kernel time of event */
 6071 +	wait_queue_head_t ewait;	/* wait queue for event */
 6072 +};
 6073 +
 6074 +struct pps {
 6075 +	int     magic;			/* magic number */
 6076 +	struct  pps_state       state;	/* data */
 6077 +};
 6078 +
 6079 +/* state variables to bind kernel consumer */
 6080 +/* PPS API (RFC 2783): current source and mode for ``kernel consumer'' */
 6081 +extern	void	*pps_kc_hardpps_dev;	/* some unique pointer to device */
 6082 +extern	int	pps_kc_hardpps_mode;	/* mode bits for kernel consumer */
 6083 +
 6084 +#else /* !KERNEL */
 6085 +
 6086 +#include	<sys/ioctl.h>
 6087 +
 6088 +/*---- 3.4 Functions ----*/
 6089 +
 6090 +/* create PPS handle from file descriptor */
 6091 +static __inline int time_pps_create(int filedes, pps_handle_t *handle)
 6092 +{
 6093 +	int error;
 6094 +
 6095 +	error = ioctl(filedes, PPS_IOC_CREATE, 0);
 6096 +	if (error < 0) {
 6097 +		*handle = -1;
 6098 +		return (-1);
 6099 +	}
 6100 +	*handle = filedes;
 6101 +	return (0);
 6102 +}
 6103 +
 6104 +/* release PPS handle */
 6105 +static __inline int time_pps_destroy(pps_handle_t handle)
 6106 +{
 6107 +	return (ioctl(handle, PPS_IOC_DESTROY, 0));
 6108 +}
 6109 +
 6110 +/* set parameters for handle */
 6111 +static __inline int time_pps_setparams(pps_handle_t handle,
 6112 +				       const pps_params_t *ppsparams)
 6113 +{
 6114 +	if (ppsparams->api_version != PPS_API_VERS_1) {
 6115 +		/* This is ugly, but there was no reasonable consensus
 6116 +                   in the API working group.  I require
 6117 +                   ``api_version'' to be set!
 6118 +		*/
 6119 +		((pps_params_t *) ppsparams)->api_version = PPS_API_VERS_1;
 6120 +	}
 6121 +
 6122 +	return (ioctl(handle, PPS_IOC_SETPARMS, ppsparams));
 6123 +}
 6124 +
 6125 +/* get parameters for handle */
 6126 +static __inline int time_pps_getparams(pps_handle_t handle,
 6127 +				       pps_params_t *ppsparams)
 6128 +{
 6129 +	return (ioctl(handle, PPS_IOC_GETPARMS, ppsparams));
 6130 +}
 6131 +
 6132 +/* get capabilities for handle */
 6133 +static __inline int time_pps_getcap(pps_handle_t handle, int *mode)
 6134 +{
 6135 +	return (ioctl(handle, PPS_IOC_GETCAP, mode));
 6136 +}
 6137 +
 6138 +/* current event for handle */
 6139 +static __inline int time_pps_fetch(pps_handle_t handle, const int tsformat,
 6140 +				   pps_info_t *ppsinfobuf,
 6141 +				   const struct timespec *timeout)
 6142 +{
 6143 +	int error;
 6144 +	struct pps_fetch_args arg;
 6145 +
 6146 +	arg.tsformat = tsformat;
 6147 +	if (timeout)
 6148 +		arg.timeout = *timeout;
 6149 +	else	/* wait forever */
 6150 +		arg.timeout.tv_sec = arg.timeout.tv_nsec = -1;
 6151 +	error = ioctl(handle, PPS_IOC_FETCH, &arg);
 6152 +	*ppsinfobuf = arg.pps_info_buf;
 6153 +	return (error);
 6154 +}
 6155 +
 6156 +/* specify kernel consumer */
 6157 +static __inline int time_pps_kcbind(pps_handle_t handle,
 6158 +				    const int kernel_consumer,
 6159 +				    const int edge, const int tsformat)
 6160 +{
 6161 +	int error;
 6162 +	struct pps_bind_args arg;
 6163 +
 6164 +	arg.tsformat = tsformat;
 6165 +	arg.edge = edge;
 6166 +	arg.consumer = kernel_consumer;
 6167 +	error = ioctl(handle, PPS_IOC_KC_BIND, &arg);
 6168 +	return(error);
 6169 +}
 6170 +
 6171 +#endif /* !KERNEL */
 6172 +#endif /* _SYS_TIMEPPS_H_ */
 6173 Index: linux/include/linux/timex.h
 6174 diff -u linux/include/linux/timex.h:1.1.1.5 linux/include/linux/timex.h:1.1.1.5.2.1
 6175 --- linux/include/linux/timex.h:1.1.1.5	Sat Aug 26 21:35:45 2006
 6176 +++ linux/include/linux/timex.h	Sat Aug 26 21:50:43 2006
 6177 @@ -1,28 +1,54 @@
 6178 -/*****************************************************************************
 6179 - *                                                                           *
 6180 - * Copyright (c) David L. Mills 1993                                         *
 6181 - *                                                                           *
 6182 - * Permission to use, copy, modify, and distribute this software and its     *
 6183 - * documentation for any purpose and without fee is hereby granted, provided *
 6184 - * that the above copyright notice appears in all copies and that both the   *
 6185 - * copyright notice and this permission notice appear in supporting          *
 6186 - * documentation, and that the name University of Delaware not be used in    *
 6187 - * advertising or publicity pertaining to distribution of the software       *
 6188 - * without specific, written prior permission.  The University of Delaware   *
 6189 - * makes no representations about the suitability this software for any      *
 6190 - * purpose.  It is provided "as is" without express or implied warranty.     *
 6191 - *                                                                           *
 6192 - *****************************************************************************/
 6193 +/***********************************************************************
 6194 + *								       *
 6195 + * Copyright (c) David L. Mills 1993-2000			       *
 6196 + *								       *
 6197 + * Permission to use, copy, modify, and distribute this software and   *
 6198 + * its documentation for any purpose and without fee is hereby	       *
 6199 + * granted, provided that the above copyright notice appears in all    *
 6200 + * copies and that both the copyright notice and this permission       *
 6201 + * notice appear in supporting documentation, and that the name        *
 6202 + * University of Delaware not be used in advertising or publicity      *
 6203 + * pertaining to distribution of the software without specific,	       *
 6204 + * written prior permission. The University of Delaware makes no       *
 6205 + * representations about the suitability this software for any	       *
 6206 + * purpose. It is provided "as is" without express or implied	       *
 6207 + * warranty.							       *
 6208 + *								       *
 6209 + **********************************************************************/
 6210  
 6211  /*
 6212   * Modification history timex.h
 6213   *
 6214 - * 29 Dec 97	Russell King
 6215 + *---- BEGIN Linux specific changes (latest on top)
 6216 + * 2000-10-14	Ulrich Windl
 6217 + *	Add declaration for ``adjtimex()''.
 6218 + * 1999-11-11	Ulrich Windl
 6219 + *	Updated for ``nanokernel-19990829'': Introduced ``NTP_API'',
 6220 + *	``MOD_PPSMAX''.  Dropped ``MOD_PLL'' and ``MOD_FLL''.  The old ``NANO''
 6221 + *	is named ``NTP_NANO'' now.  ``NTP_PHASE_LIMIT'' is nanoseconds now.
 6222 + * 1999-02-26	Ulrich Windl
 6223 + *	Updated file for new ``nanokernel'' preferred for NTPv4.  Tried to
 6224 + *	keep very close to Dave Mills' code.  Changed `tick' to `time_tick'
 6225 + *	(ns now).  Adjusted several other definitions.
 6226 + * 1999-01-24	Ulrich Windl
 6227 + *	Activated new field ``tickadj''
 6228 + * 1997-08-30	Ulrich Windl
 6229 + *	Added new constant NTP_PHASE_LIMIT
 6230 + * 1997-12-29	Russell King
 6231   *	Moved CLOCK_TICK_RATE, CLOCK_TICK_FACTOR and FINETUNE to asm/timex.h
 6232   *	for ARM machines
 6233 + * 1995-08-13	Torsten Duwe
 6234 + *	kernel PLL updated to 1994-12-13 specs (RFC-1589)
 6235 + * 1993-10-07	Torsten Duwe
 6236 + *	Derived linux/timex.h
 6237 + *---- END Linux specific changes
 6238 + *
 6239 + * 16 Aug 00	David L. Mills
 6240 + *	API Version 4. Added MOD_TAI and tai member of ntptimeval
 6241 + *	structure.
 6242   *
 6243 - *  9 Jan 97    Adrian Sun
 6244 - *      Shifted LATCH define to allow access to alpha machines.
 6245 + * 17 Nov 98	David L. Mills
 6246 + *	Revised for nanosecond kernel and user interface.
 6247   *
 6248   * 26 Sep 94	David L. Mills
 6249   *	Added defines for hybrid phase/frequency-lock loop.
 6250 @@ -40,175 +66,103 @@
 6251   *	interval.
 6252   *
 6253   * 17 Sep 93    David L. Mills
 6254 - *      Created file $NTP/include/sys/timex.h
 6255 - * 07 Oct 93    Torsten Duwe
 6256 - *      Derived linux/timex.h
 6257 - * 1995-08-13    Torsten Duwe
 6258 - *      kernel PLL updated to 1994-12-13 specs (rfc-1589)
 6259 - * 1997-08-30    Ulrich Windl
 6260 - *      Added new constant NTP_PHASE_LIMIT
 6261 + *      Created file
 6262   */
 6263 -#ifndef _LINUX_TIMEX_H
 6264 -#define _LINUX_TIMEX_H
 6265 -
 6266 -#include <asm/param.h>
 6267 -
 6268  /*
 6269 - * The following defines establish the engineering parameters of the PLL
 6270 - * model. The HZ variable establishes the timer interrupt frequency, 100 Hz
 6271 - * for the SunOS kernel, 256 Hz for the Ultrix kernel and 1024 Hz for the
 6272 - * OSF/1 kernel. The SHIFT_HZ define expresses the same value as the
 6273 - * nearest power of two in order to avoid hardware multiply operations.
 6274 - */
 6275 -#if HZ >= 12 && HZ < 24
 6276 -# define SHIFT_HZ	4
 6277 -#elif HZ >= 24 && HZ < 48
 6278 -# define SHIFT_HZ	5
 6279 -#elif HZ >= 48 && HZ < 96
 6280 -# define SHIFT_HZ	6
 6281 -#elif HZ >= 96 && HZ < 192
 6282 -# define SHIFT_HZ	7
 6283 -#elif HZ >= 192 && HZ < 384
 6284 -# define SHIFT_HZ	8
 6285 -#elif HZ >= 384 && HZ < 768
 6286 -# define SHIFT_HZ	9
 6287 -#elif HZ >= 768 && HZ < 1536
 6288 -# define SHIFT_HZ	10
 6289 -#else
 6290 -# error You lose.
 6291 -#endif
 6292 -
 6293 -/*
 6294 - * SHIFT_KG and SHIFT_KF establish the damping of the PLL and are chosen
 6295 - * for a slightly underdamped convergence characteristic. SHIFT_KH
 6296 - * establishes the damping of the FLL and is chosen by wisdom and black
 6297 - * art.
 6298 - *
 6299 - * MAXTC establishes the maximum time constant of the PLL. With the
 6300 - * SHIFT_KG and SHIFT_KF values given and a time constant range from
 6301 - * zero to MAXTC, the PLL will converge in 15 minutes to 16 hours,
 6302 - * respectively.
 6303 - */
 6304 -#define SHIFT_KG 6		/* phase factor (shift) */
 6305 -#define SHIFT_KF 16		/* PLL frequency factor (shift) */
 6306 -#define SHIFT_KH 2		/* FLL frequency factor (shift) */
 6307 -#define MAXTC 6			/* maximum time constant (shift) */
 6308 -
 6309 -/*
 6310 - * The SHIFT_SCALE define establishes the decimal point of the time_phase
 6311 - * variable which serves as an extension to the low-order bits of the
 6312 - * system clock variable. The SHIFT_UPDATE define establishes the decimal
 6313 - * point of the time_offset variable which represents the current offset
 6314 - * with respect to standard time. The FINEUSEC define represents 1 usec in
 6315 - * scaled units.
 6316 - *
 6317 - * SHIFT_USEC defines the scaling (shift) of the time_freq and
 6318 - * time_tolerance variables, which represent the current frequency
 6319 - * offset and maximum frequency tolerance.
 6320 - *
 6321 - * FINEUSEC is 1 us in SHIFT_UPDATE units of the time_phase variable.
 6322 - */
 6323 -#define SHIFT_SCALE 22		/* phase scale (shift) */
 6324 -#define SHIFT_UPDATE (SHIFT_KG + MAXTC) /* time offset scale (shift) */
 6325 -#define SHIFT_USEC 16		/* frequency offset scale (shift) */
 6326 -#define FINEUSEC (1L << SHIFT_SCALE) /* 1 us in phase units */
 6327 -
 6328 -#define MAXPHASE 512000L        /* max phase error (us) */
 6329 -#define MAXFREQ (512L << SHIFT_USEC)  /* max frequency error (ppm) */
 6330 -#define MAXTIME (200L << PPS_AVG) /* max PPS error (jitter) (200 us) */
 6331 -#define MINSEC 16L              /* min interval between updates (s) */
 6332 -#define MAXSEC 1200L            /* max interval between updates (s) */
 6333 -#define	NTP_PHASE_LIMIT	(MAXPHASE << 5)	/* beyond max. dispersion */
 6334 -
 6335 -/*
 6336 - * The following defines are used only if a pulse-per-second (PPS)
 6337 - * signal is available and connected via a modem control lead, such as
 6338 - * produced by the optional ppsclock feature incorporated in the Sun
 6339 - * asynch driver. They establish the design parameters of the frequency-
 6340 - * lock loop used to discipline the CPU clock oscillator to the PPS
 6341 - * signal.
 6342 - *
 6343 - * PPS_AVG is the averaging factor for the frequency loop, as well as
 6344 - * the time and frequency dispersion.
 6345 - *
 6346 - * PPS_SHIFT and PPS_SHIFTMAX specify the minimum and maximum
 6347 - * calibration intervals, respectively, in seconds as a power of two.
 6348 - *
 6349 - * PPS_VALID is the maximum interval before the PPS signal is considered
 6350 - * invalid and protocol updates used directly instead.
 6351 - *
 6352 - * MAXGLITCH is the maximum interval before a time offset of more than
 6353 - * MAXTIME is believed.
 6354 - */
 6355 -#define PPS_AVG 2		/* pps averaging constant (shift) */
 6356 -#define PPS_SHIFT 2		/* min interval duration (s) (shift) */
 6357 -#define PPS_SHIFTMAX 8		/* max interval duration (s) (shift) */
 6358 -#define PPS_VALID 120		/* pps signal watchdog max (s) */
 6359 -#define MAXGLITCH 30		/* pps signal glitch max (s) */
 6360 -
 6361 -/*
 6362 - * Pick up the architecture specific timex specifications
 6363 + * This header file defines the Network Time Protocol (NTP) interfaces
 6364 + * for user and daemon application programs. These are implemented using
 6365 + * defined syscalls and data structures and require specific kernel
 6366 + * support.
 6367 + *
 6368 + * The original precision time kernels developed from 1993 have an
 6369 + * ultimate resolution of one microsecond; however, the most recent
 6370 + * kernels have an ultimate resolution of one nanosecond. In these
 6371 + * kernels, a ntp_adjtime() syscalls can be used to determine which
 6372 + * resolution is in use and to select either one at any time. The
 6373 + * resolution selected affects the scaling of certain fields in the
 6374 + * ntp_gettime() and ntp_adjtime() syscalls, as described below.
 6375 + *
 6376 + * NAME
 6377 + *	ntp_gettime - NTP user application interface
 6378 + *
 6379 + * SYNOPSIS
 6380 + *	#include <sys/timex.h>
 6381 + *
 6382 + *	int ntp_gettime(struct ntptimeval *tptr);
 6383 + *
 6384 + * DESCRIPTION
 6385 + *	The time returned by ntp_gettime() is in a timeval structure,
 6386 + *	but may be in either microsecond (seconds and microseconds) or
 6387 + *	nanosecond (seconds and nanoseconds) format. The particular
 6388 + *	format in use is determined by the STA_NANO bit of the status
 6389 + *	word returned by the ntp_adjtime() syscall.
 6390 + *
 6391 + * NAME
 6392 + *	ntp_adjtime - NTP daemon application interface
 6393 + *
 6394 + * SYNOPSIS
 6395 + *	#include <sys/timex.h>
 6396 + *
 6397 + *	int ntp_adjtime(struct timex *tptr);
 6398 + *
 6399 + * DESCRIPTION
 6400 + *	Certain fields of the timex structure are interpreted in either
 6401 + *	microseconds or nanoseconds according to the state of the
 6402 + *	STA_NANO bit in the status word. See the description below for
 6403 + *	further information.
 6404   */
 6405 -#include <asm/timex.h>
 6406 -
 6407 -/* LATCH is used in the interval timer and ftape setup. */
 6408 -#define LATCH  ((CLOCK_TICK_RATE + HZ/2) / HZ)	/* For divider */
 6409 -
 6410 -/*
 6411 - * syscall interface - used (mainly by NTP daemon)
 6412 - * to discipline kernel clock oscillator
 6413 - */
 6414 -struct timex {
 6415 -	unsigned int modes;	/* mode selector */
 6416 -	long offset;		/* time offset (usec) */
 6417 -	long freq;		/* frequency offset (scaled ppm) */
 6418 -	long maxerror;		/* maximum error (usec) */
 6419 -	long esterror;		/* estimated error (usec) */
 6420 -	int status;		/* clock command/status */
 6421 -	long constant;		/* pll time constant */
 6422 -	long precision;		/* clock precision (usec) (read only) */
 6423 -	long tolerance;		/* clock frequency tolerance (ppm)
 6424 -				 * (read only)
 6425 -				 */
 6426 -	struct timeval time;	/* (read only) */
 6427 -	long tick;		/* (modified) usecs between clock ticks */
 6428 -
 6429 -	long ppsfreq;           /* pps frequency (scaled ppm) (ro) */
 6430 -	long jitter;            /* pps jitter (us) (ro) */
 6431 -	int shift;              /* interval duration (s) (shift) (ro) */
 6432 -	long stabil;            /* pps stability (scaled ppm) (ro) */
 6433 -	long jitcnt;            /* jitter limit exceeded (ro) */
 6434 -	long calcnt;            /* calibration intervals (ro) */
 6435 -	long errcnt;            /* calibration errors (ro) */
 6436 -	long stbcnt;            /* stability limit exceeded (ro) */
 6437 -
 6438 -	int  :32; int  :32; int  :32; int  :32;
 6439 -	int  :32; int  :32; int  :32; int  :32;
 6440 -	int  :32; int  :32; int  :32; int  :32;
 6441 -};
 6442 +#ifndef _LINUX_TIMEX_H
 6443 +#define _LINUX_TIMEX_H
 6444  
 6445 +#define	NTP_API		4	/* NTP API version */
 6446  /*
 6447 - * Mode codes (timex.mode)
 6448 - */
 6449 -#define ADJ_OFFSET		0x0001	/* time offset */
 6450 -#define ADJ_FREQUENCY		0x0002	/* frequency offset */
 6451 -#define ADJ_MAXERROR		0x0004	/* maximum time error */
 6452 -#define ADJ_ESTERROR		0x0008	/* estimated time error */
 6453 -#define ADJ_STATUS		0x0010	/* clock status */
 6454 -#define ADJ_TIMECONST		0x0020	/* pll time constant */
 6455 -#define ADJ_TICK		0x4000	/* tick value */
 6456 -#define ADJ_OFFSET_SINGLESHOT	0x8001	/* old-fashioned adjtime */
 6457 -
 6458 -/* xntp 3.4 compatibility names */
 6459 -#define MOD_OFFSET	ADJ_OFFSET
 6460 -#define MOD_FREQUENCY	ADJ_FREQUENCY
 6461 -#define MOD_MAXERROR	ADJ_MAXERROR
 6462 -#define MOD_ESTERROR	ADJ_ESTERROR
 6463 -#define MOD_STATUS	ADJ_STATUS
 6464 -#define MOD_TIMECONST	ADJ_TIMECONST
 6465 -#define MOD_CLKB	ADJ_TICK
 6466 -#define MOD_CLKA	ADJ_OFFSET_SINGLESHOT /* 0x8000 in original */
 6467 -
 6468 + * The following defines establish the performance envelope of the
 6469 + * kernel discipline loop. Phase or frequency errors greater than
 6470 + * NAXPHASE or MAXFREQ are clamped to these maxima. For update intervals
 6471 + * less than MINSEC, the loop always operates in PLL mode; while, for
 6472 + * update intervals greater than MAXSEC, the loop always operates in FLL
 6473 + * mode. Between these two limits the operating mode is selected by the
 6474 + * STA_FLL bit in the status word.
 6475 + */
 6476 +#define MAXPHASE	500000000L /* max phase error (ns) */
 6477 +#define MAXFREQ		500000L	/* max freq error (ns/s) */
 6478 +#define MINSEC		256	/* min FLL update interval (s) */
 6479 +#define MAXSEC		2048	/* max PLL update interval (s) */
 6480 +#define NANOSECOND	1000000000L /* nanoseconds in one second */
 6481 +#define SCALE_PPM	(65536 / 1000) /* crude ns/s to scaled PPM */
 6482 +#define MAXTC		10	/* max time constant */
 6483 +
 6484 +/*
 6485 + * The following defines and structures define the user interface for
 6486 + * the ntp_gettime() and ntp_adjtime() syscalls.
 6487 + *
 6488 + * Control mode codes (timex.modes)
 6489 + */
 6490 +#define MOD_OFFSET	0x0001	/* set time offset */
 6491 +#define MOD_FREQUENCY	0x0002	/* set frequency offset */
 6492 +#define MOD_MAXERROR	0x0004	/* set maximum time error */
 6493 +#define MOD_ESTERROR	0x0008	/* set estimated time error */
 6494 +#define MOD_STATUS	0x0010	/* set clock status bits */
 6495 +#define MOD_TIMECONST	0x0020	/* set PLL time constant */
 6496 +#define MOD_PPSMAX	0x0040	/* set PPS maximum averaging interval */
 6497 +#define MOD_TAI		0x0080	/* set TAI offset */
 6498 +#define	MOD_MICRO	0x1000	/* select microsecond resolution */
 6499 +#define	MOD_NANO	0x2000	/* select nanosecond resolution */
 6500 +#define MOD_CLKB	0x4000	/* select clock B */
 6501 +#define MOD_CLKA	0x8000	/* select clock A */
 6502 +
 6503 +/*
 6504 + * Here are the additional bits for adjtimex().  They all start with
 6505 + * ``ADJ_''.  Some of these bits were different in the old adjtimex(), thus
 6506 + * not all old binaries will work!!!
 6507 + * We've learned the lesson and allocate bits from the other end now!
 6508 + * As the `ADJ_OFFSET_SINGLESHOT' is the candidate that will most likely
 6509 + * cause trouble, take the chance and replace that ugly name with
 6510 + * `ADJ_ADJTIME'.
 6511 + */
 6512 +#define ADJ_ADJTIME	0x80000000	/* plain old adjtime() */
 6513 +#define ADJ_TIMETICK	0x40000000	/* set new value for  `time_tick' */
 6514 +#define ADJ_TICKADJ	0x20000000	/* set new value for `tickadj' */
 6515  
 6516  /*
 6517   * Status codes (timex.status)
 6518 @@ -216,7 +170,7 @@
 6519  #define STA_PLL		0x0001	/* enable PLL updates (rw) */
 6520  #define STA_PPSFREQ	0x0002	/* enable PPS freq discipline (rw) */
 6521  #define STA_PPSTIME	0x0004	/* enable PPS time discipline (rw) */
 6522 -#define STA_FLL		0x0008	/* select frequency-lock mode (rw) */
 6523 +#define STA_FLL		0x0008	/* enable FLL mode (rw) */
 6524  
 6525  #define STA_INS		0x0010	/* insert leap (rw) */
 6526  #define STA_DEL		0x0020	/* delete leap (rw) */
 6527 @@ -229,62 +183,173 @@
 6528  #define STA_PPSERROR	0x0800	/* PPS signal calibration error (ro) */
 6529  
 6530  #define STA_CLOCKERR	0x1000	/* clock hardware fault (ro) */
 6531 +#define STA_NANO	0x2000	/* resolution (0 = us, 1 = ns) (ro) */
 6532 +#define STA_MODE	0x4000	/* mode (0 = PLL, 1 = FLL) (ro) */
 6533 +#define STA_CLK		0x8000	/* clock source (0 = A, 1 = B) (ro) */
 6534  
 6535  #define STA_RONLY (STA_PPSSIGNAL | STA_PPSJITTER | STA_PPSWANDER | \
 6536 -    STA_PPSERROR | STA_CLOCKERR) /* read-only bits */
 6537 +    STA_PPSERROR | STA_CLOCKERR | STA_NANO | STA_MODE | STA_CLK)
 6538  
 6539  /*
 6540   * Clock states (time_state)
 6541   */
 6542 -#define TIME_OK		0	/* clock synchronized, no leap second */
 6543 -#define TIME_INS	1	/* insert leap second */
 6544 -#define TIME_DEL	2	/* delete leap second */
 6545 +#define TIME_OK		0	/* no leap second warning */
 6546 +#define TIME_INS	1	/* insert leap second warning */
 6547 +#define TIME_DEL	2	/* delete leap second warning */
 6548  #define TIME_OOP	3	/* leap second in progress */
 6549  #define TIME_WAIT	4	/* leap second has occurred */
 6550 -#define TIME_ERROR	5	/* clock not synchronized */
 6551 -#define TIME_BAD	TIME_ERROR /* bw compat */
 6552 +#define TIME_ERROR	5	/* error (see status word) */
 6553 +
 6554 +/*
 6555 + * NTP user interface (ntp_gettime()) - used to read kernel clock values
 6556 + *
 6557 + * Note: The time member is in microseconds if STA_NANO is zero and
 6558 + * nanoseconds if not.  Depending on whether ``NANO'' is defined when
 6559 + * including this file, the same memory location can be accessed under two
 6560 + * different names.  ``STA_NANO'' at runtime should correspond with ``NANO''
 6561 + * at compilation time.
 6562 + */
 6563 +struct ntptimeval {
 6564 +#ifdef NTP_NANO
 6565 +	struct timespec time;	/* current time (ns) (ro) */
 6566 +#else
 6567 +	struct timeval time;	/* current time (us) (ro) */
 6568 +#endif /* NTP_NANO */
 6569 +	long maxerror;		/* maximum error (us) (ro) */
 6570 +	long esterror;		/* estimated error (us) (ro) */
 6571 +	long tai;		/* TAI offset */
 6572 +};
 6573  
 6574 +/*
 6575 + * NTP daemon interface (ntp_adjtime()) - used to discipline CPU clock
 6576 + * oscillator and determine status.
 6577 + *
 6578 + * Note: The offset, precision and jitter members are in microseconds if
 6579 + * STA_NANO is zero and nanoseconds if not.
 6580 + */
 6581 +/*
 6582 + * In Linux ntp_gettime(), ntp_adjtime(), and adjtime() use a common
 6583 + * ``struct timex'' that is a superset of Dave Mills' structure.
 6584 + */
 6585 +struct timex {
 6586 +	unsigned int modes;	/* clock mode bits (wo) */
 6587 +	long	offset;		/* time offset (ns/us) (rw) */
 6588 +	long	freq;		/* frequency offset (scaled PPM) (rw) */
 6589 +	long	maxerror;	/* maximum error (us) (rw) */
 6590 +	long	esterror;	/* estimated error (us) (rw) */
 6591 +	int	status;		/* clock status bits (rw) */
 6592 +	long	constant;	/* poll interval (log2 s) (rw) */
 6593 +	long	precision;	/* clock precision (ns/us) (ro) */
 6594 +	long	tolerance;	/* clock frequency tolerance (scaled
 6595 +				 * PPM) (ro) */
 6596 +	/*----BEGIN Linux extensions (part 1) */
 6597 +	/* Depending on whether ``NTP_NANO'' is defined when including
 6598 +	 * this file, the same memory location can be accessed under
 6599 +	 * two different names.  ``STA_NANO'' at runtime should
 6600 +	 * correspond with ``NTP_NANO'' at compilation time.
 6601 +	 */
 6602 +#ifdef NTP_NANO
 6603 +	struct timespec time;	/* current time (ns) (ro) */
 6604 +#else
 6605 +	struct timeval time;	/* current time (us) (ro) */
 6606 +#endif /* NTP_NANO */
 6607 +	long	time_tick;	/* time increment per tick (?) (rw) */
 6608 +	/*----END Linux extensions (part 1) */
 6609 +	/*
 6610 +	 * The following read-only structure members are implemented
 6611 +	 * only if the PPS signal discipline is configured in the
 6612 +	 * kernel. They are included in all configurations to insure
 6613 +	 * portability.
 6614 +	 */
 6615 +	long	ppsfreq;	/* PPS frequency (scaled PPM) (ro) */
 6616 +	long	jitter;		/* PPS jitter (ns/us) (ro) */
 6617 +	int	shift;		/* interval duration (s) (shift) (ro) */
 6618 +	long	stabil;		/* PPS stability (scaled PPM) (ro) */
 6619 +	long	jitcnt;		/* jitter limit exceeded (ro) */
 6620 +	long	calcnt;		/* calibration intervals (ro) */
 6621 +	long	errcnt;		/* calibration errors (ro) */
 6622 +	long	stbcnt;		/* stability limit exceeded (ro) */
 6623 +	/*----BEGIN Linux extensions (part 2) */
 6624 +	/* first we reserve some space, then we add our new stuff */
 6625 +	/* Yes, it's ugly, but it's binary compatible */
 6626 +	long	tai;		/* TAI offset */
 6627 +	int  :32; int  :32; int  :32;
 6628 +	int	tickadj;	/* tickadj (us) (rw) -- extension by UW */
 6629 +	int  :32; int  :32; int  :32;
 6630 +	int  :32; int  :32; int  :32; int  :32;
 6631 +	/*----END Linux extensions (part 2) */
 6632 +};
 6633 +
 6634 +extern int adjtimex(struct timex *txc);	/* Linux-specific, try to avoid! */
 6635 +
 6636 +#if 0 /* This will break some libraries */
 6637 +#define	LINUX_PROVIDES_NTP_INTERFACE	/* but give the guys a chance */
 6638 +#define	ntp_adjtime	adjtimex	/* that one is easy */
 6639 +
 6640 +inline	int ntp_gettime(struct ntptimeval *ntv)
 6641 +{
 6642 +	struct timex	tntx;
 6643 +	int		result;
 6644 +	tntx.modes = 0;
 6645 +	result = __adjtimex (&tntx);
 6646 +	ntv->time = tntx.time;
 6647 +	ntv->maxerror = tntx.maxerror;
 6648 +	ntv->esterror = tntx.esterror;
 6649 +	return(result);
 6650 +}
 6651 +
 6652 +/* Adjust the current time of day by the amount in DELTA.  If OLDDELTA
 6653 + * is not NULL, it is filled in with the amount of time adjustment
 6654 + * remaining to be done from the last `adjtime' call.  This call is
 6655 + * restricted to the super-user.
 6656 + */
 6657 +/* This is a simple-minded implementation! */
 6658 +inline	int adjtime (const struct timeval *delta, struct timeval *olddelta)
 6659 +{
 6660 +	struct timex	tntx;
 6661 +	int		result;
 6662 +	if (delta) {
 6663 +		struct timeval tmp;
 6664 +
 6665 +		/* We enforce some arbitrary (2000s) limit here. */
 6666 +		tmp.tv_sec = delta->tv_sec + delta->tv_usec / 1000000L;
 6667 +		tmp.tv_usec = delta->tv_usec % 1000000L;
 6668 +		if (tmp.tv_sec > 2000 || tmp.tv_sec < -2000) {
 6669 +			return -1;
 6670 +		}
 6671 +		tntx.offset = tmp.tv_usec + tmp.tv_sec * 1000000L;
 6672 +		tntx.modes = ADJ_ADJTIME;
 6673 +	}
 6674 +	else
 6675 +		tntx.modes = 0;
 6676 +	result = __adjtimex (&tntx);
 6677 +	if (result >= 0 && olddelta) {
 6678 +		if (tntx.offset < 0) {
 6679 +			olddelta->tv_usec = -(-tntx.offset % 1000000);
 6680 +			olddelta->tv_sec  = -(-tntx.offset / 1000000);
 6681 +		} else {
 6682 +			olddelta->tv_usec = tntx.offset % 1000000;
 6683 +			olddelta->tv_sec  = tntx.offset / 1000000;
 6684 +		}
 6685 +	}
 6686 +	return result;
 6687 +}
 6688 +#endif
 6689  #ifdef __KERNEL__
 6690  /*
 6691 - * kernel variables
 6692 - * Note: maximum error = NTP synch distance = dispersion + delay / 2;
 6693 - * estimated error = NTP dispersion.
 6694 - */
 6695 -extern long tick;                      /* timer interrupt period */
 6696 -extern int tickadj;			/* amount of adjustment per tick */
 6697 -
 6698 -/*
 6699 - * phase-lock loop variables
 6700 - */
 6701 -extern int time_state;		/* clock status */
 6702 -extern int time_status;		/* clock synchronization status bits */
 6703 -extern long time_offset;	/* time adjustment (us) */
 6704 -extern long time_constant;	/* pll time constant */
 6705 -extern long time_tolerance;	/* frequency tolerance (ppm) */
 6706 -extern long time_precision;	/* clock precision (us) */
 6707 -extern long time_maxerror;	/* maximum error */
 6708 -extern long time_esterror;	/* estimated error */
 6709 -
 6710 -extern long time_phase;		/* phase offset (scaled us) */
 6711 -extern long time_freq;		/* frequency offset (scaled ppm) */
 6712 -extern long time_adj;		/* tick adjust (scaled 1 / HZ) */
 6713 -extern long time_reftime;	/* time at last adjustment (s) */
 6714 -
 6715 -extern long time_adjust;	/* The amount of adjtime left */
 6716 -
 6717 -/* interface variables pps->timer interrupt */
 6718 -extern long pps_offset;		/* pps time offset (us) */
 6719 -extern long pps_jitter;		/* time dispersion (jitter) (us) */
 6720 -extern long pps_freq;		/* frequency offset (scaled ppm) */
 6721 -extern long pps_stabil;		/* frequency dispersion (scaled ppm) */
 6722 -extern long pps_valid;		/* pps signal watchdog counter */
 6723 -
 6724 -/* interface variables pps->adjtimex */
 6725 -extern int pps_shift;		/* interval duration (s) (shift) */
 6726 -extern long pps_jitcnt;		/* jitter limit exceeded */
 6727 -extern long pps_calcnt;		/* calibration intervals */
 6728 -extern long pps_errcnt;		/* calibration errors */
 6729 -extern long pps_stbcnt;		/* stability limit exceeded */
 6730 + * Pick up the architecture specific timex specifications
 6731 + */
 6732 +#include <asm/timex.h>
 6733 +
 6734 +/* This limit (ns) will set the clock to `unsynchronized' */
 6735 +#define	NTP_PHASE_LIMIT	(MAXPHASE << 2)
 6736 +
 6737 +#ifdef CONFIG_NTP
 6738 +extern int time_state;				/* clock state */
 6739 +extern int time_status;				/* clock status bits */
 6740 +extern long time_maxerror;			/* maximum error (us) */
 6741 +extern long time_esterror;			/* estimated error (us) */
 6742 +#endif
 6743  
 6744  #endif /* KERNEL */
 6745  
 6746 Index: linux/include/linux/tty.h
 6747 diff -u linux/include/linux/tty.h:1.1.1.11 linux/include/linux/tty.h:1.1.1.11.2.1
 6748 --- linux/include/linux/tty.h:1.1.1.11	Sat Aug 26 21:35:42 2006
 6749 +++ linux/include/linux/tty.h	Sat Aug 26 21:50:43 2006
 6750 @@ -412,6 +412,9 @@
 6751  
 6752  /* n_tty.c */
 6753  extern struct tty_ldisc tty_ldisc_N_TTY;
 6754 +#ifdef CONFIG_NTP_PPS
 6755 +extern struct tty_ldisc tty_ldisc_N_PPSCLOCK;
 6756 +#endif
 6757  
 6758  /* tty_ioctl.c */
 6759  extern int n_tty_ioctl(struct tty_struct * tty, struct file * file,
 6760 Index: linux/include/net/icmp.h
 6761 diff -u linux/include/net/icmp.h:1.1.1.6 linux/include/net/icmp.h:1.1.1.6.2.1
 6762 --- linux/include/net/icmp.h:1.1.1.6	Sat Aug 26 21:35:40 2006
 6763 +++ linux/include/net/icmp.h	Sat Aug 26 21:50:46 2006
 6764 @@ -23,6 +23,7 @@
 6765  
 6766  #include <net/sock.h>
 6767  #include <net/protocol.h>
 6768 +#include <net/snmp.h>
 6769  
 6770  struct icmp_err {
 6771    int		errno;
 6772 Index: linux/include/net/ipv6.h
 6773 diff -u linux/include/net/ipv6.h:1.1.1.8 linux/include/net/ipv6.h:1.1.1.8.2.1
 6774 --- linux/include/net/ipv6.h:1.1.1.8	Sat Aug 26 21:35:40 2006
 6775 +++ linux/include/net/ipv6.h	Sat Aug 26 21:50:46 2006
 6776 @@ -19,6 +19,7 @@
 6777  #include <asm/hardirq.h>
 6778  #include <net/ndisc.h>
 6779  #include <net/flow.h>
 6780 +#include <net/snmp.h>
 6781  
 6782  #define SIN6_LEN_RFC2133	24
 6783  
 6784 Index: linux/kernel/ksyms.c
 6785 diff -u linux/kernel/ksyms.c:1.1.1.19 linux/kernel/ksyms.c:1.1.1.19.2.1
 6786 --- linux/kernel/ksyms.c:1.1.1.19	Sat Aug 26 21:35:37 2006
 6787 +++ linux/kernel/ksyms.c	Sat Aug 26 21:50:52 2006
 6788 @@ -23,6 +23,10 @@
 6789  #include <linux/serial.h>
 6790  #include <linux/locks.h>
 6791  #include <linux/delay.h>
 6792 +#include <linux/time.h>
 6793 +#ifdef CONFIG_NTP_PPS
 6794 +#include <linux/timepps.h>
 6795 +#endif
 6796  #include <linux/random.h>
 6797  #include <linux/reboot.h>
 6798  #include <linux/pagemap.h>
 6799 @@ -475,9 +479,17 @@
 6800  EXPORT_SYMBOL(yield);
 6801  EXPORT_SYMBOL(__cond_resched);
 6802  EXPORT_SYMBOL(jiffies);
 6803 +/* kernel time */
 6804  EXPORT_SYMBOL(xtime);
 6805 +EXPORT_SYMBOL(do_nanotime);
 6806  EXPORT_SYMBOL(do_gettimeofday);
 6807  EXPORT_SYMBOL(do_settimeofday);
 6808 +EXPORT_SYMBOL(do_clock_gettime);
 6809 +#ifdef	CONFIG_NTP_PPS
 6810 +EXPORT_SYMBOL(pps_kc_hardpps_dev);
 6811 +EXPORT_SYMBOL(pps_kc_hardpps_mode);
 6812 +EXPORT_SYMBOL(hardpps);
 6813 +#endif
 6814  
 6815  #if !defined(__ia64__)
 6816  EXPORT_SYMBOL(loops_per_jiffy);
 6817 Index: linux/kernel/l_fp.h
 6818 diff -u /dev/null linux/kernel/l_fp.h:1.2.12.2
 6819 --- /dev/null	Sat Aug 26 22:16:05 2006
 6820 +++ linux/kernel/l_fp.h	Sat Aug 26 21:53:27 2006
 6821 @@ -0,0 +1,205 @@
 6822 +/***********************************************************************
 6823 + *								       *
 6824 + * Copyright (c) David L. Mills 1993-1998			       *
 6825 + *								       *
 6826 + * Permission to use, copy, modify, and distribute this software and   *
 6827 + * its documentation for any purpose and without fee is hereby	       *
 6828 + * granted, provided that the above copyright notice appears in all    *
 6829 + * copies and that both the copyright notice and this permission       *
 6830 + * notice appear in supporting documentation, and that the name	       *
 6831 + * University of Delaware not be used in advertising or publicity      *
 6832 + * pertaining to distribution of the software without specific,	       *
 6833 + * written prior permission. The University of Delaware makes no       *
 6834 + * representations about the suitability this software for any	       *
 6835 + * purpose. It is provided "as is" without express or implied	       *
 6836 + * warranty.							       *
 6837 + *								       *
 6838 + **********************************************************************/
 6839 +/*
 6840 + * Modification history time_ops.h
 6841 + *
 6842 + * 23 Oct 98    David L. Mills
 6843 + *      Created file
 6844 + *
 6845 + * This file contains macro sets for 64-bit arithmetic and logic
 6846 + * operations in both 32-bit and 64-bit architectures. They are designed
 6847 + * to use the same source code in either architecture, with all
 6848 + * differences confined to this file. Macros adapted from the NTP
 6849 + * distribution ntp_fp.h, original author Dennis Ferguson.
 6850 + */
 6851 +#if !defined(NTP_L64)
 6852 +
 6853 +/*
 6854 + * Double precision macros for 32-bit machines
 6855 + *
 6856 + * A 64-bit fixed-point value is represented in 32-bit architectures as
 6857 + * two 32-bit words in the following format:
 6858 + *
 6859 + *    0		    1		   2		   3
 6860 + *    0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
 6861 + *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 6862 + *   |s|			Integral Part			     |
 6863 + *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 6864 + *   |			       Fractional Part			     |
 6865 + *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 6866 + *
 6867 + */
 6868 +typedef long int32;
 6869 +typedef unsigned long u_int32;
 6870 +typedef struct {			/* basic type in two formats */
 6871 +	union {
 6872 +		u_int32 Xl_ui;
 6873 +		int32 Xl_i;
 6874 +	} Ul_i;
 6875 +	union {
 6876 +		u_int32 Xl_uf;
 6877 +		int32 Xl_f;
 6878 +	} Ul_f;
 6879 +} l_fp;
 6880 +
 6881 +#define l_ui	Ul_i.Xl_ui		/* unsigned integral part */
 6882 +#define l_i	Ul_i.Xl_i		/* signed integral part */
 6883 +#define l_uf	Ul_f.Xl_uf		/* unsigned fractional part */
 6884 +#define l_f	Ul_f.Xl_f		/* signed fractional part */
 6885 +
 6886 +#define M_ADD(r_i, r_f, a_i, a_f)	/* r += a */ \
 6887 +	do { \
 6888 +		register u_int32 lo_tmp; \
 6889 +		register u_int32 hi_tmp; \
 6890 +		\
 6891 +		lo_tmp = ((r_f) & 0xffff) + ((a_f) & 0xffff); \
 6892 +		hi_tmp = (((r_f) >> 16) & 0xffff) + (((a_f) >> 16) & \
 6893 +		    0xffff); \
 6894 +		if (lo_tmp & 0x10000) \
 6895 +			 hi_tmp++; \
 6896 +		(r_f) = ((hi_tmp & 0xffff) << 16) | (lo_tmp & 0xffff); \
 6897 +		\
 6898 +		(r_i) += (a_i); \
 6899 +		if (hi_tmp & 0x10000) \
 6900 +			 (r_i)++; \
 6901 +	} while (0)
 6902 +
 6903 +#define M_SUB(r_i, r_f, a_i, a_f)	/* r -= a */ \
 6904 +	do { \
 6905 +		register u_int32 lo_tmp; \
 6906 +		register u_int32 hi_tmp; \
 6907 +		\
 6908 +		if ((a_f) == 0) { \
 6909 +			 (r_i) -= (a_i); \
 6910 +		} else { \
 6911 +			lo_tmp = ((r_f) & 0xffff) + ((-((int32)(a_f))) \
 6912 +			    & 0xffff); \
 6913 +			hi_tmp = (((r_f) >> 16) & 0xffff) \
 6914 +			    + (((-((int32)(a_f))) >> 16) & 0xffff); \
 6915 +			if (lo_tmp & 0x10000) \
 6916 +				 hi_tmp++; \
 6917 +			(r_f) = ((hi_tmp & 0xffff) << 16) | (lo_tmp & \
 6918 +			    0xffff); \
 6919 +			(r_i) += ~(a_i); \
 6920 +			if (hi_tmp & 0x10000) \
 6921 +				 (r_i)++; \
 6922 +		 } \
 6923 +	} while (0)
 6924 +
 6925 +#define M_NEG(v_i, v_f)  /* v = -v */ \
 6926 +	do { \
 6927 +		 if ((v_f) == 0) \
 6928 +			 (v_i) = -((int32)(v_i)); \
 6929 +		 else { \
 6930 +			 (v_f) = -((int32)(v_f)); \
 6931 +			 (v_i) = ~(v_i); \
 6932 +		 } \
 6933 +	} while (0)
 6934 +
 6935 +#define M_RSHIFT(v_i, v_f, n)		/* v >>= n */ \
 6936 +	do { \
 6937 +		if ((v_i) < 0) { \
 6938 +			M_NEG((v_i), (v_f)); \
 6939 +			if ((n) < 32) { \
 6940 +				(v_f) = ((v_f) >> (n)) | ((v_i) << \
 6941 +				    (32 - (n))); \
 6942 +				(v_i) = (v_i) >> (n); \
 6943 +			} else { \
 6944 +				(v_f) = (v_i) >> ((n) - 32); \
 6945 +				(v_i) = 0; \
 6946 +			} \
 6947 +			M_NEG((v_i), (v_f)); \
 6948 +		} else { \
 6949 +			if ((n) < 32) { \
 6950 +				(v_f) = ((v_f) >> (n)) | ((v_i) << \
 6951 +				    (32 - (n))); \
 6952 +				(v_i) = (v_i) >> (n); \
 6953 +			} else { \
 6954 +				(v_f) = (v_i) >> ((n) - 32); \
 6955 +				(v_i) = 0; \
 6956 +			} \
 6957 +		 } \
 6958 +	} while (0)
 6959 +
 6960 +#define M_MPY(v_i, v_f, m)		/* v *= m */ \
 6961 +	do { \
 6962 +		register u_int32 a, b, c, d; \
 6963 +		if ((v_i) < 0) { \
 6964 +			M_NEG((v_i), (v_f)); \
 6965 +			d = ((v_f) & 0xffff) * (m); \
 6966 +			c = ((v_f) >> 16) * (m) + (d >> 16); \
 6967 +			b = ((v_i) & 0xffff) * (m) + (c >> 16); \
 6968 +			a = ((v_i) >> 16) * (m) + (b >> 16); \
 6969 +			(v_i) = (a << 16) + (b & 0xffff); \
 6970 +			(v_f) = (c << 16) + (d & 0xffff); \
 6971 +			M_NEG((v_i), (v_f)); \
 6972 +		} else { \
 6973 +			d = ((v_f) & 0xffff) * (m); \
 6974 +			c = ((v_f) >> 16) * (m) + (d >> 16); \
 6975 +			b = ((v_i) & 0xffff) * (m) + (c >> 16); \
 6976 +			a = ((v_i) >> 16) * (m) + (b >> 16); \
 6977 +			(v_i) = (a << 16) + (b & 0xffff); \
 6978 +			(v_f) = (c << 16) + (d & 0xffff); \
 6979 +		} \
 6980 +	} while (0)
 6981 +/*
 6982 + * Operations - u,v are 64 bits; a,n are 32 bits.
 6983 + */
 6984 +#define L_ADD(v, u)	M_ADD((v).l_i, (v).l_uf, (u).l_i, (u).l_uf)
 6985 +#define L_SUB(v, u)	M_SUB((v).l_i, (v).l_uf, (u).l_i, (u).l_uf)
 6986 +#define L_ADDHI(v, a)	M_ADD((v).l_i, (v).l_uf, (a), 0)
 6987 +#define L_NEG(v)	M_NEG((v).l_i, (v).l_uf)
 6988 +#define L_RSHIFT(v, n)	M_RSHIFT((v).l_i, (v).l_uf, n)
 6989 +#define L_MPY(v, a)	M_MPY((v).l_i, (v).l_uf, a)
 6990 +#define L_CLR(v)	((v).l_i = (v).l_uf = 0)
 6991 +#define L_ISNEG(v)	((v).l_ui < 0)
 6992 +#define L_LINT(v, a)			/* load integral part */ \
 6993 +	do { \
 6994 +		 (v).l_i = (a); \
 6995 +		 (v).l_uf = 0; \
 6996 +	} while (0)
 6997 +#define L_GINT(v)	((v).l_i)	/* get integral part */
 6998 +
 6999 +#else /* NTP_L64 */
 7000 +
 7001 +/*
 7002 + * Single-precision macros for 64-bit machines
 7003 + *
 7004 + * A 64-bit fixed-point value is represented in 62-bit architectures as
 7005 + * a single 64-bit word, with the implied decimal point to the left of
 7006 + * bit 32.
 7007 + */
 7008 +typedef long long l_fp;
 7009 +#define L_ADD(v, u)	((v) += (u))
 7010 +#define L_SUB(v, u)	((v) -= (u))
 7011 +#define L_ADDHI(v, a)	((v) += (long long)(a) << 32)
 7012 +#define L_NEG(v)	((v) = -(v))
 7013 +#define L_RSHIFT(v, n) \
 7014 +	do { \
 7015 +		if ((v) < 0) \
 7016 +			(v) = -(-(v) >> (n)); \
 7017 +		else \
 7018 +			(v) = (v) >> (n); \
 7019 +	} while (0)
 7020 +#define L_MPY(v, a)	((v) *= (a))
 7021 +#define L_CLR(v)	((v) = 0)
 7022 +#define L_ISNEG(v)	((v) < 0)
 7023 +#define L_LINT(v, a)	((v) = (long long)(a) << 32)
 7024 +#define L_GINT(v)	((long) ((v) < 0 ? -(-(v) >> 32) : (v) >> 32))
 7025 +
 7026 +#endif /* NTP_L64 */
 7027 Index: linux/kernel/sysctl.c
 7028 diff -u linux/kernel/sysctl.c:1.1.1.18 linux/kernel/sysctl.c:1.1.1.18.2.1
 7029 --- linux/kernel/sysctl.c:1.1.1.18	Sat Aug 26 21:35:37 2006
 7030 +++ linux/kernel/sysctl.c	Sat Aug 26 21:50:52 2006
 7031 @@ -14,6 +14,7 @@
 7032   * Added proc_doulongvec_minmax, 09/08/99, Carlos H. Bauer.
 7033   * Changed linked lists to use list.h instead of lists.h, 02/24/00, Bill
 7034   *  Wendling.
 7035 + * Added `time' subdirectory, 11/09/99, Ulrich Windl
 7036   * The list_for_each() macro wasn't appropriate for the sysctl loop.
 7037   *  Removed it and replaced it with older style, 03/23/00, Bill Wendling
 7038   */
 7039 @@ -118,6 +119,7 @@
 7040  	{ root_table, LIST_HEAD_INIT(root_table_header.ctl_entry) };
 7041  
 7042  static ctl_table kern_table[];
 7043 +extern ctl_table kern_time_table[];
 7044  static ctl_table vm_table[];
 7045  #ifdef CONFIG_NET
 7046  extern ctl_table net_table[];
 7047 @@ -274,6 +276,7 @@
 7048  	{KERN_S390_USER_DEBUG_LOGGING,"userprocess_debug",
 7049  	 &sysctl_userprocess_debug,sizeof(int),0644,NULL,&proc_dointvec},
 7050  #endif
 7051 +	{KERN_TIME, "time", NULL, 0, 0555, kern_time_table},
 7052  #ifdef __x86_64__
 7053  	{KERN_EXCEPTION_TRACE,"exception-trace",
 7054  	 &exception_trace,sizeof(int),0644,NULL,&proc_dointvec},
 7055 Index: linux/kernel/time.c
 7056 diff -u linux/kernel/time.c:1.1.1.8 linux/kernel/time.c:1.1.1.8.2.1
 7057 --- linux/kernel/time.c:1.1.1.8	Sat Aug 26 21:35:37 2006
 7058 +++ linux/kernel/time.c	Sat Aug 26 21:50:52 2006
 7059 @@ -7,28 +7,193 @@
 7060   *  time related system calls: time, stime, gettimeofday, settimeofday,
 7061   *			       adjtime
 7062   */
 7063 +/* The following notice is for legal reasons, and it applies to some parts
 7064 + * of the code:
 7065 + */
 7066 +/***********************************************************************
 7067 + *								       *
 7068 + * Copyright (c) David L. Mills 1993-2000			       *
 7069 + *								       *
 7070 + * Permission to use, copy, modify, and distribute this software and   *
 7071 + * its documentation for any purpose and without fee is hereby	       *
 7072 + * granted, provided that the above copyright notice appears in all    *
 7073 + * copies and that both the copyright notice and this permission       *
 7074 + * notice appear in supporting documentation, and that the name	       *
 7075 + * University of Delaware not be used in advertising or publicity      *
 7076 + * pertaining to distribution of the software without specific,	       *
 7077 + * written prior permission. The University of Delaware makes no       *
 7078 + * representations about the suitability this software for any	       *
 7079 + * purpose. It is provided "as is" without express or implied	       *
 7080 + * warranty.							       *
 7081 + *								       *
 7082 + **********************************************************************/
 7083 +
 7084  /*
 7085   * Modification history kernel/time.c
 7086   * 
 7087 - * 1993-09-02    Philip Gladstone
 7088 - *      Created file with time related functions from sched.c and adjtimex() 
 7089 - * 1993-10-08    Torsten Duwe
 7090 - *      adjtime interface update and CMOS clock write code
 7091 - * 1995-08-13    Torsten Duwe
 7092 - *      kernel PLL updated to 1994-12-13 specs (rfc-1589)
 7093 - * 1999-01-16    Ulrich Windl
 7094 + * 1993-09-02	Philip Gladstone
 7095 + *	Created file with time related functions from sched.c and adjtimex() 
 7096 + * 1993-10-08	Torsten Duwe
 7097 + *	adjtime interface update and CMOS clock write code
 7098 + * 1995-08-13	Torsten Duwe
 7099 + *	kernel PLL updated to 1994-12-13 specs (RFC-1589)
 7100 + * 1997-09-10	Ulrich Windl
 7101 + *	Updated NTP code according to technical memorandum Jan '96
 7102 + *	"A Kernel Model for Precision Timekeeping" by Dave Mills
 7103 + * 1998-12-24	Andrea Arcangeli, Copyright (C) 1998
 7104 + *	Fixed a xtime SMP race (we need the xtime_lock rw spinlock to
 7105 + *	serialize accesses to xtime/lost_ticks).
 7106 + * 1999-01-16	Ulrich Windl
 7107   *	Introduced error checking for many cases in adjtimex().
 7108   *	Updated NTP code according to technical memorandum Jan '96
 7109   *	"A Kernel Model for Precision Timekeeping" by Dave Mills
 7110   *	Allow time_constant larger than MAXTC(6) for NTP v4 (MAXTC == 10)
 7111   *	(Even though the technical memorandum forbids it)
 7112 + * 1999-01-24	Ulrich Windl
 7113 + *	Added code to support ADJ_TICKADJ in adjtimex(). Added support for
 7114 + *	PPS signal.
 7115 + * 1999-01-31	Ulrich Windl
 7116 + *	Split NTP PPS support into different levels: NTP, NTP with PPS,
 7117 + *	NTP with PPS and serial carrier detect.  Moved time stuff from
 7118 + *	sched.c to the place where it really belongs.
 7119 + * 1999-05-10	Ulrich Windl
 7120 + *	Merged in changes for new NTP clock model (`nanokernel').  Added new
 7121 + *	do_clock_{get,set}time (see POSIX.4).  Added do_clock_getres
 7122 + *	(see POSIX.4).  Made heavy changes to the clock model and related
 7123 + *	variables.  Added mktime().  Fixes for the `nanokernel'.  Enforce
 7124 + *	monotonous time.  Increased speed of adjtime() to 50ms/s.
 7125 + * 1999-05-22	Ulrich Windl
 7126 + *	Added dynamic tolerance estimate to ``hardpps()''.  Also be more
 7127 + *	conservative when getting late pulses.  Optimized for fixed value
 7128 + *	of ``hz'' (namely ``HZ'').  Fixed problem for ``MOD_CONSTANT'' in
 7129 + *	``adjtimex()''.
 7130 + * 1999-08-15	Ulrich Windl
 7131 + *	Added clamping of illegal values in adjtimex() to avoid failures like
 7132 + *	that of the Ariane-5 (ntpd does not care much about errors from
 7133 + *	syscalls).
 7134 + * 1999-09-17	Andrea Arcangeli <andrea@suse.de>
 7135 + *	Fixed adjtimex/settimeofday/stime SMP races.
 7136 + * 1999-09-27	Ulrich Windl
 7137 + *	Make message about ``update_wall_time: ticks is 2'' conditional.
 7138 + *	Optimize ``tick_update'' away.  Call ``second_overflow'' without delay
 7139 + *	if multiple ticks are pending.  This may reduce nano jitter a bit.
 7140 + *	Added dynamic spike threshold for PPS jitter detection from the latest
 7141 + *	nanokernel (1999-08-29).  Also catch another condition for a bad pulse.
 7142 + * 1999-10-07	Ulrich Windl
 7143 + *	Make sure that MOD_OFFSET and ADJ_ADJTIME are not used at the same time
 7144 + *	(to avoid ns/us confusion).
 7145 + * 1999-11-03	Ulrich Windl
 7146 + *	Updated clock model according to ``nanokernel-19990829'': The new
 7147 + *	``hardpps()'' will adjust the offset and frequency quicker than the
 7148 + *	previous version.  New ``MOD_PPSMAX'' implemented.  Changed/added some
 7149 + *	spinlocks according to standard tree.  Enforce ``MOD_PPSMAX''.
 7150 + * 1999-11-14	Ulrich Windl
 7151 + *	Added sysctl for time variables.  Moved ``time_tick'' out from #ifdef.
 7152 + *	Deleted variable ``time_tolerance''; dynamic estimate is only used for
 7153 + *	maxerror.  Promote internal ``time_maxerror'' and ``time_esterror''
 7154 + *	to nanoseconds.  Added new ``rtc_update'' variable to prevent automatic
 7155 + *	updates of the CMOS clock.  Fixed some debug messages.
 7156 + * 1999-12-04	Ulrich Windl
 7157 + *	Corrected logic for ``rtc_update'' (was inverted).  Added new function
 7158 + *	``ktime_to_rtc'' and ``rtc_to_ktime'' to convert between kernel time
 7159 + *	and RTC time, depending on the timezone of the RTC (local or UTC).
 7160 + *	The new functions obsolete ``warp_clock()''.
 7161 + * 2000-01-05	Ulrich Windl, Harald Koenig
 7162 + *	Fixed Y2K bug in ``ktime_to_rtc()''.
 7163 + * 2000-01-19	Ulrich Windl
 7164 + *	Fixed problem when ``CONFIG_NTP_PPS'' is not defined.
 7165 + * 2000-03-29	Ulrich Windl
 7166 + *	Fixed incorrect start condition after bad pulse in ``hardpps()''.
 7167 + *	Fixed problem where ``NANOSECOND % HZ != 0'' (notably DEC Alpha).
 7168 + *	``time_tick'' is no longer used when ``CONFIG_NTP'' is defined!
 7169 + * 2000-03-31	Ulrich Windl
 7170 + *	Changed ``do_clock_gettime()'' to return the time offset.
 7171 + * 2000-04-16	Ulrich Windl
 7172 + *	New ``do_nanotime()'' does not include ``lost_ticks''.
 7173 + * 2000-04-24	Ulrich Windl
 7174 + *	Modified condition for ``STA_PPSWANDER'' in ``hardpps()'' to use
 7175 + *	dynamic threshold.  Converted ``tickadj'' to nanoseconds (external
 7176 + *	interface is still using microseconds).
 7177 + * 2000-05-01	Ulrich Windl
 7178 + *	Undo change from 1999-04-21 (Stop ``adjtime()'' if precision
 7179 + *	adjustments are being made).  The logic prevented ``adjtime()'' from
 7180 + *	working when ``STA_PLL'' was set.
 7181 + * 2000-05-16	Ulrich Windl
 7182 + *	Revised sysctl() interface to provide range checks.  Made ``time_tick''
 7183 + *	and ``tickadj'' writable.
 7184 + * 2000-09-23	Ulrich Windl
 7185 + *	Updated clock model according to ``nanokernel-20000921'' (NTP API v4):
 7186 + *	Added support for TAI (International Atomic Time) offset.  Offsets for
 7187 + *	``STA_PPSTIME'' are applied directly now, improving error statistics.
 7188 + *	Fixed detection of early pulses in ``hardpps()''.
 7189 + * 2000-12-09	Ulrich Windl
 7190 + *	Remove use of routines dealing with microseconds explicitly.
 7191 + * 2001-03-11	Ulrich Windl
 7192 + *	Added spinlock for ``hardpps()'' to protect time variables.  Fixed
 7193 + *	logic to detect negative time warps. Updated clock model according to
 7194 + *	``nanokernel-20001025'': ``PPS_FAVGDEF'' changed form 7 to 8. Clearing
 7195 + *	``STA_PLL'' will reset ``time_status''. Explicitly clear ``time_tai''.
 7196 + *	Finally found and fixed the time bug introduced to Linux-2.4
 7197 + *	(``wall_jiffies'' are not what I had thought).
 7198 + * 2001-04-17	Ulrich Windl
 7199 + *	Added state variables for PPS API's ``kernel consumer'' to support
 7200 + *	different devices.
 7201 + * 2001-05-10	Ulrich Windl
 7202 + *	Use new ``update_nanoscale()'' when ``time_freq'' or ``time_tick''
 7203 + *	changes.
 7204 + * 2001-05-14	Ulrich Windl
 7205 + *	Use ``pps.stabil'' when updating ``pps.frange'' for possibly more
 7206 + *	accurate result.
 7207 + * 2001-12-03	Ulrich Windl
 7208 + *	Fix compilation for ``#ifndef CONFIG_NTP''.
 7209 + * 2001-12-29	Ulrich Windl
 7210 + *	Avoid returning of non-normalized time due to rounding of microseconds.
 7211 + * 2002-10-17	Ulrich Windl, Petr Sumbera
 7212 + *	Avoid overflow into sign for ``MOD_OFFSET'' when converting
 7213 + *	microseconds to nanoseconds.  Added new sysctl value ``KERN_TIME_HZ''
 7214 + *	(``Hz'') for ``HZ''.
 7215 + * 2003-02-04	Ulrich Windl
 7216 + *	Added missing permission check for ``sys_clock_settime()''.  Added
 7217 + *	strategy routine to implement proper permission checks for sysctl.
 7218 + * 2003-03-21	Ulrich Windl
 7219 + *	Fixed bug: adjtimex() was accepting microseconds as nanoseconds,
 7220 + *	returning nanoseconds as microseconds.  Indicate broken adjtime()
 7221 + *	implementation with limited range.  Changed ``tickadj'' back to
 7222 + *	microseconds (which is the granularity of adjtime() anyway).
 7223   */
 7224  
 7225 +#include <linux/config.h>
 7226 +#define	NTP_NANO	/* we want to see nanoseconds in <linux/timex.h> */
 7227  #include <linux/mm.h>
 7228 +#include <linux/time.h>
 7229  #include <linux/timex.h>
 7230 +#ifdef CONFIG_NTP_PPS
 7231 +#include <linux/timepps.h>
 7232 +#endif
 7233 +#include <linux/smp.h>
 7234  #include <linux/smp_lock.h>
 7235 +#include <linux/sysctl.h>
 7236 +
 7237 +#define NTP_L64	/* GNU C has `long long' with 64 bit */
 7238 +#include "l_fp.h"
 7239  
 7240  #include <asm/uaccess.h>
 7241 +#if defined(CONFIG_NTP_PPS_DEBUG) && defined(CONFIG_X86_TSC)
 7242 +#include <asm/msr.h>
 7243 +#endif
 7244 +
 7245 +/* The current time */
 7246 +volatile struct timespec xtime __attribute__ ((aligned (16)));
 7247 +
 7248 +/* The xtime_lock is not only serializing the xtime read/writes but it's also
 7249 +   serializing all accesses to the global NTP variables.
 7250 +   NOTE NOTE: We really need a spinlock here as the global irq locking
 7251 +   only protect us against the timer irq and not against other time-related
 7252 +   syscall running under us. */
 7253 +/*
 7254 + * This spinlock protect us from races in SMP while playing with xtime. -arca
 7255 + */
 7256 +rwlock_t xtime_lock = RW_LOCK_UNLOCKED;
 7257  
 7258  /* 
 7259   * The timezone where the local system is located.  Used as a default by some
 7260 @@ -36,9 +201,478 @@
 7261   */
 7262  struct timezone sys_tz;
 7263  
 7264 -/* The xtime_lock is not only serializing the xtime read/writes but it's also
 7265 -   serializing all accesses to the global NTP variables now. */
 7266 -extern rwlock_t xtime_lock;
 7267 +
 7268 +/* kernel time variables */
 7269 +static long time_adjust = 0;	/* remaining adjustment for adjtime() */
 7270 +
 7271 +long time_tick;			/* nanoseconds per tick (ns) */
 7272 +static	long tickadj;	/* number of microseconds to adjust time_tick */
 7273 +
 7274 +/* [extension by UW]: If ``rtc_update'' is positive then update the RTC as
 7275 + * before, but using the value as interval between updates.
 7276 + * ``rtc_update_slave'' will contain the value of ``rtc_update'' if the RTC
 7277 + * clock should be updated.  Once updated, the value will be reset to zero.
 7278 + */
 7279 +static	int rtc_update;
 7280 +int rtc_update_slave;
 7281 +
 7282 +/* [extension by UW]: If ``rtc_runs_localtime'' is non-zero, believe
 7283 + * that the RTC uses local time.
 7284 + */
 7285 +static	int rtc_runs_localtime = 0;
 7286 +
 7287 +#define hz HZ			/* (fixed) timer interrupt frequency */
 7288 +
 7289 +#ifdef CONFIG_NTP
 7290 +#define	ADJ_SERVED_MODE_BITS	(MOD_OFFSET|MOD_FREQUENCY|MOD_MAXERROR| \
 7291 +				 MOD_ESTERROR|MOD_STATUS|MOD_TIMECONST| \
 7292 +				 MOD_PPSMAX|MOD_TAI|MOD_MICRO|MOD_NANO| \
 7293 +				 MOD_CLKB|MOD_CLKA| \
 7294 +				 ADJ_TICKADJ|ADJ_TIMETICK|ADJ_ADJTIME)
 7295 +/*
 7296 + * Generic NTP kernel interface
 7297 + *
 7298 + * These routines constitute the Network Time Protocol (NTP) interfaces
 7299 + * for user and daemon application programs. The ntp_gettime() routine
 7300 + * provides the time, maximum error (synch distance) and estimated error
 7301 + * (dispersion) to client user application programs. The ntp_adjtime()
 7302 + * routine is used by the NTP daemon to adjust the system clock to an
 7303 + * externally derived time. The time offset and related variables set by
 7304 + * this routine are used by other routines in this module to adjust the
 7305 + * phase and frequency of the clock discipline loop which controls the
 7306 + * system clock.
 7307 + *
 7308 + * When the kernel time is reckoned directly in nanoseconds (NTP_NANO
 7309 + * defined), the time at each tick interrupt is derived directly from
 7310 + * the kernel time variable. When the kernel time is reckoned in
 7311 + * microseconds, (NTP_NANO undefined), the time is derived from the kernel
 7312 + * time variable together with a variable representing the leftover
 7313 + * nanoseconds at the last tick interrupt. In either case, the current
 7314 + * nanosecond time is reckoned from these values plus an interpolated
 7315 + * value derived by the clock routines in another architecture-specific
 7316 + * module. The interpolation can use either a dedicated counter or a
 7317 + * processor cycle counter (PCC) implemented in some architectures.
 7318 + *
 7319 + * Note that all routines must run with exclusive access to the time
 7320 + * variables.
 7321 + */
 7322 +/*
 7323 + * Phase/frequency-lock loop (PLL/FLL) definitions
 7324 + *
 7325 + * The nanosecond clock discipline uses two variable types, time
 7326 + * variables and frequency variables. Both types are represented as 64-
 7327 + * bit fixed-point quantities with the decimal point between two 32-bit
 7328 + * halves. On a 32-bit machine, each half is represented as a single
 7329 + * word and mathematical operations are done using multiple-precision
 7330 + * arithmetic. On a 64-bit machine, ordinary computer arithmetic is
 7331 + * used.
 7332 + *
 7333 + * A time variable is a signed 64-bit fixed-point number in ns and
 7334 + * fraction. It represents the remaining time offset to be amortized
 7335 + * over succeeding tick interrupts. The maximum time offset is about
 7336 + * 0.5 s and the resolution is about 2.3e-10 ns.
 7337 + *
 7338 + *			1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
 7339 + *  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
 7340 + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 7341 + * |s s s|			 ns				   |
 7342 + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 7343 + * |			    fraction				   |
 7344 + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 7345 + *
 7346 + * A frequency variable is a signed 64-bit fixed-point number in ns/s
 7347 + * and fraction. It represents the ns and fraction to be added to the
 7348 + * kernel time variable at each second. The maximum frequency offset is
 7349 + * about +-500000 ns/s and the resolution is about 2.3e-10 ns/s.
 7350 + *
 7351 + *			1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
 7352 + *  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
 7353 + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 7354 + * |s s s s s s s s s s s s s|	          ns/s			   |
 7355 + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 7356 + * |			    fraction				   |
 7357 + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 7358 + */
 7359 +/*
 7360 + * The following variables establish the state of the PLL/FLL and the
 7361 + * residual time and frequency offset of the local clock.
 7362 + */
 7363 +#define SHIFT_PLL	4	/* PLL loop gain (shift) */
 7364 +#define SHIFT_FLL	2	/* FLL loop gain (shift) */
 7365 +
 7366 +int time_state;			/* clock state */
 7367 +/* bit STA_UNSYNC prevents a periodic update of the CMOS clock */
 7368 +int time_status;		/* clock status bits */
 7369 +long time_constant;		/* poll interval (shift) (s) */
 7370 +long time_tai;			/* TAI offset (s) */
 7371 +long time_monitor;		/* last time offset scaled (ns) */
 7372 +long time_precision = 1;	/* clock precision (ns) */
 7373 +long time_maxerror;		/* maximum error (ns) -- [extension by UW] */
 7374 +long time_esterror;		/* estimated error (ns) -- [extension by UW] */
 7375 +long time_reftime;		/* time at last adjustment (s) */
 7376 +l_fp time_offset;		/* time offset (ns) */
 7377 +l_fp time_freq;			/* frequency offset (ns/s) */
 7378 +l_fp time_adj;			/* tick adjust (ns/s) */
 7379 +l_fp time_phase;		/* time phase (ns) */
 7380 +
 7381 +#ifdef CONFIG_NTP_PPS
 7382 +/*
 7383 + * The following variables are used when a pulse-per-second (PPS) signal
 7384 + * is available and connected via a modem control lead. They establish
 7385 + * the engineering parameters of the clock discipline loop when
 7386 + * controlled by the PPS signal.
 7387 + */
 7388 +#define PPS_FAVG	2	/* min freq avg interval (s) (shift) */
 7389 +#define PPS_FAVGDEF	8	/* default freq avg interval (s) (shift) */
 7390 +#define PPS_FAVGMAX	15	/* max freq avg interval (s) (shift) */
 7391 +#define PPS_PAVG	4	/* phase avg interval (s) (shift) */
 7392 +#define PPS_VALID	120	/* PPS signal watchdog max (s) */
 7393 +#define PPS_MAXWANDER	100000	/* max PPS wander (ns/s) */
 7394 +#define PPS_POPCORN	2	/* popcorn spike threshold (shift) */
 7395 +
 7396 +struct pps_var {
 7397 +	struct timespec tf[3];	/* phase median filter */
 7398 +	l_fp freq;		/* scaled frequency offset (ns/s) */
 7399 +	long lastfreq;		/* last scaled freq offset (ns/s) */
 7400 +	long fcount;		/* frequency accumulator */
 7401 +	long jitter;		/* nominal jitter (ns) */
 7402 +	long stabil;		/* nominal stability (scaled ns/s) */
 7403 +	long lastsec;		/* time of last calibration (s) */
 7404 +	int valid;		/* signal watchdog counter */
 7405 +	int shift;		/* interval duration (s) (shift) */
 7406 +	int shiftmax;		/* max interval duration (s) (shift) */
 7407 +	int intcnt;		/* interval counter */
 7408 +
 7409 +/*
 7410 + * PPS signal quality monitors
 7411 + */
 7412 +	long calcnt;		/* calibration intervals */
 7413 +	long jitcnt;		/* jitter limit exceeded */
 7414 +	long stbcnt;		/* stability limit exceeded */
 7415 +	long errcnt;		/* calibration errors */
 7416 +
 7417 +/* dynamic tolerance adjustment */
 7418 +	long fmin, fmax;	/* PPS frequency minimum and maximum */
 7419 +	long frange;		/* frequency range */
 7420 +};
 7421 +
 7422 +static struct pps_var pps;
 7423 +
 7424 +/* PPS API (RFC 2783): current source and mode for ``kernel consumer'' */
 7425 +void	*pps_kc_hardpps_dev = NULL;	/* some unique pointer to device */
 7426 +int	pps_kc_hardpps_mode;		/* mode bits for kernel consumer */
 7427 +#endif /* CONFIG_NTP_PPS */
 7428 +/*
 7429 + * End of phase/frequency-lock loop (PLL/FLL) definitions
 7430 + */
 7431 +#else	/* !CONFIG_NTP */
 7432 +#define	ADJ_SERVED_MODE_BITS	(ADJ_TICKADJ|ADJ_TIMETICK|ADJ_ADJTIME)
 7433 +#endif
 7434 +
 7435 +/* Define entries for `time' subdirectory. */
 7436 +static /* const */ int	sysctl_tz_min	= -12 * 60;
 7437 +static /* const */ int	sysctl_tz_max	=  12 * 60;
 7438 +static /* const */ int	sysctl_zero	= 0;
 7439 +static /* const */ int	sysctl_one	= 1;
 7440 +static /* const */ int	sysctl_no_max	= 1 << 30;
 7441 +static /* const */ int	sysctl_tick_min	= 9 * (NANOSECOND/10/hz);
 7442 +static /* const */ int	sysctl_tick_max	= 11 * (NANOSECOND/10/hz);
 7443 +static /* const */ int	sysctl_tickadj_max = (NANOSECOND/1000/hz) / 4;
 7444 +static /* const */ int	sysctl_hz = HZ;
 7445 +
 7446 +/* strategy routine for sysctl() */
 7447 +static int time_strategy(ctl_table *table, int *name, int nlen,
 7448 +			 void *oldval, size_t *oldlenp,
 7449 +			 void *newval, size_t newlen, void **context)
 7450 +{
 7451 +	if (!capable(CAP_SYS_TIME)) {
 7452 +		return -EPERM;
 7453 +	}
 7454 +	return 0;		/* do automatic checking */
 7455 +}
 7456 +
 7457 +ctl_table kern_time_table[] = {
 7458 +	/* Warning: ``sys_tz.tz_dsttime'' isn't checked properly */
 7459 +	{KERN_TIME_TIMEZONE, "timezone", &sys_tz, sizeof(sys_tz),
 7460 +	 0644, NULL, &proc_dointvec_minmax, &time_strategy, NULL,
 7461 +	 &sysctl_tz_min, &sysctl_tz_max},
 7462 +	{KERN_TIME_RTC_RUNS_LOCALTIME, "rtc_runs_localtime",
 7463 +	 &rtc_runs_localtime, sizeof(rtc_runs_localtime),
 7464 +	 0644, NULL, &proc_dointvec_minmax, &time_strategy, NULL,
 7465 +	 &sysctl_zero, &sysctl_one},
 7466 +	{KERN_TIME_RTC_UPDATE, "rtc_update", &rtc_update, sizeof(rtc_update),
 7467 +	 0644, NULL, &proc_dointvec_minmax, &time_strategy, NULL,
 7468 +	 &sysctl_zero, &sysctl_no_max},
 7469 +	{KERN_TIME_TIME_TICK, "time_tick", &time_tick, sizeof(time_tick),
 7470 +	 0644, NULL, &proc_dointvec_minmax, &time_strategy, NULL,
 7471 +	 &sysctl_tick_min, &sysctl_tick_max},
 7472 +	{KERN_TIME_TICKADJ, "tickadj", &tickadj, sizeof(tickadj),
 7473 +	 0644, NULL, &proc_dointvec_minmax, &time_strategy, NULL,
 7474 +	 &sysctl_one, &sysctl_tickadj_max},
 7475 +	{KERN_TIME_HZ, "Hz", &sysctl_hz, sizeof(sysctl_hz),
 7476 +	 0444, NULL, &proc_dointvec},
 7477 +#ifdef CONFIG_NTP
 7478 +#ifdef CONFIG_NTP_PPS
 7479 +	/* this entry is for debugging (experimental) */
 7480 +	{KERN_TIME_PPS_VAR, "pps", &pps, sizeof(pps),
 7481 +	 0444, NULL, &proc_dointvec},
 7482 +#endif
 7483 +#endif
 7484 +	{0}
 7485 +};
 7486 +
 7487 +/* Converts Gregorian date to seconds since 1970-01-01 00:00:00.
 7488 + * Assumes input in normal date format, i.e. 1980-12-31 23:59:59
 7489 + * => year=1980, mon=12, day=31, hour=23, min=59, sec=59.
 7490 + *
 7491 + * [For the Julian calendar (which was used in Russia before 1917,
 7492 + * Britain & colonies before 1752, anywhere else before 1582,
 7493 + * and is still in use by some communities) leave out the
 7494 + * -year/100+year/400 terms, and add 10.]
 7495 + *
 7496 + * This algorithm was first published by Gauss (I think).
 7497 + *
 7498 + * WARNING: this function will overflow on 2106-02-07 06:28:16 on
 7499 + * machines were long is 32-bit! (However, as time_t is signed, we
 7500 + * will already get problems at other places on 2038-01-19 03:14:08)
 7501 + */
 7502 +unsigned long
 7503 +mktime (unsigned int year, unsigned int mon,
 7504 +	unsigned int day, unsigned int hour,
 7505 +	unsigned int min, unsigned int sec)
 7506 +{
 7507 +	if (0 >= (int) (mon -= 2)) {	/* 1..12 -> 11,12,1..10 */
 7508 +		mon += 12;		/* Puts Feb last since it has leap day */
 7509 +		year -= 1;
 7510 +	}
 7511 +
 7512 +	return (((
 7513 +		(unsigned long) (year/4 - year/100 + year/400 + 367*mon/12 + day) +
 7514 +			year*365 - 719499
 7515 +	    )*24 + hour /* now have hours */
 7516 +	  )*60 + min /* now have minutes */
 7517 +	)*60 + sec; /* finally seconds */
 7518 +}
 7519 +
 7520 +/* Convert Gregorian calendar information from RTC to kernel time
 7521 + * (uses mktime).
 7522 + */
 7523 +time_t rtc_to_ktime(unsigned int year, unsigned int mon,
 7524 +		     unsigned int day, unsigned int hour,
 7525 +		     unsigned int min, unsigned int sec)
 7526 +{
 7527 +	time_t result = mktime(year, mon, day, hour, min, sec);
 7528 +	if (rtc_runs_localtime) {		/* apply effective timezone */
 7529 +		result += sys_tz.tz_minuteswest * 60;
 7530 +		if (sys_tz.tz_dsttime)
 7531 +			result -= 3600;
 7532 +	}
 7533 +	return result;
 7534 +}
 7535 +
 7536 +/* Build time and date components from system time, intended to set the RTC
 7537 + * (based on code found in fs/fat/misc.c):
 7538 + * ``*hhmmss = seconds + (minutes << 8) + (hour << 16)''
 7539 + * ``*yyyymmdd = mday + (month << 8) + (year << 16)''
 7540 + */
 7541 +void ktime_to_rtc(time_t t, int *hhmmss, int *yyyymmdd)
 7542 +{
 7543 +	/* Table to map days in year to months (non leap-years, zero-based) */
 7544 +	static int dmt[] = {
 7545 +		0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 0,0,0,0
 7546 +	/*    Jan Feb Mar Apr  May  Jun  Jul  Aug  Sep  Oct  Nov  Dec */
 7547 +	};
 7548 +	int day, year, nl_day, month;
 7549 +	if (rtc_runs_localtime) {		/* apply effective timezone */
 7550 +		t -= sys_tz.tz_minuteswest * 60;
 7551 +		if (sys_tz.tz_dsttime)
 7552 +			t += 3600;
 7553 +	}
 7554 +	*hhmmss = (t % 60) + (((t / 60) % 60) << 8) + (((t / 3600) % 24) << 16);
 7555 +	day = t / 86400;		/* days since epoch (1970) */
 7556 +	year = day / 365;		/* non-leap years since epoch */
 7557 +	/* correct for leap years in epoch: 1972 (2) was the first */
 7558 +	if ((year + 1) / 4 + 365 * year > day)
 7559 +		year--;
 7560 +	/* reduce to day in current year */
 7561 +	day -= (year + 1) / 4 + 365 * year;
 7562 +	/* map March, 1st to Feburary, 29nd for leap years */
 7563 +	if (day == 59 && ((year + 2) & 3) == 0) {
 7564 +		nl_day = day;
 7565 +		month = 2;
 7566 +	} else {			/* determine month and day of month */
 7567 +		nl_day = ((year + 2) & 3) || day < 59 ? day : day - 1;
 7568 +		for (month = 0; month < 12; month++)
 7569 +			if (dmt[month] > nl_day)
 7570 +				break;
 7571 +	}
 7572 +	*yyyymmdd = nl_day - dmt[month - 1] + 1 + (month << 8) + ((year + 1970) << 16);
 7573 +}
 7574 +
 7575 +/*
 7576 + * Advance to a new second, and possibly update time adjustments.
 7577 + *
 7578 + * The tricky bits of code to handle the accurate clock support
 7579 + * were provided by Dave Mills (Mills@UDEL.EDU) of NTP fame.
 7580 + * They were originally developed for SUN and DEC kernels.
 7581 + * All the kudos should go to Dave for this stuff.
 7582 + *
 7583 + */
 7584 +static inline void second_overflow(void)
 7585 +{
 7586 +#ifdef CONFIG_NTP
 7587 +	l_fp ftemp;		/* 32/64-bit temporary */
 7588 +
 7589 +	/* Bump the maxerror field */
 7590 +#ifdef CONFIG_NTP_PPS
 7591 +	time_maxerror += pps.frange;
 7592 +#else
 7593 +	time_maxerror += MAXFREQ;
 7594 +#endif
 7595 +	if (time_maxerror > NTP_PHASE_LIMIT) {
 7596 +		time_maxerror = NTP_PHASE_LIMIT;
 7597 +		time_status |= STA_UNSYNC;
 7598 +	}
 7599 +	if (rtc_update > 0 && (time_status & STA_UNSYNC) == 0)
 7600 +		rtc_update_slave = rtc_update;
 7601 +
 7602 +	/*
 7603 +	 * Leap second processing. If in leap-insert state at
 7604 +	 * the end of the day, the system clock is set back one
 7605 +	 * second; if in leap-delete state, the system clock is
 7606 +	 * set ahead one second. The nano_time() routine or
 7607 +	 * external clock driver will insure that reported time
 7608 +	 * is always monotonic.
 7609 +	 */
 7610 +	switch (time_state) {
 7611 +
 7612 +	case TIME_OK:	/* no leap second */
 7613 +		if (time_status & STA_INS)
 7614 +			time_state = TIME_INS;
 7615 +		else if (time_status & STA_DEL)
 7616 +			time_state = TIME_DEL;
 7617 +		break;
 7618 +
 7619 +	case TIME_INS:	/* Insert second 23:59:60 following second 23:59:59. */
 7620 +		if (!(time_status & STA_INS))
 7621 +			time_state = TIME_OK;
 7622 +		else if (xtime.tv_sec % 86400 == 0) {
 7623 +			xtime.tv_sec--;
 7624 +			time_state = TIME_OOP;
 7625 +			printk(KERN_NOTICE
 7626 +			       "TIME_INS: inserting second 23:59:60 UTC\n");
 7627 +		}
 7628 +		break;
 7629 +
 7630 +	case TIME_DEL:	/* Skip second 23:59:59. */
 7631 +		if (!(time_status & STA_DEL))
 7632 +			time_state = TIME_OK;
 7633 +		else if ((xtime.tv_sec + 1) % 86400 == 0) {
 7634 +			xtime.tv_sec++;
 7635 +			time_tai--;
 7636 +			time_state = TIME_WAIT;
 7637 +			printk(KERN_NOTICE
 7638 +			       "TIME_DEL: skipping second 23:59:59 UTC\n");
 7639 +		}
 7640 +		break;
 7641 +
 7642 +	case TIME_OOP:	/* leap second insert in progress */
 7643 +		time_tai++;
 7644 +		time_state = TIME_WAIT;
 7645 +		break;
 7646 +
 7647 +	case TIME_WAIT:	/* Wait for status bits to clear. */
 7648 +		if (!(time_status & (STA_INS | STA_DEL)))
 7649 +			time_state = TIME_OK;
 7650 +	}
 7651 +
 7652 +	/*
 7653 +	 * Compute the total time adjustment for the next second
 7654 +	 * in ns. The offset is reduced by a factor depending on
 7655 +	 * whether the PPS signal is operating. Note that the
 7656 +	 * value is in effect scaled by the clock frequency,
 7657 +	 * since the adjustment is added at each tick interrupt.
 7658 +	 */
 7659 +	ftemp = time_offset;
 7660 +#ifdef CONFIG_NTP_PPS
 7661 +	if (time_status & STA_PPSTIME && time_status & STA_PPSSIGNAL)
 7662 +		L_RSHIFT(ftemp, pps.shift);
 7663 +	else
 7664 +#endif
 7665 +		L_RSHIFT(ftemp, SHIFT_PLL + time_constant);
 7666 +	time_adj = ftemp;
 7667 +	L_SUB(time_offset, ftemp);
 7668 +	L_ADD(time_adj, time_freq);
 7669 +	L_ADDHI(time_adj, NANOSECOND);
 7670 +#ifdef	CONFIG_NTP_PPS
 7671 +	if (pps.valid > 0)
 7672 +		pps.valid--;
 7673 +	else
 7674 +		time_status &= ~(STA_PPSSIGNAL | STA_PPSJITTER |
 7675 +				 STA_PPSWANDER | STA_PPSERROR);
 7676 +#endif
 7677 +#endif /* CONFIG_NTP */
 7678 +}
 7679 +
 7680 +/* in the NTP reference this is called "hardclock()" */
 7681 +inline static void update_wall_time_one_tick(void)
 7682 +{
 7683 +	long ltemp, time_update = time_adjust;
 7684 +
 7685 +	if (time_update != 0) {
 7686 +		/* We are doing an adjtime() thing.
 7687 +		 * Clamp time_update within bounds (-tickadj .. tickadj).
 7688 +		 */
 7689 +		if (time_update > tickadj)
 7690 +			time_update = tickadj;
 7691 +		else if (time_update < -tickadj)
 7692 +			time_update = -tickadj;
 7693 +	     
 7694 +		/* Update remaining adjustment */
 7695 +		time_adjust -= time_update;
 7696 +		time_update *= 1000;	/* convert to nanoseconds */
 7697 +	}
 7698 +#ifdef CONFIG_NTP
 7699 +	/*
 7700 +	 * Update the nanosecond and microsecond clocks. If the phase
 7701 +	 * increment exceeds the tick period, update the clock phase.
 7702 +	 */
 7703 +	L_ADD(time_phase, time_adj);
 7704 +	ltemp = L_GINT(time_phase) / hz;
 7705 +	time_update += ltemp;
 7706 +	L_ADDHI(time_phase, -ltemp * hz);
 7707 +#else
 7708 +	time_update += time_tick;
 7709 +#endif
 7710 +	xtime.tv_nsec += time_update;
 7711 +}
 7712 +
 7713 +/*
 7714 + * Using a loop looks inefficient, but "ticks" is
 7715 + * usually just one (we shouldn't be losing ticks,
 7716 + * we're doing this this way mainly for interrupt
 7717 + * latency reasons, not because we think we'll
 7718 + * have lots of lost timer ticks
 7719 + */
 7720 +void update_wall_time(unsigned long ticks)
 7721 +{
 7722 +#ifdef CONFIG_NTP_PPS_DEBUG
 7723 +	if (ticks > 1)
 7724 +		printk(KERN_INFO "update_wall_time: ticks is %lu\n", ticks);
 7725 +#endif
 7726 +	do {
 7727 +		ticks--;
 7728 +		update_wall_time_one_tick();
 7729 +		if (xtime.tv_nsec >= NANOSECOND) {
 7730 +			xtime.tv_nsec -= NANOSECOND;
 7731 +			xtime.tv_sec++;
 7732 +			second_overflow();
 7733 +		}
 7734 +	} while (ticks);
 7735 +}
 7736 +
 7737 +/* these pointers are set up during init! */
 7738 +unsigned long (*do_nanotime)(void);
 7739  
 7740  #if !defined(__alpha__) && !defined(__ia64__)
 7741  
 7742 @@ -71,7 +705,7 @@
 7743   * architectures that need it).
 7744   */
 7745   
 7746 -asmlinkage long sys_stime(int * tptr)
 7747 +asmlinkage int sys_stime(int * tptr)
 7748  {
 7749  	int value;
 7750  
 7751 @@ -82,18 +716,134 @@
 7752  	write_lock_irq(&xtime_lock);
 7753  	vxtime_lock();
 7754  	xtime.tv_sec = value;
 7755 -	xtime.tv_usec = 0;
 7756 +	xtime.tv_nsec = 0;
 7757  	vxtime_unlock();
 7758  	time_adjust = 0;	/* stop active adjtime() */
 7759 +	if (rtc_update > 0)
 7760 +		rtc_update_slave = 1;	/* update the RTC clock next second */
 7761 +#ifdef CONFIG_NTP
 7762  	time_status |= STA_UNSYNC;
 7763  	time_maxerror = NTP_PHASE_LIMIT;
 7764  	time_esterror = NTP_PHASE_LIMIT;
 7765 +#endif
 7766  	write_unlock_irq(&xtime_lock);
 7767  	return 0;
 7768  }
 7769  
 7770  #endif
 7771  
 7772 +/* Get the time with theoretical nanosecond resolution */
 7773 +inline unsigned long do_clock_gettime(clockid_t sysclock, struct timespec *tsp)
 7774 +{
 7775 +	unsigned long flags;
 7776 +	unsigned long nsec, sec;
 7777 +	unsigned long result;
 7778 +	static struct timespec last;
 7779 +
 7780 +	read_lock_irqsave(&xtime_lock, flags);
 7781 +	result = do_nanotime();
 7782 +	sec = xtime.tv_sec;
 7783 +	nsec = xtime.tv_nsec + result;
 7784 +	nsec += (jiffies - wall_jiffies) * time_tick;
 7785 +	while (nsec >= NANOSECOND) {
 7786 +		nsec -= NANOSECOND;
 7787 +		sec++;
 7788 +	}
 7789 +	/* tick adjustments and interrupt delays could make the time
 7790 +         * go backwards for very short intervals.  Avoid that.
 7791 +	 */
 7792 +	if (nsec <= last.tv_nsec && sec == last.tv_sec)
 7793 +	{
 7794 +#if 1		/* [UW]: maybe remove this once debugged */
 7795 +		static	int	complaints = 100;
 7796 +
 7797 +		if (complaints > 0) {
 7798 +			--complaints;
 7799 +			printk(KERN_WARNING
 7800 +			       "do_clock_gettime: negative time warp on "
 7801 +			       "CPU#%d: %ldns (%ld,%ld)\n",
 7802 +			       smp_processor_id(), nsec - last.tv_nsec,
 7803 +			       jiffies - wall_jiffies, result);
 7804 +			if (complaints == 0)
 7805 +				printk(KERN_INFO
 7806 +				       "(won't complain any more)\n");
 7807 +		}
 7808 +#endif
 7809 +		if ((nsec = last.tv_nsec + 1) >= NANOSECOND) {
 7810 +			nsec -= NANOSECOND;
 7811 +			sec++;
 7812 +		}
 7813 +	}
 7814 +	last.tv_sec = sec;
 7815 +	last.tv_nsec = nsec;
 7816 +	read_unlock_irqrestore(&xtime_lock, flags);
 7817 +
 7818 +#ifdef CONFIG_NTP_PPS_DEBUG
 7819 +#ifdef CONFIG_SMP	/*[UW] put CPU# into time (very ugly) */
 7820 +#if NR_CPUS > 32
 7821 +#error "NR_CPUS > 32; code has to be updated"
 7822 +#endif  
 7823 +	nsec &= ~31;
 7824 +	nsec |= smp_processor_id();
 7825 +#endif	/* CONFIG_SMP */
 7826 +#endif	/* CONFIG_NTP_PPS_DEBUG */
 7827 +	tsp->tv_sec = sec;
 7828 +	tsp->tv_nsec = nsec;
 7829 +	return result;
 7830 +}
 7831 +
 7832 +/*
 7833 + * This version of gettimeofday has better than microsecond resolution
 7834 + * on fast x86 machines with TSC.
 7835 + */
 7836 +void inline do_gettimeofday(struct timeval *tvp)
 7837 +{
 7838 +	struct timespec now;
 7839 +
 7840 +	do_clock_gettime(CLOCK_REALTIME, &now);
 7841 +	tvp->tv_sec = now.tv_sec;
 7842 +	tvp->tv_usec = now.tv_nsec / 1000;
 7843 +}
 7844 +
 7845 +/* Set the time with theoretical nanosecond resolution */
 7846 +void do_clock_settime(clockid_t sysclock, const struct timespec *tsp)
 7847 +{
 7848 +	unsigned long flags;
 7849 +	struct timespec new_ts = *tsp;
 7850 +
 7851 +	write_lock_irqsave(&xtime_lock, flags);
 7852 +	/* The value in xtime is the time at the last tick. As the current
 7853 +	 * time offset will be added when reading the time, subtract it first.
 7854 +	 * Finally think of normalizing the time again.
 7855 +	 */
 7856 +	new_ts.tv_nsec -= do_nanotime();
 7857 +	new_ts.tv_nsec -= (jiffies - wall_jiffies) * time_tick;
 7858 +
 7859 +	while (new_ts.tv_nsec < 0) {
 7860 +		new_ts.tv_nsec += NANOSECOND;
 7861 +		new_ts.tv_sec--;
 7862 +	}
 7863 +
 7864 +	xtime = new_ts;
 7865 +	time_adjust = 0;		/* stop active adjtime() */
 7866 +	if (rtc_update > 0)
 7867 +		rtc_update_slave = 1;	/* update the RTC clock next second */
 7868 +#ifdef CONFIG_NTP
 7869 +	time_status |= STA_UNSYNC;
 7870 +	time_maxerror = NTP_PHASE_LIMIT;
 7871 +	time_esterror = NTP_PHASE_LIMIT;
 7872 +#endif
 7873 +	write_unlock_irqrestore(&xtime_lock, flags);
 7874 +}
 7875 +
 7876 +void do_settimeofday(const struct timeval *tvp)
 7877 +{
 7878 +	struct timespec now;
 7879 +	now.tv_sec = tvp->tv_sec;
 7880 +	now.tv_nsec = tvp->tv_usec * 1000;
 7881 +	do_clock_settime(CLOCK_REALTIME, &now);
 7882 +}
 7883 +
 7884  asmlinkage long sys_gettimeofday(struct timeval *tv, struct timezone *tz)
 7885  {
 7886  	if (tv) {
 7887 @@ -109,6 +859,54 @@
 7888  	return 0;
 7889  }
 7890  
 7891 +/* get the time with nanosecond resolution */
 7892 +asmlinkage int sys_clock_gettime(clockid_t sysclock, struct timespec *tsp)
 7893 +{
 7894 +	/* only one clock (CLOCK_REALTIME) is implemented */
 7895 +	if (tsp) {
 7896 +		struct timespec kts;
 7897 +		do_clock_gettime(sysclock, &kts);
 7898 +		if (copy_to_user(tsp, &kts, sizeof(kts)))
 7899 +			return -EFAULT;
 7900 +	}
 7901 +	return 0;
 7902 +}
 7903 +
 7904 +/* Get clock resolution down to one nanosecond theoretically */
 7905 +void do_clock_getres(clockid_t sysclock, struct timespec *tsp)
 7906 +{
 7907 +	struct timespec ts;
 7908 +	int retry_limit;		/* implement emergency break */
 7909 +
 7910 +	/* We do a quick and dirty algorithm: Just get a non-zero time
 7911 +         * difference (with possibly some retries for fast CPUs or
 7912 +         * poor resolution)...
 7913 +	 */
 7914 +	ts.tv_sec = 0;
 7915 +	retry_limit = 1000;
 7916 +	do {
 7917 +		struct timespec	ts1, ts2;
 7918 +		do_clock_gettime(sysclock, &ts1);
 7919 +		do_clock_gettime(sysclock, &ts2);
 7920 +		ts.tv_nsec = ts2.tv_nsec - ts1.tv_nsec;
 7921 +		ts.tv_sec = ts2.tv_sec - ts1.tv_sec;
 7922 +	} while ( --retry_limit > 0 && (ts.tv_sec != 0 || ts.tv_nsec == 0) );
 7923 +	*tsp = ts;
 7924 +}
 7925 +
 7926 +/* get the time resolution down to one nanosecond */
 7927 +asmlinkage int sys_clock_getres(clockid_t sysclock, struct timespec *tsp)
 7928 +{
 7929 +	/* only one clock (CLOCK_REALTIME) is implemented */
 7930 +	if (tsp) {
 7931 +		struct timespec kts;
 7932 +		do_clock_getres(sysclock, &kts);
 7933 +		if (copy_to_user(tsp, &kts, sizeof(kts)))
 7934 +			return -EFAULT;
 7935 +	}
 7936 +	return 0;
 7937 +}
 7938 +
 7939  /*
 7940   * Adjust the time obtained from the CMOS to be UTC time instead of
 7941   * local time.
 7942 @@ -188,210 +986,766 @@
 7943  	return do_sys_settimeofday(tv ? &new_tv : NULL, tz ? &new_tz : NULL);
 7944  }
 7945  
 7946 -long pps_offset;		/* pps time offset (us) */
 7947 -long pps_jitter = MAXTIME;	/* time dispersion (jitter) (us) */
 7948 +asmlinkage int sys_clock_settime(clockid_t sysclock,
 7949 +				 const struct timespec *tsp)
 7950 +{
 7951 +	struct timespec	new_ts;
 7952  
 7953 -long pps_freq;			/* frequency offset (scaled ppm) */
 7954 -long pps_stabil = MAXFREQ;	/* frequency dispersion (scaled ppm) */
 7955 +	if (!capable(CAP_SYS_TIME))
 7956 +		return -EPERM;
 7957 +	if (tsp) {
 7958 +		if (copy_from_user(&new_ts, tsp, sizeof(*tsp)))
 7959 +			return -EFAULT;
 7960 +	}
 7961 +	do_clock_settime(sysclock, tsp);
 7962 +	return 0;
 7963 +}
 7964  
 7965 -long pps_valid = PPS_VALID;	/* pps signal watchdog counter */
 7966 +#ifdef	CONFIG_NTP_PPS
 7967 +/*
 7968 + * hardpps() - discipline CPU clock oscillator to external PPS signal
 7969 + *
 7970 + * This routine is called at each PPS interrupt in order to discipline
 7971 + * the CPU clock oscillator to the PPS signal. There are two independent
 7972 + * first-order feedback loops, one for the phase, the other for the
 7973 + * frequency. The phase loop measures and grooms the PPS phase offset
 7974 + * and leaves it in a handy spot for the seconds overflow routine. The
 7975 + * frequency loop averages successive PPS phase differences and
 7976 + * calculates the PPS frequency offset, which is also processed by the
 7977 + * seconds overflow routine. The code requires the caller to capture the
 7978 + * time and architecture-dependent hardware counter values in
 7979 + * nanoseconds at the on-time PPS signal transition.
 7980 + *
 7981 + * Note that, on some Unix systems this routine runs at an interrupt
 7982 + * priority level higher than the timer interrupt routine second_overflow().
 7983 + * Therefore, the variables used are distinct from the second_overflow()
 7984 + * variables, except for the actual time and frequency variables, which
 7985 + * are determined by this routine and updated atomically.
 7986 + */
 7987 +void hardpps(const struct timespec *p_ts, long nsec)
 7988 +{
 7989 +	static	long pps_lastcount;	/* last counter offset */
 7990 +	unsigned long flags;
 7991 +	long u_sec, u_nsec, v_nsec;	/* multi-purpose temps */
 7992 +	l_fp ftemp;
 7993 +	int fr_upd;			/* update frequency range? */
 7994 +#ifdef CONFIG_NTP_PPS_DEBUG
 7995 +#ifdef CONFIG_X86_TSC
 7996 +	struct {
 7997 +		unsigned long low;
 7998 +		/* unsigned long high; */
 7999 +	} start_cc, end_cc;
 8000 +	static long cc_min = 10000000L;
 8001 +	static long cc_max = 0L;
 8002 +	unsigned long cc;
 8003 +
 8004 +	/* read Pentium cycle counter */
 8005 +	rdtscl(start_cc.low);
 8006 +#endif
 8007 +#endif
 8008 +	write_lock_irqsave(&xtime_lock, flags);
 8009 +	/*
 8010 +	 * The signal is first processed by a range gate and frequency
 8011 +	 * discriminator. The range gate rejects noise spikes outside
 8012 +	 * the range +-500 us. The frequency discriminator rejects input
 8013 +	 * signals with apparent frequency outside the range 1 +-500
 8014 +	 * PPM. If two hits occur in the same second, we ignore the
 8015 +	 * later hit; if not and a hit occurs outside the range gate,
 8016 +	 * keep the later hit for later comparison, but do not process
 8017 +	 * it.
 8018 +	 */
 8019 +#if 0
 8020 +	time_tick = (NANOSECOND + hz/2) / hz;	/* UW: really necessary? */
 8021 +#endif
 8022 +	time_status |= STA_PPSJITTER;
 8023 +	time_status &= ~(STA_PPSWANDER | STA_PPSERROR);
 8024 +	u_sec = p_ts->tv_sec;
 8025 +	u_nsec = p_ts->tv_nsec;		/* relative position around second */
 8026 +	if (u_nsec >= (NANOSECOND >> 1)) {
 8027 +		u_nsec -= NANOSECOND;
 8028 +		u_sec++;
 8029 +	}
 8030 +	/* compute relative offset to last position */
 8031 +	v_nsec = u_nsec - pps.tf[0].tv_nsec;
 8032 +	if (u_sec == pps.tf[0].tv_sec && v_nsec < NANOSECOND - MAXFREQ) {
 8033 +#ifdef CONFIG_NTP_PPS_DEBUG
 8034 +		printk(KERN_INFO
 8035 +		       "hardpps(%ld.%08ld): early pulse, delta = %ld ns\n",
 8036 +		       p_ts->tv_sec % 10, p_ts->tv_nsec, v_nsec);
 8037 +#endif
 8038 +		goto done;			/* ignore early pulse */
 8039 +	}
 8040 +	pps.tf[2] = pps.tf[1];
 8041 +	pps.tf[1] = pps.tf[0];
 8042 +	pps.tf[0].tv_sec = u_sec;
 8043 +	pps.tf[0].tv_nsec = u_nsec;
 8044 +
 8045 +	/*
 8046 +	 * Compute the difference between the current and previous
 8047 +	 * counter values. If the difference exceeds 0.5 s, assume it
 8048 +	 * has wrapped around, so correct 1.0 s. If the result exceeds
 8049 +	 * the tick interval, the sample point has crossed a tick
 8050 +	 * boundary during the last second, so correct the tick. Very
 8051 +	 * intricate.
 8052 +	 */
 8053 +	u_nsec = nsec - pps_lastcount;
 8054 +	pps_lastcount = nsec;
 8055 +	if (u_nsec > (time_tick >> 1))
 8056 +		u_nsec -= time_tick;
 8057 +	else if (u_nsec < -(time_tick >> 1))
 8058 +		u_nsec += time_tick;
 8059 +	pps.fcount += u_nsec;
 8060 +	if (v_nsec > MAXFREQ || v_nsec < -MAXFREQ) {
 8061 +#ifdef CONFIG_NTP_PPS_DEBUG
 8062 +		printk(KERN_INFO "hardpps: bad pulse, delta = %ld ns\n",
 8063 +		       v_nsec);
 8064 +		printk(KERN_DEBUG
 8065 +		       "hardpps: last=%ld:%09ld, this=%ld:%09ld (%ld nsec)\n",
 8066 +		       pps.tf[1].tv_sec % 10, pps.tf[1].tv_nsec,
 8067 +		       pps.tf[0].tv_sec % 10, pps.tf[0].tv_nsec, u_nsec);
 8068 +#endif
 8069 +		/* Not in reference implementation: The pulse is already in
 8070 +		 * the filter stages, therefore we can't simply ignore it.
 8071 +		 * Instead we discard the current data and restart the
 8072 +		 * calibration interval.  Don't increment jitcnt.
 8073 +		 */
 8074 +		pps.lastsec = pps.tf[0].tv_sec;
 8075 +		pps.fcount = 0;
 8076 +		if (--pps.intcnt <= -4) {
 8077 +			pps.intcnt = -4;
 8078 +			if (pps.shift > PPS_FAVG) {
 8079 +				pps.shift--;
 8080 +				pps.intcnt = 0;
 8081 +			}
 8082 +		}
 8083 +		goto done;
 8084 +	}
 8085 +	pps.valid = PPS_VALID;
 8086 +	time_status |= STA_PPSSIGNAL;
 8087 +	time_status &= ~STA_PPSJITTER;
 8088 +
 8089 +	/*
 8090 +	 * A three-stage median filter is used to help denoise the PPS
 8091 +	 * time. The median sample becomes the time offset estimate; the
 8092 +	 * difference between the other two samples becomes the time
 8093 +	 * dispersion (jitter) estimate.
 8094 +	 */
 8095 +	if (pps.tf[0].tv_nsec > pps.tf[1].tv_nsec) {
 8096 +		if (pps.tf[1].tv_nsec > pps.tf[2].tv_nsec) {
 8097 +			v_nsec = pps.tf[1].tv_nsec;	/* 0 1 2 */
 8098 +			u_nsec = pps.tf[0].tv_nsec - pps.tf[2].tv_nsec;
 8099 +		} else if (pps.tf[2].tv_nsec > pps.tf[0].tv_nsec) {
 8100 +			v_nsec = pps.tf[0].tv_nsec;	/* 2 0 1 */
 8101 +			u_nsec = pps.tf[2].tv_nsec - pps.tf[1].tv_nsec;
 8102 +		} else {
 8103 +			v_nsec = pps.tf[2].tv_nsec;	/* 0 2 1 */
 8104 +			u_nsec = pps.tf[0].tv_nsec - pps.tf[1].tv_nsec;
 8105 +		}
 8106 +	} else {
 8107 +		if (pps.tf[1].tv_nsec < pps.tf[2].tv_nsec) {
 8108 +			v_nsec = pps.tf[1].tv_nsec;	/* 2 1 0 */
 8109 +			u_nsec = pps.tf[2].tv_nsec - pps.tf[0].tv_nsec;
 8110 +		} else  if (pps.tf[2].tv_nsec < pps.tf[0].tv_nsec) {
 8111 +			v_nsec = pps.tf[0].tv_nsec;	/* 1 0 2 */
 8112 +			u_nsec = pps.tf[1].tv_nsec - pps.tf[2].tv_nsec;
 8113 +		} else {
 8114 +			v_nsec = pps.tf[2].tv_nsec;	/* 1 2 0 */
 8115 +			u_nsec = pps.tf[1].tv_nsec - pps.tf[0].tv_nsec;
 8116 +		}
 8117 +	}
 8118 +
 8119 +	/*
 8120 +	 * Nominal jitter is due to PPS signal noise and interrupt
 8121 +	 * latency. If it exceeds the popcorn threshold, the sample is
 8122 +	 * discarded. otherwise, if so enabled, the time offset is
 8123 +	 * updated. We can tolerate a modest loss of data here without
 8124 +	 * much degrading time accuracy.
 8125 +	 */
 8126 +	if (u_nsec > (pps.jitter << PPS_POPCORN)) {
 8127 +#ifdef CONFIG_NTP_PPS_DEBUG
 8128 +		printk(KERN_INFO
 8129 +		       "hardpps: PPSJITTER: jitter=%ld, limit=%ld\n",
 8130 +		       u_nsec, (pps.jitter << PPS_POPCORN));
 8131 +#endif
 8132 +		time_status |= STA_PPSJITTER;
 8133 +		pps.jitcnt++;
 8134 +	} else if (time_status & STA_PPSTIME) {
 8135 +		time_monitor = -v_nsec;
 8136 +		L_LINT(time_offset, time_monitor);
 8137 +		time_adjust = 0;	/* cancel running adjtime() */
 8138 +#ifdef CONFIG_NTP_PPS_DEBUG
 8139 +		printk(KERN_DEBUG
 8140 +		       "hardpps: PPSTIME monitor=%ld, jitter=%ld\n",
 8141 +		       time_monitor, pps.jitter);
 8142 +#endif
 8143 +	}
 8144 +	pps.jitter += (u_nsec - pps.jitter) >> PPS_FAVG;
 8145 +	u_sec = pps.tf[0].tv_sec - pps.lastsec;
 8146 +	/* (The first pulse after a pause will always end a calibration
 8147 +	 * interval.  The workaround is probably not worth the extra cycles.)
 8148 +	 */
 8149 +	if (u_sec < (1 << pps.shift))
 8150 +		goto done;
 8151 +	/*
 8152 +	 * At the end of the calibration interval the difference between
 8153 +	 * the first and last counter values becomes the scaled
 8154 +	 * frequency. It will later be divided by the length of the
 8155 +	 * interval to determine the frequency update. If the frequency
 8156 +	 * exceeds a sanity threshold, or if the actual calibration
 8157 +	 * interval is not equal to the expected length, the data are
 8158 +	 * discarded. We can tolerate a modest loss of data here without
 8159 +	 * degrading frequency accuracy.
 8160 +	 */
 8161 +	pps.calcnt++;
 8162 +	v_nsec = -pps.fcount;
 8163 +	pps.lastsec = pps.tf[0].tv_sec;
 8164 +	pps.fcount = 0;
 8165 +	u_nsec = MAXFREQ << pps.shift;
 8166 +	if (v_nsec > u_nsec || v_nsec < -u_nsec || u_sec != (1 << pps.shift)) {
 8167 +#ifdef CONFIG_NTP_PPS_DEBUG
 8168 +		printk(KERN_NOTICE
 8169 +		       "hardpps: PPSERROR limit=%ld, fcount=%ld, len=%ld\n",
 8170 +		       u_nsec, v_nsec, u_sec);
 8171 +#endif
 8172 +		time_status |= STA_PPSERROR;
 8173 +		pps.errcnt++;
 8174 +		/* not in reference implementation */
 8175 +		if (--pps.intcnt <= -4) {
 8176 +			pps.intcnt = -4;
 8177 +			if (pps.shift > PPS_FAVG) {
 8178 +				pps.shift--;
 8179 +				pps.intcnt = 0;
 8180 +			}
 8181 +		}
 8182 +		goto done;
 8183 +	}
 8184 +
 8185 +	/*
 8186 +	 * Here the raw frequency offset and wander (stability) is
 8187 +	 * calculated. If the wander is less than the wander threshold
 8188 +	 * for four consecutive averaging intervals, the interval is
 8189 +	 * doubled; if it is greater than the threshold for four
 8190 +	 * consecutive intervals, the interval is halved. The scaled
 8191 +	 * frequency offset is converted to frequency offset. The
 8192 +	 * stability metric is calculated as the average of recent
 8193 +	 * frequency changes, but is used only for performance
 8194 +	 * monitoring.
 8195 +	 */
 8196 +	L_LINT(ftemp, v_nsec);
 8197 +	L_RSHIFT(ftemp, pps.shift);
 8198 +	L_SUB(ftemp, pps.freq);
 8199 +	u_nsec = L_GINT(ftemp);
 8200 +	fr_upd = 0;
 8201 +#ifdef CONFIG_NTP_PPS_DEBUG
 8202 +	printk(KERN_INFO "hardpps: new frequency %ld >> %d == %ld\n",
 8203 +	       v_nsec, pps.shift, v_nsec >> pps.shift);
 8204 +#endif
 8205 +	/* Not in reference implementation: Use dynamic limit (i.e. stability)
 8206 +	 * for maximum wander. Approximate SCALE_PPM (65536 / 1000) by 64.
 8207 +	 */
 8208 +	v_nsec = (pps.stabil >> 6) << PPS_POPCORN;
 8209 +	if (v_nsec == 0 || v_nsec > PPS_MAXWANDER)
 8210 +		v_nsec = PPS_MAXWANDER;
 8211 +#ifdef CONFIG_NTP_PPS_DEBUG
 8212 +	printk(KERN_DEBUG "hardpps: frequency change=%ld (limit %ld)\n",
 8213 +	       u_nsec, v_nsec);
 8214 +#endif
 8215 +	if (u_nsec > v_nsec || u_nsec < -v_nsec) {
 8216 +#ifdef CONFIG_NTP_PPS_DEBUG
 8217 +		printk(KERN_NOTICE
 8218 +		       "hardpps: PPSWANDER: change=%ld, limit=%ld, shift=%d\n",
 8219 +		       u_nsec, v_nsec, pps.shift);
 8220 +#endif
 8221 +		L_LINT(ftemp, u_nsec > 0 ? v_nsec : -v_nsec);
 8222 +		time_status |= STA_PPSWANDER;
 8223 +		pps.stbcnt++;
 8224 +		if (--pps.intcnt <= -4) {
 8225 +			pps.intcnt = -4;
 8226 +			if (pps.shift > PPS_FAVG) {
 8227 +				pps.shift--;
 8228 +				pps.intcnt = 0;
 8229 +			}
 8230 +		}
 8231 +	} else {	/* good sample */
 8232 +		/* Update frequency error estimate in rather stable
 8233 +                   situations.
 8234 +		*/
 8235 +		if (pps.shift > PPS_FAVG + 1)
 8236 +			fr_upd = 1;
 8237 +		if (++pps.intcnt >= 4) {
 8238 +			pps.intcnt = 4;
 8239 +			if (pps.shift < pps.shiftmax) {
 8240 +				pps.shift++;
 8241 +				pps.intcnt = 0;
 8242 +			}
 8243 +		}
 8244 +	}
 8245 +	if (u_nsec < 0)
 8246 +		u_nsec = -u_nsec;
 8247 +	pps.stabil += (u_nsec * SCALE_PPM - pps.stabil) >> PPS_FAVG;
 8248 +
 8249 +	/*
 8250 +	 * The PPS frequency is recalculated and clamped to the maximum
 8251 +	 * MAXFREQ. If enabled, the system clock frequency is updated as
 8252 +	 * well.
 8253 +	 */
 8254 +	L_ADD(pps.freq, ftemp);
 8255 +	u_nsec = L_GINT(pps.freq);
 8256 +	if (u_nsec > MAXFREQ)
 8257 +		L_LINT(pps.freq, MAXFREQ);
 8258 +	else if (u_nsec < -MAXFREQ)
 8259 +		L_LINT(pps.freq, -MAXFREQ);
 8260 +	else if (fr_upd) {
 8261 +		/* pps.frange is the maximum difference of the
 8262 +		   observed frequency values including (in-)stability.
 8263 +		   Otherwise it's a pessimistic worst-case estimate.
 8264 +		*/
 8265 +		if (u_nsec - (pps.stabil >> 7) < pps.fmin)
 8266 +			pps.fmin = u_nsec - (pps.stabil >> 7);
 8267 +		if (u_nsec + (pps.stabil >> 7)> pps.fmax)
 8268 +			pps.fmax = u_nsec + (pps.stabil >> 7);
 8269 +		pps.frange = pps.fmax - pps.fmin;
 8270 +		if (pps.frange > MAXFREQ) {
 8271 +			printk(KERN_WARNING
 8272 +			       "hardpps: pps.frange %ld exceeded MAXFREQ\n",
 8273 +			       pps.frange);
 8274 +#ifdef CONFIG_NTP_PPS_DEBUG
 8275 +			printk(KERN_INFO "hardpps: frange=%ld (%ld:%ld)\n",
 8276 +			       pps.frange, pps.fmin, pps.fmax);
 8277 +#endif
 8278 +			/* force recalculation on next update */
 8279 +			pps.fmin = MAXFREQ;
 8280 +			pps.fmax = -MAXFREQ;
 8281 +			pps.frange = MAXFREQ;
 8282 +		}
 8283 +	}
 8284 +#ifdef CONFIG_NTP_PPS_DEBUG
 8285 +	printk(KERN_INFO "hardpps: new pps.freq=%ld (add %ld)\n",
 8286 +	       L_GINT(pps.freq), L_GINT(ftemp));
 8287 +#endif
 8288 +	if ((time_status & STA_PPSFREQ) != 0 &&
 8289 +	    (time_status & STA_FREQHOLD) == 0)
 8290 +	{
 8291 +		time_freq = pps.freq;
 8292 +		update_nanoscale(time_tick * hz - NANOSECOND +
 8293 +				 L_GINT(time_freq));
 8294 +	}
 8295 +done:
 8296 +	write_unlock_irqrestore(&xtime_lock, flags);
 8297 +#ifdef CONFIG_NTP_PPS_DEBUG
 8298 +#ifdef CONFIG_X86_TSC
 8299 +	/* read Pentium cycle counter */
 8300 +	rdtscl(end_cc.low);
 8301 +	cc = end_cc.low - start_cc.low;
 8302 +	if (cc > cc_max) {
 8303 +		cc_max = cc;
 8304 +		printk(KERN_DEBUG "hardpps: new max CC = %lu\n", cc_max);
 8305 +	}
 8306 +	if (cc < cc_min) {
 8307 +		cc_min = cc;
 8308 +		printk(KERN_DEBUG "hardpps: new min CC = %lu\n", cc_min);
 8309 +	}
 8310 +#endif
 8311 +#endif
 8312 +}
 8313 +#endif	/* CONFIG_NTP_PPS */
 8314  
 8315 -int pps_shift = PPS_SHIFT;	/* interval duration (s) (shift) */
 8316 +#ifdef CONFIG_NTP
 8317 +/*
 8318 + * hardupdate() - local clock update
 8319 + *
 8320 + * This routine is called by adjtimex() to update the local clock
 8321 + * phase and frequency. The implementation is of an adaptive-parameter,
 8322 + * hybrid phase/frequency-lock loop (PLL/FLL). The routine computes new
 8323 + * time and frequency offset estimates for each call. If the kernel PPS
 8324 + * discipline code is configured (CONFIG_NTP_PPS), the PPS signal itself
 8325 + * determines the new time offset, instead of the calling argument.
 8326 + * Presumably, calls to ntp_adjtime() occur only when the caller
 8327 + * believes the local clock is valid within some bound (+-128 ms with
 8328 + * NTP). If the caller's time is far different than the PPS time, an
 8329 + * argument will ensue, and it's not clear who will lose.
 8330 + *
 8331 + * For uncompensated quartz crystal oscillators and nominal update
 8332 + * intervals less than 256 s, operation should be in phase-lock mode,
 8333 + * where the loop is disciplined to phase. For update intervals greater
 8334 + * than 1024 s, operation should be in frequency-lock mode, where the
 8335 + * loop is disciplined to frequency. Between 256 s and 1024 s, the mode
 8336 + * is selected by the STA_MODE status bit.
 8337 + */
 8338 +static inline void hardupdate(long offset)
 8339 +{
 8340 +	long mtemp;
 8341 +	l_fp ftemp;
 8342  
 8343 -long pps_jitcnt;		/* jitter limit exceeded */
 8344 -long pps_calcnt;		/* calibration intervals */
 8345 -long pps_errcnt;		/* calibration errors */
 8346 -long pps_stbcnt;		/* stability limit exceeded */
 8347 +	/*
 8348 +	 * Select how the phase is to be controlled and from which
 8349 +	 * source. If the PPS signal is present and enabled to
 8350 +	 * discipline the time, the PPS offset is used; otherwise, the
 8351 +	 * argument offset is used.
 8352 +	 */
 8353 +	if (!(time_status & STA_PLL))
 8354 +		return;
 8355 +	if (!(time_status & STA_PPSTIME && time_status & STA_PPSSIGNAL)) {
 8356 +		if (offset > MAXPHASE)
 8357 +			time_monitor = MAXPHASE;
 8358 +		else if (offset < -MAXPHASE)
 8359 +			time_monitor = -MAXPHASE;
 8360 +		else
 8361 +			time_monitor = offset;
 8362 +		L_LINT(time_offset, time_monitor);
 8363 +	}
 8364  
 8365 -/* hook for a loadable hardpps kernel module */
 8366 -void (*hardpps_ptr)(struct timeval *);
 8367 +	/*
 8368 +	 * Select how the frequency is to be controlled and in which
 8369 +	 * mode (PLL or FLL). If the PPS signal is present and enabled
 8370 +	 * to discipline the frequency, the PPS frequency is used;
 8371 +	 * otherwise, the argument offset is used to compute it.
 8372 +	 */
 8373 +	if (time_status & STA_PPSFREQ && time_status & STA_PPSSIGNAL) {
 8374 +		time_reftime = xtime.tv_sec;
 8375 +		return;
 8376 +	}
 8377 +	if (time_status & STA_FREQHOLD || time_reftime == 0)
 8378 +		time_reftime = xtime.tv_sec;
 8379 +	mtemp = xtime.tv_sec - time_reftime;
 8380 +	L_LINT(ftemp, time_monitor);
 8381 +	L_RSHIFT(ftemp, (SHIFT_PLL + 2 + time_constant) << 1);
 8382 +	L_MPY(ftemp, mtemp);
 8383 +	L_ADD(time_freq, ftemp);
 8384 +	time_status &= ~STA_MODE;
 8385 +	if (mtemp >= MINSEC && (time_status & STA_FLL || mtemp > MAXSEC)) {
 8386 +		L_LINT(ftemp, (time_monitor << 4) / mtemp);
 8387 +		L_RSHIFT(ftemp, SHIFT_FLL + 4);
 8388 +		L_ADD(time_freq, ftemp);
 8389 +		time_status |= STA_MODE;
 8390 +	}
 8391 +	time_reftime = xtime.tv_sec;
 8392 +	if (L_GINT(time_freq) > MAXFREQ)
 8393 +		L_LINT(time_freq, MAXFREQ);
 8394 +	else if (L_GINT(time_freq) < -MAXFREQ)
 8395 +		L_LINT(time_freq, -MAXFREQ);
 8396 +	update_nanoscale(time_tick * hz - NANOSECOND + L_GINT(time_freq));
 8397 +}
 8398 +#endif /* CONFIG_NTP */
 8399  
 8400  /* adjtimex mainly allows reading (and writing, if superuser) of
 8401 - * kernel time-keeping variables. used by xntpd.
 8402 + * kernel time-keeping variables. used by NTP.
 8403   */
 8404  int do_adjtimex(struct timex *txc)
 8405  {
 8406 -        long ltemp, mtemp, save_adjust;
 8407 +	long save_adjust;
 8408  	int result;
 8409  
 8410 +#if 1	/* complain about not configured mode bits */
 8411 +	if (txc->modes & ~ADJ_SERVED_MODE_BITS)
 8412 +	{
 8413 +		static int complain = 5;
 8414 +		if (complain) {
 8415 +			--complain;
 8416 +			printk(KERN_NOTICE
 8417 +			       "adjtimex: %s used unsupported ADJ bits 0x%x\n",
 8418 +			       current->comm,
 8419 +			       txc->modes & ~ADJ_SERVED_MODE_BITS);
 8420 +		}
 8421 +	}
 8422 +#endif
 8423  	/* In order to modify anything, you gotta be super-user! */
 8424  	if (txc->modes && !capable(CAP_SYS_TIME))
 8425  		return -EPERM;
 8426  		
 8427  	/* Now we validate the data before disabling interrupts */
 8428  
 8429 -	if ((txc->modes & ADJ_OFFSET_SINGLESHOT) == ADJ_OFFSET_SINGLESHOT)
 8430 -	  /* singleshot must not be used with any other mode bits */
 8431 -		if (txc->modes != ADJ_OFFSET_SINGLESHOT)
 8432 -			return -EINVAL;
 8433 -
 8434 -	if (txc->modes != ADJ_OFFSET_SINGLESHOT && (txc->modes & ADJ_OFFSET))
 8435 -	  /* adjustment Offset limited to +- .512 seconds */
 8436 -		if (txc->offset <= - MAXPHASE || txc->offset >= MAXPHASE )
 8437 -			return -EINVAL;	
 8438 -
 8439 -	/* if the quartz is off by more than 10% something is VERY wrong ! */
 8440 -	if (txc->modes & ADJ_TICK)
 8441 -		if (txc->tick < 900000/HZ || txc->tick > 1100000/HZ)
 8442 -			return -EINVAL;
 8443 +#if 1	/* Map old method to new one; this will vanish soon */
 8444 +#define	OLD_ADJTIME	(MOD_OFFSET|MOD_CLKA)
 8445 +	if ((txc->modes & OLD_ADJTIME) == OLD_ADJTIME) {
 8446 +		static int complain = 5;
 8447 +		txc->modes ^= OLD_ADJTIME;
 8448 +		txc->modes |= ADJ_ADJTIME;
 8449 +		if (complain) {
 8450 +			--complain;
 8451 +			printk(KERN_WARNING
 8452 +			       "adjtime: %s used obsolete ADJ_OFFSET_SINGLESHOT\n",
 8453 +			       current->comm);
 8454 +		}
 8455 +	}
 8456 +#undef	OLD_ADJTIME
 8457 +#if 1	/* Establish extra warnings for older applications */
 8458 +#define	OLD_ADJTICK	(MOD_CLKB)
 8459 +	if ((txc->modes & OLD_ADJTICK) == OLD_ADJTICK) {
 8460 +		static int complain = 5;
 8461 +		if (complain) {
 8462 +			--complain;
 8463 +			printk(KERN_NOTICE
 8464 +			       "adjtimex: %s may be using obsolete ADJ_TICK\n",
 8465 +			       current->comm);
 8466 +		}
 8467 +	}
 8468 +#undef	OLD_ADJTICK
 8469 +#define	OLD_ADJTICKADJ	(MOD_NANO)
 8470 +	if ((txc->modes & OLD_ADJTICKADJ) == OLD_ADJTICKADJ) {
 8471 +		static int complain = 5;
 8472 +		if (complain) {
 8473 +			--complain;
 8474 +			printk(KERN_NOTICE
 8475 +			       "adjtimex: %s could be using obsolete ADJ_TICKADJ\n",
 8476 +			       current->comm);
 8477 +		}
 8478 +	}
 8479 +#undef	OLD_ADJTICKADJ
 8480 +#endif
 8481 +#endif
 8482 +	if ((txc->modes & MOD_OFFSET) != 0 &&
 8483 +	    (txc->modes & ADJ_ADJTIME) != 0) {
 8484 +		/* ntp_adjtime() and adjtime() are mutually exclusive! */
 8485 +		return -EINVAL;
 8486 +	}
 8487  
 8488  	write_lock_irq(&xtime_lock);
 8489 -	result = time_state;	/* mostly `TIME_OK' */
 8490  
 8491 -	/* Save for later - semantics of adjtime is to return old value */
 8492 -	save_adjust = time_adjust;
 8493 +#ifdef CONFIG_NTP
 8494 +	result = time_state;	/* mostly `TIME_OK' */
 8495 +#else
 8496 +	result = 0;
 8497 +#endif
 8498  
 8499 -#if 0	/* STA_CLOCKERR is never set yet */
 8500 +#if defined(CONFIG_NTP) && 0	/* STA_CLOCKERR is never set yet */
 8501  	time_status &= ~STA_CLOCKERR;		/* reset STA_CLOCKERR */
 8502  #endif
 8503  	/* If there are input parameters, then process them */
 8504  	if (txc->modes)
 8505  	{
 8506 -	    if (txc->modes & ADJ_STATUS)	/* only set allowed bits */
 8507 -		time_status =  (txc->status & ~STA_RONLY) |
 8508 -			      (time_status & STA_RONLY);
 8509 -
 8510 -	    if (txc->modes & ADJ_FREQUENCY) {	/* p. 22 */
 8511 -		if (txc->freq > MAXFREQ || txc->freq < -MAXFREQ) {
 8512 -		    result = -EINVAL;
 8513 -		    goto leave;
 8514 -		}
 8515 -		time_freq = txc->freq - pps_freq;
 8516 -	    }
 8517 -
 8518 -	    if (txc->modes & ADJ_MAXERROR) {
 8519 -		if (txc->maxerror < 0 || txc->maxerror >= NTP_PHASE_LIMIT) {
 8520 -		    result = -EINVAL;
 8521 -		    goto leave;
 8522 -		}
 8523 -		time_maxerror = txc->maxerror;
 8524 -	    }
 8525 -
 8526 -	    if (txc->modes & ADJ_ESTERROR) {
 8527 -		if (txc->esterror < 0 || txc->esterror >= NTP_PHASE_LIMIT) {
 8528 -		    result = -EINVAL;
 8529 -		    goto leave;
 8530 -		}
 8531 -		time_esterror = txc->esterror;
 8532 -	    }
 8533 -
 8534 -	    if (txc->modes & ADJ_TIMECONST) {	/* p. 24 */
 8535 -		if (txc->constant < 0) {	/* NTP v4 uses values > 6 */
 8536 -		    result = -EINVAL;
 8537 -		    goto leave;
 8538 -		}
 8539 -		time_constant = txc->constant;
 8540 -	    }
 8541 -
 8542 -	    if (txc->modes & ADJ_OFFSET) {	/* values checked earlier */
 8543 -		if (txc->modes == ADJ_OFFSET_SINGLESHOT) {
 8544 -		    /* adjtime() is independent from ntp_adjtime() */
 8545 -		    time_adjust = txc->offset;
 8546 -		}
 8547 -		else if ( time_status & (STA_PLL | STA_PPSTIME) ) {
 8548 -		    ltemp = (time_status & (STA_PPSTIME | STA_PPSSIGNAL)) ==
 8549 -		            (STA_PPSTIME | STA_PPSSIGNAL) ?
 8550 -		            pps_offset : txc->offset;
 8551 -
 8552 -		    /*
 8553 -		     * Scale the phase adjustment and
 8554 -		     * clamp to the operating range.
 8555 -		     */
 8556 -		    if (ltemp > MAXPHASE)
 8557 -		        time_offset = MAXPHASE << SHIFT_UPDATE;
 8558 -		    else if (ltemp < -MAXPHASE)
 8559 -			time_offset = -(MAXPHASE << SHIFT_UPDATE);
 8560 -		    else
 8561 -		        time_offset = ltemp << SHIFT_UPDATE;
 8562 -
 8563 -		    /*
 8564 -		     * Select whether the frequency is to be controlled
 8565 -		     * and in which mode (PLL or FLL). Clamp to the operating
 8566 -		     * range. Ugly multiply/divide should be replaced someday.
 8567 -		     */
 8568 -
 8569 -		    if (time_status & STA_FREQHOLD || time_reftime == 0)
 8570 -		        time_reftime = xtime.tv_sec;
 8571 -		    mtemp = xtime.tv_sec - time_reftime;
 8572 -		    time_reftime = xtime.tv_sec;
 8573 -		    if (time_status & STA_FLL) {
 8574 -		        if (mtemp >= MINSEC) {
 8575 -			    ltemp = (time_offset / mtemp) << (SHIFT_USEC -
 8576 -							      SHIFT_UPDATE);
 8577 -			    if (ltemp < 0)
 8578 -			        time_freq -= -ltemp >> SHIFT_KH;
 8579 -			    else
 8580 -			        time_freq += ltemp >> SHIFT_KH;
 8581 -			} else /* calibration interval too short (p. 12) */
 8582 -				result = TIME_ERROR;
 8583 -		    } else {	/* PLL mode */
 8584 -		        if (mtemp < MAXSEC) {
 8585 -			    ltemp *= mtemp;
 8586 -			    if (ltemp < 0)
 8587 -			        time_freq -= -ltemp >> (time_constant +
 8588 -							time_constant +
 8589 -							SHIFT_KF - SHIFT_USEC);
 8590 -			    else
 8591 -			        time_freq += ltemp >> (time_constant +
 8592 -						       time_constant +
 8593 -						       SHIFT_KF - SHIFT_USEC);
 8594 -			} else /* calibration interval too long (p. 12) */
 8595 -				result = TIME_ERROR;
 8596 -		    }
 8597 -		    if (time_freq > time_tolerance)
 8598 -		        time_freq = time_tolerance;
 8599 -		    else if (time_freq < -time_tolerance)
 8600 -		        time_freq = -time_tolerance;
 8601 -		} /* STA_PLL || STA_PPSTIME */
 8602 -	    } /* txc->modes & ADJ_OFFSET */
 8603 -	    if (txc->modes & ADJ_TICK) {
 8604 -		/* if the quartz is off by more than 10% something is
 8605 -		   VERY wrong ! */
 8606 -		if (txc->tick < 900000/HZ || txc->tick > 1100000/HZ) {
 8607 -		    result = -EINVAL;
 8608 -		    goto leave;
 8609 +#ifdef CONFIG_NTP
 8610 +		if (txc->modes & MOD_STATUS) {  /* only modify allowed bits */
 8611 +			if ((time_status & STA_PLL) != 0 &&
 8612 +			    (txc->status & STA_PLL) == 0) {
 8613 +				/* If the STA_PLL bit in the status word is
 8614 +				 * cleared, the state and status words are
 8615 +				 * reset to the initial values.
 8616 +				 */
 8617 +				time_state = TIME_OK;
 8618 +				time_status = STA_UNSYNC;
 8619 +#ifdef CONFIG_NTP_PPS
 8620 +				/* the PPS calibration interval may end
 8621 +				   surprisingly early */
 8622 +				pps.shift = PPS_FAVG;
 8623 +				pps.intcnt = 0;
 8624 +#endif
 8625 +			}
 8626 +			time_status =  (txc->status & ~STA_RONLY) |
 8627 +				(time_status & STA_RONLY);
 8628 +		}
 8629 +
 8630 +		if (txc->modes & MOD_FREQUENCY) {	/* p. 22 */
 8631 +			long freq;		/* frequency ns/s) */
 8632 +			freq = txc->freq / SCALE_PPM;
 8633 +			if (freq > MAXFREQ) {
 8634 +				result = -EINVAL;
 8635 +				freq = MAXFREQ;
 8636 +			} else if (freq < -MAXFREQ) {
 8637 +				result = -EINVAL;
 8638 +				freq = -MAXFREQ;
 8639 +			}
 8640 +			L_LINT(time_freq, freq);
 8641 +			update_nanoscale(time_tick * hz - NANOSECOND +
 8642 +					 L_GINT(time_freq));
 8643 +#ifdef CONFIG_NTP_PPS
 8644 +			pps.freq = time_freq;
 8645 +#endif
 8646 +		}
 8647 +
 8648 +		if (txc->modes & MOD_MAXERROR) {
 8649 +			txc->maxerror *= 1000;	/* convert to ns */
 8650 +			if (txc->maxerror < 0
 8651 +			    || txc->maxerror >= NTP_PHASE_LIMIT) {
 8652 +				result = -EINVAL;
 8653 +				txc->maxerror = NTP_PHASE_LIMIT;
 8654 +			}
 8655 +			time_maxerror = txc->maxerror;
 8656 +		}
 8657 +
 8658 +		if (txc->modes & MOD_ESTERROR) {
 8659 +			txc->esterror *= 1000;
 8660 +			if (txc->esterror < 0
 8661 +			    || txc->esterror >= NTP_PHASE_LIMIT) {
 8662 +				result = -EINVAL;
 8663 +				txc->esterror = NTP_PHASE_LIMIT;
 8664 +			}
 8665 +			time_esterror = txc->esterror;
 8666 +		}
 8667 +
 8668 +		/* Note that the timex.constant structure member has a
 8669 +		 * dual purpose to set the time constant and to set
 8670 +		 * the TAI offset.
 8671 +		 */
 8672 +		if (txc->modes & MOD_TIMECONST) {	/* p. 24 */
 8673 +			if (txc->constant < 0) {
 8674 +				result = -EINVAL;
 8675 +				txc->constant = 0;
 8676 +			} else if (txc->constant > MAXTC) {
 8677 +				result = -EINVAL;
 8678 +				txc->constant = MAXTC;
 8679 +			}
 8680 +			time_constant = txc->constant;
 8681 +		}
 8682 +		if (txc->modes & MOD_TAI) {
 8683 +			if (txc->constant > 0)
 8684 +				time_tai = txc->constant;
 8685 +			else
 8686 +				result = -EINVAL;
 8687 +		}
 8688 +#ifdef CONFIG_NTP_PPS
 8689 +		if (txc->modes & MOD_PPSMAX) {
 8690 +			if (txc->shift < PPS_FAVG) {
 8691 +				pps.shiftmax = PPS_FAVG;
 8692 +				result = -EINVAL;
 8693 +			} else if (txc->shift > PPS_FAVGMAX) {
 8694 +				pps.shiftmax = PPS_FAVGMAX;
 8695 +				result = -EINVAL;
 8696 +			} else
 8697 +				pps.shiftmax = txc->shift;
 8698 +			if (pps.shift > pps.shiftmax) {	/* enforce limit */
 8699 +				pps.shift = pps.shiftmax;
 8700 +			}
 8701 +		}
 8702 +#endif
 8703 +		if (txc->modes & MOD_NANO)
 8704 +			time_status |= STA_NANO;
 8705 +		if (txc->modes & MOD_MICRO)
 8706 +			time_status &= ~STA_NANO;
 8707 +		if (txc->modes & MOD_CLKB)
 8708 +			time_status |= STA_CLK;
 8709 +		if (txc->modes & MOD_CLKA)
 8710 +			time_status &= ~STA_CLK;
 8711 +		if (txc->modes & MOD_OFFSET) {
 8712 +			if ((time_status & STA_NANO) == 0) {
 8713 +				/* need nanoseconds, but avoid sign overflow */
 8714 +				if (txc->offset > MAXPHASE / 1000)
 8715 +					txc->offset = MAXPHASE;
 8716 +				else if (txc->offset < -MAXPHASE / 1000)
 8717 +					txc->offset = -MAXPHASE;
 8718 +				else
 8719 +					txc->offset *= 1000;
 8720 +			}
 8721 +			/* offset will be clamped silently in hardupdate() */
 8722 +			hardupdate(txc->offset);
 8723 +		}
 8724 +#endif /* CONFIG_NTP */
 8725 +		if (txc->modes == ADJ_ADJTIME) {
 8726 +			/*
 8727 +			 * The emulation of adjtime() is actually broken,
 8728 +			 * because only one ``long'' is available to represent
 8729 +			 * a ``struct timeval''.
 8730 +			 */
 8731 +			/* Save current adjustment to be retuned */
 8732 +			save_adjust = time_adjust;
 8733 +			time_adjust = txc->offset;
 8734 +			if (txc->offset < -30000000 || txc->offset > 30000000)
 8735 +				printk(KERN_WARNING
 8736 +				       "adjtime() fails with large offsets\n");
 8737 +		}
 8738 +		if (txc->modes & ADJ_TIMETICK) {
 8739 +#ifdef CONFIG_NTP
 8740 +			if ((time_status & STA_NANO) == 0)
 8741 +				txc->time_tick *= 1000;
 8742 +#endif
 8743 +			/* if the quartz is off by more than 10% something is
 8744 +			   VERY wrong ! */
 8745 +			if (txc->time_tick <  9 * (NANOSECOND/10/hz) ||
 8746 +			    txc->time_tick > 11 * (NANOSECOND/10/hz)) {
 8747 +				result = -EINVAL;
 8748 +				goto leave;
 8749 +			}
 8750 +			time_tick = txc->time_tick;
 8751 +#ifdef CONFIG_NTP
 8752 +			update_nanoscale(time_tick * hz - NANOSECOND +
 8753 +					 L_GINT(time_freq));
 8754 +#endif
 8755 +		}
 8756 +		if (txc->modes & ADJ_TICKADJ) {
 8757 +			/* Traditional UN*Xes seem to allow about ``tick/10'',
 8758 +			   but we will allow up to ``time_tick/4'' (scaled).
 8759 +			   Larger values might cause trouble!
 8760 +			*/
 8761 +			if (txc->tickadj <= 0 ||
 8762 +			    txc->tickadj > time_tick / 4) {
 8763 +				result = -EINVAL;
 8764 +				goto leave;
 8765 +			}
 8766 +			tickadj = txc->tickadj;
 8767  		}
 8768 -		tick = txc->tick;
 8769 -	    }
 8770  	} /* txc->modes */
 8771 -leave:	if ((time_status & (STA_UNSYNC|STA_CLOCKERR)) != 0
 8772 +leave:
 8773 +	if (txc->modes & ADJ_ADJTIME)
 8774 +		txc->offset = save_adjust;
 8775 +#ifdef CONFIG_NTP
 8776 +	else {
 8777 +		if (time_status & STA_NANO)
 8778 +			txc->offset = time_monitor;
 8779 +		else
 8780 +			txc->offset = time_monitor / 1000;
 8781 +	}
 8782 +	txc->freq	   = L_GINT(time_freq) * SCALE_PPM;
 8783 +	txc->maxerror	   = time_maxerror / 1000;
 8784 +	txc->esterror	   = time_esterror / 1000;
 8785 +	txc->tai	   = time_tai;
 8786 +	txc->status	   = time_status;
 8787 +	txc->constant	   = time_constant;
 8788 +	if (time_status & STA_NANO) {
 8789 +		txc->precision = time_precision ? : 1;
 8790 +		txc->time_tick = time_tick;
 8791 +	} else {
 8792 +		txc->time.tv_nsec += 500;	/* make microseconds */
 8793 +		txc->time.tv_nsec /= 1000;
 8794 +		txc->precision = ((time_precision + 500) / 1000) ? : 1;
 8795 +		txc->time_tick = (time_tick + 500) / 1000;
 8796 +	}
 8797 +	txc->tolerance = MAXFREQ * SCALE_PPM;
 8798 +#else
 8799 +	txc->time_tick	   = time_tick;
 8800 +#endif
 8801 +#ifdef CONFIG_NTP_PPS
 8802 +	txc->ppsfreq	   = L_GINT(pps.freq) * SCALE_PPM;
 8803 +	if (time_status & STA_NANO)
 8804 +		txc->jitter = pps.jitter;
 8805 +	else
 8806 +		txc->jitter = pps.jitter / 1000;
 8807 +	txc->shift	   = pps.shift;
 8808 +	txc->stabil	   = pps.stabil;
 8809 +	txc->jitcnt	   = pps.jitcnt;
 8810 +	txc->calcnt	   = pps.calcnt;
 8811 +	txc->errcnt	   = pps.errcnt;
 8812 +	txc->stbcnt	   = pps.stbcnt;
 8813 +#endif
 8814 +	txc->tickadj	   = tickadj;	/* extension by UW */
 8815 +
 8816 +#ifdef CONFIG_NTP
 8817 +	if ((time_status & (STA_UNSYNC|STA_CLOCKERR)) != 0
 8818 +	    /* clock not synchronized (hardware or software error) */
 8819 +#ifdef CONFIG_NTP_PPS
 8820  	    || ((time_status & (STA_PPSFREQ|STA_PPSTIME)) != 0
 8821  		&& (time_status & STA_PPSSIGNAL) == 0)
 8822 -	    /* p. 24, (b) */
 8823 +	    /* PPS signal lost when either PPS time or PPS frequency
 8824 +	     * synchronization requested
 8825 +	     */
 8826  	    || ((time_status & (STA_PPSTIME|STA_PPSJITTER))
 8827  		== (STA_PPSTIME|STA_PPSJITTER))
 8828 -	    /* p. 24, (c) */
 8829 +	    /* PPS jitter exceeded when PPS time synchronization requested */
 8830  	    || ((time_status & STA_PPSFREQ) != 0
 8831 -		&& (time_status & (STA_PPSWANDER|STA_PPSERROR)) != 0))
 8832 -	    /* p. 24, (d) */
 8833 +		&& (time_status & (STA_PPSWANDER|STA_PPSERROR)) != 0)
 8834 +	    /* PPS wander exceeded or calibration error when PPS frequency
 8835 +	     * synchronization requested
 8836 +	     */
 8837 +#endif
 8838 +	   )
 8839  		result = TIME_ERROR;
 8840 +#endif
 8841  	
 8842 -	if ((txc->modes & ADJ_OFFSET_SINGLESHOT) == ADJ_OFFSET_SINGLESHOT)
 8843 -	    txc->offset	   = save_adjust;
 8844 -	else {
 8845 -	    if (time_offset < 0)
 8846 -		txc->offset = -(-time_offset >> SHIFT_UPDATE);
 8847 -	    else
 8848 -		txc->offset = time_offset >> SHIFT_UPDATE;
 8849 -	}
 8850 -	txc->freq	   = time_freq + pps_freq;
 8851 -	txc->maxerror	   = time_maxerror;
 8852 -	txc->esterror	   = time_esterror;
 8853 -	txc->status	   = time_status;
 8854 -	txc->constant	   = time_constant;
 8855 -	txc->precision	   = time_precision;
 8856 -	txc->tolerance	   = time_tolerance;
 8857 -	txc->tick	   = tick;
 8858 -	txc->ppsfreq	   = pps_freq;
 8859 -	txc->jitter	   = pps_jitter >> PPS_AVG;
 8860 -	txc->shift	   = pps_shift;
 8861 -	txc->stabil	   = pps_stabil;
 8862 -	txc->jitcnt	   = pps_jitcnt;
 8863 -	txc->calcnt	   = pps_calcnt;
 8864 -	txc->errcnt	   = pps_errcnt;
 8865 -	txc->stbcnt	   = pps_stbcnt;
 8866  	write_unlock_irq(&xtime_lock);
 8867 -	do_gettimeofday(&txc->time);
 8868 +	do_clock_gettime(CLOCK_REALTIME, &txc->time);
 8869 +#ifdef CONFIG_NTP
 8870 +	if ((time_status & STA_NANO) == 0)
 8871 +		txc->time.tv_nsec /= 1000;
 8872 +#endif
 8873  	return(result);
 8874  }
 8875  
 8876 @@ -409,3 +1763,60 @@
 8877  	ret = do_adjtimex(&txc);
 8878  	return copy_to_user(txc_p, &txc, sizeof(struct timex)) ? -EFAULT : ret;
 8879  }
 8880 +
 8881 +/*
 8882 + * timevar_init() - initialize variables and structures
 8883 + * (needed before first time interrupt is processed)
 8884 + *
 8885 + * This routine must be called after the kernel variables hz and tick
 8886 + * are set or changed and before the next tick interrupt. In this
 8887 + * particular implementation, these values are assumed set elsewhere in
 8888 + * the kernel. The design allows the clock frequency and tick interval
 8889 + * to be changed while the system is running. So, this routine should
 8890 + * probably be integrated with the code that does that.
 8891 + */
 8892 +void timevar_init(void)
 8893 +{
 8894 +	struct timespec ts;
 8895 +
 8896 +	/*
 8897 +	 * The following variable must be initialized any time the
 8898 +	 * kernel variable hz is changed.
 8899 +	 */
 8900 +	time_tick = (NANOSECOND + hz/2) / hz;
 8901 +	/* Speed of adjtime() is 50ms/s; may not be <= 0 */
 8902 +	tickadj = 50000/hz ? : 1;
 8903 +	time_adjust = 0;
 8904 +	rtc_update = 660;			/* update every 11 minutes */
 8905 +
 8906 +	/*
 8907 +	 * The following variables are initialized only at startup. Only
 8908 +	 * those structures not cleared by the compiler need to be
 8909 +	 * initialized, and these only in the simulator. In the actual
 8910 +	 * kernel, any nonzero values here will quickly evaporate.
 8911 +	 */
 8912 +#ifdef CONFIG_NTP
 8913 +	time_state = TIME_OK;
 8914 +	time_status = STA_UNSYNC;
 8915 +	time_tai = 0;
 8916 +	time_maxerror = time_esterror = NTP_PHASE_LIMIT;
 8917 +	L_CLR(time_offset);
 8918 +	L_CLR(time_freq);
 8919 +	L_LINT(time_adj, NANOSECOND);		/* preset for first second */
 8920 +	L_CLR(time_phase);
 8921 +	do_clock_getres(CLOCK_REALTIME, &ts);
 8922 +	time_precision = ts.tv_nsec;
 8923 +#ifdef CONFIG_NTP_PPS
 8924 +	pps.shift = PPS_FAVG;
 8925 +	pps.shiftmax = PPS_FAVGDEF;
 8926 +	pps.tf[0].tv_sec = pps.tf[0].tv_nsec = 0;
 8927 +	pps.tf[1].tv_sec = pps.tf[1].tv_nsec = 0;
 8928 +	pps.tf[2].tv_sec = pps.tf[2].tv_nsec = 0;
 8929 +	pps.fcount = 0;
 8930 +	L_CLR(pps.freq);
 8931 +	pps.fmin = MAXFREQ;
 8932 +	pps.fmax = -MAXFREQ;
 8933 +	pps.frange = MAXFREQ;
 8934 +#endif
 8935 +#endif
 8936 +}
 8937 Index: linux/kernel/timer.c
 8938 diff -u linux/kernel/timer.c:1.1.1.9 linux/kernel/timer.c:1.1.1.9.2.1
 8939 --- linux/kernel/timer.c:1.1.1.9	Sat Aug 26 21:35:37 2006
 8940 +++ linux/kernel/timer.c	Sat Aug 26 21:50:52 2006
 8941 @@ -17,7 +17,7 @@
 8942  
 8943  #include <linux/config.h>
 8944  #include <linux/mm.h>
 8945 -#include <linux/timex.h>
 8946 +#include <linux/time.h>
 8947  #include <linux/delay.h>
 8948  #include <linux/smp_lock.h>
 8949  #include <linux/interrupt.h>
 8950 @@ -25,42 +25,9 @@
 8951  
 8952  #include <asm/uaccess.h>
 8953  
 8954 -/*
 8955 - * Timekeeping variables
 8956 - */
 8957 -
 8958 -long tick = (1000000 + HZ/2) / HZ;	/* timer interrupt period */
 8959 -
 8960 -/* The current time */
 8961 -struct timeval xtime __attribute__ ((aligned (16)));
 8962 -
 8963 -/* Don't completely fail for HZ > 500.  */
 8964 -int tickadj = 500/HZ ? : 1;		/* microsecs */
 8965 -
 8966  DECLARE_TASK_QUEUE(tq_timer);
 8967  DECLARE_TASK_QUEUE(tq_immediate);
 8968  
 8969 -/*
 8970 - * phase-lock loop variables
 8971 - */
 8972 -/* TIME_ERROR prevents overwriting the CMOS clock */
 8973 -int time_state = TIME_OK;		/* clock synchronization status	*/
 8974 -int time_status = STA_UNSYNC;		/* clock status bits		*/
 8975 -long time_offset;			/* time adjustment (us)		*/
 8976 -long time_constant = 2;			/* pll time constant		*/
 8977 -long time_tolerance = MAXFREQ;		/* frequency tolerance (ppm)	*/
 8978 -long time_precision = 1;		/* clock precision (us)		*/
 8979 -long time_maxerror = NTP_PHASE_LIMIT;	/* maximum error (us)		*/
 8980 -long time_esterror = NTP_PHASE_LIMIT;	/* estimated error (us)		*/
 8981 -long time_phase;			/* phase offset (scaled us)	*/
 8982 -long time_freq = ((1000000 + HZ/2) % HZ - HZ/2) << SHIFT_USEC;
 8983 -					/* frequency offset (scaled ppm)*/
 8984 -long time_adj;				/* tick adjust (scaled 1 / HZ)	*/
 8985 -long time_reftime;			/* time at last adjustment (s)	*/
 8986 -
 8987 -long time_adjust;
 8988 -long time_adjust_step;
 8989 -
 8990  unsigned long event;
 8991  
 8992  extern int do_setitimer(int, struct itimerval *, struct itimerval *);
 8993 @@ -351,189 +318,6 @@
 8994  	run_task_queue(&tq_immediate);
 8995  }
 8996  
 8997 -/*
 8998 - * this routine handles the overflow of the microsecond field
 8999 - *
 9000 - * The tricky bits of code to handle the accurate clock support
 9001 - * were provided by Dave Mills (Mills@UDEL.EDU) of NTP fame.
 9002 - * They were originally developed for SUN and DEC kernels.
 9003 - * All the kudos should go to Dave for this stuff.
 9004 - *
 9005 - */
 9006 -static void second_overflow(void)
 9007 -{
 9008 -    long ltemp;
 9009 -
 9010 -    /* Bump the maxerror field */
 9011 -    time_maxerror += time_tolerance >> SHIFT_USEC;
 9012 -    if ( time_maxerror > NTP_PHASE_LIMIT ) {
 9013 -	time_maxerror = NTP_PHASE_LIMIT;
 9014 -	time_status |= STA_UNSYNC;
 9015 -    }
 9016 -
 9017 -    /*
 9018 -     * Leap second processing. If in leap-insert state at
 9019 -     * the end of the day, the system clock is set back one
 9020 -     * second; if in leap-delete state, the system clock is
 9021 -     * set ahead one second. The microtime() routine or
 9022 -     * external clock driver will insure that reported time
 9023 -     * is always monotonic. The ugly divides should be
 9024 -     * replaced.
 9025 -     */
 9026 -    switch (time_state) {
 9027 -
 9028 -    case TIME_OK:
 9029 -	if (time_status & STA_INS)
 9030 -	    time_state = TIME_INS;
 9031 -	else if (time_status & STA_DEL)
 9032 -	    time_state = TIME_DEL;
 9033 -	break;
 9034 -
 9035 -    case TIME_INS:
 9036 -	if (xtime.tv_sec % 86400 == 0) {
 9037 -	    xtime.tv_sec--;
 9038 -	    time_state = TIME_OOP;
 9039 -	    printk(KERN_NOTICE "Clock: inserting leap second 23:59:60 UTC\n");
 9040 -	}
 9041 -	break;
 9042 -
 9043 -    case TIME_DEL:
 9044 -	if ((xtime.tv_sec + 1) % 86400 == 0) {
 9045 -	    xtime.tv_sec++;
 9046 -	    time_state = TIME_WAIT;
 9047 -	    printk(KERN_NOTICE "Clock: deleting leap second 23:59:59 UTC\n");
 9048 -	}
 9049 -	break;
 9050 -
 9051 -    case TIME_OOP:
 9052 -	time_state = TIME_WAIT;
 9053 -	break;
 9054 -
 9055 -    case TIME_WAIT:
 9056 -	if (!(time_status & (STA_INS | STA_DEL)))
 9057 -	    time_state = TIME_OK;
 9058 -    }
 9059 -
 9060 -    /*
 9061 -     * Compute the phase adjustment for the next second. In
 9062 -     * PLL mode, the offset is reduced by a fixed factor
 9063 -     * times the time constant. In FLL mode the offset is
 9064 -     * used directly. In either mode, the maximum phase
 9065 -     * adjustment for each second is clamped so as to spread
 9066 -     * the adjustment over not more than the number of
 9067 -     * seconds between updates.
 9068 -     */
 9069 -    if (time_offset < 0) {
 9070 -	ltemp = -time_offset;
 9071 -	if (!(time_status & STA_FLL))
 9072 -	    ltemp >>= SHIFT_KG + time_constant;
 9073 -	if (ltemp > (MAXPHASE / MINSEC) << SHIFT_UPDATE)
 9074 -	    ltemp = (MAXPHASE / MINSEC) << SHIFT_UPDATE;
 9075 -	time_offset += ltemp;
 9076 -	time_adj = -ltemp << (SHIFT_SCALE - SHIFT_HZ - SHIFT_UPDATE);
 9077 -    } else {
 9078 -	ltemp = time_offset;
 9079 -	if (!(time_status & STA_FLL))
 9080 -	    ltemp >>= SHIFT_KG + time_constant;
 9081 -	if (ltemp > (MAXPHASE / MINSEC) << SHIFT_UPDATE)
 9082 -	    ltemp = (MAXPHASE / MINSEC) << SHIFT_UPDATE;
 9083 -	time_offset -= ltemp;
 9084 -	time_adj = ltemp << (SHIFT_SCALE - SHIFT_HZ - SHIFT_UPDATE);
 9085 -    }
 9086 -
 9087 -    /*
 9088 -     * Compute the frequency estimate and additional phase
 9089 -     * adjustment due to frequency error for the next
 9090 -     * second. When the PPS signal is engaged, gnaw on the
 9091 -     * watchdog counter and update the frequency computed by
 9092 -     * the pll and the PPS signal.
 9093 -     */
 9094 -    pps_valid++;
 9095 -    if (pps_valid == PPS_VALID) {	/* PPS signal lost */
 9096 -	pps_jitter = MAXTIME;
 9097 -	pps_stabil = MAXFREQ;
 9098 -	time_status &= ~(STA_PPSSIGNAL | STA_PPSJITTER |
 9099 -			 STA_PPSWANDER | STA_PPSERROR);
 9100 -    }
 9101 -    ltemp = time_freq + pps_freq;
 9102 -    if (ltemp < 0)
 9103 -	time_adj -= -ltemp >>
 9104 -	    (SHIFT_USEC + SHIFT_HZ - SHIFT_SCALE);
 9105 -    else
 9106 -	time_adj += ltemp >>
 9107 -	    (SHIFT_USEC + SHIFT_HZ - SHIFT_SCALE);
 9108 -
 9109 -#if HZ == 100
 9110 -    /* Compensate for (HZ==100) != (1 << SHIFT_HZ).
 9111 -     * Add 25% and 3.125% to get 128.125; => only 0.125% error (p. 14)
 9112 -     */
 9113 -    if (time_adj < 0)
 9114 -	time_adj -= (-time_adj >> 2) + (-time_adj >> 5);
 9115 -    else
 9116 -	time_adj += (time_adj >> 2) + (time_adj >> 5);
 9117 -#endif
 9118 -}
 9119 -
 9120 -/* in the NTP reference this is called "hardclock()" */
 9121 -static void update_wall_time_one_tick(void)
 9122 -{
 9123 -	if ( (time_adjust_step = time_adjust) != 0 ) {
 9124 -	    /* We are doing an adjtime thing. 
 9125 -	     *
 9126 -	     * Prepare time_adjust_step to be within bounds.
 9127 -	     * Note that a positive time_adjust means we want the clock
 9128 -	     * to run faster.
 9129 -	     *
 9130 -	     * Limit the amount of the step to be in the range
 9131 -	     * -tickadj .. +tickadj
 9132 -	     */
 9133 -	     if (time_adjust > tickadj)
 9134 -		time_adjust_step = tickadj;
 9135 -	     else if (time_adjust < -tickadj)
 9136 -		time_adjust_step = -tickadj;
 9137 -	     
 9138 -	    /* Reduce by this step the amount of time left  */
 9139 -	    time_adjust -= time_adjust_step;
 9140 -	}
 9141 -	xtime.tv_usec += tick + time_adjust_step;
 9142 -	/*
 9143 -	 * Advance the phase, once it gets to one microsecond, then
 9144 -	 * advance the tick more.
 9145 -	 */
 9146 -	time_phase += time_adj;
 9147 -	if (time_phase <= -FINEUSEC) {
 9148 -		long ltemp = -time_phase >> SHIFT_SCALE;
 9149 -		time_phase += ltemp << SHIFT_SCALE;
 9150 -		xtime.tv_usec -= ltemp;
 9151 -	}
 9152 -	else if (time_phase >= FINEUSEC) {
 9153 -		long ltemp = time_phase >> SHIFT_SCALE;
 9154 -		time_phase -= ltemp << SHIFT_SCALE;
 9155 -		xtime.tv_usec += ltemp;
 9156 -	}
 9157 -}
 9158 -
 9159 -/*
 9160 - * Using a loop looks inefficient, but "ticks" is
 9161 - * usually just one (we shouldn't be losing ticks,
 9162 - * we're doing this this way mainly for interrupt
 9163 - * latency reasons, not because we think we'll
 9164 - * have lots of lost timer ticks
 9165 - */
 9166 -static void update_wall_time(unsigned long ticks)
 9167 -{
 9168 -	do {
 9169 -		ticks--;
 9170 -		update_wall_time_one_tick();
 9171 -	} while (ticks);
 9172 -
 9173 -	if (xtime.tv_usec >= 1000000) {
 9174 -	    xtime.tv_usec -= 1000000;
 9175 -	    xtime.tv_sec++;
 9176 -	    second_overflow();
 9177 -	}
 9178 -}
 9179 -
 9180  static inline void do_process_times(struct task_struct *p,
 9181  	unsigned long user, unsigned long system)
 9182  {
 9183 @@ -661,12 +445,7 @@
 9184  }
 9185  
 9186  /* jiffies at the most recent update of wall time */
 9187 -unsigned long wall_jiffies;
 9188 -
 9189 -/*
 9190 - * This spinlock protect us from races in SMP while playing with xtime. -arca
 9191 - */
 9192 -rwlock_t xtime_lock = RW_LOCK_UNLOCKED;
 9193 +volatile unsigned long wall_jiffies;
 9194  
 9195  static inline void update_times(void)
 9196  {
 9197 Index: linux/mm/oom_kill.c
 9198 diff -u linux/mm/oom_kill.c:1.1.1.12 linux/mm/oom_kill.c:1.1.1.12.2.1
 9199 --- linux/mm/oom_kill.c:1.1.1.12	Sat Aug 26 21:34:30 2006
 9200 +++ linux/mm/oom_kill.c	Sat Aug 26 21:50:55 2006
 9201 @@ -19,7 +19,7 @@
 9202  #include <linux/sched.h>
 9203  #include <linux/swap.h>
 9204  #include <linux/swapctl.h>
 9205 -#include <linux/timex.h>
 9206 +#include <linux/time.h>
 9207  
 9208  /* #define DEBUG */
 9209  
 9210 Index: linux/net/khttpd/structure.h
 9211 diff -u linux/net/khttpd/structure.h:1.1.1.1 linux/net/khttpd/structure.h:1.1.1.1.30.1
 9212 --- linux/net/khttpd/structure.h:1.1.1.1	Thu Feb 10 04:47:20 2000
 9213 +++ linux/net/khttpd/structure.h	Sat Aug 26 21:50:57 2006
 9214 @@ -1,7 +1,7 @@
 9215  #ifndef _INCLUDE_GUARD_STRUCTURE_H_
 9216  #define _INCLUDE_GUARD_STRUCTURE_H_
 9217  
 9218 -#include <linux/time.h>
 9219 +#include <linux/types.h>
 9220  #include <linux/wait.h>
 9221  
 9222  
 9223 Index: linux/net/sunrpc/svcauth_des.c
 9224 diff -u linux/net/sunrpc/svcauth_des.c:1.1.1.1 linux/net/sunrpc/svcauth_des.c:1.1.1.1.30.1
 9225 --- linux/net/sunrpc/svcauth_des.c:1.1.1.1	Mon Apr  7 20:35:33 1997
 9226 +++ linux/net/sunrpc/svcauth_des.c	Sat Aug 26 21:50:59 2006
 9227 @@ -7,7 +7,7 @@
 9228   */
 9229  
 9230  #include <linux/types.h>
 9231 -#include <linux/sched.h>
 9232 +#include <linux/time.h>
 9233  #include <linux/sunrpc/types.h>
 9234  #include <linux/sunrpc/xdr.h>
 9235  #include <linux/sunrpc/svcauth.h>
 9236 @@ -175,7 +175,7 @@
 9237  #endif
 9238  	}
 9239  
 9240 -	now = xtime;
 9241 +	do_gettimeofday(&now);	/* now = xtime; need for speed? -- UW */
 9242  	now.tv_secs -= data->dc_window;
 9243  	if (now.tv_secs < cryptbuf[0] ||
 9244  	    (now.tv_secs == cryptbuf[0] && now.tv_usec < cryptbuf[1]))