blob: 1d706cf63ca0e697521d38ab814d9889a1b70435 [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;
Tony Lindgren140455f2010-02-12 12:26:48 -0800156#ifdef CONFIG_ARCH_OMAP2PLUS
Timo Teras77900a22006-06-26 16:16:12 -0700157 struct clk *iclk, *fclk;
158#endif
159 void __iomem *io_base;
160 unsigned reserved:1;
Timo Teras12583a72006-09-25 12:41:42 +0300161 unsigned enabled:1;
Richard Woodruff0f0d0802008-07-03 12:24:30 +0300162 unsigned posted:1;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100163};
164
Tony Lindgren882c0512010-02-12 12:26:46 -0800165static int dm_timer_count;
166
Timo Teras77900a22006-06-26 16:16:12 -0700167#ifdef CONFIG_ARCH_OMAP1
Syed Mohammed, Khasim471b3aa2007-06-21 21:48:07 -0700168static struct omap_dm_timer omap1_dm_timers[] = {
Timo Teras77900a22006-06-26 16:16:12 -0700169 { .phys_base = 0xfffb1400, .irq = INT_1610_GPTIMER1 },
170 { .phys_base = 0xfffb1c00, .irq = INT_1610_GPTIMER2 },
171 { .phys_base = 0xfffb2400, .irq = INT_1610_GPTIMER3 },
172 { .phys_base = 0xfffb2c00, .irq = INT_1610_GPTIMER4 },
173 { .phys_base = 0xfffb3400, .irq = INT_1610_GPTIMER5 },
174 { .phys_base = 0xfffb3c00, .irq = INT_1610_GPTIMER6 },
Matthew Percival53037f42007-01-25 16:24:29 -0800175 { .phys_base = 0xfffb7400, .irq = INT_1610_GPTIMER7 },
176 { .phys_base = 0xfffbd400, .irq = INT_1610_GPTIMER8 },
Timo Teras77900a22006-06-26 16:16:12 -0700177};
178
Tony Lindgren882c0512010-02-12 12:26:46 -0800179static const int omap1_dm_timer_count = ARRAY_SIZE(omap1_dm_timers);
Syed Mohammed, Khasim471b3aa2007-06-21 21:48:07 -0700180
Tony Lindgren882c0512010-02-12 12:26:46 -0800181#else
Syed Mohammed, Khasim471b3aa2007-06-21 21:48:07 -0700182#define omap1_dm_timers NULL
Tony Lindgren882c0512010-02-12 12:26:46 -0800183#define omap1_dm_timer_count 0
184#endif /* CONFIG_ARCH_OMAP1 */
Timo Terasfa4bb622006-09-25 12:41:35 +0300185
Tony Lindgren882c0512010-02-12 12:26:46 -0800186#ifdef CONFIG_ARCH_OMAP2
Syed Mohammed, Khasim471b3aa2007-06-21 21:48:07 -0700187static struct omap_dm_timer omap2_dm_timers[] = {
Timo Teras77900a22006-06-26 16:16:12 -0700188 { .phys_base = 0x48028000, .irq = INT_24XX_GPTIMER1 },
189 { .phys_base = 0x4802a000, .irq = INT_24XX_GPTIMER2 },
190 { .phys_base = 0x48078000, .irq = INT_24XX_GPTIMER3 },
191 { .phys_base = 0x4807a000, .irq = INT_24XX_GPTIMER4 },
192 { .phys_base = 0x4807c000, .irq = INT_24XX_GPTIMER5 },
193 { .phys_base = 0x4807e000, .irq = INT_24XX_GPTIMER6 },
194 { .phys_base = 0x48080000, .irq = INT_24XX_GPTIMER7 },
195 { .phys_base = 0x48082000, .irq = INT_24XX_GPTIMER8 },
196 { .phys_base = 0x48084000, .irq = INT_24XX_GPTIMER9 },
197 { .phys_base = 0x48086000, .irq = INT_24XX_GPTIMER10 },
198 { .phys_base = 0x48088000, .irq = INT_24XX_GPTIMER11 },
199 { .phys_base = 0x4808a000, .irq = INT_24XX_GPTIMER12 },
200};
201
Syed Mohammed, Khasim471b3aa2007-06-21 21:48:07 -0700202static const char *omap2_dm_source_names[] __initdata = {
Timo Teras83379c82006-06-26 16:16:23 -0700203 "sys_ck",
204 "func_32k_ck",
Syed Mohammed, Khasim471b3aa2007-06-21 21:48:07 -0700205 "alt_ck",
206 NULL
Timo Teras83379c82006-06-26 16:16:23 -0700207};
208
Santosh Shilimkaraea2a5b2009-05-25 11:08:36 -0700209static struct clk *omap2_dm_source_clocks[3];
Tony Lindgren882c0512010-02-12 12:26:46 -0800210static const int omap2_dm_timer_count = ARRAY_SIZE(omap2_dm_timers);
Timo Teras83379c82006-06-26 16:16:23 -0700211
Tony Lindgren882c0512010-02-12 12:26:46 -0800212#else
Syed Mohammed, Khasimce2df9c2007-06-25 22:55:39 -0700213#define omap2_dm_timers NULL
Tony Lindgren882c0512010-02-12 12:26:46 -0800214#define omap2_dm_timer_count 0
Syed Mohammed, Khasimce2df9c2007-06-25 22:55:39 -0700215#define omap2_dm_source_names NULL
216#define omap2_dm_source_clocks NULL
Tony Lindgren882c0512010-02-12 12:26:46 -0800217#endif /* CONFIG_ARCH_OMAP2 */
Syed Mohammed, Khasimce2df9c2007-06-25 22:55:39 -0700218
Tony Lindgren882c0512010-02-12 12:26:46 -0800219#ifdef CONFIG_ARCH_OMAP3
Syed Mohammed, Khasimce2df9c2007-06-25 22:55:39 -0700220static struct omap_dm_timer omap3_dm_timers[] = {
221 { .phys_base = 0x48318000, .irq = INT_24XX_GPTIMER1 },
222 { .phys_base = 0x49032000, .irq = INT_24XX_GPTIMER2 },
223 { .phys_base = 0x49034000, .irq = INT_24XX_GPTIMER3 },
224 { .phys_base = 0x49036000, .irq = INT_24XX_GPTIMER4 },
225 { .phys_base = 0x49038000, .irq = INT_24XX_GPTIMER5 },
226 { .phys_base = 0x4903A000, .irq = INT_24XX_GPTIMER6 },
227 { .phys_base = 0x4903C000, .irq = INT_24XX_GPTIMER7 },
228 { .phys_base = 0x4903E000, .irq = INT_24XX_GPTIMER8 },
229 { .phys_base = 0x49040000, .irq = INT_24XX_GPTIMER9 },
230 { .phys_base = 0x48086000, .irq = INT_24XX_GPTIMER10 },
231 { .phys_base = 0x48088000, .irq = INT_24XX_GPTIMER11 },
Paul Walmsley9198a402009-04-23 21:11:08 -0600232 { .phys_base = 0x48304000, .irq = INT_34XX_GPT12_IRQ },
Syed Mohammed, Khasimce2df9c2007-06-25 22:55:39 -0700233};
234
235static const char *omap3_dm_source_names[] __initdata = {
236 "sys_ck",
237 "omap_32k_fck",
238 NULL
239};
240
Santosh Shilimkaraea2a5b2009-05-25 11:08:36 -0700241static struct clk *omap3_dm_source_clocks[2];
Tony Lindgren882c0512010-02-12 12:26:46 -0800242static const int omap3_dm_timer_count = ARRAY_SIZE(omap3_dm_timers);
Syed Mohammed, Khasimce2df9c2007-06-25 22:55:39 -0700243
Tony Lindgren882c0512010-02-12 12:26:46 -0800244#else
Santosh Shilimkar44169072009-05-28 14:16:04 -0700245#define omap3_dm_timers NULL
Tony Lindgren882c0512010-02-12 12:26:46 -0800246#define omap3_dm_timer_count 0
Santosh Shilimkar44169072009-05-28 14:16:04 -0700247#define omap3_dm_source_names NULL
248#define omap3_dm_source_clocks NULL
Tony Lindgren882c0512010-02-12 12:26:46 -0800249#endif /* CONFIG_ARCH_OMAP3 */
Santosh Shilimkar44169072009-05-28 14:16:04 -0700250
Tony Lindgren882c0512010-02-12 12:26:46 -0800251#ifdef CONFIG_ARCH_OMAP4
Santosh Shilimkar44169072009-05-28 14:16:04 -0700252static struct omap_dm_timer omap4_dm_timers[] = {
Santosh Shilimkar5772ca72010-02-18 03:14:12 +0530253 { .phys_base = 0x4a318000, .irq = OMAP44XX_IRQ_GPT1 },
254 { .phys_base = 0x48032000, .irq = OMAP44XX_IRQ_GPT2 },
255 { .phys_base = 0x48034000, .irq = OMAP44XX_IRQ_GPT3 },
256 { .phys_base = 0x48036000, .irq = OMAP44XX_IRQ_GPT4 },
257 { .phys_base = 0x40138000, .irq = OMAP44XX_IRQ_GPT5 },
258 { .phys_base = 0x4013a000, .irq = OMAP44XX_IRQ_GPT6 },
259 { .phys_base = 0x4013a000, .irq = OMAP44XX_IRQ_GPT7 },
260 { .phys_base = 0x4013e000, .irq = OMAP44XX_IRQ_GPT8 },
261 { .phys_base = 0x4803e000, .irq = OMAP44XX_IRQ_GPT9 },
262 { .phys_base = 0x48086000, .irq = OMAP44XX_IRQ_GPT10 },
263 { .phys_base = 0x48088000, .irq = OMAP44XX_IRQ_GPT11 },
264 { .phys_base = 0x4a320000, .irq = OMAP44XX_IRQ_GPT12 },
Santosh Shilimkar44169072009-05-28 14:16:04 -0700265};
266static const char *omap4_dm_source_names[] __initdata = {
Rajendra Nayak1dc993b2010-05-18 20:24:00 -0600267 "sys_clkin_ck",
268 "sys_32k_ck",
Santosh Shilimkar44169072009-05-28 14:16:04 -0700269 NULL
270};
271static struct clk *omap4_dm_source_clocks[2];
Tony Lindgren882c0512010-02-12 12:26:46 -0800272static const int omap4_dm_timer_count = ARRAY_SIZE(omap4_dm_timers);
Santosh Shilimkar44169072009-05-28 14:16:04 -0700273
Timo Teras77900a22006-06-26 16:16:12 -0700274#else
Tony Lindgren882c0512010-02-12 12:26:46 -0800275#define omap4_dm_timers NULL
276#define omap4_dm_timer_count 0
277#define omap4_dm_source_names NULL
278#define omap4_dm_source_clocks NULL
279#endif /* CONFIG_ARCH_OMAP4 */
Timo Teras77900a22006-06-26 16:16:12 -0700280
Syed Mohammed, Khasim471b3aa2007-06-21 21:48:07 -0700281static struct omap_dm_timer *dm_timers;
Santosh Shilimkaraea2a5b2009-05-25 11:08:36 -0700282static const char **dm_source_names;
Syed Mohammed, Khasim471b3aa2007-06-21 21:48:07 -0700283static struct clk **dm_source_clocks;
284
Tony Lindgren92105bb2005-09-07 17:20:26 +0100285static spinlock_t dm_timer_lock;
286
Richard Woodruff0f0d0802008-07-03 12:24:30 +0300287/*
288 * Reads timer registers in posted and non-posted mode. The posted mode bit
289 * is encoded in reg. Note that in posted mode write pending bit must be
290 * checked. Otherwise a read of a non completed write will produce an error.
291 */
292static inline u32 omap_dm_timer_read_reg(struct omap_dm_timer *timer, u32 reg)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100293{
Richard Woodruff0f0d0802008-07-03 12:24:30 +0300294 if (timer->posted)
295 while (readl(timer->io_base + (OMAP_TIMER_WRITE_PEND_REG & 0xff))
296 & (reg >> WPSHIFT))
297 cpu_relax();
298 return readl(timer->io_base + (reg & 0xff));
Timo Teras77900a22006-06-26 16:16:12 -0700299}
300
Richard Woodruff0f0d0802008-07-03 12:24:30 +0300301/*
302 * Writes timer registers in posted and non-posted mode. The posted mode bit
303 * is encoded in reg. Note that in posted mode the write pending bit must be
304 * checked. Otherwise a write on a register which has a pending write will be
305 * lost.
306 */
307static void omap_dm_timer_write_reg(struct omap_dm_timer *timer, u32 reg,
308 u32 value)
Timo Teras77900a22006-06-26 16:16:12 -0700309{
Richard Woodruff0f0d0802008-07-03 12:24:30 +0300310 if (timer->posted)
311 while (readl(timer->io_base + (OMAP_TIMER_WRITE_PEND_REG & 0xff))
312 & (reg >> WPSHIFT))
313 cpu_relax();
314 writel(value, timer->io_base + (reg & 0xff));
Tony Lindgren92105bb2005-09-07 17:20:26 +0100315}
316
Timo Teras77900a22006-06-26 16:16:12 -0700317static void omap_dm_timer_wait_for_reset(struct omap_dm_timer *timer)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100318{
Timo Teras77900a22006-06-26 16:16:12 -0700319 int c;
320
321 c = 0;
322 while (!(omap_dm_timer_read_reg(timer, OMAP_TIMER_SYS_STAT_REG) & 1)) {
323 c++;
324 if (c > 100000) {
325 printk(KERN_ERR "Timer failed to reset\n");
326 return;
327 }
328 }
Tony Lindgren92105bb2005-09-07 17:20:26 +0100329}
330
Timo Teras77900a22006-06-26 16:16:12 -0700331static void omap_dm_timer_reset(struct omap_dm_timer *timer)
332{
333 u32 l;
334
Juha Yrjola39020842006-09-25 12:41:44 +0300335 if (!cpu_class_is_omap2() || timer != &dm_timers[0]) {
Timo Terase32f7ec2006-06-26 16:16:13 -0700336 omap_dm_timer_write_reg(timer, OMAP_TIMER_IF_CTRL_REG, 0x06);
337 omap_dm_timer_wait_for_reset(timer);
338 }
Timo Teras12583a72006-09-25 12:41:42 +0300339 omap_dm_timer_set_source(timer, OMAP_TIMER_SRC_32_KHZ);
Timo Teras77900a22006-06-26 16:16:12 -0700340
Timo Teras77900a22006-06-26 16:16:12 -0700341 l = omap_dm_timer_read_reg(timer, OMAP_TIMER_OCP_CFG_REG);
Richard Woodruff0f0d0802008-07-03 12:24:30 +0300342 l |= 0x02 << 3; /* Set to smart-idle mode */
343 l |= 0x2 << 8; /* Set clock activity to perserve f-clock on idle */
Juha Yrjola39020842006-09-25 12:41:44 +0300344
Richard Woodruff0f0d0802008-07-03 12:24:30 +0300345 /*
Kevin Hilman219c5b92009-04-23 21:11:08 -0600346 * Enable wake-up on OMAP2 CPUs.
Richard Woodruff0f0d0802008-07-03 12:24:30 +0300347 */
Kevin Hilman219c5b92009-04-23 21:11:08 -0600348 if (cpu_class_is_omap2())
Juha Yrjola39020842006-09-25 12:41:44 +0300349 l |= 1 << 2;
Timo Teras77900a22006-06-26 16:16:12 -0700350 omap_dm_timer_write_reg(timer, OMAP_TIMER_OCP_CFG_REG, l);
Richard Woodruff0f0d0802008-07-03 12:24:30 +0300351
352 /* Match hardware reset default of posted mode */
353 omap_dm_timer_write_reg(timer, OMAP_TIMER_IF_CTRL_REG,
354 OMAP_TIMER_CTRL_POSTED);
355 timer->posted = 1;
Timo Teras77900a22006-06-26 16:16:12 -0700356}
357
Timo Teras83379c82006-06-26 16:16:23 -0700358static void omap_dm_timer_prepare(struct omap_dm_timer *timer)
Timo Teras77900a22006-06-26 16:16:12 -0700359{
Timo Teras12583a72006-09-25 12:41:42 +0300360 omap_dm_timer_enable(timer);
Timo Teras77900a22006-06-26 16:16:12 -0700361 omap_dm_timer_reset(timer);
362}
363
364struct omap_dm_timer *omap_dm_timer_request(void)
365{
366 struct omap_dm_timer *timer = NULL;
367 unsigned long flags;
368 int i;
369
370 spin_lock_irqsave(&dm_timer_lock, flags);
371 for (i = 0; i < dm_timer_count; i++) {
372 if (dm_timers[i].reserved)
373 continue;
374
375 timer = &dm_timers[i];
Timo Teras83379c82006-06-26 16:16:23 -0700376 timer->reserved = 1;
Timo Teras77900a22006-06-26 16:16:12 -0700377 break;
378 }
379 spin_unlock_irqrestore(&dm_timer_lock, flags);
380
Timo Teras83379c82006-06-26 16:16:23 -0700381 if (timer != NULL)
382 omap_dm_timer_prepare(timer);
383
Timo Teras77900a22006-06-26 16:16:12 -0700384 return timer;
385}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700386EXPORT_SYMBOL_GPL(omap_dm_timer_request);
Timo Teras77900a22006-06-26 16:16:12 -0700387
388struct omap_dm_timer *omap_dm_timer_request_specific(int id)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100389{
390 struct omap_dm_timer *timer;
Timo Teras77900a22006-06-26 16:16:12 -0700391 unsigned long flags;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100392
Timo Teras77900a22006-06-26 16:16:12 -0700393 spin_lock_irqsave(&dm_timer_lock, flags);
394 if (id <= 0 || id > dm_timer_count || dm_timers[id-1].reserved) {
395 spin_unlock_irqrestore(&dm_timer_lock, flags);
396 printk("BUG: warning at %s:%d/%s(): unable to get timer %d\n",
Harvey Harrison8e86f422008-03-04 15:08:02 -0800397 __FILE__, __LINE__, __func__, id);
Timo Teras77900a22006-06-26 16:16:12 -0700398 dump_stack();
399 return NULL;
400 }
Tony Lindgren92105bb2005-09-07 17:20:26 +0100401
Timo Teras77900a22006-06-26 16:16:12 -0700402 timer = &dm_timers[id-1];
Timo Teras83379c82006-06-26 16:16:23 -0700403 timer->reserved = 1;
Timo Teras77900a22006-06-26 16:16:12 -0700404 spin_unlock_irqrestore(&dm_timer_lock, flags);
405
Timo Teras83379c82006-06-26 16:16:23 -0700406 omap_dm_timer_prepare(timer);
407
Timo Teras77900a22006-06-26 16:16:12 -0700408 return timer;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100409}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700410EXPORT_SYMBOL_GPL(omap_dm_timer_request_specific);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100411
Timo Teras77900a22006-06-26 16:16:12 -0700412void omap_dm_timer_free(struct omap_dm_timer *timer)
413{
Timo Teras12583a72006-09-25 12:41:42 +0300414 omap_dm_timer_enable(timer);
Timo Teras77900a22006-06-26 16:16:12 -0700415 omap_dm_timer_reset(timer);
Timo Teras12583a72006-09-25 12:41:42 +0300416 omap_dm_timer_disable(timer);
Timo Terasfa4bb622006-09-25 12:41:35 +0300417
Timo Teras77900a22006-06-26 16:16:12 -0700418 WARN_ON(!timer->reserved);
419 timer->reserved = 0;
420}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700421EXPORT_SYMBOL_GPL(omap_dm_timer_free);
Timo Teras77900a22006-06-26 16:16:12 -0700422
Timo Teras12583a72006-09-25 12:41:42 +0300423void omap_dm_timer_enable(struct omap_dm_timer *timer)
424{
425 if (timer->enabled)
426 return;
427
Tony Lindgren882c0512010-02-12 12:26:46 -0800428#ifdef CONFIG_ARCH_OMAP2PLUS
429 if (cpu_class_is_omap2()) {
430 clk_enable(timer->fclk);
431 clk_enable(timer->iclk);
432 }
433#endif
Timo Teras12583a72006-09-25 12:41:42 +0300434
435 timer->enabled = 1;
436}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700437EXPORT_SYMBOL_GPL(omap_dm_timer_enable);
Timo Teras12583a72006-09-25 12:41:42 +0300438
439void omap_dm_timer_disable(struct omap_dm_timer *timer)
440{
441 if (!timer->enabled)
442 return;
443
Tony Lindgren882c0512010-02-12 12:26:46 -0800444#ifdef CONFIG_ARCH_OMAP2PLUS
445 if (cpu_class_is_omap2()) {
446 clk_disable(timer->iclk);
447 clk_disable(timer->fclk);
448 }
449#endif
Timo Teras12583a72006-09-25 12:41:42 +0300450
451 timer->enabled = 0;
452}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700453EXPORT_SYMBOL_GPL(omap_dm_timer_disable);
Timo Teras12583a72006-09-25 12:41:42 +0300454
Timo Teras77900a22006-06-26 16:16:12 -0700455int omap_dm_timer_get_irq(struct omap_dm_timer *timer)
456{
457 return timer->irq;
458}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700459EXPORT_SYMBOL_GPL(omap_dm_timer_get_irq);
Timo Teras77900a22006-06-26 16:16:12 -0700460
461#if defined(CONFIG_ARCH_OMAP1)
462
Tony Lindgrena569c6e2006-04-02 17:46:21 +0100463/**
464 * omap_dm_timer_modify_idlect_mask - Check if any running timers use ARMXOR
465 * @inputmask: current value of idlect mask
466 */
467__u32 omap_dm_timer_modify_idlect_mask(__u32 inputmask)
468{
Timo Teras77900a22006-06-26 16:16:12 -0700469 int i;
Tony Lindgrena569c6e2006-04-02 17:46:21 +0100470
471 /* If ARMXOR cannot be idled this function call is unnecessary */
472 if (!(inputmask & (1 << 1)))
473 return inputmask;
474
475 /* If any active timer is using ARMXOR return modified mask */
Timo Teras77900a22006-06-26 16:16:12 -0700476 for (i = 0; i < dm_timer_count; i++) {
477 u32 l;
478
Tony Lindgren35912c72006-07-01 19:56:42 +0100479 l = omap_dm_timer_read_reg(&dm_timers[i], OMAP_TIMER_CTRL_REG);
Timo Teras77900a22006-06-26 16:16:12 -0700480 if (l & OMAP_TIMER_CTRL_ST) {
481 if (((omap_readl(MOD_CONF_CTRL_1) >> (i * 2)) & 0x03) == 0)
Tony Lindgrena569c6e2006-04-02 17:46:21 +0100482 inputmask &= ~(1 << 1);
483 else
484 inputmask &= ~(1 << 2);
485 }
Timo Teras77900a22006-06-26 16:16:12 -0700486 }
Tony Lindgrena569c6e2006-04-02 17:46:21 +0100487
488 return inputmask;
489}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700490EXPORT_SYMBOL_GPL(omap_dm_timer_modify_idlect_mask);
Tony Lindgrena569c6e2006-04-02 17:46:21 +0100491
Tony Lindgren140455f2010-02-12 12:26:48 -0800492#else
Timo Teras77900a22006-06-26 16:16:12 -0700493
494struct clk *omap_dm_timer_get_fclk(struct omap_dm_timer *timer)
495{
Timo Terasfa4bb622006-09-25 12:41:35 +0300496 return timer->fclk;
Timo Teras77900a22006-06-26 16:16:12 -0700497}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700498EXPORT_SYMBOL_GPL(omap_dm_timer_get_fclk);
Timo Teras77900a22006-06-26 16:16:12 -0700499
500__u32 omap_dm_timer_modify_idlect_mask(__u32 inputmask)
501{
502 BUG();
Dirk Behme21218802006-12-06 17:14:00 -0800503
504 return 0;
Timo Teras77900a22006-06-26 16:16:12 -0700505}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700506EXPORT_SYMBOL_GPL(omap_dm_timer_modify_idlect_mask);
Timo Teras77900a22006-06-26 16:16:12 -0700507
508#endif
509
510void omap_dm_timer_trigger(struct omap_dm_timer *timer)
511{
512 omap_dm_timer_write_reg(timer, OMAP_TIMER_TRIGGER_REG, 0);
513}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700514EXPORT_SYMBOL_GPL(omap_dm_timer_trigger);
Timo Teras77900a22006-06-26 16:16:12 -0700515
516void omap_dm_timer_start(struct omap_dm_timer *timer)
517{
518 u32 l;
519
520 l = omap_dm_timer_read_reg(timer, OMAP_TIMER_CTRL_REG);
521 if (!(l & OMAP_TIMER_CTRL_ST)) {
522 l |= OMAP_TIMER_CTRL_ST;
523 omap_dm_timer_write_reg(timer, OMAP_TIMER_CTRL_REG, l);
524 }
525}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700526EXPORT_SYMBOL_GPL(omap_dm_timer_start);
Timo Teras77900a22006-06-26 16:16:12 -0700527
528void omap_dm_timer_stop(struct omap_dm_timer *timer)
529{
530 u32 l;
531
532 l = omap_dm_timer_read_reg(timer, OMAP_TIMER_CTRL_REG);
533 if (l & OMAP_TIMER_CTRL_ST) {
534 l &= ~0x1;
535 omap_dm_timer_write_reg(timer, OMAP_TIMER_CTRL_REG, l);
Tony Lindgren140455f2010-02-12 12:26:48 -0800536#ifdef CONFIG_ARCH_OMAP2PLUS
Tero Kristo5c3db362009-10-23 19:03:47 +0300537 /* Readback to make sure write has completed */
538 omap_dm_timer_read_reg(timer, OMAP_TIMER_CTRL_REG);
539 /*
540 * Wait for functional clock period x 3.5 to make sure that
541 * timer is stopped
542 */
543 udelay(3500000 / clk_get_rate(timer->fclk) + 1);
Tero Kristo5c3db362009-10-23 19:03:47 +0300544#endif
Timo Teras77900a22006-06-26 16:16:12 -0700545 }
Tero Kristo856f1912010-06-09 13:53:05 +0300546 /* Ack possibly pending interrupt */
547 omap_dm_timer_write_reg(timer, OMAP_TIMER_STAT_REG,
548 OMAP_TIMER_INT_OVERFLOW);
Timo Teras77900a22006-06-26 16:16:12 -0700549}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700550EXPORT_SYMBOL_GPL(omap_dm_timer_stop);
Timo Teras77900a22006-06-26 16:16:12 -0700551
552#ifdef CONFIG_ARCH_OMAP1
Tony Lindgrena569c6e2006-04-02 17:46:21 +0100553
Paul Walmsleyf2480762009-04-23 21:11:10 -0600554int omap_dm_timer_set_source(struct omap_dm_timer *timer, int source)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100555{
556 int n = (timer - dm_timers) << 1;
557 u32 l;
558
559 l = omap_readl(MOD_CONF_CTRL_1) & ~(0x03 << n);
560 l |= source << n;
561 omap_writel(l, MOD_CONF_CTRL_1);
Paul Walmsleyf2480762009-04-23 21:11:10 -0600562
563 return 0;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100564}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700565EXPORT_SYMBOL_GPL(omap_dm_timer_set_source);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100566
Timo Teras77900a22006-06-26 16:16:12 -0700567#else
Tony Lindgren92105bb2005-09-07 17:20:26 +0100568
Paul Walmsleyf2480762009-04-23 21:11:10 -0600569int omap_dm_timer_set_source(struct omap_dm_timer *timer, int source)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100570{
Paul Walmsleyf2480762009-04-23 21:11:10 -0600571 int ret = -EINVAL;
572
Timo Teras77900a22006-06-26 16:16:12 -0700573 if (source < 0 || source >= 3)
Paul Walmsleyf2480762009-04-23 21:11:10 -0600574 return -EINVAL;
Timo Teras77900a22006-06-26 16:16:12 -0700575
Timo Teras77900a22006-06-26 16:16:12 -0700576 clk_disable(timer->fclk);
Paul Walmsleyf2480762009-04-23 21:11:10 -0600577 ret = clk_set_parent(timer->fclk, dm_source_clocks[source]);
Timo Teras77900a22006-06-26 16:16:12 -0700578 clk_enable(timer->fclk);
Timo Teras77900a22006-06-26 16:16:12 -0700579
Paul Walmsleyf2480762009-04-23 21:11:10 -0600580 /*
581 * When the functional clock disappears, too quick writes seem
582 * to cause an abort. XXX Is this still necessary?
583 */
Santosh Shilimkare7193cc2010-09-16 18:44:48 +0530584 __delay(300000);
Paul Walmsleyf2480762009-04-23 21:11:10 -0600585
586 return ret;
Timo Teras77900a22006-06-26 16:16:12 -0700587}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700588EXPORT_SYMBOL_GPL(omap_dm_timer_set_source);
Timo Teras77900a22006-06-26 16:16:12 -0700589
590#endif
591
592void omap_dm_timer_set_load(struct omap_dm_timer *timer, int autoreload,
593 unsigned int load)
594{
595 u32 l;
596
597 l = omap_dm_timer_read_reg(timer, OMAP_TIMER_CTRL_REG);
598 if (autoreload)
599 l |= OMAP_TIMER_CTRL_AR;
600 else
601 l &= ~OMAP_TIMER_CTRL_AR;
602 omap_dm_timer_write_reg(timer, OMAP_TIMER_CTRL_REG, l);
603 omap_dm_timer_write_reg(timer, OMAP_TIMER_LOAD_REG, load);
Richard Woodruff0f0d0802008-07-03 12:24:30 +0300604
Timo Teras77900a22006-06-26 16:16:12 -0700605 omap_dm_timer_write_reg(timer, OMAP_TIMER_TRIGGER_REG, 0);
606}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700607EXPORT_SYMBOL_GPL(omap_dm_timer_set_load);
Timo Teras77900a22006-06-26 16:16:12 -0700608
Richard Woodruff3fddd092008-07-03 12:24:30 +0300609/* Optimized set_load which removes costly spin wait in timer_start */
610void omap_dm_timer_set_load_start(struct omap_dm_timer *timer, int autoreload,
611 unsigned int load)
612{
613 u32 l;
614
615 l = omap_dm_timer_read_reg(timer, OMAP_TIMER_CTRL_REG);
Paul Walmsley64ce2902008-12-10 17:36:34 -0800616 if (autoreload) {
Richard Woodruff3fddd092008-07-03 12:24:30 +0300617 l |= OMAP_TIMER_CTRL_AR;
Paul Walmsley64ce2902008-12-10 17:36:34 -0800618 omap_dm_timer_write_reg(timer, OMAP_TIMER_LOAD_REG, load);
619 } else {
Richard Woodruff3fddd092008-07-03 12:24:30 +0300620 l &= ~OMAP_TIMER_CTRL_AR;
Paul Walmsley64ce2902008-12-10 17:36:34 -0800621 }
Richard Woodruff3fddd092008-07-03 12:24:30 +0300622 l |= OMAP_TIMER_CTRL_ST;
623
624 omap_dm_timer_write_reg(timer, OMAP_TIMER_COUNTER_REG, load);
Richard Woodruff3fddd092008-07-03 12:24:30 +0300625 omap_dm_timer_write_reg(timer, OMAP_TIMER_CTRL_REG, l);
626}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700627EXPORT_SYMBOL_GPL(omap_dm_timer_set_load_start);
Richard Woodruff3fddd092008-07-03 12:24:30 +0300628
Timo Teras77900a22006-06-26 16:16:12 -0700629void omap_dm_timer_set_match(struct omap_dm_timer *timer, int enable,
630 unsigned int match)
631{
632 u32 l;
633
634 l = omap_dm_timer_read_reg(timer, OMAP_TIMER_CTRL_REG);
Timo Teras83379c82006-06-26 16:16:23 -0700635 if (enable)
Timo Teras77900a22006-06-26 16:16:12 -0700636 l |= OMAP_TIMER_CTRL_CE;
637 else
638 l &= ~OMAP_TIMER_CTRL_CE;
639 omap_dm_timer_write_reg(timer, OMAP_TIMER_CTRL_REG, l);
640 omap_dm_timer_write_reg(timer, OMAP_TIMER_MATCH_REG, match);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100641}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700642EXPORT_SYMBOL_GPL(omap_dm_timer_set_match);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100643
Timo Teras77900a22006-06-26 16:16:12 -0700644void omap_dm_timer_set_pwm(struct omap_dm_timer *timer, int def_on,
645 int toggle, int trigger)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100646{
Timo Teras77900a22006-06-26 16:16:12 -0700647 u32 l;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100648
Timo Teras77900a22006-06-26 16:16:12 -0700649 l = omap_dm_timer_read_reg(timer, OMAP_TIMER_CTRL_REG);
650 l &= ~(OMAP_TIMER_CTRL_GPOCFG | OMAP_TIMER_CTRL_SCPWM |
651 OMAP_TIMER_CTRL_PT | (0x03 << 10));
652 if (def_on)
653 l |= OMAP_TIMER_CTRL_SCPWM;
654 if (toggle)
655 l |= OMAP_TIMER_CTRL_PT;
656 l |= trigger << 10;
657 omap_dm_timer_write_reg(timer, OMAP_TIMER_CTRL_REG, l);
658}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700659EXPORT_SYMBOL_GPL(omap_dm_timer_set_pwm);
Timo Teras77900a22006-06-26 16:16:12 -0700660
661void omap_dm_timer_set_prescaler(struct omap_dm_timer *timer, int prescaler)
662{
663 u32 l;
664
665 l = omap_dm_timer_read_reg(timer, OMAP_TIMER_CTRL_REG);
666 l &= ~(OMAP_TIMER_CTRL_PRE | (0x07 << 2));
667 if (prescaler >= 0x00 && prescaler <= 0x07) {
668 l |= OMAP_TIMER_CTRL_PRE;
669 l |= prescaler << 2;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100670 }
Timo Teras77900a22006-06-26 16:16:12 -0700671 omap_dm_timer_write_reg(timer, OMAP_TIMER_CTRL_REG, l);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100672}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700673EXPORT_SYMBOL_GPL(omap_dm_timer_set_prescaler);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100674
675void omap_dm_timer_set_int_enable(struct omap_dm_timer *timer,
Timo Teras77900a22006-06-26 16:16:12 -0700676 unsigned int value)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100677{
678 omap_dm_timer_write_reg(timer, OMAP_TIMER_INT_EN_REG, value);
Juha Yrjola39020842006-09-25 12:41:44 +0300679 omap_dm_timer_write_reg(timer, OMAP_TIMER_WAKEUP_EN_REG, value);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100680}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700681EXPORT_SYMBOL_GPL(omap_dm_timer_set_int_enable);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100682
683unsigned int omap_dm_timer_read_status(struct omap_dm_timer *timer)
684{
Timo Terasfa4bb622006-09-25 12:41:35 +0300685 unsigned int l;
686
Timo Terasfa4bb622006-09-25 12:41:35 +0300687 l = omap_dm_timer_read_reg(timer, OMAP_TIMER_STAT_REG);
Timo Terasfa4bb622006-09-25 12:41:35 +0300688
689 return l;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100690}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700691EXPORT_SYMBOL_GPL(omap_dm_timer_read_status);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100692
693void omap_dm_timer_write_status(struct omap_dm_timer *timer, unsigned int value)
694{
695 omap_dm_timer_write_reg(timer, OMAP_TIMER_STAT_REG, value);
696}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700697EXPORT_SYMBOL_GPL(omap_dm_timer_write_status);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100698
Tony Lindgren92105bb2005-09-07 17:20:26 +0100699unsigned int omap_dm_timer_read_counter(struct omap_dm_timer *timer)
700{
Timo Terasfa4bb622006-09-25 12:41:35 +0300701 unsigned int l;
702
Timo Terasfa4bb622006-09-25 12:41:35 +0300703 l = omap_dm_timer_read_reg(timer, OMAP_TIMER_COUNTER_REG);
Timo Terasfa4bb622006-09-25 12:41:35 +0300704
705 return l;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100706}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700707EXPORT_SYMBOL_GPL(omap_dm_timer_read_counter);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100708
Timo Teras83379c82006-06-26 16:16:23 -0700709void omap_dm_timer_write_counter(struct omap_dm_timer *timer, unsigned int value)
710{
Timo Terasfa4bb622006-09-25 12:41:35 +0300711 omap_dm_timer_write_reg(timer, OMAP_TIMER_COUNTER_REG, value);
Timo Teras83379c82006-06-26 16:16:23 -0700712}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700713EXPORT_SYMBOL_GPL(omap_dm_timer_write_counter);
Timo Teras83379c82006-06-26 16:16:23 -0700714
Timo Teras77900a22006-06-26 16:16:12 -0700715int omap_dm_timers_active(void)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100716{
Timo Teras77900a22006-06-26 16:16:12 -0700717 int i;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100718
Timo Teras77900a22006-06-26 16:16:12 -0700719 for (i = 0; i < dm_timer_count; i++) {
720 struct omap_dm_timer *timer;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100721
Timo Teras77900a22006-06-26 16:16:12 -0700722 timer = &dm_timers[i];
Timo Teras12583a72006-09-25 12:41:42 +0300723
724 if (!timer->enabled)
725 continue;
726
Timo Teras77900a22006-06-26 16:16:12 -0700727 if (omap_dm_timer_read_reg(timer, OMAP_TIMER_CTRL_REG) &
Timo Terasfa4bb622006-09-25 12:41:35 +0300728 OMAP_TIMER_CTRL_ST) {
Timo Teras77900a22006-06-26 16:16:12 -0700729 return 1;
Timo Terasfa4bb622006-09-25 12:41:35 +0300730 }
Tony Lindgren92105bb2005-09-07 17:20:26 +0100731 }
Tony Lindgren92105bb2005-09-07 17:20:26 +0100732 return 0;
733}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700734EXPORT_SYMBOL_GPL(omap_dm_timers_active);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100735
Syed Mohammed, Khasim471b3aa2007-06-21 21:48:07 -0700736int __init omap_dm_timer_init(void)
Timo Teras77900a22006-06-26 16:16:12 -0700737{
738 struct omap_dm_timer *timer;
Tony Lindgren3566fc62009-10-19 15:25:18 -0700739 int i, map_size = SZ_8K; /* Module 4KB + L4 4KB except on omap1 */
Timo Teras77900a22006-06-26 16:16:12 -0700740
Syed Mohammed, Khasimce2df9c2007-06-25 22:55:39 -0700741 if (!(cpu_is_omap16xx() || cpu_class_is_omap2()))
Timo Teras77900a22006-06-26 16:16:12 -0700742 return -ENODEV;
743
744 spin_lock_init(&dm_timer_lock);
Syed Mohammed, Khasim471b3aa2007-06-21 21:48:07 -0700745
Tony Lindgren3566fc62009-10-19 15:25:18 -0700746 if (cpu_class_is_omap1()) {
Syed Mohammed, Khasim471b3aa2007-06-21 21:48:07 -0700747 dm_timers = omap1_dm_timers;
Tony Lindgren882c0512010-02-12 12:26:46 -0800748 dm_timer_count = omap1_dm_timer_count;
Tony Lindgren3566fc62009-10-19 15:25:18 -0700749 map_size = SZ_2K;
750 } else if (cpu_is_omap24xx()) {
Syed Mohammed, Khasim471b3aa2007-06-21 21:48:07 -0700751 dm_timers = omap2_dm_timers;
Tony Lindgren882c0512010-02-12 12:26:46 -0800752 dm_timer_count = omap2_dm_timer_count;
Santosh Shilimkaraea2a5b2009-05-25 11:08:36 -0700753 dm_source_names = omap2_dm_source_names;
754 dm_source_clocks = omap2_dm_source_clocks;
Syed Mohammed, Khasimce2df9c2007-06-25 22:55:39 -0700755 } else if (cpu_is_omap34xx()) {
756 dm_timers = omap3_dm_timers;
Tony Lindgren882c0512010-02-12 12:26:46 -0800757 dm_timer_count = omap3_dm_timer_count;
Santosh Shilimkaraea2a5b2009-05-25 11:08:36 -0700758 dm_source_names = omap3_dm_source_names;
759 dm_source_clocks = omap3_dm_source_clocks;
Santosh Shilimkar44169072009-05-28 14:16:04 -0700760 } else if (cpu_is_omap44xx()) {
761 dm_timers = omap4_dm_timers;
Tony Lindgren882c0512010-02-12 12:26:46 -0800762 dm_timer_count = omap4_dm_timer_count;
Santosh Shilimkar44169072009-05-28 14:16:04 -0700763 dm_source_names = omap4_dm_source_names;
764 dm_source_clocks = omap4_dm_source_clocks;
Timo Teras83379c82006-06-26 16:16:23 -0700765 }
Syed Mohammed, Khasim471b3aa2007-06-21 21:48:07 -0700766
767 if (cpu_class_is_omap2())
768 for (i = 0; dm_source_names[i] != NULL; i++)
769 dm_source_clocks[i] = clk_get(NULL, dm_source_names[i]);
770
Syed Mohammed Khasim56a25642006-12-06 17:14:08 -0800771 if (cpu_is_omap243x())
772 dm_timers[0].phys_base = 0x49018000;
Timo Teras83379c82006-06-26 16:16:23 -0700773
Timo Teras77900a22006-06-26 16:16:12 -0700774 for (i = 0; i < dm_timer_count; i++) {
Timo Teras77900a22006-06-26 16:16:12 -0700775 timer = &dm_timers[i];
Tony Lindgren3566fc62009-10-19 15:25:18 -0700776
777 /* Static mapping, never released */
778 timer->io_base = ioremap(timer->phys_base, map_size);
779 BUG_ON(!timer->io_base);
780
Tony Lindgren140455f2010-02-12 12:26:48 -0800781#ifdef CONFIG_ARCH_OMAP2PLUS
Syed Mohammed, Khasim471b3aa2007-06-21 21:48:07 -0700782 if (cpu_class_is_omap2()) {
783 char clk_name[16];
784 sprintf(clk_name, "gpt%d_ick", i + 1);
785 timer->iclk = clk_get(NULL, clk_name);
786 sprintf(clk_name, "gpt%d_fck", i + 1);
787 timer->fclk = clk_get(NULL, clk_name);
788 }
Timo Teras77900a22006-06-26 16:16:12 -0700789#endif
790 }
791
792 return 0;
793}