blob: 321e9b4181357ab797cbda4433e4bc19941804ba [file] [log] [blame]
Dinh Nguyenad51f7c2012-10-04 06:46:02 +00001/*
2 * Copyright (C) 2012 Altera Corporation <www.altera.com>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#include <common.h>
19#include <asm/io.h>
20#include <asm/arch/timer.h>
21
22DECLARE_GLOBAL_DATA_PTR;
23
24static const struct socfpga_timer *timer_base = (void *)CONFIG_SYS_TIMERBASE;
25
26/*
27 * Timer initialization
28 */
29int timer_init(void)
30{
31 writel(TIMER_LOAD_VAL, &timer_base->load_val);
32 writel(TIMER_LOAD_VAL, &timer_base->curr_val);
33 writel(readl(&timer_base->ctrl) | 0x3, &timer_base->ctrl);
34 return 0;
35}
36
37static u32 read_timer(void)
38{
39 return readl(&timer_base->curr_val);
40}
41
42/*
43 * Delay x useconds
44 */
45void __udelay(unsigned long usec)
46{
47 unsigned long now, last;
48 /*
49 * get the tmo value based on timer clock speed
50 * tmo = delay required / period of timer clock
51 */
52 long tmo = usec * CONFIG_TIMER_CLOCK_KHZ / 1000;
53
54 last = read_timer();
55 while (tmo > 0) {
56 now = read_timer();
57 if (last >= now)
58 /* normal mode (non roll) */
59 tmo -= last - now;
60 else
61 /* we have overflow of the count down timer */
62 tmo -= TIMER_LOAD_VAL - last + now;
63 last = now;
64 }
65}
66
67/*
68 * Get the timer value
69 */
70ulong get_timer(ulong base)
71{
72 return get_timer_masked() - base;
73}
74
75/*
76 * Timer : get the time difference
77 * Unit of tick is based on the CONFIG_SYS_HZ
78 */
79ulong get_timer_masked(void)
80{
81 /* current tick value */
82 ulong now = read_timer() / (CONFIG_TIMER_CLOCK_KHZ/CONFIG_SYS_HZ);
83 if (gd->lastinc >= now) {
84 /* normal mode (non roll) */
85 /* move stamp forward with absolute diff ticks */
86 gd->tbl += gd->lastinc - now;
87 } else {
88 /* we have overflow of the count down timer */
89 gd->tbl += TIMER_LOAD_VAL - gd->lastinc + now;
90 }
91 gd->lastinc = now;
92 return gd->tbl;
93}
94
95/*
96 * Reset the timer
97 */
98void reset_timer(void)
99{
100 /* capture current decrementer value time */
101 gd->lastinc = read_timer() / (CONFIG_TIMER_CLOCK_KHZ/CONFIG_SYS_HZ);
102 /* start "advancing" time stamp from 0 */
103 gd->tbl = 0;
104}