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.
23 #include <faraday/ftpmu010.h>
24 #include <faraday/fttmr010.h>
26 DECLARE_GLOBAL_DATA_PTR;
28 #define TIMER_CLOCK 32768
29 #define TIMER_LOAD_VAL 0xffffffff
31 static inline unsigned long long tick_to_time(unsigned long long tick)
33 tick *= CONFIG_SYS_HZ;
34 do_div(tick, gd->timer_rate_hz);
39 static inline unsigned long long usec_to_tick(unsigned long long usec)
41 usec *= gd->timer_rate_hz;
42 do_div(usec, 1000000);
49 struct fttmr010 *tmr = (struct fttmr010 *)CONFIG_FTTMR010_BASE;
52 debug("%s()\n", __func__);
57 /* use 32768Hz oscillator for RTC, WDT, TIMER */
58 ftpmu010_32768osc_enable();
61 writel(TIMER_LOAD_VAL, &tmr->timer3_load);
62 writel(TIMER_LOAD_VAL, &tmr->timer3_counter);
63 writel(0, &tmr->timer3_match1);
64 writel(0, &tmr->timer3_match2);
66 /* we don't want timer to issue interrupts */
67 writel(FTTMR010_TM3_MATCH1 |
69 FTTMR010_TM3_OVERFLOW,
70 &tmr->interrupt_mask);
73 cr |= FTTMR010_TM3_CLOCK; /* use external clock */
74 cr |= FTTMR010_TM3_ENABLE;
77 gd->timer_rate_hz = TIMER_CLOCK;
78 gd->tbu = gd->tbl = 0;
84 * Get the current 64 bit timer tick count
86 unsigned long long get_ticks(void)
88 struct fttmr010 *tmr = (struct fttmr010 *)CONFIG_FTTMR010_BASE;
89 ulong now = TIMER_LOAD_VAL - readl(&tmr->timer3_counter);
91 /* increment tbu if tbl has rolled over */
95 return (((unsigned long long)gd->tbu) << 32) | gd->tbl;
98 void __udelay(unsigned long usec)
100 unsigned long long start;
103 start = get_ticks(); /* get current timestamp */
104 tmo = usec_to_tick(usec); /* convert usecs to ticks */
105 while ((get_ticks() - start) < tmo)
106 ; /* loop till time has passed */
110 * get_timer(base) can be used to check for timeouts or
111 * to measure elasped time relative to an event:
113 * ulong start_time = get_timer(0) sets start_time to the current
115 * get_timer(start_time) returns the time elapsed since then.
117 * The time is used in CONFIG_SYS_HZ units!
119 ulong get_timer(ulong base)
121 return tick_to_time(get_ticks()) - base;
125 * Return the number of timer ticks per second.
127 ulong get_tbclk(void)
129 return gd->timer_rate_hz;