blob: bc1656f73ce535685e6563433f94ad0aa7d62a38 [file] [log] [blame]
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +09001/*
Jean-Christophe PLAGNIOL-VILLARD51704102009-06-04 12:06:47 +02002 * (C) Copyright 2009
3 * Jean-Christophe PLAGNIOL-VILLARD <plagnioj@jcrosoft.com>
4 *
Nobuhiro Iwamatsub633dd12010-06-16 08:10:21 +09005 * (C) Copyright 2007-2010
Nobuhiro Iwamatsuac890472008-11-20 16:44:42 +09006 * Nobobuhiro Iwamatsu <iwamatsu@nigauri.org>
7 *
8 * (C) Copyright 2003
9 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +090010 *
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>
Jean-Christophe PLAGNIOL-VILLARD51704102009-06-04 12:06:47 +020031#include <div64.h>
Nobuhiro Iwamatsu01213252008-07-08 12:03:24 +090032#include <asm/processor.h>
Jean-Christophe PLAGNIOL-VILLARD51704102009-06-04 12:06:47 +020033#include <asm/clk.h>
Nobuhiro Iwamatsuac890472008-11-20 16:44:42 +090034#include <asm/io.h>
35
36#define TMU_MAX_COUNTER (~0UL)
Jean-Christophe PLAGNIOL-VILLARD51704102009-06-04 12:06:47 +020037
38static ulong timer_freq;
Nobuhiro Iwamatsub633dd12010-06-16 08:10:21 +090039static unsigned long last_tcnt;
40static unsigned long long overflow_ticks;
Jean-Christophe PLAGNIOL-VILLARD51704102009-06-04 12:06:47 +020041
42static inline unsigned long long tick_to_time(unsigned long long tick)
43{
44 tick *= CONFIG_SYS_HZ;
45 do_div(tick, timer_freq);
46
47 return tick;
48}
49
50static inline unsigned long long usec_to_tick(unsigned long long usec)
51{
52 usec *= timer_freq;
53 do_div(usec, 1000000);
54
55 return usec;
56}
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +090057
58static void tmu_timer_start (unsigned int timer)
59{
60 if (timer > 2)
61 return;
Nobuhiro Iwamatsuac890472008-11-20 16:44:42 +090062 writeb(readb(TSTR) | (1 << timer), TSTR);
63}
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +090064
Nobuhiro Iwamatsuac890472008-11-20 16:44:42 +090065static void tmu_timer_stop (unsigned int timer)
66{
67 if (timer > 2)
68 return;
69 writeb(readb(TSTR) & ~(1 << timer), TSTR);
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +090070}
71
72int timer_init (void)
73{
Jean-Christophe PLAGNIOL-VILLARD32e6acc2009-06-04 12:06:48 +020074 /* Divide clock by CONFIG_SYS_TMU_CLK_DIV */
Nobuhiro Iwamatsuac890472008-11-20 16:44:42 +090075 u16 bit = 0;
Jean-Christophe PLAGNIOL-VILLARD4b137182008-12-20 15:25:22 +010076
Jean-Christophe PLAGNIOL-VILLARD32e6acc2009-06-04 12:06:48 +020077 switch (CONFIG_SYS_TMU_CLK_DIV) {
Jean-Christophe PLAGNIOL-VILLARD4b137182008-12-20 15:25:22 +010078 case 1024:
79 bit = 4;
Nobuhiro Iwamatsuac890472008-11-20 16:44:42 +090080 break;
81 case 256:
82 bit = 3;
83 break;
Jean-Christophe PLAGNIOL-VILLARD4b137182008-12-20 15:25:22 +010084 case 64:
85 bit = 2;
Nobuhiro Iwamatsuac890472008-11-20 16:44:42 +090086 break;
Jean-Christophe PLAGNIOL-VILLARD4b137182008-12-20 15:25:22 +010087 case 16:
88 bit = 1;
89 break;
90 case 4:
Nobuhiro Iwamatsuac890472008-11-20 16:44:42 +090091 default:
Nobuhiro Iwamatsuac890472008-11-20 16:44:42 +090092 break;
93 }
94 writew(readw(TCR0) | bit, TCR0);
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +090095
Jean-Christophe PLAGNIOL-VILLARD51704102009-06-04 12:06:47 +020096 /* Calc clock rate */
97 timer_freq = get_tmu0_clk_rate() >> ((bit + 1) * 2);
Nobuhiro Iwamatsuac890472008-11-20 16:44:42 +090098
99 tmu_timer_stop(0);
100 tmu_timer_start(0);
101
Nobuhiro Iwamatsub633dd12010-06-16 08:10:21 +0900102 last_tcnt = 0;
103 overflow_ticks = 0;
104
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900105 return 0;
106}
107
108unsigned long long get_ticks (void)
109{
Nobuhiro Iwamatsub633dd12010-06-16 08:10:21 +0900110 unsigned long tcnt = 0 - readl(TCNT0);
111 unsigned long ticks;
112
113 if (last_tcnt > tcnt) { /* overflow */
114 overflow_ticks++;
115 ticks = (0xffffffff - last_tcnt) + tcnt;
116 } else {
117 ticks = tcnt;
118 }
119 last_tcnt = tcnt;
120
121 return (overflow_ticks << 32) | tcnt;
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900122}
123
Ingo van Lilf0f778a2009-11-24 14:09:21 +0100124void __udelay (unsigned long usec)
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900125{
Jean-Christophe PLAGNIOL-VILLARD51704102009-06-04 12:06:47 +0200126 unsigned long long tmp;
127 ulong tmo;
Nobuhiro Iwamatsuac890472008-11-20 16:44:42 +0900128
Jean-Christophe PLAGNIOL-VILLARD51704102009-06-04 12:06:47 +0200129 tmo = usec_to_tick(usec);
130 tmp = get_ticks() + tmo; /* get current timestamp */
131
132 while (get_ticks() < tmp) /* loop till event */
133 /*NOP*/;
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900134}
135
Nobuhiro Iwamatsuac890472008-11-20 16:44:42 +0900136unsigned long get_timer (unsigned long base)
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900137{
Jean-Christophe PLAGNIOL-VILLARD4b137182008-12-20 15:25:22 +0100138 /* return msec */
Jean-Christophe PLAGNIOL-VILLARD51704102009-06-04 12:06:47 +0200139 return tick_to_time(get_ticks()) - base;
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900140}
141
Nobuhiro Iwamatsuac890472008-11-20 16:44:42 +0900142void set_timer (unsigned long t)
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900143{
Nobuhiro Iwamatsuac890472008-11-20 16:44:42 +0900144 writel((0 - t), TCNT0);
145}
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900146
Nobuhiro Iwamatsuac890472008-11-20 16:44:42 +0900147void reset_timer (void)
148{
149 tmu_timer_stop(0);
150 set_timer (0);
151 tmu_timer_start(0);
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900152}
153
154unsigned long get_tbclk (void)
155{
Jean-Christophe PLAGNIOL-VILLARD51704102009-06-04 12:06:47 +0200156 return timer_freq;
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900157}