Paul Mackerras | c55377e | 2005-11-14 17:22:01 +1100 | [diff] [blame] | 1 | #ifndef _ASM_POWERPC_DELAY_H |
| 2 | #define _ASM_POWERPC_DELAY_H |
Arnd Bergmann | 88ced03 | 2005-12-16 22:43:46 +0100 | [diff] [blame] | 3 | #ifdef __KERNEL__ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | |
Nicholas Piggin | 4e287e6 | 2017-06-06 23:08:32 +1000 | [diff] [blame] | 5 | #include <linux/processor.h> |
Timur Tabi | 9317726 | 2009-05-26 05:21:41 +0000 | [diff] [blame] | 6 | #include <asm/time.h> |
| 7 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | /* |
| 9 | * Copyright 1996, Paul Mackerras. |
Timur Tabi | 9317726 | 2009-05-26 05:21:41 +0000 | [diff] [blame] | 10 | * Copyright (C) 2009 Freescale Semiconductor, Inc. All rights reserved. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | * |
| 12 | * This program is free software; you can redistribute it and/or |
| 13 | * modify it under the terms of the GNU General Public License |
| 14 | * as published by the Free Software Foundation; either version |
| 15 | * 2 of the License, or (at your option) any later version. |
| 16 | * |
| 17 | * PPC64 Support added by Dave Engebretsen, Todd Inglett, Mike Corrigan, |
| 18 | * Anton Blanchard. |
| 19 | */ |
| 20 | |
Paul Mackerras | 6defa38 | 2005-11-18 13:44:17 +1100 | [diff] [blame] | 21 | extern void __delay(unsigned long loops); |
| 22 | extern void udelay(unsigned long usecs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | |
Anton Blanchard | 1e92a55 | 2006-06-15 14:11:22 +1000 | [diff] [blame] | 24 | /* |
| 25 | * On shared processor machines the generic implementation of mdelay can |
| 26 | * result in large errors. While each iteration of the loop inside mdelay |
| 27 | * is supposed to take 1ms, the hypervisor could sleep our partition for |
| 28 | * longer (eg 10ms). With the right timing these errors can add up. |
| 29 | * |
| 30 | * Since there is no 32bit overflow issue on 64bit kernels, just call |
| 31 | * udelay directly. |
| 32 | */ |
| 33 | #ifdef CONFIG_PPC64 |
| 34 | #define mdelay(n) udelay((n) * 1000) |
| 35 | #endif |
| 36 | |
Timur Tabi | 9317726 | 2009-05-26 05:21:41 +0000 | [diff] [blame] | 37 | /** |
| 38 | * spin_event_timeout - spin until a condition gets true or a timeout elapses |
| 39 | * @condition: a C expression to evalate |
| 40 | * @timeout: timeout, in microseconds |
| 41 | * @delay: the number of microseconds to delay between each evaluation of |
| 42 | * @condition |
| 43 | * |
| 44 | * The process spins until the condition evaluates to true (non-zero) or the |
| 45 | * timeout elapses. The return value of this macro is the value of |
| 46 | * @condition when the loop terminates. This allows you to determine the cause |
| 47 | * of the loop terminates. If the return value is zero, then you know a |
| 48 | * timeout has occurred. |
| 49 | * |
| 50 | * This primary purpose of this macro is to poll on a hardware register |
| 51 | * until a status bit changes. The timeout ensures that the loop still |
| 52 | * terminates even if the bit never changes. The delay is for devices that |
| 53 | * need a delay in between successive reads. |
| 54 | * |
| 55 | * gcc will optimize out the if-statement if @delay is a constant. |
| 56 | */ |
| 57 | #define spin_event_timeout(condition, timeout, delay) \ |
| 58 | ({ \ |
| 59 | typeof(condition) __ret; \ |
| 60 | unsigned long __loops = tb_ticks_per_usec * timeout; \ |
| 61 | unsigned long __start = get_tbl(); \ |
Nicholas Piggin | 4e287e6 | 2017-06-06 23:08:32 +1000 | [diff] [blame] | 62 | \ |
| 63 | if (delay) { \ |
| 64 | while (!(__ret = (condition)) && \ |
| 65 | (tb_ticks_since(__start) <= __loops)) \ |
Timur Tabi | 9317726 | 2009-05-26 05:21:41 +0000 | [diff] [blame] | 66 | udelay(delay); \ |
Nicholas Piggin | 4e287e6 | 2017-06-06 23:08:32 +1000 | [diff] [blame] | 67 | } else { \ |
| 68 | spin_begin(); \ |
| 69 | while (!(__ret = (condition)) && \ |
| 70 | (tb_ticks_since(__start) <= __loops)) \ |
| 71 | spin_cpu_relax(); \ |
| 72 | spin_end(); \ |
| 73 | } \ |
Grant Likely | ad9064d | 2009-06-29 13:40:51 +0000 | [diff] [blame] | 74 | if (!__ret) \ |
| 75 | __ret = (condition); \ |
Timur Tabi | 9317726 | 2009-05-26 05:21:41 +0000 | [diff] [blame] | 76 | __ret; \ |
| 77 | }) |
| 78 | |
Arnd Bergmann | 88ced03 | 2005-12-16 22:43:46 +0100 | [diff] [blame] | 79 | #endif /* __KERNEL__ */ |
Paul Mackerras | c55377e | 2005-11-14 17:22:01 +1100 | [diff] [blame] | 80 | #endif /* _ASM_POWERPC_DELAY_H */ |