xref: /openbmc/linux/arch/arm64/lib/delay.c (revision 4a3fad70)
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, see <http://www.gnu.org/licenses/>.
17  *
18  * Author: Will Deacon <will.deacon@arm.com>
19  */
20 
21 #include <linux/delay.h>
22 #include <linux/init.h>
23 #include <linux/kernel.h>
24 #include <linux/module.h>
25 #include <linux/timex.h>
26 
27 #include <clocksource/arm_arch_timer.h>
28 
29 #define USECS_TO_CYCLES(time_usecs)			\
30 	xloops_to_cycles((time_usecs) * 0x10C7UL)
31 
32 static inline unsigned long xloops_to_cycles(unsigned long xloops)
33 {
34 	return (xloops * loops_per_jiffy * HZ) >> 32;
35 }
36 
37 void __delay(unsigned long cycles)
38 {
39 	cycles_t start = get_cycles();
40 
41 	if (arch_timer_evtstrm_available()) {
42 		const cycles_t timer_evt_period =
43 			USECS_TO_CYCLES(ARCH_TIMER_EVT_STREAM_PERIOD_US);
44 
45 		while ((get_cycles() - start + timer_evt_period) < cycles)
46 			wfe();
47 	}
48 
49 	while ((get_cycles() - start) < cycles)
50 		cpu_relax();
51 }
52 EXPORT_SYMBOL(__delay);
53 
54 inline void __const_udelay(unsigned long xloops)
55 {
56 	__delay(xloops_to_cycles(xloops));
57 }
58 EXPORT_SYMBOL(__const_udelay);
59 
60 void __udelay(unsigned long usecs)
61 {
62 	__const_udelay(usecs * 0x10C7UL); /* 2**32 / 1000000 (rounded up) */
63 }
64 EXPORT_SYMBOL(__udelay);
65 
66 void __ndelay(unsigned long nsecs)
67 {
68 	__const_udelay(nsecs * 0x5UL); /* 2**32 / 1000000000 (rounded up) */
69 }
70 EXPORT_SYMBOL(__ndelay);
71