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> |
Heiko Carstens | db7e007 | 2015-08-15 11:42:21 +0200 | [diff] [blame] | 15 | #include <linux/irq.h> |
Martin Schwidefsky | 27f6b41 | 2012-07-20 11:15:08 +0200 | [diff] [blame] | 16 | #include <asm/vtimer.h> |
Heiko Carstens | b396637 | 2011-05-26 09:48:28 +0200 | [diff] [blame] | 17 | #include <asm/div64.h> |
Heiko Carstens | db7e007 | 2015-08-15 11:42:21 +0200 | [diff] [blame] | 18 | #include <asm/idle.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 | } |
Guenter Roeck | 854508c | 2015-08-04 23:38:23 -0700 | [diff] [blame] | 31 | EXPORT_SYMBOL(__delay); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | |
Heiko Carstens | 0cd6a40 | 2009-10-06 10:34:05 +0200 | [diff] [blame] | 33 | static void __udelay_disabled(unsigned long long usecs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | { |
Heiko Carstens | db7e007 | 2015-08-15 11:42:21 +0200 | [diff] [blame] | 35 | unsigned long cr0, cr0_new, psw_mask; |
| 36 | struct s390_idle_data idle; |
| 37 | u64 end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | |
Heiko Carstens | 1aae056 | 2013-01-30 09:49:40 +0100 | [diff] [blame] | 39 | end = get_tod_clock() + (usecs << 12); |
Martin Schwidefsky | 4c1051e | 2012-03-11 11:59:27 -0400 | [diff] [blame] | 40 | __ctl_store(cr0, 0, 0); |
Heiko Carstens | db7e007 | 2015-08-15 11:42:21 +0200 | [diff] [blame] | 41 | cr0_new = cr0 & ~CR0_IRQ_SUBCLASS_MASK; |
| 42 | cr0_new |= (1UL << (63 - 52)); /* enable clock comparator irq */ |
| 43 | __ctl_load(cr0_new, 0, 0); |
| 44 | psw_mask = __extract_psw() | PSW_MASK_EXT | PSW_MASK_WAIT; |
| 45 | set_clock_comparator(end); |
| 46 | set_cpu_flag(CIF_IGNORE_IRQ); |
| 47 | psw_idle(&idle, psw_mask); |
| 48 | clear_cpu_flag(CIF_IGNORE_IRQ); |
| 49 | set_clock_comparator(S390_lowcore.clock_comparator); |
Martin Schwidefsky | 4c1051e | 2012-03-11 11:59:27 -0400 | [diff] [blame] | 50 | __ctl_load(cr0, 0, 0); |
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 | { |
Martin Schwidefsky | 4c1051e | 2012-03-11 11:59:27 -0400 | [diff] [blame] | 55 | u64 clock_saved, end; |
Heiko Carstens | d3d238c | 2008-10-03 21:54:59 +0200 | [diff] [blame] | 56 | |
Martin Schwidefsky | 8c071b0 | 2013-10-17 12:38:17 +0200 | [diff] [blame] | 57 | end = get_tod_clock_fast() + (usecs << 12); |
Martin Schwidefsky | d54853e | 2007-02-05 21:18:19 +0100 | [diff] [blame] | 58 | do { |
Christian Borntraeger | 78d81f2 | 2009-10-06 10:34:04 +0200 | [diff] [blame] | 59 | clock_saved = 0; |
| 60 | if (end < S390_lowcore.clock_comparator) { |
| 61 | clock_saved = local_tick_disable(); |
| 62 | set_clock_comparator(end); |
| 63 | } |
Martin Schwidefsky | b5f87f1 | 2014-10-01 10:57:57 +0200 | [diff] [blame] | 64 | enabled_wait(); |
Christian Borntraeger | 78d81f2 | 2009-10-06 10:34:04 +0200 | [diff] [blame] | 65 | if (clock_saved) |
| 66 | local_tick_enable(clock_saved); |
Martin Schwidefsky | 8c071b0 | 2013-10-17 12:38:17 +0200 | [diff] [blame] | 67 | } while (get_tod_clock_fast() < end); |
Heiko Carstens | d3d238c | 2008-10-03 21:54:59 +0200 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | /* |
| 71 | * Waits for 'usecs' microseconds using the TOD clock comparator. |
| 72 | */ |
Heiko Carstens | 0cd6a40 | 2009-10-06 10:34:05 +0200 | [diff] [blame] | 73 | void __udelay(unsigned long long usecs) |
Heiko Carstens | d3d238c | 2008-10-03 21:54:59 +0200 | [diff] [blame] | 74 | { |
| 75 | unsigned long flags; |
| 76 | |
| 77 | preempt_disable(); |
| 78 | local_irq_save(flags); |
| 79 | if (in_irq()) { |
| 80 | __udelay_disabled(usecs); |
| 81 | goto out; |
| 82 | } |
| 83 | if (in_softirq()) { |
| 84 | if (raw_irqs_disabled_flags(flags)) |
| 85 | __udelay_disabled(usecs); |
| 86 | else |
| 87 | __udelay_enabled(usecs); |
| 88 | goto out; |
| 89 | } |
| 90 | if (raw_irqs_disabled_flags(flags)) { |
| 91 | local_bh_disable(); |
| 92 | __udelay_disabled(usecs); |
| 93 | _local_bh_enable(); |
| 94 | goto out; |
| 95 | } |
| 96 | __udelay_enabled(usecs); |
| 97 | out: |
Martin Schwidefsky | d54853e | 2007-02-05 21:18:19 +0100 | [diff] [blame] | 98 | local_irq_restore(flags); |
Heiko Carstens | d3d238c | 2008-10-03 21:54:59 +0200 | [diff] [blame] | 99 | preempt_enable(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | } |
Heiko Carstens | 1485c5c | 2009-03-26 15:24:04 +0100 | [diff] [blame] | 101 | EXPORT_SYMBOL(__udelay); |
Heiko Carstens | 5a0d0e6 | 2008-10-10 21:33:22 +0200 | [diff] [blame] | 102 | |
| 103 | /* |
| 104 | * Simple udelay variant. To be used on startup and reboot |
| 105 | * when the interrupt handler isn't working. |
| 106 | */ |
Heiko Carstens | 0cd6a40 | 2009-10-06 10:34:05 +0200 | [diff] [blame] | 107 | void udelay_simple(unsigned long long usecs) |
Heiko Carstens | 5a0d0e6 | 2008-10-10 21:33:22 +0200 | [diff] [blame] | 108 | { |
| 109 | u64 end; |
| 110 | |
Martin Schwidefsky | 8c071b0 | 2013-10-17 12:38:17 +0200 | [diff] [blame] | 111 | end = get_tod_clock_fast() + (usecs << 12); |
| 112 | while (get_tod_clock_fast() < end) |
Heiko Carstens | 5a0d0e6 | 2008-10-10 21:33:22 +0200 | [diff] [blame] | 113 | cpu_relax(); |
| 114 | } |
Heiko Carstens | b396637 | 2011-05-26 09:48:28 +0200 | [diff] [blame] | 115 | |
| 116 | void __ndelay(unsigned long long nsecs) |
| 117 | { |
| 118 | u64 end; |
| 119 | |
| 120 | nsecs <<= 9; |
| 121 | do_div(nsecs, 125); |
Martin Schwidefsky | 8c071b0 | 2013-10-17 12:38:17 +0200 | [diff] [blame] | 122 | end = get_tod_clock_fast() + nsecs; |
Heiko Carstens | b396637 | 2011-05-26 09:48:28 +0200 | [diff] [blame] | 123 | if (nsecs & ~0xfffUL) |
| 124 | __udelay(nsecs >> 12); |
Martin Schwidefsky | 8c071b0 | 2013-10-17 12:38:17 +0200 | [diff] [blame] | 125 | while (get_tod_clock_fast() < end) |
Heiko Carstens | b396637 | 2011-05-26 09:48:28 +0200 | [diff] [blame] | 126 | barrier(); |
| 127 | } |
| 128 | EXPORT_SYMBOL(__ndelay); |