delay.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. trace_hardirqs_on();
  36. __load_psw_mask(mask);
  37. local_irq_disable();
  38. __ctl_load(cr0_saved, 0, 0);
  39. local_tick_enable(clock_saved);
  40. set_clock_comparator(S390_lowcore.clock_comparator);
  41. }
  42. static void __udelay_enabled(unsigned long usecs)
  43. {
  44. unsigned long mask;
  45. u64 end, time;
  46. mask = psw_kernel_bits | PSW_MASK_WAIT | PSW_MASK_EXT | PSW_MASK_IO;
  47. end = get_clock() + ((u64) usecs << 12);
  48. do {
  49. time = end < S390_lowcore.clock_comparator ?
  50. end : S390_lowcore.clock_comparator;
  51. set_clock_comparator(time);
  52. trace_hardirqs_on();
  53. __load_psw_mask(mask);
  54. local_irq_disable();
  55. } while (get_clock() < end);
  56. set_clock_comparator(S390_lowcore.clock_comparator);
  57. }
  58. /*
  59. * Waits for 'usecs' microseconds using the TOD clock comparator.
  60. */
  61. void __udelay(unsigned long usecs)
  62. {
  63. unsigned long flags;
  64. preempt_disable();
  65. local_irq_save(flags);
  66. if (in_irq()) {
  67. __udelay_disabled(usecs);
  68. goto out;
  69. }
  70. if (in_softirq()) {
  71. if (raw_irqs_disabled_flags(flags))
  72. __udelay_disabled(usecs);
  73. else
  74. __udelay_enabled(usecs);
  75. goto out;
  76. }
  77. if (raw_irqs_disabled_flags(flags)) {
  78. local_bh_disable();
  79. __udelay_disabled(usecs);
  80. _local_bh_enable();
  81. goto out;
  82. }
  83. __udelay_enabled(usecs);
  84. out:
  85. local_irq_restore(flags);
  86. preempt_enable();
  87. }
  88. EXPORT_SYMBOL(__udelay);
  89. /*
  90. * Simple udelay variant. To be used on startup and reboot
  91. * when the interrupt handler isn't working.
  92. */
  93. void udelay_simple(unsigned long usecs)
  94. {
  95. u64 end;
  96. end = get_clock() + ((u64) usecs << 12);
  97. while (get_clock() < end)
  98. cpu_relax();
  99. }