blob: dfdc3b2e3201a82abf97c6ab56a237216f78468d [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{
Richard Woodruff0f0d0802008-07-03 12:24:30 +0300173 if (timer->posted)
174 while (readl(timer->io_base + (OMAP_TIMER_WRITE_PEND_REG & 0xff))
175 & (reg >> WPSHIFT))
176 cpu_relax();
177 return readl(timer->io_base + (reg & 0xff));
Timo Teras77900a22006-06-26 16:16:12 -0700178}
179
Richard Woodruff0f0d0802008-07-03 12:24:30 +0300180/*
181 * Writes timer registers in posted and non-posted mode. The posted mode bit
182 * is encoded in reg. Note that in posted mode the write pending bit must be
183 * checked. Otherwise a write on a register which has a pending write will be
184 * lost.
185 */
186static void omap_dm_timer_write_reg(struct omap_dm_timer *timer, u32 reg,
187 u32 value)
Timo Teras77900a22006-06-26 16:16:12 -0700188{
Richard Woodruff0f0d0802008-07-03 12:24:30 +0300189 if (timer->posted)
190 while (readl(timer->io_base + (OMAP_TIMER_WRITE_PEND_REG & 0xff))
191 & (reg >> WPSHIFT))
192 cpu_relax();
193 writel(value, timer->io_base + (reg & 0xff));
Tony Lindgren92105bb2005-09-07 17:20:26 +0100194}
195
Timo Teras77900a22006-06-26 16:16:12 -0700196static void omap_dm_timer_wait_for_reset(struct omap_dm_timer *timer)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100197{
Timo Teras77900a22006-06-26 16:16:12 -0700198 int c;
199
200 c = 0;
201 while (!(omap_dm_timer_read_reg(timer, OMAP_TIMER_SYS_STAT_REG) & 1)) {
202 c++;
203 if (c > 100000) {
204 printk(KERN_ERR "Timer failed to reset\n");
205 return;
206 }
207 }
Tony Lindgren92105bb2005-09-07 17:20:26 +0100208}
209
Timo Teras77900a22006-06-26 16:16:12 -0700210static void omap_dm_timer_reset(struct omap_dm_timer *timer)
211{
212 u32 l;
213
Juha Yrjola39020842006-09-25 12:41:44 +0300214 if (!cpu_class_is_omap2() || timer != &dm_timers[0]) {
Timo Terase32f7ec2006-06-26 16:16:13 -0700215 omap_dm_timer_write_reg(timer, OMAP_TIMER_IF_CTRL_REG, 0x06);
216 omap_dm_timer_wait_for_reset(timer);
217 }
Timo Teras12583a72006-09-25 12:41:42 +0300218 omap_dm_timer_set_source(timer, OMAP_TIMER_SRC_32_KHZ);
Timo Teras77900a22006-06-26 16:16:12 -0700219
Timo Teras77900a22006-06-26 16:16:12 -0700220 l = omap_dm_timer_read_reg(timer, OMAP_TIMER_OCP_CFG_REG);
Richard Woodruff0f0d0802008-07-03 12:24:30 +0300221 l |= 0x02 << 3; /* Set to smart-idle mode */
222 l |= 0x2 << 8; /* Set clock activity to perserve f-clock on idle */
Juha Yrjola39020842006-09-25 12:41:44 +0300223
Tero Kristo4ce1e5e2011-03-10 03:50:54 -0700224 /* Enable autoidle on OMAP2 / OMAP3 */
225 if (cpu_is_omap24xx() || cpu_is_omap34xx())
226 l |= 0x1 << 0;
227
Richard Woodruff0f0d0802008-07-03 12:24:30 +0300228 /*
Kevin Hilman219c5b92009-04-23 21:11:08 -0600229 * Enable wake-up on OMAP2 CPUs.
Richard Woodruff0f0d0802008-07-03 12:24:30 +0300230 */
Kevin Hilman219c5b92009-04-23 21:11:08 -0600231 if (cpu_class_is_omap2())
Juha Yrjola39020842006-09-25 12:41:44 +0300232 l |= 1 << 2;
Timo Teras77900a22006-06-26 16:16:12 -0700233 omap_dm_timer_write_reg(timer, OMAP_TIMER_OCP_CFG_REG, l);
Richard Woodruff0f0d0802008-07-03 12:24:30 +0300234
235 /* Match hardware reset default of posted mode */
236 omap_dm_timer_write_reg(timer, OMAP_TIMER_IF_CTRL_REG,
237 OMAP_TIMER_CTRL_POSTED);
238 timer->posted = 1;
Timo Teras77900a22006-06-26 16:16:12 -0700239}
240
Timo Teras83379c82006-06-26 16:16:23 -0700241static void omap_dm_timer_prepare(struct omap_dm_timer *timer)
Timo Teras77900a22006-06-26 16:16:12 -0700242{
Timo Teras12583a72006-09-25 12:41:42 +0300243 omap_dm_timer_enable(timer);
Timo Teras77900a22006-06-26 16:16:12 -0700244 omap_dm_timer_reset(timer);
245}
246
247struct omap_dm_timer *omap_dm_timer_request(void)
248{
249 struct omap_dm_timer *timer = NULL;
250 unsigned long flags;
251 int i;
252
253 spin_lock_irqsave(&dm_timer_lock, flags);
254 for (i = 0; i < dm_timer_count; i++) {
255 if (dm_timers[i].reserved)
256 continue;
257
258 timer = &dm_timers[i];
Timo Teras83379c82006-06-26 16:16:23 -0700259 timer->reserved = 1;
Timo Teras77900a22006-06-26 16:16:12 -0700260 break;
261 }
262 spin_unlock_irqrestore(&dm_timer_lock, flags);
263
Timo Teras83379c82006-06-26 16:16:23 -0700264 if (timer != NULL)
265 omap_dm_timer_prepare(timer);
266
Timo Teras77900a22006-06-26 16:16:12 -0700267 return timer;
268}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700269EXPORT_SYMBOL_GPL(omap_dm_timer_request);
Timo Teras77900a22006-06-26 16:16:12 -0700270
271struct omap_dm_timer *omap_dm_timer_request_specific(int id)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100272{
273 struct omap_dm_timer *timer;
Timo Teras77900a22006-06-26 16:16:12 -0700274 unsigned long flags;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100275
Timo Teras77900a22006-06-26 16:16:12 -0700276 spin_lock_irqsave(&dm_timer_lock, flags);
277 if (id <= 0 || id > dm_timer_count || dm_timers[id-1].reserved) {
278 spin_unlock_irqrestore(&dm_timer_lock, flags);
279 printk("BUG: warning at %s:%d/%s(): unable to get timer %d\n",
Harvey Harrison8e86f422008-03-04 15:08:02 -0800280 __FILE__, __LINE__, __func__, id);
Timo Teras77900a22006-06-26 16:16:12 -0700281 dump_stack();
282 return NULL;
283 }
Tony Lindgren92105bb2005-09-07 17:20:26 +0100284
Timo Teras77900a22006-06-26 16:16:12 -0700285 timer = &dm_timers[id-1];
Timo Teras83379c82006-06-26 16:16:23 -0700286 timer->reserved = 1;
Timo Teras77900a22006-06-26 16:16:12 -0700287 spin_unlock_irqrestore(&dm_timer_lock, flags);
288
Timo Teras83379c82006-06-26 16:16:23 -0700289 omap_dm_timer_prepare(timer);
290
Timo Teras77900a22006-06-26 16:16:12 -0700291 return timer;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100292}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700293EXPORT_SYMBOL_GPL(omap_dm_timer_request_specific);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100294
Timo Teras77900a22006-06-26 16:16:12 -0700295void omap_dm_timer_free(struct omap_dm_timer *timer)
296{
Timo Teras12583a72006-09-25 12:41:42 +0300297 omap_dm_timer_enable(timer);
Timo Teras77900a22006-06-26 16:16:12 -0700298 omap_dm_timer_reset(timer);
Timo Teras12583a72006-09-25 12:41:42 +0300299 omap_dm_timer_disable(timer);
Timo Terasfa4bb622006-09-25 12:41:35 +0300300
Timo Teras77900a22006-06-26 16:16:12 -0700301 WARN_ON(!timer->reserved);
302 timer->reserved = 0;
303}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700304EXPORT_SYMBOL_GPL(omap_dm_timer_free);
Timo Teras77900a22006-06-26 16:16:12 -0700305
Timo Teras12583a72006-09-25 12:41:42 +0300306void omap_dm_timer_enable(struct omap_dm_timer *timer)
307{
308 if (timer->enabled)
309 return;
310
Tony Lindgren882c0512010-02-12 12:26:46 -0800311#ifdef CONFIG_ARCH_OMAP2PLUS
312 if (cpu_class_is_omap2()) {
313 clk_enable(timer->fclk);
314 clk_enable(timer->iclk);
315 }
316#endif
Timo Teras12583a72006-09-25 12:41:42 +0300317
318 timer->enabled = 1;
319}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700320EXPORT_SYMBOL_GPL(omap_dm_timer_enable);
Timo Teras12583a72006-09-25 12:41:42 +0300321
322void omap_dm_timer_disable(struct omap_dm_timer *timer)
323{
324 if (!timer->enabled)
325 return;
326
Tony Lindgren882c0512010-02-12 12:26:46 -0800327#ifdef CONFIG_ARCH_OMAP2PLUS
328 if (cpu_class_is_omap2()) {
329 clk_disable(timer->iclk);
330 clk_disable(timer->fclk);
331 }
332#endif
Timo Teras12583a72006-09-25 12:41:42 +0300333
334 timer->enabled = 0;
335}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700336EXPORT_SYMBOL_GPL(omap_dm_timer_disable);
Timo Teras12583a72006-09-25 12:41:42 +0300337
Timo Teras77900a22006-06-26 16:16:12 -0700338int omap_dm_timer_get_irq(struct omap_dm_timer *timer)
339{
340 return timer->irq;
341}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700342EXPORT_SYMBOL_GPL(omap_dm_timer_get_irq);
Timo Teras77900a22006-06-26 16:16:12 -0700343
344#if defined(CONFIG_ARCH_OMAP1)
345
Tony Lindgrena569c6e2006-04-02 17:46:21 +0100346/**
347 * omap_dm_timer_modify_idlect_mask - Check if any running timers use ARMXOR
348 * @inputmask: current value of idlect mask
349 */
350__u32 omap_dm_timer_modify_idlect_mask(__u32 inputmask)
351{
Timo Teras77900a22006-06-26 16:16:12 -0700352 int i;
Tony Lindgrena569c6e2006-04-02 17:46:21 +0100353
354 /* If ARMXOR cannot be idled this function call is unnecessary */
355 if (!(inputmask & (1 << 1)))
356 return inputmask;
357
358 /* If any active timer is using ARMXOR return modified mask */
Timo Teras77900a22006-06-26 16:16:12 -0700359 for (i = 0; i < dm_timer_count; i++) {
360 u32 l;
361
Tony Lindgren35912c72006-07-01 19:56:42 +0100362 l = omap_dm_timer_read_reg(&dm_timers[i], OMAP_TIMER_CTRL_REG);
Timo Teras77900a22006-06-26 16:16:12 -0700363 if (l & OMAP_TIMER_CTRL_ST) {
364 if (((omap_readl(MOD_CONF_CTRL_1) >> (i * 2)) & 0x03) == 0)
Tony Lindgrena569c6e2006-04-02 17:46:21 +0100365 inputmask &= ~(1 << 1);
366 else
367 inputmask &= ~(1 << 2);
368 }
Timo Teras77900a22006-06-26 16:16:12 -0700369 }
Tony Lindgrena569c6e2006-04-02 17:46:21 +0100370
371 return inputmask;
372}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700373EXPORT_SYMBOL_GPL(omap_dm_timer_modify_idlect_mask);
Tony Lindgrena569c6e2006-04-02 17:46:21 +0100374
Tony Lindgren140455f2010-02-12 12:26:48 -0800375#else
Timo Teras77900a22006-06-26 16:16:12 -0700376
377struct clk *omap_dm_timer_get_fclk(struct omap_dm_timer *timer)
378{
Timo Terasfa4bb622006-09-25 12:41:35 +0300379 return timer->fclk;
Timo Teras77900a22006-06-26 16:16:12 -0700380}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700381EXPORT_SYMBOL_GPL(omap_dm_timer_get_fclk);
Timo Teras77900a22006-06-26 16:16:12 -0700382
383__u32 omap_dm_timer_modify_idlect_mask(__u32 inputmask)
384{
385 BUG();
Dirk Behme21218802006-12-06 17:14:00 -0800386
387 return 0;
Timo Teras77900a22006-06-26 16:16:12 -0700388}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700389EXPORT_SYMBOL_GPL(omap_dm_timer_modify_idlect_mask);
Timo Teras77900a22006-06-26 16:16:12 -0700390
391#endif
392
393void omap_dm_timer_trigger(struct omap_dm_timer *timer)
394{
395 omap_dm_timer_write_reg(timer, OMAP_TIMER_TRIGGER_REG, 0);
396}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700397EXPORT_SYMBOL_GPL(omap_dm_timer_trigger);
Timo Teras77900a22006-06-26 16:16:12 -0700398
399void omap_dm_timer_start(struct omap_dm_timer *timer)
400{
401 u32 l;
402
403 l = omap_dm_timer_read_reg(timer, OMAP_TIMER_CTRL_REG);
404 if (!(l & OMAP_TIMER_CTRL_ST)) {
405 l |= OMAP_TIMER_CTRL_ST;
406 omap_dm_timer_write_reg(timer, OMAP_TIMER_CTRL_REG, l);
407 }
408}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700409EXPORT_SYMBOL_GPL(omap_dm_timer_start);
Timo Teras77900a22006-06-26 16:16:12 -0700410
411void omap_dm_timer_stop(struct omap_dm_timer *timer)
412{
413 u32 l;
414
415 l = omap_dm_timer_read_reg(timer, OMAP_TIMER_CTRL_REG);
416 if (l & OMAP_TIMER_CTRL_ST) {
417 l &= ~0x1;
418 omap_dm_timer_write_reg(timer, OMAP_TIMER_CTRL_REG, l);
Tony Lindgren140455f2010-02-12 12:26:48 -0800419#ifdef CONFIG_ARCH_OMAP2PLUS
Tero Kristo5c3db362009-10-23 19:03:47 +0300420 /* Readback to make sure write has completed */
421 omap_dm_timer_read_reg(timer, OMAP_TIMER_CTRL_REG);
422 /*
423 * Wait for functional clock period x 3.5 to make sure that
424 * timer is stopped
425 */
426 udelay(3500000 / clk_get_rate(timer->fclk) + 1);
Tero Kristo5c3db362009-10-23 19:03:47 +0300427#endif
Timo Teras77900a22006-06-26 16:16:12 -0700428 }
Tero Kristo856f1912010-06-09 13:53:05 +0300429 /* Ack possibly pending interrupt */
430 omap_dm_timer_write_reg(timer, OMAP_TIMER_STAT_REG,
431 OMAP_TIMER_INT_OVERFLOW);
Timo Teras77900a22006-06-26 16:16:12 -0700432}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700433EXPORT_SYMBOL_GPL(omap_dm_timer_stop);
Timo Teras77900a22006-06-26 16:16:12 -0700434
435#ifdef CONFIG_ARCH_OMAP1
Tony Lindgrena569c6e2006-04-02 17:46:21 +0100436
Paul Walmsleyf2480762009-04-23 21:11:10 -0600437int omap_dm_timer_set_source(struct omap_dm_timer *timer, int source)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100438{
439 int n = (timer - dm_timers) << 1;
440 u32 l;
441
442 l = omap_readl(MOD_CONF_CTRL_1) & ~(0x03 << n);
443 l |= source << n;
444 omap_writel(l, MOD_CONF_CTRL_1);
Paul Walmsleyf2480762009-04-23 21:11:10 -0600445
446 return 0;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100447}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700448EXPORT_SYMBOL_GPL(omap_dm_timer_set_source);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100449
Timo Teras77900a22006-06-26 16:16:12 -0700450#else
Tony Lindgren92105bb2005-09-07 17:20:26 +0100451
Paul Walmsleyf2480762009-04-23 21:11:10 -0600452int omap_dm_timer_set_source(struct omap_dm_timer *timer, int source)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100453{
Paul Walmsleyf2480762009-04-23 21:11:10 -0600454 int ret = -EINVAL;
455
Timo Teras77900a22006-06-26 16:16:12 -0700456 if (source < 0 || source >= 3)
Paul Walmsleyf2480762009-04-23 21:11:10 -0600457 return -EINVAL;
Timo Teras77900a22006-06-26 16:16:12 -0700458
Timo Teras77900a22006-06-26 16:16:12 -0700459 clk_disable(timer->fclk);
Paul Walmsleyf2480762009-04-23 21:11:10 -0600460 ret = clk_set_parent(timer->fclk, dm_source_clocks[source]);
Timo Teras77900a22006-06-26 16:16:12 -0700461 clk_enable(timer->fclk);
Timo Teras77900a22006-06-26 16:16:12 -0700462
Paul Walmsleyf2480762009-04-23 21:11:10 -0600463 /*
464 * When the functional clock disappears, too quick writes seem
465 * to cause an abort. XXX Is this still necessary?
466 */
Santosh Shilimkare7193cc2010-09-16 18:44:48 +0530467 __delay(300000);
Paul Walmsleyf2480762009-04-23 21:11:10 -0600468
469 return ret;
Timo Teras77900a22006-06-26 16:16:12 -0700470}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700471EXPORT_SYMBOL_GPL(omap_dm_timer_set_source);
Timo Teras77900a22006-06-26 16:16:12 -0700472
473#endif
474
475void omap_dm_timer_set_load(struct omap_dm_timer *timer, int autoreload,
476 unsigned int load)
477{
478 u32 l;
479
480 l = omap_dm_timer_read_reg(timer, OMAP_TIMER_CTRL_REG);
481 if (autoreload)
482 l |= OMAP_TIMER_CTRL_AR;
483 else
484 l &= ~OMAP_TIMER_CTRL_AR;
485 omap_dm_timer_write_reg(timer, OMAP_TIMER_CTRL_REG, l);
486 omap_dm_timer_write_reg(timer, OMAP_TIMER_LOAD_REG, load);
Richard Woodruff0f0d0802008-07-03 12:24:30 +0300487
Timo Teras77900a22006-06-26 16:16:12 -0700488 omap_dm_timer_write_reg(timer, OMAP_TIMER_TRIGGER_REG, 0);
489}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700490EXPORT_SYMBOL_GPL(omap_dm_timer_set_load);
Timo Teras77900a22006-06-26 16:16:12 -0700491
Richard Woodruff3fddd092008-07-03 12:24:30 +0300492/* Optimized set_load which removes costly spin wait in timer_start */
493void omap_dm_timer_set_load_start(struct omap_dm_timer *timer, int autoreload,
494 unsigned int load)
495{
496 u32 l;
497
498 l = omap_dm_timer_read_reg(timer, OMAP_TIMER_CTRL_REG);
Paul Walmsley64ce2902008-12-10 17:36:34 -0800499 if (autoreload) {
Richard Woodruff3fddd092008-07-03 12:24:30 +0300500 l |= OMAP_TIMER_CTRL_AR;
Paul Walmsley64ce2902008-12-10 17:36:34 -0800501 omap_dm_timer_write_reg(timer, OMAP_TIMER_LOAD_REG, load);
502 } else {
Richard Woodruff3fddd092008-07-03 12:24:30 +0300503 l &= ~OMAP_TIMER_CTRL_AR;
Paul Walmsley64ce2902008-12-10 17:36:34 -0800504 }
Richard Woodruff3fddd092008-07-03 12:24:30 +0300505 l |= OMAP_TIMER_CTRL_ST;
506
507 omap_dm_timer_write_reg(timer, OMAP_TIMER_COUNTER_REG, load);
Richard Woodruff3fddd092008-07-03 12:24:30 +0300508 omap_dm_timer_write_reg(timer, OMAP_TIMER_CTRL_REG, l);
509}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700510EXPORT_SYMBOL_GPL(omap_dm_timer_set_load_start);
Richard Woodruff3fddd092008-07-03 12:24:30 +0300511
Timo Teras77900a22006-06-26 16:16:12 -0700512void omap_dm_timer_set_match(struct omap_dm_timer *timer, int enable,
513 unsigned int match)
514{
515 u32 l;
516
517 l = omap_dm_timer_read_reg(timer, OMAP_TIMER_CTRL_REG);
Timo Teras83379c82006-06-26 16:16:23 -0700518 if (enable)
Timo Teras77900a22006-06-26 16:16:12 -0700519 l |= OMAP_TIMER_CTRL_CE;
520 else
521 l &= ~OMAP_TIMER_CTRL_CE;
522 omap_dm_timer_write_reg(timer, OMAP_TIMER_CTRL_REG, l);
523 omap_dm_timer_write_reg(timer, OMAP_TIMER_MATCH_REG, match);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100524}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700525EXPORT_SYMBOL_GPL(omap_dm_timer_set_match);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100526
Timo Teras77900a22006-06-26 16:16:12 -0700527void omap_dm_timer_set_pwm(struct omap_dm_timer *timer, int def_on,
528 int toggle, int trigger)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100529{
Timo Teras77900a22006-06-26 16:16:12 -0700530 u32 l;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100531
Timo Teras77900a22006-06-26 16:16:12 -0700532 l = omap_dm_timer_read_reg(timer, OMAP_TIMER_CTRL_REG);
533 l &= ~(OMAP_TIMER_CTRL_GPOCFG | OMAP_TIMER_CTRL_SCPWM |
534 OMAP_TIMER_CTRL_PT | (0x03 << 10));
535 if (def_on)
536 l |= OMAP_TIMER_CTRL_SCPWM;
537 if (toggle)
538 l |= OMAP_TIMER_CTRL_PT;
539 l |= trigger << 10;
540 omap_dm_timer_write_reg(timer, OMAP_TIMER_CTRL_REG, l);
541}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700542EXPORT_SYMBOL_GPL(omap_dm_timer_set_pwm);
Timo Teras77900a22006-06-26 16:16:12 -0700543
544void omap_dm_timer_set_prescaler(struct omap_dm_timer *timer, int prescaler)
545{
546 u32 l;
547
548 l = omap_dm_timer_read_reg(timer, OMAP_TIMER_CTRL_REG);
549 l &= ~(OMAP_TIMER_CTRL_PRE | (0x07 << 2));
550 if (prescaler >= 0x00 && prescaler <= 0x07) {
551 l |= OMAP_TIMER_CTRL_PRE;
552 l |= prescaler << 2;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100553 }
Timo Teras77900a22006-06-26 16:16:12 -0700554 omap_dm_timer_write_reg(timer, OMAP_TIMER_CTRL_REG, l);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100555}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700556EXPORT_SYMBOL_GPL(omap_dm_timer_set_prescaler);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100557
558void omap_dm_timer_set_int_enable(struct omap_dm_timer *timer,
Timo Teras77900a22006-06-26 16:16:12 -0700559 unsigned int value)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100560{
561 omap_dm_timer_write_reg(timer, OMAP_TIMER_INT_EN_REG, value);
Juha Yrjola39020842006-09-25 12:41:44 +0300562 omap_dm_timer_write_reg(timer, OMAP_TIMER_WAKEUP_EN_REG, value);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100563}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700564EXPORT_SYMBOL_GPL(omap_dm_timer_set_int_enable);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100565
566unsigned int omap_dm_timer_read_status(struct omap_dm_timer *timer)
567{
Timo Terasfa4bb622006-09-25 12:41:35 +0300568 unsigned int l;
569
Timo Terasfa4bb622006-09-25 12:41:35 +0300570 l = omap_dm_timer_read_reg(timer, OMAP_TIMER_STAT_REG);
Timo Terasfa4bb622006-09-25 12:41:35 +0300571
572 return l;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100573}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700574EXPORT_SYMBOL_GPL(omap_dm_timer_read_status);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100575
576void omap_dm_timer_write_status(struct omap_dm_timer *timer, unsigned int value)
577{
578 omap_dm_timer_write_reg(timer, OMAP_TIMER_STAT_REG, value);
579}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700580EXPORT_SYMBOL_GPL(omap_dm_timer_write_status);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100581
Tony Lindgren92105bb2005-09-07 17:20:26 +0100582unsigned int omap_dm_timer_read_counter(struct omap_dm_timer *timer)
583{
Timo Terasfa4bb622006-09-25 12:41:35 +0300584 unsigned int l;
585
Timo Terasfa4bb622006-09-25 12:41:35 +0300586 l = omap_dm_timer_read_reg(timer, OMAP_TIMER_COUNTER_REG);
Timo Terasfa4bb622006-09-25 12:41:35 +0300587
588 return l;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100589}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700590EXPORT_SYMBOL_GPL(omap_dm_timer_read_counter);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100591
Timo Teras83379c82006-06-26 16:16:23 -0700592void omap_dm_timer_write_counter(struct omap_dm_timer *timer, unsigned int value)
593{
Timo Terasfa4bb622006-09-25 12:41:35 +0300594 omap_dm_timer_write_reg(timer, OMAP_TIMER_COUNTER_REG, value);
Timo Teras83379c82006-06-26 16:16:23 -0700595}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700596EXPORT_SYMBOL_GPL(omap_dm_timer_write_counter);
Timo Teras83379c82006-06-26 16:16:23 -0700597
Timo Teras77900a22006-06-26 16:16:12 -0700598int omap_dm_timers_active(void)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100599{
Timo Teras77900a22006-06-26 16:16:12 -0700600 int i;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100601
Timo Teras77900a22006-06-26 16:16:12 -0700602 for (i = 0; i < dm_timer_count; i++) {
603 struct omap_dm_timer *timer;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100604
Timo Teras77900a22006-06-26 16:16:12 -0700605 timer = &dm_timers[i];
Timo Teras12583a72006-09-25 12:41:42 +0300606
607 if (!timer->enabled)
608 continue;
609
Timo Teras77900a22006-06-26 16:16:12 -0700610 if (omap_dm_timer_read_reg(timer, OMAP_TIMER_CTRL_REG) &
Timo Terasfa4bb622006-09-25 12:41:35 +0300611 OMAP_TIMER_CTRL_ST) {
Timo Teras77900a22006-06-26 16:16:12 -0700612 return 1;
Timo Terasfa4bb622006-09-25 12:41:35 +0300613 }
Tony Lindgren92105bb2005-09-07 17:20:26 +0100614 }
Tony Lindgren92105bb2005-09-07 17:20:26 +0100615 return 0;
616}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700617EXPORT_SYMBOL_GPL(omap_dm_timers_active);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100618
Syed Mohammed, Khasim471b3aa2007-06-21 21:48:07 -0700619int __init omap_dm_timer_init(void)
Timo Teras77900a22006-06-26 16:16:12 -0700620{
621 struct omap_dm_timer *timer;
Tony Lindgren3566fc62009-10-19 15:25:18 -0700622 int i, map_size = SZ_8K; /* Module 4KB + L4 4KB except on omap1 */
Timo Teras77900a22006-06-26 16:16:12 -0700623
Syed Mohammed, Khasimce2df9c2007-06-25 22:55:39 -0700624 if (!(cpu_is_omap16xx() || cpu_class_is_omap2()))
Timo Teras77900a22006-06-26 16:16:12 -0700625 return -ENODEV;
626
627 spin_lock_init(&dm_timer_lock);
Syed Mohammed, Khasim471b3aa2007-06-21 21:48:07 -0700628
Tony Lindgren3566fc62009-10-19 15:25:18 -0700629 if (cpu_class_is_omap1()) {
Syed Mohammed, Khasim471b3aa2007-06-21 21:48:07 -0700630 dm_timers = omap1_dm_timers;
Tony Lindgren882c0512010-02-12 12:26:46 -0800631 dm_timer_count = omap1_dm_timer_count;
Tony Lindgren3566fc62009-10-19 15:25:18 -0700632 map_size = SZ_2K;
633 } else if (cpu_is_omap24xx()) {
Syed Mohammed, Khasim471b3aa2007-06-21 21:48:07 -0700634 dm_timers = omap2_dm_timers;
Tony Lindgren882c0512010-02-12 12:26:46 -0800635 dm_timer_count = omap2_dm_timer_count;
Santosh Shilimkaraea2a5b2009-05-25 11:08:36 -0700636 dm_source_names = omap2_dm_source_names;
637 dm_source_clocks = omap2_dm_source_clocks;
Syed Mohammed, Khasimce2df9c2007-06-25 22:55:39 -0700638 } else if (cpu_is_omap34xx()) {
639 dm_timers = omap3_dm_timers;
Tony Lindgren882c0512010-02-12 12:26:46 -0800640 dm_timer_count = omap3_dm_timer_count;
Santosh Shilimkaraea2a5b2009-05-25 11:08:36 -0700641 dm_source_names = omap3_dm_source_names;
642 dm_source_clocks = omap3_dm_source_clocks;
Santosh Shilimkar44169072009-05-28 14:16:04 -0700643 } else if (cpu_is_omap44xx()) {
644 dm_timers = omap4_dm_timers;
Tony Lindgren882c0512010-02-12 12:26:46 -0800645 dm_timer_count = omap4_dm_timer_count;
Santosh Shilimkar44169072009-05-28 14:16:04 -0700646 dm_source_names = omap4_dm_source_names;
647 dm_source_clocks = omap4_dm_source_clocks;
Timo Teras83379c82006-06-26 16:16:23 -0700648 }
Syed Mohammed, Khasim471b3aa2007-06-21 21:48:07 -0700649
650 if (cpu_class_is_omap2())
651 for (i = 0; dm_source_names[i] != NULL; i++)
652 dm_source_clocks[i] = clk_get(NULL, dm_source_names[i]);
653
Syed Mohammed Khasim56a25642006-12-06 17:14:08 -0800654 if (cpu_is_omap243x())
655 dm_timers[0].phys_base = 0x49018000;
Timo Teras83379c82006-06-26 16:16:23 -0700656
Timo Teras77900a22006-06-26 16:16:12 -0700657 for (i = 0; i < dm_timer_count; i++) {
Timo Teras77900a22006-06-26 16:16:12 -0700658 timer = &dm_timers[i];
Tony Lindgren3566fc62009-10-19 15:25:18 -0700659
660 /* Static mapping, never released */
661 timer->io_base = ioremap(timer->phys_base, map_size);
662 BUG_ON(!timer->io_base);
663
Tony Lindgren140455f2010-02-12 12:26:48 -0800664#ifdef CONFIG_ARCH_OMAP2PLUS
Syed Mohammed, Khasim471b3aa2007-06-21 21:48:07 -0700665 if (cpu_class_is_omap2()) {
666 char clk_name[16];
667 sprintf(clk_name, "gpt%d_ick", i + 1);
668 timer->iclk = clk_get(NULL, clk_name);
669 sprintf(clk_name, "gpt%d_fck", i + 1);
670 timer->fclk = clk_get(NULL, clk_name);
671 }
Timo Teras77900a22006-06-26 16:16:12 -0700672#endif
673 }
674
675 return 0;
676}