blob: d93de1e611411f96b19c2c8e3da3674ded45cc9c [file] [log] [blame]
Rob Herring73089ad2011-10-24 08:50:20 +00001/*
2 * Copyright 2010-2011 Calxeda, Inc.
3 *
4 * Based on arm926ejs/mx27/timer.c
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the Free
8 * Software Foundation; either version 2 of the License, or (at your option)
9 * any later version.
10 *
11 * This program is distributed in the hope it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * more details.
15 *
16 * You should have received a copy of the GNU General Public License along with
17 * this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include <common.h>
21#include <div64.h>
22#include <linux/types.h> /* for size_t */
23#include <linux/stddef.h> /* for NULL */
24#include <asm/io.h>
25#include <asm/arch-armv7/systimer.h>
26
27#undef SYSTIMER_BASE
28#define SYSTIMER_BASE 0xFFF34000 /* Timer 0 and 1 base */
Rob Herringd7dadc72013-06-12 22:24:49 -050029#define SYSTIMER_RATE (150000000 / 256)
Rob Herring73089ad2011-10-24 08:50:20 +000030
31static ulong timestamp;
32static ulong lastinc;
33static struct systimer *systimer_base = (struct systimer *)SYSTIMER_BASE;
34
35/*
36 * Start the timer
37 */
38int timer_init(void)
39{
40 /*
41 * Setup timer0
42 */
Rob Herringd7dadc72013-06-12 22:24:49 -050043 writel(0, &systimer_base->timer0control);
Rob Herring73089ad2011-10-24 08:50:20 +000044 writel(SYSTIMER_RELOAD, &systimer_base->timer0load);
45 writel(SYSTIMER_RELOAD, &systimer_base->timer0value);
Rob Herringd7dadc72013-06-12 22:24:49 -050046 writel(SYSTIMER_EN | SYSTIMER_32BIT | SYSTIMER_PRESC_256,
47 &systimer_base->timer0control);
Rob Herring73089ad2011-10-24 08:50:20 +000048
49 reset_timer_masked();
50
51 return 0;
52
53}
54
55#define TICK_PER_TIME ((SYSTIMER_RATE + CONFIG_SYS_HZ / 2) / CONFIG_SYS_HZ)
56#define NS_PER_TICK (1000000000 / SYSTIMER_RATE)
57
58static inline unsigned long long tick_to_time(unsigned long long tick)
59{
60 do_div(tick, TICK_PER_TIME);
61 return tick;
62}
63
64static inline unsigned long long time_to_tick(unsigned long long time)
65{
66 return time * TICK_PER_TIME;
67}
68
69static inline unsigned long long us_to_tick(unsigned long long us)
70{
Rob Herring3ec96f32012-02-01 16:57:52 +000071 unsigned long long tick = us * 1000;
Rob Herring73089ad2011-10-24 08:50:20 +000072 tick += NS_PER_TICK - 1;
73 do_div(tick, NS_PER_TICK);
Rob Herring3ec96f32012-02-01 16:57:52 +000074 return tick;
Rob Herring73089ad2011-10-24 08:50:20 +000075}
76
77unsigned long long get_ticks(void)
78{
79 ulong now = ~readl(&systimer_base->timer0value);
80
81 if (now >= lastinc) /* normal mode (non roll) */
82 /* move stamp forward with absolut diff ticks */
83 timestamp += (now - lastinc);
84 else /* we have rollover of incrementer */
85 timestamp += (0xFFFFFFFF - lastinc) + now;
86 lastinc = now;
87 return timestamp;
88}
89
90/*
91 * Delay x useconds AND preserve advance timstamp value
92 * assumes timer is ticking at 1 msec
93 */
94void __udelay(ulong usec)
95{
96 unsigned long long tmp;
97 ulong tmo;
98
99 tmo = us_to_tick(usec);
100 tmp = get_ticks() + tmo; /* get current timestamp */
101
102 while (get_ticks() < tmp) /* loop till event */
103 /*NOP*/;
104}
105
106ulong get_timer(ulong base)
107{
108 return get_timer_masked() - base;
109}
110
111void reset_timer_masked(void)
112{
113 lastinc = ~readl(&systimer_base->timer0value);
114 timestamp = 0;
115}
116
117void reset_timer(void)
118{
119 reset_timer_masked();
120}
121
122ulong get_timer_masked(void)
123{
124 return tick_to_time(get_ticks());
125}
Rob Herring555fe5e2012-02-21 12:52:27 +0000126
127ulong get_tbclk(void)
128{
Rob Herring98861ff2013-06-12 22:24:48 -0500129 return SYSTIMER_RATE;
Rob Herring555fe5e2012-02-21 12:52:27 +0000130}