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#include <common.h>
31#include <div64.h>
32#include <asm/processor.h>
33#include <asm/clk.h>
34#include <asm/io.h>
35
36#define TMU_MAX_COUNTER (~0UL)
37
38static ulong timer_freq;
39
40static inline unsigned long long tick_to_time(unsigned long long tick)
41{
42 tick *= CONFIG_SYS_HZ;
43 do_div(tick, timer_freq);
44
45 return tick;
46}
47
48static inline unsigned long long usec_to_tick(unsigned long long usec)
49{
50 usec *= timer_freq;
51 do_div(usec, 1000000);
52
53 return usec;
54}
55
56static void tmu_timer_start (unsigned int timer)
57{
58 if (timer > 2)
59 return;
60 writeb(readb(TSTR) | (1 << timer), TSTR);
61}
62
63static void tmu_timer_stop (unsigned int timer)
64{
65 if (timer > 2)
66 return;
67 writeb(readb(TSTR) & ~(1 << timer), TSTR);
68}
69
70int timer_init (void)
71{
72
73 u16 bit = 0;
74
75 switch (CONFIG_SYS_TMU_CLK_DIV) {
76 case 1024:
77 bit = 4;
78 break;
79 case 256:
80 bit = 3;
81 break;
82 case 64:
83 bit = 2;
84 break;
85 case 16:
86 bit = 1;
87 break;
88 case 4:
89 default:
90 break;
91 }
92 writew(readw(TCR0) | bit, TCR0);
93
94
95 timer_freq = get_tmu0_clk_rate() >> ((bit + 1) * 2);
96
97 tmu_timer_stop(0);
98 tmu_timer_start(0);
99
100 return 0;
101}
102
103unsigned long long get_ticks (void)
104{
105 return 0 - readl(TCNT0);
106}
107
108void udelay (unsigned long usec)
109{
110 unsigned long long tmp;
111 ulong tmo;
112
113 tmo = usec_to_tick(usec);
114 tmp = get_ticks() + tmo;
115
116 while (get_ticks() < tmp)
117 ;
118}
119
120unsigned long get_timer (unsigned long base)
121{
122
123 return tick_to_time(get_ticks()) - base;
124}
125
126void set_timer (unsigned long t)
127{
128 writel((0 - t), TCNT0);
129}
130
131void reset_timer (void)
132{
133 tmu_timer_stop(0);
134 set_timer (0);
135 tmu_timer_start(0);
136}
137
138unsigned long get_tbclk (void)
139{
140 return timer_freq;
141}
142