1 // SPDX-License-Identifier: GPL-2.0
3 * Precise Delay Loops for S390
5 * Copyright IBM Corp. 1999, 2008
6 * Author(s): Martin Schwidefsky <schwidefsky@de.ibm.com>,
7 * Heiko Carstens <heiko.carstens@de.ibm.com>,
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>
22 static DEFINE_STATIC_KEY_FALSE(udelay_ready);
24 void __init udelay_enable(void)
26 static_branch_enable(&udelay_ready);
29 void __delay(unsigned long loops)
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.
38 asm volatile("0: brct %0,0b" : : "d" ((loops/2) + 1));
40 EXPORT_SYMBOL(__delay);
42 static void __udelay_disabled(unsigned long long usecs)
44 unsigned long cr0, cr0_new, psw_mask;
45 struct s390_idle_data idle;
48 end = get_tod_clock() + (usecs << 12);
49 __ctl_store(cr0, 0, 0);
50 cr0_new = cr0 & ~CR0_IRQ_SUBCLASS_MASK;
51 cr0_new |= (1UL << (63 - 52)); /* enable clock comparator irq */
52 __ctl_load(cr0_new, 0, 0);
53 psw_mask = __extract_psw() | PSW_MASK_EXT | PSW_MASK_WAIT;
54 set_clock_comparator(end);
55 set_cpu_flag(CIF_IGNORE_IRQ);
56 psw_idle(&idle, psw_mask);
58 clear_cpu_flag(CIF_IGNORE_IRQ);
59 set_clock_comparator(S390_lowcore.clock_comparator);
60 __ctl_load(cr0, 0, 0);
63 static void __udelay_enabled(unsigned long long usecs)
67 end = get_tod_clock_fast() + (usecs << 12);
70 if (tod_after(S390_lowcore.clock_comparator, end)) {
71 clock_saved = local_tick_disable();
72 set_clock_comparator(end);
76 local_tick_enable(clock_saved);
77 } while (get_tod_clock_fast() < end);
81 * Waits for 'usecs' microseconds using the TOD clock comparator.
83 void __udelay(unsigned long long usecs)
87 if (!static_branch_likely(&udelay_ready)) {
93 local_irq_save(flags);
95 __udelay_disabled(usecs);
99 if (raw_irqs_disabled_flags(flags))
100 __udelay_disabled(usecs);
102 __udelay_enabled(usecs);
105 if (raw_irqs_disabled_flags(flags)) {
107 __udelay_disabled(usecs);
111 __udelay_enabled(usecs);
113 local_irq_restore(flags);
116 EXPORT_SYMBOL(__udelay);
119 * Simple udelay variant. To be used on startup and reboot
120 * when the interrupt handler isn't working.
122 void udelay_simple(unsigned long long usecs)
126 end = get_tod_clock_fast() + (usecs << 12);
127 while (get_tod_clock_fast() < end)
131 void __ndelay(unsigned long long nsecs)
137 end = get_tod_clock_fast() + nsecs;
138 if (nsecs & ~0xfffUL)
139 __udelay(nsecs >> 12);
140 while (get_tod_clock_fast() < end)
143 EXPORT_SYMBOL(__ndelay);