1 /* SPDX-License-Identifier: BSD-3-Clause
2 *
3 * Copyright(c) 2018 Intel Corporation. All rights reserved.
4 *
5 * Author: Janusz Jankowski <janusz.jankowski@linux.intel.com>
6 */
7
8 #ifndef __SOF_DRIVERS_TIMER_H__
9 #define __SOF_DRIVERS_TIMER_H__
10
11 #include <arch/drivers/timer.h>
12 #include <sof/lib/cpu.h>
13 #include <sof/sof.h>
14 #include <stdint.h>
15
16 struct comp_dev;
17 struct sof_ipc_stream_posn;
18
19 #define TIMER0 0
20 #define TIMER1 1
21 #define TIMER2 2
22 #define TIMER3 3
23 #define TIMER4 4
24
25 int timer_register(struct timer *timer, void (*handler)(void *arg), void *arg);
26 void timer_unregister(struct timer *timer, void *arg);
27 void timer_enable(struct timer *timer, void *arg, int core);
28 void timer_disable(struct timer *timer, void *arg, int core);
29
timer_get(void)30 static inline struct timer *timer_get(void)
31 {
32 return sof_get()->platform_timer;
33 }
34
cpu_timer_get(void)35 static inline struct timer *cpu_timer_get(void)
36 {
37 return &(sof_get()->cpu_timers[cpu_get_id()]);
38 }
39
timer_set(struct timer * timer,uint64_t ticks)40 static inline int64_t timer_set(struct timer *timer, uint64_t ticks)
41 {
42 return arch_timer_set(timer, ticks);
43 }
44
45 void timer_set_ms(struct timer *timer, unsigned int ms);
46
timer_clear(struct timer * timer)47 static inline void timer_clear(struct timer *timer)
48 {
49 arch_timer_clear(timer);
50 }
51
52 unsigned int timer_get_count(struct timer *timer);
53
54 unsigned int timer_get_count_delta(struct timer *timer);
55
timer_get_system(struct timer * timer)56 static inline uint64_t timer_get_system(struct timer *timer)
57 {
58 return arch_timer_get_system(timer);
59 }
60
61 int64_t platform_timer_set(struct timer *timer, uint64_t ticks);
62 void platform_timer_clear(struct timer *timer);
63 uint64_t platform_timer_get(struct timer *timer);
64 uint64_t platform_timer_get_atomic(struct timer *timer);
65
platform_safe_get_time(struct timer * timer)66 static inline uint64_t platform_safe_get_time(struct timer *timer)
67 {
68 /* Default to something small but at least 1.0 microsecond so it
69 * does not look like an uninitialized zero; not even when the
70 * user does not request any microseconds decimals. See
71 * DEFAULT_CLOCK constant in logger.c
72 */
73 return timer ? platform_timer_get(timer) : 50;
74 }
75
76 void platform_timer_start(struct timer *timer);
77 void platform_timer_stop(struct timer *timer);
78
79 /* get timestamp for host stream DMA position */
80 void platform_host_timestamp(struct comp_dev *host,
81 struct sof_ipc_stream_posn *posn);
82
83 /* get timestamp for DAI stream DMA position */
84 void platform_dai_timestamp(struct comp_dev *dai,
85 struct sof_ipc_stream_posn *posn);
86
87 /* get current wallclock for componnent */
88 void platform_dai_wallclock(struct comp_dev *dai, uint64_t *wallclock);
89
90 #endif /* __SOF_DRIVERS_TIMER_H__ */
91