5fb05d575adbacedce97d5b6bc191b9042e9acfe
[profile/mobile/platform/kernel/u-boot-tm1.git] / arch / arm / cpu / armv7 / sc8830 / 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/bits.h"
44 #include "asm/arch/sci_types.h"
45 #include "asm/arch/chip_drv_common_io.h"
46 #include "asm/arch/sys_timer_reg_v0.h"
47
48 #define TIMER_MAX_VALUE 0xFFFFFFFF
49
50
51 static unsigned long long timestamp;
52 static ulong lastinc;
53
54 /*
55  * "time" is measured in 1 / CONFIG_SYS_HZ seconds,
56  * "tick" is internal timer period
57  */
58 static inline unsigned long long tick_to_time(unsigned long long tick)
59 {
60         tick *= CONFIG_SYS_HZ;
61         do_div(tick, CONFIG_SPRD_TIMER_CLK);
62         return tick;
63 }
64
65 static inline unsigned long long time_to_tick(unsigned long long time)
66 {
67         time *= CONFIG_SPRD_TIMER_CLK;
68         do_div(time, CONFIG_SYS_HZ);
69         return time;
70 }
71
72 static inline unsigned long long us_to_tick(unsigned long long us)
73 {
74         us = us*CONFIG_SPRD_TIMER_CLK;
75         do_div(us, CONFIG_SYS_HZ*1000);
76         return us;
77 }
78
79
80 /* nothing really to do with interrupts, just starts up a counter. */
81 /* The 32KHz 23-bit timer overruns in 512 seconds */
82
83 int timer_init(void)
84 {
85         REG32(REG_AON_APB_APB_EB0) |= BIT_AP_SYST_EB; //make sys cnt writable
86         REG32(REG_AON_APB_APB_EB0) |= BIT_AON_TMR_EB;
87
88         REG32(REG_AON_APB_APB_RTC_EB) |= BIT_AP_SYST_RTC_EB;  //enable sys timer rtc
89
90         return 0;
91 }
92
93 void reset_timer(void)
94 {
95         //capture current incrementer value time
96         lastinc = readl(SYS_CNT0);
97         timestamp = 0;
98 }
99 void reset_timer_masked(void)
100 {
101         reset_timer();
102 }
103 unsigned long long get_ticks(void)
104 {
105         ulong now = readl(SYS_CNT0);
106         
107         if(now >= lastinc) {
108         /* not roll
109          * move stamp forward with absolut diff ticks
110          * */
111                 timestamp += (now - lastinc);
112         }else{
113                 //timer roll over 
114                 timestamp += (TIMER_MAX_VALUE - lastinc) + now;
115         }
116         lastinc = now;
117         return timestamp;
118 }
119
120 ulong get_timer_masked(void)
121 {
122         return tick_to_time(get_ticks());
123 }
124
125 ulong get_timer(ulong base)
126 {
127         return get_timer_masked() - base;
128 }
129
130 void set_timer(ulong t)
131 {
132         timestamp = time_to_tick(t);
133 }
134
135 void __udelay (unsigned long usec)
136 {
137         unsigned long long tmp;
138         ulong tmo;
139
140         tmo = us_to_tick(usec);
141         tmp = get_ticks() + tmo; // get current timestamp
142         
143         while(get_ticks() < tmp) //loop till event
144                 /*NOP*/;
145 }
146
147 uint32 SCI_GetTickCount(void)
148 {
149         volatile uint32 tmp_tick1;
150         volatile uint32 tmp_tick2;
151
152         tmp_tick1 = SYSTEM_CURRENT_CLOCK;
153         tmp_tick2 = SYSTEM_CURRENT_CLOCK;
154
155         while (tmp_tick1 != tmp_tick2)
156         {
157                 tmp_tick1 = tmp_tick2;
158                 tmp_tick2 = SYSTEM_CURRENT_CLOCK;
159         }
160
161         return tmp_tick1;
162 }
163