delay.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Precise Delay Loops for parisc
  4. *
  5. * based on code by:
  6. * Copyright (C) 1993 Linus Torvalds
  7. * Copyright (C) 1997 Martin Mares <[email protected]>
  8. * Copyright (C) 2008 Jiri Hladky <hladky _dot_ jiri _at_ gmail _dot_ com>
  9. *
  10. * parisc implementation:
  11. * Copyright (C) 2013 Helge Deller <[email protected]>
  12. */
  13. #include <linux/module.h>
  14. #include <linux/preempt.h>
  15. #include <linux/init.h>
  16. #include <asm/delay.h>
  17. #include <asm/special_insns.h> /* for mfctl() */
  18. #include <asm/processor.h> /* for boot_cpu_data */
  19. /* CR16 based delay: */
  20. static void __cr16_delay(unsigned long __loops)
  21. {
  22. /*
  23. * Note: Due to unsigned math, cr16 rollovers shouldn't be
  24. * a problem here. However, on 32 bit, we need to make sure
  25. * we don't pass in too big a value. The current default
  26. * value of MAX_UDELAY_MS should help prevent this.
  27. */
  28. u32 bclock, now, loops = __loops;
  29. int cpu;
  30. preempt_disable();
  31. cpu = smp_processor_id();
  32. bclock = mfctl(16);
  33. for (;;) {
  34. now = mfctl(16);
  35. if ((now - bclock) >= loops)
  36. break;
  37. /* Allow RT tasks to run */
  38. preempt_enable();
  39. asm volatile(" nop\n");
  40. barrier();
  41. preempt_disable();
  42. /*
  43. * It is possible that we moved to another CPU, and
  44. * since CR16's are per-cpu we need to calculate
  45. * that. The delay must guarantee that we wait "at
  46. * least" the amount of time. Being moved to another
  47. * CPU could make the wait longer but we just need to
  48. * make sure we waited long enough. Rebalance the
  49. * counter for this CPU.
  50. */
  51. if (unlikely(cpu != smp_processor_id())) {
  52. loops -= (now - bclock);
  53. cpu = smp_processor_id();
  54. bclock = mfctl(16);
  55. }
  56. }
  57. preempt_enable();
  58. }
  59. void __udelay(unsigned long usecs)
  60. {
  61. __cr16_delay(usecs * ((unsigned long)boot_cpu_data.cpu_hz / 1000000UL));
  62. }
  63. EXPORT_SYMBOL(__udelay);