2 * (C) Copyright 2009 Faraday Technology
3 * Po-Yu Chuang <ratbert@faraday-tech.com>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include <faraday/ftpmu010.h>
23 #include <faraday/fttmr010.h>
25 static ulong timestamp;
28 static struct fttmr010 *tmr = (struct fttmr010 *)CONFIG_FTTMR010_BASE;
30 #define TIMER_CLOCK 32768
31 #define TIMER_LOAD_VAL 0xffffffff
37 debug("%s()\n", __func__);
42 /* use 32768Hz oscillator for RTC, WDT, TIMER */
43 ftpmu010_32768osc_enable();
46 writel(TIMER_LOAD_VAL, &tmr->timer3_load);
47 writel(TIMER_LOAD_VAL, &tmr->timer3_counter);
48 writel(0, &tmr->timer3_match1);
49 writel(0, &tmr->timer3_match2);
51 /* we don't want timer to issue interrupts */
52 writel(FTTMR010_TM3_MATCH1 |
54 FTTMR010_TM3_OVERFLOW,
55 &tmr->interrupt_mask);
58 cr |= FTTMR010_TM3_CLOCK; /* use external clock */
59 cr |= FTTMR010_TM3_ENABLE;
62 /* init the timestamp and lastdec value */
69 * timer without interrupts
75 void reset_timer_masked(void)
77 /* capure current decrementer value time */
78 lastdec = readl(&tmr->timer3_counter) / (TIMER_CLOCK / CONFIG_SYS_HZ);
79 timestamp = 0; /* start "advancing" time stamp from 0 */
81 debug("%s(): lastdec = %lx\n", __func__, lastdec);
84 void reset_timer(void)
86 debug("%s()\n", __func__);
93 ulong get_timer_masked(void)
95 /* current tick value */
96 ulong now = readl(&tmr->timer3_counter) / (TIMER_CLOCK / CONFIG_SYS_HZ);
98 debug("%s(): now = %lx, lastdec = %lx\n", __func__, now, lastdec);
100 if (lastdec >= now) {
102 * normal mode (non roll)
103 * move stamp fordward with absoulte diff ticks
105 timestamp += lastdec - now;
108 * we have overflow of the count down timer
110 * nts = ts + ld + (TLV - now)
111 * ts=old stamp, ld=time that passed before passing through -1
112 * (TLV-now) amount of time after passing though -1
113 * nts = new "advancing time stamp"...it could also roll and
116 timestamp += lastdec + TIMER_LOAD_VAL - now;
121 debug("%s() returns %lx\n", __func__, timestamp);
127 * return difference between timer ticks and base
129 ulong get_timer(ulong base)
131 debug("%s(%lx)\n", __func__, base);
132 return get_timer_masked() - base;
135 void set_timer(ulong t)
137 debug("%s(%lx)\n", __func__, t);
141 /* delay x useconds AND preserve advance timestamp value */
142 void __udelay(unsigned long usec)
144 long tmo = usec * (TIMER_CLOCK / 1000) / 1000;
145 unsigned long now, last = readl(&tmr->timer3_counter);
147 debug("%s(%lu)\n", __func__, usec);
149 now = readl(&tmr->timer3_counter);
150 if (now > last) /* count down timer overflow */
151 tmo -= TIMER_LOAD_VAL + last - now;
159 * This function is derived from PowerPC code (read timebase as long long).
160 * On ARM it just returns the timer value.
162 unsigned long long get_ticks(void)
164 debug("%s()\n", __func__);
169 * This function is derived from PowerPC code (timebase clock frequency).
170 * On ARM it returns the number of timer ticks per second.
172 ulong get_tbclk(void)
174 debug("%s()\n", __func__);
175 return CONFIG_SYS_HZ;