blob: f19ec611d11120554c6dbbeff3c794cf2a23b008 [file] [log] [blame]
Matthias Kaehlckefcfb6322010-02-01 21:29:39 +01001/*
2 * Cirrus Logic EP93xx timer support.
3 *
Matthias Kaehlckeb0326982010-03-09 22:13:56 +01004 * Copyright (C) 2009, 2010 Matthias Kaehlcke <matthias@kaehlcke.net>
Matthias Kaehlckefcfb6322010-02-01 21:29:39 +01005 *
6 * Copyright (C) 2004, 2005
7 * Cory T. Tusar, Videon Central, Inc., <ctusar@videon-central.com>
8 *
9 * Based on the original intr.c Cirrus Logic EP93xx Rev D. interrupt support,
10 * author unknown.
11 *
12 * See file CREDITS for list of people who contributed to this project.
13 *
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
18 *
19 * This program is distributed in the hope that it will be useful, but
20 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
21 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 * for more details.
23 *
24 * You should have received a copy of the GNU General Public License along
25 * with this program; if not, write to the Free Software Foundation, Inc.,
26 * 675 Mass Ave, Cambridge, MA 02139, USA.
27 */
28
29#include <common.h>
30#include <linux/types.h>
31#include <asm/arch/ep93xx.h>
32#include <asm/io.h>
Matthias Kaehlcked9f505e2010-02-24 00:22:00 +010033#include <div64.h>
Matthias Kaehlckefcfb6322010-02-01 21:29:39 +010034
35#define TIMER_CLKSEL (1 << 3)
Matthias Kaehlckefcfb6322010-02-01 21:29:39 +010036#define TIMER_ENABLE (1 << 7)
37
Matthias Kaehlcke7e67fb52010-03-09 22:13:20 +010038#define TIMER_FREQ 508469 /* ticks / second */
Matthias Kaehlckec7ad13a2010-02-24 00:22:09 +010039#define TIMER_MAX_VAL 0xFFFFFFFF
Matthias Kaehlckefcfb6322010-02-01 21:29:39 +010040
Matthias Kaehlckec7ad13a2010-02-24 00:22:09 +010041static struct ep93xx_timer
42{
43 unsigned long long ticks;
Matthias Kaehlckeb0326982010-03-09 22:13:56 +010044 unsigned long last_read;
Matthias Kaehlckec7ad13a2010-02-24 00:22:09 +010045} timer;
Matthias Kaehlckefcfb6322010-02-01 21:29:39 +010046
Matthias Kaehlcke7e67fb52010-03-09 22:13:20 +010047static inline unsigned long long usecs_to_ticks(unsigned long usecs)
Matthias Kaehlckefcfb6322010-02-01 21:29:39 +010048{
Matthias Kaehlcke7e67fb52010-03-09 22:13:20 +010049 unsigned long long ticks = (unsigned long long)usecs * TIMER_FREQ;
50 do_div(ticks, 1000 * 1000);
Matthias Kaehlckefcfb6322010-02-01 21:29:39 +010051
52 return ticks;
53}
54
Matthias Kaehlckeb0326982010-03-09 22:13:56 +010055static inline void read_timer(void)
Matthias Kaehlckefcfb6322010-02-01 21:29:39 +010056{
Matthias Kaehlcke33eef042010-03-09 22:13:47 +010057 struct timer_regs *timer_regs = (struct timer_regs *)TIMER_BASE;
Matthias Kaehlckeb0326982010-03-09 22:13:56 +010058 const unsigned long now = TIMER_MAX_VAL - readl(&timer_regs->timer3.value);
Matthias Kaehlckefcfb6322010-02-01 21:29:39 +010059
Matthias Kaehlckeb0326982010-03-09 22:13:56 +010060 if (now >= timer.last_read)
61 timer.ticks += now - timer.last_read;
62 else
63 /* an overflow occurred */
64 timer.ticks += TIMER_MAX_VAL - timer.last_read + now;
65
66 timer.last_read = now;
Matthias Kaehlckefcfb6322010-02-01 21:29:39 +010067}
68
69/*
Matthias Kaehlcked650da22010-03-09 22:13:33 +010070 * Get the number of ticks (in CONFIG_SYS_HZ resolution)
Matthias Kaehlckefcfb6322010-02-01 21:29:39 +010071 */
72unsigned long long get_ticks(void)
73{
Matthias Kaehlckeb0326982010-03-09 22:13:56 +010074 unsigned long long sys_ticks;
Matthias Kaehlckefcfb6322010-02-01 21:29:39 +010075
Matthias Kaehlckeb0326982010-03-09 22:13:56 +010076 read_timer();
Matthias Kaehlckefcfb6322010-02-01 21:29:39 +010077
Matthias Kaehlckeb0326982010-03-09 22:13:56 +010078 sys_ticks = timer.ticks * CONFIG_SYS_HZ;
79 do_div(sys_ticks, TIMER_FREQ);
Matthias Kaehlckefcfb6322010-02-01 21:29:39 +010080
Matthias Kaehlckeb0326982010-03-09 22:13:56 +010081 return sys_ticks;
Matthias Kaehlckefcfb6322010-02-01 21:29:39 +010082}
83
84unsigned long get_timer_masked(void)
85{
Matthias Kaehlcked650da22010-03-09 22:13:33 +010086 return get_ticks();
Matthias Kaehlckefcfb6322010-02-01 21:29:39 +010087}
88
89unsigned long get_timer(unsigned long base)
90{
91 return get_timer_masked() - base;
92}
93
94void reset_timer_masked(void)
95{
Matthias Kaehlckeb0326982010-03-09 22:13:56 +010096 read_timer();
Matthias Kaehlckec7ad13a2010-02-24 00:22:09 +010097 timer.ticks = 0;
Matthias Kaehlckefcfb6322010-02-01 21:29:39 +010098}
99
Matthias Kaehlckefcfb6322010-02-01 21:29:39 +0100100void __udelay(unsigned long usec)
101{
Matthias Kaehlckeb0326982010-03-09 22:13:56 +0100102 unsigned long long target;
Matthias Kaehlckefcfb6322010-02-01 21:29:39 +0100103
Matthias Kaehlckeb0326982010-03-09 22:13:56 +0100104 read_timer();
105
106 target = timer.ticks + usecs_to_ticks(usec);
Matthias Kaehlcked650da22010-03-09 22:13:33 +0100107
108 while (timer.ticks < target)
Matthias Kaehlckeb0326982010-03-09 22:13:56 +0100109 read_timer();
Matthias Kaehlckefcfb6322010-02-01 21:29:39 +0100110}
111
112int timer_init(void)
113{
Matthias Kaehlcke33eef042010-03-09 22:13:47 +0100114 struct timer_regs *timer_regs = (struct timer_regs *)TIMER_BASE;
Matthias Kaehlckefcfb6322010-02-01 21:29:39 +0100115
Matthias Kaehlckeb0326982010-03-09 22:13:56 +0100116 /* use timer 3 with 508KHz and free running, not enabled now */
Matthias Kaehlcke33eef042010-03-09 22:13:47 +0100117 writel(TIMER_CLKSEL, &timer_regs->timer3.control);
Matthias Kaehlckefcfb6322010-02-01 21:29:39 +0100118
Matthias Kaehlckeb0326982010-03-09 22:13:56 +0100119 /* set initial timer value */
Matthias Kaehlcke33eef042010-03-09 22:13:47 +0100120 writel(TIMER_MAX_VAL, &timer_regs->timer3.load);
Matthias Kaehlckefcfb6322010-02-01 21:29:39 +0100121
Matthias Kaehlckec7ad13a2010-02-24 00:22:09 +0100122 /* Enable the timer */
123 writel(TIMER_ENABLE | TIMER_CLKSEL,
Matthias Kaehlcke33eef042010-03-09 22:13:47 +0100124 &timer_regs->timer3.control);
Matthias Kaehlckefcfb6322010-02-01 21:29:39 +0100125
126 reset_timer_masked();
127
128 return 0;
129}
130
131/*
132 * This function is derived from PowerPC code (timebase clock frequency).
133 * On ARM it returns the number of timer ticks per second.
134 */
135unsigned long get_tbclk(void)
136{
137 return CONFIG_SYS_HZ;
138}