xref: /linux/arch/nios2/lib/delay.c (revision caab277b)
1*caab277bSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
24182de9eSLey Foon Tan /* Copyright Altera Corporation (C) 2014. All rights reserved.
34182de9eSLey Foon Tan  */
44182de9eSLey Foon Tan 
54182de9eSLey Foon Tan #include <linux/module.h>
64182de9eSLey Foon Tan #include <asm/delay.h>
74182de9eSLey Foon Tan #include <asm/param.h>
84182de9eSLey Foon Tan #include <asm/processor.h>
94182de9eSLey Foon Tan #include <asm/timex.h>
104182de9eSLey Foon Tan 
__delay(unsigned long cycles)114182de9eSLey Foon Tan void __delay(unsigned long cycles)
124182de9eSLey Foon Tan {
134182de9eSLey Foon Tan 	cycles_t start = get_cycles();
144182de9eSLey Foon Tan 
154182de9eSLey Foon Tan 	while ((get_cycles() - start) < cycles)
164182de9eSLey Foon Tan 		cpu_relax();
174182de9eSLey Foon Tan }
184182de9eSLey Foon Tan EXPORT_SYMBOL(__delay);
194182de9eSLey Foon Tan 
__const_udelay(unsigned long xloops)204182de9eSLey Foon Tan void __const_udelay(unsigned long xloops)
214182de9eSLey Foon Tan {
224182de9eSLey Foon Tan 	u64 loops;
234182de9eSLey Foon Tan 
244182de9eSLey Foon Tan 	loops = (u64)xloops * loops_per_jiffy * HZ;
254182de9eSLey Foon Tan 
264182de9eSLey Foon Tan 	__delay(loops >> 32);
274182de9eSLey Foon Tan }
284182de9eSLey Foon Tan EXPORT_SYMBOL(__const_udelay);
294182de9eSLey Foon Tan 
__udelay(unsigned long usecs)304182de9eSLey Foon Tan void __udelay(unsigned long usecs)
314182de9eSLey Foon Tan {
324182de9eSLey Foon Tan 	__const_udelay(usecs * 0x10C7UL); /* 2**32 / 1000000 (rounded up) */
334182de9eSLey Foon Tan }
344182de9eSLey Foon Tan EXPORT_SYMBOL(__udelay);
354182de9eSLey Foon Tan 
__ndelay(unsigned long nsecs)364182de9eSLey Foon Tan void __ndelay(unsigned long nsecs)
374182de9eSLey Foon Tan {
384182de9eSLey Foon Tan 	__const_udelay(nsecs * 0x5UL); /* 2**32 / 1000000000 (rounded up) */
394182de9eSLey Foon Tan }
404182de9eSLey Foon Tan EXPORT_SYMBOL(__ndelay);
41