delay.h 2.4 KB

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