delay.h 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. #include <linux/param.h>
  13. #include <linux/smp.h>
  14. #include <asm/compiler.h>
  15. static inline void __delay(unsigned long loops)
  16. {
  17. if (sizeof(long) == 4)
  18. __asm__ __volatile__ (
  19. " .set noreorder \n"
  20. " .align 3 \n"
  21. "1: bnez %0, 1b \n"
  22. " subu %0, 1 \n"
  23. " .set reorder \n"
  24. : "=r" (loops)
  25. : "0" (loops));
  26. else if (sizeof(long) == 8)
  27. __asm__ __volatile__ (
  28. " .set noreorder \n"
  29. " .align 3 \n"
  30. "1: bnez %0, 1b \n"
  31. " dsubu %0, 1 \n"
  32. " .set reorder \n"
  33. : "=r" (loops)
  34. : "0" (loops));
  35. }
  36. /*
  37. * Division by multiplication: you don't have to worry about
  38. * loss of precision.
  39. *
  40. * Use only for very small delays ( < 1 msec). Should probably use a
  41. * lookup table, really, as the multiplications take much too long with
  42. * short delays. This is a "reasonable" implementation, though (and the
  43. * first constant multiplications gets optimized away if the delay is
  44. * a constant)
  45. */
  46. static inline void __udelay(unsigned long usecs, unsigned long lpj)
  47. {
  48. unsigned long lo;
  49. /*
  50. * The rates of 128 is rounded wrongly by the catchall case
  51. * for 64-bit. Excessive precission? Probably ...
  52. */
  53. #if defined(CONFIG_64BIT) && (HZ == 128)
  54. usecs *= 0x0008637bd05af6c7UL; /* 2**64 / (1000000 / HZ) */
  55. #elif defined(CONFIG_64BIT)
  56. usecs *= (0x8000000000000000UL / (500000 / HZ));
  57. #else /* 32-bit junk follows here */
  58. usecs *= (unsigned long) (((0x8000000000000000ULL / (500000 / HZ)) +
  59. 0x80000000ULL) >> 32);
  60. #endif
  61. if (sizeof(long) == 4)
  62. __asm__("multu\t%2, %3"
  63. : "=h" (usecs), "=l" (lo)
  64. : "r" (usecs), "r" (lpj)
  65. : GCC_REG_ACCUM);
  66. else if (sizeof(long) == 8)
  67. __asm__("dmultu\t%2, %3"
  68. : "=h" (usecs), "=l" (lo)
  69. : "r" (usecs), "r" (lpj)
  70. : GCC_REG_ACCUM);
  71. __delay(usecs);
  72. }
  73. #define __udelay_val cpu_data[raw_smp_processor_id()].udelay_val
  74. #define udelay(usecs) __udelay((usecs), __udelay_val)
  75. /* make sure "usecs *= ..." in udelay do not overflow. */
  76. #if HZ >= 1000
  77. #define MAX_UDELAY_MS 1
  78. #elif HZ <= 200
  79. #define MAX_UDELAY_MS 5
  80. #else
  81. #define MAX_UDELAY_MS (1000 / HZ)
  82. #endif
  83. #endif /* _ASM_DELAY_H */