162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * Copyright (C) 2020-2022 Loongson Technology Corporation Limited
462306a36Sopenharmony_ci */
562306a36Sopenharmony_ci#include <linux/delay.h>
662306a36Sopenharmony_ci#include <linux/export.h>
762306a36Sopenharmony_ci#include <linux/smp.h>
862306a36Sopenharmony_ci#include <linux/timex.h>
962306a36Sopenharmony_ci
1062306a36Sopenharmony_ci#include <asm/processor.h>
1162306a36Sopenharmony_ci
1262306a36Sopenharmony_civoid __delay(unsigned long cycles)
1362306a36Sopenharmony_ci{
1462306a36Sopenharmony_ci	u64 t0 = get_cycles();
1562306a36Sopenharmony_ci
1662306a36Sopenharmony_ci	while ((unsigned long)(get_cycles() - t0) < cycles)
1762306a36Sopenharmony_ci		cpu_relax();
1862306a36Sopenharmony_ci}
1962306a36Sopenharmony_ciEXPORT_SYMBOL(__delay);
2062306a36Sopenharmony_ci
2162306a36Sopenharmony_ci/*
2262306a36Sopenharmony_ci * Division by multiplication: you don't have to worry about
2362306a36Sopenharmony_ci * loss of precision.
2462306a36Sopenharmony_ci *
2562306a36Sopenharmony_ci * Use only for very small delays ( < 1 msec).	Should probably use a
2662306a36Sopenharmony_ci * lookup table, really, as the multiplications take much too long with
2762306a36Sopenharmony_ci * short delays.  This is a "reasonable" implementation, though (and the
2862306a36Sopenharmony_ci * first constant multiplications gets optimized away if the delay is
2962306a36Sopenharmony_ci * a constant)
3062306a36Sopenharmony_ci */
3162306a36Sopenharmony_ci
3262306a36Sopenharmony_civoid __udelay(unsigned long us)
3362306a36Sopenharmony_ci{
3462306a36Sopenharmony_ci	__delay((us * 0x000010c7ull * HZ * lpj_fine) >> 32);
3562306a36Sopenharmony_ci}
3662306a36Sopenharmony_ciEXPORT_SYMBOL(__udelay);
3762306a36Sopenharmony_ci
3862306a36Sopenharmony_civoid __ndelay(unsigned long ns)
3962306a36Sopenharmony_ci{
4062306a36Sopenharmony_ci	__delay((ns * 0x00000005ull * HZ * lpj_fine) >> 32);
4162306a36Sopenharmony_ci}
4262306a36Sopenharmony_ciEXPORT_SYMBOL(__ndelay);
43