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#include <common.h>
36#include <div64.h>
37#include <asm/io.h>
38#include <asm/arch/imx-regs.h>
39
40
41#define GPTCR_SWR (1 << 15)
42#define GPTCR_FRR (1 << 8)
43#define GPTCR_CLKSOURCE_32 (4 << 1)
44#define GPTCR_TEN 1
45
46static ulong timestamp;
47static ulong lastinc;
48
49
50
51
52
53#ifdef CONFIG_MX27_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_MX27_CLK32);
59 return tick;
60}
61
62static inline unsigned long long time_to_tick(unsigned long long time)
63{
64 time *= CONFIG_MX27_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_MX27_CLK32 + 999999;
72 do_div(us, 1000000);
73 return us;
74}
75#else
76
77#define TICK_PER_TIME ((CONFIG_MX27_CLK32 + CONFIG_SYS_HZ / 2) / \
78 CONFIG_SYS_HZ)
79#define US_PER_TICK (1000000 / CONFIG_MX27_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 *regs = (struct gpt_regs *)IMX_TIM1_BASE;
106 struct pll_regs *pll = (struct pll_regs *)IMX_PLL_BASE;
107
108
109 writel(GPTCR_SWR, ®s->gpt_tctl);
110
111 writel(readl(&pll->pccr0) | PCCR0_GPT1_EN, &pll->pccr0);
112 writel(readl(&pll->pccr1) | PCCR1_PERCLK1_EN, &pll->pccr1);
113
114 for (i = 0; i < 100; i++)
115 writel(0, ®s->gpt_tctl);
116 writel(0, ®s->gpt_tprer);
117
118 writel(readl(®s->gpt_tctl) | GPTCR_CLKSOURCE_32 | GPTCR_FRR,
119 ®s->gpt_tctl);
120 writel(readl(®s->gpt_tctl) | GPTCR_TEN, ®s->gpt_tctl);
121
122 return 0;
123}
124
125void reset_timer_masked(void)
126{
127 struct gpt_regs *regs = (struct gpt_regs *)IMX_TIM1_BASE;
128
129
130 lastinc = readl(®s->gpt_tcn);
131 timestamp = 0;
132}
133
134void reset_timer(void)
135{
136 reset_timer_masked();
137}
138
139unsigned long long get_ticks (void)
140{
141 struct gpt_regs *regs = (struct gpt_regs *)IMX_TIM1_BASE;
142 ulong now = readl(®s->gpt_tcn);
143
144 if (now >= lastinc) {
145
146
147
148
149 timestamp += (now - lastinc);
150 } else {
151
152 timestamp += (0xFFFFFFFF - lastinc) + now;
153 }
154 lastinc = now;
155 return timestamp;
156}
157
158ulong get_timer_masked (void)
159{
160
161
162
163
164
165
166 return tick_to_time(get_ticks());
167}
168
169ulong get_timer (ulong base)
170{
171 return get_timer_masked () - base;
172}
173
174void set_timer (ulong t)
175{
176 timestamp = time_to_tick(t);
177}
178
179
180void __udelay (unsigned long usec)
181{
182 unsigned long long tmp;
183 ulong tmo;
184
185 tmo = us_to_tick(usec);
186 tmp = get_ticks() + tmo;
187
188 while (get_ticks() < tmp)
189 ;
190}
191