blob: c2f239aab2c3a4d70ba8320f4ca46d704b124fac [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 *
Wolfgang Denk1a459662013-07-08 09:37:19 +020012 * SPDX-License-Identifier: GPL-2.0+
Matthias Kaehlckefcfb6322010-02-01 21:29:39 +010013 */
14
15#include <common.h>
16#include <linux/types.h>
17#include <asm/arch/ep93xx.h>
18#include <asm/io.h>
Matthias Kaehlcked9f505e2010-02-24 00:22:00 +010019#include <div64.h>
Matthias Kaehlckefcfb6322010-02-01 21:29:39 +010020
21#define TIMER_CLKSEL (1 << 3)
Matthias Kaehlckefcfb6322010-02-01 21:29:39 +010022#define TIMER_ENABLE (1 << 7)
23
Matthias Kaehlcke7e67fb52010-03-09 22:13:20 +010024#define TIMER_FREQ 508469 /* ticks / second */
Matthias Kaehlckec7ad13a2010-02-24 00:22:09 +010025#define TIMER_MAX_VAL 0xFFFFFFFF
Matthias Kaehlckefcfb6322010-02-01 21:29:39 +010026
Matthias Kaehlckec7ad13a2010-02-24 00:22:09 +010027static struct ep93xx_timer
28{
29 unsigned long long ticks;
Matthias Kaehlckeb0326982010-03-09 22:13:56 +010030 unsigned long last_read;
Matthias Kaehlckec7ad13a2010-02-24 00:22:09 +010031} timer;
Matthias Kaehlckefcfb6322010-02-01 21:29:39 +010032
Matthias Kaehlcke7e67fb52010-03-09 22:13:20 +010033static inline unsigned long long usecs_to_ticks(unsigned long usecs)
Matthias Kaehlckefcfb6322010-02-01 21:29:39 +010034{
Matthias Kaehlcke7e67fb52010-03-09 22:13:20 +010035 unsigned long long ticks = (unsigned long long)usecs * TIMER_FREQ;
36 do_div(ticks, 1000 * 1000);
Matthias Kaehlckefcfb6322010-02-01 21:29:39 +010037
38 return ticks;
39}
40
Matthias Kaehlckeb0326982010-03-09 22:13:56 +010041static inline void read_timer(void)
Matthias Kaehlckefcfb6322010-02-01 21:29:39 +010042{
Matthias Kaehlcke33eef042010-03-09 22:13:47 +010043 struct timer_regs *timer_regs = (struct timer_regs *)TIMER_BASE;
Matthias Kaehlckeb0326982010-03-09 22:13:56 +010044 const unsigned long now = TIMER_MAX_VAL - readl(&timer_regs->timer3.value);
Matthias Kaehlckefcfb6322010-02-01 21:29:39 +010045
Matthias Kaehlckeb0326982010-03-09 22:13:56 +010046 if (now >= timer.last_read)
47 timer.ticks += now - timer.last_read;
48 else
49 /* an overflow occurred */
50 timer.ticks += TIMER_MAX_VAL - timer.last_read + now;
51
52 timer.last_read = now;
Matthias Kaehlckefcfb6322010-02-01 21:29:39 +010053}
54
55/*
Matthias Kaehlcked650da22010-03-09 22:13:33 +010056 * Get the number of ticks (in CONFIG_SYS_HZ resolution)
Matthias Kaehlckefcfb6322010-02-01 21:29:39 +010057 */
58unsigned long long get_ticks(void)
59{
Matthias Kaehlckeb0326982010-03-09 22:13:56 +010060 unsigned long long sys_ticks;
Matthias Kaehlckefcfb6322010-02-01 21:29:39 +010061
Matthias Kaehlckeb0326982010-03-09 22:13:56 +010062 read_timer();
Matthias Kaehlckefcfb6322010-02-01 21:29:39 +010063
Matthias Kaehlckeb0326982010-03-09 22:13:56 +010064 sys_ticks = timer.ticks * CONFIG_SYS_HZ;
65 do_div(sys_ticks, TIMER_FREQ);
Matthias Kaehlckefcfb6322010-02-01 21:29:39 +010066
Matthias Kaehlckeb0326982010-03-09 22:13:56 +010067 return sys_ticks;
Matthias Kaehlckefcfb6322010-02-01 21:29:39 +010068}
69
70unsigned long get_timer_masked(void)
71{
Matthias Kaehlcked650da22010-03-09 22:13:33 +010072 return get_ticks();
Matthias Kaehlckefcfb6322010-02-01 21:29:39 +010073}
74
75unsigned long get_timer(unsigned long base)
76{
77 return get_timer_masked() - base;
78}
79
Matthias Kaehlckefcfb6322010-02-01 21:29:39 +010080void __udelay(unsigned long usec)
81{
Matthias Kaehlckeb0326982010-03-09 22:13:56 +010082 unsigned long long target;
Matthias Kaehlckefcfb6322010-02-01 21:29:39 +010083
Matthias Kaehlckeb0326982010-03-09 22:13:56 +010084 read_timer();
85
86 target = timer.ticks + usecs_to_ticks(usec);
Matthias Kaehlcked650da22010-03-09 22:13:33 +010087
88 while (timer.ticks < target)
Matthias Kaehlckeb0326982010-03-09 22:13:56 +010089 read_timer();
Matthias Kaehlckefcfb6322010-02-01 21:29:39 +010090}
91
92int timer_init(void)
93{
Matthias Kaehlcke33eef042010-03-09 22:13:47 +010094 struct timer_regs *timer_regs = (struct timer_regs *)TIMER_BASE;
Matthias Kaehlckefcfb6322010-02-01 21:29:39 +010095
Matthias Kaehlckeb0326982010-03-09 22:13:56 +010096 /* use timer 3 with 508KHz and free running, not enabled now */
Matthias Kaehlcke33eef042010-03-09 22:13:47 +010097 writel(TIMER_CLKSEL, &timer_regs->timer3.control);
Matthias Kaehlckefcfb6322010-02-01 21:29:39 +010098
Matthias Kaehlckeb0326982010-03-09 22:13:56 +010099 /* set initial timer value */
Matthias Kaehlcke33eef042010-03-09 22:13:47 +0100100 writel(TIMER_MAX_VAL, &timer_regs->timer3.load);
Matthias Kaehlckefcfb6322010-02-01 21:29:39 +0100101
Matthias Kaehlckec7ad13a2010-02-24 00:22:09 +0100102 /* Enable the timer */
103 writel(TIMER_ENABLE | TIMER_CLKSEL,
Matthias Kaehlcke33eef042010-03-09 22:13:47 +0100104 &timer_regs->timer3.control);
Matthias Kaehlckefcfb6322010-02-01 21:29:39 +0100105
Graeme Russ17659d72011-07-15 02:21:14 +0000106 /* Reset the timer */
107 read_timer();
108 timer.ticks = 0;
Matthias Kaehlckefcfb6322010-02-01 21:29:39 +0100109
110 return 0;
111}
112
113/*
114 * This function is derived from PowerPC code (timebase clock frequency).
115 * On ARM it returns the number of timer ticks per second.
116 */
117unsigned long get_tbclk(void)
118{
119 return CONFIG_SYS_HZ;
120}