blob: 305faf539465c71b5e292c020a4cd5b99f75ea3a [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 *
Tarun Kanti DebBarma97933d62011-09-20 17:00:17 +05306 * Copyright (C) 2010 Texas Instruments Incorporated - http://www.ti.com/
7 * Tarun Kanti DebBarma <tarun.kanti@ti.com>
8 * Thara Gopinath <thara@ti.com>
9 *
10 * dmtimer adaptation to platform_driver.
11 *
Tony Lindgren92105bb2005-09-07 17:20:26 +010012 * Copyright (C) 2005 Nokia Corporation
Timo Teras77900a22006-06-26 16:16:12 -070013 * OMAP2 support by Juha Yrjola
14 * API improvements and OMAP2 clock framework support by Timo Teras
Tony Lindgren92105bb2005-09-07 17:20:26 +010015 *
Santosh Shilimkar44169072009-05-28 14:16:04 -070016 * Copyright (C) 2009 Texas Instruments
17 * Added OMAP4 support - Santosh Shilimkar <santosh.shilimkar@ti.com>
18 *
Tony Lindgren92105bb2005-09-07 17:20:26 +010019 * This program is free software; you can redistribute it and/or modify it
20 * under the terms of the GNU General Public License as published by the
21 * Free Software Foundation; either version 2 of the License, or (at your
22 * option) any later version.
23 *
24 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
25 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
26 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
27 * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
28 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
29 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 *
33 * You should have received a copy of the GNU General Public License along
34 * with this program; if not, write to the Free Software Foundation, Inc.,
35 * 675 Mass Ave, Cambridge, MA 02139, USA.
36 */
37
Axel Lin869dec12011-11-02 09:49:46 +080038#include <linux/module.h>
Russell Kingfced80c2008-09-06 12:10:45 +010039#include <linux/io.h>
Tarun Kanti DebBarma74dd9ec2012-04-20 18:09:20 +053040#include <linux/device.h>
Tarun Kanti DebBarma3392cdd2011-09-20 17:00:20 +053041#include <linux/err.h>
Tarun Kanti DebBarmaffe07ce2011-09-20 17:00:21 +053042#include <linux/pm_runtime.h>
Jon Hunter9725f442012-05-14 10:41:37 -050043#include <linux/of.h>
44#include <linux/of_device.h>
Tarun Kanti DebBarma3392cdd2011-09-20 17:00:20 +053045
Tony Lindgrence491cf2009-10-20 09:40:47 -070046#include <plat/dmtimer.h>
Tony Lindgren2c799ce2012-02-24 10:34:35 -080047
Jon Hunterb7b4ff72012-06-05 12:34:51 -050048static u32 omap_reserved_systimers;
Tarun Kanti DebBarmadf284722011-09-20 17:00:19 +053049static LIST_HEAD(omap_timer_list);
Tarun Kanti DebBarma3392cdd2011-09-20 17:00:20 +053050static DEFINE_SPINLOCK(dm_timer_lock);
Tony Lindgren92105bb2005-09-07 17:20:26 +010051
Tarun Kanti DebBarma3392cdd2011-09-20 17:00:20 +053052/**
53 * omap_dm_timer_read_reg - read timer registers in posted and non-posted mode
54 * @timer: timer pointer over which read operation to perform
55 * @reg: lowest byte holds the register offset
56 *
57 * The posted mode bit is encoded in reg. Note that in posted mode write
58 * pending bit must be checked. Otherwise a read of a non completed write
59 * will produce an error.
Richard Woodruff0f0d0802008-07-03 12:24:30 +030060 */
61static inline u32 omap_dm_timer_read_reg(struct omap_dm_timer *timer, u32 reg)
Tony Lindgren92105bb2005-09-07 17:20:26 +010062{
Tony Lindgrenee17f112011-09-16 15:44:20 -070063 WARN_ON((reg & 0xff) < _OMAP_TIMER_WAKEUP_EN_OFFSET);
64 return __omap_dm_timer_read(timer, reg, timer->posted);
Timo Teras77900a22006-06-26 16:16:12 -070065}
66
Tarun Kanti DebBarma3392cdd2011-09-20 17:00:20 +053067/**
68 * omap_dm_timer_write_reg - write timer registers in posted and non-posted mode
69 * @timer: timer pointer over which write operation is to perform
70 * @reg: lowest byte holds the register offset
71 * @value: data to write into the register
72 *
73 * The posted mode bit is encoded in reg. Note that in posted mode the write
74 * pending bit must be checked. Otherwise a write on a register which has a
75 * pending write will be lost.
Richard Woodruff0f0d0802008-07-03 12:24:30 +030076 */
77static void omap_dm_timer_write_reg(struct omap_dm_timer *timer, u32 reg,
78 u32 value)
Timo Teras77900a22006-06-26 16:16:12 -070079{
Tony Lindgrenee17f112011-09-16 15:44:20 -070080 WARN_ON((reg & 0xff) < _OMAP_TIMER_WAKEUP_EN_OFFSET);
81 __omap_dm_timer_write(timer, reg, value, timer->posted);
Tony Lindgren92105bb2005-09-07 17:20:26 +010082}
83
Tarun Kanti DebBarmab4811132011-09-20 17:00:24 +053084static void omap_timer_restore_context(struct omap_dm_timer *timer)
85{
Tarun Kanti DebBarmab4811132011-09-20 17:00:24 +053086 omap_dm_timer_write_reg(timer, OMAP_TIMER_WAKEUP_EN_REG,
87 timer->context.twer);
88 omap_dm_timer_write_reg(timer, OMAP_TIMER_COUNTER_REG,
89 timer->context.tcrr);
90 omap_dm_timer_write_reg(timer, OMAP_TIMER_LOAD_REG,
91 timer->context.tldr);
92 omap_dm_timer_write_reg(timer, OMAP_TIMER_MATCH_REG,
93 timer->context.tmar);
94 omap_dm_timer_write_reg(timer, OMAP_TIMER_IF_CTRL_REG,
95 timer->context.tsicr);
96 __raw_writel(timer->context.tier, timer->irq_ena);
97 omap_dm_timer_write_reg(timer, OMAP_TIMER_CTRL_REG,
98 timer->context.tclr);
99}
100
Timo Teras77900a22006-06-26 16:16:12 -0700101static void omap_dm_timer_wait_for_reset(struct omap_dm_timer *timer)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100102{
Timo Teras77900a22006-06-26 16:16:12 -0700103 int c;
104
Tony Lindgrenee17f112011-09-16 15:44:20 -0700105 if (!timer->sys_stat)
106 return;
107
Timo Teras77900a22006-06-26 16:16:12 -0700108 c = 0;
Tony Lindgrenee17f112011-09-16 15:44:20 -0700109 while (!(__raw_readl(timer->sys_stat) & 1)) {
Timo Teras77900a22006-06-26 16:16:12 -0700110 c++;
111 if (c > 100000) {
112 printk(KERN_ERR "Timer failed to reset\n");
113 return;
114 }
115 }
Tony Lindgren92105bb2005-09-07 17:20:26 +0100116}
117
Timo Teras77900a22006-06-26 16:16:12 -0700118static void omap_dm_timer_reset(struct omap_dm_timer *timer)
119{
Jon Hunterffc957b2012-07-06 16:46:35 -0500120 omap_dm_timer_write_reg(timer, OMAP_TIMER_IF_CTRL_REG, 0x06);
121 omap_dm_timer_wait_for_reset(timer);
Tarun Kanti DebBarma3392cdd2011-09-20 17:00:20 +0530122 __omap_dm_timer_reset(timer, 0, 0);
Timo Teras77900a22006-06-26 16:16:12 -0700123}
124
Tarun Kanti DebBarma3392cdd2011-09-20 17:00:20 +0530125int omap_dm_timer_prepare(struct omap_dm_timer *timer)
Timo Teras77900a22006-06-26 16:16:12 -0700126{
Jon Hunterbca45802012-06-05 12:34:58 -0500127 /*
128 * FIXME: OMAP1 devices do not use the clock framework for dmtimers so
129 * do not call clk_get() for these devices.
130 */
131 if (!(timer->capability & OMAP_TIMER_NEEDS_RESET)) {
132 timer->fclk = clk_get(&timer->pdev->dev, "fck");
133 if (WARN_ON_ONCE(IS_ERR_OR_NULL(timer->fclk))) {
134 timer->fclk = NULL;
135 dev_err(&timer->pdev->dev, ": No fclk handle.\n");
136 return -EINVAL;
137 }
Tarun Kanti DebBarma3392cdd2011-09-20 17:00:20 +0530138 }
139
Jon Hunter7b44cf22012-07-06 16:45:04 -0500140 omap_dm_timer_enable(timer);
141
Jon Hunter66159752012-06-05 12:34:57 -0500142 if (timer->capability & OMAP_TIMER_NEEDS_RESET)
Tarun Kanti DebBarma3392cdd2011-09-20 17:00:20 +0530143 omap_dm_timer_reset(timer);
144
Jon Hunter7b44cf22012-07-06 16:45:04 -0500145 __omap_dm_timer_enable_posted(timer);
146 omap_dm_timer_disable(timer);
Tarun Kanti DebBarma3392cdd2011-09-20 17:00:20 +0530147
Jon Hunter7b44cf22012-07-06 16:45:04 -0500148 return omap_dm_timer_set_source(timer, OMAP_TIMER_SRC_32_KHZ);
Timo Teras77900a22006-06-26 16:16:12 -0700149}
150
Jon Hunterb7b4ff72012-06-05 12:34:51 -0500151static inline u32 omap_dm_timer_reserved_systimer(int id)
152{
153 return (omap_reserved_systimers & (1 << (id - 1))) ? 1 : 0;
154}
155
156int omap_dm_timer_reserve_systimer(int id)
157{
158 if (omap_dm_timer_reserved_systimer(id))
159 return -ENODEV;
160
161 omap_reserved_systimers |= (1 << (id - 1));
162
163 return 0;
164}
165
Timo Teras77900a22006-06-26 16:16:12 -0700166struct omap_dm_timer *omap_dm_timer_request(void)
167{
Tarun Kanti DebBarma3392cdd2011-09-20 17:00:20 +0530168 struct omap_dm_timer *timer = NULL, *t;
Timo Teras77900a22006-06-26 16:16:12 -0700169 unsigned long flags;
Tarun Kanti DebBarma3392cdd2011-09-20 17:00:20 +0530170 int ret = 0;
Timo Teras77900a22006-06-26 16:16:12 -0700171
172 spin_lock_irqsave(&dm_timer_lock, flags);
Tarun Kanti DebBarma3392cdd2011-09-20 17:00:20 +0530173 list_for_each_entry(t, &omap_timer_list, node) {
174 if (t->reserved)
Timo Teras77900a22006-06-26 16:16:12 -0700175 continue;
176
Tarun Kanti DebBarma3392cdd2011-09-20 17:00:20 +0530177 timer = t;
Timo Teras83379c82006-06-26 16:16:23 -0700178 timer->reserved = 1;
Timo Teras77900a22006-06-26 16:16:12 -0700179 break;
180 }
Timo Kokkonenc5491d12012-08-12 13:45:34 +0300181 spin_unlock_irqrestore(&dm_timer_lock, flags);
Tarun Kanti DebBarma3392cdd2011-09-20 17:00:20 +0530182
183 if (timer) {
184 ret = omap_dm_timer_prepare(timer);
185 if (ret) {
186 timer->reserved = 0;
187 timer = NULL;
188 }
189 }
Timo Teras77900a22006-06-26 16:16:12 -0700190
Tarun Kanti DebBarma3392cdd2011-09-20 17:00:20 +0530191 if (!timer)
192 pr_debug("%s: timer request failed!\n", __func__);
Timo Teras83379c82006-06-26 16:16:23 -0700193
Timo Teras77900a22006-06-26 16:16:12 -0700194 return timer;
195}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700196EXPORT_SYMBOL_GPL(omap_dm_timer_request);
Timo Teras77900a22006-06-26 16:16:12 -0700197
198struct omap_dm_timer *omap_dm_timer_request_specific(int id)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100199{
Tarun Kanti DebBarma3392cdd2011-09-20 17:00:20 +0530200 struct omap_dm_timer *timer = NULL, *t;
Timo Teras77900a22006-06-26 16:16:12 -0700201 unsigned long flags;
Tarun Kanti DebBarma3392cdd2011-09-20 17:00:20 +0530202 int ret = 0;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100203
Jon Hunter9725f442012-05-14 10:41:37 -0500204 /* Requesting timer by ID is not supported when device tree is used */
205 if (of_have_populated_dt()) {
206 pr_warn("%s: Please use omap_dm_timer_request_by_cap()\n",
207 __func__);
208 return NULL;
209 }
210
Timo Teras77900a22006-06-26 16:16:12 -0700211 spin_lock_irqsave(&dm_timer_lock, flags);
Tarun Kanti DebBarma3392cdd2011-09-20 17:00:20 +0530212 list_for_each_entry(t, &omap_timer_list, node) {
213 if (t->pdev->id == id && !t->reserved) {
214 timer = t;
215 timer->reserved = 1;
216 break;
217 }
Timo Teras77900a22006-06-26 16:16:12 -0700218 }
Timo Kokkonenc5491d12012-08-12 13:45:34 +0300219 spin_unlock_irqrestore(&dm_timer_lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100220
Tarun Kanti DebBarma3392cdd2011-09-20 17:00:20 +0530221 if (timer) {
222 ret = omap_dm_timer_prepare(timer);
223 if (ret) {
224 timer->reserved = 0;
225 timer = NULL;
226 }
227 }
Timo Teras77900a22006-06-26 16:16:12 -0700228
Tarun Kanti DebBarma3392cdd2011-09-20 17:00:20 +0530229 if (!timer)
230 pr_debug("%s: timer%d request failed!\n", __func__, id);
Timo Teras83379c82006-06-26 16:16:23 -0700231
Timo Teras77900a22006-06-26 16:16:12 -0700232 return timer;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100233}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700234EXPORT_SYMBOL_GPL(omap_dm_timer_request_specific);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100235
Jon Hunter373fe0b2012-09-06 15:28:00 -0500236/**
237 * omap_dm_timer_request_by_cap - Request a timer by capability
238 * @cap: Bit mask of capabilities to match
239 *
240 * Find a timer based upon capabilities bit mask. Callers of this function
241 * should use the definitions found in the plat/dmtimer.h file under the
242 * comment "timer capabilities used in hwmod database". Returns pointer to
243 * timer handle on success and a NULL pointer on failure.
244 */
245struct omap_dm_timer *omap_dm_timer_request_by_cap(u32 cap)
246{
247 struct omap_dm_timer *timer = NULL, *t;
248 unsigned long flags;
249
250 if (!cap)
251 return NULL;
252
253 spin_lock_irqsave(&dm_timer_lock, flags);
254 list_for_each_entry(t, &omap_timer_list, node) {
255 if ((!t->reserved) && ((t->capability & cap) == cap)) {
256 /*
257 * If timer is not NULL, we have already found one timer
258 * but it was not an exact match because it had more
259 * capabilites that what was required. Therefore,
260 * unreserve the last timer found and see if this one
261 * is a better match.
262 */
263 if (timer)
264 timer->reserved = 0;
265
266 timer = t;
267 timer->reserved = 1;
268
269 /* Exit loop early if we find an exact match */
270 if (t->capability == cap)
271 break;
272 }
273 }
274 spin_unlock_irqrestore(&dm_timer_lock, flags);
275
276 if (timer && omap_dm_timer_prepare(timer)) {
277 timer->reserved = 0;
278 timer = NULL;
279 }
280
281 if (!timer)
282 pr_debug("%s: timer request failed!\n", __func__);
283
284 return timer;
285}
286EXPORT_SYMBOL_GPL(omap_dm_timer_request_by_cap);
287
Tarun Kanti DebBarmaab4eb8b2011-09-20 17:00:26 +0530288int omap_dm_timer_free(struct omap_dm_timer *timer)
Timo Teras77900a22006-06-26 16:16:12 -0700289{
Tarun Kanti DebBarmaab4eb8b2011-09-20 17:00:26 +0530290 if (unlikely(!timer))
291 return -EINVAL;
292
Tarun Kanti DebBarma3392cdd2011-09-20 17:00:20 +0530293 clk_put(timer->fclk);
Timo Terasfa4bb622006-09-25 12:41:35 +0300294
Timo Teras77900a22006-06-26 16:16:12 -0700295 WARN_ON(!timer->reserved);
296 timer->reserved = 0;
Tarun Kanti DebBarmaab4eb8b2011-09-20 17:00:26 +0530297 return 0;
Timo Teras77900a22006-06-26 16:16:12 -0700298}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700299EXPORT_SYMBOL_GPL(omap_dm_timer_free);
Timo Teras77900a22006-06-26 16:16:12 -0700300
Timo Teras12583a72006-09-25 12:41:42 +0300301void omap_dm_timer_enable(struct omap_dm_timer *timer)
302{
Tarun Kanti DebBarmaffe07ce2011-09-20 17:00:21 +0530303 pm_runtime_get_sync(&timer->pdev->dev);
Timo Teras12583a72006-09-25 12:41:42 +0300304}
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{
Jon Hunter54f32a32012-07-13 15:12:03 -0500309 pm_runtime_put_sync(&timer->pdev->dev);
Timo Teras12583a72006-09-25 12:41:42 +0300310}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700311EXPORT_SYMBOL_GPL(omap_dm_timer_disable);
Timo Teras12583a72006-09-25 12:41:42 +0300312
Timo Teras77900a22006-06-26 16:16:12 -0700313int omap_dm_timer_get_irq(struct omap_dm_timer *timer)
314{
Tarun Kanti DebBarmaab4eb8b2011-09-20 17:00:26 +0530315 if (timer)
316 return timer->irq;
317 return -EINVAL;
Timo Teras77900a22006-06-26 16:16:12 -0700318}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700319EXPORT_SYMBOL_GPL(omap_dm_timer_get_irq);
Timo Teras77900a22006-06-26 16:16:12 -0700320
321#if defined(CONFIG_ARCH_OMAP1)
Tony Lindgren7136f8d2012-10-31 12:38:43 -0700322#include <mach/hardware.h>
Tony Lindgrena569c6e2006-04-02 17:46:21 +0100323/**
324 * omap_dm_timer_modify_idlect_mask - Check if any running timers use ARMXOR
325 * @inputmask: current value of idlect mask
326 */
327__u32 omap_dm_timer_modify_idlect_mask(__u32 inputmask)
328{
Tarun Kanti DebBarma3392cdd2011-09-20 17:00:20 +0530329 int i = 0;
330 struct omap_dm_timer *timer = NULL;
331 unsigned long flags;
Tony Lindgrena569c6e2006-04-02 17:46:21 +0100332
333 /* If ARMXOR cannot be idled this function call is unnecessary */
334 if (!(inputmask & (1 << 1)))
335 return inputmask;
336
337 /* If any active timer is using ARMXOR return modified mask */
Tarun Kanti DebBarma3392cdd2011-09-20 17:00:20 +0530338 spin_lock_irqsave(&dm_timer_lock, flags);
339 list_for_each_entry(timer, &omap_timer_list, node) {
Timo Teras77900a22006-06-26 16:16:12 -0700340 u32 l;
341
Tarun Kanti DebBarma3392cdd2011-09-20 17:00:20 +0530342 l = omap_dm_timer_read_reg(timer, OMAP_TIMER_CTRL_REG);
Timo Teras77900a22006-06-26 16:16:12 -0700343 if (l & OMAP_TIMER_CTRL_ST) {
344 if (((omap_readl(MOD_CONF_CTRL_1) >> (i * 2)) & 0x03) == 0)
Tony Lindgrena569c6e2006-04-02 17:46:21 +0100345 inputmask &= ~(1 << 1);
346 else
347 inputmask &= ~(1 << 2);
348 }
Tarun Kanti DebBarma3392cdd2011-09-20 17:00:20 +0530349 i++;
Timo Teras77900a22006-06-26 16:16:12 -0700350 }
Tarun Kanti DebBarma3392cdd2011-09-20 17:00:20 +0530351 spin_unlock_irqrestore(&dm_timer_lock, flags);
Tony Lindgrena569c6e2006-04-02 17:46:21 +0100352
353 return inputmask;
354}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700355EXPORT_SYMBOL_GPL(omap_dm_timer_modify_idlect_mask);
Tony Lindgrena569c6e2006-04-02 17:46:21 +0100356
Tony Lindgren140455f2010-02-12 12:26:48 -0800357#else
Timo Teras77900a22006-06-26 16:16:12 -0700358
359struct clk *omap_dm_timer_get_fclk(struct omap_dm_timer *timer)
360{
Tarun Kanti DebBarmaab4eb8b2011-09-20 17:00:26 +0530361 if (timer)
362 return timer->fclk;
363 return NULL;
Timo Teras77900a22006-06-26 16:16:12 -0700364}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700365EXPORT_SYMBOL_GPL(omap_dm_timer_get_fclk);
Timo Teras77900a22006-06-26 16:16:12 -0700366
367__u32 omap_dm_timer_modify_idlect_mask(__u32 inputmask)
368{
369 BUG();
Dirk Behme21218802006-12-06 17:14:00 -0800370
371 return 0;
Timo Teras77900a22006-06-26 16:16:12 -0700372}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700373EXPORT_SYMBOL_GPL(omap_dm_timer_modify_idlect_mask);
Timo Teras77900a22006-06-26 16:16:12 -0700374
375#endif
376
Tarun Kanti DebBarmaab4eb8b2011-09-20 17:00:26 +0530377int omap_dm_timer_trigger(struct omap_dm_timer *timer)
Timo Teras77900a22006-06-26 16:16:12 -0700378{
Tarun Kanti DebBarmaab4eb8b2011-09-20 17:00:26 +0530379 if (unlikely(!timer || pm_runtime_suspended(&timer->pdev->dev))) {
380 pr_err("%s: timer not available or enabled.\n", __func__);
381 return -EINVAL;
Tarun Kanti DebBarmab4811132011-09-20 17:00:24 +0530382 }
383
Timo Teras77900a22006-06-26 16:16:12 -0700384 omap_dm_timer_write_reg(timer, OMAP_TIMER_TRIGGER_REG, 0);
Tarun Kanti DebBarmaab4eb8b2011-09-20 17:00:26 +0530385 return 0;
Timo Teras77900a22006-06-26 16:16:12 -0700386}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700387EXPORT_SYMBOL_GPL(omap_dm_timer_trigger);
Timo Teras77900a22006-06-26 16:16:12 -0700388
Tarun Kanti DebBarmaab4eb8b2011-09-20 17:00:26 +0530389int omap_dm_timer_start(struct omap_dm_timer *timer)
Timo Teras77900a22006-06-26 16:16:12 -0700390{
391 u32 l;
392
Tarun Kanti DebBarmaab4eb8b2011-09-20 17:00:26 +0530393 if (unlikely(!timer))
394 return -EINVAL;
395
Tarun Kanti DebBarmab4811132011-09-20 17:00:24 +0530396 omap_dm_timer_enable(timer);
397
Jon Hunter1c2d0762012-06-05 12:34:55 -0500398 if (!(timer->capability & OMAP_TIMER_ALWON)) {
Tony Lindgren6e740f92012-10-29 15:20:45 -0700399 if (timer->get_context_loss_count &&
400 timer->get_context_loss_count(&timer->pdev->dev) !=
Jon Hunter0b30ec12012-06-05 12:34:56 -0500401 timer->ctx_loss_count)
Tarun Kanti DebBarmab4811132011-09-20 17:00:24 +0530402 omap_timer_restore_context(timer);
403 }
404
Timo Teras77900a22006-06-26 16:16:12 -0700405 l = omap_dm_timer_read_reg(timer, OMAP_TIMER_CTRL_REG);
406 if (!(l & OMAP_TIMER_CTRL_ST)) {
407 l |= OMAP_TIMER_CTRL_ST;
408 omap_dm_timer_write_reg(timer, OMAP_TIMER_CTRL_REG, l);
409 }
Tarun Kanti DebBarmab4811132011-09-20 17:00:24 +0530410
411 /* Save the context */
412 timer->context.tclr = l;
Tarun Kanti DebBarmaab4eb8b2011-09-20 17:00:26 +0530413 return 0;
Timo Teras77900a22006-06-26 16:16:12 -0700414}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700415EXPORT_SYMBOL_GPL(omap_dm_timer_start);
Timo Teras77900a22006-06-26 16:16:12 -0700416
Tarun Kanti DebBarmaab4eb8b2011-09-20 17:00:26 +0530417int omap_dm_timer_stop(struct omap_dm_timer *timer)
Timo Teras77900a22006-06-26 16:16:12 -0700418{
Tony Lindgrencaf64f22011-03-29 15:54:48 -0700419 unsigned long rate = 0;
Timo Teras77900a22006-06-26 16:16:12 -0700420
Tarun Kanti DebBarmaab4eb8b2011-09-20 17:00:26 +0530421 if (unlikely(!timer))
422 return -EINVAL;
423
Jon Hunter66159752012-06-05 12:34:57 -0500424 if (!(timer->capability & OMAP_TIMER_NEEDS_RESET))
Tarun Kanti DebBarma3392cdd2011-09-20 17:00:20 +0530425 rate = clk_get_rate(timer->fclk);
Tony Lindgrencaf64f22011-03-29 15:54:48 -0700426
Tony Lindgrenee17f112011-09-16 15:44:20 -0700427 __omap_dm_timer_stop(timer, timer->posted, rate);
Tarun Kanti DebBarmaab4eb8b2011-09-20 17:00:26 +0530428
Tony Lindgren6e740f92012-10-29 15:20:45 -0700429 if (!(timer->capability & OMAP_TIMER_ALWON)) {
430 if (timer->get_context_loss_count)
431 timer->ctx_loss_count =
432 timer->get_context_loss_count(&timer->pdev->dev);
433 }
Tarun Kanti DebBarmadffc9da2012-03-05 16:11:00 -0800434
435 /*
436 * Since the register values are computed and written within
437 * __omap_dm_timer_stop, we need to use read to retrieve the
438 * context.
439 */
440 timer->context.tclr =
441 omap_dm_timer_read_reg(timer, OMAP_TIMER_CTRL_REG);
Tarun Kanti DebBarmadffc9da2012-03-05 16:11:00 -0800442 omap_dm_timer_disable(timer);
Tarun Kanti DebBarmaab4eb8b2011-09-20 17:00:26 +0530443 return 0;
Timo Teras77900a22006-06-26 16:16:12 -0700444}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700445EXPORT_SYMBOL_GPL(omap_dm_timer_stop);
Timo Teras77900a22006-06-26 16:16:12 -0700446
Paul Walmsleyf2480762009-04-23 21:11:10 -0600447int omap_dm_timer_set_source(struct omap_dm_timer *timer, int source)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100448{
Tarun Kanti DebBarma3392cdd2011-09-20 17:00:20 +0530449 int ret;
Jon Hunter2b2d3522012-06-05 12:34:59 -0500450 char *parent_name = NULL;
Jon Hunterd7aba552012-07-18 20:10:12 -0500451 struct clk *parent;
Tarun Kanti DebBarmaab4eb8b2011-09-20 17:00:26 +0530452 struct dmtimer_platform_data *pdata;
453
454 if (unlikely(!timer))
455 return -EINVAL;
456
457 pdata = timer->pdev->dev.platform_data;
Tarun Kanti DebBarma3392cdd2011-09-20 17:00:20 +0530458
Timo Teras77900a22006-06-26 16:16:12 -0700459 if (source < 0 || source >= 3)
Paul Walmsleyf2480762009-04-23 21:11:10 -0600460 return -EINVAL;
Timo Teras77900a22006-06-26 16:16:12 -0700461
Jon Hunter2b2d3522012-06-05 12:34:59 -0500462 /*
463 * FIXME: Used for OMAP1 devices only because they do not currently
464 * use the clock framework to set the parent clock. To be removed
465 * once OMAP1 migrated to using clock framework for dmtimers
466 */
Jon Hunter9725f442012-05-14 10:41:37 -0500467 if (pdata && pdata->set_timer_src)
Jon Hunter2b2d3522012-06-05 12:34:59 -0500468 return pdata->set_timer_src(timer->pdev, source);
469
Jon Hunterd7aba552012-07-18 20:10:12 -0500470 if (!timer->fclk)
Jon Hunter2b2d3522012-06-05 12:34:59 -0500471 return -EINVAL;
Jon Hunter2b2d3522012-06-05 12:34:59 -0500472
473 switch (source) {
474 case OMAP_TIMER_SRC_SYS_CLK:
Jon Hunterc59b5372012-06-05 12:35:00 -0500475 parent_name = "timer_sys_ck";
Jon Hunter2b2d3522012-06-05 12:34:59 -0500476 break;
477
478 case OMAP_TIMER_SRC_32_KHZ:
Jon Hunterc59b5372012-06-05 12:35:00 -0500479 parent_name = "timer_32k_ck";
Jon Hunter2b2d3522012-06-05 12:34:59 -0500480 break;
481
482 case OMAP_TIMER_SRC_EXT_CLK:
Jon Hunterc59b5372012-06-05 12:35:00 -0500483 parent_name = "timer_ext_ck";
Jon Hunter2b2d3522012-06-05 12:34:59 -0500484 break;
485 }
486
487 parent = clk_get(&timer->pdev->dev, parent_name);
488 if (IS_ERR_OR_NULL(parent)) {
489 pr_err("%s: %s not found\n", __func__, parent_name);
Jon Hunterd7aba552012-07-18 20:10:12 -0500490 return -EINVAL;
Jon Hunter2b2d3522012-06-05 12:34:59 -0500491 }
492
Jon Hunterd7aba552012-07-18 20:10:12 -0500493 ret = clk_set_parent(timer->fclk, parent);
Jon Hunter2b2d3522012-06-05 12:34:59 -0500494 if (IS_ERR_VALUE(ret))
495 pr_err("%s: failed to set %s as parent\n", __func__,
496 parent_name);
497
498 clk_put(parent);
Tarun Kanti DebBarma3392cdd2011-09-20 17:00:20 +0530499
500 return ret;
Timo Teras77900a22006-06-26 16:16:12 -0700501}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700502EXPORT_SYMBOL_GPL(omap_dm_timer_set_source);
Timo Teras77900a22006-06-26 16:16:12 -0700503
Tarun Kanti DebBarmaab4eb8b2011-09-20 17:00:26 +0530504int omap_dm_timer_set_load(struct omap_dm_timer *timer, int autoreload,
Timo Teras77900a22006-06-26 16:16:12 -0700505 unsigned int load)
506{
507 u32 l;
508
Tarun Kanti DebBarmaab4eb8b2011-09-20 17:00:26 +0530509 if (unlikely(!timer))
510 return -EINVAL;
511
Tarun Kanti DebBarmab4811132011-09-20 17:00:24 +0530512 omap_dm_timer_enable(timer);
Timo Teras77900a22006-06-26 16:16:12 -0700513 l = omap_dm_timer_read_reg(timer, OMAP_TIMER_CTRL_REG);
514 if (autoreload)
515 l |= OMAP_TIMER_CTRL_AR;
516 else
517 l &= ~OMAP_TIMER_CTRL_AR;
518 omap_dm_timer_write_reg(timer, OMAP_TIMER_CTRL_REG, l);
519 omap_dm_timer_write_reg(timer, OMAP_TIMER_LOAD_REG, load);
Richard Woodruff0f0d0802008-07-03 12:24:30 +0300520
Timo Teras77900a22006-06-26 16:16:12 -0700521 omap_dm_timer_write_reg(timer, OMAP_TIMER_TRIGGER_REG, 0);
Tarun Kanti DebBarmab4811132011-09-20 17:00:24 +0530522 /* Save the context */
523 timer->context.tclr = l;
524 timer->context.tldr = load;
525 omap_dm_timer_disable(timer);
Tarun Kanti DebBarmaab4eb8b2011-09-20 17:00:26 +0530526 return 0;
Timo Teras77900a22006-06-26 16:16:12 -0700527}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700528EXPORT_SYMBOL_GPL(omap_dm_timer_set_load);
Timo Teras77900a22006-06-26 16:16:12 -0700529
Richard Woodruff3fddd092008-07-03 12:24:30 +0300530/* Optimized set_load which removes costly spin wait in timer_start */
Tarun Kanti DebBarmaab4eb8b2011-09-20 17:00:26 +0530531int omap_dm_timer_set_load_start(struct omap_dm_timer *timer, int autoreload,
Richard Woodruff3fddd092008-07-03 12:24:30 +0300532 unsigned int load)
533{
534 u32 l;
535
Tarun Kanti DebBarmaab4eb8b2011-09-20 17:00:26 +0530536 if (unlikely(!timer))
537 return -EINVAL;
538
Tarun Kanti DebBarmab4811132011-09-20 17:00:24 +0530539 omap_dm_timer_enable(timer);
540
Jon Hunter1c2d0762012-06-05 12:34:55 -0500541 if (!(timer->capability & OMAP_TIMER_ALWON)) {
Tony Lindgren6e740f92012-10-29 15:20:45 -0700542 if (timer->get_context_loss_count &&
543 timer->get_context_loss_count(&timer->pdev->dev) !=
Jon Hunter0b30ec12012-06-05 12:34:56 -0500544 timer->ctx_loss_count)
Tarun Kanti DebBarmab4811132011-09-20 17:00:24 +0530545 omap_timer_restore_context(timer);
546 }
547
Richard Woodruff3fddd092008-07-03 12:24:30 +0300548 l = omap_dm_timer_read_reg(timer, OMAP_TIMER_CTRL_REG);
Paul Walmsley64ce2902008-12-10 17:36:34 -0800549 if (autoreload) {
Richard Woodruff3fddd092008-07-03 12:24:30 +0300550 l |= OMAP_TIMER_CTRL_AR;
Paul Walmsley64ce2902008-12-10 17:36:34 -0800551 omap_dm_timer_write_reg(timer, OMAP_TIMER_LOAD_REG, load);
552 } else {
Richard Woodruff3fddd092008-07-03 12:24:30 +0300553 l &= ~OMAP_TIMER_CTRL_AR;
Paul Walmsley64ce2902008-12-10 17:36:34 -0800554 }
Richard Woodruff3fddd092008-07-03 12:24:30 +0300555 l |= OMAP_TIMER_CTRL_ST;
556
Tony Lindgrenee17f112011-09-16 15:44:20 -0700557 __omap_dm_timer_load_start(timer, l, load, timer->posted);
Tarun Kanti DebBarmab4811132011-09-20 17:00:24 +0530558
559 /* Save the context */
560 timer->context.tclr = l;
561 timer->context.tldr = load;
562 timer->context.tcrr = load;
Tarun Kanti DebBarmaab4eb8b2011-09-20 17:00:26 +0530563 return 0;
Richard Woodruff3fddd092008-07-03 12:24:30 +0300564}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700565EXPORT_SYMBOL_GPL(omap_dm_timer_set_load_start);
Richard Woodruff3fddd092008-07-03 12:24:30 +0300566
Tarun Kanti DebBarmaab4eb8b2011-09-20 17:00:26 +0530567int omap_dm_timer_set_match(struct omap_dm_timer *timer, int enable,
Timo Teras77900a22006-06-26 16:16:12 -0700568 unsigned int match)
569{
570 u32 l;
571
Tarun Kanti DebBarmaab4eb8b2011-09-20 17:00:26 +0530572 if (unlikely(!timer))
573 return -EINVAL;
574
Tarun Kanti DebBarmab4811132011-09-20 17:00:24 +0530575 omap_dm_timer_enable(timer);
Timo Teras77900a22006-06-26 16:16:12 -0700576 l = omap_dm_timer_read_reg(timer, OMAP_TIMER_CTRL_REG);
Timo Teras83379c82006-06-26 16:16:23 -0700577 if (enable)
Timo Teras77900a22006-06-26 16:16:12 -0700578 l |= OMAP_TIMER_CTRL_CE;
579 else
580 l &= ~OMAP_TIMER_CTRL_CE;
Timo Teras77900a22006-06-26 16:16:12 -0700581 omap_dm_timer_write_reg(timer, OMAP_TIMER_MATCH_REG, match);
Jon Hunter991ad162012-10-04 18:17:42 -0500582 omap_dm_timer_write_reg(timer, OMAP_TIMER_CTRL_REG, l);
Tarun Kanti DebBarmab4811132011-09-20 17:00:24 +0530583
584 /* Save the context */
585 timer->context.tclr = l;
586 timer->context.tmar = match;
587 omap_dm_timer_disable(timer);
Tarun Kanti DebBarmaab4eb8b2011-09-20 17:00:26 +0530588 return 0;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100589}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700590EXPORT_SYMBOL_GPL(omap_dm_timer_set_match);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100591
Tarun Kanti DebBarmaab4eb8b2011-09-20 17:00:26 +0530592int omap_dm_timer_set_pwm(struct omap_dm_timer *timer, int def_on,
Timo Teras77900a22006-06-26 16:16:12 -0700593 int toggle, int trigger)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100594{
Timo Teras77900a22006-06-26 16:16:12 -0700595 u32 l;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100596
Tarun Kanti DebBarmaab4eb8b2011-09-20 17:00:26 +0530597 if (unlikely(!timer))
598 return -EINVAL;
599
Tarun Kanti DebBarmab4811132011-09-20 17:00:24 +0530600 omap_dm_timer_enable(timer);
Timo Teras77900a22006-06-26 16:16:12 -0700601 l = omap_dm_timer_read_reg(timer, OMAP_TIMER_CTRL_REG);
602 l &= ~(OMAP_TIMER_CTRL_GPOCFG | OMAP_TIMER_CTRL_SCPWM |
603 OMAP_TIMER_CTRL_PT | (0x03 << 10));
604 if (def_on)
605 l |= OMAP_TIMER_CTRL_SCPWM;
606 if (toggle)
607 l |= OMAP_TIMER_CTRL_PT;
608 l |= trigger << 10;
609 omap_dm_timer_write_reg(timer, OMAP_TIMER_CTRL_REG, l);
Tarun Kanti DebBarmab4811132011-09-20 17:00:24 +0530610
611 /* Save the context */
612 timer->context.tclr = l;
613 omap_dm_timer_disable(timer);
Tarun Kanti DebBarmaab4eb8b2011-09-20 17:00:26 +0530614 return 0;
Timo Teras77900a22006-06-26 16:16:12 -0700615}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700616EXPORT_SYMBOL_GPL(omap_dm_timer_set_pwm);
Timo Teras77900a22006-06-26 16:16:12 -0700617
Tarun Kanti DebBarmaab4eb8b2011-09-20 17:00:26 +0530618int omap_dm_timer_set_prescaler(struct omap_dm_timer *timer, int prescaler)
Timo Teras77900a22006-06-26 16:16:12 -0700619{
620 u32 l;
621
Tarun Kanti DebBarmaab4eb8b2011-09-20 17:00:26 +0530622 if (unlikely(!timer))
623 return -EINVAL;
624
Tarun Kanti DebBarmab4811132011-09-20 17:00:24 +0530625 omap_dm_timer_enable(timer);
Timo Teras77900a22006-06-26 16:16:12 -0700626 l = omap_dm_timer_read_reg(timer, OMAP_TIMER_CTRL_REG);
627 l &= ~(OMAP_TIMER_CTRL_PRE | (0x07 << 2));
628 if (prescaler >= 0x00 && prescaler <= 0x07) {
629 l |= OMAP_TIMER_CTRL_PRE;
630 l |= prescaler << 2;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100631 }
Timo Teras77900a22006-06-26 16:16:12 -0700632 omap_dm_timer_write_reg(timer, OMAP_TIMER_CTRL_REG, l);
Tarun Kanti DebBarmab4811132011-09-20 17:00:24 +0530633
634 /* Save the context */
635 timer->context.tclr = l;
636 omap_dm_timer_disable(timer);
Tarun Kanti DebBarmaab4eb8b2011-09-20 17:00:26 +0530637 return 0;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100638}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700639EXPORT_SYMBOL_GPL(omap_dm_timer_set_prescaler);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100640
Tarun Kanti DebBarmaab4eb8b2011-09-20 17:00:26 +0530641int omap_dm_timer_set_int_enable(struct omap_dm_timer *timer,
Timo Teras77900a22006-06-26 16:16:12 -0700642 unsigned int value)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100643{
Tarun Kanti DebBarmaab4eb8b2011-09-20 17:00:26 +0530644 if (unlikely(!timer))
645 return -EINVAL;
646
Tarun Kanti DebBarmab4811132011-09-20 17:00:24 +0530647 omap_dm_timer_enable(timer);
Tony Lindgrenee17f112011-09-16 15:44:20 -0700648 __omap_dm_timer_int_enable(timer, value);
Tarun Kanti DebBarmab4811132011-09-20 17:00:24 +0530649
650 /* Save the context */
651 timer->context.tier = value;
652 timer->context.twer = value;
653 omap_dm_timer_disable(timer);
Tarun Kanti DebBarmaab4eb8b2011-09-20 17:00:26 +0530654 return 0;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100655}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700656EXPORT_SYMBOL_GPL(omap_dm_timer_set_int_enable);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100657
Jon Hunter4249d962012-07-13 14:03:18 -0500658/**
659 * omap_dm_timer_set_int_disable - disable timer interrupts
660 * @timer: pointer to timer handle
661 * @mask: bit mask of interrupts to be disabled
662 *
663 * Disables the specified timer interrupts for a timer.
664 */
665int omap_dm_timer_set_int_disable(struct omap_dm_timer *timer, u32 mask)
666{
667 u32 l = mask;
668
669 if (unlikely(!timer))
670 return -EINVAL;
671
672 omap_dm_timer_enable(timer);
673
674 if (timer->revision == 1)
675 l = __raw_readl(timer->irq_ena) & ~mask;
676
677 __raw_writel(l, timer->irq_dis);
678 l = omap_dm_timer_read_reg(timer, OMAP_TIMER_WAKEUP_EN_REG) & ~mask;
679 omap_dm_timer_write_reg(timer, OMAP_TIMER_WAKEUP_EN_REG, l);
680
681 /* Save the context */
682 timer->context.tier &= ~mask;
683 timer->context.twer &= ~mask;
684 omap_dm_timer_disable(timer);
685 return 0;
686}
687EXPORT_SYMBOL_GPL(omap_dm_timer_set_int_disable);
688
Tony Lindgren92105bb2005-09-07 17:20:26 +0100689unsigned int omap_dm_timer_read_status(struct omap_dm_timer *timer)
690{
Timo Terasfa4bb622006-09-25 12:41:35 +0300691 unsigned int l;
692
Tarun Kanti DebBarmaab4eb8b2011-09-20 17:00:26 +0530693 if (unlikely(!timer || pm_runtime_suspended(&timer->pdev->dev))) {
694 pr_err("%s: timer not available or enabled.\n", __func__);
Tarun Kanti DebBarmab4811132011-09-20 17:00:24 +0530695 return 0;
696 }
697
Tony Lindgrenee17f112011-09-16 15:44:20 -0700698 l = __raw_readl(timer->irq_stat);
Timo Terasfa4bb622006-09-25 12:41:35 +0300699
700 return l;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100701}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700702EXPORT_SYMBOL_GPL(omap_dm_timer_read_status);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100703
Tarun Kanti DebBarmaab4eb8b2011-09-20 17:00:26 +0530704int omap_dm_timer_write_status(struct omap_dm_timer *timer, unsigned int value)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100705{
Tarun Kanti DebBarmaab4eb8b2011-09-20 17:00:26 +0530706 if (unlikely(!timer || pm_runtime_suspended(&timer->pdev->dev)))
707 return -EINVAL;
708
Tony Lindgrenee17f112011-09-16 15:44:20 -0700709 __omap_dm_timer_write_status(timer, value);
Jon Hunter1eaff712012-10-04 17:01:14 -0500710
Tarun Kanti DebBarmaab4eb8b2011-09-20 17:00:26 +0530711 return 0;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100712}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700713EXPORT_SYMBOL_GPL(omap_dm_timer_write_status);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100714
Tony Lindgren92105bb2005-09-07 17:20:26 +0100715unsigned int omap_dm_timer_read_counter(struct omap_dm_timer *timer)
716{
Tarun Kanti DebBarmaab4eb8b2011-09-20 17:00:26 +0530717 if (unlikely(!timer || pm_runtime_suspended(&timer->pdev->dev))) {
718 pr_err("%s: timer not iavailable or enabled.\n", __func__);
Tarun Kanti DebBarmab4811132011-09-20 17:00:24 +0530719 return 0;
720 }
721
Tony Lindgrenee17f112011-09-16 15:44:20 -0700722 return __omap_dm_timer_read_counter(timer, timer->posted);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100723}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700724EXPORT_SYMBOL_GPL(omap_dm_timer_read_counter);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100725
Tarun Kanti DebBarmaab4eb8b2011-09-20 17:00:26 +0530726int omap_dm_timer_write_counter(struct omap_dm_timer *timer, unsigned int value)
Timo Teras83379c82006-06-26 16:16:23 -0700727{
Tarun Kanti DebBarmaab4eb8b2011-09-20 17:00:26 +0530728 if (unlikely(!timer || pm_runtime_suspended(&timer->pdev->dev))) {
729 pr_err("%s: timer not available or enabled.\n", __func__);
730 return -EINVAL;
Tarun Kanti DebBarmab4811132011-09-20 17:00:24 +0530731 }
732
Timo Terasfa4bb622006-09-25 12:41:35 +0300733 omap_dm_timer_write_reg(timer, OMAP_TIMER_COUNTER_REG, value);
Tarun Kanti DebBarmab4811132011-09-20 17:00:24 +0530734
735 /* Save the context */
736 timer->context.tcrr = value;
Tarun Kanti DebBarmaab4eb8b2011-09-20 17:00:26 +0530737 return 0;
Timo Teras83379c82006-06-26 16:16:23 -0700738}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700739EXPORT_SYMBOL_GPL(omap_dm_timer_write_counter);
Timo Teras83379c82006-06-26 16:16:23 -0700740
Timo Teras77900a22006-06-26 16:16:12 -0700741int omap_dm_timers_active(void)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100742{
Tarun Kanti DebBarma3392cdd2011-09-20 17:00:20 +0530743 struct omap_dm_timer *timer;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100744
Tarun Kanti DebBarma3392cdd2011-09-20 17:00:20 +0530745 list_for_each_entry(timer, &omap_timer_list, node) {
Tarun Kanti DebBarmaffe07ce2011-09-20 17:00:21 +0530746 if (!timer->reserved)
Timo Teras12583a72006-09-25 12:41:42 +0300747 continue;
748
Timo Teras77900a22006-06-26 16:16:12 -0700749 if (omap_dm_timer_read_reg(timer, OMAP_TIMER_CTRL_REG) &
Timo Terasfa4bb622006-09-25 12:41:35 +0300750 OMAP_TIMER_CTRL_ST) {
Timo Teras77900a22006-06-26 16:16:12 -0700751 return 1;
Timo Terasfa4bb622006-09-25 12:41:35 +0300752 }
Tony Lindgren92105bb2005-09-07 17:20:26 +0100753 }
Tony Lindgren92105bb2005-09-07 17:20:26 +0100754 return 0;
755}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700756EXPORT_SYMBOL_GPL(omap_dm_timers_active);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100757
Tarun Kanti DebBarmadf284722011-09-20 17:00:19 +0530758/**
759 * omap_dm_timer_probe - probe function called for every registered device
760 * @pdev: pointer to current timer platform device
761 *
762 * Called by driver framework at the end of device registration for all
763 * timer devices.
764 */
765static int __devinit omap_dm_timer_probe(struct platform_device *pdev)
766{
Tarun Kanti DebBarmadf284722011-09-20 17:00:19 +0530767 unsigned long flags;
768 struct omap_dm_timer *timer;
Tarun Kanti DebBarma74dd9ec2012-04-20 18:09:20 +0530769 struct resource *mem, *irq;
770 struct device *dev = &pdev->dev;
Tarun Kanti DebBarmadf284722011-09-20 17:00:19 +0530771 struct dmtimer_platform_data *pdata = pdev->dev.platform_data;
772
Jon Hunter9725f442012-05-14 10:41:37 -0500773 if (!pdata && !dev->of_node) {
Tarun Kanti DebBarma74dd9ec2012-04-20 18:09:20 +0530774 dev_err(dev, "%s: no platform data.\n", __func__);
Tarun Kanti DebBarmadf284722011-09-20 17:00:19 +0530775 return -ENODEV;
776 }
777
778 irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
779 if (unlikely(!irq)) {
Tarun Kanti DebBarma74dd9ec2012-04-20 18:09:20 +0530780 dev_err(dev, "%s: no IRQ resource.\n", __func__);
Tarun Kanti DebBarmadf284722011-09-20 17:00:19 +0530781 return -ENODEV;
782 }
783
784 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
785 if (unlikely(!mem)) {
Tarun Kanti DebBarma74dd9ec2012-04-20 18:09:20 +0530786 dev_err(dev, "%s: no memory resource.\n", __func__);
Tarun Kanti DebBarmadf284722011-09-20 17:00:19 +0530787 return -ENODEV;
788 }
789
Tarun Kanti DebBarma74dd9ec2012-04-20 18:09:20 +0530790 timer = devm_kzalloc(dev, sizeof(struct omap_dm_timer), GFP_KERNEL);
Tarun Kanti DebBarmadf284722011-09-20 17:00:19 +0530791 if (!timer) {
Tarun Kanti DebBarma74dd9ec2012-04-20 18:09:20 +0530792 dev_err(dev, "%s: memory alloc failed!\n", __func__);
793 return -ENOMEM;
Tarun Kanti DebBarmadf284722011-09-20 17:00:19 +0530794 }
795
Tarun Kanti DebBarma74dd9ec2012-04-20 18:09:20 +0530796 timer->io_base = devm_request_and_ioremap(dev, mem);
Tarun Kanti DebBarmadf284722011-09-20 17:00:19 +0530797 if (!timer->io_base) {
Tarun Kanti DebBarma74dd9ec2012-04-20 18:09:20 +0530798 dev_err(dev, "%s: region already claimed.\n", __func__);
799 return -ENOMEM;
Tarun Kanti DebBarmadf284722011-09-20 17:00:19 +0530800 }
801
Jon Hunter9725f442012-05-14 10:41:37 -0500802 if (dev->of_node) {
803 if (of_find_property(dev->of_node, "ti,timer-alwon", NULL))
804 timer->capability |= OMAP_TIMER_ALWON;
805 if (of_find_property(dev->of_node, "ti,timer-dsp", NULL))
806 timer->capability |= OMAP_TIMER_HAS_DSP_IRQ;
807 if (of_find_property(dev->of_node, "ti,timer-pwm", NULL))
808 timer->capability |= OMAP_TIMER_HAS_PWM;
809 if (of_find_property(dev->of_node, "ti,timer-secure", NULL))
810 timer->capability |= OMAP_TIMER_SECURE;
811 } else {
812 timer->id = pdev->id;
Jon Hunterbfd6d022012-09-27 12:47:43 -0500813 timer->errata = pdata->timer_errata;
Jon Hunter9725f442012-05-14 10:41:37 -0500814 timer->capability = pdata->timer_capability;
815 timer->reserved = omap_dm_timer_reserved_systimer(timer->id);
Tony Lindgrenf56f52e2012-11-09 14:54:17 -0800816 timer->get_context_loss_count = pdata->get_context_loss_count;
Jon Hunter9725f442012-05-14 10:41:37 -0500817 }
818
Tarun Kanti DebBarmadf284722011-09-20 17:00:19 +0530819 timer->irq = irq->start;
820 timer->pdev = pdev;
Tarun Kanti DebBarmadf284722011-09-20 17:00:19 +0530821
Tarun Kanti DebBarmaffe07ce2011-09-20 17:00:21 +0530822 /* Skip pm_runtime_enable for OMAP1 */
Jon Hunter66159752012-06-05 12:34:57 -0500823 if (!(timer->capability & OMAP_TIMER_NEEDS_RESET)) {
Tarun Kanti DebBarma74dd9ec2012-04-20 18:09:20 +0530824 pm_runtime_enable(dev);
825 pm_runtime_irq_safe(dev);
Tarun Kanti DebBarmaffe07ce2011-09-20 17:00:21 +0530826 }
827
Tony Lindgren0dad9fa2011-09-21 16:38:51 -0700828 if (!timer->reserved) {
Tarun Kanti DebBarma74dd9ec2012-04-20 18:09:20 +0530829 pm_runtime_get_sync(dev);
Tony Lindgren0dad9fa2011-09-21 16:38:51 -0700830 __omap_dm_timer_init_regs(timer);
Tarun Kanti DebBarma74dd9ec2012-04-20 18:09:20 +0530831 pm_runtime_put(dev);
Tony Lindgren0dad9fa2011-09-21 16:38:51 -0700832 }
833
Tarun Kanti DebBarmadf284722011-09-20 17:00:19 +0530834 /* add the timer element to the list */
835 spin_lock_irqsave(&dm_timer_lock, flags);
836 list_add_tail(&timer->node, &omap_timer_list);
837 spin_unlock_irqrestore(&dm_timer_lock, flags);
838
Tarun Kanti DebBarma74dd9ec2012-04-20 18:09:20 +0530839 dev_dbg(dev, "Device Probed.\n");
Tarun Kanti DebBarmadf284722011-09-20 17:00:19 +0530840
841 return 0;
Tarun Kanti DebBarmadf284722011-09-20 17:00:19 +0530842}
843
844/**
845 * omap_dm_timer_remove - cleanup a registered timer device
846 * @pdev: pointer to current timer platform device
847 *
848 * Called by driver framework whenever a timer device is unregistered.
849 * In addition to freeing platform resources it also deletes the timer
850 * entry from the local list.
851 */
852static int __devexit omap_dm_timer_remove(struct platform_device *pdev)
853{
854 struct omap_dm_timer *timer;
855 unsigned long flags;
856 int ret = -EINVAL;
857
858 spin_lock_irqsave(&dm_timer_lock, flags);
859 list_for_each_entry(timer, &omap_timer_list, node)
Jon Hunter9725f442012-05-14 10:41:37 -0500860 if (!strcmp(dev_name(&timer->pdev->dev),
861 dev_name(&pdev->dev))) {
Tarun Kanti DebBarmadf284722011-09-20 17:00:19 +0530862 list_del(&timer->node);
Tarun Kanti DebBarmadf284722011-09-20 17:00:19 +0530863 ret = 0;
864 break;
865 }
866 spin_unlock_irqrestore(&dm_timer_lock, flags);
867
868 return ret;
869}
870
Jon Hunter9725f442012-05-14 10:41:37 -0500871static const struct of_device_id omap_timer_match[] = {
872 { .compatible = "ti,omap2-timer", },
873 {},
874};
875MODULE_DEVICE_TABLE(of, omap_timer_match);
876
Tarun Kanti DebBarmadf284722011-09-20 17:00:19 +0530877static struct platform_driver omap_dm_timer_driver = {
878 .probe = omap_dm_timer_probe,
Arnd Bergmann4c23c8d2011-10-01 18:42:47 +0200879 .remove = __devexit_p(omap_dm_timer_remove),
Tarun Kanti DebBarmadf284722011-09-20 17:00:19 +0530880 .driver = {
881 .name = "omap_timer",
Jon Hunter9725f442012-05-14 10:41:37 -0500882 .of_match_table = of_match_ptr(omap_timer_match),
Tarun Kanti DebBarmadf284722011-09-20 17:00:19 +0530883 },
884};
885
886static int __init omap_dm_timer_driver_init(void)
887{
888 return platform_driver_register(&omap_dm_timer_driver);
889}
890
891static void __exit omap_dm_timer_driver_exit(void)
892{
893 platform_driver_unregister(&omap_dm_timer_driver);
894}
895
896early_platform_init("earlytimer", &omap_dm_timer_driver);
897module_init(omap_dm_timer_driver_init);
898module_exit(omap_dm_timer_driver_exit);
899
900MODULE_DESCRIPTION("OMAP Dual-Mode Timer Driver");
901MODULE_LICENSE("GPL");
902MODULE_ALIAS("platform:" DRIVER_NAME);
903MODULE_AUTHOR("Texas Instruments Inc");