1 /*
2  * include/asm-xtensa/delay.h
3  *
4  * This file is subject to the terms and conditions of the GNU General Public
5  * License.  See the file "COPYING" in the main directory of this archive
6  * for more details.
7  *
8  * Copyright (C) 2001 - 2005 Tensilica Inc.
9  *
10  */
11 
12 #ifndef _XTENSA_DELAY_H
13 #define _XTENSA_DELAY_H
14 
15 #include <asm/timex.h>
16 #include <asm/param.h>
17 
18 extern unsigned long loops_per_jiffy;
19 
__delay(unsigned long loops)20 static inline void __delay(unsigned long loops)
21 {
22 	if (__builtin_constant_p(loops) && loops < 2)
23 		__asm__ __volatile__ ("nop");
24 	else if (loops >= 2)
25 		/* 2 cycles per loop. */
26 		__asm__ __volatile__ ("1: addi %0, %0, -2; bgeui %0, 2, 1b"
27 				: "+r" (loops));
28 }
29 
30 /* Undefined function to get compile-time error */
31 void __bad_udelay(void);
32 void __bad_ndelay(void);
33 
34 #define __MAX_UDELAY 30000
35 #define __MAX_NDELAY 30000
36 
__udelay(unsigned long usecs)37 static inline void __udelay(unsigned long usecs)
38 {
39 	unsigned long start = get_ccount();
40 	unsigned long cycles = (usecs * (ccount_freq >> 15)) >> 5;
41 
42 	/* Note: all variables are unsigned (can wrap around)! */
43 	while (((unsigned long)get_ccount()) - start < cycles)
44 		cpu_relax();
45 }
46 
udelay(unsigned long usec)47 static inline void udelay(unsigned long usec)
48 {
49 	if (__builtin_constant_p(usec) && usec >= __MAX_UDELAY)
50 		__bad_udelay();
51 	else
52 		__udelay(usec);
53 }
54 
__ndelay(unsigned long nsec)55 static inline void __ndelay(unsigned long nsec)
56 {
57 	/*
58 	 * Inner shift makes sure multiplication doesn't overflow
59 	 * for legitimate nsec values
60 	 */
61 	unsigned long cycles = (nsec * (ccount_freq >> 15)) >> 15;
62 	__delay(cycles);
63 }
64 
65 #define ndelay(n) ndelay(n)
66 
ndelay(unsigned long nsec)67 static inline void ndelay(unsigned long nsec)
68 {
69 	if (__builtin_constant_p(nsec) && nsec >= __MAX_NDELAY)
70 		__bad_ndelay();
71 	else
72 		__ndelay(nsec);
73 }
74 
75 #endif
76