blob: 809335863190925048f854c84186f12201e77d9f [file] [log] [blame]
Ley Foon Tan4eadafc22018-05-24 00:17:29 +08001// SPDX-License-Identifier: GPL-2.0
2/*
Dinesh Maniyam1a1c4e12022-06-01 15:54:59 +08003 * Copyright (C) 2017-2022 Intel Corporation <www.intel.com>
Ley Foon Tan4eadafc22018-05-24 00:17:29 +08004 *
5 */
6
Simon Glass97589732020-05-10 11:40:02 -06007#include <init.h>
Dinesh Maniyam1a1c4e12022-06-01 15:54:59 +08008#include <div64.h>
Ley Foon Tan4eadafc22018-05-24 00:17:29 +08009#include <asm/io.h>
10#include <asm/arch/timer.h>
11
12/*
13 * Timer initialization
14 */
15int timer_init(void)
16{
Chee Hong Ang92dc7ae2020-07-10 23:53:13 +080017#ifdef CONFIG_SPL_BUILD
Ley Foon Tan4eadafc22018-05-24 00:17:29 +080018 int enable = 0x3; /* timer enable + output signal masked */
19 int loadval = ~0;
20
21 /* enable system counter */
22 writel(enable, SOCFPGA_GTIMER_SEC_ADDRESS);
23 /* enable processor pysical counter */
24 asm volatile("msr cntp_ctl_el0, %0" : : "r" (enable));
25 asm volatile("msr cntp_tval_el0, %0" : : "r" (loadval));
Chee Hong Ang92dc7ae2020-07-10 23:53:13 +080026#endif
Ley Foon Tan4eadafc22018-05-24 00:17:29 +080027 return 0;
28}
Dinesh Maniyam1a1c4e12022-06-01 15:54:59 +080029
30__always_inline u64 __get_time_stamp(void)
31{
32 u64 cntpct;
33
34 isb();
35 asm volatile("mrs %0, cntpct_el0" : "=r" (cntpct));
36
37 return cntpct;
38}
39
40__always_inline uint64_t __usec_to_tick(unsigned long usec)
41{
42 u64 tick = usec;
43 u64 cntfrq;
44
45 asm volatile("mrs %0, cntfrq_el0" : "=r" (cntfrq));
46 tick *= cntfrq;
47 do_div(tick, 1000000);
48
49 return tick;
50}
51
52__always_inline void __udelay(unsigned long usec)
53{
54 /* get current timestamp */
55 u64 tmp = __get_time_stamp() + __usec_to_tick(usec);
56
57 while (__get_time_stamp() < tmp + 1) /* loop till event */
58 ;
59}