blob: 7785036f57a0e78c4387683e95a85b65ba5f54d6 [file] [log] [blame]
Po-Yu Chuang5614a4d2009-11-11 17:27:30 +08001/*
2 * (C) Copyright 2009 Faraday Technology
3 * Po-Yu Chuang <ratbert@faraday-tech.com>
4 *
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.
9 *
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.
14 *
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.
18 */
19
20#include <common.h>
21#include <asm/io.h>
Po-Yu Chuang5614a4d2009-11-11 17:27:30 +080022#include <asm/arch/fttmr010.h>
Po-Yu Chuang758898d2011-02-17 19:35:23 +000023#include <faraday/ftpmu010.h>
Po-Yu Chuang5614a4d2009-11-11 17:27:30 +080024
25static ulong timestamp;
26static ulong lastdec;
27
28static struct fttmr010 *tmr = (struct fttmr010 *)CONFIG_FTTMR010_BASE;
Po-Yu Chuang5614a4d2009-11-11 17:27:30 +080029
30#define TIMER_CLOCK 32768
31#define TIMER_LOAD_VAL 0xffffffff
32
33int timer_init(void)
34{
Po-Yu Chuang5614a4d2009-11-11 17:27:30 +080035 unsigned int cr;
36
37 debug("%s()\n", __func__);
38
39 /* disable timers */
40 writel(0, &tmr->cr);
41
Po-Yu Chuang758898d2011-02-17 19:35:23 +000042 /* use 32768Hz oscillator for RTC, WDT, TIMER */
43 ftpmu010_32768osc_enable();
Po-Yu Chuang5614a4d2009-11-11 17:27:30 +080044
45 /* setup timer */
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);
50
51 /* we don't want timer to issue interrupts */
52 writel(FTTMR010_TM3_MATCH1 |
53 FTTMR010_TM3_MATCH2 |
54 FTTMR010_TM3_OVERFLOW,
55 &tmr->interrupt_mask);
56
57 cr = readl(&tmr->cr);
58 cr |= FTTMR010_TM3_CLOCK; /* use external clock */
59 cr |= FTTMR010_TM3_ENABLE;
60 writel(cr, &tmr->cr);
61
62 /* init the timestamp and lastdec value */
63 reset_timer_masked();
64
65 return 0;
66}
67
68/*
69 * timer without interrupts
70 */
71
72/*
73 * reset time
74 */
75void reset_timer_masked(void)
76{
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 */
80
81 debug("%s(): lastdec = %lx\n", __func__, lastdec);
82}
83
84void reset_timer(void)
85{
86 debug("%s()\n", __func__);
87 reset_timer_masked();
88}
89
90/*
91 * return timer ticks
92 */
93ulong get_timer_masked(void)
94{
95 /* current tick value */
96 ulong now = readl(&tmr->timer3_counter) / (TIMER_CLOCK / CONFIG_SYS_HZ);
97
98 debug("%s(): now = %lx, lastdec = %lx\n", __func__, now, lastdec);
99
100 if (lastdec >= now) {
101 /*
102 * normal mode (non roll)
103 * move stamp fordward with absoulte diff ticks
104 */
105 timestamp += lastdec - now;
106 } else {
107 /*
108 * we have overflow of the count down timer
109 *
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
114 * cause problems.
115 */
116 timestamp += lastdec + TIMER_LOAD_VAL - now;
117 }
118
119 lastdec = now;
120
121 debug("%s() returns %lx\n", __func__, timestamp);
122
123 return timestamp;
124}
125
126/*
127 * return difference between timer ticks and base
128 */
129ulong get_timer(ulong base)
130{
131 debug("%s(%lx)\n", __func__, base);
132 return get_timer_masked() - base;
133}
134
135void set_timer(ulong t)
136{
137 debug("%s(%lx)\n", __func__, t);
138 timestamp = t;
139}
140
Wolfgang Denkd7cb3552010-05-21 23:13:18 +0200141/* delay x useconds AND preserve advance timestamp value */
Wolfgang Denk01608842010-05-21 23:14:53 +0200142void __udelay(unsigned long usec)
Po-Yu Chuang5614a4d2009-11-11 17:27:30 +0800143{
144 long tmo = usec * (TIMER_CLOCK / 1000) / 1000;
145 unsigned long now, last = readl(&tmr->timer3_counter);
146
147 debug("%s(%lu)\n", __func__, usec);
148 while (tmo > 0) {
149 now = readl(&tmr->timer3_counter);
150 if (now > last) /* count down timer overflow */
151 tmo -= TIMER_LOAD_VAL + last - now;
152 else
153 tmo -= last - now;
154 last = now;
155 }
156}
157
158/*
159 * This function is derived from PowerPC code (read timebase as long long).
160 * On ARM it just returns the timer value.
161 */
162unsigned long long get_ticks(void)
163{
164 debug("%s()\n", __func__);
165 return get_timer(0);
166}
167
168/*
169 * This function is derived from PowerPC code (timebase clock frequency).
170 * On ARM it returns the number of timer ticks per second.
171 */
172ulong get_tbclk(void)
173{
174 debug("%s()\n", __func__);
175 return CONFIG_SYS_HZ;
176}