blob: 75a847dd776a3d24ccca85c29aab32b90bbbe3e7 [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
Tony Lindgren882c0512010-02-12 12:26:46 -080044static int dm_timer_count;
45
Timo Teras77900a22006-06-26 16:16:12 -070046#ifdef CONFIG_ARCH_OMAP1
Syed Mohammed, Khasim471b3aa2007-06-21 21:48:07 -070047static struct omap_dm_timer omap1_dm_timers[] = {
Timo Teras77900a22006-06-26 16:16:12 -070048 { .phys_base = 0xfffb1400, .irq = INT_1610_GPTIMER1 },
49 { .phys_base = 0xfffb1c00, .irq = INT_1610_GPTIMER2 },
50 { .phys_base = 0xfffb2400, .irq = INT_1610_GPTIMER3 },
51 { .phys_base = 0xfffb2c00, .irq = INT_1610_GPTIMER4 },
52 { .phys_base = 0xfffb3400, .irq = INT_1610_GPTIMER5 },
53 { .phys_base = 0xfffb3c00, .irq = INT_1610_GPTIMER6 },
Matthew Percival53037f42007-01-25 16:24:29 -080054 { .phys_base = 0xfffb7400, .irq = INT_1610_GPTIMER7 },
55 { .phys_base = 0xfffbd400, .irq = INT_1610_GPTIMER8 },
Timo Teras77900a22006-06-26 16:16:12 -070056};
57
Tony Lindgren882c0512010-02-12 12:26:46 -080058static const int omap1_dm_timer_count = ARRAY_SIZE(omap1_dm_timers);
Syed Mohammed, Khasim471b3aa2007-06-21 21:48:07 -070059
Tony Lindgren882c0512010-02-12 12:26:46 -080060#else
Syed Mohammed, Khasim471b3aa2007-06-21 21:48:07 -070061#define omap1_dm_timers NULL
Tony Lindgren882c0512010-02-12 12:26:46 -080062#define omap1_dm_timer_count 0
63#endif /* CONFIG_ARCH_OMAP1 */
Timo Terasfa4bb622006-09-25 12:41:35 +030064
Tony Lindgren882c0512010-02-12 12:26:46 -080065#ifdef CONFIG_ARCH_OMAP2
Syed Mohammed, Khasim471b3aa2007-06-21 21:48:07 -070066static struct omap_dm_timer omap2_dm_timers[] = {
Timo Teras77900a22006-06-26 16:16:12 -070067 { .phys_base = 0x48028000, .irq = INT_24XX_GPTIMER1 },
68 { .phys_base = 0x4802a000, .irq = INT_24XX_GPTIMER2 },
69 { .phys_base = 0x48078000, .irq = INT_24XX_GPTIMER3 },
70 { .phys_base = 0x4807a000, .irq = INT_24XX_GPTIMER4 },
71 { .phys_base = 0x4807c000, .irq = INT_24XX_GPTIMER5 },
72 { .phys_base = 0x4807e000, .irq = INT_24XX_GPTIMER6 },
73 { .phys_base = 0x48080000, .irq = INT_24XX_GPTIMER7 },
74 { .phys_base = 0x48082000, .irq = INT_24XX_GPTIMER8 },
75 { .phys_base = 0x48084000, .irq = INT_24XX_GPTIMER9 },
76 { .phys_base = 0x48086000, .irq = INT_24XX_GPTIMER10 },
77 { .phys_base = 0x48088000, .irq = INT_24XX_GPTIMER11 },
78 { .phys_base = 0x4808a000, .irq = INT_24XX_GPTIMER12 },
79};
80
Syed Mohammed, Khasim471b3aa2007-06-21 21:48:07 -070081static const char *omap2_dm_source_names[] __initdata = {
Timo Teras83379c82006-06-26 16:16:23 -070082 "sys_ck",
83 "func_32k_ck",
Syed Mohammed, Khasim471b3aa2007-06-21 21:48:07 -070084 "alt_ck",
85 NULL
Timo Teras83379c82006-06-26 16:16:23 -070086};
87
Santosh Shilimkaraea2a5b2009-05-25 11:08:36 -070088static struct clk *omap2_dm_source_clocks[3];
Tony Lindgren882c0512010-02-12 12:26:46 -080089static const int omap2_dm_timer_count = ARRAY_SIZE(omap2_dm_timers);
Timo Teras83379c82006-06-26 16:16:23 -070090
Tony Lindgren882c0512010-02-12 12:26:46 -080091#else
Syed Mohammed, Khasimce2df9c2007-06-25 22:55:39 -070092#define omap2_dm_timers NULL
Tony Lindgren882c0512010-02-12 12:26:46 -080093#define omap2_dm_timer_count 0
Syed Mohammed, Khasimce2df9c2007-06-25 22:55:39 -070094#define omap2_dm_source_names NULL
95#define omap2_dm_source_clocks NULL
Tony Lindgren882c0512010-02-12 12:26:46 -080096#endif /* CONFIG_ARCH_OMAP2 */
Syed Mohammed, Khasimce2df9c2007-06-25 22:55:39 -070097
Tony Lindgren882c0512010-02-12 12:26:46 -080098#ifdef CONFIG_ARCH_OMAP3
Syed Mohammed, Khasimce2df9c2007-06-25 22:55:39 -070099static struct omap_dm_timer omap3_dm_timers[] = {
100 { .phys_base = 0x48318000, .irq = INT_24XX_GPTIMER1 },
101 { .phys_base = 0x49032000, .irq = INT_24XX_GPTIMER2 },
102 { .phys_base = 0x49034000, .irq = INT_24XX_GPTIMER3 },
103 { .phys_base = 0x49036000, .irq = INT_24XX_GPTIMER4 },
104 { .phys_base = 0x49038000, .irq = INT_24XX_GPTIMER5 },
105 { .phys_base = 0x4903A000, .irq = INT_24XX_GPTIMER6 },
106 { .phys_base = 0x4903C000, .irq = INT_24XX_GPTIMER7 },
107 { .phys_base = 0x4903E000, .irq = INT_24XX_GPTIMER8 },
108 { .phys_base = 0x49040000, .irq = INT_24XX_GPTIMER9 },
109 { .phys_base = 0x48086000, .irq = INT_24XX_GPTIMER10 },
110 { .phys_base = 0x48088000, .irq = INT_24XX_GPTIMER11 },
Paul Walmsley9198a402009-04-23 21:11:08 -0600111 { .phys_base = 0x48304000, .irq = INT_34XX_GPT12_IRQ },
Syed Mohammed, Khasimce2df9c2007-06-25 22:55:39 -0700112};
113
114static const char *omap3_dm_source_names[] __initdata = {
115 "sys_ck",
116 "omap_32k_fck",
117 NULL
118};
119
Santosh Shilimkaraea2a5b2009-05-25 11:08:36 -0700120static struct clk *omap3_dm_source_clocks[2];
Tony Lindgren882c0512010-02-12 12:26:46 -0800121static const int omap3_dm_timer_count = ARRAY_SIZE(omap3_dm_timers);
Syed Mohammed, Khasimce2df9c2007-06-25 22:55:39 -0700122
Tony Lindgren882c0512010-02-12 12:26:46 -0800123#else
Santosh Shilimkar44169072009-05-28 14:16:04 -0700124#define omap3_dm_timers NULL
Tony Lindgren882c0512010-02-12 12:26:46 -0800125#define omap3_dm_timer_count 0
Santosh Shilimkar44169072009-05-28 14:16:04 -0700126#define omap3_dm_source_names NULL
127#define omap3_dm_source_clocks NULL
Tony Lindgren882c0512010-02-12 12:26:46 -0800128#endif /* CONFIG_ARCH_OMAP3 */
Santosh Shilimkar44169072009-05-28 14:16:04 -0700129
Tony Lindgren882c0512010-02-12 12:26:46 -0800130#ifdef CONFIG_ARCH_OMAP4
Santosh Shilimkar44169072009-05-28 14:16:04 -0700131static struct omap_dm_timer omap4_dm_timers[] = {
Santosh Shilimkar5772ca72010-02-18 03:14:12 +0530132 { .phys_base = 0x4a318000, .irq = OMAP44XX_IRQ_GPT1 },
133 { .phys_base = 0x48032000, .irq = OMAP44XX_IRQ_GPT2 },
134 { .phys_base = 0x48034000, .irq = OMAP44XX_IRQ_GPT3 },
135 { .phys_base = 0x48036000, .irq = OMAP44XX_IRQ_GPT4 },
136 { .phys_base = 0x40138000, .irq = OMAP44XX_IRQ_GPT5 },
137 { .phys_base = 0x4013a000, .irq = OMAP44XX_IRQ_GPT6 },
138 { .phys_base = 0x4013a000, .irq = OMAP44XX_IRQ_GPT7 },
139 { .phys_base = 0x4013e000, .irq = OMAP44XX_IRQ_GPT8 },
140 { .phys_base = 0x4803e000, .irq = OMAP44XX_IRQ_GPT9 },
141 { .phys_base = 0x48086000, .irq = OMAP44XX_IRQ_GPT10 },
142 { .phys_base = 0x48088000, .irq = OMAP44XX_IRQ_GPT11 },
143 { .phys_base = 0x4a320000, .irq = OMAP44XX_IRQ_GPT12 },
Santosh Shilimkar44169072009-05-28 14:16:04 -0700144};
145static const char *omap4_dm_source_names[] __initdata = {
Rajendra Nayak1dc993b2010-05-18 20:24:00 -0600146 "sys_clkin_ck",
147 "sys_32k_ck",
Santosh Shilimkar44169072009-05-28 14:16:04 -0700148 NULL
149};
150static struct clk *omap4_dm_source_clocks[2];
Tony Lindgren882c0512010-02-12 12:26:46 -0800151static const int omap4_dm_timer_count = ARRAY_SIZE(omap4_dm_timers);
Santosh Shilimkar44169072009-05-28 14:16:04 -0700152
Timo Teras77900a22006-06-26 16:16:12 -0700153#else
Tony Lindgren882c0512010-02-12 12:26:46 -0800154#define omap4_dm_timers NULL
155#define omap4_dm_timer_count 0
156#define omap4_dm_source_names NULL
157#define omap4_dm_source_clocks NULL
158#endif /* CONFIG_ARCH_OMAP4 */
Timo Teras77900a22006-06-26 16:16:12 -0700159
Syed Mohammed, Khasim471b3aa2007-06-21 21:48:07 -0700160static struct omap_dm_timer *dm_timers;
Santosh Shilimkaraea2a5b2009-05-25 11:08:36 -0700161static const char **dm_source_names;
Syed Mohammed, Khasim471b3aa2007-06-21 21:48:07 -0700162static struct clk **dm_source_clocks;
163
Tony Lindgren92105bb2005-09-07 17:20:26 +0100164static spinlock_t dm_timer_lock;
165
Richard Woodruff0f0d0802008-07-03 12:24:30 +0300166/*
167 * Reads timer registers in posted and non-posted mode. The posted mode bit
168 * is encoded in reg. Note that in posted mode write pending bit must be
169 * checked. Otherwise a read of a non completed write will produce an error.
170 */
171static inline u32 omap_dm_timer_read_reg(struct omap_dm_timer *timer, u32 reg)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100172{
Tony Lindgrencaf64f22011-03-29 15:54:48 -0700173 return __omap_dm_timer_read(timer->io_base, reg, timer->posted);
Timo Teras77900a22006-06-26 16:16:12 -0700174}
175
Richard Woodruff0f0d0802008-07-03 12:24:30 +0300176/*
177 * Writes timer registers in posted and non-posted mode. The posted mode bit
178 * is encoded in reg. Note that in posted mode the write pending bit must be
179 * checked. Otherwise a write on a register which has a pending write will be
180 * lost.
181 */
182static void omap_dm_timer_write_reg(struct omap_dm_timer *timer, u32 reg,
183 u32 value)
Timo Teras77900a22006-06-26 16:16:12 -0700184{
Tony Lindgrencaf64f22011-03-29 15:54:48 -0700185 __omap_dm_timer_write(timer->io_base, reg, value, timer->posted);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100186}
187
Timo Teras77900a22006-06-26 16:16:12 -0700188static void omap_dm_timer_wait_for_reset(struct omap_dm_timer *timer)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100189{
Timo Teras77900a22006-06-26 16:16:12 -0700190 int c;
191
192 c = 0;
193 while (!(omap_dm_timer_read_reg(timer, OMAP_TIMER_SYS_STAT_REG) & 1)) {
194 c++;
195 if (c > 100000) {
196 printk(KERN_ERR "Timer failed to reset\n");
197 return;
198 }
199 }
Tony Lindgren92105bb2005-09-07 17:20:26 +0100200}
201
Timo Teras77900a22006-06-26 16:16:12 -0700202static void omap_dm_timer_reset(struct omap_dm_timer *timer)
203{
Tony Lindgrencaf64f22011-03-29 15:54:48 -0700204 int autoidle = 0, wakeup = 0;
Timo Teras77900a22006-06-26 16:16:12 -0700205
Juha Yrjola39020842006-09-25 12:41:44 +0300206 if (!cpu_class_is_omap2() || timer != &dm_timers[0]) {
Timo Terase32f7ec2006-06-26 16:16:13 -0700207 omap_dm_timer_write_reg(timer, OMAP_TIMER_IF_CTRL_REG, 0x06);
208 omap_dm_timer_wait_for_reset(timer);
209 }
Timo Teras12583a72006-09-25 12:41:42 +0300210 omap_dm_timer_set_source(timer, OMAP_TIMER_SRC_32_KHZ);
Timo Teras77900a22006-06-26 16:16:12 -0700211
Ambresh Kba503482011-06-15 21:12:35 +0000212 /* Enable autoidle on OMAP2+ */
213 if (cpu_class_is_omap2())
Tony Lindgrencaf64f22011-03-29 15:54:48 -0700214 autoidle = 1;
Tero Kristo4ce1e5e2011-03-10 03:50:54 -0700215
Richard Woodruff0f0d0802008-07-03 12:24:30 +0300216 /*
Kevin Hilman219c5b92009-04-23 21:11:08 -0600217 * Enable wake-up on OMAP2 CPUs.
Richard Woodruff0f0d0802008-07-03 12:24:30 +0300218 */
Kevin Hilman219c5b92009-04-23 21:11:08 -0600219 if (cpu_class_is_omap2())
Tony Lindgrencaf64f22011-03-29 15:54:48 -0700220 wakeup = 1;
Richard Woodruff0f0d0802008-07-03 12:24:30 +0300221
Tony Lindgrencaf64f22011-03-29 15:54:48 -0700222 __omap_dm_timer_reset(timer->io_base, autoidle, wakeup);
Richard Woodruff0f0d0802008-07-03 12:24:30 +0300223 timer->posted = 1;
Timo Teras77900a22006-06-26 16:16:12 -0700224}
225
Tony Lindgrencaf64f22011-03-29 15:54:48 -0700226void omap_dm_timer_prepare(struct omap_dm_timer *timer)
Timo Teras77900a22006-06-26 16:16:12 -0700227{
Timo Teras12583a72006-09-25 12:41:42 +0300228 omap_dm_timer_enable(timer);
Timo Teras77900a22006-06-26 16:16:12 -0700229 omap_dm_timer_reset(timer);
230}
231
232struct omap_dm_timer *omap_dm_timer_request(void)
233{
234 struct omap_dm_timer *timer = NULL;
235 unsigned long flags;
236 int i;
237
238 spin_lock_irqsave(&dm_timer_lock, flags);
239 for (i = 0; i < dm_timer_count; i++) {
240 if (dm_timers[i].reserved)
241 continue;
242
243 timer = &dm_timers[i];
Timo Teras83379c82006-06-26 16:16:23 -0700244 timer->reserved = 1;
Timo Teras77900a22006-06-26 16:16:12 -0700245 break;
246 }
247 spin_unlock_irqrestore(&dm_timer_lock, flags);
248
Timo Teras83379c82006-06-26 16:16:23 -0700249 if (timer != NULL)
250 omap_dm_timer_prepare(timer);
251
Timo Teras77900a22006-06-26 16:16:12 -0700252 return timer;
253}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700254EXPORT_SYMBOL_GPL(omap_dm_timer_request);
Timo Teras77900a22006-06-26 16:16:12 -0700255
256struct omap_dm_timer *omap_dm_timer_request_specific(int id)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100257{
258 struct omap_dm_timer *timer;
Timo Teras77900a22006-06-26 16:16:12 -0700259 unsigned long flags;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100260
Timo Teras77900a22006-06-26 16:16:12 -0700261 spin_lock_irqsave(&dm_timer_lock, flags);
262 if (id <= 0 || id > dm_timer_count || dm_timers[id-1].reserved) {
263 spin_unlock_irqrestore(&dm_timer_lock, flags);
264 printk("BUG: warning at %s:%d/%s(): unable to get timer %d\n",
Harvey Harrison8e86f422008-03-04 15:08:02 -0800265 __FILE__, __LINE__, __func__, id);
Timo Teras77900a22006-06-26 16:16:12 -0700266 dump_stack();
267 return NULL;
268 }
Tony Lindgren92105bb2005-09-07 17:20:26 +0100269
Timo Teras77900a22006-06-26 16:16:12 -0700270 timer = &dm_timers[id-1];
Timo Teras83379c82006-06-26 16:16:23 -0700271 timer->reserved = 1;
Timo Teras77900a22006-06-26 16:16:12 -0700272 spin_unlock_irqrestore(&dm_timer_lock, flags);
273
Timo Teras83379c82006-06-26 16:16:23 -0700274 omap_dm_timer_prepare(timer);
275
Timo Teras77900a22006-06-26 16:16:12 -0700276 return timer;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100277}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700278EXPORT_SYMBOL_GPL(omap_dm_timer_request_specific);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100279
Timo Teras77900a22006-06-26 16:16:12 -0700280void omap_dm_timer_free(struct omap_dm_timer *timer)
281{
Timo Teras12583a72006-09-25 12:41:42 +0300282 omap_dm_timer_enable(timer);
Timo Teras77900a22006-06-26 16:16:12 -0700283 omap_dm_timer_reset(timer);
Timo Teras12583a72006-09-25 12:41:42 +0300284 omap_dm_timer_disable(timer);
Timo Terasfa4bb622006-09-25 12:41:35 +0300285
Timo Teras77900a22006-06-26 16:16:12 -0700286 WARN_ON(!timer->reserved);
287 timer->reserved = 0;
288}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700289EXPORT_SYMBOL_GPL(omap_dm_timer_free);
Timo Teras77900a22006-06-26 16:16:12 -0700290
Timo Teras12583a72006-09-25 12:41:42 +0300291void omap_dm_timer_enable(struct omap_dm_timer *timer)
292{
293 if (timer->enabled)
294 return;
295
Tony Lindgren882c0512010-02-12 12:26:46 -0800296#ifdef CONFIG_ARCH_OMAP2PLUS
297 if (cpu_class_is_omap2()) {
298 clk_enable(timer->fclk);
299 clk_enable(timer->iclk);
300 }
301#endif
Timo Teras12583a72006-09-25 12:41:42 +0300302
303 timer->enabled = 1;
304}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700305EXPORT_SYMBOL_GPL(omap_dm_timer_enable);
Timo Teras12583a72006-09-25 12:41:42 +0300306
307void omap_dm_timer_disable(struct omap_dm_timer *timer)
308{
309 if (!timer->enabled)
310 return;
311
Tony Lindgren882c0512010-02-12 12:26:46 -0800312#ifdef CONFIG_ARCH_OMAP2PLUS
313 if (cpu_class_is_omap2()) {
314 clk_disable(timer->iclk);
315 clk_disable(timer->fclk);
316 }
317#endif
Timo Teras12583a72006-09-25 12:41:42 +0300318
319 timer->enabled = 0;
320}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700321EXPORT_SYMBOL_GPL(omap_dm_timer_disable);
Timo Teras12583a72006-09-25 12:41:42 +0300322
Timo Teras77900a22006-06-26 16:16:12 -0700323int omap_dm_timer_get_irq(struct omap_dm_timer *timer)
324{
325 return timer->irq;
326}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700327EXPORT_SYMBOL_GPL(omap_dm_timer_get_irq);
Timo Teras77900a22006-06-26 16:16:12 -0700328
329#if defined(CONFIG_ARCH_OMAP1)
330
Tony Lindgrena569c6e2006-04-02 17:46:21 +0100331/**
332 * omap_dm_timer_modify_idlect_mask - Check if any running timers use ARMXOR
333 * @inputmask: current value of idlect mask
334 */
335__u32 omap_dm_timer_modify_idlect_mask(__u32 inputmask)
336{
Timo Teras77900a22006-06-26 16:16:12 -0700337 int i;
Tony Lindgrena569c6e2006-04-02 17:46:21 +0100338
339 /* If ARMXOR cannot be idled this function call is unnecessary */
340 if (!(inputmask & (1 << 1)))
341 return inputmask;
342
343 /* If any active timer is using ARMXOR return modified mask */
Timo Teras77900a22006-06-26 16:16:12 -0700344 for (i = 0; i < dm_timer_count; i++) {
345 u32 l;
346
Tony Lindgren35912c72006-07-01 19:56:42 +0100347 l = omap_dm_timer_read_reg(&dm_timers[i], OMAP_TIMER_CTRL_REG);
Timo Teras77900a22006-06-26 16:16:12 -0700348 if (l & OMAP_TIMER_CTRL_ST) {
349 if (((omap_readl(MOD_CONF_CTRL_1) >> (i * 2)) & 0x03) == 0)
Tony Lindgrena569c6e2006-04-02 17:46:21 +0100350 inputmask &= ~(1 << 1);
351 else
352 inputmask &= ~(1 << 2);
353 }
Timo Teras77900a22006-06-26 16:16:12 -0700354 }
Tony Lindgrena569c6e2006-04-02 17:46:21 +0100355
356 return inputmask;
357}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700358EXPORT_SYMBOL_GPL(omap_dm_timer_modify_idlect_mask);
Tony Lindgrena569c6e2006-04-02 17:46:21 +0100359
Tony Lindgren140455f2010-02-12 12:26:48 -0800360#else
Timo Teras77900a22006-06-26 16:16:12 -0700361
362struct clk *omap_dm_timer_get_fclk(struct omap_dm_timer *timer)
363{
Timo Terasfa4bb622006-09-25 12:41:35 +0300364 return timer->fclk;
Timo Teras77900a22006-06-26 16:16:12 -0700365}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700366EXPORT_SYMBOL_GPL(omap_dm_timer_get_fclk);
Timo Teras77900a22006-06-26 16:16:12 -0700367
368__u32 omap_dm_timer_modify_idlect_mask(__u32 inputmask)
369{
370 BUG();
Dirk Behme21218802006-12-06 17:14:00 -0800371
372 return 0;
Timo Teras77900a22006-06-26 16:16:12 -0700373}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700374EXPORT_SYMBOL_GPL(omap_dm_timer_modify_idlect_mask);
Timo Teras77900a22006-06-26 16:16:12 -0700375
376#endif
377
378void omap_dm_timer_trigger(struct omap_dm_timer *timer)
379{
380 omap_dm_timer_write_reg(timer, OMAP_TIMER_TRIGGER_REG, 0);
381}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700382EXPORT_SYMBOL_GPL(omap_dm_timer_trigger);
Timo Teras77900a22006-06-26 16:16:12 -0700383
384void omap_dm_timer_start(struct omap_dm_timer *timer)
385{
386 u32 l;
387
388 l = omap_dm_timer_read_reg(timer, OMAP_TIMER_CTRL_REG);
389 if (!(l & OMAP_TIMER_CTRL_ST)) {
390 l |= OMAP_TIMER_CTRL_ST;
391 omap_dm_timer_write_reg(timer, OMAP_TIMER_CTRL_REG, l);
392 }
393}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700394EXPORT_SYMBOL_GPL(omap_dm_timer_start);
Timo Teras77900a22006-06-26 16:16:12 -0700395
396void omap_dm_timer_stop(struct omap_dm_timer *timer)
397{
Tony Lindgrencaf64f22011-03-29 15:54:48 -0700398 unsigned long rate = 0;
Timo Teras77900a22006-06-26 16:16:12 -0700399
Tony Lindgren140455f2010-02-12 12:26:48 -0800400#ifdef CONFIG_ARCH_OMAP2PLUS
Tony Lindgrencaf64f22011-03-29 15:54:48 -0700401 rate = clk_get_rate(timer->fclk);
Tero Kristo5c3db362009-10-23 19:03:47 +0300402#endif
Tony Lindgrencaf64f22011-03-29 15:54:48 -0700403
404 __omap_dm_timer_stop(timer->io_base, timer->posted, rate);
Timo Teras77900a22006-06-26 16:16:12 -0700405}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700406EXPORT_SYMBOL_GPL(omap_dm_timer_stop);
Timo Teras77900a22006-06-26 16:16:12 -0700407
408#ifdef CONFIG_ARCH_OMAP1
Tony Lindgrena569c6e2006-04-02 17:46:21 +0100409
Paul Walmsleyf2480762009-04-23 21:11:10 -0600410int omap_dm_timer_set_source(struct omap_dm_timer *timer, int source)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100411{
412 int n = (timer - dm_timers) << 1;
413 u32 l;
414
415 l = omap_readl(MOD_CONF_CTRL_1) & ~(0x03 << n);
416 l |= source << n;
417 omap_writel(l, MOD_CONF_CTRL_1);
Paul Walmsleyf2480762009-04-23 21:11:10 -0600418
419 return 0;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100420}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700421EXPORT_SYMBOL_GPL(omap_dm_timer_set_source);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100422
Timo Teras77900a22006-06-26 16:16:12 -0700423#else
Tony Lindgren92105bb2005-09-07 17:20:26 +0100424
Paul Walmsleyf2480762009-04-23 21:11:10 -0600425int omap_dm_timer_set_source(struct omap_dm_timer *timer, int source)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100426{
Timo Teras77900a22006-06-26 16:16:12 -0700427 if (source < 0 || source >= 3)
Paul Walmsleyf2480762009-04-23 21:11:10 -0600428 return -EINVAL;
Timo Teras77900a22006-06-26 16:16:12 -0700429
Tony Lindgrencaf64f22011-03-29 15:54:48 -0700430 return __omap_dm_timer_set_source(timer->fclk,
431 dm_source_clocks[source]);
Timo Teras77900a22006-06-26 16:16:12 -0700432}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700433EXPORT_SYMBOL_GPL(omap_dm_timer_set_source);
Timo Teras77900a22006-06-26 16:16:12 -0700434
435#endif
436
437void omap_dm_timer_set_load(struct omap_dm_timer *timer, int autoreload,
438 unsigned int load)
439{
440 u32 l;
441
442 l = omap_dm_timer_read_reg(timer, OMAP_TIMER_CTRL_REG);
443 if (autoreload)
444 l |= OMAP_TIMER_CTRL_AR;
445 else
446 l &= ~OMAP_TIMER_CTRL_AR;
447 omap_dm_timer_write_reg(timer, OMAP_TIMER_CTRL_REG, l);
448 omap_dm_timer_write_reg(timer, OMAP_TIMER_LOAD_REG, load);
Richard Woodruff0f0d0802008-07-03 12:24:30 +0300449
Timo Teras77900a22006-06-26 16:16:12 -0700450 omap_dm_timer_write_reg(timer, OMAP_TIMER_TRIGGER_REG, 0);
451}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700452EXPORT_SYMBOL_GPL(omap_dm_timer_set_load);
Timo Teras77900a22006-06-26 16:16:12 -0700453
Richard Woodruff3fddd092008-07-03 12:24:30 +0300454/* Optimized set_load which removes costly spin wait in timer_start */
455void omap_dm_timer_set_load_start(struct omap_dm_timer *timer, int autoreload,
456 unsigned int load)
457{
458 u32 l;
459
460 l = omap_dm_timer_read_reg(timer, OMAP_TIMER_CTRL_REG);
Paul Walmsley64ce2902008-12-10 17:36:34 -0800461 if (autoreload) {
Richard Woodruff3fddd092008-07-03 12:24:30 +0300462 l |= OMAP_TIMER_CTRL_AR;
Paul Walmsley64ce2902008-12-10 17:36:34 -0800463 omap_dm_timer_write_reg(timer, OMAP_TIMER_LOAD_REG, load);
464 } else {
Richard Woodruff3fddd092008-07-03 12:24:30 +0300465 l &= ~OMAP_TIMER_CTRL_AR;
Paul Walmsley64ce2902008-12-10 17:36:34 -0800466 }
Richard Woodruff3fddd092008-07-03 12:24:30 +0300467 l |= OMAP_TIMER_CTRL_ST;
468
Tony Lindgrencaf64f22011-03-29 15:54:48 -0700469 __omap_dm_timer_load_start(timer->io_base, l, load, timer->posted);
Richard Woodruff3fddd092008-07-03 12:24:30 +0300470}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700471EXPORT_SYMBOL_GPL(omap_dm_timer_set_load_start);
Richard Woodruff3fddd092008-07-03 12:24:30 +0300472
Timo Teras77900a22006-06-26 16:16:12 -0700473void omap_dm_timer_set_match(struct omap_dm_timer *timer, int enable,
474 unsigned int match)
475{
476 u32 l;
477
478 l = omap_dm_timer_read_reg(timer, OMAP_TIMER_CTRL_REG);
Timo Teras83379c82006-06-26 16:16:23 -0700479 if (enable)
Timo Teras77900a22006-06-26 16:16:12 -0700480 l |= OMAP_TIMER_CTRL_CE;
481 else
482 l &= ~OMAP_TIMER_CTRL_CE;
483 omap_dm_timer_write_reg(timer, OMAP_TIMER_CTRL_REG, l);
484 omap_dm_timer_write_reg(timer, OMAP_TIMER_MATCH_REG, match);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100485}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700486EXPORT_SYMBOL_GPL(omap_dm_timer_set_match);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100487
Timo Teras77900a22006-06-26 16:16:12 -0700488void omap_dm_timer_set_pwm(struct omap_dm_timer *timer, int def_on,
489 int toggle, int trigger)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100490{
Timo Teras77900a22006-06-26 16:16:12 -0700491 u32 l;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100492
Timo Teras77900a22006-06-26 16:16:12 -0700493 l = omap_dm_timer_read_reg(timer, OMAP_TIMER_CTRL_REG);
494 l &= ~(OMAP_TIMER_CTRL_GPOCFG | OMAP_TIMER_CTRL_SCPWM |
495 OMAP_TIMER_CTRL_PT | (0x03 << 10));
496 if (def_on)
497 l |= OMAP_TIMER_CTRL_SCPWM;
498 if (toggle)
499 l |= OMAP_TIMER_CTRL_PT;
500 l |= trigger << 10;
501 omap_dm_timer_write_reg(timer, OMAP_TIMER_CTRL_REG, l);
502}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700503EXPORT_SYMBOL_GPL(omap_dm_timer_set_pwm);
Timo Teras77900a22006-06-26 16:16:12 -0700504
505void omap_dm_timer_set_prescaler(struct omap_dm_timer *timer, int prescaler)
506{
507 u32 l;
508
509 l = omap_dm_timer_read_reg(timer, OMAP_TIMER_CTRL_REG);
510 l &= ~(OMAP_TIMER_CTRL_PRE | (0x07 << 2));
511 if (prescaler >= 0x00 && prescaler <= 0x07) {
512 l |= OMAP_TIMER_CTRL_PRE;
513 l |= prescaler << 2;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100514 }
Timo Teras77900a22006-06-26 16:16:12 -0700515 omap_dm_timer_write_reg(timer, OMAP_TIMER_CTRL_REG, l);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100516}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700517EXPORT_SYMBOL_GPL(omap_dm_timer_set_prescaler);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100518
519void omap_dm_timer_set_int_enable(struct omap_dm_timer *timer,
Timo Teras77900a22006-06-26 16:16:12 -0700520 unsigned int value)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100521{
Tony Lindgrencaf64f22011-03-29 15:54:48 -0700522 __omap_dm_timer_int_enable(timer->io_base, value);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100523}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700524EXPORT_SYMBOL_GPL(omap_dm_timer_set_int_enable);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100525
526unsigned int omap_dm_timer_read_status(struct omap_dm_timer *timer)
527{
Timo Terasfa4bb622006-09-25 12:41:35 +0300528 unsigned int l;
529
Timo Terasfa4bb622006-09-25 12:41:35 +0300530 l = omap_dm_timer_read_reg(timer, OMAP_TIMER_STAT_REG);
Timo Terasfa4bb622006-09-25 12:41:35 +0300531
532 return l;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100533}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700534EXPORT_SYMBOL_GPL(omap_dm_timer_read_status);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100535
536void omap_dm_timer_write_status(struct omap_dm_timer *timer, unsigned int value)
537{
Tony Lindgrencaf64f22011-03-29 15:54:48 -0700538 __omap_dm_timer_write_status(timer->io_base, value);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100539}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700540EXPORT_SYMBOL_GPL(omap_dm_timer_write_status);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100541
Tony Lindgren92105bb2005-09-07 17:20:26 +0100542unsigned int omap_dm_timer_read_counter(struct omap_dm_timer *timer)
543{
Tony Lindgrencaf64f22011-03-29 15:54:48 -0700544 return __omap_dm_timer_read_counter(timer->io_base, timer->posted);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100545}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700546EXPORT_SYMBOL_GPL(omap_dm_timer_read_counter);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100547
Timo Teras83379c82006-06-26 16:16:23 -0700548void omap_dm_timer_write_counter(struct omap_dm_timer *timer, unsigned int value)
549{
Timo Terasfa4bb622006-09-25 12:41:35 +0300550 omap_dm_timer_write_reg(timer, OMAP_TIMER_COUNTER_REG, value);
Timo Teras83379c82006-06-26 16:16:23 -0700551}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700552EXPORT_SYMBOL_GPL(omap_dm_timer_write_counter);
Timo Teras83379c82006-06-26 16:16:23 -0700553
Timo Teras77900a22006-06-26 16:16:12 -0700554int omap_dm_timers_active(void)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100555{
Timo Teras77900a22006-06-26 16:16:12 -0700556 int i;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100557
Timo Teras77900a22006-06-26 16:16:12 -0700558 for (i = 0; i < dm_timer_count; i++) {
559 struct omap_dm_timer *timer;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100560
Timo Teras77900a22006-06-26 16:16:12 -0700561 timer = &dm_timers[i];
Timo Teras12583a72006-09-25 12:41:42 +0300562
563 if (!timer->enabled)
564 continue;
565
Timo Teras77900a22006-06-26 16:16:12 -0700566 if (omap_dm_timer_read_reg(timer, OMAP_TIMER_CTRL_REG) &
Timo Terasfa4bb622006-09-25 12:41:35 +0300567 OMAP_TIMER_CTRL_ST) {
Timo Teras77900a22006-06-26 16:16:12 -0700568 return 1;
Timo Terasfa4bb622006-09-25 12:41:35 +0300569 }
Tony Lindgren92105bb2005-09-07 17:20:26 +0100570 }
Tony Lindgren92105bb2005-09-07 17:20:26 +0100571 return 0;
572}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700573EXPORT_SYMBOL_GPL(omap_dm_timers_active);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100574
Tony Lindgren11a01862011-03-29 15:54:49 -0700575static int __init omap_dm_timer_init(void)
Timo Teras77900a22006-06-26 16:16:12 -0700576{
577 struct omap_dm_timer *timer;
Tony Lindgren3566fc62009-10-19 15:25:18 -0700578 int i, map_size = SZ_8K; /* Module 4KB + L4 4KB except on omap1 */
Timo Teras77900a22006-06-26 16:16:12 -0700579
Syed Mohammed, Khasimce2df9c2007-06-25 22:55:39 -0700580 if (!(cpu_is_omap16xx() || cpu_class_is_omap2()))
Timo Teras77900a22006-06-26 16:16:12 -0700581 return -ENODEV;
582
583 spin_lock_init(&dm_timer_lock);
Syed Mohammed, Khasim471b3aa2007-06-21 21:48:07 -0700584
Tony Lindgren3566fc62009-10-19 15:25:18 -0700585 if (cpu_class_is_omap1()) {
Syed Mohammed, Khasim471b3aa2007-06-21 21:48:07 -0700586 dm_timers = omap1_dm_timers;
Tony Lindgren882c0512010-02-12 12:26:46 -0800587 dm_timer_count = omap1_dm_timer_count;
Tony Lindgren3566fc62009-10-19 15:25:18 -0700588 map_size = SZ_2K;
589 } else if (cpu_is_omap24xx()) {
Syed Mohammed, Khasim471b3aa2007-06-21 21:48:07 -0700590 dm_timers = omap2_dm_timers;
Tony Lindgren882c0512010-02-12 12:26:46 -0800591 dm_timer_count = omap2_dm_timer_count;
Santosh Shilimkaraea2a5b2009-05-25 11:08:36 -0700592 dm_source_names = omap2_dm_source_names;
593 dm_source_clocks = omap2_dm_source_clocks;
Syed Mohammed, Khasimce2df9c2007-06-25 22:55:39 -0700594 } else if (cpu_is_omap34xx()) {
595 dm_timers = omap3_dm_timers;
Tony Lindgren882c0512010-02-12 12:26:46 -0800596 dm_timer_count = omap3_dm_timer_count;
Santosh Shilimkaraea2a5b2009-05-25 11:08:36 -0700597 dm_source_names = omap3_dm_source_names;
598 dm_source_clocks = omap3_dm_source_clocks;
Santosh Shilimkar44169072009-05-28 14:16:04 -0700599 } else if (cpu_is_omap44xx()) {
600 dm_timers = omap4_dm_timers;
Tony Lindgren882c0512010-02-12 12:26:46 -0800601 dm_timer_count = omap4_dm_timer_count;
Santosh Shilimkar44169072009-05-28 14:16:04 -0700602 dm_source_names = omap4_dm_source_names;
603 dm_source_clocks = omap4_dm_source_clocks;
Timo Teras83379c82006-06-26 16:16:23 -0700604 }
Syed Mohammed, Khasim471b3aa2007-06-21 21:48:07 -0700605
606 if (cpu_class_is_omap2())
607 for (i = 0; dm_source_names[i] != NULL; i++)
608 dm_source_clocks[i] = clk_get(NULL, dm_source_names[i]);
609
Syed Mohammed Khasim56a25642006-12-06 17:14:08 -0800610 if (cpu_is_omap243x())
611 dm_timers[0].phys_base = 0x49018000;
Timo Teras83379c82006-06-26 16:16:23 -0700612
Timo Teras77900a22006-06-26 16:16:12 -0700613 for (i = 0; i < dm_timer_count; i++) {
Timo Teras77900a22006-06-26 16:16:12 -0700614 timer = &dm_timers[i];
Tony Lindgren3566fc62009-10-19 15:25:18 -0700615
616 /* Static mapping, never released */
617 timer->io_base = ioremap(timer->phys_base, map_size);
618 BUG_ON(!timer->io_base);
619
Tony Lindgren140455f2010-02-12 12:26:48 -0800620#ifdef CONFIG_ARCH_OMAP2PLUS
Syed Mohammed, Khasim471b3aa2007-06-21 21:48:07 -0700621 if (cpu_class_is_omap2()) {
622 char clk_name[16];
623 sprintf(clk_name, "gpt%d_ick", i + 1);
624 timer->iclk = clk_get(NULL, clk_name);
625 sprintf(clk_name, "gpt%d_fck", i + 1);
626 timer->fclk = clk_get(NULL, clk_name);
627 }
Tony Lindgren11a01862011-03-29 15:54:49 -0700628
629 /* One or two timers may be set up early for sys_timer */
630 if (sys_timer_reserved & (1 << i)) {
631 timer->reserved = 1;
632 timer->posted = 1;
633 }
Timo Teras77900a22006-06-26 16:16:12 -0700634#endif
635 }
636
637 return 0;
638}
Tony Lindgren11a01862011-03-29 15:54:49 -0700639
640arch_initcall(omap_dm_timer_init);