1 /*
2  * Copyright (c) 2023 Intel Corporation
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #ifndef _SEDI_DRIVER_COMMON_H_
8 #define _SEDI_DRIVER_COMMON_H_
9 
10 #include <stdbool.h>
11 #include <stdint.h>
12 
13 #include "sedi_soc_regs.h"
14 #include "sedi_soc.h"
15 
16 /*!
17  * \defgroup sedi_driver_common Common
18  * \ingroup sedi_driver
19  */
20 
21 #ifndef NULL
22 #define NULL ((void *)0)
23 #endif
24 
25 /*!
26  * \def BIT
27  * \brief Generate a mask with bit x set.
28  * \ingroup sedi_driver_common
29  */
30 
31 #ifndef BIT
32 #define BIT(x) (1U << (x))
33 #endif
34 
35 /*!
36  * \defgroup sedi_driver_common Common
37  * \ingroup sedi_driver
38  */
39 
40 #ifndef TRUE
41 #define TRUE 1
42 #endif
43 
44 /*!
45  * \defgroup sedi_driver_common Common
46  * \ingroup sedi_driver
47  */
48 
49 #ifndef FALSE
50 #define FALSE 0
51 #endif
52 
53 /*!
54  * \defgroup sedi_driver_common Common
55  * \ingroup sedi_driver
56  */
57 
58 #ifndef MS_PER_SEC
59 #define MS_PER_SEC 1000
60 #endif
61 
62 #ifndef NS_PER_US
63 #define NS_PER_US 1000
64 #endif
65 
66 /*!
67  * \def SEDI_DRIVER_VERSION_MAJOR_MINOR
68  * \ingroup sedi_driver_common
69  */
70 #define SEDI_DRIVER_VERSION_MAJOR_MINOR(major, minor) (((major) << 8) | (minor))
71 
72 /*!
73  * \struct sedi_driver_version_t
74  * \brief Driver Version
75  * \ingroup sedi_driver_common
76  */
77 typedef struct {
78 	uint16_t api; /**< API version */
79 	uint16_t drv; /**< Driver version */
80 } sedi_driver_version_t;
81 
82 /*!
83  * \defgroup return_status  Return Status Codes
84  * \ingroup sedi_driver_common
85  * \{
86  */
87 
88 /*!
89  * \def SEDI_DRIVER_OK
90  * \brief Operation succeeded
91  */
92 #define SEDI_DRIVER_OK 0
93 
94 /*!
95  * \def SEDI_DRIVER_ERROR
96  * \brief Unspecified error
97  */
98 #define SEDI_DRIVER_ERROR -1
99 
100 /*!
101  * \def SEDI_DRIVER_ERROR_BUSY
102  * \brief Driver is busy
103  */
104 #define SEDI_DRIVER_ERROR_BUSY -2
105 
106 /*!
107  * \def SEDI_DRIVER_ERROR_TIMEOUT
108  * \brief Timeout occurred
109  */
110 #define SEDI_DRIVER_ERROR_TIMEOUT -3
111 
112 /*!
113  * \def SEDI_DRIVER_ERROR_UNSUPPORTED
114  * \brief Operation not supported
115  */
116 #define SEDI_DRIVER_ERROR_UNSUPPORTED -4
117 
118 /*!
119  * \def SEDI_DRIVER_ERROR_PARAMETER
120  * \brief Parameter error
121  */
122 #define SEDI_DRIVER_ERROR_PARAMETER -5
123 
124 /*!
125  * \def SEDI_DRIVER_ERROR_TRANSFER
126  * \brief transfer error
127  */
128 #define SEDI_DRIVER_ERROR_TRANSFER -6
129 
130 /*!
131  * \def SEDI_DRIVER_ERROR_NO_DEV
132  * \brief Device not available
133  */
134 #define SEDI_DRIVER_ERROR_NO_DEV -7
135 
136 /*!
137  * \def SEDI_DRIVER_ERROR_SPECIFIC
138  * \brief Start of driver specific errors
139  */
140 #define SEDI_DRIVER_ERROR_SPECIFIC -8
141 
142 /*!
143  * \def SEDI_USART_ERROR_CANCELED
144  * \brief Operation was canceled.
145  */
146 #define SEDI_USART_ERROR_CANCELED (SEDI_DRIVER_ERROR_SPECIFIC - 1)
147 
148 /*!
149  * \def SEDI_PM_ERROR_NOMEM
150  * \brief No empty entry for client configurations.
151  */
152 #define SEDI_PM_ERROR_NOMEM (SEDI_DRIVER_ERROR_SPECIFIC - 2)
153 
154 /*!
155  * \def SEDI_PM_ERROR_INTR_PENDING
156  * \brief Interrupt pending to block clock gating.
157  */
158 #define SEDI_PM_ERROR_INTR_PENDING (SEDI_DRIVER_ERROR_SPECIFIC - 3)
159 
160 /*!
161  * \def SEDI_PM_ERROR_CG_ABORT
162  * \brief Clock gating aborted
163  */
164 #define SEDI_PM_ERROR_CG_PG_ABORT (SEDI_DRIVER_ERROR_SPECIFIC - 4)
165 
166 /*!
167  * \def PARAM_UNUSED
168  * \brief Parameter Not Used.
169  */
170 #define PARAM_UNUSED(x) (void)(x)
171 
172 /*!
173  * \def DBG_CHECK
174  * \brief Check error and return for debug mode.
175  */
176 #ifdef DEBUG
177 #define DBG_CHECK(condition, error)                                            \
178 	do {                                                                   \
179 		if (!(condition)) {                                            \
180 			return error;                                          \
181 		}                                                              \
182 	} while (0)
183 #else
184 #define DBG_CHECK(condition, error)
185 #endif
186 
187 /*!
188  * \def SEDI_ASSERT
189  * \brief Assert function for debug mode.
190  */
191 #ifdef DEBUG
192 #define SEDI_ASSERT(condition)                                                 \
193 	do {                                                                   \
194 		if (!(condition)) {                                            \
195 			/* TODO: Add log printf while c lib included */        \
196 			while (1) {                                            \
197 				;                                              \
198 			}                                                      \
199 		}                                                              \
200 	} while (0)
201 #else
202 #define SEDI_ASSERT(condition)
203 #endif
204 
205 /*!
206  * \def SET_MASK
207  * \brief Mask Value for Bit Set.
208  */
209 #define SET_MASK(x) (1UL << (x))
210 
211 /*!
212  * \def SET_MASK
213  * \brief Mask Value for Bit Clear.
214  */
215 #define CLEAR_MASK(x) (~(1UL << (x)))
216 
217 /*!
218  * \def SEDI_ISR_DECLARE
219  * \brief SEDI interrupt handler prototype
220  */
221 #define SEDI_ISR_DECLARE(func) void func(void)
222 
223 /*!
224  * \def SET_BITS
225  * \brief SEDI set certain bit with certain value
226  */
227 
228 #define SET_BITS(reg_name, start, width, value)                                \
229 	do {                                                                   \
230 		uint32_t tmp = 1 << (width);                                   \
231 		reg_name &= ~((tmp - 1) << (start));                           \
232 		reg_name |= ((value) << (start));                              \
233 	} while (0)
234 
235 /*!
236  * \def GET_BITS
237  * \brief SEDI get value from certain bit
238  */
239 
240 #define GET_BITS(reg_name, start, width)                                       \
241 	((reg_name) & (((1<<(width)) - 1) << (start)))
242 
243 /*!
244  * \}
245  */
246 
247 /*!
248  * \struct sedi_power_state_t
249  * \brief General power states
250  * \ingroup sedi_driver_common
251  */
252 typedef enum {
253 	/**< Power off: no operation possible */
254 	SEDI_POWER_OFF,
255 	/**< Suspend: context should be saved and restored by driver */
256 	SEDI_POWER_SUSPEND,
257 	/**< Force suspend: complete ongoing operation before suspend */
258 	SEDI_POWER_FORCE_SUSPEND,
259 	/**< Low Power mode: retain state, detect and signal wake-up events */
260 	SEDI_POWER_LOW,
261 	/**< Power on: full operation at maximum performance */
262 	SEDI_POWER_FULL
263 } sedi_power_state_t;
264 
265 #ifndef __IO_R
266 /*!
267  * \def __IO_R
268  * \brief 'read only' permissions
269  * \ingroup sedi_driver_common
270  */
271 #define __IO_R volatile const
272 #endif
273 
274 #ifndef __IO_W
275 /*!
276  * \def __IO_W
277  * \brief 'write only' permissions
278  * \ingroup sedi_driver_common
279  */
280 #define __IO_W volatile
281 #endif
282 
283 #ifndef __IO_RW
284 /*!
285  * \def __IO_RW
286  * \brief 'read / write' permissions
287  * \ingroup sedi_driver_common
288  */
289 #define __IO_RW volatile
290 #endif
291 
292 #ifndef IN
293 /*!
294  * \def IN
295  * \brief Input parameter indicator
296  * \ingroup sedi_driver_common
297  */
298 #define IN const
299 #endif
300 
301 #ifndef OUT
302 /*!
303  * \def OUT
304  * \brief Output parameter indicator
305  * \ingroup sedi_driver_common
306  */
307 #define OUT
308 #endif
309 
310 #ifndef INOUT
311 /*!
312  * \def  INOUT
313  * \brief Input/Output parameter indicator
314  * \ingroup sedi_driver_common
315  */
316 #define INOUT
317 #endif
318 
319 /*!
320  * \function read/write address
321  * \ingroup sedi_driver_common
322  */
323 /* register read/write help function */
read8(IN uint32_t addr)324 static inline uint8_t read8(IN uint32_t addr)
325 {
326 	return *(const volatile uint8_t *)addr;
327 }
328 
read16(IN uint32_t addr)329 static inline uint16_t read16(IN uint32_t addr)
330 {
331 	return *(const volatile uint16_t *)addr;
332 }
333 
read32(IN uint32_t addr)334 static inline uint32_t read32(IN uint32_t addr)
335 {
336 	return *(const volatile uint32_t *)addr;
337 }
338 
339 /* Note - 64 bit function isn't atomic */
read64(IN uint32_t addr)340 static inline uint64_t read64(IN uint32_t addr)
341 {
342 	return *(const volatile uint64_t *)addr;
343 }
344 
write8(uint32_t addr,IN uint8_t val)345 static inline void write8(uint32_t addr, IN uint8_t val)
346 {
347 	*(volatile uint8_t *)addr = (uint8_t)val;
348 }
349 
write16(uint32_t addr,IN uint16_t val)350 static inline void write16(uint32_t addr, IN uint16_t val)
351 {
352 	*(volatile uint16_t *)addr = (uint16_t)val;
353 }
354 
write32(uint32_t addr,IN uint32_t val)355 static inline void write32(uint32_t addr, IN uint32_t val)
356 {
357 	*(volatile uint32_t *)addr = val;
358 }
359 
360 /* Note - 64 bit function isn't atomic */
write64(uint32_t addr,IN uint64_t val)361 static inline void write64(uint32_t addr, IN uint64_t val)
362 {
363 	*(volatile uint64_t *)addr = val;
364 }
365 
366 #define SET_REG_BIT(address, smask)                                           \
367 	write32(address, read32(address) | (smask))
368 
369 #define CLEAR_REG_BIT(address, cmask)                                         \
370 	write32(address, read32(address) & ~(cmask))
371 
372 #endif /* _SEDI_DRIVER_COMMON_H_*/
373