2
* System timer for CSR SiRFprimaII
4
* Copyright (c) 2011 Cambridge Silicon Radio Limited, a CSR plc group company.
6
* Licensed under GPLv2 or later.
9
#include <linux/kernel.h>
10
#include <linux/interrupt.h>
11
#include <linux/clockchips.h>
12
#include <linux/clocksource.h>
13
#include <linux/bitops.h>
14
#include <linux/irq.h>
15
#include <linux/clk.h>
16
#include <linux/err.h>
17
#include <linux/slab.h>
19
#include <linux/of_address.h>
21
#include <asm/mach/time.h>
23
#define SIRFSOC_TIMER_COUNTER_LO 0x0000
24
#define SIRFSOC_TIMER_COUNTER_HI 0x0004
25
#define SIRFSOC_TIMER_MATCH_0 0x0008
26
#define SIRFSOC_TIMER_MATCH_1 0x000C
27
#define SIRFSOC_TIMER_MATCH_2 0x0010
28
#define SIRFSOC_TIMER_MATCH_3 0x0014
29
#define SIRFSOC_TIMER_MATCH_4 0x0018
30
#define SIRFSOC_TIMER_MATCH_5 0x001C
31
#define SIRFSOC_TIMER_STATUS 0x0020
32
#define SIRFSOC_TIMER_INT_EN 0x0024
33
#define SIRFSOC_TIMER_WATCHDOG_EN 0x0028
34
#define SIRFSOC_TIMER_DIV 0x002C
35
#define SIRFSOC_TIMER_LATCH 0x0030
36
#define SIRFSOC_TIMER_LATCHED_LO 0x0034
37
#define SIRFSOC_TIMER_LATCHED_HI 0x0038
39
#define SIRFSOC_TIMER_WDT_INDEX 5
41
#define SIRFSOC_TIMER_LATCH_BIT BIT(0)
43
#define SIRFSOC_TIMER_REG_CNT 11
45
static const u32 sirfsoc_timer_reg_list[SIRFSOC_TIMER_REG_CNT] = {
46
SIRFSOC_TIMER_MATCH_0, SIRFSOC_TIMER_MATCH_1, SIRFSOC_TIMER_MATCH_2,
47
SIRFSOC_TIMER_MATCH_3, SIRFSOC_TIMER_MATCH_4, SIRFSOC_TIMER_MATCH_5,
48
SIRFSOC_TIMER_INT_EN, SIRFSOC_TIMER_WATCHDOG_EN, SIRFSOC_TIMER_DIV,
49
SIRFSOC_TIMER_LATCHED_LO, SIRFSOC_TIMER_LATCHED_HI,
52
static u32 sirfsoc_timer_reg_val[SIRFSOC_TIMER_REG_CNT];
54
static void __iomem *sirfsoc_timer_base;
55
static void __init sirfsoc_of_timer_map(void);
57
/* timer0 interrupt handler */
58
static irqreturn_t sirfsoc_timer_interrupt(int irq, void *dev_id)
60
struct clock_event_device *ce = dev_id;
62
WARN_ON(!(readl_relaxed(sirfsoc_timer_base + SIRFSOC_TIMER_STATUS) & BIT(0)));
64
/* clear timer0 interrupt */
65
writel_relaxed(BIT(0), sirfsoc_timer_base + SIRFSOC_TIMER_STATUS);
67
ce->event_handler(ce);
72
/* read 64-bit timer counter */
73
static cycle_t sirfsoc_timer_read(struct clocksource *cs)
77
/* latch the 64-bit timer counter */
78
writel_relaxed(SIRFSOC_TIMER_LATCH_BIT, sirfsoc_timer_base + SIRFSOC_TIMER_LATCH);
79
cycles = readl_relaxed(sirfsoc_timer_base + SIRFSOC_TIMER_LATCHED_HI);
80
cycles = (cycles << 32) | readl_relaxed(sirfsoc_timer_base + SIRFSOC_TIMER_LATCHED_LO);
85
static int sirfsoc_timer_set_next_event(unsigned long delta,
86
struct clock_event_device *ce)
88
unsigned long now, next;
90
writel_relaxed(SIRFSOC_TIMER_LATCH_BIT, sirfsoc_timer_base + SIRFSOC_TIMER_LATCH);
91
now = readl_relaxed(sirfsoc_timer_base + SIRFSOC_TIMER_LATCHED_LO);
93
writel_relaxed(next, sirfsoc_timer_base + SIRFSOC_TIMER_MATCH_0);
94
writel_relaxed(SIRFSOC_TIMER_LATCH_BIT, sirfsoc_timer_base + SIRFSOC_TIMER_LATCH);
95
now = readl_relaxed(sirfsoc_timer_base + SIRFSOC_TIMER_LATCHED_LO);
97
return next - now > delta ? -ETIME : 0;
100
static void sirfsoc_timer_set_mode(enum clock_event_mode mode,
101
struct clock_event_device *ce)
103
u32 val = readl_relaxed(sirfsoc_timer_base + SIRFSOC_TIMER_INT_EN);
105
case CLOCK_EVT_MODE_PERIODIC:
108
case CLOCK_EVT_MODE_ONESHOT:
109
writel_relaxed(val | BIT(0), sirfsoc_timer_base + SIRFSOC_TIMER_INT_EN);
111
case CLOCK_EVT_MODE_SHUTDOWN:
112
writel_relaxed(val & ~BIT(0), sirfsoc_timer_base + SIRFSOC_TIMER_INT_EN);
114
case CLOCK_EVT_MODE_UNUSED:
115
case CLOCK_EVT_MODE_RESUME:
120
static void sirfsoc_clocksource_suspend(struct clocksource *cs)
124
writel_relaxed(SIRFSOC_TIMER_LATCH_BIT, sirfsoc_timer_base + SIRFSOC_TIMER_LATCH);
126
for (i = 0; i < SIRFSOC_TIMER_REG_CNT; i++)
127
sirfsoc_timer_reg_val[i] = readl_relaxed(sirfsoc_timer_base + sirfsoc_timer_reg_list[i]);
130
static void sirfsoc_clocksource_resume(struct clocksource *cs)
134
for (i = 0; i < SIRFSOC_TIMER_REG_CNT; i++)
135
writel_relaxed(sirfsoc_timer_reg_val[i], sirfsoc_timer_base + sirfsoc_timer_reg_list[i]);
137
writel_relaxed(sirfsoc_timer_reg_val[i - 2], sirfsoc_timer_base + SIRFSOC_TIMER_COUNTER_LO);
138
writel_relaxed(sirfsoc_timer_reg_val[i - 1], sirfsoc_timer_base + SIRFSOC_TIMER_COUNTER_HI);
141
static struct clock_event_device sirfsoc_clockevent = {
142
.name = "sirfsoc_clockevent",
144
.features = CLOCK_EVT_FEAT_ONESHOT,
145
.set_mode = sirfsoc_timer_set_mode,
146
.set_next_event = sirfsoc_timer_set_next_event,
149
static struct clocksource sirfsoc_clocksource = {
150
.name = "sirfsoc_clocksource",
152
.mask = CLOCKSOURCE_MASK(64),
153
.flags = CLOCK_SOURCE_IS_CONTINUOUS,
154
.read = sirfsoc_timer_read,
155
.suspend = sirfsoc_clocksource_suspend,
156
.resume = sirfsoc_clocksource_resume,
159
static struct irqaction sirfsoc_timer_irq = {
160
.name = "sirfsoc_timer0",
163
.handler = sirfsoc_timer_interrupt,
164
.dev_id = &sirfsoc_clockevent,
167
/* Overwrite weak default sched_clock with more precise one */
168
unsigned long long notrace sched_clock(void)
170
static int is_mapped;
173
* sched_clock is called earlier than .init of sys_timer
174
* if we map timer memory in .init of sys_timer, system
175
* will panic due to illegal memory access
178
sirfsoc_of_timer_map();
182
return sirfsoc_timer_read(NULL) * (NSEC_PER_SEC / CLOCK_TICK_RATE);
185
static void __init sirfsoc_clockevent_init(void)
187
clockevents_calc_mult_shift(&sirfsoc_clockevent, CLOCK_TICK_RATE, 60);
189
sirfsoc_clockevent.max_delta_ns =
190
clockevent_delta2ns(-2, &sirfsoc_clockevent);
191
sirfsoc_clockevent.min_delta_ns =
192
clockevent_delta2ns(2, &sirfsoc_clockevent);
194
sirfsoc_clockevent.cpumask = cpumask_of(0);
195
clockevents_register_device(&sirfsoc_clockevent);
198
/* initialize the kernel jiffy timer source */
199
static void __init sirfsoc_timer_init(void)
203
/* timer's input clock is io clock */
204
struct clk *clk = clk_get_sys("io", NULL);
208
rate = clk_get_rate(clk);
210
BUG_ON(rate < CLOCK_TICK_RATE);
211
BUG_ON(rate % CLOCK_TICK_RATE);
213
writel_relaxed(rate / CLOCK_TICK_RATE / 2 - 1, sirfsoc_timer_base + SIRFSOC_TIMER_DIV);
214
writel_relaxed(0, sirfsoc_timer_base + SIRFSOC_TIMER_COUNTER_LO);
215
writel_relaxed(0, sirfsoc_timer_base + SIRFSOC_TIMER_COUNTER_HI);
216
writel_relaxed(BIT(0), sirfsoc_timer_base + SIRFSOC_TIMER_STATUS);
218
BUG_ON(clocksource_register_hz(&sirfsoc_clocksource, CLOCK_TICK_RATE));
220
BUG_ON(setup_irq(sirfsoc_timer_irq.irq, &sirfsoc_timer_irq));
222
sirfsoc_clockevent_init();
225
static struct of_device_id timer_ids[] = {
226
{ .compatible = "sirf,prima2-tick" },
230
static void __init sirfsoc_of_timer_map(void)
232
struct device_node *np;
233
const unsigned int *intspec;
235
np = of_find_matching_node(NULL, timer_ids);
237
panic("unable to find compatible timer node in dtb\n");
238
sirfsoc_timer_base = of_iomap(np, 0);
239
if (!sirfsoc_timer_base)
240
panic("unable to map timer cpu registers\n");
242
/* Get the interrupts property */
243
intspec = of_get_property(np, "interrupts", NULL);
245
sirfsoc_timer_irq.irq = be32_to_cpup(intspec);
250
struct sys_timer sirfsoc_timer = {
251
.init = sirfsoc_timer_init,