blob: dfec5b3e96b563892022ef349efb4d7670018b24 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Macpaul Lin83dbca72011-10-11 22:33:18 +00002/*
3 * (C) Copyright 2009 Faraday Technology
4 * Po-Yu Chuang <ratbert@faraday-tech.com>
5 *
6 * Copyright (C) 2011 Andes Technology Corporation
7 * Shawn Lin, Andes Technology Corporation <nobuhiro@andestech.com>
8 * Macpaul Lin, Andes Technology Corporation <macpaul@andestech.com>
Macpaul Lin83dbca72011-10-11 22:33:18 +00009 */
rick6d564b82017-05-17 10:59:20 +080010#ifndef CONFIG_TIMER
Macpaul Lin83dbca72011-10-11 22:33:18 +000011#include <common.h>
12#include <asm/io.h>
13#include <faraday/fttmr010.h>
14
15static ulong timestamp;
16static ulong lastdec;
17
18int timer_init(void)
19{
Macpaul Linf4a7b802011-11-23 13:56:50 +080020 struct fttmr010 *tmr = (struct fttmr010 *)CONFIG_FTTMR010_BASE;
Macpaul Lin83dbca72011-10-11 22:33:18 +000021 unsigned int cr;
22
23 debug("%s()\n", __func__);
24
25 /* disable timers */
26 writel(0, &tmr->cr);
27
28#ifdef CONFIG_FTTMR010_EXT_CLK
29 /* use 32768Hz oscillator for RTC, WDT, TIMER */
30 ftpmu010_32768osc_enable();
31#endif
32
33 /* setup timer */
34 writel(TIMER_LOAD_VAL, &tmr->timer3_load);
35 writel(TIMER_LOAD_VAL, &tmr->timer3_counter);
36 writel(0, &tmr->timer3_match1);
37 writel(0, &tmr->timer3_match2);
38
39 /* we don't want timer to issue interrupts */
40 writel(FTTMR010_TM3_MATCH1 |
41 FTTMR010_TM3_MATCH2 |
42 FTTMR010_TM3_OVERFLOW,
43 &tmr->interrupt_mask);
44
45 cr = readl(&tmr->cr);
46#ifdef CONFIG_FTTMR010_EXT_CLK
47 cr |= FTTMR010_TM3_CLOCK; /* use external clock */
48#endif
49 cr |= FTTMR010_TM3_ENABLE;
50 writel(cr, &tmr->cr);
51
52 /* init the timestamp and lastdec value */
53 reset_timer_masked();
54
55 return 0;
56}
57
58/*
59 * timer without interrupts
60 */
61
62/*
63 * reset time
64 */
65void reset_timer_masked(void)
66{
Macpaul Linf4a7b802011-11-23 13:56:50 +080067 struct fttmr010 *tmr = (struct fttmr010 *)CONFIG_FTTMR010_BASE;
Macpaul Lin83dbca72011-10-11 22:33:18 +000068
69 /* capure current decrementer value time */
70#ifdef CONFIG_FTTMR010_EXT_CLK
71 lastdec = readl(&tmr->timer3_counter) / (TIMER_CLOCK / CONFIG_SYS_HZ);
72#else
Axel Lin55edeb52013-07-08 14:29:52 +080073 lastdec = readl(&tmr->timer3_counter) /
74 (CONFIG_SYS_CLK_FREQ / 2 / CONFIG_SYS_HZ);
Macpaul Lin83dbca72011-10-11 22:33:18 +000075#endif
76 timestamp = 0; /* start "advancing" time stamp from 0 */
77
78 debug("%s(): lastdec = %lx\n", __func__, lastdec);
79}
80
81void reset_timer(void)
82{
83 debug("%s()\n", __func__);
84 reset_timer_masked();
85}
86
87/*
88 * return timer ticks
89 */
90ulong get_timer_masked(void)
91{
Macpaul Linf4a7b802011-11-23 13:56:50 +080092 struct fttmr010 *tmr = (struct fttmr010 *)CONFIG_FTTMR010_BASE;
Macpaul Lin83dbca72011-10-11 22:33:18 +000093
94 /* current tick value */
95#ifdef CONFIG_FTTMR010_EXT_CLK
96 ulong now = readl(&tmr->timer3_counter) / (TIMER_CLOCK / CONFIG_SYS_HZ);
97#else
Axel Lin55edeb52013-07-08 14:29:52 +080098 ulong now = readl(&tmr->timer3_counter) /
99 (CONFIG_SYS_CLK_FREQ / 2 / CONFIG_SYS_HZ);
Macpaul Lin83dbca72011-10-11 22:33:18 +0000100#endif
101
102 debug("%s(): now = %lx, lastdec = %lx\n", __func__, now, lastdec);
103
104 if (lastdec >= now) {
105 /*
106 * normal mode (non roll)
107 * move stamp fordward with absoulte diff ticks
108 */
109 timestamp += lastdec - now;
110 } else {
111 /*
112 * we have overflow of the count down timer
113 *
114 * nts = ts + ld + (TLV - now)
115 * ts=old stamp, ld=time that passed before passing through -1
116 * (TLV-now) amount of time after passing though -1
117 * nts = new "advancing time stamp"...it could also roll and
118 * cause problems.
119 */
120 timestamp += lastdec + TIMER_LOAD_VAL - now;
121 }
122
123 lastdec = now;
124
125 debug("%s() returns %lx\n", __func__, timestamp);
126
127 return timestamp;
128}
129
130/*
131 * return difference between timer ticks and base
132 */
133ulong get_timer(ulong base)
134{
135 debug("%s(%lx)\n", __func__, base);
136 return get_timer_masked() - base;
137}
138
139void set_timer(ulong t)
140{
141 debug("%s(%lx)\n", __func__, t);
142 timestamp = t;
143}
144
145/* delay x useconds AND preserve advance timestamp value */
146void __udelay(unsigned long usec)
147{
Macpaul Linf4a7b802011-11-23 13:56:50 +0800148 struct fttmr010 *tmr = (struct fttmr010 *)CONFIG_FTTMR010_BASE;
Macpaul Lin83dbca72011-10-11 22:33:18 +0000149
150#ifdef CONFIG_FTTMR010_EXT_CLK
151 long tmo = usec * (TIMER_CLOCK / 1000) / 1000;
152#else
153 long tmo = usec * ((CONFIG_SYS_CLK_FREQ / 2) / 1000) / 1000;
154#endif
155 unsigned long now, last = readl(&tmr->timer3_counter);
156
157 debug("%s(%lu)\n", __func__, usec);
158 while (tmo > 0) {
159 now = readl(&tmr->timer3_counter);
160 if (now > last) /* count down timer overflow */
161 tmo -= TIMER_LOAD_VAL + last - now;
162 else
163 tmo -= last - now;
164 last = now;
165 }
166}
167
168/*
169 * This function is derived from PowerPC code (read timebase as long long).
170 * On ARM it just returns the timer value.
171 */
172unsigned long long get_ticks(void)
173{
174 debug("%s()\n", __func__);
175 return get_timer(0);
176}
177
178/*
179 * This function is derived from PowerPC code (timebase clock frequency).
180 * On ARM it returns the number of timer ticks per second.
181 */
182ulong get_tbclk(void)
183{
184 debug("%s()\n", __func__);
185#ifdef CONFIG_FTTMR010_EXT_CLK
186 return CONFIG_SYS_HZ;
187#else
188 return CONFIG_SYS_CLK_FREQ;
189#endif
190}
rick6d564b82017-05-17 10:59:20 +0800191#endif /* CONFIG_TIMER */