blob: 09be0c3c906965822d206b4268a97fbe275bc5f8 [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 */
Paul Gortmakerecea4ab2011-07-22 10:58:34 -040014#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#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>
Rafael J. Wysocki328f5cc2011-04-22 22:02:33 +020024#include <linux/syscore_ops.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/timer.h>
Frederik Deweerdte317c8c2006-10-06 18:58:24 +000026#include <linux/irq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <asm/thread_info.h>
Russell King211baa702011-01-11 16:23:04 +000029#include <asm/sched_clock.h>
Catalin Marinas2d7c11b2009-02-11 13:07:53 +010030#include <asm/stacktrace.h>
Russell King8ff14432010-12-20 10:18:36 +000031#include <asm/mach/arch.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <asm/mach/time.h>
33
Linus Torvalds1da177e2005-04-16 15:20:36 -070034/*
35 * Our system timer.
36 */
Russell King8ff14432010-12-20 10:18:36 +000037static struct sys_timer *system_timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
Arnd Bergmann63216d32011-06-10 13:58:30 +000039#if defined(CONFIG_RTC_DRV_CMOS) || defined(CONFIG_RTC_DRV_CMOS_MODULE) || \
40 defined(CONFIG_NVRAM) || defined(CONFIG_NVRAM_MODULE)
Linus Torvalds1da177e2005-04-16 15:20:36 -070041/* this needs a better home */
42DEFINE_SPINLOCK(rtc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070043EXPORT_SYMBOL(rtc_lock);
David Brownell9dd34942007-01-17 22:11:27 +010044#endif /* pc-style 'CMOS' RTC support */
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
46/* change this if you have some constant time drift */
47#define USECS_PER_JIFFY (1000000/HZ)
48
49#ifdef CONFIG_SMP
50unsigned long profile_pc(struct pt_regs *regs)
51{
Catalin Marinas2d7c11b2009-02-11 13:07:53 +010052 struct stackframe frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Catalin Marinas2d7c11b2009-02-11 13:07:53 +010054 if (!in_lock_functions(regs->ARM_pc))
55 return regs->ARM_pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Catalin Marinas2d7c11b2009-02-11 13:07:53 +010057 frame.fp = regs->ARM_fp;
58 frame.sp = regs->ARM_sp;
59 frame.lr = regs->ARM_lr;
60 frame.pc = regs->ARM_pc;
61 do {
62 int ret = unwind_frame(&frame);
63 if (ret < 0)
64 return 0;
65 } while (in_lock_functions(frame.pc));
66
67 return frame.pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068}
69EXPORT_SYMBOL(profile_pc);
70#endif
71
John Stultz5cfc8ee2010-03-24 00:22:36 +000072#ifdef CONFIG_ARCH_USES_GETTIMEOFFSET
73u32 arch_gettimeoffset(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070074{
John Stultz5cfc8ee2010-03-24 00:22:36 +000075 if (system_timer->offset != NULL)
76 return system_timer->offset() * 1000;
77
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 return 0;
79}
John Stultz5cfc8ee2010-03-24 00:22:36 +000080#endif /* CONFIG_ARCH_USES_GETTIMEOFFSET */
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
Kevin Hilman9e4559d2007-03-14 17:33:24 +010082#ifndef CONFIG_GENERIC_CLOCKEVENTS
Linus Torvalds1da177e2005-04-16 15:20:36 -070083/*
84 * Kernel system timer support.
85 */
Linus Torvalds0cd61b62006-10-06 10:53:39 -070086void timer_tick(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070087{
Frederik Deweerdte317c8c2006-10-06 18:58:24 +000088 profile_tick(CPU_PROFILING);
Torben Hohn6906e332011-01-27 15:59:21 +010089 xtime_update(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090#ifndef CONFIG_SMP
Russell Kingc97d4862006-10-25 13:59:16 +010091 update_process_times(user_mode(get_irq_regs()));
Linus Torvalds1da177e2005-04-16 15:20:36 -070092#endif
93}
Kevin Hilman9e4559d2007-03-14 17:33:24 +010094#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
Marc Zyngierbd0493e2012-05-05 19:28:44 +010096static void dummy_clock_access(struct timespec *ts)
97{
98 ts->tv_sec = 0;
99 ts->tv_nsec = 0;
100}
101
102static clock_access_fn __read_persistent_clock = dummy_clock_access;
103static clock_access_fn __read_boot_clock = dummy_clock_access;;
104
105void read_persistent_clock(struct timespec *ts)
106{
107 __read_persistent_clock(ts);
108}
109
110void read_boot_clock(struct timespec *ts)
111{
112 __read_boot_clock(ts);
113}
114
115int __init register_persistent_clock(clock_access_fn read_boot,
116 clock_access_fn read_persistent)
117{
118 /* Only allow the clockaccess functions to be registered once */
119 if (__read_persistent_clock == dummy_clock_access &&
120 __read_boot_clock == dummy_clock_access) {
121 if (read_boot)
122 __read_boot_clock = read_boot;
123 if (read_persistent)
124 __read_persistent_clock = read_persistent;
125
126 return 0;
127 }
128
129 return -EINVAL;
130}
131
Kevin Hilman9e4559d2007-03-14 17:33:24 +0100132#if defined(CONFIG_PM) && !defined(CONFIG_GENERIC_CLOCKEVENTS)
Rafael J. Wysocki328f5cc2011-04-22 22:02:33 +0200133static int timer_suspend(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134{
Rafael J. Wysocki328f5cc2011-04-22 22:02:33 +0200135 if (system_timer->suspend)
136 system_timer->suspend();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
138 return 0;
139}
140
Rafael J. Wysocki328f5cc2011-04-22 22:02:33 +0200141static void timer_resume(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142{
Rafael J. Wysocki328f5cc2011-04-22 22:02:33 +0200143 if (system_timer->resume)
144 system_timer->resume();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145}
146#else
147#define timer_suspend NULL
148#define timer_resume NULL
149#endif
150
Rafael J. Wysocki328f5cc2011-04-22 22:02:33 +0200151static struct syscore_ops timer_syscore_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 .suspend = timer_suspend,
153 .resume = timer_resume,
154};
155
Rafael J. Wysocki328f5cc2011-04-22 22:02:33 +0200156static int __init timer_init_syscore_ops(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157{
Rafael J. Wysocki328f5cc2011-04-22 22:02:33 +0200158 register_syscore_ops(&timer_syscore_ops);
Russell King8749af62005-06-25 19:39:45 +0100159
Rafael J. Wysocki328f5cc2011-04-22 22:02:33 +0200160 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161}
162
Rafael J. Wysocki328f5cc2011-04-22 22:02:33 +0200163device_initcall(timer_init_syscore_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
165void __init time_init(void)
166{
Russell King8ff14432010-12-20 10:18:36 +0000167 system_timer = machine_desc->timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 system_timer->init();
Russell King211baa702011-01-11 16:23:04 +0000169 sched_clock_postinit();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170}
171