blob: c61b9fad43cc3928c8f24275e29867badf70a6f9 [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 Carstensa53c8fa2012-07-20 11:15:04 +02004 * Copyright IBM Corp. 1999, 2008
Heiko Carstensd3d238c2008-10-03 21:54:59 +02005 * 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>
Martin Schwidefsky27f6b412012-07-20 11:15:08 +020015#include <asm/vtimer.h>
Heiko Carstensb3966372011-05-26 09:48:28 +020016#include <asm/div64.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017
18void __delay(unsigned long loops)
19{
20 /*
21 * To end the bloody studid and useless discussion about the
22 * BogoMips number I took the liberty to define the __delay
23 * function in a way that that resulting BogoMips number will
24 * yield the megahertz number of the cpu. The important function
25 * is udelay and that is done using the tod clock. -- martin.
26 */
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +020027 asm volatile("0: brct %0,0b" : : "d" ((loops/2) + 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -070028}
29
Heiko Carstens0cd6a402009-10-06 10:34:05 +020030static void __udelay_disabled(unsigned long long usecs)
Linus Torvalds1da177e2005-04-16 15:20:36 -070031{
Martin Schwidefsky4c1051e2012-03-11 11:59:27 -040032 unsigned long cr0, cr6, new;
33 u64 clock_saved, end;
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
Heiko Carstens1aae0562013-01-30 09:49:40 +010035 end = get_tod_clock() + (usecs << 12);
Heiko Carstensd3d238c2008-10-03 21:54:59 +020036 clock_saved = local_tick_disable();
Martin Schwidefsky4c1051e2012-03-11 11:59:27 -040037 __ctl_store(cr0, 0, 0);
38 __ctl_store(cr6, 6, 6);
39 new = (cr0 & 0xffff00e0) | 0x00000800;
40 __ctl_load(new , 0, 0);
41 new = 0;
42 __ctl_load(new, 6, 6);
Heiko Carstensbb8c29c2009-07-07 16:37:05 +020043 lockdep_off();
Heiko Carstense8129c62010-11-25 09:52:45 +010044 do {
45 set_clock_comparator(end);
Martin Schwidefsky4c1051e2012-03-11 11:59:27 -040046 vtime_stop_cpu();
Heiko Carstense8129c62010-11-25 09:52:45 +010047 local_irq_disable();
Heiko Carstens1aae0562013-01-30 09:49:40 +010048 } while (get_tod_clock() < end);
Heiko Carstensbb8c29c2009-07-07 16:37:05 +020049 lockdep_on();
Martin Schwidefsky4c1051e2012-03-11 11:59:27 -040050 __ctl_load(cr0, 0, 0);
51 __ctl_load(cr6, 6, 6);
Heiko Carstensd3d238c2008-10-03 21:54:59 +020052 local_tick_enable(clock_saved);
Heiko Carstensd3d238c2008-10-03 21:54:59 +020053}
Martin Schwidefskyd54853e2007-02-05 21:18:19 +010054
Heiko Carstens0cd6a402009-10-06 10:34:05 +020055static void __udelay_enabled(unsigned long long usecs)
Heiko Carstensd3d238c2008-10-03 21:54:59 +020056{
Martin Schwidefsky4c1051e2012-03-11 11:59:27 -040057 u64 clock_saved, end;
Heiko Carstensd3d238c2008-10-03 21:54:59 +020058
Heiko Carstens1aae0562013-01-30 09:49:40 +010059 end = get_tod_clock() + (usecs << 12);
Martin Schwidefskyd54853e2007-02-05 21:18:19 +010060 do {
Christian Borntraeger78d81f22009-10-06 10:34:04 +020061 clock_saved = 0;
62 if (end < S390_lowcore.clock_comparator) {
63 clock_saved = local_tick_disable();
64 set_clock_comparator(end);
65 }
Martin Schwidefsky4c1051e2012-03-11 11:59:27 -040066 vtime_stop_cpu();
Martin Schwidefskyd54853e2007-02-05 21:18:19 +010067 local_irq_disable();
Christian Borntraeger78d81f22009-10-06 10:34:04 +020068 if (clock_saved)
69 local_tick_enable(clock_saved);
Heiko Carstens1aae0562013-01-30 09:49:40 +010070 } while (get_tod_clock() < end);
Heiko Carstensd3d238c2008-10-03 21:54:59 +020071}
72
73/*
74 * Waits for 'usecs' microseconds using the TOD clock comparator.
75 */
Heiko Carstens0cd6a402009-10-06 10:34:05 +020076void __udelay(unsigned long long usecs)
Heiko Carstensd3d238c2008-10-03 21:54:59 +020077{
78 unsigned long flags;
79
80 preempt_disable();
81 local_irq_save(flags);
82 if (in_irq()) {
83 __udelay_disabled(usecs);
84 goto out;
85 }
86 if (in_softirq()) {
87 if (raw_irqs_disabled_flags(flags))
88 __udelay_disabled(usecs);
89 else
90 __udelay_enabled(usecs);
91 goto out;
92 }
93 if (raw_irqs_disabled_flags(flags)) {
94 local_bh_disable();
95 __udelay_disabled(usecs);
96 _local_bh_enable();
97 goto out;
98 }
99 __udelay_enabled(usecs);
100out:
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100101 local_irq_restore(flags);
Heiko Carstensd3d238c2008-10-03 21:54:59 +0200102 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103}
Heiko Carstens1485c5c2009-03-26 15:24:04 +0100104EXPORT_SYMBOL(__udelay);
Heiko Carstens5a0d0e62008-10-10 21:33:22 +0200105
106/*
107 * Simple udelay variant. To be used on startup and reboot
108 * when the interrupt handler isn't working.
109 */
Heiko Carstens0cd6a402009-10-06 10:34:05 +0200110void udelay_simple(unsigned long long usecs)
Heiko Carstens5a0d0e62008-10-10 21:33:22 +0200111{
112 u64 end;
113
Heiko Carstens1aae0562013-01-30 09:49:40 +0100114 end = get_tod_clock() + (usecs << 12);
115 while (get_tod_clock() < end)
Heiko Carstens5a0d0e62008-10-10 21:33:22 +0200116 cpu_relax();
117}
Heiko Carstensb3966372011-05-26 09:48:28 +0200118
119void __ndelay(unsigned long long nsecs)
120{
121 u64 end;
122
123 nsecs <<= 9;
124 do_div(nsecs, 125);
Heiko Carstens1aae0562013-01-30 09:49:40 +0100125 end = get_tod_clock() + nsecs;
Heiko Carstensb3966372011-05-26 09:48:28 +0200126 if (nsecs & ~0xfffUL)
127 __udelay(nsecs >> 12);
Heiko Carstens1aae0562013-01-30 09:49:40 +0100128 while (get_tod_clock() < end)
Heiko Carstensb3966372011-05-26 09:48:28 +0200129 barrier();
130}
131EXPORT_SYMBOL(__ndelay);