18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright (C) 2008 Michal Simek
48c2ecf20Sopenharmony_ci * Copyright (C) 2007 John Williams
58c2ecf20Sopenharmony_ci * Copyright (C) 2006 Atmark Techno, Inc.
68c2ecf20Sopenharmony_ci */
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ci#ifndef _ASM_MICROBLAZE_DELAY_H
98c2ecf20Sopenharmony_ci#define _ASM_MICROBLAZE_DELAY_H
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_ci#include <linux/param.h>
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_cistatic inline void __delay(unsigned long loops)
148c2ecf20Sopenharmony_ci{
158c2ecf20Sopenharmony_ci	asm volatile ("# __delay		\n\t"		\
168c2ecf20Sopenharmony_ci			"1: addi	%0, %0, -1\t\n"		\
178c2ecf20Sopenharmony_ci			"bneid	%0, 1b		\t\n"		\
188c2ecf20Sopenharmony_ci			"nop			\t\n"
198c2ecf20Sopenharmony_ci			: "=r" (loops)
208c2ecf20Sopenharmony_ci			: "0" (loops));
218c2ecf20Sopenharmony_ci}
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_ci/*
248c2ecf20Sopenharmony_ci * Note that 19 * 226 == 4294 ==~ 2^32 / 10^6, so
258c2ecf20Sopenharmony_ci * loops = (4294 * usecs * loops_per_jiffy * HZ) / 2^32.
268c2ecf20Sopenharmony_ci *
278c2ecf20Sopenharmony_ci * The mul instruction gives us loops = (a * b) / 2^32.
288c2ecf20Sopenharmony_ci * We choose a = usecs * 19 * HZ and b = loops_per_jiffy * 226
298c2ecf20Sopenharmony_ci * because this lets us support a wide range of HZ and
308c2ecf20Sopenharmony_ci * loops_per_jiffy values without either a or b overflowing 2^32.
318c2ecf20Sopenharmony_ci * Thus we need usecs * HZ <= (2^32 - 1) / 19 = 226050910 and
328c2ecf20Sopenharmony_ci * loops_per_jiffy <= (2^32 - 1) / 226 = 19004280
338c2ecf20Sopenharmony_ci * (which corresponds to ~3800 bogomips at HZ = 100).
348c2ecf20Sopenharmony_ci * -- paulus
358c2ecf20Sopenharmony_ci */
368c2ecf20Sopenharmony_ci#define __MAX_UDELAY	(226050910UL/HZ)	/* maximum udelay argument */
378c2ecf20Sopenharmony_ci#define __MAX_NDELAY	(4294967295UL/HZ)	/* maximum ndelay argument */
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_ciextern unsigned long loops_per_jiffy;
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_cistatic inline void __udelay(unsigned int x)
428c2ecf20Sopenharmony_ci{
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_ci	unsigned long long tmp =
458c2ecf20Sopenharmony_ci		(unsigned long long)x * (unsigned long long)loops_per_jiffy \
468c2ecf20Sopenharmony_ci			* 226LL;
478c2ecf20Sopenharmony_ci	unsigned loops = tmp >> 32;
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_ci/*
508c2ecf20Sopenharmony_ci	__asm__("mulxuu %0,%1,%2" : "=r" (loops) :
518c2ecf20Sopenharmony_ci		"r" (x), "r" (loops_per_jiffy * 226));
528c2ecf20Sopenharmony_ci*/
538c2ecf20Sopenharmony_ci	__delay(loops);
548c2ecf20Sopenharmony_ci}
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_ciextern void __bad_udelay(void);		/* deliberately undefined */
578c2ecf20Sopenharmony_ciextern void __bad_ndelay(void);		/* deliberately undefined */
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_ci#define udelay(n)						\
608c2ecf20Sopenharmony_ci	({							\
618c2ecf20Sopenharmony_ci		if (__builtin_constant_p(n)) {			\
628c2ecf20Sopenharmony_ci			if ((n) / __MAX_UDELAY >= 1)		\
638c2ecf20Sopenharmony_ci				__bad_udelay();			\
648c2ecf20Sopenharmony_ci			else					\
658c2ecf20Sopenharmony_ci				__udelay((n) * (19 * HZ));	\
668c2ecf20Sopenharmony_ci		} else {					\
678c2ecf20Sopenharmony_ci			__udelay((n) * (19 * HZ));		\
688c2ecf20Sopenharmony_ci		}						\
698c2ecf20Sopenharmony_ci	})
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_ci#define ndelay(n)						\
728c2ecf20Sopenharmony_ci	({							\
738c2ecf20Sopenharmony_ci		if (__builtin_constant_p(n)) {			\
748c2ecf20Sopenharmony_ci			if ((n) / __MAX_NDELAY >= 1)		\
758c2ecf20Sopenharmony_ci				__bad_ndelay();			\
768c2ecf20Sopenharmony_ci			else					\
778c2ecf20Sopenharmony_ci				__udelay((n) * HZ);		\
788c2ecf20Sopenharmony_ci		} else {					\
798c2ecf20Sopenharmony_ci			__udelay((n) * HZ);			\
808c2ecf20Sopenharmony_ci		}						\
818c2ecf20Sopenharmony_ci	})
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_ci#define muldiv(a, b, c)		(((a)*(b))/(c))
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_ci#endif /* _ASM_MICROBLAZE_DELAY_H */
86