xref: /linux/arch/loongarch/lib/delay.c (revision dec1c62e91ba268ab2a6e339d4d7a59287d5eba1)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (C) 2020-2022 Loongson Technology Corporation Limited
4  */
5 #include <linux/delay.h>
6 #include <linux/export.h>
7 #include <linux/smp.h>
8 #include <linux/timex.h>
9 
10 #include <asm/compiler.h>
11 #include <asm/processor.h>
12 
13 void __delay(unsigned long cycles)
14 {
15 	u64 t0 = get_cycles();
16 
17 	while ((unsigned long)(get_cycles() - t0) < cycles)
18 		cpu_relax();
19 }
20 EXPORT_SYMBOL(__delay);
21 
22 /*
23  * Division by multiplication: you don't have to worry about
24  * loss of precision.
25  *
26  * Use only for very small delays ( < 1 msec).	Should probably use a
27  * lookup table, really, as the multiplications take much too long with
28  * short delays.  This is a "reasonable" implementation, though (and the
29  * first constant multiplications gets optimized away if the delay is
30  * a constant)
31  */
32 
33 void __udelay(unsigned long us)
34 {
35 	__delay((us * 0x000010c7ull * HZ * lpj_fine) >> 32);
36 }
37 EXPORT_SYMBOL(__udelay);
38 
39 void __ndelay(unsigned long ns)
40 {
41 	__delay((ns * 0x00000005ull * HZ * lpj_fine) >> 32);
42 }
43 EXPORT_SYMBOL(__ndelay);
44