blob: 078b259ead4ef617fd0c36d43174beedd5eae033 [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 */
Sebastian Hesselbarth4178bac2013-09-04 12:24:03 +020014#include <linux/clk-provider.h>
Arnd Bergmannf414f132013-03-25 11:14:57 -050015#include <linux/clocksource.h>
Sebastian Hesselbarth4178bac2013-09-04 12:24:03 +020016#include <linux/errno.h>
17#include <linux/export.h>
18#include <linux/init.h>
19#include <linux/interrupt.h>
Frederik Deweerdte317c8c2006-10-06 18:58:24 +000020#include <linux/irq.h>
Sebastian Hesselbarth4178bac2013-09-04 12:24:03 +020021#include <linux/kernel.h>
22#include <linux/profile.h>
23#include <linux/sched.h>
Stephen Boyd38ff87f2013-06-01 23:39:40 -070024#include <linux/sched_clock.h>
Sebastian Hesselbarth4178bac2013-09-04 12:24:03 +020025#include <linux/smp.h>
26#include <linux/time.h>
27#include <linux/timex.h>
28#include <linux/timer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
Russell King8ff14432010-12-20 10:18:36 +000030#include <asm/mach/arch.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <asm/mach/time.h>
Sebastian Hesselbarth4178bac2013-09-04 12:24:03 +020032#include <asm/stacktrace.h>
33#include <asm/thread_info.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
Arnd Bergmann63216d32011-06-10 13:58:30 +000035#if defined(CONFIG_RTC_DRV_CMOS) || defined(CONFIG_RTC_DRV_CMOS_MODULE) || \
36 defined(CONFIG_NVRAM) || defined(CONFIG_NVRAM_MODULE)
Linus Torvalds1da177e2005-04-16 15:20:36 -070037/* this needs a better home */
38DEFINE_SPINLOCK(rtc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070039EXPORT_SYMBOL(rtc_lock);
David Brownell9dd34942007-01-17 22:11:27 +010040#endif /* pc-style 'CMOS' RTC support */
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42/* change this if you have some constant time drift */
43#define USECS_PER_JIFFY (1000000/HZ)
44
45#ifdef CONFIG_SMP
46unsigned long profile_pc(struct pt_regs *regs)
47{
Catalin Marinas2d7c11b2009-02-11 13:07:53 +010048 struct stackframe frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Catalin Marinas2d7c11b2009-02-11 13:07:53 +010050 if (!in_lock_functions(regs->ARM_pc))
51 return regs->ARM_pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Nikolay Borisova3250c92014-06-03 19:48:58 +010053 arm_get_current_stackframe(regs, &frame);
Catalin Marinas2d7c11b2009-02-11 13:07:53 +010054 do {
55 int ret = unwind_frame(&frame);
56 if (ret < 0)
57 return 0;
58 } while (in_lock_functions(frame.pc));
59
60 return frame.pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061}
62EXPORT_SYMBOL(profile_pc);
63#endif
64
Kevin Hilman9e4559d2007-03-14 17:33:24 +010065#ifndef CONFIG_GENERIC_CLOCKEVENTS
Linus Torvalds1da177e2005-04-16 15:20:36 -070066/*
67 * Kernel system timer support.
68 */
Linus Torvalds0cd61b62006-10-06 10:53:39 -070069void timer_tick(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070070{
Frederik Deweerdte317c8c2006-10-06 18:58:24 +000071 profile_tick(CPU_PROFILING);
Torben Hohn6906e332011-01-27 15:59:21 +010072 xtime_update(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073#ifndef CONFIG_SMP
Russell Kingc97d4862006-10-25 13:59:16 +010074 update_process_times(user_mode(get_irq_regs()));
Linus Torvalds1da177e2005-04-16 15:20:36 -070075#endif
76}
Kevin Hilman9e4559d2007-03-14 17:33:24 +010077#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
Xunlei Pangcb850712015-04-01 20:34:26 -070079static void dummy_clock_access(struct timespec64 *ts)
Marc Zyngierbd0493e2012-05-05 19:28:44 +010080{
81 ts->tv_sec = 0;
82 ts->tv_nsec = 0;
83}
84
85static clock_access_fn __read_persistent_clock = dummy_clock_access;
Marc Zyngierbd0493e2012-05-05 19:28:44 +010086
Xunlei Pangcb850712015-04-01 20:34:26 -070087void read_persistent_clock64(struct timespec64 *ts)
Marc Zyngierbd0493e2012-05-05 19:28:44 +010088{
89 __read_persistent_clock(ts);
90}
91
Pavel Tatashin227e3952018-07-19 16:55:37 -040092int __init register_persistent_clock(clock_access_fn read_persistent)
Marc Zyngierbd0493e2012-05-05 19:28:44 +010093{
94 /* Only allow the clockaccess functions to be registered once */
Pavel Tatashin227e3952018-07-19 16:55:37 -040095 if (__read_persistent_clock == dummy_clock_access) {
Marc Zyngierbd0493e2012-05-05 19:28:44 +010096 if (read_persistent)
97 __read_persistent_clock = read_persistent;
Marc Zyngierbd0493e2012-05-05 19:28:44 +010098 return 0;
99 }
100
101 return -EINVAL;
102}
103
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104void __init time_init(void)
105{
Sebastian Hesselbarth4178bac2013-09-04 12:24:03 +0200106 if (machine_desc->init_time) {
Arnd Bergmannf414f132013-03-25 11:14:57 -0500107 machine_desc->init_time();
Sebastian Hesselbarth4178bac2013-09-04 12:24:03 +0200108 } else {
109#ifdef CONFIG_COMMON_CLK
110 of_clk_init(NULL);
111#endif
Daniel Lezcanoba5d08c2017-05-26 17:40:46 +0200112 timer_probe();
Sebastian Hesselbarth4178bac2013-09-04 12:24:03 +0200113 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114}