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 DECLARE_GLOBAL_DATA_PTR;
48 #define timestamp gd->tbl
49 #define lastinc gd->lastinc
52 * "time" is measured in 1 / CONFIG_SYS_HZ seconds,
53 * "tick" is internal timer period
55 #ifdef CONFIG_MX27_TIMER_HIGH_PRECISION
56 /* ~0.4% error - measured with stop-watch on 100s boot-delay */
57 static inline unsigned long long tick_to_time(unsigned long long tick)
59 tick *= CONFIG_SYS_HZ;
60 do_div(tick, CONFIG_MX27_CLK32);
64 static inline unsigned long long time_to_tick(unsigned long long time)
66 time *= CONFIG_MX27_CLK32;
67 do_div(time, CONFIG_SYS_HZ);
71 static inline unsigned long long us_to_tick(unsigned long long us)
73 us = us * CONFIG_MX27_CLK32 + 999999;
79 #define TICK_PER_TIME ((CONFIG_MX27_CLK32 + CONFIG_SYS_HZ / 2) / \
81 #define US_PER_TICK (1000000 / CONFIG_MX27_CLK32)
83 static inline unsigned long long tick_to_time(unsigned long long tick)
85 do_div(tick, TICK_PER_TIME);
89 static inline unsigned long long time_to_tick(unsigned long long time)
91 return time * TICK_PER_TIME;
94 static inline unsigned long long us_to_tick(unsigned long long us)
96 us += US_PER_TICK - 1;
97 do_div(us, US_PER_TICK);
102 /* nothing really to do with interrupts, just starts up a counter. */
103 /* The 32768Hz 32-bit timer overruns in 131072 seconds */
107 struct gpt_regs *regs = (struct gpt_regs *)IMX_TIM1_BASE;
108 struct pll_regs *pll = (struct pll_regs *)IMX_PLL_BASE;
110 /* setup GP Timer 1 */
111 writel(GPTCR_SWR, ®s->gpt_tctl);
113 writel(readl(&pll->pccr0) | PCCR0_GPT1_EN, &pll->pccr0);
114 writel(readl(&pll->pccr1) | PCCR1_PERCLK1_EN, &pll->pccr1);
116 for (i = 0; i < 100; i++)
117 writel(0, ®s->gpt_tctl); /* We have no udelay by now */
118 writel(0, ®s->gpt_tprer); /* 32Khz */
119 /* Freerun Mode, PERCLK1 input */
120 writel(readl(®s->gpt_tctl) | GPTCR_CLKSOURCE_32 | GPTCR_FRR,
122 writel(readl(®s->gpt_tctl) | GPTCR_TEN, ®s->gpt_tctl);
127 void reset_timer_masked(void)
129 struct gpt_regs *regs = (struct gpt_regs *)IMX_TIM1_BASE;
131 /* capture current incrementer value time */
132 lastinc = readl(®s->gpt_tcn);
133 timestamp = 0; /* start "advancing" time stamp from 0 */
136 unsigned long long get_ticks (void)
138 struct gpt_regs *regs = (struct gpt_regs *)IMX_TIM1_BASE;
139 ulong now = readl(®s->gpt_tcn); /* current tick value */
141 if (now >= lastinc) {
143 * normal mode (non roll)
144 * move stamp forward with absolut diff ticks
146 timestamp += (now - lastinc);
148 /* we have rollover of incrementer */
149 timestamp += (0xFFFFFFFF - lastinc) + now;
155 ulong get_timer_masked (void)
158 * get_ticks() returns a long long (64 bit), it wraps in
159 * 2^64 / CONFIG_MX27_CLK32 = 2^64 / 2^15 = 2^49 ~ 5 * 10^14 (s) ~
160 * 5 * 10^9 days... and get_ticks() * CONFIG_SYS_HZ wraps in
161 * 5 * 10^6 days - long enough.
163 return tick_to_time(get_ticks());
166 ulong get_timer (ulong base)
168 return get_timer_masked () - base;
171 /* delay x useconds AND preserve advance timstamp value */
172 void __udelay (unsigned long usec)
174 unsigned long long tmp;
177 tmo = us_to_tick(usec);
178 tmp = get_ticks() + tmo; /* get current timestamp */
180 while (get_ticks() < tmp) /* loop till event */