blob: 70f2a862b670649e296d1c6e5277f01241161a11 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Martin Schwidefskyd54853e2007-02-05 21:18:19 +01002 * arch/s390/lib/delay.c
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/sched.h>
15#include <linux/delay.h>
Martin Schwidefskyd54853e2007-02-05 21:18:19 +010016#include <linux/timex.h>
17#include <linux/irqflags.h>
Martin Schwidefskybf6f6aa2007-02-21 10:55:00 +010018#include <linux/interrupt.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}
31
32/*
Martin Schwidefskyd54853e2007-02-05 21:18:19 +010033 * Waits for 'usecs' microseconds using the TOD clock comparator.
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 */
35void __udelay(unsigned long usecs)
36{
Martin Schwidefskyd54853e2007-02-05 21:18:19 +010037 u64 end, time, jiffy_timer = 0;
38 unsigned long flags, cr0, mask, dummy;
Martin Schwidefskybf6f6aa2007-02-21 10:55:00 +010039 int irq_context;
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
Martin Schwidefskybf6f6aa2007-02-21 10:55:00 +010041 irq_context = in_interrupt();
42 if (!irq_context)
43 local_bh_disable();
Martin Schwidefskyd54853e2007-02-05 21:18:19 +010044 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 }
Martin Schwidefskybf6f6aa2007-02-21 10:55:00 +010070 if (!irq_context)
71 _local_bh_enable();
Martin Schwidefskyd54853e2007-02-05 21:18:19 +010072 set_clock_comparator(S390_lowcore.jiffy_timer);
73 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074}