1/*
2 *	Precise Delay Loops for Meta
3 *
4 *	Copyright (C) 1993 Linus Torvalds
5 *	Copyright (C) 1997 Martin Mares <mj@atrey.karlin.mff.cuni.cz>
6 *	Copyright (C) 2007,2009 Imagination Technologies Ltd.
7 *
8 */
9
10#include <linux/export.h>
11#include <linux/sched.h>
12#include <linux/delay.h>
13
14#include <asm/core_reg.h>
15#include <asm/processor.h>
16
17/*
18 * TXTACTCYC is only 24 bits, so on chips with fast clocks it will wrap
19 * many times per-second. If it does wrap __delay will return prematurely,
20 * but this is only likely with large delay values.
21 *
22 * We also can't implement read_current_timer() with TXTACTCYC due to
23 * this wrapping behaviour.
24 */
25#define rdtimer(t) t = __core_reg_get(TXTACTCYC)
26
27void __delay(unsigned long loops)
28{
29	unsigned long bclock, now;
30
31	rdtimer(bclock);
32	do {
33		asm("NOP");
34		rdtimer(now);
35	} while ((now-bclock) < loops);
36}
37EXPORT_SYMBOL(__delay);
38
39inline void __const_udelay(unsigned long xloops)
40{
41	u64 loops = (u64)xloops * (u64)loops_per_jiffy * HZ;
42	__delay(loops >> 32);
43}
44EXPORT_SYMBOL(__const_udelay);
45
46void __udelay(unsigned long usecs)
47{
48	__const_udelay(usecs * 0x000010c7); /* 2**32 / 1000000 (rounded up) */
49}
50EXPORT_SYMBOL(__udelay);
51
52void __ndelay(unsigned long nsecs)
53{
54	__const_udelay(nsecs * 0x00005); /* 2**32 / 1000000000 (rounded up) */
55}
56EXPORT_SYMBOL(__ndelay);
57