162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */ 262306a36Sopenharmony_ci/* 362306a36Sopenharmony_ci * Copyright (C) 1995-2004 Russell King 462306a36Sopenharmony_ci * 562306a36Sopenharmony_ci * Delay routines, using a pre-computed "loops_per_second" value. 662306a36Sopenharmony_ci */ 762306a36Sopenharmony_ci#ifndef __ASM_ARM_DELAY_H 862306a36Sopenharmony_ci#define __ASM_ARM_DELAY_H 962306a36Sopenharmony_ci 1062306a36Sopenharmony_ci#include <asm/page.h> 1162306a36Sopenharmony_ci#include <asm/param.h> /* HZ */ 1262306a36Sopenharmony_ci 1362306a36Sopenharmony_ci/* 1462306a36Sopenharmony_ci * Loop (or tick) based delay: 1562306a36Sopenharmony_ci * 1662306a36Sopenharmony_ci * loops = loops_per_jiffy * jiffies_per_sec * delay_us / us_per_sec 1762306a36Sopenharmony_ci * 1862306a36Sopenharmony_ci * where: 1962306a36Sopenharmony_ci * 2062306a36Sopenharmony_ci * jiffies_per_sec = HZ 2162306a36Sopenharmony_ci * us_per_sec = 1000000 2262306a36Sopenharmony_ci * 2362306a36Sopenharmony_ci * Therefore the constant part is HZ / 1000000 which is a small 2462306a36Sopenharmony_ci * fractional number. To make this usable with integer math, we 2562306a36Sopenharmony_ci * scale up this constant by 2^31, perform the actual multiplication, 2662306a36Sopenharmony_ci * and scale the result back down by 2^31 with a simple shift: 2762306a36Sopenharmony_ci * 2862306a36Sopenharmony_ci * loops = (loops_per_jiffy * delay_us * UDELAY_MULT) >> 31 2962306a36Sopenharmony_ci * 3062306a36Sopenharmony_ci * where: 3162306a36Sopenharmony_ci * 3262306a36Sopenharmony_ci * UDELAY_MULT = 2^31 * HZ / 1000000 3362306a36Sopenharmony_ci * = (2^31 / 1000000) * HZ 3462306a36Sopenharmony_ci * = 2147.483648 * HZ 3562306a36Sopenharmony_ci * = 2147 * HZ + 483648 * HZ / 1000000 3662306a36Sopenharmony_ci * 3762306a36Sopenharmony_ci * 31 is the biggest scale shift value that won't overflow 32 bits for 3862306a36Sopenharmony_ci * delay_us * UDELAY_MULT assuming HZ <= 1000 and delay_us <= 2000. 3962306a36Sopenharmony_ci */ 4062306a36Sopenharmony_ci#define MAX_UDELAY_MS 2 4162306a36Sopenharmony_ci#define UDELAY_MULT UL(2147 * HZ + 483648 * HZ / 1000000) 4262306a36Sopenharmony_ci#define UDELAY_SHIFT 31 4362306a36Sopenharmony_ci 4462306a36Sopenharmony_ci#ifndef __ASSEMBLY__ 4562306a36Sopenharmony_ci 4662306a36Sopenharmony_cistruct delay_timer { 4762306a36Sopenharmony_ci unsigned long (*read_current_timer)(void); 4862306a36Sopenharmony_ci unsigned long freq; 4962306a36Sopenharmony_ci}; 5062306a36Sopenharmony_ci 5162306a36Sopenharmony_ciextern struct arm_delay_ops { 5262306a36Sopenharmony_ci void (*delay)(unsigned long); 5362306a36Sopenharmony_ci void (*const_udelay)(unsigned long); 5462306a36Sopenharmony_ci void (*udelay)(unsigned long); 5562306a36Sopenharmony_ci unsigned long ticks_per_jiffy; 5662306a36Sopenharmony_ci} arm_delay_ops; 5762306a36Sopenharmony_ci 5862306a36Sopenharmony_ci#define __delay(n) arm_delay_ops.delay(n) 5962306a36Sopenharmony_ci 6062306a36Sopenharmony_ci/* 6162306a36Sopenharmony_ci * This function intentionally does not exist; if you see references to 6262306a36Sopenharmony_ci * it, it means that you're calling udelay() with an out of range value. 6362306a36Sopenharmony_ci * 6462306a36Sopenharmony_ci * With currently imposed limits, this means that we support a max delay 6562306a36Sopenharmony_ci * of 2000us. Further limits: HZ<=1000 6662306a36Sopenharmony_ci */ 6762306a36Sopenharmony_ciextern void __bad_udelay(void); 6862306a36Sopenharmony_ci 6962306a36Sopenharmony_ci/* 7062306a36Sopenharmony_ci * division by multiplication: you don't have to worry about 7162306a36Sopenharmony_ci * loss of precision. 7262306a36Sopenharmony_ci * 7362306a36Sopenharmony_ci * Use only for very small delays ( < 2 msec). Should probably use a 7462306a36Sopenharmony_ci * lookup table, really, as the multiplications take much too long with 7562306a36Sopenharmony_ci * short delays. This is a "reasonable" implementation, though (and the 7662306a36Sopenharmony_ci * first constant multiplications gets optimized away if the delay is 7762306a36Sopenharmony_ci * a constant) 7862306a36Sopenharmony_ci */ 7962306a36Sopenharmony_ci#define __udelay(n) arm_delay_ops.udelay(n) 8062306a36Sopenharmony_ci#define __const_udelay(n) arm_delay_ops.const_udelay(n) 8162306a36Sopenharmony_ci 8262306a36Sopenharmony_ci#define udelay(n) \ 8362306a36Sopenharmony_ci (__builtin_constant_p(n) ? \ 8462306a36Sopenharmony_ci ((n) > (MAX_UDELAY_MS * 1000) ? __bad_udelay() : \ 8562306a36Sopenharmony_ci __const_udelay((n) * UDELAY_MULT)) : \ 8662306a36Sopenharmony_ci __udelay(n)) 8762306a36Sopenharmony_ci 8862306a36Sopenharmony_ci/* Loop-based definitions for assembly code. */ 8962306a36Sopenharmony_ciextern void __loop_delay(unsigned long loops); 9062306a36Sopenharmony_ciextern void __loop_udelay(unsigned long usecs); 9162306a36Sopenharmony_ciextern void __loop_const_udelay(unsigned long); 9262306a36Sopenharmony_ci 9362306a36Sopenharmony_ci/* Delay-loop timer registration. */ 9462306a36Sopenharmony_ci#define ARCH_HAS_READ_CURRENT_TIMER 9562306a36Sopenharmony_ciextern void register_current_timer_delay(const struct delay_timer *timer); 9662306a36Sopenharmony_ci 9762306a36Sopenharmony_ci#endif /* __ASSEMBLY__ */ 9862306a36Sopenharmony_ci 9962306a36Sopenharmony_ci#endif /* defined(_ARM_DELAY_H) */ 10062306a36Sopenharmony_ci 101