]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - cpu/arm926ejs/mx27/timer.c
imported Freescale specific U-Boot additions for i.MX28,... release L2.6.31_10.08.01
[karo-tx-uboot.git] / cpu / arm926ejs / mx27 / timer.c
1 /*
2  * (C) Copyright 2002
3  * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
4  * Marius Groeger <mgroeger@sysgo.de>
5  *
6  * (C) Copyright 2002
7  * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
8  * Alex Zuepke <azu@sysgo.de>
9  *
10  * (C) Copyright 2002
11  * Gary Jennejohn, DENX Software Engineering, <gj@denx.de>
12  *
13  * (C) Copyright 2007
14  * Sascha Hauer, Pengutronix
15  *
16  * (C) Copyright 2008-2009 Freescale Semiconductor, Inc.
17  * (C) Copyright 2009
18  * Ilya Yanok, Emcraft Systems Ltd, <yanok@emcraft.com>
19  *
20  * See file CREDITS for list of people who contributed to this
21  * project.
22  *
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.
27  *
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.
32  *
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,
36  * MA 02111-1307 USA
37  */
38
39 #include <common.h>
40 #include <div64.h>
41 #include <asm/io.h>
42 #include <asm/arch/imx-regs.h>
43
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         */
49
50 static ulong timestamp;
51 static ulong lastinc;
52
53 /*
54  * "time" is measured in 1 / CONFIG_SYS_HZ seconds,
55  * "tick" is internal timer period
56  */
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)
60 {
61         tick *= CONFIG_SYS_HZ;
62         do_div(tick, CONFIG_MX27_CLK32);
63         return tick;
64 }
65
66 static inline unsigned long long time_to_tick(unsigned long long time)
67 {
68         time *= CONFIG_MX27_CLK32;
69         do_div(time, CONFIG_SYS_HZ);
70         return time;
71 }
72
73 static inline unsigned long long us_to_tick(unsigned long long us)
74 {
75         us = us * CONFIG_MX27_CLK32 + 999999;
76         do_div(us, 1000000);
77         return us;
78 }
79 #else
80 /* ~2% error */
81 #define TICK_PER_TIME   ((CONFIG_MX27_CLK32 + CONFIG_SYS_HZ / 2) / \
82                 CONFIG_SYS_HZ)
83 #define US_PER_TICK     (1000000 / CONFIG_MX27_CLK32)
84
85 static inline unsigned long long tick_to_time(unsigned long long tick)
86 {
87         do_div(tick, TICK_PER_TIME);
88         return tick;
89 }
90
91 static inline unsigned long long time_to_tick(unsigned long long time)
92 {
93         return time * TICK_PER_TIME;
94 }
95
96 static inline unsigned long long us_to_tick(unsigned long long us)
97 {
98         us += US_PER_TICK - 1;
99         do_div(us, US_PER_TICK);
100         return us;
101 }
102 #endif
103
104 /* nothing really to do with interrupts, just starts up a counter. */
105 /* The 32768Hz 32-bit timer overruns in 131072 seconds */
106 int timer_init(void)
107 {
108         int i;
109         struct gpt_regs *regs = (struct gpt_regs *)IMX_TIM1_BASE;
110         struct pll_regs *pll = (struct pll_regs *)IMX_PLL_BASE;
111
112         /* setup GP Timer 1 */
113         writel(GPTCR_SWR, &regs->gpt_tctl);
114
115         writel(readl(&pll->pccr0) | PCCR0_GPT1_EN, &pll->pccr0);
116         writel(readl(&pll->pccr1) | PCCR1_PERCLK1_EN, &pll->pccr1);
117
118         for (i = 0; i < 100; i++)
119                 writel(0, &regs->gpt_tctl); /* We have no udelay by now */
120         writel(0, &regs->gpt_tprer); /* 32Khz */
121         /* Freerun Mode, PERCLK1 input */
122         writel(readl(&regs->gpt_tctl) | GPTCR_CLKSOURCE_32 | GPTCR_FRR,
123                         &regs->gpt_tctl);
124         writel(readl(&regs->gpt_tctl) | GPTCR_TEN, &regs->gpt_tctl);
125         return 0;
126 }
127
128 void reset_timer_masked(void)
129 {
130         struct gpt_regs *regs = (struct gpt_regs *)IMX_TIM1_BASE;
131         /* reset time */
132         /* capture current incrementer value time */
133         lastinc = readl(&regs->gpt_tcn);
134         timestamp = 0; /* start "advancing" time stamp from 0 */
135 }
136
137 void reset_timer(void)
138 {
139         reset_timer_masked();
140 }
141
142 unsigned long long get_ticks (void)
143 {
144         struct gpt_regs *regs = (struct gpt_regs *)IMX_TIM1_BASE;
145         ulong now = readl(&regs->gpt_tcn); /* current tick value */
146
147         if (now >= lastinc) {
148                 /*
149                  * normal mode (non roll)
150                  * move stamp forward with absolut diff ticks
151                  */
152                 timestamp += (now - lastinc);
153         } else {
154                 /* we have rollover of incrementer */
155                 timestamp += (0xFFFFFFFF - lastinc) + now;
156         }
157         lastinc = now;
158         return timestamp;
159 }
160
161 ulong get_timer_masked (void)
162 {
163         /*
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.
168          */
169         return tick_to_time(get_ticks());
170 }
171
172 ulong get_timer (ulong base)
173 {
174         return get_timer_masked () - base;
175 }
176
177 void set_timer (ulong t)
178 {
179         timestamp = time_to_tick(t);
180 }
181
182 /* delay x useconds AND preserve advance timstamp value */
183 void udelay (unsigned long usec)
184 {
185         unsigned long long tmp;
186         ulong tmo;
187
188         tmo = us_to_tick(usec);
189         tmp = get_ticks() + tmo;        /* get current timestamp */
190
191         while (get_ticks() < tmp)       /* loop till event */
192                  /*NOP*/;
193 }