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