blob: 099e4060afe988a3ebe28b4cd0235614dced9c54 [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>
Tony Lindgren2a296c82012-10-02 17:41:35 -070048#include "omap_hwmod.h"
Tony Lindgren25c7d492012-10-02 17:25:48 -070049#include "omap_device.h"
Tony Lindgren5c2e8852012-10-29 16:45:47 -070050#include <plat/counter-32k.h>
Tony Lindgren7d7e1eb2012-08-27 17:43:01 -070051#include <plat/dmtimer.h>
Tony Lindgren1d5aef42012-10-03 16:36:40 -070052#include "omap-pm.h"
Tarun Kanti DebBarmab4811132011-09-20 17:00:24 +053053
Tony Lindgrendbc04162012-08-31 10:59:07 -070054#include "soc.h"
Tony Lindgren7d7e1eb2012-08-27 17:43:01 -070055#include "common.h"
Tarun Kanti DebBarmab4811132011-09-20 17:00:24 +053056#include "powerdomain.h"
Tony Lindgren1dbae812005-11-10 14:26:51 +000057
Tony Lindgrenaa561882011-03-29 15:54:48 -070058/* Parent clocks, eventually these will come from the clock framework */
59
60#define OMAP2_MPU_SOURCE "sys_ck"
61#define OMAP3_MPU_SOURCE OMAP2_MPU_SOURCE
62#define OMAP4_MPU_SOURCE "sys_clkin_ck"
63#define OMAP2_32K_SOURCE "func_32k_ck"
64#define OMAP3_32K_SOURCE "omap_32k_fck"
65#define OMAP4_32K_SOURCE "sys_32k_ck"
66
67#ifdef CONFIG_OMAP_32K_TIMER
68#define OMAP2_CLKEV_SOURCE OMAP2_32K_SOURCE
69#define OMAP3_CLKEV_SOURCE OMAP3_32K_SOURCE
70#define OMAP4_CLKEV_SOURCE OMAP4_32K_SOURCE
71#define OMAP3_SECURE_TIMER 12
Jon Hunter9725f442012-05-14 10:41:37 -050072#define TIMER_PROP_SECURE "ti,timer-secure"
Tony Lindgrenaa561882011-03-29 15:54:48 -070073#else
74#define OMAP2_CLKEV_SOURCE OMAP2_MPU_SOURCE
75#define OMAP3_CLKEV_SOURCE OMAP3_MPU_SOURCE
76#define OMAP4_CLKEV_SOURCE OMAP4_MPU_SOURCE
77#define OMAP3_SECURE_TIMER 1
Jon Hunter9725f442012-05-14 10:41:37 -050078#define TIMER_PROP_SECURE "ti,timer-alwon"
Tony Lindgrenaa561882011-03-29 15:54:48 -070079#endif
Paul Walmsleyd8328f32011-01-15 21:32:01 -070080
Santosh Shilimkarfa6d79d2012-08-13 14:24:24 +053081#define REALTIME_COUNTER_BASE 0x48243200
82#define INCREMENTER_NUMERATOR_OFFSET 0x10
83#define INCREMENTER_DENUMERATOR_RELOAD_OFFSET 0x14
84#define NUMERATOR_DENUMERATOR_MASK 0xfffff000
85
Tony Lindgrenaa561882011-03-29 15:54:48 -070086/* Clockevent code */
87
88static struct omap_dm_timer clkev;
Kevin Hilman5a3a3882007-11-12 23:24:02 -080089static struct clock_event_device clockevent_gpt;
Tony Lindgren1dbae812005-11-10 14:26:51 +000090
Linus Torvalds0cd61b62006-10-06 10:53:39 -070091static irqreturn_t omap2_gp_timer_interrupt(int irq, void *dev_id)
Tony Lindgren1dbae812005-11-10 14:26:51 +000092{
Kevin Hilman5a3a3882007-11-12 23:24:02 -080093 struct clock_event_device *evt = &clockevent_gpt;
Tony Lindgren1dbae812005-11-10 14:26:51 +000094
Tony Lindgrenee17f112011-09-16 15:44:20 -070095 __omap_dm_timer_write_status(&clkev, OMAP_TIMER_INT_OVERFLOW);
Kevin Hilman5a3a3882007-11-12 23:24:02 -080096
97 evt->event_handler(evt);
Tony Lindgren1dbae812005-11-10 14:26:51 +000098 return IRQ_HANDLED;
99}
100
101static struct irqaction omap2_gp_timer_irq = {
Vaibhav Hiremathf36921b2012-05-09 10:07:05 -0700102 .name = "gp_timer",
Bernhard Walleb30faba2007-05-08 00:35:39 -0700103 .flags = IRQF_DISABLED | IRQF_TIMER | IRQF_IRQPOLL,
Tony Lindgren1dbae812005-11-10 14:26:51 +0000104 .handler = omap2_gp_timer_interrupt,
105};
106
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800107static int omap2_gp_timer_set_next_event(unsigned long cycles,
108 struct clock_event_device *evt)
Tony Lindgren1dbae812005-11-10 14:26:51 +0000109{
Tony Lindgrenee17f112011-09-16 15:44:20 -0700110 __omap_dm_timer_load_start(&clkev, OMAP_TIMER_CTRL_ST,
Jon Hunter971d0252012-09-27 11:49:45 -0500111 0xffffffff - cycles, OMAP_TIMER_POSTED);
Tony Lindgren1dbae812005-11-10 14:26:51 +0000112
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800113 return 0;
114}
115
116static void omap2_gp_timer_set_mode(enum clock_event_mode mode,
117 struct clock_event_device *evt)
118{
119 u32 period;
120
Jon Hunter971d0252012-09-27 11:49:45 -0500121 __omap_dm_timer_stop(&clkev, OMAP_TIMER_POSTED, clkev.rate);
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800122
123 switch (mode) {
124 case CLOCK_EVT_MODE_PERIODIC:
Tony Lindgrenaa561882011-03-29 15:54:48 -0700125 period = clkev.rate / HZ;
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800126 period -= 1;
Tony Lindgrenaa561882011-03-29 15:54:48 -0700127 /* Looks like we need to first set the load value separately */
Tony Lindgrenee17f112011-09-16 15:44:20 -0700128 __omap_dm_timer_write(&clkev, OMAP_TIMER_LOAD_REG,
Jon Hunter971d0252012-09-27 11:49:45 -0500129 0xffffffff - period, OMAP_TIMER_POSTED);
Tony Lindgrenee17f112011-09-16 15:44:20 -0700130 __omap_dm_timer_load_start(&clkev,
Tony Lindgrenaa561882011-03-29 15:54:48 -0700131 OMAP_TIMER_CTRL_AR | OMAP_TIMER_CTRL_ST,
Jon Hunter971d0252012-09-27 11:49:45 -0500132 0xffffffff - period, OMAP_TIMER_POSTED);
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800133 break;
134 case CLOCK_EVT_MODE_ONESHOT:
135 break;
136 case CLOCK_EVT_MODE_UNUSED:
137 case CLOCK_EVT_MODE_SHUTDOWN:
138 case CLOCK_EVT_MODE_RESUME:
139 break;
140 }
141}
142
143static struct clock_event_device clockevent_gpt = {
Vaibhav Hiremathf36921b2012-05-09 10:07:05 -0700144 .name = "gp_timer",
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800145 .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
146 .shift = 32,
Santosh Shilimkar11d6ec22012-03-17 15:00:16 +0530147 .rating = 300,
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800148 .set_next_event = omap2_gp_timer_set_next_event,
149 .set_mode = omap2_gp_timer_set_mode,
150};
151
Jon Hunterad24bde2012-06-20 15:55:24 -0500152static struct property device_disabled = {
153 .name = "status",
154 .length = sizeof("disabled"),
155 .value = "disabled",
156};
157
158static struct of_device_id omap_timer_match[] __initdata = {
159 { .compatible = "ti,omap2-timer", },
160 { }
161};
162
Jon Hunter9883f7c2012-10-09 14:12:26 -0500163static struct of_device_id omap_counter_match[] __initdata = {
164 { .compatible = "ti,omap-counter32k", },
165 { }
166};
167
Jon Hunterad24bde2012-06-20 15:55:24 -0500168/**
Jon Hunter9725f442012-05-14 10:41:37 -0500169 * omap_get_timer_dt - get a timer using device-tree
170 * @match - device-tree match structure for matching a device type
171 * @property - optional timer property to match
172 *
173 * Helper function to get a timer during early boot using device-tree for use
174 * as kernel system timer. Optionally, the property argument can be used to
175 * select a timer with a specific property. Once a timer is found then mark
176 * the timer node in device-tree as disabled, to prevent the kernel from
177 * registering this timer as a platform device and so no one else can use it.
178 */
179static struct device_node * __init omap_get_timer_dt(struct of_device_id *match,
180 const char *property)
181{
182 struct device_node *np;
183
184 for_each_matching_node(np, match) {
185 if (!of_device_is_available(np)) {
186 of_node_put(np);
187 continue;
188 }
189
190 if (property && !of_get_property(np, property, NULL)) {
191 of_node_put(np);
192 continue;
193 }
194
195 prom_add_property(np, &device_disabled);
196 return np;
197 }
198
199 return NULL;
200}
201
202/**
Jon Hunterad24bde2012-06-20 15:55:24 -0500203 * omap_dmtimer_init - initialisation function when device tree is used
204 *
205 * For secure OMAP3 devices, timers with device type "timer-secure" cannot
206 * be used by the kernel as they are reserved. Therefore, to prevent the
207 * kernel registering these devices remove them dynamically from the device
208 * tree on boot.
209 */
210void __init omap_dmtimer_init(void)
211{
212 struct device_node *np;
213
214 if (!cpu_is_omap34xx())
215 return;
216
217 /* If we are a secure device, remove any secure timer nodes */
218 if ((omap_type() != OMAP2_DEVICE_TYPE_GP)) {
Jon Hunter9725f442012-05-14 10:41:37 -0500219 np = omap_get_timer_dt(omap_timer_match, "ti,timer-secure");
220 if (np)
221 of_node_put(np);
Jon Hunterad24bde2012-06-20 15:55:24 -0500222 }
223}
224
Jon Hunterbfd6d022012-09-27 12:47:43 -0500225/**
226 * omap_dm_timer_get_errata - get errata flags for a timer
227 *
228 * Get the timer errata flags that are specific to the OMAP device being used.
229 */
230u32 __init omap_dm_timer_get_errata(void)
231{
232 if (cpu_is_omap24xx())
233 return 0;
234
235 return OMAP_TIMER_ERRATA_I103_I767;
236}
237
Tony Lindgrenaa561882011-03-29 15:54:48 -0700238static int __init omap_dm_timer_init_one(struct omap_dm_timer *timer,
239 int gptimer_id,
Jon Hunter9725f442012-05-14 10:41:37 -0500240 const char *fck_source,
Jon Hunterbfd6d022012-09-27 12:47:43 -0500241 const char *property,
242 int posted)
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800243{
Tony Lindgrenaa561882011-03-29 15:54:48 -0700244 char name[10]; /* 10 = sizeof("gptXX_Xck0") */
Jon Hunter9725f442012-05-14 10:41:37 -0500245 const char *oh_name;
246 struct device_node *np;
Tony Lindgrenaa561882011-03-29 15:54:48 -0700247 struct omap_hwmod *oh;
Paul Walmsley6c0c27f2012-04-19 04:01:50 -0600248 struct resource irq_rsrc, mem_rsrc;
Tony Lindgrenaa561882011-03-29 15:54:48 -0700249 size_t size;
250 int res = 0;
Paul Walmsley6c0c27f2012-04-19 04:01:50 -0600251 int r;
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800252
Jon Hunter9725f442012-05-14 10:41:37 -0500253 if (of_have_populated_dt()) {
254 np = omap_get_timer_dt(omap_timer_match, NULL);
255 if (!np)
256 return -ENODEV;
257
258 of_property_read_string_index(np, "ti,hwmods", 0, &oh_name);
259 if (!oh_name)
260 return -ENODEV;
261
262 timer->irq = irq_of_parse_and_map(np, 0);
263 if (!timer->irq)
264 return -ENXIO;
265
266 timer->io_base = of_iomap(np, 0);
267
268 of_node_put(np);
269 } else {
270 if (omap_dm_timer_reserve_systimer(gptimer_id))
271 return -ENODEV;
272
273 sprintf(name, "timer%d", gptimer_id);
274 oh_name = name;
275 }
276
Jon Hunter9725f442012-05-14 10:41:37 -0500277 oh = omap_hwmod_lookup(oh_name);
Tony Lindgrenaa561882011-03-29 15:54:48 -0700278 if (!oh)
279 return -ENODEV;
Paul Walmsleyf2480762009-04-23 21:11:10 -0600280
Jon Hunter9725f442012-05-14 10:41:37 -0500281 if (!of_have_populated_dt()) {
282 r = omap_hwmod_get_resource_byname(oh, IORESOURCE_IRQ, NULL,
283 &irq_rsrc);
284 if (r)
285 return -ENXIO;
286 timer->irq = irq_rsrc.start;
Paul Walmsley6c0c27f2012-04-19 04:01:50 -0600287
Jon Hunter9725f442012-05-14 10:41:37 -0500288 r = omap_hwmod_get_resource_byname(oh, IORESOURCE_MEM, NULL,
289 &mem_rsrc);
290 if (r)
291 return -ENXIO;
292 timer->phys_base = mem_rsrc.start;
293 size = mem_rsrc.end - mem_rsrc.start;
Tony Lindgrenaa561882011-03-29 15:54:48 -0700294
Jon Hunter9725f442012-05-14 10:41:37 -0500295 /* Static mapping, never released */
296 timer->io_base = ioremap(timer->phys_base, size);
297 }
298
Tony Lindgrenaa561882011-03-29 15:54:48 -0700299 if (!timer->io_base)
300 return -ENXIO;
301
302 /* After the dmtimer is using hwmod these clocks won't be needed */
Tarun Kanti DebBarmaae6df412012-07-05 18:10:59 +0530303 timer->fclk = clk_get(NULL, omap_hwmod_get_main_clk(oh));
Tony Lindgrenaa561882011-03-29 15:54:48 -0700304 if (IS_ERR(timer->fclk))
305 return -ENODEV;
306
Jon Hunter9725f442012-05-14 10:41:37 -0500307 /* FIXME: Need to remove hard-coded test on timer ID */
Tony Lindgrenaa561882011-03-29 15:54:48 -0700308 if (gptimer_id != 12) {
309 struct clk *src;
310
311 src = clk_get(NULL, fck_source);
312 if (IS_ERR(src)) {
313 res = -EINVAL;
314 } else {
Jon Hunterb1538832012-09-28 11:43:30 -0500315 res = clk_set_parent(timer->fclk, src);
Tony Lindgrenaa561882011-03-29 15:54:48 -0700316 if (IS_ERR_VALUE(res))
Jon Hunter9725f442012-05-14 10:41:37 -0500317 pr_warn("%s: %s cannot set source\n",
318 __func__, oh->name);
Tony Lindgrenaa561882011-03-29 15:54:48 -0700319 clk_put(src);
320 }
321 }
Jon Hunterb1538832012-09-28 11:43:30 -0500322
323 omap_hwmod_setup_one(oh_name);
324 omap_hwmod_enable(oh);
Tony Lindgrenee17f112011-09-16 15:44:20 -0700325 __omap_dm_timer_init_regs(timer);
Jon Hunterbfd6d022012-09-27 12:47:43 -0500326
327 if (posted)
328 __omap_dm_timer_enable_posted(timer);
329
330 /* Check that the intended posted configuration matches the actual */
331 if (posted != timer->posted)
332 return -EINVAL;
Tony Lindgrenaa561882011-03-29 15:54:48 -0700333
334 timer->rate = clk_get_rate(timer->fclk);
Tony Lindgrenaa561882011-03-29 15:54:48 -0700335 timer->reserved = 1;
Paul Walmsley38698be2011-02-23 00:14:08 -0700336
Tony Lindgrenaa561882011-03-29 15:54:48 -0700337 return res;
338}
Paul Walmsleyf2480762009-04-23 21:11:10 -0600339
Tony Lindgrenaa561882011-03-29 15:54:48 -0700340static void __init omap2_gp_clockevent_init(int gptimer_id,
Jon Hunter9725f442012-05-14 10:41:37 -0500341 const char *fck_source,
342 const char *property)
Tony Lindgrenaa561882011-03-29 15:54:48 -0700343{
344 int res;
Paul Walmsleyf2480762009-04-23 21:11:10 -0600345
Jon Hunterbfd6d022012-09-27 12:47:43 -0500346 clkev.errata = omap_dm_timer_get_errata();
347
348 /*
349 * For clock-event timers we never read the timer counter and
350 * so we are not impacted by errata i103 and i767. Therefore,
351 * we can safely ignore this errata for clock-event timers.
352 */
353 __omap_dm_timer_override_errata(&clkev, OMAP_TIMER_ERRATA_I103_I767);
354
355 res = omap_dm_timer_init_one(&clkev, gptimer_id, fck_source, property,
356 OMAP_TIMER_POSTED);
Tony Lindgrenaa561882011-03-29 15:54:48 -0700357 BUG_ON(res);
Paul Walmsleyf2480762009-04-23 21:11:10 -0600358
Paul Walmsleya032d332012-08-03 09:21:10 -0600359 omap2_gp_timer_irq.dev_id = &clkev;
Tony Lindgrenaa561882011-03-29 15:54:48 -0700360 setup_irq(clkev.irq, &omap2_gp_timer_irq);
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800361
Tony Lindgrenee17f112011-09-16 15:44:20 -0700362 __omap_dm_timer_int_enable(&clkev, OMAP_TIMER_INT_OVERFLOW);
Tony Lindgrenaa561882011-03-29 15:54:48 -0700363
364 clockevent_gpt.mult = div_sc(clkev.rate, NSEC_PER_SEC,
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800365 clockevent_gpt.shift);
366 clockevent_gpt.max_delta_ns =
367 clockevent_delta2ns(0xffffffff, &clockevent_gpt);
368 clockevent_gpt.min_delta_ns =
Aaro Koskinendf88acb2009-01-29 08:57:17 -0800369 clockevent_delta2ns(3, &clockevent_gpt);
370 /* Timer internal resynch latency. */
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800371
Santosh Shilimkar11d6ec22012-03-17 15:00:16 +0530372 clockevent_gpt.cpumask = cpu_possible_mask;
373 clockevent_gpt.irq = omap_dm_timer_get_irq(&clkev);
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800374 clockevents_register_device(&clockevent_gpt);
Tony Lindgrenaa561882011-03-29 15:54:48 -0700375
376 pr_info("OMAP clockevent source: GPTIMER%d at %lu Hz\n",
377 gptimer_id, clkev.rate);
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800378}
379
Paul Walmsleyf2480762009-04-23 21:11:10 -0600380/* Clocksource code */
Tony Lindgren3d05a3e2011-03-29 15:54:49 -0700381static struct omap_dm_timer clksrc;
Vaibhav Hiremath1fe97c82012-05-09 10:07:05 -0700382static bool use_gptimer_clksrc;
Tony Lindgren3d05a3e2011-03-29 15:54:49 -0700383
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800384/*
385 * clocksource
386 */
Magnus Damm8e196082009-04-21 12:24:00 -0700387static cycle_t clocksource_read_cycles(struct clocksource *cs)
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800388{
Jon Hunter971d0252012-09-27 11:49:45 -0500389 return (cycle_t)__omap_dm_timer_read_counter(&clksrc,
Jon Hunterbfd6d022012-09-27 12:47:43 -0500390 OMAP_TIMER_NONPOSTED);
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800391}
392
393static struct clocksource clocksource_gpt = {
Vaibhav Hiremathf36921b2012-05-09 10:07:05 -0700394 .name = "gp_timer",
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800395 .rating = 300,
396 .read = clocksource_read_cycles,
397 .mask = CLOCKSOURCE_MASK(32),
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800398 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
399};
400
Marc Zyngier2f0778af2011-12-15 12:19:23 +0100401static u32 notrace dmtimer_read_sched_clock(void)
Paul Walmsleycbc94382011-02-22 19:59:49 -0700402{
Tony Lindgren3d05a3e2011-03-29 15:54:49 -0700403 if (clksrc.reserved)
Jon Hunter971d0252012-09-27 11:49:45 -0500404 return __omap_dm_timer_read_counter(&clksrc,
Jon Hunterbfd6d022012-09-27 12:47:43 -0500405 OMAP_TIMER_NONPOSTED);
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800406
Marc Zyngier2f0778af2011-12-15 12:19:23 +0100407 return 0;
Tony Lindgren3d05a3e2011-03-29 15:54:49 -0700408}
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800409
Igor Grinberg45caae72012-08-28 01:26:14 +0300410#ifdef CONFIG_OMAP_32K_TIMER
Tony Lindgren3d05a3e2011-03-29 15:54:49 -0700411/* Setup free-running counter for clocksource */
Vaibhav Hiremath1fe97c82012-05-09 10:07:05 -0700412static int __init omap2_sync32k_clocksource_init(void)
413{
414 int ret;
Jon Hunter9883f7c2012-10-09 14:12:26 -0500415 struct device_node *np = NULL;
Vaibhav Hiremath1fe97c82012-05-09 10:07:05 -0700416 struct omap_hwmod *oh;
417 void __iomem *vbase;
418 const char *oh_name = "counter_32k";
419
420 /*
Jon Hunter9883f7c2012-10-09 14:12:26 -0500421 * If device-tree is present, then search the DT blob
422 * to see if the 32kHz counter is supported.
423 */
424 if (of_have_populated_dt()) {
425 np = omap_get_timer_dt(omap_counter_match, NULL);
426 if (!np)
427 return -ENODEV;
428
429 of_property_read_string_index(np, "ti,hwmods", 0, &oh_name);
430 if (!oh_name)
431 return -ENODEV;
432 }
433
434 /*
Vaibhav Hiremath1fe97c82012-05-09 10:07:05 -0700435 * First check hwmod data is available for sync32k counter
436 */
437 oh = omap_hwmod_lookup(oh_name);
438 if (!oh || oh->slaves_cnt == 0)
439 return -ENODEV;
440
441 omap_hwmod_setup_one(oh_name);
442
Jon Hunter9883f7c2012-10-09 14:12:26 -0500443 if (np) {
444 vbase = of_iomap(np, 0);
445 of_node_put(np);
446 } else {
447 vbase = omap_hwmod_get_mpu_rt_va(oh);
448 }
449
Vaibhav Hiremath1fe97c82012-05-09 10:07:05 -0700450 if (!vbase) {
451 pr_warn("%s: failed to get counter_32k resource\n", __func__);
452 return -ENXIO;
453 }
454
455 ret = omap_hwmod_enable(oh);
456 if (ret) {
457 pr_warn("%s: failed to enable counter_32k module (%d)\n",
458 __func__, ret);
459 return ret;
460 }
461
462 ret = omap_init_clocksource_32k(vbase);
463 if (ret) {
464 pr_warn("%s: failed to initialize counter_32k as a clocksource (%d)\n",
465 __func__, ret);
466 omap_hwmod_idle(oh);
467 }
468
469 return ret;
470}
Igor Grinberg45caae72012-08-28 01:26:14 +0300471#else
472static inline int omap2_sync32k_clocksource_init(void)
473{
474 return -ENODEV;
475}
476#endif
Vaibhav Hiremath1fe97c82012-05-09 10:07:05 -0700477
478static void __init omap2_gptimer_clocksource_init(int gptimer_id,
Tony Lindgren3d05a3e2011-03-29 15:54:49 -0700479 const char *fck_source)
480{
481 int res;
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800482
Jon Hunterbfd6d022012-09-27 12:47:43 -0500483 clksrc.errata = omap_dm_timer_get_errata();
484
485 res = omap_dm_timer_init_one(&clksrc, gptimer_id, fck_source, NULL,
486 OMAP_TIMER_NONPOSTED);
Tony Lindgren3d05a3e2011-03-29 15:54:49 -0700487 BUG_ON(res);
Paul Walmsleycbc94382011-02-22 19:59:49 -0700488
Tony Lindgrenee17f112011-09-16 15:44:20 -0700489 __omap_dm_timer_load_start(&clksrc,
Jon Hunter971d0252012-09-27 11:49:45 -0500490 OMAP_TIMER_CTRL_ST | OMAP_TIMER_CTRL_AR, 0,
Jon Hunterbfd6d022012-09-27 12:47:43 -0500491 OMAP_TIMER_NONPOSTED);
Marc Zyngier2f0778af2011-12-15 12:19:23 +0100492 setup_sched_clock(dmtimer_read_sched_clock, 32, clksrc.rate);
Tony Lindgren3d05a3e2011-03-29 15:54:49 -0700493
494 if (clocksource_register_hz(&clocksource_gpt, clksrc.rate))
495 pr_err("Could not register clocksource %s\n",
496 clocksource_gpt.name);
Vaibhav Hiremath1fe97c82012-05-09 10:07:05 -0700497 else
498 pr_info("OMAP clocksource: GPTIMER%d at %lu Hz\n",
499 gptimer_id, clksrc.rate);
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800500}
Vaibhav Hiremath1fe97c82012-05-09 10:07:05 -0700501
502static void __init omap2_clocksource_init(int gptimer_id,
503 const char *fck_source)
504{
505 /*
506 * First give preference to kernel parameter configuration
507 * by user (clocksource="gp_timer").
508 *
509 * In case of missing kernel parameter for clocksource,
510 * first check for availability for 32k-sync timer, in case
511 * of failure in finding 32k_counter module or registering
512 * it as clocksource, execution will fallback to gp-timer.
513 */
514 if (use_gptimer_clksrc == true)
515 omap2_gptimer_clocksource_init(gptimer_id, fck_source);
516 else if (omap2_sync32k_clocksource_init())
517 /* Fall back to gp-timer code */
518 omap2_gptimer_clocksource_init(gptimer_id, fck_source);
519}
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800520
Santosh Shilimkarfa6d79d2012-08-13 14:24:24 +0530521#ifdef CONFIG_SOC_HAS_REALTIME_COUNTER
522/*
523 * The realtime counter also called master counter, is a free-running
524 * counter, which is related to real time. It produces the count used
525 * by the CPU local timer peripherals in the MPU cluster. The timer counts
526 * at a rate of 6.144 MHz. Because the device operates on different clocks
527 * in different power modes, the master counter shifts operation between
528 * clocks, adjusting the increment per clock in hardware accordingly to
529 * maintain a constant count rate.
530 */
531static void __init realtime_counter_init(void)
532{
533 void __iomem *base;
534 static struct clk *sys_clk;
535 unsigned long rate;
536 unsigned int reg, num, den;
537
538 base = ioremap(REALTIME_COUNTER_BASE, SZ_32);
539 if (!base) {
540 pr_err("%s: ioremap failed\n", __func__);
541 return;
542 }
543 sys_clk = clk_get(NULL, "sys_clkin_ck");
Wei Yongjun533b2982012-10-08 15:01:41 -0700544 if (IS_ERR(sys_clk)) {
Santosh Shilimkarfa6d79d2012-08-13 14:24:24 +0530545 pr_err("%s: failed to get system clock handle\n", __func__);
546 iounmap(base);
547 return;
548 }
549
550 rate = clk_get_rate(sys_clk);
551 /* Numerator/denumerator values refer TRM Realtime Counter section */
552 switch (rate) {
553 case 1200000:
554 num = 64;
555 den = 125;
556 break;
557 case 1300000:
558 num = 768;
559 den = 1625;
560 break;
561 case 19200000:
562 num = 8;
563 den = 25;
564 break;
565 case 2600000:
566 num = 384;
567 den = 1625;
568 break;
569 case 2700000:
570 num = 256;
571 den = 1125;
572 break;
573 case 38400000:
574 default:
575 /* Program it for 38.4 MHz */
576 num = 4;
577 den = 25;
578 break;
579 }
580
581 /* Program numerator and denumerator registers */
582 reg = __raw_readl(base + INCREMENTER_NUMERATOR_OFFSET) &
583 NUMERATOR_DENUMERATOR_MASK;
584 reg |= num;
585 __raw_writel(reg, base + INCREMENTER_NUMERATOR_OFFSET);
586
587 reg = __raw_readl(base + INCREMENTER_NUMERATOR_OFFSET) &
588 NUMERATOR_DENUMERATOR_MASK;
589 reg |= den;
590 __raw_writel(reg, base + INCREMENTER_DENUMERATOR_RELOAD_OFFSET);
591
592 iounmap(base);
593}
594#else
595static inline void __init realtime_counter_init(void)
596{}
597#endif
598
Jon Hunter9725f442012-05-14 10:41:37 -0500599#define OMAP_SYS_TIMER_INIT(name, clkev_nr, clkev_src, clkev_prop, \
Tony Lindgren3d05a3e2011-03-29 15:54:49 -0700600 clksrc_nr, clksrc_src) \
Tony Lindgrene74984e2011-03-29 15:54:48 -0700601static void __init omap##name##_timer_init(void) \
602{ \
Jon Hunterad24bde2012-06-20 15:55:24 -0500603 omap_dmtimer_init(); \
Jon Hunter9725f442012-05-14 10:41:37 -0500604 omap2_gp_clockevent_init((clkev_nr), clkev_src, clkev_prop); \
Vaibhav Hiremath1fe97c82012-05-09 10:07:05 -0700605 omap2_clocksource_init((clksrc_nr), clksrc_src); \
Tony Lindgrene74984e2011-03-29 15:54:48 -0700606}
607
608#define OMAP_SYS_TIMER(name) \
609struct sys_timer omap##name##_timer = { \
610 .init = omap##name##_timer_init, \
611};
612
613#ifdef CONFIG_ARCH_OMAP2
Jon Hunter9725f442012-05-14 10:41:37 -0500614OMAP_SYS_TIMER_INIT(2, 1, OMAP2_CLKEV_SOURCE, "ti,timer-alwon",
615 2, OMAP2_MPU_SOURCE)
Tony Lindgrene74984e2011-03-29 15:54:48 -0700616OMAP_SYS_TIMER(2)
617#endif
618
619#ifdef CONFIG_ARCH_OMAP3
Jon Hunter9725f442012-05-14 10:41:37 -0500620OMAP_SYS_TIMER_INIT(3, 1, OMAP3_CLKEV_SOURCE, "ti,timer-alwon",
621 2, OMAP3_MPU_SOURCE)
Tony Lindgrene74984e2011-03-29 15:54:48 -0700622OMAP_SYS_TIMER(3)
Tony Lindgren3d05a3e2011-03-29 15:54:49 -0700623OMAP_SYS_TIMER_INIT(3_secure, OMAP3_SECURE_TIMER, OMAP3_CLKEV_SOURCE,
Jon Hunter9725f442012-05-14 10:41:37 -0500624 TIMER_PROP_SECURE, 2, OMAP3_MPU_SOURCE)
Tony Lindgrene74984e2011-03-29 15:54:48 -0700625OMAP_SYS_TIMER(3_secure)
626#endif
627
Afzal Mohammed08f30982012-05-11 00:38:49 +0530628#ifdef CONFIG_SOC_AM33XX
Jon Hunter9725f442012-05-14 10:41:37 -0500629OMAP_SYS_TIMER_INIT(3_am33xx, 1, OMAP4_MPU_SOURCE, "ti,timer-alwon",
630 2, OMAP4_MPU_SOURCE)
Afzal Mohammed08f30982012-05-11 00:38:49 +0530631OMAP_SYS_TIMER(3_am33xx)
632#endif
633
Tony Lindgrene74984e2011-03-29 15:54:48 -0700634#ifdef CONFIG_ARCH_OMAP4
Marc Zyngiera45c9832012-01-10 19:44:19 +0000635#ifdef CONFIG_LOCAL_TIMERS
636static DEFINE_TWD_LOCAL_TIMER(twd_local_timer,
Tony Lindgren3f216ef2012-10-16 11:19:16 -0700637 OMAP44XX_LOCAL_TWD_BASE, 29);
Marc Zyngiera45c9832012-01-10 19:44:19 +0000638#endif
639
Tony Lindgrene74984e2011-03-29 15:54:48 -0700640static void __init omap4_timer_init(void)
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800641{
Jon Hunter9725f442012-05-14 10:41:37 -0500642 omap2_gp_clockevent_init(1, OMAP4_CLKEV_SOURCE, "ti,timer-alwon");
Vaibhav Hiremath1fe97c82012-05-09 10:07:05 -0700643 omap2_clocksource_init(2, OMAP4_MPU_SOURCE);
Marc Zyngiera45c9832012-01-10 19:44:19 +0000644#ifdef CONFIG_LOCAL_TIMERS
645 /* Local timers are not supprted on OMAP4430 ES1.0 */
646 if (omap_rev() != OMAP4430_REV_ES1_0) {
647 int err;
648
Santosh Shilimkareed0de22012-07-04 18:32:32 +0530649 if (of_have_populated_dt()) {
650 twd_local_timer_of_register();
651 return;
652 }
653
Marc Zyngiera45c9832012-01-10 19:44:19 +0000654 err = twd_local_timer_register(&twd_local_timer);
655 if (err)
656 pr_err("twd_local_timer_register failed %d\n", err);
657 }
658#endif
Tony Lindgren1dbae812005-11-10 14:26:51 +0000659}
Tony Lindgrene74984e2011-03-29 15:54:48 -0700660OMAP_SYS_TIMER(4)
661#endif
Tarun Kanti DebBarmac345c8b2011-09-20 17:00:18 +0530662
R Sricharan37b32802012-05-02 13:07:12 +0530663#ifdef CONFIG_SOC_OMAP5
Santosh Shilimkarfa6d79d2012-08-13 14:24:24 +0530664static void __init omap5_timer_init(void)
665{
Santosh Shilimkar3c7c5da2012-08-13 14:39:03 +0530666 int err;
667
Jon Hunter9725f442012-05-14 10:41:37 -0500668 omap2_gp_clockevent_init(1, OMAP4_CLKEV_SOURCE, "ti,timer-alwon");
Santosh Shilimkarfa6d79d2012-08-13 14:24:24 +0530669 omap2_clocksource_init(2, OMAP4_MPU_SOURCE);
670 realtime_counter_init();
Santosh Shilimkar3c7c5da2012-08-13 14:39:03 +0530671
672 err = arch_timer_of_register();
673 if (err)
674 pr_err("%s: arch_timer_register failed %d\n", __func__, err);
Santosh Shilimkarfa6d79d2012-08-13 14:24:24 +0530675}
R Sricharan37b32802012-05-02 13:07:12 +0530676OMAP_SYS_TIMER(5)
677#endif
678
Tarun Kanti DebBarmac345c8b2011-09-20 17:00:18 +0530679/**
Tarun Kanti DebBarmac345c8b2011-09-20 17:00:18 +0530680 * omap_timer_init - build and register timer device with an
681 * associated timer hwmod
682 * @oh: timer hwmod pointer to be used to build timer device
683 * @user: parameter that can be passed from calling hwmod API
684 *
685 * Called by omap_hwmod_for_each_by_class to register each of the timer
686 * devices present in the system. The number of timer devices is known
687 * by parsing through the hwmod database for a given class name. At the
688 * end of function call memory is allocated for timer device and it is
689 * registered to the framework ready to be proved by the driver.
690 */
691static int __init omap_timer_init(struct omap_hwmod *oh, void *unused)
692{
693 int id;
694 int ret = 0;
695 char *name = "omap_timer";
696 struct dmtimer_platform_data *pdata;
Tony Lindgrenc541c152011-10-04 09:47:06 -0700697 struct platform_device *pdev;
Tarun Kanti DebBarmac345c8b2011-09-20 17:00:18 +0530698 struct omap_timer_capability_dev_attr *timer_dev_attr;
699
700 pr_debug("%s: %s\n", __func__, oh->name);
701
702 /* on secure device, do not register secure timer */
703 timer_dev_attr = oh->dev_attr;
704 if (omap_type() != OMAP2_DEVICE_TYPE_GP && timer_dev_attr)
705 if (timer_dev_attr->timer_capability == OMAP_TIMER_SECURE)
706 return ret;
707
708 pdata = kzalloc(sizeof(*pdata), GFP_KERNEL);
709 if (!pdata) {
710 pr_err("%s: No memory for [%s]\n", __func__, oh->name);
711 return -ENOMEM;
712 }
713
714 /*
715 * Extract the IDs from name field in hwmod database
716 * and use the same for constructing ids' for the
717 * timer devices. In a way, we are avoiding usage of
718 * static variable witin the function to do the same.
719 * CAUTION: We have to be careful and make sure the
720 * name in hwmod database does not change in which case
721 * we might either make corresponding change here or
722 * switch back static variable mechanism.
723 */
724 sscanf(oh->name, "timer%2d", &id);
725
Jon Hunterd1c16912012-06-05 12:34:52 -0500726 if (timer_dev_attr)
727 pdata->timer_capability = timer_dev_attr->timer_capability;
Tarun Kanti DebBarmac345c8b2011-09-20 17:00:18 +0530728
Jon Hunterbfd6d022012-09-27 12:47:43 -0500729 pdata->timer_errata = omap_dm_timer_get_errata();
Tony Lindgren6e740f92012-10-29 15:20:45 -0700730 pdata->get_context_loss_count = omap_pm_get_dev_context_loss_count;
731
Tony Lindgrenc541c152011-10-04 09:47:06 -0700732 pdev = omap_device_build(name, id, oh, pdata, sizeof(*pdata),
Benoit Coussonc16ae1e2011-10-04 23:20:41 +0200733 NULL, 0, 0);
Tarun Kanti DebBarmac345c8b2011-09-20 17:00:18 +0530734
Tony Lindgrenc541c152011-10-04 09:47:06 -0700735 if (IS_ERR(pdev)) {
Tarun Kanti DebBarmac345c8b2011-09-20 17:00:18 +0530736 pr_err("%s: Can't build omap_device for %s: %s.\n",
737 __func__, name, oh->name);
738 ret = -EINVAL;
739 }
740
741 kfree(pdata);
742
743 return ret;
744}
Tarun Kanti DebBarma3392cdd2011-09-20 17:00:20 +0530745
746/**
747 * omap2_dm_timer_init - top level regular device initialization
748 *
749 * Uses dedicated hwmod api to parse through hwmod database for
750 * given class name and then build and register the timer device.
751 */
752static int __init omap2_dm_timer_init(void)
753{
754 int ret;
755
Jon Hunter9725f442012-05-14 10:41:37 -0500756 /* If dtb is there, the devices will be created dynamically */
757 if (of_have_populated_dt())
758 return -ENODEV;
759
Tarun Kanti DebBarma3392cdd2011-09-20 17:00:20 +0530760 ret = omap_hwmod_for_each_by_class("timer", omap_timer_init, NULL);
761 if (unlikely(ret)) {
762 pr_err("%s: device registration failed.\n", __func__);
763 return -EINVAL;
764 }
765
766 return 0;
767}
768arch_initcall(omap2_dm_timer_init);
Vaibhav Hiremath1fe97c82012-05-09 10:07:05 -0700769
770/**
771 * omap2_override_clocksource - clocksource override with user configuration
772 *
773 * Allows user to override default clocksource, using kernel parameter
774 * clocksource="gp_timer" (For all OMAP2PLUS architectures)
775 *
776 * Note that, here we are using same standard kernel parameter "clocksource=",
777 * and not introducing any OMAP specific interface.
778 */
779static int __init omap2_override_clocksource(char *str)
780{
781 if (!str)
782 return 0;
783 /*
784 * For OMAP architecture, we only have two options
785 * - sync_32k (default)
786 * - gp_timer (sys_clk based)
787 */
788 if (!strcmp(str, "gp_timer"))
789 use_gptimer_clksrc = true;
790
791 return 0;
792}
793early_param("clocksource", omap2_override_clocksource);