delay.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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/vtimer.h>
  15. #include <asm/div64.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_tod_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. } while (get_tod_clock() < end);
  44. lockdep_on();
  45. __ctl_load(cr0, 0, 0);
  46. __ctl_load(cr6, 6, 6);
  47. local_tick_enable(clock_saved);
  48. }
  49. static void __udelay_enabled(unsigned long long usecs)
  50. {
  51. u64 clock_saved, end;
  52. end = get_tod_clock() + (usecs << 12);
  53. do {
  54. clock_saved = 0;
  55. if (end < S390_lowcore.clock_comparator) {
  56. clock_saved = local_tick_disable();
  57. set_clock_comparator(end);
  58. }
  59. vtime_stop_cpu();
  60. if (clock_saved)
  61. local_tick_enable(clock_saved);
  62. } while (get_tod_clock() < end);
  63. }
  64. /*
  65. * Waits for 'usecs' microseconds using the TOD clock comparator.
  66. */
  67. void __udelay(unsigned long long usecs)
  68. {
  69. unsigned long flags;
  70. preempt_disable();
  71. local_irq_save(flags);
  72. if (in_irq()) {
  73. __udelay_disabled(usecs);
  74. goto out;
  75. }
  76. if (in_softirq()) {
  77. if (raw_irqs_disabled_flags(flags))
  78. __udelay_disabled(usecs);
  79. else
  80. __udelay_enabled(usecs);
  81. goto out;
  82. }
  83. if (raw_irqs_disabled_flags(flags)) {
  84. local_bh_disable();
  85. __udelay_disabled(usecs);
  86. _local_bh_enable();
  87. goto out;
  88. }
  89. __udelay_enabled(usecs);
  90. out:
  91. local_irq_restore(flags);
  92. preempt_enable();
  93. }
  94. EXPORT_SYMBOL(__udelay);
  95. /*
  96. * Simple udelay variant. To be used on startup and reboot
  97. * when the interrupt handler isn't working.
  98. */
  99. void udelay_simple(unsigned long long usecs)
  100. {
  101. u64 end;
  102. end = get_tod_clock() + (usecs << 12);
  103. while (get_tod_clock() < end)
  104. cpu_relax();
  105. }
  106. void __ndelay(unsigned long long nsecs)
  107. {
  108. u64 end;
  109. nsecs <<= 9;
  110. do_div(nsecs, 125);
  111. end = get_tod_clock() + nsecs;
  112. if (nsecs & ~0xfffUL)
  113. __udelay(nsecs >> 12);
  114. while (get_tod_clock() < end)
  115. barrier();
  116. }
  117. EXPORT_SYMBOL(__ndelay);