blob: b1a01a05e9c72f48028dd602098ed90d4e3704f0 [file] [log] [blame]
Matthias Kaehlcke195dbd12010-02-01 21:29:39 +01001/*
2 * Cirrus Logic EP93xx timer support.
3 *
4 * Copyright (C) 2009, 2010
5 * Matthias Kaehlcke <matthias@kaehlcke.net>
6 *
7 * Copyright (C) 2004, 2005
8 * Cory T. Tusar, Videon Central, Inc., <ctusar@videon-central.com>
9 *
10 * Based on the original intr.c Cirrus Logic EP93xx Rev D. interrupt support,
11 * author unknown.
12 *
13 * See file CREDITS for list of people who contributed to this project.
14 *
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
19 *
20 * This program is distributed in the hope that it will be useful, but
21 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
22 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
23 * for more details.
24 *
25 * You should have received a copy of the GNU General Public License along
26 * with this program; if not, write to the Free Software Foundation, Inc.,
27 * 675 Mass Ave, Cambridge, MA 02139, USA.
28 */
29
30#include <common.h>
31#include <linux/types.h>
32#include <asm/arch/ep93xx.h>
33#include <asm/io.h>
Matthias Kaehlckef56e70f2010-02-24 00:22:00 +010034#include <div64.h>
Matthias Kaehlcke195dbd12010-02-01 21:29:39 +010035
36#define TIMER_CLKSEL (1 << 3)
Matthias Kaehlcke195dbd12010-02-01 21:29:39 +010037#define TIMER_ENABLE (1 << 7)
38
Matthias Kaehlcke8df23b72010-02-24 00:22:09 +010039#define TIMER_FREQ 508469
40#define TIMER_MAX_VAL 0xFFFFFFFF
Matthias Kaehlcke195dbd12010-02-01 21:29:39 +010041
Matthias Kaehlcke8df23b72010-02-24 00:22:09 +010042static struct ep93xx_timer
43{
44 unsigned long long ticks;
45 unsigned long last_update;
46} timer;
Matthias Kaehlcke195dbd12010-02-01 21:29:39 +010047
Matthias Kaehlcke8df23b72010-02-24 00:22:09 +010048static inline unsigned long clk_to_systicks(unsigned long long clk_ticks)
Matthias Kaehlcke195dbd12010-02-01 21:29:39 +010049{
Matthias Kaehlckef56e70f2010-02-24 00:22:00 +010050 unsigned long long sys_ticks = (clk_ticks * CONFIG_SYS_HZ);
51 do_div(sys_ticks, TIMER_FREQ);
Matthias Kaehlcke195dbd12010-02-01 21:29:39 +010052
Matthias Kaehlckef56e70f2010-02-24 00:22:00 +010053 return (unsigned long)sys_ticks;
Matthias Kaehlcke195dbd12010-02-01 21:29:39 +010054}
55
56static inline unsigned long usecs_to_ticks(unsigned long usecs)
57{
58 unsigned long ticks;
59
60 if (usecs >= 1000) {
61 ticks = usecs / 1000;
Matthias Kaehlcke8df23b72010-02-24 00:22:09 +010062 ticks *= TIMER_FREQ;
Matthias Kaehlcke195dbd12010-02-01 21:29:39 +010063 ticks /= 1000;
64 } else {
Matthias Kaehlcke8df23b72010-02-24 00:22:09 +010065 ticks = usecs * TIMER_FREQ;
Matthias Kaehlcke195dbd12010-02-01 21:29:39 +010066 ticks /= (1000 * 1000);
67 }
68
69 return ticks;
70}
71
72static inline unsigned long read_timer(void)
73{
74 struct timer_regs *timer = (struct timer_regs *)TIMER_BASE;
75
Matthias Kaehlcke8df23b72010-02-24 00:22:09 +010076 return TIMER_MAX_VAL - readl(&timer->timer3.value);
Matthias Kaehlcke195dbd12010-02-01 21:29:39 +010077}
78
79/*
80 * timer without interrupts
81 */
82unsigned long long get_ticks(void)
83{
84 const unsigned long now = read_timer();
85
Matthias Kaehlcke8df23b72010-02-24 00:22:09 +010086 if (now >= timer.last_update)
87 timer.ticks += now - timer.last_update;
88 else
89 /* an overflow occurred */
90 timer.ticks += TIMER_MAX_VAL - timer.last_update + now;
Matthias Kaehlcke195dbd12010-02-01 21:29:39 +010091
Matthias Kaehlcke8df23b72010-02-24 00:22:09 +010092 timer.last_update = now;
Matthias Kaehlcke195dbd12010-02-01 21:29:39 +010093
Matthias Kaehlcke8df23b72010-02-24 00:22:09 +010094 return timer.ticks;
Matthias Kaehlcke195dbd12010-02-01 21:29:39 +010095}
96
97unsigned long get_timer_masked(void)
98{
99 return clk_to_systicks(get_ticks());
100}
101
102unsigned long get_timer(unsigned long base)
103{
104 return get_timer_masked() - base;
105}
106
107void reset_timer_masked(void)
108{
Matthias Kaehlcke8df23b72010-02-24 00:22:09 +0100109 timer.last_update = read_timer();
110 timer.ticks = 0;
Matthias Kaehlcke195dbd12010-02-01 21:29:39 +0100111}
112
113void reset_timer(void)
114{
115 reset_timer_masked();
116}
117
Matthias Kaehlcke195dbd12010-02-01 21:29:39 +0100118void __udelay(unsigned long usec)
119{
Matthias Kaehlcke8df23b72010-02-24 00:22:09 +0100120 const unsigned long target = get_ticks() + usecs_to_ticks(usec);
Matthias Kaehlcke195dbd12010-02-01 21:29:39 +0100121
Matthias Kaehlcke8df23b72010-02-24 00:22:09 +0100122 while (get_ticks() < target)
Matthias Kaehlcke195dbd12010-02-01 21:29:39 +0100123 /* noop */;
124}
125
126int timer_init(void)
127{
128 struct timer_regs *timer = (struct timer_regs *)TIMER_BASE;
129
130 /* use timer 3 with 508KHz and free running */
131 writel(TIMER_CLKSEL, &timer->timer3.control);
132
Matthias Kaehlcke8df23b72010-02-24 00:22:09 +0100133 /* set initial timer value 3 */
134 writel(TIMER_MAX_VAL, &timer->timer3.load);
Matthias Kaehlcke195dbd12010-02-01 21:29:39 +0100135
Matthias Kaehlcke8df23b72010-02-24 00:22:09 +0100136 /* Enable the timer */
137 writel(TIMER_ENABLE | TIMER_CLKSEL,
Matthias Kaehlcke195dbd12010-02-01 21:29:39 +0100138 &timer->timer3.control);
139
140 reset_timer_masked();
141
142 return 0;
143}
144
145/*
146 * This function is derived from PowerPC code (timebase clock frequency).
147 * On ARM it returns the number of timer ticks per second.
148 */
149unsigned long get_tbclk(void)
150{
151 return CONFIG_SYS_HZ;
152}