blob: d21eebfb4eb1766cc43909c7f2e3046aab615d1e [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>
Jean-Christophe PLAGNIOL-VILLARD1d4a3792009-04-16 21:30:48 +020029#include <asm/arch/clk.h>
Stelian Popd4bfbc52008-03-26 20:52:32 +010030#include <asm/arch/io.h>
Jean-Christophe PLAGNIOL-VILLARD1d4a3792009-04-16 21:30:48 +020031#include <div64.h>
Stelian Popd1aea1c2008-01-30 21:15:54 +000032
33/*
Stelian Popeea44aa2008-03-26 20:52:28 +010034 * We're using the AT91CAP9/SAM9 PITC in 32 bit mode, by
Stelian Popd1aea1c2008-01-30 21:15:54 +000035 * setting the 20 bit counter period to its maximum (0xfffff).
36 */
37#define TIMER_LOAD_VAL 0xfffff
Stelian Popd1aea1c2008-01-30 21:15:54 +000038
Jean-Christophe PLAGNIOL-VILLARD1d4a3792009-04-16 21:30:48 +020039static ulong timestamp;
40static ulong lastinc;
41static ulong timer_freq;
42
43static 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
51static 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}
Stelian Popd1aea1c2008-01-30 21:15:54 +000058
Stelian Popd1aea1c2008-01-30 21:15:54 +000059/* nothing really to do with interrupts, just starts up a counter. */
Stelian Pop6bf2de22008-03-26 21:52:27 +010060int timer_init(void)
Stelian Popd1aea1c2008-01-30 21:15:54 +000061{
Jens Scharsiga4db1ca2010-02-03 22:46:58 +010062 at91_pmc_t *pmc = (at91_pmc_t *) AT91_PMC_BASE;
63 at91_pit_t *pit = (at91_pit_t *) AT91_PIT_BASE;
Stelian Popd1aea1c2008-01-30 21:15:54 +000064 /*
65 * Enable PITC Clock
66 * The clock is already enabled for system controller in boot
67 */
Jens Scharsiga4db1ca2010-02-03 22:46:58 +010068 writel(1 << AT91_ID_SYS, &pmc->pcer);
Stelian Popd1aea1c2008-01-30 21:15:54 +000069
70 /* Enable PITC */
Jens Scharsiga4db1ca2010-02-03 22:46:58 +010071 writel(TIMER_LOAD_VAL | AT91_PIT_MR_EN , &pit->mr);
Stelian Popd1aea1c2008-01-30 21:15:54 +000072
73 reset_timer_masked();
74
Jean-Christophe PLAGNIOL-VILLARD1d4a3792009-04-16 21:30:48 +020075 timer_freq = get_mck_clk_rate() >> 4;
76
Stelian Popd1aea1c2008-01-30 21:15:54 +000077 return 0;
78}
79
80/*
81 * timer without interrupts
82 */
Jean-Christophe PLAGNIOL-VILLARD1d4a3792009-04-16 21:30:48 +020083unsigned long long get_ticks(void)
Stelian Popd1aea1c2008-01-30 21:15:54 +000084{
Jens Scharsiga4db1ca2010-02-03 22:46:58 +010085 at91_pit_t *pit = (at91_pit_t *) AT91_PIT_BASE;
86
87 ulong now = readl(&pit->piir);
Stelian Popd4bfbc52008-03-26 20:52:32 +010088
Jean-Christophe PLAGNIOL-VILLARD1d4a3792009-04-16 21:30:48 +020089 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;
Stelian Popd1aea1c2008-01-30 21:15:54 +000096}
97
98void reset_timer_masked(void)
99{
Jean-Christophe PLAGNIOL-VILLARD1d4a3792009-04-16 21:30:48 +0200100 /* reset time */
Jens Scharsiga4db1ca2010-02-03 22:46:58 +0100101 at91_pit_t *pit = (at91_pit_t *) AT91_PIT_BASE;
102
103 /* capture current incrementer value time */
104 lastinc = readl(&pit->piir);
Jean-Christophe PLAGNIOL-VILLARD1d4a3792009-04-16 21:30:48 +0200105 timestamp = 0; /* start "advancing" time stamp from 0 */
Stelian Popd1aea1c2008-01-30 21:15:54 +0000106}
107
108ulong get_timer_masked(void)
109{
Jean-Christophe PLAGNIOL-VILLARD1d4a3792009-04-16 21:30:48 +0200110 return tick_to_time(get_ticks());
Stelian Popd1aea1c2008-01-30 21:15:54 +0000111}
112
Ingo van Lilf0f778a2009-11-24 14:09:21 +0100113void __udelay(unsigned long usec)
Stelian Popd1aea1c2008-01-30 21:15:54 +0000114{
Jean-Christophe PLAGNIOL-VILLARD1d4a3792009-04-16 21:30:48 +0200115 unsigned long long tmp;
116 ulong tmo;
Stelian Popd1aea1c2008-01-30 21:15:54 +0000117
Jean-Christophe PLAGNIOL-VILLARD1d4a3792009-04-16 21:30:48 +0200118 tmo = usec_to_tick(usec);
119 tmp = get_ticks() + tmo; /* get current timestamp */
120
121 while (get_ticks() < tmp) /* loop till event */
122 /*NOP*/;
Stelian Popd1aea1c2008-01-30 21:15:54 +0000123}
124
125void reset_timer(void)
126{
127 reset_timer_masked();
128}
129
130ulong get_timer(ulong base)
131{
Jean-Christophe PLAGNIOL-VILLARD1d4a3792009-04-16 21:30:48 +0200132 return get_timer_masked () - base;
Stelian Popd1aea1c2008-01-30 21:15:54 +0000133}
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 */
139ulong get_tbclk(void)
140{
141 ulong tbclk;
Stelian Popd4bfbc52008-03-26 20:52:32 +0100142
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200143 tbclk = CONFIG_SYS_HZ;
Stelian Popd1aea1c2008-01-30 21:15:54 +0000144 return tbclk;
145}