Ley Foon Tan | 4eadafc2 | 2018-05-24 00:17:29 +0800 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* |
Dinesh Maniyam | 1a1c4e1 | 2022-06-01 15:54:59 +0800 | [diff] [blame] | 3 | * Copyright (C) 2017-2022 Intel Corporation <www.intel.com> |
Ley Foon Tan | 4eadafc2 | 2018-05-24 00:17:29 +0800 | [diff] [blame] | 4 | * |
| 5 | */ |
| 6 | |
| 7 | #include <common.h> |
Simon Glass | 9758973 | 2020-05-10 11:40:02 -0600 | [diff] [blame] | 8 | #include <init.h> |
Dinesh Maniyam | 1a1c4e1 | 2022-06-01 15:54:59 +0800 | [diff] [blame] | 9 | #include <div64.h> |
Ley Foon Tan | 4eadafc2 | 2018-05-24 00:17:29 +0800 | [diff] [blame] | 10 | #include <asm/io.h> |
| 11 | #include <asm/arch/timer.h> |
| 12 | |
| 13 | /* |
| 14 | * Timer initialization |
| 15 | */ |
| 16 | int timer_init(void) |
| 17 | { |
Chee Hong Ang | 92dc7ae | 2020-07-10 23:53:13 +0800 | [diff] [blame] | 18 | #ifdef CONFIG_SPL_BUILD |
Ley Foon Tan | 4eadafc2 | 2018-05-24 00:17:29 +0800 | [diff] [blame] | 19 | int enable = 0x3; /* timer enable + output signal masked */ |
| 20 | int loadval = ~0; |
| 21 | |
| 22 | /* enable system counter */ |
| 23 | writel(enable, SOCFPGA_GTIMER_SEC_ADDRESS); |
| 24 | /* enable processor pysical counter */ |
| 25 | asm volatile("msr cntp_ctl_el0, %0" : : "r" (enable)); |
| 26 | asm volatile("msr cntp_tval_el0, %0" : : "r" (loadval)); |
Chee Hong Ang | 92dc7ae | 2020-07-10 23:53:13 +0800 | [diff] [blame] | 27 | #endif |
Ley Foon Tan | 4eadafc2 | 2018-05-24 00:17:29 +0800 | [diff] [blame] | 28 | return 0; |
| 29 | } |
Dinesh Maniyam | 1a1c4e1 | 2022-06-01 15:54:59 +0800 | [diff] [blame] | 30 | |
| 31 | __always_inline u64 __get_time_stamp(void) |
| 32 | { |
| 33 | u64 cntpct; |
| 34 | |
| 35 | isb(); |
| 36 | asm volatile("mrs %0, cntpct_el0" : "=r" (cntpct)); |
| 37 | |
| 38 | return cntpct; |
| 39 | } |
| 40 | |
| 41 | __always_inline uint64_t __usec_to_tick(unsigned long usec) |
| 42 | { |
| 43 | u64 tick = usec; |
| 44 | u64 cntfrq; |
| 45 | |
| 46 | asm volatile("mrs %0, cntfrq_el0" : "=r" (cntfrq)); |
| 47 | tick *= cntfrq; |
| 48 | do_div(tick, 1000000); |
| 49 | |
| 50 | return tick; |
| 51 | } |
| 52 | |
| 53 | __always_inline void __udelay(unsigned long usec) |
| 54 | { |
| 55 | /* get current timestamp */ |
| 56 | u64 tmp = __get_time_stamp() + __usec_to_tick(usec); |
| 57 | |
| 58 | while (__get_time_stamp() < tmp + 1) /* loop till event */ |
| 59 | ; |
| 60 | } |