udelay.c 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 1993, 2000 Linus Torvalds
  4. *
  5. * Delay routines, using a pre-computed "loops_per_jiffy" value.
  6. */
  7. #include <linux/module.h>
  8. #include <linux/sched.h> /* for udelay's use of smp_processor_id */
  9. #include <asm/param.h>
  10. #include <asm/smp.h>
  11. #include <linux/delay.h>
  12. /*
  13. * Use only for very small delays (< 1 msec).
  14. *
  15. * The active part of our cycle counter is only 32-bits wide, and
  16. * we're treating the difference between two marks as signed. On
  17. * a 1GHz box, that's about 2 seconds.
  18. */
  19. void
  20. __delay(int loops)
  21. {
  22. int tmp;
  23. __asm__ __volatile__(
  24. " rpcc %0\n"
  25. " addl %1,%0,%1\n"
  26. "1: rpcc %0\n"
  27. " subl %1,%0,%0\n"
  28. " bgt %0,1b"
  29. : "=&r" (tmp), "=r" (loops) : "1"(loops));
  30. }
  31. EXPORT_SYMBOL(__delay);
  32. #ifdef CONFIG_SMP
  33. #define LPJ cpu_data[smp_processor_id()].loops_per_jiffy
  34. #else
  35. #define LPJ loops_per_jiffy
  36. #endif
  37. void
  38. udelay(unsigned long usecs)
  39. {
  40. usecs *= (((unsigned long)HZ << 32) / 1000000) * LPJ;
  41. __delay((long)usecs >> 32);
  42. }
  43. EXPORT_SYMBOL(udelay);
  44. void
  45. ndelay(unsigned long nsecs)
  46. {
  47. nsecs *= (((unsigned long)HZ << 32) / 1000000000) * LPJ;
  48. __delay((long)nsecs >> 32);
  49. }
  50. EXPORT_SYMBOL(ndelay);