blob: 92447cd7a41a3f1a81db44c67cb89a55d22dc2b8 [file] [log] [blame]
Tony Lindgren1dbae812005-11-10 14:26:51 +00001/*
Tony Lindgren0f622e82011-03-29 15:54:50 -07002 * linux/arch/arm/mach-omap2/timer.c
Tony Lindgren1dbae812005-11-10 14:26:51 +00003 *
4 * OMAP2 GP timer support.
5 *
Paul Walmsleyf2480762009-04-23 21:11:10 -06006 * Copyright (C) 2009 Nokia Corporation
7 *
Kevin Hilman5a3a3882007-11-12 23:24:02 -08008 * Update to use new clocksource/clockevent layers
9 * Author: Kevin Hilman, MontaVista Software, Inc. <source@mvista.com>
10 * Copyright (C) 2007 MontaVista Software, Inc.
11 *
12 * Original driver:
Tony Lindgren1dbae812005-11-10 14:26:51 +000013 * Copyright (C) 2005 Nokia Corporation
14 * Author: Paul Mundt <paul.mundt@nokia.com>
Jan Engelhardt96de0e22007-10-19 23:21:04 +020015 * Juha Yrjölä <juha.yrjola@nokia.com>
Timo Teras77900a22006-06-26 16:16:12 -070016 * OMAP Dual-mode timer framework support by Timo Teras
Tony Lindgren1dbae812005-11-10 14:26:51 +000017 *
18 * Some parts based off of TI's 24xx code:
19 *
Santosh Shilimkar44169072009-05-28 14:16:04 -070020 * Copyright (C) 2004-2009 Texas Instruments, Inc.
Tony Lindgren1dbae812005-11-10 14:26:51 +000021 *
22 * Roughly modelled after the OMAP1 MPU timer code.
Santosh Shilimkar44169072009-05-28 14:16:04 -070023 * Added OMAP4 support - Santosh Shilimkar <santosh.shilimkar@ti.com>
Tony Lindgren1dbae812005-11-10 14:26:51 +000024 *
25 * This file is subject to the terms and conditions of the GNU General Public
26 * License. See the file "COPYING" in the main directory of this archive
27 * for more details.
28 */
29#include <linux/init.h>
30#include <linux/time.h>
31#include <linux/interrupt.h>
32#include <linux/err.h>
Russell Kingf8ce2542006-01-07 16:15:52 +000033#include <linux/clk.h>
Timo Teras77900a22006-06-26 16:16:12 -070034#include <linux/delay.h>
Dirk Behmee6687292006-12-06 17:14:00 -080035#include <linux/irq.h>
Kevin Hilman5a3a3882007-11-12 23:24:02 -080036#include <linux/clocksource.h>
37#include <linux/clockchips.h>
Tarun Kanti DebBarmac345c8b2011-09-20 17:00:18 +053038#include <linux/slab.h>
Santosh Shilimkareed0de22012-07-04 18:32:32 +053039#include <linux/of.h>
Jon Hunter9725f442012-05-14 10:41:37 -050040#include <linux/of_address.h>
41#include <linux/of_irq.h>
Russell Kingf8ce2542006-01-07 16:15:52 +000042
Tony Lindgren1dbae812005-11-10 14:26:51 +000043#include <asm/mach/time.h>
Marc Zyngiera45c9832012-01-10 19:44:19 +000044#include <asm/smp_twd.h>
Paul Walmsleycbc94382011-02-22 19:59:49 -070045#include <asm/sched_clock.h>
Tony Lindgren7d7e1eb2012-08-27 17:43:01 -070046
Santosh Shilimkar3c7c5da2012-08-13 14:39:03 +053047#include <asm/arch_timer.h>
Paul Walmsley38698be2011-02-23 00:14:08 -070048#include <plat/omap_hwmod.h>
Tarun Kanti DebBarmac345c8b2011-09-20 17:00:18 +053049#include <plat/omap_device.h>
Tony Lindgren7d7e1eb2012-08-27 17:43:01 -070050#include <plat/dmtimer.h>
Tarun Kanti DebBarmab4811132011-09-20 17:00:24 +053051#include <plat/omap-pm.h>
52
Tony Lindgrendbc04162012-08-31 10:59:07 -070053#include "soc.h"
Tony Lindgren7d7e1eb2012-08-27 17:43:01 -070054#include "common.h"
Tarun Kanti DebBarmab4811132011-09-20 17:00:24 +053055#include "powerdomain.h"
Tony Lindgren1dbae812005-11-10 14:26:51 +000056
Tony Lindgrenaa561882011-03-29 15:54:48 -070057/* Parent clocks, eventually these will come from the clock framework */
58
59#define OMAP2_MPU_SOURCE "sys_ck"
60#define OMAP3_MPU_SOURCE OMAP2_MPU_SOURCE
61#define OMAP4_MPU_SOURCE "sys_clkin_ck"
62#define OMAP2_32K_SOURCE "func_32k_ck"
63#define OMAP3_32K_SOURCE "omap_32k_fck"
64#define OMAP4_32K_SOURCE "sys_32k_ck"
65
66#ifdef CONFIG_OMAP_32K_TIMER
67#define OMAP2_CLKEV_SOURCE OMAP2_32K_SOURCE
68#define OMAP3_CLKEV_SOURCE OMAP3_32K_SOURCE
69#define OMAP4_CLKEV_SOURCE OMAP4_32K_SOURCE
70#define OMAP3_SECURE_TIMER 12
Jon Hunter9725f442012-05-14 10:41:37 -050071#define TIMER_PROP_SECURE "ti,timer-secure"
Tony Lindgrenaa561882011-03-29 15:54:48 -070072#else
73#define OMAP2_CLKEV_SOURCE OMAP2_MPU_SOURCE
74#define OMAP3_CLKEV_SOURCE OMAP3_MPU_SOURCE
75#define OMAP4_CLKEV_SOURCE OMAP4_MPU_SOURCE
76#define OMAP3_SECURE_TIMER 1
Jon Hunter9725f442012-05-14 10:41:37 -050077#define TIMER_PROP_SECURE "ti,timer-alwon"
Tony Lindgrenaa561882011-03-29 15:54:48 -070078#endif
Paul Walmsleyd8328f32011-01-15 21:32:01 -070079
Santosh Shilimkarfa6d79d2012-08-13 14:24:24 +053080#define REALTIME_COUNTER_BASE 0x48243200
81#define INCREMENTER_NUMERATOR_OFFSET 0x10
82#define INCREMENTER_DENUMERATOR_RELOAD_OFFSET 0x14
83#define NUMERATOR_DENUMERATOR_MASK 0xfffff000
84
Tony Lindgrenaa561882011-03-29 15:54:48 -070085/* Clockevent code */
86
87static struct omap_dm_timer clkev;
Kevin Hilman5a3a3882007-11-12 23:24:02 -080088static struct clock_event_device clockevent_gpt;
Tony Lindgren1dbae812005-11-10 14:26:51 +000089
Linus Torvalds0cd61b62006-10-06 10:53:39 -070090static irqreturn_t omap2_gp_timer_interrupt(int irq, void *dev_id)
Tony Lindgren1dbae812005-11-10 14:26:51 +000091{
Kevin Hilman5a3a3882007-11-12 23:24:02 -080092 struct clock_event_device *evt = &clockevent_gpt;
Tony Lindgren1dbae812005-11-10 14:26:51 +000093
Tony Lindgrenee17f112011-09-16 15:44:20 -070094 __omap_dm_timer_write_status(&clkev, OMAP_TIMER_INT_OVERFLOW);
Kevin Hilman5a3a3882007-11-12 23:24:02 -080095
96 evt->event_handler(evt);
Tony Lindgren1dbae812005-11-10 14:26:51 +000097 return IRQ_HANDLED;
98}
99
100static struct irqaction omap2_gp_timer_irq = {
Vaibhav Hiremathf36921b2012-05-09 10:07:05 -0700101 .name = "gp_timer",
Bernhard Walleb30faba2007-05-08 00:35:39 -0700102 .flags = IRQF_DISABLED | IRQF_TIMER | IRQF_IRQPOLL,
Tony Lindgren1dbae812005-11-10 14:26:51 +0000103 .handler = omap2_gp_timer_interrupt,
104};
105
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800106static int omap2_gp_timer_set_next_event(unsigned long cycles,
107 struct clock_event_device *evt)
Tony Lindgren1dbae812005-11-10 14:26:51 +0000108{
Tony Lindgrenee17f112011-09-16 15:44:20 -0700109 __omap_dm_timer_load_start(&clkev, OMAP_TIMER_CTRL_ST,
Tony Lindgrenaa561882011-03-29 15:54:48 -0700110 0xffffffff - cycles, 1);
Tony Lindgren1dbae812005-11-10 14:26:51 +0000111
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800112 return 0;
113}
114
115static void omap2_gp_timer_set_mode(enum clock_event_mode mode,
116 struct clock_event_device *evt)
117{
118 u32 period;
119
Tony Lindgrenee17f112011-09-16 15:44:20 -0700120 __omap_dm_timer_stop(&clkev, 1, clkev.rate);
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800121
122 switch (mode) {
123 case CLOCK_EVT_MODE_PERIODIC:
Tony Lindgrenaa561882011-03-29 15:54:48 -0700124 period = clkev.rate / HZ;
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800125 period -= 1;
Tony Lindgrenaa561882011-03-29 15:54:48 -0700126 /* Looks like we need to first set the load value separately */
Tony Lindgrenee17f112011-09-16 15:44:20 -0700127 __omap_dm_timer_write(&clkev, OMAP_TIMER_LOAD_REG,
Tony Lindgrenaa561882011-03-29 15:54:48 -0700128 0xffffffff - period, 1);
Tony Lindgrenee17f112011-09-16 15:44:20 -0700129 __omap_dm_timer_load_start(&clkev,
Tony Lindgrenaa561882011-03-29 15:54:48 -0700130 OMAP_TIMER_CTRL_AR | OMAP_TIMER_CTRL_ST,
131 0xffffffff - period, 1);
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800132 break;
133 case CLOCK_EVT_MODE_ONESHOT:
134 break;
135 case CLOCK_EVT_MODE_UNUSED:
136 case CLOCK_EVT_MODE_SHUTDOWN:
137 case CLOCK_EVT_MODE_RESUME:
138 break;
139 }
140}
141
142static struct clock_event_device clockevent_gpt = {
Vaibhav Hiremathf36921b2012-05-09 10:07:05 -0700143 .name = "gp_timer",
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800144 .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
145 .shift = 32,
Santosh Shilimkar11d6ec22012-03-17 15:00:16 +0530146 .rating = 300,
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800147 .set_next_event = omap2_gp_timer_set_next_event,
148 .set_mode = omap2_gp_timer_set_mode,
149};
150
Jon Hunterad24bde2012-06-20 15:55:24 -0500151static struct property device_disabled = {
152 .name = "status",
153 .length = sizeof("disabled"),
154 .value = "disabled",
155};
156
157static struct of_device_id omap_timer_match[] __initdata = {
158 { .compatible = "ti,omap2-timer", },
159 { }
160};
161
162/**
Jon Hunter9725f442012-05-14 10:41:37 -0500163 * omap_get_timer_dt - get a timer using device-tree
164 * @match - device-tree match structure for matching a device type
165 * @property - optional timer property to match
166 *
167 * Helper function to get a timer during early boot using device-tree for use
168 * as kernel system timer. Optionally, the property argument can be used to
169 * select a timer with a specific property. Once a timer is found then mark
170 * the timer node in device-tree as disabled, to prevent the kernel from
171 * registering this timer as a platform device and so no one else can use it.
172 */
173static struct device_node * __init omap_get_timer_dt(struct of_device_id *match,
174 const char *property)
175{
176 struct device_node *np;
177
178 for_each_matching_node(np, match) {
179 if (!of_device_is_available(np)) {
180 of_node_put(np);
181 continue;
182 }
183
184 if (property && !of_get_property(np, property, NULL)) {
185 of_node_put(np);
186 continue;
187 }
188
189 prom_add_property(np, &device_disabled);
190 return np;
191 }
192
193 return NULL;
194}
195
196/**
Jon Hunterad24bde2012-06-20 15:55:24 -0500197 * omap_dmtimer_init - initialisation function when device tree is used
198 *
199 * For secure OMAP3 devices, timers with device type "timer-secure" cannot
200 * be used by the kernel as they are reserved. Therefore, to prevent the
201 * kernel registering these devices remove them dynamically from the device
202 * tree on boot.
203 */
204void __init omap_dmtimer_init(void)
205{
206 struct device_node *np;
207
208 if (!cpu_is_omap34xx())
209 return;
210
211 /* If we are a secure device, remove any secure timer nodes */
212 if ((omap_type() != OMAP2_DEVICE_TYPE_GP)) {
Jon Hunter9725f442012-05-14 10:41:37 -0500213 np = omap_get_timer_dt(omap_timer_match, "ti,timer-secure");
214 if (np)
215 of_node_put(np);
Jon Hunterad24bde2012-06-20 15:55:24 -0500216 }
217}
218
Tony Lindgrenaa561882011-03-29 15:54:48 -0700219static int __init omap_dm_timer_init_one(struct omap_dm_timer *timer,
220 int gptimer_id,
Jon Hunter9725f442012-05-14 10:41:37 -0500221 const char *fck_source,
222 const char *property)
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800223{
Tony Lindgrenaa561882011-03-29 15:54:48 -0700224 char name[10]; /* 10 = sizeof("gptXX_Xck0") */
Jon Hunter9725f442012-05-14 10:41:37 -0500225 const char *oh_name;
226 struct device_node *np;
Tony Lindgrenaa561882011-03-29 15:54:48 -0700227 struct omap_hwmod *oh;
Paul Walmsley6c0c27f2012-04-19 04:01:50 -0600228 struct resource irq_rsrc, mem_rsrc;
Tony Lindgrenaa561882011-03-29 15:54:48 -0700229 size_t size;
230 int res = 0;
Paul Walmsley6c0c27f2012-04-19 04:01:50 -0600231 int r;
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800232
Jon Hunter9725f442012-05-14 10:41:37 -0500233 if (of_have_populated_dt()) {
234 np = omap_get_timer_dt(omap_timer_match, NULL);
235 if (!np)
236 return -ENODEV;
237
238 of_property_read_string_index(np, "ti,hwmods", 0, &oh_name);
239 if (!oh_name)
240 return -ENODEV;
241
242 timer->irq = irq_of_parse_and_map(np, 0);
243 if (!timer->irq)
244 return -ENXIO;
245
246 timer->io_base = of_iomap(np, 0);
247
248 of_node_put(np);
249 } else {
250 if (omap_dm_timer_reserve_systimer(gptimer_id))
251 return -ENODEV;
252
253 sprintf(name, "timer%d", gptimer_id);
254 oh_name = name;
255 }
256
257 omap_hwmod_setup_one(oh_name);
258 oh = omap_hwmod_lookup(oh_name);
259
Tony Lindgrenaa561882011-03-29 15:54:48 -0700260 if (!oh)
261 return -ENODEV;
Paul Walmsleyf2480762009-04-23 21:11:10 -0600262
Jon Hunter9725f442012-05-14 10:41:37 -0500263 if (!of_have_populated_dt()) {
264 r = omap_hwmod_get_resource_byname(oh, IORESOURCE_IRQ, NULL,
265 &irq_rsrc);
266 if (r)
267 return -ENXIO;
268 timer->irq = irq_rsrc.start;
Paul Walmsley6c0c27f2012-04-19 04:01:50 -0600269
Jon Hunter9725f442012-05-14 10:41:37 -0500270 r = omap_hwmod_get_resource_byname(oh, IORESOURCE_MEM, NULL,
271 &mem_rsrc);
272 if (r)
273 return -ENXIO;
274 timer->phys_base = mem_rsrc.start;
275 size = mem_rsrc.end - mem_rsrc.start;
Tony Lindgrenaa561882011-03-29 15:54:48 -0700276
Jon Hunter9725f442012-05-14 10:41:37 -0500277 /* Static mapping, never released */
278 timer->io_base = ioremap(timer->phys_base, size);
279 }
280
Tony Lindgrenaa561882011-03-29 15:54:48 -0700281 if (!timer->io_base)
282 return -ENXIO;
283
284 /* After the dmtimer is using hwmod these clocks won't be needed */
Tarun Kanti DebBarmaae6df412012-07-05 18:10:59 +0530285 timer->fclk = clk_get(NULL, omap_hwmod_get_main_clk(oh));
Tony Lindgrenaa561882011-03-29 15:54:48 -0700286 if (IS_ERR(timer->fclk))
287 return -ENODEV;
288
Tony Lindgrenaa561882011-03-29 15:54:48 -0700289 omap_hwmod_enable(oh);
290
Jon Hunter9725f442012-05-14 10:41:37 -0500291 /* FIXME: Need to remove hard-coded test on timer ID */
Tony Lindgrenaa561882011-03-29 15:54:48 -0700292 if (gptimer_id != 12) {
293 struct clk *src;
294
295 src = clk_get(NULL, fck_source);
296 if (IS_ERR(src)) {
297 res = -EINVAL;
298 } else {
299 res = __omap_dm_timer_set_source(timer->fclk, src);
300 if (IS_ERR_VALUE(res))
Jon Hunter9725f442012-05-14 10:41:37 -0500301 pr_warn("%s: %s cannot set source\n",
302 __func__, oh->name);
Tony Lindgrenaa561882011-03-29 15:54:48 -0700303 clk_put(src);
304 }
305 }
Tony Lindgrenee17f112011-09-16 15:44:20 -0700306 __omap_dm_timer_init_regs(timer);
307 __omap_dm_timer_reset(timer, 1, 1);
Tony Lindgrenaa561882011-03-29 15:54:48 -0700308 timer->posted = 1;
309
310 timer->rate = clk_get_rate(timer->fclk);
311
312 timer->reserved = 1;
Paul Walmsley38698be2011-02-23 00:14:08 -0700313
Tony Lindgrenaa561882011-03-29 15:54:48 -0700314 return res;
315}
Paul Walmsleyf2480762009-04-23 21:11:10 -0600316
Tony Lindgrenaa561882011-03-29 15:54:48 -0700317static void __init omap2_gp_clockevent_init(int gptimer_id,
Jon Hunter9725f442012-05-14 10:41:37 -0500318 const char *fck_source,
319 const char *property)
Tony Lindgrenaa561882011-03-29 15:54:48 -0700320{
321 int res;
Paul Walmsleyf2480762009-04-23 21:11:10 -0600322
Jon Hunter9725f442012-05-14 10:41:37 -0500323 res = omap_dm_timer_init_one(&clkev, gptimer_id, fck_source, property);
Tony Lindgrenaa561882011-03-29 15:54:48 -0700324 BUG_ON(res);
Paul Walmsleyf2480762009-04-23 21:11:10 -0600325
Paul Walmsleya032d332012-08-03 09:21:10 -0600326 omap2_gp_timer_irq.dev_id = &clkev;
Tony Lindgrenaa561882011-03-29 15:54:48 -0700327 setup_irq(clkev.irq, &omap2_gp_timer_irq);
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800328
Tony Lindgrenee17f112011-09-16 15:44:20 -0700329 __omap_dm_timer_int_enable(&clkev, OMAP_TIMER_INT_OVERFLOW);
Tony Lindgrenaa561882011-03-29 15:54:48 -0700330
331 clockevent_gpt.mult = div_sc(clkev.rate, NSEC_PER_SEC,
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800332 clockevent_gpt.shift);
333 clockevent_gpt.max_delta_ns =
334 clockevent_delta2ns(0xffffffff, &clockevent_gpt);
335 clockevent_gpt.min_delta_ns =
Aaro Koskinendf88acb2009-01-29 08:57:17 -0800336 clockevent_delta2ns(3, &clockevent_gpt);
337 /* Timer internal resynch latency. */
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800338
Santosh Shilimkar11d6ec22012-03-17 15:00:16 +0530339 clockevent_gpt.cpumask = cpu_possible_mask;
340 clockevent_gpt.irq = omap_dm_timer_get_irq(&clkev);
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800341 clockevents_register_device(&clockevent_gpt);
Tony Lindgrenaa561882011-03-29 15:54:48 -0700342
343 pr_info("OMAP clockevent source: GPTIMER%d at %lu Hz\n",
344 gptimer_id, clkev.rate);
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800345}
346
Paul Walmsleyf2480762009-04-23 21:11:10 -0600347/* Clocksource code */
Tony Lindgren3d05a3e2011-03-29 15:54:49 -0700348static struct omap_dm_timer clksrc;
Vaibhav Hiremath1fe97c82012-05-09 10:07:05 -0700349static bool use_gptimer_clksrc;
Tony Lindgren3d05a3e2011-03-29 15:54:49 -0700350
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800351/*
352 * clocksource
353 */
Magnus Damm8e196082009-04-21 12:24:00 -0700354static cycle_t clocksource_read_cycles(struct clocksource *cs)
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800355{
Tony Lindgrenee17f112011-09-16 15:44:20 -0700356 return (cycle_t)__omap_dm_timer_read_counter(&clksrc, 1);
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800357}
358
359static struct clocksource clocksource_gpt = {
Vaibhav Hiremathf36921b2012-05-09 10:07:05 -0700360 .name = "gp_timer",
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800361 .rating = 300,
362 .read = clocksource_read_cycles,
363 .mask = CLOCKSOURCE_MASK(32),
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800364 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
365};
366
Marc Zyngier2f0778af2011-12-15 12:19:23 +0100367static u32 notrace dmtimer_read_sched_clock(void)
Paul Walmsleycbc94382011-02-22 19:59:49 -0700368{
Tony Lindgren3d05a3e2011-03-29 15:54:49 -0700369 if (clksrc.reserved)
Vaibhav Hiremathdbc39822012-01-23 12:18:14 +0530370 return __omap_dm_timer_read_counter(&clksrc, 1);
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800371
Marc Zyngier2f0778af2011-12-15 12:19:23 +0100372 return 0;
Tony Lindgren3d05a3e2011-03-29 15:54:49 -0700373}
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800374
Igor Grinberg45caae72012-08-28 01:26:14 +0300375#ifdef CONFIG_OMAP_32K_TIMER
Tony Lindgren3d05a3e2011-03-29 15:54:49 -0700376/* Setup free-running counter for clocksource */
Vaibhav Hiremath1fe97c82012-05-09 10:07:05 -0700377static int __init omap2_sync32k_clocksource_init(void)
378{
379 int ret;
380 struct omap_hwmod *oh;
381 void __iomem *vbase;
382 const char *oh_name = "counter_32k";
383
384 /*
385 * First check hwmod data is available for sync32k counter
386 */
387 oh = omap_hwmod_lookup(oh_name);
388 if (!oh || oh->slaves_cnt == 0)
389 return -ENODEV;
390
391 omap_hwmod_setup_one(oh_name);
392
393 vbase = omap_hwmod_get_mpu_rt_va(oh);
394 if (!vbase) {
395 pr_warn("%s: failed to get counter_32k resource\n", __func__);
396 return -ENXIO;
397 }
398
399 ret = omap_hwmod_enable(oh);
400 if (ret) {
401 pr_warn("%s: failed to enable counter_32k module (%d)\n",
402 __func__, ret);
403 return ret;
404 }
405
406 ret = omap_init_clocksource_32k(vbase);
407 if (ret) {
408 pr_warn("%s: failed to initialize counter_32k as a clocksource (%d)\n",
409 __func__, ret);
410 omap_hwmod_idle(oh);
411 }
412
413 return ret;
414}
Igor Grinberg45caae72012-08-28 01:26:14 +0300415#else
416static inline int omap2_sync32k_clocksource_init(void)
417{
418 return -ENODEV;
419}
420#endif
Vaibhav Hiremath1fe97c82012-05-09 10:07:05 -0700421
422static void __init omap2_gptimer_clocksource_init(int gptimer_id,
Tony Lindgren3d05a3e2011-03-29 15:54:49 -0700423 const char *fck_source)
424{
425 int res;
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800426
Jon Hunter9725f442012-05-14 10:41:37 -0500427 res = omap_dm_timer_init_one(&clksrc, gptimer_id, fck_source, NULL);
Tony Lindgren3d05a3e2011-03-29 15:54:49 -0700428 BUG_ON(res);
Paul Walmsleycbc94382011-02-22 19:59:49 -0700429
Tony Lindgrenee17f112011-09-16 15:44:20 -0700430 __omap_dm_timer_load_start(&clksrc,
Hemant Pedanekare9d0b972011-08-10 13:19:35 +0000431 OMAP_TIMER_CTRL_ST | OMAP_TIMER_CTRL_AR, 0, 1);
Marc Zyngier2f0778af2011-12-15 12:19:23 +0100432 setup_sched_clock(dmtimer_read_sched_clock, 32, clksrc.rate);
Tony Lindgren3d05a3e2011-03-29 15:54:49 -0700433
434 if (clocksource_register_hz(&clocksource_gpt, clksrc.rate))
435 pr_err("Could not register clocksource %s\n",
436 clocksource_gpt.name);
Vaibhav Hiremath1fe97c82012-05-09 10:07:05 -0700437 else
438 pr_info("OMAP clocksource: GPTIMER%d at %lu Hz\n",
439 gptimer_id, clksrc.rate);
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800440}
Vaibhav Hiremath1fe97c82012-05-09 10:07:05 -0700441
442static void __init omap2_clocksource_init(int gptimer_id,
443 const char *fck_source)
444{
445 /*
446 * First give preference to kernel parameter configuration
447 * by user (clocksource="gp_timer").
448 *
449 * In case of missing kernel parameter for clocksource,
450 * first check for availability for 32k-sync timer, in case
451 * of failure in finding 32k_counter module or registering
452 * it as clocksource, execution will fallback to gp-timer.
453 */
454 if (use_gptimer_clksrc == true)
455 omap2_gptimer_clocksource_init(gptimer_id, fck_source);
456 else if (omap2_sync32k_clocksource_init())
457 /* Fall back to gp-timer code */
458 omap2_gptimer_clocksource_init(gptimer_id, fck_source);
459}
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800460
Santosh Shilimkarfa6d79d2012-08-13 14:24:24 +0530461#ifdef CONFIG_SOC_HAS_REALTIME_COUNTER
462/*
463 * The realtime counter also called master counter, is a free-running
464 * counter, which is related to real time. It produces the count used
465 * by the CPU local timer peripherals in the MPU cluster. The timer counts
466 * at a rate of 6.144 MHz. Because the device operates on different clocks
467 * in different power modes, the master counter shifts operation between
468 * clocks, adjusting the increment per clock in hardware accordingly to
469 * maintain a constant count rate.
470 */
471static void __init realtime_counter_init(void)
472{
473 void __iomem *base;
474 static struct clk *sys_clk;
475 unsigned long rate;
476 unsigned int reg, num, den;
477
478 base = ioremap(REALTIME_COUNTER_BASE, SZ_32);
479 if (!base) {
480 pr_err("%s: ioremap failed\n", __func__);
481 return;
482 }
483 sys_clk = clk_get(NULL, "sys_clkin_ck");
Wei Yongjun533b2982012-10-08 15:01:41 -0700484 if (IS_ERR(sys_clk)) {
Santosh Shilimkarfa6d79d2012-08-13 14:24:24 +0530485 pr_err("%s: failed to get system clock handle\n", __func__);
486 iounmap(base);
487 return;
488 }
489
490 rate = clk_get_rate(sys_clk);
491 /* Numerator/denumerator values refer TRM Realtime Counter section */
492 switch (rate) {
493 case 1200000:
494 num = 64;
495 den = 125;
496 break;
497 case 1300000:
498 num = 768;
499 den = 1625;
500 break;
501 case 19200000:
502 num = 8;
503 den = 25;
504 break;
505 case 2600000:
506 num = 384;
507 den = 1625;
508 break;
509 case 2700000:
510 num = 256;
511 den = 1125;
512 break;
513 case 38400000:
514 default:
515 /* Program it for 38.4 MHz */
516 num = 4;
517 den = 25;
518 break;
519 }
520
521 /* Program numerator and denumerator registers */
522 reg = __raw_readl(base + INCREMENTER_NUMERATOR_OFFSET) &
523 NUMERATOR_DENUMERATOR_MASK;
524 reg |= num;
525 __raw_writel(reg, base + INCREMENTER_NUMERATOR_OFFSET);
526
527 reg = __raw_readl(base + INCREMENTER_NUMERATOR_OFFSET) &
528 NUMERATOR_DENUMERATOR_MASK;
529 reg |= den;
530 __raw_writel(reg, base + INCREMENTER_DENUMERATOR_RELOAD_OFFSET);
531
532 iounmap(base);
533}
534#else
535static inline void __init realtime_counter_init(void)
536{}
537#endif
538
Jon Hunter9725f442012-05-14 10:41:37 -0500539#define OMAP_SYS_TIMER_INIT(name, clkev_nr, clkev_src, clkev_prop, \
Tony Lindgren3d05a3e2011-03-29 15:54:49 -0700540 clksrc_nr, clksrc_src) \
Tony Lindgrene74984e2011-03-29 15:54:48 -0700541static void __init omap##name##_timer_init(void) \
542{ \
Jon Hunterad24bde2012-06-20 15:55:24 -0500543 omap_dmtimer_init(); \
Jon Hunter9725f442012-05-14 10:41:37 -0500544 omap2_gp_clockevent_init((clkev_nr), clkev_src, clkev_prop); \
Vaibhav Hiremath1fe97c82012-05-09 10:07:05 -0700545 omap2_clocksource_init((clksrc_nr), clksrc_src); \
Tony Lindgrene74984e2011-03-29 15:54:48 -0700546}
547
548#define OMAP_SYS_TIMER(name) \
549struct sys_timer omap##name##_timer = { \
550 .init = omap##name##_timer_init, \
551};
552
553#ifdef CONFIG_ARCH_OMAP2
Jon Hunter9725f442012-05-14 10:41:37 -0500554OMAP_SYS_TIMER_INIT(2, 1, OMAP2_CLKEV_SOURCE, "ti,timer-alwon",
555 2, OMAP2_MPU_SOURCE)
Tony Lindgrene74984e2011-03-29 15:54:48 -0700556OMAP_SYS_TIMER(2)
557#endif
558
559#ifdef CONFIG_ARCH_OMAP3
Jon Hunter9725f442012-05-14 10:41:37 -0500560OMAP_SYS_TIMER_INIT(3, 1, OMAP3_CLKEV_SOURCE, "ti,timer-alwon",
561 2, OMAP3_MPU_SOURCE)
Tony Lindgrene74984e2011-03-29 15:54:48 -0700562OMAP_SYS_TIMER(3)
Tony Lindgren3d05a3e2011-03-29 15:54:49 -0700563OMAP_SYS_TIMER_INIT(3_secure, OMAP3_SECURE_TIMER, OMAP3_CLKEV_SOURCE,
Jon Hunter9725f442012-05-14 10:41:37 -0500564 TIMER_PROP_SECURE, 2, OMAP3_MPU_SOURCE)
Tony Lindgrene74984e2011-03-29 15:54:48 -0700565OMAP_SYS_TIMER(3_secure)
566#endif
567
Afzal Mohammed08f30982012-05-11 00:38:49 +0530568#ifdef CONFIG_SOC_AM33XX
Jon Hunter9725f442012-05-14 10:41:37 -0500569OMAP_SYS_TIMER_INIT(3_am33xx, 1, OMAP4_MPU_SOURCE, "ti,timer-alwon",
570 2, OMAP4_MPU_SOURCE)
Afzal Mohammed08f30982012-05-11 00:38:49 +0530571OMAP_SYS_TIMER(3_am33xx)
572#endif
573
Tony Lindgrene74984e2011-03-29 15:54:48 -0700574#ifdef CONFIG_ARCH_OMAP4
Marc Zyngiera45c9832012-01-10 19:44:19 +0000575#ifdef CONFIG_LOCAL_TIMERS
576static DEFINE_TWD_LOCAL_TIMER(twd_local_timer,
Tony Lindgren3f216ef2012-10-16 11:19:16 -0700577 OMAP44XX_LOCAL_TWD_BASE, 29);
Marc Zyngiera45c9832012-01-10 19:44:19 +0000578#endif
579
Tony Lindgrene74984e2011-03-29 15:54:48 -0700580static void __init omap4_timer_init(void)
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800581{
Jon Hunter9725f442012-05-14 10:41:37 -0500582 omap2_gp_clockevent_init(1, OMAP4_CLKEV_SOURCE, "ti,timer-alwon");
Vaibhav Hiremath1fe97c82012-05-09 10:07:05 -0700583 omap2_clocksource_init(2, OMAP4_MPU_SOURCE);
Marc Zyngiera45c9832012-01-10 19:44:19 +0000584#ifdef CONFIG_LOCAL_TIMERS
585 /* Local timers are not supprted on OMAP4430 ES1.0 */
586 if (omap_rev() != OMAP4430_REV_ES1_0) {
587 int err;
588
Santosh Shilimkareed0de22012-07-04 18:32:32 +0530589 if (of_have_populated_dt()) {
590 twd_local_timer_of_register();
591 return;
592 }
593
Marc Zyngiera45c9832012-01-10 19:44:19 +0000594 err = twd_local_timer_register(&twd_local_timer);
595 if (err)
596 pr_err("twd_local_timer_register failed %d\n", err);
597 }
598#endif
Tony Lindgren1dbae812005-11-10 14:26:51 +0000599}
Tony Lindgrene74984e2011-03-29 15:54:48 -0700600OMAP_SYS_TIMER(4)
601#endif
Tarun Kanti DebBarmac345c8b2011-09-20 17:00:18 +0530602
R Sricharan37b32802012-05-02 13:07:12 +0530603#ifdef CONFIG_SOC_OMAP5
Santosh Shilimkarfa6d79d2012-08-13 14:24:24 +0530604static void __init omap5_timer_init(void)
605{
Santosh Shilimkar3c7c5da2012-08-13 14:39:03 +0530606 int err;
607
Jon Hunter9725f442012-05-14 10:41:37 -0500608 omap2_gp_clockevent_init(1, OMAP4_CLKEV_SOURCE, "ti,timer-alwon");
Santosh Shilimkarfa6d79d2012-08-13 14:24:24 +0530609 omap2_clocksource_init(2, OMAP4_MPU_SOURCE);
610 realtime_counter_init();
Santosh Shilimkar3c7c5da2012-08-13 14:39:03 +0530611
612 err = arch_timer_of_register();
613 if (err)
614 pr_err("%s: arch_timer_register failed %d\n", __func__, err);
Santosh Shilimkarfa6d79d2012-08-13 14:24:24 +0530615}
R Sricharan37b32802012-05-02 13:07:12 +0530616OMAP_SYS_TIMER(5)
617#endif
618
Tarun Kanti DebBarmac345c8b2011-09-20 17:00:18 +0530619/**
Tarun Kanti DebBarmac345c8b2011-09-20 17:00:18 +0530620 * omap_timer_init - build and register timer device with an
621 * associated timer hwmod
622 * @oh: timer hwmod pointer to be used to build timer device
623 * @user: parameter that can be passed from calling hwmod API
624 *
625 * Called by omap_hwmod_for_each_by_class to register each of the timer
626 * devices present in the system. The number of timer devices is known
627 * by parsing through the hwmod database for a given class name. At the
628 * end of function call memory is allocated for timer device and it is
629 * registered to the framework ready to be proved by the driver.
630 */
631static int __init omap_timer_init(struct omap_hwmod *oh, void *unused)
632{
633 int id;
634 int ret = 0;
635 char *name = "omap_timer";
636 struct dmtimer_platform_data *pdata;
Tony Lindgrenc541c152011-10-04 09:47:06 -0700637 struct platform_device *pdev;
Tarun Kanti DebBarmac345c8b2011-09-20 17:00:18 +0530638 struct omap_timer_capability_dev_attr *timer_dev_attr;
639
640 pr_debug("%s: %s\n", __func__, oh->name);
641
642 /* on secure device, do not register secure timer */
643 timer_dev_attr = oh->dev_attr;
644 if (omap_type() != OMAP2_DEVICE_TYPE_GP && timer_dev_attr)
645 if (timer_dev_attr->timer_capability == OMAP_TIMER_SECURE)
646 return ret;
647
648 pdata = kzalloc(sizeof(*pdata), GFP_KERNEL);
649 if (!pdata) {
650 pr_err("%s: No memory for [%s]\n", __func__, oh->name);
651 return -ENOMEM;
652 }
653
654 /*
655 * Extract the IDs from name field in hwmod database
656 * and use the same for constructing ids' for the
657 * timer devices. In a way, we are avoiding usage of
658 * static variable witin the function to do the same.
659 * CAUTION: We have to be careful and make sure the
660 * name in hwmod database does not change in which case
661 * we might either make corresponding change here or
662 * switch back static variable mechanism.
663 */
664 sscanf(oh->name, "timer%2d", &id);
665
Jon Hunterd1c16912012-06-05 12:34:52 -0500666 if (timer_dev_attr)
667 pdata->timer_capability = timer_dev_attr->timer_capability;
Tarun Kanti DebBarmac345c8b2011-09-20 17:00:18 +0530668
Tony Lindgrenc541c152011-10-04 09:47:06 -0700669 pdev = omap_device_build(name, id, oh, pdata, sizeof(*pdata),
Benoit Coussonc16ae1e2011-10-04 23:20:41 +0200670 NULL, 0, 0);
Tarun Kanti DebBarmac345c8b2011-09-20 17:00:18 +0530671
Tony Lindgrenc541c152011-10-04 09:47:06 -0700672 if (IS_ERR(pdev)) {
Tarun Kanti DebBarmac345c8b2011-09-20 17:00:18 +0530673 pr_err("%s: Can't build omap_device for %s: %s.\n",
674 __func__, name, oh->name);
675 ret = -EINVAL;
676 }
677
678 kfree(pdata);
679
680 return ret;
681}
Tarun Kanti DebBarma3392cdd2011-09-20 17:00:20 +0530682
683/**
684 * omap2_dm_timer_init - top level regular device initialization
685 *
686 * Uses dedicated hwmod api to parse through hwmod database for
687 * given class name and then build and register the timer device.
688 */
689static int __init omap2_dm_timer_init(void)
690{
691 int ret;
692
Jon Hunter9725f442012-05-14 10:41:37 -0500693 /* If dtb is there, the devices will be created dynamically */
694 if (of_have_populated_dt())
695 return -ENODEV;
696
Tarun Kanti DebBarma3392cdd2011-09-20 17:00:20 +0530697 ret = omap_hwmod_for_each_by_class("timer", omap_timer_init, NULL);
698 if (unlikely(ret)) {
699 pr_err("%s: device registration failed.\n", __func__);
700 return -EINVAL;
701 }
702
703 return 0;
704}
705arch_initcall(omap2_dm_timer_init);
Vaibhav Hiremath1fe97c82012-05-09 10:07:05 -0700706
707/**
708 * omap2_override_clocksource - clocksource override with user configuration
709 *
710 * Allows user to override default clocksource, using kernel parameter
711 * clocksource="gp_timer" (For all OMAP2PLUS architectures)
712 *
713 * Note that, here we are using same standard kernel parameter "clocksource=",
714 * and not introducing any OMAP specific interface.
715 */
716static int __init omap2_override_clocksource(char *str)
717{
718 if (!str)
719 return 0;
720 /*
721 * For OMAP architecture, we only have two options
722 * - sync_32k (default)
723 * - gp_timer (sys_clk based)
724 */
725 if (!strcmp(str, "gp_timer"))
726 use_gptimer_clksrc = true;
727
728 return 0;
729}
730early_param("clocksource", omap2_override_clocksource);