linux/arch/xtensa/kernel/time.c
<<
>>
Prefs
   1/*
   2 * arch/xtensa/kernel/time.c
   3 *
   4 * Timer and clock support.
   5 *
   6 * This file is subject to the terms and conditions of the GNU General Public
   7 * License.  See the file "COPYING" in the main directory of this archive
   8 * for more details.
   9 *
  10 * Copyright (C) 2005 Tensilica Inc.
  11 *
  12 * Chris Zankel <chris@zankel.net>
  13 */
  14
  15#include <linux/errno.h>
  16#include <linux/time.h>
  17#include <linux/timex.h>
  18#include <linux/interrupt.h>
  19#include <linux/module.h>
  20#include <linux/init.h>
  21#include <linux/irq.h>
  22#include <linux/profile.h>
  23#include <linux/delay.h>
  24
  25#include <asm/timex.h>
  26#include <asm/platform.h>
  27
  28
  29DEFINE_SPINLOCK(rtc_lock);
  30EXPORT_SYMBOL(rtc_lock);
  31
  32
  33#ifdef CONFIG_XTENSA_CALIBRATE_CCOUNT
  34unsigned long ccount_per_jiffy;         /* per 1/HZ */
  35unsigned long nsec_per_ccount;          /* nsec per ccount increment */
  36#endif
  37
  38static long last_rtc_update = 0;
  39
  40/*
  41 * Scheduler clock - returns current tim in nanosec units.
  42 */
  43
  44unsigned long long sched_clock(void)
  45{
  46        return (unsigned long long)jiffies * (1000000000 / HZ);
  47}
  48
  49static irqreturn_t timer_interrupt(int irq, void *dev_id);
  50static struct irqaction timer_irqaction = {
  51        .handler =      timer_interrupt,
  52        .flags =        IRQF_DISABLED,
  53        .name =         "timer",
  54};
  55
  56void __init time_init(void)
  57{
  58        time_t sec_o, sec_n = 0;
  59
  60        /* The platform must provide a function to calibrate the processor
  61         * speed for the CALIBRATE.
  62         */
  63
  64#ifdef CONFIG_XTENSA_CALIBRATE_CCOUNT
  65        printk("Calibrating CPU frequency ");
  66        platform_calibrate_ccount();
  67        printk("%d.%02d MHz\n", (int)ccount_per_jiffy/(1000000/HZ),
  68                        (int)(ccount_per_jiffy/(10000/HZ))%100);
  69#endif
  70
  71        /* Set time from RTC (if provided) */
  72
  73        if (platform_get_rtc_time(&sec_o) == 0)
  74                while (platform_get_rtc_time(&sec_n))
  75                        if (sec_o != sec_n)
  76                                break;
  77
  78        xtime.tv_nsec = 0;
  79        last_rtc_update = xtime.tv_sec = sec_n;
  80
  81        set_normalized_timespec(&wall_to_monotonic,
  82                -xtime.tv_sec, -xtime.tv_nsec);
  83
  84        /* Initialize the linux timer interrupt. */
  85
  86        setup_irq(LINUX_TIMER_INT, &timer_irqaction);
  87        set_linux_timer(get_ccount() + CCOUNT_PER_JIFFY);
  88}
  89
  90
  91int do_settimeofday(struct timespec *tv)
  92{
  93        time_t wtm_sec, sec = tv->tv_sec;
  94        long wtm_nsec, nsec = tv->tv_nsec;
  95        unsigned long delta;
  96
  97        if ((unsigned long)tv->tv_nsec >= NSEC_PER_SEC)
  98                return -EINVAL;
  99
 100        write_seqlock_irq(&xtime_lock);
 101
 102        /* This is revolting. We need to set "xtime" correctly. However, the
 103         * value in this location is the value at the most recent update of
 104         * wall time.  Discover what correction gettimeofday() would have
 105         * made, and then undo it!
 106         */
 107
 108        delta = CCOUNT_PER_JIFFY;
 109        delta += get_ccount() - get_linux_timer();
 110        nsec -= delta * NSEC_PER_CCOUNT;
 111
 112        wtm_sec  = wall_to_monotonic.tv_sec + (xtime.tv_sec - sec);
 113        wtm_nsec = wall_to_monotonic.tv_nsec + (xtime.tv_nsec - nsec);
 114
 115        set_normalized_timespec(&xtime, sec, nsec);
 116        set_normalized_timespec(&wall_to_monotonic, wtm_sec, wtm_nsec);
 117
 118        ntp_clear();
 119        write_sequnlock_irq(&xtime_lock);
 120        return 0;
 121}
 122
 123EXPORT_SYMBOL(do_settimeofday);
 124
 125
 126void do_gettimeofday(struct timeval *tv)
 127{
 128        unsigned long flags;
 129        unsigned long volatile sec, usec, delta, seq;
 130
 131        do {
 132                seq = read_seqbegin_irqsave(&xtime_lock, flags);
 133
 134                sec = xtime.tv_sec;
 135                usec = (xtime.tv_nsec / NSEC_PER_USEC);
 136
 137                delta = get_linux_timer() - get_ccount();
 138
 139        } while (read_seqretry_irqrestore(&xtime_lock, seq, flags));
 140
 141        usec += (((unsigned long) CCOUNT_PER_JIFFY - delta)
 142                 * (unsigned long) NSEC_PER_CCOUNT) / NSEC_PER_USEC;
 143
 144        for (; usec >= 1000000; sec++, usec -= 1000000)
 145                ;
 146
 147        tv->tv_sec = sec;
 148        tv->tv_usec = usec;
 149}
 150
 151EXPORT_SYMBOL(do_gettimeofday);
 152
 153/*
 154 * The timer interrupt is called HZ times per second.
 155 */
 156
 157irqreturn_t timer_interrupt (int irq, void *dev_id)
 158{
 159
 160        unsigned long next;
 161
 162        next = get_linux_timer();
 163
 164again:
 165        while ((signed long)(get_ccount() - next) > 0) {
 166
 167                profile_tick(CPU_PROFILING);
 168#ifndef CONFIG_SMP
 169                update_process_times(user_mode(get_irq_regs()));
 170#endif
 171
 172                write_seqlock(&xtime_lock);
 173
 174                do_timer(1); /* Linux handler in kernel/timer.c */
 175
 176                /* Note that writing CCOMPARE clears the interrupt. */
 177
 178                next += CCOUNT_PER_JIFFY;
 179                set_linux_timer(next);
 180
 181                if (ntp_synced() &&
 182                    xtime.tv_sec - last_rtc_update >= 659 &&
 183                    abs((xtime.tv_nsec/1000)-(1000000-1000000/HZ))<5000000/HZ) {
 184
 185                        if (platform_set_rtc_time(xtime.tv_sec+1) == 0)
 186                                last_rtc_update = xtime.tv_sec+1;
 187                        else
 188                                /* Do it again in 60 s */
 189                                last_rtc_update += 60;
 190                }
 191                write_sequnlock(&xtime_lock);
 192        }
 193
 194        /* Allow platform to do something useful (Wdog). */
 195
 196        platform_heartbeat();
 197
 198        /* Make sure we didn't miss any tick... */
 199
 200        if ((signed long)(get_ccount() - next) > 0)
 201                goto again;
 202
 203        return IRQ_HANDLED;
 204}
 205
 206#ifndef CONFIG_GENERIC_CALIBRATE_DELAY
 207void __cpuinit calibrate_delay(void)
 208{
 209        loops_per_jiffy = CCOUNT_PER_JIFFY;
 210        printk("Calibrating delay loop (skipped)... "
 211               "%lu.%02lu BogoMIPS preset\n",
 212               loops_per_jiffy/(1000000/HZ),
 213               (loops_per_jiffy/(10000/HZ)) % 100);
 214}
 215#endif
 216
 217
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.