blob: c84cb5e96f73ebb03054551388c3752efdad7bb2 [file] [log] [blame]
Stelian Popd1aea1c2008-01-30 21:15:54 +00001/*
2 * (C) Copyright 2007-2008
Stelian Pop50497522008-05-08 22:52:09 +02003 * Stelian Pop <stelian.pop@leadtechdesign.com>
Stelian Popd1aea1c2008-01-30 21:15:54 +00004 * 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>
Stelian Popd4bfbc52008-03-26 20:52:32 +010027#include <asm/arch/at91_pit.h>
28#include <asm/arch/at91_pmc.h>
29#include <asm/arch/at91_rstc.h>
Jean-Christophe PLAGNIOL-VILLARD1d4a3792009-04-16 21:30:48 +020030#include <asm/arch/clk.h>
Stelian Popd4bfbc52008-03-26 20:52:32 +010031#include <asm/arch/io.h>
Jean-Christophe PLAGNIOL-VILLARD1d4a3792009-04-16 21:30:48 +020032#include <div64.h>
Stelian Popd1aea1c2008-01-30 21:15:54 +000033
34/*
Stelian Popeea44aa2008-03-26 20:52:28 +010035 * We're using the AT91CAP9/SAM9 PITC in 32 bit mode, by
Stelian Popd1aea1c2008-01-30 21:15:54 +000036 * setting the 20 bit counter period to its maximum (0xfffff).
37 */
38#define TIMER_LOAD_VAL 0xfffff
Stelian Popd4bfbc52008-03-26 20:52:32 +010039#define READ_RESET_TIMER at91_sys_read(AT91_PIT_PIVR)
40#define READ_TIMER at91_sys_read(AT91_PIT_PIIR)
Stelian Popd1aea1c2008-01-30 21:15:54 +000041
Jean-Christophe PLAGNIOL-VILLARD1d4a3792009-04-16 21:30:48 +020042static ulong timestamp;
43static ulong lastinc;
44static ulong timer_freq;
45
46static inline unsigned long long tick_to_time(unsigned long long tick)
47{
48 tick *= CONFIG_SYS_HZ;
49 do_div(tick, timer_freq);
50
51 return tick;
52}
53
54static inline unsigned long long usec_to_tick(unsigned long long usec)
55{
56 usec *= timer_freq;
57 do_div(usec, 1000000);
58
59 return usec;
60}
Stelian Popd1aea1c2008-01-30 21:15:54 +000061
Stelian Popd1aea1c2008-01-30 21:15:54 +000062/* nothing really to do with interrupts, just starts up a counter. */
Stelian Pop6bf2de22008-03-26 21:52:27 +010063int timer_init(void)
Stelian Popd1aea1c2008-01-30 21:15:54 +000064{
65 /*
66 * Enable PITC Clock
67 * The clock is already enabled for system controller in boot
68 */
Stelian Popd4bfbc52008-03-26 20:52:32 +010069 at91_sys_write(AT91_PMC_PCER, 1 << AT91_ID_SYS);
Stelian Popd1aea1c2008-01-30 21:15:54 +000070
71 /* Enable PITC */
Stelian Popd4bfbc52008-03-26 20:52:32 +010072 at91_sys_write(AT91_PIT_MR, TIMER_LOAD_VAL | AT91_PIT_PITEN);
Stelian Popd1aea1c2008-01-30 21:15:54 +000073
74 reset_timer_masked();
75
Jean-Christophe PLAGNIOL-VILLARD1d4a3792009-04-16 21:30:48 +020076 timer_freq = get_mck_clk_rate() >> 4;
77
Stelian Popd1aea1c2008-01-30 21:15:54 +000078 return 0;
79}
80
81/*
82 * timer without interrupts
83 */
Jean-Christophe PLAGNIOL-VILLARD1d4a3792009-04-16 21:30:48 +020084unsigned long long get_ticks(void)
Stelian Popd1aea1c2008-01-30 21:15:54 +000085{
86 ulong now = READ_TIMER;
Stelian Popd4bfbc52008-03-26 20:52:32 +010087
Jean-Christophe PLAGNIOL-VILLARD1d4a3792009-04-16 21:30:48 +020088 if (now >= lastinc) /* normal mode (non roll) */
89 /* move stamp forward with absolut diff ticks */
90 timestamp += (now - lastinc);
91 else /* we have rollover of incrementer */
92 timestamp += (0xFFFFFFFF - lastinc) + now;
93 lastinc = now;
94 return timestamp;
Stelian Popd1aea1c2008-01-30 21:15:54 +000095}
96
97void reset_timer_masked(void)
98{
Jean-Christophe PLAGNIOL-VILLARD1d4a3792009-04-16 21:30:48 +020099 /* reset time */
100 lastinc = READ_TIMER; /* capture current incrementer value time */
101 timestamp = 0; /* start "advancing" time stamp from 0 */
Stelian Popd1aea1c2008-01-30 21:15:54 +0000102}
103
104ulong get_timer_masked(void)
105{
Jean-Christophe PLAGNIOL-VILLARD1d4a3792009-04-16 21:30:48 +0200106 return tick_to_time(get_ticks());
Stelian Popd1aea1c2008-01-30 21:15:54 +0000107}
108
Jean-Christophe PLAGNIOL-VILLARD1d4a3792009-04-16 21:30:48 +0200109void udelay(unsigned long usec)
Stelian Popd1aea1c2008-01-30 21:15:54 +0000110{
Jean-Christophe PLAGNIOL-VILLARD1d4a3792009-04-16 21:30:48 +0200111 unsigned long long tmp;
112 ulong tmo;
Stelian Popd1aea1c2008-01-30 21:15:54 +0000113
Jean-Christophe PLAGNIOL-VILLARD1d4a3792009-04-16 21:30:48 +0200114 tmo = usec_to_tick(usec);
115 tmp = get_ticks() + tmo; /* get current timestamp */
116
117 while (get_ticks() < tmp) /* loop till event */
118 /*NOP*/;
Stelian Popd1aea1c2008-01-30 21:15:54 +0000119}
120
121void reset_timer(void)
122{
123 reset_timer_masked();
124}
125
126ulong get_timer(ulong base)
127{
Jean-Christophe PLAGNIOL-VILLARD1d4a3792009-04-16 21:30:48 +0200128 return get_timer_masked () - base;
Stelian Popd1aea1c2008-01-30 21:15:54 +0000129}
130
131/*
132 * This function is derived from PowerPC code (timebase clock frequency).
133 * On ARM it returns the number of timer ticks per second.
134 */
135ulong get_tbclk(void)
136{
137 ulong tbclk;
Stelian Popd4bfbc52008-03-26 20:52:32 +0100138
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200139 tbclk = CONFIG_SYS_HZ;
Stelian Popd1aea1c2008-01-30 21:15:54 +0000140 return tbclk;
141}
142
143/*
Stelian Popeea44aa2008-03-26 20:52:28 +0100144 * Reset the cpu by setting up the watchdog timer and let him time out.
Stelian Popd1aea1c2008-01-30 21:15:54 +0000145 */
146void reset_cpu(ulong ignored)
147{
148 /* this is the way Linux does it */
Stelian Popd4bfbc52008-03-26 20:52:32 +0100149 at91_sys_write(AT91_RSTC_CR, AT91_RSTC_KEY |
150 AT91_RSTC_PROCRST |
151 AT91_RSTC_PERRST);
Stelian Popd1aea1c2008-01-30 21:15:54 +0000152
153 while (1);
154 /* Never reached */
155}