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