blob: 97c1eca83cc24274e796c491f60d5851252de150 [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 Carstensd3d238c2008-10-03 21:54:59 +020028static void __udelay_disabled(unsigned 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();
34 set_clock_comparator(get_clock() + ((u64) usecs << 12));
35 __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 Carstensd3d238c2008-10-03 21:54:59 +020049static void __udelay_enabled(unsigned long usecs)
50{
51 unsigned long mask;
52 u64 end, time;
53
54 mask = psw_kernel_bits | PSW_MASK_WAIT | PSW_MASK_EXT | PSW_MASK_IO;
Martin Schwidefskyd54853e2007-02-05 21:18:19 +010055 end = get_clock() + ((u64) usecs << 12);
56 do {
Heiko Carstens5a62b192008-04-17 07:46:25 +020057 time = end < S390_lowcore.clock_comparator ?
58 end : S390_lowcore.clock_comparator;
Martin Schwidefskyd54853e2007-02-05 21:18:19 +010059 set_clock_comparator(time);
60 trace_hardirqs_on();
61 __load_psw_mask(mask);
62 local_irq_disable();
63 } while (get_clock() < end);
Heiko Carstens5a62b192008-04-17 07:46:25 +020064 set_clock_comparator(S390_lowcore.clock_comparator);
Heiko Carstensd3d238c2008-10-03 21:54:59 +020065}
66
67/*
68 * Waits for 'usecs' microseconds using the TOD clock comparator.
69 */
70void __udelay(unsigned long usecs)
71{
72 unsigned long flags;
73
74 preempt_disable();
75 local_irq_save(flags);
76 if (in_irq()) {
77 __udelay_disabled(usecs);
78 goto out;
79 }
80 if (in_softirq()) {
81 if (raw_irqs_disabled_flags(flags))
82 __udelay_disabled(usecs);
83 else
84 __udelay_enabled(usecs);
85 goto out;
86 }
87 if (raw_irqs_disabled_flags(flags)) {
88 local_bh_disable();
89 __udelay_disabled(usecs);
90 _local_bh_enable();
91 goto out;
92 }
93 __udelay_enabled(usecs);
94out:
Martin Schwidefskyd54853e2007-02-05 21:18:19 +010095 local_irq_restore(flags);
Heiko Carstensd3d238c2008-10-03 21:54:59 +020096 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -070097}
Heiko Carstens1485c5c2009-03-26 15:24:04 +010098EXPORT_SYMBOL(__udelay);
Heiko Carstens5a0d0e62008-10-10 21:33:22 +020099
100/*
101 * Simple udelay variant. To be used on startup and reboot
102 * when the interrupt handler isn't working.
103 */
104void udelay_simple(unsigned long usecs)
105{
106 u64 end;
107
108 end = get_clock() + ((u64) usecs << 12);
109 while (get_clock() < end)
110 cpu_relax();
111}