blob: b3a450fb310864fe8d8ee3c033b449453d0f2dd4 [file] [log] [blame]
Bo Shen60f3dd32013-05-12 22:40:54 +00001/*
2 * (C) Copyright 2007-2008
3 * Stelian Pop <stelian@popies.net>
4 * Lead Tech Design <www.leadtechdesign.com>
5 *
6 * (C) Copyright 2013
7 * Bo Shen <voice.shen@atmel.com>
8 *
9 * See file CREDITS for list of people who contributed to this
10 * project.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License as
14 * published by the Free Software Foundation; either version 2 of
15 * the License, or (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
25 * MA 02111-1307 USA
26 */
27
28#include <common.h>
29#include <asm/io.h>
30#include <asm/arch/hardware.h>
31#include <asm/arch/at91_pit.h>
32#include <asm/arch/at91_pmc.h>
33#include <asm/arch/clk.h>
34#include <div64.h>
35
36#if !defined(CONFIG_AT91FAMILY)
37# error You need to define CONFIG_AT91FAMILY in your board config!
38#endif
39
40DECLARE_GLOBAL_DATA_PTR;
41
42/*
43 * We're using the SAMA5D3x PITC in 32 bit mode, by
44 * setting the 20 bit counter period to its maximum (0xfffff).
45 * (See the relevant data sheets to understand that this really works)
46 *
47 * We do also mimic the typical powerpc way of incrementing
48 * two 32 bit registers called tbl and tbu.
49 *
50 * Those registers increment at 1/16 the main clock rate.
51 */
52
53#define TIMER_LOAD_VAL 0xfffff
54
55static inline unsigned long long tick_to_time(unsigned long long tick)
56{
57 tick *= CONFIG_SYS_HZ;
58 do_div(tick, gd->arch.timer_rate_hz);
59
60 return tick;
61}
62
63static inline unsigned long long usec_to_tick(unsigned long long usec)
64{
65 usec *= gd->arch.timer_rate_hz;
66 do_div(usec, 1000000);
67
68 return usec;
69}
70
71/*
72 * Use the PITC in full 32 bit incrementing mode
73 */
74int timer_init(void)
75{
76 at91_pit_t *pit = (at91_pit_t *)ATMEL_BASE_PIT;
77
78 /* Enable PITC Clock */
79 at91_periph_clk_enable(ATMEL_ID_SYS);
80
81 /* Enable PITC */
82 writel(TIMER_LOAD_VAL | AT91_PIT_MR_EN , &pit->mr);
83
84 gd->arch.timer_rate_hz = gd->arch.mck_rate_hz / 16;
85 gd->arch.tbu = 0;
86 gd->arch.tbl = 0;
87
88 return 0;
89}
90
91/*
92 * Get the current 64 bit timer tick count
93 */
94unsigned long long get_ticks(void)
95{
96 at91_pit_t *pit = (at91_pit_t *)ATMEL_BASE_PIT;
97
98 ulong now = readl(&pit->piir);
99
100 /* increment tbu if tbl has rolled over */
101 if (now < gd->arch.tbl)
102 gd->arch.tbu++;
103 gd->arch.tbl = now;
104 return (((unsigned long long)gd->arch.tbu) << 32) | gd->arch.tbl;
105}
106
107void __udelay(unsigned long usec)
108{
109 unsigned long long start;
110 ulong tmo;
111
112 start = get_ticks(); /* get current timestamp */
113 tmo = usec_to_tick(usec); /* convert usecs to ticks */
114 while ((get_ticks() - start) < tmo)
115 ; /* loop till time has passed */
116}
117
118/*
119 * get_timer(base) can be used to check for timeouts or
120 * to measure elasped time relative to an event:
121 *
122 * ulong start_time = get_timer(0) sets start_time to the current
123 * time value.
124 * get_timer(start_time) returns the time elapsed since then.
125 *
126 * The time is used in CONFIG_SYS_HZ units!
127 */
128ulong get_timer(ulong base)
129{
130 return tick_to_time(get_ticks()) - base;
131}
132
133/*
134 * Return the number of timer ticks per second.
135 */
136ulong get_tbclk(void)
137{
138 return gd->arch.timer_rate_hz;
139}