18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-only */
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright (C) 2004, 2007-2010, 2011-2012 Synopsys, Inc. (www.synopsys.com)
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Delay routines using pre computed loops_per_jiffy value.
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci * vineetg: Feb 2012
88c2ecf20Sopenharmony_ci *  -Rewrote in "C" to avoid dealing with availability of H/w MPY
98c2ecf20Sopenharmony_ci *  -Also reduced the num of MPY operations from 3 to 2
108c2ecf20Sopenharmony_ci *
118c2ecf20Sopenharmony_ci * Amit Bhor: Codito Technologies 2004
128c2ecf20Sopenharmony_ci */
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_ci#ifndef __ASM_ARC_UDELAY_H
158c2ecf20Sopenharmony_ci#define __ASM_ARC_UDELAY_H
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_ci#include <asm-generic/types.h>
188c2ecf20Sopenharmony_ci#include <asm/param.h>		/* HZ */
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_ciextern unsigned long loops_per_jiffy;
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_cistatic inline void __delay(unsigned long loops)
238c2ecf20Sopenharmony_ci{
248c2ecf20Sopenharmony_ci	__asm__ __volatile__(
258c2ecf20Sopenharmony_ci	"	mov lp_count, %0	\n"
268c2ecf20Sopenharmony_ci	"	lp  1f			\n"
278c2ecf20Sopenharmony_ci	"	nop			\n"
288c2ecf20Sopenharmony_ci	"1:				\n"
298c2ecf20Sopenharmony_ci	:
308c2ecf20Sopenharmony_ci        : "r"(loops)
318c2ecf20Sopenharmony_ci        : "lp_count");
328c2ecf20Sopenharmony_ci}
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_ciextern void __bad_udelay(void);
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_ci/*
378c2ecf20Sopenharmony_ci * Normal Math for computing loops in "N" usecs
388c2ecf20Sopenharmony_ci *  -we have precomputed @loops_per_jiffy
398c2ecf20Sopenharmony_ci *  -1 sec has HZ jiffies
408c2ecf20Sopenharmony_ci * loops per "N" usecs = ((loops_per_jiffy * HZ / 1000000) * N)
418c2ecf20Sopenharmony_ci *
428c2ecf20Sopenharmony_ci * Approximate Division by multiplication:
438c2ecf20Sopenharmony_ci *  -Mathematically if we multiply and divide a number by same value the
448c2ecf20Sopenharmony_ci *   result remains unchanged:  In this case, we use 2^32
458c2ecf20Sopenharmony_ci *  -> (loops_per_N_usec * 2^32 ) / 2^32
468c2ecf20Sopenharmony_ci *  -> (((loops_per_jiffy * HZ / 1000000) * N) * 2^32) / 2^32
478c2ecf20Sopenharmony_ci *  -> (loops_per_jiffy * HZ * N * 4295) / 2^32
488c2ecf20Sopenharmony_ci *
498c2ecf20Sopenharmony_ci *  -Divide by 2^32 is very simply right shift by 32
508c2ecf20Sopenharmony_ci *  -We simply need to ensure that the multiply per above eqn happens in
518c2ecf20Sopenharmony_ci *   64-bit precision (if CPU doesn't support it - gcc can emaulate it)
528c2ecf20Sopenharmony_ci */
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_cistatic inline void __udelay(unsigned long usecs)
558c2ecf20Sopenharmony_ci{
568c2ecf20Sopenharmony_ci	unsigned long loops;
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_ci	/* (u64) cast ensures 64 bit MPY - real or emulated
598c2ecf20Sopenharmony_ci	 * HZ * 4295 is pre-evaluated by gcc - hence only 2 mpy ops
608c2ecf20Sopenharmony_ci	 */
618c2ecf20Sopenharmony_ci	loops = ((u64) usecs * 4295 * HZ * loops_per_jiffy) >> 32;
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_ci	__delay(loops);
648c2ecf20Sopenharmony_ci}
658c2ecf20Sopenharmony_ci
668c2ecf20Sopenharmony_ci#define udelay(n) (__builtin_constant_p(n) ? ((n) > 20000 ? __bad_udelay() \
678c2ecf20Sopenharmony_ci				: __udelay(n)) : __udelay(n))
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_ci#endif /* __ASM_ARC_UDELAY_H */
70