blob: 47583ddd96eee851d7cf013d5aa18fe909e8146f [file] [log] [blame]
Brian Swetland2500aa12009-01-01 04:33:55 -08001/*
2 * Copyright (c) 2008, Google Inc.
3 * All rights reserved.
4 *
Amol Jadicd43ea02011-02-15 20:56:04 -08005 * Copyright (c) 2009-2011, Code Aurora Forum. All rights reserved.
Shashank Mittal23b8f422010-04-16 19:27:21 -07006 *
Brian Swetland2500aa12009-01-01 04:33:55 -08007 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the
15 * distribution.
16 * * Neither the name of Google, Inc. nor the names of its contributors
17 * may be used to endorse or promote products derived from this
18 * software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
30 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34#include <debug.h>
35#include <reg.h>
36#include <sys/types.h>
37
38#include <platform/timer.h>
39#include <platform/irqs.h>
40#include <platform/iomap.h>
41#include <platform/interrupts.h>
42#include <kernel/thread.h>
43
Greg Griscod2471ef2011-07-14 13:00:42 -070044#if PLATFORM_MSM7X30 || PLATFORM_MSM8X60 || PLATFORM_MSM8960
45
46#define MSM_DGT_BASE (MSM_TMR_BASE + 0x24)
47#define GPT_REG(off) (MSM_GPT_BASE + (off))
48#define DGT_REG(off) (MSM_DGT_BASE + (off))
49#define SPSS_TIMER_STATUS (MSM_TMR_BASE + 0x88)
50#define SPSS_TIMER_STATUS_DGT_EN (1 << 0)
51
52#define GPT_MATCH_VAL GPT_REG(0x0000)
53#define GPT_COUNT_VAL GPT_REG(0x0004)
54#define GPT_ENABLE GPT_REG(0x0008)
55#define GPT_ENABLE_CLR_ON_MATCH_EN 2
56#define GPT_ENABLE_EN 1
57#define GPT_CLEAR GPT_REG(0x000C)
58
59#define DGT_MATCH_VAL DGT_REG(0x0000)
60#define DGT_COUNT_VAL DGT_REG(0x0004)
61#define DGT_ENABLE DGT_REG(0x0008)
62#define DGT_ENABLE_CLR_ON_MATCH_EN 2
63#define DGT_ENABLE_EN 1
64#define DGT_CLEAR DGT_REG(0x000C)
65#define DGT_CLK_CTL DGT_REG(0x0010)
66
67#define HW_REVISION_NUMBER 0xABC00270
68
69
70#else
71#define GPT_REG(off) (MSM_GPT_BASE + (off))
72
73#define GPT_MATCH_VAL GPT_REG(0x0000)
74#define GPT_COUNT_VAL GPT_REG(0x0004)
75#define GPT_ENABLE GPT_REG(0x0008)
Amol Jadiaeda4e62011-07-19 18:07:29 -070076#define GPT_ENABLE_CLR_ON_MATCH_EN 2
77#define GPT_ENABLE_EN 1
78#define DGT_ENABLE_CLR_ON_MATCH_EN 2
79#define DGT_ENABLE_EN 1
Ajay Dudani232ce812009-12-02 00:14:11 -080080
Amol Jadica4f4c92011-01-13 20:19:34 -080081#define SPSS_TIMER_STATUS_DGT_EN (1 << 0)
Ajay Dudani232ce812009-12-02 00:14:11 -080082
Greg Griscod2471ef2011-07-14 13:00:42 -070083#endif
84
Brian Swetland2500aa12009-01-01 04:33:55 -080085static platform_timer_callback timer_callback;
86static void *timer_arg;
87static time_t timer_interval;
88
89static volatile uint32_t ticks;
90
91static enum handler_return timer_irq(void *arg)
92{
93 ticks += timer_interval;
94 return timer_callback(timer_arg, ticks);
95}
96
97status_t platform_set_periodic_timer(
98 platform_timer_callback callback,
99 void *arg, time_t interval)
100{
Amol Jadiaeda4e62011-07-19 18:07:29 -0700101 uint32_t tick_count = interval * platform_tick_rate()/1000;
102
Brian Swetland2500aa12009-01-01 04:33:55 -0800103 enter_critical_section();
104
105 timer_callback = callback;
106 timer_arg = arg;
107 timer_interval = interval;
108
Amol Jadiaeda4e62011-07-19 18:07:29 -0700109 writel(tick_count, DGT_MATCH_VAL);
Brian Swetland2500aa12009-01-01 04:33:55 -0800110 writel(0, DGT_CLEAR);
111 writel(DGT_ENABLE_EN | DGT_ENABLE_CLR_ON_MATCH_EN, DGT_ENABLE);
Amol Jadiaeda4e62011-07-19 18:07:29 -0700112
Brian Swetland2500aa12009-01-01 04:33:55 -0800113 register_int_handler(INT_DEBUG_TIMER_EXP, timer_irq, 0);
114 unmask_interrupt(INT_DEBUG_TIMER_EXP);
115
116 exit_critical_section();
117 return 0;
118}
119
120
121time_t current_time(void)
122{
123 return ticks;
124}
125
Brian Swetland0d7b1b82009-01-21 21:03:28 -0800126static void wait_for_timer_op(void)
127{
Amol Jadica4f4c92011-01-13 20:19:34 -0800128 while( readl(SPSS_TIMER_STATUS) & SPSS_TIMER_STATUS_DGT_EN );
Brian Swetland0d7b1b82009-01-21 21:03:28 -0800129}
130
131void platform_uninit_timer(void)
132{
133 writel(0, DGT_ENABLE);
134 wait_for_timer_op();
135 writel(0, DGT_CLEAR);
136 wait_for_timer_op();
137}
Chandan Uddaraju852cd2c2009-12-17 14:28:28 -0800138
139void mdelay(unsigned msecs)
140{
141 msecs *= 33;
142
143 writel(0, GPT_CLEAR);
144 writel(0, GPT_ENABLE);
145 while(readl(GPT_COUNT_VAL) != 0) ;
146
147 writel(GPT_ENABLE_EN, GPT_ENABLE);
148 while(readl(GPT_COUNT_VAL) < msecs) ;
149
150 writel(0, GPT_ENABLE);
151 writel(0, GPT_CLEAR);
152}
Chandan Uddaraju61e6d7c2010-07-20 17:57:06 -0700153
154void udelay(unsigned usecs)
155{
156 usecs = (usecs * 33 + 1000 - 33) / 1000;
157
158 writel(0, GPT_CLEAR);
159 writel(0, GPT_ENABLE);
160 while(readl(GPT_COUNT_VAL) != 0);
161
162 writel(GPT_ENABLE_EN, GPT_ENABLE);
163 while(readl(GPT_COUNT_VAL) < usecs);
164
165 writel(0, GPT_ENABLE);
166 writel(0, GPT_CLEAR);
167}
Channagoud Kadabiad75f982011-08-05 15:55:03 +0530168
169/* Return current time in micro seconds */
170bigtime_t current_time_hires(void)
171{
172 return ticks * 1000ULL;
173}