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 | */ |
Ralf Baechle | 656db50 | 2007-10-26 13:24:06 +0100 | [diff] [blame^] | 14 | #include <linux/bug.h> |
Ralf Baechle | 7bcf771 | 2007-10-11 23:46:09 +0100 | [diff] [blame] | 15 | #include <linux/clockchips.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #include <linux/types.h> |
| 17 | #include <linux/kernel.h> |
| 18 | #include <linux/init.h> |
| 19 | #include <linux/sched.h> |
| 20 | #include <linux/param.h> |
Yoichi Yuasa | b1043cc | 2007-09-13 13:13:28 +0900 | [diff] [blame] | 21 | #include <linux/profile.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | #include <linux/time.h> |
| 23 | #include <linux/timex.h> |
| 24 | #include <linux/smp.h> |
| 25 | #include <linux/kernel_stat.h> |
| 26 | #include <linux/spinlock.h> |
| 27 | #include <linux/interrupt.h> |
| 28 | #include <linux/module.h> |
Ralf Baechle | ea58040 | 2007-10-11 23:46:09 +0100 | [diff] [blame] | 29 | #include <linux/kallsyms.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | |
| 31 | #include <asm/bootinfo.h> |
Ralf Baechle | ec74e36 | 2005-07-13 11:48:45 +0000 | [diff] [blame] | 32 | #include <asm/cache.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | #include <asm/compiler.h> |
| 34 | #include <asm/cpu.h> |
| 35 | #include <asm/cpu-features.h> |
| 36 | #include <asm/div64.h> |
| 37 | #include <asm/sections.h> |
Ralf Baechle | ea58040 | 2007-10-11 23:46:09 +0100 | [diff] [blame] | 38 | #include <asm/smtc_ipi.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | #include <asm/time.h> |
| 40 | |
Ralf Baechle | 7bcf771 | 2007-10-11 23:46:09 +0100 | [diff] [blame] | 41 | #include <irq.h> |
| 42 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | * forward reference |
| 45 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | DEFINE_SPINLOCK(rtc_lock); |
Ralf Baechle | 4b55048 | 2007-10-11 23:46:08 +0100 | [diff] [blame] | 47 | EXPORT_SYMBOL(rtc_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | |
Ralf Baechle | 4b55048 | 2007-10-11 23:46:08 +0100 | [diff] [blame] | 49 | int __weak rtc_mips_set_time(unsigned long sec) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | { |
| 51 | return 0; |
| 52 | } |
Ralf Baechle | 4b55048 | 2007-10-11 23:46:08 +0100 | [diff] [blame] | 53 | EXPORT_SYMBOL(rtc_mips_set_time); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | |
Ralf Baechle | 4b55048 | 2007-10-11 23:46:08 +0100 | [diff] [blame] | 55 | int __weak rtc_mips_set_mmss(unsigned long nowtime) |
| 56 | { |
| 57 | return rtc_mips_set_time(nowtime); |
| 58 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | |
Ralf Baechle | f5ff0a2 | 2007-08-13 15:26:12 +0100 | [diff] [blame] | 60 | int update_persistent_clock(struct timespec now) |
| 61 | { |
| 62 | return rtc_mips_set_mmss(now.tv_sec); |
| 63 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | /* |
| 66 | * Null high precision timer functions for systems lacking one. |
| 67 | */ |
Atsushi Nemoto | 0059856 | 2006-11-12 00:10:28 +0900 | [diff] [blame] | 68 | static cycle_t null_hpt_read(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | { |
| 70 | return 0; |
| 71 | } |
| 72 | |
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 | * High precision timer functions for a R4k-compatible timer. |
| 75 | */ |
Atsushi Nemoto | 0059856 | 2006-11-12 00:10:28 +0900 | [diff] [blame] | 76 | static cycle_t c0_hpt_read(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | { |
| 78 | return read_c0_count(); |
| 79 | } |
| 80 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | int (*mips_timer_state)(void); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | /* |
| 84 | * local_timer_interrupt() does profiling and process accounting |
| 85 | * on a per-CPU basis. |
| 86 | * |
| 87 | * In UP mode, it is invoked from the (global) timer_interrupt. |
| 88 | * |
| 89 | * In SMP mode, it might invoked by per-CPU timer interrupt, or |
| 90 | * a broadcasted inter-processor interrupt which itself is triggered |
| 91 | * by the global timer interrupt. |
| 92 | */ |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 93 | void local_timer_interrupt(int irq, void *dev_id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | { |
Ralf Baechle | 937a801 | 2006-10-07 19:44:33 +0100 | [diff] [blame] | 95 | profile_tick(CPU_PROFILING); |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 96 | update_process_times(user_mode(get_irq_regs())); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | } |
| 98 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 99 | int null_perf_irq(void) |
Ralf Baechle | ba339c0 | 2005-12-09 12:29:38 +0000 | [diff] [blame] | 100 | { |
| 101 | return 0; |
| 102 | } |
| 103 | |
Ralf Baechle | 91a2fcc | 2007-10-11 23:46:09 +0100 | [diff] [blame] | 104 | EXPORT_SYMBOL(null_perf_irq); |
| 105 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 106 | int (*perf_irq)(void) = null_perf_irq; |
Ralf Baechle | ba339c0 | 2005-12-09 12:29:38 +0000 | [diff] [blame] | 107 | |
Ralf Baechle | ba339c0 | 2005-12-09 12:29:38 +0000 | [diff] [blame] | 108 | EXPORT_SYMBOL(perf_irq); |
| 109 | |
Chris Dearman | ffe9ee4 | 2007-05-24 22:24:20 +0100 | [diff] [blame] | 110 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | * time_init() - it does the following things. |
| 112 | * |
Ralf Baechle | 4b55048 | 2007-10-11 23:46:08 +0100 | [diff] [blame] | 113 | * 1) plat_time_init() - |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | * a) (optional) set up RTC routines, |
| 115 | * b) (optional) calibrate and set the mips_hpt_frequency |
Atsushi Nemoto | 16b7b2a | 2006-10-24 00:21:27 +0900 | [diff] [blame] | 116 | * (only needed if you intended to use cpu counter as timer interrupt |
| 117 | * source) |
Ralf Baechle | 4b55048 | 2007-10-11 23:46:08 +0100 | [diff] [blame] | 118 | * 2) calculate a couple of cached variables for later usage |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | */ |
| 120 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | unsigned int mips_hpt_frequency; |
| 122 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | static unsigned int __init calibrate_hpt(void) |
| 124 | { |
Atsushi Nemoto | 0059856 | 2006-11-12 00:10:28 +0900 | [diff] [blame] | 125 | cycle_t frequency, hpt_start, hpt_end, hpt_count, hz; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | |
| 127 | const int loops = HZ / 10; |
| 128 | int log_2_loops = 0; |
| 129 | int i; |
| 130 | |
| 131 | /* |
| 132 | * We want to calibrate for 0.1s, but to avoid a 64-bit |
| 133 | * division we round the number of loops up to the nearest |
| 134 | * power of 2. |
| 135 | */ |
| 136 | while (loops > 1 << log_2_loops) |
| 137 | log_2_loops++; |
| 138 | i = 1 << log_2_loops; |
| 139 | |
| 140 | /* |
| 141 | * Wait for a rising edge of the timer interrupt. |
| 142 | */ |
| 143 | while (mips_timer_state()); |
| 144 | while (!mips_timer_state()); |
| 145 | |
| 146 | /* |
| 147 | * Now see how many high precision timer ticks happen |
| 148 | * during the calculated number of periods between timer |
| 149 | * interrupts. |
| 150 | */ |
Atsushi Nemoto | 0059856 | 2006-11-12 00:10:28 +0900 | [diff] [blame] | 151 | hpt_start = clocksource_mips.read(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | do { |
| 153 | while (mips_timer_state()); |
| 154 | while (!mips_timer_state()); |
| 155 | } while (--i); |
Atsushi Nemoto | 0059856 | 2006-11-12 00:10:28 +0900 | [diff] [blame] | 156 | hpt_end = clocksource_mips.read(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | |
Atsushi Nemoto | 0059856 | 2006-11-12 00:10:28 +0900 | [diff] [blame] | 158 | hpt_count = (hpt_end - hpt_start) & clocksource_mips.mask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | hz = HZ; |
Atsushi Nemoto | 0059856 | 2006-11-12 00:10:28 +0900 | [diff] [blame] | 160 | frequency = hpt_count * hz; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | |
| 162 | return frequency >> log_2_loops; |
| 163 | } |
| 164 | |
Atsushi Nemoto | 0059856 | 2006-11-12 00:10:28 +0900 | [diff] [blame] | 165 | struct clocksource clocksource_mips = { |
Atsushi Nemoto | 16b7b2a | 2006-10-24 00:21:27 +0900 | [diff] [blame] | 166 | .name = "MIPS", |
Franck Bui-Huu | 55d0b4e | 2007-05-04 17:36:44 +0200 | [diff] [blame] | 167 | .mask = CLOCKSOURCE_MASK(32), |
Thomas Gleixner | 877fe38 | 2007-02-16 01:27:40 -0800 | [diff] [blame] | 168 | .flags = CLOCK_SOURCE_IS_CONTINUOUS, |
Atsushi Nemoto | 16b7b2a | 2006-10-24 00:21:27 +0900 | [diff] [blame] | 169 | }; |
| 170 | |
Ralf Baechle | 93c846f | 2007-10-19 08:13:08 +0100 | [diff] [blame] | 171 | void __init clocksource_set_clock(struct clocksource *cs, unsigned int clock) |
Atsushi Nemoto | 16b7b2a | 2006-10-24 00:21:27 +0900 | [diff] [blame] | 172 | { |
| 173 | u64 temp; |
| 174 | u32 shift; |
| 175 | |
Ralf Baechle | 93c846f | 2007-10-19 08:13:08 +0100 | [diff] [blame] | 176 | /* Find a shift value */ |
| 177 | for (shift = 32; shift > 0; shift--) { |
| 178 | temp = (u64) NSEC_PER_SEC << shift; |
| 179 | do_div(temp, clock); |
| 180 | if ((temp >> 32) == 0) |
| 181 | break; |
| 182 | } |
| 183 | cs->shift = shift; |
| 184 | cs->mult = (u32) temp; |
| 185 | } |
| 186 | |
| 187 | void __cpuinit clockevent_set_clock(struct clock_event_device *cd, |
| 188 | unsigned int clock) |
| 189 | { |
| 190 | u64 temp; |
| 191 | u32 shift; |
| 192 | |
| 193 | /* Find a shift value */ |
| 194 | for (shift = 32; shift > 0; shift--) { |
Atsushi Nemoto | 508a775 | 2007-10-20 00:28:33 +0900 | [diff] [blame] | 195 | temp = (u64) clock << shift; |
| 196 | do_div(temp, NSEC_PER_SEC); |
Ralf Baechle | 93c846f | 2007-10-19 08:13:08 +0100 | [diff] [blame] | 197 | if ((temp >> 32) == 0) |
| 198 | break; |
| 199 | } |
| 200 | cd->shift = shift; |
| 201 | cd->mult = (u32) temp; |
| 202 | } |
| 203 | |
| 204 | static void __init init_mips_clocksource(void) |
| 205 | { |
Atsushi Nemoto | 0059856 | 2006-11-12 00:10:28 +0900 | [diff] [blame] | 206 | if (!mips_hpt_frequency || clocksource_mips.read == null_hpt_read) |
Atsushi Nemoto | 16b7b2a | 2006-10-24 00:21:27 +0900 | [diff] [blame] | 207 | return; |
| 208 | |
| 209 | /* Calclate a somewhat reasonable rating value */ |
| 210 | clocksource_mips.rating = 200 + mips_hpt_frequency / 10000000; |
Ralf Baechle | 93c846f | 2007-10-19 08:13:08 +0100 | [diff] [blame] | 211 | |
| 212 | clocksource_set_clock(&clocksource_mips, mips_hpt_frequency); |
Atsushi Nemoto | 16b7b2a | 2006-10-24 00:21:27 +0900 | [diff] [blame] | 213 | |
| 214 | clocksource_register(&clocksource_mips); |
| 215 | } |
| 216 | |
Ralf Baechle | 4b55048 | 2007-10-11 23:46:08 +0100 | [diff] [blame] | 217 | void __init __weak plat_time_init(void) |
| 218 | { |
| 219 | } |
| 220 | |
Ralf Baechle | 656db50 | 2007-10-26 13:24:06 +0100 | [diff] [blame^] | 221 | /* |
| 222 | * This function exists in order to cause an error due to a duplicate |
| 223 | * definition if platform code should have its own implementation. The hook |
| 224 | * to use instead is plat_time_init. plat_time_init does not receive the |
| 225 | * irqaction pointer argument anymore. This is because any function which |
| 226 | * initializes an interrupt timer now takes care of its own request_irq rsp. |
| 227 | * setup_irq calls and each clock_event_device should use its own |
| 228 | * struct irqrequest. |
| 229 | */ |
| 230 | void __init plat_timer_setup(struct irqaction *irq) |
Ralf Baechle | 7bcf771 | 2007-10-11 23:46:09 +0100 | [diff] [blame] | 231 | { |
Ralf Baechle | 656db50 | 2007-10-26 13:24:06 +0100 | [diff] [blame^] | 232 | BUG(); |
Ralf Baechle | 7bcf771 | 2007-10-11 23:46:09 +0100 | [diff] [blame] | 233 | } |
| 234 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | void __init time_init(void) |
| 236 | { |
Ralf Baechle | 4b55048 | 2007-10-11 23:46:08 +0100 | [diff] [blame] | 237 | plat_time_init(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | |
| 239 | /* Choose appropriate high precision timer routines. */ |
Atsushi Nemoto | 0059856 | 2006-11-12 00:10:28 +0900 | [diff] [blame] | 240 | if (!cpu_has_counter && !clocksource_mips.read) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | /* No high precision timer -- sorry. */ |
Atsushi Nemoto | 0059856 | 2006-11-12 00:10:28 +0900 | [diff] [blame] | 242 | clocksource_mips.read = null_hpt_read; |
Atsushi Nemoto | 16b7b2a | 2006-10-24 00:21:27 +0900 | [diff] [blame] | 243 | else if (!mips_hpt_frequency && !mips_timer_state) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | /* A high precision timer of unknown frequency. */ |
Atsushi Nemoto | 0059856 | 2006-11-12 00:10:28 +0900 | [diff] [blame] | 245 | if (!clocksource_mips.read) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | /* No external high precision timer -- use R4k. */ |
Atsushi Nemoto | 0059856 | 2006-11-12 00:10:28 +0900 | [diff] [blame] | 247 | clocksource_mips.read = c0_hpt_read; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | } else { |
| 249 | /* We know counter frequency. Or we can get it. */ |
Atsushi Nemoto | 0059856 | 2006-11-12 00:10:28 +0900 | [diff] [blame] | 250 | if (!clocksource_mips.read) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | /* No external high precision timer -- use R4k. */ |
Atsushi Nemoto | 0059856 | 2006-11-12 00:10:28 +0900 | [diff] [blame] | 252 | clocksource_mips.read = c0_hpt_read; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | } |
| 254 | if (!mips_hpt_frequency) |
| 255 | mips_hpt_frequency = calibrate_hpt(); |
| 256 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | /* Report the high precision timer rate for a reference. */ |
| 258 | printk("Using %u.%03u MHz high precision timer.\n", |
| 259 | ((mips_hpt_frequency + 500) / 1000) / 1000, |
| 260 | ((mips_hpt_frequency + 500) / 1000) % 1000); |
| 261 | } |
| 262 | |
Atsushi Nemoto | 16b7b2a | 2006-10-24 00:21:27 +0900 | [diff] [blame] | 263 | init_mips_clocksource(); |
Ralf Baechle | 7bcf771 | 2007-10-11 23:46:09 +0100 | [diff] [blame] | 264 | mips_clockevent_init(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | } |