Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Martin Schwidefsky | d54853e | 2007-02-05 21:18:19 +0100 | [diff] [blame] | 2 | * arch/s390/lib/delay.c |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | #include <linux/sched.h> |
| 15 | #include <linux/delay.h> |
Martin Schwidefsky | d54853e | 2007-02-05 21:18:19 +0100 | [diff] [blame] | 16 | #include <linux/timex.h> |
| 17 | #include <linux/irqflags.h> |
Martin Schwidefsky | bf6f6aa | 2007-02-21 10:55:00 +0100 | [diff] [blame] | 18 | #include <linux/interrupt.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | */ |
Martin Schwidefsky | 94c12cc | 2006-09-28 16:56:43 +0200 | [diff] [blame] | 29 | asm volatile("0: brct %0,0b" : : "d" ((loops/2) + 1)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | } |
| 31 | |
| 32 | /* |
Martin Schwidefsky | d54853e | 2007-02-05 21:18:19 +0100 | [diff] [blame] | 33 | * Waits for 'usecs' microseconds using the TOD clock comparator. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | */ |
| 35 | void __udelay(unsigned long usecs) |
| 36 | { |
Heiko Carstens | 5a62b19 | 2008-04-17 07:46:25 +0200 | [diff] [blame] | 37 | u64 end, time, old_cc = 0; |
Martin Schwidefsky | d54853e | 2007-02-05 21:18:19 +0100 | [diff] [blame] | 38 | unsigned long flags, cr0, mask, dummy; |
Martin Schwidefsky | bf6f6aa | 2007-02-21 10:55:00 +0100 | [diff] [blame] | 39 | int irq_context; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | |
Martin Schwidefsky | bf6f6aa | 2007-02-21 10:55:00 +0100 | [diff] [blame] | 41 | irq_context = in_interrupt(); |
| 42 | if (!irq_context) |
| 43 | local_bh_disable(); |
Martin Schwidefsky | d54853e | 2007-02-05 21:18:19 +0100 | [diff] [blame] | 44 | local_irq_save(flags); |
| 45 | if (raw_irqs_disabled_flags(flags)) { |
Heiko Carstens | 5a62b19 | 2008-04-17 07:46:25 +0200 | [diff] [blame] | 46 | old_cc = S390_lowcore.clock_comparator; |
| 47 | S390_lowcore.clock_comparator = -1ULL; |
Martin Schwidefsky | d54853e | 2007-02-05 21:18:19 +0100 | [diff] [blame] | 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 { |
Heiko Carstens | 5a62b19 | 2008-04-17 07:46:25 +0200 | [diff] [blame] | 58 | time = end < S390_lowcore.clock_comparator ? |
| 59 | end : S390_lowcore.clock_comparator; |
Martin Schwidefsky | d54853e | 2007-02-05 21:18:19 +0100 | [diff] [blame] | 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); |
Heiko Carstens | 5a62b19 | 2008-04-17 07:46:25 +0200 | [diff] [blame] | 68 | S390_lowcore.clock_comparator = old_cc; |
Martin Schwidefsky | d54853e | 2007-02-05 21:18:19 +0100 | [diff] [blame] | 69 | } |
Martin Schwidefsky | bf6f6aa | 2007-02-21 10:55:00 +0100 | [diff] [blame] | 70 | if (!irq_context) |
| 71 | _local_bh_enable(); |
Heiko Carstens | 5a62b19 | 2008-04-17 07:46:25 +0200 | [diff] [blame] | 72 | set_clock_comparator(S390_lowcore.clock_comparator); |
Martin Schwidefsky | d54853e | 2007-02-05 21:18:19 +0100 | [diff] [blame] | 73 | local_irq_restore(flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | } |