blob: 943f193ad84110201c1793a7da478954309ad665 [file] [log] [blame]
Matthias Kaehlckefcfb6322010-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 Kaehlcked9f505e2010-02-24 00:22:00 +010034#include <div64.h>
Matthias Kaehlckefcfb6322010-02-01 21:29:39 +010035
36#define TIMER_CLKSEL (1 << 3)
Matthias Kaehlckefcfb6322010-02-01 21:29:39 +010037#define TIMER_ENABLE (1 << 7)
38
Matthias Kaehlcke7e67fb52010-03-09 22:13:20 +010039#define TIMER_FREQ 508469 /* ticks / second */
Matthias Kaehlckec7ad13a2010-02-24 00:22:09 +010040#define TIMER_MAX_VAL 0xFFFFFFFF
Matthias Kaehlckefcfb6322010-02-01 21:29:39 +010041
Matthias Kaehlckec7ad13a2010-02-24 00:22:09 +010042static struct ep93xx_timer
43{
44 unsigned long long ticks;
45 unsigned long last_update;
46} timer;
Matthias Kaehlckefcfb6322010-02-01 21:29:39 +010047
Matthias Kaehlckec7ad13a2010-02-24 00:22:09 +010048static inline unsigned long clk_to_systicks(unsigned long long clk_ticks)
Matthias Kaehlckefcfb6322010-02-01 21:29:39 +010049{
Matthias Kaehlcked9f505e2010-02-24 00:22:00 +010050 unsigned long long sys_ticks = (clk_ticks * CONFIG_SYS_HZ);
51 do_div(sys_ticks, TIMER_FREQ);
Matthias Kaehlckefcfb6322010-02-01 21:29:39 +010052
Matthias Kaehlcked9f505e2010-02-24 00:22:00 +010053 return (unsigned long)sys_ticks;
Matthias Kaehlckefcfb6322010-02-01 21:29:39 +010054}
55
Matthias Kaehlcke7e67fb52010-03-09 22:13:20 +010056static inline unsigned long long usecs_to_ticks(unsigned long usecs)
Matthias Kaehlckefcfb6322010-02-01 21:29:39 +010057{
Matthias Kaehlcke7e67fb52010-03-09 22:13:20 +010058 unsigned long long ticks = (unsigned long long)usecs * TIMER_FREQ;
59 do_div(ticks, 1000 * 1000);
Matthias Kaehlckefcfb6322010-02-01 21:29:39 +010060
61 return ticks;
62}
63
64static inline unsigned long read_timer(void)
65{
Matthias Kaehlcke33eef042010-03-09 22:13:47 +010066 struct timer_regs *timer_regs = (struct timer_regs *)TIMER_BASE;
Matthias Kaehlckefcfb6322010-02-01 21:29:39 +010067
Matthias Kaehlcke33eef042010-03-09 22:13:47 +010068 return TIMER_MAX_VAL - readl(&timer_regs->timer3.value);
Matthias Kaehlckefcfb6322010-02-01 21:29:39 +010069}
70
71/*
Matthias Kaehlcked650da22010-03-09 22:13:33 +010072 * Get the number of ticks (in CONFIG_SYS_HZ resolution)
Matthias Kaehlckefcfb6322010-02-01 21:29:39 +010073 */
74unsigned long long get_ticks(void)
75{
76 const unsigned long now = read_timer();
77
Matthias Kaehlckec7ad13a2010-02-24 00:22:09 +010078 if (now >= timer.last_update)
79 timer.ticks += now - timer.last_update;
80 else
81 /* an overflow occurred */
82 timer.ticks += TIMER_MAX_VAL - timer.last_update + now;
Matthias Kaehlckefcfb6322010-02-01 21:29:39 +010083
Matthias Kaehlckec7ad13a2010-02-24 00:22:09 +010084 timer.last_update = now;
Matthias Kaehlckefcfb6322010-02-01 21:29:39 +010085
Matthias Kaehlcked650da22010-03-09 22:13:33 +010086 return clk_to_systicks(timer.ticks);
Matthias Kaehlckefcfb6322010-02-01 21:29:39 +010087}
88
89unsigned long get_timer_masked(void)
90{
Matthias Kaehlcked650da22010-03-09 22:13:33 +010091 return get_ticks();
Matthias Kaehlckefcfb6322010-02-01 21:29:39 +010092}
93
94unsigned long get_timer(unsigned long base)
95{
96 return get_timer_masked() - base;
97}
98
99void reset_timer_masked(void)
100{
Matthias Kaehlckec7ad13a2010-02-24 00:22:09 +0100101 timer.last_update = read_timer();
102 timer.ticks = 0;
Matthias Kaehlckefcfb6322010-02-01 21:29:39 +0100103}
104
105void reset_timer(void)
106{
107 reset_timer_masked();
108}
109
Matthias Kaehlckefcfb6322010-02-01 21:29:39 +0100110void __udelay(unsigned long usec)
111{
Matthias Kaehlcked650da22010-03-09 22:13:33 +0100112 /* read the timer and update timer.ticks */
113 get_ticks();
Matthias Kaehlckefcfb6322010-02-01 21:29:39 +0100114
Matthias Kaehlcked650da22010-03-09 22:13:33 +0100115 const unsigned long long target = timer.ticks + usecs_to_ticks(usec);
116
117 while (timer.ticks < target)
118 get_ticks();
Matthias Kaehlckefcfb6322010-02-01 21:29:39 +0100119}
120
121int timer_init(void)
122{
Matthias Kaehlcke33eef042010-03-09 22:13:47 +0100123 struct timer_regs *timer_regs = (struct timer_regs *)TIMER_BASE;
Matthias Kaehlckefcfb6322010-02-01 21:29:39 +0100124
125 /* use timer 3 with 508KHz and free running */
Matthias Kaehlcke33eef042010-03-09 22:13:47 +0100126 writel(TIMER_CLKSEL, &timer_regs->timer3.control);
Matthias Kaehlckefcfb6322010-02-01 21:29:39 +0100127
Matthias Kaehlckec7ad13a2010-02-24 00:22:09 +0100128 /* set initial timer value 3 */
Matthias Kaehlcke33eef042010-03-09 22:13:47 +0100129 writel(TIMER_MAX_VAL, &timer_regs->timer3.load);
Matthias Kaehlckefcfb6322010-02-01 21:29:39 +0100130
Matthias Kaehlckec7ad13a2010-02-24 00:22:09 +0100131 /* Enable the timer */
132 writel(TIMER_ENABLE | TIMER_CLKSEL,
Matthias Kaehlcke33eef042010-03-09 22:13:47 +0100133 &timer_regs->timer3.control);
Matthias Kaehlckefcfb6322010-02-01 21:29:39 +0100134
135 reset_timer_masked();
136
137 return 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 */
144unsigned long get_tbclk(void)
145{
146 return CONFIG_SYS_HZ;
147}