blob: 28753805d2d16f7cffa9fd25b4164011432b014b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/arch/arm/kernel/time.c
3 *
4 * Copyright (C) 1991, 1992, 1995 Linus Torvalds
5 * Modifications for ARM (C) 1994-2001 Russell King
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 *
11 * This file contains the ARM-specific time handling details:
12 * reading the RTC at bootup, etc...
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/module.h>
15#include <linux/kernel.h>
16#include <linux/interrupt.h>
17#include <linux/time.h>
18#include <linux/init.h>
Alexey Dobriyand43c36d2009-10-07 17:09:06 +040019#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/smp.h>
21#include <linux/timex.h>
22#include <linux/errno.h>
23#include <linux/profile.h>
24#include <linux/sysdev.h>
25#include <linux/timer.h>
Frederik Deweerdte317c8c2006-10-06 18:58:24 +000026#include <linux/irq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
Ben Dooks9ca3f072006-12-24 01:36:35 +010028#include <linux/mc146818rtc.h>
29
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <asm/leds.h>
31#include <asm/thread_info.h>
Catalin Marinas2d7c11b2009-02-11 13:07:53 +010032#include <asm/stacktrace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <asm/mach/time.h>
34
Linus Torvalds1da177e2005-04-16 15:20:36 -070035/*
36 * Our system timer.
37 */
38struct sys_timer *system_timer;
39
David Brownell9dd34942007-01-17 22:11:27 +010040#if defined(CONFIG_RTC_DRV_CMOS) || defined(CONFIG_RTC_DRV_CMOS_MODULE)
Linus Torvalds1da177e2005-04-16 15:20:36 -070041/* this needs a better home */
42DEFINE_SPINLOCK(rtc_lock);
43
David Brownell9dd34942007-01-17 22:11:27 +010044#ifdef CONFIG_RTC_DRV_CMOS_MODULE
Linus Torvalds1da177e2005-04-16 15:20:36 -070045EXPORT_SYMBOL(rtc_lock);
46#endif
David Brownell9dd34942007-01-17 22:11:27 +010047#endif /* pc-style 'CMOS' RTC support */
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
49/* change this if you have some constant time drift */
50#define USECS_PER_JIFFY (1000000/HZ)
51
52#ifdef CONFIG_SMP
53unsigned long profile_pc(struct pt_regs *regs)
54{
Catalin Marinas2d7c11b2009-02-11 13:07:53 +010055 struct stackframe frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Catalin Marinas2d7c11b2009-02-11 13:07:53 +010057 if (!in_lock_functions(regs->ARM_pc))
58 return regs->ARM_pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Catalin Marinas2d7c11b2009-02-11 13:07:53 +010060 frame.fp = regs->ARM_fp;
61 frame.sp = regs->ARM_sp;
62 frame.lr = regs->ARM_lr;
63 frame.pc = regs->ARM_pc;
64 do {
65 int ret = unwind_frame(&frame);
66 if (ret < 0)
67 return 0;
68 } while (in_lock_functions(frame.pc));
69
70 return frame.pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071}
72EXPORT_SYMBOL(profile_pc);
73#endif
74
Kevin Hilman746140c2006-09-22 00:16:30 +010075#ifndef CONFIG_GENERIC_TIME
Linus Torvalds1da177e2005-04-16 15:20:36 -070076static unsigned long dummy_gettimeoffset(void)
77{
78 return 0;
79}
Kevin Hilman746140c2006-09-22 00:16:30 +010080#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
Linus Torvalds1da177e2005-04-16 15:20:36 -070082#ifdef CONFIG_LEDS_TIMER
83static inline void do_leds(void)
84{
David Brownell6d15cb42006-11-06 19:29:16 +010085 static unsigned int count = HZ/2;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
87 if (--count == 0) {
David Brownell6d15cb42006-11-06 19:29:16 +010088 count = HZ/2;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 leds_event(led_timer);
90 }
91}
92#else
93#define do_leds()
94#endif
95
Kevin Hilman746140c2006-09-22 00:16:30 +010096#ifndef CONFIG_GENERIC_TIME
Linus Torvalds1da177e2005-04-16 15:20:36 -070097void do_gettimeofday(struct timeval *tv)
98{
99 unsigned long flags;
100 unsigned long seq;
Atsushi Nemoto8ef38602006-09-30 23:28:31 -0700101 unsigned long usec, sec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
103 do {
104 seq = read_seqbegin_irqsave(&xtime_lock, flags);
105 usec = system_timer->offset();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 sec = xtime.tv_sec;
107 usec += xtime.tv_nsec / 1000;
108 } while (read_seqretry_irqrestore(&xtime_lock, seq, flags));
109
110 /* usec may have gone up a lot: be safe */
111 while (usec >= 1000000) {
112 usec -= 1000000;
113 sec++;
114 }
115
116 tv->tv_sec = sec;
117 tv->tv_usec = usec;
118}
119
120EXPORT_SYMBOL(do_gettimeofday);
121
122int do_settimeofday(struct timespec *tv)
123{
124 time_t wtm_sec, sec = tv->tv_sec;
125 long wtm_nsec, nsec = tv->tv_nsec;
126
127 if ((unsigned long)tv->tv_nsec >= NSEC_PER_SEC)
128 return -EINVAL;
129
130 write_seqlock_irq(&xtime_lock);
131 /*
132 * This is revolting. We need to set "xtime" correctly. However, the
133 * value in this location is the value at the most recent update of
134 * wall time. Discover what correction gettimeofday() would have
135 * done, and then undo it!
136 */
137 nsec -= system_timer->offset() * NSEC_PER_USEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
139 wtm_sec = wall_to_monotonic.tv_sec + (xtime.tv_sec - sec);
140 wtm_nsec = wall_to_monotonic.tv_nsec + (xtime.tv_nsec - nsec);
141
142 set_normalized_timespec(&xtime, sec, nsec);
143 set_normalized_timespec(&wall_to_monotonic, wtm_sec, wtm_nsec);
144
john stultzb149ee22005-09-06 15:17:46 -0700145 ntp_clear();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 write_sequnlock_irq(&xtime_lock);
147 clock_was_set();
148 return 0;
149}
150
151EXPORT_SYMBOL(do_settimeofday);
Kevin Hilman746140c2006-09-22 00:16:30 +0100152#endif /* !CONFIG_GENERIC_TIME */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
Kevin Hilman9e4559d2007-03-14 17:33:24 +0100154#ifndef CONFIG_GENERIC_CLOCKEVENTS
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155/*
156 * Kernel system timer support.
157 */
Linus Torvalds0cd61b62006-10-06 10:53:39 -0700158void timer_tick(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159{
Frederik Deweerdte317c8c2006-10-06 18:58:24 +0000160 profile_tick(CPU_PROFILING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 do_leds();
Russell King193c3cc2008-01-28 10:16:37 +0000162 write_seqlock(&xtime_lock);
Atsushi Nemoto3171a032006-09-29 02:00:32 -0700163 do_timer(1);
Russell King193c3cc2008-01-28 10:16:37 +0000164 write_sequnlock(&xtime_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165#ifndef CONFIG_SMP
Russell Kingc97d4862006-10-25 13:59:16 +0100166 update_process_times(user_mode(get_irq_regs()));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167#endif
168}
Kevin Hilman9e4559d2007-03-14 17:33:24 +0100169#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
Kevin Hilman9e4559d2007-03-14 17:33:24 +0100171#if defined(CONFIG_PM) && !defined(CONFIG_GENERIC_CLOCKEVENTS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172static int timer_suspend(struct sys_device *dev, pm_message_t state)
173{
174 struct sys_timer *timer = container_of(dev, struct sys_timer, dev);
175
176 if (timer->suspend != NULL)
177 timer->suspend();
178
179 return 0;
180}
181
182static int timer_resume(struct sys_device *dev)
183{
184 struct sys_timer *timer = container_of(dev, struct sys_timer, dev);
185
186 if (timer->resume != NULL)
187 timer->resume();
188
189 return 0;
190}
191#else
192#define timer_suspend NULL
193#define timer_resume NULL
194#endif
195
196static struct sysdev_class timer_sysclass = {
Kay Sieversaf5ca3f2007-12-20 02:09:39 +0100197 .name = "timer",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 .suspend = timer_suspend,
199 .resume = timer_resume,
200};
201
202static int __init timer_init_sysfs(void)
203{
204 int ret = sysdev_class_register(&timer_sysclass);
205 if (ret == 0) {
206 system_timer->dev.cls = &timer_sysclass;
207 ret = sysdev_register(&system_timer->dev);
208 }
Russell King8749af62005-06-25 19:39:45 +0100209
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 return ret;
211}
212
213device_initcall(timer_init_sysfs);
214
215void __init time_init(void)
216{
Kevin Hilman746140c2006-09-22 00:16:30 +0100217#ifndef CONFIG_GENERIC_TIME
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 if (system_timer->offset == NULL)
219 system_timer->offset = dummy_gettimeoffset;
Kevin Hilman746140c2006-09-22 00:16:30 +0100220#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 system_timer->init();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222}
223