summaryrefslogtreecommitdiff
path: root/arch/arm/cpu/armv7/vybrid-common/timer.c
blob: 6062a41e71ffa94cabfb712d5ae1cde768c82e42 (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
/*
 * Copyright 2012 Freescale Semiconductor, Inc.
 *
 * 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., 59 Temple Place, Suite 330, Boston,
 * MA 02111-1307 USA
 */

#include <common.h>
#include <asm/io.h>
#include <div64.h>
#include <asm/arch/timer.h>
#include <asm/arch/vybrid-regs.h>

DECLARE_GLOBAL_DATA_PTR;

#define timestamp (gd->tbl)
#define timerticks (gd->tbu)
#define lastinc	(gd->lastinc)
static unsigned long ltmstamp = 0;

#define CONFIG_TMR_USEPIT
#ifdef CONFIG_TMR_USEPIT

unsigned long long _usec2ticks(unsigned long long usec);

int timer_init(void)
{
	ulong usecs;
	ulong ticks;

	timestamp = 0;

	/*
	 * nsecs conversion = (1/ipg_clk) * 10^9
	 * equivalent to 1000 / (ipg_clk / 10^6)
	 */
	usecs = (gd->ipg_clk / 1000000);
	ticks = 1000 / usecs;

	clrbits_le32(PIT_MCR, 2);	/* enable PIT */

	/* ticks per 10 us = 10000 us / usecs = cycles time */
	timerticks = (10 * 1000) / ticks;

	__raw_writel(0xFFFFFFFF, PIT_LDVAL1);
	__raw_writel(0, PIT_TCTRL1);
	__raw_writel(4, PIT_TCTRL1);
	__raw_writel(5, PIT_TCTRL1);
	__raw_writel(timerticks, PIT_LDVAL0);
	__raw_writel(1, PIT_TCTRL0);

	lastinc = __raw_readl(PIT_LTMR64H);

	return 0;
}

ulong get_timer(ulong base)
{
	unsigned long now, diff;

	now = __raw_readl(PIT_LTMR64H);
	diff = -(now - lastinc);
	ltmstamp += diff;
	while (ltmstamp > 100) {
		timestamp++;
		ltmstamp -= 100;
	}
	lastinc = now;

	return timestamp - base;
}

/* delay x useconds AND preserve advance timstamp value */
void __udelay(unsigned long usec)
{
	ulong nsecs, tmp;

	/*
	 * nsecs conversion = (1/ipg_clk) * 10^9
	 * equivalent to 1000 / (ipg_clk / 10^6)
	 */
	if (usec < 5)
		usec = 10;

	nsecs = gd->ipg_clk / 1000000;
	nsecs = 1000 / nsecs;

	/* 1 us per ticks = 1000 ns / nsecs = cycles time */
	while (usec > 0) {
		if (usec > 65000)
			tmp = 65000;
		else
			tmp = usec;
		usec = usec - tmp;

		tmp =  (tmp * 1000) / nsecs;

		__raw_writel(tmp, PIT_LDVAL2);
		__raw_writel(1, PIT_TCTRL2);

		while ((__raw_readl(PIT_TFLG2) & 1) != 1)
			;
		__raw_writel(0, PIT_TCTRL2);
		__raw_writel(1, PIT_TFLG2);
	}
}
#endif			/* CONFIG_TMR_USEPIT */

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

unsigned long long get_ticks(void)
{
	return get_timer(0);
}

ulong get_tbclk(void)
{
	return CONFIG_SYS_HZ;
}