forked from zephyrproject-rtos/zephyr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsyscon_common.h
41 lines (33 loc) · 879 Bytes
/
syscon_common.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/*
* Copyright 2021 Google LLC.
* SPDX-License-Identifier: Apache-2.0
*/
#ifndef DRIVERS_SYSCON_SYSCON_COMMON_H_
#define DRIVERS_SYSCON_SYSCON_COMMON_H_
#include <sys/util.h>
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief Align and check register address
*
* @param reg Pointer to the register address in question.
* @param reg_size The size of the syscon register region.
* @param reg_width The width of a single register (in bytes).
* @return 0 if the register read is valid.
* @return -EINVAL is the read is invalid.
*/
static inline int syscon_sanitize_reg(uint16_t *reg, size_t reg_size, uint8_t reg_width)
{
/* Avoid unaligned readings */
*reg = ROUND_DOWN(*reg, reg_width);
/* Check for out-of-bounds readings */
if (*reg >= reg_size) {
return -EINVAL;
}
return 0;
}
#ifdef __cplusplus
}
#endif
#endif /* DRIVERS_SYSCON_SYSCON_COMMON_H_ */