blob: 7c2c208253c0510d9b5bc9f87eefaf2a6ac70839 [file] [log] [blame]
Sergey Kubushyne8f39122007-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 Zundelf1b3f2b2009-05-13 10:54:10 +020014 * Gary Jennejohn, DENX Software Engineering, <garyj@denx.de>
Sergey Kubushyne8f39122007-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>
Nick Thompson283398b2009-11-12 11:02:17 -050041#include <asm/io.h>
Sergey Kubushyne8f39122007-08-10 20:26:18 +020042
Nick Thompson283398b2009-11-12 11:02:17 -050043struct davinci_timer {
Sergey Kubushyne8f39122007-08-10 20:26:18 +020044 u_int32_t pid12;
Dirk Behme866c3212008-03-26 09:53:29 +010045 u_int32_t emumgt;
46 u_int32_t na1;
47 u_int32_t na2;
Sergey Kubushyne8f39122007-08-10 20:26:18 +020048 u_int32_t tim12;
49 u_int32_t tim34;
50 u_int32_t prd12;
51 u_int32_t prd34;
52 u_int32_t tcr;
53 u_int32_t tgcr;
54 u_int32_t wdtcr;
Nick Thompson283398b2009-11-12 11:02:17 -050055};
Sergey Kubushyne8f39122007-08-10 20:26:18 +020056
Nick Thompson283398b2009-11-12 11:02:17 -050057static struct davinci_timer * const timer =
58 (struct davinci_timer *)CONFIG_SYS_TIMERBASE;
Sergey Kubushyne8f39122007-08-10 20:26:18 +020059
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020060#define TIMER_LOAD_VAL (CONFIG_SYS_HZ_CLOCK / CONFIG_SYS_HZ)
Dirk Behme866c3212008-03-26 09:53:29 +010061#define TIM_CLK_DIV 16
Peter Pearsee7680982008-02-01 16:50:24 +000062
Sergey Kubushyne8f39122007-08-10 20:26:18 +020063static ulong timestamp;
64static ulong lastinc;
65
66int timer_init(void)
67{
68 /* We are using timer34 in unchained 32-bit mode, full speed */
Nick Thompson283398b2009-11-12 11:02:17 -050069 writel(0x0, &timer->tcr);
70 writel(0x0, &timer->tgcr);
71 writel(0x06 | ((TIM_CLK_DIV - 1) << 8), &timer->tgcr);
72 writel(0x0, &timer->tim34);
73 writel(TIMER_LOAD_VAL, &timer->prd34);
Sergey Kubushyne8f39122007-08-10 20:26:18 +020074 lastinc = 0;
Sergey Kubushyne8f39122007-08-10 20:26:18 +020075 timestamp = 0;
Nick Thompson283398b2009-11-12 11:02:17 -050076 writel(2 << 22, &timer->tcr);
Sergey Kubushyne8f39122007-08-10 20:26:18 +020077
78 return(0);
79}
80
81void reset_timer(void)
82{
Nick Thompson283398b2009-11-12 11:02:17 -050083 writel(0x0, &timer->tcr);
84 writel(0x0, &timer->tim34);
Dirk Behme866c3212008-03-26 09:53:29 +010085 lastinc = 0;
Sergey Kubushyne8f39122007-08-10 20:26:18 +020086 timestamp = 0;
Nick Thompson283398b2009-11-12 11:02:17 -050087 writel(2 << 22, &timer->tcr);
Sergey Kubushyne8f39122007-08-10 20:26:18 +020088}
89
Dirk Behme866c3212008-03-26 09:53:29 +010090static ulong get_timer_raw(void)
Sergey Kubushyne8f39122007-08-10 20:26:18 +020091{
Nick Thompson283398b2009-11-12 11:02:17 -050092 ulong now = readl(&timer->tim34);
Sergey Kubushyne8f39122007-08-10 20:26:18 +020093
94 if (now >= lastinc) {
95 /* normal mode */
96 timestamp += now - lastinc;
97 } else {
98 /* overflow ... */
Dirk Behme866c3212008-03-26 09:53:29 +010099 timestamp += now + TIMER_LOAD_VAL - lastinc;
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200100 }
101 lastinc = now;
102 return timestamp;
103}
104
Dirk Behme866c3212008-03-26 09:53:29 +0100105ulong get_timer(ulong base)
106{
107 return((get_timer_raw() / (TIMER_LOAD_VAL / TIM_CLK_DIV)) - base);
108}
109
110void set_timer(ulong t)
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200111{
Dirk Behme866c3212008-03-26 09:53:29 +0100112 timestamp = t;
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200113}
114
Dirk Behme866c3212008-03-26 09:53:29 +0100115void udelay(unsigned long usec)
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200116{
117 ulong tmo;
118 ulong endtime;
119 signed long diff;
120
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200121 tmo = CONFIG_SYS_HZ_CLOCK / 1000;
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200122 tmo *= usec;
Dirk Behme866c3212008-03-26 09:53:29 +0100123 tmo /= (1000 * TIM_CLK_DIV);
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200124
125 endtime = get_timer_raw() + tmo;
126
127 do {
128 ulong now = get_timer_raw();
129 diff = endtime - now;
130 } while (diff >= 0);
131}
132
133/*
134 * This function is derived from PowerPC code (read timebase as long long).
135 * On ARM it just returns the timer value.
136 */
137unsigned long long get_ticks(void)
138{
139 return(get_timer(0));
140}
141
142/*
143 * This function is derived from PowerPC code (timebase clock frequency).
144 * On ARM it returns the number of timer ticks per second.
145 */
146ulong get_tbclk(void)
147{
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200148 return CONFIG_SYS_HZ;
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200149}