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