blob: e23b7cf2b219f2ecaae7f9064c29a649de511d04 [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 Lindgrenee17f112011-09-16 15:44:20 -0700173 WARN_ON((reg & 0xff) < _OMAP_TIMER_WAKEUP_EN_OFFSET);
174 return __omap_dm_timer_read(timer, reg, timer->posted);
Timo Teras77900a22006-06-26 16:16:12 -0700175}
176
Richard Woodruff0f0d0802008-07-03 12:24:30 +0300177/*
178 * Writes timer registers in posted and non-posted mode. The posted mode bit
179 * is encoded in reg. Note that in posted mode the write pending bit must be
180 * checked. Otherwise a write on a register which has a pending write will be
181 * lost.
182 */
183static void omap_dm_timer_write_reg(struct omap_dm_timer *timer, u32 reg,
184 u32 value)
Timo Teras77900a22006-06-26 16:16:12 -0700185{
Tony Lindgrenee17f112011-09-16 15:44:20 -0700186 WARN_ON((reg & 0xff) < _OMAP_TIMER_WAKEUP_EN_OFFSET);
187 __omap_dm_timer_write(timer, reg, value, timer->posted);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100188}
189
Timo Teras77900a22006-06-26 16:16:12 -0700190static void omap_dm_timer_wait_for_reset(struct omap_dm_timer *timer)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100191{
Timo Teras77900a22006-06-26 16:16:12 -0700192 int c;
193
Tony Lindgrenee17f112011-09-16 15:44:20 -0700194 if (!timer->sys_stat)
195 return;
196
Timo Teras77900a22006-06-26 16:16:12 -0700197 c = 0;
Tony Lindgrenee17f112011-09-16 15:44:20 -0700198 while (!(__raw_readl(timer->sys_stat) & 1)) {
Timo Teras77900a22006-06-26 16:16:12 -0700199 c++;
200 if (c > 100000) {
201 printk(KERN_ERR "Timer failed to reset\n");
202 return;
203 }
204 }
Tony Lindgren92105bb2005-09-07 17:20:26 +0100205}
206
Timo Teras77900a22006-06-26 16:16:12 -0700207static void omap_dm_timer_reset(struct omap_dm_timer *timer)
208{
Tony Lindgrencaf64f22011-03-29 15:54:48 -0700209 int autoidle = 0, wakeup = 0;
Timo Teras77900a22006-06-26 16:16:12 -0700210
Juha Yrjola39020842006-09-25 12:41:44 +0300211 if (!cpu_class_is_omap2() || timer != &dm_timers[0]) {
Timo Terase32f7ec2006-06-26 16:16:13 -0700212 omap_dm_timer_write_reg(timer, OMAP_TIMER_IF_CTRL_REG, 0x06);
213 omap_dm_timer_wait_for_reset(timer);
214 }
Timo Teras12583a72006-09-25 12:41:42 +0300215 omap_dm_timer_set_source(timer, OMAP_TIMER_SRC_32_KHZ);
Timo Teras77900a22006-06-26 16:16:12 -0700216
Ambresh Kba503482011-06-15 21:12:35 +0000217 /* Enable autoidle on OMAP2+ */
218 if (cpu_class_is_omap2())
Tony Lindgrencaf64f22011-03-29 15:54:48 -0700219 autoidle = 1;
Tero Kristo4ce1e5e2011-03-10 03:50:54 -0700220
Richard Woodruff0f0d0802008-07-03 12:24:30 +0300221 /*
Kevin Hilman219c5b92009-04-23 21:11:08 -0600222 * Enable wake-up on OMAP2 CPUs.
Richard Woodruff0f0d0802008-07-03 12:24:30 +0300223 */
Kevin Hilman219c5b92009-04-23 21:11:08 -0600224 if (cpu_class_is_omap2())
Tony Lindgrencaf64f22011-03-29 15:54:48 -0700225 wakeup = 1;
Richard Woodruff0f0d0802008-07-03 12:24:30 +0300226
Tony Lindgrenee17f112011-09-16 15:44:20 -0700227 __omap_dm_timer_reset(timer, autoidle, wakeup);
Richard Woodruff0f0d0802008-07-03 12:24:30 +0300228 timer->posted = 1;
Timo Teras77900a22006-06-26 16:16:12 -0700229}
230
Tony Lindgrencaf64f22011-03-29 15:54:48 -0700231void omap_dm_timer_prepare(struct omap_dm_timer *timer)
Timo Teras77900a22006-06-26 16:16:12 -0700232{
Timo Teras12583a72006-09-25 12:41:42 +0300233 omap_dm_timer_enable(timer);
Timo Teras77900a22006-06-26 16:16:12 -0700234 omap_dm_timer_reset(timer);
235}
236
237struct omap_dm_timer *omap_dm_timer_request(void)
238{
239 struct omap_dm_timer *timer = NULL;
240 unsigned long flags;
241 int i;
242
243 spin_lock_irqsave(&dm_timer_lock, flags);
244 for (i = 0; i < dm_timer_count; i++) {
245 if (dm_timers[i].reserved)
246 continue;
247
248 timer = &dm_timers[i];
Timo Teras83379c82006-06-26 16:16:23 -0700249 timer->reserved = 1;
Timo Teras77900a22006-06-26 16:16:12 -0700250 break;
251 }
252 spin_unlock_irqrestore(&dm_timer_lock, flags);
253
Timo Teras83379c82006-06-26 16:16:23 -0700254 if (timer != NULL)
255 omap_dm_timer_prepare(timer);
256
Timo Teras77900a22006-06-26 16:16:12 -0700257 return timer;
258}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700259EXPORT_SYMBOL_GPL(omap_dm_timer_request);
Timo Teras77900a22006-06-26 16:16:12 -0700260
261struct omap_dm_timer *omap_dm_timer_request_specific(int id)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100262{
263 struct omap_dm_timer *timer;
Timo Teras77900a22006-06-26 16:16:12 -0700264 unsigned long flags;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100265
Timo Teras77900a22006-06-26 16:16:12 -0700266 spin_lock_irqsave(&dm_timer_lock, flags);
267 if (id <= 0 || id > dm_timer_count || dm_timers[id-1].reserved) {
268 spin_unlock_irqrestore(&dm_timer_lock, flags);
269 printk("BUG: warning at %s:%d/%s(): unable to get timer %d\n",
Harvey Harrison8e86f422008-03-04 15:08:02 -0800270 __FILE__, __LINE__, __func__, id);
Timo Teras77900a22006-06-26 16:16:12 -0700271 dump_stack();
272 return NULL;
273 }
Tony Lindgren92105bb2005-09-07 17:20:26 +0100274
Timo Teras77900a22006-06-26 16:16:12 -0700275 timer = &dm_timers[id-1];
Timo Teras83379c82006-06-26 16:16:23 -0700276 timer->reserved = 1;
Timo Teras77900a22006-06-26 16:16:12 -0700277 spin_unlock_irqrestore(&dm_timer_lock, flags);
278
Timo Teras83379c82006-06-26 16:16:23 -0700279 omap_dm_timer_prepare(timer);
280
Timo Teras77900a22006-06-26 16:16:12 -0700281 return timer;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100282}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700283EXPORT_SYMBOL_GPL(omap_dm_timer_request_specific);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100284
Timo Teras77900a22006-06-26 16:16:12 -0700285void omap_dm_timer_free(struct omap_dm_timer *timer)
286{
Timo Teras12583a72006-09-25 12:41:42 +0300287 omap_dm_timer_enable(timer);
Timo Teras77900a22006-06-26 16:16:12 -0700288 omap_dm_timer_reset(timer);
Timo Teras12583a72006-09-25 12:41:42 +0300289 omap_dm_timer_disable(timer);
Timo Terasfa4bb622006-09-25 12:41:35 +0300290
Timo Teras77900a22006-06-26 16:16:12 -0700291 WARN_ON(!timer->reserved);
292 timer->reserved = 0;
293}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700294EXPORT_SYMBOL_GPL(omap_dm_timer_free);
Timo Teras77900a22006-06-26 16:16:12 -0700295
Timo Teras12583a72006-09-25 12:41:42 +0300296void omap_dm_timer_enable(struct omap_dm_timer *timer)
297{
298 if (timer->enabled)
299 return;
300
Tony Lindgren882c0512010-02-12 12:26:46 -0800301#ifdef CONFIG_ARCH_OMAP2PLUS
302 if (cpu_class_is_omap2()) {
303 clk_enable(timer->fclk);
304 clk_enable(timer->iclk);
305 }
306#endif
Timo Teras12583a72006-09-25 12:41:42 +0300307
308 timer->enabled = 1;
309}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700310EXPORT_SYMBOL_GPL(omap_dm_timer_enable);
Timo Teras12583a72006-09-25 12:41:42 +0300311
312void omap_dm_timer_disable(struct omap_dm_timer *timer)
313{
314 if (!timer->enabled)
315 return;
316
Tony Lindgren882c0512010-02-12 12:26:46 -0800317#ifdef CONFIG_ARCH_OMAP2PLUS
318 if (cpu_class_is_omap2()) {
319 clk_disable(timer->iclk);
320 clk_disable(timer->fclk);
321 }
322#endif
Timo Teras12583a72006-09-25 12:41:42 +0300323
324 timer->enabled = 0;
325}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700326EXPORT_SYMBOL_GPL(omap_dm_timer_disable);
Timo Teras12583a72006-09-25 12:41:42 +0300327
Timo Teras77900a22006-06-26 16:16:12 -0700328int omap_dm_timer_get_irq(struct omap_dm_timer *timer)
329{
330 return timer->irq;
331}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700332EXPORT_SYMBOL_GPL(omap_dm_timer_get_irq);
Timo Teras77900a22006-06-26 16:16:12 -0700333
334#if defined(CONFIG_ARCH_OMAP1)
335
Tony Lindgrena569c6e2006-04-02 17:46:21 +0100336/**
337 * omap_dm_timer_modify_idlect_mask - Check if any running timers use ARMXOR
338 * @inputmask: current value of idlect mask
339 */
340__u32 omap_dm_timer_modify_idlect_mask(__u32 inputmask)
341{
Timo Teras77900a22006-06-26 16:16:12 -0700342 int i;
Tony Lindgrena569c6e2006-04-02 17:46:21 +0100343
344 /* If ARMXOR cannot be idled this function call is unnecessary */
345 if (!(inputmask & (1 << 1)))
346 return inputmask;
347
348 /* If any active timer is using ARMXOR return modified mask */
Timo Teras77900a22006-06-26 16:16:12 -0700349 for (i = 0; i < dm_timer_count; i++) {
350 u32 l;
351
Tony Lindgren35912c72006-07-01 19:56:42 +0100352 l = omap_dm_timer_read_reg(&dm_timers[i], OMAP_TIMER_CTRL_REG);
Timo Teras77900a22006-06-26 16:16:12 -0700353 if (l & OMAP_TIMER_CTRL_ST) {
354 if (((omap_readl(MOD_CONF_CTRL_1) >> (i * 2)) & 0x03) == 0)
Tony Lindgrena569c6e2006-04-02 17:46:21 +0100355 inputmask &= ~(1 << 1);
356 else
357 inputmask &= ~(1 << 2);
358 }
Timo Teras77900a22006-06-26 16:16:12 -0700359 }
Tony Lindgrena569c6e2006-04-02 17:46:21 +0100360
361 return inputmask;
362}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700363EXPORT_SYMBOL_GPL(omap_dm_timer_modify_idlect_mask);
Tony Lindgrena569c6e2006-04-02 17:46:21 +0100364
Tony Lindgren140455f2010-02-12 12:26:48 -0800365#else
Timo Teras77900a22006-06-26 16:16:12 -0700366
367struct clk *omap_dm_timer_get_fclk(struct omap_dm_timer *timer)
368{
Timo Terasfa4bb622006-09-25 12:41:35 +0300369 return timer->fclk;
Timo Teras77900a22006-06-26 16:16:12 -0700370}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700371EXPORT_SYMBOL_GPL(omap_dm_timer_get_fclk);
Timo Teras77900a22006-06-26 16:16:12 -0700372
373__u32 omap_dm_timer_modify_idlect_mask(__u32 inputmask)
374{
375 BUG();
Dirk Behme21218802006-12-06 17:14:00 -0800376
377 return 0;
Timo Teras77900a22006-06-26 16:16:12 -0700378}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700379EXPORT_SYMBOL_GPL(omap_dm_timer_modify_idlect_mask);
Timo Teras77900a22006-06-26 16:16:12 -0700380
381#endif
382
383void omap_dm_timer_trigger(struct omap_dm_timer *timer)
384{
385 omap_dm_timer_write_reg(timer, OMAP_TIMER_TRIGGER_REG, 0);
386}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700387EXPORT_SYMBOL_GPL(omap_dm_timer_trigger);
Timo Teras77900a22006-06-26 16:16:12 -0700388
389void omap_dm_timer_start(struct omap_dm_timer *timer)
390{
391 u32 l;
392
393 l = omap_dm_timer_read_reg(timer, OMAP_TIMER_CTRL_REG);
394 if (!(l & OMAP_TIMER_CTRL_ST)) {
395 l |= OMAP_TIMER_CTRL_ST;
396 omap_dm_timer_write_reg(timer, OMAP_TIMER_CTRL_REG, l);
397 }
398}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700399EXPORT_SYMBOL_GPL(omap_dm_timer_start);
Timo Teras77900a22006-06-26 16:16:12 -0700400
401void omap_dm_timer_stop(struct omap_dm_timer *timer)
402{
Tony Lindgrencaf64f22011-03-29 15:54:48 -0700403 unsigned long rate = 0;
Timo Teras77900a22006-06-26 16:16:12 -0700404
Tony Lindgren140455f2010-02-12 12:26:48 -0800405#ifdef CONFIG_ARCH_OMAP2PLUS
Tony Lindgrencaf64f22011-03-29 15:54:48 -0700406 rate = clk_get_rate(timer->fclk);
Tero Kristo5c3db362009-10-23 19:03:47 +0300407#endif
Tony Lindgrencaf64f22011-03-29 15:54:48 -0700408
Tony Lindgrenee17f112011-09-16 15:44:20 -0700409 __omap_dm_timer_stop(timer, timer->posted, rate);
Timo Teras77900a22006-06-26 16:16:12 -0700410}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700411EXPORT_SYMBOL_GPL(omap_dm_timer_stop);
Timo Teras77900a22006-06-26 16:16:12 -0700412
413#ifdef CONFIG_ARCH_OMAP1
Tony Lindgrena569c6e2006-04-02 17:46:21 +0100414
Paul Walmsleyf2480762009-04-23 21:11:10 -0600415int omap_dm_timer_set_source(struct omap_dm_timer *timer, int source)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100416{
417 int n = (timer - dm_timers) << 1;
418 u32 l;
419
420 l = omap_readl(MOD_CONF_CTRL_1) & ~(0x03 << n);
421 l |= source << n;
422 omap_writel(l, MOD_CONF_CTRL_1);
Paul Walmsleyf2480762009-04-23 21:11:10 -0600423
424 return 0;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100425}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700426EXPORT_SYMBOL_GPL(omap_dm_timer_set_source);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100427
Timo Teras77900a22006-06-26 16:16:12 -0700428#else
Tony Lindgren92105bb2005-09-07 17:20:26 +0100429
Paul Walmsleyf2480762009-04-23 21:11:10 -0600430int omap_dm_timer_set_source(struct omap_dm_timer *timer, int source)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100431{
Timo Teras77900a22006-06-26 16:16:12 -0700432 if (source < 0 || source >= 3)
Paul Walmsleyf2480762009-04-23 21:11:10 -0600433 return -EINVAL;
Timo Teras77900a22006-06-26 16:16:12 -0700434
Tony Lindgrencaf64f22011-03-29 15:54:48 -0700435 return __omap_dm_timer_set_source(timer->fclk,
436 dm_source_clocks[source]);
Timo Teras77900a22006-06-26 16:16:12 -0700437}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700438EXPORT_SYMBOL_GPL(omap_dm_timer_set_source);
Timo Teras77900a22006-06-26 16:16:12 -0700439
440#endif
441
442void omap_dm_timer_set_load(struct omap_dm_timer *timer, int autoreload,
443 unsigned int load)
444{
445 u32 l;
446
447 l = omap_dm_timer_read_reg(timer, OMAP_TIMER_CTRL_REG);
448 if (autoreload)
449 l |= OMAP_TIMER_CTRL_AR;
450 else
451 l &= ~OMAP_TIMER_CTRL_AR;
452 omap_dm_timer_write_reg(timer, OMAP_TIMER_CTRL_REG, l);
453 omap_dm_timer_write_reg(timer, OMAP_TIMER_LOAD_REG, load);
Richard Woodruff0f0d0802008-07-03 12:24:30 +0300454
Timo Teras77900a22006-06-26 16:16:12 -0700455 omap_dm_timer_write_reg(timer, OMAP_TIMER_TRIGGER_REG, 0);
456}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700457EXPORT_SYMBOL_GPL(omap_dm_timer_set_load);
Timo Teras77900a22006-06-26 16:16:12 -0700458
Richard Woodruff3fddd092008-07-03 12:24:30 +0300459/* Optimized set_load which removes costly spin wait in timer_start */
460void omap_dm_timer_set_load_start(struct omap_dm_timer *timer, int autoreload,
461 unsigned int load)
462{
463 u32 l;
464
465 l = omap_dm_timer_read_reg(timer, OMAP_TIMER_CTRL_REG);
Paul Walmsley64ce2902008-12-10 17:36:34 -0800466 if (autoreload) {
Richard Woodruff3fddd092008-07-03 12:24:30 +0300467 l |= OMAP_TIMER_CTRL_AR;
Paul Walmsley64ce2902008-12-10 17:36:34 -0800468 omap_dm_timer_write_reg(timer, OMAP_TIMER_LOAD_REG, load);
469 } else {
Richard Woodruff3fddd092008-07-03 12:24:30 +0300470 l &= ~OMAP_TIMER_CTRL_AR;
Paul Walmsley64ce2902008-12-10 17:36:34 -0800471 }
Richard Woodruff3fddd092008-07-03 12:24:30 +0300472 l |= OMAP_TIMER_CTRL_ST;
473
Tony Lindgrenee17f112011-09-16 15:44:20 -0700474 __omap_dm_timer_load_start(timer, l, load, timer->posted);
Richard Woodruff3fddd092008-07-03 12:24:30 +0300475}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700476EXPORT_SYMBOL_GPL(omap_dm_timer_set_load_start);
Richard Woodruff3fddd092008-07-03 12:24:30 +0300477
Timo Teras77900a22006-06-26 16:16:12 -0700478void omap_dm_timer_set_match(struct omap_dm_timer *timer, int enable,
479 unsigned int match)
480{
481 u32 l;
482
483 l = omap_dm_timer_read_reg(timer, OMAP_TIMER_CTRL_REG);
Timo Teras83379c82006-06-26 16:16:23 -0700484 if (enable)
Timo Teras77900a22006-06-26 16:16:12 -0700485 l |= OMAP_TIMER_CTRL_CE;
486 else
487 l &= ~OMAP_TIMER_CTRL_CE;
488 omap_dm_timer_write_reg(timer, OMAP_TIMER_CTRL_REG, l);
489 omap_dm_timer_write_reg(timer, OMAP_TIMER_MATCH_REG, match);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100490}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700491EXPORT_SYMBOL_GPL(omap_dm_timer_set_match);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100492
Timo Teras77900a22006-06-26 16:16:12 -0700493void omap_dm_timer_set_pwm(struct omap_dm_timer *timer, int def_on,
494 int toggle, int trigger)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100495{
Timo Teras77900a22006-06-26 16:16:12 -0700496 u32 l;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100497
Timo Teras77900a22006-06-26 16:16:12 -0700498 l = omap_dm_timer_read_reg(timer, OMAP_TIMER_CTRL_REG);
499 l &= ~(OMAP_TIMER_CTRL_GPOCFG | OMAP_TIMER_CTRL_SCPWM |
500 OMAP_TIMER_CTRL_PT | (0x03 << 10));
501 if (def_on)
502 l |= OMAP_TIMER_CTRL_SCPWM;
503 if (toggle)
504 l |= OMAP_TIMER_CTRL_PT;
505 l |= trigger << 10;
506 omap_dm_timer_write_reg(timer, OMAP_TIMER_CTRL_REG, l);
507}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700508EXPORT_SYMBOL_GPL(omap_dm_timer_set_pwm);
Timo Teras77900a22006-06-26 16:16:12 -0700509
510void omap_dm_timer_set_prescaler(struct omap_dm_timer *timer, int prescaler)
511{
512 u32 l;
513
514 l = omap_dm_timer_read_reg(timer, OMAP_TIMER_CTRL_REG);
515 l &= ~(OMAP_TIMER_CTRL_PRE | (0x07 << 2));
516 if (prescaler >= 0x00 && prescaler <= 0x07) {
517 l |= OMAP_TIMER_CTRL_PRE;
518 l |= prescaler << 2;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100519 }
Timo Teras77900a22006-06-26 16:16:12 -0700520 omap_dm_timer_write_reg(timer, OMAP_TIMER_CTRL_REG, l);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100521}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700522EXPORT_SYMBOL_GPL(omap_dm_timer_set_prescaler);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100523
524void omap_dm_timer_set_int_enable(struct omap_dm_timer *timer,
Timo Teras77900a22006-06-26 16:16:12 -0700525 unsigned int value)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100526{
Tony Lindgrenee17f112011-09-16 15:44:20 -0700527 __omap_dm_timer_int_enable(timer, value);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100528}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700529EXPORT_SYMBOL_GPL(omap_dm_timer_set_int_enable);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100530
531unsigned int omap_dm_timer_read_status(struct omap_dm_timer *timer)
532{
Timo Terasfa4bb622006-09-25 12:41:35 +0300533 unsigned int l;
534
Tony Lindgrenee17f112011-09-16 15:44:20 -0700535 l = __raw_readl(timer->irq_stat);
Timo Terasfa4bb622006-09-25 12:41:35 +0300536
537 return l;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100538}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700539EXPORT_SYMBOL_GPL(omap_dm_timer_read_status);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100540
541void omap_dm_timer_write_status(struct omap_dm_timer *timer, unsigned int value)
542{
Tony Lindgrenee17f112011-09-16 15:44:20 -0700543 __omap_dm_timer_write_status(timer, value);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100544}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700545EXPORT_SYMBOL_GPL(omap_dm_timer_write_status);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100546
Tony Lindgren92105bb2005-09-07 17:20:26 +0100547unsigned int omap_dm_timer_read_counter(struct omap_dm_timer *timer)
548{
Tony Lindgrenee17f112011-09-16 15:44:20 -0700549 return __omap_dm_timer_read_counter(timer, timer->posted);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100550}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700551EXPORT_SYMBOL_GPL(omap_dm_timer_read_counter);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100552
Timo Teras83379c82006-06-26 16:16:23 -0700553void omap_dm_timer_write_counter(struct omap_dm_timer *timer, unsigned int value)
554{
Timo Terasfa4bb622006-09-25 12:41:35 +0300555 omap_dm_timer_write_reg(timer, OMAP_TIMER_COUNTER_REG, value);
Timo Teras83379c82006-06-26 16:16:23 -0700556}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700557EXPORT_SYMBOL_GPL(omap_dm_timer_write_counter);
Timo Teras83379c82006-06-26 16:16:23 -0700558
Timo Teras77900a22006-06-26 16:16:12 -0700559int omap_dm_timers_active(void)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100560{
Timo Teras77900a22006-06-26 16:16:12 -0700561 int i;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100562
Timo Teras77900a22006-06-26 16:16:12 -0700563 for (i = 0; i < dm_timer_count; i++) {
564 struct omap_dm_timer *timer;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100565
Timo Teras77900a22006-06-26 16:16:12 -0700566 timer = &dm_timers[i];
Timo Teras12583a72006-09-25 12:41:42 +0300567
568 if (!timer->enabled)
569 continue;
570
Timo Teras77900a22006-06-26 16:16:12 -0700571 if (omap_dm_timer_read_reg(timer, OMAP_TIMER_CTRL_REG) &
Timo Terasfa4bb622006-09-25 12:41:35 +0300572 OMAP_TIMER_CTRL_ST) {
Timo Teras77900a22006-06-26 16:16:12 -0700573 return 1;
Timo Terasfa4bb622006-09-25 12:41:35 +0300574 }
Tony Lindgren92105bb2005-09-07 17:20:26 +0100575 }
Tony Lindgren92105bb2005-09-07 17:20:26 +0100576 return 0;
577}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700578EXPORT_SYMBOL_GPL(omap_dm_timers_active);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100579
Tony Lindgren11a01862011-03-29 15:54:49 -0700580static int __init omap_dm_timer_init(void)
Timo Teras77900a22006-06-26 16:16:12 -0700581{
582 struct omap_dm_timer *timer;
Tony Lindgren3566fc62009-10-19 15:25:18 -0700583 int i, map_size = SZ_8K; /* Module 4KB + L4 4KB except on omap1 */
Timo Teras77900a22006-06-26 16:16:12 -0700584
Syed Mohammed, Khasimce2df9c2007-06-25 22:55:39 -0700585 if (!(cpu_is_omap16xx() || cpu_class_is_omap2()))
Timo Teras77900a22006-06-26 16:16:12 -0700586 return -ENODEV;
587
588 spin_lock_init(&dm_timer_lock);
Syed Mohammed, Khasim471b3aa2007-06-21 21:48:07 -0700589
Tony Lindgren3566fc62009-10-19 15:25:18 -0700590 if (cpu_class_is_omap1()) {
Syed Mohammed, Khasim471b3aa2007-06-21 21:48:07 -0700591 dm_timers = omap1_dm_timers;
Tony Lindgren882c0512010-02-12 12:26:46 -0800592 dm_timer_count = omap1_dm_timer_count;
Tony Lindgren3566fc62009-10-19 15:25:18 -0700593 map_size = SZ_2K;
594 } else if (cpu_is_omap24xx()) {
Syed Mohammed, Khasim471b3aa2007-06-21 21:48:07 -0700595 dm_timers = omap2_dm_timers;
Tony Lindgren882c0512010-02-12 12:26:46 -0800596 dm_timer_count = omap2_dm_timer_count;
Santosh Shilimkaraea2a5b2009-05-25 11:08:36 -0700597 dm_source_names = omap2_dm_source_names;
598 dm_source_clocks = omap2_dm_source_clocks;
Syed Mohammed, Khasimce2df9c2007-06-25 22:55:39 -0700599 } else if (cpu_is_omap34xx()) {
600 dm_timers = omap3_dm_timers;
Tony Lindgren882c0512010-02-12 12:26:46 -0800601 dm_timer_count = omap3_dm_timer_count;
Santosh Shilimkaraea2a5b2009-05-25 11:08:36 -0700602 dm_source_names = omap3_dm_source_names;
603 dm_source_clocks = omap3_dm_source_clocks;
Santosh Shilimkar44169072009-05-28 14:16:04 -0700604 } else if (cpu_is_omap44xx()) {
605 dm_timers = omap4_dm_timers;
Tony Lindgren882c0512010-02-12 12:26:46 -0800606 dm_timer_count = omap4_dm_timer_count;
Santosh Shilimkar44169072009-05-28 14:16:04 -0700607 dm_source_names = omap4_dm_source_names;
608 dm_source_clocks = omap4_dm_source_clocks;
Tony Lindgrenee17f112011-09-16 15:44:20 -0700609
610 pr_err("dmtimers disabled for omap4 until hwmod conversion\n");
611 return -ENODEV;
Timo Teras83379c82006-06-26 16:16:23 -0700612 }
Syed Mohammed, Khasim471b3aa2007-06-21 21:48:07 -0700613
614 if (cpu_class_is_omap2())
615 for (i = 0; dm_source_names[i] != NULL; i++)
616 dm_source_clocks[i] = clk_get(NULL, dm_source_names[i]);
617
Syed Mohammed Khasim56a25642006-12-06 17:14:08 -0800618 if (cpu_is_omap243x())
619 dm_timers[0].phys_base = 0x49018000;
Timo Teras83379c82006-06-26 16:16:23 -0700620
Timo Teras77900a22006-06-26 16:16:12 -0700621 for (i = 0; i < dm_timer_count; i++) {
Timo Teras77900a22006-06-26 16:16:12 -0700622 timer = &dm_timers[i];
Tony Lindgren3566fc62009-10-19 15:25:18 -0700623
624 /* Static mapping, never released */
625 timer->io_base = ioremap(timer->phys_base, map_size);
626 BUG_ON(!timer->io_base);
627
Tony Lindgren140455f2010-02-12 12:26:48 -0800628#ifdef CONFIG_ARCH_OMAP2PLUS
Syed Mohammed, Khasim471b3aa2007-06-21 21:48:07 -0700629 if (cpu_class_is_omap2()) {
630 char clk_name[16];
631 sprintf(clk_name, "gpt%d_ick", i + 1);
632 timer->iclk = clk_get(NULL, clk_name);
633 sprintf(clk_name, "gpt%d_fck", i + 1);
634 timer->fclk = clk_get(NULL, clk_name);
635 }
Tony Lindgren11a01862011-03-29 15:54:49 -0700636
637 /* One or two timers may be set up early for sys_timer */
638 if (sys_timer_reserved & (1 << i)) {
639 timer->reserved = 1;
640 timer->posted = 1;
Tony Lindgrenee17f112011-09-16 15:44:20 -0700641 continue;
Tony Lindgren11a01862011-03-29 15:54:49 -0700642 }
Timo Teras77900a22006-06-26 16:16:12 -0700643#endif
Tony Lindgrenee17f112011-09-16 15:44:20 -0700644 omap_dm_timer_enable(timer);
645 __omap_dm_timer_init_regs(timer);
646 omap_dm_timer_disable(timer);
Timo Teras77900a22006-06-26 16:16:12 -0700647 }
648
649 return 0;
650}
Tony Lindgren11a01862011-03-29 15:54:49 -0700651
652arch_initcall(omap_dm_timer_init);