xref: /openbmc/linux/arch/arm/kernel/arch_timer.c (revision 023796b9)
1 /*
2  *  linux/arch/arm/kernel/arch_timer.c
3  *
4  *  Copyright (C) 2011 ARM Ltd.
5  *  All Rights Reserved
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 #include <linux/init.h>
12 #include <linux/types.h>
13 #include <linux/errno.h>
14 
15 #include <asm/delay.h>
16 #include <asm/sched_clock.h>
17 
18 #include <clocksource/arm_arch_timer.h>
19 
20 static unsigned long arch_timer_read_counter_long(void)
21 {
22 	return arch_timer_read_counter();
23 }
24 
25 static u32 sched_clock_mult __read_mostly;
26 
27 static unsigned long long notrace arch_timer_sched_clock(void)
28 {
29 	return arch_timer_read_counter() * sched_clock_mult;
30 }
31 
32 static struct delay_timer arch_delay_timer;
33 
34 static void __init arch_timer_delay_timer_register(void)
35 {
36 	/* Use the architected timer for the delay loop. */
37 	arch_delay_timer.read_current_timer = arch_timer_read_counter_long;
38 	arch_delay_timer.freq = arch_timer_get_rate();
39 	register_current_timer_delay(&arch_delay_timer);
40 }
41 
42 int __init arch_timer_of_register(void)
43 {
44 	int ret;
45 
46 	ret = arch_timer_init();
47 	if (ret)
48 		return ret;
49 
50 	arch_timer_delay_timer_register();
51 
52 	return 0;
53 }
54 
55 int __init arch_timer_sched_clock_init(void)
56 {
57         u32 arch_timer_rate = arch_timer_get_rate();
58 
59 	if (arch_timer_rate == 0)
60 		return -ENXIO;
61 
62 	/* Cache the sched_clock multiplier to save a divide in the hot path. */
63 	sched_clock_mult = NSEC_PER_SEC / arch_timer_rate;
64 	sched_clock_func = arch_timer_sched_clock;
65 	pr_info("sched_clock: ARM arch timer >56 bits at %ukHz, resolution %uns\n",
66 		arch_timer_rate / 1000, sched_clock_mult);
67 
68 	return 0;
69 }
70