blob: a65229d91c92be055931de197528c67d3403837c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Precise Delay Loops for S390
3 *
Heiko Carstensd3d238c2008-10-03 21:54:59 +02004 * Copyright IBM Corp. 1999,2008
5 * Author(s): Martin Schwidefsky <schwidefsky@de.ibm.com>,
6 * Heiko Carstens <heiko.carstens@de.ibm.com>,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 */
8
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <linux/sched.h>
10#include <linux/delay.h>
Martin Schwidefskyd54853e2007-02-05 21:18:19 +010011#include <linux/timex.h>
Heiko Carstens1485c5c2009-03-26 15:24:04 +010012#include <linux/module.h>
Martin Schwidefskyd54853e2007-02-05 21:18:19 +010013#include <linux/irqflags.h>
Martin Schwidefskybf6f6aa2007-02-21 10:55:00 +010014#include <linux/interrupt.h>
Heiko Carstensb3966372011-05-26 09:48:28 +020015#include <asm/div64.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016
17void __delay(unsigned long loops)
18{
19 /*
20 * To end the bloody studid and useless discussion about the
21 * BogoMips number I took the liberty to define the __delay
22 * function in a way that that resulting BogoMips number will
23 * yield the megahertz number of the cpu. The important function
24 * is udelay and that is done using the tod clock. -- martin.
25 */
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +020026 asm volatile("0: brct %0,0b" : : "d" ((loops/2) + 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -070027}
28
Heiko Carstens0cd6a402009-10-06 10:34:05 +020029static void __udelay_disabled(unsigned long long usecs)
Linus Torvalds1da177e2005-04-16 15:20:36 -070030{
Heiko Carstensd3d238c2008-10-03 21:54:59 +020031 unsigned long mask, cr0, cr0_saved;
32 u64 clock_saved;
Heiko Carstense8129c62010-11-25 09:52:45 +010033 u64 end;
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
Heiko Carstense8129c62010-11-25 09:52:45 +010035 mask = psw_kernel_bits | PSW_MASK_WAIT | PSW_MASK_EXT;
36 end = get_clock() + (usecs << 12);
Heiko Carstensd3d238c2008-10-03 21:54:59 +020037 clock_saved = local_tick_disable();
Heiko Carstensd3d238c2008-10-03 21:54:59 +020038 __ctl_store(cr0_saved, 0, 0);
39 cr0 = (cr0_saved & 0xffff00e0) | 0x00000800;
40 __ctl_load(cr0 , 0, 0);
Heiko Carstensbb8c29c2009-07-07 16:37:05 +020041 lockdep_off();
Heiko Carstense8129c62010-11-25 09:52:45 +010042 do {
43 set_clock_comparator(end);
44 trace_hardirqs_on();
45 __load_psw_mask(mask);
46 local_irq_disable();
47 } while (get_clock() < end);
Heiko Carstensbb8c29c2009-07-07 16:37:05 +020048 lockdep_on();
Heiko Carstensd3d238c2008-10-03 21:54:59 +020049 __ctl_load(cr0_saved, 0, 0);
50 local_tick_enable(clock_saved);
Heiko Carstensd3d238c2008-10-03 21:54:59 +020051}
Martin Schwidefskyd54853e2007-02-05 21:18:19 +010052
Heiko Carstens0cd6a402009-10-06 10:34:05 +020053static void __udelay_enabled(unsigned long long usecs)
Heiko Carstensd3d238c2008-10-03 21:54:59 +020054{
55 unsigned long mask;
Christian Borntraeger78d81f22009-10-06 10:34:04 +020056 u64 clock_saved;
57 u64 end;
Heiko Carstensd3d238c2008-10-03 21:54:59 +020058
59 mask = psw_kernel_bits | PSW_MASK_WAIT | PSW_MASK_EXT | PSW_MASK_IO;
Heiko Carstens0cd6a402009-10-06 10:34:05 +020060 end = get_clock() + (usecs << 12);
Martin Schwidefskyd54853e2007-02-05 21:18:19 +010061 do {
Christian Borntraeger78d81f22009-10-06 10:34:04 +020062 clock_saved = 0;
63 if (end < S390_lowcore.clock_comparator) {
64 clock_saved = local_tick_disable();
65 set_clock_comparator(end);
66 }
Martin Schwidefskyd54853e2007-02-05 21:18:19 +010067 trace_hardirqs_on();
68 __load_psw_mask(mask);
69 local_irq_disable();
Christian Borntraeger78d81f22009-10-06 10:34:04 +020070 if (clock_saved)
71 local_tick_enable(clock_saved);
Martin Schwidefskyd54853e2007-02-05 21:18:19 +010072 } while (get_clock() < end);
Heiko Carstensd3d238c2008-10-03 21:54:59 +020073}
74
75/*
76 * Waits for 'usecs' microseconds using the TOD clock comparator.
77 */
Heiko Carstens0cd6a402009-10-06 10:34:05 +020078void __udelay(unsigned long long usecs)
Heiko Carstensd3d238c2008-10-03 21:54:59 +020079{
80 unsigned long flags;
81
82 preempt_disable();
83 local_irq_save(flags);
84 if (in_irq()) {
85 __udelay_disabled(usecs);
86 goto out;
87 }
88 if (in_softirq()) {
89 if (raw_irqs_disabled_flags(flags))
90 __udelay_disabled(usecs);
91 else
92 __udelay_enabled(usecs);
93 goto out;
94 }
95 if (raw_irqs_disabled_flags(flags)) {
96 local_bh_disable();
97 __udelay_disabled(usecs);
98 _local_bh_enable();
99 goto out;
100 }
101 __udelay_enabled(usecs);
102out:
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100103 local_irq_restore(flags);
Heiko Carstensd3d238c2008-10-03 21:54:59 +0200104 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105}
Heiko Carstens1485c5c2009-03-26 15:24:04 +0100106EXPORT_SYMBOL(__udelay);
Heiko Carstens5a0d0e62008-10-10 21:33:22 +0200107
108/*
109 * Simple udelay variant. To be used on startup and reboot
110 * when the interrupt handler isn't working.
111 */
Heiko Carstens0cd6a402009-10-06 10:34:05 +0200112void udelay_simple(unsigned long long usecs)
Heiko Carstens5a0d0e62008-10-10 21:33:22 +0200113{
114 u64 end;
115
Heiko Carstens0cd6a402009-10-06 10:34:05 +0200116 end = get_clock() + (usecs << 12);
Heiko Carstens5a0d0e62008-10-10 21:33:22 +0200117 while (get_clock() < end)
118 cpu_relax();
119}
Heiko Carstensb3966372011-05-26 09:48:28 +0200120
121void __ndelay(unsigned long long nsecs)
122{
123 u64 end;
124
125 nsecs <<= 9;
126 do_div(nsecs, 125);
127 end = get_clock() + nsecs;
128 if (nsecs & ~0xfffUL)
129 __udelay(nsecs >> 12);
130 while (get_clock() < end)
131 barrier();
132}
133EXPORT_SYMBOL(__ndelay);