Merge tag 'locks-v3.16-2' of git://git.samba.org/jlayton/linux
[linux/fpc-iii.git] / arch / s390 / lib / delay.c
bloba9f3d0042d58ba849b8c624f23915d88d03d6f70
1 /*
2 * Precise Delay Loops for S390
4 * Copyright IBM Corp. 1999, 2008
5 * Author(s): Martin Schwidefsky <schwidefsky@de.ibm.com>,
6 * Heiko Carstens <heiko.carstens@de.ibm.com>,
7 */
9 #include <linux/sched.h>
10 #include <linux/delay.h>
11 #include <linux/timex.h>
12 #include <linux/module.h>
13 #include <linux/irqflags.h>
14 #include <linux/interrupt.h>
15 #include <asm/vtimer.h>
16 #include <asm/div64.h>
18 void __delay(unsigned long loops)
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.
27 asm volatile("0: brct %0,0b" : : "d" ((loops/2) + 1));
30 static void __udelay_disabled(unsigned long long usecs)
32 unsigned long cr0, cr6, new;
33 u64 clock_saved, end;
35 end = get_tod_clock() + (usecs << 12);
36 clock_saved = local_tick_disable();
37 __ctl_store(cr0, 0, 0);
38 __ctl_store(cr6, 6, 6);
39 new = (cr0 & 0xffff00e0) | 0x00000800;
40 __ctl_load(new , 0, 0);
41 new = 0;
42 __ctl_load(new, 6, 6);
43 lockdep_off();
44 do {
45 set_clock_comparator(end);
46 vtime_stop_cpu();
47 } while (get_tod_clock_fast() < end);
48 lockdep_on();
49 __ctl_load(cr0, 0, 0);
50 __ctl_load(cr6, 6, 6);
51 local_tick_enable(clock_saved);
54 static void __udelay_enabled(unsigned long long usecs)
56 u64 clock_saved, end;
58 end = get_tod_clock_fast() + (usecs << 12);
59 do {
60 clock_saved = 0;
61 if (end < S390_lowcore.clock_comparator) {
62 clock_saved = local_tick_disable();
63 set_clock_comparator(end);
65 vtime_stop_cpu();
66 if (clock_saved)
67 local_tick_enable(clock_saved);
68 } while (get_tod_clock_fast() < end);
72 * Waits for 'usecs' microseconds using the TOD clock comparator.
74 void __udelay(unsigned long long usecs)
76 unsigned long flags;
78 preempt_disable();
79 local_irq_save(flags);
80 if (in_irq()) {
81 __udelay_disabled(usecs);
82 goto out;
84 if (in_softirq()) {
85 if (raw_irqs_disabled_flags(flags))
86 __udelay_disabled(usecs);
87 else
88 __udelay_enabled(usecs);
89 goto out;
91 if (raw_irqs_disabled_flags(flags)) {
92 local_bh_disable();
93 __udelay_disabled(usecs);
94 _local_bh_enable();
95 goto out;
97 __udelay_enabled(usecs);
98 out:
99 local_irq_restore(flags);
100 preempt_enable();
102 EXPORT_SYMBOL(__udelay);
105 * Simple udelay variant. To be used on startup and reboot
106 * when the interrupt handler isn't working.
108 void udelay_simple(unsigned long long usecs)
110 u64 end;
112 end = get_tod_clock_fast() + (usecs << 12);
113 while (get_tod_clock_fast() < end)
114 cpu_relax();
117 void __ndelay(unsigned long long nsecs)
119 u64 end;
121 nsecs <<= 9;
122 do_div(nsecs, 125);
123 end = get_tod_clock_fast() + nsecs;
124 if (nsecs & ~0xfffUL)
125 __udelay(nsecs >> 12);
126 while (get_tod_clock_fast() < end)
127 barrier();
129 EXPORT_SYMBOL(__ndelay);