3 * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
4 * Marius Groeger <mgroeger@sysgo.de>
7 * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
8 * Alex Zuepke <azu@sysgo.de>
11 * Gary Jennejohn, DENX Software Engineering, <gj@denx.de>
14 * Ilya Yanok, Emcraft Systems Ltd, <yanok@emcraft.com>
16 * See file CREDITS for list of people who contributed to this
19 * This program is free software; you can redistribute it and/or
20 * modify it under the terms of the GNU General Public License as
21 * published by the Free Software Foundation; either version 2 of
22 * the License, or (at your option) any later version.
24 * This program is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * GNU General Public License for more details.
29 * You should have received a copy of the GNU General Public License
30 * along with this program; if not, write to the Free Software
31 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
38 #include <asm/arch/imx-regs.h>
40 /* General purpose timers bitfields */
41 #define GPTCR_SWR (1 << 15) /* Software reset */
42 #define GPTCR_FRR (1 << 8) /* Freerun / restart */
43 #define GPTCR_CLKSOURCE_32 (4 << 1) /* Clock source */
44 #define GPTCR_TEN 1 /* Timer enable */
46 static ulong timestamp;
50 * "time" is measured in 1 / CONFIG_SYS_HZ seconds,
51 * "tick" is internal timer period
53 #ifdef CONFIG_MX27_TIMER_HIGH_PRECISION
54 /* ~0.4% error - measured with stop-watch on 100s boot-delay */
55 static inline unsigned long long tick_to_time(unsigned long long tick)
57 tick *= CONFIG_SYS_HZ;
58 do_div(tick, CONFIG_MX27_CLK32);
62 static inline unsigned long long time_to_tick(unsigned long long time)
64 time *= CONFIG_MX27_CLK32;
65 do_div(time, CONFIG_SYS_HZ);
69 static inline unsigned long long us_to_tick(unsigned long long us)
71 us = us * CONFIG_MX27_CLK32 + 999999;
77 #define TICK_PER_TIME ((CONFIG_MX27_CLK32 + CONFIG_SYS_HZ / 2) / \
79 #define US_PER_TICK (1000000 / CONFIG_MX27_CLK32)
81 static inline unsigned long long tick_to_time(unsigned long long tick)
83 do_div(tick, TICK_PER_TIME);
87 static inline unsigned long long time_to_tick(unsigned long long time)
89 return time * TICK_PER_TIME;
92 static inline unsigned long long us_to_tick(unsigned long long us)
94 us += US_PER_TICK - 1;
95 do_div(us, US_PER_TICK);
100 /* nothing really to do with interrupts, just starts up a counter. */
101 /* The 32768Hz 32-bit timer overruns in 131072 seconds */
105 struct gpt_regs *regs = (struct gpt_regs *)IMX_TIM1_BASE;
106 struct pll_regs *pll = (struct pll_regs *)IMX_PLL_BASE;
108 /* setup GP Timer 1 */
109 writel(GPTCR_SWR, ®s->gpt_tctl);
111 writel(readl(&pll->pccr0) | PCCR0_GPT1_EN, &pll->pccr0);
112 writel(readl(&pll->pccr1) | PCCR1_PERCLK1_EN, &pll->pccr1);
114 for (i = 0; i < 100; i++)
115 writel(0, ®s->gpt_tctl); /* We have no udelay by now */
116 writel(0, ®s->gpt_tprer); /* 32Khz */
117 /* Freerun Mode, PERCLK1 input */
118 writel(readl(®s->gpt_tctl) | GPTCR_CLKSOURCE_32 | GPTCR_FRR,
120 writel(readl(®s->gpt_tctl) | GPTCR_TEN, ®s->gpt_tctl);
125 void reset_timer_masked(void)
127 struct gpt_regs *regs = (struct gpt_regs *)IMX_TIM1_BASE;
129 /* capture current incrementer value time */
130 lastinc = readl(®s->gpt_tcn);
131 timestamp = 0; /* start "advancing" time stamp from 0 */
134 void reset_timer(void)
136 reset_timer_masked();
139 unsigned long long get_ticks (void)
141 struct gpt_regs *regs = (struct gpt_regs *)IMX_TIM1_BASE;
142 ulong now = readl(®s->gpt_tcn); /* current tick value */
144 if (now >= lastinc) {
146 * normal mode (non roll)
147 * move stamp forward with absolut diff ticks
149 timestamp += (now - lastinc);
151 /* we have rollover of incrementer */
152 timestamp += (0xFFFFFFFF - lastinc) + now;
158 ulong get_timer_masked (void)
161 * get_ticks() returns a long long (64 bit), it wraps in
162 * 2^64 / CONFIG_MX27_CLK32 = 2^64 / 2^15 = 2^49 ~ 5 * 10^14 (s) ~
163 * 5 * 10^9 days... and get_ticks() * CONFIG_SYS_HZ wraps in
164 * 5 * 10^6 days - long enough.
166 return tick_to_time(get_ticks());
169 ulong get_timer (ulong base)
171 return get_timer_masked () - base;
174 void set_timer (ulong t)
176 timestamp = time_to_tick(t);
179 /* delay x useconds AND preserve advance timstamp value */
180 void udelay (unsigned long usec)
182 unsigned long long tmp;
185 tmo = us_to_tick(usec);
186 tmp = get_ticks() + tmo; /* get current timestamp */
188 while (get_ticks() < tmp) /* loop till event */