blob: ffb93db68e9c80b3003e6d93c7e93d25eb76182e [file] [log] [blame]
Lennert Buytenhek2bac1de2008-03-27 14:51:40 -04001/*
2 * arch/arm/plat-orion/time.c
3 *
4 * Marvell Orion SoC timer handling.
5 *
6 * This file is licensed under the terms of the GNU General Public
7 * License version 2. This program is licensed "as is" without any
8 * warranty of any kind, whether express or implied.
9 *
10 * Timer 0 is used as free-running clocksource, while timer 1 is
11 * used as clock_event_device.
12 */
13
14#include <linux/kernel.h>
Nicolas Pitrea399e3f2009-05-15 00:42:36 -040015#include <linux/timer.h>
Lennert Buytenhek2bac1de2008-03-27 14:51:40 -040016#include <linux/clockchips.h>
17#include <linux/interrupt.h>
18#include <linux/irq.h>
Stephen Boyd38ff87f2013-06-01 23:39:40 -070019#include <linux/sched_clock.h>
Andrew Lunn48fce882013-10-23 16:12:50 +020020#include <plat/time.h>
Russell Kingf19768c2015-10-19 16:00:35 +010021#include <asm/delay.h>
Lennert Buytenhek2bac1de2008-03-27 14:51:40 -040022
23/*
Lennert Buytenhek4ee1f6b2010-10-15 16:50:26 +020024 * MBus bridge block registers.
Lennert Buytenhek2bac1de2008-03-27 14:51:40 -040025 */
Lennert Buytenhek4ee1f6b2010-10-15 16:50:26 +020026#define BRIDGE_CAUSE_OFF 0x0110
27#define BRIDGE_MASK_OFF 0x0114
28#define BRIDGE_INT_TIMER0 0x0002
29#define BRIDGE_INT_TIMER1 0x0004
Lennert Buytenhek2bac1de2008-03-27 14:51:40 -040030
31
32/*
33 * Timer block registers.
34 */
Lennert Buytenhek4ee1f6b2010-10-15 16:50:26 +020035#define TIMER_CTRL_OFF 0x0000
36#define TIMER0_EN 0x0001
37#define TIMER0_RELOAD_EN 0x0002
38#define TIMER1_EN 0x0004
39#define TIMER1_RELOAD_EN 0x0008
40#define TIMER0_RELOAD_OFF 0x0010
41#define TIMER0_VAL_OFF 0x0014
42#define TIMER1_RELOAD_OFF 0x0018
43#define TIMER1_VAL_OFF 0x001c
44
45
46/*
47 * SoC-specific data.
48 */
49static void __iomem *bridge_base;
50static u32 bridge_timer1_clr_mask;
51static void __iomem *timer_base;
52
53
54/*
55 * Number of timer ticks per jiffy.
56 */
57static u32 ticks_per_jiffy;
Lennert Buytenhek2bac1de2008-03-27 14:51:40 -040058
59
60/*
Stefan Agner8a3269f2009-05-12 10:30:41 -070061 * Orion's sched_clock implementation. It has a resolution of
Russell Kingf06a1622010-12-15 21:55:06 +000062 * at least 7.5ns (133MHz TCLK).
Stefan Agner8a3269f2009-05-12 10:30:41 -070063 */
Stefan Agner8a3269f2009-05-12 10:30:41 -070064
Stephen Boydb44653b2013-11-15 15:26:24 -080065static u64 notrace orion_read_sched_clock(void)
Stefan Agner8a3269f2009-05-12 10:30:41 -070066{
Marc Zyngier2f0778af2011-12-15 12:19:23 +010067 return ~readl(timer_base + TIMER0_VAL_OFF);
Stefan Agner8a3269f2009-05-12 10:30:41 -070068}
69
70/*
Lennert Buytenhek2bac1de2008-03-27 14:51:40 -040071 * Clockevent handling.
72 */
73static int
74orion_clkevt_next_event(unsigned long delta, struct clock_event_device *dev)
75{
76 unsigned long flags;
77 u32 u;
78
79 if (delta == 0)
80 return -ETIME;
81
82 local_irq_save(flags);
83
84 /*
85 * Clear and enable clockevent timer interrupt.
86 */
Lennert Buytenhek4ee1f6b2010-10-15 16:50:26 +020087 writel(bridge_timer1_clr_mask, bridge_base + BRIDGE_CAUSE_OFF);
Lennert Buytenhek2bac1de2008-03-27 14:51:40 -040088
Lennert Buytenhek4ee1f6b2010-10-15 16:50:26 +020089 u = readl(bridge_base + BRIDGE_MASK_OFF);
Lennert Buytenhek2bac1de2008-03-27 14:51:40 -040090 u |= BRIDGE_INT_TIMER1;
Lennert Buytenhek4ee1f6b2010-10-15 16:50:26 +020091 writel(u, bridge_base + BRIDGE_MASK_OFF);
Lennert Buytenhek2bac1de2008-03-27 14:51:40 -040092
93 /*
94 * Setup new clockevent timer value.
95 */
Lennert Buytenhek4ee1f6b2010-10-15 16:50:26 +020096 writel(delta, timer_base + TIMER1_VAL_OFF);
Lennert Buytenhek2bac1de2008-03-27 14:51:40 -040097
98 /*
99 * Enable the timer.
100 */
Lennert Buytenhek4ee1f6b2010-10-15 16:50:26 +0200101 u = readl(timer_base + TIMER_CTRL_OFF);
Lennert Buytenhek2bac1de2008-03-27 14:51:40 -0400102 u = (u & ~TIMER1_RELOAD_EN) | TIMER1_EN;
Lennert Buytenhek4ee1f6b2010-10-15 16:50:26 +0200103 writel(u, timer_base + TIMER_CTRL_OFF);
Lennert Buytenhek2bac1de2008-03-27 14:51:40 -0400104
105 local_irq_restore(flags);
106
107 return 0;
108}
109
Viresh Kumar10dca882015-02-27 13:39:52 +0530110static int orion_clkevt_shutdown(struct clock_event_device *evt)
Lennert Buytenhek2bac1de2008-03-27 14:51:40 -0400111{
112 unsigned long flags;
113 u32 u;
114
115 local_irq_save(flags);
Lennert Buytenhek2bac1de2008-03-27 14:51:40 -0400116
Viresh Kumar10dca882015-02-27 13:39:52 +0530117 /* Disable timer */
118 u = readl(timer_base + TIMER_CTRL_OFF);
119 writel(u & ~TIMER1_EN, timer_base + TIMER_CTRL_OFF);
Lennert Buytenhek2bac1de2008-03-27 14:51:40 -0400120
Viresh Kumar10dca882015-02-27 13:39:52 +0530121 /* Disable timer interrupt */
122 u = readl(bridge_base + BRIDGE_MASK_OFF);
123 writel(u & ~BRIDGE_INT_TIMER1, bridge_base + BRIDGE_MASK_OFF);
Lennert Buytenhek2bac1de2008-03-27 14:51:40 -0400124
Viresh Kumar10dca882015-02-27 13:39:52 +0530125 /* ACK pending timer interrupt */
126 writel(bridge_timer1_clr_mask, bridge_base + BRIDGE_CAUSE_OFF);
Lennert Buytenhek2bac1de2008-03-27 14:51:40 -0400127
Lennert Buytenhek2bac1de2008-03-27 14:51:40 -0400128 local_irq_restore(flags);
Viresh Kumar10dca882015-02-27 13:39:52 +0530129
130 return 0;
131}
132
133static int orion_clkevt_set_periodic(struct clock_event_device *evt)
134{
135 unsigned long flags;
136 u32 u;
137
138 local_irq_save(flags);
139
140 /* Setup timer to fire at 1/HZ intervals */
141 writel(ticks_per_jiffy - 1, timer_base + TIMER1_RELOAD_OFF);
142 writel(ticks_per_jiffy - 1, timer_base + TIMER1_VAL_OFF);
143
144 /* Enable timer interrupt */
145 u = readl(bridge_base + BRIDGE_MASK_OFF);
146 writel(u | BRIDGE_INT_TIMER1, bridge_base + BRIDGE_MASK_OFF);
147
148 /* Enable timer */
149 u = readl(timer_base + TIMER_CTRL_OFF);
150 writel(u | TIMER1_EN | TIMER1_RELOAD_EN, timer_base + TIMER_CTRL_OFF);
151
152 local_irq_restore(flags);
153
154 return 0;
Lennert Buytenhek2bac1de2008-03-27 14:51:40 -0400155}
156
157static struct clock_event_device orion_clkevt = {
Viresh Kumar10dca882015-02-27 13:39:52 +0530158 .name = "orion_tick",
159 .features = CLOCK_EVT_FEAT_ONESHOT |
160 CLOCK_EVT_FEAT_PERIODIC,
161 .rating = 300,
162 .set_next_event = orion_clkevt_next_event,
163 .set_state_shutdown = orion_clkevt_shutdown,
164 .set_state_periodic = orion_clkevt_set_periodic,
165 .set_state_oneshot = orion_clkevt_shutdown,
166 .tick_resume = orion_clkevt_shutdown,
Lennert Buytenhek2bac1de2008-03-27 14:51:40 -0400167};
168
169static irqreturn_t orion_timer_interrupt(int irq, void *dev_id)
170{
171 /*
172 * ACK timer interrupt and call event handler.
173 */
Lennert Buytenhek4ee1f6b2010-10-15 16:50:26 +0200174 writel(bridge_timer1_clr_mask, bridge_base + BRIDGE_CAUSE_OFF);
Lennert Buytenhek2bac1de2008-03-27 14:51:40 -0400175 orion_clkevt.event_handler(&orion_clkevt);
176
177 return IRQ_HANDLED;
178}
179
180static struct irqaction orion_timer_irq = {
181 .name = "orion_tick",
Michael Opdenackereb4d5522013-10-12 05:49:20 +0200182 .flags = IRQF_TIMER,
Lennert Buytenhek2bac1de2008-03-27 14:51:40 -0400183 .handler = orion_timer_interrupt
184};
185
Lennert Buytenhek4ee1f6b2010-10-15 16:50:26 +0200186void __init
Thomas Petazzonie96a0302012-09-11 14:27:25 +0200187orion_time_set_base(void __iomem *_timer_base)
Lennert Buytenhek4ee1f6b2010-10-15 16:50:26 +0200188{
Thomas Petazzonie96a0302012-09-11 14:27:25 +0200189 timer_base = _timer_base;
Lennert Buytenhek4ee1f6b2010-10-15 16:50:26 +0200190}
191
Russell Kingf19768c2015-10-19 16:00:35 +0100192static unsigned long orion_delay_timer_read(void)
193{
194 return ~readl(timer_base + TIMER0_VAL_OFF);
195}
196
197static struct delay_timer orion_delay_timer = {
198 .read_current_timer = orion_delay_timer_read,
199};
200
Lennert Buytenhek4ee1f6b2010-10-15 16:50:26 +0200201void __init
Thomas Petazzonie96a0302012-09-11 14:27:25 +0200202orion_time_init(void __iomem *_bridge_base, u32 _bridge_timer1_clr_mask,
Lennert Buytenhek4ee1f6b2010-10-15 16:50:26 +0200203 unsigned int irq, unsigned int tclk)
Lennert Buytenhek2bac1de2008-03-27 14:51:40 -0400204{
205 u32 u;
206
Lennert Buytenhek4ee1f6b2010-10-15 16:50:26 +0200207 /*
208 * Set SoC-specific data.
209 */
Thomas Petazzonie96a0302012-09-11 14:27:25 +0200210 bridge_base = _bridge_base;
Lennert Buytenhek4ee1f6b2010-10-15 16:50:26 +0200211 bridge_timer1_clr_mask = _bridge_timer1_clr_mask;
212
Lennert Buytenhek2bac1de2008-03-27 14:51:40 -0400213 ticks_per_jiffy = (tclk + HZ/2) / HZ;
214
Russell Kingf19768c2015-10-19 16:00:35 +0100215 orion_delay_timer.freq = tclk;
216 register_current_timer_delay(&orion_delay_timer);
217
Stefan Agner8a3269f2009-05-12 10:30:41 -0700218 /*
Lennert Buytenhek4ee1f6b2010-10-15 16:50:26 +0200219 * Set scale and timer for sched_clock.
Stefan Agner8a3269f2009-05-12 10:30:41 -0700220 */
Stephen Boydb44653b2013-11-15 15:26:24 -0800221 sched_clock_register(orion_read_sched_clock, 32, tclk);
Lennert Buytenhek2bac1de2008-03-27 14:51:40 -0400222
223 /*
224 * Setup free-running clocksource timer (interrupts
Lennert Buytenhek4ee1f6b2010-10-15 16:50:26 +0200225 * disabled).
Lennert Buytenhek2bac1de2008-03-27 14:51:40 -0400226 */
Lennert Buytenhek4ee1f6b2010-10-15 16:50:26 +0200227 writel(0xffffffff, timer_base + TIMER0_VAL_OFF);
228 writel(0xffffffff, timer_base + TIMER0_RELOAD_OFF);
229 u = readl(bridge_base + BRIDGE_MASK_OFF);
230 writel(u & ~BRIDGE_INT_TIMER0, bridge_base + BRIDGE_MASK_OFF);
231 u = readl(timer_base + TIMER_CTRL_OFF);
232 writel(u | TIMER0_EN | TIMER0_RELOAD_EN, timer_base + TIMER_CTRL_OFF);
Russell Kingbfe45e02011-05-08 15:33:30 +0100233 clocksource_mmio_init(timer_base + TIMER0_VAL_OFF, "orion_clocksource",
234 tclk, 300, 32, clocksource_mmio_readl_down);
Lennert Buytenhek2bac1de2008-03-27 14:51:40 -0400235
Lennert Buytenhek2bac1de2008-03-27 14:51:40 -0400236 /*
Lennert Buytenhek4ee1f6b2010-10-15 16:50:26 +0200237 * Setup clockevent timer (interrupt-driven).
Lennert Buytenhek2bac1de2008-03-27 14:51:40 -0400238 */
239 setup_irq(irq, &orion_timer_irq);
Rusty Russell320ab2b2008-12-13 21:20:26 +1030240 orion_clkevt.cpumask = cpumask_of(0);
Shawn Guo838a2ae2013-01-12 11:50:05 +0000241 clockevents_config_and_register(&orion_clkevt, tclk, 1, 0xfffffffe);
Lennert Buytenhek2bac1de2008-03-27 14:51:40 -0400242}