]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - arch/arm/cpu/arm926ejs/mx28/timer.c
Unified codebase for TX28, TX48, TX51, TX53
[karo-tx-uboot.git] / arch / arm / cpu / arm926ejs / mx28 / timer.c
1 /*
2  * Freescale i.MX28 timer driver
3  *
4  * Copyright (C) 2011 Marek Vasut <marek.vasut@gmail.com>
5  * on behalf of DENX Software Engineering GmbH
6  *
7  * Based on code from LTIB:
8  * (C) Copyright 2009-2010 Freescale Semiconductor, Inc.
9  *
10  * See file CREDITS for list of people who contributed to this
11  * project.
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License as
15  * published by the Free Software Foundation; either version 2 of
16  * the License, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software
25  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
26  * MA 02111-1307 USA
27  */
28
29 #include <common.h>
30 #include <asm/io.h>
31 #include <asm/arch/imx-regs.h>
32 #include <asm/arch/sys_proto.h>
33
34 /* Maximum fixed count */
35 #define TIMER_LOAD_VAL  0xffffffff
36
37 DECLARE_GLOBAL_DATA_PTR;
38
39 /* Enable this to verify that the code can correctly
40  * handle the timer rollover
41  */
42 /* #define DEBUG_TIMER_WRAP */
43
44 #ifdef DEBUG_TIMER_WRAP
45 /*
46  * Let the timer wrap 15 seconds after start to catch misbehaving
47  * timer related code early
48  */
49 #define TIMER_START             (-time_to_tick(15 * CONFIG_SYS_HZ))
50 #else
51 #define TIMER_START             0UL
52 #endif
53
54 /*
55  * This driver uses 1kHz clock source.
56  */
57 #define MX28_INCREMENTER_HZ             1000
58
59 static inline unsigned long tick_to_time(unsigned long tick)
60 {
61         return tick / (MX28_INCREMENTER_HZ / CONFIG_SYS_HZ);
62 }
63
64 static inline unsigned long time_to_tick(unsigned long time)
65 {
66         return time * (MX28_INCREMENTER_HZ / CONFIG_SYS_HZ);
67 }
68
69 int timer_init(void)
70 {
71         struct mx28_timrot_regs *timrot_regs =
72                 (struct mx28_timrot_regs *)MXS_TIMROT_BASE;
73
74         /* Reset Timers and Rotary Encoder module */
75         mx28_reset_block(&timrot_regs->hw_timrot_rotctrl_reg);
76
77         /* Set fixed_count to 0 */
78         writel(0, &timrot_regs->hw_timrot_fixed_count0);
79
80         /* Set UPDATE bit and 1Khz frequency */
81         writel(TIMROT_TIMCTRLn_UPDATE | TIMROT_TIMCTRLn_RELOAD |
82                 TIMROT_TIMCTRLn_SELECT_1KHZ_XTAL,
83                 &timrot_regs->hw_timrot_timctrl0);
84
85 #ifndef DEBUG_TIMER_WRAP
86         /* Set fixed_count to maximum value */
87         writel(TIMER_LOAD_VAL, &timrot_regs->hw_timrot_fixed_count0);
88 #else
89         /* Set fixed_count so that the counter will wrap after 20 seconds */
90         writel(20 * MX28_INCREMENTER_HZ,
91                 &timrot_regs->hw_timrot_fixed_count0);
92         gd->lastinc = TIMER_LOAD_VAL - 20 * MX28_INCREMENTER_HZ;
93 #endif
94 #ifdef DEBUG_TIMER_WRAP
95         /* Make the usec counter roll over 30 seconds after startup */
96         writel(-30000000, MX28_HW_DIGCTL_MICROSECONDS);
97 #endif
98         writel(TIMROT_TIMCTRLn_UPDATE,
99                 &timrot_regs->hw_timrot_timctrl0_clr);
100 #ifdef DEBUG_TIMER_WRAP
101         /* Set fixed_count to maximal value for subsequent loads */
102         writel(TIMER_LOAD_VAL, &timrot_regs->hw_timrot_fixed_count0);
103 #endif
104         gd->timer_rate_hz = MX28_INCREMENTER_HZ;
105         gd->tbl = TIMER_START;
106         gd->tbu = 0;
107         return 0;
108 }
109
110 /* We use the HW_DIGCTL_MICROSECONDS register for sub-millisecond timer. */
111 #define MX28_HW_DIGCTL_MICROSECONDS     0x8001c0c0
112
113 void __udelay(unsigned long usec)
114 {
115         uint32_t start = readl(MX28_HW_DIGCTL_MICROSECONDS);
116
117         while (readl(MX28_HW_DIGCTL_MICROSECONDS) - start <= usec)
118                 /* use '<=' to guarantee a delay of _at least_
119                  * the given number of microseconds.
120                  * No need for fancy rollover checks
121                  * Two's complement arithmetic applied correctly
122                  * does everything that's needed  automagically!
123                  */
124                 ;
125 }
126
127 /* Note: This function works correctly for TIMER_LOAD_VAL == 0xffffffff!
128  * The rollover is handled automagically due to the properties of
129  * two's complement arithmetic.
130  * For any other value of TIMER_LOAD_VAL the calculations would have
131  * to be done modulus(TIMER_LOAD_VAL + 1).
132  */
133 unsigned long long get_ticks(void)
134 {
135         struct mx28_timrot_regs *timrot_regs =
136                 (struct mx28_timrot_regs *)MXS_TIMROT_BASE;
137         /* The timer is counting down, so subtract the register value from
138          * the counter period length to get an incrementing timestamp
139          */
140         unsigned long now = -readl(&timrot_regs->hw_timrot_running_count0);
141         ulong inc = now - gd->lastinc;
142
143         gd->tbl += inc;
144         gd->lastinc = now;
145         /* Since the get_timer() function only uses a 32bit value
146          * it doesn't make sense to return a real 64 bit value here.
147          */
148         return gd->tbl;
149 }
150
151 ulong get_timer_masked(void)
152 {
153         return tick_to_time(get_ticks());
154 }
155
156 ulong get_timer(ulong base)
157 {
158         /* NOTE: time_to_tick(base) is required to correctly handle rollover! */
159         return tick_to_time(get_ticks() - time_to_tick(base));
160 }
161
162 /*
163  * This function is derived from PowerPC code (timebase clock frequency).
164  * On ARM it returns the number of timer ticks per second.
165  */
166 ulong get_tbclk(void)
167 {
168         return gd->timer_rate_hz;
169 }