sh: First support code of SuperH.
[oweals/u-boot.git] / cpu / sh4 / time.c
1 /*
2  * (C) Copyright 2003
3  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4  *
5  * See file CREDITS for list of people who contributed to this
6  * project.
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License as
10  * published by the Free Software Foundation; either version 2 of
11  * the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
21  * MA 02111-1307 USA
22  */
23
24 #include <common.h>
25 #include <asm/processor.h>
26
27 #define TMU_MAX_COUNTER (~0UL)
28
29 static void tmu_timer_start (unsigned int timer)
30 {
31         if (timer > 2)
32                 return;
33
34         *((volatile unsigned char *) TSTR0) |= (1 << timer);
35 }
36
37 int timer_init (void)
38 {
39         /* Divide clock by 4 */
40         *(volatile u16 *)TCR0 = 0;
41
42         tmu_timer_start (0);
43         return 0;
44 }
45
46 /*
47    In theory we should return a true 64bit value (ie something that doesn't
48    overflow). However, we don't. Therefore if TMU runs at fastest rate of
49    6.75 MHz this value will wrap after u-boot has been running for approx
50    10 minutes.
51 */
52 unsigned long long get_ticks (void)
53 {
54         return (0 - *((volatile unsigned int *) TCNT0));
55 }
56
57 unsigned long get_timer (unsigned long base)
58 {
59         unsigned long n = 
60                 *((volatile unsigned int *)TCNT0) ;
61         
62         return ((int)n - base ) < 0 ? ( TMU_MAX_COUNTER - ( base -n )):(n - base );
63 }
64
65 void set_timer (unsigned long t)
66 {
67         *((volatile unsigned int *) TCNT0) = (0 - t);
68 }
69
70 void reset_timer (void)
71 {
72         set_timer (0);
73 }
74
75 void udelay (unsigned long usec)
76 {
77         unsigned int start = get_timer (0);
78         unsigned int end = 0;
79         if (usec > 1000000)
80                 end = ((usec/100000) * CFG_HZ) / 10;
81         else if (usec > 1000)
82                 end = ((usec/100) * CFG_HZ) / 10000;
83         else
84                 end = (usec * CFG_HZ) / 1000000;
85
86         while (get_timer (0) < end)
87                 continue;
88 }
89
90 unsigned long get_tbclk (void)
91 {
92         return CFG_HZ;
93 }
94