delay.c 2.6 KB

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