3 * Michael Schwingen, michael@schwingen.org
6 * Stefan Roese, DENX Software Engineering, sr@denx.de.
9 * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
10 * Marius Groeger <mgroeger@sysgo.de>
13 * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
14 * Alex Zuepke <azu@sysgo.de>
16 * SPDX-License-Identifier: GPL-2.0+
20 #include <asm/arch/ixp425.h>
24 DECLARE_GLOBAL_DATA_PTR;
27 * The IXP42x time-stamp timer runs at 2*OSC_IN (66.666MHz when using a
30 static inline unsigned long long tick_to_time(unsigned long long tick)
32 tick *= CONFIG_SYS_HZ;
33 do_div(tick, CONFIG_IXP425_TIMER_CLK);
37 static inline unsigned long long time_to_tick(unsigned long long time)
39 time *= CONFIG_IXP425_TIMER_CLK;
40 do_div(time, CONFIG_SYS_HZ);
44 static inline unsigned long long us_to_tick(unsigned long long us)
46 us = us * CONFIG_IXP425_TIMER_CLK + 999999;
51 unsigned long long get_ticks(void)
53 ulong now = readl(IXP425_OSTS_B);
55 if (readl(IXP425_OSST) & IXP425_OSST_TIMER_TS_PEND) {
56 /* rollover of timestamp timer register */
57 gd->arch.timestamp += (0xFFFFFFFF - gd->arch.lastinc) + now + 1;
58 writel(IXP425_OSST_TIMER_TS_PEND, IXP425_OSST);
60 /* move stamp forward with absolut diff ticks */
61 gd->arch.timestamp += (now - gd->arch.lastinc);
63 gd->arch.lastinc = now;
64 return gd->arch.timestamp;
68 void reset_timer_masked(void)
70 /* capture current timestamp counter */
71 gd->arch.lastinc = readl(IXP425_OSTS_B);
72 /* start "advancing" time stamp from 0 */
73 gd->arch.timestamp = 0;
76 ulong get_timer_masked(void)
78 return tick_to_time(get_ticks());
81 ulong get_timer(ulong base)
83 return get_timer_masked() - base;
86 /* delay x useconds AND preserve advance timestamp value */
87 void __udelay(unsigned long usec)
89 unsigned long long tmp;
91 tmp = get_ticks() + us_to_tick(usec);
93 while (get_ticks() < tmp)
99 writel(IXP425_OSST_TIMER_TS_PEND, IXP425_OSST);