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 * SPDX-License-Identifier: GPL-2.0+
26 #include <asm/arch/imx-regs.h>
27 #include <asm/arch/clock.h>
29 DECLARE_GLOBAL_DATA_PTR;
31 #define timestamp (gd->arch.tbl)
32 #define lastinc (gd->arch.lastinc)
35 * "time" is measured in 1 / CONFIG_SYS_HZ seconds,
36 * "tick" is internal timer period
38 #ifdef CONFIG_MX25_TIMER_HIGH_PRECISION
39 /* ~0.4% error - measured with stop-watch on 100s boot-delay */
40 static inline unsigned long long tick_to_time(unsigned long long tick)
42 tick *= CONFIG_SYS_HZ;
43 do_div(tick, MXC_CLK32);
47 static inline unsigned long long time_to_tick(unsigned long long time)
50 do_div(time, CONFIG_SYS_HZ);
54 static inline unsigned long long us_to_tick(unsigned long long us)
56 us = us * MXC_CLK32 + 999999;
62 #define TICK_PER_TIME ((MXC_CLK32 + CONFIG_SYS_HZ / 2) / CONFIG_SYS_HZ)
63 #define US_PER_TICK (1000000 / MXC_CLK32)
65 static inline unsigned long long tick_to_time(unsigned long long tick)
67 do_div(tick, TICK_PER_TIME);
71 static inline unsigned long long time_to_tick(unsigned long long time)
73 return time * TICK_PER_TIME;
76 static inline unsigned long long us_to_tick(unsigned long long us)
78 us += US_PER_TICK - 1;
79 do_div(us, US_PER_TICK);
84 /* nothing really to do with interrupts, just starts up a counter. */
85 /* The 32KHz 32-bit timer overruns in 134217 seconds */
89 struct gpt_regs *gpt = (struct gpt_regs *)IMX_GPT1_BASE;
90 struct ccm_regs *ccm = (struct ccm_regs *)IMX_CCM_BASE;
92 /* setup GP Timer 1 */
93 writel(GPT_CTRL_SWR, &gpt->ctrl);
95 writel(readl(&ccm->cgr1) | CCM_CGR1_GPT1, &ccm->cgr1);
97 for (i = 0; i < 100; i++)
98 writel(0, &gpt->ctrl); /* We have no udelay by now */
99 writel(0, &gpt->pre); /* prescaler = 1 */
100 /* Freerun Mode, 32KHz input */
101 writel(readl(&gpt->ctrl) | GPT_CTRL_CLKSOURCE_32 | GPT_CTRL_FRR,
103 writel(readl(&gpt->ctrl) | GPT_CTRL_TEN, &gpt->ctrl);
108 unsigned long long get_ticks(void)
110 struct gpt_regs *gpt = (struct gpt_regs *)IMX_GPT1_BASE;
111 ulong now = readl(&gpt->counter); /* current tick value */
113 if (now >= lastinc) {
115 * normal mode (non roll)
116 * move stamp forward with absolut diff ticks
118 timestamp += (now - lastinc);
120 /* we have rollover of incrementer */
121 timestamp += (0xFFFFFFFF - lastinc) + now;
127 ulong get_timer_masked(void)
130 * get_ticks() returns a long long (64 bit), it wraps in
131 * 2^64 / MXC_CLK32 = 2^64 / 2^15 = 2^49 ~ 5 * 10^14 (s) ~
132 * 5 * 10^9 days... and get_ticks() * CONFIG_SYS_HZ wraps in
133 * 5 * 10^6 days - long enough.
135 return tick_to_time(get_ticks());
138 ulong get_timer(ulong base)
140 return get_timer_masked() - base;
143 /* delay x useconds AND preserve advance timstamp value */
144 void __udelay(unsigned long usec)
146 unsigned long long tmp;
149 tmo = us_to_tick(usec);
150 tmp = get_ticks() + tmo; /* get current timestamp */
152 while (get_ticks() < tmp) /* loop till event */
157 * This function is derived from PowerPC code (timebase clock frequency).
158 * On ARM it returns the number of timer ticks per second.
160 ulong get_tbclk(void)