xref: /linux/arch/arm/lib/delay.c (revision fd639726bf15fca8ee1a00dce8e0096d0ad9bd18)
1 /*
2  * Delay loops based on the OpenRISC implementation.
3  *
4  * Copyright (C) 2012 ARM Limited
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  * Author: Will Deacon <will.deacon@arm.com>
20  */
21 
22 #include <linux/clocksource.h>
23 #include <linux/delay.h>
24 #include <linux/init.h>
25 #include <linux/kernel.h>
26 #include <linux/module.h>
27 #include <linux/timex.h>
28 
29 /*
30  * Default to the loop-based delay implementation.
31  */
32 struct arm_delay_ops arm_delay_ops __ro_after_init = {
33 	.delay		= __loop_delay,
34 	.const_udelay	= __loop_const_udelay,
35 	.udelay		= __loop_udelay,
36 };
37 
38 static const struct delay_timer *delay_timer;
39 static bool delay_calibrated;
40 static u64 delay_res;
41 
42 int read_current_timer(unsigned long *timer_val)
43 {
44 	if (!delay_timer)
45 		return -ENXIO;
46 
47 	*timer_val = delay_timer->read_current_timer();
48 	return 0;
49 }
50 EXPORT_SYMBOL_GPL(read_current_timer);
51 
52 static inline u64 cyc_to_ns(u64 cyc, u32 mult, u32 shift)
53 {
54 	return (cyc * mult) >> shift;
55 }
56 
57 static void __timer_delay(unsigned long cycles)
58 {
59 	cycles_t start = get_cycles();
60 
61 	while ((get_cycles() - start) < cycles)
62 		cpu_relax();
63 }
64 
65 static void __timer_const_udelay(unsigned long xloops)
66 {
67 	unsigned long long loops = xloops;
68 	loops *= arm_delay_ops.ticks_per_jiffy;
69 	__timer_delay(loops >> UDELAY_SHIFT);
70 }
71 
72 static void __timer_udelay(unsigned long usecs)
73 {
74 	__timer_const_udelay(usecs * UDELAY_MULT);
75 }
76 
77 void __init register_current_timer_delay(const struct delay_timer *timer)
78 {
79 	u32 new_mult, new_shift;
80 	u64 res;
81 
82 	clocks_calc_mult_shift(&new_mult, &new_shift, timer->freq,
83 			       NSEC_PER_SEC, 3600);
84 	res = cyc_to_ns(1ULL, new_mult, new_shift);
85 
86 	if (res > 1000) {
87 		pr_err("Ignoring delay timer %ps, which has insufficient resolution of %lluns\n",
88 			timer, res);
89 		return;
90 	}
91 
92 	if (!delay_calibrated && (!delay_res || (res < delay_res))) {
93 		pr_info("Switching to timer-based delay loop, resolution %lluns\n", res);
94 		delay_timer			= timer;
95 		lpj_fine			= timer->freq / HZ;
96 		delay_res			= res;
97 
98 		/* cpufreq may scale loops_per_jiffy, so keep a private copy */
99 		arm_delay_ops.ticks_per_jiffy	= lpj_fine;
100 		arm_delay_ops.delay		= __timer_delay;
101 		arm_delay_ops.const_udelay	= __timer_const_udelay;
102 		arm_delay_ops.udelay		= __timer_udelay;
103 	} else {
104 		pr_info("Ignoring duplicate/late registration of read_current_timer delay\n");
105 	}
106 }
107 
108 unsigned long calibrate_delay_is_known(void)
109 {
110 	delay_calibrated = true;
111 	return lpj_fine;
112 }
113 
114 void calibration_delay_done(void)
115 {
116 	delay_calibrated = true;
117 }
118