blob: 4e56637ede33dba711ae96db71f94aed9a94242b [file] [log] [blame]
Duy Truongf3ac7b32013-02-13 01:07:28 -08001/* Copyright (c) 2012, The Linux Foundation. All rights reserved.
Deepa Dinamani1cc97642012-05-09 11:45:38 -07002
3 * Redistribution and use in source and binary forms, with or without
4 * modification, are permitted provided that the following conditions are
5 * met:
6 * * Redistributions of source code must retain the above copyright
7 * notice, this list of conditions and the following disclaimer.
8 * * Redistributions in binary form must reproduce the above
9 * copyright notice, this list of conditions and the following
10 * disclaimer in the documentation and/or other materials provided
11 * with the distribution.
Duy Truongf3ac7b32013-02-13 01:07:28 -080012 * * Neither the name of The Linux Foundation nor the names of its
Deepa Dinamani1cc97642012-05-09 11:45:38 -070013 * contributors may be used to endorse or promote products derived
14 * from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
23 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
25 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
26 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#include <debug.h>
30#include <reg.h>
31#include <compiler.h>
32#include <qtimer.h>
33#include <platform/irqs.h>
34#include <platform/iomap.h>
35#include <platform/interrupts.h>
36
37static platform_timer_callback timer_callback;
38static void *timer_arg;
39static time_t timer_interval;
40/* time in ms from start of LK. */
41static volatile uint32_t current_time;
42static uint32_t tick_count;
43
44extern void isb();
Deepa Dinamanid642b802012-05-16 10:49:01 -070045static void qtimer_enable();
Deepa Dinamani1cc97642012-05-09 11:45:38 -070046
47static enum handler_return qtimer_irq(void *arg)
48{
49 current_time += timer_interval;
50
51 /* Program the down counter again to get
52 * an interrupt after timer_interval msecs
53 */
54
55 __asm__ volatile("mcr p15, 0, %0, c14, c2, 0" : :"r" (tick_count));
56
57 isb();
58
59 return timer_callback(timer_arg, current_time);
60}
61
62/* Programs the Physical Secure Down counter timer.
63 * interval : Counter ticks till expiry interrupt is fired.
64 */
65void qtimer_set_physical_timer(time_t msecs_interval,
66 platform_timer_callback tmr_callback,
67 void *tmr_arg)
68{
Deepa Dinamani1cc97642012-05-09 11:45:38 -070069 /* Save the timer interval and call back data*/
70 tick_count = msecs_interval * qtimer_tick_rate() / 1000;;
71 timer_interval = msecs_interval;
72 timer_arg = tmr_arg;
73 timer_callback = tmr_callback;
74
Deepa Dinamani1cc97642012-05-09 11:45:38 -070075 /* Set Physical Down Counter */
76 __asm__ volatile("mcr p15, 0, %0, c14, c2, 0" : :"r" (tick_count));
Deepa Dinamani1cc97642012-05-09 11:45:38 -070077 isb();
78
Deepa Dinamanid642b802012-05-16 10:49:01 -070079 qtimer_enable();
80
Deepa Dinamani1cc97642012-05-09 11:45:38 -070081 /* Register for timer interrupts */
82
Deepa Dinamani81eddd52012-05-31 11:18:50 -070083 register_int_handler(INT_QTMR_NON_SECURE_PHY_TIMER_EXP, qtimer_irq, 0);
84 unmask_interrupt(INT_QTMR_NON_SECURE_PHY_TIMER_EXP);
Deepa Dinamani1cc97642012-05-09 11:45:38 -070085
86 return;
87
88}
89
Deepa Dinamanid642b802012-05-16 10:49:01 -070090static void qtimer_enable()
91{
92 uint32_t ctrl;
93
94 /* read ctrl value */
95 __asm__ volatile("mrc p15, 0, %0, c14, c2, 1" : :"r" (ctrl));
96
97 ctrl |= QTMR_TIMER_CTRL_ENABLE;
98 ctrl &= ~QTMR_TIMER_CTRL_INT_MASK;
99
100 /* write ctrl value */
101 __asm__ volatile("mcr p15, 0, %0, c14, c2, 1" : :"r" (ctrl));
102 isb();
103}
104
105void qtimer_disable()
Deepa Dinamani1cc97642012-05-09 11:45:38 -0700106{
107 uint32_t ctrl;
108
Deepa Dinamani81eddd52012-05-31 11:18:50 -0700109 mask_interrupt(INT_QTMR_NON_SECURE_PHY_TIMER_EXP);
Deepa Dinamani1cc97642012-05-09 11:45:38 -0700110
Deepa Dinamanid642b802012-05-16 10:49:01 -0700111 /* read ctrl value */
112 __asm__ volatile("mrc p15, 0, %0, c14, c2, 1" : :"r" (ctrl));
Deepa Dinamani1cc97642012-05-09 11:45:38 -0700113
Deepa Dinamanid642b802012-05-16 10:49:01 -0700114 ctrl &= ~QTMR_TIMER_CTRL_ENABLE;
115 ctrl |= QTMR_TIMER_CTRL_INT_MASK;
116
117 /* write ctrl value */
Deepa Dinamani1cc97642012-05-09 11:45:38 -0700118 __asm__ volatile("mcr p15, 0, %0, c14, c2, 1" : :"r" (ctrl));
119
120 isb();
Deepa Dinamani1cc97642012-05-09 11:45:38 -0700121}
122
123/* Function to return the frequency of the timer */
124uint32_t qtimer_get_frequency()
125{
126 uint32_t freq;
127
128 /* Read the Global counter frequency */
129 __asm__ volatile("mrc p15, 0, %0, c14, c0, 0" : "=r" (freq));
130
131 return freq;
132
133}
134
135inline __ALWAYS_INLINE uint64_t qtimer_get_phy_timer_cnt()
136{
137 uint32_t phy_cnt_lo;
138 uint32_t phy_cnt_hi;
139
140 __asm__ volatile("mrrc p15,0,%0,%1, c14":
141 "=r"(phy_cnt_lo),"=r"(phy_cnt_hi));
142 return ((uint64_t)phy_cnt_hi << 32) | phy_cnt_lo;
143}
144
145uint32_t qtimer_current_time()
146{
147 return current_time;
148}