delay.c 3.0 KB

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