blob: 443d31d221e95e11762fb250430ecf3ea6afbc68 [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 Chuang758898d2011-02-17 19:35:23 +000022#include <faraday/ftpmu010.h>
Macpaul Lin0d0783e2011-03-21 01:45:43 +000023#include <faraday/fttmr010.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
Po-Yu Chuang5614a4d2009-11-11 17:27:30 +080084/*
85 * return timer ticks
86 */
87ulong get_timer_masked(void)
88{
89 /* current tick value */
90 ulong now = readl(&tmr->timer3_counter) / (TIMER_CLOCK / CONFIG_SYS_HZ);
91
92 debug("%s(): now = %lx, lastdec = %lx\n", __func__, now, lastdec);
93
94 if (lastdec >= now) {
95 /*
96 * normal mode (non roll)
97 * move stamp fordward with absoulte diff ticks
98 */
99 timestamp += lastdec - now;
100 } else {
101 /*
102 * we have overflow of the count down timer
103 *
104 * nts = ts + ld + (TLV - now)
105 * ts=old stamp, ld=time that passed before passing through -1
106 * (TLV-now) amount of time after passing though -1
107 * nts = new "advancing time stamp"...it could also roll and
108 * cause problems.
109 */
110 timestamp += lastdec + TIMER_LOAD_VAL - now;
111 }
112
113 lastdec = now;
114
115 debug("%s() returns %lx\n", __func__, timestamp);
116
117 return timestamp;
118}
119
120/*
121 * return difference between timer ticks and base
122 */
123ulong get_timer(ulong base)
124{
125 debug("%s(%lx)\n", __func__, base);
126 return get_timer_masked() - base;
127}
128
Wolfgang Denkd7cb3552010-05-21 23:13:18 +0200129/* delay x useconds AND preserve advance timestamp value */
Wolfgang Denk01608842010-05-21 23:14:53 +0200130void __udelay(unsigned long usec)
Po-Yu Chuang5614a4d2009-11-11 17:27:30 +0800131{
132 long tmo = usec * (TIMER_CLOCK / 1000) / 1000;
133 unsigned long now, last = readl(&tmr->timer3_counter);
134
135 debug("%s(%lu)\n", __func__, usec);
136 while (tmo > 0) {
137 now = readl(&tmr->timer3_counter);
138 if (now > last) /* count down timer overflow */
139 tmo -= TIMER_LOAD_VAL + last - now;
140 else
141 tmo -= last - now;
142 last = now;
143 }
144}
145
146/*
147 * This function is derived from PowerPC code (read timebase as long long).
148 * On ARM it just returns the timer value.
149 */
150unsigned long long get_ticks(void)
151{
152 debug("%s()\n", __func__);
153 return get_timer(0);
154}
155
156/*
157 * This function is derived from PowerPC code (timebase clock frequency).
158 * On ARM it returns the number of timer ticks per second.
159 */
160ulong get_tbclk(void)
161{
162 debug("%s()\n", __func__);
163 return CONFIG_SYS_HZ;
164}