summaryrefslogtreecommitdiff
path: root/cpu/arm920t/ep93xx/timer.c
blob: 943f193ad84110201c1793a7da478954309ad665 (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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*
 * Cirrus Logic EP93xx timer support.
 *
 * Copyright (C) 2009, 2010
 * Matthias Kaehlcke <matthias@kaehlcke.net>
 *
 * Copyright (C) 2004, 2005
 * Cory T. Tusar, Videon Central, Inc., <ctusar@videon-central.com>
 *
 * Based on the original intr.c Cirrus Logic EP93xx Rev D. interrupt support,
 * author unknown.
 *
 * See file CREDITS for list of people who contributed to this project.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
 * for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include <common.h>
#include <linux/types.h>
#include <asm/arch/ep93xx.h>
#include <asm/io.h>
#include <div64.h>

#define TIMER_CLKSEL	(1 << 3)
#define TIMER_ENABLE	(1 << 7)

#define TIMER_FREQ			508469		/* ticks / second */
#define TIMER_MAX_VAL			0xFFFFFFFF

static struct ep93xx_timer
{
	unsigned long long ticks;
	unsigned long last_update;
} timer;

static inline unsigned long clk_to_systicks(unsigned long long clk_ticks)
{
	unsigned long long sys_ticks = (clk_ticks * CONFIG_SYS_HZ);
	do_div(sys_ticks, TIMER_FREQ);

	return (unsigned long)sys_ticks;
}

static inline unsigned long long usecs_to_ticks(unsigned long usecs)
{
	unsigned long long ticks = (unsigned long long)usecs * TIMER_FREQ;
	do_div(ticks, 1000 * 1000);

	return ticks;
}

static inline unsigned long read_timer(void)
{
	struct timer_regs *timer_regs = (struct timer_regs *)TIMER_BASE;

	return TIMER_MAX_VAL - readl(&timer_regs->timer3.value);
}

/*
 * Get the number of ticks (in CONFIG_SYS_HZ resolution)
 */
unsigned long long get_ticks(void)
{
	const unsigned long now = read_timer();

	if (now >= timer.last_update)
		timer.ticks += now - timer.last_update;
	else
		/* an overflow occurred */
		timer.ticks += TIMER_MAX_VAL - timer.last_update + now;

	timer.last_update = now;

	return clk_to_systicks(timer.ticks);
}

unsigned long get_timer_masked(void)
{
	return get_ticks();
}

unsigned long get_timer(unsigned long base)
{
	return get_timer_masked() - base;
}

void reset_timer_masked(void)
{
	timer.last_update = read_timer();
	timer.ticks = 0;
}

void reset_timer(void)
{
	reset_timer_masked();
}

void __udelay(unsigned long usec)
{
	/* read the timer and update timer.ticks */
	get_ticks();

	const unsigned long long target = timer.ticks + usecs_to_ticks(usec);

	while (timer.ticks < target)
		get_ticks();
}

int timer_init(void)
{
	struct timer_regs *timer_regs = (struct timer_regs *)TIMER_BASE;

	/* use timer 3 with 508KHz and free running */
	writel(TIMER_CLKSEL, &timer_regs->timer3.control);

	/* set initial timer value 3 */
	writel(TIMER_MAX_VAL, &timer_regs->timer3.load);

	/* Enable the timer */
	writel(TIMER_ENABLE | TIMER_CLKSEL,
		&timer_regs->timer3.control);

	reset_timer_masked();

	return 0;
}

/*
 * This function is derived from PowerPC code (timebase clock frequency).
 * On ARM it returns the number of timer ticks per second.
 */
unsigned long get_tbclk(void)
{
	return CONFIG_SYS_HZ;
}