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#include <common.h>
40#include <div64.h>
41#include <asm/io.h>
42#include <asm/arch/imx-regs.h>
43
44DECLARE_GLOBAL_DATA_PTR;
45
46#define timestamp (gd->tbl)
47#define lastinc (gd->lastinc)
48
49
50
51
52
53#ifdef CONFIG_MX25_TIMER_HIGH_PRECISION
54
55static inline unsigned long long tick_to_time(unsigned long long tick)
56{
57 tick *= CONFIG_SYS_HZ;
58 do_div(tick, CONFIG_MX25_CLK32);
59 return tick;
60}
61
62static inline unsigned long long time_to_tick(unsigned long long time)
63{
64 time *= CONFIG_MX25_CLK32;
65 do_div(time, CONFIG_SYS_HZ);
66 return time;
67}
68
69static inline unsigned long long us_to_tick(unsigned long long us)
70{
71 us = us * CONFIG_MX25_CLK32 + 999999;
72 do_div(us, 1000000);
73 return us;
74}
75#else
76
77#define TICK_PER_TIME ((CONFIG_MX25_CLK32 + CONFIG_SYS_HZ / 2) / \
78 CONFIG_SYS_HZ)
79#define US_PER_TICK (1000000 / CONFIG_MX25_CLK32)
80
81static inline unsigned long long tick_to_time(unsigned long long tick)
82{
83 do_div(tick, TICK_PER_TIME);
84 return tick;
85}
86
87static inline unsigned long long time_to_tick(unsigned long long time)
88{
89 return time * TICK_PER_TIME;
90}
91
92static inline unsigned long long us_to_tick(unsigned long long us)
93{
94 us += US_PER_TICK - 1;
95 do_div(us, US_PER_TICK);
96 return us;
97}
98#endif
99
100
101
102int timer_init(void)
103{
104 int i;
105 struct gpt_regs *gpt = (struct gpt_regs *)IMX_GPT1_BASE;
106 struct ccm_regs *ccm = (struct ccm_regs *)IMX_CCM_BASE;
107
108
109 writel(GPT_CTRL_SWR, &gpt->ctrl);
110
111 writel(readl(&ccm->cgr1) | CCM_CGR1_GPT1, &ccm->cgr1);
112
113 for (i = 0; i < 100; i++)
114 writel(0, &gpt->ctrl);
115 writel(0, &gpt->pre);
116
117 writel(readl(&gpt->ctrl) | GPT_CTRL_CLKSOURCE_32 | GPT_CTRL_FRR,
118 &gpt->ctrl);
119 writel(readl(&gpt->ctrl) | GPT_CTRL_TEN, &gpt->ctrl);
120
121 return 0;
122}
123
124unsigned long long get_ticks(void)
125{
126 struct gpt_regs *gpt = (struct gpt_regs *)IMX_GPT1_BASE;
127 ulong now = readl(&gpt->counter);
128
129 if (now >= lastinc) {
130
131
132
133
134 timestamp += (now - lastinc);
135 } else {
136
137 timestamp += (0xFFFFFFFF - lastinc) + now;
138 }
139 lastinc = now;
140 return timestamp;
141}
142
143ulong get_timer_masked(void)
144{
145
146
147
148
149
150
151 return tick_to_time(get_ticks());
152}
153
154ulong get_timer(ulong base)
155{
156 return get_timer_masked() - base;
157}
158
159
160void __udelay(unsigned long usec)
161{
162 unsigned long long tmp;
163 ulong tmo;
164
165 tmo = us_to_tick(usec);
166 tmp = get_ticks() + tmo;
167
168 while (get_ticks() < tmp)
169 ;
170}
171
172
173
174
175
176ulong get_tbclk(void)
177{
178 ulong tbclk;
179
180 tbclk = CONFIG_MX25_CLK32;
181 return tbclk;
182}
183