Chris Zankel | 9a8fd55 | 2005-06-23 22:01:26 -0700 | [diff] [blame] | 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 | |
Baruch Siach | 8102f47 | 2013-06-17 11:29:44 +0300 | [diff] [blame] | 15 | #include <asm/timex.h> |
Chris Zankel | 9a8fd55 | 2005-06-23 22:01:26 -0700 | [diff] [blame] | 16 | #include <asm/param.h> |
| 17 | |
| 18 | extern unsigned long loops_per_jiffy; |
| 19 | |
Adrian Bunk | d99cf71 | 2005-09-03 15:57:53 -0700 | [diff] [blame] | 20 | static inline void __delay(unsigned long loops) |
Chris Zankel | 9a8fd55 | 2005-06-23 22:01:26 -0700 | [diff] [blame] | 21 | { |
Max Filippov | 01e3b3c | 2013-10-17 02:42:16 +0400 | [diff] [blame] | 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)); |
Chris Zankel | 9a8fd55 | 2005-06-23 22:01:26 -0700 | [diff] [blame] | 28 | } |
| 29 | |
Max Filippov | 58f60c2 | 2014-01-10 12:02:18 +0400 | [diff] [blame] | 30 | /* Undefined function to get compile-time error */ |
| 31 | void __bad_udelay(void); |
Max Filippov | 9ed82c6 | 2014-01-10 12:03:35 +0400 | [diff] [blame] | 32 | void __bad_ndelay(void); |
Chris Zankel | 9a8fd55 | 2005-06-23 22:01:26 -0700 | [diff] [blame] | 33 | |
Max Filippov | 58f60c2 | 2014-01-10 12:02:18 +0400 | [diff] [blame] | 34 | #define __MAX_UDELAY 30000 |
Max Filippov | 9ed82c6 | 2014-01-10 12:03:35 +0400 | [diff] [blame] | 35 | #define __MAX_NDELAY 30000 |
Max Filippov | 58f60c2 | 2014-01-10 12:02:18 +0400 | [diff] [blame] | 36 | |
| 37 | static inline void __udelay(unsigned long usecs) |
Chris Zankel | 9a8fd55 | 2005-06-23 22:01:26 -0700 | [diff] [blame] | 38 | { |
Baruch Siach | 8102f47 | 2013-06-17 11:29:44 +0300 | [diff] [blame] | 39 | unsigned long start = get_ccount(); |
Max Filippov | 58f60c2 | 2014-01-10 12:02:18 +0400 | [diff] [blame] | 40 | unsigned long cycles = (usecs * (ccount_freq >> 15)) >> 5; |
Chris Zankel | 9a8fd55 | 2005-06-23 22:01:26 -0700 | [diff] [blame] | 41 | |
| 42 | /* Note: all variables are unsigned (can wrap around)! */ |
Baruch Siach | 8102f47 | 2013-06-17 11:29:44 +0300 | [diff] [blame] | 43 | while (((unsigned long)get_ccount()) - start < cycles) |
Max Filippov | 58f60c2 | 2014-01-10 12:02:18 +0400 | [diff] [blame] | 44 | cpu_relax(); |
| 45 | } |
| 46 | |
| 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); |
Chris Zankel | 9a8fd55 | 2005-06-23 22:01:26 -0700 | [diff] [blame] | 53 | } |
| 54 | |
Max Filippov | 9ed82c6 | 2014-01-10 12:03:35 +0400 | [diff] [blame] | 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 | |
| 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 | |
Chris Zankel | 9a8fd55 | 2005-06-23 22:01:26 -0700 | [diff] [blame] | 75 | #endif |