delay.c 1.8 KB

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