Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | * Precise Delay Loops for S390 |
| 3 | * |
Heiko Carstens | d3d238c | 2008-10-03 21:54:59 +0200 | [diff] [blame] | 4 | * Copyright IBM Corp. 1999,2008 |
| 5 | * Author(s): Martin Schwidefsky <schwidefsky@de.ibm.com>, |
| 6 | * Heiko Carstens <heiko.carstens@de.ibm.com>, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | */ |
| 8 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | #include <linux/sched.h> |
| 10 | #include <linux/delay.h> |
Martin Schwidefsky | d54853e | 2007-02-05 21:18:19 +0100 | [diff] [blame] | 11 | #include <linux/timex.h> |
Heiko Carstens | 1485c5c | 2009-03-26 15:24:04 +0100 | [diff] [blame] | 12 | #include <linux/module.h> |
Martin Schwidefsky | d54853e | 2007-02-05 21:18:19 +0100 | [diff] [blame] | 13 | #include <linux/irqflags.h> |
Martin Schwidefsky | bf6f6aa | 2007-02-21 10:55:00 +0100 | [diff] [blame] | 14 | #include <linux/interrupt.h> |
Heiko Carstens | b396637 | 2011-05-26 09:48:28 +0200 | [diff] [blame] | 15 | #include <asm/div64.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | |
| 17 | void __delay(unsigned long loops) |
| 18 | { |
| 19 | /* |
| 20 | * To end the bloody studid and useless discussion about the |
| 21 | * BogoMips number I took the liberty to define the __delay |
| 22 | * function in a way that that resulting BogoMips number will |
| 23 | * yield the megahertz number of the cpu. The important function |
| 24 | * is udelay and that is done using the tod clock. -- martin. |
| 25 | */ |
Martin Schwidefsky | 94c12cc | 2006-09-28 16:56:43 +0200 | [diff] [blame] | 26 | asm volatile("0: brct %0,0b" : : "d" ((loops/2) + 1)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | } |
| 28 | |
Heiko Carstens | 0cd6a40 | 2009-10-06 10:34:05 +0200 | [diff] [blame] | 29 | static void __udelay_disabled(unsigned long long usecs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | { |
Heiko Carstens | d3d238c | 2008-10-03 21:54:59 +0200 | [diff] [blame] | 31 | unsigned long mask, cr0, cr0_saved; |
| 32 | u64 clock_saved; |
Heiko Carstens | e8129c6 | 2010-11-25 09:52:45 +0100 | [diff] [blame] | 33 | u64 end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | |
Heiko Carstens | e8129c6 | 2010-11-25 09:52:45 +0100 | [diff] [blame] | 35 | mask = psw_kernel_bits | PSW_MASK_WAIT | PSW_MASK_EXT; |
| 36 | end = get_clock() + (usecs << 12); |
Heiko Carstens | d3d238c | 2008-10-03 21:54:59 +0200 | [diff] [blame] | 37 | clock_saved = local_tick_disable(); |
Heiko Carstens | d3d238c | 2008-10-03 21:54:59 +0200 | [diff] [blame] | 38 | __ctl_store(cr0_saved, 0, 0); |
| 39 | cr0 = (cr0_saved & 0xffff00e0) | 0x00000800; |
| 40 | __ctl_load(cr0 , 0, 0); |
Heiko Carstens | bb8c29c | 2009-07-07 16:37:05 +0200 | [diff] [blame] | 41 | lockdep_off(); |
Heiko Carstens | e8129c6 | 2010-11-25 09:52:45 +0100 | [diff] [blame] | 42 | do { |
| 43 | set_clock_comparator(end); |
| 44 | trace_hardirqs_on(); |
| 45 | __load_psw_mask(mask); |
| 46 | local_irq_disable(); |
| 47 | } while (get_clock() < end); |
Heiko Carstens | bb8c29c | 2009-07-07 16:37:05 +0200 | [diff] [blame] | 48 | lockdep_on(); |
Heiko Carstens | d3d238c | 2008-10-03 21:54:59 +0200 | [diff] [blame] | 49 | __ctl_load(cr0_saved, 0, 0); |
| 50 | local_tick_enable(clock_saved); |
Heiko Carstens | d3d238c | 2008-10-03 21:54:59 +0200 | [diff] [blame] | 51 | } |
Martin Schwidefsky | d54853e | 2007-02-05 21:18:19 +0100 | [diff] [blame] | 52 | |
Heiko Carstens | 0cd6a40 | 2009-10-06 10:34:05 +0200 | [diff] [blame] | 53 | static void __udelay_enabled(unsigned long long usecs) |
Heiko Carstens | d3d238c | 2008-10-03 21:54:59 +0200 | [diff] [blame] | 54 | { |
| 55 | unsigned long mask; |
Christian Borntraeger | 78d81f2 | 2009-10-06 10:34:04 +0200 | [diff] [blame] | 56 | u64 clock_saved; |
| 57 | u64 end; |
Heiko Carstens | d3d238c | 2008-10-03 21:54:59 +0200 | [diff] [blame] | 58 | |
| 59 | mask = psw_kernel_bits | PSW_MASK_WAIT | PSW_MASK_EXT | PSW_MASK_IO; |
Heiko Carstens | 0cd6a40 | 2009-10-06 10:34:05 +0200 | [diff] [blame] | 60 | end = get_clock() + (usecs << 12); |
Martin Schwidefsky | d54853e | 2007-02-05 21:18:19 +0100 | [diff] [blame] | 61 | do { |
Christian Borntraeger | 78d81f2 | 2009-10-06 10:34:04 +0200 | [diff] [blame] | 62 | clock_saved = 0; |
| 63 | if (end < S390_lowcore.clock_comparator) { |
| 64 | clock_saved = local_tick_disable(); |
| 65 | set_clock_comparator(end); |
| 66 | } |
Martin Schwidefsky | d54853e | 2007-02-05 21:18:19 +0100 | [diff] [blame] | 67 | trace_hardirqs_on(); |
| 68 | __load_psw_mask(mask); |
| 69 | local_irq_disable(); |
Christian Borntraeger | 78d81f2 | 2009-10-06 10:34:04 +0200 | [diff] [blame] | 70 | if (clock_saved) |
| 71 | local_tick_enable(clock_saved); |
Martin Schwidefsky | d54853e | 2007-02-05 21:18:19 +0100 | [diff] [blame] | 72 | } while (get_clock() < end); |
Heiko Carstens | d3d238c | 2008-10-03 21:54:59 +0200 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | /* |
| 76 | * Waits for 'usecs' microseconds using the TOD clock comparator. |
| 77 | */ |
Heiko Carstens | 0cd6a40 | 2009-10-06 10:34:05 +0200 | [diff] [blame] | 78 | void __udelay(unsigned long long usecs) |
Heiko Carstens | d3d238c | 2008-10-03 21:54:59 +0200 | [diff] [blame] | 79 | { |
| 80 | unsigned long flags; |
| 81 | |
| 82 | preempt_disable(); |
| 83 | local_irq_save(flags); |
| 84 | if (in_irq()) { |
| 85 | __udelay_disabled(usecs); |
| 86 | goto out; |
| 87 | } |
| 88 | if (in_softirq()) { |
| 89 | if (raw_irqs_disabled_flags(flags)) |
| 90 | __udelay_disabled(usecs); |
| 91 | else |
| 92 | __udelay_enabled(usecs); |
| 93 | goto out; |
| 94 | } |
| 95 | if (raw_irqs_disabled_flags(flags)) { |
| 96 | local_bh_disable(); |
| 97 | __udelay_disabled(usecs); |
| 98 | _local_bh_enable(); |
| 99 | goto out; |
| 100 | } |
| 101 | __udelay_enabled(usecs); |
| 102 | out: |
Martin Schwidefsky | d54853e | 2007-02-05 21:18:19 +0100 | [diff] [blame] | 103 | local_irq_restore(flags); |
Heiko Carstens | d3d238c | 2008-10-03 21:54:59 +0200 | [diff] [blame] | 104 | preempt_enable(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | } |
Heiko Carstens | 1485c5c | 2009-03-26 15:24:04 +0100 | [diff] [blame] | 106 | EXPORT_SYMBOL(__udelay); |
Heiko Carstens | 5a0d0e6 | 2008-10-10 21:33:22 +0200 | [diff] [blame] | 107 | |
| 108 | /* |
| 109 | * Simple udelay variant. To be used on startup and reboot |
| 110 | * when the interrupt handler isn't working. |
| 111 | */ |
Heiko Carstens | 0cd6a40 | 2009-10-06 10:34:05 +0200 | [diff] [blame] | 112 | void udelay_simple(unsigned long long usecs) |
Heiko Carstens | 5a0d0e6 | 2008-10-10 21:33:22 +0200 | [diff] [blame] | 113 | { |
| 114 | u64 end; |
| 115 | |
Heiko Carstens | 0cd6a40 | 2009-10-06 10:34:05 +0200 | [diff] [blame] | 116 | end = get_clock() + (usecs << 12); |
Heiko Carstens | 5a0d0e6 | 2008-10-10 21:33:22 +0200 | [diff] [blame] | 117 | while (get_clock() < end) |
| 118 | cpu_relax(); |
| 119 | } |
Heiko Carstens | b396637 | 2011-05-26 09:48:28 +0200 | [diff] [blame] | 120 | |
| 121 | void __ndelay(unsigned long long nsecs) |
| 122 | { |
| 123 | u64 end; |
| 124 | |
| 125 | nsecs <<= 9; |
| 126 | do_div(nsecs, 125); |
| 127 | end = get_clock() + nsecs; |
| 128 | if (nsecs & ~0xfffUL) |
| 129 | __udelay(nsecs >> 12); |
| 130 | while (get_clock() < end) |
| 131 | barrier(); |
| 132 | } |
| 133 | EXPORT_SYMBOL(__ndelay); |