blob: bafdd3e7ea9ea93a4fc7e37c415891fd027d5c83 [file] [log] [blame]
Brian Swetland2500aa12009-01-01 04:33:55 -08001/*
2 * Copyright (c) 2008, Google Inc.
3 * All rights reserved.
4 *
Duy Truongf3ac7b32013-02-13 01:07:28 -08005 * Copyright (c) 2009-2011, The Linux Foundation. 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
Amol Jadiaeda4e62011-07-19 18:07:29 -070044#define GPT_ENABLE_CLR_ON_MATCH_EN 2
45#define GPT_ENABLE_EN 1
46#define DGT_ENABLE_CLR_ON_MATCH_EN 2
47#define DGT_ENABLE_EN 1
Ajay Dudani232ce812009-12-02 00:14:11 -080048
Amol Jadica4f4c92011-01-13 20:19:34 -080049#define SPSS_TIMER_STATUS_DGT_EN (1 << 0)
Ajay Dudani232ce812009-12-02 00:14:11 -080050
Brian Swetland2500aa12009-01-01 04:33:55 -080051static platform_timer_callback timer_callback;
52static void *timer_arg;
53static time_t timer_interval;
54
55static volatile uint32_t ticks;
56
57static enum handler_return timer_irq(void *arg)
58{
59 ticks += timer_interval;
60 return timer_callback(timer_arg, ticks);
61}
62
Ajay Dudanib01e5062011-12-03 23:23:42 -080063status_t
64platform_set_periodic_timer(platform_timer_callback callback,
65 void *arg, time_t interval)
Brian Swetland2500aa12009-01-01 04:33:55 -080066{
Ajay Dudanib01e5062011-12-03 23:23:42 -080067 uint32_t tick_count = interval * platform_tick_rate() / 1000;
Amol Jadiaeda4e62011-07-19 18:07:29 -070068
Brian Swetland2500aa12009-01-01 04:33:55 -080069 enter_critical_section();
70
71 timer_callback = callback;
72 timer_arg = arg;
73 timer_interval = interval;
74
Amol Jadiaeda4e62011-07-19 18:07:29 -070075 writel(tick_count, DGT_MATCH_VAL);
Brian Swetland2500aa12009-01-01 04:33:55 -080076 writel(0, DGT_CLEAR);
77 writel(DGT_ENABLE_EN | DGT_ENABLE_CLR_ON_MATCH_EN, DGT_ENABLE);
Amol Jadiaeda4e62011-07-19 18:07:29 -070078
Brian Swetland2500aa12009-01-01 04:33:55 -080079 register_int_handler(INT_DEBUG_TIMER_EXP, timer_irq, 0);
80 unmask_interrupt(INT_DEBUG_TIMER_EXP);
81
82 exit_critical_section();
83 return 0;
84}
85
Brian Swetland2500aa12009-01-01 04:33:55 -080086time_t current_time(void)
87{
88 return ticks;
89}
90
Brian Swetland0d7b1b82009-01-21 21:03:28 -080091static void wait_for_timer_op(void)
92{
Ajay Dudanib01e5062011-12-03 23:23:42 -080093 while (readl(SPSS_TIMER_STATUS) & SPSS_TIMER_STATUS_DGT_EN) ;
Brian Swetland0d7b1b82009-01-21 21:03:28 -080094}
95
96void platform_uninit_timer(void)
97{
98 writel(0, DGT_ENABLE);
99 wait_for_timer_op();
100 writel(0, DGT_CLEAR);
101 wait_for_timer_op();
102}
Chandan Uddaraju852cd2c2009-12-17 14:28:28 -0800103
104void mdelay(unsigned msecs)
105{
Ajay Dudanib01e5062011-12-03 23:23:42 -0800106 msecs *= 33;
Chandan Uddaraju852cd2c2009-12-17 14:28:28 -0800107
Ajay Dudanib01e5062011-12-03 23:23:42 -0800108 writel(0, GPT_CLEAR);
109 writel(0, GPT_ENABLE);
110 while (readl(GPT_COUNT_VAL) != 0) ;
Chandan Uddaraju852cd2c2009-12-17 14:28:28 -0800111
Ajay Dudanib01e5062011-12-03 23:23:42 -0800112 writel(GPT_ENABLE_EN, GPT_ENABLE);
113 while (readl(GPT_COUNT_VAL) < msecs) ;
Chandan Uddaraju852cd2c2009-12-17 14:28:28 -0800114
Ajay Dudanib01e5062011-12-03 23:23:42 -0800115 writel(0, GPT_ENABLE);
116 writel(0, GPT_CLEAR);
Chandan Uddaraju852cd2c2009-12-17 14:28:28 -0800117}
Chandan Uddaraju61e6d7c2010-07-20 17:57:06 -0700118
119void udelay(unsigned usecs)
120{
Ajay Dudanib01e5062011-12-03 23:23:42 -0800121 usecs = (usecs * 33 + 1000 - 33) / 1000;
Chandan Uddaraju61e6d7c2010-07-20 17:57:06 -0700122
Ajay Dudanib01e5062011-12-03 23:23:42 -0800123 writel(0, GPT_CLEAR);
124 writel(0, GPT_ENABLE);
125 while (readl(GPT_COUNT_VAL) != 0) ;
Chandan Uddaraju61e6d7c2010-07-20 17:57:06 -0700126
Ajay Dudanib01e5062011-12-03 23:23:42 -0800127 writel(GPT_ENABLE_EN, GPT_ENABLE);
128 while (readl(GPT_COUNT_VAL) < usecs) ;
Chandan Uddaraju61e6d7c2010-07-20 17:57:06 -0700129
Ajay Dudanib01e5062011-12-03 23:23:42 -0800130 writel(0, GPT_ENABLE);
131 writel(0, GPT_CLEAR);
Chandan Uddaraju61e6d7c2010-07-20 17:57:06 -0700132}
Channagoud Kadabiad75f982011-08-05 15:55:03 +0530133
134/* Return current time in micro seconds */
135bigtime_t current_time_hires(void)
136{
137 return ticks * 1000ULL;
138}