18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright (C) 1995-2004 Russell King
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Delay routines, using a pre-computed "loops_per_second" value.
68c2ecf20Sopenharmony_ci */
78c2ecf20Sopenharmony_ci#ifndef __ASM_ARM_DELAY_H
88c2ecf20Sopenharmony_ci#define __ASM_ARM_DELAY_H
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ci#include <asm/memory.h>
118c2ecf20Sopenharmony_ci#include <asm/param.h>	/* HZ */
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_ci/*
148c2ecf20Sopenharmony_ci * Loop (or tick) based delay:
158c2ecf20Sopenharmony_ci *
168c2ecf20Sopenharmony_ci * loops = loops_per_jiffy * jiffies_per_sec * delay_us / us_per_sec
178c2ecf20Sopenharmony_ci *
188c2ecf20Sopenharmony_ci * where:
198c2ecf20Sopenharmony_ci *
208c2ecf20Sopenharmony_ci * jiffies_per_sec = HZ
218c2ecf20Sopenharmony_ci * us_per_sec = 1000000
228c2ecf20Sopenharmony_ci *
238c2ecf20Sopenharmony_ci * Therefore the constant part is HZ / 1000000 which is a small
248c2ecf20Sopenharmony_ci * fractional number. To make this usable with integer math, we
258c2ecf20Sopenharmony_ci * scale up this constant by 2^31, perform the actual multiplication,
268c2ecf20Sopenharmony_ci * and scale the result back down by 2^31 with a simple shift:
278c2ecf20Sopenharmony_ci *
288c2ecf20Sopenharmony_ci * loops = (loops_per_jiffy * delay_us * UDELAY_MULT) >> 31
298c2ecf20Sopenharmony_ci *
308c2ecf20Sopenharmony_ci * where:
318c2ecf20Sopenharmony_ci *
328c2ecf20Sopenharmony_ci * UDELAY_MULT = 2^31 * HZ / 1000000
338c2ecf20Sopenharmony_ci *             = (2^31 / 1000000) * HZ
348c2ecf20Sopenharmony_ci *             = 2147.483648 * HZ
358c2ecf20Sopenharmony_ci *             = 2147 * HZ + 483648 * HZ / 1000000
368c2ecf20Sopenharmony_ci *
378c2ecf20Sopenharmony_ci * 31 is the biggest scale shift value that won't overflow 32 bits for
388c2ecf20Sopenharmony_ci * delay_us * UDELAY_MULT assuming HZ <= 1000 and delay_us <= 2000.
398c2ecf20Sopenharmony_ci */
408c2ecf20Sopenharmony_ci#define MAX_UDELAY_MS	2
418c2ecf20Sopenharmony_ci#define UDELAY_MULT	UL(2147 * HZ + 483648 * HZ / 1000000)
428c2ecf20Sopenharmony_ci#define UDELAY_SHIFT	31
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_ci#ifndef __ASSEMBLY__
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_cistruct delay_timer {
478c2ecf20Sopenharmony_ci	unsigned long (*read_current_timer)(void);
488c2ecf20Sopenharmony_ci	unsigned long freq;
498c2ecf20Sopenharmony_ci};
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_ciextern struct arm_delay_ops {
528c2ecf20Sopenharmony_ci	void (*delay)(unsigned long);
538c2ecf20Sopenharmony_ci	void (*const_udelay)(unsigned long);
548c2ecf20Sopenharmony_ci	void (*udelay)(unsigned long);
558c2ecf20Sopenharmony_ci	unsigned long ticks_per_jiffy;
568c2ecf20Sopenharmony_ci} arm_delay_ops;
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_ci#define __delay(n)		arm_delay_ops.delay(n)
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_ci/*
618c2ecf20Sopenharmony_ci * This function intentionally does not exist; if you see references to
628c2ecf20Sopenharmony_ci * it, it means that you're calling udelay() with an out of range value.
638c2ecf20Sopenharmony_ci *
648c2ecf20Sopenharmony_ci * With currently imposed limits, this means that we support a max delay
658c2ecf20Sopenharmony_ci * of 2000us. Further limits: HZ<=1000
668c2ecf20Sopenharmony_ci */
678c2ecf20Sopenharmony_ciextern void __bad_udelay(void);
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_ci/*
708c2ecf20Sopenharmony_ci * division by multiplication: you don't have to worry about
718c2ecf20Sopenharmony_ci * loss of precision.
728c2ecf20Sopenharmony_ci *
738c2ecf20Sopenharmony_ci * Use only for very small delays ( < 2 msec).  Should probably use a
748c2ecf20Sopenharmony_ci * lookup table, really, as the multiplications take much too long with
758c2ecf20Sopenharmony_ci * short delays.  This is a "reasonable" implementation, though (and the
768c2ecf20Sopenharmony_ci * first constant multiplications gets optimized away if the delay is
778c2ecf20Sopenharmony_ci * a constant)
788c2ecf20Sopenharmony_ci */
798c2ecf20Sopenharmony_ci#define __udelay(n)		arm_delay_ops.udelay(n)
808c2ecf20Sopenharmony_ci#define __const_udelay(n)	arm_delay_ops.const_udelay(n)
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_ci#define udelay(n)							\
838c2ecf20Sopenharmony_ci	(__builtin_constant_p(n) ?					\
848c2ecf20Sopenharmony_ci	  ((n) > (MAX_UDELAY_MS * 1000) ? __bad_udelay() :		\
858c2ecf20Sopenharmony_ci			__const_udelay((n) * UDELAY_MULT)) :		\
868c2ecf20Sopenharmony_ci	  __udelay(n))
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_ci/* Loop-based definitions for assembly code. */
898c2ecf20Sopenharmony_ciextern void __loop_delay(unsigned long loops);
908c2ecf20Sopenharmony_ciextern void __loop_udelay(unsigned long usecs);
918c2ecf20Sopenharmony_ciextern void __loop_const_udelay(unsigned long);
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_ci/* Delay-loop timer registration. */
948c2ecf20Sopenharmony_ci#define ARCH_HAS_READ_CURRENT_TIMER
958c2ecf20Sopenharmony_ciextern void register_current_timer_delay(const struct delay_timer *timer);
968c2ecf20Sopenharmony_ci
978c2ecf20Sopenharmony_ci#endif /* __ASSEMBLY__ */
988c2ecf20Sopenharmony_ci
998c2ecf20Sopenharmony_ci#endif /* defined(_ARM_DELAY_H) */
1008c2ecf20Sopenharmony_ci
101