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 | */ |
Atsushi Nemoto | 16b7b2a | 2006-10-24 00:21:27 +0900 | [diff] [blame] | 14 | #include <linux/clocksource.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #include <linux/types.h> |
| 16 | #include <linux/kernel.h> |
| 17 | #include <linux/init.h> |
| 18 | #include <linux/sched.h> |
| 19 | #include <linux/param.h> |
| 20 | #include <linux/time.h> |
| 21 | #include <linux/timex.h> |
| 22 | #include <linux/smp.h> |
| 23 | #include <linux/kernel_stat.h> |
| 24 | #include <linux/spinlock.h> |
| 25 | #include <linux/interrupt.h> |
| 26 | #include <linux/module.h> |
| 27 | |
| 28 | #include <asm/bootinfo.h> |
Ralf Baechle | ec74e36 | 2005-07-13 11:48:45 +0000 | [diff] [blame] | 29 | #include <asm/cache.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | #include <asm/compiler.h> |
| 31 | #include <asm/cpu.h> |
| 32 | #include <asm/cpu-features.h> |
| 33 | #include <asm/div64.h> |
| 34 | #include <asm/sections.h> |
| 35 | #include <asm/time.h> |
| 36 | |
| 37 | /* |
| 38 | * The integer part of the number of usecs per jiffy is taken from tick, |
| 39 | * but the fractional part is not recorded, so we calculate it using the |
| 40 | * initial value of HZ. This aids systems where tick isn't really an |
| 41 | * integer (e.g. for HZ = 128). |
| 42 | */ |
| 43 | #define USECS_PER_JIFFY TICK_SIZE |
| 44 | #define USECS_PER_JIFFY_FRAC ((unsigned long)(u32)((1000000ULL << 32) / HZ)) |
| 45 | |
| 46 | #define TICK_SIZE (tick_nsec / 1000) |
| 47 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | /* |
| 49 | * forward reference |
| 50 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | DEFINE_SPINLOCK(rtc_lock); |
| 52 | |
| 53 | /* |
| 54 | * By default we provide the null RTC ops |
| 55 | */ |
| 56 | static unsigned long null_rtc_get_time(void) |
| 57 | { |
| 58 | return mktime(2000, 1, 1, 0, 0, 0); |
| 59 | } |
| 60 | |
| 61 | static int null_rtc_set_time(unsigned long sec) |
| 62 | { |
| 63 | return 0; |
| 64 | } |
| 65 | |
Yoichi Yuasa | d23ee8f | 2006-03-27 01:16:33 -0800 | [diff] [blame] | 66 | unsigned long (*rtc_mips_get_time)(void) = null_rtc_get_time; |
| 67 | int (*rtc_mips_set_time)(unsigned long) = null_rtc_set_time; |
| 68 | int (*rtc_mips_set_mmss)(unsigned long); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | |
| 70 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | /* how many counter cycles in a jiffy */ |
Ralf Baechle | ec74e36 | 2005-07-13 11:48:45 +0000 | [diff] [blame] | 72 | static unsigned long cycles_per_jiffy __read_mostly; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | /* expirelo is the count value for next CPU timer interrupt */ |
| 75 | static unsigned int expirelo; |
| 76 | |
| 77 | |
| 78 | /* |
| 79 | * Null timer ack for systems not needing one (e.g. i8254). |
| 80 | */ |
| 81 | static void null_timer_ack(void) { /* nothing */ } |
| 82 | |
| 83 | /* |
| 84 | * Null high precision timer functions for systems lacking one. |
| 85 | */ |
| 86 | static unsigned int null_hpt_read(void) |
| 87 | { |
| 88 | return 0; |
| 89 | } |
| 90 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | /* |
| 92 | * Timer ack for an R4k-compatible timer of a known frequency. |
| 93 | */ |
| 94 | static void c0_timer_ack(void) |
| 95 | { |
| 96 | unsigned int count; |
| 97 | |
Pete Popov | bdf21b1 | 2005-07-14 17:47:57 +0000 | [diff] [blame] | 98 | #ifndef CONFIG_SOC_PNX8550 /* pnx8550 resets to zero */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | /* Ack this timer interrupt and set the next one. */ |
| 100 | expirelo += cycles_per_jiffy; |
Pete Popov | bdf21b1 | 2005-07-14 17:47:57 +0000 | [diff] [blame] | 101 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | write_c0_compare(expirelo); |
| 103 | |
| 104 | /* Check to see if we have missed any timer interrupts. */ |
Ralf Baechle | 41c594a | 2006-04-05 09:45:45 +0100 | [diff] [blame] | 105 | while (((count = read_c0_count()) - expirelo) < 0x7fffffff) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | /* missed_timer_count++; */ |
| 107 | expirelo = count + cycles_per_jiffy; |
| 108 | write_c0_compare(expirelo); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | /* |
| 113 | * High precision timer functions for a R4k-compatible timer. |
| 114 | */ |
| 115 | static unsigned int c0_hpt_read(void) |
| 116 | { |
| 117 | return read_c0_count(); |
| 118 | } |
| 119 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | /* For use both as a high precision timer and an interrupt source. */ |
Atsushi Nemoto | 16b7b2a | 2006-10-24 00:21:27 +0900 | [diff] [blame] | 121 | static void __init c0_hpt_timer_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | { |
Atsushi Nemoto | 16b7b2a | 2006-10-24 00:21:27 +0900 | [diff] [blame] | 123 | expirelo = read_c0_count() + cycles_per_jiffy; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | write_c0_compare(expirelo); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | int (*mips_timer_state)(void); |
| 128 | void (*mips_timer_ack)(void); |
| 129 | unsigned int (*mips_hpt_read)(void); |
Atsushi Nemoto | 16b7b2a | 2006-10-24 00:21:27 +0900 | [diff] [blame] | 130 | unsigned int mips_hpt_mask = 0xffffffff; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | |
| 132 | /* last time when xtime and rtc are sync'ed up */ |
| 133 | static long last_rtc_update; |
| 134 | |
| 135 | /* |
| 136 | * local_timer_interrupt() does profiling and process accounting |
| 137 | * on a per-CPU basis. |
| 138 | * |
| 139 | * In UP mode, it is invoked from the (global) timer_interrupt. |
| 140 | * |
| 141 | * In SMP mode, it might invoked by per-CPU timer interrupt, or |
| 142 | * a broadcasted inter-processor interrupt which itself is triggered |
| 143 | * by the global timer interrupt. |
| 144 | */ |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 145 | void local_timer_interrupt(int irq, void *dev_id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | { |
Ralf Baechle | 937a801 | 2006-10-07 19:44:33 +0100 | [diff] [blame] | 147 | profile_tick(CPU_PROFILING); |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 148 | update_process_times(user_mode(get_irq_regs())); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | /* |
| 152 | * High-level timer interrupt service routines. This function |
| 153 | * is set as irqaction->handler and is invoked through do_IRQ. |
| 154 | */ |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 155 | irqreturn_t timer_interrupt(int irq, void *dev_id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | { |
Ralf Baechle | d6bd0e6 | 2006-03-14 23:46:58 +0000 | [diff] [blame] | 157 | write_seqlock(&xtime_lock); |
| 158 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | mips_timer_ack(); |
| 160 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | /* |
| 162 | * call the generic timer interrupt handling |
| 163 | */ |
Atsushi Nemoto | 3171a03 | 2006-09-29 02:00:32 -0700 | [diff] [blame] | 164 | do_timer(1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | |
| 166 | /* |
| 167 | * If we have an externally synchronized Linux clock, then update |
Yoichi Yuasa | d23ee8f | 2006-03-27 01:16:33 -0800 | [diff] [blame] | 168 | * 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] | 169 | * called as close as possible to 500 ms before the new second starts. |
| 170 | */ |
john stultz | b149ee2 | 2005-09-06 15:17:46 -0700 | [diff] [blame] | 171 | if (ntp_synced() && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | xtime.tv_sec > last_rtc_update + 660 && |
| 173 | (xtime.tv_nsec / 1000) >= 500000 - ((unsigned) TICK_SIZE) / 2 && |
| 174 | (xtime.tv_nsec / 1000) <= 500000 + ((unsigned) TICK_SIZE) / 2) { |
Yoichi Yuasa | d23ee8f | 2006-03-27 01:16:33 -0800 | [diff] [blame] | 175 | if (rtc_mips_set_mmss(xtime.tv_sec) == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | last_rtc_update = xtime.tv_sec; |
| 177 | } else { |
| 178 | /* do it again in 60 s */ |
| 179 | last_rtc_update = xtime.tv_sec - 600; |
| 180 | } |
| 181 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | |
Ralf Baechle | d6bd0e6 | 2006-03-14 23:46:58 +0000 | [diff] [blame] | 183 | write_sequnlock(&xtime_lock); |
| 184 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | /* |
| 186 | * In UP mode, we call local_timer_interrupt() to do profiling |
| 187 | * and process accouting. |
| 188 | * |
| 189 | * In SMP mode, local_timer_interrupt() is invoked by appropriate |
| 190 | * low-level local timer interrupt handler. |
| 191 | */ |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 192 | local_timer_interrupt(irq, dev_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | |
| 194 | return IRQ_HANDLED; |
| 195 | } |
| 196 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 197 | int null_perf_irq(void) |
Ralf Baechle | ba339c0 | 2005-12-09 12:29:38 +0000 | [diff] [blame] | 198 | { |
| 199 | return 0; |
| 200 | } |
| 201 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 202 | int (*perf_irq)(void) = null_perf_irq; |
Ralf Baechle | ba339c0 | 2005-12-09 12:29:38 +0000 | [diff] [blame] | 203 | |
| 204 | EXPORT_SYMBOL(null_perf_irq); |
| 205 | EXPORT_SYMBOL(perf_irq); |
| 206 | |
Ralf Baechle | 937a801 | 2006-10-07 19:44:33 +0100 | [diff] [blame] | 207 | asmlinkage void ll_timer_interrupt(int irq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | { |
Ralf Baechle | ba339c0 | 2005-12-09 12:29:38 +0000 | [diff] [blame] | 209 | int r2 = cpu_has_mips_r2; |
| 210 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | irq_enter(); |
| 212 | kstat_this_cpu.irqs[irq]++; |
| 213 | |
Ralf Baechle | ba339c0 | 2005-12-09 12:29:38 +0000 | [diff] [blame] | 214 | /* |
| 215 | * Suckage alert: |
| 216 | * Before R2 of the architecture there was no way to see if a |
| 217 | * performance counter interrupt was pending, so we have to run the |
| 218 | * performance counter interrupt handler anyway. |
| 219 | */ |
| 220 | if (!r2 || (read_c0_cause() & (1 << 26))) |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 221 | if (perf_irq()) |
Ralf Baechle | ba339c0 | 2005-12-09 12:29:38 +0000 | [diff] [blame] | 222 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | |
Ralf Baechle | ba339c0 | 2005-12-09 12:29:38 +0000 | [diff] [blame] | 224 | /* we keep interrupt disabled all the time */ |
| 225 | if (!r2 || (read_c0_cause() & (1 << 30))) |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 226 | timer_interrupt(irq, NULL); |
Ralf Baechle | ba339c0 | 2005-12-09 12:29:38 +0000 | [diff] [blame] | 227 | |
| 228 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | irq_exit(); |
| 230 | } |
| 231 | |
Ralf Baechle | 937a801 | 2006-10-07 19:44:33 +0100 | [diff] [blame] | 232 | asmlinkage void ll_local_timer_interrupt(int irq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | { |
| 234 | irq_enter(); |
| 235 | if (smp_processor_id() != 0) |
| 236 | kstat_this_cpu.irqs[irq]++; |
| 237 | |
| 238 | /* we keep interrupt disabled all the time */ |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 239 | local_timer_interrupt(irq, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | |
| 241 | irq_exit(); |
| 242 | } |
| 243 | |
| 244 | /* |
| 245 | * time_init() - it does the following things. |
| 246 | * |
| 247 | * 1) board_time_init() - |
| 248 | * a) (optional) set up RTC routines, |
| 249 | * b) (optional) calibrate and set the mips_hpt_frequency |
Atsushi Nemoto | 16b7b2a | 2006-10-24 00:21:27 +0900 | [diff] [blame] | 250 | * (only needed if you intended to use cpu counter as timer interrupt |
| 251 | * source) |
Yoichi Yuasa | d23ee8f | 2006-03-27 01:16:33 -0800 | [diff] [blame] | 252 | * 2) setup xtime based on rtc_mips_get_time(). |
Atsushi Nemoto | 16b7b2a | 2006-10-24 00:21:27 +0900 | [diff] [blame] | 253 | * 3) calculate a couple of cached variables for later usage |
| 254 | * 4) plat_timer_setup() - |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | * a) (optional) over-write any choices made above by time_init(). |
| 256 | * b) machine specific code should setup the timer irqaction. |
| 257 | * c) enable the timer interrupt |
| 258 | */ |
| 259 | |
| 260 | void (*board_time_init)(void); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | |
| 262 | unsigned int mips_hpt_frequency; |
| 263 | |
| 264 | static struct irqaction timer_irqaction = { |
| 265 | .handler = timer_interrupt, |
Thomas Gleixner | f40298f | 2006-07-01 19:29:20 -0700 | [diff] [blame] | 266 | .flags = IRQF_DISABLED, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | .name = "timer", |
| 268 | }; |
| 269 | |
| 270 | static unsigned int __init calibrate_hpt(void) |
| 271 | { |
| 272 | u64 frequency; |
| 273 | u32 hpt_start, hpt_end, hpt_count, hz; |
| 274 | |
| 275 | const int loops = HZ / 10; |
| 276 | int log_2_loops = 0; |
| 277 | int i; |
| 278 | |
| 279 | /* |
| 280 | * We want to calibrate for 0.1s, but to avoid a 64-bit |
| 281 | * division we round the number of loops up to the nearest |
| 282 | * power of 2. |
| 283 | */ |
| 284 | while (loops > 1 << log_2_loops) |
| 285 | log_2_loops++; |
| 286 | i = 1 << log_2_loops; |
| 287 | |
| 288 | /* |
| 289 | * Wait for a rising edge of the timer interrupt. |
| 290 | */ |
| 291 | while (mips_timer_state()); |
| 292 | while (!mips_timer_state()); |
| 293 | |
| 294 | /* |
| 295 | * Now see how many high precision timer ticks happen |
| 296 | * during the calculated number of periods between timer |
| 297 | * interrupts. |
| 298 | */ |
| 299 | hpt_start = mips_hpt_read(); |
| 300 | do { |
| 301 | while (mips_timer_state()); |
| 302 | while (!mips_timer_state()); |
| 303 | } while (--i); |
| 304 | hpt_end = mips_hpt_read(); |
| 305 | |
Atsushi Nemoto | 16b7b2a | 2006-10-24 00:21:27 +0900 | [diff] [blame] | 306 | hpt_count = (hpt_end - hpt_start) & mips_hpt_mask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | hz = HZ; |
| 308 | frequency = (u64)hpt_count * (u64)hz; |
| 309 | |
| 310 | return frequency >> log_2_loops; |
| 311 | } |
| 312 | |
Atsushi Nemoto | 16b7b2a | 2006-10-24 00:21:27 +0900 | [diff] [blame] | 313 | static cycle_t read_mips_hpt(void) |
| 314 | { |
| 315 | return (cycle_t)mips_hpt_read(); |
| 316 | } |
| 317 | |
| 318 | static struct clocksource clocksource_mips = { |
| 319 | .name = "MIPS", |
| 320 | .read = read_mips_hpt, |
| 321 | .is_continuous = 1, |
| 322 | }; |
| 323 | |
| 324 | static void __init init_mips_clocksource(void) |
| 325 | { |
| 326 | u64 temp; |
| 327 | u32 shift; |
| 328 | |
| 329 | if (!mips_hpt_frequency || mips_hpt_read == null_hpt_read) |
| 330 | return; |
| 331 | |
| 332 | /* Calclate a somewhat reasonable rating value */ |
| 333 | clocksource_mips.rating = 200 + mips_hpt_frequency / 10000000; |
| 334 | /* Find a shift value */ |
| 335 | for (shift = 32; shift > 0; shift--) { |
| 336 | temp = (u64) NSEC_PER_SEC << shift; |
| 337 | do_div(temp, mips_hpt_frequency); |
| 338 | if ((temp >> 32) == 0) |
| 339 | break; |
| 340 | } |
| 341 | clocksource_mips.shift = shift; |
| 342 | clocksource_mips.mult = (u32)temp; |
| 343 | clocksource_mips.mask = mips_hpt_mask; |
| 344 | |
| 345 | clocksource_register(&clocksource_mips); |
| 346 | } |
| 347 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 348 | void __init time_init(void) |
| 349 | { |
| 350 | if (board_time_init) |
| 351 | board_time_init(); |
| 352 | |
Yoichi Yuasa | d23ee8f | 2006-03-27 01:16:33 -0800 | [diff] [blame] | 353 | if (!rtc_mips_set_mmss) |
| 354 | rtc_mips_set_mmss = rtc_mips_set_time; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 355 | |
Yoichi Yuasa | d23ee8f | 2006-03-27 01:16:33 -0800 | [diff] [blame] | 356 | xtime.tv_sec = rtc_mips_get_time(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 357 | xtime.tv_nsec = 0; |
| 358 | |
| 359 | set_normalized_timespec(&wall_to_monotonic, |
| 360 | -xtime.tv_sec, -xtime.tv_nsec); |
| 361 | |
| 362 | /* Choose appropriate high precision timer routines. */ |
Atsushi Nemoto | 16b7b2a | 2006-10-24 00:21:27 +0900 | [diff] [blame] | 363 | if (!cpu_has_counter && !mips_hpt_read) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | /* No high precision timer -- sorry. */ |
| 365 | mips_hpt_read = null_hpt_read; |
Atsushi Nemoto | 16b7b2a | 2006-10-24 00:21:27 +0900 | [diff] [blame] | 366 | else if (!mips_hpt_frequency && !mips_timer_state) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 367 | /* A high precision timer of unknown frequency. */ |
Atsushi Nemoto | 16b7b2a | 2006-10-24 00:21:27 +0900 | [diff] [blame] | 368 | if (!mips_hpt_read) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 369 | /* No external high precision timer -- use R4k. */ |
| 370 | mips_hpt_read = c0_hpt_read; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 371 | } else { |
| 372 | /* We know counter frequency. Or we can get it. */ |
| 373 | if (!mips_hpt_read) { |
| 374 | /* No external high precision timer -- use R4k. */ |
| 375 | mips_hpt_read = c0_hpt_read; |
| 376 | |
Atsushi Nemoto | 16b7b2a | 2006-10-24 00:21:27 +0900 | [diff] [blame] | 377 | if (!mips_timer_state) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 378 | /* No external timer interrupt -- use R4k. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 379 | mips_timer_ack = c0_timer_ack; |
Atsushi Nemoto | c87b6eb | 2006-10-28 01:14:37 +0900 | [diff] [blame^] | 380 | /* Calculate cache parameters. */ |
| 381 | cycles_per_jiffy = |
| 382 | (mips_hpt_frequency + HZ / 2) / HZ; |
| 383 | /* |
| 384 | * This sets up the high precision |
| 385 | * timer for the first interrupt. |
| 386 | */ |
| 387 | c0_hpt_timer_init(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 388 | } |
| 389 | } |
| 390 | if (!mips_hpt_frequency) |
| 391 | mips_hpt_frequency = calibrate_hpt(); |
| 392 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 393 | /* Report the high precision timer rate for a reference. */ |
| 394 | printk("Using %u.%03u MHz high precision timer.\n", |
| 395 | ((mips_hpt_frequency + 500) / 1000) / 1000, |
| 396 | ((mips_hpt_frequency + 500) / 1000) % 1000); |
| 397 | } |
| 398 | |
| 399 | if (!mips_timer_ack) |
| 400 | /* No timer interrupt ack (e.g. i8254). */ |
| 401 | mips_timer_ack = null_timer_ack; |
| 402 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 403 | /* |
| 404 | * Call board specific timer interrupt setup. |
| 405 | * |
| 406 | * this pointer must be setup in machine setup routine. |
| 407 | * |
| 408 | * Even if a machine chooses to use a low-level timer interrupt, |
| 409 | * it still needs to setup the timer_irqaction. |
| 410 | * In that case, it might be better to set timer_irqaction.handler |
| 411 | * to be NULL function so that we are sure the high-level code |
| 412 | * is not invoked accidentally. |
| 413 | */ |
Ralf Baechle | 54d0a21 | 2006-07-09 21:38:56 +0100 | [diff] [blame] | 414 | plat_timer_setup(&timer_irqaction); |
Atsushi Nemoto | 16b7b2a | 2006-10-24 00:21:27 +0900 | [diff] [blame] | 415 | |
| 416 | init_mips_clocksource(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | #define FEBRUARY 2 |
| 420 | #define STARTOFTIME 1970 |
| 421 | #define SECDAY 86400L |
| 422 | #define SECYR (SECDAY * 365) |
| 423 | #define leapyear(y) ((!((y) % 4) && ((y) % 100)) || !((y) % 400)) |
| 424 | #define days_in_year(y) (leapyear(y) ? 366 : 365) |
| 425 | #define days_in_month(m) (month_days[(m) - 1]) |
| 426 | |
| 427 | static int month_days[12] = { |
| 428 | 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 |
| 429 | }; |
| 430 | |
| 431 | void to_tm(unsigned long tim, struct rtc_time *tm) |
| 432 | { |
| 433 | long hms, day, gday; |
| 434 | int i; |
| 435 | |
| 436 | gday = day = tim / SECDAY; |
| 437 | hms = tim % SECDAY; |
| 438 | |
| 439 | /* Hours, minutes, seconds are easy */ |
| 440 | tm->tm_hour = hms / 3600; |
| 441 | tm->tm_min = (hms % 3600) / 60; |
| 442 | tm->tm_sec = (hms % 3600) % 60; |
| 443 | |
| 444 | /* Number of years in days */ |
| 445 | for (i = STARTOFTIME; day >= days_in_year(i); i++) |
| 446 | day -= days_in_year(i); |
| 447 | tm->tm_year = i; |
| 448 | |
| 449 | /* Number of months in days left */ |
| 450 | if (leapyear(tm->tm_year)) |
| 451 | days_in_month(FEBRUARY) = 29; |
| 452 | for (i = 1; day >= days_in_month(i); i++) |
| 453 | day -= days_in_month(i); |
| 454 | days_in_month(FEBRUARY) = 28; |
| 455 | tm->tm_mon = i - 1; /* tm_mon starts from 0 to 11 */ |
| 456 | |
| 457 | /* Days are what is left over (+1) from all that. */ |
| 458 | tm->tm_mday = day + 1; |
| 459 | |
| 460 | /* |
| 461 | * Determine the day of week |
| 462 | */ |
| 463 | tm->tm_wday = (gday + 4) % 7; /* 1970/1/1 was Thursday */ |
| 464 | } |
| 465 | |
| 466 | EXPORT_SYMBOL(rtc_lock); |
| 467 | EXPORT_SYMBOL(to_tm); |
Yoichi Yuasa | d23ee8f | 2006-03-27 01:16:33 -0800 | [diff] [blame] | 468 | EXPORT_SYMBOL(rtc_mips_set_time); |
| 469 | EXPORT_SYMBOL(rtc_mips_get_time); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 470 | |
| 471 | unsigned long long sched_clock(void) |
| 472 | { |
| 473 | return (unsigned long long)jiffies*(1000000000/HZ); |
| 474 | } |