Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * This file is subject to the terms and conditions of the GNU General Public |
| 3 | * License. See the file "COPYING" in the main directory of this archive |
| 4 | * for more details. |
| 5 | * |
| 6 | * Copyright (C) 1994 by Waldorf Electronics |
| 7 | * Copyright (C) 1995 - 2000, 01, 03 by Ralf Baechle |
| 8 | * Copyright (C) 1999, 2000 Silicon Graphics, Inc. |
| 9 | */ |
| 10 | #ifndef _ASM_DELAY_H |
| 11 | #define _ASM_DELAY_H |
| 12 | |
| 13 | #include <linux/config.h> |
| 14 | #include <linux/param.h> |
Ralf Baechle | 88de09f | 2005-04-18 10:40:09 +0000 | [diff] [blame] | 15 | #include <linux/smp.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #include <asm/compiler.h> |
| 17 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | static inline void __delay(unsigned long loops) |
| 19 | { |
| 20 | if (sizeof(long) == 4) |
| 21 | __asm__ __volatile__ ( |
Ralf Baechle | 5ee8235 | 2006-05-23 16:37:32 +0100 | [diff] [blame^] | 22 | " .set noreorder \n" |
| 23 | " .align 3 \n" |
| 24 | "1: bnez %0, 1b \n" |
| 25 | " subu %0, 1 \n" |
| 26 | " .set reorder \n" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | : "=r" (loops) |
| 28 | : "0" (loops)); |
| 29 | else if (sizeof(long) == 8) |
| 30 | __asm__ __volatile__ ( |
Ralf Baechle | 5ee8235 | 2006-05-23 16:37:32 +0100 | [diff] [blame^] | 31 | " .set noreorder \n" |
| 32 | " .align 3 \n" |
| 33 | "1: bnez %0, 1b \n" |
| 34 | " dsubu %0, 1 \n" |
| 35 | " .set reorder \n" |
| 36 | : "=r" (loops) |
| 37 | : "0" (loops)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | |
| 41 | /* |
| 42 | * Division by multiplication: you don't have to worry about |
| 43 | * loss of precision. |
| 44 | * |
| 45 | * Use only for very small delays ( < 1 msec). Should probably use a |
| 46 | * lookup table, really, as the multiplications take much too long with |
| 47 | * short delays. This is a "reasonable" implementation, though (and the |
| 48 | * first constant multiplications gets optimized away if the delay is |
| 49 | * a constant) |
| 50 | */ |
| 51 | |
| 52 | static inline void __udelay(unsigned long usecs, unsigned long lpj) |
| 53 | { |
| 54 | unsigned long lo; |
| 55 | |
| 56 | /* |
Atsushi Nemoto | f12555d | 2005-11-30 13:33:26 +0900 | [diff] [blame] | 57 | * The rates of 128 is rounded wrongly by the catchall case |
| 58 | * for 64-bit. Excessive precission? Probably ... |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | */ |
Ralf Baechle | 875d43e | 2005-09-03 15:56:16 -0700 | [diff] [blame] | 60 | #if defined(CONFIG_64BIT) && (HZ == 128) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | usecs *= 0x0008637bd05af6c7UL; /* 2**64 / (1000000 / HZ) */ |
Ralf Baechle | 875d43e | 2005-09-03 15:56:16 -0700 | [diff] [blame] | 62 | #elif defined(CONFIG_64BIT) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | usecs *= (0x8000000000000000UL / (500000 / HZ)); |
| 64 | #else /* 32-bit junk follows here */ |
| 65 | usecs *= (unsigned long) (((0x8000000000000000ULL / (500000 / HZ)) + |
| 66 | 0x80000000ULL) >> 32); |
| 67 | #endif |
| 68 | |
| 69 | if (sizeof(long) == 4) |
| 70 | __asm__("multu\t%2, %3" |
| 71 | : "=h" (usecs), "=l" (lo) |
| 72 | : "r" (usecs), "r" (lpj) |
| 73 | : GCC_REG_ACCUM); |
| 74 | else if (sizeof(long) == 8) |
| 75 | __asm__("dmultu\t%2, %3" |
| 76 | : "=h" (usecs), "=l" (lo) |
| 77 | : "r" (usecs), "r" (lpj) |
| 78 | : GCC_REG_ACCUM); |
| 79 | |
| 80 | __delay(usecs); |
| 81 | } |
| 82 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | #define __udelay_val cpu_data[smp_processor_id()].udelay_val |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | |
| 85 | #define udelay(usecs) __udelay((usecs),__udelay_val) |
| 86 | |
Atsushi Nemoto | a0f0820 | 2005-11-05 02:02:54 +0900 | [diff] [blame] | 87 | /* make sure "usecs *= ..." in udelay do not overflow. */ |
| 88 | #if HZ >= 1000 |
| 89 | #define MAX_UDELAY_MS 1 |
| 90 | #elif HZ <= 200 |
| 91 | #define MAX_UDELAY_MS 5 |
| 92 | #else |
| 93 | #define MAX_UDELAY_MS (1000 / HZ) |
| 94 | #endif |
| 95 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | #endif /* _ASM_DELAY_H */ |