2 * (C) Copyright 2007-2008
3 * Stelian Pop <stelian@popies.net>
4 * Lead Tech Design <www.leadtechdesign.com>
7 * Matthias Weisser, Graf-Syteco <weisserm@arcor.de>
9 * SPDX-License-Identifier: GPL-2.0+
15 #include <asm/arch/hardware.h>
17 #define TIMER_LOAD_VAL 0xffffffff
18 #define TIMER_FREQ (CONFIG_MB86R0x_IOCLK / 256)
20 DECLARE_GLOBAL_DATA_PTR;
22 #define timestamp gd->arch.tbl
23 #define lastdec gd->arch.lastinc
25 static inline unsigned long long tick_to_time(unsigned long long tick)
27 tick *= CONFIG_SYS_HZ;
28 do_div(tick, TIMER_FREQ);
33 static inline unsigned long long usec_to_tick(unsigned long long usec)
36 do_div(usec, 1000000);
41 /* nothing really to do with interrupts, just starts up a counter. */
44 struct mb86r0x_timer * timer = (struct mb86r0x_timer *)
46 ulong ctrl = readl(&timer->control);
48 writel(TIMER_LOAD_VAL, &timer->load);
50 ctrl |= MB86R0x_TIMER_ENABLE | MB86R0x_TIMER_PRS_8S |
51 MB86R0x_TIMER_SIZE_32;
53 writel(ctrl, &timer->control);
55 /* capture current value time */
56 lastdec = readl(&timer->value);
57 timestamp = 0; /* start "advancing" time stamp from 0 */
63 * timer without interrupts
65 unsigned long long get_ticks(void)
67 struct mb86r0x_timer * timer = (struct mb86r0x_timer *)
69 ulong now = readl(&timer->value);
72 /* normal mode (non roll) */
73 /* move stamp forward with absolut diff ticks */
74 timestamp += lastdec - now;
76 /* we have rollover of incrementer */
77 timestamp += lastdec + TIMER_LOAD_VAL - now;
83 ulong get_timer_masked(void)
85 return tick_to_time(get_ticks());
88 void __udelay(unsigned long usec)
90 unsigned long long tmp;
93 tmo = usec_to_tick(usec);
94 tmp = get_ticks(); /* get current timestamp */
96 while ((get_ticks() - tmp) < tmo) /* loop till event */
100 ulong get_timer(ulong base)
102 return get_timer_masked() - base;
106 * This function is derived from PowerPC code (timebase clock frequency).
107 * On ARM it returns the number of timer ticks per second.
109 ulong get_tbclk(void)