1 /* linux/arch/arm/mach-msm/timer.c
3 * Copyright (C) 2007 Google, Inc.
5 * This software is licensed under the terms of the GNU General Public
6 * License version 2, as published by the Free Software Foundation, and
7 * may be copied, distributed, and modified under those terms.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
16 #include <linux/init.h>
17 #include <linux/time.h>
18 #include <linux/interrupt.h>
19 #include <linux/irq.h>
20 #include <linux/clk.h>
21 #include <linux/clockchips.h>
22 #include <linux/delay.h>
25 #include <asm/mach/time.h>
26 #include <mach/msm_iomap.h>
29 #define TIMER_MATCH_VAL 0x0000
30 #define TIMER_COUNT_VAL 0x0004
31 #define TIMER_ENABLE 0x0008
32 #define TIMER_ENABLE_CLR_ON_MATCH_EN 2
33 #define TIMER_ENABLE_EN 1
34 #define TIMER_CLEAR 0x000C
35 #define DGT_CLK_CTL 0x0034
37 DGT_CLK_CTL_DIV_1
= 0,
38 DGT_CLK_CTL_DIV_2
= 1,
39 DGT_CLK_CTL_DIV_3
= 2,
40 DGT_CLK_CTL_DIV_4
= 3,
42 #define CSR_PROTECTION 0x0020
43 #define CSR_PROTECTION_EN 1
52 #define MSM_GLOBAL_TIMER MSM_CLOCK_DGT
54 /* TODO: Remove these ifdefs */
55 #if defined(CONFIG_ARCH_QSD8X50)
56 #define DGT_HZ (19200000 / 4) /* 19.2 MHz / 4 by default */
57 #define MSM_DGT_SHIFT (0)
58 #elif defined(CONFIG_ARCH_MSM7X30) || defined(CONFIG_ARCH_MSM8X60) || \
59 defined(CONFIG_ARCH_MSM8960)
60 #define DGT_HZ (24576000 / 4) /* 24.576 MHz (LPXO) / 4 by default */
61 #define MSM_DGT_SHIFT (0)
63 #define DGT_HZ 19200000 /* 19.2 MHz or 600 KHz after shift */
64 #define MSM_DGT_SHIFT (5)
68 struct clock_event_device clockevent
;
69 struct clocksource clocksource
;
71 void __iomem
*regbase
;
74 void __iomem
*global_counter
;
75 void __iomem
*local_counter
;
85 static struct msm_clock msm_clocks
[];
86 static struct clock_event_device
*local_clock_event
;
88 static irqreturn_t
msm_timer_interrupt(int irq
, void *dev_id
)
90 struct clock_event_device
*evt
= dev_id
;
91 if (smp_processor_id() != 0)
92 evt
= local_clock_event
;
93 if (evt
->event_handler
== NULL
)
95 evt
->event_handler(evt
);
99 static cycle_t
msm_read_timer_count(struct clocksource
*cs
)
101 struct msm_clock
*clk
= container_of(cs
, struct msm_clock
, clocksource
);
103 return readl(clk
->global_counter
);
106 static struct msm_clock
*clockevent_to_clock(struct clock_event_device
*evt
)
110 for (i
= 0; i
< NR_TIMERS
; i
++)
111 if (evt
== &(msm_clocks
[i
].clockevent
))
112 return &msm_clocks
[i
];
113 return &msm_clocks
[MSM_GLOBAL_TIMER
];
115 return container_of(evt
, struct msm_clock
, clockevent
);
119 static int msm_timer_set_next_event(unsigned long cycles
,
120 struct clock_event_device
*evt
)
122 struct msm_clock
*clock
= clockevent_to_clock(evt
);
123 uint32_t now
= readl(clock
->local_counter
);
124 uint32_t alarm
= now
+ (cycles
<< clock
->shift
);
126 writel(alarm
, clock
->regbase
+ TIMER_MATCH_VAL
);
130 static void msm_timer_set_mode(enum clock_event_mode mode
,
131 struct clock_event_device
*evt
)
133 struct msm_clock
*clock
= clockevent_to_clock(evt
);
136 case CLOCK_EVT_MODE_RESUME
:
137 case CLOCK_EVT_MODE_PERIODIC
:
139 case CLOCK_EVT_MODE_ONESHOT
:
140 writel(TIMER_ENABLE_EN
, clock
->regbase
+ TIMER_ENABLE
);
142 case CLOCK_EVT_MODE_UNUSED
:
143 case CLOCK_EVT_MODE_SHUTDOWN
:
144 writel(0, clock
->regbase
+ TIMER_ENABLE
);
149 static struct msm_clock msm_clocks
[] = {
153 .features
= CLOCK_EVT_FEAT_ONESHOT
,
156 .set_next_event
= msm_timer_set_next_event
,
157 .set_mode
= msm_timer_set_mode
,
162 .read
= msm_read_timer_count
,
163 .mask
= CLOCKSOURCE_MASK(32),
164 .flags
= CLOCK_SOURCE_IS_CONTINUOUS
,
168 .flags
= IRQF_DISABLED
| IRQF_TIMER
| IRQF_TRIGGER_RISING
,
169 .handler
= msm_timer_interrupt
,
170 .dev_id
= &msm_clocks
[0].clockevent
,
171 .irq
= INT_GP_TIMER_EXP
178 .features
= CLOCK_EVT_FEAT_ONESHOT
,
179 .shift
= 32 + MSM_DGT_SHIFT
,
181 .set_next_event
= msm_timer_set_next_event
,
182 .set_mode
= msm_timer_set_mode
,
187 .read
= msm_read_timer_count
,
188 .mask
= CLOCKSOURCE_MASK((32 - MSM_DGT_SHIFT
)),
189 .flags
= CLOCK_SOURCE_IS_CONTINUOUS
,
193 .flags
= IRQF_DISABLED
| IRQF_TIMER
| IRQF_TRIGGER_RISING
,
194 .handler
= msm_timer_interrupt
,
195 .dev_id
= &msm_clocks
[1].clockevent
,
196 .irq
= INT_DEBUG_TIMER_EXP
198 .freq
= DGT_HZ
>> MSM_DGT_SHIFT
,
199 .shift
= MSM_DGT_SHIFT
,
203 static void __init
msm_timer_init(void)
207 int global_offset
= 0;
209 if (cpu_is_msm7x01()) {
210 msm_clocks
[MSM_CLOCK_GPT
].regbase
= MSM_CSR_BASE
;
211 msm_clocks
[MSM_CLOCK_DGT
].regbase
= MSM_CSR_BASE
+ 0x10;
212 } else if (cpu_is_msm7x30()) {
213 msm_clocks
[MSM_CLOCK_GPT
].regbase
= MSM_CSR_BASE
+ 0x04;
214 msm_clocks
[MSM_CLOCK_DGT
].regbase
= MSM_CSR_BASE
+ 0x24;
215 } else if (cpu_is_qsd8x50()) {
216 msm_clocks
[MSM_CLOCK_GPT
].regbase
= MSM_CSR_BASE
;
217 msm_clocks
[MSM_CLOCK_DGT
].regbase
= MSM_CSR_BASE
+ 0x10;
218 } else if (cpu_is_msm8x60() || cpu_is_msm8960()) {
219 msm_clocks
[MSM_CLOCK_GPT
].regbase
= MSM_TMR_BASE
+ 0x04;
220 msm_clocks
[MSM_CLOCK_DGT
].regbase
= MSM_TMR_BASE
+ 0x24;
222 /* Use CPU0's timer as the global timer. */
223 global_offset
= MSM_TMR0_BASE
- MSM_TMR_BASE
;
227 #ifdef CONFIG_ARCH_MSM_SCORPIONMP
228 writel(DGT_CLK_CTL_DIV_4
, MSM_TMR_BASE
+ DGT_CLK_CTL
);
231 for (i
= 0; i
< ARRAY_SIZE(msm_clocks
); i
++) {
232 struct msm_clock
*clock
= &msm_clocks
[i
];
233 struct clock_event_device
*ce
= &clock
->clockevent
;
234 struct clocksource
*cs
= &clock
->clocksource
;
236 clock
->local_counter
= clock
->regbase
+ TIMER_COUNT_VAL
;
237 clock
->global_counter
= clock
->local_counter
+ global_offset
;
239 writel(0, clock
->regbase
+ TIMER_ENABLE
);
240 writel(0, clock
->regbase
+ TIMER_CLEAR
);
241 writel(~0, clock
->regbase
+ TIMER_MATCH_VAL
);
243 ce
->mult
= div_sc(clock
->freq
, NSEC_PER_SEC
, ce
->shift
);
244 /* allow at least 10 seconds to notice that the timer wrapped */
246 clockevent_delta2ns(0xf0000000 >> clock
->shift
, ce
);
247 /* 4 gets rounded down to 3 */
248 ce
->min_delta_ns
= clockevent_delta2ns(4, ce
);
249 ce
->cpumask
= cpumask_of(0);
251 res
= clocksource_register_hz(cs
, clock
->freq
);
253 printk(KERN_ERR
"msm_timer_init: clocksource_register "
254 "failed for %s\n", cs
->name
);
256 res
= setup_irq(clock
->irq
.irq
, &clock
->irq
);
258 printk(KERN_ERR
"msm_timer_init: setup_irq "
259 "failed for %s\n", cs
->name
);
261 clockevents_register_device(ce
);
266 int __cpuinit
local_timer_setup(struct clock_event_device
*evt
)
268 struct msm_clock
*clock
= &msm_clocks
[MSM_GLOBAL_TIMER
];
270 /* Use existing clock_event for cpu 0 */
271 if (!smp_processor_id())
274 writel(DGT_CLK_CTL_DIV_4
, MSM_TMR_BASE
+ DGT_CLK_CTL
);
276 if (!local_clock_event
) {
277 writel(0, clock
->regbase
+ TIMER_ENABLE
);
278 writel(0, clock
->regbase
+ TIMER_CLEAR
);
279 writel(~0, clock
->regbase
+ TIMER_MATCH_VAL
);
281 evt
->irq
= clock
->irq
.irq
;
282 evt
->name
= "local_timer";
283 evt
->features
= CLOCK_EVT_FEAT_ONESHOT
;
284 evt
->rating
= clock
->clockevent
.rating
;
285 evt
->set_mode
= msm_timer_set_mode
;
286 evt
->set_next_event
= msm_timer_set_next_event
;
287 evt
->shift
= clock
->clockevent
.shift
;
288 evt
->mult
= div_sc(clock
->freq
, NSEC_PER_SEC
, evt
->shift
);
290 clockevent_delta2ns(0xf0000000 >> clock
->shift
, evt
);
291 evt
->min_delta_ns
= clockevent_delta2ns(4, evt
);
293 local_clock_event
= evt
;
295 gic_enable_ppi(clock
->irq
.irq
);
297 clockevents_register_device(evt
);
301 inline int local_timer_ack(void)
308 struct sys_timer msm_timer
= {
309 .init
= msm_timer_init