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