delay.c 1019 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2020-2022 Loongson Technology Corporation Limited
  4. */
  5. #include <linux/delay.h>
  6. #include <linux/export.h>
  7. #include <linux/smp.h>
  8. #include <linux/timex.h>
  9. #include <asm/processor.h>
  10. void __delay(unsigned long cycles)
  11. {
  12. u64 t0 = get_cycles();
  13. while ((unsigned long)(get_cycles() - t0) < cycles)
  14. cpu_relax();
  15. }
  16. EXPORT_SYMBOL(__delay);
  17. /*
  18. * Division by multiplication: you don't have to worry about
  19. * loss of precision.
  20. *
  21. * Use only for very small delays ( < 1 msec). Should probably use a
  22. * lookup table, really, as the multiplications take much too long with
  23. * short delays. This is a "reasonable" implementation, though (and the
  24. * first constant multiplications gets optimized away if the delay is
  25. * a constant)
  26. */
  27. void __udelay(unsigned long us)
  28. {
  29. __delay((us * 0x000010c7ull * HZ * lpj_fine) >> 32);
  30. }
  31. EXPORT_SYMBOL(__udelay);
  32. void __ndelay(unsigned long ns)
  33. {
  34. __delay((ns * 0x00000005ull * HZ * lpj_fine) >> 32);
  35. }
  36. EXPORT_SYMBOL(__ndelay);