blob: c4e6866d5cbcce89f0a932577360698f6def6fae [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 Baechle7bcf7712007-10-11 23:46:09 +010014#include <linux/clockchips.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/types.h>
16#include <linux/kernel.h>
17#include <linux/init.h>
18#include <linux/sched.h>
19#include <linux/param.h>
Yoichi Yuasab1043cc2007-09-13 13:13:28 +090020#include <linux/profile.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/time.h>
22#include <linux/timex.h>
23#include <linux/smp.h>
24#include <linux/kernel_stat.h>
25#include <linux/spinlock.h>
26#include <linux/interrupt.h>
27#include <linux/module.h>
Ralf Baechleea580402007-10-11 23:46:09 +010028#include <linux/kallsyms.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
30#include <asm/bootinfo.h>
Ralf Baechleec74e362005-07-13 11:48:45 +000031#include <asm/cache.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <asm/compiler.h>
33#include <asm/cpu.h>
34#include <asm/cpu-features.h>
35#include <asm/div64.h>
36#include <asm/sections.h>
Ralf Baechleea580402007-10-11 23:46:09 +010037#include <asm/smtc_ipi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <asm/time.h>
39
Ralf Baechle7bcf7712007-10-11 23:46:09 +010040#include <irq.h>
41
Linus Torvalds1da177e2005-04-16 15:20:36 -070042/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 * forward reference
44 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070045DEFINE_SPINLOCK(rtc_lock);
Ralf Baechle4b550482007-10-11 23:46:08 +010046EXPORT_SYMBOL(rtc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Ralf Baechle4b550482007-10-11 23:46:08 +010048int __weak rtc_mips_set_time(unsigned long sec)
Linus Torvalds1da177e2005-04-16 15:20:36 -070049{
50 return 0;
51}
Ralf Baechle4b550482007-10-11 23:46:08 +010052EXPORT_SYMBOL(rtc_mips_set_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Ralf Baechle4b550482007-10-11 23:46:08 +010054int __weak rtc_mips_set_mmss(unsigned long nowtime)
55{
56 return rtc_mips_set_time(nowtime);
57}
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Ralf Baechlef5ff0a22007-08-13 15:26:12 +010059int update_persistent_clock(struct timespec now)
60{
61 return rtc_mips_set_mmss(now.tv_sec);
62}
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Linus Torvalds1da177e2005-04-16 15:20:36 -070064/*
65 * Null high precision timer functions for systems lacking one.
66 */
Atsushi Nemoto00598562006-11-12 00:10:28 +090067static cycle_t null_hpt_read(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070068{
69 return 0;
70}
71
Linus Torvalds1da177e2005-04-16 15:20:36 -070072/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 * High precision timer functions for a R4k-compatible timer.
74 */
Atsushi Nemoto00598562006-11-12 00:10:28 +090075static cycle_t c0_hpt_read(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070076{
77 return read_c0_count();
78}
79
Linus Torvalds1da177e2005-04-16 15:20:36 -070080int (*mips_timer_state)(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
Linus Torvalds1da177e2005-04-16 15:20:36 -070082/*
83 * local_timer_interrupt() does profiling and process accounting
84 * on a per-CPU basis.
85 *
86 * In UP mode, it is invoked from the (global) timer_interrupt.
87 *
88 * In SMP mode, it might invoked by per-CPU timer interrupt, or
89 * a broadcasted inter-processor interrupt which itself is triggered
90 * by the global timer interrupt.
91 */
David Howells7d12e782006-10-05 14:55:46 +010092void local_timer_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -070093{
Ralf Baechle937a8012006-10-07 19:44:33 +010094 profile_tick(CPU_PROFILING);
David Howells7d12e782006-10-05 14:55:46 +010095 update_process_times(user_mode(get_irq_regs()));
Linus Torvalds1da177e2005-04-16 15:20:36 -070096}
97
David Howells7d12e782006-10-05 14:55:46 +010098int null_perf_irq(void)
Ralf Baechleba339c02005-12-09 12:29:38 +000099{
100 return 0;
101}
102
Ralf Baechle91a2fcc2007-10-11 23:46:09 +0100103EXPORT_SYMBOL(null_perf_irq);
104
David Howells7d12e782006-10-05 14:55:46 +0100105int (*perf_irq)(void) = null_perf_irq;
Ralf Baechleba339c02005-12-09 12:29:38 +0000106
Ralf Baechleba339c02005-12-09 12:29:38 +0000107EXPORT_SYMBOL(perf_irq);
108
Chris Dearmanffe9ee42007-05-24 22:24:20 +0100109/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 * time_init() - it does the following things.
111 *
Ralf Baechle4b550482007-10-11 23:46:08 +0100112 * 1) plat_time_init() -
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 * a) (optional) set up RTC routines,
114 * b) (optional) calibrate and set the mips_hpt_frequency
Atsushi Nemoto16b7b2a2006-10-24 00:21:27 +0900115 * (only needed if you intended to use cpu counter as timer interrupt
116 * source)
Ralf Baechle4b550482007-10-11 23:46:08 +0100117 * 2) calculate a couple of cached variables for later usage
118 * 3) plat_timer_setup() -
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 * a) (optional) over-write any choices made above by time_init().
120 * b) machine specific code should setup the timer irqaction.
121 * c) enable the timer interrupt
122 */
123
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124unsigned int mips_hpt_frequency;
125
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126static unsigned int __init calibrate_hpt(void)
127{
Atsushi Nemoto00598562006-11-12 00:10:28 +0900128 cycle_t frequency, hpt_start, hpt_end, hpt_count, hz;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
130 const int loops = HZ / 10;
131 int log_2_loops = 0;
132 int i;
133
134 /*
135 * We want to calibrate for 0.1s, but to avoid a 64-bit
136 * division we round the number of loops up to the nearest
137 * power of 2.
138 */
139 while (loops > 1 << log_2_loops)
140 log_2_loops++;
141 i = 1 << log_2_loops;
142
143 /*
144 * Wait for a rising edge of the timer interrupt.
145 */
146 while (mips_timer_state());
147 while (!mips_timer_state());
148
149 /*
150 * Now see how many high precision timer ticks happen
151 * during the calculated number of periods between timer
152 * interrupts.
153 */
Atsushi Nemoto00598562006-11-12 00:10:28 +0900154 hpt_start = clocksource_mips.read();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 do {
156 while (mips_timer_state());
157 while (!mips_timer_state());
158 } while (--i);
Atsushi Nemoto00598562006-11-12 00:10:28 +0900159 hpt_end = clocksource_mips.read();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
Atsushi Nemoto00598562006-11-12 00:10:28 +0900161 hpt_count = (hpt_end - hpt_start) & clocksource_mips.mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 hz = HZ;
Atsushi Nemoto00598562006-11-12 00:10:28 +0900163 frequency = hpt_count * hz;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
165 return frequency >> log_2_loops;
166}
167
Atsushi Nemoto00598562006-11-12 00:10:28 +0900168struct clocksource clocksource_mips = {
Atsushi Nemoto16b7b2a2006-10-24 00:21:27 +0900169 .name = "MIPS",
Franck Bui-Huu55d0b4e2007-05-04 17:36:44 +0200170 .mask = CLOCKSOURCE_MASK(32),
Thomas Gleixner877fe382007-02-16 01:27:40 -0800171 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
Atsushi Nemoto16b7b2a2006-10-24 00:21:27 +0900172};
173
Ralf Baechle93c846f2007-10-19 08:13:08 +0100174void __init clocksource_set_clock(struct clocksource *cs, unsigned int clock)
Atsushi Nemoto16b7b2a2006-10-24 00:21:27 +0900175{
176 u64 temp;
177 u32 shift;
178
Ralf Baechle93c846f2007-10-19 08:13:08 +0100179 /* Find a shift value */
180 for (shift = 32; shift > 0; shift--) {
181 temp = (u64) NSEC_PER_SEC << shift;
182 do_div(temp, clock);
183 if ((temp >> 32) == 0)
184 break;
185 }
186 cs->shift = shift;
187 cs->mult = (u32) temp;
188}
189
190void __cpuinit clockevent_set_clock(struct clock_event_device *cd,
191 unsigned int clock)
192{
193 u64 temp;
194 u32 shift;
195
196 /* Find a shift value */
197 for (shift = 32; shift > 0; shift--) {
198 temp = (u64) NSEC_PER_SEC << shift;
199 do_div(temp, clock);
200 if ((temp >> 32) == 0)
201 break;
202 }
203 cd->shift = shift;
204 cd->mult = (u32) temp;
205}
206
207static void __init init_mips_clocksource(void)
208{
Atsushi Nemoto00598562006-11-12 00:10:28 +0900209 if (!mips_hpt_frequency || clocksource_mips.read == null_hpt_read)
Atsushi Nemoto16b7b2a2006-10-24 00:21:27 +0900210 return;
211
212 /* Calclate a somewhat reasonable rating value */
213 clocksource_mips.rating = 200 + mips_hpt_frequency / 10000000;
Ralf Baechle93c846f2007-10-19 08:13:08 +0100214
215 clocksource_set_clock(&clocksource_mips, mips_hpt_frequency);
Atsushi Nemoto16b7b2a2006-10-24 00:21:27 +0900216
217 clocksource_register(&clocksource_mips);
218}
219
Ralf Baechle4b550482007-10-11 23:46:08 +0100220void __init __weak plat_time_init(void)
221{
222}
223
Ralf Baechle7bcf7712007-10-11 23:46:09 +0100224void __init __weak plat_timer_setup(struct irqaction *irq)
225{
226}
227
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228void __init time_init(void)
229{
Ralf Baechle4b550482007-10-11 23:46:08 +0100230 plat_time_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
232 /* Choose appropriate high precision timer routines. */
Atsushi Nemoto00598562006-11-12 00:10:28 +0900233 if (!cpu_has_counter && !clocksource_mips.read)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 /* No high precision timer -- sorry. */
Atsushi Nemoto00598562006-11-12 00:10:28 +0900235 clocksource_mips.read = null_hpt_read;
Atsushi Nemoto16b7b2a2006-10-24 00:21:27 +0900236 else if (!mips_hpt_frequency && !mips_timer_state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 /* A high precision timer of unknown frequency. */
Atsushi Nemoto00598562006-11-12 00:10:28 +0900238 if (!clocksource_mips.read)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 /* No external high precision timer -- use R4k. */
Atsushi Nemoto00598562006-11-12 00:10:28 +0900240 clocksource_mips.read = c0_hpt_read;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 } else {
242 /* We know counter frequency. Or we can get it. */
Atsushi Nemoto00598562006-11-12 00:10:28 +0900243 if (!clocksource_mips.read) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 /* No external high precision timer -- use R4k. */
Atsushi Nemoto00598562006-11-12 00:10:28 +0900245 clocksource_mips.read = c0_hpt_read;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 }
247 if (!mips_hpt_frequency)
248 mips_hpt_frequency = calibrate_hpt();
249
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 /* Report the high precision timer rate for a reference. */
251 printk("Using %u.%03u MHz high precision timer.\n",
252 ((mips_hpt_frequency + 500) / 1000) / 1000,
253 ((mips_hpt_frequency + 500) / 1000) % 1000);
254 }
255
Atsushi Nemoto16b7b2a2006-10-24 00:21:27 +0900256 init_mips_clocksource();
Ralf Baechle7bcf7712007-10-11 23:46:09 +0100257 mips_clockevent_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258}