blob: 501dcd4ca4a068a5db21eed7284c390346d70546 [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>
Heiko Carstensdb7e0072015-08-15 11:42:21 +020015#include <linux/irq.h>
Martin Schwidefsky27f6b412012-07-20 11:15:08 +020016#include <asm/vtimer.h>
Heiko Carstensb3966372011-05-26 09:48:28 +020017#include <asm/div64.h>
Heiko Carstensdb7e0072015-08-15 11:42:21 +020018#include <asm/idle.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
20void __delay(unsigned long loops)
21{
22 /*
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.
28 */
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +020029 asm volatile("0: brct %0,0b" : : "d" ((loops/2) + 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -070030}
Guenter Roeck854508c2015-08-04 23:38:23 -070031EXPORT_SYMBOL(__delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
Heiko Carstens0cd6a402009-10-06 10:34:05 +020033static void __udelay_disabled(unsigned long long usecs)
Linus Torvalds1da177e2005-04-16 15:20:36 -070034{
Heiko Carstensdb7e0072015-08-15 11:42:21 +020035 unsigned long cr0, cr0_new, psw_mask;
36 struct s390_idle_data idle;
37 u64 end;
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
Heiko Carstens1aae0562013-01-30 09:49:40 +010039 end = get_tod_clock() + (usecs << 12);
Martin Schwidefsky4c1051e2012-03-11 11:59:27 -040040 __ctl_store(cr0, 0, 0);
Heiko Carstensdb7e0072015-08-15 11:42:21 +020041 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);
Martin Schwidefsky4c1051e2012-03-11 11:59:27 -040050 __ctl_load(cr0, 0, 0);
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{
Martin Schwidefsky4c1051e2012-03-11 11:59:27 -040055 u64 clock_saved, end;
Heiko Carstensd3d238c2008-10-03 21:54:59 +020056
Martin Schwidefsky8c071b02013-10-17 12:38:17 +020057 end = get_tod_clock_fast() + (usecs << 12);
Martin Schwidefskyd54853e2007-02-05 21:18:19 +010058 do {
Christian Borntraeger78d81f22009-10-06 10:34:04 +020059 clock_saved = 0;
60 if (end < S390_lowcore.clock_comparator) {
61 clock_saved = local_tick_disable();
62 set_clock_comparator(end);
63 }
Martin Schwidefskyb5f87f12014-10-01 10:57:57 +020064 enabled_wait();
Christian Borntraeger78d81f22009-10-06 10:34:04 +020065 if (clock_saved)
66 local_tick_enable(clock_saved);
Martin Schwidefsky8c071b02013-10-17 12:38:17 +020067 } while (get_tod_clock_fast() < end);
Heiko Carstensd3d238c2008-10-03 21:54:59 +020068}
69
70/*
71 * Waits for 'usecs' microseconds using the TOD clock comparator.
72 */
Heiko Carstens0cd6a402009-10-06 10:34:05 +020073void __udelay(unsigned long long usecs)
Heiko Carstensd3d238c2008-10-03 21:54:59 +020074{
75 unsigned long flags;
76
77 preempt_disable();
78 local_irq_save(flags);
79 if (in_irq()) {
80 __udelay_disabled(usecs);
81 goto out;
82 }
83 if (in_softirq()) {
84 if (raw_irqs_disabled_flags(flags))
85 __udelay_disabled(usecs);
86 else
87 __udelay_enabled(usecs);
88 goto out;
89 }
90 if (raw_irqs_disabled_flags(flags)) {
91 local_bh_disable();
92 __udelay_disabled(usecs);
93 _local_bh_enable();
94 goto out;
95 }
96 __udelay_enabled(usecs);
97out:
Martin Schwidefskyd54853e2007-02-05 21:18:19 +010098 local_irq_restore(flags);
Heiko Carstensd3d238c2008-10-03 21:54:59 +020099 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100}
Heiko Carstens1485c5c2009-03-26 15:24:04 +0100101EXPORT_SYMBOL(__udelay);
Heiko Carstens5a0d0e62008-10-10 21:33:22 +0200102
103/*
104 * Simple udelay variant. To be used on startup and reboot
105 * when the interrupt handler isn't working.
106 */
Heiko Carstens0cd6a402009-10-06 10:34:05 +0200107void udelay_simple(unsigned long long usecs)
Heiko Carstens5a0d0e62008-10-10 21:33:22 +0200108{
109 u64 end;
110
Martin Schwidefsky8c071b02013-10-17 12:38:17 +0200111 end = get_tod_clock_fast() + (usecs << 12);
112 while (get_tod_clock_fast() < end)
Heiko Carstens5a0d0e62008-10-10 21:33:22 +0200113 cpu_relax();
114}
Heiko Carstensb3966372011-05-26 09:48:28 +0200115
116void __ndelay(unsigned long long nsecs)
117{
118 u64 end;
119
120 nsecs <<= 9;
121 do_div(nsecs, 125);
Martin Schwidefsky8c071b02013-10-17 12:38:17 +0200122 end = get_tod_clock_fast() + nsecs;
Heiko Carstensb3966372011-05-26 09:48:28 +0200123 if (nsecs & ~0xfffUL)
124 __udelay(nsecs >> 12);
Martin Schwidefsky8c071b02013-10-17 12:38:17 +0200125 while (get_tod_clock_fast() < end)
Heiko Carstensb3966372011-05-26 09:48:28 +0200126 barrier();
127}
128EXPORT_SYMBOL(__ndelay);