blob: 4bfcef2379b456ac645ce4d659c44b735a914765 [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>
Po-Yu Chuangbf7a5262011-08-10 17:44:21 +000021#include <div64.h>
Po-Yu Chuang5614a4d2009-11-11 17:27:30 +080022#include <asm/io.h>
Po-Yu Chuang758898d2011-02-17 19:35:23 +000023#include <faraday/ftpmu010.h>
Macpaul Lin0d0783e2011-03-21 01:45:43 +000024#include <faraday/fttmr010.h>
Po-Yu Chuang5614a4d2009-11-11 17:27:30 +080025
Po-Yu Chuangbf7a5262011-08-10 17:44:21 +000026DECLARE_GLOBAL_DATA_PTR;
Po-Yu Chuang5614a4d2009-11-11 17:27:30 +080027
28#define TIMER_CLOCK 32768
29#define TIMER_LOAD_VAL 0xffffffff
30
Po-Yu Chuangbf7a5262011-08-10 17:44:21 +000031static inline unsigned long long tick_to_time(unsigned long long tick)
32{
33 tick *= CONFIG_SYS_HZ;
34 do_div(tick, gd->timer_rate_hz);
35
36 return tick;
37}
38
39static inline unsigned long long usec_to_tick(unsigned long long usec)
40{
41 usec *= gd->timer_rate_hz;
42 do_div(usec, 1000000);
43
44 return usec;
45}
46
Po-Yu Chuang5614a4d2009-11-11 17:27:30 +080047int timer_init(void)
48{
Po-Yu Chuangbf7a5262011-08-10 17:44:21 +000049 struct fttmr010 *tmr = (struct fttmr010 *)CONFIG_FTTMR010_BASE;
Po-Yu Chuang5614a4d2009-11-11 17:27:30 +080050 unsigned int cr;
51
52 debug("%s()\n", __func__);
53
54 /* disable timers */
55 writel(0, &tmr->cr);
56
Po-Yu Chuang758898d2011-02-17 19:35:23 +000057 /* use 32768Hz oscillator for RTC, WDT, TIMER */
58 ftpmu010_32768osc_enable();
Po-Yu Chuang5614a4d2009-11-11 17:27:30 +080059
60 /* setup timer */
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);
65
66 /* we don't want timer to issue interrupts */
67 writel(FTTMR010_TM3_MATCH1 |
68 FTTMR010_TM3_MATCH2 |
69 FTTMR010_TM3_OVERFLOW,
70 &tmr->interrupt_mask);
71
72 cr = readl(&tmr->cr);
73 cr |= FTTMR010_TM3_CLOCK; /* use external clock */
74 cr |= FTTMR010_TM3_ENABLE;
75 writel(cr, &tmr->cr);
76
Po-Yu Chuangbf7a5262011-08-10 17:44:21 +000077 gd->timer_rate_hz = TIMER_CLOCK;
78 gd->tbu = gd->tbl = 0;
Po-Yu Chuang5614a4d2009-11-11 17:27:30 +080079
80 return 0;
81}
82
83/*
Po-Yu Chuangbf7a5262011-08-10 17:44:21 +000084 * Get the current 64 bit timer tick count
Po-Yu Chuang5614a4d2009-11-11 17:27:30 +080085 */
Po-Yu Chuangbf7a5262011-08-10 17:44:21 +000086unsigned long long get_ticks(void)
Po-Yu Chuang5614a4d2009-11-11 17:27:30 +080087{
Po-Yu Chuangbf7a5262011-08-10 17:44:21 +000088 struct fttmr010 *tmr = (struct fttmr010 *)CONFIG_FTTMR010_BASE;
89 ulong now = TIMER_LOAD_VAL - readl(&tmr->timer3_counter);
Po-Yu Chuang5614a4d2009-11-11 17:27:30 +080090
Po-Yu Chuangbf7a5262011-08-10 17:44:21 +000091 /* increment tbu if tbl has rolled over */
92 if (now < gd->tbl)
93 gd->tbu++;
94 gd->tbl = now;
95 return (((unsigned long long)gd->tbu) << 32) | gd->tbl;
Po-Yu Chuang5614a4d2009-11-11 17:27:30 +080096}
97
Wolfgang Denk01608842010-05-21 23:14:53 +020098void __udelay(unsigned long usec)
Po-Yu Chuang5614a4d2009-11-11 17:27:30 +080099{
Po-Yu Chuangbf7a5262011-08-10 17:44:21 +0000100 unsigned long long start;
101 ulong tmo;
Po-Yu Chuang5614a4d2009-11-11 17:27:30 +0800102
Po-Yu Chuangbf7a5262011-08-10 17:44:21 +0000103 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 */
Po-Yu Chuang5614a4d2009-11-11 17:27:30 +0800107}
108
109/*
Po-Yu Chuangbf7a5262011-08-10 17:44:21 +0000110 * get_timer(base) can be used to check for timeouts or
111 * to measure elasped time relative to an event:
112 *
113 * ulong start_time = get_timer(0) sets start_time to the current
114 * time value.
115 * get_timer(start_time) returns the time elapsed since then.
116 *
117 * The time is used in CONFIG_SYS_HZ units!
Po-Yu Chuang5614a4d2009-11-11 17:27:30 +0800118 */
Po-Yu Chuangbf7a5262011-08-10 17:44:21 +0000119ulong get_timer(ulong base)
Po-Yu Chuang5614a4d2009-11-11 17:27:30 +0800120{
Po-Yu Chuangbf7a5262011-08-10 17:44:21 +0000121 return tick_to_time(get_ticks()) - base;
Po-Yu Chuang5614a4d2009-11-11 17:27:30 +0800122}
123
124/*
Po-Yu Chuangbf7a5262011-08-10 17:44:21 +0000125 * Return the number of timer ticks per second.
Po-Yu Chuang5614a4d2009-11-11 17:27:30 +0800126 */
127ulong get_tbclk(void)
128{
Po-Yu Chuangbf7a5262011-08-10 17:44:21 +0000129 return gd->timer_rate_hz;
Po-Yu Chuang5614a4d2009-11-11 17:27:30 +0800130}