blob: 963c31cd15416b69aacd5ae253e393f641de0e8a [file] [log] [blame]
Tony Lindgren92105bb2005-09-07 17:20:26 +01001/*
2 * linux/arch/arm/plat-omap/dmtimer.c
3 *
4 * OMAP Dual-Mode Timers
5 *
6 * Copyright (C) 2005 Nokia Corporation
Timo Teras77900a22006-06-26 16:16:12 -07007 * OMAP2 support by Juha Yrjola
8 * API improvements and OMAP2 clock framework support by Timo Teras
Tony Lindgren92105bb2005-09-07 17:20:26 +01009 *
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the
12 * Free Software Foundation; either version 2 of the License, or (at your
13 * option) any later version.
14 *
15 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
16 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
17 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
18 * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 *
24 * You should have received a copy of the GNU General Public License along
25 * with this program; if not, write to the Free Software Foundation, Inc.,
26 * 675 Mass Ave, Cambridge, MA 02139, USA.
27 */
28
29#include <linux/init.h>
Timo Teras77900a22006-06-26 16:16:12 -070030#include <linux/spinlock.h>
31#include <linux/errno.h>
32#include <linux/list.h>
33#include <linux/clk.h>
34#include <linux/delay.h>
Russell Kingfced80c2008-09-06 12:10:45 +010035#include <linux/io.h>
Russell Kinga09e64f2008-08-05 16:14:15 +010036#include <mach/hardware.h>
37#include <mach/dmtimer.h>
Russell Kinga09e64f2008-08-05 16:14:15 +010038#include <mach/irqs.h>
Tony Lindgren92105bb2005-09-07 17:20:26 +010039
Timo Teras77900a22006-06-26 16:16:12 -070040/* register offsets */
Richard Woodruff0f0d0802008-07-03 12:24:30 +030041#define _OMAP_TIMER_ID_OFFSET 0x00
42#define _OMAP_TIMER_OCP_CFG_OFFSET 0x10
43#define _OMAP_TIMER_SYS_STAT_OFFSET 0x14
44#define _OMAP_TIMER_STAT_OFFSET 0x18
45#define _OMAP_TIMER_INT_EN_OFFSET 0x1c
46#define _OMAP_TIMER_WAKEUP_EN_OFFSET 0x20
47#define _OMAP_TIMER_CTRL_OFFSET 0x24
48#define OMAP_TIMER_CTRL_GPOCFG (1 << 14)
49#define OMAP_TIMER_CTRL_CAPTMODE (1 << 13)
50#define OMAP_TIMER_CTRL_PT (1 << 12)
51#define OMAP_TIMER_CTRL_TCM_LOWTOHIGH (0x1 << 8)
52#define OMAP_TIMER_CTRL_TCM_HIGHTOLOW (0x2 << 8)
53#define OMAP_TIMER_CTRL_TCM_BOTHEDGES (0x3 << 8)
54#define OMAP_TIMER_CTRL_SCPWM (1 << 7)
55#define OMAP_TIMER_CTRL_CE (1 << 6) /* compare enable */
56#define OMAP_TIMER_CTRL_PRE (1 << 5) /* prescaler enable */
57#define OMAP_TIMER_CTRL_PTV_SHIFT 2 /* prescaler value shift */
58#define OMAP_TIMER_CTRL_POSTED (1 << 2)
59#define OMAP_TIMER_CTRL_AR (1 << 1) /* auto-reload enable */
60#define OMAP_TIMER_CTRL_ST (1 << 0) /* start timer */
61#define _OMAP_TIMER_COUNTER_OFFSET 0x28
62#define _OMAP_TIMER_LOAD_OFFSET 0x2c
63#define _OMAP_TIMER_TRIGGER_OFFSET 0x30
64#define _OMAP_TIMER_WRITE_PEND_OFFSET 0x34
65#define WP_NONE 0 /* no write pending bit */
66#define WP_TCLR (1 << 0)
67#define WP_TCRR (1 << 1)
68#define WP_TLDR (1 << 2)
69#define WP_TTGR (1 << 3)
70#define WP_TMAR (1 << 4)
71#define WP_TPIR (1 << 5)
72#define WP_TNIR (1 << 6)
73#define WP_TCVR (1 << 7)
74#define WP_TOCR (1 << 8)
75#define WP_TOWR (1 << 9)
76#define _OMAP_TIMER_MATCH_OFFSET 0x38
77#define _OMAP_TIMER_CAPTURE_OFFSET 0x3c
78#define _OMAP_TIMER_IF_CTRL_OFFSET 0x40
79#define _OMAP_TIMER_CAPTURE2_OFFSET 0x44 /* TCAR2, 34xx only */
80#define _OMAP_TIMER_TICK_POS_OFFSET 0x48 /* TPIR, 34xx only */
81#define _OMAP_TIMER_TICK_NEG_OFFSET 0x4c /* TNIR, 34xx only */
82#define _OMAP_TIMER_TICK_COUNT_OFFSET 0x50 /* TCVR, 34xx only */
83#define _OMAP_TIMER_TICK_INT_MASK_SET_OFFSET 0x54 /* TOCR, 34xx only */
84#define _OMAP_TIMER_TICK_INT_MASK_COUNT_OFFSET 0x58 /* TOWR, 34xx only */
Tony Lindgren92105bb2005-09-07 17:20:26 +010085
Richard Woodruff0f0d0802008-07-03 12:24:30 +030086/* register offsets with the write pending bit encoded */
87#define WPSHIFT 16
88
89#define OMAP_TIMER_ID_REG (_OMAP_TIMER_ID_OFFSET \
90 | (WP_NONE << WPSHIFT))
91
92#define OMAP_TIMER_OCP_CFG_REG (_OMAP_TIMER_OCP_CFG_OFFSET \
93 | (WP_NONE << WPSHIFT))
94
95#define OMAP_TIMER_SYS_STAT_REG (_OMAP_TIMER_SYS_STAT_OFFSET \
96 | (WP_NONE << WPSHIFT))
97
98#define OMAP_TIMER_STAT_REG (_OMAP_TIMER_STAT_OFFSET \
99 | (WP_NONE << WPSHIFT))
100
101#define OMAP_TIMER_INT_EN_REG (_OMAP_TIMER_INT_EN_OFFSET \
102 | (WP_NONE << WPSHIFT))
103
104#define OMAP_TIMER_WAKEUP_EN_REG (_OMAP_TIMER_WAKEUP_EN_OFFSET \
105 | (WP_NONE << WPSHIFT))
106
107#define OMAP_TIMER_CTRL_REG (_OMAP_TIMER_CTRL_OFFSET \
108 | (WP_TCLR << WPSHIFT))
109
110#define OMAP_TIMER_COUNTER_REG (_OMAP_TIMER_COUNTER_OFFSET \
111 | (WP_TCRR << WPSHIFT))
112
113#define OMAP_TIMER_LOAD_REG (_OMAP_TIMER_LOAD_OFFSET \
114 | (WP_TLDR << WPSHIFT))
115
116#define OMAP_TIMER_TRIGGER_REG (_OMAP_TIMER_TRIGGER_OFFSET \
117 | (WP_TTGR << WPSHIFT))
118
119#define OMAP_TIMER_WRITE_PEND_REG (_OMAP_TIMER_WRITE_PEND_OFFSET \
120 | (WP_NONE << WPSHIFT))
121
122#define OMAP_TIMER_MATCH_REG (_OMAP_TIMER_MATCH_OFFSET \
123 | (WP_TMAR << WPSHIFT))
124
125#define OMAP_TIMER_CAPTURE_REG (_OMAP_TIMER_CAPTURE_OFFSET \
126 | (WP_NONE << WPSHIFT))
127
128#define OMAP_TIMER_IF_CTRL_REG (_OMAP_TIMER_IF_CTRL_OFFSET \
129 | (WP_NONE << WPSHIFT))
130
131#define OMAP_TIMER_CAPTURE2_REG (_OMAP_TIMER_CAPTURE2_OFFSET \
132 | (WP_NONE << WPSHIFT))
133
134#define OMAP_TIMER_TICK_POS_REG (_OMAP_TIMER_TICK_POS_OFFSET \
135 | (WP_TPIR << WPSHIFT))
136
137#define OMAP_TIMER_TICK_NEG_REG (_OMAP_TIMER_TICK_NEG_OFFSET \
138 | (WP_TNIR << WPSHIFT))
139
140#define OMAP_TIMER_TICK_COUNT_REG (_OMAP_TIMER_TICK_COUNT_OFFSET \
141 | (WP_TCVR << WPSHIFT))
142
143#define OMAP_TIMER_TICK_INT_MASK_SET_REG \
144 (_OMAP_TIMER_TICK_INT_MASK_SET_OFFSET | (WP_TOCR << WPSHIFT))
145
146#define OMAP_TIMER_TICK_INT_MASK_COUNT_REG \
147 (_OMAP_TIMER_TICK_INT_MASK_COUNT_OFFSET | (WP_TOWR << WPSHIFT))
Tony Lindgren92105bb2005-09-07 17:20:26 +0100148
Timo Teras77900a22006-06-26 16:16:12 -0700149struct omap_dm_timer {
150 unsigned long phys_base;
151 int irq;
Syed Mohammed, Khasimce2df9c2007-06-25 22:55:39 -0700152#if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
Timo Teras77900a22006-06-26 16:16:12 -0700153 struct clk *iclk, *fclk;
154#endif
155 void __iomem *io_base;
156 unsigned reserved:1;
Timo Teras12583a72006-09-25 12:41:42 +0300157 unsigned enabled:1;
Richard Woodruff0f0d0802008-07-03 12:24:30 +0300158 unsigned posted:1;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100159};
160
Timo Teras77900a22006-06-26 16:16:12 -0700161#ifdef CONFIG_ARCH_OMAP1
162
Timo Terasfa4bb622006-09-25 12:41:35 +0300163#define omap_dm_clk_enable(x)
164#define omap_dm_clk_disable(x)
Syed Mohammed, Khasim471b3aa2007-06-21 21:48:07 -0700165#define omap2_dm_timers NULL
166#define omap2_dm_source_names NULL
167#define omap2_dm_source_clocks NULL
Syed Mohammed, Khasimce2df9c2007-06-25 22:55:39 -0700168#define omap3_dm_timers NULL
169#define omap3_dm_source_names NULL
170#define omap3_dm_source_clocks NULL
Timo Terasfa4bb622006-09-25 12:41:35 +0300171
Syed Mohammed, Khasim471b3aa2007-06-21 21:48:07 -0700172static struct omap_dm_timer omap1_dm_timers[] = {
Timo Teras77900a22006-06-26 16:16:12 -0700173 { .phys_base = 0xfffb1400, .irq = INT_1610_GPTIMER1 },
174 { .phys_base = 0xfffb1c00, .irq = INT_1610_GPTIMER2 },
175 { .phys_base = 0xfffb2400, .irq = INT_1610_GPTIMER3 },
176 { .phys_base = 0xfffb2c00, .irq = INT_1610_GPTIMER4 },
177 { .phys_base = 0xfffb3400, .irq = INT_1610_GPTIMER5 },
178 { .phys_base = 0xfffb3c00, .irq = INT_1610_GPTIMER6 },
Matthew Percival53037f42007-01-25 16:24:29 -0800179 { .phys_base = 0xfffb7400, .irq = INT_1610_GPTIMER7 },
180 { .phys_base = 0xfffbd400, .irq = INT_1610_GPTIMER8 },
Timo Teras77900a22006-06-26 16:16:12 -0700181};
182
Syed Mohammed, Khasim471b3aa2007-06-21 21:48:07 -0700183static const int dm_timer_count = ARRAY_SIZE(omap1_dm_timers);
184
Timo Teras77900a22006-06-26 16:16:12 -0700185#elif defined(CONFIG_ARCH_OMAP2)
186
Syed Mohammed, Khasim471b3aa2007-06-21 21:48:07 -0700187#define omap_dm_clk_enable(x) clk_enable(x)
188#define omap_dm_clk_disable(x) clk_disable(x)
189#define omap1_dm_timers NULL
Syed Mohammed, Khasimce2df9c2007-06-25 22:55:39 -0700190#define omap3_dm_timers NULL
191#define omap3_dm_source_names NULL
192#define omap3_dm_source_clocks NULL
Timo Terasfa4bb622006-09-25 12:41:35 +0300193
Syed Mohammed, Khasim471b3aa2007-06-21 21:48:07 -0700194static struct omap_dm_timer omap2_dm_timers[] = {
Timo Teras77900a22006-06-26 16:16:12 -0700195 { .phys_base = 0x48028000, .irq = INT_24XX_GPTIMER1 },
196 { .phys_base = 0x4802a000, .irq = INT_24XX_GPTIMER2 },
197 { .phys_base = 0x48078000, .irq = INT_24XX_GPTIMER3 },
198 { .phys_base = 0x4807a000, .irq = INT_24XX_GPTIMER4 },
199 { .phys_base = 0x4807c000, .irq = INT_24XX_GPTIMER5 },
200 { .phys_base = 0x4807e000, .irq = INT_24XX_GPTIMER6 },
201 { .phys_base = 0x48080000, .irq = INT_24XX_GPTIMER7 },
202 { .phys_base = 0x48082000, .irq = INT_24XX_GPTIMER8 },
203 { .phys_base = 0x48084000, .irq = INT_24XX_GPTIMER9 },
204 { .phys_base = 0x48086000, .irq = INT_24XX_GPTIMER10 },
205 { .phys_base = 0x48088000, .irq = INT_24XX_GPTIMER11 },
206 { .phys_base = 0x4808a000, .irq = INT_24XX_GPTIMER12 },
207};
208
Syed Mohammed, Khasim471b3aa2007-06-21 21:48:07 -0700209static const char *omap2_dm_source_names[] __initdata = {
Timo Teras83379c82006-06-26 16:16:23 -0700210 "sys_ck",
211 "func_32k_ck",
Syed Mohammed, Khasim471b3aa2007-06-21 21:48:07 -0700212 "alt_ck",
213 NULL
Timo Teras83379c82006-06-26 16:16:23 -0700214};
215
Syed Mohammed, Khasim471b3aa2007-06-21 21:48:07 -0700216static struct clk **omap2_dm_source_clocks[3];
217static const int dm_timer_count = ARRAY_SIZE(omap2_dm_timers);
Timo Teras83379c82006-06-26 16:16:23 -0700218
Syed Mohammed, Khasimce2df9c2007-06-25 22:55:39 -0700219#elif defined(CONFIG_ARCH_OMAP3)
220
221#define omap_dm_clk_enable(x) clk_enable(x)
222#define omap_dm_clk_disable(x) clk_disable(x)
223#define omap1_dm_timers NULL
224#define omap2_dm_timers NULL
225#define omap2_dm_source_names NULL
226#define omap2_dm_source_clocks NULL
227
228static struct omap_dm_timer omap3_dm_timers[] = {
229 { .phys_base = 0x48318000, .irq = INT_24XX_GPTIMER1 },
230 { .phys_base = 0x49032000, .irq = INT_24XX_GPTIMER2 },
231 { .phys_base = 0x49034000, .irq = INT_24XX_GPTIMER3 },
232 { .phys_base = 0x49036000, .irq = INT_24XX_GPTIMER4 },
233 { .phys_base = 0x49038000, .irq = INT_24XX_GPTIMER5 },
234 { .phys_base = 0x4903A000, .irq = INT_24XX_GPTIMER6 },
235 { .phys_base = 0x4903C000, .irq = INT_24XX_GPTIMER7 },
236 { .phys_base = 0x4903E000, .irq = INT_24XX_GPTIMER8 },
237 { .phys_base = 0x49040000, .irq = INT_24XX_GPTIMER9 },
238 { .phys_base = 0x48086000, .irq = INT_24XX_GPTIMER10 },
239 { .phys_base = 0x48088000, .irq = INT_24XX_GPTIMER11 },
240 { .phys_base = 0x48304000, .irq = INT_24XX_GPTIMER12 },
241};
242
243static const char *omap3_dm_source_names[] __initdata = {
244 "sys_ck",
245 "omap_32k_fck",
246 NULL
247};
248
249static struct clk **omap3_dm_source_clocks[2];
250static const int dm_timer_count = ARRAY_SIZE(omap3_dm_timers);
251
Timo Teras77900a22006-06-26 16:16:12 -0700252#else
253
254#error OMAP architecture not supported!
255
256#endif
257
Syed Mohammed, Khasim471b3aa2007-06-21 21:48:07 -0700258static struct omap_dm_timer *dm_timers;
259static char **dm_source_names;
260static struct clk **dm_source_clocks;
261
Tony Lindgren92105bb2005-09-07 17:20:26 +0100262static spinlock_t dm_timer_lock;
263
Richard Woodruff0f0d0802008-07-03 12:24:30 +0300264/*
265 * Reads timer registers in posted and non-posted mode. The posted mode bit
266 * is encoded in reg. Note that in posted mode write pending bit must be
267 * checked. Otherwise a read of a non completed write will produce an error.
268 */
269static inline u32 omap_dm_timer_read_reg(struct omap_dm_timer *timer, u32 reg)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100270{
Richard Woodruff0f0d0802008-07-03 12:24:30 +0300271 if (timer->posted)
272 while (readl(timer->io_base + (OMAP_TIMER_WRITE_PEND_REG & 0xff))
273 & (reg >> WPSHIFT))
274 cpu_relax();
275 return readl(timer->io_base + (reg & 0xff));
Timo Teras77900a22006-06-26 16:16:12 -0700276}
277
Richard Woodruff0f0d0802008-07-03 12:24:30 +0300278/*
279 * Writes timer registers in posted and non-posted mode. The posted mode bit
280 * is encoded in reg. Note that in posted mode the write pending bit must be
281 * checked. Otherwise a write on a register which has a pending write will be
282 * lost.
283 */
284static void omap_dm_timer_write_reg(struct omap_dm_timer *timer, u32 reg,
285 u32 value)
Timo Teras77900a22006-06-26 16:16:12 -0700286{
Richard Woodruff0f0d0802008-07-03 12:24:30 +0300287 if (timer->posted)
288 while (readl(timer->io_base + (OMAP_TIMER_WRITE_PEND_REG & 0xff))
289 & (reg >> WPSHIFT))
290 cpu_relax();
291 writel(value, timer->io_base + (reg & 0xff));
Tony Lindgren92105bb2005-09-07 17:20:26 +0100292}
293
Timo Teras77900a22006-06-26 16:16:12 -0700294static void omap_dm_timer_wait_for_reset(struct omap_dm_timer *timer)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100295{
Timo Teras77900a22006-06-26 16:16:12 -0700296 int c;
297
298 c = 0;
299 while (!(omap_dm_timer_read_reg(timer, OMAP_TIMER_SYS_STAT_REG) & 1)) {
300 c++;
301 if (c > 100000) {
302 printk(KERN_ERR "Timer failed to reset\n");
303 return;
304 }
305 }
Tony Lindgren92105bb2005-09-07 17:20:26 +0100306}
307
Timo Teras77900a22006-06-26 16:16:12 -0700308static void omap_dm_timer_reset(struct omap_dm_timer *timer)
309{
310 u32 l;
311
Juha Yrjola39020842006-09-25 12:41:44 +0300312 if (!cpu_class_is_omap2() || timer != &dm_timers[0]) {
Timo Terase32f7ec2006-06-26 16:16:13 -0700313 omap_dm_timer_write_reg(timer, OMAP_TIMER_IF_CTRL_REG, 0x06);
314 omap_dm_timer_wait_for_reset(timer);
315 }
Timo Teras12583a72006-09-25 12:41:42 +0300316 omap_dm_timer_set_source(timer, OMAP_TIMER_SRC_32_KHZ);
Timo Teras77900a22006-06-26 16:16:12 -0700317
Timo Teras77900a22006-06-26 16:16:12 -0700318 l = omap_dm_timer_read_reg(timer, OMAP_TIMER_OCP_CFG_REG);
Richard Woodruff0f0d0802008-07-03 12:24:30 +0300319 l |= 0x02 << 3; /* Set to smart-idle mode */
320 l |= 0x2 << 8; /* Set clock activity to perserve f-clock on idle */
Juha Yrjola39020842006-09-25 12:41:44 +0300321
Richard Woodruff0f0d0802008-07-03 12:24:30 +0300322 /*
323 * Enable wake-up only for GPT1 on OMAP2 CPUs.
324 * FIXME: All timers should have wake-up enabled and clear
325 * PRCM status.
326 */
327 if (cpu_class_is_omap2() && (timer == &dm_timers[0]))
Juha Yrjola39020842006-09-25 12:41:44 +0300328 l |= 1 << 2;
Timo Teras77900a22006-06-26 16:16:12 -0700329 omap_dm_timer_write_reg(timer, OMAP_TIMER_OCP_CFG_REG, l);
Richard Woodruff0f0d0802008-07-03 12:24:30 +0300330
331 /* Match hardware reset default of posted mode */
332 omap_dm_timer_write_reg(timer, OMAP_TIMER_IF_CTRL_REG,
333 OMAP_TIMER_CTRL_POSTED);
334 timer->posted = 1;
Timo Teras77900a22006-06-26 16:16:12 -0700335}
336
Timo Teras83379c82006-06-26 16:16:23 -0700337static void omap_dm_timer_prepare(struct omap_dm_timer *timer)
Timo Teras77900a22006-06-26 16:16:12 -0700338{
Timo Teras12583a72006-09-25 12:41:42 +0300339 omap_dm_timer_enable(timer);
Timo Teras77900a22006-06-26 16:16:12 -0700340 omap_dm_timer_reset(timer);
341}
342
343struct omap_dm_timer *omap_dm_timer_request(void)
344{
345 struct omap_dm_timer *timer = NULL;
346 unsigned long flags;
347 int i;
348
349 spin_lock_irqsave(&dm_timer_lock, flags);
350 for (i = 0; i < dm_timer_count; i++) {
351 if (dm_timers[i].reserved)
352 continue;
353
354 timer = &dm_timers[i];
Timo Teras83379c82006-06-26 16:16:23 -0700355 timer->reserved = 1;
Timo Teras77900a22006-06-26 16:16:12 -0700356 break;
357 }
358 spin_unlock_irqrestore(&dm_timer_lock, flags);
359
Timo Teras83379c82006-06-26 16:16:23 -0700360 if (timer != NULL)
361 omap_dm_timer_prepare(timer);
362
Timo Teras77900a22006-06-26 16:16:12 -0700363 return timer;
364}
365
366struct omap_dm_timer *omap_dm_timer_request_specific(int id)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100367{
368 struct omap_dm_timer *timer;
Timo Teras77900a22006-06-26 16:16:12 -0700369 unsigned long flags;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100370
Timo Teras77900a22006-06-26 16:16:12 -0700371 spin_lock_irqsave(&dm_timer_lock, flags);
372 if (id <= 0 || id > dm_timer_count || dm_timers[id-1].reserved) {
373 spin_unlock_irqrestore(&dm_timer_lock, flags);
374 printk("BUG: warning at %s:%d/%s(): unable to get timer %d\n",
Harvey Harrison8e86f422008-03-04 15:08:02 -0800375 __FILE__, __LINE__, __func__, id);
Timo Teras77900a22006-06-26 16:16:12 -0700376 dump_stack();
377 return NULL;
378 }
Tony Lindgren92105bb2005-09-07 17:20:26 +0100379
Timo Teras77900a22006-06-26 16:16:12 -0700380 timer = &dm_timers[id-1];
Timo Teras83379c82006-06-26 16:16:23 -0700381 timer->reserved = 1;
Timo Teras77900a22006-06-26 16:16:12 -0700382 spin_unlock_irqrestore(&dm_timer_lock, flags);
383
Timo Teras83379c82006-06-26 16:16:23 -0700384 omap_dm_timer_prepare(timer);
385
Timo Teras77900a22006-06-26 16:16:12 -0700386 return timer;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100387}
388
Timo Teras77900a22006-06-26 16:16:12 -0700389void omap_dm_timer_free(struct omap_dm_timer *timer)
390{
Timo Teras12583a72006-09-25 12:41:42 +0300391 omap_dm_timer_enable(timer);
Timo Teras77900a22006-06-26 16:16:12 -0700392 omap_dm_timer_reset(timer);
Timo Teras12583a72006-09-25 12:41:42 +0300393 omap_dm_timer_disable(timer);
Timo Terasfa4bb622006-09-25 12:41:35 +0300394
Timo Teras77900a22006-06-26 16:16:12 -0700395 WARN_ON(!timer->reserved);
396 timer->reserved = 0;
397}
398
Timo Teras12583a72006-09-25 12:41:42 +0300399void omap_dm_timer_enable(struct omap_dm_timer *timer)
400{
401 if (timer->enabled)
402 return;
403
404 omap_dm_clk_enable(timer->fclk);
405 omap_dm_clk_enable(timer->iclk);
406
407 timer->enabled = 1;
408}
409
410void omap_dm_timer_disable(struct omap_dm_timer *timer)
411{
412 if (!timer->enabled)
413 return;
414
415 omap_dm_clk_disable(timer->iclk);
416 omap_dm_clk_disable(timer->fclk);
417
418 timer->enabled = 0;
419}
420
Timo Teras77900a22006-06-26 16:16:12 -0700421int omap_dm_timer_get_irq(struct omap_dm_timer *timer)
422{
423 return timer->irq;
424}
425
426#if defined(CONFIG_ARCH_OMAP1)
427
Tony Lindgrena569c6e2006-04-02 17:46:21 +0100428/**
429 * omap_dm_timer_modify_idlect_mask - Check if any running timers use ARMXOR
430 * @inputmask: current value of idlect mask
431 */
432__u32 omap_dm_timer_modify_idlect_mask(__u32 inputmask)
433{
Timo Teras77900a22006-06-26 16:16:12 -0700434 int i;
Tony Lindgrena569c6e2006-04-02 17:46:21 +0100435
436 /* If ARMXOR cannot be idled this function call is unnecessary */
437 if (!(inputmask & (1 << 1)))
438 return inputmask;
439
440 /* If any active timer is using ARMXOR return modified mask */
Timo Teras77900a22006-06-26 16:16:12 -0700441 for (i = 0; i < dm_timer_count; i++) {
442 u32 l;
443
Tony Lindgren35912c72006-07-01 19:56:42 +0100444 l = omap_dm_timer_read_reg(&dm_timers[i], OMAP_TIMER_CTRL_REG);
Timo Teras77900a22006-06-26 16:16:12 -0700445 if (l & OMAP_TIMER_CTRL_ST) {
446 if (((omap_readl(MOD_CONF_CTRL_1) >> (i * 2)) & 0x03) == 0)
Tony Lindgrena569c6e2006-04-02 17:46:21 +0100447 inputmask &= ~(1 << 1);
448 else
449 inputmask &= ~(1 << 2);
450 }
Timo Teras77900a22006-06-26 16:16:12 -0700451 }
Tony Lindgrena569c6e2006-04-02 17:46:21 +0100452
453 return inputmask;
454}
455
Syed Mohammed, Khasimce2df9c2007-06-25 22:55:39 -0700456#elif defined(CONFIG_ARCH_OMAP2) || defined (CONFIG_ARCH_OMAP3)
Timo Teras77900a22006-06-26 16:16:12 -0700457
458struct clk *omap_dm_timer_get_fclk(struct omap_dm_timer *timer)
459{
Timo Terasfa4bb622006-09-25 12:41:35 +0300460 return timer->fclk;
Timo Teras77900a22006-06-26 16:16:12 -0700461}
462
463__u32 omap_dm_timer_modify_idlect_mask(__u32 inputmask)
464{
465 BUG();
Dirk Behme21218802006-12-06 17:14:00 -0800466
467 return 0;
Timo Teras77900a22006-06-26 16:16:12 -0700468}
469
470#endif
471
472void omap_dm_timer_trigger(struct omap_dm_timer *timer)
473{
474 omap_dm_timer_write_reg(timer, OMAP_TIMER_TRIGGER_REG, 0);
475}
476
477void omap_dm_timer_start(struct omap_dm_timer *timer)
478{
479 u32 l;
480
481 l = omap_dm_timer_read_reg(timer, OMAP_TIMER_CTRL_REG);
482 if (!(l & OMAP_TIMER_CTRL_ST)) {
483 l |= OMAP_TIMER_CTRL_ST;
484 omap_dm_timer_write_reg(timer, OMAP_TIMER_CTRL_REG, l);
485 }
486}
487
488void omap_dm_timer_stop(struct omap_dm_timer *timer)
489{
490 u32 l;
491
492 l = omap_dm_timer_read_reg(timer, OMAP_TIMER_CTRL_REG);
493 if (l & OMAP_TIMER_CTRL_ST) {
494 l &= ~0x1;
495 omap_dm_timer_write_reg(timer, OMAP_TIMER_CTRL_REG, l);
496 }
497}
498
499#ifdef CONFIG_ARCH_OMAP1
Tony Lindgrena569c6e2006-04-02 17:46:21 +0100500
Tony Lindgren92105bb2005-09-07 17:20:26 +0100501void omap_dm_timer_set_source(struct omap_dm_timer *timer, int source)
502{
503 int n = (timer - dm_timers) << 1;
504 u32 l;
505
506 l = omap_readl(MOD_CONF_CTRL_1) & ~(0x03 << n);
507 l |= source << n;
508 omap_writel(l, MOD_CONF_CTRL_1);
509}
510
Timo Teras77900a22006-06-26 16:16:12 -0700511#else
Tony Lindgren92105bb2005-09-07 17:20:26 +0100512
Timo Teras77900a22006-06-26 16:16:12 -0700513void omap_dm_timer_set_source(struct omap_dm_timer *timer, int source)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100514{
Timo Teras77900a22006-06-26 16:16:12 -0700515 if (source < 0 || source >= 3)
516 return;
517
Timo Teras77900a22006-06-26 16:16:12 -0700518 clk_disable(timer->fclk);
Timo Teras83379c82006-06-26 16:16:23 -0700519 clk_set_parent(timer->fclk, dm_source_clocks[source]);
Timo Teras77900a22006-06-26 16:16:12 -0700520 clk_enable(timer->fclk);
Timo Teras77900a22006-06-26 16:16:12 -0700521
522 /* When the functional clock disappears, too quick writes seem to
523 * cause an abort. */
Tony Lindgrenc40fae952006-12-07 13:58:10 -0800524 __delay(150000);
Timo Teras77900a22006-06-26 16:16:12 -0700525}
526
527#endif
528
529void omap_dm_timer_set_load(struct omap_dm_timer *timer, int autoreload,
530 unsigned int load)
531{
532 u32 l;
533
534 l = omap_dm_timer_read_reg(timer, OMAP_TIMER_CTRL_REG);
535 if (autoreload)
536 l |= OMAP_TIMER_CTRL_AR;
537 else
538 l &= ~OMAP_TIMER_CTRL_AR;
539 omap_dm_timer_write_reg(timer, OMAP_TIMER_CTRL_REG, l);
540 omap_dm_timer_write_reg(timer, OMAP_TIMER_LOAD_REG, load);
Richard Woodruff0f0d0802008-07-03 12:24:30 +0300541
542 /* REVISIT: hw feature, ttgr overtaking tldr? */
543 while (readl(timer->io_base + (OMAP_TIMER_WRITE_PEND_REG & 0xff)))
544 cpu_relax();
545
Timo Teras77900a22006-06-26 16:16:12 -0700546 omap_dm_timer_write_reg(timer, OMAP_TIMER_TRIGGER_REG, 0);
547}
548
Richard Woodruff3fddd092008-07-03 12:24:30 +0300549/* Optimized set_load which removes costly spin wait in timer_start */
550void omap_dm_timer_set_load_start(struct omap_dm_timer *timer, int autoreload,
551 unsigned int load)
552{
553 u32 l;
554
555 l = omap_dm_timer_read_reg(timer, OMAP_TIMER_CTRL_REG);
556 if (autoreload)
557 l |= OMAP_TIMER_CTRL_AR;
558 else
559 l &= ~OMAP_TIMER_CTRL_AR;
560 l |= OMAP_TIMER_CTRL_ST;
561
562 omap_dm_timer_write_reg(timer, OMAP_TIMER_COUNTER_REG, load);
563 omap_dm_timer_write_reg(timer, OMAP_TIMER_LOAD_REG, load);
564 omap_dm_timer_write_reg(timer, OMAP_TIMER_CTRL_REG, l);
565}
566
Timo Teras77900a22006-06-26 16:16:12 -0700567void omap_dm_timer_set_match(struct omap_dm_timer *timer, int enable,
568 unsigned int match)
569{
570 u32 l;
571
572 l = omap_dm_timer_read_reg(timer, OMAP_TIMER_CTRL_REG);
Timo Teras83379c82006-06-26 16:16:23 -0700573 if (enable)
Timo Teras77900a22006-06-26 16:16:12 -0700574 l |= OMAP_TIMER_CTRL_CE;
575 else
576 l &= ~OMAP_TIMER_CTRL_CE;
577 omap_dm_timer_write_reg(timer, OMAP_TIMER_CTRL_REG, l);
578 omap_dm_timer_write_reg(timer, OMAP_TIMER_MATCH_REG, match);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100579}
580
Timo Teras77900a22006-06-26 16:16:12 -0700581void omap_dm_timer_set_pwm(struct omap_dm_timer *timer, int def_on,
582 int toggle, int trigger)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100583{
Timo Teras77900a22006-06-26 16:16:12 -0700584 u32 l;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100585
Timo Teras77900a22006-06-26 16:16:12 -0700586 l = omap_dm_timer_read_reg(timer, OMAP_TIMER_CTRL_REG);
587 l &= ~(OMAP_TIMER_CTRL_GPOCFG | OMAP_TIMER_CTRL_SCPWM |
588 OMAP_TIMER_CTRL_PT | (0x03 << 10));
589 if (def_on)
590 l |= OMAP_TIMER_CTRL_SCPWM;
591 if (toggle)
592 l |= OMAP_TIMER_CTRL_PT;
593 l |= trigger << 10;
594 omap_dm_timer_write_reg(timer, OMAP_TIMER_CTRL_REG, l);
595}
596
597void omap_dm_timer_set_prescaler(struct omap_dm_timer *timer, int prescaler)
598{
599 u32 l;
600
601 l = omap_dm_timer_read_reg(timer, OMAP_TIMER_CTRL_REG);
602 l &= ~(OMAP_TIMER_CTRL_PRE | (0x07 << 2));
603 if (prescaler >= 0x00 && prescaler <= 0x07) {
604 l |= OMAP_TIMER_CTRL_PRE;
605 l |= prescaler << 2;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100606 }
Timo Teras77900a22006-06-26 16:16:12 -0700607 omap_dm_timer_write_reg(timer, OMAP_TIMER_CTRL_REG, l);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100608}
609
610void omap_dm_timer_set_int_enable(struct omap_dm_timer *timer,
Timo Teras77900a22006-06-26 16:16:12 -0700611 unsigned int value)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100612{
613 omap_dm_timer_write_reg(timer, OMAP_TIMER_INT_EN_REG, value);
Juha Yrjola39020842006-09-25 12:41:44 +0300614 omap_dm_timer_write_reg(timer, OMAP_TIMER_WAKEUP_EN_REG, value);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100615}
616
617unsigned int omap_dm_timer_read_status(struct omap_dm_timer *timer)
618{
Timo Terasfa4bb622006-09-25 12:41:35 +0300619 unsigned int l;
620
Timo Terasfa4bb622006-09-25 12:41:35 +0300621 l = omap_dm_timer_read_reg(timer, OMAP_TIMER_STAT_REG);
Timo Terasfa4bb622006-09-25 12:41:35 +0300622
623 return l;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100624}
625
626void omap_dm_timer_write_status(struct omap_dm_timer *timer, unsigned int value)
627{
628 omap_dm_timer_write_reg(timer, OMAP_TIMER_STAT_REG, value);
629}
630
Tony Lindgren92105bb2005-09-07 17:20:26 +0100631unsigned int omap_dm_timer_read_counter(struct omap_dm_timer *timer)
632{
Timo Terasfa4bb622006-09-25 12:41:35 +0300633 unsigned int l;
634
Timo Terasfa4bb622006-09-25 12:41:35 +0300635 l = omap_dm_timer_read_reg(timer, OMAP_TIMER_COUNTER_REG);
Timo Terasfa4bb622006-09-25 12:41:35 +0300636
637 return l;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100638}
639
Timo Teras83379c82006-06-26 16:16:23 -0700640void omap_dm_timer_write_counter(struct omap_dm_timer *timer, unsigned int value)
641{
Timo Terasfa4bb622006-09-25 12:41:35 +0300642 omap_dm_timer_write_reg(timer, OMAP_TIMER_COUNTER_REG, value);
Timo Teras83379c82006-06-26 16:16:23 -0700643}
644
Timo Teras77900a22006-06-26 16:16:12 -0700645int omap_dm_timers_active(void)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100646{
Timo Teras77900a22006-06-26 16:16:12 -0700647 int i;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100648
Timo Teras77900a22006-06-26 16:16:12 -0700649 for (i = 0; i < dm_timer_count; i++) {
650 struct omap_dm_timer *timer;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100651
Timo Teras77900a22006-06-26 16:16:12 -0700652 timer = &dm_timers[i];
Timo Teras12583a72006-09-25 12:41:42 +0300653
654 if (!timer->enabled)
655 continue;
656
Timo Teras77900a22006-06-26 16:16:12 -0700657 if (omap_dm_timer_read_reg(timer, OMAP_TIMER_CTRL_REG) &
Timo Terasfa4bb622006-09-25 12:41:35 +0300658 OMAP_TIMER_CTRL_ST) {
Timo Teras77900a22006-06-26 16:16:12 -0700659 return 1;
Timo Terasfa4bb622006-09-25 12:41:35 +0300660 }
Tony Lindgren92105bb2005-09-07 17:20:26 +0100661 }
Tony Lindgren92105bb2005-09-07 17:20:26 +0100662 return 0;
663}
664
Syed Mohammed, Khasim471b3aa2007-06-21 21:48:07 -0700665int __init omap_dm_timer_init(void)
Timo Teras77900a22006-06-26 16:16:12 -0700666{
667 struct omap_dm_timer *timer;
668 int i;
669
Syed Mohammed, Khasimce2df9c2007-06-25 22:55:39 -0700670 if (!(cpu_is_omap16xx() || cpu_class_is_omap2()))
Timo Teras77900a22006-06-26 16:16:12 -0700671 return -ENODEV;
672
673 spin_lock_init(&dm_timer_lock);
Syed Mohammed, Khasim471b3aa2007-06-21 21:48:07 -0700674
675 if (cpu_class_is_omap1())
676 dm_timers = omap1_dm_timers;
677 else if (cpu_is_omap24xx()) {
678 dm_timers = omap2_dm_timers;
679 dm_source_names = (char **)omap2_dm_source_names;
680 dm_source_clocks = (struct clk **)omap2_dm_source_clocks;
Syed Mohammed, Khasimce2df9c2007-06-25 22:55:39 -0700681 } else if (cpu_is_omap34xx()) {
682 dm_timers = omap3_dm_timers;
683 dm_source_names = (char **)omap3_dm_source_names;
684 dm_source_clocks = (struct clk **)omap3_dm_source_clocks;
Timo Teras83379c82006-06-26 16:16:23 -0700685 }
Syed Mohammed, Khasim471b3aa2007-06-21 21:48:07 -0700686
687 if (cpu_class_is_omap2())
688 for (i = 0; dm_source_names[i] != NULL; i++)
689 dm_source_clocks[i] = clk_get(NULL, dm_source_names[i]);
690
Syed Mohammed Khasim56a25642006-12-06 17:14:08 -0800691 if (cpu_is_omap243x())
692 dm_timers[0].phys_base = 0x49018000;
Timo Teras83379c82006-06-26 16:16:23 -0700693
Timo Teras77900a22006-06-26 16:16:12 -0700694 for (i = 0; i < dm_timer_count; i++) {
Timo Teras77900a22006-06-26 16:16:12 -0700695 timer = &dm_timers[i];
Russell Kinge8a91c92008-09-01 22:07:37 +0100696 timer->io_base = IO_ADDRESS(timer->phys_base);
Syed Mohammed, Khasimce2df9c2007-06-25 22:55:39 -0700697#if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
Syed Mohammed, Khasim471b3aa2007-06-21 21:48:07 -0700698 if (cpu_class_is_omap2()) {
699 char clk_name[16];
700 sprintf(clk_name, "gpt%d_ick", i + 1);
701 timer->iclk = clk_get(NULL, clk_name);
702 sprintf(clk_name, "gpt%d_fck", i + 1);
703 timer->fclk = clk_get(NULL, clk_name);
704 }
Timo Teras77900a22006-06-26 16:16:12 -0700705#endif
706 }
707
708 return 0;
709}