18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright (C) 2020 Loongson Technology Corporation Limited
48c2ecf20Sopenharmony_ci */
58c2ecf20Sopenharmony_ci#include <linux/delay.h>
68c2ecf20Sopenharmony_ci#include <linux/export.h>
78c2ecf20Sopenharmony_ci#include <linux/smp.h>
88c2ecf20Sopenharmony_ci#include <linux/timex.h>
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ci#include <asm/processor.h>
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_civoid __delay(unsigned long cycles)
138c2ecf20Sopenharmony_ci{
148c2ecf20Sopenharmony_ci	u64 t0 = get_cycles();
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_ci	while ((unsigned long)(get_cycles() - t0) < cycles)
178c2ecf20Sopenharmony_ci		cpu_relax();
188c2ecf20Sopenharmony_ci}
198c2ecf20Sopenharmony_ciEXPORT_SYMBOL(__delay);
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_ci/*
228c2ecf20Sopenharmony_ci * Division by multiplication: you don't have to worry about
238c2ecf20Sopenharmony_ci * loss of precision.
248c2ecf20Sopenharmony_ci *
258c2ecf20Sopenharmony_ci * Use only for very small delays ( < 1 msec).	Should probably use a
268c2ecf20Sopenharmony_ci * lookup table, really, as the multiplications take much too long with
278c2ecf20Sopenharmony_ci * short delays.  This is a "reasonable" implementation, though (and the
288c2ecf20Sopenharmony_ci * first constant multiplications gets optimized away if the delay is
298c2ecf20Sopenharmony_ci * a constant)
308c2ecf20Sopenharmony_ci */
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_civoid __udelay(unsigned long us)
338c2ecf20Sopenharmony_ci{
348c2ecf20Sopenharmony_ci	__delay((us * 0x000010c7ull * HZ * lpj_fine) >> 32);
358c2ecf20Sopenharmony_ci}
368c2ecf20Sopenharmony_ciEXPORT_SYMBOL(__udelay);
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_civoid __ndelay(unsigned long ns)
398c2ecf20Sopenharmony_ci{
408c2ecf20Sopenharmony_ci	__delay((ns * 0x00000005ull * HZ * lpj_fine) >> 32);
418c2ecf20Sopenharmony_ci}
428c2ecf20Sopenharmony_ciEXPORT_SYMBOL(__ndelay);
43