1 // SPDX-License-Identifier: GPL-2.0+
3 * Freescale i.MX28 timer driver
5 * Copyright (C) 2011 Marek Vasut <marek.vasut@gmail.com>
6 * on behalf of DENX Software Engineering GmbH
8 * Based on code from LTIB:
9 * (C) Copyright 2009-2010 Freescale Semiconductor, Inc.
16 #include <asm/arch/imx-regs.h>
17 #include <asm/arch/sys_proto.h>
18 #include <linux/delay.h>
20 /* Maximum fixed count */
21 #if defined(CONFIG_MX23)
22 #define TIMER_LOAD_VAL 0xffff
23 #elif defined(CONFIG_MX28)
24 #define TIMER_LOAD_VAL 0xffffffff
27 DECLARE_GLOBAL_DATA_PTR;
29 #define timestamp (gd->arch.tbl)
30 #define lastdec (gd->arch.lastinc)
33 * This driver uses 1kHz clock source.
35 #define MXS_INCREMENTER_HZ 1000
37 static inline unsigned long tick_to_time(unsigned long tick)
39 return tick / (MXS_INCREMENTER_HZ / CONFIG_SYS_HZ);
42 static inline unsigned long time_to_tick(unsigned long time)
44 return time * (MXS_INCREMENTER_HZ / CONFIG_SYS_HZ);
47 /* Calculate how many ticks happen in "us" microseconds */
48 static inline unsigned long us_to_tick(unsigned long us)
50 return (us * MXS_INCREMENTER_HZ) / 1000000;
55 struct mxs_timrot_regs *timrot_regs =
56 (struct mxs_timrot_regs *)MXS_TIMROT_BASE;
58 /* Reset Timers and Rotary Encoder module */
59 mxs_reset_block(&timrot_regs->hw_timrot_rotctrl_reg);
61 /* Set fixed_count to 0 */
62 #if defined(CONFIG_MX23)
63 writel(0, &timrot_regs->hw_timrot_timcount0);
64 #elif defined(CONFIG_MX28)
65 writel(0, &timrot_regs->hw_timrot_fixed_count0);
68 /* Set UPDATE bit and 1Khz frequency */
69 writel(TIMROT_TIMCTRLn_UPDATE | TIMROT_TIMCTRLn_RELOAD |
70 TIMROT_TIMCTRLn_SELECT_1KHZ_XTAL,
71 &timrot_regs->hw_timrot_timctrl0);
73 /* Set fixed_count to maximal value */
74 #if defined(CONFIG_MX23)
75 writel(TIMER_LOAD_VAL - 1, &timrot_regs->hw_timrot_timcount0);
76 #elif defined(CONFIG_MX28)
77 writel(TIMER_LOAD_VAL, &timrot_regs->hw_timrot_fixed_count0);
83 unsigned long long get_ticks(void)
85 struct mxs_timrot_regs *timrot_regs =
86 (struct mxs_timrot_regs *)MXS_TIMROT_BASE;
89 /* Current tick value */
90 #if defined(CONFIG_MX23)
91 /* Upper bits are the valid ones. */
92 now = readl(&timrot_regs->hw_timrot_timcount0) >>
93 TIMROT_RUNNING_COUNTn_RUNNING_COUNT_OFFSET;
94 #elif defined(CONFIG_MX28)
95 now = readl(&timrot_regs->hw_timrot_running_count0);
97 #error "Don't know how to read timrot_regs"
100 if (lastdec >= now) {
102 * normal mode (non roll)
103 * move stamp forward with absolut diff ticks
105 timestamp += (lastdec - now);
107 /* we have rollover of decrementer */
108 timestamp += (TIMER_LOAD_VAL - now) + lastdec;
116 ulong get_timer(ulong base)
118 return tick_to_time(get_ticks()) - base;
121 /* We use the HW_DIGCTL_MICROSECONDS register for sub-millisecond timer. */
122 #define MXS_HW_DIGCTL_MICROSECONDS 0x8001c0c0
124 void __udelay(unsigned long usec)
126 uint32_t old, new, incr;
127 uint32_t counter = 0;
129 old = readl(MXS_HW_DIGCTL_MICROSECONDS);
131 while (counter < usec) {
132 new = readl(MXS_HW_DIGCTL_MICROSECONDS);
134 /* Check if the timer wrapped. */
136 incr = 0xffffffff - old;
143 * Check if we are close to the maximum time and the counter
144 * would wrap if incremented. If that's the case, break out
145 * from the loop as the requested delay time passed.
147 if (counter + incr < counter)
155 ulong get_tbclk(void)
157 return MXS_INCREMENTER_HZ;