xref: /openbmc/linux/arch/s390/lib/delay.c (revision 3821a065)
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 
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>
17 
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 
31 static void __udelay_disabled(unsigned long long usecs)
32 {
33 	unsigned long cr0, cr6, new;
34 	u64 clock_saved, end;
35 
36 	end = get_tod_clock() + (usecs << 12);
37 	clock_saved = local_tick_disable();
38 	__ctl_store(cr0, 0, 0);
39 	__ctl_store(cr6, 6, 6);
40 	new = (cr0 &  0xffff00e0) | 0x00000800;
41 	__ctl_load(new , 0, 0);
42 	new = 0;
43 	__ctl_load(new, 6, 6);
44 	lockdep_off();
45 	do {
46 		set_clock_comparator(end);
47 		enabled_wait();
48 	} while (get_tod_clock_fast() < end);
49 	lockdep_on();
50 	__ctl_load(cr0, 0, 0);
51 	__ctl_load(cr6, 6, 6);
52 	local_tick_enable(clock_saved);
53 }
54 
55 static void __udelay_enabled(unsigned long long usecs)
56 {
57 	u64 clock_saved, end;
58 
59 	end = get_tod_clock_fast() + (usecs << 12);
60 	do {
61 		clock_saved = 0;
62 		if (end < S390_lowcore.clock_comparator) {
63 			clock_saved = local_tick_disable();
64 			set_clock_comparator(end);
65 		}
66 		enabled_wait();
67 		if (clock_saved)
68 			local_tick_enable(clock_saved);
69 	} while (get_tod_clock_fast() < end);
70 }
71 
72 /*
73  * Waits for 'usecs' microseconds using the TOD clock comparator.
74  */
75 void __udelay(unsigned long long usecs)
76 {
77 	unsigned long flags;
78 
79 	preempt_disable();
80 	local_irq_save(flags);
81 	if (in_irq()) {
82 		__udelay_disabled(usecs);
83 		goto out;
84 	}
85 	if (in_softirq()) {
86 		if (raw_irqs_disabled_flags(flags))
87 			__udelay_disabled(usecs);
88 		else
89 			__udelay_enabled(usecs);
90 		goto out;
91 	}
92 	if (raw_irqs_disabled_flags(flags)) {
93 		local_bh_disable();
94 		__udelay_disabled(usecs);
95 		_local_bh_enable();
96 		goto out;
97 	}
98 	__udelay_enabled(usecs);
99 out:
100 	local_irq_restore(flags);
101 	preempt_enable();
102 }
103 EXPORT_SYMBOL(__udelay);
104 
105 /*
106  * Simple udelay variant. To be used on startup and reboot
107  * when the interrupt handler isn't working.
108  */
109 void udelay_simple(unsigned long long usecs)
110 {
111 	u64 end;
112 
113 	end = get_tod_clock_fast() + (usecs << 12);
114 	while (get_tod_clock_fast() < end)
115 		cpu_relax();
116 }
117 
118 void __ndelay(unsigned long long nsecs)
119 {
120 	u64 end;
121 
122 	nsecs <<= 9;
123 	do_div(nsecs, 125);
124 	end = get_tod_clock_fast() + nsecs;
125 	if (nsecs & ~0xfffUL)
126 		__udelay(nsecs >> 12);
127 	while (get_tod_clock_fast() < end)
128 		barrier();
129 }
130 EXPORT_SYMBOL(__ndelay);
131