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>,
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 <linux/irq.h>
16 #include <asm/vtimer.h>
17 #include <asm/div64.h>
20 void __delay(unsigned long loops)
23 * To end the bloody studid and useless discussion about the
24 * BogoMips number I took the liberty to define the __delay
25 * function in a way that that resulting BogoMips number will
26 * yield the megahertz number of the cpu. The important function
27 * is udelay and that is done using the tod clock. -- martin.
29 asm volatile("0: brct %0,0b" : : "d" ((loops/2) + 1));
31 EXPORT_SYMBOL(__delay);
33 static void __udelay_disabled(unsigned long long usecs)
35 unsigned long cr0, cr0_new, psw_mask;
36 struct s390_idle_data idle;
39 end = get_tod_clock() + (usecs << 12);
40 __ctl_store(cr0, 0, 0);
41 cr0_new = cr0 & ~CR0_IRQ_SUBCLASS_MASK;
42 cr0_new |= (1UL << (63 - 52)); /* enable clock comparator irq */
43 __ctl_load(cr0_new, 0, 0);
44 psw_mask = __extract_psw() | PSW_MASK_EXT | PSW_MASK_WAIT;
45 set_clock_comparator(end);
46 set_cpu_flag(CIF_IGNORE_IRQ);
47 psw_idle(&idle, psw_mask);
48 clear_cpu_flag(CIF_IGNORE_IRQ);
49 set_clock_comparator(S390_lowcore.clock_comparator);
50 __ctl_load(cr0, 0, 0);
53 static void __udelay_enabled(unsigned long long usecs)
57 end = get_tod_clock_fast() + (usecs << 12);
60 if (end < S390_lowcore.clock_comparator) {
61 clock_saved = local_tick_disable();
62 set_clock_comparator(end);
66 local_tick_enable(clock_saved);
67 } while (get_tod_clock_fast() < end);
71 * Waits for 'usecs' microseconds using the TOD clock comparator.
73 void __udelay(unsigned long long usecs)
78 local_irq_save(flags);
80 __udelay_disabled(usecs);
84 if (raw_irqs_disabled_flags(flags))
85 __udelay_disabled(usecs);
87 __udelay_enabled(usecs);
90 if (raw_irqs_disabled_flags(flags)) {
92 __udelay_disabled(usecs);
96 __udelay_enabled(usecs);
98 local_irq_restore(flags);
101 EXPORT_SYMBOL(__udelay);
104 * Simple udelay variant. To be used on startup and reboot
105 * when the interrupt handler isn't working.
107 void udelay_simple(unsigned long long usecs)
111 end = get_tod_clock_fast() + (usecs << 12);
112 while (get_tod_clock_fast() < end)
116 void __ndelay(unsigned long long nsecs)
122 end = get_tod_clock_fast() + nsecs;
123 if (nsecs & ~0xfffUL)
124 __udelay(nsecs >> 12);
125 while (get_tod_clock_fast() < end)
128 EXPORT_SYMBOL(__ndelay);