Patrick Georgi | 7051707 | 2020-05-10 18:47:05 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: BSD-3-Clause */ |
Varadarajan Narayanan | a6935c2 | 2016-03-02 16:57:10 +0530 | [diff] [blame] | 2 | |
Kyösti Mälkki | 13f6650 | 2019-03-03 08:01:05 +0200 | [diff] [blame] | 3 | #include <device/mmio.h> |
Varadarajan Narayanan | a6935c2 | 2016-03-02 16:57:10 +0530 | [diff] [blame] | 4 | #include <delay.h> |
| 5 | #include <soc/iomap.h> |
| 6 | #include <soc/ipq_timer.h> |
| 7 | #include <timer.h> |
| 8 | |
Varadarajan Narayanan | 9541ba8 | 2016-03-03 13:30:07 +0530 | [diff] [blame] | 9 | #define GCNT_FREQ_MHZ 48 |
Varadarajan Narayanan | a6935c2 | 2016-03-02 16:57:10 +0530 | [diff] [blame] | 10 | |
Varadarajan Narayanan | 9541ba8 | 2016-03-03 13:30:07 +0530 | [diff] [blame] | 11 | #define TIMER_TICKS(us) (GCNT_FREQ_MHZ * (us)) |
| 12 | #define TIMER_USECS(ticks) ((ticks) / GCNT_FREQ_MHZ) |
Varadarajan Narayanan | a6935c2 | 2016-03-02 16:57:10 +0530 | [diff] [blame] | 13 | |
| 14 | /** |
| 15 | * init_timer - initialize timer |
| 16 | */ |
| 17 | void init_timer(void) |
| 18 | { |
| 19 | /* disable timer */ |
Varadarajan Narayanan | 9541ba8 | 2016-03-03 13:30:07 +0530 | [diff] [blame] | 20 | write32(GCNT_CNTCR, 0); |
Varadarajan Narayanan | a6935c2 | 2016-03-02 16:57:10 +0530 | [diff] [blame] | 21 | |
Varadarajan Narayanan | 9541ba8 | 2016-03-03 13:30:07 +0530 | [diff] [blame] | 22 | /* Reset the counters to zero */ |
| 23 | write32(GCNT_GLB_CNTCV_LO, 0); |
| 24 | write32(GCNT_GLB_CNTCV_HI, 0); |
Varadarajan Narayanan | a6935c2 | 2016-03-02 16:57:10 +0530 | [diff] [blame] | 25 | |
| 26 | /* Enable timer */ |
Varadarajan Narayanan | 9541ba8 | 2016-03-03 13:30:07 +0530 | [diff] [blame] | 27 | write32(GCNT_CNTCR, 1); |
| 28 | } |
| 29 | |
| 30 | static inline uint64_t read_gcnt_val(void) |
| 31 | { |
| 32 | uint32_t hi, lo; |
| 33 | |
| 34 | do { |
| 35 | hi = read32(GCNT_CNTCV_HI); |
| 36 | lo = read32(GCNT_CNTCV_LO); |
| 37 | } while (hi != read32(GCNT_CNTCV_HI)); |
| 38 | |
| 39 | return ((((uint64_t)hi) << 32) | lo); |
Varadarajan Narayanan | a6935c2 | 2016-03-02 16:57:10 +0530 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | /** |
| 43 | * udelay - generates micro second delay. |
| 44 | * @param usec: delay duration in microseconds |
| 45 | */ |
Martin Roth | 57e8909 | 2019-10-23 21:45:23 -0600 | [diff] [blame] | 46 | void udelay(unsigned int usec) |
Varadarajan Narayanan | a6935c2 | 2016-03-02 16:57:10 +0530 | [diff] [blame] | 47 | { |
Varadarajan Narayanan | 9541ba8 | 2016-03-03 13:30:07 +0530 | [diff] [blame] | 48 | uint64_t expire; |
Varadarajan Narayanan | a6935c2 | 2016-03-02 16:57:10 +0530 | [diff] [blame] | 49 | |
Varadarajan Narayanan | 9541ba8 | 2016-03-03 13:30:07 +0530 | [diff] [blame] | 50 | expire = read_gcnt_val() + TIMER_TICKS(usec); |
Varadarajan Narayanan | a6935c2 | 2016-03-02 16:57:10 +0530 | [diff] [blame] | 51 | |
Varadarajan Narayanan | 9541ba8 | 2016-03-03 13:30:07 +0530 | [diff] [blame] | 52 | while (expire >= read_gcnt_val()) |
| 53 | ; |
Varadarajan Narayanan | a6935c2 | 2016-03-02 16:57:10 +0530 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | void timer_monotonic_get(struct mono_time *mt) |
| 57 | { |
Varadarajan Narayanan | 9541ba8 | 2016-03-03 13:30:07 +0530 | [diff] [blame] | 58 | mono_time_set_usecs(mt, TIMER_USECS(read_gcnt_val())); |
Varadarajan Narayanan | a6935c2 | 2016-03-02 16:57:10 +0530 | [diff] [blame] | 59 | } |