Duy Truong | f3ac7b3 | 2013-02-13 01:07:28 -0800 | [diff] [blame] | 1 | /* Copyright (c) 2012, The Linux Foundation. All rights reserved. |
Deepa Dinamani | d642b80 | 2012-05-16 10:49:01 -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 | d642b80 | 2012-05-16 10:49:01 -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> |
Channagoud Kadabi | 6d6223c | 2013-08-05 15:27:55 -0700 | [diff] [blame] | 33 | #include <arch/defines.h> |
Deepa Dinamani | d642b80 | 2012-05-16 10:49:01 -0700 | [diff] [blame] | 34 | #include <platform/irqs.h> |
| 35 | #include <platform/iomap.h> |
| 36 | #include <platform/interrupts.h> |
| 37 | #include <qtimer_mmap_hw.h> |
| 38 | |
| 39 | static platform_timer_callback timer_callback; |
| 40 | static void *timer_arg; |
| 41 | static time_t timer_interval; |
| 42 | /* time in ms from start of LK. */ |
| 43 | static volatile uint32_t current_time; |
| 44 | static uint32_t tick_count; |
| 45 | |
Deepa Dinamani | d642b80 | 2012-05-16 10:49:01 -0700 | [diff] [blame] | 46 | static void qtimer_enable(); |
| 47 | |
| 48 | static enum handler_return qtimer_irq(void *arg) |
| 49 | { |
| 50 | current_time += timer_interval; |
| 51 | |
| 52 | /* Program the down counter again to get |
| 53 | * an interrupt after timer_interval msecs |
| 54 | */ |
Deepa Dinamani | d642b80 | 2012-05-16 10:49:01 -0700 | [diff] [blame] | 55 | writel(tick_count, QTMR_V1_CNTP_TVAL); |
Deepa Dinamani | d642b80 | 2012-05-16 10:49:01 -0700 | [diff] [blame] | 56 | dsb(); |
| 57 | |
| 58 | return timer_callback(timer_arg, current_time); |
| 59 | } |
| 60 | |
Amol Jadi | 42d7b5a | 2012-05-04 14:50:32 -0700 | [diff] [blame] | 61 | |
Deepa Dinamani | d642b80 | 2012-05-16 10:49:01 -0700 | [diff] [blame] | 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, |
Amol Jadi | 42d7b5a | 2012-05-04 14:50:32 -0700 | [diff] [blame] | 66 | platform_timer_callback tmr_callback, |
| 67 | void *tmr_arg) |
Deepa Dinamani | d642b80 | 2012-05-16 10:49:01 -0700 | [diff] [blame] | 68 | { |
Deepa Dinamani | d642b80 | 2012-05-16 10:49:01 -0700 | [diff] [blame] | 69 | qtimer_disable(); |
| 70 | |
| 71 | /* Save the timer interval and call back data*/ |
| 72 | tick_count = msecs_interval * qtimer_tick_rate() / 1000;; |
| 73 | timer_interval = msecs_interval; |
| 74 | timer_arg = tmr_arg; |
| 75 | timer_callback = tmr_callback; |
| 76 | |
| 77 | /* Set Physical Down Counter */ |
| 78 | writel(tick_count, QTMR_V1_CNTP_TVAL); |
| 79 | dsb(); |
| 80 | |
Amol Jadi | 42d7b5a | 2012-05-04 14:50:32 -0700 | [diff] [blame] | 81 | register_int_handler(INT_QTMR_FRM_0_PHYSICAL_TIMER_EXP, qtimer_irq, 0); |
Deepa Dinamani | d642b80 | 2012-05-16 10:49:01 -0700 | [diff] [blame] | 82 | |
Amol Jadi | 42d7b5a | 2012-05-04 14:50:32 -0700 | [diff] [blame] | 83 | unmask_interrupt(INT_QTMR_FRM_0_PHYSICAL_TIMER_EXP); |
| 84 | |
| 85 | qtimer_enable(); |
Deepa Dinamani | d642b80 | 2012-05-16 10:49:01 -0700 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | |
| 89 | /* Function to return the frequency of the timer */ |
| 90 | uint32_t qtimer_get_frequency() |
| 91 | { |
| 92 | uint32_t freq; |
| 93 | |
| 94 | /* Read the Global counter frequency */ |
Amol Jadi | 42d7b5a | 2012-05-04 14:50:32 -0700 | [diff] [blame] | 95 | /* freq = readl(QTMR_V1_CNTFRQ); */ |
| 96 | /* TODO: remove this when bootchaint sets up the frequency. */ |
| 97 | freq = 19200000; |
Deepa Dinamani | d642b80 | 2012-05-16 10:49:01 -0700 | [diff] [blame] | 98 | |
| 99 | return freq; |
Deepa Dinamani | d642b80 | 2012-05-16 10:49:01 -0700 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | static void qtimer_enable() |
| 103 | { |
| 104 | uint32_t ctrl; |
| 105 | |
| 106 | ctrl = readl(QTMR_V1_CNTP_CTL); |
| 107 | |
| 108 | /* Program CTRL Register */ |
| 109 | ctrl |= QTMR_TIMER_CTRL_ENABLE; |
| 110 | ctrl &= ~QTMR_TIMER_CTRL_INT_MASK; |
| 111 | |
| 112 | writel(ctrl, QTMR_V1_CNTP_CTL); |
Deepa Dinamani | d642b80 | 2012-05-16 10:49:01 -0700 | [diff] [blame] | 113 | dsb(); |
| 114 | } |
| 115 | |
| 116 | void qtimer_disable() |
| 117 | { |
| 118 | uint32_t ctrl; |
| 119 | |
Deepa Dinamani | d642b80 | 2012-05-16 10:49:01 -0700 | [diff] [blame] | 120 | ctrl = readl(QTMR_V1_CNTP_CTL); |
| 121 | |
| 122 | /* program cntrl register */ |
| 123 | ctrl &= ~QTMR_TIMER_CTRL_ENABLE; |
| 124 | ctrl |= QTMR_TIMER_CTRL_INT_MASK; |
| 125 | |
| 126 | writel(ctrl, QTMR_V1_CNTP_CTL); |
Deepa Dinamani | d642b80 | 2012-05-16 10:49:01 -0700 | [diff] [blame] | 127 | dsb(); |
| 128 | } |
| 129 | |
| 130 | inline __ALWAYS_INLINE uint64_t qtimer_get_phy_timer_cnt() |
| 131 | { |
| 132 | uint32_t phy_cnt_lo; |
Deepa Dinamani | 1f01f19 | 2012-08-10 16:04:10 -0700 | [diff] [blame] | 133 | uint32_t phy_cnt_hi_1; |
| 134 | uint32_t phy_cnt_hi_2; |
Deepa Dinamani | d642b80 | 2012-05-16 10:49:01 -0700 | [diff] [blame] | 135 | |
Deepa Dinamani | 1f01f19 | 2012-08-10 16:04:10 -0700 | [diff] [blame] | 136 | do { |
| 137 | phy_cnt_hi_1 = readl(QTMR_V1_CNTPCT_HI); |
| 138 | phy_cnt_lo = readl(QTMR_V1_CNTPCT_LO); |
| 139 | phy_cnt_hi_2 = readl(QTMR_V1_CNTPCT_HI); |
| 140 | } while (phy_cnt_hi_1 != phy_cnt_hi_2); |
Deepa Dinamani | d642b80 | 2012-05-16 10:49:01 -0700 | [diff] [blame] | 141 | |
Deepa Dinamani | 6da49a8 | 2012-08-15 10:54:45 -0700 | [diff] [blame] | 142 | return ((uint64_t)phy_cnt_hi_1 << 32) | phy_cnt_lo; |
Deepa Dinamani | d642b80 | 2012-05-16 10:49:01 -0700 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | uint32_t qtimer_current_time() |
| 146 | { |
| 147 | return current_time; |
| 148 | } |