Deepa Dinamani | b2a8a8b | 2013-07-18 14:13:08 -0700 | [diff] [blame] | 1 | /* Copyright (c) 2012-2013, The Linux Foundation. All rights reserved. |
Deepa Dinamani | dc036ae | 2011-12-20 18:19:52 -0800 | [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 | dc036ae | 2011-12-20 18:19:52 -0800 | [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> |
Deepa Dinamani | 1cc9764 | 2012-05-09 11:45:38 -0700 | [diff] [blame] | 31 | #include <compiler.h> |
| 32 | #include <qtimer.h> |
Deepa Dinamani | dc036ae | 2011-12-20 18:19:52 -0800 | [diff] [blame] | 33 | #include <kernel/thread.h> |
| 34 | |
Deepa Dinamani | 1cc9764 | 2012-05-09 11:45:38 -0700 | [diff] [blame] | 35 | static uint32_t ticks_per_sec; |
Deepa Dinamani | dc036ae | 2011-12-20 18:19:52 -0800 | [diff] [blame] | 36 | |
| 37 | status_t platform_set_periodic_timer(platform_timer_callback callback, |
| 38 | void *arg, time_t interval) |
| 39 | { |
Deepa Dinamani | dc036ae | 2011-12-20 18:19:52 -0800 | [diff] [blame] | 40 | |
| 41 | enter_critical_section(); |
| 42 | |
Deepa Dinamani | 1cc9764 | 2012-05-09 11:45:38 -0700 | [diff] [blame] | 43 | qtimer_set_physical_timer(interval, callback, arg); |
Deepa Dinamani | dc036ae | 2011-12-20 18:19:52 -0800 | [diff] [blame] | 44 | |
| 45 | exit_critical_section(); |
| 46 | return 0; |
| 47 | } |
| 48 | |
| 49 | time_t current_time(void) |
| 50 | { |
Deepa Dinamani | 1cc9764 | 2012-05-09 11:45:38 -0700 | [diff] [blame] | 51 | return qtimer_current_time(); |
Deepa Dinamani | dc036ae | 2011-12-20 18:19:52 -0800 | [diff] [blame] | 52 | } |
| 53 | |
Deepa Dinamani | 81eddd5 | 2012-05-31 11:18:50 -0700 | [diff] [blame] | 54 | void qtimer_uninit() |
Deepa Dinamani | dc036ae | 2011-12-20 18:19:52 -0800 | [diff] [blame] | 55 | { |
Deepa Dinamani | d642b80 | 2012-05-16 10:49:01 -0700 | [diff] [blame] | 56 | qtimer_disable(); |
Deepa Dinamani | 1cc9764 | 2012-05-09 11:45:38 -0700 | [diff] [blame] | 57 | } |
Deepa Dinamani | dc036ae | 2011-12-20 18:19:52 -0800 | [diff] [blame] | 58 | |
Deepa Dinamani | 1cc9764 | 2012-05-09 11:45:38 -0700 | [diff] [blame] | 59 | /* Blocking function to wait until the specified ticks of the timer. |
| 60 | * Note: ticks to wait for cannot be more than 56 bit. |
| 61 | * Should be sufficient for all practical purposes. |
| 62 | */ |
| 63 | static void delay(uint64_t ticks) |
| 64 | { |
| 65 | volatile uint64_t cnt; |
| 66 | uint64_t init_cnt; |
| 67 | uint64_t timeout = 0; |
Deepa Dinamani | 7d6c897 | 2011-12-14 15:16:56 -0800 | [diff] [blame] | 68 | |
Deepa Dinamani | 1cc9764 | 2012-05-09 11:45:38 -0700 | [diff] [blame] | 69 | cnt = qtimer_get_phy_timer_cnt(); |
| 70 | init_cnt = cnt; |
Deepa Dinamani | dc036ae | 2011-12-20 18:19:52 -0800 | [diff] [blame] | 71 | |
Deepa Dinamani | 1cc9764 | 2012-05-09 11:45:38 -0700 | [diff] [blame] | 72 | /* Calculate timeout = cnt + ticks (mod 2^56) |
| 73 | * to account for timer counter wrapping |
| 74 | */ |
Deepa Dinamani | 1f01f19 | 2012-08-10 16:04:10 -0700 | [diff] [blame] | 75 | timeout = (cnt + ticks) & (uint64_t)(QTMR_PHY_CNT_MAX_VALUE); |
Deepa Dinamani | dc036ae | 2011-12-20 18:19:52 -0800 | [diff] [blame] | 76 | |
Deepa Dinamani | d642b80 | 2012-05-16 10:49:01 -0700 | [diff] [blame] | 77 | /* Wait out till the counter wrapping occurs |
Deepa Dinamani | 1cc9764 | 2012-05-09 11:45:38 -0700 | [diff] [blame] | 78 | * in cases where there is a wrapping. |
| 79 | */ |
| 80 | while(timeout < cnt && init_cnt <= cnt) |
| 81 | /* read global counter */ |
| 82 | cnt = qtimer_get_phy_timer_cnt(); |
| 83 | |
| 84 | /* Wait till the number of ticks is reached*/ |
| 85 | while(timeout > cnt) |
| 86 | /* read global counter */ |
| 87 | cnt = qtimer_get_phy_timer_cnt(); |
Deepa Dinamani | dc036ae | 2011-12-20 18:19:52 -0800 | [diff] [blame] | 88 | |
| 89 | } |
| 90 | |
Deepa Dinamani | 1cc9764 | 2012-05-09 11:45:38 -0700 | [diff] [blame] | 91 | |
Deepa Dinamani | dc036ae | 2011-12-20 18:19:52 -0800 | [diff] [blame] | 92 | void mdelay(unsigned msecs) |
| 93 | { |
Deepa Dinamani | 1cc9764 | 2012-05-09 11:45:38 -0700 | [diff] [blame] | 94 | uint64_t ticks; |
Deepa Dinamani | dc036ae | 2011-12-20 18:19:52 -0800 | [diff] [blame] | 95 | |
Deepa Dinamani | b2a8a8b | 2013-07-18 14:13:08 -0700 | [diff] [blame] | 96 | ticks = ((uint64_t) msecs * ticks_per_sec) / 1000; |
Deepa Dinamani | dc036ae | 2011-12-20 18:19:52 -0800 | [diff] [blame] | 97 | |
Deepa Dinamani | 1cc9764 | 2012-05-09 11:45:38 -0700 | [diff] [blame] | 98 | delay(ticks); |
Deepa Dinamani | dc036ae | 2011-12-20 18:19:52 -0800 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | void udelay(unsigned usecs) |
| 102 | { |
Deepa Dinamani | 1cc9764 | 2012-05-09 11:45:38 -0700 | [diff] [blame] | 103 | uint64_t ticks; |
Deepa Dinamani | dc036ae | 2011-12-20 18:19:52 -0800 | [diff] [blame] | 104 | |
Deepa Dinamani | b2a8a8b | 2013-07-18 14:13:08 -0700 | [diff] [blame] | 105 | ticks = ((uint64_t) usecs * ticks_per_sec) / 1000000; |
Deepa Dinamani | dc036ae | 2011-12-20 18:19:52 -0800 | [diff] [blame] | 106 | |
Deepa Dinamani | 1cc9764 | 2012-05-09 11:45:38 -0700 | [diff] [blame] | 107 | delay(ticks); |
Deepa Dinamani | dc036ae | 2011-12-20 18:19:52 -0800 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | /* Return current time in micro seconds */ |
| 111 | bigtime_t current_time_hires(void) |
| 112 | { |
Deepa Dinamani | 1cc9764 | 2012-05-09 11:45:38 -0700 | [diff] [blame] | 113 | return qtimer_current_time() * 1000000ULL; |
| 114 | } |
| 115 | |
| 116 | void qtimer_init() |
| 117 | { |
| 118 | ticks_per_sec = qtimer_get_frequency(); |
| 119 | } |
| 120 | |
| 121 | uint32_t qtimer_tick_rate() |
| 122 | { |
| 123 | return ticks_per_sec; |
Deepa Dinamani | dc036ae | 2011-12-20 18:19:52 -0800 | [diff] [blame] | 124 | } |