summaryrefslogtreecommitdiffstats
path: root/arch/arm/mach-netx/interrupts.c
blob: af78e19bfd45b592daa55e2c6e3145f167c60fe7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <common.h>

#include <mach/netx-regs.h>

int timer_init (void)
{
	/* disable timer initially */
	GPIO_REG(GPIO_COUNTER_CTRL(0)) = 0;
	/* Reset the timer value to zero */
	GPIO_REG(GPIO_COUNTER_CURRENT(0)) = 0;
	GPIO_REG(GPIO_COUNTER_MAX(0)) = 0xffffffff;
	GPIO_REG(GPIO_COUNTER_CTRL(0)) = COUNTER_CTRL_RUN;

	return 0;
}

/* current timestamp in ms */
static unsigned long timestamp = 0;

static unsigned long last_timer = 0;

/* We can't detect overruns here since we don't know how often
 * we get called. The only thing we can do is to make sure that
 * time moves forward.
 */
ulong get_timer (ulong start)
{
	unsigned long cur_timer = GPIO_REG(GPIO_COUNTER_CURRENT(0));
	unsigned long time_inc;


	if(cur_timer > last_timer)
		time_inc = (cur_timer - last_timer) / 100000;
	else
		time_inc = ((0xffffffff - last_timer) + cur_timer) / 100000;

	if(time_inc)
		last_timer = cur_timer;

	timestamp += time_inc;

	return timestamp - start;
}

void mdelay(unsigned long msec)
{
	unsigned long now = get_timer(0);

	while( get_timer(0) < now + msec );
}

void udelay(unsigned long usec)
{
	unsigned long start, end, msec = usec / 1000;

	if(msec)
		mdelay(msec);

	usec -= msec * 1000;

	start = GPIO_REG(GPIO_COUNTER_CURRENT(0));
	end = start + usec * 100;

	if(end < start) {
		/* wait for overrun */
		while( GPIO_REG(GPIO_COUNTER_CURRENT(0)) > start);
	}

	while( GPIO_REG(GPIO_COUNTER_CURRENT(0)) < end);
}