blob: 752b362bf651d7e1bdf84a3f54601ddf863e8b12 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015
16void __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 */
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +020025 asm volatile("0: brct %0,0b" : : "d" ((loops/2) + 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -070026}
27
Heiko Carstens0cd6a402009-10-06 10:34:05 +020028static void __udelay_disabled(unsigned long long usecs)
Linus Torvalds1da177e2005-04-16 15:20:36 -070029{
Heiko Carstensd3d238c2008-10-03 21:54:59 +020030 unsigned long mask, cr0, cr0_saved;
31 u64 clock_saved;
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
Heiko Carstensd3d238c2008-10-03 21:54:59 +020033 clock_saved = local_tick_disable();
Heiko Carstens0cd6a402009-10-06 10:34:05 +020034 set_clock_comparator(get_clock() + (usecs << 12));
Heiko Carstensd3d238c2008-10-03 21:54:59 +020035 __ctl_store(cr0_saved, 0, 0);
36 cr0 = (cr0_saved & 0xffff00e0) | 0x00000800;
37 __ctl_load(cr0 , 0, 0);
38 mask = psw_kernel_bits | PSW_MASK_WAIT | PSW_MASK_EXT;
Heiko Carstensbb8c29c2009-07-07 16:37:05 +020039 lockdep_off();
Heiko Carstensd3d238c2008-10-03 21:54:59 +020040 trace_hardirqs_on();
41 __load_psw_mask(mask);
42 local_irq_disable();
Heiko Carstensbb8c29c2009-07-07 16:37:05 +020043 lockdep_on();
Heiko Carstensd3d238c2008-10-03 21:54:59 +020044 __ctl_load(cr0_saved, 0, 0);
45 local_tick_enable(clock_saved);
46 set_clock_comparator(S390_lowcore.clock_comparator);
47}
Martin Schwidefskyd54853e2007-02-05 21:18:19 +010048
Heiko Carstens0cd6a402009-10-06 10:34:05 +020049static void __udelay_enabled(unsigned long long usecs)
Heiko Carstensd3d238c2008-10-03 21:54:59 +020050{
51 unsigned long mask;
Christian Borntraeger78d81f22009-10-06 10:34:04 +020052 u64 clock_saved;
53 u64 end;
Heiko Carstensd3d238c2008-10-03 21:54:59 +020054
55 mask = psw_kernel_bits | PSW_MASK_WAIT | PSW_MASK_EXT | PSW_MASK_IO;
Heiko Carstens0cd6a402009-10-06 10:34:05 +020056 end = get_clock() + (usecs << 12);
Martin Schwidefskyd54853e2007-02-05 21:18:19 +010057 do {
Christian Borntraeger78d81f22009-10-06 10:34:04 +020058 clock_saved = 0;
59 if (end < S390_lowcore.clock_comparator) {
60 clock_saved = local_tick_disable();
61 set_clock_comparator(end);
62 }
Martin Schwidefskyd54853e2007-02-05 21:18:19 +010063 trace_hardirqs_on();
64 __load_psw_mask(mask);
65 local_irq_disable();
Christian Borntraeger78d81f22009-10-06 10:34:04 +020066 if (clock_saved)
67 local_tick_enable(clock_saved);
Martin Schwidefskyd54853e2007-02-05 21:18:19 +010068 } while (get_clock() < end);
Heiko Carstens5a62b192008-04-17 07:46:25 +020069 set_clock_comparator(S390_lowcore.clock_comparator);
Heiko Carstensd3d238c2008-10-03 21:54:59 +020070}
71
72/*
73 * Waits for 'usecs' microseconds using the TOD clock comparator.
74 */
Heiko Carstens0cd6a402009-10-06 10:34:05 +020075void __udelay(unsigned long long usecs)
Heiko Carstensd3d238c2008-10-03 21:54:59 +020076{
77 unsigned long flags;
78
79 preempt_disable();
80 local_irq_save(flags);
81 if (in_irq()) {
82 __udelay_disabled(usecs);
83 goto out;
84 }
85 if (in_softirq()) {
86 if (raw_irqs_disabled_flags(flags))
87 __udelay_disabled(usecs);
88 else
89 __udelay_enabled(usecs);
90 goto out;
91 }
92 if (raw_irqs_disabled_flags(flags)) {
93 local_bh_disable();
94 __udelay_disabled(usecs);
95 _local_bh_enable();
96 goto out;
97 }
98 __udelay_enabled(usecs);
99out:
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100100 local_irq_restore(flags);
Heiko Carstensd3d238c2008-10-03 21:54:59 +0200101 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102}
Heiko Carstens1485c5c2009-03-26 15:24:04 +0100103EXPORT_SYMBOL(__udelay);
Heiko Carstens5a0d0e62008-10-10 21:33:22 +0200104
105/*
106 * Simple udelay variant. To be used on startup and reboot
107 * when the interrupt handler isn't working.
108 */
Heiko Carstens0cd6a402009-10-06 10:34:05 +0200109void udelay_simple(unsigned long long usecs)
Heiko Carstens5a0d0e62008-10-10 21:33:22 +0200110{
111 u64 end;
112
Heiko Carstens0cd6a402009-10-06 10:34:05 +0200113 end = get_clock() + (usecs << 12);
Heiko Carstens5a0d0e62008-10-10 21:33:22 +0200114 while (get_clock() < end)
115 cpu_relax();
116}