blob: de5105d05f1ee3874a8d8e21246b3c727d09f385 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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.
Maciej W. Rozycki20d60d92007-10-23 12:43:11 +01009 * Copyright (C) 2007 Maciej W. Rozycki
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 */
11#ifndef _ASM_DELAY_H
12#define _ASM_DELAY_H
13
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/param.h>
Ralf Baechle88de09f2005-04-18 10:40:09 +000015#include <linux/smp.h>
Maciej W. Rozycki20d60d92007-10-23 12:43:11 +010016
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <asm/compiler.h>
Maciej W. Rozycki20d60d92007-10-23 12:43:11 +010018#include <asm/war.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
Linus Torvalds1da177e2005-04-16 15:20:36 -070020static inline void __delay(unsigned long loops)
21{
22 if (sizeof(long) == 4)
23 __asm__ __volatile__ (
Ralf Baechle5ee82352006-05-23 16:37:32 +010024 " .set noreorder \n"
25 " .align 3 \n"
26 "1: bnez %0, 1b \n"
27 " subu %0, 1 \n"
28 " .set reorder \n"
Linus Torvalds1da177e2005-04-16 15:20:36 -070029 : "=r" (loops)
30 : "0" (loops));
31 else if (sizeof(long) == 8)
32 __asm__ __volatile__ (
Ralf Baechle5ee82352006-05-23 16:37:32 +010033 " .set noreorder \n"
34 " .align 3 \n"
35 "1: bnez %0, 1b \n"
36 " dsubu %0, 1 \n"
37 " .set reorder \n"
38 : "=r" (loops)
39 : "0" (loops));
Linus Torvalds1da177e2005-04-16 15:20:36 -070040}
41
42
43/*
44 * Division by multiplication: you don't have to worry about
45 * loss of precision.
46 *
47 * Use only for very small delays ( < 1 msec). Should probably use a
48 * lookup table, really, as the multiplications take much too long with
49 * short delays. This is a "reasonable" implementation, though (and the
50 * first constant multiplications gets optimized away if the delay is
51 * a constant)
52 */
53
54static inline void __udelay(unsigned long usecs, unsigned long lpj)
55{
Maciej W. Rozycki20d60d92007-10-23 12:43:11 +010056 unsigned long hi, lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
58 /*
Atsushi Nemotof12555d2005-11-30 13:33:26 +090059 * The rates of 128 is rounded wrongly by the catchall case
60 * for 64-bit. Excessive precission? Probably ...
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 */
Ralf Baechle875d43e2005-09-03 15:56:16 -070062#if defined(CONFIG_64BIT) && (HZ == 128)
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 usecs *= 0x0008637bd05af6c7UL; /* 2**64 / (1000000 / HZ) */
Ralf Baechle875d43e2005-09-03 15:56:16 -070064#elif defined(CONFIG_64BIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 usecs *= (0x8000000000000000UL / (500000 / HZ));
66#else /* 32-bit junk follows here */
67 usecs *= (unsigned long) (((0x8000000000000000ULL / (500000 / HZ)) +
68 0x80000000ULL) >> 32);
69#endif
70
71 if (sizeof(long) == 4)
72 __asm__("multu\t%2, %3"
73 : "=h" (usecs), "=l" (lo)
74 : "r" (usecs), "r" (lpj)
75 : GCC_REG_ACCUM);
Maciej W. Rozycki20d60d92007-10-23 12:43:11 +010076 else if (sizeof(long) == 8 && !R4000_WAR)
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 __asm__("dmultu\t%2, %3"
78 : "=h" (usecs), "=l" (lo)
79 : "r" (usecs), "r" (lpj)
80 : GCC_REG_ACCUM);
Maciej W. Rozycki20d60d92007-10-23 12:43:11 +010081 else if (sizeof(long) == 8 && R4000_WAR)
82 __asm__("dmultu\t%3, %4\n\tmfhi\t%0"
83 : "=r" (usecs), "=h" (hi), "=l" (lo)
84 : "r" (usecs), "r" (lpj)
85 : GCC_REG_ACCUM);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
87 __delay(usecs);
88}
89
Deepak Saxena83598f12007-03-19 16:49:45 -070090#define __udelay_val cpu_data[raw_smp_processor_id()].udelay_val
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
Ralf Baechle21a151d2007-10-11 23:46:15 +010092#define udelay(usecs) __udelay((usecs), __udelay_val)
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
Atsushi Nemotoa0f08202005-11-05 02:02:54 +090094/* make sure "usecs *= ..." in udelay do not overflow. */
95#if HZ >= 1000
96#define MAX_UDELAY_MS 1
97#elif HZ <= 200
98#define MAX_UDELAY_MS 5
99#else
100#define MAX_UDELAY_MS (1000 / HZ)
101#endif
102
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103#endif /* _ASM_DELAY_H */