blob: bfd47570cc9142e2d0d2b2d0e471233f4a925979 [file] [log] [blame]
Tony Lindgren92105bb2005-09-07 17:20:26 +01001/*
2 * linux/arch/arm/plat-omap/dmtimer.c
3 *
4 * OMAP Dual-Mode Timers
5 *
6 * Copyright (C) 2005 Nokia Corporation
Timo Teras77900a22006-06-26 16:16:12 -07007 * OMAP2 support by Juha Yrjola
8 * API improvements and OMAP2 clock framework support by Timo Teras
Tony Lindgren92105bb2005-09-07 17:20:26 +01009 *
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the
12 * Free Software Foundation; either version 2 of the License, or (at your
13 * option) any later version.
14 *
15 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
16 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
17 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
18 * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 *
24 * You should have received a copy of the GNU General Public License along
25 * with this program; if not, write to the Free Software Foundation, Inc.,
26 * 675 Mass Ave, Cambridge, MA 02139, USA.
27 */
28
29#include <linux/init.h>
Timo Teras77900a22006-06-26 16:16:12 -070030#include <linux/spinlock.h>
31#include <linux/errno.h>
32#include <linux/list.h>
33#include <linux/clk.h>
34#include <linux/delay.h>
Russell Kingfced80c2008-09-06 12:10:45 +010035#include <linux/io.h>
Timo Kokkonen6c366e32009-03-23 18:07:46 -070036#include <linux/module.h>
Russell Kinga09e64f2008-08-05 16:14:15 +010037#include <mach/hardware.h>
38#include <mach/dmtimer.h>
Russell Kinga09e64f2008-08-05 16:14:15 +010039#include <mach/irqs.h>
Tony Lindgren92105bb2005-09-07 17:20:26 +010040
Timo Teras77900a22006-06-26 16:16:12 -070041/* register offsets */
Richard Woodruff0f0d0802008-07-03 12:24:30 +030042#define _OMAP_TIMER_ID_OFFSET 0x00
43#define _OMAP_TIMER_OCP_CFG_OFFSET 0x10
44#define _OMAP_TIMER_SYS_STAT_OFFSET 0x14
45#define _OMAP_TIMER_STAT_OFFSET 0x18
46#define _OMAP_TIMER_INT_EN_OFFSET 0x1c
47#define _OMAP_TIMER_WAKEUP_EN_OFFSET 0x20
48#define _OMAP_TIMER_CTRL_OFFSET 0x24
49#define OMAP_TIMER_CTRL_GPOCFG (1 << 14)
50#define OMAP_TIMER_CTRL_CAPTMODE (1 << 13)
51#define OMAP_TIMER_CTRL_PT (1 << 12)
52#define OMAP_TIMER_CTRL_TCM_LOWTOHIGH (0x1 << 8)
53#define OMAP_TIMER_CTRL_TCM_HIGHTOLOW (0x2 << 8)
54#define OMAP_TIMER_CTRL_TCM_BOTHEDGES (0x3 << 8)
55#define OMAP_TIMER_CTRL_SCPWM (1 << 7)
56#define OMAP_TIMER_CTRL_CE (1 << 6) /* compare enable */
57#define OMAP_TIMER_CTRL_PRE (1 << 5) /* prescaler enable */
58#define OMAP_TIMER_CTRL_PTV_SHIFT 2 /* prescaler value shift */
59#define OMAP_TIMER_CTRL_POSTED (1 << 2)
60#define OMAP_TIMER_CTRL_AR (1 << 1) /* auto-reload enable */
61#define OMAP_TIMER_CTRL_ST (1 << 0) /* start timer */
62#define _OMAP_TIMER_COUNTER_OFFSET 0x28
63#define _OMAP_TIMER_LOAD_OFFSET 0x2c
64#define _OMAP_TIMER_TRIGGER_OFFSET 0x30
65#define _OMAP_TIMER_WRITE_PEND_OFFSET 0x34
66#define WP_NONE 0 /* no write pending bit */
67#define WP_TCLR (1 << 0)
68#define WP_TCRR (1 << 1)
69#define WP_TLDR (1 << 2)
70#define WP_TTGR (1 << 3)
71#define WP_TMAR (1 << 4)
72#define WP_TPIR (1 << 5)
73#define WP_TNIR (1 << 6)
74#define WP_TCVR (1 << 7)
75#define WP_TOCR (1 << 8)
76#define WP_TOWR (1 << 9)
77#define _OMAP_TIMER_MATCH_OFFSET 0x38
78#define _OMAP_TIMER_CAPTURE_OFFSET 0x3c
79#define _OMAP_TIMER_IF_CTRL_OFFSET 0x40
80#define _OMAP_TIMER_CAPTURE2_OFFSET 0x44 /* TCAR2, 34xx only */
81#define _OMAP_TIMER_TICK_POS_OFFSET 0x48 /* TPIR, 34xx only */
82#define _OMAP_TIMER_TICK_NEG_OFFSET 0x4c /* TNIR, 34xx only */
83#define _OMAP_TIMER_TICK_COUNT_OFFSET 0x50 /* TCVR, 34xx only */
84#define _OMAP_TIMER_TICK_INT_MASK_SET_OFFSET 0x54 /* TOCR, 34xx only */
85#define _OMAP_TIMER_TICK_INT_MASK_COUNT_OFFSET 0x58 /* TOWR, 34xx only */
Tony Lindgren92105bb2005-09-07 17:20:26 +010086
Richard Woodruff0f0d0802008-07-03 12:24:30 +030087/* register offsets with the write pending bit encoded */
88#define WPSHIFT 16
89
90#define OMAP_TIMER_ID_REG (_OMAP_TIMER_ID_OFFSET \
91 | (WP_NONE << WPSHIFT))
92
93#define OMAP_TIMER_OCP_CFG_REG (_OMAP_TIMER_OCP_CFG_OFFSET \
94 | (WP_NONE << WPSHIFT))
95
96#define OMAP_TIMER_SYS_STAT_REG (_OMAP_TIMER_SYS_STAT_OFFSET \
97 | (WP_NONE << WPSHIFT))
98
99#define OMAP_TIMER_STAT_REG (_OMAP_TIMER_STAT_OFFSET \
100 | (WP_NONE << WPSHIFT))
101
102#define OMAP_TIMER_INT_EN_REG (_OMAP_TIMER_INT_EN_OFFSET \
103 | (WP_NONE << WPSHIFT))
104
105#define OMAP_TIMER_WAKEUP_EN_REG (_OMAP_TIMER_WAKEUP_EN_OFFSET \
106 | (WP_NONE << WPSHIFT))
107
108#define OMAP_TIMER_CTRL_REG (_OMAP_TIMER_CTRL_OFFSET \
109 | (WP_TCLR << WPSHIFT))
110
111#define OMAP_TIMER_COUNTER_REG (_OMAP_TIMER_COUNTER_OFFSET \
112 | (WP_TCRR << WPSHIFT))
113
114#define OMAP_TIMER_LOAD_REG (_OMAP_TIMER_LOAD_OFFSET \
115 | (WP_TLDR << WPSHIFT))
116
117#define OMAP_TIMER_TRIGGER_REG (_OMAP_TIMER_TRIGGER_OFFSET \
118 | (WP_TTGR << WPSHIFT))
119
120#define OMAP_TIMER_WRITE_PEND_REG (_OMAP_TIMER_WRITE_PEND_OFFSET \
121 | (WP_NONE << WPSHIFT))
122
123#define OMAP_TIMER_MATCH_REG (_OMAP_TIMER_MATCH_OFFSET \
124 | (WP_TMAR << WPSHIFT))
125
126#define OMAP_TIMER_CAPTURE_REG (_OMAP_TIMER_CAPTURE_OFFSET \
127 | (WP_NONE << WPSHIFT))
128
129#define OMAP_TIMER_IF_CTRL_REG (_OMAP_TIMER_IF_CTRL_OFFSET \
130 | (WP_NONE << WPSHIFT))
131
132#define OMAP_TIMER_CAPTURE2_REG (_OMAP_TIMER_CAPTURE2_OFFSET \
133 | (WP_NONE << WPSHIFT))
134
135#define OMAP_TIMER_TICK_POS_REG (_OMAP_TIMER_TICK_POS_OFFSET \
136 | (WP_TPIR << WPSHIFT))
137
138#define OMAP_TIMER_TICK_NEG_REG (_OMAP_TIMER_TICK_NEG_OFFSET \
139 | (WP_TNIR << WPSHIFT))
140
141#define OMAP_TIMER_TICK_COUNT_REG (_OMAP_TIMER_TICK_COUNT_OFFSET \
142 | (WP_TCVR << WPSHIFT))
143
144#define OMAP_TIMER_TICK_INT_MASK_SET_REG \
145 (_OMAP_TIMER_TICK_INT_MASK_SET_OFFSET | (WP_TOCR << WPSHIFT))
146
147#define OMAP_TIMER_TICK_INT_MASK_COUNT_REG \
148 (_OMAP_TIMER_TICK_INT_MASK_COUNT_OFFSET | (WP_TOWR << WPSHIFT))
Tony Lindgren92105bb2005-09-07 17:20:26 +0100149
Timo Teras77900a22006-06-26 16:16:12 -0700150struct omap_dm_timer {
151 unsigned long phys_base;
152 int irq;
Syed Mohammed, Khasimce2df9c2007-06-25 22:55:39 -0700153#if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
Timo Teras77900a22006-06-26 16:16:12 -0700154 struct clk *iclk, *fclk;
155#endif
156 void __iomem *io_base;
157 unsigned reserved:1;
Timo Teras12583a72006-09-25 12:41:42 +0300158 unsigned enabled:1;
Richard Woodruff0f0d0802008-07-03 12:24:30 +0300159 unsigned posted:1;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100160};
161
Timo Teras77900a22006-06-26 16:16:12 -0700162#ifdef CONFIG_ARCH_OMAP1
163
Timo Terasfa4bb622006-09-25 12:41:35 +0300164#define omap_dm_clk_enable(x)
165#define omap_dm_clk_disable(x)
Syed Mohammed, Khasim471b3aa2007-06-21 21:48:07 -0700166#define omap2_dm_timers NULL
167#define omap2_dm_source_names NULL
168#define omap2_dm_source_clocks NULL
Syed Mohammed, Khasimce2df9c2007-06-25 22:55:39 -0700169#define omap3_dm_timers NULL
170#define omap3_dm_source_names NULL
171#define omap3_dm_source_clocks NULL
Timo Terasfa4bb622006-09-25 12:41:35 +0300172
Syed Mohammed, Khasim471b3aa2007-06-21 21:48:07 -0700173static struct omap_dm_timer omap1_dm_timers[] = {
Timo Teras77900a22006-06-26 16:16:12 -0700174 { .phys_base = 0xfffb1400, .irq = INT_1610_GPTIMER1 },
175 { .phys_base = 0xfffb1c00, .irq = INT_1610_GPTIMER2 },
176 { .phys_base = 0xfffb2400, .irq = INT_1610_GPTIMER3 },
177 { .phys_base = 0xfffb2c00, .irq = INT_1610_GPTIMER4 },
178 { .phys_base = 0xfffb3400, .irq = INT_1610_GPTIMER5 },
179 { .phys_base = 0xfffb3c00, .irq = INT_1610_GPTIMER6 },
Matthew Percival53037f42007-01-25 16:24:29 -0800180 { .phys_base = 0xfffb7400, .irq = INT_1610_GPTIMER7 },
181 { .phys_base = 0xfffbd400, .irq = INT_1610_GPTIMER8 },
Timo Teras77900a22006-06-26 16:16:12 -0700182};
183
Syed Mohammed, Khasim471b3aa2007-06-21 21:48:07 -0700184static const int dm_timer_count = ARRAY_SIZE(omap1_dm_timers);
185
Timo Teras77900a22006-06-26 16:16:12 -0700186#elif defined(CONFIG_ARCH_OMAP2)
187
Syed Mohammed, Khasim471b3aa2007-06-21 21:48:07 -0700188#define omap_dm_clk_enable(x) clk_enable(x)
189#define omap_dm_clk_disable(x) clk_disable(x)
190#define omap1_dm_timers NULL
Syed Mohammed, Khasimce2df9c2007-06-25 22:55:39 -0700191#define omap3_dm_timers NULL
192#define omap3_dm_source_names NULL
193#define omap3_dm_source_clocks NULL
Timo Terasfa4bb622006-09-25 12:41:35 +0300194
Syed Mohammed, Khasim471b3aa2007-06-21 21:48:07 -0700195static struct omap_dm_timer omap2_dm_timers[] = {
Timo Teras77900a22006-06-26 16:16:12 -0700196 { .phys_base = 0x48028000, .irq = INT_24XX_GPTIMER1 },
197 { .phys_base = 0x4802a000, .irq = INT_24XX_GPTIMER2 },
198 { .phys_base = 0x48078000, .irq = INT_24XX_GPTIMER3 },
199 { .phys_base = 0x4807a000, .irq = INT_24XX_GPTIMER4 },
200 { .phys_base = 0x4807c000, .irq = INT_24XX_GPTIMER5 },
201 { .phys_base = 0x4807e000, .irq = INT_24XX_GPTIMER6 },
202 { .phys_base = 0x48080000, .irq = INT_24XX_GPTIMER7 },
203 { .phys_base = 0x48082000, .irq = INT_24XX_GPTIMER8 },
204 { .phys_base = 0x48084000, .irq = INT_24XX_GPTIMER9 },
205 { .phys_base = 0x48086000, .irq = INT_24XX_GPTIMER10 },
206 { .phys_base = 0x48088000, .irq = INT_24XX_GPTIMER11 },
207 { .phys_base = 0x4808a000, .irq = INT_24XX_GPTIMER12 },
208};
209
Syed Mohammed, Khasim471b3aa2007-06-21 21:48:07 -0700210static const char *omap2_dm_source_names[] __initdata = {
Timo Teras83379c82006-06-26 16:16:23 -0700211 "sys_ck",
212 "func_32k_ck",
Syed Mohammed, Khasim471b3aa2007-06-21 21:48:07 -0700213 "alt_ck",
214 NULL
Timo Teras83379c82006-06-26 16:16:23 -0700215};
216
Syed Mohammed, Khasim471b3aa2007-06-21 21:48:07 -0700217static struct clk **omap2_dm_source_clocks[3];
218static const int dm_timer_count = ARRAY_SIZE(omap2_dm_timers);
Timo Teras83379c82006-06-26 16:16:23 -0700219
Syed Mohammed, Khasimce2df9c2007-06-25 22:55:39 -0700220#elif defined(CONFIG_ARCH_OMAP3)
221
222#define omap_dm_clk_enable(x) clk_enable(x)
223#define omap_dm_clk_disable(x) clk_disable(x)
224#define omap1_dm_timers NULL
225#define omap2_dm_timers NULL
226#define omap2_dm_source_names NULL
227#define omap2_dm_source_clocks NULL
228
229static struct omap_dm_timer omap3_dm_timers[] = {
230 { .phys_base = 0x48318000, .irq = INT_24XX_GPTIMER1 },
231 { .phys_base = 0x49032000, .irq = INT_24XX_GPTIMER2 },
232 { .phys_base = 0x49034000, .irq = INT_24XX_GPTIMER3 },
233 { .phys_base = 0x49036000, .irq = INT_24XX_GPTIMER4 },
234 { .phys_base = 0x49038000, .irq = INT_24XX_GPTIMER5 },
235 { .phys_base = 0x4903A000, .irq = INT_24XX_GPTIMER6 },
236 { .phys_base = 0x4903C000, .irq = INT_24XX_GPTIMER7 },
237 { .phys_base = 0x4903E000, .irq = INT_24XX_GPTIMER8 },
238 { .phys_base = 0x49040000, .irq = INT_24XX_GPTIMER9 },
239 { .phys_base = 0x48086000, .irq = INT_24XX_GPTIMER10 },
240 { .phys_base = 0x48088000, .irq = INT_24XX_GPTIMER11 },
241 { .phys_base = 0x48304000, .irq = INT_24XX_GPTIMER12 },
242};
243
244static const char *omap3_dm_source_names[] __initdata = {
245 "sys_ck",
246 "omap_32k_fck",
247 NULL
248};
249
250static struct clk **omap3_dm_source_clocks[2];
251static const int dm_timer_count = ARRAY_SIZE(omap3_dm_timers);
252
Timo Teras77900a22006-06-26 16:16:12 -0700253#else
254
255#error OMAP architecture not supported!
256
257#endif
258
Syed Mohammed, Khasim471b3aa2007-06-21 21:48:07 -0700259static struct omap_dm_timer *dm_timers;
260static char **dm_source_names;
261static struct clk **dm_source_clocks;
262
Tony Lindgren92105bb2005-09-07 17:20:26 +0100263static spinlock_t dm_timer_lock;
264
Richard Woodruff0f0d0802008-07-03 12:24:30 +0300265/*
266 * Reads timer registers in posted and non-posted mode. The posted mode bit
267 * is encoded in reg. Note that in posted mode write pending bit must be
268 * checked. Otherwise a read of a non completed write will produce an error.
269 */
270static inline u32 omap_dm_timer_read_reg(struct omap_dm_timer *timer, u32 reg)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100271{
Richard Woodruff0f0d0802008-07-03 12:24:30 +0300272 if (timer->posted)
273 while (readl(timer->io_base + (OMAP_TIMER_WRITE_PEND_REG & 0xff))
274 & (reg >> WPSHIFT))
275 cpu_relax();
276 return readl(timer->io_base + (reg & 0xff));
Timo Teras77900a22006-06-26 16:16:12 -0700277}
278
Richard Woodruff0f0d0802008-07-03 12:24:30 +0300279/*
280 * Writes timer registers in posted and non-posted mode. The posted mode bit
281 * is encoded in reg. Note that in posted mode the write pending bit must be
282 * checked. Otherwise a write on a register which has a pending write will be
283 * lost.
284 */
285static void omap_dm_timer_write_reg(struct omap_dm_timer *timer, u32 reg,
286 u32 value)
Timo Teras77900a22006-06-26 16:16:12 -0700287{
Richard Woodruff0f0d0802008-07-03 12:24:30 +0300288 if (timer->posted)
289 while (readl(timer->io_base + (OMAP_TIMER_WRITE_PEND_REG & 0xff))
290 & (reg >> WPSHIFT))
291 cpu_relax();
292 writel(value, timer->io_base + (reg & 0xff));
Tony Lindgren92105bb2005-09-07 17:20:26 +0100293}
294
Timo Teras77900a22006-06-26 16:16:12 -0700295static void omap_dm_timer_wait_for_reset(struct omap_dm_timer *timer)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100296{
Timo Teras77900a22006-06-26 16:16:12 -0700297 int c;
298
299 c = 0;
300 while (!(omap_dm_timer_read_reg(timer, OMAP_TIMER_SYS_STAT_REG) & 1)) {
301 c++;
302 if (c > 100000) {
303 printk(KERN_ERR "Timer failed to reset\n");
304 return;
305 }
306 }
Tony Lindgren92105bb2005-09-07 17:20:26 +0100307}
308
Timo Teras77900a22006-06-26 16:16:12 -0700309static void omap_dm_timer_reset(struct omap_dm_timer *timer)
310{
311 u32 l;
312
Juha Yrjola39020842006-09-25 12:41:44 +0300313 if (!cpu_class_is_omap2() || timer != &dm_timers[0]) {
Timo Terase32f7ec2006-06-26 16:16:13 -0700314 omap_dm_timer_write_reg(timer, OMAP_TIMER_IF_CTRL_REG, 0x06);
315 omap_dm_timer_wait_for_reset(timer);
316 }
Timo Teras12583a72006-09-25 12:41:42 +0300317 omap_dm_timer_set_source(timer, OMAP_TIMER_SRC_32_KHZ);
Timo Teras77900a22006-06-26 16:16:12 -0700318
Timo Teras77900a22006-06-26 16:16:12 -0700319 l = omap_dm_timer_read_reg(timer, OMAP_TIMER_OCP_CFG_REG);
Richard Woodruff0f0d0802008-07-03 12:24:30 +0300320 l |= 0x02 << 3; /* Set to smart-idle mode */
321 l |= 0x2 << 8; /* Set clock activity to perserve f-clock on idle */
Juha Yrjola39020842006-09-25 12:41:44 +0300322
Richard Woodruff0f0d0802008-07-03 12:24:30 +0300323 /*
324 * Enable wake-up only for GPT1 on OMAP2 CPUs.
325 * FIXME: All timers should have wake-up enabled and clear
326 * PRCM status.
327 */
328 if (cpu_class_is_omap2() && (timer == &dm_timers[0]))
Juha Yrjola39020842006-09-25 12:41:44 +0300329 l |= 1 << 2;
Timo Teras77900a22006-06-26 16:16:12 -0700330 omap_dm_timer_write_reg(timer, OMAP_TIMER_OCP_CFG_REG, l);
Richard Woodruff0f0d0802008-07-03 12:24:30 +0300331
332 /* Match hardware reset default of posted mode */
333 omap_dm_timer_write_reg(timer, OMAP_TIMER_IF_CTRL_REG,
334 OMAP_TIMER_CTRL_POSTED);
335 timer->posted = 1;
Timo Teras77900a22006-06-26 16:16:12 -0700336}
337
Timo Teras83379c82006-06-26 16:16:23 -0700338static void omap_dm_timer_prepare(struct omap_dm_timer *timer)
Timo Teras77900a22006-06-26 16:16:12 -0700339{
Timo Teras12583a72006-09-25 12:41:42 +0300340 omap_dm_timer_enable(timer);
Timo Teras77900a22006-06-26 16:16:12 -0700341 omap_dm_timer_reset(timer);
342}
343
344struct omap_dm_timer *omap_dm_timer_request(void)
345{
346 struct omap_dm_timer *timer = NULL;
347 unsigned long flags;
348 int i;
349
350 spin_lock_irqsave(&dm_timer_lock, flags);
351 for (i = 0; i < dm_timer_count; i++) {
352 if (dm_timers[i].reserved)
353 continue;
354
355 timer = &dm_timers[i];
Timo Teras83379c82006-06-26 16:16:23 -0700356 timer->reserved = 1;
Timo Teras77900a22006-06-26 16:16:12 -0700357 break;
358 }
359 spin_unlock_irqrestore(&dm_timer_lock, flags);
360
Timo Teras83379c82006-06-26 16:16:23 -0700361 if (timer != NULL)
362 omap_dm_timer_prepare(timer);
363
Timo Teras77900a22006-06-26 16:16:12 -0700364 return timer;
365}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700366EXPORT_SYMBOL_GPL(omap_dm_timer_request);
Timo Teras77900a22006-06-26 16:16:12 -0700367
368struct omap_dm_timer *omap_dm_timer_request_specific(int id)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100369{
370 struct omap_dm_timer *timer;
Timo Teras77900a22006-06-26 16:16:12 -0700371 unsigned long flags;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100372
Timo Teras77900a22006-06-26 16:16:12 -0700373 spin_lock_irqsave(&dm_timer_lock, flags);
374 if (id <= 0 || id > dm_timer_count || dm_timers[id-1].reserved) {
375 spin_unlock_irqrestore(&dm_timer_lock, flags);
376 printk("BUG: warning at %s:%d/%s(): unable to get timer %d\n",
Harvey Harrison8e86f422008-03-04 15:08:02 -0800377 __FILE__, __LINE__, __func__, id);
Timo Teras77900a22006-06-26 16:16:12 -0700378 dump_stack();
379 return NULL;
380 }
Tony Lindgren92105bb2005-09-07 17:20:26 +0100381
Timo Teras77900a22006-06-26 16:16:12 -0700382 timer = &dm_timers[id-1];
Timo Teras83379c82006-06-26 16:16:23 -0700383 timer->reserved = 1;
Timo Teras77900a22006-06-26 16:16:12 -0700384 spin_unlock_irqrestore(&dm_timer_lock, flags);
385
Timo Teras83379c82006-06-26 16:16:23 -0700386 omap_dm_timer_prepare(timer);
387
Timo Teras77900a22006-06-26 16:16:12 -0700388 return timer;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100389}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700390EXPORT_SYMBOL_GPL(omap_dm_timer_request_specific);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100391
Timo Teras77900a22006-06-26 16:16:12 -0700392void omap_dm_timer_free(struct omap_dm_timer *timer)
393{
Timo Teras12583a72006-09-25 12:41:42 +0300394 omap_dm_timer_enable(timer);
Timo Teras77900a22006-06-26 16:16:12 -0700395 omap_dm_timer_reset(timer);
Timo Teras12583a72006-09-25 12:41:42 +0300396 omap_dm_timer_disable(timer);
Timo Terasfa4bb622006-09-25 12:41:35 +0300397
Timo Teras77900a22006-06-26 16:16:12 -0700398 WARN_ON(!timer->reserved);
399 timer->reserved = 0;
400}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700401EXPORT_SYMBOL_GPL(omap_dm_timer_free);
Timo Teras77900a22006-06-26 16:16:12 -0700402
Timo Teras12583a72006-09-25 12:41:42 +0300403void omap_dm_timer_enable(struct omap_dm_timer *timer)
404{
405 if (timer->enabled)
406 return;
407
408 omap_dm_clk_enable(timer->fclk);
409 omap_dm_clk_enable(timer->iclk);
410
411 timer->enabled = 1;
412}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700413EXPORT_SYMBOL_GPL(omap_dm_timer_enable);
Timo Teras12583a72006-09-25 12:41:42 +0300414
415void omap_dm_timer_disable(struct omap_dm_timer *timer)
416{
417 if (!timer->enabled)
418 return;
419
420 omap_dm_clk_disable(timer->iclk);
421 omap_dm_clk_disable(timer->fclk);
422
423 timer->enabled = 0;
424}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700425EXPORT_SYMBOL_GPL(omap_dm_timer_disable);
Timo Teras12583a72006-09-25 12:41:42 +0300426
Timo Teras77900a22006-06-26 16:16:12 -0700427int omap_dm_timer_get_irq(struct omap_dm_timer *timer)
428{
429 return timer->irq;
430}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700431EXPORT_SYMBOL_GPL(omap_dm_timer_get_irq);
Timo Teras77900a22006-06-26 16:16:12 -0700432
433#if defined(CONFIG_ARCH_OMAP1)
434
Tony Lindgrena569c6e2006-04-02 17:46:21 +0100435/**
436 * omap_dm_timer_modify_idlect_mask - Check if any running timers use ARMXOR
437 * @inputmask: current value of idlect mask
438 */
439__u32 omap_dm_timer_modify_idlect_mask(__u32 inputmask)
440{
Timo Teras77900a22006-06-26 16:16:12 -0700441 int i;
Tony Lindgrena569c6e2006-04-02 17:46:21 +0100442
443 /* If ARMXOR cannot be idled this function call is unnecessary */
444 if (!(inputmask & (1 << 1)))
445 return inputmask;
446
447 /* If any active timer is using ARMXOR return modified mask */
Timo Teras77900a22006-06-26 16:16:12 -0700448 for (i = 0; i < dm_timer_count; i++) {
449 u32 l;
450
Tony Lindgren35912c72006-07-01 19:56:42 +0100451 l = omap_dm_timer_read_reg(&dm_timers[i], OMAP_TIMER_CTRL_REG);
Timo Teras77900a22006-06-26 16:16:12 -0700452 if (l & OMAP_TIMER_CTRL_ST) {
453 if (((omap_readl(MOD_CONF_CTRL_1) >> (i * 2)) & 0x03) == 0)
Tony Lindgrena569c6e2006-04-02 17:46:21 +0100454 inputmask &= ~(1 << 1);
455 else
456 inputmask &= ~(1 << 2);
457 }
Timo Teras77900a22006-06-26 16:16:12 -0700458 }
Tony Lindgrena569c6e2006-04-02 17:46:21 +0100459
460 return inputmask;
461}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700462EXPORT_SYMBOL_GPL(omap_dm_timer_modify_idlect_mask);
Tony Lindgrena569c6e2006-04-02 17:46:21 +0100463
Syed Mohammed, Khasimce2df9c2007-06-25 22:55:39 -0700464#elif defined(CONFIG_ARCH_OMAP2) || defined (CONFIG_ARCH_OMAP3)
Timo Teras77900a22006-06-26 16:16:12 -0700465
466struct clk *omap_dm_timer_get_fclk(struct omap_dm_timer *timer)
467{
Timo Terasfa4bb622006-09-25 12:41:35 +0300468 return timer->fclk;
Timo Teras77900a22006-06-26 16:16:12 -0700469}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700470EXPORT_SYMBOL_GPL(omap_dm_timer_get_fclk);
Timo Teras77900a22006-06-26 16:16:12 -0700471
472__u32 omap_dm_timer_modify_idlect_mask(__u32 inputmask)
473{
474 BUG();
Dirk Behme21218802006-12-06 17:14:00 -0800475
476 return 0;
Timo Teras77900a22006-06-26 16:16:12 -0700477}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700478EXPORT_SYMBOL_GPL(omap_dm_timer_modify_idlect_mask);
Timo Teras77900a22006-06-26 16:16:12 -0700479
480#endif
481
482void omap_dm_timer_trigger(struct omap_dm_timer *timer)
483{
484 omap_dm_timer_write_reg(timer, OMAP_TIMER_TRIGGER_REG, 0);
485}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700486EXPORT_SYMBOL_GPL(omap_dm_timer_trigger);
Timo Teras77900a22006-06-26 16:16:12 -0700487
488void omap_dm_timer_start(struct omap_dm_timer *timer)
489{
490 u32 l;
491
492 l = omap_dm_timer_read_reg(timer, OMAP_TIMER_CTRL_REG);
493 if (!(l & OMAP_TIMER_CTRL_ST)) {
494 l |= OMAP_TIMER_CTRL_ST;
495 omap_dm_timer_write_reg(timer, OMAP_TIMER_CTRL_REG, l);
496 }
497}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700498EXPORT_SYMBOL_GPL(omap_dm_timer_start);
Timo Teras77900a22006-06-26 16:16:12 -0700499
500void omap_dm_timer_stop(struct omap_dm_timer *timer)
501{
502 u32 l;
503
504 l = omap_dm_timer_read_reg(timer, OMAP_TIMER_CTRL_REG);
505 if (l & OMAP_TIMER_CTRL_ST) {
506 l &= ~0x1;
507 omap_dm_timer_write_reg(timer, OMAP_TIMER_CTRL_REG, l);
508 }
509}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700510EXPORT_SYMBOL_GPL(omap_dm_timer_stop);
Timo Teras77900a22006-06-26 16:16:12 -0700511
512#ifdef CONFIG_ARCH_OMAP1
Tony Lindgrena569c6e2006-04-02 17:46:21 +0100513
Tony Lindgren92105bb2005-09-07 17:20:26 +0100514void omap_dm_timer_set_source(struct omap_dm_timer *timer, int source)
515{
516 int n = (timer - dm_timers) << 1;
517 u32 l;
518
519 l = omap_readl(MOD_CONF_CTRL_1) & ~(0x03 << n);
520 l |= source << n;
521 omap_writel(l, MOD_CONF_CTRL_1);
522}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700523EXPORT_SYMBOL_GPL(omap_dm_timer_set_source);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100524
Timo Teras77900a22006-06-26 16:16:12 -0700525#else
Tony Lindgren92105bb2005-09-07 17:20:26 +0100526
Timo Teras77900a22006-06-26 16:16:12 -0700527void omap_dm_timer_set_source(struct omap_dm_timer *timer, int source)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100528{
Timo Teras77900a22006-06-26 16:16:12 -0700529 if (source < 0 || source >= 3)
530 return;
531
Timo Teras77900a22006-06-26 16:16:12 -0700532 clk_disable(timer->fclk);
Timo Teras83379c82006-06-26 16:16:23 -0700533 clk_set_parent(timer->fclk, dm_source_clocks[source]);
Timo Teras77900a22006-06-26 16:16:12 -0700534 clk_enable(timer->fclk);
Timo Teras77900a22006-06-26 16:16:12 -0700535
536 /* When the functional clock disappears, too quick writes seem to
537 * cause an abort. */
Tony Lindgrenc40fae952006-12-07 13:58:10 -0800538 __delay(150000);
Timo Teras77900a22006-06-26 16:16:12 -0700539}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700540EXPORT_SYMBOL_GPL(omap_dm_timer_set_source);
Timo Teras77900a22006-06-26 16:16:12 -0700541
542#endif
543
544void omap_dm_timer_set_load(struct omap_dm_timer *timer, int autoreload,
545 unsigned int load)
546{
547 u32 l;
548
549 l = omap_dm_timer_read_reg(timer, OMAP_TIMER_CTRL_REG);
550 if (autoreload)
551 l |= OMAP_TIMER_CTRL_AR;
552 else
553 l &= ~OMAP_TIMER_CTRL_AR;
554 omap_dm_timer_write_reg(timer, OMAP_TIMER_CTRL_REG, l);
555 omap_dm_timer_write_reg(timer, OMAP_TIMER_LOAD_REG, load);
Richard Woodruff0f0d0802008-07-03 12:24:30 +0300556
Timo Teras77900a22006-06-26 16:16:12 -0700557 omap_dm_timer_write_reg(timer, OMAP_TIMER_TRIGGER_REG, 0);
558}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700559EXPORT_SYMBOL_GPL(omap_dm_timer_set_load);
Timo Teras77900a22006-06-26 16:16:12 -0700560
Richard Woodruff3fddd092008-07-03 12:24:30 +0300561/* Optimized set_load which removes costly spin wait in timer_start */
562void omap_dm_timer_set_load_start(struct omap_dm_timer *timer, int autoreload,
563 unsigned int load)
564{
565 u32 l;
566
567 l = omap_dm_timer_read_reg(timer, OMAP_TIMER_CTRL_REG);
Paul Walmsley64ce2902008-12-10 17:36:34 -0800568 if (autoreload) {
Richard Woodruff3fddd092008-07-03 12:24:30 +0300569 l |= OMAP_TIMER_CTRL_AR;
Paul Walmsley64ce2902008-12-10 17:36:34 -0800570 omap_dm_timer_write_reg(timer, OMAP_TIMER_LOAD_REG, load);
571 } else {
Richard Woodruff3fddd092008-07-03 12:24:30 +0300572 l &= ~OMAP_TIMER_CTRL_AR;
Paul Walmsley64ce2902008-12-10 17:36:34 -0800573 }
Richard Woodruff3fddd092008-07-03 12:24:30 +0300574 l |= OMAP_TIMER_CTRL_ST;
575
576 omap_dm_timer_write_reg(timer, OMAP_TIMER_COUNTER_REG, load);
Richard Woodruff3fddd092008-07-03 12:24:30 +0300577 omap_dm_timer_write_reg(timer, OMAP_TIMER_CTRL_REG, l);
578}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700579EXPORT_SYMBOL_GPL(omap_dm_timer_set_load_start);
Richard Woodruff3fddd092008-07-03 12:24:30 +0300580
Timo Teras77900a22006-06-26 16:16:12 -0700581void omap_dm_timer_set_match(struct omap_dm_timer *timer, int enable,
582 unsigned int match)
583{
584 u32 l;
585
586 l = omap_dm_timer_read_reg(timer, OMAP_TIMER_CTRL_REG);
Timo Teras83379c82006-06-26 16:16:23 -0700587 if (enable)
Timo Teras77900a22006-06-26 16:16:12 -0700588 l |= OMAP_TIMER_CTRL_CE;
589 else
590 l &= ~OMAP_TIMER_CTRL_CE;
591 omap_dm_timer_write_reg(timer, OMAP_TIMER_CTRL_REG, l);
592 omap_dm_timer_write_reg(timer, OMAP_TIMER_MATCH_REG, match);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100593}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700594EXPORT_SYMBOL_GPL(omap_dm_timer_set_match);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100595
Timo Teras77900a22006-06-26 16:16:12 -0700596void omap_dm_timer_set_pwm(struct omap_dm_timer *timer, int def_on,
597 int toggle, int trigger)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100598{
Timo Teras77900a22006-06-26 16:16:12 -0700599 u32 l;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100600
Timo Teras77900a22006-06-26 16:16:12 -0700601 l = omap_dm_timer_read_reg(timer, OMAP_TIMER_CTRL_REG);
602 l &= ~(OMAP_TIMER_CTRL_GPOCFG | OMAP_TIMER_CTRL_SCPWM |
603 OMAP_TIMER_CTRL_PT | (0x03 << 10));
604 if (def_on)
605 l |= OMAP_TIMER_CTRL_SCPWM;
606 if (toggle)
607 l |= OMAP_TIMER_CTRL_PT;
608 l |= trigger << 10;
609 omap_dm_timer_write_reg(timer, OMAP_TIMER_CTRL_REG, l);
610}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700611EXPORT_SYMBOL_GPL(omap_dm_timer_set_pwm);
Timo Teras77900a22006-06-26 16:16:12 -0700612
613void omap_dm_timer_set_prescaler(struct omap_dm_timer *timer, int prescaler)
614{
615 u32 l;
616
617 l = omap_dm_timer_read_reg(timer, OMAP_TIMER_CTRL_REG);
618 l &= ~(OMAP_TIMER_CTRL_PRE | (0x07 << 2));
619 if (prescaler >= 0x00 && prescaler <= 0x07) {
620 l |= OMAP_TIMER_CTRL_PRE;
621 l |= prescaler << 2;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100622 }
Timo Teras77900a22006-06-26 16:16:12 -0700623 omap_dm_timer_write_reg(timer, OMAP_TIMER_CTRL_REG, l);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100624}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700625EXPORT_SYMBOL_GPL(omap_dm_timer_set_prescaler);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100626
627void omap_dm_timer_set_int_enable(struct omap_dm_timer *timer,
Timo Teras77900a22006-06-26 16:16:12 -0700628 unsigned int value)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100629{
630 omap_dm_timer_write_reg(timer, OMAP_TIMER_INT_EN_REG, value);
Juha Yrjola39020842006-09-25 12:41:44 +0300631 omap_dm_timer_write_reg(timer, OMAP_TIMER_WAKEUP_EN_REG, value);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100632}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700633EXPORT_SYMBOL_GPL(omap_dm_timer_set_int_enable);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100634
635unsigned int omap_dm_timer_read_status(struct omap_dm_timer *timer)
636{
Timo Terasfa4bb622006-09-25 12:41:35 +0300637 unsigned int l;
638
Timo Terasfa4bb622006-09-25 12:41:35 +0300639 l = omap_dm_timer_read_reg(timer, OMAP_TIMER_STAT_REG);
Timo Terasfa4bb622006-09-25 12:41:35 +0300640
641 return l;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100642}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700643EXPORT_SYMBOL_GPL(omap_dm_timer_read_status);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100644
645void omap_dm_timer_write_status(struct omap_dm_timer *timer, unsigned int value)
646{
647 omap_dm_timer_write_reg(timer, OMAP_TIMER_STAT_REG, value);
648}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700649EXPORT_SYMBOL_GPL(omap_dm_timer_write_status);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100650
Tony Lindgren92105bb2005-09-07 17:20:26 +0100651unsigned int omap_dm_timer_read_counter(struct omap_dm_timer *timer)
652{
Timo Terasfa4bb622006-09-25 12:41:35 +0300653 unsigned int l;
654
Timo Terasfa4bb622006-09-25 12:41:35 +0300655 l = omap_dm_timer_read_reg(timer, OMAP_TIMER_COUNTER_REG);
Timo Terasfa4bb622006-09-25 12:41:35 +0300656
657 return l;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100658}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700659EXPORT_SYMBOL_GPL(omap_dm_timer_read_counter);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100660
Timo Teras83379c82006-06-26 16:16:23 -0700661void omap_dm_timer_write_counter(struct omap_dm_timer *timer, unsigned int value)
662{
Timo Terasfa4bb622006-09-25 12:41:35 +0300663 omap_dm_timer_write_reg(timer, OMAP_TIMER_COUNTER_REG, value);
Timo Teras83379c82006-06-26 16:16:23 -0700664}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700665EXPORT_SYMBOL_GPL(omap_dm_timer_write_counter);
Timo Teras83379c82006-06-26 16:16:23 -0700666
Timo Teras77900a22006-06-26 16:16:12 -0700667int omap_dm_timers_active(void)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100668{
Timo Teras77900a22006-06-26 16:16:12 -0700669 int i;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100670
Timo Teras77900a22006-06-26 16:16:12 -0700671 for (i = 0; i < dm_timer_count; i++) {
672 struct omap_dm_timer *timer;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100673
Timo Teras77900a22006-06-26 16:16:12 -0700674 timer = &dm_timers[i];
Timo Teras12583a72006-09-25 12:41:42 +0300675
676 if (!timer->enabled)
677 continue;
678
Timo Teras77900a22006-06-26 16:16:12 -0700679 if (omap_dm_timer_read_reg(timer, OMAP_TIMER_CTRL_REG) &
Timo Terasfa4bb622006-09-25 12:41:35 +0300680 OMAP_TIMER_CTRL_ST) {
Timo Teras77900a22006-06-26 16:16:12 -0700681 return 1;
Timo Terasfa4bb622006-09-25 12:41:35 +0300682 }
Tony Lindgren92105bb2005-09-07 17:20:26 +0100683 }
Tony Lindgren92105bb2005-09-07 17:20:26 +0100684 return 0;
685}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700686EXPORT_SYMBOL_GPL(omap_dm_timers_active);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100687
Syed Mohammed, Khasim471b3aa2007-06-21 21:48:07 -0700688int __init omap_dm_timer_init(void)
Timo Teras77900a22006-06-26 16:16:12 -0700689{
690 struct omap_dm_timer *timer;
691 int i;
692
Syed Mohammed, Khasimce2df9c2007-06-25 22:55:39 -0700693 if (!(cpu_is_omap16xx() || cpu_class_is_omap2()))
Timo Teras77900a22006-06-26 16:16:12 -0700694 return -ENODEV;
695
696 spin_lock_init(&dm_timer_lock);
Syed Mohammed, Khasim471b3aa2007-06-21 21:48:07 -0700697
698 if (cpu_class_is_omap1())
699 dm_timers = omap1_dm_timers;
700 else if (cpu_is_omap24xx()) {
701 dm_timers = omap2_dm_timers;
702 dm_source_names = (char **)omap2_dm_source_names;
703 dm_source_clocks = (struct clk **)omap2_dm_source_clocks;
Syed Mohammed, Khasimce2df9c2007-06-25 22:55:39 -0700704 } else if (cpu_is_omap34xx()) {
705 dm_timers = omap3_dm_timers;
706 dm_source_names = (char **)omap3_dm_source_names;
707 dm_source_clocks = (struct clk **)omap3_dm_source_clocks;
Timo Teras83379c82006-06-26 16:16:23 -0700708 }
Syed Mohammed, Khasim471b3aa2007-06-21 21:48:07 -0700709
710 if (cpu_class_is_omap2())
711 for (i = 0; dm_source_names[i] != NULL; i++)
712 dm_source_clocks[i] = clk_get(NULL, dm_source_names[i]);
713
Syed Mohammed Khasim56a25642006-12-06 17:14:08 -0800714 if (cpu_is_omap243x())
715 dm_timers[0].phys_base = 0x49018000;
Timo Teras83379c82006-06-26 16:16:23 -0700716
Timo Teras77900a22006-06-26 16:16:12 -0700717 for (i = 0; i < dm_timer_count; i++) {
Timo Teras77900a22006-06-26 16:16:12 -0700718 timer = &dm_timers[i];
Russell Kinge8a91c92008-09-01 22:07:37 +0100719 timer->io_base = IO_ADDRESS(timer->phys_base);
Syed Mohammed, Khasimce2df9c2007-06-25 22:55:39 -0700720#if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
Syed Mohammed, Khasim471b3aa2007-06-21 21:48:07 -0700721 if (cpu_class_is_omap2()) {
722 char clk_name[16];
723 sprintf(clk_name, "gpt%d_ick", i + 1);
724 timer->iclk = clk_get(NULL, clk_name);
725 sprintf(clk_name, "gpt%d_fck", i + 1);
726 timer->fclk = clk_get(NULL, clk_name);
727 }
Timo Teras77900a22006-06-26 16:16:12 -0700728#endif
729 }
730
731 return 0;
732}