blob: ab7202fef2ad881116edd041d9a4067838cf9330 [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
Tomohiro Masubuchi9a3393b2008-10-21 13:17:16 +090042static ulong timestamp;
43static ulong lastinc;
44
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020045/* "time" is measured in 1 / CONFIG_SYS_HZ seconds, "tick" is internal timer period */
Guennadi Liakhovetski4771a4c2008-09-25 20:54:37 +020046#ifdef CONFIG_MX31_TIMER_HIGH_PRECISION
47/* ~0.4% error - measured with stop-watch on 100s boot-delay */
Tomohiro Masubuchi9a3393b2008-10-21 13:17:16 +090048static inline unsigned long long tick_to_time(unsigned long long tick)
49{
50 tick *= CONFIG_SYS_HZ;
51 do_div(tick, CONFIG_MX31_CLK32);
52 return tick;
53}
54
55static inline unsigned long long time_to_tick(unsigned long long time)
56{
57 time *= CONFIG_MX31_CLK32;
58 do_div(time, CONFIG_SYS_HZ);
59 return time;
60}
61
62static inline unsigned long long us_to_tick(unsigned long long us)
63{
64 us = us * CONFIG_MX31_CLK32 + 999999;
65 do_div(us, 1000000);
66 return us;
67}
Guennadi Liakhovetski4771a4c2008-09-25 20:54:37 +020068#else
69/* ~2% error */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020070#define TICK_PER_TIME ((CONFIG_MX31_CLK32 + CONFIG_SYS_HZ / 2) / CONFIG_SYS_HZ)
Guennadi Liakhovetski4771a4c2008-09-25 20:54:37 +020071#define US_PER_TICK (1000000 / CONFIG_MX31_CLK32)
Sascha Hauer1a7676f2008-03-26 20:40:42 +010072
Tomohiro Masubuchi9a3393b2008-10-21 13:17:16 +090073static inline unsigned long long tick_to_time(unsigned long long tick)
74{
75 do_div(tick, TICK_PER_TIME);
76 return tick;
77}
78
79static inline unsigned long long time_to_tick(unsigned long long time)
80{
81 return time * TICK_PER_TIME;
82}
83
84static inline unsigned long long us_to_tick(unsigned long long us)
85{
86 us += US_PER_TICK - 1;
87 do_div(us, US_PER_TICK);
88 return us;
89}
90#endif
Magnus Lilja067a4f92008-08-29 10:36:17 +020091
Sascha Hauer1a7676f2008-03-26 20:40:42 +010092/* nothing really to do with interrupts, just starts up a counter. */
Guennadi Liakhovetski4771a4c2008-09-25 20:54:37 +020093/* The 32768Hz 32-bit timer overruns in 131072 seconds */
Sascha Hauer1a7676f2008-03-26 20:40:42 +010094int interrupt_init (void)
95{
96 int i;
97
98 /* setup GP Timer 1 */
99 GPTCR = GPTCR_SWR;
Guennadi Liakhovetski4771a4c2008-09-25 20:54:37 +0200100 for (i = 0; i < 100; i++)
101 GPTCR = 0; /* We have no udelay by now */
Sascha Hauer1a7676f2008-03-26 20:40:42 +0100102 GPTPR = 0; /* 32Khz */
Guennadi Liakhovetski4771a4c2008-09-25 20:54:37 +0200103 /* Freerun Mode, PERCLK1 input */
104 GPTCR |= GPTCR_CLKSOURCE_32 | GPTCR_TEN;
Sascha Hauer1a7676f2008-03-26 20:40:42 +0100105
106 return 0;
107}
108
109void reset_timer_masked (void)
110{
Magnus Lilja067a4f92008-08-29 10:36:17 +0200111 /* reset time */
112 lastinc = GPTCNT; /* capture current incrementer value time */
113 timestamp = 0; /* start "advancing" time stamp from 0 */
Sascha Hauer1a7676f2008-03-26 20:40:42 +0100114}
115
Magnus Lilja067a4f92008-08-29 10:36:17 +0200116void reset_timer(void)
117{
118 reset_timer_masked();
119}
120
Guennadi Liakhovetski4771a4c2008-09-25 20:54:37 +0200121unsigned long long get_ticks (void)
Sascha Hauer1a7676f2008-03-26 20:40:42 +0100122{
Magnus Lilja067a4f92008-08-29 10:36:17 +0200123 ulong now = GPTCNT; /* current tick value */
124
125 if (now >= lastinc) /* normal mode (non roll) */
126 /* move stamp forward with absolut diff ticks */
127 timestamp += (now - lastinc);
128 else /* we have rollover of incrementer */
129 timestamp += (0xFFFFFFFF - lastinc) + now;
130 lastinc = now;
131 return timestamp;
Sascha Hauer1a7676f2008-03-26 20:40:42 +0100132}
133
Guennadi Liakhovetski4771a4c2008-09-25 20:54:37 +0200134ulong get_timer_masked (void)
135{
136 /*
137 * get_ticks() returns a long long (64 bit), it wraps in
138 * 2^64 / CONFIG_MX31_CLK32 = 2^64 / 2^15 = 2^49 ~ 5 * 10^14 (s) ~
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200139 * 5 * 10^9 days... and get_ticks() * CONFIG_SYS_HZ wraps in
Guennadi Liakhovetski4771a4c2008-09-25 20:54:37 +0200140 * 5 * 10^6 days - long enough.
141 */
Tomohiro Masubuchi9a3393b2008-10-21 13:17:16 +0900142 return tick_to_time(get_ticks());
Guennadi Liakhovetski4771a4c2008-09-25 20:54:37 +0200143}
144
Sascha Hauer1a7676f2008-03-26 20:40:42 +0100145ulong get_timer (ulong base)
146{
147 return get_timer_masked () - base;
148}
149
150void set_timer (ulong t)
151{
Tomohiro Masubuchi9a3393b2008-10-21 13:17:16 +0900152 timestamp = time_to_tick(t);
Sascha Hauer1a7676f2008-03-26 20:40:42 +0100153}
154
155/* delay x useconds AND perserve advance timstamp value */
156void udelay (unsigned long usec)
157{
Guennadi Liakhovetski4771a4c2008-09-25 20:54:37 +0200158 unsigned long long tmp;
159 ulong tmo;
Sascha Hauer1a7676f2008-03-26 20:40:42 +0100160
Tomohiro Masubuchi9a3393b2008-10-21 13:17:16 +0900161 tmo = us_to_tick(usec);
Guennadi Liakhovetski4771a4c2008-09-25 20:54:37 +0200162 tmp = get_ticks() + tmo; /* get current timestamp */
Sascha Hauer1a7676f2008-03-26 20:40:42 +0100163
Guennadi Liakhovetski4771a4c2008-09-25 20:54:37 +0200164 while (get_ticks() < tmp) /* loop till event */
165 /*NOP*/;
Sascha Hauer1a7676f2008-03-26 20:40:42 +0100166}
167
168void reset_cpu (ulong addr)
169{
170 __REG16(WDOG_BASE) = 4;
171}