2 * Freescale i.MX28 timer driver
4 * Copyright (C) 2011 Marek Vasut <marek.vasut@gmail.com>
5 * on behalf of DENX Software Engineering GmbH
7 * Based on code from LTIB:
8 * (C) Copyright 2009-2010 Freescale Semiconductor, Inc.
10 * See file CREDITS for list of people who contributed to this
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.
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.
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,
31 #include <asm/arch/imx-regs.h>
32 #include <asm/arch/sys_proto.h>
34 /* Maximum fixed count */
35 #define TIMER_LOAD_VAL 0xffffffff
37 DECLARE_GLOBAL_DATA_PTR;
39 /* Enable this to verify that the code can correctly
40 * handle the timer rollover
42 /* #define DEBUG_TIMER_WRAP */
44 #ifdef DEBUG_TIMER_WRAP
46 * Let the timer wrap 15 seconds after start to catch misbehaving
47 * timer related code early
49 #define TIMER_START (-time_to_tick(15 * CONFIG_SYS_HZ))
51 #define TIMER_START 0UL
55 * This driver uses 1kHz clock source.
57 #define MX28_INCREMENTER_HZ 1000
59 static inline unsigned long tick_to_time(unsigned long tick)
61 return tick / (MX28_INCREMENTER_HZ / CONFIG_SYS_HZ);
64 static inline unsigned long time_to_tick(unsigned long time)
66 return time * (MX28_INCREMENTER_HZ / CONFIG_SYS_HZ);
71 struct mx28_timrot_regs *timrot_regs =
72 (struct mx28_timrot_regs *)MXS_TIMROT_BASE;
74 /* Reset Timers and Rotary Encoder module */
75 mx28_reset_block(&timrot_regs->hw_timrot_rotctrl_reg);
77 /* Set fixed_count to 0 */
78 writel(0, &timrot_regs->hw_timrot_fixed_count0);
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);
85 #ifndef DEBUG_TIMER_WRAP
86 /* Set fixed_count to maximum value */
87 writel(TIMER_LOAD_VAL, &timrot_regs->hw_timrot_fixed_count0);
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;
94 #ifdef DEBUG_TIMER_WRAP
95 /* Make the usec counter roll over 30 seconds after startup */
96 writel(-30000000, MX28_HW_DIGCTL_MICROSECONDS);
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);
104 gd->timer_rate_hz = MX28_INCREMENTER_HZ;
105 gd->tbl = TIMER_START;
110 /* We use the HW_DIGCTL_MICROSECONDS register for sub-millisecond timer. */
111 #define MX28_HW_DIGCTL_MICROSECONDS 0x8001c0c0
113 void __udelay(unsigned long usec)
115 uint32_t start = readl(MX28_HW_DIGCTL_MICROSECONDS);
116 while (readl(MX28_HW_DIGCTL_MICROSECONDS) - start < usec)
117 /* No need for fancy rollover checks
118 * Two's complement arithmetic applied correctly
119 * does everything that's needed automagically!
124 /* Note: This function works correctly for TIMER_LOAD_VAL == 0xffffffff!
125 * The rollover is handled automagically due to the properties of
126 * two's complement arithmetic.
127 * For any other value of TIMER_LOAD_VAL the calculations would have
128 * to be done modulus(TIMER_LOAD_VAL + 1).
130 unsigned long long get_ticks(void)
132 struct mx28_timrot_regs *timrot_regs =
133 (struct mx28_timrot_regs *)MXS_TIMROT_BASE;
134 /* The timer is counting down, so subtract the register value from
135 * the counter period length to get an incrementing timestamp
137 unsigned long now = -readl(&timrot_regs->hw_timrot_running_count0);
138 ulong inc = now - gd->lastinc;
142 /* Since the get_timer() function only uses a 32bit value
143 * it doesn't make sense to return a real 64 bit value here.
148 ulong get_timer_masked(void)
150 return tick_to_time(get_ticks());
153 ulong get_timer(ulong base)
155 /* NOTE: time_to_tick(base) is required to correctly handle rollover! */
156 return tick_to_time(get_ticks() - time_to_tick(base));
160 * This function is derived from PowerPC code (timebase clock frequency).
161 * On ARM it returns the number of timer ticks per second.
163 ulong get_tbclk(void)
165 return gd->timer_rate_hz;