blob: e5f791145bd00dc4cb9424101564ef60c0c2f6bb [file] [log] [blame]
Linus Walleij361c81f2015-06-15 13:15:26 +02001#include <linux/kernel.h>
2#include <linux/init.h>
Linus Walleij000bc172015-06-15 14:34:03 +02003#include <linux/clocksource.h>
4#include <linux/clockchips.h>
5#include <linux/sched_clock.h>
Linus Walleij361c81f2015-06-15 13:15:26 +02006#include <linux/interrupt.h>
7#include <linux/irq.h>
8#include <linux/io.h>
9#include <asm/mach/time.h>
10#include "soc.h"
11
12/*************************************************************************
13 * Timer handling for EP93xx
14 *************************************************************************
15 * The ep93xx has four internal timers. Timers 1, 2 (both 16 bit) and
16 * 3 (32 bit) count down at 508 kHz, are self-reloading, and can generate
17 * an interrupt on underflow. Timer 4 (40 bit) counts down at 983.04 kHz,
18 * is free-running, and can't generate interrupts.
19 *
20 * The 508 kHz timers are ideal for use for the timer interrupt, as the
Linus Walleij8ed39122015-06-16 09:00:44 +020021 * most common values of HZ divide 508 kHz nicely. We pick the 32 bit
22 * timer (timer 3) to get as long sleep intervals as possible when using
23 * CONFIG_NO_HZ.
Linus Walleij361c81f2015-06-15 13:15:26 +020024 *
25 * The higher clock rate of timer 4 makes it a better choice than the
Linus Walleij8ed39122015-06-16 09:00:44 +020026 * other timers for use as clock source and for sched_clock(), providing
27 * a stable 40 bit time base.
28 *************************************************************************
Linus Walleij361c81f2015-06-15 13:15:26 +020029 */
30#define EP93XX_TIMER_REG(x) (EP93XX_TIMER_BASE + (x))
31#define EP93XX_TIMER1_LOAD EP93XX_TIMER_REG(0x00)
32#define EP93XX_TIMER1_VALUE EP93XX_TIMER_REG(0x04)
33#define EP93XX_TIMER1_CONTROL EP93XX_TIMER_REG(0x08)
34#define EP93XX_TIMER123_CONTROL_ENABLE (1 << 7)
35#define EP93XX_TIMER123_CONTROL_MODE (1 << 6)
36#define EP93XX_TIMER123_CONTROL_CLKSEL (1 << 3)
37#define EP93XX_TIMER1_CLEAR EP93XX_TIMER_REG(0x0c)
38#define EP93XX_TIMER2_LOAD EP93XX_TIMER_REG(0x20)
39#define EP93XX_TIMER2_VALUE EP93XX_TIMER_REG(0x24)
40#define EP93XX_TIMER2_CONTROL EP93XX_TIMER_REG(0x28)
41#define EP93XX_TIMER2_CLEAR EP93XX_TIMER_REG(0x2c)
42#define EP93XX_TIMER4_VALUE_LOW EP93XX_TIMER_REG(0x60)
43#define EP93XX_TIMER4_VALUE_HIGH EP93XX_TIMER_REG(0x64)
44#define EP93XX_TIMER4_VALUE_HIGH_ENABLE (1 << 8)
45#define EP93XX_TIMER3_LOAD EP93XX_TIMER_REG(0x80)
46#define EP93XX_TIMER3_VALUE EP93XX_TIMER_REG(0x84)
47#define EP93XX_TIMER3_CONTROL EP93XX_TIMER_REG(0x88)
48#define EP93XX_TIMER3_CLEAR EP93XX_TIMER_REG(0x8c)
49
Linus Walleij000bc172015-06-15 14:34:03 +020050#define EP93XX_TIMER123_RATE 508469
51#define EP93XX_TIMER4_RATE 983040
Linus Walleij361c81f2015-06-15 13:15:26 +020052
Linus Walleij000bc172015-06-15 14:34:03 +020053static u64 notrace ep93xx_read_sched_clock(void)
54{
55 u64 ret;
Linus Walleij361c81f2015-06-15 13:15:26 +020056
Linus Walleijd118d972015-06-15 14:38:16 +020057 ret = readl(EP93XX_TIMER4_VALUE_LOW);
58 ret |= ((u64) (readl(EP93XX_TIMER4_VALUE_HIGH) & 0xff) << 32);
Linus Walleij000bc172015-06-15 14:34:03 +020059 return ret;
60}
61
62cycle_t ep93xx_clocksource_read(struct clocksource *c)
63{
64 u64 ret;
65
Linus Walleijd118d972015-06-15 14:38:16 +020066 ret = readl(EP93XX_TIMER4_VALUE_LOW);
67 ret |= ((u64) (readl(EP93XX_TIMER4_VALUE_HIGH) & 0xff) << 32);
Linus Walleij000bc172015-06-15 14:34:03 +020068 return (cycle_t) ret;
69}
70
71static int ep93xx_clkevt_set_next_event(unsigned long next,
72 struct clock_event_device *evt)
73{
74 /* Default mode: periodic, off, 508 kHz */
75 u32 tmode = EP93XX_TIMER123_CONTROL_MODE |
76 EP93XX_TIMER123_CONTROL_CLKSEL;
77
78 /* Clear timer */
Linus Walleijd5878e62015-06-15 14:42:25 +020079 writel(tmode, EP93XX_TIMER3_CONTROL);
Linus Walleij000bc172015-06-15 14:34:03 +020080
81 /* Set next event */
Linus Walleijd5878e62015-06-15 14:42:25 +020082 writel(next, EP93XX_TIMER3_LOAD);
Linus Walleijd118d972015-06-15 14:38:16 +020083 writel(tmode | EP93XX_TIMER123_CONTROL_ENABLE,
Linus Walleijd5878e62015-06-15 14:42:25 +020084 EP93XX_TIMER3_CONTROL);
Linus Walleij000bc172015-06-15 14:34:03 +020085 return 0;
86}
87
88
Viresh Kumara54868b2015-08-06 14:41:13 +053089static int ep93xx_clkevt_shutdown(struct clock_event_device *evt)
Linus Walleij000bc172015-06-15 14:34:03 +020090{
91 /* Disable timer */
Linus Walleijd5878e62015-06-15 14:42:25 +020092 writel(0, EP93XX_TIMER3_CONTROL);
Viresh Kumara54868b2015-08-06 14:41:13 +053093
94 return 0;
Linus Walleij000bc172015-06-15 14:34:03 +020095}
96
97static struct clock_event_device ep93xx_clockevent = {
Viresh Kumara54868b2015-08-06 14:41:13 +053098 .name = "timer1",
99 .features = CLOCK_EVT_FEAT_ONESHOT,
100 .set_state_shutdown = ep93xx_clkevt_shutdown,
101 .set_state_oneshot = ep93xx_clkevt_shutdown,
102 .tick_resume = ep93xx_clkevt_shutdown,
103 .set_next_event = ep93xx_clkevt_set_next_event,
104 .rating = 300,
Linus Walleij000bc172015-06-15 14:34:03 +0200105};
Linus Walleij361c81f2015-06-15 13:15:26 +0200106
107static irqreturn_t ep93xx_timer_interrupt(int irq, void *dev_id)
108{
Linus Walleij000bc172015-06-15 14:34:03 +0200109 struct clock_event_device *evt = dev_id;
110
Linus Walleij361c81f2015-06-15 13:15:26 +0200111 /* Writing any value clears the timer interrupt */
Linus Walleijd5878e62015-06-15 14:42:25 +0200112 writel(1, EP93XX_TIMER3_CLEAR);
Linus Walleij361c81f2015-06-15 13:15:26 +0200113
Linus Walleij000bc172015-06-15 14:34:03 +0200114 evt->event_handler(evt);
Linus Walleij361c81f2015-06-15 13:15:26 +0200115
116 return IRQ_HANDLED;
117}
118
119static struct irqaction ep93xx_timer_irq = {
120 .name = "ep93xx timer",
121 .flags = IRQF_TIMER | IRQF_IRQPOLL,
122 .handler = ep93xx_timer_interrupt,
Linus Walleij000bc172015-06-15 14:34:03 +0200123 .dev_id = &ep93xx_clockevent,
Linus Walleij361c81f2015-06-15 13:15:26 +0200124};
125
Linus Walleij361c81f2015-06-15 13:15:26 +0200126void __init ep93xx_timer_init(void)
127{
Linus Walleij000bc172015-06-15 14:34:03 +0200128 /* Enable and register clocksource and sched_clock on timer 4 */
Linus Walleijd118d972015-06-15 14:38:16 +0200129 writel(EP93XX_TIMER4_VALUE_HIGH_ENABLE,
130 EP93XX_TIMER4_VALUE_HIGH);
Linus Walleij000bc172015-06-15 14:34:03 +0200131 clocksource_mmio_init(NULL, "timer4",
132 EP93XX_TIMER4_RATE, 200, 40,
133 ep93xx_clocksource_read);
134 sched_clock_register(ep93xx_read_sched_clock, 40,
135 EP93XX_TIMER4_RATE);
Linus Walleij361c81f2015-06-15 13:15:26 +0200136
Linus Walleijd5878e62015-06-15 14:42:25 +0200137 /* Set up clockevent on timer 3 */
138 setup_irq(IRQ_EP93XX_TIMER3, &ep93xx_timer_irq);
Linus Walleij000bc172015-06-15 14:34:03 +0200139 clockevents_config_and_register(&ep93xx_clockevent,
140 EP93XX_TIMER123_RATE,
141 1,
Linus Walleijd5878e62015-06-15 14:42:25 +0200142 0xffffffffU);
Linus Walleij361c81f2015-06-15 13:15:26 +0200143}