]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - arch/arm/cpu/arm926ejs/at91/timer.c
AT91 Fix: return value of get_tbclk
[karo-tx-uboot.git] / arch / arm / cpu / arm926ejs / at91 / timer.c
1 /*
2  * (C) Copyright 2007-2008
3  * Stelian Pop <stelian.pop@leadtechdesign.com>
4  * Lead Tech Design <www.leadtechdesign.com>
5  *
6  * See file CREDITS for list of people who contributed to this
7  * project.
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License as
11  * published by the Free Software Foundation; either version 2 of
12  * the License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
22  * MA 02111-1307 USA
23  */
24
25 #include <common.h>
26 #include <asm/arch/hardware.h>
27 #include <asm/arch/at91_pit.h>
28 #include <asm/arch/at91_pmc.h>
29 #include <asm/arch/clk.h>
30 #include <asm/arch/io.h>
31 #include <div64.h>
32
33 /*
34  * We're using the AT91CAP9/SAM9 PITC in 32 bit mode, by
35  * setting the 20 bit counter period to its maximum (0xfffff).
36  */
37 #define TIMER_LOAD_VAL  0xfffff
38
39 static ulong timestamp;
40 static ulong lastinc;
41 static ulong timer_freq;
42
43 static inline unsigned long long tick_to_time(unsigned long long tick)
44 {
45         tick *= CONFIG_SYS_HZ;
46         do_div(tick, timer_freq);
47
48         return tick;
49 }
50
51 static inline unsigned long long usec_to_tick(unsigned long long usec)
52 {
53         usec *= timer_freq;
54         do_div(usec, 1000000);
55
56         return usec;
57 }
58
59 /* nothing really to do with interrupts, just starts up a counter. */
60 int timer_init(void)
61 {
62         at91_pmc_t *pmc = (at91_pmc_t *) AT91_PMC_BASE;
63         at91_pit_t *pit = (at91_pit_t *) AT91_PIT_BASE;
64         /*
65          * Enable PITC Clock
66          * The clock is already enabled for system controller in boot
67          */
68         writel(1 << AT91_ID_SYS, &pmc->pcer);
69
70         /* Enable PITC */
71         writel(TIMER_LOAD_VAL | AT91_PIT_MR_EN , &pit->mr);
72
73         reset_timer_masked();
74
75         timer_freq = get_mck_clk_rate() >> 4;
76
77         return 0;
78 }
79
80 /*
81  * timer without interrupts
82  */
83 unsigned long long get_ticks(void)
84 {
85         at91_pit_t *pit = (at91_pit_t *) AT91_PIT_BASE;
86
87         ulong now = readl(&pit->piir);
88
89         if (now >= lastinc)     /* normal mode (non roll) */
90                 /* move stamp forward with absolut diff ticks */
91                 timestamp += (now - lastinc);
92         else                    /* we have rollover of incrementer */
93                 timestamp += (0xFFFFFFFF - lastinc) + now;
94         lastinc = now;
95         return timestamp;
96 }
97
98 void reset_timer_masked(void)
99 {
100         /* reset time */
101         at91_pit_t *pit = (at91_pit_t *) AT91_PIT_BASE;
102
103         /* capture current incrementer value time */
104         lastinc = readl(&pit->piir);
105         timestamp = 0; /* start "advancing" time stamp from 0 */
106 }
107
108 ulong get_timer_masked(void)
109 {
110         return tick_to_time(get_ticks());
111 }
112
113 void __udelay(unsigned long usec)
114 {
115         unsigned long long tmp;
116         ulong tmo;
117
118         tmo = usec_to_tick(usec);
119         tmp = get_ticks() + tmo;        /* get current timestamp */
120
121         while (get_ticks() < tmp)       /* loop till event */
122                  /*NOP*/;
123 }
124
125 void reset_timer(void)
126 {
127         reset_timer_masked();
128 }
129
130 ulong get_timer(ulong base)
131 {
132         return get_timer_masked () - base;
133 }
134
135 /*
136  * This function is derived from PowerPC code (timebase clock frequency).
137  * On ARM it returns the number of timer ticks per second.
138  */
139 ulong get_tbclk(void)
140 {
141         return timer_freq;
142 }