delay.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _M68K_DELAY_H
  3. #define _M68K_DELAY_H
  4. #include <asm/param.h>
  5. /*
  6. * Copyright (C) 1994 Hamish Macdonald
  7. * Copyright (C) 2004 Greg Ungerer <[email protected]>
  8. *
  9. * Delay routines, using a pre-computed "loops_per_jiffy" value.
  10. */
  11. #if defined(CONFIG_COLDFIRE)
  12. /*
  13. * The ColdFire runs the delay loop at significantly different speeds
  14. * depending upon long word alignment or not. We'll pad it to
  15. * long word alignment which is the faster version.
  16. * The 0x4a8e is of course a 'tstl %fp' instruction. This is better
  17. * than using a NOP (0x4e71) instruction because it executes in one
  18. * cycle not three and doesn't allow for an arbitrary delay waiting
  19. * for bus cycles to finish. Also fp/a6 isn't likely to cause a
  20. * stall waiting for the register to become valid if such is added
  21. * to the coldfire at some stage.
  22. */
  23. #define DELAY_ALIGN ".balignw 4, 0x4a8e\n\t"
  24. #else
  25. /*
  26. * No instruction alignment required for other m68k types.
  27. */
  28. #define DELAY_ALIGN
  29. #endif
  30. static inline void __delay(unsigned long loops)
  31. {
  32. __asm__ __volatile__ (
  33. DELAY_ALIGN
  34. "1: subql #1,%0\n\t"
  35. "jcc 1b"
  36. : "=d" (loops)
  37. : "0" (loops));
  38. }
  39. extern void __bad_udelay(void);
  40. #ifdef CONFIG_CPU_HAS_NO_MULDIV64
  41. /*
  42. * The simpler m68k and ColdFire processors do not have a 32*32->64
  43. * multiply instruction. So we need to handle them a little differently.
  44. * We use a bit of shifting and a single 32*32->32 multiply to get close.
  45. */
  46. #define HZSCALE (268435456 / (1000000 / HZ))
  47. #define __const_udelay(u) \
  48. __delay(((((u) * HZSCALE) >> 11) * (loops_per_jiffy >> 11)) >> 6)
  49. #else
  50. static inline void __xdelay(unsigned long xloops)
  51. {
  52. unsigned long tmp;
  53. __asm__ ("mulul %2,%0:%1"
  54. : "=d" (xloops), "=d" (tmp)
  55. : "d" (xloops), "1" (loops_per_jiffy));
  56. __delay(xloops * HZ);
  57. }
  58. /*
  59. * The definition of __const_udelay is specifically made a macro so that
  60. * the const factor (4295 = 2**32 / 1000000) can be optimized out when
  61. * the delay is a const.
  62. */
  63. #define __const_udelay(n) (__xdelay((n) * 4295))
  64. #endif
  65. static inline void __udelay(unsigned long usecs)
  66. {
  67. __const_udelay(usecs);
  68. }
  69. /*
  70. * Use only for very small delays ( < 1 msec). Should probably use a
  71. * lookup table, really, as the multiplications take much too long with
  72. * short delays. This is a "reasonable" implementation, though (and the
  73. * first constant multiplications gets optimized away if the delay is
  74. * a constant)
  75. */
  76. #define udelay(n) (__builtin_constant_p(n) ? \
  77. ((n) > 20000 ? __bad_udelay() : __const_udelay(n)) : __udelay(n))
  78. /*
  79. * nanosecond delay:
  80. *
  81. * ((((HZSCALE) >> 11) * (loops_per_jiffy >> 11)) >> 6) is the number of loops
  82. * per microsecond
  83. *
  84. * 1000 / ((((HZSCALE) >> 11) * (loops_per_jiffy >> 11)) >> 6) is the number of
  85. * nanoseconds per loop
  86. *
  87. * So n / ( 1000 / ((((HZSCALE) >> 11) * (loops_per_jiffy >> 11)) >> 6) ) would
  88. * be the number of loops for n nanoseconds
  89. */
  90. /*
  91. * The simpler m68k and ColdFire processors do not have a 32*32->64
  92. * multiply instruction. So we need to handle them a little differently.
  93. * We use a bit of shifting and a single 32*32->32 multiply to get close.
  94. * This is a macro so that the const version can factor out the first
  95. * multiply and shift.
  96. */
  97. #define HZSCALE (268435456 / (1000000 / HZ))
  98. static inline void ndelay(unsigned long nsec)
  99. {
  100. __delay(DIV_ROUND_UP(nsec *
  101. ((((HZSCALE) >> 11) *
  102. (loops_per_jiffy >> 11)) >> 6),
  103. 1000));
  104. }
  105. #define ndelay(n) ndelay(n)
  106. #endif /* defined(_M68K_DELAY_H) */