blob: 4a0ce4da642081900ee94dcf731ae3b6e3dac089 [file] [log] [blame]
Matthias Kaehlcke195dbd12010-02-01 21:29:39 +01001/*
2 * Cirrus Logic EP93xx timer support.
3 *
Matthias Kaehlcke37911ae2010-03-09 22:13:56 +01004 * Copyright (C) 2009, 2010 Matthias Kaehlcke <matthias@kaehlcke.net>
Matthias Kaehlcke195dbd12010-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 Kaehlckef56e70f2010-02-24 00:22:00 +010033#include <div64.h>
Matthias Kaehlcke195dbd12010-02-01 21:29:39 +010034
35#define TIMER_CLKSEL (1 << 3)
Matthias Kaehlcke195dbd12010-02-01 21:29:39 +010036#define TIMER_ENABLE (1 << 7)
37
Matthias Kaehlcke64d667b2010-03-09 22:13:20 +010038#define TIMER_FREQ 508469 /* ticks / second */
Matthias Kaehlcke8df23b72010-02-24 00:22:09 +010039#define TIMER_MAX_VAL 0xFFFFFFFF
Matthias Kaehlcke195dbd12010-02-01 21:29:39 +010040
Matthias Kaehlcke8df23b72010-02-24 00:22:09 +010041static struct ep93xx_timer
42{
43 unsigned long long ticks;
Matthias Kaehlcke37911ae2010-03-09 22:13:56 +010044 unsigned long last_read;
Matthias Kaehlcke8df23b72010-02-24 00:22:09 +010045} timer;
Matthias Kaehlcke195dbd12010-02-01 21:29:39 +010046
Matthias Kaehlcke64d667b2010-03-09 22:13:20 +010047static inline unsigned long long usecs_to_ticks(unsigned long usecs)
Matthias Kaehlcke195dbd12010-02-01 21:29:39 +010048{
Matthias Kaehlcke64d667b2010-03-09 22:13:20 +010049 unsigned long long ticks = (unsigned long long)usecs * TIMER_FREQ;
50 do_div(ticks, 1000 * 1000);
Matthias Kaehlcke195dbd12010-02-01 21:29:39 +010051
52 return ticks;
53}
54
Matthias Kaehlcke37911ae2010-03-09 22:13:56 +010055static inline void read_timer(void)
Matthias Kaehlcke195dbd12010-02-01 21:29:39 +010056{
Matthias Kaehlcke9d466562010-03-09 22:13:47 +010057 struct timer_regs *timer_regs = (struct timer_regs *)TIMER_BASE;
Matthias Kaehlcke37911ae2010-03-09 22:13:56 +010058 const unsigned long now = TIMER_MAX_VAL - readl(&timer_regs->timer3.value);
59
60 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;
Matthias Kaehlcke195dbd12010-02-01 21:29:39 +010065
Matthias Kaehlcke37911ae2010-03-09 22:13:56 +010066 timer.last_read = now;
Matthias Kaehlcke195dbd12010-02-01 21:29:39 +010067}
68
69/*
Matthias Kaehlcke957f7b52010-03-09 22:13:33 +010070 * Get the number of ticks (in CONFIG_SYS_HZ resolution)
Matthias Kaehlcke195dbd12010-02-01 21:29:39 +010071 */
72unsigned long long get_ticks(void)
73{
Matthias Kaehlcke37911ae2010-03-09 22:13:56 +010074 unsigned long long sys_ticks;
Matthias Kaehlcke195dbd12010-02-01 21:29:39 +010075
Matthias Kaehlcke37911ae2010-03-09 22:13:56 +010076 read_timer();
Matthias Kaehlcke195dbd12010-02-01 21:29:39 +010077
Matthias Kaehlcke37911ae2010-03-09 22:13:56 +010078 sys_ticks = timer.ticks * CONFIG_SYS_HZ;
79 do_div(sys_ticks, TIMER_FREQ);
Matthias Kaehlcke195dbd12010-02-01 21:29:39 +010080
Matthias Kaehlcke37911ae2010-03-09 22:13:56 +010081 return sys_ticks;
Matthias Kaehlcke195dbd12010-02-01 21:29:39 +010082}
83
84unsigned long get_timer_masked(void)
85{
Matthias Kaehlcke957f7b52010-03-09 22:13:33 +010086 return get_ticks();
Matthias Kaehlcke195dbd12010-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 Kaehlcke37911ae2010-03-09 22:13:56 +010096 read_timer();
Matthias Kaehlcke8df23b72010-02-24 00:22:09 +010097 timer.ticks = 0;
Matthias Kaehlcke195dbd12010-02-01 21:29:39 +010098}
99
100void reset_timer(void)
101{
102 reset_timer_masked();
103}
104
Matthias Kaehlcke195dbd12010-02-01 21:29:39 +0100105void __udelay(unsigned long usec)
106{
Matthias Kaehlcke37911ae2010-03-09 22:13:56 +0100107 unsigned long long target;
Matthias Kaehlcke957f7b52010-03-09 22:13:33 +0100108
Matthias Kaehlcke37911ae2010-03-09 22:13:56 +0100109 read_timer();
110
111 target = timer.ticks + usecs_to_ticks(usec);
Matthias Kaehlcke195dbd12010-02-01 21:29:39 +0100112
Matthias Kaehlcke957f7b52010-03-09 22:13:33 +0100113 while (timer.ticks < target)
Matthias Kaehlcke37911ae2010-03-09 22:13:56 +0100114 read_timer();
Matthias Kaehlcke195dbd12010-02-01 21:29:39 +0100115}
116
117int timer_init(void)
118{
Matthias Kaehlcke9d466562010-03-09 22:13:47 +0100119 struct timer_regs *timer_regs = (struct timer_regs *)TIMER_BASE;
Matthias Kaehlcke195dbd12010-02-01 21:29:39 +0100120
Matthias Kaehlcke37911ae2010-03-09 22:13:56 +0100121 /* use timer 3 with 508KHz and free running, not enabled now */
Matthias Kaehlcke9d466562010-03-09 22:13:47 +0100122 writel(TIMER_CLKSEL, &timer_regs->timer3.control);
Matthias Kaehlcke195dbd12010-02-01 21:29:39 +0100123
Matthias Kaehlcke37911ae2010-03-09 22:13:56 +0100124 /* set initial timer value */
Matthias Kaehlcke9d466562010-03-09 22:13:47 +0100125 writel(TIMER_MAX_VAL, &timer_regs->timer3.load);
Matthias Kaehlcke195dbd12010-02-01 21:29:39 +0100126
Matthias Kaehlcke8df23b72010-02-24 00:22:09 +0100127 /* Enable the timer */
128 writel(TIMER_ENABLE | TIMER_CLKSEL,
Matthias Kaehlcke9d466562010-03-09 22:13:47 +0100129 &timer_regs->timer3.control);
Matthias Kaehlcke195dbd12010-02-01 21:29:39 +0100130
131 reset_timer_masked();
132
133 return 0;
134}
135
136/*
137 * This function is derived from PowerPC code (timebase clock frequency).
138 * On ARM it returns the number of timer ticks per second.
139 */
140unsigned long get_tbclk(void)
141{
142 return CONFIG_SYS_HZ;
143}