blob: f6be3b94a4f3b969570be04231da6427b77aafd8 [file] [log] [blame]
Sascha Hauer1a7676f2008-03-26 20:40:42 +01001/*
2 * (C) Copyright 2007
3 * Sascha Hauer, Pengutronix
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/arch/mx31-regs.h>
Tomohiro Masubuchi9a3393b2008-10-21 13:17:16 +090026#include <div64.h>
Sascha Hauer1a7676f2008-03-26 20:40:42 +010027
28#define TIMER_BASE 0x53f90000 /* General purpose timer 1 */
29
30/* General purpose timers registers */
Guennadi Liakhovetski4771a4c2008-09-25 20:54:37 +020031#define GPTCR __REG(TIMER_BASE) /* Control register */
32#define GPTPR __REG(TIMER_BASE + 0x4) /* Prescaler register */
33#define GPTSR __REG(TIMER_BASE + 0x8) /* Status register */
34#define GPTCNT __REG(TIMER_BASE + 0x24) /* Counter register */
Sascha Hauer1a7676f2008-03-26 20:40:42 +010035
36/* General purpose timers bitfields */
Guennadi Liakhovetski4771a4c2008-09-25 20:54:37 +020037#define GPTCR_SWR (1 << 15) /* Software reset */
38#define GPTCR_FRR (1 << 9) /* Freerun / restart */
39#define GPTCR_CLKSOURCE_32 (4 << 6) /* Clock source */
40#define GPTCR_TEN 1 /* Timer enable */
41
Heiko Schocherf2015952010-12-09 22:01:15 +000042DECLARE_GLOBAL_DATA_PTR;
Tomohiro Masubuchi9a3393b2008-10-21 13:17:16 +090043
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020044/* "time" is measured in 1 / CONFIG_SYS_HZ seconds, "tick" is internal timer period */
Guennadi Liakhovetski4771a4c2008-09-25 20:54:37 +020045#ifdef CONFIG_MX31_TIMER_HIGH_PRECISION
46/* ~0.4% error - measured with stop-watch on 100s boot-delay */
Tomohiro Masubuchi9a3393b2008-10-21 13:17:16 +090047static inline unsigned long long tick_to_time(unsigned long long tick)
48{
49 tick *= CONFIG_SYS_HZ;
50 do_div(tick, CONFIG_MX31_CLK32);
51 return tick;
52}
53
54static inline unsigned long long time_to_tick(unsigned long long time)
55{
56 time *= CONFIG_MX31_CLK32;
57 do_div(time, CONFIG_SYS_HZ);
58 return time;
59}
60
61static inline unsigned long long us_to_tick(unsigned long long us)
62{
63 us = us * CONFIG_MX31_CLK32 + 999999;
64 do_div(us, 1000000);
65 return us;
66}
Guennadi Liakhovetski4771a4c2008-09-25 20:54:37 +020067#else
68/* ~2% error */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020069#define TICK_PER_TIME ((CONFIG_MX31_CLK32 + CONFIG_SYS_HZ / 2) / CONFIG_SYS_HZ)
Guennadi Liakhovetski4771a4c2008-09-25 20:54:37 +020070#define US_PER_TICK (1000000 / CONFIG_MX31_CLK32)
Sascha Hauer1a7676f2008-03-26 20:40:42 +010071
Tomohiro Masubuchi9a3393b2008-10-21 13:17:16 +090072static inline unsigned long long tick_to_time(unsigned long long tick)
73{
74 do_div(tick, TICK_PER_TIME);
75 return tick;
76}
77
78static inline unsigned long long time_to_tick(unsigned long long time)
79{
80 return time * TICK_PER_TIME;
81}
82
83static inline unsigned long long us_to_tick(unsigned long long us)
84{
85 us += US_PER_TICK - 1;
86 do_div(us, US_PER_TICK);
87 return us;
88}
89#endif
Magnus Lilja067a4f92008-08-29 10:36:17 +020090
Guennadi Liakhovetski4771a4c2008-09-25 20:54:37 +020091/* The 32768Hz 32-bit timer overruns in 131072 seconds */
Jean-Christophe PLAGNIOL-VILLARD8c9fc002009-05-15 23:47:02 +020092int timer_init (void)
Sascha Hauer1a7676f2008-03-26 20:40:42 +010093{
94 int i;
95
96 /* setup GP Timer 1 */
97 GPTCR = GPTCR_SWR;
Guennadi Liakhovetski4771a4c2008-09-25 20:54:37 +020098 for (i = 0; i < 100; i++)
99 GPTCR = 0; /* We have no udelay by now */
Sascha Hauer1a7676f2008-03-26 20:40:42 +0100100 GPTPR = 0; /* 32Khz */
Guennadi Liakhovetski4771a4c2008-09-25 20:54:37 +0200101 /* Freerun Mode, PERCLK1 input */
102 GPTCR |= GPTCR_CLKSOURCE_32 | GPTCR_TEN;
Sascha Hauer1a7676f2008-03-26 20:40:42 +0100103
104 return 0;
105}
106
107void reset_timer_masked (void)
108{
Magnus Lilja067a4f92008-08-29 10:36:17 +0200109 /* reset time */
Heiko Schocherf2015952010-12-09 22:01:15 +0000110 gd->lastinc = GPTCNT; /* capture current incrementer value time */
111 gd->tbl = 0; /* start "advancing" time stamp from 0 */
Sascha Hauer1a7676f2008-03-26 20:40:42 +0100112}
113
Magnus Lilja067a4f92008-08-29 10:36:17 +0200114void reset_timer(void)
115{
116 reset_timer_masked();
117}
118
Guennadi Liakhovetski4771a4c2008-09-25 20:54:37 +0200119unsigned long long get_ticks (void)
Sascha Hauer1a7676f2008-03-26 20:40:42 +0100120{
Magnus Lilja067a4f92008-08-29 10:36:17 +0200121 ulong now = GPTCNT; /* current tick value */
122
Heiko Schocherf2015952010-12-09 22:01:15 +0000123 if (now >= gd->lastinc) /* normal mode (non roll) */
Magnus Lilja067a4f92008-08-29 10:36:17 +0200124 /* move stamp forward with absolut diff ticks */
Heiko Schocherf2015952010-12-09 22:01:15 +0000125 gd->tbl += (now - gd->lastinc);
Magnus Lilja067a4f92008-08-29 10:36:17 +0200126 else /* we have rollover of incrementer */
Heiko Schocherf2015952010-12-09 22:01:15 +0000127 gd->tbl += (0xFFFFFFFF - gd->lastinc) + now;
128 gd->lastinc = now;
129 return gd->tbl;
Sascha Hauer1a7676f2008-03-26 20:40:42 +0100130}
131
Guennadi Liakhovetski4771a4c2008-09-25 20:54:37 +0200132ulong get_timer_masked (void)
133{
134 /*
135 * get_ticks() returns a long long (64 bit), it wraps in
136 * 2^64 / CONFIG_MX31_CLK32 = 2^64 / 2^15 = 2^49 ~ 5 * 10^14 (s) ~
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200137 * 5 * 10^9 days... and get_ticks() * CONFIG_SYS_HZ wraps in
Guennadi Liakhovetski4771a4c2008-09-25 20:54:37 +0200138 * 5 * 10^6 days - long enough.
139 */
Tomohiro Masubuchi9a3393b2008-10-21 13:17:16 +0900140 return tick_to_time(get_ticks());
Guennadi Liakhovetski4771a4c2008-09-25 20:54:37 +0200141}
142
Sascha Hauer1a7676f2008-03-26 20:40:42 +0100143ulong get_timer (ulong base)
144{
145 return get_timer_masked () - base;
146}
147
148void set_timer (ulong t)
149{
Heiko Schocherf2015952010-12-09 22:01:15 +0000150 gd->tbl = time_to_tick(t);
Sascha Hauer1a7676f2008-03-26 20:40:42 +0100151}
152
Wolfgang Denkd7cb3552010-05-21 23:13:18 +0200153/* delay x useconds AND preserve advance timestamp value */
Ingo van Lilf0f778a2009-11-24 14:09:21 +0100154void __udelay (unsigned long usec)
Sascha Hauer1a7676f2008-03-26 20:40:42 +0100155{
Guennadi Liakhovetski4771a4c2008-09-25 20:54:37 +0200156 unsigned long long tmp;
157 ulong tmo;
Sascha Hauer1a7676f2008-03-26 20:40:42 +0100158
Tomohiro Masubuchi9a3393b2008-10-21 13:17:16 +0900159 tmo = us_to_tick(usec);
Guennadi Liakhovetski4771a4c2008-09-25 20:54:37 +0200160 tmp = get_ticks() + tmo; /* get current timestamp */
Sascha Hauer1a7676f2008-03-26 20:40:42 +0100161
Guennadi Liakhovetski4771a4c2008-09-25 20:54:37 +0200162 while (get_ticks() < tmp) /* loop till event */
163 /*NOP*/;
Sascha Hauer1a7676f2008-03-26 20:40:42 +0100164}
165
166void reset_cpu (ulong addr)
167{
168 __REG16(WDOG_BASE) = 4;
169}