blob: bb078196d000121af6880b595f6f021ae97ce253 [file] [log] [blame]
Matthias Weisser63c36f52010-08-09 13:31:49 +02001/*
2 * (C) Copyright 2007-2008
Stelian Pop5ee0c7f2011-11-01 00:00:39 +01003 * Stelian Pop <stelian@popies.net>
Matthias Weisser63c36f52010-08-09 13:31:49 +02004 * Lead Tech Design <www.leadtechdesign.com>
5 *
6 * (C) Copyright 2010
7 * Matthias Weisser, Graf-Syteco <weisserm@arcor.de>
8 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02009 * SPDX-License-Identifier: GPL-2.0+
Matthias Weisser63c36f52010-08-09 13:31:49 +020010 */
11
12#include <div64.h>
13#include <common.h>
14#include <asm/io.h>
15#include <asm/arch/hardware.h>
16
17#define TIMER_LOAD_VAL 0xffffffff
18#define TIMER_FREQ (CONFIG_MB86R0x_IOCLK / 256)
19
Heiko Schocher5504dab2011-01-20 22:56:39 +000020DECLARE_GLOBAL_DATA_PTR;
21
Simon Glass2655ee12012-12-13 20:48:34 +000022#define timestamp gd->arch.tbl
Simon Glassa848da52012-12-13 20:48:35 +000023#define lastdec gd->arch.lastinc
Matthias Weisser63c36f52010-08-09 13:31:49 +020024
25static inline unsigned long long tick_to_time(unsigned long long tick)
26{
27 tick *= CONFIG_SYS_HZ;
28 do_div(tick, TIMER_FREQ);
29
30 return tick;
31}
32
33static inline unsigned long long usec_to_tick(unsigned long long usec)
34{
35 usec *= TIMER_FREQ;
36 do_div(usec, 1000000);
37
38 return usec;
39}
40
41/* nothing really to do with interrupts, just starts up a counter. */
42int timer_init(void)
43{
44 struct mb86r0x_timer * timer = (struct mb86r0x_timer *)
45 MB86R0x_TIMER_BASE;
46 ulong ctrl = readl(&timer->control);
47
48 writel(TIMER_LOAD_VAL, &timer->load);
49
50 ctrl |= MB86R0x_TIMER_ENABLE | MB86R0x_TIMER_PRS_8S |
51 MB86R0x_TIMER_SIZE_32;
52
53 writel(ctrl, &timer->control);
54
Graeme Russ944a7fe2011-07-15 02:21:14 +000055 /* capture current value time */
56 lastdec = readl(&timer->value);
57 timestamp = 0; /* start "advancing" time stamp from 0 */
Matthias Weisser63c36f52010-08-09 13:31:49 +020058
59 return 0;
60}
61
62/*
63 * timer without interrupts
64 */
65unsigned long long get_ticks(void)
66{
67 struct mb86r0x_timer * timer = (struct mb86r0x_timer *)
68 MB86R0x_TIMER_BASE;
69 ulong now = readl(&timer->value);
70
71 if (now <= lastdec) {
72 /* normal mode (non roll) */
73 /* move stamp forward with absolut diff ticks */
74 timestamp += lastdec - now;
75 } else {
76 /* we have rollover of incrementer */
77 timestamp += lastdec + TIMER_LOAD_VAL - now;
78 }
79 lastdec = now;
80 return timestamp;
81}
82
Matthias Weisser63c36f52010-08-09 13:31:49 +020083ulong get_timer_masked(void)
84{
85 return tick_to_time(get_ticks());
86}
87
88void __udelay(unsigned long usec)
89{
90 unsigned long long tmp;
91 ulong tmo;
92
93 tmo = usec_to_tick(usec);
94 tmp = get_ticks(); /* get current timestamp */
95
96 while ((get_ticks() - tmp) < tmo) /* loop till event */
97 /*NOP*/;
98}
99
Matthias Weisser63c36f52010-08-09 13:31:49 +0200100ulong get_timer(ulong base)
101{
102 return get_timer_masked() - base;
103}
104
105/*
106 * This function is derived from PowerPC code (timebase clock frequency).
107 * On ARM it returns the number of timer ticks per second.
108 */
109ulong get_tbclk(void)
110{
111 ulong tbclk;
112
113 tbclk = TIMER_FREQ;
114 return tbclk;
115}