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 | */ |
Ralf Baechle | 91a2fcc | 2007-10-11 23:46:09 +0100 | [diff] [blame^] | 147 | static 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 | |
Ralf Baechle | 91a2fcc | 2007-10-11 23:46:09 +0100 | [diff] [blame^] | 177 | EXPORT_SYMBOL(null_perf_irq); |
| 178 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 179 | int (*perf_irq)(void) = null_perf_irq; |
Ralf Baechle | ba339c0 | 2005-12-09 12:29:38 +0000 | [diff] [blame] | 180 | |
Ralf Baechle | ba339c0 | 2005-12-09 12:29:38 +0000 | [diff] [blame] | 181 | EXPORT_SYMBOL(perf_irq); |
| 182 | |
Chris Dearman | ffe9ee4 | 2007-05-24 22:24:20 +0100 | [diff] [blame] | 183 | /* |
Ralf Baechle | 3b1d4ed | 2007-06-20 22:27:10 +0100 | [diff] [blame] | 184 | * Timer interrupt |
| 185 | */ |
| 186 | int cp0_compare_irq; |
| 187 | |
| 188 | /* |
Chris Dearman | ffe9ee4 | 2007-05-24 22:24:20 +0100 | [diff] [blame] | 189 | * Performance counter IRQ or -1 if shared with timer |
| 190 | */ |
Ralf Baechle | 3b1d4ed | 2007-06-20 22:27:10 +0100 | [diff] [blame] | 191 | int cp0_perfcount_irq; |
| 192 | EXPORT_SYMBOL_GPL(cp0_perfcount_irq); |
Chris Dearman | ffe9ee4 | 2007-05-24 22:24:20 +0100 | [diff] [blame] | 193 | |
| 194 | /* |
| 195 | * Possibly handle a performance counter interrupt. |
| 196 | * Return true if the timer interrupt should not be checked |
| 197 | */ |
| 198 | static inline int handle_perf_irq (int r2) |
| 199 | { |
| 200 | /* |
| 201 | * The performance counter overflow interrupt may be shared with the |
Ralf Baechle | 3b1d4ed | 2007-06-20 22:27:10 +0100 | [diff] [blame] | 202 | * timer interrupt (cp0_perfcount_irq < 0). If it is and a |
Chris Dearman | ffe9ee4 | 2007-05-24 22:24:20 +0100 | [diff] [blame] | 203 | * performance counter has overflowed (perf_irq() == IRQ_HANDLED) |
| 204 | * and we can't reliably determine if a counter interrupt has also |
| 205 | * happened (!r2) then don't check for a timer interrupt. |
| 206 | */ |
Ralf Baechle | 3b1d4ed | 2007-06-20 22:27:10 +0100 | [diff] [blame] | 207 | return (cp0_perfcount_irq < 0) && |
Chris Dearman | ffe9ee4 | 2007-05-24 22:24:20 +0100 | [diff] [blame] | 208 | perf_irq() == IRQ_HANDLED && |
| 209 | !r2; |
| 210 | } |
| 211 | |
Ralf Baechle | 91a2fcc | 2007-10-11 23:46:09 +0100 | [diff] [blame^] | 212 | void ll_timer_interrupt(int irq, void *dev_id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | { |
Ralf Baechle | 91a2fcc | 2007-10-11 23:46:09 +0100 | [diff] [blame^] | 214 | int cpu = smp_processor_id(); |
| 215 | |
| 216 | #ifdef CONFIG_MIPS_MT_SMTC |
| 217 | /* |
| 218 | * In an SMTC system, one Count/Compare set exists per VPE. |
| 219 | * Which TC within a VPE gets the interrupt is essentially |
| 220 | * random - we only know that it shouldn't be one with |
| 221 | * IXMT set. Whichever TC gets the interrupt needs to |
| 222 | * send special interprocessor interrupts to the other |
| 223 | * TCs to make sure that they schedule, etc. |
| 224 | * |
| 225 | * That code is specific to the SMTC kernel, not to |
| 226 | * the a particular platform, so it's invoked from |
| 227 | * the general MIPS timer_interrupt routine. |
| 228 | */ |
| 229 | |
| 230 | /* |
| 231 | * We could be here due to timer interrupt, |
| 232 | * perf counter overflow, or both. |
| 233 | */ |
| 234 | (void) handle_perf_irq(1); |
| 235 | |
| 236 | if (read_c0_cause() & (1 << 30)) { |
| 237 | /* |
| 238 | * There are things we only want to do once per tick |
| 239 | * in an "MP" system. One TC of each VPE will take |
| 240 | * the actual timer interrupt. The others will get |
| 241 | * timer broadcast IPIs. We use whoever it is that takes |
| 242 | * the tick on VPE 0 to run the full timer_interrupt(). |
| 243 | */ |
| 244 | if (cpu_data[cpu].vpe_id == 0) { |
| 245 | timer_interrupt(irq, NULL); |
| 246 | } else { |
| 247 | write_c0_compare(read_c0_count() + |
| 248 | (mips_hpt_frequency/HZ)); |
| 249 | local_timer_interrupt(irq, dev_id); |
| 250 | } |
| 251 | smtc_timer_broadcast(cpu_data[cpu].vpe_id); |
| 252 | } |
| 253 | #else /* CONFIG_MIPS_MT_SMTC */ |
Ralf Baechle | ba339c0 | 2005-12-09 12:29:38 +0000 | [diff] [blame] | 254 | int r2 = cpu_has_mips_r2; |
| 255 | |
Chris Dearman | ffe9ee4 | 2007-05-24 22:24:20 +0100 | [diff] [blame] | 256 | if (handle_perf_irq(r2)) |
Ralf Baechle | 91a2fcc | 2007-10-11 23:46:09 +0100 | [diff] [blame^] | 257 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | |
Chris Dearman | ffe9ee4 | 2007-05-24 22:24:20 +0100 | [diff] [blame] | 259 | if (r2 && ((read_c0_cause() & (1 << 30)) == 0)) |
Ralf Baechle | 91a2fcc | 2007-10-11 23:46:09 +0100 | [diff] [blame^] | 260 | return; |
Chris Dearman | ffe9ee4 | 2007-05-24 22:24:20 +0100 | [diff] [blame] | 261 | |
Ralf Baechle | 91a2fcc | 2007-10-11 23:46:09 +0100 | [diff] [blame^] | 262 | if (cpu == 0) { |
| 263 | /* |
| 264 | * CPU 0 handles the global timer interrupt job and process |
| 265 | * accounting resets count/compare registers to trigger next |
| 266 | * timer int. |
| 267 | */ |
| 268 | timer_interrupt(irq, NULL); |
| 269 | } else { |
| 270 | /* Everyone else needs to reset the timer int here as |
| 271 | ll_local_timer_interrupt doesn't */ |
| 272 | /* |
| 273 | * FIXME: need to cope with counter underflow. |
| 274 | * More support needs to be added to kernel/time for |
| 275 | * counter/timer interrupts on multiple CPU's |
| 276 | */ |
| 277 | write_c0_compare(read_c0_count() + (mips_hpt_frequency/HZ)); |
Ralf Baechle | ba339c0 | 2005-12-09 12:29:38 +0000 | [diff] [blame] | 278 | |
Ralf Baechle | 91a2fcc | 2007-10-11 23:46:09 +0100 | [diff] [blame^] | 279 | /* |
| 280 | * Other CPUs should do profiling and process accounting |
| 281 | */ |
| 282 | local_timer_interrupt(irq, dev_id); |
| 283 | } |
| 284 | #endif /* CONFIG_MIPS_MT_SMTC */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | /* |
| 288 | * time_init() - it does the following things. |
| 289 | * |
Ralf Baechle | 4b55048 | 2007-10-11 23:46:08 +0100 | [diff] [blame] | 290 | * 1) plat_time_init() - |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | * a) (optional) set up RTC routines, |
| 292 | * b) (optional) calibrate and set the mips_hpt_frequency |
Atsushi Nemoto | 16b7b2a | 2006-10-24 00:21:27 +0900 | [diff] [blame] | 293 | * (only needed if you intended to use cpu counter as timer interrupt |
| 294 | * source) |
Ralf Baechle | 4b55048 | 2007-10-11 23:46:08 +0100 | [diff] [blame] | 295 | * 2) calculate a couple of cached variables for later usage |
| 296 | * 3) plat_timer_setup() - |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | * a) (optional) over-write any choices made above by time_init(). |
| 298 | * b) machine specific code should setup the timer irqaction. |
| 299 | * c) enable the timer interrupt |
| 300 | */ |
| 301 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | unsigned int mips_hpt_frequency; |
| 303 | |
| 304 | static struct irqaction timer_irqaction = { |
| 305 | .handler = timer_interrupt, |
Chris Dearman | ffe9ee4 | 2007-05-24 22:24:20 +0100 | [diff] [blame] | 306 | .flags = IRQF_DISABLED | IRQF_PERCPU, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | .name = "timer", |
| 308 | }; |
| 309 | |
| 310 | static unsigned int __init calibrate_hpt(void) |
| 311 | { |
Atsushi Nemoto | 0059856 | 2006-11-12 00:10:28 +0900 | [diff] [blame] | 312 | cycle_t frequency, hpt_start, hpt_end, hpt_count, hz; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 313 | |
| 314 | const int loops = HZ / 10; |
| 315 | int log_2_loops = 0; |
| 316 | int i; |
| 317 | |
| 318 | /* |
| 319 | * We want to calibrate for 0.1s, but to avoid a 64-bit |
| 320 | * division we round the number of loops up to the nearest |
| 321 | * power of 2. |
| 322 | */ |
| 323 | while (loops > 1 << log_2_loops) |
| 324 | log_2_loops++; |
| 325 | i = 1 << log_2_loops; |
| 326 | |
| 327 | /* |
| 328 | * Wait for a rising edge of the timer interrupt. |
| 329 | */ |
| 330 | while (mips_timer_state()); |
| 331 | while (!mips_timer_state()); |
| 332 | |
| 333 | /* |
| 334 | * Now see how many high precision timer ticks happen |
| 335 | * during the calculated number of periods between timer |
| 336 | * interrupts. |
| 337 | */ |
Atsushi Nemoto | 0059856 | 2006-11-12 00:10:28 +0900 | [diff] [blame] | 338 | hpt_start = clocksource_mips.read(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 339 | do { |
| 340 | while (mips_timer_state()); |
| 341 | while (!mips_timer_state()); |
| 342 | } while (--i); |
Atsushi Nemoto | 0059856 | 2006-11-12 00:10:28 +0900 | [diff] [blame] | 343 | hpt_end = clocksource_mips.read(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 344 | |
Atsushi Nemoto | 0059856 | 2006-11-12 00:10:28 +0900 | [diff] [blame] | 345 | hpt_count = (hpt_end - hpt_start) & clocksource_mips.mask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | hz = HZ; |
Atsushi Nemoto | 0059856 | 2006-11-12 00:10:28 +0900 | [diff] [blame] | 347 | frequency = hpt_count * hz; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 348 | |
| 349 | return frequency >> log_2_loops; |
| 350 | } |
| 351 | |
Atsushi Nemoto | 0059856 | 2006-11-12 00:10:28 +0900 | [diff] [blame] | 352 | struct clocksource clocksource_mips = { |
Atsushi Nemoto | 16b7b2a | 2006-10-24 00:21:27 +0900 | [diff] [blame] | 353 | .name = "MIPS", |
Franck Bui-Huu | 55d0b4e | 2007-05-04 17:36:44 +0200 | [diff] [blame] | 354 | .mask = CLOCKSOURCE_MASK(32), |
Thomas Gleixner | 877fe38 | 2007-02-16 01:27:40 -0800 | [diff] [blame] | 355 | .flags = CLOCK_SOURCE_IS_CONTINUOUS, |
Atsushi Nemoto | 16b7b2a | 2006-10-24 00:21:27 +0900 | [diff] [blame] | 356 | }; |
| 357 | |
| 358 | static void __init init_mips_clocksource(void) |
| 359 | { |
| 360 | u64 temp; |
| 361 | u32 shift; |
| 362 | |
Atsushi Nemoto | 0059856 | 2006-11-12 00:10:28 +0900 | [diff] [blame] | 363 | if (!mips_hpt_frequency || clocksource_mips.read == null_hpt_read) |
Atsushi Nemoto | 16b7b2a | 2006-10-24 00:21:27 +0900 | [diff] [blame] | 364 | return; |
| 365 | |
| 366 | /* Calclate a somewhat reasonable rating value */ |
| 367 | clocksource_mips.rating = 200 + mips_hpt_frequency / 10000000; |
| 368 | /* Find a shift value */ |
| 369 | for (shift = 32; shift > 0; shift--) { |
| 370 | temp = (u64) NSEC_PER_SEC << shift; |
| 371 | do_div(temp, mips_hpt_frequency); |
| 372 | if ((temp >> 32) == 0) |
| 373 | break; |
| 374 | } |
| 375 | clocksource_mips.shift = shift; |
| 376 | clocksource_mips.mult = (u32)temp; |
Atsushi Nemoto | 16b7b2a | 2006-10-24 00:21:27 +0900 | [diff] [blame] | 377 | |
| 378 | clocksource_register(&clocksource_mips); |
| 379 | } |
| 380 | |
Ralf Baechle | 4b55048 | 2007-10-11 23:46:08 +0100 | [diff] [blame] | 381 | void __init __weak plat_time_init(void) |
| 382 | { |
| 383 | } |
| 384 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | void __init time_init(void) |
| 386 | { |
Ralf Baechle | 4b55048 | 2007-10-11 23:46:08 +0100 | [diff] [blame] | 387 | plat_time_init(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 388 | |
| 389 | /* Choose appropriate high precision timer routines. */ |
Atsushi Nemoto | 0059856 | 2006-11-12 00:10:28 +0900 | [diff] [blame] | 390 | if (!cpu_has_counter && !clocksource_mips.read) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 391 | /* No high precision timer -- sorry. */ |
Atsushi Nemoto | 0059856 | 2006-11-12 00:10:28 +0900 | [diff] [blame] | 392 | clocksource_mips.read = null_hpt_read; |
Atsushi Nemoto | 16b7b2a | 2006-10-24 00:21:27 +0900 | [diff] [blame] | 393 | else if (!mips_hpt_frequency && !mips_timer_state) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 394 | /* A high precision timer of unknown frequency. */ |
Atsushi Nemoto | 0059856 | 2006-11-12 00:10:28 +0900 | [diff] [blame] | 395 | if (!clocksource_mips.read) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 396 | /* No external high precision timer -- use R4k. */ |
Atsushi Nemoto | 0059856 | 2006-11-12 00:10:28 +0900 | [diff] [blame] | 397 | clocksource_mips.read = c0_hpt_read; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 398 | } else { |
| 399 | /* We know counter frequency. Or we can get it. */ |
Atsushi Nemoto | 0059856 | 2006-11-12 00:10:28 +0900 | [diff] [blame] | 400 | if (!clocksource_mips.read) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 401 | /* No external high precision timer -- use R4k. */ |
Atsushi Nemoto | 0059856 | 2006-11-12 00:10:28 +0900 | [diff] [blame] | 402 | clocksource_mips.read = c0_hpt_read; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 403 | |
Atsushi Nemoto | 16b7b2a | 2006-10-24 00:21:27 +0900 | [diff] [blame] | 404 | if (!mips_timer_state) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 405 | /* No external timer interrupt -- use R4k. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 406 | mips_timer_ack = c0_timer_ack; |
Atsushi Nemoto | c87b6eb | 2006-10-28 01:14:37 +0900 | [diff] [blame] | 407 | /* Calculate cache parameters. */ |
| 408 | cycles_per_jiffy = |
| 409 | (mips_hpt_frequency + HZ / 2) / HZ; |
| 410 | /* |
| 411 | * This sets up the high precision |
| 412 | * timer for the first interrupt. |
| 413 | */ |
| 414 | c0_hpt_timer_init(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 415 | } |
| 416 | } |
| 417 | if (!mips_hpt_frequency) |
| 418 | mips_hpt_frequency = calibrate_hpt(); |
| 419 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 420 | /* Report the high precision timer rate for a reference. */ |
| 421 | printk("Using %u.%03u MHz high precision timer.\n", |
| 422 | ((mips_hpt_frequency + 500) / 1000) / 1000, |
| 423 | ((mips_hpt_frequency + 500) / 1000) % 1000); |
| 424 | } |
| 425 | |
| 426 | if (!mips_timer_ack) |
| 427 | /* No timer interrupt ack (e.g. i8254). */ |
| 428 | mips_timer_ack = null_timer_ack; |
| 429 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 430 | /* |
| 431 | * Call board specific timer interrupt setup. |
| 432 | * |
| 433 | * this pointer must be setup in machine setup routine. |
| 434 | * |
| 435 | * Even if a machine chooses to use a low-level timer interrupt, |
| 436 | * it still needs to setup the timer_irqaction. |
| 437 | * In that case, it might be better to set timer_irqaction.handler |
| 438 | * to be NULL function so that we are sure the high-level code |
| 439 | * is not invoked accidentally. |
| 440 | */ |
Ralf Baechle | 54d0a21 | 2006-07-09 21:38:56 +0100 | [diff] [blame] | 441 | plat_timer_setup(&timer_irqaction); |
Atsushi Nemoto | 16b7b2a | 2006-10-24 00:21:27 +0900 | [diff] [blame] | 442 | |
| 443 | init_mips_clocksource(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 444 | } |