blob: f494440094e49b4f70ef2fa898ed86f2623dddf5 [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>
Stefano Babic78129d92011-03-14 15:43:56 +010025#include <asm/arch/imx-regs.h>
Tomohiro Masubuchi9a3393b2008-10-21 13:17:16 +090026#include <div64.h>
Stefano Babice17e3312011-02-02 00:49:36 +000027#include <watchdog.h>
28#include <asm/io.h>
Sascha Hauer1a7676f2008-03-26 20:40:42 +010029
30#define TIMER_BASE 0x53f90000 /* General purpose timer 1 */
31
32/* General purpose timers registers */
Guennadi Liakhovetski4771a4c2008-09-25 20:54:37 +020033#define GPTCR __REG(TIMER_BASE) /* Control register */
34#define GPTPR __REG(TIMER_BASE + 0x4) /* Prescaler register */
35#define GPTSR __REG(TIMER_BASE + 0x8) /* Status register */
36#define GPTCNT __REG(TIMER_BASE + 0x24) /* Counter register */
Sascha Hauer1a7676f2008-03-26 20:40:42 +010037
38/* General purpose timers bitfields */
Guennadi Liakhovetski4771a4c2008-09-25 20:54:37 +020039#define GPTCR_SWR (1 << 15) /* Software reset */
40#define GPTCR_FRR (1 << 9) /* Freerun / restart */
41#define GPTCR_CLKSOURCE_32 (4 << 6) /* Clock source */
42#define GPTCR_TEN 1 /* Timer enable */
43
Heiko Schocherf2015952010-12-09 22:01:15 +000044DECLARE_GLOBAL_DATA_PTR;
Tomohiro Masubuchi9a3393b2008-10-21 13:17:16 +090045
Fabio Estevamf231efb2011-10-13 05:34:59 +000046/*
47 * "time" is measured in 1 / CONFIG_SYS_HZ seconds,
48 * "tick" is internal timer period
49 */
50
Guennadi Liakhovetski4771a4c2008-09-25 20:54:37 +020051#ifdef CONFIG_MX31_TIMER_HIGH_PRECISION
52/* ~0.4% error - measured with stop-watch on 100s boot-delay */
Tomohiro Masubuchi9a3393b2008-10-21 13:17:16 +090053static inline unsigned long long tick_to_time(unsigned long long tick)
54{
55 tick *= CONFIG_SYS_HZ;
56 do_div(tick, CONFIG_MX31_CLK32);
57 return tick;
58}
59
60static inline unsigned long long time_to_tick(unsigned long long time)
61{
62 time *= CONFIG_MX31_CLK32;
63 do_div(time, CONFIG_SYS_HZ);
64 return time;
65}
66
67static inline unsigned long long us_to_tick(unsigned long long us)
68{
69 us = us * CONFIG_MX31_CLK32 + 999999;
70 do_div(us, 1000000);
71 return us;
72}
Guennadi Liakhovetski4771a4c2008-09-25 20:54:37 +020073#else
74/* ~2% error */
Fabio Estevamf231efb2011-10-13 05:34:59 +000075#define TICK_PER_TIME ((CONFIG_MX31_CLK32 + CONFIG_SYS_HZ / 2) \
76 / CONFIG_SYS_HZ)
Guennadi Liakhovetski4771a4c2008-09-25 20:54:37 +020077#define US_PER_TICK (1000000 / CONFIG_MX31_CLK32)
Sascha Hauer1a7676f2008-03-26 20:40:42 +010078
Tomohiro Masubuchi9a3393b2008-10-21 13:17:16 +090079static inline unsigned long long tick_to_time(unsigned long long tick)
80{
81 do_div(tick, TICK_PER_TIME);
82 return tick;
83}
84
85static inline unsigned long long time_to_tick(unsigned long long time)
86{
87 return time * TICK_PER_TIME;
88}
89
90static inline unsigned long long us_to_tick(unsigned long long us)
91{
92 us += US_PER_TICK - 1;
93 do_div(us, US_PER_TICK);
94 return us;
95}
96#endif
Magnus Lilja067a4f92008-08-29 10:36:17 +020097
Guennadi Liakhovetski4771a4c2008-09-25 20:54:37 +020098/* The 32768Hz 32-bit timer overruns in 131072 seconds */
Fabio Estevamf231efb2011-10-13 05:34:59 +000099int timer_init(void)
Sascha Hauer1a7676f2008-03-26 20:40:42 +0100100{
101 int i;
102
103 /* setup GP Timer 1 */
104 GPTCR = GPTCR_SWR;
Guennadi Liakhovetski4771a4c2008-09-25 20:54:37 +0200105 for (i = 0; i < 100; i++)
106 GPTCR = 0; /* We have no udelay by now */
Sascha Hauer1a7676f2008-03-26 20:40:42 +0100107 GPTPR = 0; /* 32Khz */
Guennadi Liakhovetski4771a4c2008-09-25 20:54:37 +0200108 /* Freerun Mode, PERCLK1 input */
109 GPTCR |= GPTCR_CLKSOURCE_32 | GPTCR_TEN;
Sascha Hauer1a7676f2008-03-26 20:40:42 +0100110
111 return 0;
112}
113
Fabio Estevamf231efb2011-10-13 05:34:59 +0000114unsigned long long get_ticks(void)
Sascha Hauer1a7676f2008-03-26 20:40:42 +0100115{
Magnus Lilja067a4f92008-08-29 10:36:17 +0200116 ulong now = GPTCNT; /* current tick value */
117
Heiko Schocherf2015952010-12-09 22:01:15 +0000118 if (now >= gd->lastinc) /* normal mode (non roll) */
Magnus Lilja067a4f92008-08-29 10:36:17 +0200119 /* move stamp forward with absolut diff ticks */
Heiko Schocherf2015952010-12-09 22:01:15 +0000120 gd->tbl += (now - gd->lastinc);
Magnus Lilja067a4f92008-08-29 10:36:17 +0200121 else /* we have rollover of incrementer */
Heiko Schocherf2015952010-12-09 22:01:15 +0000122 gd->tbl += (0xFFFFFFFF - gd->lastinc) + now;
123 gd->lastinc = now;
124 return gd->tbl;
Sascha Hauer1a7676f2008-03-26 20:40:42 +0100125}
126
Fabio Estevamf231efb2011-10-13 05:34:59 +0000127ulong get_timer_masked(void)
Guennadi Liakhovetski4771a4c2008-09-25 20:54:37 +0200128{
129 /*
130 * get_ticks() returns a long long (64 bit), it wraps in
131 * 2^64 / CONFIG_MX31_CLK32 = 2^64 / 2^15 = 2^49 ~ 5 * 10^14 (s) ~
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200132 * 5 * 10^9 days... and get_ticks() * CONFIG_SYS_HZ wraps in
Guennadi Liakhovetski4771a4c2008-09-25 20:54:37 +0200133 * 5 * 10^6 days - long enough.
134 */
Tomohiro Masubuchi9a3393b2008-10-21 13:17:16 +0900135 return tick_to_time(get_ticks());
Guennadi Liakhovetski4771a4c2008-09-25 20:54:37 +0200136}
137
Fabio Estevamf231efb2011-10-13 05:34:59 +0000138ulong get_timer(ulong base)
Sascha Hauer1a7676f2008-03-26 20:40:42 +0100139{
Fabio Estevamf231efb2011-10-13 05:34:59 +0000140 return get_timer_masked() - base;
Sascha Hauer1a7676f2008-03-26 20:40:42 +0100141}
142
Wolfgang Denkd7cb3552010-05-21 23:13:18 +0200143/* delay x useconds AND preserve advance timestamp value */
Fabio Estevamf231efb2011-10-13 05:34:59 +0000144void __udelay(unsigned long usec)
Sascha Hauer1a7676f2008-03-26 20:40:42 +0100145{
Guennadi Liakhovetski4771a4c2008-09-25 20:54:37 +0200146 unsigned long long tmp;
147 ulong tmo;
Sascha Hauer1a7676f2008-03-26 20:40:42 +0100148
Tomohiro Masubuchi9a3393b2008-10-21 13:17:16 +0900149 tmo = us_to_tick(usec);
Guennadi Liakhovetski4771a4c2008-09-25 20:54:37 +0200150 tmp = get_ticks() + tmo; /* get current timestamp */
Sascha Hauer1a7676f2008-03-26 20:40:42 +0100151
Guennadi Liakhovetski4771a4c2008-09-25 20:54:37 +0200152 while (get_ticks() < tmp) /* loop till event */
153 /*NOP*/;
Sascha Hauer1a7676f2008-03-26 20:40:42 +0100154}
155
Fabio Estevamf231efb2011-10-13 05:34:59 +0000156void reset_cpu(ulong addr)
Sascha Hauer1a7676f2008-03-26 20:40:42 +0100157{
Stefano Babice17e3312011-02-02 00:49:36 +0000158 struct wdog_regs *wdog = (struct wdog_regs *)WDOG_BASE;
159 wdog->wcr = WDOG_ENABLE;
160 while (1)
161 ;
Sascha Hauer1a7676f2008-03-26 20:40:42 +0100162}
Stefano Babice17e3312011-02-02 00:49:36 +0000163
164#ifdef CONFIG_HW_WATCHDOG
165void mxc_hw_watchdog_enable(void)
166{
167 struct wdog_regs *wdog = (struct wdog_regs *)WDOG_BASE;
168 u16 secs;
169
170 /*
171 * The timer watchdog can be set between
172 * 0.5 and 128 Seconds. If not defined
173 * in configuration file, sets 64 Seconds
174 */
175#ifdef CONFIG_SYS_WD_TIMER_SECS
176 secs = (CONFIG_SYS_WD_TIMER_SECS << 1) & 0xFF;
177 if (!secs) secs = 1;
178#else
179 secs = 64;
180#endif
Fabio Estevama335af92011-09-21 03:29:17 +0000181 setbits_le16(&wdog->wcr, (secs << WDOG_WT_SHIFT) | WDOG_ENABLE
182 | WDOG_WDZST);
Stefano Babice17e3312011-02-02 00:49:36 +0000183}
184
185
186void mxc_hw_watchdog_reset(void)
187{
188 struct wdog_regs *wdog = (struct wdog_regs *)WDOG_BASE;
189
190 writew(0x5555, &wdog->wsr);
191 writew(0xAAAA, &wdog->wsr);
192}
193#endif