blob: 80751add84eca84b804d0d9a8343a3de68b339ec [file] [log] [blame]
Sergey Kubushync74b2102007-08-10 20:26:18 +02001/*
2 * (C) Copyright 2003
3 * Texas Instruments <www.ti.com>
4 *
5 * (C) Copyright 2002
6 * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
7 * Marius Groeger <mgroeger@sysgo.de>
8 *
9 * (C) Copyright 2002
10 * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
11 * Alex Zuepke <azu@sysgo.de>
12 *
13 * (C) Copyright 2002-2004
Detlev Zundel792a09e2009-05-13 10:54:10 +020014 * Gary Jennejohn, DENX Software Engineering, <garyj@denx.de>
Sergey Kubushync74b2102007-08-10 20:26:18 +020015 *
16 * (C) Copyright 2004
17 * Philippe Robin, ARM Ltd. <philippe.robin@arm.com>
18 *
19 * Copyright (C) 2007 Sergey Kubushyn <ksi@koi8.net>
20 *
21 * See file CREDITS for list of people who contributed to this
22 * project.
23 *
24 * This program is free software; you can redistribute it and/or
25 * modify it under the terms of the GNU General Public License as
26 * published by the Free Software Foundation; either version 2 of
27 * the License, or (at your option) any later version.
28 *
29 * This program is distributed in the hope that it will be useful,
30 * but WITHOUT ANY WARRANTY; without even the implied warranty of
31 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
32 * GNU General Public License for more details.
33 *
34 * You should have received a copy of the GNU General Public License
35 * along with this program; if not, write to the Free Software
36 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
37 * MA 02111-1307 USA
38 */
39
40#include <common.h>
Sergey Kubushync74b2102007-08-10 20:26:18 +020041
42typedef volatile struct {
43 u_int32_t pid12;
Dirk Behme80c40b72008-03-26 09:53:29 +010044 u_int32_t emumgt;
45 u_int32_t na1;
46 u_int32_t na2;
Sergey Kubushync74b2102007-08-10 20:26:18 +020047 u_int32_t tim12;
48 u_int32_t tim34;
49 u_int32_t prd12;
50 u_int32_t prd34;
51 u_int32_t tcr;
52 u_int32_t tgcr;
53 u_int32_t wdtcr;
Sergey Kubushync74b2102007-08-10 20:26:18 +020054} davinci_timer;
55
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020056davinci_timer *timer = (davinci_timer *)CONFIG_SYS_TIMERBASE;
Sergey Kubushync74b2102007-08-10 20:26:18 +020057
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020058#define TIMER_LOAD_VAL (CONFIG_SYS_HZ_CLOCK / CONFIG_SYS_HZ)
Dirk Behme80c40b72008-03-26 09:53:29 +010059#define TIM_CLK_DIV 16
Peter Pearseea686f52008-02-01 16:50:24 +000060
Sergey Kubushync74b2102007-08-10 20:26:18 +020061static ulong timestamp;
62static ulong lastinc;
63
64int timer_init(void)
65{
66 /* We are using timer34 in unchained 32-bit mode, full speed */
67 timer->tcr = 0x0;
68 timer->tgcr = 0x0;
Dirk Behme80c40b72008-03-26 09:53:29 +010069 timer->tgcr = 0x06 | ((TIM_CLK_DIV - 1) << 8);
Sergey Kubushync74b2102007-08-10 20:26:18 +020070 timer->tim34 = 0x0;
71 timer->prd34 = TIMER_LOAD_VAL;
72 lastinc = 0;
Sergey Kubushync74b2102007-08-10 20:26:18 +020073 timestamp = 0;
Dirk Behme80c40b72008-03-26 09:53:29 +010074 timer->tcr = 2 << 22;
Sergey Kubushync74b2102007-08-10 20:26:18 +020075
76 return(0);
77}
78
79void reset_timer(void)
80{
Dirk Behme80c40b72008-03-26 09:53:29 +010081 timer->tcr = 0x0;
82 timer->tim34 = 0;
83 lastinc = 0;
84 timestamp = 0;
85 timer->tcr = 2 << 22;
86}
87
88static ulong get_timer_raw(void)
89{
90 ulong now = timer->tim34;
91
92 if (now >= lastinc) {
93 /* normal mode */
94 timestamp += now - lastinc;
95 } else {
96 /* overflow ... */
97 timestamp += now + TIMER_LOAD_VAL - lastinc;
98 }
99 lastinc = now;
100 return timestamp;
Sergey Kubushync74b2102007-08-10 20:26:18 +0200101}
102
103ulong get_timer(ulong base)
104{
Dirk Behme80c40b72008-03-26 09:53:29 +0100105 return((get_timer_raw() / (TIMER_LOAD_VAL / TIM_CLK_DIV)) - base);
Wolfgang Denk950a3922008-04-11 15:11:26 +0200106}
Sergey Kubushync74b2102007-08-10 20:26:18 +0200107
108void set_timer(ulong t)
109{
110 timestamp = t;
111}
112
113void udelay(unsigned long usec)
114{
Sergey Kubushync74b2102007-08-10 20:26:18 +0200115 ulong tmo;
116 ulong endtime;
117 signed long diff;
118
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200119 tmo = CONFIG_SYS_HZ_CLOCK / 1000;
Sergey Kubushync74b2102007-08-10 20:26:18 +0200120 tmo *= usec;
Dirk Behme80c40b72008-03-26 09:53:29 +0100121 tmo /= (1000 * TIM_CLK_DIV);
Sergey Kubushync74b2102007-08-10 20:26:18 +0200122
123 endtime = get_timer_raw() + tmo;
124
125 do {
126 ulong now = get_timer_raw();
127 diff = endtime - now;
128 } while (diff >= 0);
129}
130
131/*
132 * This function is derived from PowerPC code (read timebase as long long).
133 * On ARM it just returns the timer value.
134 */
135unsigned long long get_ticks(void)
136{
137 return(get_timer(0));
138}
139
140/*
141 * This function is derived from PowerPC code (timebase clock frequency).
142 * On ARM it returns the number of timer ticks per second.
143 */
144ulong get_tbclk(void)
145{
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200146 return CONFIG_SYS_HZ;
Sergey Kubushync74b2102007-08-10 20:26:18 +0200147}