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 * SPDX-License-Identifier: GPL-2.0+
14 #include <faraday/fttmr010.h>
16 static ulong timestamp;
21 struct fttmr010 *tmr = (struct fttmr010 *)CONFIG_FTTMR010_BASE;
24 debug("%s()\n", __func__);
29 #ifdef CONFIG_FTTMR010_EXT_CLK
30 /* use 32768Hz oscillator for RTC, WDT, TIMER */
31 ftpmu010_32768osc_enable();
35 writel(TIMER_LOAD_VAL, &tmr->timer3_load);
36 writel(TIMER_LOAD_VAL, &tmr->timer3_counter);
37 writel(0, &tmr->timer3_match1);
38 writel(0, &tmr->timer3_match2);
40 /* we don't want timer to issue interrupts */
41 writel(FTTMR010_TM3_MATCH1 |
43 FTTMR010_TM3_OVERFLOW,
44 &tmr->interrupt_mask);
47 #ifdef CONFIG_FTTMR010_EXT_CLK
48 cr |= FTTMR010_TM3_CLOCK; /* use external clock */
50 cr |= FTTMR010_TM3_ENABLE;
53 /* init the timestamp and lastdec value */
60 * timer without interrupts
66 void reset_timer_masked(void)
68 struct fttmr010 *tmr = (struct fttmr010 *)CONFIG_FTTMR010_BASE;
70 /* capure current decrementer value time */
71 #ifdef CONFIG_FTTMR010_EXT_CLK
72 lastdec = readl(&tmr->timer3_counter) / (TIMER_CLOCK / CONFIG_SYS_HZ);
74 lastdec = readl(&tmr->timer3_counter) /
75 (CONFIG_SYS_CLK_FREQ / 2 / CONFIG_SYS_HZ);
77 timestamp = 0; /* start "advancing" time stamp from 0 */
79 debug("%s(): lastdec = %lx\n", __func__, lastdec);
82 void reset_timer(void)
84 debug("%s()\n", __func__);
91 ulong get_timer_masked(void)
93 struct fttmr010 *tmr = (struct fttmr010 *)CONFIG_FTTMR010_BASE;
95 /* current tick value */
96 #ifdef CONFIG_FTTMR010_EXT_CLK
97 ulong now = readl(&tmr->timer3_counter) / (TIMER_CLOCK / CONFIG_SYS_HZ);
99 ulong now = readl(&tmr->timer3_counter) /
100 (CONFIG_SYS_CLK_FREQ / 2 / CONFIG_SYS_HZ);
103 debug("%s(): now = %lx, lastdec = %lx\n", __func__, now, lastdec);
105 if (lastdec >= now) {
107 * normal mode (non roll)
108 * move stamp fordward with absoulte diff ticks
110 timestamp += lastdec - now;
113 * we have overflow of the count down timer
115 * nts = ts + ld + (TLV - now)
116 * ts=old stamp, ld=time that passed before passing through -1
117 * (TLV-now) amount of time after passing though -1
118 * nts = new "advancing time stamp"...it could also roll and
121 timestamp += lastdec + TIMER_LOAD_VAL - now;
126 debug("%s() returns %lx\n", __func__, timestamp);
132 * return difference between timer ticks and base
134 ulong get_timer(ulong base)
136 debug("%s(%lx)\n", __func__, base);
137 return get_timer_masked() - base;
140 void set_timer(ulong t)
142 debug("%s(%lx)\n", __func__, t);
146 /* delay x useconds AND preserve advance timestamp value */
147 void __udelay(unsigned long usec)
149 struct fttmr010 *tmr = (struct fttmr010 *)CONFIG_FTTMR010_BASE;
151 #ifdef CONFIG_FTTMR010_EXT_CLK
152 long tmo = usec * (TIMER_CLOCK / 1000) / 1000;
154 long tmo = usec * ((CONFIG_SYS_CLK_FREQ / 2) / 1000) / 1000;
156 unsigned long now, last = readl(&tmr->timer3_counter);
158 debug("%s(%lu)\n", __func__, usec);
160 now = readl(&tmr->timer3_counter);
161 if (now > last) /* count down timer overflow */
162 tmo -= TIMER_LOAD_VAL + last - now;
170 * This function is derived from PowerPC code (read timebase as long long).
171 * On ARM it just returns the timer value.
173 unsigned long long get_ticks(void)
175 debug("%s()\n", __func__);
180 * This function is derived from PowerPC code (timebase clock frequency).
181 * On ARM it returns the number of timer ticks per second.
183 ulong get_tbclk(void)
185 debug("%s()\n", __func__);
186 #ifdef CONFIG_FTTMR010_EXT_CLK
187 return CONFIG_SYS_HZ;
189 return CONFIG_SYS_CLK_FREQ;