Duy Truong | f3ac7b3 | 2013-02-13 01:07:28 -0800 | [diff] [blame] | 1 | /* Copyright (c) 2012, The Linux Foundation. All rights reserved. |
Deepa Dinamani | 1cc9764 | 2012-05-09 11:45:38 -0700 | [diff] [blame] | 2 | |
| 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 Truong | f3ac7b3 | 2013-02-13 01:07:28 -0800 | [diff] [blame] | 12 | * * Neither the name of The Linux Foundation nor the names of its |
Deepa Dinamani | 1cc9764 | 2012-05-09 11:45:38 -0700 | [diff] [blame] | 13 | * 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 | |
| 37 | static platform_timer_callback timer_callback; |
| 38 | static void *timer_arg; |
| 39 | static time_t timer_interval; |
| 40 | /* time in ms from start of LK. */ |
| 41 | static volatile uint32_t current_time; |
| 42 | static uint32_t tick_count; |
| 43 | |
| 44 | extern void isb(); |
Deepa Dinamani | d642b80 | 2012-05-16 10:49:01 -0700 | [diff] [blame] | 45 | static void qtimer_enable(); |
Deepa Dinamani | 1cc9764 | 2012-05-09 11:45:38 -0700 | [diff] [blame] | 46 | |
| 47 | static 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 | */ |
| 65 | void qtimer_set_physical_timer(time_t msecs_interval, |
| 66 | platform_timer_callback tmr_callback, |
| 67 | void *tmr_arg) |
| 68 | { |
Deepa Dinamani | 1cc9764 | 2012-05-09 11:45:38 -0700 | [diff] [blame] | 69 | /* 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 Dinamani | 1cc9764 | 2012-05-09 11:45:38 -0700 | [diff] [blame] | 75 | /* Set Physical Down Counter */ |
| 76 | __asm__ volatile("mcr p15, 0, %0, c14, c2, 0" : :"r" (tick_count)); |
Deepa Dinamani | 1cc9764 | 2012-05-09 11:45:38 -0700 | [diff] [blame] | 77 | isb(); |
| 78 | |
Deepa Dinamani | d642b80 | 2012-05-16 10:49:01 -0700 | [diff] [blame] | 79 | qtimer_enable(); |
| 80 | |
Deepa Dinamani | 1cc9764 | 2012-05-09 11:45:38 -0700 | [diff] [blame] | 81 | /* Register for timer interrupts */ |
| 82 | |
Deepa Dinamani | 81eddd5 | 2012-05-31 11:18:50 -0700 | [diff] [blame] | 83 | register_int_handler(INT_QTMR_NON_SECURE_PHY_TIMER_EXP, qtimer_irq, 0); |
| 84 | unmask_interrupt(INT_QTMR_NON_SECURE_PHY_TIMER_EXP); |
Deepa Dinamani | 1cc9764 | 2012-05-09 11:45:38 -0700 | [diff] [blame] | 85 | |
| 86 | return; |
| 87 | |
| 88 | } |
| 89 | |
Deepa Dinamani | d642b80 | 2012-05-16 10:49:01 -0700 | [diff] [blame] | 90 | static 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 | |
| 105 | void qtimer_disable() |
Deepa Dinamani | 1cc9764 | 2012-05-09 11:45:38 -0700 | [diff] [blame] | 106 | { |
| 107 | uint32_t ctrl; |
| 108 | |
Deepa Dinamani | 81eddd5 | 2012-05-31 11:18:50 -0700 | [diff] [blame] | 109 | mask_interrupt(INT_QTMR_NON_SECURE_PHY_TIMER_EXP); |
Deepa Dinamani | 1cc9764 | 2012-05-09 11:45:38 -0700 | [diff] [blame] | 110 | |
Deepa Dinamani | d642b80 | 2012-05-16 10:49:01 -0700 | [diff] [blame] | 111 | /* read ctrl value */ |
| 112 | __asm__ volatile("mrc p15, 0, %0, c14, c2, 1" : :"r" (ctrl)); |
Deepa Dinamani | 1cc9764 | 2012-05-09 11:45:38 -0700 | [diff] [blame] | 113 | |
Deepa Dinamani | d642b80 | 2012-05-16 10:49:01 -0700 | [diff] [blame] | 114 | ctrl &= ~QTMR_TIMER_CTRL_ENABLE; |
| 115 | ctrl |= QTMR_TIMER_CTRL_INT_MASK; |
| 116 | |
| 117 | /* write ctrl value */ |
Deepa Dinamani | 1cc9764 | 2012-05-09 11:45:38 -0700 | [diff] [blame] | 118 | __asm__ volatile("mcr p15, 0, %0, c14, c2, 1" : :"r" (ctrl)); |
| 119 | |
| 120 | isb(); |
Deepa Dinamani | 1cc9764 | 2012-05-09 11:45:38 -0700 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | /* Function to return the frequency of the timer */ |
| 124 | uint32_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 | |
| 135 | inline __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 | |
| 145 | uint32_t qtimer_current_time() |
| 146 | { |
| 147 | return current_time; |
| 148 | } |