00230c3298013dfdefe186f44f3e30379298dfb7
[oweals/u-boot.git] / arch / sh / lib / time.c
1 /*
2  * (C) Copyright 2009
3  * Jean-Christophe PLAGNIOL-VILLARD <plagnioj@jcrosoft.com>
4  *
5  * (C) Copyright 2007-2012
6  * Nobobuhiro Iwamatsu <iwamatsu@nigauri.org>
7  *
8  * (C) Copyright 2003
9  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
10  *
11  * See file CREDITS for list of people who contributed to this
12  * project.
13  *
14  * This program is free software; you can redistribute it and/or
15  * modify it under the terms of the GNU General Public License as
16  * published by the Free Software Foundation; either version 2 of
17  * the License, or (at your option) any later version.
18  *
19  * This program is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  * GNU General Public License for more details.
23  *
24  * You should have received a copy of the GNU General Public License
25  * along with this program; if not, write to the Free Software
26  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
27  * MA 02111-1307 USA
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 #include <sh_tmu.h>
36
37 static struct tmu_regs *tmu = (struct tmu_regs *)TMU_BASE;
38
39 static u16 bit;
40 static unsigned long last_tcnt;
41 static unsigned long long overflow_ticks;
42
43 unsigned long get_tbclk(void)
44 {
45         return get_tmu0_clk_rate() >> ((bit + 1) * 2);
46 }
47
48 static inline unsigned long long tick_to_time(unsigned long long tick)
49 {
50         tick *= CONFIG_SYS_HZ;
51         do_div(tick, get_tbclk());
52
53         return tick;
54 }
55
56 static inline unsigned long long usec_to_tick(unsigned long long usec)
57 {
58         usec *= get_tbclk();
59         do_div(usec, 1000000);
60
61         return usec;
62 }
63
64 static void tmu_timer_start(unsigned int timer)
65 {
66         if (timer > 2)
67                 return;
68         writeb(readb(&tmu->tstr) | (1 << timer), &tmu->tstr);
69 }
70
71 static void tmu_timer_stop(unsigned int timer)
72 {
73         if (timer > 2)
74                 return;
75         writeb(readb(&tmu->tstr) & ~(1 << timer), &tmu->tstr);
76 }
77
78 int timer_init(void)
79 {
80         bit = (ffs(CONFIG_SYS_TMU_CLK_DIV) >> 1) - 1;
81         writew(readw(&tmu->tcr0) | bit, &tmu->tcr0);
82
83         tmu_timer_stop(0);
84         tmu_timer_start(0);
85
86         last_tcnt = 0;
87         overflow_ticks = 0;
88
89         return 0;
90 }
91
92 unsigned long long get_ticks(void)
93 {
94         unsigned long tcnt = 0 - readl(&tmu->tcnt0);
95
96         if (last_tcnt > tcnt) /* overflow */
97                 overflow_ticks++;
98         last_tcnt = tcnt;
99
100         return (overflow_ticks << 32) | tcnt;
101 }
102
103 void __udelay(unsigned long usec)
104 {
105         unsigned long long tmp;
106         ulong tmo;
107
108         tmo = usec_to_tick(usec);
109         tmp = get_ticks() + tmo;        /* get current timestamp */
110
111         while (get_ticks() < tmp)       /* loop till event */
112                  /*NOP*/;
113 }
114
115 unsigned long get_timer(unsigned long base)
116 {
117         /* return msec */
118         return tick_to_time(get_ticks()) - base;
119 }
120
121 void set_timer(unsigned long t)
122 {
123         writel((0 - t), &tmu->tcnt0);
124 }
125
126 void reset_timer(void)
127 {
128         tmu_timer_stop(0);
129         set_timer(0);
130         tmu_timer_start(0);
131 }