]>
git.ipfire.org Git - people/ms/u-boot.git/blob - arch/arm/cpu/arm926ejs/mx25/timer.c
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 * (C) Copyright 2009 DENX Software Engineering
17 * Author: John Rigby <jrigby@gmail.com>
18 * Add support for MX25
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 DECLARE_GLOBAL_DATA_PTR
;
46 #define timestamp gd->tbl
47 #define lastinc gd->lastinc
50 * "time" is measured in 1 / CONFIG_SYS_HZ seconds,
51 * "tick" is internal timer period
53 #ifdef CONFIG_MX25_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_MX25_CLK32
);
62 static inline unsigned long long time_to_tick(unsigned long long time
)
64 time
*= CONFIG_MX25_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_MX25_CLK32
+ 999999;
77 #define TICK_PER_TIME ((CONFIG_MX25_CLK32 + CONFIG_SYS_HZ / 2) / \
79 #define US_PER_TICK (1000000 / CONFIG_MX25_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 32KHz 32-bit timer overruns in 134217 seconds */
105 struct gpt_regs
*gpt
= (struct gpt_regs
*)IMX_GPT1_BASE
;
106 struct ccm_regs
*ccm
= (struct ccm_regs
*)IMX_CCM_BASE
;
108 /* setup GP Timer 1 */
109 writel(GPT_CTRL_SWR
, &gpt
->ctrl
);
111 writel(readl(&ccm
->cgr1
) | CCM_CGR1_GPT1
, &ccm
->cgr1
);
113 for (i
= 0; i
< 100; i
++)
114 writel(0, &gpt
->ctrl
); /* We have no udelay by now */
115 writel(0, &gpt
->pre
); /* prescaler = 1 */
116 /* Freerun Mode, 32KHz input */
117 writel(readl(&gpt
->ctrl
) | GPT_CTRL_CLKSOURCE_32
| GPT_CTRL_FRR
,
119 writel(readl(&gpt
->ctrl
) | GPT_CTRL_TEN
, &gpt
->ctrl
);
124 unsigned long long get_ticks (void)
126 struct gpt_regs
*gpt
= (struct gpt_regs
*)IMX_GPT1_BASE
;
127 ulong now
= readl(&gpt
->counter
); /* current tick value */
129 if (now
>= lastinc
) {
131 * normal mode (non roll)
132 * move stamp forward with absolut diff ticks
134 timestamp
+= (now
- lastinc
);
136 /* we have rollover of incrementer */
137 timestamp
+= (0xFFFFFFFF - lastinc
) + now
;
143 ulong
get_timer_masked (void)
146 * get_ticks() returns a long long (64 bit), it wraps in
147 * 2^64 / CONFIG_MX25_CLK32 = 2^64 / 2^15 = 2^49 ~ 5 * 10^14 (s) ~
148 * 5 * 10^9 days... and get_ticks() * CONFIG_SYS_HZ wraps in
149 * 5 * 10^6 days - long enough.
151 return tick_to_time(get_ticks());
154 ulong
get_timer (ulong base
)
156 return get_timer_masked () - base
;
159 /* delay x useconds AND preserve advance timstamp value */
160 void __udelay (unsigned long usec
)
162 unsigned long long tmp
;
165 tmo
= us_to_tick(usec
);
166 tmp
= get_ticks() + tmo
; /* get current timestamp */
168 while (get_ticks() < tmp
) /* loop till event */
173 * This function is derived from PowerPC code (timebase clock frequency).
174 * On ARM it returns the number of timer ticks per second.
176 ulong
get_tbclk(void)
180 tbclk
= CONFIG_MX25_CLK32
;