blob: 2bd77152825b227ddb533ab80166d6cf1283dcc1 [file] [log] [blame]
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +09001/*
Nobuhiro Iwamatsue9d5f352008-11-20 16:44:42 +09002 * (C) Copyright 2007-2008
3 * Nobobuhiro Iwamatsu <iwamatsu@nigauri.org>
4 *
5 * (C) Copyright 2003
6 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +09007 *
8 * See file CREDITS for list of people who contributed to this
9 * project.
10 *
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License as
13 * published by the Free Software Foundation; either version 2 of
14 * the License, or (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
24 * MA 02111-1307 USA
25 */
26
27#include <common.h>
Nobuhiro Iwamatsu9e23fe02008-07-08 12:03:24 +090028#include <asm/processor.h>
Nobuhiro Iwamatsue9d5f352008-11-20 16:44:42 +090029#include <asm/io.h>
30
31#define TMU_MAX_COUNTER (~0UL)
32static int clk_adj = 1;
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +090033
34static void tmu_timer_start (unsigned int timer)
35{
36 if (timer > 2)
37 return;
Nobuhiro Iwamatsue9d5f352008-11-20 16:44:42 +090038 writeb(readb(TSTR) | (1 << timer), TSTR);
39}
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +090040
Nobuhiro Iwamatsue9d5f352008-11-20 16:44:42 +090041static void tmu_timer_stop (unsigned int timer)
42{
43 if (timer > 2)
44 return;
45 writeb(readb(TSTR) & ~(1 << timer), TSTR);
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +090046}
47
48int timer_init (void)
49{
Nobuhiro Iwamatsue9d5f352008-11-20 16:44:42 +090050 /* Divide clock by TMU_CLK_DIVIDER */
51 u16 bit = 0;
52 switch( TMU_CLK_DIVIDER ){
53 case 4:
54 bit = 0;
55 break;
56 case 16:
57 bit = 1;
58 break;
59 case 64: bit = 2;
60 break;
61 case 256:
62 bit = 3;
63 break;
64 case 1024:
65 bit = 4;
66 break;
67 default:
68 bit = 0;
69 break;
70 }
71 writew(readw(TCR0) | bit, TCR0);
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +090072
Nobuhiro Iwamatsue9d5f352008-11-20 16:44:42 +090073 /* Clock adjustment calc */
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
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +090081 return 0;
82}
83
84unsigned long long get_ticks (void)
85{
Nobuhiro Iwamatsue9d5f352008-11-20 16:44:42 +090086 return 0 - readl(TCNT0);
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +090087}
88
Nobuhiro Iwamatsue9d5f352008-11-20 16:44:42 +090089static unsigned long get_usec (void)
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +090090{
Nobuhiro Iwamatsue9d5f352008-11-20 16:44:42 +090091 return (0 - readl(TCNT0));
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +090092}
93
94void udelay (unsigned long usec)
95{
Nobuhiro Iwamatsue9d5f352008-11-20 16:44:42 +090096 unsigned int start = get_usec();
97 unsigned int end = start + (usec * clk_adj);
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +090098
Nobuhiro Iwamatsue9d5f352008-11-20 16:44:42 +090099 while (get_usec() < end)
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900100 continue;
101}
102
Nobuhiro Iwamatsue9d5f352008-11-20 16:44:42 +0900103unsigned long get_timer (unsigned long base)
104{
105 /* return msec */
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
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900121unsigned long get_tbclk (void)
122{
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200123 return CONFIG_SYS_HZ;
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900124}