1 /*
2  * Copyright (c) 2023 Intel Corporation
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #ifndef _SEDI_DRIVER_HPET_H_
8 #define _SEDI_DRIVER_HPET_H_
9 
10 #ifdef __cplusplus
11 extern "C" {
12 #endif
13 
14 #include "sedi_driver_common.h"
15 
16 /*!
17  * \defgroup sedi_driver_hpet HPET
18  * \ingroup sedi_driver
19  */
20 
21 #define SEDI_HPET_API_VERSION SEDI_DRIVER_VERSION_MAJOR_MINOR(0, 1)
22 
23 /*!
24  * \defgroup sedi_sideband_t HPET instance id
25  * \ingroup sedi_driver_hpet
26  */
27 typedef enum {
28 	HPET_0 = 0,
29 	HPET_1,
30 	HPET_2,
31 	HPET_NUM,
32 } sedi_hpet_t;
33 
34 /*!
35  * \struct sedi_hpet_capabilities_t
36  * \brief HPET Driver Capabilities.
37  * \ingroup sedi_driver_hpet
38  */
39 typedef struct sedi_hpet_capabilities {
40 	uint32_t reserved;
41 } sedi_hpet_capabilities_t;
42 
43 /*!
44  * \defgroup hpet_function_calls HEPT Driver Function Calls
45  * \ingroup sedi_driver_hpet
46  * \{
47  */
48 
49 /*!
50  * \brief Hpet callback function.
51  * \param param User callback parameter.
52  */
53 typedef int (*hpet_callback_t)(void *param);
54 
55 /*!
56  * \brief Get the hpet driver's API version.
57  * \return the version of current hpet driver's API
58  */
59 sedi_driver_version_t sedi_hpet_get_version(void);
60 
61 /*!
62  * \brief Get the device's capabilities.
63  * \return the capabilities of hpet device
64  */
65 sedi_hpet_capabilities_t sedi_hpet_get_capabilities(void);
66 
67 /*!
68  * \brief Get HPET's current setting of minimal delay time.
69  * \return HPET's current setting of minimal delay time.
70  */
71 uint32_t sedi_hpet_get_min_delay(void);
72 
73 /*!
74  * \brief Set HPET's minimal delay time. The comparator value must be set
75  *	farther than this value, ahead of the current main counter value.
76  *	Or interrupt might be missed due to the hardware latency
77  * \param[in] min_delay: SoC-specific HPET minimal delay time.
78  */
79 void sedi_hpet_set_min_delay(uint32_t min_delay);
80 
81 /*!
82  * \brief Initialize the device
83  * \param[in] clk_divisor: the clock divisor to set.
84  * \param[in] min_delay: SoC-specific HPET minimal delay time to set.
85  * \return  \ref return_status
86  */
87 int32_t sedi_hpet_init(uint32_t clk_divisor, uint32_t min_delay);
88 
89 /*!
90  * \brief Uninitialize the device
91  * \return  \ref return_status
92  */
93 int32_t sedi_hpet_uninit(void);
94 
95 /*!
96  * \brief Set the device's power
97  * \param[in] state: the power state to be set to the device
98  * \return  \ref return_status
99  */
100 int32_t sedi_hpet_set_power(IN sedi_power_state_t state);
101 
102 /*!
103  * \brief Set the timer's comparator. This means when to trigger an interrupt.
104  * \param[in] timer_id: Timer ID to set.
105  * \param[in] value: The value need to set.
106  * \return  \ref return_status
107  */
108 int sedi_hpet_set_comparator(IN sedi_hpet_t timer_id, IN uint64_t value);
109 
110 /*!
111  * \brief Set the timer's main counter to the new value.
112  * \param[in] value: The value need to set.
113  */
114 void sedi_hpet_set_main_counter(uint64_t value);
115 
116 /*!
117  * \brief Get the timer's current value of main counter.
118  * \return The current value of main counter.
119  */
120 uint64_t sedi_hpet_get_main_counter(void);
121 
122 uint64_t sedi_hpet_get_us(void);
123 
124 /*!
125  * \brief Enable the timer's interrupt.
126  * \param[in] timer_id: Timer ID to enable interrupt.
127  */
128 void sedi_hpet_enable_interrupt(IN sedi_hpet_t timer_id);
129 
130 /*!
131  * \brief Disable the timer's interrupt.
132  * \param[in] timer_id: Timer ID to enable interrupt.
133  */
134 void sedi_hpet_disable_interrupt(IN sedi_hpet_t timer_id);
135 
136 /*!
137  * \brief Get the interrupt status.
138  * \return the current value of the interrupt status.
139  */
140 uint32_t sedi_hpet_get_int_status(void);
141 
142 /*!
143  * \brief Set value to the interrupt status to clear.
144  * \param[in] val: val to set to the interrupt status.
145  */
146 void sedi_hpet_set_int_status(IN uint32_t val);
147 
148 /*!
149  * \brief Handle the interrupt of HPET Timer N
150  * \param[in] timer_id: Timer ID to handle.
151  */
152 void sedi_hpet_timer_int_handler(IN sedi_hpet_t timer_id);
153 
154 /*!
155  * \brief Get the timer's period, specified in picoseconds.
156  * \1000000 ps: (1 MHz clock period)
157  * \83333 ps: (12 MHz clock period)
158  * \30517578 ps: (32.768 KHz clock period)
159  * \return the value of period.
160  */
161 uint32_t sedi_hpet_get_period(void);
162 
163 /*!
164  * \brief initialize one timer
165  * \param[in] timer_id Timer index to be configured
166  * \param[in] microseconds: the cycle of timer
167  * \param[in] callback: callback function when timeout
168  * \param[in] param: callback parameters
169  * \param[in] one_shot: if it is a one_shot timer
170  * \return fail reason
171  */
172 int32_t sedi_hpet_config_timer(IN sedi_hpet_t timer_id,
173 			       IN uint64_t microseconds,
174 			       IN hpet_callback_t callback, IN void *param,
175 			       IN bool one_shot);
176 
177 /*!
178  * \brief start the timer
179  * \param[in] timer_id: the timer's id
180  * \return fail reason
181  */
182 int32_t sedi_hpet_start_timer(IN sedi_hpet_t timer_id);
183 
184 /*!
185  * \brief kill the timer
186  * \param[in] timer_id: the timer's id
187  * \return fail reason
188  */
189 int32_t sedi_hpet_kill_timer(IN sedi_hpet_t timer_id);
190 
191 /*!
192  * \}
193  */
194 
195 #ifdef __cplusplus
196 }
197 #endif
198 
199 #endif /* _SEDI_DRIVER_HPET_H_*/
200