delay.c 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /*
  2. * Precise Delay Loops for parisc
  3. *
  4. * based on code by:
  5. * Copyright (C) 1993 Linus Torvalds
  6. * Copyright (C) 1997 Martin Mares <mj@atrey.karlin.mff.cuni.cz>
  7. * Copyright (C) 2008 Jiri Hladky <hladky _dot_ jiri _at_ gmail _dot_ com>
  8. *
  9. * parisc implementation:
  10. * Copyright (C) 2013 Helge Deller <deller@gmx.de>
  11. */
  12. #include <linux/module.h>
  13. #include <linux/preempt.h>
  14. #include <linux/init.h>
  15. #include <asm/processor.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);