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 * See file CREDITS for list of people who contributed to this
23 * This program is free software; you can redistribute it and/or
24 * modify it under the terms of the GNU General Public License as
25 * published by the Free Software Foundation; either version 2 of
26 * the License, or (at your option) any later version.
28 * This program is distributed in the hope that it will be useful,
29 * but WITHOUT ANY WARRANTY; without even the implied warranty of
30 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31 * GNU General Public License for more details.
33 * You should have received a copy of the GNU General Public License
34 * along with this program; if not, write to the Free Software
35 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
42 #include <linux/types.h>
43 #include <asm/arch/chip_drv_config_extern.h>
44 #include <asm/arch/bits.h>
45 #include "asm/arch/syscnt_drv.h"
46 #include "asm/arch/sci_types.h"
48 #define TIMER_MAX_VALUE 0xFFFFFFFF
51 static unsigned long long timestamp;
55 * "time" is measured in 1 / CONFIG_SYS_HZ seconds,
56 * "tick" is internal timer period
58 static inline unsigned long long tick_to_time(unsigned long long tick)
60 tick *= CONFIG_SYS_HZ;
61 do_div(tick, CONFIG_SPRD_TIMER_CLK);
65 static inline unsigned long long time_to_tick(unsigned long long time)
67 time *= CONFIG_SPRD_TIMER_CLK;
68 do_div(time, CONFIG_SYS_HZ);
72 static inline unsigned long long us_to_tick(unsigned long long us)
74 us = us*CONFIG_SPRD_TIMER_CLK;
75 do_div(us, CONFIG_SYS_HZ*1000);
80 /* nothing really to do with interrupts, just starts up a counter. */
81 /* The 32KHz 23-bit timer overruns in 512 seconds */
85 REG32(GR_GEN0) |= BIT_19; //make sys cnt writable
86 REG32(GR_GEN0) |= BIT_27; //enable rtc clk input
88 //clear any hanging interrupts & disable interrupt
89 REG32 (SYS_CTL) &=~ (BIT_0);
90 REG32 (SYS_CTL) |= (BIT_3);
94 void reset_timer(void)
96 //capture current incrementer value time
97 lastinc = readl(SYS_CNT0);
100 void reset_timer_masked(void)
104 unsigned long long get_ticks(void)
106 ulong now = readl(SYS_CNT0);
110 * move stamp forward with absolut diff ticks
112 timestamp += (now - lastinc);
115 timestamp += (TIMER_MAX_VALUE - lastinc) + now;
121 ulong get_timer_masked(void)
123 return tick_to_time(get_ticks());
126 ulong get_timer(ulong base)
128 return get_timer_masked() - base;
131 void set_timer(ulong t)
133 timestamp = time_to_tick(t);
136 void __udelay (unsigned long usec)
138 unsigned long long tmp;
141 tmo = us_to_tick(usec);
142 tmp = get_ticks() + tmo; // get current timestamp
144 while(get_ticks() < tmp) //loop till event