blob: 0b5689edf2f2b6a211536ea4673503d8eaa2ac8f [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 King0a5709b2005-11-16 14:51:20 +000035#include <asm/hardware.h>
Tony Lindgren92105bb2005-09-07 17:20:26 +010036#include <asm/arch/dmtimer.h>
37#include <asm/io.h>
38#include <asm/arch/irqs.h>
Tony Lindgren92105bb2005-09-07 17:20:26 +010039
Timo Teras77900a22006-06-26 16:16:12 -070040/* register offsets */
Tony Lindgren92105bb2005-09-07 17:20:26 +010041#define OMAP_TIMER_ID_REG 0x00
42#define OMAP_TIMER_OCP_CFG_REG 0x10
43#define OMAP_TIMER_SYS_STAT_REG 0x14
44#define OMAP_TIMER_STAT_REG 0x18
45#define OMAP_TIMER_INT_EN_REG 0x1c
46#define OMAP_TIMER_WAKEUP_EN_REG 0x20
47#define OMAP_TIMER_CTRL_REG 0x24
48#define OMAP_TIMER_COUNTER_REG 0x28
49#define OMAP_TIMER_LOAD_REG 0x2c
50#define OMAP_TIMER_TRIGGER_REG 0x30
Syed Mohammed, Khasim471b3aa2007-06-21 21:48:07 -070051#define OMAP_TIMER_WRITE_PEND_REG 0x34
Tony Lindgren92105bb2005-09-07 17:20:26 +010052#define OMAP_TIMER_MATCH_REG 0x38
53#define OMAP_TIMER_CAPTURE_REG 0x3c
54#define OMAP_TIMER_IF_CTRL_REG 0x40
55
Timo Teras77900a22006-06-26 16:16:12 -070056/* timer control reg bits */
57#define OMAP_TIMER_CTRL_GPOCFG (1 << 14)
58#define OMAP_TIMER_CTRL_CAPTMODE (1 << 13)
59#define OMAP_TIMER_CTRL_PT (1 << 12)
60#define OMAP_TIMER_CTRL_TCM_LOWTOHIGH (0x1 << 8)
61#define OMAP_TIMER_CTRL_TCM_HIGHTOLOW (0x2 << 8)
62#define OMAP_TIMER_CTRL_TCM_BOTHEDGES (0x3 << 8)
63#define OMAP_TIMER_CTRL_SCPWM (1 << 7)
64#define OMAP_TIMER_CTRL_CE (1 << 6) /* compare enable */
65#define OMAP_TIMER_CTRL_PRE (1 << 5) /* prescaler enable */
66#define OMAP_TIMER_CTRL_PTV_SHIFT 2 /* how much to shift the prescaler value */
67#define OMAP_TIMER_CTRL_AR (1 << 1) /* auto-reload enable */
68#define OMAP_TIMER_CTRL_ST (1 << 0) /* start timer */
Tony Lindgren92105bb2005-09-07 17:20:26 +010069
Timo Teras77900a22006-06-26 16:16:12 -070070struct omap_dm_timer {
71 unsigned long phys_base;
72 int irq;
73#ifdef CONFIG_ARCH_OMAP2
74 struct clk *iclk, *fclk;
75#endif
76 void __iomem *io_base;
77 unsigned reserved:1;
Timo Teras12583a72006-09-25 12:41:42 +030078 unsigned enabled:1;
Tony Lindgren92105bb2005-09-07 17:20:26 +010079};
80
Timo Teras77900a22006-06-26 16:16:12 -070081#ifdef CONFIG_ARCH_OMAP1
82
Timo Terasfa4bb622006-09-25 12:41:35 +030083#define omap_dm_clk_enable(x)
84#define omap_dm_clk_disable(x)
Syed Mohammed, Khasim471b3aa2007-06-21 21:48:07 -070085#define omap2_dm_timers NULL
86#define omap2_dm_source_names NULL
87#define omap2_dm_source_clocks NULL
Timo Terasfa4bb622006-09-25 12:41:35 +030088
Syed Mohammed, Khasim471b3aa2007-06-21 21:48:07 -070089static struct omap_dm_timer omap1_dm_timers[] = {
Timo Teras77900a22006-06-26 16:16:12 -070090 { .phys_base = 0xfffb1400, .irq = INT_1610_GPTIMER1 },
91 { .phys_base = 0xfffb1c00, .irq = INT_1610_GPTIMER2 },
92 { .phys_base = 0xfffb2400, .irq = INT_1610_GPTIMER3 },
93 { .phys_base = 0xfffb2c00, .irq = INT_1610_GPTIMER4 },
94 { .phys_base = 0xfffb3400, .irq = INT_1610_GPTIMER5 },
95 { .phys_base = 0xfffb3c00, .irq = INT_1610_GPTIMER6 },
Matthew Percival53037f42007-01-25 16:24:29 -080096 { .phys_base = 0xfffb7400, .irq = INT_1610_GPTIMER7 },
97 { .phys_base = 0xfffbd400, .irq = INT_1610_GPTIMER8 },
Timo Teras77900a22006-06-26 16:16:12 -070098};
99
Syed Mohammed, Khasim471b3aa2007-06-21 21:48:07 -0700100static const int dm_timer_count = ARRAY_SIZE(omap1_dm_timers);
101
Timo Teras77900a22006-06-26 16:16:12 -0700102#elif defined(CONFIG_ARCH_OMAP2)
103
Syed Mohammed, Khasim471b3aa2007-06-21 21:48:07 -0700104#define omap_dm_clk_enable(x) clk_enable(x)
105#define omap_dm_clk_disable(x) clk_disable(x)
106#define omap1_dm_timers NULL
Timo Terasfa4bb622006-09-25 12:41:35 +0300107
Syed Mohammed, Khasim471b3aa2007-06-21 21:48:07 -0700108static struct omap_dm_timer omap2_dm_timers[] = {
Timo Teras77900a22006-06-26 16:16:12 -0700109 { .phys_base = 0x48028000, .irq = INT_24XX_GPTIMER1 },
110 { .phys_base = 0x4802a000, .irq = INT_24XX_GPTIMER2 },
111 { .phys_base = 0x48078000, .irq = INT_24XX_GPTIMER3 },
112 { .phys_base = 0x4807a000, .irq = INT_24XX_GPTIMER4 },
113 { .phys_base = 0x4807c000, .irq = INT_24XX_GPTIMER5 },
114 { .phys_base = 0x4807e000, .irq = INT_24XX_GPTIMER6 },
115 { .phys_base = 0x48080000, .irq = INT_24XX_GPTIMER7 },
116 { .phys_base = 0x48082000, .irq = INT_24XX_GPTIMER8 },
117 { .phys_base = 0x48084000, .irq = INT_24XX_GPTIMER9 },
118 { .phys_base = 0x48086000, .irq = INT_24XX_GPTIMER10 },
119 { .phys_base = 0x48088000, .irq = INT_24XX_GPTIMER11 },
120 { .phys_base = 0x4808a000, .irq = INT_24XX_GPTIMER12 },
121};
122
Syed Mohammed, Khasim471b3aa2007-06-21 21:48:07 -0700123static const char *omap2_dm_source_names[] __initdata = {
Timo Teras83379c82006-06-26 16:16:23 -0700124 "sys_ck",
125 "func_32k_ck",
Syed Mohammed, Khasim471b3aa2007-06-21 21:48:07 -0700126 "alt_ck",
127 NULL
Timo Teras83379c82006-06-26 16:16:23 -0700128};
129
Syed Mohammed, Khasim471b3aa2007-06-21 21:48:07 -0700130static struct clk **omap2_dm_source_clocks[3];
131static const int dm_timer_count = ARRAY_SIZE(omap2_dm_timers);
Timo Teras83379c82006-06-26 16:16:23 -0700132
Timo Teras77900a22006-06-26 16:16:12 -0700133#else
134
135#error OMAP architecture not supported!
136
137#endif
138
Syed Mohammed, Khasim471b3aa2007-06-21 21:48:07 -0700139static struct omap_dm_timer *dm_timers;
140static char **dm_source_names;
141static struct clk **dm_source_clocks;
142
Tony Lindgren92105bb2005-09-07 17:20:26 +0100143static spinlock_t dm_timer_lock;
144
Timo Teras77900a22006-06-26 16:16:12 -0700145static inline u32 omap_dm_timer_read_reg(struct omap_dm_timer *timer, int reg)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100146{
Timo Teras77900a22006-06-26 16:16:12 -0700147 return readl(timer->io_base + reg);
148}
149
150static void omap_dm_timer_write_reg(struct omap_dm_timer *timer, int reg, u32 value)
151{
152 writel(value, timer->io_base + reg);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100153 while (omap_dm_timer_read_reg(timer, OMAP_TIMER_WRITE_PEND_REG))
154 ;
155}
156
Timo Teras77900a22006-06-26 16:16:12 -0700157static void omap_dm_timer_wait_for_reset(struct omap_dm_timer *timer)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100158{
Timo Teras77900a22006-06-26 16:16:12 -0700159 int c;
160
161 c = 0;
162 while (!(omap_dm_timer_read_reg(timer, OMAP_TIMER_SYS_STAT_REG) & 1)) {
163 c++;
164 if (c > 100000) {
165 printk(KERN_ERR "Timer failed to reset\n");
166 return;
167 }
168 }
Tony Lindgren92105bb2005-09-07 17:20:26 +0100169}
170
Timo Teras77900a22006-06-26 16:16:12 -0700171static void omap_dm_timer_reset(struct omap_dm_timer *timer)
172{
173 u32 l;
174
Juha Yrjola39020842006-09-25 12:41:44 +0300175 if (!cpu_class_is_omap2() || timer != &dm_timers[0]) {
Timo Terase32f7ec2006-06-26 16:16:13 -0700176 omap_dm_timer_write_reg(timer, OMAP_TIMER_IF_CTRL_REG, 0x06);
177 omap_dm_timer_wait_for_reset(timer);
178 }
Timo Teras12583a72006-09-25 12:41:42 +0300179 omap_dm_timer_set_source(timer, OMAP_TIMER_SRC_32_KHZ);
Timo Teras77900a22006-06-26 16:16:12 -0700180
181 /* Set to smart-idle mode */
182 l = omap_dm_timer_read_reg(timer, OMAP_TIMER_OCP_CFG_REG);
183 l |= 0x02 << 3;
Juha Yrjola39020842006-09-25 12:41:44 +0300184
185 if (cpu_class_is_omap2() && timer == &dm_timers[0]) {
186 /* Enable wake-up only for GPT1 on OMAP2 CPUs*/
187 l |= 1 << 2;
188 /* Non-posted mode */
189 omap_dm_timer_write_reg(timer, OMAP_TIMER_IF_CTRL_REG, 0);
190 }
Timo Teras77900a22006-06-26 16:16:12 -0700191 omap_dm_timer_write_reg(timer, OMAP_TIMER_OCP_CFG_REG, l);
192}
193
Timo Teras83379c82006-06-26 16:16:23 -0700194static void omap_dm_timer_prepare(struct omap_dm_timer *timer)
Timo Teras77900a22006-06-26 16:16:12 -0700195{
Timo Teras12583a72006-09-25 12:41:42 +0300196 omap_dm_timer_enable(timer);
Timo Teras77900a22006-06-26 16:16:12 -0700197 omap_dm_timer_reset(timer);
198}
199
200struct omap_dm_timer *omap_dm_timer_request(void)
201{
202 struct omap_dm_timer *timer = NULL;
203 unsigned long flags;
204 int i;
205
206 spin_lock_irqsave(&dm_timer_lock, flags);
207 for (i = 0; i < dm_timer_count; i++) {
208 if (dm_timers[i].reserved)
209 continue;
210
211 timer = &dm_timers[i];
Timo Teras83379c82006-06-26 16:16:23 -0700212 timer->reserved = 1;
Timo Teras77900a22006-06-26 16:16:12 -0700213 break;
214 }
215 spin_unlock_irqrestore(&dm_timer_lock, flags);
216
Timo Teras83379c82006-06-26 16:16:23 -0700217 if (timer != NULL)
218 omap_dm_timer_prepare(timer);
219
Timo Teras77900a22006-06-26 16:16:12 -0700220 return timer;
221}
222
223struct omap_dm_timer *omap_dm_timer_request_specific(int id)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100224{
225 struct omap_dm_timer *timer;
Timo Teras77900a22006-06-26 16:16:12 -0700226 unsigned long flags;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100227
Timo Teras77900a22006-06-26 16:16:12 -0700228 spin_lock_irqsave(&dm_timer_lock, flags);
229 if (id <= 0 || id > dm_timer_count || dm_timers[id-1].reserved) {
230 spin_unlock_irqrestore(&dm_timer_lock, flags);
231 printk("BUG: warning at %s:%d/%s(): unable to get timer %d\n",
232 __FILE__, __LINE__, __FUNCTION__, id);
233 dump_stack();
234 return NULL;
235 }
Tony Lindgren92105bb2005-09-07 17:20:26 +0100236
Timo Teras77900a22006-06-26 16:16:12 -0700237 timer = &dm_timers[id-1];
Timo Teras83379c82006-06-26 16:16:23 -0700238 timer->reserved = 1;
Timo Teras77900a22006-06-26 16:16:12 -0700239 spin_unlock_irqrestore(&dm_timer_lock, flags);
240
Timo Teras83379c82006-06-26 16:16:23 -0700241 omap_dm_timer_prepare(timer);
242
Timo Teras77900a22006-06-26 16:16:12 -0700243 return timer;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100244}
245
Timo Teras77900a22006-06-26 16:16:12 -0700246void omap_dm_timer_free(struct omap_dm_timer *timer)
247{
Timo Teras12583a72006-09-25 12:41:42 +0300248 omap_dm_timer_enable(timer);
Timo Teras77900a22006-06-26 16:16:12 -0700249 omap_dm_timer_reset(timer);
Timo Teras12583a72006-09-25 12:41:42 +0300250 omap_dm_timer_disable(timer);
Timo Terasfa4bb622006-09-25 12:41:35 +0300251
Timo Teras77900a22006-06-26 16:16:12 -0700252 WARN_ON(!timer->reserved);
253 timer->reserved = 0;
254}
255
Timo Teras12583a72006-09-25 12:41:42 +0300256void omap_dm_timer_enable(struct omap_dm_timer *timer)
257{
258 if (timer->enabled)
259 return;
260
261 omap_dm_clk_enable(timer->fclk);
262 omap_dm_clk_enable(timer->iclk);
263
264 timer->enabled = 1;
265}
266
267void omap_dm_timer_disable(struct omap_dm_timer *timer)
268{
269 if (!timer->enabled)
270 return;
271
272 omap_dm_clk_disable(timer->iclk);
273 omap_dm_clk_disable(timer->fclk);
274
275 timer->enabled = 0;
276}
277
Timo Teras77900a22006-06-26 16:16:12 -0700278int omap_dm_timer_get_irq(struct omap_dm_timer *timer)
279{
280 return timer->irq;
281}
282
283#if defined(CONFIG_ARCH_OMAP1)
284
Tony Lindgrena569c6e2006-04-02 17:46:21 +0100285/**
286 * omap_dm_timer_modify_idlect_mask - Check if any running timers use ARMXOR
287 * @inputmask: current value of idlect mask
288 */
289__u32 omap_dm_timer_modify_idlect_mask(__u32 inputmask)
290{
Timo Teras77900a22006-06-26 16:16:12 -0700291 int i;
Tony Lindgrena569c6e2006-04-02 17:46:21 +0100292
293 /* If ARMXOR cannot be idled this function call is unnecessary */
294 if (!(inputmask & (1 << 1)))
295 return inputmask;
296
297 /* If any active timer is using ARMXOR return modified mask */
Timo Teras77900a22006-06-26 16:16:12 -0700298 for (i = 0; i < dm_timer_count; i++) {
299 u32 l;
300
Tony Lindgren35912c72006-07-01 19:56:42 +0100301 l = omap_dm_timer_read_reg(&dm_timers[i], OMAP_TIMER_CTRL_REG);
Timo Teras77900a22006-06-26 16:16:12 -0700302 if (l & OMAP_TIMER_CTRL_ST) {
303 if (((omap_readl(MOD_CONF_CTRL_1) >> (i * 2)) & 0x03) == 0)
Tony Lindgrena569c6e2006-04-02 17:46:21 +0100304 inputmask &= ~(1 << 1);
305 else
306 inputmask &= ~(1 << 2);
307 }
Timo Teras77900a22006-06-26 16:16:12 -0700308 }
Tony Lindgrena569c6e2006-04-02 17:46:21 +0100309
310 return inputmask;
311}
312
Timo Teras77900a22006-06-26 16:16:12 -0700313#elif defined(CONFIG_ARCH_OMAP2)
314
315struct clk *omap_dm_timer_get_fclk(struct omap_dm_timer *timer)
316{
Timo Terasfa4bb622006-09-25 12:41:35 +0300317 return timer->fclk;
Timo Teras77900a22006-06-26 16:16:12 -0700318}
319
320__u32 omap_dm_timer_modify_idlect_mask(__u32 inputmask)
321{
322 BUG();
Dirk Behme21218802006-12-06 17:14:00 -0800323
324 return 0;
Timo Teras77900a22006-06-26 16:16:12 -0700325}
326
327#endif
328
329void omap_dm_timer_trigger(struct omap_dm_timer *timer)
330{
331 omap_dm_timer_write_reg(timer, OMAP_TIMER_TRIGGER_REG, 0);
332}
333
334void omap_dm_timer_start(struct omap_dm_timer *timer)
335{
336 u32 l;
337
338 l = omap_dm_timer_read_reg(timer, OMAP_TIMER_CTRL_REG);
339 if (!(l & OMAP_TIMER_CTRL_ST)) {
340 l |= OMAP_TIMER_CTRL_ST;
341 omap_dm_timer_write_reg(timer, OMAP_TIMER_CTRL_REG, l);
342 }
343}
344
345void omap_dm_timer_stop(struct omap_dm_timer *timer)
346{
347 u32 l;
348
349 l = omap_dm_timer_read_reg(timer, OMAP_TIMER_CTRL_REG);
350 if (l & OMAP_TIMER_CTRL_ST) {
351 l &= ~0x1;
352 omap_dm_timer_write_reg(timer, OMAP_TIMER_CTRL_REG, l);
353 }
354}
355
356#ifdef CONFIG_ARCH_OMAP1
Tony Lindgrena569c6e2006-04-02 17:46:21 +0100357
Tony Lindgren92105bb2005-09-07 17:20:26 +0100358void omap_dm_timer_set_source(struct omap_dm_timer *timer, int source)
359{
360 int n = (timer - dm_timers) << 1;
361 u32 l;
362
363 l = omap_readl(MOD_CONF_CTRL_1) & ~(0x03 << n);
364 l |= source << n;
365 omap_writel(l, MOD_CONF_CTRL_1);
366}
367
Timo Teras77900a22006-06-26 16:16:12 -0700368#else
Tony Lindgren92105bb2005-09-07 17:20:26 +0100369
Timo Teras77900a22006-06-26 16:16:12 -0700370void omap_dm_timer_set_source(struct omap_dm_timer *timer, int source)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100371{
Timo Teras77900a22006-06-26 16:16:12 -0700372 if (source < 0 || source >= 3)
373 return;
374
Timo Teras77900a22006-06-26 16:16:12 -0700375 clk_disable(timer->fclk);
Timo Teras83379c82006-06-26 16:16:23 -0700376 clk_set_parent(timer->fclk, dm_source_clocks[source]);
Timo Teras77900a22006-06-26 16:16:12 -0700377 clk_enable(timer->fclk);
Timo Teras77900a22006-06-26 16:16:12 -0700378
379 /* When the functional clock disappears, too quick writes seem to
380 * cause an abort. */
Tony Lindgrenc40fae952006-12-07 13:58:10 -0800381 __delay(150000);
Timo Teras77900a22006-06-26 16:16:12 -0700382}
383
384#endif
385
386void omap_dm_timer_set_load(struct omap_dm_timer *timer, int autoreload,
387 unsigned int load)
388{
389 u32 l;
390
391 l = omap_dm_timer_read_reg(timer, OMAP_TIMER_CTRL_REG);
392 if (autoreload)
393 l |= OMAP_TIMER_CTRL_AR;
394 else
395 l &= ~OMAP_TIMER_CTRL_AR;
396 omap_dm_timer_write_reg(timer, OMAP_TIMER_CTRL_REG, l);
397 omap_dm_timer_write_reg(timer, OMAP_TIMER_LOAD_REG, load);
398 omap_dm_timer_write_reg(timer, OMAP_TIMER_TRIGGER_REG, 0);
399}
400
401void omap_dm_timer_set_match(struct omap_dm_timer *timer, int enable,
402 unsigned int match)
403{
404 u32 l;
405
406 l = omap_dm_timer_read_reg(timer, OMAP_TIMER_CTRL_REG);
Timo Teras83379c82006-06-26 16:16:23 -0700407 if (enable)
Timo Teras77900a22006-06-26 16:16:12 -0700408 l |= OMAP_TIMER_CTRL_CE;
409 else
410 l &= ~OMAP_TIMER_CTRL_CE;
411 omap_dm_timer_write_reg(timer, OMAP_TIMER_CTRL_REG, l);
412 omap_dm_timer_write_reg(timer, OMAP_TIMER_MATCH_REG, match);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100413}
414
415
Timo Teras77900a22006-06-26 16:16:12 -0700416void omap_dm_timer_set_pwm(struct omap_dm_timer *timer, int def_on,
417 int toggle, int trigger)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100418{
Timo Teras77900a22006-06-26 16:16:12 -0700419 u32 l;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100420
Timo Teras77900a22006-06-26 16:16:12 -0700421 l = omap_dm_timer_read_reg(timer, OMAP_TIMER_CTRL_REG);
422 l &= ~(OMAP_TIMER_CTRL_GPOCFG | OMAP_TIMER_CTRL_SCPWM |
423 OMAP_TIMER_CTRL_PT | (0x03 << 10));
424 if (def_on)
425 l |= OMAP_TIMER_CTRL_SCPWM;
426 if (toggle)
427 l |= OMAP_TIMER_CTRL_PT;
428 l |= trigger << 10;
429 omap_dm_timer_write_reg(timer, OMAP_TIMER_CTRL_REG, l);
430}
431
432void omap_dm_timer_set_prescaler(struct omap_dm_timer *timer, int prescaler)
433{
434 u32 l;
435
436 l = omap_dm_timer_read_reg(timer, OMAP_TIMER_CTRL_REG);
437 l &= ~(OMAP_TIMER_CTRL_PRE | (0x07 << 2));
438 if (prescaler >= 0x00 && prescaler <= 0x07) {
439 l |= OMAP_TIMER_CTRL_PRE;
440 l |= prescaler << 2;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100441 }
Timo Teras77900a22006-06-26 16:16:12 -0700442 omap_dm_timer_write_reg(timer, OMAP_TIMER_CTRL_REG, l);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100443}
444
445void omap_dm_timer_set_int_enable(struct omap_dm_timer *timer,
Timo Teras77900a22006-06-26 16:16:12 -0700446 unsigned int value)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100447{
448 omap_dm_timer_write_reg(timer, OMAP_TIMER_INT_EN_REG, value);
Juha Yrjola39020842006-09-25 12:41:44 +0300449 omap_dm_timer_write_reg(timer, OMAP_TIMER_WAKEUP_EN_REG, value);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100450}
451
452unsigned int omap_dm_timer_read_status(struct omap_dm_timer *timer)
453{
Timo Terasfa4bb622006-09-25 12:41:35 +0300454 unsigned int l;
455
Timo Terasfa4bb622006-09-25 12:41:35 +0300456 l = omap_dm_timer_read_reg(timer, OMAP_TIMER_STAT_REG);
Timo Terasfa4bb622006-09-25 12:41:35 +0300457
458 return l;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100459}
460
461void omap_dm_timer_write_status(struct omap_dm_timer *timer, unsigned int value)
462{
463 omap_dm_timer_write_reg(timer, OMAP_TIMER_STAT_REG, value);
464}
465
Tony Lindgren92105bb2005-09-07 17:20:26 +0100466unsigned int omap_dm_timer_read_counter(struct omap_dm_timer *timer)
467{
Timo Terasfa4bb622006-09-25 12:41:35 +0300468 unsigned int l;
469
Timo Terasfa4bb622006-09-25 12:41:35 +0300470 l = omap_dm_timer_read_reg(timer, OMAP_TIMER_COUNTER_REG);
Timo Terasfa4bb622006-09-25 12:41:35 +0300471
472 return l;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100473}
474
Timo Teras83379c82006-06-26 16:16:23 -0700475void omap_dm_timer_write_counter(struct omap_dm_timer *timer, unsigned int value)
476{
Timo Terasfa4bb622006-09-25 12:41:35 +0300477 omap_dm_timer_write_reg(timer, OMAP_TIMER_COUNTER_REG, value);
Timo Teras83379c82006-06-26 16:16:23 -0700478}
479
Timo Teras77900a22006-06-26 16:16:12 -0700480int omap_dm_timers_active(void)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100481{
Timo Teras77900a22006-06-26 16:16:12 -0700482 int i;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100483
Timo Teras77900a22006-06-26 16:16:12 -0700484 for (i = 0; i < dm_timer_count; i++) {
485 struct omap_dm_timer *timer;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100486
Timo Teras77900a22006-06-26 16:16:12 -0700487 timer = &dm_timers[i];
Timo Teras12583a72006-09-25 12:41:42 +0300488
489 if (!timer->enabled)
490 continue;
491
Timo Teras77900a22006-06-26 16:16:12 -0700492 if (omap_dm_timer_read_reg(timer, OMAP_TIMER_CTRL_REG) &
Timo Terasfa4bb622006-09-25 12:41:35 +0300493 OMAP_TIMER_CTRL_ST) {
Timo Teras77900a22006-06-26 16:16:12 -0700494 return 1;
Timo Terasfa4bb622006-09-25 12:41:35 +0300495 }
Tony Lindgren92105bb2005-09-07 17:20:26 +0100496 }
Tony Lindgren92105bb2005-09-07 17:20:26 +0100497 return 0;
498}
499
Syed Mohammed, Khasim471b3aa2007-06-21 21:48:07 -0700500int __init omap_dm_timer_init(void)
Timo Teras77900a22006-06-26 16:16:12 -0700501{
502 struct omap_dm_timer *timer;
503 int i;
504
505 if (!(cpu_is_omap16xx() || cpu_is_omap24xx()))
506 return -ENODEV;
507
508 spin_lock_init(&dm_timer_lock);
Syed Mohammed, Khasim471b3aa2007-06-21 21:48:07 -0700509
510 if (cpu_class_is_omap1())
511 dm_timers = omap1_dm_timers;
512 else if (cpu_is_omap24xx()) {
513 dm_timers = omap2_dm_timers;
514 dm_source_names = (char **)omap2_dm_source_names;
515 dm_source_clocks = (struct clk **)omap2_dm_source_clocks;
Timo Teras83379c82006-06-26 16:16:23 -0700516 }
Syed Mohammed, Khasim471b3aa2007-06-21 21:48:07 -0700517
518 if (cpu_class_is_omap2())
519 for (i = 0; dm_source_names[i] != NULL; i++)
520 dm_source_clocks[i] = clk_get(NULL, dm_source_names[i]);
521
Syed Mohammed Khasim56a25642006-12-06 17:14:08 -0800522 if (cpu_is_omap243x())
523 dm_timers[0].phys_base = 0x49018000;
Timo Teras83379c82006-06-26 16:16:23 -0700524
Timo Teras77900a22006-06-26 16:16:12 -0700525 for (i = 0; i < dm_timer_count; i++) {
Timo Teras77900a22006-06-26 16:16:12 -0700526 timer = &dm_timers[i];
Syed Mohammed, Khasim471b3aa2007-06-21 21:48:07 -0700527 timer->io_base = (void __iomem *)io_p2v(timer->phys_base);
Timo Teras77900a22006-06-26 16:16:12 -0700528#ifdef CONFIG_ARCH_OMAP2
Syed Mohammed, Khasim471b3aa2007-06-21 21:48:07 -0700529 if (cpu_class_is_omap2()) {
530 char clk_name[16];
531 sprintf(clk_name, "gpt%d_ick", i + 1);
532 timer->iclk = clk_get(NULL, clk_name);
533 sprintf(clk_name, "gpt%d_fck", i + 1);
534 timer->fclk = clk_get(NULL, clk_name);
535 }
Timo Teras77900a22006-06-26 16:16:12 -0700536#endif
537 }
538
539 return 0;
540}