162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-or-later */
262306a36Sopenharmony_ci#ifndef _ASM_POWERPC_DELAY_H
362306a36Sopenharmony_ci#define _ASM_POWERPC_DELAY_H
462306a36Sopenharmony_ci#ifdef __KERNEL__
562306a36Sopenharmony_ci
662306a36Sopenharmony_ci#include <linux/processor.h>
762306a36Sopenharmony_ci#include <asm/time.h>
862306a36Sopenharmony_ci
962306a36Sopenharmony_ci/*
1062306a36Sopenharmony_ci * Copyright 1996, Paul Mackerras.
1162306a36Sopenharmony_ci * Copyright (C) 2009 Freescale Semiconductor, Inc. All rights reserved.
1262306a36Sopenharmony_ci *
1362306a36Sopenharmony_ci * PPC64 Support added by Dave Engebretsen, Todd Inglett, Mike Corrigan,
1462306a36Sopenharmony_ci * Anton Blanchard.
1562306a36Sopenharmony_ci */
1662306a36Sopenharmony_ci
1762306a36Sopenharmony_ciextern void __delay(unsigned long loops);
1862306a36Sopenharmony_ciextern void udelay(unsigned long usecs);
1962306a36Sopenharmony_ci
2062306a36Sopenharmony_ci/*
2162306a36Sopenharmony_ci * On shared processor machines the generic implementation of mdelay can
2262306a36Sopenharmony_ci * result in large errors. While each iteration of the loop inside mdelay
2362306a36Sopenharmony_ci * is supposed to take 1ms, the hypervisor could sleep our partition for
2462306a36Sopenharmony_ci * longer (eg 10ms). With the right timing these errors can add up.
2562306a36Sopenharmony_ci *
2662306a36Sopenharmony_ci * Since there is no 32bit overflow issue on 64bit kernels, just call
2762306a36Sopenharmony_ci * udelay directly.
2862306a36Sopenharmony_ci */
2962306a36Sopenharmony_ci#ifdef CONFIG_PPC64
3062306a36Sopenharmony_ci#define mdelay(n)	udelay((n) * 1000)
3162306a36Sopenharmony_ci#endif
3262306a36Sopenharmony_ci
3362306a36Sopenharmony_ci/**
3462306a36Sopenharmony_ci * spin_event_timeout - spin until a condition gets true or a timeout elapses
3562306a36Sopenharmony_ci * @condition: a C expression to evalate
3662306a36Sopenharmony_ci * @timeout: timeout, in microseconds
3762306a36Sopenharmony_ci * @delay: the number of microseconds to delay between each evaluation of
3862306a36Sopenharmony_ci *         @condition
3962306a36Sopenharmony_ci *
4062306a36Sopenharmony_ci * The process spins until the condition evaluates to true (non-zero) or the
4162306a36Sopenharmony_ci * timeout elapses.  The return value of this macro is the value of
4262306a36Sopenharmony_ci * @condition when the loop terminates. This allows you to determine the cause
4362306a36Sopenharmony_ci * of the loop terminates.  If the return value is zero, then you know a
4462306a36Sopenharmony_ci * timeout has occurred.
4562306a36Sopenharmony_ci *
4662306a36Sopenharmony_ci * This primary purpose of this macro is to poll on a hardware register
4762306a36Sopenharmony_ci * until a status bit changes.  The timeout ensures that the loop still
4862306a36Sopenharmony_ci * terminates even if the bit never changes.  The delay is for devices that
4962306a36Sopenharmony_ci * need a delay in between successive reads.
5062306a36Sopenharmony_ci *
5162306a36Sopenharmony_ci * gcc will optimize out the if-statement if @delay is a constant.
5262306a36Sopenharmony_ci */
5362306a36Sopenharmony_ci#define spin_event_timeout(condition, timeout, delay)                          \
5462306a36Sopenharmony_ci({                                                                             \
5562306a36Sopenharmony_ci	typeof(condition) __ret;                                               \
5662306a36Sopenharmony_ci	unsigned long __loops = tb_ticks_per_usec * timeout;                   \
5762306a36Sopenharmony_ci	unsigned long __start = mftb();                                     \
5862306a36Sopenharmony_ci                                                                               \
5962306a36Sopenharmony_ci	if (delay) {                                                           \
6062306a36Sopenharmony_ci		while (!(__ret = (condition)) &&                               \
6162306a36Sopenharmony_ci				(tb_ticks_since(__start) <= __loops))          \
6262306a36Sopenharmony_ci			udelay(delay);                                         \
6362306a36Sopenharmony_ci	} else {                                                               \
6462306a36Sopenharmony_ci		spin_begin();                                                  \
6562306a36Sopenharmony_ci		while (!(__ret = (condition)) &&                               \
6662306a36Sopenharmony_ci				(tb_ticks_since(__start) <= __loops))          \
6762306a36Sopenharmony_ci			spin_cpu_relax();                                      \
6862306a36Sopenharmony_ci		spin_end();                                                    \
6962306a36Sopenharmony_ci	}                                                                      \
7062306a36Sopenharmony_ci	if (!__ret)                                                            \
7162306a36Sopenharmony_ci		__ret = (condition);                                           \
7262306a36Sopenharmony_ci	__ret;		                                                       \
7362306a36Sopenharmony_ci})
7462306a36Sopenharmony_ci
7562306a36Sopenharmony_ci#endif /* __KERNEL__ */
7662306a36Sopenharmony_ci#endif /* _ASM_POWERPC_DELAY_H */
77