tizen 2.4 release
[kernel/u-boot-tm1.git] / arch / arm / cpu / arm926ejs / sc8800x / timer.c
1 /*
2  * (C) Copyright 2002
3  * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
4  * Marius Groeger <mgroeger@sysgo.de>
5  *
6  * (C) Copyright 2002
7  * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
8  * Alex Zuepke <azu@sysgo.de>
9  *
10  * (C) Copyright 2002
11  * Gary Jennejohn, DENX Software Engineering, <gj@denx.de>
12  *
13  * (C) Copyright 2009
14  * Ilya Yanok, Emcraft Systems Ltd, <yanok@emcraft.com>
15  *
16  * (C) Copyright 2009 DENX Software Engineering
17  * Author: John Rigby <jrigby@gmail.com>
18  *      Add support for MX25
19  *
20  * See file CREDITS for list of people who contributed to this
21  * project.
22  *
23  * This program is free software; you can redistribute it and/or
24  * modify it under the terms of the GNU General Public License as
25  * published by the Free Software Foundation; either version 2 of
26  * the License, or (at your option) any later version.
27  *
28  * This program is distributed in the hope that it will be useful,
29  * but WITHOUT ANY WARRANTY; without even the implied warranty of
30  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31  * GNU General Public License for more details.
32  *
33  * You should have received a copy of the GNU General Public License
34  * along with this program; if not, write to the Free Software
35  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
36  * MA 02111-1307 USA
37  */
38
39 #include <common.h>
40 #include <div64.h>
41 #include <asm/io.h>
42 #include <linux/types.h>
43 #include <asm/arch/regs_global.h>
44 #include <asm/arch/bits.h>
45 #include <asm/arch/regs_syscnt.h>
46
47
48 static unsigned long long timestamp;
49 static ulong lastinc;
50
51 /*
52  * "time" is measured in 1 / CONFIG_SYS_HZ seconds,
53  * "tick" is internal timer period
54  */
55 static inline unsigned long long tick_to_time(unsigned long long tick)
56 {
57         tick *= CONFIG_SYS_HZ;
58         do_div(tick, CONFIG_SPRD_TIMER_CLK);
59         return tick;
60 }
61
62 static inline unsigned long long time_to_tick(unsigned long long time)
63 {
64         time *= CONFIG_SPRD_TIMER_CLK;
65         do_div(time, CONFIG_SYS_HZ);
66         return time;
67 }
68
69 static inline unsigned long long us_to_tick(unsigned long long us)
70 {
71         us = us*CONFIG_SPRD_TIMER_CLK;
72         do_div(us, CONFIG_SYS_HZ*1000);
73         return us;
74 }
75
76
77 /* nothing really to do with interrupts, just starts up a counter. */
78 /* The 32KHz 23-bit timer overruns in 512 seconds */
79
80 int timer_init(void)
81 {
82         //Enable System Counter device
83         REG32(GR_GEN1) |= GEN1_SYSCLK_EN;
84         
85         return 0;
86 }
87
88 void reset_timer(void)
89 {
90         //capture current incrementer value time
91         lastinc = readl(SYSCNT_COUNT);
92         timestamp = 0;
93 }
94 void reset_timer_masked(void)
95 {
96         reset_timer();
97 }
98 unsigned long long get_ticks(void)
99 {
100         ulong now = readl(SYSCNT_COUNT);
101         
102         if(now >= lastinc) {
103         /* not roll
104          * move stamp forward with absolut diff ticks
105          * */
106                 timestamp += (now - lastinc);
107         }else{
108                 //timer roll over 
109                 timestamp += (TIMER_MAX_VALUE - lastinc) + now;
110         }
111         lastinc = now;
112         return timestamp;
113 }
114
115 ulong get_timer_masked(void)
116 {
117         return tick_to_time(get_ticks());
118 }
119
120 ulong get_timer(ulong base)
121 {
122         return get_timer_masked() - base;
123 }
124
125 void set_timer(ulong t)
126 {
127         timestamp = time_to_tick(t);
128 }
129
130 void __udelay (unsigned long usec)
131 {
132         unsigned long long tmp;
133         ulong tmo;
134         
135         tmo = us_to_tick(usec);
136         tmp = get_ticks() + tmo; // get current timestamp
137         
138         while(get_ticks() < tmp) //loop till event
139                 /*NOP*/;
140 }