delay.h 2.4 KB

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