Chris Zankel | 5a0015d | 2005-06-23 22:01:16 -0700 | [diff] [blame] | 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 | |
Chris Zankel | 5a0015d | 2005-06-23 22:01:16 -0700 | [diff] [blame] | 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 | |
Ingo Molnar | 34af946 | 2006-06-27 02:53:55 -0700 | [diff] [blame] | 29 | DEFINE_SPINLOCK(rtc_lock); |
Chris Zankel | 5a0015d | 2005-06-23 22:01:16 -0700 | [diff] [blame] | 30 | EXPORT_SYMBOL(rtc_lock); |
| 31 | |
| 32 | |
| 33 | #ifdef CONFIG_XTENSA_CALIBRATE_CCOUNT |
| 34 | unsigned long ccount_per_jiffy; /* per 1/HZ */ |
| 35 | unsigned long ccount_nsec; /* nsec per ccount increment */ |
| 36 | #endif |
| 37 | |
| 38 | unsigned int last_ccount_stamp; |
| 39 | static long last_rtc_update = 0; |
| 40 | |
| 41 | /* |
| 42 | * Scheduler clock - returns current tim in nanosec units. |
| 43 | */ |
| 44 | |
| 45 | unsigned long long sched_clock(void) |
| 46 | { |
| 47 | return (unsigned long long)jiffies * (1000000000 / HZ); |
| 48 | } |
| 49 | |
Chris Zankel | fd43fe1 | 2006-12-10 02:18:47 -0800 | [diff] [blame^] | 50 | static irqreturn_t timer_interrupt(int irq, void *dev_id); |
Chris Zankel | 5a0015d | 2005-06-23 22:01:16 -0700 | [diff] [blame] | 51 | static struct irqaction timer_irqaction = { |
| 52 | .handler = timer_interrupt, |
Thomas Gleixner | 85ac3ab | 2006-07-01 19:29:31 -0700 | [diff] [blame] | 53 | .flags = IRQF_DISABLED, |
Chris Zankel | 5a0015d | 2005-06-23 22:01:16 -0700 | [diff] [blame] | 54 | .name = "timer", |
| 55 | }; |
| 56 | |
| 57 | void __init time_init(void) |
| 58 | { |
| 59 | time_t sec_o, sec_n = 0; |
| 60 | |
| 61 | /* The platform must provide a function to calibrate the processor |
| 62 | * speed for the CALIBRATE. |
| 63 | */ |
| 64 | |
Chris Zankel | 288a60c | 2005-09-22 21:44:23 -0700 | [diff] [blame] | 65 | #ifdef CONFIG_XTENSA_CALIBRATE_CCOUNT |
Chris Zankel | 5a0015d | 2005-06-23 22:01:16 -0700 | [diff] [blame] | 66 | printk("Calibrating CPU frequency "); |
| 67 | platform_calibrate_ccount(); |
| 68 | printk("%d.%02d MHz\n", (int)ccount_per_jiffy/(1000000/HZ), |
| 69 | (int)(ccount_per_jiffy/(10000/HZ))%100); |
| 70 | #endif |
| 71 | |
| 72 | /* Set time from RTC (if provided) */ |
| 73 | |
| 74 | if (platform_get_rtc_time(&sec_o) == 0) |
| 75 | while (platform_get_rtc_time(&sec_n)) |
| 76 | if (sec_o != sec_n) |
| 77 | break; |
| 78 | |
| 79 | xtime.tv_nsec = 0; |
| 80 | last_rtc_update = xtime.tv_sec = sec_n; |
| 81 | last_ccount_stamp = get_ccount(); |
| 82 | |
| 83 | set_normalized_timespec(&wall_to_monotonic, |
| 84 | -xtime.tv_sec, -xtime.tv_nsec); |
| 85 | |
| 86 | /* Initialize the linux timer interrupt. */ |
| 87 | |
| 88 | setup_irq(LINUX_TIMER_INT, &timer_irqaction); |
| 89 | set_linux_timer(get_ccount() + CCOUNT_PER_JIFFY); |
| 90 | } |
| 91 | |
| 92 | |
| 93 | int do_settimeofday(struct timespec *tv) |
| 94 | { |
| 95 | time_t wtm_sec, sec = tv->tv_sec; |
| 96 | long wtm_nsec, nsec = tv->tv_nsec; |
| 97 | unsigned long ccount; |
| 98 | |
| 99 | if ((unsigned long)tv->tv_nsec >= NSEC_PER_SEC) |
| 100 | return -EINVAL; |
| 101 | |
| 102 | write_seqlock_irq(&xtime_lock); |
| 103 | |
| 104 | /* This is revolting. We need to set "xtime" correctly. However, the |
| 105 | * value in this location is the value at the most recent update of |
| 106 | * wall time. Discover what correction gettimeofday() would have |
| 107 | * made, and then undo it! |
| 108 | */ |
| 109 | ccount = get_ccount(); |
| 110 | nsec -= (ccount - last_ccount_stamp) * CCOUNT_NSEC; |
Chris Zankel | 5a0015d | 2005-06-23 22:01:16 -0700 | [diff] [blame] | 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 | |
john stultz | b149ee2 | 2005-09-06 15:17:46 -0700 | [diff] [blame] | 118 | ntp_clear(); |
Chris Zankel | 5a0015d | 2005-06-23 22:01:16 -0700 | [diff] [blame] | 119 | write_sequnlock_irq(&xtime_lock); |
| 120 | return 0; |
| 121 | } |
| 122 | |
| 123 | EXPORT_SYMBOL(do_settimeofday); |
| 124 | |
| 125 | |
| 126 | void do_gettimeofday(struct timeval *tv) |
| 127 | { |
| 128 | unsigned long flags; |
Atsushi Nemoto | 8ef3860 | 2006-09-30 23:28:31 -0700 | [diff] [blame] | 129 | unsigned long sec, usec, delta, seq; |
Chris Zankel | 5a0015d | 2005-06-23 22:01:16 -0700 | [diff] [blame] | 130 | |
| 131 | do { |
| 132 | seq = read_seqbegin_irqsave(&xtime_lock, flags); |
| 133 | |
| 134 | delta = get_ccount() - last_ccount_stamp; |
| 135 | sec = xtime.tv_sec; |
| 136 | usec = (xtime.tv_nsec / NSEC_PER_USEC); |
Chris Zankel | 5a0015d | 2005-06-23 22:01:16 -0700 | [diff] [blame] | 137 | } while (read_seqretry_irqrestore(&xtime_lock, seq, flags)); |
| 138 | |
Atsushi Nemoto | 8ef3860 | 2006-09-30 23:28:31 -0700 | [diff] [blame] | 139 | usec += (delta * CCOUNT_NSEC) / NSEC_PER_USEC; |
Chris Zankel | 5a0015d | 2005-06-23 22:01:16 -0700 | [diff] [blame] | 140 | for (; usec >= 1000000; sec++, usec -= 1000000) |
| 141 | ; |
| 142 | |
| 143 | tv->tv_sec = sec; |
| 144 | tv->tv_usec = usec; |
| 145 | } |
| 146 | |
| 147 | EXPORT_SYMBOL(do_gettimeofday); |
| 148 | |
| 149 | /* |
| 150 | * The timer interrupt is called HZ times per second. |
| 151 | */ |
| 152 | |
Chris Zankel | fd43fe1 | 2006-12-10 02:18:47 -0800 | [diff] [blame^] | 153 | irqreturn_t timer_interrupt (int irq, void *dev_id) |
Chris Zankel | 5a0015d | 2005-06-23 22:01:16 -0700 | [diff] [blame] | 154 | { |
| 155 | |
| 156 | unsigned long next; |
| 157 | |
| 158 | next = get_linux_timer(); |
| 159 | |
| 160 | again: |
| 161 | while ((signed long)(get_ccount() - next) > 0) { |
| 162 | |
Chris Zankel | fd43fe1 | 2006-12-10 02:18:47 -0800 | [diff] [blame^] | 163 | profile_tick(CPU_PROFILING); |
Chris Zankel | 5a0015d | 2005-06-23 22:01:16 -0700 | [diff] [blame] | 164 | #ifndef CONFIG_SMP |
Chris Zankel | fd43fe1 | 2006-12-10 02:18:47 -0800 | [diff] [blame^] | 165 | update_process_times(user_mode(get_irq_regs())); |
Chris Zankel | 5a0015d | 2005-06-23 22:01:16 -0700 | [diff] [blame] | 166 | #endif |
| 167 | |
| 168 | write_seqlock(&xtime_lock); |
| 169 | |
| 170 | last_ccount_stamp = next; |
| 171 | next += CCOUNT_PER_JIFFY; |
Atsushi Nemoto | 3171a03 | 2006-09-29 02:00:32 -0700 | [diff] [blame] | 172 | do_timer (1); /* Linux handler in kernel/timer.c */ |
Chris Zankel | 5a0015d | 2005-06-23 22:01:16 -0700 | [diff] [blame] | 173 | |
john stultz | b149ee2 | 2005-09-06 15:17:46 -0700 | [diff] [blame] | 174 | if (ntp_synced() && |
Chris Zankel | 5a0015d | 2005-06-23 22:01:16 -0700 | [diff] [blame] | 175 | xtime.tv_sec - last_rtc_update >= 659 && |
Atsushi Nemoto | 8ef3860 | 2006-09-30 23:28:31 -0700 | [diff] [blame] | 176 | abs((xtime.tv_nsec/1000)-(1000000-1000000/HZ))<5000000/HZ) { |
Chris Zankel | 5a0015d | 2005-06-23 22:01:16 -0700 | [diff] [blame] | 177 | |
| 178 | if (platform_set_rtc_time(xtime.tv_sec+1) == 0) |
| 179 | last_rtc_update = xtime.tv_sec+1; |
| 180 | else |
| 181 | /* Do it again in 60 s */ |
| 182 | last_rtc_update += 60; |
| 183 | } |
| 184 | write_sequnlock(&xtime_lock); |
| 185 | } |
| 186 | |
| 187 | /* NOTE: writing CCOMPAREn clears the interrupt. */ |
| 188 | |
| 189 | set_linux_timer (next); |
| 190 | |
| 191 | /* Make sure we didn't miss any tick... */ |
| 192 | |
| 193 | if ((signed long)(get_ccount() - next) > 0) |
| 194 | goto again; |
| 195 | |
Adrian Bunk | a58a414 | 2006-01-10 00:08:17 +0100 | [diff] [blame] | 196 | /* Allow platform to do something useful (Wdog). */ |
Chris Zankel | 5a0015d | 2005-06-23 22:01:16 -0700 | [diff] [blame] | 197 | |
| 198 | platform_heartbeat(); |
| 199 | |
| 200 | return IRQ_HANDLED; |
| 201 | } |
| 202 | |
| 203 | #ifndef CONFIG_GENERIC_CALIBRATE_DELAY |
| 204 | void __devinit calibrate_delay(void) |
| 205 | { |
| 206 | loops_per_jiffy = CCOUNT_PER_JIFFY; |
| 207 | printk("Calibrating delay loop (skipped)... " |
| 208 | "%lu.%02lu BogoMIPS preset\n", |
| 209 | loops_per_jiffy/(1000000/HZ), |
| 210 | (loops_per_jiffy/(10000/HZ)) % 100); |
| 211 | } |
| 212 | #endif |
| 213 | |