blob: 84b13ce9d3a93529ec20971a806758e086a7305d [file] [log] [blame]
Ley Foon Tan73aede52018-05-24 00:17:29 +08001// SPDX-License-Identifier: GPL-2.0
2/*
Dinesh Maniyam373c1422022-06-01 15:54:59 +08003 * Copyright (C) 2017-2022 Intel Corporation <www.intel.com>
Ley Foon Tan73aede52018-05-24 00:17:29 +08004 *
5 */
6
7#include <common.h>
Simon Glass691d7192020-05-10 11:40:02 -06008#include <init.h>
Dinesh Maniyam373c1422022-06-01 15:54:59 +08009#include <div64.h>
Ley Foon Tan73aede52018-05-24 00:17:29 +080010#include <asm/io.h>
11#include <asm/arch/timer.h>
12
13/*
14 * Timer initialization
15 */
16int timer_init(void)
17{
Chee Hong Ang12cc4482020-07-10 23:53:13 +080018#ifdef CONFIG_SPL_BUILD
Ley Foon Tan73aede52018-05-24 00:17:29 +080019 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 Ang12cc4482020-07-10 23:53:13 +080027#endif
Ley Foon Tan73aede52018-05-24 00:17:29 +080028 return 0;
29}
Dinesh Maniyam373c1422022-06-01 15:54:59 +080030
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}