| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright 2001 MontaVista Software Inc. | 
|  | 3 | * Author: Jun Sun, jsun@mvista.com or jsun@junsun.net | 
|  | 4 | * Copyright (c) 2003, 2004  Maciej W. Rozycki | 
|  | 5 | * | 
|  | 6 | * Common time service routines for MIPS machines. See | 
|  | 7 | * Documentation/mips/time.README. | 
|  | 8 | * | 
|  | 9 | * This program is free software; you can redistribute  it and/or modify it | 
|  | 10 | * under  the terms of  the GNU General  Public License as published by the | 
|  | 11 | * Free Software Foundation;  either version 2 of the  License, or (at your | 
|  | 12 | * option) any later version. | 
|  | 13 | */ | 
|  | 14 | #include <linux/types.h> | 
|  | 15 | #include <linux/kernel.h> | 
|  | 16 | #include <linux/init.h> | 
|  | 17 | #include <linux/sched.h> | 
|  | 18 | #include <linux/param.h> | 
|  | 19 | #include <linux/time.h> | 
|  | 20 | #include <linux/timex.h> | 
|  | 21 | #include <linux/smp.h> | 
|  | 22 | #include <linux/kernel_stat.h> | 
|  | 23 | #include <linux/spinlock.h> | 
|  | 24 | #include <linux/interrupt.h> | 
|  | 25 | #include <linux/module.h> | 
|  | 26 |  | 
|  | 27 | #include <asm/bootinfo.h> | 
| Ralf Baechle | ec74e36 | 2005-07-13 11:48:45 +0000 | [diff] [blame] | 28 | #include <asm/cache.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | #include <asm/compiler.h> | 
|  | 30 | #include <asm/cpu.h> | 
|  | 31 | #include <asm/cpu-features.h> | 
|  | 32 | #include <asm/div64.h> | 
|  | 33 | #include <asm/sections.h> | 
|  | 34 | #include <asm/time.h> | 
|  | 35 |  | 
|  | 36 | /* | 
|  | 37 | * The integer part of the number of usecs per jiffy is taken from tick, | 
|  | 38 | * but the fractional part is not recorded, so we calculate it using the | 
|  | 39 | * initial value of HZ.  This aids systems where tick isn't really an | 
|  | 40 | * integer (e.g. for HZ = 128). | 
|  | 41 | */ | 
|  | 42 | #define USECS_PER_JIFFY		TICK_SIZE | 
|  | 43 | #define USECS_PER_JIFFY_FRAC	((unsigned long)(u32)((1000000ULL << 32) / HZ)) | 
|  | 44 |  | 
|  | 45 | #define TICK_SIZE	(tick_nsec / 1000) | 
|  | 46 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | /* | 
|  | 48 | * forward reference | 
|  | 49 | */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | DEFINE_SPINLOCK(rtc_lock); | 
|  | 51 |  | 
|  | 52 | /* | 
|  | 53 | * By default we provide the null RTC ops | 
|  | 54 | */ | 
|  | 55 | static unsigned long null_rtc_get_time(void) | 
|  | 56 | { | 
|  | 57 | return mktime(2000, 1, 1, 0, 0, 0); | 
|  | 58 | } | 
|  | 59 |  | 
|  | 60 | static int null_rtc_set_time(unsigned long sec) | 
|  | 61 | { | 
|  | 62 | return 0; | 
|  | 63 | } | 
|  | 64 |  | 
| Yoichi Yuasa | d23ee8f | 2006-03-27 01:16:33 -0800 | [diff] [blame] | 65 | unsigned long (*rtc_mips_get_time)(void) = null_rtc_get_time; | 
|  | 66 | int (*rtc_mips_set_time)(unsigned long) = null_rtc_set_time; | 
|  | 67 | int (*rtc_mips_set_mmss)(unsigned long); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 |  | 
|  | 69 |  | 
|  | 70 | /* usecs per counter cycle, shifted to left by 32 bits */ | 
|  | 71 | static unsigned int sll32_usecs_per_cycle; | 
|  | 72 |  | 
|  | 73 | /* how many counter cycles in a jiffy */ | 
| Ralf Baechle | ec74e36 | 2005-07-13 11:48:45 +0000 | [diff] [blame] | 74 | static unsigned long cycles_per_jiffy __read_mostly; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 |  | 
|  | 76 | /* Cycle counter value at the previous timer interrupt.. */ | 
|  | 77 | static unsigned int timerhi, timerlo; | 
|  | 78 |  | 
|  | 79 | /* expirelo is the count value for next CPU timer interrupt */ | 
|  | 80 | static unsigned int expirelo; | 
|  | 81 |  | 
|  | 82 |  | 
|  | 83 | /* | 
|  | 84 | * Null timer ack for systems not needing one (e.g. i8254). | 
|  | 85 | */ | 
|  | 86 | static void null_timer_ack(void) { /* nothing */ } | 
|  | 87 |  | 
|  | 88 | /* | 
|  | 89 | * Null high precision timer functions for systems lacking one. | 
|  | 90 | */ | 
|  | 91 | static unsigned int null_hpt_read(void) | 
|  | 92 | { | 
|  | 93 | return 0; | 
|  | 94 | } | 
|  | 95 |  | 
| Ralf Baechle | ec74e36 | 2005-07-13 11:48:45 +0000 | [diff] [blame] | 96 | static void null_hpt_init(unsigned int count) | 
|  | 97 | { | 
|  | 98 | /* nothing */ | 
|  | 99 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 |  | 
|  | 101 |  | 
|  | 102 | /* | 
|  | 103 | * Timer ack for an R4k-compatible timer of a known frequency. | 
|  | 104 | */ | 
|  | 105 | static void c0_timer_ack(void) | 
|  | 106 | { | 
|  | 107 | unsigned int count; | 
|  | 108 |  | 
| Pete Popov | bdf21b1 | 2005-07-14 17:47:57 +0000 | [diff] [blame] | 109 | #ifndef CONFIG_SOC_PNX8550	/* pnx8550 resets to zero */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | /* Ack this timer interrupt and set the next one.  */ | 
|  | 111 | expirelo += cycles_per_jiffy; | 
| Pete Popov | bdf21b1 | 2005-07-14 17:47:57 +0000 | [diff] [blame] | 112 | #endif | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | write_c0_compare(expirelo); | 
|  | 114 |  | 
|  | 115 | /* Check to see if we have missed any timer interrupts.  */ | 
| Ralf Baechle | 41c594a | 2006-04-05 09:45:45 +0100 | [diff] [blame] | 116 | while (((count = read_c0_count()) - expirelo) < 0x7fffffff) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | /* missed_timer_count++; */ | 
|  | 118 | expirelo = count + cycles_per_jiffy; | 
|  | 119 | write_c0_compare(expirelo); | 
|  | 120 | } | 
|  | 121 | } | 
|  | 122 |  | 
|  | 123 | /* | 
|  | 124 | * High precision timer functions for a R4k-compatible timer. | 
|  | 125 | */ | 
|  | 126 | static unsigned int c0_hpt_read(void) | 
|  | 127 | { | 
|  | 128 | return read_c0_count(); | 
|  | 129 | } | 
|  | 130 |  | 
|  | 131 | /* For use solely as a high precision timer.  */ | 
|  | 132 | static void c0_hpt_init(unsigned int count) | 
|  | 133 | { | 
|  | 134 | write_c0_count(read_c0_count() - count); | 
|  | 135 | } | 
|  | 136 |  | 
|  | 137 | /* For use both as a high precision timer and an interrupt source.  */ | 
|  | 138 | static void c0_hpt_timer_init(unsigned int count) | 
|  | 139 | { | 
|  | 140 | count = read_c0_count() - count; | 
|  | 141 | expirelo = (count / cycles_per_jiffy + 1) * cycles_per_jiffy; | 
|  | 142 | write_c0_count(expirelo - cycles_per_jiffy); | 
|  | 143 | write_c0_compare(expirelo); | 
|  | 144 | write_c0_count(count); | 
|  | 145 | } | 
|  | 146 |  | 
|  | 147 | int (*mips_timer_state)(void); | 
|  | 148 | void (*mips_timer_ack)(void); | 
|  | 149 | unsigned int (*mips_hpt_read)(void); | 
|  | 150 | void (*mips_hpt_init)(unsigned int); | 
|  | 151 |  | 
|  | 152 |  | 
|  | 153 | /* | 
|  | 154 | * This version of gettimeofday has microsecond resolution and better than | 
|  | 155 | * microsecond precision on fast machines with cycle counter. | 
|  | 156 | */ | 
|  | 157 | void do_gettimeofday(struct timeval *tv) | 
|  | 158 | { | 
|  | 159 | unsigned long seq; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | unsigned long usec, sec; | 
| Atsushi Nemoto | 800d114 | 2006-03-01 15:16:26 +0900 | [diff] [blame] | 161 | unsigned long max_ntp_tick; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 |  | 
|  | 163 | do { | 
|  | 164 | seq = read_seqbegin(&xtime_lock); | 
|  | 165 |  | 
|  | 166 | usec = do_gettimeoffset(); | 
|  | 167 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | /* | 
|  | 169 | * If time_adjust is negative then NTP is slowing the clock | 
|  | 170 | * so make sure not to go into next possible interval. | 
|  | 171 | * Better to lose some accuracy than have time go backwards.. | 
|  | 172 | */ | 
|  | 173 | if (unlikely(time_adjust < 0)) { | 
| Atsushi Nemoto | 800d114 | 2006-03-01 15:16:26 +0900 | [diff] [blame] | 174 | max_ntp_tick = (USEC_PER_SEC / HZ) - tickadj; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | usec = min(usec, max_ntp_tick); | 
| Atsushi Nemoto | 8ef3860 | 2006-09-30 23:28:31 -0700 | [diff] [blame] | 176 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 |  | 
|  | 178 | sec = xtime.tv_sec; | 
|  | 179 | usec += (xtime.tv_nsec / 1000); | 
|  | 180 |  | 
|  | 181 | } while (read_seqretry(&xtime_lock, seq)); | 
|  | 182 |  | 
|  | 183 | while (usec >= 1000000) { | 
|  | 184 | usec -= 1000000; | 
|  | 185 | sec++; | 
|  | 186 | } | 
|  | 187 |  | 
|  | 188 | tv->tv_sec = sec; | 
|  | 189 | tv->tv_usec = usec; | 
|  | 190 | } | 
|  | 191 |  | 
|  | 192 | EXPORT_SYMBOL(do_gettimeofday); | 
|  | 193 |  | 
|  | 194 | int do_settimeofday(struct timespec *tv) | 
|  | 195 | { | 
|  | 196 | time_t wtm_sec, sec = tv->tv_sec; | 
|  | 197 | long wtm_nsec, nsec = tv->tv_nsec; | 
|  | 198 |  | 
|  | 199 | if ((unsigned long)tv->tv_nsec >= NSEC_PER_SEC) | 
|  | 200 | return -EINVAL; | 
|  | 201 |  | 
|  | 202 | write_seqlock_irq(&xtime_lock); | 
|  | 203 |  | 
|  | 204 | /* | 
|  | 205 | * This is revolting.  We need to set "xtime" correctly.  However, | 
|  | 206 | * the value in this location is the value at the most recent update | 
|  | 207 | * of wall time.  Discover what correction gettimeofday() would have | 
|  | 208 | * made, and then undo it! | 
|  | 209 | */ | 
|  | 210 | nsec -= do_gettimeoffset() * NSEC_PER_USEC; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 |  | 
|  | 212 | wtm_sec  = wall_to_monotonic.tv_sec + (xtime.tv_sec - sec); | 
|  | 213 | wtm_nsec = wall_to_monotonic.tv_nsec + (xtime.tv_nsec - nsec); | 
|  | 214 |  | 
|  | 215 | set_normalized_timespec(&xtime, sec, nsec); | 
|  | 216 | set_normalized_timespec(&wall_to_monotonic, wtm_sec, wtm_nsec); | 
|  | 217 |  | 
| john stultz | b149ee2 | 2005-09-06 15:17:46 -0700 | [diff] [blame] | 218 | ntp_clear(); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | write_sequnlock_irq(&xtime_lock); | 
|  | 220 | clock_was_set(); | 
|  | 221 | return 0; | 
|  | 222 | } | 
|  | 223 |  | 
|  | 224 | EXPORT_SYMBOL(do_settimeofday); | 
|  | 225 |  | 
|  | 226 | /* | 
|  | 227 | * Gettimeoffset routines.  These routines returns the time duration | 
|  | 228 | * since last timer interrupt in usecs. | 
|  | 229 | * | 
|  | 230 | * If the exact CPU counter frequency is known, use fixed_rate_gettimeoffset. | 
|  | 231 | * Otherwise use calibrate_gettimeoffset() | 
|  | 232 | * | 
|  | 233 | * If the CPU does not have the counter register, you can either supply | 
|  | 234 | * your own gettimeoffset() routine, or use null_gettimeoffset(), which | 
|  | 235 | * gives the same resolution as HZ. | 
|  | 236 | */ | 
|  | 237 |  | 
|  | 238 | static unsigned long null_gettimeoffset(void) | 
|  | 239 | { | 
|  | 240 | return 0; | 
|  | 241 | } | 
|  | 242 |  | 
|  | 243 |  | 
|  | 244 | /* The function pointer to one of the gettimeoffset funcs.  */ | 
|  | 245 | unsigned long (*do_gettimeoffset)(void) = null_gettimeoffset; | 
|  | 246 |  | 
|  | 247 |  | 
|  | 248 | static unsigned long fixed_rate_gettimeoffset(void) | 
|  | 249 | { | 
|  | 250 | u32 count; | 
|  | 251 | unsigned long res; | 
|  | 252 |  | 
|  | 253 | /* Get last timer tick in absolute kernel time */ | 
|  | 254 | count = mips_hpt_read(); | 
|  | 255 |  | 
|  | 256 | /* .. relative to previous jiffy (32 bits is enough) */ | 
|  | 257 | count -= timerlo; | 
|  | 258 |  | 
|  | 259 | __asm__("multu	%1,%2" | 
|  | 260 | : "=h" (res) | 
|  | 261 | : "r" (count), "r" (sll32_usecs_per_cycle) | 
|  | 262 | : "lo", GCC_REG_ACCUM); | 
|  | 263 |  | 
|  | 264 | /* | 
|  | 265 | * Due to possible jiffies inconsistencies, we need to check | 
|  | 266 | * the result so that we'll get a timer that is monotonic. | 
|  | 267 | */ | 
|  | 268 | if (res >= USECS_PER_JIFFY) | 
|  | 269 | res = USECS_PER_JIFFY - 1; | 
|  | 270 |  | 
|  | 271 | return res; | 
|  | 272 | } | 
|  | 273 |  | 
|  | 274 |  | 
|  | 275 | /* | 
|  | 276 | * Cached "1/(clocks per usec) * 2^32" value. | 
|  | 277 | * It has to be recalculated once each jiffy. | 
|  | 278 | */ | 
|  | 279 | static unsigned long cached_quotient; | 
|  | 280 |  | 
|  | 281 | /* Last jiffy when calibrate_divXX_gettimeoffset() was called. */ | 
|  | 282 | static unsigned long last_jiffies; | 
|  | 283 |  | 
|  | 284 | /* | 
|  | 285 | * This is moved from dec/time.c:do_ioasic_gettimeoffset() by Maciej. | 
|  | 286 | */ | 
|  | 287 | static unsigned long calibrate_div32_gettimeoffset(void) | 
|  | 288 | { | 
|  | 289 | u32 count; | 
|  | 290 | unsigned long res, tmp; | 
|  | 291 | unsigned long quotient; | 
|  | 292 |  | 
|  | 293 | tmp = jiffies; | 
|  | 294 |  | 
|  | 295 | quotient = cached_quotient; | 
|  | 296 |  | 
|  | 297 | if (last_jiffies != tmp) { | 
|  | 298 | last_jiffies = tmp; | 
|  | 299 | if (last_jiffies != 0) { | 
|  | 300 | unsigned long r0; | 
|  | 301 | do_div64_32(r0, timerhi, timerlo, tmp); | 
|  | 302 | do_div64_32(quotient, USECS_PER_JIFFY, | 
|  | 303 | USECS_PER_JIFFY_FRAC, r0); | 
|  | 304 | cached_quotient = quotient; | 
|  | 305 | } | 
|  | 306 | } | 
|  | 307 |  | 
|  | 308 | /* Get last timer tick in absolute kernel time */ | 
|  | 309 | count = mips_hpt_read(); | 
|  | 310 |  | 
|  | 311 | /* .. relative to previous jiffy (32 bits is enough) */ | 
|  | 312 | count -= timerlo; | 
|  | 313 |  | 
|  | 314 | __asm__("multu  %1,%2" | 
|  | 315 | : "=h" (res) | 
|  | 316 | : "r" (count), "r" (quotient) | 
|  | 317 | : "lo", GCC_REG_ACCUM); | 
|  | 318 |  | 
|  | 319 | /* | 
|  | 320 | * Due to possible jiffies inconsistencies, we need to check | 
|  | 321 | * the result so that we'll get a timer that is monotonic. | 
|  | 322 | */ | 
|  | 323 | if (res >= USECS_PER_JIFFY) | 
|  | 324 | res = USECS_PER_JIFFY - 1; | 
|  | 325 |  | 
|  | 326 | return res; | 
|  | 327 | } | 
|  | 328 |  | 
|  | 329 | static unsigned long calibrate_div64_gettimeoffset(void) | 
|  | 330 | { | 
|  | 331 | u32 count; | 
|  | 332 | unsigned long res, tmp; | 
|  | 333 | unsigned long quotient; | 
|  | 334 |  | 
|  | 335 | tmp = jiffies; | 
|  | 336 |  | 
|  | 337 | quotient = cached_quotient; | 
|  | 338 |  | 
|  | 339 | if (last_jiffies != tmp) { | 
|  | 340 | last_jiffies = tmp; | 
|  | 341 | if (last_jiffies) { | 
|  | 342 | unsigned long r0; | 
|  | 343 | __asm__(".set	push\n\t" | 
|  | 344 | ".set	mips3\n\t" | 
|  | 345 | "lwu	%0,%3\n\t" | 
|  | 346 | "dsll32	%1,%2,0\n\t" | 
|  | 347 | "or	%1,%1,%0\n\t" | 
|  | 348 | "ddivu	$0,%1,%4\n\t" | 
|  | 349 | "mflo	%1\n\t" | 
|  | 350 | "dsll32	%0,%5,0\n\t" | 
|  | 351 | "or	%0,%0,%6\n\t" | 
|  | 352 | "ddivu	$0,%0,%1\n\t" | 
|  | 353 | "mflo	%0\n\t" | 
|  | 354 | ".set	pop" | 
|  | 355 | : "=&r" (quotient), "=&r" (r0) | 
|  | 356 | : "r" (timerhi), "m" (timerlo), | 
|  | 357 | "r" (tmp), "r" (USECS_PER_JIFFY), | 
|  | 358 | "r" (USECS_PER_JIFFY_FRAC) | 
|  | 359 | : "hi", "lo", GCC_REG_ACCUM); | 
|  | 360 | cached_quotient = quotient; | 
|  | 361 | } | 
|  | 362 | } | 
|  | 363 |  | 
|  | 364 | /* Get last timer tick in absolute kernel time */ | 
|  | 365 | count = mips_hpt_read(); | 
|  | 366 |  | 
|  | 367 | /* .. relative to previous jiffy (32 bits is enough) */ | 
|  | 368 | count -= timerlo; | 
|  | 369 |  | 
|  | 370 | __asm__("multu	%1,%2" | 
|  | 371 | : "=h" (res) | 
|  | 372 | : "r" (count), "r" (quotient) | 
|  | 373 | : "lo", GCC_REG_ACCUM); | 
|  | 374 |  | 
|  | 375 | /* | 
|  | 376 | * Due to possible jiffies inconsistencies, we need to check | 
|  | 377 | * the result so that we'll get a timer that is monotonic. | 
|  | 378 | */ | 
|  | 379 | if (res >= USECS_PER_JIFFY) | 
|  | 380 | res = USECS_PER_JIFFY - 1; | 
|  | 381 |  | 
|  | 382 | return res; | 
|  | 383 | } | 
|  | 384 |  | 
|  | 385 |  | 
|  | 386 | /* last time when xtime and rtc are sync'ed up */ | 
|  | 387 | static long last_rtc_update; | 
|  | 388 |  | 
|  | 389 | /* | 
|  | 390 | * local_timer_interrupt() does profiling and process accounting | 
|  | 391 | * on a per-CPU basis. | 
|  | 392 | * | 
|  | 393 | * In UP mode, it is invoked from the (global) timer_interrupt. | 
|  | 394 | * | 
|  | 395 | * In SMP mode, it might invoked by per-CPU timer interrupt, or | 
|  | 396 | * a broadcasted inter-processor interrupt which itself is triggered | 
|  | 397 | * by the global timer interrupt. | 
|  | 398 | */ | 
|  | 399 | void local_timer_interrupt(int irq, void *dev_id, struct pt_regs *regs) | 
|  | 400 | { | 
|  | 401 | if (current->pid) | 
|  | 402 | profile_tick(CPU_PROFILING, regs); | 
|  | 403 | update_process_times(user_mode(regs)); | 
|  | 404 | } | 
|  | 405 |  | 
|  | 406 | /* | 
|  | 407 | * High-level timer interrupt service routines.  This function | 
|  | 408 | * is set as irqaction->handler and is invoked through do_IRQ. | 
|  | 409 | */ | 
|  | 410 | irqreturn_t timer_interrupt(int irq, void *dev_id, struct pt_regs *regs) | 
|  | 411 | { | 
|  | 412 | unsigned long j; | 
|  | 413 | unsigned int count; | 
|  | 414 |  | 
| Ralf Baechle | d6bd0e6 | 2006-03-14 23:46:58 +0000 | [diff] [blame] | 415 | write_seqlock(&xtime_lock); | 
|  | 416 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 417 | count = mips_hpt_read(); | 
|  | 418 | mips_timer_ack(); | 
|  | 419 |  | 
|  | 420 | /* Update timerhi/timerlo for intra-jiffy calibration. */ | 
|  | 421 | timerhi += count < timerlo;			/* Wrap around */ | 
|  | 422 | timerlo = count; | 
|  | 423 |  | 
|  | 424 | /* | 
|  | 425 | * call the generic timer interrupt handling | 
|  | 426 | */ | 
| Atsushi Nemoto | 3171a03 | 2006-09-29 02:00:32 -0700 | [diff] [blame] | 427 | do_timer(1); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 428 |  | 
|  | 429 | /* | 
|  | 430 | * If we have an externally synchronized Linux clock, then update | 
| Yoichi Yuasa | d23ee8f | 2006-03-27 01:16:33 -0800 | [diff] [blame] | 431 | * CMOS clock accordingly every ~11 minutes. rtc_mips_set_time() has to be | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 432 | * called as close as possible to 500 ms before the new second starts. | 
|  | 433 | */ | 
| john stultz | b149ee2 | 2005-09-06 15:17:46 -0700 | [diff] [blame] | 434 | if (ntp_synced() && | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 | xtime.tv_sec > last_rtc_update + 660 && | 
|  | 436 | (xtime.tv_nsec / 1000) >= 500000 - ((unsigned) TICK_SIZE) / 2 && | 
|  | 437 | (xtime.tv_nsec / 1000) <= 500000 + ((unsigned) TICK_SIZE) / 2) { | 
| Yoichi Yuasa | d23ee8f | 2006-03-27 01:16:33 -0800 | [diff] [blame] | 438 | if (rtc_mips_set_mmss(xtime.tv_sec) == 0) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 439 | last_rtc_update = xtime.tv_sec; | 
|  | 440 | } else { | 
|  | 441 | /* do it again in 60 s */ | 
|  | 442 | last_rtc_update = xtime.tv_sec - 600; | 
|  | 443 | } | 
|  | 444 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 445 |  | 
|  | 446 | /* | 
|  | 447 | * If jiffies has overflown in this timer_interrupt, we must | 
|  | 448 | * update the timer[hi]/[lo] to make fast gettimeoffset funcs | 
|  | 449 | * quotient calc still valid. -arca | 
|  | 450 | * | 
|  | 451 | * The first timer interrupt comes late as interrupts are | 
|  | 452 | * enabled long after timers are initialized.  Therefore the | 
|  | 453 | * high precision timer is fast, leading to wrong gettimeoffset() | 
|  | 454 | * calculations.  We deal with it by setting it based on the | 
|  | 455 | * number of its ticks between the second and the third interrupt. | 
|  | 456 | * That is still somewhat imprecise, but it's a good estimate. | 
|  | 457 | * --macro | 
|  | 458 | */ | 
|  | 459 | j = jiffies; | 
|  | 460 | if (j < 4) { | 
|  | 461 | static unsigned int prev_count; | 
|  | 462 | static int hpt_initialized; | 
|  | 463 |  | 
|  | 464 | switch (j) { | 
|  | 465 | case 0: | 
|  | 466 | timerhi = timerlo = 0; | 
|  | 467 | mips_hpt_init(count); | 
|  | 468 | break; | 
|  | 469 | case 2: | 
|  | 470 | prev_count = count; | 
|  | 471 | break; | 
|  | 472 | case 3: | 
|  | 473 | if (!hpt_initialized) { | 
|  | 474 | unsigned int c3 = 3 * (count - prev_count); | 
|  | 475 |  | 
|  | 476 | timerhi = 0; | 
|  | 477 | timerlo = c3; | 
|  | 478 | mips_hpt_init(count - c3); | 
|  | 479 | hpt_initialized = 1; | 
|  | 480 | } | 
|  | 481 | break; | 
|  | 482 | default: | 
|  | 483 | break; | 
|  | 484 | } | 
|  | 485 | } | 
|  | 486 |  | 
| Ralf Baechle | d6bd0e6 | 2006-03-14 23:46:58 +0000 | [diff] [blame] | 487 | write_sequnlock(&xtime_lock); | 
|  | 488 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 489 | /* | 
|  | 490 | * In UP mode, we call local_timer_interrupt() to do profiling | 
|  | 491 | * and process accouting. | 
|  | 492 | * | 
|  | 493 | * In SMP mode, local_timer_interrupt() is invoked by appropriate | 
|  | 494 | * low-level local timer interrupt handler. | 
|  | 495 | */ | 
|  | 496 | local_timer_interrupt(irq, dev_id, regs); | 
|  | 497 |  | 
|  | 498 | return IRQ_HANDLED; | 
|  | 499 | } | 
|  | 500 |  | 
| Ralf Baechle | ba339c0 | 2005-12-09 12:29:38 +0000 | [diff] [blame] | 501 | int null_perf_irq(struct pt_regs *regs) | 
|  | 502 | { | 
|  | 503 | return 0; | 
|  | 504 | } | 
|  | 505 |  | 
|  | 506 | int (*perf_irq)(struct pt_regs *regs) = null_perf_irq; | 
|  | 507 |  | 
|  | 508 | EXPORT_SYMBOL(null_perf_irq); | 
|  | 509 | EXPORT_SYMBOL(perf_irq); | 
|  | 510 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 511 | asmlinkage void ll_timer_interrupt(int irq, struct pt_regs *regs) | 
|  | 512 | { | 
| Ralf Baechle | ba339c0 | 2005-12-09 12:29:38 +0000 | [diff] [blame] | 513 | int r2 = cpu_has_mips_r2; | 
|  | 514 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 515 | irq_enter(); | 
|  | 516 | kstat_this_cpu.irqs[irq]++; | 
|  | 517 |  | 
| Ralf Baechle | ba339c0 | 2005-12-09 12:29:38 +0000 | [diff] [blame] | 518 | /* | 
|  | 519 | * Suckage alert: | 
|  | 520 | * Before R2 of the architecture there was no way to see if a | 
|  | 521 | * performance counter interrupt was pending, so we have to run the | 
|  | 522 | * performance counter interrupt handler anyway. | 
|  | 523 | */ | 
|  | 524 | if (!r2 || (read_c0_cause() & (1 << 26))) | 
|  | 525 | if (perf_irq(regs)) | 
|  | 526 | goto out; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 527 |  | 
| Ralf Baechle | ba339c0 | 2005-12-09 12:29:38 +0000 | [diff] [blame] | 528 | /* we keep interrupt disabled all the time */ | 
|  | 529 | if (!r2 || (read_c0_cause() & (1 << 30))) | 
|  | 530 | timer_interrupt(irq, NULL, regs); | 
|  | 531 |  | 
|  | 532 | out: | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 533 | irq_exit(); | 
|  | 534 | } | 
|  | 535 |  | 
|  | 536 | asmlinkage void ll_local_timer_interrupt(int irq, struct pt_regs *regs) | 
|  | 537 | { | 
|  | 538 | irq_enter(); | 
|  | 539 | if (smp_processor_id() != 0) | 
|  | 540 | kstat_this_cpu.irqs[irq]++; | 
|  | 541 |  | 
|  | 542 | /* we keep interrupt disabled all the time */ | 
|  | 543 | local_timer_interrupt(irq, NULL, regs); | 
|  | 544 |  | 
|  | 545 | irq_exit(); | 
|  | 546 | } | 
|  | 547 |  | 
|  | 548 | /* | 
|  | 549 | * time_init() - it does the following things. | 
|  | 550 | * | 
|  | 551 | * 1) board_time_init() - | 
|  | 552 | * 	a) (optional) set up RTC routines, | 
|  | 553 | *      b) (optional) calibrate and set the mips_hpt_frequency | 
|  | 554 | *	    (only needed if you intended to use fixed_rate_gettimeoffset | 
|  | 555 | *	     or use cpu counter as timer interrupt source) | 
| Yoichi Yuasa | d23ee8f | 2006-03-27 01:16:33 -0800 | [diff] [blame] | 556 | * 2) setup xtime based on rtc_mips_get_time(). | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 | * 3) choose a appropriate gettimeoffset routine. | 
|  | 558 | * 4) calculate a couple of cached variables for later usage | 
| Ralf Baechle | 54d0a21 | 2006-07-09 21:38:56 +0100 | [diff] [blame] | 559 | * 5) plat_timer_setup() - | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 560 | *	a) (optional) over-write any choices made above by time_init(). | 
|  | 561 | *	b) machine specific code should setup the timer irqaction. | 
|  | 562 | *	c) enable the timer interrupt | 
|  | 563 | */ | 
|  | 564 |  | 
|  | 565 | void (*board_time_init)(void); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 566 |  | 
|  | 567 | unsigned int mips_hpt_frequency; | 
|  | 568 |  | 
|  | 569 | static struct irqaction timer_irqaction = { | 
|  | 570 | .handler = timer_interrupt, | 
| Thomas Gleixner | f40298f | 2006-07-01 19:29:20 -0700 | [diff] [blame] | 571 | .flags = IRQF_DISABLED, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 572 | .name = "timer", | 
|  | 573 | }; | 
|  | 574 |  | 
|  | 575 | static unsigned int __init calibrate_hpt(void) | 
|  | 576 | { | 
|  | 577 | u64 frequency; | 
|  | 578 | u32 hpt_start, hpt_end, hpt_count, hz; | 
|  | 579 |  | 
|  | 580 | const int loops = HZ / 10; | 
|  | 581 | int log_2_loops = 0; | 
|  | 582 | int i; | 
|  | 583 |  | 
|  | 584 | /* | 
|  | 585 | * We want to calibrate for 0.1s, but to avoid a 64-bit | 
|  | 586 | * division we round the number of loops up to the nearest | 
|  | 587 | * power of 2. | 
|  | 588 | */ | 
|  | 589 | while (loops > 1 << log_2_loops) | 
|  | 590 | log_2_loops++; | 
|  | 591 | i = 1 << log_2_loops; | 
|  | 592 |  | 
|  | 593 | /* | 
|  | 594 | * Wait for a rising edge of the timer interrupt. | 
|  | 595 | */ | 
|  | 596 | while (mips_timer_state()); | 
|  | 597 | while (!mips_timer_state()); | 
|  | 598 |  | 
|  | 599 | /* | 
|  | 600 | * Now see how many high precision timer ticks happen | 
|  | 601 | * during the calculated number of periods between timer | 
|  | 602 | * interrupts. | 
|  | 603 | */ | 
|  | 604 | hpt_start = mips_hpt_read(); | 
|  | 605 | do { | 
|  | 606 | while (mips_timer_state()); | 
|  | 607 | while (!mips_timer_state()); | 
|  | 608 | } while (--i); | 
|  | 609 | hpt_end = mips_hpt_read(); | 
|  | 610 |  | 
|  | 611 | hpt_count = hpt_end - hpt_start; | 
|  | 612 | hz = HZ; | 
|  | 613 | frequency = (u64)hpt_count * (u64)hz; | 
|  | 614 |  | 
|  | 615 | return frequency >> log_2_loops; | 
|  | 616 | } | 
|  | 617 |  | 
|  | 618 | void __init time_init(void) | 
|  | 619 | { | 
|  | 620 | if (board_time_init) | 
|  | 621 | board_time_init(); | 
|  | 622 |  | 
| Yoichi Yuasa | d23ee8f | 2006-03-27 01:16:33 -0800 | [diff] [blame] | 623 | if (!rtc_mips_set_mmss) | 
|  | 624 | rtc_mips_set_mmss = rtc_mips_set_time; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 625 |  | 
| Yoichi Yuasa | d23ee8f | 2006-03-27 01:16:33 -0800 | [diff] [blame] | 626 | xtime.tv_sec = rtc_mips_get_time(); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 627 | xtime.tv_nsec = 0; | 
|  | 628 |  | 
|  | 629 | set_normalized_timespec(&wall_to_monotonic, | 
|  | 630 | -xtime.tv_sec, -xtime.tv_nsec); | 
|  | 631 |  | 
|  | 632 | /* Choose appropriate high precision timer routines.  */ | 
|  | 633 | if (!cpu_has_counter && !mips_hpt_read) { | 
|  | 634 | /* No high precision timer -- sorry.  */ | 
|  | 635 | mips_hpt_read = null_hpt_read; | 
|  | 636 | mips_hpt_init = null_hpt_init; | 
|  | 637 | } else if (!mips_hpt_frequency && !mips_timer_state) { | 
|  | 638 | /* A high precision timer of unknown frequency.  */ | 
|  | 639 | if (!mips_hpt_read) { | 
|  | 640 | /* No external high precision timer -- use R4k.  */ | 
|  | 641 | mips_hpt_read = c0_hpt_read; | 
|  | 642 | mips_hpt_init = c0_hpt_init; | 
|  | 643 | } | 
|  | 644 |  | 
| Ralf Baechle | b4672d3 | 2005-12-08 14:04:24 +0000 | [diff] [blame] | 645 | if (cpu_has_mips32r1 || cpu_has_mips32r2 || | 
|  | 646 | (current_cpu_data.isa_level == MIPS_CPU_ISA_I) || | 
|  | 647 | (current_cpu_data.isa_level == MIPS_CPU_ISA_II)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 648 | /* | 
|  | 649 | * We need to calibrate the counter but we don't have | 
|  | 650 | * 64-bit division. | 
|  | 651 | */ | 
|  | 652 | do_gettimeoffset = calibrate_div32_gettimeoffset; | 
|  | 653 | else | 
|  | 654 | /* | 
|  | 655 | * We need to calibrate the counter but we *do* have | 
|  | 656 | * 64-bit division. | 
|  | 657 | */ | 
|  | 658 | do_gettimeoffset = calibrate_div64_gettimeoffset; | 
|  | 659 | } else { | 
|  | 660 | /* We know counter frequency.  Or we can get it.  */ | 
|  | 661 | if (!mips_hpt_read) { | 
|  | 662 | /* No external high precision timer -- use R4k.  */ | 
|  | 663 | mips_hpt_read = c0_hpt_read; | 
|  | 664 |  | 
|  | 665 | if (mips_timer_state) | 
|  | 666 | mips_hpt_init = c0_hpt_init; | 
|  | 667 | else { | 
|  | 668 | /* No external timer interrupt -- use R4k.  */ | 
|  | 669 | mips_hpt_init = c0_hpt_timer_init; | 
|  | 670 | mips_timer_ack = c0_timer_ack; | 
|  | 671 | } | 
|  | 672 | } | 
|  | 673 | if (!mips_hpt_frequency) | 
|  | 674 | mips_hpt_frequency = calibrate_hpt(); | 
|  | 675 |  | 
|  | 676 | do_gettimeoffset = fixed_rate_gettimeoffset; | 
|  | 677 |  | 
|  | 678 | /* Calculate cache parameters.  */ | 
|  | 679 | cycles_per_jiffy = (mips_hpt_frequency + HZ / 2) / HZ; | 
|  | 680 |  | 
|  | 681 | /* sll32_usecs_per_cycle = 10^6 * 2^32 / mips_counter_freq  */ | 
|  | 682 | do_div64_32(sll32_usecs_per_cycle, | 
|  | 683 | 1000000, mips_hpt_frequency / 2, | 
|  | 684 | mips_hpt_frequency); | 
|  | 685 |  | 
|  | 686 | /* Report the high precision timer rate for a reference.  */ | 
|  | 687 | printk("Using %u.%03u MHz high precision timer.\n", | 
|  | 688 | ((mips_hpt_frequency + 500) / 1000) / 1000, | 
|  | 689 | ((mips_hpt_frequency + 500) / 1000) % 1000); | 
|  | 690 | } | 
|  | 691 |  | 
|  | 692 | if (!mips_timer_ack) | 
|  | 693 | /* No timer interrupt ack (e.g. i8254).  */ | 
|  | 694 | mips_timer_ack = null_timer_ack; | 
|  | 695 |  | 
|  | 696 | /* This sets up the high precision timer for the first interrupt.  */ | 
|  | 697 | mips_hpt_init(mips_hpt_read()); | 
|  | 698 |  | 
|  | 699 | /* | 
|  | 700 | * Call board specific timer interrupt setup. | 
|  | 701 | * | 
|  | 702 | * this pointer must be setup in machine setup routine. | 
|  | 703 | * | 
|  | 704 | * Even if a machine chooses to use a low-level timer interrupt, | 
|  | 705 | * it still needs to setup the timer_irqaction. | 
|  | 706 | * In that case, it might be better to set timer_irqaction.handler | 
|  | 707 | * to be NULL function so that we are sure the high-level code | 
|  | 708 | * is not invoked accidentally. | 
|  | 709 | */ | 
| Ralf Baechle | 54d0a21 | 2006-07-09 21:38:56 +0100 | [diff] [blame] | 710 | plat_timer_setup(&timer_irqaction); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 711 | } | 
|  | 712 |  | 
|  | 713 | #define FEBRUARY		2 | 
|  | 714 | #define STARTOFTIME		1970 | 
|  | 715 | #define SECDAY			86400L | 
|  | 716 | #define SECYR			(SECDAY * 365) | 
|  | 717 | #define leapyear(y)		((!((y) % 4) && ((y) % 100)) || !((y) % 400)) | 
|  | 718 | #define days_in_year(y)		(leapyear(y) ? 366 : 365) | 
|  | 719 | #define days_in_month(m)	(month_days[(m) - 1]) | 
|  | 720 |  | 
|  | 721 | static int month_days[12] = { | 
|  | 722 | 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 | 
|  | 723 | }; | 
|  | 724 |  | 
|  | 725 | void to_tm(unsigned long tim, struct rtc_time *tm) | 
|  | 726 | { | 
|  | 727 | long hms, day, gday; | 
|  | 728 | int i; | 
|  | 729 |  | 
|  | 730 | gday = day = tim / SECDAY; | 
|  | 731 | hms = tim % SECDAY; | 
|  | 732 |  | 
|  | 733 | /* Hours, minutes, seconds are easy */ | 
|  | 734 | tm->tm_hour = hms / 3600; | 
|  | 735 | tm->tm_min = (hms % 3600) / 60; | 
|  | 736 | tm->tm_sec = (hms % 3600) % 60; | 
|  | 737 |  | 
|  | 738 | /* Number of years in days */ | 
|  | 739 | for (i = STARTOFTIME; day >= days_in_year(i); i++) | 
|  | 740 | day -= days_in_year(i); | 
|  | 741 | tm->tm_year = i; | 
|  | 742 |  | 
|  | 743 | /* Number of months in days left */ | 
|  | 744 | if (leapyear(tm->tm_year)) | 
|  | 745 | days_in_month(FEBRUARY) = 29; | 
|  | 746 | for (i = 1; day >= days_in_month(i); i++) | 
|  | 747 | day -= days_in_month(i); | 
|  | 748 | days_in_month(FEBRUARY) = 28; | 
|  | 749 | tm->tm_mon = i - 1;		/* tm_mon starts from 0 to 11 */ | 
|  | 750 |  | 
|  | 751 | /* Days are what is left over (+1) from all that. */ | 
|  | 752 | tm->tm_mday = day + 1; | 
|  | 753 |  | 
|  | 754 | /* | 
|  | 755 | * Determine the day of week | 
|  | 756 | */ | 
|  | 757 | tm->tm_wday = (gday + 4) % 7;	/* 1970/1/1 was Thursday */ | 
|  | 758 | } | 
|  | 759 |  | 
|  | 760 | EXPORT_SYMBOL(rtc_lock); | 
|  | 761 | EXPORT_SYMBOL(to_tm); | 
| Yoichi Yuasa | d23ee8f | 2006-03-27 01:16:33 -0800 | [diff] [blame] | 762 | EXPORT_SYMBOL(rtc_mips_set_time); | 
|  | 763 | EXPORT_SYMBOL(rtc_mips_get_time); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 764 |  | 
|  | 765 | unsigned long long sched_clock(void) | 
|  | 766 | { | 
|  | 767 | return (unsigned long long)jiffies*(1000000000/HZ); | 
|  | 768 | } |