xref: /openbmc/linux/arch/csky/lib/delay.c (revision 9a87ffc99ec8eb8d35eed7c4f816d75f5cc9662e)
1c5af58b7SGuo Ren // SPDX-License-Identifier: GPL-2.0
2c5af58b7SGuo Ren // Copyright (C) 2018 Hangzhou C-SKY Microsystems co.,ltd.
3c5af58b7SGuo Ren #include <linux/kernel.h>
4c5af58b7SGuo Ren #include <linux/module.h>
5c5af58b7SGuo Ren #include <linux/init.h>
6c5af58b7SGuo Ren #include <linux/delay.h>
7c5af58b7SGuo Ren 
__delay(unsigned long loops)8*4a3ec009SJialu Xu void __aligned(8) __delay(unsigned long loops)
9c5af58b7SGuo Ren {
10c5af58b7SGuo Ren 	asm volatile (
11c5af58b7SGuo Ren 		"mov r0, r0\n"
12c5af58b7SGuo Ren 		"1:declt %0\n"
13c5af58b7SGuo Ren 		"bf	1b"
14c5af58b7SGuo Ren 		: "=r"(loops)
15c5af58b7SGuo Ren 		: "0"(loops));
16c5af58b7SGuo Ren }
17c5af58b7SGuo Ren EXPORT_SYMBOL(__delay);
18c5af58b7SGuo Ren 
__const_udelay(unsigned long xloops)19c5af58b7SGuo Ren void __const_udelay(unsigned long xloops)
20c5af58b7SGuo Ren {
21c5af58b7SGuo Ren 	unsigned long long loops;
22c5af58b7SGuo Ren 
23c5af58b7SGuo Ren 	loops = (unsigned long long)xloops * loops_per_jiffy * HZ;
24c5af58b7SGuo Ren 
25c5af58b7SGuo Ren 	__delay(loops >> 32);
26c5af58b7SGuo Ren }
27c5af58b7SGuo Ren EXPORT_SYMBOL(__const_udelay);
28c5af58b7SGuo Ren 
__udelay(unsigned long usecs)29c5af58b7SGuo Ren void __udelay(unsigned long usecs)
30c5af58b7SGuo Ren {
31c5af58b7SGuo Ren 	__const_udelay(usecs * 0x10C7UL); /* 2**32 / 1000000 (rounded up) */
32c5af58b7SGuo Ren }
33c5af58b7SGuo Ren EXPORT_SYMBOL(__udelay);
34c5af58b7SGuo Ren 
__ndelay(unsigned long nsecs)35c5af58b7SGuo Ren void __ndelay(unsigned long nsecs)
36c5af58b7SGuo Ren {
37c5af58b7SGuo Ren 	__const_udelay(nsecs * 0x5UL); /* 2**32 / 1000000000 (rounded up) */
38c5af58b7SGuo Ren }
39c5af58b7SGuo Ren EXPORT_SYMBOL(__ndelay);
40