162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
262306a36Sopenharmony_ci#ifndef _M68K_DELAY_H
362306a36Sopenharmony_ci#define _M68K_DELAY_H
462306a36Sopenharmony_ci
562306a36Sopenharmony_ci#include <asm/param.h>
662306a36Sopenharmony_ci
762306a36Sopenharmony_ci/*
862306a36Sopenharmony_ci * Copyright (C) 1994 Hamish Macdonald
962306a36Sopenharmony_ci * Copyright (C) 2004 Greg Ungerer <gerg@uclinux.com>
1062306a36Sopenharmony_ci *
1162306a36Sopenharmony_ci * Delay routines, using a pre-computed "loops_per_jiffy" value.
1262306a36Sopenharmony_ci */
1362306a36Sopenharmony_ci
1462306a36Sopenharmony_ci#if defined(CONFIG_COLDFIRE)
1562306a36Sopenharmony_ci/*
1662306a36Sopenharmony_ci * The ColdFire runs the delay loop at significantly different speeds
1762306a36Sopenharmony_ci * depending upon long word alignment or not.  We'll pad it to
1862306a36Sopenharmony_ci * long word alignment which is the faster version.
1962306a36Sopenharmony_ci * The 0x4a8e is of course a 'tstl %fp' instruction.  This is better
2062306a36Sopenharmony_ci * than using a NOP (0x4e71) instruction because it executes in one
2162306a36Sopenharmony_ci * cycle not three and doesn't allow for an arbitrary delay waiting
2262306a36Sopenharmony_ci * for bus cycles to finish.  Also fp/a6 isn't likely to cause a
2362306a36Sopenharmony_ci * stall waiting for the register to become valid if such is added
2462306a36Sopenharmony_ci * to the coldfire at some stage.
2562306a36Sopenharmony_ci */
2662306a36Sopenharmony_ci#define	DELAY_ALIGN	".balignw 4, 0x4a8e\n\t"
2762306a36Sopenharmony_ci#else
2862306a36Sopenharmony_ci/*
2962306a36Sopenharmony_ci * No instruction alignment required for other m68k types.
3062306a36Sopenharmony_ci */
3162306a36Sopenharmony_ci#define	DELAY_ALIGN
3262306a36Sopenharmony_ci#endif
3362306a36Sopenharmony_ci
3462306a36Sopenharmony_cistatic inline void __delay(unsigned long loops)
3562306a36Sopenharmony_ci{
3662306a36Sopenharmony_ci	__asm__ __volatile__ (
3762306a36Sopenharmony_ci		DELAY_ALIGN
3862306a36Sopenharmony_ci		"1: subql #1,%0\n\t"
3962306a36Sopenharmony_ci		"jcc 1b"
4062306a36Sopenharmony_ci		: "=d" (loops)
4162306a36Sopenharmony_ci		: "0" (loops));
4262306a36Sopenharmony_ci}
4362306a36Sopenharmony_ci
4462306a36Sopenharmony_ciextern void __bad_udelay(void);
4562306a36Sopenharmony_ci
4662306a36Sopenharmony_ci
4762306a36Sopenharmony_ci#ifdef CONFIG_CPU_HAS_NO_MULDIV64
4862306a36Sopenharmony_ci/*
4962306a36Sopenharmony_ci * The simpler m68k and ColdFire processors do not have a 32*32->64
5062306a36Sopenharmony_ci * multiply instruction. So we need to handle them a little differently.
5162306a36Sopenharmony_ci * We use a bit of shifting and a single 32*32->32 multiply to get close.
5262306a36Sopenharmony_ci */
5362306a36Sopenharmony_ci#define	HZSCALE		(268435456 / (1000000 / HZ))
5462306a36Sopenharmony_ci
5562306a36Sopenharmony_ci#define	__const_udelay(u) \
5662306a36Sopenharmony_ci	__delay(((((u) * HZSCALE) >> 11) * (loops_per_jiffy >> 11)) >> 6)
5762306a36Sopenharmony_ci
5862306a36Sopenharmony_ci#else
5962306a36Sopenharmony_ci
6062306a36Sopenharmony_cistatic inline void __xdelay(unsigned long xloops)
6162306a36Sopenharmony_ci{
6262306a36Sopenharmony_ci	unsigned long tmp;
6362306a36Sopenharmony_ci
6462306a36Sopenharmony_ci	__asm__ ("mulul %2,%0:%1"
6562306a36Sopenharmony_ci		: "=d" (xloops), "=d" (tmp)
6662306a36Sopenharmony_ci		: "d" (xloops), "1" (loops_per_jiffy));
6762306a36Sopenharmony_ci	__delay(xloops * HZ);
6862306a36Sopenharmony_ci}
6962306a36Sopenharmony_ci
7062306a36Sopenharmony_ci/*
7162306a36Sopenharmony_ci * The definition of __const_udelay is specifically made a macro so that
7262306a36Sopenharmony_ci * the const factor (4295 = 2**32 / 1000000) can be optimized out when
7362306a36Sopenharmony_ci * the delay is a const.
7462306a36Sopenharmony_ci */
7562306a36Sopenharmony_ci#define	__const_udelay(n)	(__xdelay((n) * 4295))
7662306a36Sopenharmony_ci
7762306a36Sopenharmony_ci#endif
7862306a36Sopenharmony_ci
7962306a36Sopenharmony_cistatic inline void __udelay(unsigned long usecs)
8062306a36Sopenharmony_ci{
8162306a36Sopenharmony_ci	__const_udelay(usecs);
8262306a36Sopenharmony_ci}
8362306a36Sopenharmony_ci
8462306a36Sopenharmony_ci/*
8562306a36Sopenharmony_ci * Use only for very small delays ( < 1 msec).  Should probably use a
8662306a36Sopenharmony_ci * lookup table, really, as the multiplications take much too long with
8762306a36Sopenharmony_ci * short delays.  This is a "reasonable" implementation, though (and the
8862306a36Sopenharmony_ci * first constant multiplications gets optimized away if the delay is
8962306a36Sopenharmony_ci * a constant)
9062306a36Sopenharmony_ci */
9162306a36Sopenharmony_ci#define udelay(n) (__builtin_constant_p(n) ? \
9262306a36Sopenharmony_ci	((n) > 20000 ? __bad_udelay() : __const_udelay(n)) : __udelay(n))
9362306a36Sopenharmony_ci
9462306a36Sopenharmony_ci/*
9562306a36Sopenharmony_ci * nanosecond delay:
9662306a36Sopenharmony_ci *
9762306a36Sopenharmony_ci * ((((HZSCALE) >> 11) * (loops_per_jiffy >> 11)) >> 6) is the number of loops
9862306a36Sopenharmony_ci * per microsecond
9962306a36Sopenharmony_ci *
10062306a36Sopenharmony_ci * 1000 / ((((HZSCALE) >> 11) * (loops_per_jiffy >> 11)) >> 6) is the number of
10162306a36Sopenharmony_ci * nanoseconds per loop
10262306a36Sopenharmony_ci *
10362306a36Sopenharmony_ci * So n / ( 1000 / ((((HZSCALE) >> 11) * (loops_per_jiffy >> 11)) >> 6) ) would
10462306a36Sopenharmony_ci * be the number of loops for n nanoseconds
10562306a36Sopenharmony_ci */
10662306a36Sopenharmony_ci
10762306a36Sopenharmony_ci/*
10862306a36Sopenharmony_ci * The simpler m68k and ColdFire processors do not have a 32*32->64
10962306a36Sopenharmony_ci * multiply instruction. So we need to handle them a little differently.
11062306a36Sopenharmony_ci * We use a bit of shifting and a single 32*32->32 multiply to get close.
11162306a36Sopenharmony_ci * This is a macro so that the const version can factor out the first
11262306a36Sopenharmony_ci * multiply and shift.
11362306a36Sopenharmony_ci */
11462306a36Sopenharmony_ci#define	HZSCALE		(268435456 / (1000000 / HZ))
11562306a36Sopenharmony_ci
11662306a36Sopenharmony_cistatic inline void ndelay(unsigned long nsec)
11762306a36Sopenharmony_ci{
11862306a36Sopenharmony_ci	__delay(DIV_ROUND_UP(nsec *
11962306a36Sopenharmony_ci			     ((((HZSCALE) >> 11) *
12062306a36Sopenharmony_ci			       (loops_per_jiffy >> 11)) >> 6),
12162306a36Sopenharmony_ci			     1000));
12262306a36Sopenharmony_ci}
12362306a36Sopenharmony_ci#define ndelay(n) ndelay(n)
12462306a36Sopenharmony_ci
12562306a36Sopenharmony_ci#endif /* defined(_M68K_DELAY_H) */
126