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 * Sascha Hauer, Pengutronix
16 * (C) Copyright 2008-2009 Freescale Semiconductor, Inc.
18 * Ilya Yanok, Emcraft Systems Ltd, <yanok@emcraft.com>
20 * See file CREDITS for list of people who contributed to this
23 * This program is free software; you can redistribute it and/or
24 * modify it under the terms of the GNU General Public License as
25 * published by the Free Software Foundation; either version 2 of
26 * the License, or (at your option) any later version.
28 * This program is distributed in the hope that it will be useful,
29 * but WITHOUT ANY WARRANTY; without even the implied warranty of
30 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31 * GNU General Public License for more details.
33 * You should have received a copy of the GNU General Public License
34 * along with this program; if not, write to the Free Software
35 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
42 #include <asm/arch/imx-regs.h>
44 /* General purpose timers bitfields */
45 #define GPTCR_SWR (1 << 15) /* Software reset */
46 #define GPTCR_FRR (1 << 8) /* Freerun / restart */
47 #define GPTCR_CLKSOURCE_32 (4 << 1) /* Clock source */
48 #define GPTCR_TEN 1 /* Timer enable */
50 static ulong timestamp;
54 * "time" is measured in 1 / CONFIG_SYS_HZ seconds,
55 * "tick" is internal timer period
57 #ifdef CONFIG_MX27_TIMER_HIGH_PRECISION
58 /* ~0.4% error - measured with stop-watch on 100s boot-delay */
59 static inline unsigned long long tick_to_time(unsigned long long tick)
61 tick *= CONFIG_SYS_HZ;
62 do_div(tick, CONFIG_MX27_CLK32);
66 static inline unsigned long long time_to_tick(unsigned long long time)
68 time *= CONFIG_MX27_CLK32;
69 do_div(time, CONFIG_SYS_HZ);
73 static inline unsigned long long us_to_tick(unsigned long long us)
75 us = us * CONFIG_MX27_CLK32 + 999999;
81 #define TICK_PER_TIME ((CONFIG_MX27_CLK32 + CONFIG_SYS_HZ / 2) / \
83 #define US_PER_TICK (1000000 / CONFIG_MX27_CLK32)
85 static inline unsigned long long tick_to_time(unsigned long long tick)
87 do_div(tick, TICK_PER_TIME);
91 static inline unsigned long long time_to_tick(unsigned long long time)
93 return time * TICK_PER_TIME;
96 static inline unsigned long long us_to_tick(unsigned long long us)
98 us += US_PER_TICK - 1;
99 do_div(us, US_PER_TICK);
104 /* nothing really to do with interrupts, just starts up a counter. */
105 /* The 32768Hz 32-bit timer overruns in 131072 seconds */
109 struct gpt_regs *regs = (struct gpt_regs *)IMX_TIM1_BASE;
110 struct pll_regs *pll = (struct pll_regs *)IMX_PLL_BASE;
112 /* setup GP Timer 1 */
113 writel(GPTCR_SWR, ®s->gpt_tctl);
115 writel(readl(&pll->pccr0) | PCCR0_GPT1_EN, &pll->pccr0);
116 writel(readl(&pll->pccr1) | PCCR1_PERCLK1_EN, &pll->pccr1);
118 for (i = 0; i < 100; i++)
119 writel(0, ®s->gpt_tctl); /* We have no udelay by now */
120 writel(0, ®s->gpt_tprer); /* 32Khz */
121 /* Freerun Mode, PERCLK1 input */
122 writel(readl(®s->gpt_tctl) | GPTCR_CLKSOURCE_32 | GPTCR_FRR,
124 writel(readl(®s->gpt_tctl) | GPTCR_TEN, ®s->gpt_tctl);
128 void reset_timer_masked(void)
130 struct gpt_regs *regs = (struct gpt_regs *)IMX_TIM1_BASE;
132 /* capture current incrementer value time */
133 lastinc = readl(®s->gpt_tcn);
134 timestamp = 0; /* start "advancing" time stamp from 0 */
137 void reset_timer(void)
139 reset_timer_masked();
142 unsigned long long get_ticks (void)
144 struct gpt_regs *regs = (struct gpt_regs *)IMX_TIM1_BASE;
145 ulong now = readl(®s->gpt_tcn); /* current tick value */
147 if (now >= lastinc) {
149 * normal mode (non roll)
150 * move stamp forward with absolut diff ticks
152 timestamp += (now - lastinc);
154 /* we have rollover of incrementer */
155 timestamp += (0xFFFFFFFF - lastinc) + now;
161 ulong get_timer_masked (void)
164 * get_ticks() returns a long long (64 bit), it wraps in
165 * 2^64 / CONFIG_MX27_CLK32 = 2^64 / 2^15 = 2^49 ~ 5 * 10^14 (s) ~
166 * 5 * 10^9 days... and get_ticks() * CONFIG_SYS_HZ wraps in
167 * 5 * 10^6 days - long enough.
169 return tick_to_time(get_ticks());
172 ulong get_timer (ulong base)
174 return get_timer_masked () - base;
177 void set_timer (ulong t)
179 timestamp = time_to_tick(t);
182 /* delay x useconds AND preserve advance timstamp value */
183 void udelay (unsigned long usec)
185 unsigned long long tmp;
188 tmo = us_to_tick(usec);
189 tmp = get_ticks() + tmo; /* get current timestamp */
191 while (get_ticks() < tmp) /* loop till event */