]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - arch/arm/cpu/arm920t/a320/timer.c
Merge branch 'master' of git://git.denx.de/u-boot-x86
[karo-tx-uboot.git] / arch / arm / cpu / arm920t / a320 / timer.c
1 /*
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 <div64.h>
22 #include <asm/io.h>
23 #include <faraday/ftpmu010.h>
24 #include <faraday/fttmr010.h>
25
26 DECLARE_GLOBAL_DATA_PTR;
27
28 #define TIMER_CLOCK     32768
29 #define TIMER_LOAD_VAL  0xffffffff
30
31 static inline unsigned long long tick_to_time(unsigned long long tick)
32 {
33         tick *= CONFIG_SYS_HZ;
34         do_div(tick, gd->arch.timer_rate_hz);
35
36         return tick;
37 }
38
39 static inline unsigned long long usec_to_tick(unsigned long long usec)
40 {
41         usec *= gd->arch.timer_rate_hz;
42         do_div(usec, 1000000);
43
44         return usec;
45 }
46
47 int timer_init(void)
48 {
49         struct fttmr010 *tmr = (struct fttmr010 *)CONFIG_FTTMR010_BASE;
50         unsigned int cr;
51
52         debug("%s()\n", __func__);
53
54         /* disable timers */
55         writel(0, &tmr->cr);
56
57         /* use 32768Hz oscillator for RTC, WDT, TIMER */
58         ftpmu010_32768osc_enable();
59
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
77         gd->arch.timer_rate_hz = TIMER_CLOCK;
78         gd->arch.tbu = gd->arch.tbl = 0;
79
80         return 0;
81 }
82
83 /*
84  * Get the current 64 bit timer tick count
85  */
86 unsigned long long get_ticks(void)
87 {
88         struct fttmr010 *tmr = (struct fttmr010 *)CONFIG_FTTMR010_BASE;
89         ulong now = TIMER_LOAD_VAL - readl(&tmr->timer3_counter);
90
91         /* increment tbu if tbl has rolled over */
92         if (now < gd->arch.tbl)
93                 gd->arch.tbu++;
94         gd->arch.tbl = now;
95         return (((unsigned long long)gd->arch.tbu) << 32) | gd->arch.tbl;
96 }
97
98 void __udelay(unsigned long usec)
99 {
100         unsigned long long start;
101         ulong tmo;
102
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 */
107 }
108
109 /*
110  * 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!
118  */
119 ulong get_timer(ulong base)
120 {
121         return tick_to_time(get_ticks()) - base;
122 }
123
124 /*
125  * Return the number of timer ticks per second.
126  */
127 ulong get_tbclk(void)
128 {
129         return gd->arch.timer_rate_hz;
130 }