2 * (C) Copyright 2009 Faraday Technology
3 * Po-Yu Chuang <ratbert@faraday-tech.com>
5 * Copyright (C) 2011 Andes Technology Corporation
6 * Shawn Lin, Andes Technology Corporation <nobuhiro@andestech.com>
7 * Macpaul Lin, Andes Technology Corporation <macpaul@andestech.com>
9 * See file CREDITS for list of people who contributed to this
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29 #include <faraday/fttmr010.h>
31 static ulong timestamp;
36 struct fttmr010 *tmr = (struct fttmr010 *)CONFIG_FTTMR010_BASE;
39 debug("%s()\n", __func__);
44 #ifdef CONFIG_FTTMR010_EXT_CLK
45 /* use 32768Hz oscillator for RTC, WDT, TIMER */
46 ftpmu010_32768osc_enable();
50 writel(TIMER_LOAD_VAL, &tmr->timer3_load);
51 writel(TIMER_LOAD_VAL, &tmr->timer3_counter);
52 writel(0, &tmr->timer3_match1);
53 writel(0, &tmr->timer3_match2);
55 /* we don't want timer to issue interrupts */
56 writel(FTTMR010_TM3_MATCH1 |
58 FTTMR010_TM3_OVERFLOW,
59 &tmr->interrupt_mask);
62 #ifdef CONFIG_FTTMR010_EXT_CLK
63 cr |= FTTMR010_TM3_CLOCK; /* use external clock */
65 cr |= FTTMR010_TM3_ENABLE;
68 /* init the timestamp and lastdec value */
75 * timer without interrupts
81 void reset_timer_masked(void)
83 struct fttmr010 *tmr = (struct fttmr010 *)CONFIG_FTTMR010_BASE;
85 /* capure current decrementer value time */
86 #ifdef CONFIG_FTTMR010_EXT_CLK
87 lastdec = readl(&tmr->timer3_counter) / (TIMER_CLOCK / CONFIG_SYS_HZ);
89 lastdec = readl(&tmr->timer3_counter) / (CONFIG_SYS_CLK_FREQ / 2);
91 timestamp = 0; /* start "advancing" time stamp from 0 */
93 debug("%s(): lastdec = %lx\n", __func__, lastdec);
96 void reset_timer(void)
98 debug("%s()\n", __func__);
105 ulong get_timer_masked(void)
107 struct fttmr010 *tmr = (struct fttmr010 *)CONFIG_FTTMR010_BASE;
109 /* current tick value */
110 #ifdef CONFIG_FTTMR010_EXT_CLK
111 ulong now = readl(&tmr->timer3_counter) / (TIMER_CLOCK / CONFIG_SYS_HZ);
113 ulong now = readl(&tmr->timer3_counter) / \
114 (CONFIG_SYS_CLK_FREQ / 2 / 1024);
117 debug("%s(): now = %lx, lastdec = %lx\n", __func__, now, lastdec);
119 if (lastdec >= now) {
121 * normal mode (non roll)
122 * move stamp fordward with absoulte diff ticks
124 timestamp += lastdec - now;
127 * we have overflow of the count down timer
129 * nts = ts + ld + (TLV - now)
130 * ts=old stamp, ld=time that passed before passing through -1
131 * (TLV-now) amount of time after passing though -1
132 * nts = new "advancing time stamp"...it could also roll and
135 timestamp += lastdec + TIMER_LOAD_VAL - now;
140 debug("%s() returns %lx\n", __func__, timestamp);
146 * return difference between timer ticks and base
148 ulong get_timer(ulong base)
150 debug("%s(%lx)\n", __func__, base);
151 return get_timer_masked() - base;
154 void set_timer(ulong t)
156 debug("%s(%lx)\n", __func__, t);
160 /* delay x useconds AND preserve advance timestamp value */
161 void __udelay(unsigned long usec)
163 struct fttmr010 *tmr = (struct fttmr010 *)CONFIG_FTTMR010_BASE;
165 #ifdef CONFIG_FTTMR010_EXT_CLK
166 long tmo = usec * (TIMER_CLOCK / 1000) / 1000;
168 long tmo = usec * ((CONFIG_SYS_CLK_FREQ / 2) / 1000) / 1000;
170 unsigned long now, last = readl(&tmr->timer3_counter);
172 debug("%s(%lu)\n", __func__, usec);
174 now = readl(&tmr->timer3_counter);
175 if (now > last) /* count down timer overflow */
176 tmo -= TIMER_LOAD_VAL + last - now;
184 * This function is derived from PowerPC code (read timebase as long long).
185 * On ARM it just returns the timer value.
187 unsigned long long get_ticks(void)
189 debug("%s()\n", __func__);
194 * This function is derived from PowerPC code (timebase clock frequency).
195 * On ARM it returns the number of timer ticks per second.
197 ulong get_tbclk(void)
199 debug("%s()\n", __func__);
200 #ifdef CONFIG_FTTMR010_EXT_CLK
201 return CONFIG_SYS_HZ;
203 return CONFIG_SYS_CLK_FREQ;