18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
28c2ecf20Sopenharmony_ci// Copyright (C) 2005-2017 Andes Technology Corporation
38c2ecf20Sopenharmony_ci
48c2ecf20Sopenharmony_ci#ifndef __NDS32_DELAY_H__
58c2ecf20Sopenharmony_ci#define __NDS32_DELAY_H__
68c2ecf20Sopenharmony_ci
78c2ecf20Sopenharmony_ci#include <asm/param.h>
88c2ecf20Sopenharmony_ci
98c2ecf20Sopenharmony_ci/* There is no clocksource cycle counter in the CPU. */
108c2ecf20Sopenharmony_cistatic inline void __delay(unsigned long loops)
118c2ecf20Sopenharmony_ci{
128c2ecf20Sopenharmony_ci	__asm__ __volatile__(".align 2\n"
138c2ecf20Sopenharmony_ci			     "1:\n"
148c2ecf20Sopenharmony_ci			     "\taddi\t%0, %0, -1\n"
158c2ecf20Sopenharmony_ci			     "\tbgtz\t%0, 1b\n"
168c2ecf20Sopenharmony_ci			     :"=r"(loops)
178c2ecf20Sopenharmony_ci			     :"0"(loops));
188c2ecf20Sopenharmony_ci}
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_cistatic inline void __udelay(unsigned long usecs, unsigned long lpj)
218c2ecf20Sopenharmony_ci{
228c2ecf20Sopenharmony_ci	usecs *= (unsigned long)(((0x8000000000000000ULL / (500000 / HZ)) +
238c2ecf20Sopenharmony_ci				  0x80000000ULL) >> 32);
248c2ecf20Sopenharmony_ci	usecs = (unsigned long)(((unsigned long long)usecs * lpj) >> 32);
258c2ecf20Sopenharmony_ci	__delay(usecs);
268c2ecf20Sopenharmony_ci}
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_ci#define udelay(usecs) __udelay((usecs), loops_per_jiffy)
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_ci/* make sure "usecs *= ..." in udelay do not overflow. */
318c2ecf20Sopenharmony_ci#if HZ >= 1000
328c2ecf20Sopenharmony_ci#define MAX_UDELAY_MS	1
338c2ecf20Sopenharmony_ci#elif HZ <= 200
348c2ecf20Sopenharmony_ci#define MAX_UDELAY_MS	5
358c2ecf20Sopenharmony_ci#else
368c2ecf20Sopenharmony_ci#define MAX_UDELAY_MS	(1000 / HZ)
378c2ecf20Sopenharmony_ci#endif
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_ci#endif
40