delay.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. * Precise Delay Loops for S390
  3. *
  4. * Copyright IBM Corp. 1999,2008
  5. * Author(s): Martin Schwidefsky <schwidefsky@de.ibm.com>,
  6. * Heiko Carstens <heiko.carstens@de.ibm.com>,
  7. */
  8. #include <linux/sched.h>
  9. #include <linux/delay.h>
  10. #include <linux/timex.h>
  11. #include <linux/module.h>
  12. #include <linux/irqflags.h>
  13. #include <linux/interrupt.h>
  14. #include <asm/div64.h>
  15. #include <asm/timer.h>
  16. void __delay(unsigned long loops)
  17. {
  18. /*
  19. * To end the bloody studid and useless discussion about the
  20. * BogoMips number I took the liberty to define the __delay
  21. * function in a way that that resulting BogoMips number will
  22. * yield the megahertz number of the cpu. The important function
  23. * is udelay and that is done using the tod clock. -- martin.
  24. */
  25. asm volatile("0: brct %0,0b" : : "d" ((loops/2) + 1));
  26. }
  27. static void __udelay_disabled(unsigned long long usecs)
  28. {
  29. unsigned long cr0, cr6, new;
  30. u64 clock_saved, end;
  31. end = get_clock() + (usecs << 12);
  32. clock_saved = local_tick_disable();
  33. __ctl_store(cr0, 0, 0);
  34. __ctl_store(cr6, 6, 6);
  35. new = (cr0 & 0xffff00e0) | 0x00000800;
  36. __ctl_load(new , 0, 0);
  37. new = 0;
  38. __ctl_load(new, 6, 6);
  39. lockdep_off();
  40. do {
  41. set_clock_comparator(end);
  42. vtime_stop_cpu();
  43. local_irq_disable();
  44. } while (get_clock() < end);
  45. lockdep_on();
  46. __ctl_load(cr0, 0, 0);
  47. __ctl_load(cr6, 6, 6);
  48. local_tick_enable(clock_saved);
  49. }
  50. static void __udelay_enabled(unsigned long long usecs)
  51. {
  52. u64 clock_saved, end;
  53. end = get_clock() + (usecs << 12);
  54. do {
  55. clock_saved = 0;
  56. if (end < S390_lowcore.clock_comparator) {
  57. clock_saved = local_tick_disable();
  58. set_clock_comparator(end);
  59. }
  60. vtime_stop_cpu();
  61. local_irq_disable();
  62. if (clock_saved)
  63. local_tick_enable(clock_saved);
  64. } while (get_clock() < end);
  65. }
  66. /*
  67. * Waits for 'usecs' microseconds using the TOD clock comparator.
  68. */
  69. void __udelay(unsigned long long usecs)
  70. {
  71. unsigned long flags;
  72. preempt_disable();
  73. local_irq_save(flags);
  74. if (in_irq()) {
  75. __udelay_disabled(usecs);
  76. goto out;
  77. }
  78. if (in_softirq()) {
  79. if (raw_irqs_disabled_flags(flags))
  80. __udelay_disabled(usecs);
  81. else
  82. __udelay_enabled(usecs);
  83. goto out;
  84. }
  85. if (raw_irqs_disabled_flags(flags)) {
  86. local_bh_disable();
  87. __udelay_disabled(usecs);
  88. _local_bh_enable();
  89. goto out;
  90. }
  91. __udelay_enabled(usecs);
  92. out:
  93. local_irq_restore(flags);
  94. preempt_enable();
  95. }
  96. EXPORT_SYMBOL(__udelay);
  97. /*
  98. * Simple udelay variant. To be used on startup and reboot
  99. * when the interrupt handler isn't working.
  100. */
  101. void udelay_simple(unsigned long long usecs)
  102. {
  103. u64 end;
  104. end = get_clock() + (usecs << 12);
  105. while (get_clock() < end)
  106. cpu_relax();
  107. }
  108. void __ndelay(unsigned long long nsecs)
  109. {
  110. u64 end;
  111. nsecs <<= 9;
  112. do_div(nsecs, 125);
  113. end = get_clock() + nsecs;
  114. if (nsecs & ~0xfffUL)
  115. __udelay(nsecs >> 12);
  116. while (get_clock() < end)
  117. barrier();
  118. }
  119. EXPORT_SYMBOL(__ndelay);