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