Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/jikos/hid
[sfrench/cifs-2.6.git] / arch / xtensa / kernel / time.c
index 22949be4a5d8cc3c5d62f93b8779a67c3ecd451f..8848120d291bc7465d9a4fcdf2b07aee7ae9c394 100644 (file)
@@ -14,7 +14,7 @@
 
 #include <linux/errno.h>
 #include <linux/time.h>
-#include <linux/timex.h>
+#include <linux/clocksource.h>
 #include <linux/interrupt.h>
 #include <linux/module.h>
 #include <linux/init.h>
 #include <asm/timex.h>
 #include <asm/platform.h>
 
-
-DEFINE_SPINLOCK(rtc_lock);
-EXPORT_SYMBOL(rtc_lock);
-
-
 #ifdef CONFIG_XTENSA_CALIBRATE_CCOUNT
 unsigned long ccount_per_jiffy;                /* per 1/HZ */
-unsigned long ccount_nsec;             /* nsec per ccount increment */
+unsigned long nsec_per_ccount;         /* nsec per ccount increment */
 #endif
 
-unsigned int last_ccount_stamp;
-static long last_rtc_update = 0;
+static cycle_t ccount_read(void)
+{
+       return (cycle_t)get_ccount();
+}
+
+static struct clocksource ccount_clocksource = {
+       .name = "ccount",
+       .rating = 200,
+       .read = ccount_read,
+       .mask = CLOCKSOURCE_MASK(32),
+       /*
+        * With a shift of 22 the lower limit of the cpu clock is
+        * 1MHz, where NSEC_PER_CCOUNT is 1000 or a bit less than
+        * 2^10: Since we have 32 bits and the multiplicator can
+        * already take up as much as 10 bits, this leaves us with
+        * remaining upper 22 bits.
+        */
+       .shift = 22,
+};
 
 static irqreturn_t timer_interrupt(int irq, void *dev_id);
 static struct irqaction timer_irqaction = {
@@ -47,11 +59,11 @@ static struct irqaction timer_irqaction = {
 
 void __init time_init(void)
 {
-       time_t sec_o, sec_n = 0;
+       xtime.tv_nsec = 0;
+       xtime.tv_sec = read_persistent_clock();
 
-       /* The platform must provide a function to calibrate the processor
-        * speed for the CALIBRATE.
-        */
+       set_normalized_timespec(&wall_to_monotonic,
+               -xtime.tv_sec, -xtime.tv_nsec);
 
 #ifdef CONFIG_XTENSA_CALIBRATE_CCOUNT
        printk("Calibrating CPU frequency ");
@@ -59,20 +71,10 @@ void __init time_init(void)
        printk("%d.%02d MHz\n", (int)ccount_per_jiffy/(1000000/HZ),
                        (int)(ccount_per_jiffy/(10000/HZ))%100);
 #endif
-
-       /* Set time from RTC (if provided) */
-
-       if (platform_get_rtc_time(&sec_o) == 0)
-               while (platform_get_rtc_time(&sec_n))
-                       if (sec_o != sec_n)
-                               break;
-
-       xtime.tv_nsec = 0;
-       last_rtc_update = xtime.tv_sec = sec_n;
-       last_ccount_stamp = get_ccount();
-
-       set_normalized_timespec(&wall_to_monotonic,
-               -xtime.tv_sec, -xtime.tv_nsec);
+       ccount_clocksource.mult =
+               clocksource_hz2mult(CCOUNT_PER_JIFFY * HZ,
+                               ccount_clocksource.shift);
+       clocksource_register(&ccount_clocksource);
 
        /* Initialize the linux timer interrupt. */
 
@@ -80,63 +82,6 @@ void __init time_init(void)
        set_linux_timer(get_ccount() + CCOUNT_PER_JIFFY);
 }
 
-
-int do_settimeofday(struct timespec *tv)
-{
-       time_t wtm_sec, sec = tv->tv_sec;
-       long wtm_nsec, nsec = tv->tv_nsec;
-       unsigned long ccount;
-
-       if ((unsigned long)tv->tv_nsec >= NSEC_PER_SEC)
-               return -EINVAL;
-
-       write_seqlock_irq(&xtime_lock);
-
-       /* This is revolting. We need to set "xtime" correctly. However, the
-        * value in this location is the value at the most recent update of
-        * wall time.  Discover what correction gettimeofday() would have
-        * made, and then undo it!
-        */
-       ccount = get_ccount();
-       nsec -= (ccount - last_ccount_stamp) * CCOUNT_NSEC;
-
-       wtm_sec  = wall_to_monotonic.tv_sec + (xtime.tv_sec - sec);
-       wtm_nsec = wall_to_monotonic.tv_nsec + (xtime.tv_nsec - nsec);
-
-       set_normalized_timespec(&xtime, sec, nsec);
-       set_normalized_timespec(&wall_to_monotonic, wtm_sec, wtm_nsec);
-
-       ntp_clear();
-       write_sequnlock_irq(&xtime_lock);
-       return 0;
-}
-
-EXPORT_SYMBOL(do_settimeofday);
-
-
-void do_gettimeofday(struct timeval *tv)
-{
-       unsigned long flags;
-       unsigned long sec, usec, delta, seq;
-
-       do {
-               seq = read_seqbegin_irqsave(&xtime_lock, flags);
-
-               delta = get_ccount() - last_ccount_stamp;
-               sec = xtime.tv_sec;
-               usec = (xtime.tv_nsec / NSEC_PER_USEC);
-       } while (read_seqretry_irqrestore(&xtime_lock, seq, flags));
-
-       usec += (delta * CCOUNT_NSEC) / NSEC_PER_USEC;
-       for (; usec >= 1000000; sec++, usec -= 1000000)
-               ;
-
-       tv->tv_sec = sec;
-       tv->tv_usec = usec;
-}
-
-EXPORT_SYMBOL(do_gettimeofday);
-
 /*
  * The timer interrupt is called HZ times per second.
  */
@@ -158,41 +103,30 @@ again:
 
                write_seqlock(&xtime_lock);
 
-               last_ccount_stamp = next;
+               do_timer(1); /* Linux handler in kernel/timer.c */
+
+               /* Note that writing CCOMPARE clears the interrupt. */
+
                next += CCOUNT_PER_JIFFY;
-               do_timer (1); /* Linux handler in kernel/timer.c */
-
-               if (ntp_synced() &&
-                   xtime.tv_sec - last_rtc_update >= 659 &&
-                   abs((xtime.tv_nsec/1000)-(1000000-1000000/HZ))<5000000/HZ) {
-
-                       if (platform_set_rtc_time(xtime.tv_sec+1) == 0)
-                               last_rtc_update = xtime.tv_sec+1;
-                       else
-                               /* Do it again in 60 s */
-                               last_rtc_update += 60;
-               }
+               set_linux_timer(next);
+
                write_sequnlock(&xtime_lock);
        }
 
-       /* NOTE: writing CCOMPAREn clears the interrupt.  */
+       /* Allow platform to do something useful (Wdog). */
 
-       set_linux_timer (next);
+       platform_heartbeat();
 
        /* Make sure we didn't miss any tick... */
 
        if ((signed long)(get_ccount() - next) > 0)
                goto again;
 
-       /* Allow platform to do something useful (Wdog). */
-
-       platform_heartbeat();
-
        return IRQ_HANDLED;
 }
 
 #ifndef CONFIG_GENERIC_CALIBRATE_DELAY
-void __devinit calibrate_delay(void)
+void __cpuinit calibrate_delay(void)
 {
        loops_per_jiffy = CCOUNT_PER_JIFFY;
        printk("Calibrating delay loop (skipped)... "
@@ -201,4 +135,3 @@ void __devinit calibrate_delay(void)
               (loops_per_jiffy/(10000/HZ)) % 100);
 }
 #endif
-