summaryrefslogtreecommitdiffstats
path: root/sys/contrib/alpine-hal/al_hal_reg_utils.h
blob: f29c3c5247b5a2494d43a1a688b79762ece8e546 (plain)
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*-
********************************************************************************
Copyright (C) 2015 Annapurna Labs Ltd.

This file may be licensed under the terms of the Annapurna Labs Commercial
License Agreement.

Alternatively, this file can be distributed under the terms of the GNU General
Public License V2 as published by the Free Software Foundation and can be
found at http://www.gnu.org/licenses/gpl-2.0.html

Alternatively, redistribution and use in source and binary forms, with or
without modification, are permitted provided that the following conditions are
met:

    *     Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.

    *     Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

*******************************************************************************/

/**
 * @defgroup group_common HAL Common Layer
 *  @{
 * @file   al_hal_reg_utils.h
 *
 * @brief  Register utilities used by HALs and platform layer
 *
 *
 */

#ifndef __AL_HAL_REG_UTILS_H__
#define __AL_HAL_REG_UTILS_H__

#include "al_hal_plat_types.h"
#include "al_hal_plat_services.h"

/* *INDENT-OFF* */
#ifdef __cplusplus
extern "C" {
#endif
/* *INDENT-ON* */

#define AL_BIT(b)	(1UL << (b))

#define AL_ADDR_LOW(x)	((uint32_t)((al_phys_addr_t)(x)))
#define AL_ADDR_HIGH(x)	((uint32_t)((((al_phys_addr_t)(x)) >> 16) >> 16))

/** get field out of 32 bit register */
#define AL_REG_FIELD_GET(reg, mask, shift)  (((reg) & (mask)) >> (shift))

/** set field of 32 bit register */
#define AL_REG_FIELD_SET(reg, mask, shift, val)			\
	(reg) =							\
		(((reg) & (~(mask))) |				\
		((((unsigned)(val)) << (shift)) & (mask)))

/** set field of 64 bit register */
#define AL_REG_FIELD_SET_64(reg, mask, shift, val)		\
	((reg) =						\
		(((reg) & (~(mask))) |				\
		((((uint64_t)(val)) << (shift)) & (mask))))

/** get single bit out of 32 bit register */
#define AL_REG_BIT_GET(reg, shift)				\
	AL_REG_FIELD_GET(reg, AL_BIT(shift), shift)

#define AL_REG_BITS_FIELD(shift, val)				\
		(((unsigned)(val)) << (shift))

/** set single bit field of 32 bit register to a given value */
#define AL_REG_BIT_VAL_SET(reg, shift, val)				\
	AL_REG_FIELD_SET(reg, AL_BIT(shift), shift, val)

/** set single bit of 32 bit register to 1 */
#define AL_REG_BIT_SET(reg, shift)				\
	AL_REG_BIT_VAL_SET(reg, shift, 1)

/** clear single bit of 32 bit register */
#define AL_REG_BIT_CLEAR(reg, shift)				\
	AL_REG_BIT_VAL_SET(reg, shift, 0)


#define AL_BIT_MASK(n)						\
	(AL_BIT(n) - 1)

#define AL_FIELD_MASK(msb, lsb)					\
	(AL_BIT(msb) + AL_BIT_MASK(msb) - AL_BIT_MASK(lsb))

/** clear bits specified by clear_mask */
#define AL_REG_MASK_CLEAR(reg, clear_mask)			\
	((reg) = (((reg) & (~(clear_mask)))))

/** set bits specified by clear_mask */
#define AL_REG_MASK_SET(reg, clear_mask)			\
	((reg) = (((reg) | (clear_mask))))


/** clear bits specified by clear_mask, and set bits specified by set_mask */
#define AL_REG_CLEAR_AND_SET(reg, clear_mask, set_mask)			\
	(reg) =	(((reg) & (~(clear_mask))) | (set_mask))

#define AL_ALIGN_UP(val, size)					\
	((size) * (((val) + (size) - 1) / (size)))

/** take bits selected by mask from one data, the rest from background */
#define AL_MASK_VAL(mask, data, background)		\
	(((mask) & (data)) | ((~mask) & (background)))

/**
 * 8 bits register masked write
 *
 * @param	reg
 *	register address
 * @param	mask
 *	bits not selected (1) by mask will be left unchanged
 * @param	data
 *	data to write. bits not selected by mask ignored.
 */
static inline void
al_reg_write8_masked(uint8_t __iomem *reg, uint8_t mask, uint8_t data)
{
	uint8_t temp;
	temp = al_reg_read8(reg);
	al_reg_write8(reg, AL_MASK_VAL(mask, data, temp));
}


/**
 * 16 bits register masked write
 *
 * @param	reg
 *	register address
 * @param	mask
 *	bits not selected (1) by mask will be left unchanged
 * @param	data
 *	data to write. bits not selected by mask ignored.
 */
static inline void
al_reg_write16_masked(uint16_t __iomem *reg, uint16_t mask, uint16_t data)
{
	uint16_t temp;
	temp = al_reg_read16(reg);
	al_reg_write16(reg, AL_MASK_VAL(mask, data, temp));
}


/**
 * 32 bits register masked write
 *
 * @param	reg
 *	register address
 * @param	mask
 *	bits not selected (1) by mask will be left unchanged
 * @param	data
 *	data to write. bits not selected by mask ignored.
 */
static inline void
al_reg_write32_masked(uint32_t __iomem *reg, uint32_t mask, uint32_t data)
{
	uint32_t temp;
	temp = al_reg_read32(reg);
	al_reg_write32(reg, AL_MASK_VAL(mask, data, temp));
}

/* *INDENT-OFF* */
#ifdef __cplusplus
}
#endif
/* *INDENT-ON* */
/** @} end of Common group */
#endif

OpenPOWER on IntegriCloud