blob: c7ef131b9e4cbdd76f79687dac40dce4b922f93e [file] [log] [blame]
David Brazdil0f672f62019-12-10 10:32:29 +00001/* SPDX-License-Identifier: GPL-2.0-only */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002/*
3 * Copyright (C) 2012 Regents of the University of California
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004 */
5
6#ifndef _ASM_RISCV_TIMEX_H
7#define _ASM_RISCV_TIMEX_H
8
David Brazdil0f672f62019-12-10 10:32:29 +00009#include <asm/csr.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000010
11typedef unsigned long cycles_t;
12
David Brazdil0f672f62019-12-10 10:32:29 +000013static inline cycles_t get_cycles(void)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000014{
David Brazdil0f672f62019-12-10 10:32:29 +000015 return csr_read(CSR_TIME);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000016}
David Brazdil0f672f62019-12-10 10:32:29 +000017#define get_cycles get_cycles
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000018
19#ifdef CONFIG_64BIT
David Brazdil0f672f62019-12-10 10:32:29 +000020static inline u64 get_cycles64(void)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000021{
David Brazdil0f672f62019-12-10 10:32:29 +000022 return get_cycles();
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000023}
David Brazdil0f672f62019-12-10 10:32:29 +000024#else /* CONFIG_64BIT */
25static inline u32 get_cycles_hi(void)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000026{
David Brazdil0f672f62019-12-10 10:32:29 +000027 return csr_read(CSR_TIMEH);
28}
29
30static inline u64 get_cycles64(void)
31{
32 u32 hi, lo;
33
34 do {
35 hi = get_cycles_hi();
36 lo = get_cycles();
37 } while (hi != get_cycles_hi());
38
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000039 return ((u64)hi << 32) | lo;
40}
David Brazdil0f672f62019-12-10 10:32:29 +000041#endif /* CONFIG_64BIT */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000042
43#define ARCH_HAS_READ_CURRENT_TIMER
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000044static inline int read_current_timer(unsigned long *timer_val)
45{
46 *timer_val = get_cycles();
47 return 0;
48}
49
50#endif /* _ASM_RISCV_TIMEX_H */