blob: c588cdf35db1d3e9004c667861535cbc6a78247a [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 *
Santosh Shilimkar44169072009-05-28 14:16:04 -070010 * Copyright (C) 2009 Texas Instruments
11 * Added OMAP4 support - Santosh Shilimkar <santosh.shilimkar@ti.com>
12 *
Tony Lindgren92105bb2005-09-07 17:20:26 +010013 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License as published by the
15 * Free Software Foundation; either version 2 of the License, or (at your
16 * option) any later version.
17 *
18 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
19 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
20 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
21 * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 *
27 * You should have received a copy of the GNU General Public License along
28 * with this program; if not, write to the Free Software Foundation, Inc.,
29 * 675 Mass Ave, Cambridge, MA 02139, USA.
30 */
31
32#include <linux/init.h>
Timo Teras77900a22006-06-26 16:16:12 -070033#include <linux/spinlock.h>
34#include <linux/errno.h>
35#include <linux/list.h>
36#include <linux/clk.h>
37#include <linux/delay.h>
Russell Kingfced80c2008-09-06 12:10:45 +010038#include <linux/io.h>
Timo Kokkonen6c366e32009-03-23 18:07:46 -070039#include <linux/module.h>
Russell Kinga09e64f2008-08-05 16:14:15 +010040#include <mach/hardware.h>
Tony Lindgrence491cf2009-10-20 09:40:47 -070041#include <plat/dmtimer.h>
Russell Kinga09e64f2008-08-05 16:14:15 +010042#include <mach/irqs.h>
Tony Lindgren92105bb2005-09-07 17:20:26 +010043
Timo Teras77900a22006-06-26 16:16:12 -070044/* register offsets */
Richard Woodruff0f0d0802008-07-03 12:24:30 +030045#define _OMAP_TIMER_ID_OFFSET 0x00
46#define _OMAP_TIMER_OCP_CFG_OFFSET 0x10
47#define _OMAP_TIMER_SYS_STAT_OFFSET 0x14
48#define _OMAP_TIMER_STAT_OFFSET 0x18
49#define _OMAP_TIMER_INT_EN_OFFSET 0x1c
50#define _OMAP_TIMER_WAKEUP_EN_OFFSET 0x20
51#define _OMAP_TIMER_CTRL_OFFSET 0x24
52#define OMAP_TIMER_CTRL_GPOCFG (1 << 14)
53#define OMAP_TIMER_CTRL_CAPTMODE (1 << 13)
54#define OMAP_TIMER_CTRL_PT (1 << 12)
55#define OMAP_TIMER_CTRL_TCM_LOWTOHIGH (0x1 << 8)
56#define OMAP_TIMER_CTRL_TCM_HIGHTOLOW (0x2 << 8)
57#define OMAP_TIMER_CTRL_TCM_BOTHEDGES (0x3 << 8)
58#define OMAP_TIMER_CTRL_SCPWM (1 << 7)
59#define OMAP_TIMER_CTRL_CE (1 << 6) /* compare enable */
60#define OMAP_TIMER_CTRL_PRE (1 << 5) /* prescaler enable */
61#define OMAP_TIMER_CTRL_PTV_SHIFT 2 /* prescaler value shift */
62#define OMAP_TIMER_CTRL_POSTED (1 << 2)
63#define OMAP_TIMER_CTRL_AR (1 << 1) /* auto-reload enable */
64#define OMAP_TIMER_CTRL_ST (1 << 0) /* start timer */
65#define _OMAP_TIMER_COUNTER_OFFSET 0x28
66#define _OMAP_TIMER_LOAD_OFFSET 0x2c
67#define _OMAP_TIMER_TRIGGER_OFFSET 0x30
68#define _OMAP_TIMER_WRITE_PEND_OFFSET 0x34
69#define WP_NONE 0 /* no write pending bit */
70#define WP_TCLR (1 << 0)
71#define WP_TCRR (1 << 1)
72#define WP_TLDR (1 << 2)
73#define WP_TTGR (1 << 3)
74#define WP_TMAR (1 << 4)
75#define WP_TPIR (1 << 5)
76#define WP_TNIR (1 << 6)
77#define WP_TCVR (1 << 7)
78#define WP_TOCR (1 << 8)
79#define WP_TOWR (1 << 9)
80#define _OMAP_TIMER_MATCH_OFFSET 0x38
81#define _OMAP_TIMER_CAPTURE_OFFSET 0x3c
82#define _OMAP_TIMER_IF_CTRL_OFFSET 0x40
83#define _OMAP_TIMER_CAPTURE2_OFFSET 0x44 /* TCAR2, 34xx only */
84#define _OMAP_TIMER_TICK_POS_OFFSET 0x48 /* TPIR, 34xx only */
85#define _OMAP_TIMER_TICK_NEG_OFFSET 0x4c /* TNIR, 34xx only */
86#define _OMAP_TIMER_TICK_COUNT_OFFSET 0x50 /* TCVR, 34xx only */
87#define _OMAP_TIMER_TICK_INT_MASK_SET_OFFSET 0x54 /* TOCR, 34xx only */
88#define _OMAP_TIMER_TICK_INT_MASK_COUNT_OFFSET 0x58 /* TOWR, 34xx only */
Tony Lindgren92105bb2005-09-07 17:20:26 +010089
Richard Woodruff0f0d0802008-07-03 12:24:30 +030090/* register offsets with the write pending bit encoded */
91#define WPSHIFT 16
92
93#define OMAP_TIMER_ID_REG (_OMAP_TIMER_ID_OFFSET \
94 | (WP_NONE << WPSHIFT))
95
96#define OMAP_TIMER_OCP_CFG_REG (_OMAP_TIMER_OCP_CFG_OFFSET \
97 | (WP_NONE << WPSHIFT))
98
99#define OMAP_TIMER_SYS_STAT_REG (_OMAP_TIMER_SYS_STAT_OFFSET \
100 | (WP_NONE << WPSHIFT))
101
102#define OMAP_TIMER_STAT_REG (_OMAP_TIMER_STAT_OFFSET \
103 | (WP_NONE << WPSHIFT))
104
105#define OMAP_TIMER_INT_EN_REG (_OMAP_TIMER_INT_EN_OFFSET \
106 | (WP_NONE << WPSHIFT))
107
108#define OMAP_TIMER_WAKEUP_EN_REG (_OMAP_TIMER_WAKEUP_EN_OFFSET \
109 | (WP_NONE << WPSHIFT))
110
111#define OMAP_TIMER_CTRL_REG (_OMAP_TIMER_CTRL_OFFSET \
112 | (WP_TCLR << WPSHIFT))
113
114#define OMAP_TIMER_COUNTER_REG (_OMAP_TIMER_COUNTER_OFFSET \
115 | (WP_TCRR << WPSHIFT))
116
117#define OMAP_TIMER_LOAD_REG (_OMAP_TIMER_LOAD_OFFSET \
118 | (WP_TLDR << WPSHIFT))
119
120#define OMAP_TIMER_TRIGGER_REG (_OMAP_TIMER_TRIGGER_OFFSET \
121 | (WP_TTGR << WPSHIFT))
122
123#define OMAP_TIMER_WRITE_PEND_REG (_OMAP_TIMER_WRITE_PEND_OFFSET \
124 | (WP_NONE << WPSHIFT))
125
126#define OMAP_TIMER_MATCH_REG (_OMAP_TIMER_MATCH_OFFSET \
127 | (WP_TMAR << WPSHIFT))
128
129#define OMAP_TIMER_CAPTURE_REG (_OMAP_TIMER_CAPTURE_OFFSET \
130 | (WP_NONE << WPSHIFT))
131
132#define OMAP_TIMER_IF_CTRL_REG (_OMAP_TIMER_IF_CTRL_OFFSET \
133 | (WP_NONE << WPSHIFT))
134
135#define OMAP_TIMER_CAPTURE2_REG (_OMAP_TIMER_CAPTURE2_OFFSET \
136 | (WP_NONE << WPSHIFT))
137
138#define OMAP_TIMER_TICK_POS_REG (_OMAP_TIMER_TICK_POS_OFFSET \
139 | (WP_TPIR << WPSHIFT))
140
141#define OMAP_TIMER_TICK_NEG_REG (_OMAP_TIMER_TICK_NEG_OFFSET \
142 | (WP_TNIR << WPSHIFT))
143
144#define OMAP_TIMER_TICK_COUNT_REG (_OMAP_TIMER_TICK_COUNT_OFFSET \
145 | (WP_TCVR << WPSHIFT))
146
147#define OMAP_TIMER_TICK_INT_MASK_SET_REG \
148 (_OMAP_TIMER_TICK_INT_MASK_SET_OFFSET | (WP_TOCR << WPSHIFT))
149
150#define OMAP_TIMER_TICK_INT_MASK_COUNT_REG \
151 (_OMAP_TIMER_TICK_INT_MASK_COUNT_OFFSET | (WP_TOWR << WPSHIFT))
Tony Lindgren92105bb2005-09-07 17:20:26 +0100152
Timo Teras77900a22006-06-26 16:16:12 -0700153struct omap_dm_timer {
154 unsigned long phys_base;
155 int irq;
Santosh Shilimkar44169072009-05-28 14:16:04 -0700156#if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3) || \
157 defined(CONFIG_ARCH_OMAP4)
Timo Teras77900a22006-06-26 16:16:12 -0700158 struct clk *iclk, *fclk;
159#endif
160 void __iomem *io_base;
161 unsigned reserved:1;
Timo Teras12583a72006-09-25 12:41:42 +0300162 unsigned enabled:1;
Richard Woodruff0f0d0802008-07-03 12:24:30 +0300163 unsigned posted:1;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100164};
165
Tony Lindgren882c0512010-02-12 12:26:46 -0800166static int dm_timer_count;
167
Timo Teras77900a22006-06-26 16:16:12 -0700168#ifdef CONFIG_ARCH_OMAP1
Syed Mohammed, Khasim471b3aa2007-06-21 21:48:07 -0700169static struct omap_dm_timer omap1_dm_timers[] = {
Timo Teras77900a22006-06-26 16:16:12 -0700170 { .phys_base = 0xfffb1400, .irq = INT_1610_GPTIMER1 },
171 { .phys_base = 0xfffb1c00, .irq = INT_1610_GPTIMER2 },
172 { .phys_base = 0xfffb2400, .irq = INT_1610_GPTIMER3 },
173 { .phys_base = 0xfffb2c00, .irq = INT_1610_GPTIMER4 },
174 { .phys_base = 0xfffb3400, .irq = INT_1610_GPTIMER5 },
175 { .phys_base = 0xfffb3c00, .irq = INT_1610_GPTIMER6 },
Matthew Percival53037f42007-01-25 16:24:29 -0800176 { .phys_base = 0xfffb7400, .irq = INT_1610_GPTIMER7 },
177 { .phys_base = 0xfffbd400, .irq = INT_1610_GPTIMER8 },
Timo Teras77900a22006-06-26 16:16:12 -0700178};
179
Tony Lindgren882c0512010-02-12 12:26:46 -0800180static const int omap1_dm_timer_count = ARRAY_SIZE(omap1_dm_timers);
Syed Mohammed, Khasim471b3aa2007-06-21 21:48:07 -0700181
Tony Lindgren882c0512010-02-12 12:26:46 -0800182#else
Syed Mohammed, Khasim471b3aa2007-06-21 21:48:07 -0700183#define omap1_dm_timers NULL
Tony Lindgren882c0512010-02-12 12:26:46 -0800184#define omap1_dm_timer_count 0
185#endif /* CONFIG_ARCH_OMAP1 */
Timo Terasfa4bb622006-09-25 12:41:35 +0300186
Tony Lindgren882c0512010-02-12 12:26:46 -0800187#ifdef CONFIG_ARCH_OMAP2
Syed Mohammed, Khasim471b3aa2007-06-21 21:48:07 -0700188static struct omap_dm_timer omap2_dm_timers[] = {
Timo Teras77900a22006-06-26 16:16:12 -0700189 { .phys_base = 0x48028000, .irq = INT_24XX_GPTIMER1 },
190 { .phys_base = 0x4802a000, .irq = INT_24XX_GPTIMER2 },
191 { .phys_base = 0x48078000, .irq = INT_24XX_GPTIMER3 },
192 { .phys_base = 0x4807a000, .irq = INT_24XX_GPTIMER4 },
193 { .phys_base = 0x4807c000, .irq = INT_24XX_GPTIMER5 },
194 { .phys_base = 0x4807e000, .irq = INT_24XX_GPTIMER6 },
195 { .phys_base = 0x48080000, .irq = INT_24XX_GPTIMER7 },
196 { .phys_base = 0x48082000, .irq = INT_24XX_GPTIMER8 },
197 { .phys_base = 0x48084000, .irq = INT_24XX_GPTIMER9 },
198 { .phys_base = 0x48086000, .irq = INT_24XX_GPTIMER10 },
199 { .phys_base = 0x48088000, .irq = INT_24XX_GPTIMER11 },
200 { .phys_base = 0x4808a000, .irq = INT_24XX_GPTIMER12 },
201};
202
Syed Mohammed, Khasim471b3aa2007-06-21 21:48:07 -0700203static const char *omap2_dm_source_names[] __initdata = {
Timo Teras83379c82006-06-26 16:16:23 -0700204 "sys_ck",
205 "func_32k_ck",
Syed Mohammed, Khasim471b3aa2007-06-21 21:48:07 -0700206 "alt_ck",
207 NULL
Timo Teras83379c82006-06-26 16:16:23 -0700208};
209
Santosh Shilimkaraea2a5b2009-05-25 11:08:36 -0700210static struct clk *omap2_dm_source_clocks[3];
Tony Lindgren882c0512010-02-12 12:26:46 -0800211static const int omap2_dm_timer_count = ARRAY_SIZE(omap2_dm_timers);
Timo Teras83379c82006-06-26 16:16:23 -0700212
Tony Lindgren882c0512010-02-12 12:26:46 -0800213#else
Syed Mohammed, Khasimce2df9c2007-06-25 22:55:39 -0700214#define omap2_dm_timers NULL
Tony Lindgren882c0512010-02-12 12:26:46 -0800215#define omap2_dm_timer_count 0
Syed Mohammed, Khasimce2df9c2007-06-25 22:55:39 -0700216#define omap2_dm_source_names NULL
217#define omap2_dm_source_clocks NULL
Tony Lindgren882c0512010-02-12 12:26:46 -0800218#endif /* CONFIG_ARCH_OMAP2 */
Syed Mohammed, Khasimce2df9c2007-06-25 22:55:39 -0700219
Tony Lindgren882c0512010-02-12 12:26:46 -0800220#ifdef CONFIG_ARCH_OMAP3
Syed Mohammed, Khasimce2df9c2007-06-25 22:55:39 -0700221static struct omap_dm_timer omap3_dm_timers[] = {
222 { .phys_base = 0x48318000, .irq = INT_24XX_GPTIMER1 },
223 { .phys_base = 0x49032000, .irq = INT_24XX_GPTIMER2 },
224 { .phys_base = 0x49034000, .irq = INT_24XX_GPTIMER3 },
225 { .phys_base = 0x49036000, .irq = INT_24XX_GPTIMER4 },
226 { .phys_base = 0x49038000, .irq = INT_24XX_GPTIMER5 },
227 { .phys_base = 0x4903A000, .irq = INT_24XX_GPTIMER6 },
228 { .phys_base = 0x4903C000, .irq = INT_24XX_GPTIMER7 },
229 { .phys_base = 0x4903E000, .irq = INT_24XX_GPTIMER8 },
230 { .phys_base = 0x49040000, .irq = INT_24XX_GPTIMER9 },
231 { .phys_base = 0x48086000, .irq = INT_24XX_GPTIMER10 },
232 { .phys_base = 0x48088000, .irq = INT_24XX_GPTIMER11 },
Paul Walmsley9198a402009-04-23 21:11:08 -0600233 { .phys_base = 0x48304000, .irq = INT_34XX_GPT12_IRQ },
Syed Mohammed, Khasimce2df9c2007-06-25 22:55:39 -0700234};
235
236static const char *omap3_dm_source_names[] __initdata = {
237 "sys_ck",
238 "omap_32k_fck",
239 NULL
240};
241
Santosh Shilimkaraea2a5b2009-05-25 11:08:36 -0700242static struct clk *omap3_dm_source_clocks[2];
Tony Lindgren882c0512010-02-12 12:26:46 -0800243static const int omap3_dm_timer_count = ARRAY_SIZE(omap3_dm_timers);
Syed Mohammed, Khasimce2df9c2007-06-25 22:55:39 -0700244
Tony Lindgren882c0512010-02-12 12:26:46 -0800245#else
Santosh Shilimkar44169072009-05-28 14:16:04 -0700246#define omap3_dm_timers NULL
Tony Lindgren882c0512010-02-12 12:26:46 -0800247#define omap3_dm_timer_count 0
Santosh Shilimkar44169072009-05-28 14:16:04 -0700248#define omap3_dm_source_names NULL
249#define omap3_dm_source_clocks NULL
Tony Lindgren882c0512010-02-12 12:26:46 -0800250#endif /* CONFIG_ARCH_OMAP3 */
Santosh Shilimkar44169072009-05-28 14:16:04 -0700251
Tony Lindgren882c0512010-02-12 12:26:46 -0800252#ifdef CONFIG_ARCH_OMAP4
Santosh Shilimkar44169072009-05-28 14:16:04 -0700253static struct omap_dm_timer omap4_dm_timers[] = {
254 { .phys_base = 0x4a318000, .irq = INT_44XX_GPTIMER1 },
255 { .phys_base = 0x48032000, .irq = INT_44XX_GPTIMER2 },
256 { .phys_base = 0x48034000, .irq = INT_44XX_GPTIMER3 },
257 { .phys_base = 0x48036000, .irq = INT_44XX_GPTIMER4 },
258 { .phys_base = 0x40138000, .irq = INT_44XX_GPTIMER5 },
259 { .phys_base = 0x4013a000, .irq = INT_44XX_GPTIMER6 },
260 { .phys_base = 0x4013a000, .irq = INT_44XX_GPTIMER7 },
261 { .phys_base = 0x4013e000, .irq = INT_44XX_GPTIMER8 },
262 { .phys_base = 0x4803e000, .irq = INT_44XX_GPTIMER9 },
263 { .phys_base = 0x48086000, .irq = INT_44XX_GPTIMER10 },
264 { .phys_base = 0x48088000, .irq = INT_44XX_GPTIMER11 },
265 { .phys_base = 0x4a320000, .irq = INT_44XX_GPTIMER12 },
266};
267static const char *omap4_dm_source_names[] __initdata = {
268 "sys_ck",
269 "omap_32k_fck",
270 NULL
271};
272static struct clk *omap4_dm_source_clocks[2];
Tony Lindgren882c0512010-02-12 12:26:46 -0800273static const int omap4_dm_timer_count = ARRAY_SIZE(omap4_dm_timers);
Santosh Shilimkar44169072009-05-28 14:16:04 -0700274
Timo Teras77900a22006-06-26 16:16:12 -0700275#else
Tony Lindgren882c0512010-02-12 12:26:46 -0800276#define omap4_dm_timers NULL
277#define omap4_dm_timer_count 0
278#define omap4_dm_source_names NULL
279#define omap4_dm_source_clocks NULL
280#endif /* CONFIG_ARCH_OMAP4 */
Timo Teras77900a22006-06-26 16:16:12 -0700281
Syed Mohammed, Khasim471b3aa2007-06-21 21:48:07 -0700282static struct omap_dm_timer *dm_timers;
Santosh Shilimkaraea2a5b2009-05-25 11:08:36 -0700283static const char **dm_source_names;
Syed Mohammed, Khasim471b3aa2007-06-21 21:48:07 -0700284static struct clk **dm_source_clocks;
285
Tony Lindgren92105bb2005-09-07 17:20:26 +0100286static spinlock_t dm_timer_lock;
287
Richard Woodruff0f0d0802008-07-03 12:24:30 +0300288/*
289 * Reads timer registers in posted and non-posted mode. The posted mode bit
290 * is encoded in reg. Note that in posted mode write pending bit must be
291 * checked. Otherwise a read of a non completed write will produce an error.
292 */
293static inline u32 omap_dm_timer_read_reg(struct omap_dm_timer *timer, u32 reg)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100294{
Richard Woodruff0f0d0802008-07-03 12:24:30 +0300295 if (timer->posted)
296 while (readl(timer->io_base + (OMAP_TIMER_WRITE_PEND_REG & 0xff))
297 & (reg >> WPSHIFT))
298 cpu_relax();
299 return readl(timer->io_base + (reg & 0xff));
Timo Teras77900a22006-06-26 16:16:12 -0700300}
301
Richard Woodruff0f0d0802008-07-03 12:24:30 +0300302/*
303 * Writes timer registers in posted and non-posted mode. The posted mode bit
304 * is encoded in reg. Note that in posted mode the write pending bit must be
305 * checked. Otherwise a write on a register which has a pending write will be
306 * lost.
307 */
308static void omap_dm_timer_write_reg(struct omap_dm_timer *timer, u32 reg,
309 u32 value)
Timo Teras77900a22006-06-26 16:16:12 -0700310{
Richard Woodruff0f0d0802008-07-03 12:24:30 +0300311 if (timer->posted)
312 while (readl(timer->io_base + (OMAP_TIMER_WRITE_PEND_REG & 0xff))
313 & (reg >> WPSHIFT))
314 cpu_relax();
315 writel(value, timer->io_base + (reg & 0xff));
Tony Lindgren92105bb2005-09-07 17:20:26 +0100316}
317
Timo Teras77900a22006-06-26 16:16:12 -0700318static void omap_dm_timer_wait_for_reset(struct omap_dm_timer *timer)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100319{
Timo Teras77900a22006-06-26 16:16:12 -0700320 int c;
321
322 c = 0;
323 while (!(omap_dm_timer_read_reg(timer, OMAP_TIMER_SYS_STAT_REG) & 1)) {
324 c++;
325 if (c > 100000) {
326 printk(KERN_ERR "Timer failed to reset\n");
327 return;
328 }
329 }
Tony Lindgren92105bb2005-09-07 17:20:26 +0100330}
331
Timo Teras77900a22006-06-26 16:16:12 -0700332static void omap_dm_timer_reset(struct omap_dm_timer *timer)
333{
334 u32 l;
335
Juha Yrjola39020842006-09-25 12:41:44 +0300336 if (!cpu_class_is_omap2() || timer != &dm_timers[0]) {
Timo Terase32f7ec2006-06-26 16:16:13 -0700337 omap_dm_timer_write_reg(timer, OMAP_TIMER_IF_CTRL_REG, 0x06);
338 omap_dm_timer_wait_for_reset(timer);
339 }
Timo Teras12583a72006-09-25 12:41:42 +0300340 omap_dm_timer_set_source(timer, OMAP_TIMER_SRC_32_KHZ);
Timo Teras77900a22006-06-26 16:16:12 -0700341
Timo Teras77900a22006-06-26 16:16:12 -0700342 l = omap_dm_timer_read_reg(timer, OMAP_TIMER_OCP_CFG_REG);
Richard Woodruff0f0d0802008-07-03 12:24:30 +0300343 l |= 0x02 << 3; /* Set to smart-idle mode */
344 l |= 0x2 << 8; /* Set clock activity to perserve f-clock on idle */
Juha Yrjola39020842006-09-25 12:41:44 +0300345
Richard Woodruff0f0d0802008-07-03 12:24:30 +0300346 /*
Kevin Hilman219c5b92009-04-23 21:11:08 -0600347 * Enable wake-up on OMAP2 CPUs.
Richard Woodruff0f0d0802008-07-03 12:24:30 +0300348 */
Kevin Hilman219c5b92009-04-23 21:11:08 -0600349 if (cpu_class_is_omap2())
Juha Yrjola39020842006-09-25 12:41:44 +0300350 l |= 1 << 2;
Timo Teras77900a22006-06-26 16:16:12 -0700351 omap_dm_timer_write_reg(timer, OMAP_TIMER_OCP_CFG_REG, l);
Richard Woodruff0f0d0802008-07-03 12:24:30 +0300352
353 /* Match hardware reset default of posted mode */
354 omap_dm_timer_write_reg(timer, OMAP_TIMER_IF_CTRL_REG,
355 OMAP_TIMER_CTRL_POSTED);
356 timer->posted = 1;
Timo Teras77900a22006-06-26 16:16:12 -0700357}
358
Timo Teras83379c82006-06-26 16:16:23 -0700359static void omap_dm_timer_prepare(struct omap_dm_timer *timer)
Timo Teras77900a22006-06-26 16:16:12 -0700360{
Timo Teras12583a72006-09-25 12:41:42 +0300361 omap_dm_timer_enable(timer);
Timo Teras77900a22006-06-26 16:16:12 -0700362 omap_dm_timer_reset(timer);
363}
364
365struct omap_dm_timer *omap_dm_timer_request(void)
366{
367 struct omap_dm_timer *timer = NULL;
368 unsigned long flags;
369 int i;
370
371 spin_lock_irqsave(&dm_timer_lock, flags);
372 for (i = 0; i < dm_timer_count; i++) {
373 if (dm_timers[i].reserved)
374 continue;
375
376 timer = &dm_timers[i];
Timo Teras83379c82006-06-26 16:16:23 -0700377 timer->reserved = 1;
Timo Teras77900a22006-06-26 16:16:12 -0700378 break;
379 }
380 spin_unlock_irqrestore(&dm_timer_lock, flags);
381
Timo Teras83379c82006-06-26 16:16:23 -0700382 if (timer != NULL)
383 omap_dm_timer_prepare(timer);
384
Timo Teras77900a22006-06-26 16:16:12 -0700385 return timer;
386}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700387EXPORT_SYMBOL_GPL(omap_dm_timer_request);
Timo Teras77900a22006-06-26 16:16:12 -0700388
389struct omap_dm_timer *omap_dm_timer_request_specific(int id)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100390{
391 struct omap_dm_timer *timer;
Timo Teras77900a22006-06-26 16:16:12 -0700392 unsigned long flags;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100393
Timo Teras77900a22006-06-26 16:16:12 -0700394 spin_lock_irqsave(&dm_timer_lock, flags);
395 if (id <= 0 || id > dm_timer_count || dm_timers[id-1].reserved) {
396 spin_unlock_irqrestore(&dm_timer_lock, flags);
397 printk("BUG: warning at %s:%d/%s(): unable to get timer %d\n",
Harvey Harrison8e86f422008-03-04 15:08:02 -0800398 __FILE__, __LINE__, __func__, id);
Timo Teras77900a22006-06-26 16:16:12 -0700399 dump_stack();
400 return NULL;
401 }
Tony Lindgren92105bb2005-09-07 17:20:26 +0100402
Timo Teras77900a22006-06-26 16:16:12 -0700403 timer = &dm_timers[id-1];
Timo Teras83379c82006-06-26 16:16:23 -0700404 timer->reserved = 1;
Timo Teras77900a22006-06-26 16:16:12 -0700405 spin_unlock_irqrestore(&dm_timer_lock, flags);
406
Timo Teras83379c82006-06-26 16:16:23 -0700407 omap_dm_timer_prepare(timer);
408
Timo Teras77900a22006-06-26 16:16:12 -0700409 return timer;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100410}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700411EXPORT_SYMBOL_GPL(omap_dm_timer_request_specific);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100412
Timo Teras77900a22006-06-26 16:16:12 -0700413void omap_dm_timer_free(struct omap_dm_timer *timer)
414{
Timo Teras12583a72006-09-25 12:41:42 +0300415 omap_dm_timer_enable(timer);
Timo Teras77900a22006-06-26 16:16:12 -0700416 omap_dm_timer_reset(timer);
Timo Teras12583a72006-09-25 12:41:42 +0300417 omap_dm_timer_disable(timer);
Timo Terasfa4bb622006-09-25 12:41:35 +0300418
Timo Teras77900a22006-06-26 16:16:12 -0700419 WARN_ON(!timer->reserved);
420 timer->reserved = 0;
421}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700422EXPORT_SYMBOL_GPL(omap_dm_timer_free);
Timo Teras77900a22006-06-26 16:16:12 -0700423
Timo Teras12583a72006-09-25 12:41:42 +0300424void omap_dm_timer_enable(struct omap_dm_timer *timer)
425{
426 if (timer->enabled)
427 return;
428
Tony Lindgren882c0512010-02-12 12:26:46 -0800429#ifdef CONFIG_ARCH_OMAP2PLUS
430 if (cpu_class_is_omap2()) {
431 clk_enable(timer->fclk);
432 clk_enable(timer->iclk);
433 }
434#endif
Timo Teras12583a72006-09-25 12:41:42 +0300435
436 timer->enabled = 1;
437}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700438EXPORT_SYMBOL_GPL(omap_dm_timer_enable);
Timo Teras12583a72006-09-25 12:41:42 +0300439
440void omap_dm_timer_disable(struct omap_dm_timer *timer)
441{
442 if (!timer->enabled)
443 return;
444
Tony Lindgren882c0512010-02-12 12:26:46 -0800445#ifdef CONFIG_ARCH_OMAP2PLUS
446 if (cpu_class_is_omap2()) {
447 clk_disable(timer->iclk);
448 clk_disable(timer->fclk);
449 }
450#endif
Timo Teras12583a72006-09-25 12:41:42 +0300451
452 timer->enabled = 0;
453}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700454EXPORT_SYMBOL_GPL(omap_dm_timer_disable);
Timo Teras12583a72006-09-25 12:41:42 +0300455
Timo Teras77900a22006-06-26 16:16:12 -0700456int omap_dm_timer_get_irq(struct omap_dm_timer *timer)
457{
458 return timer->irq;
459}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700460EXPORT_SYMBOL_GPL(omap_dm_timer_get_irq);
Timo Teras77900a22006-06-26 16:16:12 -0700461
462#if defined(CONFIG_ARCH_OMAP1)
463
Tony Lindgrena569c6e2006-04-02 17:46:21 +0100464/**
465 * omap_dm_timer_modify_idlect_mask - Check if any running timers use ARMXOR
466 * @inputmask: current value of idlect mask
467 */
468__u32 omap_dm_timer_modify_idlect_mask(__u32 inputmask)
469{
Timo Teras77900a22006-06-26 16:16:12 -0700470 int i;
Tony Lindgrena569c6e2006-04-02 17:46:21 +0100471
472 /* If ARMXOR cannot be idled this function call is unnecessary */
473 if (!(inputmask & (1 << 1)))
474 return inputmask;
475
476 /* If any active timer is using ARMXOR return modified mask */
Timo Teras77900a22006-06-26 16:16:12 -0700477 for (i = 0; i < dm_timer_count; i++) {
478 u32 l;
479
Tony Lindgren35912c72006-07-01 19:56:42 +0100480 l = omap_dm_timer_read_reg(&dm_timers[i], OMAP_TIMER_CTRL_REG);
Timo Teras77900a22006-06-26 16:16:12 -0700481 if (l & OMAP_TIMER_CTRL_ST) {
482 if (((omap_readl(MOD_CONF_CTRL_1) >> (i * 2)) & 0x03) == 0)
Tony Lindgrena569c6e2006-04-02 17:46:21 +0100483 inputmask &= ~(1 << 1);
484 else
485 inputmask &= ~(1 << 2);
486 }
Timo Teras77900a22006-06-26 16:16:12 -0700487 }
Tony Lindgrena569c6e2006-04-02 17:46:21 +0100488
489 return inputmask;
490}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700491EXPORT_SYMBOL_GPL(omap_dm_timer_modify_idlect_mask);
Tony Lindgrena569c6e2006-04-02 17:46:21 +0100492
Santosh Shilimkar44169072009-05-28 14:16:04 -0700493#elif defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3) || \
494 defined(CONFIG_ARCH_OMAP4)
Timo Teras77900a22006-06-26 16:16:12 -0700495
496struct clk *omap_dm_timer_get_fclk(struct omap_dm_timer *timer)
497{
Timo Terasfa4bb622006-09-25 12:41:35 +0300498 return timer->fclk;
Timo Teras77900a22006-06-26 16:16:12 -0700499}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700500EXPORT_SYMBOL_GPL(omap_dm_timer_get_fclk);
Timo Teras77900a22006-06-26 16:16:12 -0700501
502__u32 omap_dm_timer_modify_idlect_mask(__u32 inputmask)
503{
504 BUG();
Dirk Behme21218802006-12-06 17:14:00 -0800505
506 return 0;
Timo Teras77900a22006-06-26 16:16:12 -0700507}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700508EXPORT_SYMBOL_GPL(omap_dm_timer_modify_idlect_mask);
Timo Teras77900a22006-06-26 16:16:12 -0700509
510#endif
511
512void omap_dm_timer_trigger(struct omap_dm_timer *timer)
513{
514 omap_dm_timer_write_reg(timer, OMAP_TIMER_TRIGGER_REG, 0);
515}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700516EXPORT_SYMBOL_GPL(omap_dm_timer_trigger);
Timo Teras77900a22006-06-26 16:16:12 -0700517
518void omap_dm_timer_start(struct omap_dm_timer *timer)
519{
520 u32 l;
521
522 l = omap_dm_timer_read_reg(timer, OMAP_TIMER_CTRL_REG);
523 if (!(l & OMAP_TIMER_CTRL_ST)) {
524 l |= OMAP_TIMER_CTRL_ST;
525 omap_dm_timer_write_reg(timer, OMAP_TIMER_CTRL_REG, l);
526 }
527}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700528EXPORT_SYMBOL_GPL(omap_dm_timer_start);
Timo Teras77900a22006-06-26 16:16:12 -0700529
530void omap_dm_timer_stop(struct omap_dm_timer *timer)
531{
532 u32 l;
533
534 l = omap_dm_timer_read_reg(timer, OMAP_TIMER_CTRL_REG);
535 if (l & OMAP_TIMER_CTRL_ST) {
536 l &= ~0x1;
537 omap_dm_timer_write_reg(timer, OMAP_TIMER_CTRL_REG, l);
Tero Kristo5c3db362009-10-23 19:03:47 +0300538#if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3) || \
539 defined(CONFIG_ARCH_OMAP4)
540 /* Readback to make sure write has completed */
541 omap_dm_timer_read_reg(timer, OMAP_TIMER_CTRL_REG);
542 /*
543 * Wait for functional clock period x 3.5 to make sure that
544 * timer is stopped
545 */
546 udelay(3500000 / clk_get_rate(timer->fclk) + 1);
547 /* Ack possibly pending interrupt */
548 omap_dm_timer_write_reg(timer, OMAP_TIMER_STAT_REG,
549 OMAP_TIMER_INT_OVERFLOW);
550#endif
Timo Teras77900a22006-06-26 16:16:12 -0700551 }
552}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700553EXPORT_SYMBOL_GPL(omap_dm_timer_stop);
Timo Teras77900a22006-06-26 16:16:12 -0700554
555#ifdef CONFIG_ARCH_OMAP1
Tony Lindgrena569c6e2006-04-02 17:46:21 +0100556
Paul Walmsleyf2480762009-04-23 21:11:10 -0600557int omap_dm_timer_set_source(struct omap_dm_timer *timer, int source)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100558{
559 int n = (timer - dm_timers) << 1;
560 u32 l;
561
562 l = omap_readl(MOD_CONF_CTRL_1) & ~(0x03 << n);
563 l |= source << n;
564 omap_writel(l, MOD_CONF_CTRL_1);
Paul Walmsleyf2480762009-04-23 21:11:10 -0600565
566 return 0;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100567}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700568EXPORT_SYMBOL_GPL(omap_dm_timer_set_source);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100569
Timo Teras77900a22006-06-26 16:16:12 -0700570#else
Tony Lindgren92105bb2005-09-07 17:20:26 +0100571
Paul Walmsleyf2480762009-04-23 21:11:10 -0600572int omap_dm_timer_set_source(struct omap_dm_timer *timer, int source)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100573{
Paul Walmsleyf2480762009-04-23 21:11:10 -0600574 int ret = -EINVAL;
575
Timo Teras77900a22006-06-26 16:16:12 -0700576 if (source < 0 || source >= 3)
Paul Walmsleyf2480762009-04-23 21:11:10 -0600577 return -EINVAL;
Timo Teras77900a22006-06-26 16:16:12 -0700578
Timo Teras77900a22006-06-26 16:16:12 -0700579 clk_disable(timer->fclk);
Paul Walmsleyf2480762009-04-23 21:11:10 -0600580 ret = clk_set_parent(timer->fclk, dm_source_clocks[source]);
Timo Teras77900a22006-06-26 16:16:12 -0700581 clk_enable(timer->fclk);
Timo Teras77900a22006-06-26 16:16:12 -0700582
Paul Walmsleyf2480762009-04-23 21:11:10 -0600583 /*
584 * When the functional clock disappears, too quick writes seem
585 * to cause an abort. XXX Is this still necessary?
586 */
Tony Lindgrenc40fae952006-12-07 13:58:10 -0800587 __delay(150000);
Paul Walmsleyf2480762009-04-23 21:11:10 -0600588
589 return ret;
Timo Teras77900a22006-06-26 16:16:12 -0700590}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700591EXPORT_SYMBOL_GPL(omap_dm_timer_set_source);
Timo Teras77900a22006-06-26 16:16:12 -0700592
593#endif
594
595void omap_dm_timer_set_load(struct omap_dm_timer *timer, int autoreload,
596 unsigned int load)
597{
598 u32 l;
599
600 l = omap_dm_timer_read_reg(timer, OMAP_TIMER_CTRL_REG);
601 if (autoreload)
602 l |= OMAP_TIMER_CTRL_AR;
603 else
604 l &= ~OMAP_TIMER_CTRL_AR;
605 omap_dm_timer_write_reg(timer, OMAP_TIMER_CTRL_REG, l);
606 omap_dm_timer_write_reg(timer, OMAP_TIMER_LOAD_REG, load);
Richard Woodruff0f0d0802008-07-03 12:24:30 +0300607
Timo Teras77900a22006-06-26 16:16:12 -0700608 omap_dm_timer_write_reg(timer, OMAP_TIMER_TRIGGER_REG, 0);
609}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700610EXPORT_SYMBOL_GPL(omap_dm_timer_set_load);
Timo Teras77900a22006-06-26 16:16:12 -0700611
Richard Woodruff3fddd092008-07-03 12:24:30 +0300612/* Optimized set_load which removes costly spin wait in timer_start */
613void omap_dm_timer_set_load_start(struct omap_dm_timer *timer, int autoreload,
614 unsigned int load)
615{
616 u32 l;
617
618 l = omap_dm_timer_read_reg(timer, OMAP_TIMER_CTRL_REG);
Paul Walmsley64ce2902008-12-10 17:36:34 -0800619 if (autoreload) {
Richard Woodruff3fddd092008-07-03 12:24:30 +0300620 l |= OMAP_TIMER_CTRL_AR;
Paul Walmsley64ce2902008-12-10 17:36:34 -0800621 omap_dm_timer_write_reg(timer, OMAP_TIMER_LOAD_REG, load);
622 } else {
Richard Woodruff3fddd092008-07-03 12:24:30 +0300623 l &= ~OMAP_TIMER_CTRL_AR;
Paul Walmsley64ce2902008-12-10 17:36:34 -0800624 }
Richard Woodruff3fddd092008-07-03 12:24:30 +0300625 l |= OMAP_TIMER_CTRL_ST;
626
627 omap_dm_timer_write_reg(timer, OMAP_TIMER_COUNTER_REG, load);
Richard Woodruff3fddd092008-07-03 12:24:30 +0300628 omap_dm_timer_write_reg(timer, OMAP_TIMER_CTRL_REG, l);
629}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700630EXPORT_SYMBOL_GPL(omap_dm_timer_set_load_start);
Richard Woodruff3fddd092008-07-03 12:24:30 +0300631
Timo Teras77900a22006-06-26 16:16:12 -0700632void omap_dm_timer_set_match(struct omap_dm_timer *timer, int enable,
633 unsigned int match)
634{
635 u32 l;
636
637 l = omap_dm_timer_read_reg(timer, OMAP_TIMER_CTRL_REG);
Timo Teras83379c82006-06-26 16:16:23 -0700638 if (enable)
Timo Teras77900a22006-06-26 16:16:12 -0700639 l |= OMAP_TIMER_CTRL_CE;
640 else
641 l &= ~OMAP_TIMER_CTRL_CE;
642 omap_dm_timer_write_reg(timer, OMAP_TIMER_CTRL_REG, l);
643 omap_dm_timer_write_reg(timer, OMAP_TIMER_MATCH_REG, match);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100644}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700645EXPORT_SYMBOL_GPL(omap_dm_timer_set_match);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100646
Timo Teras77900a22006-06-26 16:16:12 -0700647void omap_dm_timer_set_pwm(struct omap_dm_timer *timer, int def_on,
648 int toggle, int trigger)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100649{
Timo Teras77900a22006-06-26 16:16:12 -0700650 u32 l;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100651
Timo Teras77900a22006-06-26 16:16:12 -0700652 l = omap_dm_timer_read_reg(timer, OMAP_TIMER_CTRL_REG);
653 l &= ~(OMAP_TIMER_CTRL_GPOCFG | OMAP_TIMER_CTRL_SCPWM |
654 OMAP_TIMER_CTRL_PT | (0x03 << 10));
655 if (def_on)
656 l |= OMAP_TIMER_CTRL_SCPWM;
657 if (toggle)
658 l |= OMAP_TIMER_CTRL_PT;
659 l |= trigger << 10;
660 omap_dm_timer_write_reg(timer, OMAP_TIMER_CTRL_REG, l);
661}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700662EXPORT_SYMBOL_GPL(omap_dm_timer_set_pwm);
Timo Teras77900a22006-06-26 16:16:12 -0700663
664void omap_dm_timer_set_prescaler(struct omap_dm_timer *timer, int prescaler)
665{
666 u32 l;
667
668 l = omap_dm_timer_read_reg(timer, OMAP_TIMER_CTRL_REG);
669 l &= ~(OMAP_TIMER_CTRL_PRE | (0x07 << 2));
670 if (prescaler >= 0x00 && prescaler <= 0x07) {
671 l |= OMAP_TIMER_CTRL_PRE;
672 l |= prescaler << 2;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100673 }
Timo Teras77900a22006-06-26 16:16:12 -0700674 omap_dm_timer_write_reg(timer, OMAP_TIMER_CTRL_REG, l);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100675}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700676EXPORT_SYMBOL_GPL(omap_dm_timer_set_prescaler);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100677
678void omap_dm_timer_set_int_enable(struct omap_dm_timer *timer,
Timo Teras77900a22006-06-26 16:16:12 -0700679 unsigned int value)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100680{
681 omap_dm_timer_write_reg(timer, OMAP_TIMER_INT_EN_REG, value);
Juha Yrjola39020842006-09-25 12:41:44 +0300682 omap_dm_timer_write_reg(timer, OMAP_TIMER_WAKEUP_EN_REG, value);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100683}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700684EXPORT_SYMBOL_GPL(omap_dm_timer_set_int_enable);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100685
686unsigned int omap_dm_timer_read_status(struct omap_dm_timer *timer)
687{
Timo Terasfa4bb622006-09-25 12:41:35 +0300688 unsigned int l;
689
Timo Terasfa4bb622006-09-25 12:41:35 +0300690 l = omap_dm_timer_read_reg(timer, OMAP_TIMER_STAT_REG);
Timo Terasfa4bb622006-09-25 12:41:35 +0300691
692 return l;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100693}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700694EXPORT_SYMBOL_GPL(omap_dm_timer_read_status);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100695
696void omap_dm_timer_write_status(struct omap_dm_timer *timer, unsigned int value)
697{
698 omap_dm_timer_write_reg(timer, OMAP_TIMER_STAT_REG, value);
699}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700700EXPORT_SYMBOL_GPL(omap_dm_timer_write_status);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100701
Tony Lindgren92105bb2005-09-07 17:20:26 +0100702unsigned int omap_dm_timer_read_counter(struct omap_dm_timer *timer)
703{
Timo Terasfa4bb622006-09-25 12:41:35 +0300704 unsigned int l;
705
Timo Terasfa4bb622006-09-25 12:41:35 +0300706 l = omap_dm_timer_read_reg(timer, OMAP_TIMER_COUNTER_REG);
Timo Terasfa4bb622006-09-25 12:41:35 +0300707
708 return l;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100709}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700710EXPORT_SYMBOL_GPL(omap_dm_timer_read_counter);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100711
Timo Teras83379c82006-06-26 16:16:23 -0700712void omap_dm_timer_write_counter(struct omap_dm_timer *timer, unsigned int value)
713{
Timo Terasfa4bb622006-09-25 12:41:35 +0300714 omap_dm_timer_write_reg(timer, OMAP_TIMER_COUNTER_REG, value);
Timo Teras83379c82006-06-26 16:16:23 -0700715}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700716EXPORT_SYMBOL_GPL(omap_dm_timer_write_counter);
Timo Teras83379c82006-06-26 16:16:23 -0700717
Timo Teras77900a22006-06-26 16:16:12 -0700718int omap_dm_timers_active(void)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100719{
Timo Teras77900a22006-06-26 16:16:12 -0700720 int i;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100721
Timo Teras77900a22006-06-26 16:16:12 -0700722 for (i = 0; i < dm_timer_count; i++) {
723 struct omap_dm_timer *timer;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100724
Timo Teras77900a22006-06-26 16:16:12 -0700725 timer = &dm_timers[i];
Timo Teras12583a72006-09-25 12:41:42 +0300726
727 if (!timer->enabled)
728 continue;
729
Timo Teras77900a22006-06-26 16:16:12 -0700730 if (omap_dm_timer_read_reg(timer, OMAP_TIMER_CTRL_REG) &
Timo Terasfa4bb622006-09-25 12:41:35 +0300731 OMAP_TIMER_CTRL_ST) {
Timo Teras77900a22006-06-26 16:16:12 -0700732 return 1;
Timo Terasfa4bb622006-09-25 12:41:35 +0300733 }
Tony Lindgren92105bb2005-09-07 17:20:26 +0100734 }
Tony Lindgren92105bb2005-09-07 17:20:26 +0100735 return 0;
736}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700737EXPORT_SYMBOL_GPL(omap_dm_timers_active);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100738
Syed Mohammed, Khasim471b3aa2007-06-21 21:48:07 -0700739int __init omap_dm_timer_init(void)
Timo Teras77900a22006-06-26 16:16:12 -0700740{
741 struct omap_dm_timer *timer;
Tony Lindgren3566fc62009-10-19 15:25:18 -0700742 int i, map_size = SZ_8K; /* Module 4KB + L4 4KB except on omap1 */
Timo Teras77900a22006-06-26 16:16:12 -0700743
Syed Mohammed, Khasimce2df9c2007-06-25 22:55:39 -0700744 if (!(cpu_is_omap16xx() || cpu_class_is_omap2()))
Timo Teras77900a22006-06-26 16:16:12 -0700745 return -ENODEV;
746
747 spin_lock_init(&dm_timer_lock);
Syed Mohammed, Khasim471b3aa2007-06-21 21:48:07 -0700748
Tony Lindgren3566fc62009-10-19 15:25:18 -0700749 if (cpu_class_is_omap1()) {
Syed Mohammed, Khasim471b3aa2007-06-21 21:48:07 -0700750 dm_timers = omap1_dm_timers;
Tony Lindgren882c0512010-02-12 12:26:46 -0800751 dm_timer_count = omap1_dm_timer_count;
Tony Lindgren3566fc62009-10-19 15:25:18 -0700752 map_size = SZ_2K;
753 } else if (cpu_is_omap24xx()) {
Syed Mohammed, Khasim471b3aa2007-06-21 21:48:07 -0700754 dm_timers = omap2_dm_timers;
Tony Lindgren882c0512010-02-12 12:26:46 -0800755 dm_timer_count = omap2_dm_timer_count;
Santosh Shilimkaraea2a5b2009-05-25 11:08:36 -0700756 dm_source_names = omap2_dm_source_names;
757 dm_source_clocks = omap2_dm_source_clocks;
Syed Mohammed, Khasimce2df9c2007-06-25 22:55:39 -0700758 } else if (cpu_is_omap34xx()) {
759 dm_timers = omap3_dm_timers;
Tony Lindgren882c0512010-02-12 12:26:46 -0800760 dm_timer_count = omap3_dm_timer_count;
Santosh Shilimkaraea2a5b2009-05-25 11:08:36 -0700761 dm_source_names = omap3_dm_source_names;
762 dm_source_clocks = omap3_dm_source_clocks;
Santosh Shilimkar44169072009-05-28 14:16:04 -0700763 } else if (cpu_is_omap44xx()) {
764 dm_timers = omap4_dm_timers;
Tony Lindgren882c0512010-02-12 12:26:46 -0800765 dm_timer_count = omap4_dm_timer_count;
Santosh Shilimkar44169072009-05-28 14:16:04 -0700766 dm_source_names = omap4_dm_source_names;
767 dm_source_clocks = omap4_dm_source_clocks;
Timo Teras83379c82006-06-26 16:16:23 -0700768 }
Syed Mohammed, Khasim471b3aa2007-06-21 21:48:07 -0700769
770 if (cpu_class_is_omap2())
771 for (i = 0; dm_source_names[i] != NULL; i++)
772 dm_source_clocks[i] = clk_get(NULL, dm_source_names[i]);
773
Syed Mohammed Khasim56a25642006-12-06 17:14:08 -0800774 if (cpu_is_omap243x())
775 dm_timers[0].phys_base = 0x49018000;
Timo Teras83379c82006-06-26 16:16:23 -0700776
Timo Teras77900a22006-06-26 16:16:12 -0700777 for (i = 0; i < dm_timer_count; i++) {
Timo Teras77900a22006-06-26 16:16:12 -0700778 timer = &dm_timers[i];
Tony Lindgren3566fc62009-10-19 15:25:18 -0700779
780 /* Static mapping, never released */
781 timer->io_base = ioremap(timer->phys_base, map_size);
782 BUG_ON(!timer->io_base);
783
Santosh Shilimkar44169072009-05-28 14:16:04 -0700784#if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3) || \
785 defined(CONFIG_ARCH_OMAP4)
Syed Mohammed, Khasim471b3aa2007-06-21 21:48:07 -0700786 if (cpu_class_is_omap2()) {
787 char clk_name[16];
788 sprintf(clk_name, "gpt%d_ick", i + 1);
789 timer->iclk = clk_get(NULL, clk_name);
790 sprintf(clk_name, "gpt%d_fck", i + 1);
791 timer->fclk = clk_get(NULL, clk_name);
792 }
Timo Teras77900a22006-06-26 16:16:12 -0700793#endif
794 }
795
796 return 0;
797}