delay.c 2.7 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3
/*
 *    Precise Delay Loops for S390
 *
H
Heiko Carstens 已提交
4 5 6
 *    Copyright IBM Corp. 1999,2008
 *    Author(s): Martin Schwidefsky <schwidefsky@de.ibm.com>,
 *		 Heiko Carstens <heiko.carstens@de.ibm.com>,
L
Linus Torvalds 已提交
7 8 9 10
 */

#include <linux/sched.h>
#include <linux/delay.h>
M
Martin Schwidefsky 已提交
11
#include <linux/timex.h>
12
#include <linux/module.h>
M
Martin Schwidefsky 已提交
13
#include <linux/irqflags.h>
14
#include <linux/interrupt.h>
L
Linus Torvalds 已提交
15 16 17 18 19 20 21 22 23 24

void __delay(unsigned long loops)
{
        /*
         * To end the bloody studid and useless discussion about the
         * BogoMips number I took the liberty to define the __delay
         * function in a way that that resulting BogoMips number will
         * yield the megahertz number of the cpu. The important function
         * is udelay and that is done using the tod clock. -- martin.
         */
25
	asm volatile("0: brct %0,0b" : : "d" ((loops/2) + 1));
L
Linus Torvalds 已提交
26 27
}

28
static void __udelay_disabled(unsigned long long usecs)
L
Linus Torvalds 已提交
29
{
H
Heiko Carstens 已提交
30 31
	unsigned long mask, cr0, cr0_saved;
	u64 clock_saved;
32
	u64 end;
M
Martin Schwidefsky 已提交
33

34 35
	mask = psw_kernel_bits | PSW_MASK_WAIT | PSW_MASK_EXT;
	end = get_clock() + (usecs << 12);
H
Heiko Carstens 已提交
36 37 38 39
	clock_saved = local_tick_disable();
	__ctl_store(cr0_saved, 0, 0);
	cr0 = (cr0_saved & 0xffff00e0) | 0x00000800;
	__ctl_load(cr0 , 0, 0);
40
	lockdep_off();
41 42 43 44 45 46
	do {
		set_clock_comparator(end);
		trace_hardirqs_on();
		__load_psw_mask(mask);
		local_irq_disable();
	} while (get_clock() < end);
47
	lockdep_on();
H
Heiko Carstens 已提交
48 49 50
	__ctl_load(cr0_saved, 0, 0);
	local_tick_enable(clock_saved);
}
M
Martin Schwidefsky 已提交
51

52
static void __udelay_enabled(unsigned long long usecs)
H
Heiko Carstens 已提交
53 54
{
	unsigned long mask;
55 56
	u64 clock_saved;
	u64 end;
H
Heiko Carstens 已提交
57 58

	mask = psw_kernel_bits | PSW_MASK_WAIT | PSW_MASK_EXT | PSW_MASK_IO;
59
	end = get_clock() + (usecs << 12);
M
Martin Schwidefsky 已提交
60
	do {
61 62 63 64 65
		clock_saved = 0;
		if (end < S390_lowcore.clock_comparator) {
			clock_saved = local_tick_disable();
			set_clock_comparator(end);
		}
M
Martin Schwidefsky 已提交
66 67 68
		trace_hardirqs_on();
		__load_psw_mask(mask);
		local_irq_disable();
69 70
		if (clock_saved)
			local_tick_enable(clock_saved);
M
Martin Schwidefsky 已提交
71
	} while (get_clock() < end);
H
Heiko Carstens 已提交
72
}
L
Linus Torvalds 已提交
73

H
Heiko Carstens 已提交
74 75 76
/*
 * Waits for 'usecs' microseconds using the TOD clock comparator.
 */
77
void __udelay(unsigned long long usecs)
H
Heiko Carstens 已提交
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
{
	unsigned long flags;

	preempt_disable();
	local_irq_save(flags);
	if (in_irq()) {
		__udelay_disabled(usecs);
		goto out;
	}
	if (in_softirq()) {
		if (raw_irqs_disabled_flags(flags))
			__udelay_disabled(usecs);
		else
			__udelay_enabled(usecs);
		goto out;
M
Martin Schwidefsky 已提交
93
	}
H
Heiko Carstens 已提交
94 95 96
	if (raw_irqs_disabled_flags(flags)) {
		local_bh_disable();
		__udelay_disabled(usecs);
97
		_local_bh_enable();
H
Heiko Carstens 已提交
98 99 100 101
		goto out;
	}
	__udelay_enabled(usecs);
out:
M
Martin Schwidefsky 已提交
102
	local_irq_restore(flags);
H
Heiko Carstens 已提交
103
	preempt_enable();
L
Linus Torvalds 已提交
104
}
105
EXPORT_SYMBOL(__udelay);
106 107 108 109 110

/*
 * Simple udelay variant. To be used on startup and reboot
 * when the interrupt handler isn't working.
 */
111
void udelay_simple(unsigned long long usecs)
112 113 114
{
	u64 end;

115
	end = get_clock() + (usecs << 12);
116 117 118
	while (get_clock() < end)
		cpu_relax();
}