-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgpio_oem.cpp
312 lines (266 loc) · 7.66 KB
/
gpio_oem.cpp
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <unistd.h>
#include <ipmid/api-types.hpp>
#include <phosphor-logging/log.hpp>
#ifdef SOC_NPCM8XX
#include "gpio_oem_npcm8xx.hpp"
#else
#include "gpio_oem.hpp"
#endif
namespace ipmi
{
namespace nuvoton
{
using namespace phosphor::logging;
uint64_t devmem_read(off_t target, unsigned width, bool &ret)
{
void *map_base, *virt_addr;
uint64_t read_result;
unsigned page_size, mapped_size, offset_in_page;
int fd;
ret = true;
fd = open("/dev/mem", (O_RDONLY | O_SYNC));
if(-1 == fd)
{
ret = false;
return 0;
}
mapped_size = page_size = getpagesize();
offset_in_page = (unsigned)target & (page_size - 1);
if (offset_in_page + width > page_size) {
mapped_size *= 2;
}
map_base = mmap(NULL, mapped_size, PROT_READ, MAP_SHARED, fd, target & ~(off_t)(page_size - 1));
if (MAP_FAILED == map_base)
{
ret = false;
close(fd);
return 0;
}
virt_addr = (char*)map_base + offset_in_page;
switch (width) {
case 8:
read_result = *(volatile uint8_t*)virt_addr;
break;
case 16:
read_result = *(volatile uint16_t*)virt_addr;
break;
case 32:
read_result = *(volatile uint32_t*)virt_addr;
break;
case 64:
read_result = *(volatile uint64_t*)virt_addr;
break;
default:
read_result = 0;
ret = false;
}
if (-1 == munmap(map_base, mapped_size))
{
ret = false;
close(fd);
return 0;
}
close(fd);
return read_result;
}
void show_multifn_select(uint32_t reg, uint32_t value,uint8_t maskBit)
{
std::string msg;
char r_value[32];
switch (reg)
{
case MFSEL1:
msg = "MFSEL1";
break;
case MFSEL2:
msg = "MFSEL2";
break;
case MFSEL3:
msg = "MFSEL3";
break;
case MFSEL4:
msg = "MFSEL4";
break;
#ifdef SOC_NPCM8XX
case MFSEL5:
msg = "MFSEL5";
break;
case MFSEL6:
msg = "MFSEL6";
break;
case MFSEL7:
msg = "MFSEL7";
break;
case I2CSEGSEL:
msg = "I2CSEGSEL";
break;
#endif
default:
msg = "ERROR";
break;
}
sprintf(r_value, "0x%08X, bit: 0x%X", value, maskBit);
msg += ", reg val:" + std::string(r_value);
log<level::DEBUG>(msg.c_str());
}
bool isGP (uint8_t pinNum)
{
uint8_t loop;
uint8_t mask = 0;
bool isGpio = false, ret;
uint64_t read_result = devmem_read( (gpioLUT[pinNum].reg), LENGTH_32BIT, ret);
std::string msg = std::to_string(pinNum);
if (!ret)
{
return false;
}
// make the bit mask. The bit number might be greater than 1.
for(loop = 0; loop < gpioLUT[pinNum].maskBit; loop++)
{
mask += (BIT_MASK << loop);
}
// check if the pin's value in MFSELx matches refValue
// If so, it means that the pin is or might be configured as a gpio.
if(gpioLUT[pinNum].refValue == ( ((uint32_t)read_result) &
(mask<<gpioLUT[pinNum].offSet) )>>gpioLUT[pinNum].offSet )
{
isGpio = true;
// if the pin needs to check the second MFSELx or the third MFSELx
// configuration in the look up table
struct gpio_lookup *next = gpioLUT[pinNum].next;
while(NULL != next)
{
struct gpio_lookup gl = *next;
read_result = devmem_read( gl.reg, LENGTH_32BIT, ret);
if (!ret)
{
isGpio = false;
break;
}
mask = 0;
for(loop = 0; loop < gl.maskBit; loop++)
{
mask += (BIT_MASK << loop);
}
if(gl.refValue == ( ( ((uint32_t)read_result) &
(mask<<gl.offSet) )>>gl.offSet ) )
isGpio = true;
else
{
isGpio = false;
show_multifn_select(gl.reg, (uint32_t)read_result, gl.maskBit);
break;
}
next = gl.next;
}
}
if (!isGpio)
{
msg = "pin function is set, not GPIO, " + msg;
show_multifn_select(gpioLUT[pinNum].reg, (uint32_t)read_result, gpioLUT[pinNum].maskBit);
}
else
{
if (gpioLUT[pinNum].refValue)
msg = "GPIO function is set, " + msg;
else
msg = "pin function is not set, " + msg;
}
log<level::DEBUG>(msg.c_str());
return isGpio;
}
bool dumpGP (uint8_t pinNum, uint8_t &direction, uint8_t &level)
{
bool ret;
uint64_t read_result = devmem_read( (GPIO0 + GPIO_OFFSET*(pinNum/GPIO_NUM_IN_GROUP) + GP_OE_OFFSET), LENGTH_32BIT, ret);
if (!ret)
{
log<level::DEBUG>("devmem_read failed, check OE");
return false;
}
uint8_t offSet = pinNum % GPIO_NUM_IN_GROUP;
// check if it's output-enabled
direction = ( ((uint32_t)read_result) & (BIT_MASK<<offSet) )>>offSet;
if (direction)
{
read_result = devmem_read( (GPIO0 + GPIO_OFFSET*(pinNum/GPIO_NUM_IN_GROUP) + GP_DOUT_OFFSET), LENGTH_32BIT, ret);
if (!ret)
{
log<level::DEBUG>("devmem_read failed, check DOUT");
return false;
}
level = ( ((uint32_t)read_result) & (BIT_MASK<<offSet) )>>offSet;
return true;
}
// check if it's input-enabled
read_result = devmem_read( (GPIO0 + GPIO_OFFSET*(pinNum/GPIO_NUM_IN_GROUP) + GP_IEM_OFFSET), LENGTH_32BIT, ret);
if (!ret)
{
log<level::DEBUG>("devmem_read failed, check IEM");
return false;
}
direction = ( ((uint32_t)read_result) & (BIT_MASK<<offSet) )>>offSet;
if (direction)
{
direction = GP_DIR_INPUT;
read_result = devmem_read( (GPIO0 + GPIO_OFFSET*(pinNum/GPIO_NUM_IN_GROUP) + GP_DIN_OFFSET), LENGTH_32BIT, ret);
if (!ret)
{
log<level::DEBUG>("devmem_read failed, check DIN");
return false;
}
level = ( ((uint32_t)read_result) & (BIT_MASK<<offSet) )>>offSet;
return true;
}
std::string msg = "not OE/IEM! pin may not set to GPIO, " + std::to_string(pinNum);
log<level::DEBUG>(msg.c_str());
return false;
}
ipmi::RspType<uint8_t, //direction
uint8_t //high/low
>
ipmiOEMGetGpioStatus (uint8_t pinNum)
{
uint8_t direction = 0;
uint8_t level = 0;
std::string msg = std::to_string(pinNum);
if (GPIO_NUM <= pinNum)
{
msg = "Invalid pin number:" + msg;
log<level::DEBUG>(msg.c_str());
return ipmi::responseParmOutOfRange();
}
if (DEF_NONE == gpioLUT[pinNum].refValue)
{
msg = "Invalid pin value, pin:" + msg;
log<level::DEBUG>(msg.c_str());
return ipmi::responseResponseError();
}
if (DEF_GPIO == gpioLUT[pinNum].refValue)
{
if(!dumpGP(pinNum, direction, level))
{
msg = "Dump pin value fail: " + msg;
log<level::DEBUG>(msg.c_str());
return ipmi::responseResponseError();
}
return ipmi::responseSuccess(direction, level);
}
else
{
// Check if the pin is used as a gpio or something else.
// If it's a gpio, then the direction and level attributes of that
// pin is retrieved.
if( isGP(pinNum) && dumpGP(pinNum, direction, level) )
return ipmi::responseSuccess(direction, level);
return ipmi::responseResponseError();
}
msg = "fallback case, should never get in, " + msg;
log<level::DEBUG>(msg.c_str());
return ipmi::responseResponseError();
}
} // namespace nuvoton
} // namespace ipmi