blob: ee9f6ebba29b8a5479353e9c56bc9f839d8e0183 [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
Tero Kristo4ce1e5e2011-03-10 03:50:54 -0700345 /* Enable autoidle on OMAP2 / OMAP3 */
346 if (cpu_is_omap24xx() || cpu_is_omap34xx())
347 l |= 0x1 << 0;
348
Richard Woodruff0f0d0802008-07-03 12:24:30 +0300349 /*
Kevin Hilman219c5b92009-04-23 21:11:08 -0600350 * Enable wake-up on OMAP2 CPUs.
Richard Woodruff0f0d0802008-07-03 12:24:30 +0300351 */
Kevin Hilman219c5b92009-04-23 21:11:08 -0600352 if (cpu_class_is_omap2())
Juha Yrjola39020842006-09-25 12:41:44 +0300353 l |= 1 << 2;
Timo Teras77900a22006-06-26 16:16:12 -0700354 omap_dm_timer_write_reg(timer, OMAP_TIMER_OCP_CFG_REG, l);
Richard Woodruff0f0d0802008-07-03 12:24:30 +0300355
356 /* Match hardware reset default of posted mode */
357 omap_dm_timer_write_reg(timer, OMAP_TIMER_IF_CTRL_REG,
358 OMAP_TIMER_CTRL_POSTED);
359 timer->posted = 1;
Timo Teras77900a22006-06-26 16:16:12 -0700360}
361
Timo Teras83379c82006-06-26 16:16:23 -0700362static void omap_dm_timer_prepare(struct omap_dm_timer *timer)
Timo Teras77900a22006-06-26 16:16:12 -0700363{
Timo Teras12583a72006-09-25 12:41:42 +0300364 omap_dm_timer_enable(timer);
Timo Teras77900a22006-06-26 16:16:12 -0700365 omap_dm_timer_reset(timer);
366}
367
368struct omap_dm_timer *omap_dm_timer_request(void)
369{
370 struct omap_dm_timer *timer = NULL;
371 unsigned long flags;
372 int i;
373
374 spin_lock_irqsave(&dm_timer_lock, flags);
375 for (i = 0; i < dm_timer_count; i++) {
376 if (dm_timers[i].reserved)
377 continue;
378
379 timer = &dm_timers[i];
Timo Teras83379c82006-06-26 16:16:23 -0700380 timer->reserved = 1;
Timo Teras77900a22006-06-26 16:16:12 -0700381 break;
382 }
383 spin_unlock_irqrestore(&dm_timer_lock, flags);
384
Timo Teras83379c82006-06-26 16:16:23 -0700385 if (timer != NULL)
386 omap_dm_timer_prepare(timer);
387
Timo Teras77900a22006-06-26 16:16:12 -0700388 return timer;
389}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700390EXPORT_SYMBOL_GPL(omap_dm_timer_request);
Timo Teras77900a22006-06-26 16:16:12 -0700391
392struct omap_dm_timer *omap_dm_timer_request_specific(int id)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100393{
394 struct omap_dm_timer *timer;
Timo Teras77900a22006-06-26 16:16:12 -0700395 unsigned long flags;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100396
Timo Teras77900a22006-06-26 16:16:12 -0700397 spin_lock_irqsave(&dm_timer_lock, flags);
398 if (id <= 0 || id > dm_timer_count || dm_timers[id-1].reserved) {
399 spin_unlock_irqrestore(&dm_timer_lock, flags);
400 printk("BUG: warning at %s:%d/%s(): unable to get timer %d\n",
Harvey Harrison8e86f422008-03-04 15:08:02 -0800401 __FILE__, __LINE__, __func__, id);
Timo Teras77900a22006-06-26 16:16:12 -0700402 dump_stack();
403 return NULL;
404 }
Tony Lindgren92105bb2005-09-07 17:20:26 +0100405
Timo Teras77900a22006-06-26 16:16:12 -0700406 timer = &dm_timers[id-1];
Timo Teras83379c82006-06-26 16:16:23 -0700407 timer->reserved = 1;
Timo Teras77900a22006-06-26 16:16:12 -0700408 spin_unlock_irqrestore(&dm_timer_lock, flags);
409
Timo Teras83379c82006-06-26 16:16:23 -0700410 omap_dm_timer_prepare(timer);
411
Timo Teras77900a22006-06-26 16:16:12 -0700412 return timer;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100413}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700414EXPORT_SYMBOL_GPL(omap_dm_timer_request_specific);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100415
Timo Teras77900a22006-06-26 16:16:12 -0700416void omap_dm_timer_free(struct omap_dm_timer *timer)
417{
Timo Teras12583a72006-09-25 12:41:42 +0300418 omap_dm_timer_enable(timer);
Timo Teras77900a22006-06-26 16:16:12 -0700419 omap_dm_timer_reset(timer);
Timo Teras12583a72006-09-25 12:41:42 +0300420 omap_dm_timer_disable(timer);
Timo Terasfa4bb622006-09-25 12:41:35 +0300421
Timo Teras77900a22006-06-26 16:16:12 -0700422 WARN_ON(!timer->reserved);
423 timer->reserved = 0;
424}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700425EXPORT_SYMBOL_GPL(omap_dm_timer_free);
Timo Teras77900a22006-06-26 16:16:12 -0700426
Timo Teras12583a72006-09-25 12:41:42 +0300427void omap_dm_timer_enable(struct omap_dm_timer *timer)
428{
429 if (timer->enabled)
430 return;
431
Tony Lindgren882c0512010-02-12 12:26:46 -0800432#ifdef CONFIG_ARCH_OMAP2PLUS
433 if (cpu_class_is_omap2()) {
434 clk_enable(timer->fclk);
435 clk_enable(timer->iclk);
436 }
437#endif
Timo Teras12583a72006-09-25 12:41:42 +0300438
439 timer->enabled = 1;
440}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700441EXPORT_SYMBOL_GPL(omap_dm_timer_enable);
Timo Teras12583a72006-09-25 12:41:42 +0300442
443void omap_dm_timer_disable(struct omap_dm_timer *timer)
444{
445 if (!timer->enabled)
446 return;
447
Tony Lindgren882c0512010-02-12 12:26:46 -0800448#ifdef CONFIG_ARCH_OMAP2PLUS
449 if (cpu_class_is_omap2()) {
450 clk_disable(timer->iclk);
451 clk_disable(timer->fclk);
452 }
453#endif
Timo Teras12583a72006-09-25 12:41:42 +0300454
455 timer->enabled = 0;
456}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700457EXPORT_SYMBOL_GPL(omap_dm_timer_disable);
Timo Teras12583a72006-09-25 12:41:42 +0300458
Timo Teras77900a22006-06-26 16:16:12 -0700459int omap_dm_timer_get_irq(struct omap_dm_timer *timer)
460{
461 return timer->irq;
462}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700463EXPORT_SYMBOL_GPL(omap_dm_timer_get_irq);
Timo Teras77900a22006-06-26 16:16:12 -0700464
465#if defined(CONFIG_ARCH_OMAP1)
466
Tony Lindgrena569c6e2006-04-02 17:46:21 +0100467/**
468 * omap_dm_timer_modify_idlect_mask - Check if any running timers use ARMXOR
469 * @inputmask: current value of idlect mask
470 */
471__u32 omap_dm_timer_modify_idlect_mask(__u32 inputmask)
472{
Timo Teras77900a22006-06-26 16:16:12 -0700473 int i;
Tony Lindgrena569c6e2006-04-02 17:46:21 +0100474
475 /* If ARMXOR cannot be idled this function call is unnecessary */
476 if (!(inputmask & (1 << 1)))
477 return inputmask;
478
479 /* If any active timer is using ARMXOR return modified mask */
Timo Teras77900a22006-06-26 16:16:12 -0700480 for (i = 0; i < dm_timer_count; i++) {
481 u32 l;
482
Tony Lindgren35912c72006-07-01 19:56:42 +0100483 l = omap_dm_timer_read_reg(&dm_timers[i], OMAP_TIMER_CTRL_REG);
Timo Teras77900a22006-06-26 16:16:12 -0700484 if (l & OMAP_TIMER_CTRL_ST) {
485 if (((omap_readl(MOD_CONF_CTRL_1) >> (i * 2)) & 0x03) == 0)
Tony Lindgrena569c6e2006-04-02 17:46:21 +0100486 inputmask &= ~(1 << 1);
487 else
488 inputmask &= ~(1 << 2);
489 }
Timo Teras77900a22006-06-26 16:16:12 -0700490 }
Tony Lindgrena569c6e2006-04-02 17:46:21 +0100491
492 return inputmask;
493}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700494EXPORT_SYMBOL_GPL(omap_dm_timer_modify_idlect_mask);
Tony Lindgrena569c6e2006-04-02 17:46:21 +0100495
Tony Lindgren140455f2010-02-12 12:26:48 -0800496#else
Timo Teras77900a22006-06-26 16:16:12 -0700497
498struct clk *omap_dm_timer_get_fclk(struct omap_dm_timer *timer)
499{
Timo Terasfa4bb622006-09-25 12:41:35 +0300500 return timer->fclk;
Timo Teras77900a22006-06-26 16:16:12 -0700501}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700502EXPORT_SYMBOL_GPL(omap_dm_timer_get_fclk);
Timo Teras77900a22006-06-26 16:16:12 -0700503
504__u32 omap_dm_timer_modify_idlect_mask(__u32 inputmask)
505{
506 BUG();
Dirk Behme21218802006-12-06 17:14:00 -0800507
508 return 0;
Timo Teras77900a22006-06-26 16:16:12 -0700509}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700510EXPORT_SYMBOL_GPL(omap_dm_timer_modify_idlect_mask);
Timo Teras77900a22006-06-26 16:16:12 -0700511
512#endif
513
514void omap_dm_timer_trigger(struct omap_dm_timer *timer)
515{
516 omap_dm_timer_write_reg(timer, OMAP_TIMER_TRIGGER_REG, 0);
517}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700518EXPORT_SYMBOL_GPL(omap_dm_timer_trigger);
Timo Teras77900a22006-06-26 16:16:12 -0700519
520void omap_dm_timer_start(struct omap_dm_timer *timer)
521{
522 u32 l;
523
524 l = omap_dm_timer_read_reg(timer, OMAP_TIMER_CTRL_REG);
525 if (!(l & OMAP_TIMER_CTRL_ST)) {
526 l |= OMAP_TIMER_CTRL_ST;
527 omap_dm_timer_write_reg(timer, OMAP_TIMER_CTRL_REG, l);
528 }
529}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700530EXPORT_SYMBOL_GPL(omap_dm_timer_start);
Timo Teras77900a22006-06-26 16:16:12 -0700531
532void omap_dm_timer_stop(struct omap_dm_timer *timer)
533{
534 u32 l;
535
536 l = omap_dm_timer_read_reg(timer, OMAP_TIMER_CTRL_REG);
537 if (l & OMAP_TIMER_CTRL_ST) {
538 l &= ~0x1;
539 omap_dm_timer_write_reg(timer, OMAP_TIMER_CTRL_REG, l);
Tony Lindgren140455f2010-02-12 12:26:48 -0800540#ifdef CONFIG_ARCH_OMAP2PLUS
Tero Kristo5c3db362009-10-23 19:03:47 +0300541 /* Readback to make sure write has completed */
542 omap_dm_timer_read_reg(timer, OMAP_TIMER_CTRL_REG);
543 /*
544 * Wait for functional clock period x 3.5 to make sure that
545 * timer is stopped
546 */
547 udelay(3500000 / clk_get_rate(timer->fclk) + 1);
Tero Kristo5c3db362009-10-23 19:03:47 +0300548#endif
Timo Teras77900a22006-06-26 16:16:12 -0700549 }
Tero Kristo856f1912010-06-09 13:53:05 +0300550 /* Ack possibly pending interrupt */
551 omap_dm_timer_write_reg(timer, OMAP_TIMER_STAT_REG,
552 OMAP_TIMER_INT_OVERFLOW);
Timo Teras77900a22006-06-26 16:16:12 -0700553}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700554EXPORT_SYMBOL_GPL(omap_dm_timer_stop);
Timo Teras77900a22006-06-26 16:16:12 -0700555
556#ifdef CONFIG_ARCH_OMAP1
Tony Lindgrena569c6e2006-04-02 17:46:21 +0100557
Paul Walmsleyf2480762009-04-23 21:11:10 -0600558int omap_dm_timer_set_source(struct omap_dm_timer *timer, int source)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100559{
560 int n = (timer - dm_timers) << 1;
561 u32 l;
562
563 l = omap_readl(MOD_CONF_CTRL_1) & ~(0x03 << n);
564 l |= source << n;
565 omap_writel(l, MOD_CONF_CTRL_1);
Paul Walmsleyf2480762009-04-23 21:11:10 -0600566
567 return 0;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100568}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700569EXPORT_SYMBOL_GPL(omap_dm_timer_set_source);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100570
Timo Teras77900a22006-06-26 16:16:12 -0700571#else
Tony Lindgren92105bb2005-09-07 17:20:26 +0100572
Paul Walmsleyf2480762009-04-23 21:11:10 -0600573int omap_dm_timer_set_source(struct omap_dm_timer *timer, int source)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100574{
Paul Walmsleyf2480762009-04-23 21:11:10 -0600575 int ret = -EINVAL;
576
Timo Teras77900a22006-06-26 16:16:12 -0700577 if (source < 0 || source >= 3)
Paul Walmsleyf2480762009-04-23 21:11:10 -0600578 return -EINVAL;
Timo Teras77900a22006-06-26 16:16:12 -0700579
Timo Teras77900a22006-06-26 16:16:12 -0700580 clk_disable(timer->fclk);
Paul Walmsleyf2480762009-04-23 21:11:10 -0600581 ret = clk_set_parent(timer->fclk, dm_source_clocks[source]);
Timo Teras77900a22006-06-26 16:16:12 -0700582 clk_enable(timer->fclk);
Timo Teras77900a22006-06-26 16:16:12 -0700583
Paul Walmsleyf2480762009-04-23 21:11:10 -0600584 /*
585 * When the functional clock disappears, too quick writes seem
586 * to cause an abort. XXX Is this still necessary?
587 */
Santosh Shilimkare7193cc2010-09-16 18:44:48 +0530588 __delay(300000);
Paul Walmsleyf2480762009-04-23 21:11:10 -0600589
590 return ret;
Timo Teras77900a22006-06-26 16:16:12 -0700591}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700592EXPORT_SYMBOL_GPL(omap_dm_timer_set_source);
Timo Teras77900a22006-06-26 16:16:12 -0700593
594#endif
595
596void omap_dm_timer_set_load(struct omap_dm_timer *timer, int autoreload,
597 unsigned int load)
598{
599 u32 l;
600
601 l = omap_dm_timer_read_reg(timer, OMAP_TIMER_CTRL_REG);
602 if (autoreload)
603 l |= OMAP_TIMER_CTRL_AR;
604 else
605 l &= ~OMAP_TIMER_CTRL_AR;
606 omap_dm_timer_write_reg(timer, OMAP_TIMER_CTRL_REG, l);
607 omap_dm_timer_write_reg(timer, OMAP_TIMER_LOAD_REG, load);
Richard Woodruff0f0d0802008-07-03 12:24:30 +0300608
Timo Teras77900a22006-06-26 16:16:12 -0700609 omap_dm_timer_write_reg(timer, OMAP_TIMER_TRIGGER_REG, 0);
610}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700611EXPORT_SYMBOL_GPL(omap_dm_timer_set_load);
Timo Teras77900a22006-06-26 16:16:12 -0700612
Richard Woodruff3fddd092008-07-03 12:24:30 +0300613/* Optimized set_load which removes costly spin wait in timer_start */
614void omap_dm_timer_set_load_start(struct omap_dm_timer *timer, int autoreload,
615 unsigned int load)
616{
617 u32 l;
618
619 l = omap_dm_timer_read_reg(timer, OMAP_TIMER_CTRL_REG);
Paul Walmsley64ce2902008-12-10 17:36:34 -0800620 if (autoreload) {
Richard Woodruff3fddd092008-07-03 12:24:30 +0300621 l |= OMAP_TIMER_CTRL_AR;
Paul Walmsley64ce2902008-12-10 17:36:34 -0800622 omap_dm_timer_write_reg(timer, OMAP_TIMER_LOAD_REG, load);
623 } else {
Richard Woodruff3fddd092008-07-03 12:24:30 +0300624 l &= ~OMAP_TIMER_CTRL_AR;
Paul Walmsley64ce2902008-12-10 17:36:34 -0800625 }
Richard Woodruff3fddd092008-07-03 12:24:30 +0300626 l |= OMAP_TIMER_CTRL_ST;
627
628 omap_dm_timer_write_reg(timer, OMAP_TIMER_COUNTER_REG, load);
Richard Woodruff3fddd092008-07-03 12:24:30 +0300629 omap_dm_timer_write_reg(timer, OMAP_TIMER_CTRL_REG, l);
630}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700631EXPORT_SYMBOL_GPL(omap_dm_timer_set_load_start);
Richard Woodruff3fddd092008-07-03 12:24:30 +0300632
Timo Teras77900a22006-06-26 16:16:12 -0700633void omap_dm_timer_set_match(struct omap_dm_timer *timer, int enable,
634 unsigned int match)
635{
636 u32 l;
637
638 l = omap_dm_timer_read_reg(timer, OMAP_TIMER_CTRL_REG);
Timo Teras83379c82006-06-26 16:16:23 -0700639 if (enable)
Timo Teras77900a22006-06-26 16:16:12 -0700640 l |= OMAP_TIMER_CTRL_CE;
641 else
642 l &= ~OMAP_TIMER_CTRL_CE;
643 omap_dm_timer_write_reg(timer, OMAP_TIMER_CTRL_REG, l);
644 omap_dm_timer_write_reg(timer, OMAP_TIMER_MATCH_REG, match);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100645}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700646EXPORT_SYMBOL_GPL(omap_dm_timer_set_match);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100647
Timo Teras77900a22006-06-26 16:16:12 -0700648void omap_dm_timer_set_pwm(struct omap_dm_timer *timer, int def_on,
649 int toggle, int trigger)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100650{
Timo Teras77900a22006-06-26 16:16:12 -0700651 u32 l;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100652
Timo Teras77900a22006-06-26 16:16:12 -0700653 l = omap_dm_timer_read_reg(timer, OMAP_TIMER_CTRL_REG);
654 l &= ~(OMAP_TIMER_CTRL_GPOCFG | OMAP_TIMER_CTRL_SCPWM |
655 OMAP_TIMER_CTRL_PT | (0x03 << 10));
656 if (def_on)
657 l |= OMAP_TIMER_CTRL_SCPWM;
658 if (toggle)
659 l |= OMAP_TIMER_CTRL_PT;
660 l |= trigger << 10;
661 omap_dm_timer_write_reg(timer, OMAP_TIMER_CTRL_REG, l);
662}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700663EXPORT_SYMBOL_GPL(omap_dm_timer_set_pwm);
Timo Teras77900a22006-06-26 16:16:12 -0700664
665void omap_dm_timer_set_prescaler(struct omap_dm_timer *timer, int prescaler)
666{
667 u32 l;
668
669 l = omap_dm_timer_read_reg(timer, OMAP_TIMER_CTRL_REG);
670 l &= ~(OMAP_TIMER_CTRL_PRE | (0x07 << 2));
671 if (prescaler >= 0x00 && prescaler <= 0x07) {
672 l |= OMAP_TIMER_CTRL_PRE;
673 l |= prescaler << 2;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100674 }
Timo Teras77900a22006-06-26 16:16:12 -0700675 omap_dm_timer_write_reg(timer, OMAP_TIMER_CTRL_REG, l);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100676}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700677EXPORT_SYMBOL_GPL(omap_dm_timer_set_prescaler);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100678
679void omap_dm_timer_set_int_enable(struct omap_dm_timer *timer,
Timo Teras77900a22006-06-26 16:16:12 -0700680 unsigned int value)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100681{
682 omap_dm_timer_write_reg(timer, OMAP_TIMER_INT_EN_REG, value);
Juha Yrjola39020842006-09-25 12:41:44 +0300683 omap_dm_timer_write_reg(timer, OMAP_TIMER_WAKEUP_EN_REG, value);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100684}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700685EXPORT_SYMBOL_GPL(omap_dm_timer_set_int_enable);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100686
687unsigned int omap_dm_timer_read_status(struct omap_dm_timer *timer)
688{
Timo Terasfa4bb622006-09-25 12:41:35 +0300689 unsigned int l;
690
Timo Terasfa4bb622006-09-25 12:41:35 +0300691 l = omap_dm_timer_read_reg(timer, OMAP_TIMER_STAT_REG);
Timo Terasfa4bb622006-09-25 12:41:35 +0300692
693 return l;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100694}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700695EXPORT_SYMBOL_GPL(omap_dm_timer_read_status);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100696
697void omap_dm_timer_write_status(struct omap_dm_timer *timer, unsigned int value)
698{
699 omap_dm_timer_write_reg(timer, OMAP_TIMER_STAT_REG, value);
700}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700701EXPORT_SYMBOL_GPL(omap_dm_timer_write_status);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100702
Tony Lindgren92105bb2005-09-07 17:20:26 +0100703unsigned int omap_dm_timer_read_counter(struct omap_dm_timer *timer)
704{
Timo Terasfa4bb622006-09-25 12:41:35 +0300705 unsigned int l;
706
Timo Terasfa4bb622006-09-25 12:41:35 +0300707 l = omap_dm_timer_read_reg(timer, OMAP_TIMER_COUNTER_REG);
Timo Terasfa4bb622006-09-25 12:41:35 +0300708
709 return l;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100710}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700711EXPORT_SYMBOL_GPL(omap_dm_timer_read_counter);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100712
Timo Teras83379c82006-06-26 16:16:23 -0700713void omap_dm_timer_write_counter(struct omap_dm_timer *timer, unsigned int value)
714{
Timo Terasfa4bb622006-09-25 12:41:35 +0300715 omap_dm_timer_write_reg(timer, OMAP_TIMER_COUNTER_REG, value);
Timo Teras83379c82006-06-26 16:16:23 -0700716}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700717EXPORT_SYMBOL_GPL(omap_dm_timer_write_counter);
Timo Teras83379c82006-06-26 16:16:23 -0700718
Timo Teras77900a22006-06-26 16:16:12 -0700719int omap_dm_timers_active(void)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100720{
Timo Teras77900a22006-06-26 16:16:12 -0700721 int i;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100722
Timo Teras77900a22006-06-26 16:16:12 -0700723 for (i = 0; i < dm_timer_count; i++) {
724 struct omap_dm_timer *timer;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100725
Timo Teras77900a22006-06-26 16:16:12 -0700726 timer = &dm_timers[i];
Timo Teras12583a72006-09-25 12:41:42 +0300727
728 if (!timer->enabled)
729 continue;
730
Timo Teras77900a22006-06-26 16:16:12 -0700731 if (omap_dm_timer_read_reg(timer, OMAP_TIMER_CTRL_REG) &
Timo Terasfa4bb622006-09-25 12:41:35 +0300732 OMAP_TIMER_CTRL_ST) {
Timo Teras77900a22006-06-26 16:16:12 -0700733 return 1;
Timo Terasfa4bb622006-09-25 12:41:35 +0300734 }
Tony Lindgren92105bb2005-09-07 17:20:26 +0100735 }
Tony Lindgren92105bb2005-09-07 17:20:26 +0100736 return 0;
737}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700738EXPORT_SYMBOL_GPL(omap_dm_timers_active);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100739
Syed Mohammed, Khasim471b3aa2007-06-21 21:48:07 -0700740int __init omap_dm_timer_init(void)
Timo Teras77900a22006-06-26 16:16:12 -0700741{
742 struct omap_dm_timer *timer;
Tony Lindgren3566fc62009-10-19 15:25:18 -0700743 int i, map_size = SZ_8K; /* Module 4KB + L4 4KB except on omap1 */
Timo Teras77900a22006-06-26 16:16:12 -0700744
Syed Mohammed, Khasimce2df9c2007-06-25 22:55:39 -0700745 if (!(cpu_is_omap16xx() || cpu_class_is_omap2()))
Timo Teras77900a22006-06-26 16:16:12 -0700746 return -ENODEV;
747
748 spin_lock_init(&dm_timer_lock);
Syed Mohammed, Khasim471b3aa2007-06-21 21:48:07 -0700749
Tony Lindgren3566fc62009-10-19 15:25:18 -0700750 if (cpu_class_is_omap1()) {
Syed Mohammed, Khasim471b3aa2007-06-21 21:48:07 -0700751 dm_timers = omap1_dm_timers;
Tony Lindgren882c0512010-02-12 12:26:46 -0800752 dm_timer_count = omap1_dm_timer_count;
Tony Lindgren3566fc62009-10-19 15:25:18 -0700753 map_size = SZ_2K;
754 } else if (cpu_is_omap24xx()) {
Syed Mohammed, Khasim471b3aa2007-06-21 21:48:07 -0700755 dm_timers = omap2_dm_timers;
Tony Lindgren882c0512010-02-12 12:26:46 -0800756 dm_timer_count = omap2_dm_timer_count;
Santosh Shilimkaraea2a5b2009-05-25 11:08:36 -0700757 dm_source_names = omap2_dm_source_names;
758 dm_source_clocks = omap2_dm_source_clocks;
Syed Mohammed, Khasimce2df9c2007-06-25 22:55:39 -0700759 } else if (cpu_is_omap34xx()) {
760 dm_timers = omap3_dm_timers;
Tony Lindgren882c0512010-02-12 12:26:46 -0800761 dm_timer_count = omap3_dm_timer_count;
Santosh Shilimkaraea2a5b2009-05-25 11:08:36 -0700762 dm_source_names = omap3_dm_source_names;
763 dm_source_clocks = omap3_dm_source_clocks;
Santosh Shilimkar44169072009-05-28 14:16:04 -0700764 } else if (cpu_is_omap44xx()) {
765 dm_timers = omap4_dm_timers;
Tony Lindgren882c0512010-02-12 12:26:46 -0800766 dm_timer_count = omap4_dm_timer_count;
Santosh Shilimkar44169072009-05-28 14:16:04 -0700767 dm_source_names = omap4_dm_source_names;
768 dm_source_clocks = omap4_dm_source_clocks;
Timo Teras83379c82006-06-26 16:16:23 -0700769 }
Syed Mohammed, Khasim471b3aa2007-06-21 21:48:07 -0700770
771 if (cpu_class_is_omap2())
772 for (i = 0; dm_source_names[i] != NULL; i++)
773 dm_source_clocks[i] = clk_get(NULL, dm_source_names[i]);
774
Syed Mohammed Khasim56a25642006-12-06 17:14:08 -0800775 if (cpu_is_omap243x())
776 dm_timers[0].phys_base = 0x49018000;
Timo Teras83379c82006-06-26 16:16:23 -0700777
Timo Teras77900a22006-06-26 16:16:12 -0700778 for (i = 0; i < dm_timer_count; i++) {
Timo Teras77900a22006-06-26 16:16:12 -0700779 timer = &dm_timers[i];
Tony Lindgren3566fc62009-10-19 15:25:18 -0700780
781 /* Static mapping, never released */
782 timer->io_base = ioremap(timer->phys_base, map_size);
783 BUG_ON(!timer->io_base);
784
Tony Lindgren140455f2010-02-12 12:26:48 -0800785#ifdef CONFIG_ARCH_OMAP2PLUS
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}