delay.c 2.7 KB

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