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