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