delay.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. void __delay(unsigned long loops)
  16. {
  17. /*
  18. * To end the bloody studid and useless discussion about the
  19. * BogoMips number I took the liberty to define the __delay
  20. * function in a way that that resulting BogoMips number will
  21. * yield the megahertz number of the cpu. The important function
  22. * is udelay and that is done using the tod clock. -- martin.
  23. */
  24. asm volatile("0: brct %0,0b" : : "d" ((loops/2) + 1));
  25. }
  26. static void __udelay_disabled(unsigned long long usecs)
  27. {
  28. unsigned long mask, cr0, cr0_saved;
  29. u64 clock_saved;
  30. u64 end;
  31. mask = psw_kernel_bits | PSW_MASK_WAIT | PSW_MASK_EXT;
  32. end = get_clock() + (usecs << 12);
  33. clock_saved = local_tick_disable();
  34. __ctl_store(cr0_saved, 0, 0);
  35. cr0 = (cr0_saved & 0xffff00e0) | 0x00000800;
  36. __ctl_load(cr0 , 0, 0);
  37. lockdep_off();
  38. do {
  39. set_clock_comparator(end);
  40. trace_hardirqs_on();
  41. __load_psw_mask(mask);
  42. local_irq_disable();
  43. } while (get_clock() < end);
  44. lockdep_on();
  45. __ctl_load(cr0_saved, 0, 0);
  46. local_tick_enable(clock_saved);
  47. }
  48. static void __udelay_enabled(unsigned long long usecs)
  49. {
  50. unsigned long mask;
  51. u64 clock_saved;
  52. u64 end;
  53. mask = psw_kernel_bits | PSW_MASK_WAIT | PSW_MASK_EXT | PSW_MASK_IO;
  54. end = get_clock() + (usecs << 12);
  55. do {
  56. clock_saved = 0;
  57. if (end < S390_lowcore.clock_comparator) {
  58. clock_saved = local_tick_disable();
  59. set_clock_comparator(end);
  60. }
  61. trace_hardirqs_on();
  62. __load_psw_mask(mask);
  63. local_irq_disable();
  64. if (clock_saved)
  65. local_tick_enable(clock_saved);
  66. } while (get_clock() < end);
  67. }
  68. /*
  69. * Waits for 'usecs' microseconds using the TOD clock comparator.
  70. */
  71. void __udelay(unsigned long long usecs)
  72. {
  73. unsigned long flags;
  74. preempt_disable();
  75. local_irq_save(flags);
  76. if (in_irq()) {
  77. __udelay_disabled(usecs);
  78. goto out;
  79. }
  80. if (in_softirq()) {
  81. if (raw_irqs_disabled_flags(flags))
  82. __udelay_disabled(usecs);
  83. else
  84. __udelay_enabled(usecs);
  85. goto out;
  86. }
  87. if (raw_irqs_disabled_flags(flags)) {
  88. local_bh_disable();
  89. __udelay_disabled(usecs);
  90. _local_bh_enable();
  91. goto out;
  92. }
  93. __udelay_enabled(usecs);
  94. out:
  95. local_irq_restore(flags);
  96. preempt_enable();
  97. }
  98. EXPORT_SYMBOL(__udelay);
  99. /*
  100. * Simple udelay variant. To be used on startup and reboot
  101. * when the interrupt handler isn't working.
  102. */
  103. void udelay_simple(unsigned long long usecs)
  104. {
  105. u64 end;
  106. end = get_clock() + (usecs << 12);
  107. while (get_clock() < end)
  108. cpu_relax();
  109. }
  110. void __ndelay(unsigned long long nsecs)
  111. {
  112. u64 end;
  113. nsecs <<= 9;
  114. do_div(nsecs, 125);
  115. end = get_clock() + nsecs;
  116. if (nsecs & ~0xfffUL)
  117. __udelay(nsecs >> 12);
  118. while (get_clock() < end)
  119. barrier();
  120. }
  121. EXPORT_SYMBOL(__ndelay);