s390/delay: simplify udelay
[linux-2.6-microblaze.git] / arch / s390 / lib / delay.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  *    Precise Delay Loops for S390
4  *
5  *    Copyright IBM Corp. 1999, 2008
6  *    Author(s): Martin Schwidefsky <schwidefsky@de.ibm.com>,
7  *               Heiko Carstens <heiko.carstens@de.ibm.com>,
8  */
9
10 #include <linux/sched.h>
11 #include <linux/delay.h>
12 #include <linux/timex.h>
13 #include <linux/export.h>
14 #include <linux/irqflags.h>
15 #include <linux/interrupt.h>
16 #include <linux/jump_label.h>
17 #include <linux/irq.h>
18 #include <asm/vtimer.h>
19 #include <asm/div64.h>
20 #include <asm/idle.h>
21
22 static DEFINE_STATIC_KEY_FALSE(udelay_ready);
23
24 void __init udelay_enable(void)
25 {
26         static_branch_enable(&udelay_ready);
27 }
28
29 void __delay(unsigned long loops)
30 {
31         /*
32          * To end the bloody studid and useless discussion about the
33          * BogoMips number I took the liberty to define the __delay
34          * function in a way that that resulting BogoMips number will
35          * yield the megahertz number of the cpu. The important function
36          * is udelay and that is done using the tod clock. -- martin.
37          */
38         asm volatile("0: brct %0,0b" : : "d" ((loops/2) + 1));
39 }
40 EXPORT_SYMBOL(__delay);
41
42 static void delay_loop(unsigned long delta, bool simple)
43 {
44         unsigned long end;
45
46         if (static_branch_likely(&udelay_ready) && !simple) {
47                 end = get_tod_clock_monotonic() + delta;
48                 while (!tod_after(get_tod_clock_monotonic(), end))
49                         cpu_relax();
50         } else {
51                 end = get_tod_clock() + delta;
52                 while (!tod_after(get_tod_clock(), end))
53                         cpu_relax();
54         }
55 }
56
57 void __udelay(unsigned long usecs)
58 {
59         delay_loop(usecs << 12, 0);
60 }
61 EXPORT_SYMBOL(__udelay);
62
63 /*
64  * Simple udelay variant. To be used on startup and reboot
65  * when the interrupt handler isn't working.
66  */
67 void udelay_simple(unsigned long usecs)
68 {
69         delay_loop(usecs << 12, 1);
70 }
71
72 void __ndelay(unsigned long nsecs)
73 {
74         nsecs <<= 9;
75         do_div(nsecs, 125);
76         delay_loop(nsecs, 0);
77 }
78 EXPORT_SYMBOL(__ndelay);