xref: /linux/arch/s390/lib/delay.c (revision 96de0e252cedffad61b3cb5e05662c591898e69a)
1 /*
2  *  arch/s390/lib/delay.c
3  *    Precise Delay Loops for S390
4  *
5  *  S390 version
6  *    Copyright (C) 1999 IBM Deutschland Entwicklung GmbH, IBM Corporation
7  *    Author(s): Martin Schwidefsky (schwidefsky@de.ibm.com),
8  *
9  *  Derived from "arch/i386/lib/delay.c"
10  *    Copyright (C) 1993 Linus Torvalds
11  *    Copyright (C) 1997 Martin Mares <mj@atrey.karlin.mff.cuni.cz>
12  */
13 
14 #include <linux/sched.h>
15 #include <linux/delay.h>
16 #include <linux/timex.h>
17 #include <linux/irqflags.h>
18 #include <linux/interrupt.h>
19 
20 void __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          */
29 	asm volatile("0: brct %0,0b" : : "d" ((loops/2) + 1));
30 }
31 
32 /*
33  * Waits for 'usecs' microseconds using the TOD clock comparator.
34  */
35 void __udelay(unsigned long usecs)
36 {
37 	u64 end, time, jiffy_timer = 0;
38 	unsigned long flags, cr0, mask, dummy;
39 	int irq_context;
40 
41 	irq_context = in_interrupt();
42 	if (!irq_context)
43 		local_bh_disable();
44 	local_irq_save(flags);
45 	if (raw_irqs_disabled_flags(flags)) {
46 		jiffy_timer = S390_lowcore.jiffy_timer;
47 		S390_lowcore.jiffy_timer = -1ULL - (4096 << 12);
48 		__ctl_store(cr0, 0, 0);
49 		dummy = (cr0 & 0xffff00e0) | 0x00000800;
50 		__ctl_load(dummy , 0, 0);
51 		mask = psw_kernel_bits | PSW_MASK_WAIT | PSW_MASK_EXT;
52 	} else
53 		mask = psw_kernel_bits | PSW_MASK_WAIT |
54 			PSW_MASK_EXT | PSW_MASK_IO;
55 
56 	end = get_clock() + ((u64) usecs << 12);
57 	do {
58 		time = end < S390_lowcore.jiffy_timer ?
59 			end : S390_lowcore.jiffy_timer;
60 		set_clock_comparator(time);
61 		trace_hardirqs_on();
62 		__load_psw_mask(mask);
63 		local_irq_disable();
64 	} while (get_clock() < end);
65 
66 	if (raw_irqs_disabled_flags(flags)) {
67 		__ctl_load(cr0, 0, 0);
68 		S390_lowcore.jiffy_timer = jiffy_timer;
69 	}
70 	if (!irq_context)
71 		_local_bh_enable();
72 	set_clock_comparator(S390_lowcore.jiffy_timer);
73 	local_irq_restore(flags);
74 }
75